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

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