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

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