2 * super.c - NTFS kernel super block handling. Part of the Linux-NTFS project.
4 * Copyright (c) 2001-2006 Anton Altaparmakov
5 * Copyright (c) 2001,2002 Richard Russon
7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program/include file is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program (in the main directory of the Linux-NTFS
19 * distribution in the file COPYING); if not, write to the Free Software
20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/stddef.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
27 #include <linux/spinlock.h>
28 #include <linux/blkdev.h> /* For bdev_hardsect_size(). */
29 #include <linux/backing-dev.h>
30 #include <linux/buffer_head.h>
31 #include <linux/vfs.h>
32 #include <linux/moduleparam.h>
33 #include <linux/smp_lock.h>
47 /* Number of mounted filesystems which have compression enabled. */
48 static unsigned long ntfs_nr_compression_users;
50 /* A global default upcase table and a corresponding reference count. */
51 static ntfschar *default_upcase = NULL;
52 static unsigned long ntfs_nr_upcase_users = 0;
54 /* Error constants/strings used in inode.c::ntfs_show_options(). */
56 /* One of these must be present, default is ON_ERRORS_CONTINUE. */
57 ON_ERRORS_PANIC = 0x01,
58 ON_ERRORS_REMOUNT_RO = 0x02,
59 ON_ERRORS_CONTINUE = 0x04,
60 /* Optional, can be combined with any of the above. */
61 ON_ERRORS_RECOVER = 0x10,
64 const option_t on_errors_arr[] = {
65 { ON_ERRORS_PANIC, "panic" },
66 { ON_ERRORS_REMOUNT_RO, "remount-ro", },
67 { ON_ERRORS_CONTINUE, "continue", },
68 { ON_ERRORS_RECOVER, "recover" },
75 * Copied from old ntfs driver (which copied from vfat driver).
77 static int simple_getbool(char *s, BOOL *setval)
80 if (!strcmp(s, "1") || !strcmp(s, "yes") || !strcmp(s, "true"))
82 else if (!strcmp(s, "0") || !strcmp(s, "no") ||
93 * parse_options - parse the (re)mount options
95 * @opt: string containing the (re)mount options
97 * Parse the recognized options in @opt for the ntfs volume described by @vol.
99 static BOOL parse_options(ntfs_volume *vol, char *opt)
102 static char *utf8 = "utf8";
103 int errors = 0, sloppy = 0;
104 uid_t uid = (uid_t)-1;
105 gid_t gid = (gid_t)-1;
106 mode_t fmask = (mode_t)-1, dmask = (mode_t)-1;
107 int mft_zone_multiplier = -1, on_errors = -1;
108 int show_sys_files = -1, case_sensitive = -1, disable_sparse = -1;
109 struct nls_table *nls_map = NULL, *old_nls;
111 /* I am lazy... (-8 */
112 #define NTFS_GETOPT_WITH_DEFAULT(option, variable, default_value) \
113 if (!strcmp(p, option)) { \
115 variable = default_value; \
117 variable = simple_strtoul(ov = v, &v, 0); \
122 #define NTFS_GETOPT(option, variable) \
123 if (!strcmp(p, option)) { \
126 variable = simple_strtoul(ov = v, &v, 0); \
130 #define NTFS_GETOPT_OCTAL(option, variable) \
131 if (!strcmp(p, option)) { \
134 variable = simple_strtoul(ov = v, &v, 8); \
138 #define NTFS_GETOPT_BOOL(option, variable) \
139 if (!strcmp(p, option)) { \
141 if (!simple_getbool(v, &val)) \
145 #define NTFS_GETOPT_OPTIONS_ARRAY(option, variable, opt_array) \
146 if (!strcmp(p, option)) { \
151 if (variable == -1) \
153 for (_i = 0; opt_array[_i].str && *opt_array[_i].str; _i++) \
154 if (!strcmp(opt_array[_i].str, v)) { \
155 variable |= opt_array[_i].val; \
158 if (!opt_array[_i].str || !*opt_array[_i].str) \
162 goto no_mount_options;
163 ntfs_debug("Entering with mount options string: %s", opt);
164 while ((p = strsep(&opt, ","))) {
165 if ((v = strchr(p, '=')))
167 NTFS_GETOPT("uid", uid)
168 else NTFS_GETOPT("gid", gid)
169 else NTFS_GETOPT_OCTAL("umask", fmask = dmask)
170 else NTFS_GETOPT_OCTAL("fmask", fmask)
171 else NTFS_GETOPT_OCTAL("dmask", dmask)
172 else NTFS_GETOPT("mft_zone_multiplier", mft_zone_multiplier)
173 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, TRUE)
174 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files)
175 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive)
176 else NTFS_GETOPT_BOOL("disable_sparse", disable_sparse)
177 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors,
179 else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes"))
180 ntfs_warning(vol->sb, "Ignoring obsolete option %s.",
182 else if (!strcmp(p, "nls") || !strcmp(p, "iocharset")) {
183 if (!strcmp(p, "iocharset"))
184 ntfs_warning(vol->sb, "Option iocharset is "
185 "deprecated. Please use "
186 "option nls=<charsetname> in "
192 nls_map = load_nls(v);
195 ntfs_error(vol->sb, "NLS character set "
199 ntfs_error(vol->sb, "NLS character set %s not "
200 "found. Using previous one %s.",
201 v, old_nls->charset);
203 } else /* nls_map */ {
207 } else if (!strcmp(p, "utf8")) {
209 ntfs_warning(vol->sb, "Option utf8 is no longer "
210 "supported, using option nls=utf8. Please "
211 "use option nls=utf8 in the future and "
212 "make sure utf8 is compiled either as a "
213 "module or into the kernel.");
216 else if (!simple_getbool(v, &val))
223 ntfs_error(vol->sb, "Unrecognized mount option %s.", p);
224 if (errors < INT_MAX)
227 #undef NTFS_GETOPT_OPTIONS_ARRAY
228 #undef NTFS_GETOPT_BOOL
230 #undef NTFS_GETOPT_WITH_DEFAULT
233 if (errors && !sloppy)
236 ntfs_warning(vol->sb, "Sloppy option given. Ignoring "
237 "unrecognized mount option(s) and continuing.");
238 /* Keep this first! */
239 if (on_errors != -1) {
241 ntfs_error(vol->sb, "Invalid errors option argument "
242 "or bug in options parser.");
247 if (vol->nls_map && vol->nls_map != nls_map) {
248 ntfs_error(vol->sb, "Cannot change NLS character set "
251 } /* else (!vol->nls_map) */
252 ntfs_debug("Using NLS character set %s.", nls_map->charset);
253 vol->nls_map = nls_map;
254 } else /* (!nls_map) */ {
256 vol->nls_map = load_nls_default();
258 ntfs_error(vol->sb, "Failed to load default "
259 "NLS character set.");
262 ntfs_debug("Using default NLS character set (%s).",
263 vol->nls_map->charset);
266 if (mft_zone_multiplier != -1) {
267 if (vol->mft_zone_multiplier && vol->mft_zone_multiplier !=
268 mft_zone_multiplier) {
269 ntfs_error(vol->sb, "Cannot change mft_zone_multiplier "
273 if (mft_zone_multiplier < 1 || mft_zone_multiplier > 4) {
274 ntfs_error(vol->sb, "Invalid mft_zone_multiplier. "
275 "Using default value, i.e. 1.");
276 mft_zone_multiplier = 1;
278 vol->mft_zone_multiplier = mft_zone_multiplier;
280 if (!vol->mft_zone_multiplier)
281 vol->mft_zone_multiplier = 1;
283 vol->on_errors = on_errors;
284 if (!vol->on_errors || vol->on_errors == ON_ERRORS_RECOVER)
285 vol->on_errors |= ON_ERRORS_CONTINUE;
286 if (uid != (uid_t)-1)
288 if (gid != (gid_t)-1)
290 if (fmask != (mode_t)-1)
292 if (dmask != (mode_t)-1)
294 if (show_sys_files != -1) {
296 NVolSetShowSystemFiles(vol);
298 NVolClearShowSystemFiles(vol);
300 if (case_sensitive != -1) {
302 NVolSetCaseSensitive(vol);
304 NVolClearCaseSensitive(vol);
306 if (disable_sparse != -1) {
308 NVolClearSparseEnabled(vol);
310 if (!NVolSparseEnabled(vol) &&
311 vol->major_ver && vol->major_ver < 3)
312 ntfs_warning(vol->sb, "Not enabling sparse "
313 "support due to NTFS volume "
314 "version %i.%i (need at least "
315 "version 3.0).", vol->major_ver,
318 NVolSetSparseEnabled(vol);
323 ntfs_error(vol->sb, "The %s option requires an argument.", p);
326 ntfs_error(vol->sb, "The %s option requires a boolean argument.", p);
329 ntfs_error(vol->sb, "Invalid %s option argument: %s", p, ov);
336 * ntfs_write_volume_flags - write new flags to the volume information flags
337 * @vol: ntfs volume on which to modify the flags
338 * @flags: new flags value for the volume information flags
340 * Internal function. You probably want to use ntfs_{set,clear}_volume_flags()
341 * instead (see below).
343 * Replace the volume information flags on the volume @vol with the value
344 * supplied in @flags. Note, this overwrites the volume information flags, so
345 * make sure to combine the flags you want to modify with the old flags and use
346 * the result when calling ntfs_write_volume_flags().
348 * Return 0 on success and -errno on error.
350 static int ntfs_write_volume_flags(ntfs_volume *vol, const VOLUME_FLAGS flags)
352 ntfs_inode *ni = NTFS_I(vol->vol_ino);
354 VOLUME_INFORMATION *vi;
355 ntfs_attr_search_ctx *ctx;
358 ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
359 le16_to_cpu(vol->vol_flags), le16_to_cpu(flags));
360 if (vol->vol_flags == flags)
363 m = map_mft_record(ni);
368 ctx = ntfs_attr_get_search_ctx(ni, m);
371 goto put_unm_err_out;
373 err = ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
376 goto put_unm_err_out;
377 vi = (VOLUME_INFORMATION*)((u8*)ctx->attr +
378 le16_to_cpu(ctx->attr->data.resident.value_offset));
379 vol->vol_flags = vi->flags = flags;
380 flush_dcache_mft_record_page(ctx->ntfs_ino);
381 mark_mft_record_dirty(ctx->ntfs_ino);
382 ntfs_attr_put_search_ctx(ctx);
383 unmap_mft_record(ni);
389 ntfs_attr_put_search_ctx(ctx);
390 unmap_mft_record(ni);
392 ntfs_error(vol->sb, "Failed with error code %i.", -err);
397 * ntfs_set_volume_flags - set bits in the volume information flags
398 * @vol: ntfs volume on which to modify the flags
399 * @flags: flags to set on the volume
401 * Set the bits in @flags in the volume information flags on the volume @vol.
403 * Return 0 on success and -errno on error.
405 static inline int ntfs_set_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
407 flags &= VOLUME_FLAGS_MASK;
408 return ntfs_write_volume_flags(vol, vol->vol_flags | flags);
412 * ntfs_clear_volume_flags - clear bits in the volume information flags
413 * @vol: ntfs volume on which to modify the flags
414 * @flags: flags to clear on the volume
416 * Clear the bits in @flags in the volume information flags on the volume @vol.
418 * Return 0 on success and -errno on error.
420 static inline int ntfs_clear_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
422 flags &= VOLUME_FLAGS_MASK;
423 flags = vol->vol_flags & cpu_to_le16(~le16_to_cpu(flags));
424 return ntfs_write_volume_flags(vol, flags);
430 * ntfs_remount - change the mount options of a mounted ntfs filesystem
431 * @sb: superblock of mounted ntfs filesystem
432 * @flags: remount flags
433 * @opt: remount options string
435 * Change the mount options of an already mounted ntfs filesystem.
437 * NOTE: The VFS sets the @sb->s_flags remount flags to @flags after
438 * ntfs_remount() returns successfully (i.e. returns 0). Otherwise,
439 * @sb->s_flags are not changed.
441 static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
443 ntfs_volume *vol = NTFS_SB(sb);
445 ntfs_debug("Entering with remount options string: %s", opt);
447 /* For read-only compiled driver, enforce read-only flag. */
451 * For the read-write compiled driver, if we are remounting read-write,
452 * make sure there are no volume errors and that no unsupported volume
453 * flags are set. Also, empty the logfile journal as it would become
454 * stale as soon as something is written to the volume and mark the
455 * volume dirty so that chkdsk is run if the volume is not umounted
456 * cleanly. Finally, mark the quotas out of date so Windows rescans
457 * the volume on boot and updates them.
459 * When remounting read-only, mark the volume clean if no volume errors
462 if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
463 static const char *es = ". Cannot remount read-write.";
465 /* Remounting read-write. */
466 if (NVolErrors(vol)) {
467 ntfs_error(sb, "Volume has errors and is read-only%s",
471 if (vol->vol_flags & VOLUME_IS_DIRTY) {
472 ntfs_error(sb, "Volume is dirty and read-only%s", es);
475 if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
476 ntfs_error(sb, "Volume has been modified by chkdsk "
477 "and is read-only%s", es);
480 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
481 ntfs_error(sb, "Volume has unsupported flags set "
482 "(0x%x) and is read-only%s",
483 (unsigned)le16_to_cpu(vol->vol_flags),
487 if (ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
488 ntfs_error(sb, "Failed to set dirty bit in volume "
489 "information flags%s", es);
493 // TODO: Enable this code once we start modifying anything that
494 // is different between NTFS 1.2 and 3.x...
495 /* Set NT4 compatibility flag on newer NTFS version volumes. */
496 if ((vol->major_ver > 1)) {
497 if (ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
498 ntfs_error(sb, "Failed to set NT4 "
499 "compatibility flag%s", es);
505 if (!ntfs_empty_logfile(vol->logfile_ino)) {
506 ntfs_error(sb, "Failed to empty journal $LogFile%s",
511 if (!ntfs_mark_quotas_out_of_date(vol)) {
512 ntfs_error(sb, "Failed to mark quotas out of date%s",
517 if (!ntfs_stamp_usnjrnl(vol)) {
518 ntfs_error(sb, "Failed to stamp transation log "
523 } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) {
524 /* Remounting read-only. */
525 if (!NVolErrors(vol)) {
526 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
527 ntfs_warning(sb, "Failed to clear dirty bit "
528 "in volume information "
529 "flags. Run chkdsk.");
534 // TODO: Deal with *flags.
536 if (!parse_options(vol, opt))
543 * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector
544 * @sb: Super block of the device to which @b belongs.
545 * @b: Boot sector of device @sb to check.
546 * @silent: If TRUE, all output will be silenced.
548 * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot
549 * sector. Returns TRUE if it is valid and FALSE if not.
551 * @sb is only needed for warning/error output, i.e. it can be NULL when silent
554 static BOOL is_boot_sector_ntfs(const struct super_block *sb,
555 const NTFS_BOOT_SECTOR *b, const BOOL silent)
558 * Check that checksum == sum of u32 values from b to the checksum
559 * field. If checksum is zero, no checking is done. We will work when
560 * the checksum test fails, since some utilities update the boot sector
561 * ignoring the checksum which leaves the checksum out-of-date. We
562 * report a warning if this is the case.
564 if ((void*)b < (void*)&b->checksum && b->checksum && !silent) {
568 for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u)
569 i += le32_to_cpup(u);
570 if (le32_to_cpu(b->checksum) != i)
571 ntfs_warning(sb, "Invalid boot sector checksum.");
573 /* Check OEMidentifier is "NTFS " */
574 if (b->oem_id != magicNTFS)
576 /* Check bytes per sector value is between 256 and 4096. */
577 if (le16_to_cpu(b->bpb.bytes_per_sector) < 0x100 ||
578 le16_to_cpu(b->bpb.bytes_per_sector) > 0x1000)
580 /* Check sectors per cluster value is valid. */
581 switch (b->bpb.sectors_per_cluster) {
582 case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128:
587 /* Check the cluster size is not above the maximum (64kiB). */
588 if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) *
589 b->bpb.sectors_per_cluster > NTFS_MAX_CLUSTER_SIZE)
591 /* Check reserved/unused fields are really zero. */
592 if (le16_to_cpu(b->bpb.reserved_sectors) ||
593 le16_to_cpu(b->bpb.root_entries) ||
594 le16_to_cpu(b->bpb.sectors) ||
595 le16_to_cpu(b->bpb.sectors_per_fat) ||
596 le32_to_cpu(b->bpb.large_sectors) || b->bpb.fats)
598 /* Check clusters per file mft record value is valid. */
599 if ((u8)b->clusters_per_mft_record < 0xe1 ||
600 (u8)b->clusters_per_mft_record > 0xf7)
601 switch (b->clusters_per_mft_record) {
602 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
607 /* Check clusters per index block value is valid. */
608 if ((u8)b->clusters_per_index_record < 0xe1 ||
609 (u8)b->clusters_per_index_record > 0xf7)
610 switch (b->clusters_per_index_record) {
611 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
617 * Check for valid end of sector marker. We will work without it, but
618 * many BIOSes will refuse to boot from a bootsector if the magic is
619 * incorrect, so we emit a warning.
621 if (!silent && b->end_of_sector_marker != const_cpu_to_le16(0xaa55))
622 ntfs_warning(sb, "Invalid end of sector marker.");
629 * read_ntfs_boot_sector - read the NTFS boot sector of a device
630 * @sb: super block of device to read the boot sector from
631 * @silent: if true, suppress all output
633 * Reads the boot sector from the device and validates it. If that fails, tries
634 * to read the backup boot sector, first from the end of the device a-la NT4 and
635 * later and then from the middle of the device a-la NT3.51 and before.
637 * If a valid boot sector is found but it is not the primary boot sector, we
638 * repair the primary boot sector silently (unless the device is read-only or
639 * the primary boot sector is not accessible).
641 * NOTE: To call this function, @sb must have the fields s_dev, the ntfs super
642 * block (u.ntfs_sb), nr_blocks and the device flags (s_flags) initialized
643 * to their respective values.
645 * Return the unlocked buffer head containing the boot sector or NULL on error.
647 static struct buffer_head *read_ntfs_boot_sector(struct super_block *sb,
650 const char *read_err_str = "Unable to read %s boot sector.";
651 struct buffer_head *bh_primary, *bh_backup;
652 sector_t nr_blocks = NTFS_SB(sb)->nr_blocks;
654 /* Try to read primary boot sector. */
655 if ((bh_primary = sb_bread(sb, 0))) {
656 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
657 bh_primary->b_data, silent))
660 ntfs_error(sb, "Primary boot sector is invalid.");
662 ntfs_error(sb, read_err_str, "primary");
663 if (!(NTFS_SB(sb)->on_errors & ON_ERRORS_RECOVER)) {
667 ntfs_error(sb, "Mount option errors=recover not used. "
668 "Aborting without trying to recover.");
671 /* Try to read NT4+ backup boot sector. */
672 if ((bh_backup = sb_bread(sb, nr_blocks - 1))) {
673 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
674 bh_backup->b_data, silent))
675 goto hotfix_primary_boot_sector;
678 ntfs_error(sb, read_err_str, "backup");
679 /* Try to read NT3.51- backup boot sector. */
680 if ((bh_backup = sb_bread(sb, nr_blocks >> 1))) {
681 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
682 bh_backup->b_data, silent))
683 goto hotfix_primary_boot_sector;
685 ntfs_error(sb, "Could not find a valid backup boot "
689 ntfs_error(sb, read_err_str, "backup");
690 /* We failed. Cleanup and return. */
694 hotfix_primary_boot_sector:
697 * If we managed to read sector zero and the volume is not
698 * read-only, copy the found, valid backup boot sector to the
699 * primary boot sector. Note we only copy the actual boot
700 * sector structure, not the actual whole device sector as that
701 * may be bigger and would potentially damage the $Boot system
702 * file (FIXME: Would be nice to know if the backup boot sector
703 * on a large sector device contains the whole boot loader or
704 * just the first 512 bytes).
706 if (!(sb->s_flags & MS_RDONLY)) {
707 ntfs_warning(sb, "Hot-fix: Recovering invalid primary "
708 "boot sector from backup copy.");
709 memcpy(bh_primary->b_data, bh_backup->b_data,
711 mark_buffer_dirty(bh_primary);
712 sync_dirty_buffer(bh_primary);
713 if (buffer_uptodate(bh_primary)) {
717 ntfs_error(sb, "Hot-fix: Device write error while "
718 "recovering primary boot sector.");
720 ntfs_warning(sb, "Hot-fix: Recovery of primary boot "
721 "sector failed: Read-only mount.");
725 ntfs_warning(sb, "Using backup boot sector.");
730 * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol
731 * @vol: volume structure to initialise with data from boot sector
732 * @b: boot sector to parse
734 * Parse the ntfs boot sector @b and store all imporant information therein in
735 * the ntfs super block @vol. Return TRUE on success and FALSE on error.
737 static BOOL parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
739 unsigned int sectors_per_cluster_bits, nr_hidden_sects;
740 int clusters_per_mft_record, clusters_per_index_record;
743 vol->sector_size = le16_to_cpu(b->bpb.bytes_per_sector);
744 vol->sector_size_bits = ffs(vol->sector_size) - 1;
745 ntfs_debug("vol->sector_size = %i (0x%x)", vol->sector_size,
747 ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits,
748 vol->sector_size_bits);
749 if (vol->sector_size < vol->sb->s_blocksize) {
750 ntfs_error(vol->sb, "Sector size (%i) is smaller than the "
751 "device block size (%lu). This is not "
752 "supported. Sorry.", vol->sector_size,
753 vol->sb->s_blocksize);
756 ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster);
757 sectors_per_cluster_bits = ffs(b->bpb.sectors_per_cluster) - 1;
758 ntfs_debug("sectors_per_cluster_bits = 0x%x",
759 sectors_per_cluster_bits);
760 nr_hidden_sects = le32_to_cpu(b->bpb.hidden_sectors);
761 ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects);
762 vol->cluster_size = vol->sector_size << sectors_per_cluster_bits;
763 vol->cluster_size_mask = vol->cluster_size - 1;
764 vol->cluster_size_bits = ffs(vol->cluster_size) - 1;
765 ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size,
767 ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask);
768 ntfs_debug("vol->cluster_size_bits = %i", vol->cluster_size_bits);
769 if (vol->cluster_size < vol->sector_size) {
770 ntfs_error(vol->sb, "Cluster size (%i) is smaller than the "
771 "sector size (%i). This is not supported. "
772 "Sorry.", vol->cluster_size, vol->sector_size);
775 clusters_per_mft_record = b->clusters_per_mft_record;
776 ntfs_debug("clusters_per_mft_record = %i (0x%x)",
777 clusters_per_mft_record, clusters_per_mft_record);
778 if (clusters_per_mft_record > 0)
779 vol->mft_record_size = vol->cluster_size <<
780 (ffs(clusters_per_mft_record) - 1);
783 * When mft_record_size < cluster_size, clusters_per_mft_record
784 * = -log2(mft_record_size) bytes. mft_record_size normaly is
785 * 1024 bytes, which is encoded as 0xF6 (-10 in decimal).
787 vol->mft_record_size = 1 << -clusters_per_mft_record;
788 vol->mft_record_size_mask = vol->mft_record_size - 1;
789 vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1;
790 ntfs_debug("vol->mft_record_size = %i (0x%x)", vol->mft_record_size,
791 vol->mft_record_size);
792 ntfs_debug("vol->mft_record_size_mask = 0x%x",
793 vol->mft_record_size_mask);
794 ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
795 vol->mft_record_size_bits, vol->mft_record_size_bits);
797 * We cannot support mft record sizes above the PAGE_CACHE_SIZE since
798 * we store $MFT/$DATA, the table of mft records in the page cache.
800 if (vol->mft_record_size > PAGE_CACHE_SIZE) {
801 ntfs_error(vol->sb, "Mft record size (%i) exceeds the "
802 "PAGE_CACHE_SIZE on your system (%lu). "
803 "This is not supported. Sorry.",
804 vol->mft_record_size, PAGE_CACHE_SIZE);
807 /* We cannot support mft record sizes below the sector size. */
808 if (vol->mft_record_size < vol->sector_size) {
809 ntfs_error(vol->sb, "Mft record size (%i) is smaller than the "
810 "sector size (%i). This is not supported. "
811 "Sorry.", vol->mft_record_size,
815 clusters_per_index_record = b->clusters_per_index_record;
816 ntfs_debug("clusters_per_index_record = %i (0x%x)",
817 clusters_per_index_record, clusters_per_index_record);
818 if (clusters_per_index_record > 0)
819 vol->index_record_size = vol->cluster_size <<
820 (ffs(clusters_per_index_record) - 1);
823 * When index_record_size < cluster_size,
824 * clusters_per_index_record = -log2(index_record_size) bytes.
825 * index_record_size normaly equals 4096 bytes, which is
826 * encoded as 0xF4 (-12 in decimal).
828 vol->index_record_size = 1 << -clusters_per_index_record;
829 vol->index_record_size_mask = vol->index_record_size - 1;
830 vol->index_record_size_bits = ffs(vol->index_record_size) - 1;
831 ntfs_debug("vol->index_record_size = %i (0x%x)",
832 vol->index_record_size, vol->index_record_size);
833 ntfs_debug("vol->index_record_size_mask = 0x%x",
834 vol->index_record_size_mask);
835 ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
836 vol->index_record_size_bits,
837 vol->index_record_size_bits);
838 /* We cannot support index record sizes below the sector size. */
839 if (vol->index_record_size < vol->sector_size) {
840 ntfs_error(vol->sb, "Index record size (%i) is smaller than "
841 "the sector size (%i). This is not "
842 "supported. Sorry.", vol->index_record_size,
847 * Get the size of the volume in clusters and check for 64-bit-ness.
848 * Windows currently only uses 32 bits to save the clusters so we do
849 * the same as it is much faster on 32-bit CPUs.
851 ll = sle64_to_cpu(b->number_of_sectors) >> sectors_per_cluster_bits;
852 if ((u64)ll >= 1ULL << 32) {
853 ntfs_error(vol->sb, "Cannot handle 64-bit clusters. Sorry.");
856 vol->nr_clusters = ll;
857 ntfs_debug("vol->nr_clusters = 0x%llx", (long long)vol->nr_clusters);
859 * On an architecture where unsigned long is 32-bits, we restrict the
860 * volume size to 2TiB (2^41). On a 64-bit architecture, the compiler
861 * will hopefully optimize the whole check away.
863 if (sizeof(unsigned long) < 8) {
864 if ((ll << vol->cluster_size_bits) >= (1ULL << 41)) {
865 ntfs_error(vol->sb, "Volume size (%lluTiB) is too "
866 "large for this architecture. "
867 "Maximum supported is 2TiB. Sorry.",
868 (unsigned long long)ll >> (40 -
869 vol->cluster_size_bits));
873 ll = sle64_to_cpu(b->mft_lcn);
874 if (ll >= vol->nr_clusters) {
875 ntfs_error(vol->sb, "MFT LCN (%lli, 0x%llx) is beyond end of "
876 "volume. Weird.", (unsigned long long)ll,
877 (unsigned long long)ll);
881 ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol->mft_lcn);
882 ll = sle64_to_cpu(b->mftmirr_lcn);
883 if (ll >= vol->nr_clusters) {
884 ntfs_error(vol->sb, "MFTMirr LCN (%lli, 0x%llx) is beyond end "
885 "of volume. Weird.", (unsigned long long)ll,
886 (unsigned long long)ll);
889 vol->mftmirr_lcn = ll;
890 ntfs_debug("vol->mftmirr_lcn = 0x%llx", (long long)vol->mftmirr_lcn);
893 * Work out the size of the mft mirror in number of mft records. If the
894 * cluster size is less than or equal to the size taken by four mft
895 * records, the mft mirror stores the first four mft records. If the
896 * cluster size is bigger than the size taken by four mft records, the
897 * mft mirror contains as many mft records as will fit into one
900 if (vol->cluster_size <= (4 << vol->mft_record_size_bits))
901 vol->mftmirr_size = 4;
903 vol->mftmirr_size = vol->cluster_size >>
904 vol->mft_record_size_bits;
905 ntfs_debug("vol->mftmirr_size = %i", vol->mftmirr_size);
907 vol->serial_no = le64_to_cpu(b->volume_serial_number);
908 ntfs_debug("vol->serial_no = 0x%llx",
909 (unsigned long long)vol->serial_no);
914 * ntfs_setup_allocators - initialize the cluster and mft allocators
915 * @vol: volume structure for which to setup the allocators
917 * Setup the cluster (lcn) and mft allocators to the starting values.
919 static void ntfs_setup_allocators(ntfs_volume *vol)
922 LCN mft_zone_size, mft_lcn;
925 ntfs_debug("vol->mft_zone_multiplier = 0x%x",
926 vol->mft_zone_multiplier);
928 /* Determine the size of the MFT zone. */
929 mft_zone_size = vol->nr_clusters;
930 switch (vol->mft_zone_multiplier) { /* % of volume size in clusters */
932 mft_zone_size >>= 1; /* 50% */
935 mft_zone_size = (mft_zone_size +
936 (mft_zone_size >> 1)) >> 2; /* 37.5% */
939 mft_zone_size >>= 2; /* 25% */
943 mft_zone_size >>= 3; /* 12.5% */
946 /* Setup the mft zone. */
947 vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn;
948 ntfs_debug("vol->mft_zone_pos = 0x%llx",
949 (unsigned long long)vol->mft_zone_pos);
951 * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
952 * source) and if the actual mft_lcn is in the expected place or even
953 * further to the front of the volume, extend the mft_zone to cover the
954 * beginning of the volume as well. This is in order to protect the
955 * area reserved for the mft bitmap as well within the mft_zone itself.
956 * On non-standard volumes we do not protect it as the overhead would
957 * be higher than the speed increase we would get by doing it.
959 mft_lcn = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size;
960 if (mft_lcn * vol->cluster_size < 16 * 1024)
961 mft_lcn = (16 * 1024 + vol->cluster_size - 1) /
963 if (vol->mft_zone_start <= mft_lcn)
964 vol->mft_zone_start = 0;
965 ntfs_debug("vol->mft_zone_start = 0x%llx",
966 (unsigned long long)vol->mft_zone_start);
968 * Need to cap the mft zone on non-standard volumes so that it does
969 * not point outside the boundaries of the volume. We do this by
970 * halving the zone size until we are inside the volume.
972 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
973 while (vol->mft_zone_end >= vol->nr_clusters) {
975 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
977 ntfs_debug("vol->mft_zone_end = 0x%llx",
978 (unsigned long long)vol->mft_zone_end);
980 * Set the current position within each data zone to the start of the
983 vol->data1_zone_pos = vol->mft_zone_end;
984 ntfs_debug("vol->data1_zone_pos = 0x%llx",
985 (unsigned long long)vol->data1_zone_pos);
986 vol->data2_zone_pos = 0;
987 ntfs_debug("vol->data2_zone_pos = 0x%llx",
988 (unsigned long long)vol->data2_zone_pos);
990 /* Set the mft data allocation position to mft record 24. */
991 vol->mft_data_pos = 24;
992 ntfs_debug("vol->mft_data_pos = 0x%llx",
993 (unsigned long long)vol->mft_data_pos);
1000 * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume
1001 * @vol: ntfs super block describing device whose mft mirror to load
1003 * Return TRUE on success or FALSE on error.
1005 static BOOL load_and_init_mft_mirror(ntfs_volume *vol)
1007 struct inode *tmp_ino;
1010 ntfs_debug("Entering.");
1011 /* Get mft mirror inode. */
1012 tmp_ino = ntfs_iget(vol->sb, FILE_MFTMirr);
1013 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1014 if (!IS_ERR(tmp_ino))
1016 /* Caller will display error message. */
1020 * Re-initialize some specifics about $MFTMirr's inode as
1021 * ntfs_read_inode() will have set up the default ones.
1023 /* Set uid and gid to root. */
1024 tmp_ino->i_uid = tmp_ino->i_gid = 0;
1025 /* Regular file. No access for anyone. */
1026 tmp_ino->i_mode = S_IFREG;
1027 /* No VFS initiated operations allowed for $MFTMirr. */
1028 tmp_ino->i_op = &ntfs_empty_inode_ops;
1029 tmp_ino->i_fop = &ntfs_empty_file_ops;
1030 /* Put in our special address space operations. */
1031 tmp_ino->i_mapping->a_ops = &ntfs_mst_aops;
1032 tmp_ni = NTFS_I(tmp_ino);
1033 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
1034 NInoSetMstProtected(tmp_ni);
1035 NInoSetSparseDisabled(tmp_ni);
1037 * Set up our little cheat allowing us to reuse the async read io
1038 * completion handler for directories.
1040 tmp_ni->itype.index.block_size = vol->mft_record_size;
1041 tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits;
1042 vol->mftmirr_ino = tmp_ino;
1043 ntfs_debug("Done.");
1048 * check_mft_mirror - compare contents of the mft mirror with the mft
1049 * @vol: ntfs super block describing device whose mft mirror to check
1051 * Return TRUE on success or FALSE on error.
1053 * Note, this function also results in the mft mirror runlist being completely
1054 * mapped into memory. The mft mirror write code requires this and will BUG()
1055 * should it find an unmapped runlist element.
1057 static BOOL check_mft_mirror(ntfs_volume *vol)
1059 struct super_block *sb = vol->sb;
1060 ntfs_inode *mirr_ni;
1061 struct page *mft_page, *mirr_page;
1063 runlist_element *rl, rl2[2];
1065 int mrecs_per_page, i;
1067 ntfs_debug("Entering.");
1068 /* Compare contents of $MFT and $MFTMirr. */
1069 mrecs_per_page = PAGE_CACHE_SIZE / vol->mft_record_size;
1070 BUG_ON(!mrecs_per_page);
1071 BUG_ON(!vol->mftmirr_size);
1072 mft_page = mirr_page = NULL;
1073 kmft = kmirr = NULL;
1078 /* Switch pages if necessary. */
1079 if (!(i % mrecs_per_page)) {
1081 ntfs_unmap_page(mft_page);
1082 ntfs_unmap_page(mirr_page);
1084 /* Get the $MFT page. */
1085 mft_page = ntfs_map_page(vol->mft_ino->i_mapping,
1087 if (IS_ERR(mft_page)) {
1088 ntfs_error(sb, "Failed to read $MFT.");
1091 kmft = page_address(mft_page);
1092 /* Get the $MFTMirr page. */
1093 mirr_page = ntfs_map_page(vol->mftmirr_ino->i_mapping,
1095 if (IS_ERR(mirr_page)) {
1096 ntfs_error(sb, "Failed to read $MFTMirr.");
1099 kmirr = page_address(mirr_page);
1102 /* Do not check the record if it is not in use. */
1103 if (((MFT_RECORD*)kmft)->flags & MFT_RECORD_IN_USE) {
1104 /* Make sure the record is ok. */
1105 if (ntfs_is_baad_recordp((le32*)kmft)) {
1106 ntfs_error(sb, "Incomplete multi sector "
1107 "transfer detected in mft "
1110 ntfs_unmap_page(mirr_page);
1112 ntfs_unmap_page(mft_page);
1116 /* Do not check the mirror record if it is not in use. */
1117 if (((MFT_RECORD*)kmirr)->flags & MFT_RECORD_IN_USE) {
1118 if (ntfs_is_baad_recordp((le32*)kmirr)) {
1119 ntfs_error(sb, "Incomplete multi sector "
1120 "transfer detected in mft "
1121 "mirror record %i.", i);
1125 /* Get the amount of data in the current record. */
1126 bytes = le32_to_cpu(((MFT_RECORD*)kmft)->bytes_in_use);
1127 if (bytes < sizeof(MFT_RECORD_OLD) ||
1128 bytes > vol->mft_record_size ||
1129 ntfs_is_baad_recordp((le32*)kmft)) {
1130 bytes = le32_to_cpu(((MFT_RECORD*)kmirr)->bytes_in_use);
1131 if (bytes < sizeof(MFT_RECORD_OLD) ||
1132 bytes > vol->mft_record_size ||
1133 ntfs_is_baad_recordp((le32*)kmirr))
1134 bytes = vol->mft_record_size;
1136 /* Compare the two records. */
1137 if (memcmp(kmft, kmirr, bytes)) {
1138 ntfs_error(sb, "$MFT and $MFTMirr (record %i) do not "
1139 "match. Run ntfsfix or chkdsk.", i);
1142 kmft += vol->mft_record_size;
1143 kmirr += vol->mft_record_size;
1144 } while (++i < vol->mftmirr_size);
1145 /* Release the last pages. */
1146 ntfs_unmap_page(mft_page);
1147 ntfs_unmap_page(mirr_page);
1149 /* Construct the mft mirror runlist by hand. */
1151 rl2[0].lcn = vol->mftmirr_lcn;
1152 rl2[0].length = (vol->mftmirr_size * vol->mft_record_size +
1153 vol->cluster_size - 1) / vol->cluster_size;
1154 rl2[1].vcn = rl2[0].length;
1155 rl2[1].lcn = LCN_ENOENT;
1158 * Because we have just read all of the mft mirror, we know we have
1159 * mapped the full runlist for it.
1161 mirr_ni = NTFS_I(vol->mftmirr_ino);
1162 down_read(&mirr_ni->runlist.lock);
1163 rl = mirr_ni->runlist.rl;
1164 /* Compare the two runlists. They must be identical. */
1167 if (rl2[i].vcn != rl[i].vcn || rl2[i].lcn != rl[i].lcn ||
1168 rl2[i].length != rl[i].length) {
1169 ntfs_error(sb, "$MFTMirr location mismatch. "
1171 up_read(&mirr_ni->runlist.lock);
1174 } while (rl2[i++].length);
1175 up_read(&mirr_ni->runlist.lock);
1176 ntfs_debug("Done.");
1181 * load_and_check_logfile - load and check the logfile inode for a volume
1182 * @vol: ntfs super block describing device whose logfile to load
1184 * Return TRUE on success or FALSE on error.
1186 static BOOL load_and_check_logfile(ntfs_volume *vol,
1187 RESTART_PAGE_HEADER **rp)
1189 struct inode *tmp_ino;
1191 ntfs_debug("Entering.");
1192 tmp_ino = ntfs_iget(vol->sb, FILE_LogFile);
1193 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1194 if (!IS_ERR(tmp_ino))
1196 /* Caller will display error message. */
1199 if (!ntfs_check_logfile(tmp_ino, rp)) {
1201 /* ntfs_check_logfile() will have displayed error output. */
1204 NInoSetSparseDisabled(NTFS_I(tmp_ino));
1205 vol->logfile_ino = tmp_ino;
1206 ntfs_debug("Done.");
1210 #define NTFS_HIBERFIL_HEADER_SIZE 4096
1213 * check_windows_hibernation_status - check if Windows is suspended on a volume
1214 * @vol: ntfs super block of device to check
1216 * Check if Windows is hibernated on the ntfs volume @vol. This is done by
1217 * looking for the file hiberfil.sys in the root directory of the volume. If
1218 * the file is not present Windows is definitely not suspended.
1220 * If hiberfil.sys exists and is less than 4kiB in size it means Windows is
1221 * definitely suspended (this volume is not the system volume). Caveat: on a
1222 * system with many volumes it is possible that the < 4kiB check is bogus but
1223 * for now this should do fine.
1225 * If hiberfil.sys exists and is larger than 4kiB in size, we need to read the
1226 * hiberfil header (which is the first 4kiB). If this begins with "hibr",
1227 * Windows is definitely suspended. If it is completely full of zeroes,
1228 * Windows is definitely not hibernated. Any other case is treated as if
1229 * Windows is suspended. This caters for the above mentioned caveat of a
1230 * system with many volumes where no "hibr" magic would be present and there is
1233 * Return 0 if Windows is not hibernated on the volume, >0 if Windows is
1234 * hibernated on the volume, and -errno on error.
1236 static int check_windows_hibernation_status(ntfs_volume *vol)
1243 ntfs_name *name = NULL;
1245 static const ntfschar hiberfil[13] = { const_cpu_to_le16('h'),
1246 const_cpu_to_le16('i'), const_cpu_to_le16('b'),
1247 const_cpu_to_le16('e'), const_cpu_to_le16('r'),
1248 const_cpu_to_le16('f'), const_cpu_to_le16('i'),
1249 const_cpu_to_le16('l'), const_cpu_to_le16('.'),
1250 const_cpu_to_le16('s'), const_cpu_to_le16('y'),
1251 const_cpu_to_le16('s'), 0 };
1253 ntfs_debug("Entering.");
1255 * Find the inode number for the hibernation file by looking up the
1256 * filename hiberfil.sys in the root directory.
1258 mutex_lock(&vol->root_ino->i_mutex);
1259 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->root_ino), hiberfil, 12,
1261 mutex_unlock(&vol->root_ino->i_mutex);
1262 if (IS_ERR_MREF(mref)) {
1263 ret = MREF_ERR(mref);
1264 /* If the file does not exist, Windows is not hibernated. */
1265 if (ret == -ENOENT) {
1266 ntfs_debug("hiberfil.sys not present. Windows is not "
1267 "hibernated on the volume.");
1270 /* A real error occured. */
1271 ntfs_error(vol->sb, "Failed to find inode number for "
1275 /* We do not care for the type of match that was found. */
1277 /* Get the inode. */
1278 vi = ntfs_iget(vol->sb, MREF(mref));
1279 if (IS_ERR(vi) || is_bad_inode(vi)) {
1282 ntfs_error(vol->sb, "Failed to load hiberfil.sys.");
1283 return IS_ERR(vi) ? PTR_ERR(vi) : -EIO;
1285 if (unlikely(i_size_read(vi) < NTFS_HIBERFIL_HEADER_SIZE)) {
1286 ntfs_debug("hiberfil.sys is smaller than 4kiB (0x%llx). "
1287 "Windows is hibernated on the volume. This "
1288 "is not the system volume.", i_size_read(vi));
1292 page = ntfs_map_page(vi->i_mapping, 0);
1294 ntfs_error(vol->sb, "Failed to read from hiberfil.sys.");
1295 ret = PTR_ERR(page);
1298 kaddr = (u32*)page_address(page);
1299 if (*(le32*)kaddr == const_cpu_to_le32(0x72626968)/*'hibr'*/) {
1300 ntfs_debug("Magic \"hibr\" found in hiberfil.sys. Windows is "
1301 "hibernated on the volume. This is the "
1305 kend = kaddr + NTFS_HIBERFIL_HEADER_SIZE/sizeof(*kaddr);
1307 if (unlikely(*kaddr)) {
1308 ntfs_debug("hiberfil.sys is larger than 4kiB "
1309 "(0x%llx), does not contain the "
1310 "\"hibr\" magic, and does not have a "
1311 "zero header. Windows is hibernated "
1312 "on the volume. This is not the "
1313 "system volume.", i_size_read(vi));
1316 } while (++kaddr < kend);
1317 ntfs_debug("hiberfil.sys contains a zero header. Windows is not "
1318 "hibernated on the volume. This is the system "
1322 ntfs_unmap_page(page);
1329 * load_and_init_quota - load and setup the quota file for a volume if present
1330 * @vol: ntfs super block describing device whose quota file to load
1332 * Return TRUE on success or FALSE on error. If $Quota is not present, we
1333 * leave vol->quota_ino as NULL and return success.
1335 static BOOL load_and_init_quota(ntfs_volume *vol)
1338 struct inode *tmp_ino;
1339 ntfs_name *name = NULL;
1340 static const ntfschar Quota[7] = { const_cpu_to_le16('$'),
1341 const_cpu_to_le16('Q'), const_cpu_to_le16('u'),
1342 const_cpu_to_le16('o'), const_cpu_to_le16('t'),
1343 const_cpu_to_le16('a'), 0 };
1344 static ntfschar Q[3] = { const_cpu_to_le16('$'),
1345 const_cpu_to_le16('Q'), 0 };
1347 ntfs_debug("Entering.");
1349 * Find the inode number for the quota file by looking up the filename
1350 * $Quota in the extended system files directory $Extend.
1352 mutex_lock(&vol->extend_ino->i_mutex);
1353 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), Quota, 6,
1355 mutex_unlock(&vol->extend_ino->i_mutex);
1356 if (IS_ERR_MREF(mref)) {
1358 * If the file does not exist, quotas are disabled and have
1359 * never been enabled on this volume, just return success.
1361 if (MREF_ERR(mref) == -ENOENT) {
1362 ntfs_debug("$Quota not present. Volume does not have "
1365 * No need to try to set quotas out of date if they are
1368 NVolSetQuotaOutOfDate(vol);
1371 /* A real error occured. */
1372 ntfs_error(vol->sb, "Failed to find inode number for $Quota.");
1375 /* We do not care for the type of match that was found. */
1377 /* Get the inode. */
1378 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1379 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1380 if (!IS_ERR(tmp_ino))
1382 ntfs_error(vol->sb, "Failed to load $Quota.");
1385 vol->quota_ino = tmp_ino;
1386 /* Get the $Q index allocation attribute. */
1387 tmp_ino = ntfs_index_iget(vol->quota_ino, Q, 2);
1388 if (IS_ERR(tmp_ino)) {
1389 ntfs_error(vol->sb, "Failed to load $Quota/$Q index.");
1392 vol->quota_q_ino = tmp_ino;
1393 ntfs_debug("Done.");
1398 * load_and_init_usnjrnl - load and setup the transaction log if present
1399 * @vol: ntfs super block describing device whose usnjrnl file to load
1401 * Return TRUE on success or FALSE on error.
1403 * If $UsnJrnl is not present or in the process of being disabled, we set
1404 * NVolUsnJrnlStamped() and return success.
1406 * If the $UsnJrnl $DATA/$J attribute has a size equal to the lowest valid usn,
1407 * i.e. transaction logging has only just been enabled or the journal has been
1408 * stamped and nothing has been logged since, we also set NVolUsnJrnlStamped()
1409 * and return success.
1411 static BOOL load_and_init_usnjrnl(ntfs_volume *vol)
1414 struct inode *tmp_ino;
1417 ntfs_name *name = NULL;
1419 static const ntfschar UsnJrnl[9] = { const_cpu_to_le16('$'),
1420 const_cpu_to_le16('U'), const_cpu_to_le16('s'),
1421 const_cpu_to_le16('n'), const_cpu_to_le16('J'),
1422 const_cpu_to_le16('r'), const_cpu_to_le16('n'),
1423 const_cpu_to_le16('l'), 0 };
1424 static ntfschar Max[5] = { const_cpu_to_le16('$'),
1425 const_cpu_to_le16('M'), const_cpu_to_le16('a'),
1426 const_cpu_to_le16('x'), 0 };
1427 static ntfschar J[3] = { const_cpu_to_le16('$'),
1428 const_cpu_to_le16('J'), 0 };
1430 ntfs_debug("Entering.");
1432 * Find the inode number for the transaction log file by looking up the
1433 * filename $UsnJrnl in the extended system files directory $Extend.
1435 mutex_lock(&vol->extend_ino->i_mutex);
1436 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), UsnJrnl, 8,
1438 mutex_unlock(&vol->extend_ino->i_mutex);
1439 if (IS_ERR_MREF(mref)) {
1441 * If the file does not exist, transaction logging is disabled,
1442 * just return success.
1444 if (MREF_ERR(mref) == -ENOENT) {
1445 ntfs_debug("$UsnJrnl not present. Volume does not "
1446 "have transaction logging enabled.");
1449 * No need to try to stamp the transaction log if
1450 * transaction logging is not enabled.
1452 NVolSetUsnJrnlStamped(vol);
1455 /* A real error occured. */
1456 ntfs_error(vol->sb, "Failed to find inode number for "
1460 /* We do not care for the type of match that was found. */
1462 /* Get the inode. */
1463 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1464 if (unlikely(IS_ERR(tmp_ino) || is_bad_inode(tmp_ino))) {
1465 if (!IS_ERR(tmp_ino))
1467 ntfs_error(vol->sb, "Failed to load $UsnJrnl.");
1470 vol->usnjrnl_ino = tmp_ino;
1472 * If the transaction log is in the process of being deleted, we can
1475 if (unlikely(vol->vol_flags & VOLUME_DELETE_USN_UNDERWAY)) {
1476 ntfs_debug("$UsnJrnl in the process of being disabled. "
1477 "Volume does not have transaction logging "
1481 /* Get the $DATA/$Max attribute. */
1482 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, Max, 4);
1483 if (IS_ERR(tmp_ino)) {
1484 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$Max "
1488 vol->usnjrnl_max_ino = tmp_ino;
1489 if (unlikely(i_size_read(tmp_ino) < sizeof(USN_HEADER))) {
1490 ntfs_error(vol->sb, "Found corrupt $UsnJrnl/$DATA/$Max "
1491 "attribute (size is 0x%llx but should be at "
1492 "least 0x%zx bytes).", i_size_read(tmp_ino),
1493 sizeof(USN_HEADER));
1496 /* Get the $DATA/$J attribute. */
1497 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, J, 2);
1498 if (IS_ERR(tmp_ino)) {
1499 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$J "
1503 vol->usnjrnl_j_ino = tmp_ino;
1504 /* Verify $J is non-resident and sparse. */
1505 tmp_ni = NTFS_I(vol->usnjrnl_j_ino);
1506 if (unlikely(!NInoNonResident(tmp_ni) || !NInoSparse(tmp_ni))) {
1507 ntfs_error(vol->sb, "$UsnJrnl/$DATA/$J attribute is resident "
1508 "and/or not sparse.");
1511 /* Read the USN_HEADER from $DATA/$Max. */
1512 page = ntfs_map_page(vol->usnjrnl_max_ino->i_mapping, 0);
1514 ntfs_error(vol->sb, "Failed to read from $UsnJrnl/$DATA/$Max "
1518 uh = (USN_HEADER*)page_address(page);
1519 /* Sanity check the $Max. */
1520 if (unlikely(sle64_to_cpu(uh->allocation_delta) >
1521 sle64_to_cpu(uh->maximum_size))) {
1522 ntfs_error(vol->sb, "Allocation delta (0x%llx) exceeds "
1523 "maximum size (0x%llx). $UsnJrnl is corrupt.",
1524 (long long)sle64_to_cpu(uh->allocation_delta),
1525 (long long)sle64_to_cpu(uh->maximum_size));
1526 ntfs_unmap_page(page);
1530 * If the transaction log has been stamped and nothing has been written
1531 * to it since, we do not need to stamp it.
1533 if (unlikely(sle64_to_cpu(uh->lowest_valid_usn) >=
1534 i_size_read(vol->usnjrnl_j_ino))) {
1535 if (likely(sle64_to_cpu(uh->lowest_valid_usn) ==
1536 i_size_read(vol->usnjrnl_j_ino))) {
1537 ntfs_unmap_page(page);
1538 ntfs_debug("$UsnJrnl is enabled but nothing has been "
1539 "logged since it was last stamped. "
1540 "Treating this as if the volume does "
1541 "not have transaction logging "
1545 ntfs_error(vol->sb, "$UsnJrnl has lowest valid usn (0x%llx) "
1546 "which is out of bounds (0x%llx). $UsnJrnl "
1548 (long long)sle64_to_cpu(uh->lowest_valid_usn),
1549 i_size_read(vol->usnjrnl_j_ino));
1550 ntfs_unmap_page(page);
1553 ntfs_unmap_page(page);
1554 ntfs_debug("Done.");
1559 * load_and_init_attrdef - load the attribute definitions table for a volume
1560 * @vol: ntfs super block describing device whose attrdef to load
1562 * Return TRUE on success or FALSE on error.
1564 static BOOL load_and_init_attrdef(ntfs_volume *vol)
1567 struct super_block *sb = vol->sb;
1570 pgoff_t index, max_index;
1573 ntfs_debug("Entering.");
1574 /* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */
1575 ino = ntfs_iget(sb, FILE_AttrDef);
1576 if (IS_ERR(ino) || is_bad_inode(ino)) {
1581 NInoSetSparseDisabled(NTFS_I(ino));
1582 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
1583 i_size = i_size_read(ino);
1584 if (i_size <= 0 || i_size > 0x7fffffff)
1586 vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(i_size);
1590 max_index = i_size >> PAGE_CACHE_SHIFT;
1591 size = PAGE_CACHE_SIZE;
1592 while (index < max_index) {
1593 /* Read the attrdef table and copy it into the linear buffer. */
1594 read_partial_attrdef_page:
1595 page = ntfs_map_page(ino->i_mapping, index);
1597 goto free_iput_failed;
1598 memcpy((u8*)vol->attrdef + (index++ << PAGE_CACHE_SHIFT),
1599 page_address(page), size);
1600 ntfs_unmap_page(page);
1602 if (size == PAGE_CACHE_SIZE) {
1603 size = i_size & ~PAGE_CACHE_MASK;
1605 goto read_partial_attrdef_page;
1607 vol->attrdef_size = i_size;
1608 ntfs_debug("Read %llu bytes from $AttrDef.", i_size);
1612 ntfs_free(vol->attrdef);
1613 vol->attrdef = NULL;
1617 ntfs_error(sb, "Failed to initialize attribute definition table.");
1621 #endif /* NTFS_RW */
1624 * load_and_init_upcase - load the upcase table for an ntfs volume
1625 * @vol: ntfs super block describing device whose upcase to load
1627 * Return TRUE on success or FALSE on error.
1629 static BOOL load_and_init_upcase(ntfs_volume *vol)
1632 struct super_block *sb = vol->sb;
1635 pgoff_t index, max_index;
1639 ntfs_debug("Entering.");
1640 /* Read upcase table and setup vol->upcase and vol->upcase_len. */
1641 ino = ntfs_iget(sb, FILE_UpCase);
1642 if (IS_ERR(ino) || is_bad_inode(ino)) {
1648 * The upcase size must not be above 64k Unicode characters, must not
1649 * be zero and must be a multiple of sizeof(ntfschar).
1651 i_size = i_size_read(ino);
1652 if (!i_size || i_size & (sizeof(ntfschar) - 1) ||
1653 i_size > 64ULL * 1024 * sizeof(ntfschar))
1654 goto iput_upcase_failed;
1655 vol->upcase = (ntfschar*)ntfs_malloc_nofs(i_size);
1657 goto iput_upcase_failed;
1659 max_index = i_size >> PAGE_CACHE_SHIFT;
1660 size = PAGE_CACHE_SIZE;
1661 while (index < max_index) {
1662 /* Read the upcase table and copy it into the linear buffer. */
1663 read_partial_upcase_page:
1664 page = ntfs_map_page(ino->i_mapping, index);
1666 goto iput_upcase_failed;
1667 memcpy((char*)vol->upcase + (index++ << PAGE_CACHE_SHIFT),
1668 page_address(page), size);
1669 ntfs_unmap_page(page);
1671 if (size == PAGE_CACHE_SIZE) {
1672 size = i_size & ~PAGE_CACHE_MASK;
1674 goto read_partial_upcase_page;
1676 vol->upcase_len = i_size >> UCHAR_T_SIZE_BITS;
1677 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
1678 i_size, 64 * 1024 * sizeof(ntfschar));
1680 mutex_lock(&ntfs_lock);
1681 if (!default_upcase) {
1682 ntfs_debug("Using volume specified $UpCase since default is "
1684 mutex_unlock(&ntfs_lock);
1687 max = default_upcase_len;
1688 if (max > vol->upcase_len)
1689 max = vol->upcase_len;
1690 for (i = 0; i < max; i++)
1691 if (vol->upcase[i] != default_upcase[i])
1694 ntfs_free(vol->upcase);
1695 vol->upcase = default_upcase;
1696 vol->upcase_len = max;
1697 ntfs_nr_upcase_users++;
1698 mutex_unlock(&ntfs_lock);
1699 ntfs_debug("Volume specified $UpCase matches default. Using "
1703 mutex_unlock(&ntfs_lock);
1704 ntfs_debug("Using volume specified $UpCase since it does not match "
1709 ntfs_free(vol->upcase);
1712 mutex_lock(&ntfs_lock);
1713 if (default_upcase) {
1714 vol->upcase = default_upcase;
1715 vol->upcase_len = default_upcase_len;
1716 ntfs_nr_upcase_users++;
1717 mutex_unlock(&ntfs_lock);
1718 ntfs_error(sb, "Failed to load $UpCase from the volume. Using "
1722 mutex_unlock(&ntfs_lock);
1723 ntfs_error(sb, "Failed to initialize upcase table.");
1728 * load_system_files - open the system files using normal functions
1729 * @vol: ntfs super block describing device whose system files to load
1731 * Open the system files with normal access functions and complete setting up
1732 * the ntfs super block @vol.
1734 * Return TRUE on success or FALSE on error.
1736 static BOOL load_system_files(ntfs_volume *vol)
1738 struct super_block *sb = vol->sb;
1740 VOLUME_INFORMATION *vi;
1741 ntfs_attr_search_ctx *ctx;
1743 RESTART_PAGE_HEADER *rp;
1745 #endif /* NTFS_RW */
1747 ntfs_debug("Entering.");
1749 /* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */
1750 if (!load_and_init_mft_mirror(vol) || !check_mft_mirror(vol)) {
1751 static const char *es1 = "Failed to load $MFTMirr";
1752 static const char *es2 = "$MFTMirr does not match $MFT";
1753 static const char *es3 = ". Run ntfsfix and/or chkdsk.";
1755 /* If a read-write mount, convert it to a read-only mount. */
1756 if (!(sb->s_flags & MS_RDONLY)) {
1757 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1758 ON_ERRORS_CONTINUE))) {
1759 ntfs_error(sb, "%s and neither on_errors="
1760 "continue nor on_errors="
1761 "remount-ro was specified%s",
1762 !vol->mftmirr_ino ? es1 : es2,
1764 goto iput_mirr_err_out;
1766 sb->s_flags |= MS_RDONLY;
1767 ntfs_error(sb, "%s. Mounting read-only%s",
1768 !vol->mftmirr_ino ? es1 : es2, es3);
1770 ntfs_warning(sb, "%s. Will not be able to remount "
1772 !vol->mftmirr_ino ? es1 : es2, es3);
1773 /* This will prevent a read-write remount. */
1776 #endif /* NTFS_RW */
1777 /* Get mft bitmap attribute inode. */
1778 vol->mftbmp_ino = ntfs_attr_iget(vol->mft_ino, AT_BITMAP, NULL, 0);
1779 if (IS_ERR(vol->mftbmp_ino)) {
1780 ntfs_error(sb, "Failed to load $MFT/$BITMAP attribute.");
1781 goto iput_mirr_err_out;
1783 /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */
1784 if (!load_and_init_upcase(vol))
1785 goto iput_mftbmp_err_out;
1788 * Read attribute definitions table and setup @vol->attrdef and
1789 * @vol->attrdef_size.
1791 if (!load_and_init_attrdef(vol))
1792 goto iput_upcase_err_out;
1793 #endif /* NTFS_RW */
1795 * Get the cluster allocation bitmap inode and verify the size, no
1796 * need for any locking at this stage as we are already running
1797 * exclusively as we are mount in progress task.
1799 vol->lcnbmp_ino = ntfs_iget(sb, FILE_Bitmap);
1800 if (IS_ERR(vol->lcnbmp_ino) || is_bad_inode(vol->lcnbmp_ino)) {
1801 if (!IS_ERR(vol->lcnbmp_ino))
1802 iput(vol->lcnbmp_ino);
1805 NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino));
1806 if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) {
1807 iput(vol->lcnbmp_ino);
1809 ntfs_error(sb, "Failed to load $Bitmap.");
1810 goto iput_attrdef_err_out;
1813 * Get the volume inode and setup our cache of the volume flags and
1816 vol->vol_ino = ntfs_iget(sb, FILE_Volume);
1817 if (IS_ERR(vol->vol_ino) || is_bad_inode(vol->vol_ino)) {
1818 if (!IS_ERR(vol->vol_ino))
1821 ntfs_error(sb, "Failed to load $Volume.");
1822 goto iput_lcnbmp_err_out;
1824 m = map_mft_record(NTFS_I(vol->vol_ino));
1830 if (!(ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m))) {
1831 ntfs_error(sb, "Failed to get attribute search context.");
1832 goto get_ctx_vol_failed;
1834 if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
1835 ctx) || ctx->attr->non_resident || ctx->attr->flags) {
1837 ntfs_attr_put_search_ctx(ctx);
1839 unmap_mft_record(NTFS_I(vol->vol_ino));
1840 goto iput_volume_failed;
1842 vi = (VOLUME_INFORMATION*)((char*)ctx->attr +
1843 le16_to_cpu(ctx->attr->data.resident.value_offset));
1844 /* Some bounds checks. */
1845 if ((u8*)vi < (u8*)ctx->attr || (u8*)vi +
1846 le32_to_cpu(ctx->attr->data.resident.value_length) >
1847 (u8*)ctx->attr + le32_to_cpu(ctx->attr->length))
1849 /* Copy the volume flags and version to the ntfs_volume structure. */
1850 vol->vol_flags = vi->flags;
1851 vol->major_ver = vi->major_ver;
1852 vol->minor_ver = vi->minor_ver;
1853 ntfs_attr_put_search_ctx(ctx);
1854 unmap_mft_record(NTFS_I(vol->vol_ino));
1855 printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver,
1857 if (vol->major_ver < 3 && NVolSparseEnabled(vol)) {
1858 ntfs_warning(vol->sb, "Disabling sparse support due to NTFS "
1859 "volume version %i.%i (need at least version "
1860 "3.0).", vol->major_ver, vol->minor_ver);
1861 NVolClearSparseEnabled(vol);
1864 /* Make sure that no unsupported volume flags are set. */
1865 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
1866 static const char *es1a = "Volume is dirty";
1867 static const char *es1b = "Volume has been modified by chkdsk";
1868 static const char *es1c = "Volume has unsupported flags set";
1869 static const char *es2a = ". Run chkdsk and mount in Windows.";
1870 static const char *es2b = ". Mount in Windows.";
1871 const char *es1, *es2;
1874 if (vol->vol_flags & VOLUME_IS_DIRTY)
1876 else if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
1881 ntfs_warning(sb, "Unsupported volume flags 0x%x "
1883 (unsigned)le16_to_cpu(vol->vol_flags));
1885 /* If a read-write mount, convert it to a read-only mount. */
1886 if (!(sb->s_flags & MS_RDONLY)) {
1887 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1888 ON_ERRORS_CONTINUE))) {
1889 ntfs_error(sb, "%s and neither on_errors="
1890 "continue nor on_errors="
1891 "remount-ro was specified%s",
1893 goto iput_vol_err_out;
1895 sb->s_flags |= MS_RDONLY;
1896 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1898 ntfs_warning(sb, "%s. Will not be able to remount "
1899 "read-write%s", es1, es2);
1901 * Do not set NVolErrors() because ntfs_remount() re-checks the
1902 * flags which we need to do in case any flags have changed.
1906 * Get the inode for the logfile, check it and determine if the volume
1907 * was shutdown cleanly.
1910 if (!load_and_check_logfile(vol, &rp) ||
1911 !ntfs_is_logfile_clean(vol->logfile_ino, rp)) {
1912 static const char *es1a = "Failed to load $LogFile";
1913 static const char *es1b = "$LogFile is not clean";
1914 static const char *es2 = ". Mount in Windows.";
1917 es1 = !vol->logfile_ino ? es1a : es1b;
1918 /* If a read-write mount, convert it to a read-only mount. */
1919 if (!(sb->s_flags & MS_RDONLY)) {
1920 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1921 ON_ERRORS_CONTINUE))) {
1922 ntfs_error(sb, "%s and neither on_errors="
1923 "continue nor on_errors="
1924 "remount-ro was specified%s",
1926 if (vol->logfile_ino) {
1930 goto iput_logfile_err_out;
1932 sb->s_flags |= MS_RDONLY;
1933 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1935 ntfs_warning(sb, "%s. Will not be able to remount "
1936 "read-write%s", es1, es2);
1937 /* This will prevent a read-write remount. */
1941 #endif /* NTFS_RW */
1942 /* Get the root directory inode so we can do path lookups. */
1943 vol->root_ino = ntfs_iget(sb, FILE_root);
1944 if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) {
1945 if (!IS_ERR(vol->root_ino))
1946 iput(vol->root_ino);
1947 ntfs_error(sb, "Failed to load root directory.");
1948 goto iput_logfile_err_out;
1952 * Check if Windows is suspended to disk on the target volume. If it
1953 * is hibernated, we must not write *anything* to the disk so set
1954 * NVolErrors() without setting the dirty volume flag and mount
1955 * read-only. This will prevent read-write remounting and it will also
1956 * prevent all writes.
1958 err = check_windows_hibernation_status(vol);
1959 if (unlikely(err)) {
1960 static const char *es1a = "Failed to determine if Windows is "
1962 static const char *es1b = "Windows is hibernated";
1963 static const char *es2 = ". Run chkdsk.";
1966 es1 = err < 0 ? es1a : es1b;
1967 /* If a read-write mount, convert it to a read-only mount. */
1968 if (!(sb->s_flags & MS_RDONLY)) {
1969 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1970 ON_ERRORS_CONTINUE))) {
1971 ntfs_error(sb, "%s and neither on_errors="
1972 "continue nor on_errors="
1973 "remount-ro was specified%s",
1975 goto iput_root_err_out;
1977 sb->s_flags |= MS_RDONLY;
1978 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1980 ntfs_warning(sb, "%s. Will not be able to remount "
1981 "read-write%s", es1, es2);
1982 /* This will prevent a read-write remount. */
1985 /* If (still) a read-write mount, mark the volume dirty. */
1986 if (!(sb->s_flags & MS_RDONLY) &&
1987 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
1988 static const char *es1 = "Failed to set dirty bit in volume "
1989 "information flags";
1990 static const char *es2 = ". Run chkdsk.";
1992 /* Convert to a read-only mount. */
1993 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1994 ON_ERRORS_CONTINUE))) {
1995 ntfs_error(sb, "%s and neither on_errors=continue nor "
1996 "on_errors=remount-ro was specified%s",
1998 goto iput_root_err_out;
2000 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2001 sb->s_flags |= MS_RDONLY;
2003 * Do not set NVolErrors() because ntfs_remount() might manage
2004 * to set the dirty flag in which case all would be well.
2008 // TODO: Enable this code once we start modifying anything that is
2009 // different between NTFS 1.2 and 3.x...
2011 * If (still) a read-write mount, set the NT4 compatibility flag on
2012 * newer NTFS version volumes.
2014 if (!(sb->s_flags & MS_RDONLY) && (vol->major_ver > 1) &&
2015 ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
2016 static const char *es1 = "Failed to set NT4 compatibility flag";
2017 static const char *es2 = ". Run chkdsk.";
2019 /* Convert to a read-only mount. */
2020 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2021 ON_ERRORS_CONTINUE))) {
2022 ntfs_error(sb, "%s and neither on_errors=continue nor "
2023 "on_errors=remount-ro was specified%s",
2025 goto iput_root_err_out;
2027 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2028 sb->s_flags |= MS_RDONLY;
2032 /* If (still) a read-write mount, empty the logfile. */
2033 if (!(sb->s_flags & MS_RDONLY) &&
2034 !ntfs_empty_logfile(vol->logfile_ino)) {
2035 static const char *es1 = "Failed to empty $LogFile";
2036 static const char *es2 = ". Mount in Windows.";
2038 /* Convert to a read-only mount. */
2039 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2040 ON_ERRORS_CONTINUE))) {
2041 ntfs_error(sb, "%s and neither on_errors=continue nor "
2042 "on_errors=remount-ro was specified%s",
2044 goto iput_root_err_out;
2046 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2047 sb->s_flags |= MS_RDONLY;
2050 #endif /* NTFS_RW */
2051 /* If on NTFS versions before 3.0, we are done. */
2052 if (unlikely(vol->major_ver < 3))
2054 /* NTFS 3.0+ specific initialization. */
2055 /* Get the security descriptors inode. */
2056 vol->secure_ino = ntfs_iget(sb, FILE_Secure);
2057 if (IS_ERR(vol->secure_ino) || is_bad_inode(vol->secure_ino)) {
2058 if (!IS_ERR(vol->secure_ino))
2059 iput(vol->secure_ino);
2060 ntfs_error(sb, "Failed to load $Secure.");
2061 goto iput_root_err_out;
2063 // TODO: Initialize security.
2064 /* Get the extended system files' directory inode. */
2065 vol->extend_ino = ntfs_iget(sb, FILE_Extend);
2066 if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) {
2067 if (!IS_ERR(vol->extend_ino))
2068 iput(vol->extend_ino);
2069 ntfs_error(sb, "Failed to load $Extend.");
2070 goto iput_sec_err_out;
2073 /* Find the quota file, load it if present, and set it up. */
2074 if (!load_and_init_quota(vol)) {
2075 static const char *es1 = "Failed to load $Quota";
2076 static const char *es2 = ". Run chkdsk.";
2078 /* If a read-write mount, convert it to a read-only mount. */
2079 if (!(sb->s_flags & MS_RDONLY)) {
2080 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2081 ON_ERRORS_CONTINUE))) {
2082 ntfs_error(sb, "%s and neither on_errors="
2083 "continue nor on_errors="
2084 "remount-ro was specified%s",
2086 goto iput_quota_err_out;
2088 sb->s_flags |= MS_RDONLY;
2089 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2091 ntfs_warning(sb, "%s. Will not be able to remount "
2092 "read-write%s", es1, es2);
2093 /* This will prevent a read-write remount. */
2096 /* If (still) a read-write mount, mark the quotas out of date. */
2097 if (!(sb->s_flags & MS_RDONLY) &&
2098 !ntfs_mark_quotas_out_of_date(vol)) {
2099 static const char *es1 = "Failed to mark quotas out of date";
2100 static const char *es2 = ". Run chkdsk.";
2102 /* Convert to a read-only mount. */
2103 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2104 ON_ERRORS_CONTINUE))) {
2105 ntfs_error(sb, "%s and neither on_errors=continue nor "
2106 "on_errors=remount-ro was specified%s",
2108 goto iput_quota_err_out;
2110 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2111 sb->s_flags |= MS_RDONLY;
2115 * Find the transaction log file ($UsnJrnl), load it if present, check
2116 * it, and set it up.
2118 if (!load_and_init_usnjrnl(vol)) {
2119 static const char *es1 = "Failed to load $UsnJrnl";
2120 static const char *es2 = ". Run chkdsk.";
2122 /* If a read-write mount, convert it to a read-only mount. */
2123 if (!(sb->s_flags & MS_RDONLY)) {
2124 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2125 ON_ERRORS_CONTINUE))) {
2126 ntfs_error(sb, "%s and neither on_errors="
2127 "continue nor on_errors="
2128 "remount-ro was specified%s",
2130 goto iput_usnjrnl_err_out;
2132 sb->s_flags |= MS_RDONLY;
2133 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2135 ntfs_warning(sb, "%s. Will not be able to remount "
2136 "read-write%s", es1, es2);
2137 /* This will prevent a read-write remount. */
2140 /* If (still) a read-write mount, stamp the transaction log. */
2141 if (!(sb->s_flags & MS_RDONLY) && !ntfs_stamp_usnjrnl(vol)) {
2142 static const char *es1 = "Failed to stamp transaction log "
2144 static const char *es2 = ". Run chkdsk.";
2146 /* Convert to a read-only mount. */
2147 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2148 ON_ERRORS_CONTINUE))) {
2149 ntfs_error(sb, "%s and neither on_errors=continue nor "
2150 "on_errors=remount-ro was specified%s",
2152 goto iput_usnjrnl_err_out;
2154 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2155 sb->s_flags |= MS_RDONLY;
2158 #endif /* NTFS_RW */
2161 iput_usnjrnl_err_out:
2162 if (vol->usnjrnl_j_ino)
2163 iput(vol->usnjrnl_j_ino);
2164 if (vol->usnjrnl_max_ino)
2165 iput(vol->usnjrnl_max_ino);
2166 if (vol->usnjrnl_ino)
2167 iput(vol->usnjrnl_ino);
2169 if (vol->quota_q_ino)
2170 iput(vol->quota_q_ino);
2172 iput(vol->quota_ino);
2173 iput(vol->extend_ino);
2174 #endif /* NTFS_RW */
2176 iput(vol->secure_ino);
2178 iput(vol->root_ino);
2179 iput_logfile_err_out:
2181 if (vol->logfile_ino)
2182 iput(vol->logfile_ino);
2184 #endif /* NTFS_RW */
2186 iput_lcnbmp_err_out:
2187 iput(vol->lcnbmp_ino);
2188 iput_attrdef_err_out:
2189 vol->attrdef_size = 0;
2191 ntfs_free(vol->attrdef);
2192 vol->attrdef = NULL;
2195 iput_upcase_err_out:
2196 #endif /* NTFS_RW */
2197 vol->upcase_len = 0;
2198 mutex_lock(&ntfs_lock);
2199 if (vol->upcase == default_upcase) {
2200 ntfs_nr_upcase_users--;
2203 mutex_unlock(&ntfs_lock);
2205 ntfs_free(vol->upcase);
2208 iput_mftbmp_err_out:
2209 iput(vol->mftbmp_ino);
2212 if (vol->mftmirr_ino)
2213 iput(vol->mftmirr_ino);
2214 #endif /* NTFS_RW */
2219 * ntfs_put_super - called by the vfs to unmount a volume
2220 * @sb: vfs superblock of volume to unmount
2222 * ntfs_put_super() is called by the VFS (from fs/super.c::do_umount()) when
2223 * the volume is being unmounted (umount system call has been invoked) and it
2224 * releases all inodes and memory belonging to the NTFS specific part of the
2227 static void ntfs_put_super(struct super_block *sb)
2229 ntfs_volume *vol = NTFS_SB(sb);
2231 ntfs_debug("Entering.");
2234 * Commit all inodes while they are still open in case some of them
2235 * cause others to be dirtied.
2237 ntfs_commit_inode(vol->vol_ino);
2239 /* NTFS 3.0+ specific. */
2240 if (vol->major_ver >= 3) {
2241 if (vol->usnjrnl_j_ino)
2242 ntfs_commit_inode(vol->usnjrnl_j_ino);
2243 if (vol->usnjrnl_max_ino)
2244 ntfs_commit_inode(vol->usnjrnl_max_ino);
2245 if (vol->usnjrnl_ino)
2246 ntfs_commit_inode(vol->usnjrnl_ino);
2247 if (vol->quota_q_ino)
2248 ntfs_commit_inode(vol->quota_q_ino);
2250 ntfs_commit_inode(vol->quota_ino);
2251 if (vol->extend_ino)
2252 ntfs_commit_inode(vol->extend_ino);
2253 if (vol->secure_ino)
2254 ntfs_commit_inode(vol->secure_ino);
2257 ntfs_commit_inode(vol->root_ino);
2259 down_write(&vol->lcnbmp_lock);
2260 ntfs_commit_inode(vol->lcnbmp_ino);
2261 up_write(&vol->lcnbmp_lock);
2263 down_write(&vol->mftbmp_lock);
2264 ntfs_commit_inode(vol->mftbmp_ino);
2265 up_write(&vol->mftbmp_lock);
2267 if (vol->logfile_ino)
2268 ntfs_commit_inode(vol->logfile_ino);
2270 if (vol->mftmirr_ino)
2271 ntfs_commit_inode(vol->mftmirr_ino);
2272 ntfs_commit_inode(vol->mft_ino);
2275 * If a read-write mount and no volume errors have occured, mark the
2276 * volume clean. Also, re-commit all affected inodes.
2278 if (!(sb->s_flags & MS_RDONLY)) {
2279 if (!NVolErrors(vol)) {
2280 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
2281 ntfs_warning(sb, "Failed to clear dirty bit "
2282 "in volume information "
2283 "flags. Run chkdsk.");
2284 ntfs_commit_inode(vol->vol_ino);
2285 ntfs_commit_inode(vol->root_ino);
2286 if (vol->mftmirr_ino)
2287 ntfs_commit_inode(vol->mftmirr_ino);
2288 ntfs_commit_inode(vol->mft_ino);
2290 ntfs_warning(sb, "Volume has errors. Leaving volume "
2291 "marked dirty. Run chkdsk.");
2294 #endif /* NTFS_RW */
2297 vol->vol_ino = NULL;
2299 /* NTFS 3.0+ specific clean up. */
2300 if (vol->major_ver >= 3) {
2302 if (vol->usnjrnl_j_ino) {
2303 iput(vol->usnjrnl_j_ino);
2304 vol->usnjrnl_j_ino = NULL;
2306 if (vol->usnjrnl_max_ino) {
2307 iput(vol->usnjrnl_max_ino);
2308 vol->usnjrnl_max_ino = NULL;
2310 if (vol->usnjrnl_ino) {
2311 iput(vol->usnjrnl_ino);
2312 vol->usnjrnl_ino = NULL;
2314 if (vol->quota_q_ino) {
2315 iput(vol->quota_q_ino);
2316 vol->quota_q_ino = NULL;
2318 if (vol->quota_ino) {
2319 iput(vol->quota_ino);
2320 vol->quota_ino = NULL;
2322 #endif /* NTFS_RW */
2323 if (vol->extend_ino) {
2324 iput(vol->extend_ino);
2325 vol->extend_ino = NULL;
2327 if (vol->secure_ino) {
2328 iput(vol->secure_ino);
2329 vol->secure_ino = NULL;
2333 iput(vol->root_ino);
2334 vol->root_ino = NULL;
2336 down_write(&vol->lcnbmp_lock);
2337 iput(vol->lcnbmp_ino);
2338 vol->lcnbmp_ino = NULL;
2339 up_write(&vol->lcnbmp_lock);
2341 down_write(&vol->mftbmp_lock);
2342 iput(vol->mftbmp_ino);
2343 vol->mftbmp_ino = NULL;
2344 up_write(&vol->mftbmp_lock);
2347 if (vol->logfile_ino) {
2348 iput(vol->logfile_ino);
2349 vol->logfile_ino = NULL;
2351 if (vol->mftmirr_ino) {
2352 /* Re-commit the mft mirror and mft just in case. */
2353 ntfs_commit_inode(vol->mftmirr_ino);
2354 ntfs_commit_inode(vol->mft_ino);
2355 iput(vol->mftmirr_ino);
2356 vol->mftmirr_ino = NULL;
2359 * If any dirty inodes are left, throw away all mft data page cache
2360 * pages to allow a clean umount. This should never happen any more
2361 * due to mft.c::ntfs_mft_writepage() cleaning all the dirty pages as
2362 * the underlying mft records are written out and cleaned. If it does,
2363 * happen anyway, we want to know...
2365 ntfs_commit_inode(vol->mft_ino);
2366 write_inode_now(vol->mft_ino, 1);
2367 if (!list_empty(&sb->s_dirty)) {
2368 const char *s1, *s2;
2370 mutex_lock(&vol->mft_ino->i_mutex);
2371 truncate_inode_pages(vol->mft_ino->i_mapping, 0);
2372 mutex_unlock(&vol->mft_ino->i_mutex);
2373 write_inode_now(vol->mft_ino, 1);
2374 if (!list_empty(&sb->s_dirty)) {
2375 static const char *_s1 = "inodes";
2376 static const char *_s2 = "";
2380 static const char *_s1 = "mft pages";
2381 static const char *_s2 = "They have been thrown "
2386 ntfs_error(sb, "Dirty %s found at umount time. %sYou should "
2387 "run chkdsk. Please email "
2388 "linux-ntfs-dev@lists.sourceforge.net and say "
2389 "that you saw this message. Thank you.", s1,
2392 #endif /* NTFS_RW */
2395 vol->mft_ino = NULL;
2397 /* Throw away the table of attribute definitions. */
2398 vol->attrdef_size = 0;
2400 ntfs_free(vol->attrdef);
2401 vol->attrdef = NULL;
2403 vol->upcase_len = 0;
2405 * Destroy the global default upcase table if necessary. Also decrease
2406 * the number of upcase users if we are a user.
2408 mutex_lock(&ntfs_lock);
2409 if (vol->upcase == default_upcase) {
2410 ntfs_nr_upcase_users--;
2413 if (!ntfs_nr_upcase_users && default_upcase) {
2414 ntfs_free(default_upcase);
2415 default_upcase = NULL;
2417 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
2418 free_compression_buffers();
2419 mutex_unlock(&ntfs_lock);
2421 ntfs_free(vol->upcase);
2425 unload_nls(vol->nls_map);
2426 vol->nls_map = NULL;
2428 sb->s_fs_info = NULL;
2434 * get_nr_free_clusters - return the number of free clusters on a volume
2435 * @vol: ntfs volume for which to obtain free cluster count
2437 * Calculate the number of free clusters on the mounted NTFS volume @vol. We
2438 * actually calculate the number of clusters in use instead because this
2439 * allows us to not care about partial pages as these will be just zero filled
2440 * and hence not be counted as allocated clusters.
2442 * The only particularity is that clusters beyond the end of the logical ntfs
2443 * volume will be marked as allocated to prevent errors which means we have to
2444 * discount those at the end. This is important as the cluster bitmap always
2445 * has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside
2446 * the logical volume and marked in use when they are not as they do not exist.
2448 * If any pages cannot be read we assume all clusters in the erroring pages are
2449 * in use. This means we return an underestimate on errors which is better than
2452 static s64 get_nr_free_clusters(ntfs_volume *vol)
2454 s64 nr_free = vol->nr_clusters;
2456 struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
2457 filler_t *readpage = (filler_t*)mapping->a_ops->readpage;
2459 pgoff_t index, max_index;
2461 ntfs_debug("Entering.");
2462 /* Serialize accesses to the cluster bitmap. */
2463 down_read(&vol->lcnbmp_lock);
2465 * Convert the number of bits into bytes rounded up, then convert into
2466 * multiples of PAGE_CACHE_SIZE, rounding up so that if we have one
2467 * full and one partial page max_index = 2.
2469 max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >>
2471 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2472 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.",
2473 max_index, PAGE_CACHE_SIZE / 4);
2474 for (index = 0; index < max_index; index++) {
2477 * Read the page from page cache, getting it from backing store
2478 * if necessary, and increment the use count.
2480 page = read_cache_page(mapping, index, (filler_t*)readpage,
2482 /* Ignore pages which errored synchronously. */
2484 ntfs_debug("Sync read_cache_page() error. Skipping "
2485 "page (index 0x%lx).", index);
2486 nr_free -= PAGE_CACHE_SIZE * 8;
2489 wait_on_page_locked(page);
2490 /* Ignore pages which errored asynchronously. */
2491 if (!PageUptodate(page)) {
2492 ntfs_debug("Async read_cache_page() error. Skipping "
2493 "page (index 0x%lx).", index);
2494 page_cache_release(page);
2495 nr_free -= PAGE_CACHE_SIZE * 8;
2498 kaddr = (u32*)kmap_atomic(page, KM_USER0);
2500 * For each 4 bytes, subtract the number of set bits. If this
2501 * is the last page and it is partial we don't really care as
2502 * it just means we do a little extra work but it won't affect
2503 * the result as all out of range bytes are set to zero by
2506 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
2507 nr_free -= (s64)hweight32(kaddr[i]);
2508 kunmap_atomic(kaddr, KM_USER0);
2509 page_cache_release(page);
2511 ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index - 1);
2513 * Fixup for eventual bits outside logical ntfs volume (see function
2514 * description above).
2516 if (vol->nr_clusters & 63)
2517 nr_free += 64 - (vol->nr_clusters & 63);
2518 up_read(&vol->lcnbmp_lock);
2519 /* If errors occured we may well have gone below zero, fix this. */
2522 ntfs_debug("Exiting.");
2527 * __get_nr_free_mft_records - return the number of free inodes on a volume
2528 * @vol: ntfs volume for which to obtain free inode count
2529 * @nr_free: number of mft records in filesystem
2530 * @max_index: maximum number of pages containing set bits
2532 * Calculate the number of free mft records (inodes) on the mounted NTFS
2533 * volume @vol. We actually calculate the number of mft records in use instead
2534 * because this allows us to not care about partial pages as these will be just
2535 * zero filled and hence not be counted as allocated mft record.
2537 * If any pages cannot be read we assume all mft records in the erroring pages
2538 * are in use. This means we return an underestimate on errors which is better
2539 * than an overestimate.
2541 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
2543 static unsigned long __get_nr_free_mft_records(ntfs_volume *vol,
2544 s64 nr_free, const pgoff_t max_index)
2547 struct address_space *mapping = vol->mftbmp_ino->i_mapping;
2548 filler_t *readpage = (filler_t*)mapping->a_ops->readpage;
2552 ntfs_debug("Entering.");
2553 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2554 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
2555 "0x%lx.", max_index, PAGE_CACHE_SIZE / 4);
2556 for (index = 0; index < max_index; index++) {
2559 * Read the page from page cache, getting it from backing store
2560 * if necessary, and increment the use count.
2562 page = read_cache_page(mapping, index, (filler_t*)readpage,
2564 /* Ignore pages which errored synchronously. */
2566 ntfs_debug("Sync read_cache_page() error. Skipping "
2567 "page (index 0x%lx).", index);
2568 nr_free -= PAGE_CACHE_SIZE * 8;
2571 wait_on_page_locked(page);
2572 /* Ignore pages which errored asynchronously. */
2573 if (!PageUptodate(page)) {
2574 ntfs_debug("Async read_cache_page() error. Skipping "
2575 "page (index 0x%lx).", index);
2576 page_cache_release(page);
2577 nr_free -= PAGE_CACHE_SIZE * 8;
2580 kaddr = (u32*)kmap_atomic(page, KM_USER0);
2582 * For each 4 bytes, subtract the number of set bits. If this
2583 * is the last page and it is partial we don't really care as
2584 * it just means we do a little extra work but it won't affect
2585 * the result as all out of range bytes are set to zero by
2588 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
2589 nr_free -= (s64)hweight32(kaddr[i]);
2590 kunmap_atomic(kaddr, KM_USER0);
2591 page_cache_release(page);
2593 ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.",
2595 /* If errors occured we may well have gone below zero, fix this. */
2598 ntfs_debug("Exiting.");
2603 * ntfs_statfs - return information about mounted NTFS volume
2604 * @dentry: dentry from mounted volume
2605 * @sfs: statfs structure in which to return the information
2607 * Return information about the mounted NTFS volume @dentry in the statfs structure
2608 * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is
2609 * called). We interpret the values to be correct of the moment in time at
2610 * which we are called. Most values are variable otherwise and this isn't just
2611 * the free values but the totals as well. For example we can increase the
2612 * total number of file nodes if we run out and we can keep doing this until
2613 * there is no more space on the volume left at all.
2615 * Called from vfs_statfs which is used to handle the statfs, fstatfs, and
2616 * ustat system calls.
2618 * Return 0 on success or -errno on error.
2620 static int ntfs_statfs(struct dentry *dentry, struct kstatfs *sfs)
2622 struct super_block *sb = dentry->d_sb;
2624 ntfs_volume *vol = NTFS_SB(sb);
2625 ntfs_inode *mft_ni = NTFS_I(vol->mft_ino);
2627 unsigned long flags;
2629 ntfs_debug("Entering.");
2630 /* Type of filesystem. */
2631 sfs->f_type = NTFS_SB_MAGIC;
2632 /* Optimal transfer block size. */
2633 sfs->f_bsize = PAGE_CACHE_SIZE;
2635 * Total data blocks in filesystem in units of f_bsize and since
2636 * inodes are also stored in data blocs ($MFT is a file) this is just
2637 * the total clusters.
2639 sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >>
2641 /* Free data blocks in filesystem in units of f_bsize. */
2642 size = get_nr_free_clusters(vol) << vol->cluster_size_bits >>
2646 /* Free blocks avail to non-superuser, same as above on NTFS. */
2647 sfs->f_bavail = sfs->f_bfree = size;
2648 /* Serialize accesses to the inode bitmap. */
2649 down_read(&vol->mftbmp_lock);
2650 read_lock_irqsave(&mft_ni->size_lock, flags);
2651 size = i_size_read(vol->mft_ino) >> vol->mft_record_size_bits;
2653 * Convert the maximum number of set bits into bytes rounded up, then
2654 * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
2655 * have one full and one partial page max_index = 2.
2657 max_index = ((((mft_ni->initialized_size >> vol->mft_record_size_bits)
2658 + 7) >> 3) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2659 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2660 /* Number of inodes in filesystem (at this point in time). */
2661 sfs->f_files = size;
2662 /* Free inodes in fs (based on current total count). */
2663 sfs->f_ffree = __get_nr_free_mft_records(vol, size, max_index);
2664 up_read(&vol->mftbmp_lock);
2666 * File system id. This is extremely *nix flavour dependent and even
2667 * within Linux itself all fs do their own thing. I interpret this to
2668 * mean a unique id associated with the mounted fs and not the id
2669 * associated with the filesystem driver, the latter is already given
2670 * by the filesystem type in sfs->f_type. Thus we use the 64-bit
2671 * volume serial number splitting it into two 32-bit parts. We enter
2672 * the least significant 32-bits in f_fsid[0] and the most significant
2673 * 32-bits in f_fsid[1].
2675 sfs->f_fsid.val[0] = vol->serial_no & 0xffffffff;
2676 sfs->f_fsid.val[1] = (vol->serial_no >> 32) & 0xffffffff;
2677 /* Maximum length of filenames. */
2678 sfs->f_namelen = NTFS_MAX_NAME_LEN;
2683 * The complete super operations.
2685 static struct super_operations ntfs_sops = {
2686 .alloc_inode = ntfs_alloc_big_inode, /* VFS: Allocate new inode. */
2687 .destroy_inode = ntfs_destroy_big_inode, /* VFS: Deallocate inode. */
2688 .put_inode = ntfs_put_inode, /* VFS: Called just before
2689 the inode reference count
2692 //.dirty_inode = NULL, /* VFS: Called from
2693 // __mark_inode_dirty(). */
2694 .write_inode = ntfs_write_inode, /* VFS: Write dirty inode to
2696 //.drop_inode = NULL, /* VFS: Called just after the
2697 // inode reference count has
2698 // been decreased to zero.
2699 // NOTE: The inode lock is
2700 // held. See fs/inode.c::
2701 // generic_drop_inode(). */
2702 //.delete_inode = NULL, /* VFS: Delete inode from disk.
2703 // Called when i_count becomes
2704 // 0 and i_nlink is also 0. */
2705 //.write_super = NULL, /* Flush dirty super block to
2707 //.sync_fs = NULL, /* ? */
2708 //.write_super_lockfs = NULL, /* ? */
2709 //.unlockfs = NULL, /* ? */
2710 #endif /* NTFS_RW */
2711 .put_super = ntfs_put_super, /* Syscall: umount. */
2712 .statfs = ntfs_statfs, /* Syscall: statfs */
2713 .remount_fs = ntfs_remount, /* Syscall: mount -o remount. */
2714 .clear_inode = ntfs_clear_big_inode, /* VFS: Called when an inode is
2715 removed from memory. */
2716 //.umount_begin = NULL, /* Forced umount. */
2717 .show_options = ntfs_show_options, /* Show mount options in
2722 * ntfs_fill_super - mount an ntfs filesystem
2723 * @sb: super block of ntfs filesystem to mount
2724 * @opt: string containing the mount options
2725 * @silent: silence error output
2727 * ntfs_fill_super() is called by the VFS to mount the device described by @sb
2728 * with the mount otions in @data with the NTFS filesystem.
2730 * If @silent is true, remain silent even if errors are detected. This is used
2731 * during bootup, when the kernel tries to mount the root filesystem with all
2732 * registered filesystems one after the other until one succeeds. This implies
2733 * that all filesystems except the correct one will quite correctly and
2734 * expectedly return an error, but nobody wants to see error messages when in
2735 * fact this is what is supposed to happen.
2737 * NOTE: @sb->s_flags contains the mount options flags.
2739 static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2742 struct buffer_head *bh;
2743 struct inode *tmp_ino;
2744 int blocksize, result;
2746 ntfs_debug("Entering.");
2748 sb->s_flags |= MS_RDONLY;
2749 #endif /* ! NTFS_RW */
2750 /* Allocate a new ntfs_volume and place it in sb->s_fs_info. */
2751 sb->s_fs_info = kmalloc(sizeof(ntfs_volume), GFP_NOFS);
2755 ntfs_error(sb, "Allocation of NTFS volume structure "
2756 "failed. Aborting mount...");
2759 /* Initialize ntfs_volume structure. */
2760 *vol = (ntfs_volume) {
2763 * Default is group and other don't have any access to files or
2764 * directories while owner has full access. Further, files by
2765 * default are not executable but directories are of course
2771 init_rwsem(&vol->mftbmp_lock);
2772 init_rwsem(&vol->lcnbmp_lock);
2776 /* By default, enable sparse support. */
2777 NVolSetSparseEnabled(vol);
2779 /* Important to get the mount options dealt with now. */
2780 if (!parse_options(vol, (char*)opt))
2783 /* We support sector sizes up to the PAGE_CACHE_SIZE. */
2784 if (bdev_hardsect_size(sb->s_bdev) > PAGE_CACHE_SIZE) {
2786 ntfs_error(sb, "Device has unsupported sector size "
2787 "(%i). The maximum supported sector "
2788 "size on this architecture is %lu "
2790 bdev_hardsect_size(sb->s_bdev),
2795 * Setup the device access block size to NTFS_BLOCK_SIZE or the hard
2796 * sector size, whichever is bigger.
2798 blocksize = sb_min_blocksize(sb, NTFS_BLOCK_SIZE);
2799 if (blocksize < NTFS_BLOCK_SIZE) {
2801 ntfs_error(sb, "Unable to set device block size.");
2804 BUG_ON(blocksize != sb->s_blocksize);
2805 ntfs_debug("Set device block size to %i bytes (block size bits %i).",
2806 blocksize, sb->s_blocksize_bits);
2807 /* Determine the size of the device in units of block_size bytes. */
2808 if (!i_size_read(sb->s_bdev->bd_inode)) {
2810 ntfs_error(sb, "Unable to determine device size.");
2813 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2814 sb->s_blocksize_bits;
2815 /* Read the boot sector and return unlocked buffer head to it. */
2816 if (!(bh = read_ntfs_boot_sector(sb, silent))) {
2818 ntfs_error(sb, "Not an NTFS volume.");
2822 * Extract the data from the boot sector and setup the ntfs volume
2825 result = parse_ntfs_boot_sector(vol, (NTFS_BOOT_SECTOR*)bh->b_data);
2829 ntfs_error(sb, "Unsupported NTFS filesystem.");
2833 * If the boot sector indicates a sector size bigger than the current
2834 * device block size, switch the device block size to the sector size.
2835 * TODO: It may be possible to support this case even when the set
2836 * below fails, we would just be breaking up the i/o for each sector
2837 * into multiple blocks for i/o purposes but otherwise it should just
2838 * work. However it is safer to leave disabled until someone hits this
2839 * error message and then we can get them to try it without the setting
2840 * so we know for sure that it works.
2842 if (vol->sector_size > blocksize) {
2843 blocksize = sb_set_blocksize(sb, vol->sector_size);
2844 if (blocksize != vol->sector_size) {
2846 ntfs_error(sb, "Unable to set device block "
2847 "size to sector size (%i).",
2851 BUG_ON(blocksize != sb->s_blocksize);
2852 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2853 sb->s_blocksize_bits;
2854 ntfs_debug("Changed device block size to %i bytes (block size "
2855 "bits %i) to match volume sector size.",
2856 blocksize, sb->s_blocksize_bits);
2858 /* Initialize the cluster and mft allocators. */
2859 ntfs_setup_allocators(vol);
2860 /* Setup remaining fields in the super block. */
2861 sb->s_magic = NTFS_SB_MAGIC;
2863 * Ntfs allows 63 bits for the file size, i.e. correct would be:
2864 * sb->s_maxbytes = ~0ULL >> 1;
2865 * But the kernel uses a long as the page cache page index which on
2866 * 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel
2867 * defined to the maximum the page cache page index can cope with
2868 * without overflowing the index or to 2^63 - 1, whichever is smaller.
2870 sb->s_maxbytes = MAX_LFS_FILESIZE;
2871 /* Ntfs measures time in 100ns intervals. */
2872 sb->s_time_gran = 100;
2874 * Now load the metadata required for the page cache and our address
2875 * space operations to function. We do this by setting up a specialised
2876 * read_inode method and then just calling the normal iget() to obtain
2877 * the inode for $MFT which is sufficient to allow our normal inode
2878 * operations and associated address space operations to function.
2880 sb->s_op = &ntfs_sops;
2881 tmp_ino = new_inode(sb);
2884 ntfs_error(sb, "Failed to load essential metadata.");
2887 tmp_ino->i_ino = FILE_MFT;
2888 insert_inode_hash(tmp_ino);
2889 if (ntfs_read_inode_mount(tmp_ino) < 0) {
2891 ntfs_error(sb, "Failed to load essential metadata.");
2892 goto iput_tmp_ino_err_out_now;
2894 mutex_lock(&ntfs_lock);
2896 * The current mount is a compression user if the cluster size is
2897 * less than or equal 4kiB.
2899 if (vol->cluster_size <= 4096 && !ntfs_nr_compression_users++) {
2900 result = allocate_compression_buffers();
2902 ntfs_error(NULL, "Failed to allocate buffers "
2903 "for compression engine.");
2904 ntfs_nr_compression_users--;
2905 mutex_unlock(&ntfs_lock);
2906 goto iput_tmp_ino_err_out_now;
2910 * Generate the global default upcase table if necessary. Also
2911 * temporarily increment the number of upcase users to avoid race
2912 * conditions with concurrent (u)mounts.
2914 if (!default_upcase)
2915 default_upcase = generate_default_upcase();
2916 ntfs_nr_upcase_users++;
2917 mutex_unlock(&ntfs_lock);
2919 * From now on, ignore @silent parameter. If we fail below this line,
2920 * it will be due to a corrupt fs or a system error, so we report it.
2923 * Open the system files with normal access functions and complete
2924 * setting up the ntfs super block.
2926 if (!load_system_files(vol)) {
2927 ntfs_error(sb, "Failed to load system files.");
2928 goto unl_upcase_iput_tmp_ino_err_out_now;
2930 if ((sb->s_root = d_alloc_root(vol->root_ino))) {
2931 /* We increment i_count simulating an ntfs_iget(). */
2932 atomic_inc(&vol->root_ino->i_count);
2933 ntfs_debug("Exiting, status successful.");
2934 /* Release the default upcase if it has no users. */
2935 mutex_lock(&ntfs_lock);
2936 if (!--ntfs_nr_upcase_users && default_upcase) {
2937 ntfs_free(default_upcase);
2938 default_upcase = NULL;
2940 mutex_unlock(&ntfs_lock);
2941 sb->s_export_op = &ntfs_export_ops;
2945 ntfs_error(sb, "Failed to allocate root directory.");
2946 /* Clean up after the successful load_system_files() call from above. */
2947 // TODO: Use ntfs_put_super() instead of repeating all this code...
2948 // FIXME: Should mark the volume clean as the error is most likely
2951 vol->vol_ino = NULL;
2952 /* NTFS 3.0+ specific clean up. */
2953 if (vol->major_ver >= 3) {
2955 if (vol->usnjrnl_j_ino) {
2956 iput(vol->usnjrnl_j_ino);
2957 vol->usnjrnl_j_ino = NULL;
2959 if (vol->usnjrnl_max_ino) {
2960 iput(vol->usnjrnl_max_ino);
2961 vol->usnjrnl_max_ino = NULL;
2963 if (vol->usnjrnl_ino) {
2964 iput(vol->usnjrnl_ino);
2965 vol->usnjrnl_ino = NULL;
2967 if (vol->quota_q_ino) {
2968 iput(vol->quota_q_ino);
2969 vol->quota_q_ino = NULL;
2971 if (vol->quota_ino) {
2972 iput(vol->quota_ino);
2973 vol->quota_ino = NULL;
2975 #endif /* NTFS_RW */
2976 if (vol->extend_ino) {
2977 iput(vol->extend_ino);
2978 vol->extend_ino = NULL;
2980 if (vol->secure_ino) {
2981 iput(vol->secure_ino);
2982 vol->secure_ino = NULL;
2985 iput(vol->root_ino);
2986 vol->root_ino = NULL;
2987 iput(vol->lcnbmp_ino);
2988 vol->lcnbmp_ino = NULL;
2989 iput(vol->mftbmp_ino);
2990 vol->mftbmp_ino = NULL;
2992 if (vol->logfile_ino) {
2993 iput(vol->logfile_ino);
2994 vol->logfile_ino = NULL;
2996 if (vol->mftmirr_ino) {
2997 iput(vol->mftmirr_ino);
2998 vol->mftmirr_ino = NULL;
3000 #endif /* NTFS_RW */
3001 /* Throw away the table of attribute definitions. */
3002 vol->attrdef_size = 0;
3004 ntfs_free(vol->attrdef);
3005 vol->attrdef = NULL;
3007 vol->upcase_len = 0;
3008 mutex_lock(&ntfs_lock);
3009 if (vol->upcase == default_upcase) {
3010 ntfs_nr_upcase_users--;
3013 mutex_unlock(&ntfs_lock);
3015 ntfs_free(vol->upcase);
3019 unload_nls(vol->nls_map);
3020 vol->nls_map = NULL;
3022 /* Error exit code path. */
3023 unl_upcase_iput_tmp_ino_err_out_now:
3025 * Decrease the number of upcase users and destroy the global default
3026 * upcase table if necessary.
3028 mutex_lock(&ntfs_lock);
3029 if (!--ntfs_nr_upcase_users && default_upcase) {
3030 ntfs_free(default_upcase);
3031 default_upcase = NULL;
3033 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
3034 free_compression_buffers();
3035 mutex_unlock(&ntfs_lock);
3036 iput_tmp_ino_err_out_now:
3038 if (vol->mft_ino && vol->mft_ino != tmp_ino)
3040 vol->mft_ino = NULL;
3042 * This is needed to get ntfs_clear_extent_inode() called for each
3043 * inode we have ever called ntfs_iget()/iput() on, otherwise we A)
3044 * leak resources and B) a subsequent mount fails automatically due to
3045 * ntfs_iget() never calling down into our ntfs_read_locked_inode()
3046 * method again... FIXME: Do we need to do this twice now because of
3047 * attribute inodes? I think not, so leave as is for now... (AIA)
3049 if (invalidate_inodes(sb)) {
3050 ntfs_error(sb, "Busy inodes left. This is most likely a NTFS "
3052 /* Copied from fs/super.c. I just love this message. (-; */
3053 printk("NTFS: Busy inodes after umount. Self-destruct in 5 "
3054 "seconds. Have a nice day...\n");
3056 /* Errors at this stage are irrelevant. */
3059 sb->s_fs_info = NULL;
3061 ntfs_debug("Failed, returning -EINVAL.");
3066 * This is a slab cache to optimize allocations and deallocations of Unicode
3067 * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN
3068 * (255) Unicode characters + a terminating NULL Unicode character.
3070 struct kmem_cache *ntfs_name_cache;
3072 /* Slab caches for efficient allocation/deallocation of inodes. */
3073 struct kmem_cache *ntfs_inode_cache;
3074 struct kmem_cache *ntfs_big_inode_cache;
3076 /* Init once constructor for the inode slab cache. */
3077 static void ntfs_big_inode_init_once(void *foo, struct kmem_cache *cachep,
3078 unsigned long flags)
3080 ntfs_inode *ni = (ntfs_inode *)foo;
3082 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
3083 SLAB_CTOR_CONSTRUCTOR)
3084 inode_init_once(VFS_I(ni));
3088 * Slab caches to optimize allocations and deallocations of attribute search
3089 * contexts and index contexts, respectively.
3091 struct kmem_cache *ntfs_attr_ctx_cache;
3092 struct kmem_cache *ntfs_index_ctx_cache;
3094 /* Driver wide mutex. */
3095 DEFINE_MUTEX(ntfs_lock);
3097 static int ntfs_get_sb(struct file_system_type *fs_type,
3098 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3100 return get_sb_bdev(fs_type, flags, dev_name, data, ntfs_fill_super,
3104 static struct file_system_type ntfs_fs_type = {
3105 .owner = THIS_MODULE,
3107 .get_sb = ntfs_get_sb,
3108 .kill_sb = kill_block_super,
3109 .fs_flags = FS_REQUIRES_DEV,
3112 /* Stable names for the slab caches. */
3113 static const char ntfs_index_ctx_cache_name[] = "ntfs_index_ctx_cache";
3114 static const char ntfs_attr_ctx_cache_name[] = "ntfs_attr_ctx_cache";
3115 static const char ntfs_name_cache_name[] = "ntfs_name_cache";
3116 static const char ntfs_inode_cache_name[] = "ntfs_inode_cache";
3117 static const char ntfs_big_inode_cache_name[] = "ntfs_big_inode_cache";
3119 static int __init init_ntfs_fs(void)
3123 /* This may be ugly but it results in pretty output so who cares. (-8 */
3124 printk(KERN_INFO "NTFS driver " NTFS_VERSION " [Flags: R/"
3138 ntfs_debug("Debug messages are enabled.");
3140 ntfs_index_ctx_cache = kmem_cache_create(ntfs_index_ctx_cache_name,
3141 sizeof(ntfs_index_context), 0 /* offset */,
3142 SLAB_HWCACHE_ALIGN, NULL /* ctor */, NULL /* dtor */);
3143 if (!ntfs_index_ctx_cache) {
3144 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3145 ntfs_index_ctx_cache_name);
3148 ntfs_attr_ctx_cache = kmem_cache_create(ntfs_attr_ctx_cache_name,
3149 sizeof(ntfs_attr_search_ctx), 0 /* offset */,
3150 SLAB_HWCACHE_ALIGN, NULL /* ctor */, NULL /* dtor */);
3151 if (!ntfs_attr_ctx_cache) {
3152 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3153 ntfs_attr_ctx_cache_name);
3157 ntfs_name_cache = kmem_cache_create(ntfs_name_cache_name,
3158 (NTFS_MAX_NAME_LEN+1) * sizeof(ntfschar), 0,
3159 SLAB_HWCACHE_ALIGN, NULL, NULL);
3160 if (!ntfs_name_cache) {
3161 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3162 ntfs_name_cache_name);
3166 ntfs_inode_cache = kmem_cache_create(ntfs_inode_cache_name,
3167 sizeof(ntfs_inode), 0,
3168 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL, NULL);
3169 if (!ntfs_inode_cache) {
3170 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3171 ntfs_inode_cache_name);
3175 ntfs_big_inode_cache = kmem_cache_create(ntfs_big_inode_cache_name,
3176 sizeof(big_ntfs_inode), 0,
3177 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD,
3178 ntfs_big_inode_init_once, NULL);
3179 if (!ntfs_big_inode_cache) {
3180 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3181 ntfs_big_inode_cache_name);
3182 goto big_inode_err_out;
3185 /* Register the ntfs sysctls. */
3186 err = ntfs_sysctl(1);
3188 printk(KERN_CRIT "NTFS: Failed to register NTFS sysctls!\n");
3189 goto sysctl_err_out;
3192 err = register_filesystem(&ntfs_fs_type);
3194 ntfs_debug("NTFS driver registered successfully.");
3195 return 0; /* Success! */
3197 printk(KERN_CRIT "NTFS: Failed to register NTFS filesystem driver!\n");
3200 kmem_cache_destroy(ntfs_big_inode_cache);
3202 kmem_cache_destroy(ntfs_inode_cache);
3204 kmem_cache_destroy(ntfs_name_cache);
3206 kmem_cache_destroy(ntfs_attr_ctx_cache);
3208 kmem_cache_destroy(ntfs_index_ctx_cache);
3211 printk(KERN_CRIT "NTFS: Aborting NTFS filesystem driver "
3212 "registration...\n");
3218 static void __exit exit_ntfs_fs(void)
3222 ntfs_debug("Unregistering NTFS driver.");
3224 unregister_filesystem(&ntfs_fs_type);
3226 if (kmem_cache_destroy(ntfs_big_inode_cache) && (err = 1))
3227 printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
3228 ntfs_big_inode_cache_name);
3229 if (kmem_cache_destroy(ntfs_inode_cache) && (err = 1))
3230 printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
3231 ntfs_inode_cache_name);
3232 if (kmem_cache_destroy(ntfs_name_cache) && (err = 1))
3233 printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
3234 ntfs_name_cache_name);
3235 if (kmem_cache_destroy(ntfs_attr_ctx_cache) && (err = 1))
3236 printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
3237 ntfs_attr_ctx_cache_name);
3238 if (kmem_cache_destroy(ntfs_index_ctx_cache) && (err = 1))
3239 printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
3240 ntfs_index_ctx_cache_name);
3242 printk(KERN_CRIT "NTFS: This causes memory to leak! There is "
3243 "probably a BUG in the driver! Please report "
3244 "you saw this message to "
3245 "linux-ntfs-dev@lists.sourceforge.net\n");
3246 /* Unregister the ntfs sysctls. */
3250 MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>");
3251 MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2006 Anton Altaparmakov");
3252 MODULE_VERSION(NTFS_VERSION);
3253 MODULE_LICENSE("GPL");
3255 module_param(debug_msgs, bool, 0);
3256 MODULE_PARM_DESC(debug_msgs, "Enable debug messages.");
3259 module_init(init_ntfs_fs)
3260 module_exit(exit_ntfs_fs)