Skip to contentSkip to conversation

ksmbd: add macOS Time Machine / AAPL SMB2 extension support

PATCH Superseded Gaël Blivet-Bailly 2 participants 8 messages Jul 7 – Jul 8

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover ksmbd: add macOS Time Machine / AAPL SMB2 extension support

From: Gael Blivet <[email protected]>

ksmbd currently doesn't implement any of the Apple "AAPL" SMB2 extensions. Without them, macOS Time Machine backups over SMB do not work at all. This series adds enough of the extension to fix that.

The underlying logic was built and live-tested against a real Time Machine backup and everyday Finder use over several days on an earlier baseline. This series is a careful re-derivation against ksmbd-for-next-next -- verified to build cleanly and pass checkpatch.pl --strict, but not independently re-tested against this exact tree.

Everything here is scoped to shares with the new KSMBD_SHARE_FLAG_TIME_MACHINE flag, not enabled globally: the AAPL extension is undocumented and reverse-engineered from observed client/server behavior (cross-checked against Samba's vfs_fruit module and the Apple Time Machine over SMB spec[1] where possible), so this keeps the blast radius contained to shares that explicitly opt in.

Patch 1 builds on top of the AAPL groundwork already in this tree:
fs/smb/common/smb2pdu.h already has the SMB2_CRTCTX_AAPL_* constant definitions (added recently, currently unused anywhere), and conn->is_aapl detection already exists for an unrelated narrow purpose (reporting UniqueId=0 to Mac clients). This series reuses both rather than introducing a parallel/competing implementation.

Patch order matters: 2-5 depend on the AAPL negotiation and conn->aapl_readdir_attr flag added in patch 1.

[1] https://developer.apple.com/library/archive/releasenotes/NetworkingInternetWeb/Time_Machine_SMB_Spec/

Gael Blivet (5):
ksmbd: add Apple AAPL kAAPL_SERVER_QUERY create context support
ksmbd: synthesize empty AFP_AfpInfo xattr on first probe
ksmbd: send inline FinderInfo in FIND responses when READDIR_ATTR
negotiated
ksmbd: defer CHANGE_NOTIFY completion instead of
STATUS_NOT_IMPLEMENTED
ksmbd: implement full-file copy for AAPL ChunkCount=0 COPYCHUNK

 fs/smb/common/smbglob.h       |   1 +
 fs/smb/server/connection.h    |   1 +
 fs/smb/server/ksmbd_netlink.h |   4 +-
 fs/smb/server/ksmbd_work.c    |   1 +
 fs/smb/server/ksmbd_work.h    |   2 +
 fs/smb/server/oplock.c        |  73 +++++++++
 fs/smb/server/oplock.h        |   1 +
 fs/smb/server/server.h        |   2 +
 fs/smb/server/smb2ops.c       |   4 +
 fs/smb/server/smb2pdu.c       | 275 +++++++++++++++++++++++++++++++---
 fs/smb/server/smb2pdu.h       |  60 ++++++++
 fs/smb/server/transport_ipc.c |   9 ++
 fs/smb/server/vfs.c           |  41 +++++
 fs/smb/server/vfs_cache.c     |  30 ++++
 fs/smb/server/vfs_cache.h     |   6 +
 15 files changed, 487 insertions(+), 23 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/5 ksmbd: add Apple AAPL kAAPL_SERVER_QUERY create context support

From: Gael Blivet <[email protected]>

macOS clients (Finder, and specifically Time Machine's backupd) send an "AAPL" SMB2 create context on CREATE to negotiate Apple-specific server capabilities (server_caps/vol_caps/model string). Without a response to this context, macOS Time Machine over SMB does not work at all.

Add the AAPL create context structs (create_aapl_rsp, aapl_server_query_req) and create_aapl_rsp_buf(), which builds the kAAPL_SERVER_QUERY response mirroring the layout observed from macOS's own smbd, including the model string workaround: omitting the model string when the client requested it causes smbfs.kext to enter a broken disconnect path requiring a full macOS reboot to recover from. Command codes and bitmap values reuse the existing SMB2_CRTCTX_AAPL_* constants in fs/smb/common/smb2pdu.h.

Hook the request parsing and response into smb2_open()'s existing create-context handling, following the same DataOffset+DataLength bounds-checking convention already used by every other context parser in this file. The AAPL model string is configurable via the existing netlink startup path (server_conf.aapl_model, default "Xserve").

This is scoped to shares with the new KSMBD_SHARE_FLAG_TIME_MACHINE flag only, not enabled globally -- Apple's AAPL extension is undocumented and reverse-engineered from observed client/server behavior, so containing its blast radius to shares that explicitly opt in limits risk to ordinary SMB shares.

conn->aapl_readdir_attr is set here when the client also advertises READDIR_ATTR support, but the actual inline-FinderInfo wire format (the feature that flag gates) is not implemented yet -- follow-up commit.

---
 fs/smb/common/smbglob.h       |  1 +
 fs/smb/server/connection.h    |  1 +
 fs/smb/server/ksmbd_netlink.h |  4 +-
 fs/smb/server/oplock.c        | 73 +++++++++++++++++++++++++++++++++++
 fs/smb/server/oplock.h        |  1 +
 fs/smb/server/server.h        |  2 +
 fs/smb/server/smb2ops.c       |  4 ++
 fs/smb/server/smb2pdu.c       | 57 ++++++++++++++++++++++++++-
 fs/smb/server/smb2pdu.h       | 60 ++++++++++++++++++++++++++++
 fs/smb/server/transport_ipc.c |  9 +++++
 10 files changed, 210 insertions(+), 2 deletions(-)
fs/smb/common/smbglob.h +1 -0
index 4e33d91cd..d9c7e6e7a 100644
@@ -39,6 +39,7 @@ struct smb_version_values {
 	size_t		create_mxac_size;
 	size_t		create_disk_id_size;
 	size_t		create_posix_size;
+	size_t		create_aapl_size;
 };
 
 static inline unsigned int get_rfc1002_len(void *buf)
fs/smb/server/connection.h +1 -0
index ec75633b7..011a00b1b 100644
@@ -124,6 +124,7 @@ struct ksmbd_conn {
 	bool				binding;
 	atomic_t			refcnt;
 	bool				is_aapl;
+	bool				aapl_readdir_attr; /* READDIR_ATTR negotiated */
 	struct work_struct		release_work;
 };
 
fs/smb/server/ksmbd_netlink.h +3 -1
index c9e1b0b68..1ea0a367b 100644
@@ -113,7 +113,8 @@ struct ksmbd_startup_request {
 	__u32	max_connections;	/* Number of maximum simultaneous connections */
 	__s8	bind_interfaces_only;
 	__u32	max_ip_connections;	/* Number of maximum connection per ip address */
-	__s8	reserved[499];		/* Reserved room */
+	__s8	aapl_model[32];		/* AAPL model string for Finder icon, e.g. "Xserve" */
+	__s8	reserved[467];		/* Reserved room */
 	__u32	ifc_list_sz;		/* interfaces list size */
 	__s8	____payload[];
 } __packed;
@@ -378,6 +379,7 @@ enum KSMBD_TREE_CONN_STATUS {
 #define KSMBD_SHARE_FLAG_CROSSMNT			BIT(15)
 #define KSMBD_SHARE_FLAG_CONTINUOUS_AVAILABILITY	BIT(16)
 #define KSMBD_SHARE_FLAG_HIDE_UNREADABLE		BIT(17)
+#define KSMBD_SHARE_FLAG_TIME_MACHINE			BIT(18)
 
 /*
  * Tree connect request flags.
fs/smb/server/oplock.c +73 -0

85 lines not on this page · raw message

fs/smb/server/oplock.h +1 -0
index 3f581d22b..f7f6afcc5 100644
@@ -125,6 +125,7 @@ void create_durable_v2_rsp_buf(char *cc, struct ksmbd_file *fp);
 void create_mxac_rsp_buf(char *cc, int maximal_access);
 void create_disk_id_rsp_buf(char *cc, __u64 file_id, __u64 vol_id);
 void create_posix_rsp_buf(char *cc, struct ksmbd_file *fp);
+void create_aapl_rsp_buf(char *cc, __u64 vol_caps, __u64 req_bitmap);
 struct create_context *smb2_find_context_vals(void *open_req, const char *tag, int tag_len);
 struct oplock_info *lookup_lease_in_table(struct ksmbd_conn *conn,
 					  char *lease_key);
fs/smb/server/server.h +2 -0
index b8a7317be..4d4d268b5 100644
@@ -48,6 +48,8 @@ struct ksmbd_server_config {
 	char			*conf[SERVER_CONF_WORK_GROUP + 1];
 	struct task_struct	*dh_task;
 	bool			bind_interfaces_only;
+	/* AAPL model string for Finder icon, e.g. "Xserve" */
+	char			aapl_model[32];
 };
 
 extern struct ksmbd_server_config server_conf;
fs/smb/server/smb2ops.c +4 -0
index c9a32ee09..97938150d 100644
@@ -37,6 +37,7 @@ static struct smb_version_values smb21_server_values = {
 	.create_mxac_size = sizeof(struct create_mxac_rsp),
 	.create_disk_id_size = sizeof(struct create_disk_id_rsp),
 	.create_posix_size = sizeof(struct create_posix_rsp),
+	.create_aapl_size = AAPL_RSP_MAX_SIZE,
 };
 
 static struct smb_version_values smb30_server_values = {
@@ -64,6 +65,7 @@ static struct smb_version_values smb30_server_values = {
 	.create_mxac_size = sizeof(struct create_mxac_rsp),
 	.create_disk_id_size = sizeof(struct create_disk_id_rsp),
 	.create_posix_size = sizeof(struct create_posix_rsp),
+	.create_aapl_size = AAPL_RSP_MAX_SIZE,
 };
 
 static struct smb_version_values smb302_server_values = {
@@ -91,6 +93,7 @@ static struct smb_version_values smb302_server_values = {
 	.create_mxac_size = sizeof(struct create_mxac_rsp),
 	.create_disk_id_size = sizeof(struct create_disk_id_rsp),
 	.create_posix_size = sizeof(struct create_posix_rsp),
+	.create_aapl_size = AAPL_RSP_MAX_SIZE,
 };
 
 static struct smb_version_values smb311_server_values = {
@@ -118,6 +121,7 @@ static struct smb_version_values smb311_server_values = {
 	.create_mxac_size = sizeof(struct create_mxac_rsp),
 	.create_disk_id_size = sizeof(struct create_disk_id_rsp),
 	.create_posix_size = sizeof(struct create_posix_rsp),
+	.create_aapl_size = AAPL_RSP_MAX_SIZE,
 };
 
 static struct smb_version_ops smb2_0_server_ops = {
fs/smb/server/smb2pdu.c +56 -1

81 lines not on this page · raw message

fs/smb/server/smb2pdu.h +60 -0

66 lines not on this page · raw message

fs/smb/server/transport_ipc.c +9 -0
index 0c581b962..bd58d3d0b 100644
@@ -322,6 +322,15 @@ static int ipc_server_config_on_startup(struct ksmbd_startup_request *req)
 		goto out;
 	}
 	server_conf.share_fake_fscaps = req->share_fake_fscaps;
+
+	/* AAPL model string for Finder icon */
+	if (req->aapl_model[0])
+		strscpy(server_conf.aapl_model, req->aapl_model,
+			sizeof(server_conf.aapl_model));
+	else
+		strscpy(server_conf.aapl_model, "Xserve",
+			sizeof(server_conf.aapl_model));
+
 	ksmbd_init_domain(req->sub_auth);
 
 	if (req->smb2_max_read)
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH 1/5] ksmbd: add Apple AAPL kAAPL_SERVER_QUERY create context support

Quoted 15 lines
fs/smb/server/ksmbd_netlink.h +2 -1
index c9e1b0b68..1ea0a367b 100644
@@ -113,7 +113,8 @@ struct ksmbd_startup_request {
        __u32   max_connections;        /* Number of maximum simultaneous connections */
        __s8    bind_interfaces_only;
        __u32   max_ip_connections;     /* Number of maximum connection per ip address */
-       __s8    reserved[499];          /* Reserved room */
+       __s8    aapl_model[32];         /* AAPL model string for Finder icon, e.g. "Xserve" */
+       __s8    reserved[467];          /* Reserved room */
        __u32   ifc_list_sz;            /* interfaces list size */
        __s8    ____payload[];
 } __packed;

fs/smb/server/ksmbd_netlink.h enum KSMBD_TREE_CONN_STATUS { · line 382

Quoted 4 lines
fs/smb/server/ksmbd_netlink.h +1 -0
@@ -378,6 +379,7 @@ enum KSMBD_TREE_CONN_STATUS {
 #define KSMBD_SHARE_FLAG_CROSSMNT                      BIT(15)
 #define KSMBD_SHARE_FLAG_CONTINUOUS_AVAILABILITY       BIT(16)
 #define KSMBD_SHARE_FLAG_HIDE_UNREADABLE               BIT(17)
+#define KSMBD_SHARE_FLAG_TIME_MACHINE                  BIT(18)

There probably needs to be a ksmbd-tools patch. I cannot find it in the list and any PR in ksmbd-tools repo.

Thanks.

#3 lore raw

PATCH 2/5 ksmbd: synthesize empty AFP_AfpInfo xattr on first probe

In reply to Gaël Blivet-Bailly

From: Gael Blivet <[email protected]>

Once a server advertises the AAPL COPYFILE capability, macOS requires an AFP_AfpInfo stream on every file it looks at for Finder type/ creator/icon resolution. smb2_set_stream_name_xattr() currently returns -EBADF (STATUS_OBJECT_NAME_NOT_FOUND) when a client opens AFP_AfpInfo with FILE_OPEN disposition and the xattr doesn't exist yet, which macOS treats as fatal for that file: Finder falls back to showing a generic icon, and file operations that depend on succeeding against this stream (e.g. Cmd+D duplication) fail.

Synthesize a 60-byte zeroed AFP_AfpInfo xattr (magic 0x00051607, version 0x00020000, both big-endian per the AFP_AfpInfo wire format) on first FILE_OPEN probe instead, matching Samba's vfs_fruit module. type=0/creator=0 tells macOS to fall back to extension-based type detection, which is correct for files with no explicit Finder metadata. The synthesized xattr persists on disk, so this only pays the extra write once per file; a later genuine write from macOS (e.g. after the user assigns a custom icon) overwrites it normally.

---
 fs/smb/server/smb2pdu.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
fs/smb/server/smb2pdu.c +15 -0
index dce2a1088..8b81c712e 100644
@@ -2824,6 +2824,21 @@ static noinline int smb2_set_stream_name_xattr(const struct path *path,
 		return 0;
 
 	if (fp->cdoption == FILE_OPEN_LE) {
+		if (!strcmp(stream_name, "AFP_AfpInfo")) {
+			/*
+			 * Synthesize an empty AFP_AfpInfo xattr on first access.
+			 * type=0/creator=0 tells macOS to use the file extension
+			 * for icon and type detection. Matches Samba vfs_fruit.
+			 */
+			static const u8 afpinfo_empty[60] = {
+				0x00, 0x05, 0x16, 0x07, /* magic  0x00051607 BE */
+				0x00, 0x02, 0x00, 0x00, /* version 0x00020000 BE */
+			};
+			rc = ksmbd_vfs_setxattr(idmap, path, xattr_stream_name,
+						(void *)afpinfo_empty,
+						sizeof(afpinfo_empty), 0, false);
+			return rc < 0 ? rc : 0;
+		}
 		ksmbd_debug(SMB, "XATTR stream name lookup failed: %d\n", rc);
 		return -EBADF;
 	}
Signature · 1 line
2.43.0
#4 lore raw

PATCH 3/5 ksmbd: send inline FinderInfo in FIND responses when READDIR_ATTR negotiated

In reply to Gaël Blivet-Bailly

From: Gael Blivet <[email protected]>

Without READDIR_ATTR, macOS Finder resolves type/creator/icon for every file in a directory listing by opening its AFP_AfpInfo stream individually -- one extra CREATE+QUERY_INFO+CLOSE round trip per file, which is the dominant cost of browsing a large directory over SMB from a Mac.

When the client negotiates READDIR_ATTR (conn->aapl_readdir_attr, set during CREATE's AAPL context exchange), inline the same information directly into each FILEID_BOTH_DIRECTORY_INFORMATION FIND entry:
EaSize = max_access, expanded specific rights
(GENERIC_ALL_FLAGS), not the raw FILE_GENERIC_ALL_LE "generic" meta-bit -- that bit has none of the specific FILE_* rights macOS's smbfs.kext checks bit-by-bit, so reporting it directly would fail every access check and show Finder's "no entry" badge on every file/folder.
ShortNameLength = 24 (fixed; the spec says 0 when there's no short
name, but real macOS clients expect 24 here)
ShortName[0..7] = resource fork size (0 -- no resource forks) ShortName[8..23] = compressed FinderInfo (all zero: type/creator
unset, client falls back to extension-based icon/type detection, consistent with the AFP_AfpInfo synthesis this mirrors)
Reserved2 = Unix mode bits
Reparse-point status is still carried via ExtFileAttributes rather than EaSize once READDIR_ATTR is active, since EaSize is repurposed for max_access.

This wire format was reverse-engineered from macOS smbfs.kext network behavior (Apple has not published it) and cross-checked against Samba's vfs_fruit marshalling (source3/smbd/smb2_trans2.c, SMB_FIND_ID_BOTH_DIRECTORY_INFO), which implements the same extension for the same reason and confirms both the max_access expansion and the ShortNameLength=24 behavior.

---
 fs/smb/server/smb2pdu.c | 57 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 5 deletions(-)
fs/smb/server/smb2pdu.c +52 -5
index 8b81c712e..b1be4df04 100644
@@ -4649,17 +4649,64 @@ static int smb2_populate_readdir_entry(struct ksmbd_conn *conn, int info_level,
 
 		fibdinfo = (struct file_id_both_directory_info *)kstat;
 		fibdinfo->FileNameLength = cpu_to_le32(conv_len);
-		fibdinfo->EaSize =
-			smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
-		if (fibdinfo->EaSize)
-			fibdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE;
 		if (conn->is_aapl)
 			fibdinfo->UniqueId = 0;
 		else
 			fibdinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
 		fibdinfo->ShortNameLength = 0;
 		fibdinfo->Reserved = 0;
-		fibdinfo->Reserved2 = cpu_to_le16(0);
+		if (conn->aapl_readdir_attr) {
+			/*
+			 * READDIR_ATTR wire format, confirmed against Samba's
+			 * vfs_fruit marshalling (source3/smbd/smb2_trans2.c):
+			 *   EaSize           = max_access (expanded specific
+			 *                      rights, simplified to "grant all")
+			 *   ShortNameLength  = 24 (fixed; not 0, despite the spec)
+			 *   ShortName[0..7]  = resource fork size (uint64 LE, 0 = no rfork)
+			 *   ShortName[8..23] = compressed FinderInfo (type+creator+flags+
+			 *                      ext_flags+date_added, 16 bytes LE; all
+			 *                      zeros means type=0/creator=0, i.e. use
+			 *                      the file extension for icon lookup)
+			 *   Reserved2        = Unix mode bits (uint16 LE)
+			 * Reparse-point tag is indicated via ExtFileAttributes, not EaSize.
+			 */
+			__le32 reparse_tag =
+				smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
+
+			if (reparse_tag)
+				fibdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE;
+			/*
+			 * FILE_GENERIC_ALL_LE (0x10000000) is the raw
+			 * "generic all" meta-bit -- valid only in a
+			 * client's requested access mask, for the server
+			 * to expand. It has none of the specific FILE_*
+			 * rights bits set (FILE_LIST_DIRECTORY, FILE_TRAVERSE,
+			 * etc.), so reporting it here as max_access would make
+			 * macOS's bit-by-bit access checks fail on every
+			 * entry -> permanent "no entry" badges in Finder.
+			 * Report the actual expanded rights instead, same
+			 * as smb_map_generic_desired_access() does when
+			 * translating a client's GENERIC_ALL request.
+			 */
+			fibdinfo->EaSize = cpu_to_le32(GENERIC_ALL_FLAGS);
+			/*
+			 * The spec says ShortNameLength should be 0 when
+			 * there's no short name, but real macOS clients
+			 * expect it set to 24 for READDIR_ATTR entries --
+			 * confirmed in Samba's vfs_fruit marshalling
+			 * (smb2_trans2.c): "on the wire behaviour shows
+			 * its set to 24 by clients."
+			 */
+			fibdinfo->ShortNameLength = 24;
+			memset(fibdinfo->ShortName, 0, sizeof(fibdinfo->ShortName));
+			fibdinfo->Reserved2 = cpu_to_le16(ksmbd_kstat->kstat->mode & 0xffff);
+		} else {
+			fibdinfo->EaSize =
+				smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
+			if (fibdinfo->EaSize)
+				fibdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE;
+			fibdinfo->Reserved2 = cpu_to_le16(0);
+		}
 		if (d_info->hide_dot_file && d_info->name[0] == '.')
 			fibdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE;
 		memcpy(fibdinfo->FileName, conv_name, conv_len);
Signature · 1 line
2.43.0
#5 lore raw

PATCH 4/5 ksmbd: defer CHANGE_NOTIFY completion instead of STATUS_NOT_IMPLEMENTED

In reply to Gaël Blivet-Bailly

From: Gael Blivet <[email protected]>

smb2_notify() currently returns STATUS_NOT_IMPLEMENTED synchronously for every CHANGE_NOTIFY request. Genuine SMB2 servers never complete a CHANGE_NOTIFY spontaneously -- it's satisfied only by a real directory change or with STATUS_NOTIFY_CLEANUP when the watched handle is closed. macOS smbfs.kext depends on this deferred-completion contract:
receiving STATUS_NOT_IMPLEMENTED instead makes it hard-freeze on unmount, since it never sees the cleanup it's waiting for.

Add a notify_pendings list on struct ksmbd_file (protected by the existing f_lock) and a notify_entry list_head on struct ksmbd_work to link onto it. smb2_notify() now replies STATUS_PENDING immediately and queues a deferred STATUS_NOTIFY_CLEANUP response on the watched handle; __ksmbd_close_fd() drains and sends any pending notifications when the handle is actually closed. The drain splices the list out under fp->f_lock first, then processes the detached copy without the lock -- smb2_notify() on another connection can be adding to the same list at the same time a close happens on this one, and ksmbd_conn_write() can sleep (it takes the connection's write mutex), so it must not be called while the spinlock is held.

Also handle the FileId=FFFF...FFFF share-root sentinel that macOS backupd sends to watch for changes without holding an open handle -- without an immediate STATUS_PENDING/STATUS_NOTIFY_CLEANUP reply here, backupd aborts Time Machine setup with STATUS_FILE_CLOSED.

---
 fs/smb/server/ksmbd_work.c |   1 +
 fs/smb/server/ksmbd_work.h |   2 +
 fs/smb/server/smb2pdu.c    | 108 +++++++++++++++++++++++++++++++++++--
 fs/smb/server/vfs_cache.c  |  30 +++++++++++
 fs/smb/server/vfs_cache.h  |   6 +++
 5 files changed, 144 insertions(+), 3 deletions(-)
fs/smb/server/ksmbd_work.c +1 -0
index e2c2f4526..97502273a 100644
@@ -56,6 +56,7 @@ 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 +2 -0
index 88104f0cf..50c4aa779 100644
@@ -104,6 +104,8 @@ struct ksmbd_work {
 	/* List head at conn->async_requests */
 	struct list_head                async_request_entry;
 	struct list_head                fp_entry;
+	/* List head at ksmbd_file->notify_pendings */
+	struct list_head                notify_entry;
 };
 
 /**
fs/smb/server/smb2pdu.c +105 -3

120 lines not on this page · raw message

fs/smb/server/vfs_cache.c +30 -0
index 2543dd7e8..76ca47ca0 100644
@@ -529,6 +529,7 @@ static void __ksmbd_close_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp)
 {
 	struct file *filp;
 	struct ksmbd_lock *smb_lock, *tmp_lock;
+	struct ksmbd_work *cn_work, *cn_tmp;
 
 	fd_limit_close();
 	ksmbd_remove_durable_fd(fp);
@@ -561,6 +562,34 @@ static void __ksmbd_close_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp)
 		kfree(smb_lock);
 	}
 
+	/*
+	 * Complete any CHANGE_NOTIFY left pending on this handle now that
+	 * it is closed. KSMBD never completes CHANGE_NOTIFY spontaneously
+	 * (no real change-notification backend), only on close -- matching
+	 * genuine SMB2/macOS smbfs semantics and avoiding the Finder
+	 * "directory changed, re-enumerate everything" loop.
+	 *
+	 * smb2_notify() on another connection can be adding to
+	 * notify_pendings under fp->f_lock at the same time this handle is
+	 * closed. Splice the list out under that same lock before touching
+	 * it, instead of walking it unlocked -- ksmbd_conn_write() can sleep
+	 * (it takes conn's write mutex), so it must not be called while
+	 * fp->f_lock is held.
+	 */
+	{
+		LIST_HEAD(cn_dispose);
+
+		spin_lock(&fp->f_lock);
+		list_splice_init(&fp->notify_pendings, &cn_dispose);
+		spin_unlock(&fp->f_lock);
+
+		list_for_each_entry_safe(cn_work, cn_tmp, &cn_dispose, notify_entry) {
+			list_del_init(&cn_work->notify_entry);
+			ksmbd_conn_write(cn_work);
+			ksmbd_free_work_struct(cn_work);
+		}
+	}
+
 	/*
 	 * Drop fp's strong reference on conn (taken in ksmbd_open_fd() /
 	 * ksmbd_reopen_durable_fd()).  Durable fps that reached the
@@ -1080,6 +1109,7 @@ struct ksmbd_file *ksmbd_open_fd(struct ksmbd_work *work, struct file *filp)
 	INIT_LIST_HEAD(&fp->blocked_works);
 	INIT_LIST_HEAD(&fp->node);
 	INIT_LIST_HEAD(&fp->lock_list);
+	INIT_LIST_HEAD(&fp->notify_pendings);
 	spin_lock_init(&fp->f_lock);
 	mutex_init(&fp->readdir_lock);
 	atomic_set(&fp->refcount, 1);
fs/smb/server/vfs_cache.h +6 -0
index 111a4e315..2d722c43c 100644
@@ -135,6 +135,12 @@ struct ksmbd_file {
 
 	bool                            is_posix_ctxt;
 	struct durable_owner		owner;
+
+	/*
+	 * 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,
Signature · 1 line
2.43.0
maintainer
#6 lore raw

Re: [PATCH 4/5] ksmbd: defer CHANGE_NOTIFY completion instead of STATUS_NOT_IMPLEMENTED

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

Quoted 8 lines
fs/smb/server/smb2pdu.c +8 -0
+       /*
+        * Transfer ownership of the async id to in_work; it stays reserved
+        * until in_work is freed after the deferred response is sent on
+        * close, so it can't be reused for an unrelated async response.
+        */
+       in_work->async_id = work->async_id;
+       work->async_id = 0;
+       release_async_work(work);

I have one concern about the CHANGE_NOTIFY async lifecycle and CANCEL handling. The notify request is registered as async and then released immediately. After that, the deferred response is kept only on the fp pending list.
However, SMB2 CANCEL lookup walks only conn async_requests. Because of this, a client-issued SMB2 CANCEL cannot find and cancel the deferred CHANGE_NOTIFY anymore. The request remains pending until file close...

Quoted 9 lines
fs/smb/server/smb2pdu.c +8 -0
+
+       spin_lock(&fp->f_lock);
+       list_add_tail(&in_work->notify_entry, &fp->notify_pendings);
+       spin_unlock(&fp->f_lock);
+
+       ksmbd_fd_put(work, fp);
+       work->send_no_response = 1;
+       return 0;
 }
#7 lore raw

PATCH 5/5 ksmbd: implement full-file copy for AAPL ChunkCount=0 COPYCHUNK

In reply to Gaël Blivet-Bailly

From: Gael Blivet <[email protected]>

fsctl_copychunk() treats FSCTL_SRV_COPYCHUNK with ChunkCount=0 as the standard SMB2 "query my copy limits, don't copy anything" request and returns success without ever looking up the file handles. That's correct for compliant SMB2 clients, but macOS Finder's Cmd+D duplicate sends ChunkCount=0 expecting the server to copy the whole file -- so duplicated files (and streams) are left at their just-created 0 bytes while the client reports success.

Scope the full-copy fallback to AAPL-negotiated connections (conn->is_aapl) only, so standard non-Apple SMB2 clients keep the spec-correct query-limits behavior unchanged. Handles both the main data fork (loop over vfs_copy_file_range, since file size doesn't fit in a single wire chunk's 32-bit Length field) and the ADS stream-to-stream path (report the actual xattr bytes copied instead of always reporting 0 when there's no real chunk list to sum).

---
 fs/smb/server/smb2pdu.c | 38 +++++++++++++++++++++++++-------------
 fs/smb/server/vfs.c     | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+), 13 deletions(-)
fs/smb/server/smb2pdu.c +25 -13
index 63beb2bcd..8546cbda0 100644
@@ -8843,23 +8843,35 @@ static int fsctl_copychunk(struct ksmbd_work *work,
 		cpu_to_le32(ksmbd_server_side_copy_max_total_size());
 
 	chunk_count = le32_to_cpu(ci_req->ChunkCount);
-	if (chunk_count == 0)
+	/*
+	 * ChunkCount=0 is the standard SMB2 "query my copy limits" request
+	 * (no data copied) -- but macOS Finder's Cmd+D duplicate sends
+	 * FSCTL_SRV_COPYCHUNK with ChunkCount=0 meaning "copy the whole
+	 * file", relying on the AAPL-negotiated server to do a full copy
+	 * instead. Keep the standard no-op behavior for everyone else.
+	 */
+	if (chunk_count == 0 && !work->conn->is_aapl)
 		goto out;
 	total_size_written = 0;
+	i = 0;
 
-	/* verify the SRV_COPYCHUNK_COPY packet */
-	if (chunk_count > ksmbd_server_side_copy_max_chunk_count() ||
-	    input_count < struct_size(ci_req, Chunks, chunk_count)) {
-		rsp->hdr.Status = STATUS_INVALID_PARAMETER;
-		return -EINVAL;
-	}
+	if (chunk_count) {
+		/* verify the SRV_COPYCHUNK_COPY packet */
+		if (chunk_count > ksmbd_server_side_copy_max_chunk_count() ||
+		    input_count < struct_size(ci_req, Chunks, chunk_count)) {
+			rsp->hdr.Status = STATUS_INVALID_PARAMETER;
+			return -EINVAL;
+		}
 
-	chunks = &ci_req->Chunks[0];
-	for (i = 0; i < chunk_count; i++) {
-		if (le32_to_cpu(chunks[i].Length) == 0 ||
-		    le32_to_cpu(chunks[i].Length) > ksmbd_server_side_copy_max_chunk_size())
-			break;
-		total_size_written += le32_to_cpu(chunks[i].Length);
+		chunks = &ci_req->Chunks[0];
+		for (i = 0; i < chunk_count; i++) {
+			if (le32_to_cpu(chunks[i].Length) == 0 ||
+			    le32_to_cpu(chunks[i].Length) > ksmbd_server_side_copy_max_chunk_size())
+				break;
+			total_size_written += le32_to_cpu(chunks[i].Length);
+		}
+	} else {
+		chunks = &ci_req->Chunks[0];
 	}
 
 	if (i < chunk_count ||
fs/smb/server/vfs.c +41 -0

53 lines not on this page · raw message

Signature · 1 line
2.43.0