Blame SOURCES/autofs-5.0.8-dont-clobber-mapent-for-negative-cache.patch

304803
autofs-5.0.8 - dont clobber mapent for negative cache
304803
304803
From: Ian Kent <raven@themaw.net>
304803
304803
When negative caching a map entry on mount fail don't save the mapent
304803
and restore it when the negative cache timeout expires.
304803
304803
Deleting the mapent, as is done now, can be expensive especially when
304803
it causes a file read for a large file map.
304803
---
304803
 daemon/lookup.c          |    6 ++-
304803
 include/automount.h      |    9 ++++
304803
 lib/cache.c              |   85 ++++++++++++++++++++++++++++++++++++++++++++++-
304803
 modules/lookup_file.c    |    6 ++-
304803
 modules/lookup_hosts.c   |   10 +++--
304803
 modules/lookup_ldap.c    |    6 ++-
304803
 modules/lookup_nisplus.c |   10 +++--
304803
 modules/lookup_program.c |   10 +++--
304803
 modules/lookup_sss.c     |    6 ++-
304803
 modules/lookup_yp.c      |    6 ++-
304803
 10 files changed, 135 insertions(+), 19 deletions(-)
304803
304803
--- autofs-5.0.7.orig/daemon/lookup.c
304803
+++ autofs-5.0.7/daemon/lookup.c
304803
@@ -860,7 +860,11 @@ static void update_negative_cache(struct
304803
 			int rv = CHE_FAIL;
304803
 
304803
 			cache_writelock(map->mc);
304803
-			rv = cache_update(map->mc, map, name, NULL, now);
304803
+			me = cache_lookup_distinct(map->mc, name);
304803
+			if (me)
304803
+				rv = cache_push_mapent(me, NULL);
304803
+			else
304803
+				rv = cache_update(map->mc, map, name, NULL, now);
304803
 			if (rv != CHE_FAIL) {
304803
 				me = cache_lookup_distinct(map->mc, name);
304803
 				me->status = now + ap->negative_timeout;
304803
--- autofs-5.0.7.orig/include/automount.h
304803
+++ autofs-5.0.7/include/automount.h
304803
@@ -146,6 +146,12 @@ struct mapent_cache {
304803
 	struct mapent **hash;
304803
 };
304803
 
304803
+struct stack {
304803
+	char *mapent;
304803
+	time_t age;
304803
+	struct stack *next;
304803
+};
304803
+
304803
 struct mapent {
304803
 	struct mapent *next;
304803
 	struct list_head ino_index;
304803
@@ -159,6 +165,7 @@ struct mapent {
304803
 	struct mapent *parent;
304803
 	char *key;
304803
 	char *mapent;
304803
+	struct stack *stack;
304803
 	time_t age;
304803
 	/* Time of last mount fail */
304803
 	time_t status;
304803
@@ -175,6 +182,8 @@ void cache_readlock(struct mapent_cache
304803
 void cache_writelock(struct mapent_cache *mc);
304803
 int cache_try_writelock(struct mapent_cache *mc);
304803
 void cache_unlock(struct mapent_cache *mc);
304803
+int cache_push_mapent(struct mapent *me, char *mapent);
304803
+int cache_pop_mapent(struct mapent *me);
304803
 struct mapent_cache *cache_init(struct autofs_point *ap, struct map_source *map);
304803
 struct mapent_cache *cache_init_null_cache(struct master *master);
304803
 int cache_set_ino_index(struct mapent_cache *mc, const char *key, dev_t dev, ino_t ino);
304803
--- autofs-5.0.7.orig/lib/cache.c
304803
+++ autofs-5.0.7/lib/cache.c
304803
@@ -177,6 +177,69 @@ static inline void ino_index_unlock(stru
304803
 	return;
304803
 }
304803
 
304803
+/* Save the cache entry mapent field onto a stack and set a new mapent */
304803
+int cache_push_mapent(struct mapent *me, char *mapent)
304803
+{
304803
+	struct stack *s;
304803
+	char *new;
304803
+
304803
+	if (!me->mapent)
304803
+		return CHE_FAIL;
304803
+
304803
+	if (!mapent)
304803
+		new = NULL;
304803
+	else {
304803
+		new = strdup(mapent);
304803
+		if (!new)
304803
+			return CHE_FAIL;
304803
+	}
304803
+
304803
+	s = malloc(sizeof(struct stack));
304803
+	if (!s) {
304803
+		if (new)
304803
+			free(new);
304803
+		return CHE_FAIL;
304803
+	}
304803
+	memset(s, 0, sizeof(*s));
304803
+
304803
+	s->mapent = me->mapent;
304803
+	s->age = me->age;
304803
+	me->mapent = mapent;
304803
+
304803
+	if (me->stack)
304803
+		s->next = me->stack;
304803
+	me->stack = s;
304803
+
304803
+	return CHE_OK;
304803
+}
304803
+
304803
+/* Restore cache entry mapent to a previously saved mapent, discard current */
304803
+int cache_pop_mapent(struct mapent *me)
304803
+{
304803
+	struct stack *s = me->stack;
304803
+	char *mapent;
304803
+	time_t age;
304803
+
304803
+	if (!s || !s->mapent)
304803
+		return CHE_FAIL;
304803
+
304803
+	mapent = s->mapent;
304803
+	age = s->age;
304803
+	me->stack = s->next;
304803
+	free(s);
304803
+
304803
+	if (age < me->age) {
304803
+		free(mapent);
304803
+		return CHE_OK;
304803
+	}
304803
+
304803
+	if (me->mapent)
304803
+		free(me->mapent);
304803
+	me->mapent = mapent;
304803
+
304803
+	return CHE_OK;
304803
+}
304803
+
304803
 struct mapent_cache *cache_init(struct autofs_point *ap, struct map_source *map)
304803
 {
304803
 	struct mapent_cache *mc;
304803
@@ -578,6 +641,8 @@ int cache_add(struct mapent_cache *mc, s
304803
 	} else
304803
 		me->mapent = NULL;
304803
 
304803
+	me->stack = NULL;
304803
+
304803
 	me->age = age;
304803
 	me->status = 0;
304803
 	me->mc = mc;
304803
@@ -689,7 +754,9 @@ void cache_update_negative(struct mapent
304803
 	int rv = CHE_OK;
304803
 
304803
 	me = cache_lookup_distinct(mc, key);
304803
-	if (!me)
304803
+	if (me)
304803
+		rv = cache_push_mapent(me, NULL);
304803
+	else
304803
 		rv = cache_update(mc, ms, key, NULL, now);
304803
 	if (rv != CHE_FAIL) {
304803
 		me = cache_lookup_distinct(mc, key);
304803
@@ -858,6 +925,7 @@ int cache_delete(struct mapent_cache *mc
304803
 		pred = me;
304803
 		me = me->next;
304803
 		if (strcmp(this, me->key) == 0) {
304803
+			struct stack *s = me->stack;
304803
 			if (me->multi && !list_empty(&me->multi_list)) {
304803
 				ret = CHE_FAIL;
304803
 				goto done;
304803
@@ -872,6 +940,13 @@ int cache_delete(struct mapent_cache *mc
304803
 			free(me->key);
304803
 			if (me->mapent)
304803
 				free(me->mapent);
304803
+			while (s) {
304803
+				struct stack *next = s->next;
304803
+				if (s->mapent)
304803
+					free(s->mapent);
304803
+				free(s);
304803
+				s = next;
304803
+			}
304803
 			free(me);
304803
 			me = pred;
304803
 		}
304803
@@ -882,6 +957,7 @@ int cache_delete(struct mapent_cache *mc
304803
 		goto done;
304803
 
304803
 	if (strcmp(this, me->key) == 0) {
304803
+		struct stack *s = me->stack;
304803
 		if (me->multi && !list_empty(&me->multi_list)) {
304803
 			ret = CHE_FAIL;
304803
 			goto done;
304803
@@ -896,6 +972,13 @@ int cache_delete(struct mapent_cache *mc
304803
 		free(me->key);
304803
 		if (me->mapent)
304803
 			free(me->mapent);
304803
+		while (s) {
304803
+			struct stack *next = s->next;
304803
+			if (s->mapent)
304803
+				free(s->mapent);
304803
+			free(s);
304803
+			s = next;
304803
+		}
304803
 		free(me);
304803
 	}
304803
 done:
304803
--- autofs-5.0.7.orig/modules/lookup_file.c
304803
+++ autofs-5.0.7/modules/lookup_file.c
304803
@@ -988,8 +988,10 @@ int lookup_mount(struct autofs_point *ap
304803
 				cache_writelock(smc);
304803
 				sme = cache_lookup_distinct(smc, key);
304803
 				/* Negative timeout expired for non-existent entry. */
304803
-				if (sme && !sme->mapent)
304803
-					cache_delete(smc, key);
304803
+				if (sme && !sme->mapent) {
304803
+					if (cache_pop_mapent(sme) == CHE_FAIL)
304803
+						cache_delete(smc, key);
304803
+				}
304803
 				cache_unlock(smc);
304803
 			}
304803
 		}
304803
--- autofs-5.0.7.orig/modules/lookup_hosts.c
304803
+++ autofs-5.0.7/modules/lookup_hosts.c
304803
@@ -155,7 +155,9 @@ static int do_parse_mount(struct autofs_
304803
 
304803
 		cache_writelock(mc);
304803
 		me = cache_lookup_distinct(mc, name);
304803
-		if (!me)
304803
+		if (me)
304803
+			rv = cache_push_mapent(me, NULL);
304803
+		else
304803
 			rv = cache_update(mc, source, name, NULL, now);
304803
 		if (rv != CHE_FAIL) {
304803
 			me = cache_lookup_distinct(mc, name);
304803
@@ -315,8 +317,10 @@ int lookup_mount(struct autofs_point *ap
304803
 				cache_writelock(smc);
304803
 				sme = cache_lookup_distinct(smc, name);
304803
 				/* Negative timeout expired for non-existent entry. */
304803
-				if (sme && !sme->mapent)
304803
-					cache_delete(smc, name);
304803
+				if (sme && !sme->mapent) {
304803
+					if (cache_pop_mapent(sme) == CHE_FAIL)
304803
+						cache_delete(smc, name);
304803
+				}
304803
 				cache_unlock(smc);
304803
 			}
304803
 		}
304803
--- autofs-5.0.7.orig/modules/lookup_ldap.c
304803
+++ autofs-5.0.7/modules/lookup_ldap.c
304803
@@ -2937,8 +2937,10 @@ int lookup_mount(struct autofs_point *ap
304803
 				cache_writelock(smc);
304803
 				sme = cache_lookup_distinct(smc, key);
304803
 				/* Negative timeout expired for non-existent entry. */
304803
-				if (sme && !sme->mapent)
304803
-					cache_delete(smc, key);
304803
+				if (sme && !sme->mapent) {
304803
+					if (cache_pop_mapent(sme) == CHE_FAIL)
304803
+						cache_delete(smc, key);
304803
+				}
304803
 				cache_unlock(smc);
304803
 			}
304803
 		}
304803
--- autofs-5.0.7.orig/modules/lookup_nisplus.c
304803
+++ autofs-5.0.7/modules/lookup_nisplus.c
304803
@@ -509,8 +509,10 @@ int lookup_mount(struct autofs_point *ap
304803
 				cache_writelock(smc);
304803
 				sme = cache_lookup_distinct(smc, key);
304803
 				/* Negative timeout expired for non-existent entry. */
304803
-				if (sme && !sme->mapent)
304803
-					cache_delete(smc, key);
304803
+				if (sme && !sme->mapent) {
304803
+					if (cache_pop_mapent(sme) == CHE_FAIL)
304803
+						cache_delete(smc, key);
304803
+				}
304803
 				cache_unlock(smc);
304803
 			}
304803
 		}
304803
@@ -602,7 +604,9 @@ int lookup_mount(struct autofs_point *ap
304803
 			return NSS_STATUS_TRYAGAIN;
304803
 		cache_writelock(mc);
304803
 		me = cache_lookup_distinct(mc, key);
304803
-		if (!me)
304803
+		if (me)
304803
+			rv = cache_push_mapent(me, NULL);
304803
+		else
304803
 			rv = cache_update(mc, source, key, NULL, now);
304803
 		if (rv != CHE_FAIL) {
304803
 			me = cache_lookup_distinct(mc, key);
304803
--- autofs-5.0.7.orig/modules/lookup_program.c
304803
+++ autofs-5.0.7/modules/lookup_program.c
304803
@@ -156,8 +156,10 @@ int lookup_mount(struct autofs_point *ap
304803
 				cache_writelock(smc);
304803
 				sme = cache_lookup_distinct(smc, name);
304803
 				/* Negative timeout expired for non-existent entry. */
304803
-				if (sme && !sme->mapent)
304803
-					cache_delete(smc, name);
304803
+				if (sme && !sme->mapent) {
304803
+					if (cache_pop_mapent(sme) == CHE_FAIL)
304803
+						cache_delete(smc, name);
304803
+				}
304803
 				cache_unlock(smc);
304803
 			}
304803
 		}
304803
@@ -451,7 +453,9 @@ out_free:
304803
 
304803
 		cache_writelock(mc);
304803
 		me = cache_lookup_distinct(mc, name);
304803
-		if (!me)
304803
+		if (me)
304803
+			rv = cache_push_mapent(me, NULL);
304803
+		else
304803
 			rv = cache_update(mc, source, name, NULL, now);
304803
 		if (rv != CHE_FAIL) {
304803
 			me = cache_lookup_distinct(mc, name);
304803
--- autofs-5.0.7.orig/modules/lookup_sss.c
304803
+++ autofs-5.0.7/modules/lookup_sss.c
304803
@@ -599,8 +599,10 @@ int lookup_mount(struct autofs_point *ap
304803
 				cache_writelock(smc);
304803
 				sme = cache_lookup_distinct(smc, key);
304803
 				/* Negative timeout expired for non-existent entry. */
304803
-				if (sme && !sme->mapent)
304803
-					cache_delete(smc, key);
304803
+				if (sme && !sme->mapent) {
304803
+					if (cache_pop_mapent(sme) == CHE_FAIL)
304803
+						cache_delete(smc, key);
304803
+				}
304803
 				cache_unlock(smc);
304803
 			}
304803
 		}
304803
--- autofs-5.0.7.orig/modules/lookup_yp.c
304803
+++ autofs-5.0.7/modules/lookup_yp.c
304803
@@ -613,8 +613,10 @@ int lookup_mount(struct autofs_point *ap
304803
 				cache_writelock(smc);
304803
 				sme = cache_lookup_distinct(smc, key);
304803
 				/* Negative timeout expired for non-existent entry. */
304803
-				if (sme && !sme->mapent)
304803
-					cache_delete(smc, key);
304803
+				if (sme && !sme->mapent) {
304803
+					if (cache_pop_mapent(sme) == CHE_FAIL)
304803
+						cache_delete(smc, key);
304803
+				}
304803
 				cache_unlock(smc);
304803
 			}
304803
 		}