Blame SOURCES/autofs-5.1.7-fix-hosts-map-offset-order.patch

96dc52
autofs-5.1.7 - fix hosts map offset order
96dc52
96dc52
From: Ian Kent <raven@themaw.net>
96dc52
96dc52
Map entry offset paths to be in shortest to longest order but exports
96dc52
from a server could come in any order. If there are a large number of
96dc52
exports this can result in a lot of overhead when adding the offset
96dc52
to the ordered list use to mount the offset during parsing since the
96dc52
path length of exports can cary a lot.
96dc52
96dc52
So leverage the tree implemention to sort the export offsets into
96dc52
shortest to longest order as we go when constructing the mapent from
96dc52
the exports list.
96dc52
96dc52
Signed-off-by: Ian Kent <raven@themaw.net>
96dc52
---
96dc52
 CHANGELOG              |    1 
96dc52
 include/automount.h    |    2 -
96dc52
 include/mounts.h       |    8 +++++
96dc52
 include/rpc_subs.h     |    3 ++
96dc52
 lib/mounts.c           |   57 +++++++++++++++++++++++++++++++++++++--
96dc52
 modules/lookup_hosts.c |   71 ++++++++++++++++++++++++++++++++++++++-----------
96dc52
 6 files changed, 124 insertions(+), 18 deletions(-)
96dc52
96dc52
--- autofs-5.1.7.orig/CHANGELOG
96dc52
+++ autofs-5.1.7/CHANGELOG
96dc52
@@ -72,6 +72,7 @@
96dc52
 - fix offset entries order.
96dc52
 - use mapent tree root for tree_mapent_add_node().
96dc52
 - eliminate redundant cache lookup in tree_mapent_add_node().
96dc52
+- fix hosts map offset order.
96dc52
 
96dc52
 25/01/2021 autofs-5.1.7
96dc52
 - make bind mounts propagation slave by default.
96dc52
--- autofs-5.1.7.orig/include/automount.h
96dc52
+++ autofs-5.1.7/include/automount.h
96dc52
@@ -31,9 +31,9 @@
96dc52
 #include "master.h"
96dc52
 #include "macros.h"
96dc52
 #include "log.h"
96dc52
+#include "mounts.h"
96dc52
 #include "rpc_subs.h"
96dc52
 #include "parse_subs.h"
96dc52
-#include "mounts.h"
96dc52
 #include "dev-ioctl-lib.h"
96dc52
 #include "parse_amd.h"
96dc52
 
96dc52
--- autofs-5.1.7.orig/include/mounts.h
96dc52
+++ autofs-5.1.7/include/mounts.h
96dc52
@@ -52,6 +52,7 @@ extern const unsigned int t_direct;
96dc52
 extern const unsigned int t_offset;
96dc52
 
96dc52
 struct mnt_list;
96dc52
+struct exportinfo;
96dc52
 struct mapent;
96dc52
 
96dc52
 struct tree_ops;
96dc52
@@ -66,6 +67,9 @@ struct tree_node {
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
+#define EXPORTINFO(n)		(container_of(n, struct exportinfo, node))
96dc52
+#define EXPORT_NODE(ptr)	((struct tree_node *) &((struct exportinfo *) ptr)->node)
96dc52
+
96dc52
 #define MAPENT(n)		(container_of(n, struct mapent, node))
96dc52
 #define MAPENT_NODE(p)		((struct tree_node *) &((struct mapent *) p)->node)
96dc52
 #define MAPENT_ROOT(p)		((struct tree_node *) ((struct mapent *) p)->mm_root)
96dc52
@@ -166,9 +170,13 @@ struct mnt_list *mnts_add_mount(struct a
96dc52
 void mnts_remove_mount(const char *mp, unsigned int flags);
96dc52
 struct mnt_list *get_mnt_list(const char *path, int include);
96dc52
 unsigned int mnts_has_mounted_mounts(struct autofs_point *ap);
96dc52
+int tree_traverse_inorder(struct tree_node *n, tree_work_fn_t work, void *ptr);
96dc52
+void tree_free(struct tree_node *root);
96dc52
 void mnts_get_expire_list(struct list_head *mnts, struct autofs_point *ap);
96dc52
 void mnts_put_expire_list(struct list_head *mnts);
96dc52
 void mnts_set_mounted_mount(struct autofs_point *ap, const char *name, unsigned int flags);
96dc52
+struct tree_node *tree_host_root(struct exportinfo *exp);
96dc52
+struct tree_node *tree_host_add_node(struct tree_node *root, struct exportinfo *exp);
96dc52
 struct tree_node *tree_mapent_root(struct mapent *me);
96dc52
 int tree_mapent_add_node(struct mapent_cache *mc, struct tree_node *root, struct mapent *me);
96dc52
 int tree_mapent_delete_offsets(struct mapent_cache *mc, const char *key);
96dc52
--- autofs-5.1.7.orig/include/rpc_subs.h
96dc52
+++ autofs-5.1.7/include/rpc_subs.h
96dc52
@@ -23,6 +23,8 @@
96dc52
 #include <linux/nfs2.h>
96dc52
 #include <linux/nfs3.h>
96dc52
 
96dc52
+#include "automount.h"
96dc52
+
96dc52
 #define NFS4_VERSION		4
96dc52
 
96dc52
 /* rpc helper subs */
96dc52
@@ -57,6 +59,7 @@ struct exportinfo {
96dc52
 	char *dir;
96dc52
 	struct hostinfo *hosts;
96dc52
 	struct exportinfo *next;
96dc52
+	struct tree_node node;
96dc52
 };
96dc52
 
96dc52
 struct conn_info {
96dc52
--- autofs-5.1.7.orig/lib/mounts.c
96dc52
+++ autofs-5.1.7/lib/mounts.c
96dc52
@@ -79,6 +79,17 @@ static struct tree_ops mnt_ops = {
96dc52
 };
96dc52
 static struct tree_ops *tree_mnt_ops = &mnt_ops;
96dc52
 
96dc52
+static struct tree_node *tree_host_new(void *ptr);
96dc52
+static int tree_host_cmp(struct tree_node *n, void *ptr);
96dc52
+static void tree_host_free(struct tree_node *n);
96dc52
+
96dc52
+static struct tree_ops host_ops = {
96dc52
+	.new = tree_host_new,
96dc52
+	.cmp = tree_host_cmp,
96dc52
+	.free = tree_host_free,
96dc52
+};
96dc52
+static struct tree_ops *tree_host_ops = &host_ops;
96dc52
+
96dc52
 static struct tree_node *tree_mapent_new(void *ptr);
96dc52
 static int tree_mapent_cmp(struct tree_node *n, void *ptr);
96dc52
 static void tree_mapent_free(struct tree_node *n);
96dc52
@@ -1341,7 +1352,7 @@ static struct tree_node *tree_add_node(s
96dc52
 	return NULL;
96dc52
 }
96dc52
 
96dc52
-static void tree_free(struct tree_node *root)
96dc52
+void tree_free(struct tree_node *root)
96dc52
 {
96dc52
 	struct tree_ops *ops = root->ops;
96dc52
 
96dc52
@@ -1352,7 +1363,7 @@ static void tree_free(struct tree_node *
96dc52
 	ops->free(root);
96dc52
 }
96dc52
 
96dc52
-static int tree_traverse_inorder(struct tree_node *n, tree_work_fn_t work, void *ptr)
96dc52
+int tree_traverse_inorder(struct tree_node *n, tree_work_fn_t work, void *ptr)
96dc52
 {
96dc52
 	int ret;
96dc52
 
96dc52
@@ -1479,6 +1490,48 @@ void mnts_put_expire_list(struct list_he
96dc52
 	mnts_hash_mutex_unlock();
96dc52
 }
96dc52
 
96dc52
+struct tree_node *tree_host_root(struct exportinfo *exp)
96dc52
+{
96dc52
+	return tree_root(tree_host_ops, exp);
96dc52
+}
96dc52
+
96dc52
+static struct tree_node *tree_host_new(void *ptr)
96dc52
+{
96dc52
+	struct tree_node *n = EXPORT_NODE(ptr);
96dc52
+
96dc52
+	n->ops = tree_host_ops;
96dc52
+	n->left = NULL;
96dc52
+	n->right = NULL;
96dc52
+
96dc52
+	return n;
96dc52
+}
96dc52
+
96dc52
+static int tree_host_cmp(struct tree_node *n, void *ptr)
96dc52
+{
96dc52
+	struct exportinfo *n_exp = EXPORTINFO(n);
96dc52
+	size_t n_exp_len = strlen(n_exp->dir);
96dc52
+	struct exportinfo *exp = ptr;
96dc52
+	size_t exp_len = strlen(exp->dir);
96dc52
+	int eq;
96dc52
+
96dc52
+	eq = strcmp(exp->dir, n_exp->dir);
96dc52
+	if (!eq)
96dc52
+		return 0;
96dc52
+	return (exp_len < n_exp_len) ? -1 : 1;
96dc52
+}
96dc52
+
96dc52
+static void tree_host_free(struct tree_node *n)
96dc52
+{
96dc52
+	n->ops = NULL;
96dc52
+	n->left = NULL;
96dc52
+	n->right = NULL;
96dc52
+}
96dc52
+
96dc52
+struct tree_node *tree_host_add_node(struct tree_node *root, struct exportinfo *exp)
96dc52
+{
96dc52
+	return tree_add_node(root, exp);
96dc52
+}
96dc52
+
96dc52
 struct tree_node *tree_mapent_root(struct mapent *me)
96dc52
 {
96dc52
 	return tree_root(tree_mapent_ops, me);
96dc52
--- autofs-5.1.7.orig/modules/lookup_hosts.c
96dc52
+++ autofs-5.1.7/modules/lookup_hosts.c
96dc52
@@ -84,14 +84,38 @@ int lookup_read_master(struct master *ma
96dc52
 	return NSS_STATUS_UNKNOWN;
96dc52
 }
96dc52
 
96dc52
+struct work_info {
96dc52
+	char *mapent;
96dc52
+	const char *host;
96dc52
+	int pos;
96dc52
+};
96dc52
+
96dc52
+static int tree_host_work(struct tree_node *n, void *ptr)
96dc52
+{
96dc52
+	struct exportinfo *exp = EXPORTINFO(n);
96dc52
+	struct work_info *wi = ptr;
96dc52
+	int len;
96dc52
+
96dc52
+	if (!wi->pos)
96dc52
+		len = sprintf(wi->mapent, "\"%s\" \"%s:%s\"",
96dc52
+				exp->dir, wi->host, exp->dir);
96dc52
+	else
96dc52
+		len = sprintf(wi->mapent + wi->pos, " \"%s\" \"%s:%s\"",
96dc52
+				exp->dir, wi->host, exp->dir);
96dc52
+	wi->pos += len;
96dc52
+
96dc52
+	return 1;
96dc52
+}
96dc52
+
96dc52
 static char *get_exports(struct autofs_point *ap, const char *host)
96dc52
 {
96dc52
 	char buf[MAX_ERR_BUF];
96dc52
 	char *mapent;
96dc52
 	struct exportinfo *exp, *this;
96dc52
+	struct tree_node *tree = NULL;
96dc52
+	struct work_info wi;
96dc52
 	size_t hostlen = strlen(host);
96dc52
 	size_t mapent_len;
96dc52
-	int len, pos;
96dc52
 
96dc52
 	debug(ap->logopt, MODPREFIX "fetchng export list for %s", host);
96dc52
 
96dc52
@@ -100,7 +124,28 @@ static char *get_exports(struct autofs_p
96dc52
 	this = exp;
96dc52
 	mapent_len = 0;
96dc52
 	while (this) {
96dc52
+		struct tree_node *n;
96dc52
+
96dc52
 		mapent_len += hostlen + 2*(strlen(this->dir) + 2) + 3;
96dc52
+
96dc52
+		if (!tree) {
96dc52
+			tree = tree_host_root(this);
96dc52
+			if (!tree) {
96dc52
+				error(ap->logopt, "failed to create exports tree root");
96dc52
+				rpc_exports_free(exp);
96dc52
+				return NULL;
96dc52
+			}
96dc52
+			goto next;
96dc52
+		}
96dc52
+
96dc52
+		n = tree_host_add_node(tree, this);
96dc52
+		if (!n) {
96dc52
+			error(ap->logopt, "failed to add exports tree node");
96dc52
+			tree_free(tree);
96dc52
+			rpc_exports_free(exp);
96dc52
+			return NULL;
96dc52
+		}
96dc52
+next:
96dc52
 		this = this->next;
96dc52
 	}
96dc52
 
96dc52
@@ -115,20 +160,16 @@ static char *get_exports(struct autofs_p
96dc52
 	}
96dc52
 	*mapent = 0;
96dc52
 
96dc52
-	pos = 0;
96dc52
-	this = exp;
96dc52
-	if (this) {
96dc52
-		len = sprintf(mapent, "\"%s\" \"%s:%s\"",
96dc52
-				this->dir, host, this->dir);
96dc52
-		pos += len;
96dc52
-		this = this->next;
96dc52
-	}
96dc52
-
96dc52
-	while (this) {
96dc52
-		len = sprintf(mapent + pos, " \"%s\" \"%s:%s\"",
96dc52
-				this->dir, host, this->dir);
96dc52
-		pos += len;
96dc52
-		this = this->next;
96dc52
+	wi.mapent = mapent;
96dc52
+	wi.host = host;
96dc52
+	wi.pos = 0;
96dc52
+
96dc52
+	if (!tree) {
96dc52
+		free(mapent);
96dc52
+		mapent = NULL;
96dc52
+	} else {
96dc52
+		tree_traverse_inorder(tree, tree_host_work, &wi;;
96dc52
+		tree_free(tree);
96dc52
 	}
96dc52
 	rpc_exports_free(exp);
96dc52