Blame SOURCES/autofs-5.1.7-add-mount-and-umount-offsets-functions.patch

96dc52
autofs-5.1.7 - add mount and umount offsets functions
96dc52
96dc52
From: Ian Kent <raven@themaw.net>
96dc52
96dc52
Add tree_mapent_mount_offsets() and tree_mapent_umount_offsets() to
96dc52
the mapent tree handling implementation.
96dc52
96dc52
Signed-off-by: Ian Kent <raven@themaw.net>
96dc52
---
96dc52
 CHANGELOG        |    1 
96dc52
 include/mounts.h |    2 
96dc52
 lib/mounts.c     |  260 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
96dc52
 3 files changed, 263 insertions(+)
96dc52
96dc52
diff --git a/CHANGELOG b/CHANGELOG
96dc52
index 0bd6f181..892f7581 100644
96dc52
--- a/CHANGELOG
96dc52
+++ b/CHANGELOG
96dc52
@@ -39,6 +39,7 @@
96dc52
 - fix mount_fullpath().
96dc52
 - add tree_mapent_cleanup_offsets().
96dc52
 - add set_offset_tree_catatonic().
96dc52
+- add mount and umount offsets functions.
96dc52
 
96dc52
 25/01/2021 autofs-5.1.7
96dc52
 - make bind mounts propagation slave by default.
96dc52
diff --git a/include/mounts.h b/include/mounts.h
96dc52
index 5441ee0e..e56f80ba 100644
96dc52
--- a/include/mounts.h
96dc52
+++ b/include/mounts.h
96dc52
@@ -172,6 +172,8 @@ struct tree_node *tree_mapent_root(struct mapent *me);
96dc52
 int tree_mapent_add_node(struct mapent_cache *mc, const char *base, const char *key);
96dc52
 int tree_mapent_delete_offsets(struct mapent_cache *mc, const char *key);
96dc52
 void tree_mapent_cleanup_offsets(struct mapent *oe);
96dc52
+int tree_mapent_mount_offsets(struct mapent *oe, int nonstrict);
96dc52
+int tree_mapent_umount_offsets(struct mapent *oe, int nonstrict);
96dc52
 int unlink_mount_tree(struct autofs_point *ap, const char *mp);
96dc52
 void free_mnt_list(struct mnt_list *list);
96dc52
 int is_mounted(const char *mp, unsigned int type);
96dc52
diff --git a/lib/mounts.c b/lib/mounts.c
96dc52
index f075a27e..f7c29475 100644
96dc52
--- a/lib/mounts.c
96dc52
+++ b/lib/mounts.c
96dc52
@@ -1692,6 +1692,266 @@ void tree_mapent_cleanup_offsets(struct mapent *oe)
96dc52
 	}
96dc52
 }
96dc52
 
96dc52
+static int tree_mapent_rmdir_path_offset(struct autofs_point *ap, struct mapent *oe)
96dc52
+{
96dc52
+	struct mapent *mm_root = MAPENT(MAPENT_ROOT(oe));
96dc52
+	char *dir, *path;
96dc52
+	unsigned int split;
96dc52
+	int ret;
96dc52
+
96dc52
+	if (ap->type == LKP_DIRECT)
96dc52
+		return rmdir_path(ap, oe->key, mm_root->dev);
96dc52
+
96dc52
+	dir = strdup(oe->key);
96dc52
+
96dc52
+	if (ap->flags & MOUNT_FLAG_GHOST)
96dc52
+		split = ap->len + mm_root->len + 1;
96dc52
+	else
96dc52
+		split = ap->len;
96dc52
+
96dc52
+	dir[split] = '\0';
96dc52
+	path = &dir[split + 1];
96dc52
+
96dc52
+	if (chdir(dir) == -1) {
96dc52
+		error(ap->logopt, "failed to chdir to %s", dir);
96dc52
+		free(dir);
96dc52
+		return -1;
96dc52
+	}
96dc52
+
96dc52
+	ret = rmdir_path(ap, path, ap->dev);
96dc52
+
96dc52
+	free(dir);
96dc52
+
96dc52
+	if (chdir("/") == -1)
96dc52
+		error(ap->logopt, "failed to chdir to /");
96dc52
+
96dc52
+	return ret;
96dc52
+}
96dc52
+
96dc52
+static int tree_mapent_mount_offset(struct mapent *oe, void *ptr)
96dc52
+{
96dc52
+	struct traverse_subtree_context *ctxt = ptr;
96dc52
+	struct autofs_point *ap = ctxt->ap;
96dc52
+	int ret;
96dc52
+
96dc52
+	debug(ap->logopt, "mount offset %s", oe->key);
96dc52
+
96dc52
+	ret = mount_autofs_offset(ap, oe);
96dc52
+	if (ret < MOUNT_OFFSET_OK) {
96dc52
+		if (ret != MOUNT_OFFSET_IGNORE) {
96dc52
+			warn(ap->logopt, "failed to mount offset");
96dc52
+			return 0;
96dc52
+		} else {
96dc52
+			debug(ap->logopt,
96dc52
+			      "ignoring \"nohide\" trigger %s", oe->key);
96dc52
+			/*
96dc52
+			 * Ok, so we shouldn't modify the mapent but
96dc52
+			 * mount requests are blocked at a point above
96dc52
+			 * this and expire only uses the mapent key or
96dc52
+			 * holds the cache write lock.
96dc52
+			 */
96dc52
+			free(oe->mapent);
96dc52
+			oe->mapent = NULL;
96dc52
+		}
96dc52
+	}
96dc52
+
96dc52
+	return 1;
96dc52
+}
96dc52
+
96dc52
+static int tree_mapent_umount_offset(struct mapent *oe, void *ptr)
96dc52
+{
96dc52
+	struct traverse_subtree_context *ctxt = ptr;
96dc52
+	struct autofs_point *ap = ctxt->ap;
96dc52
+	int ret = 1;
96dc52
+
96dc52
+	/*
96dc52
+	 * Check for and umount subtree offsets resulting from
96dc52
+	 * nonstrict mount fail.
96dc52
+	 */
96dc52
+	ret = tree_mapent_umount_offsets(oe, ctxt->strict);
96dc52
+	if (!ret)
96dc52
+		return 0;
96dc52
+
96dc52
+	/*
96dc52
+	 * If an offset that has an active mount has been removed
96dc52
+	 * from the multi-mount we don't want to attempt to trigger
96dc52
+	 * mounts for it. Obviously this is because it has been
96dc52
+	 * removed, but less obvious is the potential strange
96dc52
+	 * behaviour that can result if we do try and mount it
96dc52
+	 * again after it's been expired. For example, if an NFS
96dc52
+	 * file system is no longer exported and is later umounted
96dc52
+	 * it can be mounted again without any error message but
96dc52
+	 * shows as an empty directory. That's going to confuse
96dc52
+	 * people for sure.
96dc52
+	 *
96dc52
+	 * If the mount cannot be umounted (the process is now
96dc52
+	 * using a stale mount) the offset needs to be invalidated
96dc52
+	 * so no further mounts will be attempted but the offset
96dc52
+	 * cache entry must remain so expires can continue to
96dc52
+	 * attempt to umount it. If the mount can be umounted and
96dc52
+	 * the offset is removed, at least for NFS we will get
96dc52
+	 * ESTALE errors when attempting list the directory.
96dc52
+	 */
96dc52
+	if (oe->ioctlfd != -1 ||
96dc52
+	    is_mounted(oe->key, MNTS_REAL)) {
96dc52
+		if (umount_ent(ap, oe->key) &&
96dc52
+		    is_mounted(oe->key, MNTS_REAL)) {
96dc52
+			debug(ap->logopt,
96dc52
+			      "offset %s has active mount, invalidate",
96dc52
+			      oe->key);
96dc52
+			/*
96dc52
+			 * Ok, so we shouldn't modify the mapent but
96dc52
+			 * mount requests are blocked at a point above
96dc52
+			 * this and expire only uses the mapent key or
96dc52
+			 * holds the cache write lock.
96dc52
+			 */
96dc52
+			if (oe->mapent) {
96dc52
+				free(oe->mapent);
96dc52
+				oe->mapent = NULL;
96dc52
+			}
96dc52
+			return 0;
96dc52
+		}
96dc52
+	}
96dc52
+
96dc52
+	/* Don't bother if there's noting to umount. */
96dc52
+	if (!is_mounted(oe->key, MNTS_AUTOFS))
96dc52
+		goto done;
96dc52
+
96dc52
+	debug(ap->logopt, "umount offset %s", oe->key);
96dc52
+
96dc52
+	if (umount_autofs_offset(ap, oe)) {
96dc52
+		warn(ap->logopt, "failed to umount offset");
96dc52
+		ret = 0;
96dc52
+	} else {
96dc52
+		struct stat st;
96dc52
+		int ret;
96dc52
+
96dc52
+		if (!(oe->flags & MOUNT_FLAG_DIR_CREATED))
96dc52
+			goto done;
96dc52
+
96dc52
+		/*
96dc52
+		 * An error due to partial directory removal is
96dc52
+		 * ok so only try and remount the offset if the
96dc52
+		 * actual mount point still exists.
96dc52
+		 */
96dc52
+		ret = tree_mapent_rmdir_path_offset(ap, oe);
96dc52
+		if (ret == -1 && !stat(oe->key, &st)) {
96dc52
+			ret = tree_mapent_mount_offset(oe, ctxt);
96dc52
+			/* But we did origianlly create this */
96dc52
+			oe->flags |= MOUNT_FLAG_DIR_CREATED;
96dc52
+		}
96dc52
+	}
96dc52
+done:
96dc52
+	return ret;
96dc52
+}
96dc52
+
96dc52
+static int tree_mapent_mount_offsets_work(struct tree_node *n, void *ptr)
96dc52
+{
96dc52
+	struct traverse_subtree_context *ctxt = ptr;
96dc52
+	struct mapent *oe = MAPENT(n);
96dc52
+	struct mapent *mm_root = MAPENT(MAPENT_ROOT(oe));
96dc52
+	struct autofs_point *ap = ctxt->ap;
96dc52
+	int ret;
96dc52
+
96dc52
+	if (!oe->mapent)
96dc52
+		return 1;
96dc52
+
96dc52
+	/* Stale offset, no longer present in the mapent */
96dc52
+	if (oe->age != mm_root->age) {
96dc52
+		/* Best effort */
96dc52
+		tree_mapent_umount_offset(oe, ctxt);
96dc52
+		return 1;
96dc52
+	}
96dc52
+
96dc52
+	ret = tree_mapent_mount_offset(oe, ctxt);
96dc52
+
96dc52
+	/*
96dc52
+	 * If re-constructing a multi-mount it's necessary to walk
96dc52
+	 * into nested mounts, unlike the usual "mount only what's
96dc52
+	 * needed as you go" behavior.
96dc52
+	 */
96dc52
+	if (ap->state == ST_READMAP && ap->flags & MOUNT_FLAG_REMOUNT) {
96dc52
+		if (oe->ioctlfd != -1 ||
96dc52
+		    is_mounted(oe->key, MNTS_REAL))
96dc52
+			/* Best effort */
96dc52
+			tree_mapent_mount_offsets(oe, !ctxt->strict);
96dc52
+	}
96dc52
+
96dc52
+	return ret;
96dc52
+}
96dc52
+
96dc52
+int tree_mapent_mount_offsets(struct mapent *oe, int nonstrict)
96dc52
+{
96dc52
+	struct tree_node *base = MAPENT_NODE(oe);
96dc52
+	struct traverse_subtree_context ctxt = {
96dc52
+		.ap = oe->mc->ap,
96dc52
+		.base = base,
96dc52
+		.strict = !nonstrict,
96dc52
+	};
96dc52
+
96dc52
+	return tree_mapent_traverse_subtree(base,
96dc52
+				tree_mapent_mount_offsets_work, &ctxt);
96dc52
+}
96dc52
+
96dc52
+static int tree_mapent_umount_offsets_work(struct tree_node *n, void *ptr)
96dc52
+{
96dc52
+	struct mapent *oe = MAPENT(n);
96dc52
+
96dc52
+	return tree_mapent_umount_offset(oe, ptr);
96dc52
+}
96dc52
+
96dc52
+int tree_mapent_umount_offsets(struct mapent *oe, int nonstrict)
96dc52
+{
96dc52
+	struct tree_node *base = MAPENT_NODE(oe);
96dc52
+	struct autofs_point *ap = oe->mc->ap;
96dc52
+	struct traverse_subtree_context ctxt = {
96dc52
+		.ap = ap,
96dc52
+		.base = base,
96dc52
+		.strict = !nonstrict,
96dc52
+	};
96dc52
+	int ret;
96dc52
+
96dc52
+	ret = tree_mapent_traverse_subtree(base,
96dc52
+				tree_mapent_umount_offsets_work, &ctxt);
96dc52
+	if (ret && tree_mapent_is_root(oe)) {
96dc52
+		char mp[PATH_MAX + 1];
96dc52
+
96dc52
+		/*
96dc52
+		 * The map entry cache stores mapent keys. For indirect
96dc52
+		 * mount maps they are single direcory components so when
96dc52
+		 * one of these keys is the root of a multi-mount the mount
96dc52
+		 * path must be constructed.
96dc52
+		 */
96dc52
+		if (!mount_fullpath(mp, PATH_MAX, ap->path, oe->key)) {
96dc52
+			error(ap->logopt, "mount path is too long");
96dc52
+			return 0;
96dc52
+		}
96dc52
+
96dc52
+		/*
96dc52
+		 * Special case.
96dc52
+		 * If we can't umount the root container then we can't
96dc52
+		 * delete the offsets from the cache and we need to put
96dc52
+		 * the offset triggers back.
96dc52
+		 */
96dc52
+		if (is_mounted(mp, MNTS_REAL)) {
96dc52
+			info(ap->logopt, "unmounting dir = %s", mp);
96dc52
+			if (umount_ent(ap, mp) &&
96dc52
+			    is_mounted(mp, MNTS_REAL)) {
96dc52
+				if (!tree_mapent_mount_offsets(oe, 1))
96dc52
+					warn(ap->logopt,
96dc52
+					     "failed to remount offset triggers");
96dc52
+				return 0;
96dc52
+			}
96dc52
+		}
96dc52
+
96dc52
+		/* check for mounted mount entry and remove it if found */
96dc52
+		mnts_remove_mount(mp, MNTS_MOUNTED);
96dc52
+
96dc52
+	}
96dc52
+
96dc52
+	return ret;
96dc52
+}
96dc52
+
96dc52
 /* From glibc decode_name() */
96dc52
 /* Since the values in a line are separated by spaces, a name cannot
96dc52
  * contain a space.  Therefore some programs encode spaces in names