From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=103.168.172.153 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796174995; b=pb5V7QtCvFwYk42dNVtqLl5dVWhi7ekFEq6pcrvP6saS7cQGPMrPTJiV59qi3BwOVJkC RGo1U4NjTw0OzXWqB06T+y/n3B3qc18Lqyl1fo0Cqq90iR+4XmR8ADYG6c5oo7BZkyWRc HBUY7cQ9MuV+ZpuchFxmS+i7+Tk4AExqSwhJMbItSlnCXPwUYS5xleYI7vmQJp623olVJ nm97hpshetWyIysrKevAgN+g83DN9EgUVmgd4C0ol5tW7AgZ5jgoOt+hdDKL2wyg3nHCF Cp9A++Gj4aCPp0mDjdWCn6hsP/on8RzZtwdhH+T+xqd5gYGGJUzCaXTtoUsGEz9ZSBQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796174995; h=DMARC-Filter:Received:Received:DKIM-Signature:DKIM-Signature: X-DKIM2-Info:DKIM2-Signature:X-DKIM2-Info:Message-Instance: X-ME-Sender:X-ME-Received:X-ME-Proxy-Cause:X-ME-Proxy:Feedback-ID: Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To: References:MIME-Version:Content-Transfer-Encoding; bh=pHi+mavyKWvkA4rv03WMNNevBGBrpDHG4lXN7Lu+kYs=; b=i9WevSwbLJJqCBcTskIl1YrVpgN3z84F0/m8/O+KNNE9OFxMd9nPXs5Wn52Pw+k84GdH kVbuV/xH7yrbNRcAYm2wDyX/UfFy6oqVFkiPxGVDEKzVtBsPkGMcH46NCZ7lYhB0zXTqc OS7vjSvFIdSe34Zb2cOb0nCg9c9ER7PZUIsvh09bmPGUxhDx+3qIPs1Qk1QtoPfZhIctT SLVzUP9SCBozZ2xbgRuEmynxYIDHPWg9wJURlNCZx4bvoLVioMWwhKeuxBgBeO7Pc/xdY iIYM8OYRHHHYoygKKkRyURyFP+aL/FS+B8QDZN4Ml8ByFuqNZxuiDREhV1mq6wJ58Uw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=fastmail.org; spf=pass smtp.mailfrom=fastmail.org; dkim=pass header.d=fastmail.org header.i=@fastmail.org header.a=rsa-sha256 header.s=fm3 header.b=YYtDdX1r; dkim=pass header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm3 header.b=kXTm5jAk; arc=none smtp.remote-ip=103.168.172.153 Received: from fhigh-a2-smtp.messagingengine.com (fhigh-a2-smtp.messagingengine.com [103.168.172.153]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 445EE606A323 for ; Wed, 2 Dec 2026 01:29:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 445EE606A323 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=fastmail.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=fastmail.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=fastmail.org header.i=@fastmail.org header.a=rsa-sha256 header.s=fm3 header.b=YYtDdX1r; dkim=pass (2048-bit key, unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm3 header.b=kXTm5jAk Received: from phl-compute-09.internal (phl-compute-09.internal [10.202.2.49]) by mailfhigh.phl.internal (Postfix) with ESMTP id BDA82140019C for ; Tue, 1 Dec 2026 20:29:52 -0500 (EST) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-09.internal (MEProxy); Tue, 01 Dec 2026 20:29:52 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fastmail.org; h= cc:cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm3; t=1796174992; x= 1796261392; bh=pHi+mavyKWvkA4rv03WMNNevBGBrpDHG4lXN7Lu+kYs=; b=Y YtDdX1rIPM6sO7G8yHBEAwl2HO/NROqxD6YWaRtyxyWdMIyDPKYmaWQ+fNne4v7a Cza5zW8wAPmDGs7gG4pCUqLdkJloI6p2sokTgfqWZMMFNgJ9mD0K3SVMbiwbaSi2 31YjixJmIfnZeIuGs/J2JiNR+4Dks0nCf3h0xkQvbUPybSQnGZ+uJk/aBsh2KX/r rBYpkNNva5Ip/HpX3y6xgs1wyw7B0KsH7EUv6MxUH0HUax9T8NVWwJrlU1+5GAoI CNjI0fL+xL3LlgQ3S5d+Q7AYF1we+Wl14GL94S44PhsXGZ9oa0AX/2FI075YFNUf eKil6NOrfYwkXw3UE+5wQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1796174992; x=1796261392; bh=p Hi+mavyKWvkA4rv03WMNNevBGBrpDHG4lXN7Lu+kYs=; b=kXTm5jAkGxg9hDDjE d0TUNvxDRulQMN8tskjYiD9I0mRCssGLVwjQ0a+bgHxJI2chS1QQXrFde3YfIR4W rUWrdgSX2P3tUqTvOqu5mkZyPRjE4/cAC3IbzGY36mGx23TtbA12h6wh1+jJ3uIg Tp9gEZDmJLWo7wJuR66iGlaQkIv7MmLBn/I6Xj8xJilkAxAwT0ZgTHrQJaJLx5oW eOPgyIM+9Fi/epKfwK5kB3ZbYz2Q+/5eaQfVnMTo1iHBLt1PK6xBGYsDmfZDBzhJ ItLc5vbldeV7kDDAd+tBcskE7UFcXmZvukWCP0P2s6xIwZeUgQ1iXLomdfqqSbA+ jVc0w== X-DKIM2-Info: draft=ietf-dkim-dkim2-spec-09; repo=github.com/dkim2wg/interop; date=2026-11-03; sw=lmtpprox; action=sign d=fastmail.org a=rsa-sha256; DKIM2-Signature: i=1; m=1; t=1796174992; d=fastmail.org; mf=PGljYkBmYXN0bWFpbC5vcmc+; rt=PHN0YWJsZUB2Z2VyLmtlcm5lbC5vcmc+; s=fm3:rsa-sha256:rsJe/WN7jJFcJlX9/HRoB+dF/sH/8CPni/qkkpN0zkiTl1I r0upjzCxKyMVuy6kwqIxaYg+KnzYBzsm/kWFkUg+O4di9f1DmoFJX/TC694NURFR bGYodK75eNAFtV8e2DC8UMg2mVj933feX2FCIBVUkHFEzsCRd2fPi4ni2OSxCEs3 ME6kcP6W3a587NJmPUOFfuVCzSzcus7P5U9fc4c4NYWbaCtirzgAr9A9Cw90RWmH cdK1OO4XcTfIsWh6Ry325lJJDmfybtF3S+8Aal4PoD9MdwlXcD1mb8PSv/Cn8C5A pLhiTSra1yMbIOMfuG8XJDNuH7nxNpqDqYVxqGw==; X-DKIM2-Info: draft=ietf-dkim-dkim2-spec-09; repo=github.com/dkim2wg/interop; date=2026-11-03; sw=lmtpprox; action=mi-m=1; hc=11; hn=cc,content-transfer-encoding,date,feedback-id,from, in-reply-to,message-id,mime-version,references,subject,to; Message-Instance: m=1; h=sha256:PkT0Q7U12GTR0Sug5sLCUzYkL3v+n5oiQCEczcFJvws=:9T9OIHcsl2PpmR8gNG/GdDyBH7HDQJ5lO4Lp9a8fI34=; X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTEYUSfPfnUUUy3rC/Flvk2c/jl3mNXoL3IdVkoSlpwg1ALD6EnpXNHNWNgGNaEC6B sBW9qrSHYD3jKfe538XUjHeuV3Nkduc6D921XHTcxpdHCvGI2c4upAut502moyeTvjse7a efasm2axQiiJ9WgYf4LlvVbsarJFrt/9jOZRamwhn5oETZNFFLkoqTQ+AXa1y13maZUSqg 4C1omhqmXT1JS2aXtjEZaiOKXQy+7mr3yMQfPwb0IltfgNfdVzIENITIlwSEwoNZHK5fx8 5GoYBXpua0/NFOl2HXk6UMTHGxoF2jShOp3w0nDJoRD1NaNxEDd6b2TsNy/UhxxtG31zE8 Padnu8jd0UxvkvLdY7SCNQyYccWdMEwQ2bxBmqZs+0f7Jm84oP+6UujVubvi0WKz494251 lca+P8fBNP0Vvtx3eXi1UdPeHAiCeMUPW+Ds+WbLZfgpTVLsmp3GxWtfoReklBlwVX2djb +xVaDPhfX2bCz9jJU3TWfXK9jaPBxz75fdeb56aCl/8J3Fea9xjWpXJ5GbIME8TPhABg2X 1+XCglsHLD8Krj/u9KyvOcnJyOl7+DU5zy3pUMXDZzNpqiwRYGQlDekzK8QBVR+dKiHWk3 xQMeqAgRlYca8A/0aLjXF/pmDoJ/HiwVDPllMWKHdh17D0v7IAasR1TRCOVw X-ME-Proxy: Feedback-ID: ib53e4b78:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 1 Dec 2026 20:29:51 -0500 (EST) From: Ian Bridges 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 , 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 Message-ID: <20261202012941.2332340-2-icb@fastmail.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261202012941.2332340-1-icb@fastmail.org> References: <20261202012941.2332340-1-icb@fastmail.org> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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 #include #include +#include +#include #include #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 +#include #include #include #include @@ -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