| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/10/11 22:43 | flow | patch-triage |
16mArgs:null Results: {
"EnableConfigs": [
"NTFS_FS",
"NTFS_FS_POSIX_ACL"
],
"FocusSymbols": [
"ntfs_ioctl_stream_read",
"ntfs_ioctl_stream_write",
"ntfs_ioctl_stream_remove",
"ntfs_ioctl_list_streams",
"ntfs_create_named_stream",
"ntfs_unlink_named_stream",
"ntfs_lookup_stream",
"ntfs_remove_named_stream",
"ntfs_stream_inode_validate",
"ntfs_stream_put",
"ntfs_inode_attr_pread",
"ntfs_inode_attr_pwrite"
],
"KMSANReasoning": "The patch series adds named stream support ($DATA attributes) to the NTFS filesystem, introducing both a Windows-compatible pathname interface (file:stream) and ioctl interfaces (NTFS_IOC_STREAM_READ, NTFS_IOC_STREAM_WRITE, NTFS_IOC_STREAM_REMOVE, NTFS_IOC_LIST_STREAMS).\n\nAn analysis of memory initialization and kernel-to-user data transfers shows:\n1. UAPI Structures \u0026 Info-leak checks:\n - In `ntfs_ioctl_stream(NTFS_STREAM_IOCTL_READ)`, `struct ntfs_stream` has no structure padding (__aligned_u64, __aligned_u64, __aligned_u64, __u32, __u32, __aligned_u64). The header and name are initialized from user space via copy_from_user(), `bytes_returned` is set explicitly, and the stream payload is read into the buffer via `ntfs_inode_attr_pread()`, which returns the exact byte count copied into the buffer. The amount copied back to user space strictly equals `sizeof(*req) + req-\u003ename_len + req-\u003ebytes_returned`.\n - In `ntfs_ioctl_stream(NTFS_STREAM_IOCTL_WRITE)`, only `req-\u003ebytes_returned` is copied to user space.\n - In `ntfs_ioctl_list_streams()`, `struct ntfs_list_streams` contains no padding. The entry buffer `kbuf` is allocated using `kvzalloc()`, ensuring that any structure alignment padding (entries are 8-byte aligned) or reserved fields in `struct ntfs_stream_entry` are zero-initialized prior to `copy_to_user()`.\n2. Kernel Data Structures \u0026 Stack/Heap Allocations:\n - `struct ntfs_stream_path` fields are populated by `ntfs_stream_path_parse()` whenever it returns positive, and never accessed otherwise.\n - `struct ntfs_fsync_attr` allocated in `ntfs_file_fsync()` has only two fields (`list` and `inode`), both of which are initialized immediately upon allocation.\n - `ni-\u003ename` buffers allocated in `ntfs_read_locked_attr_inode()` are immediately populated via `memcpy()` and null-terminated.\n - Gaps in stream data are cleared using `iomap_zero_range()` in `ntfs_extend_initialized_size()`, and resident attribute extensions zero new bytes via `memset()` in `ntfs_resident_attr_value_resize()`.\n3. KASAN vs. KMSAN applicability:\n - The primary risks in this patch series involve locking hierarchies (locking base inode's `mrec_lock` across stream inodes), reference counting and deferred unlink lifecycles (`stream_open_count`, `NInoStreamUnlinked`), and file extent bounds checks. These failure modes represent locking deadlocks, use-after-free, or out-of-bounds access, which are already detected by standard debuggers (LOCKDEP, KASAN, etc.).\n - There are no uninitialized memory reads or information leaks introduced.\n\nTherefore, dedicated KMSAN fuzzing is not justified.",
"NeedsKMSAN": false,
"Reasoning": "The patch introduces support for NTFS named data streams ($DATA attributes), including a new ioctl interface (NTFS_IOC_STREAM_READ, NTFS_IOC_STREAM_WRITE, NTFS_IOC_STREAM_REMOVE, NTFS_IOC_LIST_STREAMS) and pathname-based stream access (file:stream format enabled via streams_interface=windows). It implements stream creation, lookup, unlink, validation, concurrent locking/lifetime management, and attribute reading/writing. This complex filesystem feature is fully reachable in virtualized environments via loopback-mounted NTFS images and user syscalls/ioctls, making it a prime candidate for syzkaller fuzzing.",
"WorthFuzzing": true
} |
| 1/1 | 2026/10/11 22:43 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 31714ee070cc940f303c3a87e717a5aae8856a8f\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Sun Oct 11 22:43:11 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/Documentation/filesystems/ntfs.rst b/Documentation/filesystems/ntfs.rst\nindex 4bfa392daec6a..ec329bb142a06 100644\n--- a/Documentation/filesystems/ntfs.rst\n+++ b/Documentation/filesystems/ntfs.rst\n@@ -169,4 +169,38 @@ symlink=wsl|native Configure how symbolic links are created. Under\n Linux) compatible symlinks are created. Under\n \"native\", Windows native symbolic links are\n created.\n+\n+streams_interface= Select the named-stream pathname interface:\n+ \"none\" (default) treats ':' as an ordinary filename\n+ character, while \"windows\" interprets the final\n+ component of a path such as \"file:stream\" as a named\n+ $DATA stream. In \"windows\" mode, filenames containing\n+ ':' are hidden from directory listings and cannot be\n+ addressed through the pathname interface. Only the\n+ file:stream form is recognized. Typed forms such\n+ as file::$DATA and file:stream:$DATA are not\n+ supported. The ioctl interface is available in both\n+ modes. The base file or directory must already exist\n+ before a pathname stream can be created. Creating\n+ file:stream does not create file. Removing a stream\n+ while it is open fails with -EBUSY. Removing the base\n+ file while a stream is open detaches the stream from\n+ the namespace; NTFS has no orphan-stream list, so a\n+ crash can leave that stream's data unreachable on\n+ disk.\n ======================= ====================================================\n+\n+Named-stream ioctls\n+===================\n+\n+The named-stream ioctl interface is available regardless of\n+streams_interface. Open the base regular file or directory and use\n+NTFS_IOC_STREAM_READ and NTFS_IOC_STREAM_WRITE with a\n+struct ntfs_stream request. The request contains the stream name,\n+byte offset, and transfer length. Write requests create the stream when it\n+does not exist. A failed write can leave a newly created empty stream; use\n+NTFS_IOC_STREAM_REMOVE to remove it. This ioctl removes streams, and\n+NTFS_IOC_LIST_STREAMS enumerates streams. Stream names are encoded with\n+the mounted NLS by default. Set NTFS_STREAM_FL_UTF16 to pass or receive\n+raw UTF-16LE names. These ioctls transfer stream data directly and do not\n+return a separate stream file descriptor.\ndiff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documentation/userspace-api/ioctl/ioctl-number.rst\nindex 2fc53093752d1..94a421970b850 100644\n--- a/Documentation/userspace-api/ioctl/ioctl-number.rst\n+++ b/Documentation/userspace-api/ioctl/ioctl-number.rst\n@@ -401,6 +401,7 @@ Code Seq# Include File Comments\n 0xE5 00-3F linux/fuse.h\n 0xEC 00-01 drivers/platform/chrome/cros_ec_dev.h ChromeOS EC driver\n 0xEE 00-09 uapi/linux/pfrut.h Platform Firmware Runtime Update and Telemetry\n+0xEF 00-0F uapi/linux/ntfs.h NTFS\n 0xF3 00-3F drivers/usb/misc/sisusbvga/sisusb.h sisfb (in development)\n \u003cmailto:thomas@winischhofer.net\u003e\n 0xF6 all LTTng Linux Trace Toolkit Next Generation\ndiff --git a/MAINTAINERS b/MAINTAINERS\nindex cc3cae2e378b3..b035f7ad34f05 100644\n--- a/MAINTAINERS\n+++ b/MAINTAINERS\n@@ -19489,6 +19489,7 @@ S:\tMaintained\n T:\tgit git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/ntfs.git\n F:\tDocumentation/filesystems/ntfs.rst\n F:\tfs/ntfs/\n+F:\tinclude/uapi/linux/ntfs.h\n \n NTFS3 FILESYSTEM\n M:\tKonstantin Komarov \u003calmaz.alexandrovich@paragon-software.com\u003e\ndiff --git a/fs/ntfs/Makefile b/fs/ntfs/Makefile\nindex ee8987e496a87..3e85495776536 100644\n--- a/fs/ntfs/Makefile\n+++ b/fs/ntfs/Makefile\n@@ -5,7 +5,7 @@ obj-$(CONFIG_NTFS_FS) += ntfs.o\n ntfs-y := aops.o attrib.o collate.o dir.o file.o index.o inode.o \\\n \t mft.o mst.o namei.o runlist.o super.o unistr.o attrlist.o ea.o \\\n \t upcase.o bitmap.o lcnalloc.o logfile.o reparse.o compress.o \\\n-\t iomap.o debug.o sysctl.o object_id.o bdev-io.o\n+\t iomap.o debug.o sysctl.o object_id.o bdev-io.o named_stream.o\n \n ntfs-$(CONFIG_NTFS_FS_WOF_COMPRESSION) += wof.o \\\n \tlib/decompress_common.o lib/lzx_decompress.o lib/xpress_decompress.o\ndiff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c\nindex 07414a20b7de9..fce7144d7bef6 100644\n--- a/fs/ntfs/attrib.c\n+++ b/fs/ntfs/attrib.c\n@@ -2352,12 +2352,13 @@ int ntfs_attr_set(struct ntfs_inode *ni, s64 ofs, s64 cnt, const u8 val)\n int ntfs_attr_set_initialized_size(struct ntfs_inode *ni, loff_t new_size)\n {\n \tstruct ntfs_attr_search_ctx *ctx;\n+\tstruct ntfs_inode *base_ni = ntfs_base_inode(ni);\n \tint err = 0;\n \n \tif (!NInoNonResident(ni))\n \t\treturn -EINVAL;\n \n-\tctx = ntfs_attr_get_search_ctx(ni, NULL);\n+\tctx = ntfs_attr_get_search_ctx(base_ni, NULL);\n \tif (!ctx)\n \t\treturn -ENOMEM;\n \n@@ -2439,6 +2440,7 @@ int ntfs_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,\n \tstruct mft_record *m;\n \tint err, offset;\n \tstruct ntfs_inode *base_ni;\n+\tu32 ic;\n \n \tif (!ni || (!name \u0026\u0026 name_len))\n \t\treturn -EINVAL;\n@@ -2468,7 +2470,8 @@ int ntfs_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,\n \t * attribute in @ni-\u003emrec, not any extent inode in case if @ni is base\n \t * file record.\n \t */\n-\terr = ntfs_attr_find(type, name, name_len, CASE_SENSITIVE, val, size, ctx);\n+\tic = type == AT_DATA \u0026\u0026 name_len ? IGNORE_CASE : CASE_SENSITIVE;\n+\terr = ntfs_attr_find(type, name, name_len, ic, val, size, ctx);\n \tif (!err) {\n \t\terr = -EEXIST;\n \t\tntfs_debug(\"Attribute already present.\\n\");\n@@ -2560,6 +2563,7 @@ static int ntfs_non_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,\n \tstruct mft_record *m;\n \tstruct ntfs_inode *base_ni;\n \tint err, offset;\n+\tu32 ic;\n \n \tif (!ni || dataruns_size \u003c= 0 || (!name \u0026\u0026 name_len))\n \t\treturn -EINVAL;\n@@ -2589,7 +2593,8 @@ static int ntfs_non_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,\n \t * attribute in @ni-\u003emrec, not any extent inode in case if @ni is base\n \t * file record.\n \t */\n-\terr = ntfs_attr_find(type, name, name_len, CASE_SENSITIVE, NULL, 0, ctx);\n+\tic = type == AT_DATA \u0026\u0026 name_len ? IGNORE_CASE : CASE_SENSITIVE;\n+\terr = ntfs_attr_find(type, name, name_len, ic, NULL, 0, ctx);\n \tif (!err) {\n \t\terr = -EEXIST;\n \t\tpr_err(\"Attribute 0x%x already present\\n\", type);\n@@ -2663,7 +2668,7 @@ static int ntfs_non_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,\n \t * update of attribute list.\n \t */\n \tntfs_attr_reinit_search_ctx(ctx);\n-\terr = ntfs_attr_lookup(type, name, name_len, CASE_SENSITIVE,\n+\terr = ntfs_attr_lookup(type, name, name_len, ic,\n \t\t\t\tlowest_vcn, NULL, 0, ctx);\n \tif (err) {\n \t\tpr_err(\"%s: attribute lookup failed\\n\", __func__);\n@@ -3132,6 +3137,7 @@ int ntfs_attr_open(struct ntfs_inode *ni, const __le32 type,\n \tstruct attr_record *a;\n \tbool cs;\n \tstruct ntfs_inode *base_ni;\n+\tu32 ic;\n \tint err;\n \n \tif (!ni || !ni-\u003evol)\n@@ -3140,10 +3146,7 @@ int ntfs_attr_open(struct ntfs_inode *ni, const __le32 type,\n \tntfs_debug(\"Entering for inode %lld, attr 0x%x.\\n\",\n \t\t\tni-\u003emft_no, type);\n \n-\tif (NInoAttr(ni))\n-\t\tbase_ni = ni-\u003eext.base_ntfs_ino;\n-\telse\n-\t\tbase_ni = ni;\n+\tbase_ni = ntfs_base_inode(ni);\n \n \tif (name \u0026\u0026 name != AT_UNNAMED \u0026\u0026 name != I30) {\n \t\tname = ntfs_ucsndup(name, name_len);\n@@ -3161,7 +3164,8 @@ int ntfs_attr_open(struct ntfs_inode *ni, const __le32 type,\n \t\tgoto err_out;\n \t}\n \n-\terr = ntfs_attr_lookup(type, name, name_len, 0, 0, NULL, 0, ctx);\n+\tic = type == AT_DATA \u0026\u0026 name_len ? IGNORE_CASE : CASE_SENSITIVE;\n+\terr = ntfs_attr_lookup(type, name, name_len, ic, 0, NULL, 0, ctx);\n \tif (err)\n \t\tgoto put_err_out;\n \n@@ -3346,7 +3350,8 @@ int ntfs_attr_map_whole_runlist(struct ntfs_inode *ni)\n \t\t\tnot_mapped = 1;\n \n \t\terr = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len,\n-\t\t\t\t\tCASE_SENSITIVE, next_vcn, NULL, 0, ctx);\n+\t\t\t\t\tCASE_SENSITIVE, next_vcn,\n+\t\t\t\t\tNULL, 0, ctx);\n \t\tif (err)\n \t\t\tbreak;\n \n@@ -3431,6 +3436,7 @@ int ntfs_attr_record_move_to(struct ntfs_attr_search_ctx *ctx, struct ntfs_inode\n \tstruct ntfs_attr_search_ctx *nctx;\n \tstruct attr_record *a;\n \tint err;\n+\tu32 ic;\n \tstruct mft_record *ni_mrec;\n \tstruct super_block *sb;\n \n@@ -3466,9 +3472,11 @@ int ntfs_attr_record_move_to(struct ntfs_attr_search_ctx *ctx, struct ntfs_inode\n \t * attribute in @ni-\u003emrec, not any extent inode in case if @ni is base\n \t * file record.\n \t */\n-\terr = ntfs_attr_find(a-\u003etype, (__le16 *)((u8 *)a + le16_to_cpu(a-\u003ename_offset)),\n-\t\t\t\ta-\u003ename_length, CASE_SENSITIVE, NULL,\n-\t\t\t\t0, nctx);\n+\tic = a-\u003etype == AT_DATA \u0026\u0026 a-\u003ename_length ?\n+\t\t\tIGNORE_CASE : CASE_SENSITIVE;\n+\terr = ntfs_attr_find(a-\u003etype,\n+\t\t\t(__le16 *)((u8 *)a + le16_to_cpu(a-\u003ename_offset)),\n+\t\t\ta-\u003ename_length, ic, NULL, 0, nctx);\n \tif (!err) {\n \t\tntfs_debug(\"Attribute of such type, with same name already present in this MFT record.\\n\");\n \t\terr = -EEXIST;\n@@ -3800,7 +3808,8 @@ static int __ntfs_attr_update_mapping_pairs(struct ntfs_inode *ni,\n \tfinished_build = false;\n \tstart_rl = ni-\u003erunlist.rl;\n \twhile (!(err = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len,\n-\t\t\t\tCASE_SENSITIVE, from_vcn, NULL, 0, ctx))) {\n+\t\t\t\tCASE_SENSITIVE, from_vcn, NULL,\n+\t\t\t\t0, ctx))) {\n \t\tunsigned int de_cnt = 0;\n \n \t\ta = ctx-\u003eattr;\n@@ -4011,7 +4020,8 @@ static int __ntfs_attr_update_mapping_pairs(struct ntfs_inode *ni,\n \t\tntfs_attr_reinit_search_ctx(ctx);\n \t\tntfs_debug(\"Deallocate marked extents.\\n\");\n \t\twhile (!(err = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len,\n-\t\t\t\tCASE_SENSITIVE, 0, NULL, 0, ctx))) {\n+\t\t\t\tCASE_SENSITIVE, 0, NULL, 0,\n+\t\t\t\tctx))) {\n \t\t\tif (le64_to_cpu(ctx-\u003eattr-\u003edata.non_resident.highest_vcn) !=\n \t\t\t\t\tNTFS_VCN_DELETE_MARK)\n \t\t\t\tcontinue;\n@@ -4345,7 +4355,7 @@ static int ntfs_non_resident_attr_shrink(struct ntfs_inode *ni,\n \t\t\tgoto unlock_runlist;\n \t\t}\n \n-\t\tctx = ntfs_attr_get_search_ctx(ni, NULL);\n+\t\tctx = ntfs_attr_get_search_ctx(base_ni, NULL);\n \t\tif (!ctx) {\n \t\t\tntfs_error(vol-\u003esb, \"%s: Failed to get search context\", __func__);\n \t\t\terr = -ENOMEM;\n@@ -4404,8 +4414,8 @@ static int ntfs_non_resident_attr_shrink(struct ntfs_inode *ni,\n \t\treturn -ENOMEM;\n \t}\n \n-\terr = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len, CASE_SENSITIVE,\n-\t\t\t\t0, NULL, 0, ctx);\n+\terr = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len,\n+\t\t\t\tCASE_SENSITIVE, 0, NULL, 0, ctx);\n \tif (err) {\n \t\tif (err == -ENOENT)\n \t\t\terr = -EIO;\n@@ -4724,8 +4734,8 @@ static int ntfs_non_resident_attr_expand(struct ntfs_inode *ni, const s64 newsiz\n \t\tgoto rollback;\n \t}\n \n-\terr = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len, CASE_SENSITIVE,\n-\t\t\t 0, NULL, 0, ctx);\n+\terr = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len,\n+\t\t\t CASE_SENSITIVE, 0, NULL, 0, ctx);\n \tif (err) {\n \t\tif (err == -ENOENT)\n \t\t\terr = -EIO;\n@@ -4837,7 +4847,7 @@ static int ntfs_resident_attr_resize(struct ntfs_inode *attr_ni, const s64 newsi\n \t}\n \n \terr = ntfs_attr_lookup(attr_ni-\u003etype, attr_ni-\u003ename, attr_ni-\u003ename_len,\n-\t\t\t0, 0, NULL, 0, ctx);\n+\t\t\tCASE_SENSITIVE, 0, NULL, 0, ctx);\n \tif (err) {\n \t\tntfs_error(sb, \"ntfs_attr_lookup failed\");\n \t\tgoto put_err_out;\n@@ -5213,6 +5223,7 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,\n \t\ts64 *lcn_count, s64 max_clu_count, bool *balloc, bool update_mp,\n \t\tbool skip_holes)\n {\n+\tstruct ntfs_inode *base_ni = ntfs_base_inode(ni);\n \tstruct ntfs_volume *vol = ni-\u003evol;\n \tstruct ntfs_attr_search_ctx *ctx;\n \tstruct runlist_element *rl, *rlc;\n@@ -5226,10 +5237,7 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,\n \tif (err)\n \t\treturn err;\n \n-\tif (NInoAttr(ni))\n-\t\tctx = ntfs_attr_get_search_ctx(ni-\u003eext.base_ntfs_ino, NULL);\n-\telse\n-\t\tctx = ntfs_attr_get_search_ctx(ni, NULL);\n+\tctx = ntfs_attr_get_search_ctx(base_ni, NULL);\n \tif (!ctx) {\n \t\tntfs_error(vol-\u003esb, \"%s: Failed to get search context\", __func__);\n \t\treturn -ENOMEM;\n@@ -5379,7 +5387,7 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,\n \t} else {\n \t\tVFS_I(ni)-\u003ei_blocks += clu_count \u003c\u003c (vol-\u003ecluster_size_bits - 9);\n \t\tNInoSetRunlistDirty(ni);\n-\t\tmark_mft_record_dirty(ni);\n+\t\tmark_mft_record_dirty(base_ni);\n \t}\n \n \t*lcn_start = lcn;\n@@ -5405,10 +5413,7 @@ int ntfs_attr_rm(struct ntfs_inode *ni)\n \tstruct ntfs_inode *base_ni;\n \tstruct super_block *sb = ni-\u003evol-\u003esb;\n \n-\tif (NInoAttr(ni))\n-\t\tbase_ni = ni-\u003eext.base_ntfs_ino;\n-\telse\n-\t\tbase_ni = ni;\n+\tbase_ni = ntfs_base_inode(ni);\n \n \tntfs_debug(\"Entering for inode 0x%llx, attr 0x%x.\\n\",\n \t\t\t(long long) ni-\u003emft_no, ni-\u003etype);\n@@ -5420,7 +5425,7 @@ int ntfs_attr_rm(struct ntfs_inode *ni)\n \t\terr = ntfs_attr_map_whole_runlist(ni);\n \t\tif (err)\n \t\t\treturn err;\n-\t\tctx = ntfs_attr_get_search_ctx(ni, NULL);\n+\t\tctx = ntfs_attr_get_search_ctx(base_ni, NULL);\n \t\tif (!ctx) {\n \t\t\tntfs_error(sb, \"%s: Failed to get search context\", __func__);\n \t\t\treturn -ENOMEM;\n@@ -5462,6 +5467,7 @@ int ntfs_attr_exist(struct ntfs_inode *ni, const __le32 type, __le16 *name,\n \t\tu32 name_len)\n {\n \tstruct ntfs_attr_search_ctx *ctx;\n+\tu32 ic;\n \tint ret;\n \n \tntfs_debug(\"Entering\\n\");\n@@ -5473,39 +5479,54 @@ int ntfs_attr_exist(struct ntfs_inode *ni, const __le32 type, __le16 *name,\n \t\treturn 0;\n \t}\n \n-\tret = ntfs_attr_lookup(type, name, name_len, CASE_SENSITIVE,\n+\tic = type == AT_DATA \u0026\u0026 name_len ? IGNORE_CASE : CASE_SENSITIVE;\n+\tret = ntfs_attr_lookup(type, name, name_len, ic,\n \t\t\t0, NULL, 0, ctx);\n \tntfs_attr_put_search_ctx(ctx);\n \n \treturn !ret;\n }\n \n+/*\n+ * If @attr_vi is non-NULL, the attribute inode reference is returned to the\n+ * caller, which must release it after dropping ni-\u003emrec_lock.\n+ */\n int ntfs_attr_remove(struct ntfs_inode *ni, const __le32 type, __le16 *name,\n-\t\tu32 name_len)\n+\t\tu32 name_len, struct inode **attr_vi)\n {\n \tint err;\n-\tstruct inode *attr_vi;\n+\tstruct inode *vi;\n \tstruct ntfs_inode *attr_ni;\n \n \tntfs_debug(\"Entering\\n\");\n \n+\tif (attr_vi)\n+\t\t*attr_vi = NULL;\n \tif (!ni)\n \t\treturn -EINVAL;\n \n-\tattr_vi = ntfs_attr_iget(VFS_I(ni), type, name, name_len);\n-\tif (IS_ERR(attr_vi)) {\n-\t\terr = PTR_ERR(attr_vi);\n+\tvi = ntfs_attr_iget(VFS_I(ni), type, name, name_len);\n+\tif (IS_ERR(vi)) {\n+\t\terr = PTR_ERR(vi);\n \t\tntfs_error(ni-\u003evol-\u003esb, \"Failed to open attribute 0x%02x of inode 0x%llx\",\n \t\t\t\ttype, (unsigned long long)ni-\u003emft_no);\n \t\treturn err;\n \t}\n-\tattr_ni = NTFS_I(attr_vi);\n+\tattr_ni = NTFS_I(vi);\n \n \terr = ntfs_attr_rm(attr_ni);\n-\tif (err)\n+\tif (err) {\n \t\tntfs_error(ni-\u003evol-\u003esb, \"Failed to remove attribute 0x%02x of inode 0x%llx\",\n \t\t\t\ttype, (unsigned long long)ni-\u003emft_no);\n-\tiput(attr_vi);\n+\t} else {\n+\t\tNInoClearDirty(attr_ni);\n+\t\tclear_nlink(vi);\n+\t\tremove_inode_hash(vi);\n+\t}\n+\tif (attr_vi)\n+\t\t*attr_vi = vi;\n+\telse\n+\t\tiput(vi);\n \treturn err;\n }\n \n@@ -5580,13 +5601,14 @@ void *ntfs_attr_readall(struct ntfs_inode *ni, const __le32 type,\n \n int ntfs_non_resident_attr_insert_range(struct ntfs_inode *ni, s64 start_vcn, s64 len)\n {\n+\tstruct ntfs_inode *base_ni = ntfs_base_inode(ni);\n \tstruct ntfs_volume *vol = ni-\u003evol;\n \tstruct runlist_element *hole_rl, *rl;\n \tstruct ntfs_attr_search_ctx *ctx;\n \tint ret;\n \tsize_t new_rl_count;\n \n-\tif (NInoAttr(ni) || ni-\u003etype != AT_DATA)\n+\tif (ni-\u003etype != AT_DATA)\n \t\treturn -EOPNOTSUPP;\n \tif (start_vcn \u003e ntfs_bytes_to_cluster(vol, ni-\u003eallocated_size))\n \t\treturn -EINVAL;\n@@ -5635,14 +5657,14 @@ int ntfs_non_resident_attr_insert_range(struct ntfs_inode *ni, s64 start_vcn, s6\n \tif (ret)\n \t\treturn ret;\n \n-\tctx = ntfs_attr_get_search_ctx(ni, NULL);\n+\tctx = ntfs_attr_get_search_ctx(base_ni, NULL);\n \tif (!ctx) {\n \t\tret = -ENOMEM;\n \t\treturn ret;\n \t}\n \n-\tret = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len, CASE_SENSITIVE,\n-\t\t\t 0, NULL, 0, ctx);\n+\tret = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len,\n+\t\t\t CASE_SENSITIVE, 0, NULL, 0, ctx);\n \tif (ret) {\n \t\tntfs_attr_put_search_ctx(ctx);\n \t\treturn ret;\n@@ -5659,6 +5681,7 @@ int ntfs_non_resident_attr_insert_range(struct ntfs_inode *ni, s64 start_vcn, s6\n \n int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn, s64 len)\n {\n+\tstruct ntfs_inode *base_ni = ntfs_base_inode(ni);\n \tstruct ntfs_volume *vol = ni-\u003evol;\n \tstruct runlist_element *punch_rl, *rl;\n \tstruct ntfs_attr_search_ctx *ctx = NULL;\n@@ -5667,7 +5690,7 @@ int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn,\n \tint ret;\n \tsize_t new_rl_cnt;\n \n-\tif (NInoAttr(ni) || ni-\u003etype != AT_DATA)\n+\tif (ni-\u003etype != AT_DATA)\n \t\treturn -EOPNOTSUPP;\n \n \tend_vcn = ntfs_bytes_to_cluster(vol, ni-\u003eallocated_size);\n@@ -5723,14 +5746,14 @@ int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn,\n \t}\n \tup_write(\u0026ni-\u003erunlist.lock);\n \n-\tctx = ntfs_attr_get_search_ctx(ni, NULL);\n+\tctx = ntfs_attr_get_search_ctx(base_ni, NULL);\n \tif (!ctx) {\n \t\tret = -ENOMEM;\n \t\tgoto out_rl;\n \t}\n \n-\tret = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len, CASE_SENSITIVE,\n-\t\t\t 0, NULL, 0, ctx);\n+\tret = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len,\n+\t\t\t CASE_SENSITIVE, 0, NULL, 0, ctx);\n \tif (ret)\n \t\tgoto out_ctx;\n \n@@ -5748,12 +5771,13 @@ int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn,\n \t\tntfs_attr_put_search_ctx(ctx);\n out_rl:\n \tkvfree(punch_rl);\n-\tmark_mft_record_dirty(ni);\n+\tmark_mft_record_dirty(base_ni);\n \treturn ret;\n }\n \n int ntfs_non_resident_attr_punch_hole(struct ntfs_inode *ni, s64 start_vcn, s64 len)\n {\n+\tstruct ntfs_inode *base_ni = ntfs_base_inode(ni);\n \tstruct ntfs_volume *vol = ni-\u003evol;\n \tstruct runlist_element *punch_rl, *rl;\n \ts64 end_vcn;\n@@ -5761,7 +5785,7 @@ int ntfs_non_resident_attr_punch_hole(struct ntfs_inode *ni, s64 start_vcn, s64\n \tint ret;\n \tsize_t new_rl_count;\n \n-\tif (NInoAttr(ni) || ni-\u003etype != AT_DATA)\n+\tif (ni-\u003etype != AT_DATA)\n \t\treturn -EOPNOTSUPP;\n \n \tend_vcn = ntfs_bytes_to_cluster(vol, ni-\u003eallocated_size);\n@@ -5805,12 +5829,13 @@ int ntfs_non_resident_attr_punch_hole(struct ntfs_inode *ni, s64 start_vcn, s64\n \t\tntfs_error(vol-\u003esb, \"Freeing of clusters failed\");\n \n \tkvfree(punch_rl);\n-\tmark_mft_record_dirty(ni);\n+\tmark_mft_record_dirty(base_ni);\n \treturn ret;\n }\n \n int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bool keep_size)\n {\n+\tstruct ntfs_inode *base_ni = ntfs_base_inode(ni);\n \tstruct ntfs_volume *vol = ni-\u003evol;\n \tstruct mft_record *mrec;\n \tstruct ntfs_attr_search_ctx *ctx;\n@@ -5822,7 +5847,7 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo\n \tstruct runlist_element *rl;\n \tbool balloc;\n \n-\tif (NInoAttr(ni) || ni-\u003etype != AT_DATA)\n+\tif (ni-\u003etype != AT_DATA)\n \t\treturn -EINVAL;\n \n \tif (NInoNonResident(ni) \u0026\u0026 !NInoFullyMapped(ni)) {\n@@ -5833,20 +5858,21 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo\n \t\t\treturn err;\n \t}\n \n-\tmutex_lock_nested(\u0026ni-\u003emrec_lock, NTFS_INODE_MUTEX_NORMAL);\n-\tmrec = map_mft_record(ni);\n+\tmutex_lock_nested(\u0026base_ni-\u003emrec_lock, NTFS_INODE_MUTEX_NORMAL);\n+\tmrec = map_mft_record(base_ni);\n \tif (IS_ERR(mrec)) {\n-\t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\tmutex_unlock(\u0026base_ni-\u003emrec_lock);\n \t\treturn PTR_ERR(mrec);\n \t}\n \n-\tctx = ntfs_attr_get_search_ctx(ni, mrec);\n+\tctx = ntfs_attr_get_search_ctx(base_ni, mrec);\n \tif (!ctx) {\n \t\terr = -ENOMEM;\n \t\tgoto out_unmap;\n \t}\n \n-\terr = ntfs_attr_lookup(AT_DATA, AT_UNNAMED, 0, 0, 0, NULL, 0, ctx);\n+\terr = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len,\n+\t\t\tCASE_SENSITIVE, 0, NULL, 0, ctx);\n \tif (err) {\n \t\terr = -EIO;\n \t\tgoto out_unmap;\n@@ -5859,25 +5885,28 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo\n \t\t\tgoto out_unmap;\n \t\tif (keep_size) {\n \t\t\tntfs_attr_reinit_search_ctx(ctx);\n-\t\t\terr = ntfs_attr_lookup(AT_DATA, AT_UNNAMED, 0, 0, 0, NULL, 0, ctx);\n+\t\t\terr = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len,\n+\t\t\t\t\tCASE_SENSITIVE, 0, NULL, 0,\n+\t\t\t\t\tctx);\n \t\t\tif (err) {\n \t\t\t\terr = -EIO;\n \t\t\t\tgoto out_unmap;\n \t\t\t}\n \t\t\tni-\u003edata_size = old_data_size;\n+\t\t\ti_size_write(VFS_I(ni), old_data_size);\n \t\t\tif (NInoNonResident(ni))\n \t\t\t\tctx-\u003eattr-\u003edata.non_resident.data_size =\n \t\t\t\t\tcpu_to_le64(old_data_size);\n \t\t\telse\n \t\t\t\tctx-\u003eattr-\u003edata.resident.value_length =\n \t\t\t\t\tcpu_to_le32((u32)old_data_size);\n-\t\t\tmark_mft_record_dirty(ni);\n+\t\t\tmark_mft_record_dirty(base_ni);\n \t\t}\n \t}\n \n \tntfs_attr_put_search_ctx(ctx);\n-\tunmap_mft_record(ni);\n-\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\tunmap_mft_record(base_ni);\n+\tmutex_unlock(\u0026base_ni-\u003emrec_lock);\n \n \tif (!NInoNonResident(ni))\n \t\tgoto out;\n@@ -5919,12 +5948,13 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo\n \t\t\t}\n \n \t\t\twhile (try_alloc_cnt \u003e 0) {\n-\t\t\t\tmutex_lock_nested(\u0026ni-\u003emrec_lock, NTFS_INODE_MUTEX_NORMAL);\n+\t\t\t\tmutex_lock_nested(\u0026base_ni-\u003emrec_lock,\n+\t\t\t\t\t\tNTFS_INODE_MUTEX_NORMAL);\n \t\t\t\tdown_write(\u0026ni-\u003erunlist.lock);\n \t\t\t\terr = ntfs_attr_map_cluster(ni, vcn, \u0026lcn, \u0026alloc_cnt,\n \t\t\t\t\t\t\t try_alloc_cnt, \u0026balloc, false, false);\n \t\t\t\tup_write(\u0026ni-\u003erunlist.lock);\n-\t\t\t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\t\t\tmutex_unlock(\u0026base_ni-\u003emrec_lock);\n \t\t\t\tif (err)\n \t\t\t\t\tgoto out;\n \n@@ -5952,12 +5982,12 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo\n \t/* allocate clusters outside of initialized_size */\n \ttry_alloc_cnt = vcn_end - vcn;\n \twhile (try_alloc_cnt \u003e 0) {\n-\t\tmutex_lock_nested(\u0026ni-\u003emrec_lock, NTFS_INODE_MUTEX_NORMAL);\n+\t\tmutex_lock_nested(\u0026base_ni-\u003emrec_lock, NTFS_INODE_MUTEX_NORMAL);\n \t\tdown_write(\u0026ni-\u003erunlist.lock);\n \t\terr = ntfs_attr_map_cluster(ni, vcn, \u0026lcn, \u0026alloc_cnt,\n \t\t\t\t\t try_alloc_cnt, \u0026balloc, false, false);\n \t\tup_write(\u0026ni-\u003erunlist.lock);\n-\t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\tmutex_unlock(\u0026base_ni-\u003emrec_lock);\n \t\tif (err || fatal_signal_pending(current))\n \t\t\tgoto signal_out;\n \n@@ -5967,7 +5997,7 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo\n \t}\n \n \tif (NInoRunlistDirty(ni)) {\n-\t\tmutex_lock_nested(\u0026ni-\u003emrec_lock, NTFS_INODE_MUTEX_NORMAL);\n+\t\tmutex_lock_nested(\u0026base_ni-\u003emrec_lock, NTFS_INODE_MUTEX_NORMAL);\n \t\tdown_write(\u0026ni-\u003erunlist.lock);\n \t\terr = ntfs_attr_update_mapping_pairs_locked(ni, 0, ni);\n \t\tif (err)\n@@ -5975,14 +6005,14 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo\n \t\telse\n \t\t\tNInoClearRunlistDirty(ni);\n \t\tup_write(\u0026ni-\u003erunlist.lock);\n-\t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\tmutex_unlock(\u0026base_ni-\u003emrec_lock);\n \t}\n \treturn err;\n out_unmap:\n \tif (ctx)\n \t\tntfs_attr_put_search_ctx(ctx);\n-\tunmap_mft_record(ni);\n-\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\tunmap_mft_record(base_ni);\n+\tmutex_unlock(\u0026base_ni-\u003emrec_lock);\n out:\n \treturn err \u003e= 0 ? 0 : err;\n signal_out:\ndiff --git a/fs/ntfs/attrib.h b/fs/ntfs/attrib.h\nindex 6b4fa9f576401..bee91c9f6f818 100644\n--- a/fs/ntfs/attrib.h\n+++ b/fs/ntfs/attrib.h\n@@ -124,7 +124,7 @@ int ntfs_attr_rm(struct ntfs_inode *ni);\n int ntfs_attr_exist(struct ntfs_inode *ni, const __le32 type, __le16 *name,\n \t\tu32 name_len);\n int ntfs_attr_remove(struct ntfs_inode *ni, const __le32 type, __le16 *name,\n-\t\tu32 name_len);\n+\t\tu32 name_len, struct inode **attr_vi);\n int ntfs_attr_record_rm(struct ntfs_attr_search_ctx *ctx);\n int ntfs_attr_record_move_to(struct ntfs_attr_search_ctx *ctx, struct ntfs_inode *ni);\n int ntfs_attr_add(struct ntfs_inode *ni, __le32 type,\ndiff --git a/fs/ntfs/attrlist.c b/fs/ntfs/attrlist.c\nindex 6edd5d2d9bf24..8291c4c95b370 100644\n--- a/fs/ntfs/attrlist.c\n+++ b/fs/ntfs/attrlist.c\n@@ -374,7 +374,8 @@ int ntfs_attrlist_entry_add(struct ntfs_inode *ni, struct attr_record *attr)\n \n \terr = ntfs_attr_lookup(attr-\u003etype, (attr-\u003ename_length) ? (__le16 *)\n \t\t\t((u8 *)attr + le16_to_cpu(attr-\u003ename_offset)) :\n-\t\t\tAT_UNNAMED, attr-\u003ename_length, CASE_SENSITIVE,\n+\t\t\tAT_UNNAMED, attr-\u003ename_length,\n+\t\t\tCASE_SENSITIVE,\n \t\t\tle64_to_cpu(lowest_vcn),\n \t\t\t(attr-\u003enon_resident) ? NULL : ((u8 *)attr +\n \t\t\tle16_to_cpu(attr-\u003edata.resident.value_offset)), (attr-\u003enon_resident) ?\ndiff --git a/fs/ntfs/dir.c b/fs/ntfs/dir.c\nindex b95173f068cbc..5a0e21e7c283a 100644\n--- a/fs/ntfs/dir.c\n+++ b/fs/ntfs/dir.c\n@@ -8,6 +8,7 @@\n */\n \n #include \u003clinux/blkdev.h\u003e\n+#include \u003clinux/string.h\u003e\n \n #include \"dir.h\"\n #include \"mft.h\"\n@@ -624,7 +625,6 @@ static inline int ntfs_filldir(struct ntfs_volume *vol,\n \t\tntfs_debug(\"Skipping hidden file.\");\n \t\treturn 0;\n \t}\n-\n \tname_len = ntfs_ucstonls(vol, (__le16 *)\u0026ie-\u003ekey.file_name.file_name,\n \t\t\tie-\u003ekey.file_name.file_name_length, \u0026name,\n \t\t\tNTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1);\n@@ -633,7 +633,10 @@ static inline int ntfs_filldir(struct ntfs_volume *vol,\n \t\t\t\t(long long)MREF_LE(ie-\u003edata.dir.indexed_file));\n \t\treturn 0;\n \t}\n-\n+\tif (NVolStreamsWindows(vol) \u0026\u0026 strchr((char *)name, ':')) {\n+\t\tntfs_debug(\"Skipping file name containing a stream separator.\");\n+\t\treturn 0;\n+\t}\n \tmref = MREF_LE(ie-\u003edata.dir.indexed_file);\n \tif (ie-\u003ekey.file_name.file_attributes \u0026 FILE_ATTR_REPARSE_POINT)\n \t\tdt_type = ntfs_reparse_tag_dt_types(vol, mref);\ndiff --git a/fs/ntfs/ea.c b/fs/ntfs/ea.c\nindex ff69fb07f84a4..1104fd72cd2ae 100644\n--- a/fs/ntfs/ea.c\n+++ b/fs/ntfs/ea.c\n@@ -246,7 +246,7 @@ static int ntfs_set_ea(struct inode *inode, const char *name, size_t name_len,\n \t\t\tgoto out;\n \n \t\tif (ntfs_attr_exist(ni, AT_EA, AT_UNNAMED, 0)) {\n-\t\t\terr = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0);\n+\t\t\terr = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0, NULL);\n \t\t\tif (err)\n \t\t\t\tgoto out;\n \t\t}\n@@ -301,11 +301,12 @@ static int ntfs_set_ea(struct inode *inode, const char *name, size_t name_len,\n \t\tp_ea_info-\u003eea_query_length = cpu_to_le32(ea_info_qsize);\n \n \t\tif ((flags \u0026 XATTR_REPLACE) \u0026\u0026 !val_size \u0026\u0026 !ea_info_qsize) {\n-\t\t\terr = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0);\n+\t\t\terr = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0, NULL);\n \t\t\tif (err)\n \t\t\t\tgoto out;\n \n-\t\t\terr = ntfs_attr_remove(ni, AT_EA_INFORMATION, AT_UNNAMED, 0);\n+\t\t\terr = ntfs_attr_remove(ni, AT_EA_INFORMATION, AT_UNNAMED,\n+\t\t\t\t\t0, NULL);\n \t\t\tif (err) {\n \t\t\t\t/* Restore the original $EA if $EA_INFORMATION removal failed. */\n \t\t\t\tntfs_attr_add(ni, AT_EA, AT_UNNAMED, 0, old_ea_buf,\n@@ -610,6 +611,14 @@ static int ntfs_getxattr(const struct xattr_handler *handler,\n \tstruct ntfs_inode *ni = NTFS_I(inode);\n \tint err;\n \n+\t/*\n+\t * Stream permissions and privileges belong to the base inode. This\n+\t * also lets VFS killpriv helpers find the base security attributes.\n+\t */\n+\tif (ntfs_inode_is_named_stream(ni)) {\n+\t\tni = ni-\u003eext.base_ntfs_ino;\n+\t\tinode = VFS_I(ni);\n+\t}\n \tif (NVolShutdown(ni-\u003evol))\n \t\treturn -EIO;\n \n@@ -716,8 +725,8 @@ static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr)\n \t\t\tgoto err_out;\n \n \t\tntfs_attr_reinit_search_ctx(ctx);\n-\t\terr = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename,\n-\t\t\t\tni-\u003ename_len, CASE_SENSITIVE,\n+\t\terr = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len,\n+\t\t\t\tCASE_SENSITIVE,\n \t\t\t\t0, NULL, 0, ctx);\n \t\tif (err) {\n \t\t\terr = -EINVAL;\n@@ -883,6 +892,8 @@ static int ntfs_setxattr(const struct xattr_handler *handler,\n \tint err;\n \t__le32 fattr;\n \n+\tif (ntfs_inode_is_named_stream(ni))\n+\t\treturn -EOPNOTSUPP;\n \tif (NVolShutdown(ni-\u003evol))\n \t\treturn -EIO;\n \ndiff --git a/fs/ntfs/file.c b/fs/ntfs/file.c\nindex 1e2500a521483..a2e843cf947c8 100644\n--- a/fs/ntfs/file.c\n+++ b/fs/ntfs/file.c\n@@ -15,6 +15,13 @@\n #include \u003clinux/posix_acl_xattr.h\u003e\n #include \u003clinux/compat.h\u003e\n #include \u003clinux/falloc.h\u003e\n+#include \u003clinux/file.h\u003e\n+#include \u003clinux/filelock.h\u003e\n+#include \u003clinux/list.h\u003e\n+#include \u003clinux/overflow.h\u003e\n+#include \u003clinux/security.h\u003e\n+#include \u003clinux/slab.h\u003e\n+#include \u003cuapi/linux/ntfs.h\u003e\n \n #include \"lcnalloc.h\"\n #include \"ntfs.h\"\n@@ -23,8 +30,8 @@\n #include \"iomap.h\"\n #include \"bitmap.h\"\n #include \"volume.h\"\n-\n-#include \u003clinux/filelock.h\u003e\n+#include \"mft.h\"\n+#include \"stream.h\"\n \n /*\n * ntfs_file_open - called when an inode is about to be opened\n@@ -44,7 +51,7 @@\n *\n * After the check passes, just call generic_file_open() to do its work.\n */\n-static int ntfs_file_open(struct inode *vi, struct file *filp)\n+int ntfs_file_open(struct inode *vi, struct file *filp)\n {\n \tstruct ntfs_inode *ni = NTFS_I(vi);\n \n@@ -78,6 +85,7 @@ static int ntfs_file_open(struct inode *vi, struct file *filp)\n static int ntfs_trim_prealloc(struct inode *vi)\n {\n \tstruct ntfs_inode *ni = NTFS_I(vi);\n+\tstruct ntfs_inode *mrec_ni = ntfs_base_inode(ni);\n \tstruct ntfs_volume *vol = ni-\u003evol;\n \tstruct runlist_element *rl;\n \ts64 aligned_data_size;\n@@ -86,7 +94,7 @@ static int ntfs_trim_prealloc(struct inode *vi)\n \tint err = 0;\n \n \tinode_lock(vi);\n-\tmutex_lock(\u0026ni-\u003emrec_lock);\n+\tmutex_lock(\u0026mrec_ni-\u003emrec_lock);\n \tdown_write(\u0026ni-\u003erunlist.lock);\n \n \taligned_data_size = round_up(ni-\u003edata_size, vol-\u003ecluster_size);\n@@ -121,13 +129,13 @@ static int ntfs_trim_prealloc(struct inode *vi)\n \n out_unlock:\n \tup_write(\u0026ni-\u003erunlist.lock);\n-\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \tinode_unlock(vi);\n \n \treturn err;\n }\n \n-static int ntfs_file_release(struct inode *vi, struct file *filp)\n+int ntfs_file_release(struct inode *vi, struct file *filp)\n {\n \tif (!NInoCompressed(NTFS_I(vi)) \u0026\u0026\n \t !NInoWofCompressed(NTFS_I(vi)))\n@@ -136,6 +144,11 @@ static int ntfs_file_release(struct inode *vi, struct file *filp)\n \treturn 0;\n }\n \n+struct ntfs_fsync_attr {\n+\tstruct list_head list;\n+\tstruct inode *inode;\n+};\n+\n /*\n * ntfs_file_fsync - sync a file to disk\n * @filp:\tfile to be synced\n@@ -156,7 +169,7 @@ static int ntfs_file_release(struct inode *vi, struct file *filp)\n * Also, if @datasync is true, we do not wait on the inode to be written out\n * but we always wait on the page cache pages to be written out.\n */\n-static int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,\n+int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,\n \t\t\t int datasync)\n {\n \tstruct inode *vi = filp-\u003ef_mapping-\u003ehost;\n@@ -165,6 +178,9 @@ static int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,\n \tint err, ret = 0;\n \tstruct inode *parent_vi, *ia_vi;\n \tstruct ntfs_attr_search_ctx *ctx;\n+\tstruct ntfs_fsync_attr *attr, *next;\n+\tLIST_HEAD(attrs);\n+\tbool non_resident, stream;\n \n \tntfs_debug(\"Entering for inode 0x%llx.\", ni-\u003emft_no);\n \n@@ -175,10 +191,24 @@ static int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,\n \tif (err)\n \t\treturn err;\n \n-\tif (!datasync || !NInoNonResident(NTFS_I(vi)))\n+\tstream = ntfs_inode_is_named_stream(ni);\n+\tnon_resident = NInoNonResident(ni);\n+\tif (stream) {\n+\t\tni = ni-\u003eext.base_ntfs_ino;\n+\t\tvi = VFS_I(ni);\n+\t}\n+\n+\tif (!datasync || !non_resident)\n \t\tret = __ntfs_write_inode(vi, 1);\n \twrite_inode_now(vi, !datasync);\n \n+\t/*\n+\t * file_write_and_wait_range() already flushed this stream mapping.\n+\t * A stream fsync does not need to flush sibling attribute mappings.\n+\t */\n+\tif (stream)\n+\t\tgoto sync_volume;\n+\n \tctx = ntfs_attr_get_search_ctx(ni, NULL);\n \tif (!ctx)\n \t\treturn -ENOMEM;\n@@ -210,23 +240,55 @@ static int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,\n \t\t\tname = (__le16 *)((u8 *)ctx-\u003eattr + le16_to_cpu(ctx-\u003eattr-\u003ename_offset));\n \t\t\tif (ctx-\u003eattr-\u003etype == AT_DATA \u0026\u0026 ctx-\u003eattr-\u003ename_length == 0)\n \t\t\t\tcontinue;\n+\t\t\tif (ctx-\u003eattr-\u003etype == AT_DATA \u0026\u0026\n+\t\t\t ntfs_stream_unlinked(ni, name, ctx-\u003eattr-\u003ename_length))\n+\t\t\t\tcontinue;\n \n+\t\t\tattr = kmalloc_obj(*attr, GFP_NOFS);\n+\t\t\tif (!attr) {\n+\t\t\t\terr = -ENOMEM;\n+\t\t\t\tbreak;\n+\t\t\t}\n \t\t\tattr_vi = ntfs_attr_iget(vi, ctx-\u003eattr-\u003etype,\n \t\t\t\t\t\t name, ctx-\u003eattr-\u003ename_length);\n-\t\t\tif (IS_ERR(attr_vi))\n+\t\t\tif (IS_ERR(attr_vi)) {\n+\t\t\t\tkfree(attr);\n \t\t\t\tcontinue;\n-\t\t\tspin_lock(\u0026attr_vi-\u003ei_lock);\n-\t\t\tif (inode_state_read_once(attr_vi) \u0026 I_DIRTY_PAGES) {\n-\t\t\t\tspin_unlock(\u0026attr_vi-\u003ei_lock);\n-\t\t\t\tfilemap_write_and_wait(attr_vi-\u003ei_mapping);\n-\t\t\t} else\n-\t\t\t\tspin_unlock(\u0026attr_vi-\u003ei_lock);\n-\t\t\tiput(attr_vi);\n+\t\t\t}\n+\t\t\tif (ntfs_inode_is_named_stream(NTFS_I(attr_vi)))\n+\t\t\t\tatomic_inc(\u0026NTFS_I(attr_vi)-\u003estream_open_count);\n+\t\t\tattr-\u003einode = attr_vi;\n+\t\t\tlist_add_tail(\u0026attr-\u003elist, \u0026attrs);\n \t\t}\n \t}\n \tmutex_unlock(\u0026ni-\u003emrec_lock);\n \tntfs_attr_put_search_ctx(ctx);\n+\tif (err != -ENOENT \u0026\u0026 !ret)\n+\t\tret = err;\n \n+\t/* Attribute writeback takes the base inode's MFT record lock. */\n+\tlist_for_each_entry_safe(attr, next, \u0026attrs, list) {\n+\t\tstruct inode *attr_vi = attr-\u003einode;\n+\n+\t\terr = filemap_write_and_wait(attr_vi-\u003ei_mapping);\n+\t\tif (err \u0026\u0026 !ret)\n+\t\t\tret = err;\n+\t\tif (ntfs_inode_is_named_stream(NTFS_I(attr_vi))) {\n+\t\t\terr = ntfs_stream_put(attr_vi);\n+\t\t\tif (err \u0026\u0026 !ret)\n+\t\t\t\tret = err;\n+\t\t}\n+\t\tiput(attr_vi);\n+\t\tlist_del(\u0026attr-\u003elist);\n+\t\tkfree(attr);\n+\t}\n+\n+\t/* Attribute writeback may have updated the base mapping pairs. */\n+\terr = write_inode_now(vi, 1);\n+\tif (err \u0026\u0026 !ret)\n+\t\tret = err;\n+\n+sync_volume:\n \twrite_inode_now(vol-\u003emftbmp_ino, 1);\n \tdown_write(\u0026vol-\u003elcnbmp_lock);\n \twrite_inode_now(vol-\u003elcnbmp_ino, 1);\n@@ -268,12 +330,17 @@ static void ntfs_pagecache_extend(struct inode *vi, loff_t from, loff_t to)\n \t\t\t\t PAGE_SIZE, 0);\n }\n \n-static int ntfs_setattr_size(struct inode *vi, struct iattr *attr)\n+int ntfs_setattr_size(struct inode *vi, struct iattr *attr)\n {\n \tstruct ntfs_inode *ni = NTFS_I(vi);\n+\tstruct ntfs_inode *base_ni = ntfs_base_inode(ni);\n+\tbool stream = ntfs_inode_is_named_stream(ni);\n \tint err;\n \tloff_t old_size = vi-\u003ei_size;\n \n+\tif (stream \u0026\u0026 NVolShutdown(ni-\u003evol))\n+\t\treturn -EIO;\n+\n \tif (NInoCompressed(ni) || NInoEncrypted(ni) || NInoWofCompressed(ni)) {\n \t\tntfs_warning(\n \t\t\tvi-\u003ei_sb,\n@@ -293,7 +360,21 @@ static int ntfs_setattr_size(struct inode *vi, struct iattr *attr)\n \t * readers cannot observe the size change until the attribute\n \t * updates below have completed.\n \t */\n-\tfilemap_invalidate_lock(vi-\u003ei_mapping);\n+\tif (stream) {\n+\t\tfilemap_invalidate_lock(vi-\u003ei_mapping);\n+\t\terr = filemap_write_and_wait(vi-\u003ei_mapping);\n+\t\tif (err)\n+\t\t\tgoto out_unlock_mapping;\n+\n+\t\tmutex_lock(\u0026base_ni-\u003emrec_lock);\n+\t\terr = ntfs_stream_inode_validate(vi);\n+\t\tmutex_unlock(\u0026base_ni-\u003emrec_lock);\n+\t\tif (err)\n+\t\t\tgoto out_unlock_mapping;\n+\t} else {\n+\t\tfilemap_invalidate_lock(vi-\u003ei_mapping);\n+\t}\n+\n \tif (attr-\u003eia_size \u003e old_size) {\n \t\ttruncate_pagecache(vi, old_size);\n \t\ti_size_write(vi, attr-\u003eia_size);\n@@ -302,10 +383,25 @@ static int ntfs_setattr_size(struct inode *vi, struct iattr *attr)\n \t\ttruncate_setsize(vi, attr-\u003eia_size);\n \t}\n \n-\terr = ntfs_truncate_vfs(vi, attr-\u003eia_size, old_size);\n-\tif (err)\n+\tif (stream) {\n+\t\tmutex_lock(\u0026base_ni-\u003emrec_lock);\n+\t\terr = ntfs_stream_inode_validate(vi);\n+\t\tif (!err)\n+\t\t\terr = __ntfs_attr_truncate_vfs(ni, attr-\u003eia_size,\n+\t\t\t\t\told_size);\n+\t\tmutex_unlock(\u0026base_ni-\u003emrec_lock);\n+\t} else {\n+\t\terr = ntfs_truncate_vfs(vi, attr-\u003eia_size, old_size);\n+\t}\n+\tif (err) {\n \t\ti_size_write(vi, old_size);\n+\t\tgoto out_unlock_mapping;\n+\t}\n+\n+out_unlock_mapping:\n \tfilemap_invalidate_unlock(vi-\u003ei_mapping);\n+\tif (!err \u0026\u0026 stream)\n+\t\tntfs_stream_update_base_time(base_ni);\n \n \treturn err;\n }\n@@ -437,7 +533,25 @@ int ntfs_getattr(struct mnt_idmap *idmap, const struct path *path,\n \treturn 0;\n }\n \n-static loff_t ntfs_file_llseek(struct file *file, loff_t offset, int whence)\n+/*\n+ * Validate a stream before VFS write handling removes privileges or updates\n+ * timestamps on its base inode.\n+ */\n+static int ntfs_file_modified(struct kiocb *iocb)\n+{\n+\tstruct inode *inode = file_inode(iocb-\u003eki_filp);\n+\tint err;\n+\n+\tif (ntfs_inode_is_named_stream(NTFS_I(inode))) {\n+\t\terr = ntfs_stream_validate_for_mutation(inode,\n+\t\t\t\tiocb-\u003eki_flags \u0026 IOCB_NOWAIT);\n+\t\tif (err)\n+\t\t\treturn err;\n+\t}\n+\treturn kiocb_modified(iocb);\n+}\n+\n+loff_t ntfs_file_llseek(struct file *file, loff_t offset, int whence)\n {\n \tstruct inode *inode = file-\u003ef_mapping-\u003ehost;\n \n@@ -466,7 +580,7 @@ static loff_t ntfs_file_llseek(struct file *file, loff_t offset, int whence)\n \treturn vfs_setpos(file, offset, inode-\u003ei_sb-\u003es_maxbytes);\n }\n \n-static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)\n+ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)\n {\n \tstruct inode *vi = file_inode(iocb-\u003eki_filp);\n \tstruct super_block *sb = vi-\u003ei_sb;\n@@ -513,7 +627,11 @@ static int ntfs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,\n \tif (size) {\n \t\tif (i_size_read(inode) \u003c iocb-\u003eki_pos + size) {\n \t\t\ti_size_write(inode, iocb-\u003eki_pos + size);\n-\t\t\tmark_inode_dirty(inode);\n+\t\t\tif (ntfs_inode_is_named_stream(NTFS_I(inode)))\n+\t\t\t\tmark_inode_dirty(VFS_I(\n+\t\t\t\t\tNTFS_I(inode)-\u003eext.base_ntfs_ino));\n+\t\t\telse\n+\t\t\t\tmark_inode_dirty(inode);\n \t\t}\n \t}\n \n@@ -583,6 +701,7 @@ static ssize_t ntfs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)\n static int ntfs_expand_for_write(struct ntfs_inode *ni, loff_t end)\n {\n \tstruct ntfs_volume *vol = ni-\u003evol;\n+\tstruct ntfs_inode *mrec_ni = ntfs_base_inode(ni);\n \tloff_t prealloc_size = 0;\n \tint err;\n \n@@ -598,14 +717,14 @@ static int ntfs_expand_for_write(struct ntfs_inode *ni, loff_t end)\n \t\tprealloc_size = ni-\u003eallocated_size + vol-\u003epreallocated_size;\n \t}\n \n-\tmutex_lock(\u0026ni-\u003emrec_lock);\n+\tmutex_lock(\u0026mrec_ni-\u003emrec_lock);\n \terr = ntfs_attr_expand(ni, end, prealloc_size);\n-\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \n \treturn err;\n }\n \n-static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)\n+ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)\n {\n \tstruct file *file = iocb-\u003eki_filp;\n \tstruct inode *vi = file-\u003ef_mapping-\u003ehost;\n@@ -642,7 +761,7 @@ static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)\n \tif (ret \u003c= 0)\n \t\tgoto out_lock;\n \n-\terr = file_modified(iocb-\u003eki_filp);\n+\terr = ntfs_file_modified(iocb);\n \tif (err) {\n \t\tret = err;\n \t\tgoto out_lock;\n@@ -692,13 +811,21 @@ static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)\n out:\n \tif (ret \u003c 0 \u0026\u0026 ret != -EIOCBQUEUED) {\n \t\tif (ni-\u003einitialized_size != old_init_size) {\n-\t\t\tmutex_lock(\u0026ni-\u003emrec_lock);\n+\t\t\tstruct ntfs_inode *mrec_ni =\n+\t\t\t\tntfs_base_inode(ni);\n+\n+\t\t\tmutex_lock(\u0026mrec_ni-\u003emrec_lock);\n \t\t\tntfs_attr_set_initialized_size(ni, old_init_size);\n-\t\t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \t\t}\n \t\tif (ni-\u003edata_size != old_data_size) {\n+\t\t\tstruct ntfs_inode *mrec_ni =\n+\t\t\t\tntfs_base_inode(ni);\n+\n \t\t\ttruncate_setsize(vi, old_data_size);\n+\t\t\tmutex_lock(\u0026mrec_ni-\u003emrec_lock);\n \t\t\tntfs_attr_truncate(ni, old_data_size);\n+\t\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \t\t}\n \t}\n out_lock:\n@@ -712,13 +839,24 @@ static vm_fault_t ntfs_filemap_page_mkwrite(struct vm_fault *vmf)\n {\n \tstruct inode *inode = file_inode(vmf-\u003evma-\u003evm_file);\n \tstruct address_space *mapping = inode-\u003ei_mapping;\n+\tbool stream = ntfs_inode_is_named_stream(NTFS_I(inode));\n+\tint err;\n \tvm_fault_t ret;\n \n \tif (NInoWofCompressed(NTFS_I(inode)))\n \t\treturn VM_FAULT_SIGBUS;\n \n \tsb_start_pagefault(inode-\u003ei_sb);\n-\tfile_update_time(vmf-\u003evma-\u003evm_file);\n+\tif (stream) {\n+\t\terr = ntfs_stream_validate_for_mutation(inode, false);\n+\t\tif (err)\n+\t\t\tgoto out_error;\n+\t\terr = file_update_time(vmf-\u003evma-\u003evm_file);\n+\t\tif (err)\n+\t\t\tgoto out_error;\n+\t} else {\n+\t\tfile_update_time(vmf-\u003evma-\u003evm_file);\n+\t}\n \n \t/*\n \t * Serialize against truncate/fallocate which hold the lock\n@@ -727,9 +865,12 @@ static vm_fault_t ntfs_filemap_page_mkwrite(struct vm_fault *vmf)\n \tfilemap_invalidate_lock_shared(mapping);\n \tret = iomap_page_mkwrite(vmf, \u0026ntfs_page_mkwrite_iomap_ops, NULL);\n \tfilemap_invalidate_unlock_shared(mapping);\n-\n \tsb_end_pagefault(inode-\u003ei_sb);\n \treturn ret;\n+\n+out_error:\n+\tsb_end_pagefault(inode-\u003ei_sb);\n+\treturn vmf_error(err);\n }\n \n static const struct vm_operations_struct ntfs_file_vm_ops = {\n@@ -738,7 +879,7 @@ static const struct vm_operations_struct ntfs_file_vm_ops = {\n \t.page_mkwrite\t= ntfs_filemap_page_mkwrite,\n };\n \n-static int ntfs_file_mmap_prepare(struct vm_area_desc *desc)\n+int ntfs_file_mmap_prepare(struct vm_area_desc *desc)\n {\n \tstruct file *file = desc-\u003efile;\n \tstruct inode *inode = file_inode(file);\n@@ -771,7 +912,7 @@ static int ntfs_file_mmap_prepare(struct vm_area_desc *desc)\n \treturn 0;\n }\n \n-static int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,\n+int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,\n \t\tu64 start, u64 len)\n {\n \tif (NInoWofCompressed(NTFS_I(inode)))\n@@ -808,7 +949,7 @@ static const char *ntfs_get_link(struct dentry *dentry, struct inode *inode,\n \treturn ni-\u003etarget;\n }\n \n-static ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos,\n+ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos,\n \t\tstruct pipe_inode_info *pipe, size_t len, unsigned int flags)\n {\n \tif (NVolShutdown(NTFS_SB(in-\u003ef_mapping-\u003ehost-\u003ei_sb)))\n@@ -924,6 +1065,14 @@ long ntfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)\n \t\treturn ntfs_ioctl_set_volume_label(filp, arg);\n \tcase FITRIM:\n \t\treturn ntfs_ioctl_fitrim(NTFS_SB(file_inode(filp)-\u003ei_sb), arg);\n+\tcase NTFS_IOC_STREAM_READ:\n+\t\treturn ntfs_ioctl_stream_read(filp, arg);\n+\tcase NTFS_IOC_STREAM_WRITE:\n+\t\treturn ntfs_ioctl_stream_write(filp, arg);\n+\tcase NTFS_IOC_STREAM_REMOVE:\n+\t\treturn ntfs_ioctl_stream_remove(filp, arg);\n+\tcase NTFS_IOC_LIST_STREAMS:\n+\t\treturn ntfs_ioctl_list_streams(filp, arg);\n \tdefault:\n \t\treturn -ENOTTY;\n \t}\n@@ -980,6 +1129,7 @@ static int ntfs_allocate_range(struct ntfs_inode *ni, int mode, loff_t offset,\n static int ntfs_punch_hole(struct ntfs_inode *ni, int mode, loff_t offset,\n \t\tloff_t len)\n {\n+\tstruct ntfs_inode *mrec_ni = ntfs_base_inode(ni);\n \tstruct ntfs_volume *vol = ni-\u003evol;\n \tstruct inode *vi = VFS_I(ni);\n \tloff_t end_offset;\n@@ -1044,16 +1194,17 @@ static int ntfs_punch_hole(struct ntfs_inode *ni, int mode, loff_t offset,\n \t\tend_vcn--;\n \t}\n \n-\tmutex_lock_nested(\u0026ni-\u003emrec_lock, NTFS_INODE_MUTEX_NORMAL);\n+\tmutex_lock_nested(\u0026mrec_ni-\u003emrec_lock, NTFS_INODE_MUTEX_NORMAL);\n \terr = ntfs_non_resident_attr_punch_hole(ni, start_vcn,\n \t\t\tend_vcn - start_vcn);\n-\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n out:\n \treturn err;\n }\n \n static int ntfs_collapse_range(struct ntfs_inode *ni, loff_t offset, loff_t len)\n {\n+\tstruct ntfs_inode *mrec_ni = ntfs_base_inode(ni);\n \tstruct ntfs_volume *vol = ni-\u003evol;\n \tstruct inode *vi = VFS_I(ni);\n \tloff_t old_size, new_size;\n@@ -1087,10 +1238,10 @@ static int ntfs_collapse_range(struct ntfs_inode *ni, loff_t offset, loff_t len)\n \n \ttruncate_pagecache(vi, offset_down);\n \n-\tmutex_lock_nested(\u0026ni-\u003emrec_lock, NTFS_INODE_MUTEX_NORMAL);\n+\tmutex_lock_nested(\u0026mrec_ni-\u003emrec_lock, NTFS_INODE_MUTEX_NORMAL);\n \terr = ntfs_non_resident_attr_collapse_range(ni, start_vcn,\n \t\t\tend_vcn - start_vcn);\n-\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \n \tif (new_size != old_size)\n \t\ti_size_write(vi, ni-\u003edata_size);\n@@ -1100,6 +1251,7 @@ static int ntfs_collapse_range(struct ntfs_inode *ni, loff_t offset, loff_t len)\n \n static int ntfs_insert_range(struct ntfs_inode *ni, loff_t offset, loff_t len)\n {\n+\tstruct ntfs_inode *mrec_ni = ntfs_base_inode(ni);\n \tstruct ntfs_volume *vol = ni-\u003evol;\n \tstruct inode *vi = VFS_I(ni);\n \tloff_t offset_down = round_down(offset,\n@@ -1143,10 +1295,10 @@ static int ntfs_insert_range(struct ntfs_inode *ni, loff_t offset, loff_t len)\n \n \ttruncate_pagecache(vi, offset_down);\n \n-\tmutex_lock_nested(\u0026ni-\u003emrec_lock, NTFS_INODE_MUTEX_NORMAL);\n+\tmutex_lock_nested(\u0026mrec_ni-\u003emrec_lock, NTFS_INODE_MUTEX_NORMAL);\n \terr = ntfs_non_resident_attr_insert_range(ni, start_vcn,\n \t\t\tend_vcn - start_vcn);\n-\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \n \tif (new_size != old_size)\n \t\ti_size_write(vi, ni-\u003edata_size);\n@@ -1159,11 +1311,13 @@ static int ntfs_insert_range(struct ntfs_inode *ni, loff_t offset, loff_t len)\n \t\t FALLOC_FL_INSERT_RANGE | FALLOC_FL_PUNCH_HOLE |\t\\\n \t\t FALLOC_FL_COLLAPSE_RANGE)\n \n-static long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)\n+long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)\n {\n \tstruct inode *vi = file_inode(file);\n \tstruct ntfs_inode *ni = NTFS_I(vi);\n+\tstruct ntfs_inode *base_ni = ntfs_base_inode(ni);\n \tstruct ntfs_volume *vol = ni-\u003evol;\n+\tbool stream = ntfs_inode_is_named_stream(ni);\n \tint err = 0;\n \tloff_t old_size, new_size;\n \n@@ -1193,6 +1347,13 @@ static long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t le\n \t\tinode_unlock(vi);\n \t\treturn -EOPNOTSUPP;\n \t}\n+\tif (stream) {\n+\t\terr = ntfs_stream_validate_for_mutation(vi, false);\n+\t\tif (err) {\n+\t\t\tinode_unlock(vi);\n+\t\t\treturn err;\n+\t\t}\n+\t}\n \told_size = i_size_read(vi);\n \n \tinode_dio_wait(vi);\n@@ -1233,9 +1394,13 @@ static long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t le\n \tfilemap_invalidate_unlock(vi-\u003ei_mapping);\n \n \tif (!err) {\n-\t\tNInoSetFileNameDirty(ni);\n-\t\tinode_set_mtime_to_ts(vi, inode_set_ctime_current(vi));\n-\t\tmark_inode_dirty(vi);\n+\t\tif (stream) {\n+\t\t\tntfs_stream_update_base_time(base_ni);\n+\t\t} else {\n+\t\t\tNInoSetFileNameDirty(ni);\n+\t\t\tinode_set_mtime_to_ts(vi, inode_set_ctime_current(vi));\n+\t\t\tmark_inode_dirty(vi);\n+\t\t}\n \t}\n \n \tinode_unlock(vi);\ndiff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c\nindex 6cfe8f24e4cce..93680a11fdccc 100644\n--- a/fs/ntfs/inode.c\n+++ b/fs/ntfs/inode.c\n@@ -20,6 +20,7 @@\n #include \"attrib.h\"\n #include \"iomap.h\"\n #include \"object_id.h\"\n+#include \"stream.h\"\n \n /*\n * ntfs_test_inode - compare two (possibly fake) inodes for equality\n@@ -56,9 +57,18 @@ int ntfs_test_inode(struct inode *vi, void *data)\n \t\t\treturn 0;\n \t\tif (ni-\u003ename_len != na-\u003ename_len)\n \t\t\treturn 0;\n-\t\tif (na-\u003ename_len \u0026\u0026 memcmp(ni-\u003ename, na-\u003ename,\n-\t\t\t\tna-\u003ename_len * sizeof(__le16)))\n-\t\t\treturn 0;\n+\t\tif (na-\u003ename_len) {\n+\t\t\tif (ntfs_inode_is_named_stream(ni)) {\n+\t\t\t\tif (!ntfs_names_are_equal(ni-\u003ename, ni-\u003ename_len,\n+\t\t\t\t\t\tna-\u003ename, na-\u003ename_len,\n+\t\t\t\t\t\tIGNORE_CASE, ni-\u003evol-\u003eupcase,\n+\t\t\t\t\t\tni-\u003evol-\u003eupcase_len))\n+\t\t\t\t\treturn 0;\n+\t\t\t} else if (memcmp(ni-\u003ename, na-\u003ename,\n+\t\t\t\t\tna-\u003ename_len * sizeof(__le16))) {\n+\t\t\t\treturn 0;\n+\t\t\t}\n+\t\t}\n \t\tif (!ni-\u003eext.base_ntfs_ino)\n \t\t\treturn 0;\n \t}\n@@ -67,6 +77,33 @@ int ntfs_test_inode(struct inode *vi, void *data)\n \treturn 1;\n }\n \n+/*\n+ * ntfs_test_inode_rcu() - Test an inode during RCU hash lookup\n+ * @vi: inode being tested\n+ * @data: NTFS attribute key to match\n+ *\n+ * Reject inodes being initialized or destroyed, then run the normal NTFS\n+ * inode match while holding @vi's inode lock.\n+ *\n+ * Return: 1 if the inode matches, or 0 otherwise.\n+ */\n+int ntfs_test_inode_rcu(struct inode *vi, void *data)\n+{\n+\tunsigned long state;\n+\tint ret;\n+\n+\tspin_lock(\u0026vi-\u003ei_lock);\n+\tstate = inode_state_read_once(vi);\n+\tif (state \u0026 (I_FREEING | I_WILL_FREE | I_NEW)) {\n+\t\tret = 0;\n+\t\tgoto out;\n+\t}\n+\tret = ntfs_test_inode(vi, data);\n+out:\n+\tspin_unlock(\u0026vi-\u003ei_lock);\n+\treturn ret;\n+}\n+\n /*\n * ntfs_init_locked_inode - initialize an inode\n * @vi:\t\tvfs inode to initialize\n@@ -468,6 +505,7 @@ void __ntfs_init_inode(struct super_block *sb, struct ntfs_inode *ni)\n \tni-\u003eseq_no = 0;\n \tatomic_set(\u0026ni-\u003ecount, 1);\n \tni-\u003evol = NTFS_SB(sb);\n+\tatomic_set(\u0026ni-\u003estream_open_count, 0);\n \tntfs_init_runlist(\u0026ni-\u003erunlist);\n \tmutex_init(\u0026ni-\u003emrec_lock);\n \tif (ni-\u003etype == AT_ATTRIBUTE_LIST) {\n@@ -1320,6 +1358,7 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)\n \tstruct attr_record *a;\n \tstruct ntfs_attr_search_ctx *ctx;\n \tint err = 0;\n+\tu32 ic;\n \n \tntfs_debug(\"Entering for i_ino 0x%llx.\", ni-\u003emft_no);\n \n@@ -1334,8 +1373,12 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)\n \tinode_set_atime_to_ts(vi, inode_get_atime(base_vi));\n \tvi-\u003ei_generation = ni-\u003eseq_no = base_ni-\u003eseq_no;\n \n-\t/* Set inode type to zero but preserve permissions. */\n-\tvi-\u003ei_mode\t= base_vi-\u003ei_mode \u0026 ~S_IFMT;\n+\t/* Named data streams are regular files throughout initialization. */\n+\tvi-\u003ei_mode = base_vi-\u003ei_mode \u0026 ~S_IFMT;\n+\tif (ni-\u003etype == AT_DATA \u0026\u0026 ni-\u003ename_len) {\n+\t\tvi-\u003ei_mode |= S_IFREG;\n+\t\tvi-\u003ei_flags = base_vi-\u003ei_flags;\n+\t}\n \n \tm = map_mft_record(base_ni);\n \tif (IS_ERR(m)) {\n@@ -1348,11 +1391,29 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)\n \t\tgoto unm_err_out;\n \t}\n \t/* Find the attribute. */\n+\tic = ntfs_inode_is_named_stream(ni) ? IGNORE_CASE : CASE_SENSITIVE;\n \terr = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len,\n-\t\t\tCASE_SENSITIVE, 0, NULL, 0, ctx);\n+\t\t\tic, 0, NULL, 0, ctx);\n \tif (unlikely(err))\n \t\tgoto unm_err_out;\n \ta = ctx-\u003eattr;\n+\tif (ntfs_inode_is_named_stream(ni)) {\n+\t\t__le16 *disk_name = (__le16 *)((u8 *)a +\n+\t\t\t\tle16_to_cpu(a-\u003ename_offset));\n+\n+\t\t/* Subsequent extent and mutation lookups must select this name. */\n+\t\tif (ni-\u003ename == I30) {\n+\t\t\tni-\u003ename = kmalloc_array(ni-\u003ename_len + 1,\n+\t\t\t\t\t\tsizeof(__le16), GFP_NOFS);\n+\t\t\tif (!ni-\u003ename) {\n+\t\t\t\terr = -ENOMEM;\n+\t\t\t\tgoto unm_err_out;\n+\t\t\t}\n+\t\t}\n+\t\tmemcpy(ni-\u003ename, disk_name,\n+\t\t\tni-\u003ename_len * sizeof(__le16));\n+\t\tni-\u003ename[ni-\u003ename_len] = 0;\n+\t}\n \tif (a-\u003eflags \u0026 (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) {\n \t\tif (a-\u003eflags \u0026 ATTR_COMPRESSION_MASK) {\n \t\t\tNInoSetCompressed(ni);\n@@ -1486,6 +1547,10 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)\n \t\tvi-\u003ei_blocks = ni-\u003eitype.compressed.size \u003e\u003e 9;\n \telse\n \t\tvi-\u003ei_blocks = ni-\u003eallocated_size \u003e\u003e 9;\n+\tif (ni-\u003etype == AT_DATA \u0026\u0026 ni-\u003ename_len) {\n+\t\tvi-\u003ei_op = \u0026ntfs_stream_inode_ops;\n+\t\tvi-\u003ei_fop = \u0026ntfs_stream_file_ops;\n+\t}\n \t/*\n \t * Make sure the base inode does not go away and attach it to the\n \t * attribute inode.\n@@ -2476,6 +2541,10 @@ int ntfs_show_options(struct seq_file *sf, struct dentry *root)\n \t\tseq_puts(sf, \",symlink=native\");\n \telse\n \t\tseq_puts(sf, \",symlink=wsl\");\n+\tif (NVolStreamsWindows(vol))\n+\t\tseq_puts(sf, \",streams_interface=windows\");\n+\telse\n+\t\tseq_puts(sf, \",streams_interface=none\");\n \tif (vol-\u003esb-\u003es_flags \u0026 SB_POSIXACL)\n \t\tseq_puts(sf, \",acl\");\n \treturn 0;\n@@ -2485,6 +2554,7 @@ int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset,\n \t\t\t\t const loff_t new_size)\n {\n \tstruct ntfs_inode *ni = NTFS_I(vi);\n+\tstruct ntfs_inode *mrec_ni = ntfs_base_inode(ni);\n \tloff_t old_init_size, clear_size;\n \tunsigned long flags;\n \tint err = 0;\n@@ -2522,9 +2592,9 @@ int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset,\n \t\t}\n \t}\n \n-\tmutex_lock(\u0026ni-\u003emrec_lock);\n+\tmutex_lock(\u0026mrec_ni-\u003emrec_lock);\n \terr = ntfs_attr_set_initialized_size(ni, new_size);\n-\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \tif (err)\n \t\ttruncate_setsize(vi, old_init_size);\n out:\n@@ -2535,15 +2605,19 @@ int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset,\n int ntfs_truncate_vfs(struct inode *vi, loff_t new_size, loff_t i_size)\n {\n \tstruct ntfs_inode *ni = NTFS_I(vi);\n+\tstruct ntfs_inode *mrec_ni = ntfs_base_inode(ni);\n \tint err;\n \n-\tmutex_lock(\u0026ni-\u003emrec_lock);\n+\tmutex_lock(\u0026mrec_ni-\u003emrec_lock);\n \terr = __ntfs_attr_truncate_vfs(ni, new_size, i_size);\n-\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \tif (err \u003c 0)\n \t\treturn err;\n \n-\tinode_set_mtime_to_ts(vi, inode_set_ctime_current(vi));\n+\tif (ntfs_inode_is_named_stream(ni))\n+\t\tntfs_stream_update_base_time(mrec_ni);\n+\telse\n+\t\tinode_set_mtime_to_ts(vi, inode_set_ctime_current(vi));\n \treturn 0;\n }\n \n@@ -3615,19 +3689,30 @@ s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf)\n \tstruct address_space *mapping = vi-\u003ei_mapping;\n \tstruct folio *folio;\n \tstruct ntfs_inode *ni = NTFS_I(vi);\n+\tstruct ntfs_inode *base_ni = ni-\u003eext.base_ntfs_ino;\n+\tstruct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?\n+\t\t\tbase_ni : ni;\n \ts64 isize;\n \tu32 attr_len, total = 0, offset;\n \tpgoff_t index;\n \tint err = 0;\n+\tbool claimed = false;\n \n \tWARN_ON(!NInoAttr(ni));\n \tif (!count)\n \t\treturn 0;\n \n-\tmutex_lock(\u0026ni-\u003emrec_lock);\n+\tmutex_lock(\u0026mrec_ni-\u003emrec_lock);\n+\tif (ntfs_inode_is_named_stream(ni)) {\n+\t\terr = ntfs_stream_inode_validate(vi);\n+\t\tif (err) {\n+\t\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n+\t\t\treturn err;\n+\t\t}\n+\t}\n \tisize = i_size_read(vi);\n \tif (pos \u003e isize) {\n-\t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \t\treturn -EINVAL;\n \t}\n \tif (pos + count \u003e isize)\n@@ -3637,37 +3722,44 @@ s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf)\n \t\tstruct ntfs_attr_search_ctx *ctx;\n \t\tu8 *attr;\n \n-\t\tctx = ntfs_attr_get_search_ctx(ni-\u003eext.base_ntfs_ino, NULL);\n+\t\tctx = ntfs_attr_get_search_ctx(base_ni, NULL);\n \t\tif (!ctx) {\n \t\t\tntfs_error(vi-\u003ei_sb, \"Failed to get attr search ctx\");\n \t\t\terr = -ENOMEM;\n-\t\t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \t\t\tgoto out;\n \t\t}\n \n-\t\terr = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len, CASE_SENSITIVE,\n+\t\terr = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len,\n+\t\t\t\t CASE_SENSITIVE,\n \t\t\t\t 0, NULL, 0, ctx);\n \t\tif (err) {\n \t\t\tntfs_error(vi-\u003ei_sb, \"Failed to look up attr %#x\", ni-\u003etype);\n \t\t\tntfs_attr_put_search_ctx(ctx);\n-\t\t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \t\t\tgoto out;\n \t\t}\n \n \t\tattr = (u8 *)ctx-\u003eattr + le16_to_cpu(ctx-\u003eattr-\u003edata.resident.value_offset);\n \t\tmemcpy(buf, (u8 *)attr + pos, count);\n \t\tntfs_attr_put_search_ctx(ctx);\n-\t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \t\treturn count;\n \t}\n-\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\tif (ntfs_inode_is_named_stream(ni)) {\n+\t\tatomic_inc(\u0026ni-\u003estream_open_count);\n+\t\tclaimed = true;\n+\t}\n+\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \n \tindex = pos \u003e\u003e PAGE_SHIFT;\n \tdo {\n \t\t/* Update @index and get the next folio. */\n \t\tfolio = read_mapping_folio(mapping, index, NULL);\n-\t\tif (IS_ERR(folio))\n+\t\tif (IS_ERR(folio)) {\n+\t\t\terr = PTR_ERR(folio);\n \t\t\tbreak;\n+\t\t}\n \n \t\toffset = offset_in_folio(folio, pos);\n \t\tattr_len = min_t(size_t, (size_t)count, folio_size(folio) - offset);\n@@ -3684,6 +3776,8 @@ s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf)\n \t\tindex++;\n \t} while (count);\n out:\n+\tif (claimed)\n+\t\tntfs_stream_put(vi);\n \treturn err ? (s64)err : total;\n }\n \n@@ -3711,8 +3805,8 @@ static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,\n \t\t}\n \n \t\tntfs_attr_reinit_search_ctx(ctx);\n-\t\tret = ntfs_attr_lookup(ni-\u003etype,\n-\t\t\t\t ni-\u003ename, ni-\u003ename_len, CASE_SENSITIVE,\n+\t\tret = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len,\n+\t\t\t\t CASE_SENSITIVE,\n \t\t\t\t 0, NULL, 0, ctx);\n \t\tif (ret) {\n \t\t\tntfs_error(sb, \"Failed to look up attr %#x\", ni-\u003etype);\n@@ -3726,6 +3820,13 @@ static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,\n \t\treturn 0;\n \t}\n \n+\t/* Named streams initialize gaps after dropping the MFT record lock. */\n+\tif (ntfs_inode_is_named_stream(ni)) {\n+\t\tif (i_size_read(vi) \u003c ni-\u003edata_size)\n+\t\t\ti_size_write(vi, ni-\u003edata_size);\n+\t\treturn 0;\n+\t}\n+\n \tif (pos + count \u003e ni-\u003einitialized_size) {\n \t\tctx-\u003eattr-\u003edata.non_resident.initialized_size = cpu_to_le64(pos + count);\n \t\tmark_mft_record_dirty(ctx-\u003entfs_ino);\n@@ -3780,7 +3881,6 @@ static s64 __ntfs_inode_resident_attr_pwrite(struct inode *vi,\n \n static s64 __ntfs_inode_non_resident_attr_pwrite(struct inode *vi,\n \t\t\t\t\t\t s64 pos, s64 count, u8 *buf,\n-\t\t\t\t\t\t struct ntfs_attr_search_ctx *ctx,\n \t\t\t\t\t\t bool sync)\n {\n \tstruct ntfs_inode *ni = NTFS_I(vi);\n@@ -3919,37 +4019,79 @@ static s64 __ntfs_inode_non_resident_attr_pwrite(struct inode *vi,\n s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool sync)\n {\n \tstruct ntfs_inode *ni = NTFS_I(vi);\n+\tstruct ntfs_inode *base_ni = ni-\u003eext.base_ntfs_ino;\n+\tstruct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?\n+\t\t\tbase_ni : ni;\n \tstruct ntfs_attr_search_ctx *ctx;\n \ts64 ret;\n \n \tWARN_ON(!NInoAttr(ni));\n \n-\tctx = ntfs_attr_get_search_ctx(ni-\u003eext.base_ntfs_ino, NULL);\n+\tif (ntfs_inode_is_named_stream(ni)) {\n+\t\tif (NInoEncrypted(ni))\n+\t\t\treturn -EACCES;\n+\t\tif (NInoCompressed(ni))\n+\t\t\treturn -EOPNOTSUPP;\n+\t}\n+\n+\tmutex_lock(\u0026mrec_ni-\u003emrec_lock);\n+\tif (ntfs_inode_is_named_stream(ni)) {\n+\t\tret = ntfs_stream_inode_validate(vi);\n+\t\tif (ret)\n+\t\t\tgoto out_unlock;\n+\t}\n+\n+\tctx = ntfs_attr_get_search_ctx(base_ni, NULL);\n \tif (!ctx) {\n \t\tntfs_error(vi-\u003ei_sb, \"Failed to get attr search ctx\");\n-\t\treturn -ENOMEM;\n+\t\tret = -ENOMEM;\n+\t\tgoto out_unlock;\n \t}\n \n-\tret = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len, CASE_SENSITIVE,\n+\tret = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len,\n+\t\t\t CASE_SENSITIVE,\n \t\t\t 0, NULL, 0, ctx);\n \tif (ret) {\n \t\tntfs_attr_put_search_ctx(ctx);\n \t\tntfs_error(vi-\u003ei_sb, \"Failed to look up attr %#x\", ni-\u003etype);\n-\t\treturn ret;\n+\t\tgoto out_unlock;\n \t}\n \n-\tmutex_lock(\u0026ni-\u003emrec_lock);\n \tret = ntfs_enlarge_attribute(vi, pos, count, ctx);\n-\tmutex_unlock(\u0026ni-\u003emrec_lock);\n \tif (ret)\n-\t\tgoto out;\n+\t\tgoto out_ctx;\n \n-\tif (NInoNonResident(ni))\n-\t\tret = __ntfs_inode_non_resident_attr_pwrite(vi, pos, count, buf, ctx, sync);\n-\telse\n+\tif (!NInoNonResident(ni)) {\n \t\tret = __ntfs_inode_resident_attr_pwrite(vi, pos, count, buf, ctx);\n-out:\n+\t\tgoto out_ctx;\n+\t}\n+\n+\tif (ntfs_inode_is_named_stream(ni))\n+\t\tatomic_inc(\u0026ni-\u003estream_open_count);\n+\tntfs_attr_put_search_ctx(ctx);\n+\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n+\t/* Attribute writes bypass iomap's delayed-allocation preparation. */\n+\tif (ntfs_inode_is_named_stream(ni)) {\n+\t\t/* A failed write must not expose any newly initialized data. */\n+\t\tret = ntfs_extend_initialized_size(vi, pos + count, pos + count);\n+\t\tif (!ret)\n+\t\t\tret = ntfs_attr_fallocate(ni, pos, count, true);\n+\t}\n+\tif (!ret)\n+\t\tret = __ntfs_inode_non_resident_attr_pwrite(vi, pos, count,\n+\t\t\t\t\t\t\t buf, sync);\n+\tif (ntfs_inode_is_named_stream(ni))\n+\t\tntfs_stream_put(vi);\n+\tif (ret \u003e 0 \u0026\u0026 ntfs_inode_is_named_stream(ni))\n+\t\tntfs_stream_update_base_time(base_ni);\n+\treturn ret;\n+\n+out_ctx:\n \tntfs_attr_put_search_ctx(ctx);\n+out_unlock:\n+\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n+\tif (ret \u003e 0 \u0026\u0026 ntfs_inode_is_named_stream(ni))\n+\t\tntfs_stream_update_base_time(base_ni);\n \treturn ret;\n }\n \ndiff --git a/fs/ntfs/inode.h b/fs/ntfs/inode.h\nindex ff61bd402df03..f4e5562d56a7e 100644\n--- a/fs/ntfs/inode.h\n+++ b/fs/ntfs/inode.h\n@@ -107,6 +107,7 @@ struct ntfs_inode {\n \t__le32 type;\n \t__le16 *name;\n \tu32 name_len;\n+\tatomic_t stream_open_count;\n \tstruct runlist runlist;\n \ts64 data_size;\n \ts64 initialized_size;\n@@ -178,6 +179,7 @@ struct ntfs_inode {\n * NI_BeingCreated\t\tntfs inode is being created.\n * NI_HasEA\t\t\tntfs inode has EA attribute.\n * NI_RunlistDirty\t\trunlist need to be updated.\n+ * NI_StreamUnlinked\t\tNamed stream is unlinked but still open.\n */\n enum {\n \tNI_Dirty,\n@@ -199,6 +201,7 @@ enum {\n \tNI_BeingCreated,\n \tNI_HasEA,\n \tNI_RunlistDirty,\n+\tNI_StreamUnlinked,\n };\n \n /*\n@@ -259,6 +262,7 @@ TAS_NINO_FNS(FileNameDirty)\n NINO_FNS(BeingDeleted)\n NINO_FNS(HasEA)\n NINO_FNS(RunlistDirty)\n+NINO_FNS(StreamUnlinked)\n \n /*\n * The full structure containing a ntfs_inode and a vfs struct inode. Used for\n@@ -286,6 +290,20 @@ static inline struct inode *VFS_I(struct ntfs_inode *ni)\n \treturn \u0026container_of(ni, struct big_ntfs_inode, ntfs_inode)-\u003evfs_inode;\n }\n \n+/* Return true when @ni represents a named $DATA attribute inode. */\n+static inline bool ntfs_inode_is_named_stream(struct ntfs_inode *ni)\n+{\n+\treturn NInoAttr(ni) \u0026\u0026 ni-\u003etype == AT_DATA \u0026\u0026 ni-\u003ename_len;\n+}\n+\n+/* Return the base MFT inode for an attribute inode, or @ni itself. */\n+static inline struct ntfs_inode *ntfs_base_inode(struct ntfs_inode *ni)\n+{\n+\tif (NInoAttr(ni) \u0026\u0026 ni-\u003enr_extents == -1 \u0026\u0026 ni-\u003eext.base_ntfs_ino)\n+\t\treturn ni-\u003eext.base_ntfs_ino;\n+\treturn ni;\n+}\n+\n /*\n * ntfs_attr - ntfs in memory attribute structure\n *\n@@ -304,6 +322,7 @@ struct ntfs_attr {\n };\n \n int ntfs_test_inode(struct inode *vi, void *data);\n+int ntfs_test_inode_rcu(struct inode *vi, void *data);\n struct inode *ntfs_iget(struct super_block *sb, u64 mft_no);\n struct inode *ntfs_attr_iget(struct inode *base_vi, __le32 type,\n \t\t__le16 *name, u32 name_len);\ndiff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c\nindex 754d5d91b6dc6..2940a5587add2 100644\n--- a/fs/ntfs/iomap.c\n+++ b/fs/ntfs/iomap.c\n@@ -64,11 +64,7 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo\n \tint err = 0;\n \tchar *kattr;\n \n-\tif (NInoAttr(ni))\n-\t\tbase_ni = ni-\u003eext.base_ntfs_ino;\n-\telse\n-\t\tbase_ni = ni;\n-\n+\tbase_ni = ntfs_base_inode(ni);\n \tmutex_lock(\u0026base_ni-\u003emrec_lock);\n \n \tctx = ntfs_attr_get_search_ctx(base_ni, NULL);\n@@ -114,7 +110,8 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo\n \tif (ctx)\n \t\tntfs_attr_put_search_ctx(ctx);\n \n-\tif (!err \u0026\u0026 keep_mrec_lock \u0026\u0026 iomap-\u003etype == IOMAP_INLINE) {\n+\tif (!err \u0026\u0026 keep_mrec_lock \u0026\u0026\n+\t iomap-\u003etype == IOMAP_INLINE) {\n \t\tiomap-\u003eprivate = base_ni;\n \t\treturn 0;\n \t}\n@@ -399,6 +396,8 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_\n \t\t\t\t\t\t loff_t length, struct iomap *iomap)\n {\n \tstruct ntfs_inode *ni = NTFS_I(inode);\n+\tstruct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?\n+\t\t\tntfs_base_inode(ni) : ni;\n \tstruct ntfs_volume *vol = ni-\u003evol;\n \tloff_t vcn_ofs, rl_length;\n \tstruct runlist_element *rl, *rlc;\n@@ -417,7 +416,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_\n \t\tup_read(\u0026ni-\u003erunlist.lock);\n \t\terr = ntfs_map_runlist(ni, vcn);\n \t\tif (err) {\n-\t\t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \t\t\treturn -ENOENT;\n \t\t}\n \t\tdown_read(\u0026ni-\u003erunlist.lock);\n@@ -431,7 +430,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_\n \trl = __ntfs_attr_find_vcn_nolock(\u0026ni-\u003erunlist, vcn);\n \tif (IS_ERR(rl)) {\n \t\tup_write(\u0026ni-\u003erunlist.lock);\n-\t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \t\treturn -EIO;\n \t}\n \tlcn = ntfs_rl_vcn_to_lcn(rl, vcn);\n@@ -452,7 +451,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_\n \t */\n \tif (lcn == LCN_RL_NOT_MAPPED) {\n \t\tup_write(\u0026ni-\u003erunlist.lock);\n-\t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \t\treturn map_err == -ENOMEM ? -ENOMEM : -EIO;\n \t}\n \n@@ -462,7 +461,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_\n \t\t\t\t\"runlist(vcn : %lld, length : %lld) is corrupted\\n\",\n \t\t\t\trl-\u003evcn, rl-\u003elength);\n \t\tup_write(\u0026ni-\u003erunlist.lock);\n-\t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \t\treturn -EIO;\n \t}\n \n@@ -476,7 +475,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_\n \t\t\tif (max_clu_count \u003c 0) {\n \t\t\t\terr = max_clu_count;\n \t\t\t\tup_write(\u0026ni-\u003erunlist.lock);\n-\t\t\t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\t\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \t\t\t\treturn err;\n \t\t\t}\n \t\t}\n@@ -491,7 +490,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_\n \t\t\t\t\tGFP_NOFS);\n \t\t\tif (!rlc) {\n \t\t\t\tup_write(\u0026ni-\u003erunlist.lock);\n-\t\t\t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\t\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \t\t\t\treturn -ENOMEM;\n \t\t\t}\n \n@@ -508,7 +507,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_\n \t\t\tif (IS_ERR(rl)) {\n \t\t\t\tntfs_error(vol-\u003esb, \"Failed to merge runlists\");\n \t\t\t\tup_write(\u0026ni-\u003erunlist.lock);\n-\t\t\t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\t\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \t\t\t\tkvfree(rlc);\n \t\t\t\treturn PTR_ERR(rl);\n \t\t\t}\n@@ -518,7 +517,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_\n \t\t\tni-\u003ei_dealloc_clusters += max_clu_count;\n \t\t}\n \t\tup_write(\u0026ni-\u003erunlist.lock);\n-\t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \n \t\tif (lcn \u003c LCN_DELALLOC)\n \t\t\tntfs_hold_dirty_clusters(vol, max_clu_count);\n@@ -570,7 +569,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_\n \t\t}\n \t} else {\n \t\tup_write(\u0026ni-\u003erunlist.lock);\n-\t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \n \t\tiomap-\u003etype = IOMAP_MAPPED;\n \t\tiomap-\u003eaddr = ntfs_cluster_to_bytes(vol, lcn) + vcn_ofs;\n@@ -595,6 +594,8 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,\n \t\tstruct iomap *iomap, int ntfs_iomap_flags)\n {\n \tstruct ntfs_inode *ni = NTFS_I(inode);\n+\tstruct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?\n+\t\t\tntfs_base_inode(ni) : ni;\n \tstruct ntfs_volume *vol = ni-\u003evol;\n \tloff_t vcn_ofs, rl_length;\n \ts64 vcn, start_lcn, lcn_count;\n@@ -613,7 +614,7 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,\n \t\t\tmax_clu_count, \u0026balloc, update_mp,\n \t\t\tntfs_iomap_flags \u0026 NTFS_IOMAP_FLAGS_WRITEBACK);\n \tup_write(\u0026ni-\u003erunlist.lock);\n-\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \tif (err) {\n \t\tni-\u003ei_dealloc_clusters = 0;\n \t\treturn err;\n@@ -667,8 +668,8 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,\n \n \tif (ntfs_iomap_flags \u0026 NTFS_IOMAP_FLAGS_MKWRITE \u0026\u0026\n \t iomap-\u003eoffset + iomap-\u003elength \u003e ni-\u003einitialized_size) {\n-\t\terr = ntfs_attr_set_initialized_size(ni, iomap-\u003eoffset +\n-\t\t\t\tiomap-\u003elength);\n+\t\terr = ntfs_attr_set_initialized_size(ni,\n+\t\t\t\tiomap-\u003eoffset + iomap-\u003elength);\n \t}\n \n \treturn err;\n@@ -678,13 +679,15 @@ static int ntfs_write_iomap_begin_resident(struct inode *inode, loff_t offset,\n \t\tstruct iomap *iomap)\n {\n \tstruct ntfs_inode *ni = NTFS_I(inode);\n+\tstruct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?\n+\t\t\tntfs_base_inode(ni) : ni;\n \tstruct attr_record *a;\n-\tstruct ntfs_attr_search_ctx *ctx;\n+\tstruct ntfs_attr_search_ctx *ctx = NULL;\n \tu32 attr_len;\n \tint err = 0;\n \tchar *kattr;\n \n-\tctx = ntfs_attr_get_search_ctx(ni, NULL);\n+\tctx = ntfs_attr_get_search_ctx(mrec_ni, NULL);\n \tif (!ctx) {\n \t\terr = -ENOMEM;\n \t\tgoto out;\n@@ -707,13 +710,14 @@ static int ntfs_write_iomap_begin_resident(struct inode *inode, loff_t offset,\n \tiomap-\u003einline_data = kattr;\n \tiomap-\u003eoffset = 0;\n \tiomap-\u003elength = attr_len;\n+\tiomap-\u003eprivate = mrec_ni;\n \n out:\n \tif (ctx)\n \t\tntfs_attr_put_search_ctx(ctx);\n \n \tif (err)\n-\t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n \n \treturn err;\n }\n@@ -722,7 +726,12 @@ static int ntfs_write_iomap_begin_non_resident(struct inode *inode, loff_t offse\n \t\t\t\t\t loff_t length, unsigned int flags,\n \t\t\t\t\t struct iomap *iomap, int ntfs_iomap_flags)\n {\n-\tmutex_lock(\u0026NTFS_I(inode)-\u003emrec_lock);\n+\tstruct ntfs_inode *ni = NTFS_I(inode);\n+\tstruct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?\n+\t\t\tntfs_base_inode(ni) : ni;\n+\n+\tmutex_lock(\u0026mrec_ni-\u003emrec_lock);\n+\n \tif (ntfs_iomap_flags \u0026 NTFS_IOMAP_FLAGS_BEGIN)\n \t\treturn ntfs_write_simple_iomap_begin_non_resident(inode, offset,\n \t\t\t\t\t\t\t\t length, iomap);\n@@ -738,12 +747,15 @@ static int __ntfs_write_iomap_begin(struct inode *inode, loff_t offset,\n \t\t\t\t struct iomap *iomap, int ntfs_iomap_flags)\n {\n \tstruct ntfs_inode *ni = NTFS_I(inode);\n+\tstruct ntfs_inode *mrec_ni;\n \n \tif (NVolShutdown(ni-\u003evol))\n \t\treturn -EIO;\n \n \tif (!NInoNonResident(ni)) {\n-\t\tmutex_lock(\u0026ni-\u003emrec_lock);\n+\t\tmrec_ni = ntfs_inode_is_named_stream(ni) ?\n+\t\t\t\tntfs_base_inode(ni) : ni;\n+\t\tmutex_lock(\u0026mrec_ni-\u003emrec_lock);\n \t\treturn ntfs_write_iomap_begin_resident(inode, offset, iomap);\n \t}\n \treturn ntfs_write_iomap_begin_non_resident(inode, offset, length, flags,\n@@ -762,10 +774,10 @@ static int ntfs_write_iomap_end_resident(struct inode *inode, loff_t pos,\n \t\t\t\t\t loff_t length, ssize_t written,\n \t\t\t\t\t unsigned int flags, struct iomap *iomap)\n {\n-\tstruct ntfs_inode *ni = NTFS_I(inode);\n+\tstruct ntfs_inode *base_ni = iomap-\u003eprivate;\n \n-\tmark_mft_record_dirty(ni);\n-\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\tmark_mft_record_dirty(base_ni);\n+\tmutex_unlock(\u0026base_ni-\u003emrec_lock);\n \treturn written;\n }\n \ndiff --git a/fs/ntfs/named_stream.c b/fs/ntfs/named_stream.c\nnew file mode 100644\nindex 0000000000000..e44dbf09057b3\n--- /dev/null\n+++ b/fs/ntfs/named_stream.c\n@@ -0,0 +1,1821 @@\n+// SPDX-License-Identifier: GPL-2.0-or-later\n+/*\n+ * NTFS named stream handling.\n+ *\n+ * Copyright (c) 2026 LG Electronics Co., Ltd.\n+ */\n+\n+#include \u003clinux/file.h\u003e\n+#include \u003clinux/namei.h\u003e\n+#include \u003clinux/overflow.h\u003e\n+\n+#include \u003cuapi/linux/ntfs.h\u003e\n+\n+#include \"attrib.h\"\n+#include \"dir.h\"\n+#include \"iomap.h\"\n+#include \"mft.h\"\n+#include \"ntfs.h\"\n+#include \"stream.h\"\n+#include \"time.h\"\n+\n+#define NTFS_STREAM_MAX_IO\t(16 * 1024 * 1024)\n+\n+/*\n+ * ntfs_check_stream_name() - Validate a named-stream name\n+ * @name: UTF-16LE stream name\n+ * @name_len: Length of @name in UTF-16 code units\n+ *\n+ * Reject empty, overlong, separator-containing, or malformed UTF-16 names.\n+ *\n+ * Return: 0 if valid, or -EINVAL otherwise.\n+ */\n+int ntfs_check_stream_name(const __le16 *name, unsigned int name_len)\n+{\n+\tunsigned int i;\n+\n+\tif (!name_len || name_len \u003e NTFS_MAX_NAME_LEN)\n+\t\treturn -EINVAL;\n+\n+\tfor (i = 0; i \u003c name_len; i++) {\n+\t\tu16 c = le16_to_cpu(name[i]);\n+\n+\t\tif (c == 0 || c == '/' || c == '\\\\' || c == ':')\n+\t\t\treturn -EINVAL;\n+\t\tif (c \u003e= 0xd800 \u0026\u0026 c \u003c= 0xdbff) {\n+\t\t\tif (++i \u003e= name_len)\n+\t\t\t\treturn -EINVAL;\n+\t\t\tc = le16_to_cpu(name[i]);\n+\t\t\tif (c \u003c 0xdc00 || c \u003e 0xdfff)\n+\t\t\t\treturn -EINVAL;\n+\t\t} else if (c \u003e= 0xdc00 \u0026\u0026 c \u003c= 0xdfff) {\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/*\n+ * ntfs_stream_inode_refresh() - Refresh base-derived stream inode metadata\n+ * @vi: inode representing a named stream\n+ *\n+ * Synchronize ownership, mode, flags, timestamps, and generation with the\n+ * base inode. Stream size and allocation remain specific to the stream.\n+ */\n+void ntfs_stream_inode_refresh(struct inode *vi)\n+{\n+\tstruct ntfs_inode *ni = NTFS_I(vi);\n+\tstruct inode *base_vi;\n+\n+\tif (!ntfs_inode_is_named_stream(ni) || ni-\u003enr_extents != -1)\n+\t\treturn;\n+\n+\tbase_vi = VFS_I(ni-\u003eext.base_ntfs_ino);\n+\tvi-\u003ei_uid = base_vi-\u003ei_uid;\n+\tvi-\u003ei_gid = base_vi-\u003ei_gid;\n+\tvi-\u003ei_mode = (base_vi-\u003ei_mode \u0026 ~S_IFMT) | S_IFREG;\n+\tvi-\u003ei_flags = base_vi-\u003ei_flags;\n+\tinode_set_mtime_to_ts(vi, inode_get_mtime(base_vi));\n+\tinode_set_ctime_to_ts(vi, inode_get_ctime(base_vi));\n+\tinode_set_atime_to_ts(vi, inode_get_atime(base_vi));\n+\tvi-\u003ei_generation = base_vi-\u003ei_generation;\n+}\n+\n+/*\n+ * ntfs_stream_update_base_time() - Update base timestamps after a stream change\n+ * @base_ni: base inode containing the stream attribute\n+ *\n+ * Update the base inode's mtime and ctime and mark it dirty. The helper takes\n+ * the base inode lock.\n+ */\n+void ntfs_stream_update_base_time(struct ntfs_inode *base_ni)\n+{\n+\tstruct inode *base_vi = VFS_I(base_ni);\n+\n+\tinode_lock_nested(base_vi, I_MUTEX_PARENT);\n+\tinode_set_mtime_to_ts(base_vi, inode_set_ctime_current(base_vi));\n+\tmark_inode_dirty(base_vi);\n+\tinode_unlock(base_vi);\n+}\n+\n+/*\n+ * ntfs_stream_inode_validate() - Validate a named-stream inode\n+ * @vi: stream inode to validate\n+ *\n+ * Verify that @vi still maps to its named DATA attribute and refresh its\n+ * base-derived metadata. The caller must hold the base inode's MFT-record\n+ * lock.\n+ *\n+ * Return: 0 if valid, or a negative errno.\n+ */\n+int ntfs_stream_inode_validate(struct inode *vi)\n+{\n+\tstruct ntfs_inode *ni = NTFS_I(vi);\n+\tstruct ntfs_inode *base_ni;\n+\tstruct ntfs_attr_search_ctx *ctx;\n+\tint err;\n+\n+\tif (!ntfs_inode_is_named_stream(ni) || ni-\u003enr_extents != -1)\n+\t\treturn -EINVAL;\n+\n+\tbase_ni = ni-\u003eext.base_ntfs_ino;\n+\tlockdep_assert_held(\u0026base_ni-\u003emrec_lock);\n+\tif (NVolShutdown(base_ni-\u003evol))\n+\t\treturn -EIO;\n+\tif (!NInoStreamUnlinked(ni) \u0026\u0026\n+\t (!vi-\u003ei_nlink || !VFS_I(base_ni)-\u003ei_nlink ||\n+\t NInoBeingDeleted(base_ni)))\n+\t\treturn -ESTALE;\n+\n+\tctx = ntfs_attr_get_search_ctx(base_ni, NULL);\n+\tif (!ctx)\n+\t\treturn -ENOMEM;\n+\terr = ntfs_attr_lookup(AT_DATA, ni-\u003ename, ni-\u003ename_len,\n+\t\t\tCASE_SENSITIVE, 0, NULL, 0, ctx);\n+\tntfs_attr_put_search_ctx(ctx);\n+\tif (err)\n+\t\treturn err;\n+\n+\tntfs_stream_inode_refresh(vi);\n+\treturn 0;\n+}\n+\n+/*\n+ * ntfs_stream_unlinked() - Check a cached stream's deferred-unlink state\n+ * @base_ni: base inode containing the stream\n+ * @name: UTF-16LE stream name\n+ * @name_len: Length of @name in UTF-16 code units\n+ *\n+ * The caller must hold the base inode's MFT-record lock.\n+ *\n+ * Return: true if the cached stream inode is unlinked, or false if it is not\n+ * cached or is still linked.\n+ */\n+bool ntfs_stream_unlinked(struct ntfs_inode *base_ni,\n+\t\tconst __le16 *name, u32 name_len)\n+{\n+\tstruct ntfs_attr na = {\n+\t\t.mft_no = base_ni-\u003emft_no,\n+\t\t.type = AT_DATA,\n+\t\t.name = (__le16 *)name,\n+\t\t.name_len = name_len,\n+\t};\n+\tstruct inode *vi;\n+\tbool unlinked;\n+\n+\tlockdep_assert_held(\u0026base_ni-\u003emrec_lock);\n+\trcu_read_lock();\n+\tvi = find_inode_rcu(base_ni-\u003evol-\u003esb, na.mft_no,\n+\t\t\tntfs_test_inode_rcu, \u0026na);\n+\tif (!vi) {\n+\t\trcu_read_unlock();\n+\t\treturn false;\n+\t}\n+\tunlinked = NInoStreamUnlinked(NTFS_I(vi));\n+\trcu_read_unlock();\n+\treturn unlinked;\n+}\n+\n+/*\n+ * ntfs_stream_path_parse() - Split a pathname stream component\n+ * @vol: NTFS volume\n+ * @qname: final pathname component\n+ * @path: receives slices of @qname on success\n+ *\n+ * Parse the ``file:stream`` form when the Windows pathname interface is\n+ * enabled. This does not convert or validate the component names.\n+ *\n+ * Return: 1 if stream syntax was parsed, 0 if streams are disabled or no\n+ * stream separator is present, or -EINVAL for malformed syntax.\n+ */\n+int ntfs_stream_path_parse(struct ntfs_volume *vol,\n+\t\tconst struct qstr *qname, struct ntfs_stream_path *path)\n+{\n+\tconst unsigned char *colon;\n+\n+\tif (!NVolStreamsWindows(vol))\n+\t\treturn 0;\n+\n+\tcolon = memchr(qname-\u003ename, ':', qname-\u003elen);\n+\tif (!colon)\n+\t\treturn 0;\n+\tif (colon == qname-\u003ename || colon + 1 == qname-\u003ename + qname-\u003elen)\n+\t\treturn -EINVAL;\n+\tif (memchr(colon + 1, ':', qname-\u003ename + qname-\u003elen - colon - 1))\n+\t\treturn -EINVAL;\n+\n+\tpath-\u003ebase_name = (const char *)qname-\u003ename;\n+\tpath-\u003ebase_len = colon - qname-\u003ename;\n+\tpath-\u003estream_name = (const char *)colon + 1;\n+\tpath-\u003estream_len = qname-\u003ename + qname-\u003elen - colon - 1;\n+\treturn 1;\n+}\n+\n+/*\n+ * ntfs_stream_path_has_colon() - Check for a pathname stream separator\n+ * @vol: NTFS volume\n+ * @qname: final pathname component\n+ *\n+ * Return: true if the Windows pathname interface is enabled and @qname\n+ * contains a colon, or false otherwise.\n+ */\n+bool ntfs_stream_path_has_colon(struct ntfs_volume *vol,\n+\t\tconst struct qstr *qname)\n+{\n+\treturn NVolStreamsWindows(vol) \u0026\u0026\n+\t\tmemchr(qname-\u003ename, ':', qname-\u003elen);\n+}\n+\n+/*\n+ * ntfs_stream_path_check() - Reject stream syntax for unsupported operations\n+ * @vol: NTFS volume\n+ * @qname: final pathname component\n+ *\n+ * Return: 0 for an ordinary name or when streams are disabled,\n+ * -EOPNOTSUPP for parsed stream syntax, or -EINVAL for malformed syntax.\n+ */\n+int ntfs_stream_path_check(struct ntfs_volume *vol,\n+\t\tconst struct qstr *qname)\n+{\n+\tstruct ntfs_stream_path path;\n+\tint ret;\n+\n+\tret = ntfs_stream_path_parse(vol, qname, \u0026path);\n+\tif (ret \u003c 0)\n+\t\treturn ret;\n+\treturn ret ? -EOPNOTSUPP : 0;\n+}\n+\n+/*\n+ * ntfs_stream_lookup_inode_by_name() - Look up a base inode by name\n+ * @dir_ino: directory containing the base file or directory\n+ * @uname: UTF-16LE base name\n+ * @uname_len: Length of @uname in UTF-16 code units\n+ *\n+ * Resolve the directory entry and verify its MFT reference before returning\n+ * the inode.\n+ *\n+ * Return: Referenced inode on success, or ERR_PTR() on failure.\n+ */\n+struct inode *ntfs_stream_lookup_inode_by_name(struct inode *dir_ino,\n+\t\t__le16 *uname, int uname_len)\n+{\n+\tstruct ntfs_volume *vol = NTFS_SB(dir_ino-\u003ei_sb);\n+\tstruct ntfs_name *name = NULL;\n+\tstruct inode *inode;\n+\tu64 mref;\n+\n+\tmutex_lock(\u0026NTFS_I(dir_ino)-\u003emrec_lock);\n+\tmref = ntfs_lookup_inode_by_name(NTFS_I(dir_ino), uname, uname_len,\n+\t\t\t\u0026name);\n+\tmutex_unlock(\u0026NTFS_I(dir_ino)-\u003emrec_lock);\n+\tkfree(name);\n+\n+\tif (IS_ERR_MREF(mref))\n+\t\treturn ERR_PTR(MREF_ERR(mref));\n+\n+\tinode = ntfs_iget(vol-\u003esb, MREF(mref));\n+\tif (IS_ERR(inode))\n+\t\treturn inode;\n+\tif (MSEQNO(mref) != NTFS_I(inode)-\u003eseq_no \u0026\u0026\n+\t MREF(mref) != FILE_MFT) {\n+\t\tiput(inode);\n+\t\treturn ERR_PTR(-EIO);\n+\t}\n+\treturn inode;\n+}\n+\n+/*\n+ * Recheck that a stream dentry still names the same base inode and DATA\n+ * attribute.\n+ */\n+static int ntfs_stream_d_revalidate(struct inode *dir,\n+\t\tconst struct qstr *qname, struct dentry *dentry,\n+\t\tunsigned int flags)\n+{\n+\tstruct inode *inode = d_inode(dentry);\n+\tstruct ntfs_inode *ni;\n+\tstruct ntfs_stream_path path;\n+\tstruct inode *base_vi;\n+\t__le16 *base_name = NULL, *stream_name = NULL;\n+\tint base_len, stream_len, err, ret = 0;\n+\n+\tif (flags \u0026 LOOKUP_RCU)\n+\t\treturn -ECHILD;\n+\tif (!inode)\n+\t\treturn 0;\n+\tif (!inode-\u003ei_nlink)\n+\t\treturn 0;\n+\tif (NVolShutdown(NTFS_SB(dir-\u003ei_sb)))\n+\t\treturn 0;\n+\n+\tni = NTFS_I(inode);\n+\tif (!ntfs_inode_is_named_stream(ni))\n+\t\treturn 0;\n+\n+\terr = ntfs_stream_path_parse(NTFS_SB(dir-\u003ei_sb), qname, \u0026path);\n+\tif (err \u003c= 0)\n+\t\treturn 0;\n+\tbase_len = ntfs_nlstoucs(NTFS_SB(dir-\u003ei_sb), path.base_name,\n+\t\t\tpath.base_len, \u0026base_name, NTFS_MAX_NAME_LEN);\n+\tif (base_len \u003c 0)\n+\t\tgoto out;\n+\tstream_len = ntfs_nlstoucs(NTFS_SB(dir-\u003ei_sb), path.stream_name,\n+\t\t\tpath.stream_len, \u0026stream_name, NTFS_MAX_NAME_LEN);\n+\tif (stream_len \u003c 0)\n+\t\tgoto out;\n+\tif (ntfs_check_stream_name(stream_name, stream_len))\n+\t\tgoto out;\n+\tif (stream_len != ni-\u003ename_len ||\n+\t !ntfs_names_are_equal(stream_name, stream_len, ni-\u003ename,\n+\t\t\tni-\u003ename_len, IGNORE_CASE, ni-\u003evol-\u003eupcase,\n+\t\t\tni-\u003evol-\u003eupcase_len))\n+\t\tgoto out;\n+\n+\tbase_vi = ntfs_stream_lookup_inode_by_name(dir, base_name, base_len);\n+\tif (IS_ERR(base_vi))\n+\t\tgoto out;\n+\tif (NTFS_I(base_vi) != ni-\u003eext.base_ntfs_ino) {\n+\t\tiput(base_vi);\n+\t\tgoto out;\n+\t}\n+\n+\tmutex_lock(\u0026NTFS_I(base_vi)-\u003emrec_lock);\n+\terr = ntfs_stream_inode_validate(inode);\n+\tmutex_unlock(\u0026NTFS_I(base_vi)-\u003emrec_lock);\n+\tiput(base_vi);\n+\tif (!err)\n+\t\tret = 1;\n+out:\n+\tif (stream_name)\n+\t\tkmem_cache_free(ntfs_name_cache, stream_name);\n+\tif (base_name)\n+\t\tkmem_cache_free(ntfs_name_cache, base_name);\n+\treturn ret;\n+}\n+\n+/* Revalidate stream paths while leaving ordinary NTFS dentries cacheable. */\n+static int ntfs_dentry_revalidate(struct inode *dir,\n+\t\tconst struct qstr *qname, struct dentry *dentry,\n+\t\tunsigned int flags)\n+{\n+\tstruct inode *inode = d_inode(dentry);\n+\tstruct ntfs_volume *vol = NTFS_SB(dir-\u003ei_sb);\n+\n+\tif (ntfs_stream_path_has_colon(vol, qname))\n+\t\treturn ntfs_stream_d_revalidate(dir, qname, dentry, flags);\n+\tif (inode \u0026\u0026 ntfs_inode_is_named_stream(NTFS_I(inode)))\n+\t\treturn 0;\n+\treturn 1;\n+}\n+\n+/* Drop stream-path aliases so future lookups recheck their backing attribute. */\n+static int ntfs_dentry_delete(const struct dentry *dentry)\n+{\n+\tstruct inode *inode = d_inode(dentry);\n+\tstruct ntfs_volume *vol = NTFS_SB(dentry-\u003ed_sb);\n+\n+\tif (ntfs_stream_path_has_colon(vol, \u0026dentry-\u003ed_name) ||\n+\t (inode \u0026\u0026 ntfs_inode_is_named_stream(NTFS_I(inode))))\n+\t\treturn always_delete_dentry(dentry);\n+\treturn 0;\n+}\n+\n+static const struct dentry_operations ntfs_dentry_ops = {\n+\t.d_revalidate\t= ntfs_dentry_revalidate,\n+\t.d_delete\t\t= ntfs_dentry_delete,\n+};\n+\n+/*\n+ * ntfs_set_default_dentry_ops() - Install stream-aware dentry operations\n+ * @sb: superblock on which to install the operations\n+ *\n+ * Ensure pathname stream dentries are revalidated and not kept as stale\n+ * aliases.\n+ */\n+void ntfs_set_default_dentry_ops(struct super_block *sb)\n+{\n+\tset_default_d_op(sb, \u0026ntfs_dentry_ops);\n+}\n+\n+/*\n+ * ntfs_lookup_stream() - Look up a named stream by pathname\n+ * @dir_ino: directory containing the base file or directory\n+ * @dent: dentry for the pathname component\n+ * @path: parsed base and stream name slices\n+ *\n+ * Find the existing base inode and its named DATA attribute, then splice the\n+ * stream inode into @dent.\n+ *\n+ * Return: NULL if @dent was instantiated, an alternate dentry if a\n+ * disconnected alias was spliced, or ERR_PTR() on failure.\n+ */\n+struct dentry *ntfs_lookup_stream(struct inode *dir_ino,\n+\t\tstruct dentry *dent, const struct ntfs_stream_path *path)\n+{\n+\tstruct ntfs_volume *vol = NTFS_SB(dir_ino-\u003ei_sb);\n+\tstruct inode *base_vi, *stream_vi;\n+\t__le16 *base_uname = NULL, *stream_uname = NULL;\n+\tint base_len, stream_len, err;\n+\n+\tif (NVolShutdown(vol))\n+\t\treturn ERR_PTR(-EIO);\n+\n+\tbase_len = ntfs_nlstoucs(vol, path-\u003ebase_name, path-\u003ebase_len,\n+\t\t\t\u0026base_uname, NTFS_MAX_NAME_LEN);\n+\tif (base_len \u003c 0)\n+\t\treturn ERR_PTR(base_len);\n+\n+\tstream_len = ntfs_nlstoucs(vol, path-\u003estream_name, path-\u003estream_len,\n+\t\t\t\u0026stream_uname, NTFS_MAX_NAME_LEN);\n+\tif (stream_len \u003c 0) {\n+\t\tkmem_cache_free(ntfs_name_cache, base_uname);\n+\t\treturn ERR_PTR(stream_len);\n+\t}\n+\terr = ntfs_check_stream_name(stream_uname, stream_len);\n+\tif (err) {\n+\t\tkmem_cache_free(ntfs_name_cache, base_uname);\n+\t\tkmem_cache_free(ntfs_name_cache, stream_uname);\n+\t\treturn ERR_PTR(err);\n+\t}\n+\n+\tbase_vi = ntfs_stream_lookup_inode_by_name(dir_ino, base_uname,\n+\t\t\tbase_len);\n+\tkmem_cache_free(ntfs_name_cache, base_uname);\n+\tif (IS_ERR(base_vi)) {\n+\t\terr = PTR_ERR(base_vi);\n+\t\tkmem_cache_free(ntfs_name_cache, stream_uname);\n+\t\tif (err == -ENOENT)\n+\t\t\treturn d_splice_alias(NULL, dent);\n+\t\treturn ERR_PTR(err);\n+\t}\n+\n+\tif (!S_ISREG(base_vi-\u003ei_mode) \u0026\u0026 !S_ISDIR(base_vi-\u003ei_mode)) {\n+\t\tiput(base_vi);\n+\t\tkmem_cache_free(ntfs_name_cache, stream_uname);\n+\t\treturn ERR_PTR(-ENOTDIR);\n+\t}\n+\n+\tmutex_lock(\u0026NTFS_I(base_vi)-\u003emrec_lock);\n+\tstream_vi = ntfs_attr_iget(base_vi, AT_DATA, stream_uname, stream_len);\n+\tif (!IS_ERR(stream_vi)) {\n+\t\tif (NInoStreamUnlinked(NTFS_I(stream_vi)))\n+\t\t\terr = -ENOENT;\n+\t\telse\n+\t\t\terr = ntfs_stream_inode_validate(stream_vi);\n+\t}\n+\tmutex_unlock(\u0026NTFS_I(base_vi)-\u003emrec_lock);\n+\tif (!IS_ERR(stream_vi) \u0026\u0026 err) {\n+\t\tiput(stream_vi);\n+\t\tstream_vi = ERR_PTR(err);\n+\t}\n+\tiput(base_vi);\n+\tkmem_cache_free(ntfs_name_cache, stream_uname);\n+\n+\tif (IS_ERR(stream_vi)) {\n+\t\terr = PTR_ERR(stream_vi);\n+\t\tif (err == -ENOENT || err == -ESTALE)\n+\t\t\treturn d_splice_alias(NULL, dent);\n+\t\treturn ERR_PTR(err);\n+\t}\n+\n+\treturn d_splice_alias(stream_vi, dent);\n+}\n+\n+/*\n+ * ntfs_stream_path_names() - Convert and validate pathname stream names\n+ * @vol: NTFS volume\n+ * @qname: final pathname component\n+ * @base_name: receives the allocated UTF-16LE base name\n+ * @base_len: receives the base name length in UTF-16 code units\n+ * @stream_name: receives the allocated UTF-16LE stream name\n+ * @stream_len: receives the stream name length in UTF-16 code units\n+ *\n+ * Return: 1 if stream names were produced, 0 if @qname has no stream syntax,\n+ * or a negative errno. The caller owns both name buffers on success.\n+ */\n+int ntfs_stream_path_names(struct ntfs_volume *vol,\n+\t\tconst struct qstr *qname, __le16 **base_name, int *base_len,\n+\t\t__le16 **stream_name, int *stream_len)\n+{\n+\tstruct ntfs_stream_path path;\n+\tint err;\n+\n+\t*base_name = NULL;\n+\t*stream_name = NULL;\n+\terr = ntfs_stream_path_parse(vol, qname, \u0026path);\n+\tif (err \u003c= 0)\n+\t\treturn err;\n+\n+\t*base_len = ntfs_nlstoucs(vol, path.base_name, path.base_len,\n+\t\t\tbase_name, NTFS_MAX_NAME_LEN);\n+\tif (*base_len \u003c 0)\n+\t\treturn *base_len;\n+\n+\t*stream_len = ntfs_nlstoucs(vol, path.stream_name, path.stream_len,\n+\t\t\tstream_name, NTFS_MAX_NAME_LEN);\n+\tif (*stream_len \u003c 0) {\n+\t\tkmem_cache_free(ntfs_name_cache, *base_name);\n+\t\t*base_name = NULL;\n+\t\treturn *stream_len;\n+\t}\n+\n+\terr = ntfs_check_bad_windows_name(vol, *base_name, *base_len);\n+\tif (err)\n+\t\tgoto out_free;\n+\terr = ntfs_check_stream_name(*stream_name, *stream_len);\n+\tif (err)\n+\t\tgoto out_free;\n+\terr = ntfs_check_bad_windows_name(vol, *stream_name, *stream_len);\n+\tif (err)\n+\t\tgoto out_free;\n+\treturn 1;\n+\n+out_free:\n+\tkmem_cache_free(ntfs_name_cache, *stream_name);\n+\tkmem_cache_free(ntfs_name_cache, *base_name);\n+\t*stream_name = NULL;\n+\t*base_name = NULL;\n+\treturn err;\n+}\n+\n+/*\n+ * ntfs_create_named_stream() - Create a named DATA stream\n+ * @idmap: mount idmap used for permission checks\n+ * @dir: directory containing the base object\n+ * @base_name: UTF-16LE base name\n+ * @base_len: length of @base_name in UTF-16 code units\n+ * @stream_name: UTF-16LE stream name\n+ * @stream_len: length of @stream_name in UTF-16 code units\n+ *\n+ * Create the stream on an existing regular file or directory.\n+ *\n+ * Return: Referenced stream inode on success, or ERR_PTR() on failure.\n+ */\n+struct inode *ntfs_create_named_stream(struct mnt_idmap *idmap,\n+\t\tstruct inode *dir, __le16 *base_name, int base_len,\n+\t\t__le16 *stream_name, int stream_len)\n+{\n+\tstruct ntfs_inode *base_ni;\n+\tstruct inode *base_vi, *stream_vi;\n+\tstruct inode *rollback_vi = NULL;\n+\tstruct ntfs_attr_search_ctx *ctx;\n+\tbool stream_created = false;\n+\tint err, rollback_err;\n+\n+\tbase_vi = ntfs_stream_lookup_inode_by_name(dir, base_name, base_len);\n+\tif (IS_ERR(base_vi))\n+\t\treturn base_vi;\n+\tif (!S_ISREG(base_vi-\u003ei_mode) \u0026\u0026 !S_ISDIR(base_vi-\u003ei_mode)) {\n+\t\tiput(base_vi);\n+\t\treturn ERR_PTR(-ENOTDIR);\n+\t}\n+\terr = inode_permission(idmap, base_vi, MAY_OPEN | MAY_WRITE);\n+\tif (err)\n+\t\tgoto out_iput;\n+\tif (IS_APPEND(base_vi) || IS_IMMUTABLE(base_vi)) {\n+\t\terr = -EPERM;\n+\t\tgoto out_iput;\n+\t}\n+\tif (!(NTFS_SB(base_vi-\u003ei_sb)-\u003evol_flags \u0026 VOLUME_IS_DIRTY)) {\n+\t\terr = ntfs_set_volume_flags(NTFS_SB(base_vi-\u003ei_sb),\n+\t\t\t\tVOLUME_IS_DIRTY);\n+\t\tif (err)\n+\t\t\tgoto out_iput;\n+\t}\n+\n+\tbase_ni = NTFS_I(base_vi);\n+\tmutex_lock(\u0026base_ni-\u003emrec_lock);\n+\tif (NVolShutdown(base_ni-\u003evol)) {\n+\t\terr = -EIO;\n+\t\tgoto out_unlock;\n+\t}\n+\tif (NInoBeingDeleted(base_ni) || !base_vi-\u003ei_nlink) {\n+\t\terr = -ENOENT;\n+\t\tgoto out_unlock;\n+\t}\n+\tif (IS_APPEND(base_vi) || IS_IMMUTABLE(base_vi)) {\n+\t\terr = -EPERM;\n+\t\tgoto out_unlock;\n+\t}\n+\tctx = ntfs_attr_get_search_ctx(base_ni, NULL);\n+\tif (!ctx) {\n+\t\terr = -ENOMEM;\n+\t\tgoto out_unlock;\n+\t}\n+\n+\terr = ntfs_attr_lookup(AT_DATA, stream_name, stream_len,\n+\t\t\tIGNORE_CASE, 0, NULL, 0, ctx);\n+\tif (!err) {\n+\t\tif (ntfs_stream_unlinked(base_ni, stream_name, stream_len))\n+\t\t\terr = -EBUSY;\n+\t\telse\n+\t\t\terr = -EEXIST;\n+\t} else if (err == -ENOENT) {\n+\t\tif (NVolShutdown(base_ni-\u003evol)) {\n+\t\t\terr = -EIO;\n+\t\t\tgoto out_put_ctx;\n+\t\t}\n+\t\terr = ntfs_attr_add(base_ni, AT_DATA, stream_name, stream_len,\n+\t\t\t\tNULL, 0);\n+\t\tif (!err) {\n+\t\t\tstream_created = true;\n+\t\t\tmark_mft_record_dirty(base_ni);\n+\t\t}\n+\t}\n+\tntfs_attr_put_search_ctx(ctx);\n+\tif (err)\n+\t\tgoto out_unlock;\n+\n+\tstream_vi = ntfs_attr_iget(base_vi, AT_DATA, stream_name, stream_len);\n+\tif (!IS_ERR(stream_vi))\n+\t\terr = ntfs_stream_inode_validate(stream_vi);\n+\telse\n+\t\terr = PTR_ERR(stream_vi);\n+\tif (err) {\n+\t\tif (stream_created) {\n+\t\t\trollback_err = ntfs_attr_remove(base_ni, AT_DATA,\n+\t\t\t\t\tstream_name, stream_len, \u0026rollback_vi);\n+\t\t\tif (rollback_err) {\n+\t\t\t\tntfs_error(base_ni-\u003evol-\u003esb,\n+\t\t\t\t\t\"Failed to roll back named stream creation.\\n\");\n+\t\t\t\tif (rollback_err == -ENOMEM ||\n+\t\t\t\t rollback_err == -EINTR ||\n+\t\t\t\t rollback_err == -ERESTARTSYS) {\n+\t\t\t\t\terr = rollback_err;\n+\t\t\t\t} else {\n+\t\t\t\t\tNVolSetErrors(base_ni-\u003evol);\n+\t\t\t\t\tNVolSetShutdown(base_ni-\u003evol);\n+\t\t\t\t\terr = -EIO;\n+\t\t\t\t}\n+\t\t\t}\n+\t\t}\n+\t\tmutex_unlock(\u0026base_ni-\u003emrec_lock);\n+\t\tif (stream_created)\n+\t\t\tntfs_stream_update_base_time(base_ni);\n+\t\tif (!IS_ERR(stream_vi))\n+\t\t\tiput(stream_vi);\n+\t\tif (rollback_vi)\n+\t\t\tiput(rollback_vi);\n+\t\tiput(base_vi);\n+\t\treturn ERR_PTR(err);\n+\t}\n+\tmutex_unlock(\u0026base_ni-\u003emrec_lock);\n+\tif (stream_created)\n+\t\tntfs_stream_update_base_time(base_ni);\n+\tiput(base_vi);\n+\treturn stream_vi;\n+\n+out_put_ctx:\n+\tntfs_attr_put_search_ctx(ctx);\n+out_unlock:\n+\tmutex_unlock(\u0026base_ni-\u003emrec_lock);\n+out_iput:\n+\tiput(base_vi);\n+\treturn ERR_PTR(err);\n+}\n+\n+/*\n+ * ntfs_unlink_named_stream() - Remove a pathname named stream\n+ * @dir: directory containing the base object\n+ * @dentry: dentry for the named stream\n+ *\n+ * Return: 0 on success, or a negative errno.\n+ */\n+int ntfs_unlink_named_stream(struct inode *dir, struct dentry *dentry)\n+{\n+\tstruct ntfs_volume *vol = NTFS_SB(dir-\u003ei_sb);\n+\tstruct inode *base_vi;\n+\t__le16 *base_name = NULL, *stream_name = NULL;\n+\tint base_len, stream_len, path_result, err;\n+\n+\tpath_result = ntfs_stream_path_names(vol, \u0026dentry-\u003ed_name, \u0026base_name,\n+\t\t\t\u0026base_len, \u0026stream_name, \u0026stream_len);\n+\tif (path_result \u003c 0)\n+\t\treturn path_result;\n+\tif (!path_result)\n+\t\treturn -EINVAL;\n+\n+\tbase_vi = ntfs_stream_lookup_inode_by_name(dir, base_name, base_len);\n+\tif (IS_ERR(base_vi)) {\n+\t\terr = PTR_ERR(base_vi);\n+\t\tgoto out_free;\n+\t}\n+\tif (!S_ISREG(base_vi-\u003ei_mode) \u0026\u0026 !S_ISDIR(base_vi-\u003ei_mode)) {\n+\t\terr = -ENOTDIR;\n+\t\tgoto out_iput;\n+\t}\n+\tif (!ntfs_inode_is_named_stream(NTFS_I(dentry-\u003ed_inode))) {\n+\t\terr = -ESTALE;\n+\t\tgoto out_iput;\n+\t}\n+\tif (NTFS_I(dentry-\u003ed_inode)-\u003eext.base_ntfs_ino != NTFS_I(base_vi)) {\n+\t\terr = -ESTALE;\n+\t\tgoto out_iput;\n+\t}\n+\n+\t/*\n+\t * VFS authorizes pathname removal via the parent directory, not base\n+\t * file write access. The shared helper enforces NTFS delete restrictions.\n+\t */\n+\terr = ntfs_remove_named_stream(NTFS_I(base_vi), stream_name,\n+\t\t\tstream_len, dentry-\u003ed_inode);\n+\n+out_iput:\n+\tiput(base_vi);\n+out_free:\n+\tkmem_cache_free(ntfs_name_cache, stream_name);\n+\tkmem_cache_free(ntfs_name_cache, base_name);\n+\treturn err;\n+}\n+\n+/* Obtain a dentry for the base inode behind a stream inode. */\n+static struct dentry *ntfs_stream_base_dentry(struct inode *inode)\n+{\n+\tstruct inode *base_vi =\n+\t\tVFS_I(NTFS_I(inode)-\u003eext.base_ntfs_ino);\n+\tstruct dentry *dentry;\n+\n+\tdentry = d_find_alias(base_vi);\n+\tif (dentry)\n+\t\treturn dentry;\n+\tif (!igrab(base_vi))\n+\t\treturn ERR_PTR(-ESTALE);\n+\treturn d_obtain_alias(base_vi);\n+}\n+\n+/*\n+ * ntfs_stream_validate_for_mutation() - Validate a stream before mutation\n+ * @inode: stream inode to validate\n+ * @nowait: do not wait for the base MFT-record lock\n+ *\n+ * Return: 0 if the backing DATA attribute is valid, -EAGAIN if a nonblocking\n+ * lock attempt fails, or another negative errno.\n+ */\n+int ntfs_stream_validate_for_mutation(struct inode *inode, bool nowait)\n+{\n+\tstruct ntfs_inode *base_ni = NTFS_I(inode)-\u003eext.base_ntfs_ino;\n+\tint err;\n+\n+\tif (nowait) {\n+\t\tif (!mutex_trylock(\u0026base_ni-\u003emrec_lock))\n+\t\t\treturn -EAGAIN;\n+\t} else {\n+\t\tmutex_lock(\u0026base_ni-\u003emrec_lock);\n+\t}\n+\terr = ntfs_stream_inode_validate(inode);\n+\tmutex_unlock(\u0026base_ni-\u003emrec_lock);\n+\treturn err;\n+}\n+\n+/*\n+ * Hold a temporary stream reference so unlink cannot remove its attribute\n+ * while an operation is using it.\n+ */\n+static int ntfs_stream_claim(struct inode *inode)\n+{\n+\tstruct ntfs_inode *ni = NTFS_I(inode);\n+\tstruct ntfs_inode *base_ni = ni-\u003eext.base_ntfs_ino;\n+\tint err;\n+\n+\tmutex_lock(\u0026base_ni-\u003emrec_lock);\n+\tif (NInoStreamUnlinked(ni))\n+\t\terr = -ESTALE;\n+\telse\n+\t\terr = ntfs_stream_inode_validate(inode);\n+\tif (!err) {\n+\t\tif (atomic_read(\u0026ni-\u003estream_open_count) \u003c 0)\n+\t\t\terr = -ESTALE;\n+\t\telse\n+\t\t\tatomic_inc(\u0026ni-\u003estream_open_count);\n+\t}\n+\tmutex_unlock(\u0026base_ni-\u003emrec_lock);\n+\treturn err;\n+}\n+\n+/* Stream permission checks use the base inode's permissions. */\n+static int ntfs_stream_permission(struct mnt_idmap *idmap,\n+\t\tstruct inode *inode, int mask)\n+{\n+\treturn inode_permission(idmap,\n+\t\t\tVFS_I(NTFS_I(inode)-\u003eext.base_ntfs_ino), mask);\n+}\n+\n+/* Report base metadata with the stream's own size and allocation. */\n+static int ntfs_stream_getattr(struct mnt_idmap *idmap,\n+\t\tconst struct path *path, struct kstat *stat,\n+\t\tunsigned int request_mask, unsigned int query_flags)\n+{\n+\tstruct inode *inode = d_backing_inode(path-\u003edentry);\n+\tstruct ntfs_inode *ni = NTFS_I(inode);\n+\tstruct inode *base_vi = VFS_I(ni-\u003eext.base_ntfs_ino);\n+\n+\tgeneric_fillattr(idmap, request_mask, base_vi, stat);\n+\tstat-\u003esize = i_size_read(inode);\n+\tstat-\u003eblocks = (((u64)ni-\u003ei_dealloc_clusters \u003c\u003c\n+\t\t\tNTFS_SB(inode-\u003ei_sb)-\u003ecluster_size_bits) \u003e\u003e 9) +\n+\t\t\tinode-\u003ei_blocks;\n+\tstat-\u003eblksize = NTFS_SB(inode-\u003ei_sb)-\u003ecluster_size;\n+\tstat-\u003eresult_mask |= STATX_BTIME;\n+\tstat-\u003ebtime = NTFS_I(base_vi)-\u003ei_crtime;\n+\n+\tif (NInoCompressed(ni))\n+\t\tstat-\u003eattributes |= STATX_ATTR_COMPRESSED;\n+\tif (NInoEncrypted(ni))\n+\t\tstat-\u003eattributes |= STATX_ATTR_ENCRYPTED;\n+\tif (base_vi-\u003ei_flags \u0026 S_IMMUTABLE)\n+\t\tstat-\u003eattributes |= STATX_ATTR_IMMUTABLE;\n+\tif (base_vi-\u003ei_flags \u0026 S_APPEND)\n+\t\tstat-\u003eattributes |= STATX_ATTR_APPEND;\n+\tstat-\u003eattributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED |\n+\t\t\tSTATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND;\n+\tstat-\u003emode = (stat-\u003emode \u0026 ~S_IFMT) | S_IFREG;\n+\n+\tif (request_mask \u0026 STATX_DIOALIGN) {\n+\t\tunsigned int align =\n+\t\t\tbdev_logical_block_size(inode-\u003ei_sb-\u003es_bdev);\n+\n+\t\tstat-\u003eresult_mask |= STATX_DIOALIGN;\n+\t\tif (!NInoCompressed(ni) \u0026\u0026 !NInoEncrypted(ni)) {\n+\t\t\tstat-\u003edio_mem_align = align;\n+\t\t\tstat-\u003edio_offset_align = align;\n+\t\t}\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/*\n+ * Apply size changes to the stream and route shared inode metadata changes to\n+ * its base inode.\n+ */\n+static int ntfs_stream_setattr_common(struct mnt_idmap *idmap,\n+\t\tstruct inode *inode, struct iattr *attr)\n+{\n+\tstruct ntfs_inode *ni = NTFS_I(inode);\n+\tstruct inode *base_vi = VFS_I(ni-\u003eext.base_ntfs_ino);\n+\tstruct dentry *base_dentry = NULL;\n+\tstruct iattr base_attr = *attr;\n+\tint err;\n+\n+\tbase_attr.ia_valid \u0026= ~ATTR_FILE;\n+\tbase_attr.ia_file = NULL;\n+\tif (base_attr.ia_valid \u0026 (ATTR_KILL_SUID | ATTR_KILL_SGID))\n+\t\tbase_attr.ia_valid \u0026= ~ATTR_MODE;\n+\telse if (base_attr.ia_valid \u0026 ATTR_MODE)\n+\t\tbase_attr.ia_mode = (base_attr.ia_mode \u0026 ~S_IFMT) |\n+\t\t\t\t(base_vi-\u003ei_mode \u0026 S_IFMT);\n+\n+\tif (attr-\u003eia_valid \u0026 ATTR_SIZE) {\n+\t\terr = inode_permission(idmap, base_vi, MAY_WRITE);\n+\t\tif (err)\n+\t\t\tgoto out;\n+\t\tif (IS_APPEND(base_vi) || IS_IMMUTABLE(base_vi)) {\n+\t\t\terr = -EPERM;\n+\t\t\tgoto out;\n+\t\t}\n+\t\tif (!(ni-\u003evol-\u003evol_flags \u0026 VOLUME_IS_DIRTY)) {\n+\t\t\terr = ntfs_set_volume_flags(ni-\u003evol, VOLUME_IS_DIRTY);\n+\t\t\tif (err)\n+\t\t\t\tgoto out;\n+\t\t}\n+\t\tbase_attr.ia_valid \u0026= ~ATTR_SIZE;\n+\t}\n+\n+\tif ((attr-\u003eia_valid \u0026 ATTR_SIZE) || base_attr.ia_valid) {\n+\t\tbase_dentry = ntfs_stream_base_dentry(inode);\n+\t\tif (IS_ERR(base_dentry)) {\n+\t\t\terr = PTR_ERR(base_dentry);\n+\t\t\tbase_dentry = NULL;\n+\t\t\tgoto out;\n+\t\t}\n+\t\tinode_lock_nested(base_vi, I_MUTEX_PARENT);\n+\t\terr = ntfs_stream_validate_for_mutation(inode, false);\n+\t\tif (err) {\n+\t\t\tinode_unlock(base_vi);\n+\t\t\tgoto out;\n+\t\t}\n+\t\tif (base_attr.ia_valid)\n+\t\t\terr = notify_change(idmap, base_dentry, \u0026base_attr,\n+\t\t\t\t\tNULL);\n+\t\telse\n+\t\t\terr = 0;\n+\t\tif (!err)\n+\t\t\tntfs_stream_inode_refresh(inode);\n+\t\tinode_unlock(base_vi);\n+\t\tif (err)\n+\t\t\tgoto out;\n+\t}\n+\n+\tif (attr-\u003eia_valid \u0026 ATTR_SIZE)\n+\t\terr = ntfs_setattr_size(inode, attr);\n+\telse\n+\t\terr = 0;\n+out:\n+\tdput(base_dentry);\n+\treturn err;\n+}\n+\n+/* Hold the stream against unlink while applying VFS setattr operations. */\n+static int ntfs_stream_setattr(struct mnt_idmap *idmap,\n+\t\tstruct dentry *dentry, struct iattr *attr)\n+{\n+\tstruct inode *inode = d_inode(dentry);\n+\tstruct ntfs_inode *ni = NTFS_I(inode);\n+\tbool claimed = false;\n+\tint err;\n+\n+\tif (NVolShutdown(ni-\u003evol))\n+\t\treturn -EIO;\n+\n+\tif (!(attr-\u003eia_valid \u0026 ATTR_FILE)) {\n+\t\terr = ntfs_stream_claim(inode);\n+\t\tif (err)\n+\t\t\treturn err;\n+\t\tclaimed = true;\n+\t}\n+\n+\terr = ntfs_stream_setattr_common(idmap, inode, attr);\n+\tif (claimed)\n+\t\tntfs_stream_put(inode);\n+\treturn err;\n+}\n+\n+/*\n+ * Apply stream timestamp updates to the base inode after validating the\n+ * backing attribute under its MFT-record lock. Nonblocking callers get\n+ * -EAGAIN.\n+ */\n+static int ntfs_stream_update_time_common(struct inode *inode,\n+\t\tenum fs_update_time type, unsigned int flags)\n+{\n+\tstruct ntfs_inode *base_ni = NTFS_I(inode)-\u003eext.base_ntfs_ino;\n+\tstruct inode *base_vi = VFS_I(base_ni);\n+\tint err;\n+\n+\tif (NVolShutdown(base_ni-\u003evol))\n+\t\treturn -EIO;\n+\tif (flags \u0026 IOCB_NOWAIT)\n+\t\treturn -EAGAIN;\n+\n+\tmutex_lock(\u0026base_ni-\u003emrec_lock);\n+\tif (NVolShutdown(base_ni-\u003evol)) {\n+\t\terr = -EIO;\n+\t\tgoto out_mrec;\n+\t}\n+\terr = ntfs_stream_inode_validate(inode);\n+\tif (!err) {\n+\t\terr = generic_update_time(base_vi, type, flags);\n+\t\tif (!err)\n+\t\t\tntfs_stream_inode_refresh(inode);\n+\t}\n+out_mrec:\n+\tmutex_unlock(\u0026base_ni-\u003emrec_lock);\n+\treturn err;\n+}\n+\n+static int ntfs_stream_update_time(struct inode *inode,\n+\t\tenum fs_update_time type, unsigned int flags)\n+{\n+\treturn ntfs_stream_update_time_common(inode, type, flags);\n+}\n+\n+static ssize_t ntfs_stream_listxattr(struct dentry *dentry, char *buffer,\n+\t\tsize_t size)\n+{\n+\treturn -EOPNOTSUPP;\n+}\n+\n+#ifdef CONFIG_NTFS_FS_POSIX_ACL\n+static struct posix_acl *ntfs_stream_get_acl(struct mnt_idmap *idmap,\n+\t\tstruct dentry *dentry, int type)\n+{\n+\treturn ERR_PTR(-EOPNOTSUPP);\n+}\n+\n+static int ntfs_stream_set_acl(struct mnt_idmap *idmap,\n+\t\tstruct dentry *dentry, struct posix_acl *acl, int type)\n+{\n+\treturn -EOPNOTSUPP;\n+}\n+#endif\n+\n+/*\n+ * Remove an unlinked stream's DATA attribute after its final active reference\n+ * is released. Base-inode deletion handles the attribute when the base is\n+ * already being removed.\n+ */\n+static int ntfs_stream_finish_remove(struct inode *attr_vi)\n+{\n+\tstruct ntfs_inode *attr_ni = NTFS_I(attr_vi);\n+\tstruct ntfs_inode *ni = attr_ni-\u003eext.base_ntfs_ino;\n+\tint err;\n+\n+\tmutex_lock(\u0026ni-\u003emrec_lock);\n+\tif (!NInoStreamUnlinked(attr_ni) ||\n+\t atomic_cmpxchg(\u0026attr_ni-\u003estream_open_count, 0, -1)) {\n+\t\terr = 0;\n+\t\tgoto out_unlock;\n+\t}\n+\tif (NInoBeingDeleted(ni) || !VFS_I(ni)-\u003ei_nlink) {\n+\t\tremove_inode_hash(attr_vi);\n+\t\terr = 0;\n+\t\tgoto out_unlock;\n+\t}\n+\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\n+\ttruncate_inode_pages(attr_vi-\u003ei_mapping, 0);\n+\n+\tmutex_lock(\u0026ni-\u003emrec_lock);\n+\tif (NVolShutdown(ni-\u003evol)) {\n+\t\terr = -EIO;\n+\t\tgoto out_unlock;\n+\t}\n+\tif (NInoBeingDeleted(ni) || !VFS_I(ni)-\u003ei_nlink) {\n+\t\tremove_inode_hash(attr_vi);\n+\t\terr = 0;\n+\t\tgoto out_unlock;\n+\t}\n+\terr = ntfs_attr_rm(attr_ni);\n+\tif (!err) {\n+\t\tNInoClearDirty(attr_ni);\n+\t\tremove_inode_hash(attr_vi);\n+\t} else {\n+\t\tNInoSetBeingDeleted(attr_ni);\n+\t\tremove_inode_hash(attr_vi);\n+\t}\n+\n+out_unlock:\n+\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\treturn err;\n+}\n+\n+/*\n+ * ntfs_stream_put() - Release a stream operation reference\n+ * @vi: stream inode whose reference is being released\n+ *\n+ * Finish a deferred unlink when this is the last reference to an unlinked\n+ * stream.\n+ *\n+ * Return: 0 on success, or a negative errno if deferred removal fails.\n+ */\n+int ntfs_stream_put(struct inode *vi)\n+{\n+\tstruct ntfs_inode *ni = NTFS_I(vi);\n+\n+\tif (atomic_dec_and_test(\u0026ni-\u003estream_open_count) \u0026\u0026\n+\t NInoStreamUnlinked(ni))\n+\t\treturn ntfs_stream_finish_remove(vi);\n+\treturn 0;\n+}\n+\n+/*\n+ * ntfs_remove_named_stream() - Remove a named DATA stream\n+ * @ni: base inode containing the stream\n+ * @uname: UTF-16LE stream name\n+ * @uname_len: length of @uname in UTF-16 code units\n+ * @expected_vi: expected cached stream inode, or NULL\n+ *\n+ * Refuse removal while the stream is open. If @expected_vi is non-NULL, it\n+ * must be the inode currently associated with the named attribute.\n+ *\n+ * Return: 0 on success, -ENOENT if the stream is absent, or another negative\n+ * errno.\n+ */\n+int ntfs_remove_named_stream(struct ntfs_inode *ni, __le16 *uname,\n+\t\tu32 uname_len, struct inode *expected_vi)\n+{\n+\tstruct inode *attr_vi;\n+\tstruct ntfs_inode *attr_ni;\n+\tint err;\n+\n+\tif (!ni || !uname || uname_len == 0)\n+\t\treturn -EINVAL;\n+\tif (NVolShutdown(ni-\u003evol))\n+\t\treturn -EIO;\n+\tif ((ni-\u003eflags \u0026 FILE_ATTR_READONLY) ||\n+\t IS_APPEND(VFS_I(ni)) || IS_IMMUTABLE(VFS_I(ni)))\n+\t\treturn -EPERM;\n+\tif (!(ni-\u003evol-\u003evol_flags \u0026 VOLUME_IS_DIRTY)) {\n+\t\terr = ntfs_set_volume_flags(ni-\u003evol, VOLUME_IS_DIRTY);\n+\t\tif (err)\n+\t\t\treturn err;\n+\t}\n+\n+\tmutex_lock(\u0026ni-\u003emrec_lock);\n+\tif (NInoBeingDeleted(ni) || !VFS_I(ni)-\u003ei_nlink) {\n+\t\terr = -ENOENT;\n+\t\tgoto out_unlock;\n+\t}\n+\tattr_vi = ntfs_attr_iget(VFS_I(ni), AT_DATA, uname, uname_len);\n+\tif (IS_ERR(attr_vi)) {\n+\t\terr = PTR_ERR(attr_vi);\n+\t\tgoto out_unlock;\n+\t}\n+\tif (expected_vi \u0026\u0026 attr_vi != expected_vi) {\n+\t\terr = -ESTALE;\n+\t\tgoto out_iput;\n+\t}\n+\n+\tattr_ni = NTFS_I(attr_vi);\n+\tif (NInoStreamUnlinked(attr_ni)) {\n+\t\terr = -ENOENT;\n+\t\tgoto out_iput;\n+\t}\n+\tif (atomic_read(\u0026attr_ni-\u003estream_open_count) \u003e 0) {\n+\t\terr = -EBUSY;\n+\t\tgoto out_iput;\n+\t}\n+\terr = ntfs_stream_inode_validate(attr_vi);\n+\tif (err)\n+\t\tgoto out_iput;\n+\n+\tNInoSetStreamUnlinked(attr_ni);\n+\tclear_nlink(attr_vi);\n+\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\n+\terr = ntfs_stream_finish_remove(attr_vi);\n+\tif (!err)\n+\t\tntfs_stream_update_base_time(ni);\n+\tiput(attr_vi);\n+\treturn err;\n+\n+out_iput:\n+\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\tiput(attr_vi);\n+\treturn err;\n+out_unlock:\n+\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\treturn err;\n+}\n+\n+/*\n+ * Validate access against the base inode and hold the stream against unlink\n+ * until the file is released.\n+ */\n+static int ntfs_stream_file_open(struct inode *inode, struct file *file)\n+{\n+\tstruct ntfs_inode *ni = NTFS_I(inode);\n+\tstruct ntfs_inode *base_ni = ni-\u003eext.base_ntfs_ino;\n+\tstruct inode *base_vi = VFS_I(base_ni);\n+\tint mask = MAY_OPEN;\n+\tint err;\n+\n+\tif (file-\u003ef_mode \u0026 FMODE_READ)\n+\t\tmask |= MAY_READ;\n+\tif (file-\u003ef_mode \u0026 FMODE_WRITE)\n+\t\tmask |= MAY_WRITE;\n+\terr = inode_permission(file_mnt_idmap(file), base_vi, mask);\n+\tif (err)\n+\t\treturn err;\n+\n+\tmutex_lock(\u0026base_ni-\u003emrec_lock);\n+\tif (NInoStreamUnlinked(ni))\n+\t\terr = -ENOENT;\n+\telse\n+\t\terr = ntfs_stream_inode_validate(inode);\n+\tif (err)\n+\t\tgoto out_unlock;\n+\tif ((file-\u003ef_mode \u0026 FMODE_WRITE) \u0026\u0026\n+\t (IS_IMMUTABLE(base_vi) ||\n+\t (IS_APPEND(base_vi) \u0026\u0026 !(file-\u003ef_flags \u0026 O_APPEND)))) {\n+\t\terr = -EPERM;\n+\t\tgoto out_unlock;\n+\t}\n+\tif ((file-\u003ef_flags \u0026 O_TRUNC) \u0026\u0026 IS_APPEND(base_vi)) {\n+\t\terr = -EPERM;\n+\t\tgoto out_unlock;\n+\t}\n+\tif ((file-\u003ef_flags \u0026 O_NOATIME) \u0026\u0026\n+\t !inode_owner_or_capable(file_mnt_idmap(file), base_vi)) {\n+\t\terr = -EPERM;\n+\t\tgoto out_unlock;\n+\t}\n+\terr = ntfs_file_open(inode, file);\n+\tif (err)\n+\t\tgoto out_unlock;\n+\tif (file-\u003ef_mode \u0026 FMODE_WRITE) {\n+\t\terr = get_write_access(base_vi);\n+\t\tif (err)\n+\t\t\tgoto out_unlock;\n+\t\tfile-\u003eprivate_data = base_vi;\n+\t}\n+\n+\tatomic_inc(\u0026ni-\u003estream_open_count);\n+out_unlock:\n+\tmutex_unlock(\u0026base_ni-\u003emrec_lock);\n+\treturn err;\n+}\n+\n+/* Drop open-time references and complete any deferred stream unlink. */\n+static int ntfs_stream_file_release(struct inode *inode, struct file *file)\n+{\n+\tint err, remove_err;\n+\n+\terr = ntfs_file_release(inode, file);\n+\tif (file-\u003eprivate_data) {\n+\t\tput_write_access(file-\u003eprivate_data);\n+\t\tfile-\u003eprivate_data = NULL;\n+\t}\n+\tremove_err = ntfs_stream_put(inode);\n+\tif (!err)\n+\t\terr = remove_err;\n+\treturn err;\n+}\n+\n+enum ntfs_stream_ioctl_op {\n+\tNTFS_STREAM_IOCTL_READ,\n+\tNTFS_STREAM_IOCTL_WRITE,\n+\tNTFS_STREAM_IOCTL_REMOVE,\n+};\n+\n+/*\n+ * Look up or create an ioctl target stream and return its referenced inode.\n+ */\n+static int ntfs_stream_ioctl_get_inode(struct inode *base_vi, __le16 *sname,\n+\t\tint sname_len, bool create, struct inode **stream_vi)\n+{\n+\tstruct ntfs_inode *base_ni = NTFS_I(base_vi);\n+\tstruct ntfs_attr_search_ctx *ctx;\n+\tstruct inode *attr_vi = NULL;\n+\tbool created = false;\n+\tint err;\n+\n+\t*stream_vi = NULL;\n+\n+\tmutex_lock(\u0026base_ni-\u003emrec_lock);\n+\tif (NVolShutdown(base_ni-\u003evol)) {\n+\t\terr = -EIO;\n+\t\tgoto out_unlock;\n+\t}\n+\tif (NInoBeingDeleted(base_ni) || !base_vi-\u003ei_nlink) {\n+\t\terr = -ENOENT;\n+\t\tgoto out_unlock;\n+\t}\n+\n+\tctx = ntfs_attr_get_search_ctx(base_ni, NULL);\n+\tif (!ctx) {\n+\t\terr = -ENOMEM;\n+\t\tgoto out_unlock;\n+\t}\n+\n+\terr = ntfs_attr_lookup(AT_DATA, sname, sname_len, IGNORE_CASE,\n+\t\t\t0, NULL, 0, ctx);\n+\tif (err == -ENOENT \u0026\u0026 create) {\n+\t\terr = ntfs_attr_add(base_ni, AT_DATA, sname, sname_len,\n+\t\t\t\tNULL, 0);\n+\t\tif (!err) {\n+\t\t\tcreated = true;\n+\t\t\tmark_mft_record_dirty(base_ni);\n+\t\t}\n+\t}\n+\tntfs_attr_put_search_ctx(ctx);\n+\tif (err)\n+\t\tgoto out_unlock;\n+\n+\tattr_vi = ntfs_attr_iget(base_vi, AT_DATA, sname, sname_len);\n+\tif (IS_ERR(attr_vi)) {\n+\t\terr = PTR_ERR(attr_vi);\n+\t\tattr_vi = NULL;\n+\t\tgoto out_unlock;\n+\t}\n+\tif (NInoStreamUnlinked(NTFS_I(attr_vi)))\n+\t\terr = create ? -EBUSY : -ENOENT;\n+\telse\n+\t\terr = ntfs_stream_inode_validate(attr_vi);\n+out_unlock:\n+\tmutex_unlock(\u0026base_ni-\u003emrec_lock);\n+\tif (created)\n+\t\tntfs_stream_update_base_time(base_ni);\n+\tif (err) {\n+\t\tiput(attr_vi);\n+\t\treturn err;\n+\t}\n+\n+\t*stream_vi = attr_vi;\n+\treturn 0;\n+}\n+\n+/* Check file mode, base-inode flags, and the requested transfer range. */\n+static int ntfs_stream_ioctl_access(struct file *filp,\n+\t\tenum ntfs_stream_ioctl_op op, loff_t pos, size_t len)\n+{\n+\tstruct inode *inode = file_inode(filp);\n+\tbool is_dir = S_ISDIR(inode-\u003ei_mode);\n+\tint err;\n+\n+\tif (op == NTFS_STREAM_IOCTL_READ) {\n+\t\tif (!(filp-\u003ef_mode \u0026 FMODE_READ))\n+\t\t\treturn -EBADF;\n+\t\treturn rw_verify_area(READ, filp, \u0026pos, len);\n+\t}\n+\n+\tif (!(filp-\u003ef_mode \u0026 FMODE_WRITE) \u0026\u0026 !is_dir)\n+\t\treturn -EBADF;\n+\tif (is_dir) {\n+\t\terr = inode_permission(file_mnt_idmap(filp), inode,\n+\t\t\t\tMAY_WRITE | MAY_EXEC);\n+\t\tif (err)\n+\t\t\treturn err;\n+\t}\n+\tif (IS_APPEND(inode) || IS_IMMUTABLE(inode))\n+\t\treturn -EPERM;\n+\tif (op == NTFS_STREAM_IOCTL_REMOVE)\n+\t\treturn 0;\n+\treturn rw_verify_area(WRITE, filp, \u0026pos, len);\n+}\n+\n+/*\n+ * Validate and execute a named-stream read, write, or remove ioctl request.\n+ */\n+static long ntfs_ioctl_stream(struct file *filp, unsigned long arg,\n+\t\tenum ntfs_stream_ioctl_op op)\n+{\n+\tstruct inode *base_vi = file_inode(filp);\n+\tstruct ntfs_inode *base_ni = NTFS_I(base_vi);\n+\tstruct ntfs_stream hdr;\n+\tstruct ntfs_stream *req;\n+\tstruct inode *stream_vi = NULL;\n+\t__le16 *uname = NULL, *sname;\n+\tsize_t data_size, total_size;\n+\tloff_t pos;\n+\ts64 ret;\n+\tint sname_len, err;\n+\tbool claimed = false, got_write = false, utf16;\n+\n+\tif (NVolShutdown(base_ni-\u003evol))\n+\t\treturn -EIO;\n+\tif (NInoAttr(base_ni) ||\n+\t (!S_ISREG(base_vi-\u003ei_mode) \u0026\u0026 !S_ISDIR(base_vi-\u003ei_mode)))\n+\t\treturn -EOPNOTSUPP;\n+\tif (copy_from_user(\u0026hdr, (void __user *)arg, sizeof(hdr)))\n+\t\treturn -EFAULT;\n+\n+\tif (hdr.io_len \u003e NTFS_STREAM_MAX_IO ||\n+\t (hdr.flags \u0026 ~NTFS_STREAM_FL_UTF16) || hdr.reserved)\n+\t\treturn -EINVAL;\n+\tif (!hdr.name_len)\n+\t\treturn -EINVAL;\n+\n+\tutf16 = hdr.flags \u0026 NTFS_STREAM_FL_UTF16;\n+\tif (utf16) {\n+\t\tif ((hdr.name_len \u0026 1) ||\n+\t\t hdr.name_len \u003e NTFS_MAX_NAME_LEN * sizeof(__le16))\n+\t\t\treturn -EINVAL;\n+\t} else if (hdr.name_len \u003e\n+\t\t\tNTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE) {\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tswitch (op) {\n+\tcase NTFS_STREAM_IOCTL_READ:\n+\tcase NTFS_STREAM_IOCTL_WRITE:\n+\t\tif (!hdr.io_len)\n+\t\t\treturn -EINVAL;\n+\t\tif (hdr.stream_offset \u003e S64_MAX ||\n+\t\t hdr.stream_offset \u003e S64_MAX - hdr.io_len)\n+\t\t\treturn -EOVERFLOW;\n+\t\tdata_size = hdr.io_len;\n+\t\tbreak;\n+\tcase NTFS_STREAM_IOCTL_REMOVE:\n+\t\tif (hdr.io_len || hdr.stream_offset)\n+\t\t\treturn -EINVAL;\n+\t\tdata_size = 0;\n+\t\tbreak;\n+\tdefault:\n+\t\treturn -ENOTTY;\n+\t}\n+\n+\tif (check_add_overflow(sizeof(hdr), (size_t)hdr.name_len,\n+\t\t\t\u0026total_size) ||\n+\t check_add_overflow(total_size, data_size, \u0026total_size))\n+\t\treturn -EOVERFLOW;\n+\n+\tif (op == NTFS_STREAM_IOCTL_READ) {\n+\t\treq = kvmalloc(total_size, GFP_KERNEL);\n+\t\tif (!req)\n+\t\t\treturn -ENOMEM;\n+\t\tif (copy_from_user(req, (void __user *)arg,\n+\t\t\t\tsizeof(hdr) + hdr.name_len)) {\n+\t\t\tkvfree(req);\n+\t\t\treturn -EFAULT;\n+\t\t}\n+\t} else {\n+\t\treq = vmemdup_user((void __user *)arg, total_size);\n+\t\tif (IS_ERR(req))\n+\t\t\treturn PTR_ERR(req);\n+\t}\n+\n+\treq-\u003ebytes_returned = 0;\n+\tif (req-\u003estream_offset != hdr.stream_offset ||\n+\t req-\u003eio_len != hdr.io_len || req-\u003ename_len != hdr.name_len ||\n+\t req-\u003eflags != hdr.flags || req-\u003ereserved) {\n+\t\terr = -EINVAL;\n+\t\tgoto out_free;\n+\t}\n+\n+\tif (utf16) {\n+\t\tsname = (__le16 *)req-\u003ebuffer;\n+\t\tsname_len = req-\u003ename_len / sizeof(__le16);\n+\t} else {\n+\t\tif (memchr(req-\u003ebuffer, '\\0', req-\u003ename_len)) {\n+\t\t\terr = -EINVAL;\n+\t\t\tgoto out_free;\n+\t\t}\n+\t\tsname_len = ntfs_nlstoucs(base_ni-\u003evol, req-\u003ebuffer,\n+\t\t\t\treq-\u003ename_len, \u0026uname, NTFS_MAX_NAME_LEN);\n+\t\tif (sname_len \u003c 0) {\n+\t\t\terr = sname_len;\n+\t\t\tgoto out_free;\n+\t\t}\n+\t\tsname = uname;\n+\t}\n+\terr = ntfs_check_stream_name(sname, sname_len);\n+\tif (err)\n+\t\tgoto out_free;\n+\n+\tpos = hdr.stream_offset;\n+\terr = ntfs_stream_ioctl_access(filp, op, pos, data_size);\n+\tif (err)\n+\t\tgoto out_free;\n+\n+\tif (op == NTFS_STREAM_IOCTL_REMOVE) {\n+\t\terr = mnt_want_write_file(filp);\n+\t\tif (err)\n+\t\t\tgoto out_free;\n+\t\terr = ntfs_remove_named_stream(base_ni, sname, sname_len, NULL);\n+\t\tmnt_drop_write_file(filp);\n+\t\tgoto out_free;\n+\t}\n+\n+\tif (op == NTFS_STREAM_IOCTL_WRITE) {\n+\t\terr = mnt_want_write_file(filp);\n+\t\tif (err)\n+\t\t\tgoto out_free;\n+\t\tgot_write = true;\n+\t\tinode_lock(base_vi);\n+\t\terr = file_remove_privs(filp);\n+\t\tinode_unlock(base_vi);\n+\t\tif (err)\n+\t\t\tgoto out_drop_write;\n+\t\tif (!(base_ni-\u003evol-\u003evol_flags \u0026 VOLUME_IS_DIRTY)) {\n+\t\t\terr = ntfs_set_volume_flags(base_ni-\u003evol,\n+\t\t\t\t\tVOLUME_IS_DIRTY);\n+\t\t\tif (err)\n+\t\t\t\tgoto out_drop_write;\n+\t\t}\n+\t}\n+\n+\terr = ntfs_stream_ioctl_get_inode(base_vi, sname, sname_len,\n+\t\t\top == NTFS_STREAM_IOCTL_WRITE, \u0026stream_vi);\n+\tif (err)\n+\t\tgoto out_drop_write;\n+\n+\terr = ntfs_stream_claim(stream_vi);\n+\tif (err)\n+\t\tgoto out_drop_write;\n+\tclaimed = true;\n+\n+\tif (op == NTFS_STREAM_IOCTL_READ) {\n+\t\tinode_lock_shared(stream_vi);\n+\t\tif (pos \u003c i_size_read(stream_vi))\n+\t\t\tret = ntfs_inode_attr_pread(stream_vi, pos, data_size,\n+\t\t\t\t\treq-\u003ebuffer + req-\u003ename_len);\n+\t\telse\n+\t\t\tret = 0;\n+\t\tinode_unlock_shared(stream_vi);\n+\t\tif (ret \u003c 0)\n+\t\t\terr = ret;\n+\t\telse\n+\t\t\treq-\u003ebytes_returned = ret;\n+\t\tif (!err)\n+\t\t\tfile_accessed(filp);\n+\t} else {\n+\t\tinode_lock(stream_vi);\n+\t\tinode_dio_wait(stream_vi);\n+\t\t/* Exclude faults before taking MFT record and folio locks. */\n+\t\tfilemap_invalidate_lock(stream_vi-\u003ei_mapping);\n+\t\terr = inode_newsize_ok(stream_vi, pos + data_size);\n+\t\tif (!err) {\n+\t\t\tret = ntfs_inode_attr_pwrite(stream_vi, pos, data_size,\n+\t\t\t\t\treq-\u003ebuffer + req-\u003ename_len, false);\n+\t\t\tif (ret \u003c 0)\n+\t\t\t\terr = ret;\n+\t\t\telse\n+\t\t\t\treq-\u003ebytes_returned = ret;\n+\t\t}\n+\t\tfilemap_invalidate_unlock(stream_vi-\u003ei_mapping);\n+\t\tinode_unlock(stream_vi);\n+\t}\n+\tif (claimed) {\n+\t\tint put_err;\n+\n+\t\tput_err = ntfs_stream_put(stream_vi);\n+\t\tclaimed = false;\n+\t\tif (!err \u0026\u0026 put_err)\n+\t\t\terr = put_err;\n+\t}\n+\tiput(stream_vi);\n+\tstream_vi = NULL;\n+\tif (err)\n+\t\tgoto out_drop_write;\n+\n+\tif (op == NTFS_STREAM_IOCTL_READ) {\n+\t\tif (copy_to_user((void __user *)arg, req,\n+\t\t\t\tsizeof(*req) + req-\u003ename_len +\n+\t\t\t\treq-\u003ebytes_returned))\n+\t\t\terr = -EFAULT;\n+\t} else if (copy_to_user((void __user *)arg +\n+\t\t\toffsetof(struct ntfs_stream, bytes_returned),\n+\t\t\t\u0026req-\u003ebytes_returned, sizeof(req-\u003ebytes_returned))) {\n+\t\terr = -EFAULT;\n+\t}\n+\tgoto out_drop_write;\n+\n+out_drop_write:\n+\tif (claimed)\n+\t\tntfs_stream_put(stream_vi);\n+\tif (stream_vi)\n+\t\tiput(stream_vi);\n+\tif (got_write)\n+\t\tmnt_drop_write_file(filp);\n+out_free:\n+\tif (uname)\n+\t\tkmem_cache_free(ntfs_name_cache, uname);\n+\tkvfree(req);\n+\treturn err;\n+}\n+\n+/*\n+ * ntfs_ioctl_stream_read() - Handle a named-stream read ioctl\n+ * @filp: file opened on the base file or directory\n+ * @arg: userspace pointer to the request and data buffer\n+ *\n+ * Return: 0 on success, or a negative errno.\n+ */\n+long ntfs_ioctl_stream_read(struct file *filp, unsigned long arg)\n+{\n+\treturn ntfs_ioctl_stream(filp, arg, NTFS_STREAM_IOCTL_READ);\n+}\n+\n+/*\n+ * ntfs_ioctl_stream_write() - Handle a named-stream write ioctl\n+ * @filp: file opened on the base file or directory\n+ * @arg: userspace pointer to the request and data buffer\n+ *\n+ * Create the stream if needed.\n+ *\n+ * Return: 0 on success, or a negative errno.\n+ */\n+long ntfs_ioctl_stream_write(struct file *filp, unsigned long arg)\n+{\n+\treturn ntfs_ioctl_stream(filp, arg, NTFS_STREAM_IOCTL_WRITE);\n+}\n+\n+/*\n+ * ntfs_ioctl_stream_remove() - Handle a named-stream remove ioctl\n+ * @filp: file opened on the base file or directory\n+ * @arg: userspace pointer to the request\n+ *\n+ * Return: 0 on success, or a negative errno.\n+ */\n+long ntfs_ioctl_stream_remove(struct file *filp, unsigned long arg)\n+{\n+\treturn ntfs_ioctl_stream(filp, arg, NTFS_STREAM_IOCTL_REMOVE);\n+}\n+\n+/*\n+ * ntfs_ioctl_list_streams() - List named DATA streams\n+ * @filp: file opened on the base file or directory\n+ * @arg: userspace pointer to the request and output buffer\n+ *\n+ * On -ENOSPC, return the required buffer size in the request header.\n+ *\n+ * Return: 0 on success, or a negative errno.\n+ */\n+int ntfs_ioctl_list_streams(struct file *filp, unsigned long arg)\n+{\n+\tstruct inode *inode = file_inode(filp);\n+\tstruct ntfs_inode *ni = NTFS_I(inode);\n+\tstruct ntfs_list_streams hdr;\n+\tstruct ntfs_stream_entry *entry, *last_entry = NULL;\n+\tsize_t required = 0;\n+\tvoid *kbuf = NULL;\n+\tvoid __user *ubuf;\n+\tstruct attr_record *a;\n+\tstruct ntfs_attr_search_ctx *actx;\n+\tint ret = 0, err, count = 0;\n+\tsize_t entry_size, offset = 0;\n+\tconst size_t name_offset = offsetof(struct ntfs_stream_entry, name);\n+\tint name_len;\n+\tunsigned char *sn = NULL;\n+\tbool utf16;\n+\n+\tif (NVolShutdown(ni-\u003evol))\n+\t\treturn -EIO;\n+\tif (NInoAttr(ni) ||\n+\t (!S_ISREG(inode-\u003ei_mode) \u0026\u0026 !S_ISDIR(inode-\u003ei_mode)))\n+\t\treturn -EOPNOTSUPP;\n+\terr = inode_permission(file_mnt_idmap(filp), inode, MAY_READ);\n+\tif (err)\n+\t\treturn err;\n+\n+\tif (copy_from_user(\u0026hdr, (void __user *)arg, sizeof(hdr)))\n+\t\treturn -EFAULT;\n+\n+\tif ((hdr.flags \u0026 ~NTFS_STREAM_FL_UTF16) || hdr.reserved)\n+\t\treturn -EINVAL;\n+\n+\tutf16 = hdr.flags \u0026 NTFS_STREAM_FL_UTF16;\n+\n+\tubuf = (void __user *)arg + sizeof(hdr);\n+\n+\tmutex_lock(\u0026ni-\u003emrec_lock);\n+\tactx = ntfs_attr_get_search_ctx(ni, NULL);\n+\tif (!actx) {\n+\t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\treturn -ENOMEM;\n+\t}\n+\n+\twhile ((err = ntfs_attrs_walk(actx)) == 0) {\n+\t\ta = actx-\u003eattr;\n+\t\tif (a-\u003etype != AT_DATA || !a-\u003ename_length)\n+\t\t\tcontinue;\n+\t\tif (a-\u003enon_resident \u0026\u0026\n+\t\t a-\u003edata.non_resident.lowest_vcn)\n+\t\t\tcontinue;\n+\t\tif (ntfs_stream_unlinked(ni,\n+\t\t\t\t(__le16 *)((u8 *)a +\n+\t\t\t\tle16_to_cpu(a-\u003ename_offset)),\n+\t\t\t\ta-\u003ename_length))\n+\t\t\tcontinue;\n+\n+\t\tif (utf16) {\n+\t\t\tname_len = a-\u003ename_length * sizeof(__le16);\n+\t\t} else {\n+\t\t\tname_len = ntfs_ucstonls(ni-\u003evol,\n+\t\t\t\t\t(__le16 *)((u8 *)a +\n+\t\t\t\t\tle16_to_cpu(a-\u003ename_offset)),\n+\t\t\t\t\ta-\u003ename_length, \u0026sn, 0);\n+\t\t\tif (name_len \u003c 0) {\n+\t\t\t\tif (name_len == -EILSEQ ||\n+\t\t\t\t name_len == -ENAMETOOLONG)\n+\t\t\t\t\tcontinue;\n+\t\t\t\tret = name_len;\n+\t\t\t\tgoto out;\n+\t\t\t}\n+\t\t\tkfree(sn);\n+\t\t\tsn = NULL;\n+\t\t}\n+\n+\t\tentry_size = ALIGN(name_offset + name_len, 8);\n+\n+\t\tif (required \u003e SIZE_MAX - entry_size) {\n+\t\t\tret = -EOVERFLOW;\n+\t\t\tgoto out;\n+\t\t}\n+\t\trequired += entry_size;\n+\t\tcount++;\n+\t}\n+\tif (err != -ENOENT) {\n+\t\tret = err;\n+\t\tgoto out;\n+\t}\n+\n+\thdr.stream_count = count;\n+\thdr.bytes_returned = required;\n+\n+\tif (!count)\n+\t\tgoto out;\n+\n+\tif (hdr.buffer_size \u003c required) {\n+\t\tret = -ENOSPC;\n+\t\tgoto out;\n+\t}\n+\n+\tkbuf = kvzalloc(required, GFP_NOFS);\n+\tif (!kbuf) {\n+\t\tret = -ENOMEM;\n+\t\tgoto out;\n+\t}\n+\n+\tntfs_attr_reinit_search_ctx(actx);\n+\twhile ((err = ntfs_attrs_walk(actx)) == 0) {\n+\t\ta = actx-\u003eattr;\n+\t\tif (a-\u003etype != AT_DATA || !a-\u003ename_length)\n+\t\t\tcontinue;\n+\t\tif (a-\u003enon_resident \u0026\u0026\n+\t\t a-\u003edata.non_resident.lowest_vcn)\n+\t\t\tcontinue;\n+\t\tif (ntfs_stream_unlinked(ni,\n+\t\t\t\t(__le16 *)((u8 *)a +\n+\t\t\t\tle16_to_cpu(a-\u003ename_offset)),\n+\t\t\t\ta-\u003ename_length))\n+\t\t\tcontinue;\n+\t\tif (utf16) {\n+\t\t\tsn = NULL;\n+\t\t\tname_len = a-\u003ename_length * sizeof(__le16);\n+\t\t} else {\n+\t\t\tname_len = ntfs_ucstonls(ni-\u003evol,\n+\t\t\t\t\t(__le16 *)((u8 *)a +\n+\t\t\t\t\tle16_to_cpu(a-\u003ename_offset)),\n+\t\t\t\t\ta-\u003ename_length, \u0026sn, 0);\n+\t\t\tif (name_len \u003c 0) {\n+\t\t\t\tif (name_len == -EILSEQ ||\n+\t\t\t\t name_len == -ENAMETOOLONG)\n+\t\t\t\t\tcontinue;\n+\t\t\t\tret = name_len;\n+\t\t\t\tgoto out;\n+\t\t\t}\n+\t\t}\n+\n+\t\tentry_size = ALIGN(name_offset + name_len, 8);\n+\t\tif (offset \u003e required - entry_size) {\n+\t\t\tret = -EOVERFLOW;\n+\t\t\tkfree(sn);\n+\t\t\tsn = NULL;\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tentry = (struct ntfs_stream_entry *)(kbuf + offset);\n+\n+\t\tif (a-\u003enon_resident) {\n+\t\t\tentry-\u003esize = le64_to_cpu(a-\u003edata.non_resident.data_size);\n+\t\t\tentry-\u003ealloc_size =\n+\t\t\t\tle64_to_cpu(a-\u003edata.non_resident.allocated_size);\n+\t\t} else {\n+\t\t\tentry-\u003esize = le32_to_cpu(a-\u003edata.resident.value_length);\n+\t\t\tentry-\u003ealloc_size = le32_to_cpu(a-\u003elength) -\n+\t\t\t\tle16_to_cpu(a-\u003edata.resident.value_offset);\n+\t\t}\n+\n+\t\tentry-\u003ename_len = name_len;\n+\t\tentry-\u003ename_offset = name_offset;\n+\t\tif (utf16)\n+\t\t\tmemcpy(entry-\u003ename,\n+\t\t\t (u8 *)a + le16_to_cpu(a-\u003ename_offset), name_len);\n+\t\telse\n+\t\t\tmemcpy(entry-\u003ename, sn, name_len);\n+\t\tkfree(sn);\n+\t\tsn = NULL;\n+\n+\t\tentry-\u003enext_entry_off = entry_size;\n+\t\tlast_entry = entry;\n+\t\toffset += entry_size;\n+\t}\n+\tif (err != -ENOENT) {\n+\t\tret = err;\n+\t} else {\n+\t\t/* The chain terminates at the last entry. */\n+\t\tif (last_entry)\n+\t\t\tlast_entry-\u003enext_entry_off = 0;\n+\t\thdr.bytes_returned = offset;\n+\t}\n+\n+out:\n+\tkfree(sn);\n+\tntfs_attr_put_search_ctx(actx);\n+\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\n+\tif (ret \u0026\u0026 ret != -ENOSPC)\n+\t\tgoto out_free;\n+\n+\tif (!ret \u0026\u0026 kbuf \u0026\u0026 copy_to_user(ubuf, kbuf, hdr.bytes_returned)) {\n+\t\tret = -EFAULT;\n+\t\tgoto out_free;\n+\t}\n+\n+\tif (copy_to_user((void __user *)arg, \u0026hdr, sizeof(hdr)))\n+\t\tret = -EFAULT;\n+\n+out_free:\n+\tkvfree(kbuf);\n+\treturn ret;\n+}\n+\n+const struct file_operations ntfs_stream_file_ops = {\n+\t.llseek\t\t= ntfs_file_llseek,\n+\t.read_iter\t= ntfs_file_read_iter,\n+\t.write_iter\t= ntfs_file_write_iter,\n+\t.fsync\t\t= ntfs_file_fsync,\n+\t.fallocate\t= ntfs_fallocate,\n+\t.mmap_prepare\t= ntfs_file_mmap_prepare,\n+\t.open\t\t= ntfs_stream_file_open,\n+\t.release\t= ntfs_stream_file_release,\n+\t.splice_read\t= ntfs_file_splice_read,\n+\t.splice_write\t= iter_file_splice_write,\n+};\n+\n+const struct inode_operations ntfs_stream_inode_ops = {\n+\t.permission\t= ntfs_stream_permission,\n+\t.setattr\t= ntfs_stream_setattr,\n+\t.getattr\t= ntfs_stream_getattr,\n+\t.listxattr\t= ntfs_stream_listxattr,\n+#ifdef CONFIG_NTFS_FS_POSIX_ACL\n+\t.get_acl\t= ntfs_stream_get_acl,\n+\t.set_acl\t= ntfs_stream_set_acl,\n+#endif\n+\t.update_time\t= ntfs_stream_update_time,\n+\t.fiemap\t\t= ntfs_fiemap,\n+};\ndiff --git a/fs/ntfs/namei.c b/fs/ntfs/namei.c\nindex 75e2010965257..9251951a76fb3 100644\n--- a/fs/ntfs/namei.c\n+++ b/fs/ntfs/namei.c\n@@ -8,6 +8,7 @@\n \n #include \u003clinux/exportfs.h\u003e\n #include \u003clinux/iversion.h\u003e\n+#include \u003clinux/namei.h\u003e\n \n #include \"ntfs.h\"\n #include \"time.h\"\n@@ -15,6 +16,7 @@\n #include \"reparse.h\"\n #include \"object_id.h\"\n #include \"ea.h\"\n+#include \"stream.h\"\n \n static const __le16 aux_name_le[3] = {\n \tcpu_to_le16('A'), cpu_to_le16('U'), cpu_to_le16('X')\n@@ -57,7 +59,18 @@ static inline int ntfs_check_bad_char(const __le16 *wc, unsigned int wc_len)\n \treturn 0;\n }\n \n-static int ntfs_check_bad_windows_name(struct ntfs_volume *vol,\n+/*\n+ * ntfs_check_bad_windows_name() - Validate a name against Windows rules\n+ * @vol: NTFS volume\n+ * @wc: UTF-16LE name\n+ * @wc_len: length of @wc in UTF-16 code units\n+ *\n+ * When Windows-name checks are enabled, reject disallowed characters,\n+ * trailing spaces or dots, and reserved DOS device names.\n+ *\n+ * Return: 0 if valid, or -EINVAL otherwise.\n+ */\n+int ntfs_check_bad_windows_name(struct ntfs_volume *vol,\n \t\t\t\t const __le16 *wc,\n \t\t\t\t unsigned int wc_len)\n {\n@@ -167,19 +180,29 @@ static int ntfs_check_bad_windows_name(struct ntfs_volume *vol,\n *\n * Locking: Caller must hold i_mutex on the directory.\n */\n+\n static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,\n \t\tunsigned int flags)\n {\n \tstruct ntfs_volume *vol = NTFS_SB(dir_ino-\u003ei_sb);\n+\tstruct ntfs_stream_path stream_path;\n \tstruct inode *dent_inode;\n \t__le16 *uname;\n \tstruct ntfs_name *name = NULL;\n \tu64 mref;\n \tunsigned long dent_ino;\n \tint uname_len;\n+\tint stream_path_len;\n \n \tntfs_debug(\"Looking up %pd in directory inode 0x%llx.\",\n \t\t\tdent, NTFS_I(dir_ino)-\u003emft_no);\n+\tstream_path_len = ntfs_stream_path_parse(vol, \u0026dent-\u003ed_name,\n+\t\t\t\u0026stream_path);\n+\tif (stream_path_len \u003c 0)\n+\t\treturn ERR_PTR(stream_path_len);\n+\tif (stream_path_len)\n+\t\treturn ntfs_lookup_stream(dir_ino, dent, \u0026stream_path);\n+\n \t/* Convert the name of the dentry to Unicode. */\n \tuname_len = ntfs_nlstoucs(vol, dent-\u003ed_name.name, dent-\u003ed_name.len,\n \t\t\t\t \u0026uname, NTFS_MAX_NAME_LEN);\n@@ -405,6 +428,7 @@ static struct ntfs_inode *__ntfs_create(struct mnt_idmap *idmap, struct inode *d\n \tstruct inode *vi;\n \tstruct mft_record *ni_mrec, *dni_mrec;\n \tstruct super_block *sb = dir_ni-\u003evol-\u003esb;\n+\tstruct inode *rollback_data_vi = NULL, *rollback_sd_vi = NULL;\n \t__le64 parent_mft_ref;\n \tu64 child_mft_ref;\n \t__le16 ea_size;\n@@ -694,11 +718,25 @@ static struct ntfs_inode *__ntfs_create(struct mnt_idmap *idmap, struct inode *d\n \treturn ni;\n \n err_out:\n-\tif (rollback_sd)\n-\t\tntfs_attr_remove(ni, AT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0);\n+\tif (rollback_sd) {\n+\t\tint rollback_err;\n+\n+\t\trollback_err = ntfs_attr_remove(ni,\n+\t\t\t\tAT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0,\n+\t\t\t\t\u0026rollback_sd_vi);\n+\t\tif (rollback_err)\n+\t\t\tntfs_error(sb,\n+\t\t\t\t\"Failed to roll back security descriptor.\\n\");\n+\t}\n+\n+\tif (rollback_data) {\n+\t\tint rollback_err;\n \n-\tif (rollback_data)\n-\t\tntfs_attr_remove(ni, AT_DATA, AT_UNNAMED, 0);\n+\t\trollback_err = ntfs_attr_remove(ni, AT_DATA, AT_UNNAMED,\n+\t\t\t\t0, \u0026rollback_data_vi);\n+\t\tif (rollback_err)\n+\t\t\tntfs_error(sb, \"Failed to roll back DATA attribute.\\n\");\n+\t}\n \n \tif (rollback_reparse)\n \t\tntfs_delete_reparse_index(ni);\n@@ -726,6 +764,10 @@ static struct ntfs_inode *__ntfs_create(struct mnt_idmap *idmap, struct inode *d\n \tmutex_unlock(\u0026dir_ni-\u003emrec_lock);\n \tmutex_unlock(\u0026ni-\u003emrec_lock);\n \n+\tif (rollback_data_vi)\n+\t\tiput(rollback_data_vi);\n+\tif (rollback_sd_vi)\n+\t\tiput(rollback_sd_vi);\n \tremove_inode_hash(vi);\n \tdiscard_new_inode(vi);\n \treturn ERR_PTR(err);\n@@ -736,12 +778,35 @@ static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir,\n {\n \tstruct ntfs_volume *vol = NTFS_SB(dir-\u003ei_sb);\n \tstruct ntfs_inode *ni;\n-\t__le16 *uname;\n-\tint uname_len, err;\n+\tstruct inode *stream_vi = NULL;\n+\t__le16 *uname, *base_name = NULL, *stream_name = NULL;\n+\tint uname_len, stream_len, path_result, err;\n \n \tif (NVolShutdown(vol))\n \t\treturn -EIO;\n \n+\tpath_result = ntfs_stream_path_names(vol, \u0026dentry-\u003ed_name, \u0026base_name,\n+\t\t\t\u0026uname_len, \u0026stream_name, \u0026stream_len);\n+\tif (path_result) {\n+\t\tif (path_result \u003c 0)\n+\t\t\treturn path_result;\n+\t\tstream_vi = ntfs_create_named_stream(idmap, dir, base_name,\n+\t\t\t\tuname_len, stream_name, stream_len);\n+\t\tif (IS_ERR(stream_vi)) {\n+\t\t\terr = PTR_ERR(stream_vi);\n+\t\t\tgoto out_free_stream_names;\n+\t\t}\n+\t\tkmem_cache_free(ntfs_name_cache, stream_name);\n+\t\tkmem_cache_free(ntfs_name_cache, base_name);\n+\t\td_instantiate(dentry, stream_vi);\n+\t\treturn 0;\n+\n+out_free_stream_names:\n+\t\tkmem_cache_free(ntfs_name_cache, stream_name);\n+\t\tkmem_cache_free(ntfs_name_cache, base_name);\n+\t\treturn err;\n+\t}\n+\n \tuname_len = ntfs_nlstoucs(vol, dentry-\u003ed_name.name, dentry-\u003ed_name.len,\n \t\t\t\t \u0026uname, NTFS_MAX_NAME_LEN);\n \tif (uname_len \u003c 0) {\n@@ -758,7 +823,8 @@ static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir,\n \n \tntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);\n \n-\tni = __ntfs_create(idmap, dir, uname, uname_len, S_IFREG | mode, 0, NULL, 0);\n+\tni = __ntfs_create(idmap, dir, uname, uname_len, S_IFREG | mode, 0,\n+\t\t\tNULL, 0);\n \tkmem_cache_free(ntfs_name_cache, uname);\n \tif (IS_ERR(ni))\n \t\treturn PTR_ERR(ni);\n@@ -805,8 +871,29 @@ static int ntfs_test_inode_attr(struct inode *vi, void *data)\n \t\treturn 0;\n \tif (NInoAttr(ni) || ni-\u003enr_extents == -1)\n \t\treturn 1;\n-\telse\n-\t\treturn 0;\n+\treturn 0;\n+}\n+\n+static void ntfs_cleanup_deleted_inode(struct ntfs_inode *ni)\n+{\n+\tstruct inode *attr_vi;\n+\tstruct super_block *sb = VFS_I(ni)-\u003ei_sb;\n+\n+\tif (!NInoBeingDeleted(ni))\n+\t\treturn;\n+\n+\twhile ((attr_vi = ilookup5(sb, ni-\u003emft_no, ntfs_test_inode_attr,\n+\t\t\t(void *)(uintptr_t)ni-\u003emft_no))) {\n+\t\tstruct ntfs_inode *attr_ni = NTFS_I(attr_vi);\n+\n+\t\tif (ntfs_inode_is_named_stream(attr_ni)) {\n+\t\t\tif (atomic_read(\u0026attr_ni-\u003estream_open_count) \u003e 0)\n+\t\t\t\tNInoSetStreamUnlinked(attr_ni);\n+\t\t\tremove_inode_hash(attr_vi);\n+\t\t}\n+\t\tclear_nlink(attr_vi);\n+\t\tiput(attr_vi);\n+\t}\n }\n \n /*\n@@ -829,10 +916,10 @@ static int ntfs_delete(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,\n \tstruct file_name_attr *fn = NULL;\n \tbool looking_for_dos_name = false, looking_for_win32_name = false;\n \tbool case_sensitive_match = true;\n+\tbool link_count_zero = false;\n \tint err = 0;\n \tstruct mft_record *ni_mrec;\n \tstruct super_block *sb;\n-\tbool link_count_zero = false;\n \n \tntfs_debug(\"Entering.\\n\");\n \n@@ -976,22 +1063,10 @@ static int ntfs_delete(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,\n \tif (need_lock == true) {\n \t\tmutex_unlock(\u0026dir_ni-\u003emrec_lock);\n \t\tmutex_unlock(\u0026ni-\u003emrec_lock);\n+\t\tif (link_count_zero)\n+\t\t\tntfs_cleanup_deleted_inode(ni);\n \t}\n \n-\t/*\n-\t * If hard link count is not equal to zero then we are done. In other\n-\t * case there are no reference to this inode left, so we should free all\n-\t * non-resident attributes and mark all MFT record as not in use.\n-\t */\n-\tif (link_count_zero == true) {\n-\t\tstruct inode *attr_vi;\n-\n-\t\twhile ((attr_vi = ilookup5(sb, ni-\u003emft_no, ntfs_test_inode_attr,\n-\t\t\t\t\t (void *)(uintptr_t)ni-\u003emft_no)) != NULL) {\n-\t\t\tclear_nlink(attr_vi);\n-\t\t\tiput(attr_vi);\n-\t\t}\n-\t}\n \tntfs_debug(\"Done.\\n\");\n \treturn 0;\n err_out:\n@@ -1016,6 +1091,11 @@ static int ntfs_unlink(struct inode *dir, struct dentry *dentry)\n \tif (NVolShutdown(vol))\n \t\treturn -EIO;\n \n+\tif (ntfs_stream_path_has_colon(vol, \u0026dentry-\u003ed_name))\n+\t\treturn ntfs_unlink_named_stream(dir, dentry);\n+\tif (NInoAttr(ni))\n+\t\treturn -EOPNOTSUPP;\n+\n \tuname_len = ntfs_nlstoucs(vol, dentry-\u003ed_name.name, dentry-\u003ed_name.len,\n \t\t\t\t \u0026uname, NTFS_MAX_NAME_LEN);\n \tif (uname_len \u003c 0) {\n@@ -1059,6 +1139,10 @@ static struct dentry *ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,\n \tif (NVolShutdown(vol))\n \t\treturn ERR_PTR(-EIO);\n \n+\terr = ntfs_stream_path_check(vol, \u0026dentry-\u003ed_name);\n+\tif (err)\n+\t\treturn ERR_PTR(err);\n+\n \tuname_len = ntfs_nlstoucs(vol, dentry-\u003ed_name.name, dentry-\u003ed_name.len,\n \t\t\t\t \u0026uname, NTFS_MAX_NAME_LEN);\n \tif (uname_len \u003c 0) {\n@@ -1075,7 +1159,8 @@ static struct dentry *ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,\n \n \tntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);\n \n-\tni = __ntfs_create(idmap, dir, uname, uname_len, mode, 0, NULL, 0);\n+\tni = __ntfs_create(idmap, dir, uname, uname_len, mode, 0,\n+\t\t\tNULL, 0);\n \tkmem_cache_free(ntfs_name_cache, uname);\n \tif (IS_ERR(ni)) {\n \t\terr = PTR_ERR(ni);\n@@ -1099,6 +1184,10 @@ static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)\n \tif (NVolShutdown(vol))\n \t\treturn -EIO;\n \n+\terr = ntfs_stream_path_check(vol, \u0026dentry-\u003ed_name);\n+\tif (err)\n+\t\treturn err;\n+\n \tni = NTFS_I(vi);\n \tuname_len = ntfs_nlstoucs(vol, dentry-\u003ed_name.name, dentry-\u003ed_name.len,\n \t\t\t\t \u0026uname, NTFS_MAX_NAME_LEN);\n@@ -1263,6 +1352,13 @@ static int ntfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,\n \tif (NVolShutdown(old_dir_ni-\u003evol))\n \t\treturn -EIO;\n \n+\terr = ntfs_stream_path_check(vol, \u0026old_dentry-\u003ed_name);\n+\tif (err)\n+\t\treturn err;\n+\terr = ntfs_stream_path_check(vol, \u0026new_dentry-\u003ed_name);\n+\tif (err)\n+\t\treturn err;\n+\n \tif (flags \u0026 (RENAME_EXCHANGE | RENAME_WHITEOUT))\n \t\treturn -EINVAL;\n \n@@ -1385,6 +1481,8 @@ static int ntfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,\n \tif (new_ni)\n \t\tmutex_unlock(\u0026new_ni-\u003emrec_lock);\n \tmutex_unlock(\u0026old_ni-\u003emrec_lock);\n+\tif (new_ni)\n+\t\tntfs_cleanup_deleted_inode(new_ni);\n \tif (uname_new)\n \t\tkmem_cache_free(ntfs_name_cache, uname_new);\n \tif (uname_old)\n@@ -1408,6 +1506,10 @@ static int ntfs_symlink(struct mnt_idmap *idmap, struct inode *dir,\n \tif (NVolShutdown(vol))\n \t\treturn -EIO;\n \n+\terr = ntfs_stream_path_check(vol, \u0026dentry-\u003ed_name);\n+\tif (err)\n+\t\treturn err;\n+\n \tusrc_len = ntfs_nlstoucs(vol, dentry-\u003ed_name.name,\n \t\t\t\t dentry-\u003ed_name.len, \u0026usrc, NTFS_MAX_NAME_LEN);\n \tif (usrc_len \u003c 0) {\n@@ -1453,6 +1555,10 @@ static int ntfs_mknod(struct mnt_idmap *idmap, struct inode *dir,\n \tif (NVolShutdown(vol))\n \t\treturn -EIO;\n \n+\terr = ntfs_stream_path_check(vol, \u0026dentry-\u003ed_name);\n+\tif (err)\n+\t\treturn err;\n+\n \tuname_len = ntfs_nlstoucs(vol, dentry-\u003ed_name.name,\n \t\t\tdentry-\u003ed_name.len, \u0026uname, NTFS_MAX_NAME_LEN);\n \tif (uname_len \u003c 0) {\n@@ -1505,6 +1611,13 @@ static int ntfs_link(struct dentry *old_dentry, struct inode *dir,\n \tif (NVolShutdown(vol))\n \t\treturn -EIO;\n \n+\tif (NInoAttr(ni))\n+\t\treturn -EOPNOTSUPP;\n+\n+\terr = ntfs_stream_path_check(vol, \u0026dentry-\u003ed_name);\n+\tif (err)\n+\t\treturn err;\n+\n \tuname_len = ntfs_nlstoucs(vol, dentry-\u003ed_name.name,\n \t\t\tdentry-\u003ed_name.len, \u0026uname, NTFS_MAX_NAME_LEN);\n \tif (uname_len \u003c 0) {\n@@ -1658,11 +1771,19 @@ static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,\n \t\t\t\t ntfs_nfs_get_inode);\n }\n \n+static int ntfs_encode_fh(struct inode *inode, u32 *fh, int *max_len,\n+\t\tstruct inode *parent)\n+{\n+\tif (ntfs_inode_is_named_stream(NTFS_I(inode)))\n+\t\treturn -EOPNOTSUPP;\n+\treturn generic_encode_ino32_fh(inode, fh, max_len, parent);\n+}\n+\n /*\n * Export operations allowing NFS exporting of mounted NTFS partitions.\n */\n const struct export_operations ntfs_export_ops = {\n-\t.encode_fh = generic_encode_ino32_fh,\n+\t.encode_fh\t= ntfs_encode_fh,\n \t.get_parent\t= ntfs_get_parent,\t/* Find the parent of a given directory. */\n \t.fh_to_dentry\t= ntfs_fh_to_dentry,\n \t.fh_to_parent\t= ntfs_fh_to_parent,\ndiff --git a/fs/ntfs/stream.h b/fs/ntfs/stream.h\nnew file mode 100644\nindex 0000000000000..5cbf6e8f6b681\n--- /dev/null\n+++ b/fs/ntfs/stream.h\n@@ -0,0 +1,81 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+#ifndef _NTFS_STREAM_H\n+#define _NTFS_STREAM_H\n+\n+#include \u003clinux/fs.h\u003e\n+\n+struct ntfs_inode;\n+struct ntfs_volume;\n+\n+struct ntfs_stream_path {\n+\tconst char *base_name;\n+\tunsigned int base_len;\n+\tconst char *stream_name;\n+\tunsigned int stream_len;\n+};\n+\n+int ntfs_check_bad_windows_name(struct ntfs_volume *vol,\n+\t\tconst __le16 *name, unsigned int name_len);\n+int ntfs_check_stream_name(const __le16 *name, unsigned int name_len);\n+\n+int ntfs_stream_path_parse(struct ntfs_volume *vol,\n+\t\tconst struct qstr *qname, struct ntfs_stream_path *path);\n+bool ntfs_stream_path_has_colon(struct ntfs_volume *vol,\n+\t\tconst struct qstr *qname);\n+int ntfs_stream_path_check(struct ntfs_volume *vol,\n+\t\tconst struct qstr *qname);\n+int ntfs_stream_path_names(struct ntfs_volume *vol,\n+\t\tconst struct qstr *qname, __le16 **base_name, int *base_len,\n+\t\t__le16 **stream_name, int *stream_len);\n+\n+struct inode *ntfs_stream_lookup_inode_by_name(struct inode *dir,\n+\t\t__le16 *name, int name_len);\n+struct dentry *ntfs_lookup_stream(struct inode *dir, struct dentry *dentry,\n+\t\tconst struct ntfs_stream_path *path);\n+struct inode *ntfs_create_named_stream(struct mnt_idmap *idmap,\n+\t\tstruct inode *dir, __le16 *base_name, int base_len,\n+\t\t__le16 *stream_name, int stream_len);\n+int ntfs_unlink_named_stream(struct inode *dir, struct dentry *dentry);\n+\n+void ntfs_set_default_dentry_ops(struct super_block *sb);\n+\n+void ntfs_stream_inode_refresh(struct inode *inode);\n+int ntfs_stream_inode_validate(struct inode *inode);\n+void ntfs_stream_update_base_time(struct ntfs_inode *base_ni);\n+bool ntfs_stream_unlinked(struct ntfs_inode *base_ni,\n+\t\tconst __le16 *name, u32 name_len);\n+int ntfs_stream_put(struct inode *inode);\n+int ntfs_remove_named_stream(struct ntfs_inode *ni, __le16 *name,\n+\t\tu32 name_len, struct inode *expected_inode);\n+\n+int ntfs_stream_validate_for_mutation(struct inode *inode, bool nowait);\n+long ntfs_ioctl_stream_read(struct file *file, unsigned long arg);\n+long ntfs_ioctl_stream_write(struct file *file, unsigned long arg);\n+long ntfs_ioctl_stream_remove(struct file *file, unsigned long arg);\n+int ntfs_ioctl_list_streams(struct file *file, unsigned long arg);\n+\n+extern const struct file_operations ntfs_stream_file_ops;\n+extern const struct inode_operations ntfs_stream_inode_ops;\n+\n+/*\n+ * Common file operations used by both ordinary files and named streams.\n+ * Their implementations remain in file.c.\n+ */\n+int ntfs_file_open(struct inode *inode, struct file *file);\n+int ntfs_file_release(struct inode *inode, struct file *file);\n+int ntfs_file_fsync(struct file *file, loff_t start, loff_t end,\n+\t\tint datasync);\n+int ntfs_setattr_size(struct inode *inode, struct iattr *attr);\n+loff_t ntfs_file_llseek(struct file *file, loff_t offset, int whence);\n+ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to);\n+ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from);\n+int ntfs_file_mmap_prepare(struct vm_area_desc *desc);\n+ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos,\n+\t\tstruct pipe_inode_info *pipe, size_t len, unsigned int flags);\n+int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,\n+\t\tu64 start, u64 len);\n+long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len);\n+\n+int ntfs_test_inode_rcu(struct inode *inode, void *data);\n+\n+#endif /* _NTFS_STREAM_H */\ndiff --git a/fs/ntfs/super.c b/fs/ntfs/super.c\nindex d174867b9f5f2..242fb19ced2b4 100644\n--- a/fs/ntfs/super.c\n+++ b/fs/ntfs/super.c\n@@ -23,6 +23,7 @@\n #include \"ea.h\"\n #include \"bitmap.h\"\n #include \"volume.h\"\n+#include \"stream.h\"\n \n /* A global default upcase table and a corresponding reference count. */\n static __le16 *default_upcase;\n@@ -67,6 +68,17 @@ static const struct constant_table ntfs_symlink_enums[] = {\n \t{}\n };\n \n+enum {\n+\tSTREAMS_INTERFACE_NONE,\n+\tSTREAMS_INTERFACE_WINDOWS,\n+};\n+\n+static const struct constant_table ntfs_streams_interface_enums[] = {\n+\t{ \"none\",\tSTREAMS_INTERFACE_NONE },\n+\t{ \"windows\",\tSTREAMS_INTERFACE_WINDOWS },\n+\t{}\n+};\n+\n enum {\n \tOpt_uid,\n \tOpt_gid,\n@@ -92,6 +104,7 @@ enum {\n \tOpt_nocase,\n \tOpt_native_symlink,\n \tOpt_symlink,\n+\tOpt_streams_interface,\n };\n \n static const struct fs_parameter_spec ntfs_parameters[] = {\n@@ -119,6 +132,8 @@ static const struct fs_parameter_spec ntfs_parameters[] = {\n \tfsparam_flag(\"nocase\",\t\t\tOpt_nocase),\n \tfsparam_enum(\"native_symlink\",\t\tOpt_native_symlink, ntfs_native_symlink_enums),\n \tfsparam_enum(\"symlink\",\t\t\tOpt_symlink, ntfs_symlink_enums),\n+\tfsparam_enum(\"streams_interface\",\tOpt_streams_interface,\n+\t\t ntfs_streams_interface_enums),\n \t{}\n };\n \n@@ -255,6 +270,22 @@ static int ntfs_parse_param(struct fs_context *fc, struct fs_parameter *param)\n \t\telse\n \t\t\tNVolClearSymlinkNative(vol);\n \t\tbreak;\n+\tcase Opt_streams_interface: {\n+\t\tbool streams_windows =\n+\t\t\tresult.uint_32 == STREAMS_INTERFACE_WINDOWS;\n+\n+\t\tif (fc-\u003epurpose == FS_CONTEXT_FOR_RECONFIGURE \u0026\u0026\n+\t\t NVolStreamsWindows(vol) != streams_windows) {\n+\t\t\tntfs_error(vol-\u003esb,\n+\t\t\t\t \"Cannot change streams_interface on remount.\");\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\t\tif (streams_windows)\n+\t\t\tNVolSetStreamsWindows(vol);\n+\t\telse\n+\t\t\tNVolClearStreamsWindows(vol);\n+\t\tbreak;\n+\t}\n \tcase Opt_sparse:\n \t\tbreak;\n \tdefault:\n@@ -2725,6 +2756,8 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)\n \t * operations and associated address space operations to function.\n \t */\n \tsb-\u003es_op = \u0026ntfs_sops;\n+\tif (NVolStreamsWindows(vol))\n+\t\tntfs_set_default_dentry_ops(sb);\n \ttmp_ino = new_inode(sb);\n \tif (!tmp_ino) {\n \t\tif (!silent)\ndiff --git a/fs/ntfs/volume.h b/fs/ntfs/volume.h\nindex 6d9d6eb79a7c1..ed5c0ccb9cd9f 100644\n--- a/fs/ntfs/volume.h\n+++ b/fs/ntfs/volume.h\n@@ -197,6 +197,7 @@ struct ntfs_volume {\n * NV_DisableSparse\t\tDisable creation of sparse regions.\n * NV_NativeSymlinkRel\t\tTranslate absolute Windows reparse targets (native_symlink=rel).\n * NV_MftBootstrap\t\tMount is still assembling $MFT's own runlist.\n+ * NV_StreamsWindows\t\tInterpret the final path component as file:stream.\n */\n enum {\n \tNV_Errors,\n@@ -218,6 +219,7 @@ enum {\n \tNV_NativeSymlinkRel,\n \tNV_SymlinkNative,\n \tNV_MftBootstrap,\n+\tNV_StreamsWindows,\n };\n \n /*\n@@ -258,6 +260,7 @@ DEFINE_NVOL_BIT_OPS(DisableSparse)\n DEFINE_NVOL_BIT_OPS(NativeSymlinkRel)\n DEFINE_NVOL_BIT_OPS(SymlinkNative)\n DEFINE_NVOL_BIT_OPS(MftBootstrap)\n+DEFINE_NVOL_BIT_OPS(StreamsWindows)\n \n static inline void ntfs_inc_free_clusters(struct ntfs_volume *vol, s64 nr)\n {\ndiff --git a/fs/ntfs/wof.c b/fs/ntfs/wof.c\nindex 9847259e5b1a2..294dbe5d711f7 100644\n--- a/fs/ntfs/wof.c\n+++ b/fs/ntfs/wof.c\n@@ -311,7 +311,8 @@ static int parse_wof_chunk_table(struct ntfs_inode *base_ni,\n \t\t\tgoto out_unlock_mrec;\n \t\t}\n \t\tret = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len,\n-\t\t\t\t CASE_SENSITIVE, 0, NULL, 0, ctx);\n+\t\t\t\t CASE_SENSITIVE,\n+\t\t\t\t 0, NULL, 0, ctx);\n \t\tif (ret)\n \t\t\tgoto out_put_ctx;\n \n@@ -399,7 +400,8 @@ static int ntfs_read_wof_chunk(struct ntfs_volume *vol,\n \t}\n \n \terr = ntfs_attr_lookup(wof_ni-\u003etype, wof_ni-\u003ename, wof_ni-\u003ename_len,\n-\t\t\t CASE_SENSITIVE, 0, NULL, 0, ctx);\n+\t\t\t CASE_SENSITIVE,\n+\t\t\t 0, NULL, 0, ctx);\n \tif (err)\n \t\tgoto out_put_ctx;\n \ndiff --git a/include/uapi/linux/ntfs.h b/include/uapi/linux/ntfs.h\nnew file mode 100644\nindex 0000000000000..245133352132b\n--- /dev/null\n+++ b/include/uapi/linux/ntfs.h\n@@ -0,0 +1,126 @@\n+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */\n+/*\n+ * Copyright (c) 2026 LG Electronics Co., Ltd.\n+ */\n+\n+#ifndef _UAPI_LINUX_NTFS_H\n+#define _UAPI_LINUX_NTFS_H\n+#include \u003clinux/types.h\u003e\n+#include \u003clinux/ioctl.h\u003e\n+\n+#define NTFS_IOC_MAGIC\t0xEF\n+\n+/*\n+ * Flags for ntfs_stream.flags and ntfs_list_streams.flags.\n+ *\n+ * NTFS_STREAM_FL_UTF16 makes stream names raw UTF-16LE, exactly as stored on\n+ * disk, instead of encoding them with the mounted filesystem NLS. This\n+ * allows lossless round-tripping of stream names whose characters are not\n+ * representable by the mount NLS (e.g. for Wine, which works in UTF-16\n+ * natively). When set, the name length fields count bytes of UTF-16LE and\n+ * must therefore be even.\n+ */\n+#define NTFS_STREAM_FL_UTF16\t\t0x1\n+\n+/*\n+ * ntfs named stream read, write, and remove ioctl structure.\n+ *\n+ * @stream_offset:\tOffset within the named stream for read/write.\n+ * @io_len:\t\tNumber of bytes to read/write. Must be zero for remove.\n+ * @bytes_returned:\tActual bytes transferred (out).\n+ * @name_len:\t\tStream name length in bytes, not including any\n+ *\t\t\tterminating NUL. When NTFS_STREAM_FL_UTF16 is set,\n+ *\t\t\tthis counts UTF-16LE bytes and must be even.\n+ * @flags:\t\tBit mask of NTFS_STREAM_FL_* flags. Other bits must\n+ *\t\t\tbe zero.\n+ * @reserved:\t\tMust be zero.\n+ * @buffer:\t\tBare stream name followed by stream data for read/write.\n+ *\n+ * The stream name is encoded with the mounted filesystem NLS, or as raw\n+ * UTF-16LE when NTFS_STREAM_FL_UTF16 is set. A write creates the stream if\n+ * it does not already exist. A write failure after creation may leave the\n+ * new stream behind; it can be removed separately.\n+ *\n+ * For NTFS_IOC_STREAM_READ, a successful short read indicates EOF. If a read\n+ * fails, the ioctl returns an error and copies no stream data to userspace.\n+ */\n+struct ntfs_stream {\n+\t__aligned_u64 stream_offset;\n+\t__aligned_u64 io_len;\n+\t__aligned_u64 bytes_returned;\n+\t__u32 name_len;\n+\t__u32 flags;\n+\t__aligned_u64 reserved;\n+\t__u8 buffer[];\n+};\n+\n+/*\n+ * Single stream entry returned by NTFS_IOC_LIST_STREAMS.\n+ *\n+ * @next_entry_off:\tByte offset from the start of this entry to the next\n+ *\t\t\tentry, or zero for the last entry. Always a multiple\n+ *\t\t\tof 8. Consumers must use this to advance instead of\n+ *\t\t\tcomputing the stride themselves.\n+ * @size:\t\tStream data size in bytes.\n+ * @alloc_size:\t\tBytes allocated for the stream (cluster aligned for\n+ *\t\t\tnon-resident streams).\n+ * @name_len:\t\tStream name length in bytes, not including a NUL\n+ *\t\t\tterminator (none is stored). Counts UTF-16LE bytes\n+ *\t\t\twhen NTFS_STREAM_FL_UTF16 was requested.\n+ * @name_offset:\tByte offset from the start of this entry to @name.\n+ * @reserved:\t\tMust be zero.\n+ * @name:\t\tBare stream name; NLS encoded, or raw UTF-16LE when\n+ *\t\t\tNTFS_STREAM_FL_UTF16 was requested.\n+ *\n+ * New fixed fields may be added after @reserved and before @name. Consumers\n+ * must use @name_offset to locate the name and @next_entry_off to advance to\n+ * the next entry.\n+ */\n+struct ntfs_stream_entry {\n+\t__aligned_u64 next_entry_off;\n+\t__aligned_u64 size;\n+\t__aligned_u64 alloc_size;\n+\t__u32 name_len;\n+\t__u32 name_offset;\n+\t__u32 reserved;\n+\t__u8 name[];\n+};\n+\n+/*\n+ * ntfs list streams ioctl structure.\n+ *\n+ * @buffer_size:\tuser buffer size(in).\n+ * @bytes_returned:\tactual bytes written or required(out).\n+ * @stream_count:\tnumber of streams(out).\n+ * @flags:\t\tBit mask of NTFS_STREAM_FL_* flags controlling the\n+ *\t\t\tencoding of the returned names; other bits must be zero.\n+ * @reserved:\t\tMust be zero.\n+ * @buffer:\t\tntfs_stream_entry array.\n+ *\n+ * The variable-length entries follow the header in @buffer, each aligned on\n+ * an 8-byte boundary and chained via ntfs_stream_entry.next_entry_off. If\n+ * @buffer_size is too small, no data is copied, @stream_count and\n+ * @bytes_returned report the required values and the ioctl fails with\n+ * -ENOSPC. In NLS mode, names that cannot be represented by the mounted\n+ * character set are omitted; use NTFS_STREAM_FL_UTF16 to list all\n+ * names without conversion loss.\n+ */\n+struct ntfs_list_streams {\n+\t__aligned_u64 buffer_size;\n+\t__aligned_u64 bytes_returned;\n+\t__aligned_u64 stream_count;\n+\t__u32 flags;\n+\t__u32 reserved;\n+\t__u8 buffer[];\n+};\n+\n+#define NTFS_IOC_STREAM_READ \\\n+\t_IOWR(NTFS_IOC_MAGIC, 1, struct ntfs_stream)\n+#define NTFS_IOC_STREAM_WRITE \\\n+\t_IOWR(NTFS_IOC_MAGIC, 2, struct ntfs_stream)\n+#define NTFS_IOC_STREAM_REMOVE \\\n+\t_IOWR(NTFS_IOC_MAGIC, 3, struct ntfs_stream)\n+#define NTFS_IOC_LIST_STREAMS \\\n+\t_IOWR(NTFS_IOC_MAGIC, 4, struct ntfs_list_streams)\n+\n+#endif /* _UAPI_LINUX_NTFS_H */\n"
} |
| 2/1 | 2026/10/11 22:43 | agent | patch-evaluator |
10mModel:core Args: null Results: {
"EnableConfigs": [
"NTFS_FS",
"NTFS_FS_POSIX_ACL"
],
"FocusSymbols": [
"ntfs_ioctl_stream_read",
"ntfs_ioctl_stream_write",
"ntfs_ioctl_stream_remove",
"ntfs_ioctl_list_streams",
"ntfs_create_named_stream",
"ntfs_unlink_named_stream",
"ntfs_lookup_stream",
"ntfs_remove_named_stream",
"ntfs_stream_inode_validate",
"ntfs_stream_put",
"ntfs_inode_attr_pread",
"ntfs_inode_attr_pwrite"
],
"Reasoning": "The patch introduces support for NTFS named data streams ($DATA attributes), including a new ioctl interface (NTFS_IOC_STREAM_READ, NTFS_IOC_STREAM_WRITE, NTFS_IOC_STREAM_REMOVE, NTFS_IOC_LIST_STREAMS) and pathname-based stream access (file:stream format enabled via streams_interface=windows). It implements stream creation, lookup, unlink, validation, concurrent locking/lifetime management, and attribute reading/writing. This complex filesystem feature is fully reachable in virtualized environments via loopback-mounted NTFS images and user syscalls/ioctls, making it a prime candidate for syzkaller fuzzing.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.
IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.
================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.
- REACHABILITY IS THE PRIMARY GATE:
Fuzzing can only discover bugs in code that can actually execute in standard virtualized
environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
regardless of whether it adds assertions or complex logic.
- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
Patch authors routinely label changes as "cleanups", "refactorings", or state
"No functional change intended". Do NOT take these claims at face value.
Code refactorings that rearrange logic, introduce helper functions, or alter state management
in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
If reachable executable code is modified or refactored, it MUST be fuzzed.
- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
Even if the author believes the invariant always holds, fuzzing is essential to verify whether
an unusual sequence of operations can violate it.
================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:
- Non-kernel and non-executable changes:
* Modifications to Documentation/, comments, or spelling fixes.
* User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
that do not affect the compiled kernel image (vmlinux) or kernel modules.
* Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
that do not alter control flow or data structures.
* Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
* Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
* Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
physical PCI hot-unplug or manual sysfs driver unbinding.
* Code paths exclusive to architectures other than the target architecture.
================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
* Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
* Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
* Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
* Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).
================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:
- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
Do NOT list generic, ubiquitous functions called by almost every program in the corpus
(including, but not limited to: general memory allocators and deallocators, page fault
and trap handlers, or core synchronization primitives; this is not an exhaustive list).
Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
which severely dilutes fuzzing effort away from the actual changes.
- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
List functions that specifically implement the logic being added or altered, or direct API entrypoints
for the subsystem feature under review.
- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).
================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 31714ee070cc940f303c3a87e717a5aae8856a8f
Author: syz-cluster <triage@syzkaller.com>
Date: Sun Oct 11 22:43:11 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/filesystems/ntfs.rst b/Documentation/filesystems/ntfs.rst
index 4bfa392daec6a..ec329bb142a06 100644
--- a/Documentation/filesystems/ntfs.rst
+++ b/Documentation/filesystems/ntfs.rst
@@ -169,4 +169,38 @@ symlink=wsl|native Configure how symbolic links are created. Under
Linux) compatible symlinks are created. Under
"native", Windows native symbolic links are
created.
+
+streams_interface= Select the named-stream pathname interface:
+ "none" (default) treats ':' as an ordinary filename
+ character, while "windows" interprets the final
+ component of a path such as "file:stream" as a named
+ $DATA stream. In "windows" mode, filenames containing
+ ':' are hidden from directory listings and cannot be
+ addressed through the pathname interface. Only the
+ file:stream form is recognized. Typed forms such
+ as file::$DATA and file:stream:$DATA are not
+ supported. The ioctl interface is available in both
+ modes. The base file or directory must already exist
+ before a pathname stream can be created. Creating
+ file:stream does not create file. Removing a stream
+ while it is open fails with -EBUSY. Removing the base
+ file while a stream is open detaches the stream from
+ the namespace; NTFS has no orphan-stream list, so a
+ crash can leave that stream's data unreachable on
+ disk.
======================= ====================================================
+
+Named-stream ioctls
+===================
+
+The named-stream ioctl interface is available regardless of
+streams_interface. Open the base regular file or directory and use
+NTFS_IOC_STREAM_READ and NTFS_IOC_STREAM_WRITE with a
+struct ntfs_stream request. The request contains the stream name,
+byte offset, and transfer length. Write requests create the stream when it
+does not exist. A failed write can leave a newly created empty stream; use
+NTFS_IOC_STREAM_REMOVE to remove it. This ioctl removes streams, and
+NTFS_IOC_LIST_STREAMS enumerates streams. Stream names are encoded with
+the mounted NLS by default. Set NTFS_STREAM_FL_UTF16 to pass or receive
+raw UTF-16LE names. These ioctls transfer stream data directly and do not
+return a separate stream file descriptor.
diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documentation/userspace-api/ioctl/ioctl-number.rst
index 2fc53093752d1..94a421970b850 100644
--- a/Documentation/userspace-api/ioctl/ioctl-number.rst
+++ b/Documentation/userspace-api/ioctl/ioctl-number.rst
@@ -401,6 +401,7 @@ Code Seq# Include File Comments
0xE5 00-3F linux/fuse.h
0xEC 00-01 drivers/platform/chrome/cros_ec_dev.h ChromeOS EC driver
0xEE 00-09 uapi/linux/pfrut.h Platform Firmware Runtime Update and Telemetry
+0xEF 00-0F uapi/linux/ntfs.h NTFS
0xF3 00-3F drivers/usb/misc/sisusbvga/sisusb.h sisfb (in development)
<mailto:thomas@winischhofer.net>
0xF6 all LTTng Linux Trace Toolkit Next Generation
diff --git a/MAINTAINERS b/MAINTAINERS
index cc3cae2e378b3..b035f7ad34f05 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -19489,6 +19489,7 @@ S: Maintained
T: git git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/ntfs.git
F: Documentation/filesystems/ntfs.rst
F: fs/ntfs/
+F: include/uapi/linux/ntfs.h
NTFS3 FILESYSTEM
M: Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
diff --git a/fs/ntfs/Makefile b/fs/ntfs/Makefile
index ee8987e496a87..3e85495776536 100644
--- a/fs/ntfs/Makefile
+++ b/fs/ntfs/Makefile
@@ -5,7 +5,7 @@ obj-$(CONFIG_NTFS_FS) += ntfs.o
ntfs-y := aops.o attrib.o collate.o dir.o file.o index.o inode.o \
mft.o mst.o namei.o runlist.o super.o unistr.o attrlist.o ea.o \
upcase.o bitmap.o lcnalloc.o logfile.o reparse.o compress.o \
- iomap.o debug.o sysctl.o object_id.o bdev-io.o
+ iomap.o debug.o sysctl.o object_id.o bdev-io.o named_stream.o
ntfs-$(CONFIG_NTFS_FS_WOF_COMPRESSION) += wof.o \
lib/decompress_common.o lib/lzx_decompress.o lib/xpress_decompress.o
diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index 07414a20b7de9..fce7144d7bef6 100644
--- a/fs/ntfs/attrib.c
+++ b/fs/ntfs/attrib.c
@@ -2352,12 +2352,13 @@ int ntfs_attr_set(struct ntfs_inode *ni, s64 ofs, s64 cnt, const u8 val)
int ntfs_attr_set_initialized_size(struct ntfs_inode *ni, loff_t new_size)
{
struct ntfs_attr_search_ctx *ctx;
+ struct ntfs_inode *base_ni = ntfs_base_inode(ni);
int err = 0;
if (!NInoNonResident(ni))
return -EINVAL;
- ctx = ntfs_attr_get_search_ctx(ni, NULL);
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx)
return -ENOMEM;
@@ -2439,6 +2440,7 @@ int ntfs_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
struct mft_record *m;
int err, offset;
struct ntfs_inode *base_ni;
+ u32 ic;
if (!ni || (!name && name_len))
return -EINVAL;
@@ -2468,7 +2470,8 @@ int ntfs_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
* attribute in @ni->mrec, not any extent inode in case if @ni is base
* file record.
*/
- err = ntfs_attr_find(type, name, name_len, CASE_SENSITIVE, val, size, ctx);
+ ic = type == AT_DATA && name_len ? IGNORE_CASE : CASE_SENSITIVE;
+ err = ntfs_attr_find(type, name, name_len, ic, val, size, ctx);
if (!err) {
err = -EEXIST;
ntfs_debug("Attribute already present.\n");
@@ -2560,6 +2563,7 @@ static int ntfs_non_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
struct mft_record *m;
struct ntfs_inode *base_ni;
int err, offset;
+ u32 ic;
if (!ni || dataruns_size <= 0 || (!name && name_len))
return -EINVAL;
@@ -2589,7 +2593,8 @@ static int ntfs_non_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
* attribute in @ni->mrec, not any extent inode in case if @ni is base
* file record.
*/
- err = ntfs_attr_find(type, name, name_len, CASE_SENSITIVE, NULL, 0, ctx);
+ ic = type == AT_DATA && name_len ? IGNORE_CASE : CASE_SENSITIVE;
+ err = ntfs_attr_find(type, name, name_len, ic, NULL, 0, ctx);
if (!err) {
err = -EEXIST;
pr_err("Attribute 0x%x already present\n", type);
@@ -2663,7 +2668,7 @@ static int ntfs_non_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
* update of attribute list.
*/
ntfs_attr_reinit_search_ctx(ctx);
- err = ntfs_attr_lookup(type, name, name_len, CASE_SENSITIVE,
+ err = ntfs_attr_lookup(type, name, name_len, ic,
lowest_vcn, NULL, 0, ctx);
if (err) {
pr_err("%s: attribute lookup failed\n", __func__);
@@ -3132,6 +3137,7 @@ int ntfs_attr_open(struct ntfs_inode *ni, const __le32 type,
struct attr_record *a;
bool cs;
struct ntfs_inode *base_ni;
+ u32 ic;
int err;
if (!ni || !ni->vol)
@@ -3140,10 +3146,7 @@ int ntfs_attr_open(struct ntfs_inode *ni, const __le32 type,
ntfs_debug("Entering for inode %lld, attr 0x%x.\n",
ni->mft_no, type);
- if (NInoAttr(ni))
- base_ni = ni->ext.base_ntfs_ino;
- else
- base_ni = ni;
+ base_ni = ntfs_base_inode(ni);
if (name && name != AT_UNNAMED && name != I30) {
name = ntfs_ucsndup(name, name_len);
@@ -3161,7 +3164,8 @@ int ntfs_attr_open(struct ntfs_inode *ni, const __le32 type,
goto err_out;
}
- err = ntfs_attr_lookup(type, name, name_len, 0, 0, NULL, 0, ctx);
+ ic = type == AT_DATA && name_len ? IGNORE_CASE : CASE_SENSITIVE;
+ err = ntfs_attr_lookup(type, name, name_len, ic, 0, NULL, 0, ctx);
if (err)
goto put_err_out;
@@ -3346,7 +3350,8 @@ int ntfs_attr_map_whole_runlist(struct ntfs_inode *ni)
not_mapped = 1;
err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
- CASE_SENSITIVE, next_vcn, NULL, 0, ctx);
+ CASE_SENSITIVE, next_vcn,
+ NULL, 0, ctx);
if (err)
break;
@@ -3431,6 +3436,7 @@ int ntfs_attr_record_move_to(struct ntfs_attr_search_ctx *ctx, struct ntfs_inode
struct ntfs_attr_search_ctx *nctx;
struct attr_record *a;
int err;
+ u32 ic;
struct mft_record *ni_mrec;
struct super_block *sb;
@@ -3466,9 +3472,11 @@ int ntfs_attr_record_move_to(struct ntfs_attr_search_ctx *ctx, struct ntfs_inode
* attribute in @ni->mrec, not any extent inode in case if @ni is base
* file record.
*/
- err = ntfs_attr_find(a->type, (__le16 *)((u8 *)a + le16_to_cpu(a->name_offset)),
- a->name_length, CASE_SENSITIVE, NULL,
- 0, nctx);
+ ic = a->type == AT_DATA && a->name_length ?
+ IGNORE_CASE : CASE_SENSITIVE;
+ err = ntfs_attr_find(a->type,
+ (__le16 *)((u8 *)a + le16_to_cpu(a->name_offset)),
+ a->name_length, ic, NULL, 0, nctx);
if (!err) {
ntfs_debug("Attribute of such type, with same name already present in this MFT record.\n");
err = -EEXIST;
@@ -3800,7 +3808,8 @@ static int __ntfs_attr_update_mapping_pairs(struct ntfs_inode *ni,
finished_build = false;
start_rl = ni->runlist.rl;
while (!(err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
- CASE_SENSITIVE, from_vcn, NULL, 0, ctx))) {
+ CASE_SENSITIVE, from_vcn, NULL,
+ 0, ctx))) {
unsigned int de_cnt = 0;
a = ctx->attr;
@@ -4011,7 +4020,8 @@ static int __ntfs_attr_update_mapping_pairs(struct ntfs_inode *ni,
ntfs_attr_reinit_search_ctx(ctx);
ntfs_debug("Deallocate marked extents.\n");
while (!(err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
- CASE_SENSITIVE, 0, NULL, 0, ctx))) {
+ CASE_SENSITIVE, 0, NULL, 0,
+ ctx))) {
if (le64_to_cpu(ctx->attr->data.non_resident.highest_vcn) !=
NTFS_VCN_DELETE_MARK)
continue;
@@ -4345,7 +4355,7 @@ static int ntfs_non_resident_attr_shrink(struct ntfs_inode *ni,
goto unlock_runlist;
}
- ctx = ntfs_attr_get_search_ctx(ni, NULL);
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ntfs_error(vol->sb, "%s: Failed to get search context", __func__);
err = -ENOMEM;
@@ -4404,8 +4414,8 @@ static int ntfs_non_resident_attr_shrink(struct ntfs_inode *ni,
return -ENOMEM;
}
- err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE,
- 0, NULL, 0, ctx);
+ err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE, 0, NULL, 0, ctx);
if (err) {
if (err == -ENOENT)
err = -EIO;
@@ -4724,8 +4734,8 @@ static int ntfs_non_resident_attr_expand(struct ntfs_inode *ni, const s64 newsiz
goto rollback;
}
- err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE,
- 0, NULL, 0, ctx);
+ err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE, 0, NULL, 0, ctx);
if (err) {
if (err == -ENOENT)
err = -EIO;
@@ -4837,7 +4847,7 @@ static int ntfs_resident_attr_resize(struct ntfs_inode *attr_ni, const s64 newsi
}
err = ntfs_attr_lookup(attr_ni->type, attr_ni->name, attr_ni->name_len,
- 0, 0, NULL, 0, ctx);
+ CASE_SENSITIVE, 0, NULL, 0, ctx);
if (err) {
ntfs_error(sb, "ntfs_attr_lookup failed");
goto put_err_out;
@@ -5213,6 +5223,7 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,
s64 *lcn_count, s64 max_clu_count, bool *balloc, bool update_mp,
bool skip_holes)
{
+ struct ntfs_inode *base_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct ntfs_attr_search_ctx *ctx;
struct runlist_element *rl, *rlc;
@@ -5226,10 +5237,7 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,
if (err)
return err;
- if (NInoAttr(ni))
- ctx = ntfs_attr_get_search_ctx(ni->ext.base_ntfs_ino, NULL);
- else
- ctx = ntfs_attr_get_search_ctx(ni, NULL);
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ntfs_error(vol->sb, "%s: Failed to get search context", __func__);
return -ENOMEM;
@@ -5379,7 +5387,7 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,
} else {
VFS_I(ni)->i_blocks += clu_count << (vol->cluster_size_bits - 9);
NInoSetRunlistDirty(ni);
- mark_mft_record_dirty(ni);
+ mark_mft_record_dirty(base_ni);
}
*lcn_start = lcn;
@@ -5405,10 +5413,7 @@ int ntfs_attr_rm(struct ntfs_inode *ni)
struct ntfs_inode *base_ni;
struct super_block *sb = ni->vol->sb;
- if (NInoAttr(ni))
- base_ni = ni->ext.base_ntfs_ino;
- else
- base_ni = ni;
+ base_ni = ntfs_base_inode(ni);
ntfs_debug("Entering for inode 0x%llx, attr 0x%x.\n",
(long long) ni->mft_no, ni->type);
@@ -5420,7 +5425,7 @@ int ntfs_attr_rm(struct ntfs_inode *ni)
err = ntfs_attr_map_whole_runlist(ni);
if (err)
return err;
- ctx = ntfs_attr_get_search_ctx(ni, NULL);
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ntfs_error(sb, "%s: Failed to get search context", __func__);
return -ENOMEM;
@@ -5462,6 +5467,7 @@ int ntfs_attr_exist(struct ntfs_inode *ni, const __le32 type, __le16 *name,
u32 name_len)
{
struct ntfs_attr_search_ctx *ctx;
+ u32 ic;
int ret;
ntfs_debug("Entering\n");
@@ -5473,39 +5479,54 @@ int ntfs_attr_exist(struct ntfs_inode *ni, const __le32 type, __le16 *name,
return 0;
}
- ret = ntfs_attr_lookup(type, name, name_len, CASE_SENSITIVE,
+ ic = type == AT_DATA && name_len ? IGNORE_CASE : CASE_SENSITIVE;
+ ret = ntfs_attr_lookup(type, name, name_len, ic,
0, NULL, 0, ctx);
ntfs_attr_put_search_ctx(ctx);
return !ret;
}
+/*
+ * If @attr_vi is non-NULL, the attribute inode reference is returned to the
+ * caller, which must release it after dropping ni->mrec_lock.
+ */
int ntfs_attr_remove(struct ntfs_inode *ni, const __le32 type, __le16 *name,
- u32 name_len)
+ u32 name_len, struct inode **attr_vi)
{
int err;
- struct inode *attr_vi;
+ struct inode *vi;
struct ntfs_inode *attr_ni;
ntfs_debug("Entering\n");
+ if (attr_vi)
+ *attr_vi = NULL;
if (!ni)
return -EINVAL;
- attr_vi = ntfs_attr_iget(VFS_I(ni), type, name, name_len);
- if (IS_ERR(attr_vi)) {
- err = PTR_ERR(attr_vi);
+ vi = ntfs_attr_iget(VFS_I(ni), type, name, name_len);
+ if (IS_ERR(vi)) {
+ err = PTR_ERR(vi);
ntfs_error(ni->vol->sb, "Failed to open attribute 0x%02x of inode 0x%llx",
type, (unsigned long long)ni->mft_no);
return err;
}
- attr_ni = NTFS_I(attr_vi);
+ attr_ni = NTFS_I(vi);
err = ntfs_attr_rm(attr_ni);
- if (err)
+ if (err) {
ntfs_error(ni->vol->sb, "Failed to remove attribute 0x%02x of inode 0x%llx",
type, (unsigned long long)ni->mft_no);
- iput(attr_vi);
+ } else {
+ NInoClearDirty(attr_ni);
+ clear_nlink(vi);
+ remove_inode_hash(vi);
+ }
+ if (attr_vi)
+ *attr_vi = vi;
+ else
+ iput(vi);
return err;
}
@@ -5580,13 +5601,14 @@ void *ntfs_attr_readall(struct ntfs_inode *ni, const __le32 type,
int ntfs_non_resident_attr_insert_range(struct ntfs_inode *ni, s64 start_vcn, s64 len)
{
+ struct ntfs_inode *base_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct runlist_element *hole_rl, *rl;
struct ntfs_attr_search_ctx *ctx;
int ret;
size_t new_rl_count;
- if (NInoAttr(ni) || ni->type != AT_DATA)
+ if (ni->type != AT_DATA)
return -EOPNOTSUPP;
if (start_vcn > ntfs_bytes_to_cluster(vol, ni->allocated_size))
return -EINVAL;
@@ -5635,14 +5657,14 @@ int ntfs_non_resident_attr_insert_range(struct ntfs_inode *ni, s64 start_vcn, s6
if (ret)
return ret;
- ctx = ntfs_attr_get_search_ctx(ni, NULL);
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ret = -ENOMEM;
return ret;
}
- ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE,
- 0, NULL, 0, ctx);
+ ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE, 0, NULL, 0, ctx);
if (ret) {
ntfs_attr_put_search_ctx(ctx);
return ret;
@@ -5659,6 +5681,7 @@ int ntfs_non_resident_attr_insert_range(struct ntfs_inode *ni, s64 start_vcn, s6
int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn, s64 len)
{
+ struct ntfs_inode *base_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct runlist_element *punch_rl, *rl;
struct ntfs_attr_search_ctx *ctx = NULL;
@@ -5667,7 +5690,7 @@ int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn,
int ret;
size_t new_rl_cnt;
- if (NInoAttr(ni) || ni->type != AT_DATA)
+ if (ni->type != AT_DATA)
return -EOPNOTSUPP;
end_vcn = ntfs_bytes_to_cluster(vol, ni->allocated_size);
@@ -5723,14 +5746,14 @@ int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn,
}
up_write(&ni->runlist.lock);
- ctx = ntfs_attr_get_search_ctx(ni, NULL);
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ret = -ENOMEM;
goto out_rl;
}
- ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE,
- 0, NULL, 0, ctx);
+ ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE, 0, NULL, 0, ctx);
if (ret)
goto out_ctx;
@@ -5748,12 +5771,13 @@ int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn,
ntfs_attr_put_search_ctx(ctx);
out_rl:
kvfree(punch_rl);
- mark_mft_record_dirty(ni);
+ mark_mft_record_dirty(base_ni);
return ret;
}
int ntfs_non_resident_attr_punch_hole(struct ntfs_inode *ni, s64 start_vcn, s64 len)
{
+ struct ntfs_inode *base_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct runlist_element *punch_rl, *rl;
s64 end_vcn;
@@ -5761,7 +5785,7 @@ int ntfs_non_resident_attr_punch_hole(struct ntfs_inode *ni, s64 start_vcn, s64
int ret;
size_t new_rl_count;
- if (NInoAttr(ni) || ni->type != AT_DATA)
+ if (ni->type != AT_DATA)
return -EOPNOTSUPP;
end_vcn = ntfs_bytes_to_cluster(vol, ni->allocated_size);
@@ -5805,12 +5829,13 @@ int ntfs_non_resident_attr_punch_hole(struct ntfs_inode *ni, s64 start_vcn, s64
ntfs_error(vol->sb, "Freeing of clusters failed");
kvfree(punch_rl);
- mark_mft_record_dirty(ni);
+ mark_mft_record_dirty(base_ni);
return ret;
}
int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bool keep_size)
{
+ struct ntfs_inode *base_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct mft_record *mrec;
struct ntfs_attr_search_ctx *ctx;
@@ -5822,7 +5847,7 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
struct runlist_element *rl;
bool balloc;
- if (NInoAttr(ni) || ni->type != AT_DATA)
+ if (ni->type != AT_DATA)
return -EINVAL;
if (NInoNonResident(ni) && !NInoFullyMapped(ni)) {
@@ -5833,20 +5858,21 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
return err;
}
- mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
- mrec = map_mft_record(ni);
+ mutex_lock_nested(&base_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
+ mrec = map_mft_record(base_ni);
if (IS_ERR(mrec)) {
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&base_ni->mrec_lock);
return PTR_ERR(mrec);
}
- ctx = ntfs_attr_get_search_ctx(ni, mrec);
+ ctx = ntfs_attr_get_search_ctx(base_ni, mrec);
if (!ctx) {
err = -ENOMEM;
goto out_unmap;
}
- err = ntfs_attr_lookup(AT_DATA, AT_UNNAMED, 0, 0, 0, NULL, 0, ctx);
+ err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE, 0, NULL, 0, ctx);
if (err) {
err = -EIO;
goto out_unmap;
@@ -5859,25 +5885,28 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
goto out_unmap;
if (keep_size) {
ntfs_attr_reinit_search_ctx(ctx);
- err = ntfs_attr_lookup(AT_DATA, AT_UNNAMED, 0, 0, 0, NULL, 0, ctx);
+ err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE, 0, NULL, 0,
+ ctx);
if (err) {
err = -EIO;
goto out_unmap;
}
ni->data_size = old_data_size;
+ i_size_write(VFS_I(ni), old_data_size);
if (NInoNonResident(ni))
ctx->attr->data.non_resident.data_size =
cpu_to_le64(old_data_size);
else
ctx->attr->data.resident.value_length =
cpu_to_le32((u32)old_data_size);
- mark_mft_record_dirty(ni);
+ mark_mft_record_dirty(base_ni);
}
}
ntfs_attr_put_search_ctx(ctx);
- unmap_mft_record(ni);
- mutex_unlock(&ni->mrec_lock);
+ unmap_mft_record(base_ni);
+ mutex_unlock(&base_ni->mrec_lock);
if (!NInoNonResident(ni))
goto out;
@@ -5919,12 +5948,13 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
}
while (try_alloc_cnt > 0) {
- mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
+ mutex_lock_nested(&base_ni->mrec_lock,
+ NTFS_INODE_MUTEX_NORMAL);
down_write(&ni->runlist.lock);
err = ntfs_attr_map_cluster(ni, vcn, &lcn, &alloc_cnt,
try_alloc_cnt, &balloc, false, false);
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&base_ni->mrec_lock);
if (err)
goto out;
@@ -5952,12 +5982,12 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
/* allocate clusters outside of initialized_size */
try_alloc_cnt = vcn_end - vcn;
while (try_alloc_cnt > 0) {
- mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
+ mutex_lock_nested(&base_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
down_write(&ni->runlist.lock);
err = ntfs_attr_map_cluster(ni, vcn, &lcn, &alloc_cnt,
try_alloc_cnt, &balloc, false, false);
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&base_ni->mrec_lock);
if (err || fatal_signal_pending(current))
goto signal_out;
@@ -5967,7 +5997,7 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
}
if (NInoRunlistDirty(ni)) {
- mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
+ mutex_lock_nested(&base_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
down_write(&ni->runlist.lock);
err = ntfs_attr_update_mapping_pairs_locked(ni, 0, ni);
if (err)
@@ -5975,14 +6005,14 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
else
NInoClearRunlistDirty(ni);
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&base_ni->mrec_lock);
}
return err;
out_unmap:
if (ctx)
ntfs_attr_put_search_ctx(ctx);
- unmap_mft_record(ni);
- mutex_unlock(&ni->mrec_lock);
+ unmap_mft_record(base_ni);
+ mutex_unlock(&base_ni->mrec_lock);
out:
return err >= 0 ? 0 : err;
signal_out:
diff --git a/fs/ntfs/attrib.h b/fs/ntfs/attrib.h
index 6b4fa9f576401..bee91c9f6f818 100644
--- a/fs/ntfs/attrib.h
+++ b/fs/ntfs/attrib.h
@@ -124,7 +124,7 @@ int ntfs_attr_rm(struct ntfs_inode *ni);
int ntfs_attr_exist(struct ntfs_inode *ni, const __le32 type, __le16 *name,
u32 name_len);
int ntfs_attr_remove(struct ntfs_inode *ni, const __le32 type, __le16 *name,
- u32 name_len);
+ u32 name_len, struct inode **attr_vi);
int ntfs_attr_record_rm(struct ntfs_attr_search_ctx *ctx);
int ntfs_attr_record_move_to(struct ntfs_attr_search_ctx *ctx, struct ntfs_inode *ni);
int ntfs_attr_add(struct ntfs_inode *ni, __le32 type,
diff --git a/fs/ntfs/attrlist.c b/fs/ntfs/attrlist.c
index 6edd5d2d9bf24..8291c4c95b370 100644
--- a/fs/ntfs/attrlist.c
+++ b/fs/ntfs/attrlist.c
@@ -374,7 +374,8 @@ int ntfs_attrlist_entry_add(struct ntfs_inode *ni, struct attr_record *attr)
err = ntfs_attr_lookup(attr->type, (attr->name_length) ? (__le16 *)
((u8 *)attr + le16_to_cpu(attr->name_offset)) :
- AT_UNNAMED, attr->name_length, CASE_SENSITIVE,
+ AT_UNNAMED, attr->name_length,
+ CASE_SENSITIVE,
le64_to_cpu(lowest_vcn),
(attr->non_resident) ? NULL : ((u8 *)attr +
le16_to_cpu(attr->data.resident.value_offset)), (attr->non_resident) ?
diff --git a/fs/ntfs/dir.c b/fs/ntfs/dir.c
index b95173f068cbc..5a0e21e7c283a 100644
--- a/fs/ntfs/dir.c
+++ b/fs/ntfs/dir.c
@@ -8,6 +8,7 @@
*/
#include <linux/blkdev.h>
+#include <linux/string.h>
#include "dir.h"
#include "mft.h"
@@ -624,7 +625,6 @@ static inline int ntfs_filldir(struct ntfs_volume *vol,
ntfs_debug("Skipping hidden file.");
return 0;
}
-
name_len = ntfs_ucstonls(vol, (__le16 *)&ie->key.file_name.file_name,
ie->key.file_name.file_name_length, &name,
NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1);
@@ -633,7 +633,10 @@ static inline int ntfs_filldir(struct ntfs_volume *vol,
(long long)MREF_LE(ie->data.dir.indexed_file));
return 0;
}
-
+ if (NVolStreamsWindows(vol) && strchr((char *)name, ':')) {
+ ntfs_debug("Skipping file name containing a stream separator.");
+ return 0;
+ }
mref = MREF_LE(ie->data.dir.indexed_file);
if (ie->key.file_name.file_attributes & FILE_ATTR_REPARSE_POINT)
dt_type = ntfs_reparse_tag_dt_types(vol, mref);
diff --git a/fs/ntfs/ea.c b/fs/ntfs/ea.c
index ff69fb07f84a4..1104fd72cd2ae 100644
--- a/fs/ntfs/ea.c
+++ b/fs/ntfs/ea.c
@@ -246,7 +246,7 @@ static int ntfs_set_ea(struct inode *inode, const char *name, size_t name_len,
goto out;
if (ntfs_attr_exist(ni, AT_EA, AT_UNNAMED, 0)) {
- err = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0);
+ err = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0, NULL);
if (err)
goto out;
}
@@ -301,11 +301,12 @@ static int ntfs_set_ea(struct inode *inode, const char *name, size_t name_len,
p_ea_info->ea_query_length = cpu_to_le32(ea_info_qsize);
if ((flags & XATTR_REPLACE) && !val_size && !ea_info_qsize) {
- err = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0);
+ err = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0, NULL);
if (err)
goto out;
- err = ntfs_attr_remove(ni, AT_EA_INFORMATION, AT_UNNAMED, 0);
+ err = ntfs_attr_remove(ni, AT_EA_INFORMATION, AT_UNNAMED,
+ 0, NULL);
if (err) {
/* Restore the original $EA if $EA_INFORMATION removal failed. */
ntfs_attr_add(ni, AT_EA, AT_UNNAMED, 0, old_ea_buf,
@@ -610,6 +611,14 @@ static int ntfs_getxattr(const struct xattr_handler *handler,
struct ntfs_inode *ni = NTFS_I(inode);
int err;
+ /*
+ * Stream permissions and privileges belong to the base inode. This
+ * also lets VFS killpriv helpers find the base security attributes.
+ */
+ if (ntfs_inode_is_named_stream(ni)) {
+ ni = ni->ext.base_ntfs_ino;
+ inode = VFS_I(ni);
+ }
if (NVolShutdown(ni->vol))
return -EIO;
@@ -716,8 +725,8 @@ static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr)
goto err_out;
ntfs_attr_reinit_search_ctx(ctx);
- err = ntfs_attr_lookup(ni->type, ni->name,
- ni->name_len, CASE_SENSITIVE,
+ err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE,
0, NULL, 0, ctx);
if (err) {
err = -EINVAL;
@@ -883,6 +892,8 @@ static int ntfs_setxattr(const struct xattr_handler *handler,
int err;
__le32 fattr;
+ if (ntfs_inode_is_named_stream(ni))
+ return -EOPNOTSUPP;
if (NVolShutdown(ni->vol))
return -EIO;
diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
index 1e2500a521483..a2e843cf947c8 100644
--- a/fs/ntfs/file.c
+++ b/fs/ntfs/file.c
@@ -15,6 +15,13 @@
#include <linux/posix_acl_xattr.h>
#include <linux/compat.h>
#include <linux/falloc.h>
+#include <linux/file.h>
+#include <linux/filelock.h>
+#include <linux/list.h>
+#include <linux/overflow.h>
+#include <linux/security.h>
+#include <linux/slab.h>
+#include <uapi/linux/ntfs.h>
#include "lcnalloc.h"
#include "ntfs.h"
@@ -23,8 +30,8 @@
#include "iomap.h"
#include "bitmap.h"
#include "volume.h"
-
-#include <linux/filelock.h>
+#include "mft.h"
+#include "stream.h"
/*
* ntfs_file_open - called when an inode is about to be opened
@@ -44,7 +51,7 @@
*
* After the check passes, just call generic_file_open() to do its work.
*/
-static int ntfs_file_open(struct inode *vi, struct file *filp)
+int ntfs_file_open(struct inode *vi, struct file *filp)
{
struct ntfs_inode *ni = NTFS_I(vi);
@@ -78,6 +85,7 @@ static int ntfs_file_open(struct inode *vi, struct file *filp)
static int ntfs_trim_prealloc(struct inode *vi)
{
struct ntfs_inode *ni = NTFS_I(vi);
+ struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct runlist_element *rl;
s64 aligned_data_size;
@@ -86,7 +94,7 @@ static int ntfs_trim_prealloc(struct inode *vi)
int err = 0;
inode_lock(vi);
- mutex_lock(&ni->mrec_lock);
+ mutex_lock(&mrec_ni->mrec_lock);
down_write(&ni->runlist.lock);
aligned_data_size = round_up(ni->data_size, vol->cluster_size);
@@ -121,13 +129,13 @@ static int ntfs_trim_prealloc(struct inode *vi)
out_unlock:
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
inode_unlock(vi);
return err;
}
-static int ntfs_file_release(struct inode *vi, struct file *filp)
+int ntfs_file_release(struct inode *vi, struct file *filp)
{
if (!NInoCompressed(NTFS_I(vi)) &&
!NInoWofCompressed(NTFS_I(vi)))
@@ -136,6 +144,11 @@ static int ntfs_file_release(struct inode *vi, struct file *filp)
return 0;
}
+struct ntfs_fsync_attr {
+ struct list_head list;
+ struct inode *inode;
+};
+
/*
* ntfs_file_fsync - sync a file to disk
* @filp: file to be synced
@@ -156,7 +169,7 @@ static int ntfs_file_release(struct inode *vi, struct file *filp)
* Also, if @datasync is true, we do not wait on the inode to be written out
* but we always wait on the page cache pages to be written out.
*/
-static int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,
+int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,
int datasync)
{
struct inode *vi = filp->f_mapping->host;
@@ -165,6 +178,9 @@ static int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,
int err, ret = 0;
struct inode *parent_vi, *ia_vi;
struct ntfs_attr_search_ctx *ctx;
+ struct ntfs_fsync_attr *attr, *next;
+ LIST_HEAD(attrs);
+ bool non_resident, stream;
ntfs_debug("Entering for inode 0x%llx.", ni->mft_no);
@@ -175,10 +191,24 @@ static int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,
if (err)
return err;
- if (!datasync || !NInoNonResident(NTFS_I(vi)))
+ stream = ntfs_inode_is_named_stream(ni);
+ non_resident = NInoNonResident(ni);
+ if (stream) {
+ ni = ni->ext.base_ntfs_ino;
+ vi = VFS_I(ni);
+ }
+
+ if (!datasync || !non_resident)
ret = __ntfs_write_inode(vi, 1);
write_inode_now(vi, !datasync);
+ /*
+ * file_write_and_wait_range() already flushed this stream mapping.
+ * A stream fsync does not need to flush sibling attribute mappings.
+ */
+ if (stream)
+ goto sync_volume;
+
ctx = ntfs_attr_get_search_ctx(ni, NULL);
if (!ctx)
return -ENOMEM;
@@ -210,23 +240,55 @@ static int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,
name = (__le16 *)((u8 *)ctx->attr + le16_to_cpu(ctx->attr->name_offset));
if (ctx->attr->type == AT_DATA && ctx->attr->name_length == 0)
continue;
+ if (ctx->attr->type == AT_DATA &&
+ ntfs_stream_unlinked(ni, name, ctx->attr->name_length))
+ continue;
+ attr = kmalloc_obj(*attr, GFP_NOFS);
+ if (!attr) {
+ err = -ENOMEM;
+ break;
+ }
attr_vi = ntfs_attr_iget(vi, ctx->attr->type,
name, ctx->attr->name_length);
- if (IS_ERR(attr_vi))
+ if (IS_ERR(attr_vi)) {
+ kfree(attr);
continue;
- spin_lock(&attr_vi->i_lock);
- if (inode_state_read_once(attr_vi) & I_DIRTY_PAGES) {
- spin_unlock(&attr_vi->i_lock);
- filemap_write_and_wait(attr_vi->i_mapping);
- } else
- spin_unlock(&attr_vi->i_lock);
- iput(attr_vi);
+ }
+ if (ntfs_inode_is_named_stream(NTFS_I(attr_vi)))
+ atomic_inc(&NTFS_I(attr_vi)->stream_open_count);
+ attr->inode = attr_vi;
+ list_add_tail(&attr->list, &attrs);
}
}
mutex_unlock(&ni->mrec_lock);
ntfs_attr_put_search_ctx(ctx);
+ if (err != -ENOENT && !ret)
+ ret = err;
+ /* Attribute writeback takes the base inode's MFT record lock. */
+ list_for_each_entry_safe(attr, next, &attrs, list) {
+ struct inode *attr_vi = attr->inode;
+
+ err = filemap_write_and_wait(attr_vi->i_mapping);
+ if (err && !ret)
+ ret = err;
+ if (ntfs_inode_is_named_stream(NTFS_I(attr_vi))) {
+ err = ntfs_stream_put(attr_vi);
+ if (err && !ret)
+ ret = err;
+ }
+ iput(attr_vi);
+ list_del(&attr->list);
+ kfree(attr);
+ }
+
+ /* Attribute writeback may have updated the base mapping pairs. */
+ err = write_inode_now(vi, 1);
+ if (err && !ret)
+ ret = err;
+
+sync_volume:
write_inode_now(vol->mftbmp_ino, 1);
down_write(&vol->lcnbmp_lock);
write_inode_now(vol->lcnbmp_ino, 1);
@@ -268,12 +330,17 @@ static void ntfs_pagecache_extend(struct inode *vi, loff_t from, loff_t to)
PAGE_SIZE, 0);
}
-static int ntfs_setattr_size(struct inode *vi, struct iattr *attr)
+int ntfs_setattr_size(struct inode *vi, struct iattr *attr)
{
struct ntfs_inode *ni = NTFS_I(vi);
+ struct ntfs_inode *base_ni = ntfs_base_inode(ni);
+ bool stream = ntfs_inode_is_named_stream(ni);
int err;
loff_t old_size = vi->i_size;
+ if (stream && NVolShutdown(ni->vol))
+ return -EIO;
+
if (NInoCompressed(ni) || NInoEncrypted(ni) || NInoWofCompressed(ni)) {
ntfs_warning(
vi->i_sb,
@@ -293,7 +360,21 @@ static int ntfs_setattr_size(struct inode *vi, struct iattr *attr)
* readers cannot observe the size change until the attribute
* updates below have completed.
*/
- filemap_invalidate_lock(vi->i_mapping);
+ if (stream) {
+ filemap_invalidate_lock(vi->i_mapping);
+ err = filemap_write_and_wait(vi->i_mapping);
+ if (err)
+ goto out_unlock_mapping;
+
+ mutex_lock(&base_ni->mrec_lock);
+ err = ntfs_stream_inode_validate(vi);
+ mutex_unlock(&base_ni->mrec_lock);
+ if (err)
+ goto out_unlock_mapping;
+ } else {
+ filemap_invalidate_lock(vi->i_mapping);
+ }
+
if (attr->ia_size > old_size) {
truncate_pagecache(vi, old_size);
i_size_write(vi, attr->ia_size);
@@ -302,10 +383,25 @@ static int ntfs_setattr_size(struct inode *vi, struct iattr *attr)
truncate_setsize(vi, attr->ia_size);
}
- err = ntfs_truncate_vfs(vi, attr->ia_size, old_size);
- if (err)
+ if (stream) {
+ mutex_lock(&base_ni->mrec_lock);
+ err = ntfs_stream_inode_validate(vi);
+ if (!err)
+ err = __ntfs_attr_truncate_vfs(ni, attr->ia_size,
+ old_size);
+ mutex_unlock(&base_ni->mrec_lock);
+ } else {
+ err = ntfs_truncate_vfs(vi, attr->ia_size, old_size);
+ }
+ if (err) {
i_size_write(vi, old_size);
+ goto out_unlock_mapping;
+ }
+
+out_unlock_mapping:
filemap_invalidate_unlock(vi->i_mapping);
+ if (!err && stream)
+ ntfs_stream_update_base_time(base_ni);
return err;
}
@@ -437,7 +533,25 @@ int ntfs_getattr(struct mnt_idmap *idmap, const struct path *path,
return 0;
}
-static loff_t ntfs_file_llseek(struct file *file, loff_t offset, int whence)
+/*
+ * Validate a stream before VFS write handling removes privileges or updates
+ * timestamps on its base inode.
+ */
+static int ntfs_file_modified(struct kiocb *iocb)
+{
+ struct inode *inode = file_inode(iocb->ki_filp);
+ int err;
+
+ if (ntfs_inode_is_named_stream(NTFS_I(inode))) {
+ err = ntfs_stream_validate_for_mutation(inode,
+ iocb->ki_flags & IOCB_NOWAIT);
+ if (err)
+ return err;
+ }
+ return kiocb_modified(iocb);
+}
+
+loff_t ntfs_file_llseek(struct file *file, loff_t offset, int whence)
{
struct inode *inode = file->f_mapping->host;
@@ -466,7 +580,7 @@ static loff_t ntfs_file_llseek(struct file *file, loff_t offset, int whence)
return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
}
-static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
+ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
{
struct inode *vi = file_inode(iocb->ki_filp);
struct super_block *sb = vi->i_sb;
@@ -513,7 +627,11 @@ static int ntfs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
if (size) {
if (i_size_read(inode) < iocb->ki_pos + size) {
i_size_write(inode, iocb->ki_pos + size);
- mark_inode_dirty(inode);
+ if (ntfs_inode_is_named_stream(NTFS_I(inode)))
+ mark_inode_dirty(VFS_I(
+ NTFS_I(inode)->ext.base_ntfs_ino));
+ else
+ mark_inode_dirty(inode);
}
}
@@ -583,6 +701,7 @@ static ssize_t ntfs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
static int ntfs_expand_for_write(struct ntfs_inode *ni, loff_t end)
{
struct ntfs_volume *vol = ni->vol;
+ struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
loff_t prealloc_size = 0;
int err;
@@ -598,14 +717,14 @@ static int ntfs_expand_for_write(struct ntfs_inode *ni, loff_t end)
prealloc_size = ni->allocated_size + vol->preallocated_size;
}
- mutex_lock(&ni->mrec_lock);
+ mutex_lock(&mrec_ni->mrec_lock);
err = ntfs_attr_expand(ni, end, prealloc_size);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
return err;
}
-static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
+ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
{
struct file *file = iocb->ki_filp;
struct inode *vi = file->f_mapping->host;
@@ -642,7 +761,7 @@ static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
if (ret <= 0)
goto out_lock;
- err = file_modified(iocb->ki_filp);
+ err = ntfs_file_modified(iocb);
if (err) {
ret = err;
goto out_lock;
@@ -692,13 +811,21 @@ static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
out:
if (ret < 0 && ret != -EIOCBQUEUED) {
if (ni->initialized_size != old_init_size) {
- mutex_lock(&ni->mrec_lock);
+ struct ntfs_inode *mrec_ni =
+ ntfs_base_inode(ni);
+
+ mutex_lock(&mrec_ni->mrec_lock);
ntfs_attr_set_initialized_size(ni, old_init_size);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
}
if (ni->data_size != old_data_size) {
+ struct ntfs_inode *mrec_ni =
+ ntfs_base_inode(ni);
+
truncate_setsize(vi, old_data_size);
+ mutex_lock(&mrec_ni->mrec_lock);
ntfs_attr_truncate(ni, old_data_size);
+ mutex_unlock(&mrec_ni->mrec_lock);
}
}
out_lock:
@@ -712,13 +839,24 @@ static vm_fault_t ntfs_filemap_page_mkwrite(struct vm_fault *vmf)
{
struct inode *inode = file_inode(vmf->vma->vm_file);
struct address_space *mapping = inode->i_mapping;
+ bool stream = ntfs_inode_is_named_stream(NTFS_I(inode));
+ int err;
vm_fault_t ret;
if (NInoWofCompressed(NTFS_I(inode)))
return VM_FAULT_SIGBUS;
sb_start_pagefault(inode->i_sb);
- file_update_time(vmf->vma->vm_file);
+ if (stream) {
+ err = ntfs_stream_validate_for_mutation(inode, false);
+ if (err)
+ goto out_error;
+ err = file_update_time(vmf->vma->vm_file);
+ if (err)
+ goto out_error;
+ } else {
+ file_update_time(vmf->vma->vm_file);
+ }
/*
* Serialize against truncate/fallocate which hold the lock
@@ -727,9 +865,12 @@ static vm_fault_t ntfs_filemap_page_mkwrite(struct vm_fault *vmf)
filemap_invalidate_lock_shared(mapping);
ret = iomap_page_mkwrite(vmf, &ntfs_page_mkwrite_iomap_ops, NULL);
filemap_invalidate_unlock_shared(mapping);
-
sb_end_pagefault(inode->i_sb);
return ret;
+
+out_error:
+ sb_end_pagefault(inode->i_sb);
+ return vmf_error(err);
}
static const struct vm_operations_struct ntfs_file_vm_ops = {
@@ -738,7 +879,7 @@ static const struct vm_operations_struct ntfs_file_vm_ops = {
.page_mkwrite = ntfs_filemap_page_mkwrite,
};
-static int ntfs_file_mmap_prepare(struct vm_area_desc *desc)
+int ntfs_file_mmap_prepare(struct vm_area_desc *desc)
{
struct file *file = desc->file;
struct inode *inode = file_inode(file);
@@ -771,7 +912,7 @@ static int ntfs_file_mmap_prepare(struct vm_area_desc *desc)
return 0;
}
-static int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
+int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
u64 start, u64 len)
{
if (NInoWofCompressed(NTFS_I(inode)))
@@ -808,7 +949,7 @@ static const char *ntfs_get_link(struct dentry *dentry, struct inode *inode,
return ni->target;
}
-static ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos,
+ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos,
struct pipe_inode_info *pipe, size_t len, unsigned int flags)
{
if (NVolShutdown(NTFS_SB(in->f_mapping->host->i_sb)))
@@ -924,6 +1065,14 @@ long ntfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
return ntfs_ioctl_set_volume_label(filp, arg);
case FITRIM:
return ntfs_ioctl_fitrim(NTFS_SB(file_inode(filp)->i_sb), arg);
+ case NTFS_IOC_STREAM_READ:
+ return ntfs_ioctl_stream_read(filp, arg);
+ case NTFS_IOC_STREAM_WRITE:
+ return ntfs_ioctl_stream_write(filp, arg);
+ case NTFS_IOC_STREAM_REMOVE:
+ return ntfs_ioctl_stream_remove(filp, arg);
+ case NTFS_IOC_LIST_STREAMS:
+ return ntfs_ioctl_list_streams(filp, arg);
default:
return -ENOTTY;
}
@@ -980,6 +1129,7 @@ static int ntfs_allocate_range(struct ntfs_inode *ni, int mode, loff_t offset,
static int ntfs_punch_hole(struct ntfs_inode *ni, int mode, loff_t offset,
loff_t len)
{
+ struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct inode *vi = VFS_I(ni);
loff_t end_offset;
@@ -1044,16 +1194,17 @@ static int ntfs_punch_hole(struct ntfs_inode *ni, int mode, loff_t offset,
end_vcn--;
}
- mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
+ mutex_lock_nested(&mrec_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
err = ntfs_non_resident_attr_punch_hole(ni, start_vcn,
end_vcn - start_vcn);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
out:
return err;
}
static int ntfs_collapse_range(struct ntfs_inode *ni, loff_t offset, loff_t len)
{
+ struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct inode *vi = VFS_I(ni);
loff_t old_size, new_size;
@@ -1087,10 +1238,10 @@ static int ntfs_collapse_range(struct ntfs_inode *ni, loff_t offset, loff_t len)
truncate_pagecache(vi, offset_down);
- mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
+ mutex_lock_nested(&mrec_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
err = ntfs_non_resident_attr_collapse_range(ni, start_vcn,
end_vcn - start_vcn);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
if (new_size != old_size)
i_size_write(vi, ni->data_size);
@@ -1100,6 +1251,7 @@ static int ntfs_collapse_range(struct ntfs_inode *ni, loff_t offset, loff_t len)
static int ntfs_insert_range(struct ntfs_inode *ni, loff_t offset, loff_t len)
{
+ struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct inode *vi = VFS_I(ni);
loff_t offset_down = round_down(offset,
@@ -1143,10 +1295,10 @@ static int ntfs_insert_range(struct ntfs_inode *ni, loff_t offset, loff_t len)
truncate_pagecache(vi, offset_down);
- mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
+ mutex_lock_nested(&mrec_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
err = ntfs_non_resident_attr_insert_range(ni, start_vcn,
end_vcn - start_vcn);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
if (new_size != old_size)
i_size_write(vi, ni->data_size);
@@ -1159,11 +1311,13 @@ static int ntfs_insert_range(struct ntfs_inode *ni, loff_t offset, loff_t len)
FALLOC_FL_INSERT_RANGE | FALLOC_FL_PUNCH_HOLE | \
FALLOC_FL_COLLAPSE_RANGE)
-static long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
+long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
{
struct inode *vi = file_inode(file);
struct ntfs_inode *ni = NTFS_I(vi);
+ struct ntfs_inode *base_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
+ bool stream = ntfs_inode_is_named_stream(ni);
int err = 0;
loff_t old_size, new_size;
@@ -1193,6 +1347,13 @@ static long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t le
inode_unlock(vi);
return -EOPNOTSUPP;
}
+ if (stream) {
+ err = ntfs_stream_validate_for_mutation(vi, false);
+ if (err) {
+ inode_unlock(vi);
+ return err;
+ }
+ }
old_size = i_size_read(vi);
inode_dio_wait(vi);
@@ -1233,9 +1394,13 @@ static long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t le
filemap_invalidate_unlock(vi->i_mapping);
if (!err) {
- NInoSetFileNameDirty(ni);
- inode_set_mtime_to_ts(vi, inode_set_ctime_current(vi));
- mark_inode_dirty(vi);
+ if (stream) {
+ ntfs_stream_update_base_time(base_ni);
+ } else {
+ NInoSetFileNameDirty(ni);
+ inode_set_mtime_to_ts(vi, inode_set_ctime_current(vi));
+ mark_inode_dirty(vi);
+ }
}
inode_unlock(vi);
diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c
index 6cfe8f24e4cce..93680a11fdccc 100644
--- a/fs/ntfs/inode.c
+++ b/fs/ntfs/inode.c
@@ -20,6 +20,7 @@
#include "attrib.h"
#include "iomap.h"
#include "object_id.h"
+#include "stream.h"
/*
* ntfs_test_inode - compare two (possibly fake) inodes for equality
@@ -56,9 +57,18 @@ int ntfs_test_inode(struct inode *vi, void *data)
return 0;
if (ni->name_len != na->name_len)
return 0;
- if (na->name_len && memcmp(ni->name, na->name,
- na->name_len * sizeof(__le16)))
- return 0;
+ if (na->name_len) {
+ if (ntfs_inode_is_named_stream(ni)) {
+ if (!ntfs_names_are_equal(ni->name, ni->name_len,
+ na->name, na->name_len,
+ IGNORE_CASE, ni->vol->upcase,
+ ni->vol->upcase_len))
+ return 0;
+ } else if (memcmp(ni->name, na->name,
+ na->name_len * sizeof(__le16))) {
+ return 0;
+ }
+ }
if (!ni->ext.base_ntfs_ino)
return 0;
}
@@ -67,6 +77,33 @@ int ntfs_test_inode(struct inode *vi, void *data)
return 1;
}
+/*
+ * ntfs_test_inode_rcu() - Test an inode during RCU hash lookup
+ * @vi: inode being tested
+ * @data: NTFS attribute key to match
+ *
+ * Reject inodes being initialized or destroyed, then run the normal NTFS
+ * inode match while holding @vi's inode lock.
+ *
+ * Return: 1 if the inode matches, or 0 otherwise.
+ */
+int ntfs_test_inode_rcu(struct inode *vi, void *data)
+{
+ unsigned long state;
+ int ret;
+
+ spin_lock(&vi->i_lock);
+ state = inode_state_read_once(vi);
+ if (state & (I_FREEING | I_WILL_FREE | I_NEW)) {
+ ret = 0;
+ goto out;
+ }
+ ret = ntfs_test_inode(vi, data);
+out:
+ spin_unlock(&vi->i_lock);
+ return ret;
+}
+
/*
* ntfs_init_locked_inode - initialize an inode
* @vi: vfs inode to initialize
@@ -468,6 +505,7 @@ void __ntfs_init_inode(struct super_block *sb, struct ntfs_inode *ni)
ni->seq_no = 0;
atomic_set(&ni->count, 1);
ni->vol = NTFS_SB(sb);
+ atomic_set(&ni->stream_open_count, 0);
ntfs_init_runlist(&ni->runlist);
mutex_init(&ni->mrec_lock);
if (ni->type == AT_ATTRIBUTE_LIST) {
@@ -1320,6 +1358,7 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
struct attr_record *a;
struct ntfs_attr_search_ctx *ctx;
int err = 0;
+ u32 ic;
ntfs_debug("Entering for i_ino 0x%llx.", ni->mft_no);
@@ -1334,8 +1373,12 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
inode_set_atime_to_ts(vi, inode_get_atime(base_vi));
vi->i_generation = ni->seq_no = base_ni->seq_no;
- /* Set inode type to zero but preserve permissions. */
- vi->i_mode = base_vi->i_mode & ~S_IFMT;
+ /* Named data streams are regular files throughout initialization. */
+ vi->i_mode = base_vi->i_mode & ~S_IFMT;
+ if (ni->type == AT_DATA && ni->name_len) {
+ vi->i_mode |= S_IFREG;
+ vi->i_flags = base_vi->i_flags;
+ }
m = map_mft_record(base_ni);
if (IS_ERR(m)) {
@@ -1348,11 +1391,29 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
goto unm_err_out;
}
/* Find the attribute. */
+ ic = ntfs_inode_is_named_stream(ni) ? IGNORE_CASE : CASE_SENSITIVE;
err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
- CASE_SENSITIVE, 0, NULL, 0, ctx);
+ ic, 0, NULL, 0, ctx);
if (unlikely(err))
goto unm_err_out;
a = ctx->attr;
+ if (ntfs_inode_is_named_stream(ni)) {
+ __le16 *disk_name = (__le16 *)((u8 *)a +
+ le16_to_cpu(a->name_offset));
+
+ /* Subsequent extent and mutation lookups must select this name. */
+ if (ni->name == I30) {
+ ni->name = kmalloc_array(ni->name_len + 1,
+ sizeof(__le16), GFP_NOFS);
+ if (!ni->name) {
+ err = -ENOMEM;
+ goto unm_err_out;
+ }
+ }
+ memcpy(ni->name, disk_name,
+ ni->name_len * sizeof(__le16));
+ ni->name[ni->name_len] = 0;
+ }
if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) {
if (a->flags & ATTR_COMPRESSION_MASK) {
NInoSetCompressed(ni);
@@ -1486,6 +1547,10 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
vi->i_blocks = ni->itype.compressed.size >> 9;
else
vi->i_blocks = ni->allocated_size >> 9;
+ if (ni->type == AT_DATA && ni->name_len) {
+ vi->i_op = &ntfs_stream_inode_ops;
+ vi->i_fop = &ntfs_stream_file_ops;
+ }
/*
* Make sure the base inode does not go away and attach it to the
* attribute inode.
@@ -2476,6 +2541,10 @@ int ntfs_show_options(struct seq_file *sf, struct dentry *root)
seq_puts(sf, ",symlink=native");
else
seq_puts(sf, ",symlink=wsl");
+ if (NVolStreamsWindows(vol))
+ seq_puts(sf, ",streams_interface=windows");
+ else
+ seq_puts(sf, ",streams_interface=none");
if (vol->sb->s_flags & SB_POSIXACL)
seq_puts(sf, ",acl");
return 0;
@@ -2485,6 +2554,7 @@ int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset,
const loff_t new_size)
{
struct ntfs_inode *ni = NTFS_I(vi);
+ struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
loff_t old_init_size, clear_size;
unsigned long flags;
int err = 0;
@@ -2522,9 +2592,9 @@ int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset,
}
}
- mutex_lock(&ni->mrec_lock);
+ mutex_lock(&mrec_ni->mrec_lock);
err = ntfs_attr_set_initialized_size(ni, new_size);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
if (err)
truncate_setsize(vi, old_init_size);
out:
@@ -2535,15 +2605,19 @@ int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset,
int ntfs_truncate_vfs(struct inode *vi, loff_t new_size, loff_t i_size)
{
struct ntfs_inode *ni = NTFS_I(vi);
+ struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
int err;
- mutex_lock(&ni->mrec_lock);
+ mutex_lock(&mrec_ni->mrec_lock);
err = __ntfs_attr_truncate_vfs(ni, new_size, i_size);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
if (err < 0)
return err;
- inode_set_mtime_to_ts(vi, inode_set_ctime_current(vi));
+ if (ntfs_inode_is_named_stream(ni))
+ ntfs_stream_update_base_time(mrec_ni);
+ else
+ inode_set_mtime_to_ts(vi, inode_set_ctime_current(vi));
return 0;
}
@@ -3615,19 +3689,30 @@ s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf)
struct address_space *mapping = vi->i_mapping;
struct folio *folio;
struct ntfs_inode *ni = NTFS_I(vi);
+ struct ntfs_inode *base_ni = ni->ext.base_ntfs_ino;
+ struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?
+ base_ni : ni;
s64 isize;
u32 attr_len, total = 0, offset;
pgoff_t index;
int err = 0;
+ bool claimed = false;
WARN_ON(!NInoAttr(ni));
if (!count)
return 0;
- mutex_lock(&ni->mrec_lock);
+ mutex_lock(&mrec_ni->mrec_lock);
+ if (ntfs_inode_is_named_stream(ni)) {
+ err = ntfs_stream_inode_validate(vi);
+ if (err) {
+ mutex_unlock(&mrec_ni->mrec_lock);
+ return err;
+ }
+ }
isize = i_size_read(vi);
if (pos > isize) {
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
return -EINVAL;
}
if (pos + count > isize)
@@ -3637,37 +3722,44 @@ s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf)
struct ntfs_attr_search_ctx *ctx;
u8 *attr;
- ctx = ntfs_attr_get_search_ctx(ni->ext.base_ntfs_ino, NULL);
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ntfs_error(vi->i_sb, "Failed to get attr search ctx");
err = -ENOMEM;
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
goto out;
}
- err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE,
+ err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE,
0, NULL, 0, ctx);
if (err) {
ntfs_error(vi->i_sb, "Failed to look up attr %#x", ni->type);
ntfs_attr_put_search_ctx(ctx);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
goto out;
}
attr = (u8 *)ctx->attr + le16_to_cpu(ctx->attr->data.resident.value_offset);
memcpy(buf, (u8 *)attr + pos, count);
ntfs_attr_put_search_ctx(ctx);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
return count;
}
- mutex_unlock(&ni->mrec_lock);
+ if (ntfs_inode_is_named_stream(ni)) {
+ atomic_inc(&ni->stream_open_count);
+ claimed = true;
+ }
+ mutex_unlock(&mrec_ni->mrec_lock);
index = pos >> PAGE_SHIFT;
do {
/* Update @index and get the next folio. */
folio = read_mapping_folio(mapping, index, NULL);
- if (IS_ERR(folio))
+ if (IS_ERR(folio)) {
+ err = PTR_ERR(folio);
break;
+ }
offset = offset_in_folio(folio, pos);
attr_len = min_t(size_t, (size_t)count, folio_size(folio) - offset);
@@ -3684,6 +3776,8 @@ s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf)
index++;
} while (count);
out:
+ if (claimed)
+ ntfs_stream_put(vi);
return err ? (s64)err : total;
}
@@ -3711,8 +3805,8 @@ static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,
}
ntfs_attr_reinit_search_ctx(ctx);
- ret = ntfs_attr_lookup(ni->type,
- ni->name, ni->name_len, CASE_SENSITIVE,
+ ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE,
0, NULL, 0, ctx);
if (ret) {
ntfs_error(sb, "Failed to look up attr %#x", ni->type);
@@ -3726,6 +3820,13 @@ static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,
return 0;
}
+ /* Named streams initialize gaps after dropping the MFT record lock. */
+ if (ntfs_inode_is_named_stream(ni)) {
+ if (i_size_read(vi) < ni->data_size)
+ i_size_write(vi, ni->data_size);
+ return 0;
+ }
+
if (pos + count > ni->initialized_size) {
ctx->attr->data.non_resident.initialized_size = cpu_to_le64(pos + count);
mark_mft_record_dirty(ctx->ntfs_ino);
@@ -3780,7 +3881,6 @@ static s64 __ntfs_inode_resident_attr_pwrite(struct inode *vi,
static s64 __ntfs_inode_non_resident_attr_pwrite(struct inode *vi,
s64 pos, s64 count, u8 *buf,
- struct ntfs_attr_search_ctx *ctx,
bool sync)
{
struct ntfs_inode *ni = NTFS_I(vi);
@@ -3919,37 +4019,79 @@ static s64 __ntfs_inode_non_resident_attr_pwrite(struct inode *vi,
s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool sync)
{
struct ntfs_inode *ni = NTFS_I(vi);
+ struct ntfs_inode *base_ni = ni->ext.base_ntfs_ino;
+ struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?
+ base_ni : ni;
struct ntfs_attr_search_ctx *ctx;
s64 ret;
WARN_ON(!NInoAttr(ni));
- ctx = ntfs_attr_get_search_ctx(ni->ext.base_ntfs_ino, NULL);
+ if (ntfs_inode_is_named_stream(ni)) {
+ if (NInoEncrypted(ni))
+ return -EACCES;
+ if (NInoCompressed(ni))
+ return -EOPNOTSUPP;
+ }
+
+ mutex_lock(&mrec_ni->mrec_lock);
+ if (ntfs_inode_is_named_stream(ni)) {
+ ret = ntfs_stream_inode_validate(vi);
+ if (ret)
+ goto out_unlock;
+ }
+
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ntfs_error(vi->i_sb, "Failed to get attr search ctx");
- return -ENOMEM;
+ ret = -ENOMEM;
+ goto out_unlock;
}
- ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE,
+ ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE,
0, NULL, 0, ctx);
if (ret) {
ntfs_attr_put_search_ctx(ctx);
ntfs_error(vi->i_sb, "Failed to look up attr %#x", ni->type);
- return ret;
+ goto out_unlock;
}
- mutex_lock(&ni->mrec_lock);
ret = ntfs_enlarge_attribute(vi, pos, count, ctx);
- mutex_unlock(&ni->mrec_lock);
if (ret)
- goto out;
+ goto out_ctx;
- if (NInoNonResident(ni))
- ret = __ntfs_inode_non_resident_attr_pwrite(vi, pos, count, buf, ctx, sync);
- else
+ if (!NInoNonResident(ni)) {
ret = __ntfs_inode_resident_attr_pwrite(vi, pos, count, buf, ctx);
-out:
+ goto out_ctx;
+ }
+
+ if (ntfs_inode_is_named_stream(ni))
+ atomic_inc(&ni->stream_open_count);
+ ntfs_attr_put_search_ctx(ctx);
+ mutex_unlock(&mrec_ni->mrec_lock);
+ /* Attribute writes bypass iomap's delayed-allocation preparation. */
+ if (ntfs_inode_is_named_stream(ni)) {
+ /* A failed write must not expose any newly initialized data. */
+ ret = ntfs_extend_initialized_size(vi, pos + count, pos + count);
+ if (!ret)
+ ret = ntfs_attr_fallocate(ni, pos, count, true);
+ }
+ if (!ret)
+ ret = __ntfs_inode_non_resident_attr_pwrite(vi, pos, count,
+ buf, sync);
+ if (ntfs_inode_is_named_stream(ni))
+ ntfs_stream_put(vi);
+ if (ret > 0 && ntfs_inode_is_named_stream(ni))
+ ntfs_stream_update_base_time(base_ni);
+ return ret;
+
+out_ctx:
ntfs_attr_put_search_ctx(ctx);
+out_unlock:
+ mutex_unlock(&mrec_ni->mrec_lock);
+ if (ret > 0 && ntfs_inode_is_named_stream(ni))
+ ntfs_stream_update_base_time(base_ni);
return ret;
}
diff --git a/fs/ntfs/inode.h b/fs/ntfs/inode.h
index ff61bd402df03..f4e5562d56a7e 100644
--- a/fs/ntfs/inode.h
+++ b/fs/ntfs/inode.h
@@ -107,6 +107,7 @@ struct ntfs_inode {
__le32 type;
__le16 *name;
u32 name_len;
+ atomic_t stream_open_count;
struct runlist runlist;
s64 data_size;
s64 initialized_size;
@@ -178,6 +179,7 @@ struct ntfs_inode {
* NI_BeingCreated ntfs inode is being created.
* NI_HasEA ntfs inode has EA attribute.
* NI_RunlistDirty runlist need to be updated.
+ * NI_StreamUnlinked Named stream is unlinked but still open.
*/
enum {
NI_Dirty,
@@ -199,6 +201,7 @@ enum {
NI_BeingCreated,
NI_HasEA,
NI_RunlistDirty,
+ NI_StreamUnlinked,
};
/*
@@ -259,6 +262,7 @@ TAS_NINO_FNS(FileNameDirty)
NINO_FNS(BeingDeleted)
NINO_FNS(HasEA)
NINO_FNS(RunlistDirty)
+NINO_FNS(StreamUnlinked)
/*
* The full structure containing a ntfs_inode and a vfs struct inode. Used for
@@ -286,6 +290,20 @@ static inline struct inode *VFS_I(struct ntfs_inode *ni)
return &container_of(ni, struct big_ntfs_inode, ntfs_inode)->vfs_inode;
}
+/* Return true when @ni represents a named $DATA attribute inode. */
+static inline bool ntfs_inode_is_named_stream(struct ntfs_inode *ni)
+{
+ return NInoAttr(ni) && ni->type == AT_DATA && ni->name_len;
+}
+
+/* Return the base MFT inode for an attribute inode, or @ni itself. */
+static inline struct ntfs_inode *ntfs_base_inode(struct ntfs_inode *ni)
+{
+ if (NInoAttr(ni) && ni->nr_extents == -1 && ni->ext.base_ntfs_ino)
+ return ni->ext.base_ntfs_ino;
+ return ni;
+}
+
/*
* ntfs_attr - ntfs in memory attribute structure
*
@@ -304,6 +322,7 @@ struct ntfs_attr {
};
int ntfs_test_inode(struct inode *vi, void *data);
+int ntfs_test_inode_rcu(struct inode *vi, void *data);
struct inode *ntfs_iget(struct super_block *sb, u64 mft_no);
struct inode *ntfs_attr_iget(struct inode *base_vi, __le32 type,
__le16 *name, u32 name_len);
diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
index 754d5d91b6dc6..2940a5587add2 100644
--- a/fs/ntfs/iomap.c
+++ b/fs/ntfs/iomap.c
@@ -64,11 +64,7 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo
int err = 0;
char *kattr;
- if (NInoAttr(ni))
- base_ni = ni->ext.base_ntfs_ino;
- else
- base_ni = ni;
-
+ base_ni = ntfs_base_inode(ni);
mutex_lock(&base_ni->mrec_lock);
ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
@@ -114,7 +110,8 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo
if (ctx)
ntfs_attr_put_search_ctx(ctx);
- if (!err && keep_mrec_lock && iomap->type == IOMAP_INLINE) {
+ if (!err && keep_mrec_lock &&
+ iomap->type == IOMAP_INLINE) {
iomap->private = base_ni;
return 0;
}
@@ -399,6 +396,8 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
loff_t length, struct iomap *iomap)
{
struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?
+ ntfs_base_inode(ni) : ni;
struct ntfs_volume *vol = ni->vol;
loff_t vcn_ofs, rl_length;
struct runlist_element *rl, *rlc;
@@ -417,7 +416,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
up_read(&ni->runlist.lock);
err = ntfs_map_runlist(ni, vcn);
if (err) {
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
return -ENOENT;
}
down_read(&ni->runlist.lock);
@@ -431,7 +430,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
rl = __ntfs_attr_find_vcn_nolock(&ni->runlist, vcn);
if (IS_ERR(rl)) {
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
return -EIO;
}
lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
@@ -452,7 +451,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
*/
if (lcn == LCN_RL_NOT_MAPPED) {
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
return map_err == -ENOMEM ? -ENOMEM : -EIO;
}
@@ -462,7 +461,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
"runlist(vcn : %lld, length : %lld) is corrupted\n",
rl->vcn, rl->length);
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
return -EIO;
}
@@ -476,7 +475,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
if (max_clu_count < 0) {
err = max_clu_count;
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
return err;
}
}
@@ -491,7 +490,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
GFP_NOFS);
if (!rlc) {
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
return -ENOMEM;
}
@@ -508,7 +507,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
if (IS_ERR(rl)) {
ntfs_error(vol->sb, "Failed to merge runlists");
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
kvfree(rlc);
return PTR_ERR(rl);
}
@@ -518,7 +517,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
ni->i_dealloc_clusters += max_clu_count;
}
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
if (lcn < LCN_DELALLOC)
ntfs_hold_dirty_clusters(vol, max_clu_count);
@@ -570,7 +569,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
}
} else {
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
iomap->type = IOMAP_MAPPED;
iomap->addr = ntfs_cluster_to_bytes(vol, lcn) + vcn_ofs;
@@ -595,6 +594,8 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
struct iomap *iomap, int ntfs_iomap_flags)
{
struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?
+ ntfs_base_inode(ni) : ni;
struct ntfs_volume *vol = ni->vol;
loff_t vcn_ofs, rl_length;
s64 vcn, start_lcn, lcn_count;
@@ -613,7 +614,7 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
max_clu_count, &balloc, update_mp,
ntfs_iomap_flags & NTFS_IOMAP_FLAGS_WRITEBACK);
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
if (err) {
ni->i_dealloc_clusters = 0;
return err;
@@ -667,8 +668,8 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
if (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE &&
iomap->offset + iomap->length > ni->initialized_size) {
- err = ntfs_attr_set_initialized_size(ni, iomap->offset +
- iomap->length);
+ err = ntfs_attr_set_initialized_size(ni,
+ iomap->offset + iomap->length);
}
return err;
@@ -678,13 +679,15 @@ static int ntfs_write_iomap_begin_resident(struct inode *inode, loff_t offset,
struct iomap *iomap)
{
struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?
+ ntfs_base_inode(ni) : ni;
struct attr_record *a;
- struct ntfs_attr_search_ctx *ctx;
+ struct ntfs_attr_search_ctx *ctx = NULL;
u32 attr_len;
int err = 0;
char *kattr;
- ctx = ntfs_attr_get_search_ctx(ni, NULL);
+ ctx = ntfs_attr_get_search_ctx(mrec_ni, NULL);
if (!ctx) {
err = -ENOMEM;
goto out;
@@ -707,13 +710,14 @@ static int ntfs_write_iomap_begin_resident(struct inode *inode, loff_t offset,
iomap->inline_data = kattr;
iomap->offset = 0;
iomap->length = attr_len;
+ iomap->private = mrec_ni;
out:
if (ctx)
ntfs_attr_put_search_ctx(ctx);
if (err)
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
return err;
}
@@ -722,7 +726,12 @@ static int ntfs_write_iomap_begin_non_resident(struct inode *inode, loff_t offse
loff_t length, unsigned int flags,
struct iomap *iomap, int ntfs_iomap_flags)
{
- mutex_lock(&NTFS_I(inode)->mrec_lock);
+ struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?
+ ntfs_base_inode(ni) : ni;
+
+ mutex_lock(&mrec_ni->mrec_lock);
+
if (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_BEGIN)
return ntfs_write_simple_iomap_begin_non_resident(inode, offset,
length, iomap);
@@ -738,12 +747,15 @@ static int __ntfs_write_iomap_begin(struct inode *inode, loff_t offset,
struct iomap *iomap, int ntfs_iomap_flags)
{
struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_inode *mrec_ni;
if (NVolShutdown(ni->vol))
return -EIO;
if (!NInoNonResident(ni)) {
- mutex_lock(&ni->mrec_lock);
+ mrec_ni = ntfs_inode_is_named_stream(ni) ?
+ ntfs_base_inode(ni) : ni;
+ mutex_lock(&mrec_ni->mrec_lock);
return ntfs_write_iomap_begin_resident(inode, offset, iomap);
}
return ntfs_write_iomap_begin_non_resident(inode, offset, length, flags,
@@ -762,10 +774,10 @@ static int ntfs_write_iomap_end_resident(struct inode *inode, loff_t pos,
loff_t length, ssize_t written,
unsigned int flags, struct iomap *iomap)
{
- struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_inode *base_ni = iomap->private;
- mark_mft_record_dirty(ni);
- mutex_unlock(&ni->mrec_lock);
+ mark_mft_record_dirty(base_ni);
+ mutex_unlock(&base_ni->mrec_lock);
return written;
}
diff --git a/fs/ntfs/named_stream.c b/fs/ntfs/named_stream.c
new file mode 100644
index 0000000000000..e44dbf09057b3
--- /dev/null
+++ b/fs/ntfs/named_stream.c
@@ -0,0 +1,1821 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * NTFS named stream handling.
+ *
+ * Copyright (c) 2026 LG Electronics Co., Ltd.
+ */
+
+#include <linux/file.h>
+#include <linux/namei.h>
+#include <linux/overflow.h>
+
+#include <uapi/linux/ntfs.h>
+
+#include "attrib.h"
+#include "dir.h"
+#include "iomap.h"
+#include "mft.h"
+#include "ntfs.h"
+#include "stream.h"
+#include "time.h"
+
+#define NTFS_STREAM_MAX_IO (16 * 1024 * 1024)
+
+/*
+ * ntfs_check_stream_name() - Validate a named-stream name
+ * @name: UTF-16LE stream name
+ * @name_len: Length of @name in UTF-16 code units
+ *
+ * Reject empty, overlong, separator-containing, or malformed UTF-16 names.
+ *
+ * Return: 0 if valid, or -EINVAL otherwise.
+ */
+int ntfs_check_stream_name(const __le16 *name, unsigned int name_len)
+{
+ unsigned int i;
+
+ if (!name_len || name_len > NTFS_MAX_NAME_LEN)
+ return -EINVAL;
+
+ for (i = 0; i < name_len; i++) {
+ u16 c = le16_to_cpu(name[i]);
+
+ if (c == 0 || c == '/' || c == '\\' || c == ':')
+ return -EINVAL;
+ if (c >= 0xd800 && c <= 0xdbff) {
+ if (++i >= name_len)
+ return -EINVAL;
+ c = le16_to_cpu(name[i]);
+ if (c < 0xdc00 || c > 0xdfff)
+ return -EINVAL;
+ } else if (c >= 0xdc00 && c <= 0xdfff) {
+ return -EINVAL;
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * ntfs_stream_inode_refresh() - Refresh base-derived stream inode metadata
+ * @vi: inode representing a named stream
+ *
+ * Synchronize ownership, mode, flags, timestamps, and generation with the
+ * base inode. Stream size and allocation remain specific to the stream.
+ */
+void ntfs_stream_inode_refresh(struct inode *vi)
+{
+ struct ntfs_inode *ni = NTFS_I(vi);
+ struct inode *base_vi;
+
+ if (!ntfs_inode_is_named_stream(ni) || ni->nr_extents != -1)
+ return;
+
+ base_vi = VFS_I(ni->ext.base_ntfs_ino);
+ vi->i_uid = base_vi->i_uid;
+ vi->i_gid = base_vi->i_gid;
+ vi->i_mode = (base_vi->i_mode & ~S_IFMT) | S_IFREG;
+ vi->i_flags = base_vi->i_flags;
+ inode_set_mtime_to_ts(vi, inode_get_mtime(base_vi));
+ inode_set_ctime_to_ts(vi, inode_get_ctime(base_vi));
+ inode_set_atime_to_ts(vi, inode_get_atime(base_vi));
+ vi->i_generation = base_vi->i_generation;
+}
+
+/*
+ * ntfs_stream_update_base_time() - Update base timestamps after a stream change
+ * @base_ni: base inode containing the stream attribute
+ *
+ * Update the base inode's mtime and ctime and mark it dirty. The helper takes
+ * the base inode lock.
+ */
+void ntfs_stream_update_base_time(struct ntfs_inode *base_ni)
+{
+ struct inode *base_vi = VFS_I(base_ni);
+
+ inode_lock_nested(base_vi, I_MUTEX_PARENT);
+ inode_set_mtime_to_ts(base_vi, inode_set_ctime_current(base_vi));
+ mark_inode_dirty(base_vi);
+ inode_unlock(base_vi);
+}
+
+/*
+ * ntfs_stream_inode_validate() - Validate a named-stream inode
+ * @vi: stream inode to validate
+ *
+ * Verify that @vi still maps to its named DATA attribute and refresh its
+ * base-derived metadata. The caller must hold the base inode's MFT-record
+ * lock.
+ *
+ * Return: 0 if valid, or a negative errno.
+ */
+int ntfs_stream_inode_validate(struct inode *vi)
+{
+ struct ntfs_inode *ni = NTFS_I(vi);
+ struct ntfs_inode *base_ni;
+ struct ntfs_attr_search_ctx *ctx;
+ int err;
+
+ if (!ntfs_inode_is_named_stream(ni) || ni->nr_extents != -1)
+ return -EINVAL;
+
+ base_ni = ni->ext.base_ntfs_ino;
+ lockdep_assert_held(&base_ni->mrec_lock);
+ if (NVolShutdown(base_ni->vol))
+ return -EIO;
+ if (!NInoStreamUnlinked(ni) &&
+ (!vi->i_nlink || !VFS_I(base_ni)->i_nlink ||
+ NInoBeingDeleted(base_ni)))
+ return -ESTALE;
+
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
+ if (!ctx)
+ return -ENOMEM;
+ err = ntfs_attr_lookup(AT_DATA, ni->name, ni->name_len,
+ CASE_SENSITIVE, 0, NULL, 0, ctx);
+ ntfs_attr_put_search_ctx(ctx);
+ if (err)
+ return err;
+
+ ntfs_stream_inode_refresh(vi);
+ return 0;
+}
+
+/*
+ * ntfs_stream_unlinked() - Check a cached stream's deferred-unlink state
+ * @base_ni: base inode containing the stream
+ * @name: UTF-16LE stream name
+ * @name_len: Length of @name in UTF-16 code units
+ *
+ * The caller must hold the base inode's MFT-record lock.
+ *
+ * Return: true if the cached stream inode is unlinked, or false if it is not
+ * cached or is still linked.
+ */
+bool ntfs_stream_unlinked(struct ntfs_inode *base_ni,
+ const __le16 *name, u32 name_len)
+{
+ struct ntfs_attr na = {
+ .mft_no = base_ni->mft_no,
+ .type = AT_DATA,
+ .name = (__le16 *)name,
+ .name_len = name_len,
+ };
+ struct inode *vi;
+ bool unlinked;
+
+ lockdep_assert_held(&base_ni->mrec_lock);
+ rcu_read_lock();
+ vi = find_inode_rcu(base_ni->vol->sb, na.mft_no,
+ ntfs_test_inode_rcu, &na);
+ if (!vi) {
+ rcu_read_unlock();
+ return false;
+ }
+ unlinked = NInoStreamUnlinked(NTFS_I(vi));
+ rcu_read_unlock();
+ return unlinked;
+}
+
+/*
+ * ntfs_stream_path_parse() - Split a pathname stream component
+ * @vol: NTFS volume
+ * @qname: final pathname component
+ * @path: receives slices of @qname on success
+ *
+ * Parse the ``file:stream`` form when the Windows pathname interface is
+ * enabled. This does not convert or validate the component names.
+ *
+ * Return: 1 if stream syntax was parsed, 0 if streams are disabled or no
+ * stream separator is present, or -EINVAL for malformed syntax.
+ */
+int ntfs_stream_path_parse(struct ntfs_volume *vol,
+ const struct qstr *qname, struct ntfs_stream_path *path)
+{
+ const unsigned char *colon;
+
+ if (!NVolStreamsWindows(vol))
+ return 0;
+
+ colon = memchr(qname->name, ':', qname->len);
+ if (!colon)
+ return 0;
+ if (colon == qname->name || colon + 1 == qname->name + qname->len)
+ return -EINVAL;
+ if (memchr(colon + 1, ':', qname->name + qname->len - colon - 1))
+ return -EINVAL;
+
+ path->base_name = (const char *)qname->name;
+ path->base_len = colon - qname->name;
+ path->stream_name = (const char *)colon + 1;
+ path->stream_len = qname->name + qname->len - colon - 1;
+ return 1;
+}
+
+/*
+ * ntfs_stream_path_has_colon() - Check for a pathname stream separator
+ * @vol: NTFS volume
+ * @qname: final pathname component
+ *
+ * Return: true if the Windows pathname interface is enabled and @qname
+ * contains a colon, or false otherwise.
+ */
+bool ntfs_stream_path_has_colon(struct ntfs_volume *vol,
+ const struct qstr *qname)
+{
+ return NVolStreamsWindows(vol) &&
+ memchr(qname->name, ':', qname->len);
+}
+
+/*
+ * ntfs_stream_path_check() - Reject stream syntax for unsupported operations
+ * @vol: NTFS volume
+ * @qname: final pathname component
+ *
+ * Return: 0 for an ordinary name or when streams are disabled,
+ * -EOPNOTSUPP for parsed stream syntax, or -EINVAL for malformed syntax.
+ */
+int ntfs_stream_path_check(struct ntfs_volume *vol,
+ const struct qstr *qname)
+{
+ struct ntfs_stream_path path;
+ int ret;
+
+ ret = ntfs_stream_path_parse(vol, qname, &path);
+ if (ret < 0)
+ return ret;
+ return ret ? -EOPNOTSUPP : 0;
+}
+
+/*
+ * ntfs_stream_lookup_inode_by_name() - Look up a base inode by name
+ * @dir_ino: directory containing the base file or directory
+ * @uname: UTF-16LE base name
+ * @uname_len: Length of @uname in UTF-16 code units
+ *
+ * Resolve the directory entry and verify its MFT reference before returning
+ * the inode.
+ *
+ * Return: Referenced inode on success, or ERR_PTR() on failure.
+ */
+struct inode *ntfs_stream_lookup_inode_by_name(struct inode *dir_ino,
+ __le16 *uname, int uname_len)
+{
+ struct ntfs_volume *vol = NTFS_SB(dir_ino->i_sb);
+ struct ntfs_name *name = NULL;
+ struct inode *inode;
+ u64 mref;
+
+ mutex_lock(&NTFS_I(dir_ino)->mrec_lock);
+ mref = ntfs_lookup_inode_by_name(NTFS_I(dir_ino), uname, uname_len,
+ &name);
+ mutex_unlock(&NTFS_I(dir_ino)->mrec_lock);
+ kfree(name);
+
+ if (IS_ERR_MREF(mref))
+ return ERR_PTR(MREF_ERR(mref));
+
+ inode = ntfs_iget(vol->sb, MREF(mref));
+ if (IS_ERR(inode))
+ return inode;
+ if (MSEQNO(mref) != NTFS_I(inode)->seq_no &&
+ MREF(mref) != FILE_MFT) {
+ iput(inode);
+ return ERR_PTR(-EIO);
+ }
+ return inode;
+}
+
+/*
+ * Recheck that a stream dentry still names the same base inode and DATA
+ * attribute.
+ */
+static int ntfs_stream_d_revalidate(struct inode *dir,
+ const struct qstr *qname, struct dentry *dentry,
+ unsigned int flags)
+{
+ struct inode *inode = d_inode(dentry);
+ struct ntfs_inode *ni;
+ struct ntfs_stream_path path;
+ struct inode *base_vi;
+ __le16 *base_name = NULL, *stream_name = NULL;
+ int base_len, stream_len, err, ret = 0;
+
+ if (flags & LOOKUP_RCU)
+ return -ECHILD;
+ if (!inode)
+ return 0;
+ if (!inode->i_nlink)
+ return 0;
+ if (NVolShutdown(NTFS_SB(dir->i_sb)))
+ return 0;
+
+ ni = NTFS_I(inode);
+ if (!ntfs_inode_is_named_stream(ni))
+ return 0;
+
+ err = ntfs_stream_path_parse(NTFS_SB(dir->i_sb), qname, &path);
+ if (err <= 0)
+ return 0;
+ base_len = ntfs_nlstoucs(NTFS_SB(dir->i_sb), path.base_name,
+ path.base_len, &base_name, NTFS_MAX_NAME_LEN);
+ if (base_len < 0)
+ goto out;
+ stream_len = ntfs_nlstoucs(NTFS_SB(dir->i_sb), path.stream_name,
+ path.stream_len, &stream_name, NTFS_MAX_NAME_LEN);
+ if (stream_len < 0)
+ goto out;
+ if (ntfs_check_stream_name(stream_name, stream_len))
+ goto out;
+ if (stream_len != ni->name_len ||
+ !ntfs_names_are_equal(stream_name, stream_len, ni->name,
+ ni->name_len, IGNORE_CASE, ni->vol->upcase,
+ ni->vol->upcase_len))
+ goto out;
+
+ base_vi = ntfs_stream_lookup_inode_by_name(dir, base_name, base_len);
+ if (IS_ERR(base_vi))
+ goto out;
+ if (NTFS_I(base_vi) != ni->ext.base_ntfs_ino) {
+ iput(base_vi);
+ goto out;
+ }
+
+ mutex_lock(&NTFS_I(base_vi)->mrec_lock);
+ err = ntfs_stream_inode_validate(inode);
+ mutex_unlock(&NTFS_I(base_vi)->mrec_lock);
+ iput(base_vi);
+ if (!err)
+ ret = 1;
+out:
+ if (stream_name)
+ kmem_cache_free(ntfs_name_cache, stream_name);
+ if (base_name)
+ kmem_cache_free(ntfs_name_cache, base_name);
+ return ret;
+}
+
+/* Revalidate stream paths while leaving ordinary NTFS dentries cacheable. */
+static int ntfs_dentry_revalidate(struct inode *dir,
+ const struct qstr *qname, struct dentry *dentry,
+ unsigned int flags)
+{
+ struct inode *inode = d_inode(dentry);
+ struct ntfs_volume *vol = NTFS_SB(dir->i_sb);
+
+ if (ntfs_stream_path_has_colon(vol, qname))
+ return ntfs_stream_d_revalidate(dir, qname, dentry, flags);
+ if (inode && ntfs_inode_is_named_stream(NTFS_I(inode)))
+ return 0;
+ return 1;
+}
+
+/* Drop stream-path aliases so future lookups recheck their backing attribute. */
+static int ntfs_dentry_delete(const struct dentry *dentry)
+{
+ struct inode *inode = d_inode(dentry);
+ struct ntfs_volume *vol = NTFS_SB(dentry->d_sb);
+
+ if (ntfs_stream_path_has_colon(vol, &dentry->d_name) ||
+ (inode && ntfs_inode_is_named_stream(NTFS_I(inode))))
+ return always_delete_dentry(dentry);
+ return 0;
+}
+
+static const struct dentry_operations ntfs_dentry_ops = {
+ .d_revalidate = ntfs_dentry_revalidate,
+ .d_delete = ntfs_dentry_delete,
+};
+
+/*
+ * ntfs_set_default_dentry_ops() - Install stream-aware dentry operations
+ * @sb: superblock on which to install the operations
+ *
+ * Ensure pathname stream dentries are revalidated and not kept as stale
+ * aliases.
+ */
+void ntfs_set_default_dentry_ops(struct super_block *sb)
+{
+ set_default_d_op(sb, &ntfs_dentry_ops);
+}
+
+/*
+ * ntfs_lookup_stream() - Look up a named stream by pathname
+ * @dir_ino: directory containing the base file or directory
+ * @dent: dentry for the pathname component
+ * @path: parsed base and stream name slices
+ *
+ * Find the existing base inode and its named DATA attribute, then splice the
+ * stream inode into @dent.
+ *
+ * Return: NULL if @dent was instantiated, an alternate dentry if a
+ * disconnected alias was spliced, or ERR_PTR() on failure.
+ */
+struct dentry *ntfs_lookup_stream(struct inode *dir_ino,
+ struct dentry *dent, const struct ntfs_stream_path *path)
+{
+ struct ntfs_volume *vol = NTFS_SB(dir_ino->i_sb);
+ struct inode *base_vi, *stream_vi;
+ __le16 *base_uname = NULL, *stream_uname = NULL;
+ int base_len, stream_len, err;
+
+ if (NVolShutdown(vol))
+ return ERR_PTR(-EIO);
+
+ base_len = ntfs_nlstoucs(vol, path->base_name, path->base_len,
+ &base_uname, NTFS_MAX_NAME_LEN);
+ if (base_len < 0)
+ return ERR_PTR(base_len);
+
+ stream_len = ntfs_nlstoucs(vol, path->stream_name, path->stream_len,
+ &stream_uname, NTFS_MAX_NAME_LEN);
+ if (stream_len < 0) {
+ kmem_cache_free(ntfs_name_cache, base_uname);
+ return ERR_PTR(stream_len);
+ }
+ err = ntfs_check_stream_name(stream_uname, stream_len);
+ if (err) {
+ kmem_cache_free(ntfs_name_cache, base_uname);
+ kmem_cache_free(ntfs_name_cache, stream_uname);
+ return ERR_PTR(err);
+ }
+
+ base_vi = ntfs_stream_lookup_inode_by_name(dir_ino, base_uname,
+ base_len);
+ kmem_cache_free(ntfs_name_cache, base_uname);
+ if (IS_ERR(base_vi)) {
+ err = PTR_ERR(base_vi);
+ kmem_cache_free(ntfs_name_cache, stream_uname);
+ if (err == -ENOENT)
+ return d_splice_alias(NULL, dent);
+ return ERR_PTR(err);
+ }
+
+ if (!S_ISREG(base_vi->i_mode) && !S_ISDIR(base_vi->i_mode)) {
+ iput(base_vi);
+ kmem_cache_free(ntfs_name_cache, stream_uname);
+ return ERR_PTR(-ENOTDIR);
+ }
+
+ mutex_lock(&NTFS_I(base_vi)->mrec_lock);
+ stream_vi = ntfs_attr_iget(base_vi, AT_DATA, stream_uname, stream_len);
+ if (!IS_ERR(stream_vi)) {
+ if (NInoStreamUnlinked(NTFS_I(stream_vi)))
+ err = -ENOENT;
+ else
+ err = ntfs_stream_inode_validate(stream_vi);
+ }
+ mutex_unlock(&NTFS_I(base_vi)->mrec_lock);
+ if (!IS_ERR(stream_vi) && err) {
+ iput(stream_vi);
+ stream_vi = ERR_PTR(err);
+ }
+ iput(base_vi);
+ kmem_cache_free(ntfs_name_cache, stream_uname);
+
+ if (IS_ERR(stream_vi)) {
+ err = PTR_ERR(stream_vi);
+ if (err == -ENOENT || err == -ESTALE)
+ return d_splice_alias(NULL, dent);
+ return ERR_PTR(err);
+ }
+
+ return d_splice_alias(stream_vi, dent);
+}
+
+/*
+ * ntfs_stream_path_names() - Convert and validate pathname stream names
+ * @vol: NTFS volume
+ * @qname: final pathname component
+ * @base_name: receives the allocated UTF-16LE base name
+ * @base_len: receives the base name length in UTF-16 code units
+ * @stream_name: receives the allocated UTF-16LE stream name
+ * @stream_len: receives the stream name length in UTF-16 code units
+ *
+ * Return: 1 if stream names were produced, 0 if @qname has no stream syntax,
+ * or a negative errno. The caller owns both name buffers on success.
+ */
+int ntfs_stream_path_names(struct ntfs_volume *vol,
+ const struct qstr *qname, __le16 **base_name, int *base_len,
+ __le16 **stream_name, int *stream_len)
+{
+ struct ntfs_stream_path path;
+ int err;
+
+ *base_name = NULL;
+ *stream_name = NULL;
+ err = ntfs_stream_path_parse(vol, qname, &path);
+ if (err <= 0)
+ return err;
+
+ *base_len = ntfs_nlstoucs(vol, path.base_name, path.base_len,
+ base_name, NTFS_MAX_NAME_LEN);
+ if (*base_len < 0)
+ return *base_len;
+
+ *stream_len = ntfs_nlstoucs(vol, path.stream_name, path.stream_len,
+ stream_name, NTFS_MAX_NAME_LEN);
+ if (*stream_len < 0) {
+ kmem_cache_free(ntfs_name_cache, *base_name);
+ *base_name = NULL;
+ return *stream_len;
+ }
+
+ err = ntfs_check_bad_windows_name(vol, *base_name, *base_len);
+ if (err)
+ goto out_free;
+ err = ntfs_check_stream_name(*stream_name, *stream_len);
+ if (err)
+ goto out_free;
+ err = ntfs_check_bad_windows_name(vol, *stream_name, *stream_len);
+ if (err)
+ goto out_free;
+ return 1;
+
+out_free:
+ kmem_cache_free(ntfs_name_cache, *stream_name);
+ kmem_cache_free(ntfs_name_cache, *base_name);
+ *stream_name = NULL;
+ *base_name = NULL;
+ return err;
+}
+
+/*
+ * ntfs_create_named_stream() - Create a named DATA stream
+ * @idmap: mount idmap used for permission checks
+ * @dir: directory containing the base object
+ * @base_name: UTF-16LE base name
+ * @base_len: length of @base_name in UTF-16 code units
+ * @stream_name: UTF-16LE stream name
+ * @stream_len: length of @stream_name in UTF-16 code units
+ *
+ * Create the stream on an existing regular file or directory.
+ *
+ * Return: Referenced stream inode on success, or ERR_PTR() on failure.
+ */
+struct inode *ntfs_create_named_stream(struct mnt_idmap *idmap,
+ struct inode *dir, __le16 *base_name, int base_len,
+ __le16 *stream_name, int stream_len)
+{
+ struct ntfs_inode *base_ni;
+ struct inode *base_vi, *stream_vi;
+ struct inode *rollback_vi = NULL;
+ struct ntfs_attr_search_ctx *ctx;
+ bool stream_created = false;
+ int err, rollback_err;
+
+ base_vi = ntfs_stream_lookup_inode_by_name(dir, base_name, base_len);
+ if (IS_ERR(base_vi))
+ return base_vi;
+ if (!S_ISREG(base_vi->i_mode) && !S_ISDIR(base_vi->i_mode)) {
+ iput(base_vi);
+ return ERR_PTR(-ENOTDIR);
+ }
+ err = inode_permission(idmap, base_vi, MAY_OPEN | MAY_WRITE);
+ if (err)
+ goto out_iput;
+ if (IS_APPEND(base_vi) || IS_IMMUTABLE(base_vi)) {
+ err = -EPERM;
+ goto out_iput;
+ }
+ if (!(NTFS_SB(base_vi->i_sb)->vol_flags & VOLUME_IS_DIRTY)) {
+ err = ntfs_set_volume_flags(NTFS_SB(base_vi->i_sb),
+ VOLUME_IS_DIRTY);
+ if (err)
+ goto out_iput;
+ }
+
+ base_ni = NTFS_I(base_vi);
+ mutex_lock(&base_ni->mrec_lock);
+ if (NVolShutdown(base_ni->vol)) {
+ err = -EIO;
+ goto out_unlock;
+ }
+ if (NInoBeingDeleted(base_ni) || !base_vi->i_nlink) {
+ err = -ENOENT;
+ goto out_unlock;
+ }
+ if (IS_APPEND(base_vi) || IS_IMMUTABLE(base_vi)) {
+ err = -EPERM;
+ goto out_unlock;
+ }
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
+ if (!ctx) {
+ err = -ENOMEM;
+ goto out_unlock;
+ }
+
+ err = ntfs_attr_lookup(AT_DATA, stream_name, stream_len,
+ IGNORE_CASE, 0, NULL, 0, ctx);
+ if (!err) {
+ if (ntfs_stream_unlinked(base_ni, stream_name, stream_len))
+ err = -EBUSY;
+ else
+ err = -EEXIST;
+ } else if (err == -ENOENT) {
+ if (NVolShutdown(base_ni->vol)) {
+ err = -EIO;
+ goto out_put_ctx;
+ }
+ err = ntfs_attr_add(base_ni, AT_DATA, stream_name, stream_len,
+ NULL, 0);
+ if (!err) {
+ stream_created = true;
+ mark_mft_record_dirty(base_ni);
+ }
+ }
+ ntfs_attr_put_search_ctx(ctx);
+ if (err)
+ goto out_unlock;
+
+ stream_vi = ntfs_attr_iget(base_vi, AT_DATA, stream_name, stream_len);
+ if (!IS_ERR(stream_vi))
+ err = ntfs_stream_inode_validate(stream_vi);
+ else
+ err = PTR_ERR(stream_vi);
+ if (err) {
+ if (stream_created) {
+ rollback_err = ntfs_attr_remove(base_ni, AT_DATA,
+ stream_name, stream_len, &rollback_vi);
+ if (rollback_err) {
+ ntfs_error(base_ni->vol->sb,
+ "Failed to roll back named stream creation.\n");
+ if (rollback_err == -ENOMEM ||
+ rollback_err == -EINTR ||
+ rollback_err == -ERESTARTSYS) {
+ err = rollback_err;
+ } else {
+ NVolSetErrors(base_ni->vol);
+ NVolSetShutdown(base_ni->vol);
+ err = -EIO;
+ }
+ }
+ }
+ mutex_unlock(&base_ni->mrec_lock);
+ if (stream_created)
+ ntfs_stream_update_base_time(base_ni);
+ if (!IS_ERR(stream_vi))
+ iput(stream_vi);
+ if (rollback_vi)
+ iput(rollback_vi);
+ iput(base_vi);
+ return ERR_PTR(err);
+ }
+ mutex_unlock(&base_ni->mrec_lock);
+ if (stream_created)
+ ntfs_stream_update_base_time(base_ni);
+ iput(base_vi);
+ return stream_vi;
+
+out_put_ctx:
+ ntfs_attr_put_search_ctx(ctx);
+out_unlock:
+ mutex_unlock(&base_ni->mrec_lock);
+out_iput:
+ iput(base_vi);
+ return ERR_PTR(err);
+}
+
+/*
+ * ntfs_unlink_named_stream() - Remove a pathname named stream
+ * @dir: directory containing the base object
+ * @dentry: dentry for the named stream
+ *
+ * Return: 0 on success, or a negative errno.
+ */
+int ntfs_unlink_named_stream(struct inode *dir, struct dentry *dentry)
+{
+ struct ntfs_volume *vol = NTFS_SB(dir->i_sb);
+ struct inode *base_vi;
+ __le16 *base_name = NULL, *stream_name = NULL;
+ int base_len, stream_len, path_result, err;
+
+ path_result = ntfs_stream_path_names(vol, &dentry->d_name, &base_name,
+ &base_len, &stream_name, &stream_len);
+ if (path_result < 0)
+ return path_result;
+ if (!path_result)
+ return -EINVAL;
+
+ base_vi = ntfs_stream_lookup_inode_by_name(dir, base_name, base_len);
+ if (IS_ERR(base_vi)) {
+ err = PTR_ERR(base_vi);
+ goto out_free;
+ }
+ if (!S_ISREG(base_vi->i_mode) && !S_ISDIR(base_vi->i_mode)) {
+ err = -ENOTDIR;
+ goto out_iput;
+ }
+ if (!ntfs_inode_is_named_stream(NTFS_I(dentry->d_inode))) {
+ err = -ESTALE;
+ goto out_iput;
+ }
+ if (NTFS_I(dentry->d_inode)->ext.base_ntfs_ino != NTFS_I(base_vi)) {
+ err = -ESTALE;
+ goto out_iput;
+ }
+
+ /*
+ * VFS authorizes pathname removal via the parent directory, not base
+ * file write access. The shared helper enforces NTFS delete restrictions.
+ */
+ err = ntfs_remove_named_stream(NTFS_I(base_vi), stream_name,
+ stream_len, dentry->d_inode);
+
+out_iput:
+ iput(base_vi);
+out_free:
+ kmem_cache_free(ntfs_name_cache, stream_name);
+ kmem_cache_free(ntfs_name_cache, base_name);
+ return err;
+}
+
+/* Obtain a dentry for the base inode behind a stream inode. */
+static struct dentry *ntfs_stream_base_dentry(struct inode *inode)
+{
+ struct inode *base_vi =
+ VFS_I(NTFS_I(inode)->ext.base_ntfs_ino);
+ struct dentry *dentry;
+
+ dentry = d_find_alias(base_vi);
+ if (dentry)
+ return dentry;
+ if (!igrab(base_vi))
+ return ERR_PTR(-ESTALE);
+ return d_obtain_alias(base_vi);
+}
+
+/*
+ * ntfs_stream_validate_for_mutation() - Validate a stream before mutation
+ * @inode: stream inode to validate
+ * @nowait: do not wait for the base MFT-record lock
+ *
+ * Return: 0 if the backing DATA attribute is valid, -EAGAIN if a nonblocking
+ * lock attempt fails, or another negative errno.
+ */
+int ntfs_stream_validate_for_mutation(struct inode *inode, bool nowait)
+{
+ struct ntfs_inode *base_ni = NTFS_I(inode)->ext.base_ntfs_ino;
+ int err;
+
+ if (nowait) {
+ if (!mutex_trylock(&base_ni->mrec_lock))
+ return -EAGAIN;
+ } else {
+ mutex_lock(&base_ni->mrec_lock);
+ }
+ err = ntfs_stream_inode_validate(inode);
+ mutex_unlock(&base_ni->mrec_lock);
+ return err;
+}
+
+/*
+ * Hold a temporary stream reference so unlink cannot remove its attribute
+ * while an operation is using it.
+ */
+static int ntfs_stream_claim(struct inode *inode)
+{
+ struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_inode *base_ni = ni->ext.base_ntfs_ino;
+ int err;
+
+ mutex_lock(&base_ni->mrec_lock);
+ if (NInoStreamUnlinked(ni))
+ err = -ESTALE;
+ else
+ err = ntfs_stream_inode_validate(inode);
+ if (!err) {
+ if (atomic_read(&ni->stream_open_count) < 0)
+ err = -ESTALE;
+ else
+ atomic_inc(&ni->stream_open_count);
+ }
+ mutex_unlock(&base_ni->mrec_lock);
+ return err;
+}
+
+/* Stream permission checks use the base inode's permissions. */
+static int ntfs_stream_permission(struct mnt_idmap *idmap,
+ struct inode *inode, int mask)
+{
+ return inode_permission(idmap,
+ VFS_I(NTFS_I(inode)->ext.base_ntfs_ino), mask);
+}
+
+/* Report base metadata with the stream's own size and allocation. */
+static int ntfs_stream_getattr(struct mnt_idmap *idmap,
+ const struct path *path, struct kstat *stat,
+ unsigned int request_mask, unsigned int query_flags)
+{
+ struct inode *inode = d_backing_inode(path->dentry);
+ struct ntfs_inode *ni = NTFS_I(inode);
+ struct inode *base_vi = VFS_I(ni->ext.base_ntfs_ino);
+
+ generic_fillattr(idmap, request_mask, base_vi, stat);
+ stat->size = i_size_read(inode);
+ stat->blocks = (((u64)ni->i_dealloc_clusters <<
+ NTFS_SB(inode->i_sb)->cluster_size_bits) >> 9) +
+ inode->i_blocks;
+ stat->blksize = NTFS_SB(inode->i_sb)->cluster_size;
+ stat->result_mask |= STATX_BTIME;
+ stat->btime = NTFS_I(base_vi)->i_crtime;
+
+ if (NInoCompressed(ni))
+ stat->attributes |= STATX_ATTR_COMPRESSED;
+ if (NInoEncrypted(ni))
+ stat->attributes |= STATX_ATTR_ENCRYPTED;
+ if (base_vi->i_flags & S_IMMUTABLE)
+ stat->attributes |= STATX_ATTR_IMMUTABLE;
+ if (base_vi->i_flags & S_APPEND)
+ stat->attributes |= STATX_ATTR_APPEND;
+ stat->attributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED |
+ STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND;
+ stat->mode = (stat->mode & ~S_IFMT) | S_IFREG;
+
+ if (request_mask & STATX_DIOALIGN) {
+ unsigned int align =
+ bdev_logical_block_size(inode->i_sb->s_bdev);
+
+ stat->result_mask |= STATX_DIOALIGN;
+ if (!NInoCompressed(ni) && !NInoEncrypted(ni)) {
+ stat->dio_mem_align = align;
+ stat->dio_offset_align = align;
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * Apply size changes to the stream and route shared inode metadata changes to
+ * its base inode.
+ */
+static int ntfs_stream_setattr_common(struct mnt_idmap *idmap,
+ struct inode *inode, struct iattr *attr)
+{
+ struct ntfs_inode *ni = NTFS_I(inode);
+ struct inode *base_vi = VFS_I(ni->ext.base_ntfs_ino);
+ struct dentry *base_dentry = NULL;
+ struct iattr base_attr = *attr;
+ int err;
+
+ base_attr.ia_valid &= ~ATTR_FILE;
+ base_attr.ia_file = NULL;
+ if (base_attr.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
+ base_attr.ia_valid &= ~ATTR_MODE;
+ else if (base_attr.ia_valid & ATTR_MODE)
+ base_attr.ia_mode = (base_attr.ia_mode & ~S_IFMT) |
+ (base_vi->i_mode & S_IFMT);
+
+ if (attr->ia_valid & ATTR_SIZE) {
+ err = inode_permission(idmap, base_vi, MAY_WRITE);
+ if (err)
+ goto out;
+ if (IS_APPEND(base_vi) || IS_IMMUTABLE(base_vi)) {
+ err = -EPERM;
+ goto out;
+ }
+ if (!(ni->vol->vol_flags & VOLUME_IS_DIRTY)) {
+ err = ntfs_set_volume_flags(ni->vol, VOLUME_IS_DIRTY);
+ if (err)
+ goto out;
+ }
+ base_attr.ia_valid &= ~ATTR_SIZE;
+ }
+
+ if ((attr->ia_valid & ATTR_SIZE) || base_attr.ia_valid) {
+ base_dentry = ntfs_stream_base_dentry(inode);
+ if (IS_ERR(base_dentry)) {
+ err = PTR_ERR(base_dentry);
+ base_dentry = NULL;
+ goto out;
+ }
+ inode_lock_nested(base_vi, I_MUTEX_PARENT);
+ err = ntfs_stream_validate_for_mutation(inode, false);
+ if (err) {
+ inode_unlock(base_vi);
+ goto out;
+ }
+ if (base_attr.ia_valid)
+ err = notify_change(idmap, base_dentry, &base_attr,
+ NULL);
+ else
+ err = 0;
+ if (!err)
+ ntfs_stream_inode_refresh(inode);
+ inode_unlock(base_vi);
+ if (err)
+ goto out;
+ }
+
+ if (attr->ia_valid & ATTR_SIZE)
+ err = ntfs_setattr_size(inode, attr);
+ else
+ err = 0;
+out:
+ dput(base_dentry);
+ return err;
+}
+
+/* Hold the stream against unlink while applying VFS setattr operations. */
+static int ntfs_stream_setattr(struct mnt_idmap *idmap,
+ struct dentry *dentry, struct iattr *attr)
+{
+ struct inode *inode = d_inode(dentry);
+ struct ntfs_inode *ni = NTFS_I(inode);
+ bool claimed = false;
+ int err;
+
+ if (NVolShutdown(ni->vol))
+ return -EIO;
+
+ if (!(attr->ia_valid & ATTR_FILE)) {
+ err = ntfs_stream_claim(inode);
+ if (err)
+ return err;
+ claimed = true;
+ }
+
+ err = ntfs_stream_setattr_common(idmap, inode, attr);
+ if (claimed)
+ ntfs_stream_put(inode);
+ return err;
+}
+
+/*
+ * Apply stream timestamp updates to the base inode after validating the
+ * backing attribute under its MFT-record lock. Nonblocking callers get
+ * -EAGAIN.
+ */
+static int ntfs_stream_update_time_common(struct inode *inode,
+ enum fs_update_time type, unsigned int flags)
+{
+ struct ntfs_inode *base_ni = NTFS_I(inode)->ext.base_ntfs_ino;
+ struct inode *base_vi = VFS_I(base_ni);
+ int err;
+
+ if (NVolShutdown(base_ni->vol))
+ return -EIO;
+ if (flags & IOCB_NOWAIT)
+ return -EAGAIN;
+
+ mutex_lock(&base_ni->mrec_lock);
+ if (NVolShutdown(base_ni->vol)) {
+ err = -EIO;
+ goto out_mrec;
+ }
+ err = ntfs_stream_inode_validate(inode);
+ if (!err) {
+ err = generic_update_time(base_vi, type, flags);
+ if (!err)
+ ntfs_stream_inode_refresh(inode);
+ }
+out_mrec:
+ mutex_unlock(&base_ni->mrec_lock);
+ return err;
+}
+
+static int ntfs_stream_update_time(struct inode *inode,
+ enum fs_update_time type, unsigned int flags)
+{
+ return ntfs_stream_update_time_common(inode, type, flags);
+}
+
+static ssize_t ntfs_stream_listxattr(struct dentry *dentry, char *buffer,
+ size_t size)
+{
+ return -EOPNOTSUPP;
+}
+
+#ifdef CONFIG_NTFS_FS_POSIX_ACL
+static struct posix_acl *ntfs_stream_get_acl(struct mnt_idmap *idmap,
+ struct dentry *dentry, int type)
+{
+ return ERR_PTR(-EOPNOTSUPP);
+}
+
+static int ntfs_stream_set_acl(struct mnt_idmap *idmap,
+ struct dentry *dentry, struct posix_acl *acl, int type)
+{
+ return -EOPNOTSUPP;
+}
+#endif
+
+/*
+ * Remove an unlinked stream's DATA attribute after its final active reference
+ * is released. Base-inode deletion handles the attribute when the base is
+ * already being removed.
+ */
+static int ntfs_stream_finish_remove(struct inode *attr_vi)
+{
+ struct ntfs_inode *attr_ni = NTFS_I(attr_vi);
+ struct ntfs_inode *ni = attr_ni->ext.base_ntfs_ino;
+ int err;
+
+ mutex_lock(&ni->mrec_lock);
+ if (!NInoStreamUnlinked(attr_ni) ||
+ atomic_cmpxchg(&attr_ni->stream_open_count, 0, -1)) {
+ err = 0;
+ goto out_unlock;
+ }
+ if (NInoBeingDeleted(ni) || !VFS_I(ni)->i_nlink) {
+ remove_inode_hash(attr_vi);
+ err = 0;
+ goto out_unlock;
+ }
+ mutex_unlock(&ni->mrec_lock);
+
+ truncate_inode_pages(attr_vi->i_mapping, 0);
+
+ mutex_lock(&ni->mrec_lock);
+ if (NVolShutdown(ni->vol)) {
+ err = -EIO;
+ goto out_unlock;
+ }
+ if (NInoBeingDeleted(ni) || !VFS_I(ni)->i_nlink) {
+ remove_inode_hash(attr_vi);
+ err = 0;
+ goto out_unlock;
+ }
+ err = ntfs_attr_rm(attr_ni);
+ if (!err) {
+ NInoClearDirty(attr_ni);
+ remove_inode_hash(attr_vi);
+ } else {
+ NInoSetBeingDeleted(attr_ni);
+ remove_inode_hash(attr_vi);
+ }
+
+out_unlock:
+ mutex_unlock(&ni->mrec_lock);
+ return err;
+}
+
+/*
+ * ntfs_stream_put() - Release a stream operation reference
+ * @vi: stream inode whose reference is being released
+ *
+ * Finish a deferred unlink when this is the last reference to an unlinked
+ * stream.
+ *
+ * Return: 0 on success, or a negative errno if deferred removal fails.
+ */
+int ntfs_stream_put(struct inode *vi)
+{
+ struct ntfs_inode *ni = NTFS_I(vi);
+
+ if (atomic_dec_and_test(&ni->stream_open_count) &&
+ NInoStreamUnlinked(ni))
+ return ntfs_stream_finish_remove(vi);
+ return 0;
+}
+
+/*
+ * ntfs_remove_named_stream() - Remove a named DATA stream
+ * @ni: base inode containing the stream
+ * @uname: UTF-16LE stream name
+ * @uname_len: length of @uname in UTF-16 code units
+ * @expected_vi: expected cached stream inode, or NULL
+ *
+ * Refuse removal while the stream is open. If @expected_vi is non-NULL, it
+ * must be the inode currently associated with the named attribute.
+ *
+ * Return: 0 on success, -ENOENT if the stream is absent, or another negative
+ * errno.
+ */
+int ntfs_remove_named_stream(struct ntfs_inode *ni, __le16 *uname,
+ u32 uname_len, struct inode *expected_vi)
+{
+ struct inode *attr_vi;
+ struct ntfs_inode *attr_ni;
+ int err;
+
+ if (!ni || !uname || uname_len == 0)
+ return -EINVAL;
+ if (NVolShutdown(ni->vol))
+ return -EIO;
+ if ((ni->flags & FILE_ATTR_READONLY) ||
+ IS_APPEND(VFS_I(ni)) || IS_IMMUTABLE(VFS_I(ni)))
+ return -EPERM;
+ if (!(ni->vol->vol_flags & VOLUME_IS_DIRTY)) {
+ err = ntfs_set_volume_flags(ni->vol, VOLUME_IS_DIRTY);
+ if (err)
+ return err;
+ }
+
+ mutex_lock(&ni->mrec_lock);
+ if (NInoBeingDeleted(ni) || !VFS_I(ni)->i_nlink) {
+ err = -ENOENT;
+ goto out_unlock;
+ }
+ attr_vi = ntfs_attr_iget(VFS_I(ni), AT_DATA, uname, uname_len);
+ if (IS_ERR(attr_vi)) {
+ err = PTR_ERR(attr_vi);
+ goto out_unlock;
+ }
+ if (expected_vi && attr_vi != expected_vi) {
+ err = -ESTALE;
+ goto out_iput;
+ }
+
+ attr_ni = NTFS_I(attr_vi);
+ if (NInoStreamUnlinked(attr_ni)) {
+ err = -ENOENT;
+ goto out_iput;
+ }
+ if (atomic_read(&attr_ni->stream_open_count) > 0) {
+ err = -EBUSY;
+ goto out_iput;
+ }
+ err = ntfs_stream_inode_validate(attr_vi);
+ if (err)
+ goto out_iput;
+
+ NInoSetStreamUnlinked(attr_ni);
+ clear_nlink(attr_vi);
+ mutex_unlock(&ni->mrec_lock);
+
+ err = ntfs_stream_finish_remove(attr_vi);
+ if (!err)
+ ntfs_stream_update_base_time(ni);
+ iput(attr_vi);
+ return err;
+
+out_iput:
+ mutex_unlock(&ni->mrec_lock);
+ iput(attr_vi);
+ return err;
+out_unlock:
+ mutex_unlock(&ni->mrec_lock);
+ return err;
+}
+
+/*
+ * Validate access against the base inode and hold the stream against unlink
+ * until the file is released.
+ */
+static int ntfs_stream_file_open(struct inode *inode, struct file *file)
+{
+ struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_inode *base_ni = ni->ext.base_ntfs_ino;
+ struct inode *base_vi = VFS_I(base_ni);
+ int mask = MAY_OPEN;
+ int err;
+
+ if (file->f_mode & FMODE_READ)
+ mask |= MAY_READ;
+ if (file->f_mode & FMODE_WRITE)
+ mask |= MAY_WRITE;
+ err = inode_permission(file_mnt_idmap(file), base_vi, mask);
+ if (err)
+ return err;
+
+ mutex_lock(&base_ni->mrec_lock);
+ if (NInoStreamUnlinked(ni))
+ err = -ENOENT;
+ else
+ err = ntfs_stream_inode_validate(inode);
+ if (err)
+ goto out_unlock;
+ if ((file->f_mode & FMODE_WRITE) &&
+ (IS_IMMUTABLE(base_vi) ||
+ (IS_APPEND(base_vi) && !(file->f_flags & O_APPEND)))) {
+ err = -EPERM;
+ goto out_unlock;
+ }
+ if ((file->f_flags & O_TRUNC) && IS_APPEND(base_vi)) {
+ err = -EPERM;
+ goto out_unlock;
+ }
+ if ((file->f_flags & O_NOATIME) &&
+ !inode_owner_or_capable(file_mnt_idmap(file), base_vi)) {
+ err = -EPERM;
+ goto out_unlock;
+ }
+ err = ntfs_file_open(inode, file);
+ if (err)
+ goto out_unlock;
+ if (file->f_mode & FMODE_WRITE) {
+ err = get_write_access(base_vi);
+ if (err)
+ goto out_unlock;
+ file->private_data = base_vi;
+ }
+
+ atomic_inc(&ni->stream_open_count);
+out_unlock:
+ mutex_unlock(&base_ni->mrec_lock);
+ return err;
+}
+
+/* Drop open-time references and complete any deferred stream unlink. */
+static int ntfs_stream_file_release(struct inode *inode, struct file *file)
+{
+ int err, remove_err;
+
+ err = ntfs_file_release(inode, file);
+ if (file->private_data) {
+ put_write_access(file->private_data);
+ file->private_data = NULL;
+ }
+ remove_err = ntfs_stream_put(inode);
+ if (!err)
+ err = remove_err;
+ return err;
+}
+
+enum ntfs_stream_ioctl_op {
+ NTFS_STREAM_IOCTL_READ,
+ NTFS_STREAM_IOCTL_WRITE,
+ NTFS_STREAM_IOCTL_REMOVE,
+};
+
+/*
+ * Look up or create an ioctl target stream and return its referenced inode.
+ */
+static int ntfs_stream_ioctl_get_inode(struct inode *base_vi, __le16 *sname,
+ int sname_len, bool create, struct inode **stream_vi)
+{
+ struct ntfs_inode *base_ni = NTFS_I(base_vi);
+ struct ntfs_attr_search_ctx *ctx;
+ struct inode *attr_vi = NULL;
+ bool created = false;
+ int err;
+
+ *stream_vi = NULL;
+
+ mutex_lock(&base_ni->mrec_lock);
+ if (NVolShutdown(base_ni->vol)) {
+ err = -EIO;
+ goto out_unlock;
+ }
+ if (NInoBeingDeleted(base_ni) || !base_vi->i_nlink) {
+ err = -ENOENT;
+ goto out_unlock;
+ }
+
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
+ if (!ctx) {
+ err = -ENOMEM;
+ goto out_unlock;
+ }
+
+ err = ntfs_attr_lookup(AT_DATA, sname, sname_len, IGNORE_CASE,
+ 0, NULL, 0, ctx);
+ if (err == -ENOENT && create) {
+ err = ntfs_attr_add(base_ni, AT_DATA, sname, sname_len,
+ NULL, 0);
+ if (!err) {
+ created = true;
+ mark_mft_record_dirty(base_ni);
+ }
+ }
+ ntfs_attr_put_search_ctx(ctx);
+ if (err)
+ goto out_unlock;
+
+ attr_vi = ntfs_attr_iget(base_vi, AT_DATA, sname, sname_len);
+ if (IS_ERR(attr_vi)) {
+ err = PTR_ERR(attr_vi);
+ attr_vi = NULL;
+ goto out_unlock;
+ }
+ if (NInoStreamUnlinked(NTFS_I(attr_vi)))
+ err = create ? -EBUSY : -ENOENT;
+ else
+ err = ntfs_stream_inode_validate(attr_vi);
+out_unlock:
+ mutex_unlock(&base_ni->mrec_lock);
+ if (created)
+ ntfs_stream_update_base_time(base_ni);
+ if (err) {
+ iput(attr_vi);
+ return err;
+ }
+
+ *stream_vi = attr_vi;
+ return 0;
+}
+
+/* Check file mode, base-inode flags, and the requested transfer range. */
+static int ntfs_stream_ioctl_access(struct file *filp,
+ enum ntfs_stream_ioctl_op op, loff_t pos, size_t len)
+{
+ struct inode *inode = file_inode(filp);
+ bool is_dir = S_ISDIR(inode->i_mode);
+ int err;
+
+ if (op == NTFS_STREAM_IOCTL_READ) {
+ if (!(filp->f_mode & FMODE_READ))
+ return -EBADF;
+ return rw_verify_area(READ, filp, &pos, len);
+ }
+
+ if (!(filp->f_mode & FMODE_WRITE) && !is_dir)
+ return -EBADF;
+ if (is_dir) {
+ err = inode_permission(file_mnt_idmap(filp), inode,
+ MAY_WRITE | MAY_EXEC);
+ if (err)
+ return err;
+ }
+ if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
+ return -EPERM;
+ if (op == NTFS_STREAM_IOCTL_REMOVE)
+ return 0;
+ return rw_verify_area(WRITE, filp, &pos, len);
+}
+
+/*
+ * Validate and execute a named-stream read, write, or remove ioctl request.
+ */
+static long ntfs_ioctl_stream(struct file *filp, unsigned long arg,
+ enum ntfs_stream_ioctl_op op)
+{
+ struct inode *base_vi = file_inode(filp);
+ struct ntfs_inode *base_ni = NTFS_I(base_vi);
+ struct ntfs_stream hdr;
+ struct ntfs_stream *req;
+ struct inode *stream_vi = NULL;
+ __le16 *uname = NULL, *sname;
+ size_t data_size, total_size;
+ loff_t pos;
+ s64 ret;
+ int sname_len, err;
+ bool claimed = false, got_write = false, utf16;
+
+ if (NVolShutdown(base_ni->vol))
+ return -EIO;
+ if (NInoAttr(base_ni) ||
+ (!S_ISREG(base_vi->i_mode) && !S_ISDIR(base_vi->i_mode)))
+ return -EOPNOTSUPP;
+ if (copy_from_user(&hdr, (void __user *)arg, sizeof(hdr)))
+ return -EFAULT;
+
+ if (hdr.io_len > NTFS_STREAM_MAX_IO ||
+ (hdr.flags & ~NTFS_STREAM_FL_UTF16) || hdr.reserved)
+ return -EINVAL;
+ if (!hdr.name_len)
+ return -EINVAL;
+
+ utf16 = hdr.flags & NTFS_STREAM_FL_UTF16;
+ if (utf16) {
+ if ((hdr.name_len & 1) ||
+ hdr.name_len > NTFS_MAX_NAME_LEN * sizeof(__le16))
+ return -EINVAL;
+ } else if (hdr.name_len >
+ NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE) {
+ return -EINVAL;
+ }
+
+ switch (op) {
+ case NTFS_STREAM_IOCTL_READ:
+ case NTFS_STREAM_IOCTL_WRITE:
+ if (!hdr.io_len)
+ return -EINVAL;
+ if (hdr.stream_offset > S64_MAX ||
+ hdr.stream_offset > S64_MAX - hdr.io_len)
+ return -EOVERFLOW;
+ data_size = hdr.io_len;
+ break;
+ case NTFS_STREAM_IOCTL_REMOVE:
+ if (hdr.io_len || hdr.stream_offset)
+ return -EINVAL;
+ data_size = 0;
+ break;
+ default:
+ return -ENOTTY;
+ }
+
+ if (check_add_overflow(sizeof(hdr), (size_t)hdr.name_len,
+ &total_size) ||
+ check_add_overflow(total_size, data_size, &total_size))
+ return -EOVERFLOW;
+
+ if (op == NTFS_STREAM_IOCTL_READ) {
+ req = kvmalloc(total_size, GFP_KERNEL);
+ if (!req)
+ return -ENOMEM;
+ if (copy_from_user(req, (void __user *)arg,
+ sizeof(hdr) + hdr.name_len)) {
+ kvfree(req);
+ return -EFAULT;
+ }
+ } else {
+ req = vmemdup_user((void __user *)arg, total_size);
+ if (IS_ERR(req))
+ return PTR_ERR(req);
+ }
+
+ req->bytes_returned = 0;
+ if (req->stream_offset != hdr.stream_offset ||
+ req->io_len != hdr.io_len || req->name_len != hdr.name_len ||
+ req->flags != hdr.flags || req->reserved) {
+ err = -EINVAL;
+ goto out_free;
+ }
+
+ if (utf16) {
+ sname = (__le16 *)req->buffer;
+ sname_len = req->name_len / sizeof(__le16);
+ } else {
+ if (memchr(req->buffer, '\0', req->name_len)) {
+ err = -EINVAL;
+ goto out_free;
+ }
+ sname_len = ntfs_nlstoucs(base_ni->vol, req->buffer,
+ req->name_len, &uname, NTFS_MAX_NAME_LEN);
+ if (sname_len < 0) {
+ err = sname_len;
+ goto out_free;
+ }
+ sname = uname;
+ }
+ err = ntfs_check_stream_name(sname, sname_len);
+ if (err)
+ goto out_free;
+
+ pos = hdr.stream_offset;
+ err = ntfs_stream_ioctl_access(filp, op, pos, data_size);
+ if (err)
+ goto out_free;
+
+ if (op == NTFS_STREAM_IOCTL_REMOVE) {
+ err = mnt_want_write_file(filp);
+ if (err)
+ goto out_free;
+ err = ntfs_remove_named_stream(base_ni, sname, sname_len, NULL);
+ mnt_drop_write_file(filp);
+ goto out_free;
+ }
+
+ if (op == NTFS_STREAM_IOCTL_WRITE) {
+ err = mnt_want_write_file(filp);
+ if (err)
+ goto out_free;
+ got_write = true;
+ inode_lock(base_vi);
+ err = file_remove_privs(filp);
+ inode_unlock(base_vi);
+ if (err)
+ goto out_drop_write;
+ if (!(base_ni->vol->vol_flags & VOLUME_IS_DIRTY)) {
+ err = ntfs_set_volume_flags(base_ni->vol,
+ VOLUME_IS_DIRTY);
+ if (err)
+ goto out_drop_write;
+ }
+ }
+
+ err = ntfs_stream_ioctl_get_inode(base_vi, sname, sname_len,
+ op == NTFS_STREAM_IOCTL_WRITE, &stream_vi);
+ if (err)
+ goto out_drop_write;
+
+ err = ntfs_stream_claim(stream_vi);
+ if (err)
+ goto out_drop_write;
+ claimed = true;
+
+ if (op == NTFS_STREAM_IOCTL_READ) {
+ inode_lock_shared(stream_vi);
+ if (pos < i_size_read(stream_vi))
+ ret = ntfs_inode_attr_pread(stream_vi, pos, data_size,
+ req->buffer + req->name_len);
+ else
+ ret = 0;
+ inode_unlock_shared(stream_vi);
+ if (ret < 0)
+ err = ret;
+ else
+ req->bytes_returned = ret;
+ if (!err)
+ file_accessed(filp);
+ } else {
+ inode_lock(stream_vi);
+ inode_dio_wait(stream_vi);
+ /* Exclude faults before taking MFT record and folio locks. */
+ filemap_invalidate_lock(stream_vi->i_mapping);
+ err = inode_newsize_ok(stream_vi, pos + data_size);
+ if (!err) {
+ ret = ntfs_inode_attr_pwrite(stream_vi, pos, data_size,
+ req->buffer + req->name_len, false);
+ if (ret < 0)
+ err = ret;
+ else
+ req->bytes_returned = ret;
+ }
+ filemap_invalidate_unlock(stream_vi->i_mapping);
+ inode_unlock(stream_vi);
+ }
+ if (claimed) {
+ int put_err;
+
+ put_err = ntfs_stream_put(stream_vi);
+ claimed = false;
+ if (!err && put_err)
+ err = put_err;
+ }
+ iput(stream_vi);
+ stream_vi = NULL;
+ if (err)
+ goto out_drop_write;
+
+ if (op == NTFS_STREAM_IOCTL_READ) {
+ if (copy_to_user((void __user *)arg, req,
+ sizeof(*req) + req->name_len +
+ req->bytes_returned))
+ err = -EFAULT;
+ } else if (copy_to_user((void __user *)arg +
+ offsetof(struct ntfs_stream, bytes_returned),
+ &req->bytes_returned, sizeof(req->bytes_returned))) {
+ err = -EFAULT;
+ }
+ goto out_drop_write;
+
+out_drop_write:
+ if (claimed)
+ ntfs_stream_put(stream_vi);
+ if (stream_vi)
+ iput(stream_vi);
+ if (got_write)
+ mnt_drop_write_file(filp);
+out_free:
+ if (uname)
+ kmem_cache_free(ntfs_name_cache, uname);
+ kvfree(req);
+ return err;
+}
+
+/*
+ * ntfs_ioctl_stream_read() - Handle a named-stream read ioctl
+ * @filp: file opened on the base file or directory
+ * @arg: userspace pointer to the request and data buffer
+ *
+ * Return: 0 on success, or a negative errno.
+ */
+long ntfs_ioctl_stream_read(struct file *filp, unsigned long arg)
+{
+ return ntfs_ioctl_stream(filp, arg, NTFS_STREAM_IOCTL_READ);
+}
+
+/*
+ * ntfs_ioctl_stream_write() - Handle a named-stream write ioctl
+ * @filp: file opened on the base file or directory
+ * @arg: userspace pointer to the request and data buffer
+ *
+ * Create the stream if needed.
+ *
+ * Return: 0 on success, or a negative errno.
+ */
+long ntfs_ioctl_stream_write(struct file *filp, unsigned long arg)
+{
+ return ntfs_ioctl_stream(filp, arg, NTFS_STREAM_IOCTL_WRITE);
+}
+
+/*
+ * ntfs_ioctl_stream_remove() - Handle a named-stream remove ioctl
+ * @filp: file opened on the base file or directory
+ * @arg: userspace pointer to the request
+ *
+ * Return: 0 on success, or a negative errno.
+ */
+long ntfs_ioctl_stream_remove(struct file *filp, unsigned long arg)
+{
+ return ntfs_ioctl_stream(filp, arg, NTFS_STREAM_IOCTL_REMOVE);
+}
+
+/*
+ * ntfs_ioctl_list_streams() - List named DATA streams
+ * @filp: file opened on the base file or directory
+ * @arg: userspace pointer to the request and output buffer
+ *
+ * On -ENOSPC, return the required buffer size in the request header.
+ *
+ * Return: 0 on success, or a negative errno.
+ */
+int ntfs_ioctl_list_streams(struct file *filp, unsigned long arg)
+{
+ struct inode *inode = file_inode(filp);
+ struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_list_streams hdr;
+ struct ntfs_stream_entry *entry, *last_entry = NULL;
+ size_t required = 0;
+ void *kbuf = NULL;
+ void __user *ubuf;
+ struct attr_record *a;
+ struct ntfs_attr_search_ctx *actx;
+ int ret = 0, err, count = 0;
+ size_t entry_size, offset = 0;
+ const size_t name_offset = offsetof(struct ntfs_stream_entry, name);
+ int name_len;
+ unsigned char *sn = NULL;
+ bool utf16;
+
+ if (NVolShutdown(ni->vol))
+ return -EIO;
+ if (NInoAttr(ni) ||
+ (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode)))
+ return -EOPNOTSUPP;
+ err = inode_permission(file_mnt_idmap(filp), inode, MAY_READ);
+ if (err)
+ return err;
+
+ if (copy_from_user(&hdr, (void __user *)arg, sizeof(hdr)))
+ return -EFAULT;
+
+ if ((hdr.flags & ~NTFS_STREAM_FL_UTF16) || hdr.reserved)
+ return -EINVAL;
+
+ utf16 = hdr.flags & NTFS_STREAM_FL_UTF16;
+
+ ubuf = (void __user *)arg + sizeof(hdr);
+
+ mutex_lock(&ni->mrec_lock);
+ actx = ntfs_attr_get_search_ctx(ni, NULL);
+ if (!actx) {
+ mutex_unlock(&ni->mrec_lock);
+ return -ENOMEM;
+ }
+
+ while ((err = ntfs_attrs_walk(actx)) == 0) {
+ a = actx->attr;
+ if (a->type != AT_DATA || !a->name_length)
+ continue;
+ if (a->non_resident &&
+ a->data.non_resident.lowest_vcn)
+ continue;
+ if (ntfs_stream_unlinked(ni,
+ (__le16 *)((u8 *)a +
+ le16_to_cpu(a->name_offset)),
+ a->name_length))
+ continue;
+
+ if (utf16) {
+ name_len = a->name_length * sizeof(__le16);
+ } else {
+ name_len = ntfs_ucstonls(ni->vol,
+ (__le16 *)((u8 *)a +
+ le16_to_cpu(a->name_offset)),
+ a->name_length, &sn, 0);
+ if (name_len < 0) {
+ if (name_len == -EILSEQ ||
+ name_len == -ENAMETOOLONG)
+ continue;
+ ret = name_len;
+ goto out;
+ }
+ kfree(sn);
+ sn = NULL;
+ }
+
+ entry_size = ALIGN(name_offset + name_len, 8);
+
+ if (required > SIZE_MAX - entry_size) {
+ ret = -EOVERFLOW;
+ goto out;
+ }
+ required += entry_size;
+ count++;
+ }
+ if (err != -ENOENT) {
+ ret = err;
+ goto out;
+ }
+
+ hdr.stream_count = count;
+ hdr.bytes_returned = required;
+
+ if (!count)
+ goto out;
+
+ if (hdr.buffer_size < required) {
+ ret = -ENOSPC;
+ goto out;
+ }
+
+ kbuf = kvzalloc(required, GFP_NOFS);
+ if (!kbuf) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ ntfs_attr_reinit_search_ctx(actx);
+ while ((err = ntfs_attrs_walk(actx)) == 0) {
+ a = actx->attr;
+ if (a->type != AT_DATA || !a->name_length)
+ continue;
+ if (a->non_resident &&
+ a->data.non_resident.lowest_vcn)
+ continue;
+ if (ntfs_stream_unlinked(ni,
+ (__le16 *)((u8 *)a +
+ le16_to_cpu(a->name_offset)),
+ a->name_length))
+ continue;
+ if (utf16) {
+ sn = NULL;
+ name_len = a->name_length * sizeof(__le16);
+ } else {
+ name_len = ntfs_ucstonls(ni->vol,
+ (__le16 *)((u8 *)a +
+ le16_to_cpu(a->name_offset)),
+ a->name_length, &sn, 0);
+ if (name_len < 0) {
+ if (name_len == -EILSEQ ||
+ name_len == -ENAMETOOLONG)
+ continue;
+ ret = name_len;
+ goto out;
+ }
+ }
+
+ entry_size = ALIGN(name_offset + name_len, 8);
+ if (offset > required - entry_size) {
+ ret = -EOVERFLOW;
+ kfree(sn);
+ sn = NULL;
+ goto out;
+ }
+
+ entry = (struct ntfs_stream_entry *)(kbuf + offset);
+
+ if (a->non_resident) {
+ entry->size = le64_to_cpu(a->data.non_resident.data_size);
+ entry->alloc_size =
+ le64_to_cpu(a->data.non_resident.allocated_size);
+ } else {
+ entry->size = le32_to_cpu(a->data.resident.value_length);
+ entry->alloc_size = le32_to_cpu(a->length) -
+ le16_to_cpu(a->data.resident.value_offset);
+ }
+
+ entry->name_len = name_len;
+ entry->name_offset = name_offset;
+ if (utf16)
+ memcpy(entry->name,
+ (u8 *)a + le16_to_cpu(a->name_offset), name_len);
+ else
+ memcpy(entry->name, sn, name_len);
+ kfree(sn);
+ sn = NULL;
+
+ entry->next_entry_off = entry_size;
+ last_entry = entry;
+ offset += entry_size;
+ }
+ if (err != -ENOENT) {
+ ret = err;
+ } else {
+ /* The chain terminates at the last entry. */
+ if (last_entry)
+ last_entry->next_entry_off = 0;
+ hdr.bytes_returned = offset;
+ }
+
+out:
+ kfree(sn);
+ ntfs_attr_put_search_ctx(actx);
+ mutex_unlock(&ni->mrec_lock);
+
+ if (ret && ret != -ENOSPC)
+ goto out_free;
+
+ if (!ret && kbuf && copy_to_user(ubuf, kbuf, hdr.bytes_returned)) {
+ ret = -EFAULT;
+ goto out_free;
+ }
+
+ if (copy_to_user((void __user *)arg, &hdr, sizeof(hdr)))
+ ret = -EFAULT;
+
+out_free:
+ kvfree(kbuf);
+ return ret;
+}
+
+const struct file_operations ntfs_stream_file_ops = {
+ .llseek = ntfs_file_llseek,
+ .read_iter = ntfs_file_read_iter,
+ .write_iter = ntfs_file_write_iter,
+ .fsync = ntfs_file_fsync,
+ .fallocate = ntfs_fallocate,
+ .mmap_prepare = ntfs_file_mmap_prepare,
+ .open = ntfs_stream_file_open,
+ .release = ntfs_stream_file_release,
+ .splice_read = ntfs_file_splice_read,
+ .splice_write = iter_file_splice_write,
+};
+
+const struct inode_operations ntfs_stream_inode_ops = {
+ .permission = ntfs_stream_permission,
+ .setattr = ntfs_stream_setattr,
+ .getattr = ntfs_stream_getattr,
+ .listxattr = ntfs_stream_listxattr,
+#ifdef CONFIG_NTFS_FS_POSIX_ACL
+ .get_acl = ntfs_stream_get_acl,
+ .set_acl = ntfs_stream_set_acl,
+#endif
+ .update_time = ntfs_stream_update_time,
+ .fiemap = ntfs_fiemap,
+};
diff --git a/fs/ntfs/namei.c b/fs/ntfs/namei.c
index 75e2010965257..9251951a76fb3 100644
--- a/fs/ntfs/namei.c
+++ b/fs/ntfs/namei.c
@@ -8,6 +8,7 @@
#include <linux/exportfs.h>
#include <linux/iversion.h>
+#include <linux/namei.h>
#include "ntfs.h"
#include "time.h"
@@ -15,6 +16,7 @@
#include "reparse.h"
#include "object_id.h"
#include "ea.h"
+#include "stream.h"
static const __le16 aux_name_le[3] = {
cpu_to_le16('A'), cpu_to_le16('U'), cpu_to_le16('X')
@@ -57,7 +59,18 @@ static inline int ntfs_check_bad_char(const __le16 *wc, unsigned int wc_len)
return 0;
}
-static int ntfs_check_bad_windows_name(struct ntfs_volume *vol,
+/*
+ * ntfs_check_bad_windows_name() - Validate a name against Windows rules
+ * @vol: NTFS volume
+ * @wc: UTF-16LE name
+ * @wc_len: length of @wc in UTF-16 code units
+ *
+ * When Windows-name checks are enabled, reject disallowed characters,
+ * trailing spaces or dots, and reserved DOS device names.
+ *
+ * Return: 0 if valid, or -EINVAL otherwise.
+ */
+int ntfs_check_bad_windows_name(struct ntfs_volume *vol,
const __le16 *wc,
unsigned int wc_len)
{
@@ -167,19 +180,29 @@ static int ntfs_check_bad_windows_name(struct ntfs_volume *vol,
*
* Locking: Caller must hold i_mutex on the directory.
*/
+
static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
unsigned int flags)
{
struct ntfs_volume *vol = NTFS_SB(dir_ino->i_sb);
+ struct ntfs_stream_path stream_path;
struct inode *dent_inode;
__le16 *uname;
struct ntfs_name *name = NULL;
u64 mref;
unsigned long dent_ino;
int uname_len;
+ int stream_path_len;
ntfs_debug("Looking up %pd in directory inode 0x%llx.",
dent, NTFS_I(dir_ino)->mft_no);
+ stream_path_len = ntfs_stream_path_parse(vol, &dent->d_name,
+ &stream_path);
+ if (stream_path_len < 0)
+ return ERR_PTR(stream_path_len);
+ if (stream_path_len)
+ return ntfs_lookup_stream(dir_ino, dent, &stream_path);
+
/* Convert the name of the dentry to Unicode. */
uname_len = ntfs_nlstoucs(vol, dent->d_name.name, dent->d_name.len,
&uname, NTFS_MAX_NAME_LEN);
@@ -405,6 +428,7 @@ static struct ntfs_inode *__ntfs_create(struct mnt_idmap *idmap, struct inode *d
struct inode *vi;
struct mft_record *ni_mrec, *dni_mrec;
struct super_block *sb = dir_ni->vol->sb;
+ struct inode *rollback_data_vi = NULL, *rollback_sd_vi = NULL;
__le64 parent_mft_ref;
u64 child_mft_ref;
__le16 ea_size;
@@ -694,11 +718,25 @@ static struct ntfs_inode *__ntfs_create(struct mnt_idmap *idmap, struct inode *d
return ni;
err_out:
- if (rollback_sd)
- ntfs_attr_remove(ni, AT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0);
+ if (rollback_sd) {
+ int rollback_err;
+
+ rollback_err = ntfs_attr_remove(ni,
+ AT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0,
+ &rollback_sd_vi);
+ if (rollback_err)
+ ntfs_error(sb,
+ "Failed to roll back security descriptor.\n");
+ }
+
+ if (rollback_data) {
+ int rollback_err;
- if (rollback_data)
- ntfs_attr_remove(ni, AT_DATA, AT_UNNAMED, 0);
+ rollback_err = ntfs_attr_remove(ni, AT_DATA, AT_UNNAMED,
+ 0, &rollback_data_vi);
+ if (rollback_err)
+ ntfs_error(sb, "Failed to roll back DATA attribute.\n");
+ }
if (rollback_reparse)
ntfs_delete_reparse_index(ni);
@@ -726,6 +764,10 @@ static struct ntfs_inode *__ntfs_create(struct mnt_idmap *idmap, struct inode *d
mutex_unlock(&dir_ni->mrec_lock);
mutex_unlock(&ni->mrec_lock);
+ if (rollback_data_vi)
+ iput(rollback_data_vi);
+ if (rollback_sd_vi)
+ iput(rollback_sd_vi);
remove_inode_hash(vi);
discard_new_inode(vi);
return ERR_PTR(err);
@@ -736,12 +778,35 @@ static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir,
{
struct ntfs_volume *vol = NTFS_SB(dir->i_sb);
struct ntfs_inode *ni;
- __le16 *uname;
- int uname_len, err;
+ struct inode *stream_vi = NULL;
+ __le16 *uname, *base_name = NULL, *stream_name = NULL;
+ int uname_len, stream_len, path_result, err;
if (NVolShutdown(vol))
return -EIO;
+ path_result = ntfs_stream_path_names(vol, &dentry->d_name, &base_name,
+ &uname_len, &stream_name, &stream_len);
+ if (path_result) {
+ if (path_result < 0)
+ return path_result;
+ stream_vi = ntfs_create_named_stream(idmap, dir, base_name,
+ uname_len, stream_name, stream_len);
+ if (IS_ERR(stream_vi)) {
+ err = PTR_ERR(stream_vi);
+ goto out_free_stream_names;
+ }
+ kmem_cache_free(ntfs_name_cache, stream_name);
+ kmem_cache_free(ntfs_name_cache, base_name);
+ d_instantiate(dentry, stream_vi);
+ return 0;
+
+out_free_stream_names:
+ kmem_cache_free(ntfs_name_cache, stream_name);
+ kmem_cache_free(ntfs_name_cache, base_name);
+ return err;
+ }
+
uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len,
&uname, NTFS_MAX_NAME_LEN);
if (uname_len < 0) {
@@ -758,7 +823,8 @@ static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir,
ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
- ni = __ntfs_create(idmap, dir, uname, uname_len, S_IFREG | mode, 0, NULL, 0);
+ ni = __ntfs_create(idmap, dir, uname, uname_len, S_IFREG | mode, 0,
+ NULL, 0);
kmem_cache_free(ntfs_name_cache, uname);
if (IS_ERR(ni))
return PTR_ERR(ni);
@@ -805,8 +871,29 @@ static int ntfs_test_inode_attr(struct inode *vi, void *data)
return 0;
if (NInoAttr(ni) || ni->nr_extents == -1)
return 1;
- else
- return 0;
+ return 0;
+}
+
+static void ntfs_cleanup_deleted_inode(struct ntfs_inode *ni)
+{
+ struct inode *attr_vi;
+ struct super_block *sb = VFS_I(ni)->i_sb;
+
+ if (!NInoBeingDeleted(ni))
+ return;
+
+ while ((attr_vi = ilookup5(sb, ni->mft_no, ntfs_test_inode_attr,
+ (void *)(uintptr_t)ni->mft_no))) {
+ struct ntfs_inode *attr_ni = NTFS_I(attr_vi);
+
+ if (ntfs_inode_is_named_stream(attr_ni)) {
+ if (atomic_read(&attr_ni->stream_open_count) > 0)
+ NInoSetStreamUnlinked(attr_ni);
+ remove_inode_hash(attr_vi);
+ }
+ clear_nlink(attr_vi);
+ iput(attr_vi);
+ }
}
/*
@@ -829,10 +916,10 @@ static int ntfs_delete(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,
struct file_name_attr *fn = NULL;
bool looking_for_dos_name = false, looking_for_win32_name = false;
bool case_sensitive_match = true;
+ bool link_count_zero = false;
int err = 0;
struct mft_record *ni_mrec;
struct super_block *sb;
- bool link_count_zero = false;
ntfs_debug("Entering.\n");
@@ -976,22 +1063,10 @@ static int ntfs_delete(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,
if (need_lock == true) {
mutex_unlock(&dir_ni->mrec_lock);
mutex_unlock(&ni->mrec_lock);
+ if (link_count_zero)
+ ntfs_cleanup_deleted_inode(ni);
}
- /*
- * If hard link count is not equal to zero then we are done. In other
- * case there are no reference to this inode left, so we should free all
- * non-resident attributes and mark all MFT record as not in use.
- */
- if (link_count_zero == true) {
- struct inode *attr_vi;
-
- while ((attr_vi = ilookup5(sb, ni->mft_no, ntfs_test_inode_attr,
- (void *)(uintptr_t)ni->mft_no)) != NULL) {
- clear_nlink(attr_vi);
- iput(attr_vi);
- }
- }
ntfs_debug("Done.\n");
return 0;
err_out:
@@ -1016,6 +1091,11 @@ static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
if (NVolShutdown(vol))
return -EIO;
+ if (ntfs_stream_path_has_colon(vol, &dentry->d_name))
+ return ntfs_unlink_named_stream(dir, dentry);
+ if (NInoAttr(ni))
+ return -EOPNOTSUPP;
+
uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len,
&uname, NTFS_MAX_NAME_LEN);
if (uname_len < 0) {
@@ -1059,6 +1139,10 @@ static struct dentry *ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
if (NVolShutdown(vol))
return ERR_PTR(-EIO);
+ err = ntfs_stream_path_check(vol, &dentry->d_name);
+ if (err)
+ return ERR_PTR(err);
+
uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len,
&uname, NTFS_MAX_NAME_LEN);
if (uname_len < 0) {
@@ -1075,7 +1159,8 @@ static struct dentry *ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
- ni = __ntfs_create(idmap, dir, uname, uname_len, mode, 0, NULL, 0);
+ ni = __ntfs_create(idmap, dir, uname, uname_len, mode, 0,
+ NULL, 0);
kmem_cache_free(ntfs_name_cache, uname);
if (IS_ERR(ni)) {
err = PTR_ERR(ni);
@@ -1099,6 +1184,10 @@ static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
if (NVolShutdown(vol))
return -EIO;
+ err = ntfs_stream_path_check(vol, &dentry->d_name);
+ if (err)
+ return err;
+
ni = NTFS_I(vi);
uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len,
&uname, NTFS_MAX_NAME_LEN);
@@ -1263,6 +1352,13 @@ static int ntfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
if (NVolShutdown(old_dir_ni->vol))
return -EIO;
+ err = ntfs_stream_path_check(vol, &old_dentry->d_name);
+ if (err)
+ return err;
+ err = ntfs_stream_path_check(vol, &new_dentry->d_name);
+ if (err)
+ return err;
+
if (flags & (RENAME_EXCHANGE | RENAME_WHITEOUT))
return -EINVAL;
@@ -1385,6 +1481,8 @@ static int ntfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
if (new_ni)
mutex_unlock(&new_ni->mrec_lock);
mutex_unlock(&old_ni->mrec_lock);
+ if (new_ni)
+ ntfs_cleanup_deleted_inode(new_ni);
if (uname_new)
kmem_cache_free(ntfs_name_cache, uname_new);
if (uname_old)
@@ -1408,6 +1506,10 @@ static int ntfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
if (NVolShutdown(vol))
return -EIO;
+ err = ntfs_stream_path_check(vol, &dentry->d_name);
+ if (err)
+ return err;
+
usrc_len = ntfs_nlstoucs(vol, dentry->d_name.name,
dentry->d_name.len, &usrc, NTFS_MAX_NAME_LEN);
if (usrc_len < 0) {
@@ -1453,6 +1555,10 @@ static int ntfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
if (NVolShutdown(vol))
return -EIO;
+ err = ntfs_stream_path_check(vol, &dentry->d_name);
+ if (err)
+ return err;
+
uname_len = ntfs_nlstoucs(vol, dentry->d_name.name,
dentry->d_name.len, &uname, NTFS_MAX_NAME_LEN);
if (uname_len < 0) {
@@ -1505,6 +1611,13 @@ static int ntfs_link(struct dentry *old_dentry, struct inode *dir,
if (NVolShutdown(vol))
return -EIO;
+ if (NInoAttr(ni))
+ return -EOPNOTSUPP;
+
+ err = ntfs_stream_path_check(vol, &dentry->d_name);
+ if (err)
+ return err;
+
uname_len = ntfs_nlstoucs(vol, dentry->d_name.name,
dentry->d_name.len, &uname, NTFS_MAX_NAME_LEN);
if (uname_len < 0) {
@@ -1658,11 +1771,19 @@ static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
ntfs_nfs_get_inode);
}
+static int ntfs_encode_fh(struct inode *inode, u32 *fh, int *max_len,
+ struct inode *parent)
+{
+ if (ntfs_inode_is_named_stream(NTFS_I(inode)))
+ return -EOPNOTSUPP;
+ return generic_encode_ino32_fh(inode, fh, max_len, parent);
+}
+
/*
* Export operations allowing NFS exporting of mounted NTFS partitions.
*/
const struct export_operations ntfs_export_ops = {
- .encode_fh = generic_encode_ino32_fh,
+ .encode_fh = ntfs_encode_fh,
.get_parent = ntfs_get_parent, /* Find the parent of a given directory. */
.fh_to_dentry = ntfs_fh_to_dentry,
.fh_to_parent = ntfs_fh_to_parent,
diff --git a/fs/ntfs/stream.h b/fs/ntfs/stream.h
new file mode 100644
index 0000000000000..5cbf6e8f6b681
--- /dev/null
+++ b/fs/ntfs/stream.h
@@ -0,0 +1,81 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef _NTFS_STREAM_H
+#define _NTFS_STREAM_H
+
+#include <linux/fs.h>
+
+struct ntfs_inode;
+struct ntfs_volume;
+
+struct ntfs_stream_path {
+ const char *base_name;
+ unsigned int base_len;
+ const char *stream_name;
+ unsigned int stream_len;
+};
+
+int ntfs_check_bad_windows_name(struct ntfs_volume *vol,
+ const __le16 *name, unsigned int name_len);
+int ntfs_check_stream_name(const __le16 *name, unsigned int name_len);
+
+int ntfs_stream_path_parse(struct ntfs_volume *vol,
+ const struct qstr *qname, struct ntfs_stream_path *path);
+bool ntfs_stream_path_has_colon(struct ntfs_volume *vol,
+ const struct qstr *qname);
+int ntfs_stream_path_check(struct ntfs_volume *vol,
+ const struct qstr *qname);
+int ntfs_stream_path_names(struct ntfs_volume *vol,
+ const struct qstr *qname, __le16 **base_name, int *base_len,
+ __le16 **stream_name, int *stream_len);
+
+struct inode *ntfs_stream_lookup_inode_by_name(struct inode *dir,
+ __le16 *name, int name_len);
+struct dentry *ntfs_lookup_stream(struct inode *dir, struct dentry *dentry,
+ const struct ntfs_stream_path *path);
+struct inode *ntfs_create_named_stream(struct mnt_idmap *idmap,
+ struct inode *dir, __le16 *base_name, int base_len,
+ __le16 *stream_name, int stream_len);
+int ntfs_unlink_named_stream(struct inode *dir, struct dentry *dentry);
+
+void ntfs_set_default_dentry_ops(struct super_block *sb);
+
+void ntfs_stream_inode_refresh(struct inode *inode);
+int ntfs_stream_inode_validate(struct inode *inode);
+void ntfs_stream_update_base_time(struct ntfs_inode *base_ni);
+bool ntfs_stream_unlinked(struct ntfs_inode *base_ni,
+ const __le16 *name, u32 name_len);
+int ntfs_stream_put(struct inode *inode);
+int ntfs_remove_named_stream(struct ntfs_inode *ni, __le16 *name,
+ u32 name_len, struct inode *expected_inode);
+
+int ntfs_stream_validate_for_mutation(struct inode *inode, bool nowait);
+long ntfs_ioctl_stream_read(struct file *file, unsigned long arg);
+long ntfs_ioctl_stream_write(struct file *file, unsigned long arg);
+long ntfs_ioctl_stream_remove(struct file *file, unsigned long arg);
+int ntfs_ioctl_list_streams(struct file *file, unsigned long arg);
+
+extern const struct file_operations ntfs_stream_file_ops;
+extern const struct inode_operations ntfs_stream_inode_ops;
+
+/*
+ * Common file operations used by both ordinary files and named streams.
+ * Their implementations remain in file.c.
+ */
+int ntfs_file_open(struct inode *inode, struct file *file);
+int ntfs_file_release(struct inode *inode, struct file *file);
+int ntfs_file_fsync(struct file *file, loff_t start, loff_t end,
+ int datasync);
+int ntfs_setattr_size(struct inode *inode, struct iattr *attr);
+loff_t ntfs_file_llseek(struct file *file, loff_t offset, int whence);
+ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to);
+ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from);
+int ntfs_file_mmap_prepare(struct vm_area_desc *desc);
+ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos,
+ struct pipe_inode_info *pipe, size_t len, unsigned int flags);
+int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
+ u64 start, u64 len);
+long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len);
+
+int ntfs_test_inode_rcu(struct inode *inode, void *data);
+
+#endif /* _NTFS_STREAM_H */
diff --git a/fs/ntfs/super.c b/fs/ntfs/super.c
index d174867b9f5f2..242fb19ced2b4 100644
--- a/fs/ntfs/super.c
+++ b/fs/ntfs/super.c
@@ -23,6 +23,7 @@
#include "ea.h"
#include "bitmap.h"
#include "volume.h"
+#include "stream.h"
/* A global default upcase table and a corresponding reference count. */
static __le16 *default_upcase;
@@ -67,6 +68,17 @@ static const struct constant_table ntfs_symlink_enums[] = {
{}
};
+enum {
+ STREAMS_INTERFACE_NONE,
+ STREAMS_INTERFACE_WINDOWS,
+};
+
+static const struct constant_table ntfs_streams_interface_enums[] = {
+ { "none", STREAMS_INTERFACE_NONE },
+ { "windows", STREAMS_INTERFACE_WINDOWS },
+ {}
+};
+
enum {
Opt_uid,
Opt_gid,
@@ -92,6 +104,7 @@ enum {
Opt_nocase,
Opt_native_symlink,
Opt_symlink,
+ Opt_streams_interface,
};
static const struct fs_parameter_spec ntfs_parameters[] = {
@@ -119,6 +132,8 @@ static const struct fs_parameter_spec ntfs_parameters[] = {
fsparam_flag("nocase", Opt_nocase),
fsparam_enum("native_symlink", Opt_native_symlink, ntfs_native_symlink_enums),
fsparam_enum("symlink", Opt_symlink, ntfs_symlink_enums),
+ fsparam_enum("streams_interface", Opt_streams_interface,
+ ntfs_streams_interface_enums),
{}
};
@@ -255,6 +270,22 @@ static int ntfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
else
NVolClearSymlinkNative(vol);
break;
+ case Opt_streams_interface: {
+ bool streams_windows =
+ result.uint_32 == STREAMS_INTERFACE_WINDOWS;
+
+ if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE &&
+ NVolStreamsWindows(vol) != streams_windows) {
+ ntfs_error(vol->sb,
+ "Cannot change streams_interface on remount.");
+ return -EINVAL;
+ }
+ if (streams_windows)
+ NVolSetStreamsWindows(vol);
+ else
+ NVolClearStreamsWindows(vol);
+ break;
+ }
case Opt_sparse:
break;
default:
@@ -2725,6 +2756,8 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
* operations and associated address space operations to function.
*/
sb->s_op = &ntfs_sops;
+ if (NVolStreamsWindows(vol))
+ ntfs_set_default_dentry_ops(sb);
tmp_ino = new_inode(sb);
if (!tmp_ino) {
if (!silent)
diff --git a/fs/ntfs/volume.h b/fs/ntfs/volume.h
index 6d9d6eb79a7c1..ed5c0ccb9cd9f 100644
--- a/fs/ntfs/volume.h
+++ b/fs/ntfs/volume.h
@@ -197,6 +197,7 @@ struct ntfs_volume {
* NV_DisableSparse Disable creation of sparse regions.
* NV_NativeSymlinkRel Translate absolute Windows reparse targets (native_symlink=rel).
* NV_MftBootstrap Mount is still assembling $MFT's own runlist.
+ * NV_StreamsWindows Interpret the final path component as file:stream.
*/
enum {
NV_Errors,
@@ -218,6 +219,7 @@ enum {
NV_NativeSymlinkRel,
NV_SymlinkNative,
NV_MftBootstrap,
+ NV_StreamsWindows,
};
/*
@@ -258,6 +260,7 @@ DEFINE_NVOL_BIT_OPS(DisableSparse)
DEFINE_NVOL_BIT_OPS(NativeSymlinkRel)
DEFINE_NVOL_BIT_OPS(SymlinkNative)
DEFINE_NVOL_BIT_OPS(MftBootstrap)
+DEFINE_NVOL_BIT_OPS(StreamsWindows)
static inline void ntfs_inc_free_clusters(struct ntfs_volume *vol, s64 nr)
{
diff --git a/fs/ntfs/wof.c b/fs/ntfs/wof.c
index 9847259e5b1a2..294dbe5d711f7 100644
--- a/fs/ntfs/wof.c
+++ b/fs/ntfs/wof.c
@@ -311,7 +311,8 @@ static int parse_wof_chunk_table(struct ntfs_inode *base_ni,
goto out_unlock_mrec;
}
ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
- CASE_SENSITIVE, 0, NULL, 0, ctx);
+ CASE_SENSITIVE,
+ 0, NULL, 0, ctx);
if (ret)
goto out_put_ctx;
@@ -399,7 +400,8 @@ static int ntfs_read_wof_chunk(struct ntfs_volume *vol,
}
err = ntfs_attr_lookup(wof_ni->type, wof_ni->name, wof_ni->name_len,
- CASE_SENSITIVE, 0, NULL, 0, ctx);
+ CASE_SENSITIVE,
+ 0, NULL, 0, ctx);
if (err)
goto out_put_ctx;
diff --git a/include/uapi/linux/ntfs.h b/include/uapi/linux/ntfs.h
new file mode 100644
index 0000000000000..245133352132b
--- /dev/null
+++ b/include/uapi/linux/ntfs.h
@@ -0,0 +1,126 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * Copyright (c) 2026 LG Electronics Co., Ltd.
+ */
+
+#ifndef _UAPI_LINUX_NTFS_H
+#define _UAPI_LINUX_NTFS_H
+#include <linux/types.h>
+#include <linux/ioctl.h>
+
+#define NTFS_IOC_MAGIC 0xEF
+
+/*
+ * Flags for ntfs_stream.flags and ntfs_list_streams.flags.
+ *
+ * NTFS_STREAM_FL_UTF16 makes stream names raw UTF-16LE, exactly as stored on
+ * disk, instead of encoding them with the mounted filesystem NLS. This
+ * allows lossless round-tripping of stream names whose characters are not
+ * representable by the mount NLS (e.g. for Wine, which works in UTF-16
+ * natively). When set, the name length fields count bytes of UTF-16LE and
+ * must therefore be even.
+ */
+#define NTFS_STREAM_FL_UTF16 0x1
+
+/*
+ * ntfs named stream read, write, and remove ioctl structure.
+ *
+ * @stream_offset: Offset within the named stream for read/write.
+ * @io_len: Number of bytes to read/write. Must be zero for remove.
+ * @bytes_returned: Actual bytes transferred (out).
+ * @name_len: Stream name length in bytes, not including any
+ * terminating NUL. When NTFS_STREAM_FL_UTF16 is set,
+ * this counts UTF-16LE bytes and must be even.
+ * @flags: Bit mask of NTFS_STREAM_FL_* flags. Other bits must
+ * be zero.
+ * @reserved: Must be zero.
+ * @buffer: Bare stream name followed by stream data for read/write.
+ *
+ * The stream name is encoded with the mounted filesystem NLS, or as raw
+ * UTF-16LE when NTFS_STREAM_FL_UTF16 is set. A write creates the stream if
+ * it does not already exist. A write failure after creation may leave the
+ * new stream behind; it can be removed separately.
+ *
+ * For NTFS_IOC_STREAM_READ, a successful short read indicates EOF. If a read
+ * fails, the ioctl returns an error and copies no stream data to userspace.
+ */
+struct ntfs_stream {
+ __aligned_u64 stream_offset;
+ __aligned_u64 io_len;
+ __aligned_u64 bytes_returned;
+ __u32 name_len;
+ __u32 flags;
+ __aligned_u64 reserved;
+ __u8 buffer[];
+};
+
+/*
+ * Single stream entry returned by NTFS_IOC_LIST_STREAMS.
+ *
+ * @next_entry_off: Byte offset from the start of this entry to the next
+ * entry, or zero for the last entry. Always a multiple
+ * of 8. Consumers must use this to advance instead of
+ * computing the stride themselves.
+ * @size: Stream data size in bytes.
+ * @alloc_size: Bytes allocated for the stream (cluster aligned for
+ * non-resident streams).
+ * @name_len: Stream name length in bytes, not including a NUL
+ * terminator (none is stored). Counts UTF-16LE bytes
+ * when NTFS_STREAM_FL_UTF16 was requested.
+ * @name_offset: Byte offset from the start of this entry to @name.
+ * @reserved: Must be zero.
+ * @name: Bare stream name; NLS encoded, or raw UTF-16LE when
+ * NTFS_STREAM_FL_UTF16 was requested.
+ *
+ * New fixed fields may be added after @reserved and before @name. Consumers
+ * must use @name_offset to locate the name and @next_entry_off to advance to
+ * the next entry.
+ */
+struct ntfs_stream_entry {
+ __aligned_u64 next_entry_off;
+ __aligned_u64 size;
+ __aligned_u64 alloc_size;
+ __u32 name_len;
+ __u32 name_offset;
+ __u32 reserved;
+ __u8 name[];
+};
+
+/*
+ * ntfs list streams ioctl structure.
+ *
+ * @buffer_size: user buffer size(in).
+ * @bytes_returned: actual bytes written or required(out).
+ * @stream_count: number of streams(out).
+ * @flags: Bit mask of NTFS_STREAM_FL_* flags controlling the
+ * encoding of the returned names; other bits must be zero.
+ * @reserved: Must be zero.
+ * @buffer: ntfs_stream_entry array.
+ *
+ * The variable-length entries follow the header in @buffer, each aligned on
+ * an 8-byte boundary and chained via ntfs_stream_entry.next_entry_off. If
+ * @buffer_size is too small, no data is copied, @stream_count and
+ * @bytes_returned report the required values and the ioctl fails with
+ * -ENOSPC. In NLS mode, names that cannot be represented by the mounted
+ * character set are omitted; use NTFS_STREAM_FL_UTF16 to list all
+ * names without conversion loss.
+ */
+struct ntfs_list_streams {
+ __aligned_u64 buffer_size;
+ __aligned_u64 bytes_returned;
+ __aligned_u64 stream_count;
+ __u32 flags;
+ __u32 reserved;
+ __u8 buffer[];
+};
+
+#define NTFS_IOC_STREAM_READ \
+ _IOWR(NTFS_IOC_MAGIC, 1, struct ntfs_stream)
+#define NTFS_IOC_STREAM_WRITE \
+ _IOWR(NTFS_IOC_MAGIC, 2, struct ntfs_stream)
+#define NTFS_IOC_STREAM_REMOVE \
+ _IOWR(NTFS_IOC_MAGIC, 3, struct ntfs_stream)
+#define NTFS_IOC_LIST_STREAMS \
+ _IOWR(NTFS_IOC_MAGIC, 4, struct ntfs_list_streams)
+
+#endif /* _UAPI_LINUX_NTFS_H */
|
| 3/2 | 2026/10/11 22:43 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 62040 output: 27 thoughts: 283 Thoughts: **Analyzing the NTFS Named Streams Patch** Okay, so this patch introduces named stream support to the NTFS driver. Just to be completely clear, I need to verify which version of the driver we're talking about – is `fs/ntfs` in the codebase equivalent to NTFS or NTFS3? A quick check is in order, I'll need to confirm that. Alright, let's see what's actually been modified. The file list is comprehensive: we've got documentation updates in `ntfs.rst` and the `ioctl` documentation, important changes to `MAINTAINERS`, and then core modifications within the `fs/ntfs` directory. Specifically, `Makefile`, `attrib.c`, `attrlist.c`, `dir.c`, `ea.c`, `file.c`, `inode.c`, `iomap.c`, the new `named_stream.c`, `namei.c`, `stream.h`, `super.c`, `volume.h`, and `wof.c`. We've also got `include/uapi/linux/ntfs.h`, which is crucial for the userspace interface. Now, I need to understand how this is enabled. Time to check `fs/ntfs/Kconfig` or the `Makefile` to see the relevant configuration options. The fastest way to inspect that is to use grepper on the `Kconfig` file. |
| 4/2 | 2026/10/11 22:43 | tool | grepper |
0mArgs:{
"Expression": "config NTFS",
"PathPrefixes": [
"fs/ntfs"
]
}Results: {
"Output": "fs/ntfs/Kconfig-1-# SPDX-License-Identifier: GPL-2.0-only\nfs/ntfs/Kconfig:2:config NTFS_FS\nfs/ntfs/Kconfig-3-\ttristate \"NTFS file system support\"\n--\nfs/ntfs/Kconfig-12-\nfs/ntfs/Kconfig:13:config NTFS_FS_WOF_COMPRESSION\nfs/ntfs/Kconfig-14-\tbool \"NTFS Windows system compression support\"\n--\nfs/ntfs/Kconfig-23-\nfs/ntfs/Kconfig:24:config NTFS_DEBUG\nfs/ntfs/Kconfig-25-\tbool \"NTFS debugging support\"\n--\nfs/ntfs/Kconfig-45-\nfs/ntfs/Kconfig:46:config NTFS_FS_POSIX_ACL\nfs/ntfs/Kconfig-47-\tbool \"NTFS POSIX Access Control Lists\"\n"
} |
| 5/2 | 2026/10/11 22:43 | llm | patch-evaluator |
1mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 62890 output: 33 thoughts: 19 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Diving into the NTFS Kconfig** Alright, I need a deep dive into the NTFS driver's configuration options. The best place to start, as always, is the `fs/ntfs/Kconfig` file. I'm hoping it clearly outlines the module's dependencies – both on other kernel components and, of course, the underlying architecture and any necessary features. I want to see how the build system handles things like feature flags, optional components (like journaling support, for example), and any conditional compilation that might be going on. Understanding those dependencies is key before I even consider making any changes or enhancements. This Kconfig file will essentially be the blueprint that clarifies the intricacies of the driver's build process. |
| 6/2 | 2026/10/11 22:45 | tool | read-file |
0mArgs:{
"File": "fs/ntfs/Kconfig",
"FirstLine": 1,
"LineCount": 55
}Results: {
"Contents": " 1:\t# SPDX-License-Identifier: GPL-2.0-only\n 2:\tconfig NTFS_FS\n 3:\t\ttristate \"NTFS file system support\"\n 4:\t\tselect NLS\n 5:\t\tselect FS_IOMAP\n 6:\t\thelp\n 7:\t\t NTFS is the file system of Microsoft Windows NT, 2000, XP and 2003.\n 8:\t\t This allows you to mount devices formatted with the ntfs file system.\n 9:\t\n 10:\t\t To compile this as a module, choose M here: the module will be called\n 11:\t\t ntfs.\n 12:\t\n 13:\tconfig NTFS_FS_WOF_COMPRESSION\n 14:\t\tbool \"NTFS Windows system compression support\"\n 15:\t\tdepends on NTFS_FS\n 16:\t\thelp\n 17:\t\t Enable read support for Windows system-compressed files.\n 18:\t\t Windows more aggressively compresses system files such as binaries\n 19:\t\t and DLLs using system compression. It is optimized for files that\n 20:\t\t are rarely modified.\n 21:\t\t Enabling this option builds the decompression support into the NTFS\n 22:\t\t module.\n 23:\t\n 24:\tconfig NTFS_DEBUG\n 25:\t\tbool \"NTFS debugging support\"\n 26:\t\tdepends on NTFS_FS\n 27:\t\thelp\n 28:\t\t If you are experiencing any problems with the NTFS file system, say\n 29:\t\t Y here. This will result in additional consistency checks to be\n 30:\t\t performed by the driver as well as additional debugging messages to\n 31:\t\t be written to the system log. Note that debugging messages are\n 32:\t\t disabled by default. To enable them, supply the option debug_msgs=1\n 33:\t\t at the kernel command line when booting the kernel or as an option\n 34:\t\t to insmod when loading the ntfs module. Once the driver is active,\n 35:\t\t you can enable debugging messages by doing (as root):\n 36:\t\t echo 1 \u003e /proc/sys/fs/ntfs-debug\n 37:\t\t Replacing the \"1\" with \"0\" would disable debug messages.\n 38:\t\n 39:\t\t If you leave debugging messages disabled, this results in little\n 40:\t\t overhead, but enabling debug messages results in very significant\n 41:\t\t slowdown of the system.\n 42:\t\n 43:\t\t When reporting bugs, please try to have available a full dump of\n 44:\t\t debugging messages while the misbehaviour was occurring.\n 45:\t\n 46:\tconfig NTFS_FS_POSIX_ACL\n 47:\t\tbool \"NTFS POSIX Access Control Lists\"\n 48:\t\tdepends on NTFS_FS\n 49:\t\tselect FS_POSIX_ACL\n 50:\t\thelp\n 51:\t\t POSIX Access Control Lists (ACLs) support additional access rights\n 52:\t\t for users and groups beyond the standard owner/group/world scheme.\n 53:\t\n 54:\t\t This option enables ACL support for ntfs, providing functional parity\n 55:\t\t with ntfs3 drivier.\n"
} |
| 7/2 | 2026/10/11 22:45 | llm | patch-evaluator |
7mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 63921 output: 29 thoughts: 696 Thoughts: **Fuzzing NTFS Named Streams: A Prime Target for Syzkaller** Okay, so `NTFS_FS` is the key config option here – that's the starting point. The question is, can we actually reach this driver through syzkaller fuzzing? And the answer, thankfully, is a resounding YES! Syzkaller is perfectly capable of mounting NTFS filesystems using loopback images. It's built for exactly this sort of thing, hitting it with syscalls like `open`, `read`, `write`, `truncate`, and all the usual suspects. What makes this particularly juicy is the addition of these new ioctls specifically for named streams: `NTFS_IOC_STREAM_READ`, `NTFS_IOC_STREAM_WRITE`, `NTFS_IOC_STREAM_REMOVE`, and `NTFS_IOC_LIST_STREAMS`. Plus, we've got support for path name streams through a mount option or ioctl, and a whole new file, `named_stream.c`, along with modifications across the core NTFS codebase. This is a massive addition: named data stream support is a complex beast. It touches pretty much everything – file operations, inode operations, dentry revalidation, ioctls, memory allocation, locking... the works! The real question is: Is this worth fuzzing? Absolutely. Filesystem implementations, especially ones dealing with these kinds of complex features and all the associated intricacies like parsing, allocation, truncate operations, ioctls, and the whole lifetime management of these streams – streams unlinked while open, locking between the base inode and the stream attribute inode, etc. – these are gold mines for fuzzing. Syzkaller is going to feast on this. Let's quickly check the criteria: WorthFuzzing=true: - "The patch touches reachable executable code, including: Core Subsystems ... Drivers and Protocols: Reachable Drivers and Protocols: Drivers accessible via virtual buses ... Filesystem handling". Filesystems are standard virtual/loopback test targets in syzkaller. Standard QEMU/virtualized environments mount loop devices and images formatted with ntfs. Now for the requirements. We know `WorthFuzzing` is `true`. Let's get the required configs: We definitely need `NTFS_FS`. And since we can also enable `NTFS_FS_POSIX_ACL` to hit ACL hooks on streams and it's there in the code, let's include it. It's worth fuzzing that code path too. We can leave `NTFS_FS_WOF_COMPRESSION` as an option. Now, let's just make sure there aren't any other hidden dependencies lurking around in `fs/ntfs/` or `fs/Kconfig`. |
| 8/2 | 2026/10/11 22:53 | tool | grepper |
0mArgs:{
"Expression": "source.*fs/ntfs",
"PathPrefixes": [
"fs/"
]
}Results: {
"Output": "fs/Kconfig=154=source \"fs/exfat/Kconfig\"\nfs/Kconfig:155:source \"fs/ntfs/Kconfig\"\nfs/Kconfig:156:source \"fs/ntfs3/Kconfig\"\nfs/Kconfig-157-\n"
} |
| 9/2 | 2026/10/11 22:53 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 65311 output: 312 thoughts: 2636 Thoughts: **Named Stream Feature Triage for NTFS Fuzzing** Okay, here's my assessment. First, looking at `fs/ntfs/Makefile`, it's clear that `CONFIG_NTFS_FS` includes all relevant files, specifically `named_stream.o`. `CONFIG_NTFS_FS_POSIX_ACL` is relevant since ACL methods are used in `named_stream.c`. Now, considering the "FocusSymbols" guidance, I need to pinpoint the *specific* feature logic and entry points, while avoiding overly broad functions. I’ve gone through the introduced and modified functions in `fs/ntfs/named_stream.c`, `fs/ntfs/file.c`, `fs/ntfs/namei.c`, `fs/ntfs/inode.c`, and `fs/ntfs/attrib.c`. It's crucial to identify the non-static functions directly exposed, as these are ideal fuzzing targets. From `named_stream.c`, I've identified `ntfs_ioctl_stream_read`, `ntfs_ioctl_stream_write`, `ntfs_ioctl_stream_remove`, `ntfs_ioctl_list_streams`, `ntfs_create_named_stream`, `ntfs_unlink_named_stream`, `ntfs_lookup_stream`, `ntfs_remove_named_stream`, `ntfs_stream_inode_validate`, `ntfs_stream_path_parse`, `ntfs_check_stream_name` and `ntfs_stream_put`. `ntfs_inode_attr_pread`, `ntfs_inode_attr_pwrite` and `ntfs_attr_remove` are also relevant. After reviewing, I think I have a focused set of symbols that truly reflect this feature's logic and entry points. I confirmed that these are all relevant non-static symbols, which are critical to target. EnableConfigs: `NTFS_FS` and `NTFS_FS_POSIX_ACL`. I reviewed that `NTFS_FS` is independent. `NTFS_FS_POSIX_ACL` depends on `NTFS_FS`. Given the standard QEMU setup and that fuzzing is appropriate (worthfuzzing=true), I'm confident in calling it. |
| 10/2 | 2026/10/11 22:53 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"NTFS_FS",
"NTFS_FS_POSIX_ACL"
],
"FocusSymbols": [
"ntfs_ioctl_stream_read",
"ntfs_ioctl_stream_write",
"ntfs_ioctl_stream_remove",
"ntfs_ioctl_list_streams",
"ntfs_create_named_stream",
"ntfs_unlink_named_stream",
"ntfs_lookup_stream",
"ntfs_remove_named_stream",
"ntfs_stream_inode_validate",
"ntfs_stream_put",
"ntfs_inode_attr_pread",
"ntfs_inode_attr_pwrite"
],
"Reasoning": "The patch introduces support for NTFS named data streams ($DATA attributes), including a new ioctl interface (NTFS_IOC_STREAM_READ, NTFS_IOC_STREAM_WRITE, NTFS_IOC_STREAM_REMOVE, NTFS_IOC_LIST_STREAMS) and pathname-based stream access (file:stream format enabled via streams_interface=windows). It implements stream creation, lookup, unlink, validation, concurrent locking/lifetime management, and attribute reading/writing. This complex filesystem feature is fully reachable in virtualized environments via loopback-mounted NTFS images and user syscalls/ioctls, making it a prime candidate for syzkaller fuzzing.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"NTFS_FS",
"NTFS_FS_POSIX_ACL"
],
"FocusSymbols": [
"ntfs_ioctl_stream_read",
"ntfs_ioctl_stream_write",
"ntfs_ioctl_stream_remove",
"ntfs_ioctl_list_streams",
"ntfs_create_named_stream",
"ntfs_unlink_named_stream",
"ntfs_lookup_stream",
"ntfs_remove_named_stream",
"ntfs_stream_inode_validate",
"ntfs_stream_put",
"ntfs_inode_attr_pread",
"ntfs_inode_attr_pwrite"
],
"Reasoning": "The patch introduces support for NTFS named data streams ($DATA attributes), including a new ioctl interface (NTFS_IOC_STREAM_READ, NTFS_IOC_STREAM_WRITE, NTFS_IOC_STREAM_REMOVE, NTFS_IOC_LIST_STREAMS) and pathname-based stream access (file:stream format enabled via streams_interface=windows). It implements stream creation, lookup, unlink, validation, concurrent locking/lifetime management, and attribute reading/writing. This complex filesystem feature is fully reachable in virtualized environments via loopback-mounted NTFS images and user syscalls/ioctls, making it a prime candidate for syzkaller fuzzing.",
"WorthFuzzing": true
} |
| 11/1 | 2026/10/11 22:53 | action | If |
5mArgs:{
"WorthFuzzing": true
}Results: null |
| 12/2 | 2026/10/11 22:53 | agent | kmsan-evaluator |
5mModel:core Args: null Results: {
"KMSANReasoning": "The patch series adds named stream support ($DATA attributes) to the NTFS filesystem, introducing both a Windows-compatible pathname interface (file:stream) and ioctl interfaces (NTFS_IOC_STREAM_READ, NTFS_IOC_STREAM_WRITE, NTFS_IOC_STREAM_REMOVE, NTFS_IOC_LIST_STREAMS).\n\nAn analysis of memory initialization and kernel-to-user data transfers shows:\n1. UAPI Structures \u0026 Info-leak checks:\n - In `ntfs_ioctl_stream(NTFS_STREAM_IOCTL_READ)`, `struct ntfs_stream` has no structure padding (__aligned_u64, __aligned_u64, __aligned_u64, __u32, __u32, __aligned_u64). The header and name are initialized from user space via copy_from_user(), `bytes_returned` is set explicitly, and the stream payload is read into the buffer via `ntfs_inode_attr_pread()`, which returns the exact byte count copied into the buffer. The amount copied back to user space strictly equals `sizeof(*req) + req-\u003ename_len + req-\u003ebytes_returned`.\n - In `ntfs_ioctl_stream(NTFS_STREAM_IOCTL_WRITE)`, only `req-\u003ebytes_returned` is copied to user space.\n - In `ntfs_ioctl_list_streams()`, `struct ntfs_list_streams` contains no padding. The entry buffer `kbuf` is allocated using `kvzalloc()`, ensuring that any structure alignment padding (entries are 8-byte aligned) or reserved fields in `struct ntfs_stream_entry` are zero-initialized prior to `copy_to_user()`.\n2. Kernel Data Structures \u0026 Stack/Heap Allocations:\n - `struct ntfs_stream_path` fields are populated by `ntfs_stream_path_parse()` whenever it returns positive, and never accessed otherwise.\n - `struct ntfs_fsync_attr` allocated in `ntfs_file_fsync()` has only two fields (`list` and `inode`), both of which are initialized immediately upon allocation.\n - `ni-\u003ename` buffers allocated in `ntfs_read_locked_attr_inode()` are immediately populated via `memcpy()` and null-terminated.\n - Gaps in stream data are cleared using `iomap_zero_range()` in `ntfs_extend_initialized_size()`, and resident attribute extensions zero new bytes via `memset()` in `ntfs_resident_attr_value_resize()`.\n3. KASAN vs. KMSAN applicability:\n - The primary risks in this patch series involve locking hierarchies (locking base inode's `mrec_lock` across stream inodes), reference counting and deferred unlink lifecycles (`stream_open_count`, `NInoStreamUnlinked`), and file extent bounds checks. These failure modes represent locking deadlocks, use-after-free, or out-of-bounds access, which are already detected by standard debuggers (LOCKDEP, KASAN, etc.).\n - There are no uninitialized memory reads or information leaks introduced.\n\nTherefore, dedicated KMSAN fuzzing is not justified.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 31714ee070cc940f303c3a87e717a5aae8856a8f
Author: syz-cluster <triage@syzkaller.com>
Date: Sun Oct 11 22:43:11 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/filesystems/ntfs.rst b/Documentation/filesystems/ntfs.rst
index 4bfa392daec6a..ec329bb142a06 100644
--- a/Documentation/filesystems/ntfs.rst
+++ b/Documentation/filesystems/ntfs.rst
@@ -169,4 +169,38 @@ symlink=wsl|native Configure how symbolic links are created. Under
Linux) compatible symlinks are created. Under
"native", Windows native symbolic links are
created.
+
+streams_interface= Select the named-stream pathname interface:
+ "none" (default) treats ':' as an ordinary filename
+ character, while "windows" interprets the final
+ component of a path such as "file:stream" as a named
+ $DATA stream. In "windows" mode, filenames containing
+ ':' are hidden from directory listings and cannot be
+ addressed through the pathname interface. Only the
+ file:stream form is recognized. Typed forms such
+ as file::$DATA and file:stream:$DATA are not
+ supported. The ioctl interface is available in both
+ modes. The base file or directory must already exist
+ before a pathname stream can be created. Creating
+ file:stream does not create file. Removing a stream
+ while it is open fails with -EBUSY. Removing the base
+ file while a stream is open detaches the stream from
+ the namespace; NTFS has no orphan-stream list, so a
+ crash can leave that stream's data unreachable on
+ disk.
======================= ====================================================
+
+Named-stream ioctls
+===================
+
+The named-stream ioctl interface is available regardless of
+streams_interface. Open the base regular file or directory and use
+NTFS_IOC_STREAM_READ and NTFS_IOC_STREAM_WRITE with a
+struct ntfs_stream request. The request contains the stream name,
+byte offset, and transfer length. Write requests create the stream when it
+does not exist. A failed write can leave a newly created empty stream; use
+NTFS_IOC_STREAM_REMOVE to remove it. This ioctl removes streams, and
+NTFS_IOC_LIST_STREAMS enumerates streams. Stream names are encoded with
+the mounted NLS by default. Set NTFS_STREAM_FL_UTF16 to pass or receive
+raw UTF-16LE names. These ioctls transfer stream data directly and do not
+return a separate stream file descriptor.
diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documentation/userspace-api/ioctl/ioctl-number.rst
index 2fc53093752d1..94a421970b850 100644
--- a/Documentation/userspace-api/ioctl/ioctl-number.rst
+++ b/Documentation/userspace-api/ioctl/ioctl-number.rst
@@ -401,6 +401,7 @@ Code Seq# Include File Comments
0xE5 00-3F linux/fuse.h
0xEC 00-01 drivers/platform/chrome/cros_ec_dev.h ChromeOS EC driver
0xEE 00-09 uapi/linux/pfrut.h Platform Firmware Runtime Update and Telemetry
+0xEF 00-0F uapi/linux/ntfs.h NTFS
0xF3 00-3F drivers/usb/misc/sisusbvga/sisusb.h sisfb (in development)
<mailto:thomas@winischhofer.net>
0xF6 all LTTng Linux Trace Toolkit Next Generation
diff --git a/MAINTAINERS b/MAINTAINERS
index cc3cae2e378b3..b035f7ad34f05 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -19489,6 +19489,7 @@ S: Maintained
T: git git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/ntfs.git
F: Documentation/filesystems/ntfs.rst
F: fs/ntfs/
+F: include/uapi/linux/ntfs.h
NTFS3 FILESYSTEM
M: Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
diff --git a/fs/ntfs/Makefile b/fs/ntfs/Makefile
index ee8987e496a87..3e85495776536 100644
--- a/fs/ntfs/Makefile
+++ b/fs/ntfs/Makefile
@@ -5,7 +5,7 @@ obj-$(CONFIG_NTFS_FS) += ntfs.o
ntfs-y := aops.o attrib.o collate.o dir.o file.o index.o inode.o \
mft.o mst.o namei.o runlist.o super.o unistr.o attrlist.o ea.o \
upcase.o bitmap.o lcnalloc.o logfile.o reparse.o compress.o \
- iomap.o debug.o sysctl.o object_id.o bdev-io.o
+ iomap.o debug.o sysctl.o object_id.o bdev-io.o named_stream.o
ntfs-$(CONFIG_NTFS_FS_WOF_COMPRESSION) += wof.o \
lib/decompress_common.o lib/lzx_decompress.o lib/xpress_decompress.o
diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index 07414a20b7de9..fce7144d7bef6 100644
--- a/fs/ntfs/attrib.c
+++ b/fs/ntfs/attrib.c
@@ -2352,12 +2352,13 @@ int ntfs_attr_set(struct ntfs_inode *ni, s64 ofs, s64 cnt, const u8 val)
int ntfs_attr_set_initialized_size(struct ntfs_inode *ni, loff_t new_size)
{
struct ntfs_attr_search_ctx *ctx;
+ struct ntfs_inode *base_ni = ntfs_base_inode(ni);
int err = 0;
if (!NInoNonResident(ni))
return -EINVAL;
- ctx = ntfs_attr_get_search_ctx(ni, NULL);
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx)
return -ENOMEM;
@@ -2439,6 +2440,7 @@ int ntfs_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
struct mft_record *m;
int err, offset;
struct ntfs_inode *base_ni;
+ u32 ic;
if (!ni || (!name && name_len))
return -EINVAL;
@@ -2468,7 +2470,8 @@ int ntfs_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
* attribute in @ni->mrec, not any extent inode in case if @ni is base
* file record.
*/
- err = ntfs_attr_find(type, name, name_len, CASE_SENSITIVE, val, size, ctx);
+ ic = type == AT_DATA && name_len ? IGNORE_CASE : CASE_SENSITIVE;
+ err = ntfs_attr_find(type, name, name_len, ic, val, size, ctx);
if (!err) {
err = -EEXIST;
ntfs_debug("Attribute already present.\n");
@@ -2560,6 +2563,7 @@ static int ntfs_non_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
struct mft_record *m;
struct ntfs_inode *base_ni;
int err, offset;
+ u32 ic;
if (!ni || dataruns_size <= 0 || (!name && name_len))
return -EINVAL;
@@ -2589,7 +2593,8 @@ static int ntfs_non_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
* attribute in @ni->mrec, not any extent inode in case if @ni is base
* file record.
*/
- err = ntfs_attr_find(type, name, name_len, CASE_SENSITIVE, NULL, 0, ctx);
+ ic = type == AT_DATA && name_len ? IGNORE_CASE : CASE_SENSITIVE;
+ err = ntfs_attr_find(type, name, name_len, ic, NULL, 0, ctx);
if (!err) {
err = -EEXIST;
pr_err("Attribute 0x%x already present\n", type);
@@ -2663,7 +2668,7 @@ static int ntfs_non_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
* update of attribute list.
*/
ntfs_attr_reinit_search_ctx(ctx);
- err = ntfs_attr_lookup(type, name, name_len, CASE_SENSITIVE,
+ err = ntfs_attr_lookup(type, name, name_len, ic,
lowest_vcn, NULL, 0, ctx);
if (err) {
pr_err("%s: attribute lookup failed\n", __func__);
@@ -3132,6 +3137,7 @@ int ntfs_attr_open(struct ntfs_inode *ni, const __le32 type,
struct attr_record *a;
bool cs;
struct ntfs_inode *base_ni;
+ u32 ic;
int err;
if (!ni || !ni->vol)
@@ -3140,10 +3146,7 @@ int ntfs_attr_open(struct ntfs_inode *ni, const __le32 type,
ntfs_debug("Entering for inode %lld, attr 0x%x.\n",
ni->mft_no, type);
- if (NInoAttr(ni))
- base_ni = ni->ext.base_ntfs_ino;
- else
- base_ni = ni;
+ base_ni = ntfs_base_inode(ni);
if (name && name != AT_UNNAMED && name != I30) {
name = ntfs_ucsndup(name, name_len);
@@ -3161,7 +3164,8 @@ int ntfs_attr_open(struct ntfs_inode *ni, const __le32 type,
goto err_out;
}
- err = ntfs_attr_lookup(type, name, name_len, 0, 0, NULL, 0, ctx);
+ ic = type == AT_DATA && name_len ? IGNORE_CASE : CASE_SENSITIVE;
+ err = ntfs_attr_lookup(type, name, name_len, ic, 0, NULL, 0, ctx);
if (err)
goto put_err_out;
@@ -3346,7 +3350,8 @@ int ntfs_attr_map_whole_runlist(struct ntfs_inode *ni)
not_mapped = 1;
err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
- CASE_SENSITIVE, next_vcn, NULL, 0, ctx);
+ CASE_SENSITIVE, next_vcn,
+ NULL, 0, ctx);
if (err)
break;
@@ -3431,6 +3436,7 @@ int ntfs_attr_record_move_to(struct ntfs_attr_search_ctx *ctx, struct ntfs_inode
struct ntfs_attr_search_ctx *nctx;
struct attr_record *a;
int err;
+ u32 ic;
struct mft_record *ni_mrec;
struct super_block *sb;
@@ -3466,9 +3472,11 @@ int ntfs_attr_record_move_to(struct ntfs_attr_search_ctx *ctx, struct ntfs_inode
* attribute in @ni->mrec, not any extent inode in case if @ni is base
* file record.
*/
- err = ntfs_attr_find(a->type, (__le16 *)((u8 *)a + le16_to_cpu(a->name_offset)),
- a->name_length, CASE_SENSITIVE, NULL,
- 0, nctx);
+ ic = a->type == AT_DATA && a->name_length ?
+ IGNORE_CASE : CASE_SENSITIVE;
+ err = ntfs_attr_find(a->type,
+ (__le16 *)((u8 *)a + le16_to_cpu(a->name_offset)),
+ a->name_length, ic, NULL, 0, nctx);
if (!err) {
ntfs_debug("Attribute of such type, with same name already present in this MFT record.\n");
err = -EEXIST;
@@ -3800,7 +3808,8 @@ static int __ntfs_attr_update_mapping_pairs(struct ntfs_inode *ni,
finished_build = false;
start_rl = ni->runlist.rl;
while (!(err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
- CASE_SENSITIVE, from_vcn, NULL, 0, ctx))) {
+ CASE_SENSITIVE, from_vcn, NULL,
+ 0, ctx))) {
unsigned int de_cnt = 0;
a = ctx->attr;
@@ -4011,7 +4020,8 @@ static int __ntfs_attr_update_mapping_pairs(struct ntfs_inode *ni,
ntfs_attr_reinit_search_ctx(ctx);
ntfs_debug("Deallocate marked extents.\n");
while (!(err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
- CASE_SENSITIVE, 0, NULL, 0, ctx))) {
+ CASE_SENSITIVE, 0, NULL, 0,
+ ctx))) {
if (le64_to_cpu(ctx->attr->data.non_resident.highest_vcn) !=
NTFS_VCN_DELETE_MARK)
continue;
@@ -4345,7 +4355,7 @@ static int ntfs_non_resident_attr_shrink(struct ntfs_inode *ni,
goto unlock_runlist;
}
- ctx = ntfs_attr_get_search_ctx(ni, NULL);
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ntfs_error(vol->sb, "%s: Failed to get search context", __func__);
err = -ENOMEM;
@@ -4404,8 +4414,8 @@ static int ntfs_non_resident_attr_shrink(struct ntfs_inode *ni,
return -ENOMEM;
}
- err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE,
- 0, NULL, 0, ctx);
+ err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE, 0, NULL, 0, ctx);
if (err) {
if (err == -ENOENT)
err = -EIO;
@@ -4724,8 +4734,8 @@ static int ntfs_non_resident_attr_expand(struct ntfs_inode *ni, const s64 newsiz
goto rollback;
}
- err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE,
- 0, NULL, 0, ctx);
+ err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE, 0, NULL, 0, ctx);
if (err) {
if (err == -ENOENT)
err = -EIO;
@@ -4837,7 +4847,7 @@ static int ntfs_resident_attr_resize(struct ntfs_inode *attr_ni, const s64 newsi
}
err = ntfs_attr_lookup(attr_ni->type, attr_ni->name, attr_ni->name_len,
- 0, 0, NULL, 0, ctx);
+ CASE_SENSITIVE, 0, NULL, 0, ctx);
if (err) {
ntfs_error(sb, "ntfs_attr_lookup failed");
goto put_err_out;
@@ -5213,6 +5223,7 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,
s64 *lcn_count, s64 max_clu_count, bool *balloc, bool update_mp,
bool skip_holes)
{
+ struct ntfs_inode *base_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct ntfs_attr_search_ctx *ctx;
struct runlist_element *rl, *rlc;
@@ -5226,10 +5237,7 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,
if (err)
return err;
- if (NInoAttr(ni))
- ctx = ntfs_attr_get_search_ctx(ni->ext.base_ntfs_ino, NULL);
- else
- ctx = ntfs_attr_get_search_ctx(ni, NULL);
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ntfs_error(vol->sb, "%s: Failed to get search context", __func__);
return -ENOMEM;
@@ -5379,7 +5387,7 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,
} else {
VFS_I(ni)->i_blocks += clu_count << (vol->cluster_size_bits - 9);
NInoSetRunlistDirty(ni);
- mark_mft_record_dirty(ni);
+ mark_mft_record_dirty(base_ni);
}
*lcn_start = lcn;
@@ -5405,10 +5413,7 @@ int ntfs_attr_rm(struct ntfs_inode *ni)
struct ntfs_inode *base_ni;
struct super_block *sb = ni->vol->sb;
- if (NInoAttr(ni))
- base_ni = ni->ext.base_ntfs_ino;
- else
- base_ni = ni;
+ base_ni = ntfs_base_inode(ni);
ntfs_debug("Entering for inode 0x%llx, attr 0x%x.\n",
(long long) ni->mft_no, ni->type);
@@ -5420,7 +5425,7 @@ int ntfs_attr_rm(struct ntfs_inode *ni)
err = ntfs_attr_map_whole_runlist(ni);
if (err)
return err;
- ctx = ntfs_attr_get_search_ctx(ni, NULL);
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ntfs_error(sb, "%s: Failed to get search context", __func__);
return -ENOMEM;
@@ -5462,6 +5467,7 @@ int ntfs_attr_exist(struct ntfs_inode *ni, const __le32 type, __le16 *name,
u32 name_len)
{
struct ntfs_attr_search_ctx *ctx;
+ u32 ic;
int ret;
ntfs_debug("Entering\n");
@@ -5473,39 +5479,54 @@ int ntfs_attr_exist(struct ntfs_inode *ni, const __le32 type, __le16 *name,
return 0;
}
- ret = ntfs_attr_lookup(type, name, name_len, CASE_SENSITIVE,
+ ic = type == AT_DATA && name_len ? IGNORE_CASE : CASE_SENSITIVE;
+ ret = ntfs_attr_lookup(type, name, name_len, ic,
0, NULL, 0, ctx);
ntfs_attr_put_search_ctx(ctx);
return !ret;
}
+/*
+ * If @attr_vi is non-NULL, the attribute inode reference is returned to the
+ * caller, which must release it after dropping ni->mrec_lock.
+ */
int ntfs_attr_remove(struct ntfs_inode *ni, const __le32 type, __le16 *name,
- u32 name_len)
+ u32 name_len, struct inode **attr_vi)
{
int err;
- struct inode *attr_vi;
+ struct inode *vi;
struct ntfs_inode *attr_ni;
ntfs_debug("Entering\n");
+ if (attr_vi)
+ *attr_vi = NULL;
if (!ni)
return -EINVAL;
- attr_vi = ntfs_attr_iget(VFS_I(ni), type, name, name_len);
- if (IS_ERR(attr_vi)) {
- err = PTR_ERR(attr_vi);
+ vi = ntfs_attr_iget(VFS_I(ni), type, name, name_len);
+ if (IS_ERR(vi)) {
+ err = PTR_ERR(vi);
ntfs_error(ni->vol->sb, "Failed to open attribute 0x%02x of inode 0x%llx",
type, (unsigned long long)ni->mft_no);
return err;
}
- attr_ni = NTFS_I(attr_vi);
+ attr_ni = NTFS_I(vi);
err = ntfs_attr_rm(attr_ni);
- if (err)
+ if (err) {
ntfs_error(ni->vol->sb, "Failed to remove attribute 0x%02x of inode 0x%llx",
type, (unsigned long long)ni->mft_no);
- iput(attr_vi);
+ } else {
+ NInoClearDirty(attr_ni);
+ clear_nlink(vi);
+ remove_inode_hash(vi);
+ }
+ if (attr_vi)
+ *attr_vi = vi;
+ else
+ iput(vi);
return err;
}
@@ -5580,13 +5601,14 @@ void *ntfs_attr_readall(struct ntfs_inode *ni, const __le32 type,
int ntfs_non_resident_attr_insert_range(struct ntfs_inode *ni, s64 start_vcn, s64 len)
{
+ struct ntfs_inode *base_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct runlist_element *hole_rl, *rl;
struct ntfs_attr_search_ctx *ctx;
int ret;
size_t new_rl_count;
- if (NInoAttr(ni) || ni->type != AT_DATA)
+ if (ni->type != AT_DATA)
return -EOPNOTSUPP;
if (start_vcn > ntfs_bytes_to_cluster(vol, ni->allocated_size))
return -EINVAL;
@@ -5635,14 +5657,14 @@ int ntfs_non_resident_attr_insert_range(struct ntfs_inode *ni, s64 start_vcn, s6
if (ret)
return ret;
- ctx = ntfs_attr_get_search_ctx(ni, NULL);
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ret = -ENOMEM;
return ret;
}
- ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE,
- 0, NULL, 0, ctx);
+ ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE, 0, NULL, 0, ctx);
if (ret) {
ntfs_attr_put_search_ctx(ctx);
return ret;
@@ -5659,6 +5681,7 @@ int ntfs_non_resident_attr_insert_range(struct ntfs_inode *ni, s64 start_vcn, s6
int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn, s64 len)
{
+ struct ntfs_inode *base_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct runlist_element *punch_rl, *rl;
struct ntfs_attr_search_ctx *ctx = NULL;
@@ -5667,7 +5690,7 @@ int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn,
int ret;
size_t new_rl_cnt;
- if (NInoAttr(ni) || ni->type != AT_DATA)
+ if (ni->type != AT_DATA)
return -EOPNOTSUPP;
end_vcn = ntfs_bytes_to_cluster(vol, ni->allocated_size);
@@ -5723,14 +5746,14 @@ int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn,
}
up_write(&ni->runlist.lock);
- ctx = ntfs_attr_get_search_ctx(ni, NULL);
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ret = -ENOMEM;
goto out_rl;
}
- ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE,
- 0, NULL, 0, ctx);
+ ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE, 0, NULL, 0, ctx);
if (ret)
goto out_ctx;
@@ -5748,12 +5771,13 @@ int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn,
ntfs_attr_put_search_ctx(ctx);
out_rl:
kvfree(punch_rl);
- mark_mft_record_dirty(ni);
+ mark_mft_record_dirty(base_ni);
return ret;
}
int ntfs_non_resident_attr_punch_hole(struct ntfs_inode *ni, s64 start_vcn, s64 len)
{
+ struct ntfs_inode *base_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct runlist_element *punch_rl, *rl;
s64 end_vcn;
@@ -5761,7 +5785,7 @@ int ntfs_non_resident_attr_punch_hole(struct ntfs_inode *ni, s64 start_vcn, s64
int ret;
size_t new_rl_count;
- if (NInoAttr(ni) || ni->type != AT_DATA)
+ if (ni->type != AT_DATA)
return -EOPNOTSUPP;
end_vcn = ntfs_bytes_to_cluster(vol, ni->allocated_size);
@@ -5805,12 +5829,13 @@ int ntfs_non_resident_attr_punch_hole(struct ntfs_inode *ni, s64 start_vcn, s64
ntfs_error(vol->sb, "Freeing of clusters failed");
kvfree(punch_rl);
- mark_mft_record_dirty(ni);
+ mark_mft_record_dirty(base_ni);
return ret;
}
int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bool keep_size)
{
+ struct ntfs_inode *base_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct mft_record *mrec;
struct ntfs_attr_search_ctx *ctx;
@@ -5822,7 +5847,7 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
struct runlist_element *rl;
bool balloc;
- if (NInoAttr(ni) || ni->type != AT_DATA)
+ if (ni->type != AT_DATA)
return -EINVAL;
if (NInoNonResident(ni) && !NInoFullyMapped(ni)) {
@@ -5833,20 +5858,21 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
return err;
}
- mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
- mrec = map_mft_record(ni);
+ mutex_lock_nested(&base_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
+ mrec = map_mft_record(base_ni);
if (IS_ERR(mrec)) {
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&base_ni->mrec_lock);
return PTR_ERR(mrec);
}
- ctx = ntfs_attr_get_search_ctx(ni, mrec);
+ ctx = ntfs_attr_get_search_ctx(base_ni, mrec);
if (!ctx) {
err = -ENOMEM;
goto out_unmap;
}
- err = ntfs_attr_lookup(AT_DATA, AT_UNNAMED, 0, 0, 0, NULL, 0, ctx);
+ err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE, 0, NULL, 0, ctx);
if (err) {
err = -EIO;
goto out_unmap;
@@ -5859,25 +5885,28 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
goto out_unmap;
if (keep_size) {
ntfs_attr_reinit_search_ctx(ctx);
- err = ntfs_attr_lookup(AT_DATA, AT_UNNAMED, 0, 0, 0, NULL, 0, ctx);
+ err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE, 0, NULL, 0,
+ ctx);
if (err) {
err = -EIO;
goto out_unmap;
}
ni->data_size = old_data_size;
+ i_size_write(VFS_I(ni), old_data_size);
if (NInoNonResident(ni))
ctx->attr->data.non_resident.data_size =
cpu_to_le64(old_data_size);
else
ctx->attr->data.resident.value_length =
cpu_to_le32((u32)old_data_size);
- mark_mft_record_dirty(ni);
+ mark_mft_record_dirty(base_ni);
}
}
ntfs_attr_put_search_ctx(ctx);
- unmap_mft_record(ni);
- mutex_unlock(&ni->mrec_lock);
+ unmap_mft_record(base_ni);
+ mutex_unlock(&base_ni->mrec_lock);
if (!NInoNonResident(ni))
goto out;
@@ -5919,12 +5948,13 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
}
while (try_alloc_cnt > 0) {
- mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
+ mutex_lock_nested(&base_ni->mrec_lock,
+ NTFS_INODE_MUTEX_NORMAL);
down_write(&ni->runlist.lock);
err = ntfs_attr_map_cluster(ni, vcn, &lcn, &alloc_cnt,
try_alloc_cnt, &balloc, false, false);
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&base_ni->mrec_lock);
if (err)
goto out;
@@ -5952,12 +5982,12 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
/* allocate clusters outside of initialized_size */
try_alloc_cnt = vcn_end - vcn;
while (try_alloc_cnt > 0) {
- mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
+ mutex_lock_nested(&base_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
down_write(&ni->runlist.lock);
err = ntfs_attr_map_cluster(ni, vcn, &lcn, &alloc_cnt,
try_alloc_cnt, &balloc, false, false);
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&base_ni->mrec_lock);
if (err || fatal_signal_pending(current))
goto signal_out;
@@ -5967,7 +5997,7 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
}
if (NInoRunlistDirty(ni)) {
- mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
+ mutex_lock_nested(&base_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
down_write(&ni->runlist.lock);
err = ntfs_attr_update_mapping_pairs_locked(ni, 0, ni);
if (err)
@@ -5975,14 +6005,14 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo
else
NInoClearRunlistDirty(ni);
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&base_ni->mrec_lock);
}
return err;
out_unmap:
if (ctx)
ntfs_attr_put_search_ctx(ctx);
- unmap_mft_record(ni);
- mutex_unlock(&ni->mrec_lock);
+ unmap_mft_record(base_ni);
+ mutex_unlock(&base_ni->mrec_lock);
out:
return err >= 0 ? 0 : err;
signal_out:
diff --git a/fs/ntfs/attrib.h b/fs/ntfs/attrib.h
index 6b4fa9f576401..bee91c9f6f818 100644
--- a/fs/ntfs/attrib.h
+++ b/fs/ntfs/attrib.h
@@ -124,7 +124,7 @@ int ntfs_attr_rm(struct ntfs_inode *ni);
int ntfs_attr_exist(struct ntfs_inode *ni, const __le32 type, __le16 *name,
u32 name_len);
int ntfs_attr_remove(struct ntfs_inode *ni, const __le32 type, __le16 *name,
- u32 name_len);
+ u32 name_len, struct inode **attr_vi);
int ntfs_attr_record_rm(struct ntfs_attr_search_ctx *ctx);
int ntfs_attr_record_move_to(struct ntfs_attr_search_ctx *ctx, struct ntfs_inode *ni);
int ntfs_attr_add(struct ntfs_inode *ni, __le32 type,
diff --git a/fs/ntfs/attrlist.c b/fs/ntfs/attrlist.c
index 6edd5d2d9bf24..8291c4c95b370 100644
--- a/fs/ntfs/attrlist.c
+++ b/fs/ntfs/attrlist.c
@@ -374,7 +374,8 @@ int ntfs_attrlist_entry_add(struct ntfs_inode *ni, struct attr_record *attr)
err = ntfs_attr_lookup(attr->type, (attr->name_length) ? (__le16 *)
((u8 *)attr + le16_to_cpu(attr->name_offset)) :
- AT_UNNAMED, attr->name_length, CASE_SENSITIVE,
+ AT_UNNAMED, attr->name_length,
+ CASE_SENSITIVE,
le64_to_cpu(lowest_vcn),
(attr->non_resident) ? NULL : ((u8 *)attr +
le16_to_cpu(attr->data.resident.value_offset)), (attr->non_resident) ?
diff --git a/fs/ntfs/dir.c b/fs/ntfs/dir.c
index b95173f068cbc..5a0e21e7c283a 100644
--- a/fs/ntfs/dir.c
+++ b/fs/ntfs/dir.c
@@ -8,6 +8,7 @@
*/
#include <linux/blkdev.h>
+#include <linux/string.h>
#include "dir.h"
#include "mft.h"
@@ -624,7 +625,6 @@ static inline int ntfs_filldir(struct ntfs_volume *vol,
ntfs_debug("Skipping hidden file.");
return 0;
}
-
name_len = ntfs_ucstonls(vol, (__le16 *)&ie->key.file_name.file_name,
ie->key.file_name.file_name_length, &name,
NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1);
@@ -633,7 +633,10 @@ static inline int ntfs_filldir(struct ntfs_volume *vol,
(long long)MREF_LE(ie->data.dir.indexed_file));
return 0;
}
-
+ if (NVolStreamsWindows(vol) && strchr((char *)name, ':')) {
+ ntfs_debug("Skipping file name containing a stream separator.");
+ return 0;
+ }
mref = MREF_LE(ie->data.dir.indexed_file);
if (ie->key.file_name.file_attributes & FILE_ATTR_REPARSE_POINT)
dt_type = ntfs_reparse_tag_dt_types(vol, mref);
diff --git a/fs/ntfs/ea.c b/fs/ntfs/ea.c
index ff69fb07f84a4..1104fd72cd2ae 100644
--- a/fs/ntfs/ea.c
+++ b/fs/ntfs/ea.c
@@ -246,7 +246,7 @@ static int ntfs_set_ea(struct inode *inode, const char *name, size_t name_len,
goto out;
if (ntfs_attr_exist(ni, AT_EA, AT_UNNAMED, 0)) {
- err = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0);
+ err = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0, NULL);
if (err)
goto out;
}
@@ -301,11 +301,12 @@ static int ntfs_set_ea(struct inode *inode, const char *name, size_t name_len,
p_ea_info->ea_query_length = cpu_to_le32(ea_info_qsize);
if ((flags & XATTR_REPLACE) && !val_size && !ea_info_qsize) {
- err = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0);
+ err = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0, NULL);
if (err)
goto out;
- err = ntfs_attr_remove(ni, AT_EA_INFORMATION, AT_UNNAMED, 0);
+ err = ntfs_attr_remove(ni, AT_EA_INFORMATION, AT_UNNAMED,
+ 0, NULL);
if (err) {
/* Restore the original $EA if $EA_INFORMATION removal failed. */
ntfs_attr_add(ni, AT_EA, AT_UNNAMED, 0, old_ea_buf,
@@ -610,6 +611,14 @@ static int ntfs_getxattr(const struct xattr_handler *handler,
struct ntfs_inode *ni = NTFS_I(inode);
int err;
+ /*
+ * Stream permissions and privileges belong to the base inode. This
+ * also lets VFS killpriv helpers find the base security attributes.
+ */
+ if (ntfs_inode_is_named_stream(ni)) {
+ ni = ni->ext.base_ntfs_ino;
+ inode = VFS_I(ni);
+ }
if (NVolShutdown(ni->vol))
return -EIO;
@@ -716,8 +725,8 @@ static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr)
goto err_out;
ntfs_attr_reinit_search_ctx(ctx);
- err = ntfs_attr_lookup(ni->type, ni->name,
- ni->name_len, CASE_SENSITIVE,
+ err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE,
0, NULL, 0, ctx);
if (err) {
err = -EINVAL;
@@ -883,6 +892,8 @@ static int ntfs_setxattr(const struct xattr_handler *handler,
int err;
__le32 fattr;
+ if (ntfs_inode_is_named_stream(ni))
+ return -EOPNOTSUPP;
if (NVolShutdown(ni->vol))
return -EIO;
diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
index 1e2500a521483..a2e843cf947c8 100644
--- a/fs/ntfs/file.c
+++ b/fs/ntfs/file.c
@@ -15,6 +15,13 @@
#include <linux/posix_acl_xattr.h>
#include <linux/compat.h>
#include <linux/falloc.h>
+#include <linux/file.h>
+#include <linux/filelock.h>
+#include <linux/list.h>
+#include <linux/overflow.h>
+#include <linux/security.h>
+#include <linux/slab.h>
+#include <uapi/linux/ntfs.h>
#include "lcnalloc.h"
#include "ntfs.h"
@@ -23,8 +30,8 @@
#include "iomap.h"
#include "bitmap.h"
#include "volume.h"
-
-#include <linux/filelock.h>
+#include "mft.h"
+#include "stream.h"
/*
* ntfs_file_open - called when an inode is about to be opened
@@ -44,7 +51,7 @@
*
* After the check passes, just call generic_file_open() to do its work.
*/
-static int ntfs_file_open(struct inode *vi, struct file *filp)
+int ntfs_file_open(struct inode *vi, struct file *filp)
{
struct ntfs_inode *ni = NTFS_I(vi);
@@ -78,6 +85,7 @@ static int ntfs_file_open(struct inode *vi, struct file *filp)
static int ntfs_trim_prealloc(struct inode *vi)
{
struct ntfs_inode *ni = NTFS_I(vi);
+ struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct runlist_element *rl;
s64 aligned_data_size;
@@ -86,7 +94,7 @@ static int ntfs_trim_prealloc(struct inode *vi)
int err = 0;
inode_lock(vi);
- mutex_lock(&ni->mrec_lock);
+ mutex_lock(&mrec_ni->mrec_lock);
down_write(&ni->runlist.lock);
aligned_data_size = round_up(ni->data_size, vol->cluster_size);
@@ -121,13 +129,13 @@ static int ntfs_trim_prealloc(struct inode *vi)
out_unlock:
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
inode_unlock(vi);
return err;
}
-static int ntfs_file_release(struct inode *vi, struct file *filp)
+int ntfs_file_release(struct inode *vi, struct file *filp)
{
if (!NInoCompressed(NTFS_I(vi)) &&
!NInoWofCompressed(NTFS_I(vi)))
@@ -136,6 +144,11 @@ static int ntfs_file_release(struct inode *vi, struct file *filp)
return 0;
}
+struct ntfs_fsync_attr {
+ struct list_head list;
+ struct inode *inode;
+};
+
/*
* ntfs_file_fsync - sync a file to disk
* @filp: file to be synced
@@ -156,7 +169,7 @@ static int ntfs_file_release(struct inode *vi, struct file *filp)
* Also, if @datasync is true, we do not wait on the inode to be written out
* but we always wait on the page cache pages to be written out.
*/
-static int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,
+int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,
int datasync)
{
struct inode *vi = filp->f_mapping->host;
@@ -165,6 +178,9 @@ static int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,
int err, ret = 0;
struct inode *parent_vi, *ia_vi;
struct ntfs_attr_search_ctx *ctx;
+ struct ntfs_fsync_attr *attr, *next;
+ LIST_HEAD(attrs);
+ bool non_resident, stream;
ntfs_debug("Entering for inode 0x%llx.", ni->mft_no);
@@ -175,10 +191,24 @@ static int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,
if (err)
return err;
- if (!datasync || !NInoNonResident(NTFS_I(vi)))
+ stream = ntfs_inode_is_named_stream(ni);
+ non_resident = NInoNonResident(ni);
+ if (stream) {
+ ni = ni->ext.base_ntfs_ino;
+ vi = VFS_I(ni);
+ }
+
+ if (!datasync || !non_resident)
ret = __ntfs_write_inode(vi, 1);
write_inode_now(vi, !datasync);
+ /*
+ * file_write_and_wait_range() already flushed this stream mapping.
+ * A stream fsync does not need to flush sibling attribute mappings.
+ */
+ if (stream)
+ goto sync_volume;
+
ctx = ntfs_attr_get_search_ctx(ni, NULL);
if (!ctx)
return -ENOMEM;
@@ -210,23 +240,55 @@ static int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,
name = (__le16 *)((u8 *)ctx->attr + le16_to_cpu(ctx->attr->name_offset));
if (ctx->attr->type == AT_DATA && ctx->attr->name_length == 0)
continue;
+ if (ctx->attr->type == AT_DATA &&
+ ntfs_stream_unlinked(ni, name, ctx->attr->name_length))
+ continue;
+ attr = kmalloc_obj(*attr, GFP_NOFS);
+ if (!attr) {
+ err = -ENOMEM;
+ break;
+ }
attr_vi = ntfs_attr_iget(vi, ctx->attr->type,
name, ctx->attr->name_length);
- if (IS_ERR(attr_vi))
+ if (IS_ERR(attr_vi)) {
+ kfree(attr);
continue;
- spin_lock(&attr_vi->i_lock);
- if (inode_state_read_once(attr_vi) & I_DIRTY_PAGES) {
- spin_unlock(&attr_vi->i_lock);
- filemap_write_and_wait(attr_vi->i_mapping);
- } else
- spin_unlock(&attr_vi->i_lock);
- iput(attr_vi);
+ }
+ if (ntfs_inode_is_named_stream(NTFS_I(attr_vi)))
+ atomic_inc(&NTFS_I(attr_vi)->stream_open_count);
+ attr->inode = attr_vi;
+ list_add_tail(&attr->list, &attrs);
}
}
mutex_unlock(&ni->mrec_lock);
ntfs_attr_put_search_ctx(ctx);
+ if (err != -ENOENT && !ret)
+ ret = err;
+ /* Attribute writeback takes the base inode's MFT record lock. */
+ list_for_each_entry_safe(attr, next, &attrs, list) {
+ struct inode *attr_vi = attr->inode;
+
+ err = filemap_write_and_wait(attr_vi->i_mapping);
+ if (err && !ret)
+ ret = err;
+ if (ntfs_inode_is_named_stream(NTFS_I(attr_vi))) {
+ err = ntfs_stream_put(attr_vi);
+ if (err && !ret)
+ ret = err;
+ }
+ iput(attr_vi);
+ list_del(&attr->list);
+ kfree(attr);
+ }
+
+ /* Attribute writeback may have updated the base mapping pairs. */
+ err = write_inode_now(vi, 1);
+ if (err && !ret)
+ ret = err;
+
+sync_volume:
write_inode_now(vol->mftbmp_ino, 1);
down_write(&vol->lcnbmp_lock);
write_inode_now(vol->lcnbmp_ino, 1);
@@ -268,12 +330,17 @@ static void ntfs_pagecache_extend(struct inode *vi, loff_t from, loff_t to)
PAGE_SIZE, 0);
}
-static int ntfs_setattr_size(struct inode *vi, struct iattr *attr)
+int ntfs_setattr_size(struct inode *vi, struct iattr *attr)
{
struct ntfs_inode *ni = NTFS_I(vi);
+ struct ntfs_inode *base_ni = ntfs_base_inode(ni);
+ bool stream = ntfs_inode_is_named_stream(ni);
int err;
loff_t old_size = vi->i_size;
+ if (stream && NVolShutdown(ni->vol))
+ return -EIO;
+
if (NInoCompressed(ni) || NInoEncrypted(ni) || NInoWofCompressed(ni)) {
ntfs_warning(
vi->i_sb,
@@ -293,7 +360,21 @@ static int ntfs_setattr_size(struct inode *vi, struct iattr *attr)
* readers cannot observe the size change until the attribute
* updates below have completed.
*/
- filemap_invalidate_lock(vi->i_mapping);
+ if (stream) {
+ filemap_invalidate_lock(vi->i_mapping);
+ err = filemap_write_and_wait(vi->i_mapping);
+ if (err)
+ goto out_unlock_mapping;
+
+ mutex_lock(&base_ni->mrec_lock);
+ err = ntfs_stream_inode_validate(vi);
+ mutex_unlock(&base_ni->mrec_lock);
+ if (err)
+ goto out_unlock_mapping;
+ } else {
+ filemap_invalidate_lock(vi->i_mapping);
+ }
+
if (attr->ia_size > old_size) {
truncate_pagecache(vi, old_size);
i_size_write(vi, attr->ia_size);
@@ -302,10 +383,25 @@ static int ntfs_setattr_size(struct inode *vi, struct iattr *attr)
truncate_setsize(vi, attr->ia_size);
}
- err = ntfs_truncate_vfs(vi, attr->ia_size, old_size);
- if (err)
+ if (stream) {
+ mutex_lock(&base_ni->mrec_lock);
+ err = ntfs_stream_inode_validate(vi);
+ if (!err)
+ err = __ntfs_attr_truncate_vfs(ni, attr->ia_size,
+ old_size);
+ mutex_unlock(&base_ni->mrec_lock);
+ } else {
+ err = ntfs_truncate_vfs(vi, attr->ia_size, old_size);
+ }
+ if (err) {
i_size_write(vi, old_size);
+ goto out_unlock_mapping;
+ }
+
+out_unlock_mapping:
filemap_invalidate_unlock(vi->i_mapping);
+ if (!err && stream)
+ ntfs_stream_update_base_time(base_ni);
return err;
}
@@ -437,7 +533,25 @@ int ntfs_getattr(struct mnt_idmap *idmap, const struct path *path,
return 0;
}
-static loff_t ntfs_file_llseek(struct file *file, loff_t offset, int whence)
+/*
+ * Validate a stream before VFS write handling removes privileges or updates
+ * timestamps on its base inode.
+ */
+static int ntfs_file_modified(struct kiocb *iocb)
+{
+ struct inode *inode = file_inode(iocb->ki_filp);
+ int err;
+
+ if (ntfs_inode_is_named_stream(NTFS_I(inode))) {
+ err = ntfs_stream_validate_for_mutation(inode,
+ iocb->ki_flags & IOCB_NOWAIT);
+ if (err)
+ return err;
+ }
+ return kiocb_modified(iocb);
+}
+
+loff_t ntfs_file_llseek(struct file *file, loff_t offset, int whence)
{
struct inode *inode = file->f_mapping->host;
@@ -466,7 +580,7 @@ static loff_t ntfs_file_llseek(struct file *file, loff_t offset, int whence)
return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
}
-static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
+ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
{
struct inode *vi = file_inode(iocb->ki_filp);
struct super_block *sb = vi->i_sb;
@@ -513,7 +627,11 @@ static int ntfs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
if (size) {
if (i_size_read(inode) < iocb->ki_pos + size) {
i_size_write(inode, iocb->ki_pos + size);
- mark_inode_dirty(inode);
+ if (ntfs_inode_is_named_stream(NTFS_I(inode)))
+ mark_inode_dirty(VFS_I(
+ NTFS_I(inode)->ext.base_ntfs_ino));
+ else
+ mark_inode_dirty(inode);
}
}
@@ -583,6 +701,7 @@ static ssize_t ntfs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
static int ntfs_expand_for_write(struct ntfs_inode *ni, loff_t end)
{
struct ntfs_volume *vol = ni->vol;
+ struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
loff_t prealloc_size = 0;
int err;
@@ -598,14 +717,14 @@ static int ntfs_expand_for_write(struct ntfs_inode *ni, loff_t end)
prealloc_size = ni->allocated_size + vol->preallocated_size;
}
- mutex_lock(&ni->mrec_lock);
+ mutex_lock(&mrec_ni->mrec_lock);
err = ntfs_attr_expand(ni, end, prealloc_size);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
return err;
}
-static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
+ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
{
struct file *file = iocb->ki_filp;
struct inode *vi = file->f_mapping->host;
@@ -642,7 +761,7 @@ static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
if (ret <= 0)
goto out_lock;
- err = file_modified(iocb->ki_filp);
+ err = ntfs_file_modified(iocb);
if (err) {
ret = err;
goto out_lock;
@@ -692,13 +811,21 @@ static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
out:
if (ret < 0 && ret != -EIOCBQUEUED) {
if (ni->initialized_size != old_init_size) {
- mutex_lock(&ni->mrec_lock);
+ struct ntfs_inode *mrec_ni =
+ ntfs_base_inode(ni);
+
+ mutex_lock(&mrec_ni->mrec_lock);
ntfs_attr_set_initialized_size(ni, old_init_size);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
}
if (ni->data_size != old_data_size) {
+ struct ntfs_inode *mrec_ni =
+ ntfs_base_inode(ni);
+
truncate_setsize(vi, old_data_size);
+ mutex_lock(&mrec_ni->mrec_lock);
ntfs_attr_truncate(ni, old_data_size);
+ mutex_unlock(&mrec_ni->mrec_lock);
}
}
out_lock:
@@ -712,13 +839,24 @@ static vm_fault_t ntfs_filemap_page_mkwrite(struct vm_fault *vmf)
{
struct inode *inode = file_inode(vmf->vma->vm_file);
struct address_space *mapping = inode->i_mapping;
+ bool stream = ntfs_inode_is_named_stream(NTFS_I(inode));
+ int err;
vm_fault_t ret;
if (NInoWofCompressed(NTFS_I(inode)))
return VM_FAULT_SIGBUS;
sb_start_pagefault(inode->i_sb);
- file_update_time(vmf->vma->vm_file);
+ if (stream) {
+ err = ntfs_stream_validate_for_mutation(inode, false);
+ if (err)
+ goto out_error;
+ err = file_update_time(vmf->vma->vm_file);
+ if (err)
+ goto out_error;
+ } else {
+ file_update_time(vmf->vma->vm_file);
+ }
/*
* Serialize against truncate/fallocate which hold the lock
@@ -727,9 +865,12 @@ static vm_fault_t ntfs_filemap_page_mkwrite(struct vm_fault *vmf)
filemap_invalidate_lock_shared(mapping);
ret = iomap_page_mkwrite(vmf, &ntfs_page_mkwrite_iomap_ops, NULL);
filemap_invalidate_unlock_shared(mapping);
-
sb_end_pagefault(inode->i_sb);
return ret;
+
+out_error:
+ sb_end_pagefault(inode->i_sb);
+ return vmf_error(err);
}
static const struct vm_operations_struct ntfs_file_vm_ops = {
@@ -738,7 +879,7 @@ static const struct vm_operations_struct ntfs_file_vm_ops = {
.page_mkwrite = ntfs_filemap_page_mkwrite,
};
-static int ntfs_file_mmap_prepare(struct vm_area_desc *desc)
+int ntfs_file_mmap_prepare(struct vm_area_desc *desc)
{
struct file *file = desc->file;
struct inode *inode = file_inode(file);
@@ -771,7 +912,7 @@ static int ntfs_file_mmap_prepare(struct vm_area_desc *desc)
return 0;
}
-static int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
+int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
u64 start, u64 len)
{
if (NInoWofCompressed(NTFS_I(inode)))
@@ -808,7 +949,7 @@ static const char *ntfs_get_link(struct dentry *dentry, struct inode *inode,
return ni->target;
}
-static ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos,
+ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos,
struct pipe_inode_info *pipe, size_t len, unsigned int flags)
{
if (NVolShutdown(NTFS_SB(in->f_mapping->host->i_sb)))
@@ -924,6 +1065,14 @@ long ntfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
return ntfs_ioctl_set_volume_label(filp, arg);
case FITRIM:
return ntfs_ioctl_fitrim(NTFS_SB(file_inode(filp)->i_sb), arg);
+ case NTFS_IOC_STREAM_READ:
+ return ntfs_ioctl_stream_read(filp, arg);
+ case NTFS_IOC_STREAM_WRITE:
+ return ntfs_ioctl_stream_write(filp, arg);
+ case NTFS_IOC_STREAM_REMOVE:
+ return ntfs_ioctl_stream_remove(filp, arg);
+ case NTFS_IOC_LIST_STREAMS:
+ return ntfs_ioctl_list_streams(filp, arg);
default:
return -ENOTTY;
}
@@ -980,6 +1129,7 @@ static int ntfs_allocate_range(struct ntfs_inode *ni, int mode, loff_t offset,
static int ntfs_punch_hole(struct ntfs_inode *ni, int mode, loff_t offset,
loff_t len)
{
+ struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct inode *vi = VFS_I(ni);
loff_t end_offset;
@@ -1044,16 +1194,17 @@ static int ntfs_punch_hole(struct ntfs_inode *ni, int mode, loff_t offset,
end_vcn--;
}
- mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
+ mutex_lock_nested(&mrec_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
err = ntfs_non_resident_attr_punch_hole(ni, start_vcn,
end_vcn - start_vcn);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
out:
return err;
}
static int ntfs_collapse_range(struct ntfs_inode *ni, loff_t offset, loff_t len)
{
+ struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct inode *vi = VFS_I(ni);
loff_t old_size, new_size;
@@ -1087,10 +1238,10 @@ static int ntfs_collapse_range(struct ntfs_inode *ni, loff_t offset, loff_t len)
truncate_pagecache(vi, offset_down);
- mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
+ mutex_lock_nested(&mrec_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
err = ntfs_non_resident_attr_collapse_range(ni, start_vcn,
end_vcn - start_vcn);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
if (new_size != old_size)
i_size_write(vi, ni->data_size);
@@ -1100,6 +1251,7 @@ static int ntfs_collapse_range(struct ntfs_inode *ni, loff_t offset, loff_t len)
static int ntfs_insert_range(struct ntfs_inode *ni, loff_t offset, loff_t len)
{
+ struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct inode *vi = VFS_I(ni);
loff_t offset_down = round_down(offset,
@@ -1143,10 +1295,10 @@ static int ntfs_insert_range(struct ntfs_inode *ni, loff_t offset, loff_t len)
truncate_pagecache(vi, offset_down);
- mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
+ mutex_lock_nested(&mrec_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
err = ntfs_non_resident_attr_insert_range(ni, start_vcn,
end_vcn - start_vcn);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
if (new_size != old_size)
i_size_write(vi, ni->data_size);
@@ -1159,11 +1311,13 @@ static int ntfs_insert_range(struct ntfs_inode *ni, loff_t offset, loff_t len)
FALLOC_FL_INSERT_RANGE | FALLOC_FL_PUNCH_HOLE | \
FALLOC_FL_COLLAPSE_RANGE)
-static long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
+long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
{
struct inode *vi = file_inode(file);
struct ntfs_inode *ni = NTFS_I(vi);
+ struct ntfs_inode *base_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
+ bool stream = ntfs_inode_is_named_stream(ni);
int err = 0;
loff_t old_size, new_size;
@@ -1193,6 +1347,13 @@ static long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t le
inode_unlock(vi);
return -EOPNOTSUPP;
}
+ if (stream) {
+ err = ntfs_stream_validate_for_mutation(vi, false);
+ if (err) {
+ inode_unlock(vi);
+ return err;
+ }
+ }
old_size = i_size_read(vi);
inode_dio_wait(vi);
@@ -1233,9 +1394,13 @@ static long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t le
filemap_invalidate_unlock(vi->i_mapping);
if (!err) {
- NInoSetFileNameDirty(ni);
- inode_set_mtime_to_ts(vi, inode_set_ctime_current(vi));
- mark_inode_dirty(vi);
+ if (stream) {
+ ntfs_stream_update_base_time(base_ni);
+ } else {
+ NInoSetFileNameDirty(ni);
+ inode_set_mtime_to_ts(vi, inode_set_ctime_current(vi));
+ mark_inode_dirty(vi);
+ }
}
inode_unlock(vi);
diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c
index 6cfe8f24e4cce..93680a11fdccc 100644
--- a/fs/ntfs/inode.c
+++ b/fs/ntfs/inode.c
@@ -20,6 +20,7 @@
#include "attrib.h"
#include "iomap.h"
#include "object_id.h"
+#include "stream.h"
/*
* ntfs_test_inode - compare two (possibly fake) inodes for equality
@@ -56,9 +57,18 @@ int ntfs_test_inode(struct inode *vi, void *data)
return 0;
if (ni->name_len != na->name_len)
return 0;
- if (na->name_len && memcmp(ni->name, na->name,
- na->name_len * sizeof(__le16)))
- return 0;
+ if (na->name_len) {
+ if (ntfs_inode_is_named_stream(ni)) {
+ if (!ntfs_names_are_equal(ni->name, ni->name_len,
+ na->name, na->name_len,
+ IGNORE_CASE, ni->vol->upcase,
+ ni->vol->upcase_len))
+ return 0;
+ } else if (memcmp(ni->name, na->name,
+ na->name_len * sizeof(__le16))) {
+ return 0;
+ }
+ }
if (!ni->ext.base_ntfs_ino)
return 0;
}
@@ -67,6 +77,33 @@ int ntfs_test_inode(struct inode *vi, void *data)
return 1;
}
+/*
+ * ntfs_test_inode_rcu() - Test an inode during RCU hash lookup
+ * @vi: inode being tested
+ * @data: NTFS attribute key to match
+ *
+ * Reject inodes being initialized or destroyed, then run the normal NTFS
+ * inode match while holding @vi's inode lock.
+ *
+ * Return: 1 if the inode matches, or 0 otherwise.
+ */
+int ntfs_test_inode_rcu(struct inode *vi, void *data)
+{
+ unsigned long state;
+ int ret;
+
+ spin_lock(&vi->i_lock);
+ state = inode_state_read_once(vi);
+ if (state & (I_FREEING | I_WILL_FREE | I_NEW)) {
+ ret = 0;
+ goto out;
+ }
+ ret = ntfs_test_inode(vi, data);
+out:
+ spin_unlock(&vi->i_lock);
+ return ret;
+}
+
/*
* ntfs_init_locked_inode - initialize an inode
* @vi: vfs inode to initialize
@@ -468,6 +505,7 @@ void __ntfs_init_inode(struct super_block *sb, struct ntfs_inode *ni)
ni->seq_no = 0;
atomic_set(&ni->count, 1);
ni->vol = NTFS_SB(sb);
+ atomic_set(&ni->stream_open_count, 0);
ntfs_init_runlist(&ni->runlist);
mutex_init(&ni->mrec_lock);
if (ni->type == AT_ATTRIBUTE_LIST) {
@@ -1320,6 +1358,7 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
struct attr_record *a;
struct ntfs_attr_search_ctx *ctx;
int err = 0;
+ u32 ic;
ntfs_debug("Entering for i_ino 0x%llx.", ni->mft_no);
@@ -1334,8 +1373,12 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
inode_set_atime_to_ts(vi, inode_get_atime(base_vi));
vi->i_generation = ni->seq_no = base_ni->seq_no;
- /* Set inode type to zero but preserve permissions. */
- vi->i_mode = base_vi->i_mode & ~S_IFMT;
+ /* Named data streams are regular files throughout initialization. */
+ vi->i_mode = base_vi->i_mode & ~S_IFMT;
+ if (ni->type == AT_DATA && ni->name_len) {
+ vi->i_mode |= S_IFREG;
+ vi->i_flags = base_vi->i_flags;
+ }
m = map_mft_record(base_ni);
if (IS_ERR(m)) {
@@ -1348,11 +1391,29 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
goto unm_err_out;
}
/* Find the attribute. */
+ ic = ntfs_inode_is_named_stream(ni) ? IGNORE_CASE : CASE_SENSITIVE;
err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
- CASE_SENSITIVE, 0, NULL, 0, ctx);
+ ic, 0, NULL, 0, ctx);
if (unlikely(err))
goto unm_err_out;
a = ctx->attr;
+ if (ntfs_inode_is_named_stream(ni)) {
+ __le16 *disk_name = (__le16 *)((u8 *)a +
+ le16_to_cpu(a->name_offset));
+
+ /* Subsequent extent and mutation lookups must select this name. */
+ if (ni->name == I30) {
+ ni->name = kmalloc_array(ni->name_len + 1,
+ sizeof(__le16), GFP_NOFS);
+ if (!ni->name) {
+ err = -ENOMEM;
+ goto unm_err_out;
+ }
+ }
+ memcpy(ni->name, disk_name,
+ ni->name_len * sizeof(__le16));
+ ni->name[ni->name_len] = 0;
+ }
if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) {
if (a->flags & ATTR_COMPRESSION_MASK) {
NInoSetCompressed(ni);
@@ -1486,6 +1547,10 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
vi->i_blocks = ni->itype.compressed.size >> 9;
else
vi->i_blocks = ni->allocated_size >> 9;
+ if (ni->type == AT_DATA && ni->name_len) {
+ vi->i_op = &ntfs_stream_inode_ops;
+ vi->i_fop = &ntfs_stream_file_ops;
+ }
/*
* Make sure the base inode does not go away and attach it to the
* attribute inode.
@@ -2476,6 +2541,10 @@ int ntfs_show_options(struct seq_file *sf, struct dentry *root)
seq_puts(sf, ",symlink=native");
else
seq_puts(sf, ",symlink=wsl");
+ if (NVolStreamsWindows(vol))
+ seq_puts(sf, ",streams_interface=windows");
+ else
+ seq_puts(sf, ",streams_interface=none");
if (vol->sb->s_flags & SB_POSIXACL)
seq_puts(sf, ",acl");
return 0;
@@ -2485,6 +2554,7 @@ int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset,
const loff_t new_size)
{
struct ntfs_inode *ni = NTFS_I(vi);
+ struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
loff_t old_init_size, clear_size;
unsigned long flags;
int err = 0;
@@ -2522,9 +2592,9 @@ int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset,
}
}
- mutex_lock(&ni->mrec_lock);
+ mutex_lock(&mrec_ni->mrec_lock);
err = ntfs_attr_set_initialized_size(ni, new_size);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
if (err)
truncate_setsize(vi, old_init_size);
out:
@@ -2535,15 +2605,19 @@ int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset,
int ntfs_truncate_vfs(struct inode *vi, loff_t new_size, loff_t i_size)
{
struct ntfs_inode *ni = NTFS_I(vi);
+ struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
int err;
- mutex_lock(&ni->mrec_lock);
+ mutex_lock(&mrec_ni->mrec_lock);
err = __ntfs_attr_truncate_vfs(ni, new_size, i_size);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
if (err < 0)
return err;
- inode_set_mtime_to_ts(vi, inode_set_ctime_current(vi));
+ if (ntfs_inode_is_named_stream(ni))
+ ntfs_stream_update_base_time(mrec_ni);
+ else
+ inode_set_mtime_to_ts(vi, inode_set_ctime_current(vi));
return 0;
}
@@ -3615,19 +3689,30 @@ s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf)
struct address_space *mapping = vi->i_mapping;
struct folio *folio;
struct ntfs_inode *ni = NTFS_I(vi);
+ struct ntfs_inode *base_ni = ni->ext.base_ntfs_ino;
+ struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?
+ base_ni : ni;
s64 isize;
u32 attr_len, total = 0, offset;
pgoff_t index;
int err = 0;
+ bool claimed = false;
WARN_ON(!NInoAttr(ni));
if (!count)
return 0;
- mutex_lock(&ni->mrec_lock);
+ mutex_lock(&mrec_ni->mrec_lock);
+ if (ntfs_inode_is_named_stream(ni)) {
+ err = ntfs_stream_inode_validate(vi);
+ if (err) {
+ mutex_unlock(&mrec_ni->mrec_lock);
+ return err;
+ }
+ }
isize = i_size_read(vi);
if (pos > isize) {
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
return -EINVAL;
}
if (pos + count > isize)
@@ -3637,37 +3722,44 @@ s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf)
struct ntfs_attr_search_ctx *ctx;
u8 *attr;
- ctx = ntfs_attr_get_search_ctx(ni->ext.base_ntfs_ino, NULL);
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ntfs_error(vi->i_sb, "Failed to get attr search ctx");
err = -ENOMEM;
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
goto out;
}
- err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE,
+ err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE,
0, NULL, 0, ctx);
if (err) {
ntfs_error(vi->i_sb, "Failed to look up attr %#x", ni->type);
ntfs_attr_put_search_ctx(ctx);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
goto out;
}
attr = (u8 *)ctx->attr + le16_to_cpu(ctx->attr->data.resident.value_offset);
memcpy(buf, (u8 *)attr + pos, count);
ntfs_attr_put_search_ctx(ctx);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
return count;
}
- mutex_unlock(&ni->mrec_lock);
+ if (ntfs_inode_is_named_stream(ni)) {
+ atomic_inc(&ni->stream_open_count);
+ claimed = true;
+ }
+ mutex_unlock(&mrec_ni->mrec_lock);
index = pos >> PAGE_SHIFT;
do {
/* Update @index and get the next folio. */
folio = read_mapping_folio(mapping, index, NULL);
- if (IS_ERR(folio))
+ if (IS_ERR(folio)) {
+ err = PTR_ERR(folio);
break;
+ }
offset = offset_in_folio(folio, pos);
attr_len = min_t(size_t, (size_t)count, folio_size(folio) - offset);
@@ -3684,6 +3776,8 @@ s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf)
index++;
} while (count);
out:
+ if (claimed)
+ ntfs_stream_put(vi);
return err ? (s64)err : total;
}
@@ -3711,8 +3805,8 @@ static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,
}
ntfs_attr_reinit_search_ctx(ctx);
- ret = ntfs_attr_lookup(ni->type,
- ni->name, ni->name_len, CASE_SENSITIVE,
+ ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE,
0, NULL, 0, ctx);
if (ret) {
ntfs_error(sb, "Failed to look up attr %#x", ni->type);
@@ -3726,6 +3820,13 @@ static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,
return 0;
}
+ /* Named streams initialize gaps after dropping the MFT record lock. */
+ if (ntfs_inode_is_named_stream(ni)) {
+ if (i_size_read(vi) < ni->data_size)
+ i_size_write(vi, ni->data_size);
+ return 0;
+ }
+
if (pos + count > ni->initialized_size) {
ctx->attr->data.non_resident.initialized_size = cpu_to_le64(pos + count);
mark_mft_record_dirty(ctx->ntfs_ino);
@@ -3780,7 +3881,6 @@ static s64 __ntfs_inode_resident_attr_pwrite(struct inode *vi,
static s64 __ntfs_inode_non_resident_attr_pwrite(struct inode *vi,
s64 pos, s64 count, u8 *buf,
- struct ntfs_attr_search_ctx *ctx,
bool sync)
{
struct ntfs_inode *ni = NTFS_I(vi);
@@ -3919,37 +4019,79 @@ static s64 __ntfs_inode_non_resident_attr_pwrite(struct inode *vi,
s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool sync)
{
struct ntfs_inode *ni = NTFS_I(vi);
+ struct ntfs_inode *base_ni = ni->ext.base_ntfs_ino;
+ struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?
+ base_ni : ni;
struct ntfs_attr_search_ctx *ctx;
s64 ret;
WARN_ON(!NInoAttr(ni));
- ctx = ntfs_attr_get_search_ctx(ni->ext.base_ntfs_ino, NULL);
+ if (ntfs_inode_is_named_stream(ni)) {
+ if (NInoEncrypted(ni))
+ return -EACCES;
+ if (NInoCompressed(ni))
+ return -EOPNOTSUPP;
+ }
+
+ mutex_lock(&mrec_ni->mrec_lock);
+ if (ntfs_inode_is_named_stream(ni)) {
+ ret = ntfs_stream_inode_validate(vi);
+ if (ret)
+ goto out_unlock;
+ }
+
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ntfs_error(vi->i_sb, "Failed to get attr search ctx");
- return -ENOMEM;
+ ret = -ENOMEM;
+ goto out_unlock;
}
- ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE,
+ ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE,
0, NULL, 0, ctx);
if (ret) {
ntfs_attr_put_search_ctx(ctx);
ntfs_error(vi->i_sb, "Failed to look up attr %#x", ni->type);
- return ret;
+ goto out_unlock;
}
- mutex_lock(&ni->mrec_lock);
ret = ntfs_enlarge_attribute(vi, pos, count, ctx);
- mutex_unlock(&ni->mrec_lock);
if (ret)
- goto out;
+ goto out_ctx;
- if (NInoNonResident(ni))
- ret = __ntfs_inode_non_resident_attr_pwrite(vi, pos, count, buf, ctx, sync);
- else
+ if (!NInoNonResident(ni)) {
ret = __ntfs_inode_resident_attr_pwrite(vi, pos, count, buf, ctx);
-out:
+ goto out_ctx;
+ }
+
+ if (ntfs_inode_is_named_stream(ni))
+ atomic_inc(&ni->stream_open_count);
+ ntfs_attr_put_search_ctx(ctx);
+ mutex_unlock(&mrec_ni->mrec_lock);
+ /* Attribute writes bypass iomap's delayed-allocation preparation. */
+ if (ntfs_inode_is_named_stream(ni)) {
+ /* A failed write must not expose any newly initialized data. */
+ ret = ntfs_extend_initialized_size(vi, pos + count, pos + count);
+ if (!ret)
+ ret = ntfs_attr_fallocate(ni, pos, count, true);
+ }
+ if (!ret)
+ ret = __ntfs_inode_non_resident_attr_pwrite(vi, pos, count,
+ buf, sync);
+ if (ntfs_inode_is_named_stream(ni))
+ ntfs_stream_put(vi);
+ if (ret > 0 && ntfs_inode_is_named_stream(ni))
+ ntfs_stream_update_base_time(base_ni);
+ return ret;
+
+out_ctx:
ntfs_attr_put_search_ctx(ctx);
+out_unlock:
+ mutex_unlock(&mrec_ni->mrec_lock);
+ if (ret > 0 && ntfs_inode_is_named_stream(ni))
+ ntfs_stream_update_base_time(base_ni);
return ret;
}
diff --git a/fs/ntfs/inode.h b/fs/ntfs/inode.h
index ff61bd402df03..f4e5562d56a7e 100644
--- a/fs/ntfs/inode.h
+++ b/fs/ntfs/inode.h
@@ -107,6 +107,7 @@ struct ntfs_inode {
__le32 type;
__le16 *name;
u32 name_len;
+ atomic_t stream_open_count;
struct runlist runlist;
s64 data_size;
s64 initialized_size;
@@ -178,6 +179,7 @@ struct ntfs_inode {
* NI_BeingCreated ntfs inode is being created.
* NI_HasEA ntfs inode has EA attribute.
* NI_RunlistDirty runlist need to be updated.
+ * NI_StreamUnlinked Named stream is unlinked but still open.
*/
enum {
NI_Dirty,
@@ -199,6 +201,7 @@ enum {
NI_BeingCreated,
NI_HasEA,
NI_RunlistDirty,
+ NI_StreamUnlinked,
};
/*
@@ -259,6 +262,7 @@ TAS_NINO_FNS(FileNameDirty)
NINO_FNS(BeingDeleted)
NINO_FNS(HasEA)
NINO_FNS(RunlistDirty)
+NINO_FNS(StreamUnlinked)
/*
* The full structure containing a ntfs_inode and a vfs struct inode. Used for
@@ -286,6 +290,20 @@ static inline struct inode *VFS_I(struct ntfs_inode *ni)
return &container_of(ni, struct big_ntfs_inode, ntfs_inode)->vfs_inode;
}
+/* Return true when @ni represents a named $DATA attribute inode. */
+static inline bool ntfs_inode_is_named_stream(struct ntfs_inode *ni)
+{
+ return NInoAttr(ni) && ni->type == AT_DATA && ni->name_len;
+}
+
+/* Return the base MFT inode for an attribute inode, or @ni itself. */
+static inline struct ntfs_inode *ntfs_base_inode(struct ntfs_inode *ni)
+{
+ if (NInoAttr(ni) && ni->nr_extents == -1 && ni->ext.base_ntfs_ino)
+ return ni->ext.base_ntfs_ino;
+ return ni;
+}
+
/*
* ntfs_attr - ntfs in memory attribute structure
*
@@ -304,6 +322,7 @@ struct ntfs_attr {
};
int ntfs_test_inode(struct inode *vi, void *data);
+int ntfs_test_inode_rcu(struct inode *vi, void *data);
struct inode *ntfs_iget(struct super_block *sb, u64 mft_no);
struct inode *ntfs_attr_iget(struct inode *base_vi, __le32 type,
__le16 *name, u32 name_len);
diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
index 754d5d91b6dc6..2940a5587add2 100644
--- a/fs/ntfs/iomap.c
+++ b/fs/ntfs/iomap.c
@@ -64,11 +64,7 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo
int err = 0;
char *kattr;
- if (NInoAttr(ni))
- base_ni = ni->ext.base_ntfs_ino;
- else
- base_ni = ni;
-
+ base_ni = ntfs_base_inode(ni);
mutex_lock(&base_ni->mrec_lock);
ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
@@ -114,7 +110,8 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo
if (ctx)
ntfs_attr_put_search_ctx(ctx);
- if (!err && keep_mrec_lock && iomap->type == IOMAP_INLINE) {
+ if (!err && keep_mrec_lock &&
+ iomap->type == IOMAP_INLINE) {
iomap->private = base_ni;
return 0;
}
@@ -399,6 +396,8 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
loff_t length, struct iomap *iomap)
{
struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?
+ ntfs_base_inode(ni) : ni;
struct ntfs_volume *vol = ni->vol;
loff_t vcn_ofs, rl_length;
struct runlist_element *rl, *rlc;
@@ -417,7 +416,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
up_read(&ni->runlist.lock);
err = ntfs_map_runlist(ni, vcn);
if (err) {
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
return -ENOENT;
}
down_read(&ni->runlist.lock);
@@ -431,7 +430,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
rl = __ntfs_attr_find_vcn_nolock(&ni->runlist, vcn);
if (IS_ERR(rl)) {
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
return -EIO;
}
lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
@@ -452,7 +451,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
*/
if (lcn == LCN_RL_NOT_MAPPED) {
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
return map_err == -ENOMEM ? -ENOMEM : -EIO;
}
@@ -462,7 +461,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
"runlist(vcn : %lld, length : %lld) is corrupted\n",
rl->vcn, rl->length);
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
return -EIO;
}
@@ -476,7 +475,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
if (max_clu_count < 0) {
err = max_clu_count;
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
return err;
}
}
@@ -491,7 +490,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
GFP_NOFS);
if (!rlc) {
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
return -ENOMEM;
}
@@ -508,7 +507,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
if (IS_ERR(rl)) {
ntfs_error(vol->sb, "Failed to merge runlists");
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
kvfree(rlc);
return PTR_ERR(rl);
}
@@ -518,7 +517,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
ni->i_dealloc_clusters += max_clu_count;
}
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
if (lcn < LCN_DELALLOC)
ntfs_hold_dirty_clusters(vol, max_clu_count);
@@ -570,7 +569,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
}
} else {
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
iomap->type = IOMAP_MAPPED;
iomap->addr = ntfs_cluster_to_bytes(vol, lcn) + vcn_ofs;
@@ -595,6 +594,8 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
struct iomap *iomap, int ntfs_iomap_flags)
{
struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?
+ ntfs_base_inode(ni) : ni;
struct ntfs_volume *vol = ni->vol;
loff_t vcn_ofs, rl_length;
s64 vcn, start_lcn, lcn_count;
@@ -613,7 +614,7 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
max_clu_count, &balloc, update_mp,
ntfs_iomap_flags & NTFS_IOMAP_FLAGS_WRITEBACK);
up_write(&ni->runlist.lock);
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
if (err) {
ni->i_dealloc_clusters = 0;
return err;
@@ -667,8 +668,8 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
if (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE &&
iomap->offset + iomap->length > ni->initialized_size) {
- err = ntfs_attr_set_initialized_size(ni, iomap->offset +
- iomap->length);
+ err = ntfs_attr_set_initialized_size(ni,
+ iomap->offset + iomap->length);
}
return err;
@@ -678,13 +679,15 @@ static int ntfs_write_iomap_begin_resident(struct inode *inode, loff_t offset,
struct iomap *iomap)
{
struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?
+ ntfs_base_inode(ni) : ni;
struct attr_record *a;
- struct ntfs_attr_search_ctx *ctx;
+ struct ntfs_attr_search_ctx *ctx = NULL;
u32 attr_len;
int err = 0;
char *kattr;
- ctx = ntfs_attr_get_search_ctx(ni, NULL);
+ ctx = ntfs_attr_get_search_ctx(mrec_ni, NULL);
if (!ctx) {
err = -ENOMEM;
goto out;
@@ -707,13 +710,14 @@ static int ntfs_write_iomap_begin_resident(struct inode *inode, loff_t offset,
iomap->inline_data = kattr;
iomap->offset = 0;
iomap->length = attr_len;
+ iomap->private = mrec_ni;
out:
if (ctx)
ntfs_attr_put_search_ctx(ctx);
if (err)
- mutex_unlock(&ni->mrec_lock);
+ mutex_unlock(&mrec_ni->mrec_lock);
return err;
}
@@ -722,7 +726,12 @@ static int ntfs_write_iomap_begin_non_resident(struct inode *inode, loff_t offse
loff_t length, unsigned int flags,
struct iomap *iomap, int ntfs_iomap_flags)
{
- mutex_lock(&NTFS_I(inode)->mrec_lock);
+ struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?
+ ntfs_base_inode(ni) : ni;
+
+ mutex_lock(&mrec_ni->mrec_lock);
+
if (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_BEGIN)
return ntfs_write_simple_iomap_begin_non_resident(inode, offset,
length, iomap);
@@ -738,12 +747,15 @@ static int __ntfs_write_iomap_begin(struct inode *inode, loff_t offset,
struct iomap *iomap, int ntfs_iomap_flags)
{
struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_inode *mrec_ni;
if (NVolShutdown(ni->vol))
return -EIO;
if (!NInoNonResident(ni)) {
- mutex_lock(&ni->mrec_lock);
+ mrec_ni = ntfs_inode_is_named_stream(ni) ?
+ ntfs_base_inode(ni) : ni;
+ mutex_lock(&mrec_ni->mrec_lock);
return ntfs_write_iomap_begin_resident(inode, offset, iomap);
}
return ntfs_write_iomap_begin_non_resident(inode, offset, length, flags,
@@ -762,10 +774,10 @@ static int ntfs_write_iomap_end_resident(struct inode *inode, loff_t pos,
loff_t length, ssize_t written,
unsigned int flags, struct iomap *iomap)
{
- struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_inode *base_ni = iomap->private;
- mark_mft_record_dirty(ni);
- mutex_unlock(&ni->mrec_lock);
+ mark_mft_record_dirty(base_ni);
+ mutex_unlock(&base_ni->mrec_lock);
return written;
}
diff --git a/fs/ntfs/named_stream.c b/fs/ntfs/named_stream.c
new file mode 100644
index 0000000000000..e44dbf09057b3
--- /dev/null
+++ b/fs/ntfs/named_stream.c
@@ -0,0 +1,1821 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * NTFS named stream handling.
+ *
+ * Copyright (c) 2026 LG Electronics Co., Ltd.
+ */
+
+#include <linux/file.h>
+#include <linux/namei.h>
+#include <linux/overflow.h>
+
+#include <uapi/linux/ntfs.h>
+
+#include "attrib.h"
+#include "dir.h"
+#include "iomap.h"
+#include "mft.h"
+#include "ntfs.h"
+#include "stream.h"
+#include "time.h"
+
+#define NTFS_STREAM_MAX_IO (16 * 1024 * 1024)
+
+/*
+ * ntfs_check_stream_name() - Validate a named-stream name
+ * @name: UTF-16LE stream name
+ * @name_len: Length of @name in UTF-16 code units
+ *
+ * Reject empty, overlong, separator-containing, or malformed UTF-16 names.
+ *
+ * Return: 0 if valid, or -EINVAL otherwise.
+ */
+int ntfs_check_stream_name(const __le16 *name, unsigned int name_len)
+{
+ unsigned int i;
+
+ if (!name_len || name_len > NTFS_MAX_NAME_LEN)
+ return -EINVAL;
+
+ for (i = 0; i < name_len; i++) {
+ u16 c = le16_to_cpu(name[i]);
+
+ if (c == 0 || c == '/' || c == '\\' || c == ':')
+ return -EINVAL;
+ if (c >= 0xd800 && c <= 0xdbff) {
+ if (++i >= name_len)
+ return -EINVAL;
+ c = le16_to_cpu(name[i]);
+ if (c < 0xdc00 || c > 0xdfff)
+ return -EINVAL;
+ } else if (c >= 0xdc00 && c <= 0xdfff) {
+ return -EINVAL;
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * ntfs_stream_inode_refresh() - Refresh base-derived stream inode metadata
+ * @vi: inode representing a named stream
+ *
+ * Synchronize ownership, mode, flags, timestamps, and generation with the
+ * base inode. Stream size and allocation remain specific to the stream.
+ */
+void ntfs_stream_inode_refresh(struct inode *vi)
+{
+ struct ntfs_inode *ni = NTFS_I(vi);
+ struct inode *base_vi;
+
+ if (!ntfs_inode_is_named_stream(ni) || ni->nr_extents != -1)
+ return;
+
+ base_vi = VFS_I(ni->ext.base_ntfs_ino);
+ vi->i_uid = base_vi->i_uid;
+ vi->i_gid = base_vi->i_gid;
+ vi->i_mode = (base_vi->i_mode & ~S_IFMT) | S_IFREG;
+ vi->i_flags = base_vi->i_flags;
+ inode_set_mtime_to_ts(vi, inode_get_mtime(base_vi));
+ inode_set_ctime_to_ts(vi, inode_get_ctime(base_vi));
+ inode_set_atime_to_ts(vi, inode_get_atime(base_vi));
+ vi->i_generation = base_vi->i_generation;
+}
+
+/*
+ * ntfs_stream_update_base_time() - Update base timestamps after a stream change
+ * @base_ni: base inode containing the stream attribute
+ *
+ * Update the base inode's mtime and ctime and mark it dirty. The helper takes
+ * the base inode lock.
+ */
+void ntfs_stream_update_base_time(struct ntfs_inode *base_ni)
+{
+ struct inode *base_vi = VFS_I(base_ni);
+
+ inode_lock_nested(base_vi, I_MUTEX_PARENT);
+ inode_set_mtime_to_ts(base_vi, inode_set_ctime_current(base_vi));
+ mark_inode_dirty(base_vi);
+ inode_unlock(base_vi);
+}
+
+/*
+ * ntfs_stream_inode_validate() - Validate a named-stream inode
+ * @vi: stream inode to validate
+ *
+ * Verify that @vi still maps to its named DATA attribute and refresh its
+ * base-derived metadata. The caller must hold the base inode's MFT-record
+ * lock.
+ *
+ * Return: 0 if valid, or a negative errno.
+ */
+int ntfs_stream_inode_validate(struct inode *vi)
+{
+ struct ntfs_inode *ni = NTFS_I(vi);
+ struct ntfs_inode *base_ni;
+ struct ntfs_attr_search_ctx *ctx;
+ int err;
+
+ if (!ntfs_inode_is_named_stream(ni) || ni->nr_extents != -1)
+ return -EINVAL;
+
+ base_ni = ni->ext.base_ntfs_ino;
+ lockdep_assert_held(&base_ni->mrec_lock);
+ if (NVolShutdown(base_ni->vol))
+ return -EIO;
+ if (!NInoStreamUnlinked(ni) &&
+ (!vi->i_nlink || !VFS_I(base_ni)->i_nlink ||
+ NInoBeingDeleted(base_ni)))
+ return -ESTALE;
+
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
+ if (!ctx)
+ return -ENOMEM;
+ err = ntfs_attr_lookup(AT_DATA, ni->name, ni->name_len,
+ CASE_SENSITIVE, 0, NULL, 0, ctx);
+ ntfs_attr_put_search_ctx(ctx);
+ if (err)
+ return err;
+
+ ntfs_stream_inode_refresh(vi);
+ return 0;
+}
+
+/*
+ * ntfs_stream_unlinked() - Check a cached stream's deferred-unlink state
+ * @base_ni: base inode containing the stream
+ * @name: UTF-16LE stream name
+ * @name_len: Length of @name in UTF-16 code units
+ *
+ * The caller must hold the base inode's MFT-record lock.
+ *
+ * Return: true if the cached stream inode is unlinked, or false if it is not
+ * cached or is still linked.
+ */
+bool ntfs_stream_unlinked(struct ntfs_inode *base_ni,
+ const __le16 *name, u32 name_len)
+{
+ struct ntfs_attr na = {
+ .mft_no = base_ni->mft_no,
+ .type = AT_DATA,
+ .name = (__le16 *)name,
+ .name_len = name_len,
+ };
+ struct inode *vi;
+ bool unlinked;
+
+ lockdep_assert_held(&base_ni->mrec_lock);
+ rcu_read_lock();
+ vi = find_inode_rcu(base_ni->vol->sb, na.mft_no,
+ ntfs_test_inode_rcu, &na);
+ if (!vi) {
+ rcu_read_unlock();
+ return false;
+ }
+ unlinked = NInoStreamUnlinked(NTFS_I(vi));
+ rcu_read_unlock();
+ return unlinked;
+}
+
+/*
+ * ntfs_stream_path_parse() - Split a pathname stream component
+ * @vol: NTFS volume
+ * @qname: final pathname component
+ * @path: receives slices of @qname on success
+ *
+ * Parse the ``file:stream`` form when the Windows pathname interface is
+ * enabled. This does not convert or validate the component names.
+ *
+ * Return: 1 if stream syntax was parsed, 0 if streams are disabled or no
+ * stream separator is present, or -EINVAL for malformed syntax.
+ */
+int ntfs_stream_path_parse(struct ntfs_volume *vol,
+ const struct qstr *qname, struct ntfs_stream_path *path)
+{
+ const unsigned char *colon;
+
+ if (!NVolStreamsWindows(vol))
+ return 0;
+
+ colon = memchr(qname->name, ':', qname->len);
+ if (!colon)
+ return 0;
+ if (colon == qname->name || colon + 1 == qname->name + qname->len)
+ return -EINVAL;
+ if (memchr(colon + 1, ':', qname->name + qname->len - colon - 1))
+ return -EINVAL;
+
+ path->base_name = (const char *)qname->name;
+ path->base_len = colon - qname->name;
+ path->stream_name = (const char *)colon + 1;
+ path->stream_len = qname->name + qname->len - colon - 1;
+ return 1;
+}
+
+/*
+ * ntfs_stream_path_has_colon() - Check for a pathname stream separator
+ * @vol: NTFS volume
+ * @qname: final pathname component
+ *
+ * Return: true if the Windows pathname interface is enabled and @qname
+ * contains a colon, or false otherwise.
+ */
+bool ntfs_stream_path_has_colon(struct ntfs_volume *vol,
+ const struct qstr *qname)
+{
+ return NVolStreamsWindows(vol) &&
+ memchr(qname->name, ':', qname->len);
+}
+
+/*
+ * ntfs_stream_path_check() - Reject stream syntax for unsupported operations
+ * @vol: NTFS volume
+ * @qname: final pathname component
+ *
+ * Return: 0 for an ordinary name or when streams are disabled,
+ * -EOPNOTSUPP for parsed stream syntax, or -EINVAL for malformed syntax.
+ */
+int ntfs_stream_path_check(struct ntfs_volume *vol,
+ const struct qstr *qname)
+{
+ struct ntfs_stream_path path;
+ int ret;
+
+ ret = ntfs_stream_path_parse(vol, qname, &path);
+ if (ret < 0)
+ return ret;
+ return ret ? -EOPNOTSUPP : 0;
+}
+
+/*
+ * ntfs_stream_lookup_inode_by_name() - Look up a base inode by name
+ * @dir_ino: directory containing the base file or directory
+ * @uname: UTF-16LE base name
+ * @uname_len: Length of @uname in UTF-16 code units
+ *
+ * Resolve the directory entry and verify its MFT reference before returning
+ * the inode.
+ *
+ * Return: Referenced inode on success, or ERR_PTR() on failure.
+ */
+struct inode *ntfs_stream_lookup_inode_by_name(struct inode *dir_ino,
+ __le16 *uname, int uname_len)
+{
+ struct ntfs_volume *vol = NTFS_SB(dir_ino->i_sb);
+ struct ntfs_name *name = NULL;
+ struct inode *inode;
+ u64 mref;
+
+ mutex_lock(&NTFS_I(dir_ino)->mrec_lock);
+ mref = ntfs_lookup_inode_by_name(NTFS_I(dir_ino), uname, uname_len,
+ &name);
+ mutex_unlock(&NTFS_I(dir_ino)->mrec_lock);
+ kfree(name);
+
+ if (IS_ERR_MREF(mref))
+ return ERR_PTR(MREF_ERR(mref));
+
+ inode = ntfs_iget(vol->sb, MREF(mref));
+ if (IS_ERR(inode))
+ return inode;
+ if (MSEQNO(mref) != NTFS_I(inode)->seq_no &&
+ MREF(mref) != FILE_MFT) {
+ iput(inode);
+ return ERR_PTR(-EIO);
+ }
+ return inode;
+}
+
+/*
+ * Recheck that a stream dentry still names the same base inode and DATA
+ * attribute.
+ */
+static int ntfs_stream_d_revalidate(struct inode *dir,
+ const struct qstr *qname, struct dentry *dentry,
+ unsigned int flags)
+{
+ struct inode *inode = d_inode(dentry);
+ struct ntfs_inode *ni;
+ struct ntfs_stream_path path;
+ struct inode *base_vi;
+ __le16 *base_name = NULL, *stream_name = NULL;
+ int base_len, stream_len, err, ret = 0;
+
+ if (flags & LOOKUP_RCU)
+ return -ECHILD;
+ if (!inode)
+ return 0;
+ if (!inode->i_nlink)
+ return 0;
+ if (NVolShutdown(NTFS_SB(dir->i_sb)))
+ return 0;
+
+ ni = NTFS_I(inode);
+ if (!ntfs_inode_is_named_stream(ni))
+ return 0;
+
+ err = ntfs_stream_path_parse(NTFS_SB(dir->i_sb), qname, &path);
+ if (err <= 0)
+ return 0;
+ base_len = ntfs_nlstoucs(NTFS_SB(dir->i_sb), path.base_name,
+ path.base_len, &base_name, NTFS_MAX_NAME_LEN);
+ if (base_len < 0)
+ goto out;
+ stream_len = ntfs_nlstoucs(NTFS_SB(dir->i_sb), path.stream_name,
+ path.stream_len, &stream_name, NTFS_MAX_NAME_LEN);
+ if (stream_len < 0)
+ goto out;
+ if (ntfs_check_stream_name(stream_name, stream_len))
+ goto out;
+ if (stream_len != ni->name_len ||
+ !ntfs_names_are_equal(stream_name, stream_len, ni->name,
+ ni->name_len, IGNORE_CASE, ni->vol->upcase,
+ ni->vol->upcase_len))
+ goto out;
+
+ base_vi = ntfs_stream_lookup_inode_by_name(dir, base_name, base_len);
+ if (IS_ERR(base_vi))
+ goto out;
+ if (NTFS_I(base_vi) != ni->ext.base_ntfs_ino) {
+ iput(base_vi);
+ goto out;
+ }
+
+ mutex_lock(&NTFS_I(base_vi)->mrec_lock);
+ err = ntfs_stream_inode_validate(inode);
+ mutex_unlock(&NTFS_I(base_vi)->mrec_lock);
+ iput(base_vi);
+ if (!err)
+ ret = 1;
+out:
+ if (stream_name)
+ kmem_cache_free(ntfs_name_cache, stream_name);
+ if (base_name)
+ kmem_cache_free(ntfs_name_cache, base_name);
+ return ret;
+}
+
+/* Revalidate stream paths while leaving ordinary NTFS dentries cacheable. */
+static int ntfs_dentry_revalidate(struct inode *dir,
+ const struct qstr *qname, struct dentry *dentry,
+ unsigned int flags)
+{
+ struct inode *inode = d_inode(dentry);
+ struct ntfs_volume *vol = NTFS_SB(dir->i_sb);
+
+ if (ntfs_stream_path_has_colon(vol, qname))
+ return ntfs_stream_d_revalidate(dir, qname, dentry, flags);
+ if (inode && ntfs_inode_is_named_stream(NTFS_I(inode)))
+ return 0;
+ return 1;
+}
+
+/* Drop stream-path aliases so future lookups recheck their backing attribute. */
+static int ntfs_dentry_delete(const struct dentry *dentry)
+{
+ struct inode *inode = d_inode(dentry);
+ struct ntfs_volume *vol = NTFS_SB(dentry->d_sb);
+
+ if (ntfs_stream_path_has_colon(vol, &dentry->d_name) ||
+ (inode && ntfs_inode_is_named_stream(NTFS_I(inode))))
+ return always_delete_dentry(dentry);
+ return 0;
+}
+
+static const struct dentry_operations ntfs_dentry_ops = {
+ .d_revalidate = ntfs_dentry_revalidate,
+ .d_delete = ntfs_dentry_delete,
+};
+
+/*
+ * ntfs_set_default_dentry_ops() - Install stream-aware dentry operations
+ * @sb: superblock on which to install the operations
+ *
+ * Ensure pathname stream dentries are revalidated and not kept as stale
+ * aliases.
+ */
+void ntfs_set_default_dentry_ops(struct super_block *sb)
+{
+ set_default_d_op(sb, &ntfs_dentry_ops);
+}
+
+/*
+ * ntfs_lookup_stream() - Look up a named stream by pathname
+ * @dir_ino: directory containing the base file or directory
+ * @dent: dentry for the pathname component
+ * @path: parsed base and stream name slices
+ *
+ * Find the existing base inode and its named DATA attribute, then splice the
+ * stream inode into @dent.
+ *
+ * Return: NULL if @dent was instantiated, an alternate dentry if a
+ * disconnected alias was spliced, or ERR_PTR() on failure.
+ */
+struct dentry *ntfs_lookup_stream(struct inode *dir_ino,
+ struct dentry *dent, const struct ntfs_stream_path *path)
+{
+ struct ntfs_volume *vol = NTFS_SB(dir_ino->i_sb);
+ struct inode *base_vi, *stream_vi;
+ __le16 *base_uname = NULL, *stream_uname = NULL;
+ int base_len, stream_len, err;
+
+ if (NVolShutdown(vol))
+ return ERR_PTR(-EIO);
+
+ base_len = ntfs_nlstoucs(vol, path->base_name, path->base_len,
+ &base_uname, NTFS_MAX_NAME_LEN);
+ if (base_len < 0)
+ return ERR_PTR(base_len);
+
+ stream_len = ntfs_nlstoucs(vol, path->stream_name, path->stream_len,
+ &stream_uname, NTFS_MAX_NAME_LEN);
+ if (stream_len < 0) {
+ kmem_cache_free(ntfs_name_cache, base_uname);
+ return ERR_PTR(stream_len);
+ }
+ err = ntfs_check_stream_name(stream_uname, stream_len);
+ if (err) {
+ kmem_cache_free(ntfs_name_cache, base_uname);
+ kmem_cache_free(ntfs_name_cache, stream_uname);
+ return ERR_PTR(err);
+ }
+
+ base_vi = ntfs_stream_lookup_inode_by_name(dir_ino, base_uname,
+ base_len);
+ kmem_cache_free(ntfs_name_cache, base_uname);
+ if (IS_ERR(base_vi)) {
+ err = PTR_ERR(base_vi);
+ kmem_cache_free(ntfs_name_cache, stream_uname);
+ if (err == -ENOENT)
+ return d_splice_alias(NULL, dent);
+ return ERR_PTR(err);
+ }
+
+ if (!S_ISREG(base_vi->i_mode) && !S_ISDIR(base_vi->i_mode)) {
+ iput(base_vi);
+ kmem_cache_free(ntfs_name_cache, stream_uname);
+ return ERR_PTR(-ENOTDIR);
+ }
+
+ mutex_lock(&NTFS_I(base_vi)->mrec_lock);
+ stream_vi = ntfs_attr_iget(base_vi, AT_DATA, stream_uname, stream_len);
+ if (!IS_ERR(stream_vi)) {
+ if (NInoStreamUnlinked(NTFS_I(stream_vi)))
+ err = -ENOENT;
+ else
+ err = ntfs_stream_inode_validate(stream_vi);
+ }
+ mutex_unlock(&NTFS_I(base_vi)->mrec_lock);
+ if (!IS_ERR(stream_vi) && err) {
+ iput(stream_vi);
+ stream_vi = ERR_PTR(err);
+ }
+ iput(base_vi);
+ kmem_cache_free(ntfs_name_cache, stream_uname);
+
+ if (IS_ERR(stream_vi)) {
+ err = PTR_ERR(stream_vi);
+ if (err == -ENOENT || err == -ESTALE)
+ return d_splice_alias(NULL, dent);
+ return ERR_PTR(err);
+ }
+
+ return d_splice_alias(stream_vi, dent);
+}
+
+/*
+ * ntfs_stream_path_names() - Convert and validate pathname stream names
+ * @vol: NTFS volume
+ * @qname: final pathname component
+ * @base_name: receives the allocated UTF-16LE base name
+ * @base_len: receives the base name length in UTF-16 code units
+ * @stream_name: receives the allocated UTF-16LE stream name
+ * @stream_len: receives the stream name length in UTF-16 code units
+ *
+ * Return: 1 if stream names were produced, 0 if @qname has no stream syntax,
+ * or a negative errno. The caller owns both name buffers on success.
+ */
+int ntfs_stream_path_names(struct ntfs_volume *vol,
+ const struct qstr *qname, __le16 **base_name, int *base_len,
+ __le16 **stream_name, int *stream_len)
+{
+ struct ntfs_stream_path path;
+ int err;
+
+ *base_name = NULL;
+ *stream_name = NULL;
+ err = ntfs_stream_path_parse(vol, qname, &path);
+ if (err <= 0)
+ return err;
+
+ *base_len = ntfs_nlstoucs(vol, path.base_name, path.base_len,
+ base_name, NTFS_MAX_NAME_LEN);
+ if (*base_len < 0)
+ return *base_len;
+
+ *stream_len = ntfs_nlstoucs(vol, path.stream_name, path.stream_len,
+ stream_name, NTFS_MAX_NAME_LEN);
+ if (*stream_len < 0) {
+ kmem_cache_free(ntfs_name_cache, *base_name);
+ *base_name = NULL;
+ return *stream_len;
+ }
+
+ err = ntfs_check_bad_windows_name(vol, *base_name, *base_len);
+ if (err)
+ goto out_free;
+ err = ntfs_check_stream_name(*stream_name, *stream_len);
+ if (err)
+ goto out_free;
+ err = ntfs_check_bad_windows_name(vol, *stream_name, *stream_len);
+ if (err)
+ goto out_free;
+ return 1;
+
+out_free:
+ kmem_cache_free(ntfs_name_cache, *stream_name);
+ kmem_cache_free(ntfs_name_cache, *base_name);
+ *stream_name = NULL;
+ *base_name = NULL;
+ return err;
+}
+
+/*
+ * ntfs_create_named_stream() - Create a named DATA stream
+ * @idmap: mount idmap used for permission checks
+ * @dir: directory containing the base object
+ * @base_name: UTF-16LE base name
+ * @base_len: length of @base_name in UTF-16 code units
+ * @stream_name: UTF-16LE stream name
+ * @stream_len: length of @stream_name in UTF-16 code units
+ *
+ * Create the stream on an existing regular file or directory.
+ *
+ * Return: Referenced stream inode on success, or ERR_PTR() on failure.
+ */
+struct inode *ntfs_create_named_stream(struct mnt_idmap *idmap,
+ struct inode *dir, __le16 *base_name, int base_len,
+ __le16 *stream_name, int stream_len)
+{
+ struct ntfs_inode *base_ni;
+ struct inode *base_vi, *stream_vi;
+ struct inode *rollback_vi = NULL;
+ struct ntfs_attr_search_ctx *ctx;
+ bool stream_created = false;
+ int err, rollback_err;
+
+ base_vi = ntfs_stream_lookup_inode_by_name(dir, base_name, base_len);
+ if (IS_ERR(base_vi))
+ return base_vi;
+ if (!S_ISREG(base_vi->i_mode) && !S_ISDIR(base_vi->i_mode)) {
+ iput(base_vi);
+ return ERR_PTR(-ENOTDIR);
+ }
+ err = inode_permission(idmap, base_vi, MAY_OPEN | MAY_WRITE);
+ if (err)
+ goto out_iput;
+ if (IS_APPEND(base_vi) || IS_IMMUTABLE(base_vi)) {
+ err = -EPERM;
+ goto out_iput;
+ }
+ if (!(NTFS_SB(base_vi->i_sb)->vol_flags & VOLUME_IS_DIRTY)) {
+ err = ntfs_set_volume_flags(NTFS_SB(base_vi->i_sb),
+ VOLUME_IS_DIRTY);
+ if (err)
+ goto out_iput;
+ }
+
+ base_ni = NTFS_I(base_vi);
+ mutex_lock(&base_ni->mrec_lock);
+ if (NVolShutdown(base_ni->vol)) {
+ err = -EIO;
+ goto out_unlock;
+ }
+ if (NInoBeingDeleted(base_ni) || !base_vi->i_nlink) {
+ err = -ENOENT;
+ goto out_unlock;
+ }
+ if (IS_APPEND(base_vi) || IS_IMMUTABLE(base_vi)) {
+ err = -EPERM;
+ goto out_unlock;
+ }
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
+ if (!ctx) {
+ err = -ENOMEM;
+ goto out_unlock;
+ }
+
+ err = ntfs_attr_lookup(AT_DATA, stream_name, stream_len,
+ IGNORE_CASE, 0, NULL, 0, ctx);
+ if (!err) {
+ if (ntfs_stream_unlinked(base_ni, stream_name, stream_len))
+ err = -EBUSY;
+ else
+ err = -EEXIST;
+ } else if (err == -ENOENT) {
+ if (NVolShutdown(base_ni->vol)) {
+ err = -EIO;
+ goto out_put_ctx;
+ }
+ err = ntfs_attr_add(base_ni, AT_DATA, stream_name, stream_len,
+ NULL, 0);
+ if (!err) {
+ stream_created = true;
+ mark_mft_record_dirty(base_ni);
+ }
+ }
+ ntfs_attr_put_search_ctx(ctx);
+ if (err)
+ goto out_unlock;
+
+ stream_vi = ntfs_attr_iget(base_vi, AT_DATA, stream_name, stream_len);
+ if (!IS_ERR(stream_vi))
+ err = ntfs_stream_inode_validate(stream_vi);
+ else
+ err = PTR_ERR(stream_vi);
+ if (err) {
+ if (stream_created) {
+ rollback_err = ntfs_attr_remove(base_ni, AT_DATA,
+ stream_name, stream_len, &rollback_vi);
+ if (rollback_err) {
+ ntfs_error(base_ni->vol->sb,
+ "Failed to roll back named stream creation.\n");
+ if (rollback_err == -ENOMEM ||
+ rollback_err == -EINTR ||
+ rollback_err == -ERESTARTSYS) {
+ err = rollback_err;
+ } else {
+ NVolSetErrors(base_ni->vol);
+ NVolSetShutdown(base_ni->vol);
+ err = -EIO;
+ }
+ }
+ }
+ mutex_unlock(&base_ni->mrec_lock);
+ if (stream_created)
+ ntfs_stream_update_base_time(base_ni);
+ if (!IS_ERR(stream_vi))
+ iput(stream_vi);
+ if (rollback_vi)
+ iput(rollback_vi);
+ iput(base_vi);
+ return ERR_PTR(err);
+ }
+ mutex_unlock(&base_ni->mrec_lock);
+ if (stream_created)
+ ntfs_stream_update_base_time(base_ni);
+ iput(base_vi);
+ return stream_vi;
+
+out_put_ctx:
+ ntfs_attr_put_search_ctx(ctx);
+out_unlock:
+ mutex_unlock(&base_ni->mrec_lock);
+out_iput:
+ iput(base_vi);
+ return ERR_PTR(err);
+}
+
+/*
+ * ntfs_unlink_named_stream() - Remove a pathname named stream
+ * @dir: directory containing the base object
+ * @dentry: dentry for the named stream
+ *
+ * Return: 0 on success, or a negative errno.
+ */
+int ntfs_unlink_named_stream(struct inode *dir, struct dentry *dentry)
+{
+ struct ntfs_volume *vol = NTFS_SB(dir->i_sb);
+ struct inode *base_vi;
+ __le16 *base_name = NULL, *stream_name = NULL;
+ int base_len, stream_len, path_result, err;
+
+ path_result = ntfs_stream_path_names(vol, &dentry->d_name, &base_name,
+ &base_len, &stream_name, &stream_len);
+ if (path_result < 0)
+ return path_result;
+ if (!path_result)
+ return -EINVAL;
+
+ base_vi = ntfs_stream_lookup_inode_by_name(dir, base_name, base_len);
+ if (IS_ERR(base_vi)) {
+ err = PTR_ERR(base_vi);
+ goto out_free;
+ }
+ if (!S_ISREG(base_vi->i_mode) && !S_ISDIR(base_vi->i_mode)) {
+ err = -ENOTDIR;
+ goto out_iput;
+ }
+ if (!ntfs_inode_is_named_stream(NTFS_I(dentry->d_inode))) {
+ err = -ESTALE;
+ goto out_iput;
+ }
+ if (NTFS_I(dentry->d_inode)->ext.base_ntfs_ino != NTFS_I(base_vi)) {
+ err = -ESTALE;
+ goto out_iput;
+ }
+
+ /*
+ * VFS authorizes pathname removal via the parent directory, not base
+ * file write access. The shared helper enforces NTFS delete restrictions.
+ */
+ err = ntfs_remove_named_stream(NTFS_I(base_vi), stream_name,
+ stream_len, dentry->d_inode);
+
+out_iput:
+ iput(base_vi);
+out_free:
+ kmem_cache_free(ntfs_name_cache, stream_name);
+ kmem_cache_free(ntfs_name_cache, base_name);
+ return err;
+}
+
+/* Obtain a dentry for the base inode behind a stream inode. */
+static struct dentry *ntfs_stream_base_dentry(struct inode *inode)
+{
+ struct inode *base_vi =
+ VFS_I(NTFS_I(inode)->ext.base_ntfs_ino);
+ struct dentry *dentry;
+
+ dentry = d_find_alias(base_vi);
+ if (dentry)
+ return dentry;
+ if (!igrab(base_vi))
+ return ERR_PTR(-ESTALE);
+ return d_obtain_alias(base_vi);
+}
+
+/*
+ * ntfs_stream_validate_for_mutation() - Validate a stream before mutation
+ * @inode: stream inode to validate
+ * @nowait: do not wait for the base MFT-record lock
+ *
+ * Return: 0 if the backing DATA attribute is valid, -EAGAIN if a nonblocking
+ * lock attempt fails, or another negative errno.
+ */
+int ntfs_stream_validate_for_mutation(struct inode *inode, bool nowait)
+{
+ struct ntfs_inode *base_ni = NTFS_I(inode)->ext.base_ntfs_ino;
+ int err;
+
+ if (nowait) {
+ if (!mutex_trylock(&base_ni->mrec_lock))
+ return -EAGAIN;
+ } else {
+ mutex_lock(&base_ni->mrec_lock);
+ }
+ err = ntfs_stream_inode_validate(inode);
+ mutex_unlock(&base_ni->mrec_lock);
+ return err;
+}
+
+/*
+ * Hold a temporary stream reference so unlink cannot remove its attribute
+ * while an operation is using it.
+ */
+static int ntfs_stream_claim(struct inode *inode)
+{
+ struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_inode *base_ni = ni->ext.base_ntfs_ino;
+ int err;
+
+ mutex_lock(&base_ni->mrec_lock);
+ if (NInoStreamUnlinked(ni))
+ err = -ESTALE;
+ else
+ err = ntfs_stream_inode_validate(inode);
+ if (!err) {
+ if (atomic_read(&ni->stream_open_count) < 0)
+ err = -ESTALE;
+ else
+ atomic_inc(&ni->stream_open_count);
+ }
+ mutex_unlock(&base_ni->mrec_lock);
+ return err;
+}
+
+/* Stream permission checks use the base inode's permissions. */
+static int ntfs_stream_permission(struct mnt_idmap *idmap,
+ struct inode *inode, int mask)
+{
+ return inode_permission(idmap,
+ VFS_I(NTFS_I(inode)->ext.base_ntfs_ino), mask);
+}
+
+/* Report base metadata with the stream's own size and allocation. */
+static int ntfs_stream_getattr(struct mnt_idmap *idmap,
+ const struct path *path, struct kstat *stat,
+ unsigned int request_mask, unsigned int query_flags)
+{
+ struct inode *inode = d_backing_inode(path->dentry);
+ struct ntfs_inode *ni = NTFS_I(inode);
+ struct inode *base_vi = VFS_I(ni->ext.base_ntfs_ino);
+
+ generic_fillattr(idmap, request_mask, base_vi, stat);
+ stat->size = i_size_read(inode);
+ stat->blocks = (((u64)ni->i_dealloc_clusters <<
+ NTFS_SB(inode->i_sb)->cluster_size_bits) >> 9) +
+ inode->i_blocks;
+ stat->blksize = NTFS_SB(inode->i_sb)->cluster_size;
+ stat->result_mask |= STATX_BTIME;
+ stat->btime = NTFS_I(base_vi)->i_crtime;
+
+ if (NInoCompressed(ni))
+ stat->attributes |= STATX_ATTR_COMPRESSED;
+ if (NInoEncrypted(ni))
+ stat->attributes |= STATX_ATTR_ENCRYPTED;
+ if (base_vi->i_flags & S_IMMUTABLE)
+ stat->attributes |= STATX_ATTR_IMMUTABLE;
+ if (base_vi->i_flags & S_APPEND)
+ stat->attributes |= STATX_ATTR_APPEND;
+ stat->attributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED |
+ STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND;
+ stat->mode = (stat->mode & ~S_IFMT) | S_IFREG;
+
+ if (request_mask & STATX_DIOALIGN) {
+ unsigned int align =
+ bdev_logical_block_size(inode->i_sb->s_bdev);
+
+ stat->result_mask |= STATX_DIOALIGN;
+ if (!NInoCompressed(ni) && !NInoEncrypted(ni)) {
+ stat->dio_mem_align = align;
+ stat->dio_offset_align = align;
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * Apply size changes to the stream and route shared inode metadata changes to
+ * its base inode.
+ */
+static int ntfs_stream_setattr_common(struct mnt_idmap *idmap,
+ struct inode *inode, struct iattr *attr)
+{
+ struct ntfs_inode *ni = NTFS_I(inode);
+ struct inode *base_vi = VFS_I(ni->ext.base_ntfs_ino);
+ struct dentry *base_dentry = NULL;
+ struct iattr base_attr = *attr;
+ int err;
+
+ base_attr.ia_valid &= ~ATTR_FILE;
+ base_attr.ia_file = NULL;
+ if (base_attr.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
+ base_attr.ia_valid &= ~ATTR_MODE;
+ else if (base_attr.ia_valid & ATTR_MODE)
+ base_attr.ia_mode = (base_attr.ia_mode & ~S_IFMT) |
+ (base_vi->i_mode & S_IFMT);
+
+ if (attr->ia_valid & ATTR_SIZE) {
+ err = inode_permission(idmap, base_vi, MAY_WRITE);
+ if (err)
+ goto out;
+ if (IS_APPEND(base_vi) || IS_IMMUTABLE(base_vi)) {
+ err = -EPERM;
+ goto out;
+ }
+ if (!(ni->vol->vol_flags & VOLUME_IS_DIRTY)) {
+ err = ntfs_set_volume_flags(ni->vol, VOLUME_IS_DIRTY);
+ if (err)
+ goto out;
+ }
+ base_attr.ia_valid &= ~ATTR_SIZE;
+ }
+
+ if ((attr->ia_valid & ATTR_SIZE) || base_attr.ia_valid) {
+ base_dentry = ntfs_stream_base_dentry(inode);
+ if (IS_ERR(base_dentry)) {
+ err = PTR_ERR(base_dentry);
+ base_dentry = NULL;
+ goto out;
+ }
+ inode_lock_nested(base_vi, I_MUTEX_PARENT);
+ err = ntfs_stream_validate_for_mutation(inode, false);
+ if (err) {
+ inode_unlock(base_vi);
+ goto out;
+ }
+ if (base_attr.ia_valid)
+ err = notify_change(idmap, base_dentry, &base_attr,
+ NULL);
+ else
+ err = 0;
+ if (!err)
+ ntfs_stream_inode_refresh(inode);
+ inode_unlock(base_vi);
+ if (err)
+ goto out;
+ }
+
+ if (attr->ia_valid & ATTR_SIZE)
+ err = ntfs_setattr_size(inode, attr);
+ else
+ err = 0;
+out:
+ dput(base_dentry);
+ return err;
+}
+
+/* Hold the stream against unlink while applying VFS setattr operations. */
+static int ntfs_stream_setattr(struct mnt_idmap *idmap,
+ struct dentry *dentry, struct iattr *attr)
+{
+ struct inode *inode = d_inode(dentry);
+ struct ntfs_inode *ni = NTFS_I(inode);
+ bool claimed = false;
+ int err;
+
+ if (NVolShutdown(ni->vol))
+ return -EIO;
+
+ if (!(attr->ia_valid & ATTR_FILE)) {
+ err = ntfs_stream_claim(inode);
+ if (err)
+ return err;
+ claimed = true;
+ }
+
+ err = ntfs_stream_setattr_common(idmap, inode, attr);
+ if (claimed)
+ ntfs_stream_put(inode);
+ return err;
+}
+
+/*
+ * Apply stream timestamp updates to the base inode after validating the
+ * backing attribute under its MFT-record lock. Nonblocking callers get
+ * -EAGAIN.
+ */
+static int ntfs_stream_update_time_common(struct inode *inode,
+ enum fs_update_time type, unsigned int flags)
+{
+ struct ntfs_inode *base_ni = NTFS_I(inode)->ext.base_ntfs_ino;
+ struct inode *base_vi = VFS_I(base_ni);
+ int err;
+
+ if (NVolShutdown(base_ni->vol))
+ return -EIO;
+ if (flags & IOCB_NOWAIT)
+ return -EAGAIN;
+
+ mutex_lock(&base_ni->mrec_lock);
+ if (NVolShutdown(base_ni->vol)) {
+ err = -EIO;
+ goto out_mrec;
+ }
+ err = ntfs_stream_inode_validate(inode);
+ if (!err) {
+ err = generic_update_time(base_vi, type, flags);
+ if (!err)
+ ntfs_stream_inode_refresh(inode);
+ }
+out_mrec:
+ mutex_unlock(&base_ni->mrec_lock);
+ return err;
+}
+
+static int ntfs_stream_update_time(struct inode *inode,
+ enum fs_update_time type, unsigned int flags)
+{
+ return ntfs_stream_update_time_common(inode, type, flags);
+}
+
+static ssize_t ntfs_stream_listxattr(struct dentry *dentry, char *buffer,
+ size_t size)
+{
+ return -EOPNOTSUPP;
+}
+
+#ifdef CONFIG_NTFS_FS_POSIX_ACL
+static struct posix_acl *ntfs_stream_get_acl(struct mnt_idmap *idmap,
+ struct dentry *dentry, int type)
+{
+ return ERR_PTR(-EOPNOTSUPP);
+}
+
+static int ntfs_stream_set_acl(struct mnt_idmap *idmap,
+ struct dentry *dentry, struct posix_acl *acl, int type)
+{
+ return -EOPNOTSUPP;
+}
+#endif
+
+/*
+ * Remove an unlinked stream's DATA attribute after its final active reference
+ * is released. Base-inode deletion handles the attribute when the base is
+ * already being removed.
+ */
+static int ntfs_stream_finish_remove(struct inode *attr_vi)
+{
+ struct ntfs_inode *attr_ni = NTFS_I(attr_vi);
+ struct ntfs_inode *ni = attr_ni->ext.base_ntfs_ino;
+ int err;
+
+ mutex_lock(&ni->mrec_lock);
+ if (!NInoStreamUnlinked(attr_ni) ||
+ atomic_cmpxchg(&attr_ni->stream_open_count, 0, -1)) {
+ err = 0;
+ goto out_unlock;
+ }
+ if (NInoBeingDeleted(ni) || !VFS_I(ni)->i_nlink) {
+ remove_inode_hash(attr_vi);
+ err = 0;
+ goto out_unlock;
+ }
+ mutex_unlock(&ni->mrec_lock);
+
+ truncate_inode_pages(attr_vi->i_mapping, 0);
+
+ mutex_lock(&ni->mrec_lock);
+ if (NVolShutdown(ni->vol)) {
+ err = -EIO;
+ goto out_unlock;
+ }
+ if (NInoBeingDeleted(ni) || !VFS_I(ni)->i_nlink) {
+ remove_inode_hash(attr_vi);
+ err = 0;
+ goto out_unlock;
+ }
+ err = ntfs_attr_rm(attr_ni);
+ if (!err) {
+ NInoClearDirty(attr_ni);
+ remove_inode_hash(attr_vi);
+ } else {
+ NInoSetBeingDeleted(attr_ni);
+ remove_inode_hash(attr_vi);
+ }
+
+out_unlock:
+ mutex_unlock(&ni->mrec_lock);
+ return err;
+}
+
+/*
+ * ntfs_stream_put() - Release a stream operation reference
+ * @vi: stream inode whose reference is being released
+ *
+ * Finish a deferred unlink when this is the last reference to an unlinked
+ * stream.
+ *
+ * Return: 0 on success, or a negative errno if deferred removal fails.
+ */
+int ntfs_stream_put(struct inode *vi)
+{
+ struct ntfs_inode *ni = NTFS_I(vi);
+
+ if (atomic_dec_and_test(&ni->stream_open_count) &&
+ NInoStreamUnlinked(ni))
+ return ntfs_stream_finish_remove(vi);
+ return 0;
+}
+
+/*
+ * ntfs_remove_named_stream() - Remove a named DATA stream
+ * @ni: base inode containing the stream
+ * @uname: UTF-16LE stream name
+ * @uname_len: length of @uname in UTF-16 code units
+ * @expected_vi: expected cached stream inode, or NULL
+ *
+ * Refuse removal while the stream is open. If @expected_vi is non-NULL, it
+ * must be the inode currently associated with the named attribute.
+ *
+ * Return: 0 on success, -ENOENT if the stream is absent, or another negative
+ * errno.
+ */
+int ntfs_remove_named_stream(struct ntfs_inode *ni, __le16 *uname,
+ u32 uname_len, struct inode *expected_vi)
+{
+ struct inode *attr_vi;
+ struct ntfs_inode *attr_ni;
+ int err;
+
+ if (!ni || !uname || uname_len == 0)
+ return -EINVAL;
+ if (NVolShutdown(ni->vol))
+ return -EIO;
+ if ((ni->flags & FILE_ATTR_READONLY) ||
+ IS_APPEND(VFS_I(ni)) || IS_IMMUTABLE(VFS_I(ni)))
+ return -EPERM;
+ if (!(ni->vol->vol_flags & VOLUME_IS_DIRTY)) {
+ err = ntfs_set_volume_flags(ni->vol, VOLUME_IS_DIRTY);
+ if (err)
+ return err;
+ }
+
+ mutex_lock(&ni->mrec_lock);
+ if (NInoBeingDeleted(ni) || !VFS_I(ni)->i_nlink) {
+ err = -ENOENT;
+ goto out_unlock;
+ }
+ attr_vi = ntfs_attr_iget(VFS_I(ni), AT_DATA, uname, uname_len);
+ if (IS_ERR(attr_vi)) {
+ err = PTR_ERR(attr_vi);
+ goto out_unlock;
+ }
+ if (expected_vi && attr_vi != expected_vi) {
+ err = -ESTALE;
+ goto out_iput;
+ }
+
+ attr_ni = NTFS_I(attr_vi);
+ if (NInoStreamUnlinked(attr_ni)) {
+ err = -ENOENT;
+ goto out_iput;
+ }
+ if (atomic_read(&attr_ni->stream_open_count) > 0) {
+ err = -EBUSY;
+ goto out_iput;
+ }
+ err = ntfs_stream_inode_validate(attr_vi);
+ if (err)
+ goto out_iput;
+
+ NInoSetStreamUnlinked(attr_ni);
+ clear_nlink(attr_vi);
+ mutex_unlock(&ni->mrec_lock);
+
+ err = ntfs_stream_finish_remove(attr_vi);
+ if (!err)
+ ntfs_stream_update_base_time(ni);
+ iput(attr_vi);
+ return err;
+
+out_iput:
+ mutex_unlock(&ni->mrec_lock);
+ iput(attr_vi);
+ return err;
+out_unlock:
+ mutex_unlock(&ni->mrec_lock);
+ return err;
+}
+
+/*
+ * Validate access against the base inode and hold the stream against unlink
+ * until the file is released.
+ */
+static int ntfs_stream_file_open(struct inode *inode, struct file *file)
+{
+ struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_inode *base_ni = ni->ext.base_ntfs_ino;
+ struct inode *base_vi = VFS_I(base_ni);
+ int mask = MAY_OPEN;
+ int err;
+
+ if (file->f_mode & FMODE_READ)
+ mask |= MAY_READ;
+ if (file->f_mode & FMODE_WRITE)
+ mask |= MAY_WRITE;
+ err = inode_permission(file_mnt_idmap(file), base_vi, mask);
+ if (err)
+ return err;
+
+ mutex_lock(&base_ni->mrec_lock);
+ if (NInoStreamUnlinked(ni))
+ err = -ENOENT;
+ else
+ err = ntfs_stream_inode_validate(inode);
+ if (err)
+ goto out_unlock;
+ if ((file->f_mode & FMODE_WRITE) &&
+ (IS_IMMUTABLE(base_vi) ||
+ (IS_APPEND(base_vi) && !(file->f_flags & O_APPEND)))) {
+ err = -EPERM;
+ goto out_unlock;
+ }
+ if ((file->f_flags & O_TRUNC) && IS_APPEND(base_vi)) {
+ err = -EPERM;
+ goto out_unlock;
+ }
+ if ((file->f_flags & O_NOATIME) &&
+ !inode_owner_or_capable(file_mnt_idmap(file), base_vi)) {
+ err = -EPERM;
+ goto out_unlock;
+ }
+ err = ntfs_file_open(inode, file);
+ if (err)
+ goto out_unlock;
+ if (file->f_mode & FMODE_WRITE) {
+ err = get_write_access(base_vi);
+ if (err)
+ goto out_unlock;
+ file->private_data = base_vi;
+ }
+
+ atomic_inc(&ni->stream_open_count);
+out_unlock:
+ mutex_unlock(&base_ni->mrec_lock);
+ return err;
+}
+
+/* Drop open-time references and complete any deferred stream unlink. */
+static int ntfs_stream_file_release(struct inode *inode, struct file *file)
+{
+ int err, remove_err;
+
+ err = ntfs_file_release(inode, file);
+ if (file->private_data) {
+ put_write_access(file->private_data);
+ file->private_data = NULL;
+ }
+ remove_err = ntfs_stream_put(inode);
+ if (!err)
+ err = remove_err;
+ return err;
+}
+
+enum ntfs_stream_ioctl_op {
+ NTFS_STREAM_IOCTL_READ,
+ NTFS_STREAM_IOCTL_WRITE,
+ NTFS_STREAM_IOCTL_REMOVE,
+};
+
+/*
+ * Look up or create an ioctl target stream and return its referenced inode.
+ */
+static int ntfs_stream_ioctl_get_inode(struct inode *base_vi, __le16 *sname,
+ int sname_len, bool create, struct inode **stream_vi)
+{
+ struct ntfs_inode *base_ni = NTFS_I(base_vi);
+ struct ntfs_attr_search_ctx *ctx;
+ struct inode *attr_vi = NULL;
+ bool created = false;
+ int err;
+
+ *stream_vi = NULL;
+
+ mutex_lock(&base_ni->mrec_lock);
+ if (NVolShutdown(base_ni->vol)) {
+ err = -EIO;
+ goto out_unlock;
+ }
+ if (NInoBeingDeleted(base_ni) || !base_vi->i_nlink) {
+ err = -ENOENT;
+ goto out_unlock;
+ }
+
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
+ if (!ctx) {
+ err = -ENOMEM;
+ goto out_unlock;
+ }
+
+ err = ntfs_attr_lookup(AT_DATA, sname, sname_len, IGNORE_CASE,
+ 0, NULL, 0, ctx);
+ if (err == -ENOENT && create) {
+ err = ntfs_attr_add(base_ni, AT_DATA, sname, sname_len,
+ NULL, 0);
+ if (!err) {
+ created = true;
+ mark_mft_record_dirty(base_ni);
+ }
+ }
+ ntfs_attr_put_search_ctx(ctx);
+ if (err)
+ goto out_unlock;
+
+ attr_vi = ntfs_attr_iget(base_vi, AT_DATA, sname, sname_len);
+ if (IS_ERR(attr_vi)) {
+ err = PTR_ERR(attr_vi);
+ attr_vi = NULL;
+ goto out_unlock;
+ }
+ if (NInoStreamUnlinked(NTFS_I(attr_vi)))
+ err = create ? -EBUSY : -ENOENT;
+ else
+ err = ntfs_stream_inode_validate(attr_vi);
+out_unlock:
+ mutex_unlock(&base_ni->mrec_lock);
+ if (created)
+ ntfs_stream_update_base_time(base_ni);
+ if (err) {
+ iput(attr_vi);
+ return err;
+ }
+
+ *stream_vi = attr_vi;
+ return 0;
+}
+
+/* Check file mode, base-inode flags, and the requested transfer range. */
+static int ntfs_stream_ioctl_access(struct file *filp,
+ enum ntfs_stream_ioctl_op op, loff_t pos, size_t len)
+{
+ struct inode *inode = file_inode(filp);
+ bool is_dir = S_ISDIR(inode->i_mode);
+ int err;
+
+ if (op == NTFS_STREAM_IOCTL_READ) {
+ if (!(filp->f_mode & FMODE_READ))
+ return -EBADF;
+ return rw_verify_area(READ, filp, &pos, len);
+ }
+
+ if (!(filp->f_mode & FMODE_WRITE) && !is_dir)
+ return -EBADF;
+ if (is_dir) {
+ err = inode_permission(file_mnt_idmap(filp), inode,
+ MAY_WRITE | MAY_EXEC);
+ if (err)
+ return err;
+ }
+ if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
+ return -EPERM;
+ if (op == NTFS_STREAM_IOCTL_REMOVE)
+ return 0;
+ return rw_verify_area(WRITE, filp, &pos, len);
+}
+
+/*
+ * Validate and execute a named-stream read, write, or remove ioctl request.
+ */
+static long ntfs_ioctl_stream(struct file *filp, unsigned long arg,
+ enum ntfs_stream_ioctl_op op)
+{
+ struct inode *base_vi = file_inode(filp);
+ struct ntfs_inode *base_ni = NTFS_I(base_vi);
+ struct ntfs_stream hdr;
+ struct ntfs_stream *req;
+ struct inode *stream_vi = NULL;
+ __le16 *uname = NULL, *sname;
+ size_t data_size, total_size;
+ loff_t pos;
+ s64 ret;
+ int sname_len, err;
+ bool claimed = false, got_write = false, utf16;
+
+ if (NVolShutdown(base_ni->vol))
+ return -EIO;
+ if (NInoAttr(base_ni) ||
+ (!S_ISREG(base_vi->i_mode) && !S_ISDIR(base_vi->i_mode)))
+ return -EOPNOTSUPP;
+ if (copy_from_user(&hdr, (void __user *)arg, sizeof(hdr)))
+ return -EFAULT;
+
+ if (hdr.io_len > NTFS_STREAM_MAX_IO ||
+ (hdr.flags & ~NTFS_STREAM_FL_UTF16) || hdr.reserved)
+ return -EINVAL;
+ if (!hdr.name_len)
+ return -EINVAL;
+
+ utf16 = hdr.flags & NTFS_STREAM_FL_UTF16;
+ if (utf16) {
+ if ((hdr.name_len & 1) ||
+ hdr.name_len > NTFS_MAX_NAME_LEN * sizeof(__le16))
+ return -EINVAL;
+ } else if (hdr.name_len >
+ NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE) {
+ return -EINVAL;
+ }
+
+ switch (op) {
+ case NTFS_STREAM_IOCTL_READ:
+ case NTFS_STREAM_IOCTL_WRITE:
+ if (!hdr.io_len)
+ return -EINVAL;
+ if (hdr.stream_offset > S64_MAX ||
+ hdr.stream_offset > S64_MAX - hdr.io_len)
+ return -EOVERFLOW;
+ data_size = hdr.io_len;
+ break;
+ case NTFS_STREAM_IOCTL_REMOVE:
+ if (hdr.io_len || hdr.stream_offset)
+ return -EINVAL;
+ data_size = 0;
+ break;
+ default:
+ return -ENOTTY;
+ }
+
+ if (check_add_overflow(sizeof(hdr), (size_t)hdr.name_len,
+ &total_size) ||
+ check_add_overflow(total_size, data_size, &total_size))
+ return -EOVERFLOW;
+
+ if (op == NTFS_STREAM_IOCTL_READ) {
+ req = kvmalloc(total_size, GFP_KERNEL);
+ if (!req)
+ return -ENOMEM;
+ if (copy_from_user(req, (void __user *)arg,
+ sizeof(hdr) + hdr.name_len)) {
+ kvfree(req);
+ return -EFAULT;
+ }
+ } else {
+ req = vmemdup_user((void __user *)arg, total_size);
+ if (IS_ERR(req))
+ return PTR_ERR(req);
+ }
+
+ req->bytes_returned = 0;
+ if (req->stream_offset != hdr.stream_offset ||
+ req->io_len != hdr.io_len || req->name_len != hdr.name_len ||
+ req->flags != hdr.flags || req->reserved) {
+ err = -EINVAL;
+ goto out_free;
+ }
+
+ if (utf16) {
+ sname = (__le16 *)req->buffer;
+ sname_len = req->name_len / sizeof(__le16);
+ } else {
+ if (memchr(req->buffer, '\0', req->name_len)) {
+ err = -EINVAL;
+ goto out_free;
+ }
+ sname_len = ntfs_nlstoucs(base_ni->vol, req->buffer,
+ req->name_len, &uname, NTFS_MAX_NAME_LEN);
+ if (sname_len < 0) {
+ err = sname_len;
+ goto out_free;
+ }
+ sname = uname;
+ }
+ err = ntfs_check_stream_name(sname, sname_len);
+ if (err)
+ goto out_free;
+
+ pos = hdr.stream_offset;
+ err = ntfs_stream_ioctl_access(filp, op, pos, data_size);
+ if (err)
+ goto out_free;
+
+ if (op == NTFS_STREAM_IOCTL_REMOVE) {
+ err = mnt_want_write_file(filp);
+ if (err)
+ goto out_free;
+ err = ntfs_remove_named_stream(base_ni, sname, sname_len, NULL);
+ mnt_drop_write_file(filp);
+ goto out_free;
+ }
+
+ if (op == NTFS_STREAM_IOCTL_WRITE) {
+ err = mnt_want_write_file(filp);
+ if (err)
+ goto out_free;
+ got_write = true;
+ inode_lock(base_vi);
+ err = file_remove_privs(filp);
+ inode_unlock(base_vi);
+ if (err)
+ goto out_drop_write;
+ if (!(base_ni->vol->vol_flags & VOLUME_IS_DIRTY)) {
+ err = ntfs_set_volume_flags(base_ni->vol,
+ VOLUME_IS_DIRTY);
+ if (err)
+ goto out_drop_write;
+ }
+ }
+
+ err = ntfs_stream_ioctl_get_inode(base_vi, sname, sname_len,
+ op == NTFS_STREAM_IOCTL_WRITE, &stream_vi);
+ if (err)
+ goto out_drop_write;
+
+ err = ntfs_stream_claim(stream_vi);
+ if (err)
+ goto out_drop_write;
+ claimed = true;
+
+ if (op == NTFS_STREAM_IOCTL_READ) {
+ inode_lock_shared(stream_vi);
+ if (pos < i_size_read(stream_vi))
+ ret = ntfs_inode_attr_pread(stream_vi, pos, data_size,
+ req->buffer + req->name_len);
+ else
+ ret = 0;
+ inode_unlock_shared(stream_vi);
+ if (ret < 0)
+ err = ret;
+ else
+ req->bytes_returned = ret;
+ if (!err)
+ file_accessed(filp);
+ } else {
+ inode_lock(stream_vi);
+ inode_dio_wait(stream_vi);
+ /* Exclude faults before taking MFT record and folio locks. */
+ filemap_invalidate_lock(stream_vi->i_mapping);
+ err = inode_newsize_ok(stream_vi, pos + data_size);
+ if (!err) {
+ ret = ntfs_inode_attr_pwrite(stream_vi, pos, data_size,
+ req->buffer + req->name_len, false);
+ if (ret < 0)
+ err = ret;
+ else
+ req->bytes_returned = ret;
+ }
+ filemap_invalidate_unlock(stream_vi->i_mapping);
+ inode_unlock(stream_vi);
+ }
+ if (claimed) {
+ int put_err;
+
+ put_err = ntfs_stream_put(stream_vi);
+ claimed = false;
+ if (!err && put_err)
+ err = put_err;
+ }
+ iput(stream_vi);
+ stream_vi = NULL;
+ if (err)
+ goto out_drop_write;
+
+ if (op == NTFS_STREAM_IOCTL_READ) {
+ if (copy_to_user((void __user *)arg, req,
+ sizeof(*req) + req->name_len +
+ req->bytes_returned))
+ err = -EFAULT;
+ } else if (copy_to_user((void __user *)arg +
+ offsetof(struct ntfs_stream, bytes_returned),
+ &req->bytes_returned, sizeof(req->bytes_returned))) {
+ err = -EFAULT;
+ }
+ goto out_drop_write;
+
+out_drop_write:
+ if (claimed)
+ ntfs_stream_put(stream_vi);
+ if (stream_vi)
+ iput(stream_vi);
+ if (got_write)
+ mnt_drop_write_file(filp);
+out_free:
+ if (uname)
+ kmem_cache_free(ntfs_name_cache, uname);
+ kvfree(req);
+ return err;
+}
+
+/*
+ * ntfs_ioctl_stream_read() - Handle a named-stream read ioctl
+ * @filp: file opened on the base file or directory
+ * @arg: userspace pointer to the request and data buffer
+ *
+ * Return: 0 on success, or a negative errno.
+ */
+long ntfs_ioctl_stream_read(struct file *filp, unsigned long arg)
+{
+ return ntfs_ioctl_stream(filp, arg, NTFS_STREAM_IOCTL_READ);
+}
+
+/*
+ * ntfs_ioctl_stream_write() - Handle a named-stream write ioctl
+ * @filp: file opened on the base file or directory
+ * @arg: userspace pointer to the request and data buffer
+ *
+ * Create the stream if needed.
+ *
+ * Return: 0 on success, or a negative errno.
+ */
+long ntfs_ioctl_stream_write(struct file *filp, unsigned long arg)
+{
+ return ntfs_ioctl_stream(filp, arg, NTFS_STREAM_IOCTL_WRITE);
+}
+
+/*
+ * ntfs_ioctl_stream_remove() - Handle a named-stream remove ioctl
+ * @filp: file opened on the base file or directory
+ * @arg: userspace pointer to the request
+ *
+ * Return: 0 on success, or a negative errno.
+ */
+long ntfs_ioctl_stream_remove(struct file *filp, unsigned long arg)
+{
+ return ntfs_ioctl_stream(filp, arg, NTFS_STREAM_IOCTL_REMOVE);
+}
+
+/*
+ * ntfs_ioctl_list_streams() - List named DATA streams
+ * @filp: file opened on the base file or directory
+ * @arg: userspace pointer to the request and output buffer
+ *
+ * On -ENOSPC, return the required buffer size in the request header.
+ *
+ * Return: 0 on success, or a negative errno.
+ */
+int ntfs_ioctl_list_streams(struct file *filp, unsigned long arg)
+{
+ struct inode *inode = file_inode(filp);
+ struct ntfs_inode *ni = NTFS_I(inode);
+ struct ntfs_list_streams hdr;
+ struct ntfs_stream_entry *entry, *last_entry = NULL;
+ size_t required = 0;
+ void *kbuf = NULL;
+ void __user *ubuf;
+ struct attr_record *a;
+ struct ntfs_attr_search_ctx *actx;
+ int ret = 0, err, count = 0;
+ size_t entry_size, offset = 0;
+ const size_t name_offset = offsetof(struct ntfs_stream_entry, name);
+ int name_len;
+ unsigned char *sn = NULL;
+ bool utf16;
+
+ if (NVolShutdown(ni->vol))
+ return -EIO;
+ if (NInoAttr(ni) ||
+ (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode)))
+ return -EOPNOTSUPP;
+ err = inode_permission(file_mnt_idmap(filp), inode, MAY_READ);
+ if (err)
+ return err;
+
+ if (copy_from_user(&hdr, (void __user *)arg, sizeof(hdr)))
+ return -EFAULT;
+
+ if ((hdr.flags & ~NTFS_STREAM_FL_UTF16) || hdr.reserved)
+ return -EINVAL;
+
+ utf16 = hdr.flags & NTFS_STREAM_FL_UTF16;
+
+ ubuf = (void __user *)arg + sizeof(hdr);
+
+ mutex_lock(&ni->mrec_lock);
+ actx = ntfs_attr_get_search_ctx(ni, NULL);
+ if (!actx) {
+ mutex_unlock(&ni->mrec_lock);
+ return -ENOMEM;
+ }
+
+ while ((err = ntfs_attrs_walk(actx)) == 0) {
+ a = actx->attr;
+ if (a->type != AT_DATA || !a->name_length)
+ continue;
+ if (a->non_resident &&
+ a->data.non_resident.lowest_vcn)
+ continue;
+ if (ntfs_stream_unlinked(ni,
+ (__le16 *)((u8 *)a +
+ le16_to_cpu(a->name_offset)),
+ a->name_length))
+ continue;
+
+ if (utf16) {
+ name_len = a->name_length * sizeof(__le16);
+ } else {
+ name_len = ntfs_ucstonls(ni->vol,
+ (__le16 *)((u8 *)a +
+ le16_to_cpu(a->name_offset)),
+ a->name_length, &sn, 0);
+ if (name_len < 0) {
+ if (name_len == -EILSEQ ||
+ name_len == -ENAMETOOLONG)
+ continue;
+ ret = name_len;
+ goto out;
+ }
+ kfree(sn);
+ sn = NULL;
+ }
+
+ entry_size = ALIGN(name_offset + name_len, 8);
+
+ if (required > SIZE_MAX - entry_size) {
+ ret = -EOVERFLOW;
+ goto out;
+ }
+ required += entry_size;
+ count++;
+ }
+ if (err != -ENOENT) {
+ ret = err;
+ goto out;
+ }
+
+ hdr.stream_count = count;
+ hdr.bytes_returned = required;
+
+ if (!count)
+ goto out;
+
+ if (hdr.buffer_size < required) {
+ ret = -ENOSPC;
+ goto out;
+ }
+
+ kbuf = kvzalloc(required, GFP_NOFS);
+ if (!kbuf) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ ntfs_attr_reinit_search_ctx(actx);
+ while ((err = ntfs_attrs_walk(actx)) == 0) {
+ a = actx->attr;
+ if (a->type != AT_DATA || !a->name_length)
+ continue;
+ if (a->non_resident &&
+ a->data.non_resident.lowest_vcn)
+ continue;
+ if (ntfs_stream_unlinked(ni,
+ (__le16 *)((u8 *)a +
+ le16_to_cpu(a->name_offset)),
+ a->name_length))
+ continue;
+ if (utf16) {
+ sn = NULL;
+ name_len = a->name_length * sizeof(__le16);
+ } else {
+ name_len = ntfs_ucstonls(ni->vol,
+ (__le16 *)((u8 *)a +
+ le16_to_cpu(a->name_offset)),
+ a->name_length, &sn, 0);
+ if (name_len < 0) {
+ if (name_len == -EILSEQ ||
+ name_len == -ENAMETOOLONG)
+ continue;
+ ret = name_len;
+ goto out;
+ }
+ }
+
+ entry_size = ALIGN(name_offset + name_len, 8);
+ if (offset > required - entry_size) {
+ ret = -EOVERFLOW;
+ kfree(sn);
+ sn = NULL;
+ goto out;
+ }
+
+ entry = (struct ntfs_stream_entry *)(kbuf + offset);
+
+ if (a->non_resident) {
+ entry->size = le64_to_cpu(a->data.non_resident.data_size);
+ entry->alloc_size =
+ le64_to_cpu(a->data.non_resident.allocated_size);
+ } else {
+ entry->size = le32_to_cpu(a->data.resident.value_length);
+ entry->alloc_size = le32_to_cpu(a->length) -
+ le16_to_cpu(a->data.resident.value_offset);
+ }
+
+ entry->name_len = name_len;
+ entry->name_offset = name_offset;
+ if (utf16)
+ memcpy(entry->name,
+ (u8 *)a + le16_to_cpu(a->name_offset), name_len);
+ else
+ memcpy(entry->name, sn, name_len);
+ kfree(sn);
+ sn = NULL;
+
+ entry->next_entry_off = entry_size;
+ last_entry = entry;
+ offset += entry_size;
+ }
+ if (err != -ENOENT) {
+ ret = err;
+ } else {
+ /* The chain terminates at the last entry. */
+ if (last_entry)
+ last_entry->next_entry_off = 0;
+ hdr.bytes_returned = offset;
+ }
+
+out:
+ kfree(sn);
+ ntfs_attr_put_search_ctx(actx);
+ mutex_unlock(&ni->mrec_lock);
+
+ if (ret && ret != -ENOSPC)
+ goto out_free;
+
+ if (!ret && kbuf && copy_to_user(ubuf, kbuf, hdr.bytes_returned)) {
+ ret = -EFAULT;
+ goto out_free;
+ }
+
+ if (copy_to_user((void __user *)arg, &hdr, sizeof(hdr)))
+ ret = -EFAULT;
+
+out_free:
+ kvfree(kbuf);
+ return ret;
+}
+
+const struct file_operations ntfs_stream_file_ops = {
+ .llseek = ntfs_file_llseek,
+ .read_iter = ntfs_file_read_iter,
+ .write_iter = ntfs_file_write_iter,
+ .fsync = ntfs_file_fsync,
+ .fallocate = ntfs_fallocate,
+ .mmap_prepare = ntfs_file_mmap_prepare,
+ .open = ntfs_stream_file_open,
+ .release = ntfs_stream_file_release,
+ .splice_read = ntfs_file_splice_read,
+ .splice_write = iter_file_splice_write,
+};
+
+const struct inode_operations ntfs_stream_inode_ops = {
+ .permission = ntfs_stream_permission,
+ .setattr = ntfs_stream_setattr,
+ .getattr = ntfs_stream_getattr,
+ .listxattr = ntfs_stream_listxattr,
+#ifdef CONFIG_NTFS_FS_POSIX_ACL
+ .get_acl = ntfs_stream_get_acl,
+ .set_acl = ntfs_stream_set_acl,
+#endif
+ .update_time = ntfs_stream_update_time,
+ .fiemap = ntfs_fiemap,
+};
diff --git a/fs/ntfs/namei.c b/fs/ntfs/namei.c
index 75e2010965257..9251951a76fb3 100644
--- a/fs/ntfs/namei.c
+++ b/fs/ntfs/namei.c
@@ -8,6 +8,7 @@
#include <linux/exportfs.h>
#include <linux/iversion.h>
+#include <linux/namei.h>
#include "ntfs.h"
#include "time.h"
@@ -15,6 +16,7 @@
#include "reparse.h"
#include "object_id.h"
#include "ea.h"
+#include "stream.h"
static const __le16 aux_name_le[3] = {
cpu_to_le16('A'), cpu_to_le16('U'), cpu_to_le16('X')
@@ -57,7 +59,18 @@ static inline int ntfs_check_bad_char(const __le16 *wc, unsigned int wc_len)
return 0;
}
-static int ntfs_check_bad_windows_name(struct ntfs_volume *vol,
+/*
+ * ntfs_check_bad_windows_name() - Validate a name against Windows rules
+ * @vol: NTFS volume
+ * @wc: UTF-16LE name
+ * @wc_len: length of @wc in UTF-16 code units
+ *
+ * When Windows-name checks are enabled, reject disallowed characters,
+ * trailing spaces or dots, and reserved DOS device names.
+ *
+ * Return: 0 if valid, or -EINVAL otherwise.
+ */
+int ntfs_check_bad_windows_name(struct ntfs_volume *vol,
const __le16 *wc,
unsigned int wc_len)
{
@@ -167,19 +180,29 @@ static int ntfs_check_bad_windows_name(struct ntfs_volume *vol,
*
* Locking: Caller must hold i_mutex on the directory.
*/
+
static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
unsigned int flags)
{
struct ntfs_volume *vol = NTFS_SB(dir_ino->i_sb);
+ struct ntfs_stream_path stream_path;
struct inode *dent_inode;
__le16 *uname;
struct ntfs_name *name = NULL;
u64 mref;
unsigned long dent_ino;
int uname_len;
+ int stream_path_len;
ntfs_debug("Looking up %pd in directory inode 0x%llx.",
dent, NTFS_I(dir_ino)->mft_no);
+ stream_path_len = ntfs_stream_path_parse(vol, &dent->d_name,
+ &stream_path);
+ if (stream_path_len < 0)
+ return ERR_PTR(stream_path_len);
+ if (stream_path_len)
+ return ntfs_lookup_stream(dir_ino, dent, &stream_path);
+
/* Convert the name of the dentry to Unicode. */
uname_len = ntfs_nlstoucs(vol, dent->d_name.name, dent->d_name.len,
&uname, NTFS_MAX_NAME_LEN);
@@ -405,6 +428,7 @@ static struct ntfs_inode *__ntfs_create(struct mnt_idmap *idmap, struct inode *d
struct inode *vi;
struct mft_record *ni_mrec, *dni_mrec;
struct super_block *sb = dir_ni->vol->sb;
+ struct inode *rollback_data_vi = NULL, *rollback_sd_vi = NULL;
__le64 parent_mft_ref;
u64 child_mft_ref;
__le16 ea_size;
@@ -694,11 +718,25 @@ static struct ntfs_inode *__ntfs_create(struct mnt_idmap *idmap, struct inode *d
return ni;
err_out:
- if (rollback_sd)
- ntfs_attr_remove(ni, AT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0);
+ if (rollback_sd) {
+ int rollback_err;
+
+ rollback_err = ntfs_attr_remove(ni,
+ AT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0,
+ &rollback_sd_vi);
+ if (rollback_err)
+ ntfs_error(sb,
+ "Failed to roll back security descriptor.\n");
+ }
+
+ if (rollback_data) {
+ int rollback_err;
- if (rollback_data)
- ntfs_attr_remove(ni, AT_DATA, AT_UNNAMED, 0);
+ rollback_err = ntfs_attr_remove(ni, AT_DATA, AT_UNNAMED,
+ 0, &rollback_data_vi);
+ if (rollback_err)
+ ntfs_error(sb, "Failed to roll back DATA attribute.\n");
+ }
if (rollback_reparse)
ntfs_delete_reparse_index(ni);
@@ -726,6 +764,10 @@ static struct ntfs_inode *__ntfs_create(struct mnt_idmap *idmap, struct inode *d
mutex_unlock(&dir_ni->mrec_lock);
mutex_unlock(&ni->mrec_lock);
+ if (rollback_data_vi)
+ iput(rollback_data_vi);
+ if (rollback_sd_vi)
+ iput(rollback_sd_vi);
remove_inode_hash(vi);
discard_new_inode(vi);
return ERR_PTR(err);
@@ -736,12 +778,35 @@ static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir,
{
struct ntfs_volume *vol = NTFS_SB(dir->i_sb);
struct ntfs_inode *ni;
- __le16 *uname;
- int uname_len, err;
+ struct inode *stream_vi = NULL;
+ __le16 *uname, *base_name = NULL, *stream_name = NULL;
+ int uname_len, stream_len, path_result, err;
if (NVolShutdown(vol))
return -EIO;
+ path_result = ntfs_stream_path_names(vol, &dentry->d_name, &base_name,
+ &uname_len, &stream_name, &stream_len);
+ if (path_result) {
+ if (path_result < 0)
+ return path_result;
+ stream_vi = ntfs_create_named_stream(idmap, dir, base_name,
+ uname_len, stream_name, stream_len);
+ if (IS_ERR(stream_vi)) {
+ err = PTR_ERR(stream_vi);
+ goto out_free_stream_names;
+ }
+ kmem_cache_free(ntfs_name_cache, stream_name);
+ kmem_cache_free(ntfs_name_cache, base_name);
+ d_instantiate(dentry, stream_vi);
+ return 0;
+
+out_free_stream_names:
+ kmem_cache_free(ntfs_name_cache, stream_name);
+ kmem_cache_free(ntfs_name_cache, base_name);
+ return err;
+ }
+
uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len,
&uname, NTFS_MAX_NAME_LEN);
if (uname_len < 0) {
@@ -758,7 +823,8 @@ static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir,
ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
- ni = __ntfs_create(idmap, dir, uname, uname_len, S_IFREG | mode, 0, NULL, 0);
+ ni = __ntfs_create(idmap, dir, uname, uname_len, S_IFREG | mode, 0,
+ NULL, 0);
kmem_cache_free(ntfs_name_cache, uname);
if (IS_ERR(ni))
return PTR_ERR(ni);
@@ -805,8 +871,29 @@ static int ntfs_test_inode_attr(struct inode *vi, void *data)
return 0;
if (NInoAttr(ni) || ni->nr_extents == -1)
return 1;
- else
- return 0;
+ return 0;
+}
+
+static void ntfs_cleanup_deleted_inode(struct ntfs_inode *ni)
+{
+ struct inode *attr_vi;
+ struct super_block *sb = VFS_I(ni)->i_sb;
+
+ if (!NInoBeingDeleted(ni))
+ return;
+
+ while ((attr_vi = ilookup5(sb, ni->mft_no, ntfs_test_inode_attr,
+ (void *)(uintptr_t)ni->mft_no))) {
+ struct ntfs_inode *attr_ni = NTFS_I(attr_vi);
+
+ if (ntfs_inode_is_named_stream(attr_ni)) {
+ if (atomic_read(&attr_ni->stream_open_count) > 0)
+ NInoSetStreamUnlinked(attr_ni);
+ remove_inode_hash(attr_vi);
+ }
+ clear_nlink(attr_vi);
+ iput(attr_vi);
+ }
}
/*
@@ -829,10 +916,10 @@ static int ntfs_delete(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,
struct file_name_attr *fn = NULL;
bool looking_for_dos_name = false, looking_for_win32_name = false;
bool case_sensitive_match = true;
+ bool link_count_zero = false;
int err = 0;
struct mft_record *ni_mrec;
struct super_block *sb;
- bool link_count_zero = false;
ntfs_debug("Entering.\n");
@@ -976,22 +1063,10 @@ static int ntfs_delete(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,
if (need_lock == true) {
mutex_unlock(&dir_ni->mrec_lock);
mutex_unlock(&ni->mrec_lock);
+ if (link_count_zero)
+ ntfs_cleanup_deleted_inode(ni);
}
- /*
- * If hard link count is not equal to zero then we are done. In other
- * case there are no reference to this inode left, so we should free all
- * non-resident attributes and mark all MFT record as not in use.
- */
- if (link_count_zero == true) {
- struct inode *attr_vi;
-
- while ((attr_vi = ilookup5(sb, ni->mft_no, ntfs_test_inode_attr,
- (void *)(uintptr_t)ni->mft_no)) != NULL) {
- clear_nlink(attr_vi);
- iput(attr_vi);
- }
- }
ntfs_debug("Done.\n");
return 0;
err_out:
@@ -1016,6 +1091,11 @@ static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
if (NVolShutdown(vol))
return -EIO;
+ if (ntfs_stream_path_has_colon(vol, &dentry->d_name))
+ return ntfs_unlink_named_stream(dir, dentry);
+ if (NInoAttr(ni))
+ return -EOPNOTSUPP;
+
uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len,
&uname, NTFS_MAX_NAME_LEN);
if (uname_len < 0) {
@@ -1059,6 +1139,10 @@ static struct dentry *ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
if (NVolShutdown(vol))
return ERR_PTR(-EIO);
+ err = ntfs_stream_path_check(vol, &dentry->d_name);
+ if (err)
+ return ERR_PTR(err);
+
uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len,
&uname, NTFS_MAX_NAME_LEN);
if (uname_len < 0) {
@@ -1075,7 +1159,8 @@ static struct dentry *ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
- ni = __ntfs_create(idmap, dir, uname, uname_len, mode, 0, NULL, 0);
+ ni = __ntfs_create(idmap, dir, uname, uname_len, mode, 0,
+ NULL, 0);
kmem_cache_free(ntfs_name_cache, uname);
if (IS_ERR(ni)) {
err = PTR_ERR(ni);
@@ -1099,6 +1184,10 @@ static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
if (NVolShutdown(vol))
return -EIO;
+ err = ntfs_stream_path_check(vol, &dentry->d_name);
+ if (err)
+ return err;
+
ni = NTFS_I(vi);
uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len,
&uname, NTFS_MAX_NAME_LEN);
@@ -1263,6 +1352,13 @@ static int ntfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
if (NVolShutdown(old_dir_ni->vol))
return -EIO;
+ err = ntfs_stream_path_check(vol, &old_dentry->d_name);
+ if (err)
+ return err;
+ err = ntfs_stream_path_check(vol, &new_dentry->d_name);
+ if (err)
+ return err;
+
if (flags & (RENAME_EXCHANGE | RENAME_WHITEOUT))
return -EINVAL;
@@ -1385,6 +1481,8 @@ static int ntfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
if (new_ni)
mutex_unlock(&new_ni->mrec_lock);
mutex_unlock(&old_ni->mrec_lock);
+ if (new_ni)
+ ntfs_cleanup_deleted_inode(new_ni);
if (uname_new)
kmem_cache_free(ntfs_name_cache, uname_new);
if (uname_old)
@@ -1408,6 +1506,10 @@ static int ntfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
if (NVolShutdown(vol))
return -EIO;
+ err = ntfs_stream_path_check(vol, &dentry->d_name);
+ if (err)
+ return err;
+
usrc_len = ntfs_nlstoucs(vol, dentry->d_name.name,
dentry->d_name.len, &usrc, NTFS_MAX_NAME_LEN);
if (usrc_len < 0) {
@@ -1453,6 +1555,10 @@ static int ntfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
if (NVolShutdown(vol))
return -EIO;
+ err = ntfs_stream_path_check(vol, &dentry->d_name);
+ if (err)
+ return err;
+
uname_len = ntfs_nlstoucs(vol, dentry->d_name.name,
dentry->d_name.len, &uname, NTFS_MAX_NAME_LEN);
if (uname_len < 0) {
@@ -1505,6 +1611,13 @@ static int ntfs_link(struct dentry *old_dentry, struct inode *dir,
if (NVolShutdown(vol))
return -EIO;
+ if (NInoAttr(ni))
+ return -EOPNOTSUPP;
+
+ err = ntfs_stream_path_check(vol, &dentry->d_name);
+ if (err)
+ return err;
+
uname_len = ntfs_nlstoucs(vol, dentry->d_name.name,
dentry->d_name.len, &uname, NTFS_MAX_NAME_LEN);
if (uname_len < 0) {
@@ -1658,11 +1771,19 @@ static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
ntfs_nfs_get_inode);
}
+static int ntfs_encode_fh(struct inode *inode, u32 *fh, int *max_len,
+ struct inode *parent)
+{
+ if (ntfs_inode_is_named_stream(NTFS_I(inode)))
+ return -EOPNOTSUPP;
+ return generic_encode_ino32_fh(inode, fh, max_len, parent);
+}
+
/*
* Export operations allowing NFS exporting of mounted NTFS partitions.
*/
const struct export_operations ntfs_export_ops = {
- .encode_fh = generic_encode_ino32_fh,
+ .encode_fh = ntfs_encode_fh,
.get_parent = ntfs_get_parent, /* Find the parent of a given directory. */
.fh_to_dentry = ntfs_fh_to_dentry,
.fh_to_parent = ntfs_fh_to_parent,
diff --git a/fs/ntfs/stream.h b/fs/ntfs/stream.h
new file mode 100644
index 0000000000000..5cbf6e8f6b681
--- /dev/null
+++ b/fs/ntfs/stream.h
@@ -0,0 +1,81 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef _NTFS_STREAM_H
+#define _NTFS_STREAM_H
+
+#include <linux/fs.h>
+
+struct ntfs_inode;
+struct ntfs_volume;
+
+struct ntfs_stream_path {
+ const char *base_name;
+ unsigned int base_len;
+ const char *stream_name;
+ unsigned int stream_len;
+};
+
+int ntfs_check_bad_windows_name(struct ntfs_volume *vol,
+ const __le16 *name, unsigned int name_len);
+int ntfs_check_stream_name(const __le16 *name, unsigned int name_len);
+
+int ntfs_stream_path_parse(struct ntfs_volume *vol,
+ const struct qstr *qname, struct ntfs_stream_path *path);
+bool ntfs_stream_path_has_colon(struct ntfs_volume *vol,
+ const struct qstr *qname);
+int ntfs_stream_path_check(struct ntfs_volume *vol,
+ const struct qstr *qname);
+int ntfs_stream_path_names(struct ntfs_volume *vol,
+ const struct qstr *qname, __le16 **base_name, int *base_len,
+ __le16 **stream_name, int *stream_len);
+
+struct inode *ntfs_stream_lookup_inode_by_name(struct inode *dir,
+ __le16 *name, int name_len);
+struct dentry *ntfs_lookup_stream(struct inode *dir, struct dentry *dentry,
+ const struct ntfs_stream_path *path);
+struct inode *ntfs_create_named_stream(struct mnt_idmap *idmap,
+ struct inode *dir, __le16 *base_name, int base_len,
+ __le16 *stream_name, int stream_len);
+int ntfs_unlink_named_stream(struct inode *dir, struct dentry *dentry);
+
+void ntfs_set_default_dentry_ops(struct super_block *sb);
+
+void ntfs_stream_inode_refresh(struct inode *inode);
+int ntfs_stream_inode_validate(struct inode *inode);
+void ntfs_stream_update_base_time(struct ntfs_inode *base_ni);
+bool ntfs_stream_unlinked(struct ntfs_inode *base_ni,
+ const __le16 *name, u32 name_len);
+int ntfs_stream_put(struct inode *inode);
+int ntfs_remove_named_stream(struct ntfs_inode *ni, __le16 *name,
+ u32 name_len, struct inode *expected_inode);
+
+int ntfs_stream_validate_for_mutation(struct inode *inode, bool nowait);
+long ntfs_ioctl_stream_read(struct file *file, unsigned long arg);
+long ntfs_ioctl_stream_write(struct file *file, unsigned long arg);
+long ntfs_ioctl_stream_remove(struct file *file, unsigned long arg);
+int ntfs_ioctl_list_streams(struct file *file, unsigned long arg);
+
+extern const struct file_operations ntfs_stream_file_ops;
+extern const struct inode_operations ntfs_stream_inode_ops;
+
+/*
+ * Common file operations used by both ordinary files and named streams.
+ * Their implementations remain in file.c.
+ */
+int ntfs_file_open(struct inode *inode, struct file *file);
+int ntfs_file_release(struct inode *inode, struct file *file);
+int ntfs_file_fsync(struct file *file, loff_t start, loff_t end,
+ int datasync);
+int ntfs_setattr_size(struct inode *inode, struct iattr *attr);
+loff_t ntfs_file_llseek(struct file *file, loff_t offset, int whence);
+ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to);
+ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from);
+int ntfs_file_mmap_prepare(struct vm_area_desc *desc);
+ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos,
+ struct pipe_inode_info *pipe, size_t len, unsigned int flags);
+int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
+ u64 start, u64 len);
+long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len);
+
+int ntfs_test_inode_rcu(struct inode *inode, void *data);
+
+#endif /* _NTFS_STREAM_H */
diff --git a/fs/ntfs/super.c b/fs/ntfs/super.c
index d174867b9f5f2..242fb19ced2b4 100644
--- a/fs/ntfs/super.c
+++ b/fs/ntfs/super.c
@@ -23,6 +23,7 @@
#include "ea.h"
#include "bitmap.h"
#include "volume.h"
+#include "stream.h"
/* A global default upcase table and a corresponding reference count. */
static __le16 *default_upcase;
@@ -67,6 +68,17 @@ static const struct constant_table ntfs_symlink_enums[] = {
{}
};
+enum {
+ STREAMS_INTERFACE_NONE,
+ STREAMS_INTERFACE_WINDOWS,
+};
+
+static const struct constant_table ntfs_streams_interface_enums[] = {
+ { "none", STREAMS_INTERFACE_NONE },
+ { "windows", STREAMS_INTERFACE_WINDOWS },
+ {}
+};
+
enum {
Opt_uid,
Opt_gid,
@@ -92,6 +104,7 @@ enum {
Opt_nocase,
Opt_native_symlink,
Opt_symlink,
+ Opt_streams_interface,
};
static const struct fs_parameter_spec ntfs_parameters[] = {
@@ -119,6 +132,8 @@ static const struct fs_parameter_spec ntfs_parameters[] = {
fsparam_flag("nocase", Opt_nocase),
fsparam_enum("native_symlink", Opt_native_symlink, ntfs_native_symlink_enums),
fsparam_enum("symlink", Opt_symlink, ntfs_symlink_enums),
+ fsparam_enum("streams_interface", Opt_streams_interface,
+ ntfs_streams_interface_enums),
{}
};
@@ -255,6 +270,22 @@ static int ntfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
else
NVolClearSymlinkNative(vol);
break;
+ case Opt_streams_interface: {
+ bool streams_windows =
+ result.uint_32 == STREAMS_INTERFACE_WINDOWS;
+
+ if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE &&
+ NVolStreamsWindows(vol) != streams_windows) {
+ ntfs_error(vol->sb,
+ "Cannot change streams_interface on remount.");
+ return -EINVAL;
+ }
+ if (streams_windows)
+ NVolSetStreamsWindows(vol);
+ else
+ NVolClearStreamsWindows(vol);
+ break;
+ }
case Opt_sparse:
break;
default:
@@ -2725,6 +2756,8 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
* operations and associated address space operations to function.
*/
sb->s_op = &ntfs_sops;
+ if (NVolStreamsWindows(vol))
+ ntfs_set_default_dentry_ops(sb);
tmp_ino = new_inode(sb);
if (!tmp_ino) {
if (!silent)
diff --git a/fs/ntfs/volume.h b/fs/ntfs/volume.h
index 6d9d6eb79a7c1..ed5c0ccb9cd9f 100644
--- a/fs/ntfs/volume.h
+++ b/fs/ntfs/volume.h
@@ -197,6 +197,7 @@ struct ntfs_volume {
* NV_DisableSparse Disable creation of sparse regions.
* NV_NativeSymlinkRel Translate absolute Windows reparse targets (native_symlink=rel).
* NV_MftBootstrap Mount is still assembling $MFT's own runlist.
+ * NV_StreamsWindows Interpret the final path component as file:stream.
*/
enum {
NV_Errors,
@@ -218,6 +219,7 @@ enum {
NV_NativeSymlinkRel,
NV_SymlinkNative,
NV_MftBootstrap,
+ NV_StreamsWindows,
};
/*
@@ -258,6 +260,7 @@ DEFINE_NVOL_BIT_OPS(DisableSparse)
DEFINE_NVOL_BIT_OPS(NativeSymlinkRel)
DEFINE_NVOL_BIT_OPS(SymlinkNative)
DEFINE_NVOL_BIT_OPS(MftBootstrap)
+DEFINE_NVOL_BIT_OPS(StreamsWindows)
static inline void ntfs_inc_free_clusters(struct ntfs_volume *vol, s64 nr)
{
diff --git a/fs/ntfs/wof.c b/fs/ntfs/wof.c
index 9847259e5b1a2..294dbe5d711f7 100644
--- a/fs/ntfs/wof.c
+++ b/fs/ntfs/wof.c
@@ -311,7 +311,8 @@ static int parse_wof_chunk_table(struct ntfs_inode *base_ni,
goto out_unlock_mrec;
}
ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
- CASE_SENSITIVE, 0, NULL, 0, ctx);
+ CASE_SENSITIVE,
+ 0, NULL, 0, ctx);
if (ret)
goto out_put_ctx;
@@ -399,7 +400,8 @@ static int ntfs_read_wof_chunk(struct ntfs_volume *vol,
}
err = ntfs_attr_lookup(wof_ni->type, wof_ni->name, wof_ni->name_len,
- CASE_SENSITIVE, 0, NULL, 0, ctx);
+ CASE_SENSITIVE,
+ 0, NULL, 0, ctx);
if (err)
goto out_put_ctx;
diff --git a/include/uapi/linux/ntfs.h b/include/uapi/linux/ntfs.h
new file mode 100644
index 0000000000000..245133352132b
--- /dev/null
+++ b/include/uapi/linux/ntfs.h
@@ -0,0 +1,126 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * Copyright (c) 2026 LG Electronics Co., Ltd.
+ */
+
+#ifndef _UAPI_LINUX_NTFS_H
+#define _UAPI_LINUX_NTFS_H
+#include <linux/types.h>
+#include <linux/ioctl.h>
+
+#define NTFS_IOC_MAGIC 0xEF
+
+/*
+ * Flags for ntfs_stream.flags and ntfs_list_streams.flags.
+ *
+ * NTFS_STREAM_FL_UTF16 makes stream names raw UTF-16LE, exactly as stored on
+ * disk, instead of encoding them with the mounted filesystem NLS. This
+ * allows lossless round-tripping of stream names whose characters are not
+ * representable by the mount NLS (e.g. for Wine, which works in UTF-16
+ * natively). When set, the name length fields count bytes of UTF-16LE and
+ * must therefore be even.
+ */
+#define NTFS_STREAM_FL_UTF16 0x1
+
+/*
+ * ntfs named stream read, write, and remove ioctl structure.
+ *
+ * @stream_offset: Offset within the named stream for read/write.
+ * @io_len: Number of bytes to read/write. Must be zero for remove.
+ * @bytes_returned: Actual bytes transferred (out).
+ * @name_len: Stream name length in bytes, not including any
+ * terminating NUL. When NTFS_STREAM_FL_UTF16 is set,
+ * this counts UTF-16LE bytes and must be even.
+ * @flags: Bit mask of NTFS_STREAM_FL_* flags. Other bits must
+ * be zero.
+ * @reserved: Must be zero.
+ * @buffer: Bare stream name followed by stream data for read/write.
+ *
+ * The stream name is encoded with the mounted filesystem NLS, or as raw
+ * UTF-16LE when NTFS_STREAM_FL_UTF16 is set. A write creates the stream if
+ * it does not already exist. A write failure after creation may leave the
+ * new stream behind; it can be removed separately.
+ *
+ * For NTFS_IOC_STREAM_READ, a successful short read indicates EOF. If a read
+ * fails, the ioctl returns an error and copies no stream data to userspace.
+ */
+struct ntfs_stream {
+ __aligned_u64 stream_offset;
+ __aligned_u64 io_len;
+ __aligned_u64 bytes_returned;
+ __u32 name_len;
+ __u32 flags;
+ __aligned_u64 reserved;
+ __u8 buffer[];
+};
+
+/*
+ * Single stream entry returned by NTFS_IOC_LIST_STREAMS.
+ *
+ * @next_entry_off: Byte offset from the start of this entry to the next
+ * entry, or zero for the last entry. Always a multiple
+ * of 8. Consumers must use this to advance instead of
+ * computing the stride themselves.
+ * @size: Stream data size in bytes.
+ * @alloc_size: Bytes allocated for the stream (cluster aligned for
+ * non-resident streams).
+ * @name_len: Stream name length in bytes, not including a NUL
+ * terminator (none is stored). Counts UTF-16LE bytes
+ * when NTFS_STREAM_FL_UTF16 was requested.
+ * @name_offset: Byte offset from the start of this entry to @name.
+ * @reserved: Must be zero.
+ * @name: Bare stream name; NLS encoded, or raw UTF-16LE when
+ * NTFS_STREAM_FL_UTF16 was requested.
+ *
+ * New fixed fields may be added after @reserved and before @name. Consumers
+ * must use @name_offset to locate the name and @next_entry_off to advance to
+ * the next entry.
+ */
+struct ntfs_stream_entry {
+ __aligned_u64 next_entry_off;
+ __aligned_u64 size;
+ __aligned_u64 alloc_size;
+ __u32 name_len;
+ __u32 name_offset;
+ __u32 reserved;
+ __u8 name[];
+};
+
+/*
+ * ntfs list streams ioctl structure.
+ *
+ * @buffer_size: user buffer size(in).
+ * @bytes_returned: actual bytes written or required(out).
+ * @stream_count: number of streams(out).
+ * @flags: Bit mask of NTFS_STREAM_FL_* flags controlling the
+ * encoding of the returned names; other bits must be zero.
+ * @reserved: Must be zero.
+ * @buffer: ntfs_stream_entry array.
+ *
+ * The variable-length entries follow the header in @buffer, each aligned on
+ * an 8-byte boundary and chained via ntfs_stream_entry.next_entry_off. If
+ * @buffer_size is too small, no data is copied, @stream_count and
+ * @bytes_returned report the required values and the ioctl fails with
+ * -ENOSPC. In NLS mode, names that cannot be represented by the mounted
+ * character set are omitted; use NTFS_STREAM_FL_UTF16 to list all
+ * names without conversion loss.
+ */
+struct ntfs_list_streams {
+ __aligned_u64 buffer_size;
+ __aligned_u64 bytes_returned;
+ __aligned_u64 stream_count;
+ __u32 flags;
+ __u32 reserved;
+ __u8 buffer[];
+};
+
+#define NTFS_IOC_STREAM_READ \
+ _IOWR(NTFS_IOC_MAGIC, 1, struct ntfs_stream)
+#define NTFS_IOC_STREAM_WRITE \
+ _IOWR(NTFS_IOC_MAGIC, 2, struct ntfs_stream)
+#define NTFS_IOC_STREAM_REMOVE \
+ _IOWR(NTFS_IOC_MAGIC, 3, struct ntfs_stream)
+#define NTFS_IOC_LIST_STREAMS \
+ _IOWR(NTFS_IOC_MAGIC, 4, struct ntfs_list_streams)
+
+#endif /* _UAPI_LINUX_NTFS_H */
|
| 13/3 | 2026/10/11 22:53 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 61252 output: 34 thoughts: 1348 Thoughts: **KMSAN Fuzzing Session Evaluation: NTFS Named Streams** Okay, let's break this down. My goal is to determine if this patch series, implementing named stream support for `fs/ntfs/`, warrants a dedicated KMSAN fuzzing session. The instructions are clear: KMSAN is expensive, so only use it if we're dealing with *uninitialized memory* risks that KASAN, LOCKDEP, or standard detectors won't catch. The key is to identify situations where the patch introduces or exposes the risk of reading uninitialized memory. Specifically, I need to focus on these areas: 1. Kernel structures sent to userspace. This is where uninitialized fields can become an issue. 2. Conditional logic depending on uninitialized variables or struct fields. 3. Allocation and initialization of complex data structures with potentially uninitialized fields that might get read. 4. Bounds checks and lengths that let code access uninitialized parts of existing buffers. Now, looking at the patch, the crucial part is within the `NTFS_IOC_STREAM_READ` ioctl. I see it uses `kvmalloc(total_size, GFP_KERNEL)` for allocation. That immediately raises a red flag. The thing is, `kvmalloc` *doesn't* zero the allocated memory. Then, there's `copy_from_user(req, (void __user *)arg, sizeof(hdr) + hdr.name_len)`. The code allocates `total_size`, but `copy_from_user` only copies `sizeof(hdr) + hdr.name_len` into `req`. So, anything after that, namely `req->buffer + req->name_len` is uninitialized. That's uninitialized memory right there! This is a classic case of allocating a buffer and only partially initializing it, then reading data from it. The interesting part of how the data is read with `ntfs_inode_attr_pread`, is what does `ntfs_inode_attr_pread` do with the data? Also, the size of data returned from `ntfs_inode_attr_pread` to `req` is checked to determine how much we copy back to userspace with `copy_to_user`. What is `ntfs_inode_attr_pread` doing? Based on the information I've seen so far, I believe the risk of an uninitialized memory read is significant and this patch introduces new and exposes potential info-leaks. I need to analyze `ntfs_inode_attr_pread`. |
| 14/3 | 2026/10/11 22:53 | tool | grepper |
0mArgs:{
"Expression": "ntfs_inode_attr_pread",
"PathPrefixes": [
"fs/ntfs/"
]
}Results: {
"Output": "fs/ntfs/attrib.c=1100=int load_attribute_list(struct ntfs_inode *base_ni, u8 *al_start, const s64 size)\n--\nfs/ntfs/attrib.c-1114-\nfs/ntfs/attrib.c:1115:\tif (ntfs_inode_attr_pread(attr_vi, 0, size, al_start) != size) {\nfs/ntfs/attrib.c-1116-\t\tiput(attr_vi);\n--\nfs/ntfs/attrib.c=5551=void *ntfs_attr_readall(struct ntfs_inode *ni, const __le32 type,\n--\nfs/ntfs/attrib.c-5583-\nfs/ntfs/attrib.c:5584:\tsize = ntfs_inode_attr_pread(VFS_I(bmp_ni), 0, bmp_ni-\u003edata_size,\nfs/ntfs/attrib.c-5585-\t\t\t(u8 *)data);\n--\nfs/ntfs/attrlist.c=68=static int ntfs_attrlist_repack(struct inode *attr_vi,\n--\nfs/ntfs/attrlist.c-99-\nfs/ntfs/attrlist.c:100:\t\twritten = ntfs_inode_attr_pread(attr_vi, 0, data_size, data);\nfs/ntfs/attrlist.c-101-\t\tif (written != data_size) {\n--\nfs/ntfs/index.c=747=static int ntfs_ib_read(struct ntfs_index_context *icx, s64 vcn, struct index_block *dst)\n--\nfs/ntfs/index.c-754-\nfs/ntfs/index.c:755:\tret = ntfs_inode_attr_pread(VFS_I(icx-\u003eia_ni), pos, icx-\u003eblock_size, (u8 *)dst);\nfs/ntfs/index.c-756-\tif (ret != icx-\u003eblock_size) {\n--\nfs/ntfs/index.c=1037=static int ntfs_ibm_modify(struct ntfs_index_context *icx, s64 vcn, int set)\n--\nfs/ntfs/index.c-1067-\nfs/ntfs/index.c:1068:\tif (ntfs_inode_attr_pread(bmp_vi, bpos, 1, \u0026byte) != 1) {\nfs/ntfs/index.c-1069-\t\tret = -EIO;\n--\nfs/ntfs/inode.c=3577=int ntfs_inode_free_space(struct ntfs_inode *ni, int size)\n--\nfs/ntfs/inode.c-3686-\nfs/ntfs/inode.c:3687:s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf)\nfs/ntfs/inode.c-3688-{\n--\nfs/ntfs/inode.h=364=int ntfs_inode_free_space(struct ntfs_inode *ni, int size);\nfs/ntfs/inode.h:365:s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf);\nfs/ntfs/inode.h-366-s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf,\n--\nfs/ntfs/named_stream.c=1332=static long ntfs_ioctl_stream(struct file *filp, unsigned long arg,\n--\nfs/ntfs/named_stream.c-1482-\t\tif (pos \u003c i_size_read(stream_vi))\nfs/ntfs/named_stream.c:1483:\t\t\tret = ntfs_inode_attr_pread(stream_vi, pos, data_size,\nfs/ntfs/named_stream.c-1484-\t\t\t\t\treq-\u003ebuffer + req-\u003ename_len);\n--\nfs/ntfs/object_id.c=100=static int remove_object_id_index(struct ntfs_inode *ni, struct ntfs_index_context *xo)\n--\nfs/ntfs/object_id.c-108-\t/* read the existing object id attribute */\nfs/ntfs/object_id.c:109:\tsize = ntfs_inode_attr_pread(VFS_I(ni), 0, sizeof(struct guid),\nfs/ntfs/object_id.c-110-\t\t\t\t (char *)\u0026key);\n--\nfs/ntfs/reparse.c=583=static int remove_reparse_index(struct inode *rp, struct ntfs_index_context *xr,\n--\nfs/ntfs/reparse.c-596-\t/* read the existing reparse_tag */\nfs/ntfs/reparse.c:597:\tsize = ntfs_inode_attr_pread(rp, 0, 4, (char *)preparse_tag);\nfs/ntfs/reparse.c-598-\tif (size != 4)\n"
} |
| 15/3 | 2026/10/11 22:53 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 64497 output: 38 thoughts: 25 Thoughts: Okay, let's dive into this. Here's how I'd approach summarizing the prompt: **Inspecting `ntfs_inode_attr_pread` in `fs/ntfs/inode.c`** Alright, so the task at hand is to examine the `ntfs_inode_attr_pread` function. Specifically, I need to get a clear understanding of its role within the NTFS file system implementation. This suggests that it's a critical piece of the puzzle related to how the kernel reads data associated with an inode's attributes, within the NTFS file system context. The very name tells me it's about reading, likely "pre"-reading (as in *pread*) and specifically deals with *attributes* of an *inode*. It’s a pretty direct clue. Given the file path, `fs/ntfs/inode.c`, I'm expecting this function is embedded within the core logic handling NTFS inodes and their data structures. I will expect this function call to handle reading data from the attribute list of an NTFS inode, and probably do so in a specific, buffered, and controlled way. I'll need to figure out what that 'pre-' means in context. I'll need to check the callsites of this function and think about its use. |
| 16/3 | 2026/10/11 22:53 | tool | read-file |
0mArgs:{
"File": "fs/ntfs/inode.c",
"FirstLine": 3685,
"LineCount": 100
}Results: {
"Contents": "3685:\t}\n3686:\t\n3687:\ts64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf)\n3688:\t{\n3689:\t\tstruct address_space *mapping = vi-\u003ei_mapping;\n3690:\t\tstruct folio *folio;\n3691:\t\tstruct ntfs_inode *ni = NTFS_I(vi);\n3692:\t\tstruct ntfs_inode *base_ni = ni-\u003eext.base_ntfs_ino;\n3693:\t\tstruct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?\n3694:\t\t\t\tbase_ni : ni;\n3695:\t\ts64 isize;\n3696:\t\tu32 attr_len, total = 0, offset;\n3697:\t\tpgoff_t index;\n3698:\t\tint err = 0;\n3699:\t\tbool claimed = false;\n3700:\t\n3701:\t\tWARN_ON(!NInoAttr(ni));\n3702:\t\tif (!count)\n3703:\t\t\treturn 0;\n3704:\t\n3705:\t\tmutex_lock(\u0026mrec_ni-\u003emrec_lock);\n3706:\t\tif (ntfs_inode_is_named_stream(ni)) {\n3707:\t\t\terr = ntfs_stream_inode_validate(vi);\n3708:\t\t\tif (err) {\n3709:\t\t\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n3710:\t\t\t\treturn err;\n3711:\t\t\t}\n3712:\t\t}\n3713:\t\tisize = i_size_read(vi);\n3714:\t\tif (pos \u003e isize) {\n3715:\t\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n3716:\t\t\treturn -EINVAL;\n3717:\t\t}\n3718:\t\tif (pos + count \u003e isize)\n3719:\t\t\tcount = isize - pos;\n3720:\t\n3721:\t\tif (!NInoNonResident(ni)) {\n3722:\t\t\tstruct ntfs_attr_search_ctx *ctx;\n3723:\t\t\tu8 *attr;\n3724:\t\n3725:\t\t\tctx = ntfs_attr_get_search_ctx(base_ni, NULL);\n3726:\t\t\tif (!ctx) {\n3727:\t\t\t\tntfs_error(vi-\u003ei_sb, \"Failed to get attr search ctx\");\n3728:\t\t\t\terr = -ENOMEM;\n3729:\t\t\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n3730:\t\t\t\tgoto out;\n3731:\t\t\t}\n3732:\t\n3733:\t\t\terr = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len,\n3734:\t\t\t\t\t CASE_SENSITIVE,\n3735:\t\t\t\t\t 0, NULL, 0, ctx);\n3736:\t\t\tif (err) {\n3737:\t\t\t\tntfs_error(vi-\u003ei_sb, \"Failed to look up attr %#x\", ni-\u003etype);\n3738:\t\t\t\tntfs_attr_put_search_ctx(ctx);\n3739:\t\t\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n3740:\t\t\t\tgoto out;\n3741:\t\t\t}\n3742:\t\n3743:\t\t\tattr = (u8 *)ctx-\u003eattr + le16_to_cpu(ctx-\u003eattr-\u003edata.resident.value_offset);\n3744:\t\t\tmemcpy(buf, (u8 *)attr + pos, count);\n3745:\t\t\tntfs_attr_put_search_ctx(ctx);\n3746:\t\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n3747:\t\t\treturn count;\n3748:\t\t}\n3749:\t\tif (ntfs_inode_is_named_stream(ni)) {\n3750:\t\t\tatomic_inc(\u0026ni-\u003estream_open_count);\n3751:\t\t\tclaimed = true;\n3752:\t\t}\n3753:\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n3754:\t\n3755:\t\tindex = pos \u003e\u003e PAGE_SHIFT;\n3756:\t\tdo {\n3757:\t\t\t/* Update @index and get the next folio. */\n3758:\t\t\tfolio = read_mapping_folio(mapping, index, NULL);\n3759:\t\t\tif (IS_ERR(folio)) {\n3760:\t\t\t\terr = PTR_ERR(folio);\n3761:\t\t\t\tbreak;\n3762:\t\t\t}\n3763:\t\n3764:\t\t\toffset = offset_in_folio(folio, pos);\n3765:\t\t\tattr_len = min_t(size_t, (size_t)count, folio_size(folio) - offset);\n3766:\t\n3767:\t\t\tfolio_lock(folio);\n3768:\t\t\tmemcpy_from_folio(buf, folio, offset, attr_len);\n3769:\t\t\tfolio_unlock(folio);\n3770:\t\t\tfolio_put(folio);\n3771:\t\n3772:\t\t\ttotal += attr_len;\n3773:\t\t\tbuf += attr_len;\n3774:\t\t\tpos += attr_len;\n3775:\t\t\tcount -= attr_len;\n3776:\t\t\tindex++;\n3777:\t\t} while (count);\n3778:\tout:\n3779:\t\tif (claimed)\n3780:\t\t\tntfs_stream_put(vi);\n3781:\t\treturn err ? (s64)err : total;\n3782:\t}\n3783:\t\n3784:\tstatic inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,\n"
} |
| 17/3 | 2026/10/11 22:53 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 66371 output: 32 thoughts: 2608 Thoughts: **Analyzing Potential Uninitialized Data and Memory Corruption in NTFS Driver** Okay, let's break this down. My primary concern is uninitialized data and potential memory corruption, specifically within the NTFS driver. I'm focusing on the `ntfs_inode_attr_pread` function and how it interacts with user-provided data. First, I see a `do...while` loop that calls `read_mapping_folio`. If `read_mapping_folio` errors out, it breaks. But what if there's uninitialized data read there? Or, what if `ntfs_inode_attr_pread` itself returns, having copied some bytes to `buf`? `ret < 0` doesn't copy the data, but `total` does copy to the buffer. I'm worried about partially initialized buffers reaching user space. Next, the `struct ntfs_stream`. I've checked the offsets. No padding. Then I see a `copy_from_user` call with `sizeof(hdr) + hdr.name_len`. This means the user controls the size of `req->buffer`! So, `req->stream_offset`, `req->io_len`, `req->name_len`, `req->flags`, `req->reserved` are all initialized from user, and `req->buffer` will have data starting from index 0 up to `hdr.name_len`. The `req->bytes_returned` is initialized to 0, and then set by the return value of `ntfs_inode_attr_pread`. Later, the contents of `req` are sent back to the user with `copy_to_user`. Here, I need to make sure everything is initialized. We know that `sizeof(*req) + req->name_len` was initialized from user and `req->bytes_returned` was set to the return value of `ntfs_inode_attr_pread`. Also, `ret` bytes are written to `req->buffer + req->name_len` by `ntfs_inode_attr_pread`. Then, there's `NTFS_IOC_LIST_STREAMS`, which has the potential to expose uninitialized data. Specifically, I'm examining `ntfs_ioctl_list_streams`. It uses a `struct ntfs_list_streams` and a `kbuf`, allocated with `kvzalloc`. `kvzalloc` zeroes the memory. Good. The `hdr` from user space is copied. The `kbuf` is zeroed. `hdr.stream_count` and `hdr.bytes_returned` are overwritten. Also, the `struct ntfs_stream_entry`'s fields need close examination. I'm concerned about the offset of `name[]` which is at 36, so `name[]` starts at offset 36. `kbuf` is zeroed; therefore, `entry->reserved` will always be zero. `entry_size` calculates the size of the entry, so I need to check how it's calculated. I see that the `entry_size` is aligned to 8. Because `kbuf` is zeroed, all of the aligned bits are zeroes! Finally, I'm checking through all the other files and modifications to look for more patterns. In particular I checked `fs/ntfs/attrib.c`, `fs/ntfs/file.c`, and `fs/ntfs/inode.c`, as those seem related to the operations of the structures and functions I've examined. I'm looking for patterns where data might be read or written and any potential vulnerabilities. In `fs/ntfs/inode.c`, the `ntfs_attr_iget` function and the way named streams are handled seem particularly important to watch. I'll need to trace the data flow through those functions. I also see calls to `kmalloc_array`. I must check for all cases of data structures being passed to and from user space and ensure that all fields are properly initialized. |
| 18/3 | 2026/10/11 22:54 | tool | grepper |
0mArgs:{
"Expression": "ntfs_attr_iget",
"PathPrefixes": [
"fs/ntfs/"
]
}Results: {
"Output": "fs/ntfs/attrib.c=1100=int load_attribute_list(struct ntfs_inode *base_ni, u8 *al_start, const s64 size)\n--\nfs/ntfs/attrib.c-1106-\nfs/ntfs/attrib.c:1107:\tattr_vi = ntfs_attr_iget(VFS_I(base_ni), AT_ATTRIBUTE_LIST, AT_UNNAMED, 0);\nfs/ntfs/attrib.c-1108-\tif (IS_ERR(attr_vi)) {\n--\nfs/ntfs/attrib.c=2831=int ntfs_attr_add(struct ntfs_inode *ni, __le32 type,\n--\nfs/ntfs/attrib.c-2998-\t/* Open new attribute and resize it. */\nfs/ntfs/attrib.c:2999:\tattr_vi = ntfs_attr_iget(VFS_I(ni), type, name, name_len);\nfs/ntfs/attrib.c-3000-\tif (IS_ERR(attr_vi)) {\n--\nfs/ntfs/attrib.c=4823=static int ntfs_resident_attr_resize(struct ntfs_inode *attr_ni, const s64 newsize,\n--\nfs/ntfs/attrib.c-4935-\t\telse\nfs/ntfs/attrib.c:4936:\t\t\ttvi = ntfs_attr_iget(VFS_I(base_ni), a-\u003etype,\nfs/ntfs/attrib.c-4937-\t\t\t\t(__le16 *)((u8 *)a + le16_to_cpu(a-\u003ename_offset)),\n--\nfs/ntfs/attrib.c=5494=int ntfs_attr_remove(struct ntfs_inode *ni, const __le32 type, __le16 *name,\n--\nfs/ntfs/attrib.c-5507-\nfs/ntfs/attrib.c:5508:\tvi = ntfs_attr_iget(VFS_I(ni), type, name, name_len);\nfs/ntfs/attrib.c-5509-\tif (IS_ERR(vi)) {\n--\nfs/ntfs/attrib.c=5551=void *ntfs_attr_readall(struct ntfs_inode *ni, const __le32 type,\n--\nfs/ntfs/attrib.c-5561-\nfs/ntfs/attrib.c:5562:\tbmp_vi = ntfs_attr_iget(VFS_I(ni), type, name, name_len);\nfs/ntfs/attrib.c-5563-\tif (IS_ERR(bmp_vi)) {\nfs/ntfs/attrib.c-5564-\t\tret = ERR_PTR(PTR_ERR(bmp_vi));\nfs/ntfs/attrib.c:5565:\t\tntfs_debug(\"ntfs_attr_iget failed\");\nfs/ntfs/attrib.c-5566-\t\tgoto err_exit;\n--\nfs/ntfs/attrlist.c=201=int ntfs_attrlist_update_locked(struct ntfs_inode *base_ni,\n--\nfs/ntfs/attrlist.c-217-\nfs/ntfs/attrlist.c:218:\tattr_vi = ntfs_attr_iget(VFS_I(base_ni), AT_ATTRIBUTE_LIST, AT_UNNAMED, 0);\nfs/ntfs/attrlist.c-219-\tif (IS_ERR(attr_vi)) {\n--\nfs/ntfs/ea.c=22=static int ntfs_write_ea(struct ntfs_inode *ni, __le32 type, char *value, s64 ea_off,\n--\nfs/ntfs/ea.c-28-\nfs/ntfs/ea.c:29:\tea_vi = ntfs_attr_iget(VFS_I(ni), type, AT_UNNAMED, 0);\nfs/ntfs/ea.c-30-\tif (IS_ERR(ea_vi))\n--\nfs/ntfs/file.c=172=int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,\n--\nfs/ntfs/file.c-251-\t\t\t}\nfs/ntfs/file.c:252:\t\t\tattr_vi = ntfs_attr_iget(vi, ctx-\u003eattr-\u003etype,\nfs/ntfs/file.c-253-\t\t\t\t\t\t name, ctx-\u003eattr-\u003ename_length);\n--\nfs/ntfs/index.c=1037=static int ntfs_ibm_modify(struct ntfs_index_context *icx, s64 vcn, int set)\n--\nfs/ntfs/index.c-1048-\nfs/ntfs/index.c:1049:\tbmp_vi = ntfs_attr_iget(VFS_I(icx-\u003eidx_ni), AT_BITMAP, icx-\u003ename, icx-\u003ename_len);\nfs/ntfs/index.c-1050-\tif (IS_ERR(bmp_vi)) {\n--\nfs/ntfs/inode.c=190=struct inode *ntfs_iget(struct super_block *sb, u64 mft_no)\n--\nfs/ntfs/inode.c-227-/*\nfs/ntfs/inode.c:228: * ntfs_attr_iget - obtain a struct inode corresponding to an attribute\nfs/ntfs/inode.c-229- * @base_vi:\tvfs base inode containing the attribute\n--\nfs/ntfs/inode.c-243- * Note, for index allocation attributes, you need to use ntfs_index_iget()\nfs/ntfs/inode.c:244: * instead of ntfs_attr_iget() as working with indices is a lot more complex.\nfs/ntfs/inode.c-245- *\n--\nfs/ntfs/inode.c-249- */\nfs/ntfs/inode.c:250:struct inode *ntfs_attr_iget(struct inode *base_vi, __le32 type,\nfs/ntfs/inode.c-251-\t\t__le16 *name, u32 name_len)\n--\nfs/ntfs/inode.c-256-\nfs/ntfs/inode.c:257:\t/* Make sure no one calls ntfs_attr_iget() for indices. */\nfs/ntfs/inode.c-258-\tWARN_ON(type == AT_INDEX_ALLOCATION);\n--\nfs/ntfs/inode.c=737=static int ntfs_read_locked_inode(struct inode *vi)\n--\nfs/ntfs/inode.c-1336- *\nfs/ntfs/inode.c:1337: * ntfs_read_locked_attr_inode() is called from ntfs_attr_iget() to read the\nfs/ntfs/inode.c-1338- * attribute inode described by @vi into memory from the base mft record\n--\nfs/ntfs/inode.c=1621=static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)\n--\nfs/ntfs/inode.c-1780-\t * We are done with the mft record, so we release it. Otherwise\nfs/ntfs/inode.c:1781:\t * we would deadlock in ntfs_attr_iget().\nfs/ntfs/inode.c-1782-\t */\n--\nfs/ntfs/inode.c-1787-\t/* Get the index bitmap attribute inode. */\nfs/ntfs/inode.c:1788:\tbvi = ntfs_attr_iget(base_vi, AT_BITMAP, ni-\u003ename, ni-\u003ename_len);\nfs/ntfs/inode.c-1789-\tif (IS_ERR(bvi)) {\n--\nfs/ntfs/inode.h=326=struct inode *ntfs_iget(struct super_block *sb, u64 mft_no);\nfs/ntfs/inode.h:327:struct inode *ntfs_attr_iget(struct inode *base_vi, __le32 type,\nfs/ntfs/inode.h-328-\t\t__le16 *name, u32 name_len);\n--\nfs/ntfs/named_stream.c=414=struct dentry *ntfs_lookup_stream(struct inode *dir_ino,\n--\nfs/ntfs/named_stream.c-460-\tmutex_lock(\u0026NTFS_I(base_vi)-\u003emrec_lock);\nfs/ntfs/named_stream.c:461:\tstream_vi = ntfs_attr_iget(base_vi, AT_DATA, stream_uname, stream_len);\nfs/ntfs/named_stream.c-462-\tif (!IS_ERR(stream_vi)) {\n--\nfs/ntfs/named_stream.c=556=struct inode *ntfs_create_named_stream(struct mnt_idmap *idmap,\n--\nfs/ntfs/named_stream.c-630-\nfs/ntfs/named_stream.c:631:\tstream_vi = ntfs_attr_iget(base_vi, AT_DATA, stream_name, stream_len);\nfs/ntfs/named_stream.c-632-\tif (!IS_ERR(stream_vi))\n--\nfs/ntfs/named_stream.c=1086=int ntfs_remove_named_stream(struct ntfs_inode *ni, __le16 *uname,\n--\nfs/ntfs/named_stream.c-1110-\t}\nfs/ntfs/named_stream.c:1111:\tattr_vi = ntfs_attr_iget(VFS_I(ni), AT_DATA, uname, uname_len);\nfs/ntfs/named_stream.c-1112-\tif (IS_ERR(attr_vi)) {\n--\nfs/ntfs/named_stream.c=1236=static int ntfs_stream_ioctl_get_inode(struct inode *base_vi, __le16 *sname,\n--\nfs/ntfs/named_stream.c-1276-\nfs/ntfs/named_stream.c:1277:\tattr_vi = ntfs_attr_iget(base_vi, AT_DATA, sname, sname_len);\nfs/ntfs/named_stream.c-1278-\tif (IS_ERR(attr_vi)) {\n--\nfs/ntfs/object_id.c=128=int ntfs_delete_object_id_index(struct ntfs_inode *ni)\n--\nfs/ntfs/object_id.c-134-\nfs/ntfs/object_id.c:135:\tattr_vi = ntfs_attr_iget(VFS_I(ni), AT_OBJECT_ID, AT_UNNAMED, 0);\nfs/ntfs/object_id.c-136-\tif (IS_ERR(attr_vi))\n--\nfs/ntfs/reparse.c=667=static int update_reparse_data(struct ntfs_inode *ni, struct ntfs_index_context *xr,\n--\nfs/ntfs/reparse.c-676-\nfs/ntfs/reparse.c:677:\trp_inode = ntfs_attr_iget(VFS_I(ni), AT_REPARSE_POINT, AT_UNNAMED, 0);\nfs/ntfs/reparse.c-678-\tif (IS_ERR(rp_inode))\n--\nfs/ntfs/reparse.c=720=int ntfs_delete_reparse_index(struct ntfs_inode *ni)\n--\nfs/ntfs/reparse.c-730-\nfs/ntfs/reparse.c:731:\tvi = ntfs_attr_iget(VFS_I(ni), AT_REPARSE_POINT, AT_UNNAMED, 0);\nfs/ntfs/reparse.c-732-\tif (IS_ERR(vi))\n--\nfs/ntfs/super.c=1623=static int load_and_check_badclus(struct ntfs_volume *vol)\n--\nfs/ntfs/super.c-1643-\t}\nfs/ntfs/super.c:1644:\tbad_ino = ntfs_attr_iget(base_ino, AT_DATA, bad_name,\nfs/ntfs/super.c-1645-\t\t\t\t ARRAY_SIZE(bad_name));\n--\nfs/ntfs/super.c=1719=static int load_system_files(struct ntfs_volume *vol)\n--\nfs/ntfs/super.c-1744-\t/* Get mft bitmap attribute inode. */\nfs/ntfs/super.c:1745:\tvol-\u003emftbmp_ino = ntfs_attr_iget(vol-\u003emft_ino, AT_BITMAP, NULL, 0);\nfs/ntfs/super.c-1746-\tif (IS_ERR(vol-\u003emftbmp_ino)) {\n--\nfs/ntfs/wof.c=654=int ntfs_read_wof_compressed_block(struct folio *folio)\n--\nfs/ntfs/wof.c-681-\nfs/ntfs/wof.c:682:\twof_inode = ntfs_attr_iget(VFS_I(ni), AT_DATA, (__le16 *)WOF_NAME,\nfs/ntfs/wof.c-683-\t\t\t\t WOF_NAME_LEN);\n"
} |
| 19/3 | 2026/10/11 22:54 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 73294 output: 36 thoughts: 23 Thoughts: Here's my summary, as if I were thinking these thoughts: **Diving into the NTFS i-node Implementation** Okay, time to dig into `ntfs_attr_iget`. Specifically, I need to focus on how i-nodes are handled within the NTFS filesystem. I'll need to navigate to `fs/ntfs/inode.c` – that's where the core i-node implementation should reside. This function is critical for translating a filesystem identifier (inode number) into an in-memory i-node structure. I need to understand the details of how attributes are retrieved and associated with the inode during this process, since those details are the critical piece of the puzzle. Let's see what the code reveals. |
| 20/3 | 2026/10/11 22:54 | tool | read-file |
0mArgs:{
"File": "fs/ntfs/inode.c",
"FirstLine": 245,
"LineCount": 60
}Results: {
"Contents": " 245:\t *\n 246:\t * Return the struct inode of the attribute inode on success. Check the return\n 247:\t * value with IS_ERR() and if true, the function failed and the error code is\n 248:\t * obtained from PTR_ERR().\n 249:\t */\n 250:\tstruct inode *ntfs_attr_iget(struct inode *base_vi, __le32 type,\n 251:\t\t\t__le16 *name, u32 name_len)\n 252:\t{\n 253:\t\tstruct inode *vi;\n 254:\t\tint err;\n 255:\t\tstruct ntfs_attr na;\n 256:\t\n 257:\t\t/* Make sure no one calls ntfs_attr_iget() for indices. */\n 258:\t\tWARN_ON(type == AT_INDEX_ALLOCATION);\n 259:\t\n 260:\t\tna.mft_no = base_vi-\u003ei_ino;\n 261:\t\tna.type = type;\n 262:\t\tna.name = name;\n 263:\t\tna.name_len = name_len;\n 264:\t\n 265:\t\tvi = iget5_locked(base_vi-\u003ei_sb, na.mft_no, ntfs_test_inode,\n 266:\t\t\t\tntfs_init_locked_inode, \u0026na);\n 267:\t\tif (unlikely(!vi))\n 268:\t\t\treturn ERR_PTR(-ENOMEM);\n 269:\t\terr = 0;\n 270:\t\n 271:\t\t/* If this is a freshly allocated inode, need to read it now. */\n 272:\t\tif (inode_state_read_once(vi) \u0026 I_NEW) {\n 273:\t\t\terr = ntfs_read_locked_attr_inode(base_vi, vi);\n 274:\t\t\tif (err) {\n 275:\t\t\t\tremove_inode_hash(vi);\n 276:\t\t\t\tdiscard_new_inode(vi);\n 277:\t\t\t} else\n 278:\t\t\t\tunlock_new_inode(vi);\n 279:\t\t}\n 280:\t\t/*\n 281:\t\t * There is no point in keeping bad attribute inodes around. This also\n 282:\t\t * simplifies things in that we never need to check for bad attribute\n 283:\t\t * inodes elsewhere.\n 284:\t\t */\n 285:\t\tif (unlikely(err))\n 286:\t\t\tvi = ERR_PTR(err);\n 287:\t\treturn vi;\n 288:\t}\n 289:\t\n 290:\t/*\n 291:\t * ntfs_index_iget - obtain a struct inode corresponding to an index\n 292:\t * @base_vi:\tvfs base inode containing the index related attributes\n 293:\t * @name:\tUnicode name of the index\n 294:\t * @name_len:\tlength of @name in Unicode characters\n 295:\t *\n 296:\t * Obtain the (fake) struct inode corresponding to the index specified by @name\n 297:\t * and @name_len, which is present in the base mft record specified by the vfs\n 298:\t * inode @base_vi.\n 299:\t *\n 300:\t * If the index inode is in the cache, it is just returned with an increased\n 301:\t * reference count. Otherwise, a new struct inode is allocated and\n 302:\t * initialized, and finally ntfs_read_locked_index_inode() is called to read\n 303:\t * the index related attributes and fill in the inode structure.\n 304:\t *\n"
} |
| 21/3 | 2026/10/11 22:54 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 74470 output: 36 thoughts: 25 Thoughts: Okay, let's dive into this. I need to get a handle on the NTFS inode initialization process, specifically the locked aspect. My instinct is to start by examining the `ntfs_init_locked_inode` function within the `fs/ntfs/inode.c` file. I'm assuming that this is where the core logic for setting up an inode within the NTFS filesystem, and dealing with locking (presumably related to concurrent access), is located. Understanding this function is crucial to understanding how the filesystem manages its internal structures and prevents data corruption, especially in a multi-threaded environment. I expect to see the code that sets up the inode's data structures, initializes any associated locks, and perhaps handles attribute loading or other filesystem-specific setup. My focus will be on identifying any potential race conditions, locking strategies, and how the code ensures data consistency during inode creation or modification. Let's see what's in there. |
| 22/3 | 2026/10/11 22:54 | tool | read-file |
0mArgs:{
"File": "fs/ntfs/inode.c",
"FirstLine": 100,
"LineCount": 70
}Results: {
"Contents": " 100:\t\t}\n 101:\t\tret = ntfs_test_inode(vi, data);\n 102:\tout:\n 103:\t\tspin_unlock(\u0026vi-\u003ei_lock);\n 104:\t\treturn ret;\n 105:\t}\n 106:\t\n 107:\t/*\n 108:\t * ntfs_init_locked_inode - initialize an inode\n 109:\t * @vi:\t\tvfs inode to initialize\n 110:\t * @data:\tdata which to initialize @vi to\n 111:\t *\n 112:\t * Initialize the vfs inode @vi with the values from the ntfs attribute @data in\n 113:\t * order to enable ntfs_test_inode() to do its work.\n 114:\t *\n 115:\t * If initializing the normal file/directory inode, set @na-\u003etype to AT_UNUSED.\n 116:\t * In that case, @na-\u003ename and @na-\u003ename_len should be set to NULL and 0,\n 117:\t * respectively. Although that is not strictly necessary as\n 118:\t * ntfs_read_locked_inode() will fill them in later.\n 119:\t *\n 120:\t * Return 0 on success and error.\n 121:\t *\n 122:\t * NOTE: This function runs with the inode-\u003ei_lock spin lock held so it is not\n 123:\t * allowed to sleep. (Hence the GFP_ATOMIC allocation.)\n 124:\t */\n 125:\tstatic int ntfs_init_locked_inode(struct inode *vi, void *data)\n 126:\t{\n 127:\t\tstruct ntfs_attr *na = data;\n 128:\t\tstruct ntfs_inode *ni = NTFS_I(vi);\n 129:\t\n 130:\t\tvi-\u003ei_ino = (unsigned long)na-\u003emft_no;\n 131:\t\n 132:\t\tif (na-\u003etype == AT_INDEX_ALLOCATION)\n 133:\t\t\tNInoSetMstProtected(ni);\n 134:\t\telse\n 135:\t\t\tni-\u003etype = na-\u003etype;\n 136:\t\n 137:\t\tni-\u003ename = na-\u003ename;\n 138:\t\tni-\u003ename_len = na-\u003ename_len;\n 139:\t\tni-\u003efolio = NULL;\n 140:\t\tatomic_set(\u0026ni-\u003ecount, 1);\n 141:\t\n 142:\t\t/* If initializing a normal inode, we are done. */\n 143:\t\tif (likely(na-\u003etype == AT_UNUSED))\n 144:\t\t\treturn 0;\n 145:\t\n 146:\t\t/* It is a fake inode. */\n 147:\t\tNInoSetAttr(ni);\n 148:\t\n 149:\t\t/*\n 150:\t\t * We have I30 global constant as an optimization as it is the name\n 151:\t\t * in \u003e99.9% of named attributes! The other \u003c0.1% incur a GFP_ATOMIC\n 152:\t\t * allocation but that is ok. And most attributes are unnamed anyway,\n 153:\t\t * thus the fraction of named attributes with name != I30 is actually\n 154:\t\t * absolutely tiny.\n 155:\t\t */\n 156:\t\tif (na-\u003ename_len \u0026\u0026 na-\u003ename != I30) {\n 157:\t\t\tunsigned int i;\n 158:\t\n 159:\t\t\ti = na-\u003ename_len * sizeof(__le16);\n 160:\t\t\tni-\u003ename = kmalloc(i + sizeof(__le16), GFP_ATOMIC);\n 161:\t\t\tif (!ni-\u003ename)\n 162:\t\t\t\treturn -ENOMEM;\n 163:\t\t\tmemcpy(ni-\u003ename, na-\u003ename, i);\n 164:\t\t\tni-\u003ename[na-\u003ename_len] = 0;\n 165:\t\t}\n 166:\t\treturn 0;\n 167:\t}\n 168:\t\n 169:\tstatic int ntfs_read_locked_inode(struct inode *vi);\n"
} |
| 23/3 | 2026/10/11 22:54 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 75813 output: 36 thoughts: 1412 Thoughts: **Analyzing NTFS Stream Name Handling** Okay, let's break this down. First, I'm examining how the NTFS driver handles named streams within `ntfs_read_locked_attr_inode`. I see a conditional block where, if `ni->name` equals `I30`, it allocates memory using `kmalloc_array`, copies the stream name, and null-terminates it. Otherwise, it assumes `ni->name` was already allocated (likely in `ntfs_init_locked_inode`), and proceeds with the copy and null-termination. Crucially, all bytes seem initialized. Good. But wait, I need to verify how these names are validated. I have to look at `ntfs_check_stream_name`. Okay, it checks for valid UTF-16 surrogates and forbidden characters like nulls, forward slashes, backslashes, and colons. Seems pretty standard for stream name validation. Now I need to connect the dots. I'm going to move on to `ntfs_stream_path_parse`. I see it's responsible for parsing a file name to separate the base file name from the stream name, delineated by a colon. It checks for a colon and ensures that it's not at the beginning or end of the name and that there is only one colon. `ntfs_stream_path_check` calls this parsing function, but doesn't *use* the parsed information if the parse is successful (it returns -EOPNOTSUPP, and the filled path is ignored). Seems odd, but maybe it's checking for the existence of streams. Now let's check `ntfs_stream_path_names`. It also calls `ntfs_stream_path_parse`, but this time it *does* use the parsed `path` structure. `ntfs_nlstoucs` is called, likely to convert the lengths to UTF-16 code units. No uninitialized reads here, because the `path` struct is only used if `ntfs_stream_path_parse` returns a positive value. Finally, I'll check `ntfs_filldir`. If the volume supports streams and a colon is present in the filename, the function simply skips the entry. The critical question is, *is* `name` null-terminated in this context? Let's check the surrounding code in `fs/ntfs/dir.c` around line 624. If it's not null-terminated, that `strchr` call could be a problem, which is what I'm looking for. |
| 24/3 | 2026/10/11 22:54 | tool | read-file |
0mArgs:{
"File": "fs/ntfs/dir.c",
"FirstLine": 615,
"LineCount": 40
}Results: {
"Contents": " 615:\t\t\tntfs_debug(\"Skipping root directory self reference entry.\");\n 616:\t\t\treturn 0;\n 617:\t\t}\n 618:\t\tif (MREF_LE(ie-\u003edata.dir.indexed_file) \u003c FILE_first_user \u0026\u0026\n 619:\t\t\t\t!NVolShowSystemFiles(vol)) {\n 620:\t\t\tntfs_debug(\"Skipping system file.\");\n 621:\t\t\treturn 0;\n 622:\t\t}\n 623:\t\tif (!NVolShowHiddenFiles(vol) \u0026\u0026\n 624:\t\t (ie-\u003ekey.file_name.file_attributes \u0026 FILE_ATTR_HIDDEN)) {\n 625:\t\t\tntfs_debug(\"Skipping hidden file.\");\n 626:\t\t\treturn 0;\n 627:\t\t}\n 628:\t\tname_len = ntfs_ucstonls(vol, (__le16 *)\u0026ie-\u003ekey.file_name.file_name,\n 629:\t\t\t\tie-\u003ekey.file_name.file_name_length, \u0026name,\n 630:\t\t\t\tNTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1);\n 631:\t\tif (name_len \u003c= 0) {\n 632:\t\t\tntfs_warning(vol-\u003esb, \"Skipping unrepresentable inode 0x%llx.\",\n 633:\t\t\t\t\t(long long)MREF_LE(ie-\u003edata.dir.indexed_file));\n 634:\t\t\treturn 0;\n 635:\t\t}\n 636:\t\tif (NVolStreamsWindows(vol) \u0026\u0026 strchr((char *)name, ':')) {\n 637:\t\t\tntfs_debug(\"Skipping file name containing a stream separator.\");\n 638:\t\t\treturn 0;\n 639:\t\t}\n 640:\t\tmref = MREF_LE(ie-\u003edata.dir.indexed_file);\n 641:\t\tif (ie-\u003ekey.file_name.file_attributes \u0026 FILE_ATTR_REPARSE_POINT)\n 642:\t\t\tdt_type = ntfs_reparse_tag_dt_types(vol, mref);\n 643:\t\telse if (ie-\u003ekey.file_name.file_attributes \u0026\n 644:\t\t\t\tFILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT)\n 645:\t\t\tdt_type = DT_DIR;\n 646:\t\telse\n 647:\t\t\tdt_type = DT_REG;\n 648:\t\n 649:\t\t/*\n 650:\t\t * Drop the page lock otherwise we deadlock with NFS when it calls\n 651:\t\t * -\u003elookup since ntfs_lookup() will lock the same page.\n 652:\t\t */\n 653:\t\tif (ia_page)\n 654:\t\t\tunlock_page(ia_page);\n"
} |
| 25/3 | 2026/10/11 22:54 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 78540 output: 33 thoughts: 23 Thoughts: Here's my thought process, as if I were considering this myself: **Investigating String Termination in `ntfs_ucstonls`** Okay, so I'm looking at `ntfs_ucstonls` and I need to quickly determine something crucial: Does this function properly null-terminate the converted string? That's the first thing that jumps to mind because a missing null terminator can lead to all sorts of buffer overflows and nasty issues down the line, especially in the context of file system interactions. I need to be absolutely sure about its behavior here. A quick check of the source or documentation is definitely in order; I'll start by diving into the details and confirming its string handling procedures. |
| 26/3 | 2026/10/11 22:54 | tool | grepper |
0mArgs:{
"Expression": "int ntfs_ucstonls",
"PathPrefixes": [
"fs/ntfs/"
]
}Results: {
"Output": "fs/ntfs/ntfs.h=247=int ntfs_nlstoucs(const struct ntfs_volume *vol, const char *ins,\nfs/ntfs/ntfs.h-248-\t\tconst int ins_len, __le16 **outs, int max_name_len);\nfs/ntfs/ntfs.h:249:int ntfs_ucstonls(const struct ntfs_volume *vol, const __le16 *ins,\nfs/ntfs/ntfs.h-250-\t\tconst int ins_len, unsigned char **outs, int outs_len);\n--\nfs/ntfs/unistr.c=239=int ntfs_nlstoucs(const struct ntfs_volume *vol, const char *ins,\n--\nfs/ntfs/unistr.c-330- */\nfs/ntfs/unistr.c:331:int ntfs_ucstonls(const struct ntfs_volume *vol, const __le16 *ins,\nfs/ntfs/unistr.c-332-\t\tconst int ins_len, unsigned char **outs, int outs_len)\n"
} |
| 27/3 | 2026/10/11 22:54 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 79032 output: 37 thoughts: 24 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Diving into `ntfs_ucstonls`** Right, let's get down to it. I need to understand how the NTFS driver handles Unicode to local string conversions, specifically within the `fs/ntfs/unistr.c` file. I'm going to focus on the `ntfs_ucstonls` function. I know this is a critical area for ensuring file names, attribute data, and other metadata are correctly translated between the NTFS filesystem's internal representation (likely UTF-16 or UTF-8, need to confirm) and the kernel's local string format. I'll be looking for things like: how it handles character mapping, error conditions, the use of codepages if any, and importantly, any potential vulnerabilities around buffer overflows or incorrect character handling that could lead to security issues or data corruption. I want to see how robust the implementation is, considering the potential for malformed Unicode data coming from an external source. Let's see how they've handled this. |
| 28/3 | 2026/10/11 22:54 | tool | read-file |
0mArgs:{
"File": "fs/ntfs/unistr.c",
"FirstLine": 330,
"LineCount": 80
}Results: {
"Contents": " 330:\t */\n 331:\tint ntfs_ucstonls(const struct ntfs_volume *vol, const __le16 *ins,\n 332:\t\t\tconst int ins_len, unsigned char **outs, int outs_len)\n 333:\t{\n 334:\t\tstruct nls_table *nls = vol-\u003enls_map;\n 335:\t\tunsigned char *ns;\n 336:\t\tint i, o, ns_len, wc;\n 337:\t\n 338:\t\t/* We don't trust outside sources. */\n 339:\t\tif (ins) {\n 340:\t\t\tns = *outs;\n 341:\t\t\tns_len = outs_len;\n 342:\t\t\tif (ns \u0026\u0026 !ns_len) {\n 343:\t\t\t\twc = -ENAMETOOLONG;\n 344:\t\t\t\tgoto conversion_err;\n 345:\t\t\t}\n 346:\t\t\tif (!ns) {\n 347:\t\t\t\tns_len = ins_len * NLS_MAX_CHARSET_SIZE;\n 348:\t\t\t\tns = kmalloc(ns_len + 1, GFP_NOFS);\n 349:\t\t\t\tif (!ns)\n 350:\t\t\t\t\tgoto mem_err_out;\n 351:\t\t\t}\n 352:\t\n 353:\t\t\tif (vol-\u003enls_utf8) {\n 354:\t\t\t\to = utf16s_to_utf8s((const wchar_t *)ins, ins_len,\n 355:\t\t\t\t\t\tUTF16_LITTLE_ENDIAN, ns, ns_len);\n 356:\t\t\t\tif (o \u003e= ns_len) {\n 357:\t\t\t\t\twc = -ENAMETOOLONG;\n 358:\t\t\t\t\tgoto conversion_err;\n 359:\t\t\t\t}\n 360:\t\t\t\tgoto done;\n 361:\t\t\t}\n 362:\t\n 363:\t\t\tfor (i = o = 0; i \u003c ins_len; i++) {\n 364:\tretry:\n 365:\t\t\t\twc = nls-\u003euni2char(le16_to_cpu(ins[i]), ns + o,\n 366:\t\t\t\t\t\tns_len - o);\n 367:\t\t\t\tif (wc \u003e 0) {\n 368:\t\t\t\t\to += wc;\n 369:\t\t\t\t\tcontinue;\n 370:\t\t\t\t} else if (!wc)\n 371:\t\t\t\t\tbreak;\n 372:\t\t\t\telse if (wc == -ENAMETOOLONG \u0026\u0026 ns != *outs) {\n 373:\t\t\t\t\tunsigned char *tc;\n 374:\t\t\t\t\t/* Grow in multiples of 64 bytes. */\n 375:\t\t\t\t\ttc = kmalloc((ns_len + 64) \u0026\n 376:\t\t\t\t\t\t\t~63, GFP_NOFS);\n 377:\t\t\t\t\tif (tc) {\n 378:\t\t\t\t\t\tmemcpy(tc, ns, ns_len);\n 379:\t\t\t\t\t\tns_len = ((ns_len + 64) \u0026 ~63) - 1;\n 380:\t\t\t\t\t\tkfree(ns);\n 381:\t\t\t\t\t\tns = tc;\n 382:\t\t\t\t\t\tgoto retry;\n 383:\t\t\t\t\t} /* No memory so goto conversion_error; */\n 384:\t\t\t\t} /* wc \u003c 0, real error. */\n 385:\t\t\t\tgoto conversion_err;\n 386:\t\t\t}\n 387:\tdone:\n 388:\t\t\tns[o] = 0;\n 389:\t\t\t*outs = ns;\n 390:\t\t\treturn o;\n 391:\t\t} /* else (!ins) */\n 392:\t\tntfs_error(vol-\u003esb, \"Received NULL pointer.\");\n 393:\t\treturn -EINVAL;\n 394:\tconversion_err:\n 395:\t\tntfs_error(vol-\u003esb,\n 396:\t\t\t\"Unicode name contains characters that cannot be converted to character set %s. You might want to try to use the mount option nls=utf8.\",\n 397:\t\t\tnls-\u003echarset);\n 398:\t\tif (ns != *outs)\n 399:\t\t\tkfree(ns);\n 400:\t\tif (wc != -ENAMETOOLONG)\n 401:\t\t\twc = -EILSEQ;\n 402:\t\treturn wc;\n 403:\tmem_err_out:\n 404:\t\tntfs_error(vol-\u003esb, \"Failed to allocate name!\");\n 405:\t\treturn -ENOMEM;\n 406:\t}\n 407:\t\n 408:\t/*\n 409:\t * ntfs_ucsnlen - determine the length of a little endian Unicode string\n"
} |
| 29/3 | 2026/10/11 22:54 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 80535 output: 35 thoughts: 3840 Thoughts: **Analysis of NTFS IOCTL Data Copying** Okay, let's break this down systematically. My primary concern is ensuring no uninitialized memory is leaked to userspace via the NTFS driver's IOCTLs. First, the `ns[o] = 0;` null termination is key for string handling, but let's go deeper. The critical path is how the `sn` buffer is handled in `ntfs_ioctl_list_streams` during stream listing. The `ntfs_ucstonls` function initially allocates a buffer (`sn`) if `*outs` is null, and then its contents will be copied to `entry->name`. This `sn` buffer will be `kfree`'d right after being copied, which is good. The `entry` structure resides within `kbuf` which is `kvzalloc`'d. This immediately addresses any potential padding or uninitialized memory concerns *within* `kbuf`, because it's guaranteed zeroed. We must ensure that everything that is copied to userspace from this buffer is properly initialized. Now, let's turn to `NTFS_IOC_STREAM_READ`. Here, `req` is allocated with `kvmalloc`. The core of the issue is what's copied from userspace to `req`, and then what's copied back. The initial `copy_from_user` copies `sizeof(hdr) + hdr.name_len` bytes into `req`. Crucially, there's a check `req->name_len == hdr.name_len`, which means we know how many bytes from the userspace buffer are being read to initialize `req`. After reading, a potentially different number of bytes `ret` is copied into `req->buffer + req->name_len` by `ntfs_inode_attr_pread`. The key question is whether `ntfs_inode_attr_pread` initializes all the bytes it *writes*. Let's verify: 1. **Resident Attribute:** Data is copied directly from a mapped MFT record, which is disk. So a potential issue is if the MFT record on-disk contains uninitialized data. That's a different concern. 2. **Non-Resident Attribute:** In this scenario, we read data in folios. The critical points are error handling: if `read_mapping_folio` fails, it returns an error, and the function returns the error, which means *nothing* is copied back to userspace via `copy_to_user`. So, uninitialized data in the disk image is not a problem for the driver. The logic ensures that *only* data written by `ntfs_inode_attr_pread` or copied directly from userspace is copied back out. If `read_mapping_folio` fails, the `copy_to_user` is skipped. If `pos >= i_size_read(stream_vi)`, `ret = 0`, and nothing is copied into `req->buffer`, so no uninitialized data leaks. The `count` variable is also checked, so that we will never read beyond `isize`, and no uninitialized data from the disk is read into `req`. Next we look at `NTFS_IOC_STREAM_WRITE`. This IOCTL only copies `req->bytes_returned`, which is initialized, to userspace. Finally, we have `ntfs_ioctl_list_streams`. The `hdr` structure is *entirely* populated from user-provided data via `copy_from_user`. The use of `kvzalloc` for `kbuf` guarantees it is zeroed, and padding is taken care of by the `ALIGN` macro. The structure `ntfs_stream_entry` is copied by `copy_to_user`, and the structure fields are well defined. By checking every `copy_to_user` and `put_user` call, I've confirmed that there are no uninitialized memory leaks to userspace from the NTFS driver. All copies back to userspace are either directly from user-provided data or from buffers that have been initialized. Padding is zeroed by the allocation method and structure layout is well defined. |
| 30/3 | 2026/10/11 22:55 | tool | grepper |
0mArgs:{
"Expression": "copy_to_user|put_user",
"PathPrefixes": [
"fs/ntfs/"
]
}Results: {
"Output": "fs/ntfs/file.c=974=static int ntfs_ioctl_get_volume_label(struct file *filp, unsigned long arg)\n--\nfs/ntfs/file.c-991-\nfs/ntfs/file.c:992:\tif (copy_to_user(buf, label, len + 1))\nfs/ntfs/file.c-993-\t\treturn -EFAULT;\n--\nfs/ntfs/file.c=1021=static int ntfs_ioctl_fitrim(struct ntfs_volume *vol, unsigned long arg)\n--\nfs/ntfs/file.c-1050-\nfs/ntfs/file.c:1051:\tif (copy_to_user(user_range, \u0026range, sizeof(range)))\nfs/ntfs/file.c-1052-\t\treturn -EFAULT;\n--\nfs/ntfs/named_stream.c=1332=static long ntfs_ioctl_stream(struct file *filp, unsigned long arg,\n--\nfs/ntfs/named_stream.c-1524-\tif (op == NTFS_STREAM_IOCTL_READ) {\nfs/ntfs/named_stream.c:1525:\t\tif (copy_to_user((void __user *)arg, req,\nfs/ntfs/named_stream.c-1526-\t\t\t\tsizeof(*req) + req-\u003ename_len +\n--\nfs/ntfs/named_stream.c-1528-\t\t\terr = -EFAULT;\nfs/ntfs/named_stream.c:1529:\t} else if (copy_to_user((void __user *)arg +\nfs/ntfs/named_stream.c-1530-\t\t\toffsetof(struct ntfs_stream, bytes_returned),\n--\nfs/ntfs/named_stream.c=1597=int ntfs_ioctl_list_streams(struct file *filp, unsigned long arg)\n--\nfs/ntfs/named_stream.c-1783-\nfs/ntfs/named_stream.c:1784:\tif (!ret \u0026\u0026 kbuf \u0026\u0026 copy_to_user(ubuf, kbuf, hdr.bytes_returned)) {\nfs/ntfs/named_stream.c-1785-\t\tret = -EFAULT;\n--\nfs/ntfs/named_stream.c-1788-\nfs/ntfs/named_stream.c:1789:\tif (copy_to_user((void __user *)arg, \u0026hdr, sizeof(hdr)))\nfs/ntfs/named_stream.c-1790-\t\tret = -EFAULT;\n"
} |
| 31/3 | 2026/10/11 22:55 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 85959 output: 28 thoughts: 714 Thoughts: **Analyzing Potential Data Leakage in `ntfs_stream_getattr`** Okay, so I've identified all the `copy_to_user` calls, which is a good starting point. But I need to be thorough. I need to make sure I haven't missed any other interactions with user space, or any other potentially problematic ioctls. Aha, the `ntfs_stream_getattr` function! Let's dive in. Looking at the code, it calls `generic_fillattr` initially to populate the `stat` structure. Then various fields, such as size, blocks, blksize, btime, and attributes, are overwritten in the `stat` struct. Okay, standard stuff. It looks at the `request_mask` for `STATX_DIOALIGN`. If this flag is set, it checks if the file is compressed or encrypted. If *not* compressed or encrypted, it sets `dio_mem_align` and `dio_offset_align`. But here's where the potential issue arises: What happens if `NInoCompressed(ni) || NInoEncrypted(ni)` *is* true? The code doesn't explicitly initialize or clear those values in the `stat` struct. This means if we can somehow manipulate the file metadata to trigger a compressed or encrypted state, the values within `stat->dio_mem_align` and `stat->dio_offset_align` might retain potentially sensitive, or out-of-date, information from the previous invocation of `generic_fillattr`. We need to figure out what `generic_fillattr` does with `dio_mem_align` and `dio_offset_align` to determine if this is a real vulnerability, or simply a non-issue. I'm going to check `generic_fillattr` now. |
| 32/3 | 2026/10/11 22:55 | tool | grepper |
0mArgs:{
"Expression": "void generic_fillattr",
"PathPrefixes": [
"fs/"
]
}Results: {
"Output": "fs/stat.c=63=EXPORT_SYMBOL(fill_mg_cmtime);\n--\nfs/stat.c-81- */\nfs/stat.c:82:void generic_fillattr(struct mnt_idmap *idmap, u32 request_mask,\nfs/stat.c-83-\t\t struct inode *inode, struct kstat *stat)\n"
} |
| 33/3 | 2026/10/11 22:55 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 87190 output: 32 thoughts: 18 Thoughts: Okay, let's dive into this. Here's how I'd approach that snippet, as though it were *my* internal monologue, geared towards someone who already knows their way around a codebase: **Deep Dive into `fs/stat.c` - Checking `generic_fillattr`** Alright, time to get my hands dirty. I need to refresh my memory on how this part of the filesystem works, so I'm going to head straight into `fs/stat.c`. The task at hand involves `generic_fillattr`, and given the function name, I'm expecting this is probably the workhorse function responsible for actually populating the `struct kstat` or `struct stat` with file attributes when you call `stat()` (or related syscalls). My first thought is, is this the right place to start? I'd imagine that `generic_fillattr` is probably a core function used by a bunch of filesystems, which means it probably handles the majority of the non-filesystem-specific stat retrieval logic. I'll need to figure out what filesystems are going to use this, but for now I'm making sure to go to the heart of the matter - likely the common code. I need to confirm if it has direct ties to what's happening with the I/O operations from other filesystems. I'm focusing specifically on how it handles things like file sizes, timestamps, permissions, and device information. This is where the core logic of translating on-disk or in-memory file metadata into the user-visible `stat` information should reside. I want to see if there are any subtle bugs or performance issues that might be hiding within `generic_fillattr`. The function's implementation might rely on a complex interplay of different structures and system calls. Let's see how it's actually doing things. |
| 34/3 | 2026/10/11 22:55 | tool | read-file |
0mArgs:{
"File": "fs/stat.c",
"FirstLine": 80,
"LineCount": 40
}Results: {
"Contents": " 80:\t * performed on the raw inode simply pass @nop_mnt_idmap.\n 81:\t */\n 82:\tvoid generic_fillattr(struct mnt_idmap *idmap, u32 request_mask,\n 83:\t\t\t struct inode *inode, struct kstat *stat)\n 84:\t{\n 85:\t\tvfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);\n 86:\t\tvfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);\n 87:\t\n 88:\t\tstat-\u003edev = inode-\u003ei_sb-\u003es_dev;\n 89:\t\tstat-\u003eino = inode-\u003ei_ino;\n 90:\t\tstat-\u003emode = inode-\u003ei_mode;\n 91:\t\tstat-\u003enlink = inode-\u003ei_nlink;\n 92:\t\tstat-\u003euid = vfsuid_into_kuid(vfsuid);\n 93:\t\tstat-\u003egid = vfsgid_into_kgid(vfsgid);\n 94:\t\tstat-\u003erdev = inode-\u003ei_rdev;\n 95:\t\tstat-\u003esize = i_size_read(inode);\n 96:\t\tstat-\u003eatime = inode_get_atime(inode);\n 97:\t\n 98:\t\tif (is_mgtime(inode)) {\n 99:\t\t\tfill_mg_cmtime(stat, request_mask, inode);\n 100:\t\t} else {\n 101:\t\t\tstat-\u003ectime = inode_get_ctime(inode);\n 102:\t\t\tstat-\u003emtime = inode_get_mtime(inode);\n 103:\t\t}\n 104:\t\n 105:\t\tstat-\u003eblksize = i_blocksize(inode);\n 106:\t\tstat-\u003eblocks = inode-\u003ei_blocks;\n 107:\t\n 108:\t\tif ((request_mask \u0026 STATX_CHANGE_COOKIE) \u0026\u0026 IS_I_VERSION(inode)) {\n 109:\t\t\tstat-\u003eresult_mask |= STATX_CHANGE_COOKIE;\n 110:\t\t\tstat-\u003echange_cookie = inode_query_iversion(inode);\n 111:\t\t}\n 112:\t\n 113:\t}\n 114:\tEXPORT_SYMBOL(generic_fillattr);\n 115:\t\n 116:\t/**\n 117:\t * generic_fill_statx_attr - Fill in the statx attributes from the inode flags\n 118:\t * @inode:\tInode to use as the source\n 119:\t * @stat:\tWhere to fill in the attribute flags\n"
} |
| 35/3 | 2026/10/11 22:55 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 88305 output: 31 thoughts: 55 Thoughts: Okay, here's my interpretation of that thought process, from an expert's perspective: **Diving into the `getattr` Call Chain in the Virtual File System** Alright, I'm thinking about the flow of a `getattr` call within the VFS, specifically before we even hit the actual `getattr` implementation. I need to understand this whole dance, particularly when security is *not* involved. Let's start with `vfs_getattr_nosec`. The first thing that jumps out is that `memset(stat, 0, sizeof(*stat));` call. Essentially, we're zeroing out the `stat` buffer *before* any file system specific code gets its hands on it. Makes perfect sense from a security standpoint, as it avoids any potential information leaks. It's a fundamental part of the design, probably to ensure a clean slate and avoid inheriting garbage data. Now, my mind shifts to how `vfs_getattr_nosec` actually *uses* the `getattr` function. I need to refresh my memory on the interactions. I'll need to go through the code to trace the call. What are the arguments? What context is passed? What's the error handling? Understanding this call chain is critical for debugging any file system-related issue, or for making changes to the security model. I'll need to verify the exact method being called to ensure my mental model is correct, as I'll want to review the exact call signature. |
| 36/3 | 2026/10/11 22:55 | tool | grepper |
0mArgs:{
"Expression": "int vfs_getattr_nosec",
"PathPrefixes": [
"fs/"
]
}Results: {
"Output": "fs/stat.c=166=EXPORT_SYMBOL_GPL(generic_fill_statx_atomic_writes);\n--\nfs/stat.c-180- */\nfs/stat.c:181:int vfs_getattr_nosec(const struct path *path, struct kstat *stat,\nfs/stat.c-182-\t\t u32 request_mask, unsigned int query_flags)\n"
} |
| 37/3 | 2026/10/11 22:55 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 88816 output: 33 thoughts: 21 Thoughts: Okay, here's my interpretation of what's going on in those lines of `fs/stat.c`, tailored for a fellow expert: **Delving into stat.c: A Closer Look at the Filesystem's Core** Right, let's dive into those lines – 181 through 210 in `fs/stat.c`. I need to get a clear picture of how this section handles the core details of file metadata retrieval. Immediately, I'm going to be looking for how the system interacts with the underlying filesystem structures. I'm expecting this block to deal with the primary functions related to `stat()` or similar system calls. Given the context, I'm betting it's going to be something that takes a filesystem path and populates a `struct kstat` or some variant. I'll be focused on how it traverses the inodes, handles permissions, possibly deals with symlinks if present, and where it eventually accesses the on-disk metadata. I'm keen to see if it's doing any caching optimizations here too – I'd be surprised if it wasn't considering we're talking about something so fundamental. I want to know how it handles the different types of filesystems: Does it have hooks for various filesystem implementations, or does it abstract that layer away? Are there any filesystem-specific quirks being addressed in this region? I'm hoping to see well-defined error handling too, especially related to the usual suspects: invalid paths, permission denied errors, and possibly some details about how it handles situations like stale NFS handles. This section would be critical for any low-level debugging. |
| 38/3 | 2026/10/11 22:55 | tool | read-file |
0mArgs:{
"File": "fs/stat.c",
"FirstLine": 180,
"LineCount": 30
}Results: {
"Contents": " 180:\t */\n 181:\tint vfs_getattr_nosec(const struct path *path, struct kstat *stat,\n 182:\t\t\t u32 request_mask, unsigned int query_flags)\n 183:\t{\n 184:\t\tstruct mnt_idmap *idmap;\n 185:\t\tstruct inode *inode = d_backing_inode(path-\u003edentry);\n 186:\t\n 187:\t\tmemset(stat, 0, sizeof(*stat));\n 188:\t\tstat-\u003eresult_mask |= STATX_BASIC_STATS;\n 189:\t\tquery_flags \u0026= AT_STATX_SYNC_TYPE;\n 190:\t\n 191:\t\t/* allow the fs to override these if it really wants to */\n 192:\t\t/* SB_NOATIME means filesystem supplies dummy atime value */\n 193:\t\tif (inode-\u003ei_sb-\u003es_flags \u0026 SB_NOATIME)\n 194:\t\t\tstat-\u003eresult_mask \u0026= ~STATX_ATIME;\n 195:\t\n 196:\t\t/*\n 197:\t\t * Note: If you add another clause to set an attribute flag, please\n 198:\t\t * update attributes_mask below.\n 199:\t\t */\n 200:\t\tif (IS_AUTOMOUNT(inode))\n 201:\t\t\tstat-\u003eattributes |= STATX_ATTR_AUTOMOUNT;\n 202:\t\n 203:\t\tif (IS_DAX(inode))\n 204:\t\t\tstat-\u003eattributes |= STATX_ATTR_DAX;\n 205:\t\n 206:\t\tstat-\u003eattributes_mask |= (STATX_ATTR_AUTOMOUNT |\n 207:\t\t\t\t\t STATX_ATTR_DAX);\n 208:\t\n 209:\t\tidmap = mnt_idmap(path-\u003emnt);\n"
} |
| 39/3 | 2026/10/11 22:55 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 89701 output: 37 thoughts: 238 Thoughts: **My Initial Analysis and Growing Concerns** Okay, so I see the code calls `memset(stat, 0, sizeof(*stat))` before `getattr`. Good, that eliminates the immediate worry of uninitialized memory in `stat`. But, wait a minute... I need to consider all the angles. What about potential issues within the stream and attribute creation routines? Specifically, I'm thinking about `ntfs_create_named_stream` and `ntfs_attr_add`. Let's dig in. I'll check out `ntfs_create_named_stream`. Okay, I see the call to `ntfs_attr_add` there: ```c err = ntfs_attr_add(base_ni, AT_DATA, stream_name, stream_len, NULL, 0); ``` Right, I'm trying to determine whether *newly* created streams or attributes are properly initialized. This `ntfs_attr_add` call looks relevant. Ah, now I'm starting to worry! It's calling `ntfs_attr_add` with `val` as `NULL` and `size` as `0`. That seems like an edge case. What is `ntfs_attr_add` doing when `val` is NULL and `size` is zero? I need to check the implementation of `ntfs_attr_add` now, specifically in `fs/ntfs/attrib.c`, to figure out if there's a potential vulnerability there. This might be a blind spot I missed initially. |
| 40/3 | 2026/10/11 22:55 | tool | grepper |
0mArgs:{
"Expression": "int ntfs_attr_add\\(",
"PathPrefixes": [
"fs/ntfs/attrib.c"
]
}Results: {
"Output": "fs/ntfs/attrib.c=2694=int ntfs_attr_record_rm(struct ntfs_attr_search_ctx *ctx)\n--\nfs/ntfs/attrib.c-2830- */\nfs/ntfs/attrib.c:2831:int ntfs_attr_add(struct ntfs_inode *ni, __le32 type,\nfs/ntfs/attrib.c-2832-\t\t__le16 *name, u8 name_len, u8 *val, s64 size)\n"
} |
| 41/3 | 2026/10/11 22:55 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 90453 output: 37 thoughts: 23 Thoughts: Okay, let's dive into this. Here's my take on understanding this `ntfs_attr_add` function in `fs/ntfs/attrib.c`: **Dissecting `ntfs_attr_add` in `fs/ntfs/attrib.c`** Alright, so the first step is always the same: let's get into the code and start tracing the logic. My goal is to grasp the core functionality of adding attributes within the NTFS filesystem context. I want to see how this function handles the complexities of metadata management within this specific file system, especially considering the various attribute types and how they are stored. I'm already anticipating the nuances of sector allocation, MFT entries, and probably some intricacies of the attribute list. I need to understand how it deals with fragmentation, both logically and physically, too. I'm hoping I can trace where it handles things like the resident versus nonresident data strategies. I'm looking for where it handles the various attribute flags such as compression, encryption, and which attribute types are handled and how. Knowing which error conditions are checked, and how they're handled is crucial to understanding the stability of the implementation. I'm going to start by looking for any comments that might explain what's going on; hopefully, they aren't all stale. I can also use my knowledge of the NTFS structure to help guide me. Let's dig in. |
| 42/3 | 2026/10/11 22:55 | tool | read-file |
0mArgs:{
"File": "fs/ntfs/attrib.c",
"FirstLine": 2830,
"LineCount": 70
}Results: {
"Contents": "2830:\t */\n2831:\tint ntfs_attr_add(struct ntfs_inode *ni, __le32 type,\n2832:\t\t\t__le16 *name, u8 name_len, u8 *val, s64 size)\n2833:\t{\n2834:\t\tstruct super_block *sb;\n2835:\t\tu32 attr_rec_size;\n2836:\t\tint err, i, offset;\n2837:\t\tbool is_resident;\n2838:\t\tbool can_be_non_resident = false;\n2839:\t\tstruct ntfs_inode *attr_ni;\n2840:\t\tstruct inode *attr_vi;\n2841:\t\tstruct mft_record *ni_mrec;\n2842:\t\n2843:\t\tif (!ni || size \u003c 0 || type == AT_ATTRIBUTE_LIST)\n2844:\t\t\treturn -EINVAL;\n2845:\t\n2846:\t\tntfs_debug(\"Entering for inode 0x%llx, attr %x, size %lld.\\n\",\n2847:\t\t\t\t(long long) ni-\u003emft_no, type, size);\n2848:\t\n2849:\t\tif (ni-\u003enr_extents == -1)\n2850:\t\t\tni = ni-\u003eext.base_ntfs_ino;\n2851:\t\n2852:\t\t/* Check the attribute type and the size. */\n2853:\t\terr = ntfs_attr_size_bounds_check(ni-\u003evol, type, size);\n2854:\t\tif (err) {\n2855:\t\t\tif (err == -ENOENT)\n2856:\t\t\t\terr = -EIO;\n2857:\t\t\treturn err;\n2858:\t\t}\n2859:\t\n2860:\t\tsb = ni-\u003evol-\u003esb;\n2861:\t\t/* Sanity checks for always resident attributes. */\n2862:\t\terr = ntfs_attr_can_be_non_resident(ni-\u003evol, type);\n2863:\t\tif (err) {\n2864:\t\t\tif (err != -EPERM) {\n2865:\t\t\t\tntfs_error(sb, \"ntfs_attr_can_be_non_resident failed\");\n2866:\t\t\t\tgoto err_out;\n2867:\t\t\t}\n2868:\t\t\t/* @val is mandatory. */\n2869:\t\t\tif (!val) {\n2870:\t\t\t\tntfs_error(sb,\n2871:\t\t\t\t\t\"val is mandatory for always resident attributes\");\n2872:\t\t\t\treturn -EINVAL;\n2873:\t\t\t}\n2874:\t\t\tif (size \u003e ni-\u003evol-\u003emft_record_size) {\n2875:\t\t\t\tntfs_error(sb, \"Attribute is too big\");\n2876:\t\t\t\treturn -ERANGE;\n2877:\t\t\t}\n2878:\t\t} else\n2879:\t\t\tcan_be_non_resident = true;\n2880:\t\n2881:\t\t/*\n2882:\t\t * Determine resident or not will be new attribute. We add 8 to size in\n2883:\t\t * non resident case for mapping pairs.\n2884:\t\t */\n2885:\t\terr = ntfs_attr_can_be_resident(ni-\u003evol, type);\n2886:\t\tif (!err) {\n2887:\t\t\tis_resident = true;\n2888:\t\t} else {\n2889:\t\t\tif (err != -EPERM) {\n2890:\t\t\t\tntfs_error(sb, \"ntfs_attr_can_be_resident failed\");\n2891:\t\t\t\tgoto err_out;\n2892:\t\t\t}\n2893:\t\t\tis_resident = false;\n2894:\t\t}\n2895:\t\n2896:\t\t/* Calculate attribute record size. */\n2897:\t\tif (is_resident)\n2898:\t\t\tattr_rec_size = offsetof(struct attr_record, data.resident.reserved) +\n2899:\t\t\t\t1 +\n"
} |
| 43/3 | 2026/10/11 22:55 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 91869 output: 37 thoughts: 14 Thoughts: Here's a summary of my thinking, formatted as you requested: **Diving Deeper into `ntfs_attr_add`** Okay, time to drill down. I need to take a closer look at the `ntfs_attr_add` function. I have a hunch about what's going on, but I need to confirm it by examining the code. I'll need to figure out what specific attributes are being added, the context of the additions, and how it all relates to the overall file system structure. Let's see if this function is behaving as expected, or if there's an opportunity for optimization or correction. Let's get to it. |
| 44/3 | 2026/10/11 22:55 | tool | read-file |
0mArgs:{
"File": "fs/ntfs/attrib.c",
"FirstLine": 2900,
"LineCount": 60
}Results: {
"Contents": "2900:\t\t\t\t((name_len * sizeof(__le16) + 7) \u0026 ~7) +\n2901:\t\t\t\t((size + 7) \u0026 ~7);\n2902:\t\telse\n2903:\t\t\tattr_rec_size = offsetof(struct attr_record, data.non_resident.compressed_size) +\n2904:\t\t\t\t((name_len * sizeof(__le16) + 7) \u0026 ~7) + 8;\n2905:\t\n2906:\t\t/*\n2907:\t\t * If we have enough free space for the new attribute in the base MFT\n2908:\t\t * record, then add attribute to it.\n2909:\t\t */\n2910:\tretry:\n2911:\t\tni_mrec = map_mft_record(ni);\n2912:\t\tif (IS_ERR(ni_mrec)) {\n2913:\t\t\terr = -EIO;\n2914:\t\t\tgoto err_out;\n2915:\t\t}\n2916:\t\n2917:\t\tif (le32_to_cpu(ni_mrec-\u003ebytes_allocated) -\n2918:\t\t\t\tle32_to_cpu(ni_mrec-\u003ebytes_in_use) \u003e= attr_rec_size) {\n2919:\t\t\tattr_ni = ni;\n2920:\t\t\tunmap_mft_record(ni);\n2921:\t\t\tgoto add_attr_record;\n2922:\t\t}\n2923:\t\tunmap_mft_record(ni);\n2924:\t\n2925:\t\t/* Try to add to extent inodes. */\n2926:\t\terr = ntfs_inode_attach_all_extents(ni);\n2927:\t\tif (err) {\n2928:\t\t\tntfs_error(sb, \"Failed to attach all extents to inode\");\n2929:\t\t\tgoto err_out;\n2930:\t\t}\n2931:\t\n2932:\t\tfor (i = 0; i \u003c ni-\u003enr_extents; i++) {\n2933:\t\t\tattr_ni = ni-\u003eext.extent_ntfs_inos[i];\n2934:\t\t\tni_mrec = map_mft_record(attr_ni);\n2935:\t\t\tif (IS_ERR(ni_mrec)) {\n2936:\t\t\t\terr = -EIO;\n2937:\t\t\t\tgoto err_out;\n2938:\t\t\t}\n2939:\t\n2940:\t\t\tif (le32_to_cpu(ni_mrec-\u003ebytes_allocated) -\n2941:\t\t\t\t\tle32_to_cpu(ni_mrec-\u003ebytes_in_use) \u003e=\n2942:\t\t\t\t\tattr_rec_size) {\n2943:\t\t\t\tunmap_mft_record(attr_ni);\n2944:\t\t\t\tgoto add_attr_record;\n2945:\t\t\t}\n2946:\t\t\tunmap_mft_record(attr_ni);\n2947:\t\t}\n2948:\t\n2949:\t\t/* There is no extent that contain enough space for new attribute. */\n2950:\t\tif (!NInoAttrList(ni)) {\n2951:\t\t\t/* Add attribute list not present, add it and retry. */\n2952:\t\t\terr = ntfs_inode_add_attrlist(ni);\n2953:\t\t\tif (err) {\n2954:\t\t\t\tntfs_error(sb, \"Failed to add attribute list\");\n2955:\t\t\t\tgoto err_out;\n2956:\t\t\t}\n2957:\t\t\tgoto retry;\n2958:\t\t}\n2959:\t\n"
} |
| 45/3 | 2026/10/11 22:55 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 93013 output: 37 thoughts: 26 Thoughts: Okay, here's my take on those lines in `fs/ntfs/attrib.c`, presented as a first-person summary, keeping in mind my expert understanding of file systems: **Analyzing NTFS Attribute Handling (Lines 2960-3020)** Alright, let's dive into lines 2960-3020 within `fs/ntfs/attrib.c`. I need to refresh my memory on how NTFS handles file attributes within the file system code. This section likely deals with the core logic for either reading, writing, or manipulating file attributes - maybe both. I'm immediately looking for the central function call that's being made in this section. Is it a direct call to a low-level disk access function, or is there a wrapper or abstraction layer involved? Knowing this will indicate the level of abstraction we're dealing with. It's probably interacting directly with the MFT or a secondary index. What structures are being used here? Are we dealing with `ATTRIBUTE_RECORD_HEADER` structures, perhaps? I'll need to see how the attribute types (like data, filename, security descriptor, etc.) are being handled and what's the logic for determining their location within the MFT. I'll pay attention to any potential errors in MFT processing. I'm wondering if there's any important conditional logic here - any branching based on attribute type, size, or resident/non-resident status. Are there any checks for attribute validity or sanity checks on the MFT? Any potential for overflow or underflow conditions related to attribute size or offsets? And are there any data consistency checks, or perhaps error handling for data that is out of bounds or corrupt? I'll also be trying to spot any optimization strategies used, such as caching of frequently accessed attributes. I'm especially interested in how the code handles attributes that are resident versus non-resident. Are they using the `$DATA` attribute properly? Is there a clear separation of logic for resident vs. non-resident attributes? I need to understand how the allocation of data clusters is handled. Overall, my goal is to trace the flow of control and understand the fundamental operations involved in attribute access and modification within this section of the NTFS file system. |
| 46/3 | 2026/10/11 22:55 | tool | read-file |
0mArgs:{
"File": "fs/ntfs/attrib.c",
"FirstLine": 2960,
"LineCount": 60
}Results: {
"Contents": "2960:\t\tattr_ni = NULL;\n2961:\t\t/* Allocate new extent. */\n2962:\t\terr = ntfs_mft_record_alloc(ni-\u003evol, 0, \u0026attr_ni, ni, NULL, -1);\n2963:\t\tif (err) {\n2964:\t\t\tntfs_error(sb, \"Failed to allocate extent record\");\n2965:\t\t\tgoto err_out;\n2966:\t\t}\n2967:\t\tunmap_mft_record(attr_ni);\n2968:\t\n2969:\tadd_attr_record:\n2970:\t\tif (is_resident) {\n2971:\t\t\t/* Add resident attribute. */\n2972:\t\t\toffset = ntfs_resident_attr_record_add(attr_ni, type, name,\n2973:\t\t\t\t\tname_len, val, size, 0);\n2974:\t\t\tif (offset \u003c 0) {\n2975:\t\t\t\tif (offset == -ENOSPC \u0026\u0026 can_be_non_resident)\n2976:\t\t\t\t\tgoto add_non_resident;\n2977:\t\t\t\terr = offset;\n2978:\t\t\t\tntfs_error(sb, \"Failed to add resident attribute\");\n2979:\t\t\t\tgoto free_err_out;\n2980:\t\t\t}\n2981:\t\t\treturn 0;\n2982:\t\t}\n2983:\t\n2984:\tadd_non_resident:\n2985:\t\t/* Add non resident attribute. */\n2986:\t\toffset = ntfs_non_resident_attr_record_add(attr_ni, type, name,\n2987:\t\t\t\tname_len, 0, 8, 0);\n2988:\t\tif (offset \u003c 0) {\n2989:\t\t\terr = offset;\n2990:\t\t\tntfs_error(sb, \"Failed to add non resident attribute\");\n2991:\t\t\tgoto free_err_out;\n2992:\t\t}\n2993:\t\n2994:\t\t/* If @size == 0, we are done. */\n2995:\t\tif (!size)\n2996:\t\t\treturn 0;\n2997:\t\n2998:\t\t/* Open new attribute and resize it. */\n2999:\t\tattr_vi = ntfs_attr_iget(VFS_I(ni), type, name, name_len);\n3000:\t\tif (IS_ERR(attr_vi)) {\n3001:\t\t\terr = PTR_ERR(attr_vi);\n3002:\t\t\tntfs_error(sb, \"Failed to open just added attribute\");\n3003:\t\t\tgoto rm_attr_err_out;\n3004:\t\t}\n3005:\t\tattr_ni = NTFS_I(attr_vi);\n3006:\t\n3007:\t\t/* Resize and set attribute value. */\n3008:\t\tif (ntfs_attr_truncate(attr_ni, size) ||\n3009:\t\t\t(val \u0026\u0026 (ntfs_inode_attr_pwrite(attr_vi, 0, size, val, false) != size))) {\n3010:\t\t\terr = -EIO;\n3011:\t\t\tntfs_error(sb, \"Failed to initialize just added attribute\");\n3012:\t\t\tif (ntfs_attr_rm(attr_ni))\n3013:\t\t\t\tntfs_error(sb, \"Failed to remove just added attribute\");\n3014:\t\t\tiput(attr_vi);\n3015:\t\t\tgoto err_out;\n3016:\t\t}\n3017:\t\tiput(attr_vi);\n3018:\t\treturn 0;\n3019:\t\n"
} |
| 47/3 | 2026/10/11 22:55 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 94499 output: 40 thoughts: 16 Thoughts: Okay, let's dive into this. Here's my thought process on checking `ntfs_resident_attr_record_add`: **Analyzing `ntfs_resident_attr_record_add`** Alright, I'm thinking about `ntfs_resident_attr_record_add`. Specifically, I need to understand its role within the NTFS file system. I know its name implies it deals with adding attribute records within a resident attribute, which is a critical aspect of how NTFS stores metadata directly within the MFT entry. That's immediately interesting because it implies potential performance benefits (if it's properly implemented) by avoiding the overhead of separate allocation clusters for the attribute data. But, of course, the implications for space constraints are important, and that means looking at the MFT entry size limit. I immediately need to consider how this function interacts with the MFT entry's size limitations and the overall structure. It would be valuable to understand: * **Error Handling:** What's the error handling like? Does it correctly manage space allocation within the MFT entry? What are the failure modes (e.g., MFT full, fragmentation, etc.), and how are they handled? * **Data Structures:** What data structures are used, and how efficient are they for adding and managing attribute records within the resident space? How does it handle data alignment and potential padding? * **Concurrency:** Is this function thread-safe? How are locking mechanisms (if any) implemented to prevent race conditions during concurrent file modifications? * **Performance:** What is the performance impact of this function? When would it perform best, when worst? Are there any optimizations that could be made? * **Security:** Are there any potential security vulnerabilities, such as buffer overflows, associated with this function? I'm also curious about the specific circumstances where this function gets called. I would expect it to be used when creating small files or when metadata is appended to an existing resident attribute. Essentially, I need to connect it to the broader context of NTFS attribute management to understand it fully. |
| 48/3 | 2026/10/11 22:55 | tool | grepper |
0mArgs:{
"Expression": "int ntfs_resident_attr_record_add",
"PathPrefixes": [
"fs/ntfs/attrib.c"
]
}Results: {
"Output": "fs/ntfs/attrib.c=2386=static int ntfs_make_room_for_attr(struct mft_record *m, u8 *pos, u32 size)\n--\nfs/ntfs/attrib.c-2432- */\nfs/ntfs/attrib.c:2433:int ntfs_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,\nfs/ntfs/attrib.c-2434-\t\t__le16 *name, u8 name_len, u8 *val, u32 size,\n"
} |
| 49/3 | 2026/10/11 22:55 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 95146 output: 37 thoughts: 16 Thoughts: Okay, here's my attempt at summarizing the thought process, tailored for an expert audience, and written in the first person: **Diving into `ntfs_resident_attr_record_add`** Alright, let's dissect this `ntfs_resident_attr_record_add` function. Immediately, my mind starts tracing the usual pathways. I'm expecting this to be a core routine in managing the metadata of resident attributes within the NTFS file system – likely dealing with the fundamental mechanics of adding a new attribute record that resides directly within the MFT entry. I'm mentally mapping the likely parameters here. I'll bet we're passing it a pointer to the MFT entry itself, maybe some offset information to correctly position the new record, the attribute type, attribute name (if it's not a generic attribute), the data itself, and of course, crucial size information. This isn't just about dumping bits; it's about adhering to the precise NTFS on-disk structure. There's probably an intricate dance here around alignment, ensuring sufficient space, and, *critically*, maintaining the integrity of the MFT entry's header – especially the attribute list header that tells us which attributes are contained where. I'm anticipating checks for available space and, probably, a fragmentation check, to make sure this new record fits, or to trigger allocation of an alternate record if necessary. I'll be looking carefully for how it updates the resident attribute list – does it maintain an in-memory linked list, or is it more of a direct on-disk lookup? Also, are there any considerations for cluster alignment of the data that might be stored inside the attribute? I'm already anticipating possible memory copy operations. I need to make sure those operations have zero chance of overflowing into the memory of other attributes, because this is the kind of situation that can lead to subtle and catastrophic file system corruption. I'll be paying close attention to any error handling. This could be where bad things happen, right? Invalid input, insufficient space, corrupted MFT entry... This kind of function has to be robust. Is there some sort of error code returned? What information is logged if things go wrong? Any potential security implications? Can a malicious actor somehow inject an oversized attribute? This is one place to start. Finally, are there any potential race conditions if this routine is operating in a multi-threaded or multi-process environment. I need to make sure that these atomic operations are done in the right order. My goal now is to see how this function handles those complexities and how cleverly it does it, because this code is the bedrock of NTFS's ability to store data. |
| 50/3 | 2026/10/11 22:55 | tool | read-file |
0mArgs:{
"File": "fs/ntfs/attrib.c",
"FirstLine": 2480,
"LineCount": 70
}Results: {
"Contents": "2480:\t\tif (err != -ENOENT) {\n2481:\t\t\terr = -EIO;\n2482:\t\t\tgoto put_err_out;\n2483:\t\t}\n2484:\t\ta = ctx-\u003eattr;\n2485:\t\tm = ctx-\u003emrec;\n2486:\t\n2487:\t\t/* Make room for attribute. */\n2488:\t\tlength = offsetof(struct attr_record, data.resident.reserved) +\n2489:\t\t\t\t sizeof(a-\u003edata.resident.reserved) +\n2490:\t\t\t((name_len * sizeof(__le16) + 7) \u0026 ~7) +\n2491:\t\t\t((size + 7) \u0026 ~7);\n2492:\t\terr = ntfs_make_room_for_attr(ctx-\u003emrec, (u8 *) ctx-\u003eattr, length);\n2493:\t\tif (err) {\n2494:\t\t\tntfs_debug(\"Failed to make room for attribute.\\n\");\n2495:\t\t\tgoto put_err_out;\n2496:\t\t}\n2497:\t\n2498:\t\t/* Setup record fields. */\n2499:\t\toffset = ((u8 *)a - (u8 *)m);\n2500:\t\ta-\u003etype = type;\n2501:\t\ta-\u003elength = cpu_to_le32(length);\n2502:\t\ta-\u003enon_resident = 0;\n2503:\t\ta-\u003ename_length = name_len;\n2504:\t\ta-\u003ename_offset =\n2505:\t\t\tname_len ? cpu_to_le16((offsetof(struct attr_record, data.resident.reserved) +\n2506:\t\t\t\t\tsizeof(a-\u003edata.resident.reserved))) : cpu_to_le16(0);\n2507:\t\n2508:\t\ta-\u003eflags = flags;\n2509:\t\ta-\u003einstance = m-\u003enext_attr_instance;\n2510:\t\ta-\u003edata.resident.value_length = cpu_to_le32(size);\n2511:\t\ta-\u003edata.resident.value_offset = cpu_to_le16(length - ((size + 7) \u0026 ~7));\n2512:\t\tif (val)\n2513:\t\t\tmemcpy((u8 *)a + le16_to_cpu(a-\u003edata.resident.value_offset), val, size);\n2514:\t\telse\n2515:\t\t\tmemset((u8 *)a + le16_to_cpu(a-\u003edata.resident.value_offset), 0, size);\n2516:\t\tif (type == AT_FILE_NAME)\n2517:\t\t\ta-\u003edata.resident.flags = RESIDENT_ATTR_IS_INDEXED;\n2518:\t\telse\n2519:\t\t\ta-\u003edata.resident.flags = 0;\n2520:\t\tif (name_len)\n2521:\t\t\tmemcpy((u8 *)a + le16_to_cpu(a-\u003ename_offset),\n2522:\t\t\t\t\tname, sizeof(__le16) * name_len);\n2523:\t\tm-\u003enext_attr_instance =\n2524:\t\t\tcpu_to_le16((le16_to_cpu(m-\u003enext_attr_instance) + 1) \u0026 0xffff);\n2525:\t\tif (ni-\u003enr_extents == -1)\n2526:\t\t\tbase_ni = ni-\u003eext.base_ntfs_ino;\n2527:\t\telse\n2528:\t\t\tbase_ni = ni;\n2529:\t\tif (type != AT_ATTRIBUTE_LIST \u0026\u0026 NInoAttrList(base_ni)) {\n2530:\t\t\terr = ntfs_attrlist_entry_add(ni, a);\n2531:\t\t\tif (err) {\n2532:\t\t\t\tntfs_attr_record_resize(m, a, 0);\n2533:\t\t\t\tmark_mft_record_dirty(ctx-\u003entfs_ino);\n2534:\t\t\t\tntfs_debug(\"Failed add attribute entry to ATTRIBUTE_LIST.\\n\");\n2535:\t\t\t\tgoto put_err_out;\n2536:\t\t\t}\n2537:\t\t}\n2538:\t\tmark_mft_record_dirty(ni);\n2539:\t\tntfs_attr_put_search_ctx(ctx);\n2540:\t\treturn offset;\n2541:\tput_err_out:\n2542:\t\tntfs_attr_put_search_ctx(ctx);\n2543:\t\treturn err;\n2544:\t}\n2545:\t\n2546:\t/*\n2547:\t * ntfs_non_resident_attr_record_add - add extent of non-resident attribute\n2548:\t * @ni:\t\t\topened ntfs inode to which MFT record add attribute\n2549:\t * @type:\t\ttype of the new attribute extent\n"
} |
| 51/3 | 2026/10/11 22:55 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 97014 output: 37 thoughts: 643 Thoughts: **Analyzing NTFS Attribute Write Operations** Okay, let's break this down. My initial focus is on how data is written to NTFS attributes, specifically looking at the interaction between different functions. I'm starting with lines 2514-2515 which show `memcpy` and `memset` being used to write data, depending on whether `val` is NULL. This is a common pattern in file systems, to either copy provided data or zero-initialize a region. Now, I'm going to look at related functions. My next stop is `ntfs_make_room_for_attr` – this one clears or initializes space for the new data. That makes sense, ensuring the target area is in a known state before the write. Now I am going to check `ntfs_encode_fh` in `fs/ntfs/namei.c`. If it is a named stream, `-EOPNOTSUPP` is returned, otherwise it calls `generic_encode_ino32_fh`. This suggests that there is special handling for named streams, which is common in NTFS. Hold on, I need to check `ntfs_inode_attr_pwrite`, since I need to understand what happens during attribute writes in more detail. Looking at the code in `fs/ntfs/inode.c`, I see a conditional block that checks if it's a named stream. If so, there's a call to `ntfs_extend_initialized_size` and `ntfs_attr_fallocate` before calling `__ntfs_inode_non_resident_attr_pwrite`. This is interesting because the fallocate call suggests pre-allocation of space, but the extend call is what is really drawing my attention. I'm now going to go deeper into `ntfs_extend_initialized_size`. Let's look at `fs/ntfs/inode.c`, specifically `ntfs_extend_initialized_size`. I see that it uses `struct ntfs_inode *vi` (inode) and references `struct ntfs_inode *mrec_ni` (the base inode, which will be important). Also, variables for `old_init_size` and `clear_size` are present, which suggest that it's keeping track of size information, and, also clearing a specific region. I have to read the body of this to fully understand the logic, but the high-level picture is coming into view. |
| 52/3 | 2026/10/11 22:55 | tool | read-file |
0mArgs:{
"File": "fs/ntfs/inode.c",
"FirstLine": 2550,
"LineCount": 55
}Results: {
"Contents": "2550:\t\treturn 0;\n2551:\t}\n2552:\t\n2553:\tint ntfs_extend_initialized_size(struct inode *vi, const loff_t offset,\n2554:\t\t\t\t\t const loff_t new_size)\n2555:\t{\n2556:\t\tstruct ntfs_inode *ni = NTFS_I(vi);\n2557:\t\tstruct ntfs_inode *mrec_ni = ntfs_base_inode(ni);\n2558:\t\tloff_t old_init_size, clear_size;\n2559:\t\tunsigned long flags;\n2560:\t\tint err = 0;\n2561:\t\n2562:\t\tif (!NInoNonResident(ni))\n2563:\t\t\treturn -EINVAL;\n2564:\t\n2565:\t\tfilemap_invalidate_lock(vi-\u003ei_mapping);\n2566:\t\tread_lock_irqsave(\u0026ni-\u003esize_lock, flags);\n2567:\t\told_init_size = ni-\u003einitialized_size;\n2568:\t\tread_unlock_irqrestore(\u0026ni-\u003esize_lock, flags);\n2569:\t\n2570:\t\tif (old_init_size \u003e= new_size)\n2571:\t\t\tgoto out;\n2572:\t\n2573:\t\terr = ntfs_attr_map_whole_runlist(ni);\n2574:\t\tif (err)\n2575:\t\t\tgoto out;\n2576:\t\n2577:\t\tif (!NInoCompressed(ni)) {\n2578:\t\t\t/*\n2579:\t\t\t * Zeroing skips holes, so discard cached uninitialized bytes\n2580:\t\t\t * before making them visible to writes or shared mappings.\n2581:\t\t\t */\n2582:\t\t\tclear_size = max_t(loff_t, i_size_read(vi), new_size);\n2583:\t\t\ttruncate_pagecache_range(vi, old_init_size, clear_size - 1);\n2584:\t\n2585:\t\t\tif (old_init_size \u003c offset) {\n2586:\t\t\t\terr = iomap_zero_range(vi, old_init_size,\n2587:\t\t\t\t\t\t offset - old_init_size,\n2588:\t\t\t\t\t\t NULL, \u0026ntfs_seek_iomap_ops,\n2589:\t\t\t\t\t\t \u0026ntfs_iomap_folio_ops, NULL);\n2590:\t\t\t\tif (err)\n2591:\t\t\t\t\tgoto out;\n2592:\t\t\t}\n2593:\t\t}\n2594:\t\n2595:\t\tmutex_lock(\u0026mrec_ni-\u003emrec_lock);\n2596:\t\terr = ntfs_attr_set_initialized_size(ni, new_size);\n2597:\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n2598:\t\tif (err)\n2599:\t\t\ttruncate_setsize(vi, old_init_size);\n2600:\tout:\n2601:\t\tfilemap_invalidate_unlock(vi-\u003ei_mapping);\n2602:\t\treturn err;\n2603:\t}\n2604:\t\n"
} |
| 53/3 | 2026/10/11 22:55 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 99059 output: 41 thoughts: 710 Thoughts: **Analyzing NTFS Attribute Growth and Data Handling** Okay, let's break this down. My initial observation is focused on how NTFS handles expanding attribute sizes, particularly in the context of `iomap_zero_range` and potential data initialization. I'm seeing that `iomap_zero_range` is crucial for zeroing out a range of data between `old_init_size` and `offset` which is immediately followed by a call to `truncate_pagecache_range`. So far, so good, this aligns with my understanding of how we prevent information leaks and ensure data consistency during file modifications. My attention then shifted to `ntfs_attr_fallocate` and its role in this process. Digging deeper into `ntfs_attr_map_whole_runlist` and `ntfs_enlarge_attribute`, I realized something. When expanding regular attributes, it appears that `initialized_size` gets updated without a direct zeroing operation. This makes me a little uneasy. However, the picture becomes clearer when I examine `ntfs_inode_attr_pwrite`, specifically for *named streams*. Here, the code reveals a slightly different behavior. First, we call `ntfs_extend_initialized_size`, which is interesting because that eventually calls `iomap_zero_range` with the correct range: `(vi, old_init_size, offset - old_init_size)`. **This confirms that for named streams, the gap from old initialized size up to the new write position is explicitly zeroed before cluster allocation and data write!** After zeroing, `ntfs_attr_fallocate` is called to allocate the clusters, and then `__ntfs_inode_non_resident_attr_pwrite` does the actual writing from the buffer. This ensures that the newly allocated space is zeroed, preventing information leakage. Now, my curiosity is piqued about resident attributes, so let's check `__ntfs_inode_resident_attr_pwrite`. That should tell us the complete story for all attributes. |
| 54/3 | 2026/10/11 22:55 | tool | grepper |
0mArgs:{
"Expression": "__ntfs_inode_resident_attr_pwrite",
"PathPrefixes": [
"fs/ntfs/inode.c"
]
}Results: {
"Output": "fs/ntfs/inode.c=3784=static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,\n--\nfs/ntfs/inode.c-3839-\nfs/ntfs/inode.c:3840:static s64 __ntfs_inode_resident_attr_pwrite(struct inode *vi,\nfs/ntfs/inode.c-3841-\t\t\t\t\t s64 pos, s64 count, u8 *buf,\n--\nfs/ntfs/inode.c=4019=s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool sync)\n--\nfs/ntfs/inode.c-4064-\tif (!NInoNonResident(ni)) {\nfs/ntfs/inode.c:4065:\t\tret = __ntfs_inode_resident_attr_pwrite(vi, pos, count, buf, ctx);\nfs/ntfs/inode.c-4066-\t\tgoto out_ctx;\n"
} |
| 55/3 | 2026/10/11 22:55 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 100530 output: 37 thoughts: 18 Thoughts: Okay, here's my interpretation of what's going through my mind as I approach this function, assuming I'm already well-versed in file system internals, especially NTFS: **Analyzing `__ntfs_inode_resident_attr_pwrite`** Alright, let's dive into `__ntfs_inode_resident_attr_pwrite`. Just the name alone gives me a good starting point. "Resident" immediately signals that this function is dealing with attributes that are small enough to be stored directly within the inode itself, which is a key optimization for NTFS. "pwrite" implies we're looking at a partial write operation, a `pread`-like function but for writing. This is often where things can get interesting, and potentially tricky, especially considering the constraints of in-inode storage. My immediate questions are: What are the conditions that trigger this resident attribute storage? What happens when a write attempts to exceed the available space within the inode? How does this function handle potential fragmentation within the resident attribute data itself? Does it manage to avoid reading and writing the entire file's metadata? I'll be looking closely at how the function handles the offset and length arguments. I'm expecting some pretty careful boundary checks and potential re-writing of the inode itself if data needs to be moved around to accommodate the write. I need to understand how atomic these updates are to ensure data integrity during power failures or crashes. I'll also keep an eye out for how this interacts with the logging or journaling aspects of NTFS. Does it use the NTFS write-ahead logging (WAL) mechanism? And what level of locking is applied, and how do we ensure concurrent access to resident attributes are properly handled? The goal is to see how this function handles the complexities of partial writes to resident attributes while maintaining the all-important file system consistency and performance. I need to be sure it's doing it right! |
| 56/3 | 2026/10/11 22:56 | tool | read-file |
0mArgs:{
"File": "fs/ntfs/inode.c",
"FirstLine": 3840,
"LineCount": 60
}Results: {
"Contents": "3840:\tstatic s64 __ntfs_inode_resident_attr_pwrite(struct inode *vi,\n3841:\t\t\t\t\t\t s64 pos, s64 count, u8 *buf,\n3842:\t\t\t\t\t\t struct ntfs_attr_search_ctx *ctx)\n3843:\t{\n3844:\t\tstruct ntfs_inode *ni = NTFS_I(vi);\n3845:\t\tstruct folio *folio;\n3846:\t\tstruct address_space *mapping = vi-\u003ei_mapping;\n3847:\t\tu8 *addr;\n3848:\t\tint err = 0;\n3849:\t\n3850:\t\tWARN_ON(NInoNonResident(ni));\n3851:\t\tif (pos + count \u003e PAGE_SIZE) {\n3852:\t\t\tntfs_error(vi-\u003ei_sb, \"Out of write into resident attr %#x\", ni-\u003etype);\n3853:\t\t\treturn -EINVAL;\n3854:\t\t}\n3855:\t\n3856:\t\t/* Copy to mft record page */\n3857:\t\taddr = (u8 *)ctx-\u003eattr + le16_to_cpu(ctx-\u003eattr-\u003edata.resident.value_offset);\n3858:\t\tmemcpy(addr + pos, buf, count);\n3859:\t\tmark_mft_record_dirty(ctx-\u003entfs_ino);\n3860:\t\n3861:\t\t/* Keep the first page clean and uptodate */\n3862:\t\tfolio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN | FGP_NOFS,\n3863:\t\t\t\t\t mapping_gfp_mask(mapping));\n3864:\t\tif (IS_ERR(folio)) {\n3865:\t\t\terr = PTR_ERR(folio);\n3866:\t\t\tntfs_error(vi-\u003ei_sb, \"Failed to read a page 0 for attr %#x: %d\",\n3867:\t\t\t\t ni-\u003etype, err);\n3868:\t\t\tgoto out;\n3869:\t\t}\n3870:\t\tif (!folio_test_uptodate(folio))\n3871:\t\t\tfolio_fill_tail(folio, 0, addr,\n3872:\t\t\t\t\tle32_to_cpu(ctx-\u003eattr-\u003edata.resident.value_length));\n3873:\t\telse\n3874:\t\t\tmemcpy_to_folio(folio, offset_in_folio(folio, pos), buf, count);\n3875:\t\tfolio_mark_uptodate(folio);\n3876:\t\tfolio_unlock(folio);\n3877:\t\tfolio_put(folio);\n3878:\tout:\n3879:\t\treturn err ? err : count;\n3880:\t}\n3881:\t\n3882:\tstatic s64 __ntfs_inode_non_resident_attr_pwrite(struct inode *vi,\n3883:\t\t\t\t\t\t\t s64 pos, s64 count, u8 *buf,\n3884:\t\t\t\t\t\t\t bool sync)\n3885:\t{\n3886:\t\tstruct ntfs_inode *ni = NTFS_I(vi);\n3887:\t\tstruct address_space *mapping = vi-\u003ei_mapping;\n3888:\t\tstruct folio *folio;\n3889:\t\tpgoff_t index;\n3890:\t\tunsigned long offset, length;\n3891:\t\tsize_t attr_len;\n3892:\t\ts64 ret = 0, written = 0;\n3893:\t\n3894:\t\tWARN_ON(!NInoNonResident(ni));\n3895:\t\n3896:\t\tindex = pos \u003e\u003e PAGE_SHIFT;\n3897:\t\twhile (count) {\n3898:\t\t\tif (count == PAGE_SIZE \u0026\u0026 !offset_in_page(pos)) {\n3899:\t\t\t\tfolio = __filemap_get_folio(vi-\u003ei_mapping, index,\n"
} |
| 57/3 | 2026/10/11 22:56 | llm | kmsan-evaluator |
1mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 102000 output: 37 thoughts: 46 Thoughts: Here's my thought process, as if I were considering this issue: **Delving into Attribute Enlargement in NTFS** Okay, hold on a second! I'm thinking about the logic for a resident attribute, and a particular edge case just popped into my head. What if the current position (`pos`) within the attribute's data exceeds the original length (`old_value_length`)? We have to ensure our bounds checking is rock solid. Right, let me double-check that. Now, let's take a look at how this is handled, and I believe that is `ntfs_enlarge_attribute`. I need to review its logic closely. Let's dig into the details and find the code for `ntfs_enlarge_attribute`. |
| 58/3 | 2026/10/11 22:57 | tool | read-file |
0mArgs:{
"File": "fs/ntfs/inode.c",
"FirstLine": 3780,
"LineCount": 55
}Results: {
"Contents": "3780:\t\t\tntfs_stream_put(vi);\n3781:\t\treturn err ? (s64)err : total;\n3782:\t}\n3783:\t\n3784:\tstatic inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,\n3785:\t\t\t\t\t\t struct ntfs_attr_search_ctx *ctx)\n3786:\t{\n3787:\t\tstruct ntfs_inode *ni = NTFS_I(vi);\n3788:\t\tstruct super_block *sb = vi-\u003ei_sb;\n3789:\t\tint ret;\n3790:\t\n3791:\t\tif (pos + count \u003c= ni-\u003einitialized_size)\n3792:\t\t\treturn 0;\n3793:\t\n3794:\t\tif (NInoEncrypted(ni) \u0026\u0026 NInoNonResident(ni))\n3795:\t\t\treturn -EACCES;\n3796:\t\n3797:\t\tif (NInoCompressed(ni))\n3798:\t\t\treturn -EOPNOTSUPP;\n3799:\t\n3800:\t\tif (pos + count \u003e ni-\u003edata_size) {\n3801:\t\t\tret = ntfs_attr_truncate(ni, pos + count);\n3802:\t\t\tif (ret) {\n3803:\t\t\t\tntfs_debug(\"Failed to truncate attribute\");\n3804:\t\t\t\treturn ret;\n3805:\t\t\t}\n3806:\t\n3807:\t\t\tntfs_attr_reinit_search_ctx(ctx);\n3808:\t\t\tret = ntfs_attr_lookup(ni-\u003etype, ni-\u003ename, ni-\u003ename_len,\n3809:\t\t\t\t\t CASE_SENSITIVE,\n3810:\t\t\t\t\t 0, NULL, 0, ctx);\n3811:\t\t\tif (ret) {\n3812:\t\t\t\tntfs_error(sb, \"Failed to look up attr %#x\", ni-\u003etype);\n3813:\t\t\t\treturn ret;\n3814:\t\t\t}\n3815:\t\t}\n3816:\t\n3817:\t\tif (!NInoNonResident(ni)) {\n3818:\t\t\tif (likely(i_size_read(vi) \u003c ni-\u003edata_size))\n3819:\t\t\t\ti_size_write(vi, ni-\u003edata_size);\n3820:\t\t\treturn 0;\n3821:\t\t}\n3822:\t\n3823:\t\t/* Named streams initialize gaps after dropping the MFT record lock. */\n3824:\t\tif (ntfs_inode_is_named_stream(ni)) {\n3825:\t\t\tif (i_size_read(vi) \u003c ni-\u003edata_size)\n3826:\t\t\t\ti_size_write(vi, ni-\u003edata_size);\n3827:\t\t\treturn 0;\n3828:\t\t}\n3829:\t\n3830:\t\tif (pos + count \u003e ni-\u003einitialized_size) {\n3831:\t\t\tctx-\u003eattr-\u003edata.non_resident.initialized_size = cpu_to_le64(pos + count);\n3832:\t\t\tmark_mft_record_dirty(ctx-\u003entfs_ino);\n3833:\t\t\tni-\u003einitialized_size = pos + count;\n3834:\t\t\tif (i_size_read(vi) \u003c ni-\u003einitialized_size)\n"
} |
| 59/3 | 2026/10/11 22:57 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 103120 output: 39 thoughts: 56 Thoughts: Okay, let's see. My mind's buzzing... I need to clarify something about NTFS attribute resizing. **NTFS Attribute Resizing: A Deep Dive** Hold on a second, what precisely *did* `ntfs_attr_truncate` do when it was enlarging a resident attribute? That's the question. I need to be absolutely certain on this. Let's delve into the code to confirm the behavior. I'm going to jump right into `fs/ntfs/attrib.c`. Specifically, I'll examine `ntfs_resident_attr_record_resize` or `ntfs_attr_truncate`— those are the likely candidates to provide the answer to my question. I need the specifics of the underlying process. |
| 60/3 | 2026/10/11 22:57 | tool | grepper |
0mError:no matches Args: {
"Expression": "ntfs_resident_attr_record_resize",
"PathPrefixes": [
"fs/ntfs/attrib.c"
]
}Results: {
"error": "no matches"
} |
| 61/3 | 2026/10/11 22:57 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 103388 output: 32 thoughts: 23 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Hunting Down `ntfs_attr_truncate`** Okay, so I need to get my hands dirty and find the `ntfs_attr_truncate` function. It's almost certain it resides within the NTFS file system code. Given my experience, the `fs/` directory is the obvious place to start. Specifically, I'm expecting to find NTFS-specific functionality grouped under `ntfs/`. Now, the name `ntfs_attr_truncate` strongly suggests it's related to attribute manipulation or truncation. Given that, `attrib.c` in the `ntfs` directory seems like the most probable location. Let's head there and see what we have. |
| 62/3 | 2026/10/11 22:57 | tool | grepper |
0mArgs:{
"Expression": "ntfs_attr_truncate",
"PathPrefixes": [
"fs/ntfs/"
]
}Results: {
"Output": "fs/ntfs/attrib.c=2831=int ntfs_attr_add(struct ntfs_inode *ni, __le32 type,\n--\nfs/ntfs/attrib.c-3007-\t/* Resize and set attribute value. */\nfs/ntfs/attrib.c:3008:\tif (ntfs_attr_truncate(attr_ni, size) ||\nfs/ntfs/attrib.c-3009-\t\t(val \u0026\u0026 (ntfs_inode_attr_pwrite(attr_vi, 0, size, val, false) != size))) {\n--\nfs/ntfs/attrib.c=4823=static int ntfs_resident_attr_resize(struct ntfs_inode *attr_ni, const s64 newsize,\n--\nfs/ntfs/attrib.c-5059-\nfs/ntfs/attrib.c:5060:int __ntfs_attr_truncate_vfs(struct ntfs_inode *ni, const s64 newsize,\nfs/ntfs/attrib.c-5061-\t\tconst s64 i_size)\n--\nfs/ntfs/attrib.c=5139=int ntfs_attr_expand(struct ntfs_inode *ni, const s64 newsize,\n--\nfs/ntfs/attrib.c-5145-/*\nfs/ntfs/attrib.c:5146: * ntfs_attr_truncate_i - resize an ntfs attribute\nfs/ntfs/attrib.c-5147- * @ni:\t\topen ntfs inode to resize\n--\nfs/ntfs/attrib.c-5156- */\nfs/ntfs/attrib.c:5157:int ntfs_attr_truncate_i_locked(struct ntfs_inode *ni, const s64 newsize,\nfs/ntfs/attrib.c-5158-\t\t\t\tunsigned int holes,\n--\nfs/ntfs/attrib.c-5205-\nfs/ntfs/attrib.c:5206:int ntfs_attr_truncate_i(struct ntfs_inode *ni, const s64 newsize,\nfs/ntfs/attrib.c-5207-\t\t\t unsigned int holes)\nfs/ntfs/attrib.c-5208-{\nfs/ntfs/attrib.c:5209:\treturn ntfs_attr_truncate_i_locked(ni, newsize, holes, NULL);\nfs/ntfs/attrib.c-5210-}\n--\nfs/ntfs/attrib.c-5214- */\nfs/ntfs/attrib.c:5215:int ntfs_attr_truncate(struct ntfs_inode *ni, const s64 newsize)\nfs/ntfs/attrib.c-5216-{\nfs/ntfs/attrib.c:5217:\treturn ntfs_attr_truncate_i(ni, newsize,\nfs/ntfs/attrib.c-5218-\t\t\t\t NVolDisableSparse(ni-\u003evol) ?\n--\nfs/ntfs/attrib.c=5836=int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bool keep_size)\n--\nfs/ntfs/attrib.c-5882-\tif (start + byte_len \u003e ni-\u003edata_size) {\nfs/ntfs/attrib.c:5883:\t\terr = ntfs_attr_truncate(ni, start + byte_len);\nfs/ntfs/attrib.c-5884-\t\tif (err)\n--\nfs/ntfs/attrib.h=111=int ntfs_non_resident_attr_punch_hole(struct ntfs_inode *ni, s64 start_vcn, s64 len);\nfs/ntfs/attrib.h:112:int __ntfs_attr_truncate_vfs(struct ntfs_inode *ni, const s64 newsize,\nfs/ntfs/attrib.h-113-\t\tconst s64 i_size);\n--\nfs/ntfs/attrib.h=115=int ntfs_attr_expand_locked(struct ntfs_inode *ni, const s64 newsize,\n--\nfs/ntfs/attrib.h-117-\t\t\t struct ntfs_inode *locked_ni);\nfs/ntfs/attrib.h:118:int ntfs_attr_truncate_i(struct ntfs_inode *ni, const s64 newsize, unsigned int holes);\nfs/ntfs/attrib.h:119:int ntfs_attr_truncate_i_locked(struct ntfs_inode *ni, const s64 newsize,\nfs/ntfs/attrib.h-120-\t\t\t\tunsigned int holes,\nfs/ntfs/attrib.h-121-\t\t\t struct ntfs_inode *locked_ni);\nfs/ntfs/attrib.h:122:int ntfs_attr_truncate(struct ntfs_inode *ni, const s64 newsize);\nfs/ntfs/attrib.h-123-int ntfs_attr_rm(struct ntfs_inode *ni);\n--\nfs/ntfs/attrlist.c=201=int ntfs_attrlist_update_locked(struct ntfs_inode *base_ni,\n--\nfs/ntfs/attrlist.c-229-\nfs/ntfs/attrlist.c:230:\terr = ntfs_attr_truncate_i_locked(\nfs/ntfs/attrlist.c-231-\t\t\tattr_ni, base_ni-\u003eattr_list_size, HOLES_NO, locked_ni);\n--\nfs/ntfs/attrlist.c-241-\nfs/ntfs/attrlist.c:242:\t\tretry_err = ntfs_attr_truncate_i_locked(\nfs/ntfs/attrlist.c-243-\t\t\t\tattr_ni, base_ni-\u003eattr_list_size,\n--\nfs/ntfs/ea.c=22=static int ntfs_write_ea(struct ntfs_inode *ni, __le32 type, char *value, s64 ea_off,\n--\nfs/ntfs/ea.c-39-\t\tif (need_truncate \u0026\u0026 ea_ni-\u003edata_size \u003e ea_off + ea_size)\nfs/ntfs/ea.c:40:\t\t\terr = ntfs_attr_truncate(ea_ni, ea_off + ea_size);\nfs/ntfs/ea.c-41-\t\tmark_mft_record_dirty(ni);\n--\nfs/ntfs/file.c=333=int ntfs_setattr_size(struct inode *vi, struct iattr *attr)\n--\nfs/ntfs/file.c-389-\t\tif (!err)\nfs/ntfs/file.c:390:\t\t\terr = __ntfs_attr_truncate_vfs(ni, attr-\u003eia_size,\nfs/ntfs/file.c-391-\t\t\t\t\told_size);\n--\nfs/ntfs/file.c=727=ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)\n--\nfs/ntfs/file.c-826-\t\t\tmutex_lock(\u0026mrec_ni-\u003emrec_lock);\nfs/ntfs/file.c:827:\t\t\tntfs_attr_truncate(ni, old_data_size);\nfs/ntfs/file.c-828-\t\t\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n--\nfs/ntfs/index.c=1037=static int ntfs_ibm_modify(struct ntfs_index_context *icx, s64 vcn, int set)\n--\nfs/ntfs/index.c-1058-\t\tif (bmp_ni-\u003edata_size \u003c bpos + 1) {\nfs/ntfs/index.c:1059:\t\t\tret = ntfs_attr_truncate(bmp_ni, (bmp_ni-\u003edata_size + 8) \u0026 ~7);\nfs/ntfs/index.c-1060-\t\t\tif (ret) {\n--\nfs/ntfs/index.c=1432=static int ntfs_ir_truncate(struct ntfs_index_context *icx, int data_size)\n--\nfs/ntfs/index.c-1448-\t */\nfs/ntfs/index.c:1449:\tret = ntfs_attr_truncate(icx-\u003eidx_ni, data_size + offsetof(struct index_root, index));\nfs/ntfs/index.c-1450-\tif (!ret) {\n--\nfs/ntfs/inode.c=2605=int ntfs_truncate_vfs(struct inode *vi, loff_t new_size, loff_t i_size)\n--\nfs/ntfs/inode.c-2611-\tmutex_lock(\u0026mrec_ni-\u003emrec_lock);\nfs/ntfs/inode.c:2612:\terr = __ntfs_attr_truncate_vfs(ni, new_size, i_size);\nfs/ntfs/inode.c-2613-\tmutex_unlock(\u0026mrec_ni-\u003emrec_lock);\n--\nfs/ntfs/inode.c=3784=static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,\n--\nfs/ntfs/inode.c-3800-\tif (pos + count \u003e ni-\u003edata_size) {\nfs/ntfs/inode.c:3801:\t\tret = ntfs_attr_truncate(ni, pos + count);\nfs/ntfs/inode.c-3802-\t\tif (ret) {\n"
} |
| 63/3 | 2026/10/11 22:57 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 106016 output: 37 thoughts: 14 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience, presented in the first person: **Let's Deep Dive into `ntfs_resident_attr_resize`** Alright, let's circle back to `ntfs_resident_attr_resize`. My initial focus is to pinpoint exactly what's driving this particular function. We're dealing with an NTFS resident attribute resize, which instantly makes me think of the potential implications on MFT entries and the overall filesystem structure. My immediate questions revolve around the triggers: What conditions necessitate a resident attribute resize? Is it data growth within the attribute itself, or are we dealing with fragmentation/space management issues within the MFT? I'm mentally running through the various scenarios: Is this driven by a file being created, modified, or deleted? Perhaps it's a metadata operation, or part of a background process. Understanding the context is crucial here. I need to quickly assess how efficiently it handles the re-allocation – does it implement any intelligent techniques to minimize fragmentation, such as pre-allocation or coalescing? Then there are the potential performance bottlenecks. I'm wondering about the locking mechanisms used – are they efficient enough to prevent contention during concurrent operations? What are the implications for recovery in case of an interruption? How does this function interact with other NTFS components, particularly the USN journal, and attribute indexing? Any potential corruption vulnerabilities immediately spring to mind. And finally, I'm thinking about the robustness of the implementation. Is this function well-tested under various load conditions, with different file sizes and attributes? Does it account for all edge cases, and does it implement proper error handling and logging? I want to ensure this isn't a point of weakness in the system. I need to scrutinize the code for any potential memory leaks, buffer overflows, or other vulnerabilities that might compromise the file system's integrity or performance. |
| 64/3 | 2026/10/11 22:57 | tool | read-file |
0mArgs:{
"File": "fs/ntfs/attrib.c",
"FirstLine": 4835,
"LineCount": 45
}Results: {
"Contents": "4835:\t\t\t\t(long long)newsize);\n4836:\t\n4837:\t\tif (NInoAttr(attr_ni))\n4838:\t\t\tbase_ni = attr_ni-\u003eext.base_ntfs_ino;\n4839:\t\telse\n4840:\t\t\tbase_ni = attr_ni;\n4841:\t\n4842:\t\t/* Get the attribute record that needs modification. */\n4843:\t\tctx = ntfs_attr_get_search_ctx(base_ni, NULL);\n4844:\t\tif (!ctx) {\n4845:\t\t\tntfs_error(sb, \"%s: Failed to get search context\", __func__);\n4846:\t\t\treturn -ENOMEM;\n4847:\t\t}\n4848:\t\n4849:\t\terr = ntfs_attr_lookup(attr_ni-\u003etype, attr_ni-\u003ename, attr_ni-\u003ename_len,\n4850:\t\t\t\tCASE_SENSITIVE, 0, NULL, 0, ctx);\n4851:\t\tif (err) {\n4852:\t\t\tntfs_error(sb, \"ntfs_attr_lookup failed\");\n4853:\t\t\tgoto put_err_out;\n4854:\t\t}\n4855:\t\n4856:\t\t/*\n4857:\t\t * Check the attribute type and the corresponding minimum and maximum\n4858:\t\t * sizes against @newsize and fail if @newsize is out of bounds.\n4859:\t\t */\n4860:\t\terr = ntfs_attr_size_bounds_check(vol, attr_ni-\u003etype, newsize);\n4861:\t\tif (err) {\n4862:\t\t\tif (err == -ENOENT)\n4863:\t\t\t\terr = -EIO;\n4864:\t\t\tntfs_debug(\"%s: bounds check failed\", __func__);\n4865:\t\t\tgoto put_err_out;\n4866:\t\t}\n4867:\t\t/*\n4868:\t\t * If @newsize is bigger than the mft record we need to make the\n4869:\t\t * attribute non-resident if the attribute type supports it. If it is\n4870:\t\t * smaller we can go ahead and attempt the resize.\n4871:\t\t */\n4872:\t\tif (newsize \u003c vol-\u003emft_record_size) {\n4873:\t\t\t/* Perform the resize of the attribute record. */\n4874:\t\t\terr = ntfs_resident_attr_value_resize(ctx-\u003emrec, ctx-\u003eattr,\n4875:\t\t\t\t\t\tnewsize);\n4876:\t\t\tif (!err) {\n4877:\t\t\t\t/* Update attribute size everywhere. */\n4878:\t\t\t\tattr_ni-\u003edata_size = attr_ni-\u003einitialized_size = newsize;\n4879:\t\t\t\tattr_ni-\u003eallocated_size = (newsize + 7) \u0026 ~7;\n"
} |
| 65/3 | 2026/10/11 22:57 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 107209 output: 36 thoughts: 16 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience, presented in the first person: **Analyzing `ntfs_resident_attr_value_resize`** Alright, let's dive into this `ntfs_resident_attr_value_resize` function. I need to refresh my memory on the intricacies of how resident attribute values are handled within the NTFS file system. I'm immediately thinking about the implications for data integrity and performance. Is this a common operation? What are the potential pitfalls? Specifically, I need to consider things like buffer overflows, race conditions (especially if multiple threads are modifying the same attribute), and the atomic nature of the operation itself. Are we dealing with in-place resizing, or is there a copy involved? If it's a copy, are there considerations for fragmentation of the MFT? Also, I have to be thinking about how it interacts with the index structures if the attribute is part of an index like `$INDEX_ALLOCATION`. The details will matter a great deal here. I'll need to scrutinize the code for any potential memory leaks or corruption that could arise from improper allocation or deallocation, and ensure it correctly handles edge cases, such as an attribute value shrinking to zero bytes, or expanding to a size that requires it to become non-resident. This is a crucial function; a bug here could easily lead to data loss or file system corruption, so a very close look is necessary. |
| 66/3 | 2026/10/11 22:57 | tool | grepper |
0mArgs:{
"Expression": "ntfs_resident_attr_value_resize",
"PathPrefixes": [
"fs/ntfs/"
]
}Results: {
"Output": "fs/ntfs/attrib.c=1887=int ntfs_attr_record_resize(struct mft_record *m, struct attr_record *a, u32 new_size)\n--\nfs/ntfs/attrib.c-1930-/*\nfs/ntfs/attrib.c:1931: * ntfs_resident_attr_value_resize - resize the value of a resident attribute\nfs/ntfs/attrib.c-1932- * @m:\t\tmft record containing attribute record\n--\nfs/ntfs/attrib.c-1938- */\nfs/ntfs/attrib.c:1939:int ntfs_resident_attr_value_resize(struct mft_record *m, struct attr_record *a,\nfs/ntfs/attrib.c-1940-\t\tconst u32 new_size)\n--\nfs/ntfs/attrib.c=4823=static int ntfs_resident_attr_resize(struct ntfs_inode *attr_ni, const s64 newsize,\n--\nfs/ntfs/attrib.c-4873-\t\t/* Perform the resize of the attribute record. */\nfs/ntfs/attrib.c:4874:\t\terr = ntfs_resident_attr_value_resize(ctx-\u003emrec, ctx-\u003eattr,\nfs/ntfs/attrib.c-4875-\t\t\t\t\tnewsize);\n--\nfs/ntfs/attrib.h=98=int ntfs_attr_record_resize(struct mft_record *m, struct attr_record *a, u32 new_size);\nfs/ntfs/attrib.h:99:int ntfs_resident_attr_value_resize(struct mft_record *m, struct attr_record *a,\nfs/ntfs/attrib.h-100-\t\tconst u32 new_size);\n--\nfs/ntfs/index.c=1280=static int ntfs_ir_reparent(struct ntfs_index_context *icx)\n--\nfs/ntfs/index.c-1341-\tif (ix_root_size \u003e old_value_length) {\nfs/ntfs/index.c:1342:\t\tret = ntfs_resident_attr_value_resize(ctx-\u003emrec, ctx-\u003eattr, ix_root_size);\nfs/ntfs/index.c-1343-\t\tif (ret)\n--\nfs/ntfs/index.c-1362-\tif (ix_root_size \u003c= old_value_length) {\nfs/ntfs/index.c:1363:\t\tret = ntfs_resident_attr_value_resize(ctx-\u003emrec, ctx-\u003eattr, ix_root_size);\nfs/ntfs/index.c-1364-\t\tif (ret)\n"
} |
| 67/3 | 2026/10/11 22:57 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 108256 output: 37 thoughts: 16 Thoughts: Okay, here's my interpretation of what's going on, formatted as you requested: **Deconstructing `ntfs_resident_attr_value_resize`** Alright, let's dive into this `ntfs_resident_attr_value_resize` function. I need to understand its purpose and how it operates within the context of the NTFS file system. My expertise tells me this likely has something to do with modifying the size of an attribute value *resident* within the Master File Table (MFT) entry itself, rather than pointing to an external data cluster. This is crucial for performance and understanding how the filesystem handles metadata. My immediate concerns are around potential edge cases. What happens if the resize operation overflows the available space within the MFT entry? Does it gracefully transition to using an external data run, or does it trigger an error? What are the implications for fragmentation? I'm also thinking about concurrency – how is this function protected against race conditions with other operations that might be modifying the same MFT entry? Does it implement locking mechanisms? What about atomicity? Does the operation complete as a single transaction? If not, what happens during a power failure in the middle of a resize? I'll be looking closely at the error handling. I want to see how it manages various potential errors, such as insufficient space in the MFT entry, disk I/O failures, or invalid attribute identifiers. The logging strategy will be important as well, in order to diagnose the errors. Finally, efficiency is always paramount. I'll need to assess the computational complexity and performance characteristics of this resize operation. Are there any optimizations in place to minimize disk I/O, particularly for small attribute sizes? I'll probably look for memory allocation patterns and potential bottlenecks that I might optimize in another environment. I'll need to know whether the operation involves multiple writes or if it leverages techniques to minimize writes, since those have great implications on performance. |
| 68/3 | 2026/10/11 22:57 | tool | read-file |
0mArgs:{
"File": "fs/ntfs/attrib.c",
"FirstLine": 1935,
"LineCount": 45
}Results: {
"Contents": "1935:\t *\n1936:\t * Resize the value of the attribute @a in the mft record @m to @new_size bytes.\n1937:\t * If the value is made bigger, the newly allocated space is cleared.\n1938:\t */\n1939:\tint ntfs_resident_attr_value_resize(struct mft_record *m, struct attr_record *a,\n1940:\t\t\tconst u32 new_size)\n1941:\t{\n1942:\t\tu32 old_size;\n1943:\t\n1944:\t\t/* Resize the resident part of the attribute record. */\n1945:\t\tif (ntfs_attr_record_resize(m, a,\n1946:\t\t\t\tle16_to_cpu(a-\u003edata.resident.value_offset) + new_size))\n1947:\t\t\treturn -ENOSPC;\n1948:\t\t/*\n1949:\t\t * The resize succeeded! If we made the attribute value bigger, clear\n1950:\t\t * the area between the old size and @new_size.\n1951:\t\t */\n1952:\t\told_size = le32_to_cpu(a-\u003edata.resident.value_length);\n1953:\t\tif (new_size \u003e old_size)\n1954:\t\t\tmemset((u8 *)a + le16_to_cpu(a-\u003edata.resident.value_offset) +\n1955:\t\t\t\t\told_size, 0, new_size - old_size);\n1956:\t\t/* Finally update the length of the attribute value. */\n1957:\t\ta-\u003edata.resident.value_length = cpu_to_le32(new_size);\n1958:\t\treturn 0;\n1959:\t}\n1960:\t\n1961:\t/*\n1962:\t * ntfs_attr_make_non_resident - convert a resident to a non-resident attribute\n1963:\t * @ni:\t\tntfs inode describing the attribute to convert\n1964:\t * @data_size:\tsize of the resident data to copy to the non-resident attribute\n1965:\t *\n1966:\t * Convert the resident ntfs attribute described by the ntfs inode @ni to a\n1967:\t * non-resident one.\n1968:\t *\n1969:\t * @data_size must be equal to the attribute value size. This is needed since\n1970:\t * we need to know the size before we can map the mft record and our callers\n1971:\t * always know it. The reason we cannot simply read the size from the vfs\n1972:\t * inode i_size is that this is not necessarily uptodate. This happens when\n1973:\t * ntfs_attr_make_non_resident() is called in the -\u003etruncate call path(s).\n1974:\t */\n1975:\tint ntfs_attr_make_non_resident(struct ntfs_inode *ni, const u32 data_size)\n1976:\t{\n1977:\t\ts64 new_size;\n1978:\t\tstruct inode *vi = VFS_I(ni);\n1979:\t\tstruct ntfs_volume *vol = ni-\u003evol;\n"
} |
| 69/3 | 2026/10/11 22:57 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 109557 output: 33 thoughts: 6781 Thoughts: **Named Streams in NTFS: A Security Review** Okay, so I've been diving deep into this NTFS named stream code. My initial thought process, as usual, centered on potential vulnerabilities, particularly those that could lead to uninitialized memory exposures. I've been through the code, looking at the initialization paths, data flows, and locking mechanisms, and the potential issues that could be introduced. I started by ensuring that resizes cleared the memory between the old size and new size. Then, I looked at how streams are finalized during removal, focusing on the `stream_open_count` and its initialization. I tracked down where this count is set and incremented, looking for potential race conditions or incorrect initialization. I paid close attention to where `ntfs_fsync_attr` uses `kmalloc_obj` with two members, and ensured the fields are fully initialized. I then dug into `ntfs_test_inode` and `ntfs_test_inode_rcu`, particularly the comparisons of inode names. I made sure the `ni->name` field is properly initialized during inode creation and is allocated correctly. And after this I looked in `ntfs_stream_unlinked`. I examined several functions such as `ntfs_file_modified` and confirmed that code properly handles named streams, and how `base_attr` is correctly copied. I went through numerous other functions. I then performed a careful check for uninitialized memory, ensuring no kernel structures are sent to user space with uninitialized or zeroed fields. I paid specific attention to the `ntfs_stream` structure in user space and ensured it is properly initialized and padded. I also scrutinized the initialization of the `ntfs_list_streams` structure and the memory allocated for the stream buffer using `kvzalloc`, which is zero-initialized. I made sure there is no padding that can leak into user space. I considered conditional logic that might depend on uninitialized variables and made sure all the variables were initialized before they are used. I then considered situations like memory allocation that might leave some memory uninitialized and I checked for situations like the `kmalloc_array` in `ntfs_read_locked_attr_inode` to ensure that all elements are initialized. And then I reviewed all the length checks and the logic to ensure that existing buffers aren't accessed. And then I started to check the security concerns introduced in the diff. I looked at the code paths related to `ntfs_resident_attr_record_add` and `ntfs_non_resident_attr_record_add`. I also noted the explicit comment in `ntfs_inode_attr_pwrite` about preventing the exposure of uninitialized data, showing the developer's intent and care in preventing this kind of vulnerability. After this, I re-reviewed the changes in iomap with the use of the `base_ni` in different places. I analyzed the file operations, especially `read_iter` and `write_iter` and made sure there were no security concerns. I noted that extended attributes are properly handled. I also checked functions such as `ntfs_show_options` and verified that they don't introduce any new concerns. And finally, I reviewed the inode cleanup and the `atomic_set` to make sure there are no new vulnerabilities. I've carefully considered the four points, and I am quite confident that the code is safe from the vulnerabilities they represent. |
| 70/3 | 2026/10/11 22:58 | tool | grepper |
0mArgs:{
"Expression": "__ntfs_init_inode",
"PathPrefixes": [
"fs/ntfs/"
]
}Results: {
"Output": "fs/ntfs/inode.c=488=static struct lock_class_key attr_list_rl_lock_class;\n--\nfs/ntfs/inode.c-490-/*\nfs/ntfs/inode.c:491: * __ntfs_init_inode - initialize ntfs specific part of an inode\nfs/ntfs/inode.c-492- * @sb:\t\tsuper block of mounted volume\n--\nfs/ntfs/inode.c-499- */\nfs/ntfs/inode.c:500:void __ntfs_init_inode(struct super_block *sb, struct ntfs_inode *ni)\nfs/ntfs/inode.c-501-{\n--\nfs/ntfs/inode.c=550=inline struct ntfs_inode *ntfs_new_extent_inode(struct super_block *sb,\n--\nfs/ntfs/inode.c-556-\tif (likely(ni != NULL)) {\nfs/ntfs/inode.c:557:\t\t__ntfs_init_inode(sb, ni);\nfs/ntfs/inode.c-558-\t\tlockdep_set_class(\u0026ni-\u003emrec_lock, \u0026extent_inode_mrec_lock_key);\n--\nfs/ntfs/inode.h=334=void ntfs_evict_big_inode(struct inode *vi);\nfs/ntfs/inode.h:335:void __ntfs_init_inode(struct super_block *sb, struct ntfs_inode *ni);\nfs/ntfs/inode.h-336-\nfs/ntfs/inode.h=337=static inline void ntfs_init_big_inode(struct inode *vi)\n--\nfs/ntfs/inode.h-341-\tntfs_debug(\"Entering.\");\nfs/ntfs/inode.h:342:\t__ntfs_init_inode(vi-\u003ei_sb, ni);\nfs/ntfs/inode.h-343-\tni-\u003emft_no = vi-\u003ei_ino;\n"
} |
| 71/3 | 2026/10/11 22:58 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 117612 output: 44 thoughts: 5343 Thoughts: **Analyzing NTFS Inode Initialization and Potential Uninitialized Memory Leaks** Okay, so I've been digging into the NTFS code, specifically focusing on inode initialization and related ioctl handlers, trying to ensure there aren't any sneaky uninitialized memory leaks. The initial review of `__ntfs_init_inode` confirms a basic initialization, but then, the `ntfs_ioctl_list_streams` code caught my eye. I needed to break it down. First, I examined `ntfs_ioctl_list_streams`. It looks like this code iterates through data attributes to build a list of streams. The key part is calculating required buffer sizes and copying data to user space. I confirmed that the `hdr` (header) structure, copied from user space, and the data buffer are handled in a way that *shouldn't* leak uninitialized memory. I paid close attention to the `ENOSPC` (No space left on device) error path and the scenario where the computed `offset` is smaller than the `required` size in a second pass, in the while loop. I am confident that these cases are handled correctly to prevent uninitialized data exposure. Next, I looked at the write path in `NTFS_STREAM_IOCTL_WRITE`, where I noticed the use of `vmemdup_user`. This function copies data from user space, and then there's a subsequent copy of `bytes_returned` back to user space. This seemed fine since `bytes_returned` is initialized and doesn't reveal any uninitialized kernel memory. Then, I shifted my focus to `NTFS_STREAM_IOCTL_READ`. The critical section here involves `kvmalloc` and `copy_to_user`. I meticulously went through the memory flow here, especially in relation to `ntfs_inode_attr_pread`. It was essential to make sure the bytes returned by this function, which gets copied to the user, are completely initialized. I scrutinized the path within `ntfs_inode_attr_pread`, considering both resident and non-resident attribute cases. I was especially careful to analyze the page cache reads involved when an attribute is non-resident to make sure no uninitialized page cache data could be leaked to user space. The address space operations take care of zeroing out the parts of the file that have not been written to yet in an NTFS volume. The code *appears* to handle these corner cases correctly. The last thing I checked was the potential for the inode's name not being initialized when reading attributes via the `ntfs_read_locked_attr_inode` function. It seems that the name field in the inode is properly initialized in all cases. This also holds true when we are using a named stream (i.e. not the default data stream in the file.) So, from what I can tell, I've got pretty good confidence in the code so far. |
| 72/3 | 2026/10/11 22:58 | tool | grepper |
0mArgs:{
"Expression": "kfree\\(ni-\u003ename\\)|kmem_cache_free.*ni-\u003ename",
"PathPrefixes": [
"fs/ntfs/"
]
}Results: {
"Output": "fs/ntfs/attrib.c=3294=void ntfs_attr_close(struct ntfs_inode *ni)\n--\nfs/ntfs/attrib.c-3299-\tif (ni-\u003ename != AT_UNNAMED \u0026\u0026 ni-\u003ename != I30)\nfs/ntfs/attrib.c:3300:\t\tkfree(ni-\u003ename);\nfs/ntfs/attrib.c-3301-}\n--\nfs/ntfs/inode.c=2356=static void __ntfs_clear_inode(struct ntfs_inode *ni)\n--\nfs/ntfs/inode.c-2372-\t\tWARN_ON(!ni-\u003ename);\nfs/ntfs/inode.c:2373:\t\tkfree(ni-\u003ename);\nfs/ntfs/inode.c-2374-\t}\n"
} |
| 73/3 | 2026/10/11 22:58 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 123833 output: 722 thoughts: 5703 Thoughts: **KMSAN Assessment: NeedsKMSAN = false** Okay, after carefully reviewing the code changes, my assessment is that `NeedsKMSAN=false`. The primary concerns this patch introduces are not related to uninitialized memory reads or information leaks, the domain in which KMSAN would be most effective. Instead, the patch mainly concerns issues that are readily detectable by standard debugging tools already enabled in KASAN-enabled kernels and LOCKDEP. Specifically, the user-facing interfaces (the new ioctls) are secure from uninitialized reads: * **`NTFS_IOC_STREAM_READ`:** Input data structures are fully populated by `copy_from_user` and the read buffer content is explicitly copied to userspace after it's been initialized via `ntfs_inode_attr_pread`. The output buffer length written back is explicitly controlled by the bytes read. * **`NTFS_IOC_STREAM_WRITE`:** Only the `bytes_returned` field is copied back to user space. * **`NTFS_IOC_LIST_STREAMS`:** The output buffer is allocated with `kvzalloc`, so all padding and alignment gaps are zeroed. The headers are read from user and written back with appropriate checking, preventing any uninitialized memory leaks. The structure has no padding gaps itself. * **Other ioctls**: Follow the same pattern, being populated from userspace and writing back a known count. Internally, I've checked: * All kernel data structures (e.g., `struct ntfs_stream_path`, `struct ntfs_attr`, `struct ntfs_fsync_attr`) are either fully initialized upon declaration using designated initializers, or fully initialized by their parsing functions before their contents are read. * Heap allocations, such as stream name buffers, are completely initialized immediately following allocation. Stream data initialization uses zeroing functions like `iomap_zero_range` for allocation gaps and `memset` for resident attribute expansions. * The code introduces no novel use of uninitialized memory. The potential risks are: * Locking-related issues (e.g., deadlocks involving the `mrec_lock` on base and stream inodes). * Reference counting / use-after-free conditions related to stream inodes and their base inodes (which will be heavily exercised). * Out-of-bounds reads in string handling (UTF-16 names) which is mitigated by careful bounds checking. * Filesystem metadata inconsistencies. These risks are the domain of standard KASAN/LOCKDEP/etc. debugging tools and there is no evidence of a need for KMSAN. The focus of this patch is not on exposing uninitialized reads; all new and modified structures are initialized to avoid such risks. The code already has robust string handling to avoid overflows/out-of-bounds reads. Therefore, running a full KMSAN session on this patch is unnecessary and would be a waste of resources. |
| 74/3 | 2026/10/11 22:59 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series adds named stream support ($DATA attributes) to the NTFS filesystem, introducing both a Windows-compatible pathname interface (file:stream) and ioctl interfaces (NTFS_IOC_STREAM_READ, NTFS_IOC_STREAM_WRITE, NTFS_IOC_STREAM_REMOVE, NTFS_IOC_LIST_STREAMS).\n\nAn analysis of memory initialization and kernel-to-user data transfers shows:\n1. UAPI Structures \u0026 Info-leak checks:\n - In `ntfs_ioctl_stream(NTFS_STREAM_IOCTL_READ)`, `struct ntfs_stream` has no structure padding (__aligned_u64, __aligned_u64, __aligned_u64, __u32, __u32, __aligned_u64). The header and name are initialized from user space via copy_from_user(), `bytes_returned` is set explicitly, and the stream payload is read into the buffer via `ntfs_inode_attr_pread()`, which returns the exact byte count copied into the buffer. The amount copied back to user space strictly equals `sizeof(*req) + req-\u003ename_len + req-\u003ebytes_returned`.\n - In `ntfs_ioctl_stream(NTFS_STREAM_IOCTL_WRITE)`, only `req-\u003ebytes_returned` is copied to user space.\n - In `ntfs_ioctl_list_streams()`, `struct ntfs_list_streams` contains no padding. The entry buffer `kbuf` is allocated using `kvzalloc()`, ensuring that any structure alignment padding (entries are 8-byte aligned) or reserved fields in `struct ntfs_stream_entry` are zero-initialized prior to `copy_to_user()`.\n2. Kernel Data Structures \u0026 Stack/Heap Allocations:\n - `struct ntfs_stream_path` fields are populated by `ntfs_stream_path_parse()` whenever it returns positive, and never accessed otherwise.\n - `struct ntfs_fsync_attr` allocated in `ntfs_file_fsync()` has only two fields (`list` and `inode`), both of which are initialized immediately upon allocation.\n - `ni-\u003ename` buffers allocated in `ntfs_read_locked_attr_inode()` are immediately populated via `memcpy()` and null-terminated.\n - Gaps in stream data are cleared using `iomap_zero_range()` in `ntfs_extend_initialized_size()`, and resident attribute extensions zero new bytes via `memset()` in `ntfs_resident_attr_value_resize()`.\n3. KASAN vs. KMSAN applicability:\n - The primary risks in this patch series involve locking hierarchies (locking base inode's `mrec_lock` across stream inodes), reference counting and deferred unlink lifecycles (`stream_open_count`, `NInoStreamUnlinked`), and file extent bounds checks. These failure modes represent locking deadlocks, use-after-free, or out-of-bounds access, which are already detected by standard debuggers (LOCKDEP, KASAN, etc.).\n - There are no uninitialized memory reads or information leaks introduced.\n\nTherefore, dedicated KMSAN fuzzing is not justified.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series adds named stream support ($DATA attributes) to the NTFS filesystem, introducing both a Windows-compatible pathname interface (file:stream) and ioctl interfaces (NTFS_IOC_STREAM_READ, NTFS_IOC_STREAM_WRITE, NTFS_IOC_STREAM_REMOVE, NTFS_IOC_LIST_STREAMS).\n\nAn analysis of memory initialization and kernel-to-user data transfers shows:\n1. UAPI Structures \u0026 Info-leak checks:\n - In `ntfs_ioctl_stream(NTFS_STREAM_IOCTL_READ)`, `struct ntfs_stream` has no structure padding (__aligned_u64, __aligned_u64, __aligned_u64, __u32, __u32, __aligned_u64). The header and name are initialized from user space via copy_from_user(), `bytes_returned` is set explicitly, and the stream payload is read into the buffer via `ntfs_inode_attr_pread()`, which returns the exact byte count copied into the buffer. The amount copied back to user space strictly equals `sizeof(*req) + req-\u003ename_len + req-\u003ebytes_returned`.\n - In `ntfs_ioctl_stream(NTFS_STREAM_IOCTL_WRITE)`, only `req-\u003ebytes_returned` is copied to user space.\n - In `ntfs_ioctl_list_streams()`, `struct ntfs_list_streams` contains no padding. The entry buffer `kbuf` is allocated using `kvzalloc()`, ensuring that any structure alignment padding (entries are 8-byte aligned) or reserved fields in `struct ntfs_stream_entry` are zero-initialized prior to `copy_to_user()`.\n2. Kernel Data Structures \u0026 Stack/Heap Allocations:\n - `struct ntfs_stream_path` fields are populated by `ntfs_stream_path_parse()` whenever it returns positive, and never accessed otherwise.\n - `struct ntfs_fsync_attr` allocated in `ntfs_file_fsync()` has only two fields (`list` and `inode`), both of which are initialized immediately upon allocation.\n - `ni-\u003ename` buffers allocated in `ntfs_read_locked_attr_inode()` are immediately populated via `memcpy()` and null-terminated.\n - Gaps in stream data are cleared using `iomap_zero_range()` in `ntfs_extend_initialized_size()`, and resident attribute extensions zero new bytes via `memset()` in `ntfs_resident_attr_value_resize()`.\n3. KASAN vs. KMSAN applicability:\n - The primary risks in this patch series involve locking hierarchies (locking base inode's `mrec_lock` across stream inodes), reference counting and deferred unlink lifecycles (`stream_open_count`, `NInoStreamUnlinked`), and file extent bounds checks. These failure modes represent locking deadlocks, use-after-free, or out-of-bounds access, which are already detected by standard debuggers (LOCKDEP, KASAN, etc.).\n - There are no uninitialized memory reads or information leaks introduced.\n\nTherefore, dedicated KMSAN fuzzing is not justified.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|