Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796136582; b=UMJv8xHF2wRZ9EHftrz559tKnTfNE+hsL4wI5ZqyBWmLpFNHKmRkOCcYyA2hA5TpsIvS ZBI50Js8CyH9HW8c0c/ZQ7b42JZ4qombBe3tAtYmm0LhADUBAlMC+qY9VK5rPwYtlX3XZ zwoJim+BStSHBC1TaqNoDsYR+lxQ3Lwz3rnQomRNZD+LCiIsM8Nkyhfk9MILyN7lqG6K3 ZnW4p8quHbdW+giVcgljRmVYRpNaWwBxT1CA4+p1p8yueJewSA82ohif+UHfTkmeb7WVS zq08fIS5/NTDcnWY8qWBDk9D6qcVI3PaOvDW+lHMPoDcqfaLL+gKT94LrRmkxzCCn8w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796136582; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:Received:Received:From:To:Cc:Subject: Date:Message-ID:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Scanned-By; bh=FEpjSAyXwKX/u+xP5p1vOodu3vkzBW1N0ULk4WAsZOA=; b=a2waNWXR8dNXY3UOZZbR1TClC3O8DdHeT98J+QLWub2r125y/H0pSYa1leA7xzbvqBQC HUUKN0eDpK5VXh1r06SYWnT+Oc5DVQep9WwCIrpY451qzID4t8TtsbdBpXQIQZQTeC0qV eh8ryb5A6qVr6wrFyIaCN2lAb4V1AH7JDV3nhlOfYiKVsEynG/K68zKq4lVFjpoo4OT2b 0NxQ4W5pfyQeb8r4RLrtnT0s7BFJ8XIqnnoDLMhlUPQJdOijwJfMW/FVGcSK10jc+WvtL 5nmBR/xwUZBSJv96/1vNe/Gi+RrQ5ixmdyJ2KxV3M8GWL5Il9aF/NSNnAQLjWoeE67Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=PzDznJUB; arc=none smtp.remote-ip=170.10.133.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 40A91606A320 for ; Tue, 1 Dec 2026 14:49:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 40A91606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=PzDznJUB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796136582; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FEpjSAyXwKX/u+xP5p1vOodu3vkzBW1N0ULk4WAsZOA=; b=PzDznJUB1FitylhKqlz/f4/MrTd31QwvwR17iu48LoWi+YJEg+yCBOJO1pEbzUX2UOoBJ3 yc93ON9BGt3AdzPDGygiVBfmPkwii5oJPLpa74j8WS0j/9IdP8TiysGJWLSNb6msth4mc5 qZIkf9rE9KG6F8+AkKscSjsnyEKTQAs= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-592-2k3Yk-0_NwuEzg15UhuJmw-1; Tue, 1 Dec 2026 14:49:36 +0000 X-MC-Unique: 2k3Yk-0_NwuEzg15UhuJmw-1 X-Mimecast-MFC-AGG-ID: 2k3Yk-0_NwuEzg15UhuJmw_1796136575 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 865CD196DFCC; Tue, 1 Dec 2026 14:49:35 +0000 (UTC) Received: from smayhew-thinkpadp1gen4i.remote.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C1B151955F1C; Tue, 1 Dec 2026 14:49:34 +0000 (UTC) Received: from smayhew-thinkpadp1gen4i.redhat.corp (localhost [IPv6:::1]) by smayhew-thinkpadp1gen4i.remote.csb (Postfix) with ESMTP id 172E05D1DBD8; Tue, 01 Dec 2026 09:49:31 -0500 (EST) From: Scott Mayhew To: steved@redhat.com Cc: jlayton@kernel.org, linux-nfs@vger.kernel.org Subject: [nfs-utils PATCH 4/6] mountd: fetch root filehandles over netlink Date: Tue, 1 Dec 2026 09:49:28 -0500 Message-ID: <20261201144930.369297-5-smayhew@redhat.com> In-Reply-To: <20261201144930.369297-1-smayhew@redhat.com> References: <20261201144930.369297-1-smayhew@redhat.com> X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 From: Jeff Layton 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/20261021-nfsd-filehandle-get-v2-1-a317c596c9e6@kernel.org/). 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. Signed-off-by: Jeff Layton Signed-off-by: Scott Mayhew --- support/export/cache.c | 171 +++++++++++++++++++++++++++++--- support/include/lockd_netlink.h | 1 + support/include/nfsd_netlink.h | 15 ++- 3 files changed, 170 insertions(+), 17 deletions(-) diff --git a/support/export/cache.c b/support/export/cache.c index 65a59b2a..93b44cf0 100644 --- a/support/export/cache.c +++ b/support/export/cache.c @@ -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; } diff --git a/support/include/lockd_netlink.h b/support/include/lockd_netlink.h index 21c65aec..2d766a0f 100644 --- a/support/include/lockd_netlink.h +++ b/support/include/lockd_netlink.h @@ -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 diff --git a/support/include/nfsd_netlink.h b/support/include/nfsd_netlink.h index 942a1c6b..f67efa23 100644 --- a/support/include/nfsd_netlink.h +++ b/support/include/nfsd_netlink.h @@ -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) -- 2.56.1