Skip to contentSkip to conversation

Miscellaneous fixes

PATCH In review Scott Mayhew 2 participants 8 messages Dec 1

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Miscellaneous fixes

The first patch removes 4 files that should not be tracked in git and updates .gitignore so it doesn't happen again.

The remaining patches fix up a problem where the v1 version of Jeff's "mountd: fetch root filehandles over netlink" patch somehow got squashed into my "sm-notify: fix multihomed address rotation and first-attempt address choice" patch when it was committed. Note this isn't a straight revert - since Jeff's commit 3282da1d ("nfsd-iomode-advisor: recommend read_io= and write_io= for an export") subsequently modified the netlink header check in configure.ac, the configure.ac changes were omitted from both the revert and the corrected patches.

Jeff Layton (1):
mountd: fetch root filehandles over netlink

Scott Mayhew (5):
Update .gitignore
Revert "sm-notify: use a separate socket per address family" Revert "sm-notify: fix multihomed address rotation and first-attempt
address choice"
sm-notify: fix multihomed address rotation and first-attempt address
choice
sm-notify: use a separate socket per address family

 .gitignore                           |   5 ++++-
 support/export/cache.c               |   5 ++++-
 systemd/rpc_pipefs.target            |   3 ---
 systemd/var-lib-nfs-rpc_pipefs.mount |  10 ----------
 tools/nfs-fh-verify/nfs-fh-verify    | Bin 145328 -> 0 bytes
 tools/nfsrahead/99-nfs.rules         |   1 -
 6 files changed, 8 insertions(+), 16 deletions(-)
 delete mode 100644 systemd/rpc_pipefs.target
 delete mode 100644 systemd/var-lib-nfs-rpc_pipefs.mount
 delete mode 100755 tools/nfs-fh-verify/nfs-fh-verify
 delete mode 100644 tools/nfsrahead/99-nfs.rules
Signature · 1 line
2.56.1
#1 lore raw

PATCH 1/6 Update .gitignore

rpc_pipefs.target, var-lib-nfs-rpc_pipefs.mount, and 99-nfs.rules are generated from .in sources, and nfs-fh-verify is a compiled binary. None of these should be tracked in git.

---
 .gitignore                           |   5 ++++-
 systemd/rpc_pipefs.target            |   3 ---
 systemd/var-lib-nfs-rpc_pipefs.mount |  10 ----------
 tools/nfs-fh-verify/nfs-fh-verify    | Bin 145328 -> 0 bytes
 tools/nfsrahead/99-nfs.rules         |   1 -
 5 files changed, 4 insertions(+), 15 deletions(-)
 delete mode 100644 systemd/rpc_pipefs.target
 delete mode 100644 systemd/var-lib-nfs-rpc_pipefs.mount
 delete mode 100755 tools/nfs-fh-verify/nfs-fh-verify
 delete mode 100644 tools/nfsrahead/99-nfs.rules
.gitignore +4 -1
index 622134bb..2adab123 100644
@@ -62,8 +62,9 @@ utils/statd/statd
 tools/locktest/testlk
 tools/getiversion/getiversion
 tools/nfsconf/nfsconf
+tools/nfs-fh-verify/nfs-fh-verify
 tools/nfsrahead/nfsrahead
-tools/nfsrahead/99-nfs_bdi.rules
+tools/nfsrahead/99-nfs.rules
 support/export/mount.h
 support/export/mount_clnt.c
 support/export/mount_xdr.c
@@ -83,8 +84,10 @@ utils/nfsref/nfsref
 systemd/nfsroot-generator
 systemd/nfs-server-generator
 systemd/rpc-pipefs-generator
+systemd/rpc_pipefs.target
 systemd/nfs-config.service
 systemd/rpc-gssd.service
+systemd/var-lib-nfs-rpc_pipefs.mount
 # cscope database files
 cscope.*
 # generic editor backup et al
systemd/rpc_pipefs.target deleted +0 -3
deleted file mode 100644
index 01d4d278..00000000
@@ -1,3 +0,0 @@
-[Unit]
-Requires=var-lib-nfs-rpc_pipefs.mount
-After=var-lib-nfs-rpc_pipefs.mount
systemd/var-lib-nfs-rpc_pipefs.mount deleted +0 -10
deleted file mode 100644
index 26d1c763..00000000
@@ -1,10 +0,0 @@
-[Unit]
-Description=RPC Pipe File System
-DefaultDependencies=no
-After=systemd-tmpfiles-setup.service
-Conflicts=umount.target
-
-[Mount]
-What=sunrpc
-Where=/var/lib/nfs/rpc_pipefs
-Type=rpc_pipefs
tools/nfs-fh-verify/nfs-fh-verify deleted binary +0 -0
deleted file mode 100755
index 80faac7ad1defec704fadfeb6ea22513a81e704b..0000000000000000000000000000000000000000
GIT binary patch
tools/nfsrahead/99-nfs.rules deleted +0 -1
deleted file mode 100644
index c74914b2..00000000
@@ -1 +0,0 @@
-SUBSYSTEM=="bdi", ACTION=="add", PROGRAM="/usr/libexec/nfsrahead %k", ATTR{read_ahead_kb}="%c"
Signature · 1 line
2.56.1
#2 lore raw

PATCH 2/6 Revert "sm-notify: use a separate socket per address family"

In reply to Scott Mayhew

This reverts commit ba96fb357f748e8f301ca6a44f16ce5198bab284.

This revert is necessary for the revert of 62ece1e9f935bf8d96d00d3bd0d35b02a4ed82ce which follows.

---
 utils/statd/sm-notify.c | 253 ++++++++++++++--------------------------
 1 file changed, 90 insertions(+), 163 deletions(-)
utils/statd/sm-notify.c +90 -163
index 89e67c49..affaea28 100644
@@ -68,26 +68,15 @@ struct nsm_host {
 
 static char		nsm_hostname[SM_MAXSTRLEN + 1];
 static int		nsm_state;
+static int		nsm_family = AF_INET;
 static int		opt_debug = 0;
 static _Bool		opt_update_state = true;
 static unsigned int	opt_max_retry = 15 * 60;
 static char *		opt_srcaddr = NULL;
 static char *		opt_srcport = NULL;
 
-/*
- * sm-notify uses one datagram socket per address family so that it can
- * reach both IPv4 and IPv6 peers without relying on IPv4-mapped IPv6
- * addresses.  Either descriptor may be -1 when that family is
- * unavailable (eg. IPv6 is disabled, or a source address was requested
- * for one family only).
- */
-struct nsm_sockets {
-	int	v4;
-	int	v6;
-};
-
-static void		notify(const struct nsm_sockets *socks);
-static int		notify_host(const struct nsm_sockets *, struct nsm_host *);
+static void		notify(const int sock);
+static int		notify_host(int, struct nsm_host *);
 static void		recv_reply(int);
 static void		insert_host(struct nsm_host *);
 static struct nsm_host *find_host(uint32_t);
@@ -101,11 +90,7 @@ smn_lookup(const char *name)
 {
 	struct addrinfo	*ai = NULL;
 	struct addrinfo hint = {
-#ifdef IPV6_SUPPORTED
-		.ai_family	= AF_UNSPEC,
-#else	/* !IPV6_SUPPORTED */
-		.ai_family	= AF_INET,
-#endif	/* !IPV6_SUPPORTED */
+		.ai_family	= (nsm_family == AF_INET ? AF_INET: AF_UNSPEC),
 		.ai_protocol	= (int)IPPROTO_UDP,
 	};
 	int error;
@@ -291,53 +276,78 @@ smn_get_host(const char *hostname,
 	return 1;
 }
 
-/*
- * Create a non-blocking datagram socket of the given address family.
- *
- * Returns the socket descriptor, or -1 if the family is unavailable
- * or an error occurs.
- */
-static int
-smn_socket(const int family)
+#ifdef IPV6_SUPPORTED
+static int smn_socket(void)
 {
 	int sock;
 
-	sock = socket(family, SOCK_DGRAM, 0);
+	/*
+	 * Use an AF_INET socket if IPv6 is disabled on the
+	 * local system.
+	 */
+	sock = socket(AF_INET6, SOCK_DGRAM, 0);
 	if (sock == -1) {
-		/* The family being unsupported is not an error: the
-		 * caller simply won't use a socket for it. */
-		if (errno == EAFNOSUPPORT)
+		if (errno != EAFNOSUPPORT) {
+			xlog(L_ERROR, "Failed to create RPC socket: %m");
 			return -1;
-		xlog(L_ERROR, "Failed to create RPC socket: %m");
-		return -1;
-	}
+		}
+		sock = socket(AF_INET, SOCK_DGRAM, 0);
+		if (sock < 0) {
+			xlog(L_ERROR, "Failed to create RPC socket: %m");
+			return -1;
+		}
+	} else
+		nsm_family = AF_INET6;
 
 	if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
 		xlog(L_ERROR, "fcntl(3) on RPC socket failed: %m");
-		(void)close(sock);
-		return -1;
+		goto out_close;
 	}
 
-#ifdef IPV6_SUPPORTED
 	/*
-	 * Keep the IPv6 socket strictly IPv6; the AF_INET socket is used
-	 * to reach IPv4 peers.  This avoids any dependence on IPv4-mapped
-	 * IPv6 addresses, which are not usable on all configurations.
+	 * TI-RPC over IPv6 (udp6/tcp6) does not handle IPv4.  However,
+	 * since sm-notify open-codes all of its RPC support, it can
+	 * use a single socket and let the local network stack provide
+	 * the correct mapping between address families automatically.
+	 * This is the same thing that is done in the kernel.
 	 */
-	if (family == AF_INET6) {
-		const int one = 1;
+	if (nsm_family == AF_INET6) {
+		const int zero = 0;
+		socklen_t zerolen = (socklen_t)sizeof(zero);
 
 		if (setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
-				(char *)&one, (socklen_t)sizeof(one)) == -1) {
+					(char *)&zero, zerolen) == -1) {
 			xlog(L_ERROR, "setsockopt(3) on RPC socket failed: %m");
-			(void)close(sock);
-			return -1;
+			goto out_close;
 		}
 	}
-#endif	/* IPV6_SUPPORTED */
 
 	return sock;
+
+out_close:
+	(void)close(sock);
+	return -1;
 }
+#else	/* !IPV6_SUPPORTED */
+static int smn_socket(void)
+{
+	int sock;
+
+	sock = socket(AF_INET, SOCK_DGRAM, 0);
+	if (sock == -1) {
+		xlog(L_ERROR, "Failed to create RPC socket: %m");
+		return -1;
+	}
+
+	if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
+		xlog(L_ERROR, "fcntl(3) on RPC socket failed: %m");
+		(void)close(sock);
+		return -1;
+	}
+
+	return sock;
+}
+#endif	/* !IPV6_SUPPORTED */
 
 /*
  * If admin specified a source address or srcport, then convert those
@@ -345,12 +355,12 @@ smn_socket(const int family)
  */
 __attribute__((__malloc__))
 static struct addrinfo *
-smn_bind_address(const char *srcaddr, const char *srcport, const int family)
+smn_bind_address(const char *srcaddr, const char *srcport)
 {
 	struct addrinfo *ai = NULL;
 	struct addrinfo hint = {
-		.ai_flags	= AI_NUMERICSERV,
-		.ai_family	= family,
+		.ai_flags	= AI_NUMERICSERV | AI_V4MAPPED,
+		.ai_family	= nsm_family,
 		.ai_protocol	= (int)IPPROTO_UDP,
 	};
 	int error;
@@ -364,17 +374,6 @@ smn_bind_address(const char *srcaddr, const char *srcport, const int family)
 	else
 		error = getaddrinfo(srcaddr, srcport, &hint, &ai);
 	if (error != 0) {
-		/*
-		 * When a specific source address was requested, it is normal
-		 * for it to have no address in one of the families; that
-		 * family is simply not served by this invocation.
-		 */
-		if (srcaddr != NULL) {
-			xlog(D_GENERAL, "No %s source address for '%s': %s",
-				family == AF_INET ? "IPv4" : "IPv6",
-				srcaddr, gai_strerror(error));
-			return NULL;
-		}
 		xlog(L_ERROR,
 			"Invalid bind address or port for RPC socket: %s",
 				gai_strerror(error));
@@ -411,17 +410,17 @@ smn_bindresvport(int sock, struct sockaddr *sap)
  * an error occurs.
  */
 static int
-smn_create_socket(const int family, const char *srcaddr, const char *srcport)
+smn_create_socket(const char *srcaddr, const char *srcport)
 {
 	int sock, retry_cnt = 0;
 	struct addrinfo *ai;
 
 retry:
-	sock = smn_socket(family);
+	sock = smn_socket();
 	if (sock == -1)
 		return -1;
 
-	ai = smn_bind_address(srcaddr, srcport, family);
+	ai = smn_bind_address(srcaddr, srcport);
 	if (ai == NULL) {
 		(void)close(sock);
 		return -1;
@@ -504,9 +503,8 @@ read_smnotify_conf(char **argv)
 int
 main(int argc, char **argv)
 {
-	int	c;
+	int	c, sock;
 	char *	progname;
-	struct nsm_sockets socks;
 
 	progname = strrchr(argv[0], '/');
 	if (progname != NULL)
@@ -612,21 +610,14 @@ usage:		fprintf(stderr,
 		close(2);
 	}
 
-	socks.v4 = smn_create_socket(AF_INET, opt_srcaddr, opt_srcport);
-#ifdef IPV6_SUPPORTED
-	socks.v6 = smn_create_socket(AF_INET6, opt_srcaddr, opt_srcport);
-#else	/* !IPV6_SUPPORTED */
-	socks.v6 = -1;
-#endif	/* !IPV6_SUPPORTED */
-	if (socks.v4 == -1 && socks.v6 == -1) {
-		xlog(L_ERROR, "Failed to create an RPC socket");
+	sock = smn_create_socket(opt_srcaddr, opt_srcport);
+	if (sock == -1)
 		exit(1);
-	}
 
 	if (!nsm_drop_privileges(-1))
 		exit(1);
 
-	notify(&socks);
+	notify(sock);
 
 	if (hosts) {
 		struct nsm_host	*hp;
@@ -642,50 +633,11 @@ usage:		fprintf(stderr,
 	exit(0);
 }
 
-/*
- * Return the socket descriptor to use for the given address family,
- * or -1 if no socket is available for it.
- */
-static int
-smn_sock_for_family(const struct nsm_sockets *socks, const int family)
-{
-	switch (family) {
-	case AF_INET:
-		return socks->v4;
-	case AF_INET6:
-		return socks->v6;
-	}
-	return -1;
-}
-
-/*
- * Move the first address on a host's list to the end, so the next
- * attempt uses a different address.  A no-op if there is only one.
- */
-static void
-smn_rotate_address(struct nsm_host *host)
-{
-	if (host->ai->ai_next != NULL) {
-		struct addrinfo *first = host->ai;
-		struct addrinfo **next;
-
-		/* remove the first entry from the list */
-		host->ai = first->ai_next;
-		first->ai_next = NULL;
-		/* find the end of the (new) list */
-		next = &host->ai;
-		while ( *next )
-			next = & (*next)->ai_next;
-		/* put first entry at end */
-		*next = first;
-	}
-}
-
 /*
  * Notify hosts
  */
 static void
-notify(const struct nsm_sockets *socks)
+notify(const int sock)
 {
 	time_t	failtime = 0;
 
@@ -693,8 +645,7 @@ notify(const struct nsm_sockets *socks)
 		failtime = time(NULL) + opt_max_retry;
 
 	while (hosts) {
-		struct pollfd	pfd[2];
-		int		nfds, i;
+		struct pollfd	pfd;
 		time_t		now = time(NULL);
 		unsigned int	sent = 0;
 		struct nsm_host	*hp;
@@ -712,7 +663,7 @@ notify(const struct nsm_sockets *socks)
 			hp = hosts;
 			hosts = hp->next;
 
-			if (notify_host(socks, hp))
+			if (notify_host(sock, hp))
 				continue;
 
 			/* Set the timeout for this call, using an
@@ -731,27 +682,16 @@ notify(const struct nsm_sockets *socks)
 		xlog(D_GENERAL, "Host %s due in %ld seconds",
 				hosts->name, wait);
 
-		nfds = 0;
-		if (socks->v4 != -1) {
-			pfd[nfds].fd = socks->v4;
-			pfd[nfds].events = POLLIN;
-			nfds++;
-		}
-		if (socks->v6 != -1) {
-			pfd[nfds].fd = socks->v6;
-			pfd[nfds].events = POLLIN;
-			nfds++;
-		}
+		pfd.fd = sock;
+		pfd.events = POLLIN;
 
 		wait *= 1000;
 		if (wait < 100)
 			wait = 100;
-		if (poll(pfd, (nfds_t)nfds, wait) <= 0)
+		if (poll(&pfd, 1, wait) != 1)
 			continue;
 
-		for (i = 0; i < nfds; i++)
-			if (pfd[i].revents & POLLIN)
-				recv_reply(pfd[i].fd);
+		recv_reply(sock);
 	}
 }
 
@@ -759,13 +699,10 @@ notify(const struct nsm_sockets *socks)
  * Send notification to a single host
  */
 static int
-notify_host(const struct nsm_sockets *socks, struct nsm_host *host)
+notify_host(int sock, struct nsm_host *host)
 {
 	struct sockaddr *sap;
 	socklen_t salen;
-	unsigned int naddr, tried;
-	struct addrinfo *a;
-	int sock;
 
 	if (host->ai == NULL) {
 		host->ai = smn_lookup(host->name);
@@ -782,34 +719,24 @@ notify_host(const struct nsm_sockets *socks, struct nsm_host *host)
 	 * point.
 	 */
 	if (host->retries >= 4) {
-		smn_rotate_address(host);
-		nfs_set_port(host->ai->ai_addr, 0);
-		host->retries = 0;
-	}
-
-	/*
-	 * Select the socket matching the address family of the current
-	 * address.  If we have no socket for that family (eg. a source
-	 * address was requested for the other family only), rotate through
-	 * the remaining addresses looking for one we can send from.  If
-	 * none is usable, give up on this host, leaving its on-disk record
-	 * so a later run can retry.
-	 */
-	naddr = 0;
-	for (a = host->ai; a != NULL; a = a->ai_next)
-		naddr++;
-
-	for (tried = 0; ; tried++) {
-		sock = smn_sock_for_family(socks, host->ai->ai_addr->sa_family);
-		if (sock != -1)
-			break;
-		if (tried + 1 >= naddr) {
-			xlog_warn("No usable source address to notify %s; "
-				"giving up", host->name);
-			return 1;
+		/* don't rotate if there is only one addrinfo */
+		if (host->ai->ai_next != NULL) {
+			struct addrinfo *first = host->ai;
+			struct addrinfo **next;
+
+			/* remove the first entry from the list */
+			host->ai = first->ai_next;
+			first->ai_next = NULL;
+			/* find the end of the (new) list */
+			next = &host->ai;
+			while ( *next )
+				next = & (*next)->ai_next;
+			/* put first entry at end */
+			*next = first;
 		}
-		smn_rotate_address(host);
+
 		nfs_set_port(host->ai->ai_addr, 0);
+		host->retries = 0;
 	}
 
 	sap = host->ai->ai_addr;
Signature · 1 line
2.56.1
#3 lore raw

PATCH 3/6 Revert "sm-notify: fix multihomed address rotation and first-attempt address choice"

In reply to Scott Mayhew

This reverts commit 62ece1e9f935bf8d96d00d3bd0d35b02a4ed82ce.

Commit 62ece1e9f935bf8d96d00d3bd0d35b02a4ed82ce included unrelated changes from the v1 posting of "mountd: fetch root filehandles over netlink" posted by Jeff Layton on 10/20/26. Revert it so the patches can be committed properly.

Note that the value used to check for the latest netlink features has moved on since this commit was committed, so the configure.ac change is not included in the revert.

---
 support/export/cache.c          | 168 +++-----------------------------
 support/include/lockd_netlink.h |   1 -
 support/include/nfsd_netlink.h  |  15 +--
 utils/statd/sm-notify.c         |   8 +-
 4 files changed, 21 insertions(+), 171 deletions(-)
support/export/cache.c +16 -152
index cc327452..65a59b2a 100644
@@ -4292,135 +4292,30 @@ int cache_export(nfs_export *exp, char *path)
 	return cache_export_ent(buf, sizeof(buf), domain, &exp->m_export, path);
 }
 
-struct filehandle_get_data {
-	struct nfs_fh_len	*fh;
-	int			got_reply;
-	int			kern_err;
-	int			done;
-};
-
-static int filehandle_get_valid_cb(struct nl_msg *msg, void *arg)
-{
-	struct filehandle_get_data *data = arg;
-	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
-	struct nlattr *tb[NFSD_A_FILEHANDLE_MAX + 1];
-	int len;
-
-	if (nla_parse(tb, NFSD_A_FILEHANDLE_MAX,
-		      genlmsg_attrdata(gnlh, 0),
-		      genlmsg_attrlen(gnlh, 0), NULL) < 0 ||
-	    !tb[NFSD_A_FILEHANDLE_HANDLE]) {
-		data->kern_err = -EBADMSG;
-		return NL_SKIP;
-	}
-
-	len = nla_len(tb[NFSD_A_FILEHANDLE_HANDLE]);
-	if (len <= 0 || len > NFS3_FHSIZE) {
-		data->kern_err = -EBADMSG;
-		return NL_SKIP;
-	}
-
-	memset(data->fh->fh_handle, 0, sizeof(data->fh->fh_handle));
-	memcpy(data->fh->fh_handle,
-	       nla_data(tb[NFSD_A_FILEHANDLE_HANDLE]), len);
-	data->fh->fh_size = len;
-	data->got_reply = 1;
-	return NL_SKIP;
-}
-
-static int filehandle_get_ack_cb(struct nl_msg *UNUSED(msg), void *arg)
-{
-	struct filehandle_get_data *data = arg;
-
-	data->done = 1;
-	return NL_STOP;
-}
-
-static int filehandle_get_error_cb(struct sockaddr_nl *UNUSED(nla),
-				   struct nlmsgerr *nlerr, void *arg)
-{
-	struct filehandle_get_data *data = arg;
-
-	data->kern_err = nlerr->error;
-	data->done = 1;
-	return NL_STOP;
-}
-
-/*
- * Fetch an export's root filehandle over netlink.
+/**
+ * cache_get_filehandle - given an nfs_export, get its root filehandle
+ * @exp: target nfs_export
+ * @len: length of requested file handle
+ * @p: NUL-terminated C string containing export path
  *
- * Returns 0 with @fh filled in, -EOPNOTSUPP when the running kernel
- * does not implement FILEHANDLE_GET, or another negative errno.
- */
-static int nl_cache_filehandle(char *domain, char *path, int len,
-			       struct nfs_fh_len *fh)
-{
-	struct filehandle_get_data data = { .fh = fh };
-	struct nl_msg *msg;
-	struct nl_cb *cb;
-	int ret;
-
-	msg = cache_nl_new_msg(nfsd_nl_family, NFSD_CMD_FILEHANDLE_GET, 0);
-	if (!msg)
-		return -ENOMEM;
-
-	if (nla_put_string(msg, NFSD_A_FILEHANDLE_DOMAIN, domain) < 0 ||
-	    nla_put_string(msg, NFSD_A_FILEHANDLE_PATH, path) < 0 ||
-	    nla_put_u32(msg, NFSD_A_FILEHANDLE_MAXSIZE, len) < 0) {
-		nlmsg_free(msg);
-		return -ENOMEM;
-	}
-
-	cb = nl_cb_alloc(NL_CB_DEFAULT);
-	if (!cb) {
-		nlmsg_free(msg);
-		return -ENOMEM;
-	}
-
-	nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, filehandle_get_valid_cb, &data);
-	nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, filehandle_get_ack_cb, &data);
-	nl_cb_err(cb, NL_CB_CUSTOM, filehandle_get_error_cb, &data);
-
-	ret = nl_send_auto(nfsd_nl_cmd_sock, msg);
-	nlmsg_free(msg);
-	if (ret < 0) {
-		nl_cb_put(cb);
-		return ret;
-	}
-
-	while (!data.done) {
-		ret = nl_recvmsgs(nfsd_nl_cmd_sock, cb);
-		if (ret < 0)
-			break;
-	}
-	nl_cb_put(cb);
-
-	if (data.kern_err)
-		return data.kern_err;
-	if (!data.got_reply)
-		return ret < 0 ? ret : -EIO;
-	return 0;
-}
-
-/*
- * Fetch an export's root filehandle via /proc, for kernels that predate
- * FILEHANDLE_GET.
+ * Returns pointer to NFS file handle of root directory of export
  *
- * {
- *   echo $domain $path $length
+ * { 
+ *   echo $domain $path $length 
  *   read filehandle <&0
  * } <> /proc/fs/nfsd/filehandle
  */
-static int proc_cache_filehandle(nfs_export *exp, int len, char *p,
-				 struct nfs_fh_len *fh)
+struct nfs_fh_len *
+cache_get_filehandle(nfs_export *exp, int len, char *p)
 {
+	static struct nfs_fh_len fh;
 	char buf[RPC_CHAN_BUF_SIZE], *bp;
 	int blen, f;
 
 	f = open("/proc/fs/nfsd/filehandle", O_RDWR);
 	if (f < 0) {
 		f = open("/proc/fs/nfs/filehandle", O_RDWR);
-		if (f < 0) return -1;
+		if (f < 0) return NULL;
 	}
 
 	bp = buf, blen = sizeof(buf);
@@ -4430,49 +4325,18 @@ static int proc_cache_filehandle(nfs_export *exp, int len, char *p,
 	qword_addeol(&bp, &blen);
 	if (blen <= 0 || cache_write(f, buf, bp - buf) != bp - buf) {
 		close(f);
-		return -1;
+		return NULL;
 	}
 	bp = buf;
 	blen = cache_read(f, buf, sizeof(buf));
 	close(f);
 
 	if (blen <= 0 || buf[blen-1] != '\n')
-		return -1;
+		return NULL;
 	buf[blen-1] = 0;
 
-	memset(fh->fh_handle, 0, sizeof(fh->fh_handle));
-	fh->fh_size = qword_get(&bp, (char *)fh->fh_handle, NFS3_FHSIZE);
-	return 0;
-}
-
-/**
- * cache_get_filehandle - given an nfs_export, get its root filehandle
- * @exp: target nfs_export
- * @len: length of requested file handle
- * @p: NUL-terminated C string containing export path
- *
- * Returns pointer to NFS file handle of root directory of export
- *
- * The handle is fetched over netlink whenever the command socket is up
- * and the kernel implements FILEHANDLE_GET, and via /proc otherwise.
- */
-struct nfs_fh_len *
-cache_get_filehandle(nfs_export *exp, int len, char *p)
-{
-	static struct nfs_fh_len fh;
-	int err;
-
-	if (nfsd_nl_cmd_sock) {
-		err = nl_cache_filehandle(exp->m_client->m_hostname, p,
-					  len, &fh);
-		if (!err)
-			return &fh;
-		if (err != -EOPNOTSUPP)
-			return NULL;
-	}
-
-	if (proc_cache_filehandle(exp, len, p, &fh) < 0)
-		return NULL;
+	memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
+	fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);
 	return &fh;
 }
 
support/include/lockd_netlink.h +0 -1
index 2d766a0f..21c65aec 100644
@@ -2,7 +2,6 @@
 /* Do not edit directly, auto-generated from: */
 /*	Documentation/netlink/specs/lockd.yaml */
 /* YNL-GEN uapi header */
-/* To regenerate run: tools/net/ynl/ynl-regen.sh */
 
 #ifndef _UAPI_LINUX_LOCKD_NETLINK_H
 #define _UAPI_LINUX_LOCKD_NETLINK_H
support/include/nfsd_netlink.h +1 -14
index f67efa23..942a1c6b 100644
@@ -51,9 +51,7 @@ enum nfsd_xprtsec_mode {
 };
 
 /*
- * Constraints that apply to an rpcbind registration. no-udp means the kernel
- * would not have registered this program and version over UDP, so the caller
- * must skip the udp and udp6 netids for it.
+ * Constraints that apply to an rpcbind registration.
  */
 enum nfsd_rpcbind_flags {
 	NFSD_RPCBIND_FLAGS_NO_UDP = 1,
@@ -292,16 +290,6 @@ enum {
 	NFSD_A_SERVER_STATS_MAX = (__NFSD_A_SERVER_STATS_MAX - 1)
 };
 
-enum {
-	NFSD_A_FILEHANDLE_DOMAIN = 1,
-	NFSD_A_FILEHANDLE_PATH,
-	NFSD_A_FILEHANDLE_MAXSIZE,
-	NFSD_A_FILEHANDLE_HANDLE,
-
-	__NFSD_A_FILEHANDLE_MAX,
-	NFSD_A_FILEHANDLE_MAX = (__NFSD_A_FILEHANDLE_MAX - 1)
-};
-
 enum {
 	NFSD_CMD_RPC_STATUS_GET = 1,
 	NFSD_CMD_THREADS_SET,
@@ -322,7 +310,6 @@ enum {
 	NFSD_CMD_UNLOCK_FILESYSTEM,
 	NFSD_CMD_UNLOCK_EXPORT,
 	NFSD_CMD_SERVER_STATS_GET,
-	NFSD_CMD_FILEHANDLE_GET,
 
 	__NFSD_CMD_MAX,
 	NFSD_CMD_MAX = (__NFSD_CMD_MAX - 1)
utils/statd/sm-notify.c +4 -4
index affaea28..ed82b8f2 100644
@@ -235,7 +235,7 @@ smn_alloc_host(const char *hostname, const char *mon_name,
 
 	host->last_used = timestamp;
 	host->timeout = NSM_TIMEOUT;
-	host->retries = 0;
+	host->retries = 100;		/* force address retry */
 
 	return host;
 
@@ -722,13 +722,13 @@ notify_host(int sock, struct nsm_host *host)
 		/* don't rotate if there is only one addrinfo */
 		if (host->ai->ai_next != NULL) {
 			struct addrinfo *first = host->ai;
-			struct addrinfo **next;
+			struct addrinfo **next = &host->ai;
 
 			/* remove the first entry from the list */
 			host->ai = first->ai_next;
 			first->ai_next = NULL;
-			/* find the end of the (new) list */
-			next = &host->ai;
+			/* find the end of the list */
+			next = &first->ai_next;
 			while ( *next )
 				next = & (*next)->ai_next;
 			/* put first entry at end */
Signature · 1 line
2.56.1
#4 lore raw

PATCH 4/6 mountd: fetch root filehandles over netlink

In reply to Scott Mayhew

From: Jeff Layton <[email protected]>

Try NFSD_CMD_FILEHANDLE_GET first in cache_get_filehandle(), falling back to /proc/fs/nfsd/filehandle when netlink is unavailable or the kernel predates the new command. Remember an -EOPNOTSUPP so older kernels don't pay a failed netlink round trip on every MNT. Sync the bundled nfsd_netlink.h with the new command and attributes, and require them in the configure probe.

Note the original posting of this patch updated the check for the lastest netlink features in configure.ac (see
https://lore.kernel.org/linux-nfs/[email protected]/).
But then the changes from the v1 patch got squashed into unrelated commit 62ece1e9 ("sm-notify: fix multihomed address rotation and first-attempt address choice. That commit has been reverted so that both commits can be applied properly, but since the netlink header check was further modified by a3da58d3 ("exports: Add read_io= and write_io= export options"), this version omits the change to configure.ac.

---
 support/export/cache.c          | 171 +++++++++++++++++++++++++++++---
 support/include/lockd_netlink.h |   1 +
 support/include/nfsd_netlink.h  |  15 ++-
 3 files changed, 170 insertions(+), 17 deletions(-)
support/export/cache.c +155 -16
index 65a59b2a..93b44cf0 100644
@@ -4292,30 +4292,135 @@ int cache_export(nfs_export *exp, char *path)
 	return cache_export_ent(buf, sizeof(buf), domain, &exp->m_export, path);
 }
 
-/**
- * cache_get_filehandle - given an nfs_export, get its root filehandle
- * @exp: target nfs_export
- * @len: length of requested file handle
- * @p: NUL-terminated C string containing export path
+struct filehandle_get_data {
+	struct nfs_fh_len	*fh;
+	int			got_reply;
+	int			kern_err;
+	int			done;
+};
+
+static int filehandle_get_valid_cb(struct nl_msg *msg, void *arg)
+{
+	struct filehandle_get_data *data = arg;
+	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
+	struct nlattr *tb[NFSD_A_FILEHANDLE_MAX + 1];
+	int len;
+
+	if (nla_parse(tb, NFSD_A_FILEHANDLE_MAX,
+		      genlmsg_attrdata(gnlh, 0),
+		      genlmsg_attrlen(gnlh, 0), NULL) < 0 ||
+	    !tb[NFSD_A_FILEHANDLE_HANDLE]) {
+		data->kern_err = -EBADMSG;
+		return NL_SKIP;
+	}
+
+	len = nla_len(tb[NFSD_A_FILEHANDLE_HANDLE]);
+	if (len <= 0 || len > NFS3_FHSIZE) {
+		data->kern_err = -EBADMSG;
+		return NL_SKIP;
+	}
+
+	memset(data->fh->fh_handle, 0, sizeof(data->fh->fh_handle));
+	memcpy(data->fh->fh_handle,
+	       nla_data(tb[NFSD_A_FILEHANDLE_HANDLE]), len);
+	data->fh->fh_size = len;
+	data->got_reply = 1;
+	return NL_SKIP;
+}
+
+static int filehandle_get_ack_cb(struct nl_msg *UNUSED(msg), void *arg)
+{
+	struct filehandle_get_data *data = arg;
+
+	data->done = 1;
+	return NL_STOP;
+}
+
+static int filehandle_get_error_cb(struct sockaddr_nl *UNUSED(nla),
+				   struct nlmsgerr *nlerr, void *arg)
+{
+	struct filehandle_get_data *data = arg;
+
+	data->kern_err = nlerr->error;
+	data->done = 1;
+	return NL_STOP;
+}
+
+/*
+ * Fetch an export's root filehandle over netlink.
  *
- * Returns pointer to NFS file handle of root directory of export
+ * Returns 0 with @fh filled in, -EOPNOTSUPP when the running kernel
+ * does not implement FILEHANDLE_GET, or another negative errno.
+ */
+static int nl_cache_filehandle(char *domain, char *path, int len,
+			       struct nfs_fh_len *fh)
+{
+	struct filehandle_get_data data = { .fh = fh };
+	struct nl_msg *msg;
+	struct nl_cb *cb;
+	int ret;
+
+	msg = cache_nl_new_msg(nfsd_nl_family, NFSD_CMD_FILEHANDLE_GET, 0);
+	if (!msg)
+		return -ENOMEM;
+
+	if (nla_put_string(msg, NFSD_A_FILEHANDLE_DOMAIN, domain) < 0 ||
+	    nla_put_string(msg, NFSD_A_FILEHANDLE_PATH, path) < 0 ||
+	    nla_put_u32(msg, NFSD_A_FILEHANDLE_MAXSIZE, len) < 0) {
+		nlmsg_free(msg);
+		return -ENOMEM;
+	}
+
+	cb = nl_cb_alloc(NL_CB_DEFAULT);
+	if (!cb) {
+		nlmsg_free(msg);
+		return -ENOMEM;
+	}
+
+	nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, filehandle_get_valid_cb, &data);
+	nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, filehandle_get_ack_cb, &data);
+	nl_cb_err(cb, NL_CB_CUSTOM, filehandle_get_error_cb, &data);
+
+	ret = nl_send_auto(nfsd_nl_cmd_sock, msg);
+	nlmsg_free(msg);
+	if (ret < 0) {
+		nl_cb_put(cb);
+		return ret;
+	}
+
+	while (!data.done) {
+		ret = nl_recvmsgs(nfsd_nl_cmd_sock, cb);
+		if (ret < 0)
+			break;
+	}
+	nl_cb_put(cb);
+
+	if (data.kern_err)
+		return data.kern_err;
+	if (!data.got_reply)
+		return ret < 0 ? ret : -EIO;
+	return 0;
+}
+
+/*
+ * Fetch an export's root filehandle via /proc, for kernels that predate
+ * FILEHANDLE_GET.
  *
- * { 
- *   echo $domain $path $length 
+ * {
+ *   echo $domain $path $length
  *   read filehandle <&0
  * } <> /proc/fs/nfsd/filehandle
  */
-struct nfs_fh_len *
-cache_get_filehandle(nfs_export *exp, int len, char *p)
+static int proc_cache_filehandle(nfs_export *exp, int len, char *p,
+				 struct nfs_fh_len *fh)
 {
-	static struct nfs_fh_len fh;
 	char buf[RPC_CHAN_BUF_SIZE], *bp;
 	int blen, f;
 
 	f = open("/proc/fs/nfsd/filehandle", O_RDWR);
 	if (f < 0) {
 		f = open("/proc/fs/nfs/filehandle", O_RDWR);
-		if (f < 0) return NULL;
+		if (f < 0) return -1;
 	}
 
 	bp = buf, blen = sizeof(buf);
@@ -4325,18 +4430,52 @@ cache_get_filehandle(nfs_export *exp, int len, char *p)
 	qword_addeol(&bp, &blen);
 	if (blen <= 0 || cache_write(f, buf, bp - buf) != bp - buf) {
 		close(f);
-		return NULL;
+		return -1;
 	}
 	bp = buf;
 	blen = cache_read(f, buf, sizeof(buf));
 	close(f);
 
 	if (blen <= 0 || buf[blen-1] != '\n')
-		return NULL;
+		return -1;
 	buf[blen-1] = 0;
 
-	memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
-	fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);
+	memset(fh->fh_handle, 0, sizeof(fh->fh_handle));
+	fh->fh_size = qword_get(&bp, (char *)fh->fh_handle, NFS3_FHSIZE);
+	return 0;
+}
+
+/**
+ * cache_get_filehandle - given an nfs_export, get its root filehandle
+ * @exp: target nfs_export
+ * @len: length of requested file handle
+ * @p: NUL-terminated C string containing export path
+ *
+ * Returns pointer to NFS file handle of root directory of export
+ *
+ * The handle is fetched over netlink whenever the command socket is up
+ * and the kernel implements FILEHANDLE_GET, and via /proc otherwise.
+ */
+struct nfs_fh_len *
+cache_get_filehandle(nfs_export *exp, int len, char *p)
+{
+	static struct nfs_fh_len fh;
+	/* Remembered so older kernels don't pay a netlink round trip per MNT */
+	static bool nl_unsupported;
+	int err;
+
+	if (nfsd_nl_cmd_sock && !nl_unsupported) {
+		err = nl_cache_filehandle(exp->m_client->m_hostname, p,
+					  len, &fh);
+		if (!err)
+			return &fh;
+		if (err != -EOPNOTSUPP)
+			return NULL;
+		nl_unsupported = true;
+	}
+
+	if (proc_cache_filehandle(exp, len, p, &fh) < 0)
+		return NULL;
 	return &fh;
 }
 
support/include/lockd_netlink.h +1 -0
index 21c65aec..2d766a0f 100644
@@ -2,6 +2,7 @@
 /* Do not edit directly, auto-generated from: */
 /*	Documentation/netlink/specs/lockd.yaml */
 /* YNL-GEN uapi header */
+/* To regenerate run: tools/net/ynl/ynl-regen.sh */
 
 #ifndef _UAPI_LINUX_LOCKD_NETLINK_H
 #define _UAPI_LINUX_LOCKD_NETLINK_H
support/include/nfsd_netlink.h +14 -1
index 942a1c6b..f67efa23 100644
@@ -51,7 +51,9 @@ enum nfsd_xprtsec_mode {
 };
 
 /*
- * Constraints that apply to an rpcbind registration.
+ * Constraints that apply to an rpcbind registration. no-udp means the kernel
+ * would not have registered this program and version over UDP, so the caller
+ * must skip the udp and udp6 netids for it.
  */
 enum nfsd_rpcbind_flags {
 	NFSD_RPCBIND_FLAGS_NO_UDP = 1,
@@ -290,6 +292,16 @@ enum {
 	NFSD_A_SERVER_STATS_MAX = (__NFSD_A_SERVER_STATS_MAX - 1)
 };
 
+enum {
+	NFSD_A_FILEHANDLE_DOMAIN = 1,
+	NFSD_A_FILEHANDLE_PATH,
+	NFSD_A_FILEHANDLE_MAXSIZE,
+	NFSD_A_FILEHANDLE_HANDLE,
+
+	__NFSD_A_FILEHANDLE_MAX,
+	NFSD_A_FILEHANDLE_MAX = (__NFSD_A_FILEHANDLE_MAX - 1)
+};
+
 enum {
 	NFSD_CMD_RPC_STATUS_GET = 1,
 	NFSD_CMD_THREADS_SET,
@@ -310,6 +322,7 @@ enum {
 	NFSD_CMD_UNLOCK_FILESYSTEM,
 	NFSD_CMD_UNLOCK_EXPORT,
 	NFSD_CMD_SERVER_STATS_GET,
+	NFSD_CMD_FILEHANDLE_GET,
 
 	__NFSD_CMD_MAX,
 	NFSD_CMD_MAX = (__NFSD_CMD_MAX - 1)
Signature · 1 line
2.56.1
#5 lore raw

PATCH 5/6 sm-notify: fix multihomed address rotation and first-attempt address choice

In reply to Scott Mayhew

notify_host() rotates through a host's addresses after 4 retransmits so that all addresses of a multihomed peer are tried. Two defects prevented this from working for dual-stack hosts:

1. The "find the end of the list" loop started from &first->ai_next, which had just been set to NULL, so instead of appending the removed head to the tail of the (new) list it set first->ai_next = first, orphaning that address. For a 2-address host this collapsed the list to a single entry after the first rotation, after which the ai_next != NULL guard was never true again and no further rotation could occur. Walk from &host->ai so the removed head is re-appended.

2. smn_alloc_host() initialized host->retries to 100 to "force address retry" on the first notify_host() call. That made sense when the code transmitted to 'first' (the original head) after rotating, but since commit e132bb5d ("sm-notify: Replace RPC code") the transmit uses host->ai->ai_addr (the new head). As a result the very first packet was sent to the second address returned by getaddrinfo(3), skipping the preferred first address. Combined with defect #1, a host whose name resolved to both an IPv6 and an IPv4 address had its IPv6 address rotated out and orphaned before any packet was sent, pinning sm-notify to an unreachable IPv4 address. Initialize retries to 0; a freshly resolved addrinfo already has port 0, so the first call still performs an rpcbind lookup without forcing a rotation.

Fixes e132bb5d ("sm-notify: Replace RPC code")
Assisted-by LLM
---
 utils/statd/sm-notify.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
utils/statd/sm-notify.c +4 -4
index ed82b8f2..affaea28 100644
@@ -235,7 +235,7 @@ smn_alloc_host(const char *hostname, const char *mon_name,
 
 	host->last_used = timestamp;
 	host->timeout = NSM_TIMEOUT;
-	host->retries = 100;		/* force address retry */
+	host->retries = 0;
 
 	return host;
 
@@ -722,13 +722,13 @@ notify_host(int sock, struct nsm_host *host)
 		/* don't rotate if there is only one addrinfo */
 		if (host->ai->ai_next != NULL) {
 			struct addrinfo *first = host->ai;
-			struct addrinfo **next = &host->ai;
+			struct addrinfo **next;
 
 			/* remove the first entry from the list */
 			host->ai = first->ai_next;
 			first->ai_next = NULL;
-			/* find the end of the list */
-			next = &first->ai_next;
+			/* find the end of the (new) list */
+			next = &host->ai;
 			while ( *next )
 				next = & (*next)->ai_next;
 			/* put first entry at end */
Signature · 1 line
2.56.1
#6 lore raw

PATCH 6/6 sm-notify: use a separate socket per address family

In reply to Scott Mayhew

Previously sm-notify sent all of its RPC traffic over a single AF_INET6 socket with IPV6_V6ONLY disabled, relying on the network stack to reach IPv4 peers via IPv4-mapped IPv6 addresses. That approach is fragile:

- It depends on IPv4-mapped IPv6 addresses being usable, which is not true on all configurations, and requires getaddrinfo(3) to be asked for mapped addresses (AI_V4MAPPED) or the IPv4 results are unsendable.

- A socket bound to a specific source address (via -v / outgoing-addr) takes on that address's family, so an IPv6 source address cannot send to IPv4 peers (and vice versa), failing with ENETUNREACH.

Create one datagram socket per address family instead, each strictly single-family (IPV6_V6ONLY is now enabled on the IPv6 socket), and send each host's notification on the socket matching the destination's family. Peer names are resolved with native address families (no AI_V4MAPPED), and IPv4 peers once again use PMAP_GETPORT, which is also understood by legacy portmappers.

When a specific source address is requested, each family's socket is bound to the matching address from that name; a name that resolves to both families can now notify peers of both families from a single invocation. A host that only resolves to a family for which no socket is available is skipped with a warning, leaving its on-disk monitor record intact so a later run can retry.

Assisted-by LLM
---
 utils/statd/sm-notify.c | 253 ++++++++++++++++++++++++++--------------
 1 file changed, 163 insertions(+), 90 deletions(-)
utils/statd/sm-notify.c +163 -90
index affaea28..89e67c49 100644
@@ -68,15 +68,26 @@ struct nsm_host {
 
 static char		nsm_hostname[SM_MAXSTRLEN + 1];
 static int		nsm_state;
-static int		nsm_family = AF_INET;
 static int		opt_debug = 0;
 static _Bool		opt_update_state = true;
 static unsigned int	opt_max_retry = 15 * 60;
 static char *		opt_srcaddr = NULL;
 static char *		opt_srcport = NULL;
 
-static void		notify(const int sock);
-static int		notify_host(int, struct nsm_host *);
+/*
+ * sm-notify uses one datagram socket per address family so that it can
+ * reach both IPv4 and IPv6 peers without relying on IPv4-mapped IPv6
+ * addresses.  Either descriptor may be -1 when that family is
+ * unavailable (eg. IPv6 is disabled, or a source address was requested
+ * for one family only).
+ */
+struct nsm_sockets {
+	int	v4;
+	int	v6;
+};
+
+static void		notify(const struct nsm_sockets *socks);
+static int		notify_host(const struct nsm_sockets *, struct nsm_host *);
 static void		recv_reply(int);
 static void		insert_host(struct nsm_host *);
 static struct nsm_host *find_host(uint32_t);
@@ -90,7 +101,11 @@ smn_lookup(const char *name)
 {
 	struct addrinfo	*ai = NULL;
 	struct addrinfo hint = {
-		.ai_family	= (nsm_family == AF_INET ? AF_INET: AF_UNSPEC),
+#ifdef IPV6_SUPPORTED
+		.ai_family	= AF_UNSPEC,
+#else	/* !IPV6_SUPPORTED */
+		.ai_family	= AF_INET,
+#endif	/* !IPV6_SUPPORTED */
 		.ai_protocol	= (int)IPPROTO_UDP,
 	};
 	int error;
@@ -276,78 +291,53 @@ smn_get_host(const char *hostname,
 	return 1;
 }
 
-#ifdef IPV6_SUPPORTED
-static int smn_socket(void)
+/*
+ * Create a non-blocking datagram socket of the given address family.
+ *
+ * Returns the socket descriptor, or -1 if the family is unavailable
+ * or an error occurs.
+ */
+static int
+smn_socket(const int family)
 {
 	int sock;
 
-	/*
-	 * Use an AF_INET socket if IPv6 is disabled on the
-	 * local system.
-	 */
-	sock = socket(AF_INET6, SOCK_DGRAM, 0);
+	sock = socket(family, SOCK_DGRAM, 0);
 	if (sock == -1) {
-		if (errno != EAFNOSUPPORT) {
-			xlog(L_ERROR, "Failed to create RPC socket: %m");
+		/* The family being unsupported is not an error: the
+		 * caller simply won't use a socket for it. */
+		if (errno == EAFNOSUPPORT)
 			return -1;
-		}
-		sock = socket(AF_INET, SOCK_DGRAM, 0);
-		if (sock < 0) {
-			xlog(L_ERROR, "Failed to create RPC socket: %m");
-			return -1;
-		}
-	} else
-		nsm_family = AF_INET6;
+		xlog(L_ERROR, "Failed to create RPC socket: %m");
+		return -1;
+	}
 
 	if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
 		xlog(L_ERROR, "fcntl(3) on RPC socket failed: %m");
-		goto out_close;
+		(void)close(sock);
+		return -1;
 	}
 
+#ifdef IPV6_SUPPORTED
 	/*
-	 * TI-RPC over IPv6 (udp6/tcp6) does not handle IPv4.  However,
-	 * since sm-notify open-codes all of its RPC support, it can
-	 * use a single socket and let the local network stack provide
-	 * the correct mapping between address families automatically.
-	 * This is the same thing that is done in the kernel.
+	 * Keep the IPv6 socket strictly IPv6; the AF_INET socket is used
+	 * to reach IPv4 peers.  This avoids any dependence on IPv4-mapped
+	 * IPv6 addresses, which are not usable on all configurations.
 	 */
-	if (nsm_family == AF_INET6) {
-		const int zero = 0;
-		socklen_t zerolen = (socklen_t)sizeof(zero);
+	if (family == AF_INET6) {
+		const int one = 1;
 
 		if (setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
-					(char *)&zero, zerolen) == -1) {
+				(char *)&one, (socklen_t)sizeof(one)) == -1) {
 			xlog(L_ERROR, "setsockopt(3) on RPC socket failed: %m");
-			goto out_close;
+			(void)close(sock);
+			return -1;
 		}
 	}
+#endif	/* IPV6_SUPPORTED */
 
 	return sock;
-
-out_close:
-	(void)close(sock);
-	return -1;
 }
-#else	/* !IPV6_SUPPORTED */
-static int smn_socket(void)
-{
-	int sock;
-
-	sock = socket(AF_INET, SOCK_DGRAM, 0);
-	if (sock == -1) {
-		xlog(L_ERROR, "Failed to create RPC socket: %m");
-		return -1;
-	}
-
-	if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
-		xlog(L_ERROR, "fcntl(3) on RPC socket failed: %m");
-		(void)close(sock);
-		return -1;
-	}
-
-	return sock;
-}
-#endif	/* !IPV6_SUPPORTED */
 
 /*
  * If admin specified a source address or srcport, then convert those
@@ -355,12 +345,12 @@ static int smn_socket(void)
  */
 __attribute__((__malloc__))
 static struct addrinfo *
-smn_bind_address(const char *srcaddr, const char *srcport)
+smn_bind_address(const char *srcaddr, const char *srcport, const int family)
 {
 	struct addrinfo *ai = NULL;
 	struct addrinfo hint = {
-		.ai_flags	= AI_NUMERICSERV | AI_V4MAPPED,
-		.ai_family	= nsm_family,
+		.ai_flags	= AI_NUMERICSERV,
+		.ai_family	= family,
 		.ai_protocol	= (int)IPPROTO_UDP,
 	};
 	int error;
@@ -374,6 +364,17 @@ smn_bind_address(const char *srcaddr, const char *srcport)
 	else
 		error = getaddrinfo(srcaddr, srcport, &hint, &ai);
 	if (error != 0) {
+		/*
+		 * When a specific source address was requested, it is normal
+		 * for it to have no address in one of the families; that
+		 * family is simply not served by this invocation.
+		 */
+		if (srcaddr != NULL) {
+			xlog(D_GENERAL, "No %s source address for '%s': %s",
+				family == AF_INET ? "IPv4" : "IPv6",
+				srcaddr, gai_strerror(error));
+			return NULL;
+		}
 		xlog(L_ERROR,
 			"Invalid bind address or port for RPC socket: %s",
 				gai_strerror(error));
@@ -410,17 +411,17 @@ smn_bindresvport(int sock, struct sockaddr *sap)
  * an error occurs.
  */
 static int
-smn_create_socket(const char *srcaddr, const char *srcport)
+smn_create_socket(const int family, const char *srcaddr, const char *srcport)
 {
 	int sock, retry_cnt = 0;
 	struct addrinfo *ai;
 
 retry:
-	sock = smn_socket();
+	sock = smn_socket(family);
 	if (sock == -1)
 		return -1;
 
-	ai = smn_bind_address(srcaddr, srcport);
+	ai = smn_bind_address(srcaddr, srcport, family);
 	if (ai == NULL) {
 		(void)close(sock);
 		return -1;
@@ -503,8 +504,9 @@ read_smnotify_conf(char **argv)
 int
 main(int argc, char **argv)
 {
-	int	c, sock;
+	int	c;
 	char *	progname;
+	struct nsm_sockets socks;
 
 	progname = strrchr(argv[0], '/');
 	if (progname != NULL)
@@ -610,14 +612,21 @@ usage:		fprintf(stderr,
 		close(2);
 	}
 
-	sock = smn_create_socket(opt_srcaddr, opt_srcport);
-	if (sock == -1)
+	socks.v4 = smn_create_socket(AF_INET, opt_srcaddr, opt_srcport);
+#ifdef IPV6_SUPPORTED
+	socks.v6 = smn_create_socket(AF_INET6, opt_srcaddr, opt_srcport);
+#else	/* !IPV6_SUPPORTED */
+	socks.v6 = -1;
+#endif	/* !IPV6_SUPPORTED */
+	if (socks.v4 == -1 && socks.v6 == -1) {
+		xlog(L_ERROR, "Failed to create an RPC socket");
 		exit(1);
+	}
 
 	if (!nsm_drop_privileges(-1))
 		exit(1);
 
-	notify(sock);
+	notify(&socks);
 
 	if (hosts) {
 		struct nsm_host	*hp;
@@ -633,11 +642,50 @@ usage:		fprintf(stderr,
 	exit(0);
 }
 
+/*
+ * Return the socket descriptor to use for the given address family,
+ * or -1 if no socket is available for it.
+ */
+static int
+smn_sock_for_family(const struct nsm_sockets *socks, const int family)
+{
+	switch (family) {
+	case AF_INET:
+		return socks->v4;
+	case AF_INET6:
+		return socks->v6;
+	}
+	return -1;
+}
+
+/*
+ * Move the first address on a host's list to the end, so the next
+ * attempt uses a different address.  A no-op if there is only one.
+ */
+static void
+smn_rotate_address(struct nsm_host *host)
+{
+	if (host->ai->ai_next != NULL) {
+		struct addrinfo *first = host->ai;
+		struct addrinfo **next;
+
+		/* remove the first entry from the list */
+		host->ai = first->ai_next;
+		first->ai_next = NULL;
+		/* find the end of the (new) list */
+		next = &host->ai;
+		while ( *next )
+			next = & (*next)->ai_next;
+		/* put first entry at end */
+		*next = first;
+	}
+}
+
 /*
  * Notify hosts
  */
 static void
-notify(const int sock)
+notify(const struct nsm_sockets *socks)
 {
 	time_t	failtime = 0;
 
@@ -645,7 +693,8 @@ notify(const int sock)
 		failtime = time(NULL) + opt_max_retry;
 
 	while (hosts) {
-		struct pollfd	pfd;
+		struct pollfd	pfd[2];
+		int		nfds, i;
 		time_t		now = time(NULL);
 		unsigned int	sent = 0;
 		struct nsm_host	*hp;
@@ -663,7 +712,7 @@ notify(const int sock)
 			hp = hosts;
 			hosts = hp->next;
 
-			if (notify_host(sock, hp))
+			if (notify_host(socks, hp))
 				continue;
 
 			/* Set the timeout for this call, using an
@@ -682,16 +731,27 @@ notify(const int sock)
 		xlog(D_GENERAL, "Host %s due in %ld seconds",
 				hosts->name, wait);
 
-		pfd.fd = sock;
-		pfd.events = POLLIN;
+		nfds = 0;
+		if (socks->v4 != -1) {
+			pfd[nfds].fd = socks->v4;
+			pfd[nfds].events = POLLIN;
+			nfds++;
+		}
+		if (socks->v6 != -1) {
+			pfd[nfds].fd = socks->v6;
+			pfd[nfds].events = POLLIN;
+			nfds++;
+		}
 
 		wait *= 1000;
 		if (wait < 100)
 			wait = 100;
-		if (poll(&pfd, 1, wait) != 1)
+		if (poll(pfd, (nfds_t)nfds, wait) <= 0)
 			continue;
 
-		recv_reply(sock);
+		for (i = 0; i < nfds; i++)
+			if (pfd[i].revents & POLLIN)
+				recv_reply(pfd[i].fd);
 	}
 }
 
@@ -699,10 +759,13 @@ notify(const int sock)
  * Send notification to a single host
  */
 static int
-notify_host(int sock, struct nsm_host *host)
+notify_host(const struct nsm_sockets *socks, struct nsm_host *host)
 {
 	struct sockaddr *sap;
 	socklen_t salen;
+	unsigned int naddr, tried;
+	struct addrinfo *a;
+	int sock;
 
 	if (host->ai == NULL) {
 		host->ai = smn_lookup(host->name);
@@ -719,26 +782,36 @@ notify_host(int sock, struct nsm_host *host)
 	 * point.
 	 */
 	if (host->retries >= 4) {
-		/* don't rotate if there is only one addrinfo */
-		if (host->ai->ai_next != NULL) {
-			struct addrinfo *first = host->ai;
-			struct addrinfo **next;
-
-			/* remove the first entry from the list */
-			host->ai = first->ai_next;
-			first->ai_next = NULL;
-			/* find the end of the (new) list */
-			next = &host->ai;
-			while ( *next )
-				next = & (*next)->ai_next;
-			/* put first entry at end */
-			*next = first;
-		}
-
+		smn_rotate_address(host);
 		nfs_set_port(host->ai->ai_addr, 0);
 		host->retries = 0;
 	}
 
+	/*
+	 * Select the socket matching the address family of the current
+	 * address.  If we have no socket for that family (eg. a source
+	 * address was requested for the other family only), rotate through
+	 * the remaining addresses looking for one we can send from.  If
+	 * none is usable, give up on this host, leaving its on-disk record
+	 * so a later run can retry.
+	 */
+	naddr = 0;
+	for (a = host->ai; a != NULL; a = a->ai_next)
+		naddr++;
+
+	for (tried = 0; ; tried++) {
+		sock = smn_sock_for_family(socks, host->ai->ai_addr->sa_family);
+		if (sock != -1)
+			break;
+		if (tried + 1 >= naddr) {
+			xlog_warn("No usable source address to notify %s; "
+				"giving up", host->name);
+			return 1;
+		}
+		smn_rotate_address(host);
+		nfs_set_port(host->ai->ai_addr, 0);
+	}
+
 	sap = host->ai->ai_addr;
 	salen = host->ai->ai_addrlen;
 
Signature · 1 line
2.56.1
maintainer Reviewed-by on the v1 series
#7 lore raw
In reply to Scott Mayhew
Quoted 24 lines · Scott Mayhew

The first patch removes 4 files that should not be tracked in git and updates .gitignore so it doesn't happen again.

The remaining patches fix up a problem where the v1 version of Jeff's "mountd: fetch root filehandles over netlink" patch somehow got squashed into my "sm-notify: fix multihomed address rotation and first-attempt address choice" patch when it was committed. Note this isn't a straight revert - since Jeff's commit 3282da1d ("nfsd-iomode-advisor: recommend read_io= and write_io= for an export") subsequently modified the netlink header check in configure.ac, the configure.ac changes were omitted from both the revert and the corrected patches.

Jeff Layton (1):
mountd: fetch root filehandles over netlink

Scott Mayhew (5):
Update .gitignore
Revert "sm-notify: use a separate socket per address family" Revert "sm-notify: fix multihomed address rotation and first-attempt
address choice"
sm-notify: fix multihomed address rotation and first-attempt address
choice
sm-notify: use a separate socket per address family

Quoted 11 lines
 .gitignore                           |   5 ++++-
 support/export/cache.c               |   5 ++++-
 systemd/rpc_pipefs.target            |   3 ---
 systemd/var-lib-nfs-rpc_pipefs.mount |  10 ----------
 tools/nfs-fh-verify/nfs-fh-verify    | Bin 145328 -> 0 bytes
 tools/nfsrahead/99-nfs.rules         |   1 -
 6 files changed, 8 insertions(+), 16 deletions(-)
 delete mode 100644 systemd/rpc_pipefs.target
 delete mode 100644 systemd/var-lib-nfs-rpc_pipefs.mount
 delete mode 100755 tools/nfs-fh-verify/nfs-fh-verify
 delete mode 100644 tools/nfsrahead/99-nfs.rules

Thanks for cleaning this up, Scott.