Skip to contentSkip to conversation

ksmbd: refactor smb2_notify() to a blocking wait, matching smb2_lock()

PATCH Superseded Gaël Blivet-Bailly 2 participants 2 messages Jul 24 – Jul 25

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ksmbd: refactor smb2_notify() to a blocking wait, matching smb2_lock()

From: Gael Blivet <[email protected]>

This refactors the design "ksmbd: defer CHANGE_NOTIFY completion instead of STATUS_NOT_IMPLEMENTED" introduced. That patch hand-rolled its own async registration, id lifecycle, and workqueue-deferred response send around a synthetic work struct (in_work) that outlived the calling worker, instead of reusing setup_async_work(), release_async_work(), and smb2_send_interim_resp().

Replace it with the same blocking-wait pattern smb2_lock() already uses for a pending byte-range lock: setup_async_work() on the calling work directly, registered on fp->blocked_works, woken by cancel or by the handle closing via the already-generic set_close_state_blocked_works() (vfs_cache.c). This drops the synthetic work struct, the separate notify_pendings list and its close-time drain, and the deferred workqueue send. It also makes the skeleton compatible with a planned real event-delivery implementation built on the same blocking wait. The tradeoff: the worker now stays blocked on ksmbd_wq for as long as the watch stays open, instead of returning immediately. wait_event_interruptible() is used rather than smb2_lock()'s plain wait_event(), since a watch can stay pending indefinitely where a lock wait normally doesn't.

No real notify event source exists yet, so the wait ends only on a cancel or a close (a signal is treated as a cancel); report STATUS_CANCELLED or STATUS_NOTIFY_CLEANUP accordingly, same contract as before.

Assisted-by Claude:claude-sonnet-5
---
 fs/smb/server/ksmbd_work.c |   1 -
 fs/smb/server/ksmbd_work.h |   3 +-
 fs/smb/server/smb2pdu.c    | 262 +++++++++++--------------------------
 fs/smb/server/vfs_cache.c  |  53 ++------
 fs/smb/server/vfs_cache.h  |   6 -
 5 files changed, 89 insertions(+), 236 deletions(-)
fs/smb/server/ksmbd_work.c +0 -1
index f35335307..9cccccc40 100644
@@ -57,7 +57,6 @@ struct ksmbd_work *ksmbd_alloc_work_struct(void)
 		INIT_LIST_HEAD(&work->request_entry);
 		INIT_LIST_HEAD(&work->async_request_entry);
 		INIT_LIST_HEAD(&work->fp_entry);
-		INIT_LIST_HEAD(&work->notify_entry);
 		INIT_LIST_HEAD(&work->aux_read_list);
 		work->iov_alloc_cnt = ARRAY_SIZE(work->iov_inline);
 		work->iov = work->iov_inline;
fs/smb/server/ksmbd_work.h +1 -2
index 52d0c4dee..12313ee85 100644
@@ -114,9 +114,8 @@ struct ksmbd_work {
 	struct list_head                request_entry;
 	/* List head at conn->async_requests */
 	struct list_head                async_request_entry;
+	/* List head at ksmbd_file->blocked_works */
 	struct list_head                fp_entry;
-	/* List head at ksmbd_file->notify_pendings */
-	struct list_head                notify_entry;
 };
 
 /**
fs/smb/server/smb2pdu.c +80 -182

331 lines not on this page · raw message

fs/smb/server/vfs_cache.c +8 -45

72 lines not on this page · raw message

fs/smb/server/vfs_cache.h +0 -6
index 5fac4b0b4..11bfbb981 100644
@@ -157,12 +157,6 @@ struct ksmbd_file {
 	unsigned int			outstanding_requests;
 	unsigned int			outstanding_pre_requests;
 	struct ksmbd_lock_sequence	lock_seq[KSMBD_LOCK_SEQ_ARRAY_SIZE];
-
-	/*
-	 * Pending CHANGE_NOTIFY completions for this handle, sent with
-	 * STATUS_NOTIFY_CLEANUP when the handle is closed.
-	 */
-	struct list_head		notify_pendings;
 };
 
 static inline void set_ctx_actor(struct dir_context *ctx,

base-commit: e3ebfe6d5045a2365caae7f8cb988b1d087bbbef

Signature · 1 line
2.43.0
reviewer
#1 lore raw

Thanks for your patch.

Quoted 5 lines · Gaël Blivet-Bailly

/**
* smb2_notify() - handler for smb2 notify request
* @work: smb work containing notify command buffer
*
+ * KSMBD does not implement a real change-notification backend yet.

...

Quoted 1 line

+ * wait to be interrupted matters more here.

Are these comments a bit too detailed for the source code? Perhaps it would be better to put them in the commit message.

Quoted 3 lines
fs/smb/server/smb2pdu.c +0 -0
@@ -10969,54 +10939,26 @@ int smb2_notify(struct ksmbd_work *work)
  	if (req->VolatileFileId == SMB2_NO_FID &&
  	    req->PersistentFileId == SMB2_NO_FID) {

...

fs/smb/server/smb2pdu.c int smb2_notify(struct ksmbd_work *work) · line 10956

Quoted 3 lines
  		smb2_send_interim_resp(work, STATUS_PENDING);
fs/smb/server/smb2pdu.c +2 -0
+		rsp->hdr.Status = STATUS_NOTIFY_CLEANUP;
+		smb2_send_interim_resp(work, STATUS_NOTIFY_CLEANUP);

There is no need to send interim responses STATUS_PENDING and STATUS_NOTIFY_CLEANUP, just call smb2_set_err_rsp() with STATUS_INVALID_PARAMETER.

Quoted 1 line
(quoted diff) +0 -0
@@ -11024,96 +10966,52 @@ int smb2_notify(struct ksmbd_work *work)

...

fs/smb/server/smb2pdu.c int smb2_notify(struct ksmbd_work *work) · line 11067

Quoted 1 line
fs/smb/server/smb2pdu.c +0 -1
-	in_work->owns_conn_ref = true;

Please delete owns_conn_ref in struct ksmbd_work.

fs/smb/server/vfs_cache.c static void __ksmbd_close_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp) · line 657

Quoted 10 lines
fs/smb/server/vfs_cache.c +8 -0
@@ -650,50 +649,15 @@ static void __ksmbd_close_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp)
+	 * Any CHANGE_NOTIFY blocked on this handle is woken by
+	 * set_close_state_blocked_works() (via ksmbd_close_fd()/
+	 * ksmbd_mark_fp_closed(), both called before __ksmbd_close_fd() is
+	 * ever reached), the same generic fp->blocked_works mechanism
+	 * smb2_lock() uses for a pending byte-range lock -- nothing further
+	 * to do here. The waiter holds its own reference on fp (from
+	 * ksmbd_lookup_fd_slow()) until it finishes handling the wake and
+	 * calls ksmbd_fd_put(), so fp can't be freed out from under it.
  	 */

These comments can be removed, since a CHANGE_NOTIFY request blocked on this handle can also be woken by an event.

Signature · 3 lines
ChenXiaoSong <[email protected]>
Chinese Homepage: https://chenxiaosong.com
English Homepage: https://chenxiaosong.com/en