Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * |
| 4 | * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved. |
| 5 | * |
| 6 | * |
| 7 | * terminology |
| 8 | * |
| 9 | * cluster - allocation unit - 512,1K,2K,4K,...,2M |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 10 | * vcn - virtual cluster number - Offset inside the file in clusters. |
| 11 | * vbo - virtual byte offset - Offset inside the file in bytes. |
| 12 | * lcn - logical cluster number - 0 based cluster in clusters heap. |
| 13 | * lbo - logical byte offset - Absolute position inside volume. |
| 14 | * run - maps VCN to LCN - Stored in attributes in packed form. |
| 15 | * attr - attribute segment - std/name/data etc records inside MFT. |
| 16 | * mi - MFT inode - One MFT record(usually 1024 bytes or 4K), consists of attributes. |
| 17 | * ni - NTFS inode - Extends linux inode. consists of one or more mft inodes. |
| 18 | * index - unit inside directory - 2K, 4K, <=page size, does not depend on cluster size. |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 19 | * |
| 20 | * WSL - Windows Subsystem for Linux |
| 21 | * https://docs.microsoft.com/en-us/windows/wsl/file-permissions |
| 22 | * It stores uid/gid/mode/dev in xattr |
| 23 | * |
Konstantin Komarov | bd6ae04 | 2022-09-09 19:12:31 +0300 | [diff] [blame] | 24 | * ntfs allows up to 2^64 clusters per volume. |
| 25 | * It means you should use 64 bits lcn to operate with ntfs. |
| 26 | * Implementation of ntfs.sys uses only 32 bits lcn. |
| 27 | * Default ntfs3 uses 32 bits lcn too. |
| 28 | * ntfs3 built with CONFIG_NTFS3_64BIT_CLUSTER (ntfs3_64) uses 64 bits per lcn. |
| 29 | * |
| 30 | * |
| 31 | * ntfs limits, cluster size is 4K (2^12) |
| 32 | * ----------------------------------------------------------------------------- |
| 33 | * | Volume size | Clusters | ntfs.sys | ntfs3 | ntfs3_64 | mkntfs | chkdsk | |
| 34 | * ----------------------------------------------------------------------------- |
| 35 | * | < 16T, 2^44 | < 2^32 | yes | yes | yes | yes | yes | |
| 36 | * | > 16T, 2^44 | > 2^32 | no | no | yes | yes | yes | |
| 37 | * ----------------------------------------------------------|------------------ |
| 38 | * |
| 39 | * To mount large volumes as ntfs one should use large cluster size (up to 2M) |
| 40 | * The maximum volume size in this case is 2^32 * 2^21 = 2^53 = 8P |
| 41 | * |
| 42 | * ntfs limits, cluster size is 2M (2^31) |
| 43 | * ----------------------------------------------------------------------------- |
| 44 | * | < 8P, 2^54 | < 2^32 | yes | yes | yes | yes | yes | |
| 45 | * | > 8P, 2^54 | > 2^32 | no | no | yes | yes | yes | |
| 46 | * ----------------------------------------------------------|------------------ |
| 47 | * |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 48 | */ |
| 49 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 50 | #include <linux/blkdev.h> |
| 51 | #include <linux/buffer_head.h> |
| 52 | #include <linux/exportfs.h> |
| 53 | #include <linux/fs.h> |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 54 | #include <linux/fs_context.h> |
| 55 | #include <linux/fs_parser.h> |
Kari Argillander | 528c9b3 | 2021-08-16 13:37:32 +0300 | [diff] [blame] | 56 | #include <linux/log2.h> |
Konstantin Komarov | cd39981 | 2022-05-11 19:58:36 +0300 | [diff] [blame] | 57 | #include <linux/minmax.h> |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 58 | #include <linux/module.h> |
| 59 | #include <linux/nls.h> |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 60 | #include <linux/seq_file.h> |
| 61 | #include <linux/statfs.h> |
| 62 | |
| 63 | #include "debug.h" |
| 64 | #include "ntfs.h" |
| 65 | #include "ntfs_fs.h" |
| 66 | #ifdef CONFIG_NTFS3_LZX_XPRESS |
| 67 | #include "lib/lib.h" |
| 68 | #endif |
| 69 | |
| 70 | #ifdef CONFIG_PRINTK |
| 71 | /* |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 72 | * ntfs_printk - Trace warnings/notices/errors. |
| 73 | * |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 74 | * Thanks Joe Perches <joe@perches.com> for implementation |
| 75 | */ |
| 76 | void ntfs_printk(const struct super_block *sb, const char *fmt, ...) |
| 77 | { |
| 78 | struct va_format vaf; |
| 79 | va_list args; |
| 80 | int level; |
| 81 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 82 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 83 | /* Should we use different ratelimits for warnings/notices/errors? */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 84 | if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3")) |
| 85 | return; |
| 86 | |
| 87 | va_start(args, fmt); |
| 88 | |
| 89 | level = printk_get_level(fmt); |
| 90 | vaf.fmt = printk_skip_level(fmt); |
| 91 | vaf.va = &args; |
| 92 | printk("%c%cntfs3: %s: %pV\n", KERN_SOH_ASCII, level, sb->s_id, &vaf); |
| 93 | |
| 94 | va_end(args); |
| 95 | } |
| 96 | |
| 97 | static char s_name_buf[512]; |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 98 | static atomic_t s_name_buf_cnt = ATOMIC_INIT(1); // 1 means 'free s_name_buf'. |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 99 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 100 | /* |
| 101 | * ntfs_inode_printk |
| 102 | * |
| 103 | * Print warnings/notices/errors about inode using name or inode number. |
| 104 | */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 105 | void ntfs_inode_printk(struct inode *inode, const char *fmt, ...) |
| 106 | { |
| 107 | struct super_block *sb = inode->i_sb; |
| 108 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 109 | char *name; |
| 110 | va_list args; |
| 111 | struct va_format vaf; |
| 112 | int level; |
| 113 | |
| 114 | if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3")) |
| 115 | return; |
| 116 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 117 | /* Use static allocated buffer, if possible. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 118 | name = atomic_dec_and_test(&s_name_buf_cnt) |
| 119 | ? s_name_buf |
| 120 | : kmalloc(sizeof(s_name_buf), GFP_NOFS); |
| 121 | |
| 122 | if (name) { |
| 123 | struct dentry *de = d_find_alias(inode); |
| 124 | const u32 name_len = ARRAY_SIZE(s_name_buf) - 1; |
| 125 | |
| 126 | if (de) { |
| 127 | spin_lock(&de->d_lock); |
| 128 | snprintf(name, name_len, " \"%s\"", de->d_name.name); |
| 129 | spin_unlock(&de->d_lock); |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 130 | name[name_len] = 0; /* To be sure. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 131 | } else { |
| 132 | name[0] = 0; |
| 133 | } |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 134 | dput(de); /* Cocci warns if placed in branch "if (de)" */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | va_start(args, fmt); |
| 138 | |
| 139 | level = printk_get_level(fmt); |
| 140 | vaf.fmt = printk_skip_level(fmt); |
| 141 | vaf.va = &args; |
| 142 | |
| 143 | printk("%c%cntfs3: %s: ino=%lx,%s %pV\n", KERN_SOH_ASCII, level, |
| 144 | sb->s_id, inode->i_ino, name ? name : "", &vaf); |
| 145 | |
| 146 | va_end(args); |
| 147 | |
| 148 | atomic_inc(&s_name_buf_cnt); |
| 149 | if (name != s_name_buf) |
| 150 | kfree(name); |
| 151 | } |
| 152 | #endif |
| 153 | |
| 154 | /* |
| 155 | * Shared memory struct. |
| 156 | * |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 157 | * On-disk ntfs's upcase table is created by ntfs formatter. |
| 158 | * 'upcase' table is 128K bytes of memory. |
| 159 | * We should read it into memory when mounting. |
| 160 | * Several ntfs volumes likely use the same 'upcase' table. |
| 161 | * It is good idea to share in-memory 'upcase' table between different volumes. |
| 162 | * Unfortunately winxp/vista/win7 use different upcase tables. |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 163 | */ |
| 164 | static DEFINE_SPINLOCK(s_shared_lock); |
| 165 | |
| 166 | static struct { |
| 167 | void *ptr; |
| 168 | u32 len; |
| 169 | int cnt; |
| 170 | } s_shared[8]; |
| 171 | |
| 172 | /* |
| 173 | * ntfs_set_shared |
| 174 | * |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 175 | * Return: |
| 176 | * * @ptr - If pointer was saved in shared memory. |
| 177 | * * NULL - If pointer was not shared. |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 178 | */ |
| 179 | void *ntfs_set_shared(void *ptr, u32 bytes) |
| 180 | { |
| 181 | void *ret = NULL; |
| 182 | int i, j = -1; |
| 183 | |
| 184 | spin_lock(&s_shared_lock); |
| 185 | for (i = 0; i < ARRAY_SIZE(s_shared); i++) { |
| 186 | if (!s_shared[i].cnt) { |
| 187 | j = i; |
| 188 | } else if (bytes == s_shared[i].len && |
| 189 | !memcmp(s_shared[i].ptr, ptr, bytes)) { |
| 190 | s_shared[i].cnt += 1; |
| 191 | ret = s_shared[i].ptr; |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if (!ret && j != -1) { |
| 197 | s_shared[j].ptr = ptr; |
| 198 | s_shared[j].len = bytes; |
| 199 | s_shared[j].cnt = 1; |
| 200 | ret = ptr; |
| 201 | } |
| 202 | spin_unlock(&s_shared_lock); |
| 203 | |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * ntfs_put_shared |
| 209 | * |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 210 | * Return: |
| 211 | * * @ptr - If pointer is not shared anymore. |
| 212 | * * NULL - If pointer is still shared. |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 213 | */ |
| 214 | void *ntfs_put_shared(void *ptr) |
| 215 | { |
| 216 | void *ret = ptr; |
| 217 | int i; |
| 218 | |
| 219 | spin_lock(&s_shared_lock); |
| 220 | for (i = 0; i < ARRAY_SIZE(s_shared); i++) { |
| 221 | if (s_shared[i].cnt && s_shared[i].ptr == ptr) { |
| 222 | if (--s_shared[i].cnt) |
| 223 | ret = NULL; |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | spin_unlock(&s_shared_lock); |
| 228 | |
| 229 | return ret; |
| 230 | } |
| 231 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 232 | static inline void put_mount_options(struct ntfs_mount_options *options) |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 233 | { |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 234 | kfree(options->nls_name); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 235 | unload_nls(options->nls); |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 236 | kfree(options); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | enum Opt { |
| 240 | Opt_uid, |
| 241 | Opt_gid, |
| 242 | Opt_umask, |
| 243 | Opt_dmask, |
| 244 | Opt_fmask, |
| 245 | Opt_immutable, |
| 246 | Opt_discard, |
| 247 | Opt_force, |
| 248 | Opt_sparse, |
| 249 | Opt_nohidden, |
Konstantin Komarov | 098250d | 2022-09-12 18:28:51 +0300 | [diff] [blame] | 250 | Opt_hide_dot_files, |
Daniel Pinto | 1d07a9d | 2022-10-10 12:14:31 +0100 | [diff] [blame] | 251 | Opt_windows_names, |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 252 | Opt_showmeta, |
| 253 | Opt_acl, |
Kari Argillander | e274cde | 2021-09-07 18:35:55 +0300 | [diff] [blame] | 254 | Opt_iocharset, |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 255 | Opt_prealloc, |
Kari Argillander | 28a941f | 2021-09-07 18:35:56 +0300 | [diff] [blame] | 256 | Opt_noacsrules, |
Konstantin Komarov | a3a956c | 2022-09-23 12:42:18 +0300 | [diff] [blame] | 257 | Opt_nocase, |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 258 | Opt_err, |
| 259 | }; |
| 260 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 261 | static const struct fs_parameter_spec ntfs_fs_parameters[] = { |
| 262 | fsparam_u32("uid", Opt_uid), |
| 263 | fsparam_u32("gid", Opt_gid), |
| 264 | fsparam_u32oct("umask", Opt_umask), |
| 265 | fsparam_u32oct("dmask", Opt_dmask), |
| 266 | fsparam_u32oct("fmask", Opt_fmask), |
| 267 | fsparam_flag_no("sys_immutable", Opt_immutable), |
| 268 | fsparam_flag_no("discard", Opt_discard), |
| 269 | fsparam_flag_no("force", Opt_force), |
| 270 | fsparam_flag_no("sparse", Opt_sparse), |
Kari Argillander | 9d1939f | 2021-09-07 18:35:54 +0300 | [diff] [blame] | 271 | fsparam_flag_no("hidden", Opt_nohidden), |
Daniel Pinto | dc0fcc9 | 2022-10-10 12:37:13 +0100 | [diff] [blame] | 272 | fsparam_flag_no("hide_dot_files", Opt_hide_dot_files), |
Daniel Pinto | 1d07a9d | 2022-10-10 12:14:31 +0100 | [diff] [blame] | 273 | fsparam_flag_no("windows_names", Opt_windows_names), |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 274 | fsparam_flag_no("acl", Opt_acl), |
| 275 | fsparam_flag_no("showmeta", Opt_showmeta), |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 276 | fsparam_flag_no("prealloc", Opt_prealloc), |
Kari Argillander | 28a941f | 2021-09-07 18:35:56 +0300 | [diff] [blame] | 277 | fsparam_flag_no("acsrules", Opt_noacsrules), |
Konstantin Komarov | a3a956c | 2022-09-23 12:42:18 +0300 | [diff] [blame] | 278 | fsparam_flag_no("nocase", Opt_nocase), |
Kari Argillander | e274cde | 2021-09-07 18:35:55 +0300 | [diff] [blame] | 279 | fsparam_string("iocharset", Opt_iocharset), |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 280 | {} |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 281 | }; |
| 282 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 283 | /* |
| 284 | * Load nls table or if @nls is utf8 then return NULL. |
| 285 | */ |
| 286 | static struct nls_table *ntfs_load_nls(char *nls) |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 287 | { |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 288 | struct nls_table *ret; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 289 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 290 | if (!nls) |
| 291 | nls = CONFIG_NLS_DEFAULT; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 292 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 293 | if (strcmp(nls, "utf8") == 0) |
| 294 | return NULL; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 295 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 296 | if (strcmp(nls, CONFIG_NLS_DEFAULT) == 0) |
| 297 | return load_nls_default(); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 298 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 299 | ret = load_nls(nls); |
| 300 | if (ret) |
| 301 | return ret; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 302 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 303 | return ERR_PTR(-EINVAL); |
| 304 | } |
| 305 | |
| 306 | static int ntfs_fs_parse_param(struct fs_context *fc, |
| 307 | struct fs_parameter *param) |
| 308 | { |
| 309 | struct ntfs_mount_options *opts = fc->fs_private; |
| 310 | struct fs_parse_result result; |
| 311 | int opt; |
| 312 | |
| 313 | opt = fs_parse(fc, ntfs_fs_parameters, param, &result); |
| 314 | if (opt < 0) |
| 315 | return opt; |
| 316 | |
| 317 | switch (opt) { |
| 318 | case Opt_uid: |
| 319 | opts->fs_uid = make_kuid(current_user_ns(), result.uint_32); |
| 320 | if (!uid_valid(opts->fs_uid)) |
| 321 | return invalf(fc, "ntfs3: Invalid value for uid."); |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 322 | break; |
| 323 | case Opt_gid: |
| 324 | opts->fs_gid = make_kgid(current_user_ns(), result.uint_32); |
| 325 | if (!gid_valid(opts->fs_gid)) |
| 326 | return invalf(fc, "ntfs3: Invalid value for gid."); |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 327 | break; |
| 328 | case Opt_umask: |
| 329 | if (result.uint_32 & ~07777) |
| 330 | return invalf(fc, "ntfs3: Invalid value for umask."); |
| 331 | opts->fs_fmask_inv = ~result.uint_32; |
| 332 | opts->fs_dmask_inv = ~result.uint_32; |
| 333 | opts->fmask = 1; |
| 334 | opts->dmask = 1; |
| 335 | break; |
| 336 | case Opt_dmask: |
| 337 | if (result.uint_32 & ~07777) |
| 338 | return invalf(fc, "ntfs3: Invalid value for dmask."); |
| 339 | opts->fs_dmask_inv = ~result.uint_32; |
| 340 | opts->dmask = 1; |
| 341 | break; |
| 342 | case Opt_fmask: |
| 343 | if (result.uint_32 & ~07777) |
| 344 | return invalf(fc, "ntfs3: Invalid value for fmask."); |
| 345 | opts->fs_fmask_inv = ~result.uint_32; |
| 346 | opts->fmask = 1; |
| 347 | break; |
| 348 | case Opt_immutable: |
| 349 | opts->sys_immutable = result.negated ? 0 : 1; |
| 350 | break; |
| 351 | case Opt_discard: |
| 352 | opts->discard = result.negated ? 0 : 1; |
| 353 | break; |
| 354 | case Opt_force: |
| 355 | opts->force = result.negated ? 0 : 1; |
| 356 | break; |
| 357 | case Opt_sparse: |
| 358 | opts->sparse = result.negated ? 0 : 1; |
| 359 | break; |
| 360 | case Opt_nohidden: |
Kari Argillander | 9d1939f | 2021-09-07 18:35:54 +0300 | [diff] [blame] | 361 | opts->nohidden = result.negated ? 1 : 0; |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 362 | break; |
Konstantin Komarov | 098250d | 2022-09-12 18:28:51 +0300 | [diff] [blame] | 363 | case Opt_hide_dot_files: |
Daniel Pinto | 4c9ba19 | 2022-10-10 12:30:15 +0100 | [diff] [blame] | 364 | opts->hide_dot_files = result.negated ? 0 : 1; |
Konstantin Komarov | 098250d | 2022-09-12 18:28:51 +0300 | [diff] [blame] | 365 | break; |
Daniel Pinto | 1d07a9d | 2022-10-10 12:14:31 +0100 | [diff] [blame] | 366 | case Opt_windows_names: |
| 367 | opts->windows_names = result.negated ? 0 : 1; |
| 368 | break; |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 369 | case Opt_acl: |
| 370 | if (!result.negated) |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 371 | #ifdef CONFIG_NTFS3_FS_POSIX_ACL |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 372 | fc->sb_flags |= SB_POSIXACL; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 373 | #else |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 374 | return invalf(fc, "ntfs3: Support for ACL not compiled in!"); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 375 | #endif |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 376 | else |
| 377 | fc->sb_flags &= ~SB_POSIXACL; |
| 378 | break; |
| 379 | case Opt_showmeta: |
| 380 | opts->showmeta = result.negated ? 0 : 1; |
| 381 | break; |
Kari Argillander | e274cde | 2021-09-07 18:35:55 +0300 | [diff] [blame] | 382 | case Opt_iocharset: |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 383 | kfree(opts->nls_name); |
| 384 | opts->nls_name = param->string; |
| 385 | param->string = NULL; |
| 386 | break; |
| 387 | case Opt_prealloc: |
| 388 | opts->prealloc = result.negated ? 0 : 1; |
| 389 | break; |
Kari Argillander | 28a941f | 2021-09-07 18:35:56 +0300 | [diff] [blame] | 390 | case Opt_noacsrules: |
| 391 | opts->noacsrules = result.negated ? 1 : 0; |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 392 | break; |
Konstantin Komarov | a3a956c | 2022-09-23 12:42:18 +0300 | [diff] [blame] | 393 | case Opt_nocase: |
| 394 | opts->nocase = result.negated ? 1 : 0; |
| 395 | break; |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 396 | default: |
| 397 | /* Should not be here unless we forget add case. */ |
| 398 | return -EINVAL; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 399 | } |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 400 | return 0; |
| 401 | } |
| 402 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 403 | static int ntfs_fs_reconfigure(struct fs_context *fc) |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 404 | { |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 405 | struct super_block *sb = fc->root->d_sb; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 406 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 407 | struct ntfs_mount_options *new_opts = fc->fs_private; |
| 408 | int ro_rw; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 409 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 410 | ro_rw = sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 411 | if (ro_rw && (sbi->flags & NTFS_FLAGS_NEED_REPLAY)) { |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 412 | errorf(fc, "ntfs3: Couldn't remount rw because journal is not replayed. Please umount/remount instead\n"); |
| 413 | return -EINVAL; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 414 | } |
| 415 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 416 | new_opts->nls = ntfs_load_nls(new_opts->nls_name); |
| 417 | if (IS_ERR(new_opts->nls)) { |
| 418 | new_opts->nls = NULL; |
Kari Argillander | e274cde | 2021-09-07 18:35:55 +0300 | [diff] [blame] | 419 | errorf(fc, "ntfs3: Cannot load iocharset %s", new_opts->nls_name); |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 420 | return -EINVAL; |
| 421 | } |
| 422 | if (new_opts->nls != sbi->options->nls) |
Kari Argillander | e274cde | 2021-09-07 18:35:55 +0300 | [diff] [blame] | 423 | return invalf(fc, "ntfs3: Cannot use different iocharset when remounting!"); |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 424 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 425 | sync_filesystem(sb); |
| 426 | |
| 427 | if (ro_rw && (sbi->volume.flags & VOLUME_FLAG_DIRTY) && |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 428 | !new_opts->force) { |
| 429 | errorf(fc, "ntfs3: Volume is dirty and \"force\" flag is not set!"); |
| 430 | return -EINVAL; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 431 | } |
| 432 | |
Konstantin Komarov | cd39981 | 2022-05-11 19:58:36 +0300 | [diff] [blame] | 433 | swap(sbi->options, fc->fs_private); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 434 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 435 | return 0; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | static struct kmem_cache *ntfs_inode_cachep; |
| 439 | |
| 440 | static struct inode *ntfs_alloc_inode(struct super_block *sb) |
| 441 | { |
Muchun Song | fd60b28 | 2022-03-22 14:41:03 -0700 | [diff] [blame] | 442 | struct ntfs_inode *ni = alloc_inode_sb(sb, ntfs_inode_cachep, GFP_NOFS); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 443 | |
| 444 | if (!ni) |
| 445 | return NULL; |
| 446 | |
| 447 | memset(ni, 0, offsetof(struct ntfs_inode, vfs_inode)); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 448 | mutex_init(&ni->ni_lock); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 449 | return &ni->vfs_inode; |
| 450 | } |
| 451 | |
Konstantin Komarov | ae6b47b | 2022-09-12 18:54:06 +0300 | [diff] [blame] | 452 | static void ntfs_free_inode(struct inode *inode) |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 453 | { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 454 | struct ntfs_inode *ni = ntfs_i(inode); |
| 455 | |
| 456 | mutex_destroy(&ni->ni_lock); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 457 | kmem_cache_free(ntfs_inode_cachep, ni); |
| 458 | } |
| 459 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 460 | static void init_once(void *foo) |
| 461 | { |
| 462 | struct ntfs_inode *ni = foo; |
| 463 | |
| 464 | inode_init_once(&ni->vfs_inode); |
| 465 | } |
| 466 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 467 | /* |
| 468 | * put_ntfs - Noinline to reduce binary size. |
| 469 | */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 470 | static noinline void put_ntfs(struct ntfs_sb_info *sbi) |
| 471 | { |
Kari Argillander | 195c52b | 2021-08-24 21:37:07 +0300 | [diff] [blame] | 472 | kfree(sbi->new_rec); |
| 473 | kvfree(ntfs_put_shared(sbi->upcase)); |
| 474 | kfree(sbi->def_table); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 475 | |
| 476 | wnd_close(&sbi->mft.bitmap); |
| 477 | wnd_close(&sbi->used.bitmap); |
| 478 | |
| 479 | if (sbi->mft.ni) |
| 480 | iput(&sbi->mft.ni->vfs_inode); |
| 481 | |
| 482 | if (sbi->security.ni) |
| 483 | iput(&sbi->security.ni->vfs_inode); |
| 484 | |
| 485 | if (sbi->reparse.ni) |
| 486 | iput(&sbi->reparse.ni->vfs_inode); |
| 487 | |
| 488 | if (sbi->objid.ni) |
| 489 | iput(&sbi->objid.ni->vfs_inode); |
| 490 | |
| 491 | if (sbi->volume.ni) |
| 492 | iput(&sbi->volume.ni->vfs_inode); |
| 493 | |
| 494 | ntfs_update_mftmirr(sbi, 0); |
| 495 | |
| 496 | indx_clear(&sbi->security.index_sii); |
| 497 | indx_clear(&sbi->security.index_sdh); |
| 498 | indx_clear(&sbi->reparse.index_r); |
| 499 | indx_clear(&sbi->objid.index_o); |
Kari Argillander | 195c52b | 2021-08-24 21:37:07 +0300 | [diff] [blame] | 500 | kfree(sbi->compress.lznt); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 501 | #ifdef CONFIG_NTFS3_LZX_XPRESS |
| 502 | xpress_free_decompressor(sbi->compress.xpress); |
| 503 | lzx_free_decompressor(sbi->compress.lzx); |
| 504 | #endif |
Kari Argillander | 195c52b | 2021-08-24 21:37:07 +0300 | [diff] [blame] | 505 | kfree(sbi); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static void ntfs_put_super(struct super_block *sb) |
| 509 | { |
| 510 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 511 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 512 | /* Mark rw ntfs as clear, if possible. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 513 | ntfs_set_state(sbi, NTFS_DIRTY_CLEAR); |
| 514 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 515 | put_mount_options(sbi->options); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 516 | put_ntfs(sbi); |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 517 | sb->s_fs_info = NULL; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 518 | |
| 519 | sync_blockdev(sb->s_bdev); |
| 520 | } |
| 521 | |
| 522 | static int ntfs_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 523 | { |
| 524 | struct super_block *sb = dentry->d_sb; |
| 525 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 526 | struct wnd_bitmap *wnd = &sbi->used.bitmap; |
| 527 | |
| 528 | buf->f_type = sb->s_magic; |
| 529 | buf->f_bsize = sbi->cluster_size; |
| 530 | buf->f_blocks = wnd->nbits; |
| 531 | |
| 532 | buf->f_bfree = buf->f_bavail = wnd_zeroes(wnd); |
| 533 | buf->f_fsid.val[0] = sbi->volume.ser_num; |
| 534 | buf->f_fsid.val[1] = (sbi->volume.ser_num >> 32); |
| 535 | buf->f_namelen = NTFS_NAME_LEN; |
| 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | static int ntfs_show_options(struct seq_file *m, struct dentry *root) |
| 541 | { |
| 542 | struct super_block *sb = root->d_sb; |
| 543 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
Kari Argillander | 564c97b | 2021-09-07 18:35:51 +0300 | [diff] [blame] | 544 | struct ntfs_mount_options *opts = sbi->options; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 545 | struct user_namespace *user_ns = seq_user_ns(m); |
| 546 | |
Kari Argillander | 15b2ae7 | 2021-09-07 18:35:57 +0300 | [diff] [blame] | 547 | seq_printf(m, ",uid=%u", |
| 548 | from_kuid_munged(user_ns, opts->fs_uid)); |
| 549 | seq_printf(m, ",gid=%u", |
| 550 | from_kgid_munged(user_ns, opts->fs_gid)); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 551 | if (opts->fmask) |
Marc Aurèle La France | f27b92e | 2022-08-10 14:28:04 -0600 | [diff] [blame] | 552 | seq_printf(m, ",fmask=%04o", opts->fs_fmask_inv ^ 0xffff); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 553 | if (opts->dmask) |
Marc Aurèle La France | f27b92e | 2022-08-10 14:28:04 -0600 | [diff] [blame] | 554 | seq_printf(m, ",dmask=%04o", opts->fs_dmask_inv ^ 0xffff); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 555 | if (opts->nls) |
Kari Argillander | e274cde | 2021-09-07 18:35:55 +0300 | [diff] [blame] | 556 | seq_printf(m, ",iocharset=%s", opts->nls->charset); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 557 | else |
Kari Argillander | e274cde | 2021-09-07 18:35:55 +0300 | [diff] [blame] | 558 | seq_puts(m, ",iocharset=utf8"); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 559 | if (opts->sys_immutable) |
| 560 | seq_puts(m, ",sys_immutable"); |
| 561 | if (opts->discard) |
| 562 | seq_puts(m, ",discard"); |
| 563 | if (opts->sparse) |
| 564 | seq_puts(m, ",sparse"); |
| 565 | if (opts->showmeta) |
| 566 | seq_puts(m, ",showmeta"); |
| 567 | if (opts->nohidden) |
| 568 | seq_puts(m, ",nohidden"); |
Daniel Pinto | 1d07a9d | 2022-10-10 12:14:31 +0100 | [diff] [blame] | 569 | if (opts->windows_names) |
| 570 | seq_puts(m, ",windows_names"); |
Daniel Pinto | 19b4245 | 2022-10-10 12:34:06 +0100 | [diff] [blame] | 571 | if (opts->hide_dot_files) |
Daniel Pinto | dc0fcc9 | 2022-10-10 12:37:13 +0100 | [diff] [blame] | 572 | seq_puts(m, ",hide_dot_files"); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 573 | if (opts->force) |
| 574 | seq_puts(m, ",force"); |
Kari Argillander | 28a941f | 2021-09-07 18:35:56 +0300 | [diff] [blame] | 575 | if (opts->noacsrules) |
| 576 | seq_puts(m, ",noacsrules"); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 577 | if (opts->prealloc) |
| 578 | seq_puts(m, ",prealloc"); |
| 579 | if (sb->s_flags & SB_POSIXACL) |
| 580 | seq_puts(m, ",acl"); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 585 | /* |
| 586 | * ntfs_sync_fs - super_operations::sync_fs |
| 587 | */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 588 | static int ntfs_sync_fs(struct super_block *sb, int wait) |
| 589 | { |
| 590 | int err = 0, err2; |
| 591 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 592 | struct ntfs_inode *ni; |
| 593 | struct inode *inode; |
| 594 | |
| 595 | ni = sbi->security.ni; |
| 596 | if (ni) { |
| 597 | inode = &ni->vfs_inode; |
| 598 | err2 = _ni_write_inode(inode, wait); |
| 599 | if (err2 && !err) |
| 600 | err = err2; |
| 601 | } |
| 602 | |
| 603 | ni = sbi->objid.ni; |
| 604 | if (ni) { |
| 605 | inode = &ni->vfs_inode; |
| 606 | err2 = _ni_write_inode(inode, wait); |
| 607 | if (err2 && !err) |
| 608 | err = err2; |
| 609 | } |
| 610 | |
| 611 | ni = sbi->reparse.ni; |
| 612 | if (ni) { |
| 613 | inode = &ni->vfs_inode; |
| 614 | err2 = _ni_write_inode(inode, wait); |
| 615 | if (err2 && !err) |
| 616 | err = err2; |
| 617 | } |
| 618 | |
| 619 | if (!err) |
| 620 | ntfs_set_state(sbi, NTFS_DIRTY_CLEAR); |
| 621 | |
| 622 | ntfs_update_mftmirr(sbi, wait); |
| 623 | |
| 624 | return err; |
| 625 | } |
| 626 | |
| 627 | static const struct super_operations ntfs_sops = { |
| 628 | .alloc_inode = ntfs_alloc_inode, |
Konstantin Komarov | ae6b47b | 2022-09-12 18:54:06 +0300 | [diff] [blame] | 629 | .free_inode = ntfs_free_inode, |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 630 | .evict_inode = ntfs_evict_inode, |
| 631 | .put_super = ntfs_put_super, |
| 632 | .statfs = ntfs_statfs, |
| 633 | .show_options = ntfs_show_options, |
| 634 | .sync_fs = ntfs_sync_fs, |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 635 | .write_inode = ntfs3_write_inode, |
| 636 | }; |
| 637 | |
| 638 | static struct inode *ntfs_export_get_inode(struct super_block *sb, u64 ino, |
| 639 | u32 generation) |
| 640 | { |
| 641 | struct MFT_REF ref; |
| 642 | struct inode *inode; |
| 643 | |
| 644 | ref.low = cpu_to_le32(ino); |
| 645 | #ifdef CONFIG_NTFS3_64BIT_CLUSTER |
| 646 | ref.high = cpu_to_le16(ino >> 32); |
| 647 | #else |
| 648 | ref.high = 0; |
| 649 | #endif |
| 650 | ref.seq = cpu_to_le16(generation); |
| 651 | |
| 652 | inode = ntfs_iget5(sb, &ref, NULL); |
| 653 | if (!IS_ERR(inode) && is_bad_inode(inode)) { |
| 654 | iput(inode); |
| 655 | inode = ERR_PTR(-ESTALE); |
| 656 | } |
| 657 | |
| 658 | return inode; |
| 659 | } |
| 660 | |
| 661 | static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid, |
| 662 | int fh_len, int fh_type) |
| 663 | { |
| 664 | return generic_fh_to_dentry(sb, fid, fh_len, fh_type, |
| 665 | ntfs_export_get_inode); |
| 666 | } |
| 667 | |
| 668 | static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid, |
| 669 | int fh_len, int fh_type) |
| 670 | { |
| 671 | return generic_fh_to_parent(sb, fid, fh_len, fh_type, |
| 672 | ntfs_export_get_inode); |
| 673 | } |
| 674 | |
| 675 | /* TODO: == ntfs_sync_inode */ |
| 676 | static int ntfs_nfs_commit_metadata(struct inode *inode) |
| 677 | { |
| 678 | return _ni_write_inode(inode, 1); |
| 679 | } |
| 680 | |
| 681 | static const struct export_operations ntfs_export_ops = { |
| 682 | .fh_to_dentry = ntfs_fh_to_dentry, |
| 683 | .fh_to_parent = ntfs_fh_to_parent, |
| 684 | .get_parent = ntfs3_get_parent, |
| 685 | .commit_metadata = ntfs_nfs_commit_metadata, |
| 686 | }; |
| 687 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 688 | /* |
| 689 | * format_size_gb - Return Gb,Mb to print with "%u.%02u Gb". |
| 690 | */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 691 | static u32 format_size_gb(const u64 bytes, u32 *mb) |
| 692 | { |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 693 | /* Do simple right 30 bit shift of 64 bit value. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 694 | u64 kbytes = bytes >> 10; |
| 695 | u32 kbytes32 = kbytes; |
| 696 | |
| 697 | *mb = (100 * (kbytes32 & 0xfffff) + 0x7ffff) >> 20; |
| 698 | if (*mb >= 100) |
| 699 | *mb = 99; |
| 700 | |
| 701 | return (kbytes32 >> 20) | (((u32)(kbytes >> 32)) << 12); |
| 702 | } |
| 703 | |
| 704 | static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot) |
| 705 | { |
Randy Dunlap | a3b7743 | 2022-05-12 20:38:37 -0700 | [diff] [blame] | 706 | if (boot->sectors_per_clusters <= 0x80) |
| 707 | return boot->sectors_per_clusters; |
| 708 | if (boot->sectors_per_clusters >= 0xf4) /* limit shift to 2MB max */ |
Shigeru Yoshida | caad9dd | 2022-08-23 23:46:25 +0900 | [diff] [blame] | 709 | return 1U << -(s8)boot->sectors_per_clusters; |
Randy Dunlap | a3b7743 | 2022-05-12 20:38:37 -0700 | [diff] [blame] | 710 | return -EINVAL; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 711 | } |
| 712 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 713 | /* |
| 714 | * ntfs_init_from_boot - Init internal info from on-disk boot sector. |
| 715 | */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 716 | static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size, |
| 717 | u64 dev_size) |
| 718 | { |
| 719 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 720 | int err; |
| 721 | u32 mb, gb, boot_sector_size, sct_per_clst, record_size; |
Konstantin Komarov | dbf59e2 | 2021-09-28 19:19:49 +0300 | [diff] [blame] | 722 | u64 sectors, clusters, mlcn, mlcn2; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 723 | struct NTFS_BOOT *boot; |
| 724 | struct buffer_head *bh; |
| 725 | struct MFT_REC *rec; |
| 726 | u16 fn, ao; |
| 727 | |
| 728 | sbi->volume.blocks = dev_size >> PAGE_SHIFT; |
| 729 | |
| 730 | bh = ntfs_bread(sb, 0); |
| 731 | if (!bh) |
| 732 | return -EIO; |
| 733 | |
| 734 | err = -EINVAL; |
| 735 | boot = (struct NTFS_BOOT *)bh->b_data; |
| 736 | |
| 737 | if (memcmp(boot->system_id, "NTFS ", sizeof("NTFS ") - 1)) |
| 738 | goto out; |
| 739 | |
| 740 | /* 0x55AA is not mandaroty. Thanks Maxim Suhanov*/ |
| 741 | /*if (0x55 != boot->boot_magic[0] || 0xAA != boot->boot_magic[1]) |
| 742 | * goto out; |
| 743 | */ |
| 744 | |
| 745 | boot_sector_size = (u32)boot->bytes_per_sector[1] << 8; |
| 746 | if (boot->bytes_per_sector[0] || boot_sector_size < SECTOR_SIZE || |
Kari Argillander | 528c9b3 | 2021-08-16 13:37:32 +0300 | [diff] [blame] | 747 | !is_power_of_2(boot_sector_size)) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 748 | goto out; |
| 749 | } |
| 750 | |
| 751 | /* cluster size: 512, 1K, 2K, 4K, ... 2M */ |
| 752 | sct_per_clst = true_sectors_per_clst(boot); |
Randy Dunlap | a3b7743 | 2022-05-12 20:38:37 -0700 | [diff] [blame] | 753 | if ((int)sct_per_clst < 0) |
| 754 | goto out; |
Kari Argillander | 528c9b3 | 2021-08-16 13:37:32 +0300 | [diff] [blame] | 755 | if (!is_power_of_2(sct_per_clst)) |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 756 | goto out; |
| 757 | |
| 758 | mlcn = le64_to_cpu(boot->mft_clst); |
| 759 | mlcn2 = le64_to_cpu(boot->mft2_clst); |
| 760 | sectors = le64_to_cpu(boot->sectors_per_volume); |
| 761 | |
| 762 | if (mlcn * sct_per_clst >= sectors) |
| 763 | goto out; |
| 764 | |
| 765 | if (mlcn2 * sct_per_clst >= sectors) |
| 766 | goto out; |
| 767 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 768 | /* Check MFT record size. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 769 | if ((boot->record_size < 0 && |
| 770 | SECTOR_SIZE > (2U << (-boot->record_size))) || |
Kari Argillander | 528c9b3 | 2021-08-16 13:37:32 +0300 | [diff] [blame] | 771 | (boot->record_size >= 0 && !is_power_of_2(boot->record_size))) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 772 | goto out; |
| 773 | } |
| 774 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 775 | /* Check index record size. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 776 | if ((boot->index_size < 0 && |
| 777 | SECTOR_SIZE > (2U << (-boot->index_size))) || |
Kari Argillander | 528c9b3 | 2021-08-16 13:37:32 +0300 | [diff] [blame] | 778 | (boot->index_size >= 0 && !is_power_of_2(boot->index_size))) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 779 | goto out; |
| 780 | } |
| 781 | |
Konstantin Komarov | dbf59e2 | 2021-09-28 19:19:49 +0300 | [diff] [blame] | 782 | sbi->volume.size = sectors * boot_sector_size; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 783 | |
Konstantin Komarov | dbf59e2 | 2021-09-28 19:19:49 +0300 | [diff] [blame] | 784 | gb = format_size_gb(sbi->volume.size + boot_sector_size, &mb); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 785 | |
| 786 | /* |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 787 | * - Volume formatted and mounted with the same sector size. |
| 788 | * - Volume formatted 4K and mounted as 512. |
| 789 | * - Volume formatted 512 and mounted as 4K. |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 790 | */ |
Konstantin Komarov | dbf59e2 | 2021-09-28 19:19:49 +0300 | [diff] [blame] | 791 | if (boot_sector_size != sector_size) { |
| 792 | ntfs_warn( |
| 793 | sb, |
| 794 | "Different NTFS' sector size (%u) and media sector size (%u)", |
| 795 | boot_sector_size, sector_size); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 796 | dev_size += sector_size - 1; |
| 797 | } |
| 798 | |
| 799 | sbi->cluster_size = boot_sector_size * sct_per_clst; |
| 800 | sbi->cluster_bits = blksize_bits(sbi->cluster_size); |
| 801 | |
| 802 | sbi->mft.lbo = mlcn << sbi->cluster_bits; |
| 803 | sbi->mft.lbo2 = mlcn2 << sbi->cluster_bits; |
| 804 | |
Konstantin Komarov | 09f7c33 | 2021-09-28 19:19:17 +0300 | [diff] [blame] | 805 | /* Compare boot's cluster and sector. */ |
| 806 | if (sbi->cluster_size < boot_sector_size) |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 807 | goto out; |
| 808 | |
Konstantin Komarov | 09f7c33 | 2021-09-28 19:19:17 +0300 | [diff] [blame] | 809 | /* Compare boot's cluster and media sector. */ |
| 810 | if (sbi->cluster_size < sector_size) { |
| 811 | /* No way to use ntfs_get_block in this case. */ |
| 812 | ntfs_err( |
| 813 | sb, |
| 814 | "Failed to mount 'cause NTFS's cluster size (%u) is less than media sector size (%u)", |
| 815 | sbi->cluster_size, sector_size); |
| 816 | goto out; |
| 817 | } |
| 818 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 819 | sbi->cluster_mask = sbi->cluster_size - 1; |
| 820 | sbi->cluster_mask_inv = ~(u64)sbi->cluster_mask; |
| 821 | sbi->record_size = record_size = boot->record_size < 0 |
| 822 | ? 1 << (-boot->record_size) |
| 823 | : (u32)boot->record_size |
| 824 | << sbi->cluster_bits; |
| 825 | |
edward lo | 0b66046 | 2022-08-01 15:37:31 +0800 | [diff] [blame] | 826 | if (record_size > MAXIMUM_BYTES_PER_MFT || record_size < SECTOR_SIZE) |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 827 | goto out; |
| 828 | |
| 829 | sbi->record_bits = blksize_bits(record_size); |
| 830 | sbi->attr_size_tr = (5 * record_size >> 4); // ~320 bytes |
| 831 | |
| 832 | sbi->max_bytes_per_attr = |
Kari Argillander | fa3cacf | 2021-08-26 11:56:29 +0300 | [diff] [blame] | 833 | record_size - ALIGN(MFTRECORD_FIXUP_OFFSET_1, 8) - |
| 834 | ALIGN(((record_size >> SECTOR_SHIFT) * sizeof(short)), 8) - |
| 835 | ALIGN(sizeof(enum ATTR_TYPE), 8); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 836 | |
| 837 | sbi->index_size = boot->index_size < 0 |
| 838 | ? 1u << (-boot->index_size) |
| 839 | : (u32)boot->index_size << sbi->cluster_bits; |
| 840 | |
| 841 | sbi->volume.ser_num = le64_to_cpu(boot->serial_num); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 842 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 843 | /* Warning if RAW volume. */ |
Konstantin Komarov | dbf59e2 | 2021-09-28 19:19:49 +0300 | [diff] [blame] | 844 | if (dev_size < sbi->volume.size + boot_sector_size) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 845 | u32 mb0, gb0; |
| 846 | |
| 847 | gb0 = format_size_gb(dev_size, &mb0); |
| 848 | ntfs_warn( |
| 849 | sb, |
| 850 | "RAW NTFS volume: Filesystem size %u.%02u Gb > volume size %u.%02u Gb. Mount in read-only", |
| 851 | gb, mb, gb0, mb0); |
| 852 | sb->s_flags |= SB_RDONLY; |
| 853 | } |
| 854 | |
| 855 | clusters = sbi->volume.size >> sbi->cluster_bits; |
| 856 | #ifndef CONFIG_NTFS3_64BIT_CLUSTER |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 857 | /* 32 bits per cluster. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 858 | if (clusters >> 32) { |
| 859 | ntfs_notice( |
| 860 | sb, |
| 861 | "NTFS %u.%02u Gb is too big to use 32 bits per cluster", |
| 862 | gb, mb); |
| 863 | goto out; |
| 864 | } |
| 865 | #elif BITS_PER_LONG < 64 |
| 866 | #error "CONFIG_NTFS3_64BIT_CLUSTER incompatible in 32 bit OS" |
| 867 | #endif |
| 868 | |
| 869 | sbi->used.bitmap.nbits = clusters; |
| 870 | |
Kari Argillander | 195c52b | 2021-08-24 21:37:07 +0300 | [diff] [blame] | 871 | rec = kzalloc(record_size, GFP_NOFS); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 872 | if (!rec) { |
| 873 | err = -ENOMEM; |
| 874 | goto out; |
| 875 | } |
| 876 | |
| 877 | sbi->new_rec = rec; |
| 878 | rec->rhdr.sign = NTFS_FILE_SIGNATURE; |
| 879 | rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET_1); |
| 880 | fn = (sbi->record_size >> SECTOR_SHIFT) + 1; |
| 881 | rec->rhdr.fix_num = cpu_to_le16(fn); |
Kari Argillander | fa3cacf | 2021-08-26 11:56:29 +0300 | [diff] [blame] | 882 | ao = ALIGN(MFTRECORD_FIXUP_OFFSET_1 + sizeof(short) * fn, 8); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 883 | rec->attr_off = cpu_to_le16(ao); |
Kari Argillander | fa3cacf | 2021-08-26 11:56:29 +0300 | [diff] [blame] | 884 | rec->used = cpu_to_le32(ao + ALIGN(sizeof(enum ATTR_TYPE), 8)); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 885 | rec->total = cpu_to_le32(sbi->record_size); |
| 886 | ((struct ATTRIB *)Add2Ptr(rec, ao))->type = ATTR_END; |
| 887 | |
Kari Argillander | 28861e3 | 2021-09-09 21:09:42 +0300 | [diff] [blame] | 888 | sb_set_blocksize(sb, min_t(u32, sbi->cluster_size, PAGE_SIZE)); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 889 | |
| 890 | sbi->block_mask = sb->s_blocksize - 1; |
| 891 | sbi->blocks_per_cluster = sbi->cluster_size >> sb->s_blocksize_bits; |
| 892 | sbi->volume.blocks = sbi->volume.size >> sb->s_blocksize_bits; |
| 893 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 894 | /* Maximum size for normal files. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 895 | sbi->maxbytes = (clusters << sbi->cluster_bits) - 1; |
| 896 | |
| 897 | #ifdef CONFIG_NTFS3_64BIT_CLUSTER |
| 898 | if (clusters >= (1ull << (64 - sbi->cluster_bits))) |
| 899 | sbi->maxbytes = -1; |
| 900 | sbi->maxbytes_sparse = -1; |
Kari Argillander | 28861e3 | 2021-09-09 21:09:42 +0300 | [diff] [blame] | 901 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 902 | #else |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 903 | /* Maximum size for sparse file. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 904 | sbi->maxbytes_sparse = (1ull << (sbi->cluster_bits + 32)) - 1; |
Kari Argillander | 28861e3 | 2021-09-09 21:09:42 +0300 | [diff] [blame] | 905 | sb->s_maxbytes = 0xFFFFFFFFull << sbi->cluster_bits; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 906 | #endif |
| 907 | |
Konstantin Komarov | 8335ebe | 2022-07-07 19:25:43 +0300 | [diff] [blame] | 908 | /* |
| 909 | * Compute the MFT zone at two steps. |
| 910 | * It would be nice if we are able to allocate 1/8 of |
| 911 | * total clusters for MFT but not more then 512 MB. |
| 912 | */ |
| 913 | sbi->zone_max = min_t(CLST, 0x20000000 >> sbi->cluster_bits, clusters >> 3); |
| 914 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 915 | err = 0; |
| 916 | |
| 917 | out: |
| 918 | brelse(bh); |
| 919 | |
| 920 | return err; |
| 921 | } |
| 922 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 923 | /* |
| 924 | * ntfs_fill_super - Try to mount. |
| 925 | */ |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 926 | static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc) |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 927 | { |
| 928 | int err; |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 929 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 930 | struct block_device *bdev = sb->s_bdev; |
Kari Argillander | 10b4f12 | 2021-09-09 21:09:36 +0300 | [diff] [blame] | 931 | struct inode *inode; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 932 | struct ntfs_inode *ni; |
Konstantin Komarov | ec5fc72 | 2022-10-11 20:19:36 +0300 | [diff] [blame] | 933 | size_t i, tt, bad_len, bad_frags; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 934 | CLST vcn, lcn, len; |
| 935 | struct ATTRIB *attr; |
| 936 | const struct VOLUME_INFO *info; |
| 937 | u32 idx, done, bytes; |
| 938 | struct ATTR_DEF_ENTRY *t; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 939 | u16 *shared; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 940 | struct MFT_REF ref; |
| 941 | |
| 942 | ref.high = 0; |
| 943 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 944 | sbi->sb = sb; |
Konstantin Komarov | cd39981 | 2022-05-11 19:58:36 +0300 | [diff] [blame] | 945 | sbi->options = fc->fs_private; |
| 946 | fc->fs_private = NULL; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 947 | sb->s_flags |= SB_NODIRATIME; |
| 948 | sb->s_magic = 0x7366746e; // "ntfs" |
| 949 | sb->s_op = &ntfs_sops; |
| 950 | sb->s_export_op = &ntfs_export_ops; |
| 951 | sb->s_time_gran = NTFS_TIME_GRAN; // 100 nsec |
| 952 | sb->s_xattr = ntfs_xattr_handlers; |
Konstantin Komarov | a3a956c | 2022-09-23 12:42:18 +0300 | [diff] [blame] | 953 | sb->s_d_op = sbi->options->nocase ? &ntfs_dentry_ops : NULL; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 954 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 955 | sbi->options->nls = ntfs_load_nls(sbi->options->nls_name); |
| 956 | if (IS_ERR(sbi->options->nls)) { |
| 957 | sbi->options->nls = NULL; |
| 958 | errorf(fc, "Cannot load nls %s", sbi->options->nls_name); |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 959 | err = -EINVAL; |
| 960 | goto out; |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 961 | } |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 962 | |
Christoph Hellwig | 7b47ef5 | 2022-04-15 06:52:56 +0200 | [diff] [blame] | 963 | if (bdev_max_discard_sectors(bdev) && bdev_discard_granularity(bdev)) { |
| 964 | sbi->discard_granularity = bdev_discard_granularity(bdev); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 965 | sbi->discard_granularity_mask_inv = |
| 966 | ~(u64)(sbi->discard_granularity - 1); |
| 967 | } |
| 968 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 969 | /* Parse boot. */ |
Christoph Hellwig | f09dac9 | 2022-04-15 06:52:40 +0200 | [diff] [blame] | 970 | err = ntfs_init_from_boot(sb, bdev_logical_block_size(bdev), |
Christoph Hellwig | d54f13a | 2021-10-18 11:37:15 -0600 | [diff] [blame] | 971 | bdev_nr_bytes(bdev)); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 972 | if (err) |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 973 | goto out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 974 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 975 | /* |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 976 | * Load $Volume. This should be done before $LogFile |
| 977 | * 'cause 'sbi->volume.ni' is used 'ntfs_set_state'. |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 978 | */ |
| 979 | ref.low = cpu_to_le32(MFT_REC_VOL); |
| 980 | ref.seq = cpu_to_le16(MFT_REC_VOL); |
| 981 | inode = ntfs_iget5(sb, &ref, &NAME_VOLUME); |
| 982 | if (IS_ERR(inode)) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 983 | ntfs_err(sb, "Failed to load $Volume."); |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 984 | err = PTR_ERR(inode); |
| 985 | goto out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | ni = ntfs_i(inode); |
| 989 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 990 | /* Load and save label (not necessary). */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 991 | attr = ni_find_attr(ni, NULL, NULL, ATTR_LABEL, NULL, 0, NULL, NULL); |
| 992 | |
| 993 | if (!attr) { |
| 994 | /* It is ok if no ATTR_LABEL */ |
| 995 | } else if (!attr->non_res && !is_attr_ext(attr)) { |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 996 | /* $AttrDef allows labels to be up to 128 symbols. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 997 | err = utf16s_to_utf8s(resident_data(attr), |
| 998 | le32_to_cpu(attr->res.data_size) >> 1, |
| 999 | UTF16_LITTLE_ENDIAN, sbi->volume.label, |
| 1000 | sizeof(sbi->volume.label)); |
| 1001 | if (err < 0) |
| 1002 | sbi->volume.label[0] = 0; |
| 1003 | } else { |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1004 | /* Should we break mounting here? */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1005 | //err = -EINVAL; |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1006 | //goto put_inode_out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | attr = ni_find_attr(ni, attr, NULL, ATTR_VOL_INFO, NULL, 0, NULL, NULL); |
| 1010 | if (!attr || is_attr_ext(attr)) { |
| 1011 | err = -EINVAL; |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1012 | goto put_inode_out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO); |
| 1016 | if (!info) { |
| 1017 | err = -EINVAL; |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1018 | goto put_inode_out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | sbi->volume.major_ver = info->major_ver; |
| 1022 | sbi->volume.minor_ver = info->minor_ver; |
| 1023 | sbi->volume.flags = info->flags; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1024 | sbi->volume.ni = ni; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1025 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1026 | /* Load $MFTMirr to estimate recs_mirr. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1027 | ref.low = cpu_to_le32(MFT_REC_MIRR); |
| 1028 | ref.seq = cpu_to_le16(MFT_REC_MIRR); |
| 1029 | inode = ntfs_iget5(sb, &ref, &NAME_MIRROR); |
| 1030 | if (IS_ERR(inode)) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1031 | ntfs_err(sb, "Failed to load $MFTMirr."); |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1032 | err = PTR_ERR(inode); |
| 1033 | goto out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | sbi->mft.recs_mirr = |
| 1037 | ntfs_up_cluster(sbi, inode->i_size) >> sbi->record_bits; |
| 1038 | |
| 1039 | iput(inode); |
| 1040 | |
Konstantin Komarov | d362446 | 2021-08-31 16:57:40 +0300 | [diff] [blame] | 1041 | /* Load LogFile to replay. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1042 | ref.low = cpu_to_le32(MFT_REC_LOG); |
| 1043 | ref.seq = cpu_to_le16(MFT_REC_LOG); |
| 1044 | inode = ntfs_iget5(sb, &ref, &NAME_LOGFILE); |
| 1045 | if (IS_ERR(inode)) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1046 | ntfs_err(sb, "Failed to load \x24LogFile."); |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1047 | err = PTR_ERR(inode); |
| 1048 | goto out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | ni = ntfs_i(inode); |
| 1052 | |
| 1053 | err = ntfs_loadlog_and_replay(ni, sbi); |
| 1054 | if (err) |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1055 | goto put_inode_out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1056 | |
| 1057 | iput(inode); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1058 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1059 | if (sbi->flags & NTFS_FLAGS_NEED_REPLAY) { |
Kari Argillander | 0cde7e81 | 2021-09-09 21:09:38 +0300 | [diff] [blame] | 1060 | if (!sb_rdonly(sb)) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1061 | ntfs_warn(sb, |
| 1062 | "failed to replay log file. Can't mount rw!"); |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1063 | err = -EINVAL; |
| 1064 | goto out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1065 | } |
| 1066 | } else if (sbi->volume.flags & VOLUME_FLAG_DIRTY) { |
Kari Argillander | 0cde7e81 | 2021-09-09 21:09:38 +0300 | [diff] [blame] | 1067 | if (!sb_rdonly(sb) && !sbi->options->force) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1068 | ntfs_warn( |
| 1069 | sb, |
| 1070 | "volume is dirty and \"force\" flag is not set!"); |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1071 | err = -EINVAL; |
| 1072 | goto out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1076 | /* Load $MFT. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1077 | ref.low = cpu_to_le32(MFT_REC_MFT); |
| 1078 | ref.seq = cpu_to_le16(1); |
| 1079 | |
| 1080 | inode = ntfs_iget5(sb, &ref, &NAME_MFT); |
| 1081 | if (IS_ERR(inode)) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1082 | ntfs_err(sb, "Failed to load $MFT."); |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1083 | err = PTR_ERR(inode); |
| 1084 | goto out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | ni = ntfs_i(inode); |
| 1088 | |
| 1089 | sbi->mft.used = ni->i_valid >> sbi->record_bits; |
| 1090 | tt = inode->i_size >> sbi->record_bits; |
| 1091 | sbi->mft.next_free = MFT_REC_USER; |
| 1092 | |
| 1093 | err = wnd_init(&sbi->mft.bitmap, sb, tt); |
| 1094 | if (err) |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1095 | goto put_inode_out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1096 | |
| 1097 | err = ni_load_all_mi(ni); |
| 1098 | if (err) |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1099 | goto put_inode_out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1100 | |
| 1101 | sbi->mft.ni = ni; |
| 1102 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1103 | /* Load $Bitmap. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1104 | ref.low = cpu_to_le32(MFT_REC_BITMAP); |
| 1105 | ref.seq = cpu_to_le16(MFT_REC_BITMAP); |
| 1106 | inode = ntfs_iget5(sb, &ref, &NAME_BITMAP); |
| 1107 | if (IS_ERR(inode)) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1108 | ntfs_err(sb, "Failed to load $Bitmap."); |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1109 | err = PTR_ERR(inode); |
| 1110 | goto out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1111 | } |
| 1112 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1113 | #ifndef CONFIG_NTFS3_64BIT_CLUSTER |
| 1114 | if (inode->i_size >> 32) { |
| 1115 | err = -EINVAL; |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1116 | goto put_inode_out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1117 | } |
| 1118 | #endif |
| 1119 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1120 | /* Check bitmap boundary. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1121 | tt = sbi->used.bitmap.nbits; |
| 1122 | if (inode->i_size < bitmap_size(tt)) { |
| 1123 | err = -EINVAL; |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1124 | goto put_inode_out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1125 | } |
| 1126 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1127 | /* Not necessary. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1128 | sbi->used.bitmap.set_tail = true; |
Kari Argillander | b4f110d | 2021-09-09 21:09:37 +0300 | [diff] [blame] | 1129 | err = wnd_init(&sbi->used.bitmap, sb, tt); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1130 | if (err) |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1131 | goto put_inode_out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1132 | |
| 1133 | iput(inode); |
| 1134 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1135 | /* Compute the MFT zone. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1136 | err = ntfs_refresh_zone(sbi); |
| 1137 | if (err) |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1138 | goto out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1139 | |
Konstantin Komarov | ec5fc72 | 2022-10-11 20:19:36 +0300 | [diff] [blame] | 1140 | /* Load $BadClus. */ |
| 1141 | ref.low = cpu_to_le32(MFT_REC_BADCLUST); |
| 1142 | ref.seq = cpu_to_le16(MFT_REC_BADCLUST); |
| 1143 | inode = ntfs_iget5(sb, &ref, &NAME_BADCLUS); |
| 1144 | if (IS_ERR(inode)) { |
| 1145 | err = PTR_ERR(inode); |
| 1146 | ntfs_err(sb, "Failed to load $BadClus (%d).", err); |
| 1147 | goto out; |
| 1148 | } |
| 1149 | |
| 1150 | ni = ntfs_i(inode); |
| 1151 | bad_len = bad_frags = 0; |
| 1152 | for (i = 0; run_get_entry(&ni->file.run, i, &vcn, &lcn, &len); i++) { |
| 1153 | if (lcn == SPARSE_LCN) |
| 1154 | continue; |
| 1155 | |
| 1156 | bad_len += len; |
| 1157 | bad_frags += 1; |
| 1158 | if (sb_rdonly(sb)) |
| 1159 | continue; |
| 1160 | |
| 1161 | if (wnd_set_used_safe(&sbi->used.bitmap, lcn, len, &tt) || tt) { |
| 1162 | /* Bad blocks marked as free in bitmap. */ |
| 1163 | ntfs_set_state(sbi, NTFS_DIRTY_ERROR); |
| 1164 | } |
| 1165 | } |
| 1166 | if (bad_len) { |
| 1167 | /* |
| 1168 | * Notice about bad blocks. |
| 1169 | * In normal cases these blocks are marked as used in bitmap. |
| 1170 | * And we never allocate space in it. |
| 1171 | */ |
| 1172 | ntfs_notice(sb, |
| 1173 | "Volume contains %zu bad blocks in %zu fragments.", |
| 1174 | bad_len, bad_frags); |
| 1175 | } |
| 1176 | iput(inode); |
| 1177 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1178 | /* Load $AttrDef. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1179 | ref.low = cpu_to_le32(MFT_REC_ATTR); |
| 1180 | ref.seq = cpu_to_le16(MFT_REC_ATTR); |
Kari Argillander | b4f110d | 2021-09-09 21:09:37 +0300 | [diff] [blame] | 1181 | inode = ntfs_iget5(sb, &ref, &NAME_ATTRDEF); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1182 | if (IS_ERR(inode)) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1183 | ntfs_err(sb, "Failed to load $AttrDef -> %d", err); |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1184 | err = PTR_ERR(inode); |
| 1185 | goto out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | if (inode->i_size < sizeof(struct ATTR_DEF_ENTRY)) { |
| 1189 | err = -EINVAL; |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1190 | goto put_inode_out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1191 | } |
| 1192 | bytes = inode->i_size; |
Tetsuo Handa | 59bfd7a | 2022-10-02 23:54:11 +0900 | [diff] [blame] | 1193 | sbi->def_table = t = kmalloc(bytes, GFP_NOFS | __GFP_NOWARN); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1194 | if (!t) { |
| 1195 | err = -ENOMEM; |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1196 | goto put_inode_out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | for (done = idx = 0; done < bytes; done += PAGE_SIZE, idx++) { |
| 1200 | unsigned long tail = bytes - done; |
| 1201 | struct page *page = ntfs_map_page(inode->i_mapping, idx); |
| 1202 | |
| 1203 | if (IS_ERR(page)) { |
| 1204 | err = PTR_ERR(page); |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1205 | goto put_inode_out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1206 | } |
| 1207 | memcpy(Add2Ptr(t, done), page_address(page), |
| 1208 | min(PAGE_SIZE, tail)); |
| 1209 | ntfs_unmap_page(page); |
| 1210 | |
| 1211 | if (!idx && ATTR_STD != t->type) { |
| 1212 | err = -EINVAL; |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1213 | goto put_inode_out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | t += 1; |
| 1218 | sbi->def_entries = 1; |
| 1219 | done = sizeof(struct ATTR_DEF_ENTRY); |
| 1220 | sbi->reparse.max_size = MAXIMUM_REPARSE_DATA_BUFFER_SIZE; |
Colin Ian King | f8d87ed | 2021-08-16 11:13:08 +0100 | [diff] [blame] | 1221 | sbi->ea_max_size = 0x10000; /* default formatter value */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1222 | |
| 1223 | while (done + sizeof(struct ATTR_DEF_ENTRY) <= bytes) { |
| 1224 | u32 t32 = le32_to_cpu(t->type); |
| 1225 | u64 sz = le64_to_cpu(t->max_sz); |
| 1226 | |
| 1227 | if ((t32 & 0xF) || le32_to_cpu(t[-1].type) >= t32) |
| 1228 | break; |
| 1229 | |
| 1230 | if (t->type == ATTR_REPARSE) |
| 1231 | sbi->reparse.max_size = sz; |
| 1232 | else if (t->type == ATTR_EA) |
| 1233 | sbi->ea_max_size = sz; |
| 1234 | |
| 1235 | done += sizeof(struct ATTR_DEF_ENTRY); |
| 1236 | t += 1; |
| 1237 | sbi->def_entries += 1; |
| 1238 | } |
| 1239 | iput(inode); |
| 1240 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1241 | /* Load $UpCase. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1242 | ref.low = cpu_to_le32(MFT_REC_UPCASE); |
| 1243 | ref.seq = cpu_to_le16(MFT_REC_UPCASE); |
| 1244 | inode = ntfs_iget5(sb, &ref, &NAME_UPCASE); |
| 1245 | if (IS_ERR(inode)) { |
Kari Argillander | 0412016 | 2021-09-09 21:09:32 +0300 | [diff] [blame] | 1246 | ntfs_err(sb, "Failed to load $UpCase."); |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1247 | err = PTR_ERR(inode); |
| 1248 | goto out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1249 | } |
| 1250 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1251 | if (inode->i_size != 0x10000 * sizeof(short)) { |
| 1252 | err = -EINVAL; |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1253 | goto put_inode_out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1254 | } |
| 1255 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1256 | for (idx = 0; idx < (0x10000 * sizeof(short) >> PAGE_SHIFT); idx++) { |
| 1257 | const __le16 *src; |
Kari Argillander | 0056b27 | 2021-09-09 21:09:40 +0300 | [diff] [blame] | 1258 | u16 *dst = Add2Ptr(sbi->upcase, idx << PAGE_SHIFT); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1259 | struct page *page = ntfs_map_page(inode->i_mapping, idx); |
| 1260 | |
| 1261 | if (IS_ERR(page)) { |
| 1262 | err = PTR_ERR(page); |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1263 | goto put_inode_out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | src = page_address(page); |
| 1267 | |
| 1268 | #ifdef __BIG_ENDIAN |
| 1269 | for (i = 0; i < PAGE_SIZE / sizeof(u16); i++) |
| 1270 | *dst++ = le16_to_cpu(*src++); |
| 1271 | #else |
| 1272 | memcpy(dst, src, PAGE_SIZE); |
| 1273 | #endif |
| 1274 | ntfs_unmap_page(page); |
| 1275 | } |
| 1276 | |
Kari Argillander | 0056b27 | 2021-09-09 21:09:40 +0300 | [diff] [blame] | 1277 | shared = ntfs_set_shared(sbi->upcase, 0x10000 * sizeof(short)); |
| 1278 | if (shared && sbi->upcase != shared) { |
| 1279 | kvfree(sbi->upcase); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1280 | sbi->upcase = shared; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | iput(inode); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1284 | |
| 1285 | if (is_ntfs3(sbi)) { |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1286 | /* Load $Secure. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1287 | err = ntfs_security_init(sbi); |
| 1288 | if (err) |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1289 | goto out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1290 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1291 | /* Load $Extend. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1292 | err = ntfs_extend_init(sbi); |
| 1293 | if (err) |
| 1294 | goto load_root; |
| 1295 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1296 | /* Load $Extend\$Reparse. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1297 | err = ntfs_reparse_init(sbi); |
| 1298 | if (err) |
| 1299 | goto load_root; |
| 1300 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1301 | /* Load $Extend\$ObjId. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1302 | err = ntfs_objid_init(sbi); |
| 1303 | if (err) |
| 1304 | goto load_root; |
| 1305 | } |
| 1306 | |
| 1307 | load_root: |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1308 | /* Load root. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1309 | ref.low = cpu_to_le32(MFT_REC_ROOT); |
| 1310 | ref.seq = cpu_to_le16(MFT_REC_ROOT); |
| 1311 | inode = ntfs_iget5(sb, &ref, &NAME_ROOT); |
Edward Lo | c1ca8ef | 2022-09-09 09:03:10 +0800 | [diff] [blame] | 1312 | if (IS_ERR(inode) || !inode->i_op) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1313 | ntfs_err(sb, "Failed to load root."); |
Edward Lo | c1ca8ef | 2022-09-09 09:03:10 +0800 | [diff] [blame] | 1314 | err = IS_ERR(inode) ? PTR_ERR(inode) : -EINVAL; |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1315 | goto out; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1316 | } |
| 1317 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1318 | sb->s_root = d_make_root(inode); |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1319 | if (!sb->s_root) { |
| 1320 | err = -ENOMEM; |
| 1321 | goto put_inode_out; |
| 1322 | } |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1323 | |
| 1324 | return 0; |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1325 | |
| 1326 | put_inode_out: |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1327 | iput(inode); |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1328 | out: |
| 1329 | /* |
| 1330 | * Free resources here. |
| 1331 | * ntfs_fs_free will be called with fc->s_fs_info = NULL |
| 1332 | */ |
Shigeru Yoshida | 51e76a2 | 2022-08-23 19:32:05 +0900 | [diff] [blame] | 1333 | put_mount_options(sbi->options); |
Konstantin Komarov | 9b75450 | 2021-09-28 19:00:30 +0300 | [diff] [blame] | 1334 | put_ntfs(sbi); |
| 1335 | sb->s_fs_info = NULL; |
| 1336 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1337 | return err; |
| 1338 | } |
| 1339 | |
| 1340 | void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len) |
| 1341 | { |
| 1342 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 1343 | struct block_device *bdev = sb->s_bdev; |
| 1344 | sector_t devblock = (u64)lcn * sbi->blocks_per_cluster; |
| 1345 | unsigned long blocks = (u64)len * sbi->blocks_per_cluster; |
| 1346 | unsigned long cnt = 0; |
| 1347 | unsigned long limit = global_zone_page_state(NR_FREE_PAGES) |
| 1348 | << (PAGE_SHIFT - sb->s_blocksize_bits); |
| 1349 | |
| 1350 | if (limit >= 0x2000) |
| 1351 | limit -= 0x1000; |
| 1352 | else if (limit < 32) |
| 1353 | limit = 32; |
| 1354 | else |
| 1355 | limit >>= 1; |
| 1356 | |
| 1357 | while (blocks--) { |
| 1358 | clean_bdev_aliases(bdev, devblock++, 1); |
| 1359 | if (cnt++ >= limit) { |
| 1360 | sync_blockdev(bdev); |
| 1361 | cnt = 0; |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | /* |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1367 | * ntfs_discard - Issue a discard request (trim for SSD). |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1368 | */ |
| 1369 | int ntfs_discard(struct ntfs_sb_info *sbi, CLST lcn, CLST len) |
| 1370 | { |
| 1371 | int err; |
| 1372 | u64 lbo, bytes, start, end; |
| 1373 | struct super_block *sb; |
| 1374 | |
| 1375 | if (sbi->used.next_free_lcn == lcn + len) |
| 1376 | sbi->used.next_free_lcn = lcn; |
| 1377 | |
| 1378 | if (sbi->flags & NTFS_FLAGS_NODISCARD) |
| 1379 | return -EOPNOTSUPP; |
| 1380 | |
Kari Argillander | 564c97b | 2021-09-07 18:35:51 +0300 | [diff] [blame] | 1381 | if (!sbi->options->discard) |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1382 | return -EOPNOTSUPP; |
| 1383 | |
| 1384 | lbo = (u64)lcn << sbi->cluster_bits; |
| 1385 | bytes = (u64)len << sbi->cluster_bits; |
| 1386 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1387 | /* Align up 'start' on discard_granularity. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1388 | start = (lbo + sbi->discard_granularity - 1) & |
| 1389 | sbi->discard_granularity_mask_inv; |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1390 | /* Align down 'end' on discard_granularity. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1391 | end = (lbo + bytes) & sbi->discard_granularity_mask_inv; |
| 1392 | |
| 1393 | sb = sbi->sb; |
| 1394 | if (start >= end) |
| 1395 | return 0; |
| 1396 | |
| 1397 | err = blkdev_issue_discard(sb->s_bdev, start >> 9, (end - start) >> 9, |
Christoph Hellwig | 44abff2c | 2022-04-15 06:52:57 +0200 | [diff] [blame] | 1398 | GFP_NOFS); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1399 | |
| 1400 | if (err == -EOPNOTSUPP) |
| 1401 | sbi->flags |= NTFS_FLAGS_NODISCARD; |
| 1402 | |
| 1403 | return err; |
| 1404 | } |
| 1405 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 1406 | static int ntfs_fs_get_tree(struct fs_context *fc) |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1407 | { |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 1408 | return get_tree_bdev(fc, ntfs_fill_super); |
| 1409 | } |
| 1410 | |
| 1411 | /* |
| 1412 | * ntfs_fs_free - Free fs_context. |
| 1413 | * |
| 1414 | * Note that this will be called after fill_super and reconfigure |
| 1415 | * even when they pass. So they have to take pointers if they pass. |
| 1416 | */ |
| 1417 | static void ntfs_fs_free(struct fs_context *fc) |
| 1418 | { |
| 1419 | struct ntfs_mount_options *opts = fc->fs_private; |
| 1420 | struct ntfs_sb_info *sbi = fc->s_fs_info; |
| 1421 | |
| 1422 | if (sbi) |
| 1423 | put_ntfs(sbi); |
| 1424 | |
| 1425 | if (opts) |
| 1426 | put_mount_options(opts); |
| 1427 | } |
| 1428 | |
| 1429 | static const struct fs_context_operations ntfs_context_ops = { |
| 1430 | .parse_param = ntfs_fs_parse_param, |
| 1431 | .get_tree = ntfs_fs_get_tree, |
| 1432 | .reconfigure = ntfs_fs_reconfigure, |
| 1433 | .free = ntfs_fs_free, |
| 1434 | }; |
| 1435 | |
| 1436 | /* |
| 1437 | * ntfs_init_fs_context - Initialize spi and opts |
| 1438 | * |
Konstantin Komarov | 6700eab | 2022-06-30 18:49:24 +0300 | [diff] [blame] | 1439 | * This will called when mount/remount. We will first initialize |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 1440 | * options so that if remount we can use just that. |
| 1441 | */ |
| 1442 | static int ntfs_init_fs_context(struct fs_context *fc) |
| 1443 | { |
| 1444 | struct ntfs_mount_options *opts; |
| 1445 | struct ntfs_sb_info *sbi; |
| 1446 | |
| 1447 | opts = kzalloc(sizeof(struct ntfs_mount_options), GFP_NOFS); |
| 1448 | if (!opts) |
| 1449 | return -ENOMEM; |
| 1450 | |
| 1451 | /* Default options. */ |
| 1452 | opts->fs_uid = current_uid(); |
| 1453 | opts->fs_gid = current_gid(); |
| 1454 | opts->fs_fmask_inv = ~current_umask(); |
| 1455 | opts->fs_dmask_inv = ~current_umask(); |
| 1456 | |
| 1457 | if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) |
| 1458 | goto ok; |
| 1459 | |
| 1460 | sbi = kzalloc(sizeof(struct ntfs_sb_info), GFP_NOFS); |
Kari Argillander | 27fac77 | 2021-09-07 18:35:53 +0300 | [diff] [blame] | 1461 | if (!sbi) |
| 1462 | goto free_opts; |
| 1463 | |
| 1464 | sbi->upcase = kvmalloc(0x10000 * sizeof(short), GFP_KERNEL); |
| 1465 | if (!sbi->upcase) |
| 1466 | goto free_sbi; |
| 1467 | |
| 1468 | ratelimit_state_init(&sbi->msg_ratelimit, DEFAULT_RATELIMIT_INTERVAL, |
| 1469 | DEFAULT_RATELIMIT_BURST); |
| 1470 | |
| 1471 | mutex_init(&sbi->compress.mtx_lznt); |
| 1472 | #ifdef CONFIG_NTFS3_LZX_XPRESS |
| 1473 | mutex_init(&sbi->compress.mtx_xpress); |
| 1474 | mutex_init(&sbi->compress.mtx_lzx); |
| 1475 | #endif |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 1476 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 1477 | fc->s_fs_info = sbi; |
| 1478 | ok: |
| 1479 | fc->fs_private = opts; |
| 1480 | fc->ops = &ntfs_context_ops; |
| 1481 | |
| 1482 | return 0; |
Kari Argillander | 27fac77 | 2021-09-07 18:35:53 +0300 | [diff] [blame] | 1483 | free_sbi: |
| 1484 | kfree(sbi); |
Colin Ian King | 880301b | 2021-09-10 11:02:02 +0100 | [diff] [blame] | 1485 | free_opts: |
| 1486 | kfree(opts); |
Kari Argillander | 27fac77 | 2021-09-07 18:35:53 +0300 | [diff] [blame] | 1487 | return -ENOMEM; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | // clang-format off |
| 1491 | static struct file_system_type ntfs_fs_type = { |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 1492 | .owner = THIS_MODULE, |
| 1493 | .name = "ntfs3", |
| 1494 | .init_fs_context = ntfs_init_fs_context, |
| 1495 | .parameters = ntfs_fs_parameters, |
| 1496 | .kill_sb = kill_block_super, |
| 1497 | .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP, |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1498 | }; |
| 1499 | // clang-format on |
| 1500 | |
| 1501 | static int __init init_ntfs_fs(void) |
| 1502 | { |
| 1503 | int err; |
| 1504 | |
Kari Argillander | 2e3a51b | 2021-08-29 17:42:39 +0300 | [diff] [blame] | 1505 | pr_info("ntfs3: Max link count %u\n", NTFS_LINK_MAX); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1506 | |
Kari Argillander | 2e3a51b | 2021-08-29 17:42:39 +0300 | [diff] [blame] | 1507 | if (IS_ENABLED(CONFIG_NTFS3_FS_POSIX_ACL)) |
| 1508 | pr_info("ntfs3: Enabled Linux POSIX ACLs support\n"); |
| 1509 | if (IS_ENABLED(CONFIG_NTFS3_64BIT_CLUSTER)) |
| 1510 | pr_notice("ntfs3: Warning: Activated 64 bits per cluster. Windows does not support this\n"); |
| 1511 | if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS)) |
| 1512 | pr_info("ntfs3: Read-only LZX/Xpress compression included\n"); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1513 | |
| 1514 | err = ntfs3_init_bitmap(); |
| 1515 | if (err) |
| 1516 | return err; |
| 1517 | |
| 1518 | ntfs_inode_cachep = kmem_cache_create( |
| 1519 | "ntfs_inode_cache", sizeof(struct ntfs_inode), 0, |
| 1520 | (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT), |
| 1521 | init_once); |
| 1522 | if (!ntfs_inode_cachep) { |
| 1523 | err = -ENOMEM; |
| 1524 | goto out1; |
| 1525 | } |
| 1526 | |
| 1527 | err = register_filesystem(&ntfs_fs_type); |
| 1528 | if (err) |
| 1529 | goto out; |
| 1530 | |
| 1531 | return 0; |
| 1532 | out: |
| 1533 | kmem_cache_destroy(ntfs_inode_cachep); |
| 1534 | out1: |
| 1535 | ntfs3_exit_bitmap(); |
| 1536 | return err; |
| 1537 | } |
| 1538 | |
| 1539 | static void __exit exit_ntfs_fs(void) |
| 1540 | { |
Konstantin Komarov | ae6b47b | 2022-09-12 18:54:06 +0300 | [diff] [blame] | 1541 | rcu_barrier(); |
| 1542 | kmem_cache_destroy(ntfs_inode_cachep); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1543 | unregister_filesystem(&ntfs_fs_type); |
| 1544 | ntfs3_exit_bitmap(); |
| 1545 | } |
| 1546 | |
| 1547 | MODULE_LICENSE("GPL"); |
| 1548 | MODULE_DESCRIPTION("ntfs3 read/write filesystem"); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1549 | #ifdef CONFIG_NTFS3_FS_POSIX_ACL |
| 1550 | MODULE_INFO(behaviour, "Enabled Linux POSIX ACLs support"); |
| 1551 | #endif |
| 1552 | #ifdef CONFIG_NTFS3_64BIT_CLUSTER |
Kari Argillander | 2e3a51b | 2021-08-29 17:42:39 +0300 | [diff] [blame] | 1553 | MODULE_INFO(cluster, "Warning: Activated 64 bits per cluster. Windows does not support this"); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1554 | #endif |
| 1555 | #ifdef CONFIG_NTFS3_LZX_XPRESS |
| 1556 | MODULE_INFO(compression, "Read-only lzx/xpress compression included"); |
| 1557 | #endif |
| 1558 | |
| 1559 | MODULE_AUTHOR("Konstantin Komarov"); |
| 1560 | MODULE_ALIAS_FS("ntfs3"); |
| 1561 | |
| 1562 | module_init(init_ntfs_fs); |
| 1563 | module_exit(exit_ntfs_fs); |