1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * super.c - NTFS kernel super block handling. Part of the Linux-NTFS project.
5 * Copyright (c) 2001-2012 Anton Altaparmakov and Tuxera Inc.
6 * Copyright (c) 2001,2002 Richard Russon
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/stddef.h>
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <linux/string.h>
14 #include <linux/spinlock.h>
15 #include <linux/blkdev.h> /* For bdev_logical_block_size(). */
16 #include <linux/backing-dev.h>
17 #include <linux/buffer_head.h>
18 #include <linux/vfs.h>
19 #include <linux/moduleparam.h>
20 #include <linux/bitmap.h>
35 /* Number of mounted filesystems which have compression enabled. */
36 static unsigned long ntfs_nr_compression_users;
38 /* A global default upcase table and a corresponding reference count. */
39 static ntfschar *default_upcase;
40 static unsigned long ntfs_nr_upcase_users;
42 /* Error constants/strings used in inode.c::ntfs_show_options(). */
44 /* One of these must be present, default is ON_ERRORS_CONTINUE. */
45 ON_ERRORS_PANIC = 0x01,
46 ON_ERRORS_REMOUNT_RO = 0x02,
47 ON_ERRORS_CONTINUE = 0x04,
48 /* Optional, can be combined with any of the above. */
49 ON_ERRORS_RECOVER = 0x10,
52 const option_t on_errors_arr[] = {
53 { ON_ERRORS_PANIC, "panic" },
54 { ON_ERRORS_REMOUNT_RO, "remount-ro", },
55 { ON_ERRORS_CONTINUE, "continue", },
56 { ON_ERRORS_RECOVER, "recover" },
63 * Copied from old ntfs driver (which copied from vfat driver).
65 static int simple_getbool(char *s, bool *setval)
68 if (!strcmp(s, "1") || !strcmp(s, "yes") || !strcmp(s, "true"))
70 else if (!strcmp(s, "0") || !strcmp(s, "no") ||
81 * parse_options - parse the (re)mount options
83 * @opt: string containing the (re)mount options
85 * Parse the recognized options in @opt for the ntfs volume described by @vol.
87 static bool parse_options(ntfs_volume *vol, char *opt)
90 static char *utf8 = "utf8";
91 int errors = 0, sloppy = 0;
92 kuid_t uid = INVALID_UID;
93 kgid_t gid = INVALID_GID;
94 umode_t fmask = (umode_t)-1, dmask = (umode_t)-1;
95 int mft_zone_multiplier = -1, on_errors = -1;
96 int show_sys_files = -1, case_sensitive = -1, disable_sparse = -1;
97 struct nls_table *nls_map = NULL, *old_nls;
99 /* I am lazy... (-8 */
100 #define NTFS_GETOPT_WITH_DEFAULT(option, variable, default_value) \
101 if (!strcmp(p, option)) { \
103 variable = default_value; \
105 variable = simple_strtoul(ov = v, &v, 0); \
110 #define NTFS_GETOPT(option, variable) \
111 if (!strcmp(p, option)) { \
114 variable = simple_strtoul(ov = v, &v, 0); \
118 #define NTFS_GETOPT_UID(option, variable) \
119 if (!strcmp(p, option)) { \
123 uid_value = simple_strtoul(ov = v, &v, 0); \
126 variable = make_kuid(current_user_ns(), uid_value); \
127 if (!uid_valid(variable)) \
130 #define NTFS_GETOPT_GID(option, variable) \
131 if (!strcmp(p, option)) { \
135 gid_value = simple_strtoul(ov = v, &v, 0); \
138 variable = make_kgid(current_user_ns(), gid_value); \
139 if (!gid_valid(variable)) \
142 #define NTFS_GETOPT_OCTAL(option, variable) \
143 if (!strcmp(p, option)) { \
146 variable = simple_strtoul(ov = v, &v, 8); \
150 #define NTFS_GETOPT_BOOL(option, variable) \
151 if (!strcmp(p, option)) { \
153 if (!simple_getbool(v, &val)) \
157 #define NTFS_GETOPT_OPTIONS_ARRAY(option, variable, opt_array) \
158 if (!strcmp(p, option)) { \
163 if (variable == -1) \
165 for (_i = 0; opt_array[_i].str && *opt_array[_i].str; _i++) \
166 if (!strcmp(opt_array[_i].str, v)) { \
167 variable |= opt_array[_i].val; \
170 if (!opt_array[_i].str || !*opt_array[_i].str) \
174 goto no_mount_options;
175 ntfs_debug("Entering with mount options string: %s", opt);
176 while ((p = strsep(&opt, ","))) {
177 if ((v = strchr(p, '=')))
179 NTFS_GETOPT_UID("uid", uid)
180 else NTFS_GETOPT_GID("gid", gid)
181 else NTFS_GETOPT_OCTAL("umask", fmask = dmask)
182 else NTFS_GETOPT_OCTAL("fmask", fmask)
183 else NTFS_GETOPT_OCTAL("dmask", dmask)
184 else NTFS_GETOPT("mft_zone_multiplier", mft_zone_multiplier)
185 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, true)
186 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files)
187 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive)
188 else NTFS_GETOPT_BOOL("disable_sparse", disable_sparse)
189 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors,
191 else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes"))
192 ntfs_warning(vol->sb, "Ignoring obsolete option %s.",
194 else if (!strcmp(p, "nls") || !strcmp(p, "iocharset")) {
195 if (!strcmp(p, "iocharset"))
196 ntfs_warning(vol->sb, "Option iocharset is "
197 "deprecated. Please use "
198 "option nls=<charsetname> in "
204 nls_map = load_nls(v);
207 ntfs_error(vol->sb, "NLS character set "
211 ntfs_error(vol->sb, "NLS character set %s not "
212 "found. Using previous one %s.",
213 v, old_nls->charset);
215 } else /* nls_map */ {
218 } else if (!strcmp(p, "utf8")) {
220 ntfs_warning(vol->sb, "Option utf8 is no longer "
221 "supported, using option nls=utf8. Please "
222 "use option nls=utf8 in the future and "
223 "make sure utf8 is compiled either as a "
224 "module or into the kernel.");
227 else if (!simple_getbool(v, &val))
234 ntfs_error(vol->sb, "Unrecognized mount option %s.", p);
235 if (errors < INT_MAX)
238 #undef NTFS_GETOPT_OPTIONS_ARRAY
239 #undef NTFS_GETOPT_BOOL
241 #undef NTFS_GETOPT_WITH_DEFAULT
244 if (errors && !sloppy)
247 ntfs_warning(vol->sb, "Sloppy option given. Ignoring "
248 "unrecognized mount option(s) and continuing.");
249 /* Keep this first! */
250 if (on_errors != -1) {
252 ntfs_error(vol->sb, "Invalid errors option argument "
253 "or bug in options parser.");
258 if (vol->nls_map && vol->nls_map != nls_map) {
259 ntfs_error(vol->sb, "Cannot change NLS character set "
262 } /* else (!vol->nls_map) */
263 ntfs_debug("Using NLS character set %s.", nls_map->charset);
264 vol->nls_map = nls_map;
265 } else /* (!nls_map) */ {
267 vol->nls_map = load_nls_default();
269 ntfs_error(vol->sb, "Failed to load default "
270 "NLS character set.");
273 ntfs_debug("Using default NLS character set (%s).",
274 vol->nls_map->charset);
277 if (mft_zone_multiplier != -1) {
278 if (vol->mft_zone_multiplier && vol->mft_zone_multiplier !=
279 mft_zone_multiplier) {
280 ntfs_error(vol->sb, "Cannot change mft_zone_multiplier "
284 if (mft_zone_multiplier < 1 || mft_zone_multiplier > 4) {
285 ntfs_error(vol->sb, "Invalid mft_zone_multiplier. "
286 "Using default value, i.e. 1.");
287 mft_zone_multiplier = 1;
289 vol->mft_zone_multiplier = mft_zone_multiplier;
291 if (!vol->mft_zone_multiplier)
292 vol->mft_zone_multiplier = 1;
294 vol->on_errors = on_errors;
295 if (!vol->on_errors || vol->on_errors == ON_ERRORS_RECOVER)
296 vol->on_errors |= ON_ERRORS_CONTINUE;
301 if (fmask != (umode_t)-1)
303 if (dmask != (umode_t)-1)
305 if (show_sys_files != -1) {
307 NVolSetShowSystemFiles(vol);
309 NVolClearShowSystemFiles(vol);
311 if (case_sensitive != -1) {
313 NVolSetCaseSensitive(vol);
315 NVolClearCaseSensitive(vol);
317 if (disable_sparse != -1) {
319 NVolClearSparseEnabled(vol);
321 if (!NVolSparseEnabled(vol) &&
322 vol->major_ver && vol->major_ver < 3)
323 ntfs_warning(vol->sb, "Not enabling sparse "
324 "support due to NTFS volume "
325 "version %i.%i (need at least "
326 "version 3.0).", vol->major_ver,
329 NVolSetSparseEnabled(vol);
334 ntfs_error(vol->sb, "The %s option requires an argument.", p);
337 ntfs_error(vol->sb, "The %s option requires a boolean argument.", p);
340 ntfs_error(vol->sb, "Invalid %s option argument: %s", p, ov);
347 * ntfs_write_volume_flags - write new flags to the volume information flags
348 * @vol: ntfs volume on which to modify the flags
349 * @flags: new flags value for the volume information flags
351 * Internal function. You probably want to use ntfs_{set,clear}_volume_flags()
352 * instead (see below).
354 * Replace the volume information flags on the volume @vol with the value
355 * supplied in @flags. Note, this overwrites the volume information flags, so
356 * make sure to combine the flags you want to modify with the old flags and use
357 * the result when calling ntfs_write_volume_flags().
359 * Return 0 on success and -errno on error.
361 static int ntfs_write_volume_flags(ntfs_volume *vol, const VOLUME_FLAGS flags)
363 ntfs_inode *ni = NTFS_I(vol->vol_ino);
365 VOLUME_INFORMATION *vi;
366 ntfs_attr_search_ctx *ctx;
369 ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
370 le16_to_cpu(vol->vol_flags), le16_to_cpu(flags));
371 if (vol->vol_flags == flags)
374 m = map_mft_record(ni);
379 ctx = ntfs_attr_get_search_ctx(ni, m);
382 goto put_unm_err_out;
384 err = ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
387 goto put_unm_err_out;
388 vi = (VOLUME_INFORMATION*)((u8*)ctx->attr +
389 le16_to_cpu(ctx->attr->data.resident.value_offset));
390 vol->vol_flags = vi->flags = flags;
391 flush_dcache_mft_record_page(ctx->ntfs_ino);
392 mark_mft_record_dirty(ctx->ntfs_ino);
393 ntfs_attr_put_search_ctx(ctx);
394 unmap_mft_record(ni);
400 ntfs_attr_put_search_ctx(ctx);
401 unmap_mft_record(ni);
403 ntfs_error(vol->sb, "Failed with error code %i.", -err);
408 * ntfs_set_volume_flags - set bits in the volume information flags
409 * @vol: ntfs volume on which to modify the flags
410 * @flags: flags to set on the volume
412 * Set the bits in @flags in the volume information flags on the volume @vol.
414 * Return 0 on success and -errno on error.
416 static inline int ntfs_set_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
418 flags &= VOLUME_FLAGS_MASK;
419 return ntfs_write_volume_flags(vol, vol->vol_flags | flags);
423 * ntfs_clear_volume_flags - clear bits in the volume information flags
424 * @vol: ntfs volume on which to modify the flags
425 * @flags: flags to clear on the volume
427 * Clear the bits in @flags in the volume information flags on the volume @vol.
429 * Return 0 on success and -errno on error.
431 static inline int ntfs_clear_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
433 flags &= VOLUME_FLAGS_MASK;
434 flags = vol->vol_flags & cpu_to_le16(~le16_to_cpu(flags));
435 return ntfs_write_volume_flags(vol, flags);
441 * ntfs_remount - change the mount options of a mounted ntfs filesystem
442 * @sb: superblock of mounted ntfs filesystem
443 * @flags: remount flags
444 * @opt: remount options string
446 * Change the mount options of an already mounted ntfs filesystem.
448 * NOTE: The VFS sets the @sb->s_flags remount flags to @flags after
449 * ntfs_remount() returns successfully (i.e. returns 0). Otherwise,
450 * @sb->s_flags are not changed.
452 static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
454 ntfs_volume *vol = NTFS_SB(sb);
456 ntfs_debug("Entering with remount options string: %s", opt);
461 /* For read-only compiled driver, enforce read-only flag. */
465 * For the read-write compiled driver, if we are remounting read-write,
466 * make sure there are no volume errors and that no unsupported volume
467 * flags are set. Also, empty the logfile journal as it would become
468 * stale as soon as something is written to the volume and mark the
469 * volume dirty so that chkdsk is run if the volume is not umounted
470 * cleanly. Finally, mark the quotas out of date so Windows rescans
471 * the volume on boot and updates them.
473 * When remounting read-only, mark the volume clean if no volume errors
476 if (sb_rdonly(sb) && !(*flags & SB_RDONLY)) {
477 static const char *es = ". Cannot remount read-write.";
479 /* Remounting read-write. */
480 if (NVolErrors(vol)) {
481 ntfs_error(sb, "Volume has errors and is read-only%s",
485 if (vol->vol_flags & VOLUME_IS_DIRTY) {
486 ntfs_error(sb, "Volume is dirty and read-only%s", es);
489 if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
490 ntfs_error(sb, "Volume has been modified by chkdsk "
491 "and is read-only%s", es);
494 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
495 ntfs_error(sb, "Volume has unsupported flags set "
496 "(0x%x) and is read-only%s",
497 (unsigned)le16_to_cpu(vol->vol_flags),
501 if (ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
502 ntfs_error(sb, "Failed to set dirty bit in volume "
503 "information flags%s", es);
507 // TODO: Enable this code once we start modifying anything that
508 // is different between NTFS 1.2 and 3.x...
509 /* Set NT4 compatibility flag on newer NTFS version volumes. */
510 if ((vol->major_ver > 1)) {
511 if (ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
512 ntfs_error(sb, "Failed to set NT4 "
513 "compatibility flag%s", es);
519 if (!ntfs_empty_logfile(vol->logfile_ino)) {
520 ntfs_error(sb, "Failed to empty journal $LogFile%s",
525 if (!ntfs_mark_quotas_out_of_date(vol)) {
526 ntfs_error(sb, "Failed to mark quotas out of date%s",
531 if (!ntfs_stamp_usnjrnl(vol)) {
532 ntfs_error(sb, "Failed to stamp transaction log "
537 } else if (!sb_rdonly(sb) && (*flags & SB_RDONLY)) {
538 /* Remounting read-only. */
539 if (!NVolErrors(vol)) {
540 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
541 ntfs_warning(sb, "Failed to clear dirty bit "
542 "in volume information "
543 "flags. Run chkdsk.");
548 // TODO: Deal with *flags.
550 if (!parse_options(vol, opt))
558 * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector
559 * @sb: Super block of the device to which @b belongs.
560 * @b: Boot sector of device @sb to check.
561 * @silent: If 'true', all output will be silenced.
563 * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot
564 * sector. Returns 'true' if it is valid and 'false' if not.
566 * @sb is only needed for warning/error output, i.e. it can be NULL when silent
569 static bool is_boot_sector_ntfs(const struct super_block *sb,
570 const NTFS_BOOT_SECTOR *b, const bool silent)
573 * Check that checksum == sum of u32 values from b to the checksum
574 * field. If checksum is zero, no checking is done. We will work when
575 * the checksum test fails, since some utilities update the boot sector
576 * ignoring the checksum which leaves the checksum out-of-date. We
577 * report a warning if this is the case.
579 if ((void*)b < (void*)&b->checksum && b->checksum && !silent) {
583 for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u)
584 i += le32_to_cpup(u);
585 if (le32_to_cpu(b->checksum) != i)
586 ntfs_warning(sb, "Invalid boot sector checksum.");
588 /* Check OEMidentifier is "NTFS " */
589 if (b->oem_id != magicNTFS)
591 /* Check bytes per sector value is between 256 and 4096. */
592 if (le16_to_cpu(b->bpb.bytes_per_sector) < 0x100 ||
593 le16_to_cpu(b->bpb.bytes_per_sector) > 0x1000)
595 /* Check sectors per cluster value is valid. */
596 switch (b->bpb.sectors_per_cluster) {
597 case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128:
602 /* Check the cluster size is not above the maximum (64kiB). */
603 if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) *
604 b->bpb.sectors_per_cluster > NTFS_MAX_CLUSTER_SIZE)
606 /* Check reserved/unused fields are really zero. */
607 if (le16_to_cpu(b->bpb.reserved_sectors) ||
608 le16_to_cpu(b->bpb.root_entries) ||
609 le16_to_cpu(b->bpb.sectors) ||
610 le16_to_cpu(b->bpb.sectors_per_fat) ||
611 le32_to_cpu(b->bpb.large_sectors) || b->bpb.fats)
613 /* Check clusters per file mft record value is valid. */
614 if ((u8)b->clusters_per_mft_record < 0xe1 ||
615 (u8)b->clusters_per_mft_record > 0xf7)
616 switch (b->clusters_per_mft_record) {
617 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
622 /* Check clusters per index block value is valid. */
623 if ((u8)b->clusters_per_index_record < 0xe1 ||
624 (u8)b->clusters_per_index_record > 0xf7)
625 switch (b->clusters_per_index_record) {
626 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
632 * Check for valid end of sector marker. We will work without it, but
633 * many BIOSes will refuse to boot from a bootsector if the magic is
634 * incorrect, so we emit a warning.
636 if (!silent && b->end_of_sector_marker != cpu_to_le16(0xaa55))
637 ntfs_warning(sb, "Invalid end of sector marker.");
644 * read_ntfs_boot_sector - read the NTFS boot sector of a device
645 * @sb: super block of device to read the boot sector from
646 * @silent: if true, suppress all output
648 * Reads the boot sector from the device and validates it. If that fails, tries
649 * to read the backup boot sector, first from the end of the device a-la NT4 and
650 * later and then from the middle of the device a-la NT3.51 and before.
652 * If a valid boot sector is found but it is not the primary boot sector, we
653 * repair the primary boot sector silently (unless the device is read-only or
654 * the primary boot sector is not accessible).
656 * NOTE: To call this function, @sb must have the fields s_dev, the ntfs super
657 * block (u.ntfs_sb), nr_blocks and the device flags (s_flags) initialized
658 * to their respective values.
660 * Return the unlocked buffer head containing the boot sector or NULL on error.
662 static struct buffer_head *read_ntfs_boot_sector(struct super_block *sb,
665 const char *read_err_str = "Unable to read %s boot sector.";
666 struct buffer_head *bh_primary, *bh_backup;
667 sector_t nr_blocks = NTFS_SB(sb)->nr_blocks;
669 /* Try to read primary boot sector. */
670 if ((bh_primary = sb_bread(sb, 0))) {
671 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
672 bh_primary->b_data, silent))
675 ntfs_error(sb, "Primary boot sector is invalid.");
677 ntfs_error(sb, read_err_str, "primary");
678 if (!(NTFS_SB(sb)->on_errors & ON_ERRORS_RECOVER)) {
682 ntfs_error(sb, "Mount option errors=recover not used. "
683 "Aborting without trying to recover.");
686 /* Try to read NT4+ backup boot sector. */
687 if ((bh_backup = sb_bread(sb, nr_blocks - 1))) {
688 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
689 bh_backup->b_data, silent))
690 goto hotfix_primary_boot_sector;
693 ntfs_error(sb, read_err_str, "backup");
694 /* Try to read NT3.51- backup boot sector. */
695 if ((bh_backup = sb_bread(sb, nr_blocks >> 1))) {
696 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
697 bh_backup->b_data, silent))
698 goto hotfix_primary_boot_sector;
700 ntfs_error(sb, "Could not find a valid backup boot "
704 ntfs_error(sb, read_err_str, "backup");
705 /* We failed. Cleanup and return. */
709 hotfix_primary_boot_sector:
712 * If we managed to read sector zero and the volume is not
713 * read-only, copy the found, valid backup boot sector to the
714 * primary boot sector. Note we only copy the actual boot
715 * sector structure, not the actual whole device sector as that
716 * may be bigger and would potentially damage the $Boot system
717 * file (FIXME: Would be nice to know if the backup boot sector
718 * on a large sector device contains the whole boot loader or
719 * just the first 512 bytes).
721 if (!sb_rdonly(sb)) {
722 ntfs_warning(sb, "Hot-fix: Recovering invalid primary "
723 "boot sector from backup copy.");
724 memcpy(bh_primary->b_data, bh_backup->b_data,
726 mark_buffer_dirty(bh_primary);
727 sync_dirty_buffer(bh_primary);
728 if (buffer_uptodate(bh_primary)) {
732 ntfs_error(sb, "Hot-fix: Device write error while "
733 "recovering primary boot sector.");
735 ntfs_warning(sb, "Hot-fix: Recovery of primary boot "
736 "sector failed: Read-only mount.");
740 ntfs_warning(sb, "Using backup boot sector.");
745 * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol
746 * @vol: volume structure to initialise with data from boot sector
747 * @b: boot sector to parse
749 * Parse the ntfs boot sector @b and store all imporant information therein in
750 * the ntfs super block @vol. Return 'true' on success and 'false' on error.
752 static bool parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
754 unsigned int sectors_per_cluster_bits, nr_hidden_sects;
755 int clusters_per_mft_record, clusters_per_index_record;
758 vol->sector_size = le16_to_cpu(b->bpb.bytes_per_sector);
759 vol->sector_size_bits = ffs(vol->sector_size) - 1;
760 ntfs_debug("vol->sector_size = %i (0x%x)", vol->sector_size,
762 ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits,
763 vol->sector_size_bits);
764 if (vol->sector_size < vol->sb->s_blocksize) {
765 ntfs_error(vol->sb, "Sector size (%i) is smaller than the "
766 "device block size (%lu). This is not "
767 "supported. Sorry.", vol->sector_size,
768 vol->sb->s_blocksize);
771 ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster);
772 sectors_per_cluster_bits = ffs(b->bpb.sectors_per_cluster) - 1;
773 ntfs_debug("sectors_per_cluster_bits = 0x%x",
774 sectors_per_cluster_bits);
775 nr_hidden_sects = le32_to_cpu(b->bpb.hidden_sectors);
776 ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects);
777 vol->cluster_size = vol->sector_size << sectors_per_cluster_bits;
778 vol->cluster_size_mask = vol->cluster_size - 1;
779 vol->cluster_size_bits = ffs(vol->cluster_size) - 1;
780 ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size,
782 ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask);
783 ntfs_debug("vol->cluster_size_bits = %i", vol->cluster_size_bits);
784 if (vol->cluster_size < vol->sector_size) {
785 ntfs_error(vol->sb, "Cluster size (%i) is smaller than the "
786 "sector size (%i). This is not supported. "
787 "Sorry.", vol->cluster_size, vol->sector_size);
790 clusters_per_mft_record = b->clusters_per_mft_record;
791 ntfs_debug("clusters_per_mft_record = %i (0x%x)",
792 clusters_per_mft_record, clusters_per_mft_record);
793 if (clusters_per_mft_record > 0)
794 vol->mft_record_size = vol->cluster_size <<
795 (ffs(clusters_per_mft_record) - 1);
798 * When mft_record_size < cluster_size, clusters_per_mft_record
799 * = -log2(mft_record_size) bytes. mft_record_size normaly is
800 * 1024 bytes, which is encoded as 0xF6 (-10 in decimal).
802 vol->mft_record_size = 1 << -clusters_per_mft_record;
803 vol->mft_record_size_mask = vol->mft_record_size - 1;
804 vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1;
805 ntfs_debug("vol->mft_record_size = %i (0x%x)", vol->mft_record_size,
806 vol->mft_record_size);
807 ntfs_debug("vol->mft_record_size_mask = 0x%x",
808 vol->mft_record_size_mask);
809 ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
810 vol->mft_record_size_bits, vol->mft_record_size_bits);
812 * We cannot support mft record sizes above the PAGE_SIZE since
813 * we store $MFT/$DATA, the table of mft records in the page cache.
815 if (vol->mft_record_size > PAGE_SIZE) {
816 ntfs_error(vol->sb, "Mft record size (%i) exceeds the "
817 "PAGE_SIZE on your system (%lu). "
818 "This is not supported. Sorry.",
819 vol->mft_record_size, PAGE_SIZE);
822 /* We cannot support mft record sizes below the sector size. */
823 if (vol->mft_record_size < vol->sector_size) {
824 ntfs_error(vol->sb, "Mft record size (%i) is smaller than the "
825 "sector size (%i). This is not supported. "
826 "Sorry.", vol->mft_record_size,
830 clusters_per_index_record = b->clusters_per_index_record;
831 ntfs_debug("clusters_per_index_record = %i (0x%x)",
832 clusters_per_index_record, clusters_per_index_record);
833 if (clusters_per_index_record > 0)
834 vol->index_record_size = vol->cluster_size <<
835 (ffs(clusters_per_index_record) - 1);
838 * When index_record_size < cluster_size,
839 * clusters_per_index_record = -log2(index_record_size) bytes.
840 * index_record_size normaly equals 4096 bytes, which is
841 * encoded as 0xF4 (-12 in decimal).
843 vol->index_record_size = 1 << -clusters_per_index_record;
844 vol->index_record_size_mask = vol->index_record_size - 1;
845 vol->index_record_size_bits = ffs(vol->index_record_size) - 1;
846 ntfs_debug("vol->index_record_size = %i (0x%x)",
847 vol->index_record_size, vol->index_record_size);
848 ntfs_debug("vol->index_record_size_mask = 0x%x",
849 vol->index_record_size_mask);
850 ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
851 vol->index_record_size_bits,
852 vol->index_record_size_bits);
853 /* We cannot support index record sizes below the sector size. */
854 if (vol->index_record_size < vol->sector_size) {
855 ntfs_error(vol->sb, "Index record size (%i) is smaller than "
856 "the sector size (%i). This is not "
857 "supported. Sorry.", vol->index_record_size,
862 * Get the size of the volume in clusters and check for 64-bit-ness.
863 * Windows currently only uses 32 bits to save the clusters so we do
864 * the same as it is much faster on 32-bit CPUs.
866 ll = sle64_to_cpu(b->number_of_sectors) >> sectors_per_cluster_bits;
867 if ((u64)ll >= 1ULL << 32) {
868 ntfs_error(vol->sb, "Cannot handle 64-bit clusters. Sorry.");
871 vol->nr_clusters = ll;
872 ntfs_debug("vol->nr_clusters = 0x%llx", (long long)vol->nr_clusters);
874 * On an architecture where unsigned long is 32-bits, we restrict the
875 * volume size to 2TiB (2^41). On a 64-bit architecture, the compiler
876 * will hopefully optimize the whole check away.
878 if (sizeof(unsigned long) < 8) {
879 if ((ll << vol->cluster_size_bits) >= (1ULL << 41)) {
880 ntfs_error(vol->sb, "Volume size (%lluTiB) is too "
881 "large for this architecture. "
882 "Maximum supported is 2TiB. Sorry.",
883 (unsigned long long)ll >> (40 -
884 vol->cluster_size_bits));
888 ll = sle64_to_cpu(b->mft_lcn);
889 if (ll >= vol->nr_clusters) {
890 ntfs_error(vol->sb, "MFT LCN (%lli, 0x%llx) is beyond end of "
891 "volume. Weird.", (unsigned long long)ll,
892 (unsigned long long)ll);
896 ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol->mft_lcn);
897 ll = sle64_to_cpu(b->mftmirr_lcn);
898 if (ll >= vol->nr_clusters) {
899 ntfs_error(vol->sb, "MFTMirr LCN (%lli, 0x%llx) is beyond end "
900 "of volume. Weird.", (unsigned long long)ll,
901 (unsigned long long)ll);
904 vol->mftmirr_lcn = ll;
905 ntfs_debug("vol->mftmirr_lcn = 0x%llx", (long long)vol->mftmirr_lcn);
908 * Work out the size of the mft mirror in number of mft records. If the
909 * cluster size is less than or equal to the size taken by four mft
910 * records, the mft mirror stores the first four mft records. If the
911 * cluster size is bigger than the size taken by four mft records, the
912 * mft mirror contains as many mft records as will fit into one
915 if (vol->cluster_size <= (4 << vol->mft_record_size_bits))
916 vol->mftmirr_size = 4;
918 vol->mftmirr_size = vol->cluster_size >>
919 vol->mft_record_size_bits;
920 ntfs_debug("vol->mftmirr_size = %i", vol->mftmirr_size);
922 vol->serial_no = le64_to_cpu(b->volume_serial_number);
923 ntfs_debug("vol->serial_no = 0x%llx",
924 (unsigned long long)vol->serial_no);
929 * ntfs_setup_allocators - initialize the cluster and mft allocators
930 * @vol: volume structure for which to setup the allocators
932 * Setup the cluster (lcn) and mft allocators to the starting values.
934 static void ntfs_setup_allocators(ntfs_volume *vol)
937 LCN mft_zone_size, mft_lcn;
940 ntfs_debug("vol->mft_zone_multiplier = 0x%x",
941 vol->mft_zone_multiplier);
943 /* Determine the size of the MFT zone. */
944 mft_zone_size = vol->nr_clusters;
945 switch (vol->mft_zone_multiplier) { /* % of volume size in clusters */
947 mft_zone_size >>= 1; /* 50% */
950 mft_zone_size = (mft_zone_size +
951 (mft_zone_size >> 1)) >> 2; /* 37.5% */
954 mft_zone_size >>= 2; /* 25% */
958 mft_zone_size >>= 3; /* 12.5% */
961 /* Setup the mft zone. */
962 vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn;
963 ntfs_debug("vol->mft_zone_pos = 0x%llx",
964 (unsigned long long)vol->mft_zone_pos);
966 * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
967 * source) and if the actual mft_lcn is in the expected place or even
968 * further to the front of the volume, extend the mft_zone to cover the
969 * beginning of the volume as well. This is in order to protect the
970 * area reserved for the mft bitmap as well within the mft_zone itself.
971 * On non-standard volumes we do not protect it as the overhead would
972 * be higher than the speed increase we would get by doing it.
974 mft_lcn = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size;
975 if (mft_lcn * vol->cluster_size < 16 * 1024)
976 mft_lcn = (16 * 1024 + vol->cluster_size - 1) /
978 if (vol->mft_zone_start <= mft_lcn)
979 vol->mft_zone_start = 0;
980 ntfs_debug("vol->mft_zone_start = 0x%llx",
981 (unsigned long long)vol->mft_zone_start);
983 * Need to cap the mft zone on non-standard volumes so that it does
984 * not point outside the boundaries of the volume. We do this by
985 * halving the zone size until we are inside the volume.
987 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
988 while (vol->mft_zone_end >= vol->nr_clusters) {
990 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
992 ntfs_debug("vol->mft_zone_end = 0x%llx",
993 (unsigned long long)vol->mft_zone_end);
995 * Set the current position within each data zone to the start of the
998 vol->data1_zone_pos = vol->mft_zone_end;
999 ntfs_debug("vol->data1_zone_pos = 0x%llx",
1000 (unsigned long long)vol->data1_zone_pos);
1001 vol->data2_zone_pos = 0;
1002 ntfs_debug("vol->data2_zone_pos = 0x%llx",
1003 (unsigned long long)vol->data2_zone_pos);
1005 /* Set the mft data allocation position to mft record 24. */
1006 vol->mft_data_pos = 24;
1007 ntfs_debug("vol->mft_data_pos = 0x%llx",
1008 (unsigned long long)vol->mft_data_pos);
1009 #endif /* NTFS_RW */
1015 * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume
1016 * @vol: ntfs super block describing device whose mft mirror to load
1018 * Return 'true' on success or 'false' on error.
1020 static bool load_and_init_mft_mirror(ntfs_volume *vol)
1022 struct inode *tmp_ino;
1025 ntfs_debug("Entering.");
1026 /* Get mft mirror inode. */
1027 tmp_ino = ntfs_iget(vol->sb, FILE_MFTMirr);
1028 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1029 if (!IS_ERR(tmp_ino))
1031 /* Caller will display error message. */
1035 * Re-initialize some specifics about $MFTMirr's inode as
1036 * ntfs_read_inode() will have set up the default ones.
1038 /* Set uid and gid to root. */
1039 tmp_ino->i_uid = GLOBAL_ROOT_UID;
1040 tmp_ino->i_gid = GLOBAL_ROOT_GID;
1041 /* Regular file. No access for anyone. */
1042 tmp_ino->i_mode = S_IFREG;
1043 /* No VFS initiated operations allowed for $MFTMirr. */
1044 tmp_ino->i_op = &ntfs_empty_inode_ops;
1045 tmp_ino->i_fop = &ntfs_empty_file_ops;
1046 /* Put in our special address space operations. */
1047 tmp_ino->i_mapping->a_ops = &ntfs_mst_aops;
1048 tmp_ni = NTFS_I(tmp_ino);
1049 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
1050 NInoSetMstProtected(tmp_ni);
1051 NInoSetSparseDisabled(tmp_ni);
1053 * Set up our little cheat allowing us to reuse the async read io
1054 * completion handler for directories.
1056 tmp_ni->itype.index.block_size = vol->mft_record_size;
1057 tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits;
1058 vol->mftmirr_ino = tmp_ino;
1059 ntfs_debug("Done.");
1064 * check_mft_mirror - compare contents of the mft mirror with the mft
1065 * @vol: ntfs super block describing device whose mft mirror to check
1067 * Return 'true' on success or 'false' on error.
1069 * Note, this function also results in the mft mirror runlist being completely
1070 * mapped into memory. The mft mirror write code requires this and will BUG()
1071 * should it find an unmapped runlist element.
1073 static bool check_mft_mirror(ntfs_volume *vol)
1075 struct super_block *sb = vol->sb;
1076 ntfs_inode *mirr_ni;
1077 struct page *mft_page, *mirr_page;
1079 runlist_element *rl, rl2[2];
1081 int mrecs_per_page, i;
1083 ntfs_debug("Entering.");
1084 /* Compare contents of $MFT and $MFTMirr. */
1085 mrecs_per_page = PAGE_SIZE / vol->mft_record_size;
1086 BUG_ON(!mrecs_per_page);
1087 BUG_ON(!vol->mftmirr_size);
1088 mft_page = mirr_page = NULL;
1089 kmft = kmirr = NULL;
1094 /* Switch pages if necessary. */
1095 if (!(i % mrecs_per_page)) {
1097 ntfs_unmap_page(mft_page);
1098 ntfs_unmap_page(mirr_page);
1100 /* Get the $MFT page. */
1101 mft_page = ntfs_map_page(vol->mft_ino->i_mapping,
1103 if (IS_ERR(mft_page)) {
1104 ntfs_error(sb, "Failed to read $MFT.");
1107 kmft = page_address(mft_page);
1108 /* Get the $MFTMirr page. */
1109 mirr_page = ntfs_map_page(vol->mftmirr_ino->i_mapping,
1111 if (IS_ERR(mirr_page)) {
1112 ntfs_error(sb, "Failed to read $MFTMirr.");
1115 kmirr = page_address(mirr_page);
1118 /* Do not check the record if it is not in use. */
1119 if (((MFT_RECORD*)kmft)->flags & MFT_RECORD_IN_USE) {
1120 /* Make sure the record is ok. */
1121 if (ntfs_is_baad_recordp((le32*)kmft)) {
1122 ntfs_error(sb, "Incomplete multi sector "
1123 "transfer detected in mft "
1126 ntfs_unmap_page(mirr_page);
1128 ntfs_unmap_page(mft_page);
1132 /* Do not check the mirror record if it is not in use. */
1133 if (((MFT_RECORD*)kmirr)->flags & MFT_RECORD_IN_USE) {
1134 if (ntfs_is_baad_recordp((le32*)kmirr)) {
1135 ntfs_error(sb, "Incomplete multi sector "
1136 "transfer detected in mft "
1137 "mirror record %i.", i);
1141 /* Get the amount of data in the current record. */
1142 bytes = le32_to_cpu(((MFT_RECORD*)kmft)->bytes_in_use);
1143 if (bytes < sizeof(MFT_RECORD_OLD) ||
1144 bytes > vol->mft_record_size ||
1145 ntfs_is_baad_recordp((le32*)kmft)) {
1146 bytes = le32_to_cpu(((MFT_RECORD*)kmirr)->bytes_in_use);
1147 if (bytes < sizeof(MFT_RECORD_OLD) ||
1148 bytes > vol->mft_record_size ||
1149 ntfs_is_baad_recordp((le32*)kmirr))
1150 bytes = vol->mft_record_size;
1152 /* Compare the two records. */
1153 if (memcmp(kmft, kmirr, bytes)) {
1154 ntfs_error(sb, "$MFT and $MFTMirr (record %i) do not "
1155 "match. Run ntfsfix or chkdsk.", i);
1158 kmft += vol->mft_record_size;
1159 kmirr += vol->mft_record_size;
1160 } while (++i < vol->mftmirr_size);
1161 /* Release the last pages. */
1162 ntfs_unmap_page(mft_page);
1163 ntfs_unmap_page(mirr_page);
1165 /* Construct the mft mirror runlist by hand. */
1167 rl2[0].lcn = vol->mftmirr_lcn;
1168 rl2[0].length = (vol->mftmirr_size * vol->mft_record_size +
1169 vol->cluster_size - 1) / vol->cluster_size;
1170 rl2[1].vcn = rl2[0].length;
1171 rl2[1].lcn = LCN_ENOENT;
1174 * Because we have just read all of the mft mirror, we know we have
1175 * mapped the full runlist for it.
1177 mirr_ni = NTFS_I(vol->mftmirr_ino);
1178 down_read(&mirr_ni->runlist.lock);
1179 rl = mirr_ni->runlist.rl;
1180 /* Compare the two runlists. They must be identical. */
1183 if (rl2[i].vcn != rl[i].vcn || rl2[i].lcn != rl[i].lcn ||
1184 rl2[i].length != rl[i].length) {
1185 ntfs_error(sb, "$MFTMirr location mismatch. "
1187 up_read(&mirr_ni->runlist.lock);
1190 } while (rl2[i++].length);
1191 up_read(&mirr_ni->runlist.lock);
1192 ntfs_debug("Done.");
1197 * load_and_check_logfile - load and check the logfile inode for a volume
1198 * @vol: ntfs super block describing device whose logfile to load
1200 * Return 'true' on success or 'false' on error.
1202 static bool load_and_check_logfile(ntfs_volume *vol,
1203 RESTART_PAGE_HEADER **rp)
1205 struct inode *tmp_ino;
1207 ntfs_debug("Entering.");
1208 tmp_ino = ntfs_iget(vol->sb, FILE_LogFile);
1209 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1210 if (!IS_ERR(tmp_ino))
1212 /* Caller will display error message. */
1215 if (!ntfs_check_logfile(tmp_ino, rp)) {
1217 /* ntfs_check_logfile() will have displayed error output. */
1220 NInoSetSparseDisabled(NTFS_I(tmp_ino));
1221 vol->logfile_ino = tmp_ino;
1222 ntfs_debug("Done.");
1226 #define NTFS_HIBERFIL_HEADER_SIZE 4096
1229 * check_windows_hibernation_status - check if Windows is suspended on a volume
1230 * @vol: ntfs super block of device to check
1232 * Check if Windows is hibernated on the ntfs volume @vol. This is done by
1233 * looking for the file hiberfil.sys in the root directory of the volume. If
1234 * the file is not present Windows is definitely not suspended.
1236 * If hiberfil.sys exists and is less than 4kiB in size it means Windows is
1237 * definitely suspended (this volume is not the system volume). Caveat: on a
1238 * system with many volumes it is possible that the < 4kiB check is bogus but
1239 * for now this should do fine.
1241 * If hiberfil.sys exists and is larger than 4kiB in size, we need to read the
1242 * hiberfil header (which is the first 4kiB). If this begins with "hibr",
1243 * Windows is definitely suspended. If it is completely full of zeroes,
1244 * Windows is definitely not hibernated. Any other case is treated as if
1245 * Windows is suspended. This caters for the above mentioned caveat of a
1246 * system with many volumes where no "hibr" magic would be present and there is
1249 * Return 0 if Windows is not hibernated on the volume, >0 if Windows is
1250 * hibernated on the volume, and -errno on error.
1252 static int check_windows_hibernation_status(ntfs_volume *vol)
1258 ntfs_name *name = NULL;
1260 static const ntfschar hiberfil[13] = { cpu_to_le16('h'),
1261 cpu_to_le16('i'), cpu_to_le16('b'),
1262 cpu_to_le16('e'), cpu_to_le16('r'),
1263 cpu_to_le16('f'), cpu_to_le16('i'),
1264 cpu_to_le16('l'), cpu_to_le16('.'),
1265 cpu_to_le16('s'), cpu_to_le16('y'),
1266 cpu_to_le16('s'), 0 };
1268 ntfs_debug("Entering.");
1270 * Find the inode number for the hibernation file by looking up the
1271 * filename hiberfil.sys in the root directory.
1273 inode_lock(vol->root_ino);
1274 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->root_ino), hiberfil, 12,
1276 inode_unlock(vol->root_ino);
1277 if (IS_ERR_MREF(mref)) {
1278 ret = MREF_ERR(mref);
1279 /* If the file does not exist, Windows is not hibernated. */
1280 if (ret == -ENOENT) {
1281 ntfs_debug("hiberfil.sys not present. Windows is not "
1282 "hibernated on the volume.");
1285 /* A real error occurred. */
1286 ntfs_error(vol->sb, "Failed to find inode number for "
1290 /* We do not care for the type of match that was found. */
1292 /* Get the inode. */
1293 vi = ntfs_iget(vol->sb, MREF(mref));
1294 if (IS_ERR(vi) || is_bad_inode(vi)) {
1297 ntfs_error(vol->sb, "Failed to load hiberfil.sys.");
1298 return IS_ERR(vi) ? PTR_ERR(vi) : -EIO;
1300 if (unlikely(i_size_read(vi) < NTFS_HIBERFIL_HEADER_SIZE)) {
1301 ntfs_debug("hiberfil.sys is smaller than 4kiB (0x%llx). "
1302 "Windows is hibernated on the volume. This "
1303 "is not the system volume.", i_size_read(vi));
1306 page = ntfs_map_page(vi->i_mapping, 0);
1308 ntfs_error(vol->sb, "Failed to read from hiberfil.sys.");
1309 ret = PTR_ERR(page);
1312 kaddr = (u32*)page_address(page);
1313 if (*(le32*)kaddr == cpu_to_le32(0x72626968)/*'hibr'*/) {
1314 ntfs_debug("Magic \"hibr\" found in hiberfil.sys. Windows is "
1315 "hibernated on the volume. This is the "
1319 kend = kaddr + NTFS_HIBERFIL_HEADER_SIZE/sizeof(*kaddr);
1321 if (unlikely(*kaddr)) {
1322 ntfs_debug("hiberfil.sys is larger than 4kiB "
1323 "(0x%llx), does not contain the "
1324 "\"hibr\" magic, and does not have a "
1325 "zero header. Windows is hibernated "
1326 "on the volume. This is not the "
1327 "system volume.", i_size_read(vi));
1330 } while (++kaddr < kend);
1331 ntfs_debug("hiberfil.sys contains a zero header. Windows is not "
1332 "hibernated on the volume. This is the system "
1336 ntfs_unmap_page(page);
1343 * load_and_init_quota - load and setup the quota file for a volume if present
1344 * @vol: ntfs super block describing device whose quota file to load
1346 * Return 'true' on success or 'false' on error. If $Quota is not present, we
1347 * leave vol->quota_ino as NULL and return success.
1349 static bool load_and_init_quota(ntfs_volume *vol)
1352 struct inode *tmp_ino;
1353 ntfs_name *name = NULL;
1354 static const ntfschar Quota[7] = { cpu_to_le16('$'),
1355 cpu_to_le16('Q'), cpu_to_le16('u'),
1356 cpu_to_le16('o'), cpu_to_le16('t'),
1357 cpu_to_le16('a'), 0 };
1358 static ntfschar Q[3] = { cpu_to_le16('$'),
1359 cpu_to_le16('Q'), 0 };
1361 ntfs_debug("Entering.");
1363 * Find the inode number for the quota file by looking up the filename
1364 * $Quota in the extended system files directory $Extend.
1366 inode_lock(vol->extend_ino);
1367 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), Quota, 6,
1369 inode_unlock(vol->extend_ino);
1370 if (IS_ERR_MREF(mref)) {
1372 * If the file does not exist, quotas are disabled and have
1373 * never been enabled on this volume, just return success.
1375 if (MREF_ERR(mref) == -ENOENT) {
1376 ntfs_debug("$Quota not present. Volume does not have "
1379 * No need to try to set quotas out of date if they are
1382 NVolSetQuotaOutOfDate(vol);
1385 /* A real error occurred. */
1386 ntfs_error(vol->sb, "Failed to find inode number for $Quota.");
1389 /* We do not care for the type of match that was found. */
1391 /* Get the inode. */
1392 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1393 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1394 if (!IS_ERR(tmp_ino))
1396 ntfs_error(vol->sb, "Failed to load $Quota.");
1399 vol->quota_ino = tmp_ino;
1400 /* Get the $Q index allocation attribute. */
1401 tmp_ino = ntfs_index_iget(vol->quota_ino, Q, 2);
1402 if (IS_ERR(tmp_ino)) {
1403 ntfs_error(vol->sb, "Failed to load $Quota/$Q index.");
1406 vol->quota_q_ino = tmp_ino;
1407 ntfs_debug("Done.");
1412 * load_and_init_usnjrnl - load and setup the transaction log if present
1413 * @vol: ntfs super block describing device whose usnjrnl file to load
1415 * Return 'true' on success or 'false' on error.
1417 * If $UsnJrnl is not present or in the process of being disabled, we set
1418 * NVolUsnJrnlStamped() and return success.
1420 * If the $UsnJrnl $DATA/$J attribute has a size equal to the lowest valid usn,
1421 * i.e. transaction logging has only just been enabled or the journal has been
1422 * stamped and nothing has been logged since, we also set NVolUsnJrnlStamped()
1423 * and return success.
1425 static bool load_and_init_usnjrnl(ntfs_volume *vol)
1428 struct inode *tmp_ino;
1431 ntfs_name *name = NULL;
1433 static const ntfschar UsnJrnl[9] = { cpu_to_le16('$'),
1434 cpu_to_le16('U'), cpu_to_le16('s'),
1435 cpu_to_le16('n'), cpu_to_le16('J'),
1436 cpu_to_le16('r'), cpu_to_le16('n'),
1437 cpu_to_le16('l'), 0 };
1438 static ntfschar Max[5] = { cpu_to_le16('$'),
1439 cpu_to_le16('M'), cpu_to_le16('a'),
1440 cpu_to_le16('x'), 0 };
1441 static ntfschar J[3] = { cpu_to_le16('$'),
1442 cpu_to_le16('J'), 0 };
1444 ntfs_debug("Entering.");
1446 * Find the inode number for the transaction log file by looking up the
1447 * filename $UsnJrnl in the extended system files directory $Extend.
1449 inode_lock(vol->extend_ino);
1450 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), UsnJrnl, 8,
1452 inode_unlock(vol->extend_ino);
1453 if (IS_ERR_MREF(mref)) {
1455 * If the file does not exist, transaction logging is disabled,
1456 * just return success.
1458 if (MREF_ERR(mref) == -ENOENT) {
1459 ntfs_debug("$UsnJrnl not present. Volume does not "
1460 "have transaction logging enabled.");
1463 * No need to try to stamp the transaction log if
1464 * transaction logging is not enabled.
1466 NVolSetUsnJrnlStamped(vol);
1469 /* A real error occurred. */
1470 ntfs_error(vol->sb, "Failed to find inode number for "
1474 /* We do not care for the type of match that was found. */
1476 /* Get the inode. */
1477 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1478 if (IS_ERR(tmp_ino) || unlikely(is_bad_inode(tmp_ino))) {
1479 if (!IS_ERR(tmp_ino))
1481 ntfs_error(vol->sb, "Failed to load $UsnJrnl.");
1484 vol->usnjrnl_ino = tmp_ino;
1486 * If the transaction log is in the process of being deleted, we can
1489 if (unlikely(vol->vol_flags & VOLUME_DELETE_USN_UNDERWAY)) {
1490 ntfs_debug("$UsnJrnl in the process of being disabled. "
1491 "Volume does not have transaction logging "
1495 /* Get the $DATA/$Max attribute. */
1496 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, Max, 4);
1497 if (IS_ERR(tmp_ino)) {
1498 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$Max "
1502 vol->usnjrnl_max_ino = tmp_ino;
1503 if (unlikely(i_size_read(tmp_ino) < sizeof(USN_HEADER))) {
1504 ntfs_error(vol->sb, "Found corrupt $UsnJrnl/$DATA/$Max "
1505 "attribute (size is 0x%llx but should be at "
1506 "least 0x%zx bytes).", i_size_read(tmp_ino),
1507 sizeof(USN_HEADER));
1510 /* Get the $DATA/$J attribute. */
1511 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, J, 2);
1512 if (IS_ERR(tmp_ino)) {
1513 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$J "
1517 vol->usnjrnl_j_ino = tmp_ino;
1518 /* Verify $J is non-resident and sparse. */
1519 tmp_ni = NTFS_I(vol->usnjrnl_j_ino);
1520 if (unlikely(!NInoNonResident(tmp_ni) || !NInoSparse(tmp_ni))) {
1521 ntfs_error(vol->sb, "$UsnJrnl/$DATA/$J attribute is resident "
1522 "and/or not sparse.");
1525 /* Read the USN_HEADER from $DATA/$Max. */
1526 page = ntfs_map_page(vol->usnjrnl_max_ino->i_mapping, 0);
1528 ntfs_error(vol->sb, "Failed to read from $UsnJrnl/$DATA/$Max "
1532 uh = (USN_HEADER*)page_address(page);
1533 /* Sanity check the $Max. */
1534 if (unlikely(sle64_to_cpu(uh->allocation_delta) >
1535 sle64_to_cpu(uh->maximum_size))) {
1536 ntfs_error(vol->sb, "Allocation delta (0x%llx) exceeds "
1537 "maximum size (0x%llx). $UsnJrnl is corrupt.",
1538 (long long)sle64_to_cpu(uh->allocation_delta),
1539 (long long)sle64_to_cpu(uh->maximum_size));
1540 ntfs_unmap_page(page);
1544 * If the transaction log has been stamped and nothing has been written
1545 * to it since, we do not need to stamp it.
1547 if (unlikely(sle64_to_cpu(uh->lowest_valid_usn) >=
1548 i_size_read(vol->usnjrnl_j_ino))) {
1549 if (likely(sle64_to_cpu(uh->lowest_valid_usn) ==
1550 i_size_read(vol->usnjrnl_j_ino))) {
1551 ntfs_unmap_page(page);
1552 ntfs_debug("$UsnJrnl is enabled but nothing has been "
1553 "logged since it was last stamped. "
1554 "Treating this as if the volume does "
1555 "not have transaction logging "
1559 ntfs_error(vol->sb, "$UsnJrnl has lowest valid usn (0x%llx) "
1560 "which is out of bounds (0x%llx). $UsnJrnl "
1562 (long long)sle64_to_cpu(uh->lowest_valid_usn),
1563 i_size_read(vol->usnjrnl_j_ino));
1564 ntfs_unmap_page(page);
1567 ntfs_unmap_page(page);
1568 ntfs_debug("Done.");
1573 * load_and_init_attrdef - load the attribute definitions table for a volume
1574 * @vol: ntfs super block describing device whose attrdef to load
1576 * Return 'true' on success or 'false' on error.
1578 static bool load_and_init_attrdef(ntfs_volume *vol)
1581 struct super_block *sb = vol->sb;
1584 pgoff_t index, max_index;
1587 ntfs_debug("Entering.");
1588 /* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */
1589 ino = ntfs_iget(sb, FILE_AttrDef);
1590 if (IS_ERR(ino) || is_bad_inode(ino)) {
1595 NInoSetSparseDisabled(NTFS_I(ino));
1596 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
1597 i_size = i_size_read(ino);
1598 if (i_size <= 0 || i_size > 0x7fffffff)
1600 vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(i_size);
1604 max_index = i_size >> PAGE_SHIFT;
1606 while (index < max_index) {
1607 /* Read the attrdef table and copy it into the linear buffer. */
1608 read_partial_attrdef_page:
1609 page = ntfs_map_page(ino->i_mapping, index);
1611 goto free_iput_failed;
1612 memcpy((u8*)vol->attrdef + (index++ << PAGE_SHIFT),
1613 page_address(page), size);
1614 ntfs_unmap_page(page);
1616 if (size == PAGE_SIZE) {
1617 size = i_size & ~PAGE_MASK;
1619 goto read_partial_attrdef_page;
1621 vol->attrdef_size = i_size;
1622 ntfs_debug("Read %llu bytes from $AttrDef.", i_size);
1626 ntfs_free(vol->attrdef);
1627 vol->attrdef = NULL;
1631 ntfs_error(sb, "Failed to initialize attribute definition table.");
1635 #endif /* NTFS_RW */
1638 * load_and_init_upcase - load the upcase table for an ntfs volume
1639 * @vol: ntfs super block describing device whose upcase to load
1641 * Return 'true' on success or 'false' on error.
1643 static bool load_and_init_upcase(ntfs_volume *vol)
1646 struct super_block *sb = vol->sb;
1649 pgoff_t index, max_index;
1653 ntfs_debug("Entering.");
1654 /* Read upcase table and setup vol->upcase and vol->upcase_len. */
1655 ino = ntfs_iget(sb, FILE_UpCase);
1656 if (IS_ERR(ino) || is_bad_inode(ino)) {
1662 * The upcase size must not be above 64k Unicode characters, must not
1663 * be zero and must be a multiple of sizeof(ntfschar).
1665 i_size = i_size_read(ino);
1666 if (!i_size || i_size & (sizeof(ntfschar) - 1) ||
1667 i_size > 64ULL * 1024 * sizeof(ntfschar))
1668 goto iput_upcase_failed;
1669 vol->upcase = (ntfschar*)ntfs_malloc_nofs(i_size);
1671 goto iput_upcase_failed;
1673 max_index = i_size >> PAGE_SHIFT;
1675 while (index < max_index) {
1676 /* Read the upcase table and copy it into the linear buffer. */
1677 read_partial_upcase_page:
1678 page = ntfs_map_page(ino->i_mapping, index);
1680 goto iput_upcase_failed;
1681 memcpy((char*)vol->upcase + (index++ << PAGE_SHIFT),
1682 page_address(page), size);
1683 ntfs_unmap_page(page);
1685 if (size == PAGE_SIZE) {
1686 size = i_size & ~PAGE_MASK;
1688 goto read_partial_upcase_page;
1690 vol->upcase_len = i_size >> UCHAR_T_SIZE_BITS;
1691 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
1692 i_size, 64 * 1024 * sizeof(ntfschar));
1694 mutex_lock(&ntfs_lock);
1695 if (!default_upcase) {
1696 ntfs_debug("Using volume specified $UpCase since default is "
1698 mutex_unlock(&ntfs_lock);
1701 max = default_upcase_len;
1702 if (max > vol->upcase_len)
1703 max = vol->upcase_len;
1704 for (i = 0; i < max; i++)
1705 if (vol->upcase[i] != default_upcase[i])
1708 ntfs_free(vol->upcase);
1709 vol->upcase = default_upcase;
1710 vol->upcase_len = max;
1711 ntfs_nr_upcase_users++;
1712 mutex_unlock(&ntfs_lock);
1713 ntfs_debug("Volume specified $UpCase matches default. Using "
1717 mutex_unlock(&ntfs_lock);
1718 ntfs_debug("Using volume specified $UpCase since it does not match "
1723 ntfs_free(vol->upcase);
1726 mutex_lock(&ntfs_lock);
1727 if (default_upcase) {
1728 vol->upcase = default_upcase;
1729 vol->upcase_len = default_upcase_len;
1730 ntfs_nr_upcase_users++;
1731 mutex_unlock(&ntfs_lock);
1732 ntfs_error(sb, "Failed to load $UpCase from the volume. Using "
1736 mutex_unlock(&ntfs_lock);
1737 ntfs_error(sb, "Failed to initialize upcase table.");
1742 * The lcn and mft bitmap inodes are NTFS-internal inodes with
1743 * their own special locking rules:
1745 static struct lock_class_key
1746 lcnbmp_runlist_lock_key, lcnbmp_mrec_lock_key,
1747 mftbmp_runlist_lock_key, mftbmp_mrec_lock_key;
1750 * load_system_files - open the system files using normal functions
1751 * @vol: ntfs super block describing device whose system files to load
1753 * Open the system files with normal access functions and complete setting up
1754 * the ntfs super block @vol.
1756 * Return 'true' on success or 'false' on error.
1758 static bool load_system_files(ntfs_volume *vol)
1760 struct super_block *sb = vol->sb;
1762 VOLUME_INFORMATION *vi;
1763 ntfs_attr_search_ctx *ctx;
1765 RESTART_PAGE_HEADER *rp;
1767 #endif /* NTFS_RW */
1769 ntfs_debug("Entering.");
1771 /* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */
1772 if (!load_and_init_mft_mirror(vol) || !check_mft_mirror(vol)) {
1773 static const char *es1 = "Failed to load $MFTMirr";
1774 static const char *es2 = "$MFTMirr does not match $MFT";
1775 static const char *es3 = ". Run ntfsfix and/or chkdsk.";
1777 /* If a read-write mount, convert it to a read-only mount. */
1778 if (!sb_rdonly(sb)) {
1779 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1780 ON_ERRORS_CONTINUE))) {
1781 ntfs_error(sb, "%s and neither on_errors="
1782 "continue nor on_errors="
1783 "remount-ro was specified%s",
1784 !vol->mftmirr_ino ? es1 : es2,
1786 goto iput_mirr_err_out;
1788 sb->s_flags |= SB_RDONLY;
1789 ntfs_error(sb, "%s. Mounting read-only%s",
1790 !vol->mftmirr_ino ? es1 : es2, es3);
1792 ntfs_warning(sb, "%s. Will not be able to remount "
1794 !vol->mftmirr_ino ? es1 : es2, es3);
1795 /* This will prevent a read-write remount. */
1798 #endif /* NTFS_RW */
1799 /* Get mft bitmap attribute inode. */
1800 vol->mftbmp_ino = ntfs_attr_iget(vol->mft_ino, AT_BITMAP, NULL, 0);
1801 if (IS_ERR(vol->mftbmp_ino)) {
1802 ntfs_error(sb, "Failed to load $MFT/$BITMAP attribute.");
1803 goto iput_mirr_err_out;
1805 lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->runlist.lock,
1806 &mftbmp_runlist_lock_key);
1807 lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->mrec_lock,
1808 &mftbmp_mrec_lock_key);
1809 /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */
1810 if (!load_and_init_upcase(vol))
1811 goto iput_mftbmp_err_out;
1814 * Read attribute definitions table and setup @vol->attrdef and
1815 * @vol->attrdef_size.
1817 if (!load_and_init_attrdef(vol))
1818 goto iput_upcase_err_out;
1819 #endif /* NTFS_RW */
1821 * Get the cluster allocation bitmap inode and verify the size, no
1822 * need for any locking at this stage as we are already running
1823 * exclusively as we are mount in progress task.
1825 vol->lcnbmp_ino = ntfs_iget(sb, FILE_Bitmap);
1826 if (IS_ERR(vol->lcnbmp_ino) || is_bad_inode(vol->lcnbmp_ino)) {
1827 if (!IS_ERR(vol->lcnbmp_ino))
1828 iput(vol->lcnbmp_ino);
1831 lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->runlist.lock,
1832 &lcnbmp_runlist_lock_key);
1833 lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->mrec_lock,
1834 &lcnbmp_mrec_lock_key);
1836 NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino));
1837 if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) {
1838 iput(vol->lcnbmp_ino);
1840 ntfs_error(sb, "Failed to load $Bitmap.");
1841 goto iput_attrdef_err_out;
1844 * Get the volume inode and setup our cache of the volume flags and
1847 vol->vol_ino = ntfs_iget(sb, FILE_Volume);
1848 if (IS_ERR(vol->vol_ino) || is_bad_inode(vol->vol_ino)) {
1849 if (!IS_ERR(vol->vol_ino))
1852 ntfs_error(sb, "Failed to load $Volume.");
1853 goto iput_lcnbmp_err_out;
1855 m = map_mft_record(NTFS_I(vol->vol_ino));
1861 if (!(ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m))) {
1862 ntfs_error(sb, "Failed to get attribute search context.");
1863 goto get_ctx_vol_failed;
1865 if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
1866 ctx) || ctx->attr->non_resident || ctx->attr->flags) {
1868 ntfs_attr_put_search_ctx(ctx);
1870 unmap_mft_record(NTFS_I(vol->vol_ino));
1871 goto iput_volume_failed;
1873 vi = (VOLUME_INFORMATION*)((char*)ctx->attr +
1874 le16_to_cpu(ctx->attr->data.resident.value_offset));
1875 /* Some bounds checks. */
1876 if ((u8*)vi < (u8*)ctx->attr || (u8*)vi +
1877 le32_to_cpu(ctx->attr->data.resident.value_length) >
1878 (u8*)ctx->attr + le32_to_cpu(ctx->attr->length))
1880 /* Copy the volume flags and version to the ntfs_volume structure. */
1881 vol->vol_flags = vi->flags;
1882 vol->major_ver = vi->major_ver;
1883 vol->minor_ver = vi->minor_ver;
1884 ntfs_attr_put_search_ctx(ctx);
1885 unmap_mft_record(NTFS_I(vol->vol_ino));
1886 pr_info("volume version %i.%i.\n", vol->major_ver,
1888 if (vol->major_ver < 3 && NVolSparseEnabled(vol)) {
1889 ntfs_warning(vol->sb, "Disabling sparse support due to NTFS "
1890 "volume version %i.%i (need at least version "
1891 "3.0).", vol->major_ver, vol->minor_ver);
1892 NVolClearSparseEnabled(vol);
1895 /* Make sure that no unsupported volume flags are set. */
1896 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
1897 static const char *es1a = "Volume is dirty";
1898 static const char *es1b = "Volume has been modified by chkdsk";
1899 static const char *es1c = "Volume has unsupported flags set";
1900 static const char *es2a = ". Run chkdsk and mount in Windows.";
1901 static const char *es2b = ". Mount in Windows.";
1902 const char *es1, *es2;
1905 if (vol->vol_flags & VOLUME_IS_DIRTY)
1907 else if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
1912 ntfs_warning(sb, "Unsupported volume flags 0x%x "
1914 (unsigned)le16_to_cpu(vol->vol_flags));
1916 /* If a read-write mount, convert it to a read-only mount. */
1917 if (!sb_rdonly(sb)) {
1918 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1919 ON_ERRORS_CONTINUE))) {
1920 ntfs_error(sb, "%s and neither on_errors="
1921 "continue nor on_errors="
1922 "remount-ro was specified%s",
1924 goto iput_vol_err_out;
1926 sb->s_flags |= SB_RDONLY;
1927 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1929 ntfs_warning(sb, "%s. Will not be able to remount "
1930 "read-write%s", es1, es2);
1932 * Do not set NVolErrors() because ntfs_remount() re-checks the
1933 * flags which we need to do in case any flags have changed.
1937 * Get the inode for the logfile, check it and determine if the volume
1938 * was shutdown cleanly.
1941 if (!load_and_check_logfile(vol, &rp) ||
1942 !ntfs_is_logfile_clean(vol->logfile_ino, rp)) {
1943 static const char *es1a = "Failed to load $LogFile";
1944 static const char *es1b = "$LogFile is not clean";
1945 static const char *es2 = ". Mount in Windows.";
1948 es1 = !vol->logfile_ino ? es1a : es1b;
1949 /* If a read-write mount, convert it to a read-only mount. */
1950 if (!sb_rdonly(sb)) {
1951 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1952 ON_ERRORS_CONTINUE))) {
1953 ntfs_error(sb, "%s and neither on_errors="
1954 "continue nor on_errors="
1955 "remount-ro was specified%s",
1957 if (vol->logfile_ino) {
1961 goto iput_logfile_err_out;
1963 sb->s_flags |= SB_RDONLY;
1964 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1966 ntfs_warning(sb, "%s. Will not be able to remount "
1967 "read-write%s", es1, es2);
1968 /* This will prevent a read-write remount. */
1972 #endif /* NTFS_RW */
1973 /* Get the root directory inode so we can do path lookups. */
1974 vol->root_ino = ntfs_iget(sb, FILE_root);
1975 if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) {
1976 if (!IS_ERR(vol->root_ino))
1977 iput(vol->root_ino);
1978 ntfs_error(sb, "Failed to load root directory.");
1979 goto iput_logfile_err_out;
1983 * Check if Windows is suspended to disk on the target volume. If it
1984 * is hibernated, we must not write *anything* to the disk so set
1985 * NVolErrors() without setting the dirty volume flag and mount
1986 * read-only. This will prevent read-write remounting and it will also
1987 * prevent all writes.
1989 err = check_windows_hibernation_status(vol);
1990 if (unlikely(err)) {
1991 static const char *es1a = "Failed to determine if Windows is "
1993 static const char *es1b = "Windows is hibernated";
1994 static const char *es2 = ". Run chkdsk.";
1997 es1 = err < 0 ? es1a : es1b;
1998 /* If a read-write mount, convert it to a read-only mount. */
1999 if (!sb_rdonly(sb)) {
2000 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2001 ON_ERRORS_CONTINUE))) {
2002 ntfs_error(sb, "%s and neither on_errors="
2003 "continue nor on_errors="
2004 "remount-ro was specified%s",
2006 goto iput_root_err_out;
2008 sb->s_flags |= SB_RDONLY;
2009 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2011 ntfs_warning(sb, "%s. Will not be able to remount "
2012 "read-write%s", es1, es2);
2013 /* This will prevent a read-write remount. */
2016 /* If (still) a read-write mount, mark the volume dirty. */
2017 if (!sb_rdonly(sb) && ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
2018 static const char *es1 = "Failed to set dirty bit in volume "
2019 "information flags";
2020 static const char *es2 = ". Run chkdsk.";
2022 /* Convert to a read-only mount. */
2023 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2024 ON_ERRORS_CONTINUE))) {
2025 ntfs_error(sb, "%s and neither on_errors=continue nor "
2026 "on_errors=remount-ro was specified%s",
2028 goto iput_root_err_out;
2030 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2031 sb->s_flags |= SB_RDONLY;
2033 * Do not set NVolErrors() because ntfs_remount() might manage
2034 * to set the dirty flag in which case all would be well.
2038 // TODO: Enable this code once we start modifying anything that is
2039 // different between NTFS 1.2 and 3.x...
2041 * If (still) a read-write mount, set the NT4 compatibility flag on
2042 * newer NTFS version volumes.
2044 if (!(sb->s_flags & SB_RDONLY) && (vol->major_ver > 1) &&
2045 ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
2046 static const char *es1 = "Failed to set NT4 compatibility flag";
2047 static const char *es2 = ". Run chkdsk.";
2049 /* Convert to a read-only mount. */
2050 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2051 ON_ERRORS_CONTINUE))) {
2052 ntfs_error(sb, "%s and neither on_errors=continue nor "
2053 "on_errors=remount-ro was specified%s",
2055 goto iput_root_err_out;
2057 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2058 sb->s_flags |= SB_RDONLY;
2062 /* If (still) a read-write mount, empty the logfile. */
2063 if (!sb_rdonly(sb) && !ntfs_empty_logfile(vol->logfile_ino)) {
2064 static const char *es1 = "Failed to empty $LogFile";
2065 static const char *es2 = ". Mount in Windows.";
2067 /* Convert to a read-only mount. */
2068 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2069 ON_ERRORS_CONTINUE))) {
2070 ntfs_error(sb, "%s and neither on_errors=continue nor "
2071 "on_errors=remount-ro was specified%s",
2073 goto iput_root_err_out;
2075 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2076 sb->s_flags |= SB_RDONLY;
2079 #endif /* NTFS_RW */
2080 /* If on NTFS versions before 3.0, we are done. */
2081 if (unlikely(vol->major_ver < 3))
2083 /* NTFS 3.0+ specific initialization. */
2084 /* Get the security descriptors inode. */
2085 vol->secure_ino = ntfs_iget(sb, FILE_Secure);
2086 if (IS_ERR(vol->secure_ino) || is_bad_inode(vol->secure_ino)) {
2087 if (!IS_ERR(vol->secure_ino))
2088 iput(vol->secure_ino);
2089 ntfs_error(sb, "Failed to load $Secure.");
2090 goto iput_root_err_out;
2092 // TODO: Initialize security.
2093 /* Get the extended system files' directory inode. */
2094 vol->extend_ino = ntfs_iget(sb, FILE_Extend);
2095 if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) {
2096 if (!IS_ERR(vol->extend_ino))
2097 iput(vol->extend_ino);
2098 ntfs_error(sb, "Failed to load $Extend.");
2099 goto iput_sec_err_out;
2102 /* Find the quota file, load it if present, and set it up. */
2103 if (!load_and_init_quota(vol)) {
2104 static const char *es1 = "Failed to load $Quota";
2105 static const char *es2 = ". Run chkdsk.";
2107 /* If a read-write mount, convert it to a read-only mount. */
2108 if (!sb_rdonly(sb)) {
2109 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2110 ON_ERRORS_CONTINUE))) {
2111 ntfs_error(sb, "%s and neither on_errors="
2112 "continue nor on_errors="
2113 "remount-ro was specified%s",
2115 goto iput_quota_err_out;
2117 sb->s_flags |= SB_RDONLY;
2118 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2120 ntfs_warning(sb, "%s. Will not be able to remount "
2121 "read-write%s", es1, es2);
2122 /* This will prevent a read-write remount. */
2125 /* If (still) a read-write mount, mark the quotas out of date. */
2126 if (!sb_rdonly(sb) && !ntfs_mark_quotas_out_of_date(vol)) {
2127 static const char *es1 = "Failed to mark quotas out of date";
2128 static const char *es2 = ". Run chkdsk.";
2130 /* Convert to a read-only mount. */
2131 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2132 ON_ERRORS_CONTINUE))) {
2133 ntfs_error(sb, "%s and neither on_errors=continue nor "
2134 "on_errors=remount-ro was specified%s",
2136 goto iput_quota_err_out;
2138 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2139 sb->s_flags |= SB_RDONLY;
2143 * Find the transaction log file ($UsnJrnl), load it if present, check
2144 * it, and set it up.
2146 if (!load_and_init_usnjrnl(vol)) {
2147 static const char *es1 = "Failed to load $UsnJrnl";
2148 static const char *es2 = ". Run chkdsk.";
2150 /* If a read-write mount, convert it to a read-only mount. */
2151 if (!sb_rdonly(sb)) {
2152 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2153 ON_ERRORS_CONTINUE))) {
2154 ntfs_error(sb, "%s and neither on_errors="
2155 "continue nor on_errors="
2156 "remount-ro was specified%s",
2158 goto iput_usnjrnl_err_out;
2160 sb->s_flags |= SB_RDONLY;
2161 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2163 ntfs_warning(sb, "%s. Will not be able to remount "
2164 "read-write%s", es1, es2);
2165 /* This will prevent a read-write remount. */
2168 /* If (still) a read-write mount, stamp the transaction log. */
2169 if (!sb_rdonly(sb) && !ntfs_stamp_usnjrnl(vol)) {
2170 static const char *es1 = "Failed to stamp transaction log "
2172 static const char *es2 = ". Run chkdsk.";
2174 /* Convert to a read-only mount. */
2175 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2176 ON_ERRORS_CONTINUE))) {
2177 ntfs_error(sb, "%s and neither on_errors=continue nor "
2178 "on_errors=remount-ro was specified%s",
2180 goto iput_usnjrnl_err_out;
2182 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2183 sb->s_flags |= SB_RDONLY;
2186 #endif /* NTFS_RW */
2189 iput_usnjrnl_err_out:
2190 iput(vol->usnjrnl_j_ino);
2191 iput(vol->usnjrnl_max_ino);
2192 iput(vol->usnjrnl_ino);
2194 iput(vol->quota_q_ino);
2195 iput(vol->quota_ino);
2196 iput(vol->extend_ino);
2197 #endif /* NTFS_RW */
2199 iput(vol->secure_ino);
2201 iput(vol->root_ino);
2202 iput_logfile_err_out:
2204 iput(vol->logfile_ino);
2206 #endif /* NTFS_RW */
2208 iput_lcnbmp_err_out:
2209 iput(vol->lcnbmp_ino);
2210 iput_attrdef_err_out:
2211 vol->attrdef_size = 0;
2213 ntfs_free(vol->attrdef);
2214 vol->attrdef = NULL;
2217 iput_upcase_err_out:
2218 #endif /* NTFS_RW */
2219 vol->upcase_len = 0;
2220 mutex_lock(&ntfs_lock);
2221 if (vol->upcase == default_upcase) {
2222 ntfs_nr_upcase_users--;
2225 mutex_unlock(&ntfs_lock);
2227 ntfs_free(vol->upcase);
2230 iput_mftbmp_err_out:
2231 iput(vol->mftbmp_ino);
2234 iput(vol->mftmirr_ino);
2235 #endif /* NTFS_RW */
2240 * ntfs_put_super - called by the vfs to unmount a volume
2241 * @sb: vfs superblock of volume to unmount
2243 * ntfs_put_super() is called by the VFS (from fs/super.c::do_umount()) when
2244 * the volume is being unmounted (umount system call has been invoked) and it
2245 * releases all inodes and memory belonging to the NTFS specific part of the
2248 static void ntfs_put_super(struct super_block *sb)
2250 ntfs_volume *vol = NTFS_SB(sb);
2252 ntfs_debug("Entering.");
2256 * Commit all inodes while they are still open in case some of them
2257 * cause others to be dirtied.
2259 ntfs_commit_inode(vol->vol_ino);
2261 /* NTFS 3.0+ specific. */
2262 if (vol->major_ver >= 3) {
2263 if (vol->usnjrnl_j_ino)
2264 ntfs_commit_inode(vol->usnjrnl_j_ino);
2265 if (vol->usnjrnl_max_ino)
2266 ntfs_commit_inode(vol->usnjrnl_max_ino);
2267 if (vol->usnjrnl_ino)
2268 ntfs_commit_inode(vol->usnjrnl_ino);
2269 if (vol->quota_q_ino)
2270 ntfs_commit_inode(vol->quota_q_ino);
2272 ntfs_commit_inode(vol->quota_ino);
2273 if (vol->extend_ino)
2274 ntfs_commit_inode(vol->extend_ino);
2275 if (vol->secure_ino)
2276 ntfs_commit_inode(vol->secure_ino);
2279 ntfs_commit_inode(vol->root_ino);
2281 down_write(&vol->lcnbmp_lock);
2282 ntfs_commit_inode(vol->lcnbmp_ino);
2283 up_write(&vol->lcnbmp_lock);
2285 down_write(&vol->mftbmp_lock);
2286 ntfs_commit_inode(vol->mftbmp_ino);
2287 up_write(&vol->mftbmp_lock);
2289 if (vol->logfile_ino)
2290 ntfs_commit_inode(vol->logfile_ino);
2292 if (vol->mftmirr_ino)
2293 ntfs_commit_inode(vol->mftmirr_ino);
2294 ntfs_commit_inode(vol->mft_ino);
2297 * If a read-write mount and no volume errors have occurred, mark the
2298 * volume clean. Also, re-commit all affected inodes.
2300 if (!sb_rdonly(sb)) {
2301 if (!NVolErrors(vol)) {
2302 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
2303 ntfs_warning(sb, "Failed to clear dirty bit "
2304 "in volume information "
2305 "flags. Run chkdsk.");
2306 ntfs_commit_inode(vol->vol_ino);
2307 ntfs_commit_inode(vol->root_ino);
2308 if (vol->mftmirr_ino)
2309 ntfs_commit_inode(vol->mftmirr_ino);
2310 ntfs_commit_inode(vol->mft_ino);
2312 ntfs_warning(sb, "Volume has errors. Leaving volume "
2313 "marked dirty. Run chkdsk.");
2316 #endif /* NTFS_RW */
2319 vol->vol_ino = NULL;
2321 /* NTFS 3.0+ specific clean up. */
2322 if (vol->major_ver >= 3) {
2324 if (vol->usnjrnl_j_ino) {
2325 iput(vol->usnjrnl_j_ino);
2326 vol->usnjrnl_j_ino = NULL;
2328 if (vol->usnjrnl_max_ino) {
2329 iput(vol->usnjrnl_max_ino);
2330 vol->usnjrnl_max_ino = NULL;
2332 if (vol->usnjrnl_ino) {
2333 iput(vol->usnjrnl_ino);
2334 vol->usnjrnl_ino = NULL;
2336 if (vol->quota_q_ino) {
2337 iput(vol->quota_q_ino);
2338 vol->quota_q_ino = NULL;
2340 if (vol->quota_ino) {
2341 iput(vol->quota_ino);
2342 vol->quota_ino = NULL;
2344 #endif /* NTFS_RW */
2345 if (vol->extend_ino) {
2346 iput(vol->extend_ino);
2347 vol->extend_ino = NULL;
2349 if (vol->secure_ino) {
2350 iput(vol->secure_ino);
2351 vol->secure_ino = NULL;
2355 iput(vol->root_ino);
2356 vol->root_ino = NULL;
2358 down_write(&vol->lcnbmp_lock);
2359 iput(vol->lcnbmp_ino);
2360 vol->lcnbmp_ino = NULL;
2361 up_write(&vol->lcnbmp_lock);
2363 down_write(&vol->mftbmp_lock);
2364 iput(vol->mftbmp_ino);
2365 vol->mftbmp_ino = NULL;
2366 up_write(&vol->mftbmp_lock);
2369 if (vol->logfile_ino) {
2370 iput(vol->logfile_ino);
2371 vol->logfile_ino = NULL;
2373 if (vol->mftmirr_ino) {
2374 /* Re-commit the mft mirror and mft just in case. */
2375 ntfs_commit_inode(vol->mftmirr_ino);
2376 ntfs_commit_inode(vol->mft_ino);
2377 iput(vol->mftmirr_ino);
2378 vol->mftmirr_ino = NULL;
2381 * We should have no dirty inodes left, due to
2382 * mft.c::ntfs_mft_writepage() cleaning all the dirty pages as
2383 * the underlying mft records are written out and cleaned.
2385 ntfs_commit_inode(vol->mft_ino);
2386 write_inode_now(vol->mft_ino, 1);
2387 #endif /* NTFS_RW */
2390 vol->mft_ino = NULL;
2392 /* Throw away the table of attribute definitions. */
2393 vol->attrdef_size = 0;
2395 ntfs_free(vol->attrdef);
2396 vol->attrdef = NULL;
2398 vol->upcase_len = 0;
2400 * Destroy the global default upcase table if necessary. Also decrease
2401 * the number of upcase users if we are a user.
2403 mutex_lock(&ntfs_lock);
2404 if (vol->upcase == default_upcase) {
2405 ntfs_nr_upcase_users--;
2408 if (!ntfs_nr_upcase_users && default_upcase) {
2409 ntfs_free(default_upcase);
2410 default_upcase = NULL;
2412 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
2413 free_compression_buffers();
2414 mutex_unlock(&ntfs_lock);
2416 ntfs_free(vol->upcase);
2420 unload_nls(vol->nls_map);
2422 sb->s_fs_info = NULL;
2427 * get_nr_free_clusters - return the number of free clusters on a volume
2428 * @vol: ntfs volume for which to obtain free cluster count
2430 * Calculate the number of free clusters on the mounted NTFS volume @vol. We
2431 * actually calculate the number of clusters in use instead because this
2432 * allows us to not care about partial pages as these will be just zero filled
2433 * and hence not be counted as allocated clusters.
2435 * The only particularity is that clusters beyond the end of the logical ntfs
2436 * volume will be marked as allocated to prevent errors which means we have to
2437 * discount those at the end. This is important as the cluster bitmap always
2438 * has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside
2439 * the logical volume and marked in use when they are not as they do not exist.
2441 * If any pages cannot be read we assume all clusters in the erroring pages are
2442 * in use. This means we return an underestimate on errors which is better than
2445 static s64 get_nr_free_clusters(ntfs_volume *vol)
2447 s64 nr_free = vol->nr_clusters;
2448 struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
2450 pgoff_t index, max_index;
2452 ntfs_debug("Entering.");
2453 /* Serialize accesses to the cluster bitmap. */
2454 down_read(&vol->lcnbmp_lock);
2456 * Convert the number of bits into bytes rounded up, then convert into
2457 * multiples of PAGE_SIZE, rounding up so that if we have one
2458 * full and one partial page max_index = 2.
2460 max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_SIZE - 1) >>
2462 /* Use multiples of 4 bytes, thus max_size is PAGE_SIZE / 4. */
2463 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.",
2464 max_index, PAGE_SIZE / 4);
2465 for (index = 0; index < max_index; index++) {
2466 unsigned long *kaddr;
2469 * Read the page from page cache, getting it from backing store
2470 * if necessary, and increment the use count.
2472 page = read_mapping_page(mapping, index, NULL);
2473 /* Ignore pages which errored synchronously. */
2475 ntfs_debug("read_mapping_page() error. Skipping "
2476 "page (index 0x%lx).", index);
2477 nr_free -= PAGE_SIZE * 8;
2480 kaddr = kmap_atomic(page);
2482 * Subtract the number of set bits. If this
2483 * is the last page and it is partial we don't really care as
2484 * it just means we do a little extra work but it won't affect
2485 * the result as all out of range bytes are set to zero by
2488 nr_free -= bitmap_weight(kaddr,
2489 PAGE_SIZE * BITS_PER_BYTE);
2490 kunmap_atomic(kaddr);
2493 ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index - 1);
2495 * Fixup for eventual bits outside logical ntfs volume (see function
2496 * description above).
2498 if (vol->nr_clusters & 63)
2499 nr_free += 64 - (vol->nr_clusters & 63);
2500 up_read(&vol->lcnbmp_lock);
2501 /* If errors occurred we may well have gone below zero, fix this. */
2504 ntfs_debug("Exiting.");
2509 * __get_nr_free_mft_records - return the number of free inodes on a volume
2510 * @vol: ntfs volume for which to obtain free inode count
2511 * @nr_free: number of mft records in filesystem
2512 * @max_index: maximum number of pages containing set bits
2514 * Calculate the number of free mft records (inodes) on the mounted NTFS
2515 * volume @vol. We actually calculate the number of mft records in use instead
2516 * because this allows us to not care about partial pages as these will be just
2517 * zero filled and hence not be counted as allocated mft record.
2519 * If any pages cannot be read we assume all mft records in the erroring pages
2520 * are in use. This means we return an underestimate on errors which is better
2521 * than an overestimate.
2523 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
2525 static unsigned long __get_nr_free_mft_records(ntfs_volume *vol,
2526 s64 nr_free, const pgoff_t max_index)
2528 struct address_space *mapping = vol->mftbmp_ino->i_mapping;
2532 ntfs_debug("Entering.");
2533 /* Use multiples of 4 bytes, thus max_size is PAGE_SIZE / 4. */
2534 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
2535 "0x%lx.", max_index, PAGE_SIZE / 4);
2536 for (index = 0; index < max_index; index++) {
2537 unsigned long *kaddr;
2540 * Read the page from page cache, getting it from backing store
2541 * if necessary, and increment the use count.
2543 page = read_mapping_page(mapping, index, NULL);
2544 /* Ignore pages which errored synchronously. */
2546 ntfs_debug("read_mapping_page() error. Skipping "
2547 "page (index 0x%lx).", index);
2548 nr_free -= PAGE_SIZE * 8;
2551 kaddr = kmap_atomic(page);
2553 * Subtract the number of set bits. If this
2554 * is the last page and it is partial we don't really care as
2555 * it just means we do a little extra work but it won't affect
2556 * the result as all out of range bytes are set to zero by
2559 nr_free -= bitmap_weight(kaddr,
2560 PAGE_SIZE * BITS_PER_BYTE);
2561 kunmap_atomic(kaddr);
2564 ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.",
2566 /* If errors occurred we may well have gone below zero, fix this. */
2569 ntfs_debug("Exiting.");
2574 * ntfs_statfs - return information about mounted NTFS volume
2575 * @dentry: dentry from mounted volume
2576 * @sfs: statfs structure in which to return the information
2578 * Return information about the mounted NTFS volume @dentry in the statfs structure
2579 * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is
2580 * called). We interpret the values to be correct of the moment in time at
2581 * which we are called. Most values are variable otherwise and this isn't just
2582 * the free values but the totals as well. For example we can increase the
2583 * total number of file nodes if we run out and we can keep doing this until
2584 * there is no more space on the volume left at all.
2586 * Called from vfs_statfs which is used to handle the statfs, fstatfs, and
2587 * ustat system calls.
2589 * Return 0 on success or -errno on error.
2591 static int ntfs_statfs(struct dentry *dentry, struct kstatfs *sfs)
2593 struct super_block *sb = dentry->d_sb;
2595 ntfs_volume *vol = NTFS_SB(sb);
2596 ntfs_inode *mft_ni = NTFS_I(vol->mft_ino);
2598 unsigned long flags;
2600 ntfs_debug("Entering.");
2601 /* Type of filesystem. */
2602 sfs->f_type = NTFS_SB_MAGIC;
2603 /* Optimal transfer block size. */
2604 sfs->f_bsize = PAGE_SIZE;
2606 * Total data blocks in filesystem in units of f_bsize and since
2607 * inodes are also stored in data blocs ($MFT is a file) this is just
2608 * the total clusters.
2610 sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >>
2612 /* Free data blocks in filesystem in units of f_bsize. */
2613 size = get_nr_free_clusters(vol) << vol->cluster_size_bits >>
2617 /* Free blocks avail to non-superuser, same as above on NTFS. */
2618 sfs->f_bavail = sfs->f_bfree = size;
2619 /* Serialize accesses to the inode bitmap. */
2620 down_read(&vol->mftbmp_lock);
2621 read_lock_irqsave(&mft_ni->size_lock, flags);
2622 size = i_size_read(vol->mft_ino) >> vol->mft_record_size_bits;
2624 * Convert the maximum number of set bits into bytes rounded up, then
2625 * convert into multiples of PAGE_SIZE, rounding up so that if we
2626 * have one full and one partial page max_index = 2.
2628 max_index = ((((mft_ni->initialized_size >> vol->mft_record_size_bits)
2629 + 7) >> 3) + PAGE_SIZE - 1) >> PAGE_SHIFT;
2630 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2631 /* Number of inodes in filesystem (at this point in time). */
2632 sfs->f_files = size;
2633 /* Free inodes in fs (based on current total count). */
2634 sfs->f_ffree = __get_nr_free_mft_records(vol, size, max_index);
2635 up_read(&vol->mftbmp_lock);
2637 * File system id. This is extremely *nix flavour dependent and even
2638 * within Linux itself all fs do their own thing. I interpret this to
2639 * mean a unique id associated with the mounted fs and not the id
2640 * associated with the filesystem driver, the latter is already given
2641 * by the filesystem type in sfs->f_type. Thus we use the 64-bit
2642 * volume serial number splitting it into two 32-bit parts. We enter
2643 * the least significant 32-bits in f_fsid[0] and the most significant
2644 * 32-bits in f_fsid[1].
2646 sfs->f_fsid = u64_to_fsid(vol->serial_no);
2647 /* Maximum length of filenames. */
2648 sfs->f_namelen = NTFS_MAX_NAME_LEN;
2653 static int ntfs_write_inode(struct inode *vi, struct writeback_control *wbc)
2655 return __ntfs_write_inode(vi, wbc->sync_mode == WB_SYNC_ALL);
2660 * The complete super operations.
2662 static const struct super_operations ntfs_sops = {
2663 .alloc_inode = ntfs_alloc_big_inode, /* VFS: Allocate new inode. */
2664 .free_inode = ntfs_free_big_inode, /* VFS: Deallocate inode. */
2666 .write_inode = ntfs_write_inode, /* VFS: Write dirty inode to
2668 #endif /* NTFS_RW */
2669 .put_super = ntfs_put_super, /* Syscall: umount. */
2670 .statfs = ntfs_statfs, /* Syscall: statfs */
2671 .remount_fs = ntfs_remount, /* Syscall: mount -o remount. */
2672 .evict_inode = ntfs_evict_big_inode, /* VFS: Called when an inode is
2673 removed from memory. */
2674 .show_options = ntfs_show_options, /* Show mount options in
2679 * ntfs_fill_super - mount an ntfs filesystem
2680 * @sb: super block of ntfs filesystem to mount
2681 * @opt: string containing the mount options
2682 * @silent: silence error output
2684 * ntfs_fill_super() is called by the VFS to mount the device described by @sb
2685 * with the mount otions in @data with the NTFS filesystem.
2687 * If @silent is true, remain silent even if errors are detected. This is used
2688 * during bootup, when the kernel tries to mount the root filesystem with all
2689 * registered filesystems one after the other until one succeeds. This implies
2690 * that all filesystems except the correct one will quite correctly and
2691 * expectedly return an error, but nobody wants to see error messages when in
2692 * fact this is what is supposed to happen.
2694 * NOTE: @sb->s_flags contains the mount options flags.
2696 static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2699 struct buffer_head *bh;
2700 struct inode *tmp_ino;
2701 int blocksize, result;
2704 * We do a pretty difficult piece of bootstrap by reading the
2705 * MFT (and other metadata) from disk into memory. We'll only
2706 * release this metadata during umount, so the locking patterns
2707 * observed during bootstrap do not count. So turn off the
2708 * observation of locking patterns (strictly for this context
2709 * only) while mounting NTFS. [The validator is still active
2710 * otherwise, even for this context: it will for example record
2711 * lock class registrations.]
2714 ntfs_debug("Entering.");
2716 sb->s_flags |= SB_RDONLY;
2717 #endif /* ! NTFS_RW */
2718 /* Allocate a new ntfs_volume and place it in sb->s_fs_info. */
2719 sb->s_fs_info = kmalloc(sizeof(ntfs_volume), GFP_NOFS);
2723 ntfs_error(sb, "Allocation of NTFS volume structure "
2724 "failed. Aborting mount...");
2728 /* Initialize ntfs_volume structure. */
2729 *vol = (ntfs_volume) {
2732 * Default is group and other don't have any access to files or
2733 * directories while owner has full access. Further, files by
2734 * default are not executable but directories are of course
2740 init_rwsem(&vol->mftbmp_lock);
2741 init_rwsem(&vol->lcnbmp_lock);
2743 /* By default, enable sparse support. */
2744 NVolSetSparseEnabled(vol);
2746 /* Important to get the mount options dealt with now. */
2747 if (!parse_options(vol, (char*)opt))
2750 /* We support sector sizes up to the PAGE_SIZE. */
2751 if (bdev_logical_block_size(sb->s_bdev) > PAGE_SIZE) {
2753 ntfs_error(sb, "Device has unsupported sector size "
2754 "(%i). The maximum supported sector "
2755 "size on this architecture is %lu "
2757 bdev_logical_block_size(sb->s_bdev),
2762 * Setup the device access block size to NTFS_BLOCK_SIZE or the hard
2763 * sector size, whichever is bigger.
2765 blocksize = sb_min_blocksize(sb, NTFS_BLOCK_SIZE);
2766 if (blocksize < NTFS_BLOCK_SIZE) {
2768 ntfs_error(sb, "Unable to set device block size.");
2771 BUG_ON(blocksize != sb->s_blocksize);
2772 ntfs_debug("Set device block size to %i bytes (block size bits %i).",
2773 blocksize, sb->s_blocksize_bits);
2774 /* Determine the size of the device in units of block_size bytes. */
2775 if (!i_size_read(sb->s_bdev->bd_inode)) {
2777 ntfs_error(sb, "Unable to determine device size.");
2780 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2781 sb->s_blocksize_bits;
2782 /* Read the boot sector and return unlocked buffer head to it. */
2783 if (!(bh = read_ntfs_boot_sector(sb, silent))) {
2785 ntfs_error(sb, "Not an NTFS volume.");
2789 * Extract the data from the boot sector and setup the ntfs volume
2792 result = parse_ntfs_boot_sector(vol, (NTFS_BOOT_SECTOR*)bh->b_data);
2796 ntfs_error(sb, "Unsupported NTFS filesystem.");
2800 * If the boot sector indicates a sector size bigger than the current
2801 * device block size, switch the device block size to the sector size.
2802 * TODO: It may be possible to support this case even when the set
2803 * below fails, we would just be breaking up the i/o for each sector
2804 * into multiple blocks for i/o purposes but otherwise it should just
2805 * work. However it is safer to leave disabled until someone hits this
2806 * error message and then we can get them to try it without the setting
2807 * so we know for sure that it works.
2809 if (vol->sector_size > blocksize) {
2810 blocksize = sb_set_blocksize(sb, vol->sector_size);
2811 if (blocksize != vol->sector_size) {
2813 ntfs_error(sb, "Unable to set device block "
2814 "size to sector size (%i).",
2818 BUG_ON(blocksize != sb->s_blocksize);
2819 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2820 sb->s_blocksize_bits;
2821 ntfs_debug("Changed device block size to %i bytes (block size "
2822 "bits %i) to match volume sector size.",
2823 blocksize, sb->s_blocksize_bits);
2825 /* Initialize the cluster and mft allocators. */
2826 ntfs_setup_allocators(vol);
2827 /* Setup remaining fields in the super block. */
2828 sb->s_magic = NTFS_SB_MAGIC;
2830 * Ntfs allows 63 bits for the file size, i.e. correct would be:
2831 * sb->s_maxbytes = ~0ULL >> 1;
2832 * But the kernel uses a long as the page cache page index which on
2833 * 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel
2834 * defined to the maximum the page cache page index can cope with
2835 * without overflowing the index or to 2^63 - 1, whichever is smaller.
2837 sb->s_maxbytes = MAX_LFS_FILESIZE;
2838 /* Ntfs measures time in 100ns intervals. */
2839 sb->s_time_gran = 100;
2841 * Now load the metadata required for the page cache and our address
2842 * space operations to function. We do this by setting up a specialised
2843 * read_inode method and then just calling the normal iget() to obtain
2844 * the inode for $MFT which is sufficient to allow our normal inode
2845 * operations and associated address space operations to function.
2847 sb->s_op = &ntfs_sops;
2848 tmp_ino = new_inode(sb);
2851 ntfs_error(sb, "Failed to load essential metadata.");
2854 tmp_ino->i_ino = FILE_MFT;
2855 insert_inode_hash(tmp_ino);
2856 if (ntfs_read_inode_mount(tmp_ino) < 0) {
2858 ntfs_error(sb, "Failed to load essential metadata.");
2859 goto iput_tmp_ino_err_out_now;
2861 mutex_lock(&ntfs_lock);
2863 * The current mount is a compression user if the cluster size is
2864 * less than or equal 4kiB.
2866 if (vol->cluster_size <= 4096 && !ntfs_nr_compression_users++) {
2867 result = allocate_compression_buffers();
2869 ntfs_error(NULL, "Failed to allocate buffers "
2870 "for compression engine.");
2871 ntfs_nr_compression_users--;
2872 mutex_unlock(&ntfs_lock);
2873 goto iput_tmp_ino_err_out_now;
2877 * Generate the global default upcase table if necessary. Also
2878 * temporarily increment the number of upcase users to avoid race
2879 * conditions with concurrent (u)mounts.
2881 if (!default_upcase)
2882 default_upcase = generate_default_upcase();
2883 ntfs_nr_upcase_users++;
2884 mutex_unlock(&ntfs_lock);
2886 * From now on, ignore @silent parameter. If we fail below this line,
2887 * it will be due to a corrupt fs or a system error, so we report it.
2890 * Open the system files with normal access functions and complete
2891 * setting up the ntfs super block.
2893 if (!load_system_files(vol)) {
2894 ntfs_error(sb, "Failed to load system files.");
2895 goto unl_upcase_iput_tmp_ino_err_out_now;
2898 /* We grab a reference, simulating an ntfs_iget(). */
2899 ihold(vol->root_ino);
2900 if ((sb->s_root = d_make_root(vol->root_ino))) {
2901 ntfs_debug("Exiting, status successful.");
2902 /* Release the default upcase if it has no users. */
2903 mutex_lock(&ntfs_lock);
2904 if (!--ntfs_nr_upcase_users && default_upcase) {
2905 ntfs_free(default_upcase);
2906 default_upcase = NULL;
2908 mutex_unlock(&ntfs_lock);
2909 sb->s_export_op = &ntfs_export_ops;
2913 ntfs_error(sb, "Failed to allocate root directory.");
2914 /* Clean up after the successful load_system_files() call from above. */
2915 // TODO: Use ntfs_put_super() instead of repeating all this code...
2916 // FIXME: Should mark the volume clean as the error is most likely
2919 vol->vol_ino = NULL;
2920 /* NTFS 3.0+ specific clean up. */
2921 if (vol->major_ver >= 3) {
2923 if (vol->usnjrnl_j_ino) {
2924 iput(vol->usnjrnl_j_ino);
2925 vol->usnjrnl_j_ino = NULL;
2927 if (vol->usnjrnl_max_ino) {
2928 iput(vol->usnjrnl_max_ino);
2929 vol->usnjrnl_max_ino = NULL;
2931 if (vol->usnjrnl_ino) {
2932 iput(vol->usnjrnl_ino);
2933 vol->usnjrnl_ino = NULL;
2935 if (vol->quota_q_ino) {
2936 iput(vol->quota_q_ino);
2937 vol->quota_q_ino = NULL;
2939 if (vol->quota_ino) {
2940 iput(vol->quota_ino);
2941 vol->quota_ino = NULL;
2943 #endif /* NTFS_RW */
2944 if (vol->extend_ino) {
2945 iput(vol->extend_ino);
2946 vol->extend_ino = NULL;
2948 if (vol->secure_ino) {
2949 iput(vol->secure_ino);
2950 vol->secure_ino = NULL;
2953 iput(vol->root_ino);
2954 vol->root_ino = NULL;
2955 iput(vol->lcnbmp_ino);
2956 vol->lcnbmp_ino = NULL;
2957 iput(vol->mftbmp_ino);
2958 vol->mftbmp_ino = NULL;
2960 if (vol->logfile_ino) {
2961 iput(vol->logfile_ino);
2962 vol->logfile_ino = NULL;
2964 if (vol->mftmirr_ino) {
2965 iput(vol->mftmirr_ino);
2966 vol->mftmirr_ino = NULL;
2968 #endif /* NTFS_RW */
2969 /* Throw away the table of attribute definitions. */
2970 vol->attrdef_size = 0;
2972 ntfs_free(vol->attrdef);
2973 vol->attrdef = NULL;
2975 vol->upcase_len = 0;
2976 mutex_lock(&ntfs_lock);
2977 if (vol->upcase == default_upcase) {
2978 ntfs_nr_upcase_users--;
2981 mutex_unlock(&ntfs_lock);
2983 ntfs_free(vol->upcase);
2987 unload_nls(vol->nls_map);
2988 vol->nls_map = NULL;
2990 /* Error exit code path. */
2991 unl_upcase_iput_tmp_ino_err_out_now:
2993 * Decrease the number of upcase users and destroy the global default
2994 * upcase table if necessary.
2996 mutex_lock(&ntfs_lock);
2997 if (!--ntfs_nr_upcase_users && default_upcase) {
2998 ntfs_free(default_upcase);
2999 default_upcase = NULL;
3001 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
3002 free_compression_buffers();
3003 mutex_unlock(&ntfs_lock);
3004 iput_tmp_ino_err_out_now:
3006 if (vol->mft_ino && vol->mft_ino != tmp_ino)
3008 vol->mft_ino = NULL;
3009 /* Errors at this stage are irrelevant. */
3011 sb->s_fs_info = NULL;
3013 ntfs_debug("Failed, returning -EINVAL.");
3019 * This is a slab cache to optimize allocations and deallocations of Unicode
3020 * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN
3021 * (255) Unicode characters + a terminating NULL Unicode character.
3023 struct kmem_cache *ntfs_name_cache;
3025 /* Slab caches for efficient allocation/deallocation of inodes. */
3026 struct kmem_cache *ntfs_inode_cache;
3027 struct kmem_cache *ntfs_big_inode_cache;
3029 /* Init once constructor for the inode slab cache. */
3030 static void ntfs_big_inode_init_once(void *foo)
3032 ntfs_inode *ni = (ntfs_inode *)foo;
3034 inode_init_once(VFS_I(ni));
3038 * Slab caches to optimize allocations and deallocations of attribute search
3039 * contexts and index contexts, respectively.
3041 struct kmem_cache *ntfs_attr_ctx_cache;
3042 struct kmem_cache *ntfs_index_ctx_cache;
3044 /* Driver wide mutex. */
3045 DEFINE_MUTEX(ntfs_lock);
3047 static struct dentry *ntfs_mount(struct file_system_type *fs_type,
3048 int flags, const char *dev_name, void *data)
3050 return mount_bdev(fs_type, flags, dev_name, data, ntfs_fill_super);
3053 static struct file_system_type ntfs_fs_type = {
3054 .owner = THIS_MODULE,
3056 .mount = ntfs_mount,
3057 .kill_sb = kill_block_super,
3058 .fs_flags = FS_REQUIRES_DEV,
3060 MODULE_ALIAS_FS("ntfs");
3062 /* Stable names for the slab caches. */
3063 static const char ntfs_index_ctx_cache_name[] = "ntfs_index_ctx_cache";
3064 static const char ntfs_attr_ctx_cache_name[] = "ntfs_attr_ctx_cache";
3065 static const char ntfs_name_cache_name[] = "ntfs_name_cache";
3066 static const char ntfs_inode_cache_name[] = "ntfs_inode_cache";
3067 static const char ntfs_big_inode_cache_name[] = "ntfs_big_inode_cache";
3069 static int __init init_ntfs_fs(void)
3073 /* This may be ugly but it results in pretty output so who cares. (-8 */
3074 pr_info("driver " NTFS_VERSION " [Flags: R/"
3088 ntfs_debug("Debug messages are enabled.");
3090 ntfs_index_ctx_cache = kmem_cache_create(ntfs_index_ctx_cache_name,
3091 sizeof(ntfs_index_context), 0 /* offset */,
3092 SLAB_HWCACHE_ALIGN, NULL /* ctor */);
3093 if (!ntfs_index_ctx_cache) {
3094 pr_crit("Failed to create %s!\n", ntfs_index_ctx_cache_name);
3097 ntfs_attr_ctx_cache = kmem_cache_create(ntfs_attr_ctx_cache_name,
3098 sizeof(ntfs_attr_search_ctx), 0 /* offset */,
3099 SLAB_HWCACHE_ALIGN, NULL /* ctor */);
3100 if (!ntfs_attr_ctx_cache) {
3101 pr_crit("NTFS: Failed to create %s!\n",
3102 ntfs_attr_ctx_cache_name);
3106 ntfs_name_cache = kmem_cache_create(ntfs_name_cache_name,
3107 (NTFS_MAX_NAME_LEN+1) * sizeof(ntfschar), 0,
3108 SLAB_HWCACHE_ALIGN, NULL);
3109 if (!ntfs_name_cache) {
3110 pr_crit("Failed to create %s!\n", ntfs_name_cache_name);
3114 ntfs_inode_cache = kmem_cache_create(ntfs_inode_cache_name,
3115 sizeof(ntfs_inode), 0,
3116 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL);
3117 if (!ntfs_inode_cache) {
3118 pr_crit("Failed to create %s!\n", ntfs_inode_cache_name);
3122 ntfs_big_inode_cache = kmem_cache_create(ntfs_big_inode_cache_name,
3123 sizeof(big_ntfs_inode), 0,
3124 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
3125 SLAB_ACCOUNT, ntfs_big_inode_init_once);
3126 if (!ntfs_big_inode_cache) {
3127 pr_crit("Failed to create %s!\n", ntfs_big_inode_cache_name);
3128 goto big_inode_err_out;
3131 /* Register the ntfs sysctls. */
3132 err = ntfs_sysctl(1);
3134 pr_crit("Failed to register NTFS sysctls!\n");
3135 goto sysctl_err_out;
3138 err = register_filesystem(&ntfs_fs_type);
3140 ntfs_debug("NTFS driver registered successfully.");
3141 return 0; /* Success! */
3143 pr_crit("Failed to register NTFS filesystem driver!\n");
3145 /* Unregister the ntfs sysctls. */
3148 kmem_cache_destroy(ntfs_big_inode_cache);
3150 kmem_cache_destroy(ntfs_inode_cache);
3152 kmem_cache_destroy(ntfs_name_cache);
3154 kmem_cache_destroy(ntfs_attr_ctx_cache);
3156 kmem_cache_destroy(ntfs_index_ctx_cache);
3159 pr_crit("Aborting NTFS filesystem driver registration...\n");
3165 static void __exit exit_ntfs_fs(void)
3167 ntfs_debug("Unregistering NTFS driver.");
3169 unregister_filesystem(&ntfs_fs_type);
3172 * Make sure all delayed rcu free inodes are flushed before we
3176 kmem_cache_destroy(ntfs_big_inode_cache);
3177 kmem_cache_destroy(ntfs_inode_cache);
3178 kmem_cache_destroy(ntfs_name_cache);
3179 kmem_cache_destroy(ntfs_attr_ctx_cache);
3180 kmem_cache_destroy(ntfs_index_ctx_cache);
3181 /* Unregister the ntfs sysctls. */
3185 MODULE_AUTHOR("Anton Altaparmakov <anton@tuxera.com>");
3186 MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2014 Anton Altaparmakov and Tuxera Inc.");
3187 MODULE_VERSION(NTFS_VERSION);
3188 MODULE_LICENSE("GPL");
3190 module_param(debug_msgs, bint, 0);
3191 MODULE_PARM_DESC(debug_msgs, "Enable debug messages.");
3194 module_init(init_ntfs_fs)
3195 module_exit(exit_ntfs_fs)