autofs-5.1.9 - refactor mnts_get_expire_list()

From: Ian Kent <raven@themaw.net>

Use source and destination list arguments instead of a list and a
struct autofs_point.

Also factor out the addition of tree nodes.

Signed-off-by: Ian Kent <raven@themaw.net>
---
 CHANGELOG         |    1 +
 daemon/direct.c   |    2 +-
 daemon/indirect.c |    4 ++--
 include/mounts.h  |    2 +-
 lib/mounts.c      |   51 ++++++++++++++++++++++++++++-----------------------
 5 files changed, 33 insertions(+), 27 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 428fbd837..92210eb72 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -67,6 +67,7 @@
 - fix leak with non-strict mount fails.
 - fix timed_read() error return.
 - fix state queue not processing state changes.
+- refactor mnts_get_expire_list().
 
 02/11/2023 autofs-5.1.9
 - fix kernel mount status notification.
diff --git a/daemon/direct.c b/daemon/direct.c
index 34cf2cf2d..9566ee71c 100644
--- a/daemon/direct.c
+++ b/daemon/direct.c
@@ -837,7 +837,7 @@ void *expire_proc_direct(void *arg)
 	left = 0;
 
 	/* Get the list of real mounts and expire them if possible */
-	mnts_get_expire_list(&mnts, ap);
+	mnts_get_expire_list(&ap->mounts, &mnts);
 	if (list_empty(&mnts))
 		goto done;
 	pthread_cleanup_push(mnts_cleanup, &mnts);
diff --git a/daemon/indirect.c b/daemon/indirect.c
index cf8f2934e..0e81360b4 100644
--- a/daemon/indirect.c
+++ b/daemon/indirect.c
@@ -369,7 +369,7 @@ void *expire_proc_indirect(void *arg)
 	left = 0;
 
 	/* Get the list of real mounts and expire them if possible */
-	mnts_get_expire_list(&mnts, ap);
+	mnts_get_expire_list(&ap->mounts, &mnts);
 	if (list_empty(&mnts))
 		goto done;
 	pthread_cleanup_push(mnts_cleanup, &mnts);
@@ -514,7 +514,7 @@ void *expire_proc_indirect(void *arg)
 	pthread_cleanup_pop(1);
 
 	count = offsets = submnts = 0;
-	mnts_get_expire_list(&mnts, ap);
+	mnts_get_expire_list(&ap->mounts, &mnts);
 	pthread_cleanup_push(mnts_cleanup, &mnts);
 	/* Are there any real mounts left */
 	list_for_each_entry(mnt, &mnts, expire) {
diff --git a/include/mounts.h b/include/mounts.h
index 8b3acac51..08b3ff424 100644
--- a/include/mounts.h
+++ b/include/mounts.h
@@ -178,7 +178,7 @@ struct mnt_list *get_mnt_list(const char *path, int include);
 unsigned int mnts_has_mounted_mounts(struct autofs_point *ap);
 int tree_traverse_inorder(struct tree_node *n, tree_work_fn_t work, void *ptr);
 void tree_free(struct tree_node *root);
-void mnts_get_expire_list(struct list_head *mnts, struct autofs_point *ap);
+void mnts_get_expire_list(struct list_head *mounted, struct list_head *mnts);
 void mnts_put_expire_list(struct list_head *mnts);
 void mnts_set_mounted_mount(struct autofs_point *ap, const char *name, unsigned int flags);
 struct tree_node *tree_host_root(struct exportinfo *exp);
diff --git a/lib/mounts.c b/lib/mounts.c
index 9e7fbcdd1..c2b8ed7cc 100644
--- a/lib/mounts.c
+++ b/lib/mounts.c
@@ -1493,42 +1493,47 @@ static int tree_mnt_expire_list_work(struct tree_node *n, void *ptr)
 	return 1;
 }
 
-void mnts_get_expire_list(struct list_head *mnts, struct autofs_point *ap)
+static struct tree_node *mnts_add_tree_node(struct tree_node *tree, struct mnt_list *mnt)
+{
+	struct tree_node *n;
+
+	if (!tree) {
+		tree = tree_mnt_root(mnt);
+		if (!tree)
+			error(LOGOPT_ANY, "failed to create expire tree root");
+		return tree;
+	}
+
+	n = tree_add_node(tree, mnt);
+	if (!n) {
+		error(LOGOPT_ANY, "failed to add expire tree node");
+		tree_free(tree);
+		tree = NULL;
+	}
+
+	return tree;
+}
+
+void mnts_get_expire_list(struct list_head *mounted, struct list_head *mnts)
 {
 	struct tree_node *tree = NULL;
 	struct mnt_list *mnt;
 
 	mnts_hash_mutex_lock();
-	if (list_empty(&ap->mounts))
+	if (list_empty(mounted))
 		goto done;
 
-	list_for_each_entry(mnt, &ap->mounts, mount) {
-		struct tree_node *n;
-
+	list_for_each_entry(mnt, mounted, mount) {
 		if (!(mnt->flags & MNTS_MOUNTED))
 			continue;
 
-		if (!tree) {
-			tree = tree_mnt_root(mnt);
-			if (!tree) {
-				error(LOGOPT_ANY, "failed to create expire tree root");
-				goto done;
-			}
-			continue;
-		}
-
-		n = tree_add_node(tree, mnt);
-		if (!n) {
-			error(LOGOPT_ANY, "failed to add expire tree node");
-			tree_free(tree);
+		tree = mnts_add_tree_node(tree, mnt);
+		if (!tree)
 			goto done;
-		}
 	}
 
-	if (tree) {
-		tree_traverse_inorder(tree, tree_mnt_expire_list_work, mnts);
-		tree_free(tree);
-	}
+	tree_traverse_inorder(tree, tree_mnt_expire_list_work, mnts);
+	tree_free(tree);
 done:
 	mnts_hash_mutex_unlock();
 }
