ntfsplus: ntfs filesystem remake
Subsystems (3)
v2 cover ntfsplus: ntfs filesystem remake
Introduction
============
The NTFS filesystem[1] still remains the default filesystem for Windows and The well-maintained NTFS driver in the Linux kernel enhances interoperability with Windows devices, making it easier for Linux users to work with NTFS-formatted drives. Currently, ntfs support in Linux was the long-neglected NTFS Classic (read-only), which has been removed from the Linux kernel, leaving the poorly maintained ntfs3. ntfs3 still has many problems and is poorly maintained, so users and distributions are still using the old legacy ntfs-3g.
What is ntfsplus?
=================
The remade ntfs called ntfsplus is an implementation that supports write and the essential requirements(iomap, no buffer-head, utilities, xfstests test result) based on read-only classic NTFS.
The old read-only ntfs code is much cleaner, with extensive comments, offers readability that makes understanding NTFS easier. This is why ntfsplus was developed on old read-only NTFS base.
The target is to provide current trends(iomap, no buffer head, folio), enhanced performance, stable maintenance, utility support including fsck.
Key Features
============
- Write support:
Implement write support on classic read-only NTFS. Additionally, integrate delayed allocation to enhance write performance through multi-cluster allocation and minimized fragmentation of cluster bitmap.
- Switch to using iomap:
Use iomap for buffered IO writes, reads, direct IO, file extent mapping, readpages, writepages operations.
- Stop using the buffer head:
The use of buffer head in old ntfs and switched to use folio instead. As a result, CONFIG_BUFFER_HEAD option enable is removed in Kconfig also.
- Public utilities include fsck[2]:
While ntfs-3g includes ntfsprogs as a component, it notably lacks the fsck implementation. So we have launched a new ntfs utilitiies project called ntfsprogs-plus by forking from ntfs-3g after removing unnecessary ntfs fuse implementation. fsck.ntfs can be used for ntfs testing with xfstests as well as for recovering corrupted NTFS device.
- Performance Enhancements:
- ntfsplus vs. ntfs3:
* Performance was benchmarked using iozone with various chunk size.
- In single-thread(1T) write tests, ntfsplus show approximately 3~5% better performance.
- In multi-thread(4T) write tests, ntfsplus show approximately 35~110% better performance.
- Read throughput is identical for both ntfs implementations.
1GB file size:4096 size:16384 size:65536
MB/sec ntfsplus | ntfs3 ntfsplus | ntfs3 ntfsplus | ntfs3
─────────────────────────────────────────────────────────────────
read 399 | 399 426 | 424 429 | 430
─────────────────────────────────────────────────────────────────
write(1T) 291 | 276 325 | 305 333 | 317
write(4T) 105 | 50 113 | 78 114 | 99.6
* File list browsing performance. (about 12~14% faster)
files:100000 files:200000 files:400000
Sec ntfsplus | ntfs3 ntfsplus | ntfs3 ntfsplus | ntfs3
─────────────────────────────────────────────────────────────────
ls -lR 7.07 | 8.10 14.03 | 16.35 28.27 | 32.86
* mount time.
parti_size:1TB parti_size:2TB parti_size:4TB
Sec ntfsplus | ntfs3 ntfsplus | ntfs3 ntfsplus | ntfs3
─────────────────────────────────────────────────────────────────
mount 0.38 | 2.03 0.39 | 2.25 0.70 | 4.51
The following are the reasons why ntfsplus performance is higher
compared to ntfs3:
- Use iomap aops.
- Delayed allocation support.
- Optimize zero out for newly allocated clusters.
- Optimize runlist merge overhead with small chunck size.
- pre-load mft(inode) blocks and index(dentry) blocks to improve readdir + stat performance.
- Load lcn bitmap on background.
- Stability improvement:
a. Pass more xfstests tests:
ntfsplus passed 287 tests, significantly higher than ntfs3's 218. ntfsplus implement fallocate, idmapped mount and permission, etc, resulting in a significantly high number of xfstests passing compared to ntfs3.
b. Bonnie++ issue[3]:
The Bonnie++ benchmark fails on ntfs3 with a "Directory not empty" error during file deletion. ntfs3 currently iterates directory entries by reading index blocks one by one. When entries are deleted concurrently, index block merging or entry relocation can cause readdir() to skip some entries, leaving files undeleted in workloads(bonnie++) that mix unlink and directory scans. ntfsplus implement leaf chain traversal in readdir to avoid entry skip on deletion.
- Journaling support:
ntfs3 does not provide full journaling support. It only implement journal replay[4], which in our testing did not function correctly. My next task after upstreaming will be to add full journal support to ntfsplus.
The feature comparison summary
==============================
Feature ntfsplus ntfs3 =================================== ======== =========== Write support Yes Yes iomap support Yes No No buffer head Yes No Public utilities(mkfs, fsck, etc.) Yes No xfstests passed 287 218 Idmapped mount Yes No Delayed allocation Yes No Bonnie++ Pass Fail Journaling Planned Inoperative =================================== ======== ===========
References
==========
[1] https://en.wikipedia.org/wiki/NTFS
[2] https://github.com/ntfsprogs-plus/ntfsprogs-plus
[3] https://lore.kernel.org/ntfs3/CAOZgwEd7NDkGEpdF6UQTcbYuupDavaHBoj4WwTy3Qe4Bqm6V0g@mail.gmail.com/
[4] https://marc.info/?l=linux-fsdevel&m=161738417018673&q=mbox
Available in the Git repository at:
===================================git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/ntfs.git ntfs-next
v2:
- Add ntfs3-compatible mount options(sys_immutable, nohidden, hide_dot_files, nocase, acl, windows_names, disable_sparse, discard).
- Add iocharset mount option.
- Add ntfs3-compatible dos attribute and ntfs attribute load/store in setxattr/getattr().
- Add support for FS_IOC_{GET,SET}FSLABEL ioctl.
- Add support for FITRIM ioctl.
- Fix the warnings(duplicate symbol, __divdi3, etc) from kernel test robot.
- Prefix pr_xxx() with ntfsplus.
- Add support for $MFT File extension.
- Add Documentation/filesystems/ntfsplus.rst.
- Mark experimental.
- Remove BUG traps warnings from checkpatch.pl.
Namjae Jeon (11):
ntfsplus: in-memory, on-disk structures and headers
ntfsplus: add super block operations
ntfsplus: add inode operations
ntfsplus: add directory operations
ntfsplus: add file operations
ntfsplus: add iomap and address space operations
ntfsplus: add attrib operatrions
ntfsplus: add runlist handling and cluster allocator
ntfsplus: add reparse and ea operations
ntfsplus: add misc operations
ntfsplus: add Kconfig and Makefile
Documentation/filesystems/index.rst | 1 + Documentation/filesystems/ntfsplus.rst | 199 + fs/Kconfig | 1 + fs/Makefile | 1 + fs/ntfsplus/Kconfig | 45 + fs/ntfsplus/Makefile | 18 + fs/ntfsplus/aops.c | 617 +++ fs/ntfsplus/aops.h | 92 + fs/ntfsplus/attrib.c | 5377 ++++++++++++++++++++++++ fs/ntfsplus/attrib.h | 159 + fs/ntfsplus/attrlist.c | 285 ++ fs/ntfsplus/attrlist.h | 21 + fs/ntfsplus/bitmap.c | 290 ++ fs/ntfsplus/bitmap.h | 93 + fs/ntfsplus/collate.c | 178 + fs/ntfsplus/collate.h | 37 + fs/ntfsplus/compress.c | 1564 +++++++ fs/ntfsplus/dir.c | 1230 ++++++ fs/ntfsplus/dir.h | 33 + fs/ntfsplus/ea.c | 931 ++++ fs/ntfsplus/ea.h | 25 + fs/ntfsplus/file.c | 1142 +++++ fs/ntfsplus/index.c | 2112 ++++++++++ fs/ntfsplus/index.h | 127 + fs/ntfsplus/inode.c | 3729 ++++++++++++++++ fs/ntfsplus/inode.h | 353 ++ fs/ntfsplus/layout.h | 2288 ++++++++++ fs/ntfsplus/lcnalloc.c | 1012 +++++ fs/ntfsplus/lcnalloc.h | 127 + fs/ntfsplus/logfile.c | 770 ++++ fs/ntfsplus/logfile.h | 316 ++ fs/ntfsplus/mft.c | 2698 ++++++++++++ fs/ntfsplus/mft.h | 92 + fs/ntfsplus/misc.c | 213 + fs/ntfsplus/misc.h | 218 + fs/ntfsplus/mst.c | 195 + fs/ntfsplus/namei.c | 1677 ++++++++ fs/ntfsplus/ntfs.h | 180 + fs/ntfsplus/ntfs_iomap.c | 700 +++ fs/ntfsplus/ntfs_iomap.h | 22 + fs/ntfsplus/reparse.c | 550 +++ fs/ntfsplus/reparse.h | 15 + fs/ntfsplus/runlist.c | 1983 +++++++++ fs/ntfsplus/runlist.h | 91 + fs/ntfsplus/super.c | 2865 +++++++++++++ fs/ntfsplus/unistr.c | 473 +++ fs/ntfsplus/upcase.c | 73 + fs/ntfsplus/volume.h | 254 ++ include/uapi/linux/ntfs.h | 23 + 49 files changed, 35495 insertions(+) create mode 100644 Documentation/filesystems/ntfsplus.rst create mode 100644 fs/ntfsplus/Kconfig create mode 100644 fs/ntfsplus/Makefile create mode 100644 fs/ntfsplus/aops.c create mode 100644 fs/ntfsplus/aops.h create mode 100644 fs/ntfsplus/attrib.c create mode 100644 fs/ntfsplus/attrib.h create mode 100644 fs/ntfsplus/attrlist.c create mode 100644 fs/ntfsplus/attrlist.h create mode 100644 fs/ntfsplus/bitmap.c create mode 100644 fs/ntfsplus/bitmap.h create mode 100644 fs/ntfsplus/collate.c create mode 100644 fs/ntfsplus/collate.h create mode 100644 fs/ntfsplus/compress.c create mode 100644 fs/ntfsplus/dir.c create mode 100644 fs/ntfsplus/dir.h create mode 100644 fs/ntfsplus/ea.c create mode 100644 fs/ntfsplus/ea.h create mode 100644 fs/ntfsplus/file.c create mode 100644 fs/ntfsplus/index.c create mode 100644 fs/ntfsplus/index.h create mode 100644 fs/ntfsplus/inode.c create mode 100644 fs/ntfsplus/inode.h create mode 100644 fs/ntfsplus/layout.h create mode 100644 fs/ntfsplus/lcnalloc.c create mode 100644 fs/ntfsplus/lcnalloc.h create mode 100644 fs/ntfsplus/logfile.c create mode 100644 fs/ntfsplus/logfile.h create mode 100644 fs/ntfsplus/mft.c create mode 100644 fs/ntfsplus/mft.h create mode 100644 fs/ntfsplus/misc.c create mode 100644 fs/ntfsplus/misc.h create mode 100644 fs/ntfsplus/mst.c create mode 100644 fs/ntfsplus/namei.c create mode 100644 fs/ntfsplus/ntfs.h create mode 100644 fs/ntfsplus/ntfs_iomap.c create mode 100644 fs/ntfsplus/ntfs_iomap.h create mode 100644 fs/ntfsplus/reparse.c create mode 100644 fs/ntfsplus/reparse.h create mode 100644 fs/ntfsplus/runlist.c create mode 100644 fs/ntfsplus/runlist.h create mode 100644 fs/ntfsplus/super.c create mode 100644 fs/ntfsplus/unistr.c create mode 100644 fs/ntfsplus/upcase.c create mode 100644 fs/ntfsplus/volume.h create mode 100644 include/uapi/linux/ntfs.h
Signature · 1 line
2.25.1
PATCH v2 1/11 ntfsplus: in-memory, on-disk structures and headers
This adds in-memory, on-disk structures, headers and documentation.
--- Documentation/filesystems/index.rst | 1 + Documentation/filesystems/ntfsplus.rst | 199 +++ fs/ntfsplus/aops.h | 92 + fs/ntfsplus/attrib.h | 159 ++ fs/ntfsplus/attrlist.h | 21 + fs/ntfsplus/bitmap.h | 93 + fs/ntfsplus/collate.h | 37 + fs/ntfsplus/dir.h | 33 + fs/ntfsplus/ea.h | 25 + fs/ntfsplus/index.h | 127 ++ fs/ntfsplus/inode.h | 353 ++++ fs/ntfsplus/layout.h | 2288 ++++++++++++++++++++++++ fs/ntfsplus/lcnalloc.h | 127 ++ fs/ntfsplus/logfile.h | 316 ++++ fs/ntfsplus/mft.h | 92 + fs/ntfsplus/misc.h | 218 +++ fs/ntfsplus/ntfs.h | 180 ++ fs/ntfsplus/ntfs_iomap.h | 22 + fs/ntfsplus/reparse.h | 15 + fs/ntfsplus/runlist.h | 91 + fs/ntfsplus/volume.h | 254 +++ include/uapi/linux/ntfs.h | 23 + 22 files changed, 4766 insertions(+) create mode 100644 Documentation/filesystems/ntfsplus.rst create mode 100644 fs/ntfsplus/aops.h create mode 100644 fs/ntfsplus/attrib.h create mode 100644 fs/ntfsplus/attrlist.h create mode 100644 fs/ntfsplus/bitmap.h create mode 100644 fs/ntfsplus/collate.h create mode 100644 fs/ntfsplus/dir.h create mode 100644 fs/ntfsplus/ea.h create mode 100644 fs/ntfsplus/index.h create mode 100644 fs/ntfsplus/inode.h create mode 100644 fs/ntfsplus/layout.h create mode 100644 fs/ntfsplus/lcnalloc.h create mode 100644 fs/ntfsplus/logfile.h create mode 100644 fs/ntfsplus/mft.h create mode 100644 fs/ntfsplus/misc.h create mode 100644 fs/ntfsplus/ntfs.h create mode 100644 fs/ntfsplus/ntfs_iomap.h create mode 100644 fs/ntfsplus/reparse.h create mode 100644 fs/ntfsplus/runlist.h create mode 100644 fs/ntfsplus/volume.h create mode 100644 include/uapi/linux/ntfs.h
Documentation/filesystems/index.rst +1 -0
@@ -101,6 +101,7 @@ Documentation for filesystem implementations. nilfs2 nfs/index ntfs3 + ntfsplus ocfs2 ocfs2-online-filecheck omfs
Documentation/filesystems/ntfsplus.rst new file +199 -0
199 lines not on this page · raw message
fs/ntfsplus/aops.h new file +92 -0
92 lines not on this page · raw message
fs/ntfsplus/attrib.h new file +159 -0
159 lines not on this page · raw message
fs/ntfsplus/attrlist.h new file +21 -0
@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Exports for attribute list attribute handling. +* Originated from Linux-NTFS project. +* +* Copyright (c) 2004 Anton Altaparmakov +* Copyright (c) 2004 Yura Pakhuchiy +* Copyright (c) 2025 LG Electronics Co., Ltd. +*/ + +#ifndef _NTFS_ATTRLIST_H +#define _NTFS_ATTRLIST_H + +#include "attrib.h" + +int ntfs_attrlist_need (struct ntfs_inode *ni); +int ntfs_attrlist_entry_add (struct ntfs_inode *ni,struct attr_record *attr); +int ntfs_attrlist_entry_rm (struct ntfs_attr_search_ctx *ctx); +int ntfs_attrlist_update (struct ntfs_inode *base_ni); + +#endif /* defined _NTFS_ATTRLIST_H */
fs/ntfsplus/bitmap.h new file +93 -0
93 lines not on this page · raw message
fs/ntfsplus/collate.h new file +37 -0
@@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Defines for NTFS kernel collation handling. +* Part of the Linux-NTFS project. +* +* Copyright (c) 2004 Anton Altaparmakov +* +* Part of this file is based on code from the NTFS-3G project. +* and is copyrighted by the respective authors below: +* Copyright (c) 2004 Anton Altaparmakov +* Copyright (c) 2005 Yura Pakhuchiy +*/ + +#ifndef _LINUX_NTFS_COLLATE_H +#define _LINUX_NTFS_COLLATE_H + +#include "volume.h" + +static inline bool ntfs_is_collation_rule_supported (__le32 cr) +{ +int i; + +if (unlikely (cr !=COLLATION_BINARY && cr !=COLLATION_NTOFS_ULONG && + cr !=COLLATION_FILE_NAME ) && cr !=COLLATION_NTOFS_ULONGS ) +return false; + i =le32_to_cpu (cr); +if (likely (((i >=0 ) && (i <=0x02 )) || + ((i >=0x10 ) && (i <=0x13 )))) +return true; +return false; +} + +int ntfs_collate (struct ntfs_volume *vol,__le32 cr, +const void *data1,const int data1_len, +const void *data2,const int data2_len); + +#endif /* _LINUX_NTFS_COLLATE_H */
fs/ntfsplus/dir.h new file +33 -0
@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Defines for directory handling in NTFS Linux kernel driver. +* Part of the Linux-NTFS project. +* +* Copyright (c) 2002-2004 Anton Altaparmakov +*/ + +#ifndef _LINUX_NTFS_DIR_H +#define _LINUX_NTFS_DIR_H + +#include "inode.h" + +/* +* ntfs_name is used to return the file name to the caller of +* ntfs_lookup_inode_by_name() in order for the caller (namei.c::ntfs_lookup()) +* to be able to deal with dcache aliasing issues. +*/ +struct ntfs_name { +u64 mref; +u8 type; +u8 len; +__le16 name[]; +} __packed; + +/* The little endian Unicode string $I30 as a global constant. */ +extern __le16 I30 [5 ]; + +u64 ntfs_lookup_inode_by_name (struct ntfs_inode *dir_ni, +const __le16 *uname,const int uname_len,struct ntfs_name **res); +int ntfs_check_empty_dir (struct ntfs_inode *ni,struct mft_record *ni_mrec); + +#endif /* _LINUX_NTFS_FS_DIR_H */
fs/ntfsplus/ea.h new file +25 -0
@@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#define NTFS_EA_UID BIT(1) +#define NTFS_EA_GID BIT(2) +#define NTFS_EA_MODE BIT(3) + +extern const struct xattr_handler *const ntfsp_xattr_handlers[]; + +int ntfs_ea_set_wsl_not_symlink (struct ntfs_inode *ni,mode_t mode,dev_t dev); +int ntfs_ea_get_wsl_inode (struct inode *inode,dev_t *rdevp,unsigned int flags); +int ntfs_ea_set_wsl_inode (struct inode *inode,dev_t rdev,__le16 *ea_size, +unsigned int flags); +ssize_t ntfsp_listxattr (struct dentry *dentry,char *buffer,size_t size); + +#ifdef CONFIG_NTFSPLUS_FS_POSIX_ACL +struct posix_acl *ntfsp_get_acl (struct mnt_idmap *idmap,struct dentry *dentry, +int type); +int ntfsp_set_acl (struct mnt_idmap *idmap,struct dentry *dentry, +struct posix_acl *acl,int type); +int ntfsp_init_acl (struct mnt_idmap *idmap,struct inode *inode, +struct inode *dir); +#else +#define ntfsp_get_acl NULL +#define ntfsp_set_acl NULL +#endif
fs/ntfsplus/index.h new file +127 -0
127 lines not on this page · raw message
fs/ntfsplus/inode.h new file +353 -0
353 lines not on this page · raw message
fs/ntfsplus/layout.h new file +2288 -0
1,854 lines not on this page · raw message
fs/ntfsplus/lcnalloc.h new file +127 -0
127 lines not on this page · raw message
fs/ntfsplus/logfile.h new file +316 -0
316 lines not on this page · raw message
fs/ntfsplus/mft.h new file +92 -0
92 lines not on this page · raw message
fs/ntfsplus/misc.h new file +218 -0
218 lines not on this page · raw message
fs/ntfsplus/ntfs.h new file +180 -0
180 lines not on this page · raw message
fs/ntfsplus/runlist.h new file +91 -0
91 lines not on this page · raw message
fs/ntfsplus/volume.h new file +254 -0
254 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.25.1
Part of the diff is not shown here. View the raw message.
PATCH v2 2/11 ntfsplus: add super block operations
In reply to Namjae JeonThis adds the implementation of superblock operations for ntfsplus.
--- fs/ntfsplus/super.c | 2865 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 2865 insertions(+) create mode 100644 fs/ntfsplus/super.c
fs/ntfsplus/super.c new file +2865 -0
@@ -0,0 +1,2865 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* NTFS kernel super block handling. Part of the Linux-NTFS project. +* +* Copyright (c) 2001-2012 Anton Altaparmakov and Tuxera Inc. +* Copyright (c) 2001,2002 Richard Russon +* Copyright (c) 2025 LG Electronics Co., Ltd. +*/ + +#include <linux/blkdev.h> /* For bdev_logical_block_size(). */ +#include <linux/backing-dev.h> +#include <linux/vfs.h> +#include <linux/fs_struct.h> +#include <linux/sched/mm.h> +#include <linux/fs_context.h> +#include <linux/fs_parser.h> +#include <uapi/linux/ntfs.h> + +#include "misc.h" +#include "logfile.h" +#include "index.h" +#include "ntfs.h" +#include "ea.h" +#include "volume.h" + +/* A global default upcase table and a corresponding reference count. */ +static __le16 *default_upcase; +static unsigned long ntfs_nr_upcase_users; + +static struct workqueue_struct *ntfs_wq; + +/* Error constants/strings used in inode.c::ntfs_show_options(). */ +enum { +/* One of these must be present, default is ON_ERRORS_CONTINUE. */ +ON_ERRORS_PANIC =0x01 , +ON_ERRORS_REMOUNT_RO =0x02 , +ON_ERRORS_CONTINUE =0x04 , +}; + +static const struct constant_table ntfs_param_enums[] = { + {"panic" ,ON_ERRORS_PANIC }, + {"remount-ro" ,ON_ERRORS_REMOUNT_RO }, + {"continue" ,ON_ERRORS_CONTINUE }, + {} +}; + +enum { + Opt_uid, + Opt_gid, + Opt_umask, + Opt_dmask, + Opt_fmask, + Opt_errors, + Opt_nls, + Opt_charset, + Opt_show_sys_files, + Opt_show_meta, + Opt_case_sensitive, + Opt_disable_sparse, + Opt_sparse, + Opt_mft_zone_multiplier, + Opt_preallocated_size, + Opt_sys_immutable, + Opt_nohidden, + Opt_hide_dot_files, + Opt_check_windows_names, + Opt_acl, + Opt_discard, + Opt_nocase, +}; + +static const struct fs_parameter_spec ntfs_parameters[] = { +fsparam_u32 ("uid" , Opt_uid), +fsparam_u32 ("gid" , Opt_gid), +fsparam_u32oct ("umask" , Opt_umask), +fsparam_u32oct ("dmask" , Opt_dmask), +fsparam_u32oct ("fmask" , Opt_fmask), +fsparam_string ("nls" , Opt_nls), +fsparam_string ("iocharset" , Opt_charset), +fsparam_enum ("errors" , Opt_errors, ntfs_param_enums), +fsparam_flag ("show_sys_files" , Opt_show_sys_files), +fsparam_flag ("showmeta" , Opt_show_meta), +fsparam_flag ("case_sensitive" , Opt_case_sensitive), +fsparam_flag ("disable_sparse" , Opt_disable_sparse), +fsparam_s32 ("mft_zone_multiplier" , Opt_mft_zone_multiplier), +fsparam_u64 ("preallocated_size" , Opt_preallocated_size), +fsparam_flag ("sys_immutable" , Opt_sys_immutable), +fsparam_flag ("nohidden" , Opt_nohidden), +fsparam_flag ("hide_dot_files" , Opt_hide_dot_files), +fsparam_flag ("windows_names" , Opt_check_windows_names), +fsparam_flag ("acl" , Opt_acl), +fsparam_flag ("discard" , Opt_discard), +fsparam_flag ("sparse" , Opt_sparse), +fsparam_flag ("nocase" , Opt_nocase), + {} +}; + +static int ntfs_parse_param (struct fs_context *fc,struct fs_parameter *param) +{ +struct ntfs_volume *vol = fc->s_fs_info; +struct fs_parse_result result; +int opt; + + opt =fs_parse (fc, ntfs_parameters, param, &result); +if (opt <0 ) +return opt; + +switch (opt) { +case Opt_uid: + vol->uid =make_kuid (current_user_ns (), result.uint_32); +break ; +case Opt_gid: + vol->gid =make_kgid (current_user_ns (), result.uint_32); +break ; +case Opt_umask: + vol->fmask = vol->dmask = result.uint_32; +break ; +case Opt_dmask: + vol->dmask = result.uint_32; +break ; +case Opt_fmask: + vol->fmask = result.uint_32; +break ; +case Opt_errors: + vol->on_errors = result.uint_32; +break ; +case Opt_nls: +case Opt_charset: +if (vol->nls_map) +unload_nls (vol->nls_map); + vol->nls_map =load_nls (param->string); +if (!vol->nls_map) { +ntfs_error (vol->sb,"Failed to load NLS table '%s'." , + param->string); +return -EINVAL ; + } +break ; +case Opt_mft_zone_multiplier: +if (vol->mft_zone_multiplier && vol->mft_zone_multiplier != + result.int_32) { +ntfs_error (vol->sb,"Cannot change mft_zone_multiplier on remount." ); +return -EINVAL ; + } +if (result.int_32 <1 || result.int_32 >4 ) { +ntfs_error (vol->sb, +"Invalid mft_zone_multiplier. Using default value, i.e. 1." ); + vol->mft_zone_multiplier =1 ; + }else + vol->mft_zone_multiplier = result.int_32; +break ; +case Opt_show_sys_files: +case Opt_show_meta: +if (result.boolean) +NVolSetShowSystemFiles (vol); +else +NVolClearShowSystemFiles (vol); +break ; +case Opt_case_sensitive: +if (result.boolean) +NVolSetCaseSensitive (vol); +else +NVolClearCaseSensitive (vol); +break ; +case Opt_nocase: +if (result.boolean) +NVolClearCaseSensitive (vol); +else +NVolSetCaseSensitive (vol); +break ; +case Opt_preallocated_size: + vol->preallocated_size = (loff_t )result.uint_64; +break ; +case Opt_sys_immutable: +if (result.boolean) +NVolSetSysImmutable (vol); +else +NVolClearSysImmutable (vol); +break ; +case Opt_nohidden: +if (result.boolean) +NVolClearShowHiddenFiles (vol); +else +NVolSetShowHiddenFiles (vol); +break ; +case Opt_hide_dot_files: +if (result.boolean) +NVolSetHideDotFiles (vol); +else +NVolClearHideDotFiles (vol); +break ; +case Opt_check_windows_names: +if (result.boolean) +NVolSetCheckWindowsNames (vol); +else +NVolClearCheckWindowsNames (vol); +break ; +case Opt_acl: +if (result.boolean) + fc->sb_flags |=SB_POSIXACL ; +else + fc->sb_flags &= ~SB_POSIXACL ; +break ; +case Opt_discard: +if (result.boolean) +NVolSetDiscard (vol); +else +NVolClearDiscard (vol); +break ; +case Opt_disable_sparse: +if (result.boolean) +NVolSetDisableSparse (vol); +else +NVolClearDisableSparse (vol); +break ; +case Opt_sparse: +break ; +default : +return -EINVAL ; + } + +return 0 ; +} + +/** +* ntfs_mark_quotas_out_of_date - mark the quotas out of date on an ntfs volume +* @vol: ntfs volume on which to mark the quotas out of date +* +* Mark the quotas out of date on the ntfs volume @vol and return 'true' on +* success and 'false' on error. +*/ +static bool ntfs_mark_quotas_out_of_date (struct ntfs_volume *vol) +{ +struct ntfs_index_context *ictx; +struct quota_control_entry *qce; +const __le32 qid =QUOTA_DEFAULTS_ID ; +int err; + +ntfs_debug ("Entering." ); +if (NVolQuotaOutOfDate (vol)) + goto done; +if (!vol->quota_ino || !vol->quota_q_ino) { +ntfs_error (vol->sb,"Quota inodes are not open." ); +return false; + } +inode_lock (vol->quota_q_ino); + ictx =ntfs_index_ctx_get (NTFS_I (vol->quota_q_ino),I30 ,4 ); +if (!ictx) { +ntfs_error (vol->sb,"Failed to get index context." ); + goto err_out; + } + err =ntfs_index_lookup (&qid,sizeof (qid), ictx); +if (err) { +if (err == -ENOENT ) +ntfs_error (vol->sb,"Quota defaults entry is not present." ); +else +ntfs_error (vol->sb,"Lookup of quota defaults entry failed." ); + goto err_out; + } +if (ictx->data_len < offsetof(struct quota_control_entry , sid)) { +ntfs_error (vol->sb,"Quota defaults entry size is invalid. Run chkdsk." ); + goto err_out; + } + qce = (struct quota_control_entry *)ictx->data; +if (le32_to_cpu (qce->version) !=QUOTA_VERSION ) { +ntfs_error (vol->sb, +"Quota defaults entry version 0x%x is not supported." , +le32_to_cpu (qce->version)); + goto err_out; + } +ntfs_debug ("Quota defaults flags = 0x%x." ,le32_to_cpu (qce->flags)); +/* If quotas are already marked out of date, no need to do anything. */ +if (qce->flags "A_FLAG_OUT_OF_DATE ) + goto set_done; +/* +* If quota tracking is neither requested, nor enabled and there are no +* pending deletes, no need to mark the quotas out of date. +*/ +if (!(qce->flags & (QUOTA_FLAG_TRACKING_ENABLED | +QUOTA_FLAG_TRACKING_REQUESTED | +QUOTA_FLAG_PENDING_DELETES ))) + goto set_done; +/* +* Set the QUOTA_FLAG_OUT_OF_DATE bit thus marking quotas out of date. +* This is verified on WinXP to be sufficient to cause windows to +* rescan the volume on boot and update all quota entries. +*/ + qce->flags |=QUOTA_FLAG_OUT_OF_DATE ; +/* Ensure the modified flags are written to disk. */ +ntfs_index_entry_flush_dcache_page (ictx); +ntfs_index_entry_mark_dirty (ictx); +set_done: +ntfs_index_ctx_put (ictx); +inode_unlock (vol->quota_q_ino); +/* +* We set the flag so we do not try to mark the quotas out of date +* again on remount. +*/ +NVolSetQuotaOutOfDate (vol); +done: +ntfs_debug ("Done." ); +return true; +err_out: +if (ictx) +ntfs_index_ctx_put (ictx); +inode_unlock (vol->quota_q_ino); +return false; +} + +static int ntfs_reconfigure (struct fs_context *fc) +{ +struct super_block *sb = fc->root->d_sb; +struct ntfs_volume *vol =NTFS_SB (sb); + +ntfs_debug ("Entering with remount" ); + +sync_filesystem (sb); + +/* +* For the read-write compiled driver, if we are remounting read-write, +* make sure there are no volume errors and that no unsupported volume +* flags are set. Also, empty the logfile journal as it would become +* stale as soon as something is written to the volume and mark the +* volume dirty so that chkdsk is run if the volume is not umounted +* cleanly. Finally, mark the quotas out of date so Windows rescans +* the volume on boot and updates them. +* +* When remounting read-only, mark the volume clean if no volume errors +* have occurred. +*/ +if (sb_rdonly (sb) && !(fc->sb_flags &SB_RDONLY )) { +static const char *es =". Cannot remount read-write." ; + +/* Remounting read-write. */ +if (NVolErrors (vol)) { +ntfs_error (sb,"Volume has errors and is read-only%s" , + es); +return -EROFS ; + } +if (vol->vol_flags &VOLUME_IS_DIRTY ) { +ntfs_error (sb,"Volume is dirty and read-only%s" , es); +return -EROFS ; + } +if (vol->vol_flags &VOLUME_MODIFIED_BY_CHKDSK ) { +ntfs_error (sb,"Volume has been modified by chkdsk and is read-only%s" , es); +return -EROFS ; + } +if (vol->vol_flags &VOLUME_MUST_MOUNT_RO_MASK ) { +ntfs_error (sb,"Volume has unsupported flags set (0x%x) and is read-only%s" , +le16_to_cpu (vol->vol_flags), es); +return -EROFS ; + } +if (vol->logfile_ino && !ntfs_empty_logfile (vol->logfile_ino)) { +ntfs_error (sb,"Failed to empty journal LogFile%s" , + es); +NVolSetErrors (vol); +return -EROFS ; + } +if (!ntfs_mark_quotas_out_of_date (vol)) { +ntfs_error (sb,"Failed to mark quotas out of date%s" , + es); +NVolSetErrors (vol); +return -EROFS ; + } + }else if (!sb_rdonly (sb) && (fc->sb_flags &SB_RDONLY )) { +/* Remounting read-only. */ +if (!NVolErrors (vol)) { +if (ntfs_clear_volume_flags (vol,VOLUME_IS_DIRTY )) +ntfs_warning (sb, +"Failed to clear dirty bit in volume information flags. Run chkdsk." ); + } + } + +ntfs_debug ("Done." ); +return 0 ; +} + +const struct option_t on_errors_arr[] = { + {ON_ERRORS_PANIC ,"panic" }, + {ON_ERRORS_REMOUNT_RO ,"remount-ro" , }, + {ON_ERRORS_CONTINUE ,"continue" , }, + {0 ,NULL } +}; + +void ntfs_handle_error (struct super_block *sb) +{ +struct ntfs_volume *vol =NTFS_SB (sb); + +if (sb_rdonly (sb)) +return ; + +if (vol->on_errors ==ON_ERRORS_REMOUNT_RO ) { + sb->s_flags |=SB_RDONLY ; +pr_crit ("(device %s): Filesystem has been set read-only\n" , + sb->s_id); + }else if (vol->on_errors ==ON_ERRORS_PANIC ) { +panic ("ntfs: (device %s): panic from previous error\n" , + sb->s_id); + }else if (vol->on_errors ==ON_ERRORS_CONTINUE ) { +if (errseq_check (&sb->s_wb_err, vol->wb_err) == -ENODEV ) { +NVolSetShutdown (vol); + vol->wb_err = sb->s_wb_err; + } + } +} + +/** +* ntfs_write_volume_flags - write new flags to the volume information flags +* @vol: ntfs volume on which to modify the flags +* @flags: new flags value for the volume information flags +* +* Internal function. You probably want to use ntfs_{set,clear}_volume_flags() +* instead (see below). +* +* Replace the volume information flags on the volume @vol with the value +* supplied in @flags. Note, this overwrites the volume information flags, so +* make sure to combine the flags you want to modify with the old flags and use +* the result when calling ntfs_write_volume_flags(). +* +* Return 0 on success and -errno on error. +*/ +static int ntfs_write_volume_flags (struct ntfs_volume *vol,const __le16 flags) +{ +struct ntfs_inode *ni =NTFS_I (vol->vol_ino); +struct volume_information *vi; +struct ntfs_attr_search_ctx *ctx; +int err; + +ntfs_debug ("Entering, old flags = 0x%x, new flags = 0x%x." , +le16_to_cpu (vol->vol_flags),le16_to_cpu (flags)); +mutex_lock (&ni->mrec_lock); +if (vol->vol_flags == flags) + goto done; + + ctx =ntfs_attr_get_search_ctx (ni,NULL ); +if (!ctx) { + err = -ENOMEM ; + goto put_unm_err_out; + } + + err =ntfs_attr_lookup (AT_VOLUME_INFORMATION ,NULL ,0 ,0 ,0 ,NULL ,0 , + ctx); +if (err) + goto put_unm_err_out; + + vi = (struct volume_information *)((u8 *)ctx->attr + +le16_to_cpu (ctx->attr->data.resident.value_offset)); + vol->vol_flags = vi->flags = flags; +mark_mft_record_dirty (ctx->ntfs_ino); +ntfs_attr_put_search_ctx (ctx); +done: +mutex_unlock (&ni->mrec_lock); +ntfs_debug ("Done." ); +return 0 ; +put_unm_err_out: +if (ctx) +ntfs_attr_put_search_ctx (ctx); +mutex_unlock (&ni->mrec_lock); +ntfs_error (vol->sb,"Failed with error code %i." , -err); +return err; +} + +/** +* ntfs_set_volume_flags - set bits in the volume information flags +* @vol: ntfs volume on which to modify the flags +* @flags: flags to set on the volume +* +* Set the bits in @flags in the volume information flags on the volume @vol. +* +* Return 0 on success and -errno on error. +*/ +int ntfs_set_volume_flags (struct ntfs_volume *vol,__le16 flags) +{ + flags &=VOLUME_FLAGS_MASK ; +return ntfs_write_volume_flags (vol, vol->vol_flags | flags); +} + +/** +* ntfs_clear_volume_flags - clear bits in the volume information flags +* @vol: ntfs volume on which to modify the flags +* @flags: flags to clear on the volume +* +* Clear the bits in @flags in the volume information flags on the volume @vol. +* +* Return 0 on success and -errno on error. +*/ +int ntfs_clear_volume_flags (struct ntfs_volume *vol,__le16 flags) +{ + flags &=VOLUME_FLAGS_MASK ; + flags = vol->vol_flags &cpu_to_le16 (~le16_to_cpu (flags)); +return ntfs_write_volume_flags (vol, flags); +} + +int ntfs_write_volume_label (struct ntfs_volume *vol,char *label) +{ +struct ntfs_inode *vol_ni =NTFS_I (vol->vol_ino); +struct ntfs_attr_search_ctx *ctx; +__le16 *uname; +int uname_len, ret; + + uname_len =ntfs_nlstoucs (vol, label,strlen (label), + &uname,FSLABEL_MAX ); +if (uname_len <0 ) { +ntfs_error (vol->sb, +"Failed to convert volume label '%s' to Unicode." , + label); +return uname_len; + } + +if (uname_len >NTFS_MAX_LABEL_LEN ) { +ntfs_error (vol->sb, +"Volume label is too long (max %d characters)." , +NTFS_MAX_LABEL_LEN ); +kvfree (uname); +return -EINVAL ; + } + +mutex_lock (&vol_ni->mrec_lock); + ctx =ntfs_attr_get_search_ctx (vol_ni,NULL ); +if (!ctx) { + ret = -ENOMEM ; + goto out; + } + +if (!ntfs_attr_lookup (AT_VOLUME_NAME ,NULL ,0 ,0 ,0 ,NULL ,0 , + ctx)) +ntfs_attr_record_rm (ctx); +ntfs_attr_put_search_ctx (ctx); + + ret =ntfs_resident_attr_record_add (vol_ni,AT_VOLUME_NAME ,AT_UNNAMED ,0 , + (u8 *)uname, uname_len *sizeof (__le16),0 ); +out: +mutex_unlock (&vol_ni->mrec_lock); +kvfree (uname); +mark_inode_dirty_sync (vol->vol_ino); + +if (ret >=0 ) { +kfree (vol->volume_label); + vol->volume_label =kstrdup (label,GFP_KERNEL ); + ret =0 ; + } +return ret; +} + +/** +* is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector +* @sb: Super block of the device to which @b belongs. +* @b: Boot sector of device @sb to check. +* @silent: If 'true', all output will be silenced. +* +* is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot +* sector. Returns 'true' if it is valid and 'false' if not. +* +* @sb is only needed for warning/error output, i.e. it can be NULL when silent +* is 'true'. +*/ +static bool is_boot_sector_ntfs (const struct super_block *sb, +const struct ntfs_boot_sector *b,const bool silent) +{ +/* +* Check that checksum == sum of u32 values from b to the checksum +* field. If checksum is zero, no checking is done. We will work when +* the checksum test fails, since some utilities update the boot sector +* ignoring the checksum which leaves the checksum out-of-date. We +* report a warning if this is the case. +*/ +if ((void *)b < (void *)&b->checksum && b->checksum && !silent) { +__le32 *u; +u32 i; + +for (i =0 , u = (__le32 *)b; u < (__le32 *)(&b->checksum); ++u) + i +=le32_to_cpup (u); +if (le32_to_cpu (b->checksum) != i) +ntfs_warning (sb,"Invalid boot sector checksum." ); + } +/* Check OEMidentifier is "NTFS " */ +if (b->oem_id != magicNTFS) + goto not_ntfs; +/* Check bytes per sector value is between 256 and 4096. */ +if (le16_to_cpu (b->bpb.bytes_per_sector) <0x100 || +le16_to_cpu (b->bpb.bytes_per_sector) >0x1000 ) + goto not_ntfs; +/* +* Check sectors per cluster value is valid and the cluster size +* is not above the maximum (2MB). +*/ +if (b->bpb.sectors_per_cluster >0x80 && + b->bpb.sectors_per_cluster <0xf4 ) + goto not_ntfs; + +/* Check reserved/unused fields are really zero. */ +if (le16_to_cpu (b->bpb.reserved_sectors) || +le16_to_cpu (b->bpb.root_entries) || +le16_to_cpu (b->bpb.sectors) || +le16_to_cpu (b->bpb.sectors_per_fat) || +le32_to_cpu (b->bpb.large_sectors) || b->bpb.fats) + goto not_ntfs; +/* Check clusters per file mft record value is valid. */ +if ((u8 )b->clusters_per_mft_record <0xe1 || + (u8 )b->clusters_per_mft_record >0xf7 ) +switch (b->clusters_per_mft_record) { +case 1 :case 2 :case 4 :case 8 :case 16 :case 32 :case 64 : +break ; +default : + goto not_ntfs; + } +/* Check clusters per index block value is valid. */ +if ((u8 )b->clusters_per_index_record <0xe1 || + (u8 )b->clusters_per_index_record >0xf7 ) +switch (b->clusters_per_index_record) { +case 1 :case 2 :case 4 :case 8 :case 16 :case 32 :case 64 : +break ; +default : + goto not_ntfs; + } +/* +* Check for valid end of sector marker. We will work without it, but +* many BIOSes will refuse to boot from a bootsector if the magic is +* incorrect, so we emit a warning. +*/ +if (!silent && b->end_of_sector_marker !=cpu_to_le16 (0xaa55 )) +ntfs_warning (sb,"Invalid end of sector marker." ); +return true; +not_ntfs: +return false; +} + +/** +* read_ntfs_boot_sector - read the NTFS boot sector of a device +* @sb: super block of device to read the boot sector from +* @silent: if true, suppress all output +* +* Reads the boot sector from the device and validates it. +*/ +static char *read_ntfs_boot_sector (struct super_block *sb, +const int silent) +{ +char *boot_sector; + + boot_sector =ntfs_malloc_nofs (PAGE_SIZE ); +if (!boot_sector) +return NULL ; + +if (ntfs_dev_read (sb, boot_sector,0 ,PAGE_SIZE )) { +if (!silent) +ntfs_error (sb,"Unable to read primary boot sector." ); +kfree (boot_sector); +return NULL ; + } + +if (!is_boot_sector_ntfs (sb, (struct ntfs_boot_sector *)boot_sector, + silent)) { +if (!silent) +ntfs_error (sb,"Primary boot sector is invalid." ); +kfree (boot_sector); +return NULL ; + } + +return boot_sector; +} + +/** +* parse_ntfs_boot_sector - parse the boot sector and store the data in @vol +* @vol: volume structure to initialise with data from boot sector +* @b: boot sector to parse +* +* Parse the ntfs boot sector @b and store all imporant information therein in +* the ntfs super block @vol. Return 'true' on success and 'false' on error. +*/ +static bool parse_ntfs_boot_sector (struct ntfs_volume *vol, +const struct ntfs_boot_sector *b) +{ +unsigned int sectors_per_cluster, sectors_per_cluster_bits, nr_hidden_sects; +int clusters_per_mft_record, clusters_per_index_record; +s64 ll; + + vol->sector_size =le16_to_cpu (b->bpb.bytes_per_sector); + vol->sector_size_bits =ffs (vol->sector_size) -1 ; +ntfs_debug ("vol->sector_size = %i (0x%x)" , vol->sector_size, + vol->sector_size); +ntfs_debug ("vol->sector_size_bits = %i (0x%x)" , vol->sector_size_bits, + vol->sector_size_bits); +if (vol->sector_size < vol->sb->s_blocksize) { +ntfs_error (vol->sb, +"Sector size (%i) is smaller than the device block size (%lu). This is not supported." , + vol->sector_size, vol->sb->s_blocksize); +return false; + } + +if (b->bpb.sectors_per_cluster >=0xf4 ) + sectors_per_cluster =1U << -(s8 )b->bpb.sectors_per_cluster; +else + sectors_per_cluster = b->bpb.sectors_per_cluster; +ntfs_debug ("sectors_per_cluster = 0x%x" , b->bpb.sectors_per_cluster); + sectors_per_cluster_bits =ffs (sectors_per_cluster) -1 ; +ntfs_debug ("sectors_per_cluster_bits = 0x%x" , + sectors_per_cluster_bits); + nr_hidden_sects =le32_to_cpu (b->bpb.hidden_sectors); +ntfs_debug ("number of hidden sectors = 0x%x" , nr_hidden_sects); + vol->cluster_size = vol->sector_size << sectors_per_cluster_bits; + vol->cluster_size_mask = vol->cluster_size -1 ; + vol->cluster_size_bits =ffs (vol->cluster_size) -1 ; +ntfs_debug ("vol->cluster_size = %i (0x%x)" , vol->cluster_size, + vol->cluster_size); +ntfs_debug ("vol->cluster_size_mask = 0x%x" , vol->cluster_size_mask); +ntfs_debug ("vol->cluster_size_bits = %i" , vol->cluster_size_bits); +if (vol->cluster_size < vol->sector_size) { +ntfs_error (vol->sb, +"Cluster size (%i) is smaller than the sector size (%i). This is not supported." , + vol->cluster_size, vol->sector_size); +return false; + } + clusters_per_mft_record = b->clusters_per_mft_record; +ntfs_debug ("clusters_per_mft_record = %i (0x%x)" , + clusters_per_mft_record, clusters_per_mft_record); +if (clusters_per_mft_record >0 ) + vol->mft_record_size = vol->cluster_size << + (ffs (clusters_per_mft_record) -1 ); +else +/* +* When mft_record_size < cluster_size, clusters_per_mft_record +* = -log2(mft_record_size) bytes. mft_record_size normaly is +* 1024 bytes, which is encoded as 0xF6 (-10 in decimal). +*/ + vol->mft_record_size =1 << -clusters_per_mft_record; + vol->mft_record_size_mask = vol->mft_record_size -1 ; + vol->mft_record_size_bits =ffs (vol->mft_record_size) -1 ; +ntfs_debug ("vol->mft_record_size = %i (0x%x)" , vol->mft_record_size, + vol->mft_record_size); +ntfs_debug ("vol->mft_record_size_mask = 0x%x" , + vol->mft_record_size_mask); +ntfs_debug ("vol->mft_record_size_bits = %i (0x%x)" , + vol->mft_record_size_bits, vol->mft_record_size_bits); +/* +* We cannot support mft record sizes above the PAGE_SIZE since +* we store $MFT/$DATA, the table of mft records in the page cache. +*/ +if (vol->mft_record_size >PAGE_SIZE ) { +ntfs_error (vol->sb, +"Mft record size (%i) exceeds the PAGE_SIZE on your system (%lu). This is not supported." , + vol->mft_record_size,PAGE_SIZE ); +return false; + } +/* We cannot support mft record sizes below the sector size. */ +if (vol->mft_record_size < vol->sector_size) { +ntfs_warning (vol->sb,"Mft record size (%i) is smaller than the sector size (%i)." , + vol->mft_record_size, vol->sector_size); + } + clusters_per_index_record = b->clusters_per_index_record; +ntfs_debug ("clusters_per_index_record = %i (0x%x)" , + clusters_per_index_record, clusters_per_index_record); +if (clusters_per_index_record >0 ) + vol->index_record_size = vol->cluster_size << + (ffs (clusters_per_index_record) -1 ); +else +/* +* When index_record_size < cluster_size, +* clusters_per_index_record = -log2(index_record_size) bytes. +* index_record_size normaly equals 4096 bytes, which is +* encoded as 0xF4 (-12 in decimal). +*/ + vol->index_record_size =1 << -clusters_per_index_record; + vol->index_record_size_mask = vol->index_record_size -1 ; + vol->index_record_size_bits =ffs (vol->index_record_size) -1 ; +ntfs_debug ("vol->index_record_size = %i (0x%x)" , + vol->index_record_size, vol->index_record_size); +ntfs_debug ("vol->index_record_size_mask = 0x%x" , + vol->index_record_size_mask); +ntfs_debug ("vol->index_record_size_bits = %i (0x%x)" , + vol->index_record_size_bits, + vol->index_record_size_bits); +/* We cannot support index record sizes below the sector size. */ +if (vol->index_record_size < vol->sector_size) { +ntfs_error (vol->sb, +"Index record size (%i) is smaller than the sector size (%i). This is not supported." , + vol->index_record_size, vol->sector_size); +return false; + } +/* +* Get the size of the volume in clusters and check for 64-bit-ness. +* Windows currently only uses 32 bits to save the clusters so we do +* the same as it is much faster on 32-bit CPUs. +*/ + ll =le64_to_cpu (b->number_of_sectors) >> sectors_per_cluster_bits; +if ((u64 )ll >=1ULL <<32 ) { +ntfs_error (vol->sb,"Cannot handle 64-bit clusters." ); +return false; + } + vol->nr_clusters = ll; +ntfs_debug ("vol->nr_clusters = 0x%llx" , vol->nr_clusters); +/* +* On an architecture where unsigned long is 32-bits, we restrict the +* volume size to 2TiB (2^41). On a 64-bit architecture, the compiler +* will hopefully optimize the whole check away. +*/ +if (sizeof (unsigned long ) <8 ) { +if ((ll << vol->cluster_size_bits) >= (1ULL <<41 )) { +ntfs_error (vol->sb, +"Volume size (%lluTiB) is too large for this architecture. Maximum supported is 2TiB." , + ll >> (40 - vol->cluster_size_bits)); +return false; + } + } + ll =le64_to_cpu (b->mft_lcn); +if (ll >= vol->nr_clusters) { +ntfs_error (vol->sb,"MFT LCN (%lli, 0x%llx) is beyond end of volume. Weird." , + ll, ll); +return false; + } + vol->mft_lcn = ll; +ntfs_debug ("vol->mft_lcn = 0x%llx" , vol->mft_lcn); + ll =le64_to_cpu (b->mftmirr_lcn); +if (ll >= vol->nr_clusters) { +ntfs_error (vol->sb,"MFTMirr LCN (%lli, 0x%llx) is beyond end of volume. Weird." , + ll, ll); +return false; + } + vol->mftmirr_lcn = ll; +ntfs_debug ("vol->mftmirr_lcn = 0x%llx" , vol->mftmirr_lcn); +/* +* Work out the size of the mft mirror in number of mft records. If the +* cluster size is less than or equal to the size taken by four mft +* records, the mft mirror stores the first four mft records. If the +* cluster size is bigger than the size taken by four mft records, the +* mft mirror contains as many mft records as will fit into one +* cluster. +*/ +if (vol->cluster_size <= (4 << vol->mft_record_size_bits)) + vol->mftmirr_size =4 ; +else + vol->mftmirr_size = vol->cluster_size >> + vol->mft_record_size_bits; +ntfs_debug ("vol->mftmirr_size = %i" , vol->mftmirr_size); + vol->serial_no =le64_to_cpu (b->volume_serial_number); +ntfs_debug ("vol->serial_no = 0x%llx" , vol->serial_no); + + vol->sparse_compression_unit =4 ; +if (vol->cluster_size >4096 ) { +switch (vol->cluster_size) { +case 65536 : + vol->sparse_compression_unit =0 ; +break ; +case 32768 : + vol->sparse_compression_unit =1 ; +break ; +case 16384 : + vol->sparse_compression_unit =2 ; +break ; +case 8192 : + vol->sparse_compression_unit =3 ; +break ; + } + } + +return true; +} + +/** +* ntfs_setup_allocators - initialize the cluster and mft allocators +* @vol: volume structure for which to setup the allocators +* +* Setup the cluster (lcn) and mft allocators to the starting values. +*/ +static void ntfs_setup_allocators (struct ntfs_volume *vol) +{ +s64 mft_zone_size, mft_lcn; + +ntfs_debug ("vol->mft_zone_multiplier = 0x%x" , + vol->mft_zone_multiplier); +/* Determine the size of the MFT zone. */ + mft_zone_size = vol->nr_clusters; +switch (vol->mft_zone_multiplier) {/* % of volume size in clusters */ +case 4 : + mft_zone_size >>=1 ;/* 50% */ +break ; +case 3 : + mft_zone_size = (mft_zone_size + + (mft_zone_size >>1 )) >>2 ;/* 37.5% */ +break ; +case 2 : + mft_zone_size >>=2 ;/* 25% */ +break ; +/* case 1: */ +default : + mft_zone_size >>=3 ;/* 12.5% */ +break ; + } +/* Setup the mft zone. */ + vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn; +ntfs_debug ("vol->mft_zone_pos = 0x%llx" , vol->mft_zone_pos); +/* +* Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs +* source) and if the actual mft_lcn is in the expected place or even +* further to the front of the volume, extend the mft_zone to cover the +* beginning of the volume as well. This is in order to protect the +* area reserved for the mft bitmap as well within the mft_zone itself. +* On non-standard volumes we do not protect it as the overhead would +* be higher than the speed increase we would get by doing it. +*/ + mft_lcn = (8192 +2 * vol->cluster_size -1 ) >> vol->cluster_size_bits; +if (mft_lcn * vol->cluster_size <16 *1024 ) + mft_lcn = (16 *1024 + vol->cluster_size -1 ) >> + vol->cluster_size_bits; +if (vol->mft_zone_start <= mft_lcn) + vol->mft_zone_start =0 ; +ntfs_debug ("vol->mft_zone_start = 0x%llx" , vol->mft_zone_start); +/* +* Need to cap the mft zone on non-standard volumes so that it does +* not point outside the boundaries of the volume. We do this by +* halving the zone size until we are inside the volume. +*/ + vol->mft_zone_end = vol->mft_lcn + mft_zone_size; +while (vol->mft_zone_end >= vol->nr_clusters) { + mft_zone_size >>=1 ; + vol->mft_zone_end = vol->mft_lcn + mft_zone_size; + } +ntfs_debug ("vol->mft_zone_end = 0x%llx" , vol->mft_zone_end); +/* +* Set the current position within each data zone to the start of the +* respective zone. +*/ + vol->data1_zone_pos = vol->mft_zone_end; +ntfs_debug ("vol->data1_zone_pos = 0x%llx" , vol->data1_zone_pos); + vol->data2_zone_pos =0 ; +ntfs_debug ("vol->data2_zone_pos = 0x%llx" , vol->data2_zone_pos); + +/* Set the mft data allocation position to mft record 24. */ + vol->mft_data_pos =24 ; +ntfs_debug ("vol->mft_data_pos = 0x%llx" , vol->mft_data_pos); +} + +static struct lock_class_key mftmirr_runlist_lock_key, + mftmirr_mrec_lock_key; +/** +* load_and_init_mft_mirror - load and setup the mft mirror inode for a volume +* @vol: ntfs super block describing device whose mft mirror to load +* +* Return 'true' on success or 'false' on error. +*/ +static bool load_and_init_mft_mirror (struct ntfs_volume *vol) +{ +struct inode *tmp_ino; +struct ntfs_inode *tmp_ni; + +ntfs_debug ("Entering." ); +/* Get mft mirror inode. */ + tmp_ino =ntfs_iget (vol->sb, FILE_MFTMirr); +if (IS_ERR (tmp_ino)) { +if (!IS_ERR (tmp_ino)) +iput (tmp_ino); +/* Caller will display error message. */ +return false; + } +lockdep_set_class (&NTFS_I (tmp_ino)->runlist.lock, + &mftmirr_runlist_lock_key); +lockdep_set_class (&NTFS_I (tmp_ino)->mrec_lock, + &mftmirr_mrec_lock_key); +/* +* Re-initialize some specifics about $MFTMirr's inode as +* ntfs_read_inode() will have set up the default ones. +*/ +/* Set uid and gid to root. */ + tmp_ino->i_uid =GLOBAL_ROOT_UID ; + tmp_ino->i_gid =GLOBAL_ROOT_GID ; +/* Regular file. No access for anyone. */ + tmp_ino->i_mode =S_IFREG ; +/* No VFS initiated operations allowed for $MFTMirr. */ + tmp_ino->i_op = &ntfs_empty_inode_ops; + tmp_ino->i_fop = &ntfs_empty_file_ops; +/* Put in our special address space operations. */ + tmp_ino->i_mapping->a_ops = &ntfs_mst_aops; + tmp_ni =NTFS_I (tmp_ino); +/* The $MFTMirr, like the $MFT is multi sector transfer protected. */ +NInoSetMstProtected (tmp_ni); +NInoSetSparseDisabled (tmp_ni); +/* +* Set up our little cheat allowing us to reuse the async read io +* completion handler for directories. +*/ + tmp_ni->itype.index.block_size = vol->mft_record_size; + tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits; + vol->mftmirr_ino = tmp_ino; +ntfs_debug ("Done." ); +return true; +} + +/** +* check_mft_mirror - compare contents of the mft mirror with the mft +* @vol: ntfs super block describing device whose mft mirror to check +* +* Return 'true' on success or 'false' on error. +* +* Note, this function also results in the mft mirror runlist being completely +* mapped into memory. The mft mirror write code requires this and will BUG() +* should it find an unmapped runlist element. +*/ +static bool check_mft_mirror (struct ntfs_volume *vol) +{ +struct super_block *sb = vol->sb; +struct ntfs_inode *mirr_ni; +struct folio *mft_folio =NULL , *mirr_folio =NULL ; +u8 *kmft =NULL , *kmirr =NULL ; +struct runlist_element *rl, rl2[2 ]; +pgoff_t index; +int mrecs_per_page, i; + +ntfs_debug ("Entering." ); +/* Compare contents of $MFT and $MFTMirr. */ + mrecs_per_page =PAGE_SIZE / vol->mft_record_size; + index = i =0 ; +do { +u32 bytes; + +/* Switch pages if necessary. */ +if (!(i % mrecs_per_page)) { +if (index) { +ntfs_unmap_folio (mirr_folio, kmirr); +ntfs_unmap_folio (mft_folio, kmft); + } +/* Get the $MFT page. */ + mft_folio =ntfs_read_mapping_folio (vol->mft_ino->i_mapping, + index); +if (IS_ERR (mft_folio)) { +ntfs_error (sb,"Failed to read $MFT." ); +return false; + } + kmft =kmap_local_folio (mft_folio,0 ); +/* Get the $MFTMirr page. */ + mirr_folio =ntfs_read_mapping_folio (vol->mftmirr_ino->i_mapping, + index); +if (IS_ERR (mirr_folio)) { +ntfs_error (sb,"Failed to read $MFTMirr." ); + goto mft_unmap_out; + } + kmirr =kmap_local_folio (mirr_folio,0 ); + ++index; + } + +/* Do not check the record if it is not in use. */ +if (((struct mft_record *)kmft)->flags &MFT_RECORD_IN_USE ) { +/* Make sure the record is ok. */ +if (ntfs_is_baad_recordp ((__le32 *)kmft)) { +ntfs_error (sb, +"Incomplete multi sector transfer detected in mft record %i." , + i); +mm_unmap_out: +ntfs_unmap_folio (mirr_folio, kmirr); +mft_unmap_out: +ntfs_unmap_folio (mft_folio, kmft); +return false; + } + } +/* Do not check the mirror record if it is not in use. */ +if (((struct mft_record *)kmirr)->flags &MFT_RECORD_IN_USE ) { +if (ntfs_is_baad_recordp ((__le32 *)kmirr)) { +ntfs_error (sb, +"Incomplete multi sector transfer detected in mft mirror record %i." , + i); + goto mm_unmap_out; + } + } +/* Get the amount of data in the current record. */ + bytes =le32_to_cpu (((struct mft_record *)kmft)->bytes_in_use); +if (bytes <sizeof (struct mft_record_old ) || + bytes > vol->mft_record_size || +ntfs_is_baad_recordp ((__le32 *)kmft)) { + bytes =le32_to_cpu (((struct mft_record *)kmirr)->bytes_in_use); +if (bytes <sizeof (struct mft_record_old ) || + bytes > vol->mft_record_size || +ntfs_is_baad_recordp ((__le32 *)kmirr)) + bytes = vol->mft_record_size; + } + kmft += vol->mft_record_size; + kmirr += vol->mft_record_size; + }while (++i < vol->mftmirr_size); +/* Release the last folios. */ +ntfs_unmap_folio (mirr_folio, kmirr); +ntfs_unmap_folio (mft_folio, kmft); + +/* Construct the mft mirror runlist by hand. */ + rl2[0 ].vcn =0 ; + rl2[0 ].lcn = vol->mftmirr_lcn; + rl2[0 ].length = (vol->mftmirr_size * vol->mft_record_size + + vol->cluster_size -1 ) >> vol->cluster_size_bits; + rl2[1 ].vcn = rl2[0 ].length; + rl2[1 ].lcn =LCN_ENOENT ; + rl2[1 ].length =0 ; +/* +* Because we have just read all of the mft mirror, we know we have +* mapped the full runlist for it. +*/ + mirr_ni =NTFS_I (vol->mftmirr_ino); +down_read (&mirr_ni->runlist.lock); + rl = mirr_ni->runlist.rl; +/* Compare the two runlists. They must be identical. */ + i =0 ; +do { +if (rl2[i].vcn != rl[i].vcn || rl2[i].lcn != rl[i].lcn || + rl2[i].length != rl[i].length) { +ntfs_error (sb,"$MFTMirr location mismatch. Run chkdsk." ); +up_read (&mirr_ni->runlist.lock); +return false; + } + }while (rl2[i++].length); +up_read (&mirr_ni->runlist.lock); +ntfs_debug ("Done." ); +return true; +} + +/** +* load_and_check_logfile - load and check the logfile inode for a volume +* +* Return 0 on success or errno on error. +*/ +static int load_and_check_logfile (struct ntfs_volume *vol, +struct restart_page_header **rp) +{ +struct inode *tmp_ino; +int err =0 ; + +ntfs_debug ("Entering." ); + tmp_ino =ntfs_iget (vol->sb, FILE_LogFile); +if (IS_ERR (tmp_ino)) { +if (!IS_ERR (tmp_ino)) +iput (tmp_ino); +/* Caller will display error message. */ +return -ENOENT ; + } +if (!ntfs_check_logfile (tmp_ino, rp)) + err = -EINVAL ; +NInoSetSparseDisabled (NTFS_I (tmp_ino)); + vol->logfile_ino = tmp_ino; +ntfs_debug ("Done." ); +return err; +} + +#define NTFS_HIBERFIL_HEADER_SIZE 4096 + +/** +* check_windows_hibernation_status - check if Windows is suspended on a volume +* @vol: ntfs super block of device to check +* +* Check if Windows is hibernated on the ntfs volume @vol. This is done by +* looking for the file hiberfil.sys in the root directory of the volume. If +* the file is not present Windows is definitely not suspended. +* +* If hiberfil.sys exists and is less than 4kiB in size it means Windows is +* definitely suspended (this volume is not the system volume). Caveat: on a +* system with many volumes it is possible that the < 4kiB check is bogus but +* for now this should do fine. +* +* If hiberfil.sys exists and is larger than 4kiB in size, we need to read the +* hiberfil header (which is the first 4kiB). If this begins with "hibr", +* Windows is definitely suspended. If it is completely full of zeroes, +* Windows is definitely not hibernated. Any other case is treated as if +* Windows is suspended. This caters for the above mentioned caveat of a +* system with many volumes where no "hibr" magic would be present and there is +* no zero header. +* +* Return 0 if Windows is not hibernated on the volume, >0 if Windows is +* hibernated on the volume, and -errno on error. +*/ +static int check_windows_hibernation_status (struct ntfs_volume *vol) +{ +static const __le16 hiberfil[13 ] = {cpu_to_le16 ('h' ), +cpu_to_le16 ('i' ),cpu_to_le16 ('b' ), +cpu_to_le16 ('e' ),cpu_to_le16 ('r' ), +cpu_to_le16 ('f' ),cpu_to_le16 ('i' ), +cpu_to_le16 ('l' ),cpu_to_le16 ('.' ), +cpu_to_le16 ('s' ),cpu_to_le16 ('y' ), +cpu_to_le16 ('s' ),0 }; +u64 mref; +struct inode *vi; +struct folio *folio; +u32 *kaddr, *kend, *start_addr =NULL ; +struct ntfs_name *name =NULL ; +int ret =1 ; + +ntfs_debug ("Entering." ); +/* +* Find the inode number for the hibernation file by looking up the +* filename hiberfil.sys in the root directory. +*/ +inode_lock (vol->root_ino); + mref =ntfs_lookup_inode_by_name (NTFS_I (vol->root_ino), hiberfil,12 , + &name); +inode_unlock (vol->root_ino); +kfree (name); +if (IS_ERR_MREF (mref)) { + ret =MREF_ERR (mref); +/* If the file does not exist, Windows is not hibernated. */ +if (ret == -ENOENT ) { +ntfs_debug ("hiberfil.sys not present. Windows is not hibernated on the volume." ); +return 0 ; + } +/* A real error occurred. */ +ntfs_error (vol->sb,"Failed to find inode number for hiberfil.sys." ); +return ret; + } +/* Get the inode. */ + vi =ntfs_iget (vol->sb,MREF (mref)); +if (IS_ERR (vi)) { +if (!IS_ERR (vi)) +iput (vi); +ntfs_error (vol->sb,"Failed to load hiberfil.sys." ); +return IS_ERR (vi) ?PTR_ERR (vi) : -EIO ; + } +if (unlikely (i_size_read (vi) <NTFS_HIBERFIL_HEADER_SIZE )) { +ntfs_debug ("hiberfil.sys is smaller than 4kiB (0x%llx). Windows is hibernated on the volume. This is not the system volume." , +i_size_read (vi)); + goto iput_out; + } + + folio =ntfs_read_mapping_folio (vi->i_mapping,0 ); +if (IS_ERR (folio)) { +ntfs_error (vol->sb,"Failed to read from hiberfil.sys." ); + ret =PTR_ERR (folio); + goto iput_out; + } + start_addr = (u32 *)kmap_local_folio (folio,0 ); + kaddr = start_addr; +if (*(__le32 *)kaddr ==cpu_to_le32 (0x72626968 )/*'hibr'*/ ) { +ntfs_debug ("Magic \"hibr\" found in hiberfil.sys. Windows is hibernated on the volume. This is the system volume." ); + goto unm_iput_out; + } + kend = kaddr +NTFS_HIBERFIL_HEADER_SIZE /sizeof (*kaddr); +do { +if (unlikely (*kaddr)) { +ntfs_debug ("hiberfil.sys is larger than 4kiB (0x%llx), does not contain the \"hibr\" magic, and does not have a zero header. Windows is hibernated on the volume. This is not the system volume." , +i_size_read (vi)); + goto unm_iput_out; + } + }while (++kaddr < kend); +ntfs_debug ("hiberfil.sys contains a zero header. Windows is not hibernated on the volume. This is the system volume." ); + ret =0 ; +unm_iput_out: +ntfs_unmap_folio (folio, start_addr); +iput_out: +iput (vi); +return ret; +} + +/** +* load_and_init_quota - load and setup the quota file for a volume if present +* @vol: ntfs super block describing device whose quota file to load +* +* Return 'true' on success or 'false' on error. If $Quota is not present, we +* leave vol->quota_ino as NULL and return success. +*/ +static bool load_and_init_quota (struct ntfs_volume *vol) +{ +static const __le16 Quota[7 ] = {cpu_to_le16 ('$' ), +cpu_to_le16 ('Q' ),cpu_to_le16 ('u' ), +cpu_to_le16 ('o' ),cpu_to_le16 ('t' ), +cpu_to_le16 ('a' ),0 }; +static __le16 Q [3 ] = {cpu_to_le16 ('$' ), +cpu_to_le16 ('Q' ),0 }; +struct ntfs_name *name =NULL ; +u64 mref; +struct inode *tmp_ino; + +ntfs_debug ("Entering." ); +/* +* Find the inode number for the quota file by looking up the filename +* $Quota in the extended system files directory $Extend. +*/ +inode_lock (vol->extend_ino); + mref =ntfs_lookup_inode_by_name (NTFS_I (vol->extend_ino), Quota,6 , + &name); +inode_unlock (vol->extend_ino); +kfree (name); +if (IS_ERR_MREF (mref)) { +/* +* If the file does not exist, quotas are disabled and have +* never been enabled on this volume, just return success. +*/ +if (MREF_ERR (mref) == -ENOENT ) { +ntfs_debug ("$Quota not present. Volume does not have quotas enabled." ); +/* +* No need to try to set quotas out of date if they are +* not enabled. +*/ +NVolSetQuotaOutOfDate (vol); +return true; + } +/* A real error occurred. */ +ntfs_error (vol->sb,"Failed to find inode number for $Quota." ); +return false; + } +/* Get the inode. */ + tmp_ino =ntfs_iget (vol->sb,MREF (mref)); +if (IS_ERR (tmp_ino)) { +if (!IS_ERR (tmp_ino)) +iput (tmp_ino); +ntfs_error (vol->sb,"Failed to load $Quota." ); +return false; + } + vol->quota_ino = tmp_ino; +/* Get the $Q index allocation attribute. */ + tmp_ino =ntfs_index_iget (vol->quota_ino,Q ,2 ); +if (IS_ERR (tmp_ino)) { +ntfs_error (vol->sb,"Failed to load $Quota/$Q index." ); +return false; + } + vol->quota_q_ino = tmp_ino; +ntfs_debug ("Done." ); +return true; +} + +/** +* load_and_init_attrdef - load the attribute definitions table for a volume +* @vol: ntfs super block describing device whose attrdef to load +* +* Return 'true' on success or 'false' on error. +*/ +static bool load_and_init_attrdef (struct ntfs_volume *vol) +{ +loff_t i_size; +struct super_block *sb = vol->sb; +struct inode *ino; +struct folio *folio; +u8 *addr; +pgoff_t index, max_index; +unsigned int size; + +ntfs_debug ("Entering." ); +/* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */ + ino =ntfs_iget (sb, FILE_AttrDef); +if (IS_ERR (ino)) { +if (!IS_ERR (ino)) +iput (ino); + goto failed; + } +NInoSetSparseDisabled (NTFS_I (ino)); +/* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */ + i_size =i_size_read (ino); +if (i_size <=0 || i_size >0x7fffffff ) + goto iput_failed; + vol->attrdef = (struct attr_def *)ntfs_malloc_nofs (i_size); +if (!vol->attrdef) + goto iput_failed; + index =0 ; + max_index = i_size >>PAGE_SHIFT ; + size =PAGE_SIZE ; +while (index < max_index) { +/* Read the attrdef table and copy it into the linear buffer. */ +read_partial_attrdef_page: + folio =ntfs_read_mapping_folio (ino->i_mapping, index); +if (IS_ERR (folio)) + goto free_iput_failed; + addr =kmap_local_folio (folio,0 ); +memcpy ((u8 *)vol->attrdef + (index++ <<PAGE_SHIFT ), + addr, size); +ntfs_unmap_folio (folio, addr); + } +if (size ==PAGE_SIZE ) { + size = i_size & ~PAGE_MASK ; +if (size) + goto read_partial_attrdef_page; + } + vol->attrdef_size = i_size; +ntfs_debug ("Read %llu bytes from $AttrDef." , i_size); +iput (ino); +return true; +free_iput_failed: +ntfs_free (vol->attrdef); + vol->attrdef =NULL ; +iput_failed: +iput (ino); +failed: +ntfs_error (sb,"Failed to initialize attribute definition table." ); +return false; +} + +/** +* load_and_init_upcase - load the upcase table for an ntfs volume +* @vol: ntfs super block describing device whose upcase to load +* +* Return 'true' on success or 'false' on error. +*/ +static bool load_and_init_upcase (struct ntfs_volume *vol) +{ +loff_t i_size; +struct super_block *sb = vol->sb; +struct inode *ino; +struct folio *folio; +u8 *addr; +pgoff_t index, max_index; +unsigned int size; +int i, max; + +ntfs_debug ("Entering." ); +/* Read upcase table and setup vol->upcase and vol->upcase_len. */ + ino =ntfs_iget (sb, FILE_UpCase); +if (IS_ERR (ino)) { +if (!IS_ERR (ino)) +iput (ino); + goto upcase_failed; + } +/* +* The upcase size must not be above 64k Unicode characters, must not +* be zero and must be a multiple of sizeof(__le16). +*/ + i_size =i_size_read (ino); +if (!i_size || i_size & (sizeof (__le16) -1 ) || + i_size >64ULL *1024 *sizeof (__le16)) + goto iput_upcase_failed; + vol->upcase = (__le16 *)ntfs_malloc_nofs (i_size); +if (!vol->upcase) + goto iput_upcase_failed; + index =0 ; + max_index = i_size >>PAGE_SHIFT ; + size =PAGE_SIZE ; +while (index < max_index) { +/* Read the upcase table and copy it into the linear buffer. */ +read_partial_upcase_page: + folio =ntfs_read_mapping_folio (ino->i_mapping, index); +if (IS_ERR (folio)) + goto iput_upcase_failed; + addr =kmap_local_folio (folio,0 ); +memcpy ((char *)vol->upcase + (index++ <<PAGE_SHIFT ), + addr, size); +ntfs_unmap_folio (folio, addr); + }; +if (size ==PAGE_SIZE ) { + size = i_size & ~PAGE_MASK ; +if (size) + goto read_partial_upcase_page; + } + vol->upcase_len = i_size >>UCHAR_T_SIZE_BITS ; +ntfs_debug ("Read %llu bytes from $UpCase (expected %zu bytes)." , + i_size,64 *1024 *sizeof (__le16)); +iput (ino); +mutex_lock (&ntfs_lock); +if (!default_upcase) { +ntfs_debug ("Using volume specified $UpCase since default is not present." ); +mutex_unlock (&ntfs_lock); +return true; + } + max = default_upcase_len; +if (max > vol->upcase_len) + max = vol->upcase_len; +for (i =0 ; i < max; i++) +if (vol->upcase[i] != default_upcase[i]) +break ; +if (i == max) { +ntfs_free (vol->upcase); + vol->upcase = default_upcase; + vol->upcase_len = max; + ntfs_nr_upcase_users++; +mutex_unlock (&ntfs_lock); +ntfs_debug ("Volume specified $UpCase matches default. Using default." ); +return true; + } +mutex_unlock (&ntfs_lock); +ntfs_debug ("Using volume specified $UpCase since it does not match the default." ); +return true; +iput_upcase_failed: +iput (ino); +ntfs_free (vol->upcase); + vol->upcase =NULL ; +upcase_failed: +mutex_lock (&ntfs_lock); +if (default_upcase) { + vol->upcase = default_upcase; + vol->upcase_len = default_upcase_len; + ntfs_nr_upcase_users++; +mutex_unlock (&ntfs_lock); +ntfs_error (sb,"Failed to load $UpCase from the volume. Using default." ); +return true; + } +mutex_unlock (&ntfs_lock); +ntfs_error (sb,"Failed to initialize upcase table." ); +return false; +} + +/* +* The lcn and mft bitmap inodes are NTFS-internal inodes with +* their own special locking rules: +*/ +static struct lock_class_key + lcnbmp_runlist_lock_key, lcnbmp_mrec_lock_key, + mftbmp_runlist_lock_key, mftbmp_mrec_lock_key; + +/** +* load_system_files - open the system files using normal functions +* @vol: ntfs super block describing device whose system files to load +* +* Open the system files with normal access functions and complete setting up +* the ntfs super block @vol. +* +* Return 'true' on success or 'false' on error. +*/ +static bool load_system_files (struct ntfs_volume *vol) +{ +struct super_block *sb = vol->sb; +struct mft_record *m; +struct volume_information *vi; +struct ntfs_attr_search_ctx *ctx; +struct restart_page_header *rp; +int err; + +ntfs_debug ("Entering." ); +/* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */ +if (!load_and_init_mft_mirror (vol) || !check_mft_mirror (vol)) { +/* If a read-write mount, convert it to a read-only mount. */ +if (!sb_rdonly (sb) && vol->on_errors ==ON_ERRORS_REMOUNT_RO ) { +static const char *es1 ="Failed to load $MFTMirr" ; +static const char *es2 ="$MFTMirr does not match $MFT" ; +static const char *es3 =". Run ntfsck and/or chkdsk." ; + + sb->s_flags |=SB_RDONLY ; +ntfs_error (sb,"%s. Mounting read-only%s" , + !vol->mftmirr_ino ? es1 : es2, es3); + } +NVolSetErrors (vol); + } +/* Get mft bitmap attribute inode. */ + vol->mftbmp_ino =ntfs_attr_iget (vol->mft_ino,AT_BITMAP ,NULL ,0 ); +if (IS_ERR (vol->mftbmp_ino)) { +ntfs_error (sb,"Failed to load $MFT/$BITMAP attribute." ); + goto iput_mirr_err_out; + } +lockdep_set_class (&NTFS_I (vol->mftbmp_ino)->runlist.lock, + &mftbmp_runlist_lock_key); +lockdep_set_class (&NTFS_I (vol->mftbmp_ino)->mrec_lock, + &mftbmp_mrec_lock_key); +/* Read upcase table and setup @vol->upcase and @vol->upcase_len. */ +if (!load_and_init_upcase (vol)) + goto iput_mftbmp_err_out; +/* +* Read attribute definitions table and setup @vol->attrdef and +* @vol->attrdef_size. +*/ +if (!load_and_init_attrdef (vol)) + goto iput_upcase_err_out; +/* +* Get the cluster allocation bitmap inode and verify the size, no +* need for any locking at this stage as we are already running +* exclusively as we are mount in progress task. +*/ + vol->lcnbmp_ino =ntfs_iget (sb, FILE_Bitmap); +if (IS_ERR (vol->lcnbmp_ino)) { +if (!IS_ERR (vol->lcnbmp_ino)) +iput (vol->lcnbmp_ino); + goto bitmap_failed; + } +lockdep_set_class (&NTFS_I (vol->lcnbmp_ino)->runlist.lock, + &lcnbmp_runlist_lock_key); +lockdep_set_class (&NTFS_I (vol->lcnbmp_ino)->mrec_lock, + &lcnbmp_mrec_lock_key); + +NInoSetSparseDisabled (NTFS_I (vol->lcnbmp_ino)); +if ((vol->nr_clusters +7 ) >>3 >i_size_read (vol->lcnbmp_ino)) { +iput (vol->lcnbmp_ino); +bitmap_failed: +ntfs_error (sb,"Failed to load $Bitmap." ); + goto iput_attrdef_err_out; + } +/* +* Get the volume inode and setup our cache of the volume flags and +* version. +*/ + vol->vol_ino =ntfs_iget (sb, FILE_Volume); +if (IS_ERR (vol->vol_ino)) { +if (!IS_ERR (vol->vol_ino)) +iput (vol->vol_ino); +volume_failed: +ntfs_error (sb,"Failed to load $Volume." ); + goto iput_lcnbmp_err_out; + } + m =map_mft_record (NTFS_I (vol->vol_ino)); +if (IS_ERR (m)) { +iput_volume_failed: +iput (vol->vol_ino); + goto volume_failed; + } + + ctx =ntfs_attr_get_search_ctx (NTFS_I (vol->vol_ino), m); +if (!ctx) { +ntfs_error (sb,"Failed to get attribute search context." ); + goto get_ctx_vol_failed; + } + +if (!ntfs_attr_lookup (AT_VOLUME_NAME ,NULL ,0 ,0 ,0 ,NULL ,0 , ctx) && + !ctx->attr->non_resident && + !(ctx->attr->flags & (ATTR_IS_SPARSE |ATTR_IS_COMPRESSED )) && +le32_to_cpu (ctx->attr->data.resident.value_length) >0 ) { + err =ntfs_ucstonls (vol, (__le16 *)((u8 *)ctx->attr + +le16_to_cpu (ctx->attr->data.resident.value_offset)), +le32_to_cpu (ctx->attr->data.resident.value_length) /2 , + &vol->volume_label,NTFS_MAX_LABEL_LEN ); +if (err <0 ) + vol->volume_label =NULL ; + } + +if (ntfs_attr_lookup (AT_VOLUME_INFORMATION ,NULL ,0 ,0 ,0 ,NULL ,0 , + ctx) || ctx->attr->non_resident || ctx->attr->flags) { +err_put_vol: +ntfs_attr_put_search_ctx (ctx); +get_ctx_vol_failed: +unmap_mft_record (NTFS_I (vol->vol_ino)); + goto iput_volume_failed; + } + vi = (struct volume_information *)((char *)ctx->attr + +le16_to_cpu (ctx->attr->data.resident.value_offset)); +/* Some bounds checks. */ +if ((u8 *)vi < (u8 *)ctx->attr || (u8 *)vi + +le32_to_cpu (ctx->attr->data.resident.value_length) > + (u8 *)ctx->attr +le32_to_cpu (ctx->attr->length)) + goto err_put_vol; +/* Copy the volume flags and version to the struct ntfs_volume structure. */ + vol->vol_flags = vi->flags; + vol->major_ver = vi->major_ver; + vol->minor_ver = vi->minor_ver; +ntfs_attr_put_search_ctx (ctx); +unmap_mft_record (NTFS_I (vol->vol_ino)); +pr_info ("volume version %i.%i, dev %s, cluster size %d\n" , + vol->major_ver, vol->minor_ver, sb->s_id, vol->cluster_size); + +/* Make sure that no unsupported volume flags are set. */ +if (vol->vol_flags &VOLUME_MUST_MOUNT_RO_MASK ) { +static const char *es1a ="Volume is dirty" ; +static const char *es1b ="Volume has been modified by chkdsk" ; +static const char *es1c ="Volume has unsupported flags set" ; +static const char *es2a =". Run chkdsk and mount in Windows." ; +static const char *es2b =". Mount in Windows." ; +const char *es1, *es2; + + es2 = es2a; +if (vol->vol_flags &VOLUME_IS_DIRTY ) + es1 = es1a; +else if (vol->vol_flags &VOLUME_MODIFIED_BY_CHKDSK ) { + es1 = es1b; + es2 = es2b; + }else { + es1 = es1c; +ntfs_warning (sb,"Unsupported volume flags 0x%x encountered." , + (unsigned int )le16_to_cpu (vol->vol_flags)); + } +/* If a read-write mount, convert it to a read-only mount. */ +if (!sb_rdonly (sb) && vol->on_errors ==ON_ERRORS_REMOUNT_RO ) { + sb->s_flags |=SB_RDONLY ; +ntfs_error (sb,"%s. Mounting read-only%s" , es1, es2); + } +/* +* Do not set NVolErrors() because ntfs_remount() re-checks the +* flags which we need to do in case any flags have changed. +*/ + } +/* +* Get the inode for the logfile, check it and determine if the volume +* was shutdown cleanly. +*/ + rp =NULL ; + err =load_and_check_logfile (vol, &rp); +if (err) { +/* If a read-write mount, convert it to a read-only mount. */ +if (!sb_rdonly (sb) && vol->on_errors ==ON_ERRORS_REMOUNT_RO ) { + sb->s_flags |=SB_RDONLY ; +ntfs_error (sb,"Failed to load LogFile. Mounting read-only." ); + } +NVolSetErrors (vol); + } + +ntfs_free (rp); +/* Get the root directory inode so we can do path lookups. */ + vol->root_ino =ntfs_iget (sb, FILE_root); +if (IS_ERR (vol->root_ino)) { +if (!IS_ERR (vol->root_ino)) +iput (vol->root_ino); +ntfs_error (sb,"Failed to load root directory." ); + goto iput_logfile_err_out; + } +/* +* Check if Windows is suspended to disk on the target volume. If it +* is hibernated, we must not write *anything* to the disk so set +* NVolErrors() without setting the dirty volume flag and mount +* read-only. This will prevent read-write remounting and it will also +* prevent all writes. +*/ + err =check_windows_hibernation_status (vol); +if (unlikely (err)) { +static const char *es1a ="Failed to determine if Windows is hibernated" ; +static const char *es1b ="Windows is hibernated" ; +static const char *es2 =". Run chkdsk." ; +const char *es1; + + es1 = err <0 ? es1a : es1b; +/* If a read-write mount, convert it to a read-only mount. */ +if (!sb_rdonly (sb) && vol->on_errors ==ON_ERRORS_REMOUNT_RO ) { + sb->s_flags |=SB_RDONLY ; +ntfs_error (sb,"%s. Mounting read-only%s" , es1, es2); + } +NVolSetErrors (vol); + } + +/* If (still) a read-write mount, empty the logfile. */ +if (!sb_rdonly (sb) && + vol->logfile_ino && !ntfs_empty_logfile (vol->logfile_ino) && + vol->on_errors ==ON_ERRORS_REMOUNT_RO ) { +static const char *es1 ="Failed to empty LogFile" ; +static const char *es2 =". Mount in Windows." ; + +/* Convert to a read-only mount. */ +ntfs_error (sb,"%s. Mounting read-only%s" , es1, es2); + sb->s_flags |=SB_RDONLY ; +NVolSetErrors (vol); + } +/* If on NTFS versions before 3.0, we are done. */ +if (unlikely (vol->major_ver <3 )) +return true; +/* NTFS 3.0+ specific initialization. */ +/* Get the security descriptors inode. */ + vol->secure_ino =ntfs_iget (sb, FILE_Secure); +if (IS_ERR (vol->secure_ino)) { +if (!IS_ERR (vol->secure_ino)) +iput (vol->secure_ino); +ntfs_error (sb,"Failed to load $Secure." ); + goto iput_root_err_out; + } +/* Get the extended system files' directory inode. */ + vol->extend_ino =ntfs_iget (sb, FILE_Extend); +if (IS_ERR (vol->extend_ino) || + !S_ISDIR (vol->extend_ino->i_mode)) { +if (!IS_ERR (vol->extend_ino)) +iput (vol->extend_ino); +ntfs_error (sb,"Failed to load $Extend." ); + goto iput_sec_err_out; + } +/* Find the quota file, load it if present, and set it up. */ +if (!load_and_init_quota (vol) && + vol->on_errors ==ON_ERRORS_REMOUNT_RO ) { +static const char *es1 ="Failed to load $Quota" ; +static const char *es2 =". Run chkdsk." ; + + sb->s_flags |=SB_RDONLY ; +ntfs_error (sb,"%s. Mounting read-only%s" , es1, es2); +/* This will prevent a read-write remount. */ +NVolSetErrors (vol); + } + +return true; + +iput_sec_err_out: +iput (vol->secure_ino); +iput_root_err_out: +iput (vol->root_ino); +iput_logfile_err_out: +if (vol->logfile_ino) +iput (vol->logfile_ino); +iput (vol->vol_ino); +iput_lcnbmp_err_out: +iput (vol->lcnbmp_ino); +iput_attrdef_err_out: + vol->attrdef_size =0 ; +if (vol->attrdef) { +ntfs_free (vol->attrdef); + vol->attrdef =NULL ; + } +iput_upcase_err_out: + vol->upcase_len =0 ; +mutex_lock (&ntfs_lock); +if (vol->upcase == default_upcase) { + ntfs_nr_upcase_users--; + vol->upcase =NULL ; + } +mutex_unlock (&ntfs_lock); +if (vol->upcase) { +ntfs_free (vol->upcase); + vol->upcase =NULL ; + } +iput_mftbmp_err_out: +iput (vol->mftbmp_ino); +iput_mirr_err_out: +iput (vol->mftmirr_ino); +return false; +} + +static void ntfs_volume_free (struct ntfs_volume *vol) +{ +/* Throw away the table of attribute definitions. */ + vol->attrdef_size =0 ; +if (vol->attrdef) { +ntfs_free (vol->attrdef); + vol->attrdef =NULL ; + } + vol->upcase_len =0 ; +/* +* Destroy the global default upcase table if necessary. Also decrease +* the number of upcase users if we are a user. +*/ +mutex_lock (&ntfs_lock); +if (vol->upcase == default_upcase) { + ntfs_nr_upcase_users--; + vol->upcase =NULL ; + } + +if (!ntfs_nr_upcase_users && default_upcase) { +ntfs_free (default_upcase); + default_upcase =NULL ; + } + +free_compression_buffers (); + +mutex_unlock (&ntfs_lock); +if (vol->upcase) { +ntfs_free (vol->upcase); + vol->upcase =NULL ; + } + +unload_nls (vol->nls_map); + +if (vol->lcn_empty_bits_per_page) +kvfree (vol->lcn_empty_bits_per_page); +kfree (vol->volume_label); +kfree (vol); +} + +/** +* ntfs_put_super - called by the vfs to unmount a volume +* @sb: vfs superblock of volume to unmount +*/ +static void ntfs_put_super (struct super_block *sb) +{ +struct ntfs_volume *vol =NTFS_SB (sb); + +pr_info ("Entering %s, dev %s\n" , __func__, sb->s_id); + +cancel_work_sync (&vol->precalc_work); + +/* +* Commit all inodes while they are still open in case some of them +* cause others to be dirtied. +*/ +ntfs_commit_inode (vol->vol_ino); + +/* NTFS 3.0+ specific. */ +if (vol->major_ver >=3 ) { +if (vol->quota_q_ino) +ntfs_commit_inode (vol->quota_q_ino); +if (vol->quota_ino) +ntfs_commit_inode (vol->quota_ino); +if (vol->extend_ino) +ntfs_commit_inode (vol->extend_ino); +if (vol->secure_ino) +ntfs_commit_inode (vol->secure_ino); + } + +ntfs_commit_inode (vol->root_ino); + +ntfs_commit_inode (vol->lcnbmp_ino); + +/* +* the GFP_NOFS scope is not needed because ntfs_commit_inode +* does nothing +*/ +ntfs_commit_inode (vol->mftbmp_ino); + +if (vol->logfile_ino) +ntfs_commit_inode (vol->logfile_ino); + +if (vol->mftmirr_ino) +ntfs_commit_inode (vol->mftmirr_ino); +ntfs_commit_inode (vol->mft_ino); + +/* +* If a read-write mount and no volume errors have occurred, mark the +* volume clean. Also, re-commit all affected inodes. +*/ +if (!sb_rdonly (sb)) { +if (!NVolErrors (vol)) { +if (ntfs_clear_volume_flags (vol,VOLUME_IS_DIRTY )) +ntfs_warning (sb, +"Failed to clear dirty bit in volume information flags. Run chkdsk." ); +ntfs_commit_inode (vol->vol_ino); +ntfs_commit_inode (vol->root_ino); +if (vol->mftmirr_ino) +ntfs_commit_inode (vol->mftmirr_ino); +ntfs_commit_inode (vol->mft_ino); + }else { +ntfs_warning (sb, +"Volume has errors. Leaving volume marked dirty. Run chkdsk." ); + } + } + +iput (vol->vol_ino); + vol->vol_ino =NULL ; + +/* NTFS 3.0+ specific clean up. */ +if (vol->major_ver >=3 ) { +if (vol->quota_q_ino) { +iput (vol->quota_q_ino); + vol->quota_q_ino =NULL ; + } +if (vol->quota_ino) { +iput (vol->quota_ino); + vol->quota_ino =NULL ; + } +if (vol->extend_ino) { +iput (vol->extend_ino); + vol->extend_ino =NULL ; + } +if (vol->secure_ino) { +iput (vol->secure_ino); + vol->secure_ino =NULL ; + } + } + +iput (vol->root_ino); + vol->root_ino =NULL ; + +iput (vol->lcnbmp_ino); + vol->lcnbmp_ino =NULL ; + +iput (vol->mftbmp_ino); + vol->mftbmp_ino =NULL ; + +if (vol->logfile_ino) { +iput (vol->logfile_ino); + vol->logfile_ino =NULL ; + } +if (vol->mftmirr_ino) { +/* Re-commit the mft mirror and mft just in case. */ +ntfs_commit_inode (vol->mftmirr_ino); +ntfs_commit_inode (vol->mft_ino); +iput (vol->mftmirr_ino); + vol->mftmirr_ino =NULL ; + } +/* +* We should have no dirty inodes left, due to +* mft.c::ntfs_mft_writepage() cleaning all the dirty pages as +* the underlying mft records are written out and cleaned. +*/ +ntfs_commit_inode (vol->mft_ino); +write_inode_now (vol->mft_ino,1 ); + +iput (vol->mft_ino); + vol->mft_ino =NULL ; + +ntfs_volume_free (vol); +} + +int ntfs_force_shutdown (struct super_block *sb,u32 flags) +{ +struct ntfs_volume *vol =NTFS_SB (sb); +int ret; + +if (NVolShutdown (vol)) +return 0 ; + +switch (flags) { +case NTFS_GOING_DOWN_DEFAULT : +case NTFS_GOING_DOWN_FULLSYNC : + ret =bdev_freeze (sb->s_bdev); +if (ret) +return ret; +bdev_thaw (sb->s_bdev); +NVolSetShutdown (vol); +break ; +case NTFS_GOING_DOWN_NOSYNC : +NVolSetShutdown (vol); +break ; +default : +return -EINVAL ; + } + +return 0 ; +} + +static void ntfs_shutdown (struct super_block *sb) +{ +ntfs_force_shutdown (sb,NTFS_GOING_DOWN_NOSYNC ); + +} + +static int ntfs_sync_fs (struct super_block *sb,int wait) +{ +struct ntfs_volume *vol =NTFS_SB (sb); +int err =0 ; + +if (NVolShutdown (vol)) +return -EIO ; + +if (!wait) +return 0 ; + +/* If there are some dirty buffers in the bdev inode */ +if (ntfs_clear_volume_flags (vol,VOLUME_IS_DIRTY )) { +ntfs_warning (sb,"Failed to clear dirty bit in volume information flags. Run chkdsk." ); + err = -EIO ; + } +sync_inodes_sb (sb); +sync_blockdev (sb->s_bdev); +blkdev_issue_flush (sb->s_bdev); +return err; +} + +/** +* get_nr_free_clusters - return the number of free clusters on a volume +* @vol: ntfs volume for which to obtain free cluster count +* +* Calculate the number of free clusters on the mounted NTFS volume @vol. We +* actually calculate the number of clusters in use instead because this +* allows us to not care about partial pages as these will be just zero filled +* and hence not be counted as allocated clusters. +* +* The only particularity is that clusters beyond the end of the logical ntfs +* volume will be marked as allocated to prevent errors which means we have to +* discount those at the end. This is important as the cluster bitmap always +* has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside +* the logical volume and marked in use when they are not as they do not exist. +* +* If any pages cannot be read we assume all clusters in the erroring pages are +* in use. This means we return an underestimate on errors which is better than +* an overestimate. +*/ +s64 get_nr_free_clusters (struct ntfs_volume *vol) +{ +s64 nr_free = vol->nr_clusters; +u32 nr_used; +struct address_space *mapping = vol->lcnbmp_ino->i_mapping; +struct folio *folio; +pgoff_t index, max_index; +struct file_ra_state *ra; + +ntfs_debug ("Entering." ); +/* Serialize accesses to the cluster bitmap. */ + +if (NVolFreeClusterKnown (vol)) +return atomic64_read (&vol->free_clusters); + + ra =kzalloc (sizeof (*ra),GFP_NOFS ); +if (!ra) +return 0 ; + +file_ra_state_init (ra, mapping); + +/* +* Convert the number of bits into bytes rounded up, then convert into +* multiples of PAGE_SIZE, rounding up so that if we have one +* full and one partial page max_index = 2. +*/ + max_index = (((vol->nr_clusters +7 ) >>3 ) +PAGE_SIZE -1 ) >> +PAGE_SHIFT ; +/* Use multiples of 4 bytes, thus max_size is PAGE_SIZE / 4. */ +ntfs_debug ("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx." , + max_index,PAGE_SIZE /4 ); +for (index =0 ; index < max_index; index++) { +unsigned long *kaddr; + +/* +* Get folio from page cache, getting it from backing store +* if necessary, and increment the use count. +*/ + folio =filemap_lock_folio (mapping, index); +if (IS_ERR (folio)) { +page_cache_sync_readahead (mapping, ra,NULL , + index, max_index - index); + folio =ntfs_read_mapping_folio (mapping, index); +if (!IS_ERR (folio)) +folio_lock (folio); + } + +/* Ignore pages which errored synchronously. */ +if (IS_ERR (folio)) { +ntfs_debug ("Skipping page (index 0x%lx)." , index); + nr_free -=PAGE_SIZE *8 ; + vol->lcn_empty_bits_per_page[index] =0 ; +continue ; + } + + kaddr =kmap_local_folio (folio,0 ); +/* +* Subtract the number of set bits. If this +* is the last page and it is partial we don't really care as +* it just means we do a little extra work but it won't affect +* the result as all out of range bytes are set to zero by +* ntfs_readpage(). +*/ + nr_used =bitmap_weight (kaddr,PAGE_SIZE *BITS_PER_BYTE ); + nr_free -= nr_used; + vol->lcn_empty_bits_per_page[index] =PAGE_SIZE *BITS_PER_BYTE - nr_used; +kunmap_local (kaddr); +folio_unlock (folio); +folio_put (folio); + } +ntfs_debug ("Finished reading $Bitmap, last index = 0x%lx." , index -1 ); +/* +* Fixup for eventual bits outside logical ntfs volume (see function +* description above). +*/ +if (vol->nr_clusters &63 ) + nr_free +=64 - (vol->nr_clusters &63 ); + +/* If errors occurred we may well have gone below zero, fix this. */ +if (nr_free <0 ) + nr_free =0 ; +else +atomic64_set (&vol->free_clusters, nr_free); + +kfree (ra); +NVolSetFreeClusterKnown (vol); +wake_up_all (&vol->free_waitq); +ntfs_debug ("Exiting." ); +return nr_free; +} + +/* +* @nr_clusters is the number of clusters requested for allocation. +* +* Return the number of clusters available for allocation within +* the range of @nr_clusters, which is counts that considered +* for delayed allocation. +*/ +s64 ntfs_available_clusters_count (struct ntfs_volume *vol,s64 nr_clusters) +{ +s64 free_clusters; + +/* wait event */ +if (!NVolFreeClusterKnown (vol)) +wait_event (vol->free_waitq,NVolFreeClusterKnown (vol)); + + free_clusters =atomic64_read (&vol->free_clusters) - +atomic64_read (&vol->dirty_clusters); +if (free_clusters <=0 ) +return -ENOSPC ; +else if (free_clusters < nr_clusters) + nr_clusters = free_clusters; + +return nr_clusters; +} + +/** +* __get_nr_free_mft_records - return the number of free inodes on a volume +* @vol: ntfs volume for which to obtain free inode count +* @nr_free: number of mft records in filesystem +* @max_index: maximum number of pages containing set bits +* +* Calculate the number of free mft records (inodes) on the mounted NTFS +* volume @vol. We actually calculate the number of mft records in use instead +* because this allows us to not care about partial pages as these will be just +* zero filled and hence not be counted as allocated mft record. +* +* If any pages cannot be read we assume all mft records in the erroring pages +* are in use. This means we return an underestimate on errors which is better +* than an overestimate. +* +* NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing. +*/ +static unsigned long __get_nr_free_mft_records (struct ntfs_volume *vol, +s64 nr_free,const pgoff_t max_index) +{ +struct address_space *mapping = vol->mftbmp_ino->i_mapping; +struct folio *folio; +pgoff_t index; +struct file_ra_state *ra; + +ntfs_debug ("Entering." ); + + ra =kzalloc (sizeof (*ra),GFP_NOFS ); +if (!ra) +return 0 ; + +file_ra_state_init (ra, mapping); + +/* Use multiples of 4 bytes, thus max_size is PAGE_SIZE / 4. */ +ntfs_debug ("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = 0x%lx." , + max_index,PAGE_SIZE /4 ); +for (index =0 ; index < max_index; index++) { +unsigned long *kaddr; + +/* +* Get folio from page cache, getting it from backing store +* if necessary, and increment the use count. +*/ + folio =filemap_lock_folio (mapping, index); +if (IS_ERR (folio)) { +page_cache_sync_readahead (mapping, ra,NULL , + index, max_index - index); + folio =ntfs_read_mapping_folio (mapping, index); +if (!IS_ERR (folio)) +folio_lock (folio); + } + +/* Ignore pages which errored synchronously. */ +if (IS_ERR (folio)) { +ntfs_debug ("read_mapping_page() error. Skipping page (index 0x%lx)." , + index); + nr_free -=PAGE_SIZE *8 ; +continue ; + } + + kaddr =kmap_local_folio (folio,0 ); +/* +* Subtract the number of set bits. If this +* is the last page and it is partial we don't really care as +* it just means we do a little extra work but it won't affect +* the result as all out of range bytes are set to zero by +* ntfs_readpage(). +*/ + nr_free -=bitmap_weight (kaddr, +PAGE_SIZE *BITS_PER_BYTE ); +kunmap_local (kaddr); +folio_unlock (folio); +folio_put (folio); + } +ntfs_debug ("Finished reading $MFT/$BITMAP, last index = 0x%lx." , + index -1 ); +/* If errors occurred we may well have gone below zero, fix this. */ +if (nr_free <0 ) + nr_free =0 ; +else +atomic64_set (&vol->free_mft_records, nr_free); + +kfree (ra); +ntfs_debug ("Exiting." ); +return nr_free; +} + +/** +* ntfs_statfs - return information about mounted NTFS volume +* @dentry: dentry from mounted volume +* @sfs: statfs structure in which to return the information +* +* Return information about the mounted NTFS volume @dentry in the statfs structure +* pointed to by @sfs (this is initialized with zeros before ntfs_statfs is +* called). We interpret the values to be correct of the moment in time at +* which we are called. Most values are variable otherwise and this isn't just +* the free values but the totals as well. For example we can increase the +* total number of file nodes if we run out and we can keep doing this until +* there is no more space on the volume left at all. +* +* Called from vfs_statfs which is used to handle the statfs, fstatfs, and +* ustat system calls. +* +* Return 0 on success or -errno on error. +*/ +static int ntfs_statfs (struct dentry *dentry,struct kstatfs *sfs) +{ +struct super_block *sb = dentry->d_sb; +s64 size; +struct ntfs_volume *vol =NTFS_SB (sb); +struct ntfs_inode *mft_ni =NTFS_I (vol->mft_ino); +unsigned long flags; + +ntfs_debug ("Entering." ); +/* Type of filesystem. */ + sfs->f_type =NTFS_SB_MAGIC ; +/* Optimal transfer block size. */ + sfs->f_bsize = vol->cluster_size; +/* Fundamental file system block size, used as the unit. */ + sfs->f_frsize = vol->cluster_size; + +/* +* Total data blocks in filesystem in units of f_bsize and since +* inodes are also stored in data blocs ($MFT is a file) this is just +* the total clusters. +*/ + sfs->f_blocks = vol->nr_clusters; + +/* wait event */ +if (!NVolFreeClusterKnown (vol)) +wait_event (vol->free_waitq,NVolFreeClusterKnown (vol)); + +/* Free data blocks in filesystem in units of f_bsize. */ + size =atomic64_read (&vol->free_clusters) - +atomic64_read (&vol->dirty_clusters); +if (size <0LL ) + size =0LL ; + +/* Free blocks avail to non-superuser, same as above on NTFS. */ + sfs->f_bavail = sfs->f_bfree = size; + +/* Number of inodes in filesystem (at this point in time). */ +read_lock_irqsave (&mft_ni->size_lock, flags); + sfs->f_files =i_size_read (vol->mft_ino) >> vol->mft_record_size_bits; +read_unlock_irqrestore (&mft_ni->size_lock, flags); + +/* Free inodes in fs (based on current total count). */ + sfs->f_ffree =atomic64_read (&vol->free_mft_records); + +/* +* File system id. This is extremely *nix flavour dependent and even +* within Linux itself all fs do their own thing. I interpret this to +* mean a unique id associated with the mounted fs and not the id +* associated with the filesystem driver, the latter is already given +* by the filesystem type in sfs->f_type. Thus we use the 64-bit +* volume serial number splitting it into two 32-bit parts. We enter +* the least significant 32-bits in f_fsid[0] and the most significant +* 32-bits in f_fsid[1]. +*/ + sfs->f_fsid =u64_to_fsid (vol->serial_no); +/* Maximum length of filenames. */ + sfs->f_namelen =NTFS_MAX_NAME_LEN ; + +return 0 ; +} + +static int ntfs_write_inode (struct inode *vi,struct writeback_control *wbc) +{ +return __ntfs_write_inode (vi, wbc->sync_mode ==WB_SYNC_ALL ); +} + +/** +* The complete super operations. +*/ +static const struct super_operations ntfs_sops = { + .alloc_inode = ntfs_alloc_big_inode,/* VFS: Allocate new inode. */ + .free_inode = ntfs_free_big_inode,/* VFS: Deallocate inode. */ + .drop_inode = ntfs_drop_big_inode, + .write_inode = ntfs_write_inode,/* VFS: Write dirty inode to disk. */ + .put_super = ntfs_put_super,/* Syscall: umount. */ + .shutdown = ntfs_shutdown, + .sync_fs = ntfs_sync_fs,/* Syscall: sync. */ + .statfs = ntfs_statfs,/* Syscall: statfs */ + .evict_inode = ntfs_evict_big_inode, + .show_options = ntfs_show_options,/* Show mount options in proc. */ +}; + +static void precalc_free_clusters (struct work_struct *work) +{ + struct ntfs_volume *vol = container_of(work, struct ntfs_volume, precalc_work); +s64 nr_free; + + nr_free =get_nr_free_clusters (vol); + +ntfs_debug ("pre-calculate free clusters(%lld) using workqueue" , + nr_free); +} + +/** +* ntfs_fill_super - mount an ntfs filesystem +* +* ntfs_fill_super() is called by the VFS to mount the device described by @sb +* with the mount otions in @data with the NTFS filesystem. +* +* If @silent is true, remain silent even if errors are detected. This is used +* during bootup, when the kernel tries to mount the root filesystem with all +* registered filesystems one after the other until one succeeds. This implies +* that all filesystems except the correct one will quite correctly and +* expectedly return an error, but nobody wants to see error messages when in +* fact this is what is supposed to happen. +*/ +static struct lock_class_key ntfs_mft_inval_lock_key; + +static int ntfs_fill_super (struct super_block *sb,struct fs_context *fc) +{ +char *boot; +struct inode *tmp_ino; +int blocksize, result; +pgoff_t lcn_bit_pages; +struct ntfs_volume *vol =NTFS_SB (sb); +int silent = fc->sb_flags &SB_SILENT ; + + vol->sb = sb; + +/* +* We do a pretty difficult piece of bootstrap by reading the +* MFT (and other metadata) from disk into memory. We'll only +* release this metadata during umount, so the locking patterns +* observed during bootstrap do not count. So turn off the +* observation of locking patterns (strictly for this context +* only) while mounting NTFS. [The validator is still active +* otherwise, even for this context: it will for example record +* lock class registrations.] +*/ +lockdep_off (); +ntfs_debug ("Entering." ); + +if (vol->nls_map && !strcmp (vol->nls_map->charset,"utf8" )) + vol->nls_utf8 = true; +if (NVolDisableSparse (vol)) + vol->preallocated_size =0 ; + +if (NVolDiscard (vol) && !bdev_max_discard_sectors (sb->s_bdev)) { +ntfs_warning ( + sb, +"Discard requested but device does not support discard. Discard disabled." ); +NVolClearDiscard (vol); + } + +/* We support sector sizes up to the PAGE_SIZE. */ +if (bdev_logical_block_size (sb->s_bdev) >PAGE_SIZE ) { +if (!silent) +ntfs_error (sb, +"Device has unsupported sector size (%i). The maximum supported sector size on this architecture is %lu bytes." , +bdev_logical_block_size (sb->s_bdev), +PAGE_SIZE ); + goto err_out_now; + } + +/* +* Setup the device access block size to NTFS_BLOCK_SIZE or the hard +* sector size, whichever is bigger. +*/ + blocksize =sb_min_blocksize (sb,NTFS_BLOCK_SIZE ); +if (blocksize <NTFS_BLOCK_SIZE ) { +if (!silent) +ntfs_error (sb,"Unable to set device block size." ); + goto err_out_now; + } + +ntfs_debug ("Set device block size to %i bytes (block size bits %i)." , + blocksize, sb->s_blocksize_bits); +/* Determine the size of the device in units of block_size bytes. */ +if (!bdev_nr_bytes (sb->s_bdev)) { +if (!silent) +ntfs_error (sb,"Unable to determine device size." ); + goto err_out_now; + } + vol->nr_blocks =bdev_nr_bytes (sb->s_bdev) >> + sb->s_blocksize_bits; +/* Read the boot sector and return unlocked buffer head to it. */ + boot =read_ntfs_boot_sector (sb, silent); +if (!boot) { +if (!silent) +ntfs_error (sb,"Not an NTFS volume." ); + goto err_out_now; + } +/* +* Extract the data from the boot sector and setup the ntfs volume +* using it. +*/ + result =parse_ntfs_boot_sector (vol, (struct ntfs_boot_sector *)boot); +kfree (boot); +if (!result) { +if (!silent) +ntfs_error (sb,"Unsupported NTFS filesystem." ); + goto err_out_now; + } + +if (vol->sector_size > blocksize) { + blocksize =sb_set_blocksize (sb, vol->sector_size); +if (blocksize != vol->sector_size) { +if (!silent) +ntfs_error (sb, +"Unable to set device block size to sector size (%i)." , + vol->sector_size); + goto err_out_now; + } + vol->nr_blocks =bdev_nr_bytes (sb->s_bdev) >> + sb->s_blocksize_bits; +ntfs_debug ("Changed device block size to %i bytes (block size bits %i) to match volume sector size." , + blocksize, sb->s_blocksize_bits); + } +/* Initialize the cluster and mft allocators. */ +ntfs_setup_allocators (vol); +/* Setup remaining fields in the super block. */ + sb->s_magic =NTFS_SB_MAGIC ; +/* +* Ntfs allows 63 bits for the file size, i.e. correct would be: +* sb->s_maxbytes = ~0ULL >> 1; +* But the kernel uses a long as the page cache page index which on +* 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel +* defined to the maximum the page cache page index can cope with +* without overflowing the index or to 2^63 - 1, whichever is smaller. +*/ + sb->s_maxbytes =MAX_LFS_FILESIZE ; +/* Ntfs measures time in 100ns intervals. */ + sb->s_time_gran =100 ; + + sb->s_xattr = ntfsp_xattr_handlers; +/* +* Now load the metadata required for the page cache and our address +* space operations to function. We do this by setting up a specialised +* read_inode method and then just calling the normal iget() to obtain +* the inode for $MFT which is sufficient to allow our normal inode +* operations and associated address space operations to function. +*/ + sb->s_op = &ntfs_sops; + tmp_ino =new_inode (sb); +if (!tmp_ino) { +if (!silent) +ntfs_error (sb,"Failed to load essential metadata." ); + goto err_out_now; + } + + tmp_ino->i_ino =FILE_MFT ; +insert_inode_hash (tmp_ino); +if (ntfs_read_inode_mount (tmp_ino) <0 ) { +if (!silent) +ntfs_error (sb,"Failed to load essential metadata." ); + goto iput_tmp_ino_err_out_now; + } +lockdep_set_class (&tmp_ino->i_mapping->invalidate_lock, + &ntfs_mft_inval_lock_key); + +mutex_lock (&ntfs_lock); + +/* +* Generate the global default upcase table if necessary. Also +* temporarily increment the number of upcase users to avoid race +* conditions with concurrent (u)mounts. +*/ +if (!default_upcase) + default_upcase =generate_default_upcase (); + ntfs_nr_upcase_users++; +mutex_unlock (&ntfs_lock); + + lcn_bit_pages = (((vol->nr_clusters +7 ) >>3 ) +PAGE_SIZE -1 ) >>PAGE_SHIFT ; + vol->lcn_empty_bits_per_page =kvmalloc_array (lcn_bit_pages,sizeof (unsigned int ), +GFP_KERNEL ); +if (!vol->lcn_empty_bits_per_page) { +ntfs_error (sb, +"Unable to allocate pages for storing LCN empty bit counts\n" ); + goto unl_upcase_iput_tmp_ino_err_out_now; + } + +/* +* From now on, ignore @silent parameter. If we fail below this line, +* it will be due to a corrupt fs or a system error, so we report it. +*/ +/* +* Open the system files with normal access functions and complete +* setting up the ntfs super block. +*/ +if (!load_system_files (vol)) { +ntfs_error (sb,"Failed to load system files." ); + goto unl_upcase_iput_tmp_ino_err_out_now; + } + +/* We grab a reference, simulating an ntfs_iget(). */ +ihold (vol->root_ino); + sb->s_root =d_make_root (vol->root_ino); +if (sb->s_root) { +s64 nr_records; + +ntfs_debug ("Exiting, status successful." ); + +/* Release the default upcase if it has no users. */ +mutex_lock (&ntfs_lock); +if (!--ntfs_nr_upcase_users && default_upcase) { +ntfs_free (default_upcase); + default_upcase =NULL ; + } +mutex_unlock (&ntfs_lock); + sb->s_export_op = &ntfs_export_ops; +lockdep_on (); + + nr_records =__get_nr_free_mft_records (vol, +i_size_read (vol->mft_ino) >> vol->mft_record_size_bits, + ((((NTFS_I (vol->mft_ino)->initialized_size >> + vol->mft_record_size_bits) + +7 ) >>3 ) +PAGE_SIZE -1 ) >>PAGE_SHIFT ); +ntfs_debug ("Free mft records(%lld)" , nr_records); + +init_waitqueue_head (&vol->free_waitq); +INIT_WORK (&vol->precalc_work, precalc_free_clusters); +queue_work (ntfs_wq, &vol->precalc_work); +return 0 ; + } +ntfs_error (sb,"Failed to allocate root directory." ); +/* Clean up after the successful load_system_files() call from above. */ +iput (vol->vol_ino); + vol->vol_ino =NULL ; +/* NTFS 3.0+ specific clean up. */ +if (vol->major_ver >=3 ) { +if (vol->quota_q_ino) { +iput (vol->quota_q_ino); + vol->quota_q_ino =NULL ; + } +if (vol->quota_ino) { +iput (vol->quota_ino); + vol->quota_ino =NULL ; + } +if (vol->extend_ino) { +iput (vol->extend_ino); + vol->extend_ino =NULL ; + } +if (vol->secure_ino) { +iput (vol->secure_ino); + vol->secure_ino =NULL ; + } + } +iput (vol->root_ino); + vol->root_ino =NULL ; +iput (vol->lcnbmp_ino); + vol->lcnbmp_ino =NULL ; +iput (vol->mftbmp_ino); + vol->mftbmp_ino =NULL ; +if (vol->logfile_ino) { +iput (vol->logfile_ino); + vol->logfile_ino =NULL ; + } +if (vol->mftmirr_ino) { +iput (vol->mftmirr_ino); + vol->mftmirr_ino =NULL ; + } +/* Throw away the table of attribute definitions. */ + vol->attrdef_size =0 ; +if (vol->attrdef) { +ntfs_free (vol->attrdef); + vol->attrdef =NULL ; + } + vol->upcase_len =0 ; +mutex_lock (&ntfs_lock); +if (vol->upcase == default_upcase) { + ntfs_nr_upcase_users--; + vol->upcase =NULL ; + } +mutex_unlock (&ntfs_lock); +if (vol->upcase) { +ntfs_free (vol->upcase); + vol->upcase =NULL ; + } +if (vol->nls_map) { +unload_nls (vol->nls_map); + vol->nls_map =NULL ; + } +/* Error exit code path. */ +unl_upcase_iput_tmp_ino_err_out_now: +if (vol->lcn_empty_bits_per_page) +kvfree (vol->lcn_empty_bits_per_page); +/* +* Decrease the number of upcase users and destroy the global default +* upcase table if necessary. +*/ +mutex_lock (&ntfs_lock); +if (!--ntfs_nr_upcase_users && default_upcase) { +ntfs_free (default_upcase); + default_upcase =NULL ; + } + +mutex_unlock (&ntfs_lock); +iput_tmp_ino_err_out_now: +iput (tmp_ino); +if (vol->mft_ino && vol->mft_ino != tmp_ino) +iput (vol->mft_ino); + vol->mft_ino =NULL ; +/* Errors at this stage are irrelevant. */ +err_out_now: + sb->s_fs_info =NULL ; +kfree (vol); +ntfs_debug ("Failed, returning -EINVAL." ); +lockdep_on (); +return -EINVAL ; +} + +/* +* This is a slab cache to optimize allocations and deallocations of Unicode +* strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN +* (255) Unicode characters + a terminating NULL Unicode character. +*/ +struct kmem_cache *ntfs_name_cache; + +/* Slab caches for efficient allocation/deallocation of inodes. */ +struct kmem_cache *ntfs_inode_cache; +struct kmem_cache *ntfs_big_inode_cache; + +/* Init once constructor for the inode slab cache. */ +static void ntfs_big_inode_init_once (void *foo) +{ +struct ntfs_inode *ni = (struct ntfs_inode *)foo; + +inode_init_once (VFS_I (ni)); +} + +/* +* Slab caches to optimize allocations and deallocations of attribute search +* contexts and index contexts, respectively. +*/ +struct kmem_cache *ntfs_attr_ctx_cache; +struct kmem_cache *ntfs_index_ctx_cache; + +/* Driver wide mutex. */ +DEFINE_MUTEX (ntfs_lock); + +static int ntfs_get_tree (struct fs_context *fc) +{ +return get_tree_bdev (fc, ntfs_fill_super); +} + +static void ntfs_free_fs_context (struct fs_context *fc) +{ +struct ntfs_volume *vol = fc->s_fs_info; + +if (vol) +ntfs_volume_free (vol); +} + +static const struct fs_context_operations ntfs_context_ops = { + .parse_param = ntfs_parse_param, + .get_tree = ntfs_get_tree, + .free = ntfs_free_fs_context, + .reconfigure = ntfs_reconfigure, +}; + +static int ntfs_init_fs_context (struct fs_context *fc) +{ +struct ntfs_volume *vol; + +/* Allocate a new struct ntfs_volume and place it in sb->s_fs_info. */ + vol =kmalloc (sizeof (struct ntfs_volume ),GFP_NOFS ); +if (!vol) +return -ENOMEM ; + +/* Initialize struct ntfs_volume structure. */ + *vol = (struct ntfs_volume ) { + .uid =INVALID_UID , + .gid =INVALID_GID , + .fmask =0 , + .dmask =0 , + .mft_zone_multiplier =1 , + .on_errors =ON_ERRORS_CONTINUE , + .nls_map =load_nls_default (), + .preallocated_size =NTFS_DEF_PREALLOC_SIZE , + }; + +NVolSetShowHiddenFiles (vol); +NVolSetCaseSensitive (vol); +init_rwsem (&vol->mftbmp_lock); +init_rwsem (&vol->lcnbmp_lock); + + fc->s_fs_info = vol; + fc->ops = &ntfs_context_ops; +return 0 ; +} + +static struct file_system_type ntfs_fs_type = { + .owner =THIS_MODULE , + .name ="ntfsplus" , + .init_fs_context = ntfs_init_fs_context, + .parameters = ntfs_parameters, + .kill_sb = kill_block_super, + .fs_flags =FS_REQUIRES_DEV |FS_ALLOW_IDMAP , +}; +MODULE_ALIAS_FS ("ntfsplus" ); + +static int ntfs_workqueue_init (void ) +{ + ntfs_wq =alloc_workqueue ("ntfsplus-bg-io" ,0 ,0 ); +if (!ntfs_wq) +return -ENOMEM ; +return 0 ; +} + +static void ntfs_workqueue_destroy (void ) +{ +destroy_workqueue (ntfs_wq); + ntfs_wq =NULL ; +} + +/* Stable names for the slab caches. */ +static const char ntfs_index_ctx_cache_name[] ="ntfs_index_ctx_cache" ; +static const char ntfs_attr_ctx_cache_name[] ="ntfs_attr_ctx_cache" ; +static const char ntfs_name_cache_name[] ="ntfs_name_cache" ; +static const char ntfs_inode_cache_name[] ="ntfs_inode_cache" ; +static const char ntfs_big_inode_cache_name[] ="ntfs_big_inode_cache" ; + +static int __init init_ntfs_fs (void ) +{ +int err =0 ; + + err =ntfs_workqueue_init (); +if (err) { +pr_crit ("Failed to register workqueue!\n" ); +return err; + } + + ntfs_index_ctx_cache =kmem_cache_create (ntfs_index_ctx_cache_name, +sizeof (struct ntfs_index_context ),0 /* offset */ , +SLAB_HWCACHE_ALIGN ,NULL /* ctor */ ); +if (!ntfs_index_ctx_cache) { +pr_crit ("Failed to create %s!\n" , ntfs_index_ctx_cache_name); + goto ictx_err_out; + } + ntfs_attr_ctx_cache =kmem_cache_create (ntfs_attr_ctx_cache_name, +sizeof (struct ntfs_attr_search_ctx ),0 /* offset */ , +SLAB_HWCACHE_ALIGN ,NULL /* ctor */ ); +if (!ntfs_attr_ctx_cache) { +pr_crit ("ntfs+: Failed to create %s!\n" , + ntfs_attr_ctx_cache_name); + goto actx_err_out; + } + + ntfs_name_cache =kmem_cache_create (ntfs_name_cache_name, + (NTFS_MAX_NAME_LEN +2 ) *sizeof (__le16),0 , +SLAB_HWCACHE_ALIGN ,NULL ); +if (!ntfs_name_cache) { +pr_crit ("Failed to create %s!\n" , ntfs_name_cache_name); + goto name_err_out; + } + + ntfs_inode_cache =kmem_cache_create (ntfs_inode_cache_name, +sizeof (struct ntfs_inode ),0 ,SLAB_RECLAIM_ACCOUNT ,NULL ); +if (!ntfs_inode_cache) { +pr_crit ("Failed to create %s!\n" , ntfs_inode_cache_name); + goto inode_err_out; + } + + ntfs_big_inode_cache =kmem_cache_create (ntfs_big_inode_cache_name, +sizeof (struct big_ntfs_inode ),0 ,SLAB_HWCACHE_ALIGN | +SLAB_RECLAIM_ACCOUNT |SLAB_ACCOUNT , + ntfs_big_inode_init_once); +if (!ntfs_big_inode_cache) { +pr_crit ("Failed to create %s!\n" , ntfs_big_inode_cache_name); + goto big_inode_err_out; + } + +/* Register the ntfs sysctls. */ + err =ntfs_sysctl (1 ); +if (err) { +pr_crit ("Failed to register NTFS sysctls!\n" ); + goto sysctl_err_out; + } + + err =register_filesystem (&ntfs_fs_type); +if (!err) { +ntfs_debug ("ntfs+ driver registered successfully." ); +return 0 ;/* Success! */ + } +pr_crit ("Failed to register ntfs+ filesystem driver!\n" ); + +/* Unregister the ntfs sysctls. */ +ntfs_sysctl (0 ); +sysctl_err_out: +kmem_cache_destroy (ntfs_big_inode_cache); +big_inode_err_out: +kmem_cache_destroy (ntfs_inode_cache); +inode_err_out: +kmem_cache_destroy (ntfs_name_cache); +name_err_out: +kmem_cache_destroy (ntfs_attr_ctx_cache); +actx_err_out: +kmem_cache_destroy (ntfs_index_ctx_cache); +ictx_err_out: +if (!err) { +pr_crit ("Aborting ntfs+ filesystem driver registration...\n" ); + err = -ENOMEM ; + } +return err; +} + +static void __exit exit_ntfs_fs(void) +{ +ntfs_debug ("Unregistering ntfs+ driver." ); + +unregister_filesystem (&ntfs_fs_type); + +/* +* Make sure all delayed rcu free inodes are flushed before we +* destroy cache. +*/ +rcu_barrier (); +kmem_cache_destroy (ntfs_big_inode_cache); +kmem_cache_destroy (ntfs_inode_cache); +kmem_cache_destroy (ntfs_name_cache); +kmem_cache_destroy (ntfs_attr_ctx_cache); +kmem_cache_destroy (ntfs_index_ctx_cache); +ntfs_workqueue_destroy (); +/* Unregister the ntfs sysctls. */ +ntfs_sysctl (0 ); +} + +module_init (init_ntfs_fs); +module_exit (exit_ntfs_fs); + +MODULE_AUTHOR ("Anton Altaparmakov <[email protected]>" );/* Original read-only NTFS driver */ +MODULE_AUTHOR ("Namjae Jeon <[email protected]>" );/* Add write, iomap and various features */ +MODULE_DESCRIPTION ("NTFS+ read-write filesystem driver" ); +MODULE_LICENSE ("GPL" ); +#ifdef DEBUG +module_param (debug_msgs, bint,0 ); +MODULE_PARM_DESC (debug_msgs,"Enable debug messages." ); +#endif
Signature · 1 line
2.25.1
PATCH v2 3/11 ntfsplus: add inode operations
In reply to Namjae JeonThis adds the implementation of inode operations for ntfsplus.
--- fs/ntfsplus/inode.c | 3729 +++++++++++++++++++++++++++++++++++++++++++ fs/ntfsplus/mft.c | 2698 +++++++++++++++++++++++++++++++ fs/ntfsplus/mst.c | 195 +++ fs/ntfsplus/namei.c | 1677 +++++++++++++++++++ 4 files changed, 8299 insertions(+) create mode 100644 fs/ntfsplus/inode.c create mode 100644 fs/ntfsplus/mft.c create mode 100644 fs/ntfsplus/mst.c create mode 100644 fs/ntfsplus/namei.c
fs/ntfsplus/inode.c new file +3729 -0
@@ -0,0 +1,3729 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** +* NTFS kernel inode handling. +* +* Copyright (c) 2001-2014 Anton Altaparmakov and Tuxera Inc. +* Copyright (c) 2025 LG Electronics Co., Ltd. +*/ + +#include <linux/writeback.h> +#include <linux/seq_file.h> + +#include "lcnalloc.h" +#include "misc.h" +#include "ntfs.h" +#include "index.h" +#include "attrlist.h" +#include "reparse.h" +#include "ea.h" +#include "attrib.h" +#include "ntfs_iomap.h" + +/** +* ntfs_test_inode - compare two (possibly fake) inodes for equality +* @vi: vfs inode which to test +* @data: data which is being tested with +* +* Compare the ntfs attribute embedded in the ntfs specific part of the vfs +* inode @vi for equality with the ntfs attribute @data. +* +* If searching for the normal file/directory inode, set @na->type to AT_UNUSED. +* @na->name and @na->name_len are then ignored. +* +* Return 1 if the attributes match and 0 if not. +* +* NOTE: This function runs with the inode_hash_lock spin lock held so it is not +* allowed to sleep. +*/ +int ntfs_test_inode (struct inode *vi,void *data) +{ +struct ntfs_attr *na = (struct ntfs_attr *)data; +struct ntfs_inode *ni; + +if (vi->i_ino != na->mft_no) +return 0 ; + + ni =NTFS_I (vi); + +/* If !NInoAttr(ni), @vi is a normal file or directory inode. */ +if (likely (!NInoAttr (ni))) { +/* If not looking for a normal inode this is a mismatch. */ +if (unlikely (na->type !=AT_UNUSED )) +return 0 ; + }else { +/* A fake inode describing an attribute. */ +if (ni->type != na->type) +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 (!ni->ext.base_ntfs_ino) +return 0 ; + } + +/* Match! */ +return 1 ; +} + +/** +* ntfs_init_locked_inode - initialize an inode +* @vi: vfs inode to initialize +* @data: data which to initialize @vi to +* +* Initialize the vfs inode @vi with the values from the ntfs attribute @data in +* order to enable ntfs_test_inode() to do its work. +* +* If initializing the normal file/directory inode, set @na->type to AT_UNUSED. +* In that case, @na->name and @na->name_len should be set to NULL and 0, +* respectively. Although that is not strictly necessary as +* ntfs_read_locked_inode() will fill them in later. +* +* Return 0 on success and error. +* +* NOTE: This function runs with the inode->i_lock spin lock held so it is not +* allowed to sleep. (Hence the GFP_ATOMIC allocation.) +*/ +static int ntfs_init_locked_inode (struct inode *vi,void *data) +{ +struct ntfs_attr *na = (struct ntfs_attr *)data; +struct ntfs_inode *ni =NTFS_I (vi); + + vi->i_ino = na->mft_no; + +if (na->type ==AT_INDEX_ALLOCATION ) +NInoSetMstProtected (ni); +else + ni->type = na->type; + + ni->name = na->name; + ni->name_len = na->name_len; + ni->folio =NULL ; +atomic_set (&ni->count,1 ); + +/* If initializing a normal inode, we are done. */ +if (likely (na->type ==AT_UNUSED )) +return 0 ; + +/* It is a fake inode. */ +NInoSetAttr (ni); + +/* +* We have I30 global constant as an optimization as it is the name +* in >99.9% of named attributes! The other <0.1% incur a GFP_ATOMIC +* allocation but that is ok. And most attributes are unnamed anyway, +* thus the fraction of named attributes with name != I30 is actually +* absolutely tiny. +*/ +if (na->name_len && na->name !=I30 ) { +unsigned int i; + + i = na->name_len *sizeof (__le16); + ni->name =kmalloc (i +sizeof (__le16),GFP_ATOMIC ); +if (!ni->name) +return -ENOMEM ; +memcpy (ni->name, na->name, i); + ni->name[na->name_len] =0 ; + } +return 0 ; +} + +static int ntfs_read_locked_inode (struct inode *vi); +static int ntfs_read_locked_attr_inode (struct inode *base_vi,struct inode *vi); +static int ntfs_read_locked_index_inode (struct inode *base_vi, +struct inode *vi); + +/** +* ntfs_iget - obtain a struct inode corresponding to a specific normal inode +* @sb: super block of mounted volume +* @mft_no: mft record number / inode number to obtain +* +* Obtain the struct inode corresponding to a specific normal inode (i.e. a +* file or directory). +* +* If the inode is in the cache, it is just returned with an increased +* reference count. Otherwise, a new struct inode is allocated and initialized, +* and finally ntfs_read_locked_inode() is called to read in the inode and +* fill in the remainder of the inode structure. +* +* Return the struct inode on success. Check the return value with IS_ERR() and +* if true, the function failed and the error code is obtained from PTR_ERR(). +*/ +struct inode *ntfs_iget (struct super_block *sb,unsigned long mft_no) +{ +struct inode *vi; +int err; +struct ntfs_attr na; + + na.mft_no = mft_no; + na.type =AT_UNUSED ; + na.name =NULL ; + na.name_len =0 ; + + vi =iget5_locked (sb, mft_no, ntfs_test_inode, + ntfs_init_locked_inode, &na); +if (unlikely (!vi)) +return ERR_PTR (-ENOMEM ); + + err =0 ; + +/* If this is a freshly allocated inode, need to read it now. */ +if (vi->i_state &I_NEW ) { + err =ntfs_read_locked_inode (vi); +unlock_new_inode (vi); + } +/* +* There is no point in keeping bad inodes around if the failure was +* due to ENOMEM. We want to be able to retry again later. +*/ +if (unlikely (err == -ENOMEM )) { +iput (vi); + vi =ERR_PTR (err); + } +return vi; +} + +/** +* ntfs_attr_iget - obtain a struct inode corresponding to an attribute +* @base_vi: vfs base inode containing the attribute +* @type: attribute type +* @name: Unicode name of the attribute (NULL if unnamed) +* @name_len: length of @name in Unicode characters (0 if unnamed) +* +* Obtain the (fake) struct inode corresponding to the attribute specified by +* @type, @name, and @name_len, which is present in the base mft record +* specified by the vfs inode @base_vi. +* +* If the attribute inode is in the cache, it is just returned with an +* increased reference count. Otherwise, a new struct inode is allocated and +* initialized, and finally ntfs_read_locked_attr_inode() is called to read the +* attribute and fill in the inode structure. +* +* Note, for index allocation attributes, you need to use ntfs_index_iget() +* instead of ntfs_attr_iget() as working with indices is a lot more complex. +* +* Return the struct inode of the attribute inode on success. Check the return +* value with IS_ERR() and if true, the function failed and the error code is +* obtained from PTR_ERR(). +*/ +struct inode *ntfs_attr_iget (struct inode *base_vi,__le32 type, +__le16 *name,u32 name_len) +{ +struct inode *vi; +int err; +struct ntfs_attr na; + +/* Make sure no one calls ntfs_attr_iget() for indices. */ +WARN_ON (type ==AT_INDEX_ALLOCATION ); + + na.mft_no = base_vi->i_ino; + na.type = type; + na.name = name; + na.name_len = name_len; + + vi =iget5_locked (base_vi->i_sb, na.mft_no, ntfs_test_inode, + ntfs_init_locked_inode, &na); +if (unlikely (!vi)) +return ERR_PTR (-ENOMEM ); + err =0 ; + +/* If this is a freshly allocated inode, need to read it now. */ +if (vi->i_state &I_NEW ) { + err =ntfs_read_locked_attr_inode (base_vi, vi); +unlock_new_inode (vi); + } +/* +* There is no point in keeping bad attribute inodes around. This also +* simplifies things in that we never need to check for bad attribute +* inodes elsewhere. +*/ +if (unlikely (err)) { +iput (vi); + vi =ERR_PTR (err); + } +return vi; +} + +/** +* ntfs_index_iget - obtain a struct inode corresponding to an index +* @base_vi: vfs base inode containing the index related attributes +* @name: Unicode name of the index +* @name_len: length of @name in Unicode characters +* +* Obtain the (fake) struct inode corresponding to the index specified by @name +* and @name_len, which is present in the base mft record specified by the vfs +* inode @base_vi. +* +* If the index inode is in the cache, it is just returned with an increased +* reference count. Otherwise, a new struct inode is allocated and +* initialized, and finally ntfs_read_locked_index_inode() is called to read +* the index related attributes and fill in the inode structure. +* +* Return the struct inode of the index inode on success. Check the return +* value with IS_ERR() and if true, the function failed and the error code is +* obtained from PTR_ERR(). +*/ +struct inode *ntfs_index_iget (struct inode *base_vi,__le16 *name, +u32 name_len) +{ +struct inode *vi; +int err; +struct ntfs_attr na; + + na.mft_no = base_vi->i_ino; + na.type =AT_INDEX_ALLOCATION ; + na.name = name; + na.name_len = name_len; + + vi =iget5_locked (base_vi->i_sb, na.mft_no, ntfs_test_inode, + ntfs_init_locked_inode, &na); +if (unlikely (!vi)) +return ERR_PTR (-ENOMEM ); + + err =0 ; + +/* If this is a freshly allocated inode, need to read it now. */ +if (vi->i_state &I_NEW ) { + err =ntfs_read_locked_index_inode (base_vi, vi); +unlock_new_inode (vi); + } +/* +* There is no point in keeping bad index inodes around. This also +* simplifies things in that we never need to check for bad index +* inodes elsewhere. +*/ +if (unlikely (err)) { +iput (vi); + vi =ERR_PTR (err); + } +return vi; +} + +struct inode *ntfs_alloc_big_inode (struct super_block *sb) +{ +struct ntfs_inode *ni; + +ntfs_debug ("Entering." ); + ni =alloc_inode_sb (sb, ntfs_big_inode_cache,GFP_NOFS ); +if (likely (ni !=NULL )) { + ni->state =0 ; + ni->type =0 ; + ni->mft_no =0 ; +return VFS_I (ni); + } +ntfs_error (sb,"Allocation of NTFS big inode structure failed." ); +return NULL ; +} + +void ntfs_free_big_inode (struct inode *inode) +{ +kmem_cache_free (ntfs_big_inode_cache,NTFS_I (inode)); +} + +static int ntfs_non_resident_dealloc_clusters (struct ntfs_inode *ni) +{ +struct super_block *sb = ni->vol->sb; +struct ntfs_attr_search_ctx *actx; +int err =0 ; + + actx =ntfs_attr_get_search_ctx (ni,NULL ); +if (!actx) +return -ENOMEM ; +WARN_ON (actx->mrec->link_count !=0 ); + +/** +* ntfs_truncate_vfs cannot be called in evict() context due +* to some limitations, which are the @ni vfs inode is marked +* with I_FREEING, and etc. +*/ +if (NInoRunlistDirty (ni)) { + err =ntfs_cluster_free_from_rl (ni->vol, ni->runlist.rl); +if (err) +ntfs_error (sb, +"Failed to free clusters. Leaving inconsistent metadata.\n" ); + } + +while ((err =ntfs_attrs_walk (actx)) ==0 ) { +if (actx->attr->non_resident && + (!NInoRunlistDirty (ni) || actx->attr->type !=AT_DATA )) { +struct runlist_element *rl; +size_t new_rl_count; + + rl =ntfs_mapping_pairs_decompress (ni->vol, actx->attr,NULL , + &new_rl_count); +if (IS_ERR (rl)) { + err =PTR_ERR (rl); +ntfs_error (sb, +"Failed to decompress runlist. Leaving inconsistent metadata.\n" ); +continue ; + } + + err =ntfs_cluster_free_from_rl (ni->vol, rl); +if (err) +ntfs_error (sb, +"Failed to free attribute clusters. Leaving inconsistent metadata.\n" ); +ntfs_free (rl); + } + } + +ntfs_release_dirty_clusters (ni->vol, ni->i_dealloc_clusters); +ntfs_attr_put_search_ctx (actx); +return err; +} + +int ntfs_drop_big_inode (struct inode *inode) +{ +struct ntfs_inode *ni =NTFS_I (inode); + +if (!inode_unhashed (inode) && inode->i_state &I_SYNC ) { +if (ni->type ==AT_DATA || ni->type ==AT_INDEX_ALLOCATION ) { +if (!inode->i_nlink) { +struct ntfs_inode *ni =NTFS_I (inode); + +if (ni->data_size ==0 ) +return 0 ; + +/* To avoid evict_inode call simultaneously */ +atomic_inc (&inode->i_count); +spin_unlock (&inode->i_lock); + +truncate_setsize (VFS_I (ni),0 ); +ntfs_truncate_vfs (VFS_I (ni),0 ,1 ); + +sb_start_intwrite (inode->i_sb); +i_size_write (inode,0 ); + ni->allocated_size = ni->initialized_size = ni->data_size =0 ; + +truncate_inode_pages_final (inode->i_mapping); +sb_end_intwrite (inode->i_sb); + +spin_lock (&inode->i_lock); +atomic_dec (&inode->i_count); + } +return 0 ; + }else if (ni->type ==AT_INDEX_ROOT ) +return 0 ; + } + +return inode_generic_drop (inode); +} + +static inline struct ntfs_inode *ntfs_alloc_extent_inode (void ) +{ +struct ntfs_inode *ni; + +ntfs_debug ("Entering." ); + ni =kmem_cache_alloc (ntfs_inode_cache,GFP_NOFS ); +if (likely (ni !=NULL )) { + ni->state =0 ; +return ni; + } +ntfs_error (NULL ,"Allocation of NTFS inode structure failed." ); +return NULL ; +} + +static void ntfs_destroy_extent_inode (struct ntfs_inode *ni) +{ +ntfs_debug ("Entering." ); + +if (!atomic_dec_and_test (&ni->count)) +WARN_ON (1 ); +if (ni->folio) +ntfs_unmap_folio (ni->folio,NULL ); +kfree (ni->mrec); +kmem_cache_free (ntfs_inode_cache, ni); +} + +static struct lock_class_key attr_inode_mrec_lock_class; +static struct lock_class_key attr_list_inode_mrec_lock_class; + +/* +* The attribute runlist lock has separate locking rules from the +* normal runlist lock, so split the two lock-classes: +*/ +static struct lock_class_key attr_list_rl_lock_class; + +/** +* __ntfs_init_inode - initialize ntfs specific part of an inode +* @sb: super block of mounted volume +* @ni: freshly allocated ntfs inode which to initialize +* +* Initialize an ntfs inode to defaults. +* +* NOTE: ni->mft_no, ni->state, ni->type, ni->name, and ni->name_len are left +* untouched. Make sure to initialize them elsewhere. +*/ +void __ntfs_init_inode (struct super_block *sb,struct ntfs_inode *ni) +{ +ntfs_debug ("Entering." ); +rwlock_init (&ni->size_lock); + ni->initialized_size = ni->allocated_size =0 ; + ni->seq_no =0 ; +atomic_set (&ni->count,1 ); + ni->vol =NTFS_SB (sb); +ntfs_init_runlist (&ni->runlist); + ni->lcn_seek_trunc =LCN_RL_NOT_MAPPED ; +mutex_init (&ni->mrec_lock); +if (ni->type ==AT_ATTRIBUTE_LIST ) { +lockdep_set_class (&ni->mrec_lock, + &attr_list_inode_mrec_lock_class); +lockdep_set_class (&ni->runlist.lock, + &attr_list_rl_lock_class); + }else if (NInoAttr (ni)) { +lockdep_set_class (&ni->mrec_lock, + &attr_inode_mrec_lock_class); + } + + ni->folio =NULL ; + ni->folio_ofs =0 ; + ni->mrec =NULL ; + ni->attr_list_size =0 ; + ni->attr_list =NULL ; + ni->itype.index.block_size =0 ; + ni->itype.index.vcn_size =0 ; + ni->itype.index.collation_rule =0 ; + ni->itype.index.block_size_bits =0 ; + ni->itype.index.vcn_size_bits =0 ; +mutex_init (&ni->extent_lock); + ni->nr_extents =0 ; + ni->ext.base_ntfs_ino =NULL ; + ni->flags =0 ; + ni->mft_lcn[0 ] =LCN_RL_NOT_MAPPED ; + ni->mft_lcn_count =0 ; + ni->target =NULL ; + ni->i_dealloc_clusters =0 ; +} + +/* +* Extent inodes get MFT-mapped in a nested way, while the base inode +* is still mapped. Teach this nesting to the lock validator by creating +* a separate class for nested inode's mrec_lock's: +*/ +static struct lock_class_key extent_inode_mrec_lock_key; + +inline struct ntfs_inode *ntfs_new_extent_inode (struct super_block *sb, +unsigned long mft_no) +{ +struct ntfs_inode *ni =ntfs_alloc_extent_inode (); + +ntfs_debug ("Entering." ); +if (likely (ni !=NULL )) { +__ntfs_init_inode (sb, ni); +lockdep_set_class (&ni->mrec_lock, &extent_inode_mrec_lock_key); + ni->mft_no = mft_no; + ni->type =AT_UNUSED ; + ni->name =NULL ; + ni->name_len =0 ; + } +return ni; +} + +/** +* ntfs_is_extended_system_file - check if a file is in the $Extend directory +* @ctx: initialized attribute search context +* +* Search all file name attributes in the inode described by the attribute +* search context @ctx and check if any of the names are in the $Extend system +* directory. +* +* Return values: +* 1: file is in $Extend directory +* 0: file is not in $Extend directory +* -errno: failed to determine if the file is in the $Extend directory +*/ +static int ntfs_is_extended_system_file (struct ntfs_attr_search_ctx *ctx) +{ +int nr_links, err; + +/* Restart search. */ +ntfs_attr_reinit_search_ctx (ctx); + +/* Get number of hard links. */ + nr_links =le16_to_cpu (ctx->mrec->link_count); + +/* Loop through all hard links. */ +while (!(err =ntfs_attr_lookup (AT_FILE_NAME ,NULL ,0 ,0 ,0 ,NULL ,0 , + ctx))) { +struct file_name_attr *file_name_attr; +struct attr_record *attr = ctx->attr; +u8 *p, *p2; + + nr_links--; +/* +* Maximum sanity checking as we are called on an inode that +* we suspect might be corrupt. +*/ + p = (u8 *)attr +le32_to_cpu (attr->length); +if (p < (u8 *)ctx->mrec || (u8 *)p > (u8 *)ctx->mrec + +le32_to_cpu (ctx->mrec->bytes_in_use)) { +err_corrupt_attr: +ntfs_error (ctx->ntfs_ino->vol->sb, +"Corrupt file name attribute. You should run chkdsk." ); +return -EIO ; + } +if (attr->non_resident) { +ntfs_error (ctx->ntfs_ino->vol->sb, +"Non-resident file name. You should run chkdsk." ); +return -EIO ; + } +if (attr->flags) { +ntfs_error (ctx->ntfs_ino->vol->sb, +"File name with invalid flags. You should run chkdsk." ); +return -EIO ; + } +if (!(attr->data.resident.flags &RESIDENT_ATTR_IS_INDEXED )) { +ntfs_error (ctx->ntfs_ino->vol->sb, +"Unindexed file name. You should run chkdsk." ); +return -EIO ; + } + file_name_attr = (struct file_name_attr *)((u8 *)attr + +le16_to_cpu (attr->data.resident.value_offset)); + p2 = (u8 *)file_name_attr +le32_to_cpu (attr->data.resident.value_length); +if (p2 < (u8 *)attr || p2 > p) + goto err_corrupt_attr; +/* This attribute is ok, but is it in the $Extend directory? */ +if (MREF_LE (file_name_attr->parent_directory) == FILE_Extend) { +unsigned char *s; + + s =ntfs_attr_name_get (ctx->ntfs_ino->vol, + file_name_attr->file_name, + file_name_attr->file_name_length); +if (!s) +return 1 ; +if (!strcmp ("$Reparse" , s)) { +ntfs_attr_name_free (&s); +return 2 ;/* it's reparse point file */ + } +ntfs_attr_name_free (&s); +return 1 ;/* YES, it's an extended system file. */ + } + } +if (unlikely (err != -ENOENT )) +return err; +if (unlikely (nr_links)) { +ntfs_error (ctx->ntfs_ino->vol->sb, +"Inode hard link count doesn't match number of name attributes. You should run chkdsk." ); +return -EIO ; + } +return 0 ;/* NO, it is not an extended system file. */ +} + +static struct lock_class_key ntfs_dir_inval_lock_key; + +void ntfs_set_vfs_operations (struct inode *inode,mode_t mode,dev_t dev) +{ +if (S_ISDIR (mode)) { +if (!NInoAttr (NTFS_I (inode))) { + inode->i_op = &ntfs_dir_inode_ops; + inode->i_fop = &ntfs_dir_ops; + } +if (NInoMstProtected (NTFS_I (inode))) + inode->i_mapping->a_ops = &ntfs_mst_aops; +else + inode->i_mapping->a_ops = &ntfs_normal_aops; +lockdep_set_class (&inode->i_mapping->invalidate_lock, + &ntfs_dir_inval_lock_key); + }else if (S_ISLNK (mode)) { + inode->i_op = &ntfs_symlink_inode_operations; + inode->i_mapping->a_ops = &ntfs_normal_aops; + }else if (S_ISCHR (mode) ||S_ISBLK (mode) ||S_ISFIFO (mode) ||S_ISSOCK (mode)) { + inode->i_op = &ntfsp_special_inode_operations; +init_special_inode (inode, inode->i_mode, dev); + }else { +if (!NInoAttr (NTFS_I (inode))) { + inode->i_op = &ntfs_file_inode_ops; + inode->i_fop = &ntfs_file_ops; + } +if (NInoMstProtected (NTFS_I (inode))) + inode->i_mapping->a_ops = &ntfs_mst_aops; +else if (NInoCompressed (NTFS_I (inode))) + inode->i_mapping->a_ops = &ntfs_compressed_aops; +else + inode->i_mapping->a_ops = &ntfs_normal_aops; + } +} + +__le16 R [3 ] = {cpu_to_le16 ('$' ),cpu_to_le16 ('R' ),0 }; + +/** +* ntfs_read_locked_inode - read an inode from its device +* @vi: inode to read +* +* ntfs_read_locked_inode() is called from ntfs_iget() to read the inode +* described by @vi into memory from the device. +* +* The only fields in @vi that we need to/can look at when the function is +* called are i_sb, pointing to the mounted device's super block, and i_ino, +* the number of the inode to load. +* +* ntfs_read_locked_inode() maps, pins and locks the mft record number i_ino +* for reading and sets up the necessary @vi fields as well as initializing +* the ntfs inode. +* +* Q: What locks are held when the function is called? +* A: i_state has I_NEW set, hence the inode is locked, also +* i_count is set to 1, so it is not going to go away +* i_flags is set to 0 and we have no business touching it. Only an ioctl() +* is allowed to write to them. We should of course be honouring them but +* we need to do that using the IS_* macros defined in include/linux/fs.h. +* In any case ntfs_read_locked_inode() has nothing to do with i_flags. +* +* Return 0 on success and -errno on error. +*/ +static int ntfs_read_locked_inode (struct inode *vi) +{ +struct ntfs_volume *vol =NTFS_SB (vi->i_sb); +struct ntfs_inode *ni; +struct mft_record *m; +struct attr_record *a; +struct standard_information *si; +struct ntfs_attr_search_ctx *ctx; +int err =0 ; +__le16 *name =I30 ; +unsigned int name_len =4 , flags =0 ; +int extend_sys =0 ; +dev_t dev =0 ; +bool vol_err = true; + +ntfs_debug ("Entering for i_ino 0x%lx." , vi->i_ino); + +if (uid_valid (vol->uid)) { + vi->i_uid = vol->uid; + flags |=NTFS_VOL_UID ; + }else + vi->i_uid =GLOBAL_ROOT_UID ; + +if (gid_valid (vol->gid)) { + vi->i_gid = vol->gid; + flags |=NTFS_VOL_GID ; + }else + vi->i_gid =GLOBAL_ROOT_GID ; + + vi->i_mode =0777 ; + +/* +* Initialize the ntfs specific part of @vi special casing +* FILE_MFT which we need to do at mount time. +*/ +if (vi->i_ino !=FILE_MFT ) +ntfs_init_big_inode (vi); + ni =NTFS_I (vi); + + m =map_mft_record (ni); +if (IS_ERR (m)) { + err =PTR_ERR (m); + goto err_out; + } + + ctx =ntfs_attr_get_search_ctx (ni, m); +if (!ctx) { + err = -ENOMEM ; + goto unm_err_out; + } + +if (!(m->flags &MFT_RECORD_IN_USE )) { + err = -ENOENT ; + vol_err = false; + goto unm_err_out; + } + +if (m->base_mft_record) { +ntfs_error (vi->i_sb,"Inode is an extent inode!" ); + goto unm_err_out; + } + +/* Transfer information from mft record into vfs and ntfs inodes. */ + vi->i_generation = ni->seq_no =le16_to_cpu (m->sequence_number); + +if (le16_to_cpu (m->link_count) <1 ) { +ntfs_error (vi->i_sb,"Inode link count is 0!" ); + goto unm_err_out; + } +set_nlink (vi,le16_to_cpu (m->link_count)); + +/* If read-only, no one gets write permissions. */ +if (IS_RDONLY (vi)) + vi->i_mode &= ~0222 ; + +/* +* Find the standard information attribute in the mft record. At this +* stage we haven't setup the attribute list stuff yet, so this could +* in fact fail if the standard information is in an extent record, but +* I don't think this actually ever happens. +*/ +ntfs_attr_reinit_search_ctx (ctx); + err =ntfs_attr_lookup (AT_STANDARD_INFORMATION ,NULL ,0 ,0 ,0 ,NULL ,0 , + ctx); +if (unlikely (err)) { +if (err == -ENOENT ) +ntfs_error (vi->i_sb,"$STANDARD_INFORMATION attribute is missing." ); + goto unm_err_out; + } + a = ctx->attr; +/* Get the standard information attribute value. */ +if ((u8 *)a +le16_to_cpu (a->data.resident.value_offset) + +le32_to_cpu (a->data.resident.value_length) > + (u8 *)ctx->mrec + vol->mft_record_size) { +ntfs_error (vi->i_sb,"Corrupt standard information attribute in inode." ); + goto unm_err_out; + } + si = (struct standard_information *)((u8 *)a + +le16_to_cpu (a->data.resident.value_offset)); + +/* Transfer information from the standard information into vi. */ +/* +* Note: The i_?times do not quite map perfectly onto the NTFS times, +* but they are close enough, and in the end it doesn't really matter +* that much... +*/ +/* +* mtime is the last change of the data within the file. Not changed +* when only metadata is changed, e.g. a rename doesn't affect mtime. +*/ + ni->i_crtime =ntfs2utc (si->creation_time); + +inode_set_mtime_to_ts (vi,ntfs2utc (si->last_data_change_time)); +/* +* ctime is the last change of the metadata of the file. This obviously +* always changes, when mtime is changed. ctime can be changed on its +* own, mtime is then not changed, e.g. when a file is renamed. +*/ +inode_set_ctime_to_ts (vi,ntfs2utc (si->last_mft_change_time)); +/* +* Last access to the data within the file. Not changed during a rename +* for example but changed whenever the file is written to. +*/ +inode_set_atime_to_ts (vi,ntfs2utc (si->last_access_time)); + ni->flags = si->file_attributes; + +/* Find the attribute list attribute if present. */ +ntfs_attr_reinit_search_ctx (ctx); + err =ntfs_attr_lookup (AT_ATTRIBUTE_LIST ,NULL ,0 ,0 ,0 ,NULL ,0 , ctx); +if (err) { +if (unlikely (err != -ENOENT )) { +ntfs_error (vi->i_sb,"Failed to lookup attribute list attribute." ); + goto unm_err_out; + } + }else { +if (vi->i_ino ==FILE_MFT ) + goto skip_attr_list_load; +ntfs_debug ("Attribute list found in inode 0x%lx." , vi->i_ino); +NInoSetAttrList (ni); + a = ctx->attr; +if (a->flags &ATTR_COMPRESSION_MASK ) { +ntfs_error (vi->i_sb, +"Attribute list attribute is compressed." ); + goto unm_err_out; + } +if (a->flags &ATTR_IS_ENCRYPTED || + a->flags &ATTR_IS_SPARSE ) { +if (a->non_resident) { +ntfs_error (vi->i_sb, +"Non-resident attribute list attribute is encrypted/sparse." ); + goto unm_err_out; + } +ntfs_warning (vi->i_sb, +"Resident attribute list attribute in inode 0x%lx is marked encrypted/sparse which is not true. However, Windows allows this and chkdsk does not detect or correct it so we will just ignore the invalid flags and pretend they are not set." , + vi->i_ino); + } +/* Now allocate memory for the attribute list. */ + ni->attr_list_size = (u32 )ntfs_attr_size (a); +if (!ni->attr_list_size) { +ntfs_error (vi->i_sb,"Attr_list_size is zero" ); + goto unm_err_out; + } + ni->attr_list =ntfs_malloc_nofs (ni->attr_list_size); +if (!ni->attr_list) { +ntfs_error (vi->i_sb, +"Not enough memory to allocate buffer for attribute list." ); + err = -ENOMEM ; + goto unm_err_out; + } +if (a->non_resident) { +NInoSetAttrListNonResident (ni); +if (a->data.non_resident.lowest_vcn) { +ntfs_error (vi->i_sb,"Attribute list has non zero lowest_vcn." ); + goto unm_err_out; + } + +/* Now load the attribute list. */ + err =load_attribute_list (ni, ni->attr_list, ni->attr_list_size); +if (err) { +ntfs_error (vi->i_sb,"Failed to load attribute list attribute." ); + goto unm_err_out; + } + }else /* if (!a->non_resident) */ { +if ((u8 *)a +le16_to_cpu (a->data.resident.value_offset) + +le32_to_cpu ( + a->data.resident.value_length) > + (u8 *)ctx->mrec + vol->mft_record_size) { +ntfs_error (vi->i_sb,"Corrupt attribute list in inode." ); + goto unm_err_out; + } +/* Now copy the attribute list. */ +memcpy (ni->attr_list, (u8 *)a +le16_to_cpu ( + a->data.resident.value_offset), +le32_to_cpu ( + a->data.resident.value_length)); + } + } +skip_attr_list_load: + err =ntfs_attr_lookup (AT_EA_INFORMATION ,NULL ,0 ,0 ,0 ,NULL ,0 , ctx); +if (!err) +NInoSetHasEA (ni); + +ntfs_ea_get_wsl_inode (vi, &dev, flags); + +if (m->flags &MFT_RECORD_IS_DIRECTORY ) { + vi->i_mode |=S_IFDIR ; +/* +* Apply the directory permissions mask set in the mount +* options. +*/ + vi->i_mode &= ~vol->dmask; +/* Things break without this kludge! */ +if (vi->i_nlink >1 ) +set_nlink (vi,1 ); + }else { +if (ni->flags &FILE_ATTR_REPARSE_POINT ) { +unsigned int mode; + + mode =ntfs_make_symlink (ni); +if (mode) + vi->i_mode |= mode; +else { + vi->i_mode &= ~S_IFLNK ; + vi->i_mode |=S_IFREG ; + } + }else + vi->i_mode |=S_IFREG ; +/* Apply the file permissions mask set in the mount options. */ + vi->i_mode &= ~vol->fmask; + } + +/* +* If an attribute list is present we now have the attribute list value +* in ntfs_ino->attr_list and it is ntfs_ino->attr_list_size bytes. +*/ +if (S_ISDIR (vi->i_mode)) { +struct index_root *ir; +u8 *ir_end, *index_end; + +view_index_meta: +/* It is a directory, find index root attribute. */ +ntfs_attr_reinit_search_ctx (ctx); + err =ntfs_attr_lookup (AT_INDEX_ROOT , name, name_len,CASE_SENSITIVE , +0 ,NULL ,0 , ctx); +if (unlikely (err)) { +if (err == -ENOENT ) +ntfs_error (vi->i_sb,"$INDEX_ROOT attribute is missing." ); + goto unm_err_out; + } + a = ctx->attr; +/* Set up the state. */ +if (unlikely (a->non_resident)) { +ntfs_error (vol->sb, +"$INDEX_ROOT attribute is not resident." ); + goto unm_err_out; + } +/* Ensure the attribute name is placed before the value. */ +if (unlikely (a->name_length && (le16_to_cpu (a->name_offset) >= +le16_to_cpu (a->data.resident.value_offset)))) { +ntfs_error (vol->sb, +"$INDEX_ROOT attribute name is placed after the attribute value." ); + goto unm_err_out; + } +/* +* Compressed/encrypted index root just means that the newly +* created files in that directory should be created compressed/ +* encrypted. However index root cannot be both compressed and +* encrypted. +*/ +if (a->flags &ATTR_COMPRESSION_MASK ) { +NInoSetCompressed (ni); + ni->flags |=FILE_ATTR_COMPRESSED ; + } +if (a->flags &ATTR_IS_ENCRYPTED ) { +if (a->flags &ATTR_COMPRESSION_MASK ) { +ntfs_error (vi->i_sb,"Found encrypted and compressed attribute." ); + goto unm_err_out; + } +NInoSetEncrypted (ni); + ni->flags |=FILE_ATTR_ENCRYPTED ; + } +if (a->flags &ATTR_IS_SPARSE ) { +NInoSetSparse (ni); + ni->flags |=FILE_ATTR_SPARSE_FILE ; + } + ir = (struct index_root *)((u8 *)a + +le16_to_cpu (a->data.resident.value_offset)); + ir_end = (u8 *)ir +le32_to_cpu (a->data.resident.value_length); +if (ir_end > (u8 *)ctx->mrec + vol->mft_record_size) { +ntfs_error (vi->i_sb,"$INDEX_ROOT attribute is corrupt." ); + goto unm_err_out; + } + index_end = (u8 *)&ir->index + +le32_to_cpu (ir->index.index_length); +if (index_end > ir_end) { +ntfs_error (vi->i_sb,"Directory index is corrupt." ); + goto unm_err_out; + } + +if (extend_sys) { +if (ir->type) { +ntfs_error (vi->i_sb,"Indexed attribute is not zero." ); + goto unm_err_out; + } + }else { +if (ir->type !=AT_FILE_NAME ) { +ntfs_error (vi->i_sb,"Indexed attribute is not $FILE_NAME." ); + goto unm_err_out; + } + +if (ir->collation_rule !=COLLATION_FILE_NAME ) { +ntfs_error (vi->i_sb, +"Index collation rule is not COLLATION_FILE_NAME." ); + goto unm_err_out; + } + } + + ni->itype.index.collation_rule = ir->collation_rule; + ni->itype.index.block_size =le32_to_cpu (ir->index_block_size); +if (ni->itype.index.block_size & + (ni->itype.index.block_size -1 )) { +ntfs_error (vi->i_sb,"Index block size (%u) is not a power of two." , + ni->itype.index.block_size); + goto unm_err_out; + } +if (ni->itype.index.block_size >PAGE_SIZE ) { +ntfs_error (vi->i_sb, +"Index block size (%u) > PAGE_SIZE (%ld) is not supported." , + ni->itype.index.block_size, +PAGE_SIZE ); + err = -EOPNOTSUPP ; + goto unm_err_out; + } +if (ni->itype.index.block_size <NTFS_BLOCK_SIZE ) { +ntfs_error (vi->i_sb, +"Index block size (%u) < NTFS_BLOCK_SIZE (%i) is not supported." , + ni->itype.index.block_size, +NTFS_BLOCK_SIZE ); + err = -EOPNOTSUPP ; + goto unm_err_out; + } + ni->itype.index.block_size_bits = +ffs (ni->itype.index.block_size) -1 ; +/* Determine the size of a vcn in the directory index. */ +if (vol->cluster_size <= ni->itype.index.block_size) { + ni->itype.index.vcn_size = vol->cluster_size; + ni->itype.index.vcn_size_bits = vol->cluster_size_bits; + }else { + ni->itype.index.vcn_size = vol->sector_size; + ni->itype.index.vcn_size_bits = vol->sector_size_bits; + } + +/* Setup the index allocation attribute, even if not present. */ + ni->type =AT_INDEX_ROOT ; + ni->name = name; + ni->name_len = name_len; + vi->i_size = ni->initialized_size = ni->data_size = +le32_to_cpu (a->data.resident.value_length); + ni->allocated_size = (ni->data_size +7 ) & ~7 ; +/* We are done with the mft record, so we release it. */ +ntfs_attr_put_search_ctx (ctx); +unmap_mft_record (ni); + m =NULL ; + ctx =NULL ; +/* Setup the operations for this inode. */ +ntfs_set_vfs_operations (vi,S_IFDIR ,0 ); +if (ir->index.flags &LARGE_INDEX ) +NInoSetIndexAllocPresent (ni); + }else { +/* It is a file. */ +ntfs_attr_reinit_search_ctx (ctx); + +/* Setup the data attribute, even if not present. */ + ni->type =AT_DATA ; + ni->name =AT_UNNAMED ; + ni->name_len =0 ; + +/* Find first extent of the unnamed data attribute. */ + err =ntfs_attr_lookup (AT_DATA ,NULL ,0 ,0 ,0 ,NULL ,0 , ctx); +if (unlikely (err)) { + vi->i_size = ni->initialized_size = + ni->allocated_size =0 ; +if (err != -ENOENT ) { +ntfs_error (vi->i_sb,"Failed to lookup $DATA attribute." ); + goto unm_err_out; + } +/* +* FILE_Secure does not have an unnamed $DATA +* attribute, so we special case it here. +*/ +if (vi->i_ino == FILE_Secure) + goto no_data_attr_special_case; +/* +* Most if not all the system files in the $Extend +* system directory do not have unnamed data +* attributes so we need to check if the parent +* directory of the file is FILE_Extend and if it is +* ignore this error. To do this we need to get the +* name of this inode from the mft record as the name +* contains the back reference to the parent directory. +*/ + extend_sys =ntfs_is_extended_system_file (ctx); +if (extend_sys >0 ) { +if (m->flags &MFT_RECORD_IS_VIEW_INDEX && + extend_sys ==2 ) { + name =R ; + name_len =2 ; + goto view_index_meta; + } + goto no_data_attr_special_case; + } + + err = extend_sys; +ntfs_error (vi->i_sb,"$DATA attribute is missing, err : %d" , err); + goto unm_err_out; + } + a = ctx->attr; +/* Setup the state. */ +if (a->flags & (ATTR_COMPRESSION_MASK |ATTR_IS_SPARSE )) { +if (a->flags &ATTR_COMPRESSION_MASK ) { +NInoSetCompressed (ni); + ni->flags |=FILE_ATTR_COMPRESSED ; +if (vol->cluster_size >4096 ) { +ntfs_error (vi->i_sb, +"Found compressed data but compression is disabled due to cluster size (%i) > 4kiB." , + vol->cluster_size); + goto unm_err_out; + } +if ((a->flags &ATTR_COMPRESSION_MASK ) + !=ATTR_IS_COMPRESSED ) { +ntfs_error (vi->i_sb, +"Found unknown compression method or corrupt file." ); + goto unm_err_out; + } + } +if (a->flags &ATTR_IS_SPARSE ) { +NInoSetSparse (ni); + ni->flags |=FILE_ATTR_SPARSE_FILE ; + } + } +if (a->flags &ATTR_IS_ENCRYPTED ) { +if (NInoCompressed (ni)) { +ntfs_error (vi->i_sb,"Found encrypted and compressed data." ); + goto unm_err_out; + } +NInoSetEncrypted (ni); + ni->flags |=FILE_ATTR_ENCRYPTED ; + } +if (a->non_resident) { +NInoSetNonResident (ni); +if (NInoCompressed (ni) ||NInoSparse (ni)) { +if (NInoCompressed (ni) && + a->data.non_resident.compression_unit !=4 ) { +ntfs_error (vi->i_sb, +"Found non-standard compression unit (%u instead of 4). Cannot handle this." , + a->data.non_resident.compression_unit); + err = -EOPNOTSUPP ; + goto unm_err_out; + } + +if (NInoSparse (ni) && + a->data.non_resident.compression_unit && + a->data.non_resident.compression_unit != + vol->sparse_compression_unit) { +ntfs_error (vi->i_sb, +"Found non-standard compression unit (%u instead of 0 or %d). Cannot handle this." , + a->data.non_resident.compression_unit, + vol->sparse_compression_unit); + err = -EOPNOTSUPP ; + goto unm_err_out; + } + + +if (a->data.non_resident.compression_unit) { + ni->itype.compressed.block_size =1U << + (a->data.non_resident.compression_unit + + vol->cluster_size_bits); + ni->itype.compressed.block_size_bits = +ffs (ni->itype.compressed.block_size) -1 ; + ni->itype.compressed.block_clusters = +1U << a->data.non_resident.compression_unit; + }else { + ni->itype.compressed.block_size =0 ; + ni->itype.compressed.block_size_bits = +0 ; + ni->itype.compressed.block_clusters = +0 ; + } + ni->itype.compressed.size =le64_to_cpu ( + a->data.non_resident.compressed_size); + } +if (a->data.non_resident.lowest_vcn) { +ntfs_error (vi->i_sb, +"First extent of $DATA attribute has non zero lowest_vcn." ); + goto unm_err_out; + } + vi->i_size = ni->data_size =le64_to_cpu (a->data.non_resident.data_size); + ni->initialized_size =le64_to_cpu (a->data.non_resident.initialized_size); + ni->allocated_size =le64_to_cpu (a->data.non_resident.allocated_size); + }else {/* Resident attribute. */ + vi->i_size = ni->data_size = ni->initialized_size =le32_to_cpu ( + a->data.resident.value_length); + ni->allocated_size =le32_to_cpu (a->length) - +le16_to_cpu ( + a->data.resident.value_offset); +if (vi->i_size > ni->allocated_size) { +ntfs_error (vi->i_sb, +"Resident data attribute is corrupt (size exceeds allocation)." ); + goto unm_err_out; + } + } +no_data_attr_special_case: +/* We are done with the mft record, so we release it. */ +ntfs_attr_put_search_ctx (ctx); +unmap_mft_record (ni); + m =NULL ; + ctx =NULL ; +/* Setup the operations for this inode. */ +ntfs_set_vfs_operations (vi, vi->i_mode, dev); + } + +if (NVolSysImmutable (vol) && (ni->flags &FILE_ATTR_SYSTEM ) && + !S_ISFIFO (vi->i_mode) && !S_ISSOCK (vi->i_mode) && !S_ISLNK (vi->i_mode)) + vi->i_flags |=S_IMMUTABLE ; + +/* +* The number of 512-byte blocks used on disk (for stat). This is in so +* far inaccurate as it doesn't account for any named streams or other +* special non-resident attributes, but that is how Windows works, too, +* so we are at least consistent with Windows, if not entirely +* consistent with the Linux Way. Doing it the Linux Way would cause a +* significant slowdown as it would involve iterating over all +* attributes in the mft record and adding the allocated/compressed +* sizes of all non-resident attributes present to give us the Linux +* correct size that should go into i_blocks (after division by 512). +*/ +if (S_ISREG (vi->i_mode) && (NInoCompressed (ni) ||NInoSparse (ni))) + vi->i_blocks = ni->itype.compressed.size >>9 ; +else + vi->i_blocks = ni->allocated_size >>9 ; + +ntfs_debug ("Done." ); +return 0 ; +unm_err_out: +if (!err) + err = -EIO ; +if (ctx) +ntfs_attr_put_search_ctx (ctx); +if (m) +unmap_mft_record (ni); +err_out: +if (err != -EOPNOTSUPP && err != -ENOMEM && vol_err == true) { +ntfs_error (vol->sb, +"Failed with error code %i. Marking corrupt inode 0x%lx as bad. Run chkdsk." , + err, vi->i_ino); +NVolSetErrors (vol); + } +return err; +} + +/** +* ntfs_read_locked_attr_inode - read an attribute inode from its base inode +* @base_vi: base inode +* @vi: attribute inode to read +* +* ntfs_read_locked_attr_inode() is called from ntfs_attr_iget() to read the +* attribute inode described by @vi into memory from the base mft record +* described by @base_ni. +* +* ntfs_read_locked_attr_inode() maps, pins and locks the base inode for +* reading and looks up the attribute described by @vi before setting up the +* necessary fields in @vi as well as initializing the ntfs inode. +* +* Q: What locks are held when the function is called? +* A: i_state has I_NEW set, hence the inode is locked, also +* i_count is set to 1, so it is not going to go away +* +* Return 0 on success and -errno on error. +* +* Note this cannot be called for AT_INDEX_ALLOCATION. +*/ +static int ntfs_read_locked_attr_inode (struct inode *base_vi,struct inode *vi) +{ +struct ntfs_volume *vol =NTFS_SB (vi->i_sb); +struct ntfs_inode *ni =NTFS_I (vi), *base_ni =NTFS_I (base_vi); +struct mft_record *m; +struct attr_record *a; +struct ntfs_attr_search_ctx *ctx; +int err =0 ; + +ntfs_debug ("Entering for i_ino 0x%lx." , vi->i_ino); + +ntfs_init_big_inode (vi); + +/* Just mirror the values from the base inode. */ + vi->i_uid = base_vi->i_uid; + vi->i_gid = base_vi->i_gid; +set_nlink (vi, base_vi->i_nlink); +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 = ni->seq_no = base_ni->seq_no; + +/* Set inode type to zero but preserve permissions. */ + vi->i_mode = base_vi->i_mode & ~S_IFMT ; + + m =map_mft_record (base_ni); +if (IS_ERR (m)) { + err =PTR_ERR (m); + goto err_out; + } + ctx =ntfs_attr_get_search_ctx (base_ni, m); +if (!ctx) { + err = -ENOMEM ; + goto unm_err_out; + } +/* Find the attribute. */ + err =ntfs_attr_lookup (ni->type, ni->name, ni->name_len, +CASE_SENSITIVE ,0 ,NULL ,0 , ctx); +if (unlikely (err)) + goto unm_err_out; + a = ctx->attr; +if (a->flags & (ATTR_COMPRESSION_MASK |ATTR_IS_SPARSE )) { +if (a->flags &ATTR_COMPRESSION_MASK ) { +NInoSetCompressed (ni); + ni->flags |=FILE_ATTR_COMPRESSED ; +if ((ni->type !=AT_DATA ) || (ni->type ==AT_DATA && + ni->name_len)) { +ntfs_error (vi->i_sb, +"Found compressed non-data or named data attribute." ); + goto unm_err_out; + } +if (vol->cluster_size >4096 ) { +ntfs_error (vi->i_sb, +"Found compressed attribute but compression is disabled due to cluster size (%i) > 4kiB." , + vol->cluster_size); + goto unm_err_out; + } +if ((a->flags &ATTR_COMPRESSION_MASK ) != +ATTR_IS_COMPRESSED ) { +ntfs_error (vi->i_sb,"Found unknown compression method." ); + goto unm_err_out; + } + } +/* +* The compressed/sparse flag set in an index root just means +* to compress all files. +*/ +if (NInoMstProtected (ni) && ni->type !=AT_INDEX_ROOT ) { +ntfs_error (vi->i_sb, +"Found mst protected attribute but the attribute is %s." , +NInoCompressed (ni) ?"compressed" :"sparse" ); + goto unm_err_out; + } +if (a->flags &ATTR_IS_SPARSE ) { +NInoSetSparse (ni); + ni->flags |=FILE_ATTR_SPARSE_FILE ; + } + } +if (a->flags &ATTR_IS_ENCRYPTED ) { +if (NInoCompressed (ni)) { +ntfs_error (vi->i_sb,"Found encrypted and compressed data." ); + goto unm_err_out; + } +/* +* The encryption flag set in an index root just means to +* encrypt all files. +*/ +if (NInoMstProtected (ni) && ni->type !=AT_INDEX_ROOT ) { +ntfs_error (vi->i_sb, +"Found mst protected attribute but the attribute is encrypted." ); + goto unm_err_out; + } +if (ni->type !=AT_DATA ) { +ntfs_error (vi->i_sb, +"Found encrypted non-data attribute." ); + goto unm_err_out; + } +NInoSetEncrypted (ni); + ni->flags |=FILE_ATTR_ENCRYPTED ; + } +if (!a->non_resident) { +/* Ensure the attribute name is placed before the value. */ +if (unlikely (a->name_length && (le16_to_cpu (a->name_offset) >= +le16_to_cpu (a->data.resident.value_offset)))) { +ntfs_error (vol->sb, +"Attribute name is placed after the attribute value." ); + goto unm_err_out; + } +if (NInoMstProtected (ni)) { +ntfs_error (vi->i_sb, +"Found mst protected attribute but the attribute is resident." ); + goto unm_err_out; + } + vi->i_size = ni->initialized_size = ni->data_size =le32_to_cpu ( + a->data.resident.value_length); + ni->allocated_size =le32_to_cpu (a->length) - +le16_to_cpu (a->data.resident.value_offset); +if (vi->i_size > ni->allocated_size) { +ntfs_error (vi->i_sb, +"Resident attribute is corrupt (size exceeds allocation)." ); + goto unm_err_out; + } + }else { +NInoSetNonResident (ni); +/* +* Ensure the attribute name is placed before the mapping pairs +* array. +*/ +if (unlikely (a->name_length && (le16_to_cpu (a->name_offset) >= +le16_to_cpu ( + a->data.non_resident.mapping_pairs_offset)))) { +ntfs_error (vol->sb, +"Attribute name is placed after the mapping pairs array." ); + goto unm_err_out; + } +if (NInoCompressed (ni) ||NInoSparse (ni)) { +if (NInoCompressed (ni) && a->data.non_resident.compression_unit !=4 ) { +ntfs_error (vi->i_sb, +"Found non-standard compression unit (%u instead of 4). Cannot handle this." , + a->data.non_resident.compression_unit); + err = -EOPNOTSUPP ; + goto unm_err_out; + } +if (a->data.non_resident.compression_unit) { + ni->itype.compressed.block_size =1U << + (a->data.non_resident.compression_unit + + vol->cluster_size_bits); + ni->itype.compressed.block_size_bits = +ffs (ni->itype.compressed.block_size) -1 ; + ni->itype.compressed.block_clusters =1U << + a->data.non_resident.compression_unit; + }else { + ni->itype.compressed.block_size =0 ; + ni->itype.compressed.block_size_bits =0 ; + ni->itype.compressed.block_clusters =0 ; + } + ni->itype.compressed.size =le64_to_cpu ( + a->data.non_resident.compressed_size); + } +if (a->data.non_resident.lowest_vcn) { +ntfs_error (vi->i_sb,"First extent of attribute has non-zero lowest_vcn." ); + goto unm_err_out; + } + vi->i_size = ni->data_size =le64_to_cpu (a->data.non_resident.data_size); + ni->initialized_size =le64_to_cpu (a->data.non_resident.initialized_size); + ni->allocated_size =le64_to_cpu (a->data.non_resident.allocated_size); + } + vi->i_mapping->a_ops = &ntfs_normal_aops; +if (NInoMstProtected (ni)) + vi->i_mapping->a_ops = &ntfs_mst_aops; +else if (NInoCompressed (ni)) + vi->i_mapping->a_ops = &ntfs_compressed_aops; +if ((NInoCompressed (ni) ||NInoSparse (ni)) && ni->type !=AT_INDEX_ROOT ) + vi->i_blocks = ni->itype.compressed.size >>9 ; +else + vi->i_blocks = ni->allocated_size >>9 ; +/* +* Make sure the base inode does not go away and attach it to the +* attribute inode. +*/ +if (!igrab (base_vi)) { + err = -ENOENT ; + goto unm_err_out; + } + ni->ext.base_ntfs_ino = base_ni; + ni->nr_extents =-1 ; + +ntfs_attr_put_search_ctx (ctx); +unmap_mft_record (base_ni); + +ntfs_debug ("Done." ); +return 0 ; + +unm_err_out: +if (!err) + err = -EIO ; +if (ctx) +ntfs_attr_put_search_ctx (ctx); +unmap_mft_record (base_ni); +err_out: +if (err != -ENOENT ) +ntfs_error (vol->sb, +"Failed with error code %i while reading attribute inode (mft_no 0x%lx, type 0x%x, name_len %i). Marking corrupt inode and base inode 0x%lx as bad. Run chkdsk." , + err, vi->i_ino, ni->type, ni->name_len, + base_vi->i_ino); +if (err != -ENOENT && err != -ENOMEM ) +NVolSetErrors (vol); +return err; +} + +/** +* ntfs_read_locked_index_inode - read an index inode from its base inode +* @base_vi: base inode +* @vi: index inode to read +* +* ntfs_read_locked_index_inode() is called from ntfs_index_iget() to read the +* index inode described by @vi into memory from the base mft record described +* by @base_ni. +* +* ntfs_read_locked_index_inode() maps, pins and locks the base inode for +* reading and looks up the attributes relating to the index described by @vi +* before setting up the necessary fields in @vi as well as initializing the +* ntfs inode. +* +* Note, index inodes are essentially attribute inodes (NInoAttr() is true) +* with the attribute type set to AT_INDEX_ALLOCATION. Apart from that, they +* are setup like directory inodes since directories are a special case of +* indices ao they need to be treated in much the same way. Most importantly, +* for small indices the index allocation attribute might not actually exist. +* However, the index root attribute always exists but this does not need to +* have an inode associated with it and this is why we define a new inode type +* index. Also, like for directories, we need to have an attribute inode for +* the bitmap attribute corresponding to the index allocation attribute and we +* can store this in the appropriate field of the inode, just like we do for +* normal directory inodes. +* +* Q: What locks are held when the function is called? +* A: i_state has I_NEW set, hence the inode is locked, also +* i_count is set to 1, so it is not going to go away +* +* Return 0 on success and -errno on error. +*/ +static int ntfs_read_locked_index_inode (struct inode *base_vi,struct inode *vi) +{ +loff_t bvi_size; +struct ntfs_volume *vol =NTFS_SB (vi->i_sb); +struct ntfs_inode *ni =NTFS_I (vi), *base_ni =NTFS_I (base_vi), *bni; +struct inode *bvi; +struct mft_record *m; +struct attr_record *a; +struct ntfs_attr_search_ctx *ctx; +struct index_root *ir; +u8 *ir_end, *index_end; +int err =0 ; + +ntfs_debug ("Entering for i_ino 0x%lx." , vi->i_ino); +lockdep_assert_held (&base_ni->mrec_lock); + +ntfs_init_big_inode (vi); +/* Just mirror the values from the base inode. */ + vi->i_uid = base_vi->i_uid; + vi->i_gid = base_vi->i_gid; +set_nlink (vi, base_vi->i_nlink); +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 = ni->seq_no = base_ni->seq_no; +/* Set inode type to zero but preserve permissions. */ + vi->i_mode = base_vi->i_mode & ~S_IFMT ; +/* Map the mft record for the base inode. */ + m =map_mft_record (base_ni); +if (IS_ERR (m)) { + err =PTR_ERR (m); + goto err_out; + } + ctx =ntfs_attr_get_search_ctx (base_ni, m); +if (!ctx) { + err = -ENOMEM ; + goto unm_err_out; + } +/* Find the index root attribute. */ + err =ntfs_attr_lookup (AT_INDEX_ROOT , ni->name, ni->name_len, +CASE_SENSITIVE ,0 ,NULL ,0 , ctx); +if (unlikely (err)) { +if (err == -ENOENT ) +ntfs_error (vi->i_sb,"$INDEX_ROOT attribute is missing." ); + goto unm_err_out; + } + a = ctx->attr; +/* Set up the state. */ +if (unlikely (a->non_resident)) { +ntfs_error (vol->sb,"$INDEX_ROOT attribute is not resident." ); + goto unm_err_out; + } +/* Ensure the attribute name is placed before the value. */ +if (unlikely (a->name_length && (le16_to_cpu (a->name_offset) >= +le16_to_cpu (a->data.resident.value_offset)))) { +ntfs_error (vol->sb, +"$INDEX_ROOT attribute name is placed after the attribute value." ); + goto unm_err_out; + } + + ir = (struct index_root *)((u8 *)a +le16_to_cpu (a->data.resident.value_offset)); + ir_end = (u8 *)ir +le32_to_cpu (a->data.resident.value_length); +if (ir_end > (u8 *)ctx->mrec + vol->mft_record_size) { +ntfs_error (vi->i_sb,"$INDEX_ROOT attribute is corrupt." ); + goto unm_err_out; + } + index_end = (u8 *)&ir->index +le32_to_cpu (ir->index.index_length); +if (index_end > ir_end) { +ntfs_error (vi->i_sb,"Index is corrupt." ); + goto unm_err_out; + } + + ni->itype.index.collation_rule = ir->collation_rule; +ntfs_debug ("Index collation rule is 0x%x." , +le32_to_cpu (ir->collation_rule)); + ni->itype.index.block_size =le32_to_cpu (ir->index_block_size); +if (!is_power_of_2 (ni->itype.index.block_size)) { +ntfs_error (vi->i_sb,"Index block size (%u) is not a power of two." , + ni->itype.index.block_size); + goto unm_err_out; + } +if (ni->itype.index.block_size >PAGE_SIZE ) { +ntfs_error (vi->i_sb,"Index block size (%u) > PAGE_SIZE (%ld) is not supported." , + ni->itype.index.block_size,PAGE_SIZE ); + err = -EOPNOTSUPP ; + goto unm_err_out; + } +if (ni->itype.index.block_size <NTFS_BLOCK_SIZE ) { +ntfs_error (vi->i_sb, +"Index block size (%u) < NTFS_BLOCK_SIZE (%i) is not supported." , + ni->itype.index.block_size,NTFS_BLOCK_SIZE ); + err = -EOPNOTSUPP ; + goto unm_err_out; + } + ni->itype.index.block_size_bits =ffs (ni->itype.index.block_size) -1 ; +/* Determine the size of a vcn in the index. */ +if (vol->cluster_size <= ni->itype.index.block_size) { + ni->itype.index.vcn_size = vol->cluster_size; + ni->itype.index.vcn_size_bits = vol->cluster_size_bits; + }else { + ni->itype.index.vcn_size = vol->sector_size; + ni->itype.index.vcn_size_bits = vol->sector_size_bits; + } + +/* Find index allocation attribute. */ +ntfs_attr_reinit_search_ctx (ctx); + err =ntfs_attr_lookup (AT_INDEX_ALLOCATION , ni->name, ni->name_len, +CASE_SENSITIVE ,0 ,NULL ,0 , ctx); +if (unlikely (err)) { +if (err == -ENOENT ) { +/* No index allocation. */ + vi->i_size = ni->initialized_size = ni->allocated_size =0 ; +/* We are done with the mft record, so we release it. */ +ntfs_attr_put_search_ctx (ctx); +unmap_mft_record (base_ni); + m =NULL ; + ctx =NULL ; + goto skip_large_index_stuff; + }else +ntfs_error (vi->i_sb,"Failed to lookup $INDEX_ALLOCATION attribute." ); + goto unm_err_out; + } +NInoSetIndexAllocPresent (ni); +NInoSetNonResident (ni); + ni->type =AT_INDEX_ALLOCATION ; + + a = ctx->attr; +if (!a->non_resident) { +ntfs_error (vi->i_sb,"$INDEX_ALLOCATION attribute is resident." ); + goto unm_err_out; + } +/* +* Ensure the attribute name is placed before the mapping pairs array. +*/ +if (unlikely (a->name_length && (le16_to_cpu (a->name_offset) >= +le16_to_cpu (a->data.non_resident.mapping_pairs_offset)))) { +ntfs_error (vol->sb, +"$INDEX_ALLOCATION attribute name is placed after the mapping pairs array." ); + goto unm_err_out; + } +if (a->flags &ATTR_IS_ENCRYPTED ) { +ntfs_error (vi->i_sb,"$INDEX_ALLOCATION attribute is encrypted." ); + goto unm_err_out; + } +if (a->flags &ATTR_IS_SPARSE ) { +ntfs_error (vi->i_sb,"$INDEX_ALLOCATION attribute is sparse." ); + goto unm_err_out; + } +if (a->flags &ATTR_COMPRESSION_MASK ) { +ntfs_error (vi->i_sb, +"$INDEX_ALLOCATION attribute is compressed." ); + goto unm_err_out; + } +if (a->data.non_resident.lowest_vcn) { +ntfs_error (vi->i_sb, +"First extent of $INDEX_ALLOCATION attribute has non zero lowest_vcn." ); + goto unm_err_out; + } + vi->i_size = ni->data_size =le64_to_cpu (a->data.non_resident.data_size); + ni->initialized_size =le64_to_cpu (a->data.non_resident.initialized_size); + ni->allocated_size =le64_to_cpu (a->data.non_resident.allocated_size); +/* +* We are done with the mft record, so we release it. Otherwise +* we would deadlock in ntfs_attr_iget(). +*/ +ntfs_attr_put_search_ctx (ctx); +unmap_mft_record (base_ni); + m =NULL ; + ctx =NULL ; +/* Get the index bitmap attribute inode. */ + bvi =ntfs_attr_iget (base_vi,AT_BITMAP , ni->name, ni->name_len); +if (IS_ERR (bvi)) { +ntfs_error (vi->i_sb,"Failed to get bitmap attribute." ); + err =PTR_ERR (bvi); + goto unm_err_out; + } + bni =NTFS_I (bvi); +if (NInoCompressed (bni) ||NInoEncrypted (bni) || +NInoSparse (bni)) { +ntfs_error (vi->i_sb, +"$BITMAP attribute is compressed and/or encrypted and/or sparse." ); + goto iput_unm_err_out; + } +/* Consistency check bitmap size vs. index allocation size. */ + bvi_size =i_size_read (bvi); +if ((bvi_size <<3 ) < (vi->i_size >> ni->itype.index.block_size_bits)) { +ntfs_error (vi->i_sb, +"Index bitmap too small (0x%llx) for index allocation (0x%llx)." , + bvi_size <<3 , vi->i_size); + goto iput_unm_err_out; + } +iput (bvi); +skip_large_index_stuff: +/* Setup the operations for this index inode. */ +ntfs_set_vfs_operations (vi,S_IFDIR ,0 ); + vi->i_blocks = ni->allocated_size >>9 ; +/* +* Make sure the base inode doesn't go away and attach it to the +* index inode. +*/ +if (!igrab (base_vi)) + goto unm_err_out; + ni->ext.base_ntfs_ino = base_ni; + ni->nr_extents =-1 ; + +ntfs_debug ("Done." ); +return 0 ; +iput_unm_err_out: +iput (bvi); +unm_err_out: +if (!err) + err = -EIO ; +if (ctx) +ntfs_attr_put_search_ctx (ctx); +if (m) +unmap_mft_record (base_ni); +err_out: +ntfs_error (vi->i_sb, +"Failed with error code %i while reading index inode (mft_no 0x%lx, name_len %i." , + err, vi->i_ino, ni->name_len); +if (err != -EOPNOTSUPP && err != -ENOMEM ) +NVolSetErrors (vol); +return err; +} + +/** +* load_attribute_list_mount - load an attribute list into memory +* @vol: ntfs volume from which to read +* @runlist: runlist of the attribute list +* @al_start: destination buffer +* @size: size of the destination buffer in bytes +* @initialized_size: initialized size of the attribute list +* +* Walk the runlist @runlist and load all clusters from it copying them into +* the linear buffer @al. The maximum number of bytes copied to @al is @size +* bytes. Note, @size does not need to be a multiple of the cluster size. If +* @initialized_size is less than @size, the region in @al between +* @initialized_size and @size will be zeroed and not read from disk. +* +* Return 0 on success or -errno on error. +*/ +static int load_attribute_list_mount (struct ntfs_volume *vol, +struct runlist_element *rl,u8 *al_start,const s64 size, +const s64 initialized_size) +{ +s64 lcn; +u8 *al = al_start; +u8 *al_end = al + initialized_size; +struct super_block *sb; +int err =0 ; +loff_t rl_byte_off, rl_byte_len; + +ntfs_debug ("Entering." ); +if (!vol || !rl || !al || size <=0 || initialized_size <0 || + initialized_size > size) +return -EINVAL ; +if (!initialized_size) { +memset (al,0 , size); +return 0 ; + } + sb = vol->sb; + +/* Read all clusters specified by the runlist one run at a time. */ +while (rl->length) { + lcn =ntfs_rl_vcn_to_lcn (rl, rl->vcn); +ntfs_debug ("Reading vcn = 0x%llx, lcn = 0x%llx." , + (unsigned long long )rl->vcn, + (unsigned long long )lcn); +/* The attribute list cannot be sparse. */ +if (lcn <0 ) { +ntfs_error (sb,"ntfs_rl_vcn_to_lcn() failed. Cannot read attribute list." ); + goto err_out; + } + + rl_byte_off = lcn << vol->cluster_size_bits; + rl_byte_len = rl->length << vol->cluster_size_bits; + +if (al + rl_byte_len > al_end) + rl_byte_len = al_end - al; + + err =ntfs_dev_read (sb, al, rl_byte_off, rl_byte_len); +if (err) { +ntfs_error (sb,"Cannot read attribute list." ); + goto err_out; + } + +if (al + rl_byte_len >= al_end) { +if (initialized_size < size) + goto initialize; + goto done; + } + + al += rl_byte_len; + rl++; + } +if (initialized_size < size) { +initialize: +memset (al_start + initialized_size,0 , size - initialized_size); + } +done: +return err; +/* Real overflow! */ +ntfs_error (sb,"Attribute list buffer overflow. Read attribute list is truncated." ); +err_out: + err = -EIO ; + goto done; +} +/* +* The MFT inode has special locking, so teach the lock validator +* about this by splitting off the locking rules of the MFT from +* the locking rules of other inodes. The MFT inode can never be +* accessed from the VFS side (or even internally), only by the +* map_mft functions. +*/ +static struct lock_class_key mft_ni_runlist_lock_key, mft_ni_mrec_lock_key; + +/** +* ntfs_read_inode_mount - special read_inode for mount time use only +* @vi: inode to read +* +* Read inode FILE_MFT at mount time, only called with super_block lock +* held from within the read_super() code path. +* +* This function exists because when it is called the page cache for $MFT/$DATA +* is not initialized and hence we cannot get at the contents of mft records +* by calling map_mft_record*(). +* +* Further it needs to cope with the circular references problem, i.e. cannot +* load any attributes other than $ATTRIBUTE_LIST until $DATA is loaded, because +* we do not know where the other extent mft records are yet and again, because +* we cannot call map_mft_record*() yet. Obviously this applies only when an +* attribute list is actually present in $MFT inode. +* +* We solve these problems by starting with the $DATA attribute before anything +* else and iterating using ntfs_attr_lookup($DATA) over all extents. As each +* extent is found, we ntfs_mapping_pairs_decompress() including the implied +* ntfs_runlists_merge(). Each step of the iteration necessarily provides +* sufficient information for the next step to complete. +* +* This should work but there are two possible pit falls (see inline comments +* below), but only time will tell if they are real pits or just smoke... +*/ +int ntfs_read_inode_mount (struct inode *vi) +{ +s64 next_vcn, last_vcn, highest_vcn; +struct super_block *sb = vi->i_sb; +struct ntfs_volume *vol =NTFS_SB (sb); +struct ntfs_inode *ni; +struct mft_record *m =NULL ; +struct attr_record *a; +struct ntfs_attr_search_ctx *ctx; +unsigned int i, nr_blocks; +int err; +size_t new_rl_count; + +ntfs_debug ("Entering." ); + +/* Initialize the ntfs specific part of @vi. */ +ntfs_init_big_inode (vi); + + ni =NTFS_I (vi); + +/* Setup the data attribute. It is special as it is mst protected. */ +NInoSetNonResident (ni); +NInoSetMstProtected (ni); +NInoSetSparseDisabled (ni); + ni->type =AT_DATA ; + ni->name =AT_UNNAMED ; + ni->name_len =0 ; +/* +* This sets up our little cheat allowing us to reuse the async read io +* completion handler for directories. +*/ + ni->itype.index.block_size = vol->mft_record_size; + ni->itype.index.block_size_bits = vol->mft_record_size_bits; + +/* Very important! Needed to be able to call map_mft_record*(). */ + vol->mft_ino = vi; + +/* Allocate enough memory to read the first mft record. */ +if (vol->mft_record_size >64 *1024 ) { +ntfs_error (sb,"Unsupported mft record size %i (max 64kiB)." , + vol->mft_record_size); + goto err_out; + } + + i = vol->mft_record_size; +if (i < sb->s_blocksize) + i = sb->s_blocksize; + + m = (struct mft_record *)ntfs_malloc_nofs (i); +if (!m) { +ntfs_error (sb,"Failed to allocate buffer for $MFT record 0." ); + goto err_out; + } + +/* Determine the first block of the $MFT/$DATA attribute. */ + nr_blocks = vol->mft_record_size >> sb->s_blocksize_bits; +if (!nr_blocks) + nr_blocks =1 ; + +/* Load $MFT/$DATA's first mft record. */ + err =ntfs_dev_read (sb, m, vol->mft_lcn << vol->cluster_size_bits, i); +if (err) { +ntfs_error (sb,"Device read failed." ); + goto err_out; + } + +if (le32_to_cpu (m->bytes_allocated) != vol->mft_record_size) { +ntfs_error (sb,"Incorrect mft record size %u in superblock, should be %u." , +le32_to_cpu (m->bytes_allocated), vol->mft_record_size); + goto err_out; + } + +/* Apply the mst fixups. */ +if (post_read_mst_fixup ((struct ntfs_record *)m, vol->mft_record_size)) { +ntfs_error (sb,"MST fixup failed. $MFT is corrupt." ); + goto err_out; + } + +if (ntfs_mft_record_check (vol, m,FILE_MFT )) { +ntfs_error (sb,"ntfs_mft_record_check failed. $MFT is corrupt." ); + goto err_out; + } + +/* Need this to sanity check attribute list references to $MFT. */ + vi->i_generation = ni->seq_no =le16_to_cpu (m->sequence_number); + +/* Provides read_folio() for map_mft_record(). */ + vi->i_mapping->a_ops = &ntfs_mst_aops; + + ctx =ntfs_attr_get_search_ctx (ni, m); +if (!ctx) { + err = -ENOMEM ; + goto err_out; + } + +/* Find the attribute list attribute if present. */ + err =ntfs_attr_lookup (AT_ATTRIBUTE_LIST ,NULL ,0 ,0 ,0 ,NULL ,0 , ctx); +if (err) { +if (unlikely (err != -ENOENT )) { +ntfs_error (sb, +"Failed to lookup attribute list attribute. You should run chkdsk." ); + goto put_err_out; + } + }else /* if (!err) */ { +struct attr_list_entry *al_entry, *next_al_entry; +u8 *al_end; +static const char *es =" Not allowed. $MFT is corrupt. You should run chkdsk." ; + +ntfs_debug ("Attribute list attribute found in $MFT." ); +NInoSetAttrList (ni); + a = ctx->attr; +if (a->flags &ATTR_COMPRESSION_MASK ) { +ntfs_error (sb, +"Attribute list attribute is compressed.%s" , + es); + goto put_err_out; + } +if (a->flags &ATTR_IS_ENCRYPTED || + a->flags &ATTR_IS_SPARSE ) { +if (a->non_resident) { +ntfs_error (sb, +"Non-resident attribute list attribute is encrypted/sparse.%s" , + es); + goto put_err_out; + } +ntfs_warning (sb, +"Resident attribute list attribute in $MFT system file is marked encrypted/sparse which is not true. However, Windows allows this and chkdsk does not detect or correct it so we will just ignore the invalid flags and pretend they are not set." ); + } +/* Now allocate memory for the attribute list. */ + ni->attr_list_size = (u32 )ntfs_attr_size (a); +if (!ni->attr_list_size) { +ntfs_error (sb,"Attr_list_size is zero" ); + goto put_err_out; + } + ni->attr_list =ntfs_malloc_nofs (ni->attr_list_size); +if (!ni->attr_list) { +ntfs_error (sb,"Not enough memory to allocate buffer for attribute list." ); + goto put_err_out; + } +if (a->non_resident) { +struct runlist_element *rl; +size_t new_rl_count; + +NInoSetAttrListNonResident (ni); +if (a->data.non_resident.lowest_vcn) { +ntfs_error (sb, +"Attribute list has non zero lowest_vcn. $MFT is corrupt. You should run chkdsk." ); + goto put_err_out; + } + + rl =ntfs_mapping_pairs_decompress (vol, a,NULL , &new_rl_count); +if (IS_ERR (rl)) { + err =PTR_ERR (rl); +ntfs_error (sb, +"Mapping pairs decompression failed with error code %i." , + -err); + goto put_err_out; + } + + err =load_attribute_list_mount (vol, rl, ni->attr_list, ni->attr_list_size, +le64_to_cpu (a->data.non_resident.initialized_size)); +ntfs_free (rl); +if (err) { +ntfs_error (sb, +"Failed to load attribute list with error code %i." , + -err); + goto put_err_out; + } + }else /* if (!ctx.attr->non_resident) */ { +if ((u8 *)a +le16_to_cpu ( + a->data.resident.value_offset) + +le32_to_cpu (a->data.resident.value_length) > + (u8 *)ctx->mrec + vol->mft_record_size) { +ntfs_error (sb,"Corrupt attribute list attribute." ); + goto put_err_out; + } +/* Now copy the attribute list. */ +memcpy (ni->attr_list, (u8 *)a +le16_to_cpu ( + a->data.resident.value_offset), +le32_to_cpu (a->data.resident.value_length)); + } +/* The attribute list is now setup in memory. */ + al_entry = (struct attr_list_entry *)ni->attr_list; + al_end = (u8 *)al_entry + ni->attr_list_size; +for (;; al_entry = next_al_entry) { +/* Out of bounds check. */ +if ((u8 *)al_entry < ni->attr_list || + (u8 *)al_entry > al_end) + goto em_put_err_out; +/* Catch the end of the attribute list. */ +if ((u8 *)al_entry == al_end) + goto em_put_err_out; +if (!al_entry->length) + goto em_put_err_out; +if ((u8 *)al_entry +6 > al_end || + (u8 *)al_entry +le16_to_cpu (al_entry->length) > al_end) + goto em_put_err_out; + next_al_entry = (struct attr_list_entry *)((u8 *)al_entry + +le16_to_cpu (al_entry->length)); +if (le32_to_cpu (al_entry->type) >le32_to_cpu (AT_DATA )) + goto em_put_err_out; +if (al_entry->type !=AT_DATA ) +continue ; +/* We want an unnamed attribute. */ +if (al_entry->name_length) + goto em_put_err_out; +/* Want the first entry, i.e. lowest_vcn == 0. */ +if (al_entry->lowest_vcn) + goto em_put_err_out; +/* First entry has to be in the base mft record. */ +if (MREF_LE (al_entry->mft_reference) != vi->i_ino) { +/* MFT references do not match, logic fails. */ +ntfs_error (sb, +"BUG: The first $DATA extent of $MFT is not in the base mft record." ); + goto put_err_out; + }else { +/* Sequence numbers must match. */ +if (MSEQNO_LE (al_entry->mft_reference) != + ni->seq_no) + goto em_put_err_out; +/* Got it. All is ok. We can stop now. */ +break ; + } + } + } + +ntfs_attr_reinit_search_ctx (ctx); + +/* Now load all attribute extents. */ + a =NULL ; + next_vcn = last_vcn = highest_vcn =0 ; +while (!(err =ntfs_attr_lookup (AT_DATA ,NULL ,0 ,0 , next_vcn,NULL ,0 , + ctx))) { +struct runlist_element *nrl; + +/* Cache the current attribute. */ + a = ctx->attr; +/* $MFT must be non-resident. */ +if (!a->non_resident) { +ntfs_error (sb, +"$MFT must be non-resident but a resident extent was found. $MFT is corrupt. Run chkdsk." ); + goto put_err_out; + } +/* $MFT must be uncompressed and unencrypted. */ +if (a->flags &ATTR_COMPRESSION_MASK || + a->flags &ATTR_IS_ENCRYPTED || + a->flags &ATTR_IS_SPARSE ) { +ntfs_error (sb, +"$MFT must be uncompressed, non-sparse, and unencrypted but a compressed/sparse/encrypted extent was found. $MFT is corrupt. Run chkdsk." ); + goto put_err_out; + } +/* +* Decompress the mapping pairs array of this extent and merge +* the result into the existing runlist. No need for locking +* as we have exclusive access to the inode at this time and we +* are a mount in progress task, too. +*/ + nrl =ntfs_mapping_pairs_decompress (vol, a, &ni->runlist, + &new_rl_count); +if (IS_ERR (nrl)) { +ntfs_error (sb, +"ntfs_mapping_pairs_decompress() failed with error code %ld." , +PTR_ERR (nrl)); + goto put_err_out; + } + ni->runlist.rl = nrl; + ni->runlist.count = new_rl_count; + +/* Are we in the first extent? */ +if (!next_vcn) { +if (a->data.non_resident.lowest_vcn) { +ntfs_error (sb, +"First extent of $DATA attribute has non zero lowest_vcn. $MFT is corrupt. You should run chkdsk." ); + goto put_err_out; + } +/* Get the last vcn in the $DATA attribute. */ + last_vcn =le64_to_cpu (a->data.non_resident.allocated_size) >> + vol->cluster_size_bits; +/* Fill in the inode size. */ + vi->i_size =le64_to_cpu (a->data.non_resident.data_size); + ni->initialized_size =le64_to_cpu (a->data.non_resident.initialized_size); + ni->allocated_size =le64_to_cpu (a->data.non_resident.allocated_size); +/* +* Verify the number of mft records does not exceed +* 2^32 - 1. +*/ +if ((vi->i_size >> vol->mft_record_size_bits) >= + (1ULL <<32 )) { +ntfs_error (sb,"$MFT is too big! Aborting." ); + goto put_err_out; + } +/* +* We have got the first extent of the runlist for +* $MFT which means it is now relatively safe to call +* the normal ntfs_read_inode() function. +* Complete reading the inode, this will actually +* re-read the mft record for $MFT, this time entering +* it into the page cache with which we complete the +* kick start of the volume. It should be safe to do +* this now as the first extent of $MFT/$DATA is +* already known and we would hope that we don't need +* further extents in order to find the other +* attributes belonging to $MFT. Only time will tell if +* this is really the case. If not we will have to play +* magic at this point, possibly duplicating a lot of +* ntfs_read_inode() at this point. We will need to +* ensure we do enough of its work to be able to call +* ntfs_read_inode() on extents of $MFT/$DATA. But lets +* hope this never happens... +*/ + err =ntfs_read_locked_inode (vi); +if (err) { +ntfs_error (sb,"ntfs_read_inode() of $MFT failed.\n" ); +ntfs_attr_put_search_ctx (ctx); +/* Revert to the safe super operations. */ +ntfs_free (m); +return -1 ; + } +/* +* Re-initialize some specifics about $MFT's inode as +* ntfs_read_inode() will have set up the default ones. +*/ +/* Set uid and gid to root. */ + vi->i_uid =GLOBAL_ROOT_UID ; + vi->i_gid =GLOBAL_ROOT_GID ; +/* Regular file. No access for anyone. */ + vi->i_mode =S_IFREG ; +/* No VFS initiated operations allowed for $MFT. */ + vi->i_op = &ntfs_empty_inode_ops; + vi->i_fop = &ntfs_empty_file_ops; + } + +/* Get the lowest vcn for the next extent. */ + highest_vcn =le64_to_cpu (a->data.non_resident.highest_vcn); + next_vcn = highest_vcn +1 ; + +/* Only one extent or error, which we catch below. */ +if (next_vcn <=0 ) +break ; + +/* Avoid endless loops due to corruption. */ +if (next_vcn <le64_to_cpu (a->data.non_resident.lowest_vcn)) { +ntfs_error (sb,"$MFT has corrupt attribute list attribute. Run chkdsk." ); + goto put_err_out; + } + } +if (err != -ENOENT ) { +ntfs_error (sb,"Failed to lookup $MFT/$DATA attribute extent. Run chkdsk.\n" ); + goto put_err_out; + } +if (!a) { +ntfs_error (sb,"$MFT/$DATA attribute not found. $MFT is corrupt. Run chkdsk." ); + goto put_err_out; + } +if (highest_vcn && highest_vcn != last_vcn -1 ) { +ntfs_error (sb,"Failed to load the complete runlist for $MFT/$DATA. Run chkdsk." ); +ntfs_debug ("highest_vcn = 0x%llx, last_vcn - 1 = 0x%llx" , + (unsigned long long )highest_vcn, + (unsigned long long )last_vcn -1 ); + goto put_err_out; + } +ntfs_attr_put_search_ctx (ctx); +ntfs_debug ("Done." ); +ntfs_free (m); + +/* +* Split the locking rules of the MFT inode from the +* locking rules of other inodes: +*/ +lockdep_set_class (&ni->runlist.lock, &mft_ni_runlist_lock_key); +lockdep_set_class (&ni->mrec_lock, &mft_ni_mrec_lock_key); + +return 0 ; + +em_put_err_out: +ntfs_error (sb, +"Couldn't find first extent of $DATA attribute in attribute list. $MFT is corrupt. Run chkdsk." ); +put_err_out: +ntfs_attr_put_search_ctx (ctx); +err_out: +ntfs_error (sb,"Failed. Marking inode as bad." ); +ntfs_free (m); +return -1 ; +} + +static void __ntfs_clear_inode (struct ntfs_inode *ni) +{ +/* Free all alocated memory. */ +if (NInoNonResident (ni) && ni->runlist.rl) { +ntfs_free (ni->runlist.rl); + ni->runlist.rl =NULL ; + } + +if (ni->attr_list) { +ntfs_free (ni->attr_list); + ni->attr_list =NULL ; + } + +if (ni->name_len && ni->name !=I30 && + ni->name != reparse_index_name && + ni->name !=R ) { +WARN_ON (!ni->name); +kfree (ni->name); + } +} + +void ntfs_clear_extent_inode (struct ntfs_inode *ni) +{ +ntfs_debug ("Entering for inode 0x%lx." , ni->mft_no); + +WARN_ON (NInoAttr (ni)); +WARN_ON (ni->nr_extents !=-1 ); + +__ntfs_clear_inode (ni); +ntfs_destroy_extent_inode (ni); +} + +static int ntfs_delete_base_inode (struct ntfs_inode *ni) +{ +struct super_block *sb = ni->vol->sb; +int err; + +if (NInoAttr (ni) || ni->nr_extents ==-1 ) +return 0 ; + + err =ntfs_non_resident_dealloc_clusters (ni); + +/* +* Deallocate extent mft records and free extent inodes. +* No need to lock as no one else has a reference. +*/ +while (ni->nr_extents) { + err =ntfs_mft_record_free (ni->vol, *(ni->ext.extent_ntfs_inos)); +if (err) +ntfs_error (sb, +"Failed to free extent MFT record. Leaving inconsistent metadata.\n" ); +ntfs_inode_close (*(ni->ext.extent_ntfs_inos)); + } + +/* Deallocate base mft record */ + err =ntfs_mft_record_free (ni->vol, ni); +if (err) +ntfs_error (sb,"Failed to free base MFT record. Leaving inconsistent metadata.\n" ); +return err; +} + +/** +* ntfs_evict_big_inode - clean up the ntfs specific part of an inode +* @vi: vfs inode pending annihilation +* +* When the VFS is going to remove an inode from memory, ntfs_clear_big_inode() +* is called, which deallocates all memory belonging to the NTFS specific part +* of the inode and returns. +* +* If the MFT record is dirty, we commit it before doing anything else. +*/ +void ntfs_evict_big_inode (struct inode *vi) +{ +struct ntfs_inode *ni =NTFS_I (vi); + +truncate_inode_pages_final (&vi->i_data); + +if (!vi->i_nlink) { +if (!NInoAttr (ni)) { +/* Never called with extent inodes */ +WARN_ON (ni->nr_extents ==-1 ); +ntfs_delete_base_inode (ni); + } + goto release; + } + +if (NInoDirty (ni)) { +/* Committing the inode also commits all extent inodes. */ +ntfs_commit_inode (vi); + +if (NInoDirty (ni)) { +ntfs_debug ("Failed to commit dirty inode 0x%lx. Losing data!" , + vi->i_ino); +NInoClearAttrListDirty (ni); +NInoClearDirty (ni); + } + } + +/* No need to lock at this stage as no one else has a reference. */ +if (ni->nr_extents >0 ) { +int i; + +for (i =0 ; i < ni->nr_extents; i++) { +if (ni->ext.extent_ntfs_inos[i]) +ntfs_clear_extent_inode (ni->ext.extent_ntfs_inos[i]); + } + ni->nr_extents =0 ; +ntfs_free (ni->ext.extent_ntfs_inos); + } + +release: +clear_inode (vi); +__ntfs_clear_inode (ni); + +if (NInoAttr (ni)) { +/* Release the base inode if we are holding it. */ +if (ni->nr_extents ==-1 ) { +iput (VFS_I (ni->ext.base_ntfs_ino)); + ni->nr_extents =0 ; + ni->ext.base_ntfs_ino =NULL ; + } + } + +if (!atomic_dec_and_test (&ni->count)) +WARN_ON (1 ); +if (ni->folio) +ntfs_unmap_folio (ni->folio,NULL ); +kfree (ni->mrec); +ntfs_free (ni->target); +} + +/** +* ntfs_show_options - show mount options in /proc/mounts +* @sf: seq_file in which to write our mount options +* @root: root of the mounted tree whose mount options to display +* +* Called by the VFS once for each mounted ntfs volume when someone reads +* /proc/mounts in order to display the NTFS specific mount options of each +* mount. The mount options of fs specified by @root are written to the seq file +* @sf and success is returned. +*/ +int ntfs_show_options (struct seq_file *sf,struct dentry *root) +{ +struct ntfs_volume *vol =NTFS_SB (root->d_sb); +int i; + +if (uid_valid (vol->uid)) +seq_printf (sf,",uid=%i" ,from_kuid_munged (&init_user_ns, vol->uid)); +if (gid_valid (vol->gid)) +seq_printf (sf,",gid=%i" ,from_kgid_munged (&init_user_ns, vol->gid)); +if (vol->fmask == vol->dmask) +seq_printf (sf,",umask=0%o" , vol->fmask); +else { +seq_printf (sf,",fmask=0%o" , vol->fmask); +seq_printf (sf,",dmask=0%o" , vol->dmask); + } +seq_printf (sf,",iocharset=%s" , vol->nls_map->charset); +if (NVolCaseSensitive (vol)) +seq_puts (sf,",case_sensitive" ); +else +seq_puts (sf,",nocase" ); +if (NVolShowSystemFiles (vol)) +seq_puts (sf,",show_sys_files,showmeta" ); +for (i =0 ; on_errors_arr[i].val; i++) { +if (on_errors_arr[i].val == vol->on_errors) +seq_printf (sf,",errors=%s" , on_errors_arr[i].str); + } +seq_printf (sf,",mft_zone_multiplier=%i" , vol->mft_zone_multiplier); +if (NVolSysImmutable (vol)) +seq_puts (sf,",sys_immutable" ); +if (!NVolShowHiddenFiles (vol)) +seq_puts (sf,",nohidden" ); +if (NVolHideDotFiles (vol)) +seq_puts (sf,",hide_dot_files" ); +if (NVolCheckWindowsNames (vol)) +seq_puts (sf,",windows_names" ); +if (NVolDiscard (vol)) +seq_puts (sf,",discard" ); +if (NVolDisableSparse (vol)) +seq_puts (sf,",disable_sparse" ); +if (vol->sb->s_flags &SB_POSIXACL ) +seq_puts (sf,",acl" ); +return 0 ; +} + +int ntfs_extend_initialized_size (struct inode *vi,const loff_t offset, +const loff_t new_size) +{ +struct ntfs_inode *ni =NTFS_I (vi); +loff_t old_init_size; +unsigned long flags; +int err; + +read_lock_irqsave (&ni->size_lock, flags); + old_init_size = ni->initialized_size; +read_unlock_irqrestore (&ni->size_lock, flags); + +if (!NInoNonResident (ni)) +return -EINVAL ; +if (old_init_size >= new_size) +return 0 ; + + err =ntfs_attr_map_whole_runlist (ni); +if (err) +return err; + +if (!NInoCompressed (ni) && old_init_size < offset) { + err =iomap_zero_range (vi, old_init_size, + offset - old_init_size, +NULL , &ntfs_read_iomap_ops, + &ntfs_iomap_folio_ops,NULL ); +if (err) +return err; + } + + +mutex_lock (&ni->mrec_lock); + err =ntfs_attr_set_initialized_size (ni, new_size); +mutex_unlock (&ni->mrec_lock); +if (err) +truncate_setsize (vi, old_init_size); +return err; +} + +int ntfs_truncate_vfs (struct inode *vi,loff_t new_size,loff_t i_size) +{ +struct ntfs_inode *ni =NTFS_I (vi); +int err; + +mutex_lock (&ni->mrec_lock); + err =__ntfs_attr_truncate_vfs (ni, new_size, i_size); +mutex_unlock (&ni->mrec_lock); +if (err <0 ) +return err; + +inode_set_mtime_to_ts (vi,inode_set_ctime_current (vi)); +return 0 ; +} + +/** +* ntfs_inode_sync_standard_information - update standard information attribute +* @vi: inode to update standard information +* @m: mft record +* +* Return 0 on success or -errno on error. +*/ +static int ntfs_inode_sync_standard_information (struct inode *vi,struct mft_record *m) +{ +struct ntfs_inode *ni =NTFS_I (vi); +struct ntfs_attr_search_ctx *ctx; +struct standard_information *si; +__le64 nt; +int err =0 ; +bool modified = false; + +/* Update the access times in the standard information attribute. */ + ctx =ntfs_attr_get_search_ctx (ni, m); +if (unlikely (!ctx)) +return -ENOMEM ; + err =ntfs_attr_lookup (AT_STANDARD_INFORMATION ,NULL ,0 , +CASE_SENSITIVE ,0 ,NULL ,0 , ctx); +if (unlikely (err)) { +ntfs_attr_put_search_ctx (ctx); +return err; + } + si = (struct standard_information *)((u8 *)ctx->attr + +le16_to_cpu (ctx->attr->data.resident.value_offset)); +if (si->file_attributes != ni->flags) { + si->file_attributes = ni->flags; + modified = true; + } + +/* Update the creation times if they have changed. */ + nt =utc2ntfs (ni->i_crtime); +if (si->creation_time != nt) { +ntfs_debug ("Updating creation time for inode 0x%lx: old = 0x%llx, new = 0x%llx" , + vi->i_ino,le64_to_cpu (si->creation_time), +le64_to_cpu (nt)); + si->creation_time = nt; + modified = true; + } + +/* Update the access times if they have changed. */ + nt =utc2ntfs (inode_get_mtime (vi)); +if (si->last_data_change_time != nt) { +ntfs_debug ("Updating mtime for inode 0x%lx: old = 0x%llx, new = 0x%llx" , + vi->i_ino,le64_to_cpu (si->last_data_change_time), +le64_to_cpu (nt)); + si->last_data_change_time = nt; + modified = true; + } + + nt =utc2ntfs (inode_get_ctime (vi)); +if (si->last_mft_change_time != nt) { +ntfs_debug ("Updating ctime for inode 0x%lx: old = 0x%llx, new = 0x%llx" , + vi->i_ino,le64_to_cpu (si->last_mft_change_time), +le64_to_cpu (nt)); + si->last_mft_change_time = nt; + modified = true; + } + nt =utc2ntfs (inode_get_atime (vi)); +if (si->last_access_time != nt) { +ntfs_debug ("Updating atime for inode 0x%lx: old = 0x%llx, new = 0x%llx" , + vi->i_ino, +le64_to_cpu (si->last_access_time), +le64_to_cpu (nt)); + si->last_access_time = nt; + modified = true; + } + +/* +* If we just modified the standard information attribute we need to +* mark the mft record it is in dirty. We do this manually so that +* mark_inode_dirty() is not called which would redirty the inode and +* hence result in an infinite loop of trying to write the inode. +* There is no need to mark the base inode nor the base mft record +* dirty, since we are going to write this mft record below in any case +* and the base mft record may actually not have been modified so it +* might not need to be written out. +* NOTE: It is not a problem when the inode for $MFT itself is being +* written out as mark_ntfs_record_dirty() will only set I_DIRTY_PAGES +* on the $MFT inode and hence ntfs_write_inode() will not be +* re-invoked because of it which in turn is ok since the dirtied mft +* record will be cleaned and written out to disk below, i.e. before +* this function returns. +*/ +if (modified) +NInoSetDirty (ctx->ntfs_ino); +ntfs_attr_put_search_ctx (ctx); + +return err; +} + +/** +* ntfs_inode_sync_filename - update FILE_NAME attributes +* @ni: ntfs inode to update FILE_NAME attributes +* +* Update all FILE_NAME attributes for inode @ni in the index. +* +* Return 0 on success or error. +*/ +int ntfs_inode_sync_filename (struct ntfs_inode *ni) +{ +struct inode *index_vi; +struct super_block *sb =VFS_I (ni)->i_sb; +struct ntfs_attr_search_ctx *ctx =NULL ; +struct ntfs_index_context *ictx; +struct ntfs_inode *index_ni; +struct file_name_attr *fn; +struct file_name_attr *fnx; +struct reparse_point *rpp; +__le32 reparse_tag; +int err =0 ; +unsigned long flags; + +ntfs_debug ("Entering for inode %lld\n" , (long long )ni->mft_no); + + ctx =ntfs_attr_get_search_ctx (ni,NULL ); +if (!ctx) +return -ENOMEM ; + +/* Collect the reparse tag, if any */ + reparse_tag =cpu_to_le32 (0 ); +if (ni->flags &FILE_ATTR_REPARSE_POINT ) { +if (!ntfs_attr_lookup (AT_REPARSE_POINT ,NULL , +0 ,CASE_SENSITIVE ,0 ,NULL ,0 , ctx)) { + rpp = (struct reparse_point *)((u8 *)ctx->attr + +le16_to_cpu (ctx->attr->data.resident.value_offset)); + reparse_tag = rpp->reparse_tag; + } +ntfs_attr_reinit_search_ctx (ctx); + } + +/* Walk through all FILE_NAME attributes and update them. */ +while (!(err =ntfs_attr_lookup (AT_FILE_NAME ,NULL ,0 ,0 ,0 ,NULL ,0 , ctx))) { + fn = (struct file_name_attr *)((u8 *)ctx->attr + +le16_to_cpu (ctx->attr->data.resident.value_offset)); +if (MREF_LE (fn->parent_directory) == ni->mft_no) +continue ; + + index_vi =ntfs_iget (sb,MREF_LE (fn->parent_directory)); +if (IS_ERR (index_vi)) { +ntfs_error (sb,"Failed to open inode %lld with index" , + (long long )MREF_LE (fn->parent_directory)); +continue ; + } + + index_ni =NTFS_I (index_vi); + +mutex_lock_nested (&index_ni->mrec_lock,NTFS_INODE_MUTEX_PARENT ); +if (NInoBeingDeleted (ni)) { +iput (index_vi); +mutex_unlock (&index_ni->mrec_lock); +continue ; + } + + ictx =ntfs_index_ctx_get (index_ni,I30 ,4 ); +if (!ictx) { +ntfs_error (sb,"Failed to get index ctx, inode %lld" , + (long long )index_ni->mft_no); +iput (index_vi); +mutex_unlock (&index_ni->mrec_lock); +continue ; + } + + err =ntfs_index_lookup (fn,sizeof (struct file_name_attr ), ictx); +if (err) { +ntfs_debug ("Index lookup failed, inode %lld" , + (long long )index_ni->mft_no); +ntfs_index_ctx_put (ictx); +iput (index_vi); +mutex_unlock (&index_ni->mrec_lock); +continue ; + } +/* Update flags and file size. */ + fnx = (struct file_name_attr *)ictx->data; + fnx->file_attributes = + (fnx->file_attributes & ~FILE_ATTR_VALID_FLAGS ) | + (ni->flags &FILE_ATTR_VALID_FLAGS ); +if (ctx->mrec->flags &MFT_RECORD_IS_DIRECTORY ) + fnx->data_size = fnx->allocated_size =0 ; +else { +read_lock_irqsave (&ni->size_lock, flags); +if (NInoSparse (ni) ||NInoCompressed (ni)) + fnx->allocated_size =cpu_to_le64 (ni->itype.compressed.size); +else + fnx->allocated_size =cpu_to_le64 (ni->allocated_size); + fnx->data_size =cpu_to_le64 (ni->data_size); + +/* +* The file name record has also to be fixed if some +* attribute update implied the unnamed data to be +* made non-resident +*/ + fn->allocated_size = fnx->allocated_size; + fn->data_size = fnx->data_size; +read_unlock_irqrestore (&ni->size_lock, flags); + } + +/* update or clear the reparse tag in the index */ + fnx->type.rp.reparse_point_tag = reparse_tag; + fnx->creation_time = fn->creation_time; + fnx->last_data_change_time = fn->last_data_change_time; + fnx->last_mft_change_time = fn->last_mft_change_time; + fnx->last_access_time = fn->last_access_time; +ntfs_index_entry_mark_dirty (ictx); +ntfs_icx_ib_sync_write (ictx); +NInoSetDirty (ctx->ntfs_ino); +ntfs_index_ctx_put (ictx); +mutex_unlock (&index_ni->mrec_lock); +iput (index_vi); + } +/* Check for real error occurred. */ +if (err != -ENOENT ) { +ntfs_error (sb,"Attribute lookup failed, err : %d, inode %lld" , err, + (long long )ni->mft_no); + }else + err =0 ; + +ntfs_attr_put_search_ctx (ctx); +return err; +} + +/** +* __ntfs_write_inode - write out a dirty inode +* @vi: inode to write out +* @sync: if true, write out synchronously +* +* Write out a dirty inode to disk including any extent inodes if present. +* +* If @sync is true, commit the inode to disk and wait for io completion. This +* is done using write_mft_record(). +* +* If @sync is false, just schedule the write to happen but do not wait for i/o +* completion. +* +* Return 0 on success and -errno on error. +*/ +int __ntfs_write_inode (struct inode *vi,int sync) +{ +struct ntfs_inode *ni =NTFS_I (vi); +struct mft_record *m; +int err =0 ; +bool need_iput = false; + +ntfs_debug ("Entering for %sinode 0x%lx." ,NInoAttr (ni) ?"attr " :"" , + vi->i_ino); + +if (NVolShutdown (ni->vol)) +return -EIO ; + +/* +* Dirty attribute inodes are written via their real inodes so just +* clean them here. Access time updates are taken care off when the +* real inode is written. +*/ +if (NInoAttr (ni) || ni->nr_extents ==-1 ) { +NInoClearDirty (ni); +ntfs_debug ("Done." ); +return 0 ; + } + +/* igrab prevents vi from being evicted while mrec_lock is hold. */ +if (igrab (vi) !=NULL ) + need_iput = true; + +mutex_lock_nested (&ni->mrec_lock,NTFS_INODE_MUTEX_NORMAL ); +/* Map, pin, and lock the mft record belonging to the inode. */ + m =map_mft_record (ni); +if (IS_ERR (m)) { +mutex_unlock (&ni->mrec_lock); + err =PTR_ERR (m); + goto err_out; + } + +if (NInoNonResident (ni) &&NInoRunlistDirty (ni)) { +down_write (&ni->runlist.lock); + err =ntfs_attr_update_mapping_pairs (ni,0 ); +if (!err) +NInoClearRunlistDirty (ni); +up_write (&ni->runlist.lock); + } + + err =ntfs_inode_sync_standard_information (vi, m); +if (err) + goto unm_err_out; + +/* +* when being umounted and inodes are evicted, write_inode() +* is called with all inodes being marked with I_FREEING. +* then ntfs_inode_sync_filename() waits infinitly because +* of ntfs_iget. This situation happens only where sync_filesysem() +* from umount fails because of a disk unplug and etc. +* the absent of SB_ACTIVE means umounting. +*/ +if ((vi->i_sb->s_flags &SB_ACTIVE ) &&NInoTestClearFileNameDirty (ni)) +ntfs_inode_sync_filename (ni); + +/* Now the access times are updated, write the base mft record. */ +if (NInoDirty (ni)) { + err =write_mft_record (ni, m, sync); +if (err) +ntfs_error (vi->i_sb,"write_mft_record failed, err : %d\n" , err); + } +unmap_mft_record (ni); + +/* Write all attached extent mft records. */ +mutex_lock (&ni->extent_lock); +if (ni->nr_extents >0 ) { +struct ntfs_inode **extent_nis = ni->ext.extent_ntfs_inos; +int i; + +ntfs_debug ("Writing %i extent inodes." , ni->nr_extents); +for (i =0 ; i < ni->nr_extents; i++) { +struct ntfs_inode *tni = extent_nis[i]; + +if (NInoDirty (tni)) { +struct mft_record *tm; +int ret; + +mutex_lock (&tni->mrec_lock); + tm =map_mft_record (tni); +if (IS_ERR (tm)) { +mutex_unlock (&tni->mrec_lock); +if (!err || err == -ENOMEM ) + err =PTR_ERR (tm); +continue ; + } + + ret =write_mft_record (tni, tm, sync); +unmap_mft_record (tni); +mutex_unlock (&tni->mrec_lock); + +if (unlikely (ret)) { +if (!err || err == -ENOMEM ) + err = ret; + } + } + } + } +mutex_unlock (&ni->extent_lock); +mutex_unlock (&ni->mrec_lock); + +if (unlikely (err)) + goto err_out; +if (need_iput) +iput (vi); +ntfs_debug ("Done." ); +return 0 ; +unm_err_out: +unmap_mft_record (ni); +mutex_unlock (&ni->mrec_lock); +err_out: +if (err == -ENOMEM ) +mark_inode_dirty (vi); +else { +ntfs_error (vi->i_sb,"Failed (error %i): Run chkdsk." , -err); +NVolSetErrors (ni->vol); + } +if (need_iput) +iput (vi); +return err; +} + +/** +* ntfs_extent_inode_open - load an extent inode and attach it to its base +* @base_ni: base ntfs inode +* @mref: mft reference of the extent inode to load (in little endian) +* +* First check if the extent inode @mref is already attached to the base ntfs +* inode @base_ni, and if so, return a pointer to the attached extent inode. +* +* If the extent inode is not already attached to the base inode, allocate an +* ntfs_inode structure and initialize it for the given inode @mref. @mref +* specifies the inode number / mft record to read, including the sequence +* number, which can be 0 if no sequence number checking is to be performed. +* +* Then, allocate a buffer for the mft record, read the mft record from the +* volume @base_ni->vol, and attach it to the ntfs_inode structure (->mrec). +* The mft record is mst deprotected and sanity checked for validity and we +* abort if deprotection or checks fail. +* +* Finally attach the ntfs inode to its base inode @base_ni and return a +* pointer to the ntfs_inode structure on success or NULL on error, with errno +* set to the error code. +* +* Note, extent inodes are never closed directly. They are automatically +* disposed off by the closing of the base inode. +*/ +static struct ntfs_inode *ntfs_extent_inode_open (struct ntfs_inode *base_ni, +const __le64 mref) +{ +u64 mft_no =MREF_LE (mref); +struct ntfs_inode *ni =NULL ; +struct ntfs_inode **extent_nis; +int i; +struct mft_record *ni_mrec; +struct super_block *sb; + +if (!base_ni) +return NULL ; + + sb = base_ni->vol->sb; +ntfs_debug ("Opening extent inode %lld (base mft record %lld).\n" , + (unsigned long long )mft_no, + (unsigned long long )base_ni->mft_no); + +/* Is the extent inode already open and attached to the base inode? */ +if (base_ni->nr_extents >0 ) { + extent_nis = base_ni->ext.extent_ntfs_inos; +for (i =0 ; i < base_ni->nr_extents; i++) { +u16 seq_no; + + ni = extent_nis[i]; +if (mft_no != ni->mft_no) +continue ; + ni_mrec =map_mft_record (ni); +if (IS_ERR (ni_mrec)) { +ntfs_error (sb,"failed to map mft record for %lu" , + ni->mft_no); + goto out; + } +/* Verify the sequence number if given. */ + seq_no =MSEQNO_LE (mref); +if (seq_no && + seq_no !=le16_to_cpu (ni_mrec->sequence_number)) { +ntfs_error (sb,"Found stale extent mft reference mft=%lld" , + (long long )ni->mft_no); +unmap_mft_record (ni); + goto out; + } +unmap_mft_record (ni); + goto out; + } + } +/* Wasn't there, we need to load the extent inode. */ + ni =ntfs_new_extent_inode (base_ni->vol->sb, mft_no); +if (!ni) + goto out; + + ni->seq_no = (u16 )MSEQNO_LE (mref); + ni->nr_extents =-1 ; + ni->ext.base_ntfs_ino = base_ni; +/* Attach extent inode to base inode, reallocating memory if needed. */ +if (!(base_ni->nr_extents &3 )) { + i = (base_ni->nr_extents +4 ) *sizeof (struct ntfs_inode *); + + extent_nis =ntfs_malloc_nofs (i); +if (!extent_nis) + goto err_out; +if (base_ni->nr_extents) { +memcpy (extent_nis, base_ni->ext.extent_ntfs_inos, + i -4 *sizeof (struct ntfs_inode *)); +ntfs_free (base_ni->ext.extent_ntfs_inos); + } + base_ni->ext.extent_ntfs_inos = extent_nis; + } + base_ni->ext.extent_ntfs_inos[base_ni->nr_extents++] = ni; + +out: +ntfs_debug ("\n" ); +return ni; +err_out: +ntfs_destroy_ext_inode (ni); + ni =NULL ; + goto out; +} + +/** +* ntfs_inode_attach_all_extents - attach all extents for target inode +* @ni: opened ntfs inode for which perform attach +* +* Return 0 on success and error. +*/ +int ntfs_inode_attach_all_extents (struct ntfs_inode *ni) +{ +struct attr_list_entry *ale; +u64 prev_attached =0 ; + +if (!ni) { +ntfs_debug ("Invalid arguments.\n" ); +return -EINVAL ; + } + +if (NInoAttr (ni)) + ni = ni->ext.base_ntfs_ino; + +ntfs_debug ("Entering for inode 0x%llx.\n" , (long long ) ni->mft_no); + +/* Inode haven't got attribute list, thus nothing to attach. */ +if (!NInoAttrList (ni)) +return 0 ; + +if (!ni->attr_list) { +ntfs_debug ("Corrupt in-memory struct.\n" ); +return -EINVAL ; + } + +/* Walk through attribute list and attach all extents. */ + ale = (struct attr_list_entry *)ni->attr_list; +while ((u8 *)ale < ni->attr_list + ni->attr_list_size) { +if (ni->mft_no !=MREF_LE (ale->mft_reference) && + prev_attached !=MREF_LE (ale->mft_reference)) { +if (!ntfs_extent_inode_open (ni, ale->mft_reference)) { +ntfs_debug ("Couldn't attach extent inode.\n" ); +return -1 ; + } + prev_attached =MREF_LE (ale->mft_reference); + } + ale = (struct attr_list_entry *)((u8 *)ale +le16_to_cpu (ale->length)); + } +return 0 ; +} + +/** +* ntfs_inode_add_attrlist - add attribute list to inode and fill it +* @ni: opened ntfs inode to which add attribute list +* +* Return 0 on success or error. +*/ +int ntfs_inode_add_attrlist (struct ntfs_inode *ni) +{ +int err; +struct ntfs_attr_search_ctx *ctx; +u8 *al =NULL , *aln; +int al_len =0 ; +struct attr_list_entry *ale =NULL ; +struct mft_record *ni_mrec; +u32 attr_al_len; + +if (!ni) +return -EINVAL ; + +ntfs_debug ("inode %llu\n" , (unsigned long long ) ni->mft_no); + +if (NInoAttrList (ni) || ni->nr_extents) { +ntfs_error (ni->vol->sb,"Inode already has attribute list" );
Lines of this file are not shown. View the raw message.
fs/ntfsplus/mft.c new file +2698 -0
2,698 lines not on this page · raw message
fs/ntfsplus/mst.c new file +195 -0
195 lines not on this page · raw message
fs/ntfsplus/namei.c new file +1677 -0
1,677 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.25.1
Part of the diff is not shown here. View the raw message.
PATCH v2 4/11 ntfsplus: add directory operations
In reply to Namjae JeonThis adds the implementation of directory operations for ntfsplus.
--- fs/ntfsplus/dir.c | 1230 +++++++++++++++++++++++++ fs/ntfsplus/index.c | 2112 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 3342 insertions(+) create mode 100644 fs/ntfsplus/dir.c create mode 100644 fs/ntfsplus/index.c
fs/ntfsplus/dir.c new file +1230 -0
@@ -0,0 +1,1230 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** +* NTFS kernel directory operations. Part of the Linux-NTFS project. +* +* Copyright (c) 2001-2007 Anton Altaparmakov +* Copyright (c) 2002 Richard Russon +* Copyright (c) 2025 LG Electronics Co., Ltd. +*/ + +#include <linux/blkdev.h> + +#include "dir.h" +#include "mft.h" +#include "ntfs.h" +#include "index.h" +#include "reparse.h" + +/** +* The little endian Unicode string $I30 as a global constant. +*/ +__le16 I30 [5 ] = {cpu_to_le16 ('$' ),cpu_to_le16 ('I' ), +cpu_to_le16 ('3' ),cpu_to_le16 ('0' ),0 }; + +/** +* ntfs_lookup_inode_by_name - find an inode in a directory given its name +* @dir_ni: ntfs inode of the directory in which to search for the name +* @uname: Unicode name for which to search in the directory +* @uname_len: length of the name @uname in Unicode characters +* @res: return the found file name if necessary (see below) +* +* Look for an inode with name @uname in the directory with inode @dir_ni. +* ntfs_lookup_inode_by_name() walks the contents of the directory looking for +* the Unicode name. If the name is found in the directory, the corresponding +* inode number (>= 0) is returned as a mft reference in cpu format, i.e. it +* is a 64-bit number containing the sequence number. +* +* On error, a negative value is returned corresponding to the error code. In +* particular if the inode is not found -ENOENT is returned. Note that you +* can't just check the return value for being negative, you have to check the +* inode number for being negative which you can extract using MREC(return +* value). +* +* Note, @uname_len does not include the (optional) terminating NULL character. +* +* Note, we look for a case sensitive match first but we also look for a case +* insensitive match at the same time. If we find a case insensitive match, we +* save that for the case that we don't find an exact match, where we return +* the case insensitive match and setup @res (which we allocate!) with the mft +* reference, the file name type, length and with a copy of the little endian +* Unicode file name itself. If we match a file name which is in the DOS name +* space, we only return the mft reference and file name type in @res. +* ntfs_lookup() then uses this to find the long file name in the inode itself. +* This is to avoid polluting the dcache with short file names. We want them to +* work but we don't care for how quickly one can access them. This also fixes +* the dcache aliasing issues. +* +* Locking: - Caller must hold i_mutex on the directory. +* - Each page cache page in the index allocation mapping must be +* locked whilst being accessed otherwise we may find a corrupt +* page due to it being under ->writepage at the moment which +* applies the mst protection fixups before writing out and then +* removes them again after the write is complete after which it +* unlocks the page. +*/ +u64 ntfs_lookup_inode_by_name (struct ntfs_inode *dir_ni,const __le16 *uname, +const int uname_len,struct ntfs_name **res) +{ +struct ntfs_volume *vol = dir_ni->vol; +struct super_block *sb = vol->sb; +struct inode *ia_vi =NULL ; +struct mft_record *m; +struct index_root *ir; +struct index_entry *ie; +struct index_block *ia; +u8 *index_end; +u64 mref; +struct ntfs_attr_search_ctx *ctx; +int err, rc; +s64 vcn, old_vcn; +struct address_space *ia_mapping; +struct folio *folio; +u8 *kaddr =NULL ; +struct ntfs_name *name =NULL ; + +/* Get hold of the mft record for the directory. */ + m =map_mft_record (dir_ni); +if (IS_ERR (m)) { +ntfs_error (sb,"map_mft_record() failed with error code %ld." , + -PTR_ERR (m)); +return ERR_MREF (PTR_ERR (m)); + } + ctx =ntfs_attr_get_search_ctx (dir_ni, m); +if (unlikely (!ctx)) { + err = -ENOMEM ; + goto err_out; + } +/* Find the index root attribute in the mft record. */ + err =ntfs_attr_lookup (AT_INDEX_ROOT ,I30 ,4 ,CASE_SENSITIVE ,0 ,NULL , +0 , ctx); +if (unlikely (err)) { +if (err == -ENOENT ) { +ntfs_error (sb, +"Index root attribute missing in directory inode 0x%lx." , + dir_ni->mft_no); + err = -EIO ; + } + goto err_out; + } +/* Get to the index root value (it's been verified in read_inode). */ + ir = (struct index_root *)((u8 *)ctx->attr + +le16_to_cpu (ctx->attr->data.resident.value_offset)); + index_end = (u8 *)&ir->index +le32_to_cpu (ir->index.index_length); +/* The first index entry. */ + ie = (struct index_entry *)((u8 *)&ir->index + +le32_to_cpu (ir->index.entries_offset)); +/* +* Loop until we exceed valid memory (corruption case) or until we +* reach the last entry. +*/ +for (;; ie = (struct index_entry *)((u8 *)ie +le16_to_cpu (ie->length))) { +/* Bounds checks. */ +if ((u8 *)ie < (u8 *)ctx->mrec || + (u8 *)ie +sizeof (struct index_entry_header ) > index_end || + (u8 *)ie +sizeof (struct index_entry_header ) +le16_to_cpu (ie->key_length) > + index_end || (u8 *)ie +le16_to_cpu (ie->length) > index_end) + goto dir_err_out; +/* +* The last entry cannot contain a name. It can however contain +* a pointer to a child node in the B+tree so we just break out. +*/ +if (ie->flags &INDEX_ENTRY_END ) +break ; +/* Key length should not be zero if it is not last entry. */ +if (!ie->key_length) + goto dir_err_out; +/* Check the consistency of an index entry */ +if (ntfs_index_entry_inconsistent (NULL , vol, ie,COLLATION_FILE_NAME , + dir_ni->mft_no)) + goto dir_err_out; +/* +* We perform a case sensitive comparison and if that matches +* we are done and return the mft reference of the inode (i.e. +* the inode number together with the sequence number for +* consistency checking). We convert it to cpu format before +* returning. +*/ +if (ntfs_are_names_equal (uname, uname_len, + (__le16 *)&ie->key.file_name.file_name, + ie->key.file_name.file_name_length, +CASE_SENSITIVE , vol->upcase, vol->upcase_len)) { +found_it: +/* +* We have a perfect match, so we don't need to care +* about having matched imperfectly before, so we can +* free name and set *res to NULL. +* However, if the perfect match is a short file name, +* we need to signal this through *res, so that +* ntfs_lookup() can fix dcache aliasing issues. +* As an optimization we just reuse an existing +* allocation of *res. +*/ +if (ie->key.file_name.file_name_type ==FILE_NAME_DOS ) { +if (!name) { + name =kmalloc (sizeof (struct ntfs_name ), +GFP_NOFS ); +if (!name) { + err = -ENOMEM ; + goto err_out; + } + } + name->mref =le64_to_cpu ( + ie->data.dir.indexed_file); + name->type =FILE_NAME_DOS ; + name->len =0 ; + *res = name; + }else { +kfree (name); + *res =NULL ; + } + mref =le64_to_cpu (ie->data.dir.indexed_file); +ntfs_attr_put_search_ctx (ctx); +unmap_mft_record (dir_ni); +return mref; + } +/* +* For a case insensitive mount, we also perform a case +* insensitive comparison (provided the file name is not in the +* POSIX namespace). If the comparison matches, and the name is +* in the WIN32 namespace, we cache the filename in *res so +* that the caller, ntfs_lookup(), can work on it. If the +* comparison matches, and the name is in the DOS namespace, we +* only cache the mft reference and the file name type (we set +* the name length to zero for simplicity). +*/ +if ((!NVolCaseSensitive (vol) || + ie->key.file_name.file_name_type ==FILE_NAME_DOS ) && +ntfs_are_names_equal (uname, uname_len, + (__le16 *)&ie->key.file_name.file_name, + ie->key.file_name.file_name_length, +IGNORE_CASE , vol->upcase, + vol->upcase_len)) { +int name_size =sizeof (struct ntfs_name ); +u8 type = ie->key.file_name.file_name_type; +u8 len = ie->key.file_name.file_name_length; + +/* Only one case insensitive matching name allowed. */ +if (name) { +ntfs_error (sb, +"Found already allocated name in phase 1. Please run chkdsk" ); + goto dir_err_out; + } + +if (type !=FILE_NAME_DOS ) + name_size += len *sizeof (__le16); + name =kmalloc (name_size,GFP_NOFS ); +if (!name) { + err = -ENOMEM ; + goto err_out; + } + name->mref =le64_to_cpu (ie->data.dir.indexed_file); + name->type = type; +if (type !=FILE_NAME_DOS ) { + name->len = len; +memcpy (name->name, ie->key.file_name.file_name, + len *sizeof (__le16)); + }else + name->len =0 ; + *res = name; + } +/* +* Not a perfect match, need to do full blown collation so we +* know which way in the B+tree we have to go. +*/ + rc =ntfs_collate_names (uname, uname_len, + (__le16 *)&ie->key.file_name.file_name, + ie->key.file_name.file_name_length,1 , +IGNORE_CASE , vol->upcase, vol->upcase_len); +/* +* If uname collates before the name of the current entry, there +* is definitely no such name in this index but we might need to +* descend into the B+tree so we just break out of the loop. +*/ +if (rc ==-1 ) +break ; +/* The names are not equal, continue the search. */ +if (rc) +continue ; +/* +* Names match with case insensitive comparison, now try the +* case sensitive comparison, which is required for proper +* collation. +*/ + rc =ntfs_collate_names (uname, uname_len, + (__le16 *)&ie->key.file_name.file_name, + ie->key.file_name.file_name_length,1 , +CASE_SENSITIVE , vol->upcase, vol->upcase_len); +if (rc ==-1 ) +break ; +if (rc) +continue ; +/* +* Perfect match, this will never happen as the +* ntfs_are_names_equal() call will have gotten a match but we +* still treat it correctly. +*/ + goto found_it; + } +/* +* We have finished with this index without success. Check for the +* presence of a child node and if not present return -ENOENT, unless +* we have got a matching name cached in name in which case return the +* mft reference associated with it. +*/ +if (!(ie->flags &INDEX_ENTRY_NODE )) { +if (name) { +ntfs_attr_put_search_ctx (ctx); +unmap_mft_record (dir_ni); +return name->mref; + } +ntfs_debug ("Entry not found." ); + err = -ENOENT ; + goto err_out; + }/* Child node present, descend into it. */ + +/* Get the starting vcn of the index_block holding the child node. */ + vcn =le64_to_cpup ((__le64 *)((u8 *)ie +le16_to_cpu (ie->length) -8 )); + +/* +* We are done with the index root and the mft record. Release them, +* otherwise we deadlock with ntfs_read_mapping_folio(). +*/ +ntfs_attr_put_search_ctx (ctx); +unmap_mft_record (dir_ni); + m =NULL ; + ctx =NULL ; + + ia_vi =ntfs_index_iget (VFS_I (dir_ni),I30 ,4 ); +if (IS_ERR (ia_vi)) { + err =PTR_ERR (ia_vi); + goto err_out; + } + + ia_mapping = ia_vi->i_mapping; +descend_into_child_node: +/* +* Convert vcn to index into the index allocation attribute in units +* of PAGE_SIZE and map the page cache page, reading it from +* disk if necessary. +*/ + folio =ntfs_read_mapping_folio (ia_mapping, vcn << + dir_ni->itype.index.vcn_size_bits >>PAGE_SHIFT ); +if (IS_ERR (folio)) { +ntfs_error (sb,"Failed to map directory index page, error %ld." , + -PTR_ERR (folio)); + err =PTR_ERR (folio); + goto err_out; + } + +folio_lock (folio); + kaddr =kmalloc (PAGE_SIZE ,GFP_NOFS ); +if (!kaddr) { + err = -ENOMEM ; +folio_unlock (folio); +folio_put (folio); + goto unm_err_out; + } + +memcpy_from_folio (kaddr, folio,0 ,PAGE_SIZE ); +post_read_mst_fixup ((struct ntfs_record *)kaddr,PAGE_SIZE ); +folio_unlock (folio); +folio_put (folio); +fast_descend_into_child_node: +/* Get to the index allocation block. */ + ia = (struct index_block *)(kaddr + ((vcn << + dir_ni->itype.index.vcn_size_bits) & ~PAGE_MASK )); +/* Bounds checks. */ +if ((u8 *)ia < kaddr || (u8 *)ia > kaddr +PAGE_SIZE ) { +ntfs_error (sb, +"Out of bounds check failed. Corrupt directory inode 0x%lx or driver bug." , + dir_ni->mft_no); + goto unm_err_out; + } +/* Catch multi sector transfer fixup errors. */ +if (unlikely (!ntfs_is_indx_record (ia->magic))) { +ntfs_error (sb, +"Directory index record with vcn 0x%llx is corrupt. Corrupt inode 0x%lx. Run chkdsk." , + (unsigned long long )vcn, dir_ni->mft_no); + goto unm_err_out; + } +if (le64_to_cpu (ia->index_block_vcn) != vcn) { +ntfs_error (sb, +"Actual VCN (0x%llx) of index buffer is different from expected VCN (0x%llx). Directory inode 0x%lx is corrupt or driver bug." , + (unsigned long long )le64_to_cpu (ia->index_block_vcn), + (unsigned long long )vcn, dir_ni->mft_no); + goto unm_err_out; + } +if (le32_to_cpu (ia->index.allocated_size) +0x18 != + dir_ni->itype.index.block_size) { +ntfs_error (sb, +"Index buffer (VCN 0x%llx) of directory inode 0x%lx has a size (%u) differing from the directory specified size (%u). Directory inode is corrupt or driver bug." , + (unsigned long long )vcn, dir_ni->mft_no, +le32_to_cpu (ia->index.allocated_size) +0x18 , + dir_ni->itype.index.block_size); + goto unm_err_out; + } + index_end = (u8 *)ia + dir_ni->itype.index.block_size; +if (index_end > kaddr +PAGE_SIZE ) { +ntfs_error (sb, +"Index buffer (VCN 0x%llx) of directory inode 0x%lx crosses page boundary. Impossible! Cannot access! This is probably a bug in the driver." , + (unsigned long long )vcn, dir_ni->mft_no); + goto unm_err_out; + } + index_end = (u8 *)&ia->index +le32_to_cpu (ia->index.index_length); +if (index_end > (u8 *)ia + dir_ni->itype.index.block_size) { +ntfs_error (sb, +"Size of index buffer (VCN 0x%llx) of directory inode 0x%lx exceeds maximum size." , + (unsigned long long )vcn, dir_ni->mft_no); + goto unm_err_out; + } +/* The first index entry. */ + ie = (struct index_entry *)((u8 *)&ia->index + +le32_to_cpu (ia->index.entries_offset)); +/* +* Iterate similar to above big loop but applied to index buffer, thus +* loop until we exceed valid memory (corruption case) or until we +* reach the last entry. +*/ +for (;; ie = (struct index_entry *)((u8 *)ie +le16_to_cpu (ie->length))) { +/* Bounds checks. */ +if ((u8 *)ie < (u8 *)ia || + (u8 *)ie +sizeof (struct index_entry_header ) > index_end || + (u8 *)ie +sizeof (struct index_entry_header ) +le16_to_cpu (ie->key_length) > + index_end || (u8 *)ie +le16_to_cpu (ie->length) > index_end) { +ntfs_error (sb,"Index entry out of bounds in directory inode 0x%lx." , + dir_ni->mft_no); + goto unm_err_out; + } +/* +* The last entry cannot contain a name. It can however contain +* a pointer to a child node in the B+tree so we just break out. +*/ +if (ie->flags &INDEX_ENTRY_END ) +break ; +/* Key length should not be zero if it is not last entry. */ +if (!ie->key_length) + goto unm_err_out; +/* Check the consistency of an index entry */ +if (ntfs_index_entry_inconsistent (NULL , vol, ie,COLLATION_FILE_NAME , + dir_ni->mft_no)) + goto unm_err_out; +/* +* We perform a case sensitive comparison and if that matches +* we are done and return the mft reference of the inode (i.e. +* the inode number together with the sequence number for +* consistency checking). We convert it to cpu format before +* returning. +*/ +if (ntfs_are_names_equal (uname, uname_len, + (__le16 *)&ie->key.file_name.file_name, + ie->key.file_name.file_name_length, +CASE_SENSITIVE , vol->upcase, vol->upcase_len)) { +found_it2: +/* +* We have a perfect match, so we don't need to care +* about having matched imperfectly before, so we can +* free name and set *res to NULL. +* However, if the perfect match is a short file name, +* we need to signal this through *res, so that +* ntfs_lookup() can fix dcache aliasing issues. +* As an optimization we just reuse an existing +* allocation of *res. +*/ +if (ie->key.file_name.file_name_type ==FILE_NAME_DOS ) { +if (!name) { + name =kmalloc (sizeof (struct ntfs_name ), +GFP_NOFS ); +if (!name) { + err = -ENOMEM ; + goto unm_err_out; + } + } + name->mref =le64_to_cpu ( + ie->data.dir.indexed_file); + name->type =FILE_NAME_DOS ; + name->len =0 ; + *res = name; + }else { +kfree (name); + *res =NULL ; + } + mref =le64_to_cpu (ie->data.dir.indexed_file); +kfree (kaddr); +iput (ia_vi); +return mref; + } +/* +* For a case insensitive mount, we also perform a case +* insensitive comparison (provided the file name is not in the +* POSIX namespace). If the comparison matches, and the name is +* in the WIN32 namespace, we cache the filename in *res so +* that the caller, ntfs_lookup(), can work on it. If the +* comparison matches, and the name is in the DOS namespace, we +* only cache the mft reference and the file name type (we set +* the name length to zero for simplicity). +*/ +if ((!NVolCaseSensitive (vol) || + ie->key.file_name.file_name_type ==FILE_NAME_DOS ) && +ntfs_are_names_equal (uname, uname_len, + (__le16 *)&ie->key.file_name.file_name, + ie->key.file_name.file_name_length, +IGNORE_CASE , vol->upcase, + vol->upcase_len)) { +int name_size =sizeof (struct ntfs_name ); +u8 type = ie->key.file_name.file_name_type; +u8 len = ie->key.file_name.file_name_length; + +/* Only one case insensitive matching name allowed. */ +if (name) { +ntfs_error (sb, +"Found already allocated name in phase 2. Please run chkdsk" ); +kfree (kaddr); + goto dir_err_out; + } + +if (type !=FILE_NAME_DOS ) + name_size += len *sizeof (__le16); + name =kmalloc (name_size,GFP_NOFS ); +if (!name) { + err = -ENOMEM ; + goto unm_err_out; + } + name->mref =le64_to_cpu (ie->data.dir.indexed_file); + name->type = type; +if (type !=FILE_NAME_DOS ) { + name->len = len; +memcpy (name->name, ie->key.file_name.file_name, + len *sizeof (__le16)); + }else + name->len =0 ; + *res = name; + } +/* +* Not a perfect match, need to do full blown collation so we +* know which way in the B+tree we have to go. +*/ + rc =ntfs_collate_names (uname, uname_len, + (__le16 *)&ie->key.file_name.file_name, + ie->key.file_name.file_name_length,1 , +IGNORE_CASE , vol->upcase, vol->upcase_len); +/* +* If uname collates before the name of the current entry, there +* is definitely no such name in this index but we might need to +* descend into the B+tree so we just break out of the loop. +*/ +if (rc ==-1 ) +break ; +/* The names are not equal, continue the search. */ +if (rc) +continue ; +/* +* Names match with case insensitive comparison, now try the +* case sensitive comparison, which is required for proper +* collation. +*/ + rc =ntfs_collate_names (uname, uname_len, + (__le16 *)&ie->key.file_name.file_name, + ie->key.file_name.file_name_length,1 , +CASE_SENSITIVE , vol->upcase, vol->upcase_len); +if (rc ==-1 ) +break ; +if (rc) +continue ; +/* +* Perfect match, this will never happen as the +* ntfs_are_names_equal() call will have gotten a match but we +* still treat it correctly. +*/ + goto found_it2; + } +/* +* We have finished with this index buffer without success. Check for +* the presence of a child node. +*/ +if (ie->flags &INDEX_ENTRY_NODE ) { +if ((ia->index.flags &NODE_MASK ) ==LEAF_NODE ) { +ntfs_error (sb, +"Index entry with child node found in a leaf node in directory inode 0x%lx." , + dir_ni->mft_no); + goto unm_err_out; + } +/* Child node present, descend into it. */ + old_vcn = vcn; + vcn =le64_to_cpup ((__le64 *)((u8 *)ie + +le16_to_cpu (ie->length) -8 )); +if (vcn >=0 ) { +/* +* If vcn is in the same page cache page as old_vcn we +* recycle the mapped page. +*/ +if ((old_vcn << vol->cluster_size_bits >>PAGE_SHIFT ) == + (vcn << vol->cluster_size_bits >>PAGE_SHIFT )) + goto fast_descend_into_child_node; +kfree (kaddr); + kaddr =NULL ; + goto descend_into_child_node; + } +ntfs_error (sb,"Negative child node vcn in directory inode 0x%lx." , + dir_ni->mft_no); + goto unm_err_out; + } +/* +* No child node present, return -ENOENT, unless we have got a matching +* name cached in name in which case return the mft reference +* associated with it. +*/ +if (name) { +kfree (kaddr); +iput (ia_vi); +return name->mref; + } +ntfs_debug ("Entry not found." ); + err = -ENOENT ; +unm_err_out: +kfree (kaddr); +err_out: +if (!err) + err = -EIO ; +if (ctx) +ntfs_attr_put_search_ctx (ctx); +if (m) +unmap_mft_record (dir_ni); +kfree (name); + *res =NULL ; +if (ia_vi && !IS_ERR (ia_vi)) +iput (ia_vi); +return ERR_MREF (err); +dir_err_out: +ntfs_error (sb,"Corrupt directory. Aborting lookup." ); + goto err_out; +} + +/** +* ntfs_filldir - ntfs specific filldir method +* @vol: current ntfs volume +* @ndir: ntfs inode of current directory +* @ia_page: page in which the index allocation buffer @ie is in resides +* @ie: current index entry +* @name: buffer to use for the converted name +* @actor: what to feed the entries to +* +* Convert the Unicode @name to the loaded NLS and pass it to the @filldir +* callback. +* +* If @ia_page is not NULL it is the locked page containing the index +* allocation block containing the index entry @ie. +* +* Note, we drop (and then reacquire) the page lock on @ia_page across the +* @filldir() call otherwise we would deadlock with NFSd when it calls ->lookup +* since ntfs_lookup() will lock the same page. As an optimization, we do not +* retake the lock if we are returning a non-zero value as ntfs_readdir() +* would need to drop the lock immediately anyway. +*/ +static inline int ntfs_filldir (struct ntfs_volume *vol, +struct ntfs_inode *ndir,struct page *ia_page,struct index_entry *ie, +u8 *name,struct dir_context *actor) +{ +unsigned long mref; +int name_len; +unsigned int dt_type; +u8 name_type; + + name_type = ie->key.file_name.file_name_type; +if (name_type ==FILE_NAME_DOS ) { +ntfs_debug ("Skipping DOS name space entry." ); +return 0 ; + } +if (MREF_LE (ie->data.dir.indexed_file) == FILE_root) { +ntfs_debug ("Skipping root directory self reference entry." ); +return 0 ; + } +if (MREF_LE (ie->data.dir.indexed_file) < FILE_first_user && + !NVolShowSystemFiles (vol)) { +ntfs_debug ("Skipping system file." ); +return 0 ; + } +if (!NVolShowHiddenFiles (vol) && + (ie->key.file_name.file_attributes &FILE_ATTR_HIDDEN )) { +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 ); +if (name_len <=0 ) { +ntfs_warning (vol->sb,"Skipping unrepresentable inode 0x%llx." , + (long long )MREF_LE (ie->data.dir.indexed_file)); +return 0 ; + } + + mref =MREF_LE (ie->data.dir.indexed_file); +if (ie->key.file_name.file_attributes & +FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT ) + dt_type =DT_DIR ; +else if (ie->key.file_name.file_attributes &FILE_ATTR_REPARSE_POINT ) + dt_type =ntfs_reparse_tag_dt_types (vol, mref); +else + dt_type =DT_REG ; + +/* +* Drop the page lock otherwise we deadlock with NFS when it calls +* ->lookup since ntfs_lookup() will lock the same page. +*/ +if (ia_page) +unlock_page (ia_page); +ntfs_debug ("Calling filldir for %s with len %i, fpos 0x%llx, inode 0x%lx, DT_%s." , + name, name_len, actor->pos, mref, dt_type ==DT_DIR ?"DIR" :"REG" ); +if (!dir_emit (actor, name, name_len, mref, dt_type)) +return 1 ; +/* Relock the page but not if we are aborting ->readdir. */ +if (ia_page) +lock_page (ia_page); +return 0 ; +} + +struct ntfs_file_private { +void *key; +__le16 key_length; +bool end_in_iterate; +loff_t curr_pos; +}; + +struct ntfs_index_ra { +unsigned long start_index; +unsigned int count; +struct rb_node rb_node; +}; + +static void ntfs_insert_rb (struct ntfs_index_ra *nir,struct rb_root *root) +{ +struct rb_node **new = &root->rb_node, *parent =NULL ; +struct ntfs_index_ra *cnir; + +while (*new) { + parent = *new; + cnir = rb_entry(parent, struct ntfs_index_ra, rb_node); +if (nir->start_index < cnir->start_index) + new = &parent->rb_left; +else if (nir->start_index >= cnir->start_index + cnir->count) + new = &parent->rb_right; +else { +pr_err ("nir start index : %ld, count : %d, cnir start_index : %ld, count : %d\n" , + nir->start_index, nir->count, cnir->start_index, cnir->count); +return ; + } + } + +rb_link_node (&nir->rb_node, parent, new); +rb_insert_color (&nir->rb_node, root); +} + +static int ntfs_ia_blocks_readahead (struct ntfs_inode *ia_ni,loff_t pos) +{ +unsigned long dir_start_index, dir_end_index; +struct inode *ia_vi =VFS_I (ia_ni); +struct file_ra_state *dir_ra; + + dir_end_index = (i_size_read (ia_vi) +PAGE_SIZE -1 ) >>PAGE_SHIFT ; + dir_start_index = (pos +PAGE_SIZE -1 ) >>PAGE_SHIFT ; + +if (dir_start_index >= dir_end_index) +return 0 ; + + dir_ra =kzalloc (sizeof (*dir_ra),GFP_NOFS ); +if (!dir_ra) +return -ENOMEM ; + +file_ra_state_init (dir_ra, ia_vi->i_mapping); + dir_end_index = (i_size_read (ia_vi) +PAGE_SIZE -1 ) >>PAGE_SHIFT ; + dir_start_index = (pos +PAGE_SIZE -1 ) >>PAGE_SHIFT ; + dir_ra->ra_pages = dir_end_index - dir_start_index; +page_cache_sync_readahead (ia_vi->i_mapping, dir_ra,NULL , + dir_start_index, dir_end_index - dir_start_index); +kfree (dir_ra); + +return 0 ; +} + +static int ntfs_readdir (struct file *file,struct dir_context *actor) +{ +struct inode *vdir =file_inode (file); +struct super_block *sb = vdir->i_sb; +struct ntfs_inode *ndir =NTFS_I (vdir); +struct ntfs_volume *vol =NTFS_SB (sb); +struct ntfs_attr_search_ctx *ctx =NULL ; +struct ntfs_index_context *ictx =NULL ; +u8 *name; +struct index_root *ir; +struct index_entry *next =NULL ; +struct ntfs_file_private *private =NULL ; +int err =0 ; +loff_t ie_pos =2 ;/* initialize it with dot and dotdot size */ +struct ntfs_index_ra *nir =NULL ; +unsigned long index; +struct rb_root ra_root =RB_ROOT ; +struct file_ra_state *ra; + +ntfs_debug ("Entering for inode 0x%lx, fpos 0x%llx." , + vdir->i_ino, actor->pos); + +if (file->private_data) { + private = file->private_data; + +if (actor->pos != private->curr_pos) { +/* +* If actor->pos is different from the previous passed +* one, Discard the private->key and fill dirent buffer +* with linear lookup. +*/ +kfree (private->key); + private->key =NULL ; + private->end_in_iterate = false; + }else if (private->end_in_iterate) { +kfree (private->key); +kfree (file->private_data); + file->private_data =NULL ; +return 0 ; + } + } + +/* Emulate . and .. for all directories. */ +if (!dir_emit_dots (file, actor)) +return 0 ; + +/* +* Allocate a buffer to store the current name being processed +* converted to format determined by current NLS. +*/ + name =kmalloc (NTFS_MAX_NAME_LEN *NLS_MAX_CHARSET_SIZE +1 ,GFP_NOFS ); +if (unlikely (!name)) +return -ENOMEM ; + +mutex_lock_nested (&ndir->mrec_lock,NTFS_INODE_MUTEX_PARENT ); + ictx =ntfs_index_ctx_get (ndir,I30 ,4 ); +if (!ictx) { +kfree (name); +mutex_unlock (&ndir->mrec_lock); +return -ENOMEM ; + } + + ra =kzalloc (sizeof (struct file_ra_state ),GFP_NOFS ); +if (!ra) { +kfree (name); +ntfs_index_ctx_put (ictx); +mutex_unlock (&ndir->mrec_lock); +return -ENOMEM ; + } +file_ra_state_init (ra, vol->mft_ino->i_mapping); + +if (private && private->key) { +/* +* Find index witk private->key using ntfs_index_lookup() +* instead of linear index lookup. +*/ + err =ntfs_index_lookup (private->key, +le16_to_cpu (private->key_length), + ictx); +if (!err) { + next = ictx->entry; +/* +* Update ie_pos with private->curr_pos +* to make next d_off of dirent correct. +*/ + ie_pos = private->curr_pos; + +if (actor->pos > vol->mft_record_size && ictx->ia_ni) { + err =ntfs_ia_blocks_readahead (ictx->ia_ni, actor->pos); +if (err) + goto out; + } + + goto nextdir; + }else { + goto out; + } + }else if (!private) { + private =kzalloc (sizeof (struct ntfs_file_private ),GFP_KERNEL ); +if (!private) { + err = -ENOMEM ; + goto out; + } + file->private_data = private; + } + + ctx =ntfs_attr_get_search_ctx (ndir,NULL ); +if (!ctx) { + err = -ENOMEM ; + goto out; + } + +/* Find the index root attribute in the mft record. */ +if (ntfs_attr_lookup (AT_INDEX_ROOT ,I30 ,4 ,CASE_SENSITIVE ,0 ,NULL ,0 , + ctx)) { +ntfs_error (sb,"Index root attribute missing in directory inode %ld" , + ndir->mft_no); +ntfs_attr_put_search_ctx (ctx); + err = -ENOMEM ; + goto out; + } + +/* Get to the index root value. */ + ir = (struct index_root *)((u8 *)ctx->attr + +le16_to_cpu (ctx->attr->data.resident.value_offset)); + + ictx->ir = ir; + ictx->actx = ctx; + ictx->parent_vcn[ictx->pindex] =VCN_INDEX_ROOT_PARENT ; + ictx->is_in_root = true; + ictx->parent_pos[ictx->pindex] =0 ; + + ictx->block_size =le32_to_cpu (ir->index_block_size); +if (ictx->block_size <NTFS_BLOCK_SIZE ) { +ntfs_error (sb,"Index block size (%d) is smaller than the sector size (%d)" , + ictx->block_size,NTFS_BLOCK_SIZE ); + err = -EIO ; + goto out; + } + +if (vol->cluster_size <= ictx->block_size) + ictx->vcn_size_bits = vol->cluster_size_bits; +else + ictx->vcn_size_bits =NTFS_BLOCK_SIZE_BITS ; + +/* The first index entry. */ + next = (struct index_entry *)((u8 *)&ir->index + +le32_to_cpu (ir->index.entries_offset)); + +if (next->flags &INDEX_ENTRY_NODE ) { + ictx->ia_ni =ntfs_ia_open (ictx, ictx->idx_ni); +if (!ictx->ia_ni) { + err = -EINVAL ; + goto out; + } + + err =ntfs_ia_blocks_readahead (ictx->ia_ni, actor->pos); +if (err) + goto out; + } + +if (next->flags &INDEX_ENTRY_NODE ) { + next =ntfs_index_walk_down (next, ictx); +if (!next) { + err = -EIO ; + goto out; + } + } + +if (next && !(next->flags &INDEX_ENTRY_END )) + goto nextdir; + +while ((next =ntfs_index_next (next, ictx)) !=NULL ) { +nextdir: +/* Check the consistency of an index entry */ +if (ntfs_index_entry_inconsistent (ictx, vol, next,COLLATION_FILE_NAME , + ndir->mft_no)) { + err = -EIO ; + goto out; + } + +if (ie_pos < actor->pos) { + ie_pos += next->length; +continue ; + } + + actor->pos = ie_pos; + + index = (MREF_LE (next->data.dir.indexed_file) << + vol->mft_record_size_bits) >>PAGE_SHIFT ; +if (nir) { +struct ntfs_index_ra *cnir; +struct rb_node *node = ra_root.rb_node; + +if (nir->start_index <= index && + index < nir->start_index + nir->count) { +/* No behavior */ + goto filldir; + } + +while (node) { + cnir = rb_entry(node, struct ntfs_index_ra, rb_node); +if (cnir->start_index <= index && + index < cnir->start_index + cnir->count) { + goto filldir; + }else if (cnir->start_index + cnir->count == index) { + cnir->count++; + goto filldir; + }else if (!cnir->start_index && cnir->start_index -1 == index) { + cnir->start_index = index; + goto filldir; + } + +if (index < cnir->start_index) + node = node->rb_left; +else if (index >= cnir->start_index + cnir->count) + node = node->rb_right; + } + +if (nir->start_index + nir->count == index) { + nir->count++; + }else if (!nir->start_index && nir->start_index -1 == index) { + nir->start_index = index; + }else if (nir->count >2 ) { +ntfs_insert_rb (nir, &ra_root); + nir =NULL ; + }else { + nir->start_index = index; + nir->count =1 ; + } + } + +if (!nir) { + nir =kzalloc (sizeof (struct ntfs_index_ra ),GFP_KERNEL ); +if (nir) { + nir->start_index = index; + nir->count =1 ; + } + } + +filldir: +/* Submit the name to the filldir callback. */ + err =ntfs_filldir (vol, ndir,NULL , next, name, actor); +if (err) { +/* +* Store index key value to file private_data to start +* from current index offset on next round. +*/ + private = file->private_data; +kfree (private->key); + private->key =kmalloc (le16_to_cpu (next->key_length),GFP_KERNEL ); +if (!private->key) { + err = -ENOMEM ; + goto out; + } + +memcpy (private->key, &next->key.file_name,le16_to_cpu (next->key_length)); + private->key_length = next->key_length; +break ; + } + ie_pos += next->length; + } + +if (!err) + private->end_in_iterate = true; +else + err =0 ; + + private->curr_pos = actor->pos = ie_pos; +out: +while (!RB_EMPTY_ROOT (&ra_root)) { +struct ntfs_index_ra *cnir; +struct rb_node *node; + + node =rb_first (&ra_root); + cnir = rb_entry(node, struct ntfs_index_ra, rb_node); + ra->ra_pages = cnir->count; +page_cache_sync_readahead (vol->mft_ino->i_mapping, ra,NULL , + cnir->start_index, cnir->count); +rb_erase (node, &ra_root); +kfree (cnir); + } + +if (err) { + private->curr_pos = actor->pos; + private->end_in_iterate = true; + err =0 ; + } +ntfs_index_ctx_put (ictx); +kfree (name); +kfree (nir); +kfree (ra); +mutex_unlock (&ndir->mrec_lock); +return err; +} + +int ntfs_check_empty_dir (struct ntfs_inode *ni,struct mft_record *ni_mrec) +{ +struct ntfs_attr_search_ctx *ctx; +int ret =0 ; + +if (!(ni_mrec->flags &MFT_RECORD_IS_DIRECTORY )) +return 0 ; + + ctx =ntfs_attr_get_search_ctx (ni,NULL ); +if (!ctx) { +ntfs_error (ni->vol->sb,"Failed to get search context" ); +return -ENOMEM ; + } + +/* Find the index root attribute in the mft record. */ + ret =ntfs_attr_lookup (AT_INDEX_ROOT ,I30 ,4 ,CASE_SENSITIVE ,0 ,NULL , +0 , ctx); +if (ret) { +ntfs_error (ni->vol->sb,"Index root attribute missing in directory inode %lld" , + (unsigned long long )ni->mft_no); +ntfs_attr_put_search_ctx (ctx); +return ret; + } + +/* Non-empty directory? */ +if (ctx->attr->data.resident.value_length != +sizeof (struct index_root ) +sizeof (struct index_entry_header )) { +/* Both ENOTEMPTY and EEXIST are ok. We use the more common. */ + ret = -ENOTEMPTY ; +ntfs_debug ("Directory is not empty\n" ); + } + +ntfs_attr_put_search_ctx (ctx); + +return ret; +} + +/** +* ntfs_dir_open - called when an inode is about to be opened +* @vi: inode to be opened +* @filp: file structure describing the inode +* +* Limit directory size to the page cache limit on architectures where unsigned +* long is 32-bits. This is the most we can do for now without overflowing the +* page cache page index. Doing it this way means we don't run into problems +* because of existing too large directories. It would be better to allow the +* user to read the accessible part of the directory but I doubt very much +* anyone is going to hit this check on a 32-bit architecture, so there is no +* point in adding the extra complexity required to support this. +* +* On 64-bit architectures, the check is hopefully optimized away by the +* compiler. +*/ +static int ntfs_dir_open (struct inode *vi,struct file *filp) +{ +if (sizeof (unsigned long ) <8 ) { +if (i_size_read (vi) >MAX_LFS_FILESIZE ) +return -EFBIG ; + } +return 0 ; +} + +static int ntfs_dir_release (struct inode *vi,struct file *filp) +{ +if (filp->private_data) { +kfree (((struct ntfs_file_private *)filp->private_data)->key); +kfree (filp->private_data); + filp->private_data =NULL ; + } +return 0 ; +} + +/** +* ntfs_dir_fsync - sync a directory to disk +* @filp: file describing the directory to be synced +* @start: start offset to be synced +* @end: end offset to be synced +* @datasync: if non-zero only flush user data and not metadata +* +* Data integrity sync of a directory to disk. Used for fsync, fdatasync, and +* msync system calls. This function is based on file.c::ntfs_file_fsync(). +* +* Write the mft record and all associated extent mft records as well as the +* $INDEX_ALLOCATION and $BITMAP attributes and then sync the block device. +* +* If @datasync is true, we do not wait on the inode(s) to be written out +* but we always wait on the page cache pages to be written out. +* +* Note: In the past @filp could be NULL so we ignore it as we don't need it +* anyway. +* +* Locking: Caller must hold i_mutex on the inode. +*/ +static int ntfs_dir_fsync (struct file *filp,loff_t start,loff_t end, +int datasync) +{ +struct inode *bmp_vi, *vi = filp->f_mapping->host; +struct ntfs_volume *vol =NTFS_I (vi)->vol; +struct ntfs_inode *ni =NTFS_I (vi); +struct ntfs_attr_search_ctx *ctx; +struct inode *parent_vi, *ia_vi; +int err, ret; +struct ntfs_attr na; + +ntfs_debug ("Entering for inode 0x%lx." , vi->i_ino); + +if (NVolShutdown (vol)) +return -EIO ; + + ctx =ntfs_attr_get_search_ctx (ni,NULL ); +if (!ctx) +return -ENOMEM ; + +mutex_lock_nested (&ni->mrec_lock,NTFS_INODE_MUTEX_NORMAL_2 ); +while (!(err =ntfs_attr_lookup (AT_FILE_NAME ,NULL ,0 ,0 ,0 ,NULL ,0 , ctx))) { +struct file_name_attr *fn = (struct file_name_attr *)((u8 *)ctx->attr + +le16_to_cpu (ctx->attr->data.resident.value_offset)); + + parent_vi =ntfs_iget (vi->i_sb,MREF_LE (fn->parent_directory)); +if (IS_ERR (parent_vi)) +continue ; +mutex_lock_nested (&NTFS_I (parent_vi)->mrec_lock,NTFS_INODE_MUTEX_PARENT_2 ); + ia_vi =ntfs_index_iget (parent_vi,I30 ,4 ); +mutex_unlock (&NTFS_I (parent_vi)->mrec_lock); +if (IS_ERR (ia_vi)) { +iput (parent_vi); +continue ; + } +write_inode_now (ia_vi,1 ); +iput (ia_vi); +write_inode_now (parent_vi,1 ); +iput (parent_vi); + } +mutex_unlock (&ni->mrec_lock); +ntfs_attr_put_search_ctx (ctx); + + err =file_write_and_wait_range (filp, start, end); +if (err) +return err; +inode_lock (vi); + +/* If the bitmap attribute inode is in memory sync it, too. */ + na.mft_no = vi->i_ino; + na.type =AT_BITMAP ; + na.name =I30 ; + na.name_len =4 ; + bmp_vi =ilookup5 (vi->i_sb, vi->i_ino, ntfs_test_inode, &na); +if (bmp_vi) { +write_inode_now (bmp_vi, !datasync); +iput (bmp_vi); + } + ret =__ntfs_write_inode (vi,1 ); + +write_inode_now (vi, !datasync); + +write_inode_now (vol->mftbmp_ino,1 ); +down_write (&vol->lcnbmp_lock); +write_inode_now (vol->lcnbmp_ino,1 ); +up_write (&vol->lcnbmp_lock); +write_inode_now (vol->mft_ino,1 ); + + err =sync_blockdev (vi->i_sb->s_bdev); +if (unlikely (err && !ret)) + ret = err; +if (likely (!ret)) +ntfs_debug ("Done." ); +else +ntfs_warning (vi->i_sb, +"Failed to f%ssync inode 0x%lx. Error %u." , + datasync ?"data" :"" , vi->i_ino, -ret); +inode_unlock (vi); +return ret; +} + +const struct file_operations ntfs_dir_ops = { + .llseek = generic_file_llseek,/* Seek inside directory. */ + .read = generic_read_dir,/* Return -EISDIR. */ + .iterate_shared = ntfs_readdir,/* Read directory contents. */ + .fsync = ntfs_dir_fsync,/* Sync a directory to disk. */ + .open = ntfs_dir_open,/* Open directory. */ + .release = ntfs_dir_release, + .unlocked_ioctl = ntfsp_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = ntfsp_compat_ioctl, +#endif +};
fs/ntfsplus/index.c new file +2112 -0
1,770 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.25.1
Part of the diff is not shown here. View the raw message.
PATCH v2 5/11 ntfsplus: add file operations
In reply to Namjae JeonThis adds the implementation of file operations for ntfsplus.
--- fs/ntfsplus/file.c | 1142 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1142 insertions(+) create mode 100644 fs/ntfsplus/file.c
fs/ntfsplus/file.c new file +1142 -0
@@ -0,0 +1,1142 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* NTFS kernel file operations. Part of the Linux-NTFS project. +* +* Copyright (c) 2001-2015 Anton Altaparmakov and Tuxera Inc. +* Copyright (c) 2025 LG Electronics Co., Ltd. +*/ + +#include <linux/writeback.h> +#include <linux/blkdev.h> +#include <linux/fs.h> +#include <linux/iomap.h> +#include <linux/uio.h> +#include <linux/posix_acl.h> +#include <linux/posix_acl_xattr.h> +#include <linux/compat.h> +#include <linux/falloc.h> +#include <uapi/linux/ntfs.h> + +#include "lcnalloc.h" +#include "ntfs.h" +#include "aops.h" +#include "reparse.h" +#include "ea.h" +#include "ntfs_iomap.h" +#include "misc.h" +#include "bitmap.h" + +/** +* ntfs_file_open - called when an inode is about to be opened +* @vi: inode to be opened +* @filp: file structure describing the inode +* +* Limit file size to the page cache limit on architectures where unsigned long +* is 32-bits. This is the most we can do for now without overflowing the page +* cache page index. Doing it this way means we don't run into problems because +* of existing too large files. It would be better to allow the user to read +* the beginning of the file but I doubt very much anyone is going to hit this +* check on a 32-bit architecture, so there is no point in adding the extra +* complexity required to support this. +* +* On 64-bit architectures, the check is hopefully optimized away by the +* compiler. +* +* After the check passes, just call generic_file_open() to do its work. +*/ +static int ntfs_file_open (struct inode *vi,struct file *filp) +{ +struct ntfs_inode *ni =NTFS_I (vi); + +if (NVolShutdown (ni->vol)) +return -EIO ; + +if (sizeof (unsigned long ) <8 ) { +if (i_size_read (vi) >MAX_LFS_FILESIZE ) +return -EOVERFLOW ; + } + +if (filp->f_flags &O_TRUNC &&NInoNonResident (ni)) { +int err; + +mutex_lock (&ni->mrec_lock); +down_read (&ni->runlist.lock); +if (!ni->runlist.rl) { + err =ntfs_attr_map_whole_runlist (ni); +if (err) { +up_read (&ni->runlist.lock); +mutex_unlock (&ni->mrec_lock); +return err; + } + } + ni->lcn_seek_trunc = ni->runlist.rl->lcn; +up_read (&ni->runlist.lock); +mutex_unlock (&ni->mrec_lock); + } + + filp->f_mode |=FMODE_NOWAIT ; + +return generic_file_open (vi, filp); +} + +static int ntfs_file_release (struct inode *vi,struct file *filp) +{ +struct ntfs_inode *ni =NTFS_I (vi); +struct ntfs_volume *vol = ni->vol; +s64 aligned_data_size =round_up (ni->data_size, vol->cluster_size); + +if (NInoCompressed (ni)) +return 0 ; + +inode_lock (vi); +mutex_lock (&ni->mrec_lock); +down_write (&ni->runlist.lock); +if (aligned_data_size < ni->allocated_size) { +int err; +s64 vcn_ds = aligned_data_size >> vol->cluster_size_bits; +s64 vcn_tr =-1 ; +struct runlist_element *rl = ni->runlist.rl; +ssize_t rc = ni->runlist.count -2 ; + +while (rc >=0 && rl[rc].lcn ==LCN_HOLE && vcn_ds <= rl[rc].vcn) { + vcn_tr = rl[rc].vcn; + rc--; + } + +if (vcn_tr >=0 ) { + err =ntfs_rl_truncate_nolock (vol, &ni->runlist, vcn_tr); +if (err) { +ntfs_free (ni->runlist.rl); + ni->runlist.rl =NULL ; +ntfs_error (vol->sb,"Preallocated block rollback failed" ); + }else { + ni->allocated_size = vcn_tr << vol->cluster_size_bits; + err =ntfs_attr_update_mapping_pairs (ni,0 ); +if (err) +ntfs_error (vol->sb, +"Failed to rollback mapping pairs for prealloc" ); + } + } + } +up_write (&ni->runlist.lock); +mutex_unlock (&ni->mrec_lock); +inode_unlock (vi); + +return 0 ; +} + +/** +* ntfs_file_fsync - sync a file to disk +* @filp: file to be synced +* @start: start offset to be synced +* @end: end offset to be synced +* @datasync: if non-zero only flush user data and not metadata +* +* Data integrity sync of a file to disk. Used for fsync, fdatasync, and msync +* system calls. This function is inspired by fs/buffer.c::file_fsync(). +* +* If @datasync is false, write the mft record and all associated extent mft +* records as well as the $DATA attribute and then sync the block device. +* +* If @datasync is true and the attribute is non-resident, we skip the writing +* of the mft record and all associated extent mft records (this might still +* happen due to the write_inode_now() call). +* +* 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 datasync) +{ +struct inode *vi = filp->f_mapping->host; +struct ntfs_inode *ni =NTFS_I (vi); +struct ntfs_volume *vol = ni->vol; +int err, ret =0 ; +struct inode *parent_vi, *ia_vi; +struct ntfs_attr_search_ctx *ctx; + +ntfs_debug ("Entering for inode 0x%lx." , vi->i_ino); + +if (NVolShutdown (vol)) +return -EIO ; + + err =file_write_and_wait_range (filp, start, end); +if (err) +return err; + +if (!datasync || !NInoNonResident (NTFS_I (vi))) + ret =__ntfs_write_inode (vi,1 ); +write_inode_now (vi, !datasync); + + ctx =ntfs_attr_get_search_ctx (ni,NULL ); +if (!ctx) +return -ENOMEM ; + +mutex_lock_nested (&ni->mrec_lock,NTFS_INODE_MUTEX_NORMAL_2 ); +while (!(err =ntfs_attr_lookup (AT_UNUSED ,NULL ,0 ,0 ,0 ,NULL ,0 , ctx))) { +if (ctx->attr->type ==AT_FILE_NAME ) { +struct file_name_attr *fn = (struct file_name_attr *)((u8 *)ctx->attr + +le16_to_cpu (ctx->attr->data.resident.value_offset)); + + parent_vi =ntfs_iget (vi->i_sb,MREF_LE (fn->parent_directory)); +if (IS_ERR (parent_vi)) +continue ; +mutex_lock_nested (&NTFS_I (parent_vi)->mrec_lock,NTFS_INODE_MUTEX_PARENT_2 ); + ia_vi =ntfs_index_iget (parent_vi,I30 ,4 ); +mutex_unlock (&NTFS_I (parent_vi)->mrec_lock); +if (IS_ERR (ia_vi)) { +iput (parent_vi); +continue ; + } +write_inode_now (ia_vi,1 ); +iput (ia_vi); +write_inode_now (parent_vi,1 ); +iput (parent_vi); + }else if (ctx->attr->non_resident) { +struct inode *attr_vi; +__le16 *name; + + name = (__le16 *)((u8 *)ctx->attr +le16_to_cpu (ctx->attr->name_offset)); +if (ctx->attr->type ==AT_DATA && ctx->attr->name_length ==0 ) +continue ; + + attr_vi =ntfs_attr_iget (vi, ctx->attr->type, + name, ctx->attr->name_length); +if (IS_ERR (attr_vi)) +continue ; +spin_lock (&attr_vi->i_lock); +if (attr_vi->i_state &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); + } + } +mutex_unlock (&ni->mrec_lock); +ntfs_attr_put_search_ctx (ctx); + +write_inode_now (vol->mftbmp_ino,1 ); +down_write (&vol->lcnbmp_lock); +write_inode_now (vol->lcnbmp_ino,1 ); +up_write (&vol->lcnbmp_lock); +write_inode_now (vol->mft_ino,1 ); + +/* +* NOTE: If we were to use mapping->private_list (see ext2 and +* fs/buffer.c) for dirty blocks then we could optimize the below to be +* sync_mapping_buffers(vi->i_mapping). +*/ + err =sync_blockdev (vi->i_sb->s_bdev); +if (unlikely (err && !ret)) + ret = err; +if (likely (!ret)) +ntfs_debug ("Done." ); +else +ntfs_warning (vi->i_sb, +"Failed to f%ssync inode 0x%lx. Error %u." , + datasync ?"data" :"" , vi->i_ino, -ret); +if (!ret) +blkdev_issue_flush (vi->i_sb->s_bdev); +return ret; +} + +/** +* ntfsp_setattr - called from notify_change() when an attribute is being changed +* @idmap: idmap of the mount the inode was found from +* @dentry: dentry whose attributes to change +* @attr: structure describing the attributes and the changes +* +* We have to trap VFS attempts to truncate the file described by @dentry as +* soon as possible, because we do not implement changes in i_size yet. So we +* abort all i_size changes here. +* +* We also abort all changes of user, group, and mode as we do not implement +* the NTFS ACLs yet. +*/ +int ntfsp_setattr (struct mnt_idmap *idmap,struct dentry *dentry, +struct iattr *attr) +{ +struct inode *vi =d_inode (dentry); +int err; +unsigned int ia_valid = attr->ia_valid; +struct ntfs_inode *ni =NTFS_I (vi); +struct ntfs_volume *vol = ni->vol; + +if (NVolShutdown (vol)) +return -EIO ; + + err =setattr_prepare (idmap, dentry, attr); +if (err) + goto out; + +if (!(vol->vol_flags &VOLUME_IS_DIRTY )) +ntfs_set_volume_flags (vol,VOLUME_IS_DIRTY ); + +if (ia_valid &ATTR_SIZE ) { +if (NInoCompressed (ni) ||NInoEncrypted (ni)) { +ntfs_warning (vi->i_sb, +"Changes in inode size are not supported yet for %s files, ignoring." , +NInoCompressed (ni) ?"compressed" :"encrypted" ); + err = -EOPNOTSUPP ; + }else { +loff_t old_size = vi->i_size; + + err =inode_newsize_ok (vi, attr->ia_size); +if (err) + goto out; + +inode_dio_wait (vi); +/* Serialize against page faults */ +if (NInoNonResident (NTFS_I (vi)) && + attr->ia_size < old_size) { + err =iomap_truncate_page (vi, attr->ia_size,NULL , + &ntfs_read_iomap_ops, + &ntfs_iomap_folio_ops,NULL ); +if (err) + goto out; + } + +truncate_setsize (vi, attr->ia_size); + err =ntfs_truncate_vfs (vi, attr->ia_size, old_size); +if (err) { +i_size_write (vi, old_size); + goto out; + } + +if (NInoNonResident (ni) && attr->ia_size > old_size && + old_size %PAGE_SIZE !=0 ) { +loff_t len =min_t (loff_t, +round_up (old_size,PAGE_SIZE ) - old_size, + attr->ia_size - old_size); + err =iomap_zero_range (vi, old_size, len, +NULL , &ntfs_read_iomap_ops, + &ntfs_iomap_folio_ops,NULL ); + } + } +if (ia_valid ==ATTR_SIZE ) + goto out; + ia_valid |=ATTR_MTIME |ATTR_CTIME ; + } + +setattr_copy (idmap, vi, attr); + +if (vol->sb->s_flags &SB_POSIXACL && !S_ISLNK (vi->i_mode)) { + err =posix_acl_chmod (idmap, dentry, vi->i_mode); +if (err) + goto out; + } + +if (0222 & vi->i_mode) + ni->flags &= ~FILE_ATTR_READONLY ; +else + ni->flags |=FILE_ATTR_READONLY ; + +if (ia_valid & (ATTR_UID |ATTR_GID |ATTR_MODE )) { +unsigned int flags =0 ; + +if (ia_valid &ATTR_UID ) + flags |=NTFS_EA_UID ; +if (ia_valid &ATTR_GID ) + flags |=NTFS_EA_GID ; +if (ia_valid &ATTR_MODE ) + flags |=NTFS_EA_MODE ; + +if (S_ISDIR (vi->i_mode)) + vi->i_mode &= ~vol->dmask; +else + vi->i_mode &= ~vol->fmask; + +mutex_lock (&ni->mrec_lock); +ntfs_ea_set_wsl_inode (vi,0 ,NULL , flags); +mutex_unlock (&ni->mrec_lock); + } + +mark_inode_dirty (vi); +out: +return err; +} + +int ntfsp_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); + +generic_fillattr (idmap, request_mask, inode, stat); + + stat->blksize =NTFS_SB (inode->i_sb)->cluster_size; + stat->blocks = (((u64 )NTFS_I (inode)->i_dealloc_clusters << +NTFS_SB (inode->i_sb)->cluster_size_bits) >>9 ) + inode->i_blocks; + stat->result_mask |=STATX_BTIME ; + stat->btime =NTFS_I (inode)->i_crtime; + +return 0 ; +} + +static loff_t ntfs_file_llseek (struct file *file,loff_t offset,int whence) +{ +struct inode *vi = file->f_mapping->host; + +if (whence ==SEEK_DATA || whence ==SEEK_HOLE ) { +struct ntfs_inode *ni =NTFS_I (vi); +struct ntfs_volume *vol = ni->vol; +struct runlist_element *rl; +s64 vcn; +unsigned int vcn_off; +loff_t end_off; +unsigned long flags; +int i; + +inode_lock_shared (vi); + +if (NInoCompressed (ni) ||NInoEncrypted (ni)) + goto error; + +read_lock_irqsave (&ni->size_lock, flags); + end_off = ni->data_size; +read_unlock_irqrestore (&ni->size_lock, flags); + +if (offset <0 || offset >= end_off) + goto error; + +if (!NInoNonResident (ni)) { +if (whence ==SEEK_HOLE ) + offset = end_off; + goto found_no_runlist_lock; + } + + vcn = offset >> vol->cluster_size_bits; + vcn_off = offset & vol->cluster_size_mask; + +down_read (&ni->runlist.lock); + rl = ni->runlist.rl; + i =0 ; + +#ifdef DEBUG +ntfs_debug ("init:" ); +ntfs_debug_dump_runlist (rl); +#endif +while (1 ) { +if (!rl || !NInoFullyMapped (ni) || rl[i].lcn ==LCN_RL_NOT_MAPPED ) { +int ret; + +up_read (&ni->runlist.lock); + ret =ntfs_map_runlist (ni, rl ? rl[i].vcn :0 ); +if (ret) + goto error; +down_read (&ni->runlist.lock); + rl = ni->runlist.rl; +#ifdef DEBUG +ntfs_debug ("mapped:" ); +ntfs_debug_dump_runlist (ni->runlist.rl); +#endif +continue ; + }else if (rl[i].lcn ==LCN_ENOENT ) { +if (whence ==SEEK_DATA ) { +up_read (&ni->runlist.lock); + goto error; + }else { + offset = end_off; + goto found; + } + }else if (rl[i +1 ].vcn > vcn) { +if ((whence ==SEEK_DATA && (rl[i].lcn >=0 || + rl[i].lcn ==LCN_DELALLOC )) || + (whence ==SEEK_HOLE && rl[i].lcn ==LCN_HOLE )) { + offset = (vcn << vol->cluster_size_bits) + vcn_off; +if (offset < ni->data_size) + goto found; + } + vcn = rl[i +1 ].vcn; + vcn_off =0 ; + } + i++; + } +up_read (&ni->runlist.lock); +inode_unlock_shared (vi); +return -EIO ; +found: +up_read (&ni->runlist.lock); +found_no_runlist_lock: +inode_unlock_shared (vi); +return vfs_setpos (file, offset, vi->i_sb->s_maxbytes); +error: +inode_unlock_shared (vi); +return -ENXIO ; + }else { +return generic_file_llseek_size (file, offset, whence, + vi->i_sb->s_maxbytes, +i_size_read (vi)); + } +} + +static 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; +ssize_t ret; + +if (NVolShutdown (NTFS_SB (sb))) +return -EIO ; + +if (NInoCompressed (NTFS_I (vi)) && iocb->ki_flags &IOCB_DIRECT ) +return -EOPNOTSUPP ; + +inode_lock_shared (vi); + +if (iocb->ki_flags &IOCB_DIRECT ) { +size_t count =iov_iter_count (to); + +if ((iocb->ki_pos | count) & (sb->s_blocksize -1 )) { + ret = -EINVAL ; + goto inode_unlock; + } + +file_accessed (iocb->ki_filp); + ret =iomap_dio_rw (iocb, to, &ntfs_read_iomap_ops,NULL ,IOMAP_DIO_PARTIAL , +NULL ,0 ); + }else { + ret =generic_file_read_iter (iocb, to); + } + +inode_unlock: +inode_unlock_shared (vi); + +return ret; +} + +static int ntfs_file_write_dio_end_io (struct kiocb *iocb,ssize_t size, +int error,unsigned int flags) +{ +struct inode *inode =file_inode (iocb->ki_filp); + +if (error) +return error; + +if (size) { +if (i_size_read (inode) < iocb->ki_pos + size) { +i_size_write (inode, iocb->ki_pos + size); +mark_inode_dirty (inode); + } + } + +return 0 ; +} + +static const struct iomap_dio_ops ntfs_write_dio_ops = { + .end_io = ntfs_file_write_dio_end_io, +}; + +static 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; +struct ntfs_inode *ni =NTFS_I (vi); +struct ntfs_volume *vol = ni->vol; +ssize_t ret; +ssize_t count; +loff_t pos; +int err; +loff_t old_data_size, old_init_size; + +if (NVolShutdown (vol)) +return -EIO ; + +if (NInoEncrypted (ni)) { +ntfs_error (vi->i_sb,"Writing for %s files is not supported yet" , +NInoCompressed (ni) ?"Compressed" :"Encrypted" ); +return -EOPNOTSUPP ; + } + +if (NInoCompressed (ni) && iocb->ki_flags &IOCB_DIRECT ) +return -EOPNOTSUPP ; + +if (iocb->ki_flags &IOCB_NOWAIT ) { +if (!inode_trylock (vi)) +return -EAGAIN ; + }else +inode_lock (vi); + + ret =generic_write_checks (iocb, from); +if (ret <=0 ) + goto out_lock; + +if (NInoNonResident (ni) && (iocb->ki_flags &IOCB_DIRECT ) && + ((iocb->ki_pos | ret) & (vi->i_sb->s_blocksize -1 ))) { + ret = -EINVAL ; + goto out_lock; + } + + err =file_modified (iocb->ki_filp); +if (err) { + ret = err; + goto out_lock; + } + +if (!(vol->vol_flags &VOLUME_IS_DIRTY )) +ntfs_set_volume_flags (vol,VOLUME_IS_DIRTY ); + + pos = iocb->ki_pos; + count = ret; + + old_data_size = ni->data_size; + old_init_size = ni->initialized_size; +if (iocb->ki_pos + ret > old_data_size) { +mutex_lock (&ni->mrec_lock); +if (!NInoCompressed (ni) && iocb->ki_pos + ret > ni->allocated_size && + iocb->ki_pos + ret < ni->allocated_size + vol->preallocated_size) + ret =ntfs_attr_expand (ni, iocb->ki_pos + ret, + ni->allocated_size + vol->preallocated_size); +else if (NInoCompressed (ni) && iocb->ki_pos + ret > ni->allocated_size) + ret =ntfs_attr_expand (ni, iocb->ki_pos + ret, +round_up (iocb->ki_pos + ret, ni->itype.compressed.block_size)); +else + ret =ntfs_attr_expand (ni, iocb->ki_pos + ret,0 ); +mutex_unlock (&ni->mrec_lock); +if (ret <0 ) + goto out; + } + +if (NInoNonResident (ni) && iocb->ki_pos + count > old_init_size) { + ret =ntfs_extend_initialized_size (vi, iocb->ki_pos, + iocb->ki_pos + count); +if (ret <0 ) + goto out; + } + +if (NInoNonResident (ni) &&NInoCompressed (ni)) { + ret =ntfs_compress_write (ni, pos, count, from); +if (ret >0 ) + iocb->ki_pos += ret; + goto out; + } + +if (NInoNonResident (ni) && iocb->ki_flags &IOCB_DIRECT ) { + ret =iomap_dio_rw (iocb, from, &ntfs_dio_iomap_ops, + &ntfs_write_dio_ops,0 ,NULL ,0 ); +if (ret == -ENOTBLK ) + ret =0 ; +else if (ret <0 ) + goto out; + +if (iov_iter_count (from)) { +loff_t offset, end; +ssize_t written; +int ret2; + + offset = iocb->ki_pos; + iocb->ki_flags &= ~IOCB_DIRECT ; + written =iomap_file_buffered_write (iocb, from, + &ntfs_write_iomap_ops, &ntfs_iomap_folio_ops, +NULL ); +if (written <0 ) { + err = written; + goto out; + } + + ret += written; + end = iocb->ki_pos + written -1 ; + ret2 =filemap_write_and_wait_range (iocb->ki_filp->f_mapping, + offset, end); +if (ret2) + goto out_err; +if (!ret2) +invalidate_mapping_pages (iocb->ki_filp->f_mapping, + offset >>PAGE_SHIFT , + end >>PAGE_SHIFT ); + } + }else { + ret =iomap_file_buffered_write (iocb, from, &ntfs_write_iomap_ops, + &ntfs_iomap_folio_ops,NULL ); + } +out: +if (ret <0 && ret != -EIOCBQUEUED ) { +out_err: +if (ni->initialized_size != old_init_size) { +mutex_lock (&ni->mrec_lock); +ntfs_attr_set_initialized_size (ni, old_init_size); +mutex_unlock (&ni->mrec_lock); + } +if (ni->data_size != old_data_size) { +truncate_setsize (vi, old_data_size); +ntfs_attr_truncate (ni, old_data_size); + } + } +out_lock: +inode_unlock (vi); +if (ret >0 ) + ret =generic_write_sync (iocb, ret); +return ret; +} + +static vm_fault_t ntfs_filemap_page_mkwrite (struct vm_fault *vmf) +{ +struct inode *inode =file_inode (vmf->vma->vm_file); +vm_fault_t ret; + +if (unlikely (IS_IMMUTABLE (inode))) +return VM_FAULT_SIGBUS ; + +sb_start_pagefault (inode->i_sb); +file_update_time (vmf->vma->vm_file); + + ret =iomap_page_mkwrite (vmf, &ntfs_page_mkwrite_iomap_ops,NULL ); +sb_end_pagefault (inode->i_sb); +return ret; +} + +static const struct vm_operations_struct ntfs_file_vm_ops = { + .fault = filemap_fault, + .map_pages = filemap_map_pages, + .page_mkwrite = ntfs_filemap_page_mkwrite, +}; + +static int ntfs_file_mmap_prepare (struct vm_area_desc *desc) +{ +struct file *file = desc->file; +struct inode *inode =file_inode (file); + +if (NVolShutdown (NTFS_SB (file->f_mapping->host->i_sb))) +return -EIO ; + +if (NInoCompressed (NTFS_I (inode))) +return -EOPNOTSUPP ; + +if (desc->vm_flags &VM_WRITE ) { +struct inode *inode =file_inode (file); +loff_t from, to; +int err; + + from = ((loff_t )desc->pgoff <<PAGE_SHIFT ); + to =min_t (loff_t,i_size_read (inode), + from + desc->end - desc->start); + +if (NTFS_I (inode)->initialized_size < to) { + err =ntfs_extend_initialized_size (inode, to, to); +if (err) +return err; + } + } + + +file_accessed (file); + desc->vm_ops = &ntfs_file_vm_ops; +return 0 ; +} + +static int ntfs_fiemap (struct inode *inode,struct fiemap_extent_info *fieinfo, +u64 start,u64 len) +{ +return iomap_fiemap (inode, fieinfo, start, len, &ntfs_read_iomap_ops); +} + +static const char *ntfs_get_link (struct dentry *dentry,struct inode *inode, +struct delayed_call *done) +{ +if (!NTFS_I (inode)->target) +return ERR_PTR (-EINVAL ); + +return NTFS_I (inode)->target; +} + +static 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))) +return -EIO ; + +return filemap_splice_read (in, ppos, pipe, len, flags); +} + +static int ntfs_ioctl_shutdown (struct super_block *sb,unsigned long arg) +{ +u32 flags; + +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ; + +if (get_user (flags, (__u32 __user *)arg)) +return -EFAULT ; + +return ntfs_force_shutdown (sb, flags); +} + +static int ntfs_ioctl_get_volume_label (struct file *filp,unsigned long arg) +{ +struct ntfs_volume *vol =NTFS_SB (file_inode (filp)->i_sb); +char __user *buf = (char __user *)arg; + +if (!vol->volume_label) { +if (copy_to_user (buf,"" ,1 )) +return -EFAULT ; + }else if (copy_to_user (buf, vol->volume_label, +MIN (FSLABEL_MAX ,strlen (vol->volume_label) +1 ))) +return -EFAULT ; +return 0 ; +} + +static int ntfs_ioctl_set_volume_label (struct file *filp,unsigned long arg) +{ +struct ntfs_volume *vol =NTFS_SB (file_inode (filp)->i_sb); +char *label; +int ret; + +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ; + + label =strndup_user ((const char __user *)arg,FSLABEL_MAX ); +if (IS_ERR (label)) +return PTR_ERR (label); + + ret =mnt_want_write_file (filp); +if (ret) + goto out; + + ret =ntfs_write_volume_label (vol, label); +mnt_drop_write_file (filp); +out: +kfree (label); +return ret; +} + +static int ntfs_ioctl_fitrim (struct ntfs_volume *vol,unsigned long arg) +{ +struct fstrim_range __user *user_range; +struct fstrim_range range; +struct block_device *dev; +int err; + +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ; + + dev = vol->sb->s_bdev; +if (!bdev_max_discard_sectors (dev)) +return -EOPNOTSUPP ; + + user_range = (struct fstrim_range __user *)arg; +if (copy_from_user (&range, user_range,sizeof (range))) +return -EFAULT ; + +if (range.len ==0 ) +return -EINVAL ; + +if (range.len < vol->cluster_size) +return -EINVAL ; + + range.minlen =max_t (u32, range.minlen,bdev_discard_granularity (dev)); + + err =ntfsp_trim_fs (vol, &range); +if (err <0 ) +return err; + +if (copy_to_user (user_range, &range,sizeof (range))) +return -EFAULT ; + +return 0 ; +} + +long ntfsp_ioctl (struct file *filp,unsigned int cmd,unsigned long arg) +{ +switch (cmd) { +case NTFS_IOC_SHUTDOWN : +return ntfs_ioctl_shutdown (file_inode (filp)->i_sb, arg); +case FS_IOC_GETFSLABEL : +return ntfs_ioctl_get_volume_label (filp, arg); +case FS_IOC_SETFSLABEL : +return ntfs_ioctl_set_volume_label (filp, arg); +case FITRIM : +return ntfs_ioctl_fitrim (NTFS_SB (file_inode (filp)->i_sb), arg); +default : +return -ENOTTY ; + } +} + +#ifdef CONFIG_COMPAT +long ntfsp_compat_ioctl (struct file *filp,unsigned int cmd, +unsigned long arg) +{ +return ntfsp_ioctl (filp, cmd, (unsigned long )compat_ptr (arg)); +} +#endif + +static 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_volume *vol = ni->vol; +int err =0 ; +loff_t end_offset = offset + len; +loff_t old_size, new_size; +s64 start_vcn, end_vcn; +bool map_locked = false; + +if (!S_ISREG (vi->i_mode)) +return -EOPNOTSUPP ; + +if (mode & ~(FALLOC_FL_KEEP_SIZE |FALLOC_FL_INSERT_RANGE | +FALLOC_FL_PUNCH_HOLE |FALLOC_FL_COLLAPSE_RANGE )) +return -EOPNOTSUPP ; + +if (!NVolFreeClusterKnown (vol)) +wait_event (vol->free_waitq,NVolFreeClusterKnown (vol)); + +if ((ni->vol->mft_zone_end - ni->vol->mft_zone_start) ==0 ) +return -ENOSPC ; + +if (NInoNonResident (ni) && !NInoFullyMapped (ni)) { +down_write (&ni->runlist.lock); + err =ntfs_attr_map_whole_runlist (ni); +up_write (&ni->runlist.lock); +if (err) +return err; + } + +if (!(vol->vol_flags &VOLUME_IS_DIRTY )) { + err =ntfs_set_volume_flags (vol,VOLUME_IS_DIRTY ); +if (err) +return err; + } + + old_size =i_size_read (vi); + new_size =max_t (loff_t, old_size, end_offset); + start_vcn = offset >> vol->cluster_size_bits; + end_vcn = ((end_offset -1 ) >> vol->cluster_size_bits) +1 ; + +inode_lock (vi); +if (NInoCompressed (ni) ||NInoEncrypted (ni)) { + err = -EOPNOTSUPP ; + goto out; + } + +inode_dio_wait (vi); +if (mode & (FALLOC_FL_PUNCH_HOLE |FALLOC_FL_COLLAPSE_RANGE | +FALLOC_FL_INSERT_RANGE )) { +filemap_invalidate_lock (vi->i_mapping); + map_locked = true; + } + +if (mode &FALLOC_FL_INSERT_RANGE ) { +loff_t offset_down =round_down (offset, + max_t(unsigned long, vol->cluster_size, PAGE_SIZE)); +loff_t alloc_size; + +if (NVolDisableSparse (vol)) { + err = -EOPNOTSUPP ; + goto out; + } + +if ((offset & vol->cluster_size_mask) || + (len & vol->cluster_size_mask) || + offset >= ni->allocated_size) { + err = -EINVAL ; + goto out; + } + + new_size = old_size + + ((end_vcn - start_vcn) << vol->cluster_size_bits); + alloc_size = ni->allocated_size + + ((end_vcn - start_vcn) << vol->cluster_size_bits); +if (alloc_size <0 ) { + err = -EFBIG ; + goto out; + } + err =inode_newsize_ok (vi, alloc_size); +if (err) + goto out; + + err =filemap_write_and_wait_range (vi->i_mapping, + offset_down,LLONG_MAX ); +if (err) + goto out; + +truncate_pagecache (vi, offset_down); + +mutex_lock_nested (&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); +if (err) + goto out; + }else if (mode &FALLOC_FL_COLLAPSE_RANGE ) { +loff_t offset_down =round_down (offset, + max_t(unsigned long, vol->cluster_size, PAGE_SIZE)); + +if ((offset & vol->cluster_size_mask) || + (len & vol->cluster_size_mask) || + offset >= ni->allocated_size) { + err = -EINVAL ; + goto out; + } + +if ((end_vcn << vol->cluster_size_bits) > ni->allocated_size) + end_vcn =DIV_ROUND_UP (ni->allocated_size -1 , + vol->cluster_size) +1 ; + new_size = old_size - + ((end_vcn - start_vcn) << vol->cluster_size_bits); +if (new_size <0 ) + new_size =0 ; + err =filemap_write_and_wait_range (vi->i_mapping, + offset_down,LLONG_MAX ); +if (err) + goto out; + +truncate_pagecache (vi, offset_down); + +mutex_lock_nested (&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); +if (err) + goto out; + }else if (mode &FALLOC_FL_PUNCH_HOLE ) { + loff_t offset_down = round_down(offset, max_t(unsigned int, + vol->cluster_size,PAGE_SIZE )); + +if (NVolDisableSparse (vol)) { + err = -EOPNOTSUPP ; + goto out; + } + +if (!(mode &FALLOC_FL_KEEP_SIZE )) { + err = -EINVAL ; + goto out; + } + +if (offset >= ni->data_size) + goto out; + +if (offset + len > ni->data_size) { + end_offset = ni->data_size; + end_vcn = ((end_offset -1 ) >> vol->cluster_size_bits) +1 ; + } + + err =filemap_write_and_wait_range (vi->i_mapping, offset_down,LLONG_MAX ); +if (err) + goto out; +truncate_pagecache (vi, offset_down); + +if (offset & vol->cluster_size_mask) { +loff_t to; + + to =min_t (loff_t, (start_vcn +1 ) << vol->cluster_size_bits, + end_offset); + err =iomap_zero_range (vi, offset, to - offset,NULL , + &ntfs_read_iomap_ops, + &ntfs_iomap_folio_ops,NULL ); +if (err <0 || (end_vcn - start_vcn) ==1 ) + goto out; + start_vcn++; + } +if (end_offset & vol->cluster_size_mask) { +loff_t from; + + from = (end_vcn -1 ) << vol->cluster_size_bits; + err =iomap_zero_range (vi, from, end_offset - from,NULL , + &ntfs_read_iomap_ops, + &ntfs_iomap_folio_ops,NULL ); +if (err <0 || (end_vcn - start_vcn) ==1 ) + goto out; + end_vcn--; + } + +mutex_lock_nested (&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); +if (err) + goto out; + }else if (mode ==0 || mode ==FALLOC_FL_KEEP_SIZE ) { +s64 need_space; + + err =inode_newsize_ok (vi, new_size); +if (err) + goto out; + + need_space = ni->allocated_size << vol->cluster_size_bits; +if (need_space > start_vcn) + need_space = end_vcn - need_space; +else + need_space = end_vcn - start_vcn; +if (need_space >0 && + need_space > (atomic64_read (&vol->free_clusters) - +atomic64_read (&vol->dirty_clusters))) { + err = -ENOSPC ; + goto out; + } + + err =ntfs_attr_fallocate (ni, offset, len, + mode &FALLOC_FL_KEEP_SIZE ? true : false); +if (err) + goto out; + } + +/* inode->i_blocks is already updated in ntfs_attr_update_mapping_pairs */ +if (!(mode &FALLOC_FL_KEEP_SIZE ) && new_size != old_size) +i_size_write (vi, ni->data_size); + +out: +if (map_locked) +filemap_invalidate_unlock (vi->i_mapping); +if (!err) { +if (mode ==0 &&NInoNonResident (ni) && + offset > old_size && old_size %PAGE_SIZE !=0 ) { +loff_t len =min_t (loff_t, +round_up (old_size,PAGE_SIZE ) - old_size, + offset - old_size); + err =iomap_zero_range (vi, old_size, len,NULL , + &ntfs_read_iomap_ops, + &ntfs_iomap_folio_ops,NULL ); + } +NInoSetFileNameDirty (ni); +inode_set_mtime_to_ts (vi,inode_set_ctime_current (vi)); +mark_inode_dirty (vi); + } + +inode_unlock (vi); +return err; +} + +const struct file_operations ntfs_file_ops = { + .llseek = ntfs_file_llseek, + .read_iter = ntfs_file_read_iter, + .write_iter = ntfs_file_write_iter, + .fsync = ntfs_file_fsync, + .mmap_prepare = ntfs_file_mmap_prepare, + .open = ntfs_file_open, + .release = ntfs_file_release, + .splice_read = ntfs_file_splice_read, + .splice_write = iter_file_splice_write, + .unlocked_ioctl = ntfsp_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = ntfsp_compat_ioctl, +#endif + .fallocate = ntfs_fallocate, +}; + +const struct inode_operations ntfs_file_inode_ops = { + .setattr = ntfsp_setattr, + .getattr = ntfsp_getattr, + .listxattr = ntfsp_listxattr, + .get_acl = ntfsp_get_acl, + .set_acl = ntfsp_set_acl, + .fiemap = ntfs_fiemap, +}; + +const struct inode_operations ntfs_symlink_inode_operations = { + .get_link = ntfs_get_link, + .setattr = ntfsp_setattr, + .listxattr = ntfsp_listxattr, +}; + +const struct inode_operations ntfsp_special_inode_operations = { + .setattr = ntfsp_setattr, + .getattr = ntfsp_getattr, + .listxattr = ntfsp_listxattr, + .get_acl = ntfsp_get_acl, + .set_acl = ntfsp_set_acl, +}; + +const struct file_operations ntfs_empty_file_ops = {}; + +const struct inode_operations ntfs_empty_inode_ops = {};
Signature · 1 line
2.25.1
PATCH v2 6/11 ntfsplus: add iomap and address space operations
In reply to Namjae JeonThis adds the implementation of iomap and address space operations for ntfsplus.
--- fs/ntfsplus/aops.c | 617 ++++++++++++++++++++++++++++++++++ fs/ntfsplus/ntfs_iomap.c | 700 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 1317 insertions(+) create mode 100644 fs/ntfsplus/aops.c create mode 100644 fs/ntfsplus/ntfs_iomap.c
fs/ntfsplus/aops.c new file +617 -0
@@ -0,0 +1,617 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** +* NTFS kernel address space operations and page cache handling. +* +* Copyright (c) 2001-2014 Anton Altaparmakov and Tuxera Inc. +* Copyright (c) 2002 Richard Russon +* Copyright (c) 2025 LG Electronics Co., Ltd. +*/ + +#include <linux/writeback.h> +#include <linux/mpage.h> +#include <linux/uio.h> + +#include "aops.h" +#include "attrib.h" +#include "mft.h" +#include "ntfs.h" +#include "misc.h" +#include "ntfs_iomap.h" + +static s64 ntfs_convert_page_index_into_lcn (struct ntfs_volume *vol,struct ntfs_inode *ni, +unsigned long page_index) +{ +sector_t iblock; +s64 vcn; +s64 lcn; +unsigned char blocksize_bits = vol->sb->s_blocksize_bits; + + iblock = (s64 )page_index << (PAGE_SHIFT - blocksize_bits); + vcn = (s64 )iblock << blocksize_bits >> vol->cluster_size_bits; + +down_read (&ni->runlist.lock); + lcn =ntfs_attr_vcn_to_lcn_nolock (ni, vcn, false); +up_read (&ni->runlist.lock); + +return lcn; +} + +struct bio *ntfs_setup_bio (struct ntfs_volume *vol,blk_opf_t opf,s64 lcn, +unsigned int pg_ofs) +{ +struct bio *bio; + + bio =bio_alloc (vol->sb->s_bdev,1 , opf,GFP_NOIO ); +if (!bio) +return NULL ; + bio->bi_iter.bi_sector = ((lcn << vol->cluster_size_bits) + pg_ofs) >> + vol->sb->s_blocksize_bits; + +return bio; +} + +/** +* ntfs_read_folio - fill a @folio of a @file with data from the device +* @file: open file to which the folio @folio belongs or NULL +* @folio: page cache folio to fill with data +* +* For non-resident attributes, ntfs_read_folio() fills the @folio of the open +* file @file by calling the ntfs version of the generic block_read_full_folio() +* function, which in turn creates and reads in the buffers associated with +* the folio asynchronously. +* +* For resident attributes, OTOH, ntfs_read_folio() fills @folio by copying the +* data from the mft record (which at this stage is most likely in memory) and +* fills the remainder with zeroes. Thus, in this case, I/O is synchronous, as +* even if the mft record is not cached at this point in time, we need to wait +* for it to be read in before we can do the copy. +* +* Return 0 on success and -errno on error. +*/ +static int ntfs_read_folio (struct file *file,struct folio *folio) +{ +loff_t i_size; +struct inode *vi; +struct ntfs_inode *ni; + + vi = folio->mapping->host; + i_size =i_size_read (vi); +/* Is the page fully outside i_size? (truncate in progress) */ +if (unlikely (folio->index >= (i_size +PAGE_SIZE -1 ) >> +PAGE_SHIFT )) { +folio_zero_segment (folio,0 ,PAGE_SIZE ); +ntfs_debug ("Read outside i_size - truncated?" ); +folio_mark_uptodate (folio); +folio_unlock (folio); +return 0 ; + } +/* +* This can potentially happen because we clear PageUptodate() during +* ntfs_writepage() of MstProtected() attributes. +*/ +if (folio_test_uptodate (folio)) { +folio_unlock (folio); +return 0 ; + } + ni =NTFS_I (vi); + +/* +* Only $DATA attributes can be encrypted and only unnamed $DATA +* attributes can be compressed. Index root can have the flags set but +* this means to create compressed/encrypted files, not that the +* attribute is compressed/encrypted. Note we need to check for +* AT_INDEX_ALLOCATION since this is the type of both directory and +* index inodes. +*/ +if (ni->type !=AT_INDEX_ALLOCATION ) { +/* If attribute is encrypted, deny access, just like NT4. */ +if (NInoEncrypted (ni)) { +folio_unlock (folio); +return -EACCES ; + } +/* Compressed data streams are handled in compress.c. */ +if (NInoNonResident (ni) &&NInoCompressed (ni)) +return ntfs_read_compressed_block (folio); + } + +return iomap_read_folio (folio, &ntfs_read_iomap_ops); +} + +static int ntfs_write_mft_block (struct ntfs_inode *ni,struct folio *folio, +struct writeback_control *wbc) +{ +struct inode *vi =VFS_I (ni); +struct ntfs_volume *vol = ni->vol; +u8 *kaddr; +struct ntfs_inode *locked_nis[PAGE_SIZE /NTFS_BLOCK_SIZE ]; +int nr_locked_nis =0 , err =0 , mft_ofs, prev_mft_ofs; +struct bio *bio =NULL ; +unsigned long mft_no; +struct ntfs_inode *tni; +s64 lcn; +s64 vcn = (s64 )folio->index <<PAGE_SHIFT >> vol->cluster_size_bits; +s64 end_vcn = ni->allocated_size >> vol->cluster_size_bits; +unsigned int folio_sz; +struct runlist_element *rl; + +ntfs_debug ("Entering for inode 0x%lx, attribute type 0x%x, folio index 0x%lx." , + vi->i_ino, ni->type, folio->index); + + lcn =ntfs_convert_page_index_into_lcn (vol, ni, folio->index); +if (lcn <=LCN_HOLE ) { +folio_start_writeback (folio); +folio_unlock (folio); +folio_end_writeback (folio); +return -EIO ; + } + +/* Map folio so we can access its contents. */ + kaddr =kmap_local_folio (folio,0 ); +/* Clear the page uptodate flag whilst the mst fixups are applied. */ +folio_clear_uptodate (folio); + +for (mft_ofs =0 ; mft_ofs <PAGE_SIZE && vcn < end_vcn; + mft_ofs += vol->mft_record_size) { +/* Get the mft record number. */ + mft_no = (((s64 )folio->index <<PAGE_SHIFT ) + mft_ofs) >> + vol->mft_record_size_bits; + vcn = mft_no << vol->mft_record_size_bits >> vol->cluster_size_bits; +/* Check whether to write this mft record. */ + tni =NULL ; +if (ntfs_may_write_mft_record (vol, mft_no, + (struct mft_record *)(kaddr + mft_ofs), &tni)) { +unsigned int mft_record_off =0 ; +s64 vcn_off = vcn; + +/* +* The record should be written. If a locked ntfs +* inode was returned, add it to the array of locked +* ntfs inodes. +*/ +if (tni) + locked_nis[nr_locked_nis++] = tni; + +if (bio && (mft_ofs != prev_mft_ofs + vol->mft_record_size)) { +flush_bio: +flush_dcache_folio (folio); +submit_bio_wait (bio); +bio_put (bio); + bio =NULL ; + } + +if (vol->cluster_size <folio_size (folio)) { +down_write (&ni->runlist.lock); + rl =ntfs_attr_vcn_to_rl (ni, vcn_off, &lcn); +up_write (&ni->runlist.lock); +if (IS_ERR (rl) || lcn <0 ) { + err = -EIO ; + goto unm_done; + } + +if (bio && + (bio_end_sector (bio) >> (vol->cluster_size_bits -9 )) != + lcn) { +flush_dcache_folio (folio); +submit_bio_wait (bio); +bio_put (bio); + bio =NULL ; + } + } + +if (!bio) { +unsigned int off; + + off = ((mft_no << vol->mft_record_size_bits) + + mft_record_off) & vol->cluster_size_mask; + + bio =ntfs_setup_bio (vol,REQ_OP_WRITE , lcn, off); +if (!bio) { + err = -ENOMEM ; + goto unm_done; + } + } + +if (vol->cluster_size ==NTFS_BLOCK_SIZE && + (mft_record_off || + rl->length - (vcn_off - rl->vcn) ==1 || + mft_ofs +NTFS_BLOCK_SIZE >=PAGE_SIZE )) + folio_sz =NTFS_BLOCK_SIZE ; +else + folio_sz = vol->mft_record_size; +if (!bio_add_folio (bio, folio, folio_sz, + mft_ofs + mft_record_off)) { + err = -EIO ; +bio_put (bio); + goto unm_done; + } + mft_record_off += folio_sz; + +if (mft_record_off != vol->mft_record_size) { + vcn_off++; + goto flush_bio; + } + prev_mft_ofs = mft_ofs; + +if (mft_no < vol->mftmirr_size) +ntfs_sync_mft_mirror (vol, mft_no, + (struct mft_record *)(kaddr + mft_ofs)); + } + + } + +if (bio) { +flush_dcache_folio (folio); +submit_bio_wait (bio); +bio_put (bio); + } +flush_dcache_folio (folio); +unm_done: +folio_mark_uptodate (folio); +kunmap_local (kaddr); + +folio_start_writeback (folio); +folio_unlock (folio); +folio_end_writeback (folio); + +/* Unlock any locked inodes. */ +while (nr_locked_nis-- >0 ) { +struct ntfs_inode *base_tni; + + tni = locked_nis[nr_locked_nis]; +mutex_unlock (&tni->mrec_lock); + +/* Get the base inode. */ +mutex_lock (&tni->extent_lock); +if (tni->nr_extents >=0 ) + base_tni = tni; +else + base_tni = tni->ext.base_ntfs_ino; +mutex_unlock (&tni->extent_lock); +ntfs_debug ("Unlocking %s inode 0x%lx." , + tni == base_tni ?"base" :"extent" , + tni->mft_no); +atomic_dec (&tni->count); +iput (VFS_I (base_tni)); + } + +if (unlikely (err && err != -ENOMEM )) +NVolSetErrors (vol); +if (likely (!err)) +ntfs_debug ("Done." ); +return err; +} + +/** +* ntfs_bmap - map logical file block to physical device block +* @mapping: address space mapping to which the block to be mapped belongs +* @block: logical block to map to its physical device block +* +* For regular, non-resident files (i.e. not compressed and not encrypted), map +* the logical @block belonging to the file described by the address space +* mapping @mapping to its physical device block. +* +* The size of the block is equal to the @s_blocksize field of the super block +* of the mounted file system which is guaranteed to be smaller than or equal +* to the cluster size thus the block is guaranteed to fit entirely inside the +* cluster which means we do not need to care how many contiguous bytes are +* available after the beginning of the block. +* +* Return the physical device block if the mapping succeeded or 0 if the block +* is sparse or there was an error. +* +* Note: This is a problem if someone tries to run bmap() on $Boot system file +* as that really is in block zero but there is nothing we can do. bmap() is +* just broken in that respect (just like it cannot distinguish sparse from +* not available or error). +*/ +static sector_t ntfs_bmap (struct address_space *mapping,sector_t block) +{ +s64 ofs, size; +loff_t i_size; +s64 lcn; +unsigned long blocksize, flags; +struct ntfs_inode *ni =NTFS_I (mapping->host); +struct ntfs_volume *vol = ni->vol; +unsigned int delta; +unsigned char blocksize_bits, cluster_size_shift; + +ntfs_debug ("Entering for mft_no 0x%lx, logical block 0x%llx." , + ni->mft_no, (unsigned long long )block); +if (ni->type !=AT_DATA || !NInoNonResident (ni) ||NInoEncrypted (ni)) { +ntfs_error (vol->sb,"BMAP does not make sense for %s attributes, returning 0." , + (ni->type !=AT_DATA ) ?"non-data" : + (!NInoNonResident (ni) ?"resident" : +"encrypted" )); +return 0 ; + } +/* None of these can happen. */ + blocksize = vol->sb->s_blocksize; + blocksize_bits = vol->sb->s_blocksize_bits; + ofs = (s64 )block << blocksize_bits; +read_lock_irqsave (&ni->size_lock, flags); + size = ni->initialized_size; + i_size =i_size_read (VFS_I (ni)); +read_unlock_irqrestore (&ni->size_lock, flags); +/* +* If the offset is outside the initialized size or the block straddles +* the initialized size then pretend it is a hole unless the +* initialized size equals the file size. +*/ +if (unlikely (ofs >= size || (ofs + blocksize > size && size < i_size))) + goto hole; + cluster_size_shift = vol->cluster_size_bits; +down_read (&ni->runlist.lock); + lcn =ntfs_attr_vcn_to_lcn_nolock (ni, ofs >> cluster_size_shift, false); +up_read (&ni->runlist.lock); +if (unlikely (lcn <LCN_HOLE )) { +/* +* Step down to an integer to avoid gcc doing a long long +* comparision in the switch when we know @lcn is between +* LCN_HOLE and LCN_EIO (i.e. -1 to -5). +* +* Otherwise older gcc (at least on some architectures) will +* try to use __cmpdi2() which is of course not available in +* the kernel. +*/ +switch ((int )lcn) { +case LCN_ENOENT : +/* +* If the offset is out of bounds then pretend it is a +* hole. +*/ + goto hole; +case LCN_ENOMEM : +ntfs_error (vol->sb, +"Not enough memory to complete mapping for inode 0x%lx. Returning 0." , + ni->mft_no); +break ; +default : +ntfs_error (vol->sb, +"Failed to complete mapping for inode 0x%lx. Run chkdsk. Returning 0." , + ni->mft_no); +break ; + } +return 0 ; + } +if (lcn <0 ) { +/* It is a hole. */ +hole: +ntfs_debug ("Done (returning hole)." ); +return 0 ; + } +/* +* The block is really allocated and fullfils all our criteria. +* Convert the cluster to units of block size and return the result. +*/ + delta = ofs & vol->cluster_size_mask; +if (unlikely (sizeof (block) <sizeof (lcn))) { + block = lcn = ((lcn << cluster_size_shift) + delta) >> + blocksize_bits; +/* If the block number was truncated return 0. */ +if (unlikely (block != lcn)) { +ntfs_error (vol->sb, +"Physical block 0x%llx is too large to be returned, returning 0." , + (long long )lcn); +return 0 ; + } + }else + block = ((lcn << cluster_size_shift) + delta) >> + blocksize_bits; +ntfs_debug ("Done (returning block 0x%llx)." , (unsigned long long )lcn); +return block; +} + +static void ntfs_readahead (struct readahead_control *rac) +{ +struct address_space *mapping = rac->mapping; +struct inode *inode = mapping->host; +struct ntfs_inode *ni =NTFS_I (inode); + +if (!NInoNonResident (ni) ||NInoCompressed (ni)) { +/* No readahead for resident and compressed. */ +return ; + } + +if (NInoMstProtected (ni) && + (ni->mft_no ==FILE_MFT || ni->mft_no == FILE_MFTMirr)) +return ; + +iomap_readahead (rac, &ntfs_read_iomap_ops); +} + +static int ntfs_mft_writepage (struct folio *folio,struct writeback_control *wbc) +{ +struct address_space *mapping = folio->mapping; +struct inode *vi = mapping->host; +struct ntfs_inode *ni =NTFS_I (vi); +loff_t i_size; +int ret; + + i_size =i_size_read (vi); + +/* We have to zero every time due to mmap-at-end-of-file. */ +if (folio->index >= (i_size >>PAGE_SHIFT )) { +/* The page straddles i_size. */ +unsigned int ofs = i_size & ~PAGE_MASK ; + +folio_zero_segment (folio, ofs,PAGE_SIZE ); + } + + ret =ntfs_write_mft_block (ni, folio, wbc); +mapping_set_error (mapping, ret); +return ret; +} + +static int ntfs_writepages (struct address_space *mapping, +struct writeback_control *wbc) +{ +struct inode *inode = mapping->host; +struct ntfs_inode *ni =NTFS_I (inode); +struct iomap_writepage_ctx wpc = { + .inode = mapping->host, + .wbc = wbc, + .ops = &ntfs_writeback_ops, + }; + +if (NVolShutdown (ni->vol)) +return -EIO ; + +if (!NInoNonResident (ni)) +return 0 ; + +if (NInoMstProtected (ni) && ni->mft_no ==FILE_MFT ) { +struct folio *folio =NULL ; +int error; + +while ((folio =writeback_iter (mapping, wbc, folio, &error))) + error =ntfs_mft_writepage (folio, wbc); +return error; + } + +/* If file is encrypted, deny access, just like NT4. */ +if (NInoEncrypted (ni)) { +ntfs_debug ("Denying write access to encrypted file." ); +return -EACCES ; + } + +return iomap_writepages (&wpc); +} + +static int ntfs_swap_activate (struct swap_info_struct *sis, +struct file *swap_file,sector_t *span) +{ +return iomap_swapfile_activate (sis, swap_file, span, + &ntfs_read_iomap_ops); +} + +/** +* ntfs_normal_aops - address space operations for normal inodes and attributes +* +* Note these are not used for compressed or mst protected inodes and +* attributes. +*/ +const struct address_space_operations ntfs_normal_aops = { + .read_folio = ntfs_read_folio, + .readahead = ntfs_readahead, + .writepages = ntfs_writepages, + .direct_IO = noop_direct_IO, + .dirty_folio = iomap_dirty_folio, + .bmap = ntfs_bmap, + .migrate_folio = filemap_migrate_folio, + .is_partially_uptodate = iomap_is_partially_uptodate, + .error_remove_folio = generic_error_remove_folio, + .release_folio = iomap_release_folio, + .invalidate_folio = iomap_invalidate_folio, + .swap_activate = ntfs_swap_activate, +}; + +/** +* ntfs_compressed_aops - address space operations for compressed inodes +*/ +const struct address_space_operations ntfs_compressed_aops = { + .read_folio = ntfs_read_folio, + .direct_IO = noop_direct_IO, + .writepages = ntfs_writepages, + .dirty_folio = iomap_dirty_folio, + .migrate_folio = filemap_migrate_folio, + .is_partially_uptodate = iomap_is_partially_uptodate, + .error_remove_folio = generic_error_remove_folio, + .release_folio = iomap_release_folio, + .invalidate_folio = iomap_invalidate_folio, +}; + +/** +* ntfs_mst_aops - general address space operations for mst protecteed inodes +* and attributes +*/ +const struct address_space_operations ntfs_mst_aops = { + .read_folio = ntfs_read_folio,/* Fill page with data. */ + .readahead = ntfs_readahead, + .writepages = ntfs_writepages,/* Write dirty page to disk. */ + .dirty_folio = iomap_dirty_folio, + .migrate_folio = filemap_migrate_folio, + .is_partially_uptodate = iomap_is_partially_uptodate, + .error_remove_folio = generic_error_remove_folio, + .release_folio = iomap_release_folio, + .invalidate_folio = iomap_invalidate_folio, +}; + +void mark_ntfs_record_dirty (struct folio *folio) +{ +iomap_dirty_folio (folio->mapping, folio); +} + +int ntfs_dev_read (struct super_block *sb,void *buf,loff_t start,loff_t size) +{ +pgoff_t idx, idx_end; +loff_t offset, end = start + size; +u32 from, to, buf_off =0 ; +struct folio *folio; +char *kaddr; + + idx = start >>PAGE_SHIFT ; + idx_end = end >>PAGE_SHIFT ; + from = start & ~PAGE_MASK ; + +if (idx == idx_end) + idx_end++; + +for (; idx < idx_end; idx++, from =0 ) { + folio =ntfs_read_mapping_folio (sb->s_bdev->bd_mapping, idx); +if (IS_ERR (folio)) { +ntfs_error (sb,"Unable to read %ld page" , idx); +return PTR_ERR (folio); + } + + kaddr =kmap_local_folio (folio,0 ); + offset = (loff_t )idx <<PAGE_SHIFT ; + to =min_t (u32, end - offset,PAGE_SIZE ); + +memcpy (buf + buf_off, kaddr + from, to); + buf_off += to; +kunmap_local (kaddr); +folio_put (folio); + } + +return 0 ; +} + +int ntfs_dev_write (struct super_block *sb,void *buf,loff_t start, +loff_t size,bool wait) +{ +pgoff_t idx, idx_end; +loff_t offset, end = start + size; +u32 from, to, buf_off =0 ; +struct folio *folio; +char *kaddr; + + idx = start >>PAGE_SHIFT ; + idx_end = end >>PAGE_SHIFT ; + from = start & ~PAGE_MASK ; + +if (idx == idx_end) + idx_end++; + +for (; idx < idx_end; idx++, from =0 ) { + folio =ntfs_read_mapping_folio (sb->s_bdev->bd_mapping, idx); +if (IS_ERR (folio)) { +ntfs_error (sb,"Unable to read %ld page" , idx); +return PTR_ERR (folio); + } + + kaddr =kmap_local_folio (folio,0 ); + offset = (loff_t )idx <<PAGE_SHIFT ; + to =min_t (u32, end - offset,PAGE_SIZE ); + +memcpy (kaddr + from, buf + buf_off, to); + buf_off += to; +kunmap_local (kaddr); +folio_mark_uptodate (folio); +folio_mark_dirty (folio); +if (wait) +folio_wait_stable (folio); +folio_put (folio); + } + +return 0 ; +}
fs/ntfsplus/ntfs_iomap.c new file +700 -0
700 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v2 7/11 ntfsplus: add attrib operatrions
In reply to Namjae JeonThis adds the implementation of attrib operatrions for ntfsplus.
--- fs/ntfsplus/attrib.c | 5377 ++++++++++++++++++++++++++++++++++++++++ fs/ntfsplus/attrlist.c | 285 +++ fs/ntfsplus/compress.c | 1564 ++++++++++++ 3 files changed, 7226 insertions(+) create mode 100644 fs/ntfsplus/attrib.c create mode 100644 fs/ntfsplus/attrlist.c create mode 100644 fs/ntfsplus/compress.c
fs/ntfsplus/attrib.c new file +5377 -0
@@ -0,0 +1,5377 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** +* NTFS attribute operations. Part of the Linux-NTFS project. +* +* Copyright (c) 2001-2012 Anton Altaparmakov and Tuxera Inc. +* Copyright (c) 2002 Richard Russon +* Copyright (c) 2025 LG Electronics Co., Ltd. +* +* Part of this file is based on code from the NTFS-3G project. +* and is copyrighted by the respective authors below: +* Copyright (c) 2000-2010 Anton Altaparmakov +* Copyright (c) 2002-2005 Richard Russon +* Copyright (c) 2002-2008 Szabolcs Szakacsits +* Copyright (c) 2004-2007 Yura Pakhuchiy +* Copyright (c) 2007-2021 Jean-Pierre Andre +* Copyright (c) 2010 Erik Larsson +*/ + +#include <linux/writeback.h> +#include <linux/iomap.h> + +#include "attrib.h" +#include "attrlist.h" +#include "lcnalloc.h" +#include "misc.h" +#include "mft.h" +#include "ntfs.h" +#include "aops.h" +#include "ntfs_iomap.h" + +__le16 AT_UNNAMED [] = {cpu_to_le16 ('\0' ) }; + +/** +* ntfs_map_runlist_nolock - map (a part of) a runlist of an ntfs inode +* @ni: ntfs inode for which to map (part of) a runlist +* @vcn: map runlist part containing this vcn +* @ctx: active attribute search context if present or NULL if not +* +* Map the part of a runlist containing the @vcn of the ntfs inode @ni. +* +* If @ctx is specified, it is an active search context of @ni and its base mft +* record. This is needed when ntfs_map_runlist_nolock() encounters unmapped +* runlist fragments and allows their mapping. If you do not have the mft +* record mapped, you can specify @ctx as NULL and ntfs_map_runlist_nolock() +* will perform the necessary mapping and unmapping. +* +* Note, ntfs_map_runlist_nolock() saves the state of @ctx on entry and +* restores it before returning. Thus, @ctx will be left pointing to the same +* attribute on return as on entry. However, the actual pointers in @ctx may +* point to different memory locations on return, so you must remember to reset +* any cached pointers from the @ctx, i.e. after the call to +* ntfs_map_runlist_nolock(), you will probably want to do: +* m = ctx->mrec; +* a = ctx->attr; +* Assuming you cache ctx->attr in a variable @a of type attr_record * and that +* you cache ctx->mrec in a variable @m of type struct mft_record *. +*/ +int ntfs_map_runlist_nolock (struct ntfs_inode *ni,s64 vcn,struct ntfs_attr_search_ctx *ctx) +{ +s64 end_vcn; +unsigned long flags; +struct ntfs_inode *base_ni; +struct mft_record *m; +struct attr_record *a; +struct runlist_element *rl; +struct folio *put_this_folio =NULL ; +int err =0 ; +bool ctx_is_temporary, ctx_needs_reset; +struct ntfs_attr_search_ctx old_ctx = {NULL , }; +size_t new_rl_count; + +ntfs_debug ("Mapping runlist part containing vcn 0x%llx." , + (unsigned long long )vcn); +if (!NInoAttr (ni)) + base_ni = ni; +else + base_ni = ni->ext.base_ntfs_ino; +if (!ctx) { + ctx_is_temporary = ctx_needs_reset = true; + m =map_mft_record (base_ni); +if (IS_ERR (m)) +return PTR_ERR (m); + ctx =ntfs_attr_get_search_ctx (base_ni, m); +if (unlikely (!ctx)) { + err = -ENOMEM ; + goto err_out; + } + }else { +s64 allocated_size_vcn; + +WARN_ON (IS_ERR (ctx->mrec)); + a = ctx->attr; + ctx_is_temporary = false; +if (!a->non_resident) { + err = -EIO ; + goto err_out; + } + end_vcn =le64_to_cpu (a->data.non_resident.highest_vcn); +read_lock_irqsave (&ni->size_lock, flags); + allocated_size_vcn = ni->allocated_size >> + ni->vol->cluster_size_bits; +read_unlock_irqrestore (&ni->size_lock, flags); +if (!a->data.non_resident.lowest_vcn && end_vcn <=0 ) + end_vcn = allocated_size_vcn -1 ; +/* +* If we already have the attribute extent containing @vcn in +* @ctx, no need to look it up again. We slightly cheat in +* that if vcn exceeds the allocated size, we will refuse to +* map the runlist below, so there is definitely no need to get +* the right attribute extent. +*/ +if (vcn >= allocated_size_vcn || (a->type == ni->type && + a->name_length == ni->name_len && + !memcmp ((u8 *)a +le16_to_cpu (a->name_offset), + ni->name, ni->name_len) && +le64_to_cpu (a->data.non_resident.lowest_vcn) + <= vcn && end_vcn >= vcn)) + ctx_needs_reset = false; +else { +/* Save the old search context. */ + old_ctx = *ctx; +/* +* If the currently mapped (extent) inode is not the +* base inode we will unmap it when we reinitialize the +* search context which means we need to get a +* reference to the page containing the mapped mft +* record so we do not accidentally drop changes to the +* mft record when it has not been marked dirty yet. +*/ +if (old_ctx.base_ntfs_ino && old_ctx.ntfs_ino != + old_ctx.base_ntfs_ino) { + put_this_folio = old_ctx.ntfs_ino->folio; +folio_get (put_this_folio); + } +/* +* Reinitialize the search context so we can lookup the +* needed attribute extent. +*/ +ntfs_attr_reinit_search_ctx (ctx); + ctx_needs_reset = true; + } + } +if (ctx_needs_reset) { + err =ntfs_attr_lookup (ni->type, ni->name, ni->name_len, +CASE_SENSITIVE , vcn,NULL ,0 , ctx); +if (unlikely (err)) { +if (err == -ENOENT ) + err = -EIO ; + goto err_out; + } +WARN_ON (!ctx->attr->non_resident); + } + a = ctx->attr; +/* +* Only decompress the mapping pairs if @vcn is inside it. Otherwise +* we get into problems when we try to map an out of bounds vcn because +* we then try to map the already mapped runlist fragment and +* ntfs_mapping_pairs_decompress() fails. +*/ + end_vcn =le64_to_cpu (a->data.non_resident.highest_vcn) +1 ; +if (unlikely (vcn && vcn >= end_vcn)) { + err = -ENOENT ; + goto err_out; + } + rl =ntfs_mapping_pairs_decompress (ni->vol, a, &ni->runlist, &new_rl_count); +if (IS_ERR (rl)) + err =PTR_ERR (rl); +else { + ni->runlist.rl = rl; + ni->runlist.count = new_rl_count; + } +err_out: +if (ctx_is_temporary) { +if (likely (ctx)) +ntfs_attr_put_search_ctx (ctx); +unmap_mft_record (base_ni); + }else if (ctx_needs_reset) { +/* +* If there is no attribute list, restoring the search context +* is accomplished simply by copying the saved context back over +* the caller supplied context. If there is an attribute list, +* things are more complicated as we need to deal with mapping +* of mft records and resulting potential changes in pointers. +*/ +if (NInoAttrList (base_ni)) { +/* +* If the currently mapped (extent) inode is not the +* one we had before, we need to unmap it and map the +* old one. +*/ +if (ctx->ntfs_ino != old_ctx.ntfs_ino) { +/* +* If the currently mapped inode is not the +* base inode, unmap it. +*/ +if (ctx->base_ntfs_ino && ctx->ntfs_ino != + ctx->base_ntfs_ino) { +unmap_extent_mft_record (ctx->ntfs_ino); + ctx->mrec = ctx->base_mrec; +WARN_ON (!ctx->mrec); + } +/* +* If the old mapped inode is not the base +* inode, map it. +*/ +if (old_ctx.base_ntfs_ino && + old_ctx.ntfs_ino != old_ctx.base_ntfs_ino) { +retry_map: + ctx->mrec =map_mft_record (old_ctx.ntfs_ino); +/* +* Something bad has happened. If out +* of memory retry till it succeeds. +* Any other errors are fatal and we +* return the error code in ctx->mrec. +* Let the caller deal with it... We +* just need to fudge things so the +* caller can reinit and/or put the +* search context safely. +*/ +if (IS_ERR (ctx->mrec)) { +if (PTR_ERR (ctx->mrec) == -ENOMEM ) { +schedule (); + goto retry_map; + }else + old_ctx.ntfs_ino = + old_ctx.base_ntfs_ino; + } + } + } +/* Update the changed pointers in the saved context. */ +if (ctx->mrec != old_ctx.mrec) { +if (!IS_ERR (ctx->mrec)) + old_ctx.attr = (struct attr_record *)( + (u8 *)ctx->mrec + + ((u8 *)old_ctx.attr - + (u8 *)old_ctx.mrec)); + old_ctx.mrec = ctx->mrec; + } + } +/* Restore the search context to the saved one. */ + *ctx = old_ctx; +/* +* We drop the reference on the page we took earlier. In the +* case that IS_ERR(ctx->mrec) is true this means we might lose +* some changes to the mft record that had been made between +* the last time it was marked dirty/written out and now. This +* at this stage is not a problem as the mapping error is fatal +* enough that the mft record cannot be written out anyway and +* the caller is very likely to shutdown the whole inode +* immediately and mark the volume dirty for chkdsk to pick up +* the pieces anyway. +*/ +if (put_this_folio) +folio_put (put_this_folio); + } +return err; +} + +/** +* ntfs_map_runlist - map (a part of) a runlist of an ntfs inode +* @ni: ntfs inode for which to map (part of) a runlist +* @vcn: map runlist part containing this vcn +* +* Map the part of a runlist containing the @vcn of the ntfs inode @ni. +*/ +int ntfs_map_runlist (struct ntfs_inode *ni,s64 vcn) +{ +int err =0 ; + +down_write (&ni->runlist.lock); +/* Make sure someone else didn't do the work while we were sleeping. */ +if (likely (ntfs_rl_vcn_to_lcn (ni->runlist.rl, vcn) <= +LCN_RL_NOT_MAPPED )) + err =ntfs_map_runlist_nolock (ni, vcn,NULL ); +up_write (&ni->runlist.lock); +return err; +} + +struct runlist_element *ntfs_attr_vcn_to_rl (struct ntfs_inode *ni,s64 vcn,s64 *lcn) +{ +struct runlist_element *rl; +int err; +bool is_retry = false; + + rl = ni->runlist.rl; +if (!rl) { + err =ntfs_attr_map_whole_runlist (ni); +if (err) +return ERR_PTR (-ENOENT ); + rl = ni->runlist.rl; + } + +remap_rl: +/* Seek to element containing target vcn. */ +while (rl->length && rl[1 ].vcn <= vcn) + rl++; + *lcn =ntfs_rl_vcn_to_lcn (rl, vcn); + +if (*lcn <=LCN_RL_NOT_MAPPED && is_retry == false) { + is_retry = true; +if (!ntfs_map_runlist_nolock (ni, vcn,NULL )) { + rl = ni->runlist.rl; + goto remap_rl; + } + } + +return rl; +} + +/** +* ntfs_attr_vcn_to_lcn_nolock - convert a vcn into a lcn given an ntfs inode +* @ni: ntfs inode of the attribute whose runlist to search +* @vcn: vcn to convert +* @write_locked: true if the runlist is locked for writing +* +* Find the virtual cluster number @vcn in the runlist of the ntfs attribute +* described by the ntfs inode @ni and return the corresponding logical cluster +* number (lcn). +* +* If the @vcn is not mapped yet, the attempt is made to map the attribute +* extent containing the @vcn and the vcn to lcn conversion is retried. +* +* If @write_locked is true the caller has locked the runlist for writing and +* if false for reading. +* +* Since lcns must be >= 0, we use negative return codes with special meaning: +* +* Return code Meaning / Description +* ========================================== +* LCN_HOLE Hole / not allocated on disk. +* LCN_ENOENT There is no such vcn in the runlist, i.e. @vcn is out of bounds. +* LCN_ENOMEM Not enough memory to map runlist. +* LCN_EIO Critical error (runlist/file is corrupt, i/o error, etc). +* +* Locking: - The runlist must be locked on entry and is left locked on return. +* - If @write_locked is 'false', i.e. the runlist is locked for reading, +* the lock may be dropped inside the function so you cannot rely on +* the runlist still being the same when this function returns. +*/ +s64 ntfs_attr_vcn_to_lcn_nolock (struct ntfs_inode *ni,const s64 vcn, +const bool write_locked) +{ +s64 lcn; +unsigned long flags; +bool is_retry = false; + +ntfs_debug ("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked." , + ni->mft_no, (unsigned long long )vcn, + write_locked ?"write" :"read" ); +if (!ni->runlist.rl) { +read_lock_irqsave (&ni->size_lock, flags); +if (!ni->allocated_size) { +read_unlock_irqrestore (&ni->size_lock, flags); +return LCN_ENOENT ; + } +read_unlock_irqrestore (&ni->size_lock, flags); + } +retry_remap: +/* Convert vcn to lcn. If that fails map the runlist and retry once. */ + lcn =ntfs_rl_vcn_to_lcn (ni->runlist.rl, vcn); +if (likely (lcn >=LCN_HOLE )) { +ntfs_debug ("Done, lcn 0x%llx." , (long long )lcn); +return lcn; + } +if (lcn !=LCN_RL_NOT_MAPPED ) { +if (lcn !=LCN_ENOENT ) + lcn =LCN_EIO ; + }else if (!is_retry) { +int err; + +if (!write_locked) { +up_read (&ni->runlist.lock); +down_write (&ni->runlist.lock); +if (unlikely (ntfs_rl_vcn_to_lcn (ni->runlist.rl, vcn) != +LCN_RL_NOT_MAPPED )) { +up_write (&ni->runlist.lock); +down_read (&ni->runlist.lock); + goto retry_remap; + } + } + err =ntfs_map_runlist_nolock (ni, vcn,NULL ); +if (!write_locked) { +up_write (&ni->runlist.lock); +down_read (&ni->runlist.lock); + } +if (likely (!err)) { + is_retry = true; + goto retry_remap; + } +if (err == -ENOENT ) + lcn =LCN_ENOENT ; +else if (err == -ENOMEM ) + lcn =LCN_ENOMEM ; +else + lcn =LCN_EIO ; + } +if (lcn !=LCN_ENOENT ) +ntfs_error (ni->vol->sb,"Failed with error code %lli." , + (long long )lcn); +return lcn; +} + +struct runlist_element *__ntfs_attr_find_vcn_nolock (struct runlist *runlist,const s64 vcn) +{ +size_t lower_idx, upper_idx, idx; +struct runlist_element *run; + +if (runlist->count <=1 ) +return ERR_PTR (-ENOENT ); + + run = &runlist->rl[0 ]; +if (vcn < run->vcn) +return ERR_PTR (-ENOENT ); +else if (vcn < run->vcn + run->length) +return run; + + run = &runlist->rl[runlist->count -2 ]; +if (vcn >= run->vcn && vcn < run->vcn + run->length) +return run; +if (vcn >= run->vcn + run->length) +return ERR_PTR (-ENOENT ); + + lower_idx =1 ; + upper_idx = runlist->count -2 ; + +while (lower_idx <= upper_idx) { + idx = (lower_idx + upper_idx) >>1 ; + run = &runlist->rl[idx]; + +if (vcn < run->vcn) + upper_idx = idx -1 ; +else if (vcn >= run->vcn + run->length) + lower_idx = idx +1 ; +else +return run; + } + +return ERR_PTR (-ENOENT ); +} + +/** +* ntfs_attr_find_vcn_nolock - find a vcn in the runlist of an ntfs inode +* @ni: ntfs inode describing the runlist to search +* @vcn: vcn to find +* @ctx: active attribute search context if present or NULL if not +* +* Find the virtual cluster number @vcn in the runlist described by the ntfs +* inode @ni and return the address of the runlist element containing the @vcn. +* +* If the @vcn is not mapped yet, the attempt is made to map the attribute +* extent containing the @vcn and the vcn to lcn conversion is retried. +* +* If @ctx is specified, it is an active search context of @ni and its base mft +* record. This is needed when ntfs_attr_find_vcn_nolock() encounters unmapped +* runlist fragments and allows their mapping. If you do not have the mft +* record mapped, you can specify @ctx as NULL and ntfs_attr_find_vcn_nolock() +* will perform the necessary mapping and unmapping. +* +* Note, ntfs_attr_find_vcn_nolock() saves the state of @ctx on entry and +* restores it before returning. Thus, @ctx will be left pointing to the same +* attribute on return as on entry. However, the actual pointers in @ctx may +* point to different memory locations on return, so you must remember to reset +* any cached pointers from the @ctx, i.e. after the call to +* ntfs_attr_find_vcn_nolock(), you will probably want to do: +* m = ctx->mrec; +* a = ctx->attr; +* Assuming you cache ctx->attr in a variable @a of type attr_record * and that +* you cache ctx->mrec in a variable @m of type struct mft_record *. +* Note you need to distinguish between the lcn of the returned runlist element +* being >= 0 and LCN_HOLE. In the later case you have to return zeroes on +* read and allocate clusters on write. +*/ +struct runlist_element *ntfs_attr_find_vcn_nolock (struct ntfs_inode *ni,const s64 vcn, +struct ntfs_attr_search_ctx *ctx) +{ +unsigned long flags; +struct runlist_element *rl; +int err =0 ; +bool is_retry = false; + +ntfs_debug ("Entering for i_ino 0x%lx, vcn 0x%llx, with%s ctx." , + ni->mft_no, (unsigned long long )vcn, ctx ?"" :"out" ); +if (!ni->runlist.rl) { +read_lock_irqsave (&ni->size_lock, flags); +if (!ni->allocated_size) { +read_unlock_irqrestore (&ni->size_lock, flags); +return ERR_PTR (-ENOENT ); + } +read_unlock_irqrestore (&ni->size_lock, flags); + } + +retry_remap: + rl = ni->runlist.rl; +if (likely (rl && vcn >= rl[0 ].vcn)) { + rl =__ntfs_attr_find_vcn_nolock (&ni->runlist, vcn); +if (IS_ERR (rl)) + err =PTR_ERR (rl); +else if (rl->lcn >=LCN_HOLE ) +return rl; +else if (rl->lcn <=LCN_ENOENT ) + err = -EIO ; + } +if (!err && !is_retry) { +/* +* If the search context is invalid we cannot map the unmapped +* region. +*/ +if (ctx &&IS_ERR (ctx->mrec)) + err =PTR_ERR (ctx->mrec); +else { +/* +* The @vcn is in an unmapped region, map the runlist +* and retry. +*/ + err =ntfs_map_runlist_nolock (ni, vcn, ctx); +if (likely (!err)) { + is_retry = true; + goto retry_remap; + } + } +if (err == -EINVAL ) + err = -EIO ; + }else if (!err) + err = -EIO ; +if (err != -ENOENT ) +ntfs_error (ni->vol->sb,"Failed with error code %i." , err); +return ERR_PTR (err); +} + +/** +* ntfs_attr_find - find (next) attribute in mft record +* @type: attribute type to find +* @name: attribute name to find (optional, i.e. NULL means don't care) +* @name_len: attribute name length (only needed if @name present) +* @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present) +* @val: attribute value to find (optional, resident attributes only) +* @val_len: attribute value length +* @ctx: search context with mft record and attribute to search from +* +* You should not need to call this function directly. Use ntfs_attr_lookup() +* instead. +* +* ntfs_attr_find() takes a search context @ctx as parameter and searches the +* mft record specified by @ctx->mrec, beginning at @ctx->attr, for an +* attribute of @type, optionally @name and @val. +* +* If the attribute is found, ntfs_attr_find() returns 0 and @ctx->attr will +* point to the found attribute. +* +* If the attribute is not found, ntfs_attr_find() returns -ENOENT and +* @ctx->attr will point to the attribute before which the attribute being +* searched for would need to be inserted if such an action were to be desired. +* +* On actual error, ntfs_attr_find() returns -EIO. In this case @ctx->attr is +* undefined and in particular do not rely on it not changing. +* +* If @ctx->is_first is 'true', the search begins with @ctx->attr itself. If it +* is 'false', the search begins after @ctx->attr. +* +* If @ic is IGNORE_CASE, the @name comparisson is not case sensitive and +* @ctx->ntfs_ino must be set to the ntfs inode to which the mft record +* @ctx->mrec belongs. This is so we can get at the ntfs volume and hence at +* the upcase table. If @ic is CASE_SENSITIVE, the comparison is case +* sensitive. When @name is present, @name_len is the @name length in Unicode +* characters. +* +* If @name is not present (NULL), we assume that the unnamed attribute is +* being searched for. +* +* Finally, the resident attribute value @val is looked for, if present. If +* @val is not present (NULL), @val_len is ignored. +* +* ntfs_attr_find() only searches the specified mft record and it ignores the +* presence of an attribute list attribute (unless it is the one being searched +* for, obviously). If you need to take attribute lists into consideration, +* use ntfs_attr_lookup() instead (see below). This also means that you cannot +* use ntfs_attr_find() to search for extent records of non-resident +* attributes, as extents with lowest_vcn != 0 are usually described by the +* attribute list attribute only. - Note that it is possible that the first +* extent is only in the attribute list while the last extent is in the base +* mft record, so do not rely on being able to find the first extent in the +* base mft record. +* +* Warning: Never use @val when looking for attribute types which can be +* non-resident as this most likely will result in a crash! +*/ +static int ntfs_attr_find (const __le32 type,const __le16 *name, +const u32 name_len,const u32 ic, +const u8 *val,const u32 val_len,struct ntfs_attr_search_ctx *ctx) +{ +struct attr_record *a; +struct ntfs_volume *vol = ctx->ntfs_ino->vol; +__le16 *upcase = vol->upcase; +u32 upcase_len = vol->upcase_len; +unsigned int space; + +/* +* Iterate over attributes in mft record starting at @ctx->attr, or the +* attribute following that, if @ctx->is_first is 'true'. +*/ +if (ctx->is_first) { + a = ctx->attr; + ctx->is_first = false; + }else + a = (struct attr_record *)((u8 *)ctx->attr + +le32_to_cpu (ctx->attr->length)); +for (;; a = (struct attr_record *)((u8 *)a +le32_to_cpu (a->length))) { +if ((u8 *)a < (u8 *)ctx->mrec || (u8 *)a > (u8 *)ctx->mrec + +le32_to_cpu (ctx->mrec->bytes_allocated)) +break ; + + space =le32_to_cpu (ctx->mrec->bytes_in_use) - ((u8 *)a - (u8 *)ctx->mrec); + if ((space < offsetof(struct attr_record, data.resident.reserved) + 1 || + space < le32_to_cpu(a->length)) && (space < 4 || a->type != AT_END)) +break ; + + ctx->attr = a; +if (((type !=AT_UNUSED ) && (le32_to_cpu (a->type) >le32_to_cpu (type))) || + a->type ==AT_END ) +return -ENOENT ; +if (unlikely (!a->length)) +break ; +if (type ==AT_UNUSED ) +return 0 ; +if (a->type != type) +continue ; +/* +* If @name is present, compare the two names. If @name is +* missing, assume we want an unnamed attribute. +*/ +if (!name || name ==AT_UNNAMED ) { +/* The search failed if the found attribute is named. */ +if (a->name_length) +return -ENOENT ; + }else { +if (a->name_length && ((le16_to_cpu (a->name_offset) + + a->name_length *sizeof (__le16)) > +le32_to_cpu (a->length))) { +ntfs_error (vol->sb,"Corrupt attribute name in MFT record %lld\n" , + (long long )ctx->ntfs_ino->mft_no); +break ; + } + +if (!ntfs_are_names_equal (name, name_len, + (__le16 *)((u8 *)a +le16_to_cpu (a->name_offset)), + a->name_length, ic, upcase, upcase_len)) { + registerint rc; + + rc =ntfs_collate_names (name, name_len, + (__le16 *)((u8 *)a +le16_to_cpu (a->name_offset)), + a->name_length,1 ,IGNORE_CASE , + upcase, upcase_len); +/* +* If @name collates before a->name, there is no +* matching attribute. +*/ +if (rc ==-1 ) +return -ENOENT ; +/* If the strings are not equal, continue search. */ +if (rc) +continue ; + rc =ntfs_collate_names (name, name_len, + (__le16 *)((u8 *)a +le16_to_cpu (a->name_offset)), + a->name_length,1 ,CASE_SENSITIVE , + upcase, upcase_len); +if (rc ==-1 ) +return -ENOENT ; +if (rc) +continue ; + } + } +/* +* The names match or @name not present and attribute is +* unnamed. If no @val specified, we have found the attribute +* and are done. +*/ +if (!val) +return 0 ; +/* @val is present; compare values. */ +else { + registerint rc; + + rc =memcmp (val, (u8 *)a +le16_to_cpu ( + a->data.resident.value_offset), +min_t (u32, val_len,le32_to_cpu ( + a->data.resident.value_length))); +/* +* If @val collates before the current attribute's +* value, there is no matching attribute. +*/ +if (!rc) { + registeru32 avl; + + avl =le32_to_cpu (a->data.resident.value_length); +if (val_len == avl) +return 0 ; +if (val_len < avl) +return -ENOENT ; + }else if (rc <0 ) +return -ENOENT ; + } + } +ntfs_error (vol->sb,"Inode is corrupt. Run chkdsk." ); +NVolSetErrors (vol); +return -EIO ; +} + +void ntfs_attr_name_free (unsigned char **name) +{ +if (*name) { +ntfs_free (*name); + *name =NULL ; + } +} + +char *ntfs_attr_name_get (const struct ntfs_volume *vol,const __le16 *uname, +const int uname_len) +{ +unsigned char *name =NULL ; +int name_len; + + name_len =ntfs_ucstonls (vol, uname, uname_len, &name,0 ); +if (name_len <0 ) { +ntfs_error (vol->sb,"ntfs_ucstonls error" ); +/* This function when returns -1, memory for name might +* be allocated. So lets free this memory. +*/ +ntfs_attr_name_free (&name); +return NULL ; + + }else if (name_len >0 ) +return name; + +ntfs_attr_name_free (&name); +return NULL ; +} + +int load_attribute_list (struct ntfs_inode *base_ni,u8 *al_start,const s64 size) +{ +struct inode *attr_vi =NULL ; +u8 *al; +struct attr_list_entry *ale; + +if (!al_start || size <=0 ) +return -EINVAL ; + + attr_vi =ntfs_attr_iget (VFS_I (base_ni),AT_ATTRIBUTE_LIST ,AT_UNNAMED ,0 ); +if (IS_ERR (attr_vi)) { +ntfs_error (base_ni->vol->sb, +"Failed to open an inode for Attribute list, mft = %ld" , + base_ni->mft_no); +return PTR_ERR (attr_vi); + } + +if (ntfs_inode_attr_pread (attr_vi,0 , size, al_start) != size) { +iput (attr_vi); +ntfs_error (base_ni->vol->sb, +"Failed to read attribute list, mft = %ld" , + base_ni->mft_no); +return -EIO ; + } +iput (attr_vi); + +for (al = al_start; al < al_start + size; al +=le16_to_cpu (ale->length)) { + ale = (struct attr_list_entry *)al; +if (ale->name_offset !=sizeof (struct attr_list_entry )) +break ; +if (le16_to_cpu (ale->length) <= ale->name_offset + ale->name_length || + al +le16_to_cpu (ale->length) > al_start + size) +break ; +if (ale->type ==AT_UNUSED ) +break ; +if (MSEQNO_LE (ale->mft_reference) ==0 ) +break ; + } +if (al != al_start + size) { +ntfs_error (base_ni->vol->sb,"Corrupt attribute list, mft = %ld" , + base_ni->mft_no); +return -EIO ; + } +return 0 ; +} + +/** +* ntfs_external_attr_find - find an attribute in the attribute list of an inode +* @type: attribute type to find +* @name: attribute name to find (optional, i.e. NULL means don't care) +* @name_len: attribute name length (only needed if @name present) +* @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present) +* @lowest_vcn: lowest vcn to find (optional, non-resident attributes only) +* @val: attribute value to find (optional, resident attributes only) +* @val_len: attribute value length +* @ctx: search context with mft record and attribute to search from +* +* You should not need to call this function directly. Use ntfs_attr_lookup() +* instead. +* +* Find an attribute by searching the attribute list for the corresponding +* attribute list entry. Having found the entry, map the mft record if the +* attribute is in a different mft record/inode, ntfs_attr_find() the attribute +* in there and return it. +* +* On first search @ctx->ntfs_ino must be the base mft record and @ctx must +* have been obtained from a call to ntfs_attr_get_search_ctx(). On subsequent +* calls @ctx->ntfs_ino can be any extent inode, too (@ctx->base_ntfs_ino is +* then the base inode). +* +* After finishing with the attribute/mft record you need to call +* ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any +* mapped inodes, etc). +* +* If the attribute is found, ntfs_external_attr_find() returns 0 and +* @ctx->attr will point to the found attribute. @ctx->mrec will point to the +* mft record in which @ctx->attr is located and @ctx->al_entry will point to +* the attribute list entry for the attribute. +* +* If the attribute is not found, ntfs_external_attr_find() returns -ENOENT and +* @ctx->attr will point to the attribute in the base mft record before which +* the attribute being searched for would need to be inserted if such an action +* were to be desired. @ctx->mrec will point to the mft record in which +* @ctx->attr is located and @ctx->al_entry will point to the attribute list +* entry of the attribute before which the attribute being searched for would +* need to be inserted if such an action were to be desired. +* +* Thus to insert the not found attribute, one wants to add the attribute to +* @ctx->mrec (the base mft record) and if there is not enough space, the +* attribute should be placed in a newly allocated extent mft record. The +* attribute list entry for the inserted attribute should be inserted in the +* attribute list attribute at @ctx->al_entry. +* +* On actual error, ntfs_external_attr_find() returns -EIO. In this case +* @ctx->attr is undefined and in particular do not rely on it not changing. +*/ +static int ntfs_external_attr_find (const __le32 type, +const __le16 *name,const u32 name_len, +const u32 ic,const s64 lowest_vcn, +const u8 *val,const u32 val_len,struct ntfs_attr_search_ctx *ctx) +{ +struct ntfs_inode *base_ni, *ni; +struct ntfs_volume *vol; +struct attr_list_entry *al_entry, *next_al_entry; +u8 *al_start, *al_end; +struct attr_record *a; +__le16 *al_name; +u32 al_name_len; +bool is_first_search = false; +int err =0 ; +static const char *es =" Unmount and run chkdsk." ; + + ni = ctx->ntfs_ino; + base_ni = ctx->base_ntfs_ino; +ntfs_debug ("Entering for inode 0x%lx, type 0x%x." , ni->mft_no, type); +if (!base_ni) { +/* First call happens with the base mft record. */ + base_ni = ctx->base_ntfs_ino = ctx->ntfs_ino; + ctx->base_mrec = ctx->mrec; + ctx->mapped_base_mrec = ctx->mapped_mrec; + } +if (ni == base_ni) + ctx->base_attr = ctx->attr; +if (type ==AT_END ) + goto not_found; + vol = base_ni->vol; + al_start = base_ni->attr_list; + al_end = al_start + base_ni->attr_list_size; +if (!ctx->al_entry) { + ctx->al_entry = (struct attr_list_entry *)al_start; + is_first_search = true; + } +/* +* Iterate over entries in attribute list starting at @ctx->al_entry, +* or the entry following that, if @ctx->is_first is 'true'. +*/ +if (ctx->is_first) { + al_entry = ctx->al_entry; + ctx->is_first = false; +/* +* If an enumeration and the first attribute is higher than +* the attribute list itself, need to return the attribute list +* attribute. +*/ +if ((type ==AT_UNUSED ) && is_first_search && +le32_to_cpu (al_entry->type) > +le32_to_cpu (AT_ATTRIBUTE_LIST )) + goto find_attr_list_attr; + }else { +/* Check for small entry */ +if (((al_end - (u8 *)ctx->al_entry) < + (long )offsetof(struct attr_list_entry , name)) || + (le16_to_cpu (ctx->al_entry->length) &7 ) || + (le16_to_cpu (ctx->al_entry->length) < offsetof(struct attr_list_entry , name))) + goto corrupt; + + al_entry = (struct attr_list_entry *)((u8 *)ctx->al_entry + +le16_to_cpu (ctx->al_entry->length)); + +if ((u8 *)al_entry == al_end) + goto not_found; + +/* Preliminary check for small entry */ +if ((al_end - (u8 *)al_entry) < + (long )offsetof(struct attr_list_entry , name)) + goto corrupt; + +/* +* If this is an enumeration and the attribute list attribute +* is the next one in the enumeration sequence, just return the +* attribute list attribute from the base mft record as it is +* not listed in the attribute list itself. +*/ +if ((type ==AT_UNUSED ) &&le32_to_cpu (ctx->al_entry->type) < +le32_to_cpu (AT_ATTRIBUTE_LIST ) && +le32_to_cpu (al_entry->type) > +le32_to_cpu (AT_ATTRIBUTE_LIST )) { +find_attr_list_attr: + +/* Check for bogus calls. */ +if (name || name_len || val || val_len || lowest_vcn) +return -EINVAL ; + +/* We want the base record. */ +if (ctx->ntfs_ino != base_ni) +unmap_mft_record (ctx->ntfs_ino); + ctx->ntfs_ino = base_ni; + ctx->mapped_mrec = ctx->mapped_base_mrec; + ctx->mrec = ctx->base_mrec; + ctx->is_first = true; + +/* Sanity checks are performed elsewhere. */ + ctx->attr = (struct attr_record *)((u8 *)ctx->mrec + +le16_to_cpu (ctx->mrec->attrs_offset)); + +/* Find the attribute list attribute. */ + err =ntfs_attr_find (AT_ATTRIBUTE_LIST ,NULL ,0 , +IGNORE_CASE ,NULL ,0 , ctx); + +/* +* Setup the search context so the correct +* attribute is returned next time round. +*/ + ctx->al_entry = al_entry; + ctx->is_first = true; + +/* Got it. Done. */ +if (!err) +return 0 ; + +/* Error! If other than not found return it. */ +if (err != -ENOENT ) +return err; + +/* Not found?!? Absurd! */ +ntfs_error (ctx->ntfs_ino->vol->sb,"Attribute list wasn't found" ); +return -EIO ; + } + } +for (;; al_entry = next_al_entry) { +/* Out of bounds check. */ +if ((u8 *)al_entry < base_ni->attr_list || + (u8 *)al_entry > al_end) +break ;/* Inode is corrupt. */ + ctx->al_entry = al_entry; +/* Catch the end of the attribute list. */ +if ((u8 *)al_entry == al_end) + goto not_found; + +if ((((u8 *)al_entry + offsetof(struct attr_list_entry , name)) > al_end) || + ((u8 *)al_entry +le16_to_cpu (al_entry->length) > al_end) || + (le16_to_cpu (al_entry->length) &7 ) || + (le16_to_cpu (al_entry->length) < + offsetof(struct attr_list_entry , name_length)) || + (al_entry->name_length && ((u8 *)al_entry + al_entry->name_offset + + al_entry->name_length *sizeof (__le16)) > al_end)) +break ;/* corrupt */ + + next_al_entry = (struct attr_list_entry *)((u8 *)al_entry + +le16_to_cpu (al_entry->length)); +if (type !=AT_UNUSED ) { +if (le32_to_cpu (al_entry->type) >le32_to_cpu (type)) + goto not_found; +if (type != al_entry->type) +continue ; + } +/* +* If @name is present, compare the two names. If @name is +* missing, assume we want an unnamed attribute. +*/ + al_name_len = al_entry->name_length; + al_name = (__le16 *)((u8 *)al_entry + al_entry->name_offset); + +/* +* If !@type we want the attribute represented by this +* attribute list entry. +*/ +if (type ==AT_UNUSED ) + goto is_enumeration; + +if (!name || name ==AT_UNNAMED ) { +if (al_name_len) + goto not_found; + }else if (!ntfs_are_names_equal (al_name, al_name_len, name, + name_len, ic, vol->upcase, vol->upcase_len)) { + registerint rc; + + rc =ntfs_collate_names (name, name_len, al_name, + al_name_len,1 ,IGNORE_CASE , + vol->upcase, vol->upcase_len); +/* +* If @name collates before al_name, there is no +* matching attribute. +*/ +if (rc ==-1 ) + goto not_found; +/* If the strings are not equal, continue search. */ +if (rc) +continue ; + + rc =ntfs_collate_names (name, name_len, al_name, + al_name_len,1 ,CASE_SENSITIVE , + vol->upcase, vol->upcase_len); +if (rc ==-1 ) + goto not_found; +if (rc) +continue ; + } +/* +* The names match or @name not present and attribute is +* unnamed. Now check @lowest_vcn. Continue search if the +* next attribute list entry still fits @lowest_vcn. Otherwise +* we have reached the right one or the search has failed. +*/ +if (lowest_vcn && (u8 *)next_al_entry >= al_start && + (u8 *)next_al_entry +6 < al_end && + (u8 *)next_al_entry +le16_to_cpu ( + next_al_entry->length) <= al_end && +le64_to_cpu (next_al_entry->lowest_vcn) <= + lowest_vcn && + next_al_entry->type == al_entry->type && + next_al_entry->name_length == al_name_len && +ntfs_are_names_equal ((__le16 *)((u8 *) + next_al_entry + + next_al_entry->name_offset), + next_al_entry->name_length, + al_name, al_name_len,CASE_SENSITIVE , + vol->upcase, vol->upcase_len)) +continue ; + +is_enumeration: +if (MREF_LE (al_entry->mft_reference) == ni->mft_no) { +if (MSEQNO_LE (al_entry->mft_reference) != ni->seq_no) { +ntfs_error (vol->sb, +"Found stale mft reference in attribute list of base inode 0x%lx.%s" , + base_ni->mft_no, es); + err = -EIO ; +break ; + } + }else {/* Mft references do not match. */ +/* If there is a mapped record unmap it first. */ +if (ni != base_ni) +unmap_extent_mft_record (ni); +/* Do we want the base record back? */ +if (MREF_LE (al_entry->mft_reference) == + base_ni->mft_no) { + ni = ctx->ntfs_ino = base_ni; + ctx->mrec = ctx->base_mrec; + ctx->mapped_mrec = ctx->mapped_base_mrec; + }else { +/* We want an extent record. */ + ctx->mrec =map_extent_mft_record (base_ni, +le64_to_cpu ( + al_entry->mft_reference), &ni); +if (IS_ERR (ctx->mrec)) { +ntfs_error (vol->sb, +"Failed to map extent mft record 0x%lx of base inode 0x%lx.%s" , +MREF_LE (al_entry->mft_reference), + base_ni->mft_no, es); + err =PTR_ERR (ctx->mrec); +if (err == -ENOENT ) + err = -EIO ; +/* Cause @ctx to be sanitized below. */ + ni =NULL ; +break ; + } + ctx->ntfs_ino = ni; + ctx->mapped_mrec = true; + + } + } + a = ctx->attr = (struct attr_record *)((u8 *)ctx->mrec + +le16_to_cpu (ctx->mrec->attrs_offset)); +/* +* ctx->vfs_ino, ctx->mrec, and ctx->attr now point to the +* mft record containing the attribute represented by the +* current al_entry. +*/ +/* +* We could call into ntfs_attr_find() to find the right +* attribute in this mft record but this would be less +* efficient and not quite accurate as ntfs_attr_find() ignores +* the attribute instance numbers for example which become +* important when one plays with attribute lists. Also, +* because a proper match has been found in the attribute list +* entry above, the comparison can now be optimized. So it is +* worth re-implementing a simplified ntfs_attr_find() here. +*/ +/* +* Use a manual loop so we can still use break and continue +* with the same meanings as above. +*/ +do_next_attr_loop: +if ((u8 *)a < (u8 *)ctx->mrec || (u8 *)a > (u8 *)ctx->mrec + +le32_to_cpu (ctx->mrec->bytes_allocated)) +break ; +if (a->type ==AT_END ) +continue ; +if (!a->length) +break ; +if (al_entry->instance != a->instance) + goto do_next_attr; +/* +* If the type and/or the name are mismatched between the +* attribute list entry and the attribute record, there is +* corruption so we break and return error EIO. +*/ +if (al_entry->type != a->type) +break ; +if (!ntfs_are_names_equal ((__le16 *)((u8 *)a + +le16_to_cpu (a->name_offset)), a->name_length, + al_name, al_name_len,CASE_SENSITIVE , + vol->upcase, vol->upcase_len)) +break ; + ctx->attr = a; +/* +* If no @val specified or @val specified and it matches, we +* have found it! +*/ +if ((type ==AT_UNUSED ) || !val || (!a->non_resident &&le32_to_cpu ( + a->data.resident.value_length) == val_len && + !memcmp ((u8 *)a + +le16_to_cpu (a->data.resident.value_offset), + val, val_len))) { +ntfs_debug ("Done, found." ); +return 0 ; + } +do_next_attr: +/* Proceed to the next attribute in the current mft record. */ + a = (struct attr_record *)((u8 *)a +le32_to_cpu (a->length)); + goto do_next_attr_loop; + } + +corrupt: +if (ni != base_ni) { +if (ni) +unmap_extent_mft_record (ni); + ctx->ntfs_ino = base_ni; + ctx->mrec = ctx->base_mrec; + ctx->attr = ctx->base_attr; + ctx->mapped_mrec = ctx->mapped_base_mrec; + } + +if (!err) { +ntfs_error (vol->sb, +"Base inode 0x%lx contains corrupt attribute list attribute.%s" , + base_ni->mft_no, es); + err = -EIO ; + } + +if (err != -ENOMEM ) +NVolSetErrors (vol); +return err; +not_found: +/* +* If we were looking for AT_END, we reset the search context @ctx and +* use ntfs_attr_find() to seek to the end of the base mft record. +*/ +if (type ==AT_UNUSED || type ==AT_END ) { +ntfs_attr_reinit_search_ctx (ctx); +return ntfs_attr_find (AT_END , name, name_len, ic, val, val_len, + ctx); + } +/* +* The attribute was not found. Before we return, we want to ensure +* @ctx->mrec and @ctx->attr indicate the position at which the +* attribute should be inserted in the base mft record. Since we also +* want to preserve @ctx->al_entry we cannot reinitialize the search +* context using ntfs_attr_reinit_search_ctx() as this would set +* @ctx->al_entry to NULL. Thus we do the necessary bits manually (see +* ntfs_attr_init_search_ctx() below). Note, we _only_ preserve +* @ctx->al_entry as the remaining fields (base_*) are identical to +* their non base_ counterparts and we cannot set @ctx->base_attr +* correctly yet as we do not know what @ctx->attr will be set to by +* the call to ntfs_attr_find() below. +*/ +if (ni != base_ni) +unmap_extent_mft_record (ni); + ctx->mrec = ctx->base_mrec; + ctx->attr = (struct attr_record *)((u8 *)ctx->mrec + +le16_to_cpu (ctx->mrec->attrs_offset)); + ctx->is_first = true; + ctx->ntfs_ino = base_ni; + ctx->base_ntfs_ino =NULL ; + ctx->base_mrec =NULL ; + ctx->base_attr =NULL ; + ctx->mapped_mrec = ctx->mapped_base_mrec; +/* +* In case there are multiple matches in the base mft record, need to +* keep enumerating until we get an attribute not found response (or +* another error), otherwise we would keep returning the same attribute +* over and over again and all programs using us for enumeration would +* lock up in a tight loop. +*/ +do { + err =ntfs_attr_find (type, name, name_len, ic, val, val_len, + ctx); + }while (!err); +ntfs_debug ("Done, not found." ); +return err; +} + +/** +* ntfs_attr_lookup - find an attribute in an ntfs inode +* @type: attribute type to find +* @name: attribute name to find (optional, i.e. NULL means don't care) +* @name_len: attribute name length (only needed if @name present) +* @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present) +* @lowest_vcn: lowest vcn to find (optional, non-resident attributes only) +* @val: attribute value to find (optional, resident attributes only) +* @val_len: attribute value length +* @ctx: search context with mft record and attribute to search from +* +* Find an attribute in an ntfs inode. On first search @ctx->ntfs_ino must +* be the base mft record and @ctx must have been obtained from a call to +* ntfs_attr_get_search_ctx(). +* +* This function transparently handles attribute lists and @ctx is used to +* continue searches where they were left off at. +* +* After finishing with the attribute/mft record you need to call +* ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any +* mapped inodes, etc). +* +* Return 0 if the search was successful and -errno if not. +* +* When 0, @ctx->attr is the found attribute and it is in mft record +* @ctx->mrec. If an attribute list attribute is present, @ctx->al_entry is +* the attribute list entry of the found attribute. +* +* When -ENOENT, @ctx->attr is the attribute which collates just after the +* attribute being searched for, i.e. if one wants to add the attribute to the +* mft record this is the correct place to insert it into. If an attribute +* list attribute is present, @ctx->al_entry is the attribute list entry which +* collates just after the attribute list entry of the attribute being searched +* for, i.e. if one wants to add the attribute to the mft record this is the +* correct place to insert its attribute list entry into. +*/ +int ntfs_attr_lookup (const __le32 type,const __le16 *name, +const u32 name_len,const u32 ic, +const s64 lowest_vcn,const u8 *val,const u32 val_len, +struct ntfs_attr_search_ctx *ctx) +{ +struct ntfs_inode *base_ni; + +ntfs_debug ("Entering." ); +if (ctx->base_ntfs_ino) + base_ni = ctx->base_ntfs_ino; +else + base_ni = ctx->ntfs_ino; +/* Sanity check, just for debugging really. */ +if (!base_ni || !NInoAttrList (base_ni) || type ==AT_ATTRIBUTE_LIST ) +return ntfs_attr_find (type, name, name_len, ic, val, val_len, + ctx); +return ntfs_external_attr_find (type, name, name_len, ic, lowest_vcn, + val, val_len, ctx); +} + +/** +* ntfs_attr_init_search_ctx - initialize an attribute search context +* @ctx: attribute search context to initialize +* @ni: ntfs inode with which to initialize the search context +* @mrec: mft record with which to initialize the search context +* +* Initialize the attribute search context @ctx with @ni and @mrec. +*/ +static bool ntfs_attr_init_search_ctx (struct ntfs_attr_search_ctx *ctx, +struct ntfs_inode *ni,struct mft_record *mrec) +{ +if (!mrec) { + mrec =map_mft_record (ni); +if (IS_ERR (mrec)) +return false; + ctx->mapped_mrec = true; + }else { + ctx->mapped_mrec = false; + } + + ctx->mrec = mrec; +/* Sanity checks are performed elsewhere. */ + ctx->attr = (struct attr_record *)((u8 *)mrec +le16_to_cpu (mrec->attrs_offset)); + ctx->is_first = true; + ctx->ntfs_ino = ni; + ctx->al_entry =NULL ; + ctx->base_ntfs_ino =NULL ; + ctx->base_mrec =NULL ; + ctx->base_attr =NULL ; + ctx->mapped_base_mrec = false; +return true; +} + +/** +* ntfs_attr_reinit_search_ctx - reinitialize an attribute search context +* @ctx: attribute search context to reinitialize +* +* Reinitialize the attribute search context @ctx, unmapping an associated +* extent mft record if present, and initialize the search context again. +* +* This is used when a search for a new attribute is being started to reset +* the search context to the beginning. +*/ +void ntfs_attr_reinit_search_ctx (struct ntfs_attr_search_ctx *ctx) +{ +bool mapped_mrec; + +if (likely (!ctx->base_ntfs_ino)) { +/* No attribute list. */ + ctx->is_first = true; +/* Sanity checks are performed elsewhere. */ + ctx->attr = (struct attr_record *)((u8 *)ctx->mrec + +le16_to_cpu (ctx->mrec->attrs_offset)); +/* +* This needs resetting due to ntfs_external_attr_find() which +* can leave it set despite having zeroed ctx->base_ntfs_ino. +*/ + ctx->al_entry =NULL ; +return ; + }/* Attribute list. */ +if (ctx->ntfs_ino != ctx->base_ntfs_ino && ctx->ntfs_ino) +unmap_extent_mft_record (ctx->ntfs_ino); + + mapped_mrec = ctx->mapped_base_mrec; +ntfs_attr_init_search_ctx (ctx, ctx->base_ntfs_ino, ctx->base_mrec); + ctx->mapped_mrec = mapped_mrec; +} + +/** +* ntfs_attr_get_search_ctx - allocate/initialize a new attribute search context +* @ni: ntfs inode with which to initialize the search context +* @mrec: mft record with which to initialize the search context +* +* Allocate a new attribute search context, initialize it with @ni and @mrec, +* and return it. Return NULL if allocation failed. +*/ +struct ntfs_attr_search_ctx *ntfs_attr_get_search_ctx (struct ntfs_inode *ni, +struct mft_record *mrec) +{ +struct ntfs_attr_search_ctx *ctx; +bool init; + + ctx =kmem_cache_alloc (ntfs_attr_ctx_cache,GFP_NOFS ); +if (ctx) { + init =ntfs_attr_init_search_ctx (ctx, ni, mrec); +if (init == false) { +kmem_cache_free (ntfs_attr_ctx_cache, ctx); + ctx =NULL ; + } + } + +return ctx; +} + +/** +* ntfs_attr_put_search_ctx - release an attribute search context +* @ctx: attribute search context to free +* +* Release the attribute search context @ctx, unmapping an associated extent +* mft record if present. +*/ +void ntfs_attr_put_search_ctx (struct ntfs_attr_search_ctx *ctx) +{ +if (ctx->mapped_mrec) +unmap_mft_record (ctx->ntfs_ino); + +if (ctx->mapped_base_mrec && ctx->base_ntfs_ino && + ctx->ntfs_ino != ctx->base_ntfs_ino) +unmap_extent_mft_record (ctx->base_ntfs_ino); +kmem_cache_free (ntfs_attr_ctx_cache, ctx); +} + +/** +* ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file +* @vol: ntfs volume to which the attribute belongs +* @type: attribute type which to find +* +* Search for the attribute definition record corresponding to the attribute +* @type in the $AttrDef system file. +* +* Return the attribute type definition record if found and NULL if not found. +*/ +static struct attr_def *ntfs_attr_find_in_attrdef (const struct ntfs_volume *vol, +const __le32 type) +{ +struct attr_def *ad; + +WARN_ON (!type); +for (ad = vol->attrdef; (u8 *)ad - (u8 *)vol->attrdef < + vol->attrdef_size && ad->type; ++ad) { +/* We have not found it yet, carry on searching. */ +if (likely (le32_to_cpu (ad->type) <le32_to_cpu (type))) +continue ; +/* We found the attribute; return it. */ +if (likely (ad->type == type)) +return ad; +/* We have gone too far already. No point in continuing. */ +break ; + } +/* Attribute not found. */ +ntfs_debug ("Attribute type 0x%x not found in $AttrDef." , +le32_to_cpu (type)); +return NULL ; +} + +/** +* ntfs_attr_size_bounds_check - check a size of an attribute type for validity +* @vol: ntfs volume to which the attribute belongs +* @type: attribute type which to check +* @size: size which to check +* +* Check whether the @size in bytes is valid for an attribute of @type on the +* ntfs volume @vol. This information is obtained from $AttrDef system file. +*/ +int ntfs_attr_size_bounds_check (const struct ntfs_volume *vol,const __le32 type, +const s64 size) +{ +struct attr_def *ad; + +if (size <0 ) +return -EINVAL ; + +/* +* $ATTRIBUTE_LIST has a maximum size of 256kiB, but this is not +* listed in $AttrDef. +*/ +if (unlikely (type ==AT_ATTRIBUTE_LIST && size >256 *1024 )) +return -ERANGE ; +/* Get the $AttrDef entry for the attribute @type. */ + ad =ntfs_attr_find_in_attrdef (vol, type); +if (unlikely (!ad)) +return -ENOENT ; +/* Do the bounds check. */ +if (((le64_to_cpu (ad->min_size) >0 ) && + size <le64_to_cpu (ad->min_size)) || + ((le64_to_cpu (ad->max_size) >0 ) && size > +le64_to_cpu (ad->max_size))) +return -ERANGE ; +return 0 ; +} + +/** +* ntfs_attr_can_be_non_resident - check if an attribute can be non-resident +* @vol: ntfs volume to which the attribute belongs +* @type: attribute type which to check +* +* Check whether the attribute of @type on the ntfs volume @vol is allowed to +* be non-resident. This information is obtained from $AttrDef system file. +*/ +static int ntfs_attr_can_be_non_resident (const struct ntfs_volume *vol, +const __le32 type) +{ +struct attr_def *ad; + +/* Find the attribute definition record in $AttrDef. */ + ad =ntfs_attr_find_in_attrdef (vol, type); +if (unlikely (!ad)) +return -ENOENT ; +/* Check the flags and return the result. */ +if (ad->flags &ATTR_DEF_RESIDENT ) +return -EPERM ; +return 0 ; +} + +/** +* ntfs_attr_can_be_resident - check if an attribute can be resident +* @vol: ntfs volume to which the attribute belongs +* @type: attribute type which to check +* +* Check whether the attribute of @type on the ntfs volume @vol is allowed to +* be resident. This information is derived from our ntfs knowledge and may +* not be completely accurate, especially when user defined attributes are +* present. Basically we allow everything to be resident except for index +* allocation and $EA attributes. +* +* Return 0 if the attribute is allowed to be non-resident and -EPERM if not. +* +* Warning: In the system file $MFT the attribute $Bitmap must be non-resident +* otherwise windows will not boot (blue screen of death)! We cannot +* check for this here as we do not know which inode's $Bitmap is +* being asked about so the caller needs to special case this. +*/ +int ntfs_attr_can_be_resident (const struct ntfs_volume *vol,const __le32 type) +{ +if (type ==AT_INDEX_ALLOCATION ) +return -EPERM ; +return 0 ; +} + +/** +* ntfs_attr_record_resize - resize an attribute record +* @m: mft record containing attribute record +* @a: attribute record to resize +* @new_size: new size in bytes to which to resize the attribute record @a +* +* Resize the attribute record @a, i.e. the resident part of the attribute, in +* the mft record @m to @new_size bytes. +*/ +int ntfs_attr_record_resize (struct mft_record *m,struct attr_record *a,u32 new_size) +{ +u32 old_size, alloc_size, attr_size; + + old_size =le32_to_cpu (m->bytes_in_use); + alloc_size =le32_to_cpu (m->bytes_allocated); + attr_size =le32_to_cpu (a->length); + +ntfs_debug ("Sizes: old=%u alloc=%u attr=%u new=%u\n" , + (unsigned int )old_size, (unsigned int )alloc_size, + (unsigned int )attr_size, (unsigned int )new_size); + +/* Align to 8 bytes if it is not already done. */ +if (new_size &7 ) + new_size = (new_size +7 ) & ~7 ; +/* If the actual attribute length has changed, move things around. */ +if (new_size != attr_size) { +u32 new_muse =le32_to_cpu (m->bytes_in_use) - + attr_size + new_size; +/* Not enough space in this mft record. */ +if (new_muse >le32_to_cpu (m->bytes_allocated)) +return -ENOSPC ; + +if (a->type ==AT_INDEX_ROOT && new_size > attr_size && + new_muse +120 > alloc_size && old_size +120 <= alloc_size) { +ntfs_debug ("Too big struct index_root (%u > %u)\n" , + new_muse, alloc_size); +return -ENOSPC ; + } + +/* Move attributes following @a to their new location. */ +memmove ((u8 *)a + new_size, (u8 *)a +le32_to_cpu (a->length), +le32_to_cpu (m->bytes_in_use) - ((u8 *)a - + (u8 *)m) - attr_size); +/* Adjust @m to reflect the change in used space. */ + m->bytes_in_use =cpu_to_le32 (new_muse); +/* Adjust @a to reflect the new size. */ +if (new_size >= offsetof(struct attr_record , length) +sizeof (a->length)) + a->length =cpu_to_le32 (new_size); + } +return 0 ; +} + +/** +* ntfs_resident_attr_value_resize - resize the value of a resident attribute +* @m: mft record containing attribute record +* @a: attribute record whose value to resize +* @new_size: new size in bytes to which to resize the attribute value of @a +* +* Resize the value of the attribute @a in the mft record @m to @new_size bytes. +* If the value is made bigger, the newly allocated space is cleared. +*/ +int ntfs_resident_attr_value_resize (struct mft_record *m,struct attr_record *a, +const u32 new_size) +{ +u32 old_size; + +/* Resize the resident part of the attribute record. */ +if (ntfs_attr_record_resize (m, a, +le16_to_cpu (a->data.resident.value_offset) + new_size)) +return -ENOSPC ; +/* +* The resize succeeded! If we made the attribute value bigger, clear +* the area between the old size and @new_size. +*/ + old_size =le32_to_cpu (a->data.resident.value_length); +if (new_size > old_size) +memset ((u8 *)a +le16_to_cpu (a->data.resident.value_offset) + + old_size,0 , new_size - old_size); +/* Finally update the length of the attribute value. */ + a->data.resident.value_length =cpu_to_le32 (new_size); +return 0 ; +} + +/** +* ntfs_attr_make_non_resident - convert a resident to a non-resident attribute +* @ni: ntfs inode describing the attribute to convert +* @data_size: size of the resident data to copy to the non-resident attribute +* +* Convert the resident ntfs attribute described by the ntfs inode @ni to a +* non-resident one. +* +* @data_size must be equal to the attribute value size. This is needed since +* we need to know the size before we can map the mft record and our callers +* always know it. The reason we cannot simply read the size from the vfs +* inode i_size is that this is not necessarily uptodate. This happens when +* ntfs_attr_make_non_resident() is called in the ->truncate call path(s). +*/ +int ntfs_attr_make_non_resident (struct ntfs_inode *ni,const u32 data_size) +{ +s64 new_size; +struct inode *vi =VFS_I (ni); +struct ntfs_volume *vol = ni->vol; +struct ntfs_inode *base_ni; +struct mft_record *m; +struct attr_record *a; +struct ntfs_attr_search_ctx *ctx; +struct folio *folio; +struct runlist_element *rl; +u8 *kaddr; +unsigned long flags; +int mp_size, mp_ofs, name_ofs, arec_size, err, err2; +u32 attr_size; +u8 old_res_attr_flags; + +if (NInoNonResident (ni)) { +ntfs_warning (vol->sb, +"Trying to make non-resident attribute non-resident. Aborting...\n" ); +return -EINVAL ; + } + +/* Check that the attribute is allowed to be non-resident. */ + err =ntfs_attr_can_be_non_resident (vol, ni->type); +if (unlikely (err)) { +if (err == -EPERM ) +ntfs_debug ("Attribute is not allowed to be non-resident." ); +else +ntfs_debug ("Attribute not defined on the NTFS volume!" ); +return err; + } + +if (NInoEncrypted (ni)) +return -EIO ; + +if (!NInoAttr (ni)) + base_ni = ni; +else + base_ni = ni->ext.base_ntfs_ino; + m =map_mft_record (base_ni); +if (IS_ERR (m)) { + err =PTR_ERR (m); + m =NULL ; + ctx =NULL ; + goto err_out; + } + ctx =ntfs_attr_get_search_ctx (base_ni, m); +if (unlikely (!ctx)) { + err = -ENOMEM ; + goto err_out; + } + err =ntfs_attr_lookup (ni->type, ni->name, ni->name_len, +CASE_SENSITIVE ,0 ,NULL ,0 , ctx); +if (unlikely (err)) { +if (err == -ENOENT ) + err = -EIO ; + goto err_out; + } + m = ctx->mrec; + a = ctx->attr; + +/* +* The size needs to be aligned to a cluster boundary for allocation +* purposes. +*/ + new_size = (data_size + vol->cluster_size -1 ) & + ~(vol->cluster_size -1 ); +if (new_size >0 ) { +if ((a->flags &ATTR_COMPRESSION_MASK ) ==ATTR_IS_COMPRESSED ) { +/* must allocate full compression blocks */ + new_size = + ((new_size -1 ) | + ((1L << (STANDARD_COMPRESSION_UNIT + + vol->cluster_size_bits)) -1 )) +1 ; + } + +/* +* Will need folio later and since folio lock nests +* outside all ntfs locks, we need to get the folio now. +*/ + folio =__filemap_get_folio (vi->i_mapping,0 , +FGP_CREAT |FGP_LOCK , +mapping_gfp_mask (vi->i_mapping)); +if (IS_ERR (folio)) { + err = -ENOMEM ; + goto err_out; + } + +/* Start by allocating clusters to hold the attribute value. */ + rl =ntfs_cluster_alloc (vol,0 , new_size >> + vol->cluster_size_bits,-1 ,DATA_ZONE , true, + false, false); +if (IS_ERR (rl)) { + err =PTR_ERR (rl); +ntfs_debug ("Failed to allocate cluster%s, error code %i." , + (new_size >> vol->cluster_size_bits) >1 ?"s" :"" , + err); + goto folio_err_out; + } + }else { + rl =NULL ; + folio =NULL ; + } + +down_write (&ni->runlist.lock); +/* Determine the size of the mapping pairs array. */ + mp_size =ntfs_get_size_for_mapping_pairs (vol, rl,0 ,-1 ,-1 ); +if (unlikely (mp_size <0 )) { + err = mp_size; +ntfs_debug ("Failed to get size for mapping pairs array, error code %i.\n" , err); + goto rl_err_out; + } + +if (NInoNonResident (ni) || a->non_resident) { + err = -EIO ; + goto rl_err_out; + } + +/* +* Calculate new offsets for the name and the mapping pairs array. +*/ +if (NInoSparse (ni) ||NInoCompressed (ni)) + name_ofs = (offsetof(struct attr_record , + data.non_resident.compressed_size) + +sizeof (a->data.non_resident.compressed_size) + + 7) & ~7; +else + name_ofs = (offsetof(struct attr_record , + data.non_resident.compressed_size) + 7) & ~7; + mp_ofs = (name_ofs + a->name_length *sizeof (__le16) +7 ) & ~7 ; +/* +* Determine the size of the resident part of the now non-resident +* attribute record. +*/ + arec_size = (mp_ofs + mp_size +7 ) & ~7 ; +/* +* If the folio is not uptodate bring it uptodate by copying from the +* attribute value. +*/ + attr_size =le32_to_cpu (a->data.resident.value_length); +WARN_ON (attr_size != data_size); +if (folio && !folio_test_uptodate (folio)) { + kaddr =kmap_local_folio (folio,0 ); +memcpy (kaddr, (u8 *)a + +le16_to_cpu (a->data.resident.value_offset), + attr_size); +memset (kaddr + attr_size,0 ,PAGE_SIZE - attr_size); +kunmap_local (kaddr); +flush_dcache_folio (folio); +folio_mark_uptodate (folio); + } + +/* Backup the attribute flag. */ + old_res_attr_flags = a->data.resident.flags; +/* Resize the resident part of the attribute record. */ + err =ntfs_attr_record_resize (m, a, arec_size); +if (unlikely (err)) + goto rl_err_out; + +/* +* Convert the resident part of the attribute record to describe a +* non-resident attribute. +*/ + a->non_resident =1 ; +/* Move the attribute name if it exists and update the offset. */ +if (a->name_length) +memmove ((u8 *)a + name_ofs, (u8 *)a +le16_to_cpu (a->name_offset), + a->name_length *sizeof (__le16)); + a->name_offset =cpu_to_le16 (name_ofs); +/* Setup the fields specific to non-resident attributes. */ + a->data.non_resident.lowest_vcn =0 ; + a->data.non_resident.highest_vcn =cpu_to_le64 ((new_size -1 ) >> + vol->cluster_size_bits); + a->data.non_resident.mapping_pairs_offset =cpu_to_le16 (mp_ofs); +memset (&a->data.non_resident.reserved,0 , +sizeof (a->data.non_resident.reserved)); + a->data.non_resident.allocated_size =cpu_to_le64 (new_size); + a->data.non_resident.data_size = + a->data.non_resident.initialized_size = +cpu_to_le64 (attr_size); +if (NInoSparse (ni) ||NInoCompressed (ni)) { + a->data.non_resident.compression_unit =0 ; +if (NInoCompressed (ni) || vol->major_ver <3 ) + a->data.non_resident.compression_unit =4 ; + a->data.non_resident.compressed_size = + a->data.non_resident.allocated_size; + }else + a->data.non_resident.compression_unit =0 ; +/* Generate the mapping pairs array into the attribute record. */ + err =ntfs_mapping_pairs_build (vol, (u8 *)a + mp_ofs, + arec_size - mp_ofs, rl,0 ,-1 ,NULL ,NULL ,NULL ); +if (unlikely (err)) { +ntfs_error (vol->sb,"Failed to build mapping pairs, error code %i." , + err); + goto undo_err_out; + } + +/* Setup the in-memory attribute structure to be non-resident. */ + ni->runlist.rl = rl; +if (rl) { +for (ni->runlist.count =1 ; rl->length !=0 ; rl++) + ni->runlist.count++; + }else + ni->runlist.count =0 ; +write_lock_irqsave (&ni->size_lock, flags); + ni->allocated_size = new_size; +if (NInoSparse (ni) ||NInoCompressed (ni)) { + ni->itype.compressed.size = ni->allocated_size; +if (a->data.non_resident.compression_unit) { + ni->itype.compressed.block_size =1U << + (a->data.non_resident.compression_unit + + vol->cluster_size_bits); + ni->itype.compressed.block_size_bits = +ffs (ni->itype.compressed.block_size) - +1 ; + ni->itype.compressed.block_clusters =1U << + a->data.non_resident.compression_unit; + }else { + ni->itype.compressed.block_size =0 ; + ni->itype.compressed.block_size_bits =0 ; + ni->itype.compressed.block_clusters =0 ; + } + vi->i_blocks = ni->itype.compressed.size >>9 ; + }else + vi->i_blocks = ni->allocated_size >>9 ; +write_unlock_irqrestore (&ni->size_lock, flags); +/* +* This needs to be last since the address space operations ->read_folio +* and ->writepage can run concurrently with us as they are not +* serialized on i_mutex. Note, we are not allowed to fail once we flip +* this switch, which is another reason to do this last. +*/ +NInoSetNonResident (ni); +NInoSetFullyMapped (ni); +/* Mark the mft record dirty, so it gets written back. */ +mark_mft_record_dirty (ctx->ntfs_ino); +ntfs_attr_put_search_ctx (ctx); +unmap_mft_record (base_ni); +up_write (&ni->runlist.lock); +if (folio) { +iomap_dirty_folio (vi->i_mapping, folio); +folio_unlock (folio); +folio_put (folio); + } +ntfs_debug ("Done." ); +return 0 ; +undo_err_out: +/* Convert the attribute back into a resident attribute. */ + a->non_resident =0 ; +/* Move the attribute name if it exists and update the offset. */ + name_ofs = (offsetof(struct attr_record, data.resident.reserved) + + sizeof(a->data.resident.reserved) + 7) & ~7; +if (a->name_length) +memmove ((u8 *)a + name_ofs, (u8 *)a +le16_to_cpu (a->name_offset), + a->name_length *sizeof (__le16)); + mp_ofs = (name_ofs + a->name_length *sizeof (__le16) +7 ) & ~7 ; + a->name_offset =cpu_to_le16 (name_ofs); + arec_size = (mp_ofs + attr_size +7 ) & ~7 ; +/* Resize the resident part of the attribute record. */ + err2 =ntfs_attr_record_resize (m, a, arec_size); +if (unlikely (err2)) { +/* +* This cannot happen (well if memory corruption is at work it +* could happen in theory), but deal with it as well as we can. +* If the old size is too small, truncate the attribute, +* otherwise simply give it a larger allocated size. +*/ + arec_size =le32_to_cpu (a->length); +if ((mp_ofs + attr_size) > arec_size) { + err2 = attr_size; + attr_size = arec_size - mp_ofs; +ntfs_error (vol->sb, +"Failed to undo partial resident to non-resident attribute conversion. Truncating inode 0x%lx, attribute type 0x%x from %i bytes to %i bytes to maintain metadata consistency. THIS MEANS YOU ARE LOSING %i BYTES DATA FROM THIS %s." , + vi->i_ino, + (unsigned int )le32_to_cpu (ni->type), + err2, attr_size, err2 - attr_size, + ((ni->type ==AT_DATA ) && + !ni->name_len) ?"FILE" :"ATTRIBUTE" ); +write_lock_irqsave (&ni->size_lock, flags); + ni->initialized_size = attr_size; +i_size_write (vi, attr_size); +write_unlock_irqrestore (&ni->size_lock, flags); + } + } +/* Setup the fields specific to resident attributes. */ + a->data.resident.value_length =cpu_to_le32 (attr_size); + a->data.resident.value_offset =cpu_to_le16 (mp_ofs); + a->data.resident.flags = old_res_attr_flags; +memset (&a->data.resident.reserved,0 , +sizeof (a->data.resident.reserved)); +/* Copy the data from folio back to the attribute value. */ +if (folio) +memcpy_from_folio ((u8 *)a + mp_ofs, folio,0 , attr_size); +/* Setup the allocated size in the ntfs inode in case it changed. */ +write_lock_irqsave (&ni->size_lock, flags); + ni->allocated_size = arec_size - mp_ofs; +write_unlock_irqrestore (&ni->size_lock, flags); +/* Mark the mft record dirty, so it gets written back. */ +mark_mft_record_dirty (ctx->ntfs_ino); +rl_err_out: +up_write (&ni->runlist.lock); +if (rl) { +if (ntfs_cluster_free_from_rl (vol, rl) <0 ) { +ntfs_error (vol->sb, +"Failed to release allocated cluster(s) in error code path. Run chkdsk to recover the lost cluster(s)." ); +NVolSetErrors (vol); + } +ntfs_free (rl); +folio_err_out: +folio_unlock (folio); +folio_put (folio); + } +err_out: +if (ctx) +ntfs_attr_put_search_ctx (ctx); +if (m) +unmap_mft_record (base_ni); + ni->runlist.rl =NULL ; + +if (err == -EINVAL ) + err = -EIO ; +return err; +} + +/** +* ntfs_attr_set - fill (a part of) an attribute with a byte +* @ni: ntfs inode describing the attribute to fill +* @ofs: offset inside the attribute at which to start to fill +* @cnt: number of bytes to fill +* @val: the unsigned 8-bit value with which to fill the attribute +* +* Fill @cnt bytes of the attribute described by the ntfs inode @ni starting at +* byte offset @ofs inside the attribute with the constant byte @val. +* +* This function is effectively like memset() applied to an ntfs attribute. +* Note thie function actually only operates on the page cache pages belonging +* to the ntfs attribute and it marks them dirty after doing the memset(). +* Thus it relies on the vm dirty page write code paths to cause the modified +* pages to be written to the mft record/disk. +*/ +int ntfs_attr_set (struct ntfs_inode *ni,s64 ofs,s64 cnt,const u8 val) +{ +struct address_space *mapping =VFS_I (ni)->i_mapping; +struct folio *folio; +pgoff_t index; +u8 *addr; +unsigned long offset; +size_t attr_len; +int ret =0 ; + + index = ofs >>PAGE_SHIFT ; +while (cnt) { + folio =ntfs_read_mapping_folio (mapping, index); +if (IS_ERR (folio)) { + ret =PTR_ERR (folio); +ntfs_error (VFS_I (ni)->i_sb,"Failed to read a page %lu for attr %#x: %ld" , + index, ni->type,PTR_ERR (folio)); +break ; + } + + offset =offset_in_folio (folio, ofs); + attr_len =min_t (size_t, (size_t )cnt,folio_size (folio) - offset); + +folio_lock (folio); + addr =kmap_local_folio (folio, offset); +memset (addr, val, attr_len); +kunmap_local (addr); + +flush_dcache_folio (folio); +folio_mark_dirty (folio); +folio_unlock (folio); +folio_put (folio); + + ofs += attr_len; + cnt -= attr_len; + index++; +cond_resched (); + } + +return ret; +} + +int ntfs_attr_set_initialized_size (struct ntfs_inode *ni,loff_t new_size) +{ +struct ntfs_attr_search_ctx *ctx; +int err =0 ; + +if (!NInoNonResident (ni)) +return -EINVAL ; + + ctx =ntfs_attr_get_search_ctx (ni,NULL ); +if (!ctx) +return -ENOMEM ; + + err =ntfs_attr_lookup (ni->type, ni->name, ni->name_len, +CASE_SENSITIVE ,0 ,NULL ,0 , ctx); +if (err) + goto out_ctx; + + ctx->attr->data.non_resident.initialized_size =cpu_to_le64 (new_size); + ni->initialized_size = new_size; +mark_mft_record_dirty (ctx->ntfs_ino); +out_ctx: +ntfs_attr_put_search_ctx (ctx); +return err; +} + +/** +* ntfs_make_room_for_attr - make room for an attribute inside an mft record +* @m: mft record +* @pos: position at which to make space +* @size: byte size to make available at this position +* +* @pos points to the attribute in front of which we want to make space. +*/ +static int ntfs_make_room_for_attr (struct mft_record *m,u8 *pos,u32 size) +{ +u32 biu; + +ntfs_debug ("Entering for pos 0x%x, size %u.\n" , + (int )(pos - (u8 *)m), (unsigned int ) size); + +/* Make size 8-byte alignment. */ + size = (size +7 ) & ~7 ; + +/* Rigorous consistency checks. */ +if (!m || !pos || pos < (u8 *)m) { +pr_err ("%s: pos=%p m=%p" , __func__, pos, m); +return -EINVAL ; + } + +/* The -8 is for the attribute terminator. */ +if (pos - (u8 *)m > (int )le32_to_cpu (m->bytes_in_use) -8 ) +return -EINVAL ; +/* Nothing to do. */ +if (!size) +return 0 ; + + biu =le32_to_cpu (m->bytes_in_use); +/* Do we have enough space? */ +if (biu + size >le32_to_cpu (m->bytes_allocated) || + pos + size > (u8 *)m +le32_to_cpu (m->bytes_allocated)) { +ntfs_debug ("No enough space in the MFT record\n" ); +return -ENOSPC ; + } +/* Move everything after pos to pos + size. */ +memmove (pos + size, pos, biu - (pos - (u8 *)m)); +/* Update mft record. */ + m->bytes_in_use =cpu_to_le32 (biu + size); +return 0 ; +} + +/** +* ntfs_resident_attr_record_add - add resident attribute to inode +* @ni: opened ntfs inode to which MFT record add attribute +* @type: type of the new attribute +* @name: name of the new attribute +* @name_len: name length of the new attribute +* @val: value of the new attribute +* @size: size of new attribute (length of @val, if @val != NULL) +* @flags: flags of the new attribute +*/ +int ntfs_resident_attr_record_add (struct ntfs_inode *ni,__le32 type, +__le16 *name,u8 name_len,u8 *val,u32 size, +__le16 flags) +{ +struct ntfs_attr_search_ctx *ctx; +u32 length; +struct attr_record *a; +struct mft_record *m; +int err, offset; +struct ntfs_inode *base_ni; + +ntfs_debug ("Entering for inode 0x%llx, attr 0x%x, flags 0x%x.\n" , + (long long ) ni->mft_no, (unsigned int )le32_to_cpu (type), + (unsigned int )le16_to_cpu (flags)); + +if (!ni || (!name && name_len)) +return -EINVAL ; + + err =ntfs_attr_can_be_resident (ni->vol, type); +if (err) { +if (err == -EPERM ) +ntfs_debug ("Attribute can't be resident.\n" ); +else +ntfs_debug ("ntfs_attr_can_be_resident failed.\n" ); +return err; + } + +/* Locate place where record should be. */ + ctx =ntfs_attr_get_search_ctx (ni,NULL ); +if (!ctx) { +ntfs_error (ni->vol->sb,"%s: Failed to get search context" , + __func__); +return -ENOMEM ; + } +/* +* Use ntfs_attr_find instead of ntfs_attr_lookup to find place for +* 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); +if (!err) { + err = -EEXIST ; +ntfs_debug ("Attribute already present.\n" ); + goto put_err_out; + } +if (err != -ENOENT ) { + err = -EIO ; + goto put_err_out; + } + a = ctx->attr; + m = ctx->mrec; + +/* Make room for attribute. */ + length = offsetof(struct attr_record, data.resident.reserved) + +sizeof (a->data.resident.reserved) + + ((name_len *sizeof (__le16) +7 ) & ~7 ) + + ((size +7 ) & ~7 ); + err =ntfs_make_room_for_attr (ctx->mrec, (u8 *) ctx->attr, length); +if (err) { +ntfs_debug ("Failed to make room for attribute.\n" ); + goto put_err_out; + } + +/* Setup record fields. */ + offset = ((u8 *)a - (u8 *)m); + a->type = type; + a->length =cpu_to_le32 (length); + a->non_resident =0 ; + a->name_length = name_len; + a->name_offset = + name_len ? cpu_to_le16((offsetof(struct attr_record, data.resident.reserved) + + sizeof(a->data.resident.reserved))) : cpu_to_le16(0); + + a->flags = flags; + a->instance = m->next_attr_instance; + a->data.resident.value_length =cpu_to_le32 (size); + a->data.resident.value_offset =cpu_to_le16 (length - ((size +7 ) & ~7 )); +if (val) +memcpy ((u8 *)a +le16_to_cpu (a->data.resident.value_offset), val, size); +else +memset ((u8 *)a +le16_to_cpu (a->data.resident.value_offset),0 , size); +if (type ==AT_FILE_NAME ) + a->data.resident.flags =RESIDENT_ATTR_IS_INDEXED ; +else + a->data.resident.flags =0 ; +if (name_len) +memcpy ((u8 *)a +le16_to_cpu (a->name_offset), + name,sizeof (__le16) * name_len); + m->next_attr_instance = +cpu_to_le16 ((le16_to_cpu (m->next_attr_instance) +1 ) &0xffff ); +if (ni->nr_extents ==-1 ) + base_ni = ni->ext.base_ntfs_ino; +else + base_ni = ni; +if (type !=AT_ATTRIBUTE_LIST &&NInoAttrList (base_ni)) { + err =ntfs_attrlist_entry_add (ni, a); +if (err) { +ntfs_attr_record_resize (m, a,0 ); +mark_mft_record_dirty (ctx->ntfs_ino); +ntfs_debug ("Failed add attribute entry to ATTRIBUTE_LIST.\n" ); + goto put_err_out; + } + } +mark_mft_record_dirty (ni); +ntfs_attr_put_search_ctx (ctx); +return offset; +put_err_out: +ntfs_attr_put_search_ctx (ctx); +return -EIO ; +} + +/** +* ntfs_non_resident_attr_record_add - add extent of non-resident attribute +* @ni: opened ntfs inode to which MFT record add attribute +* @type: type of the new attribute extent +* @name: name of the new attribute extent +* @name_len: name length of the new attribute extent +* @lowest_vcn: lowest vcn of the new attribute extent +* @dataruns_size: dataruns size of the new attribute extent +* @flags: flags of the new attribute extent +*/ +static int ntfs_non_resident_attr_record_add (struct ntfs_inode *ni,__le32 type, +__le16 *name,u8 name_len,s64 lowest_vcn,int dataruns_size, +__le16 flags) +{ +struct ntfs_attr_search_ctx *ctx; +u32 length; +struct attr_record *a; +struct mft_record *m; +struct ntfs_inode *base_ni; +int err, offset; + +ntfs_debug ("Entering for inode 0x%llx, attr 0x%x, lowest_vcn %lld, dataruns_size %d, flags 0x%x.\n" , + (long long ) ni->mft_no, (unsigned int )le32_to_cpu (type), + (long long ) lowest_vcn, dataruns_size, + (unsigned int )le16_to_cpu (flags)); + +if (!ni || dataruns_size <=0 || (!name && name_len)) +return -EINVAL ; + + err =ntfs_attr_can_be_non_resident (ni->vol, type); +if (err) { +if (err == -EPERM ) +pr_err ("Attribute can't be non resident" ); +else +pr_err ("ntfs_attr_can_be_non_resident failed" ); +return err; + } + +/* Locate place where record should be. */ + ctx =ntfs_attr_get_search_ctx (ni,NULL ); +if (!ctx) { +pr_err ("%s: Failed to get search context" , __func__); +return -ENOMEM ; + } +/* +* Use ntfs_attr_find instead of ntfs_attr_lookup to find place for +* 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); +if (!err) { + err = -EEXIST ; +pr_err ("Attribute 0x%x already present" , type); + goto put_err_out; + } +if (err != -ENOENT ) { +pr_err ("ntfs_attr_find failed" ); + err = -EIO ; + goto put_err_out; + } + a = ctx->attr; + m = ctx->mrec; + +/* Make room for attribute. */ + dataruns_size = (dataruns_size +7 ) & ~7 ; + length = offsetof(struct attr_record, data.non_resident.compressed_size) + + ((sizeof (__le16) * name_len +7 ) & ~7 ) + dataruns_size + + ((flags & (ATTR_IS_COMPRESSED |ATTR_IS_SPARSE )) ? +sizeof (a->data.non_resident.compressed_size) :0 ); + err =ntfs_make_room_for_attr (ctx->mrec, (u8 *) ctx->attr, length); +if (err) { +pr_err ("Failed to make room for attribute" ); + goto put_err_out; + } + +/* Setup record fields. */ + a->type = type; + a->length =cpu_to_le32 (length); + a->non_resident =1 ; + a->name_length = name_len; + a->name_offset =cpu_to_le16 (offsetof(struct attr_record , + data.non_resident.compressed_size) + + ((flags & (ATTR_IS_COMPRESSED |ATTR_IS_SPARSE )) ? + sizeof(a->data.non_resident.compressed_size) : 0)); + a->flags = flags; + a->instance = m->next_attr_instance; + a->data.non_resident.lowest_vcn =cpu_to_le64 (lowest_vcn); + a->data.non_resident.mapping_pairs_offset =cpu_to_le16 (length - dataruns_size); + a->data.non_resident.compression_unit = + (flags &ATTR_IS_COMPRESSED ) ?STANDARD_COMPRESSION_UNIT :0 ; +/* If @lowest_vcn == 0, than setup empty attribute. */ +if (!lowest_vcn) { + a->data.non_resident.highest_vcn =cpu_to_le64 (-1 ); + a->data.non_resident.allocated_size =0 ; + a->data.non_resident.data_size =0 ; + a->data.non_resident.initialized_size =0 ; +/* Set empty mapping pairs. */ + *((u8 *)a +le16_to_cpu (a->data.non_resident.mapping_pairs_offset)) =0 ; + } +if (name_len) +memcpy ((u8 *)a +le16_to_cpu (a->name_offset), + name,sizeof (__le16) * name_len); + m->next_attr_instance = +cpu_to_le16 ((le16_to_cpu (m->next_attr_instance) +1 ) &0xffff ); +if (ni->nr_extents ==-1 ) + base_ni = ni->ext.base_ntfs_ino; +else + base_ni = ni; +if (type !=AT_ATTRIBUTE_LIST &&NInoAttrList (base_ni)) { + err =ntfs_attrlist_entry_add (ni, a); +if (err) { +pr_err ("Failed add attr entry to attrlist" ); +ntfs_attr_record_resize (m, a,0 ); + goto put_err_out; + } + } +mark_mft_record_dirty (ni); +/* +* Locate offset from start of the MFT record where new attribute is +* placed. We need relookup it, because record maybe moved during +* update of attribute list. +*/ +ntfs_attr_reinit_search_ctx (ctx); + err =ntfs_attr_lookup (type, name, name_len,CASE_SENSITIVE , + lowest_vcn,NULL ,0 , ctx); +if (err) { +pr_err ("%s: attribute lookup failed" , __func__); +ntfs_attr_put_search_ctx (ctx); +return err; + + } + offset = (u8 *)ctx->attr - (u8 *)ctx->mrec; +ntfs_attr_put_search_ctx (ctx); +return offset; +put_err_out: +ntfs_attr_put_search_ctx (ctx); +return -1 ; +} + +/** +* ntfs_attr_record_rm - remove attribute extent +* @ctx: search context describing the attribute which should be removed +* +* If this function succeed, user should reinit search context if he/she wants +* use it anymore. +*/ +int ntfs_attr_record_rm (struct ntfs_attr_search_ctx *ctx) +{ +struct ntfs_inode *base_ni, *ni; +__le32 type; +int err; + +if (!ctx || !ctx->ntfs_ino || !ctx->mrec || !ctx->attr) +return -EINVAL ; + +ntfs_debug ("Entering for inode 0x%llx, attr 0x%x.\n" , + (long long ) ctx->ntfs_ino->mft_no, + (unsigned int )le32_to_cpu (ctx->attr->type)); + type = ctx->attr->type; + ni = ctx->ntfs_ino; +if (ctx->base_ntfs_ino) + base_ni = ctx->base_ntfs_ino; +else + base_ni = ctx->ntfs_ino; + +/* Remove attribute itself. */ +if (ntfs_attr_record_resize (ctx->mrec, ctx->attr,0 )) { +ntfs_debug ("Couldn't remove attribute record. Bug or damaged MFT record.\n" ); +return -EIO ; + } +mark_mft_record_dirty (ni); + +/* +* Remove record from $ATTRIBUTE_LIST if present and we don't want +* delete $ATTRIBUTE_LIST itself. +*/ +if (NInoAttrList (base_ni) && type !=AT_ATTRIBUTE_LIST ) { + err =ntfs_attrlist_entry_rm (ctx); +if (err) { +ntfs_debug ("Couldn't delete record from $ATTRIBUTE_LIST.\n" ); +return err; + } + } + +/* Post $ATTRIBUTE_LIST delete setup. */ +if (type ==AT_ATTRIBUTE_LIST ) { +if (NInoAttrList (base_ni) && base_ni->attr_list) +ntfs_free (base_ni->attr_list); + base_ni->attr_list =NULL ; +NInoClearAttrList (base_ni); + } + +/* Free MFT record, if it doesn't contain attributes. */ +if (le32_to_cpu (ctx->mrec->bytes_in_use) - +le16_to_cpu (ctx->mrec->attrs_offset) ==8 ) { +if (ntfs_mft_record_free (ni->vol, ni)) { +ntfs_debug ("Couldn't free MFT record.\n" ); +return -EIO ; + } +/* Remove done if we freed base inode. */ +if (ni == base_ni) +return 0 ; +ntfs_inode_close (ni); + ctx->ntfs_ino = ni =NULL ; + } + +if (type ==AT_ATTRIBUTE_LIST || !NInoAttrList (base_ni)) +return 0 ; + +/* Remove attribute list if we don't need it any more. */ +if (!ntfs_attrlist_need (base_ni)) { +struct ntfs_attr na; +struct inode *attr_vi; + +ntfs_attr_reinit_search_ctx (ctx); +if (ntfs_attr_lookup (AT_ATTRIBUTE_LIST ,NULL ,0 ,CASE_SENSITIVE , +0 ,NULL ,0 , ctx)) { +ntfs_debug ("Couldn't find attribute list. Succeed anyway.\n" ); +return 0 ; + } +/* Deallocate clusters. */ +if (ctx->attr->non_resident) { +struct runlist_element *al_rl; +size_t new_rl_count; + + al_rl =ntfs_mapping_pairs_decompress (base_ni->vol, + ctx->attr,NULL , &new_rl_count); +if (IS_ERR (al_rl)) { +ntfs_debug ("Couldn't decompress attribute list runlist. Succeed anyway.\n" ); +return 0 ; + } +if (ntfs_cluster_free_from_rl (base_ni->vol, al_rl)) +ntfs_debug ("Leaking clusters! Run chkdsk. Couldn't free clusters from attribute list runlist.\n" ); +ntfs_free (al_rl); + } +/* Remove attribute record itself. */ +if (ntfs_attr_record_rm (ctx)) { +ntfs_debug ("Couldn't remove attribute list. Succeed anyway.\n" ); +return 0 ; + } + + na.mft_no =VFS_I (base_ni)->i_ino; + na.type =AT_ATTRIBUTE_LIST ; + na.name =NULL ; + na.name_len =0 ; + + attr_vi =ilookup5 (VFS_I (base_ni)->i_sb,VFS_I (base_ni)->i_ino, + ntfs_test_inode, &na); +if (attr_vi) { +clear_nlink (attr_vi); +iput (attr_vi); + } + + } +return 0 ; +} + +/** +* ntfs_attr_add - add attribute to inode +* @ni: opened ntfs inode to which add attribute +* @type: type of the new attribute +* @name: name in unicode of the new attribute +* @name_len: name length in unicode characters of the new attribute +* @val: value of new attribute +* @size: size of the new attribute / length of @val (if specified) +* +* @val should always be specified for always resident attributes (eg. FILE_NAME +* attribute), for attributes that can become non-resident @val can be NULL +* (eg. DATA attribute). @size can be specified even if @val is NULL, in this +* case data size will be equal to @size and initialized size will be equal +* to 0. +* +* If inode haven't got enough space to add attribute, add attribute to one of +* it extents, if no extents present or no one of them have enough space, than +* allocate new extent and add attribute to it. +* +* If on one of this steps attribute list is needed but not present, than it is +* added transparently to caller. So, this function should not be called with +* @type == AT_ATTRIBUTE_LIST, if you really need to add attribute list call +* ntfs_inode_add_attrlist instead. +* +* On success return 0. On error return -1 with errno set to the error code. +*/ +int ntfs_attr_add (struct ntfs_inode *ni,__le32 type, +__le16 *name,u8 name_len,u8 *val,s64 size) +{ +struct super_block *sb; +u32 attr_rec_size; +int err, i, offset; +bool is_resident; +bool can_be_non_resident = false; +struct ntfs_inode *attr_ni; +struct inode *attr_vi; +struct mft_record *ni_mrec; + +if (!ni || size <0 || type ==AT_ATTRIBUTE_LIST ) +return -EINVAL ; + +ntfs_debug ("Entering for inode 0x%llx, attr %x, size %lld.\n" , + (long long ) ni->mft_no, type, size); + +if (ni->nr_extents ==-1 ) + ni = ni->ext.base_ntfs_ino; + +/* Check the attribute type and the size. */ + err =ntfs_attr_size_bounds_check (ni->vol, type, size); +if (err) { +if (err == -ENOENT ) + err = -EIO ; +return err; + } + + sb = ni->vol->sb; +/* Sanity checks for always resident attributes. */ + err =ntfs_attr_can_be_non_resident (ni->vol, type); +if (err) { +if (err != -EPERM ) { +ntfs_error (sb,"ntfs_attr_can_be_non_resident failed" ); + goto err_out; + } +/* @val is mandatory. */ +if (!val) { +ntfs_error (sb, +"val is mandatory for always resident attributes" ); +return -EINVAL ; + } +if (size > ni->vol->mft_record_size) { +ntfs_error (sb,"Attribute is too big" ); +return -ERANGE ; + } + }else + can_be_non_resident = true; + +/* +* Determine resident or not will be new attribute. We add 8 to size in +* non resident case for mapping pairs. +*/ + err =ntfs_attr_can_be_resident (ni->vol, type); +if (!err) { + is_resident = true; + }else { +if (err != -EPERM ) { +ntfs_error (sb,"ntfs_attr_can_be_resident failed" ); + goto err_out; + } + is_resident = false; + } + +/* Calculate attribute record size. */ +if (is_resident) + attr_rec_size = offsetof(struct attr_record, data.resident.reserved) + +1 + + ((name_len *sizeof (__le16) +7 ) & ~7 ) + + ((size +7 ) & ~7 ); +else + attr_rec_size = offsetof(struct attr_record, data.non_resident.compressed_size) + + ((name_len *sizeof (__le16) +7 ) & ~7 ) +8 ; + +/* +* If we have enough free space for the new attribute in the base MFT +* record, then add attribute to it. +*/ +retry: + ni_mrec =map_mft_record (ni); +if (IS_ERR (ni_mrec)) { + err = -EIO ; + goto err_out; + } + +if (le32_to_cpu (ni_mrec->bytes_allocated) - +le32_to_cpu (ni_mrec->bytes_in_use) >= attr_rec_size) { + attr_ni = ni; +unmap_mft_record (ni); + goto add_attr_record; + } +unmap_mft_record (ni); + +/* Try to add to extent inodes. */ + err =ntfs_inode_attach_all_extents (ni); +if (err) { +ntfs_error (sb,"Failed to attach all extents to inode" ); + goto err_out; + } + +for (i =0 ; i < ni->nr_extents; i++) { + attr_ni = ni->ext.extent_ntfs_inos[i]; + ni_mrec =map_mft_record (attr_ni); +if (IS_ERR (ni_mrec)) { + err = -EIO ; + goto err_out; + } + +if (le32_to_cpu (ni_mrec->bytes_allocated) - +le32_to_cpu (ni_mrec->bytes_in_use) >= + attr_rec_size) { +unmap_mft_record (attr_ni); + goto add_attr_record; + } +unmap_mft_record (attr_ni); + } + +/* There is no extent that contain enough space for new attribute. */ +if (!NInoAttrList (ni)) { +/* Add attribute list not present, add it and retry. */ + err =ntfs_inode_add_attrlist (ni); +if (err) { +ntfs_error (sb,"Failed to add attribute list" ); + goto err_out; + } + goto retry; + } + + attr_ni =NULL ; +/* Allocate new extent. */ + err =ntfs_mft_record_alloc (ni->vol,0 , &attr_ni, ni,NULL ); +if (err) { +ntfs_error (sb,"Failed to allocate extent record" ); + goto err_out; + } +unmap_mft_record (attr_ni); + +add_attr_record: +if (is_resident) { +/* Add resident attribute. */ + offset =ntfs_resident_attr_record_add (attr_ni, type, name, + name_len, val, size,0 ); +if (offset <0 ) { +if (offset == -ENOSPC && can_be_non_resident) + goto add_non_resident; + err = offset; +ntfs_error (sb,"Failed to add resident attribute" ); + goto free_err_out; + } +return 0 ; + } + +add_non_resident: +/* Add non resident attribute. */ + offset =ntfs_non_resident_attr_record_add (attr_ni, type, name, + name_len,0 ,8 ,0 ); +if (offset <0 ) { + err = offset; +ntfs_error (sb,"Failed to add non resident attribute" ); + goto free_err_out; + } + +/* If @size == 0, we are done. */ +if (!size) +return 0 ; + +/* Open new attribute and resize it. */ + attr_vi =ntfs_attr_iget (VFS_I (ni), type, name, name_len); +if (IS_ERR (attr_vi)) { +ntfs_error (sb,"Failed to open just added attribute" ); + goto rm_attr_err_out; + } + attr_ni =NTFS_I (attr_vi); + +/* Resize and set attribute value. */ +if (ntfs_attr_truncate (attr_ni, size) || + (val && (ntfs_inode_attr_pwrite (attr_vi,0 , size, val, false) != size))) { + err = -EIO ; +ntfs_error (sb,"Failed to initialize just added attribute" ); +if (ntfs_attr_rm (attr_ni)) +ntfs_error (sb,"Failed to remove just added attribute" ); +iput (attr_vi); + goto err_out; + } +iput (attr_vi); +return 0 ; + +rm_attr_err_out: +/* Remove just added attribute. */ + ni_mrec =map_mft_record (attr_ni); +if (!IS_ERR (ni_mrec)) { +if (ntfs_attr_record_resize (ni_mrec, + (struct attr_record *)((u8 *)ni_mrec + offset),0 )) +ntfs_error (sb,"Failed to remove just added attribute #2" ); +unmap_mft_record (attr_ni); + }else +pr_err ("EIO when try to remove new added attr\n" ); + +free_err_out: +/* Free MFT record, if it doesn't contain attributes. */ + ni_mrec =map_mft_record (attr_ni); +if (!IS_ERR (ni_mrec)) { +int attr_size; + + attr_size =le32_to_cpu (ni_mrec->bytes_in_use) - +le16_to_cpu (ni_mrec->attrs_offset); +unmap_mft_record (attr_ni); +if (attr_size ==8 ) { +if (ntfs_mft_record_free (attr_ni->vol, attr_ni)) +ntfs_error (sb,"Failed to free MFT record" ); +if (attr_ni->nr_extents <0 ) +ntfs_inode_close (attr_ni); + } + }else +pr_err ("EIO when testing mft record is free-able\n" ); + +err_out: +return err; +} + +/** +* __ntfs_attr_init - primary initialization of an ntfs attribute structure +* @ni: ntfs attribute inode to initialize +* @ni: ntfs inode with which to initialize the ntfs attribute +* @type: attribute type +* @name: attribute name in little endian Unicode or NULL +* @name_len: length of attribute @name in Unicode characters (if @name given) +* +* Initialize the ntfs attribute @na with @ni, @type, @name, and @name_len. +*/ +static void __ntfs_attr_init (struct ntfs_inode *ni, +const __le32 type,__le16 *name,const u32 name_len) +{ + ni->runlist.rl =NULL ; + ni->type = type; + ni->name = name; +if (name) + ni->name_len = name_len; +else + ni->name_len =0 ; +} + +/** +* ntfs_attr_init - initialize an ntfs_attr with data sizes and status +* Final initialization for an ntfs attribute. +*/ +static void ntfs_attr_init (struct ntfs_inode *ni,const bool non_resident, +const bool compressed,const bool encrypted,const bool sparse, +const s64 allocated_size,const s64 data_size, +const s64 initialized_size,const s64 compressed_size, +const u8 compression_unit) +{ +if (non_resident) +NInoSetNonResident (ni); +if (compressed) { +NInoSetCompressed (ni); + ni->flags |=FILE_ATTR_COMPRESSED ; + } +if (encrypted) { +NInoSetEncrypted (ni); + ni->flags |=FILE_ATTR_ENCRYPTED ; + } +if (sparse) { +NInoSetSparse (ni); + ni->flags |=FILE_ATTR_SPARSE_FILE ; + } + ni->allocated_size = allocated_size; + ni->data_size = data_size; + ni->initialized_size = initialized_size; +if (compressed || sparse) { +struct ntfs_volume *vol = ni->vol; + + ni->itype.compressed.size = compressed_size; + ni->itype.compressed.block_clusters =1 << compression_unit; + ni->itype.compressed.block_size =1 << (compression_unit + + vol->cluster_size_bits); + ni->itype.compressed.block_size_bits =ffs ( + ni->itype.compressed.block_size) -1 ; + } +} + +/** +* ntfs_attr_open - open an ntfs attribute for access +* @ni: open ntfs inode in which the ntfs attribute resides +* @type: attribute type +* @name: attribute name in little endian Unicode or AT_UNNAMED or NULL +* @name_len: length of attribute @name in Unicode characters (if @name given) +*/ +int ntfs_attr_open (struct ntfs_inode *ni,const __le32 type, +__le16 *name,u32 name_len) +{ +struct ntfs_attr_search_ctx *ctx; +__le16 *newname =NULL ; +struct attr_record *a; +bool cs; +struct ntfs_inode *base_ni; +int err; + +ntfs_debug ("Entering for inode %lld, attr 0x%x.\n" , + (unsigned long long )ni->mft_no, type); + +if (!ni || !ni->vol) +return -EINVAL ; + +if (NInoAttr (ni)) + base_ni = ni->ext.base_ntfs_ino; +else + base_ni = ni; + +if (name && name !=AT_UNNAMED && name !=I30 ) { + name =ntfs_ucsndup (name, name_len); +if (!name) { + err = -ENOMEM ; + goto err_out; + } + newname = name; + } + + ctx =ntfs_attr_get_search_ctx (base_ni,NULL ); +if (!ctx) { + err = -ENOMEM ; +pr_err ("%s: Failed to get search context" , __func__); + goto err_out; + } + + err =ntfs_attr_lookup (type, name, name_len,0 ,0 ,NULL ,0 , ctx); +if (err) + goto put_err_out; + + a = ctx->attr; + +if (!name) { +if (a->name_length) { + name =ntfs_ucsndup ((__le16 *)((u8 *)a +le16_to_cpu (a->name_offset)), + a->name_length); +if (!name) + goto put_err_out; + newname = name; + name_len = a->name_length; + }else { + name =AT_UNNAMED ; + name_len =0 ; + } + } + +__ntfs_attr_init (ni, type, name, name_len); + +/* +* Wipe the flags in case they are not zero for an attribute list +* attribute. Windows does not complain about invalid flags and chkdsk +* does not detect or fix them so we need to cope with it, too. +*/ +if (type ==AT_ATTRIBUTE_LIST ) + a->flags =0 ; + +if ((type ==AT_DATA ) && + (a->non_resident ? !a->data.non_resident.initialized_size : + !a->data.resident.value_length)) { +/* +* Define/redefine the compression state if stream is +* empty, based on the compression mark on parent +* directory (for unnamed data streams) or on current +* inode (for named data streams). The compression mark +* may change any time, the compression state can only +* change when stream is wiped out. +* +* Also prevent compression on NTFS version < 3.0 +* or cluster size > 4K or compression is disabled +*/ + a->flags &= ~ATTR_COMPRESSION_MASK ; +if (NInoCompressed (ni) + && (ni->vol->major_ver >=3 ) + &&NVolCompression (ni->vol) + && (ni->vol->cluster_size <=MAX_COMPRESSION_CLUSTER_SIZE )) + a->flags |=ATTR_IS_COMPRESSED ; + } + + cs = a->flags & (ATTR_IS_COMPRESSED |ATTR_IS_SPARSE ); + +if (ni->type ==AT_DATA && ni->name ==AT_UNNAMED && + ((!(a->flags &ATTR_IS_COMPRESSED ) != !NInoCompressed (ni)) || + (!(a->flags &ATTR_IS_SPARSE ) != !NInoSparse (ni)) || + (!(a->flags &ATTR_IS_ENCRYPTED ) != !NInoEncrypted (ni)))) { + err = -EIO ; +pr_err ("Inode %lld has corrupt attribute flags (0x%x <> 0x%x)\n" , + (unsigned long long )ni->mft_no, + a->flags, ni->flags); + goto put_err_out; + } + +if (a->non_resident) { +if (((a->flags &ATTR_COMPRESSION_MASK ) || a->data.non_resident.compression_unit) && + (ni->vol->major_ver <3 )) { + err = -EIO ; +pr_err ("Compressed inode %lld not allowed on NTFS %d.%d\n" , + (unsigned long long )ni->mft_no, + ni->vol->major_ver, + ni->vol->major_ver); + goto put_err_out; + } + +if ((a->flags &ATTR_IS_COMPRESSED ) && !a->data.non_resident.compression_unit) { + err = -EIO ; +pr_err ("Compressed inode %lld attr 0x%x has no compression unit\n" , + (unsigned long long )ni->mft_no, type); + goto put_err_out; + } +if ((a->flags &ATTR_COMPRESSION_MASK ) && + (a->data.non_resident.compression_unit !=STANDARD_COMPRESSION_UNIT )) { + err = -EIO ; +pr_err ("Compressed inode %lld attr 0x%lx has an unsupported compression unit %d\n" , + (unsigned long long )ni->mft_no, + (long )le32_to_cpu (type), + (int )a->data.non_resident.compression_unit); + goto put_err_out; + } +ntfs_attr_init (ni, true, a->flags &ATTR_IS_COMPRESSED , + a->flags &ATTR_IS_ENCRYPTED , + a->flags &ATTR_IS_SPARSE , +le64_to_cpu (a->data.non_resident.allocated_size), +le64_to_cpu (a->data.non_resident.data_size), +le64_to_cpu (a->data.non_resident.initialized_size), + cs ?le64_to_cpu (a->data.non_resident.compressed_size) :0 , + cs ? a->data.non_resident.compression_unit :0 ); + }else { +s64 l =le32_to_cpu (a->data.resident.value_length); + +ntfs_attr_init (ni, false, a->flags &ATTR_IS_COMPRESSED , + a->flags &ATTR_IS_ENCRYPTED , + a->flags &ATTR_IS_SPARSE , (l +7 ) & ~7 , l, l, + cs ? (l +7 ) & ~7 :0 ,0 ); + } +ntfs_attr_put_search_ctx (ctx); +out: +ntfs_debug ("\n" ); +return err; + +put_err_out: +ntfs_attr_put_search_ctx (ctx); +err_out: +ntfs_free (newname); + goto out; +} + +/** +* ntfs_attr_close - free an ntfs attribute structure +* @ni: ntfs inode to free +* +* Release all memory associated with the ntfs attribute @na and then release +* @na itself. +*/ +void ntfs_attr_close (struct ntfs_inode *ni) +{ +if (NInoNonResident (ni) && ni->runlist.rl) +ntfs_free (ni->runlist.rl); +/* Don't release if using an internal constant. */ +if (ni->name !=AT_UNNAMED && ni->name !=I30 ) +ntfs_free (ni->name); +} + +/** +* ntfs_attr_map_whole_runlist - map the whole runlist of an ntfs attribute +* @ni: ntfs inode for which to map the runlist +* +* Map the whole runlist of the ntfs attribute @na. For an attribute made up +* of only one attribute extent this is the same as calling +* ntfs_map_runlist(ni, 0) but for an attribute with multiple extents this +* will map the runlist fragments from each of the extents thus giving access +* to the entirety of the disk allocation of an attribute. +*/ +int ntfs_attr_map_whole_runlist (struct ntfs_inode *ni) +{ +s64 next_vcn, last_vcn, highest_vcn; +struct ntfs_attr_search_ctx *ctx; +struct ntfs_volume *vol = ni->vol; +struct super_block *sb = vol->sb; +struct attr_record *a; +int err; +struct ntfs_inode *base_ni; +int not_mapped; +size_t new_rl_count; + +ntfs_debug ("Entering for inode 0x%llx, attr 0x%x.\n" , + (unsigned long long )ni->mft_no, ni->type); + +if (NInoFullyMapped (ni) && ni->runlist.rl) +return 0 ; + +if (NInoAttr (ni)) + base_ni = ni->ext.base_ntfs_ino; +else + base_ni = ni; + + ctx =ntfs_attr_get_search_ctx (base_ni,NULL ); +if (!ctx) { +ntfs_error (sb,"%s: Failed to get search context" , __func__); +return -ENOMEM ; + } + +/* Map all attribute extents one by one. */ + next_vcn = last_vcn = highest_vcn =0 ; + a =NULL ; +while (1 ) { +struct runlist_element *rl; + + not_mapped =0 ; +if (ntfs_rl_vcn_to_lcn (ni->runlist.rl, next_vcn) ==LCN_RL_NOT_MAPPED ) + not_mapped =1 ; + + err =ntfs_attr_lookup (ni->type, ni->name, ni->name_len, +CASE_SENSITIVE , next_vcn,NULL ,0 , ctx); +if (err) +break ; + + a = ctx->attr; + +if (not_mapped) { +/* Decode the runlist. */ + rl =ntfs_mapping_pairs_decompress (ni->vol, a, &ni->runlist, + &new_rl_count); +if (IS_ERR (rl)) { + err =PTR_ERR (rl); + goto err_out; + } + ni->runlist.rl = rl; + ni->runlist.count = new_rl_count; + } + +/* Are we in the first extent? */ +if (!next_vcn) { +if (a->data.non_resident.lowest_vcn) { + err = -EIO ; +ntfs_error (sb, +"First extent of inode %llu attribute has non-zero lowest_vcn" , + (unsigned long long )ni->mft_no); + goto err_out; + } +/* Get the last vcn in the attribute. */ + last_vcn =le64_to_cpu (a->data.non_resident.allocated_size) >> + vol->cluster_size_bits; + }
Lines of this file are not shown. View the raw message.
fs/ntfsplus/attrlist.c new file +285 -0
285 lines not on this page · raw message
fs/ntfsplus/compress.c new file +1564 -0
1,564 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.25.1
Part of the diff is not shown here. View the raw message.
PATCH v2 8/11 ntfsplus: add runlist handling and cluster allocator
In reply to Namjae JeonThis adds the implementation of runlist handling and cluster allocator for ntfsplus.
--- fs/ntfsplus/bitmap.c | 290 ++++++ fs/ntfsplus/lcnalloc.c | 1012 ++++++++++++++++++++ fs/ntfsplus/runlist.c | 1983 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 3285 insertions(+) create mode 100644 fs/ntfsplus/bitmap.c create mode 100644 fs/ntfsplus/lcnalloc.c create mode 100644 fs/ntfsplus/runlist.c
fs/ntfsplus/bitmap.c new file +290 -0
@@ -0,0 +1,290 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* NTFS kernel bitmap handling. Part of the Linux-NTFS project. +* +* Copyright (c) 2004-2005 Anton Altaparmakov +* Copyright (c) 2025 LG Electronics Co., Ltd. +*/ + +#include <linux/bitops.h> + +#include "bitmap.h" +#include "aops.h" +#include "ntfs.h" + +int ntfsp_trim_fs (struct ntfs_volume *vol,struct fstrim_range *range) +{ +size_t buf_clusters; +pgoff_t index, start_index, end_index; +struct file_ra_state *ra; +struct folio *folio; +unsigned long *bitmap; +char *kaddr; +u64 end, trimmed =0 , start_buf, end_buf, end_cluster; +u64 start_cluster = range->start >> vol->cluster_size_bits; +u32 dq =bdev_discard_granularity (vol->sb->s_bdev); +int ret =0 ; + +if (!dq) + dq = vol->cluster_size; + +if (start_cluster >= vol->nr_clusters) +return -EINVAL ; + +if (range->len == (u64)-1 ) + end_cluster = vol->nr_clusters; +else { + end_cluster = (range->start + range->len + vol->cluster_size -1 ) >> + vol->cluster_size_bits; +if (end_cluster > vol->nr_clusters) + end_cluster = vol->nr_clusters; + } + + ra =kzalloc (sizeof (*ra),GFP_NOFS ); +if (!ra) +return -ENOMEM ; + + buf_clusters =PAGE_SIZE *8 ; + start_index = start_cluster >>15 ; + end_index = (end_cluster + buf_clusters -1 ) >>15 ; + +for (index = start_index; index < end_index; index++) { + folio =filemap_lock_folio (vol->lcnbmp_ino->i_mapping, index); +if (IS_ERR (folio)) { +page_cache_sync_readahead (vol->lcnbmp_ino->i_mapping, ra,NULL , + index, end_index - index); + folio =ntfs_read_mapping_folio (vol->lcnbmp_ino->i_mapping, index); +if (!IS_ERR (folio)) +folio_lock (folio); + } +if (IS_ERR (folio)) { + ret =PTR_ERR (folio); + goto out_free; + } + + kaddr =kmap_local_folio (folio,0 ); + bitmap = (unsigned long *)kaddr; + + start_buf =max_t (u64, index * buf_clusters, start_cluster); + end_buf =min_t (u64, (index +1 ) * buf_clusters, end_cluster); + + end = start_buf; +while (end < end_buf) { +u64 aligned_start, aligned_count; +u64 start =find_next_zero_bit (bitmap, end_buf - start_buf, + end - start_buf) + start_buf; +if (start >= end_buf) +break ; + + end =find_next_bit (bitmap, end_buf - start_buf, + start - start_buf) + start_buf; + + aligned_start =ALIGN (start << vol->cluster_size_bits, dq); + aligned_count =ALIGN_DOWN ((end - start) << vol->cluster_size_bits, dq); +if (aligned_count >= range->minlen) { + ret =blkdev_issue_discard (vol->sb->s_bdev, aligned_start >>9 , + aligned_count >>9 ,GFP_NOFS ); +if (ret) + goto out_unmap; + trimmed += aligned_count; + } + } + +out_unmap: +kunmap_local (kaddr); +folio_unlock (folio); +folio_put (folio); + +if (ret) + goto out_free; + } + + range->len = trimmed; + +out_free: +kfree (ra); +return ret; +} + +/** +* __ntfs_bitmap_set_bits_in_run - set a run of bits in a bitmap to a value +* @vi: vfs inode describing the bitmap +* @start_bit: first bit to set +* @count: number of bits to set +* @value: value to set the bits to (i.e. 0 or 1) +* @is_rollback: if 'true' this is a rollback operation +* +* Set @count bits starting at bit @start_bit in the bitmap described by the +* vfs inode @vi to @value, where @value is either 0 or 1. +* +* @is_rollback should always be 'false', it is for internal use to rollback +* errors. You probably want to use ntfs_bitmap_set_bits_in_run() instead. +*/ +int __ntfs_bitmap_set_bits_in_run (struct inode *vi,const s64 start_bit, +const s64 count,const u8 value,const bool is_rollback) +{ +s64 cnt = count; +pgoff_t index, end_index; +struct address_space *mapping; +struct folio *folio; +u8 *kaddr; +int pos, len; +u8 bit; +struct ntfs_inode *ni =NTFS_I (vi); +struct ntfs_volume *vol = ni->vol; + +ntfs_debug ("Entering for i_ino 0x%lx, start_bit 0x%llx, count 0x%llx, value %u.%s" , + vi->i_ino, (unsigned long long )start_bit, + (unsigned long long )cnt, (unsigned int )value, + is_rollback ?" (rollback)" :"" ); + +if (start_bit <0 || cnt <0 || value >1 ) +return -EINVAL ; + +/* +* Calculate the indices for the pages containing the first and last +* bits, i.e. @start_bit and @start_bit + @cnt - 1, respectively. +*/ + index = start_bit >> (3 +PAGE_SHIFT ); + end_index = (start_bit + cnt -1 ) >> (3 +PAGE_SHIFT ); + +/* Get the page containing the first bit (@start_bit). */ + mapping = vi->i_mapping; + folio =ntfs_read_mapping_folio (mapping, index); +if (IS_ERR (folio)) { +if (!is_rollback) +ntfs_error (vi->i_sb, +"Failed to map first page (error %li), aborting." , +PTR_ERR (folio)); +return PTR_ERR (folio); + } + +folio_lock (folio); + kaddr =kmap_local_folio (folio,0 ); + +/* Set @pos to the position of the byte containing @start_bit. */ + pos = (start_bit >>3 ) & ~PAGE_MASK ; + +/* Calculate the position of @start_bit in the first byte. */ + bit = start_bit &7 ; + +/* If the first byte is partial, modify the appropriate bits in it. */ +if (bit) { +u8 *byte = kaddr + pos; + +if (ni->mft_no == FILE_Bitmap) +ntfs_set_lcn_empty_bits (vol, index, value,min_t (s64,8 - bit, cnt)); +while ((bit &7 ) && cnt) { + cnt--; +if (value) + *byte |=1 << bit++; +else + *byte &= ~(1 << bit++); + } +/* If we are done, unmap the page and return success. */ +if (!cnt) + goto done; + +/* Update @pos to the new position. */ + pos++; + } +/* +* Depending on @value, modify all remaining whole bytes in the page up +* to @cnt. +*/ + len =min_t (s64, cnt >>3 ,PAGE_SIZE - pos); +memset (kaddr + pos, value ?0xff :0 , len); + cnt -= len <<3 ; +if (ni->mft_no == FILE_Bitmap) +ntfs_set_lcn_empty_bits (vol, index, value, len <<3 ); + +/* Update @len to point to the first not-done byte in the page. */ +if (cnt <8 ) + len += pos; + +/* If we are not in the last page, deal with all subsequent pages. */ +while (index < end_index) { +if (cnt <=0 ) + goto rollback; + +/* Update @index and get the next folio. */ +flush_dcache_folio (folio); +folio_mark_dirty (folio); +folio_unlock (folio); +ntfs_unmap_folio (folio, kaddr); + folio =ntfs_read_mapping_folio (mapping, ++index); +if (IS_ERR (folio)) { +ntfs_error (vi->i_sb, +"Failed to map subsequent page (error %li), aborting." , +PTR_ERR (folio)); + goto rollback; + } + +folio_lock (folio); + kaddr =kmap_local_folio (folio,0 ); +/* +* Depending on @value, modify all remaining whole bytes in the +* page up to @cnt. +*/ + len =min_t (s64, cnt >>3 ,PAGE_SIZE ); +memset (kaddr, value ?0xff :0 , len); + cnt -= len <<3 ; +if (ni->mft_no == FILE_Bitmap) +ntfs_set_lcn_empty_bits (vol, index, value, len <<3 ); + } +/* +* The currently mapped page is the last one. If the last byte is +* partial, modify the appropriate bits in it. Note, @len is the +* position of the last byte inside the page. +*/ +if (cnt) { +u8 *byte; + +WARN_ON (cnt >7 ); + + bit = cnt; + byte = kaddr + len; +if (ni->mft_no == FILE_Bitmap) +ntfs_set_lcn_empty_bits (vol, index, value, bit); +while (bit--) { +if (value) + *byte |=1 << bit; +else + *byte &= ~(1 << bit); + } + } +done: +/* We are done. Unmap the folio and return success. */ +flush_dcache_folio (folio); +folio_mark_dirty (folio); +folio_unlock (folio); +ntfs_unmap_folio (folio, kaddr); +ntfs_debug ("Done." ); +return 0 ; +rollback: +/* +* Current state: +* - no pages are mapped +* - @count - @cnt is the number of bits that have been modified +*/ +if (is_rollback) +return PTR_ERR (folio); +if (count != cnt) + pos =__ntfs_bitmap_set_bits_in_run (vi, start_bit, count - cnt, + value ?0 :1 , true); +else + pos =0 ; +if (!pos) { +/* Rollback was successful. */ +ntfs_error (vi->i_sb, +"Failed to map subsequent page (error %li), aborting." , +PTR_ERR (folio)); + }else { +/* Rollback failed. */ +ntfs_error (vi->i_sb, +"Failed to map subsequent page (error %li) and rollback failed (error %i). Aborting and leaving inconsistent metadata. Unmount and run chkdsk." , +PTR_ERR (folio), pos); +NVolSetErrors (NTFS_SB (vi->i_sb)); + } +return PTR_ERR (folio); +}
fs/ntfsplus/lcnalloc.c new file +1012 -0
1,012 lines not on this page · raw message
fs/ntfsplus/runlist.c new file +1983 -0
1,698 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.25.1
Part of the diff is not shown here. View the raw message.
PATCH v2 9/11 ntfsplus: add reparse and ea operations
In reply to Namjae JeonThis adds the implementation of reparse and ea operations for ntfsplus.
--- fs/ntfsplus/ea.c | 931 ++++++++++++++++++++++++++++++++++++++++++ fs/ntfsplus/reparse.c | 550 +++++++++++++++++++++++++ 2 files changed, 1481 insertions(+) create mode 100644 fs/ntfsplus/ea.c create mode 100644 fs/ntfsplus/reparse.c
fs/ntfsplus/ea.c new file +931 -0
@@ -0,0 +1,931 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** +* Pocessing of EA's +* +* Part of this file is based on code from the NTFS-3G project. +* +* Copyright (c) 2014-2021 Jean-Pierre Andre +* Copyright (c) 2025 LG Electronics Co., Ltd. +*/ + +#include <linux/fs.h> +#include <linux/posix_acl.h> +#include <linux/posix_acl_xattr.h> +#include <linux/xattr.h> + +#include "layout.h" +#include "attrib.h" +#include "index.h" +#include "dir.h" +#include "ea.h" +#include "misc.h" + +static int ntfs_write_ea (struct ntfs_inode *ni,int type,char *value,s64 ea_off, +s64 ea_size,bool need_truncate) +{ +struct inode *ea_vi; +int err =0 ; +s64 written; + + ea_vi =ntfs_attr_iget (VFS_I (ni), type,AT_UNNAMED ,0 ); +if (IS_ERR (ea_vi)) +return PTR_ERR (ea_vi); + + written =ntfs_inode_attr_pwrite (ea_vi, ea_off, ea_size, value, false); +if (written != ea_size) + err = -EIO ; +else { +struct ntfs_inode *ea_ni =NTFS_I (ea_vi); + +if (need_truncate && ea_ni->data_size > ea_off + ea_size) +ntfs_attr_truncate (ea_ni, ea_off + ea_size); +mark_mft_record_dirty (ni); + } + +iput (ea_vi); +return err; +} + +static int ntfs_ea_lookup (char *ea_buf,s64 ea_buf_size,const char *name, +int name_len,s64 *ea_offset,s64 *ea_size) +{ +const struct ea_attr *p_ea; +s64 offset; +unsigned int next; + +if (ea_buf_size <sizeof (struct ea_attr )) + goto out; + + offset =0 ; +do { + p_ea = (const struct ea_attr *)&ea_buf[offset]; + next =le32_to_cpu (p_ea->next_entry_offset); + +if (offset + next > ea_buf_size || + ((1 + p_ea->ea_name_length) > (ea_buf_size - offset))) +break ; + +if (p_ea->ea_name_length == name_len && + !memcmp (p_ea->ea_name, name, name_len)) { + *ea_offset = offset; +if (next) + *ea_size = next; +else { +unsigned int ea_len =1 + p_ea->ea_name_length + +le16_to_cpu (p_ea->ea_value_length); + +if ((ea_buf_size - offset) < ea_len) + goto out; + + *ea_size =ALIGN (struct_size (p_ea, ea_name, +1 + p_ea->ea_name_length + +le16_to_cpu (p_ea->ea_value_length)),4 ); + } + +if (ea_buf_size < *ea_offset + *ea_size) + goto out; + +return 0 ; + } + offset += next; + }while (next >0 && offset < ea_buf_size && +sizeof (struct ea_attr ) < (ea_buf_size - offset)); + +out: +return -ENOENT ; +} + +/* +* Return the existing EA +* +* The EA_INFORMATION is not examined and the consistency of the +* existing EA is not checked. +* +* If successful, the full attribute is returned unchanged +* and its size is returned. +* If the designated buffer is too small, the needed size is +* returned, and the buffer is left unchanged. +* If there is an error, a negative value is returned and errno +* is set according to the error. +*/ +static int ntfs_get_ea (struct inode *inode,const char *name,size_t name_len, +void *buffer,size_t size) +{ +struct ntfs_inode *ni =NTFS_I (inode); +const struct ea_attr *p_ea; +char *ea_buf; +s64 ea_off, ea_size, all_ea_size, ea_info_size; +int err; +unsigned short int ea_value_len, ea_info_qlen; +struct ea_information *p_ea_info; + +if (!NInoHasEA (ni)) +return -ENODATA ; + + p_ea_info =ntfs_attr_readall (ni,AT_EA_INFORMATION ,NULL ,0 , + &ea_info_size); +if (!p_ea_info || ea_info_size !=sizeof (struct ea_information )) { +ntfs_free (p_ea_info); +return -ENODATA ; + } + + ea_info_qlen =le16_to_cpu (p_ea_info->ea_query_length); +ntfs_free (p_ea_info); + + ea_buf =ntfs_attr_readall (ni,AT_EA ,NULL ,0 , &all_ea_size); +if (!ea_buf) +return -ENODATA ; + + err =ntfs_ea_lookup (ea_buf, ea_info_qlen, name, name_len, &ea_off, + &ea_size); +if (!err) { + p_ea = (struct ea_attr *)&ea_buf[ea_off]; + ea_value_len =le16_to_cpu (p_ea->ea_value_length); +if (!buffer) { +ntfs_free (ea_buf); +return ea_value_len; + } + +if (ea_value_len > size) { + err = -ERANGE ; + goto free_ea_buf; + } + +memcpy (buffer, &p_ea->ea_name[p_ea->ea_name_length +1 ], + ea_value_len); +ntfs_free (ea_buf); +return ea_value_len; + } + + err = -ENODATA ; +free_ea_buf: +ntfs_free (ea_buf); +return err; +} + +static inline int ea_packed_size (const struct ea_attr *p_ea) +{ +/* +* 4 bytes for header (flags and lengths) + name length + 1 + +* value length. +*/ +return 5 + p_ea->ea_name_length +le16_to_cpu (p_ea->ea_value_length); +} + +/* +* Set a new EA, and set EA_INFORMATION accordingly +* +* This is roughly the same as ZwSetEaFile() on Windows, however +* the "offset to next" of the last EA should not be cleared. +* +* Consistency of the new EA is first checked. +* +* EA_INFORMATION is set first, and it is restored to its former +* state if setting EA fails. +*/ +static int ntfs_set_ea (struct inode *inode,const char *name,size_t name_len, +const void *value,size_t val_size,int flags, +__le16 *packed_ea_size) +{ +struct ntfs_inode *ni =NTFS_I (inode); +struct ea_information *p_ea_info =NULL ; +int ea_packed, err =0 ; +struct ea_attr *p_ea; +unsigned short int ea_info_qsize; +char *ea_buf =NULL ; +size_t new_ea_size =ALIGN (struct_size (p_ea, ea_name,1 + name_len + val_size),4 ); +s64 ea_off, ea_info_size, all_ea_size, ea_size; + +if (name_len >255 ) +return -ENAMETOOLONG ; + +if (ntfs_attr_exist (ni,AT_EA_INFORMATION ,AT_UNNAMED ,0 )) { + p_ea_info =ntfs_attr_readall (ni,AT_EA_INFORMATION ,NULL ,0 , + &ea_info_size); +if (!p_ea_info || ea_info_size !=sizeof (struct ea_information )) + goto out; + + ea_buf =ntfs_attr_readall (ni,AT_EA ,NULL ,0 , &all_ea_size); +if (!ea_buf) { + ea_info_qsize =0 ; +ntfs_free (p_ea_info); + goto create_ea_info; + } + + ea_info_qsize =le32_to_cpu (p_ea_info->ea_query_length); + }else { +create_ea_info: + p_ea_info =ntfs_malloc_nofs (sizeof (struct ea_information )); +if (!p_ea_info) +return -ENOMEM ; + + ea_info_qsize =0 ; + err =ntfs_attr_add (ni,AT_EA_INFORMATION ,AT_UNNAMED ,0 , + (char *)p_ea_info,sizeof (struct ea_information )); +if (err) + goto out; + +if (ntfs_attr_exist (ni,AT_EA ,AT_UNNAMED ,0 )) { + err =ntfs_attr_remove (ni,AT_EA ,AT_UNNAMED ,0 ); +if (err) + goto out; + } + + goto alloc_new_ea; + } + +if (ea_info_qsize > all_ea_size) { + err = -EIO ; + goto out; + } + + err =ntfs_ea_lookup (ea_buf, ea_info_qsize, name, name_len, &ea_off, + &ea_size); +if (ea_info_qsize && !err) { +if (flags &XATTR_CREATE ) { + err = -EEXIST ; + goto out; + } + + p_ea = (struct ea_attr *)(ea_buf + ea_off); + +if (val_size && +le16_to_cpu (p_ea->ea_value_length) == val_size && + !memcmp (p_ea->ea_name + p_ea->ea_name_length +1 , value, + val_size)) + goto out; + +le16_add_cpu (&p_ea_info->ea_length,0 -ea_packed_size (p_ea)); + +if (p_ea->flags &NEED_EA ) +le16_add_cpu (&p_ea_info->need_ea_count,-1 ); + +memmove ((char *)p_ea, (char *)p_ea + ea_size, ea_info_qsize - (ea_off + ea_size)); + ea_info_qsize -= ea_size; + p_ea_info->ea_query_length =cpu_to_le16 (ea_info_qsize); + + err =ntfs_write_ea (ni,AT_EA_INFORMATION , (char *)p_ea_info,0 , +sizeof (struct ea_information ), false); +if (err) + goto out; + + err =ntfs_write_ea (ni,AT_EA , ea_buf,0 , ea_info_qsize, true); +if (err) + goto out; + +if ((flags &XATTR_REPLACE ) && !val_size) { +/* Remove xattr. */ + goto out; + } + }else { +if (flags &XATTR_REPLACE ) { + err = -ENODATA ; + goto out; + } + } +ntfs_free (ea_buf); + +alloc_new_ea: + ea_buf =kzalloc (new_ea_size,GFP_NOFS ); +if (!ea_buf) { + err = -ENOMEM ; + goto out; + } + +/* +* EA and REPARSE_POINT compatibility not checked any more, +* required by Windows 10, but having both may lead to +* problems with earlier versions. +*/ + p_ea = (struct ea_attr *)ea_buf; +memcpy (p_ea->ea_name, name, name_len); + p_ea->ea_name_length = name_len; + p_ea->ea_name[name_len] =0 ; +memcpy (p_ea->ea_name + name_len +1 , value, val_size); + p_ea->ea_value_length =cpu_to_le16 (val_size); + p_ea->next_entry_offset =cpu_to_le32 (new_ea_size); + + ea_packed =le16_to_cpu (p_ea_info->ea_length) +ea_packed_size (p_ea); + p_ea_info->ea_length =cpu_to_le16 (ea_packed); + p_ea_info->ea_query_length =cpu_to_le32 (ea_info_qsize + new_ea_size); + +if (ea_packed >0xffff || +ntfs_attr_size_bounds_check (ni->vol,AT_EA , new_ea_size)) { + err = -EFBIG ; + goto out; + } + +/* +* no EA or EA_INFORMATION : add them +*/ +if (!ntfs_attr_exist (ni,AT_EA ,AT_UNNAMED ,0 )) { + err =ntfs_attr_add (ni,AT_EA ,AT_UNNAMED ,0 , (char *)p_ea, + new_ea_size); +if (err) + goto out; + }else { + err =ntfs_write_ea (ni,AT_EA , (char *)p_ea, ea_info_qsize, + new_ea_size, false); +if (err) + goto out; + } + + err =ntfs_write_ea (ni,AT_EA_INFORMATION , (char *)p_ea_info,0 , +sizeof (struct ea_information ), false); +if (err) + goto out; + +if (packed_ea_size) + *packed_ea_size = p_ea_info->ea_length; +mark_mft_record_dirty (ni); +out: +if (ea_info_qsize >0 ) +NInoSetHasEA (ni); +else +NInoClearHasEA (ni); + +ntfs_free (ea_buf); +ntfs_free (p_ea_info); + +return err; +} + +/* +* Check for the presence of an EA "$LXDEV" (used by WSL) +* and return its value as a device address +*/ +int ntfs_ea_get_wsl_inode (struct inode *inode,dev_t *rdevp,unsigned int flags) +{ +int err; +__le32 v; + +if (!(flags &NTFS_VOL_UID )) { +/* Load uid to lxuid EA */ + err =ntfs_get_ea (inode,"$LXUID" ,sizeof ("$LXUID" ) -1 , &v, +sizeof (v)); +if (err <0 ) +return err; +i_uid_write (inode,le32_to_cpu (v)); + } + +if (!(flags &NTFS_VOL_UID )) { +/* Load gid to lxgid EA */ + err =ntfs_get_ea (inode,"$LXGID" ,sizeof ("$LXGID" ) -1 , &v, +sizeof (v)); +if (err <0 ) +return err; +i_gid_write (inode,le32_to_cpu (v)); + } + +/* Load mode to lxmod EA */ + err =ntfs_get_ea (inode,"$LXMOD" ,sizeof ("$LXMOD" ) -1 , &v,sizeof (v)); +if (err >0 ) { + inode->i_mode =le32_to_cpu (v); + }else { +/* Everyone gets all permissions. */ + inode->i_mode |=0777 ; + } + +/* Load mode to lxdev EA */ + err =ntfs_get_ea (inode,"$LXDEV" ,sizeof ("$LXDEV" ) -1 , &v,sizeof (v)); +if (err >0 ) + *rdevp =le32_to_cpu (v); + err =0 ; + +return err; +} + +int ntfs_ea_set_wsl_inode (struct inode *inode,dev_t rdev,__le16 *ea_size, +unsigned int flags) +{ +__le32 v; +int err; + +if (flags &NTFS_EA_UID ) { +/* Store uid to lxuid EA */ + v =cpu_to_le32 (i_uid_read (inode)); + err =ntfs_set_ea (inode,"$LXUID" ,sizeof ("$LXUID" ) -1 , &v, +sizeof (v),0 , ea_size); +if (err) +return err; + } + +if (flags &NTFS_EA_GID ) { +/* Store gid to lxgid EA */ + v =cpu_to_le32 (i_gid_read (inode)); + err =ntfs_set_ea (inode,"$LXGID" ,sizeof ("$LXGID" ) -1 , &v, +sizeof (v),0 , ea_size); +if (err) +return err; + } + +if (flags &NTFS_EA_MODE ) { +/* Store mode to lxmod EA */ + v =cpu_to_le32 (inode->i_mode); + err =ntfs_set_ea (inode,"$LXMOD" ,sizeof ("$LXMOD" ) -1 , &v, +sizeof (v),0 , ea_size); +if (err) +return err; + } + +if (rdev) { + v =cpu_to_le32 (rdev); + err =ntfs_set_ea (inode,"$LXDEV" ,sizeof ("$LXDEV" ) -1 , &v,sizeof (v), +0 , ea_size); + } + +return err; +} + +ssize_t ntfsp_listxattr (struct dentry *dentry,char *buffer,size_t size) +{ +struct inode *inode =d_inode (dentry); +struct ntfs_inode *ni =NTFS_I (inode); +const struct ea_attr *p_ea; +s64 offset, ea_buf_size, ea_info_size; +int next, err =0 , ea_size; +unsigned int ea_info_qsize; +char *ea_buf =NULL ; +ssize_t ret =0 ; +struct ea_information *ea_info; + +if (!NInoHasEA (ni)) +return 0 ; + +mutex_lock (&NTFS_I (inode)->mrec_lock); + ea_info =ntfs_attr_readall (ni,AT_EA_INFORMATION ,NULL ,0 , + &ea_info_size); +if (!ea_info || ea_info_size !=sizeof (struct ea_information )) + goto out; + + ea_info_qsize =le16_to_cpu (ea_info->ea_query_length); + + ea_buf =ntfs_attr_readall (ni,AT_EA ,NULL ,0 , &ea_buf_size); +if (!ea_buf) + goto out; + +if (ea_info_qsize > ea_buf_size) + goto out; + +if (ea_buf_size <sizeof (struct ea_attr )) + goto out; + + offset =0 ; +do { + p_ea = (const struct ea_attr *)&ea_buf[offset]; + next =le32_to_cpu (p_ea->next_entry_offset); +if (next) + ea_size = next; +else + ea_size =ALIGN (struct_size (p_ea, ea_name, +1 + p_ea->ea_name_length + +le16_to_cpu (p_ea->ea_value_length)), +4 ); +if (buffer) { +if (offset + ea_size > ea_info_qsize) +break ; + +if (ret + p_ea->ea_name_length +1 > size) { + err = -ERANGE ; + goto out; + } + +if (p_ea->ea_name_length +1 > (ea_info_qsize - offset)) +break ; + +memcpy (buffer + ret, p_ea->ea_name, p_ea->ea_name_length); + buffer[ret + p_ea->ea_name_length] =0 ; + } + + ret += p_ea->ea_name_length +1 ; + offset += ea_size; + }while (next >0 && offset < ea_info_qsize && +sizeof (struct ea_attr ) < (ea_info_qsize - offset)); + +out: +mutex_unlock (&NTFS_I (inode)->mrec_lock); +ntfs_free (ea_info); +ntfs_free (ea_buf); + +return err ? err : ret; +} + +// clang-format off +#define SYSTEM_DOS_ATTRIB "system.dos_attrib" +#define SYSTEM_NTFS_ATTRIB "system.ntfs_attrib" +#define SYSTEM_NTFS_ATTRIB_BE "system.ntfs_attrib_be" +// clang-format on + +static int ntfs_getxattr (const struct xattr_handler *handler, +struct dentry *unused,struct inode *inode,const char *name, +void *buffer,size_t size) +{ +struct ntfs_inode *ni =NTFS_I (inode); +int err; + +if (NVolShutdown (ni->vol)) +return -EIO ; + +if (!strcmp (name,SYSTEM_DOS_ATTRIB )) { +if (!buffer) { + err =sizeof (u8); + }else if (size <sizeof (u8)) { + err = -ENODATA ; + }else { + err =sizeof (u8); + *(u8 *)buffer = ni->flags; + } + goto out; + } + +if (!strcmp (name,SYSTEM_NTFS_ATTRIB ) || + !strcmp (name,SYSTEM_NTFS_ATTRIB_BE )) { +if (!buffer) { + err =sizeof (u32); + }else if (size <sizeof (u32)) { + err = -ENODATA ; + }else { + err =sizeof (u32); + *(u32 *)buffer =le32_to_cpu (ni->flags); +if (!strcmp (name,SYSTEM_NTFS_ATTRIB_BE )) + *(__be32 *)buffer =cpu_to_be32 (*(u32 *)buffer); + } + goto out; + } + +mutex_lock (&ni->mrec_lock); + err =ntfs_get_ea (inode, name,strlen (name), buffer, size); +mutex_unlock (&ni->mrec_lock); + +out: +return err; +} + +static int ntfs_new_attr_flags (struct ntfs_inode *ni,__le32 fattr) +{ +struct ntfs_attr_search_ctx *ctx; +struct mft_record *m; +struct attr_record *a; +__le16 new_aflags; +int mp_size, mp_ofs, name_ofs, arec_size, err; + + m =map_mft_record (ni); +if (IS_ERR (m)) +return PTR_ERR (m); + + ctx =ntfs_attr_get_search_ctx (ni, m); +if (!ctx) { + err = -ENOMEM ; + goto err_out; + } + + err =ntfs_attr_lookup (ni->type, ni->name, ni->name_len, +CASE_SENSITIVE ,0 ,NULL ,0 , ctx); +if (err) { + err = -EINVAL ; + goto err_out; + } + + a = ctx->attr; + new_aflags = ctx->attr->flags; + +if (fattr &FILE_ATTR_SPARSE_FILE ) + new_aflags |=ATTR_IS_SPARSE ; +else + new_aflags &= ~ATTR_IS_SPARSE ; + +if (fattr &FILE_ATTR_COMPRESSED ) + new_aflags |=ATTR_IS_COMPRESSED ; +else + new_aflags &= ~ATTR_IS_COMPRESSED ; + +if (new_aflags == a->flags) +return 0 ; + +if ((new_aflags & (ATTR_IS_SPARSE |ATTR_IS_COMPRESSED )) == + (ATTR_IS_SPARSE |ATTR_IS_COMPRESSED )) { +pr_err ("file can't be sparsed and compressed\n" ); + err = -EOPNOTSUPP ; + goto err_out; + } + +if (!a->non_resident) + goto out; + +if (a->data.non_resident.data_size) { +pr_err ("Can't change sparsed/compressed for non-empty file" ); + err = -EOPNOTSUPP ; + goto err_out; + } + +if (new_aflags & (ATTR_IS_SPARSE |ATTR_IS_COMPRESSED )) + name_ofs = (offsetof(struct attr_record , + data.non_resident.compressed_size) + + sizeof(a->data.non_resident.compressed_size) + 7) & ~7; +else + name_ofs = (offsetof(struct attr_record , + data.non_resident.compressed_size) + 7) & ~7; + + mp_size =ntfs_get_size_for_mapping_pairs (ni->vol, ni->runlist.rl,0 ,-1 ,-1 ); +if (unlikely (mp_size <0 )) { + err = mp_size; +ntfs_debug ("Failed to get size for mapping pairs array, error code %i.\n" , err); + goto err_out; + } + + mp_ofs = (name_ofs + a->name_length *sizeof (__le16) +7 ) & ~7 ; + arec_size = (mp_ofs + mp_size +7 ) & ~7 ; + + err =ntfs_attr_record_resize (m, a, arec_size); +if (unlikely (err)) + goto err_out; + +if (new_aflags & (ATTR_IS_SPARSE |ATTR_IS_COMPRESSED )) { + a->data.non_resident.compression_unit =0 ; +if (new_aflags &ATTR_IS_COMPRESSED || ni->vol->major_ver <3 ) + a->data.non_resident.compression_unit =4 ; + a->data.non_resident.compressed_size =0 ; + ni->itype.compressed.size =0 ; +if (a->data.non_resident.compression_unit) { + ni->itype.compressed.block_size =1U << + (a->data.non_resident.compression_unit + + ni->vol->cluster_size_bits); + ni->itype.compressed.block_size_bits = +ffs (ni->itype.compressed.block_size) - +1 ; + ni->itype.compressed.block_clusters =1U << + a->data.non_resident.compression_unit; + }else { + ni->itype.compressed.block_size =0 ; + ni->itype.compressed.block_size_bits =0 ; + ni->itype.compressed.block_clusters =0 ; + } + +if (new_aflags &ATTR_IS_SPARSE ) { +NInoSetSparse (ni); + ni->flags |=FILE_ATTR_SPARSE_FILE ; + } + +if (new_aflags &ATTR_IS_COMPRESSED ) { +NInoSetCompressed (ni); + ni->flags |=FILE_ATTR_COMPRESSED ; +VFS_I (ni)->i_mapping->a_ops = &ntfs_compressed_aops; + } + }else { + ni->flags &= ~(FILE_ATTR_SPARSE_FILE |FILE_ATTR_COMPRESSED ); + a->data.non_resident.compression_unit =0 ; +VFS_I (ni)->i_mapping->a_ops = &ntfs_normal_aops; +NInoClearSparse (ni); +NInoClearCompressed (ni); + } + + a->name_offset =cpu_to_le16 (name_ofs); + a->data.non_resident.mapping_pairs_offset =cpu_to_le16 (mp_ofs); + +out: + a->flags = new_aflags; +mark_mft_record_dirty (ctx->ntfs_ino); +err_out: +ntfs_attr_put_search_ctx (ctx); +unmap_mft_record (ni); +return err; +} + +static int ntfs_setxattr (const struct xattr_handler *handler, +struct mnt_idmap *idmap,struct dentry *unused, +struct inode *inode,const char *name,const void *value, +size_t size,int flags) +{ +struct ntfs_inode *ni =NTFS_I (inode); +int err; +__le32 fattr; + +if (NVolShutdown (ni->vol)) +return -EIO ; + +if (!strcmp (name,SYSTEM_DOS_ATTRIB )) { +if (sizeof (u8) != size) + goto out; + fattr =cpu_to_le32 (*(u8 *)value); + goto set_fattr; + } + +if (!strcmp (name,SYSTEM_NTFS_ATTRIB ) || + !strcmp (name,SYSTEM_NTFS_ATTRIB_BE )) { +if (size !=sizeof (u32)) + goto out; +if (!strcmp (name,SYSTEM_NTFS_ATTRIB_BE )) + fattr =cpu_to_le32 (be32_to_cpu (*(__be32 *)value)); +else + fattr =cpu_to_le32 (*(u32 *)value); + +if (S_ISREG (inode->i_mode)) { +mutex_lock (&ni->mrec_lock); + err =ntfs_new_attr_flags (ni, fattr); +mutex_unlock (&ni->mrec_lock); +if (err) + goto out; + } + +set_fattr: +if (S_ISDIR (inode->i_mode)) + fattr |=FILE_ATTR_DIRECTORY ; +else + fattr &= ~FILE_ATTR_DIRECTORY ; + +if (ni->flags != fattr) { + ni->flags = fattr; +if (fattr &FILE_ATTR_READONLY ) + inode->i_mode &= ~0222 ; +else + inode->i_mode |=0222 ; +NInoSetFileNameDirty (ni); +mark_inode_dirty (inode); + } + err =0 ; + goto out; + } + +mutex_lock (&ni->mrec_lock); + err =ntfs_set_ea (inode, name,strlen (name), value, size, flags,NULL ); +mutex_unlock (&ni->mrec_lock); + +out: +inode_set_ctime_current (inode); +mark_inode_dirty (inode); +return err; +} + +static bool ntfs_xattr_user_list (struct dentry *dentry) +{ +return true; +} + +// clang-format off +static const struct xattr_handler ntfs_other_xattr_handler = { + .prefix ="" , + .get = ntfs_getxattr, + .set = ntfs_setxattr, + .list = ntfs_xattr_user_list, +}; + +const struct xattr_handler *const ntfsp_xattr_handlers[] = { + &ntfs_other_xattr_handler, +NULL , +}; +// clang-format on + +#ifdef CONFIG_NTFSPLUS_FS_POSIX_ACL +struct posix_acl *ntfsp_get_acl (struct mnt_idmap *idmap,struct dentry *dentry, +int type) +{ +struct inode *inode =d_inode (dentry); +struct ntfs_inode *ni =NTFS_I (inode); +const char *name; +size_t name_len; +struct posix_acl *acl; +int err; +void *buf; + +/* Allocate PATH_MAX bytes. */ + buf =__getname (); +if (!buf) +return ERR_PTR (-ENOMEM ); + +/* Possible values of 'type' was already checked above. */ +if (type ==ACL_TYPE_ACCESS ) { + name =XATTR_NAME_POSIX_ACL_ACCESS ; + name_len =sizeof (XATTR_NAME_POSIX_ACL_ACCESS ) -1 ; + }else { + name =XATTR_NAME_POSIX_ACL_DEFAULT ; + name_len =sizeof (XATTR_NAME_POSIX_ACL_DEFAULT ) -1 ; + } + +mutex_lock (&ni->mrec_lock); + err =ntfs_get_ea (inode, name, name_len, buf,PATH_MAX ); +mutex_unlock (&ni->mrec_lock); + +/* Translate extended attribute to acl. */ +if (err >=0 ) + acl =posix_acl_from_xattr (&init_user_ns, buf, err); +else if (err == -ENODATA ) + acl =NULL ; +else + acl =ERR_PTR (err); + +if (!IS_ERR (acl)) +set_cached_acl (inode, type, acl); + +__putname (buf); + +return acl; +} + +static noinline int ntfs_set_acl_ex(struct mnt_idmap *idmap, +struct inode *inode,struct posix_acl *acl, +int type,bool init_acl) +{ +const char *name; +size_t size, name_len; +void *value; +int err; +int flags; +umode_t mode; + +if (S_ISLNK (inode->i_mode)) +return -EOPNOTSUPP ; + + mode = inode->i_mode; +switch (type) { +case ACL_TYPE_ACCESS : +/* Do not change i_mode if we are in init_acl */ +if (acl && !init_acl) { + err =posix_acl_update_mode (idmap, inode, &mode, &acl); +if (err) +return err; + } + name =XATTR_NAME_POSIX_ACL_ACCESS ; + name_len =sizeof (XATTR_NAME_POSIX_ACL_ACCESS ) -1 ; +break ; + +case ACL_TYPE_DEFAULT : +if (!S_ISDIR (inode->i_mode)) +return acl ? -EACCES :0 ; + name =XATTR_NAME_POSIX_ACL_DEFAULT ; + name_len =sizeof (XATTR_NAME_POSIX_ACL_DEFAULT ) -1 ; +break ; + +default : +return -EINVAL ; + } + +if (!acl) { +/* Remove xattr if it can be presented via mode. */ + size =0 ; + value =NULL ; + flags =XATTR_REPLACE ; + }else { + size =posix_acl_xattr_size (acl->a_count); + value =kmalloc (size,GFP_NOFS ); +if (!value) +return -ENOMEM ; + err =posix_acl_to_xattr (&init_user_ns, acl, value, size); +if (err <0 ) + goto out; + flags =0 ; + } + +mutex_lock (&NTFS_I (inode)->mrec_lock); + err =ntfs_set_ea (inode, name, name_len, value, size, flags,NULL ); +mutex_unlock (&NTFS_I (inode)->mrec_lock); +if (err == -ENODATA && !size) + err =0 ;/* Removing non existed xattr. */ +if (!err) { +set_cached_acl (inode, type, acl); + inode->i_mode = mode; +inode_set_ctime_current (inode); +mark_inode_dirty (inode); + } + +out: +kfree (value); + +return err; +} + +int ntfsp_set_acl (struct mnt_idmap *idmap,struct dentry *dentry, +struct posix_acl *acl,int type) +{ +return ntfs_set_acl_ex (idmap,d_inode (dentry), acl, type, false); +} + +int ntfsp_init_acl (struct mnt_idmap *idmap,struct inode *inode, +struct inode *dir) +{ +struct posix_acl *default_acl, *acl; +int err; + + err =posix_acl_create (dir, &inode->i_mode, &default_acl, &acl); +if (err) +return err; + +if (default_acl) { + err =ntfs_set_acl_ex (idmap, inode, default_acl, +ACL_TYPE_DEFAULT , true); +posix_acl_release (default_acl); + }else { + inode->i_default_acl =NULL ; + } + +if (acl) { +if (!err) + err =ntfs_set_acl_ex (idmap, inode, acl, +ACL_TYPE_ACCESS , true); +posix_acl_release (acl); + }else { + inode->i_acl =NULL ; + } + +return err; +} +#endif
fs/ntfsplus/reparse.c new file +550 -0
550 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v2 10/11 ntfsplus: add misc operations
In reply to Namjae JeonThis adds the implementation of misc operations for ntfsplus.
--- fs/ntfsplus/collate.c | 178 ++++++++++ fs/ntfsplus/logfile.c | 770 ++++++++++++++++++++++++++++++++++++++++++ fs/ntfsplus/misc.c | 213 ++++++++++++ fs/ntfsplus/unistr.c | 473 ++++++++++++++++++++++++++ fs/ntfsplus/upcase.c | 73 ++++ 5 files changed, 1707 insertions(+) create mode 100644 fs/ntfsplus/collate.c create mode 100644 fs/ntfsplus/logfile.c create mode 100644 fs/ntfsplus/misc.c create mode 100644 fs/ntfsplus/unistr.c create mode 100644 fs/ntfsplus/upcase.c
fs/ntfsplus/collate.c new file +178 -0
@@ -0,0 +1,178 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* NTFS kernel collation handling. Part of the Linux-NTFS project. +* +* Copyright (c) 2004 Anton Altaparmakov +* +* Part of this file is based on code from the NTFS-3G project. +* and is copyrighted by the respective authors below: +* Copyright (c) 2004 Anton Altaparmakov +* Copyright (c) 2005 Yura Pakhuchiy +*/ + +#include "collate.h" +#include "misc.h" +#include "ntfs.h" + +static int ntfs_collate_binary (struct ntfs_volume *vol, +const void *data1,const int data1_len, +const void *data2,const int data2_len) +{ +int rc; + +ntfs_debug ("Entering." ); + rc =memcmp (data1, data2,min (data1_len, data2_len)); +if (!rc && (data1_len != data2_len)) { +if (data1_len < data2_len) + rc =-1 ; +else + rc =1 ; + } +ntfs_debug ("Done, returning %i" , rc); +return rc; +} + +static int ntfs_collate_ntofs_ulong (struct ntfs_volume *vol, +const void *data1,const int data1_len, +const void *data2,const int data2_len) +{ +int rc; +u32 d1, d2; + +ntfs_debug ("Entering." ); + +if (data1_len != data2_len || data1_len !=4 ) +return -EINVAL ; + + d1 =le32_to_cpup (data1); + d2 =le32_to_cpup (data2); +if (d1 < d2) + rc =-1 ; +else { +if (d1 == d2) + rc =0 ; +else + rc =1 ; + } +ntfs_debug ("Done, returning %i" , rc); +return rc; +} + +/** +* ntfs_collate_ntofs_ulongs - Which of two le32 arrays should be listed first +* +* Returns: -1, 0 or 1 depending of how the arrays compare +*/ +static int ntfs_collate_ntofs_ulongs (struct ntfs_volume *vol, +const void *data1,const int data1_len, +const void *data2,const int data2_len) +{ +int rc; +int len; +const __le32 *p1, *p2; +u32 d1, d2; + +ntfs_debug ("Entering." ); +if ((data1_len != data2_len) || (data1_len <=0 ) || (data1_len &3 )) { +ntfs_error (vol->sb,"data1_len or data2_len not valid\n" ); +return -1 ; + } + + p1 = (const __le32 *)data1; + p2 = (const __le32 *)data2; + len = data1_len; +do { + d1 =le32_to_cpup (p1); + p1++; + d2 =le32_to_cpup (p2); + p2++; + }while ((d1 == d2) && ((len -=4 ) >0 )); +if (d1 < d2) + rc =-1 ; +else { +if (d1 == d2) + rc =0 ; +else + rc =1 ; + } +ntfs_debug ("Done, returning %i." , rc); +return rc; +} + +/** +* ntfs_collate_file_name - Which of two filenames should be listed first +*/ +static int ntfs_collate_file_name (struct ntfs_volume *vol, +const void *data1,const int __always_unused data1_len, +const void *data2,const int __always_unused data2_len) +{ +int rc; + +ntfs_debug ("Entering.\n" ); + rc =ntfs_file_compare_values (data1, data2,-2 , +IGNORE_CASE , vol->upcase, vol->upcase_len); +if (!rc) + rc =ntfs_file_compare_values (data1, data2, +-2 ,CASE_SENSITIVE , vol->upcase, vol->upcase_len); +ntfs_debug ("Done, returning %i.\n" , rc); +return rc; +} + +typedef int (*ntfs_collate_func_t )(struct ntfs_volume *,const void *,const int , +const void *,const int ); + +static ntfs_collate_func_t ntfs_do_collate0x0[3 ] = { + ntfs_collate_binary, + ntfs_collate_file_name, +NULL /*ntfs_collate_unicode_string*/ , +}; + +static ntfs_collate_func_t ntfs_do_collate0x1[4 ] = { + ntfs_collate_ntofs_ulong, +NULL /*ntfs_collate_ntofs_sid*/ , +NULL /*ntfs_collate_ntofs_security_hash*/ , + ntfs_collate_ntofs_ulongs, +}; + +/** +* ntfs_collate - collate two data items using a specified collation rule +* @vol: ntfs volume to which the data items belong +* @cr: collation rule to use when comparing the items +* @data1: first data item to collate +* @data1_len: length in bytes of @data1 +* @data2: second data item to collate +* @data2_len: length in bytes of @data2 +* +* Collate the two data items @data1 and @data2 using the collation rule @cr +* and return -1, 0, ir 1 if @data1 is found, respectively, to collate before, +* to match, or to collate after @data2. +* +* For speed we use the collation rule @cr as an index into two tables of +* function pointers to call the appropriate collation function. +*/ +int ntfs_collate (struct ntfs_volume *vol,__le32 cr, +const void *data1,const int data1_len, +const void *data2,const int data2_len) +{ +int i; + +ntfs_debug ("Entering." ); + +if (cr !=COLLATION_BINARY && cr !=COLLATION_NTOFS_ULONG && + cr !=COLLATION_FILE_NAME && cr !=COLLATION_NTOFS_ULONGS ) +return -EINVAL ; + + i =le32_to_cpu (cr); +if (i <0 ) +return -1 ; +if (i <=0x02 ) +return ntfs_do_collate0x0[i](vol, data1, data1_len, + data2, data2_len); +if (i <0x10 ) +return -1 ; + i -=0x10 ; +if (likely (i <=3 )) +return ntfs_do_collate0x1[i](vol, data1, data1_len, + data2, data2_len); +return 0 ; +}
fs/ntfsplus/logfile.c new file +770 -0
770 lines not on this page · raw message
fs/ntfsplus/misc.c new file +213 -0
213 lines not on this page · raw message
fs/ntfsplus/unistr.c new file +473 -0
473 lines not on this page · raw message
fs/ntfsplus/upcase.c new file +73 -0
73 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v2 11/11 ntfsplus: add Kconfig and Makefile
In reply to Namjae JeonThis adds the Kconfig and Makefile for ntfsplus.
--- fs/Kconfig | 1 + fs/Makefile | 1 + fs/ntfsplus/Kconfig | 45 ++++++++++++++++++++++++++++++++++++++++++++ fs/ntfsplus/Makefile | 18 ++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 fs/ntfsplus/Kconfig create mode 100644 fs/ntfsplus/Makefile
fs/Kconfig +1 -0
@@ -153,6 +153,7 @@ menu "DOS/FAT/EXFAT/NT Filesystems" source "fs/fat/Kconfig" source "fs/exfat/Kconfig" source "fs/ntfs3/Kconfig" +source "fs/ntfsplus/Kconfig" endmenu endif # BLOCK
fs/Makefile +1 -0
@@ -91,6 +91,7 @@ obj-y += unicode/ obj-$(CONFIG_SMBFS) += smb/ obj-$(CONFIG_HPFS_FS) += hpfs/ obj-$(CONFIG_NTFS3_FS) += ntfs3/ +obj-$(CONFIG_NTFSPLUS_FS) += ntfsplus/ obj-$(CONFIG_UFS_FS) += ufs/ obj-$(CONFIG_EFS_FS) += efs/ obj-$(CONFIG_JFFS2_FS) += jffs2/
fs/ntfsplus/Kconfig new file +45 -0
45 lines not on this page · raw message
fs/ntfsplus/Makefile new file +18 -0
@@ -0,0 +1,18 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Makefile for the ntfsplus filesystem support. +# + +# to check robot warnings +ccflags-y += -Wint-to-pointer-cast \ + $(call cc-option,-Wunused-but-set-variable,-Wunused-const-variable) \ + $(call cc-option,-Wold-style-declaration,-Wout-of-line-declaration) + +obj-$(CONFIG_NTFSPLUS_FS) += ntfsplus.o + +ntfsplus-y := aops.o attrib.o collate.o misc.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 \ + ntfs_iomap.o + +ccflags-$(CONFIG_NTFSPLUS_DEBUG) += -DDEBUG
Signature · 1 line
2.25.1
Re: [PATCH v2 11/11] ntfsplus: add Kconfig and Makefile
Quoted 2 lines · Namjae Jeon
This adds the
KconfigandMakefilefor ntfsplus.
MAINTAINERS?
Quoted 113 lines
Signed-off-by Namjae Jeon <[email protected]>--- fs/Kconfig | 1 + fs/Makefile | 1 + fs/ntfsplus/Kconfig | 45 ++++++++++++++++++++++++++++++++++++++++++++ fs/ntfsplus/Makefile | 18 ++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 fs/ntfsplus/Kconfig create mode 100644 fs/ntfsplus/Makefilefs/Kconfig +1
-0@@ -153,6 +153,7 @@ menu "DOS/FAT/EXFAT/NT Filesystems" source "fs/fat/Kconfig" source "fs/exfat/Kconfig" source "fs/ntfs3/Kconfig" +source "fs/ntfsplus/Kconfig" endmenu endif # BLOCKfs/Makefile +1
-0@@ -91,6 +91,7 @@ obj-y += unicode/ obj-$(CONFIG_SMBFS) += smb/ obj-$(CONFIG_HPFS_FS) += hpfs/ obj-$(CONFIG_NTFS3_FS) += ntfs3/ +obj-$(CONFIG_NTFSPLUS_FS) += ntfsplus/ obj-$(CONFIG_UFS_FS) += ufs/ obj-$(CONFIG_EFS_FS) += efs/ obj-$(CONFIG_JFFS2_FS) += jffs2/fs/ntfsplus/Kconfig new file +45
-0@@ -0,0 +1,45 @@ +# SPDX-License-Identifier: GPL-2.0-only +config NTFSPLUS_FS + tristate "NTFS+ file system support" + select NLS + help + NTFS is the file system of Microsoft Windows NT, 2000, XP and 2003. + This allows you to mount devices formatted with the ntfs file system. + + To compile this as a module, choose M here: the module will be called + ntfsplus. + +config NTFSPLUS_DEBUG + bool "NTFS+ debugging support" + depends on NTFSPLUS_FS + help + If you are experiencing any problems with the NTFS file system, say + Y here. This will result in additional consistency checks to be + performed by the driver as well as additional debugging messages to + be written to the system log. Note that debugging messages are + disabled by default. To enable them, supply the option debug_msgs=1 + at the kernel command line when booting the kernel or as an option + to insmod when loading the ntfs module. Once the driver is active, + you can enable debugging messages by doing (as root): + echo 1 > /proc/sys/fs/ntfs-debug + Replacing the "1" with "0" would disable debug messages. + + If you leave debugging messages disabled, this results in little + overhead, but enabling debug messages results in very significant + slowdown of the system. + + When reporting bugs, please try to have available a full dump of + debugging messages while the misbehaviour was occurring. + +config NTFSPLUS_FS_POSIX_ACL + bool "NTFS+ POSIX Access Control Lists" + depends on NTFSPLUS_FS + select FS_POSIX_ACL + help + POSIX Access Control Lists (ACLs) support additional access rights + for users and groups beyond the standard owner/group/world scheme, + and this option selects support for ACLs specifically for ntfs + filesystems. + NOTE: this is linux only feature. Windows will ignore these ACLs. + + If you don't know what Access Control Lists are, say N.fs/ntfsplus/Makefile new file +18
-1@@ -0,0 +1,18 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Makefile for the ntfsplus filesystem support. +# + +# to check robot warnings +ccflags-y += -Wint-to-pointer-cast \ + $(call cc-option,-Wunused-but-set-variable,-Wunused-const-variable) \ + $(call cc-option,-Wold-style-declaration,-Wout-of-line-declaration) + +obj-$(CONFIG_NTFSPLUS_FS) += ntfsplus.o + +ntfsplus-y := aops.o attrib.o collate.o misc.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 \ + ntfs_iomap.o + +ccflags-$(CONFIG_NTFSPLUS_DEBUG) += -DDEBUG --2.25.1
Quoted 4 lines · Namjae Jeon
Introduction
============
Quoted 8 lines
The NTFS filesystem[1] still remains the default filesystem for Windows and The well-maintained NTFS driver in the Linux kernel enhances interoperability with Windows devices, making it easier for Linux users to work with NTFS-formatted drives. Currently, ntfs support in Linux was the long-neglected NTFS Classic (read-only), which has been removed from the Linux kernel, leaving the poorly maintained ntfs3. ntfs3 still has many problems and is poorly maintained, so users and distributions are still using the old legacy ntfs-3g.
May I suggest that you add a patch to your series to add a deprecation message to ntfs3?
See for example eb103a51640ee ("reiserfs: Deprecate reiserfs")
Quoted 5 lines
What is ntfsplus?
=================
Quoted 9 lines
The remade ntfs called ntfsplus is an implementation that supports write and the essential
requirements(iomap, no buffer-head, utilities, xfstests test result)based on read-only classic NTFS.
The old read-only ntfs code is much cleaner, with extensive comments, offers readability that makes understanding NTFS easier. This is why ntfsplus was developed on old read-only NTFS base.
The target is to provide currenttrends(iomap, no buffer head, folio), enhanced performance, stable maintenance, utility support including fsck.
You are bringing back the old ntfs driver code from the dead, preserving the code and Copyrights and everything to bring it up to speed with modern vfs API and to add super nice features. Right?
Apart from its history, the new refurbished ntfs driver is also fully backward compact to the old read-only driver. Right?
Why is the rebranding to ntfsplus useful then?
I can understand that you want a new name for a new ntfsprogs-plus project which is a fork of ntfs-3g, but I don't think that the new name for the kernel driver is useful or welcome.
Do you have any objections to leaving its original ntfs name?
You can also do:MODULE_ALIAS_FS("ntfs");MODULE_ALIAS_FS("ntfsplus");
If that is useful for ntfsprogs-plus somehow.
Thanks,
Amir.
Quoted 249 lines
Key Features
============- Write support:
Implement write support on classic read-only NTFS. Additionally, integrate delayed allocation to enhance write performance through multi-cluster allocation and minimized fragmentation of cluster bitmap.- Switch to using iomap:
Use iomap for buffered IO writes, reads, direct IO, file extent mapping, readpages, writepages operations.- Stop using the buffer head:
The use of buffer head in old ntfs and switched to use folio instead. As a result,CONFIG_BUFFER_HEADoption enable is removed inKconfigalso.- Public utilities include fsck[2]:
While ntfs-3g includes ntfsprogs as a component, it notably lacks the fsck implementation. So we have launched a new ntfs utilitiies project called ntfsprogs-plus by forking from ntfs-3g after removing unnecessary ntfs fuse implementation. fsck.ntfs can be used for ntfs testing with xfstests as well as for recovering corrupted NTFS device.- Performance Enhancements:
- ntfsplus vs. ntfs3:
* Performance was benchmarked using iozone with various chunk size.
- In single-thread(1T) write tests, ntfsplus show approximately 3~5% better performance.
- In multi-thread(4T) write tests, ntfsplus show approximately 35~110% better performance.
- Read throughput is identical for both ntfs implementations.1GB file size:4096 size:16384 size:65536
MB/sec ntfsplus | ntfs3 ntfsplus | ntfs3 ntfsplus | ntfs3
─────────────────────────────────────────────────────────────────
read 399 | 399 426 | 424 429 | 430
─────────────────────────────────────────────────────────────────
write(1T) 291 | 276 325 | 305 333 | 317
write(4T) 105 | 50 113 | 78 114 | 99.6* File list browsing performance. (about 12~14% faster)
files:100000 files:200000 files:400000
Sec ntfsplus | ntfs3 ntfsplus | ntfs3 ntfsplus | ntfs3
─────────────────────────────────────────────────────────────────
ls -lR 7.07 | 8.10 14.03 | 16.35 28.27 | 32.86* mount time.
parti_size:1TB parti_size:2TB parti_size:4TB
Sec ntfsplus | ntfs3 ntfsplus | ntfs3 ntfsplus | ntfs3
─────────────────────────────────────────────────────────────────
mount 0.38 | 2.03 0.39 | 2.25 0.70 | 4.51The following are the reasons why ntfsplus performance is higher
compared to ntfs3:
- Use iomap aops.
- Delayed allocation support.
- Optimize zero out for newly allocated clusters.
- Optimize runlist merge overhead with small chunck size.
- pre-load mft(inode) blocks and index(dentry) blocks to improve readdir + stat performance.
- Load lcn bitmap on background.- Stability improvement:
a. Pass more xfstests tests:
ntfsplus passed 287 tests, significantly higher than ntfs3's 218. ntfsplus implement fallocate, idmapped mount and permission, etc, resulting in a significantly high number of xfstests passing compared to ntfs3.
b. Bonnie++ issue[3]:
The Bonnie++ benchmark fails on ntfs3 with a "Directory not empty" error during file deletion. ntfs3 currently iterates directory entries by reading index blocks one by one. When entries are deleted concurrently, index block merging or entry relocation can causereaddir()to skip some entries, leaving files undeleted in workloads(bonnie++) that mix unlink and directory scans. ntfsplus implement leaf chain traversal in readdir to avoid entry skip on deletion.- Journaling support:
ntfs3 does not provide full journaling support. It only implement journal replay[4], which in our testing did not function correctly. My next task after upstreaming will be to add full journal support to ntfsplus.The feature comparison summary
==============================Feature ntfsplus ntfs3 =================================== ======== =========== Write support Yes Yes iomap support Yes No No buffer head Yes No Public utilities(mkfs, fsck, etc.) Yes No xfstests passed 287 218 Idmapped mount Yes No Delayed allocation Yes No Bonnie++ Pass Fail Journaling Planned Inoperative =================================== ======== ===========References
==========
[1] https://en.wikipedia.org/wiki/NTFS
[2] https://github.com/ntfsprogs-plus/ntfsprogs-plus
[3] https://lore.kernel.org/ntfs3/CAOZgwEd7NDkGEpdF6UQTcbYuupDavaHBoj4WwTy3Qe4Bqm6V0g@mail.gmail.com/
[4] https://marc.info/?l=linux-fsdevel&m=161738417018673&q=mboxAvailable in the Git repository at:
===================================git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/ntfs.gitntfs-nextv2:
- Add ntfs3-compatible mountoptions(sys_immutable, nohidden, hide_dot_files, nocase, acl, windows_names, disable_sparse, discard).
- Add iocharset mount option.
- Add ntfs3-compatible dos attribute and ntfs attribute load/store in setxattr/getattr().
- Add support for FS_IOC_{GET,SET}FSLABEL ioctl.
- Add support for FITRIM ioctl.
- Fix thewarnings(duplicate symbol, __divdi3, etc)from kernel test robot.
- Prefixpr_xxx()with ntfsplus.
- Add support for $MFT File extension.
- AddDocumentation/filesystems/ntfsplus.rst.
- Mark experimental.
- Remove BUG traps warnings from checkpatch.pl.Namjae Jeon (11):
ntfsplus: in-memory, on-disk structures and headers
ntfsplus: add super block operations
ntfsplus: add inode operations
ntfsplus: add directory operations
ntfsplus: add file operations
ntfsplus: add iomap and address space operations
ntfsplus: add attrib operatrions
ntfsplus: add runlist handling and cluster allocator
ntfsplus: add reparse and ea operations
ntfsplus: add misc operations
ntfsplus: addKconfigandMakefileDocumentation/filesystems/index.rst | 1 + Documentation/filesystems/ntfsplus.rst | 199 + fs/Kconfig | 1 + fs/Makefile | 1 + fs/ntfsplus/Kconfig | 45 + fs/ntfsplus/Makefile | 18 + fs/ntfsplus/aops.c | 617 +++ fs/ntfsplus/aops.h | 92 + fs/ntfsplus/attrib.c | 5377 ++++++++++++++++++++++++ fs/ntfsplus/attrib.h | 159 + fs/ntfsplus/attrlist.c | 285 ++ fs/ntfsplus/attrlist.h | 21 + fs/ntfsplus/bitmap.c | 290 ++ fs/ntfsplus/bitmap.h | 93 + fs/ntfsplus/collate.c | 178 + fs/ntfsplus/collate.h | 37 + fs/ntfsplus/compress.c | 1564 +++++++ fs/ntfsplus/dir.c | 1230 ++++++ fs/ntfsplus/dir.h | 33 + fs/ntfsplus/ea.c | 931 ++++ fs/ntfsplus/ea.h | 25 + fs/ntfsplus/file.c | 1142 +++++ fs/ntfsplus/index.c | 2112 ++++++++++ fs/ntfsplus/index.h | 127 + fs/ntfsplus/inode.c | 3729 ++++++++++++++++ fs/ntfsplus/inode.h | 353 ++ fs/ntfsplus/layout.h | 2288 ++++++++++ fs/ntfsplus/lcnalloc.c | 1012 +++++ fs/ntfsplus/lcnalloc.h | 127 + fs/ntfsplus/logfile.c | 770 ++++ fs/ntfsplus/logfile.h | 316 ++ fs/ntfsplus/mft.c | 2698 ++++++++++++ fs/ntfsplus/mft.h | 92 + fs/ntfsplus/misc.c | 213 + fs/ntfsplus/misc.h | 218 + fs/ntfsplus/mst.c | 195 + fs/ntfsplus/namei.c | 1677 ++++++++ fs/ntfsplus/ntfs.h | 180 + fs/ntfsplus/ntfs_iomap.c | 700 +++ fs/ntfsplus/ntfs_iomap.h | 22 + fs/ntfsplus/reparse.c | 550 +++ fs/ntfsplus/reparse.h | 15 + fs/ntfsplus/runlist.c | 1983 +++++++++ fs/ntfsplus/runlist.h | 91 + fs/ntfsplus/super.c | 2865 +++++++++++++ fs/ntfsplus/unistr.c | 473 +++ fs/ntfsplus/upcase.c | 73 + fs/ntfsplus/volume.h | 254 ++ include/uapi/linux/ntfs.h | 23 + 49 files changed, 35495 insertions(+) create mode 100644 Documentation/filesystems/ntfsplus.rst create mode 100644 fs/ntfsplus/Kconfig create mode 100644 fs/ntfsplus/Makefile create mode 100644 fs/ntfsplus/aops.c create mode 100644 fs/ntfsplus/aops.h create mode 100644 fs/ntfsplus/attrib.c create mode 100644 fs/ntfsplus/attrib.h create mode 100644 fs/ntfsplus/attrlist.c create mode 100644 fs/ntfsplus/attrlist.h create mode 100644 fs/ntfsplus/bitmap.c create mode 100644 fs/ntfsplus/bitmap.h create mode 100644 fs/ntfsplus/collate.c create mode 100644 fs/ntfsplus/collate.h create mode 100644 fs/ntfsplus/compress.c create mode 100644 fs/ntfsplus/dir.c create mode 100644 fs/ntfsplus/dir.h create mode 100644 fs/ntfsplus/ea.c create mode 100644 fs/ntfsplus/ea.h create mode 100644 fs/ntfsplus/file.c create mode 100644 fs/ntfsplus/index.c create mode 100644 fs/ntfsplus/index.h create mode 100644 fs/ntfsplus/inode.c create mode 100644 fs/ntfsplus/inode.h create mode 100644 fs/ntfsplus/layout.h create mode 100644 fs/ntfsplus/lcnalloc.c create mode 100644 fs/ntfsplus/lcnalloc.h create mode 100644 fs/ntfsplus/logfile.c create mode 100644 fs/ntfsplus/logfile.h create mode 100644 fs/ntfsplus/mft.c create mode 100644 fs/ntfsplus/mft.h create mode 100644 fs/ntfsplus/misc.c create mode 100644 fs/ntfsplus/misc.h create mode 100644 fs/ntfsplus/mst.c create mode 100644 fs/ntfsplus/namei.c create mode 100644 fs/ntfsplus/ntfs.h create mode 100644 fs/ntfsplus/ntfs_iomap.c create mode 100644 fs/ntfsplus/ntfs_iomap.h create mode 100644 fs/ntfsplus/reparse.c create mode 100644 fs/ntfsplus/reparse.h create mode 100644 fs/ntfsplus/runlist.c create mode 100644 fs/ntfsplus/runlist.h create mode 100644 fs/ntfsplus/super.c create mode 100644 fs/ntfsplus/unistr.c create mode 100644 fs/ntfsplus/upcase.c create mode 100644 fs/ntfsplus/volume.h create mode 100644 include/uapi/linux/ntfs.h--
2.25.1
Re: [PATCH v2 11/11] ntfsplus: add Kconfig and Makefile
In reply to Namjae JeonQuoted 30 lines · Namjae Jeon
This adds the
KconfigandMakefilefor ntfsplus.Signed-off-by Namjae Jeon <[email protected]>--- fs/Kconfig | 1 + fs/Makefile | 1 + fs/ntfsplus/Kconfig | 45 ++++++++++++++++++++++++++++++++++++++++++++ fs/ntfsplus/Makefile | 18 ++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 fs/ntfsplus/Kconfig create mode 100644 fs/ntfsplus/Makefilefs/Kconfig +1
-0@@ -153,6 +153,7 @@ menu "DOS/FAT/EXFAT/NT Filesystems" source "fs/fat/Kconfig" source "fs/exfat/Kconfig" source "fs/ntfs3/Kconfig" +source "fs/ntfsplus/Kconfig" endmenu endif # BLOCKfs/Makefile +0
-0
fs/Makefile obj-y += unicode/ · line 94
Quoted 4 lines
fs/Makefile +1
-0@@ -91,6 +91,7 @@ obj-y += unicode/ obj-$(CONFIG_SMBFS) += smb/ obj-$(CONFIG_HPFS_FS) += hpfs/ obj-$(CONFIG_NTFS3_FS) += ntfs3/ +obj-$(CONFIG_NTFSPLUS_FS) += ntfsplus/
I suggested in another reply to keep the original ntfs name
More important is to keep your driver linked before the unmaintained ntfs3, so that it hopefully gets picked up before ntfs3 for auto mount type if both drivers are built-in.
I am not sure if keeping the order here would guarantee the link/registration order. If not, it may make sense to mutually exclude them as built-in drivers.
Thanks,
Amir.
Quoted 6 lines · Amir Goldstein
Quoted 4 lines · Namjae Jeon
Introduction
============
Quoted 9 lines
Quoted 8 lines
The NTFS filesystem[1] still remains the default filesystem for Windows and The well-maintained NTFS driver in the Linux kernel enhances interoperability with Windows devices, making it easier for Linux users to work with NTFS-formatted drives. Currently, ntfs support in Linux was the long-neglected NTFS Classic (read-only), which has been removed from the Linux kernel, leaving the poorly maintained ntfs3. ntfs3 still has many problems and is poorly maintained, so users and distributions are still using the old legacy ntfs-3g.
Hi Amir,
Quoted 4 lines
May I suggest that you add a patch to your series to add a deprecation message to ntfs3?
See for example
eb103a51640ee("reiserfs: Deprecate reiserfs")
Okay, I'll add it in the next version, referring to this reiserfs patch.
Quoted 16 lines
Quoted 6 lines
What is ntfsplus?
=================Quoted 9 lines
The remade ntfs called ntfsplus is an implementation that supports write and the essential
requirements(iomap, no buffer-head, utilities, xfstests test result)based on read-only classic NTFS.
The old read-only ntfs code is much cleaner, with extensive comments, offers readability that makes understanding NTFS easier. This is why ntfsplus was developed on old read-only NTFS base.
The target is to provide currenttrends(iomap, no buffer head, folio), enhanced performance, stable maintenance, utility support including fsck.
Quoted 3 lines
You are bringing back the old ntfs driver code from the dead, preserving the code and Copyrights and everything to bring it up to speed with modern vfs API and to add super nice features. Right?
Yes.
Quoted 3 lines
Apart from its history, the new refurbished ntfs driver is also fully backward compact to the old read-only driver. Right?
Yes.
Quoted 6 lines
Why is the rebranding to ntfsplus useful then?
I can understand that you want a new name for a new ntfsprogs-plus project which is a fork of ntfs-3g, but I don't think that the new name for the kernel driver is useful or welcome.
Right, I wanted to rebrand ntfsprogs-plus and ntfsplus into a paired set of names. Also, ntfs3 was already used as an alias for ntfs, so I couldn't touch ntfs3 driver without consensus from the fs maintainers.
Quoted 2 lines
Do you have any objections to leaving its original ntfs name?
I have no objection to using ntfsplus as an alias for ntfs if we add a deprecation message to ntfs3.
Quoted 4 lines
You can also do:
MODULE_ALIAS_FS("ntfs");MODULE_ALIAS_FS("ntfsplus");
I will add this in the next version with ntfs3 deprecation patch.
Quoted 2 lines
If that is useful for ntfsprogs-plus somehow.
That is very useful and thanks for your review!
Quoted 253 lines
Thanks,
Amir.Quoted 249 lines
Key Features
============- Write support:
Implement write support on classic read-only NTFS. Additionally, integrate delayed allocation to enhance write performance through multi-cluster allocation and minimized fragmentation of cluster bitmap.- Switch to using iomap:
Use iomap for buffered IO writes, reads, direct IO, file extent mapping, readpages, writepages operations.- Stop using the buffer head:
The use of buffer head in old ntfs and switched to use folio instead. As a result,CONFIG_BUFFER_HEADoption enable is removed inKconfigalso.- Public utilities include fsck[2]:
While ntfs-3g includes ntfsprogs as a component, it notably lacks the fsck implementation. So we have launched a new ntfs utilitiies project called ntfsprogs-plus by forking from ntfs-3g after removing unnecessary ntfs fuse implementation. fsck.ntfs can be used for ntfs testing with xfstests as well as for recovering corrupted NTFS device.- Performance Enhancements:
- ntfsplus vs. ntfs3:
* Performance was benchmarked using iozone with various chunk size.
- In single-thread(1T) write tests, ntfsplus show approximately 3~5% better performance.
- In multi-thread(4T) write tests, ntfsplus show approximately 35~110% better performance.
- Read throughput is identical for both ntfs implementations.1GB file size:4096 size:16384 size:65536
MB/sec ntfsplus | ntfs3 ntfsplus | ntfs3 ntfsplus | ntfs3
─────────────────────────────────────────────────────────────────
read 399 | 399 426 | 424 429 | 430
─────────────────────────────────────────────────────────────────
write(1T) 291 | 276 325 | 305 333 | 317
write(4T) 105 | 50 113 | 78 114 | 99.6* File list browsing performance. (about 12~14% faster)
files:100000 files:200000 files:400000
Sec ntfsplus | ntfs3 ntfsplus | ntfs3 ntfsplus | ntfs3
─────────────────────────────────────────────────────────────────
ls -lR 7.07 | 8.10 14.03 | 16.35 28.27 | 32.86* mount time.
parti_size:1TB parti_size:2TB parti_size:4TB
Sec ntfsplus | ntfs3 ntfsplus | ntfs3 ntfsplus | ntfs3
─────────────────────────────────────────────────────────────────
mount 0.38 | 2.03 0.39 | 2.25 0.70 | 4.51The following are the reasons why ntfsplus performance is higher
compared to ntfs3:
- Use iomap aops.
- Delayed allocation support.
- Optimize zero out for newly allocated clusters.
- Optimize runlist merge overhead with small chunck size.
- pre-load mft(inode) blocks and index(dentry) blocks to improve readdir + stat performance.
- Load lcn bitmap on background.- Stability improvement:
a. Pass more xfstests tests:
ntfsplus passed 287 tests, significantly higher than ntfs3's 218. ntfsplus implement fallocate, idmapped mount and permission, etc, resulting in a significantly high number of xfstests passing compared to ntfs3.
b. Bonnie++ issue[3]:
The Bonnie++ benchmark fails on ntfs3 with a "Directory not empty" error during file deletion. ntfs3 currently iterates directory entries by reading index blocks one by one. When entries are deleted concurrently, index block merging or entry relocation can causereaddir()to skip some entries, leaving files undeleted in workloads(bonnie++) that mix unlink and directory scans. ntfsplus implement leaf chain traversal in readdir to avoid entry skip on deletion.- Journaling support:
ntfs3 does not provide full journaling support. It only implement journal replay[4], which in our testing did not function correctly. My next task after upstreaming will be to add full journal support to ntfsplus.The feature comparison summary
==============================Feature ntfsplus ntfs3 =================================== ======== =========== Write support Yes Yes iomap support Yes No No buffer head Yes No Public utilities(mkfs, fsck, etc.) Yes No xfstests passed 287 218 Idmapped mount Yes No Delayed allocation Yes No Bonnie++ Pass Fail Journaling Planned Inoperative =================================== ======== ===========References
==========
[1] https://en.wikipedia.org/wiki/NTFS
[2] https://github.com/ntfsprogs-plus/ntfsprogs-plus
[3] https://lore.kernel.org/ntfs3/CAOZgwEd7NDkGEpdF6UQTcbYuupDavaHBoj4WwTy3Qe4Bqm6V0g@mail.gmail.com/
[4] https://marc.info/?l=linux-fsdevel&m=161738417018673&q=mboxAvailable in the Git repository at:
===================================git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/ntfs.gitntfs-nextv2:
- Add ntfs3-compatible mountoptions(sys_immutable, nohidden, hide_dot_files, nocase, acl, windows_names, disable_sparse, discard).
- Add iocharset mount option.
- Add ntfs3-compatible dos attribute and ntfs attribute load/store in setxattr/getattr().
- Add support for FS_IOC_{GET,SET}FSLABEL ioctl.
- Add support for FITRIM ioctl.
- Fix thewarnings(duplicate symbol, __divdi3, etc)from kernel test robot.
- Prefixpr_xxx()with ntfsplus.
- Add support for $MFT File extension.
- AddDocumentation/filesystems/ntfsplus.rst.
- Mark experimental.
- Remove BUG traps warnings from checkpatch.pl.Namjae Jeon (11):
ntfsplus: in-memory, on-disk structures and headers
ntfsplus: add super block operations
ntfsplus: add inode operations
ntfsplus: add directory operations
ntfsplus: add file operations
ntfsplus: add iomap and address space operations
ntfsplus: add attrib operatrions
ntfsplus: add runlist handling and cluster allocator
ntfsplus: add reparse and ea operations
ntfsplus: add misc operations
ntfsplus: addKconfigandMakefileDocumentation/filesystems/index.rst | 1 + Documentation/filesystems/ntfsplus.rst | 199 + fs/Kconfig | 1 + fs/Makefile | 1 + fs/ntfsplus/Kconfig | 45 + fs/ntfsplus/Makefile | 18 + fs/ntfsplus/aops.c | 617 +++ fs/ntfsplus/aops.h | 92 + fs/ntfsplus/attrib.c | 5377 ++++++++++++++++++++++++ fs/ntfsplus/attrib.h | 159 + fs/ntfsplus/attrlist.c | 285 ++ fs/ntfsplus/attrlist.h | 21 + fs/ntfsplus/bitmap.c | 290 ++ fs/ntfsplus/bitmap.h | 93 + fs/ntfsplus/collate.c | 178 + fs/ntfsplus/collate.h | 37 + fs/ntfsplus/compress.c | 1564 +++++++ fs/ntfsplus/dir.c | 1230 ++++++ fs/ntfsplus/dir.h | 33 + fs/ntfsplus/ea.c | 931 ++++ fs/ntfsplus/ea.h | 25 + fs/ntfsplus/file.c | 1142 +++++ fs/ntfsplus/index.c | 2112 ++++++++++ fs/ntfsplus/index.h | 127 + fs/ntfsplus/inode.c | 3729 ++++++++++++++++ fs/ntfsplus/inode.h | 353 ++ fs/ntfsplus/layout.h | 2288 ++++++++++ fs/ntfsplus/lcnalloc.c | 1012 +++++ fs/ntfsplus/lcnalloc.h | 127 + fs/ntfsplus/logfile.c | 770 ++++ fs/ntfsplus/logfile.h | 316 ++ fs/ntfsplus/mft.c | 2698 ++++++++++++ fs/ntfsplus/mft.h | 92 + fs/ntfsplus/misc.c | 213 + fs/ntfsplus/misc.h | 218 + fs/ntfsplus/mst.c | 195 + fs/ntfsplus/namei.c | 1677 ++++++++ fs/ntfsplus/ntfs.h | 180 + fs/ntfsplus/ntfs_iomap.c | 700 +++ fs/ntfsplus/ntfs_iomap.h | 22 + fs/ntfsplus/reparse.c | 550 +++ fs/ntfsplus/reparse.h | 15 + fs/ntfsplus/runlist.c | 1983 +++++++++ fs/ntfsplus/runlist.h | 91 + fs/ntfsplus/super.c | 2865 +++++++++++++ fs/ntfsplus/unistr.c | 473 +++ fs/ntfsplus/upcase.c | 73 + fs/ntfsplus/volume.h | 254 ++ include/uapi/linux/ntfs.h | 23 + 49 files changed, 35495 insertions(+) create mode 100644 Documentation/filesystems/ntfsplus.rst create mode 100644 fs/ntfsplus/Kconfig create mode 100644 fs/ntfsplus/Makefile create mode 100644 fs/ntfsplus/aops.c create mode 100644 fs/ntfsplus/aops.h create mode 100644 fs/ntfsplus/attrib.c create mode 100644 fs/ntfsplus/attrib.h create mode 100644 fs/ntfsplus/attrlist.c create mode 100644 fs/ntfsplus/attrlist.h create mode 100644 fs/ntfsplus/bitmap.c create mode 100644 fs/ntfsplus/bitmap.h create mode 100644 fs/ntfsplus/collate.c create mode 100644 fs/ntfsplus/collate.h create mode 100644 fs/ntfsplus/compress.c create mode 100644 fs/ntfsplus/dir.c create mode 100644 fs/ntfsplus/dir.h create mode 100644 fs/ntfsplus/ea.c create mode 100644 fs/ntfsplus/ea.h create mode 100644 fs/ntfsplus/file.c create mode 100644 fs/ntfsplus/index.c create mode 100644 fs/ntfsplus/index.h create mode 100644 fs/ntfsplus/inode.c create mode 100644 fs/ntfsplus/inode.h create mode 100644 fs/ntfsplus/layout.h create mode 100644 fs/ntfsplus/lcnalloc.c create mode 100644 fs/ntfsplus/lcnalloc.h create mode 100644 fs/ntfsplus/logfile.c create mode 100644 fs/ntfsplus/logfile.h create mode 100644 fs/ntfsplus/mft.c create mode 100644 fs/ntfsplus/mft.h create mode 100644 fs/ntfsplus/misc.c create mode 100644 fs/ntfsplus/misc.h create mode 100644 fs/ntfsplus/mst.c create mode 100644 fs/ntfsplus/namei.c create mode 100644 fs/ntfsplus/ntfs.h create mode 100644 fs/ntfsplus/ntfs_iomap.c create mode 100644 fs/ntfsplus/ntfs_iomap.h create mode 100644 fs/ntfsplus/reparse.c create mode 100644 fs/ntfsplus/reparse.h create mode 100644 fs/ntfsplus/runlist.c create mode 100644 fs/ntfsplus/runlist.h create mode 100644 fs/ntfsplus/super.c create mode 100644 fs/ntfsplus/unistr.c create mode 100644 fs/ntfsplus/upcase.c create mode 100644 fs/ntfsplus/volume.h create mode 100644 include/uapi/linux/ntfs.h--
2.25.1
Re: [PATCH v2 11/11] ntfsplus: add Kconfig and Makefile
In reply to Amir GoldsteinQuoted 6 lines · Amir Goldstein
Quoted 2 lines · Namjae Jeon
This adds the
KconfigandMakefilefor ntfsplus.
MAINTAINERS?
Okay, I will add it on next version:)
Thanks!
Quoted 114 lines
Quoted 114 lines
Signed-off-by Namjae Jeon <[email protected]>--- fs/Kconfig | 1 + fs/Makefile | 1 + fs/ntfsplus/Kconfig | 45 ++++++++++++++++++++++++++++++++++++++++++++ fs/ntfsplus/Makefile | 18 ++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 fs/ntfsplus/Kconfig create mode 100644 fs/ntfsplus/Makefilefs/Kconfig +1
-0@@ -153,6 +153,7 @@ menu "DOS/FAT/EXFAT/NT Filesystems" source "fs/fat/Kconfig" source "fs/exfat/Kconfig" source "fs/ntfs3/Kconfig" +source "fs/ntfsplus/Kconfig" endmenu endif # BLOCKfs/Makefile +1
-0@@ -91,6 +91,7 @@ obj-y += unicode/ obj-$(CONFIG_SMBFS) += smb/ obj-$(CONFIG_HPFS_FS) += hpfs/ obj-$(CONFIG_NTFS3_FS) += ntfs3/ +obj-$(CONFIG_NTFSPLUS_FS) += ntfsplus/ obj-$(CONFIG_UFS_FS) += ufs/ obj-$(CONFIG_EFS_FS) += efs/ obj-$(CONFIG_JFFS2_FS) += jffs2/fs/ntfsplus/Kconfig new file +45
-0@@ -0,0 +1,45 @@ +# SPDX-License-Identifier: GPL-2.0-only +config NTFSPLUS_FS + tristate "NTFS+ file system support" + select NLS + help + NTFS is the file system of Microsoft Windows NT, 2000, XP and 2003. + This allows you to mount devices formatted with the ntfs file system. + + To compile this as a module, choose M here: the module will be called + ntfsplus. + +config NTFSPLUS_DEBUG + bool "NTFS+ debugging support" + depends on NTFSPLUS_FS + help + If you are experiencing any problems with the NTFS file system, say + Y here. This will result in additional consistency checks to be + performed by the driver as well as additional debugging messages to + be written to the system log. Note that debugging messages are + disabled by default. To enable them, supply the option debug_msgs=1 + at the kernel command line when booting the kernel or as an option + to insmod when loading the ntfs module. Once the driver is active, + you can enable debugging messages by doing (as root): + echo 1 > /proc/sys/fs/ntfs-debug + Replacing the "1" with "0" would disable debug messages. + + If you leave debugging messages disabled, this results in little + overhead, but enabling debug messages results in very significant + slowdown of the system. + + When reporting bugs, please try to have available a full dump of + debugging messages while the misbehaviour was occurring. + +config NTFSPLUS_FS_POSIX_ACL + bool "NTFS+ POSIX Access Control Lists" + depends on NTFSPLUS_FS + select FS_POSIX_ACL + help + POSIX Access Control Lists (ACLs) support additional access rights + for users and groups beyond the standard owner/group/world scheme, + and this option selects support for ACLs specifically for ntfs + filesystems. + NOTE: this is linux only feature. Windows will ignore these ACLs. + + If you don't know what Access Control Lists are, say N.fs/ntfsplus/Makefile new file +18
-1@@ -0,0 +1,18 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Makefile for the ntfsplus filesystem support. +# + +# to check robot warnings +ccflags-y += -Wint-to-pointer-cast \ + $(call cc-option,-Wunused-but-set-variable,-Wunused-const-variable) \ + $(call cc-option,-Wold-style-declaration,-Wout-of-line-declaration) + +obj-$(CONFIG_NTFSPLUS_FS) += ntfsplus.o + +ntfsplus-y := aops.o attrib.o collate.o misc.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 \ + ntfs_iomap.o + +ccflags-$(CONFIG_NTFSPLUS_DEBUG) += -DDEBUG --2.25.1
Re: [PATCH v2 11/11] ntfsplus: add Kconfig and Makefile
In reply to Amir GoldsteinQuoted 39 lines · Amir Goldstein
Quoted 30 lines · Namjae Jeon
This adds the
KconfigandMakefilefor ntfsplus.Signed-off-by Namjae Jeon <[email protected]>--- fs/Kconfig | 1 + fs/Makefile | 1 + fs/ntfsplus/Kconfig | 45 ++++++++++++++++++++++++++++++++++++++++++++ fs/ntfsplus/Makefile | 18 ++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 fs/ntfsplus/Kconfig create mode 100644 fs/ntfsplus/Makefilefs/Kconfig +1
-0@@ -153,6 +153,7 @@ menu "DOS/FAT/EXFAT/NT Filesystems" source "fs/fat/Kconfig" source "fs/exfat/Kconfig" source "fs/ntfs3/Kconfig" +source "fs/ntfsplus/Kconfig" endmenu endif # BLOCKfs/Makefile +0
-0fs/Makefile obj-y += unicode/ · line 94
Quoted 4 lines
fs/Makefile +1
-0@@ -91,6 +91,7 @@ obj-y += unicode/ obj-$(CONFIG_SMBFS) += smb/ obj-$(CONFIG_HPFS_FS) += hpfs/ obj-$(CONFIG_NTFS3_FS) += ntfs3/ +obj-$(CONFIG_NTFSPLUS_FS) += ntfsplus/I suggested in another reply to keep the original ntfs name
Quoted 3 lines
More important is to keep your driver linked before the unmaintained ntfs3, so that it hopefully gets picked up before ntfs3 for auto mount type if both drivers are built-in.
Okay, I will check it:)
Quoted 3 lines
I am not sure if keeping the order here would guarantee the link/registration order. If not, it may make sense to mutually exclude them as built-in drivers.
Okay, I am leaning towards the latter. If you have no objection, I will add the patch to mutually excluding the two ntfs implementation. Thanks!
Quoted 3 lines
Thanks,
Amir.
Re: [PATCH v2 11/11] ntfsplus: add Kconfig and Makefile
Quoted 41 lines · Namjae Jeon
Quoted 39 lines · Amir Goldstein
Quoted 30 lines · Namjae Jeon
This adds the
KconfigandMakefilefor ntfsplus.Signed-off-by Namjae Jeon <[email protected]>--- fs/Kconfig | 1 + fs/Makefile | 1 + fs/ntfsplus/Kconfig | 45 ++++++++++++++++++++++++++++++++++++++++++++ fs/ntfsplus/Makefile | 18 ++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 fs/ntfsplus/Kconfig create mode 100644 fs/ntfsplus/Makefilefs/Kconfig +1
-0@@ -153,6 +153,7 @@ menu "DOS/FAT/EXFAT/NT Filesystems" source "fs/fat/Kconfig" source "fs/exfat/Kconfig" source "fs/ntfs3/Kconfig" +source "fs/ntfsplus/Kconfig" endmenu endif # BLOCKfs/Makefile +0
-0fs/Makefile obj-y += unicode/ · line 94
Quoted 4 lines
fs/Makefile +1
-0@@ -91,6 +91,7 @@ obj-y += unicode/ obj-$(CONFIG_SMBFS) += smb/ obj-$(CONFIG_HPFS_FS) += hpfs/ obj-$(CONFIG_NTFS3_FS) += ntfs3/ +obj-$(CONFIG_NTFSPLUS_FS) += ntfsplus/I suggested in another reply to keep the original ntfs name
Quoted 8 lines
Quoted 3 lines
More important is to keep your driver linked before the unmaintained ntfs3, so that it hopefully gets picked up before ntfs3 for auto mount type if both drivers are built-in.
Okay, I will check it:)
Quoted 3 lines
I am not sure if keeping the order here would guarantee the link/registration order. If not, it may make sense to mutually exclude them as built-in drivers.
Okay, I am leaning towards the latter.
Well it's not this OR that.
please add you driver as the original was before ntfs3
obj-$(CONFIG_NTFS_FS) += ntfs/
obj-$(CONFIG_NTFS3_FS) += ntfs3/
Quoted 1 line
If you have no objection, I will add the patch to mutually exclude the two ntfs implementation.
You should definitely allow them both if at least one is built as a module I think it would be valuable for testing.
Just thatCONFIG_NTFS_FS=yCONFIG_NTFS3_FS=y
I don't see the usefulness in allowing that.
(other people may disagree)
I think that the way to implement it is using an auxiliary choice config var in fs/Kconfig (i.e. CONFIG_DEFAULT_NTFS) and select/depends statements to only allow the default ntfs driver to be configured as 'y', but couldn't find a good example to point you at.
Thanks,
Amir.
Quoted 17 lines · Namjae Jeon
Quoted 6 lines · Amir Goldstein
Quoted 4 lines · Namjae Jeon
Introduction
============Quoted 9 lines
Quoted 8 lines
The NTFS filesystem[1] still remains the default filesystem for Windows and The well-maintained NTFS driver in the Linux kernel enhances interoperability with Windows devices, making it easier for Linux users to work with NTFS-formatted drives. Currently, ntfs support in Linux was the long-neglected NTFS Classic (read-only), which has been removed from the Linux kernel, leaving the poorly maintained ntfs3. ntfs3 still has many problems and is poorly maintained, so users and distributions are still using the old legacy ntfs-3g.
Quoted 7 lines
Hi Amir,
Quoted 4 lines
May I suggest that you add a patch to your series to add a deprecation message to ntfs3?
See for example
eb103a51640ee("reiserfs: Deprecate reiserfs")Okay, I'll add it in the next version, referring to this reiserfs patch.
Quoted 1 line
There is no need to refer to this patch, there is nothing special about it. It's just an example for you of past deprecation procedures.
Unlike resierfs, the deprecation warning and help text for ntfs3 should refer users to the better in-tree alternative.
Thanks,
Amir.
Quoted 297 lines
Quoted 15 lines
Quoted 5 lines
What is ntfsplus?
=================Quoted 9 lines
The remade ntfs called ntfsplus is an implementation that supports write and the essential
requirements(iomap, no buffer-head, utilities, xfstests test result)based on read-only classic NTFS.
The old read-only ntfs code is much cleaner, with extensive comments, offers readability that makes understanding NTFS easier. This is why ntfsplus was developed on old read-only NTFS base.
The target is to provide currenttrends(iomap, no buffer head, folio), enhanced performance, stable maintenance, utility support including fsck.Quoted 3 lines
You are bringing back the old ntfs driver code from the dead, preserving the code and Copyrights and everything to bring it up to speed with modern vfs API and to add super nice features. Right?
Yes.
Quoted 3 lines
Apart from its history, the new refurbished ntfs driver is also fully backward compact to the old read-only driver. Right?
Yes.
Quoted 6 lines
Why is the rebranding to ntfsplus useful then?
I can understand that you want a new name for a new ntfsprogs-plus project which is a fork of ntfs-3g, but I don't think that the new name for the kernel driver is useful or welcome.
Right, I wanted to rebrand ntfsprogs-plus and ntfsplus into a paired set of names. Also, ntfs3 was already used as an alias for ntfs, so I couldn't touch ntfs3 driver without consensus from the fs maintainers.
Quoted 2 lines
Do you have any objections to leaving its original ntfs name?
I have no objection to using ntfsplus as an alias for ntfs if we add a deprecation message to ntfs3.
Quoted 4 lines
You can also do:
MODULE_ALIAS_FS("ntfs");MODULE_ALIAS_FS("ntfsplus");I will add this in the next version with ntfs3 deprecation patch.
Quoted 2 lines
If that is useful for ntfsprogs-plus somehow.
That is very useful and thanks for your review!
Quoted 253 lines
Thanks,
Amir.Quoted 249 lines
Key Features
============- Write support:
Implement write support on classic read-only NTFS. Additionally, integrate delayed allocation to enhance write performance through multi-cluster allocation and minimized fragmentation of cluster bitmap.- Switch to using iomap:
Use iomap for buffered IO writes, reads, direct IO, file extent mapping, readpages, writepages operations.- Stop using the buffer head:
The use of buffer head in old ntfs and switched to use folio instead. As a result,CONFIG_BUFFER_HEADoption enable is removed inKconfigalso.- Public utilities include fsck[2]:
While ntfs-3g includes ntfsprogs as a component, it notably lacks the fsck implementation. So we have launched a new ntfs utilitiies project called ntfsprogs-plus by forking from ntfs-3g after removing unnecessary ntfs fuse implementation. fsck.ntfs can be used for ntfs testing with xfstests as well as for recovering corrupted NTFS device.- Performance Enhancements:
- ntfsplus vs. ntfs3:
* Performance was benchmarked using iozone with various chunk size.
- In single-thread(1T) write tests, ntfsplus show approximately 3~5% better performance.
- In multi-thread(4T) write tests, ntfsplus show approximately 35~110% better performance.
- Read throughput is identical for both ntfs implementations.1GB file size:4096 size:16384 size:65536
MB/sec ntfsplus | ntfs3 ntfsplus | ntfs3 ntfsplus | ntfs3
─────────────────────────────────────────────────────────────────
read 399 | 399 426 | 424 429 | 430
─────────────────────────────────────────────────────────────────
write(1T) 291 | 276 325 | 305 333 | 317
write(4T) 105 | 50 113 | 78 114 | 99.6* File list browsing performance. (about 12~14% faster)
files:100000 files:200000 files:400000
Sec ntfsplus | ntfs3 ntfsplus | ntfs3 ntfsplus | ntfs3
─────────────────────────────────────────────────────────────────
ls -lR 7.07 | 8.10 14.03 | 16.35 28.27 | 32.86* mount time.
parti_size:1TB parti_size:2TB parti_size:4TB
Sec ntfsplus | ntfs3 ntfsplus | ntfs3 ntfsplus | ntfs3
─────────────────────────────────────────────────────────────────
mount 0.38 | 2.03 0.39 | 2.25 0.70 | 4.51The following are the reasons why ntfsplus performance is higher
compared to ntfs3:
- Use iomap aops.
- Delayed allocation support.
- Optimize zero out for newly allocated clusters.
- Optimize runlist merge overhead with small chunck size.
- pre-load mft(inode) blocks and index(dentry) blocks to improve readdir + stat performance.
- Load lcn bitmap on background.- Stability improvement:
a. Pass more xfstests tests:
ntfsplus passed 287 tests, significantly higher than ntfs3's 218. ntfsplus implement fallocate, idmapped mount and permission, etc, resulting in a significantly high number of xfstests passing compared to ntfs3.
b. Bonnie++ issue[3]:
The Bonnie++ benchmark fails on ntfs3 with a "Directory not empty" error during file deletion. ntfs3 currently iterates directory entries by reading index blocks one by one. When entries are deleted concurrently, index block merging or entry relocation can causereaddir()to skip some entries, leaving files undeleted in workloads(bonnie++) that mix unlink and directory scans. ntfsplus implement leaf chain traversal in readdir to avoid entry skip on deletion.- Journaling support:
ntfs3 does not provide full journaling support. It only implement journal replay[4], which in our testing did not function correctly. My next task after upstreaming will be to add full journal support to ntfsplus.The feature comparison summary
==============================Feature ntfsplus ntfs3 =================================== ======== =========== Write support Yes Yes iomap support Yes No No buffer head Yes No Public utilities(mkfs, fsck, etc.) Yes No xfstests passed 287 218 Idmapped mount Yes No Delayed allocation Yes No Bonnie++ Pass Fail Journaling Planned Inoperative =================================== ======== ===========References
==========
[1] https://en.wikipedia.org/wiki/NTFS
[2] https://github.com/ntfsprogs-plus/ntfsprogs-plus
[3] https://lore.kernel.org/ntfs3/CAOZgwEd7NDkGEpdF6UQTcbYuupDavaHBoj4WwTy3Qe4Bqm6V0g@mail.gmail.com/
[4] https://marc.info/?l=linux-fsdevel&m=161738417018673&q=mboxAvailable in the Git repository at:
===================================git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/ntfs.gitntfs-nextv2:
- Add ntfs3-compatible mountoptions(sys_immutable, nohidden, hide_dot_files, nocase, acl, windows_names, disable_sparse, discard).
- Add iocharset mount option.
- Add ntfs3-compatible dos attribute and ntfs attribute load/store in setxattr/getattr().
- Add support for FS_IOC_{GET,SET}FSLABEL ioctl.
- Add support for FITRIM ioctl.
- Fix thewarnings(duplicate symbol, __divdi3, etc)from kernel test robot.
- Prefixpr_xxx()with ntfsplus.
- Add support for $MFT File extension.
- AddDocumentation/filesystems/ntfsplus.rst.
- Mark experimental.
- Remove BUG traps warnings from checkpatch.pl.Namjae Jeon (11):
ntfsplus: in-memory, on-disk structures and headers
ntfsplus: add super block operations
ntfsplus: add inode operations
ntfsplus: add directory operations
ntfsplus: add file operations
ntfsplus: add iomap and address space operations
ntfsplus: add attrib operatrions
ntfsplus: add runlist handling and cluster allocator
ntfsplus: add reparse and ea operations
ntfsplus: add misc operations
ntfsplus: addKconfigandMakefileDocumentation/filesystems/index.rst | 1 + Documentation/filesystems/ntfsplus.rst | 199 + fs/Kconfig | 1 + fs/Makefile | 1 + fs/ntfsplus/Kconfig | 45 + fs/ntfsplus/Makefile | 18 + fs/ntfsplus/aops.c | 617 +++ fs/ntfsplus/aops.h | 92 + fs/ntfsplus/attrib.c | 5377 ++++++++++++++++++++++++ fs/ntfsplus/attrib.h | 159 + fs/ntfsplus/attrlist.c | 285 ++ fs/ntfsplus/attrlist.h | 21 + fs/ntfsplus/bitmap.c | 290 ++ fs/ntfsplus/bitmap.h | 93 + fs/ntfsplus/collate.c | 178 + fs/ntfsplus/collate.h | 37 + fs/ntfsplus/compress.c | 1564 +++++++ fs/ntfsplus/dir.c | 1230 ++++++ fs/ntfsplus/dir.h | 33 + fs/ntfsplus/ea.c | 931 ++++ fs/ntfsplus/ea.h | 25 + fs/ntfsplus/file.c | 1142 +++++ fs/ntfsplus/index.c | 2112 ++++++++++ fs/ntfsplus/index.h | 127 + fs/ntfsplus/inode.c | 3729 ++++++++++++++++ fs/ntfsplus/inode.h | 353 ++ fs/ntfsplus/layout.h | 2288 ++++++++++ fs/ntfsplus/lcnalloc.c | 1012 +++++ fs/ntfsplus/lcnalloc.h | 127 + fs/ntfsplus/logfile.c | 770 ++++ fs/ntfsplus/logfile.h | 316 ++ fs/ntfsplus/mft.c | 2698 ++++++++++++ fs/ntfsplus/mft.h | 92 + fs/ntfsplus/misc.c | 213 + fs/ntfsplus/misc.h | 218 + fs/ntfsplus/mst.c | 195 + fs/ntfsplus/namei.c | 1677 ++++++++ fs/ntfsplus/ntfs.h | 180 + fs/ntfsplus/ntfs_iomap.c | 700 +++ fs/ntfsplus/ntfs_iomap.h | 22 + fs/ntfsplus/reparse.c | 550 +++ fs/ntfsplus/reparse.h | 15 + fs/ntfsplus/runlist.c | 1983 +++++++++ fs/ntfsplus/runlist.h | 91 + fs/ntfsplus/super.c | 2865 +++++++++++++ fs/ntfsplus/unistr.c | 473 +++ fs/ntfsplus/upcase.c | 73 + fs/ntfsplus/volume.h | 254 ++ include/uapi/linux/ntfs.h | 23 + 49 files changed, 35495 insertions(+) create mode 100644 Documentation/filesystems/ntfsplus.rst create mode 100644 fs/ntfsplus/Kconfig create mode 100644 fs/ntfsplus/Makefile create mode 100644 fs/ntfsplus/aops.c create mode 100644 fs/ntfsplus/aops.h create mode 100644 fs/ntfsplus/attrib.c create mode 100644 fs/ntfsplus/attrib.h create mode 100644 fs/ntfsplus/attrlist.c create mode 100644 fs/ntfsplus/attrlist.h create mode 100644 fs/ntfsplus/bitmap.c create mode 100644 fs/ntfsplus/bitmap.h create mode 100644 fs/ntfsplus/collate.c create mode 100644 fs/ntfsplus/collate.h create mode 100644 fs/ntfsplus/compress.c create mode 100644 fs/ntfsplus/dir.c create mode 100644 fs/ntfsplus/dir.h create mode 100644 fs/ntfsplus/ea.c create mode 100644 fs/ntfsplus/ea.h create mode 100644 fs/ntfsplus/file.c create mode 100644 fs/ntfsplus/index.c create mode 100644 fs/ntfsplus/index.h create mode 100644 fs/ntfsplus/inode.c create mode 100644 fs/ntfsplus/inode.h create mode 100644 fs/ntfsplus/layout.h create mode 100644 fs/ntfsplus/lcnalloc.c create mode 100644 fs/ntfsplus/lcnalloc.h create mode 100644 fs/ntfsplus/logfile.c create mode 100644 fs/ntfsplus/logfile.h create mode 100644 fs/ntfsplus/mft.c create mode 100644 fs/ntfsplus/mft.h create mode 100644 fs/ntfsplus/misc.c create mode 100644 fs/ntfsplus/misc.h create mode 100644 fs/ntfsplus/mst.c create mode 100644 fs/ntfsplus/namei.c create mode 100644 fs/ntfsplus/ntfs.h create mode 100644 fs/ntfsplus/ntfs_iomap.c create mode 100644 fs/ntfsplus/ntfs_iomap.h create mode 100644 fs/ntfsplus/reparse.c create mode 100644 fs/ntfsplus/reparse.h create mode 100644 fs/ntfsplus/runlist.c create mode 100644 fs/ntfsplus/runlist.h create mode 100644 fs/ntfsplus/super.c create mode 100644 fs/ntfsplus/unistr.c create mode 100644 fs/ntfsplus/upcase.c create mode 100644 fs/ntfsplus/volume.h create mode 100644 include/uapi/linux/ntfs.h--
2.25.1
Quoted 8 lines · Namjae Jeon
Quoted 5 lines
Why is the rebranding to ntfsplus useful then?
I can understand that you want a new name for a new ntfsprogs-plus project which is a fork of ntfs-3g, but I don't think that the new name for the kernel driver is useful or welcome.
Right, I wanted to rebrand ntfsprogs-plus and ntfsplus into a paired set of names. Also, ntfs3 was already used as an alias for ntfs, so I couldn't touch ntfs3 driver without consensus from the fs maintainers.
I think you're adding more confusion than you're removing with the name change. Please, just call it ntfs. We have hfs and hfsplus already, and those refer to different filesystems. We should just call this ntfs.
Quoted 24 lines · Amir Goldstein
Quoted 22 lines · Namjae Jeon
Quoted 6 lines · Amir Goldstein
Quoted 4 lines · Namjae Jeon
Introduction
============Quoted 9 lines
Quoted 8 lines
The NTFS filesystem[1] still remains the default filesystem for Windows and The well-maintained NTFS driver in the Linux kernel enhances interoperability with Windows devices, making it easier for Linux users to work with NTFS-formatted drives. Currently, ntfs support in Linux was the long-neglected NTFS Classic (read-only), which has been removed from the Linux kernel, leaving the poorly maintained ntfs3. ntfs3 still has many problems and is poorly maintained, so users and distributions are still using the old legacy ntfs-3g.
Hi Amir,
Quoted 4 lines
May I suggest that you add a patch to your series to add a deprecation message to ntfs3?
See for example
eb103a51640ee("reiserfs: Deprecate reiserfs")
Quoted 8 lines
Quoted 2 lines
Okay, I'll add it in the next version, referring to this reiserfs patch.
Quoted 1 line
There is no need to refer to this patch, there is nothing special about it. It's just an example for you of past deprecation procedures.
Unlike resierfs, the deprecation warning and help text for ntfs3 should refer users to the better in-tree alternative.
Okay, I will do that.
Thanks!
Quoted 301 lines
Thanks,
Amir.Quoted 297 lines
Quoted 15 lines
Quoted 5 lines
What is ntfsplus?
=================Quoted 9 lines
The remade ntfs called ntfsplus is an implementation that supports write and the essential
requirements(iomap, no buffer-head, utilities, xfstests test result)based on read-only classic NTFS.
The old read-only ntfs code is much cleaner, with extensive comments, offers readability that makes understanding NTFS easier. This is why ntfsplus was developed on old read-only NTFS base.
The target is to provide currenttrends(iomap, no buffer head, folio), enhanced performance, stable maintenance, utility support including fsck.Quoted 3 lines
You are bringing back the old ntfs driver code from the dead, preserving the code and Copyrights and everything to bring it up to speed with modern vfs API and to add super nice features. Right?
Yes.
Quoted 3 lines
Apart from its history, the new refurbished ntfs driver is also fully backward compact to the old read-only driver. Right?
Yes.
Quoted 6 lines
Why is the rebranding to ntfsplus useful then?
I can understand that you want a new name for a new ntfsprogs-plus project which is a fork of ntfs-3g, but I don't think that the new name for the kernel driver is useful or welcome.
Right, I wanted to rebrand ntfsprogs-plus and ntfsplus into a paired set of names. Also, ntfs3 was already used as an alias for ntfs, so I couldn't touch ntfs3 driver without consensus from the fs maintainers.
Quoted 2 lines
Do you have any objections to leaving its original ntfs name?
I have no objection to using ntfsplus as an alias for ntfs if we add a deprecation message to ntfs3.
Quoted 4 lines
You can also do:
MODULE_ALIAS_FS("ntfs");MODULE_ALIAS_FS("ntfsplus");I will add this in the next version with ntfs3 deprecation patch.
Quoted 2 lines
If that is useful for ntfsprogs-plus somehow.
That is very useful and thanks for your review!
Quoted 253 lines
Thanks,
Amir.Quoted 249 lines
Key Features
============- Write support:
Implement write support on classic read-only NTFS. Additionally, integrate delayed allocation to enhance write performance through multi-cluster allocation and minimized fragmentation of cluster bitmap.- Switch to using iomap:
Use iomap for buffered IO writes, reads, direct IO, file extent mapping, readpages, writepages operations.- Stop using the buffer head:
The use of buffer head in old ntfs and switched to use folio instead. As a result,CONFIG_BUFFER_HEADoption enable is removed inKconfigalso.- Public utilities include fsck[2]:
While ntfs-3g includes ntfsprogs as a component, it notably lacks the fsck implementation. So we have launched a new ntfs utilitiies project called ntfsprogs-plus by forking from ntfs-3g after removing unnecessary ntfs fuse implementation. fsck.ntfs can be used for ntfs testing with xfstests as well as for recovering corrupted NTFS device.- Performance Enhancements:
- ntfsplus vs. ntfs3:
* Performance was benchmarked using iozone with various chunk size.
- In single-thread(1T) write tests, ntfsplus show approximately 3~5% better performance.
- In multi-thread(4T) write tests, ntfsplus show approximately 35~110% better performance.
- Read throughput is identical for both ntfs implementations.1GB file size:4096 size:16384 size:65536
MB/sec ntfsplus | ntfs3 ntfsplus | ntfs3 ntfsplus | ntfs3
─────────────────────────────────────────────────────────────────
read 399 | 399 426 | 424 429 | 430
─────────────────────────────────────────────────────────────────
write(1T) 291 | 276 325 | 305 333 | 317
write(4T) 105 | 50 113 | 78 114 | 99.6* File list browsing performance. (about 12~14% faster)
files:100000 files:200000 files:400000
Sec ntfsplus | ntfs3 ntfsplus | ntfs3 ntfsplus | ntfs3
─────────────────────────────────────────────────────────────────
ls -lR 7.07 | 8.10 14.03 | 16.35 28.27 | 32.86* mount time.
parti_size:1TB parti_size:2TB parti_size:4TB
Sec ntfsplus | ntfs3 ntfsplus | ntfs3 ntfsplus | ntfs3
─────────────────────────────────────────────────────────────────
mount 0.38 | 2.03 0.39 | 2.25 0.70 | 4.51The following are the reasons why ntfsplus performance is higher
compared to ntfs3:
- Use iomap aops.
- Delayed allocation support.
- Optimize zero out for newly allocated clusters.
- Optimize runlist merge overhead with small chunck size.
- pre-load mft(inode) blocks and index(dentry) blocks to improve readdir + stat performance.
- Load lcn bitmap on background.- Stability improvement:
a. Pass more xfstests tests:
ntfsplus passed 287 tests, significantly higher than ntfs3's 218. ntfsplus implement fallocate, idmapped mount and permission, etc, resulting in a significantly high number of xfstests passing compared to ntfs3.
b. Bonnie++ issue[3]:
The Bonnie++ benchmark fails on ntfs3 with a "Directory not empty" error during file deletion. ntfs3 currently iterates directory entries by reading index blocks one by one. When entries are deleted concurrently, index block merging or entry relocation can causereaddir()to skip some entries, leaving files undeleted in workloads(bonnie++) that mix unlink and directory scans. ntfsplus implement leaf chain traversal in readdir to avoid entry skip on deletion.- Journaling support:
ntfs3 does not provide full journaling support. It only implement journal replay[4], which in our testing did not function correctly. My next task after upstreaming will be to add full journal support to ntfsplus.The feature comparison summary
==============================Feature ntfsplus ntfs3 =================================== ======== =========== Write support Yes Yes iomap support Yes No No buffer head Yes No Public utilities(mkfs, fsck, etc.) Yes No xfstests passed 287 218 Idmapped mount Yes No Delayed allocation Yes No Bonnie++ Pass Fail Journaling Planned Inoperative =================================== ======== ===========References
==========
[1] https://en.wikipedia.org/wiki/NTFS
[2] https://github.com/ntfsprogs-plus/ntfsprogs-plus
[3] https://lore.kernel.org/ntfs3/CAOZgwEd7NDkGEpdF6UQTcbYuupDavaHBoj4WwTy3Qe4Bqm6V0g@mail.gmail.com/
[4] https://marc.info/?l=linux-fsdevel&m=161738417018673&q=mboxAvailable in the Git repository at:
===================================git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/ntfs.gitntfs-nextv2:
- Add ntfs3-compatible mountoptions(sys_immutable, nohidden, hide_dot_files, nocase, acl, windows_names, disable_sparse, discard).
- Add iocharset mount option.
- Add ntfs3-compatible dos attribute and ntfs attribute load/store in setxattr/getattr().
- Add support for FS_IOC_{GET,SET}FSLABEL ioctl.
- Add support for FITRIM ioctl.
- Fix thewarnings(duplicate symbol, __divdi3, etc)from kernel test robot.
- Prefixpr_xxx()with ntfsplus.
- Add support for $MFT File extension.
- AddDocumentation/filesystems/ntfsplus.rst.
- Mark experimental.
- Remove BUG traps warnings from checkpatch.pl.Namjae Jeon (11):
ntfsplus: in-memory, on-disk structures and headers
ntfsplus: add super block operations
ntfsplus: add inode operations
ntfsplus: add directory operations
ntfsplus: add file operations
ntfsplus: add iomap and address space operations
ntfsplus: add attrib operatrions
ntfsplus: add runlist handling and cluster allocator
ntfsplus: add reparse and ea operations
ntfsplus: add misc operations
ntfsplus: addKconfigandMakefileDocumentation/filesystems/index.rst | 1 + Documentation/filesystems/ntfsplus.rst | 199 + fs/Kconfig | 1 + fs/Makefile | 1 + fs/ntfsplus/Kconfig | 45 + fs/ntfsplus/Makefile | 18 + fs/ntfsplus/aops.c | 617 +++ fs/ntfsplus/aops.h | 92 + fs/ntfsplus/attrib.c | 5377 ++++++++++++++++++++++++ fs/ntfsplus/attrib.h | 159 + fs/ntfsplus/attrlist.c | 285 ++ fs/ntfsplus/attrlist.h | 21 + fs/ntfsplus/bitmap.c | 290 ++ fs/ntfsplus/bitmap.h | 93 + fs/ntfsplus/collate.c | 178 + fs/ntfsplus/collate.h | 37 + fs/ntfsplus/compress.c | 1564 +++++++ fs/ntfsplus/dir.c | 1230 ++++++ fs/ntfsplus/dir.h | 33 + fs/ntfsplus/ea.c | 931 ++++ fs/ntfsplus/ea.h | 25 + fs/ntfsplus/file.c | 1142 +++++ fs/ntfsplus/index.c | 2112 ++++++++++ fs/ntfsplus/index.h | 127 + fs/ntfsplus/inode.c | 3729 ++++++++++++++++ fs/ntfsplus/inode.h | 353 ++ fs/ntfsplus/layout.h | 2288 ++++++++++ fs/ntfsplus/lcnalloc.c | 1012 +++++ fs/ntfsplus/lcnalloc.h | 127 + fs/ntfsplus/logfile.c | 770 ++++ fs/ntfsplus/logfile.h | 316 ++ fs/ntfsplus/mft.c | 2698 ++++++++++++ fs/ntfsplus/mft.h | 92 + fs/ntfsplus/misc.c | 213 + fs/ntfsplus/misc.h | 218 + fs/ntfsplus/mst.c | 195 + fs/ntfsplus/namei.c | 1677 ++++++++ fs/ntfsplus/ntfs.h | 180 + fs/ntfsplus/ntfs_iomap.c | 700 +++ fs/ntfsplus/ntfs_iomap.h | 22 + fs/ntfsplus/reparse.c | 550 +++ fs/ntfsplus/reparse.h | 15 + fs/ntfsplus/runlist.c | 1983 +++++++++ fs/ntfsplus/runlist.h | 91 + fs/ntfsplus/super.c | 2865 +++++++++++++ fs/ntfsplus/unistr.c | 473 +++ fs/ntfsplus/upcase.c | 73 + fs/ntfsplus/volume.h | 254 ++ include/uapi/linux/ntfs.h | 23 + 49 files changed, 35495 insertions(+) create mode 100644 Documentation/filesystems/ntfsplus.rst create mode 100644 fs/ntfsplus/Kconfig create mode 100644 fs/ntfsplus/Makefile create mode 100644 fs/ntfsplus/aops.c create mode 100644 fs/ntfsplus/aops.h create mode 100644 fs/ntfsplus/attrib.c create mode 100644 fs/ntfsplus/attrib.h create mode 100644 fs/ntfsplus/attrlist.c create mode 100644 fs/ntfsplus/attrlist.h create mode 100644 fs/ntfsplus/bitmap.c create mode 100644 fs/ntfsplus/bitmap.h create mode 100644 fs/ntfsplus/collate.c create mode 100644 fs/ntfsplus/collate.h create mode 100644 fs/ntfsplus/compress.c create mode 100644 fs/ntfsplus/dir.c create mode 100644 fs/ntfsplus/dir.h create mode 100644 fs/ntfsplus/ea.c create mode 100644 fs/ntfsplus/ea.h create mode 100644 fs/ntfsplus/file.c create mode 100644 fs/ntfsplus/index.c create mode 100644 fs/ntfsplus/index.h create mode 100644 fs/ntfsplus/inode.c create mode 100644 fs/ntfsplus/inode.h create mode 100644 fs/ntfsplus/layout.h create mode 100644 fs/ntfsplus/lcnalloc.c create mode 100644 fs/ntfsplus/lcnalloc.h create mode 100644 fs/ntfsplus/logfile.c create mode 100644 fs/ntfsplus/logfile.h create mode 100644 fs/ntfsplus/mft.c create mode 100644 fs/ntfsplus/mft.h create mode 100644 fs/ntfsplus/misc.c create mode 100644 fs/ntfsplus/misc.h create mode 100644 fs/ntfsplus/mst.c create mode 100644 fs/ntfsplus/namei.c create mode 100644 fs/ntfsplus/ntfs.h create mode 100644 fs/ntfsplus/ntfs_iomap.c create mode 100644 fs/ntfsplus/ntfs_iomap.h create mode 100644 fs/ntfsplus/reparse.c create mode 100644 fs/ntfsplus/reparse.h create mode 100644 fs/ntfsplus/runlist.c create mode 100644 fs/ntfsplus/runlist.h create mode 100644 fs/ntfsplus/super.c create mode 100644 fs/ntfsplus/unistr.c create mode 100644 fs/ntfsplus/upcase.c create mode 100644 fs/ntfsplus/volume.h create mode 100644 include/uapi/linux/ntfs.h--
2.25.1
Quoted 7 lines · Matthew Wilcox
Quoted 5 lines · Namjae Jeon
Quoted 5 lines
Why is the rebranding to ntfsplus useful then?
I can understand that you want a new name for a new ntfsprogs-plus project which is a fork of ntfs-3g, but I don't think that the new name for the kernel driver is useful or welcome.
Quoted 7 lines
Quoted 3 lines
Right, I wanted to rebrand ntfsprogs-plus and ntfsplus into a paired set of names. Also, ntfs3 was already used as an alias for ntfs, so I couldn't touch ntfs3 driver without consensus from the fs maintainers.
I think you're adding more confusion than you're removing with the name change. Please, just call it ntfs. We have hfs and hfsplus already, and those refer to different filesystems. We should just call this ntfs.
Okay, I will change it back to the name "ntfs" in the next version. Thanks!
On Thu, 27 Nov 2025 13:59:33 +0900
Namjae Jeon <[email protected]> wrote:
Hello Namjae,
Thank you for posting this patchset. We are very interested in the development of ntfsplus.
In our production environment, we have been relying on the out-of-tree ntfs-3g driver for NTFS read-write support. However, it comes with several limitations regarding performance and integration. While we have been closely monitoring the in-kernel ntfs3 driver, we feel that features like full journaling support and a robust fsck utility are critical for our use cases, and we have been waiting for these to mature.
Given your proven track record with the exfat driver upstreaming and maintenance, we are confident in the quality and future of this ntfsplus initiative. We are hopeful that it will address the long-standing gaps we've observed.
We are eagerly following the progress of ntfsplus. Once it reaches a stable and feature-complete state—especially with reliable journaling and fsck—we would seriously consider deploying it to replace ntfs-3g in our production systems.
Quoted 13 lines
Introduction
============The NTFS filesystem[1] still remains the default filesystem for Windows and The well-maintained NTFS driver in the Linux kernel enhances interoperability with Windows devices, making it easier for Linux users to work with NTFS-formatted drives. Currently, ntfs support in Linux was the long-neglected NTFS Classic (read-only), which has been removed from the Linux kernel, leaving the poorly maintained ntfs3. ntfs3 still has many problems and is poorly maintained, so users and distributions are still using the old legacy ntfs-3g.
Signature · 2 lines
Thanks, Winston
Re: [PATCH v2 11/11] ntfsplus: add Kconfig and Makefile
In reply to Amir GoldsteinQuoted 50 lines · Amir Goldstein
Quoted 48 lines · Namjae Jeon
Quoted 39 lines · Amir Goldstein
Quoted 30 lines · Namjae Jeon
This adds the
KconfigandMakefilefor ntfsplus.Signed-off-by Namjae Jeon <[email protected]>--- fs/Kconfig | 1 + fs/Makefile | 1 + fs/ntfsplus/Kconfig | 45 ++++++++++++++++++++++++++++++++++++++++++++ fs/ntfsplus/Makefile | 18 ++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 fs/ntfsplus/Kconfig create mode 100644 fs/ntfsplus/Makefilefs/Kconfig +1
-0@@ -153,6 +153,7 @@ menu "DOS/FAT/EXFAT/NT Filesystems" source "fs/fat/Kconfig" source "fs/exfat/Kconfig" source "fs/ntfs3/Kconfig" +source "fs/ntfsplus/Kconfig" endmenu endif # BLOCKfs/Makefile +0
-0fs/Makefile obj-y += unicode/ · line 94
Quoted 4 lines
fs/Makefile +1
-0@@ -91,6 +91,7 @@ obj-y += unicode/ obj-$(CONFIG_SMBFS) += smb/ obj-$(CONFIG_HPFS_FS) += hpfs/ obj-$(CONFIG_NTFS3_FS) += ntfs3/ +obj-$(CONFIG_NTFSPLUS_FS) += ntfsplus/I suggested in another reply to keep the original ntfs name
Quoted 3 lines
More important is to keep your driver linked before the unmaintained ntfs3, so that it hopefully gets picked up before ntfs3 for auto mount type if both drivers are built-in.
Okay, I will check it:)
Quoted 3 lines
I am not sure if keeping the order here would guarantee the link/registration order. If not, it may make sense to mutually exclude them as built-in drivers.
Quoted 7 lines
Quoted 1 line
Okay, I am leaning towards the latter.
Well it's not this OR that.
please add you driver as the original was before ntfs3obj-$(CONFIG_NTFS_FS) += ntfs/
obj-$(CONFIG_NTFS3_FS) += ntfs3/
Okay.
Quoted 10 lines
Quoted 2 lines
If you have no objection, I will add the patch to mutually exclude the two ntfs implementation.
You should definitely allow them both if at least one is built as a module I think it would be valuable for testing.
Just that
CONFIG_NTFS_FS=yCONFIG_NTFS3_FS=y
Quoted 7 lines
I don't see the usefulness in allowing that.
(other people may disagree)I think that the way to implement it is using an auxiliary choice config var in fs/Kconfig (i.e.
CONFIG_DEFAULT_NTFS) and select/depends statements to only allow the default ntfs driver to be configured as 'y', but couldn't find a good example to point you at.
Okay. Could you please check whether the attached patch matches what you described ?
Thanks!
Quoted 3 lines
Thanks,
Amir.
From 11154917ff53d6cf218ac58e6776e603246587b6 Mon Sep 17 00:00:00 2001
From: Namjae Jeon <[email protected]>
Date: Fri, 28 Nov 2025 11:44:45 +0900
Subject: [PATCH] ntfs: restrict built-in NTFS seclection to one driver, allow
both as modules
--- fs/Kconfig | 11 +++++++++++ fs/ntfs3/Kconfig | 2 ++ fs/ntfsplus/Kconfig | 1 + 3 files changed, 14 insertions(+)
fs/Kconfig +11 -0
@@ -155,6 +155,17 @@ source "fs/exfat/Kconfig" source "fs/ntfs3/Kconfig" source "fs/ntfsplus/Kconfig" +choice + prompt "Select built-in NTFS filesystem (only one can be built-in)" + help + Only one NTFS can be built into the kernel(y). + Both can still be built as modules(m). + + config DEFAULT_NTFSPLUS + bool "NTFS+" + config DEFAULT_NTFS3 + bool "NTFS3" +endchoice endmenu endif # BLOCK
fs/ntfs3/Kconfig +2 -0
@@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only config NTFS3_FS tristate "NTFS Read-Write file system support" + depends on !DEFAULT_NTFSPLUS || m select BUFFER_HEAD select NLS select LEGACY_DIRECT_IO
@@ -49,6 +50,7 @@ config NTFS3_FS_POSIX_ACL config NTFS_FS tristate "NTFS file system support" + depends on !DEFAULT_NTFSPLUS || m select NTFS3_FS select BUFFER_HEAD select NLS
fs/ntfsplus/Kconfig +1 -0
@@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only config NTFSPLUS_FS tristate "NTFS+ file system support (EXPERIMENTAL)" + depends on !DEFAULT_NTFS3 || m select NLS help NTFS is the file system of Microsoft Windows NT, 2000, XP and 2003.
Signature · 1 line
2.34.1
Quoted 2 lines · Winston Wen
On Thu, 27 Nov 2025 13:59:33 +0900
Namjae Jeon <[email protected]> wrote:
...
Quoted 5 lines
Given your proven track record with the exfat driver upstreaming and maintenance, we are confident in the quality and future of this ntfsplus initiative. We are hopeful that it will address the long-standing gaps we've observed.
Just for the record here, I heard about this from Namjae almost a year ago about this. Personally, I support this NTFS work simply because Namjae has a good visible reputation in the community as well as he is a real kernel filesystem developer active for more than a decade. I can imagine it will be more likely to be a success (and healthy driven by the community development) compared to previous attempts.
Currently I am lacking time for this, but I could help if needed.
Thanks,
Gao Xiang
Quoted 5 lines · Winston Wen
On Thu, 27 Nov 2025 13:59:33 +0900
Namjae Jeon <[email protected]> wrote:Hello Namjae,
Hello Winston,
Quoted 17 lines
Thank you for posting this patchset. We are very interested in the development of ntfsplus.
In our production environment, we have been relying on the out-of-tree ntfs-3g driver for NTFS read-write support. However, it comes with several limitations regarding performance and integration. While we have been closely monitoring the in-kernel ntfs3 driver, we feel that features like full journaling support and a robust fsck utility are critical for our use cases, and we have been waiting for these to mature.
Given your proven track record with the exfat driver upstreaming and maintenance, we are confident in the quality and future of this ntfsplus initiative. We are hopeful that it will address the long-standing gaps we've observed.
Quoted 4 lines
We are eagerly following the progress of ntfsplus. Once it reaches a stable and feature-complete state—especially with reliable journaling and fsck—we would seriously consider deploying it to replace ntfs-3g in our production systems.
fsck.ntfs is supported through ntfsprogs-plus, and we plan to continue updating it. I believe that fsck.ntfs will partially cover the issues caused by the lack of journaling. I don't know your usecase but I don't have any major difficulties supporting NTFS on our products using ntfsplus yet, even without journaling. And I plan to begin implementing journaling support for ntfsplus early next year, and welcome any feedback or collaboration from you. I'll make sure to CC you when sending the journaling patches to the list.
Re: [PATCH v2 11/11] ntfsplus: add Kconfig and Makefile
In reply to Namjae JeonQuoted 72 lines · Namjae Jeon
Quoted 50 lines · Amir Goldstein
Quoted 48 lines · Namjae Jeon
Quoted 39 lines · Amir Goldstein
Quoted 30 lines · Namjae Jeon
This adds the
KconfigandMakefilefor ntfsplus.Signed-off-by Namjae Jeon <[email protected]>--- fs/Kconfig | 1 + fs/Makefile | 1 + fs/ntfsplus/Kconfig | 45 ++++++++++++++++++++++++++++++++++++++++++++ fs/ntfsplus/Makefile | 18 ++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 fs/ntfsplus/Kconfig create mode 100644 fs/ntfsplus/Makefilefs/Kconfig +1
-0@@ -153,6 +153,7 @@ menu "DOS/FAT/EXFAT/NT Filesystems" source "fs/fat/Kconfig" source "fs/exfat/Kconfig" source "fs/ntfs3/Kconfig" +source "fs/ntfsplus/Kconfig" endmenu endif # BLOCKfs/Makefile +0
-0fs/Makefile obj-y += unicode/ · line 94
Quoted 4 lines
fs/Makefile +1
-0@@ -91,6 +91,7 @@ obj-y += unicode/ obj-$(CONFIG_SMBFS) += smb/ obj-$(CONFIG_HPFS_FS) += hpfs/ obj-$(CONFIG_NTFS3_FS) += ntfs3/ +obj-$(CONFIG_NTFSPLUS_FS) += ntfsplus/I suggested in another reply to keep the original ntfs name
Quoted 3 lines
More important is to keep your driver linked before the unmaintained ntfs3, so that it hopefully gets picked up before ntfs3 for auto mount type if both drivers are built-in.
Okay, I will check it:)
Quoted 3 lines
I am not sure if keeping the order here would guarantee the link/registration order. If not, it may make sense to mutually exclude them as built-in drivers.
Quoted 7 lines
Quoted 1 line
Okay, I am leaning towards the latter.
Well it's not this OR that.
please add you driver as the original was before ntfs3obj-$(CONFIG_NTFS_FS) += ntfs/
obj-$(CONFIG_NTFS3_FS) += ntfs3/Okay.
Quoted 13 lines
Quoted 2 lines
If you have no objection, I will add the patch to mutually exclude the two ntfs implementation.
You should definitely allow them both if at least one is built as a module I think it would be valuable for testing.
Just that
CONFIG_NTFS_FS=yCONFIG_NTFS3_FS=yI don't see the usefulness in allowing that.
(other people may disagree)
Quoted 6 lines
Quoted 4 lines
I think that the way to implement it is using an auxiliary choice config var in fs/Kconfig (i.e.
CONFIG_DEFAULT_NTFS) and select/depends statements to only allow the default ntfs driver to be configured as 'y', but couldn't find a good example to point you at.Okay. Could you please check whether the attached patch matches what you described ?
It's what I meant, but now I think it could be simpler...
Quoted 24 lines
Thanks!
Quoted 3 lines
Thanks,
Amir.From
11154917ff53d6cf218ac58e6776e603246587b6Mon Sep 17 00:00:00 2001
From: Namjae Jeon <[email protected]>
Date: Fri, 28 Nov 2025 11:44:45 +0900
Subject: [PATCH] ntfs: restrict built-in NTFS seclection to one driver, allow
both as modulesSigned-off-by Namjae Jeon <[email protected]>--- fs/Kconfig | 11 +++++++++++ fs/ntfs3/Kconfig | 2 ++ fs/ntfsplus/Kconfig | 1 + 3 files changed, 14 insertions(+)fs/Kconfig +0
-0
fs/Kconfig source "fs/exfat/Kconfig" · line 159
Quoted 5 lines
fs/Kconfig +2
-0@@ -155,6 +155,17 @@ source "fs/exfat/Kconfig" source "fs/ntfs3/Kconfig" source "fs/ntfsplus/Kconfig" +choice + prompt "Select built-in NTFS filesystem (only one can be built-in)"
Usually for choice vars there should be a default and usually there should be a DEFAULT_NTFS_NONE
fs/Kconfig source "fs/exfat/Kconfig" · line 165
Quoted 6 lines
fs/Kconfig +6
-0+ help + Only one NTFS can be built into the kernel(y). + Both can still be built as modules(m). + + config DEFAULT_NTFSPLUS + bool "NTFS+"
Usually, this would also 'select NTFS_FS'
Quoted 11 lines
fs/Kconfig +3
-0+ config DEFAULT_NTFS3 + bool "NTFS3" +endchoice endmenu endif # BLOCKfs/ntfs3/Kconfig +0
-0
fs/ntfs3/Kconfig line 4
Quoted 4 lines
fs/ntfs3/Kconfig +1
-0@@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only config NTFS3_FS tristate "NTFS Read-Write file system support" + depends on !DEFAULT_NTFSPLUS || m
So seeing how this condition looks, instead of aux var, it could be directly
depends on NTFS_FS != y || m
fs/ntfs3/Kconfig config NTFS3_FS_POSIX_ACL · line 51
Quoted 6 lines
fs/ntfs3/Kconfig +0
-0select BUFFER_HEAD select NLS select LEGACY_DIRECT_IO@@ -49,6 +50,7 @@ config NTFS3_FS_POSIX_ACL config NTFS_FS
This alias should definitely go a way when you add back the original NTFS_FS.
Preferably revert the commit that added the alias at the start of your series.
Quoted 10 lines
fs/ntfs3/Kconfig +1
-0tristate "NTFS file system support" + depends on !DEFAULT_NTFSPLUS || m select NTFS3_FS select BUFFER_HEAD select NLSfs/ntfsplus/Kconfig +0
-0
fs/ntfsplus/Kconfig line 4
Quoted 4 lines
fs/ntfsplus/Kconfig +1
-0@@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only config NTFSPLUS_FS tristate "NTFS+ file system support (EXPERIMENTAL)" + depends on !DEFAULT_NTFS3 || m
Likewise:
depends on NTFS3_FS != y || m
Obviously, not tested.
If this works, I don't think there is a need for the choice variable
As long as you keep the order in fs/Kconfig correct (ntfs before ntfs3) I assume that an oldconfig with both set to y NTFS_FS will win? Please verify that.
Thanks,
Amir.