Blame SOURCES/autofs-5.1.7-make-tree-implementation-data-independent.patch

96dc52
autofs-5.1.7 - make tree implementation data independent
96dc52
96dc52
From: Ian Kent <raven@themaw.net>
96dc52
96dc52
Generalise the tree implementation so that it's independent of the
96dc52
data structure that's used.
96dc52
96dc52
Do this by refactoring it into core tree functions and functions
96dc52
specific to the data structure to be used so that different data
96dc52
structures can be used when needed by adding an implementation for
96dc52
the data structure specific functions.
96dc52
96dc52
Signed-off-by: Ian Kent <raven@themaw.net>
96dc52
---
96dc52
 CHANGELOG        |    1 
96dc52
 include/mounts.h |   29 +++++++++
96dc52
 lib/mounts.c     |  174 ++++++++++++++++++++++++++++++++++--------------------
96dc52
 3 files changed, 140 insertions(+), 64 deletions(-)
96dc52
96dc52
diff --git a/CHANGELOG b/CHANGELOG
96dc52
index 0dae6761..74571570 100644
96dc52
--- a/CHANGELOG
96dc52
+++ b/CHANGELOG
96dc52
@@ -31,6 +31,7 @@
96dc52
 - add some multi-mount macros.
96dc52
 - remove unused functions cache_dump_multi() and cache_dump_cache().
96dc52
 - add a len field to struct autofs_point.
96dc52
+- make tree implementation data independent.
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 ac480c06..71d29566 100644
96dc52
--- a/include/mounts.h
96dc52
+++ b/include/mounts.h
96dc52
@@ -51,10 +51,36 @@ extern const unsigned int t_indirect;
96dc52
 extern const unsigned int t_direct;
96dc52
 extern const unsigned int t_offset;
96dc52
 
96dc52
+struct mnt_list;
96dc52
 struct mapent;
96dc52
 
96dc52
+struct tree_ops;
96dc52
+
96dc52
+struct tree_node {
96dc52
+	struct tree_ops *ops;
96dc52
+	struct tree_node *left;
96dc52
+	struct tree_node *right;
96dc52
+};
96dc52
+#define INIT_TREE_NODE(ptr)	((ptr)->ops = NULL, (ptr)->left = NULL, (ptr)->right = NULL)
96dc52
+
96dc52
+#define MNT_LIST(n)		(container_of(n, struct mnt_list, node))
96dc52
+#define MNT_LIST_NODE(ptr)	((struct tree_node *) &((struct mnt_list *) ptr)->node)
96dc52
+
96dc52
+typedef struct tree_node *(*tree_new_t) (void *ptr);
96dc52
+typedef int  (*tree_cmp_t) (struct tree_node *n, void *ptr);
96dc52
+typedef void (*tree_free_t) (struct tree_node *n);
96dc52
+
96dc52
+struct tree_ops {
96dc52
+	tree_new_t new;
96dc52
+	tree_cmp_t cmp;
96dc52
+	tree_free_t free;
96dc52
+};
96dc52
+
96dc52
+typedef int (*tree_work_fn_t) (struct tree_node *n, void *ptr);
96dc52
+
96dc52
 struct mnt_list {
96dc52
 	char *mp;
96dc52
+	size_t len;
96dc52
 	unsigned int flags;
96dc52
 
96dc52
 	/* Hash of all mounts */
96dc52
@@ -79,6 +105,9 @@ struct mnt_list {
96dc52
 	unsigned int amd_cache_opts;
96dc52
 	struct list_head amdmount;
96dc52
 
96dc52
+	/* Tree operations */
96dc52
+	struct tree_node node;
96dc52
+
96dc52
 	/*
96dc52
 	 * List operations ie. get_mnt_list.
96dc52
 	 */
96dc52
diff --git a/lib/mounts.c b/lib/mounts.c
96dc52
index b478ecb4..a6d1c5a7 100644
96dc52
--- a/lib/mounts.c
96dc52
+++ b/lib/mounts.c
96dc52
@@ -68,6 +68,17 @@ static pthread_mutex_t ext_mount_hash_mutex = PTHREAD_MUTEX_INITIALIZER;
96dc52
 static DEFINE_HASHTABLE(mnts_hash, MNTS_HASH_BITS);
96dc52
 static pthread_mutex_t mnts_hash_mutex = PTHREAD_MUTEX_INITIALIZER;
96dc52
 
96dc52
+static struct tree_node *tree_mnt_new(void *ptr);
96dc52
+static int tree_mnt_cmp(struct tree_node *n, void *ptr);
96dc52
+static void tree_mnt_free(struct tree_node *n);
96dc52
+
96dc52
+static struct tree_ops mnt_ops = {
96dc52
+	.new = tree_mnt_new,
96dc52
+	.cmp = tree_mnt_cmp,
96dc52
+	.free = tree_mnt_free,
96dc52
+};
96dc52
+static struct tree_ops *tree_mnt_ops = &mnt_ops;
96dc52
+
96dc52
 unsigned int linux_version_code(void)
96dc52
 {
96dc52
 	struct utsname my_utsname;
96dc52
@@ -904,6 +915,7 @@ static struct mnt_list *mnts_alloc_mount(const char *mp)
96dc52
 		this = NULL;
96dc52
 		goto done;
96dc52
 	}
96dc52
+	this->len = strlen(mp);
96dc52
 
96dc52
 	this->ref = 1;
96dc52
 	INIT_HLIST_NODE(&this->hash);
96dc52
@@ -912,6 +924,7 @@ static struct mnt_list *mnts_alloc_mount(const char *mp)
96dc52
 	INIT_LIST_HEAD(&this->submount_work);
96dc52
 	INIT_LIST_HEAD(&this->amdmount);
96dc52
 	INIT_LIST_HEAD(&this->expire);
96dc52
+	INIT_TREE_NODE(&this->node);
96dc52
 done:
96dc52
 	return this;
96dc52
 }
96dc52
@@ -1225,91 +1238,58 @@ done:
96dc52
 	return has_mounted_mounts;
96dc52
 }
96dc52
 
96dc52
-struct tree_node {
96dc52
-	struct mnt_list *mnt;
96dc52
-	struct tree_node *left;
96dc52
-	struct tree_node *right;
96dc52
-};
96dc52
-
96dc52
-static struct tree_node *tree_new(struct mnt_list *mnt)
96dc52
+static inline struct tree_node *tree_root(struct tree_ops *ops, void *ptr)
96dc52
 {
96dc52
-	struct tree_node *n;
96dc52
-
96dc52
-	n = malloc(sizeof(struct tree_node));
96dc52
-	if (!n)
96dc52
-		return NULL;
96dc52
-	memset(n, 0, sizeof(struct tree_node));
96dc52
-	n->mnt = mnt;
96dc52
-
96dc52
-	return n;
96dc52
-}
96dc52
-
96dc52
-static struct tree_node *tree_root(struct mnt_list *mnt)
96dc52
-{
96dc52
-	struct tree_node *n;
96dc52
-
96dc52
-	n = tree_new(mnt);
96dc52
-	if (!n) {
96dc52
-		error(LOGOPT_ANY, "failed to allcate tree root");
96dc52
-		return NULL;
96dc52
-	}
96dc52
-
96dc52
-	return n;
96dc52
+	return ops->new(ptr);
96dc52
 }
96dc52
 
96dc52
-static struct tree_node *tree_add_left(struct tree_node *n, struct mnt_list *mnt)
96dc52
+static struct tree_node *tree_add_left(struct tree_node *n, void *ptr)
96dc52
 {
96dc52
 	struct tree_node *new;
96dc52
 
96dc52
-	new = tree_new(mnt);
96dc52
-	if (!new) {
96dc52
-		error(LOGOPT_ANY, "failed to allcate tree node");
96dc52
-		return NULL;
96dc52
-	}
96dc52
+	new = n->ops->new(ptr);
96dc52
 	n->left = new;
96dc52
 
96dc52
-	return n;
96dc52
+	return new;
96dc52
 }
96dc52
 
96dc52
-static struct tree_node *tree_add_right(struct tree_node *n, struct mnt_list *mnt)
96dc52
+static struct tree_node *tree_add_right(struct tree_node *n, void *ptr)
96dc52
 {
96dc52
 	struct tree_node *new;
96dc52
 
96dc52
-	new = tree_new(mnt);
96dc52
-	if (!new) {
96dc52
-		error(LOGOPT_ANY, "failed to allcate tree node");
96dc52
-		return NULL;
96dc52
-	}
96dc52
+	new = n->ops->new(ptr);
96dc52
 	n->right = new;
96dc52
 
96dc52
-	return n;
96dc52
+	return new;
96dc52
 }
96dc52
 
96dc52
-static struct tree_node *tree_add_node(struct tree_node *root, struct mnt_list *mnt)
96dc52
+static struct tree_node *tree_add_node(struct tree_node *root, void *ptr)
96dc52
 {
96dc52
 	struct tree_node *p, *q;
96dc52
-	unsigned int mp_len;
96dc52
-
96dc52
-	mp_len = strlen(mnt->mp);
96dc52
+	struct tree_ops *ops = root->ops;
96dc52
+	int eq;
96dc52
 
96dc52
 	q = root;
96dc52
 	p = root;
96dc52
 
96dc52
-	while (q && strcmp(mnt->mp, p->mnt->mp)) {
96dc52
+	while (q) {
96dc52
 		p = q;
96dc52
-		if (mp_len < strlen(p->mnt->mp))
96dc52
+		eq = ops->cmp(p, ptr);
96dc52
+		if (!eq)
96dc52
+			break;
96dc52
+		if (eq < 0)
96dc52
 			q = p->left;
96dc52
 		else
96dc52
 			q = p->right;
96dc52
 	}
96dc52
 
96dc52
-	if (strcmp(mnt->mp, p->mnt->mp) == 0)
96dc52
-		error(LOGOPT_ANY, "duplicate entry in mounts list");
96dc52
+	if (!eq)
96dc52
+		error(LOGOPT_ANY, "cannot add duplicate entry to tree");
96dc52
 	else {
96dc52
-		if (mp_len < strlen(p->mnt->mp))
96dc52
-			return tree_add_left(p, mnt);
96dc52
+		if (eq < 0)
96dc52
+			return tree_add_left(p, ptr);
96dc52
 		else
96dc52
-			return tree_add_right(p, mnt);
96dc52
+			return tree_add_right(p, ptr);
96dc52
 	}
96dc52
 
96dc52
 	return NULL;
96dc52
@@ -1317,26 +1297,92 @@ static struct tree_node *tree_add_node(struct tree_node *root, struct mnt_list *
96dc52
 
96dc52
 static void tree_free(struct tree_node *root)
96dc52
 {
96dc52
+	struct tree_ops *ops = root->ops;
96dc52
+
96dc52
 	if (root->right)
96dc52
 		tree_free(root->right);
96dc52
 	if (root->left)
96dc52
 		tree_free(root->left);
96dc52
-	free(root);
96dc52
+	ops->free(root);
96dc52
 }
96dc52
 
96dc52
-static void tree_traverse(struct tree_node *n, struct list_head *mnts)
96dc52
+static int tree_traverse_inorder(struct tree_node *n, tree_work_fn_t work, void *ptr)
96dc52
 {
96dc52
-	if (n->right)
96dc52
-		tree_traverse(n->right, mnts);
96dc52
-	list_add_tail(&n->mnt->expire, mnts);
96dc52
-	if (n->left)
96dc52
-		tree_traverse(n->left, mnts);
96dc52
+	int ret;
96dc52
+
96dc52
+	if (n->left) {
96dc52
+		ret = tree_traverse_inorder(n->left, work, ptr);
96dc52
+		if (!ret)
96dc52
+			goto done;
96dc52
+	}
96dc52
+	ret = work(n, ptr);
96dc52
+	if (!ret)
96dc52
+		goto done;
96dc52
+	if (n->right) {
96dc52
+		ret = tree_traverse_inorder(n->right, work, ptr);
96dc52
+		if (!ret)
96dc52
+			goto done;
96dc52
+	}
96dc52
+done:
96dc52
+	return ret;
96dc52
+}
96dc52
+
96dc52
+static struct tree_node *tree_mnt_root(struct mnt_list *mnt)
96dc52
+{
96dc52
+	return tree_root(tree_mnt_ops, mnt);
96dc52
+}
96dc52
+
96dc52
+static struct tree_node *tree_mnt_new(void *ptr)
96dc52
+{
96dc52
+	struct tree_node *n = MNT_LIST_NODE(ptr);
96dc52
+
96dc52
+	n->ops = tree_mnt_ops;
96dc52
+	n->left = NULL;
96dc52
+	n->right = NULL;
96dc52
+
96dc52
+	return n;
96dc52
+}
96dc52
+
96dc52
+static int tree_mnt_cmp(struct tree_node *n, void *ptr)
96dc52
+{
96dc52
+	struct mnt_list *n_mnt = MNT_LIST(n);
96dc52
+	size_t n_mnt_len = n_mnt->len;
96dc52
+	struct mnt_list *mnt = ptr;
96dc52
+	size_t mnt_len = mnt->len;
96dc52
+	int eq;
96dc52
+
96dc52
+	eq = strcmp(mnt->mp, n_mnt->mp);
96dc52
+	if (!eq)
96dc52
+		return 0;
96dc52
+	return (mnt_len < n_mnt_len) ? -1 : 1;
96dc52
+}
96dc52
+
96dc52
+static void tree_mnt_free(struct tree_node *n)
96dc52
+{
96dc52
+	n->ops = NULL;
96dc52
+	n->left = NULL;
96dc52
+	n->right = NULL;
96dc52
+}
96dc52
+
96dc52
+static int tree_mnt_expire_list_work(struct tree_node *n, void *ptr)
96dc52
+{
96dc52
+	struct mnt_list *mnt = MNT_LIST(n);
96dc52
+	struct list_head *mnts = ptr;
96dc52
+
96dc52
+	/* The expire of the root offset of an offset tree is the same
96dc52
+	 * as expiring the offset tree root itself (if theree is a root
96dc52
+	 * offset).
96dc52
+	 */
96dc52
+	if (mnt->mp[mnt->len - 1] != '/')
96dc52
+		list_add(&mnt->expire, mnts);
96dc52
+
96dc52
+	return 1;
96dc52
 }
96dc52
 
96dc52
 void mnts_get_expire_list(struct list_head *mnts, struct autofs_point *ap)
96dc52
 {
96dc52
-	struct mnt_list *mnt;
96dc52
 	struct tree_node *tree = NULL;
96dc52
+	struct mnt_list *mnt;
96dc52
 
96dc52
 	mnts_hash_mutex_lock();
96dc52
 	if (list_empty(&ap->mounts))
96dc52
@@ -1351,7 +1397,7 @@ void mnts_get_expire_list(struct list_head *mnts, struct autofs_point *ap)
96dc52
 		__mnts_get_mount(mnt);
96dc52
 
96dc52
 		if (!tree) {
96dc52
-			tree = tree_root(mnt);
96dc52
+			tree = tree_mnt_root(mnt);
96dc52
 			if (!tree) {
96dc52
 				error(LOGOPT_ANY, "failed to create expire tree root");
96dc52
 				goto done;
96dc52
@@ -1367,7 +1413,7 @@ void mnts_get_expire_list(struct list_head *mnts, struct autofs_point *ap)
96dc52
 		}
96dc52
 	}
96dc52
 
96dc52
-	tree_traverse(tree, mnts);
96dc52
+	tree_traverse_inorder(tree, tree_mnt_expire_list_work, mnts);
96dc52
 	tree_free(tree);
96dc52
 done:
96dc52
 	mnts_hash_mutex_unlock();