2 * super.c - NTFS kernel super block handling. Part of the Linux-NTFS project.
4 * Copyright (c) 2001-2005 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/string.h>
26 #include <linux/spinlock.h>
27 #include <linux/blkdev.h> /* For bdev_hardsect_size(). */
28 #include <linux/backing-dev.h>
29 #include <linux/buffer_head.h>
30 #include <linux/vfs.h>
31 #include <linux/moduleparam.h>
32 #include <linux/smp_lock.h>
46 /* Number of mounted filesystems which have compression enabled. */
47 static unsigned long ntfs_nr_compression_users
;
49 /* A global default upcase table and a corresponding reference count. */
50 static ntfschar
*default_upcase
= NULL
;
51 static unsigned long ntfs_nr_upcase_users
= 0;
53 /* Error constants/strings used in inode.c::ntfs_show_options(). */
55 /* One of these must be present, default is ON_ERRORS_CONTINUE. */
56 ON_ERRORS_PANIC
= 0x01,
57 ON_ERRORS_REMOUNT_RO
= 0x02,
58 ON_ERRORS_CONTINUE
= 0x04,
59 /* Optional, can be combined with any of the above. */
60 ON_ERRORS_RECOVER
= 0x10,
63 const option_t on_errors_arr
[] = {
64 { ON_ERRORS_PANIC
, "panic" },
65 { ON_ERRORS_REMOUNT_RO
, "remount-ro", },
66 { ON_ERRORS_CONTINUE
, "continue", },
67 { ON_ERRORS_RECOVER
, "recover" },
74 * Copied from old ntfs driver (which copied from vfat driver).
76 static int simple_getbool(char *s
, BOOL
*setval
)
79 if (!strcmp(s
, "1") || !strcmp(s
, "yes") || !strcmp(s
, "true"))
81 else if (!strcmp(s
, "0") || !strcmp(s
, "no") ||
92 * parse_options - parse the (re)mount options
94 * @opt: string containing the (re)mount options
96 * Parse the recognized options in @opt for the ntfs volume described by @vol.
98 static BOOL
parse_options(ntfs_volume
*vol
, char *opt
)
101 static char *utf8
= "utf8";
102 int errors
= 0, sloppy
= 0;
103 uid_t uid
= (uid_t
)-1;
104 gid_t gid
= (gid_t
)-1;
105 mode_t fmask
= (mode_t
)-1, dmask
= (mode_t
)-1;
106 int mft_zone_multiplier
= -1, on_errors
= -1;
107 int show_sys_files
= -1, case_sensitive
= -1, disable_sparse
= -1;
108 struct nls_table
*nls_map
= NULL
, *old_nls
;
110 /* I am lazy... (-8 */
111 #define NTFS_GETOPT_WITH_DEFAULT(option, variable, default_value) \
112 if (!strcmp(p, option)) { \
114 variable = default_value; \
116 variable = simple_strtoul(ov = v, &v, 0); \
121 #define NTFS_GETOPT(option, variable) \
122 if (!strcmp(p, option)) { \
125 variable = simple_strtoul(ov = v, &v, 0); \
129 #define NTFS_GETOPT_BOOL(option, variable) \
130 if (!strcmp(p, option)) { \
132 if (!simple_getbool(v, &val)) \
136 #define NTFS_GETOPT_OPTIONS_ARRAY(option, variable, opt_array) \
137 if (!strcmp(p, option)) { \
142 if (variable == -1) \
144 for (_i = 0; opt_array[_i].str && *opt_array[_i].str; _i++) \
145 if (!strcmp(opt_array[_i].str, v)) { \
146 variable |= opt_array[_i].val; \
149 if (!opt_array[_i].str || !*opt_array[_i].str) \
153 goto no_mount_options
;
154 ntfs_debug("Entering with mount options string: %s", opt
);
155 while ((p
= strsep(&opt
, ","))) {
156 if ((v
= strchr(p
, '=')))
158 NTFS_GETOPT("uid", uid
)
159 else NTFS_GETOPT("gid", gid
)
160 else NTFS_GETOPT("umask", fmask
= dmask
)
161 else NTFS_GETOPT("fmask", fmask
)
162 else NTFS_GETOPT("dmask", dmask
)
163 else NTFS_GETOPT("mft_zone_multiplier", mft_zone_multiplier
)
164 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy
, TRUE
)
165 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files
)
166 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive
)
167 else NTFS_GETOPT_BOOL("disable_sparse", disable_sparse
)
168 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors
,
170 else if (!strcmp(p
, "posix") || !strcmp(p
, "show_inodes"))
171 ntfs_warning(vol
->sb
, "Ignoring obsolete option %s.",
173 else if (!strcmp(p
, "nls") || !strcmp(p
, "iocharset")) {
174 if (!strcmp(p
, "iocharset"))
175 ntfs_warning(vol
->sb
, "Option iocharset is "
176 "deprecated. Please use "
177 "option nls=<charsetname> in "
183 nls_map
= load_nls(v
);
186 ntfs_error(vol
->sb
, "NLS character set "
190 ntfs_error(vol
->sb
, "NLS character set %s not "
191 "found. Using previous one %s.",
192 v
, old_nls
->charset
);
194 } else /* nls_map */ {
198 } else if (!strcmp(p
, "utf8")) {
200 ntfs_warning(vol
->sb
, "Option utf8 is no longer "
201 "supported, using option nls=utf8. Please "
202 "use option nls=utf8 in the future and "
203 "make sure utf8 is compiled either as a "
204 "module or into the kernel.");
207 else if (!simple_getbool(v
, &val
))
214 ntfs_error(vol
->sb
, "Unrecognized mount option %s.", p
);
215 if (errors
< INT_MAX
)
218 #undef NTFS_GETOPT_OPTIONS_ARRAY
219 #undef NTFS_GETOPT_BOOL
221 #undef NTFS_GETOPT_WITH_DEFAULT
224 if (errors
&& !sloppy
)
227 ntfs_warning(vol
->sb
, "Sloppy option given. Ignoring "
228 "unrecognized mount option(s) and continuing.");
229 /* Keep this first! */
230 if (on_errors
!= -1) {
232 ntfs_error(vol
->sb
, "Invalid errors option argument "
233 "or bug in options parser.");
238 if (vol
->nls_map
&& vol
->nls_map
!= nls_map
) {
239 ntfs_error(vol
->sb
, "Cannot change NLS character set "
242 } /* else (!vol->nls_map) */
243 ntfs_debug("Using NLS character set %s.", nls_map
->charset
);
244 vol
->nls_map
= nls_map
;
245 } else /* (!nls_map) */ {
247 vol
->nls_map
= load_nls_default();
249 ntfs_error(vol
->sb
, "Failed to load default "
250 "NLS character set.");
253 ntfs_debug("Using default NLS character set (%s).",
254 vol
->nls_map
->charset
);
257 if (mft_zone_multiplier
!= -1) {
258 if (vol
->mft_zone_multiplier
&& vol
->mft_zone_multiplier
!=
259 mft_zone_multiplier
) {
260 ntfs_error(vol
->sb
, "Cannot change mft_zone_multiplier "
264 if (mft_zone_multiplier
< 1 || mft_zone_multiplier
> 4) {
265 ntfs_error(vol
->sb
, "Invalid mft_zone_multiplier. "
266 "Using default value, i.e. 1.");
267 mft_zone_multiplier
= 1;
269 vol
->mft_zone_multiplier
= mft_zone_multiplier
;
271 if (!vol
->mft_zone_multiplier
)
272 vol
->mft_zone_multiplier
= 1;
274 vol
->on_errors
= on_errors
;
275 if (!vol
->on_errors
|| vol
->on_errors
== ON_ERRORS_RECOVER
)
276 vol
->on_errors
|= ON_ERRORS_CONTINUE
;
277 if (uid
!= (uid_t
)-1)
279 if (gid
!= (gid_t
)-1)
281 if (fmask
!= (mode_t
)-1)
283 if (dmask
!= (mode_t
)-1)
285 if (show_sys_files
!= -1) {
287 NVolSetShowSystemFiles(vol
);
289 NVolClearShowSystemFiles(vol
);
291 if (case_sensitive
!= -1) {
293 NVolSetCaseSensitive(vol
);
295 NVolClearCaseSensitive(vol
);
297 if (disable_sparse
!= -1) {
299 NVolClearSparseEnabled(vol
);
301 if (!NVolSparseEnabled(vol
) &&
302 vol
->major_ver
&& vol
->major_ver
< 3)
303 ntfs_warning(vol
->sb
, "Not enabling sparse "
304 "support due to NTFS volume "
305 "version %i.%i (need at least "
306 "version 3.0).", vol
->major_ver
,
309 NVolSetSparseEnabled(vol
);
314 ntfs_error(vol
->sb
, "The %s option requires an argument.", p
);
317 ntfs_error(vol
->sb
, "The %s option requires a boolean argument.", p
);
320 ntfs_error(vol
->sb
, "Invalid %s option argument: %s", p
, ov
);
327 * ntfs_write_volume_flags - write new flags to the volume information flags
328 * @vol: ntfs volume on which to modify the flags
329 * @flags: new flags value for the volume information flags
331 * Internal function. You probably want to use ntfs_{set,clear}_volume_flags()
332 * instead (see below).
334 * Replace the volume information flags on the volume @vol with the value
335 * supplied in @flags. Note, this overwrites the volume information flags, so
336 * make sure to combine the flags you want to modify with the old flags and use
337 * the result when calling ntfs_write_volume_flags().
339 * Return 0 on success and -errno on error.
341 static int ntfs_write_volume_flags(ntfs_volume
*vol
, const VOLUME_FLAGS flags
)
343 ntfs_inode
*ni
= NTFS_I(vol
->vol_ino
);
345 VOLUME_INFORMATION
*vi
;
346 ntfs_attr_search_ctx
*ctx
;
349 ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
350 le16_to_cpu(vol
->vol_flags
), le16_to_cpu(flags
));
351 if (vol
->vol_flags
== flags
)
354 m
= map_mft_record(ni
);
359 ctx
= ntfs_attr_get_search_ctx(ni
, m
);
362 goto put_unm_err_out
;
364 err
= ntfs_attr_lookup(AT_VOLUME_INFORMATION
, NULL
, 0, 0, 0, NULL
, 0,
367 goto put_unm_err_out
;
368 vi
= (VOLUME_INFORMATION
*)((u8
*)ctx
->attr
+
369 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
));
370 vol
->vol_flags
= vi
->flags
= flags
;
371 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
372 mark_mft_record_dirty(ctx
->ntfs_ino
);
373 ntfs_attr_put_search_ctx(ctx
);
374 unmap_mft_record(ni
);
380 ntfs_attr_put_search_ctx(ctx
);
381 unmap_mft_record(ni
);
383 ntfs_error(vol
->sb
, "Failed with error code %i.", -err
);
388 * ntfs_set_volume_flags - set bits in the volume information flags
389 * @vol: ntfs volume on which to modify the flags
390 * @flags: flags to set on the volume
392 * Set the bits in @flags in the volume information flags on the volume @vol.
394 * Return 0 on success and -errno on error.
396 static inline int ntfs_set_volume_flags(ntfs_volume
*vol
, VOLUME_FLAGS flags
)
398 flags
&= VOLUME_FLAGS_MASK
;
399 return ntfs_write_volume_flags(vol
, vol
->vol_flags
| flags
);
403 * ntfs_clear_volume_flags - clear bits in the volume information flags
404 * @vol: ntfs volume on which to modify the flags
405 * @flags: flags to clear on the volume
407 * Clear the bits in @flags in the volume information flags on the volume @vol.
409 * Return 0 on success and -errno on error.
411 static inline int ntfs_clear_volume_flags(ntfs_volume
*vol
, VOLUME_FLAGS flags
)
413 flags
&= VOLUME_FLAGS_MASK
;
414 flags
= vol
->vol_flags
& cpu_to_le16(~le16_to_cpu(flags
));
415 return ntfs_write_volume_flags(vol
, flags
);
421 * ntfs_remount - change the mount options of a mounted ntfs filesystem
422 * @sb: superblock of mounted ntfs filesystem
423 * @flags: remount flags
424 * @opt: remount options string
426 * Change the mount options of an already mounted ntfs filesystem.
428 * NOTE: The VFS sets the @sb->s_flags remount flags to @flags after
429 * ntfs_remount() returns successfully (i.e. returns 0). Otherwise,
430 * @sb->s_flags are not changed.
432 static int ntfs_remount(struct super_block
*sb
, int *flags
, char *opt
)
434 ntfs_volume
*vol
= NTFS_SB(sb
);
436 ntfs_debug("Entering with remount options string: %s", opt
);
438 /* For read-only compiled driver, enforce all read-only flags. */
439 *flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
442 * For the read-write compiled driver, if we are remounting read-write,
443 * make sure there are no volume errors and that no unsupported volume
444 * flags are set. Also, empty the logfile journal as it would become
445 * stale as soon as something is written to the volume and mark the
446 * volume dirty so that chkdsk is run if the volume is not umounted
447 * cleanly. Finally, mark the quotas out of date so Windows rescans
448 * the volume on boot and updates them.
450 * When remounting read-only, mark the volume clean if no volume errors
453 if ((sb
->s_flags
& MS_RDONLY
) && !(*flags
& MS_RDONLY
)) {
454 static const char *es
= ". Cannot remount read-write.";
456 /* Remounting read-write. */
457 if (NVolErrors(vol
)) {
458 ntfs_error(sb
, "Volume has errors and is read-only%s",
462 if (vol
->vol_flags
& VOLUME_IS_DIRTY
) {
463 ntfs_error(sb
, "Volume is dirty and read-only%s", es
);
466 if (vol
->vol_flags
& VOLUME_MUST_MOUNT_RO_MASK
) {
467 ntfs_error(sb
, "Volume has unsupported flags set and "
468 "is read-only%s", es
);
471 if (ntfs_set_volume_flags(vol
, VOLUME_IS_DIRTY
)) {
472 ntfs_error(sb
, "Failed to set dirty bit in volume "
473 "information flags%s", es
);
477 // TODO: Enable this code once we start modifying anything that
478 // is different between NTFS 1.2 and 3.x...
479 /* Set NT4 compatibility flag on newer NTFS version volumes. */
480 if ((vol
->major_ver
> 1)) {
481 if (ntfs_set_volume_flags(vol
, VOLUME_MOUNTED_ON_NT4
)) {
482 ntfs_error(sb
, "Failed to set NT4 "
483 "compatibility flag%s", es
);
489 if (!ntfs_empty_logfile(vol
->logfile_ino
)) {
490 ntfs_error(sb
, "Failed to empty journal $LogFile%s",
495 if (!ntfs_mark_quotas_out_of_date(vol
)) {
496 ntfs_error(sb
, "Failed to mark quotas out of date%s",
501 if (!ntfs_stamp_usnjrnl(vol
)) {
502 ntfs_error(sb
, "Failed to stamp transation log "
507 } else if (!(sb
->s_flags
& MS_RDONLY
) && (*flags
& MS_RDONLY
)) {
508 /* Remounting read-only. */
509 if (!NVolErrors(vol
)) {
510 if (ntfs_clear_volume_flags(vol
, VOLUME_IS_DIRTY
))
511 ntfs_warning(sb
, "Failed to clear dirty bit "
512 "in volume information "
513 "flags. Run chkdsk.");
518 // TODO: Deal with *flags.
520 if (!parse_options(vol
, opt
))
527 * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector
528 * @sb: Super block of the device to which @b belongs.
529 * @b: Boot sector of device @sb to check.
530 * @silent: If TRUE, all output will be silenced.
532 * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot
533 * sector. Returns TRUE if it is valid and FALSE if not.
535 * @sb is only needed for warning/error output, i.e. it can be NULL when silent
538 static BOOL
is_boot_sector_ntfs(const struct super_block
*sb
,
539 const NTFS_BOOT_SECTOR
*b
, const BOOL silent
)
542 * Check that checksum == sum of u32 values from b to the checksum
543 * field. If checksum is zero, no checking is done. We will work when
544 * the checksum test fails, since some utilities update the boot sector
545 * ignoring the checksum which leaves the checksum out-of-date. We
546 * report a warning if this is the case.
548 if ((void*)b
< (void*)&b
->checksum
&& b
->checksum
&& !silent
) {
552 for (i
= 0, u
= (le32
*)b
; u
< (le32
*)(&b
->checksum
); ++u
)
553 i
+= le32_to_cpup(u
);
554 if (le32_to_cpu(b
->checksum
) != i
)
555 ntfs_warning(sb
, "Invalid boot sector checksum.");
557 /* Check OEMidentifier is "NTFS " */
558 if (b
->oem_id
!= magicNTFS
)
560 /* Check bytes per sector value is between 256 and 4096. */
561 if (le16_to_cpu(b
->bpb
.bytes_per_sector
) < 0x100 ||
562 le16_to_cpu(b
->bpb
.bytes_per_sector
) > 0x1000)
564 /* Check sectors per cluster value is valid. */
565 switch (b
->bpb
.sectors_per_cluster
) {
566 case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128:
571 /* Check the cluster size is not above the maximum (64kiB). */
572 if ((u32
)le16_to_cpu(b
->bpb
.bytes_per_sector
) *
573 b
->bpb
.sectors_per_cluster
> NTFS_MAX_CLUSTER_SIZE
)
575 /* Check reserved/unused fields are really zero. */
576 if (le16_to_cpu(b
->bpb
.reserved_sectors
) ||
577 le16_to_cpu(b
->bpb
.root_entries
) ||
578 le16_to_cpu(b
->bpb
.sectors
) ||
579 le16_to_cpu(b
->bpb
.sectors_per_fat
) ||
580 le32_to_cpu(b
->bpb
.large_sectors
) || b
->bpb
.fats
)
582 /* Check clusters per file mft record value is valid. */
583 if ((u8
)b
->clusters_per_mft_record
< 0xe1 ||
584 (u8
)b
->clusters_per_mft_record
> 0xf7)
585 switch (b
->clusters_per_mft_record
) {
586 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
591 /* Check clusters per index block value is valid. */
592 if ((u8
)b
->clusters_per_index_record
< 0xe1 ||
593 (u8
)b
->clusters_per_index_record
> 0xf7)
594 switch (b
->clusters_per_index_record
) {
595 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
601 * Check for valid end of sector marker. We will work without it, but
602 * many BIOSes will refuse to boot from a bootsector if the magic is
603 * incorrect, so we emit a warning.
605 if (!silent
&& b
->end_of_sector_marker
!= const_cpu_to_le16(0xaa55))
606 ntfs_warning(sb
, "Invalid end of sector marker.");
613 * read_ntfs_boot_sector - read the NTFS boot sector of a device
614 * @sb: super block of device to read the boot sector from
615 * @silent: if true, suppress all output
617 * Reads the boot sector from the device and validates it. If that fails, tries
618 * to read the backup boot sector, first from the end of the device a-la NT4 and
619 * later and then from the middle of the device a-la NT3.51 and before.
621 * If a valid boot sector is found but it is not the primary boot sector, we
622 * repair the primary boot sector silently (unless the device is read-only or
623 * the primary boot sector is not accessible).
625 * NOTE: To call this function, @sb must have the fields s_dev, the ntfs super
626 * block (u.ntfs_sb), nr_blocks and the device flags (s_flags) initialized
627 * to their respective values.
629 * Return the unlocked buffer head containing the boot sector or NULL on error.
631 static struct buffer_head
*read_ntfs_boot_sector(struct super_block
*sb
,
634 const char *read_err_str
= "Unable to read %s boot sector.";
635 struct buffer_head
*bh_primary
, *bh_backup
;
636 long nr_blocks
= NTFS_SB(sb
)->nr_blocks
;
638 /* Try to read primary boot sector. */
639 if ((bh_primary
= sb_bread(sb
, 0))) {
640 if (is_boot_sector_ntfs(sb
, (NTFS_BOOT_SECTOR
*)
641 bh_primary
->b_data
, silent
))
644 ntfs_error(sb
, "Primary boot sector is invalid.");
646 ntfs_error(sb
, read_err_str
, "primary");
647 if (!(NTFS_SB(sb
)->on_errors
& ON_ERRORS_RECOVER
)) {
651 ntfs_error(sb
, "Mount option errors=recover not used. "
652 "Aborting without trying to recover.");
655 /* Try to read NT4+ backup boot sector. */
656 if ((bh_backup
= sb_bread(sb
, nr_blocks
- 1))) {
657 if (is_boot_sector_ntfs(sb
, (NTFS_BOOT_SECTOR
*)
658 bh_backup
->b_data
, silent
))
659 goto hotfix_primary_boot_sector
;
662 ntfs_error(sb
, read_err_str
, "backup");
663 /* Try to read NT3.51- backup boot sector. */
664 if ((bh_backup
= sb_bread(sb
, nr_blocks
>> 1))) {
665 if (is_boot_sector_ntfs(sb
, (NTFS_BOOT_SECTOR
*)
666 bh_backup
->b_data
, silent
))
667 goto hotfix_primary_boot_sector
;
669 ntfs_error(sb
, "Could not find a valid backup boot "
673 ntfs_error(sb
, read_err_str
, "backup");
674 /* We failed. Cleanup and return. */
678 hotfix_primary_boot_sector
:
681 * If we managed to read sector zero and the volume is not
682 * read-only, copy the found, valid backup boot sector to the
683 * primary boot sector.
685 if (!(sb
->s_flags
& MS_RDONLY
)) {
686 ntfs_warning(sb
, "Hot-fix: Recovering invalid primary "
687 "boot sector from backup copy.");
688 memcpy(bh_primary
->b_data
, bh_backup
->b_data
,
690 mark_buffer_dirty(bh_primary
);
691 sync_dirty_buffer(bh_primary
);
692 if (buffer_uptodate(bh_primary
)) {
696 ntfs_error(sb
, "Hot-fix: Device write error while "
697 "recovering primary boot sector.");
699 ntfs_warning(sb
, "Hot-fix: Recovery of primary boot "
700 "sector failed: Read-only mount.");
704 ntfs_warning(sb
, "Using backup boot sector.");
709 * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol
710 * @vol: volume structure to initialise with data from boot sector
711 * @b: boot sector to parse
713 * Parse the ntfs boot sector @b and store all imporant information therein in
714 * the ntfs super block @vol. Return TRUE on success and FALSE on error.
716 static BOOL
parse_ntfs_boot_sector(ntfs_volume
*vol
, const NTFS_BOOT_SECTOR
*b
)
718 unsigned int sectors_per_cluster_bits
, nr_hidden_sects
;
719 int clusters_per_mft_record
, clusters_per_index_record
;
722 vol
->sector_size
= le16_to_cpu(b
->bpb
.bytes_per_sector
);
723 vol
->sector_size_bits
= ffs(vol
->sector_size
) - 1;
724 ntfs_debug("vol->sector_size = %i (0x%x)", vol
->sector_size
,
726 ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol
->sector_size_bits
,
727 vol
->sector_size_bits
);
728 if (vol
->sector_size
!= vol
->sb
->s_blocksize
)
729 ntfs_warning(vol
->sb
, "The boot sector indicates a sector size "
730 "different from the device sector size.");
731 ntfs_debug("sectors_per_cluster = 0x%x", b
->bpb
.sectors_per_cluster
);
732 sectors_per_cluster_bits
= ffs(b
->bpb
.sectors_per_cluster
) - 1;
733 ntfs_debug("sectors_per_cluster_bits = 0x%x",
734 sectors_per_cluster_bits
);
735 nr_hidden_sects
= le32_to_cpu(b
->bpb
.hidden_sectors
);
736 ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects
);
737 vol
->cluster_size
= vol
->sector_size
<< sectors_per_cluster_bits
;
738 vol
->cluster_size_mask
= vol
->cluster_size
- 1;
739 vol
->cluster_size_bits
= ffs(vol
->cluster_size
) - 1;
740 ntfs_debug("vol->cluster_size = %i (0x%x)", vol
->cluster_size
,
742 ntfs_debug("vol->cluster_size_mask = 0x%x", vol
->cluster_size_mask
);
743 ntfs_debug("vol->cluster_size_bits = %i (0x%x)",
744 vol
->cluster_size_bits
, vol
->cluster_size_bits
);
745 if (vol
->sector_size
> vol
->cluster_size
) {
746 ntfs_error(vol
->sb
, "Sector sizes above the cluster size are "
747 "not supported. Sorry.");
750 if (vol
->sb
->s_blocksize
> vol
->cluster_size
) {
751 ntfs_error(vol
->sb
, "Cluster sizes smaller than the device "
752 "sector size are not supported. Sorry.");
755 clusters_per_mft_record
= b
->clusters_per_mft_record
;
756 ntfs_debug("clusters_per_mft_record = %i (0x%x)",
757 clusters_per_mft_record
, clusters_per_mft_record
);
758 if (clusters_per_mft_record
> 0)
759 vol
->mft_record_size
= vol
->cluster_size
<<
760 (ffs(clusters_per_mft_record
) - 1);
763 * When mft_record_size < cluster_size, clusters_per_mft_record
764 * = -log2(mft_record_size) bytes. mft_record_size normaly is
765 * 1024 bytes, which is encoded as 0xF6 (-10 in decimal).
767 vol
->mft_record_size
= 1 << -clusters_per_mft_record
;
768 vol
->mft_record_size_mask
= vol
->mft_record_size
- 1;
769 vol
->mft_record_size_bits
= ffs(vol
->mft_record_size
) - 1;
770 ntfs_debug("vol->mft_record_size = %i (0x%x)", vol
->mft_record_size
,
771 vol
->mft_record_size
);
772 ntfs_debug("vol->mft_record_size_mask = 0x%x",
773 vol
->mft_record_size_mask
);
774 ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
775 vol
->mft_record_size_bits
, vol
->mft_record_size_bits
);
777 * We cannot support mft record sizes above the PAGE_CACHE_SIZE since
778 * we store $MFT/$DATA, the table of mft records in the page cache.
780 if (vol
->mft_record_size
> PAGE_CACHE_SIZE
) {
781 ntfs_error(vol
->sb
, "Mft record size %i (0x%x) exceeds the "
782 "page cache size on your system %lu (0x%lx). "
783 "This is not supported. Sorry.",
784 vol
->mft_record_size
, vol
->mft_record_size
,
785 PAGE_CACHE_SIZE
, PAGE_CACHE_SIZE
);
788 clusters_per_index_record
= b
->clusters_per_index_record
;
789 ntfs_debug("clusters_per_index_record = %i (0x%x)",
790 clusters_per_index_record
, clusters_per_index_record
);
791 if (clusters_per_index_record
> 0)
792 vol
->index_record_size
= vol
->cluster_size
<<
793 (ffs(clusters_per_index_record
) - 1);
796 * When index_record_size < cluster_size,
797 * clusters_per_index_record = -log2(index_record_size) bytes.
798 * index_record_size normaly equals 4096 bytes, which is
799 * encoded as 0xF4 (-12 in decimal).
801 vol
->index_record_size
= 1 << -clusters_per_index_record
;
802 vol
->index_record_size_mask
= vol
->index_record_size
- 1;
803 vol
->index_record_size_bits
= ffs(vol
->index_record_size
) - 1;
804 ntfs_debug("vol->index_record_size = %i (0x%x)",
805 vol
->index_record_size
, vol
->index_record_size
);
806 ntfs_debug("vol->index_record_size_mask = 0x%x",
807 vol
->index_record_size_mask
);
808 ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
809 vol
->index_record_size_bits
,
810 vol
->index_record_size_bits
);
812 * Get the size of the volume in clusters and check for 64-bit-ness.
813 * Windows currently only uses 32 bits to save the clusters so we do
814 * the same as it is much faster on 32-bit CPUs.
816 ll
= sle64_to_cpu(b
->number_of_sectors
) >> sectors_per_cluster_bits
;
817 if ((u64
)ll
>= 1ULL << 32) {
818 ntfs_error(vol
->sb
, "Cannot handle 64-bit clusters. Sorry.");
821 vol
->nr_clusters
= ll
;
822 ntfs_debug("vol->nr_clusters = 0x%llx", (long long)vol
->nr_clusters
);
824 * On an architecture where unsigned long is 32-bits, we restrict the
825 * volume size to 2TiB (2^41). On a 64-bit architecture, the compiler
826 * will hopefully optimize the whole check away.
828 if (sizeof(unsigned long) < 8) {
829 if ((ll
<< vol
->cluster_size_bits
) >= (1ULL << 41)) {
830 ntfs_error(vol
->sb
, "Volume size (%lluTiB) is too "
831 "large for this architecture. "
832 "Maximum supported is 2TiB. Sorry.",
833 (unsigned long long)ll
>> (40 -
834 vol
->cluster_size_bits
));
838 ll
= sle64_to_cpu(b
->mft_lcn
);
839 if (ll
>= vol
->nr_clusters
) {
840 ntfs_error(vol
->sb
, "MFT LCN is beyond end of volume. Weird.");
844 ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol
->mft_lcn
);
845 ll
= sle64_to_cpu(b
->mftmirr_lcn
);
846 if (ll
>= vol
->nr_clusters
) {
847 ntfs_error(vol
->sb
, "MFTMirr LCN is beyond end of volume. "
851 vol
->mftmirr_lcn
= ll
;
852 ntfs_debug("vol->mftmirr_lcn = 0x%llx", (long long)vol
->mftmirr_lcn
);
855 * Work out the size of the mft mirror in number of mft records. If the
856 * cluster size is less than or equal to the size taken by four mft
857 * records, the mft mirror stores the first four mft records. If the
858 * cluster size is bigger than the size taken by four mft records, the
859 * mft mirror contains as many mft records as will fit into one
862 if (vol
->cluster_size
<= (4 << vol
->mft_record_size_bits
))
863 vol
->mftmirr_size
= 4;
865 vol
->mftmirr_size
= vol
->cluster_size
>>
866 vol
->mft_record_size_bits
;
867 ntfs_debug("vol->mftmirr_size = %i", vol
->mftmirr_size
);
869 vol
->serial_no
= le64_to_cpu(b
->volume_serial_number
);
870 ntfs_debug("vol->serial_no = 0x%llx",
871 (unsigned long long)vol
->serial_no
);
876 * ntfs_setup_allocators - initialize the cluster and mft allocators
877 * @vol: volume structure for which to setup the allocators
879 * Setup the cluster (lcn) and mft allocators to the starting values.
881 static void ntfs_setup_allocators(ntfs_volume
*vol
)
884 LCN mft_zone_size
, mft_lcn
;
887 ntfs_debug("vol->mft_zone_multiplier = 0x%x",
888 vol
->mft_zone_multiplier
);
890 /* Determine the size of the MFT zone. */
891 mft_zone_size
= vol
->nr_clusters
;
892 switch (vol
->mft_zone_multiplier
) { /* % of volume size in clusters */
894 mft_zone_size
>>= 1; /* 50% */
897 mft_zone_size
= (mft_zone_size
+
898 (mft_zone_size
>> 1)) >> 2; /* 37.5% */
901 mft_zone_size
>>= 2; /* 25% */
905 mft_zone_size
>>= 3; /* 12.5% */
908 /* Setup the mft zone. */
909 vol
->mft_zone_start
= vol
->mft_zone_pos
= vol
->mft_lcn
;
910 ntfs_debug("vol->mft_zone_pos = 0x%llx",
911 (unsigned long long)vol
->mft_zone_pos
);
913 * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
914 * source) and if the actual mft_lcn is in the expected place or even
915 * further to the front of the volume, extend the mft_zone to cover the
916 * beginning of the volume as well. This is in order to protect the
917 * area reserved for the mft bitmap as well within the mft_zone itself.
918 * On non-standard volumes we do not protect it as the overhead would
919 * be higher than the speed increase we would get by doing it.
921 mft_lcn
= (8192 + 2 * vol
->cluster_size
- 1) / vol
->cluster_size
;
922 if (mft_lcn
* vol
->cluster_size
< 16 * 1024)
923 mft_lcn
= (16 * 1024 + vol
->cluster_size
- 1) /
925 if (vol
->mft_zone_start
<= mft_lcn
)
926 vol
->mft_zone_start
= 0;
927 ntfs_debug("vol->mft_zone_start = 0x%llx",
928 (unsigned long long)vol
->mft_zone_start
);
930 * Need to cap the mft zone on non-standard volumes so that it does
931 * not point outside the boundaries of the volume. We do this by
932 * halving the zone size until we are inside the volume.
934 vol
->mft_zone_end
= vol
->mft_lcn
+ mft_zone_size
;
935 while (vol
->mft_zone_end
>= vol
->nr_clusters
) {
937 vol
->mft_zone_end
= vol
->mft_lcn
+ mft_zone_size
;
939 ntfs_debug("vol->mft_zone_end = 0x%llx",
940 (unsigned long long)vol
->mft_zone_end
);
942 * Set the current position within each data zone to the start of the
945 vol
->data1_zone_pos
= vol
->mft_zone_end
;
946 ntfs_debug("vol->data1_zone_pos = 0x%llx",
947 (unsigned long long)vol
->data1_zone_pos
);
948 vol
->data2_zone_pos
= 0;
949 ntfs_debug("vol->data2_zone_pos = 0x%llx",
950 (unsigned long long)vol
->data2_zone_pos
);
952 /* Set the mft data allocation position to mft record 24. */
953 vol
->mft_data_pos
= 24;
954 ntfs_debug("vol->mft_data_pos = 0x%llx",
955 (unsigned long long)vol
->mft_data_pos
);
962 * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume
963 * @vol: ntfs super block describing device whose mft mirror to load
965 * Return TRUE on success or FALSE on error.
967 static BOOL
load_and_init_mft_mirror(ntfs_volume
*vol
)
969 struct inode
*tmp_ino
;
972 ntfs_debug("Entering.");
973 /* Get mft mirror inode. */
974 tmp_ino
= ntfs_iget(vol
->sb
, FILE_MFTMirr
);
975 if (IS_ERR(tmp_ino
) || is_bad_inode(tmp_ino
)) {
976 if (!IS_ERR(tmp_ino
))
978 /* Caller will display error message. */
982 * Re-initialize some specifics about $MFTMirr's inode as
983 * ntfs_read_inode() will have set up the default ones.
985 /* Set uid and gid to root. */
986 tmp_ino
->i_uid
= tmp_ino
->i_gid
= 0;
987 /* Regular file. No access for anyone. */
988 tmp_ino
->i_mode
= S_IFREG
;
989 /* No VFS initiated operations allowed for $MFTMirr. */
990 tmp_ino
->i_op
= &ntfs_empty_inode_ops
;
991 tmp_ino
->i_fop
= &ntfs_empty_file_ops
;
992 /* Put in our special address space operations. */
993 tmp_ino
->i_mapping
->a_ops
= &ntfs_mst_aops
;
994 tmp_ni
= NTFS_I(tmp_ino
);
995 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
996 NInoSetMstProtected(tmp_ni
);
997 NInoSetSparseDisabled(tmp_ni
);
999 * Set up our little cheat allowing us to reuse the async read io
1000 * completion handler for directories.
1002 tmp_ni
->itype
.index
.block_size
= vol
->mft_record_size
;
1003 tmp_ni
->itype
.index
.block_size_bits
= vol
->mft_record_size_bits
;
1004 vol
->mftmirr_ino
= tmp_ino
;
1005 ntfs_debug("Done.");
1010 * check_mft_mirror - compare contents of the mft mirror with the mft
1011 * @vol: ntfs super block describing device whose mft mirror to check
1013 * Return TRUE on success or FALSE on error.
1015 * Note, this function also results in the mft mirror runlist being completely
1016 * mapped into memory. The mft mirror write code requires this and will BUG()
1017 * should it find an unmapped runlist element.
1019 static BOOL
check_mft_mirror(ntfs_volume
*vol
)
1021 struct super_block
*sb
= vol
->sb
;
1022 ntfs_inode
*mirr_ni
;
1023 struct page
*mft_page
, *mirr_page
;
1025 runlist_element
*rl
, rl2
[2];
1027 int mrecs_per_page
, i
;
1029 ntfs_debug("Entering.");
1030 /* Compare contents of $MFT and $MFTMirr. */
1031 mrecs_per_page
= PAGE_CACHE_SIZE
/ vol
->mft_record_size
;
1032 BUG_ON(!mrecs_per_page
);
1033 BUG_ON(!vol
->mftmirr_size
);
1034 mft_page
= mirr_page
= NULL
;
1035 kmft
= kmirr
= NULL
;
1040 /* Switch pages if necessary. */
1041 if (!(i
% mrecs_per_page
)) {
1043 ntfs_unmap_page(mft_page
);
1044 ntfs_unmap_page(mirr_page
);
1046 /* Get the $MFT page. */
1047 mft_page
= ntfs_map_page(vol
->mft_ino
->i_mapping
,
1049 if (IS_ERR(mft_page
)) {
1050 ntfs_error(sb
, "Failed to read $MFT.");
1053 kmft
= page_address(mft_page
);
1054 /* Get the $MFTMirr page. */
1055 mirr_page
= ntfs_map_page(vol
->mftmirr_ino
->i_mapping
,
1057 if (IS_ERR(mirr_page
)) {
1058 ntfs_error(sb
, "Failed to read $MFTMirr.");
1061 kmirr
= page_address(mirr_page
);
1064 /* Make sure the record is ok. */
1065 if (ntfs_is_baad_recordp((le32
*)kmft
)) {
1066 ntfs_error(sb
, "Incomplete multi sector transfer "
1067 "detected in mft record %i.", i
);
1069 ntfs_unmap_page(mirr_page
);
1071 ntfs_unmap_page(mft_page
);
1074 if (ntfs_is_baad_recordp((le32
*)kmirr
)) {
1075 ntfs_error(sb
, "Incomplete multi sector transfer "
1076 "detected in mft mirror record %i.", i
);
1079 /* Get the amount of data in the current record. */
1080 bytes
= le32_to_cpu(((MFT_RECORD
*)kmft
)->bytes_in_use
);
1081 if (!bytes
|| bytes
> vol
->mft_record_size
) {
1082 bytes
= le32_to_cpu(((MFT_RECORD
*)kmirr
)->bytes_in_use
);
1083 if (!bytes
|| bytes
> vol
->mft_record_size
)
1084 bytes
= vol
->mft_record_size
;
1086 /* Compare the two records. */
1087 if (memcmp(kmft
, kmirr
, bytes
)) {
1088 ntfs_error(sb
, "$MFT and $MFTMirr (record %i) do not "
1089 "match. Run ntfsfix or chkdsk.", i
);
1092 kmft
+= vol
->mft_record_size
;
1093 kmirr
+= vol
->mft_record_size
;
1094 } while (++i
< vol
->mftmirr_size
);
1095 /* Release the last pages. */
1096 ntfs_unmap_page(mft_page
);
1097 ntfs_unmap_page(mirr_page
);
1099 /* Construct the mft mirror runlist by hand. */
1101 rl2
[0].lcn
= vol
->mftmirr_lcn
;
1102 rl2
[0].length
= (vol
->mftmirr_size
* vol
->mft_record_size
+
1103 vol
->cluster_size
- 1) / vol
->cluster_size
;
1104 rl2
[1].vcn
= rl2
[0].length
;
1105 rl2
[1].lcn
= LCN_ENOENT
;
1108 * Because we have just read all of the mft mirror, we know we have
1109 * mapped the full runlist for it.
1111 mirr_ni
= NTFS_I(vol
->mftmirr_ino
);
1112 down_read(&mirr_ni
->runlist
.lock
);
1113 rl
= mirr_ni
->runlist
.rl
;
1114 /* Compare the two runlists. They must be identical. */
1117 if (rl2
[i
].vcn
!= rl
[i
].vcn
|| rl2
[i
].lcn
!= rl
[i
].lcn
||
1118 rl2
[i
].length
!= rl
[i
].length
) {
1119 ntfs_error(sb
, "$MFTMirr location mismatch. "
1121 up_read(&mirr_ni
->runlist
.lock
);
1124 } while (rl2
[i
++].length
);
1125 up_read(&mirr_ni
->runlist
.lock
);
1126 ntfs_debug("Done.");
1131 * load_and_check_logfile - load and check the logfile inode for a volume
1132 * @vol: ntfs super block describing device whose logfile to load
1134 * Return TRUE on success or FALSE on error.
1136 static BOOL
load_and_check_logfile(ntfs_volume
*vol
,
1137 RESTART_PAGE_HEADER
**rp
)
1139 struct inode
*tmp_ino
;
1141 ntfs_debug("Entering.");
1142 tmp_ino
= ntfs_iget(vol
->sb
, FILE_LogFile
);
1143 if (IS_ERR(tmp_ino
) || is_bad_inode(tmp_ino
)) {
1144 if (!IS_ERR(tmp_ino
))
1146 /* Caller will display error message. */
1149 if (!ntfs_check_logfile(tmp_ino
, rp
)) {
1151 /* ntfs_check_logfile() will have displayed error output. */
1154 NInoSetSparseDisabled(NTFS_I(tmp_ino
));
1155 vol
->logfile_ino
= tmp_ino
;
1156 ntfs_debug("Done.");
1160 #define NTFS_HIBERFIL_HEADER_SIZE 4096
1163 * check_windows_hibernation_status - check if Windows is suspended on a volume
1164 * @vol: ntfs super block of device to check
1166 * Check if Windows is hibernated on the ntfs volume @vol. This is done by
1167 * looking for the file hiberfil.sys in the root directory of the volume. If
1168 * the file is not present Windows is definitely not suspended.
1170 * If hiberfil.sys exists and is less than 4kiB in size it means Windows is
1171 * definitely suspended (this volume is not the system volume). Caveat: on a
1172 * system with many volumes it is possible that the < 4kiB check is bogus but
1173 * for now this should do fine.
1175 * If hiberfil.sys exists and is larger than 4kiB in size, we need to read the
1176 * hiberfil header (which is the first 4kiB). If this begins with "hibr",
1177 * Windows is definitely suspended. If it is completely full of zeroes,
1178 * Windows is definitely not hibernated. Any other case is treated as if
1179 * Windows is suspended. This caters for the above mentioned caveat of a
1180 * system with many volumes where no "hibr" magic would be present and there is
1183 * Return 0 if Windows is not hibernated on the volume, >0 if Windows is
1184 * hibernated on the volume, and -errno on error.
1186 static int check_windows_hibernation_status(ntfs_volume
*vol
)
1193 ntfs_name
*name
= NULL
;
1195 static const ntfschar hiberfil
[13] = { const_cpu_to_le16('h'),
1196 const_cpu_to_le16('i'), const_cpu_to_le16('b'),
1197 const_cpu_to_le16('e'), const_cpu_to_le16('r'),
1198 const_cpu_to_le16('f'), const_cpu_to_le16('i'),
1199 const_cpu_to_le16('l'), const_cpu_to_le16('.'),
1200 const_cpu_to_le16('s'), const_cpu_to_le16('y'),
1201 const_cpu_to_le16('s'), 0 };
1203 ntfs_debug("Entering.");
1205 * Find the inode number for the hibernation file by looking up the
1206 * filename hiberfil.sys in the root directory.
1208 down(&vol
->root_ino
->i_sem
);
1209 mref
= ntfs_lookup_inode_by_name(NTFS_I(vol
->root_ino
), hiberfil
, 12,
1211 up(&vol
->root_ino
->i_sem
);
1212 if (IS_ERR_MREF(mref
)) {
1213 ret
= MREF_ERR(mref
);
1214 /* If the file does not exist, Windows is not hibernated. */
1215 if (ret
== -ENOENT
) {
1216 ntfs_debug("hiberfil.sys not present. Windows is not "
1217 "hibernated on the volume.");
1220 /* A real error occured. */
1221 ntfs_error(vol
->sb
, "Failed to find inode number for "
1225 /* We do not care for the type of match that was found. */
1227 /* Get the inode. */
1228 vi
= ntfs_iget(vol
->sb
, MREF(mref
));
1229 if (IS_ERR(vi
) || is_bad_inode(vi
)) {
1232 ntfs_error(vol
->sb
, "Failed to load hiberfil.sys.");
1233 return IS_ERR(vi
) ? PTR_ERR(vi
) : -EIO
;
1235 if (unlikely(i_size_read(vi
) < NTFS_HIBERFIL_HEADER_SIZE
)) {
1236 ntfs_debug("hiberfil.sys is smaller than 4kiB (0x%llx). "
1237 "Windows is hibernated on the volume. This "
1238 "is not the system volume.", i_size_read(vi
));
1242 page
= ntfs_map_page(vi
->i_mapping
, 0);
1244 ntfs_error(vol
->sb
, "Failed to read from hiberfil.sys.");
1245 ret
= PTR_ERR(page
);
1248 kaddr
= (u32
*)page_address(page
);
1249 if (*(le32
*)kaddr
== const_cpu_to_le32(0x72626968)/*'hibr'*/) {
1250 ntfs_debug("Magic \"hibr\" found in hiberfil.sys. Windows is "
1251 "hibernated on the volume. This is the "
1255 kend
= kaddr
+ NTFS_HIBERFIL_HEADER_SIZE
/sizeof(*kaddr
);
1257 if (unlikely(*kaddr
)) {
1258 ntfs_debug("hiberfil.sys is larger than 4kiB "
1259 "(0x%llx), does not contain the "
1260 "\"hibr\" magic, and does not have a "
1261 "zero header. Windows is hibernated "
1262 "on the volume. This is not the "
1263 "system volume.", i_size_read(vi
));
1266 } while (++kaddr
< kend
);
1267 ntfs_debug("hiberfil.sys contains a zero header. Windows is not "
1268 "hibernated on the volume. This is the system "
1272 ntfs_unmap_page(page
);
1279 * load_and_init_quota - load and setup the quota file for a volume if present
1280 * @vol: ntfs super block describing device whose quota file to load
1282 * Return TRUE on success or FALSE on error. If $Quota is not present, we
1283 * leave vol->quota_ino as NULL and return success.
1285 static BOOL
load_and_init_quota(ntfs_volume
*vol
)
1288 struct inode
*tmp_ino
;
1289 ntfs_name
*name
= NULL
;
1290 static const ntfschar Quota
[7] = { const_cpu_to_le16('$'),
1291 const_cpu_to_le16('Q'), const_cpu_to_le16('u'),
1292 const_cpu_to_le16('o'), const_cpu_to_le16('t'),
1293 const_cpu_to_le16('a'), 0 };
1294 static ntfschar Q
[3] = { const_cpu_to_le16('$'),
1295 const_cpu_to_le16('Q'), 0 };
1297 ntfs_debug("Entering.");
1299 * Find the inode number for the quota file by looking up the filename
1300 * $Quota in the extended system files directory $Extend.
1302 down(&vol
->extend_ino
->i_sem
);
1303 mref
= ntfs_lookup_inode_by_name(NTFS_I(vol
->extend_ino
), Quota
, 6,
1305 up(&vol
->extend_ino
->i_sem
);
1306 if (IS_ERR_MREF(mref
)) {
1308 * If the file does not exist, quotas are disabled and have
1309 * never been enabled on this volume, just return success.
1311 if (MREF_ERR(mref
) == -ENOENT
) {
1312 ntfs_debug("$Quota not present. Volume does not have "
1315 * No need to try to set quotas out of date if they are
1318 NVolSetQuotaOutOfDate(vol
);
1321 /* A real error occured. */
1322 ntfs_error(vol
->sb
, "Failed to find inode number for $Quota.");
1325 /* We do not care for the type of match that was found. */
1327 /* Get the inode. */
1328 tmp_ino
= ntfs_iget(vol
->sb
, MREF(mref
));
1329 if (IS_ERR(tmp_ino
) || is_bad_inode(tmp_ino
)) {
1330 if (!IS_ERR(tmp_ino
))
1332 ntfs_error(vol
->sb
, "Failed to load $Quota.");
1335 vol
->quota_ino
= tmp_ino
;
1336 /* Get the $Q index allocation attribute. */
1337 tmp_ino
= ntfs_index_iget(vol
->quota_ino
, Q
, 2);
1338 if (IS_ERR(tmp_ino
)) {
1339 ntfs_error(vol
->sb
, "Failed to load $Quota/$Q index.");
1342 vol
->quota_q_ino
= tmp_ino
;
1343 ntfs_debug("Done.");
1348 * load_and_init_usnjrnl - load and setup the transaction log if present
1349 * @vol: ntfs super block describing device whose usnjrnl file to load
1351 * Return TRUE on success or FALSE on error.
1353 * If $UsnJrnl is not present or in the process of being disabled, we set
1354 * NVolUsnJrnlStamped() and return success.
1356 * If the $UsnJrnl $DATA/$J attribute has a size equal to the lowest valid usn,
1357 * i.e. transaction logging has only just been enabled or the journal has been
1358 * stamped and nothing has been logged since, we also set NVolUsnJrnlStamped()
1359 * and return success.
1361 static BOOL
load_and_init_usnjrnl(ntfs_volume
*vol
)
1364 struct inode
*tmp_ino
;
1367 ntfs_name
*name
= NULL
;
1369 static const ntfschar UsnJrnl
[9] = { const_cpu_to_le16('$'),
1370 const_cpu_to_le16('U'), const_cpu_to_le16('s'),
1371 const_cpu_to_le16('n'), const_cpu_to_le16('J'),
1372 const_cpu_to_le16('r'), const_cpu_to_le16('n'),
1373 const_cpu_to_le16('l'), 0 };
1374 static ntfschar Max
[5] = { const_cpu_to_le16('$'),
1375 const_cpu_to_le16('M'), const_cpu_to_le16('a'),
1376 const_cpu_to_le16('x'), 0 };
1377 static ntfschar J
[3] = { const_cpu_to_le16('$'),
1378 const_cpu_to_le16('J'), 0 };
1380 ntfs_debug("Entering.");
1382 * Find the inode number for the transaction log file by looking up the
1383 * filename $UsnJrnl in the extended system files directory $Extend.
1385 down(&vol
->extend_ino
->i_sem
);
1386 mref
= ntfs_lookup_inode_by_name(NTFS_I(vol
->extend_ino
), UsnJrnl
, 8,
1388 up(&vol
->extend_ino
->i_sem
);
1389 if (IS_ERR_MREF(mref
)) {
1391 * If the file does not exist, transaction logging is disabled,
1392 * just return success.
1394 if (MREF_ERR(mref
) == -ENOENT
) {
1395 ntfs_debug("$UsnJrnl not present. Volume does not "
1396 "have transaction logging enabled.");
1399 * No need to try to stamp the transaction log if
1400 * transaction logging is not enabled.
1402 NVolSetUsnJrnlStamped(vol
);
1405 /* A real error occured. */
1406 ntfs_error(vol
->sb
, "Failed to find inode number for "
1410 /* We do not care for the type of match that was found. */
1412 /* Get the inode. */
1413 tmp_ino
= ntfs_iget(vol
->sb
, MREF(mref
));
1414 if (unlikely(IS_ERR(tmp_ino
) || is_bad_inode(tmp_ino
))) {
1415 if (!IS_ERR(tmp_ino
))
1417 ntfs_error(vol
->sb
, "Failed to load $UsnJrnl.");
1420 vol
->usnjrnl_ino
= tmp_ino
;
1422 * If the transaction log is in the process of being deleted, we can
1425 if (unlikely(vol
->vol_flags
& VOLUME_DELETE_USN_UNDERWAY
)) {
1426 ntfs_debug("$UsnJrnl in the process of being disabled. "
1427 "Volume does not have transaction logging "
1431 /* Get the $DATA/$Max attribute. */
1432 tmp_ino
= ntfs_attr_iget(vol
->usnjrnl_ino
, AT_DATA
, Max
, 4);
1433 if (IS_ERR(tmp_ino
)) {
1434 ntfs_error(vol
->sb
, "Failed to load $UsnJrnl/$DATA/$Max "
1438 vol
->usnjrnl_max_ino
= tmp_ino
;
1439 if (unlikely(i_size_read(tmp_ino
) < sizeof(USN_HEADER
))) {
1440 ntfs_error(vol
->sb
, "Found corrupt $UsnJrnl/$DATA/$Max "
1441 "attribute (size is 0x%llx but should be at "
1442 "least 0x%x bytes).", i_size_read(tmp_ino
),
1443 sizeof(USN_HEADER
));
1446 /* Get the $DATA/$J attribute. */
1447 tmp_ino
= ntfs_attr_iget(vol
->usnjrnl_ino
, AT_DATA
, J
, 2);
1448 if (IS_ERR(tmp_ino
)) {
1449 ntfs_error(vol
->sb
, "Failed to load $UsnJrnl/$DATA/$J "
1453 vol
->usnjrnl_j_ino
= tmp_ino
;
1454 /* Verify $J is non-resident and sparse. */
1455 tmp_ni
= NTFS_I(vol
->usnjrnl_j_ino
);
1456 if (unlikely(!NInoNonResident(tmp_ni
) || !NInoSparse(tmp_ni
))) {
1457 ntfs_error(vol
->sb
, "$UsnJrnl/$DATA/$J attribute is resident "
1458 "and/or not sparse.");
1461 /* Read the USN_HEADER from $DATA/$Max. */
1462 page
= ntfs_map_page(vol
->usnjrnl_max_ino
->i_mapping
, 0);
1464 ntfs_error(vol
->sb
, "Failed to read from $UsnJrnl/$DATA/$Max "
1468 uh
= (USN_HEADER
*)page_address(page
);
1469 /* Sanity check the $Max. */
1470 if (unlikely(sle64_to_cpu(uh
->allocation_delta
) >
1471 sle64_to_cpu(uh
->maximum_size
))) {
1472 ntfs_error(vol
->sb
, "Allocation delta (0x%llx) exceeds "
1473 "maximum size (0x%llx). $UsnJrnl is corrupt.",
1474 (long long)sle64_to_cpu(uh
->allocation_delta
),
1475 (long long)sle64_to_cpu(uh
->maximum_size
));
1476 ntfs_unmap_page(page
);
1480 * If the transaction log has been stamped and nothing has been written
1481 * to it since, we do not need to stamp it.
1483 if (unlikely(sle64_to_cpu(uh
->lowest_valid_usn
) >=
1484 i_size_read(vol
->usnjrnl_j_ino
))) {
1485 if (likely(sle64_to_cpu(uh
->lowest_valid_usn
) ==
1486 i_size_read(vol
->usnjrnl_j_ino
))) {
1487 ntfs_unmap_page(page
);
1488 ntfs_debug("$UsnJrnl is enabled but nothing has been "
1489 "logged since it was last stamped. "
1490 "Treating this as if the volume does "
1491 "not have transaction logging "
1495 ntfs_error(vol
->sb
, "$UsnJrnl has lowest valid usn (0x%llx) "
1496 "which is out of bounds (0x%llx). $UsnJrnl "
1498 (long long)sle64_to_cpu(uh
->lowest_valid_usn
),
1499 i_size_read(vol
->usnjrnl_j_ino
));
1500 ntfs_unmap_page(page
);
1503 ntfs_unmap_page(page
);
1504 ntfs_debug("Done.");
1509 * load_and_init_attrdef - load the attribute definitions table for a volume
1510 * @vol: ntfs super block describing device whose attrdef to load
1512 * Return TRUE on success or FALSE on error.
1514 static BOOL
load_and_init_attrdef(ntfs_volume
*vol
)
1517 struct super_block
*sb
= vol
->sb
;
1520 pgoff_t index
, max_index
;
1523 ntfs_debug("Entering.");
1524 /* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */
1525 ino
= ntfs_iget(sb
, FILE_AttrDef
);
1526 if (IS_ERR(ino
) || is_bad_inode(ino
)) {
1531 NInoSetSparseDisabled(NTFS_I(ino
));
1532 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
1533 i_size
= i_size_read(ino
);
1534 if (i_size
<= 0 || i_size
> 0x7fffffff)
1536 vol
->attrdef
= (ATTR_DEF
*)ntfs_malloc_nofs(i_size
);
1540 max_index
= i_size
>> PAGE_CACHE_SHIFT
;
1541 size
= PAGE_CACHE_SIZE
;
1542 while (index
< max_index
) {
1543 /* Read the attrdef table and copy it into the linear buffer. */
1544 read_partial_attrdef_page
:
1545 page
= ntfs_map_page(ino
->i_mapping
, index
);
1547 goto free_iput_failed
;
1548 memcpy((u8
*)vol
->attrdef
+ (index
++ << PAGE_CACHE_SHIFT
),
1549 page_address(page
), size
);
1550 ntfs_unmap_page(page
);
1552 if (size
== PAGE_CACHE_SIZE
) {
1553 size
= i_size
& ~PAGE_CACHE_MASK
;
1555 goto read_partial_attrdef_page
;
1557 vol
->attrdef_size
= i_size
;
1558 ntfs_debug("Read %llu bytes from $AttrDef.", i_size
);
1562 ntfs_free(vol
->attrdef
);
1563 vol
->attrdef
= NULL
;
1567 ntfs_error(sb
, "Failed to initialize attribute definition table.");
1571 #endif /* NTFS_RW */
1574 * load_and_init_upcase - load the upcase table for an ntfs volume
1575 * @vol: ntfs super block describing device whose upcase to load
1577 * Return TRUE on success or FALSE on error.
1579 static BOOL
load_and_init_upcase(ntfs_volume
*vol
)
1582 struct super_block
*sb
= vol
->sb
;
1585 pgoff_t index
, max_index
;
1589 ntfs_debug("Entering.");
1590 /* Read upcase table and setup vol->upcase and vol->upcase_len. */
1591 ino
= ntfs_iget(sb
, FILE_UpCase
);
1592 if (IS_ERR(ino
) || is_bad_inode(ino
)) {
1598 * The upcase size must not be above 64k Unicode characters, must not
1599 * be zero and must be a multiple of sizeof(ntfschar).
1601 i_size
= i_size_read(ino
);
1602 if (!i_size
|| i_size
& (sizeof(ntfschar
) - 1) ||
1603 i_size
> 64ULL * 1024 * sizeof(ntfschar
))
1604 goto iput_upcase_failed
;
1605 vol
->upcase
= (ntfschar
*)ntfs_malloc_nofs(i_size
);
1607 goto iput_upcase_failed
;
1609 max_index
= i_size
>> PAGE_CACHE_SHIFT
;
1610 size
= PAGE_CACHE_SIZE
;
1611 while (index
< max_index
) {
1612 /* Read the upcase table and copy it into the linear buffer. */
1613 read_partial_upcase_page
:
1614 page
= ntfs_map_page(ino
->i_mapping
, index
);
1616 goto iput_upcase_failed
;
1617 memcpy((char*)vol
->upcase
+ (index
++ << PAGE_CACHE_SHIFT
),
1618 page_address(page
), size
);
1619 ntfs_unmap_page(page
);
1621 if (size
== PAGE_CACHE_SIZE
) {
1622 size
= i_size
& ~PAGE_CACHE_MASK
;
1624 goto read_partial_upcase_page
;
1626 vol
->upcase_len
= i_size
>> UCHAR_T_SIZE_BITS
;
1627 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
1628 i_size
, 64 * 1024 * sizeof(ntfschar
));
1631 if (!default_upcase
) {
1632 ntfs_debug("Using volume specified $UpCase since default is "
1637 max
= default_upcase_len
;
1638 if (max
> vol
->upcase_len
)
1639 max
= vol
->upcase_len
;
1640 for (i
= 0; i
< max
; i
++)
1641 if (vol
->upcase
[i
] != default_upcase
[i
])
1644 ntfs_free(vol
->upcase
);
1645 vol
->upcase
= default_upcase
;
1646 vol
->upcase_len
= max
;
1647 ntfs_nr_upcase_users
++;
1649 ntfs_debug("Volume specified $UpCase matches default. Using "
1654 ntfs_debug("Using volume specified $UpCase since it does not match "
1659 ntfs_free(vol
->upcase
);
1663 if (default_upcase
) {
1664 vol
->upcase
= default_upcase
;
1665 vol
->upcase_len
= default_upcase_len
;
1666 ntfs_nr_upcase_users
++;
1668 ntfs_error(sb
, "Failed to load $UpCase from the volume. Using "
1673 ntfs_error(sb
, "Failed to initialize upcase table.");
1678 * load_system_files - open the system files using normal functions
1679 * @vol: ntfs super block describing device whose system files to load
1681 * Open the system files with normal access functions and complete setting up
1682 * the ntfs super block @vol.
1684 * Return TRUE on success or FALSE on error.
1686 static BOOL
load_system_files(ntfs_volume
*vol
)
1688 struct super_block
*sb
= vol
->sb
;
1690 VOLUME_INFORMATION
*vi
;
1691 ntfs_attr_search_ctx
*ctx
;
1693 RESTART_PAGE_HEADER
*rp
;
1695 #endif /* NTFS_RW */
1697 ntfs_debug("Entering.");
1699 /* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */
1700 if (!load_and_init_mft_mirror(vol
) || !check_mft_mirror(vol
)) {
1701 static const char *es1
= "Failed to load $MFTMirr";
1702 static const char *es2
= "$MFTMirr does not match $MFT";
1703 static const char *es3
= ". Run ntfsfix and/or chkdsk.";
1705 /* If a read-write mount, convert it to a read-only mount. */
1706 if (!(sb
->s_flags
& MS_RDONLY
)) {
1707 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1708 ON_ERRORS_CONTINUE
))) {
1709 ntfs_error(sb
, "%s and neither on_errors="
1710 "continue nor on_errors="
1711 "remount-ro was specified%s",
1712 !vol
->mftmirr_ino
? es1
: es2
,
1714 goto iput_mirr_err_out
;
1716 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1717 ntfs_error(sb
, "%s. Mounting read-only%s",
1718 !vol
->mftmirr_ino
? es1
: es2
, es3
);
1720 ntfs_warning(sb
, "%s. Will not be able to remount "
1722 !vol
->mftmirr_ino
? es1
: es2
, es3
);
1723 /* This will prevent a read-write remount. */
1726 #endif /* NTFS_RW */
1727 /* Get mft bitmap attribute inode. */
1728 vol
->mftbmp_ino
= ntfs_attr_iget(vol
->mft_ino
, AT_BITMAP
, NULL
, 0);
1729 if (IS_ERR(vol
->mftbmp_ino
)) {
1730 ntfs_error(sb
, "Failed to load $MFT/$BITMAP attribute.");
1731 goto iput_mirr_err_out
;
1733 /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */
1734 if (!load_and_init_upcase(vol
))
1735 goto iput_mftbmp_err_out
;
1738 * Read attribute definitions table and setup @vol->attrdef and
1739 * @vol->attrdef_size.
1741 if (!load_and_init_attrdef(vol
))
1742 goto iput_upcase_err_out
;
1743 #endif /* NTFS_RW */
1745 * Get the cluster allocation bitmap inode and verify the size, no
1746 * need for any locking at this stage as we are already running
1747 * exclusively as we are mount in progress task.
1749 vol
->lcnbmp_ino
= ntfs_iget(sb
, FILE_Bitmap
);
1750 if (IS_ERR(vol
->lcnbmp_ino
) || is_bad_inode(vol
->lcnbmp_ino
)) {
1751 if (!IS_ERR(vol
->lcnbmp_ino
))
1752 iput(vol
->lcnbmp_ino
);
1755 NInoSetSparseDisabled(NTFS_I(vol
->lcnbmp_ino
));
1756 if ((vol
->nr_clusters
+ 7) >> 3 > i_size_read(vol
->lcnbmp_ino
)) {
1757 iput(vol
->lcnbmp_ino
);
1759 ntfs_error(sb
, "Failed to load $Bitmap.");
1760 goto iput_attrdef_err_out
;
1763 * Get the volume inode and setup our cache of the volume flags and
1766 vol
->vol_ino
= ntfs_iget(sb
, FILE_Volume
);
1767 if (IS_ERR(vol
->vol_ino
) || is_bad_inode(vol
->vol_ino
)) {
1768 if (!IS_ERR(vol
->vol_ino
))
1771 ntfs_error(sb
, "Failed to load $Volume.");
1772 goto iput_lcnbmp_err_out
;
1774 m
= map_mft_record(NTFS_I(vol
->vol_ino
));
1780 if (!(ctx
= ntfs_attr_get_search_ctx(NTFS_I(vol
->vol_ino
), m
))) {
1781 ntfs_error(sb
, "Failed to get attribute search context.");
1782 goto get_ctx_vol_failed
;
1784 if (ntfs_attr_lookup(AT_VOLUME_INFORMATION
, NULL
, 0, 0, 0, NULL
, 0,
1785 ctx
) || ctx
->attr
->non_resident
|| ctx
->attr
->flags
) {
1787 ntfs_attr_put_search_ctx(ctx
);
1789 unmap_mft_record(NTFS_I(vol
->vol_ino
));
1790 goto iput_volume_failed
;
1792 vi
= (VOLUME_INFORMATION
*)((char*)ctx
->attr
+
1793 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
));
1794 /* Some bounds checks. */
1795 if ((u8
*)vi
< (u8
*)ctx
->attr
|| (u8
*)vi
+
1796 le32_to_cpu(ctx
->attr
->data
.resident
.value_length
) >
1797 (u8
*)ctx
->attr
+ le32_to_cpu(ctx
->attr
->length
))
1799 /* Copy the volume flags and version to the ntfs_volume structure. */
1800 vol
->vol_flags
= vi
->flags
;
1801 vol
->major_ver
= vi
->major_ver
;
1802 vol
->minor_ver
= vi
->minor_ver
;
1803 ntfs_attr_put_search_ctx(ctx
);
1804 unmap_mft_record(NTFS_I(vol
->vol_ino
));
1805 printk(KERN_INFO
"NTFS volume version %i.%i.\n", vol
->major_ver
,
1807 if (vol
->major_ver
< 3 && NVolSparseEnabled(vol
)) {
1808 ntfs_warning(vol
->sb
, "Disabling sparse support due to NTFS "
1809 "volume version %i.%i (need at least version "
1810 "3.0).", vol
->major_ver
, vol
->minor_ver
);
1811 NVolClearSparseEnabled(vol
);
1814 /* Make sure that no unsupported volume flags are set. */
1815 if (vol
->vol_flags
& VOLUME_MUST_MOUNT_RO_MASK
) {
1816 static const char *es1a
= "Volume is dirty";
1817 static const char *es1b
= "Volume has unsupported flags set";
1818 static const char *es2
= ". Run chkdsk and mount in Windows.";
1821 es1
= vol
->vol_flags
& VOLUME_IS_DIRTY
? es1a
: es1b
;
1822 /* If a read-write mount, convert it to a read-only mount. */
1823 if (!(sb
->s_flags
& MS_RDONLY
)) {
1824 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1825 ON_ERRORS_CONTINUE
))) {
1826 ntfs_error(sb
, "%s and neither on_errors="
1827 "continue nor on_errors="
1828 "remount-ro was specified%s",
1830 goto iput_vol_err_out
;
1832 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1833 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
1835 ntfs_warning(sb
, "%s. Will not be able to remount "
1836 "read-write%s", es1
, es2
);
1838 * Do not set NVolErrors() because ntfs_remount() re-checks the
1839 * flags which we need to do in case any flags have changed.
1843 * Get the inode for the logfile, check it and determine if the volume
1844 * was shutdown cleanly.
1847 if (!load_and_check_logfile(vol
, &rp
) ||
1848 !ntfs_is_logfile_clean(vol
->logfile_ino
, rp
)) {
1849 static const char *es1a
= "Failed to load $LogFile";
1850 static const char *es1b
= "$LogFile is not clean";
1851 static const char *es2
= ". Mount in Windows.";
1854 es1
= !vol
->logfile_ino
? es1a
: es1b
;
1855 /* If a read-write mount, convert it to a read-only mount. */
1856 if (!(sb
->s_flags
& MS_RDONLY
)) {
1857 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1858 ON_ERRORS_CONTINUE
))) {
1859 ntfs_error(sb
, "%s and neither on_errors="
1860 "continue nor on_errors="
1861 "remount-ro was specified%s",
1863 if (vol
->logfile_ino
) {
1867 goto iput_logfile_err_out
;
1869 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1870 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
1872 ntfs_warning(sb
, "%s. Will not be able to remount "
1873 "read-write%s", es1
, es2
);
1874 /* This will prevent a read-write remount. */
1878 #endif /* NTFS_RW */
1879 /* Get the root directory inode so we can do path lookups. */
1880 vol
->root_ino
= ntfs_iget(sb
, FILE_root
);
1881 if (IS_ERR(vol
->root_ino
) || is_bad_inode(vol
->root_ino
)) {
1882 if (!IS_ERR(vol
->root_ino
))
1883 iput(vol
->root_ino
);
1884 ntfs_error(sb
, "Failed to load root directory.");
1885 goto iput_logfile_err_out
;
1889 * Check if Windows is suspended to disk on the target volume. If it
1890 * is hibernated, we must not write *anything* to the disk so set
1891 * NVolErrors() without setting the dirty volume flag and mount
1892 * read-only. This will prevent read-write remounting and it will also
1893 * prevent all writes.
1895 err
= check_windows_hibernation_status(vol
);
1896 if (unlikely(err
)) {
1897 static const char *es1a
= "Failed to determine if Windows is "
1899 static const char *es1b
= "Windows is hibernated";
1900 static const char *es2
= ". Run chkdsk.";
1903 es1
= err
< 0 ? es1a
: es1b
;
1904 /* If a read-write mount, convert it to a read-only mount. */
1905 if (!(sb
->s_flags
& MS_RDONLY
)) {
1906 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1907 ON_ERRORS_CONTINUE
))) {
1908 ntfs_error(sb
, "%s and neither on_errors="
1909 "continue nor on_errors="
1910 "remount-ro was specified%s",
1912 goto iput_root_err_out
;
1914 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1915 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
1917 ntfs_warning(sb
, "%s. Will not be able to remount "
1918 "read-write%s", es1
, es2
);
1919 /* This will prevent a read-write remount. */
1922 /* If (still) a read-write mount, mark the volume dirty. */
1923 if (!(sb
->s_flags
& MS_RDONLY
) &&
1924 ntfs_set_volume_flags(vol
, VOLUME_IS_DIRTY
)) {
1925 static const char *es1
= "Failed to set dirty bit in volume "
1926 "information flags";
1927 static const char *es2
= ". Run chkdsk.";
1929 /* Convert to a read-only mount. */
1930 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1931 ON_ERRORS_CONTINUE
))) {
1932 ntfs_error(sb
, "%s and neither on_errors=continue nor "
1933 "on_errors=remount-ro was specified%s",
1935 goto iput_root_err_out
;
1937 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
1938 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1940 * Do not set NVolErrors() because ntfs_remount() might manage
1941 * to set the dirty flag in which case all would be well.
1945 // TODO: Enable this code once we start modifying anything that is
1946 // different between NTFS 1.2 and 3.x...
1948 * If (still) a read-write mount, set the NT4 compatibility flag on
1949 * newer NTFS version volumes.
1951 if (!(sb
->s_flags
& MS_RDONLY
) && (vol
->major_ver
> 1) &&
1952 ntfs_set_volume_flags(vol
, VOLUME_MOUNTED_ON_NT4
)) {
1953 static const char *es1
= "Failed to set NT4 compatibility flag";
1954 static const char *es2
= ". Run chkdsk.";
1956 /* Convert to a read-only mount. */
1957 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1958 ON_ERRORS_CONTINUE
))) {
1959 ntfs_error(sb
, "%s and neither on_errors=continue nor "
1960 "on_errors=remount-ro was specified%s",
1962 goto iput_root_err_out
;
1964 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
1965 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1969 /* If (still) a read-write mount, empty the logfile. */
1970 if (!(sb
->s_flags
& MS_RDONLY
) &&
1971 !ntfs_empty_logfile(vol
->logfile_ino
)) {
1972 static const char *es1
= "Failed to empty $LogFile";
1973 static const char *es2
= ". Mount in Windows.";
1975 /* Convert to a read-only mount. */
1976 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1977 ON_ERRORS_CONTINUE
))) {
1978 ntfs_error(sb
, "%s and neither on_errors=continue nor "
1979 "on_errors=remount-ro was specified%s",
1981 goto iput_root_err_out
;
1983 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
1984 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1987 #endif /* NTFS_RW */
1988 /* If on NTFS versions before 3.0, we are done. */
1989 if (unlikely(vol
->major_ver
< 3))
1991 /* NTFS 3.0+ specific initialization. */
1992 /* Get the security descriptors inode. */
1993 vol
->secure_ino
= ntfs_iget(sb
, FILE_Secure
);
1994 if (IS_ERR(vol
->secure_ino
) || is_bad_inode(vol
->secure_ino
)) {
1995 if (!IS_ERR(vol
->secure_ino
))
1996 iput(vol
->secure_ino
);
1997 ntfs_error(sb
, "Failed to load $Secure.");
1998 goto iput_root_err_out
;
2000 // TODO: Initialize security.
2001 /* Get the extended system files' directory inode. */
2002 vol
->extend_ino
= ntfs_iget(sb
, FILE_Extend
);
2003 if (IS_ERR(vol
->extend_ino
) || is_bad_inode(vol
->extend_ino
)) {
2004 if (!IS_ERR(vol
->extend_ino
))
2005 iput(vol
->extend_ino
);
2006 ntfs_error(sb
, "Failed to load $Extend.");
2007 goto iput_sec_err_out
;
2010 /* Find the quota file, load it if present, and set it up. */
2011 if (!load_and_init_quota(vol
)) {
2012 static const char *es1
= "Failed to load $Quota";
2013 static const char *es2
= ". Run chkdsk.";
2015 /* If a read-write mount, convert it to a read-only mount. */
2016 if (!(sb
->s_flags
& MS_RDONLY
)) {
2017 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
2018 ON_ERRORS_CONTINUE
))) {
2019 ntfs_error(sb
, "%s and neither on_errors="
2020 "continue nor on_errors="
2021 "remount-ro was specified%s",
2023 goto iput_quota_err_out
;
2025 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
2026 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
2028 ntfs_warning(sb
, "%s. Will not be able to remount "
2029 "read-write%s", es1
, es2
);
2030 /* This will prevent a read-write remount. */
2033 /* If (still) a read-write mount, mark the quotas out of date. */
2034 if (!(sb
->s_flags
& MS_RDONLY
) &&
2035 !ntfs_mark_quotas_out_of_date(vol
)) {
2036 static const char *es1
= "Failed to mark quotas out of date";
2037 static const char *es2
= ". Run chkdsk.";
2039 /* Convert to a read-only mount. */
2040 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
2041 ON_ERRORS_CONTINUE
))) {
2042 ntfs_error(sb
, "%s and neither on_errors=continue nor "
2043 "on_errors=remount-ro was specified%s",
2045 goto iput_quota_err_out
;
2047 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
2048 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
2052 * Find the transaction log file ($UsnJrnl), load it if present, check
2053 * it, and set it up.
2055 if (!load_and_init_usnjrnl(vol
)) {
2056 static const char *es1
= "Failed to load $UsnJrnl";
2057 static const char *es2
= ". Run chkdsk.";
2059 /* If a read-write mount, convert it to a read-only mount. */
2060 if (!(sb
->s_flags
& MS_RDONLY
)) {
2061 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
2062 ON_ERRORS_CONTINUE
))) {
2063 ntfs_error(sb
, "%s and neither on_errors="
2064 "continue nor on_errors="
2065 "remount-ro was specified%s",
2067 goto iput_usnjrnl_err_out
;
2069 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
2070 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
2072 ntfs_warning(sb
, "%s. Will not be able to remount "
2073 "read-write%s", es1
, es2
);
2074 /* This will prevent a read-write remount. */
2077 /* If (still) a read-write mount, stamp the transaction log. */
2078 if (!(sb
->s_flags
& MS_RDONLY
) && !ntfs_stamp_usnjrnl(vol
)) {
2079 static const char *es1
= "Failed to stamp transaction log "
2081 static const char *es2
= ". Run chkdsk.";
2083 /* Convert to a read-only mount. */
2084 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
2085 ON_ERRORS_CONTINUE
))) {
2086 ntfs_error(sb
, "%s and neither on_errors=continue nor "
2087 "on_errors=remount-ro was specified%s",
2089 goto iput_usnjrnl_err_out
;
2091 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
2092 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
2095 #endif /* NTFS_RW */
2098 iput_usnjrnl_err_out
:
2099 if (vol
->usnjrnl_j_ino
)
2100 iput(vol
->usnjrnl_j_ino
);
2101 if (vol
->usnjrnl_max_ino
)
2102 iput(vol
->usnjrnl_max_ino
);
2103 if (vol
->usnjrnl_ino
)
2104 iput(vol
->usnjrnl_ino
);
2106 if (vol
->quota_q_ino
)
2107 iput(vol
->quota_q_ino
);
2109 iput(vol
->quota_ino
);
2110 iput(vol
->extend_ino
);
2111 #endif /* NTFS_RW */
2113 iput(vol
->secure_ino
);
2115 iput(vol
->root_ino
);
2116 iput_logfile_err_out
:
2118 if (vol
->logfile_ino
)
2119 iput(vol
->logfile_ino
);
2121 #endif /* NTFS_RW */
2123 iput_lcnbmp_err_out
:
2124 iput(vol
->lcnbmp_ino
);
2125 iput_attrdef_err_out
:
2126 vol
->attrdef_size
= 0;
2128 ntfs_free(vol
->attrdef
);
2129 vol
->attrdef
= NULL
;
2132 iput_upcase_err_out
:
2133 #endif /* NTFS_RW */
2134 vol
->upcase_len
= 0;
2136 if (vol
->upcase
== default_upcase
) {
2137 ntfs_nr_upcase_users
--;
2142 ntfs_free(vol
->upcase
);
2145 iput_mftbmp_err_out
:
2146 iput(vol
->mftbmp_ino
);
2149 if (vol
->mftmirr_ino
)
2150 iput(vol
->mftmirr_ino
);
2151 #endif /* NTFS_RW */
2156 * ntfs_put_super - called by the vfs to unmount a volume
2157 * @sb: vfs superblock of volume to unmount
2159 * ntfs_put_super() is called by the VFS (from fs/super.c::do_umount()) when
2160 * the volume is being unmounted (umount system call has been invoked) and it
2161 * releases all inodes and memory belonging to the NTFS specific part of the
2164 static void ntfs_put_super(struct super_block
*sb
)
2166 ntfs_volume
*vol
= NTFS_SB(sb
);
2168 ntfs_debug("Entering.");
2171 * Commit all inodes while they are still open in case some of them
2172 * cause others to be dirtied.
2174 ntfs_commit_inode(vol
->vol_ino
);
2176 /* NTFS 3.0+ specific. */
2177 if (vol
->major_ver
>= 3) {
2178 if (vol
->usnjrnl_j_ino
)
2179 ntfs_commit_inode(vol
->usnjrnl_j_ino
);
2180 if (vol
->usnjrnl_max_ino
)
2181 ntfs_commit_inode(vol
->usnjrnl_max_ino
);
2182 if (vol
->usnjrnl_ino
)
2183 ntfs_commit_inode(vol
->usnjrnl_ino
);
2184 if (vol
->quota_q_ino
)
2185 ntfs_commit_inode(vol
->quota_q_ino
);
2187 ntfs_commit_inode(vol
->quota_ino
);
2188 if (vol
->extend_ino
)
2189 ntfs_commit_inode(vol
->extend_ino
);
2190 if (vol
->secure_ino
)
2191 ntfs_commit_inode(vol
->secure_ino
);
2194 ntfs_commit_inode(vol
->root_ino
);
2196 down_write(&vol
->lcnbmp_lock
);
2197 ntfs_commit_inode(vol
->lcnbmp_ino
);
2198 up_write(&vol
->lcnbmp_lock
);
2200 down_write(&vol
->mftbmp_lock
);
2201 ntfs_commit_inode(vol
->mftbmp_ino
);
2202 up_write(&vol
->mftbmp_lock
);
2204 if (vol
->logfile_ino
)
2205 ntfs_commit_inode(vol
->logfile_ino
);
2207 if (vol
->mftmirr_ino
)
2208 ntfs_commit_inode(vol
->mftmirr_ino
);
2209 ntfs_commit_inode(vol
->mft_ino
);
2212 * If a read-write mount and no volume errors have occured, mark the
2213 * volume clean. Also, re-commit all affected inodes.
2215 if (!(sb
->s_flags
& MS_RDONLY
)) {
2216 if (!NVolErrors(vol
)) {
2217 if (ntfs_clear_volume_flags(vol
, VOLUME_IS_DIRTY
))
2218 ntfs_warning(sb
, "Failed to clear dirty bit "
2219 "in volume information "
2220 "flags. Run chkdsk.");
2221 ntfs_commit_inode(vol
->vol_ino
);
2222 ntfs_commit_inode(vol
->root_ino
);
2223 if (vol
->mftmirr_ino
)
2224 ntfs_commit_inode(vol
->mftmirr_ino
);
2225 ntfs_commit_inode(vol
->mft_ino
);
2227 ntfs_warning(sb
, "Volume has errors. Leaving volume "
2228 "marked dirty. Run chkdsk.");
2231 #endif /* NTFS_RW */
2234 vol
->vol_ino
= NULL
;
2236 /* NTFS 3.0+ specific clean up. */
2237 if (vol
->major_ver
>= 3) {
2239 if (vol
->usnjrnl_j_ino
) {
2240 iput(vol
->usnjrnl_j_ino
);
2241 vol
->usnjrnl_j_ino
= NULL
;
2243 if (vol
->usnjrnl_max_ino
) {
2244 iput(vol
->usnjrnl_max_ino
);
2245 vol
->usnjrnl_max_ino
= NULL
;
2247 if (vol
->usnjrnl_ino
) {
2248 iput(vol
->usnjrnl_ino
);
2249 vol
->usnjrnl_ino
= NULL
;
2251 if (vol
->quota_q_ino
) {
2252 iput(vol
->quota_q_ino
);
2253 vol
->quota_q_ino
= NULL
;
2255 if (vol
->quota_ino
) {
2256 iput(vol
->quota_ino
);
2257 vol
->quota_ino
= NULL
;
2259 #endif /* NTFS_RW */
2260 if (vol
->extend_ino
) {
2261 iput(vol
->extend_ino
);
2262 vol
->extend_ino
= NULL
;
2264 if (vol
->secure_ino
) {
2265 iput(vol
->secure_ino
);
2266 vol
->secure_ino
= NULL
;
2270 iput(vol
->root_ino
);
2271 vol
->root_ino
= NULL
;
2273 down_write(&vol
->lcnbmp_lock
);
2274 iput(vol
->lcnbmp_ino
);
2275 vol
->lcnbmp_ino
= NULL
;
2276 up_write(&vol
->lcnbmp_lock
);
2278 down_write(&vol
->mftbmp_lock
);
2279 iput(vol
->mftbmp_ino
);
2280 vol
->mftbmp_ino
= NULL
;
2281 up_write(&vol
->mftbmp_lock
);
2284 if (vol
->logfile_ino
) {
2285 iput(vol
->logfile_ino
);
2286 vol
->logfile_ino
= NULL
;
2288 if (vol
->mftmirr_ino
) {
2289 /* Re-commit the mft mirror and mft just in case. */
2290 ntfs_commit_inode(vol
->mftmirr_ino
);
2291 ntfs_commit_inode(vol
->mft_ino
);
2292 iput(vol
->mftmirr_ino
);
2293 vol
->mftmirr_ino
= NULL
;
2296 * If any dirty inodes are left, throw away all mft data page cache
2297 * pages to allow a clean umount. This should never happen any more
2298 * due to mft.c::ntfs_mft_writepage() cleaning all the dirty pages as
2299 * the underlying mft records are written out and cleaned. If it does,
2300 * happen anyway, we want to know...
2302 ntfs_commit_inode(vol
->mft_ino
);
2303 write_inode_now(vol
->mft_ino
, 1);
2304 if (!list_empty(&sb
->s_dirty
)) {
2305 const char *s1
, *s2
;
2307 down(&vol
->mft_ino
->i_sem
);
2308 truncate_inode_pages(vol
->mft_ino
->i_mapping
, 0);
2309 up(&vol
->mft_ino
->i_sem
);
2310 write_inode_now(vol
->mft_ino
, 1);
2311 if (!list_empty(&sb
->s_dirty
)) {
2312 static const char *_s1
= "inodes";
2313 static const char *_s2
= "";
2317 static const char *_s1
= "mft pages";
2318 static const char *_s2
= "They have been thrown "
2323 ntfs_error(sb
, "Dirty %s found at umount time. %sYou should "
2324 "run chkdsk. Please email "
2325 "linux-ntfs-dev@lists.sourceforge.net and say "
2326 "that you saw this message. Thank you.", s1
,
2329 #endif /* NTFS_RW */
2332 vol
->mft_ino
= NULL
;
2334 /* Throw away the table of attribute definitions. */
2335 vol
->attrdef_size
= 0;
2337 ntfs_free(vol
->attrdef
);
2338 vol
->attrdef
= NULL
;
2340 vol
->upcase_len
= 0;
2342 * Destroy the global default upcase table if necessary. Also decrease
2343 * the number of upcase users if we are a user.
2346 if (vol
->upcase
== default_upcase
) {
2347 ntfs_nr_upcase_users
--;
2350 if (!ntfs_nr_upcase_users
&& default_upcase
) {
2351 ntfs_free(default_upcase
);
2352 default_upcase
= NULL
;
2354 if (vol
->cluster_size
<= 4096 && !--ntfs_nr_compression_users
)
2355 free_compression_buffers();
2358 ntfs_free(vol
->upcase
);
2362 unload_nls(vol
->nls_map
);
2363 vol
->nls_map
= NULL
;
2365 sb
->s_fs_info
= NULL
;
2371 * get_nr_free_clusters - return the number of free clusters on a volume
2372 * @vol: ntfs volume for which to obtain free cluster count
2374 * Calculate the number of free clusters on the mounted NTFS volume @vol. We
2375 * actually calculate the number of clusters in use instead because this
2376 * allows us to not care about partial pages as these will be just zero filled
2377 * and hence not be counted as allocated clusters.
2379 * The only particularity is that clusters beyond the end of the logical ntfs
2380 * volume will be marked as allocated to prevent errors which means we have to
2381 * discount those at the end. This is important as the cluster bitmap always
2382 * has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside
2383 * the logical volume and marked in use when they are not as they do not exist.
2385 * If any pages cannot be read we assume all clusters in the erroring pages are
2386 * in use. This means we return an underestimate on errors which is better than
2389 static s64
get_nr_free_clusters(ntfs_volume
*vol
)
2391 s64 nr_free
= vol
->nr_clusters
;
2393 struct address_space
*mapping
= vol
->lcnbmp_ino
->i_mapping
;
2394 filler_t
*readpage
= (filler_t
*)mapping
->a_ops
->readpage
;
2396 pgoff_t index
, max_index
;
2398 ntfs_debug("Entering.");
2399 /* Serialize accesses to the cluster bitmap. */
2400 down_read(&vol
->lcnbmp_lock
);
2402 * Convert the number of bits into bytes rounded up, then convert into
2403 * multiples of PAGE_CACHE_SIZE, rounding up so that if we have one
2404 * full and one partial page max_index = 2.
2406 max_index
= (((vol
->nr_clusters
+ 7) >> 3) + PAGE_CACHE_SIZE
- 1) >>
2408 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2409 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.",
2410 max_index
, PAGE_CACHE_SIZE
/ 4);
2411 for (index
= 0; index
< max_index
; index
++) {
2414 * Read the page from page cache, getting it from backing store
2415 * if necessary, and increment the use count.
2417 page
= read_cache_page(mapping
, index
, (filler_t
*)readpage
,
2419 /* Ignore pages which errored synchronously. */
2421 ntfs_debug("Sync read_cache_page() error. Skipping "
2422 "page (index 0x%lx).", index
);
2423 nr_free
-= PAGE_CACHE_SIZE
* 8;
2426 wait_on_page_locked(page
);
2427 /* Ignore pages which errored asynchronously. */
2428 if (!PageUptodate(page
)) {
2429 ntfs_debug("Async read_cache_page() error. Skipping "
2430 "page (index 0x%lx).", index
);
2431 page_cache_release(page
);
2432 nr_free
-= PAGE_CACHE_SIZE
* 8;
2435 kaddr
= (u32
*)kmap_atomic(page
, KM_USER0
);
2437 * For each 4 bytes, subtract the number of set bits. If this
2438 * is the last page and it is partial we don't really care as
2439 * it just means we do a little extra work but it won't affect
2440 * the result as all out of range bytes are set to zero by
2443 for (i
= 0; i
< PAGE_CACHE_SIZE
/ 4; i
++)
2444 nr_free
-= (s64
)hweight32(kaddr
[i
]);
2445 kunmap_atomic(kaddr
, KM_USER0
);
2446 page_cache_release(page
);
2448 ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index
- 1);
2450 * Fixup for eventual bits outside logical ntfs volume (see function
2451 * description above).
2453 if (vol
->nr_clusters
& 63)
2454 nr_free
+= 64 - (vol
->nr_clusters
& 63);
2455 up_read(&vol
->lcnbmp_lock
);
2456 /* If errors occured we may well have gone below zero, fix this. */
2459 ntfs_debug("Exiting.");
2464 * __get_nr_free_mft_records - return the number of free inodes on a volume
2465 * @vol: ntfs volume for which to obtain free inode count
2466 * @nr_free: number of mft records in filesystem
2467 * @max_index: maximum number of pages containing set bits
2469 * Calculate the number of free mft records (inodes) on the mounted NTFS
2470 * volume @vol. We actually calculate the number of mft records in use instead
2471 * because this allows us to not care about partial pages as these will be just
2472 * zero filled and hence not be counted as allocated mft record.
2474 * If any pages cannot be read we assume all mft records in the erroring pages
2475 * are in use. This means we return an underestimate on errors which is better
2476 * than an overestimate.
2478 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
2480 static unsigned long __get_nr_free_mft_records(ntfs_volume
*vol
,
2481 s64 nr_free
, const pgoff_t max_index
)
2484 struct address_space
*mapping
= vol
->mftbmp_ino
->i_mapping
;
2485 filler_t
*readpage
= (filler_t
*)mapping
->a_ops
->readpage
;
2489 ntfs_debug("Entering.");
2490 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2491 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
2492 "0x%lx.", max_index
, PAGE_CACHE_SIZE
/ 4);
2493 for (index
= 0; index
< max_index
; index
++) {
2496 * Read the page from page cache, getting it from backing store
2497 * if necessary, and increment the use count.
2499 page
= read_cache_page(mapping
, index
, (filler_t
*)readpage
,
2501 /* Ignore pages which errored synchronously. */
2503 ntfs_debug("Sync read_cache_page() error. Skipping "
2504 "page (index 0x%lx).", index
);
2505 nr_free
-= PAGE_CACHE_SIZE
* 8;
2508 wait_on_page_locked(page
);
2509 /* Ignore pages which errored asynchronously. */
2510 if (!PageUptodate(page
)) {
2511 ntfs_debug("Async read_cache_page() error. Skipping "
2512 "page (index 0x%lx).", index
);
2513 page_cache_release(page
);
2514 nr_free
-= PAGE_CACHE_SIZE
* 8;
2517 kaddr
= (u32
*)kmap_atomic(page
, KM_USER0
);
2519 * For each 4 bytes, subtract the number of set bits. If this
2520 * is the last page and it is partial we don't really care as
2521 * it just means we do a little extra work but it won't affect
2522 * the result as all out of range bytes are set to zero by
2525 for (i
= 0; i
< PAGE_CACHE_SIZE
/ 4; i
++)
2526 nr_free
-= (s64
)hweight32(kaddr
[i
]);
2527 kunmap_atomic(kaddr
, KM_USER0
);
2528 page_cache_release(page
);
2530 ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.",
2532 /* If errors occured we may well have gone below zero, fix this. */
2535 ntfs_debug("Exiting.");
2540 * ntfs_statfs - return information about mounted NTFS volume
2541 * @sb: super block of mounted volume
2542 * @sfs: statfs structure in which to return the information
2544 * Return information about the mounted NTFS volume @sb in the statfs structure
2545 * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is
2546 * called). We interpret the values to be correct of the moment in time at
2547 * which we are called. Most values are variable otherwise and this isn't just
2548 * the free values but the totals as well. For example we can increase the
2549 * total number of file nodes if we run out and we can keep doing this until
2550 * there is no more space on the volume left at all.
2552 * Called from vfs_statfs which is used to handle the statfs, fstatfs, and
2553 * ustat system calls.
2555 * Return 0 on success or -errno on error.
2557 static int ntfs_statfs(struct super_block
*sb
, struct kstatfs
*sfs
)
2560 ntfs_volume
*vol
= NTFS_SB(sb
);
2561 ntfs_inode
*mft_ni
= NTFS_I(vol
->mft_ino
);
2563 unsigned long flags
;
2565 ntfs_debug("Entering.");
2566 /* Type of filesystem. */
2567 sfs
->f_type
= NTFS_SB_MAGIC
;
2568 /* Optimal transfer block size. */
2569 sfs
->f_bsize
= PAGE_CACHE_SIZE
;
2571 * Total data blocks in filesystem in units of f_bsize and since
2572 * inodes are also stored in data blocs ($MFT is a file) this is just
2573 * the total clusters.
2575 sfs
->f_blocks
= vol
->nr_clusters
<< vol
->cluster_size_bits
>>
2577 /* Free data blocks in filesystem in units of f_bsize. */
2578 size
= get_nr_free_clusters(vol
) << vol
->cluster_size_bits
>>
2582 /* Free blocks avail to non-superuser, same as above on NTFS. */
2583 sfs
->f_bavail
= sfs
->f_bfree
= size
;
2584 /* Serialize accesses to the inode bitmap. */
2585 down_read(&vol
->mftbmp_lock
);
2586 read_lock_irqsave(&mft_ni
->size_lock
, flags
);
2587 size
= i_size_read(vol
->mft_ino
) >> vol
->mft_record_size_bits
;
2589 * Convert the maximum number of set bits into bytes rounded up, then
2590 * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
2591 * have one full and one partial page max_index = 2.
2593 max_index
= ((((mft_ni
->initialized_size
>> vol
->mft_record_size_bits
)
2594 + 7) >> 3) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
2595 read_unlock_irqrestore(&mft_ni
->size_lock
, flags
);
2596 /* Number of inodes in filesystem (at this point in time). */
2597 sfs
->f_files
= size
;
2598 /* Free inodes in fs (based on current total count). */
2599 sfs
->f_ffree
= __get_nr_free_mft_records(vol
, size
, max_index
);
2600 up_read(&vol
->mftbmp_lock
);
2602 * File system id. This is extremely *nix flavour dependent and even
2603 * within Linux itself all fs do their own thing. I interpret this to
2604 * mean a unique id associated with the mounted fs and not the id
2605 * associated with the filesystem driver, the latter is already given
2606 * by the filesystem type in sfs->f_type. Thus we use the 64-bit
2607 * volume serial number splitting it into two 32-bit parts. We enter
2608 * the least significant 32-bits in f_fsid[0] and the most significant
2609 * 32-bits in f_fsid[1].
2611 sfs
->f_fsid
.val
[0] = vol
->serial_no
& 0xffffffff;
2612 sfs
->f_fsid
.val
[1] = (vol
->serial_no
>> 32) & 0xffffffff;
2613 /* Maximum length of filenames. */
2614 sfs
->f_namelen
= NTFS_MAX_NAME_LEN
;
2619 * The complete super operations.
2621 static struct super_operations ntfs_sops
= {
2622 .alloc_inode
= ntfs_alloc_big_inode
, /* VFS: Allocate new inode. */
2623 .destroy_inode
= ntfs_destroy_big_inode
, /* VFS: Deallocate inode. */
2624 .put_inode
= ntfs_put_inode
, /* VFS: Called just before
2625 the inode reference count
2628 //.dirty_inode = NULL, /* VFS: Called from
2629 // __mark_inode_dirty(). */
2630 .write_inode
= ntfs_write_inode
, /* VFS: Write dirty inode to
2632 //.drop_inode = NULL, /* VFS: Called just after the
2633 // inode reference count has
2634 // been decreased to zero.
2635 // NOTE: The inode lock is
2636 // held. See fs/inode.c::
2637 // generic_drop_inode(). */
2638 //.delete_inode = NULL, /* VFS: Delete inode from disk.
2639 // Called when i_count becomes
2640 // 0 and i_nlink is also 0. */
2641 //.write_super = NULL, /* Flush dirty super block to
2643 //.sync_fs = NULL, /* ? */
2644 //.write_super_lockfs = NULL, /* ? */
2645 //.unlockfs = NULL, /* ? */
2646 #endif /* NTFS_RW */
2647 .put_super
= ntfs_put_super
, /* Syscall: umount. */
2648 .statfs
= ntfs_statfs
, /* Syscall: statfs */
2649 .remount_fs
= ntfs_remount
, /* Syscall: mount -o remount. */
2650 .clear_inode
= ntfs_clear_big_inode
, /* VFS: Called when an inode is
2651 removed from memory. */
2652 //.umount_begin = NULL, /* Forced umount. */
2653 .show_options
= ntfs_show_options
, /* Show mount options in
2658 * ntfs_fill_super - mount an ntfs filesystem
2659 * @sb: super block of ntfs filesystem to mount
2660 * @opt: string containing the mount options
2661 * @silent: silence error output
2663 * ntfs_fill_super() is called by the VFS to mount the device described by @sb
2664 * with the mount otions in @data with the NTFS filesystem.
2666 * If @silent is true, remain silent even if errors are detected. This is used
2667 * during bootup, when the kernel tries to mount the root filesystem with all
2668 * registered filesystems one after the other until one succeeds. This implies
2669 * that all filesystems except the correct one will quite correctly and
2670 * expectedly return an error, but nobody wants to see error messages when in
2671 * fact this is what is supposed to happen.
2673 * NOTE: @sb->s_flags contains the mount options flags.
2675 static int ntfs_fill_super(struct super_block
*sb
, void *opt
, const int silent
)
2678 struct buffer_head
*bh
;
2679 struct inode
*tmp_ino
;
2682 ntfs_debug("Entering.");
2684 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
2685 #endif /* ! NTFS_RW */
2686 /* Allocate a new ntfs_volume and place it in sb->s_fs_info. */
2687 sb
->s_fs_info
= kmalloc(sizeof(ntfs_volume
), GFP_NOFS
);
2691 ntfs_error(sb
, "Allocation of NTFS volume structure "
2692 "failed. Aborting mount...");
2695 /* Initialize ntfs_volume structure. */
2696 *vol
= (ntfs_volume
) {
2699 * Default is group and other don't have any access to files or
2700 * directories while owner has full access. Further, files by
2701 * default are not executable but directories are of course
2707 init_rwsem(&vol
->mftbmp_lock
);
2708 init_rwsem(&vol
->lcnbmp_lock
);
2712 /* By default, enable sparse support. */
2713 NVolSetSparseEnabled(vol
);
2715 /* Important to get the mount options dealt with now. */
2716 if (!parse_options(vol
, (char*)opt
))
2720 * TODO: Fail safety check. In the future we should really be able to
2721 * cope with this being the case, but for now just bail out.
2723 if (bdev_hardsect_size(sb
->s_bdev
) > NTFS_BLOCK_SIZE
) {
2725 ntfs_error(sb
, "Device has unsupported hardsect_size.");
2729 /* Setup the device access block size to NTFS_BLOCK_SIZE. */
2730 if (sb_set_blocksize(sb
, NTFS_BLOCK_SIZE
) != NTFS_BLOCK_SIZE
) {
2732 ntfs_error(sb
, "Unable to set block size.");
2736 /* Get the size of the device in units of NTFS_BLOCK_SIZE bytes. */
2737 vol
->nr_blocks
= i_size_read(sb
->s_bdev
->bd_inode
) >>
2738 NTFS_BLOCK_SIZE_BITS
;
2740 /* Read the boot sector and return unlocked buffer head to it. */
2741 if (!(bh
= read_ntfs_boot_sector(sb
, silent
))) {
2743 ntfs_error(sb
, "Not an NTFS volume.");
2748 * Extract the data from the boot sector and setup the ntfs super block
2751 result
= parse_ntfs_boot_sector(vol
, (NTFS_BOOT_SECTOR
*)bh
->b_data
);
2753 /* Initialize the cluster and mft allocators. */
2754 ntfs_setup_allocators(vol
);
2760 ntfs_error(sb
, "Unsupported NTFS filesystem.");
2765 * TODO: When we start coping with sector sizes different from
2766 * NTFS_BLOCK_SIZE, we now probably need to set the blocksize of the
2767 * device (probably to NTFS_BLOCK_SIZE).
2770 /* Setup remaining fields in the super block. */
2771 sb
->s_magic
= NTFS_SB_MAGIC
;
2774 * Ntfs allows 63 bits for the file size, i.e. correct would be:
2775 * sb->s_maxbytes = ~0ULL >> 1;
2776 * But the kernel uses a long as the page cache page index which on
2777 * 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel
2778 * defined to the maximum the page cache page index can cope with
2779 * without overflowing the index or to 2^63 - 1, whichever is smaller.
2781 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
2783 sb
->s_time_gran
= 100;
2786 * Now load the metadata required for the page cache and our address
2787 * space operations to function. We do this by setting up a specialised
2788 * read_inode method and then just calling the normal iget() to obtain
2789 * the inode for $MFT which is sufficient to allow our normal inode
2790 * operations and associated address space operations to function.
2792 sb
->s_op
= &ntfs_sops
;
2793 tmp_ino
= new_inode(sb
);
2796 ntfs_error(sb
, "Failed to load essential metadata.");
2799 tmp_ino
->i_ino
= FILE_MFT
;
2800 insert_inode_hash(tmp_ino
);
2801 if (ntfs_read_inode_mount(tmp_ino
) < 0) {
2803 ntfs_error(sb
, "Failed to load essential metadata.");
2804 goto iput_tmp_ino_err_out_now
;
2808 * The current mount is a compression user if the cluster size is
2809 * less than or equal 4kiB.
2811 if (vol
->cluster_size
<= 4096 && !ntfs_nr_compression_users
++) {
2812 result
= allocate_compression_buffers();
2814 ntfs_error(NULL
, "Failed to allocate buffers "
2815 "for compression engine.");
2816 ntfs_nr_compression_users
--;
2818 goto iput_tmp_ino_err_out_now
;
2822 * Generate the global default upcase table if necessary. Also
2823 * temporarily increment the number of upcase users to avoid race
2824 * conditions with concurrent (u)mounts.
2826 if (!default_upcase
)
2827 default_upcase
= generate_default_upcase();
2828 ntfs_nr_upcase_users
++;
2831 * From now on, ignore @silent parameter. If we fail below this line,
2832 * it will be due to a corrupt fs or a system error, so we report it.
2835 * Open the system files with normal access functions and complete
2836 * setting up the ntfs super block.
2838 if (!load_system_files(vol
)) {
2839 ntfs_error(sb
, "Failed to load system files.");
2840 goto unl_upcase_iput_tmp_ino_err_out_now
;
2842 if ((sb
->s_root
= d_alloc_root(vol
->root_ino
))) {
2843 /* We increment i_count simulating an ntfs_iget(). */
2844 atomic_inc(&vol
->root_ino
->i_count
);
2845 ntfs_debug("Exiting, status successful.");
2846 /* Release the default upcase if it has no users. */
2848 if (!--ntfs_nr_upcase_users
&& default_upcase
) {
2849 ntfs_free(default_upcase
);
2850 default_upcase
= NULL
;
2853 sb
->s_export_op
= &ntfs_export_ops
;
2857 ntfs_error(sb
, "Failed to allocate root directory.");
2858 /* Clean up after the successful load_system_files() call from above. */
2859 // TODO: Use ntfs_put_super() instead of repeating all this code...
2860 // FIXME: Should mark the volume clean as the error is most likely
2863 vol
->vol_ino
= NULL
;
2864 /* NTFS 3.0+ specific clean up. */
2865 if (vol
->major_ver
>= 3) {
2867 if (vol
->usnjrnl_j_ino
) {
2868 iput(vol
->usnjrnl_j_ino
);
2869 vol
->usnjrnl_j_ino
= NULL
;
2871 if (vol
->usnjrnl_max_ino
) {
2872 iput(vol
->usnjrnl_max_ino
);
2873 vol
->usnjrnl_max_ino
= NULL
;
2875 if (vol
->usnjrnl_ino
) {
2876 iput(vol
->usnjrnl_ino
);
2877 vol
->usnjrnl_ino
= NULL
;
2879 if (vol
->quota_q_ino
) {
2880 iput(vol
->quota_q_ino
);
2881 vol
->quota_q_ino
= NULL
;
2883 if (vol
->quota_ino
) {
2884 iput(vol
->quota_ino
);
2885 vol
->quota_ino
= NULL
;
2887 #endif /* NTFS_RW */
2888 if (vol
->extend_ino
) {
2889 iput(vol
->extend_ino
);
2890 vol
->extend_ino
= NULL
;
2892 if (vol
->secure_ino
) {
2893 iput(vol
->secure_ino
);
2894 vol
->secure_ino
= NULL
;
2897 iput(vol
->root_ino
);
2898 vol
->root_ino
= NULL
;
2899 iput(vol
->lcnbmp_ino
);
2900 vol
->lcnbmp_ino
= NULL
;
2901 iput(vol
->mftbmp_ino
);
2902 vol
->mftbmp_ino
= NULL
;
2904 if (vol
->logfile_ino
) {
2905 iput(vol
->logfile_ino
);
2906 vol
->logfile_ino
= NULL
;
2908 if (vol
->mftmirr_ino
) {
2909 iput(vol
->mftmirr_ino
);
2910 vol
->mftmirr_ino
= NULL
;
2912 #endif /* NTFS_RW */
2913 /* Throw away the table of attribute definitions. */
2914 vol
->attrdef_size
= 0;
2916 ntfs_free(vol
->attrdef
);
2917 vol
->attrdef
= NULL
;
2919 vol
->upcase_len
= 0;
2921 if (vol
->upcase
== default_upcase
) {
2922 ntfs_nr_upcase_users
--;
2927 ntfs_free(vol
->upcase
);
2931 unload_nls(vol
->nls_map
);
2932 vol
->nls_map
= NULL
;
2934 /* Error exit code path. */
2935 unl_upcase_iput_tmp_ino_err_out_now
:
2937 * Decrease the number of upcase users and destroy the global default
2938 * upcase table if necessary.
2941 if (!--ntfs_nr_upcase_users
&& default_upcase
) {
2942 ntfs_free(default_upcase
);
2943 default_upcase
= NULL
;
2945 if (vol
->cluster_size
<= 4096 && !--ntfs_nr_compression_users
)
2946 free_compression_buffers();
2948 iput_tmp_ino_err_out_now
:
2950 if (vol
->mft_ino
&& vol
->mft_ino
!= tmp_ino
)
2952 vol
->mft_ino
= NULL
;
2954 * This is needed to get ntfs_clear_extent_inode() called for each
2955 * inode we have ever called ntfs_iget()/iput() on, otherwise we A)
2956 * leak resources and B) a subsequent mount fails automatically due to
2957 * ntfs_iget() never calling down into our ntfs_read_locked_inode()
2958 * method again... FIXME: Do we need to do this twice now because of
2959 * attribute inodes? I think not, so leave as is for now... (AIA)
2961 if (invalidate_inodes(sb
)) {
2962 ntfs_error(sb
, "Busy inodes left. This is most likely a NTFS "
2964 /* Copied from fs/super.c. I just love this message. (-; */
2965 printk("NTFS: Busy inodes after umount. Self-destruct in 5 "
2966 "seconds. Have a nice day...\n");
2968 /* Errors at this stage are irrelevant. */
2971 sb
->s_fs_info
= NULL
;
2973 ntfs_debug("Failed, returning -EINVAL.");
2978 * This is a slab cache to optimize allocations and deallocations of Unicode
2979 * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN
2980 * (255) Unicode characters + a terminating NULL Unicode character.
2982 kmem_cache_t
*ntfs_name_cache
;
2984 /* Slab caches for efficient allocation/deallocation of inodes. */
2985 kmem_cache_t
*ntfs_inode_cache
;
2986 kmem_cache_t
*ntfs_big_inode_cache
;
2988 /* Init once constructor for the inode slab cache. */
2989 static void ntfs_big_inode_init_once(void *foo
, kmem_cache_t
*cachep
,
2990 unsigned long flags
)
2992 ntfs_inode
*ni
= (ntfs_inode
*)foo
;
2994 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
2995 SLAB_CTOR_CONSTRUCTOR
)
2996 inode_init_once(VFS_I(ni
));
3000 * Slab caches to optimize allocations and deallocations of attribute search
3001 * contexts and index contexts, respectively.
3003 kmem_cache_t
*ntfs_attr_ctx_cache
;
3004 kmem_cache_t
*ntfs_index_ctx_cache
;
3006 /* Driver wide semaphore. */
3007 DECLARE_MUTEX(ntfs_lock
);
3009 static struct super_block
*ntfs_get_sb(struct file_system_type
*fs_type
,
3010 int flags
, const char *dev_name
, void *data
)
3012 return get_sb_bdev(fs_type
, flags
, dev_name
, data
, ntfs_fill_super
);
3015 static struct file_system_type ntfs_fs_type
= {
3016 .owner
= THIS_MODULE
,
3018 .get_sb
= ntfs_get_sb
,
3019 .kill_sb
= kill_block_super
,
3020 .fs_flags
= FS_REQUIRES_DEV
,
3023 /* Stable names for the slab caches. */
3024 static const char ntfs_index_ctx_cache_name
[] = "ntfs_index_ctx_cache";
3025 static const char ntfs_attr_ctx_cache_name
[] = "ntfs_attr_ctx_cache";
3026 static const char ntfs_name_cache_name
[] = "ntfs_name_cache";
3027 static const char ntfs_inode_cache_name
[] = "ntfs_inode_cache";
3028 static const char ntfs_big_inode_cache_name
[] = "ntfs_big_inode_cache";
3030 static int __init
init_ntfs_fs(void)
3034 /* This may be ugly but it results in pretty output so who cares. (-8 */
3035 printk(KERN_INFO
"NTFS driver " NTFS_VERSION
" [Flags: R/"
3049 ntfs_debug("Debug messages are enabled.");
3051 ntfs_index_ctx_cache
= kmem_cache_create(ntfs_index_ctx_cache_name
,
3052 sizeof(ntfs_index_context
), 0 /* offset */,
3053 SLAB_HWCACHE_ALIGN
, NULL
/* ctor */, NULL
/* dtor */);
3054 if (!ntfs_index_ctx_cache
) {
3055 printk(KERN_CRIT
"NTFS: Failed to create %s!\n",
3056 ntfs_index_ctx_cache_name
);
3059 ntfs_attr_ctx_cache
= kmem_cache_create(ntfs_attr_ctx_cache_name
,
3060 sizeof(ntfs_attr_search_ctx
), 0 /* offset */,
3061 SLAB_HWCACHE_ALIGN
, NULL
/* ctor */, NULL
/* dtor */);
3062 if (!ntfs_attr_ctx_cache
) {
3063 printk(KERN_CRIT
"NTFS: Failed to create %s!\n",
3064 ntfs_attr_ctx_cache_name
);
3068 ntfs_name_cache
= kmem_cache_create(ntfs_name_cache_name
,
3069 (NTFS_MAX_NAME_LEN
+1) * sizeof(ntfschar
), 0,
3070 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
3071 if (!ntfs_name_cache
) {
3072 printk(KERN_CRIT
"NTFS: Failed to create %s!\n",
3073 ntfs_name_cache_name
);
3077 ntfs_inode_cache
= kmem_cache_create(ntfs_inode_cache_name
,
3078 sizeof(ntfs_inode
), 0,
3079 SLAB_RECLAIM_ACCOUNT
, NULL
, NULL
);
3080 if (!ntfs_inode_cache
) {
3081 printk(KERN_CRIT
"NTFS: Failed to create %s!\n",
3082 ntfs_inode_cache_name
);
3086 ntfs_big_inode_cache
= kmem_cache_create(ntfs_big_inode_cache_name
,
3087 sizeof(big_ntfs_inode
), 0,
3088 SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
,
3089 ntfs_big_inode_init_once
, NULL
);
3090 if (!ntfs_big_inode_cache
) {
3091 printk(KERN_CRIT
"NTFS: Failed to create %s!\n",
3092 ntfs_big_inode_cache_name
);
3093 goto big_inode_err_out
;
3096 /* Register the ntfs sysctls. */
3097 err
= ntfs_sysctl(1);
3099 printk(KERN_CRIT
"NTFS: Failed to register NTFS sysctls!\n");
3100 goto sysctl_err_out
;
3103 err
= register_filesystem(&ntfs_fs_type
);
3105 ntfs_debug("NTFS driver registered successfully.");
3106 return 0; /* Success! */
3108 printk(KERN_CRIT
"NTFS: Failed to register NTFS filesystem driver!\n");
3111 kmem_cache_destroy(ntfs_big_inode_cache
);
3113 kmem_cache_destroy(ntfs_inode_cache
);
3115 kmem_cache_destroy(ntfs_name_cache
);
3117 kmem_cache_destroy(ntfs_attr_ctx_cache
);
3119 kmem_cache_destroy(ntfs_index_ctx_cache
);
3122 printk(KERN_CRIT
"NTFS: Aborting NTFS filesystem driver "
3123 "registration...\n");
3129 static void __exit
exit_ntfs_fs(void)
3133 ntfs_debug("Unregistering NTFS driver.");
3135 unregister_filesystem(&ntfs_fs_type
);
3137 if (kmem_cache_destroy(ntfs_big_inode_cache
) && (err
= 1))
3138 printk(KERN_CRIT
"NTFS: Failed to destory %s.\n",
3139 ntfs_big_inode_cache_name
);
3140 if (kmem_cache_destroy(ntfs_inode_cache
) && (err
= 1))
3141 printk(KERN_CRIT
"NTFS: Failed to destory %s.\n",
3142 ntfs_inode_cache_name
);
3143 if (kmem_cache_destroy(ntfs_name_cache
) && (err
= 1))
3144 printk(KERN_CRIT
"NTFS: Failed to destory %s.\n",
3145 ntfs_name_cache_name
);
3146 if (kmem_cache_destroy(ntfs_attr_ctx_cache
) && (err
= 1))
3147 printk(KERN_CRIT
"NTFS: Failed to destory %s.\n",
3148 ntfs_attr_ctx_cache_name
);
3149 if (kmem_cache_destroy(ntfs_index_ctx_cache
) && (err
= 1))
3150 printk(KERN_CRIT
"NTFS: Failed to destory %s.\n",
3151 ntfs_index_ctx_cache_name
);
3153 printk(KERN_CRIT
"NTFS: This causes memory to leak! There is "
3154 "probably a BUG in the driver! Please report "
3155 "you saw this message to "
3156 "linux-ntfs-dev@lists.sourceforge.net\n");
3157 /* Unregister the ntfs sysctls. */
3161 MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>");
3162 MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2005 Anton Altaparmakov");
3163 MODULE_VERSION(NTFS_VERSION
);
3164 MODULE_LICENSE("GPL");
3166 module_param(debug_msgs
, bool, 0);
3167 MODULE_PARM_DESC(debug_msgs
, "Enable debug messages.");
3170 module_init(init_ntfs_fs
)
3171 module_exit(exit_ntfs_fs
)