Blame SOURCES/autofs-5.1.6-use-mnt_list-for-submounts.patch

beb904
autofs-5.1.6 - use mnt_list for submounts
beb904
beb904
From: Ian Kent <raven@themaw.net>
beb904
beb904
Use struct mnt_list objects for submount list entries instead of
beb904
struct autofs_point itself.
beb904
beb904
Signed-off-by: Ian Kent <raven@themaw.net>
beb904
---
beb904
 CHANGELOG              |    1 
beb904
 daemon/automount.c     |   11 ++++-
beb904
 daemon/direct.c        |    7 ++-
beb904
 daemon/indirect.c      |    5 ++
beb904
 include/automount.h    |    2 -
beb904
 include/master.h       |    2 -
beb904
 include/mounts.h       |   10 +++++
beb904
 lib/master.c           |   93 +++++++++++++++----------------------------------
beb904
 lib/mounts.c           |   81 ++++++++++++++++++++++++++++++++++++++++++
beb904
 modules/mount_autofs.c |   17 ++++++++
beb904
 10 files changed, 157 insertions(+), 72 deletions(-)
beb904
beb904
--- autofs-5.1.4.orig/CHANGELOG
beb904
+++ autofs-5.1.4/CHANGELOG
beb904
@@ -122,6 +122,7 @@ xx/xx/2018 autofs-5.1.5
beb904
 - make external mounts independent of amd_entry.
beb904
 - make external mounts use simpler hashtable.
beb904
 - add a hash index to mnt_list.
beb904
+- use mnt_list for submounts.
beb904
 
beb904
 19/12/2017 autofs-5.1.4
beb904
 - fix spec file url.
beb904
--- autofs-5.1.4.orig/daemon/automount.c
beb904
+++ autofs-5.1.4/daemon/automount.c
beb904
@@ -624,6 +624,7 @@ done:
beb904
    it also tries to umount path itself */
beb904
 int umount_multi(struct autofs_point *ap, const char *path, int incl)
beb904
 {
beb904
+	struct mnt_list *sbmnt;
beb904
 	int is_autofs_fs;
beb904
 	struct stat st;
beb904
 	int left;
beb904
@@ -685,8 +686,11 @@ int umount_multi(struct autofs_point *ap
beb904
 	}
beb904
 
beb904
 	is_autofs_fs = 0;
beb904
-	if (master_find_submount(ap, path))
beb904
+	sbmnt = mnts_find_submount(path);
beb904
+	if (sbmnt) {
beb904
 		is_autofs_fs = 1;
beb904
+		mnts_put_mount(sbmnt);
beb904
+	}
beb904
 
beb904
 	left = 0;
beb904
 
beb904
@@ -1717,9 +1721,12 @@ static void handle_mounts_cleanup(void *
beb904
 
beb904
 	if (submount) {
beb904
 		struct amd_entry *am;
beb904
+
beb904
 		/* We are finishing up */
beb904
 		ap->parent->submnt_count--;
beb904
-		list_del_init(&ap->mounts);
beb904
+
beb904
+		/* Submount at ap->path belongs to parent submount list. */
beb904
+		mnts_remove_submount(ap->path);
beb904
 		am = __master_find_amdmount(ap->parent, ap->path);
beb904
 		if (am) {
beb904
 			list_del_init(&am->entries);
beb904
--- autofs-5.1.4.orig/daemon/direct.c
beb904
+++ autofs-5.1.4/daemon/direct.c
beb904
@@ -1211,14 +1211,17 @@ static void *do_mount_direct(void *arg)
beb904
 	 */
beb904
 	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &state);
beb904
 	if (status) {
beb904
+		struct mnt_list *sbmnt;
beb904
 		struct mapent *me;
beb904
 		struct statfs fs;
beb904
 		unsigned int close_fd = 0;
beb904
 
beb904
+		sbmnt = mnts_find_submount(mt.name);
beb904
 		if (statfs(mt.name, &fs) == -1 ||
beb904
-		   (fs.f_type == AUTOFS_SUPER_MAGIC &&
beb904
-		    !master_find_submount(ap, mt.name)))
beb904
+		   (fs.f_type == AUTOFS_SUPER_MAGIC && !sbmnt))
beb904
 			close_fd = 1;
beb904
+		if (sbmnt)
beb904
+			mnts_put_mount(sbmnt);
beb904
 		cache_writelock(mt.mc);
beb904
 		if ((me = cache_lookup_distinct(mt.mc, mt.name))) {
beb904
 			/*
beb904
--- autofs-5.1.4.orig/daemon/indirect.c
beb904
+++ autofs-5.1.4/daemon/indirect.c
beb904
@@ -401,6 +401,7 @@ void *expire_proc_indirect(void *arg)
beb904
 			if (next->flags & MNTS_INDIRECT)
beb904
 				master_notify_submount(ap, next->mp, ap->state);
beb904
 			else if (next->flags & MNTS_OFFSET) {
beb904
+				struct mnt_list *sbmnt;
beb904
 				struct map_source *map;
beb904
 				struct mapent_cache *mc = NULL;
beb904
 				struct mapent *me = NULL;
beb904
@@ -413,7 +414,9 @@ void *expire_proc_indirect(void *arg)
beb904
 				}
beb904
 
beb904
 				/* Don't touch submounts */
beb904
-				if (master_find_submount(ap, next->mp)) {
beb904
+				sbmnt = mnts_find_submount(next->mp);
beb904
+				if (sbmnt) {
beb904
+					mnts_put_mount(sbmnt);
beb904
 					pthread_setcancelstate(cur_state, NULL);
beb904
 					continue;
beb904
 				}
beb904
--- autofs-5.1.4.orig/include/automount.h
beb904
+++ autofs-5.1.4/include/automount.h
beb904
@@ -570,9 +570,9 @@ struct autofs_point {
beb904
 	struct list_head mounts;	/* List of autofs mounts at current level */
beb904
 	struct list_head amdmounts;	/* List of non submount amd mounts */
beb904
 	unsigned int submount;		/* Is this a submount */
beb904
-	unsigned int shutdown;		/* Shutdown notification */
beb904
 	unsigned int submnt_count;	/* Number of submounts */
beb904
 	struct list_head submounts;	/* List of child submounts */
beb904
+	unsigned int shutdown;		/* Shutdown notification */
beb904
 };
beb904
 
beb904
 #define UNLINK_AND_CONT		0x01
beb904
--- autofs-5.1.4.orig/include/master.h
beb904
+++ autofs-5.1.4/include/master.h
beb904
@@ -109,8 +109,6 @@ void master_source_current_wait(struct m
beb904
 void master_source_current_signal(struct master_mapent *);
beb904
 struct master_mapent *master_find_mapent(struct master *, const char *);
beb904
 unsigned int master_partial_match_mapent(struct master *, const char *);
beb904
-struct autofs_point *__master_find_submount(struct autofs_point *, const char *);
beb904
-struct autofs_point *master_find_submount(struct autofs_point *, const char *);
beb904
 struct amd_entry *__master_find_amdmount(struct autofs_point *, const char *);
beb904
 struct amd_entry *master_find_amdmount(struct autofs_point *, const char *);
beb904
 struct master_mapent *master_new_mapent(struct master *, const char *, time_t);
beb904
--- autofs-5.1.4.orig/include/mounts.h
beb904
+++ autofs-5.1.4/include/mounts.h
beb904
@@ -59,6 +59,11 @@ struct mnt_list {
beb904
 	struct hlist_node hash;
beb904
 	unsigned int ref;
beb904
 
beb904
+	/* List of sub-mounts of an autofs_point */
beb904
+	struct autofs_point *ap;
beb904
+	struct list_head submount;
beb904
+	struct list_head submount_work;
beb904
+
beb904
 	/*
beb904
 	 * List operations ie. get_mnt_list.
beb904
 	 */
beb904
@@ -108,6 +113,11 @@ int ext_mount_remove(const char *);
beb904
 int ext_mount_inuse(const char *);
beb904
 struct mnt_list *mnts_lookup_mount(const char *mp);
beb904
 void mnts_put_mount(struct mnt_list *mnt);
beb904
+struct mnt_list *mnts_find_submount(const char *path);
beb904
+struct mnt_list *mnts_add_submount(struct autofs_point *ap);
beb904
+void mnts_remove_submount(const char *mp);
beb904
+void mnts_get_submount_list(struct list_head *mnts, struct autofs_point *ap);
beb904
+void mnts_put_submount_list(struct list_head *mnts);
beb904
 struct mnt_list *get_mnt_list(const char *path, int include);
beb904
 int unlink_mount_tree(struct autofs_point *ap, const char *mp);
beb904
 void free_mnt_list(struct mnt_list *list);
beb904
--- autofs-5.1.4.orig/lib/master.c
beb904
+++ autofs-5.1.4/lib/master.c
beb904
@@ -761,34 +761,6 @@ unsigned int master_partial_match_mapent
beb904
 	return ret;
beb904
 }
beb904
 
beb904
-struct autofs_point *__master_find_submount(struct autofs_point *ap, const char *path)
beb904
-{
beb904
-	struct list_head *head, *p;
beb904
-
beb904
-	head = &ap->submounts;
beb904
-	list_for_each(p, head) {
beb904
-		struct autofs_point *submount;
beb904
-
beb904
-		submount = list_entry(p, struct autofs_point, mounts);
beb904
-
beb904
-		if (!strcmp(submount->path, path))
beb904
-			return submount;
beb904
-	}
beb904
-
beb904
-	return NULL;
beb904
-}
beb904
-
beb904
-struct autofs_point *master_find_submount(struct autofs_point *ap, const char *path)
beb904
-{
beb904
-	struct autofs_point *submount;
beb904
-
beb904
-	mounts_mutex_lock(ap);
beb904
-	submount = __master_find_submount(ap, path);
beb904
-	mounts_mutex_unlock(ap);
beb904
-
beb904
-	return submount;
beb904
-}
beb904
-
beb904
 struct amd_entry *__master_find_amdmount(struct autofs_point *ap, const char *path)
beb904
 {
beb904
 	struct list_head *head, *p;
beb904
@@ -1190,85 +1162,80 @@ int master_submount_list_empty(struct au
beb904
 
beb904
 int master_notify_submount(struct autofs_point *ap, const char *path, enum states state)
beb904
 {
beb904
-	struct list_head *head, *p;
beb904
-	struct autofs_point *this = NULL;
beb904
+	struct mnt_list *this, *sbmnt;
beb904
+	LIST_HEAD(sbmnts);
beb904
 	int ret = 1;
beb904
 
beb904
-	mounts_mutex_lock(ap);
beb904
-
beb904
-	head = &ap->submounts;
beb904
-	p = head->prev;
beb904
-	while (p != head) {
beb904
-		this = list_entry(p, struct autofs_point, mounts);
beb904
-		p = p->prev;
beb904
+	mnts_get_submount_list(&sbmnts, ap);
beb904
+	if (list_empty(&sbmnts))
beb904
+		return 1;
beb904
+
beb904
+	list_for_each_entry(this, &sbmnts, submount_work) {
beb904
+		/* Not a submount */
beb904
+		if (!(this->flags & MNTS_AUTOFS))
beb904
+			continue;
beb904
 
beb904
 		/* path not the same */
beb904
-		if (strcmp(this->path, path))
beb904
+		if (strcmp(this->mp, path))
beb904
 			continue;
beb904
 
beb904
-		if (!master_submount_list_empty(this)) {
beb904
-			char *this_path = strdup(this->path);
beb904
-			if (this_path) {
beb904
-				mounts_mutex_unlock(ap);
beb904
-				master_notify_submount(this, path, state);
beb904
-				mounts_mutex_lock(ap);
beb904
-				if (!__master_find_submount(ap, this_path)) {
beb904
-					free(this_path);
beb904
-					continue;
beb904
-				}
beb904
-				free(this_path);
beb904
-			}
beb904
+		if (!master_submount_list_empty(this->ap)) {
beb904
+			struct mnt_list *sm;
beb904
+
beb904
+			master_notify_submount(this->ap, path, state);
beb904
+			sm = mnts_find_submount(path);
beb904
+			if (!sm)
beb904
+				continue;
beb904
+			mnts_put_mount(sm);
beb904
 		}
beb904
 
beb904
 		/* Now we have found the submount we want to expire */
beb904
 
beb904
 		st_mutex_lock();
beb904
 
beb904
-		if (this->state == ST_SHUTDOWN) {
beb904
+		if (this->ap->state == ST_SHUTDOWN) {
beb904
 			this = NULL;
beb904
 			st_mutex_unlock();
beb904
 			break;
beb904
 		}
beb904
 
beb904
-		this->shutdown = ap->shutdown;
beb904
+		this->ap->shutdown = ap->shutdown;
beb904
 
beb904
-		__st_add_task(this, state);
beb904
+		__st_add_task(this->ap, state);
beb904
 
beb904
 		st_mutex_unlock();
beb904
-		mounts_mutex_unlock(ap);
beb904
 
beb904
-		st_wait_task(this, state, 0);
beb904
+		st_wait_task(this->ap, state, 0);
beb904
 
beb904
 		/*
beb904
 		 * If our submount gets to state ST_SHUTDOWN, ST_SHUTDOWN_PENDING or
beb904
 		 * ST_SHUTDOWN_FORCE we need to wait until it goes away or changes
beb904
 		 * to ST_READY.
beb904
 		 */
beb904
-		mounts_mutex_lock(ap);
beb904
 		st_mutex_lock();
beb904
-		while ((this = __master_find_submount(ap, path))) {
beb904
+		while ((sbmnt = mnts_find_submount(path))) {
beb904
 			struct timespec t = { 0, 300000000 };
beb904
 			struct timespec r;
beb904
 
beb904
-			if (this->state != ST_SHUTDOWN &&
beb904
-			    this->state != ST_SHUTDOWN_PENDING &&
beb904
-			    this->state != ST_SHUTDOWN_FORCE) {
beb904
+			if (sbmnt->ap->state != ST_SHUTDOWN &&
beb904
+			    sbmnt->ap->state != ST_SHUTDOWN_PENDING &&
beb904
+			    sbmnt->ap->state != ST_SHUTDOWN_FORCE) {
beb904
 				ret = 0;
beb904
+				mnts_put_mount(sbmnt);
beb904
 				break;
beb904
 			}
beb904
+			mnts_put_mount(sbmnt);
beb904
 
beb904
 			st_mutex_unlock();
beb904
-			mounts_mutex_unlock(ap);
beb904
 			while (nanosleep(&t, &r) == -1 && errno == EINTR)
beb904
 				memcpy(&t, &r, sizeof(struct timespec));
beb904
-			mounts_mutex_lock(ap);
beb904
 			st_mutex_lock();
beb904
 		}
beb904
 		st_mutex_unlock();
beb904
 		break;
beb904
 	}
beb904
 
beb904
-	mounts_mutex_unlock(ap);
beb904
+	mnts_put_submount_list(&sbmnts);
beb904
 
beb904
 	return ret;
beb904
 }
beb904
--- autofs-5.1.4.orig/lib/mounts.c
beb904
+++ autofs-5.1.4/lib/mounts.c
beb904
@@ -885,6 +885,8 @@ static struct mnt_list *mnts_alloc_mount
beb904
 
beb904
 	this->ref = 1;
beb904
 	INIT_HLIST_NODE(&this->hash);
beb904
+	INIT_LIST_HEAD(&this->submount);
beb904
+	INIT_LIST_HEAD(&this->submount_work);
beb904
 done:
beb904
 	return this;
beb904
 }
beb904
@@ -967,6 +969,85 @@ void mnts_put_mount(struct mnt_list *mnt
beb904
 	mnts_hash_mutex_unlock();
beb904
 }
beb904
 
beb904
+struct mnt_list *mnts_find_submount(const char *path)
beb904
+{
beb904
+	struct mnt_list *mnt;
beb904
+
beb904
+	mnt = mnts_lookup_mount(path);
beb904
+	if (mnt && mnt->flags & MNTS_AUTOFS)
beb904
+		return mnt;
beb904
+	mnts_put_mount(mnt);
beb904
+	return NULL;
beb904
+}
beb904
+
beb904
+struct mnt_list *mnts_add_submount(struct autofs_point *ap)
beb904
+{
beb904
+	struct mnt_list *this;
beb904
+
beb904
+	mnts_hash_mutex_lock();
beb904
+	this = mnts_get_mount(ap->path);
beb904
+	if (this) {
beb904
+		if (!this->ap)
beb904
+			this->ap = ap;
beb904
+		else if (this->ap != ap ||
beb904
+			 this->ap->parent != ap->parent) {
beb904
+			__mnts_put_mount(this);
beb904
+			mnts_hash_mutex_unlock();
beb904
+			error(ap->logopt,
beb904
+			      "conflict with submount owner: %s", ap->path);
beb904
+			goto fail;
beb904
+		}
beb904
+		this->flags |= MNTS_AUTOFS;
beb904
+		if (list_empty(&this->submount))
beb904
+			list_add_tail(&this->submount, &ap->parent->submounts);
beb904
+	}
beb904
+	mnts_hash_mutex_unlock();
beb904
+fail:
beb904
+	return this;
beb904
+}
beb904
+
beb904
+void mnts_remove_submount(const char *mp)
beb904
+{
beb904
+	struct mnt_list *this;
beb904
+
beb904
+	mnts_hash_mutex_lock();
beb904
+	this = mnts_lookup(mp);
beb904
+	if (this && this->flags & MNTS_AUTOFS) {
beb904
+		this->flags &= ~MNTS_AUTOFS;
beb904
+		this->ap = NULL;
beb904
+		list_del_init(&this->submount);
beb904
+		__mnts_put_mount(this);
beb904
+	}
beb904
+	mnts_hash_mutex_unlock();
beb904
+}
beb904
+
beb904
+void mnts_get_submount_list(struct list_head *mnts, struct autofs_point *ap)
beb904
+{
beb904
+	struct mnt_list *mnt;
beb904
+
beb904
+	mnts_hash_mutex_lock();
beb904
+	if (list_empty(&ap->submounts))
beb904
+		goto done;
beb904
+	list_for_each_entry(mnt, &ap->submounts, submount) {
beb904
+		__mnts_get_mount(mnt);
beb904
+		list_add(&mnt->submount_work, mnts);
beb904
+	}
beb904
+done:
beb904
+	mnts_hash_mutex_unlock();
beb904
+}
beb904
+
beb904
+void mnts_put_submount_list(struct list_head *mnts)
beb904
+{
beb904
+	struct mnt_list *mnt, *tmp;
beb904
+
beb904
+	mnts_hash_mutex_lock();
beb904
+	list_for_each_entry_safe(mnt, tmp, mnts, submount_work) {
beb904
+		list_del_init(&mnt->submount_work);
beb904
+		__mnts_put_mount(mnt);
beb904
+	}
beb904
+	mnts_hash_mutex_unlock();
beb904
+}
beb904
+
beb904
 /* From glibc decode_name() */
beb904
 /* Since the values in a line are separated by spaces, a name cannot
beb904
  * contain a space.  Therefore some programs encode spaces in names
beb904
--- autofs-5.1.4.orig/modules/mount_autofs.c
beb904
+++ autofs-5.1.4/modules/mount_autofs.c
beb904
@@ -65,6 +65,7 @@ int mount_mount(struct autofs_point *ap,
beb904
 	struct master_mapent *entry;
beb904
 	struct map_source *source;
beb904
 	struct autofs_point *nap;
beb904
+	struct mnt_list *mnt;
beb904
 	char buf[MAX_ERR_BUF];
beb904
 	char *options, *p;
beb904
 	int len, ret;
beb904
@@ -307,6 +308,18 @@ int mount_mount(struct autofs_point *ap,
beb904
 		return 1;
beb904
 	}
beb904
 
beb904
+	mnt = mnts_add_submount(nap);
beb904
+	if (!mnt) {
beb904
+		crit(ap->logopt,
beb904
+		     MODPREFIX "failed to allocate mount %s", realpath);
beb904
+		handle_mounts_startup_cond_destroy(&suc);
beb904
+		mounts_mutex_unlock(ap);
beb904
+		master_free_map_source(source, 1);
beb904
+		master_free_mapent(entry);
beb904
+		return 1;
beb904
+	}
beb904
+
beb904
+
beb904
 	suc.ap = nap;
beb904
 	suc.root = mountpoint;
beb904
 	suc.done = 0;
beb904
@@ -318,6 +331,7 @@ int mount_mount(struct autofs_point *ap,
beb904
 		     "failed to create mount handler thread for %s",
beb904
 		     realpath);
beb904
 		handle_mounts_startup_cond_destroy(&suc);
beb904
+		mnts_remove_submount(nap->path);
beb904
 		mounts_mutex_unlock(ap);
beb904
 		master_free_map_source(source, 1);
beb904
 		master_free_mapent(entry);
beb904
@@ -328,6 +342,7 @@ int mount_mount(struct autofs_point *ap,
beb904
 		status = pthread_cond_wait(&suc.cond, &suc.mutex);
beb904
 		if (status) {
beb904
 			handle_mounts_startup_cond_destroy(&suc);
beb904
+			mnts_remove_submount(nap->path);
beb904
 			mounts_mutex_unlock(ap);
beb904
 			master_free_map_source(source, 1);
beb904
 			master_free_mapent(entry);
beb904
@@ -339,6 +354,7 @@ int mount_mount(struct autofs_point *ap,
beb904
 		crit(ap->logopt,
beb904
 		     MODPREFIX "failed to create submount for %s", realpath);
beb904
 		handle_mounts_startup_cond_destroy(&suc);
beb904
+		mnts_remove_submount(nap->path);
beb904
 		mounts_mutex_unlock(ap);
beb904
 		master_free_map_source(source, 1);
beb904
 		master_free_mapent(entry);
beb904
@@ -347,7 +363,6 @@ int mount_mount(struct autofs_point *ap,
beb904
 	nap->thid = thid;
beb904
 
beb904
 	ap->submnt_count++;
beb904
-	list_add(&nap->mounts, &ap->submounts);
beb904
 
beb904
 	handle_mounts_startup_cond_destroy(&suc);
beb904
 	mounts_mutex_unlock(ap);