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_OCTAL(option, variable) \
130 if (!strcmp(p, option)) { \
133 variable = simple_strtoul(ov = v, &v, 8); \
137 #define NTFS_GETOPT_BOOL(option, variable) \
138 if (!strcmp(p, option)) { \
140 if (!simple_getbool(v, &val)) \
144 #define NTFS_GETOPT_OPTIONS_ARRAY(option, variable, opt_array) \
145 if (!strcmp(p, option)) { \
150 if (variable == -1) \
152 for (_i = 0; opt_array[_i].str && *opt_array[_i].str; _i++) \
153 if (!strcmp(opt_array[_i].str, v)) { \
154 variable |= opt_array[_i].val; \
157 if (!opt_array[_i].str || !*opt_array[_i].str) \
161 goto no_mount_options
;
162 ntfs_debug("Entering with mount options string: %s", opt
);
163 while ((p
= strsep(&opt
, ","))) {
164 if ((v
= strchr(p
, '=')))
166 NTFS_GETOPT("uid", uid
)
167 else NTFS_GETOPT("gid", gid
)
168 else NTFS_GETOPT_OCTAL("umask", fmask
= dmask
)
169 else NTFS_GETOPT_OCTAL("fmask", fmask
)
170 else NTFS_GETOPT_OCTAL("dmask", dmask
)
171 else NTFS_GETOPT("mft_zone_multiplier", mft_zone_multiplier
)
172 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy
, TRUE
)
173 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files
)
174 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive
)
175 else NTFS_GETOPT_BOOL("disable_sparse", disable_sparse
)
176 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors
,
178 else if (!strcmp(p
, "posix") || !strcmp(p
, "show_inodes"))
179 ntfs_warning(vol
->sb
, "Ignoring obsolete option %s.",
181 else if (!strcmp(p
, "nls") || !strcmp(p
, "iocharset")) {
182 if (!strcmp(p
, "iocharset"))
183 ntfs_warning(vol
->sb
, "Option iocharset is "
184 "deprecated. Please use "
185 "option nls=<charsetname> in "
191 nls_map
= load_nls(v
);
194 ntfs_error(vol
->sb
, "NLS character set "
198 ntfs_error(vol
->sb
, "NLS character set %s not "
199 "found. Using previous one %s.",
200 v
, old_nls
->charset
);
202 } else /* nls_map */ {
206 } else if (!strcmp(p
, "utf8")) {
208 ntfs_warning(vol
->sb
, "Option utf8 is no longer "
209 "supported, using option nls=utf8. Please "
210 "use option nls=utf8 in the future and "
211 "make sure utf8 is compiled either as a "
212 "module or into the kernel.");
215 else if (!simple_getbool(v
, &val
))
222 ntfs_error(vol
->sb
, "Unrecognized mount option %s.", p
);
223 if (errors
< INT_MAX
)
226 #undef NTFS_GETOPT_OPTIONS_ARRAY
227 #undef NTFS_GETOPT_BOOL
229 #undef NTFS_GETOPT_WITH_DEFAULT
232 if (errors
&& !sloppy
)
235 ntfs_warning(vol
->sb
, "Sloppy option given. Ignoring "
236 "unrecognized mount option(s) and continuing.");
237 /* Keep this first! */
238 if (on_errors
!= -1) {
240 ntfs_error(vol
->sb
, "Invalid errors option argument "
241 "or bug in options parser.");
246 if (vol
->nls_map
&& vol
->nls_map
!= nls_map
) {
247 ntfs_error(vol
->sb
, "Cannot change NLS character set "
250 } /* else (!vol->nls_map) */
251 ntfs_debug("Using NLS character set %s.", nls_map
->charset
);
252 vol
->nls_map
= nls_map
;
253 } else /* (!nls_map) */ {
255 vol
->nls_map
= load_nls_default();
257 ntfs_error(vol
->sb
, "Failed to load default "
258 "NLS character set.");
261 ntfs_debug("Using default NLS character set (%s).",
262 vol
->nls_map
->charset
);
265 if (mft_zone_multiplier
!= -1) {
266 if (vol
->mft_zone_multiplier
&& vol
->mft_zone_multiplier
!=
267 mft_zone_multiplier
) {
268 ntfs_error(vol
->sb
, "Cannot change mft_zone_multiplier "
272 if (mft_zone_multiplier
< 1 || mft_zone_multiplier
> 4) {
273 ntfs_error(vol
->sb
, "Invalid mft_zone_multiplier. "
274 "Using default value, i.e. 1.");
275 mft_zone_multiplier
= 1;
277 vol
->mft_zone_multiplier
= mft_zone_multiplier
;
279 if (!vol
->mft_zone_multiplier
)
280 vol
->mft_zone_multiplier
= 1;
282 vol
->on_errors
= on_errors
;
283 if (!vol
->on_errors
|| vol
->on_errors
== ON_ERRORS_RECOVER
)
284 vol
->on_errors
|= ON_ERRORS_CONTINUE
;
285 if (uid
!= (uid_t
)-1)
287 if (gid
!= (gid_t
)-1)
289 if (fmask
!= (mode_t
)-1)
291 if (dmask
!= (mode_t
)-1)
293 if (show_sys_files
!= -1) {
295 NVolSetShowSystemFiles(vol
);
297 NVolClearShowSystemFiles(vol
);
299 if (case_sensitive
!= -1) {
301 NVolSetCaseSensitive(vol
);
303 NVolClearCaseSensitive(vol
);
305 if (disable_sparse
!= -1) {
307 NVolClearSparseEnabled(vol
);
309 if (!NVolSparseEnabled(vol
) &&
310 vol
->major_ver
&& vol
->major_ver
< 3)
311 ntfs_warning(vol
->sb
, "Not enabling sparse "
312 "support due to NTFS volume "
313 "version %i.%i (need at least "
314 "version 3.0).", vol
->major_ver
,
317 NVolSetSparseEnabled(vol
);
322 ntfs_error(vol
->sb
, "The %s option requires an argument.", p
);
325 ntfs_error(vol
->sb
, "The %s option requires a boolean argument.", p
);
328 ntfs_error(vol
->sb
, "Invalid %s option argument: %s", p
, ov
);
335 * ntfs_write_volume_flags - write new flags to the volume information flags
336 * @vol: ntfs volume on which to modify the flags
337 * @flags: new flags value for the volume information flags
339 * Internal function. You probably want to use ntfs_{set,clear}_volume_flags()
340 * instead (see below).
342 * Replace the volume information flags on the volume @vol with the value
343 * supplied in @flags. Note, this overwrites the volume information flags, so
344 * make sure to combine the flags you want to modify with the old flags and use
345 * the result when calling ntfs_write_volume_flags().
347 * Return 0 on success and -errno on error.
349 static int ntfs_write_volume_flags(ntfs_volume
*vol
, const VOLUME_FLAGS flags
)
351 ntfs_inode
*ni
= NTFS_I(vol
->vol_ino
);
353 VOLUME_INFORMATION
*vi
;
354 ntfs_attr_search_ctx
*ctx
;
357 ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
358 le16_to_cpu(vol
->vol_flags
), le16_to_cpu(flags
));
359 if (vol
->vol_flags
== flags
)
362 m
= map_mft_record(ni
);
367 ctx
= ntfs_attr_get_search_ctx(ni
, m
);
370 goto put_unm_err_out
;
372 err
= ntfs_attr_lookup(AT_VOLUME_INFORMATION
, NULL
, 0, 0, 0, NULL
, 0,
375 goto put_unm_err_out
;
376 vi
= (VOLUME_INFORMATION
*)((u8
*)ctx
->attr
+
377 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
));
378 vol
->vol_flags
= vi
->flags
= flags
;
379 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
380 mark_mft_record_dirty(ctx
->ntfs_ino
);
381 ntfs_attr_put_search_ctx(ctx
);
382 unmap_mft_record(ni
);
388 ntfs_attr_put_search_ctx(ctx
);
389 unmap_mft_record(ni
);
391 ntfs_error(vol
->sb
, "Failed with error code %i.", -err
);
396 * ntfs_set_volume_flags - set bits in the volume information flags
397 * @vol: ntfs volume on which to modify the flags
398 * @flags: flags to set on the volume
400 * Set the bits in @flags in the volume information flags on the volume @vol.
402 * Return 0 on success and -errno on error.
404 static inline int ntfs_set_volume_flags(ntfs_volume
*vol
, VOLUME_FLAGS flags
)
406 flags
&= VOLUME_FLAGS_MASK
;
407 return ntfs_write_volume_flags(vol
, vol
->vol_flags
| flags
);
411 * ntfs_clear_volume_flags - clear bits in the volume information flags
412 * @vol: ntfs volume on which to modify the flags
413 * @flags: flags to clear on the volume
415 * Clear the bits in @flags in the volume information flags on the volume @vol.
417 * Return 0 on success and -errno on error.
419 static inline int ntfs_clear_volume_flags(ntfs_volume
*vol
, VOLUME_FLAGS flags
)
421 flags
&= VOLUME_FLAGS_MASK
;
422 flags
= vol
->vol_flags
& cpu_to_le16(~le16_to_cpu(flags
));
423 return ntfs_write_volume_flags(vol
, flags
);
429 * ntfs_remount - change the mount options of a mounted ntfs filesystem
430 * @sb: superblock of mounted ntfs filesystem
431 * @flags: remount flags
432 * @opt: remount options string
434 * Change the mount options of an already mounted ntfs filesystem.
436 * NOTE: The VFS sets the @sb->s_flags remount flags to @flags after
437 * ntfs_remount() returns successfully (i.e. returns 0). Otherwise,
438 * @sb->s_flags are not changed.
440 static int ntfs_remount(struct super_block
*sb
, int *flags
, char *opt
)
442 ntfs_volume
*vol
= NTFS_SB(sb
);
444 ntfs_debug("Entering with remount options string: %s", opt
);
446 /* For read-only compiled driver, enforce all read-only flags. */
447 *flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
450 * For the read-write compiled driver, if we are remounting read-write,
451 * make sure there are no volume errors and that no unsupported volume
452 * flags are set. Also, empty the logfile journal as it would become
453 * stale as soon as something is written to the volume and mark the
454 * volume dirty so that chkdsk is run if the volume is not umounted
455 * cleanly. Finally, mark the quotas out of date so Windows rescans
456 * the volume on boot and updates them.
458 * When remounting read-only, mark the volume clean if no volume errors
461 if ((sb
->s_flags
& MS_RDONLY
) && !(*flags
& MS_RDONLY
)) {
462 static const char *es
= ". Cannot remount read-write.";
464 /* Remounting read-write. */
465 if (NVolErrors(vol
)) {
466 ntfs_error(sb
, "Volume has errors and is read-only%s",
470 if (vol
->vol_flags
& VOLUME_IS_DIRTY
) {
471 ntfs_error(sb
, "Volume is dirty and read-only%s", es
);
474 if (vol
->vol_flags
& VOLUME_MUST_MOUNT_RO_MASK
) {
475 ntfs_error(sb
, "Volume has unsupported flags set and "
476 "is read-only%s", es
);
479 if (ntfs_set_volume_flags(vol
, VOLUME_IS_DIRTY
)) {
480 ntfs_error(sb
, "Failed to set dirty bit in volume "
481 "information flags%s", es
);
485 // TODO: Enable this code once we start modifying anything that
486 // is different between NTFS 1.2 and 3.x...
487 /* Set NT4 compatibility flag on newer NTFS version volumes. */
488 if ((vol
->major_ver
> 1)) {
489 if (ntfs_set_volume_flags(vol
, VOLUME_MOUNTED_ON_NT4
)) {
490 ntfs_error(sb
, "Failed to set NT4 "
491 "compatibility flag%s", es
);
497 if (!ntfs_empty_logfile(vol
->logfile_ino
)) {
498 ntfs_error(sb
, "Failed to empty journal $LogFile%s",
503 if (!ntfs_mark_quotas_out_of_date(vol
)) {
504 ntfs_error(sb
, "Failed to mark quotas out of date%s",
509 if (!ntfs_stamp_usnjrnl(vol
)) {
510 ntfs_error(sb
, "Failed to stamp transation log "
515 } else if (!(sb
->s_flags
& MS_RDONLY
) && (*flags
& MS_RDONLY
)) {
516 /* Remounting read-only. */
517 if (!NVolErrors(vol
)) {
518 if (ntfs_clear_volume_flags(vol
, VOLUME_IS_DIRTY
))
519 ntfs_warning(sb
, "Failed to clear dirty bit "
520 "in volume information "
521 "flags. Run chkdsk.");
526 // TODO: Deal with *flags.
528 if (!parse_options(vol
, opt
))
535 * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector
536 * @sb: Super block of the device to which @b belongs.
537 * @b: Boot sector of device @sb to check.
538 * @silent: If TRUE, all output will be silenced.
540 * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot
541 * sector. Returns TRUE if it is valid and FALSE if not.
543 * @sb is only needed for warning/error output, i.e. it can be NULL when silent
546 static BOOL
is_boot_sector_ntfs(const struct super_block
*sb
,
547 const NTFS_BOOT_SECTOR
*b
, const BOOL silent
)
550 * Check that checksum == sum of u32 values from b to the checksum
551 * field. If checksum is zero, no checking is done. We will work when
552 * the checksum test fails, since some utilities update the boot sector
553 * ignoring the checksum which leaves the checksum out-of-date. We
554 * report a warning if this is the case.
556 if ((void*)b
< (void*)&b
->checksum
&& b
->checksum
&& !silent
) {
560 for (i
= 0, u
= (le32
*)b
; u
< (le32
*)(&b
->checksum
); ++u
)
561 i
+= le32_to_cpup(u
);
562 if (le32_to_cpu(b
->checksum
) != i
)
563 ntfs_warning(sb
, "Invalid boot sector checksum.");
565 /* Check OEMidentifier is "NTFS " */
566 if (b
->oem_id
!= magicNTFS
)
568 /* Check bytes per sector value is between 256 and 4096. */
569 if (le16_to_cpu(b
->bpb
.bytes_per_sector
) < 0x100 ||
570 le16_to_cpu(b
->bpb
.bytes_per_sector
) > 0x1000)
572 /* Check sectors per cluster value is valid. */
573 switch (b
->bpb
.sectors_per_cluster
) {
574 case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128:
579 /* Check the cluster size is not above the maximum (64kiB). */
580 if ((u32
)le16_to_cpu(b
->bpb
.bytes_per_sector
) *
581 b
->bpb
.sectors_per_cluster
> NTFS_MAX_CLUSTER_SIZE
)
583 /* Check reserved/unused fields are really zero. */
584 if (le16_to_cpu(b
->bpb
.reserved_sectors
) ||
585 le16_to_cpu(b
->bpb
.root_entries
) ||
586 le16_to_cpu(b
->bpb
.sectors
) ||
587 le16_to_cpu(b
->bpb
.sectors_per_fat
) ||
588 le32_to_cpu(b
->bpb
.large_sectors
) || b
->bpb
.fats
)
590 /* Check clusters per file mft record value is valid. */
591 if ((u8
)b
->clusters_per_mft_record
< 0xe1 ||
592 (u8
)b
->clusters_per_mft_record
> 0xf7)
593 switch (b
->clusters_per_mft_record
) {
594 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
599 /* Check clusters per index block value is valid. */
600 if ((u8
)b
->clusters_per_index_record
< 0xe1 ||
601 (u8
)b
->clusters_per_index_record
> 0xf7)
602 switch (b
->clusters_per_index_record
) {
603 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
609 * Check for valid end of sector marker. We will work without it, but
610 * many BIOSes will refuse to boot from a bootsector if the magic is
611 * incorrect, so we emit a warning.
613 if (!silent
&& b
->end_of_sector_marker
!= const_cpu_to_le16(0xaa55))
614 ntfs_warning(sb
, "Invalid end of sector marker.");
621 * read_ntfs_boot_sector - read the NTFS boot sector of a device
622 * @sb: super block of device to read the boot sector from
623 * @silent: if true, suppress all output
625 * Reads the boot sector from the device and validates it. If that fails, tries
626 * to read the backup boot sector, first from the end of the device a-la NT4 and
627 * later and then from the middle of the device a-la NT3.51 and before.
629 * If a valid boot sector is found but it is not the primary boot sector, we
630 * repair the primary boot sector silently (unless the device is read-only or
631 * the primary boot sector is not accessible).
633 * NOTE: To call this function, @sb must have the fields s_dev, the ntfs super
634 * block (u.ntfs_sb), nr_blocks and the device flags (s_flags) initialized
635 * to their respective values.
637 * Return the unlocked buffer head containing the boot sector or NULL on error.
639 static struct buffer_head
*read_ntfs_boot_sector(struct super_block
*sb
,
642 const char *read_err_str
= "Unable to read %s boot sector.";
643 struct buffer_head
*bh_primary
, *bh_backup
;
644 long nr_blocks
= NTFS_SB(sb
)->nr_blocks
;
646 /* Try to read primary boot sector. */
647 if ((bh_primary
= sb_bread(sb
, 0))) {
648 if (is_boot_sector_ntfs(sb
, (NTFS_BOOT_SECTOR
*)
649 bh_primary
->b_data
, silent
))
652 ntfs_error(sb
, "Primary boot sector is invalid.");
654 ntfs_error(sb
, read_err_str
, "primary");
655 if (!(NTFS_SB(sb
)->on_errors
& ON_ERRORS_RECOVER
)) {
659 ntfs_error(sb
, "Mount option errors=recover not used. "
660 "Aborting without trying to recover.");
663 /* Try to read NT4+ 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
;
670 ntfs_error(sb
, read_err_str
, "backup");
671 /* Try to read NT3.51- backup boot sector. */
672 if ((bh_backup
= sb_bread(sb
, nr_blocks
>> 1))) {
673 if (is_boot_sector_ntfs(sb
, (NTFS_BOOT_SECTOR
*)
674 bh_backup
->b_data
, silent
))
675 goto hotfix_primary_boot_sector
;
677 ntfs_error(sb
, "Could not find a valid backup boot "
681 ntfs_error(sb
, read_err_str
, "backup");
682 /* We failed. Cleanup and return. */
686 hotfix_primary_boot_sector
:
689 * If we managed to read sector zero and the volume is not
690 * read-only, copy the found, valid backup boot sector to the
691 * primary boot sector.
693 if (!(sb
->s_flags
& MS_RDONLY
)) {
694 ntfs_warning(sb
, "Hot-fix: Recovering invalid primary "
695 "boot sector from backup copy.");
696 memcpy(bh_primary
->b_data
, bh_backup
->b_data
,
698 mark_buffer_dirty(bh_primary
);
699 sync_dirty_buffer(bh_primary
);
700 if (buffer_uptodate(bh_primary
)) {
704 ntfs_error(sb
, "Hot-fix: Device write error while "
705 "recovering primary boot sector.");
707 ntfs_warning(sb
, "Hot-fix: Recovery of primary boot "
708 "sector failed: Read-only mount.");
712 ntfs_warning(sb
, "Using backup boot sector.");
717 * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol
718 * @vol: volume structure to initialise with data from boot sector
719 * @b: boot sector to parse
721 * Parse the ntfs boot sector @b and store all imporant information therein in
722 * the ntfs super block @vol. Return TRUE on success and FALSE on error.
724 static BOOL
parse_ntfs_boot_sector(ntfs_volume
*vol
, const NTFS_BOOT_SECTOR
*b
)
726 unsigned int sectors_per_cluster_bits
, nr_hidden_sects
;
727 int clusters_per_mft_record
, clusters_per_index_record
;
730 vol
->sector_size
= le16_to_cpu(b
->bpb
.bytes_per_sector
);
731 vol
->sector_size_bits
= ffs(vol
->sector_size
) - 1;
732 ntfs_debug("vol->sector_size = %i (0x%x)", vol
->sector_size
,
734 ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol
->sector_size_bits
,
735 vol
->sector_size_bits
);
736 if (vol
->sector_size
!= vol
->sb
->s_blocksize
)
737 ntfs_warning(vol
->sb
, "The boot sector indicates a sector size "
738 "different from the device sector size.");
739 ntfs_debug("sectors_per_cluster = 0x%x", b
->bpb
.sectors_per_cluster
);
740 sectors_per_cluster_bits
= ffs(b
->bpb
.sectors_per_cluster
) - 1;
741 ntfs_debug("sectors_per_cluster_bits = 0x%x",
742 sectors_per_cluster_bits
);
743 nr_hidden_sects
= le32_to_cpu(b
->bpb
.hidden_sectors
);
744 ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects
);
745 vol
->cluster_size
= vol
->sector_size
<< sectors_per_cluster_bits
;
746 vol
->cluster_size_mask
= vol
->cluster_size
- 1;
747 vol
->cluster_size_bits
= ffs(vol
->cluster_size
) - 1;
748 ntfs_debug("vol->cluster_size = %i (0x%x)", vol
->cluster_size
,
750 ntfs_debug("vol->cluster_size_mask = 0x%x", vol
->cluster_size_mask
);
751 ntfs_debug("vol->cluster_size_bits = %i (0x%x)",
752 vol
->cluster_size_bits
, vol
->cluster_size_bits
);
753 if (vol
->sector_size
> vol
->cluster_size
) {
754 ntfs_error(vol
->sb
, "Sector sizes above the cluster size are "
755 "not supported. Sorry.");
758 if (vol
->sb
->s_blocksize
> vol
->cluster_size
) {
759 ntfs_error(vol
->sb
, "Cluster sizes smaller than the device "
760 "sector size are not supported. Sorry.");
763 clusters_per_mft_record
= b
->clusters_per_mft_record
;
764 ntfs_debug("clusters_per_mft_record = %i (0x%x)",
765 clusters_per_mft_record
, clusters_per_mft_record
);
766 if (clusters_per_mft_record
> 0)
767 vol
->mft_record_size
= vol
->cluster_size
<<
768 (ffs(clusters_per_mft_record
) - 1);
771 * When mft_record_size < cluster_size, clusters_per_mft_record
772 * = -log2(mft_record_size) bytes. mft_record_size normaly is
773 * 1024 bytes, which is encoded as 0xF6 (-10 in decimal).
775 vol
->mft_record_size
= 1 << -clusters_per_mft_record
;
776 vol
->mft_record_size_mask
= vol
->mft_record_size
- 1;
777 vol
->mft_record_size_bits
= ffs(vol
->mft_record_size
) - 1;
778 ntfs_debug("vol->mft_record_size = %i (0x%x)", vol
->mft_record_size
,
779 vol
->mft_record_size
);
780 ntfs_debug("vol->mft_record_size_mask = 0x%x",
781 vol
->mft_record_size_mask
);
782 ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
783 vol
->mft_record_size_bits
, vol
->mft_record_size_bits
);
785 * We cannot support mft record sizes above the PAGE_CACHE_SIZE since
786 * we store $MFT/$DATA, the table of mft records in the page cache.
788 if (vol
->mft_record_size
> PAGE_CACHE_SIZE
) {
789 ntfs_error(vol
->sb
, "Mft record size %i (0x%x) exceeds the "
790 "page cache size on your system %lu (0x%lx). "
791 "This is not supported. Sorry.",
792 vol
->mft_record_size
, vol
->mft_record_size
,
793 PAGE_CACHE_SIZE
, PAGE_CACHE_SIZE
);
796 clusters_per_index_record
= b
->clusters_per_index_record
;
797 ntfs_debug("clusters_per_index_record = %i (0x%x)",
798 clusters_per_index_record
, clusters_per_index_record
);
799 if (clusters_per_index_record
> 0)
800 vol
->index_record_size
= vol
->cluster_size
<<
801 (ffs(clusters_per_index_record
) - 1);
804 * When index_record_size < cluster_size,
805 * clusters_per_index_record = -log2(index_record_size) bytes.
806 * index_record_size normaly equals 4096 bytes, which is
807 * encoded as 0xF4 (-12 in decimal).
809 vol
->index_record_size
= 1 << -clusters_per_index_record
;
810 vol
->index_record_size_mask
= vol
->index_record_size
- 1;
811 vol
->index_record_size_bits
= ffs(vol
->index_record_size
) - 1;
812 ntfs_debug("vol->index_record_size = %i (0x%x)",
813 vol
->index_record_size
, vol
->index_record_size
);
814 ntfs_debug("vol->index_record_size_mask = 0x%x",
815 vol
->index_record_size_mask
);
816 ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
817 vol
->index_record_size_bits
,
818 vol
->index_record_size_bits
);
820 * Get the size of the volume in clusters and check for 64-bit-ness.
821 * Windows currently only uses 32 bits to save the clusters so we do
822 * the same as it is much faster on 32-bit CPUs.
824 ll
= sle64_to_cpu(b
->number_of_sectors
) >> sectors_per_cluster_bits
;
825 if ((u64
)ll
>= 1ULL << 32) {
826 ntfs_error(vol
->sb
, "Cannot handle 64-bit clusters. Sorry.");
829 vol
->nr_clusters
= ll
;
830 ntfs_debug("vol->nr_clusters = 0x%llx", (long long)vol
->nr_clusters
);
832 * On an architecture where unsigned long is 32-bits, we restrict the
833 * volume size to 2TiB (2^41). On a 64-bit architecture, the compiler
834 * will hopefully optimize the whole check away.
836 if (sizeof(unsigned long) < 8) {
837 if ((ll
<< vol
->cluster_size_bits
) >= (1ULL << 41)) {
838 ntfs_error(vol
->sb
, "Volume size (%lluTiB) is too "
839 "large for this architecture. "
840 "Maximum supported is 2TiB. Sorry.",
841 (unsigned long long)ll
>> (40 -
842 vol
->cluster_size_bits
));
846 ll
= sle64_to_cpu(b
->mft_lcn
);
847 if (ll
>= vol
->nr_clusters
) {
848 ntfs_error(vol
->sb
, "MFT LCN is beyond end of volume. Weird.");
852 ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol
->mft_lcn
);
853 ll
= sle64_to_cpu(b
->mftmirr_lcn
);
854 if (ll
>= vol
->nr_clusters
) {
855 ntfs_error(vol
->sb
, "MFTMirr LCN is beyond end of volume. "
859 vol
->mftmirr_lcn
= ll
;
860 ntfs_debug("vol->mftmirr_lcn = 0x%llx", (long long)vol
->mftmirr_lcn
);
863 * Work out the size of the mft mirror in number of mft records. If the
864 * cluster size is less than or equal to the size taken by four mft
865 * records, the mft mirror stores the first four mft records. If the
866 * cluster size is bigger than the size taken by four mft records, the
867 * mft mirror contains as many mft records as will fit into one
870 if (vol
->cluster_size
<= (4 << vol
->mft_record_size_bits
))
871 vol
->mftmirr_size
= 4;
873 vol
->mftmirr_size
= vol
->cluster_size
>>
874 vol
->mft_record_size_bits
;
875 ntfs_debug("vol->mftmirr_size = %i", vol
->mftmirr_size
);
877 vol
->serial_no
= le64_to_cpu(b
->volume_serial_number
);
878 ntfs_debug("vol->serial_no = 0x%llx",
879 (unsigned long long)vol
->serial_no
);
884 * ntfs_setup_allocators - initialize the cluster and mft allocators
885 * @vol: volume structure for which to setup the allocators
887 * Setup the cluster (lcn) and mft allocators to the starting values.
889 static void ntfs_setup_allocators(ntfs_volume
*vol
)
892 LCN mft_zone_size
, mft_lcn
;
895 ntfs_debug("vol->mft_zone_multiplier = 0x%x",
896 vol
->mft_zone_multiplier
);
898 /* Determine the size of the MFT zone. */
899 mft_zone_size
= vol
->nr_clusters
;
900 switch (vol
->mft_zone_multiplier
) { /* % of volume size in clusters */
902 mft_zone_size
>>= 1; /* 50% */
905 mft_zone_size
= (mft_zone_size
+
906 (mft_zone_size
>> 1)) >> 2; /* 37.5% */
909 mft_zone_size
>>= 2; /* 25% */
913 mft_zone_size
>>= 3; /* 12.5% */
916 /* Setup the mft zone. */
917 vol
->mft_zone_start
= vol
->mft_zone_pos
= vol
->mft_lcn
;
918 ntfs_debug("vol->mft_zone_pos = 0x%llx",
919 (unsigned long long)vol
->mft_zone_pos
);
921 * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
922 * source) and if the actual mft_lcn is in the expected place or even
923 * further to the front of the volume, extend the mft_zone to cover the
924 * beginning of the volume as well. This is in order to protect the
925 * area reserved for the mft bitmap as well within the mft_zone itself.
926 * On non-standard volumes we do not protect it as the overhead would
927 * be higher than the speed increase we would get by doing it.
929 mft_lcn
= (8192 + 2 * vol
->cluster_size
- 1) / vol
->cluster_size
;
930 if (mft_lcn
* vol
->cluster_size
< 16 * 1024)
931 mft_lcn
= (16 * 1024 + vol
->cluster_size
- 1) /
933 if (vol
->mft_zone_start
<= mft_lcn
)
934 vol
->mft_zone_start
= 0;
935 ntfs_debug("vol->mft_zone_start = 0x%llx",
936 (unsigned long long)vol
->mft_zone_start
);
938 * Need to cap the mft zone on non-standard volumes so that it does
939 * not point outside the boundaries of the volume. We do this by
940 * halving the zone size until we are inside the volume.
942 vol
->mft_zone_end
= vol
->mft_lcn
+ mft_zone_size
;
943 while (vol
->mft_zone_end
>= vol
->nr_clusters
) {
945 vol
->mft_zone_end
= vol
->mft_lcn
+ mft_zone_size
;
947 ntfs_debug("vol->mft_zone_end = 0x%llx",
948 (unsigned long long)vol
->mft_zone_end
);
950 * Set the current position within each data zone to the start of the
953 vol
->data1_zone_pos
= vol
->mft_zone_end
;
954 ntfs_debug("vol->data1_zone_pos = 0x%llx",
955 (unsigned long long)vol
->data1_zone_pos
);
956 vol
->data2_zone_pos
= 0;
957 ntfs_debug("vol->data2_zone_pos = 0x%llx",
958 (unsigned long long)vol
->data2_zone_pos
);
960 /* Set the mft data allocation position to mft record 24. */
961 vol
->mft_data_pos
= 24;
962 ntfs_debug("vol->mft_data_pos = 0x%llx",
963 (unsigned long long)vol
->mft_data_pos
);
970 * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume
971 * @vol: ntfs super block describing device whose mft mirror to load
973 * Return TRUE on success or FALSE on error.
975 static BOOL
load_and_init_mft_mirror(ntfs_volume
*vol
)
977 struct inode
*tmp_ino
;
980 ntfs_debug("Entering.");
981 /* Get mft mirror inode. */
982 tmp_ino
= ntfs_iget(vol
->sb
, FILE_MFTMirr
);
983 if (IS_ERR(tmp_ino
) || is_bad_inode(tmp_ino
)) {
984 if (!IS_ERR(tmp_ino
))
986 /* Caller will display error message. */
990 * Re-initialize some specifics about $MFTMirr's inode as
991 * ntfs_read_inode() will have set up the default ones.
993 /* Set uid and gid to root. */
994 tmp_ino
->i_uid
= tmp_ino
->i_gid
= 0;
995 /* Regular file. No access for anyone. */
996 tmp_ino
->i_mode
= S_IFREG
;
997 /* No VFS initiated operations allowed for $MFTMirr. */
998 tmp_ino
->i_op
= &ntfs_empty_inode_ops
;
999 tmp_ino
->i_fop
= &ntfs_empty_file_ops
;
1000 /* Put in our special address space operations. */
1001 tmp_ino
->i_mapping
->a_ops
= &ntfs_mst_aops
;
1002 tmp_ni
= NTFS_I(tmp_ino
);
1003 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
1004 NInoSetMstProtected(tmp_ni
);
1005 NInoSetSparseDisabled(tmp_ni
);
1007 * Set up our little cheat allowing us to reuse the async read io
1008 * completion handler for directories.
1010 tmp_ni
->itype
.index
.block_size
= vol
->mft_record_size
;
1011 tmp_ni
->itype
.index
.block_size_bits
= vol
->mft_record_size_bits
;
1012 vol
->mftmirr_ino
= tmp_ino
;
1013 ntfs_debug("Done.");
1018 * check_mft_mirror - compare contents of the mft mirror with the mft
1019 * @vol: ntfs super block describing device whose mft mirror to check
1021 * Return TRUE on success or FALSE on error.
1023 * Note, this function also results in the mft mirror runlist being completely
1024 * mapped into memory. The mft mirror write code requires this and will BUG()
1025 * should it find an unmapped runlist element.
1027 static BOOL
check_mft_mirror(ntfs_volume
*vol
)
1029 struct super_block
*sb
= vol
->sb
;
1030 ntfs_inode
*mirr_ni
;
1031 struct page
*mft_page
, *mirr_page
;
1033 runlist_element
*rl
, rl2
[2];
1035 int mrecs_per_page
, i
;
1037 ntfs_debug("Entering.");
1038 /* Compare contents of $MFT and $MFTMirr. */
1039 mrecs_per_page
= PAGE_CACHE_SIZE
/ vol
->mft_record_size
;
1040 BUG_ON(!mrecs_per_page
);
1041 BUG_ON(!vol
->mftmirr_size
);
1042 mft_page
= mirr_page
= NULL
;
1043 kmft
= kmirr
= NULL
;
1048 /* Switch pages if necessary. */
1049 if (!(i
% mrecs_per_page
)) {
1051 ntfs_unmap_page(mft_page
);
1052 ntfs_unmap_page(mirr_page
);
1054 /* Get the $MFT page. */
1055 mft_page
= ntfs_map_page(vol
->mft_ino
->i_mapping
,
1057 if (IS_ERR(mft_page
)) {
1058 ntfs_error(sb
, "Failed to read $MFT.");
1061 kmft
= page_address(mft_page
);
1062 /* Get the $MFTMirr page. */
1063 mirr_page
= ntfs_map_page(vol
->mftmirr_ino
->i_mapping
,
1065 if (IS_ERR(mirr_page
)) {
1066 ntfs_error(sb
, "Failed to read $MFTMirr.");
1069 kmirr
= page_address(mirr_page
);
1072 /* Make sure the record is ok. */
1073 if (ntfs_is_baad_recordp((le32
*)kmft
)) {
1074 ntfs_error(sb
, "Incomplete multi sector transfer "
1075 "detected in mft record %i.", i
);
1077 ntfs_unmap_page(mirr_page
);
1079 ntfs_unmap_page(mft_page
);
1082 if (ntfs_is_baad_recordp((le32
*)kmirr
)) {
1083 ntfs_error(sb
, "Incomplete multi sector transfer "
1084 "detected in mft mirror record %i.", i
);
1087 /* Get the amount of data in the current record. */
1088 bytes
= le32_to_cpu(((MFT_RECORD
*)kmft
)->bytes_in_use
);
1089 if (!bytes
|| bytes
> vol
->mft_record_size
) {
1090 bytes
= le32_to_cpu(((MFT_RECORD
*)kmirr
)->bytes_in_use
);
1091 if (!bytes
|| bytes
> vol
->mft_record_size
)
1092 bytes
= vol
->mft_record_size
;
1094 /* Compare the two records. */
1095 if (memcmp(kmft
, kmirr
, bytes
)) {
1096 ntfs_error(sb
, "$MFT and $MFTMirr (record %i) do not "
1097 "match. Run ntfsfix or chkdsk.", i
);
1100 kmft
+= vol
->mft_record_size
;
1101 kmirr
+= vol
->mft_record_size
;
1102 } while (++i
< vol
->mftmirr_size
);
1103 /* Release the last pages. */
1104 ntfs_unmap_page(mft_page
);
1105 ntfs_unmap_page(mirr_page
);
1107 /* Construct the mft mirror runlist by hand. */
1109 rl2
[0].lcn
= vol
->mftmirr_lcn
;
1110 rl2
[0].length
= (vol
->mftmirr_size
* vol
->mft_record_size
+
1111 vol
->cluster_size
- 1) / vol
->cluster_size
;
1112 rl2
[1].vcn
= rl2
[0].length
;
1113 rl2
[1].lcn
= LCN_ENOENT
;
1116 * Because we have just read all of the mft mirror, we know we have
1117 * mapped the full runlist for it.
1119 mirr_ni
= NTFS_I(vol
->mftmirr_ino
);
1120 down_read(&mirr_ni
->runlist
.lock
);
1121 rl
= mirr_ni
->runlist
.rl
;
1122 /* Compare the two runlists. They must be identical. */
1125 if (rl2
[i
].vcn
!= rl
[i
].vcn
|| rl2
[i
].lcn
!= rl
[i
].lcn
||
1126 rl2
[i
].length
!= rl
[i
].length
) {
1127 ntfs_error(sb
, "$MFTMirr location mismatch. "
1129 up_read(&mirr_ni
->runlist
.lock
);
1132 } while (rl2
[i
++].length
);
1133 up_read(&mirr_ni
->runlist
.lock
);
1134 ntfs_debug("Done.");
1139 * load_and_check_logfile - load and check the logfile inode for a volume
1140 * @vol: ntfs super block describing device whose logfile to load
1142 * Return TRUE on success or FALSE on error.
1144 static BOOL
load_and_check_logfile(ntfs_volume
*vol
,
1145 RESTART_PAGE_HEADER
**rp
)
1147 struct inode
*tmp_ino
;
1149 ntfs_debug("Entering.");
1150 tmp_ino
= ntfs_iget(vol
->sb
, FILE_LogFile
);
1151 if (IS_ERR(tmp_ino
) || is_bad_inode(tmp_ino
)) {
1152 if (!IS_ERR(tmp_ino
))
1154 /* Caller will display error message. */
1157 if (!ntfs_check_logfile(tmp_ino
, rp
)) {
1159 /* ntfs_check_logfile() will have displayed error output. */
1162 NInoSetSparseDisabled(NTFS_I(tmp_ino
));
1163 vol
->logfile_ino
= tmp_ino
;
1164 ntfs_debug("Done.");
1168 #define NTFS_HIBERFIL_HEADER_SIZE 4096
1171 * check_windows_hibernation_status - check if Windows is suspended on a volume
1172 * @vol: ntfs super block of device to check
1174 * Check if Windows is hibernated on the ntfs volume @vol. This is done by
1175 * looking for the file hiberfil.sys in the root directory of the volume. If
1176 * the file is not present Windows is definitely not suspended.
1178 * If hiberfil.sys exists and is less than 4kiB in size it means Windows is
1179 * definitely suspended (this volume is not the system volume). Caveat: on a
1180 * system with many volumes it is possible that the < 4kiB check is bogus but
1181 * for now this should do fine.
1183 * If hiberfil.sys exists and is larger than 4kiB in size, we need to read the
1184 * hiberfil header (which is the first 4kiB). If this begins with "hibr",
1185 * Windows is definitely suspended. If it is completely full of zeroes,
1186 * Windows is definitely not hibernated. Any other case is treated as if
1187 * Windows is suspended. This caters for the above mentioned caveat of a
1188 * system with many volumes where no "hibr" magic would be present and there is
1191 * Return 0 if Windows is not hibernated on the volume, >0 if Windows is
1192 * hibernated on the volume, and -errno on error.
1194 static int check_windows_hibernation_status(ntfs_volume
*vol
)
1201 ntfs_name
*name
= NULL
;
1203 static const ntfschar hiberfil
[13] = { const_cpu_to_le16('h'),
1204 const_cpu_to_le16('i'), const_cpu_to_le16('b'),
1205 const_cpu_to_le16('e'), const_cpu_to_le16('r'),
1206 const_cpu_to_le16('f'), const_cpu_to_le16('i'),
1207 const_cpu_to_le16('l'), const_cpu_to_le16('.'),
1208 const_cpu_to_le16('s'), const_cpu_to_le16('y'),
1209 const_cpu_to_le16('s'), 0 };
1211 ntfs_debug("Entering.");
1213 * Find the inode number for the hibernation file by looking up the
1214 * filename hiberfil.sys in the root directory.
1216 down(&vol
->root_ino
->i_sem
);
1217 mref
= ntfs_lookup_inode_by_name(NTFS_I(vol
->root_ino
), hiberfil
, 12,
1219 up(&vol
->root_ino
->i_sem
);
1220 if (IS_ERR_MREF(mref
)) {
1221 ret
= MREF_ERR(mref
);
1222 /* If the file does not exist, Windows is not hibernated. */
1223 if (ret
== -ENOENT
) {
1224 ntfs_debug("hiberfil.sys not present. Windows is not "
1225 "hibernated on the volume.");
1228 /* A real error occured. */
1229 ntfs_error(vol
->sb
, "Failed to find inode number for "
1233 /* We do not care for the type of match that was found. */
1235 /* Get the inode. */
1236 vi
= ntfs_iget(vol
->sb
, MREF(mref
));
1237 if (IS_ERR(vi
) || is_bad_inode(vi
)) {
1240 ntfs_error(vol
->sb
, "Failed to load hiberfil.sys.");
1241 return IS_ERR(vi
) ? PTR_ERR(vi
) : -EIO
;
1243 if (unlikely(i_size_read(vi
) < NTFS_HIBERFIL_HEADER_SIZE
)) {
1244 ntfs_debug("hiberfil.sys is smaller than 4kiB (0x%llx). "
1245 "Windows is hibernated on the volume. This "
1246 "is not the system volume.", i_size_read(vi
));
1250 page
= ntfs_map_page(vi
->i_mapping
, 0);
1252 ntfs_error(vol
->sb
, "Failed to read from hiberfil.sys.");
1253 ret
= PTR_ERR(page
);
1256 kaddr
= (u32
*)page_address(page
);
1257 if (*(le32
*)kaddr
== const_cpu_to_le32(0x72626968)/*'hibr'*/) {
1258 ntfs_debug("Magic \"hibr\" found in hiberfil.sys. Windows is "
1259 "hibernated on the volume. This is the "
1263 kend
= kaddr
+ NTFS_HIBERFIL_HEADER_SIZE
/sizeof(*kaddr
);
1265 if (unlikely(*kaddr
)) {
1266 ntfs_debug("hiberfil.sys is larger than 4kiB "
1267 "(0x%llx), does not contain the "
1268 "\"hibr\" magic, and does not have a "
1269 "zero header. Windows is hibernated "
1270 "on the volume. This is not the "
1271 "system volume.", i_size_read(vi
));
1274 } while (++kaddr
< kend
);
1275 ntfs_debug("hiberfil.sys contains a zero header. Windows is not "
1276 "hibernated on the volume. This is the system "
1280 ntfs_unmap_page(page
);
1287 * load_and_init_quota - load and setup the quota file for a volume if present
1288 * @vol: ntfs super block describing device whose quota file to load
1290 * Return TRUE on success or FALSE on error. If $Quota is not present, we
1291 * leave vol->quota_ino as NULL and return success.
1293 static BOOL
load_and_init_quota(ntfs_volume
*vol
)
1296 struct inode
*tmp_ino
;
1297 ntfs_name
*name
= NULL
;
1298 static const ntfschar Quota
[7] = { const_cpu_to_le16('$'),
1299 const_cpu_to_le16('Q'), const_cpu_to_le16('u'),
1300 const_cpu_to_le16('o'), const_cpu_to_le16('t'),
1301 const_cpu_to_le16('a'), 0 };
1302 static ntfschar Q
[3] = { const_cpu_to_le16('$'),
1303 const_cpu_to_le16('Q'), 0 };
1305 ntfs_debug("Entering.");
1307 * Find the inode number for the quota file by looking up the filename
1308 * $Quota in the extended system files directory $Extend.
1310 down(&vol
->extend_ino
->i_sem
);
1311 mref
= ntfs_lookup_inode_by_name(NTFS_I(vol
->extend_ino
), Quota
, 6,
1313 up(&vol
->extend_ino
->i_sem
);
1314 if (IS_ERR_MREF(mref
)) {
1316 * If the file does not exist, quotas are disabled and have
1317 * never been enabled on this volume, just return success.
1319 if (MREF_ERR(mref
) == -ENOENT
) {
1320 ntfs_debug("$Quota not present. Volume does not have "
1323 * No need to try to set quotas out of date if they are
1326 NVolSetQuotaOutOfDate(vol
);
1329 /* A real error occured. */
1330 ntfs_error(vol
->sb
, "Failed to find inode number for $Quota.");
1333 /* We do not care for the type of match that was found. */
1335 /* Get the inode. */
1336 tmp_ino
= ntfs_iget(vol
->sb
, MREF(mref
));
1337 if (IS_ERR(tmp_ino
) || is_bad_inode(tmp_ino
)) {
1338 if (!IS_ERR(tmp_ino
))
1340 ntfs_error(vol
->sb
, "Failed to load $Quota.");
1343 vol
->quota_ino
= tmp_ino
;
1344 /* Get the $Q index allocation attribute. */
1345 tmp_ino
= ntfs_index_iget(vol
->quota_ino
, Q
, 2);
1346 if (IS_ERR(tmp_ino
)) {
1347 ntfs_error(vol
->sb
, "Failed to load $Quota/$Q index.");
1350 vol
->quota_q_ino
= tmp_ino
;
1351 ntfs_debug("Done.");
1356 * load_and_init_usnjrnl - load and setup the transaction log if present
1357 * @vol: ntfs super block describing device whose usnjrnl file to load
1359 * Return TRUE on success or FALSE on error.
1361 * If $UsnJrnl is not present or in the process of being disabled, we set
1362 * NVolUsnJrnlStamped() and return success.
1364 * If the $UsnJrnl $DATA/$J attribute has a size equal to the lowest valid usn,
1365 * i.e. transaction logging has only just been enabled or the journal has been
1366 * stamped and nothing has been logged since, we also set NVolUsnJrnlStamped()
1367 * and return success.
1369 static BOOL
load_and_init_usnjrnl(ntfs_volume
*vol
)
1372 struct inode
*tmp_ino
;
1375 ntfs_name
*name
= NULL
;
1377 static const ntfschar UsnJrnl
[9] = { const_cpu_to_le16('$'),
1378 const_cpu_to_le16('U'), const_cpu_to_le16('s'),
1379 const_cpu_to_le16('n'), const_cpu_to_le16('J'),
1380 const_cpu_to_le16('r'), const_cpu_to_le16('n'),
1381 const_cpu_to_le16('l'), 0 };
1382 static ntfschar Max
[5] = { const_cpu_to_le16('$'),
1383 const_cpu_to_le16('M'), const_cpu_to_le16('a'),
1384 const_cpu_to_le16('x'), 0 };
1385 static ntfschar J
[3] = { const_cpu_to_le16('$'),
1386 const_cpu_to_le16('J'), 0 };
1388 ntfs_debug("Entering.");
1390 * Find the inode number for the transaction log file by looking up the
1391 * filename $UsnJrnl in the extended system files directory $Extend.
1393 down(&vol
->extend_ino
->i_sem
);
1394 mref
= ntfs_lookup_inode_by_name(NTFS_I(vol
->extend_ino
), UsnJrnl
, 8,
1396 up(&vol
->extend_ino
->i_sem
);
1397 if (IS_ERR_MREF(mref
)) {
1399 * If the file does not exist, transaction logging is disabled,
1400 * just return success.
1402 if (MREF_ERR(mref
) == -ENOENT
) {
1403 ntfs_debug("$UsnJrnl not present. Volume does not "
1404 "have transaction logging enabled.");
1407 * No need to try to stamp the transaction log if
1408 * transaction logging is not enabled.
1410 NVolSetUsnJrnlStamped(vol
);
1413 /* A real error occured. */
1414 ntfs_error(vol
->sb
, "Failed to find inode number for "
1418 /* We do not care for the type of match that was found. */
1420 /* Get the inode. */
1421 tmp_ino
= ntfs_iget(vol
->sb
, MREF(mref
));
1422 if (unlikely(IS_ERR(tmp_ino
) || is_bad_inode(tmp_ino
))) {
1423 if (!IS_ERR(tmp_ino
))
1425 ntfs_error(vol
->sb
, "Failed to load $UsnJrnl.");
1428 vol
->usnjrnl_ino
= tmp_ino
;
1430 * If the transaction log is in the process of being deleted, we can
1433 if (unlikely(vol
->vol_flags
& VOLUME_DELETE_USN_UNDERWAY
)) {
1434 ntfs_debug("$UsnJrnl in the process of being disabled. "
1435 "Volume does not have transaction logging "
1439 /* Get the $DATA/$Max attribute. */
1440 tmp_ino
= ntfs_attr_iget(vol
->usnjrnl_ino
, AT_DATA
, Max
, 4);
1441 if (IS_ERR(tmp_ino
)) {
1442 ntfs_error(vol
->sb
, "Failed to load $UsnJrnl/$DATA/$Max "
1446 vol
->usnjrnl_max_ino
= tmp_ino
;
1447 if (unlikely(i_size_read(tmp_ino
) < sizeof(USN_HEADER
))) {
1448 ntfs_error(vol
->sb
, "Found corrupt $UsnJrnl/$DATA/$Max "
1449 "attribute (size is 0x%llx but should be at "
1450 "least 0x%x bytes).", i_size_read(tmp_ino
),
1451 sizeof(USN_HEADER
));
1454 /* Get the $DATA/$J attribute. */
1455 tmp_ino
= ntfs_attr_iget(vol
->usnjrnl_ino
, AT_DATA
, J
, 2);
1456 if (IS_ERR(tmp_ino
)) {
1457 ntfs_error(vol
->sb
, "Failed to load $UsnJrnl/$DATA/$J "
1461 vol
->usnjrnl_j_ino
= tmp_ino
;
1462 /* Verify $J is non-resident and sparse. */
1463 tmp_ni
= NTFS_I(vol
->usnjrnl_j_ino
);
1464 if (unlikely(!NInoNonResident(tmp_ni
) || !NInoSparse(tmp_ni
))) {
1465 ntfs_error(vol
->sb
, "$UsnJrnl/$DATA/$J attribute is resident "
1466 "and/or not sparse.");
1469 /* Read the USN_HEADER from $DATA/$Max. */
1470 page
= ntfs_map_page(vol
->usnjrnl_max_ino
->i_mapping
, 0);
1472 ntfs_error(vol
->sb
, "Failed to read from $UsnJrnl/$DATA/$Max "
1476 uh
= (USN_HEADER
*)page_address(page
);
1477 /* Sanity check the $Max. */
1478 if (unlikely(sle64_to_cpu(uh
->allocation_delta
) >
1479 sle64_to_cpu(uh
->maximum_size
))) {
1480 ntfs_error(vol
->sb
, "Allocation delta (0x%llx) exceeds "
1481 "maximum size (0x%llx). $UsnJrnl is corrupt.",
1482 (long long)sle64_to_cpu(uh
->allocation_delta
),
1483 (long long)sle64_to_cpu(uh
->maximum_size
));
1484 ntfs_unmap_page(page
);
1488 * If the transaction log has been stamped and nothing has been written
1489 * to it since, we do not need to stamp it.
1491 if (unlikely(sle64_to_cpu(uh
->lowest_valid_usn
) >=
1492 i_size_read(vol
->usnjrnl_j_ino
))) {
1493 if (likely(sle64_to_cpu(uh
->lowest_valid_usn
) ==
1494 i_size_read(vol
->usnjrnl_j_ino
))) {
1495 ntfs_unmap_page(page
);
1496 ntfs_debug("$UsnJrnl is enabled but nothing has been "
1497 "logged since it was last stamped. "
1498 "Treating this as if the volume does "
1499 "not have transaction logging "
1503 ntfs_error(vol
->sb
, "$UsnJrnl has lowest valid usn (0x%llx) "
1504 "which is out of bounds (0x%llx). $UsnJrnl "
1506 (long long)sle64_to_cpu(uh
->lowest_valid_usn
),
1507 i_size_read(vol
->usnjrnl_j_ino
));
1508 ntfs_unmap_page(page
);
1511 ntfs_unmap_page(page
);
1512 ntfs_debug("Done.");
1517 * load_and_init_attrdef - load the attribute definitions table for a volume
1518 * @vol: ntfs super block describing device whose attrdef to load
1520 * Return TRUE on success or FALSE on error.
1522 static BOOL
load_and_init_attrdef(ntfs_volume
*vol
)
1525 struct super_block
*sb
= vol
->sb
;
1528 pgoff_t index
, max_index
;
1531 ntfs_debug("Entering.");
1532 /* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */
1533 ino
= ntfs_iget(sb
, FILE_AttrDef
);
1534 if (IS_ERR(ino
) || is_bad_inode(ino
)) {
1539 NInoSetSparseDisabled(NTFS_I(ino
));
1540 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
1541 i_size
= i_size_read(ino
);
1542 if (i_size
<= 0 || i_size
> 0x7fffffff)
1544 vol
->attrdef
= (ATTR_DEF
*)ntfs_malloc_nofs(i_size
);
1548 max_index
= i_size
>> PAGE_CACHE_SHIFT
;
1549 size
= PAGE_CACHE_SIZE
;
1550 while (index
< max_index
) {
1551 /* Read the attrdef table and copy it into the linear buffer. */
1552 read_partial_attrdef_page
:
1553 page
= ntfs_map_page(ino
->i_mapping
, index
);
1555 goto free_iput_failed
;
1556 memcpy((u8
*)vol
->attrdef
+ (index
++ << PAGE_CACHE_SHIFT
),
1557 page_address(page
), size
);
1558 ntfs_unmap_page(page
);
1560 if (size
== PAGE_CACHE_SIZE
) {
1561 size
= i_size
& ~PAGE_CACHE_MASK
;
1563 goto read_partial_attrdef_page
;
1565 vol
->attrdef_size
= i_size
;
1566 ntfs_debug("Read %llu bytes from $AttrDef.", i_size
);
1570 ntfs_free(vol
->attrdef
);
1571 vol
->attrdef
= NULL
;
1575 ntfs_error(sb
, "Failed to initialize attribute definition table.");
1579 #endif /* NTFS_RW */
1582 * load_and_init_upcase - load the upcase table for an ntfs volume
1583 * @vol: ntfs super block describing device whose upcase to load
1585 * Return TRUE on success or FALSE on error.
1587 static BOOL
load_and_init_upcase(ntfs_volume
*vol
)
1590 struct super_block
*sb
= vol
->sb
;
1593 pgoff_t index
, max_index
;
1597 ntfs_debug("Entering.");
1598 /* Read upcase table and setup vol->upcase and vol->upcase_len. */
1599 ino
= ntfs_iget(sb
, FILE_UpCase
);
1600 if (IS_ERR(ino
) || is_bad_inode(ino
)) {
1606 * The upcase size must not be above 64k Unicode characters, must not
1607 * be zero and must be a multiple of sizeof(ntfschar).
1609 i_size
= i_size_read(ino
);
1610 if (!i_size
|| i_size
& (sizeof(ntfschar
) - 1) ||
1611 i_size
> 64ULL * 1024 * sizeof(ntfschar
))
1612 goto iput_upcase_failed
;
1613 vol
->upcase
= (ntfschar
*)ntfs_malloc_nofs(i_size
);
1615 goto iput_upcase_failed
;
1617 max_index
= i_size
>> PAGE_CACHE_SHIFT
;
1618 size
= PAGE_CACHE_SIZE
;
1619 while (index
< max_index
) {
1620 /* Read the upcase table and copy it into the linear buffer. */
1621 read_partial_upcase_page
:
1622 page
= ntfs_map_page(ino
->i_mapping
, index
);
1624 goto iput_upcase_failed
;
1625 memcpy((char*)vol
->upcase
+ (index
++ << PAGE_CACHE_SHIFT
),
1626 page_address(page
), size
);
1627 ntfs_unmap_page(page
);
1629 if (size
== PAGE_CACHE_SIZE
) {
1630 size
= i_size
& ~PAGE_CACHE_MASK
;
1632 goto read_partial_upcase_page
;
1634 vol
->upcase_len
= i_size
>> UCHAR_T_SIZE_BITS
;
1635 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
1636 i_size
, 64 * 1024 * sizeof(ntfschar
));
1639 if (!default_upcase
) {
1640 ntfs_debug("Using volume specified $UpCase since default is "
1645 max
= default_upcase_len
;
1646 if (max
> vol
->upcase_len
)
1647 max
= vol
->upcase_len
;
1648 for (i
= 0; i
< max
; i
++)
1649 if (vol
->upcase
[i
] != default_upcase
[i
])
1652 ntfs_free(vol
->upcase
);
1653 vol
->upcase
= default_upcase
;
1654 vol
->upcase_len
= max
;
1655 ntfs_nr_upcase_users
++;
1657 ntfs_debug("Volume specified $UpCase matches default. Using "
1662 ntfs_debug("Using volume specified $UpCase since it does not match "
1667 ntfs_free(vol
->upcase
);
1671 if (default_upcase
) {
1672 vol
->upcase
= default_upcase
;
1673 vol
->upcase_len
= default_upcase_len
;
1674 ntfs_nr_upcase_users
++;
1676 ntfs_error(sb
, "Failed to load $UpCase from the volume. Using "
1681 ntfs_error(sb
, "Failed to initialize upcase table.");
1686 * load_system_files - open the system files using normal functions
1687 * @vol: ntfs super block describing device whose system files to load
1689 * Open the system files with normal access functions and complete setting up
1690 * the ntfs super block @vol.
1692 * Return TRUE on success or FALSE on error.
1694 static BOOL
load_system_files(ntfs_volume
*vol
)
1696 struct super_block
*sb
= vol
->sb
;
1698 VOLUME_INFORMATION
*vi
;
1699 ntfs_attr_search_ctx
*ctx
;
1701 RESTART_PAGE_HEADER
*rp
;
1703 #endif /* NTFS_RW */
1705 ntfs_debug("Entering.");
1707 /* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */
1708 if (!load_and_init_mft_mirror(vol
) || !check_mft_mirror(vol
)) {
1709 static const char *es1
= "Failed to load $MFTMirr";
1710 static const char *es2
= "$MFTMirr does not match $MFT";
1711 static const char *es3
= ". Run ntfsfix and/or chkdsk.";
1713 /* If a read-write mount, convert it to a read-only mount. */
1714 if (!(sb
->s_flags
& MS_RDONLY
)) {
1715 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1716 ON_ERRORS_CONTINUE
))) {
1717 ntfs_error(sb
, "%s and neither on_errors="
1718 "continue nor on_errors="
1719 "remount-ro was specified%s",
1720 !vol
->mftmirr_ino
? es1
: es2
,
1722 goto iput_mirr_err_out
;
1724 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1725 ntfs_error(sb
, "%s. Mounting read-only%s",
1726 !vol
->mftmirr_ino
? es1
: es2
, es3
);
1728 ntfs_warning(sb
, "%s. Will not be able to remount "
1730 !vol
->mftmirr_ino
? es1
: es2
, es3
);
1731 /* This will prevent a read-write remount. */
1734 #endif /* NTFS_RW */
1735 /* Get mft bitmap attribute inode. */
1736 vol
->mftbmp_ino
= ntfs_attr_iget(vol
->mft_ino
, AT_BITMAP
, NULL
, 0);
1737 if (IS_ERR(vol
->mftbmp_ino
)) {
1738 ntfs_error(sb
, "Failed to load $MFT/$BITMAP attribute.");
1739 goto iput_mirr_err_out
;
1741 /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */
1742 if (!load_and_init_upcase(vol
))
1743 goto iput_mftbmp_err_out
;
1746 * Read attribute definitions table and setup @vol->attrdef and
1747 * @vol->attrdef_size.
1749 if (!load_and_init_attrdef(vol
))
1750 goto iput_upcase_err_out
;
1751 #endif /* NTFS_RW */
1753 * Get the cluster allocation bitmap inode and verify the size, no
1754 * need for any locking at this stage as we are already running
1755 * exclusively as we are mount in progress task.
1757 vol
->lcnbmp_ino
= ntfs_iget(sb
, FILE_Bitmap
);
1758 if (IS_ERR(vol
->lcnbmp_ino
) || is_bad_inode(vol
->lcnbmp_ino
)) {
1759 if (!IS_ERR(vol
->lcnbmp_ino
))
1760 iput(vol
->lcnbmp_ino
);
1763 NInoSetSparseDisabled(NTFS_I(vol
->lcnbmp_ino
));
1764 if ((vol
->nr_clusters
+ 7) >> 3 > i_size_read(vol
->lcnbmp_ino
)) {
1765 iput(vol
->lcnbmp_ino
);
1767 ntfs_error(sb
, "Failed to load $Bitmap.");
1768 goto iput_attrdef_err_out
;
1771 * Get the volume inode and setup our cache of the volume flags and
1774 vol
->vol_ino
= ntfs_iget(sb
, FILE_Volume
);
1775 if (IS_ERR(vol
->vol_ino
) || is_bad_inode(vol
->vol_ino
)) {
1776 if (!IS_ERR(vol
->vol_ino
))
1779 ntfs_error(sb
, "Failed to load $Volume.");
1780 goto iput_lcnbmp_err_out
;
1782 m
= map_mft_record(NTFS_I(vol
->vol_ino
));
1788 if (!(ctx
= ntfs_attr_get_search_ctx(NTFS_I(vol
->vol_ino
), m
))) {
1789 ntfs_error(sb
, "Failed to get attribute search context.");
1790 goto get_ctx_vol_failed
;
1792 if (ntfs_attr_lookup(AT_VOLUME_INFORMATION
, NULL
, 0, 0, 0, NULL
, 0,
1793 ctx
) || ctx
->attr
->non_resident
|| ctx
->attr
->flags
) {
1795 ntfs_attr_put_search_ctx(ctx
);
1797 unmap_mft_record(NTFS_I(vol
->vol_ino
));
1798 goto iput_volume_failed
;
1800 vi
= (VOLUME_INFORMATION
*)((char*)ctx
->attr
+
1801 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
));
1802 /* Some bounds checks. */
1803 if ((u8
*)vi
< (u8
*)ctx
->attr
|| (u8
*)vi
+
1804 le32_to_cpu(ctx
->attr
->data
.resident
.value_length
) >
1805 (u8
*)ctx
->attr
+ le32_to_cpu(ctx
->attr
->length
))
1807 /* Copy the volume flags and version to the ntfs_volume structure. */
1808 vol
->vol_flags
= vi
->flags
;
1809 vol
->major_ver
= vi
->major_ver
;
1810 vol
->minor_ver
= vi
->minor_ver
;
1811 ntfs_attr_put_search_ctx(ctx
);
1812 unmap_mft_record(NTFS_I(vol
->vol_ino
));
1813 printk(KERN_INFO
"NTFS volume version %i.%i.\n", vol
->major_ver
,
1815 if (vol
->major_ver
< 3 && NVolSparseEnabled(vol
)) {
1816 ntfs_warning(vol
->sb
, "Disabling sparse support due to NTFS "
1817 "volume version %i.%i (need at least version "
1818 "3.0).", vol
->major_ver
, vol
->minor_ver
);
1819 NVolClearSparseEnabled(vol
);
1822 /* Make sure that no unsupported volume flags are set. */
1823 if (vol
->vol_flags
& VOLUME_MUST_MOUNT_RO_MASK
) {
1824 static const char *es1a
= "Volume is dirty";
1825 static const char *es1b
= "Volume has unsupported flags set";
1826 static const char *es2
= ". Run chkdsk and mount in Windows.";
1829 es1
= vol
->vol_flags
& VOLUME_IS_DIRTY
? es1a
: es1b
;
1830 /* If a read-write mount, convert it to a read-only mount. */
1831 if (!(sb
->s_flags
& MS_RDONLY
)) {
1832 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1833 ON_ERRORS_CONTINUE
))) {
1834 ntfs_error(sb
, "%s and neither on_errors="
1835 "continue nor on_errors="
1836 "remount-ro was specified%s",
1838 goto iput_vol_err_out
;
1840 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1841 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
1843 ntfs_warning(sb
, "%s. Will not be able to remount "
1844 "read-write%s", es1
, es2
);
1846 * Do not set NVolErrors() because ntfs_remount() re-checks the
1847 * flags which we need to do in case any flags have changed.
1851 * Get the inode for the logfile, check it and determine if the volume
1852 * was shutdown cleanly.
1855 if (!load_and_check_logfile(vol
, &rp
) ||
1856 !ntfs_is_logfile_clean(vol
->logfile_ino
, rp
)) {
1857 static const char *es1a
= "Failed to load $LogFile";
1858 static const char *es1b
= "$LogFile is not clean";
1859 static const char *es2
= ". Mount in Windows.";
1862 es1
= !vol
->logfile_ino
? es1a
: es1b
;
1863 /* If a read-write mount, convert it to a read-only mount. */
1864 if (!(sb
->s_flags
& MS_RDONLY
)) {
1865 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1866 ON_ERRORS_CONTINUE
))) {
1867 ntfs_error(sb
, "%s and neither on_errors="
1868 "continue nor on_errors="
1869 "remount-ro was specified%s",
1871 if (vol
->logfile_ino
) {
1875 goto iput_logfile_err_out
;
1877 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1878 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
1880 ntfs_warning(sb
, "%s. Will not be able to remount "
1881 "read-write%s", es1
, es2
);
1882 /* This will prevent a read-write remount. */
1886 #endif /* NTFS_RW */
1887 /* Get the root directory inode so we can do path lookups. */
1888 vol
->root_ino
= ntfs_iget(sb
, FILE_root
);
1889 if (IS_ERR(vol
->root_ino
) || is_bad_inode(vol
->root_ino
)) {
1890 if (!IS_ERR(vol
->root_ino
))
1891 iput(vol
->root_ino
);
1892 ntfs_error(sb
, "Failed to load root directory.");
1893 goto iput_logfile_err_out
;
1897 * Check if Windows is suspended to disk on the target volume. If it
1898 * is hibernated, we must not write *anything* to the disk so set
1899 * NVolErrors() without setting the dirty volume flag and mount
1900 * read-only. This will prevent read-write remounting and it will also
1901 * prevent all writes.
1903 err
= check_windows_hibernation_status(vol
);
1904 if (unlikely(err
)) {
1905 static const char *es1a
= "Failed to determine if Windows is "
1907 static const char *es1b
= "Windows is hibernated";
1908 static const char *es2
= ". Run chkdsk.";
1911 es1
= err
< 0 ? es1a
: es1b
;
1912 /* If a read-write mount, convert it to a read-only mount. */
1913 if (!(sb
->s_flags
& MS_RDONLY
)) {
1914 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1915 ON_ERRORS_CONTINUE
))) {
1916 ntfs_error(sb
, "%s and neither on_errors="
1917 "continue nor on_errors="
1918 "remount-ro was specified%s",
1920 goto iput_root_err_out
;
1922 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1923 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
1925 ntfs_warning(sb
, "%s. Will not be able to remount "
1926 "read-write%s", es1
, es2
);
1927 /* This will prevent a read-write remount. */
1930 /* If (still) a read-write mount, mark the volume dirty. */
1931 if (!(sb
->s_flags
& MS_RDONLY
) &&
1932 ntfs_set_volume_flags(vol
, VOLUME_IS_DIRTY
)) {
1933 static const char *es1
= "Failed to set dirty bit in volume "
1934 "information flags";
1935 static const char *es2
= ". Run chkdsk.";
1937 /* Convert to a read-only mount. */
1938 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1939 ON_ERRORS_CONTINUE
))) {
1940 ntfs_error(sb
, "%s and neither on_errors=continue nor "
1941 "on_errors=remount-ro was specified%s",
1943 goto iput_root_err_out
;
1945 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
1946 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1948 * Do not set NVolErrors() because ntfs_remount() might manage
1949 * to set the dirty flag in which case all would be well.
1953 // TODO: Enable this code once we start modifying anything that is
1954 // different between NTFS 1.2 and 3.x...
1956 * If (still) a read-write mount, set the NT4 compatibility flag on
1957 * newer NTFS version volumes.
1959 if (!(sb
->s_flags
& MS_RDONLY
) && (vol
->major_ver
> 1) &&
1960 ntfs_set_volume_flags(vol
, VOLUME_MOUNTED_ON_NT4
)) {
1961 static const char *es1
= "Failed to set NT4 compatibility flag";
1962 static const char *es2
= ". Run chkdsk.";
1964 /* Convert to a read-only mount. */
1965 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1966 ON_ERRORS_CONTINUE
))) {
1967 ntfs_error(sb
, "%s and neither on_errors=continue nor "
1968 "on_errors=remount-ro was specified%s",
1970 goto iput_root_err_out
;
1972 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
1973 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1977 /* If (still) a read-write mount, empty the logfile. */
1978 if (!(sb
->s_flags
& MS_RDONLY
) &&
1979 !ntfs_empty_logfile(vol
->logfile_ino
)) {
1980 static const char *es1
= "Failed to empty $LogFile";
1981 static const char *es2
= ". Mount in Windows.";
1983 /* Convert to a read-only mount. */
1984 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1985 ON_ERRORS_CONTINUE
))) {
1986 ntfs_error(sb
, "%s and neither on_errors=continue nor "
1987 "on_errors=remount-ro was specified%s",
1989 goto iput_root_err_out
;
1991 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
1992 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1995 #endif /* NTFS_RW */
1996 /* If on NTFS versions before 3.0, we are done. */
1997 if (unlikely(vol
->major_ver
< 3))
1999 /* NTFS 3.0+ specific initialization. */
2000 /* Get the security descriptors inode. */
2001 vol
->secure_ino
= ntfs_iget(sb
, FILE_Secure
);
2002 if (IS_ERR(vol
->secure_ino
) || is_bad_inode(vol
->secure_ino
)) {
2003 if (!IS_ERR(vol
->secure_ino
))
2004 iput(vol
->secure_ino
);
2005 ntfs_error(sb
, "Failed to load $Secure.");
2006 goto iput_root_err_out
;
2008 // TODO: Initialize security.
2009 /* Get the extended system files' directory inode. */
2010 vol
->extend_ino
= ntfs_iget(sb
, FILE_Extend
);
2011 if (IS_ERR(vol
->extend_ino
) || is_bad_inode(vol
->extend_ino
)) {
2012 if (!IS_ERR(vol
->extend_ino
))
2013 iput(vol
->extend_ino
);
2014 ntfs_error(sb
, "Failed to load $Extend.");
2015 goto iput_sec_err_out
;
2018 /* Find the quota file, load it if present, and set it up. */
2019 if (!load_and_init_quota(vol
)) {
2020 static const char *es1
= "Failed to load $Quota";
2021 static const char *es2
= ". Run chkdsk.";
2023 /* If a read-write mount, convert it to a read-only mount. */
2024 if (!(sb
->s_flags
& MS_RDONLY
)) {
2025 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
2026 ON_ERRORS_CONTINUE
))) {
2027 ntfs_error(sb
, "%s and neither on_errors="
2028 "continue nor on_errors="
2029 "remount-ro was specified%s",
2031 goto iput_quota_err_out
;
2033 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
2034 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
2036 ntfs_warning(sb
, "%s. Will not be able to remount "
2037 "read-write%s", es1
, es2
);
2038 /* This will prevent a read-write remount. */
2041 /* If (still) a read-write mount, mark the quotas out of date. */
2042 if (!(sb
->s_flags
& MS_RDONLY
) &&
2043 !ntfs_mark_quotas_out_of_date(vol
)) {
2044 static const char *es1
= "Failed to mark quotas out of date";
2045 static const char *es2
= ". Run chkdsk.";
2047 /* Convert to a read-only mount. */
2048 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
2049 ON_ERRORS_CONTINUE
))) {
2050 ntfs_error(sb
, "%s and neither on_errors=continue nor "
2051 "on_errors=remount-ro was specified%s",
2053 goto iput_quota_err_out
;
2055 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
2056 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
2060 * Find the transaction log file ($UsnJrnl), load it if present, check
2061 * it, and set it up.
2063 if (!load_and_init_usnjrnl(vol
)) {
2064 static const char *es1
= "Failed to load $UsnJrnl";
2065 static const char *es2
= ". Run chkdsk.";
2067 /* If a read-write mount, convert it to a read-only mount. */
2068 if (!(sb
->s_flags
& MS_RDONLY
)) {
2069 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
2070 ON_ERRORS_CONTINUE
))) {
2071 ntfs_error(sb
, "%s and neither on_errors="
2072 "continue nor on_errors="
2073 "remount-ro was specified%s",
2075 goto iput_usnjrnl_err_out
;
2077 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
2078 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
2080 ntfs_warning(sb
, "%s. Will not be able to remount "
2081 "read-write%s", es1
, es2
);
2082 /* This will prevent a read-write remount. */
2085 /* If (still) a read-write mount, stamp the transaction log. */
2086 if (!(sb
->s_flags
& MS_RDONLY
) && !ntfs_stamp_usnjrnl(vol
)) {
2087 static const char *es1
= "Failed to stamp transaction log "
2089 static const char *es2
= ". Run chkdsk.";
2091 /* Convert to a read-only mount. */
2092 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
2093 ON_ERRORS_CONTINUE
))) {
2094 ntfs_error(sb
, "%s and neither on_errors=continue nor "
2095 "on_errors=remount-ro was specified%s",
2097 goto iput_usnjrnl_err_out
;
2099 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
2100 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
2103 #endif /* NTFS_RW */
2106 iput_usnjrnl_err_out
:
2107 if (vol
->usnjrnl_j_ino
)
2108 iput(vol
->usnjrnl_j_ino
);
2109 if (vol
->usnjrnl_max_ino
)
2110 iput(vol
->usnjrnl_max_ino
);
2111 if (vol
->usnjrnl_ino
)
2112 iput(vol
->usnjrnl_ino
);
2114 if (vol
->quota_q_ino
)
2115 iput(vol
->quota_q_ino
);
2117 iput(vol
->quota_ino
);
2118 iput(vol
->extend_ino
);
2119 #endif /* NTFS_RW */
2121 iput(vol
->secure_ino
);
2123 iput(vol
->root_ino
);
2124 iput_logfile_err_out
:
2126 if (vol
->logfile_ino
)
2127 iput(vol
->logfile_ino
);
2129 #endif /* NTFS_RW */
2131 iput_lcnbmp_err_out
:
2132 iput(vol
->lcnbmp_ino
);
2133 iput_attrdef_err_out
:
2134 vol
->attrdef_size
= 0;
2136 ntfs_free(vol
->attrdef
);
2137 vol
->attrdef
= NULL
;
2140 iput_upcase_err_out
:
2141 #endif /* NTFS_RW */
2142 vol
->upcase_len
= 0;
2144 if (vol
->upcase
== default_upcase
) {
2145 ntfs_nr_upcase_users
--;
2150 ntfs_free(vol
->upcase
);
2153 iput_mftbmp_err_out
:
2154 iput(vol
->mftbmp_ino
);
2157 if (vol
->mftmirr_ino
)
2158 iput(vol
->mftmirr_ino
);
2159 #endif /* NTFS_RW */
2164 * ntfs_put_super - called by the vfs to unmount a volume
2165 * @sb: vfs superblock of volume to unmount
2167 * ntfs_put_super() is called by the VFS (from fs/super.c::do_umount()) when
2168 * the volume is being unmounted (umount system call has been invoked) and it
2169 * releases all inodes and memory belonging to the NTFS specific part of the
2172 static void ntfs_put_super(struct super_block
*sb
)
2174 ntfs_volume
*vol
= NTFS_SB(sb
);
2176 ntfs_debug("Entering.");
2179 * Commit all inodes while they are still open in case some of them
2180 * cause others to be dirtied.
2182 ntfs_commit_inode(vol
->vol_ino
);
2184 /* NTFS 3.0+ specific. */
2185 if (vol
->major_ver
>= 3) {
2186 if (vol
->usnjrnl_j_ino
)
2187 ntfs_commit_inode(vol
->usnjrnl_j_ino
);
2188 if (vol
->usnjrnl_max_ino
)
2189 ntfs_commit_inode(vol
->usnjrnl_max_ino
);
2190 if (vol
->usnjrnl_ino
)
2191 ntfs_commit_inode(vol
->usnjrnl_ino
);
2192 if (vol
->quota_q_ino
)
2193 ntfs_commit_inode(vol
->quota_q_ino
);
2195 ntfs_commit_inode(vol
->quota_ino
);
2196 if (vol
->extend_ino
)
2197 ntfs_commit_inode(vol
->extend_ino
);
2198 if (vol
->secure_ino
)
2199 ntfs_commit_inode(vol
->secure_ino
);
2202 ntfs_commit_inode(vol
->root_ino
);
2204 down_write(&vol
->lcnbmp_lock
);
2205 ntfs_commit_inode(vol
->lcnbmp_ino
);
2206 up_write(&vol
->lcnbmp_lock
);
2208 down_write(&vol
->mftbmp_lock
);
2209 ntfs_commit_inode(vol
->mftbmp_ino
);
2210 up_write(&vol
->mftbmp_lock
);
2212 if (vol
->logfile_ino
)
2213 ntfs_commit_inode(vol
->logfile_ino
);
2215 if (vol
->mftmirr_ino
)
2216 ntfs_commit_inode(vol
->mftmirr_ino
);
2217 ntfs_commit_inode(vol
->mft_ino
);
2220 * If a read-write mount and no volume errors have occured, mark the
2221 * volume clean. Also, re-commit all affected inodes.
2223 if (!(sb
->s_flags
& MS_RDONLY
)) {
2224 if (!NVolErrors(vol
)) {
2225 if (ntfs_clear_volume_flags(vol
, VOLUME_IS_DIRTY
))
2226 ntfs_warning(sb
, "Failed to clear dirty bit "
2227 "in volume information "
2228 "flags. Run chkdsk.");
2229 ntfs_commit_inode(vol
->vol_ino
);
2230 ntfs_commit_inode(vol
->root_ino
);
2231 if (vol
->mftmirr_ino
)
2232 ntfs_commit_inode(vol
->mftmirr_ino
);
2233 ntfs_commit_inode(vol
->mft_ino
);
2235 ntfs_warning(sb
, "Volume has errors. Leaving volume "
2236 "marked dirty. Run chkdsk.");
2239 #endif /* NTFS_RW */
2242 vol
->vol_ino
= NULL
;
2244 /* NTFS 3.0+ specific clean up. */
2245 if (vol
->major_ver
>= 3) {
2247 if (vol
->usnjrnl_j_ino
) {
2248 iput(vol
->usnjrnl_j_ino
);
2249 vol
->usnjrnl_j_ino
= NULL
;
2251 if (vol
->usnjrnl_max_ino
) {
2252 iput(vol
->usnjrnl_max_ino
);
2253 vol
->usnjrnl_max_ino
= NULL
;
2255 if (vol
->usnjrnl_ino
) {
2256 iput(vol
->usnjrnl_ino
);
2257 vol
->usnjrnl_ino
= NULL
;
2259 if (vol
->quota_q_ino
) {
2260 iput(vol
->quota_q_ino
);
2261 vol
->quota_q_ino
= NULL
;
2263 if (vol
->quota_ino
) {
2264 iput(vol
->quota_ino
);
2265 vol
->quota_ino
= NULL
;
2267 #endif /* NTFS_RW */
2268 if (vol
->extend_ino
) {
2269 iput(vol
->extend_ino
);
2270 vol
->extend_ino
= NULL
;
2272 if (vol
->secure_ino
) {
2273 iput(vol
->secure_ino
);
2274 vol
->secure_ino
= NULL
;
2278 iput(vol
->root_ino
);
2279 vol
->root_ino
= NULL
;
2281 down_write(&vol
->lcnbmp_lock
);
2282 iput(vol
->lcnbmp_ino
);
2283 vol
->lcnbmp_ino
= NULL
;
2284 up_write(&vol
->lcnbmp_lock
);
2286 down_write(&vol
->mftbmp_lock
);
2287 iput(vol
->mftbmp_ino
);
2288 vol
->mftbmp_ino
= NULL
;
2289 up_write(&vol
->mftbmp_lock
);
2292 if (vol
->logfile_ino
) {
2293 iput(vol
->logfile_ino
);
2294 vol
->logfile_ino
= NULL
;
2296 if (vol
->mftmirr_ino
) {
2297 /* Re-commit the mft mirror and mft just in case. */
2298 ntfs_commit_inode(vol
->mftmirr_ino
);
2299 ntfs_commit_inode(vol
->mft_ino
);
2300 iput(vol
->mftmirr_ino
);
2301 vol
->mftmirr_ino
= NULL
;
2304 * If any dirty inodes are left, throw away all mft data page cache
2305 * pages to allow a clean umount. This should never happen any more
2306 * due to mft.c::ntfs_mft_writepage() cleaning all the dirty pages as
2307 * the underlying mft records are written out and cleaned. If it does,
2308 * happen anyway, we want to know...
2310 ntfs_commit_inode(vol
->mft_ino
);
2311 write_inode_now(vol
->mft_ino
, 1);
2312 if (!list_empty(&sb
->s_dirty
)) {
2313 const char *s1
, *s2
;
2315 down(&vol
->mft_ino
->i_sem
);
2316 truncate_inode_pages(vol
->mft_ino
->i_mapping
, 0);
2317 up(&vol
->mft_ino
->i_sem
);
2318 write_inode_now(vol
->mft_ino
, 1);
2319 if (!list_empty(&sb
->s_dirty
)) {
2320 static const char *_s1
= "inodes";
2321 static const char *_s2
= "";
2325 static const char *_s1
= "mft pages";
2326 static const char *_s2
= "They have been thrown "
2331 ntfs_error(sb
, "Dirty %s found at umount time. %sYou should "
2332 "run chkdsk. Please email "
2333 "linux-ntfs-dev@lists.sourceforge.net and say "
2334 "that you saw this message. Thank you.", s1
,
2337 #endif /* NTFS_RW */
2340 vol
->mft_ino
= NULL
;
2342 /* Throw away the table of attribute definitions. */
2343 vol
->attrdef_size
= 0;
2345 ntfs_free(vol
->attrdef
);
2346 vol
->attrdef
= NULL
;
2348 vol
->upcase_len
= 0;
2350 * Destroy the global default upcase table if necessary. Also decrease
2351 * the number of upcase users if we are a user.
2354 if (vol
->upcase
== default_upcase
) {
2355 ntfs_nr_upcase_users
--;
2358 if (!ntfs_nr_upcase_users
&& default_upcase
) {
2359 ntfs_free(default_upcase
);
2360 default_upcase
= NULL
;
2362 if (vol
->cluster_size
<= 4096 && !--ntfs_nr_compression_users
)
2363 free_compression_buffers();
2366 ntfs_free(vol
->upcase
);
2370 unload_nls(vol
->nls_map
);
2371 vol
->nls_map
= NULL
;
2373 sb
->s_fs_info
= NULL
;
2379 * get_nr_free_clusters - return the number of free clusters on a volume
2380 * @vol: ntfs volume for which to obtain free cluster count
2382 * Calculate the number of free clusters on the mounted NTFS volume @vol. We
2383 * actually calculate the number of clusters in use instead because this
2384 * allows us to not care about partial pages as these will be just zero filled
2385 * and hence not be counted as allocated clusters.
2387 * The only particularity is that clusters beyond the end of the logical ntfs
2388 * volume will be marked as allocated to prevent errors which means we have to
2389 * discount those at the end. This is important as the cluster bitmap always
2390 * has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside
2391 * the logical volume and marked in use when they are not as they do not exist.
2393 * If any pages cannot be read we assume all clusters in the erroring pages are
2394 * in use. This means we return an underestimate on errors which is better than
2397 static s64
get_nr_free_clusters(ntfs_volume
*vol
)
2399 s64 nr_free
= vol
->nr_clusters
;
2401 struct address_space
*mapping
= vol
->lcnbmp_ino
->i_mapping
;
2402 filler_t
*readpage
= (filler_t
*)mapping
->a_ops
->readpage
;
2404 pgoff_t index
, max_index
;
2406 ntfs_debug("Entering.");
2407 /* Serialize accesses to the cluster bitmap. */
2408 down_read(&vol
->lcnbmp_lock
);
2410 * Convert the number of bits into bytes rounded up, then convert into
2411 * multiples of PAGE_CACHE_SIZE, rounding up so that if we have one
2412 * full and one partial page max_index = 2.
2414 max_index
= (((vol
->nr_clusters
+ 7) >> 3) + PAGE_CACHE_SIZE
- 1) >>
2416 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2417 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.",
2418 max_index
, PAGE_CACHE_SIZE
/ 4);
2419 for (index
= 0; index
< max_index
; index
++) {
2422 * Read the page from page cache, getting it from backing store
2423 * if necessary, and increment the use count.
2425 page
= read_cache_page(mapping
, index
, (filler_t
*)readpage
,
2427 /* Ignore pages which errored synchronously. */
2429 ntfs_debug("Sync read_cache_page() error. Skipping "
2430 "page (index 0x%lx).", index
);
2431 nr_free
-= PAGE_CACHE_SIZE
* 8;
2434 wait_on_page_locked(page
);
2435 /* Ignore pages which errored asynchronously. */
2436 if (!PageUptodate(page
)) {
2437 ntfs_debug("Async read_cache_page() error. Skipping "
2438 "page (index 0x%lx).", index
);
2439 page_cache_release(page
);
2440 nr_free
-= PAGE_CACHE_SIZE
* 8;
2443 kaddr
= (u32
*)kmap_atomic(page
, KM_USER0
);
2445 * For each 4 bytes, subtract the number of set bits. If this
2446 * is the last page and it is partial we don't really care as
2447 * it just means we do a little extra work but it won't affect
2448 * the result as all out of range bytes are set to zero by
2451 for (i
= 0; i
< PAGE_CACHE_SIZE
/ 4; i
++)
2452 nr_free
-= (s64
)hweight32(kaddr
[i
]);
2453 kunmap_atomic(kaddr
, KM_USER0
);
2454 page_cache_release(page
);
2456 ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index
- 1);
2458 * Fixup for eventual bits outside logical ntfs volume (see function
2459 * description above).
2461 if (vol
->nr_clusters
& 63)
2462 nr_free
+= 64 - (vol
->nr_clusters
& 63);
2463 up_read(&vol
->lcnbmp_lock
);
2464 /* If errors occured we may well have gone below zero, fix this. */
2467 ntfs_debug("Exiting.");
2472 * __get_nr_free_mft_records - return the number of free inodes on a volume
2473 * @vol: ntfs volume for which to obtain free inode count
2474 * @nr_free: number of mft records in filesystem
2475 * @max_index: maximum number of pages containing set bits
2477 * Calculate the number of free mft records (inodes) on the mounted NTFS
2478 * volume @vol. We actually calculate the number of mft records in use instead
2479 * because this allows us to not care about partial pages as these will be just
2480 * zero filled and hence not be counted as allocated mft record.
2482 * If any pages cannot be read we assume all mft records in the erroring pages
2483 * are in use. This means we return an underestimate on errors which is better
2484 * than an overestimate.
2486 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
2488 static unsigned long __get_nr_free_mft_records(ntfs_volume
*vol
,
2489 s64 nr_free
, const pgoff_t max_index
)
2492 struct address_space
*mapping
= vol
->mftbmp_ino
->i_mapping
;
2493 filler_t
*readpage
= (filler_t
*)mapping
->a_ops
->readpage
;
2497 ntfs_debug("Entering.");
2498 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2499 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
2500 "0x%lx.", max_index
, PAGE_CACHE_SIZE
/ 4);
2501 for (index
= 0; index
< max_index
; index
++) {
2504 * Read the page from page cache, getting it from backing store
2505 * if necessary, and increment the use count.
2507 page
= read_cache_page(mapping
, index
, (filler_t
*)readpage
,
2509 /* Ignore pages which errored synchronously. */
2511 ntfs_debug("Sync read_cache_page() error. Skipping "
2512 "page (index 0x%lx).", index
);
2513 nr_free
-= PAGE_CACHE_SIZE
* 8;
2516 wait_on_page_locked(page
);
2517 /* Ignore pages which errored asynchronously. */
2518 if (!PageUptodate(page
)) {
2519 ntfs_debug("Async read_cache_page() error. Skipping "
2520 "page (index 0x%lx).", index
);
2521 page_cache_release(page
);
2522 nr_free
-= PAGE_CACHE_SIZE
* 8;
2525 kaddr
= (u32
*)kmap_atomic(page
, KM_USER0
);
2527 * For each 4 bytes, subtract the number of set bits. If this
2528 * is the last page and it is partial we don't really care as
2529 * it just means we do a little extra work but it won't affect
2530 * the result as all out of range bytes are set to zero by
2533 for (i
= 0; i
< PAGE_CACHE_SIZE
/ 4; i
++)
2534 nr_free
-= (s64
)hweight32(kaddr
[i
]);
2535 kunmap_atomic(kaddr
, KM_USER0
);
2536 page_cache_release(page
);
2538 ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.",
2540 /* If errors occured we may well have gone below zero, fix this. */
2543 ntfs_debug("Exiting.");
2548 * ntfs_statfs - return information about mounted NTFS volume
2549 * @sb: super block of mounted volume
2550 * @sfs: statfs structure in which to return the information
2552 * Return information about the mounted NTFS volume @sb in the statfs structure
2553 * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is
2554 * called). We interpret the values to be correct of the moment in time at
2555 * which we are called. Most values are variable otherwise and this isn't just
2556 * the free values but the totals as well. For example we can increase the
2557 * total number of file nodes if we run out and we can keep doing this until
2558 * there is no more space on the volume left at all.
2560 * Called from vfs_statfs which is used to handle the statfs, fstatfs, and
2561 * ustat system calls.
2563 * Return 0 on success or -errno on error.
2565 static int ntfs_statfs(struct super_block
*sb
, struct kstatfs
*sfs
)
2568 ntfs_volume
*vol
= NTFS_SB(sb
);
2569 ntfs_inode
*mft_ni
= NTFS_I(vol
->mft_ino
);
2571 unsigned long flags
;
2573 ntfs_debug("Entering.");
2574 /* Type of filesystem. */
2575 sfs
->f_type
= NTFS_SB_MAGIC
;
2576 /* Optimal transfer block size. */
2577 sfs
->f_bsize
= PAGE_CACHE_SIZE
;
2579 * Total data blocks in filesystem in units of f_bsize and since
2580 * inodes are also stored in data blocs ($MFT is a file) this is just
2581 * the total clusters.
2583 sfs
->f_blocks
= vol
->nr_clusters
<< vol
->cluster_size_bits
>>
2585 /* Free data blocks in filesystem in units of f_bsize. */
2586 size
= get_nr_free_clusters(vol
) << vol
->cluster_size_bits
>>
2590 /* Free blocks avail to non-superuser, same as above on NTFS. */
2591 sfs
->f_bavail
= sfs
->f_bfree
= size
;
2592 /* Serialize accesses to the inode bitmap. */
2593 down_read(&vol
->mftbmp_lock
);
2594 read_lock_irqsave(&mft_ni
->size_lock
, flags
);
2595 size
= i_size_read(vol
->mft_ino
) >> vol
->mft_record_size_bits
;
2597 * Convert the maximum number of set bits into bytes rounded up, then
2598 * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
2599 * have one full and one partial page max_index = 2.
2601 max_index
= ((((mft_ni
->initialized_size
>> vol
->mft_record_size_bits
)
2602 + 7) >> 3) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
2603 read_unlock_irqrestore(&mft_ni
->size_lock
, flags
);
2604 /* Number of inodes in filesystem (at this point in time). */
2605 sfs
->f_files
= size
;
2606 /* Free inodes in fs (based on current total count). */
2607 sfs
->f_ffree
= __get_nr_free_mft_records(vol
, size
, max_index
);
2608 up_read(&vol
->mftbmp_lock
);
2610 * File system id. This is extremely *nix flavour dependent and even
2611 * within Linux itself all fs do their own thing. I interpret this to
2612 * mean a unique id associated with the mounted fs and not the id
2613 * associated with the filesystem driver, the latter is already given
2614 * by the filesystem type in sfs->f_type. Thus we use the 64-bit
2615 * volume serial number splitting it into two 32-bit parts. We enter
2616 * the least significant 32-bits in f_fsid[0] and the most significant
2617 * 32-bits in f_fsid[1].
2619 sfs
->f_fsid
.val
[0] = vol
->serial_no
& 0xffffffff;
2620 sfs
->f_fsid
.val
[1] = (vol
->serial_no
>> 32) & 0xffffffff;
2621 /* Maximum length of filenames. */
2622 sfs
->f_namelen
= NTFS_MAX_NAME_LEN
;
2627 * The complete super operations.
2629 static struct super_operations ntfs_sops
= {
2630 .alloc_inode
= ntfs_alloc_big_inode
, /* VFS: Allocate new inode. */
2631 .destroy_inode
= ntfs_destroy_big_inode
, /* VFS: Deallocate inode. */
2632 .put_inode
= ntfs_put_inode
, /* VFS: Called just before
2633 the inode reference count
2636 //.dirty_inode = NULL, /* VFS: Called from
2637 // __mark_inode_dirty(). */
2638 .write_inode
= ntfs_write_inode
, /* VFS: Write dirty inode to
2640 //.drop_inode = NULL, /* VFS: Called just after the
2641 // inode reference count has
2642 // been decreased to zero.
2643 // NOTE: The inode lock is
2644 // held. See fs/inode.c::
2645 // generic_drop_inode(). */
2646 //.delete_inode = NULL, /* VFS: Delete inode from disk.
2647 // Called when i_count becomes
2648 // 0 and i_nlink is also 0. */
2649 //.write_super = NULL, /* Flush dirty super block to
2651 //.sync_fs = NULL, /* ? */
2652 //.write_super_lockfs = NULL, /* ? */
2653 //.unlockfs = NULL, /* ? */
2654 #endif /* NTFS_RW */
2655 .put_super
= ntfs_put_super
, /* Syscall: umount. */
2656 .statfs
= ntfs_statfs
, /* Syscall: statfs */
2657 .remount_fs
= ntfs_remount
, /* Syscall: mount -o remount. */
2658 .clear_inode
= ntfs_clear_big_inode
, /* VFS: Called when an inode is
2659 removed from memory. */
2660 //.umount_begin = NULL, /* Forced umount. */
2661 .show_options
= ntfs_show_options
, /* Show mount options in
2666 * ntfs_fill_super - mount an ntfs filesystem
2667 * @sb: super block of ntfs filesystem to mount
2668 * @opt: string containing the mount options
2669 * @silent: silence error output
2671 * ntfs_fill_super() is called by the VFS to mount the device described by @sb
2672 * with the mount otions in @data with the NTFS filesystem.
2674 * If @silent is true, remain silent even if errors are detected. This is used
2675 * during bootup, when the kernel tries to mount the root filesystem with all
2676 * registered filesystems one after the other until one succeeds. This implies
2677 * that all filesystems except the correct one will quite correctly and
2678 * expectedly return an error, but nobody wants to see error messages when in
2679 * fact this is what is supposed to happen.
2681 * NOTE: @sb->s_flags contains the mount options flags.
2683 static int ntfs_fill_super(struct super_block
*sb
, void *opt
, const int silent
)
2686 struct buffer_head
*bh
;
2687 struct inode
*tmp_ino
;
2690 ntfs_debug("Entering.");
2692 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
2693 #endif /* ! NTFS_RW */
2694 /* Allocate a new ntfs_volume and place it in sb->s_fs_info. */
2695 sb
->s_fs_info
= kmalloc(sizeof(ntfs_volume
), GFP_NOFS
);
2699 ntfs_error(sb
, "Allocation of NTFS volume structure "
2700 "failed. Aborting mount...");
2703 /* Initialize ntfs_volume structure. */
2704 *vol
= (ntfs_volume
) {
2707 * Default is group and other don't have any access to files or
2708 * directories while owner has full access. Further, files by
2709 * default are not executable but directories are of course
2715 init_rwsem(&vol
->mftbmp_lock
);
2716 init_rwsem(&vol
->lcnbmp_lock
);
2720 /* By default, enable sparse support. */
2721 NVolSetSparseEnabled(vol
);
2723 /* Important to get the mount options dealt with now. */
2724 if (!parse_options(vol
, (char*)opt
))
2728 * TODO: Fail safety check. In the future we should really be able to
2729 * cope with this being the case, but for now just bail out.
2731 if (bdev_hardsect_size(sb
->s_bdev
) > NTFS_BLOCK_SIZE
) {
2733 ntfs_error(sb
, "Device has unsupported hardsect_size.");
2737 /* Setup the device access block size to NTFS_BLOCK_SIZE. */
2738 if (sb_set_blocksize(sb
, NTFS_BLOCK_SIZE
) != NTFS_BLOCK_SIZE
) {
2740 ntfs_error(sb
, "Unable to set block size.");
2744 /* Get the size of the device in units of NTFS_BLOCK_SIZE bytes. */
2745 vol
->nr_blocks
= i_size_read(sb
->s_bdev
->bd_inode
) >>
2746 NTFS_BLOCK_SIZE_BITS
;
2748 /* Read the boot sector and return unlocked buffer head to it. */
2749 if (!(bh
= read_ntfs_boot_sector(sb
, silent
))) {
2751 ntfs_error(sb
, "Not an NTFS volume.");
2756 * Extract the data from the boot sector and setup the ntfs super block
2759 result
= parse_ntfs_boot_sector(vol
, (NTFS_BOOT_SECTOR
*)bh
->b_data
);
2761 /* Initialize the cluster and mft allocators. */
2762 ntfs_setup_allocators(vol
);
2768 ntfs_error(sb
, "Unsupported NTFS filesystem.");
2773 * TODO: When we start coping with sector sizes different from
2774 * NTFS_BLOCK_SIZE, we now probably need to set the blocksize of the
2775 * device (probably to NTFS_BLOCK_SIZE).
2778 /* Setup remaining fields in the super block. */
2779 sb
->s_magic
= NTFS_SB_MAGIC
;
2782 * Ntfs allows 63 bits for the file size, i.e. correct would be:
2783 * sb->s_maxbytes = ~0ULL >> 1;
2784 * But the kernel uses a long as the page cache page index which on
2785 * 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel
2786 * defined to the maximum the page cache page index can cope with
2787 * without overflowing the index or to 2^63 - 1, whichever is smaller.
2789 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
2791 sb
->s_time_gran
= 100;
2794 * Now load the metadata required for the page cache and our address
2795 * space operations to function. We do this by setting up a specialised
2796 * read_inode method and then just calling the normal iget() to obtain
2797 * the inode for $MFT which is sufficient to allow our normal inode
2798 * operations and associated address space operations to function.
2800 sb
->s_op
= &ntfs_sops
;
2801 tmp_ino
= new_inode(sb
);
2804 ntfs_error(sb
, "Failed to load essential metadata.");
2807 tmp_ino
->i_ino
= FILE_MFT
;
2808 insert_inode_hash(tmp_ino
);
2809 if (ntfs_read_inode_mount(tmp_ino
) < 0) {
2811 ntfs_error(sb
, "Failed to load essential metadata.");
2812 goto iput_tmp_ino_err_out_now
;
2816 * The current mount is a compression user if the cluster size is
2817 * less than or equal 4kiB.
2819 if (vol
->cluster_size
<= 4096 && !ntfs_nr_compression_users
++) {
2820 result
= allocate_compression_buffers();
2822 ntfs_error(NULL
, "Failed to allocate buffers "
2823 "for compression engine.");
2824 ntfs_nr_compression_users
--;
2826 goto iput_tmp_ino_err_out_now
;
2830 * Generate the global default upcase table if necessary. Also
2831 * temporarily increment the number of upcase users to avoid race
2832 * conditions with concurrent (u)mounts.
2834 if (!default_upcase
)
2835 default_upcase
= generate_default_upcase();
2836 ntfs_nr_upcase_users
++;
2839 * From now on, ignore @silent parameter. If we fail below this line,
2840 * it will be due to a corrupt fs or a system error, so we report it.
2843 * Open the system files with normal access functions and complete
2844 * setting up the ntfs super block.
2846 if (!load_system_files(vol
)) {
2847 ntfs_error(sb
, "Failed to load system files.");
2848 goto unl_upcase_iput_tmp_ino_err_out_now
;
2850 if ((sb
->s_root
= d_alloc_root(vol
->root_ino
))) {
2851 /* We increment i_count simulating an ntfs_iget(). */
2852 atomic_inc(&vol
->root_ino
->i_count
);
2853 ntfs_debug("Exiting, status successful.");
2854 /* Release the default upcase if it has no users. */
2856 if (!--ntfs_nr_upcase_users
&& default_upcase
) {
2857 ntfs_free(default_upcase
);
2858 default_upcase
= NULL
;
2861 sb
->s_export_op
= &ntfs_export_ops
;
2865 ntfs_error(sb
, "Failed to allocate root directory.");
2866 /* Clean up after the successful load_system_files() call from above. */
2867 // TODO: Use ntfs_put_super() instead of repeating all this code...
2868 // FIXME: Should mark the volume clean as the error is most likely
2871 vol
->vol_ino
= NULL
;
2872 /* NTFS 3.0+ specific clean up. */
2873 if (vol
->major_ver
>= 3) {
2875 if (vol
->usnjrnl_j_ino
) {
2876 iput(vol
->usnjrnl_j_ino
);
2877 vol
->usnjrnl_j_ino
= NULL
;
2879 if (vol
->usnjrnl_max_ino
) {
2880 iput(vol
->usnjrnl_max_ino
);
2881 vol
->usnjrnl_max_ino
= NULL
;
2883 if (vol
->usnjrnl_ino
) {
2884 iput(vol
->usnjrnl_ino
);
2885 vol
->usnjrnl_ino
= NULL
;
2887 if (vol
->quota_q_ino
) {
2888 iput(vol
->quota_q_ino
);
2889 vol
->quota_q_ino
= NULL
;
2891 if (vol
->quota_ino
) {
2892 iput(vol
->quota_ino
);
2893 vol
->quota_ino
= NULL
;
2895 #endif /* NTFS_RW */
2896 if (vol
->extend_ino
) {
2897 iput(vol
->extend_ino
);
2898 vol
->extend_ino
= NULL
;
2900 if (vol
->secure_ino
) {
2901 iput(vol
->secure_ino
);
2902 vol
->secure_ino
= NULL
;
2905 iput(vol
->root_ino
);
2906 vol
->root_ino
= NULL
;
2907 iput(vol
->lcnbmp_ino
);
2908 vol
->lcnbmp_ino
= NULL
;
2909 iput(vol
->mftbmp_ino
);
2910 vol
->mftbmp_ino
= NULL
;
2912 if (vol
->logfile_ino
) {
2913 iput(vol
->logfile_ino
);
2914 vol
->logfile_ino
= NULL
;
2916 if (vol
->mftmirr_ino
) {
2917 iput(vol
->mftmirr_ino
);
2918 vol
->mftmirr_ino
= NULL
;
2920 #endif /* NTFS_RW */
2921 /* Throw away the table of attribute definitions. */
2922 vol
->attrdef_size
= 0;
2924 ntfs_free(vol
->attrdef
);
2925 vol
->attrdef
= NULL
;
2927 vol
->upcase_len
= 0;
2929 if (vol
->upcase
== default_upcase
) {
2930 ntfs_nr_upcase_users
--;
2935 ntfs_free(vol
->upcase
);
2939 unload_nls(vol
->nls_map
);
2940 vol
->nls_map
= NULL
;
2942 /* Error exit code path. */
2943 unl_upcase_iput_tmp_ino_err_out_now
:
2945 * Decrease the number of upcase users and destroy the global default
2946 * upcase table if necessary.
2949 if (!--ntfs_nr_upcase_users
&& default_upcase
) {
2950 ntfs_free(default_upcase
);
2951 default_upcase
= NULL
;
2953 if (vol
->cluster_size
<= 4096 && !--ntfs_nr_compression_users
)
2954 free_compression_buffers();
2956 iput_tmp_ino_err_out_now
:
2958 if (vol
->mft_ino
&& vol
->mft_ino
!= tmp_ino
)
2960 vol
->mft_ino
= NULL
;
2962 * This is needed to get ntfs_clear_extent_inode() called for each
2963 * inode we have ever called ntfs_iget()/iput() on, otherwise we A)
2964 * leak resources and B) a subsequent mount fails automatically due to
2965 * ntfs_iget() never calling down into our ntfs_read_locked_inode()
2966 * method again... FIXME: Do we need to do this twice now because of
2967 * attribute inodes? I think not, so leave as is for now... (AIA)
2969 if (invalidate_inodes(sb
)) {
2970 ntfs_error(sb
, "Busy inodes left. This is most likely a NTFS "
2972 /* Copied from fs/super.c. I just love this message. (-; */
2973 printk("NTFS: Busy inodes after umount. Self-destruct in 5 "
2974 "seconds. Have a nice day...\n");
2976 /* Errors at this stage are irrelevant. */
2979 sb
->s_fs_info
= NULL
;
2981 ntfs_debug("Failed, returning -EINVAL.");
2986 * This is a slab cache to optimize allocations and deallocations of Unicode
2987 * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN
2988 * (255) Unicode characters + a terminating NULL Unicode character.
2990 kmem_cache_t
*ntfs_name_cache
;
2992 /* Slab caches for efficient allocation/deallocation of inodes. */
2993 kmem_cache_t
*ntfs_inode_cache
;
2994 kmem_cache_t
*ntfs_big_inode_cache
;
2996 /* Init once constructor for the inode slab cache. */
2997 static void ntfs_big_inode_init_once(void *foo
, kmem_cache_t
*cachep
,
2998 unsigned long flags
)
3000 ntfs_inode
*ni
= (ntfs_inode
*)foo
;
3002 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
3003 SLAB_CTOR_CONSTRUCTOR
)
3004 inode_init_once(VFS_I(ni
));
3008 * Slab caches to optimize allocations and deallocations of attribute search
3009 * contexts and index contexts, respectively.
3011 kmem_cache_t
*ntfs_attr_ctx_cache
;
3012 kmem_cache_t
*ntfs_index_ctx_cache
;
3014 /* Driver wide semaphore. */
3015 DECLARE_MUTEX(ntfs_lock
);
3017 static struct super_block
*ntfs_get_sb(struct file_system_type
*fs_type
,
3018 int flags
, const char *dev_name
, void *data
)
3020 return get_sb_bdev(fs_type
, flags
, dev_name
, data
, ntfs_fill_super
);
3023 static struct file_system_type ntfs_fs_type
= {
3024 .owner
= THIS_MODULE
,
3026 .get_sb
= ntfs_get_sb
,
3027 .kill_sb
= kill_block_super
,
3028 .fs_flags
= FS_REQUIRES_DEV
,
3031 /* Stable names for the slab caches. */
3032 static const char ntfs_index_ctx_cache_name
[] = "ntfs_index_ctx_cache";
3033 static const char ntfs_attr_ctx_cache_name
[] = "ntfs_attr_ctx_cache";
3034 static const char ntfs_name_cache_name
[] = "ntfs_name_cache";
3035 static const char ntfs_inode_cache_name
[] = "ntfs_inode_cache";
3036 static const char ntfs_big_inode_cache_name
[] = "ntfs_big_inode_cache";
3038 static int __init
init_ntfs_fs(void)
3042 /* This may be ugly but it results in pretty output so who cares. (-8 */
3043 printk(KERN_INFO
"NTFS driver " NTFS_VERSION
" [Flags: R/"
3057 ntfs_debug("Debug messages are enabled.");
3059 ntfs_index_ctx_cache
= kmem_cache_create(ntfs_index_ctx_cache_name
,
3060 sizeof(ntfs_index_context
), 0 /* offset */,
3061 SLAB_HWCACHE_ALIGN
, NULL
/* ctor */, NULL
/* dtor */);
3062 if (!ntfs_index_ctx_cache
) {
3063 printk(KERN_CRIT
"NTFS: Failed to create %s!\n",
3064 ntfs_index_ctx_cache_name
);
3067 ntfs_attr_ctx_cache
= kmem_cache_create(ntfs_attr_ctx_cache_name
,
3068 sizeof(ntfs_attr_search_ctx
), 0 /* offset */,
3069 SLAB_HWCACHE_ALIGN
, NULL
/* ctor */, NULL
/* dtor */);
3070 if (!ntfs_attr_ctx_cache
) {
3071 printk(KERN_CRIT
"NTFS: Failed to create %s!\n",
3072 ntfs_attr_ctx_cache_name
);
3076 ntfs_name_cache
= kmem_cache_create(ntfs_name_cache_name
,
3077 (NTFS_MAX_NAME_LEN
+1) * sizeof(ntfschar
), 0,
3078 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
3079 if (!ntfs_name_cache
) {
3080 printk(KERN_CRIT
"NTFS: Failed to create %s!\n",
3081 ntfs_name_cache_name
);
3085 ntfs_inode_cache
= kmem_cache_create(ntfs_inode_cache_name
,
3086 sizeof(ntfs_inode
), 0,
3087 SLAB_RECLAIM_ACCOUNT
, NULL
, NULL
);
3088 if (!ntfs_inode_cache
) {
3089 printk(KERN_CRIT
"NTFS: Failed to create %s!\n",
3090 ntfs_inode_cache_name
);
3094 ntfs_big_inode_cache
= kmem_cache_create(ntfs_big_inode_cache_name
,
3095 sizeof(big_ntfs_inode
), 0,
3096 SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
,
3097 ntfs_big_inode_init_once
, NULL
);
3098 if (!ntfs_big_inode_cache
) {
3099 printk(KERN_CRIT
"NTFS: Failed to create %s!\n",
3100 ntfs_big_inode_cache_name
);
3101 goto big_inode_err_out
;
3104 /* Register the ntfs sysctls. */
3105 err
= ntfs_sysctl(1);
3107 printk(KERN_CRIT
"NTFS: Failed to register NTFS sysctls!\n");
3108 goto sysctl_err_out
;
3111 err
= register_filesystem(&ntfs_fs_type
);
3113 ntfs_debug("NTFS driver registered successfully.");
3114 return 0; /* Success! */
3116 printk(KERN_CRIT
"NTFS: Failed to register NTFS filesystem driver!\n");
3119 kmem_cache_destroy(ntfs_big_inode_cache
);
3121 kmem_cache_destroy(ntfs_inode_cache
);
3123 kmem_cache_destroy(ntfs_name_cache
);
3125 kmem_cache_destroy(ntfs_attr_ctx_cache
);
3127 kmem_cache_destroy(ntfs_index_ctx_cache
);
3130 printk(KERN_CRIT
"NTFS: Aborting NTFS filesystem driver "
3131 "registration...\n");
3137 static void __exit
exit_ntfs_fs(void)
3141 ntfs_debug("Unregistering NTFS driver.");
3143 unregister_filesystem(&ntfs_fs_type
);
3145 if (kmem_cache_destroy(ntfs_big_inode_cache
) && (err
= 1))
3146 printk(KERN_CRIT
"NTFS: Failed to destory %s.\n",
3147 ntfs_big_inode_cache_name
);
3148 if (kmem_cache_destroy(ntfs_inode_cache
) && (err
= 1))
3149 printk(KERN_CRIT
"NTFS: Failed to destory %s.\n",
3150 ntfs_inode_cache_name
);
3151 if (kmem_cache_destroy(ntfs_name_cache
) && (err
= 1))
3152 printk(KERN_CRIT
"NTFS: Failed to destory %s.\n",
3153 ntfs_name_cache_name
);
3154 if (kmem_cache_destroy(ntfs_attr_ctx_cache
) && (err
= 1))
3155 printk(KERN_CRIT
"NTFS: Failed to destory %s.\n",
3156 ntfs_attr_ctx_cache_name
);
3157 if (kmem_cache_destroy(ntfs_index_ctx_cache
) && (err
= 1))
3158 printk(KERN_CRIT
"NTFS: Failed to destory %s.\n",
3159 ntfs_index_ctx_cache_name
);
3161 printk(KERN_CRIT
"NTFS: This causes memory to leak! There is "
3162 "probably a BUG in the driver! Please report "
3163 "you saw this message to "
3164 "linux-ntfs-dev@lists.sourceforge.net\n");
3165 /* Unregister the ntfs sysctls. */
3169 MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>");
3170 MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2005 Anton Altaparmakov");
3171 MODULE_VERSION(NTFS_VERSION
);
3172 MODULE_LICENSE("GPL");
3174 module_param(debug_msgs
, bool, 0);
3175 MODULE_PARM_DESC(debug_msgs
, "Enable debug messages.");
3178 module_init(init_ntfs_fs
)
3179 module_exit(exit_ntfs_fs
)