Blame SOURCES/autofs-5.1.8-serialise-lookup-module-open-and-reinit.patch

288172
autofs-5.1.8 - serialise lookup module open and reinit
288172
288172
From: Ian Kent <raven@themaw.net>
288172
288172
Add a map source lock to serialise map setting and use of module
288172
structure fields such as the context.
288172
288172
Signed-off-by: Ian Kent <raven@themaw.net>
288172
---
288172
 CHANGELOG           |    1 +
288172
 daemon/lookup.c     |   35 +++++++++++++++++++++--------------
288172
 daemon/master.c     |   43 +++++++++++++++++++++++++++++++++++++++++++
288172
 include/master.h    |    5 +++++
288172
 modules/parse_amd.c |   26 +++++++++++++++-----------
288172
 5 files changed, 85 insertions(+), 25 deletions(-)
288172
288172
--- autofs-5.1.4.orig/CHANGELOG
288172
+++ autofs-5.1.4/CHANGELOG
288172
@@ -97,6 +97,7 @@
288172
 - dont use initgroups() at spawn.
288172
 - fix invalid tsv access.
288172
 - fix parse module instance mutex naming.
288172
+- serialise lookup module open and reinit.
288172
 
288172
 xx/xx/2018 autofs-5.1.5
288172
 - fix flag file permission.
288172
--- autofs-5.1.4.orig/daemon/lookup.c
288172
+++ autofs-5.1.4/daemon/lookup.c
288172
@@ -318,28 +318,27 @@ static int do_read_map(struct autofs_poi
288172
 	struct lookup_mod *lookup;
288172
 	int status;
288172
 
288172
-	lookup = NULL;
288172
-	master_source_writelock(ap->entry);
288172
+	pthread_cleanup_push(map_module_lock_cleanup, map);
288172
+	map_module_writelock(map);
288172
 	if (!map->lookup) {
288172
 		status = open_lookup(map->type, "", map->format,
288172
 				     map->argc, map->argv, &lookup);
288172
-		if (status != NSS_STATUS_SUCCESS) {
288172
-			master_source_unlock(ap->entry);
288172
+		if (status == NSS_STATUS_SUCCESS)
288172
+			map->lookup = lookup;
288172
+		else
288172
 			debug(ap->logopt,
288172
 			      "lookup module %s open failed", map->type);
288172
-			return status;
288172
-		}
288172
-		map->lookup = lookup;
288172
 	} else {
288172
-		lookup = map->lookup;
288172
-		status = lookup->lookup_reinit(map->format,
288172
-					       map->argc, map->argv,
288172
-					       &lookup->context);
288172
+		status = map->lookup->lookup_reinit(map->format,
288172
+						    map->argc, map->argv,
288172
+						    &map->lookup->context);
288172
 		if (status)
288172
 			warn(ap->logopt,
288172
 			     "lookup module %s reinit failed", map->type);
288172
 	}
288172
-	master_source_unlock(ap->entry);
288172
+	pthread_cleanup_pop(1);
288172
+	if (status != NSS_STATUS_SUCCESS)
288172
+		return status;
288172
 
288172
 	if (!map->stale)
288172
 		return NSS_STATUS_SUCCESS;
288172
@@ -347,7 +346,11 @@ static int do_read_map(struct autofs_poi
288172
 	master_source_current_wait(ap->entry);
288172
 	ap->entry->current = map;
288172
 
288172
+	pthread_cleanup_push(map_module_lock_cleanup, map);
288172
+	map_module_readlock(map);
288172
+	lookup = map->lookup;
288172
 	status = lookup->lookup_read_map(ap, age, lookup->context);
288172
+	pthread_cleanup_pop(1);
288172
 
288172
 	if (status != NSS_STATUS_SUCCESS)
288172
 		map->stale = 0;
288172
@@ -812,23 +815,27 @@ int do_lookup_mount(struct autofs_point
288172
 	struct lookup_mod *lookup;
288172
 	int status;
288172
 
288172
+	map_module_writelock(map);
288172
 	if (!map->lookup) {
288172
 		status = open_lookup(map->type, "",
288172
 				     map->format, map->argc, map->argv, &lookup);
288172
 		if (status != NSS_STATUS_SUCCESS) {
288172
+			map_module_unlock(map);
288172
 			debug(ap->logopt,
288172
 			      "lookup module %s open failed", map->type);
288172
 			return status;
288172
 		}
288172
 		map->lookup = lookup;
288172
 	}
288172
-
288172
-	lookup = map->lookup;
288172
+	map_module_unlock(map);
288172
 
288172
 	master_source_current_wait(ap->entry);
288172
 	ap->entry->current = map;
288172
 
288172
+	map_module_readlock(map);
288172
+	lookup = map->lookup;
288172
 	status = lookup->lookup_mount(ap, name, name_len, lookup->context);
288172
+	map_module_unlock(map);
288172
 
288172
 	return status;
288172
 }
288172
--- autofs-5.1.4.orig/daemon/master.c
288172
+++ autofs-5.1.4/daemon/master.c
288172
@@ -65,6 +65,34 @@ void master_mutex_lock_cleanup(void *arg
288172
 	return;
288172
 }
288172
 
288172
+void map_module_writelock(struct map_source *map)
288172
+{
288172
+	int status = pthread_rwlock_wrlock(&map->module_lock);
288172
+	if (status)
288172
+		fatal(status);
288172
+}
288172
+
288172
+void map_module_readlock(struct map_source *map)
288172
+{
288172
+	int status = pthread_rwlock_rdlock(&map->module_lock);
288172
+	if (status)
288172
+		fatal(status);
288172
+}
288172
+
288172
+void map_module_unlock(struct map_source *map)
288172
+{
288172
+	int status = pthread_rwlock_unlock(&map->module_lock);
288172
+	if (status)
288172
+		fatal(status);
288172
+}
288172
+
288172
+void map_module_lock_cleanup(void *arg)
288172
+{
288172
+	struct map_source *map = (struct map_source *) arg;
288172
+
288172
+	map_module_unlock(map);
288172
+}
288172
+
288172
 int master_add_autofs_point(struct master_mapent *entry, unsigned logopt,
288172
 			    unsigned nobind, unsigned ghost, int submount)
288172
 {
288172
@@ -155,6 +183,7 @@ master_add_map_source(struct master_mape
288172
 	struct map_source *source;
288172
 	char *ntype, *nformat;
288172
 	const char **tmpargv;
288172
+	int status;
288172
 
288172
 	source = malloc(sizeof(struct map_source));
288172
 	if (!source)
288172
@@ -241,6 +270,10 @@ master_add_map_source(struct master_mape
288172
 
288172
 	master_source_unlock(entry);
288172
 
288172
+	status = pthread_rwlock_init(&source->module_lock, NULL);
288172
+	if (status)
288172
+		fatal(status);
288172
+
288172
 	return source;
288172
 }
288172
 
288172
@@ -330,6 +363,8 @@ master_get_map_source(struct master_mape
288172
 
288172
 static void __master_free_map_source(struct map_source *source, unsigned int free_cache)
288172
 {
288172
+	int status;
288172
+
288172
 	/* instance map sources are not ref counted */
288172
 	if (source->ref && --source->ref)
288172
 		return;
288172
@@ -365,6 +400,10 @@ static void __master_free_map_source(str
288172
 		}
288172
 	}
288172
 
288172
+	status = pthread_rwlock_destroy(&source->module_lock);
288172
+	if (status)
288172
+		fatal(status);
288172
+
288172
 	free(source);
288172
 
288172
 	return;
288172
@@ -496,6 +535,10 @@ master_add_source_instance(struct map_so
288172
 	if (status)
288172
 		fatal(status);
288172
 
288172
+	status = pthread_rwlock_init(&new->module_lock, NULL);
288172
+	if (status)
288172
+		fatal(status);
288172
+
288172
 	return new;
288172
 }
288172
 
288172
--- autofs-5.1.4.orig/include/master.h
288172
+++ autofs-5.1.4/include/master.h
288172
@@ -35,6 +35,7 @@ struct map_source {
288172
 	unsigned int stale;
288172
 	unsigned int recurse;
288172
 	unsigned int depth;
288172
+	pthread_rwlock_t module_lock;
288172
 	struct lookup_mod *lookup;
288172
 	int argc;
288172
 	const char **argv;
288172
@@ -126,5 +127,9 @@ int __master_list_empty(struct master *)
288172
 int master_list_empty(struct master *);
288172
 int master_done(struct master *);
288172
 int master_kill(struct master *);
288172
+void map_module_writelock(struct map_source *map);
288172
+void map_module_readlock(struct map_source *map);
288172
+void map_module_unlock(struct map_source *map);
288172
+void map_module_lock_cleanup(void *arg);
288172
 
288172
 #endif
288172
--- autofs-5.1.4.orig/modules/parse_amd.c
288172
+++ autofs-5.1.4/modules/parse_amd.c
288172
@@ -1358,14 +1358,6 @@ static int do_host_mount(struct autofs_p
288172
 		argc = 1;
288172
 	}
288172
 
288172
-	parse_instance_mutex_lock();
288172
-	status = open_lookup("hosts", MODPREFIX, NULL, argc, pargv, &lookup);
288172
-	if (status != NSS_STATUS_SUCCESS) {
288172
-		debug(ap->logopt, "open lookup module hosts failed");
288172
-		parse_instance_mutex_unlock();
288172
-		goto out;
288172
-	}
288172
-
288172
 	instance = master_find_source_instance(source,
288172
 					 "hosts", "sun", argc, pargv);
288172
 	if (!instance) {
288172
@@ -1374,13 +1366,22 @@ static int do_host_mount(struct autofs_p
288172
 		if (!instance) {
288172
 			error(ap->logopt, MODPREFIX
288172
 			     "failed to create source instance for hosts map");
288172
-			parse_instance_mutex_unlock();
288172
 			close_lookup(lookup);
288172
 			goto out;
288172
 		}
288172
 	}
288172
-	instance->lookup = lookup;
288172
-	parse_instance_mutex_unlock();
288172
+
288172
+	map_module_writelock(instance);
288172
+	if (!instance->lookup) {
288172
+		status = open_lookup("hosts", MODPREFIX, NULL, argc, pargv, &lookup);
288172
+		if (status != NSS_STATUS_SUCCESS) {
288172
+			map_module_unlock(instance);
288172
+			debug(ap->logopt, "open lookup module hosts failed");
288172
+			goto out;
288172
+		}
288172
+		instance->lookup = lookup;
288172
+	}
288172
+	map_module_unlock(instance);
288172
 
288172
 	cache_writelock(source->mc);
288172
 	me = cache_lookup_distinct(source->mc, name);
288172
@@ -1391,8 +1392,11 @@ static int do_host_mount(struct autofs_p
288172
 	master_source_current_wait(ap->entry);
288172
 	ap->entry->current = source;
288172
 
288172
+	map_module_readlock(instance);
288172
+	lookup = instance->lookup;
288172
 	ret = lookup->lookup_mount(ap, entry->rhost,
288172
 				   strlen(entry->rhost), lookup->context);
288172
+	map_module_unlock(instance);
288172
 
288172
 	if (!strcmp(name, entry->rhost))
288172
 		goto out;