Blame SOURCES/autofs-5.1.6-use-struct-mnt_list-to-track-mounted-mounts.patch

beb904
autofs-5.1.6 - use struct mnt_list to track mounted mounts
beb904
beb904
From: Ian Kent <raven@themaw.net>
beb904
beb904
Use struct mnt_list to track mounted mounts so that it's no longer
beb904
necessary to use the system mount table to get the list of mounted
beb904
mounts.
beb904
beb904
Signed-off-by: Ian Kent <raven@themaw.net>
beb904
---
beb904
 CHANGELOG          |    1 
beb904
 daemon/automount.c |    5 ++
beb904
 daemon/direct.c    |   19 ++++++++
beb904
 daemon/indirect.c  |    3 +
beb904
 include/mounts.h   |   10 ++++
beb904
 lib/mounts.c       |  125 +++++++++++++++++++++++++++++++++++++++++++++++++----
beb904
 6 files changed, 154 insertions(+), 9 deletions(-)
beb904
beb904
--- autofs-5.1.4.orig/CHANGELOG
beb904
+++ autofs-5.1.4/CHANGELOG
beb904
@@ -128,6 +128,7 @@ xx/xx/2018 autofs-5.1.5
beb904
 - remove force parameter from umount_all().
beb904
 - fix remount expire.
beb904
 - fix stale offset directories disable mount.
beb904
+- use struct mnt_list to track mounted mounts.
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
@@ -677,6 +677,9 @@ int umount_multi(struct autofs_point *ap
beb904
 			mnts_put_mount(mnt);
beb904
 		}
beb904
 
beb904
+		/* Check for mounted mount and remove it if found */
beb904
+		mnts_remove_mount(path, MNTS_MOUNTED);
beb904
+
beb904
 		return 0;
beb904
 	}
beb904
 
beb904
@@ -1719,6 +1722,8 @@ static void handle_mounts_cleanup(void *
beb904
 
beb904
 		/* Submount at ap->path belongs to parent submount list. */
beb904
 		mnts_remove_submount(ap->path);
beb904
+		/* Also remove from parent mounted list */
beb904
+		mnts_remove_mount(ap->path, MNTS_MOUNTED);
beb904
 		mnt = mnts_find_amdmount(ap->path);
beb904
 		if (mnt) {
beb904
 			mnts_remove_amdmount(ap->path);
beb904
--- autofs-5.1.4.orig/daemon/direct.c
beb904
+++ autofs-5.1.4/daemon/direct.c
beb904
@@ -606,6 +606,9 @@ force_umount:
beb904
 	} else
beb904
 		info(ap->logopt, "umounted offset mount %s", me->key);
beb904
 
beb904
+	if (!rv)
beb904
+		mnts_remove_mount(me->key, MNTS_OFFSET);
beb904
+
beb904
 	return rv;
beb904
 }
beb904
 
beb904
@@ -622,6 +625,7 @@ int mount_autofs_offset(struct autofs_po
beb904
 	const char *map_name = hosts_map_name;
beb904
 	const char *type;
beb904
 	char mountpoint[PATH_MAX];
beb904
+	struct mnt_list *mnt;
beb904
 
beb904
 	if (ops->version && ap->flags & MOUNT_FLAG_REMOUNT) {
beb904
 		ret = try_remount(ap, me, t_offset);
beb904
@@ -635,6 +639,11 @@ int mount_autofs_offset(struct autofs_po
beb904
 			if (ap->state != ST_READMAP)
beb904
 				warn(ap->logopt,
beb904
 				     "trigger %s already mounted", me->key);
beb904
+			mnt = mnts_add_mount(ap, me->key, MNTS_OFFSET);
beb904
+			if (!mnt)
beb904
+				error(ap->logopt,
beb904
+				      "failed to add offset mount %s to mounted list",
beb904
+				      me->key);
beb904
 			return MOUNT_OFFSET_OK;
beb904
 		}
beb904
 
beb904
@@ -757,6 +766,12 @@ int mount_autofs_offset(struct autofs_po
beb904
 		notify_mount_result(ap, me->key, timeout, str_offset);
beb904
 	ops->close(ap->logopt, ioctlfd);
beb904
 
beb904
+	mnt = mnts_add_mount(ap, mountpoint, MNTS_OFFSET);
beb904
+	if (!mnt)
beb904
+		error(ap->logopt,
beb904
+		      "failed to add offset mount %s to mounted list",
beb904
+		      mountpoint);
beb904
+
beb904
 	debug(ap->logopt, "mounted trigger %s at %s", me->key, mountpoint);
beb904
 
beb904
 	return MOUNT_OFFSET_OK;
beb904
@@ -877,6 +892,7 @@ void *expire_proc_direct(void *arg)
beb904
 				ops->close(ap->logopt, me->ioctlfd);
beb904
 				me->ioctlfd = -1;
beb904
 				cache_unlock(me->mc);
beb904
+				mnts_remove_mount(next->mp, MNTS_MOUNTED);
beb904
 				pthread_setcancelstate(cur_state, NULL);
beb904
 				continue;
beb904
 			}
beb904
@@ -1238,7 +1254,10 @@ static void *do_mount_direct(void *arg)
beb904
 		cache_unlock(mt.mc);
beb904
 		if (close_fd)
beb904
 			ops->close(ap->logopt, mt.ioctlfd);
beb904
+
beb904
 		info(ap->logopt, "mounted %s", mt.name);
beb904
+
beb904
+		mnts_set_mounted_mount(ap, mt.name);
beb904
 	} else {
beb904
 		/* TODO: get mount return status from lookup_nss_mount */
beb904
 		ops->send_fail(ap->logopt,
beb904
--- autofs-5.1.4.orig/daemon/indirect.c
beb904
+++ autofs-5.1.4/daemon/indirect.c
beb904
@@ -751,7 +751,10 @@ static void *do_mount_indirect(void *arg
beb904
 	if (status) {
beb904
 		ops->send_ready(ap->logopt,
beb904
 				ap->ioctlfd, mt.wait_queue_token);
beb904
+
beb904
 		info(ap->logopt, "mounted %s", buf);
beb904
+
beb904
+		mnts_set_mounted_mount(ap, mt.name);
beb904
 	} else {
beb904
 		/* TODO: get mount return status from lookup_nss_mount */
beb904
 		ops->send_fail(ap->logopt,
beb904
--- autofs-5.1.4.orig/include/mounts.h
beb904
+++ autofs-5.1.4/include/mounts.h
beb904
@@ -39,6 +39,7 @@
beb904
 #define MNTS_DIRECT	0x0010
beb904
 #define MNTS_OFFSET	0x0020
beb904
 #define MNTS_AMD_MOUNT	0x0040
beb904
+#define MNTS_MOUNTED	0x0080
beb904
 
beb904
 #define REMOUNT_SUCCESS		0x0000
beb904
 #define REMOUNT_FAIL		0x0001
beb904
@@ -60,6 +61,9 @@ struct mnt_list {
beb904
 	struct hlist_node hash;
beb904
 	unsigned int ref;
beb904
 
beb904
+	/* List of mounts of an autofs_point */
beb904
+	struct list_head mount;
beb904
+
beb904
 	/* List of sub-mounts of an autofs_point */
beb904
 	struct autofs_point *ap;
beb904
 	struct list_head submount;
beb904
@@ -130,7 +134,13 @@ void mnts_put_submount_list(struct list_
beb904
 struct mnt_list *mnts_find_amdmount(const char *path);
beb904
 struct mnt_list *mnts_add_amdmount(struct autofs_point *ap, struct amd_entry *entry);
beb904
 void mnts_remove_amdmount(const char *mp);
beb904
+struct mnt_list *mnts_add_mount(struct autofs_point *ap, const char *name, unsigned int flags);
beb904
+void mnts_remove_mount(const char *mp, unsigned int flags);
beb904
 struct mnt_list *get_mnt_list(const char *path, int include);
beb904
+unsigned int mnts_has_mounted_mounts(struct autofs_point *ap);
beb904
+void mnts_get_expire_list(struct list_head *mnts, struct autofs_point *ap);
beb904
+void mnts_put_expire_list(struct list_head *mnts);
beb904
+void mnts_set_mounted_mount(struct autofs_point *ap, const char *name);
beb904
 int unlink_mount_tree(struct autofs_point *ap, const char *mp);
beb904
 void free_mnt_list(struct mnt_list *list);
beb904
 int is_mounted(const char *mp, unsigned int type);
beb904
--- autofs-5.1.4.orig/lib/mounts.c
beb904
+++ autofs-5.1.4/lib/mounts.c
beb904
@@ -884,6 +884,7 @@ static struct mnt_list *mnts_alloc_mount
beb904
 
beb904
 	this->ref = 1;
beb904
 	INIT_HLIST_NODE(&this->hash);
beb904
+	INIT_LIST_HEAD(&this->mount);
beb904
 	INIT_LIST_HEAD(&this->submount);
beb904
 	INIT_LIST_HEAD(&this->submount_work);
beb904
 	INIT_LIST_HEAD(&this->amdmount);
beb904
@@ -1149,6 +1150,90 @@ done:
beb904
 	mnts_hash_mutex_unlock();
beb904
 }
beb904
 
beb904
+struct mnt_list *mnts_add_mount(struct autofs_point *ap,
beb904
+				const char *name, unsigned int flags)
beb904
+{
beb904
+	struct mnt_list *this;
beb904
+	char *mp;
beb904
+
beb904
+	if (*name == '/') {
beb904
+		mp = strdup(name);
beb904
+		if (!mp)
beb904
+			goto fail;
beb904
+	} else {
beb904
+		int len = strlen(ap->path) + strlen(name) + 2;
beb904
+
beb904
+		mp = malloc(len);
beb904
+		if (!mp)
beb904
+			goto fail;
beb904
+		strcpy(mp, ap->path);
beb904
+		strcat(mp, "/");
beb904
+		strcat(mp, name);
beb904
+	}
beb904
+
beb904
+	mnts_hash_mutex_lock();
beb904
+	this = mnts_get_mount(mp);
beb904
+	if (this) {
beb904
+		this->flags |= flags;
beb904
+		if (list_empty(&this->mount))
beb904
+			list_add(&this->mount, &ap->mounts);
beb904
+	}
beb904
+	mnts_hash_mutex_unlock();
beb904
+	free(mp);
beb904
+
beb904
+	return this;
beb904
+fail:
beb904
+	if (mp)
beb904
+		free(mp);
beb904
+	return NULL;
beb904
+}
beb904
+
beb904
+void mnts_remove_mount(const char *mp, unsigned int flags)
beb904
+{
beb904
+	struct mnt_list *this;
beb904
+
beb904
+	mnts_hash_mutex_lock();
beb904
+	this = mnts_lookup(mp);
beb904
+	if (this && this->flags & flags) {
beb904
+		this->flags &= ~flags;
beb904
+		if (!(this->flags & (MNTS_OFFSET|MNTS_MOUNTED)))
beb904
+			list_del_init(&this->mount);
beb904
+		__mnts_put_mount(this);
beb904
+	}
beb904
+	mnts_hash_mutex_unlock();
beb904
+}
beb904
+
beb904
+void mnts_set_mounted_mount(struct autofs_point *ap, const char *name)
beb904
+{
beb904
+	struct mnt_list *mnt;
beb904
+
beb904
+	mnt = mnts_add_mount(ap, name, MNTS_MOUNTED);
beb904
+	if (!mnt) {
beb904
+		error(ap->logopt,
beb904
+		      "failed to add mount %s to mounted list", name);
beb904
+		return;
beb904
+	}
beb904
+
beb904
+	/* Offset mount failed but non-strict returns success */
beb904
+	if (mnt->flags & MNTS_OFFSET &&
beb904
+	    !is_mounted(mnt->mp, MNTS_REAL)) {
beb904
+		mnt->flags &= ~MNTS_MOUNTED;
beb904
+		mnts_put_mount(mnt);
beb904
+	}
beb904
+
beb904
+	/* Housekeeping.
beb904
+	 * Set the base type of the mounted mount.
beb904
+	 * MNTS_AUTOFS and MNTS_OFFSET are set at mount time and
beb904
+	 * are used during expire.
beb904
+	 */
beb904
+	if (!(mnt->flags & (MNTS_AUTOFS|MNTS_OFFSET))) {
beb904
+		if (ap->type == LKP_INDIRECT)
beb904
+			mnt->flags |= MNTS_INDIRECT;
beb904
+		else
beb904
+			mnt->flags |= MNTS_DIRECT;
beb904
+	}
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
@@ -1962,7 +2047,8 @@ void notify_mount_result(struct autofs_p
beb904
 	return;
beb904
 }
beb904
 
beb904
-static int do_remount_direct(struct autofs_point *ap, int fd, const char *path)
beb904
+static int do_remount_direct(struct autofs_point *ap,
beb904
+			     const unsigned int type, int fd, const char *path)
beb904
 {
beb904
 	struct ioctl_ops *ops = get_ioctl_ops();
beb904
 	int status = REMOUNT_SUCCESS;
beb904
@@ -1975,9 +2061,21 @@ static int do_remount_direct(struct auto
beb904
 		set_tsd_user_vars(ap->logopt, uid, gid);
beb904
 
beb904
 	ret = lookup_nss_mount(ap, NULL, path, strlen(path));
beb904
-	if (ret)
beb904
+	if (ret) {
beb904
+		struct mnt_list *mnt;
beb904
+
beb904
+		/* If it's an offset mount add a mount reference */
beb904
+		if (type == t_offset) {
beb904
+			mnt = mnts_add_mount(ap, path, MNTS_OFFSET);
beb904
+			if (!mnt)
beb904
+				error(ap->logopt,
beb904
+				      "failed to add mount %s to mounted list", path);
beb904
+		}
beb904
+
beb904
+		mnts_set_mounted_mount(ap, path);
beb904
+
beb904
 		info(ap->logopt, "re-connected to %s", path);
beb904
-	else {
beb904
+	} else {
beb904
 		status = REMOUNT_FAIL;
beb904
 		info(ap->logopt, "failed to re-connect %s", path);
beb904
 	}
beb904
@@ -1985,7 +2083,7 @@ static int do_remount_direct(struct auto
beb904
 	return status;
beb904
 }
beb904
 
beb904
-static int do_remount_indirect(struct autofs_point *ap, int fd, const char *path)
beb904
+static int do_remount_indirect(struct autofs_point *ap, const unsigned int type, int fd, const char *path)
beb904
 {
beb904
 	struct ioctl_ops *ops = get_ioctl_ops();
beb904
 	int status = REMOUNT_SUCCESS;
beb904
@@ -2046,9 +2144,11 @@ static int do_remount_indirect(struct au
beb904
 		len = strlen(de[n]->d_name);
beb904
 
beb904
 		ret = lookup_nss_mount(ap, NULL, de[n]->d_name, len);
beb904
-		if (ret)
beb904
+		if (ret) {
beb904
+			mnts_set_mounted_mount(ap, buf);
beb904
+
beb904
 			info(ap->logopt, "re-connected to %s", buf);
beb904
-		else {
beb904
+		} else {
beb904
 			status = REMOUNT_FAIL;
beb904
 			info(ap->logopt, "failed to re-connect %s", buf);
beb904
 		}
beb904
@@ -2149,9 +2249,9 @@ static int remount_active_mount(struct a
beb904
 		 * following will be broken?
beb904
 		 */
beb904
 		if (type == t_indirect)
beb904
-			do_remount_indirect(ap, fd, path);
beb904
+			do_remount_indirect(ap, type, fd, path);
beb904
 		else
beb904
-			do_remount_direct(ap, fd, path);
beb904
+			do_remount_direct(ap, type, fd, path);
beb904
 	}
beb904
 
beb904
 	debug(ap->logopt, "re-connected to mount %s", path);
beb904
@@ -2389,7 +2489,7 @@ int umount_ent(struct autofs_point *ap,
beb904
 		 * so that we do not try to call rmdir_path on the
beb904
 		 * directory.
beb904
 		 */
beb904
-		if (!rv && is_mounted(path, MNTS_REAL)) {
beb904
+		if (is_mounted(path, MNTS_REAL)) {
beb904
 			crit(ap->logopt,
beb904
 			     "the umount binary reported that %s was "
beb904
 			     "unmounted, but there is still something "
beb904
@@ -2398,6 +2498,10 @@ int umount_ent(struct autofs_point *ap,
beb904
 		}
beb904
 	}
beb904
 
beb904
+	/* On success, check for mounted mount and remove it if found */
beb904
+	if (!rv)
beb904
+		mnts_remove_mount(path, MNTS_MOUNTED);
beb904
+
beb904
 	return rv;
beb904
 }
beb904
 
beb904
@@ -2690,6 +2794,9 @@ int umount_multi_triggers(struct autofs_
beb904
 		status = cache_delete_offset_list(mc, me->key);
beb904
 		if (status != CHE_OK)
beb904
 			warn(ap->logopt, "couldn't delete offset list");
beb904
+
beb904
+	       /* check for mounted mount entry and remove it if found */
beb904
+               mnts_remove_mount(root, MNTS_MOUNTED);
beb904
 	}
beb904
 
beb904
 	return left;