ksmbd: add macOS Time Machine / AAPL SMB2 extension support
Subsystems (2)
v2 cover ksmbd: add macOS Time Machine / AAPL SMB2 extension support
From: Gael Blivet <[email protected]>
Link to v1:
https://lore.kernel.org/linux-cifs/[email protected]/
v2 addresses the CHANGE_NOTIFY CANCEL concern raised on v1:smb2_cancel() calls each async request's cancel_fn() while holding conn->request_lock, so cancel_fn must not sleep. Patch 4 now splits this into inline lock-protected list/state manipulation (matching every other cancel_fn in this file) plus a workqueue-deferred send of the actual STATUS_CANCELLED response, which is the one part that sleeps.
Separately: the corresponding ksmbd-tools userspace patch (aapl_model field, "time machine"/"aapl model" config options) has been sent to that project directly, since it's a different repository from this kernel series.
New since v1: patch 6, AAPL READDIR_ATTR V2 support -- real macOS clients negotiate V2 over V1 when both are offered, so v1-only left a gap for current macOS versions.
Also new: patches 1, 3, and 6 are now additionally cross-checked against Apple's published SMBClient kernel source (apple-oss-distributions/SMBClient, smb_smb_2.c) -- everything matched the network-derived understanding v1 was built on, except one inaccurate assumption in patch 3, now corrected.
Patch 5 also gets a correctness fix: the synthesized ChunkCount=0 full-copy response was reporting ChunksWritten=1/ChunkBytesWritten= <file size>, describing a chunk the client never actually sent (it sent zero). Now reports 0/0, matching what's actually been validated against a real client; only TotalBytesWritten carries a value.
Patches 1-4 are otherwise unchanged in substance from v1, re-derived against the current tree (this branch has moved since v1).
Gael Blivet (6):
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
ksmbd: add AAPL READDIR_ATTR V2 support
fs/smb/common/smb2pdu.h | 8 + fs/smb/common/smbglob.h | 1 + fs/smb/server/connection.h | 2 + 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 | 85 +++++++ fs/smb/server/oplock.h | 2 + fs/smb/server/server.h | 2 + fs/smb/server/smb2ops.c | 4 + fs/smb/server/smb2pdu.c | 442 ++++++++++++++++++++++++++++++++-- fs/smb/server/smb2pdu.h | 76 ++++++ fs/smb/server/transport_ipc.c | 9 + fs/smb/server/vfs.c | 88 +++++++ fs/smb/server/vfs.h | 1 + fs/smb/server/vfs_cache.c | 48 ++++ fs/smb/server/vfs_cache.h | 6 + 17 files changed, 758 insertions(+), 23 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/6 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.
Wire format confirmed against Apple's published SMBClient kernel source (apple-oss-distributions/SMBClient, smb_smb_2.c) -- every field here and every SMB2_CRTCTX_AAPL_* constant matches exactly.
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, 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.
---
v1 -> v2: Cross-checked wire format against Apple's published SMBClient kernel source (apple-oss-distributions/SMBClient, smb_smb_2.c) -- confirmed every field and constant matches; no functional change.
fs/smb/common/smbglob.h | 1 + fs/smb/server/connection.h | 1 + fs/smb/server/ksmbd_netlink.h | 4 +- fs/smb/server/oplock.c | 77 +++++++++++++++++++++++++++++++++++ fs/smb/server/oplock.h | 1 + fs/smb/server/server.h | 2 + fs/smb/server/smb2ops.c | 4 ++ fs/smb/server/smb2pdu.c | 56 ++++++++++++++++++++++++- fs/smb/server/smb2pdu.h | 60 +++++++++++++++++++++++++++ fs/smb/server/transport_ipc.c | 9 ++++ 10 files changed, 213 insertions(+), 2 deletions(-)
fs/smb/common/smbglob.h +1 -0
@@ -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
@@ -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
@@ -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 +77 -0
89 lines not on this page · raw message
fs/smb/server/oplock.h +1 -0
@@ -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
@@ -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
@@ -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 +55 -1
80 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
@@ -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)
base-commit: 545c40084bb831530033265b654290ff7be5b090
Signature · 1 line
2.43.0
PATCH v2 2/6 ksmbd: synthesize empty AFP_AfpInfo xattr on first probe
In reply to Gaël Blivet-BaillyFrom: 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 | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
fs/smb/server/smb2pdu.c +24 -0
@@ -2864,6 +2864,30 @@ 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" ) && +test_share_config_flag (fp->tcon->share_conf, +KSMBD_SHARE_FLAG_TIME_MACHINE )) { +/* +* 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. +* +* Scoped to TIME_MACHINE shares, matching the rest of +* the AAPL series -- conn->is_aapl alone isn't a safe +* gate here, since the pre-existing narrow UniqueId=0 +* path can also set it on ordinary, non-Time-Machine +* shares whenever a Mac client happens to negotiate +* AAPL there too. +*/ +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
PATCH v2 3/6 ksmbd: send inline FinderInfo in FIND responses when READDIR_ATTR negotiated
In reply to Gaël Blivet-BaillyFrom: 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; kept for wire parity with Samba, see below)
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.
Reverse-engineered from macOS smbfs.kext network behavior and cross-checked against Samba's vfs_fruit marshalling (source3/smbd/smb2_trans2.c, SMB_FIND_ID_BOTH_DIRECTORY_INFO). Also confirmed against Apple's published SMBClient kernel source (apple-oss-distributions/SMBClient, smb_smb_2.c) -- every field here matches exactly, except ShortNameLength=24: real V1 clients read but never examine that field, so it's kept for wire parity with Samba, not because macOS requires it.
---
v1 -> v2: Cross-checked wire format against Apple's published SMBClient kernel source -- confirmed every field matches, and corrected an inaccurate claim about ShortNameLength=24 being something real clients require (they don't; a comment-only fix).
fs/smb/server/smb2pdu.c | 60 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 5 deletions(-)
fs/smb/server/smb2pdu.c +55 -5
@@ -4733,17 +4733,67 @@ 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; 24 here instead matches +* Samba's vfs_fruit marshalling (smb2_trans2.c) for +* server-to-server wire parity. Apple's own SMBClient +* source (smb_smb_2.c) confirms real macOS clients +* read this field but never examine its value for a +* V1 (non-READDIR_ATTR_V2) connection -- V2 repurposes +* it as a flags field that is interpreted; V1 doesn't. +* Either value is safe here, so keep 24 for parity. +*/ + 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
PATCH v2 4/6 ksmbd: defer CHANGE_NOTIFY completion instead of STATUS_NOT_IMPLEMENTED
In reply to Gaël Blivet-BaillyFrom: 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.
---
v1 -> v2: Addressed the CANCEL deadlock concern raised in review:smb2_notify_cancel_fn() now does only lock-protected list/state manipulation inline under conn->request_lock (matching every other cancel_fn in this file), deferring the one sleeping operation (sending STATUS_CANCELLED via ksmbd_conn_write()) to a workqueue. The close-time drain in __ksmbd_inode_close() changed to match: it pops one entry at a time via list_del_init() under fp->f_lock rather than a bulk list_splice_init(), so a racing cancel_fn can tell if it lost the race the same way, and both sides can never free the same ksmbd_work.
fs/smb/server/ksmbd_work.c | 1 + fs/smb/server/ksmbd_work.h | 2 + fs/smb/server/smb2pdu.c | 226 ++++++++++++++++++++++++++++++++++++- fs/smb/server/vfs_cache.c | 48 ++++++++ fs/smb/server/vfs_cache.h | 6 + 5 files changed, 280 insertions(+), 3 deletions(-)
fs/smb/server/ksmbd_work.c +1 -0
@@ -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
@@ -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 +223 -3
244 lines not on this page · raw message
fs/smb/server/vfs_cache.c +48 -0
66 lines not on this page · raw message
fs/smb/server/vfs_cache.h +6 -0
@@ -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
ChenXiaoSong
reviewerRe: [PATCH v2 4/6] ksmbd: defer CHANGE_NOTIFY completion instead of STATUS_NOT_IMPLEMENTED
It looks like this patch from Gaël has already implemented notify pending and cancel support. I will rebase on this patch.
I will send the remaining ksmbd notify patches soon.
Quoted 28 lines · Gaël Blivet-Bailly
From: Gael Blivet<[email protected]>
smb2_notify()currently returnsSTATUS_NOT_IMPLEMENTEDsynchronously for everyCHANGE_NOTIFYrequest. Genuine SMB2 servers never complete aCHANGE_NOTIFYspontaneously -- it's satisfied only by a real directory change or withSTATUS_NOTIFY_CLEANUPwhen the watched handle is closed. macOS smbfs.kext depends on this deferred-completion contract:
receivingSTATUS_NOT_IMPLEMENTEDinstead makes it hard-freeze on unmount, since it never sees the cleanup it's waiting for.Add a
notify_pendingslist on structksmbd_file(protected by the existingf_lock) and anotify_entrylist_headon structksmbd_workto link onto it.smb2_notify()now repliesSTATUS_PENDINGimmediately and queues a deferredSTATUS_NOTIFY_CLEANUPresponse 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 underfp->f_lockfirst, 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, andksmbd_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.Signed-off-by Gael Blivet<[email protected]>
Signature · 3 lines
ChenXiaoSong <[email protected]> Chinese Homepage: https://chenxiaosong.com English Homepage: https://chenxiaosong.com/en
ChenXiaoSong
reviewerRe: [PATCH v2 4/6] ksmbd: defer CHANGE_NOTIFY completion instead of STATUS_NOT_IMPLEMENTED
In reply to Gaël Blivet-BaillyHi Gaël and Namjae,
Do you think it would be better to send STATUS_CANCELLED in smb2_notify()?
Quoted 20 lines · Gaël Blivet-Bailly
fs/smb/server/smb2pdu.c +20
-0+static void smb2_notify_cancel_deferred (struct work_struct *w) +{ +struct notify_cancel_ctx *ctx = + container_of(w, struct notify_cancel_ctx, work); +struct ksmbd_work *in_work = ctx->in_work; +struct smb2_hdr *in_hdr; + + in_hdr =smb_get_msg (in_work->response_buf); + in_hdr->Status =STATUS_CANCELLED ; +ksmbd_conn_write (in_work); +ksmbd_free_work_struct (in_work); +kfree (ctx); +} + +static void smb2_notify_cancel_fn (void **argv) +{ + ... +INIT_WORK (&ctx->work, smb2_notify_cancel_deferred); +schedule_work (&ctx->work); +}
Signature · 3 lines
ChenXiaoSong <[email protected]> Chinese Homepage: https://chenxiaosong.com English Homepage: https://chenxiaosong.com/en
Re: [PATCH v2 4/6] ksmbd: defer CHANGE_NOTIFY completion instead of STATUS_NOT_IMPLEMENTED
Hi ChenXiaoSong,
Because cancel_fn runs under conn->request_lock, smb2_cancel() holds it as a spinlock while walking async_requests, so cancel_fn can’t sleep. ksmbd_conn_write() takes conn->srv_mutex and can block on the actual socket write too, so calling it there directly would mean sleeping under a spinlock.
smb2_notify_cancel_fn() only does the non-sleeping parts inline (list removal, freeing cancel_argv, releasing the async id), same as smb2_remove_blocked_lock() elsewhere in this file, and pushes the actual response send to a workqueue.
Quoted 5 lines · ChenXiaoSong
Hi Gaël and Namjae,
Do you think it would be better to send
STATUS_CANCELLEDinsmb2_notify()?
ChenXiaoSong
reviewerRe: [PATCH v2 4/6] ksmbd: defer CHANGE_NOTIFY completion instead of STATUS_NOT_IMPLEMENTED
Hi Gaël,
Thanks for your patches.
I mean sending STATUS_CANCELLED in smb2_notify() (not in smb2_notify_cancel_fn()). The event notification and STATUS_CANCELLED response should be handled in smb2_notify().
You can refer to the implementation of smb2_lock():
```
int smb2_lock()
{
smb2_send_interim_resp(work, STATUS_PENDING);
ksmbd_vfs_posix_lock_wait(flock); // woken up by
smb2_remove_blocked_lock()
smb2_send_interim_resp(work, STATUS_CANCELLED);
work->send_no_response = 1
}
```I have already implemented the complete notify feature, but since my patch series is quite large, I still need to make some changes and split it into smaller patches that are easier to review.
Quoted 3 lines · Gaël Blivet
Because
cancel_fnruns underconn->request_lock,smb2_cancel()holds it as a spinlock while walkingasync_requests, socancel_fncan’t sleep.ksmbd_conn_write()takesconn->srv_mutexand can block on the actual socket write too, so calling it there directly would mean sleeping under a spinlock.
smb2_notify_cancel_fn()only does the non-sleeping parts inline (list removal, freeingcancel_argv, releasing the async id), same assmb2_remove_blocked_lock()elsewhere in this file, and pushes the actual response send to a workqueue.
Signature · 3 lines
ChenXiaoSong <[email protected]> Chinese Homepage: https://chenxiaosong.com English Homepage: https://chenxiaosong.com/en
ChenXiaoSong
reviewerRe: [PATCH v2 4/6] ksmbd: defer CHANGE_NOTIFY completion instead of STATUS_NOT_IMPLEMENTED
Would it be better to do it like the following?
```
int smb2_notify()
{
setup_async_work(..., smb2_notify_cancel_fn, ...);
smb2_send_interim_resp(work, STATUS_PENDING);
wait_for_completion_interruptible(); // woken up by
smb2_notify_cancel_fn()
if (work->state == KSMBD_WORK_CANCELLED) {
smb2_send_interim_resp(work, STATUS_CANCELLED);
work->send_no_response = 1;
goto out;
}
// Add my implementation of notification events here.
}
```Quoted 27 lines · ChenXiaoSong
Hi Gaël,
Thanks for your patches.
I mean sending
STATUS_CANCELLEDinsmb2_notify()(not insmb2_notify_cancel_fn()). The event notification andSTATUS_CANCELLEDresponse should be handled insmb2_notify().You can refer to the implementation of
smb2_lock():``` int smb2_lock() { smb2_send_interim_resp(work, STATUS_PENDING); ksmbd_vfs_posix_lock_wait(flock); // woken up by smb2_remove_blocked_lock() smb2_send_interim_resp(work, STATUS_CANCELLED); work->send_no_response = 1 } ```I have already implemented the complete notify feature, but since my patch series is quite large, I still need to make some changes and split it into smaller patches that are easier to review.
Quoted 3 lines · Gaël Blivet
Because
cancel_fnruns underconn->request_lock,smb2_cancel()holds it as a spinlock while walkingasync_requests, socancel_fncan’t sleep.ksmbd_conn_write()takesconn->srv_mutexand can block on the actual socket write too, so calling it there directly would mean sleeping under a spinlock.
smb2_notify_cancel_fn()only does the non-sleeping parts inline (list removal, freeingcancel_argv, releasing the async id), same assmb2_remove_blocked_lock()elsewhere in this file, and pushes the actual response send to a workqueue.
Signature · 3 lines
ChenXiaoSong <[email protected]> Chinese Homepage: https://chenxiaosong.com English Homepage: https://chenxiaosong.com/en
Re: [PATCH v2 4/6] ksmbd: defer CHANGE_NOTIFY completion instead of STATUS_NOT_IMPLEMENTED
Hi ChenXiaoSong,
Makes sense, sorry for the misunderstanding. Could be cleaner, but doesn't wait_for_completion_interruptible() still block a worker thread from ksmbd_wq for as long as the watch stays open?
Happy to help align the merged patch with your series once it's up.
Quoted 20 lines · ChenXiaoSong
Would it be better to do it like the following?
```
intsmb2_notify(){ setup_async_work(..., smb2_notify_cancel_fn, ...); smb2_send_interim_resp(work, STATUS_PENDING); wait_for_completion_interruptible(); // woken up by smb2_notify_cancel_fn() if (work->state == KSMBD_WORK_CANCELLED) { smb2_send_interim_resp(work, STATUS_CANCELLED); work->send_no_response = 1; goto out; } // Add my implementation of notification events here. }
ChenXiaoSong
reviewerRe: [PATCH v2 4/6] ksmbd: defer CHANGE_NOTIFY completion instead of STATUS_NOT_IMPLEMENTED
Hi Gaël,
Yes, before the cancel response or event notification response is sent, the notify request has not completed yet, so the worker will remain waiting.
If you find that this approach causes any issues, we can refactor it and use a better solution.
Quoted 3 lines · Gaël Blivet
Makes sense, sorry for the misunderstanding. Could be cleaner, but doesn't
wait_for_completion_interruptible()still block a worker thread fromksmbd_wqfor as long as the watch stays open?Happy to help align the merged patch with your series once it's up.
Signature · 3 lines
ChenXiaoSong <[email protected]> Chinese Homepage: https://chenxiaosong.com English Homepage: https://chenxiaosong.com/en
Re: [PATCH v2 4/6] ksmbd: defer CHANGE_NOTIFY completion instead of STATUS_NOT_IMPLEMENTED
We can keep that in mind and see in practice how it performs (for example with Time Machine, that can open long-running watches).
Quoted 5 lines · ChenXiaoSong
Yes, before the cancel response or event notification response is sent, the notify request has not completed yet, so the worker will remain waiting.
If you find that this approach causes any issues, we can refactor it and use a better solution.
PATCH v2 5/6 ksmbd: implement full-file copy for AAPL ChunkCount=0 COPYCHUNK
In reply to Gaël Blivet-BaillyFrom: 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/stream -- so duplicated files are left at their just-created 0 bytes while the client reports success.
Scope the full-copy fallback to AAPL-negotiated connections on a Time Machine share (conn->is_aapl && KSMBD_SHARE_FLAG_TIME_MACHINE) only, so standard non-Apple SMB2 clients, and AAPL-negotiated clients on ordinary shares, keep the spec-correct query-limits behavior unchanged.
Re-derived against the copychunk rework in
commit 8afe741dbd67 ("ksmbd: support copychunk for alternate data streams") and commit 165e5b86fd2c ("ksmbd: handle AAPL stream copy length mismatch"):
both streams and regular files now share a single chunk_count == 0 fast path added right after src_file_size is computed, reusing the same buffered-copy helper and vfs_copy_file_range()/COPY_FILE_SPLICE fallback the existing per-chunk loop already uses, rather than the separate xattr-specific get/setxattr path this used before that rework.
ChunksWritten/ChunkBytesWritten are 0 in the response: this is a synthesized whole-file copy, not a response to any chunk descriptor the client actually sent (it sent none), so there's no real chunk to report the count/size of. Only TotalBytesWritten is meaningful here.
---
v1 -> v2: Re-derived against the copychunk rework that landed upstream since v1 (commit 8afe741dbd67, commit 165e5b86fd2c), which replaced the separate xattr-specific stream-copy path this used with a unified chunk_count == 0 fast path shared by streams and regular files. Also fixed ChunksWritten/ChunkBytesWritten in the response: v1 reported ChunksWritten=1 and a nonzero ChunkBytesWritten, describing a chunk the client never sent (it sent zero); now both are 0, matching a value actually validated against a real client.
fs/smb/server/smb2pdu.c | 47 +++++++++++++++++++++++--------- fs/smb/server/vfs.c | 59 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 13 deletions(-)
fs/smb/server/smb2pdu.c +34 -13
@@ -8934,23 +8934,44 @@ 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. +* +* Gate on the TIME_MACHINE share flag, not just conn->is_aapl: +* that flag alone has ambiguous provenance -- the pre-existing +* narrow UniqueId=0 path can also set it on ordinary, +* non-Time-Machine shares, and this series' stated design keeps +* every AAPL-driven behavior opt-in per share. +*/ +if (chunk_count ==0 && + !(work->conn->is_aapl && +test_share_config_flag (work->tcon->share_conf, +KSMBD_SHARE_FLAG_TIME_MACHINE ))) 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 +59 -0
65 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v2 6/6 ksmbd: add AAPL READDIR_ATTR V2 support
In reply to Gaël Blivet-BaillyFrom: Gael Blivet <[email protected]>
Extends the existing V1 inline-FinderInfo mechanism (SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) with the V2 variant:
byte-identical layout otherwise, confirmed against Apple's actual SMBClient kernel source (apple-oss-distributions/SMBClient/kernel/netsmb/smb_smb_2.c), except the ShortNameLength+Reserved bytes (ignored outright by V1 clients) become a single flags field that V2 clients actually interpret.
Negotiation: when a client's own client_caps requests V2 (SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR_V2), advertise V2 instead of V1 in the server's own server_caps reply -- they're mutually exclusive on the wire, not both set together.
Wire format: the only currently-defined V2 flag, AAPL_READDIR_ATTR_V2_NO_XATTR, signals that an item has no xattrs/streams so the client can skip a separate query. Compute this per-entry in ksmbd_vfs_fill_dentry_attrs() by checking for any xattr under the XATTR_NAME_STREAM ("user.DosStream.") prefix -- a reliable, distinct marker for genuine ADS/stream xattrs, unlike DOSATTRIB or ACL xattrs which live under different prefixes, so this can't false-positive into telling Finder a file has no extra data when it actually does. Only computed when a V2 connection is active, to avoid the extra listxattr() call otherwise.
V1's fixed ShortNameLength=24 convention (real macOS clients ignore the value outright per the same client source, so it's cosmetic parity with other real servers, not a functional requirement) is kept V1-only rather than reused as a V2 base value -- V2 clients do interpret this field, so it needs a clean 0-or-flag value, not a leftover V1 constant that happens not to collide with the one defined flag bit today.
Confirmed via live diagnostics that macOS actually negotiates and uses V2 (client_caps bit 0x10 set) rather than falling back to V1 or ignoring the capability.
---
New for v2.
fs/smb/common/smb2pdu.h | 8 ++++++++ fs/smb/server/connection.h | 1 + fs/smb/server/oplock.c | 12 ++++++++++-- fs/smb/server/oplock.h | 3 ++- fs/smb/server/smb2pdu.c | 33 +++++++++++++++++++++++++++++++-- fs/smb/server/smb2pdu.h | 16 ++++++++++++++++ fs/smb/server/vfs.c | 29 +++++++++++++++++++++++++++++ fs/smb/server/vfs.h | 1 + 8 files changed, 98 insertions(+), 5 deletions(-)
fs/smb/common/smb2pdu.h +8 -0
@@ -1261,6 +1261,14 @@ struct create_mxac_req {#define SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE 2#define SMB2_CRTCTX_AAPL_UNIX_BASED 4#define SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE 8 +/* +* V2 extends the same inline-FinderInfo mechanism as +* SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR with an added flags field, +* confirmed byte-identical to V1 otherwise against Apple's actual +* SMBClient kernel source (apple-oss-distributions/SMBClient). Mutually +* exclusive with the V1 bit on the wire, not both set together. +*/ +#define SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR_V2 16/* "AAPL" Volume Capabilities bitmap */ #define SMB2_CRTCTX_AAPL_SUPPORT_RESOLVE_ID 1
fs/smb/server/connection.h +1 -0
@@ -125,6 +125,7 @@ struct ksmbd_conn {atomic_t refcnt;bool is_aapl;bool aapl_readdir_attr;/* READDIR_ATTR negotiated */ +bool aapl_readdir_attr_v2;/* V2 specifically */ struct work_struct release_work; };
fs/smb/server/oplock.c +10 -2
@@ -2156,11 +2156,15 @@ void create_posix_rsp_buf(char *cc, struct ksmbd_file *fp)* * Sending reply_bitmap with MODEL_INFO set but no model string causes * smbfs.kext to enter a broken disconnect path requiring a macOS reboot. +* @readdir_attr_v2: advertise SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR_V2 +* instead of the V1 bit */ -void create_aapl_rsp_buf (char *cc,__u64 vol_caps,__u64 req_bitmap) +void create_aapl_rsp_buf (char *cc,__u64 vol_caps,__u64 req_bitmap, +bool readdir_attr_v2) {struct create_aapl_rsp *buf;u64 reply_bitmap; +u64 server_caps;u32 data_len; buf = (struct create_aapl_rsp *)cc;
@@ -2186,8 +2190,12 @@ void create_aapl_rsp_buf(char *cc, __u64 vol_caps, __u64 req_bitmap) buf->cmd =cpu_to_le32 (SMB2_CRTCTX_AAPL_SERVER_QUERY ); buf->reply_bitmap =cpu_to_le64 (reply_bitmap); + server_caps =AAPL_SERVER_CAPS_KSMBD ; +if (readdir_attr_v2) + server_caps = (server_caps & ~SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR ) | +SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR_V2 ; buf->server_caps = (reply_bitmap &SMB2_CRTCTX_AAPL_SERVER_CAPS ) ? -cpu_to_le64 (AAPL_SERVER_CAPS_KSMBD ) :0 ; +cpu_to_le64 (server_caps) :0 ; buf->vol_caps = (reply_bitmap &SMB2_CRTCTX_AAPL_VOLUME_CAPS ) ?cpu_to_le64 (vol_caps) :0 ;
fs/smb/server/oplock.h +2 -1
@@ -125,7 +125,8 @@ 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); +void create_aapl_rsp_buf (char *cc,__u64 vol_caps,__u64 req_bitmap, +bool readdir_attr_v2);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/smb2pdu.c +31 -2
@@ -4404,12 +4404,23 @@ int smb2_open(struct ksmbd_work *work)if (aapl_ctxt) {if (aapl_client_caps &SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR ) conn->aapl_readdir_attr = true; +/* +* V2 extends the same inline-FinderInfo mechanism (see +* smb2pdu.h), so a V2-requesting client also gets +* aapl_readdir_attr treatment -- the reply just advertises +* the V2 bit instead of the V1 one (create_aapl_rsp_buf). +*/ +if (aapl_client_caps &SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR_V2 ) { + conn->aapl_readdir_attr = true; + conn->aapl_readdir_attr_v2 = true; + } contxt_cnt++;create_aapl_rsp_buf (rsp->Buffer +le32_to_cpu (rsp->CreateContextsLength),SMB2_CRTCTX_AAPL_FULL_SYNC , - aapl_req_bitmap); + aapl_req_bitmap, + conn->aapl_readdir_attr_v2);le32_add_cpu (&rsp->CreateContextsLength, conn->vals->create_aapl_size); iov_len += conn->vals->create_aapl_size;
@@ -4753,6 +4764,11 @@ static int smb2_populate_readdir_entry(struct ksmbd_conn *conn, int info_level,* the file extension for icon lookup) * Reserved2 = Unix mode bits (uint16 LE) * Reparse-point tag is indicated via ExtFileAttributes, not EaSize. +* +* V2 (conn->aapl_readdir_attr_v2): ShortNameLength+Reserved +* are read as a single flags field instead of being ignored +* -- see smb2pdu.h for the wire-format confirmation and +* AAPL_READDIR_ATTR_V2_NO_XATTR's meaning. */ __le32 reparse_tag =smb2_get_reparse_tag_special_file (ksmbd_kstat->kstat->mode);
@@ -4784,7 +4800,20 @@ static int smb2_populate_readdir_entry(struct ksmbd_conn *conn, int info_level,* it as a flags field that is interpreted; V1 doesn't. * Either value is safe here, so keep 24 for parity. */ - fibdinfo->ShortNameLength =24 ; +if (conn->aapl_readdir_attr_v2) { +/* +* V2 repurposes this field as flags (see comment +* above) -- 24 is a V1-only convention that real +* macOS clients ignore outright, so don't reuse it +* here as a base value for a field V2 clients +* actually interpret. +*/ + fibdinfo->ShortNameLength =0 ; +if (!ksmbd_kstat->has_ads_stream) + fibdinfo->ShortNameLength =AAPL_READDIR_ATTR_V2_NO_XATTR ; + }else { + fibdinfo->ShortNameLength =24 ; + }memset (fibdinfo->ShortName,0 ,sizeof (fibdinfo->ShortName)); fibdinfo->Reserved2 =cpu_to_le16 (ksmbd_kstat->kstat->mode &0xffff ); }else {
fs/smb/server/smb2pdu.h +16 -0
@@ -96,6 +96,22 @@ struct preauth_integrity_info { SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE | \ SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) +/* +* READDIR_ATTR_V2 (SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR_V2, see +* fs/smb/common/smb2pdu.h) extends the same inline-FinderInfo mechanism +* above with a flags field, confirmed byte-identical to V1 otherwise +* against Apple's actual SMBClient kernel source +* (apple-oss-distributions/SMBClient). When the client's own client_caps +* requests V2, the server advertises V2 instead of V1 in its own +* server_caps reply; V1 and V2 are mutually exclusive on the wire, not +* both set together. The wire format's ShortNameLength+Reserved +* (ignored in V1) become a single flags field in V2 -- +* AAPL_READDIR_ATTR_V2_NO_XATTR is the only flag bit currently defined, +* signaling the item has no xattrs/streams so the client can skip a +* separate query. +*/ +#define AAPL_READDIR_ATTR_V2_NO_XATTR 0x01 +/* Model string: up to 31 ASCII chars */ #define AAPL_MODEL_MAX_CHARS 31#define AAPL_MODEL_UTF16_BYTES (AAPL_MODEL_MAX_CHARS * 2)
fs/smb/server/vfs.c +29 -0
@@ -1684,6 +1684,35 @@ int ksmbd_vfs_fill_dentry_attrs(struct ksmbd_work *work, } } +/* +* Only pay for this when it'll actually be used: AAPL +* READDIR_ATTR_V2's flags field (AAPL_READDIR_ATTR_V2_NO_XATTR) is +* the only consumer. XATTR_NAME_STREAM ("user.DosStream.") is a +* reliable, distinct prefix for genuine ADS/stream xattrs -- unlike +* DOSATTRIB or ACL xattrs, which live under different prefixes, so +* this can't false-positive into telling Finder a file has no extra +* data when it actually does. +*/ + ksmbd_kstat->has_ads_stream = false; +if (work->conn->aapl_readdir_attr_v2) { +char *xattr_list =NULL , *name; +ssize_t xattr_list_len; + + xattr_list_len =ksmbd_vfs_listxattr (dentry, &xattr_list); +if (xattr_list_len >0 ) { +for (name = xattr_list; + name - xattr_list < xattr_list_len; + name +=strlen (name) +1 ) { +if (!strncmp (name,XATTR_NAME_STREAM , +XATTR_NAME_STREAM_LEN )) { + ksmbd_kstat->has_ads_stream = true; +break ; + } + } + } +kvfree (xattr_list); + } +return 0 ; }
fs/smb/server/vfs.h +1 -0
@@ -70,6 +70,7 @@ struct ksmbd_kstat {struct kstat *kstat;unsigned long long create_time;__le32 file_attributes; +bool has_ads_stream;/* AAPL READDIR_ATTR V2 xattr-presence flag */ };int ksmbd_vfs_lock_parent (struct dentry *parent,struct dentry *child);
Signature · 1 line
2.43.0
Quoted 38 lines · Gaël Blivet-Bailly
From: Gael Blivet <[email protected]>
Link to v1:
https://lore.kernel.org/linux-cifs/[email protected]/v2 addresses the
CHANGE_NOTIFYCANCEL concern raised on v1:smb2_cancel()calls each async request'scancel_fn()while holdingconn->request_lock, socancel_fnmust not sleep. Patch 4 now splits this into inline lock-protected list/state manipulation (matching every othercancel_fnin this file) plus a workqueue-deferred send of the actualSTATUS_CANCELLEDresponse, which is the one part that sleeps.Separately: the corresponding ksmbd-tools userspace patch (
aapl_modelfield, "time machine"/"aapl model" config options) has been sent to that project directly, since it's a different repository from this kernel series.New since v1: patch 6, AAPL
READDIR_ATTRV2 support -- real macOS clients negotiate V2 over V1 when both are offered, so v1-only left a gap for current macOS versions.Also new: patches 1, 3, and 6 are now additionally cross-checked against Apple's published SMBClient kernel source (apple-oss-distributions/SMBClient,
smb_smb_2.c) -- everything matched the network-derived understanding v1 was built on, except one inaccurate assumption in patch 3, now corrected.Patch 5 also gets a correctness fix: the synthesized ChunkCount=0 full-copy response was reporting ChunksWritten=1/ChunkBytesWritten= <file size>, describing a chunk the client never actually sent (it sent zero). Now reports 0/0, matching what's actually been validated against a real client; only TotalBytesWritten carries a value.
Patches 1-4 are otherwise unchanged in substance from v1, re-derived against the current tree (this branch has moved since v1).
Quoted 9 lines
Gael Blivet (6):
ksmbd: add Apple AAPLkAAPL_SERVER_QUERYcreate context support
ksmbd: synthesize emptyAFP_AfpInfoxattr on first probe
ksmbd: send inline FinderInfo in FIND responses whenREADDIR_ATTR
negotiated
ksmbd: deferCHANGE_NOTIFYcompletion instead of
STATUS_NOT_IMPLEMENTED
ksmbd: implement full-file copy for AAPL ChunkCount=0 COPYCHUNK
ksmbd: add AAPLREADDIR_ATTRV2 support
Applied them to #ksmbd-for-next-next.
Thanks!