[PATCH RFC v2 20/28] nfsd: add data structures for handling CB_NOTIFY to directory delegation
From: Jeff Layton
Date: Mon Jun 02 2025 - 10:10:33 EST
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@xxxxxxxxxx>
---
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