Skip to contentSkip to conversation

LOCALIO: don't recall a localio client's own delegations

PATCH New Jeff Layton 1 participant 4 messages Dec 4

Subsystems (2)
lore
Share & tools
lore

Jeff Layton

author maintainer
#0 lore raw

cover LOCALIO: don't recall a localio client's own delegations

This patchset fixes an issue with localio: namely that localio opens break the the client's own lease. Because that open is done from the client's context, we can't rely on looking at whether it's an nfsd thread.

Thus we need to do a bit of extra tracking in the server to keep track of tasks in localio. The first patch adds this. The second and third patches make the localio callers in the client pass in the right info to vet the lease holder and breaker.

This is a combined client and server patchset, but most of the changes are in nfsd, so this should probably go in via Chuck's tree.

Please consider for v7.6!

---
Jeff Layton (3):
NFSD: don't recall a LOCALIO client's own delegations
NFS: tell nfsd which client a LOCALIO open is for pNFS/flexfiles: tell nfsd which client a LOCALIO data server open is for

 fs/nfs/flexfilelayout/flexfilelayout.c | 10 ++++-
 fs/nfs/internal.h                      |  7 +--
 fs/nfs/localio.c                       | 39 +++++++++++++----
 fs/nfs/pagelist.c                      |  2 +-
 fs/nfs/write.c                         |  3 +-
 fs/nfs_common/nfslocalio.c             | 13 +++++-
 fs/nfsd/localio.c                      | 14 +++++-
 fs/nfsd/netns.h                        |  3 ++
 fs/nfsd/nfs4state.c                    | 78 ++++++++++++++++++++++++++++++++++
 fs/nfsd/nfsctl.c                       |  2 +
 fs/nfsd/state.h                        | 22 ++++++++++
 include/linux/nfslocalio.h             |  5 ++-
 12 files changed, 179 insertions(+), 19 deletions(-)

---
base-commit: eb76c146d6facf9588b6c1da360e2db41cdda335
change-id: 20261204-localio-deleg-d07d4e7ee937

Best regards,

Signature · 1 line
Jeff Layton <[email protected]>
author maintainer
#1 lore raw

PATCH 1/3 NFSD: don't recall a LOCALIO client's own delegations

Opening a file for LOCALIO breaks leases like any other nfsd open, but it runs in the NFS client's task, not in an nfsd thread. nfsd_breaker_owns_lease() identifies the breaker through the svc_rqst of the current nfsd thread, finds none, and treats the client's own delegation as a conflict. So the client's first LOCALIO read or write recalls its own delegation:

nfsd_cb_recall <- nfsd_break_deleg_cb <- __break_lease
<- nfsd_file_do_acquire <- nfsd_file_acquire_local <- nfsd_open_local_fh <- nfs_open_local_fh <- nfs_local_open_fh

The open fails with NFS4ERR_DELAY, that I/O goes over the wire, and the client returns the delegation. A LOCALIO client never keeps a write delegation past its first I/O. When the open misses the nfsd_file cache, do_dentry_open() breaks leases again, and since that break can block, the open would also wait for the recall to complete.

Let the caller of nfsd_open_local_fh() pass the NFSv4 client ID of the client whose open state covers the I/O. While nfsd opens the file, it records that client ID against the current task, on a list in the server's network namespace. When there is no svc_rqst, nfsd_breaker_owns_lease() looks the task up on the list of the namespace of the client holding the delegation, and compares the ID with that client's. This covers nfsd's own lease break and the one in do_dentry_open(). Other clients' delegations are still recalled, and so is every delegation when the client ID is 0 or doesn't name the client holding it.

The client itself isn't looked up or pinned. nfsd shutdown destroys clients and frees the client tables without waiting for LOCALIO opens in progress, and the delegation being broken pins its client anyway.

The client ID can't be inferred from the LOCALIO handshake: the I/O may come from an NFSv3 nfs_client, as with flexfiles data servers, while the delegation belongs to the NFSv4 client of the metadata server. Nothing passes a client ID yet.

Fixes fa4983862e50 ("nfsd: add LOCALIO support")
---
 fs/nfs_common/nfslocalio.c |  2 +-
 fs/nfsd/localio.c          | 14 ++++++++-
 fs/nfsd/netns.h            |  3 ++
 fs/nfsd/nfs4state.c        | 78 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/nfsd/nfsctl.c           |  2 ++
 fs/nfsd/state.h            | 22 +++++++++++++
 include/linux/nfslocalio.h |  2 +-
 7 files changed, 120 insertions(+), 3 deletions(-)
fs/nfs_common/nfslocalio.c +1 -1
index b2b8d7c3d93e..e77e42c2d567 100644
@@ -365,7 +365,7 @@ struct nfsd_file *nfs_open_local_fh(nfs_uuid_t *uuid,
 	rcu_read_unlock();
 	/* We have an implied reference to net thanks to nfsd_net_try_get */
 	localio = nfs_to->nfsd_open_local_fh(net, dom, rpc_clnt, cred,
-					     nfs_fh, pnf, fmode);
+					     nfs_fh, pnf, fmode, 0);
 	if (!IS_ERR(localio) && nfs_uuid_add_file(uuid, nfl) < 0) {
 		/*
 		 * Delete the cached file when racing with nfs_uuid_put().
fs/nfsd/localio.c +13 -1
index 33b56d1b3f44..c735f1f4e9eb 100644
@@ -21,6 +21,7 @@
 #include "netns.h"
 #include "filecache.h"
 #include "cache.h"
+#include "state.h"
 
 /**
  * nfsd_open_local_fh - lookup a local filehandle @nfs_fh and map to nfsd_file
@@ -32,6 +33,8 @@
  * @nfs_fh: filehandle to lookup
  * @pnf: place to find the nfsd_file, or store it if it was non-NULL
  * @fmode: fmode_t to use for open
+ * @clientid: NFSv4 client ID of the NFS client whose open state covers the
+ *	I/O, or 0. Its delegations aren't recalled by this open.
  *
  * This function maps a local fh to a path on a local filesystem.
  * This is useful when the nfs client has the local server mounted - it can
@@ -45,9 +48,10 @@ static struct nfsd_file *
 nfsd_open_local_fh(struct net *net, struct auth_domain *dom,
 		   struct rpc_clnt *rpc_clnt, const struct cred *cred,
 		   const struct nfs_fh *nfs_fh, struct nfsd_file __rcu **pnf,
-		   const fmode_t fmode)
+		   const fmode_t fmode, u64 clientid)
 {
 	int mayflags = NFSD_MAY_LOCALIO;
+	struct nfsd_localio_breaker breaker;
 	struct svc_cred rq_cred;
 	struct svc_fh fh;
 	struct nfsd_file *localio;
@@ -77,8 +81,16 @@ nfsd_open_local_fh(struct net *net, struct auth_domain *dom,
 
 	svcauth_map_clnt_to_svc_cred_local(rpc_clnt, cred, &rq_cred);
 
+	/*
+	 * Opening the file breaks leases, both in nfsd and in the VFS, from
+	 * this task. Tell nfsd_breaker_owns_lease() which client that is.
+	 */
+	if (clientid)
+		nfsd4_localio_breaker_begin(net, clientid, &breaker);
 	beres = nfsd_file_acquire_local(net, &rq_cred, dom,
 					&fh, mayflags, &localio);
+	if (clientid)
+		nfsd4_localio_breaker_end(net, &breaker);
 	if (beres)
 		localio = ERR_PTR(nfs_stat_to_errno(be32_to_cpu(beres)));
 
fs/nfsd/netns.h +3 -0
index 16be473f2c04..8ab306111251 100644
@@ -277,6 +277,9 @@ struct nfsd_net {
 	/* Local clients to be invalidated when net is shut down */
 	spinlock_t              local_clients_lock;
 	struct list_head	local_clients;
+	/* LOCALIO opens in progress: struct nfsd_localio_breaker */
+	spinlock_t		localio_breakers_lock;
+	struct list_head	localio_breakers;
 #endif
 	siphash_key_t		*fh_key;
 
fs/nfsd/nfs4state.c +78 -0

90 lines not on this page · raw message

fs/nfsd/nfsctl.c +2 -0
index b4411b253ad8..352a4c896252 100644
@@ -3068,6 +3068,8 @@ static __net_init int nfsd_net_init(struct net *net)
 #if IS_ENABLED(CONFIG_NFS_LOCALIO)
 	spin_lock_init(&nn->local_clients_lock);
 	INIT_LIST_HEAD(&nn->local_clients);
+	spin_lock_init(&nn->localio_breakers_lock);
+	INIT_LIST_HEAD(&nn->localio_breakers);
 #endif
 	return 0;
 
fs/nfsd/state.h +22 -0
index 7afdf6ef6727..8036b01e0725 100644
@@ -997,12 +997,26 @@ static inline void get_nfs4_file(struct nfs4_file *fi)
 }
 struct nfsd_file *find_any_file(struct nfs4_file *f);
 
+/*
+ * A LOCALIO open runs in the NFS client's task, not in an nfsd thread, so
+ * the lease breaks it causes can't be tied to an NFSv4 client through the
+ * svc_rqst. This records which client the task is opening the file for.
+ */
+struct nfsd_localio_breaker {
+	struct list_head	list;
+	struct task_struct	*task;
+	clientid_t		clid;
+};
+
 #ifdef CONFIG_NFSD_V4
 void nfsd4_revoke_states(struct nfsd_net *nn, struct super_block *sb);
 void nfsd4_revoke_export_states(struct nfsd_net *nn, const struct path *path);
 void nfsd4_cancel_copy_by_sb(struct net *net, struct super_block *sb);
 int nfsd_net_cb_init(struct nfsd_net *nn);
 void nfsd_net_cb_shutdown(struct nfsd_net *nn);
+void nfsd4_localio_breaker_begin(struct net *net, u64 clientid,
+				 struct nfsd_localio_breaker *b);
+void nfsd4_localio_breaker_end(struct net *net, struct nfsd_localio_breaker *b);
 #else
 static inline void nfsd4_revoke_states(struct nfsd_net *nn, struct super_block *sb)
 {
@@ -1021,6 +1035,14 @@ static inline int nfsd_net_cb_init(struct nfsd_net *nn)
 static inline void nfsd_net_cb_shutdown(struct nfsd_net *nn)
 {
 }
+static inline void nfsd4_localio_breaker_begin(struct net *net, u64 clientid,
+					       struct nfsd_localio_breaker *b)
+{
+}
+static inline void nfsd4_localio_breaker_end(struct net *net,
+					     struct nfsd_localio_breaker *b)
+{
+}
 #endif
 
 /* grace period management */
include/linux/nfslocalio.h +1 -1
index 914246e3f342..6b4746dde88a 100644
@@ -78,7 +78,7 @@ struct nfsd_localio_operations {
 						const struct cred *,
 						const struct nfs_fh *,
 						struct nfsd_file __rcu **pnf,
-						const fmode_t);
+						const fmode_t, u64 clientid);
 	struct net *(*nfsd_file_put_local)(struct nfsd_file __rcu **);
 	struct file *(*nfsd_file_file)(struct nfsd_file *);
 	void (*nfsd_file_dio_alignment)(struct nfsd_file *,
Signature · 1 line
2.56.1
author maintainer
#2 lore raw

PATCH 2/3 NFS: tell nfsd which client a LOCALIO open is for

In reply to Jeff Layton

Pass nfsd the NFSv4 client ID of the client whose open state covers a LOCALIO read, write or commit, so that opening the file doesn't recall that client's own delegations. nfs_local_open_fh() gets a second client argument for this. For ordinary I/O it is the client the I/O goes through; for NFSv3 mounts it yields no client ID, and nothing changes. nfs_open_local_fh() only passes the ID on if that client talks to the nfsd instance it has pinned for the open, since client IDs are only unique within a server.

flexfilelayout passes NULL for now.

Fixes fa4983862e50 ("nfsd: add LOCALIO support")
---
 fs/nfs/flexfilelayout/flexfilelayout.c |  3 ++-
 fs/nfs/internal.h                      |  7 +++---
 fs/nfs/localio.c                       | 39 +++++++++++++++++++++++++++-------
 fs/nfs/pagelist.c                      |  2 +-
 fs/nfs/write.c                         |  3 ++-
 fs/nfs_common/nfslocalio.c             | 13 ++++++++++--
 include/linux/nfslocalio.h             |  3 ++-
 7 files changed, 53 insertions(+), 17 deletions(-)
fs/nfs/flexfilelayout/flexfilelayout.c +2 -1
index 5c289737366f..8ed5f96e9706 100644
@@ -173,7 +173,8 @@ ff_local_open_fh(struct pnfs_layout_segment *lseg, u32 ds_idx, u32 dss_id,
 #if IS_ENABLED(CONFIG_NFS_LOCALIO)
 	struct nfs4_ff_layout_mirror *mirror = FF_LAYOUT_COMP(lseg, ds_idx);
 
-	return nfs_local_open_fh(clp, cred, fh, &mirror->dss[dss_id].nfl, mode);
+	return nfs_local_open_fh(clp, NULL, cred, fh, &mirror->dss[dss_id].nfl,
+				 mode);
 #else
 	return NULL;
 #endif
fs/nfs/internal.h +4 -3
index 5af6a032f145..415c5c4d0d8f 100644
@@ -474,6 +474,7 @@ struct nfs_local_dio {
 extern void nfs_local_probe_async(struct nfs_client *);
 extern void nfs_local_probe_async_work(struct work_struct *);
 extern struct nfsd_file *nfs_local_open_fh(struct nfs_client *,
+					   const struct nfs_client *,
 					   const struct cred *,
 					   struct nfs_fh *,
 					   struct nfs_file_localio *,
@@ -491,9 +492,9 @@ extern bool nfs_server_is_local(const struct nfs_client *clp);
 static inline void nfs_local_probe(struct nfs_client *clp) {}
 static inline void nfs_local_probe_async(struct nfs_client *clp) {}
 static inline struct nfsd_file *
-nfs_local_open_fh(struct nfs_client *clp, const struct cred *cred,
-		  struct nfs_fh *fh, struct nfs_file_localio *nfl,
-		  const fmode_t mode)
+nfs_local_open_fh(struct nfs_client *clp, const struct nfs_client *state_clp,
+		  const struct cred *cred, struct nfs_fh *fh,
+		  struct nfs_file_localio *nfl, const fmode_t mode)
 {
 	return NULL;
 }
fs/nfs/localio.c +31 -8
index 7f8d794581a3..c38476d1a8c8 100644
@@ -334,6 +334,23 @@ static inline void nfs_local_file_put(struct nfsd_file *localio)
 	nfs_to_nfsd_file_put_local(&nf);
 }
 
+/*
+ * The NFSv4 client ID of @state_clp, so that nfsd doesn't recall that
+ * client's own delegations, or 0 if there is none. nfs_open_local_fh()
+ * only passes it on if @state_clp talks to the server it opens the file
+ * on, since client IDs are only unique within one server.
+ */
+static u64 nfs_local_clientid(const struct nfs_client *state_clp)
+{
+#if IS_ENABLED(CONFIG_NFS_V4)
+	if (!state_clp || state_clp->rpc_ops->version != 4)
+		return 0;
+	return state_clp->cl_clientid;
+#else
+	return 0;
+#endif
+}
+
 /*
  * __nfs_local_open_fh - open a local filehandle in terms of nfsd_file.
  *
@@ -341,16 +358,18 @@ static inline void nfs_local_file_put(struct nfsd_file *localio)
  * Caller must release returned nfsd_file with nfs_to_nfsd_file_put_local().
  */
 static struct nfsd_file *
-__nfs_local_open_fh(struct nfs_client *clp, const struct cred *cred,
-		    struct nfs_fh *fh, struct nfs_file_localio *nfl,
-		    struct nfsd_file __rcu **pnf,
+__nfs_local_open_fh(struct nfs_client *clp, const struct nfs_client *state_clp,
+		    const struct cred *cred, struct nfs_fh *fh,
+		    struct nfs_file_localio *nfl, struct nfsd_file __rcu **pnf,
 		    const fmode_t mode)
 {
 	int status = 0;
 	struct nfsd_file *localio;
 
 	localio = nfs_open_local_fh(&clp->cl_uuid, clp->cl_rpcclient,
-				    cred, fh, nfl, pnf, mode);
+				    cred, fh, nfl, pnf, mode,
+				    state_clp ? &state_clp->cl_uuid : NULL,
+				    nfs_local_clientid(state_clp));
 	if (IS_ERR(localio)) {
 		status = PTR_ERR(localio);
 		switch (status) {
@@ -371,12 +390,16 @@ __nfs_local_open_fh(struct nfs_client *clp, const struct cred *cred,
  * First checking if the open nfsd_file is already cached, otherwise
  * must __nfs_local_open_fh and insert the nfsd_file in nfs_file_localio.
  *
+ * @clp is the client the I/O is done through. @state_clp is the NFSv4
+ * client whose open state covers it, if any: the same client for NFSv4
+ * I/O, the metadata server's client for pNFS I/O to a data server.
+ *
  * Returns a pointer to a struct nfsd_file or NULL.
  */
 struct nfsd_file *
-nfs_local_open_fh(struct nfs_client *clp, const struct cred *cred,
-		  struct nfs_fh *fh, struct nfs_file_localio *nfl,
-		  const fmode_t mode)
+nfs_local_open_fh(struct nfs_client *clp, const struct nfs_client *state_clp,
+		  const struct cred *cred, struct nfs_fh *fh,
+		  struct nfs_file_localio *nfl, const fmode_t mode)
 {
 	struct nfsd_file *nf, __rcu **pnf;
 
@@ -390,7 +413,7 @@ nfs_local_open_fh(struct nfs_client *clp, const struct cred *cred,
 	else
 		pnf = &nfl->ro_file;
 
-	nf = __nfs_local_open_fh(clp, cred, fh, nfl, pnf, mode);
+	nf = __nfs_local_open_fh(clp, state_clp, cred, fh, nfl, pnf, mode);
 	if (IS_ERR(nf))
 		return NULL;
 	return nf;
fs/nfs/pagelist.c +1 -1
index 71f0ce2bc4ea..2219c0595083 100644
@@ -1009,7 +1009,7 @@ static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
 		struct nfs_client *clp = NFS_SERVER(hdr->inode)->nfs_client;
 
 		struct nfsd_file *localio =
-			nfs_local_open_fh(clp, hdr->cred, hdr->args.fh,
+			nfs_local_open_fh(clp, clp, hdr->cred, hdr->args.fh,
 					  &hdr->args.context->nfl,
 					  hdr->args.context->mode);
 
fs/nfs/write.c +2 -1
index 0b42d31f7a75..6f70bcb6775e 100644
@@ -1782,7 +1782,8 @@ nfs_commit_list(struct inode *inode, struct list_head *head, int how,
 	if (NFS_SERVER(inode)->nfs_client->cl_minorversion)
 		task_flags = RPC_TASK_MOVEABLE;
 
-	localio = nfs_local_open_fh(NFS_SERVER(inode)->nfs_client, data->cred,
+	localio = nfs_local_open_fh(NFS_SERVER(inode)->nfs_client,
+				    NFS_SERVER(inode)->nfs_client, data->cred,
 				    data->args.fh, &data->context->nfl,
 				    data->context->mode);
 	return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode),
fs/nfs_common/nfslocalio.c +11 -2
index e77e42c2d567..676cd350a82b 100644
@@ -335,7 +335,8 @@ struct nfsd_file *nfs_open_local_fh(nfs_uuid_t *uuid,
 		   struct rpc_clnt *rpc_clnt, const struct cred *cred,
 		   const struct nfs_fh *nfs_fh, struct nfs_file_localio *nfl,
 		   struct nfsd_file __rcu **pnf,
-		   const fmode_t fmode)
+		   const fmode_t fmode, const nfs_uuid_t *state_uuid,
+		   u64 clientid)
 {
 	struct net *net;
 	struct auth_domain *dom;
@@ -362,10 +363,18 @@ struct nfsd_file *nfs_open_local_fh(nfs_uuid_t *uuid,
 	dom = uuid->dom;
 	kref_get(&dom->ref);
 	spin_unlock(&uuid->lock);
+	/*
+	 * @clientid belongs to the client that @state_uuid is for. Client IDs
+	 * are only unique within one server, so drop it unless that client
+	 * talks to the server just pinned.
+	 */
+	if (state_uuid != uuid &&
+	    (!state_uuid || rcu_access_pointer(state_uuid->net) != net))
+		clientid = 0;
 	rcu_read_unlock();
 	/* We have an implied reference to net thanks to nfsd_net_try_get */
 	localio = nfs_to->nfsd_open_local_fh(net, dom, rpc_clnt, cred,
-					     nfs_fh, pnf, fmode, 0);
+					     nfs_fh, pnf, fmode, clientid);
 	if (!IS_ERR(localio) && nfs_uuid_add_file(uuid, nfl) < 0) {
 		/*
 		 * Delete the cached file when racing with nfs_uuid_put().
include/linux/nfslocalio.h +2 -1
index 6b4746dde88a..5a92a0ca955b 100644
@@ -92,7 +92,8 @@ struct nfsd_file *nfs_open_local_fh(nfs_uuid_t *,
 		   struct rpc_clnt *, const struct cred *,
 		   const struct nfs_fh *, struct nfs_file_localio *,
 		   struct nfsd_file __rcu **pnf,
-		   const fmode_t);
+		   const fmode_t, const nfs_uuid_t *state_uuid,
+		   u64 clientid);
 
 static inline void nfs_to_nfsd_net_put(struct net *net)
 {
Signature · 1 line
2.56.1
author maintainer
#3 lore raw

PATCH 3/3 pNFS/flexfiles: tell nfsd which client a LOCALIO data server open is for

In reply to Jeff Layton

A knfsd that hands out flexfiles layouts names itself as the data server, with NFSv3 and the same filehandle. When the client does that I/O with LOCALIO, the open goes through the NFSv3 data server client, which has no NFSv4 client ID, so nfsd recalls the delegation that the client holds on the same file from the metadata server.

The I/O is covered by the open state on the metadata server, so pass that client to nfs_local_open_fh(). Its client ID is only sent when the metadata server and the data server are the same local nfsd instance, since client IDs are only unique within one server; otherwise leases are broken as before.

Fixes d488b9d01fbc ("pnfs/flexfiles: enable localio support")
---
 fs/nfs/flexfilelayout/flexfilelayout.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
fs/nfs/flexfilelayout/flexfilelayout.c +9 -2
index 8ed5f96e9706..055527b9d13d 100644
@@ -172,9 +172,16 @@ ff_local_open_fh(struct pnfs_layout_segment *lseg, u32 ds_idx, u32 dss_id,
 {
 #if IS_ENABLED(CONFIG_NFS_LOCALIO)
 	struct nfs4_ff_layout_mirror *mirror = FF_LAYOUT_COMP(lseg, ds_idx);
+	/*
+	 * The I/O is covered by open state on the metadata server. When the
+	 * data server is the same knfsd, opening the file there must not
+	 * recall that client's own delegation.
+	 */
+	struct nfs_client *mds_clp =
+		NFS_SERVER(lseg->pls_layout->plh_inode)->nfs_client;
 
-	return nfs_local_open_fh(clp, NULL, cred, fh, &mirror->dss[dss_id].nfl,
-				 mode);
+	return nfs_local_open_fh(clp, mds_clp, cred, fh,
+				 &mirror->dss[dss_id].nfl, mode);
 #else
 	return NULL;
 #endif
Signature · 1 line
2.56.1