Skip to contentSkip to conversation

smb/server: change notify support

RFC PATCH In review ChenXiaoSong 2 participants 19 messages Jul 23 – Jul 25

Subsystems (2)
lore
Share & tools
lore

ChenXiaoSong

author reviewer
#0 lore raw

cover smb/server: change notify support

From: ChenXiaoSong <[email protected]>

Sorry for taking such a long time to complete the ksmbd change notify feature. Over the past few months, I have been busy with the birth of my daughter and my PhD-related tasks, which left me with limited time to work on it.

Once Gaël updates his patch ("ksmbd: defer CHANGE_NOTIFY completion instead of STATUS_NOT_IMPLEMENTED"), I will rebase my patchset on top of it.

To test this feature, the following patches need to be applied to cifs-utils and kernel smb client:
- cifs-utils: https://chenxiaosong.com/lkml/cifs-utils-parse-notify
- kernel smb client: https://chenxiaosong.com/lkml/cifs-improve-notify

The complete kernel client and server patches are also available at the following link:
https://chenxiaosong.com/repo/linux/ksmbd-change-notify

ChenXiaoSong (11):
smb/server: add debug type for change notify smb/server: validate notify requests
smb/server: support canceling notify requests smb/server: watch directories for changes
smb/server: keep notify watches on file handles smb/server: save simple notify events
smb/server: save old names for rename notify events smb/server: match rename notify events
smb/server: encode notify events
smb/server: send notify events to the client smb/server: break directory leases before sending notify events

 fs/smb/server/Kconfig     |    1 +
 fs/smb/server/Makefile    |    2 +-
 fs/smb/server/glob.h      |    3 +-
 fs/smb/server/notify.c    | 1011 +++++++++++++++++++++++++++++++++++++
 fs/smb/server/notify.h    |   24 +
 fs/smb/server/oplock.c    |   40 ++
 fs/smb/server/oplock.h    |    1 +
 fs/smb/server/server.c    |    2 +-
 fs/smb/server/smb2pdu.c   |   12 +-
 fs/smb/server/vfs_cache.c |    3 +
 fs/smb/server/vfs_cache.h |    4 +
 11 files changed, 1095 insertions(+), 8 deletions(-)
 create mode 100644 fs/smb/server/notify.c
 create mode 100644 fs/smb/server/notify.h
Signature · 1 line
2.54.0
author reviewer
#1 lore raw

RFC PATCH 1/11 smb/server: add debug type for change notify

From: ChenXiaoSong <[email protected]>

Add KSMBD_DEBUG_NOTIFY and expose it as "notify" to print logs for the change notify feature.

---
 fs/smb/server/glob.h   | 3 ++-
 fs/smb/server/server.c | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)
fs/smb/server/glob.h +2 -1
index 344b069cfbbd..87cbc88275fe 100644
@@ -22,10 +22,11 @@ extern int ksmbd_debug_types;
 #define KSMBD_DEBUG_IPC         BIT(4)
 #define KSMBD_DEBUG_CONN        BIT(5)
 #define KSMBD_DEBUG_RDMA        BIT(6)
+#define KSMBD_DEBUG_NOTIFY      BIT(7)
 #define KSMBD_DEBUG_ALL         (KSMBD_DEBUG_SMB | KSMBD_DEBUG_AUTH |	\
 				KSMBD_DEBUG_VFS | KSMBD_DEBUG_OPLOCK |	\
 				KSMBD_DEBUG_IPC | KSMBD_DEBUG_CONN |	\
-				KSMBD_DEBUG_RDMA)
+				KSMBD_DEBUG_RDMA | KSMBD_DEBUG_NOTIFY)
 
 #ifdef pr_fmt
 #undef pr_fmt
fs/smb/server/server.c +1 -1
index 5b38406129b5..4bea3ac8011b 100644
@@ -522,7 +522,7 @@ static ssize_t kill_server_store(const struct class *class,
 
 static const char * const debug_type_strings[] = {"smb", "auth", "vfs",
 						  "oplock", "ipc", "conn",
-						  "rdma"};
+						  "rdma", "notify"};
 
 static ssize_t debug_show(const struct class *class, const struct class_attribute *attr,
 			  char *buf)
Signature · 1 line
2.54.0
author reviewer
#2 lore raw

RFC PATCH 2/11 smb/server: validate notify requests

In reply to ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Validate the file ID, directory type, access rights and output buffer length before handling a change notify request.

---
 fs/smb/server/Makefile  |   2 +-
 fs/smb/server/notify.c  | 108 ++++++++++++++++++++++++++++++++++++++++
 fs/smb/server/notify.h  |  22 ++++++++
 fs/smb/server/smb2pdu.c |  12 +++--
 4 files changed, 138 insertions(+), 6 deletions(-)
 create mode 100644 fs/smb/server/notify.c
 create mode 100644 fs/smb/server/notify.h
fs/smb/server/Makefile +1 -1
index a3e9306055e8..51d8972a0942 100644
@@ -9,7 +9,7 @@ ksmbd-y :=	unicode.o auth.o vfs.o vfs_cache.o server.o ndr.o \
 		mgmt/ksmbd_ida.o mgmt/user_config.o mgmt/share_config.o \
 		mgmt/tree_connect.o mgmt/user_session.o smb_common.o \
 		transport_tcp.o transport_ipc.o smbacl.o smb2pdu.o \
-		smb2ops.o smb2misc.o ksmbd_spnego_negtokeninit.asn1.o \
+		smb2ops.o smb2misc.o notify.o ksmbd_spnego_negtokeninit.asn1.o \
 		ksmbd_spnego_negtokentarg.asn1.o asn1.o compress.o
 
 $(obj)/asn1.o: $(obj)/ksmbd_spnego_negtokeninit.asn1.h $(obj)/ksmbd_spnego_negtokentarg.asn1.h
fs/smb/server/notify.c new file +108 -0

108 lines not on this page · raw message

fs/smb/server/notify.h new file +22 -0
new file mode 100644
index 000000000000..3118ad09c841
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ *
+ *   SMB2 CHANGE_NOTIFY
+ *
+ *   Copyright (C) 2026 KylinSoft Co., Ltd. All rights reserved.
+ *   Author(s): ChenXiaoSong <[email protected]>
+ *
+ */
+
+#ifndef __SMB_SERVER_NOTIFY_H__
+#define __SMB_SERVER_NOTIFY_H__
+
+struct ksmbd_work;
+struct smb2_change_notify_req;
+struct smb2_change_notify_rsp;
+
+int ksmbd_handle_notify(struct ksmbd_work *work,
+			struct smb2_change_notify_req *req,
+			struct smb2_change_notify_rsp *rsp);
+
+#endif /* __SMB_SERVER_NOTIFY_H__ */
fs/smb/server/smb2pdu.c +7 -5
index 0680222a8da4..fe1b6c9f71c5 100644
@@ -29,6 +29,7 @@
 #include "vfs.h"
 #include "vfs_cache.h"
 #include "misc.h"
+#include "notify.h"
 
 #include "server.h"
 #include "smb_common.h"
@@ -10860,22 +10861,23 @@ int smb2_notify(struct ksmbd_work *work)
 	struct smb2_change_notify_req *req;
 	struct smb2_change_notify_rsp *rsp;
 
-	ksmbd_debug(SMB, "Received smb2 notify\n");
+	ksmbd_debug(NOTIFY, "Received smb2 notify\n");
 
 	WORK_BUFFERS(work, req, rsp);
 
-	if (smb2_compound_has_failed(work, &rsp->hdr))
+	if (smb2_compound_has_failed(work, &rsp->hdr)) {
+		pr_err("Failed compound notify request\n");
 		return -EACCES;
+	}
 
 	if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) {
+		pr_err("Notify request is not the last compound command\n");
 		rsp->hdr.Status = STATUS_INTERNAL_ERROR;
 		smb2_set_err_rsp(work);
 		return -EIO;
 	}
 
-	smb2_set_err_rsp(work);
-	rsp->hdr.Status = STATUS_NOT_IMPLEMENTED;
-	return -EOPNOTSUPP;
+	return ksmbd_handle_notify(work, req, rsp);
 }
 
 /**
Signature · 1 line
2.54.0
maintainer
#3 lore raw

Re: [RFC PATCH 02/11] smb/server: validate notify requests

fs/smb/server/notify.c line 46

Quoted 4 lines
fs/smb/server/notify.c new file +4 -0
+       if (!S_ISDIR(file_inode(fp->filp)->i_mode)) {
+               pr_err("Notify file id is not a directory, fid %llu:%llu\n",
+                      fp->persistent_id, fp->volatile_id);
+               rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;

CHANGE_NOTIFY request on a non-directory open to fail with STATUS_INVALID_PARAMETER. Please check [MS-SMB2] section 3.3.5.19.

Quoted 3 lines
fs/smb/server/notify.c new file +3 -0
+               err = -ENOTDIR;
+               goto err_put_fp;
+       }
author reviewer
#4 lore raw

RFC PATCH 3/11 smb/server: support canceling notify requests

In reply to ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Register CHANGE_NOTIFY requests as asynchronous work so SMB2 CANCEL requests can wake and complete them with STATUS_CANCELLED.

Example:

1. smbinfo notify /mnt
2. Then press Ctrl+C to exit smbinfo notify command.
3. The following packets can be captured using tcpdump:
- Notify Request, File: <share>
- Notify Response, Error: STATUS_PENDING
- Cancel Request
- Notify Response, Error: STATUS_CANCELLED

---
 fs/smb/server/notify.c | 103 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 99 insertions(+), 4 deletions(-)
fs/smb/server/notify.c +99 -4
index ad812fde5879..d1b971a75037 100644
@@ -8,6 +8,9 @@
  *
  */
 
+#include <linux/slab.h>
+#include <linux/wait.h>
+
 #include "glob.h"
 #include "../common/smb2status.h"
 #include "connection.h"
@@ -17,6 +20,18 @@
 #include "smb2pdu.h"
 #include "vfs_cache.h"
 
+struct ksmbd_notify_req {
+	wait_queue_head_t wait;
+};
+
+static void smb2_notify_cancel(void **argv)
+{
+	struct ksmbd_notify_req *notify_req = argv[0];
+
+	ksmbd_debug(NOTIFY, "Wake pending notify request\n");
+	wake_up(&notify_req->wait);
+}
+
 static struct ksmbd_file *
 ksmbd_notify_validate_req(struct ksmbd_work *work,
 			  struct smb2_change_notify_req *req,
@@ -73,6 +88,36 @@ ksmbd_notify_validate_req(struct ksmbd_work *work,
 	return ERR_PTR(err);
 }
 
+static int ksmbd_notify_wait(struct ksmbd_work *work,
+			     struct ksmbd_file *fp,
+			     struct ksmbd_notify_req *notify_req)
+{
+	int err;
+
+	spin_lock(&fp->f_lock);
+	list_add_tail(&work->fp_entry, &fp->blocked_works);
+	spin_unlock(&fp->f_lock);
+
+	ksmbd_debug(NOTIFY, "Notify request pending, async id %d\n",
+		    work->async_id);
+	smb2_send_interim_resp(work, STATUS_PENDING);
+
+	err = wait_event_interruptible(notify_req->wait,
+				       READ_ONCE(work->state) !=
+					       KSMBD_WORK_ACTIVE);
+	if (err && READ_ONCE(work->state) == KSMBD_WORK_ACTIVE) {
+		pr_err("Notify wait interrupted, async id %d: %d\n",
+		       work->async_id, err);
+		WRITE_ONCE(work->state, KSMBD_WORK_CANCELLED);
+	}
+
+	spin_lock(&fp->f_lock);
+	list_del_init(&work->fp_entry);
+	spin_unlock(&fp->f_lock);
+
+	return err;
+}
+
 /**
  * ksmbd_handle_notify() - handle an SMB2 change notify request
  * @work: smb work containing notify command buffer
@@ -85,24 +130,74 @@ int ksmbd_handle_notify(struct ksmbd_work *work,
 			struct smb2_change_notify_req *req,
 			struct smb2_change_notify_rsp *rsp)
 {
+	struct ksmbd_notify_req *notify_req = NULL;
 	struct ksmbd_file *fp = NULL;
+	void **argv = NULL;
+	bool async_work = false;
 	int err = 0;
 
 	fp = ksmbd_notify_validate_req(work, req, rsp);
 	if (IS_ERR(fp)) {
 		err = PTR_ERR(fp);
+		fp = NULL;
 		goto out;
 	}
 
-	ksmbd_fd_put(work, fp);
-	rsp->hdr.Status = STATUS_NOT_IMPLEMENTED;
-	err = -EOPNOTSUPP;
+	notify_req = kzalloc_obj(*notify_req, KSMBD_DEFAULT_GFP);
+	if (!notify_req) {
+		pr_err("Failed to allocate notify request\n");
+		rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
+		err = -ENOMEM;
+		goto out;
+	}
+
+	argv = kmalloc_obj(*argv, KSMBD_DEFAULT_GFP);
+	if (!argv) {
+		pr_err("Failed to allocate notify cancel arguments\n");
+		rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
+		err = -ENOMEM;
+		goto out;
+	}
+
+	init_waitqueue_head(&notify_req->wait);
+	argv[0] = notify_req;
+	err = setup_async_work(work, smb2_notify_cancel, argv);
+	if (err) {
+		pr_err("Failed to set up asynchronous notify work: %d\n", err);
+		rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
+		goto out;
+	}
+	async_work = true;
+
+	err = ksmbd_notify_wait(work, fp, notify_req);
+
+	if (work->state == KSMBD_WORK_CANCELLED) {
+		ksmbd_debug(NOTIFY, "Notify request cancelled, async id %d\n",
+			    work->async_id);
+		rsp->hdr.Status = STATUS_CANCELLED;
+		smb2_send_interim_resp(work, STATUS_CANCELLED);
+		work->send_no_response = 1;
+	} else if (work->state == KSMBD_WORK_CLOSED) {
+		ksmbd_debug(NOTIFY, "Notify handle closed, async id %d\n",
+			    work->async_id);
+		rsp->hdr.Status = STATUS_NOTIFY_CLEANUP;
+		smb2_send_interim_resp(work, STATUS_NOTIFY_CLEANUP);
+		work->send_no_response = 1;
+	}
 
 out:
 	if (err)
 		pr_err("Failed to handle notify request: %d, status: 0x%x\n",
 		       err, le32_to_cpu(rsp->hdr.Status));
-	if (rsp->hdr.Status != STATUS_SUCCESS)
+	if (rsp->hdr.Status != STATUS_SUCCESS && !work->send_no_response)
 		smb2_set_err_rsp(work);
+	if (async_work)
+		release_async_work(work);
+	else
+		kfree(argv);
+	if (notify_req)
+		kfree(notify_req);
+	if (fp)
+		ksmbd_fd_put(work, fp);
 	return err;
 }
Signature · 1 line
2.54.0
author reviewer
#5 lore raw

RFC PATCH 4/11 smb/server: watch directories for changes

In reply to ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Use fsnotify to watch a directory for changes. Add one watch for each SMB2 CHANGE_NOTIFY request.

Example:

1. client: smbinfo notify /mnt
2. server: ksmbd.control --debug=notify
3. server: touch /export/file
4. server debug log:

     ksmbd: fid 4:4, notify event: mask=0x00000100 inode=2 name=file cookie=0
     ksmbd: fid 4:4, notify event: mask=0x08000004 inode=2 name=file cookie=0
---
 fs/smb/server/Kconfig  |   1 +
 fs/smb/server/notify.c | 228 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 227 insertions(+), 2 deletions(-)
fs/smb/server/Kconfig +1 -0
index 08d8b7a965a6..56f8902ec355 100644
@@ -20,6 +20,7 @@ config SMB_SERVER
 	select ASN1
 	select OID_REGISTRY
 	select CRC32
+	select FSNOTIFY
 	default n
 	help
 	  Choose Y here if you want to allow SMB3 compliant clients
fs/smb/server/notify.c +226 -2

283 lines not on this page · raw message

Signature · 1 line
2.54.0
maintainer
#6 lore raw

Re: [RFC PATCH 04/11] smb/server: watch directories for changes

Quoted 14 lines
fs/smb/server/notify.c +14 -0
+static struct fsnotify_mark *
+ksmbd_notify_add_mark(struct ksmbd_notify *notify, u32 mask,
+                     struct fsnotify_group **group)
+{
+       struct ksmbd_notify_mark *notify_mark;
+       int err;
+
+       *group = fsnotify_alloc_group(&ksmbd_notify_fsnotify_ops, 0);
+       if (IS_ERR(*group)) {
+               pr_err("Failed to allocate fsnotify group: %ld\n",
+                      PTR_ERR(*group));
+               return ERR_CAST(*group);
+       }
+

fs/smb/server/notify.c static void smb2_notify_cancel(void **argv) · line 149

Quoted 10 lines
fs/smb/server/notify.c +10 -0
+       notify_mark = kzalloc_obj(*notify_mark, KSMBD_DEFAULT_GFP);
+       if (!notify_mark) {
+               pr_err("Failed to allocate fsnotify mark\n");
+               err = -ENOMEM;
+               goto err_put_group;
+       }
+
+       notify_mark->notify = notify;
+       fsnotify_init_mark(&notify_mark->mark, *group);
+       notify_mark->mark.mask = mask | FS_EVENT_ON_CHILD;

Where is SMB2_WATCH_TREE handled?
FS_EVENT_ON_CHILD only reports events for immediate children of this inode. it does not recursively monitor descendants.

Quoted 9 lines

static struct ksmbd_file * ksmbd_notify_validate_req(struct ksmbd_work *work,
struct smb2_change_notify_req *req,

fs/smb/server/notify.c +2 -0
@@ -88,10 +270,40 @@ ksmbd_notify_validate_req(struct ksmbd_work *work,
        return ERR_PTR(err);
 }
 
+static struct ksmbd_notify *
+ksmbd_notify_setup_watch(struct ksmbd_file *fp,

fs/smb/server/notify.c ksmbd_notify_validate_req(struct ksmbd_work *work, · line 284

Quoted 10 lines
fs/smb/server/notify.c +10 -0
+                        struct smb2_change_notify_req *req,
+                        struct smb2_change_notify_rsp *rsp)
+{
+       struct ksmbd_notify *notify;
+       u32 filter, mask;
+       int err;
+
+       filter = le32_to_cpu(req->CompletionFilter);
+       mask = ksmbd_notify_map(filter);
+       if (!mask) {

Please do not reject a CompletionFilter merely because no currently mapped bit remains.
If no valid bits remain, the request must stay pending until it is cancelled or the directory handle is closed.
Also, the mapping currently omits valid filter bits such as FILE_NOTIFY_CHANGE_SIZE, FILE_NOTIFY_CHANGE_CREATION, and the stream-related filters. Those need either implementation or explicitly correct handling according to the protocol requirements.

author reviewer
#7 lore raw

RFC PATCH 5/11 smb/server: keep notify watches on file handles

In reply to ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Keep one notify watch on each file handle. Reuse it for later requests, and remove it when the handle is closed.

---
 fs/smb/server/notify.c    | 47 +++++++++++++++++++++++++++++++++++----
 fs/smb/server/notify.h    |  2 ++
 fs/smb/server/vfs_cache.c |  3 +++
 fs/smb/server/vfs_cache.h |  4 ++++
 4 files changed, 52 insertions(+), 4 deletions(-)
fs/smb/server/notify.c +43 -4
index 1c020752a8ed..82de53963dc0 100644
@@ -182,6 +182,21 @@ static int ksmbd_notify_add(struct ksmbd_file *fp, u32 mask, u32 filter,
 	struct fsnotify_mark *mark;
 	int err = 0;
 
+	mutex_lock(&fp->notify_lock);
+	if (fp->notify) {
+		spin_lock(&fp->notify->lock);
+		fp->notify->filter |= filter;
+		fp->notify->mask |= mask;
+		spin_unlock(&fp->notify->lock);
+		fsnotify_modify_mark_mask(fp->notify->mark, mask, 0);
+		ksmbd_debug(NOTIFY,
+			    "Updated fsnotify mark, inode %llu, mask 0x%x\n",
+			    (unsigned long long)file_inode(fp->filp)->i_ino,
+			    fp->notify->mark->mask);
+		*notify_out = fp->notify;
+		goto out;
+	}
+
 	notify = kzalloc_obj(*notify, KSMBD_DEFAULT_GFP);
 	if (!notify) {
 		pr_err("Failed to allocate notify watch\n");
@@ -203,6 +218,7 @@ static int ksmbd_notify_add(struct ksmbd_file *fp, u32 mask, u32 filter,
 	}
 
 	notify->mark = mark;
+	fp->notify = notify;
 	*notify_out = notify;
 	ksmbd_debug(NOTIFY,
 		    "Added fsnotify mark, inode %llu, mask 0x%x, filter 0x%x, "
@@ -211,9 +227,36 @@ static int ksmbd_notify_add(struct ksmbd_file *fp, u32 mask, u32 filter,
 		    filter, max_buffer_size);
 
 out:
+	mutex_unlock(&fp->notify_lock);
 	return err;
 }
 
+/**
+ * ksmbd_notify_remove() - remove the notify watch for a closing handle
+ * @fp: file handle whose watch is being removed
+ *
+ * A cancelled CHANGE_NOTIFY request leaves this watch installed. The watch is
+ * owned by @fp and removed only when the file handle is finally closed.
+ */
+void ksmbd_notify_remove(struct ksmbd_file *fp)
+{
+	struct ksmbd_notify *notify;
+
+	mutex_lock(&fp->notify_lock);
+	notify = fp->notify;
+	fp->notify = NULL;
+	mutex_unlock(&fp->notify_lock);
+	if (!notify)
+		return;
+
+	ksmbd_debug(NOTIFY,
+		    "Removing fsnotify mark, inode %llu, mask 0x%x\n",
+		    (unsigned long long)file_inode(fp->filp)->i_ino,
+		    notify->mark->mask);
+	ksmbd_notify_destroy_mark(notify->group, notify->mark);
+	kfree(notify);
+}
+
 static struct ksmbd_file *
 ksmbd_notify_validate_req(struct ksmbd_work *work,
 			  struct smb2_change_notify_req *req,
@@ -417,10 +460,6 @@ int ksmbd_handle_notify(struct ksmbd_work *work,
 		kfree(argv);
 	if (notify_req)
 		kfree(notify_req);
-	if (notify) {
-		ksmbd_notify_destroy_mark(notify->group, notify->mark);
-		kfree(notify);
-	}
 	if (fp)
 		ksmbd_fd_put(work, fp);
 	return err;
fs/smb/server/notify.h +2 -0
index 3118ad09c841..9d022ba9090f 100644
@@ -12,11 +12,13 @@
 #define __SMB_SERVER_NOTIFY_H__
 
 struct ksmbd_work;
+struct ksmbd_file;
 struct smb2_change_notify_req;
 struct smb2_change_notify_rsp;
 
 int ksmbd_handle_notify(struct ksmbd_work *work,
 			struct smb2_change_notify_req *req,
 			struct smb2_change_notify_rsp *rsp);
+void ksmbd_notify_remove(struct ksmbd_file *fp);
 
 #endif /* __SMB_SERVER_NOTIFY_H__ */
fs/smb/server/vfs_cache.c +3 -0
index d0fd954e9ec8..64bef7ce5fd2 100644
@@ -18,6 +18,7 @@
 #include "vfs.h"
 #include "connection.h"
 #include "misc.h"
+#include "notify.h"
 #include "mgmt/tree_connect.h"
 #include "mgmt/user_session.h"
 #include "mgmt/user_config.h"
@@ -625,6 +626,7 @@ static void __ksmbd_close_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp)
 	close_id_del_oplock(fp);
 	filp = fp->filp;
 
+	ksmbd_notify_remove(fp);
 	__ksmbd_inode_close(fp);
 	if (!IS_ERR_OR_NULL(filp))
 		fput(filp);
@@ -1175,6 +1177,7 @@ struct ksmbd_file *ksmbd_open_fd(struct ksmbd_work *work, struct file *filp)
 	INIT_LIST_HEAD(&fp->node);
 	INIT_LIST_HEAD(&fp->lock_list);
 	spin_lock_init(&fp->f_lock);
+	mutex_init(&fp->notify_lock);
 	mutex_init(&fp->readdir_lock);
 	atomic_set(&fp->refcount, 1);
 
fs/smb/server/vfs_cache.h +4 -0
index 11bfbb981004..aff7db9127e0 100644
@@ -33,6 +33,7 @@
 #define SMB2_NO_FID		(0xFFFFFFFFFFFFFFFFULL)
 
 struct ksmbd_conn;
+struct ksmbd_notify;
 struct ksmbd_session;
 
 struct ksmbd_lock {
@@ -96,6 +97,9 @@ struct ksmbd_file {
 	u64				durable_volatile_id;
 
 	spinlock_t			f_lock;
+	/* Protects notify watch creation and removal. */
+	struct mutex			notify_lock;
+	struct ksmbd_notify		*notify;
 
 	struct ksmbd_inode		*f_ci;
 	struct ksmbd_inode		*f_parent_ci;
Signature · 1 line
2.54.0
maintainer
#8 lore raw

Re: [RFC PATCH 05/11] smb/server: keep notify watches on file handles

fs/smb/server/notify.c static int ksmbd_notify_add(struct ksmbd_file *fp, u32 mask, u32 filter, · line 189

Quoted 9 lines
fs/smb/server/notify.c +5 -0
@@ -182,6 +182,21 @@ static int ksmbd_notify_add(struct ksmbd_file *fp, u32 mask, u32 filter,
        struct fsnotify_mark *mark;
        int err = 0;
 
+       mutex_lock(&fp->notify_lock);
+       if (fp->notify) {
+               spin_lock(&fp->notify->lock);
+               fp->notify->filter |= filter;
+               fp->notify->mask |= mask;

This changes the watch semantics for later CHANGE_NOTIFY requests by unioning their filter into the existing watch. CHANGE_NOTIFY request is queued per open, but it must not reconfigure the underlying directory watch.

Quoted 10 lines
fs/smb/server/notify.c +10 -0
+               spin_unlock(&fp->notify->lock);
+               fsnotify_modify_mark_mask(fp->notify->mark, mask, 0);
+               ksmbd_debug(NOTIFY,
+                           "Updated fsnotify mark, inode %llu, mask 0x%x\n",
+                           (unsigned long long)file_inode(fp->filp)->i_ino,
+                           fp->notify->mark->mask);
+               *notify_out = fp->notify;
+               goto out;
+       }
+
author reviewer
#9 lore raw

RFC PATCH 6/11 smb/server: save simple notify events

In reply to ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Save create, delete, and change events.

Example:

1. client: smbinfo notify /mnt
2. server: ksmbd.control --debug=notify
3. server: touch /export/file
server debug log:
ksmbd: Queueing notify event, action 1, name file
ksmbd: Queueing notify event, action 3, name file
4. server: rm /export/file
server debug log:
ksmbd: Queueing notify event, action 2, name file

---
 fs/smb/server/notify.c | 106 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 106 insertions(+)
fs/smb/server/notify.c +106 -0
index 82de53963dc0..a873625555ea 100644
@@ -9,6 +9,7 @@
  */
 
 #include <linux/fsnotify_backend.h>
+#include <linux/ktime.h>
 #include <linux/slab.h>
 #include <linux/wait.h>
 
@@ -26,12 +27,22 @@ struct ksmbd_notify_mark {
 	struct ksmbd_notify *notify;
 };
 
+struct ksmbd_notify_event {
+	struct list_head list;
+	u32 action;
+	u64 when;
+	size_t name_len;
+	char name[];
+};
+
 struct ksmbd_notify {
 	struct fsnotify_group *group;
 	struct fsnotify_mark *mark;
 	struct ksmbd_file *fp;
 	/* Protects filter, rename state and the queued events. */
 	spinlock_t lock;
+	struct list_head events;
+	unsigned int num_events;
 	u32 filter;
 	u32 mask;
 	u32 max_buffer_size;
@@ -43,6 +54,8 @@ struct ksmbd_notify_req {
 
 #define KSMBD_NOTIFY_NAME_EVENT_MASK	(FS_CREATE | FS_DELETE | \
 					 FS_MOVED_FROM | FS_MOVED_TO)
+#define KSMBD_NOTIFY_EVENT_MASK		(FS_ATTRIB | FS_MODIFY | \
+					 KSMBD_NOTIFY_NAME_EVENT_MASK)
 
 static const struct {
 	u32 notify_mask;
@@ -77,6 +90,34 @@ static u32 ksmbd_notify_map(u32 filter)
 	return mask;
 }
 
+static void ksmbd_notify_free_events(struct list_head *events)
+{
+	struct ksmbd_notify_event *event, *tmp;
+
+	list_for_each_entry_safe(event, tmp, events, list) {
+		list_del(&event->list);
+		kfree(event);
+	}
+}
+
+static bool ksmbd_notify_filter_match(struct ksmbd_notify *notify, u32 mask)
+{
+	u32 filter, notify_mask;
+
+	spin_lock(&notify->lock);
+	filter = notify->filter;
+	notify_mask = notify->mask;
+	spin_unlock(&notify->lock);
+
+	if (mask & KSMBD_NOTIFY_NAME_EVENT_MASK) {
+		if (mask & FS_ISDIR)
+			return filter & FILE_NOTIFY_CHANGE_DIR_NAME;
+		return filter & FILE_NOTIFY_CHANGE_FILE_NAME;
+	}
+
+	return mask & notify_mask;
+}
+
 static void smb2_notify_cancel(void **argv)
 {
 	struct ksmbd_notify_req *notify_req = argv[0];
@@ -85,6 +126,44 @@ static void smb2_notify_cancel(void **argv)
 	wake_up(&notify_req->wait);
 }
 
+static struct ksmbd_notify_event *
+ksmbd_notify_alloc_event(u32 action, const struct qstr *file_name, gfp_t gfp)
+{
+	struct ksmbd_notify_event *event;
+	size_t name_len = file_name ? file_name->len : 0;
+
+	event = kmalloc(sizeof(*event) + name_len + 1, gfp);
+	if (!event) {
+		pr_err("Failed to allocate notify event, action %u, name %.*s\n",
+		       action, file_name ? file_name->len : 0,
+		       file_name ? (const char *)file_name->name : "");
+		return NULL;
+	}
+
+	INIT_LIST_HEAD(&event->list);
+	event->action = action;
+	event->when = ktime_get_ns();
+	event->name_len = name_len;
+	if (name_len)
+		memcpy(event->name, file_name->name, name_len);
+	event->name[name_len] = '\0';
+
+	return event;
+}
+
+static void
+ksmbd_notify_queue_event(struct ksmbd_notify *notify,
+			 struct ksmbd_notify_event *event)
+{
+	ksmbd_debug(NOTIFY, "Queueing notify event, action %u, name %s\n",
+		    event->action, event->name);
+
+	spin_lock(&notify->lock);
+	list_add_tail(&event->list, &notify->events);
+	notify->num_events++;
+	spin_unlock(&notify->lock);
+}
+
 static int ksmbd_notify_handle_inode_event(struct fsnotify_mark *mark,
 					   u32 mask, struct inode *inode,
 					   struct inode *dir,
@@ -92,9 +171,11 @@ static int ksmbd_notify_handle_inode_event(struct fsnotify_mark *mark,
 					   u32 cookie)
 {
 	struct ksmbd_notify_mark *notify_mark;
+	struct ksmbd_notify_event *event;
 	struct ksmbd_notify *notify;
 	struct inode *event_inode = dir ?: inode;
 	struct ksmbd_file *fp;
+	u32 action;
 
 	notify_mark = container_of(mark, struct ksmbd_notify_mark, mark);
 	notify = notify_mark->notify;
@@ -107,6 +188,29 @@ static int ksmbd_notify_handle_inode_event(struct fsnotify_mark *mark,
 		    file_name ? file_name->len : 0,
 		    file_name ? (const char *)file_name->name : "", cookie);
 
+	if (!(mask & KSMBD_NOTIFY_EVENT_MASK)) {
+		ksmbd_debug(NOTIFY, "Ignored notify event mask 0x%x\n", mask);
+		return 0;
+	}
+
+	if (mask & FS_CREATE) {
+		action = FILE_ACTION_ADDED;
+	} else if (mask & FS_DELETE) {
+		action = FILE_ACTION_REMOVED;
+	} else {
+		action = FILE_ACTION_MODIFIED;
+	}
+
+	if (!ksmbd_notify_filter_match(notify, mask))
+		return 0;
+
+	event = ksmbd_notify_alloc_event(action, file_name,
+					 KSMBD_DEFAULT_GFP);
+	if (!event)
+		return 0;
+
+	ksmbd_notify_queue_event(notify, event);
+
 	return 0;
 }
 
@@ -206,6 +310,7 @@ static int ksmbd_notify_add(struct ksmbd_file *fp, u32 mask, u32 filter,
 
 	notify->fp = fp;
 	spin_lock_init(&notify->lock);
+	INIT_LIST_HEAD(&notify->events);
 	notify->filter = filter;
 	notify->mask = mask;
 	notify->max_buffer_size = max_buffer_size;
@@ -254,6 +359,7 @@ void ksmbd_notify_remove(struct ksmbd_file *fp)
 		    (unsigned long long)file_inode(fp->filp)->i_ino,
 		    notify->mark->mask);
 	ksmbd_notify_destroy_mark(notify->group, notify->mark);
+	ksmbd_notify_free_events(&notify->events);
 	kfree(notify);
 }
 
Signature · 1 line
2.54.0
author reviewer
#10 lore raw

RFC PATCH 7/11 smb/server: save old names for rename notify events

In reply to ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Save the old name from FS_MOVED_FROM events for matching with a subsequent FS_MOVED_TO event. Queue unmatched events as removals after a short delay.

Example:

1. client: smbinfo notify /mnt
2. server: ksmbd.control --debug=notify
3. server: mkdir /export/dir/; touch /export/file
4. server: mv /export/file /export/dir/

     server debug log:
       [40622.865858] ksmbd: Saved moved from, mask 0x40, name file, cookie 1134
       [40622.975906] ksmbd: Queue moved from as removed, name file
---
 fs/smb/server/notify.c | 78 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)
fs/smb/server/notify.c +78 -0
index a873625555ea..80c1eb089b60 100644
@@ -9,9 +9,11 @@
  */
 
 #include <linux/fsnotify_backend.h>
+#include <linux/jiffies.h>
 #include <linux/ktime.h>
 #include <linux/slab.h>
 #include <linux/wait.h>
+#include <linux/workqueue.h>
 
 #include "glob.h"
 #include "../common/smb2status.h"
@@ -46,12 +48,17 @@ struct ksmbd_notify {
 	u32 filter;
 	u32 mask;
 	u32 max_buffer_size;
+	struct ksmbd_notify_event *moved_from_event;
+	u32 moved_from_mask;
+	u32 moved_from_cookie;
+	struct delayed_work moved_from_work;
 };
 
 struct ksmbd_notify_req {
 	wait_queue_head_t wait;
 };
 
+#define KSMBD_NOTIFY_MOVED_FROM_MSECS	100
 #define KSMBD_NOTIFY_NAME_EVENT_MASK	(FS_CREATE | FS_DELETE | \
 					 FS_MOVED_FROM | FS_MOVED_TO)
 #define KSMBD_NOTIFY_EVENT_MASK		(FS_ATTRIB | FS_MODIFY | \
@@ -164,6 +171,65 @@ ksmbd_notify_queue_event(struct ksmbd_notify *notify,
 	spin_unlock(&notify->lock);
 }
 
+static void
+ksmbd_notify_trigger_removed(struct ksmbd_notify *notify,
+			     struct ksmbd_notify_event *event)
+{
+	ksmbd_debug(NOTIFY,
+		    "Queue moved from as removed, name %s\n",
+		    event->name);
+	event->action = FILE_ACTION_REMOVED;
+	event->when = ktime_get_ns();
+	ksmbd_notify_queue_event(notify, event);
+}
+
+static void ksmbd_notify_moved_from_timeout(struct work_struct *work)
+{
+	struct ksmbd_notify_event *event;
+	struct ksmbd_notify *notify;
+
+	notify = container_of(to_delayed_work(work), struct ksmbd_notify,
+			      moved_from_work);
+
+	spin_lock(&notify->lock);
+	event = notify->moved_from_event;
+	notify->moved_from_event = NULL;
+	spin_unlock(&notify->lock);
+
+	if (!event)
+		return;
+	ksmbd_notify_trigger_removed(notify, event);
+}
+
+static void ksmbd_notify_save_moved_from(struct ksmbd_notify *notify,
+					 u32 mask, u32 cookie,
+					 const struct qstr *file_name)
+{
+	struct ksmbd_notify_event *event, *old_event;
+
+	event = ksmbd_notify_alloc_event(FILE_ACTION_RENAMED_OLD_NAME,
+					 file_name, KSMBD_DEFAULT_GFP);
+	if (!event)
+		return;
+
+	spin_lock(&notify->lock);
+	old_event = notify->moved_from_event;
+	notify->moved_from_event = event;
+	notify->moved_from_mask = mask;
+	notify->moved_from_cookie = cookie;
+	spin_unlock(&notify->lock);
+
+	ksmbd_debug(NOTIFY,
+		    "Saved moved from, mask 0x%x, name %.*s, cookie %u\n",
+		    mask, file_name ? file_name->len : 0,
+		    file_name ? (const char *)file_name->name : "", cookie);
+	if (old_event)
+		ksmbd_notify_trigger_removed(notify, old_event);
+
+	mod_delayed_work(system_dfl_wq, &notify->moved_from_work,
+			 msecs_to_jiffies(KSMBD_NOTIFY_MOVED_FROM_MSECS));
+}
+
 static int ksmbd_notify_handle_inode_event(struct fsnotify_mark *mark,
 					   u32 mask, struct inode *inode,
 					   struct inode *dir,
@@ -193,6 +259,14 @@ static int ksmbd_notify_handle_inode_event(struct fsnotify_mark *mark,
 		return 0;
 	}
 
+	if (mask & FS_MOVED_FROM) {
+		ksmbd_notify_save_moved_from(notify, mask, cookie, file_name);
+		return 0;
+	}
+
+	if (mask & FS_MOVED_TO)
+		return 0;
+
 	if (mask & FS_CREATE) {
 		action = FILE_ACTION_ADDED;
 	} else if (mask & FS_DELETE) {
@@ -314,6 +388,8 @@ static int ksmbd_notify_add(struct ksmbd_file *fp, u32 mask, u32 filter,
 	notify->filter = filter;
 	notify->mask = mask;
 	notify->max_buffer_size = max_buffer_size;
+	INIT_DELAYED_WORK(&notify->moved_from_work,
+			  ksmbd_notify_moved_from_timeout);
 
 	mark = ksmbd_notify_add_mark(notify, mask, &notify->group);
 	if (IS_ERR(mark)) {
@@ -359,6 +435,8 @@ void ksmbd_notify_remove(struct ksmbd_file *fp)
 		    (unsigned long long)file_inode(fp->filp)->i_ino,
 		    notify->mark->mask);
 	ksmbd_notify_destroy_mark(notify->group, notify->mark);
+	cancel_delayed_work_sync(&notify->moved_from_work);
+	kfree(notify->moved_from_event);
 	ksmbd_notify_free_events(&notify->events);
 	kfree(notify);
 }
Signature · 1 line
2.54.0
author reviewer
#11 lore raw

RFC PATCH 8/11 smb/server: match rename notify events

In reply to ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Match old and new rename names by cookie. Save both names as events.

Example:

1. client: smbinfo notify /mnt
2. server: ksmbd.control --debug=notify
3. server: touch /export/file1
4. server: mv /export/file1 /export/file2
server debug log:
ksmbd: fid 5:5, notify event: mask=0x00000040 inode=2 name=file1 cookie=974
ksmbd: Saved moved from, mask 0x40, name file1, cookie 974
ksmbd: fid 5:5, notify event: mask=0x00000080 inode=2 name=file2 cookie=974
ksmbd: Matched rename file1 to file2, cookie 974
ksmbd: Queueing notify event, action 4, name file1
ksmbd: Queueing notify event, action 5, name file2

---
 fs/smb/server/notify.c | 71 +++++++++++++++++++++++++++++++++++++++---
 1 file changed, 67 insertions(+), 4 deletions(-)
fs/smb/server/notify.c +67 -4
index 80c1eb089b60..ac253e7326c2 100644
@@ -230,6 +230,67 @@ static void ksmbd_notify_save_moved_from(struct ksmbd_notify *notify,
 			 msecs_to_jiffies(KSMBD_NOTIFY_MOVED_FROM_MSECS));
 }
 
+static bool
+ksmbd_notify_handle_rename(struct ksmbd_notify *notify, u32 mask,
+				 u32 cookie, const struct qstr *file_name)
+{
+	struct ksmbd_notify_event *from, *to;
+	u32 from_mask;
+
+	to = ksmbd_notify_alloc_event(FILE_ACTION_RENAMED_NEW_NAME, file_name,
+				      KSMBD_DEFAULT_GFP);
+	if (!to)
+		return false;
+
+	spin_lock(&notify->lock);
+	if (!notify->moved_from_event ||
+	    notify->moved_from_cookie != cookie) {
+		spin_unlock(&notify->lock);
+		kfree(to);
+		ksmbd_debug(NOTIFY,
+			    "No rename source for destination %.*s, cookie %u\n",
+			    file_name ? file_name->len : 0,
+			    file_name ? (const char *)file_name->name : "",
+			    cookie);
+		return false;
+	}
+	from = notify->moved_from_event;
+	from_mask = notify->moved_from_mask;
+	notify->moved_from_event = NULL;
+	notify->moved_from_mask = 0;
+	notify->moved_from_cookie = 0;
+	cancel_delayed_work(&notify->moved_from_work);
+	spin_unlock(&notify->lock);
+
+	from->action = FILE_ACTION_RENAMED_OLD_NAME;
+	ksmbd_debug(NOTIFY, "Matched rename %.*s to %.*s, cookie %u\n",
+		    (int)from->name_len, from->name,
+		    file_name ? file_name->len : 0,
+		    file_name ? (const char *)file_name->name : "", cookie);
+
+	if (!ksmbd_notify_filter_match(notify, from_mask)) {
+		ksmbd_debug(NOTIFY, "Filtered rename source %.*s\n",
+			    (int)from->name_len, from->name);
+		kfree(from);
+		from = NULL;
+	}
+
+	if (!ksmbd_notify_filter_match(notify, mask)) {
+		ksmbd_debug(NOTIFY, "Filtered rename destination %.*s\n",
+			    file_name ? file_name->len : 0,
+			    file_name ? (const char *)file_name->name : "");
+		kfree(to);
+		to = NULL;
+	}
+
+	if (from)
+		ksmbd_notify_queue_event(notify, from);
+	if (to)
+		ksmbd_notify_queue_event(notify, to);
+
+	return true;
+}
+
 static int ksmbd_notify_handle_inode_event(struct fsnotify_mark *mark,
 					   u32 mask, struct inode *inode,
 					   struct inode *dir,
@@ -264,10 +325,12 @@ static int ksmbd_notify_handle_inode_event(struct fsnotify_mark *mark,
 		return 0;
 	}
 
-	if (mask & FS_MOVED_TO)
-		return 0;
-
-	if (mask & FS_CREATE) {
+	if (mask & FS_MOVED_TO) {
+		if (ksmbd_notify_handle_rename(notify, mask, cookie,
+					       file_name))
+			return 0;
+		action = FILE_ACTION_ADDED;
+	} else if (mask & FS_CREATE) {
 		action = FILE_ACTION_ADDED;
 	} else if (mask & FS_DELETE) {
 		action = FILE_ACTION_REMOVED;
Signature · 1 line
2.54.0
author reviewer
#12 lore raw

RFC PATCH 9/11 smb/server: encode notify events

In reply to ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Sort and merge events, then encode their names as UTF-16 records.

---
 fs/smb/server/notify.c | 99 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 99 insertions(+)
fs/smb/server/notify.c +99 -0
index ac253e7326c2..6fff7b5584b9 100644
@@ -11,6 +11,7 @@
 #include <linux/fsnotify_backend.h>
 #include <linux/jiffies.h>
 #include <linux/ktime.h>
+#include <linux/list_sort.h>
 #include <linux/slab.h>
 #include <linux/wait.h>
 #include <linux/workqueue.h>
@@ -504,6 +505,104 @@ void ksmbd_notify_remove(struct ksmbd_file *fp)
 	kfree(notify);
 }
 
+static int ksmbd_notify_event_cmp(void *priv, const struct list_head *a,
+				  const struct list_head *b)
+{
+	struct ksmbd_notify_event *event_a;
+	struct ksmbd_notify_event *event_b;
+
+	event_a = list_entry(a, struct ksmbd_notify_event, list);
+	event_b = list_entry(b, struct ksmbd_notify_event, list);
+
+	if (event_a->when < event_b->when)
+		return -1;
+	if (event_a->when > event_b->when)
+		return 1;
+	return 0;
+}
+
+static void *ksmbd_notify_encode_events(struct ksmbd_work *work,
+					struct list_head *events,
+					u32 max_len, size_t *data_len)
+{
+	struct ksmbd_notify_event *event;
+	u8 *data = NULL;
+	size_t len = 0;
+
+	list_sort(NULL, events, ksmbd_notify_event_cmp);
+
+	list_for_each_entry(event, events, list) {
+		struct file_notify_information *info;
+		struct ksmbd_notify_event *next;
+		size_t alloc_len, name_buf_len, record_len;
+		bool last = list_is_last(&event->list, events);
+		__le16 *name;
+		u8 *new_data;
+		int name_len;
+
+		/* Coalesce adjacent, case-sensitive duplicate records. */
+		if (!last) {
+			next = list_next_entry(event, list);
+			if (event->action == next->action &&
+			    event->name_len == next->name_len &&
+			    !memcmp(event->name, next->name, event->name_len))
+				continue;
+		}
+
+		name_buf_len = (event->name_len + 1) * sizeof(__le16);
+		name = kmalloc(name_buf_len, KSMBD_DEFAULT_GFP);
+		if (!name) {
+			pr_err("Failed to allocate notify event name buffer\n");
+			goto fail;
+		}
+
+		name_len = smbConvertToUTF16(name, event->name,
+					     event->name_len,
+					     work->conn->local_nls, 0);
+		name_len *= sizeof(__le16);
+		record_len = sizeof(*info) + name_len;
+		alloc_len = ALIGN(record_len, 4);
+		if (len > SIZE_MAX - alloc_len) {
+			pr_err("Notify event data length overflow\n");
+			kfree(name);
+			goto fail;
+		}
+
+		new_data = kvrealloc(data, len + alloc_len, KSMBD_DEFAULT_GFP);
+		if (!new_data) {
+			pr_err("Failed to allocate notify event data, length %zu\n",
+			       len + alloc_len);
+			kfree(name);
+			goto fail;
+		}
+		data = new_data;
+		memset(data + len, 0, alloc_len);
+
+		info = (struct file_notify_information *)(data + len);
+		info->NextEntryOffset = last ? 0 : cpu_to_le32(alloc_len);
+		info->Action = cpu_to_le32(event->action);
+		info->FileNameLength = cpu_to_le32(name_len);
+		memcpy(info->FileName, name, name_len);
+		kfree(name);
+
+		len += alloc_len;
+		if (len > max_len) {
+			ksmbd_debug(NOTIFY,
+				    "Notify event data length %zu exceeds output buffer %u\n",
+				    len, max_len);
+			goto fail;
+		}
+	}
+
+	*data_len = len;
+	return data;
+
+fail:
+	kvfree(data);
+	*data_len = 0;
+	return NULL;
+}
+
 static struct ksmbd_file *
 ksmbd_notify_validate_req(struct ksmbd_work *work,
 			  struct smb2_change_notify_req *req,
Signature · 1 line
2.54.0
author reviewer
#13 lore raw

RFC PATCH 10/11 smb/server: send notify events to the client

In reply to ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Wake one pending request and send its saved events in the reply.

Example:

smbinfo notify /mnt

# server: touch /export/file1
Notifications received, returned data_len is 48
Action: 0x00000001, FileName: file1
Action: 0x00000003, FileName: file1

# server: mv /export/file1 /export/file2
Notifications received, returned data_len is 48
Action: 0x00000004, FileName: file1
Action: 0x00000005, FileName: file2

---
 fs/smb/server/notify.c | 171 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 171 insertions(+)
fs/smb/server/notify.c +171 -0
index 6fff7b5584b9..63e7ed7d615c 100644
@@ -53,12 +53,18 @@ struct ksmbd_notify {
 	u32 moved_from_mask;
 	u32 moved_from_cookie;
 	struct delayed_work moved_from_work;
+	struct delayed_work broadcast_work;
 };
 
 struct ksmbd_notify_req {
 	wait_queue_head_t wait;
+	struct list_head events;
+	unsigned int num_events;
+	bool notified;
 };
 
+#define KSMBD_NOTIFY_BROADCAST_MSECS	1000
+#define KSMBD_NOTIFY_BROADCAST_MAX_EVENTS	100
 #define KSMBD_NOTIFY_MOVED_FROM_MSECS	100
 #define KSMBD_NOTIFY_NAME_EVENT_MASK	(FS_CREATE | FS_DELETE | \
 					 FS_MOVED_FROM | FS_MOVED_TO)
@@ -163,13 +169,28 @@ static void
 ksmbd_notify_queue_event(struct ksmbd_notify *notify,
 			 struct ksmbd_notify_event *event)
 {
+	unsigned long broadcast_delay = 0;
+	bool schedule_broadcast = false;
+
 	ksmbd_debug(NOTIFY, "Queueing notify event, action %u, name %s\n",
 		    event->action, event->name);
 
 	spin_lock(&notify->lock);
 	list_add_tail(&event->list, &notify->events);
 	notify->num_events++;
+	/* Start one timer per batch; reaching the limit flushes it early. */
+	if (notify->num_events == 1) {
+		broadcast_delay =
+			msecs_to_jiffies(KSMBD_NOTIFY_BROADCAST_MSECS);
+		schedule_broadcast = true;
+	} else if (notify->num_events >= KSMBD_NOTIFY_BROADCAST_MAX_EVENTS) {
+		schedule_broadcast = true;
+	}
 	spin_unlock(&notify->lock);
+
+	if (schedule_broadcast)
+		mod_delayed_work(system_dfl_long_wq, &notify->broadcast_work,
+				 broadcast_delay);
 }
 
 static void
@@ -292,6 +313,48 @@ ksmbd_notify_handle_rename(struct ksmbd_notify *notify, u32 mask,
 	return true;
 }
 
+static void ksmbd_notify_broadcast(struct ksmbd_notify *notify)
+{
+	struct ksmbd_notify_req *notify_req;
+	struct ksmbd_work *work;
+
+	spin_lock(&notify->lock);
+	if (list_empty(&notify->events))
+		goto out_unlock;
+
+	spin_lock(&notify->fp->f_lock);
+	list_for_each_entry(work, &notify->fp->blocked_works, fp_entry) {
+		if (READ_ONCE(work->state) != KSMBD_WORK_ACTIVE ||
+		    work->cancel_fn != smb2_notify_cancel ||
+		    !work->cancel_argv)
+			continue;
+
+		notify_req = work->cancel_argv[0];
+		if (READ_ONCE(notify_req->notified))
+			continue;
+
+		list_splice_tail_init(&notify->events, &notify_req->events);
+		notify_req->num_events = notify->num_events;
+		notify->num_events = 0;
+		WRITE_ONCE(notify_req->notified, true);
+		wake_up(&notify_req->wait);
+		break;
+	}
+	spin_unlock(&notify->fp->f_lock);
+
+out_unlock:
+	spin_unlock(&notify->lock);
+}
+
+static void ksmbd_notify_broadcast_work(struct work_struct *work)
+{
+	struct ksmbd_notify *notify;
+
+	notify = container_of(to_delayed_work(work), struct ksmbd_notify,
+			      broadcast_work);
+	ksmbd_notify_broadcast(notify);
+}
+
 static int ksmbd_notify_handle_inode_event(struct fsnotify_mark *mark,
 					   u32 mask, struct inode *inode,
 					   struct inode *dir,
@@ -454,6 +517,8 @@ static int ksmbd_notify_add(struct ksmbd_file *fp, u32 mask, u32 filter,
 	notify->max_buffer_size = max_buffer_size;
 	INIT_DELAYED_WORK(&notify->moved_from_work,
 			  ksmbd_notify_moved_from_timeout);
+	INIT_DELAYED_WORK(&notify->broadcast_work,
+			  ksmbd_notify_broadcast_work);
 
 	mark = ksmbd_notify_add_mark(notify, mask, &notify->group);
 	if (IS_ERR(mark)) {
@@ -500,11 +565,49 @@ void ksmbd_notify_remove(struct ksmbd_file *fp)
 		    notify->mark->mask);
 	ksmbd_notify_destroy_mark(notify->group, notify->mark);
 	cancel_delayed_work_sync(&notify->moved_from_work);
+	cancel_delayed_work_sync(&notify->broadcast_work);
 	kfree(notify->moved_from_event);
 	ksmbd_notify_free_events(&notify->events);
 	kfree(notify);
 }
 
+static unsigned int
+ksmbd_notify_take_events(struct ksmbd_notify *notify, struct list_head *events)
+{
+	unsigned int num_events;
+
+	spin_lock(&notify->lock);
+	num_events = notify->num_events;
+	if (num_events) {
+		list_splice_tail_init(&notify->events, events);
+		notify->num_events = 0;
+	}
+	spin_unlock(&notify->lock);
+
+	if (num_events)
+		ksmbd_debug(NOTIFY,
+			    "Take %u queued notify events for synchronous reply\n",
+			    num_events);
+	return num_events;
+}
+
+static void
+ksmbd_notify_requeue_events(struct ksmbd_notify *notify,
+			    struct ksmbd_notify_req *notify_req)
+{
+	if (!notify_req->num_events)
+		return;
+
+	spin_lock(&notify->lock);
+	/* These events happened before any events already on the queue. */
+	list_splice_init(&notify_req->events, &notify->events);
+	notify->num_events += notify_req->num_events;
+	notify_req->num_events = 0;
+	spin_unlock(&notify->lock);
+
+	ksmbd_notify_broadcast(notify);
+}
+
 static int ksmbd_notify_event_cmp(void *priv, const struct list_head *a,
 				  const struct list_head *b)
 {
@@ -603,6 +706,50 @@ static void *ksmbd_notify_encode_events(struct ksmbd_work *work,
 	return NULL;
 }
 
+static int ksmbd_notify_reply(struct ksmbd_work *work,
+			      struct ksmbd_notify *notify,
+			      struct smb2_change_notify_req *req,
+			      struct smb2_change_notify_rsp *rsp,
+			      struct list_head *events)
+{
+	u32 max_len = min(le32_to_cpu(req->OutputBufferLength),
+			  notify->max_buffer_size);
+	size_t data_len = 0;
+	void *data;
+	int err;
+
+	data = ksmbd_notify_encode_events(work, events, max_len, &data_len);
+	ksmbd_notify_free_events(events);
+
+	/* Maps a successful zero-length notify reply to ENUM_DIR. */
+	if (!data_len) {
+		ksmbd_debug(NOTIFY,
+			    "Return notify enum directory, output buffer length %u\n",
+			    max_len);
+		rsp->hdr.Status = STATUS_NOTIFY_ENUM_DIR;
+		return 0;
+	}
+
+	rsp->StructureSize = cpu_to_le16(9);
+	rsp->OutputBufferOffset = cpu_to_le16(72);
+	rsp->OutputBufferLength = cpu_to_le32(data_len);
+	err = ksmbd_iov_pin_rsp_read(work, rsp,
+				     offsetof(struct smb2_change_notify_rsp, Buffer),
+				     data, data_len);
+	if (err) {
+		pr_err("Failed to pin notify response data, length %zu: %d\n",
+		       data_len, err);
+		kvfree(data);
+		rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
+	} else {
+		ksmbd_debug(NOTIFY,
+			    "Prepared notify response, data length %zu\n",
+			    data_len);
+	}
+
+	return err;
+}
+
 static struct ksmbd_file *
 ksmbd_notify_validate_req(struct ksmbd_work *work,
 			  struct smb2_change_notify_req *req,
@@ -699,11 +846,15 @@ static int ksmbd_notify_wait(struct ksmbd_work *work,
 	list_add_tail(&work->fp_entry, &fp->blocked_works);
 	spin_unlock(&fp->f_lock);
 
+	/* Close the race between the synchronous check and queuing the waiter. */
+	ksmbd_notify_broadcast(notify);
+
 	ksmbd_debug(NOTIFY, "Notify request pending, async id %d\n",
 		    work->async_id);
 	smb2_send_interim_resp(work, STATUS_PENDING);
 
 	err = wait_event_interruptible(notify_req->wait,
+				       READ_ONCE(notify_req->notified) ||
 				       READ_ONCE(work->state) !=
 					       KSMBD_WORK_ACTIVE);
 	if (err && READ_ONCE(work->state) == KSMBD_WORK_ACTIVE) {
@@ -734,6 +885,7 @@ int ksmbd_handle_notify(struct ksmbd_work *work,
 	struct ksmbd_notify_req *notify_req = NULL;
 	struct ksmbd_notify *notify = NULL;
 	struct ksmbd_file *fp = NULL;
+	LIST_HEAD(events);
 	void **argv = NULL;
 	bool async_work = false;
 	int err = 0;
@@ -752,6 +904,12 @@ int ksmbd_handle_notify(struct ksmbd_work *work,
 		goto out;
 	}
 
+	/* Changes which arrived without a waiter are returned synchronously. */
+	if (ksmbd_notify_take_events(notify, &events)) {
+		err = ksmbd_notify_reply(work, notify, req, rsp, &events);
+		goto out;
+	}
+
 	notify_req = kzalloc_obj(*notify_req, KSMBD_DEFAULT_GFP);
 	if (!notify_req) {
 		pr_err("Failed to allocate notify request\n");
@@ -769,6 +927,7 @@ int ksmbd_handle_notify(struct ksmbd_work *work,
 	}
 
 	init_waitqueue_head(&notify_req->wait);
+	INIT_LIST_HEAD(&notify_req->events);
 	argv[0] = notify_req;
 	err = setup_async_work(work, smb2_notify_cancel, argv);
 	if (err) {
@@ -781,17 +940,29 @@ int ksmbd_handle_notify(struct ksmbd_work *work,
 	err = ksmbd_notify_wait(work, notify, notify_req);
 
 	if (work->state == KSMBD_WORK_CANCELLED) {
+		ksmbd_notify_requeue_events(notify, notify_req);
 		ksmbd_debug(NOTIFY, "Notify request cancelled, async id %d\n",
 			    work->async_id);
 		rsp->hdr.Status = STATUS_CANCELLED;
 		smb2_send_interim_resp(work, STATUS_CANCELLED);
 		work->send_no_response = 1;
 	} else if (work->state == KSMBD_WORK_CLOSED) {
+		ksmbd_notify_requeue_events(notify, notify_req);
 		ksmbd_debug(NOTIFY, "Notify handle closed, async id %d\n",
 			    work->async_id);
 		rsp->hdr.Status = STATUS_NOTIFY_CLEANUP;
 		smb2_send_interim_resp(work, STATUS_NOTIFY_CLEANUP);
 		work->send_no_response = 1;
+	} else {
+		/* Complete the request using the AsyncId sent in STATUS_PENDING. */
+		rsp->hdr.Flags |= SMB2_FLAGS_ASYNC_COMMAND;
+		rsp->hdr.Id.AsyncId = cpu_to_le64(work->async_id);
+		err = ksmbd_notify_reply(work, notify, req, rsp,
+					 &notify_req->events);
+		if (!err)
+			ksmbd_debug(NOTIFY,
+				    "Completed notify request, async id %d\n",
+				    work->async_id);
 	}
 
 out:
Signature · 1 line
2.54.0
maintainer
#14 lore raw

Re: [RFC PATCH 10/11] smb/server: send notify events to the client

fs/smb/server/notify.c int ksmbd_handle_notify(struct ksmbd_work *work, · line 908

Quoted 6 lines
fs/smb/server/notify.c +2 -0
@@ -752,6 +904,12 @@ int ksmbd_handle_notify(struct ksmbd_work *work,
                goto out;
        }
 
+       /* Changes which arrived without a waiter are returned synchronously. */
+       if (ksmbd_notify_take_events(notify, &events)) {

This can violate the required FIFO completion order. If an older CHANGE_NOTIFY request is already pending and events are waiting for the delayed broadcast worker, a newly arriving request can take notify->events here and complete before the older request. Please give queued events to the oldest pending waiter first, and use this synchronous path only when no earlier request is pending.

Quoted 4 lines
fs/smb/server/notify.c +4 -0
+               err = ksmbd_notify_reply(work, notify, req, rsp, &events);
+               goto out;
+       }
+
maintainer
#15 lore raw

Re: [RFC PATCH 10/11] smb/server: send notify events to the client

In reply to ChenXiaoSong

fs/smb/server/notify.c static void *ksmbd_notify_encode_events(struct ksmbd_work *work, · line 716

Quoted 8 lines
fs/smb/server/notify.c +8 -0
+static int ksmbd_notify_reply(struct ksmbd_work *work,
+                             struct ksmbd_notify *notify,
+                             struct smb2_change_notify_req *req,
+                             struct smb2_change_notify_rsp *rsp,
+                             struct list_head *events)
+{
+       u32 max_len = min(le32_to_cpu(req->OutputBufferLength),
+                         notify->max_buffer_size);

Why is the response limited by notify->max_buffer_size, which is captured when the watch is first created?
The maximum amount returned for this completion should be governed by the current request's OutputBufferLength. Limiting it to the first request's buffer size can unnecessarily return STATUS_NOTIFY_ENUM_DIR even when a later request supplied a sufficiently large output buffer.

Thanks for the patch!

author reviewer
#16 lore raw

Re: [RFC PATCH 10/11] smb/server: send notify events to the client

Thanks for your reviews. Your suggestions are very helpful to me.

Quoted 16 lines · Namjae Jeon

fs/smb/server/notify.c static void *ksmbd_notify_encode_events(struct ksmbd_work *work, · line 716

Quoted 8 lines
fs/smb/server/notify.c +8 -0
+static int ksmbd_notify_reply(struct ksmbd_work *work,
+                             struct ksmbd_notify *notify,
+                             struct smb2_change_notify_req *req,
+                             struct smb2_change_notify_rsp *rsp,
+                             struct list_head *events)
+{
+       u32 max_len = min(le32_to_cpu(req->OutputBufferLength),
+                         notify->max_buffer_size);

Why is the response limited by notify->max_buffer_size, which is captured when the watch is first created?
The maximum amount returned for this completion should be governed by the current request's OutputBufferLength. Limiting it to the first request's buffer size can unnecessarily return STATUS_NOTIFY_ENUM_DIR even when a later request supplied a sufficiently large output buffer.

Thanks for the patch!

Signature · 3 lines
ChenXiaoSong <[email protected]>
Chinese Homepage: https://chenxiaosong.com
English Homepage: https://chenxiaosong.com/en
author reviewer
#17 lore raw

RFC PATCH 11/11 smb/server: break directory leases before sending notify events

In reply to ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Windows can keep directory data in a cache while it has a directory lease. A file change made on the server does not use the SMB request path, so the lease is not broken.

The server sends a notify event, but Windows may still use the old data. Some new files are then not shown in File Explorer.

---
 fs/smb/server/notify.c | 34 +++++++++++++++++++++++++++++++---
 fs/smb/server/oplock.c | 40 ++++++++++++++++++++++++++++++++++++++++
 fs/smb/server/oplock.h |  1 +
 3 files changed, 72 insertions(+), 3 deletions(-)
fs/smb/server/notify.c +31 -3
index 63e7ed7d615c..1b27c17a8aef 100644
@@ -21,6 +21,7 @@
 #include "connection.h"
 #include "ksmbd_work.h"
 #include "notify.h"
+#include "oplock.h"
 #include "smb_common.h"
 #include "smb2pdu.h"
 #include "vfs_cache.h"
@@ -346,13 +347,34 @@ static void ksmbd_notify_broadcast(struct ksmbd_notify *notify)
 	spin_unlock(&notify->lock);
 }
 
+static bool ksmbd_notify_events_pending(struct ksmbd_notify *notify)
+{
+	bool pending;
+
+	spin_lock(&notify->lock);
+	pending = !list_empty(&notify->events);
+	spin_unlock(&notify->lock);
+
+	return pending;
+}
+
+/* Invalidate directory caches before making the change visible to a client. */
+static void ksmbd_notify_dispatch(struct ksmbd_notify *notify)
+{
+	if (!ksmbd_notify_events_pending(notify))
+		return;
+
+	smb_break_dir_lease(notify->fp);
+	ksmbd_notify_broadcast(notify);
+}
+
 static void ksmbd_notify_broadcast_work(struct work_struct *work)
 {
 	struct ksmbd_notify *notify;
 
 	notify = container_of(to_delayed_work(work), struct ksmbd_notify,
 			      broadcast_work);
-	ksmbd_notify_broadcast(notify);
+	ksmbd_notify_dispatch(notify);
 }
 
 static int ksmbd_notify_handle_inode_event(struct fsnotify_mark *mark,
@@ -576,6 +598,12 @@ ksmbd_notify_take_events(struct ksmbd_notify *notify, struct list_head *events)
 {
 	unsigned int num_events;
 
+	if (!ksmbd_notify_events_pending(notify))
+		return 0;
+
+	/* This path bypasses broadcast_work, so break directory leases here. */
+	smb_break_dir_lease(notify->fp);
+
 	spin_lock(&notify->lock);
 	num_events = notify->num_events;
 	if (num_events) {
@@ -605,7 +633,7 @@ ksmbd_notify_requeue_events(struct ksmbd_notify *notify,
 	notify_req->num_events = 0;
 	spin_unlock(&notify->lock);
 
-	ksmbd_notify_broadcast(notify);
+	ksmbd_notify_dispatch(notify);
 }
 
 static int ksmbd_notify_event_cmp(void *priv, const struct list_head *a,
@@ -847,7 +875,7 @@ static int ksmbd_notify_wait(struct ksmbd_work *work,
 	spin_unlock(&fp->f_lock);
 
 	/* Close the race between the synchronous check and queuing the waiter. */
-	ksmbd_notify_broadcast(notify);
+	ksmbd_notify_dispatch(notify);
 
 	ksmbd_debug(NOTIFY, "Notify request pending, async id %d\n",
 		    work->async_id);
fs/smb/server/oplock.c +40 -0
index 591b2fca1d4e..6bd870e84cf6 100644
@@ -1404,6 +1404,46 @@ void smb_send_parent_lease_break_noti(struct ksmbd_file *fp,
 	ksmbd_inode_put(p_ci);
 }
 
+/**
+ * smb_break_dir_lease() - break leases when a directory changes
+ * @fp: open directory that changed
+ *
+ * Some directory changes do not go through the SMB request path. fsnotify
+ * reports these changes. Break the directory leases before sending the
+ * changes to the SMB client.
+ *
+ * This function can sleep while it waits for a reply from the client. Do not
+ * call it from the fsnotify callback.
+ */
+void smb_break_dir_lease(struct ksmbd_file *fp)
+{
+	struct ksmbd_inode *ci = fp->f_ci;
+	struct oplock_info *opinfo;
+	LIST_HEAD(brk_list);
+
+	down_read(&ci->m_lock);
+	list_for_each_entry(opinfo, &ci->m_op_list, op_entry) {
+		if (!opinfo->conn || !opinfo->is_lease ||
+		    !opinfo->o_lease->is_dir ||
+		    opinfo->o_lease->state == SMB2_LEASE_NONE_LE)
+			continue;
+
+		if (!atomic_inc_not_zero(&opinfo->refcount))
+			continue;
+
+		if (ksmbd_conn_releasing(opinfo->conn)) {
+			opinfo_put(opinfo);
+			continue;
+		}
+
+		if (oplock_break_add(&brk_list, opinfo))
+			opinfo_put(opinfo);
+	}
+	up_read(&ci->m_lock);
+
+	oplock_break_drain_none(&brk_list);
+}
+
 void smb_lazy_parent_lease_break_close(struct ksmbd_file *fp)
 {
 	struct oplock_info *opinfo;
fs/smb/server/oplock.h +1 -0
index 23274b645ede..3f05fd684640 100644
@@ -136,6 +136,7 @@ int find_same_lease_key(struct ksmbd_conn *conn, struct ksmbd_inode *ci,
 void destroy_lease_table(struct ksmbd_conn *conn);
 void smb_send_parent_lease_break_noti(struct ksmbd_file *fp,
 				      struct lease_ctx_info *lctx);
+void smb_break_dir_lease(struct ksmbd_file *fp);
 void smb_lazy_parent_lease_break_close(struct ksmbd_file *fp);
 int smb2_check_durable_oplock(struct ksmbd_conn *conn,
 			      struct ksmbd_share_config *share,
Signature · 1 line
2.54.0
author reviewer
#18 lore raw
In reply to ChenXiaoSong

Do you have any suggestions for this patchset? I plan to send the non-RFC version in the next few days.

Quoted 2 lines · Namjae Jeon

He needs to submit a patch aligned with the current base. We can't just wait since we don't know when Gaël will send the patch.

Quoted 44 lines · ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Sorry for taking such a long time to complete the ksmbd change notify feature. Over the past few months, I have been busy with the birth of my daughter and my PhD-related tasks, which left me with limited time to work on it.

Once Gaël updates his patch ("ksmbd: defer CHANGE_NOTIFY completion instead of STATUS_NOT_IMPLEMENTED"), I will rebase my patchset on top of it.

To test this feature, the following patches need to be applied to cifs-utils and kernel smb client:
- cifs-utils: https://chenxiaosong.com/lkml/cifs-utils-parse-notify
- kernel smb client: https://chenxiaosong.com/lkml/cifs-improve-notify

The complete kernel client and server patches are also available at the following link:
https://chenxiaosong.com/repo/linux/ksmbd-change-notify

ChenXiaoSong (11):
smb/server: add debug type for change notify smb/server: validate notify requests
smb/server: support canceling notify requests smb/server: watch directories for changes smb/server: keep notify watches on file handles smb/server: save simple notify events
smb/server: save old names for rename notify events smb/server: match rename notify events
smb/server: encode notify events
smb/server: send notify events to the client smb/server: break directory leases before sending notify events

  fs/smb/server/Kconfig     |    1 +
  fs/smb/server/Makefile    |    2 +-
  fs/smb/server/glob.h      |    3 +-
  fs/smb/server/notify.c    | 1011 +++++++++++++++++++++++++++++++++++++
  fs/smb/server/notify.h    |   24 +
  fs/smb/server/oplock.c    |   40 ++
  fs/smb/server/oplock.h    |    1 +
  fs/smb/server/server.c    |    2 +-
  fs/smb/server/smb2pdu.c   |   12 +-
  fs/smb/server/vfs_cache.c |    3 +
  fs/smb/server/vfs_cache.h |    4 +
  11 files changed, 1095 insertions(+), 8 deletions(-)

create mode 100644 fs/smb/server/notify.c
create mode 100644 fs/smb/server/notify.h

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