at commit c01b091b
| 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 |
| 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. |
| 19 | * resident attribute - Attribute with content stored directly in the MFT record |
| 20 | * non-resident attribute - Attribute with content stored in clusters |
| 21 | * data_size - Size of attribute content in bytes. Equal to inode->i_size |
| 22 | * valid_size - Number of bytes written to the non-resident attribute |
| 23 | * allocated_size - Total size of clusters allocated for non-resident content |
| 24 | * total_size - Actual size of allocated clusters for sparse or compressed attributes |
| 25 | * - Constraint: valid_size <= data_size <= allocated_size |
| 26 | * ADS - Alternative data stream: Named data attribute (0x80) |
| 27 | * |
| 28 | * WSL - Windows Subsystem for Linux |
| 29 | * https://docs.microsoft.com/en-us/windows/wsl/file-permissions |
| 30 | * It stores uid/gid/mode/dev in xattr |
| 31 | * |
| 32 | * ntfs allows up to 2^64 clusters per volume. |
| 33 | * It means you should use 64 bits lcn to operate with ntfs. |
| 34 | * Implementation of ntfs.sys uses only 32 bits lcn. |
| 35 | * Default ntfs3 uses 32 bits lcn too. |
| 36 | * ntfs3 built with CONFIG_NTFS3_64BIT_CLUSTER (ntfs3_64) uses 64 bits per lcn. |
| 37 | * |
| 38 | * |
| 39 | * ntfs limits, cluster size is 4K (2^12) |
| 40 | * ----------------------------------------------------------------------------- |
| 41 | * | Volume size | Clusters | ntfs.sys | ntfs3 | ntfs3_64 | mkntfs | chkdsk | |
| 42 | * ----------------------------------------------------------------------------- |
| 43 | * | < 16T, 2^44 | < 2^32 | yes | yes | yes | yes | yes | |
| 44 | * | > 16T, 2^44 | > 2^32 | no | no | yes | yes | yes | |
| 45 | * ----------------------------------------------------------|------------------ |
| 46 | * |
| 47 | * To mount large volumes as ntfs one should use large cluster size (up to 2M) |
| 48 | * The maximum volume size in this case is 2^32 * 2^21 = 2^53 = 8P |
| 49 | * |
| 50 | * ntfs limits, cluster size is 2M (2^21) |
| 51 | * ----------------------------------------------------------------------------- |
| 52 | * | < 8P, 2^53 | < 2^32 | yes | yes | yes | yes | yes | |
| 53 | * | > 8P, 2^53 | > 2^32 | no | no | yes | yes | yes | |
| 54 | * ----------------------------------------------------------|------------------ |
| 55 | * |
| 56 | */ |
| 57 | |
| 58 | #include <linux/blkdev.h> |
| 59 | #include <linux/buffer_head.h> |
| 60 | #include <linux/exportfs.h> |
| 61 | #include <linux/fs.h> |
| 62 | #include <linux/fs_context.h> |
| 63 | #include <linux/fs_parser.h> |
| 64 | #include <linux/fs_struct.h> |
| 65 | #include <linux/log2.h> |
| 66 | #include <linux/minmax.h> |
| 67 | #include <linux/module.h> |
| 68 | #include <linux/nls.h> |
| 69 | #include <linux/overflow.h> |
| 70 | #include <linux/proc_fs.h> |
| 71 | #include <linux/seq_file.h> |
| 72 | #include <linux/statfs.h> |
| 73 | |
| 74 | #include "debug.h" |
| 75 | #include "ntfs.h" |
| 76 | #include "ntfs_fs.h" |
| 77 | #ifdef CONFIG_NTFS3_LZX_XPRESS |
| 78 | #include "lib/lib.h" |
| 79 | #endif |
| 80 | |
| 81 | #ifdef CONFIG_PRINTK |
| 82 | /* |
| 83 | * ntfs_printk - Trace warnings/notices/errors. |
| 84 | * |
| 85 | * Thanks Joe Perches <joe@perches.com> for implementation |
| 86 | */ |
| 87 | void ntfs_printk(const struct super_block *sb, const char *fmt, ...) |
| 88 | { |
| 89 | struct va_format vaf; |
| 90 | va_list args; |
| 91 | int level; |
| 92 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 93 | |
| 94 | /* Should we use different ratelimits for warnings/notices/errors? */ |
| 95 | if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3")) |
| 96 | return; |
| 97 | |
| 98 | va_start(args, fmt); |
| 99 | |
| 100 | level = printk_get_level(fmt); |
| 101 | vaf.fmt = printk_skip_level(fmt); |
| 102 | vaf.va = &args; |
| 103 | printk("%c%cntfs3(%s): %pV\n", KERN_SOH_ASCII, level, sb->s_id, &vaf); |
| 104 | |
| 105 | va_end(args); |
| 106 | } |
| 107 | |
| 108 | static char s_name_buf[512]; |
| 109 | static atomic_t s_name_buf_cnt = ATOMIC_INIT(1); // 1 means 'free s_name_buf'. |
| 110 | |
| 111 | /* |
| 112 | * ntfs_inode_printk |
| 113 | * |
| 114 | * Print warnings/notices/errors about inode using name or inode number. |
| 115 | */ |
| 116 | void ntfs_inode_printk(struct inode *inode, const char *fmt, ...) |
| 117 | { |
| 118 | struct super_block *sb = inode->i_sb; |
| 119 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 120 | char *name; |
| 121 | va_list args; |
| 122 | struct va_format vaf; |
| 123 | int level; |
| 124 | |
| 125 | if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3")) |
| 126 | return; |
| 127 | |
| 128 | /* Use static allocated buffer, if possible. */ |
| 129 | name = atomic_dec_and_test(&s_name_buf_cnt) ? |
| 130 | s_name_buf : |
| 131 | kmalloc(sizeof(s_name_buf), GFP_NOFS); |
| 132 | |
| 133 | if (name) { |
| 134 | struct dentry *de = d_find_alias(inode); |
| 135 | |
| 136 | if (de) { |
| 137 | int len; |
| 138 | spin_lock(&de->d_lock); |
| 139 | len = snprintf(name, sizeof(s_name_buf), " \"%s\"", |
| 140 | de->d_name.name); |
| 141 | spin_unlock(&de->d_lock); |
| 142 | if (len <= 0) |
| 143 | name[0] = 0; |
| 144 | else if (len >= sizeof(s_name_buf)) |
| 145 | name[sizeof(s_name_buf) - 1] = 0; |
| 146 | } else { |
| 147 | name[0] = 0; |
| 148 | } |
| 149 | dput(de); /* Cocci warns if placed in branch "if (de)" */ |
| 150 | } |
| 151 | |
| 152 | va_start(args, fmt); |
| 153 | |
| 154 | level = printk_get_level(fmt); |
| 155 | vaf.fmt = printk_skip_level(fmt); |
| 156 | vaf.va = &args; |
| 157 | |
| 158 | printk("%c%cntfs3(%s): ino=%llx,%s %pV\n", KERN_SOH_ASCII, level, |
| 159 | sb->s_id, inode->i_ino, name ? name : "", &vaf); |
| 160 | |
| 161 | va_end(args); |
| 162 | |
| 163 | atomic_inc(&s_name_buf_cnt); |
| 164 | if (name != s_name_buf) |
| 165 | kfree(name); |
| 166 | } |
| 167 | #endif |
| 168 | |
| 169 | /* |
| 170 | * Shared memory struct. |
| 171 | * |
| 172 | * On-disk ntfs's upcase table is created by ntfs formatter. |
| 173 | * 'upcase' table is 128K bytes of memory. |
| 174 | * We should read it into memory when mounting. |
| 175 | * Several ntfs volumes likely use the same 'upcase' table. |
| 176 | * It is good idea to share in-memory 'upcase' table between different volumes. |
| 177 | * Unfortunately winxp/vista/win7 use different upcase tables. |
| 178 | */ |
| 179 | static DEFINE_SPINLOCK(s_shared_lock); |
| 180 | |
| 181 | static struct { |
| 182 | void *ptr; |
| 183 | u32 len; |
| 184 | int cnt; |
| 185 | } s_shared[8]; |
| 186 | |
| 187 | /* |
| 188 | * ntfs_set_shared |
| 189 | * |
| 190 | * Return: |
| 191 | * * @ptr - If pointer was saved in shared memory. |
| 192 | * * NULL - If pointer was not shared. |
| 193 | */ |
| 194 | void *ntfs_set_shared(void *ptr, u32 bytes) |
| 195 | { |
| 196 | void *ret = NULL; |
| 197 | int i, j = -1; |
| 198 | |
| 199 | spin_lock_bh(&s_shared_lock); |
| 200 | for (i = 0; i < ARRAY_SIZE(s_shared); i++) { |
| 201 | if (!s_shared[i].cnt) { |
| 202 | j = i; |
| 203 | } else if (bytes == s_shared[i].len && |
| 204 | !memcmp(s_shared[i].ptr, ptr, bytes)) { |
| 205 | s_shared[i].cnt += 1; |
| 206 | ret = s_shared[i].ptr; |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | if (!ret && j != -1) { |
| 212 | s_shared[j].ptr = ptr; |
| 213 | s_shared[j].len = bytes; |
| 214 | s_shared[j].cnt = 1; |
| 215 | ret = ptr; |
| 216 | } |
| 217 | spin_unlock_bh(&s_shared_lock); |
| 218 | |
| 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * ntfs_put_shared |
| 224 | * |
| 225 | * Return: |
| 226 | * * @ptr - If pointer is not shared anymore. |
| 227 | * * NULL - If pointer is still shared. |
| 228 | */ |
| 229 | void *ntfs_put_shared(void *ptr) |
| 230 | { |
| 231 | void *ret = ptr; |
| 232 | int i; |
| 233 | |
| 234 | spin_lock_bh(&s_shared_lock); |
| 235 | for (i = 0; i < ARRAY_SIZE(s_shared); i++) { |
| 236 | if (s_shared[i].cnt && s_shared[i].ptr == ptr) { |
| 237 | if (--s_shared[i].cnt) |
| 238 | ret = NULL; |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | spin_unlock_bh(&s_shared_lock); |
| 243 | |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | static inline void put_mount_options(struct ntfs_mount_options *options) |
| 248 | { |
| 249 | kfree(options->nls_name); |
| 250 | unload_nls(options->nls); |
| 251 | kfree(options); |
| 252 | } |
| 253 | |
| 254 | enum Opt { |
| 255 | Opt_uid, |
| 256 | Opt_gid, |
| 257 | Opt_umask, |
| 258 | Opt_dmask, |
| 259 | Opt_fmask, |
| 260 | Opt_immutable, |
| 261 | Opt_discard, |
| 262 | Opt_force, |
| 263 | Opt_sparse, |
| 264 | Opt_nohidden, |
| 265 | Opt_hide_dot_files, |
| 266 | Opt_windows_names, |
| 267 | Opt_showmeta, |
| 268 | Opt_acl, |
| 269 | Opt_acl_bool, |
| 270 | Opt_iocharset, |
| 271 | Opt_prealloc, |
| 272 | Opt_prealloc_bool, |
| 273 | Opt_nocase, |
| 274 | Opt_delalloc, |
| 275 | Opt_delalloc_bool, |
| 276 | Opt_ads, |
| 277 | Opt_ads_bool, |
| 278 | Opt_err, |
| 279 | }; |
| 280 | |
| 281 | // clang-format off |
| 282 | static const struct fs_parameter_spec ntfs_fs_parameters[] = { |
| 283 | fsparam_uid("uid", Opt_uid), |
| 284 | fsparam_gid("gid", Opt_gid), |
| 285 | fsparam_u32oct("umask", Opt_umask), |
| 286 | fsparam_u32oct("dmask", Opt_dmask), |
| 287 | fsparam_u32oct("fmask", Opt_fmask), |
| 288 | fsparam_flag("sys_immutable", Opt_immutable), |
| 289 | fsparam_flag("discard", Opt_discard), |
| 290 | fsparam_flag("force", Opt_force), |
| 291 | fsparam_flag("sparse", Opt_sparse), |
| 292 | fsparam_flag("nohidden", Opt_nohidden), |
| 293 | fsparam_flag("hide_dot_files", Opt_hide_dot_files), |
| 294 | fsparam_flag("windows_names", Opt_windows_names), |
| 295 | fsparam_flag("showmeta", Opt_showmeta), |
| 296 | fsparam_flag("acl", Opt_acl), |
| 297 | fsparam_bool("acl", Opt_acl_bool), |
| 298 | fsparam_string("iocharset", Opt_iocharset), |
| 299 | fsparam_flag("prealloc", Opt_prealloc), |
| 300 | fsparam_bool("prealloc", Opt_prealloc_bool), |
| 301 | fsparam_flag("nocase", Opt_nocase), |
| 302 | fsparam_flag("delalloc", Opt_delalloc), |
| 303 | fsparam_bool("delalloc", Opt_delalloc_bool), |
| 304 | fsparam_flag("ads", Opt_ads), |
| 305 | fsparam_bool("ads", Opt_ads_bool), |
| 306 | {} |
| 307 | }; |
| 308 | // clang-format on |
| 309 | |
| 310 | /* |
| 311 | * Load nls table or if @nls is utf8 then return NULL. |
| 312 | * |
| 313 | */ |
| 314 | static struct nls_table *ntfs_load_nls(const char *nls) |
| 315 | { |
| 316 | struct nls_table *ret; |
| 317 | |
| 318 | if (!nls) |
| 319 | nls = CONFIG_NLS_DEFAULT; |
| 320 | |
| 321 | if (strcmp(nls, "utf8") == 0) |
| 322 | return NULL; |
| 323 | |
| 324 | if (strcmp(nls, CONFIG_NLS_DEFAULT) == 0) |
| 325 | return load_nls_default(); |
| 326 | |
| 327 | ret = load_nls(nls); |
| 328 | if (ret) |
| 329 | return ret; |
| 330 | |
| 331 | return ERR_PTR(-EINVAL); |
| 332 | } |
| 333 | |
| 334 | static int ntfs_fs_parse_param(struct fs_context *fc, |
| 335 | struct fs_parameter *param) |
| 336 | { |
| 337 | struct ntfs_mount_options *opts = fc->fs_private; |
| 338 | struct fs_parse_result result; |
| 339 | int opt; |
| 340 | |
| 341 | opt = fs_parse(fc, ntfs_fs_parameters, param, &result); |
| 342 | if (opt < 0) |
| 343 | return opt; |
| 344 | |
| 345 | switch (opt) { |
| 346 | case Opt_uid: |
| 347 | opts->fs_uid = result.uid; |
| 348 | break; |
| 349 | case Opt_gid: |
| 350 | opts->fs_gid = result.gid; |
| 351 | break; |
| 352 | case Opt_umask: |
| 353 | if (result.uint_32 & ~07777) |
| 354 | return invalf(fc, "ntfs3: Invalid value for umask."); |
| 355 | opts->fs_fmask_inv = ~result.uint_32; |
| 356 | opts->fs_dmask_inv = ~result.uint_32; |
| 357 | opts->fmask = 1; |
| 358 | opts->dmask = 1; |
| 359 | break; |
| 360 | case Opt_dmask: |
| 361 | if (result.uint_32 & ~07777) |
| 362 | return invalf(fc, "ntfs3: Invalid value for dmask."); |
| 363 | opts->fs_dmask_inv = ~result.uint_32; |
| 364 | opts->dmask = 1; |
| 365 | break; |
| 366 | case Opt_fmask: |
| 367 | if (result.uint_32 & ~07777) |
| 368 | return invalf(fc, "ntfs3: Invalid value for fmask."); |
| 369 | opts->fs_fmask_inv = ~result.uint_32; |
| 370 | opts->fmask = 1; |
| 371 | break; |
| 372 | case Opt_immutable: |
| 373 | opts->sys_immutable = 1; |
| 374 | break; |
| 375 | case Opt_discard: |
| 376 | opts->discard = 1; |
| 377 | break; |
| 378 | case Opt_force: |
| 379 | opts->force = 1; |
| 380 | break; |
| 381 | case Opt_sparse: |
| 382 | opts->sparse = 1; |
| 383 | break; |
| 384 | case Opt_nohidden: |
| 385 | opts->nohidden = 1; |
| 386 | break; |
| 387 | case Opt_hide_dot_files: |
| 388 | opts->hide_dot_files = 1; |
| 389 | break; |
| 390 | case Opt_windows_names: |
| 391 | opts->windows_names = 1; |
| 392 | break; |
| 393 | case Opt_showmeta: |
| 394 | opts->showmeta = 1; |
| 395 | break; |
| 396 | case Opt_acl_bool: |
| 397 | if (result.boolean) { |
| 398 | fallthrough; |
| 399 | case Opt_acl: |
| 400 | #ifdef CONFIG_NTFS3_FS_POSIX_ACL |
| 401 | fc->sb_flags |= SB_POSIXACL; |
| 402 | #else |
| 403 | return invalf( |
| 404 | fc, "ntfs3: Support for ACL not compiled in!"); |
| 405 | #endif |
| 406 | } else |
| 407 | fc->sb_flags &= ~SB_POSIXACL; |
| 408 | break; |
| 409 | case Opt_iocharset: |
| 410 | kfree(opts->nls_name); |
| 411 | opts->nls_name = param->string; |
| 412 | param->string = NULL; |
| 413 | break; |
| 414 | case Opt_prealloc: |
| 415 | opts->prealloc = 1; |
| 416 | break; |
| 417 | case Opt_prealloc_bool: |
| 418 | opts->prealloc = result.boolean; |
| 419 | break; |
| 420 | case Opt_nocase: |
| 421 | opts->nocase = 1; |
| 422 | break; |
| 423 | case Opt_delalloc: |
| 424 | opts->delalloc = 1; |
| 425 | break; |
| 426 | case Opt_delalloc_bool: |
| 427 | opts->delalloc = result.boolean; |
| 428 | break; |
| 429 | case Opt_ads: |
| 430 | opts->ads = 1; |
| 431 | break; |
| 432 | case Opt_ads_bool: |
| 433 | opts->ads = result.boolean; |
| 434 | break; |
| 435 | default: |
| 436 | /* Should not be here unless we forget add case. */ |
| 437 | return -EINVAL; |
| 438 | } |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | static int ntfs_fs_reconfigure(struct fs_context *fc) |
| 443 | { |
| 444 | struct super_block *sb = fc->root->d_sb; |
| 445 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 446 | struct ntfs_mount_options *new_opts = fc->fs_private; |
| 447 | int ro_rw; |
| 448 | |
| 449 | ro_rw = sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY); |
| 450 | if (ro_rw && (sbi->flags & NTFS_FLAGS_NEED_REPLAY)) { |
| 451 | errorf(fc, |
| 452 | "ntfs3: Couldn't remount rw because journal is not replayed. Please umount/remount instead\n"); |
| 453 | return -EINVAL; |
| 454 | } |
| 455 | |
| 456 | new_opts->nls = ntfs_load_nls(new_opts->nls_name); |
| 457 | if (IS_ERR(new_opts->nls)) { |
| 458 | new_opts->nls = NULL; |
| 459 | errorf(fc, "ntfs3: Cannot load iocharset %s", |
| 460 | new_opts->nls_name); |
| 461 | return -EINVAL; |
| 462 | } |
| 463 | if (new_opts->nls != sbi->options->nls) |
| 464 | return invalf( |
| 465 | fc, |
| 466 | "ntfs3: Cannot use different iocharset when remounting!"); |
| 467 | |
| 468 | if (ro_rw && (sbi->volume.flags & VOLUME_FLAG_DIRTY) && |
| 469 | !new_opts->force) { |
| 470 | errorf(fc, |
| 471 | "ntfs3: Volume is dirty and \"force\" flag is not set!"); |
| 472 | return -EINVAL; |
| 473 | } |
| 474 | |
| 475 | sync_filesystem(sb); |
| 476 | swap(sbi->options, fc->fs_private); |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | #ifdef CONFIG_PROC_FS |
| 482 | static struct proc_dir_entry *proc_info_root; |
| 483 | |
| 484 | /* |
| 485 | * ntfs3_volinfo: |
| 486 | * |
| 487 | * The content of /proc/fs/ntfs3/<dev>/volinfo |
| 488 | * |
| 489 | * ntfs3.1 |
| 490 | * cluster size |
| 491 | * number of clusters |
| 492 | * total number of mft records |
| 493 | * number of used mft records ~= number of files + folders |
| 494 | * real state of ntfs "dirty"/"clean" |
| 495 | * current state of ntfs "dirty"/"clean" |
| 496 | */ |
| 497 | static int ntfs3_volinfo(struct seq_file *m, void *o) |
| 498 | { |
| 499 | struct super_block *sb = m->private; |
| 500 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 501 | |
| 502 | seq_printf(m, "ntfs%d.%d\n%u\n%zu\n%zu\n%zu\n%s\n%s\n", |
| 503 | sbi->volume.major_ver, sbi->volume.minor_ver, |
| 504 | sbi->cluster_size, sbi->used.bitmap.nbits, |
| 505 | sbi->mft.bitmap.nbits, |
| 506 | sbi->mft.bitmap.nbits - wnd_zeroes(&sbi->mft.bitmap), |
| 507 | sbi->volume.real_dirty ? "dirty" : "clean", |
| 508 | (sbi->volume.flags & VOLUME_FLAG_DIRTY) ? "dirty" : "clean"); |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | static int ntfs3_volinfo_open(struct inode *inode, struct file *file) |
| 514 | { |
| 515 | return single_open(file, ntfs3_volinfo, pde_data(inode)); |
| 516 | } |
| 517 | |
| 518 | /* read /proc/fs/ntfs3/<dev>/label */ |
| 519 | static int ntfs3_label_show(struct seq_file *m, void *o) |
| 520 | { |
| 521 | struct super_block *sb = m->private; |
| 522 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 523 | |
| 524 | seq_printf(m, "%s\n", sbi->volume.label); |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | /* write /proc/fs/ntfs3/<dev>/label */ |
| 530 | static ssize_t ntfs3_label_write(struct file *file, const char __user *buffer, |
| 531 | size_t count, loff_t *ppos) |
| 532 | { |
| 533 | int err; |
| 534 | struct super_block *sb = pde_data(file_inode(file)); |
| 535 | ssize_t ret = count; |
| 536 | u8 *label; |
| 537 | |
| 538 | if (sb_rdonly(sb)) |
| 539 | return -EROFS; |
| 540 | |
| 541 | label = kmalloc(count, GFP_NOFS); |
| 542 | |
| 543 | if (!label) |
| 544 | return -ENOMEM; |
| 545 | |
| 546 | if (copy_from_user(label, buffer, ret)) { |
| 547 | ret = -EFAULT; |
| 548 | goto out; |
| 549 | } |
| 550 | while (ret > 0 && label[ret - 1] == '\n') |
| 551 | ret -= 1; |
| 552 | |
| 553 | err = ntfs_set_label(sb->s_fs_info, label, ret); |
| 554 | |
| 555 | if (err < 0) { |
| 556 | ntfs_err(sb, "failed (%d) to write label", err); |
| 557 | ret = err; |
| 558 | goto out; |
| 559 | } |
| 560 | |
| 561 | *ppos += count; |
| 562 | ret = count; |
| 563 | out: |
| 564 | kfree(label); |
| 565 | return ret; |
| 566 | } |
| 567 | |
| 568 | static int ntfs3_label_open(struct inode *inode, struct file *file) |
| 569 | { |
| 570 | return single_open(file, ntfs3_label_show, pde_data(inode)); |
| 571 | } |
| 572 | |
| 573 | static const struct proc_ops ntfs3_volinfo_fops = { |
| 574 | .proc_read = seq_read, |
| 575 | .proc_lseek = seq_lseek, |
| 576 | .proc_release = single_release, |
| 577 | .proc_open = ntfs3_volinfo_open, |
| 578 | }; |
| 579 | |
| 580 | static const struct proc_ops ntfs3_label_fops = { |
| 581 | .proc_read = seq_read, |
| 582 | .proc_lseek = seq_lseek, |
| 583 | .proc_release = single_release, |
| 584 | .proc_open = ntfs3_label_open, |
| 585 | .proc_write = ntfs3_label_write, |
| 586 | }; |
| 587 | |
| 588 | static void ntfs_create_procdir(struct super_block *sb) |
| 589 | { |
| 590 | struct proc_dir_entry *e; |
| 591 | |
| 592 | if (!proc_info_root) |
| 593 | return; |
| 594 | |
| 595 | e = proc_mkdir(sb->s_id, proc_info_root); |
| 596 | if (e) { |
| 597 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 598 | |
| 599 | proc_create_data("volinfo", 0444, e, &ntfs3_volinfo_fops, sb); |
| 600 | proc_create_data("label", 0644, e, &ntfs3_label_fops, sb); |
| 601 | sbi->procdir = e; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | static void ntfs_remove_procdir(struct super_block *sb) |
| 606 | { |
| 607 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 608 | |
| 609 | if (!sbi->procdir) |
| 610 | return; |
| 611 | |
| 612 | remove_proc_entry("label", sbi->procdir); |
| 613 | remove_proc_entry("volinfo", sbi->procdir); |
| 614 | remove_proc_entry(sb->s_id, proc_info_root); |
| 615 | sbi->procdir = NULL; |
| 616 | } |
| 617 | |
| 618 | static void ntfs_create_proc_root(void) |
| 619 | { |
| 620 | proc_info_root = proc_mkdir("fs/ntfs3", NULL); |
| 621 | } |
| 622 | |
| 623 | static void ntfs_remove_proc_root(void) |
| 624 | { |
| 625 | if (proc_info_root) { |
| 626 | remove_proc_entry("fs/ntfs3", NULL); |
| 627 | proc_info_root = NULL; |
| 628 | } |
| 629 | } |
| 630 | #else |
| 631 | // clang-format off |
| 632 | static void ntfs_create_procdir(struct super_block *sb){} |
| 633 | static void ntfs_remove_procdir(struct super_block *sb){} |
| 634 | static void ntfs_create_proc_root(void){} |
| 635 | static void ntfs_remove_proc_root(void){} |
| 636 | // clang-format on |
| 637 | #endif |
| 638 | |
| 639 | static struct kmem_cache *ntfs_inode_cachep; |
| 640 | |
| 641 | static struct inode *ntfs_alloc_inode(struct super_block *sb) |
| 642 | { |
| 643 | struct ntfs_inode *ni = alloc_inode_sb(sb, ntfs_inode_cachep, GFP_NOFS); |
| 644 | |
| 645 | if (!ni) |
| 646 | return NULL; |
| 647 | |
| 648 | memset(ni, 0, offsetof(struct ntfs_inode, vfs_inode)); |
| 649 | mutex_init(&ni->ni_lock); |
| 650 | return &ni->vfs_inode; |
| 651 | } |
| 652 | |
| 653 | static void ntfs_free_inode(struct inode *inode) |
| 654 | { |
| 655 | struct ntfs_inode *ni = ntfs_i(inode); |
| 656 | |
| 657 | mutex_destroy(&ni->ni_lock); |
| 658 | kmem_cache_free(ntfs_inode_cachep, ni); |
| 659 | } |
| 660 | |
| 661 | static void init_once(void *foo) |
| 662 | { |
| 663 | struct ntfs_inode *ni = foo; |
| 664 | |
| 665 | inode_init_once(&ni->vfs_inode); |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | * Noinline to reduce binary size. |
| 670 | */ |
| 671 | static noinline void ntfs3_put_sbi(struct ntfs_sb_info *sbi) |
| 672 | { |
| 673 | wnd_close(&sbi->mft.bitmap); |
| 674 | wnd_close(&sbi->used.bitmap); |
| 675 | |
| 676 | if (sbi->mft.ni) { |
| 677 | iput(&sbi->mft.ni->vfs_inode); |
| 678 | sbi->mft.ni = NULL; |
| 679 | } |
| 680 | |
| 681 | if (sbi->security.ni) { |
| 682 | iput(&sbi->security.ni->vfs_inode); |
| 683 | sbi->security.ni = NULL; |
| 684 | } |
| 685 | |
| 686 | if (sbi->reparse.ni) { |
| 687 | iput(&sbi->reparse.ni->vfs_inode); |
| 688 | sbi->reparse.ni = NULL; |
| 689 | } |
| 690 | |
| 691 | if (sbi->objid.ni) { |
| 692 | iput(&sbi->objid.ni->vfs_inode); |
| 693 | sbi->objid.ni = NULL; |
| 694 | } |
| 695 | |
| 696 | if (sbi->volume.ni) { |
| 697 | iput(&sbi->volume.ni->vfs_inode); |
| 698 | sbi->volume.ni = NULL; |
| 699 | } |
| 700 | |
| 701 | ntfs_update_mftmirr(sbi); |
| 702 | |
| 703 | indx_clear(&sbi->security.index_sii); |
| 704 | indx_clear(&sbi->security.index_sdh); |
| 705 | indx_clear(&sbi->reparse.index_r); |
| 706 | indx_clear(&sbi->objid.index_o); |
| 707 | } |
| 708 | |
| 709 | static void ntfs3_free_sbi(struct ntfs_sb_info *sbi) |
| 710 | { |
| 711 | if (sbi->options) |
| 712 | put_mount_options(sbi->options); |
| 713 | kfree(sbi->new_rec); |
| 714 | kvfree(ntfs_put_shared(sbi->upcase)); |
| 715 | kvfree(sbi->def_table); |
| 716 | kfree(sbi->compress.lznt); |
| 717 | #ifdef CONFIG_NTFS3_LZX_XPRESS |
| 718 | xpress_free_decompressor(sbi->compress.xpress); |
| 719 | lzx_free_decompressor(sbi->compress.lzx); |
| 720 | #endif |
| 721 | kfree(sbi); |
| 722 | } |
| 723 | |
| 724 | static void ntfs3_free_sbi_rcu(struct rcu_head *head) |
| 725 | { |
| 726 | ntfs3_free_sbi(container_of(head, struct ntfs_sb_info, rcu)); |
| 727 | } |
| 728 | |
| 729 | static void ntfs_put_super(struct super_block *sb) |
| 730 | { |
| 731 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 732 | |
| 733 | ntfs_remove_procdir(sb); |
| 734 | |
| 735 | /* Mark rw ntfs as clear, if possible. */ |
| 736 | ntfs_set_state(sbi, NTFS_DIRTY_CLEAR); |
| 737 | |
| 738 | /* |
| 739 | * The mount options stay until the sbi is freed: ->d_hash() and |
| 740 | * ->d_compare() of a pathwalk in rcu mode read the nls table through |
| 741 | * them. |
| 742 | */ |
| 743 | ntfs3_put_sbi(sbi); |
| 744 | } |
| 745 | |
| 746 | static int ntfs_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 747 | { |
| 748 | struct super_block *sb = dentry->d_sb; |
| 749 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 750 | struct wnd_bitmap *wnd = &sbi->used.bitmap; |
| 751 | CLST da_clusters = ntfs_get_da(sbi); |
| 752 | |
| 753 | buf->f_type = sb->s_magic; |
| 754 | buf->f_bsize = buf->f_frsize = sbi->cluster_size; |
| 755 | buf->f_blocks = wnd->nbits; |
| 756 | |
| 757 | buf->f_bfree = wnd_zeroes(wnd); |
| 758 | if (buf->f_bfree > da_clusters) { |
| 759 | buf->f_bfree -= da_clusters; |
| 760 | } else { |
| 761 | buf->f_bfree = 0; |
| 762 | } |
| 763 | buf->f_bavail = buf->f_bfree; |
| 764 | |
| 765 | buf->f_fsid.val[0] = sbi->volume.ser_num; |
| 766 | buf->f_fsid.val[1] = sbi->volume.ser_num >> 32; |
| 767 | buf->f_namelen = NTFS_NAME_LEN; |
| 768 | |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | static int ntfs_show_options(struct seq_file *m, struct dentry *root) |
| 773 | { |
| 774 | struct super_block *sb = root->d_sb; |
| 775 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 776 | struct ntfs_mount_options *opts = sbi->options; |
| 777 | struct user_namespace *user_ns = seq_user_ns(m); |
| 778 | |
| 779 | seq_printf(m, ",uid=%u", from_kuid_munged(user_ns, opts->fs_uid)); |
| 780 | seq_printf(m, ",gid=%u", from_kgid_munged(user_ns, opts->fs_gid)); |
| 781 | if (opts->dmask) |
| 782 | seq_printf(m, ",dmask=%04o", opts->fs_dmask_inv ^ 0xffff); |
| 783 | if (opts->fmask) |
| 784 | seq_printf(m, ",fmask=%04o", opts->fs_fmask_inv ^ 0xffff); |
| 785 | if (opts->sys_immutable) |
| 786 | seq_puts(m, ",sys_immutable"); |
| 787 | if (opts->discard) |
| 788 | seq_puts(m, ",discard"); |
| 789 | if (opts->force) |
| 790 | seq_puts(m, ",force"); |
| 791 | if (opts->sparse) |
| 792 | seq_puts(m, ",sparse"); |
| 793 | if (opts->nohidden) |
| 794 | seq_puts(m, ",nohidden"); |
| 795 | if (opts->hide_dot_files) |
| 796 | seq_puts(m, ",hide_dot_files"); |
| 797 | if (opts->windows_names) |
| 798 | seq_puts(m, ",windows_names"); |
| 799 | if (opts->showmeta) |
| 800 | seq_puts(m, ",showmeta"); |
| 801 | if (sb->s_flags & SB_POSIXACL) |
| 802 | seq_puts(m, ",acl"); |
| 803 | if (opts->nls) |
| 804 | seq_printf(m, ",iocharset=%s", opts->nls->charset); |
| 805 | else |
| 806 | seq_puts(m, ",iocharset=utf8"); |
| 807 | if (opts->prealloc) |
| 808 | seq_puts(m, ",prealloc"); |
| 809 | if (opts->nocase) |
| 810 | seq_puts(m, ",nocase"); |
| 811 | if (opts->delalloc) |
| 812 | seq_puts(m, ",delalloc"); |
| 813 | if (opts->ads) |
| 814 | seq_puts(m, ",ads"); |
| 815 | |
| 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | /* |
| 820 | * ntfs_shutdown - super_operations::shutdown |
| 821 | */ |
| 822 | static void ntfs_shutdown(struct super_block *sb) |
| 823 | { |
| 824 | set_bit(NTFS_FLAGS_SHUTDOWN_BIT, &ntfs_sb(sb)->flags); |
| 825 | } |
| 826 | |
| 827 | /* |
| 828 | * ntfs_sync_fs - super_operations::sync_fs |
| 829 | */ |
| 830 | static int ntfs_sync_fs(struct super_block *sb, int wait) |
| 831 | { |
| 832 | int err = 0, err2; |
| 833 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 834 | struct ntfs_inode *ni; |
| 835 | struct inode *inode; |
| 836 | |
| 837 | if (unlikely(ntfs3_forced_shutdown(sb))) |
| 838 | return -EIO; |
| 839 | |
| 840 | ni = sbi->security.ni; |
| 841 | if (ni) { |
| 842 | inode = &ni->vfs_inode; |
| 843 | err2 = _ni_write_inode(inode, wait); |
| 844 | if (err2 && !err) |
| 845 | err = err2; |
| 846 | } |
| 847 | |
| 848 | ni = sbi->objid.ni; |
| 849 | if (ni) { |
| 850 | inode = &ni->vfs_inode; |
| 851 | err2 = _ni_write_inode(inode, wait); |
| 852 | if (err2 && !err) |
| 853 | err = err2; |
| 854 | } |
| 855 | |
| 856 | ni = sbi->reparse.ni; |
| 857 | if (ni) { |
| 858 | inode = &ni->vfs_inode; |
| 859 | err2 = _ni_write_inode(inode, wait); |
| 860 | if (err2 && !err) |
| 861 | err = err2; |
| 862 | } |
| 863 | |
| 864 | if (!err) |
| 865 | ntfs_set_state(sbi, NTFS_DIRTY_CLEAR); |
| 866 | |
| 867 | ntfs_update_mftmirr(sbi); |
| 868 | |
| 869 | if (wait) { |
| 870 | sync_blockdev(sb->s_bdev); |
| 871 | blkdev_issue_flush(sb->s_bdev); |
| 872 | } |
| 873 | |
| 874 | return err; |
| 875 | } |
| 876 | |
| 877 | static const struct super_operations ntfs_sops = { |
| 878 | .alloc_inode = ntfs_alloc_inode, |
| 879 | .free_inode = ntfs_free_inode, |
| 880 | .evict_inode = ntfs_evict_inode, |
| 881 | .put_super = ntfs_put_super, |
| 882 | .statfs = ntfs_statfs, |
| 883 | .show_options = ntfs_show_options, |
| 884 | .shutdown = ntfs_shutdown, |
| 885 | .sync_fs = ntfs_sync_fs, |
| 886 | .write_inode = ntfs3_write_inode, |
| 887 | }; |
| 888 | |
| 889 | static struct inode *ntfs_export_get_inode(struct super_block *sb, u64 ino, |
| 890 | u32 generation) |
| 891 | { |
| 892 | struct MFT_REF ref; |
| 893 | struct inode *inode; |
| 894 | |
| 895 | ref.low = cpu_to_le32(ino); |
| 896 | #ifdef CONFIG_NTFS3_64BIT_CLUSTER |
| 897 | ref.high = cpu_to_le16(ino >> 32); |
| 898 | #else |
| 899 | ref.high = 0; |
| 900 | #endif |
| 901 | ref.seq = cpu_to_le16(generation); |
| 902 | |
| 903 | inode = ntfs_iget5(sb, &ref, NULL); |
| 904 | if (!IS_ERR(inode) && is_bad_inode(inode)) { |
| 905 | iput(inode); |
| 906 | inode = ERR_PTR(-ESTALE); |
| 907 | } |
| 908 | |
| 909 | return inode; |
| 910 | } |
| 911 | |
| 912 | static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid, |
| 913 | int fh_len, int fh_type) |
| 914 | { |
| 915 | return generic_fh_to_dentry(sb, fid, fh_len, fh_type, |
| 916 | ntfs_export_get_inode); |
| 917 | } |
| 918 | |
| 919 | static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid, |
| 920 | int fh_len, int fh_type) |
| 921 | { |
| 922 | return generic_fh_to_parent(sb, fid, fh_len, fh_type, |
| 923 | ntfs_export_get_inode); |
| 924 | } |
| 925 | |
| 926 | /* TODO: == ntfs_sync_inode */ |
| 927 | static int ntfs_nfs_commit_metadata(struct inode *inode) |
| 928 | { |
| 929 | return _ni_write_inode(inode, 1); |
| 930 | } |
| 931 | |
| 932 | static const struct export_operations ntfs_export_ops = { |
| 933 | .encode_fh = generic_encode_ino32_fh, |
| 934 | .fh_to_dentry = ntfs_fh_to_dentry, |
| 935 | .fh_to_parent = ntfs_fh_to_parent, |
| 936 | .get_parent = ntfs3_get_parent, |
| 937 | .commit_metadata = ntfs_nfs_commit_metadata, |
| 938 | }; |
| 939 | |
| 940 | /* |
| 941 | * format_size_gb - Return Gb,Mb to print with "%u.%02u Gb". |
| 942 | */ |
| 943 | static u32 format_size_gb(const u64 bytes, u32 *mb) |
| 944 | { |
| 945 | /* Do simple right 30 bit shift of 64 bit value. */ |
| 946 | u64 kbytes = bytes >> 10; |
| 947 | u32 kbytes32 = kbytes; |
| 948 | |
| 949 | *mb = (100 * (kbytes32 & 0xfffff) + 0x7ffff) >> 20; |
| 950 | if (*mb >= 100) |
| 951 | *mb = 99; |
| 952 | |
| 953 | return (kbytes32 >> 20) | (((u32)(kbytes >> 32)) << 12); |
| 954 | } |
| 955 | |
| 956 | static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot) |
| 957 | { |
| 958 | if (boot->sectors_per_clusters <= 0x80) |
| 959 | return boot->sectors_per_clusters; |
| 960 | if (boot->sectors_per_clusters >= 0xf4) /* limit shift to 2MB max */ |
| 961 | return 1U << (-(s8)boot->sectors_per_clusters); |
| 962 | return -EINVAL; |
| 963 | } |
| 964 | |
| 965 | /* |
| 966 | * ntfs_init_from_boot - Init internal info from on-disk boot sector. |
| 967 | * |
| 968 | * NTFS mount begins from boot - special formatted 512 bytes. |
| 969 | * There are two boots: the first and the last 512 bytes of volume. |
| 970 | * The content of boot is not changed during ntfs life. |
| 971 | * |
| 972 | * NOTE: ntfs.sys checks only first (primary) boot. |
| 973 | * chkdsk checks both boots. |
| 974 | */ |
| 975 | static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size, |
| 976 | u64 dev_size, struct NTFS_BOOT **boot2) |
| 977 | { |
| 978 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 979 | int err; |
| 980 | u32 mb, gb, boot_sector_size, sct_per_clst, record_size; |
| 981 | u64 sectors, clusters, mlcn, mlcn2, mft_pos, mft2_pos, dev_size0; |
| 982 | struct NTFS_BOOT *boot; |
| 983 | struct buffer_head *bh; |
| 984 | struct MFT_REC *rec; |
| 985 | u16 fn, ao; |
| 986 | u8 cluster_bits; |
| 987 | u32 boot_off = 0; |
| 988 | sector_t boot_block = 0; |
| 989 | const char *hint = "Primary boot"; |
| 990 | |
| 991 | /* Save original dev_size. Used with alternative boot. */ |
| 992 | dev_size0 = dev_size; |
| 993 | |
| 994 | sbi->volume.blocks = dev_size >> PAGE_SHIFT; |
| 995 | |
| 996 | /* Set dummy blocksize to read boot_block. */ |
| 997 | if (!sb_min_blocksize(sb, PAGE_SIZE)) { |
| 998 | return -EINVAL; |
| 999 | } |
| 1000 | |
| 1001 | read_boot: |
| 1002 | bh = ntfs_bread(sb, boot_block); |
| 1003 | if (!bh) |
| 1004 | return boot_block ? -EINVAL : -EIO; |
| 1005 | |
| 1006 | err = -EINVAL; |
| 1007 | |
| 1008 | /* Corrupted image; do not read OOB */ |
| 1009 | if (bh->b_size - sizeof(*boot) < boot_off) |
| 1010 | goto out; |
| 1011 | |
| 1012 | boot = (struct NTFS_BOOT *)Add2Ptr(bh->b_data, boot_off); |
| 1013 | |
| 1014 | if (memcmp(boot->system_id, "NTFS ", sizeof("NTFS ") - 1)) { |
| 1015 | ntfs_err(sb, "%s signature is not NTFS.", hint); |
| 1016 | goto out; |
| 1017 | } |
| 1018 | |
| 1019 | /* 0x55AA is not mandaroty. Thanks Maxim Suhanov*/ |
| 1020 | /*if (0x55 != boot->boot_magic[0] || 0xAA != boot->boot_magic[1]) |
| 1021 | * goto out; |
| 1022 | */ |
| 1023 | |
| 1024 | boot_sector_size = ((u32)boot->bytes_per_sector[1] << 8) | |
| 1025 | boot->bytes_per_sector[0]; |
| 1026 | if (boot_sector_size < SECTOR_SIZE || |
| 1027 | !is_power_of_2(boot_sector_size)) { |
| 1028 | ntfs_err(sb, "%s: invalid bytes per sector %u.", hint, |
| 1029 | boot_sector_size); |
| 1030 | goto out; |
| 1031 | } |
| 1032 | |
| 1033 | /* cluster size: 512, 1K, 2K, 4K, ... 2M */ |
| 1034 | sct_per_clst = true_sectors_per_clst(boot); |
| 1035 | if ((int)sct_per_clst < 0 || !is_power_of_2(sct_per_clst)) { |
| 1036 | ntfs_err(sb, "%s: invalid sectors per cluster %u.", hint, |
| 1037 | sct_per_clst); |
| 1038 | goto out; |
| 1039 | } |
| 1040 | |
| 1041 | sbi->cluster_size = boot_sector_size * sct_per_clst; |
| 1042 | sbi->cluster_bits = cluster_bits = blksize_bits(sbi->cluster_size); |
| 1043 | sbi->cluster_mask = sbi->cluster_size - 1; |
| 1044 | sbi->cluster_mask_inv = ~(u64)sbi->cluster_mask; |
| 1045 | |
| 1046 | mlcn = le64_to_cpu(boot->mft_clst); |
| 1047 | mlcn2 = le64_to_cpu(boot->mft2_clst); |
| 1048 | sectors = le64_to_cpu(boot->sectors_per_volume); |
| 1049 | |
| 1050 | /* |
| 1051 | * Convert mlcn/mlcn2 to sector positions before comparing with |
| 1052 | * 'sectors'. All three are u64 values that come from the boot |
| 1053 | * sector, so use check_mul_overflow() to keep a wraparound from |
| 1054 | * silently bypassing the comparison. |
| 1055 | */ |
| 1056 | if (check_mul_overflow(mlcn, (u64)sct_per_clst, &mft_pos) || |
| 1057 | check_mul_overflow(mlcn2, (u64)sct_per_clst, &mft2_pos) || |
| 1058 | mft_pos >= sectors || mft2_pos >= sectors) { |
| 1059 | ntfs_err( |
| 1060 | sb, |
| 1061 | "%s: start of MFT 0x%llx (0x%llx) is out of volume 0x%llx.", |
| 1062 | hint, mlcn, mlcn2, sectors); |
| 1063 | goto out; |
| 1064 | } |
| 1065 | |
| 1066 | if (boot->record_size >= 0) { |
| 1067 | record_size = (u32)boot->record_size << cluster_bits; |
| 1068 | } else if (-boot->record_size <= MAXIMUM_SHIFT_BYTES_PER_MFT) { |
| 1069 | record_size = 1u << (-boot->record_size); |
| 1070 | } else { |
| 1071 | ntfs_err(sb, "%s: invalid record size %d.", hint, |
| 1072 | boot->record_size); |
| 1073 | goto out; |
| 1074 | } |
| 1075 | |
| 1076 | sbi->record_size = record_size; |
| 1077 | sbi->record_bits = blksize_bits(record_size); |
| 1078 | sbi->attr_size_tr = (5 * record_size >> 4); // ~320 bytes |
| 1079 | |
| 1080 | /* Check MFT record size. */ |
| 1081 | if (record_size < SECTOR_SIZE || !is_power_of_2(record_size)) { |
| 1082 | ntfs_err(sb, "%s: invalid bytes per MFT record %u (%d).", hint, |
| 1083 | record_size, boot->record_size); |
| 1084 | goto out; |
| 1085 | } |
| 1086 | |
| 1087 | if (record_size > MAXIMUM_BYTES_PER_MFT) { |
| 1088 | ntfs_err(sb, "Unsupported bytes per MFT record %u.", |
| 1089 | record_size); |
| 1090 | goto out; |
| 1091 | } |
| 1092 | |
| 1093 | if (boot->index_size >= 0) { |
| 1094 | sbi->index_size = (u32)boot->index_size << cluster_bits; |
| 1095 | } else if (-boot->index_size <= MAXIMUM_SHIFT_BYTES_PER_INDEX) { |
| 1096 | sbi->index_size = 1u << (-boot->index_size); |
| 1097 | } else { |
| 1098 | ntfs_err(sb, "%s: invalid index size %d.", hint, |
| 1099 | boot->index_size); |
| 1100 | goto out; |
| 1101 | } |
| 1102 | |
| 1103 | /* Check index record size. */ |
| 1104 | if (sbi->index_size < SECTOR_SIZE || !is_power_of_2(sbi->index_size)) { |
| 1105 | ntfs_err(sb, "%s: invalid bytes per index %u(%d).", hint, |
| 1106 | sbi->index_size, boot->index_size); |
| 1107 | goto out; |
| 1108 | } |
| 1109 | |
| 1110 | if (sbi->index_size > MAXIMUM_BYTES_PER_INDEX) { |
| 1111 | ntfs_err(sb, "%s: unsupported bytes per index %u.", hint, |
| 1112 | sbi->index_size); |
| 1113 | goto out; |
| 1114 | } |
| 1115 | |
| 1116 | sbi->volume.size = sectors * boot_sector_size; |
| 1117 | |
| 1118 | gb = format_size_gb(sbi->volume.size + boot_sector_size, &mb); |
| 1119 | |
| 1120 | /* |
| 1121 | * - Volume formatted and mounted with the same sector size. |
| 1122 | * - Volume formatted 4K and mounted as 512. |
| 1123 | * - Volume formatted 512 and mounted as 4K. |
| 1124 | */ |
| 1125 | if (boot_sector_size != sector_size) { |
| 1126 | ntfs_warn( |
| 1127 | sb, |
| 1128 | "Different NTFS sector size (%u) and media sector size (%u).", |
| 1129 | boot_sector_size, sector_size); |
| 1130 | dev_size += sector_size - 1; |
| 1131 | } |
| 1132 | |
| 1133 | sbi->bdev_blocksize = max(boot_sector_size, sector_size); |
| 1134 | sbi->mft.lbo = mlcn << cluster_bits; |
| 1135 | sbi->mft.lbo2 = mlcn2 << cluster_bits; |
| 1136 | |
| 1137 | /* Compare boot's cluster and sector. */ |
| 1138 | if (sbi->cluster_size < boot_sector_size) { |
| 1139 | ntfs_err(sb, "%s: invalid bytes per cluster (%u).", hint, |
| 1140 | sbi->cluster_size); |
| 1141 | goto out; |
| 1142 | } |
| 1143 | |
| 1144 | /* Compare boot's cluster and media sector. */ |
| 1145 | if (sbi->cluster_size < sector_size) { |
| 1146 | /* No way to use ntfs_get_block in this case. */ |
| 1147 | ntfs_err( |
| 1148 | sb, |
| 1149 | "Failed to mount 'cause NTFS's cluster size (%u) is less than media sector size (%u).", |
| 1150 | sbi->cluster_size, sector_size); |
| 1151 | goto out; |
| 1152 | } |
| 1153 | |
| 1154 | sbi->max_bytes_per_attr = |
| 1155 | record_size - ALIGN(MFTRECORD_FIXUP_OFFSET, 8) - |
| 1156 | ALIGN(((record_size >> SECTOR_SHIFT) * sizeof(short)), 8) - |
| 1157 | ALIGN(sizeof(enum ATTR_TYPE), 8); |
| 1158 | |
| 1159 | sbi->volume.ser_num = le64_to_cpu(boot->serial_num); |
| 1160 | |
| 1161 | /* Warning if RAW volume. */ |
| 1162 | if (dev_size < sbi->volume.size + boot_sector_size) { |
| 1163 | u32 mb0, gb0; |
| 1164 | |
| 1165 | gb0 = format_size_gb(dev_size, &mb0); |
| 1166 | ntfs_warn( |
| 1167 | sb, |
| 1168 | "RAW NTFS volume: Filesystem size %u.%02u Gb > volume size %u.%02u Gb. Mount in read-only.", |
| 1169 | gb, mb, gb0, mb0); |
| 1170 | sb->s_flags |= SB_RDONLY; |
| 1171 | } |
| 1172 | |
| 1173 | clusters = sbi->volume.size >> cluster_bits; |
| 1174 | #ifndef CONFIG_NTFS3_64BIT_CLUSTER |
| 1175 | /* 32 bits per cluster. */ |
| 1176 | if (clusters >> 32) { |
| 1177 | ntfs_notice( |
| 1178 | sb, |
| 1179 | "NTFS %u.%02u Gb is too big to use 32 bits per cluster.", |
| 1180 | gb, mb); |
| 1181 | goto out; |
| 1182 | } |
| 1183 | #elif BITS_PER_LONG < 64 |
| 1184 | #error "CONFIG_NTFS3_64BIT_CLUSTER incompatible in 32 bit OS" |
| 1185 | #endif |
| 1186 | |
| 1187 | sbi->used.bitmap.nbits = clusters; |
| 1188 | |
| 1189 | rec = kzalloc(record_size, GFP_NOFS); |
| 1190 | if (!rec) { |
| 1191 | err = -ENOMEM; |
| 1192 | goto out; |
| 1193 | } |
| 1194 | |
| 1195 | sbi->new_rec = rec; |
| 1196 | rec->rhdr.sign = NTFS_FILE_SIGNATURE; |
| 1197 | rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET); |
| 1198 | fn = (sbi->record_size >> SECTOR_SHIFT) + 1; |
| 1199 | rec->rhdr.fix_num = cpu_to_le16(fn); |
| 1200 | ao = ALIGN(MFTRECORD_FIXUP_OFFSET + sizeof(short) * fn, 8); |
| 1201 | rec->attr_off = cpu_to_le16(ao); |
| 1202 | rec->used = cpu_to_le32(ao + ALIGN(sizeof(enum ATTR_TYPE), 8)); |
| 1203 | rec->total = cpu_to_le32(sbi->record_size); |
| 1204 | ((struct ATTRIB *)Add2Ptr(rec, ao))->type = ATTR_END; |
| 1205 | |
| 1206 | if (!sb_set_blocksize(sb, min_t(u32, sbi->cluster_size, PAGE_SIZE))) { |
| 1207 | err = -EINVAL; |
| 1208 | goto out; |
| 1209 | } |
| 1210 | |
| 1211 | sbi->block_mask = sb->s_blocksize - 1; |
| 1212 | sbi->blocks_per_cluster = sbi->cluster_size >> sb->s_blocksize_bits; |
| 1213 | sbi->volume.blocks = sbi->volume.size >> sb->s_blocksize_bits; |
| 1214 | |
| 1215 | /* Maximum size for normal files. */ |
| 1216 | sbi->maxbytes = (clusters << cluster_bits) - 1; |
| 1217 | |
| 1218 | #ifdef CONFIG_NTFS3_64BIT_CLUSTER |
| 1219 | if (clusters >= (1ull << (64 - cluster_bits))) |
| 1220 | sbi->maxbytes = -1; |
| 1221 | sbi->maxbytes_sparse = MAX_LFS_FILESIZE; |
| 1222 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
| 1223 | #else |
| 1224 | /* Maximum size for sparse file. */ |
| 1225 | sbi->maxbytes_sparse = (1ull << (cluster_bits + 32)) - 1; |
| 1226 | sb->s_maxbytes = 0xFFFFFFFFull << cluster_bits; |
| 1227 | #endif |
| 1228 | |
| 1229 | /* |
| 1230 | * Compute the MFT zone at two steps. |
| 1231 | * It would be nice if we are able to allocate 1/8 of |
| 1232 | * total clusters for MFT but not more then 512 MB. |
| 1233 | */ |
| 1234 | sbi->zone_max = min_t(CLST, 0x20000000 >> cluster_bits, clusters >> 3); |
| 1235 | |
| 1236 | err = 0; |
| 1237 | |
| 1238 | if (bh->b_blocknr && !sb_rdonly(sb)) { |
| 1239 | /* |
| 1240 | * Alternative boot is ok but primary is not ok. |
| 1241 | * Do not update primary boot here 'cause it may be faked boot. |
| 1242 | * Let ntfs to be mounted and update boot later. |
| 1243 | */ |
| 1244 | *boot2 = kmemdup(boot, sizeof(*boot), GFP_NOFS | __GFP_NOWARN); |
| 1245 | } |
| 1246 | |
| 1247 | out: |
| 1248 | brelse(bh); |
| 1249 | |
| 1250 | if (err == -EINVAL && !boot_block && dev_size0 > PAGE_SHIFT) { |
| 1251 | u32 block_size = min_t(u32, sector_size, PAGE_SIZE); |
| 1252 | u64 lbo = dev_size0 - sizeof(*boot); |
| 1253 | |
| 1254 | boot_block = lbo >> blksize_bits(block_size); |
| 1255 | boot_off = lbo & (block_size - 1); |
| 1256 | if (boot_block && block_size >= boot_off + sizeof(*boot)) { |
| 1257 | /* |
| 1258 | * Try alternative boot (last sector) |
| 1259 | */ |
| 1260 | if (!sb_set_blocksize(sb, block_size)) |
| 1261 | return -EINVAL; |
| 1262 | hint = "Alternative boot"; |
| 1263 | dev_size = dev_size0; /* restore original size. */ |
| 1264 | goto read_boot; |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | return err; |
| 1269 | } |
| 1270 | |
| 1271 | /* |
| 1272 | * ntfs_fill_super - Try to mount. |
| 1273 | */ |
| 1274 | static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc) |
| 1275 | { |
| 1276 | int err; |
| 1277 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 1278 | struct block_device *bdev = sb->s_bdev; |
| 1279 | struct ntfs_mount_options *fc_opts; |
| 1280 | struct ntfs_mount_options *options = NULL; |
| 1281 | struct inode *inode; |
| 1282 | struct ntfs_inode *ni; |
| 1283 | size_t i, tt, bad_len, bad_frags; |
| 1284 | CLST vcn, lcn, len; |
| 1285 | struct ATTRIB *attr; |
| 1286 | const struct VOLUME_INFO *info; |
| 1287 | u32 done, bytes; |
| 1288 | struct ATTR_DEF_ENTRY *t; |
| 1289 | u16 *shared; |
| 1290 | struct MFT_REF ref; |
| 1291 | bool ro = sb_rdonly(sb); |
| 1292 | struct NTFS_BOOT *boot2 = NULL; |
| 1293 | |
| 1294 | ref.high = 0; |
| 1295 | |
| 1296 | sbi->sb = sb; |
| 1297 | fc_opts = fc->fs_private; |
| 1298 | if (!fc_opts) { |
| 1299 | errorf(fc, "missing mount options"); |
| 1300 | return -EINVAL; |
| 1301 | } |
| 1302 | options = kmemdup(fc_opts, sizeof(*fc_opts), GFP_KERNEL); |
| 1303 | if (!options) |
| 1304 | return -ENOMEM; |
| 1305 | |
| 1306 | if (fc_opts->nls_name) { |
| 1307 | options->nls_name = kstrdup(fc_opts->nls_name, GFP_KERNEL); |
| 1308 | if (!options->nls_name) { |
| 1309 | kfree(options); |
| 1310 | return -ENOMEM; |
| 1311 | } |
| 1312 | } |
| 1313 | sbi->options = options; |
| 1314 | sb->s_flags |= SB_NODIRATIME; |
| 1315 | sb->s_magic = 0x7366746e; // "ntfs" |
| 1316 | sb->s_op = &ntfs_sops; |
| 1317 | sb->s_export_op = &ntfs_export_ops; |
| 1318 | sb->s_time_gran = NTFS_TIME_GRAN; // 100 nsec |
| 1319 | sb->s_xattr = ntfs_xattr_handlers; |
| 1320 | set_default_d_op(sb, options->nocase ? &ntfs_dentry_ops : NULL); |
| 1321 | |
| 1322 | options->nls = ntfs_load_nls(options->nls_name); |
| 1323 | if (IS_ERR(options->nls)) { |
| 1324 | options->nls = NULL; |
| 1325 | errorf(fc, "Cannot load nls %s", options->nls_name); |
| 1326 | err = -EINVAL; |
| 1327 | goto out; |
| 1328 | } |
| 1329 | |
| 1330 | if (bdev_max_discard_sectors(bdev) && bdev_discard_granularity(bdev)) { |
| 1331 | sbi->discard_granularity = bdev_discard_granularity(bdev); |
| 1332 | sbi->discard_granularity_mask_inv = |
| 1333 | ~(u64)(sbi->discard_granularity - 1); |
| 1334 | } |
| 1335 | |
| 1336 | /* Parse boot. */ |
| 1337 | err = ntfs_init_from_boot(sb, bdev_logical_block_size(bdev), |
| 1338 | bdev_nr_bytes(bdev), &boot2); |
| 1339 | if (err) |
| 1340 | goto out; |
| 1341 | |
| 1342 | /* |
| 1343 | * Load $Volume. This should be done before $LogFile |
| 1344 | * 'cause 'sbi->volume.ni' is used in 'ntfs_set_state'. |
| 1345 | */ |
| 1346 | ref.low = cpu_to_le32(MFT_REC_VOL); |
| 1347 | ref.seq = cpu_to_le16(MFT_REC_VOL); |
| 1348 | inode = ntfs_iget5(sb, &ref, &NAME_VOLUME); |
| 1349 | if (IS_ERR(inode)) { |
| 1350 | err = PTR_ERR(inode); |
| 1351 | ntfs_err(sb, "Failed to load $Volume (%d).", err); |
| 1352 | goto out; |
| 1353 | } |
| 1354 | |
| 1355 | ni = ntfs_i(inode); |
| 1356 | |
| 1357 | /* Load and save label (not necessary). */ |
| 1358 | attr = ni_find_attr(ni, NULL, NULL, ATTR_LABEL, NULL, 0, NULL, NULL); |
| 1359 | |
| 1360 | if (!attr) { |
| 1361 | /* It is ok if no ATTR_LABEL */ |
| 1362 | } else if (!attr->non_res && !is_attr_ext(attr)) { |
| 1363 | /* $AttrDef allows labels to be up to 128 symbols. */ |
| 1364 | err = utf16s_to_utf8s(resident_data(attr), |
| 1365 | le32_to_cpu(attr->res.data_size) >> 1, |
| 1366 | UTF16_LITTLE_ENDIAN, sbi->volume.label, |
| 1367 | sizeof(sbi->volume.label)); |
| 1368 | if (err < 0) { |
| 1369 | sbi->volume.label[0] = 0; |
| 1370 | } else if (err >= sizeof(sbi->volume.label)) { |
| 1371 | sbi->volume.label[sizeof(sbi->volume.label) - 1] = 0; |
| 1372 | } else { |
| 1373 | sbi->volume.label[err] = 0; |
| 1374 | } |
| 1375 | } else { |
| 1376 | /* Should we break mounting here? */ |
| 1377 | //err = -EINVAL; |
| 1378 | //goto put_inode_out; |
| 1379 | } |
| 1380 | |
| 1381 | attr = ni_find_attr(ni, attr, NULL, ATTR_VOL_INFO, NULL, 0, NULL, NULL); |
| 1382 | if (!attr || is_attr_ext(attr) || |
| 1383 | !(info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO))) { |
| 1384 | ntfs_err(sb, "$Volume is corrupted."); |
| 1385 | err = -EINVAL; |
| 1386 | goto put_inode_out; |
| 1387 | } |
| 1388 | |
| 1389 | sbi->volume.major_ver = info->major_ver; |
| 1390 | sbi->volume.minor_ver = info->minor_ver; |
| 1391 | sbi->volume.flags = info->flags; |
| 1392 | sbi->volume.ni = ni; |
| 1393 | if (info->flags & VOLUME_FLAG_DIRTY) { |
| 1394 | sbi->volume.real_dirty = true; |
| 1395 | ntfs_info(sb, "It is recommended to use chkdsk."); |
| 1396 | } |
| 1397 | |
| 1398 | /* Load $MFTMirr to estimate recs_mirr. */ |
| 1399 | ref.low = cpu_to_le32(MFT_REC_MIRR); |
| 1400 | ref.seq = cpu_to_le16(MFT_REC_MIRR); |
| 1401 | inode = ntfs_iget5(sb, &ref, &NAME_MIRROR); |
| 1402 | if (IS_ERR(inode)) { |
| 1403 | err = PTR_ERR(inode); |
| 1404 | ntfs_err(sb, "Failed to load $MFTMirr (%d).", err); |
| 1405 | goto out; |
| 1406 | } |
| 1407 | |
| 1408 | sbi->mft.recs_mirr = ntfs_up_cluster(sbi, inode->i_size) >> |
| 1409 | sbi->record_bits; |
| 1410 | |
| 1411 | iput(inode); |
| 1412 | |
| 1413 | /* Load LogFile to replay. */ |
| 1414 | ref.low = cpu_to_le32(MFT_REC_LOG); |
| 1415 | ref.seq = cpu_to_le16(MFT_REC_LOG); |
| 1416 | inode = ntfs_iget5(sb, &ref, &NAME_LOGFILE); |
| 1417 | if (IS_ERR(inode)) { |
| 1418 | err = PTR_ERR(inode); |
| 1419 | ntfs_err(sb, "Failed to load \x24LogFile (%d).", err); |
| 1420 | goto out; |
| 1421 | } |
| 1422 | |
| 1423 | ni = ntfs_i(inode); |
| 1424 | |
| 1425 | err = ntfs_loadlog_and_replay(ni, sbi); |
| 1426 | if (err) |
| 1427 | goto put_inode_out; |
| 1428 | |
| 1429 | iput(inode); |
| 1430 | |
| 1431 | if ((sbi->flags & NTFS_FLAGS_NEED_REPLAY) && !ro) { |
| 1432 | ntfs_warn(sb, "failed to replay log file. Can't mount rw!"); |
| 1433 | err = -EINVAL; |
| 1434 | goto out; |
| 1435 | } |
| 1436 | |
| 1437 | if ((sbi->volume.flags & VOLUME_FLAG_DIRTY) && !ro && !options->force) { |
| 1438 | ntfs_warn(sb, "volume is dirty and \"force\" flag is not set!"); |
| 1439 | err = -EINVAL; |
| 1440 | goto out; |
| 1441 | } |
| 1442 | |
| 1443 | /* Load $MFT. */ |
| 1444 | ref.low = cpu_to_le32(MFT_REC_MFT); |
| 1445 | ref.seq = cpu_to_le16(1); |
| 1446 | |
| 1447 | inode = ntfs_iget5(sb, &ref, &NAME_MFT); |
| 1448 | if (IS_ERR(inode)) { |
| 1449 | err = PTR_ERR(inode); |
| 1450 | ntfs_err(sb, "Failed to load $MFT (%d).", err); |
| 1451 | goto out; |
| 1452 | } |
| 1453 | |
| 1454 | ni = ntfs_i(inode); |
| 1455 | |
| 1456 | sbi->mft.used = ni->i_valid >> sbi->record_bits; |
| 1457 | tt = inode->i_size >> sbi->record_bits; |
| 1458 | sbi->mft.next_free = MFT_REC_USER; |
| 1459 | |
| 1460 | err = ni_load_all_mi(ni); |
| 1461 | if (err) { |
| 1462 | ntfs_err(sb, "Failed to load $MFT's subrecords (%d).", err); |
| 1463 | goto put_inode_out; |
| 1464 | } |
| 1465 | |
| 1466 | /* Merge MFT bitmap runs from extent records loaded by ni_load_all_mi. */ |
| 1467 | { |
| 1468 | struct ATTRIB *a = NULL; |
| 1469 | struct ATTR_LIST_ENTRY *le = NULL; |
| 1470 | |
| 1471 | while ((a = ni_enum_attr_ex(ni, a, &le, NULL))) { |
| 1472 | CLST svcn, evcn; |
| 1473 | u16 roff; |
| 1474 | |
| 1475 | if (a->type != ATTR_BITMAP || !a->non_res) |
| 1476 | continue; |
| 1477 | |
| 1478 | svcn = le64_to_cpu(a->nres.svcn); |
| 1479 | if (!svcn) |
| 1480 | continue; /* Base record runs already loaded. */ |
| 1481 | |
| 1482 | evcn = le64_to_cpu(a->nres.evcn); |
| 1483 | roff = le16_to_cpu(a->nres.run_off); |
| 1484 | |
| 1485 | err = run_unpack_ex(&sbi->mft.bitmap.run, sbi, |
| 1486 | MFT_REC_MFT, svcn, evcn, svcn, |
| 1487 | Add2Ptr(a, roff), |
| 1488 | le32_to_cpu(a->size) - roff); |
| 1489 | if (err < 0) { |
| 1490 | ntfs_err( |
| 1491 | sb, |
| 1492 | "Failed to unpack $MFT bitmap extent (%d).", |
| 1493 | err); |
| 1494 | goto put_inode_out; |
| 1495 | } |
| 1496 | err = 0; |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | err = wnd_init(&sbi->mft.bitmap, sb, tt); |
| 1501 | if (err) |
| 1502 | goto put_inode_out; |
| 1503 | |
| 1504 | sbi->mft.ni = ni; |
| 1505 | |
| 1506 | /* Load $Bitmap. */ |
| 1507 | ref.low = cpu_to_le32(MFT_REC_BITMAP); |
| 1508 | ref.seq = cpu_to_le16(MFT_REC_BITMAP); |
| 1509 | inode = ntfs_iget5(sb, &ref, &NAME_BITMAP); |
| 1510 | if (IS_ERR(inode)) { |
| 1511 | err = PTR_ERR(inode); |
| 1512 | ntfs_err(sb, "Failed to load $Bitmap (%d).", err); |
| 1513 | goto out; |
| 1514 | } |
| 1515 | |
| 1516 | #ifndef CONFIG_NTFS3_64BIT_CLUSTER |
| 1517 | if (inode->i_size >> 32) { |
| 1518 | err = -EINVAL; |
| 1519 | goto put_inode_out; |
| 1520 | } |
| 1521 | #endif |
| 1522 | |
| 1523 | /* Check bitmap boundary. */ |
| 1524 | tt = sbi->used.bitmap.nbits; |
| 1525 | if (inode->i_size < ntfs3_bitmap_size(tt)) { |
| 1526 | ntfs_err(sb, "$Bitmap is corrupted."); |
| 1527 | err = -EINVAL; |
| 1528 | goto put_inode_out; |
| 1529 | } |
| 1530 | |
| 1531 | err = wnd_init(&sbi->used.bitmap, sb, tt); |
| 1532 | if (err) { |
| 1533 | ntfs_err(sb, "Failed to initialize $Bitmap (%d).", err); |
| 1534 | goto put_inode_out; |
| 1535 | } |
| 1536 | |
| 1537 | iput(inode); |
| 1538 | |
| 1539 | /* Compute the MFT zone. */ |
| 1540 | err = ntfs_refresh_zone(sbi); |
| 1541 | if (err) { |
| 1542 | ntfs_err(sb, "Failed to initialize MFT zone (%d).", err); |
| 1543 | goto out; |
| 1544 | } |
| 1545 | |
| 1546 | /* Load $BadClus. */ |
| 1547 | ref.low = cpu_to_le32(MFT_REC_BADCLUST); |
| 1548 | ref.seq = cpu_to_le16(MFT_REC_BADCLUST); |
| 1549 | inode = ntfs_iget5(sb, &ref, &NAME_BADCLUS); |
| 1550 | if (IS_ERR(inode)) { |
| 1551 | err = PTR_ERR(inode); |
| 1552 | ntfs_err(sb, "Failed to load $BadClus (%d).", err); |
| 1553 | goto out; |
| 1554 | } |
| 1555 | |
| 1556 | ni = ntfs_i(inode); |
| 1557 | bad_len = bad_frags = 0; |
| 1558 | for (i = 0; run_get_entry(&ni->file.run, i, &vcn, &lcn, &len); i++) { |
| 1559 | if (lcn == SPARSE_LCN) |
| 1560 | continue; |
| 1561 | |
| 1562 | bad_len += len; |
| 1563 | bad_frags += 1; |
| 1564 | if (ro) |
| 1565 | continue; |
| 1566 | |
| 1567 | if (wnd_set_used_safe(&sbi->used.bitmap, lcn, len, &tt) || tt) { |
| 1568 | /* Bad blocks marked as free in bitmap. */ |
| 1569 | ntfs_set_state(sbi, NTFS_DIRTY_ERROR); |
| 1570 | } |
| 1571 | } |
| 1572 | if (bad_len) { |
| 1573 | /* |
| 1574 | * Notice about bad blocks. |
| 1575 | * In normal cases these blocks are marked as used in bitmap. |
| 1576 | * And we never allocate space in it. |
| 1577 | */ |
| 1578 | ntfs_notice(sb, |
| 1579 | "Volume contains %zu bad blocks in %zu fragments.", |
| 1580 | bad_len, bad_frags); |
| 1581 | } |
| 1582 | iput(inode); |
| 1583 | |
| 1584 | /* Load $AttrDef. */ |
| 1585 | ref.low = cpu_to_le32(MFT_REC_ATTR); |
| 1586 | ref.seq = cpu_to_le16(MFT_REC_ATTR); |
| 1587 | inode = ntfs_iget5(sb, &ref, &NAME_ATTRDEF); |
| 1588 | if (IS_ERR(inode)) { |
| 1589 | err = PTR_ERR(inode); |
| 1590 | ntfs_err(sb, "Failed to load $AttrDef (%d)", err); |
| 1591 | goto out; |
| 1592 | } |
| 1593 | |
| 1594 | /* |
| 1595 | * Typical $AttrDef contains up to 20 entries. |
| 1596 | * Check for extremely large/small size. |
| 1597 | */ |
| 1598 | if (inode->i_size < sizeof(struct ATTR_DEF_ENTRY) || |
| 1599 | inode->i_size > 100 * sizeof(struct ATTR_DEF_ENTRY)) { |
| 1600 | ntfs_err(sb, "Looks like $AttrDef is corrupted (size=%llu).", |
| 1601 | inode->i_size); |
| 1602 | err = -EINVAL; |
| 1603 | goto put_inode_out; |
| 1604 | } |
| 1605 | |
| 1606 | bytes = inode->i_size; |
| 1607 | sbi->def_table = t = kvmalloc(bytes, GFP_KERNEL); |
| 1608 | if (!t) { |
| 1609 | err = -ENOMEM; |
| 1610 | goto put_inode_out; |
| 1611 | } |
| 1612 | |
| 1613 | /* Read the entire file. */ |
| 1614 | err = inode_read_data(inode, sbi->def_table, bytes); |
| 1615 | if (err) { |
| 1616 | ntfs_err(sb, "Failed to read $AttrDef (%d).", err); |
| 1617 | goto put_inode_out; |
| 1618 | } |
| 1619 | |
| 1620 | if (ATTR_STD != t->type) { |
| 1621 | ntfs_err(sb, "$AttrDef is corrupted."); |
| 1622 | err = -EINVAL; |
| 1623 | goto put_inode_out; |
| 1624 | } |
| 1625 | |
| 1626 | t += 1; |
| 1627 | sbi->def_entries = 1; |
| 1628 | done = sizeof(struct ATTR_DEF_ENTRY); |
| 1629 | |
| 1630 | while (done + sizeof(struct ATTR_DEF_ENTRY) <= bytes) { |
| 1631 | u32 t32 = le32_to_cpu(t->type); |
| 1632 | u64 sz = le64_to_cpu(t->max_sz); |
| 1633 | |
| 1634 | if ((t32 & 0xF) || le32_to_cpu(t[-1].type) >= t32) |
| 1635 | break; |
| 1636 | |
| 1637 | if (t->type == ATTR_REPARSE) |
| 1638 | sbi->reparse.max_size = sz; |
| 1639 | else if (t->type == ATTR_EA) |
| 1640 | sbi->ea_max_size = sz; |
| 1641 | |
| 1642 | done += sizeof(struct ATTR_DEF_ENTRY); |
| 1643 | t += 1; |
| 1644 | sbi->def_entries += 1; |
| 1645 | } |
| 1646 | iput(inode); |
| 1647 | |
| 1648 | /* Load $UpCase. */ |
| 1649 | ref.low = cpu_to_le32(MFT_REC_UPCASE); |
| 1650 | ref.seq = cpu_to_le16(MFT_REC_UPCASE); |
| 1651 | inode = ntfs_iget5(sb, &ref, &NAME_UPCASE); |
| 1652 | if (IS_ERR(inode)) { |
| 1653 | err = PTR_ERR(inode); |
| 1654 | ntfs_err(sb, "Failed to load $UpCase (%d).", err); |
| 1655 | goto out; |
| 1656 | } |
| 1657 | |
| 1658 | if (inode->i_size != 0x10000 * sizeof(short)) { |
| 1659 | err = -EINVAL; |
| 1660 | ntfs_err(sb, "$UpCase is corrupted."); |
| 1661 | goto put_inode_out; |
| 1662 | } |
| 1663 | |
| 1664 | /* Read the entire file. */ |
| 1665 | err = inode_read_data(inode, sbi->upcase, 0x10000 * sizeof(short)); |
| 1666 | if (err) { |
| 1667 | ntfs_err(sb, "Failed to read $UpCase (%d).", err); |
| 1668 | goto put_inode_out; |
| 1669 | } |
| 1670 | |
| 1671 | #ifdef __BIG_ENDIAN |
| 1672 | { |
| 1673 | u16 *dst = sbi->upcase; |
| 1674 | |
| 1675 | for (i = 0; i < 0x10000; i++) |
| 1676 | __swab16s(dst++); |
| 1677 | } |
| 1678 | #endif |
| 1679 | |
| 1680 | shared = ntfs_set_shared(sbi->upcase, 0x10000 * sizeof(short)); |
| 1681 | if (shared && sbi->upcase != shared) { |
| 1682 | kvfree(sbi->upcase); |
| 1683 | sbi->upcase = shared; |
| 1684 | } |
| 1685 | |
| 1686 | iput(inode); |
| 1687 | |
| 1688 | if (is_ntfs3(sbi)) { |
| 1689 | /* Load $Secure. */ |
| 1690 | err = ntfs_security_init(sbi); |
| 1691 | if (err) { |
| 1692 | ntfs_err(sb, "Failed to initialize $Secure (%d).", err); |
| 1693 | goto out; |
| 1694 | } |
| 1695 | |
| 1696 | /* Load $Extend. */ |
| 1697 | err = ntfs_extend_init(sbi); |
| 1698 | if (err) { |
| 1699 | ntfs_warn(sb, "Failed to initialize $Extend."); |
| 1700 | goto load_root; |
| 1701 | } |
| 1702 | |
| 1703 | /* Load $Extend/$Reparse. */ |
| 1704 | err = ntfs_reparse_init(sbi); |
| 1705 | if (err) { |
| 1706 | ntfs_warn(sb, "Failed to initialize $Extend/$Reparse."); |
| 1707 | goto load_root; |
| 1708 | } |
| 1709 | |
| 1710 | /* Load $Extend/$ObjId. */ |
| 1711 | err = ntfs_objid_init(sbi); |
| 1712 | if (err) { |
| 1713 | ntfs_warn(sb, "Failed to initialize $Extend/$ObjId."); |
| 1714 | goto load_root; |
| 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | load_root: |
| 1719 | /* Load root. */ |
| 1720 | ref.low = cpu_to_le32(MFT_REC_ROOT); |
| 1721 | ref.seq = cpu_to_le16(MFT_REC_ROOT); |
| 1722 | inode = ntfs_iget5(sb, &ref, &NAME_ROOT); |
| 1723 | if (IS_ERR(inode)) { |
| 1724 | err = PTR_ERR(inode); |
| 1725 | ntfs_err(sb, "Failed to load root (%d).", err); |
| 1726 | goto out; |
| 1727 | } |
| 1728 | |
| 1729 | /* |
| 1730 | * Final check. Looks like this case should never occurs. |
| 1731 | */ |
| 1732 | if (!inode->i_op) { |
| 1733 | err = -EINVAL; |
| 1734 | ntfs_err(sb, "Failed to load root (%d).", err); |
| 1735 | goto put_inode_out; |
| 1736 | } |
| 1737 | |
| 1738 | sb->s_root = d_make_root(inode); |
| 1739 | if (!sb->s_root) { |
| 1740 | err = -ENOMEM; |
| 1741 | goto out; |
| 1742 | } |
| 1743 | |
| 1744 | if (boot2) { |
| 1745 | /* |
| 1746 | * Alternative boot is ok but primary is not ok. |
| 1747 | * Volume is recognized as NTFS. Update primary boot. |
| 1748 | */ |
| 1749 | struct buffer_head *bh0 = sb_getblk(sb, 0); |
| 1750 | if (bh0) { |
| 1751 | wait_on_buffer(bh0); |
| 1752 | lock_buffer(bh0); |
| 1753 | memcpy(bh0->b_data, boot2, sizeof(*boot2)); |
| 1754 | set_buffer_uptodate(bh0); |
| 1755 | mark_buffer_dirty(bh0); |
| 1756 | unlock_buffer(bh0); |
| 1757 | if (!sync_dirty_buffer(bh0)) |
| 1758 | ntfs_warn(sb, "primary boot is updated"); |
| 1759 | put_bh(bh0); |
| 1760 | } |
| 1761 | |
| 1762 | kfree(boot2); |
| 1763 | } |
| 1764 | |
| 1765 | ntfs_create_procdir(sb); |
| 1766 | |
| 1767 | return 0; |
| 1768 | |
| 1769 | put_inode_out: |
| 1770 | iput(inode); |
| 1771 | out: |
| 1772 | /* sbi->options == options */ |
| 1773 | if (options) { |
| 1774 | put_mount_options(sbi->options); |
| 1775 | sbi->options = NULL; |
| 1776 | } |
| 1777 | |
| 1778 | ntfs3_put_sbi(sbi); |
| 1779 | kfree(boot2); |
| 1780 | return err; |
| 1781 | } |
| 1782 | |
| 1783 | void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len) |
| 1784 | { |
| 1785 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 1786 | struct block_device *bdev = sb->s_bdev; |
| 1787 | sector_t devblock = (u64)lcn * sbi->blocks_per_cluster; |
| 1788 | unsigned long blocks = (u64)len * sbi->blocks_per_cluster; |
| 1789 | unsigned long cnt = 0; |
| 1790 | unsigned long limit = global_zone_page_state(NR_FREE_PAGES) |
| 1791 | << (PAGE_SHIFT - sb->s_blocksize_bits); |
| 1792 | |
| 1793 | if (limit >= 0x2000) |
| 1794 | limit -= 0x1000; |
| 1795 | else if (limit < 32) |
| 1796 | limit = 32; |
| 1797 | else |
| 1798 | limit >>= 1; |
| 1799 | |
| 1800 | while (blocks--) { |
| 1801 | clean_bdev_aliases(bdev, devblock++, 1); |
| 1802 | if (cnt++ >= limit) { |
| 1803 | sync_blockdev(bdev); |
| 1804 | cnt = 0; |
| 1805 | } |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | /* |
| 1810 | * ntfs_discard - Issue a discard request (trim for SSD). |
| 1811 | */ |
| 1812 | int ntfs_discard(struct ntfs_sb_info *sbi, CLST lcn, CLST len) |
| 1813 | { |
| 1814 | int err; |
| 1815 | u64 lbo, bytes, start, end; |
| 1816 | struct super_block *sb; |
| 1817 | |
| 1818 | if (sbi->used.next_free_lcn == lcn + len) |
| 1819 | sbi->used.next_free_lcn = lcn; |
| 1820 | |
| 1821 | if (sbi->flags & NTFS_FLAGS_NODISCARD) |
| 1822 | return -EOPNOTSUPP; |
| 1823 | |
| 1824 | if (!sbi->options->discard) |
| 1825 | return -EOPNOTSUPP; |
| 1826 | |
| 1827 | lbo = (u64)lcn << sbi->cluster_bits; |
| 1828 | bytes = (u64)len << sbi->cluster_bits; |
| 1829 | |
| 1830 | /* Align up 'start' on discard_granularity. */ |
| 1831 | start = (lbo + sbi->discard_granularity - 1) & |
| 1832 | sbi->discard_granularity_mask_inv; |
| 1833 | /* Align down 'end' on discard_granularity. */ |
| 1834 | end = (lbo + bytes) & sbi->discard_granularity_mask_inv; |
| 1835 | |
| 1836 | sb = sbi->sb; |
| 1837 | if (start >= end) |
| 1838 | return 0; |
| 1839 | |
| 1840 | err = blkdev_issue_discard(sb->s_bdev, start >> 9, (end - start) >> 9, |
| 1841 | GFP_NOFS); |
| 1842 | |
| 1843 | if (err == -EOPNOTSUPP) |
| 1844 | sbi->flags |= NTFS_FLAGS_NODISCARD; |
| 1845 | |
| 1846 | return err; |
| 1847 | } |
| 1848 | |
| 1849 | static int ntfs_fs_get_tree(struct fs_context *fc) |
| 1850 | { |
| 1851 | return get_tree_bdev(fc, ntfs_fill_super); |
| 1852 | } |
| 1853 | |
| 1854 | /* |
| 1855 | * ntfs_fs_free - Free fs_context. |
| 1856 | * |
| 1857 | * Note that this will be called after fill_super and reconfigure |
| 1858 | * even when they pass. So they have to take pointers if they pass. |
| 1859 | */ |
| 1860 | static void ntfs_fs_free(struct fs_context *fc) |
| 1861 | { |
| 1862 | struct ntfs_mount_options *opts = fc->fs_private; |
| 1863 | struct ntfs_sb_info *sbi = fc->s_fs_info; |
| 1864 | |
| 1865 | if (sbi) { |
| 1866 | ntfs3_put_sbi(sbi); |
| 1867 | ntfs3_free_sbi(sbi); |
| 1868 | } |
| 1869 | |
| 1870 | if (opts) |
| 1871 | put_mount_options(opts); |
| 1872 | } |
| 1873 | |
| 1874 | // clang-format off |
| 1875 | static const struct fs_context_operations ntfs_context_ops = { |
| 1876 | .parse_param = ntfs_fs_parse_param, |
| 1877 | .get_tree = ntfs_fs_get_tree, |
| 1878 | .reconfigure = ntfs_fs_reconfigure, |
| 1879 | .free = ntfs_fs_free, |
| 1880 | }; |
| 1881 | // clang-format on |
| 1882 | |
| 1883 | /* |
| 1884 | * ntfs_init_fs_context - Initialize sbi and opts |
| 1885 | * |
| 1886 | * This will called when mount/remount. We will first initialize |
| 1887 | * options so that if remount we can use just that. |
| 1888 | */ |
| 1889 | static int ntfs_init_fs_context(struct fs_context *fc) |
| 1890 | { |
| 1891 | struct ntfs_mount_options *opts; |
| 1892 | struct ntfs_sb_info *sbi; |
| 1893 | |
| 1894 | opts = kzalloc_obj(struct ntfs_mount_options, GFP_NOFS); |
| 1895 | if (!opts) |
| 1896 | return -ENOMEM; |
| 1897 | |
| 1898 | /* Default options. */ |
| 1899 | opts->fs_uid = current_uid(); |
| 1900 | opts->fs_gid = current_gid(); |
| 1901 | opts->fs_fmask_inv = opts->fs_dmask_inv = ~current_umask(); |
| 1902 | opts->prealloc = 1; |
| 1903 | opts->ads = 1; |
| 1904 | |
| 1905 | #ifdef CONFIG_NTFS3_FS_POSIX_ACL |
| 1906 | /* Set the default value 'acl' */ |
| 1907 | fc->sb_flags |= SB_POSIXACL; |
| 1908 | #endif |
| 1909 | |
| 1910 | if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) |
| 1911 | goto ok; |
| 1912 | |
| 1913 | sbi = kzalloc_obj(struct ntfs_sb_info, GFP_NOFS); |
| 1914 | if (!sbi) |
| 1915 | goto free_opts; |
| 1916 | |
| 1917 | sbi->upcase = kvmalloc(0x10000 * sizeof(short), GFP_KERNEL); |
| 1918 | if (!sbi->upcase) |
| 1919 | goto free_sbi; |
| 1920 | |
| 1921 | ratelimit_state_init(&sbi->msg_ratelimit, DEFAULT_RATELIMIT_INTERVAL, |
| 1922 | DEFAULT_RATELIMIT_BURST); |
| 1923 | |
| 1924 | mutex_init(&sbi->compress.mtx_lznt); |
| 1925 | #ifdef CONFIG_NTFS3_LZX_XPRESS |
| 1926 | mutex_init(&sbi->compress.mtx_xpress); |
| 1927 | mutex_init(&sbi->compress.mtx_lzx); |
| 1928 | #endif |
| 1929 | |
| 1930 | fc->s_fs_info = sbi; |
| 1931 | ok: |
| 1932 | fc->fs_private = opts; |
| 1933 | fc->ops = &ntfs_context_ops; |
| 1934 | |
| 1935 | return 0; |
| 1936 | free_sbi: |
| 1937 | kfree(sbi); |
| 1938 | free_opts: |
| 1939 | kfree(opts); |
| 1940 | return -ENOMEM; |
| 1941 | } |
| 1942 | |
| 1943 | static void ntfs3_kill_sb(struct super_block *sb) |
| 1944 | { |
| 1945 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 1946 | |
| 1947 | kill_block_super(sb); |
| 1948 | |
| 1949 | /* |
| 1950 | * A pathwalk in rcu mode may still be in ->d_hash() or ->d_compare() |
| 1951 | * and read the upcase table and the nls table through the sbi. |
| 1952 | */ |
| 1953 | call_rcu(&sbi->rcu, ntfs3_free_sbi_rcu); |
| 1954 | } |
| 1955 | |
| 1956 | // clang-format off |
| 1957 | static struct file_system_type ntfs_fs_type = { |
| 1958 | .owner = THIS_MODULE, |
| 1959 | .name = "ntfs3", |
| 1960 | .init_fs_context = ntfs_init_fs_context, |
| 1961 | .parameters = ntfs_fs_parameters, |
| 1962 | .kill_sb = ntfs3_kill_sb, |
| 1963 | .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP, |
| 1964 | }; |
| 1965 | // clang-format on |
| 1966 | |
| 1967 | static int __init init_ntfs_fs(void) |
| 1968 | { |
| 1969 | int err; |
| 1970 | |
| 1971 | if (IS_ENABLED(CONFIG_NTFS3_FS_POSIX_ACL)) |
| 1972 | pr_info("ntfs3: Enabled Linux POSIX ACLs support\n"); |
| 1973 | if (IS_ENABLED(CONFIG_NTFS3_64BIT_CLUSTER)) |
| 1974 | pr_notice( |
| 1975 | "ntfs3: Warning: Activated 64 bits per cluster. Windows does not support this\n"); |
| 1976 | if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS)) |
| 1977 | pr_info("ntfs3: Read-only LZX/Xpress compression included\n"); |
| 1978 | |
| 1979 | ntfs_create_proc_root(); |
| 1980 | |
| 1981 | err = ntfs3_init_bitmap(); |
| 1982 | if (err) |
| 1983 | goto out2; |
| 1984 | |
| 1985 | ntfs_inode_cachep = kmem_cache_create( |
| 1986 | "ntfs_inode_cache", sizeof(struct ntfs_inode), 0, |
| 1987 | (SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT), init_once); |
| 1988 | if (!ntfs_inode_cachep) { |
| 1989 | err = -ENOMEM; |
| 1990 | goto out1; |
| 1991 | } |
| 1992 | |
| 1993 | err = register_filesystem(&ntfs_fs_type); |
| 1994 | if (err) |
| 1995 | goto out; |
| 1996 | |
| 1997 | return 0; |
| 1998 | out: |
| 1999 | kmem_cache_destroy(ntfs_inode_cachep); |
| 2000 | out1: |
| 2001 | ntfs3_exit_bitmap(); |
| 2002 | out2: |
| 2003 | ntfs_remove_proc_root(); |
| 2004 | return err; |
| 2005 | } |
| 2006 | |
| 2007 | static void __exit exit_ntfs_fs(void) |
| 2008 | { |
| 2009 | rcu_barrier(); |
| 2010 | kmem_cache_destroy(ntfs_inode_cachep); |
| 2011 | unregister_filesystem(&ntfs_fs_type); |
| 2012 | ntfs3_exit_bitmap(); |
| 2013 | ntfs_remove_proc_root(); |
| 2014 | } |
| 2015 | |
| 2016 | MODULE_LICENSE("GPL"); |
| 2017 | MODULE_DESCRIPTION("ntfs3 read/write filesystem"); |
| 2018 | #ifdef CONFIG_NTFS3_FS_POSIX_ACL |
| 2019 | MODULE_INFO(behaviour, "Enabled Linux POSIX ACLs support"); |
| 2020 | #endif |
| 2021 | #ifdef CONFIG_NTFS3_64BIT_CLUSTER |
| 2022 | MODULE_INFO( |
| 2023 | cluster, |
| 2024 | "Warning: Activated 64 bits per cluster. Windows does not support this"); |
| 2025 | #endif |
| 2026 | #ifdef CONFIG_NTFS3_LZX_XPRESS |
| 2027 | MODULE_INFO(compression, "Read-only lzx/xpress compression included"); |
| 2028 | #endif |
| 2029 | |
| 2030 | MODULE_AUTHOR("Konstantin Komarov"); |
| 2031 | MODULE_ALIAS_FS("ntfs3"); |
| 2032 | |
| 2033 | module_init(init_ntfs_fs); |
| 2034 | module_exit(exit_ntfs_fs); |