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

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