linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff Layton <jlayton@kernel.org>
To: Alexander Viro <viro@zeniv.linux.org.uk>,
	 Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
	 Chuck Lever <chuck.lever@oracle.com>,
	 Alexander Aring <alex.aring@gmail.com>,
	 Trond Myklebust <trondmy@kernel.org>,
	Anna Schumaker <anna@kernel.org>,
	 Steve French <sfrench@samba.org>,
	Paulo Alcantara <pc@manguebit.com>,
	 Ronnie Sahlberg <ronniesahlberg@gmail.com>,
	 Shyam Prasad N <sprasad@microsoft.com>,
	Tom Talpey <tom@talpey.com>,
	 Bharath SM <bharathsm@microsoft.com>,
	NeilBrown <neil@brown.name>,
	 Olga Kornievskaia <okorniev@redhat.com>,
	Dai Ngo <Dai.Ngo@oracle.com>,  Jonathan Corbet <corbet@lwn.net>,
	Amir Goldstein <amir73il@gmail.com>,
	 Miklos Szeredi <miklos@szeredi.hu>
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	 linux-nfs@vger.kernel.org, linux-cifs@vger.kernel.org,
	 samba-technical@lists.samba.org, linux-doc@vger.kernel.org,
	 Jeff Layton <jlayton@kernel.org>
Subject: [PATCH RFC v2 20/28] nfsd: add data structures for handling CB_NOTIFY to directory delegation
Date: Mon, 02 Jun 2025 10:02:03 -0400	[thread overview]
Message-ID: <20250602-dir-deleg-v2-20-a7919700de86@kernel.org> (raw)
In-Reply-To: <20250602-dir-deleg-v2-0-a7919700de86@kernel.org>

When a directory delegation is created, have it allocate the necessary
data structures to collect events and run a CB_NOTIFY callback.

Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
 fs/nfsd/nfs4state.c | 143 +++++++++++++++++++++++++++++++++++++++++++++-------
 fs/nfsd/state.h     |  33 +++++++++++-
 2 files changed, 157 insertions(+), 19 deletions(-)

diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
index ed5d6486d171ea0c886bd1f1ea1129bf4ccf429c..ebebfd6d304627d6c82bae5b84ea6c599d9e9474 100644
--- a/fs/nfsd/nfs4state.c
+++ b/fs/nfsd/nfs4state.c
@@ -130,6 +130,7 @@ static void free_session(struct nfsd4_session *);
 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
 static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops;
+static const struct nfsd4_callback_ops nfsd4_cb_notify_ops;
 
 static struct workqueue_struct *laundry_wq;
 
@@ -1048,6 +1049,45 @@ static void nfs4_free_deleg(struct nfs4_stid *stid)
 	atomic_long_dec(&num_delegations);
 }
 
+static struct nfsd4_notify_spool *alloc_notify_spool(void)
+{
+	struct nfsd4_notify_spool *spool;
+
+	spool = kmalloc(sizeof(*spool), GFP_KERNEL);
+	if (!spool)
+		return NULL;
+
+	spool->nns_page = alloc_page(GFP_KERNEL);
+	if (!spool->nns_page) {
+		kfree(spool);
+		return NULL;
+	}
+
+	spool->nns_idx = 0;
+	spool->nns_xdr.buflen = PAGE_SIZE;
+	spool->nns_xdr.pages = &spool->nns_page;
+
+	xdr_init_encode_pages(&spool->nns_stream, &spool->nns_xdr);
+	return spool;
+}
+
+static void free_notify_spool(struct nfsd4_notify_spool *spool)
+{
+	if (spool) {
+		put_page(spool->nns_page);
+		kfree(spool);
+	}
+}
+
+static void nfs4_free_dir_deleg(struct nfs4_stid *stid)
+{
+	struct nfs4_delegation *dp = delegstateid(stid);
+
+	free_notify_spool(dp->dl_cb_notify.ncn_gather);
+	free_notify_spool(dp->dl_cb_notify.ncn_send);
+	nfs4_free_deleg(stid);
+}
+
 /*
  * When we recall a delegation, we should be careful not to hand it
  * out again straight away.
@@ -1126,29 +1166,22 @@ static void block_delegations(struct knfsd_fh *fh)
 }
 
 static struct nfs4_delegation *
-alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
-		 struct nfs4_clnt_odstate *odstate, u32 dl_type)
+__alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
+		   struct nfs4_clnt_odstate *odstate, u32 dl_type,
+		   void (*sc_free)(struct nfs4_stid *))
 {
+	struct nfs4_stid *stid = nfs4_alloc_stid(clp, deleg_slab, sc_free);
 	struct nfs4_delegation *dp;
-	struct nfs4_stid *stid;
-	long n;
 
-	dprintk("NFSD alloc_init_deleg\n");
-	n = atomic_long_inc_return(&num_delegations);
-	if (n < 0 || n > max_delegations)
-		goto out_dec;
-	if (delegation_blocked(&fp->fi_fhandle))
-		goto out_dec;
-	stid = nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg);
 	if (stid == NULL)
-		goto out_dec;
-	dp = delegstateid(stid);
+		return NULL;
 
 	/*
 	 * delegation seqid's are never incremented.  The 4.1 special
 	 * meaning of seqid 0 isn't meaningful, really, but let's avoid
-	 * 0 anyway just for consistency and use 1:
+	 * 0 anyway just for consistency and use 1.
 	 */
+	dp = delegstateid(stid);
 	dp->dl_stid.sc_stateid.si_generation = 1;
 	INIT_LIST_HEAD(&dp->dl_perfile);
 	INIT_LIST_HEAD(&dp->dl_perclnt);
@@ -1158,19 +1191,65 @@ alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
 	dp->dl_type = dl_type;
 	dp->dl_retries = 1;
 	dp->dl_recalled = false;
+	get_nfs4_file(fp);
+	dp->dl_stid.sc_file = fp;
 	nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
 		      &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
+	return dp;
+}
+
+static struct nfs4_delegation *
+alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
+		 struct nfs4_clnt_odstate *odstate, u32 dl_type)
+{
+	struct nfs4_delegation *dp;
+	long n;
+
+	dprintk("NFSD alloc_init_deleg\n");
+	n = atomic_long_inc_return(&num_delegations);
+	if (n < 0 || n > max_delegations)
+		goto out_dec;
+	if (delegation_blocked(&fp->fi_fhandle))
+		goto out_dec;
+
+	dp = __alloc_init_deleg(clp, fp, odstate, dl_type, nfs4_free_deleg);
+	if (!dp)
+		goto out_dec;
+
 	nfsd4_init_cb(&dp->dl_cb_fattr.ncf_getattr, dp->dl_stid.sc_client,
 			&nfsd4_cb_getattr_ops, NFSPROC4_CLNT_CB_GETATTR);
 	dp->dl_cb_fattr.ncf_file_modified = false;
-	get_nfs4_file(fp);
-	dp->dl_stid.sc_file = fp;
 	return dp;
 out_dec:
 	atomic_long_dec(&num_delegations);
 	return NULL;
 }
 
+static struct nfs4_delegation *
+alloc_init_dir_deleg(struct nfs4_client *clp, struct nfs4_file *fp)
+{
+	struct nfs4_delegation *dp;
+	struct nfsd4_cb_notify *cbn;
+	struct nfsd4_notify_spool *ncn;
+
+	ncn = alloc_notify_spool();
+	if (!ncn)
+		return NULL;
+
+	dp = __alloc_init_deleg(clp, fp, NULL, NFS4_OPEN_DELEGATE_READ,
+				nfs4_free_dir_deleg);
+	if (!dp) {
+		free_notify_spool(ncn);
+		return NULL;
+	}
+
+	cbn = &dp->dl_cb_notify;
+	cbn->ncn_gather = ncn;
+	nfsd4_init_cb(&cbn->ncn_cb, dp->dl_stid.sc_client,
+			&nfsd4_cb_notify_ops, NFSPROC4_CLNT_CB_NOTIFY);
+	return dp;
+}
+
 void
 nfs4_put_stid(struct nfs4_stid *s)
 {
@@ -3197,6 +3276,30 @@ nfsd4_cb_getattr_release(struct nfsd4_callback *cb)
 	nfs4_put_stid(&dp->dl_stid);
 }
 
+static int
+nfsd4_cb_notify_done(struct nfsd4_callback *cb,
+				struct rpc_task *task)
+{
+	switch (task->tk_status) {
+	case -NFS4ERR_DELAY:
+		rpc_delay(task, 2 * HZ);
+		return 0;
+	default:
+		return 1;
+	}
+}
+
+static void
+nfsd4_cb_notify_release(struct nfsd4_callback *cb)
+{
+	struct nfsd4_cb_notify *ncn =
+			container_of(cb, struct nfsd4_cb_notify, ncn_cb);
+	struct nfs4_delegation *dp =
+			container_of(ncn, struct nfs4_delegation, dl_cb_notify);
+
+	nfs4_put_stid(&dp->dl_stid);
+}
+
 static const struct nfsd4_callback_ops nfsd4_cb_recall_any_ops = {
 	.done		= nfsd4_cb_recall_any_done,
 	.release	= nfsd4_cb_recall_any_release,
@@ -3209,6 +3312,12 @@ static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops = {
 	.opcode		= OP_CB_GETATTR,
 };
 
+static const struct nfsd4_callback_ops nfsd4_cb_notify_ops = {
+	.done		= nfsd4_cb_notify_done,
+	.release	= nfsd4_cb_notify_release,
+	.opcode		= OP_CB_NOTIFY,
+};
+
 static void nfs4_cb_getattr(struct nfs4_cb_fattr *ncf)
 {
 	struct nfs4_delegation *dp =
@@ -9350,7 +9459,7 @@ nfsd_get_dir_deleg(struct nfsd4_compound_state *cstate,
 
 	/* Try to set up the lease */
 	status = -ENOMEM;
-	dp = alloc_init_deleg(clp, fp, NULL, NFS4_OPEN_DELEGATE_READ);
+	dp = alloc_init_dir_deleg(clp, fp);
 	if (!dp)
 		goto out_delegees;
 
diff --git a/fs/nfsd/state.h b/fs/nfsd/state.h
index 5f21c79be032cc1334a301aad73e6bbcc8da5eb0..706bbc7076a4f1d0be3ea7067d193683821d74eb 100644
--- a/fs/nfsd/state.h
+++ b/fs/nfsd/state.h
@@ -188,6 +188,31 @@ struct nfs4_cb_fattr {
 	u64 ncf_cur_fsize;
 };
 
+#define NFSD4_NOTIFY_SPOOL_SZ	16
+
+/* A place to collect notifications */
+struct nfsd4_notify_spool {
+	struct xdr_stream	nns_stream;
+	struct xdr_buf		nns_xdr;
+	struct page		*nns_page;
+	struct notify4		nns_ent[NFSD4_NOTIFY_SPOOL_SZ];
+	u32			nns_idx;
+};
+
+/*
+ * Represents a directory delegation. The callback is for handling CB_NOTIFYs.
+ * As notifications from fsnotify come in, encode the relevant notify_*4 in the
+ * ncn_stream, and append a new ncn_notify_array value.
+ *
+ * Periodically, fire off a CB_NOTIFY request to the server. Replace the with
+ * new ones and send the request.
+ */
+struct nfsd4_cb_notify {
+	struct nfsd4_callback		ncn_cb;
+	struct nfsd4_notify_spool	*ncn_gather;
+	struct nfsd4_notify_spool	*ncn_send;
+};
+
 /*
  * Represents a delegation stateid. The nfs4_client holds references to these
  * and they are put when it is being destroyed or when the delegation is
@@ -222,8 +247,12 @@ struct nfs4_delegation {
 	struct nfsd4_callback	dl_recall;
 	bool			dl_recalled;
 
-	/* for CB_GETATTR */
-	struct nfs4_cb_fattr    dl_cb_fattr;
+	union {
+		/* for CB_GETATTR */
+		struct nfs4_cb_fattr    dl_cb_fattr;
+		/* for CB_NOTIFY */
+		struct nfsd4_cb_notify	dl_cb_notify;
+	};
 };
 
 static inline bool deleg_is_read(u32 dl_type)

-- 
2.49.0


  parent reply	other threads:[~2025-06-02 14:03 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-02 14:01 [PATCH RFC v2 00/28] vfs, nfsd, nfs: implement directory delegations Jeff Layton
2025-06-02 14:01 ` [PATCH RFC v2 01/28] filelock: push the S_ISREG check down to ->setlease handlers Jeff Layton
2025-06-02 14:01 ` [PATCH RFC v2 02/28] filelock: add a lm_may_setlease lease_manager callback Jeff Layton
2025-06-02 14:01 ` [PATCH RFC v2 03/28] vfs: add try_break_deleg calls for parents to vfs_{link,rename,unlink} Jeff Layton
2025-06-02 14:01 ` [PATCH RFC v2 04/28] vfs: allow mkdir to wait for delegation break on parent Jeff Layton
2025-06-05 11:19   ` Jan Kara
2025-06-05 11:25     ` Jeff Layton
2025-06-06 10:10       ` Christian Brauner
2025-06-02 14:01 ` [PATCH RFC v2 05/28] vfs: allow rmdir " Jeff Layton
2025-06-02 14:01 ` [PATCH RFC v2 06/28] vfs: break parent dir delegations in open(..., O_CREAT) codepath Jeff Layton
2025-06-02 14:01 ` [PATCH RFC v2 07/28] vfs: make vfs_create break delegations on parent directory Jeff Layton
2025-06-02 14:01 ` [PATCH RFC v2 08/28] vfs: make vfs_mknod " Jeff Layton
2025-06-02 14:01 ` [PATCH RFC v2 09/28] filelock: lift the ban on directory leases in generic_setlease Jeff Layton
2025-06-02 14:01 ` [PATCH RFC v2 10/28] nfsd: allow filecache to hold S_IFDIR files Jeff Layton
2025-06-02 14:01 ` [PATCH RFC v2 11/28] nfsd: allow DELEGRETURN on directories Jeff Layton
2025-06-02 14:01 ` [PATCH RFC v2 12/28] nfsd: check for delegation conflicts vs. the same client Jeff Layton
2025-06-02 14:01 ` [PATCH RFC v2 13/28] nfsd: wire up GET_DIR_DELEGATION handling Jeff Layton
2025-06-02 14:01 ` [PATCH RFC v2 14/28] filelock: rework the __break_lease API to use flags Jeff Layton
2025-06-02 14:01 ` [PATCH RFC v2 15/28] filelock: add struct delegated_inode Jeff Layton
2025-06-02 14:01 ` [PATCH RFC v2 16/28] filelock: add support for ignoring deleg breaks for dir change events Jeff Layton
2025-06-02 14:02 ` [PATCH RFC v2 17/28] filelock: add an inode_lease_ignore_mask helper Jeff Layton
2025-06-02 14:02 ` [PATCH RFC v2 18/28] nfsd: add protocol support for CB_NOTIFY Jeff Layton
2025-06-02 14:02 ` [PATCH RFC v2 19/28] nfsd: add callback encoding and decoding linkages " Jeff Layton
2025-06-02 14:02 ` Jeff Layton [this message]
2025-06-02 14:02 ` [PATCH RFC v2 21/28] fsnotify: export fsnotify_recalc_mask() Jeff Layton
2025-06-03 20:13   ` Jan Kara
2025-06-03 20:17     ` Jeff Layton
2025-06-02 14:02 ` [PATCH RFC v2 22/28] nfsd: update the fsnotify mark when setting or removing a dir delegation Jeff Layton
2025-06-02 14:02 ` [PATCH RFC v2 23/28] nfsd: make nfsd4_callback_ops->prepare operation bool return Jeff Layton
2025-06-02 14:02 ` [PATCH RFC v2 24/28] nfsd: add notification handlers for dir events Jeff Layton
2025-06-02 14:02 ` [PATCH RFC v2 25/28] nfsd: allow nfsd to get a dir lease with an ignore mask Jeff Layton
2025-06-02 14:02 ` [PATCH RFC v2 26/28] nfsd: add a tracepoint for nfsd_file_fsnotify_handle_dir_event() Jeff Layton
2025-06-02 14:02 ` [PATCH RFC v2 27/28] nfsd: add support for NOTIFY4_ADD_ENTRY events Jeff Layton
2025-06-02 14:02 ` [PATCH RFC v2 28/28] nfsd: add support for NOTIFY4_RENAME_ENTRY events Jeff Layton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250602-dir-deleg-v2-20-a7919700de86@kernel.org \
    --to=jlayton@kernel.org \
    --cc=Dai.Ngo@oracle.com \
    --cc=alex.aring@gmail.com \
    --cc=amir73il@gmail.com \
    --cc=anna@kernel.org \
    --cc=bharathsm@microsoft.com \
    --cc=brauner@kernel.org \
    --cc=chuck.lever@oracle.com \
    --cc=corbet@lwn.net \
    --cc=jack@suse.cz \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=miklos@szeredi.hu \
    --cc=neil@brown.name \
    --cc=okorniev@redhat.com \
    --cc=pc@manguebit.com \
    --cc=ronniesahlberg@gmail.com \
    --cc=samba-technical@lists.samba.org \
    --cc=sfrench@samba.org \
    --cc=sprasad@microsoft.com \
    --cc=tom@talpey.com \
    --cc=trondmy@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).