[PATCH 19/22] autofs-5.1.4 - refactor negative map entry check

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Create a function to check for a negative map entry on lookup and use
it in each of the relevant modules.

Signed-off-by: Ian Kent <raven@xxxxxxxxxx>
---
 CHANGELOG                |    1 +
 include/automount.h      |    1 +
 lib/cache.c              |   30 ++++++++++++++++++++++++++++++
 modules/lookup_file.c    |   22 ++--------------------
 modules/lookup_hesiod.c  |   22 ++--------------------
 modules/lookup_hosts.c   |   22 ++--------------------
 modules/lookup_ldap.c    |   22 ++--------------------
 modules/lookup_nisplus.c |   22 ++--------------------
 modules/lookup_program.c |   22 ++--------------------
 modules/lookup_sss.c     |   22 ++--------------------
 modules/lookup_yp.c      |   22 ++--------------------
 11 files changed, 48 insertions(+), 160 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 21fc9ca7..0de3f31b 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -48,6 +48,7 @@ xx/xx/2018 autofs-5.1.5
 - use flags for startup boolean options.
 - move close stdio descriptors to become_daemon().
 - add systemd service command line option.
+- refactor negative map entry check.
 
 19/12/2017 autofs-5.1.4
 - fix spec file url.
diff --git a/include/automount.h b/include/automount.h
index 45fde53e..947daa10 100644
--- a/include/automount.h
+++ b/include/automount.h
@@ -223,6 +223,7 @@ struct mapent *cache_partial_match(struct mapent_cache *mc, const char *prefix);
 struct mapent *cache_partial_match_wild(struct mapent_cache *mc, const char *prefix);
 int cache_add(struct mapent_cache *mc, struct map_source *ms, const char *key, const char *mapent, time_t age);
 int cache_update_offset(struct mapent_cache *mc, const char *mkey, const char *key, const char *mapent, time_t age);
+int cache_lookup_negative(struct mapent *me, const char *key);
 void cache_update_negative(struct mapent_cache *mc, struct map_source *ms, const char *key, time_t timeout);
 int cache_set_parents(struct mapent *mm);
 int cache_update(struct mapent_cache *mc, struct map_source *ms, const char *key, const char *mapent, time_t age);
diff --git a/lib/cache.c b/lib/cache.c
index 44e323dd..d3b6642b 100644
--- a/lib/cache.c
+++ b/lib/cache.c
@@ -771,6 +771,36 @@ done:
 	return ret; 
 }
 
+/* Called with cache_lock held for map entry me and is released
+ * on return.
+ */
+int cache_lookup_negative(struct mapent *me, const char *key)
+{
+	if (me->status >= monotonic_time(NULL)) {
+		cache_unlock(me->mc);
+		return CHE_UNAVAIL;
+	} else {
+		struct mapent_cache *smc = me->mc;
+		struct mapent *sme;
+
+		if (me->mapent)
+			cache_unlock(smc);
+		else {
+			cache_unlock(smc);
+			cache_writelock(smc);
+			sme = cache_lookup_distinct(smc, key);
+			/* Negative timeout expired for non-existent entry. */
+			if (sme && !sme->mapent) {
+				if (cache_pop_mapent(sme) == CHE_FAIL)
+					cache_delete(smc, key);
+			}
+			cache_unlock(smc);
+		}
+	}
+
+	return CHE_OK;
+}
+
 void cache_update_negative(struct mapent_cache *mc,
 			   struct map_source *ms, const char *key,
 			   time_t timeout)
diff --git a/modules/lookup_file.c b/modules/lookup_file.c
index ed5caa47..6986830b 100644
--- a/modules/lookup_file.c
+++ b/modules/lookup_file.c
@@ -1177,27 +1177,9 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
 	/* Check if we recorded a mount fail for this key anywhere */
 	me = lookup_source_mapent(ap, key, LKP_DISTINCT);
 	if (me) {
-		if (me->status >= monotonic_time(NULL)) {
-			cache_unlock(me->mc);
+		/* negative timeout has not passed, return fail */
+		if (cache_lookup_negative(me, key) == CHE_UNAVAIL)
 			return NSS_STATUS_NOTFOUND;
-		} else {
-			struct mapent_cache *smc = me->mc;
-			struct mapent *sme;
-
-			if (me->mapent)
-				cache_unlock(smc);
-			else {
-				cache_unlock(smc);
-				cache_writelock(smc);
-				sme = cache_lookup_distinct(smc, key);
-				/* Negative timeout expired for non-existent entry. */
-				if (sme && !sme->mapent) {
-					if (cache_pop_mapent(sme) == CHE_FAIL)
-						cache_delete(smc, key);
-				}
-				cache_unlock(smc);
-			}
-		}
 	}
 
 	/*
diff --git a/modules/lookup_hesiod.c b/modules/lookup_hesiod.c
index b3278d20..13f3678d 100644
--- a/modules/lookup_hesiod.c
+++ b/modules/lookup_hesiod.c
@@ -397,27 +397,9 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
 	/* Check if we recorded a mount fail for this key anywhere */
 	me = lookup_source_mapent(ap, name, LKP_DISTINCT);
 	if (me) {
-		if (me->status >= monotonic_time(NULL)) {
-			cache_unlock(me->mc);
+		/* negative timeout has not passed, return fail */
+		if (cache_lookup_negative(me, key) == CHE_UNAVAIL)
 			return NSS_STATUS_NOTFOUND;
-		} else {
-			struct mapent_cache *smc = me->mc;
-			struct mapent *sme;
-
-			if (me->mapent)
-				cache_unlock(smc);
-			else {
-				cache_unlock(smc);
-				cache_writelock(smc);
-				sme = cache_lookup_distinct(smc, name);
-				/* Negative timeout expired for non-existent entry. */
-				if (sme && !sme->mapent) {
-					if (cache_pop_mapent(sme) == CHE_FAIL)
-						cache_delete(smc, name);
-				}
-				cache_unlock(smc);
-			}
-		}
 	}
 
 	/* If this is not here the filesystem stays busy, for some reason... */
diff --git a/modules/lookup_hosts.c b/modules/lookup_hosts.c
index 163b02d5..744062e2 100644
--- a/modules/lookup_hosts.c
+++ b/modules/lookup_hosts.c
@@ -314,27 +314,9 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
 	/* Check if we recorded a mount fail for this key anywhere */
 	me = lookup_source_mapent(ap, name, LKP_DISTINCT);
 	if (me) {
-		if (me->status >= monotonic_time(NULL)) {
-			cache_unlock(me->mc);
+		/* negative timeout has not passed, return fail */
+		if (cache_lookup_negative(me, name) == CHE_UNAVAIL)
 			return NSS_STATUS_NOTFOUND;
-		} else {
-			struct mapent_cache *smc = me->mc;
-			struct mapent *sme;
-
-			if (me->mapent)
-				cache_unlock(smc);
-			else {
-				cache_unlock(smc);
-				cache_writelock(smc);
-				sme = cache_lookup_distinct(smc, name);
-				/* Negative timeout expired for non-existent entry. */
-				if (sme && !sme->mapent) {
-					if (cache_pop_mapent(sme) == CHE_FAIL)
-						cache_delete(smc, name);
-				}
-				cache_unlock(smc);
-			}
-		}
 	}
 
 	cache_readlock(mc);
diff --git a/modules/lookup_ldap.c b/modules/lookup_ldap.c
index 911a34a5..f7432a2a 100644
--- a/modules/lookup_ldap.c
+++ b/modules/lookup_ldap.c
@@ -3687,27 +3687,9 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
 	/* Check if we recorded a mount fail for this key anywhere */
 	me = lookup_source_mapent(ap, key, LKP_DISTINCT);
 	if (me) {
-		if (me->status >= monotonic_time(NULL)) {
-			cache_unlock(me->mc);
+		/* negative timeout has not passed, return fail */
+		if (cache_lookup_negative(me, key) == CHE_UNAVAIL)
 			return NSS_STATUS_NOTFOUND;
-		} else {
-			struct mapent_cache *smc = me->mc;
-			struct mapent *sme;
-
-			if (me->mapent)
-				cache_unlock(smc);
-			else {
-				cache_unlock(smc);
-				cache_writelock(smc);
-				sme = cache_lookup_distinct(smc, key);
-				/* Negative timeout expired for non-existent entry. */
-				if (sme && !sme->mapent) {
-					if (cache_pop_mapent(sme) == CHE_FAIL)
-						cache_delete(smc, key);
-				}
-				cache_unlock(smc);
-			}
-		}
 	}
 
         /*
diff --git a/modules/lookup_nisplus.c b/modules/lookup_nisplus.c
index 8dee4630..cbd03cdb 100644
--- a/modules/lookup_nisplus.c
+++ b/modules/lookup_nisplus.c
@@ -709,27 +709,9 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
 	/* Check if we recorded a mount fail for this key anywhere */
 	me = lookup_source_mapent(ap, key, LKP_DISTINCT);
 	if (me) {
-		if (me->status >= monotonic_time(NULL)) {
-			cache_unlock(me->mc);
+		/* negative timeout has not passed, return fail */
+		if (cache_lookup_negative(me, key) == CHE_UNAVAIL)
 			return NSS_STATUS_NOTFOUND;
-		} else {
-			struct mapent_cache *smc = me->mc;
-			struct mapent *sme;
-
-			if (me->mapent)
-				cache_unlock(smc);
-			else {
-				cache_unlock(smc);
-				cache_writelock(smc);
-				sme = cache_lookup_distinct(smc, key);
-				/* Negative timeout expired for non-existent entry. */
-				if (sme && !sme->mapent) {
-					if (cache_pop_mapent(sme) == CHE_FAIL)
-						cache_delete(smc, key);
-				}
-				cache_unlock(smc);
-			}
-		}
 	}
 
 	/*
diff --git a/modules/lookup_program.c b/modules/lookup_program.c
index 56e19394..88939471 100644
--- a/modules/lookup_program.c
+++ b/modules/lookup_program.c
@@ -601,27 +601,9 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
 	/* Check if we recorded a mount fail for this key anywhere */
 	me = lookup_source_mapent(ap, name, LKP_DISTINCT);
 	if (me) {
-		if (me->status >= monotonic_time(NULL)) {
-			cache_unlock(me->mc);
+		/* negative timeout has not passed, return fail */
+		if (cache_lookup_negative(me, key) == CHE_UNAVAIL)
 			return NSS_STATUS_NOTFOUND;
-		} else {
-			struct mapent_cache *smc = me->mc;
-			struct mapent *sme;
-
-			if (me->mapent)
-				cache_unlock(smc);
-			else {
-				cache_unlock(smc);
-				cache_writelock(smc);
-				sme = cache_lookup_distinct(smc, name);
-				/* Negative timeout expired for non-existent entry. */
-				if (sme && !sme->mapent) {
-					if (cache_pop_mapent(sme) == CHE_FAIL)
-						cache_delete(smc, name);
-				}
-				cache_unlock(smc);
-			}
-		}
 	}
 
 	/* Catch installed direct offset triggers */
diff --git a/modules/lookup_sss.c b/modules/lookup_sss.c
index 78598300..ce60acd3 100644
--- a/modules/lookup_sss.c
+++ b/modules/lookup_sss.c
@@ -726,27 +726,9 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
 	/* Check if we recorded a mount fail for this key anywhere */
 	me = lookup_source_mapent(ap, key, LKP_DISTINCT);
 	if (me) {
-		if (me->status >= monotonic_time(NULL)) {
-			cache_unlock(me->mc);
+		/* negative timeout has not passed, return fail */
+		if (cache_lookup_negative(me, key) == CHE_UNAVAIL)
 			return NSS_STATUS_NOTFOUND;
-		} else {
-			struct mapent_cache *smc = me->mc;
-			struct mapent *sme;
-
-			if (me->mapent)
-				cache_unlock(smc);
-			else {
-				cache_unlock(smc);
-				cache_writelock(smc);
-				sme = cache_lookup_distinct(smc, key);
-				/* Negative timeout expired for non-existent entry. */
-				if (sme && !sme->mapent) {
-					if (cache_pop_mapent(sme) == CHE_FAIL)
-						cache_delete(smc, key);
-				}
-				cache_unlock(smc);
-			}
-		}
 	}
 
         /*
diff --git a/modules/lookup_yp.c b/modules/lookup_yp.c
index d1ffcae4..38f75497 100644
--- a/modules/lookup_yp.c
+++ b/modules/lookup_yp.c
@@ -813,27 +813,9 @@ int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *
 	/* Check if we recorded a mount fail for this key anywhere */
 	me = lookup_source_mapent(ap, key, LKP_DISTINCT);
 	if (me) {
-		if (me->status >= monotonic_time(NULL)) {
-			cache_unlock(me->mc);
+		/* negative timeout has not passed, return fail */
+		if (cache_lookup_negative(me, key) == CHE_UNAVAIL)
 			return NSS_STATUS_NOTFOUND;
-		} else {
-			struct mapent_cache *smc = me->mc;
-			struct mapent *sme;
-
-			if (me->mapent)
-				cache_unlock(smc);
-			else {
-				cache_unlock(smc);
-				cache_writelock(smc);
-				sme = cache_lookup_distinct(smc, key);
-				/* Negative timeout expired for non-existent entry. */
-				if (sme && !sme->mapent) {
-					if (cache_pop_mapent(sme) == CHE_FAIL)
-						cache_delete(smc, key);
-				}
-				cache_unlock(smc);
-			}
-		}
 	}
 
 	 /*




[Index of Archives]     [Linux Filesystem Development]     [Linux Ext4]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]

  Powered by Linux