All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ian Bridges <icb@fastmail.org>
To: linkinjeon@kernel.org
Cc: senozhatsky@chromium.org, tom@talpey.com,
	chenxiaosong@chenxiaosong.com, gael.blivet@gmail.com,
	linux-cifs@vger.kernel.org, Ian Bridges <icb@fastmail.org>,
	stable@vger.kernel.org
Subject: [PATCH v2 1/2] ksmbd: share one stream object between the handles of a named stream
Date: Tue,  1 Dec 2026 19:29:40 -0600	[thread overview]
Message-ID: <20261202012941.2332340-2-icb@fastmail.org> (raw)
In-Reply-To: <20261202012941.2332340-1-icb@fastmail.org>

The handles of a named stream share no state. Each handle keeps its
own copy of the stream name, and two handles of one stream are matched
only by comparing names. So state that belongs to the stream, such as
its delete mark, has no place to live.

Make struct stream an object per named stream of an open file. It is
kept on the ksmbd_inode and shared by the handles of the stream, and
each handle holds a reference from its open until its final close. The
file position stays with the handle. The share mode check now compares
the stream objects of two handles of the same inode instead of their
names.

There is no change in behaviour.

Cc: stable@vger.kernel.org
Signed-off-by: Ian Bridges <icb@fastmail.org>
---
 fs/smb/server/smb2pdu.c    | 46 ++++++++++----------
 fs/smb/server/smb_common.c |  7 ++--
 fs/smb/server/vfs.c        | 20 ++++-----
 fs/smb/server/vfs_cache.c  | 86 +++++++++++++++++++++++++++++++++++---
 fs/smb/server/vfs_cache.h  | 25 ++++++++---
 5 files changed, 140 insertions(+), 44 deletions(-)

diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index 0e0946bd5684..681036019537 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -3689,7 +3689,7 @@ static int smb2_set_ea(struct smb2_ea_info *eabuf, unsigned int buf_len,
 static noinline int smb2_set_stream_name_xattr(const struct path *path,
 					       struct ksmbd_file *fp,
 					       char *stream_name, int s_type,
-					       struct stream *stream,
+					       char **name, ssize_t *size,
 					       bool *created)
 {
 	const struct mnt_idmap *idmap = mnt_idmap(path->mnt);
@@ -3704,8 +3704,8 @@ static noinline int smb2_set_stream_name_xattr(const struct path *path,
 	if (rc)
 		return rc;
 
-	stream->name = xattr_stream_name;
-	stream->size = xattr_stream_size;
+	*name = xattr_stream_name;
+	*size = xattr_stream_size;
 
 	/* Keep the existing xattr's case for subsequent writes and removal. */
 	rc = ksmbd_vfs_casexattr_len(idmap,
@@ -3766,7 +3766,7 @@ static noinline int smb2_set_stream_name_xattr(const struct path *path,
 }
 
 /*
- * fp->stream.size is the byte length of the mangled xattr *name*
+ * fp->stream->size is the byte length of the mangled xattr *name*
  * (used as attr_name_len when looking the xattr up), not the size of
  * the xattr's value. Reporting it as EndOfFile/AllocationSize for a
  * stream handle is wrong -- query the xattr's actual value length
@@ -3776,8 +3776,8 @@ static loff_t ksmbd_stream_eof(struct ksmbd_file *fp)
 {
 	ssize_t slen = ksmbd_vfs_casexattr_len(file_mnt_idmap(fp->filp),
 					       fp->filp->f_path.dentry,
-					       fp->stream.name,
-					       fp->stream.size, NULL);
+					       fp->stream->name,
+					       fp->stream->size, NULL);
 	return slen < 0 ? 0 : (loff_t)slen;
 }
 
@@ -4436,9 +4436,9 @@ static int smb2_app_instance_stream_matches(struct ksmbd_file *fp,
 	if (rc)
 		return rc;
 
-	*matches = fp->stream.name && fp->stream.size >= 0 &&
-		   (size_t)fp->stream.size == xattr_stream_size &&
-		   !strncasecmp(fp->stream.name, xattr_stream_name,
+	*matches = ksmbd_stream_fd(fp) && fp->stream->size >= 0 &&
+		   (size_t)fp->stream->size == xattr_stream_size &&
+		   !strncasecmp(fp->stream->name, xattr_stream_name,
 				xattr_stream_size);
 	kfree(xattr_stream_name);
 	return 0;
@@ -5371,23 +5371,27 @@ int smb2_open(struct ksmbd_work *work)
 	}
 
 	if (stream_name) {
-		struct stream stream = {};
+		char *xattr_name = NULL;
+		ssize_t xattr_size = 0;
 
 		rc = smb2_set_stream_name_xattr(&path,
 						fp,
 						stream_name,
 						s_type,
-						&stream,
+						&xattr_name,
+						&xattr_size,
 						&stream_created);
 		if (rc) {
-			kfree(stream.name);
+			kfree(xattr_name);
 			goto err_out;
 		}
 		/*
 		 * Attach the stream only on success, so that a failed lookup
 		 * or creation cannot delete a stream when fp is closed.
 		 */
-		fp->stream = stream;
+		rc = ksmbd_stream_get(fp, xattr_name, xattr_size);
+		if (rc)
+			goto err_out;
 		file_info = FILE_CREATED;
 	}
 
@@ -7544,7 +7548,7 @@ static int get_file_all_info(struct ksmbd_work *work,
 	if (ksmbd_stream_fd(fp) == false)
 		file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
 	else
-		file_info->CurrentByteOffset = cpu_to_le64(fp->stream.pos);
+		file_info->CurrentByteOffset = cpu_to_le64(fp->stream_pos);
 	file_info->Mode = fp->coption;
 	file_info->AlignmentRequirement = 0;
 	conv_len = smbConvertToUTF16((__le16 *)file_info->FileName, filename,
@@ -7596,8 +7600,8 @@ static char *smb2_get_normalized_stream_name(struct ksmbd_file *fp)
 	     name += strlen(name) + 1) {
 		char *type;
 
-		if (strlen(name) + 1 != fp->stream.size ||
-		    strncasecmp(name, fp->stream.name, fp->stream.size - 1))
+		if (strlen(name) + 1 != fp->stream->size ||
+		    strncasecmp(name, fp->stream->name, fp->stream->size - 1))
 			continue;
 
 		name += XATTR_NAME_STREAM_LEN;
@@ -7896,7 +7900,7 @@ static void get_file_position_info(struct smb2_query_info_rsp *rsp,
 	if (ksmbd_stream_fd(fp) == false)
 		file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
 	else
-		file_info->CurrentByteOffset = cpu_to_le64(fp->stream.pos);
+		file_info->CurrentByteOffset = cpu_to_le64(fp->stream_pos);
 
 	rsp->OutputBufferLength =
 		cpu_to_le32(sizeof(struct smb2_file_pos_info));
@@ -8955,15 +8959,15 @@ static int smb2_rename(struct ksmbd_work *work,
 
 		/* A handle opened without a stream has no source to copy. */
 		if (ksmbd_stream_fd(fp)) {
-			if (!strcasecmp(xattr_stream_name, fp->stream.name)) {
+			if (!strcasecmp(xattr_stream_name, fp->stream->name)) {
 				kfree(xattr_stream_name);
 				goto out;
 			}
 
 			stream_buf_len = ksmbd_vfs_getcasexattr(file_mnt_idmap(fp->filp),
 								fp->filp->f_path.dentry,
-								fp->stream.name,
-								fp->stream.size,
+								fp->stream->name,
+								fp->stream->size,
 								&stream_buf);
 			if (stream_buf_len < 0) {
 				rc = stream_buf_len;
@@ -9381,7 +9385,7 @@ static int set_file_position_info(struct ksmbd_file *fp,
 	else {
 		if (current_byte_offset > XATTR_SIZE_MAX)
 			current_byte_offset = XATTR_SIZE_MAX;
-		fp->stream.pos = current_byte_offset;
+		fp->stream_pos = current_byte_offset;
 	}
 	return 0;
 }
diff --git a/fs/smb/server/smb_common.c b/fs/smb/server/smb_common.c
index 6595d40b06bc..4289e78ea743 100644
--- a/fs/smb/server/smb_common.c
+++ b/fs/smb/server/smb_common.c
@@ -726,9 +726,10 @@ int ksmbd_smb_check_shared_mode(struct file *filp, struct ksmbd_file *curr_fp)
 		if (filp == prev_fp->filp)
 			continue;
 
-		if (ksmbd_stream_fd(prev_fp) && ksmbd_stream_fd(curr_fp))
-			if (strcmp(prev_fp->stream.name, curr_fp->stream.name))
-				continue;
+		/* Both handles are on one inode, so one stream is one object. */
+		if (ksmbd_stream_fd(prev_fp) && ksmbd_stream_fd(curr_fp) &&
+		    prev_fp->stream != curr_fp->stream)
+			continue;
 
 		if (prev_fp->attrib_only != curr_fp->attrib_only)
 			continue;
diff --git a/fs/smb/server/vfs.c b/fs/smb/server/vfs.c
index dd0b4ee549a6..2df2d7483c5e 100644
--- a/fs/smb/server/vfs.c
+++ b/fs/smb/server/vfs.c
@@ -275,8 +275,8 @@ static int ksmbd_vfs_stream_read(struct ksmbd_file *fp, char *buf, loff_t *pos,
 	saved_cred = override_creds(fp->filp->f_cred);
 	v_len = ksmbd_vfs_getcasexattr(file_mnt_idmap(fp->filp),
 				       fp->filp->f_path.dentry,
-				       fp->stream.name,
-				       fp->stream.size,
+				       fp->stream->name,
+				       fp->stream->size,
 				       &stream_buf);
 	revert_creds(saved_cred);
 	if ((int)v_len <= 0)
@@ -289,7 +289,7 @@ static int ksmbd_vfs_stream_read(struct ksmbd_file *fp, char *buf, loff_t *pos,
 
 	if (v_len - *pos < count)
 		count = v_len - *pos;
-	fp->stream.pos = v_len;
+	fp->stream_pos = v_len;
 
 	memcpy(buf, &stream_buf[*pos], count);
 
@@ -425,8 +425,8 @@ static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos,
 	saved_cred = override_creds(fp->filp->f_cred);
 	v_len = ksmbd_vfs_getcasexattr(idmap,
 				       fp->filp->f_path.dentry,
-				       fp->stream.name,
-				       fp->stream.size,
+				       fp->stream->name,
+				       fp->stream->size,
 				       &stream_buf);
 	if (v_len < 0) {
 		pr_err("not found stream in xattr : %zd\n", v_len);
@@ -453,7 +453,7 @@ static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos,
 
 	err = ksmbd_vfs_setxattr(idmap,
 				 &fp->filp->f_path,
-				 fp->stream.name,
+				 fp->stream->name,
 				 (void *)stream_buf,
 				 size,
 				 0,
@@ -463,7 +463,7 @@ static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos,
 	if (err < 0)
 		goto out;
 	else
-		fp->stream.pos = write_end;
+		fp->stream_pos = write_end;
 	err = 0;
 out:
 	kvfree(stream_buf);
@@ -494,7 +494,7 @@ int ksmbd_vfs_stream_truncate(struct ksmbd_file *fp, loff_t newsize)
 
 	saved_cred = override_creds(fp->filp->f_cred);
 	v_len = ksmbd_vfs_getcasexattr(idmap, fp->filp->f_path.dentry,
-				       fp->stream.name, fp->stream.size,
+				       fp->stream->name, fp->stream->size,
 				       &stream_buf);
 	if (v_len < 0) {
 		err = v_len;
@@ -516,7 +516,7 @@ int ksmbd_vfs_stream_truncate(struct ksmbd_file *fp, loff_t newsize)
 			memcpy(new_buf, stream_buf, v_len);
 	}
 
-	err = ksmbd_vfs_setxattr(idmap, &fp->filp->f_path, fp->stream.name,
+	err = ksmbd_vfs_setxattr(idmap, &fp->filp->f_path, fp->stream->name,
 				 new_buf, newsize, 0, true);
 	if (new_buf != stream_buf)
 		kvfree(new_buf);
@@ -2245,7 +2245,7 @@ int ksmbd_vfs_copy_file_ranges(struct ksmbd_work *work,
 		src_file_size = ksmbd_vfs_casexattr_len(
 				file_mnt_idmap(src_fp->filp),
 				src_fp->filp->f_path.dentry,
-				src_fp->stream.name, src_fp->stream.size, NULL);
+				src_fp->stream->name, src_fp->stream->size, NULL);
 		revert_creds(saved_cred);
 		if (src_file_size < 0)
 			return src_file_size;
diff --git a/fs/smb/server/vfs_cache.c b/fs/smb/server/vfs_cache.c
index 7db0a52dd4ae..4456d34190cb 100644
--- a/fs/smb/server/vfs_cache.c
+++ b/fs/smb/server/vfs_cache.c
@@ -12,6 +12,8 @@
 #include <linux/freezer.h>
 #include <linux/dcache.h>
 #include <linux/hashtable.h>
+#include <linux/stringhash.h>
+#include <linux/refcount.h>
 #include <linux/jiffies.h>
 
 #include "glob.h"
@@ -182,7 +184,7 @@ static unsigned int ksmbd_proc_file_flags(struct ksmbd_file *fp)
 		flags |= KSMBD_PROC_FILE_RESILIENT;
 	if (fp->coption & FILE_DELETE_ON_CLOSE_LE)
 		flags |= KSMBD_PROC_FILE_DELETE_ON_CLOSE;
-	if (fp->stream.name)
+	if (ksmbd_stream_fd(fp))
 		flags |= KSMBD_PROC_FILE_STREAM;
 	if (fp->is_posix_ctxt)
 		flags |= KSMBD_PROC_FILE_POSIX;
@@ -390,6 +392,78 @@ int ksmbd_query_inode_status(struct dentry *dentry)
 	return ret;
 }
 
+static struct stream *ksmbd_stream_find(struct ksmbd_inode *ci,
+					const char *name, u64 hash_len)
+{
+	struct stream *st;
+
+	list_for_each_entry(st, &ci->m_streams, entry) {
+		/*
+		 * Compare the hash and length first. The name is compared only
+		 * when both match, because different names can have the same
+		 * hash.
+		 */
+		if (st->hash_len == hash_len &&
+		    !strncmp(st->name, name, hashlen_len(hash_len)))
+			return st;
+	}
+	return NULL;
+}
+
+/*
+ * Find or create the stream object of the xattr name, and give fp a
+ * reference on it. Takes over name, which is either kept by a new object or
+ * freed.
+ */
+int ksmbd_stream_get(struct ksmbd_file *fp, char *name, ssize_t size)
+{
+	struct ksmbd_inode *ci = fp->f_ci;
+	u64 hash_len = hashlen_string(NULL, name);
+	struct stream *st;
+
+	down_write(&ci->m_lock);
+	st = ksmbd_stream_find(ci, name, hash_len);
+	if (st) {
+		refcount_inc(&st->refs);
+		kfree(name);
+	} else {
+		st = kzalloc_obj(*st, KSMBD_DEFAULT_GFP);
+		if (!st) {
+			up_write(&ci->m_lock);
+			kfree(name);
+			return -ENOMEM;
+		}
+		st->name = name;
+		st->size = size;
+		st->hash_len = hash_len;
+		refcount_set(&st->refs, 1);
+		list_add(&st->entry, &ci->m_streams);
+	}
+	fp->stream = st;
+	up_write(&ci->m_lock);
+	return 0;
+}
+
+/* Drop the reference of fp on its stream object. */
+static void ksmbd_stream_put(struct ksmbd_file *fp)
+{
+	struct ksmbd_inode *ci = fp->f_ci;
+	struct stream *st = fp->stream;
+	bool last;
+
+	down_write(&ci->m_lock);
+	last = refcount_dec_and_test(&st->refs);
+	if (last)
+		list_del(&st->entry);
+	up_write(&ci->m_lock);
+	fp->stream = NULL;
+
+	if (last) {
+		kfree(st->name);
+		kfree(st);
+	}
+}
+
 bool ksmbd_inode_pending_delete(struct ksmbd_file *fp)
 {
 	struct ksmbd_inode *ci = fp->f_ci;
@@ -529,6 +603,7 @@ static int ksmbd_inode_init(struct ksmbd_inode *ci, struct ksmbd_file *fp)
 	ci->m_fattr = 0;
 	INIT_LIST_HEAD(&ci->m_fp_list);
 	INIT_LIST_HEAD(&ci->m_op_list);
+	INIT_LIST_HEAD(&ci->m_streams);
 	init_rwsem(&ci->m_lock);
 	ci->m_de = fp->filp->f_path.dentry;
 	return 0;
@@ -570,6 +645,8 @@ static struct ksmbd_inode *ksmbd_inode_get(struct ksmbd_file *fp)
 
 static void ksmbd_inode_free(struct ksmbd_inode *ci)
 {
+	/* The final close of each stream handle drops its stream reference. */
+	WARN_ON_ONCE(!list_empty(&ci->m_streams));
 	ksmbd_inode_unhash(ci);
 	kfree(ci);
 }
@@ -644,13 +721,14 @@ static void __ksmbd_inode_close(struct ksmbd_file *fp)
 			saved_cred = override_creds(filp->f_cred);
 			err = ksmbd_vfs_remove_xattr(file_mnt_idmap(filp),
 						     &filp->f_path,
-						     fp->stream.name,
+						     fp->stream->name,
 						     true);
 			revert_creds(saved_cred);
 			if (err)
 				pr_err("remove xattr failed : %s\n",
-				       fp->stream.name);
+				       fp->stream->name);
 		}
+		ksmbd_stream_put(fp);
 	}
 
 	down_write(&ci->m_lock);
@@ -960,8 +1038,6 @@ static void __ksmbd_close_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp)
 		fp->tcon = NULL;
 	}
 
-	if (ksmbd_stream_fd(fp))
-		kfree(fp->stream.name);
 	ksmbd_share_config_put(fp->share_conf);
 	kfree(fp->owner.name);
 
diff --git a/fs/smb/server/vfs_cache.h b/fs/smb/server/vfs_cache.h
index bc1955b87855..592edf89b043 100644
--- a/fs/smb/server/vfs_cache.h
+++ b/fs/smb/server/vfs_cache.h
@@ -7,6 +7,7 @@
 #define __VFS_CACHE_H__
 
 #include <linux/file.h>
+#include <linux/refcount.h>
 #include <linux/fs.h>
 #include <linux/mutex.h>
 #include <linux/rwsem.h>
@@ -60,10 +61,19 @@ void ksmbd_file_lock_remove(struct ksmbd_lock *lock);
 int ksmbd_file_lock_unlock(struct ksmbd_lock *lock);
 extern wait_queue_head_t ksmbd_lock_wait;
 
+/*
+ * A named stream of an open file. The handles of the stream share it, and
+ * each holds a reference from its open until its final close. Kept on
+ * ksmbd_inode.m_streams under m_lock.
+ */
 struct stream {
-	char *name;
-	ssize_t size;
-	loff_t pos;
+	struct list_head	entry;
+	refcount_t		refs;
+	/* hashlen_string() of name, its hash and length */
+	u64			hash_len;
+	/* The xattr name of the stream, and its length with the NUL */
+	char			*name;
+	ssize_t			size;
 };
 
 struct ksmbd_inode {
@@ -77,6 +87,8 @@ struct ksmbd_inode {
 	struct hlist_node		m_hash;
 	struct list_head		m_fp_list;
 	struct list_head		m_op_list;
+	/* Named streams with open handles, struct stream */
+	struct list_head		m_streams;
 	struct oplock_info		*m_opinfo;
 	__le32				m_fattr;
 };
@@ -139,7 +151,9 @@ struct ksmbd_file {
 	char				create_guid[16];
 	char				app_instance_id[16];
 
-	struct stream			stream;
+	struct stream			*stream;
+	/* File position of a stream handle */
+	loff_t				stream_pos;
 	struct list_head		node;
 	struct list_head		blocked_works;
 	struct list_head		lock_list;
@@ -201,7 +215,7 @@ static inline bool has_file_id(u64 id)
 
 static inline bool ksmbd_stream_fd(struct ksmbd_file *fp)
 {
-	return fp->stream.name != NULL;
+	return fp->stream;
 }
 
 int ksmbd_init_file_table(struct ksmbd_file_table *ft);
@@ -275,6 +289,7 @@ void ksmbd_fd_set_delete_on_close(struct ksmbd_file *fp,
 				  int file_info);
 void ksmbd_fd_set_delete_pending(struct ksmbd_file *fp);
 void ksmbd_fd_clear_delete_pending(struct ksmbd_file *fp);
+int ksmbd_stream_get(struct ksmbd_file *fp, char *name, ssize_t size);
 int ksmbd_reopen_durable_fd(struct ksmbd_work *work, struct ksmbd_file *fp);
 int ksmbd_rebind_replay_fd(struct ksmbd_work *work, struct ksmbd_file *fp);
 int ksmbd_validate_name_reconnect(struct ksmbd_share_config *share,
-- 
2.47.3


  reply	other threads:[~2026-12-02  1:29 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  1:29 [PATCH v2 0/2] ksmbd: fix delete-on-close of named streams Ian Bridges
2026-12-02  1:29 ` Ian Bridges [this message]
2026-12-02  1:29 ` [PATCH v2 2/2] ksmbd: delete only the stream whose handle has delete-on-close Ian Bridges
2026-12-02  1:43 ` [PATCH v2 0/2] ksmbd: fix delete-on-close of named streams ChenXiaoSong
2026-12-02  2:00   ` ChenXiaoSong
2026-12-02  3:28     ` Ian Bridges

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202012941.2332340-2-icb@fastmail.org \
    --to=icb@fastmail.org \
    --cc=chenxiaosong@chenxiaosong.com \
    --cc=gael.blivet@gmail.com \
    --cc=linkinjeon@kernel.org \
    --cc=linux-cifs@vger.kernel.org \
    --cc=senozhatsky@chromium.org \
    --cc=stable@vger.kernel.org \
    --cc=tom@talpey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.