/kern_oII/fs/ntfs/super.c

http://omnia2droid.googlecode.com/ · C · 3235 lines · 2307 code · 122 blank · 806 comment · 468 complexity · ba5b458e9a92b0a893b4639d8ff81057 MD5 · raw file

  1. /*
  2. * super.c - NTFS kernel super block handling. Part of the Linux-NTFS project.
  3. *
  4. * Copyright (c) 2001-2007 Anton Altaparmakov
  5. * Copyright (c) 2001,2002 Richard Russon
  6. *
  7. * This program/include file is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as published
  9. * by the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program/include file is distributed in the hope that it will be
  13. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program (in the main directory of the Linux-NTFS
  19. * distribution in the file COPYING); if not, write to the Free Software
  20. * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/stddef.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/string.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/blkdev.h> /* For bdev_logical_block_size(). */
  28. #include <linux/backing-dev.h>
  29. #include <linux/buffer_head.h>
  30. #include <linux/vfs.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/smp_lock.h>
  33. #include "sysctl.h"
  34. #include "logfile.h"
  35. #include "quota.h"
  36. #include "usnjrnl.h"
  37. #include "dir.h"
  38. #include "debug.h"
  39. #include "index.h"
  40. #include "aops.h"
  41. #include "layout.h"
  42. #include "malloc.h"
  43. #include "ntfs.h"
  44. /* Number of mounted filesystems which have compression enabled. */
  45. static unsigned long ntfs_nr_compression_users;
  46. /* A global default upcase table and a corresponding reference count. */
  47. static ntfschar *default_upcase = NULL;
  48. static unsigned long ntfs_nr_upcase_users = 0;
  49. /* Error constants/strings used in inode.c::ntfs_show_options(). */
  50. typedef enum {
  51. /* One of these must be present, default is ON_ERRORS_CONTINUE. */
  52. ON_ERRORS_PANIC = 0x01,
  53. ON_ERRORS_REMOUNT_RO = 0x02,
  54. ON_ERRORS_CONTINUE = 0x04,
  55. /* Optional, can be combined with any of the above. */
  56. ON_ERRORS_RECOVER = 0x10,
  57. } ON_ERRORS_ACTIONS;
  58. const option_t on_errors_arr[] = {
  59. { ON_ERRORS_PANIC, "panic" },
  60. { ON_ERRORS_REMOUNT_RO, "remount-ro", },
  61. { ON_ERRORS_CONTINUE, "continue", },
  62. { ON_ERRORS_RECOVER, "recover" },
  63. { 0, NULL }
  64. };
  65. /**
  66. * simple_getbool -
  67. *
  68. * Copied from old ntfs driver (which copied from vfat driver).
  69. */
  70. static int simple_getbool(char *s, bool *setval)
  71. {
  72. if (s) {
  73. if (!strcmp(s, "1") || !strcmp(s, "yes") || !strcmp(s, "true"))
  74. *setval = true;
  75. else if (!strcmp(s, "0") || !strcmp(s, "no") ||
  76. !strcmp(s, "false"))
  77. *setval = false;
  78. else
  79. return 0;
  80. } else
  81. *setval = true;
  82. return 1;
  83. }
  84. /**
  85. * parse_options - parse the (re)mount options
  86. * @vol: ntfs volume
  87. * @opt: string containing the (re)mount options
  88. *
  89. * Parse the recognized options in @opt for the ntfs volume described by @vol.
  90. */
  91. static bool parse_options(ntfs_volume *vol, char *opt)
  92. {
  93. char *p, *v, *ov;
  94. static char *utf8 = "utf8";
  95. int errors = 0, sloppy = 0;
  96. uid_t uid = (uid_t)-1;
  97. gid_t gid = (gid_t)-1;
  98. mode_t fmask = (mode_t)-1, dmask = (mode_t)-1;
  99. int mft_zone_multiplier = -1, on_errors = -1;
  100. int show_sys_files = -1, case_sensitive = -1, disable_sparse = -1;
  101. struct nls_table *nls_map = NULL, *old_nls;
  102. /* I am lazy... (-8 */
  103. #define NTFS_GETOPT_WITH_DEFAULT(option, variable, default_value) \
  104. if (!strcmp(p, option)) { \
  105. if (!v || !*v) \
  106. variable = default_value; \
  107. else { \
  108. variable = simple_strtoul(ov = v, &v, 0); \
  109. if (*v) \
  110. goto needs_val; \
  111. } \
  112. }
  113. #define NTFS_GETOPT(option, variable) \
  114. if (!strcmp(p, option)) { \
  115. if (!v || !*v) \
  116. goto needs_arg; \
  117. variable = simple_strtoul(ov = v, &v, 0); \
  118. if (*v) \
  119. goto needs_val; \
  120. }
  121. #define NTFS_GETOPT_OCTAL(option, variable) \
  122. if (!strcmp(p, option)) { \
  123. if (!v || !*v) \
  124. goto needs_arg; \
  125. variable = simple_strtoul(ov = v, &v, 8); \
  126. if (*v) \
  127. goto needs_val; \
  128. }
  129. #define NTFS_GETOPT_BOOL(option, variable) \
  130. if (!strcmp(p, option)) { \
  131. bool val; \
  132. if (!simple_getbool(v, &val)) \
  133. goto needs_bool; \
  134. variable = val; \
  135. }
  136. #define NTFS_GETOPT_OPTIONS_ARRAY(option, variable, opt_array) \
  137. if (!strcmp(p, option)) { \
  138. int _i; \
  139. if (!v || !*v) \
  140. goto needs_arg; \
  141. ov = v; \
  142. if (variable == -1) \
  143. variable = 0; \
  144. for (_i = 0; opt_array[_i].str && *opt_array[_i].str; _i++) \
  145. if (!strcmp(opt_array[_i].str, v)) { \
  146. variable |= opt_array[_i].val; \
  147. break; \
  148. } \
  149. if (!opt_array[_i].str || !*opt_array[_i].str) \
  150. goto needs_val; \
  151. }
  152. if (!opt || !*opt)
  153. goto no_mount_options;
  154. ntfs_debug("Entering with mount options string: %s", opt);
  155. while ((p = strsep(&opt, ","))) {
  156. if ((v = strchr(p, '=')))
  157. *v++ = 0;
  158. NTFS_GETOPT("uid", uid)
  159. else NTFS_GETOPT("gid", gid)
  160. else NTFS_GETOPT_OCTAL("umask", fmask = dmask)
  161. else NTFS_GETOPT_OCTAL("fmask", fmask)
  162. else NTFS_GETOPT_OCTAL("dmask", dmask)
  163. else NTFS_GETOPT("mft_zone_multiplier", mft_zone_multiplier)
  164. else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, true)
  165. else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files)
  166. else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive)
  167. else NTFS_GETOPT_BOOL("disable_sparse", disable_sparse)
  168. else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors,
  169. on_errors_arr)
  170. else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes"))
  171. ntfs_warning(vol->sb, "Ignoring obsolete option %s.",
  172. p);
  173. else if (!strcmp(p, "nls") || !strcmp(p, "iocharset")) {
  174. if (!strcmp(p, "iocharset"))
  175. ntfs_warning(vol->sb, "Option iocharset is "
  176. "deprecated. Please use "
  177. "option nls=<charsetname> in "
  178. "the future.");
  179. if (!v || !*v)
  180. goto needs_arg;
  181. use_utf8:
  182. old_nls = nls_map;
  183. nls_map = load_nls(v);
  184. if (!nls_map) {
  185. if (!old_nls) {
  186. ntfs_error(vol->sb, "NLS character set "
  187. "%s not found.", v);
  188. return false;
  189. }
  190. ntfs_error(vol->sb, "NLS character set %s not "
  191. "found. Using previous one %s.",
  192. v, old_nls->charset);
  193. nls_map = old_nls;
  194. } else /* nls_map */ {
  195. if (old_nls)
  196. unload_nls(old_nls);
  197. }
  198. } else if (!strcmp(p, "utf8")) {
  199. bool val = false;
  200. ntfs_warning(vol->sb, "Option utf8 is no longer "
  201. "supported, using option nls=utf8. Please "
  202. "use option nls=utf8 in the future and "
  203. "make sure utf8 is compiled either as a "
  204. "module or into the kernel.");
  205. if (!v || !*v)
  206. val = true;
  207. else if (!simple_getbool(v, &val))
  208. goto needs_bool;
  209. if (val) {
  210. v = utf8;
  211. goto use_utf8;
  212. }
  213. } else {
  214. ntfs_error(vol->sb, "Unrecognized mount option %s.", p);
  215. if (errors < INT_MAX)
  216. errors++;
  217. }
  218. #undef NTFS_GETOPT_OPTIONS_ARRAY
  219. #undef NTFS_GETOPT_BOOL
  220. #undef NTFS_GETOPT
  221. #undef NTFS_GETOPT_WITH_DEFAULT
  222. }
  223. no_mount_options:
  224. if (errors && !sloppy)
  225. return false;
  226. if (sloppy)
  227. ntfs_warning(vol->sb, "Sloppy option given. Ignoring "
  228. "unrecognized mount option(s) and continuing.");
  229. /* Keep this first! */
  230. if (on_errors != -1) {
  231. if (!on_errors) {
  232. ntfs_error(vol->sb, "Invalid errors option argument "
  233. "or bug in options parser.");
  234. return false;
  235. }
  236. }
  237. if (nls_map) {
  238. if (vol->nls_map && vol->nls_map != nls_map) {
  239. ntfs_error(vol->sb, "Cannot change NLS character set "
  240. "on remount.");
  241. return false;
  242. } /* else (!vol->nls_map) */
  243. ntfs_debug("Using NLS character set %s.", nls_map->charset);
  244. vol->nls_map = nls_map;
  245. } else /* (!nls_map) */ {
  246. if (!vol->nls_map) {
  247. vol->nls_map = load_nls_default();
  248. if (!vol->nls_map) {
  249. ntfs_error(vol->sb, "Failed to load default "
  250. "NLS character set.");
  251. return false;
  252. }
  253. ntfs_debug("Using default NLS character set (%s).",
  254. vol->nls_map->charset);
  255. }
  256. }
  257. if (mft_zone_multiplier != -1) {
  258. if (vol->mft_zone_multiplier && vol->mft_zone_multiplier !=
  259. mft_zone_multiplier) {
  260. ntfs_error(vol->sb, "Cannot change mft_zone_multiplier "
  261. "on remount.");
  262. return false;
  263. }
  264. if (mft_zone_multiplier < 1 || mft_zone_multiplier > 4) {
  265. ntfs_error(vol->sb, "Invalid mft_zone_multiplier. "
  266. "Using default value, i.e. 1.");
  267. mft_zone_multiplier = 1;
  268. }
  269. vol->mft_zone_multiplier = mft_zone_multiplier;
  270. }
  271. if (!vol->mft_zone_multiplier)
  272. vol->mft_zone_multiplier = 1;
  273. if (on_errors != -1)
  274. vol->on_errors = on_errors;
  275. if (!vol->on_errors || vol->on_errors == ON_ERRORS_RECOVER)
  276. vol->on_errors |= ON_ERRORS_CONTINUE;
  277. if (uid != (uid_t)-1)
  278. vol->uid = uid;
  279. if (gid != (gid_t)-1)
  280. vol->gid = gid;
  281. if (fmask != (mode_t)-1)
  282. vol->fmask = fmask;
  283. if (dmask != (mode_t)-1)
  284. vol->dmask = dmask;
  285. if (show_sys_files != -1) {
  286. if (show_sys_files)
  287. NVolSetShowSystemFiles(vol);
  288. else
  289. NVolClearShowSystemFiles(vol);
  290. }
  291. if (case_sensitive != -1) {
  292. if (case_sensitive)
  293. NVolSetCaseSensitive(vol);
  294. else
  295. NVolClearCaseSensitive(vol);
  296. }
  297. if (disable_sparse != -1) {
  298. if (disable_sparse)
  299. NVolClearSparseEnabled(vol);
  300. else {
  301. if (!NVolSparseEnabled(vol) &&
  302. vol->major_ver && vol->major_ver < 3)
  303. ntfs_warning(vol->sb, "Not enabling sparse "
  304. "support due to NTFS volume "
  305. "version %i.%i (need at least "
  306. "version 3.0).", vol->major_ver,
  307. vol->minor_ver);
  308. else
  309. NVolSetSparseEnabled(vol);
  310. }
  311. }
  312. return true;
  313. needs_arg:
  314. ntfs_error(vol->sb, "The %s option requires an argument.", p);
  315. return false;
  316. needs_bool:
  317. ntfs_error(vol->sb, "The %s option requires a boolean argument.", p);
  318. return false;
  319. needs_val:
  320. ntfs_error(vol->sb, "Invalid %s option argument: %s", p, ov);
  321. return false;
  322. }
  323. #ifdef NTFS_RW
  324. /**
  325. * ntfs_write_volume_flags - write new flags to the volume information flags
  326. * @vol: ntfs volume on which to modify the flags
  327. * @flags: new flags value for the volume information flags
  328. *
  329. * Internal function. You probably want to use ntfs_{set,clear}_volume_flags()
  330. * instead (see below).
  331. *
  332. * Replace the volume information flags on the volume @vol with the value
  333. * supplied in @flags. Note, this overwrites the volume information flags, so
  334. * make sure to combine the flags you want to modify with the old flags and use
  335. * the result when calling ntfs_write_volume_flags().
  336. *
  337. * Return 0 on success and -errno on error.
  338. */
  339. static int ntfs_write_volume_flags(ntfs_volume *vol, const VOLUME_FLAGS flags)
  340. {
  341. ntfs_inode *ni = NTFS_I(vol->vol_ino);
  342. MFT_RECORD *m;
  343. VOLUME_INFORMATION *vi;
  344. ntfs_attr_search_ctx *ctx;
  345. int err;
  346. ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
  347. le16_to_cpu(vol->vol_flags), le16_to_cpu(flags));
  348. if (vol->vol_flags == flags)
  349. goto done;
  350. BUG_ON(!ni);
  351. m = map_mft_record(ni);
  352. if (IS_ERR(m)) {
  353. err = PTR_ERR(m);
  354. goto err_out;
  355. }
  356. ctx = ntfs_attr_get_search_ctx(ni, m);
  357. if (!ctx) {
  358. err = -ENOMEM;
  359. goto put_unm_err_out;
  360. }
  361. err = ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
  362. ctx);
  363. if (err)
  364. goto put_unm_err_out;
  365. vi = (VOLUME_INFORMATION*)((u8*)ctx->attr +
  366. le16_to_cpu(ctx->attr->data.resident.value_offset));
  367. vol->vol_flags = vi->flags = flags;
  368. flush_dcache_mft_record_page(ctx->ntfs_ino);
  369. mark_mft_record_dirty(ctx->ntfs_ino);
  370. ntfs_attr_put_search_ctx(ctx);
  371. unmap_mft_record(ni);
  372. done:
  373. ntfs_debug("Done.");
  374. return 0;
  375. put_unm_err_out:
  376. if (ctx)
  377. ntfs_attr_put_search_ctx(ctx);
  378. unmap_mft_record(ni);
  379. err_out:
  380. ntfs_error(vol->sb, "Failed with error code %i.", -err);
  381. return err;
  382. }
  383. /**
  384. * ntfs_set_volume_flags - set bits in the volume information flags
  385. * @vol: ntfs volume on which to modify the flags
  386. * @flags: flags to set on the volume
  387. *
  388. * Set the bits in @flags in the volume information flags on the volume @vol.
  389. *
  390. * Return 0 on success and -errno on error.
  391. */
  392. static inline int ntfs_set_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
  393. {
  394. flags &= VOLUME_FLAGS_MASK;
  395. return ntfs_write_volume_flags(vol, vol->vol_flags | flags);
  396. }
  397. /**
  398. * ntfs_clear_volume_flags - clear bits in the volume information flags
  399. * @vol: ntfs volume on which to modify the flags
  400. * @flags: flags to clear on the volume
  401. *
  402. * Clear the bits in @flags in the volume information flags on the volume @vol.
  403. *
  404. * Return 0 on success and -errno on error.
  405. */
  406. static inline int ntfs_clear_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
  407. {
  408. flags &= VOLUME_FLAGS_MASK;
  409. flags = vol->vol_flags & cpu_to_le16(~le16_to_cpu(flags));
  410. return ntfs_write_volume_flags(vol, flags);
  411. }
  412. #endif /* NTFS_RW */
  413. /**
  414. * ntfs_remount - change the mount options of a mounted ntfs filesystem
  415. * @sb: superblock of mounted ntfs filesystem
  416. * @flags: remount flags
  417. * @opt: remount options string
  418. *
  419. * Change the mount options of an already mounted ntfs filesystem.
  420. *
  421. * NOTE: The VFS sets the @sb->s_flags remount flags to @flags after
  422. * ntfs_remount() returns successfully (i.e. returns 0). Otherwise,
  423. * @sb->s_flags are not changed.
  424. */
  425. static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
  426. {
  427. ntfs_volume *vol = NTFS_SB(sb);
  428. ntfs_debug("Entering with remount options string: %s", opt);
  429. lock_kernel();
  430. #ifndef NTFS_RW
  431. /* For read-only compiled driver, enforce read-only flag. */
  432. *flags |= MS_RDONLY;
  433. #else /* NTFS_RW */
  434. /*
  435. * For the read-write compiled driver, if we are remounting read-write,
  436. * make sure there are no volume errors and that no unsupported volume
  437. * flags are set. Also, empty the logfile journal as it would become
  438. * stale as soon as something is written to the volume and mark the
  439. * volume dirty so that chkdsk is run if the volume is not umounted
  440. * cleanly. Finally, mark the quotas out of date so Windows rescans
  441. * the volume on boot and updates them.
  442. *
  443. * When remounting read-only, mark the volume clean if no volume errors
  444. * have occured.
  445. */
  446. if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
  447. static const char *es = ". Cannot remount read-write.";
  448. /* Remounting read-write. */
  449. if (NVolErrors(vol)) {
  450. ntfs_error(sb, "Volume has errors and is read-only%s",
  451. es);
  452. unlock_kernel();
  453. return -EROFS;
  454. }
  455. if (vol->vol_flags & VOLUME_IS_DIRTY) {
  456. ntfs_error(sb, "Volume is dirty and read-only%s", es);
  457. unlock_kernel();
  458. return -EROFS;
  459. }
  460. if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
  461. ntfs_error(sb, "Volume has been modified by chkdsk "
  462. "and is read-only%s", es);
  463. unlock_kernel();
  464. return -EROFS;
  465. }
  466. if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
  467. ntfs_error(sb, "Volume has unsupported flags set "
  468. "(0x%x) and is read-only%s",
  469. (unsigned)le16_to_cpu(vol->vol_flags),
  470. es);
  471. unlock_kernel();
  472. return -EROFS;
  473. }
  474. if (ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
  475. ntfs_error(sb, "Failed to set dirty bit in volume "
  476. "information flags%s", es);
  477. unlock_kernel();
  478. return -EROFS;
  479. }
  480. #if 0
  481. // TODO: Enable this code once we start modifying anything that
  482. // is different between NTFS 1.2 and 3.x...
  483. /* Set NT4 compatibility flag on newer NTFS version volumes. */
  484. if ((vol->major_ver > 1)) {
  485. if (ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
  486. ntfs_error(sb, "Failed to set NT4 "
  487. "compatibility flag%s", es);
  488. NVolSetErrors(vol);
  489. return -EROFS;
  490. }
  491. }
  492. #endif
  493. if (!ntfs_empty_logfile(vol->logfile_ino)) {
  494. ntfs_error(sb, "Failed to empty journal $LogFile%s",
  495. es);
  496. NVolSetErrors(vol);
  497. unlock_kernel();
  498. return -EROFS;
  499. }
  500. if (!ntfs_mark_quotas_out_of_date(vol)) {
  501. ntfs_error(sb, "Failed to mark quotas out of date%s",
  502. es);
  503. NVolSetErrors(vol);
  504. unlock_kernel();
  505. return -EROFS;
  506. }
  507. if (!ntfs_stamp_usnjrnl(vol)) {
  508. ntfs_error(sb, "Failed to stamp transation log "
  509. "($UsnJrnl)%s", es);
  510. NVolSetErrors(vol);
  511. unlock_kernel();
  512. return -EROFS;
  513. }
  514. } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) {
  515. /* Remounting read-only. */
  516. if (!NVolErrors(vol)) {
  517. if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
  518. ntfs_warning(sb, "Failed to clear dirty bit "
  519. "in volume information "
  520. "flags. Run chkdsk.");
  521. }
  522. }
  523. #endif /* NTFS_RW */
  524. // TODO: Deal with *flags.
  525. if (!parse_options(vol, opt)) {
  526. unlock_kernel();
  527. return -EINVAL;
  528. }
  529. unlock_kernel();
  530. ntfs_debug("Done.");
  531. return 0;
  532. }
  533. /**
  534. * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector
  535. * @sb: Super block of the device to which @b belongs.
  536. * @b: Boot sector of device @sb to check.
  537. * @silent: If 'true', all output will be silenced.
  538. *
  539. * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot
  540. * sector. Returns 'true' if it is valid and 'false' if not.
  541. *
  542. * @sb is only needed for warning/error output, i.e. it can be NULL when silent
  543. * is 'true'.
  544. */
  545. static bool is_boot_sector_ntfs(const struct super_block *sb,
  546. const NTFS_BOOT_SECTOR *b, const bool silent)
  547. {
  548. /*
  549. * Check that checksum == sum of u32 values from b to the checksum
  550. * field. If checksum is zero, no checking is done. We will work when
  551. * the checksum test fails, since some utilities update the boot sector
  552. * ignoring the checksum which leaves the checksum out-of-date. We
  553. * report a warning if this is the case.
  554. */
  555. if ((void*)b < (void*)&b->checksum && b->checksum && !silent) {
  556. le32 *u;
  557. u32 i;
  558. for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u)
  559. i += le32_to_cpup(u);
  560. if (le32_to_cpu(b->checksum) != i)
  561. ntfs_warning(sb, "Invalid boot sector checksum.");
  562. }
  563. /* Check OEMidentifier is "NTFS " */
  564. if (b->oem_id != magicNTFS)
  565. goto not_ntfs;
  566. /* Check bytes per sector value is between 256 and 4096. */
  567. if (le16_to_cpu(b->bpb.bytes_per_sector) < 0x100 ||
  568. le16_to_cpu(b->bpb.bytes_per_sector) > 0x1000)
  569. goto not_ntfs;
  570. /* Check sectors per cluster value is valid. */
  571. switch (b->bpb.sectors_per_cluster) {
  572. case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128:
  573. break;
  574. default:
  575. goto not_ntfs;
  576. }
  577. /* Check the cluster size is not above the maximum (64kiB). */
  578. if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) *
  579. b->bpb.sectors_per_cluster > NTFS_MAX_CLUSTER_SIZE)
  580. goto not_ntfs;
  581. /* Check reserved/unused fields are really zero. */
  582. if (le16_to_cpu(b->bpb.reserved_sectors) ||
  583. le16_to_cpu(b->bpb.root_entries) ||
  584. le16_to_cpu(b->bpb.sectors) ||
  585. le16_to_cpu(b->bpb.sectors_per_fat) ||
  586. le32_to_cpu(b->bpb.large_sectors) || b->bpb.fats)
  587. goto not_ntfs;
  588. /* Check clusters per file mft record value is valid. */
  589. if ((u8)b->clusters_per_mft_record < 0xe1 ||
  590. (u8)b->clusters_per_mft_record > 0xf7)
  591. switch (b->clusters_per_mft_record) {
  592. case 1: case 2: case 4: case 8: case 16: case 32: case 64:
  593. break;
  594. default:
  595. goto not_ntfs;
  596. }
  597. /* Check clusters per index block value is valid. */
  598. if ((u8)b->clusters_per_index_record < 0xe1 ||
  599. (u8)b->clusters_per_index_record > 0xf7)
  600. switch (b->clusters_per_index_record) {
  601. case 1: case 2: case 4: case 8: case 16: case 32: case 64:
  602. break;
  603. default:
  604. goto not_ntfs;
  605. }
  606. /*
  607. * Check for valid end of sector marker. We will work without it, but
  608. * many BIOSes will refuse to boot from a bootsector if the magic is
  609. * incorrect, so we emit a warning.
  610. */
  611. if (!silent && b->end_of_sector_marker != cpu_to_le16(0xaa55))
  612. ntfs_warning(sb, "Invalid end of sector marker.");
  613. return true;
  614. not_ntfs:
  615. return false;
  616. }
  617. /**
  618. * read_ntfs_boot_sector - read the NTFS boot sector of a device
  619. * @sb: super block of device to read the boot sector from
  620. * @silent: if true, suppress all output
  621. *
  622. * Reads the boot sector from the device and validates it. If that fails, tries
  623. * to read the backup boot sector, first from the end of the device a-la NT4 and
  624. * later and then from the middle of the device a-la NT3.51 and before.
  625. *
  626. * If a valid boot sector is found but it is not the primary boot sector, we
  627. * repair the primary boot sector silently (unless the device is read-only or
  628. * the primary boot sector is not accessible).
  629. *
  630. * NOTE: To call this function, @sb must have the fields s_dev, the ntfs super
  631. * block (u.ntfs_sb), nr_blocks and the device flags (s_flags) initialized
  632. * to their respective values.
  633. *
  634. * Return the unlocked buffer head containing the boot sector or NULL on error.
  635. */
  636. static struct buffer_head *read_ntfs_boot_sector(struct super_block *sb,
  637. const int silent)
  638. {
  639. const char *read_err_str = "Unable to read %s boot sector.";
  640. struct buffer_head *bh_primary, *bh_backup;
  641. sector_t nr_blocks = NTFS_SB(sb)->nr_blocks;
  642. /* Try to read primary boot sector. */
  643. if ((bh_primary = sb_bread(sb, 0))) {
  644. if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
  645. bh_primary->b_data, silent))
  646. return bh_primary;
  647. if (!silent)
  648. ntfs_error(sb, "Primary boot sector is invalid.");
  649. } else if (!silent)
  650. ntfs_error(sb, read_err_str, "primary");
  651. if (!(NTFS_SB(sb)->on_errors & ON_ERRORS_RECOVER)) {
  652. if (bh_primary)
  653. brelse(bh_primary);
  654. if (!silent)
  655. ntfs_error(sb, "Mount option errors=recover not used. "
  656. "Aborting without trying to recover.");
  657. return NULL;
  658. }
  659. /* Try to read NT4+ backup boot sector. */
  660. if ((bh_backup = sb_bread(sb, nr_blocks - 1))) {
  661. if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
  662. bh_backup->b_data, silent))
  663. goto hotfix_primary_boot_sector;
  664. brelse(bh_backup);
  665. } else if (!silent)
  666. ntfs_error(sb, read_err_str, "backup");
  667. /* Try to read NT3.51- backup boot sector. */
  668. if ((bh_backup = sb_bread(sb, nr_blocks >> 1))) {
  669. if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
  670. bh_backup->b_data, silent))
  671. goto hotfix_primary_boot_sector;
  672. if (!silent)
  673. ntfs_error(sb, "Could not find a valid backup boot "
  674. "sector.");
  675. brelse(bh_backup);
  676. } else if (!silent)
  677. ntfs_error(sb, read_err_str, "backup");
  678. /* We failed. Cleanup and return. */
  679. if (bh_primary)
  680. brelse(bh_primary);
  681. return NULL;
  682. hotfix_primary_boot_sector:
  683. if (bh_primary) {
  684. /*
  685. * If we managed to read sector zero and the volume is not
  686. * read-only, copy the found, valid backup boot sector to the
  687. * primary boot sector. Note we only copy the actual boot
  688. * sector structure, not the actual whole device sector as that
  689. * may be bigger and would potentially damage the $Boot system
  690. * file (FIXME: Would be nice to know if the backup boot sector
  691. * on a large sector device contains the whole boot loader or
  692. * just the first 512 bytes).
  693. */
  694. if (!(sb->s_flags & MS_RDONLY)) {
  695. ntfs_warning(sb, "Hot-fix: Recovering invalid primary "
  696. "boot sector from backup copy.");
  697. memcpy(bh_primary->b_data, bh_backup->b_data,
  698. NTFS_BLOCK_SIZE);
  699. mark_buffer_dirty(bh_primary);
  700. sync_dirty_buffer(bh_primary);
  701. if (buffer_uptodate(bh_primary)) {
  702. brelse(bh_backup);
  703. return bh_primary;
  704. }
  705. ntfs_error(sb, "Hot-fix: Device write error while "
  706. "recovering primary boot sector.");
  707. } else {
  708. ntfs_warning(sb, "Hot-fix: Recovery of primary boot "
  709. "sector failed: Read-only mount.");
  710. }
  711. brelse(bh_primary);
  712. }
  713. ntfs_warning(sb, "Using backup boot sector.");
  714. return bh_backup;
  715. }
  716. /**
  717. * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol
  718. * @vol: volume structure to initialise with data from boot sector
  719. * @b: boot sector to parse
  720. *
  721. * Parse the ntfs boot sector @b and store all imporant information therein in
  722. * the ntfs super block @vol. Return 'true' on success and 'false' on error.
  723. */
  724. static bool parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
  725. {
  726. unsigned int sectors_per_cluster_bits, nr_hidden_sects;
  727. int clusters_per_mft_record, clusters_per_index_record;
  728. s64 ll;
  729. vol->sector_size = le16_to_cpu(b->bpb.bytes_per_sector);
  730. vol->sector_size_bits = ffs(vol->sector_size) - 1;
  731. ntfs_debug("vol->sector_size = %i (0x%x)", vol->sector_size,
  732. vol->sector_size);
  733. ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits,
  734. vol->sector_size_bits);
  735. if (vol->sector_size < vol->sb->s_blocksize) {
  736. ntfs_error(vol->sb, "Sector size (%i) is smaller than the "
  737. "device block size (%lu). This is not "
  738. "supported. Sorry.", vol->sector_size,
  739. vol->sb->s_blocksize);
  740. return false;
  741. }
  742. ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster);
  743. sectors_per_cluster_bits = ffs(b->bpb.sectors_per_cluster) - 1;
  744. ntfs_debug("sectors_per_cluster_bits = 0x%x",
  745. sectors_per_cluster_bits);
  746. nr_hidden_sects = le32_to_cpu(b->bpb.hidden_sectors);
  747. ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects);
  748. vol->cluster_size = vol->sector_size << sectors_per_cluster_bits;
  749. vol->cluster_size_mask = vol->cluster_size - 1;
  750. vol->cluster_size_bits = ffs(vol->cluster_size) - 1;
  751. ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size,
  752. vol->cluster_size);
  753. ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask);
  754. ntfs_debug("vol->cluster_size_bits = %i", vol->cluster_size_bits);
  755. if (vol->cluster_size < vol->sector_size) {
  756. ntfs_error(vol->sb, "Cluster size (%i) is smaller than the "
  757. "sector size (%i). This is not supported. "
  758. "Sorry.", vol->cluster_size, vol->sector_size);
  759. return false;
  760. }
  761. clusters_per_mft_record = b->clusters_per_mft_record;
  762. ntfs_debug("clusters_per_mft_record = %i (0x%x)",
  763. clusters_per_mft_record, clusters_per_mft_record);
  764. if (clusters_per_mft_record > 0)
  765. vol->mft_record_size = vol->cluster_size <<
  766. (ffs(clusters_per_mft_record) - 1);
  767. else
  768. /*
  769. * When mft_record_size < cluster_size, clusters_per_mft_record
  770. * = -log2(mft_record_size) bytes. mft_record_size normaly is
  771. * 1024 bytes, which is encoded as 0xF6 (-10 in decimal).
  772. */
  773. vol->mft_record_size = 1 << -clusters_per_mft_record;
  774. vol->mft_record_size_mask = vol->mft_record_size - 1;
  775. vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1;
  776. ntfs_debug("vol->mft_record_size = %i (0x%x)", vol->mft_record_size,
  777. vol->mft_record_size);
  778. ntfs_debug("vol->mft_record_size_mask = 0x%x",
  779. vol->mft_record_size_mask);
  780. ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
  781. vol->mft_record_size_bits, vol->mft_record_size_bits);
  782. /*
  783. * We cannot support mft record sizes above the PAGE_CACHE_SIZE since
  784. * we store $MFT/$DATA, the table of mft records in the page cache.
  785. */
  786. if (vol->mft_record_size > PAGE_CACHE_SIZE) {
  787. ntfs_error(vol->sb, "Mft record size (%i) exceeds the "
  788. "PAGE_CACHE_SIZE on your system (%lu). "
  789. "This is not supported. Sorry.",
  790. vol->mft_record_size, PAGE_CACHE_SIZE);
  791. return false;
  792. }
  793. /* We cannot support mft record sizes below the sector size. */
  794. if (vol->mft_record_size < vol->sector_size) {
  795. ntfs_error(vol->sb, "Mft record size (%i) is smaller than the "
  796. "sector size (%i). This is not supported. "
  797. "Sorry.", vol->mft_record_size,
  798. vol->sector_size);
  799. return false;
  800. }
  801. clusters_per_index_record = b->clusters_per_index_record;
  802. ntfs_debug("clusters_per_index_record = %i (0x%x)",
  803. clusters_per_index_record, clusters_per_index_record);
  804. if (clusters_per_index_record > 0)
  805. vol->index_record_size = vol->cluster_size <<
  806. (ffs(clusters_per_index_record) - 1);
  807. else
  808. /*
  809. * When index_record_size < cluster_size,
  810. * clusters_per_index_record = -log2(index_record_size) bytes.
  811. * index_record_size normaly equals 4096 bytes, which is
  812. * encoded as 0xF4 (-12 in decimal).
  813. */
  814. vol->index_record_size = 1 << -clusters_per_index_record;
  815. vol->index_record_size_mask = vol->index_record_size - 1;
  816. vol->index_record_size_bits = ffs(vol->index_record_size) - 1;
  817. ntfs_debug("vol->index_record_size = %i (0x%x)",
  818. vol->index_record_size, vol->index_record_size);
  819. ntfs_debug("vol->index_record_size_mask = 0x%x",
  820. vol->index_record_size_mask);
  821. ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
  822. vol->index_record_size_bits,
  823. vol->index_record_size_bits);
  824. /* We cannot support index record sizes below the sector size. */
  825. if (vol->index_record_size < vol->sector_size) {
  826. ntfs_error(vol->sb, "Index record size (%i) is smaller than "
  827. "the sector size (%i). This is not "
  828. "supported. Sorry.", vol->index_record_size,
  829. vol->sector_size);
  830. return false;
  831. }
  832. /*
  833. * Get the size of the volume in clusters and check for 64-bit-ness.
  834. * Windows currently only uses 32 bits to save the clusters so we do
  835. * the same as it is much faster on 32-bit CPUs.
  836. */
  837. ll = sle64_to_cpu(b->number_of_sectors) >> sectors_per_cluster_bits;
  838. if ((u64)ll >= 1ULL << 32) {
  839. ntfs_error(vol->sb, "Cannot handle 64-bit clusters. Sorry.");
  840. return false;
  841. }
  842. vol->nr_clusters = ll;
  843. ntfs_debug("vol->nr_clusters = 0x%llx", (long long)vol->nr_clusters);
  844. /*
  845. * On an architecture where unsigned long is 32-bits, we restrict the
  846. * volume size to 2TiB (2^41). On a 64-bit architecture, the compiler
  847. * will hopefully optimize the whole check away.
  848. */
  849. if (sizeof(unsigned long) < 8) {
  850. if ((ll << vol->cluster_size_bits) >= (1ULL << 41)) {
  851. ntfs_error(vol->sb, "Volume size (%lluTiB) is too "
  852. "large for this architecture. "
  853. "Maximum supported is 2TiB. Sorry.",
  854. (unsigned long long)ll >> (40 -
  855. vol->cluster_size_bits));
  856. return false;
  857. }
  858. }
  859. ll = sle64_to_cpu(b->mft_lcn);
  860. if (ll >= vol->nr_clusters) {
  861. ntfs_error(vol->sb, "MFT LCN (%lli, 0x%llx) is beyond end of "
  862. "volume. Weird.", (unsigned long long)ll,
  863. (unsigned long long)ll);
  864. return false;
  865. }
  866. vol->mft_lcn = ll;
  867. ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol->mft_lcn);
  868. ll = sle64_to_cpu(b->mftmirr_lcn);
  869. if (ll >= vol->nr_clusters) {
  870. ntfs_error(vol->sb, "MFTMirr LCN (%lli, 0x%llx) is beyond end "
  871. "of volume. Weird.", (unsigned long long)ll,
  872. (unsigned long long)ll);
  873. return false;
  874. }
  875. vol->mftmirr_lcn = ll;
  876. ntfs_debug("vol->mftmirr_lcn = 0x%llx", (long long)vol->mftmirr_lcn);
  877. #ifdef NTFS_RW
  878. /*
  879. * Work out the size of the mft mirror in number of mft records. If the
  880. * cluster size is less than or equal to the size taken by four mft
  881. * records, the mft mirror stores the first four mft records. If the
  882. * cluster size is bigger than the size taken by four mft records, the
  883. * mft mirror contains as many mft records as will fit into one
  884. * cluster.
  885. */
  886. if (vol->cluster_size <= (4 << vol->mft_record_size_bits))
  887. vol->mftmirr_size = 4;
  888. else
  889. vol->mftmirr_size = vol->cluster_size >>
  890. vol->mft_record_size_bits;
  891. ntfs_debug("vol->mftmirr_size = %i", vol->mftmirr_size);
  892. #endif /* NTFS_RW */
  893. vol->serial_no = le64_to_cpu(b->volume_serial_number);
  894. ntfs_debug("vol->serial_no = 0x%llx",
  895. (unsigned long long)vol->serial_no);
  896. return true;
  897. }
  898. /**
  899. * ntfs_setup_allocators - initialize the cluster and mft allocators
  900. * @vol: volume structure for which to setup the allocators
  901. *
  902. * Setup the cluster (lcn) and mft allocators to the starting values.
  903. */
  904. static void ntfs_setup_allocators(ntfs_volume *vol)
  905. {
  906. #ifdef NTFS_RW
  907. LCN mft_zone_size, mft_lcn;
  908. #endif /* NTFS_RW */
  909. ntfs_debug("vol->mft_zone_multiplier = 0x%x",
  910. vol->mft_zone_multiplier);
  911. #ifdef NTFS_RW
  912. /* Determine the size of the MFT zone. */
  913. mft_zone_size = vol->nr_clusters;
  914. switch (vol->mft_zone_multiplier) { /* % of volume size in clusters */
  915. case 4:
  916. mft_zone_size >>= 1; /* 50% */
  917. break;
  918. case 3:
  919. mft_zone_size = (mft_zone_size +
  920. (mft_zone_size >> 1)) >> 2; /* 37.5% */
  921. break;
  922. case 2:
  923. mft_zone_size >>= 2; /* 25% */
  924. break;
  925. /* case 1: */
  926. default:
  927. mft_zone_size >>= 3; /* 12.5% */
  928. break;
  929. }
  930. /* Setup the mft zone. */
  931. vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn;
  932. ntfs_debug("vol->mft_zone_pos = 0x%llx",
  933. (unsigned long long)vol->mft_zone_pos);
  934. /*
  935. * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
  936. * source) and if the actual mft_lcn is in the expected place or even
  937. * further to the front of the volume, extend the mft_zone to cover the
  938. * beginning of the volume as well. This is in order to protect the
  939. * area reserved for the mft bitmap as well within the mft_zone itself.
  940. * On non-standard volumes we do not protect it as the overhead would
  941. * be higher than the speed increase we would get by doing it.
  942. */
  943. mft_lcn = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size;
  944. if (mft_lcn * vol->cluster_size < 16 * 1024)
  945. mft_lcn = (16 * 1024 + vol->cluster_size - 1) /
  946. vol->cluster_size;
  947. if (vol->mft_zone_start <= mft_lcn)
  948. vol->mft_zone_start = 0;
  949. ntfs_debug("vol->mft_zone_start = 0x%llx",
  950. (unsigned long long)vol->mft_zone_start);
  951. /*
  952. * Need to cap the mft zone on non-standard volumes so that it does
  953. * not point outside the boundaries of the volume. We do this by
  954. * halving the zone size until we are inside the volume.
  955. */
  956. vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
  957. while (vol->mft_zone_end >= vol->nr_clusters) {
  958. mft_zone_size >>= 1;
  959. vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
  960. }
  961. ntfs_debug("vol->mft_zone_end = 0x%llx",
  962. (unsigned long long)vol->mft_zone_end);
  963. /*
  964. * Set the current position within each data zone to the start of the
  965. * respective zone.
  966. */
  967. vol->data1_zone_pos = vol->mft_zone_end;
  968. ntfs_debug("vol->data1_zone_pos = 0x%llx",
  969. (unsigned long long)vol->data1_zone_pos);
  970. vol->data2_zone_pos = 0;
  971. ntfs_debug("vol->data2_zone_pos = 0x%llx",
  972. (unsigned long long)vol->data2_zone_pos);
  973. /* Set the mft data allocation position to mft record 24. */
  974. vol->mft_data_pos = 24;
  975. ntfs_debug("vol->mft_data_pos = 0x%llx",
  976. (unsigned long long)vol->mft_data_pos);
  977. #endif /* NTFS_RW */
  978. }
  979. #ifdef NTFS_RW
  980. /**
  981. * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume
  982. * @vol: ntfs super block describing device whose mft mirror to load
  983. *
  984. * Return 'true' on success or 'false' on error.
  985. */
  986. static bool load_and_init_mft_mirror(ntfs_volume *vol)
  987. {
  988. struct inode *tmp_ino;
  989. ntfs_inode *tmp_ni;
  990. ntfs_debug("Entering.");
  991. /* Get mft mirror inode. */
  992. tmp_ino = ntfs_iget(vol->sb, FILE_MFTMirr);
  993. if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
  994. if (!IS_ERR(tmp_ino))
  995. iput(tmp_ino);
  996. /* Caller will display error message. */
  997. return false;
  998. }
  999. /*
  1000. * Re-initialize some specifics about $MFTMirr's inode as
  1001. * ntfs_read_inode() will have set up the default ones.
  1002. */
  1003. /* Set uid and gid to root. */
  1004. tmp_ino->i_uid = tmp_ino->i_gid = 0;
  1005. /* Regular file. No access for anyone. */
  1006. tmp_ino->i_mode = S_IFREG;
  1007. /* No VFS initiated operations allowed for $MFTMirr. */
  1008. tmp_ino->i_op = &ntfs_empty_inode_ops;
  1009. tmp_ino->i_fop = &ntfs_empty_file_ops;
  1010. /* Put in our special address space operations. */
  1011. tmp_ino->i_mapping->a_ops = &ntfs_mst_aops;
  1012. tmp_ni = NTFS_I(tmp_ino);
  1013. /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
  1014. NInoSetMstProtected(tmp_ni);
  1015. NInoSetSparseDisabled(tmp_ni);
  1016. /*
  1017. * Set up our little cheat allowing us to reuse the async read io
  1018. * completion handler for directories.
  1019. */
  1020. tmp_ni->itype.index.block_size = vol->mft_record_size;
  1021. tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits;
  1022. vol->mftmirr_ino = tmp_ino;
  1023. ntfs_debug("Done.");
  1024. return true;
  1025. }
  1026. /**
  1027. * check_mft_mirror - compare contents of the mft mirror with the mft
  1028. * @vol: ntfs super block describing device whose mft mirror to check
  1029. *
  1030. * Return 'true' on success or 'false' on error.
  1031. *
  1032. * Note, this function also results in the mft mirror runlist being completely
  1033. * mapped into memory. The mft mirror write code requires this and will BUG()
  1034. * should it find an unmapped runlist element.
  1035. */
  1036. static bool check_mft_mirror(ntfs_volume *vol)
  1037. {
  1038. struct super_block *sb = vol->sb;
  1039. ntfs_inode *mirr_ni;
  1040. struct page *mft_page, *mirr_page;
  1041. u8 *kmft, *kmirr;
  1042. runlist_element *rl, rl2[2];
  1043. pgoff_t index;
  1044. int mrecs_per_page, i;
  1045. ntfs_debug("Entering.");
  1046. /* Compare contents of $MFT and $MFTMirr. */
  1047. mrecs_per_page = PAGE_CACHE_SIZE / vol->mft_record_size;
  1048. BUG_ON(!mrecs_per_page);
  1049. BUG_ON(!vol->mftmirr_size);
  1050. mft_page = mirr_page = NULL;
  1051. kmft = kmirr = NULL;
  1052. index = i = 0;
  1053. do {
  1054. u32 bytes;
  1055. /* Switch pages if necessary. */
  1056. if (!(i % mrecs_per_page)) {
  1057. if (index) {
  1058. ntfs_unmap_page(mft_page);
  1059. ntfs_unmap_page(mirr_page);
  1060. }
  1061. /* Get the $MFT page. */
  1062. mft_page = ntfs_map_page(vol->mft_ino->i_mapping,
  1063. index);
  1064. if (IS_ERR(mft_page)) {
  1065. ntfs_error(sb, "Failed to read $MFT.");
  1066. return false;
  1067. }
  1068. kmft = page_address(mft_page);
  1069. /* Get the $MFTMirr page. */
  1070. mirr_page = ntfs_map_page(vol->mftmirr_ino->i_mapping,
  1071. index);
  1072. if (IS_ERR(mirr_page)) {
  1073. ntfs_error(sb, "Failed to read $MFTMirr.");
  1074. goto mft_unmap_out;
  1075. }
  1076. kmirr = page_address(mirr_page);
  1077. ++index;
  1078. }
  1079. /* Do not check the record if it is not in use. */
  1080. if (((MFT_RECORD*)kmft)->flags & MFT_RECORD_IN_USE) {
  1081. /* Make sure the record is ok. */
  1082. if (ntfs_is_baad_recordp((le32*)kmft)) {
  1083. ntfs_error(sb, "Incomplete multi sector "
  1084. "transfer detected in mft "
  1085. "record %i.", i);
  1086. mm_unmap_out:
  1087. ntfs_unmap_page(mirr_page);
  1088. mft_unmap_out:
  1089. ntfs_unmap_page(mft_page);
  1090. return false;
  1091. }
  1092. }
  1093. /* Do not check the mirror record if it is not in use. */
  1094. if (((MFT_RECORD*)kmirr)->flags & MFT_RECORD_IN_USE) {
  1095. if (ntfs_is_baad_recordp((le32*)kmirr)) {
  1096. ntfs_error(sb, "Incomplete multi sector "
  1097. "transfer detected in mft "
  1098. "mirror record %i.", i);
  1099. goto mm_unmap_out;
  1100. }
  1101. }
  1102. /* Get the amount of data in the current record. */
  1103. bytes = le32_to_cpu(((MFT_RECORD*)kmft)->bytes_in_use);
  1104. if (bytes < sizeof(MFT_RECORD_OLD) ||
  1105. bytes > vol->mft_record_size ||
  1106. ntfs_is_baad_recordp((le32*)kmft)) {
  1107. bytes = le32_to_cpu(((MFT_RECORD*)kmirr)->bytes_in_use);
  1108. if (bytes < sizeof(MFT_RECORD_OLD) ||
  1109. bytes > vol->mft_record_size ||
  1110. ntfs_is_baad_recordp((le32*)kmirr))
  1111. bytes = vol->mft_record_size;
  1112. }
  1113. /* Compare the two records. */
  1114. if (memcmp(kmft, kmirr, bytes)) {
  1115. ntfs_error(sb, "$MFT and $MFTMirr (record %i) do not "
  1116. "match. Run ntfsfix or chkdsk.", i);
  1117. goto mm_unmap_out;
  1118. }
  1119. kmft += vol->mft_record_size;
  1120. kmirr += vol->mft_record_size;
  1121. } while (++i < vol->mftmirr_size);
  1122. /* Release the last pages. */
  1123. ntfs_unmap_page(mft_page);
  1124. ntfs_unmap_page(mirr_page);
  1125. /* Construct the mft mirror runlist by hand. */
  1126. rl2[0].vcn = 0;
  1127. rl2[0].lcn = vol->mftmirr_lcn;
  1128. rl2[0].length = (vol->mftmirr_size * vol->mft_record_size +
  1129. vol->cluster_size - 1) / vol->cluster_size;
  1130. rl2[1].vcn = rl2[0].length;
  1131. rl2[1].lcn = LCN_ENOENT;
  1132. rl2[1].length = 0;
  1133. /*
  1134. * Because we have just read all of the mft mirror, we know we have
  1135. * mapped the full runlist for it.
  1136. */
  1137. mirr_ni = NTFS_I(vol->mftmirr_ino);
  1138. down_read(&mirr_ni->runlist.lock);
  1139. rl = mirr_ni->runlist.rl;
  1140. /* Compare the two runlists. They must be identical. */
  1141. i = 0;
  1142. do {
  1143. if (rl2[i].vcn != rl[i].vcn || rl2[i].lcn != rl[i].lcn ||
  1144. rl2[i].length != rl[i].length) {
  1145. ntfs_error(sb, "$MFTMirr location mismatch. "
  1146. "Run chkdsk.");
  1147. up_read(&mirr_ni->runlist.lock);
  1148. return false;
  1149. }
  1150. } while (rl2[i++].length);
  1151. up_read(&mirr_ni->runlist.lock);
  1152. ntfs_debug("Done.");
  1153. return true;
  1154. }
  1155. /**
  1156. * load_and_check_logfile - load and check the logfile inode for a volume
  1157. * @vol: ntfs super block describing device whose logfile to load
  1158. *
  1159. * Return 'true' on success or 'false' on error.
  1160. */
  1161. static bool load_and_check_logfile(ntfs_volume *vol,
  1162. RESTART_PAGE_HEADER **rp)
  1163. {
  1164. struct inode *tmp_ino;
  1165. ntfs_debug("Entering.");
  1166. tmp_ino = ntfs_iget(vol->sb, FILE_LogFile);
  1167. if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
  1168. if (!IS_ERR(tmp_ino))
  1169. iput(tmp_ino);
  1170. /* Caller will display error message. */
  1171. return false;
  1172. }
  1173. if (!ntfs_check_logfile(tmp_ino, rp)) {
  1174. iput(tmp_ino);
  1175. /* ntfs_check_logfile() will have displayed error output. */
  1176. return false;
  1177. }
  1178. NInoSetSparseDisabled(NTFS_I(tmp_ino));
  1179. vol->logfile_ino = tmp_ino;
  1180. ntfs_debug("Done.");
  1181. return true;
  1182. }
  1183. #define NTFS_HIBERFIL_HEADER_SIZE 4096
  1184. /**
  1185. * check_windows_hibernation_status - check if Windows is suspended on a volume
  1186. * @vol: ntfs super block of device to check
  1187. *
  1188. * Check if Windows is hibernated on the ntfs volume @vol. This is done by
  1189. * looking for the file hiberfil.sys in the root directory of the volume. If
  1190. * the file is not present Windows is definitely not suspended.
  1191. *
  1192. * If hiberfil.sys exists and is less than 4kiB in size it means Windows is
  1193. * definitely suspended (this volume is not the system volume). Caveat: on a
  1194. * system with many volumes it is possible that the < 4kiB check is bogus but
  1195. * for now this should do fine.
  1196. *
  1197. * If hiberfil.sys exists and is larger than 4kiB in size, we need to read the
  1198. * hiberfil header (which is the first 4kiB). If this begins with "hibr",
  1199. * Windows is definitely suspended. If it is completely full of zeroes,
  1200. * Windows is definitely not hibernated. Any other case is treated as if
  1201. * Windows is suspended. This caters for the above mentioned caveat of a
  1202. * system with many volumes where no "hibr" magic would be present and there is
  1203. * no zero header.
  1204. *
  1205. * Return 0 if Windows is not hibernated on the volume, >0 if Windows is
  1206. * hibernated on the volume, and -errno on error.
  1207. */
  1208. static int check_windows_hibernation_status(ntfs_volume *vol)
  1209. {
  1210. MFT_REF mref;
  1211. struct inode *vi;
  1212. ntfs_inode *ni;
  1213. struct page *page;
  1214. u32 *kaddr, *kend;
  1215. ntfs_name *name = NULL;
  1216. int ret = 1;
  1217. static const ntfschar hiberfil[13] = { cpu_to_le16('h'),
  1218. cpu_to_le16('i'), cpu_to_le16('b'),
  1219. cpu_to_le16('e'), cpu_to_le16('r'),
  1220. cpu_to_le16('f'), cpu_to_le16('i'),
  1221. cpu_to_le16('l'), cpu_to_le16('.'),
  1222. cpu_to_le16('s'), cpu_to_le16('y'),
  1223. cpu_to_le16('s'), 0 };
  1224. ntfs_debug("Entering.");
  1225. /*
  1226. * Find the inode number for the hibernation file by looking up the
  1227. * filename hiberfil.sys in the root directory.
  1228. */
  1229. mutex_lock(&vol->root_ino->i_mutex);
  1230. mref = ntfs_lookup_inode_by_name(NTFS_I(vol->root_ino), hiberfil, 12,
  1231. &name);
  1232. mutex_unlock(&vol->root_ino->i_mutex);
  1233. if (IS_ERR_MREF(mref)) {
  1234. ret = MREF_ERR(mref);
  1235. /* If the file does not exist, Windows is not hibernated. */
  1236. if (ret == -ENOENT) {
  1237. ntfs_debug("hiberfil.sys not present. Windows is not "
  1238. "hibernated on the volume.");
  1239. return 0;
  1240. }
  1241. /* A real error occured. */
  1242. ntfs_error(vol->sb, "Failed to find inode number for "
  1243. "hiberfil.sys.");
  1244. return ret;
  1245. }
  1246. /* We do not care for the type of match that was found. */
  1247. kfree(name);
  1248. /* Get the inode. */
  1249. vi = ntfs_iget(vol->sb, MREF(mref));
  1250. if (IS_ERR(vi) || is_bad_inode(vi)) {
  1251. if (!IS_ERR(vi))
  1252. iput(vi);
  1253. ntfs_error(vol->sb, "Failed to load hiberfil.sys.");
  1254. return IS_ERR(vi) ? PTR_ERR(vi) : -EIO;
  1255. }
  1256. if (unlikely(i_size_read(vi) < NTFS_HIBERFIL_HEADER_SIZE)) {
  1257. ntfs_debug("hiberfil.sys is smaller than 4kiB (0x%llx). "
  1258. "Windows is hibernated on the volume. This "
  1259. "is not the system volume.", i_size_read(vi));
  1260. goto iput_out;
  1261. }
  1262. ni = NTFS_I(vi);
  1263. page = ntfs_map_page(vi->i_mapping, 0);
  1264. if (IS_ERR(page)) {
  1265. ntfs_error(vol->sb, "Failed to read from hiberfil.sys.");
  1266. ret = PTR_ERR(page);
  1267. goto iput_out;
  1268. }
  1269. kaddr = (u32*)page_address(page);
  1270. if (*(le32*)kaddr == cpu_to_le32(0x72626968)/*'hibr'*/) {
  1271. ntfs_debug("Magic \"hibr\" found in hiberfil.sys. Windows is "
  1272. "hibernated on the volume. This is the "
  1273. "system volume.");
  1274. goto unm_iput_out;
  1275. }
  1276. kend = kaddr + NTFS_HIBERFIL_HEADER_SIZE/sizeof(*kaddr);
  1277. do {
  1278. if (unlikely(*kaddr)) {
  1279. ntfs_debug("hiberfil.sys is larger than 4kiB "
  1280. "(0x%llx), does not contain the "
  1281. "\"hibr\" magic, and does not have a "
  1282. "zero header. Windows is hibernated "
  1283. "on the volume. This is not the "
  1284. "system volume.", i_size_read(vi));
  1285. goto unm_iput_out;
  1286. }
  1287. } while (++kaddr < kend);
  1288. ntfs_debug("hiberfil.sys contains a zero header. Windows is not "
  1289. "hibernated on the volume. This is the system "
  1290. "volume.");
  1291. ret = 0;
  1292. unm_iput_out:
  1293. ntfs_unmap_page(page);
  1294. iput_out:
  1295. iput(vi);
  1296. return ret;
  1297. }
  1298. /**
  1299. * load_and_init_quota - load and setup the quota file for a volume if present
  1300. * @vol: ntfs super block describing device whose quota file to load
  1301. *
  1302. * Return 'true' on success or 'false' on error. If $Quota is not present, we
  1303. * leave vol->quota_ino as NULL and return success.
  1304. */
  1305. static bool load_and_init_quota(ntfs_volume *vol)
  1306. {
  1307. MFT_REF mref;
  1308. struct inode *tmp_ino;
  1309. ntfs_name *name = NULL;
  1310. static const ntfschar Quota[7] = { cpu_to_le16('$'),
  1311. cpu_to_le16('Q'), cpu_to_le16('u'),
  1312. cpu_to_le16('o'), cpu_to_le16('t'),
  1313. cpu_to_le16('a'), 0 };
  1314. static ntfschar Q[3] = { cpu_to_le16('$'),
  1315. cpu_to_le16('Q'), 0 };
  1316. ntfs_debug("Entering.");
  1317. /*
  1318. * Find the inode number for the quota file by looking up the filename
  1319. * $Quota in the extended system files directory $Extend.
  1320. */
  1321. mutex_lock(&vol->extend_ino->i_mutex);
  1322. mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), Quota, 6,
  1323. &name);
  1324. mutex_unlock(&vol->extend_ino->i_mutex);
  1325. if (IS_ERR_MREF(mref)) {
  1326. /*
  1327. * If the file does not exist, quotas are disabled and have
  1328. * never been enabled on this volume, just return success.
  1329. */
  1330. if (MREF_ERR(mref) == -ENOENT) {
  1331. ntfs_debug("$Quota not present. Volume does not have "
  1332. "quotas enabled.");
  1333. /*
  1334. * No need to try to set quotas out of date if they are
  1335. * not enabled.
  1336. */
  1337. NVolSetQuotaOutOfDate(vol);
  1338. return true;
  1339. }
  1340. /* A real error occured. */
  1341. ntfs_error(vol->sb, "Failed to find inode number for $Quota.");
  1342. return false;
  1343. }
  1344. /* We do not care for the type of match that was found. */
  1345. kfree(name);
  1346. /* Get the inode. */
  1347. tmp_ino = ntfs_iget(vol->sb, MREF(mref));
  1348. if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
  1349. if (!IS_ERR(tmp_ino))
  1350. iput(tmp_ino);
  1351. ntfs_error(vol->sb, "Failed to load $Quota.");
  1352. return false;
  1353. }
  1354. vol->quota_ino = tmp_ino;
  1355. /* Get the $Q index allocation attribute. */
  1356. tmp_ino = ntfs_index_iget(vol->quota_ino, Q, 2);
  1357. if (IS_ERR(tmp_ino)) {
  1358. ntfs_error(vol->sb, "Failed to load $Quota/$Q index.");
  1359. return false;
  1360. }
  1361. vol->quota_q_ino = tmp_ino;
  1362. ntfs_debug("Done.");
  1363. return true;
  1364. }
  1365. /**
  1366. * load_and_init_usnjrnl - load and setup the transaction log if present
  1367. * @vol: ntfs super block describing device whose usnjrnl file to load
  1368. *
  1369. * Return 'true' on success or 'false' on error.
  1370. *
  1371. * If $UsnJrnl is not present or in the process of being disabled, we set
  1372. * NVolUsnJrnlStamped() and return success.
  1373. *
  1374. * If the $UsnJrnl $DATA/$J attribute has a size equal to the lowest valid usn,
  1375. * i.e. transaction logging has only just been enabled or the journal has been
  1376. * stamped and nothing has been logged since, we also set NVolUsnJrnlStamped()
  1377. * and return success.
  1378. */
  1379. static bool load_and_init_usnjrnl(ntfs_volume *vol)
  1380. {
  1381. MFT_REF mref;
  1382. struct inode *tmp_ino;
  1383. ntfs_inode *tmp_ni;
  1384. struct page *page;
  1385. ntfs_name *name = NULL;
  1386. USN_HEADER *uh;
  1387. static const ntfschar UsnJrnl[9] = { cpu_to_le16('$'),
  1388. cpu_to_le16('U'), cpu_to_le16('s'),
  1389. cpu_to_le16('n'), cpu_to_le16('J'),
  1390. cpu_to_le16('r'), cpu_to_le16('n'),
  1391. cpu_to_le16('l'), 0 };
  1392. static ntfschar Max[5] = { cpu_to_le16('$'),
  1393. cpu_to_le16('M'), cpu_to_le16('a'),
  1394. cpu_to_le16('x'), 0 };
  1395. static ntfschar J[3] = { cpu_to_le16('$'),
  1396. cpu_to_le16('J'), 0 };
  1397. ntfs_debug("Entering.");
  1398. /*
  1399. * Find the inode number for the transaction log file by looking up the
  1400. * filename $UsnJrnl in the extended system files directory $Extend.
  1401. */
  1402. mutex_lock(&vol->extend_ino->i_mutex);
  1403. mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), UsnJrnl, 8,
  1404. &name);
  1405. mutex_unlock(&vol->extend_ino->i_mutex);
  1406. if (IS_ERR_MREF(mref)) {
  1407. /*
  1408. * If the file does not exist, transaction logging is disabled,
  1409. * just return success.
  1410. */
  1411. if (MREF_ERR(mref) == -ENOENT) {
  1412. ntfs_debug("$UsnJrnl not present. Volume does not "
  1413. "have transaction logging enabled.");
  1414. not_enabled:
  1415. /*
  1416. * No need to try to stamp the transaction log if
  1417. * transaction logging is not enabled.
  1418. */
  1419. NVolSetUsnJrnlStamped(vol);
  1420. return true;
  1421. }
  1422. /* A real error occured. */
  1423. ntfs_error(vol->sb, "Failed to find inode number for "
  1424. "$UsnJrnl.");
  1425. return false;
  1426. }
  1427. /* We do not care for the type of match that was found. */
  1428. kfree(name);
  1429. /* Get the inode. */
  1430. tmp_ino = ntfs_iget(vol->sb, MREF(mref));
  1431. if (unlikely(IS_ERR(tmp_ino) || is_bad_inode(tmp_ino))) {
  1432. if (!IS_ERR(tmp_ino))
  1433. iput(tmp_ino);
  1434. ntfs_error(vol->sb, "Failed to load $UsnJrnl.");
  1435. return false;
  1436. }
  1437. vol->usnjrnl_ino = tmp_ino;
  1438. /*
  1439. * If the transaction log is in the process of being deleted, we can
  1440. * ignore it.
  1441. */
  1442. if (unlikely(vol->vol_flags & VOLUME_DELETE_USN_UNDERWAY)) {
  1443. ntfs_debug("$UsnJrnl in the process of being disabled. "
  1444. "Volume does not have transaction logging "
  1445. "enabled.");
  1446. goto not_enabled;
  1447. }
  1448. /* Get the $DATA/$Max attribute. */
  1449. tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, Max, 4);
  1450. if (IS_ERR(tmp_ino)) {
  1451. ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$Max "
  1452. "attribute.");
  1453. return false;
  1454. }
  1455. vol->usnjrnl_max_ino = tmp_ino;
  1456. if (unlikely(i_size_read(tmp_ino) < sizeof(USN_HEADER))) {
  1457. ntfs_error(vol->sb, "Found corrupt $UsnJrnl/$DATA/$Max "
  1458. "attribute (size is 0x%llx but should be at "
  1459. "least 0x%zx bytes).", i_size_read(tmp_ino),
  1460. sizeof(USN_HEADER));
  1461. return false;
  1462. }
  1463. /* Get the $DATA/$J attribute. */
  1464. tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, J, 2);
  1465. if (IS_ERR(tmp_ino)) {
  1466. ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$J "
  1467. "attribute.");
  1468. return false;
  1469. }
  1470. vol->usnjrnl_j_ino = tmp_ino;
  1471. /* Verify $J is non-resident and sparse. */
  1472. tmp_ni = NTFS_I(vol->usnjrnl_j_ino);
  1473. if (unlikely(!NInoNonResident(tmp_ni) || !NInoSparse(tmp_ni))) {
  1474. ntfs_error(vol->sb, "$UsnJrnl/$DATA/$J attribute is resident "
  1475. "and/or not sparse.");
  1476. return false;
  1477. }
  1478. /* Read the USN_HEADER from $DATA/$Max. */
  1479. page = ntfs_map_page(vol->usnjrnl_max_ino->i_mapping, 0);
  1480. if (IS_ERR(page)) {
  1481. ntfs_error(vol->sb, "Failed to read from $UsnJrnl/$DATA/$Max "
  1482. "attribute.");
  1483. return false;
  1484. }
  1485. uh = (USN_HEADER*)page_address(page);
  1486. /* Sanity check the $Max. */
  1487. if (unlikely(sle64_to_cpu(uh->allocation_delta) >
  1488. sle64_to_cpu(uh->maximum_size))) {
  1489. ntfs_error(vol->sb, "Allocation delta (0x%llx) exceeds "
  1490. "maximum size (0x%llx). $UsnJrnl is corrupt.",
  1491. (long long)sle64_to_cpu(uh->allocation_delta),
  1492. (long long)sle64_to_cpu(uh->maximum_size));
  1493. ntfs_unmap_page(page);
  1494. return false;
  1495. }
  1496. /*
  1497. * If the transaction log has been stamped and nothing has been written
  1498. * to it since, we do not need to stamp it.
  1499. */
  1500. if (unlikely(sle64_to_cpu(uh->lowest_valid_usn) >=
  1501. i_size_read(vol->usnjrnl_j_ino))) {
  1502. if (likely(sle64_to_cpu(uh->lowest_valid_usn) ==
  1503. i_size_read(vol->usnjrnl_j_ino))) {
  1504. ntfs_unmap_page(page);
  1505. ntfs_debug("$UsnJrnl is enabled but nothing has been "
  1506. "logged since it was last stamped. "
  1507. "Treating this as if the volume does "
  1508. "not have transaction logging "
  1509. "enabled.");
  1510. goto not_enabled;
  1511. }
  1512. ntfs_error(vol->sb, "$UsnJrnl has lowest valid usn (0x%llx) "
  1513. "which is out of bounds (0x%llx). $UsnJrnl "
  1514. "is corrupt.",
  1515. (long long)sle64_to_cpu(uh->lowest_valid_usn),
  1516. i_size_read(vol->usnjrnl_j_ino));
  1517. ntfs_unmap_page(page);
  1518. return false;
  1519. }
  1520. ntfs_unmap_page(page);
  1521. ntfs_debug("Done.");
  1522. return true;
  1523. }
  1524. /**
  1525. * load_and_init_attrdef - load the attribute definitions table for a volume
  1526. * @vol: ntfs super block describing device whose attrdef to load
  1527. *
  1528. * Return 'true' on success or 'false' on error.
  1529. */
  1530. static bool load_and_init_attrdef(ntfs_volume *vol)
  1531. {
  1532. loff_t i_size;
  1533. struct super_block *sb = vol->sb;
  1534. struct inode *ino;
  1535. struct page *page;
  1536. pgoff_t index, max_index;
  1537. unsigned int size;
  1538. ntfs_debug("Entering.");
  1539. /* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */
  1540. ino = ntfs_iget(sb, FILE_AttrDef);
  1541. if (IS_ERR(ino) || is_bad_inode(ino)) {
  1542. if (!IS_ERR(ino))
  1543. iput(ino);
  1544. goto failed;
  1545. }
  1546. NInoSetSparseDisabled(NTFS_I(ino));
  1547. /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
  1548. i_size = i_size_read(ino);
  1549. if (i_size <= 0 || i_size > 0x7fffffff)
  1550. goto iput_failed;
  1551. vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(i_size);
  1552. if (!vol->attrdef)
  1553. goto iput_failed;
  1554. index = 0;
  1555. max_index = i_size >> PAGE_CACHE_SHIFT;
  1556. size = PAGE_CACHE_SIZE;
  1557. while (index < max_index) {
  1558. /* Read the attrdef table and copy it into the linear buffer. */
  1559. read_partial_attrdef_page:
  1560. page = ntfs_map_page(ino->i_mapping, index);
  1561. if (IS_ERR(page))
  1562. goto free_iput_failed;
  1563. memcpy((u8*)vol->attrdef + (index++ << PAGE_CACHE_SHIFT),
  1564. page_address(page), size);
  1565. ntfs_unmap_page(page);
  1566. };
  1567. if (size == PAGE_CACHE_SIZE) {
  1568. size = i_size & ~PAGE_CACHE_MASK;
  1569. if (size)
  1570. goto read_partial_attrdef_page;
  1571. }
  1572. vol->attrdef_size = i_size;
  1573. ntfs_debug("Read %llu bytes from $AttrDef.", i_size);
  1574. iput(ino);
  1575. return true;
  1576. free_iput_failed:
  1577. ntfs_free(vol->attrdef);
  1578. vol->attrdef = NULL;
  1579. iput_failed:
  1580. iput(ino);
  1581. failed:
  1582. ntfs_error(sb, "Failed to initialize attribute definition table.");
  1583. return false;
  1584. }
  1585. #endif /* NTFS_RW */
  1586. /**
  1587. * load_and_init_upcase - load the upcase table for an ntfs volume
  1588. * @vol: ntfs super block describing device whose upcase to load
  1589. *
  1590. * Return 'true' on success or 'false' on error.
  1591. */
  1592. static bool load_and_init_upcase(ntfs_volume *vol)
  1593. {
  1594. loff_t i_size;
  1595. struct super_block *sb = vol->sb;
  1596. struct inode *ino;
  1597. struct page *page;
  1598. pgoff_t index, max_index;
  1599. unsigned int size;
  1600. int i, max;
  1601. ntfs_debug("Entering.");
  1602. /* Read upcase table and setup vol->upcase and vol->upcase_len. */
  1603. ino = ntfs_iget(sb, FILE_UpCase);
  1604. if (IS_ERR(ino) || is_bad_inode(ino)) {
  1605. if (!IS_ERR(ino))
  1606. iput(ino);
  1607. goto upcase_failed;
  1608. }
  1609. /*
  1610. * The upcase size must not be above 64k Unicode characters, must not
  1611. * be zero and must be a multiple of sizeof(ntfschar).
  1612. */
  1613. i_size = i_size_read(ino);
  1614. if (!i_size || i_size & (sizeof(ntfschar) - 1) ||
  1615. i_size > 64ULL * 1024 * sizeof(ntfschar))
  1616. goto iput_upcase_failed;
  1617. vol->upcase = (ntfschar*)ntfs_malloc_nofs(i_size);
  1618. if (!vol->upcase)
  1619. goto iput_upcase_failed;
  1620. index = 0;
  1621. max_index = i_size >> PAGE_CACHE_SHIFT;
  1622. size = PAGE_CACHE_SIZE;
  1623. while (index < max_index) {
  1624. /* Read the upcase table and copy it into the linear buffer. */
  1625. read_partial_upcase_page:
  1626. page = ntfs_map_page(ino->i_mapping, index);
  1627. if (IS_ERR(page))
  1628. goto iput_upcase_failed;
  1629. memcpy((char*)vol->upcase + (index++ << PAGE_CACHE_SHIFT),
  1630. page_address(page), size);
  1631. ntfs_unmap_page(page);
  1632. };
  1633. if (size == PAGE_CACHE_SIZE) {
  1634. size = i_size & ~PAGE_CACHE_MASK;
  1635. if (size)
  1636. goto read_partial_upcase_page;
  1637. }
  1638. vol->upcase_len = i_size >> UCHAR_T_SIZE_BITS;
  1639. ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
  1640. i_size, 64 * 1024 * sizeof(ntfschar));
  1641. iput(ino);
  1642. mutex_lock(&ntfs_lock);
  1643. if (!default_upcase) {
  1644. ntfs_debug("Using volume specified $UpCase since default is "
  1645. "not present.");
  1646. mutex_unlock(&ntfs_lock);
  1647. return true;
  1648. }
  1649. max = default_upcase_len;
  1650. if (max > vol->upcase_len)
  1651. max = vol->upcase_len;
  1652. for (i = 0; i < max; i++)
  1653. if (vol->upcase[i] != default_upcase[i])
  1654. break;
  1655. if (i == max) {
  1656. ntfs_free(vol->upcase);
  1657. vol->upcase = default_upcase;
  1658. vol->upcase_len = max;
  1659. ntfs_nr_upcase_users++;
  1660. mutex_unlock(&ntfs_lock);
  1661. ntfs_debug("Volume specified $UpCase matches default. Using "
  1662. "default.");
  1663. return true;
  1664. }
  1665. mutex_unlock(&ntfs_lock);
  1666. ntfs_debug("Using volume specified $UpCase since it does not match "
  1667. "the default.");
  1668. return true;
  1669. iput_upcase_failed:
  1670. iput(ino);
  1671. ntfs_free(vol->upcase);
  1672. vol->upcase = NULL;
  1673. upcase_failed:
  1674. mutex_lock(&ntfs_lock);
  1675. if (default_upcase) {
  1676. vol->upcase = default_upcase;
  1677. vol->upcase_len = default_upcase_len;
  1678. ntfs_nr_upcase_users++;
  1679. mutex_unlock(&ntfs_lock);
  1680. ntfs_error(sb, "Failed to load $UpCase from the volume. Using "
  1681. "default.");
  1682. return true;
  1683. }
  1684. mutex_unlock(&ntfs_lock);
  1685. ntfs_error(sb, "Failed to initialize upcase table.");
  1686. return false;
  1687. }
  1688. /*
  1689. * The lcn and mft bitmap inodes are NTFS-internal inodes with
  1690. * their own special locking rules:
  1691. */
  1692. static struct lock_class_key
  1693. lcnbmp_runlist_lock_key, lcnbmp_mrec_lock_key,
  1694. mftbmp_runlist_lock_key, mftbmp_mrec_lock_key;
  1695. /**
  1696. * load_system_files - open the system files using normal functions
  1697. * @vol: ntfs super block describing device whose system files to load
  1698. *
  1699. * Open the system files with normal access functions and complete setting up
  1700. * the ntfs super block @vol.
  1701. *
  1702. * Return 'true' on success or 'false' on error.
  1703. */
  1704. static bool load_system_files(ntfs_volume *vol)
  1705. {
  1706. struct super_block *sb = vol->sb;
  1707. MFT_RECORD *m;
  1708. VOLUME_INFORMATION *vi;
  1709. ntfs_attr_search_ctx *ctx;
  1710. #ifdef NTFS_RW
  1711. RESTART_PAGE_HEADER *rp;
  1712. int err;
  1713. #endif /* NTFS_RW */
  1714. ntfs_debug("Entering.");
  1715. #ifdef NTFS_RW
  1716. /* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */
  1717. if (!load_and_init_mft_mirror(vol) || !check_mft_mirror(vol)) {
  1718. static const char *es1 = "Failed to load $MFTMirr";
  1719. static const char *es2 = "$MFTMirr does not match $MFT";
  1720. static const char *es3 = ". Run ntfsfix and/or chkdsk.";
  1721. /* If a read-write mount, convert it to a read-only mount. */
  1722. if (!(sb->s_flags & MS_RDONLY)) {
  1723. if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
  1724. ON_ERRORS_CONTINUE))) {
  1725. ntfs_error(sb, "%s and neither on_errors="
  1726. "continue nor on_errors="
  1727. "remount-ro was specified%s",
  1728. !vol->mftmirr_ino ? es1 : es2,
  1729. es3);
  1730. goto iput_mirr_err_out;
  1731. }
  1732. sb->s_flags |= MS_RDONLY;
  1733. ntfs_error(sb, "%s. Mounting read-only%s",
  1734. !vol->mftmirr_ino ? es1 : es2, es3);
  1735. } else
  1736. ntfs_warning(sb, "%s. Will not be able to remount "
  1737. "read-write%s",
  1738. !vol->mftmirr_ino ? es1 : es2, es3);
  1739. /* This will prevent a read-write remount. */
  1740. NVolSetErrors(vol);
  1741. }
  1742. #endif /* NTFS_RW */
  1743. /* Get mft bitmap attribute inode. */
  1744. vol->mftbmp_ino = ntfs_attr_iget(vol->mft_ino, AT_BITMAP, NULL, 0);
  1745. if (IS_ERR(vol->mftbmp_ino)) {
  1746. ntfs_error(sb, "Failed to load $MFT/$BITMAP attribute.");
  1747. goto iput_mirr_err_out;
  1748. }
  1749. lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->runlist.lock,
  1750. &mftbmp_runlist_lock_key);
  1751. lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->mrec_lock,
  1752. &mftbmp_mrec_lock_key);
  1753. /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */
  1754. if (!load_and_init_upcase(vol))
  1755. goto iput_mftbmp_err_out;
  1756. #ifdef NTFS_RW
  1757. /*
  1758. * Read attribute definitions table and setup @vol->attrdef and
  1759. * @vol->attrdef_size.
  1760. */
  1761. if (!load_and_init_attrdef(vol))
  1762. goto iput_upcase_err_out;
  1763. #endif /* NTFS_RW */
  1764. /*
  1765. * Get the cluster allocation bitmap inode and verify the size, no
  1766. * need for any locking at this stage as we are already running
  1767. * exclusively as we are mount in progress task.
  1768. */
  1769. vol->lcnbmp_ino = ntfs_iget(sb, FILE_Bitmap);
  1770. if (IS_ERR(vol->lcnbmp_ino) || is_bad_inode(vol->lcnbmp_ino)) {
  1771. if (!IS_ERR(vol->lcnbmp_ino))
  1772. iput(vol->lcnbmp_ino);
  1773. goto bitmap_failed;
  1774. }
  1775. lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->runlist.lock,
  1776. &lcnbmp_runlist_lock_key);
  1777. lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->mrec_lock,
  1778. &lcnbmp_mrec_lock_key);
  1779. NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino));
  1780. if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) {
  1781. iput(vol->lcnbmp_ino);
  1782. bitmap_failed:
  1783. ntfs_error(sb, "Failed to load $Bitmap.");
  1784. goto iput_attrdef_err_out;
  1785. }
  1786. /*
  1787. * Get the volume inode and setup our cache of the volume flags and
  1788. * version.
  1789. */
  1790. vol->vol_ino = ntfs_iget(sb, FILE_Volume);
  1791. if (IS_ERR(vol->vol_ino) || is_bad_inode(vol->vol_ino)) {
  1792. if (!IS_ERR(vol->vol_ino))
  1793. iput(vol->vol_ino);
  1794. volume_failed:
  1795. ntfs_error(sb, "Failed to load $Volume.");
  1796. goto iput_lcnbmp_err_out;
  1797. }
  1798. m = map_mft_record(NTFS_I(vol->vol_ino));
  1799. if (IS_ERR(m)) {
  1800. iput_volume_failed:
  1801. iput(vol->vol_ino);
  1802. goto volume_failed;
  1803. }
  1804. if (!(ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m))) {
  1805. ntfs_error(sb, "Failed to get attribute search context.");
  1806. goto get_ctx_vol_failed;
  1807. }
  1808. if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
  1809. ctx) || ctx->attr->non_resident || ctx->attr->flags) {
  1810. err_put_vol:
  1811. ntfs_attr_put_search_ctx(ctx);
  1812. get_ctx_vol_failed:
  1813. unmap_mft_record(NTFS_I(vol->vol_ino));
  1814. goto iput_volume_failed;
  1815. }
  1816. vi = (VOLUME_INFORMATION*)((char*)ctx->attr +
  1817. le16_to_cpu(ctx->attr->data.resident.value_offset));
  1818. /* Some bounds checks. */
  1819. if ((u8*)vi < (u8*)ctx->attr || (u8*)vi +
  1820. le32_to_cpu(ctx->attr->data.resident.value_length) >
  1821. (u8*)ctx->attr + le32_to_cpu(ctx->attr->length))
  1822. goto err_put_vol;
  1823. /* Copy the volume flags and version to the ntfs_volume structure. */
  1824. vol->vol_flags = vi->flags;
  1825. vol->major_ver = vi->major_ver;
  1826. vol->minor_ver = vi->minor_ver;
  1827. ntfs_attr_put_search_ctx(ctx);
  1828. unmap_mft_record(NTFS_I(vol->vol_ino));
  1829. printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver,
  1830. vol->minor_ver);
  1831. if (vol->major_ver < 3 && NVolSparseEnabled(vol)) {
  1832. ntfs_warning(vol->sb, "Disabling sparse support due to NTFS "
  1833. "volume version %i.%i (need at least version "
  1834. "3.0).", vol->major_ver, vol->minor_ver);
  1835. NVolClearSparseEnabled(vol);
  1836. }
  1837. #ifdef NTFS_RW
  1838. /* Make sure that no unsupported volume flags are set. */
  1839. if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
  1840. static const char *es1a = "Volume is dirty";
  1841. static const char *es1b = "Volume has been modified by chkdsk";
  1842. static const char *es1c = "Volume has unsupported flags set";
  1843. static const char *es2a = ". Run chkdsk and mount in Windows.";
  1844. static const char *es2b = ". Mount in Windows.";
  1845. const char *es1, *es2;
  1846. es2 = es2a;
  1847. if (vol->vol_flags & VOLUME_IS_DIRTY)
  1848. es1 = es1a;
  1849. else if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
  1850. es1 = es1b;
  1851. es2 = es2b;
  1852. } else {
  1853. es1 = es1c;
  1854. ntfs_warning(sb, "Unsupported volume flags 0x%x "
  1855. "encountered.",
  1856. (unsigned)le16_to_cpu(vol->vol_flags));
  1857. }
  1858. /* If a read-write mount, convert it to a read-only mount. */
  1859. if (!(sb->s_flags & MS_RDONLY)) {
  1860. if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
  1861. ON_ERRORS_CONTINUE))) {
  1862. ntfs_error(sb, "%s and neither on_errors="
  1863. "continue nor on_errors="
  1864. "remount-ro was specified%s",
  1865. es1, es2);
  1866. goto iput_vol_err_out;
  1867. }
  1868. sb->s_flags |= MS_RDONLY;
  1869. ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
  1870. } else
  1871. ntfs_warning(sb, "%s. Will not be able to remount "
  1872. "read-write%s", es1, es2);
  1873. /*
  1874. * Do not set NVolErrors() because ntfs_remount() re-checks the
  1875. * flags which we need to do in case any flags have changed.
  1876. */
  1877. }
  1878. /*
  1879. * Get the inode for the logfile, check it and determine if the volume
  1880. * was shutdown cleanly.
  1881. */
  1882. rp = NULL;
  1883. if (!load_and_check_logfile(vol, &rp) ||
  1884. !ntfs_is_logfile_clean(vol->logfile_ino, rp)) {
  1885. static const char *es1a = "Failed to load $LogFile";
  1886. static const char *es1b = "$LogFile is not clean";
  1887. static const char *es2 = ". Mount in Windows.";
  1888. const char *es1;
  1889. es1 = !vol->logfile_ino ? es1a : es1b;
  1890. /* If a read-write mount, convert it to a read-only mount. */
  1891. if (!(sb->s_flags & MS_RDONLY)) {
  1892. if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
  1893. ON_ERRORS_CONTINUE))) {
  1894. ntfs_error(sb, "%s and neither on_errors="
  1895. "continue nor on_errors="
  1896. "remount-ro was specified%s",
  1897. es1, es2);
  1898. if (vol->logfile_ino) {
  1899. BUG_ON(!rp);
  1900. ntfs_free(rp);
  1901. }
  1902. goto iput_logfile_err_out;
  1903. }
  1904. sb->s_flags |= MS_RDONLY;
  1905. ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
  1906. } else
  1907. ntfs_warning(sb, "%s. Will not be able to remount "
  1908. "read-write%s", es1, es2);
  1909. /* This will prevent a read-write remount. */
  1910. NVolSetErrors(vol);
  1911. }
  1912. ntfs_free(rp);
  1913. #endif /* NTFS_RW */
  1914. /* Get the root directory inode so we can do path lookups. */
  1915. vol->root_ino = ntfs_iget(sb, FILE_root);
  1916. if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) {
  1917. if (!IS_ERR(vol->root_ino))
  1918. iput(vol->root_ino);
  1919. ntfs_error(sb, "Failed to load root directory.");
  1920. goto iput_logfile_err_out;
  1921. }
  1922. #ifdef NTFS_RW
  1923. /*
  1924. * Check if Windows is suspended to disk on the target volume. If it
  1925. * is hibernated, we must not write *anything* to the disk so set
  1926. * NVolErrors() without setting the dirty volume flag and mount
  1927. * read-only. This will prevent read-write remounting and it will also
  1928. * prevent all writes.
  1929. */
  1930. err = check_windows_hibernation_status(vol);
  1931. if (unlikely(err)) {
  1932. static const char *es1a = "Failed to determine if Windows is "
  1933. "hibernated";
  1934. static const char *es1b = "Windows is hibernated";
  1935. static const char *es2 = ". Run chkdsk.";
  1936. const char *es1;
  1937. es1 = err < 0 ? es1a : es1b;
  1938. /* If a read-write mount, convert it to a read-only mount. */
  1939. if (!(sb->s_flags & MS_RDONLY)) {
  1940. if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
  1941. ON_ERRORS_CONTINUE))) {
  1942. ntfs_error(sb, "%s and neither on_errors="
  1943. "continue nor on_errors="
  1944. "remount-ro was specified%s",
  1945. es1, es2);
  1946. goto iput_root_err_out;
  1947. }
  1948. sb->s_flags |= MS_RDONLY;
  1949. ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
  1950. } else
  1951. ntfs_warning(sb, "%s. Will not be able to remount "
  1952. "read-write%s", es1, es2);
  1953. /* This will prevent a read-write remount. */
  1954. NVolSetErrors(vol);
  1955. }
  1956. /* If (still) a read-write mount, mark the volume dirty. */
  1957. if (!(sb->s_flags & MS_RDONLY) &&
  1958. ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
  1959. static const char *es1 = "Failed to set dirty bit in volume "
  1960. "information flags";
  1961. static const char *es2 = ". Run chkdsk.";
  1962. /* Convert to a read-only mount. */
  1963. if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
  1964. ON_ERRORS_CONTINUE))) {
  1965. ntfs_error(sb, "%s and neither on_errors=continue nor "
  1966. "on_errors=remount-ro was specified%s",
  1967. es1, es2);
  1968. goto iput_root_err_out;
  1969. }
  1970. ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
  1971. sb->s_flags |= MS_RDONLY;
  1972. /*
  1973. * Do not set NVolErrors() because ntfs_remount() might manage
  1974. * to set the dirty flag in which case all would be well.
  1975. */
  1976. }
  1977. #if 0
  1978. // TODO: Enable this code once we start modifying anything that is
  1979. // different between NTFS 1.2 and 3.x...
  1980. /*
  1981. * If (still) a read-write mount, set the NT4 compatibility flag on
  1982. * newer NTFS version volumes.
  1983. */
  1984. if (!(sb->s_flags & MS_RDONLY) && (vol->major_ver > 1) &&
  1985. ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
  1986. static const char *es1 = "Failed to set NT4 compatibility flag";
  1987. static const char *es2 = ". Run chkdsk.";
  1988. /* Convert to a read-only mount. */
  1989. if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
  1990. ON_ERRORS_CONTINUE))) {
  1991. ntfs_error(sb, "%s and neither on_errors=continue nor "
  1992. "on_errors=remount-ro was specified%s",
  1993. es1, es2);
  1994. goto iput_root_err_out;
  1995. }
  1996. ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
  1997. sb->s_flags |= MS_RDONLY;
  1998. NVolSetErrors(vol);
  1999. }
  2000. #endif
  2001. /* If (still) a read-write mount, empty the logfile. */
  2002. if (!(sb->s_flags & MS_RDONLY) &&
  2003. !ntfs_empty_logfile(vol->logfile_ino)) {
  2004. static const char *es1 = "Failed to empty $LogFile";
  2005. static const char *es2 = ". Mount in Windows.";
  2006. /* Convert to a read-only mount. */
  2007. if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
  2008. ON_ERRORS_CONTINUE))) {
  2009. ntfs_error(sb, "%s and neither on_errors=continue nor "
  2010. "on_errors=remount-ro was specified%s",
  2011. es1, es2);
  2012. goto iput_root_err_out;
  2013. }
  2014. ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
  2015. sb->s_flags |= MS_RDONLY;
  2016. NVolSetErrors(vol);
  2017. }
  2018. #endif /* NTFS_RW */
  2019. /* If on NTFS versions before 3.0, we are done. */
  2020. if (unlikely(vol->major_ver < 3))
  2021. return true;
  2022. /* NTFS 3.0+ specific initialization. */
  2023. /* Get the security descriptors inode. */
  2024. vol->secure_ino = ntfs_iget(sb, FILE_Secure);
  2025. if (IS_ERR(vol->secure_ino) || is_bad_inode(vol->secure_ino)) {
  2026. if (!IS_ERR(vol->secure_ino))
  2027. iput(vol->secure_ino);
  2028. ntfs_error(sb, "Failed to load $Secure.");
  2029. goto iput_root_err_out;
  2030. }
  2031. // TODO: Initialize security.
  2032. /* Get the extended system files' directory inode. */
  2033. vol->extend_ino = ntfs_iget(sb, FILE_Extend);
  2034. if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) {
  2035. if (!IS_ERR(vol->extend_ino))
  2036. iput(vol->extend_ino);
  2037. ntfs_error(sb, "Failed to load $Extend.");
  2038. goto iput_sec_err_out;
  2039. }
  2040. #ifdef NTFS_RW
  2041. /* Find the quota file, load it if present, and set it up. */
  2042. if (!load_and_init_quota(vol)) {
  2043. static const char *es1 = "Failed to load $Quota";
  2044. static const char *es2 = ". Run chkdsk.";
  2045. /* If a read-write mount, convert it to a read-only mount. */
  2046. if (!(sb->s_flags & MS_RDONLY)) {
  2047. if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
  2048. ON_ERRORS_CONTINUE))) {
  2049. ntfs_error(sb, "%s and neither on_errors="
  2050. "continue nor on_errors="
  2051. "remount-ro was specified%s",
  2052. es1, es2);
  2053. goto iput_quota_err_out;
  2054. }
  2055. sb->s_flags |= MS_RDONLY;
  2056. ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
  2057. } else
  2058. ntfs_warning(sb, "%s. Will not be able to remount "
  2059. "read-write%s", es1, es2);
  2060. /* This will prevent a read-write remount. */
  2061. NVolSetErrors(vol);
  2062. }
  2063. /* If (still) a read-write mount, mark the quotas out of date. */
  2064. if (!(sb->s_flags & MS_RDONLY) &&
  2065. !ntfs_mark_quotas_out_of_date(vol)) {
  2066. static const char *es1 = "Failed to mark quotas out of date";
  2067. static const char *es2 = ". Run chkdsk.";
  2068. /* Convert to a read-only mount. */
  2069. if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
  2070. ON_ERRORS_CONTINUE))) {
  2071. ntfs_error(sb, "%s and neither on_errors=continue nor "
  2072. "on_errors=remount-ro was specified%s",
  2073. es1, es2);
  2074. goto iput_quota_err_out;
  2075. }
  2076. ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
  2077. sb->s_flags |= MS_RDONLY;
  2078. NVolSetErrors(vol);
  2079. }
  2080. /*
  2081. * Find the transaction log file ($UsnJrnl), load it if present, check
  2082. * it, and set it up.
  2083. */
  2084. if (!load_and_init_usnjrnl(vol)) {
  2085. static const char *es1 = "Failed to load $UsnJrnl";
  2086. static const char *es2 = ". Run chkdsk.";
  2087. /* If a read-write mount, convert it to a read-only mount. */
  2088. if (!(sb->s_flags & MS_RDONLY)) {
  2089. if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
  2090. ON_ERRORS_CONTINUE))) {
  2091. ntfs_error(sb, "%s and neither on_errors="
  2092. "continue nor on_errors="
  2093. "remount-ro was specified%s",
  2094. es1, es2);
  2095. goto iput_usnjrnl_err_out;
  2096. }
  2097. sb->s_flags |= MS_RDONLY;
  2098. ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
  2099. } else
  2100. ntfs_warning(sb, "%s. Will not be able to remount "
  2101. "read-write%s", es1, es2);
  2102. /* This will prevent a read-write remount. */
  2103. NVolSetErrors(vol);
  2104. }
  2105. /* If (still) a read-write mount, stamp the transaction log. */
  2106. if (!(sb->s_flags & MS_RDONLY) && !ntfs_stamp_usnjrnl(vol)) {
  2107. static const char *es1 = "Failed to stamp transaction log "
  2108. "($UsnJrnl)";
  2109. static const char *es2 = ". Run chkdsk.";
  2110. /* Convert to a read-only mount. */
  2111. if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
  2112. ON_ERRORS_CONTINUE))) {
  2113. ntfs_error(sb, "%s and neither on_errors=continue nor "
  2114. "on_errors=remount-ro was specified%s",
  2115. es1, es2);
  2116. goto iput_usnjrnl_err_out;
  2117. }
  2118. ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
  2119. sb->s_flags |= MS_RDONLY;
  2120. NVolSetErrors(vol);
  2121. }
  2122. #endif /* NTFS_RW */
  2123. return true;
  2124. #ifdef NTFS_RW
  2125. iput_usnjrnl_err_out:
  2126. if (vol->usnjrnl_j_ino)
  2127. iput(vol->usnjrnl_j_ino);
  2128. if (vol->usnjrnl_max_ino)
  2129. iput(vol->usnjrnl_max_ino);
  2130. if (vol->usnjrnl_ino)
  2131. iput(vol->usnjrnl_ino);
  2132. iput_quota_err_out:
  2133. if (vol->quota_q_ino)
  2134. iput(vol->quota_q_ino);
  2135. if (vol->quota_ino)
  2136. iput(vol->quota_ino);
  2137. iput(vol->extend_ino);
  2138. #endif /* NTFS_RW */
  2139. iput_sec_err_out:
  2140. iput(vol->secure_ino);
  2141. iput_root_err_out:
  2142. iput(vol->root_ino);
  2143. iput_logfile_err_out:
  2144. #ifdef NTFS_RW
  2145. if (vol->logfile_ino)
  2146. iput(vol->logfile_ino);
  2147. iput_vol_err_out:
  2148. #endif /* NTFS_RW */
  2149. iput(vol->vol_ino);
  2150. iput_lcnbmp_err_out:
  2151. iput(vol->lcnbmp_ino);
  2152. iput_attrdef_err_out:
  2153. vol->attrdef_size = 0;
  2154. if (vol->attrdef) {
  2155. ntfs_free(vol->attrdef);
  2156. vol->attrdef = NULL;
  2157. }
  2158. #ifdef NTFS_RW
  2159. iput_upcase_err_out:
  2160. #endif /* NTFS_RW */
  2161. vol->upcase_len = 0;
  2162. mutex_lock(&ntfs_lock);
  2163. if (vol->upcase == default_upcase) {
  2164. ntfs_nr_upcase_users--;
  2165. vol->upcase = NULL;
  2166. }
  2167. mutex_unlock(&ntfs_lock);
  2168. if (vol->upcase) {
  2169. ntfs_free(vol->upcase);
  2170. vol->upcase = NULL;
  2171. }
  2172. iput_mftbmp_err_out:
  2173. iput(vol->mftbmp_ino);
  2174. iput_mirr_err_out:
  2175. #ifdef NTFS_RW
  2176. if (vol->mftmirr_ino)
  2177. iput(vol->mftmirr_ino);
  2178. #endif /* NTFS_RW */
  2179. return false;
  2180. }
  2181. /**
  2182. * ntfs_put_super - called by the vfs to unmount a volume
  2183. * @sb: vfs superblock of volume to unmount
  2184. *
  2185. * ntfs_put_super() is called by the VFS (from fs/super.c::do_umount()) when
  2186. * the volume is being unmounted (umount system call has been invoked) and it
  2187. * releases all inodes and memory belonging to the NTFS specific part of the
  2188. * super block.
  2189. */
  2190. static void ntfs_put_super(struct super_block *sb)
  2191. {
  2192. ntfs_volume *vol = NTFS_SB(sb);
  2193. ntfs_debug("Entering.");
  2194. lock_kernel();
  2195. #ifdef NTFS_RW
  2196. /*
  2197. * Commit all inodes while they are still open in case some of them
  2198. * cause others to be dirtied.
  2199. */
  2200. ntfs_commit_inode(vol->vol_ino);
  2201. /* NTFS 3.0+ specific. */
  2202. if (vol->major_ver >= 3) {
  2203. if (vol->usnjrnl_j_ino)
  2204. ntfs_commit_inode(vol->usnjrnl_j_ino);
  2205. if (vol->usnjrnl_max_ino)
  2206. ntfs_commit_inode(vol->usnjrnl_max_ino);
  2207. if (vol->usnjrnl_ino)
  2208. ntfs_commit_inode(vol->usnjrnl_ino);
  2209. if (vol->quota_q_ino)
  2210. ntfs_commit_inode(vol->quota_q_ino);
  2211. if (vol->quota_ino)
  2212. ntfs_commit_inode(vol->quota_ino);
  2213. if (vol->extend_ino)
  2214. ntfs_commit_inode(vol->extend_ino);
  2215. if (vol->secure_ino)
  2216. ntfs_commit_inode(vol->secure_ino);
  2217. }
  2218. ntfs_commit_inode(vol->root_ino);
  2219. down_write(&vol->lcnbmp_lock);
  2220. ntfs_commit_inode(vol->lcnbmp_ino);
  2221. up_write(&vol->lcnbmp_lock);
  2222. down_write(&vol->mftbmp_lock);
  2223. ntfs_commit_inode(vol->mftbmp_ino);
  2224. up_write(&vol->mftbmp_lock);
  2225. if (vol->logfile_ino)
  2226. ntfs_commit_inode(vol->logfile_ino);
  2227. if (vol->mftmirr_ino)
  2228. ntfs_commit_inode(vol->mftmirr_ino);
  2229. ntfs_commit_inode(vol->mft_ino);
  2230. /*
  2231. * If a read-write mount and no volume errors have occured, mark the
  2232. * volume clean. Also, re-commit all affected inodes.
  2233. */
  2234. if (!(sb->s_flags & MS_RDONLY)) {
  2235. if (!NVolErrors(vol)) {
  2236. if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
  2237. ntfs_warning(sb, "Failed to clear dirty bit "
  2238. "in volume information "
  2239. "flags. Run chkdsk.");
  2240. ntfs_commit_inode(vol->vol_ino);
  2241. ntfs_commit_inode(vol->root_ino);
  2242. if (vol->mftmirr_ino)
  2243. ntfs_commit_inode(vol->mftmirr_ino);
  2244. ntfs_commit_inode(vol->mft_ino);
  2245. } else {
  2246. ntfs_warning(sb, "Volume has errors. Leaving volume "
  2247. "marked dirty. Run chkdsk.");
  2248. }
  2249. }
  2250. #endif /* NTFS_RW */
  2251. iput(vol->vol_ino);
  2252. vol->vol_ino = NULL;
  2253. /* NTFS 3.0+ specific clean up. */
  2254. if (vol->major_ver >= 3) {
  2255. #ifdef NTFS_RW
  2256. if (vol->usnjrnl_j_ino) {
  2257. iput(vol->usnjrnl_j_ino);
  2258. vol->usnjrnl_j_ino = NULL;
  2259. }
  2260. if (vol->usnjrnl_max_ino) {
  2261. iput(vol->usnjrnl_max_ino);
  2262. vol->usnjrnl_max_ino = NULL;
  2263. }
  2264. if (vol->usnjrnl_ino) {
  2265. iput(vol->usnjrnl_ino);
  2266. vol->usnjrnl_ino = NULL;
  2267. }
  2268. if (vol->quota_q_ino) {
  2269. iput(vol->quota_q_ino);
  2270. vol->quota_q_ino = NULL;
  2271. }
  2272. if (vol->quota_ino) {
  2273. iput(vol->quota_ino);
  2274. vol->quota_ino = NULL;
  2275. }
  2276. #endif /* NTFS_RW */
  2277. if (vol->extend_ino) {
  2278. iput(vol->extend_ino);
  2279. vol->extend_ino = NULL;
  2280. }
  2281. if (vol->secure_ino) {
  2282. iput(vol->secure_ino);
  2283. vol->secure_ino = NULL;
  2284. }
  2285. }
  2286. iput(vol->root_ino);
  2287. vol->root_ino = NULL;
  2288. down_write(&vol->lcnbmp_lock);
  2289. iput(vol->lcnbmp_ino);
  2290. vol->lcnbmp_ino = NULL;
  2291. up_write(&vol->lcnbmp_lock);
  2292. down_write(&vol->mftbmp_lock);
  2293. iput(vol->mftbmp_ino);
  2294. vol->mftbmp_ino = NULL;
  2295. up_write(&vol->mftbmp_lock);
  2296. #ifdef NTFS_RW
  2297. if (vol->logfile_ino) {
  2298. iput(vol->logfile_ino);
  2299. vol->logfile_ino = NULL;
  2300. }
  2301. if (vol->mftmirr_ino) {
  2302. /* Re-commit the mft mirror and mft just in case. */
  2303. ntfs_commit_inode(vol->mftmirr_ino);
  2304. ntfs_commit_inode(vol->mft_ino);
  2305. iput(vol->mftmirr_ino);
  2306. vol->mftmirr_ino = NULL;
  2307. }
  2308. /*
  2309. * We should have no dirty inodes left, due to
  2310. * mft.c::ntfs_mft_writepage() cleaning all the dirty pages as
  2311. * the underlying mft records are written out and cleaned.
  2312. */
  2313. ntfs_commit_inode(vol->mft_ino);
  2314. write_inode_now(vol->mft_ino, 1);
  2315. #endif /* NTFS_RW */
  2316. iput(vol->mft_ino);
  2317. vol->mft_ino = NULL;
  2318. /* Throw away the table of attribute definitions. */
  2319. vol->attrdef_size = 0;
  2320. if (vol->attrdef) {
  2321. ntfs_free(vol->attrdef);
  2322. vol->attrdef = NULL;
  2323. }
  2324. vol->upcase_len = 0;
  2325. /*
  2326. * Destroy the global default upcase table if necessary. Also decrease
  2327. * the number of upcase users if we are a user.
  2328. */
  2329. mutex_lock(&ntfs_lock);
  2330. if (vol->upcase == default_upcase) {
  2331. ntfs_nr_upcase_users--;
  2332. vol->upcase = NULL;
  2333. }
  2334. if (!ntfs_nr_upcase_users && default_upcase) {
  2335. ntfs_free(default_upcase);
  2336. default_upcase = NULL;
  2337. }
  2338. if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
  2339. free_compression_buffers();
  2340. mutex_unlock(&ntfs_lock);
  2341. if (vol->upcase) {
  2342. ntfs_free(vol->upcase);
  2343. vol->upcase = NULL;
  2344. }
  2345. if (vol->nls_map) {
  2346. unload_nls(vol->nls_map);
  2347. vol->nls_map = NULL;
  2348. }
  2349. sb->s_fs_info = NULL;
  2350. kfree(vol);
  2351. unlock_kernel();
  2352. }
  2353. /**
  2354. * get_nr_free_clusters - return the number of free clusters on a volume
  2355. * @vol: ntfs volume for which to obtain free cluster count
  2356. *
  2357. * Calculate the number of free clusters on the mounted NTFS volume @vol. We
  2358. * actually calculate the number of clusters in use instead because this
  2359. * allows us to not care about partial pages as these will be just zero filled
  2360. * and hence not be counted as allocated clusters.
  2361. *
  2362. * The only particularity is that clusters beyond the end of the logical ntfs
  2363. * volume will be marked as allocated to prevent errors which means we have to
  2364. * discount those at the end. This is important as the cluster bitmap always
  2365. * has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside
  2366. * the logical volume and marked in use when they are not as they do not exist.
  2367. *
  2368. * If any pages cannot be read we assume all clusters in the erroring pages are
  2369. * in use. This means we return an underestimate on errors which is better than
  2370. * an overestimate.
  2371. */
  2372. static s64 get_nr_free_clusters(ntfs_volume *vol)
  2373. {
  2374. s64 nr_free = vol->nr_clusters;
  2375. u32 *kaddr;
  2376. struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
  2377. struct page *page;
  2378. pgoff_t index, max_index;
  2379. ntfs_debug("Entering.");
  2380. /* Serialize accesses to the cluster bitmap. */
  2381. down_read(&vol->lcnbmp_lock);
  2382. /*
  2383. * Convert the number of bits into bytes rounded up, then convert into
  2384. * multiples of PAGE_CACHE_SIZE, rounding up so that if we have one
  2385. * full and one partial page max_index = 2.
  2386. */
  2387. max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >>
  2388. PAGE_CACHE_SHIFT;
  2389. /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
  2390. ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.",
  2391. max_index, PAGE_CACHE_SIZE / 4);
  2392. for (index = 0; index < max_index; index++) {
  2393. unsigned int i;
  2394. /*
  2395. * Read the page from page cache, getting it from backing store
  2396. * if necessary, and increment the use count.
  2397. */
  2398. page = read_mapping_page(mapping, index, NULL);
  2399. /* Ignore pages which errored synchronously. */
  2400. if (IS_ERR(page)) {
  2401. ntfs_debug("read_mapping_page() error. Skipping "
  2402. "page (index 0x%lx).", index);
  2403. nr_free -= PAGE_CACHE_SIZE * 8;
  2404. continue;
  2405. }
  2406. kaddr = (u32*)kmap_atomic(page, KM_USER0);
  2407. /*
  2408. * For each 4 bytes, subtract the number of set bits. If this
  2409. * is the last page and it is partial we don't really care as
  2410. * it just means we do a little extra work but it won't affect
  2411. * the result as all out of range bytes are set to zero by
  2412. * ntfs_readpage().
  2413. */
  2414. for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
  2415. nr_free -= (s64)hweight32(kaddr[i]);
  2416. kunmap_atomic(kaddr, KM_USER0);
  2417. page_cache_release(page);
  2418. }
  2419. ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index - 1);
  2420. /*
  2421. * Fixup for eventual bits outside logical ntfs volume (see function
  2422. * description above).
  2423. */
  2424. if (vol->nr_clusters & 63)
  2425. nr_free += 64 - (vol->nr_clusters & 63);
  2426. up_read(&vol->lcnbmp_lock);
  2427. /* If errors occured we may well have gone below zero, fix this. */
  2428. if (nr_free < 0)
  2429. nr_free = 0;
  2430. ntfs_debug("Exiting.");
  2431. return nr_free;
  2432. }
  2433. /**
  2434. * __get_nr_free_mft_records - return the number of free inodes on a volume
  2435. * @vol: ntfs volume for which to obtain free inode count
  2436. * @nr_free: number of mft records in filesystem
  2437. * @max_index: maximum number of pages containing set bits
  2438. *
  2439. * Calculate the number of free mft records (inodes) on the mounted NTFS
  2440. * volume @vol. We actually calculate the number of mft records in use instead
  2441. * because this allows us to not care about partial pages as these will be just
  2442. * zero filled and hence not be counted as allocated mft record.
  2443. *
  2444. * If any pages cannot be read we assume all mft records in the erroring pages
  2445. * are in use. This means we return an underestimate on errors which is better
  2446. * than an overestimate.
  2447. *
  2448. * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
  2449. */
  2450. static unsigned long __get_nr_free_mft_records(ntfs_volume *vol,
  2451. s64 nr_free, const pgoff_t max_index)
  2452. {
  2453. u32 *kaddr;
  2454. struct address_space *mapping = vol->mftbmp_ino->i_mapping;
  2455. struct page *page;
  2456. pgoff_t index;
  2457. ntfs_debug("Entering.");
  2458. /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
  2459. ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
  2460. "0x%lx.", max_index, PAGE_CACHE_SIZE / 4);
  2461. for (index = 0; index < max_index; index++) {
  2462. unsigned int i;
  2463. /*
  2464. * Read the page from page cache, getting it from backing store
  2465. * if necessary, and increment the use count.
  2466. */
  2467. page = read_mapping_page(mapping, index, NULL);
  2468. /* Ignore pages which errored synchronously. */
  2469. if (IS_ERR(page)) {
  2470. ntfs_debug("read_mapping_page() error. Skipping "
  2471. "page (index 0x%lx).", index);
  2472. nr_free -= PAGE_CACHE_SIZE * 8;
  2473. continue;
  2474. }
  2475. kaddr = (u32*)kmap_atomic(page, KM_USER0);
  2476. /*
  2477. * For each 4 bytes, subtract the number of set bits. If this
  2478. * is the last page and it is partial we don't really care as
  2479. * it just means we do a little extra work but it won't affect
  2480. * the result as all out of range bytes are set to zero by
  2481. * ntfs_readpage().
  2482. */
  2483. for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
  2484. nr_free -= (s64)hweight32(kaddr[i]);
  2485. kunmap_atomic(kaddr, KM_USER0);
  2486. page_cache_release(page);
  2487. }
  2488. ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.",
  2489. index - 1);
  2490. /* If errors occured we may well have gone below zero, fix this. */
  2491. if (nr_free < 0)
  2492. nr_free = 0;
  2493. ntfs_debug("Exiting.");
  2494. return nr_free;
  2495. }
  2496. /**
  2497. * ntfs_statfs - return information about mounted NTFS volume
  2498. * @dentry: dentry from mounted volume
  2499. * @sfs: statfs structure in which to return the information
  2500. *
  2501. * Return information about the mounted NTFS volume @dentry in the statfs structure
  2502. * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is
  2503. * called). We interpret the values to be correct of the moment in time at
  2504. * which we are called. Most values are variable otherwise and this isn't just
  2505. * the free values but the totals as well. For example we can increase the
  2506. * total number of file nodes if we run out and we can keep doing this until
  2507. * there is no more space on the volume left at all.
  2508. *
  2509. * Called from vfs_statfs which is used to handle the statfs, fstatfs, and
  2510. * ustat system calls.
  2511. *
  2512. * Return 0 on success or -errno on error.
  2513. */
  2514. static int ntfs_statfs(struct dentry *dentry, struct kstatfs *sfs)
  2515. {
  2516. struct super_block *sb = dentry->d_sb;
  2517. s64 size;
  2518. ntfs_volume *vol = NTFS_SB(sb);
  2519. ntfs_inode *mft_ni = NTFS_I(vol->mft_ino);
  2520. pgoff_t max_index;
  2521. unsigned long flags;
  2522. ntfs_debug("Entering.");
  2523. /* Type of filesystem. */
  2524. sfs->f_type = NTFS_SB_MAGIC;
  2525. /* Optimal transfer block size. */
  2526. sfs->f_bsize = PAGE_CACHE_SIZE;
  2527. /*
  2528. * Total data blocks in filesystem in units of f_bsize and since
  2529. * inodes are also stored in data blocs ($MFT is a file) this is just
  2530. * the total clusters.
  2531. */
  2532. sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >>
  2533. PAGE_CACHE_SHIFT;
  2534. /* Free data blocks in filesystem in units of f_bsize. */
  2535. size = get_nr_free_clusters(vol) << vol->cluster_size_bits >>
  2536. PAGE_CACHE_SHIFT;
  2537. if (size < 0LL)
  2538. size = 0LL;
  2539. /* Free blocks avail to non-superuser, same as above on NTFS. */
  2540. sfs->f_bavail = sfs->f_bfree = size;
  2541. /* Serialize accesses to the inode bitmap. */
  2542. down_read(&vol->mftbmp_lock);
  2543. read_lock_irqsave(&mft_ni->size_lock, flags);
  2544. size = i_size_read(vol->mft_ino) >> vol->mft_record_size_bits;
  2545. /*
  2546. * Convert the maximum number of set bits into bytes rounded up, then
  2547. * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
  2548. * have one full and one partial page max_index = 2.
  2549. */
  2550. max_index = ((((mft_ni->initialized_size >> vol->mft_record_size_bits)
  2551. + 7) >> 3) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  2552. read_unlock_irqrestore(&mft_ni->size_lock, flags);
  2553. /* Number of inodes in filesystem (at this point in time). */
  2554. sfs->f_files = size;
  2555. /* Free inodes in fs (based on current total count). */
  2556. sfs->f_ffree = __get_nr_free_mft_records(vol, size, max_index);
  2557. up_read(&vol->mftbmp_lock);
  2558. /*
  2559. * File system id. This is extremely *nix flavour dependent and even
  2560. * within Linux itself all fs do their own thing. I interpret this to
  2561. * mean a unique id associated with the mounted fs and not the id
  2562. * associated with the filesystem driver, the latter is already given
  2563. * by the filesystem type in sfs->f_type. Thus we use the 64-bit
  2564. * volume serial number splitting it into two 32-bit parts. We enter
  2565. * the least significant 32-bits in f_fsid[0] and the most significant
  2566. * 32-bits in f_fsid[1].
  2567. */
  2568. sfs->f_fsid.val[0] = vol->serial_no & 0xffffffff;
  2569. sfs->f_fsid.val[1] = (vol->serial_no >> 32) & 0xffffffff;
  2570. /* Maximum length of filenames. */
  2571. sfs->f_namelen = NTFS_MAX_NAME_LEN;
  2572. return 0;
  2573. }
  2574. /**
  2575. * The complete super operations.
  2576. */
  2577. static const struct super_operations ntfs_sops = {
  2578. .alloc_inode = ntfs_alloc_big_inode, /* VFS: Allocate new inode. */
  2579. .destroy_inode = ntfs_destroy_big_inode, /* VFS: Deallocate inode. */
  2580. #ifdef NTFS_RW
  2581. //.dirty_inode = NULL, /* VFS: Called from
  2582. // __mark_inode_dirty(). */
  2583. .write_inode = ntfs_write_inode, /* VFS: Write dirty inode to
  2584. disk. */
  2585. //.drop_inode = NULL, /* VFS: Called just after the
  2586. // inode reference count has
  2587. // been decreased to zero.
  2588. // NOTE: The inode lock is
  2589. // held. See fs/inode.c::
  2590. // generic_drop_inode(). */
  2591. //.delete_inode = NULL, /* VFS: Delete inode from disk.
  2592. // Called when i_count becomes
  2593. // 0 and i_nlink is also 0. */
  2594. //.write_super = NULL, /* Flush dirty super block to
  2595. // disk. */
  2596. //.sync_fs = NULL, /* ? */
  2597. //.write_super_lockfs = NULL, /* ? */
  2598. //.unlockfs = NULL, /* ? */
  2599. #endif /* NTFS_RW */
  2600. .put_super = ntfs_put_super, /* Syscall: umount. */
  2601. .statfs = ntfs_statfs, /* Syscall: statfs */
  2602. .remount_fs = ntfs_remount, /* Syscall: mount -o remount. */
  2603. .clear_inode = ntfs_clear_big_inode, /* VFS: Called when an inode is
  2604. removed from memory. */
  2605. //.umount_begin = NULL, /* Forced umount. */
  2606. .show_options = ntfs_show_options, /* Show mount options in
  2607. proc. */
  2608. };
  2609. /**
  2610. * ntfs_fill_super - mount an ntfs filesystem
  2611. * @sb: super block of ntfs filesystem to mount
  2612. * @opt: string containing the mount options
  2613. * @silent: silence error output
  2614. *
  2615. * ntfs_fill_super() is called by the VFS to mount the device described by @sb
  2616. * with the mount otions in @data with the NTFS filesystem.
  2617. *
  2618. * If @silent is true, remain silent even if errors are detected. This is used
  2619. * during bootup, when the kernel tries to mount the root filesystem with all
  2620. * registered filesystems one after the other until one succeeds. This implies
  2621. * that all filesystems except the correct one will quite correctly and
  2622. * expectedly return an error, but nobody wants to see error messages when in
  2623. * fact this is what is supposed to happen.
  2624. *
  2625. * NOTE: @sb->s_flags contains the mount options flags.
  2626. */
  2627. static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
  2628. {
  2629. ntfs_volume *vol;
  2630. struct buffer_head *bh;
  2631. struct inode *tmp_ino;
  2632. int blocksize, result;
  2633. /*
  2634. * We do a pretty difficult piece of bootstrap by reading the
  2635. * MFT (and other metadata) from disk into memory. We'll only
  2636. * release this metadata during umount, so the locking patterns
  2637. * observed during bootstrap do not count. So turn off the
  2638. * observation of locking patterns (strictly for this context
  2639. * only) while mounting NTFS. [The validator is still active
  2640. * otherwise, even for this context: it will for example record
  2641. * lock class registrations.]
  2642. */
  2643. lockdep_off();
  2644. ntfs_debug("Entering.");
  2645. #ifndef NTFS_RW
  2646. sb->s_flags |= MS_RDONLY;
  2647. #endif /* ! NTFS_RW */
  2648. /* Allocate a new ntfs_volume and place it in sb->s_fs_info. */
  2649. sb->s_fs_info = kmalloc(sizeof(ntfs_volume), GFP_NOFS);
  2650. vol = NTFS_SB(sb);
  2651. if (!vol) {
  2652. if (!silent)
  2653. ntfs_error(sb, "Allocation of NTFS volume structure "
  2654. "failed. Aborting mount...");
  2655. lockdep_on();
  2656. return -ENOMEM;
  2657. }
  2658. /* Initialize ntfs_volume structure. */
  2659. *vol = (ntfs_volume) {
  2660. .sb = sb,
  2661. /*
  2662. * Default is group and other don't have any access to files or
  2663. * directories while owner has full access. Further, files by
  2664. * default are not executable but directories are of course
  2665. * browseable.
  2666. */
  2667. .fmask = 0177,
  2668. .dmask = 0077,
  2669. };
  2670. init_rwsem(&vol->mftbmp_lock);
  2671. init_rwsem(&vol->lcnbmp_lock);
  2672. unlock_kernel();
  2673. /* By default, enable sparse support. */
  2674. NVolSetSparseEnabled(vol);
  2675. /* Important to get the mount options dealt with now. */
  2676. if (!parse_options(vol, (char*)opt))
  2677. goto err_out_now;
  2678. /* We support sector sizes up to the PAGE_CACHE_SIZE. */
  2679. if (bdev_logical_block_size(sb->s_bdev) > PAGE_CACHE_SIZE) {
  2680. if (!silent)
  2681. ntfs_error(sb, "Device has unsupported sector size "
  2682. "(%i). The maximum supported sector "
  2683. "size on this architecture is %lu "
  2684. "bytes.",
  2685. bdev_logical_block_size(sb->s_bdev),
  2686. PAGE_CACHE_SIZE);
  2687. goto err_out_now;
  2688. }
  2689. /*
  2690. * Setup the device access block size to NTFS_BLOCK_SIZE or the hard
  2691. * sector size, whichever is bigger.
  2692. */
  2693. blocksize = sb_min_blocksize(sb, NTFS_BLOCK_SIZE);
  2694. if (blocksize < NTFS_BLOCK_SIZE) {
  2695. if (!silent)
  2696. ntfs_error(sb, "Unable to set device block size.");
  2697. goto err_out_now;
  2698. }
  2699. BUG_ON(blocksize != sb->s_blocksize);
  2700. ntfs_debug("Set device block size to %i bytes (block size bits %i).",
  2701. blocksize, sb->s_blocksize_bits);
  2702. /* Determine the size of the device in units of block_size bytes. */
  2703. if (!i_size_read(sb->s_bdev->bd_inode)) {
  2704. if (!silent)
  2705. ntfs_error(sb, "Unable to determine device size.");
  2706. goto err_out_now;
  2707. }
  2708. vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
  2709. sb->s_blocksize_bits;
  2710. /* Read the boot sector and return unlocked buffer head to it. */
  2711. if (!(bh = read_ntfs_boot_sector(sb, silent))) {
  2712. if (!silent)
  2713. ntfs_error(sb, "Not an NTFS volume.");
  2714. goto err_out_now;
  2715. }
  2716. /*
  2717. * Extract the data from the boot sector and setup the ntfs volume
  2718. * using it.
  2719. */
  2720. result = parse_ntfs_boot_sector(vol, (NTFS_BOOT_SECTOR*)bh->b_data);
  2721. brelse(bh);
  2722. if (!result) {
  2723. if (!silent)
  2724. ntfs_error(sb, "Unsupported NTFS filesystem.");
  2725. goto err_out_now;
  2726. }
  2727. /*
  2728. * If the boot sector indicates a sector size bigger than the current
  2729. * device block size, switch the device block size to the sector size.
  2730. * TODO: It may be possible to support this case even when the set
  2731. * below fails, we would just be breaking up the i/o for each sector
  2732. * into multiple blocks for i/o purposes but otherwise it should just
  2733. * work. However it is safer to leave disabled until someone hits this
  2734. * error message and then we can get them to try it without the setting
  2735. * so we know for sure that it works.
  2736. */
  2737. if (vol->sector_size > blocksize) {
  2738. blocksize = sb_set_blocksize(sb, vol->sector_size);
  2739. if (blocksize != vol->sector_size) {
  2740. if (!silent)
  2741. ntfs_error(sb, "Unable to set device block "
  2742. "size to sector size (%i).",
  2743. vol->sector_size);
  2744. goto err_out_now;
  2745. }
  2746. BUG_ON(blocksize != sb->s_blocksize);
  2747. vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
  2748. sb->s_blocksize_bits;
  2749. ntfs_debug("Changed device block size to %i bytes (block size "
  2750. "bits %i) to match volume sector size.",
  2751. blocksize, sb->s_blocksize_bits);
  2752. }
  2753. /* Initialize the cluster and mft allocators. */
  2754. ntfs_setup_allocators(vol);
  2755. /* Setup remaining fields in the super block. */
  2756. sb->s_magic = NTFS_SB_MAGIC;
  2757. /*
  2758. * Ntfs allows 63 bits for the file size, i.e. correct would be:
  2759. * sb->s_maxbytes = ~0ULL >> 1;
  2760. * But the kernel uses a long as the page cache page index which on
  2761. * 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel
  2762. * defined to the maximum the page cache page index can cope with
  2763. * without overflowing the index or to 2^63 - 1, whichever is smaller.
  2764. */
  2765. sb->s_maxbytes = MAX_LFS_FILESIZE;
  2766. /* Ntfs measures time in 100ns intervals. */
  2767. sb->s_time_gran = 100;
  2768. /*
  2769. * Now load the metadata required for the page cache and our address
  2770. * space operations to function. We do this by setting up a specialised
  2771. * read_inode method and then just calling the normal iget() to obtain
  2772. * the inode for $MFT which is sufficient to allow our normal inode
  2773. * operations and associated address space operations to function.
  2774. */
  2775. sb->s_op = &ntfs_sops;
  2776. tmp_ino = new_inode(sb);
  2777. if (!tmp_ino) {
  2778. if (!silent)
  2779. ntfs_error(sb, "Failed to load essential metadata.");
  2780. goto err_out_now;
  2781. }
  2782. tmp_ino->i_ino = FILE_MFT;
  2783. insert_inode_hash(tmp_ino);
  2784. if (ntfs_read_inode_mount(tmp_ino) < 0) {
  2785. if (!silent)
  2786. ntfs_error(sb, "Failed to load essential metadata.");
  2787. goto iput_tmp_ino_err_out_now;
  2788. }
  2789. mutex_lock(&ntfs_lock);
  2790. /*
  2791. * The current mount is a compression user if the cluster size is
  2792. * less than or equal 4kiB.
  2793. */
  2794. if (vol->cluster_size <= 4096 && !ntfs_nr_compression_users++) {
  2795. result = allocate_compression_buffers();
  2796. if (result) {
  2797. ntfs_error(NULL, "Failed to allocate buffers "
  2798. "for compression engine.");
  2799. ntfs_nr_compression_users--;
  2800. mutex_unlock(&ntfs_lock);
  2801. goto iput_tmp_ino_err_out_now;
  2802. }
  2803. }
  2804. /*
  2805. * Generate the global default upcase table if necessary. Also
  2806. * temporarily increment the number of upcase users to avoid race
  2807. * conditions with concurrent (u)mounts.
  2808. */
  2809. if (!default_upcase)
  2810. default_upcase = generate_default_upcase();
  2811. ntfs_nr_upcase_users++;
  2812. mutex_unlock(&ntfs_lock);
  2813. /*
  2814. * From now on, ignore @silent parameter. If we fail below this line,
  2815. * it will be due to a corrupt fs or a system error, so we report it.
  2816. */
  2817. /*
  2818. * Open the system files with normal access functions and complete
  2819. * setting up the ntfs super block.
  2820. */
  2821. if (!load_system_files(vol)) {
  2822. ntfs_error(sb, "Failed to load system files.");
  2823. goto unl_upcase_iput_tmp_ino_err_out_now;
  2824. }
  2825. if ((sb->s_root = d_alloc_root(vol->root_ino))) {
  2826. /* We increment i_count simulating an ntfs_iget(). */
  2827. atomic_inc(&vol->root_ino->i_count);
  2828. ntfs_debug("Exiting, status successful.");
  2829. /* Release the default upcase if it has no users. */
  2830. mutex_lock(&ntfs_lock);
  2831. if (!--ntfs_nr_upcase_users && default_upcase) {
  2832. ntfs_free(default_upcase);
  2833. default_upcase = NULL;
  2834. }
  2835. mutex_unlock(&ntfs_lock);
  2836. sb->s_export_op = &ntfs_export_ops;
  2837. lock_kernel();
  2838. lockdep_on();
  2839. return 0;
  2840. }
  2841. ntfs_error(sb, "Failed to allocate root directory.");
  2842. /* Clean up after the successful load_system_files() call from above. */
  2843. // TODO: Use ntfs_put_super() instead of repeating all this code...
  2844. // FIXME: Should mark the volume clean as the error is most likely
  2845. // -ENOMEM.
  2846. iput(vol->vol_ino);
  2847. vol->vol_ino = NULL;
  2848. /* NTFS 3.0+ specific clean up. */
  2849. if (vol->major_ver >= 3) {
  2850. #ifdef NTFS_RW
  2851. if (vol->usnjrnl_j_ino) {
  2852. iput(vol->usnjrnl_j_ino);
  2853. vol->usnjrnl_j_ino = NULL;
  2854. }
  2855. if (vol->usnjrnl_max_ino) {
  2856. iput(vol->usnjrnl_max_ino);
  2857. vol->usnjrnl_max_ino = NULL;
  2858. }
  2859. if (vol->usnjrnl_ino) {
  2860. iput(vol->usnjrnl_ino);
  2861. vol->usnjrnl_ino = NULL;
  2862. }
  2863. if (vol->quota_q_ino) {
  2864. iput(vol->quota_q_ino);
  2865. vol->quota_q_ino = NULL;
  2866. }
  2867. if (vol->quota_ino) {
  2868. iput(vol->quota_ino);
  2869. vol->quota_ino = NULL;
  2870. }
  2871. #endif /* NTFS_RW */
  2872. if (vol->extend_ino) {
  2873. iput(vol->extend_ino);
  2874. vol->extend_ino = NULL;
  2875. }
  2876. if (vol->secure_ino) {
  2877. iput(vol->secure_ino);
  2878. vol->secure_ino = NULL;
  2879. }
  2880. }
  2881. iput(vol->root_ino);
  2882. vol->root_ino = NULL;
  2883. iput(vol->lcnbmp_ino);
  2884. vol->lcnbmp_ino = NULL;
  2885. iput(vol->mftbmp_ino);
  2886. vol->mftbmp_ino = NULL;
  2887. #ifdef NTFS_RW
  2888. if (vol->logfile_ino) {
  2889. iput(vol->logfile_ino);
  2890. vol->logfile_ino = NULL;
  2891. }
  2892. if (vol->mftmirr_ino) {
  2893. iput(vol->mftmirr_ino);
  2894. vol->mftmirr_ino = NULL;
  2895. }
  2896. #endif /* NTFS_RW */
  2897. /* Throw away the table of attribute definitions. */
  2898. vol->attrdef_size = 0;
  2899. if (vol->attrdef) {
  2900. ntfs_free(vol->attrdef);
  2901. vol->attrdef = NULL;
  2902. }
  2903. vol->upcase_len = 0;
  2904. mutex_lock(&ntfs_lock);
  2905. if (vol->upcase == default_upcase) {
  2906. ntfs_nr_upcase_users--;
  2907. vol->upcase = NULL;
  2908. }
  2909. mutex_unlock(&ntfs_lock);
  2910. if (vol->upcase) {
  2911. ntfs_free(vol->upcase);
  2912. vol->upcase = NULL;
  2913. }
  2914. if (vol->nls_map) {
  2915. unload_nls(vol->nls_map);
  2916. vol->nls_map = NULL;
  2917. }
  2918. /* Error exit code path. */
  2919. unl_upcase_iput_tmp_ino_err_out_now:
  2920. /*
  2921. * Decrease the number of upcase users and destroy the global default
  2922. * upcase table if necessary.
  2923. */
  2924. mutex_lock(&ntfs_lock);
  2925. if (!--ntfs_nr_upcase_users && default_upcase) {
  2926. ntfs_free(default_upcase);
  2927. default_upcase = NULL;
  2928. }
  2929. if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
  2930. free_compression_buffers();
  2931. mutex_unlock(&ntfs_lock);
  2932. iput_tmp_ino_err_out_now:
  2933. iput(tmp_ino);
  2934. if (vol->mft_ino && vol->mft_ino != tmp_ino)
  2935. iput(vol->mft_ino);
  2936. vol->mft_ino = NULL;
  2937. /*
  2938. * This is needed to get ntfs_clear_extent_inode() called for each
  2939. * inode we have ever called ntfs_iget()/iput() on, otherwise we A)
  2940. * leak resources and B) a subsequent mount fails automatically due to
  2941. * ntfs_iget() never calling down into our ntfs_read_locked_inode()
  2942. * method again... FIXME: Do we need to do this twice now because of
  2943. * attribute inodes? I think not, so leave as is for now... (AIA)
  2944. */
  2945. if (invalidate_inodes(sb)) {
  2946. ntfs_error(sb, "Busy inodes left. This is most likely a NTFS "
  2947. "driver bug.");
  2948. /* Copied from fs/super.c. I just love this message. (-; */
  2949. printk("NTFS: Busy inodes after umount. Self-destruct in 5 "
  2950. "seconds. Have a nice day...\n");
  2951. }
  2952. /* Errors at this stage are irrelevant. */
  2953. err_out_now:
  2954. lock_kernel();
  2955. sb->s_fs_info = NULL;
  2956. kfree(vol);
  2957. ntfs_debug("Failed, returning -EINVAL.");
  2958. lockdep_on();
  2959. return -EINVAL;
  2960. }
  2961. /*
  2962. * This is a slab cache to optimize allocations and deallocations of Unicode
  2963. * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN
  2964. * (255) Unicode characters + a terminating NULL Unicode character.
  2965. */
  2966. struct kmem_cache *ntfs_name_cache;
  2967. /* Slab caches for efficient allocation/deallocation of inodes. */
  2968. struct kmem_cache *ntfs_inode_cache;
  2969. struct kmem_cache *ntfs_big_inode_cache;
  2970. /* Init once constructor for the inode slab cache. */
  2971. static void ntfs_big_inode_init_once(void *foo)
  2972. {
  2973. ntfs_inode *ni = (ntfs_inode *)foo;
  2974. inode_init_once(VFS_I(ni));
  2975. }
  2976. /*
  2977. * Slab caches to optimize allocations and deallocations of attribute search
  2978. * contexts and index contexts, respectively.
  2979. */
  2980. struct kmem_cache *ntfs_attr_ctx_cache;
  2981. struct kmem_cache *ntfs_index_ctx_cache;
  2982. /* Driver wide mutex. */
  2983. DEFINE_MUTEX(ntfs_lock);
  2984. static int ntfs_get_sb(struct file_system_type *fs_type,
  2985. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  2986. {
  2987. return get_sb_bdev(fs_type, flags, dev_name, data, ntfs_fill_super,
  2988. mnt);
  2989. }
  2990. static struct file_system_type ntfs_fs_type = {
  2991. .owner = THIS_MODULE,
  2992. .name = "ntfs",
  2993. .get_sb = ntfs_get_sb,
  2994. .kill_sb = kill_block_super,
  2995. .fs_flags = FS_REQUIRES_DEV,
  2996. };
  2997. /* Stable names for the slab caches. */
  2998. static const char ntfs_index_ctx_cache_name[] = "ntfs_index_ctx_cache";
  2999. static const char ntfs_attr_ctx_cache_name[] = "ntfs_attr_ctx_cache";
  3000. static const char ntfs_name_cache_name[] = "ntfs_name_cache";
  3001. static const char ntfs_inode_cache_name[] = "ntfs_inode_cache";
  3002. static const char ntfs_big_inode_cache_name[] = "ntfs_big_inode_cache";
  3003. static int __init init_ntfs_fs(void)
  3004. {
  3005. int err = 0;
  3006. /* This may be ugly but it results in pretty output so who cares. (-8 */
  3007. printk(KERN_INFO "NTFS driver " NTFS_VERSION " [Flags: R/"
  3008. #ifdef NTFS_RW
  3009. "W"
  3010. #else
  3011. "O"
  3012. #endif
  3013. #ifdef DEBUG
  3014. " DEBUG"
  3015. #endif
  3016. #ifdef MODULE
  3017. " MODULE"
  3018. #endif
  3019. "].\n");
  3020. ntfs_debug("Debug messages are enabled.");
  3021. ntfs_index_ctx_cache = kmem_cache_create(ntfs_index_ctx_cache_name,
  3022. sizeof(ntfs_index_context), 0 /* offset */,
  3023. SLAB_HWCACHE_ALIGN, NULL /* ctor */);
  3024. if (!ntfs_index_ctx_cache) {
  3025. printk(KERN_CRIT "NTFS: Failed to create %s!\n",
  3026. ntfs_index_ctx_cache_name);
  3027. goto ictx_err_out;
  3028. }
  3029. ntfs_attr_ctx_cache = kmem_cache_create(ntfs_attr_ctx_cache_name,
  3030. sizeof(ntfs_attr_search_ctx), 0 /* offset */,
  3031. SLAB_HWCACHE_ALIGN, NULL /* ctor */);
  3032. if (!ntfs_attr_ctx_cache) {
  3033. printk(KERN_CRIT "NTFS: Failed to create %s!\n",
  3034. ntfs_attr_ctx_cache_name);
  3035. goto actx_err_out;
  3036. }
  3037. ntfs_name_cache = kmem_cache_create(ntfs_name_cache_name,
  3038. (NTFS_MAX_NAME_LEN+1) * sizeof(ntfschar), 0,
  3039. SLAB_HWCACHE_ALIGN, NULL);
  3040. if (!ntfs_name_cache) {
  3041. printk(KERN_CRIT "NTFS: Failed to create %s!\n",
  3042. ntfs_name_cache_name);
  3043. goto name_err_out;
  3044. }
  3045. ntfs_inode_cache = kmem_cache_create(ntfs_inode_cache_name,
  3046. sizeof(ntfs_inode), 0,
  3047. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL);
  3048. if (!ntfs_inode_cache) {
  3049. printk(KERN_CRIT "NTFS: Failed to create %s!\n",
  3050. ntfs_inode_cache_name);
  3051. goto inode_err_out;
  3052. }
  3053. ntfs_big_inode_cache = kmem_cache_create(ntfs_big_inode_cache_name,
  3054. sizeof(big_ntfs_inode), 0,
  3055. SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD,
  3056. ntfs_big_inode_init_once);
  3057. if (!ntfs_big_inode_cache) {
  3058. printk(KERN_CRIT "NTFS: Failed to create %s!\n",
  3059. ntfs_big_inode_cache_name);
  3060. goto big_inode_err_out;
  3061. }
  3062. /* Register the ntfs sysctls. */
  3063. err = ntfs_sysctl(1);
  3064. if (err) {
  3065. printk(KERN_CRIT "NTFS: Failed to register NTFS sysctls!\n");
  3066. goto sysctl_err_out;
  3067. }
  3068. err = register_filesystem(&ntfs_fs_type);
  3069. if (!err) {
  3070. ntfs_debug("NTFS driver registered successfully.");
  3071. return 0; /* Success! */
  3072. }
  3073. printk(KERN_CRIT "NTFS: Failed to register NTFS filesystem driver!\n");
  3074. sysctl_err_out:
  3075. kmem_cache_destroy(ntfs_big_inode_cache);
  3076. big_inode_err_out:
  3077. kmem_cache_destroy(ntfs_inode_cache);
  3078. inode_err_out:
  3079. kmem_cache_destroy(ntfs_name_cache);
  3080. name_err_out:
  3081. kmem_cache_destroy(ntfs_attr_ctx_cache);
  3082. actx_err_out:
  3083. kmem_cache_destroy(ntfs_index_ctx_cache);
  3084. ictx_err_out:
  3085. if (!err) {
  3086. printk(KERN_CRIT "NTFS: Aborting NTFS filesystem driver "
  3087. "registration...\n");
  3088. err = -ENOMEM;
  3089. }
  3090. return err;
  3091. }
  3092. static void __exit exit_ntfs_fs(void)
  3093. {
  3094. ntfs_debug("Unregistering NTFS driver.");
  3095. unregister_filesystem(&ntfs_fs_type);
  3096. kmem_cache_destroy(ntfs_big_inode_cache);
  3097. kmem_cache_destroy(ntfs_inode_cache);
  3098. kmem_cache_destroy(ntfs_name_cache);
  3099. kmem_cache_destroy(ntfs_attr_ctx_cache);
  3100. kmem_cache_destroy(ntfs_index_ctx_cache);
  3101. /* Unregister the ntfs sysctls. */
  3102. ntfs_sysctl(0);
  3103. }
  3104. MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>");
  3105. MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2007 Anton Altaparmakov");
  3106. MODULE_VERSION(NTFS_VERSION);
  3107. MODULE_LICENSE("GPL");
  3108. #ifdef DEBUG
  3109. module_param(debug_msgs, bool, 0);
  3110. MODULE_PARM_DESC(debug_msgs, "Enable debug messages.");
  3111. #endif
  3112. module_init(init_ntfs_fs)
  3113. module_exit(exit_ntfs_fs)