autofs-5.1.9 - refactor mount_nfs.c:mount_mount()

From: Ian Kent <raven@themaw.net>

Factor out the code that attempts to mount the list of mounts.

Signed-off-by: Ian Kent <raven@themaw.net>
---
 CHANGELOG           |    1 
 modules/mount_nfs.c |  439 ++++++++++++++++++++++++++++-----------------------
 2 files changed, 241 insertions(+), 199 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index f167f4a3e..c19d7c85f 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -104,6 +104,7 @@
 - make negative timeout check consistent.
 - add ability to disable negative cache.
 - add cifs mount module.
+- refactor mount_nfs.c:mount_mount().
 
 02/11/2023 autofs-5.1.9
 - fix kernel mount status notification.
diff --git a/modules/mount_nfs.c b/modules/mount_nfs.c
index 0d13b3679..0606c4c27 100644
--- a/modules/mount_nfs.c
+++ b/modules/mount_nfs.c
@@ -59,28 +59,186 @@ int mount_reinit(void **context)
 	return 0;
 }
 
+struct try_hosts_params {
+	struct host *hosts;
+	char fullpath[PATH_MAX];
+	const char *root;
+	const char *name;
+	int name_len;
+	const char *fstype;
+	char *nfsoptions;
+	unsigned int vers;
+	int port;
+	int symlink;
+	int nobind;
+	int rdma;
+	int ro;
+};
+
+int try_hosts(struct autofs_point *ap, struct try_hosts_params *params)
+{
+	void (*mountlog)(unsigned int, const char*, ...) = &log_debug;
+	struct host *this;
+	char buf[MAX_ERR_BUF];
+	int err;
+
+	if (defaults_get_mount_verbose())
+		mountlog = &log_info;
+
+	this = params->hosts;
+	while (this) {
+		char *loc;
+
+		/* Port option specified, don't try to bind */
+		if (!params->nobind && this->proximity == PROXIMITY_LOCAL) {
+			/* Local host -- do a "bind" */
+			const char *bind_options;
+
+			debug(ap->logopt,
+			      MODPREFIX "%s is local, attempt bind mount",
+			      params->name);
+
+			bind_options = params->symlink ? "symlink" : params->ro ? "ro" : "";
+
+			err = mount_bind->mount_mount(ap, params->root,
+					       params->name, params->name_len,
+					       this->path, "bind", bind_options,
+					       mount_bind->context);
+
+			/* Success - we're done */
+			if (!err)
+				return 0;
+
+			/* Failed to update mtab, don't try any more */
+			if (err == MNT_FORCE_FAIL)
+				goto forced_fail;
+
+			/* No hostname, can't be NFS */
+			if (!this->name) {
+				this = this->next;
+				continue;
+			}
+		}
+
+		/* Not a local host - do an NFS mount */
+
+		if (this->rr && this->addr &&
+		    !defaults_use_hostname_for_mounts()) {
+			socklen_t len = INET6_ADDRSTRLEN;
+			char n_buf[len + 1];
+			const char *n_addr;
+
+			n_addr = get_addr_string(this->addr, n_buf, len);
+			if (!n_addr) {
+				char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
+				error(ap->logopt, "get_addr_string: %s", estr);
+				goto forced_fail;
+			}
+			loc = malloc(strlen(n_addr) + strlen(this->path) + 4);
+			if (!loc) {
+				char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
+				error(ap->logopt, "malloc: %s", estr);
+				goto forced_fail;
+			}
+			if (this->addr->sa_family == AF_INET6) {
+				strcpy(loc, "[");
+				strcat(loc, n_addr);
+				strcat(loc, "]");
+			} else
+				strcpy(loc, n_addr);
+		} else {
+			char *host = this->name ? this->name : "localhost";
+			loc = malloc(strlen(host) + strlen(this->path) + 2);
+			if (!loc) {
+				char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
+				error(ap->logopt, "malloc: %s", estr);
+				goto forced_fail;
+			}
+			strcpy(loc, host);
+		}
+		strcat(loc, ":");
+		strcat(loc, this->path);
+
+		/* If this is a fallback from a bind mount failure
+		 * check if the local NFS server is available to try
+		 * and prevent lengthy mount failure waits.
+		 */
+		if (this->proximity == PROXIMITY_LOCAL) {
+			char *host = this->name ? this->name : "localhost";
+			int ret = 1;
+
+			/* If we're using RDMA, rpc_ping will fail when
+			 * nfs-server is local. Therefore, don't probe
+			 * when we're using RDMA.
+			 */
+			if(!params->rdma)
+				ret = rpc_ping(host, params->port,
+					       params->vers, 2, 0, RPC_CLOSE_DEFAULT);
+			if (ret <= 0)
+				goto next;
+		}
+
+		if (params->nfsoptions && *params->nfsoptions) {
+			mountlog(ap->logopt,
+				 MODPREFIX "calling mount -t %s " SLOPPY
+				 "-o %s %s %s", params->fstype, params->nfsoptions, loc,
+				 params->fullpath);
+
+			err = spawn_mount(ap->logopt,
+					  "-t", params->fstype, SLOPPYOPT "-o",
+					  params->nfsoptions, loc, params->fullpath, NULL);
+		} else {
+			mountlog(ap->logopt,
+				 MODPREFIX "calling mount -t %s %s %s",
+				 params->fstype, loc, params->fullpath);
+			err = spawn_mount(ap->logopt, "-t",
+				          params->fstype, loc, params->fullpath, NULL);
+		}
+
+		if (!err) {
+			mountlog(ap->logopt,
+			         MODPREFIX "mounted %s type %s on %s",
+				 loc, params->fstype, params->fullpath);
+			free(loc);
+			return 0;
+		}
+next:
+		free(loc);
+		this = this->next;
+	}
+
+	return 1;
+
+forced_fail:
+	return MNT_FORCE_FAIL;
+}
+
 int mount_mount(struct autofs_point *ap, const char *root, const char *name, int name_len,
 		const char *what, const char *fstype, const char *options,
 		void *context)
 {
-	char fullpath[PATH_MAX];
+	struct try_hosts_params params;
 	char buf[MAX_ERR_BUF];
-	struct host *this, *hosts = NULL;
-	unsigned int mount_default_proto, vers;
-	char *nfsoptions = NULL;
+	unsigned int mount_default_proto;
 	unsigned int flags = ap->flags &
 			(MOUNT_FLAG_RANDOM_SELECT | MOUNT_FLAG_USE_WEIGHT_ONLY);
-	int symlink = (*name != '/' && (ap->flags & MOUNT_FLAG_SYMLINK));
-	int nobind = ap->flags & MOUNT_FLAG_NOBIND;
 	int len, status, err, existed = 1;
-	int port = -1;
-	int ro = 0;            /* Set if mount bind should be read-only */
-	int rdma = 0;
 	void (*mountlog)(unsigned int, const char*, ...) = &log_debug;
 
 	if (ap->flags & MOUNT_FLAG_REMOUNT)
 		return 0;
 
+	memset(&params, 0, sizeof(struct try_hosts_params));
+
+	params.root = root;
+	params.name = name;
+	params.name_len = name_len;
+	params.fstype = fstype;
+
+	params.symlink = (*name != '/' && (ap->flags & MOUNT_FLAG_SYMLINK));
+	params.nobind = ap->flags & MOUNT_FLAG_NOBIND;
+	params.port = -1;
+
 	if (defaults_get_mount_verbose())
 		mountlog = &log_info;
 
@@ -89,11 +247,11 @@ int mount_mount(struct autofs_point *ap, const char *root, const char *name, int
 		 root, name, what, fstype, options);
 
 	mount_default_proto = defaults_get_mount_nfs_default_proto();
-	vers = NFS_VERS_DEFAULT | NFS_PROTO_DEFAULT;
+	params.vers = NFS_VERS_DEFAULT | NFS_PROTO_DEFAULT;
 	if (strcmp(fstype, "nfs4") == 0)
-		vers = NFS4_VERS_DEFAULT | TCP_SUPPORTED | NFS4_ONLY_REQUESTED;
+		params.vers = NFS4_VERS_DEFAULT | TCP_SUPPORTED | NFS4_ONLY_REQUESTED;
 	else if (mount_default_proto == 4)
-		vers = vers | NFS4_VERS_DEFAULT;
+		params.vers = params.vers | NFS4_VERS_DEFAULT;
 
 	/* Extract "nosymlink" pseudo-option which stops local filesystems
 	 * from being symlinked.
@@ -106,11 +264,11 @@ int mount_mount(struct autofs_point *ap, const char *root, const char *name, int
 		char *nfsp;
 		int o_len = strlen(options) + 1;
 
-		nfsp = nfsoptions = malloc(o_len + 1);
-		if (!nfsoptions)
+		nfsp = params.nfsoptions = malloc(o_len + 1);
+		if (!params.nfsoptions)
 			return 1;
 
-		memset(nfsoptions, '\0', o_len + 1);
+		memset(params.nfsoptions, '\0', o_len + 1);
 
 		for (comma = options; *comma != '\0';) {
 			const char *cp;
@@ -136,19 +294,19 @@ int mount_mount(struct autofs_point *ap, const char *root, const char *name, int
 
 			if (_strncmp("proto=rdma", cp, o_len) == 0 ||
 				   _strncmp("rdma", cp, o_len) == 0)
-				rdma = 1;
+				params.rdma = 1;
 
 			if (_strncmp("nosymlink", cp, o_len) == 0) {
 				warn(ap->logopt, MODPREFIX
 				     "the \"nosymlink\" option is depricated "
 				     "and will soon be removed, "
 				     "use the \"nobind\" option instead");
-				nobind = 1;
+				params.nobind = 1;
 			} else if (_strncmp("symlink", cp, o_len) == 0) {
 				if (*name != '/')
-					symlink = 1;
+					params.symlink = 1;
 			} else if (_strncmp("nobind", cp, o_len) == 0) {
-				nobind = 1;
+				params.nobind = 1;
 			} else if (_strncmp("no-use-weight-only", cp, o_len) == 0) {
 				flags &= ~MOUNT_FLAG_USE_WEIGHT_ONLY;
 			} else if (_strncmp("use-weight-only", cp, o_len) == 0) {
@@ -157,48 +315,48 @@ int mount_mount(struct autofs_point *ap, const char *root, const char *name, int
 				/* Is any version of NFSv4 in the options */
 				if (_strncmp("vers=4", cp, 6) == 0 ||
 				    _strncmp("nfsvers=4", cp, 9) == 0) {
-					vers &= ~(NFS_VERS_MASK);
-					vers |= NFS4_VERS_MASK | TCP_SUPPORTED | NFS4_ONLY_REQUESTED;
+					params.vers &= ~(NFS_VERS_MASK);
+					params.vers |= NFS4_VERS_MASK | TCP_SUPPORTED | NFS4_ONLY_REQUESTED;
 				} else if (_strncmp("vers=3", cp, o_len) == 0 ||
 					 _strncmp("nfsvers=3", cp, o_len) == 0) {
-					vers &= ~(NFS4_VERS_MASK | NFS_VERS_MASK | NFS4_ONLY_REQUESTED);
-					vers |= NFS3_REQUESTED;
+					params.vers &= ~(NFS4_VERS_MASK | NFS_VERS_MASK | NFS4_ONLY_REQUESTED);
+					params.vers |= NFS3_REQUESTED;
 				} else if (_strncmp("vers=2", cp, o_len) == 0 ||
 					 _strncmp("nfsvers=2", cp, o_len) == 0) {
-					vers &= ~(NFS4_VERS_MASK | NFS_VERS_MASK | NFS4_ONLY_REQUESTED);
-					vers |= NFS2_REQUESTED;
+					params.vers &= ~(NFS4_VERS_MASK | NFS_VERS_MASK | NFS4_ONLY_REQUESTED);
+					params.vers |= NFS2_REQUESTED;
 				} else if (strstr(cp, "port=") == cp &&
 					 o_len - 5 < 25) {
 					char optport[25];
 
 					strncpy(optport, cp + 5, o_len - 5);
 					optport[o_len - 5] = '\0';
-					port = atoi(optport);
-					if (port < 0)
-						port = 0;
+					params.port = atoi(optport);
+					if (params.port < 0)
+						warn(ap->logopt, MODPREFIX "port is negative");
 				} else if (_strncmp("proto=udp", cp, o_len) == 0 ||
 					   _strncmp("udp", cp, o_len) == 0) {
-					vers &= ~TCP_REQUESTED;
-					vers |= UDP_REQUESTED;
+					params.vers &= ~TCP_REQUESTED;
+					params.vers |= UDP_REQUESTED;
 				} else if (_strncmp("proto=udp6", cp, o_len) == 0 ||
 					   _strncmp("udp6", cp, o_len) == 0) {
-					vers &= ~(TCP_REQUESTED|TCP6_REQUESTED);
-					vers |= (UDP_REQUESTED|UDP6_REQUESTED);
+					params.vers &= ~(TCP_REQUESTED|TCP6_REQUESTED);
+					params.vers |= (UDP_REQUESTED|UDP6_REQUESTED);
 				} else if (_strncmp("proto=tcp", cp, o_len) == 0 ||
 					   _strncmp("tcp", cp, o_len) == 0) {
-					vers &= ~UDP_REQUESTED;
-					vers |= TCP_REQUESTED;
+					params.vers &= ~UDP_REQUESTED;
+					params.vers |= TCP_REQUESTED;
 				} else if (_strncmp("proto=tcp6", cp, o_len) == 0 ||
 					   _strncmp("tcp6", cp, o_len) == 0) {
-					vers &= ~(UDP_REQUESTED|UDP6_REQUESTED);
-					vers |= TCP_REQUESTED|TCP6_REQUESTED;
+					params.vers &= ~(UDP_REQUESTED|UDP6_REQUESTED);
+					params.vers |= TCP_REQUESTED|TCP6_REQUESTED;
 				}
 				/* Check for options that also make sense
 				   with bind mounts */
 				else if (_strncmp("ro", cp, o_len) == 0)
-					ro = 1;
+					params.ro = 1;
 				else if (_strncmp("rw", cp, o_len) == 0)
-					ro = 0;
+					params.ro = 0;
 				/* and jump over trailing white space */
 				memcpy(nfsp, cp, comma - cp + 1);
 				nfsp += comma - cp + 1;
@@ -206,32 +364,35 @@ int mount_mount(struct autofs_point *ap, const char *root, const char *name, int
 		}
 
 		/* In case both tcp and udp options were given */
-		if ((vers & NFS_PROTO_MASK) == 0)
-			vers |= NFS_PROTO_MASK;
+		if ((params.vers & NFS_PROTO_MASK) == 0)
+			params.vers |= NFS_PROTO_MASK;
 
 		mountlog(ap->logopt, MODPREFIX
-			 "nfs options=\"%s\", nobind=%d, ro=%d", nfsoptions, nobind, ro);
+			 "nfs options=\"%s\", nobind=%d, ro=%d",
+			 params.nfsoptions, params.nobind, params.ro);
 	}
 
 	/* Construct mount point directory */
-	len = mount_fullpath(fullpath, PATH_MAX, root, 0, name);
+	len = mount_fullpath(params.fullpath, PATH_MAX, root, 0, name);
 	if (!len) {
 		error(ap->logopt,
 		      MODPREFIX "mount point path too long");
+		if (params.nfsoptions)
+			free(params.nfsoptions);
 		return 1;
 	}
 
-	if (!parse_location(ap->logopt, &hosts, what, flags)) {
+	if (!parse_location(ap->logopt, &params.hosts, what, flags)) {
 		info(ap->logopt, MODPREFIX "no hosts available");
-		if (nfsoptions)
-			free(nfsoptions);
+		if (params.nfsoptions)
+			free(params.nfsoptions);
 		return 1;
 	}
 	/*
 	 * We can't probe protocol rdma so leave it to mount.nfs(8)
 	 * and and suffer the delay if a server isn't available.
 	 */
-	if (rdma)
+	if (params.rdma)
 		goto dont_probe;
 
 	/*
@@ -245,20 +406,20 @@ int mount_mount(struct autofs_point *ap, const char *root, const char *name, int
 	 * NFSv4 over UDP so if it has been requested in the mount options
 	 * we can't use this at all.
 	 */
-	if ((hosts && !hosts->next) &&
+	if ((params.hosts && !params.hosts->next) &&
 	    mount_default_proto == 4 &&
-	    (vers & NFS_VERS_MASK) != 0 &&
-	    (vers & NFS4_VERS_MASK) != 0 &&
-	    !(vers & (UDP_REQUESTED|UDP6_REQUESTED))) {
+	    (params.vers & NFS_VERS_MASK) != 0 &&
+	    (params.vers & NFS4_VERS_MASK) != 0 &&
+	    !(params.vers & (UDP_REQUESTED|UDP6_REQUESTED))) {
 		unsigned int v4_probe_ok = 0;
-		struct host *tmp = new_host(hosts->name, 0,
-					    hosts->addr, hosts->addr_len,
-					    hosts->proximity,
-					    hosts->weight, hosts->options);
+		struct host *tmp = new_host(params.hosts->name, 0,
+					    params.hosts->addr, params.hosts->addr_len,
+					    params.hosts->proximity,
+					    params.hosts->weight, params.hosts->options);
 		if (tmp) {
-			tmp->rr = hosts->rr;
+			tmp->rr = params.hosts->rr;
 			prune_host_list(ap->logopt, &tmp,
-					NFS4_VERS_MASK|TCP_SUPPORTED, port);
+					NFS4_VERS_MASK|TCP_SUPPORTED, params.port);
 			/* If probe succeeds just try the mount with host in hosts */
 			if (tmp) {
 				v4_probe_ok = 1;
@@ -266,176 +427,56 @@ int mount_mount(struct autofs_point *ap, const char *root, const char *name, int
 			}
 		}
 		if (!v4_probe_ok)
-			prune_host_list(ap->logopt, &hosts, vers, port);
+			prune_host_list(ap->logopt, &params.hosts, params.vers, params.port);
 	} else {
-		prune_host_list(ap->logopt, &hosts, vers, port);
+		prune_host_list(ap->logopt, &params.hosts, params.vers, params.port);
 	}
 
 dont_probe:
-	if (!hosts) {
+	if (!params.hosts) {
 		info(ap->logopt, MODPREFIX "no hosts available");
-		if (nfsoptions)
-			free(nfsoptions);
+		if (params.nfsoptions)
+			free(params.nfsoptions);
 		return 1;
 	}
 
-	debug(ap->logopt, MODPREFIX "calling mkdir_path %s", fullpath);
+	debug(ap->logopt, MODPREFIX "calling mkdir_path %s", params.fullpath);
 
-	status = mkdir_path(fullpath, mp_mode);
+	status = mkdir_path(params.fullpath, mp_mode);
 	if (status && errno != EEXIST) {
 		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
 		error(ap->logopt,
-		      MODPREFIX "mkdir_path %s failed: %s", fullpath, estr);
-		if (nfsoptions)
-			free(nfsoptions);
-		free_host_list(&hosts);
+		      MODPREFIX "mkdir_path %s failed: %s", params.fullpath, estr);
+		if (params.nfsoptions)
+			free(params.nfsoptions);
+		free_host_list(&params.hosts);
 		return 1;
 	}
 
 	if (!status)
 		existed = 0;
 
-	this = hosts;
-	while (this) {
-		char *loc;
-
-		/* Port option specified, don't try to bind */
-		if (!nobind && this->proximity == PROXIMITY_LOCAL) {
-			/* Local host -- do a "bind" */
-			const char *bind_options;
-
-			debug(ap->logopt,
-			      MODPREFIX "%s is local, attempt bind mount",
-			      name);
-
-			bind_options = symlink ? "symlink" : ro ? "ro" : "";
-
-			err = mount_bind->mount_mount(ap, root, name, name_len,
-					       this->path, "bind", bind_options,
-					       mount_bind->context);
-
-			/* Success - we're done */
-			if (!err) {
-				if (nfsoptions)
-					free(nfsoptions);
-				free_host_list(&hosts);
-				return 0;
-			}
-
-			/* Failed to update mtab, don't try any more */
-			if (err == MNT_FORCE_FAIL)
-				goto forced_fail;
-
-			/* No hostname, can't be NFS */
-			if (!this->name) {
-				this = this->next;
-				continue;
-			}
-		}
-
-		/* Not a local host - do an NFS mount */
-
-		if (this->rr && this->addr &&
-		    !defaults_use_hostname_for_mounts()) {
-			socklen_t len = INET6_ADDRSTRLEN;
-			char n_buf[len + 1];
-			const char *n_addr;
-
-			n_addr = get_addr_string(this->addr, n_buf, len);
-			if (!n_addr) {
-				char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
-				error(ap->logopt, "get_addr_string: %s", estr);
-				goto forced_fail;
-			}
-			loc = malloc(strlen(n_addr) + strlen(this->path) + 4);
-			if (!loc) {
-				char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
-				error(ap->logopt, "malloc: %s", estr);
-				goto forced_fail;
-			}
-			if (this->addr->sa_family == AF_INET6) {
-				strcpy(loc, "[");
-				strcat(loc, n_addr);
-				strcat(loc, "]");
-			} else
-				strcpy(loc, n_addr);
-		} else {
-			char *host = this->name ? this->name : "localhost";
-			loc = malloc(strlen(host) + strlen(this->path) + 2);
-			if (!loc) {
-				char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
-				error(ap->logopt, "malloc: %s", estr);
-				goto forced_fail;
-			}
-			strcpy(loc, host);
-		}
-		strcat(loc, ":");
-		strcat(loc, this->path);
-
-		/* If this is a fallback from a bind mount failure
-		 * check if the local NFS server is available to try
-		 * and prevent lengthy mount failure waits.
-		 */
-		if (this->proximity == PROXIMITY_LOCAL) {
-			char *host = this->name ? this->name : "localhost";
-			int ret = 1;
-
-			/* If we're using RDMA, rpc_ping will fail when
-			 * nfs-server is local. Therefore, don't probe
-			 * when we're using RDMA.
-			 */
-			if(!rdma)
-				ret = rpc_ping(host, port, vers, 2, 0, RPC_CLOSE_DEFAULT);
-			if (ret <= 0)
-				goto next;
-		}
-
-		if (nfsoptions && *nfsoptions) {
-			mountlog(ap->logopt,
-				 MODPREFIX "calling mount -t %s " SLOPPY
-				 "-o %s %s %s", fstype, nfsoptions, loc,
-				 fullpath);
-
-			err = spawn_mount(ap->logopt,
-					  "-t", fstype, SLOPPYOPT "-o",
-					  nfsoptions, loc, fullpath, NULL);
-		} else {
-			mountlog(ap->logopt,
-				 MODPREFIX "calling mount -t %s %s %s",
-				 fstype, loc, fullpath);
-			err = spawn_mount(ap->logopt,
-					  "-t", fstype, loc, fullpath, NULL);
-		}
-
-		if (!err) {
-			mountlog(ap->logopt,
-			         MODPREFIX "mounted %s type %s on %s",
-				 loc, fstype, fullpath);
-			free(loc);
-			if (nfsoptions)
-				free(nfsoptions);
-			free_host_list(&hosts);
-			return 0;
-		}
-next:
-		free(loc);
-		this = this->next;
+	err = try_hosts(ap, &params);
+	if(!err) {
+		if (params.nfsoptions)
+			free(params.nfsoptions);
+		free_host_list(&params.hosts);
+		return 0;
 	}
 
-forced_fail:
-	if (nfsoptions)
-		free(nfsoptions);
-	free_host_list(&hosts);
-
 	/* If we get here we've failed to complete the mount */
 
-	info(ap->logopt, MODPREFIX "nfs: mount failure %s on %s", what, fullpath);
+	if (params.nfsoptions)
+		free(params.nfsoptions);
+	free_host_list(&params.hosts);
+
+	info(ap->logopt, MODPREFIX "nfs: mount failure %s on %s", what, params.fullpath);
 
 	if (ap->type != LKP_INDIRECT)
 		return 1;
 
 	if ((!(ap->flags & MOUNT_FLAG_GHOST) && name_len) || !existed)
-		rmdir_path(ap, fullpath, ap->dev);
+		rmdir_path(ap, params.fullpath, ap->dev);
 
 	return 1;
 }
