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>
45 /* Number of mounted filesystems which have compression enabled. */
46 static unsigned long ntfs_nr_compression_users
;
48 /* A global default upcase table and a corresponding reference count. */
49 static ntfschar
*default_upcase
= NULL
;
50 static unsigned long ntfs_nr_upcase_users
= 0;
52 /* Error constants/strings used in inode.c::ntfs_show_options(). */
54 /* One of these must be present, default is ON_ERRORS_CONTINUE. */
55 ON_ERRORS_PANIC
= 0x01,
56 ON_ERRORS_REMOUNT_RO
= 0x02,
57 ON_ERRORS_CONTINUE
= 0x04,
58 /* Optional, can be combined with any of the above. */
59 ON_ERRORS_RECOVER
= 0x10,
62 const option_t on_errors_arr
[] = {
63 { ON_ERRORS_PANIC
, "panic" },
64 { ON_ERRORS_REMOUNT_RO
, "remount-ro", },
65 { ON_ERRORS_CONTINUE
, "continue", },
66 { ON_ERRORS_RECOVER
, "recover" },
73 * Copied from old ntfs driver (which copied from vfat driver).
75 static int simple_getbool(char *s
, BOOL
*setval
)
78 if (!strcmp(s
, "1") || !strcmp(s
, "yes") || !strcmp(s
, "true"))
80 else if (!strcmp(s
, "0") || !strcmp(s
, "no") ||
91 * parse_options - parse the (re)mount options
93 * @opt: string containing the (re)mount options
95 * Parse the recognized options in @opt for the ntfs volume described by @vol.
97 static BOOL
parse_options(ntfs_volume
*vol
, char *opt
)
100 static char *utf8
= "utf8";
101 int errors
= 0, sloppy
= 0;
102 uid_t uid
= (uid_t
)-1;
103 gid_t gid
= (gid_t
)-1;
104 mode_t fmask
= (mode_t
)-1, dmask
= (mode_t
)-1;
105 int mft_zone_multiplier
= -1, on_errors
= -1;
106 int show_sys_files
= -1, case_sensitive
= -1, disable_sparse
= -1;
107 struct nls_table
*nls_map
= NULL
, *old_nls
;
109 /* I am lazy... (-8 */
110 #define NTFS_GETOPT_WITH_DEFAULT(option, variable, default_value) \
111 if (!strcmp(p, option)) { \
113 variable = default_value; \
115 variable = simple_strtoul(ov = v, &v, 0); \
120 #define NTFS_GETOPT(option, variable) \
121 if (!strcmp(p, option)) { \
124 variable = simple_strtoul(ov = v, &v, 0); \
128 #define NTFS_GETOPT_BOOL(option, variable) \
129 if (!strcmp(p, option)) { \
131 if (!simple_getbool(v, &val)) \
135 #define NTFS_GETOPT_OPTIONS_ARRAY(option, variable, opt_array) \
136 if (!strcmp(p, option)) { \
141 if (variable == -1) \
143 for (_i = 0; opt_array[_i].str && *opt_array[_i].str; _i++) \
144 if (!strcmp(opt_array[_i].str, v)) { \
145 variable |= opt_array[_i].val; \
148 if (!opt_array[_i].str || !*opt_array[_i].str) \
152 goto no_mount_options
;
153 ntfs_debug("Entering with mount options string: %s", opt
);
154 while ((p
= strsep(&opt
, ","))) {
155 if ((v
= strchr(p
, '=')))
157 NTFS_GETOPT("uid", uid
)
158 else NTFS_GETOPT("gid", gid
)
159 else NTFS_GETOPT("umask", fmask
= dmask
)
160 else NTFS_GETOPT("fmask", fmask
)
161 else NTFS_GETOPT("dmask", dmask
)
162 else NTFS_GETOPT("mft_zone_multiplier", mft_zone_multiplier
)
163 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy
, TRUE
)
164 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files
)
165 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive
)
166 else NTFS_GETOPT_BOOL("disable_sparse", disable_sparse
)
167 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors
,
169 else if (!strcmp(p
, "posix") || !strcmp(p
, "show_inodes"))
170 ntfs_warning(vol
->sb
, "Ignoring obsolete option %s.",
172 else if (!strcmp(p
, "nls") || !strcmp(p
, "iocharset")) {
173 if (!strcmp(p
, "iocharset"))
174 ntfs_warning(vol
->sb
, "Option iocharset is "
175 "deprecated. Please use "
176 "option nls=<charsetname> in "
182 nls_map
= load_nls(v
);
185 ntfs_error(vol
->sb
, "NLS character set "
189 ntfs_error(vol
->sb
, "NLS character set %s not "
190 "found. Using previous one %s.",
191 v
, old_nls
->charset
);
193 } else /* nls_map */ {
197 } else if (!strcmp(p
, "utf8")) {
199 ntfs_warning(vol
->sb
, "Option utf8 is no longer "
200 "supported, using option nls=utf8. Please "
201 "use option nls=utf8 in the future and "
202 "make sure utf8 is compiled either as a "
203 "module or into the kernel.");
206 else if (!simple_getbool(v
, &val
))
213 ntfs_error(vol
->sb
, "Unrecognized mount option %s.", p
);
214 if (errors
< INT_MAX
)
217 #undef NTFS_GETOPT_OPTIONS_ARRAY
218 #undef NTFS_GETOPT_BOOL
220 #undef NTFS_GETOPT_WITH_DEFAULT
223 if (errors
&& !sloppy
)
226 ntfs_warning(vol
->sb
, "Sloppy option given. Ignoring "
227 "unrecognized mount option(s) and continuing.");
228 /* Keep this first! */
229 if (on_errors
!= -1) {
231 ntfs_error(vol
->sb
, "Invalid errors option argument "
232 "or bug in options parser.");
237 if (vol
->nls_map
&& vol
->nls_map
!= nls_map
) {
238 ntfs_error(vol
->sb
, "Cannot change NLS character set "
241 } /* else (!vol->nls_map) */
242 ntfs_debug("Using NLS character set %s.", nls_map
->charset
);
243 vol
->nls_map
= nls_map
;
244 } else /* (!nls_map) */ {
246 vol
->nls_map
= load_nls_default();
248 ntfs_error(vol
->sb
, "Failed to load default "
249 "NLS character set.");
252 ntfs_debug("Using default NLS character set (%s).",
253 vol
->nls_map
->charset
);
256 if (mft_zone_multiplier
!= -1) {
257 if (vol
->mft_zone_multiplier
&& vol
->mft_zone_multiplier
!=
258 mft_zone_multiplier
) {
259 ntfs_error(vol
->sb
, "Cannot change mft_zone_multiplier "
263 if (mft_zone_multiplier
< 1 || mft_zone_multiplier
> 4) {
264 ntfs_error(vol
->sb
, "Invalid mft_zone_multiplier. "
265 "Using default value, i.e. 1.");
266 mft_zone_multiplier
= 1;
268 vol
->mft_zone_multiplier
= mft_zone_multiplier
;
270 if (!vol
->mft_zone_multiplier
)
271 vol
->mft_zone_multiplier
= 1;
273 vol
->on_errors
= on_errors
;
274 if (!vol
->on_errors
|| vol
->on_errors
== ON_ERRORS_RECOVER
)
275 vol
->on_errors
|= ON_ERRORS_CONTINUE
;
276 if (uid
!= (uid_t
)-1)
278 if (gid
!= (gid_t
)-1)
280 if (fmask
!= (mode_t
)-1)
282 if (dmask
!= (mode_t
)-1)
284 if (show_sys_files
!= -1) {
286 NVolSetShowSystemFiles(vol
);
288 NVolClearShowSystemFiles(vol
);
290 if (case_sensitive
!= -1) {
292 NVolSetCaseSensitive(vol
);
294 NVolClearCaseSensitive(vol
);
296 if (disable_sparse
!= -1) {
298 NVolClearSparseEnabled(vol
);
300 if (!NVolSparseEnabled(vol
) &&
301 vol
->major_ver
&& vol
->major_ver
< 3)
302 ntfs_warning(vol
->sb
, "Not enabling sparse "
303 "support due to NTFS volume "
304 "version %i.%i (need at least "
305 "version 3.0).", vol
->major_ver
,
308 NVolSetSparseEnabled(vol
);
313 ntfs_error(vol
->sb
, "The %s option requires an argument.", p
);
316 ntfs_error(vol
->sb
, "The %s option requires a boolean argument.", p
);
319 ntfs_error(vol
->sb
, "Invalid %s option argument: %s", p
, ov
);
326 * ntfs_write_volume_flags - write new flags to the volume information flags
327 * @vol: ntfs volume on which to modify the flags
328 * @flags: new flags value for the volume information flags
330 * Internal function. You probably want to use ntfs_{set,clear}_volume_flags()
331 * instead (see below).
333 * Replace the volume information flags on the volume @vol with the value
334 * supplied in @flags. Note, this overwrites the volume information flags, so
335 * make sure to combine the flags you want to modify with the old flags and use
336 * the result when calling ntfs_write_volume_flags().
338 * Return 0 on success and -errno on error.
340 static int ntfs_write_volume_flags(ntfs_volume
*vol
, const VOLUME_FLAGS flags
)
342 ntfs_inode
*ni
= NTFS_I(vol
->vol_ino
);
344 VOLUME_INFORMATION
*vi
;
345 ntfs_attr_search_ctx
*ctx
;
348 ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
349 le16_to_cpu(vol
->vol_flags
), le16_to_cpu(flags
));
350 if (vol
->vol_flags
== flags
)
353 m
= map_mft_record(ni
);
358 ctx
= ntfs_attr_get_search_ctx(ni
, m
);
361 goto put_unm_err_out
;
363 err
= ntfs_attr_lookup(AT_VOLUME_INFORMATION
, NULL
, 0, 0, 0, NULL
, 0,
366 goto put_unm_err_out
;
367 vi
= (VOLUME_INFORMATION
*)((u8
*)ctx
->attr
+
368 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
));
369 vol
->vol_flags
= vi
->flags
= flags
;
370 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
371 mark_mft_record_dirty(ctx
->ntfs_ino
);
372 ntfs_attr_put_search_ctx(ctx
);
373 unmap_mft_record(ni
);
379 ntfs_attr_put_search_ctx(ctx
);
380 unmap_mft_record(ni
);
382 ntfs_error(vol
->sb
, "Failed with error code %i.", -err
);
387 * ntfs_set_volume_flags - set bits in the volume information flags
388 * @vol: ntfs volume on which to modify the flags
389 * @flags: flags to set on the volume
391 * Set the bits in @flags in the volume information flags on the volume @vol.
393 * Return 0 on success and -errno on error.
395 static inline int ntfs_set_volume_flags(ntfs_volume
*vol
, VOLUME_FLAGS flags
)
397 flags
&= VOLUME_FLAGS_MASK
;
398 return ntfs_write_volume_flags(vol
, vol
->vol_flags
| flags
);
402 * ntfs_clear_volume_flags - clear bits in the volume information flags
403 * @vol: ntfs volume on which to modify the flags
404 * @flags: flags to clear on the volume
406 * Clear the bits in @flags in the volume information flags on the volume @vol.
408 * Return 0 on success and -errno on error.
410 static inline int ntfs_clear_volume_flags(ntfs_volume
*vol
, VOLUME_FLAGS flags
)
412 flags
&= VOLUME_FLAGS_MASK
;
413 flags
= vol
->vol_flags
& cpu_to_le16(~le16_to_cpu(flags
));
414 return ntfs_write_volume_flags(vol
, flags
);
420 * ntfs_remount - change the mount options of a mounted ntfs filesystem
421 * @sb: superblock of mounted ntfs filesystem
422 * @flags: remount flags
423 * @opt: remount options string
425 * Change the mount options of an already mounted ntfs filesystem.
427 * NOTE: The VFS sets the @sb->s_flags remount flags to @flags after
428 * ntfs_remount() returns successfully (i.e. returns 0). Otherwise,
429 * @sb->s_flags are not changed.
431 static int ntfs_remount(struct super_block
*sb
, int *flags
, char *opt
)
433 ntfs_volume
*vol
= NTFS_SB(sb
);
435 ntfs_debug("Entering with remount options string: %s", opt
);
437 /* For read-only compiled driver, enforce all read-only flags. */
438 *flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
441 * For the read-write compiled driver, if we are remounting read-write,
442 * make sure there are no volume errors and that no unsupported volume
443 * flags are set. Also, empty the logfile journal as it would become
444 * stale as soon as something is written to the volume and mark the
445 * volume dirty so that chkdsk is run if the volume is not umounted
446 * cleanly. Finally, mark the quotas out of date so Windows rescans
447 * the volume on boot and updates them.
449 * When remounting read-only, mark the volume clean if no volume errors
452 if ((sb
->s_flags
& MS_RDONLY
) && !(*flags
& MS_RDONLY
)) {
453 static const char *es
= ". Cannot remount read-write.";
455 /* Remounting read-write. */
456 if (NVolErrors(vol
)) {
457 ntfs_error(sb
, "Volume has errors and is read-only%s",
461 if (vol
->vol_flags
& VOLUME_IS_DIRTY
) {
462 ntfs_error(sb
, "Volume is dirty and read-only%s", es
);
465 if (vol
->vol_flags
& VOLUME_MUST_MOUNT_RO_MASK
) {
466 ntfs_error(sb
, "Volume has unsupported flags set and "
467 "is read-only%s", es
);
470 if (ntfs_set_volume_flags(vol
, VOLUME_IS_DIRTY
)) {
471 ntfs_error(sb
, "Failed to set dirty bit in volume "
472 "information flags%s", es
);
476 // TODO: Enable this code once we start modifying anything that
477 // is different between NTFS 1.2 and 3.x...
478 /* Set NT4 compatibility flag on newer NTFS version volumes. */
479 if ((vol
->major_ver
> 1)) {
480 if (ntfs_set_volume_flags(vol
, VOLUME_MOUNTED_ON_NT4
)) {
481 ntfs_error(sb
, "Failed to set NT4 "
482 "compatibility flag%s", es
);
488 if (!ntfs_empty_logfile(vol
->logfile_ino
)) {
489 ntfs_error(sb
, "Failed to empty journal $LogFile%s",
494 if (!ntfs_mark_quotas_out_of_date(vol
)) {
495 ntfs_error(sb
, "Failed to mark quotas out of date%s",
500 } else if (!(sb
->s_flags
& MS_RDONLY
) && (*flags
& MS_RDONLY
)) {
501 /* Remounting read-only. */
502 if (!NVolErrors(vol
)) {
503 if (ntfs_clear_volume_flags(vol
, VOLUME_IS_DIRTY
))
504 ntfs_warning(sb
, "Failed to clear dirty bit "
505 "in volume information "
506 "flags. Run chkdsk.");
511 // TODO: Deal with *flags.
513 if (!parse_options(vol
, opt
))
520 * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector
521 * @sb: Super block of the device to which @b belongs.
522 * @b: Boot sector of device @sb to check.
523 * @silent: If TRUE, all output will be silenced.
525 * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot
526 * sector. Returns TRUE if it is valid and FALSE if not.
528 * @sb is only needed for warning/error output, i.e. it can be NULL when silent
531 static BOOL
is_boot_sector_ntfs(const struct super_block
*sb
,
532 const NTFS_BOOT_SECTOR
*b
, const BOOL silent
)
535 * Check that checksum == sum of u32 values from b to the checksum
536 * field. If checksum is zero, no checking is done. We will work when
537 * the checksum test fails, since some utilities update the boot sector
538 * ignoring the checksum which leaves the checksum out-of-date. We
539 * report a warning if this is the case.
541 if ((void*)b
< (void*)&b
->checksum
&& b
->checksum
&& !silent
) {
545 for (i
= 0, u
= (le32
*)b
; u
< (le32
*)(&b
->checksum
); ++u
)
546 i
+= le32_to_cpup(u
);
547 if (le32_to_cpu(b
->checksum
) != i
)
548 ntfs_warning(sb
, "Invalid boot sector checksum.");
550 /* Check OEMidentifier is "NTFS " */
551 if (b
->oem_id
!= magicNTFS
)
553 /* Check bytes per sector value is between 256 and 4096. */
554 if (le16_to_cpu(b
->bpb
.bytes_per_sector
) < 0x100 ||
555 le16_to_cpu(b
->bpb
.bytes_per_sector
) > 0x1000)
557 /* Check sectors per cluster value is valid. */
558 switch (b
->bpb
.sectors_per_cluster
) {
559 case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128:
564 /* Check the cluster size is not above 65536 bytes. */
565 if ((u32
)le16_to_cpu(b
->bpb
.bytes_per_sector
) *
566 b
->bpb
.sectors_per_cluster
> 0x10000)
568 /* Check reserved/unused fields are really zero. */
569 if (le16_to_cpu(b
->bpb
.reserved_sectors
) ||
570 le16_to_cpu(b
->bpb
.root_entries
) ||
571 le16_to_cpu(b
->bpb
.sectors
) ||
572 le16_to_cpu(b
->bpb
.sectors_per_fat
) ||
573 le32_to_cpu(b
->bpb
.large_sectors
) || b
->bpb
.fats
)
575 /* Check clusters per file mft record value is valid. */
576 if ((u8
)b
->clusters_per_mft_record
< 0xe1 ||
577 (u8
)b
->clusters_per_mft_record
> 0xf7)
578 switch (b
->clusters_per_mft_record
) {
579 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
584 /* Check clusters per index block value is valid. */
585 if ((u8
)b
->clusters_per_index_record
< 0xe1 ||
586 (u8
)b
->clusters_per_index_record
> 0xf7)
587 switch (b
->clusters_per_index_record
) {
588 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
594 * Check for valid end of sector marker. We will work without it, but
595 * many BIOSes will refuse to boot from a bootsector if the magic is
596 * incorrect, so we emit a warning.
598 if (!silent
&& b
->end_of_sector_marker
!= const_cpu_to_le16(0xaa55))
599 ntfs_warning(sb
, "Invalid end of sector marker.");
606 * read_ntfs_boot_sector - read the NTFS boot sector of a device
607 * @sb: super block of device to read the boot sector from
608 * @silent: if true, suppress all output
610 * Reads the boot sector from the device and validates it. If that fails, tries
611 * to read the backup boot sector, first from the end of the device a-la NT4 and
612 * later and then from the middle of the device a-la NT3.51 and before.
614 * If a valid boot sector is found but it is not the primary boot sector, we
615 * repair the primary boot sector silently (unless the device is read-only or
616 * the primary boot sector is not accessible).
618 * NOTE: To call this function, @sb must have the fields s_dev, the ntfs super
619 * block (u.ntfs_sb), nr_blocks and the device flags (s_flags) initialized
620 * to their respective values.
622 * Return the unlocked buffer head containing the boot sector or NULL on error.
624 static struct buffer_head
*read_ntfs_boot_sector(struct super_block
*sb
,
627 const char *read_err_str
= "Unable to read %s boot sector.";
628 struct buffer_head
*bh_primary
, *bh_backup
;
629 long nr_blocks
= NTFS_SB(sb
)->nr_blocks
;
631 /* Try to read primary boot sector. */
632 if ((bh_primary
= sb_bread(sb
, 0))) {
633 if (is_boot_sector_ntfs(sb
, (NTFS_BOOT_SECTOR
*)
634 bh_primary
->b_data
, silent
))
637 ntfs_error(sb
, "Primary boot sector is invalid.");
639 ntfs_error(sb
, read_err_str
, "primary");
640 if (!(NTFS_SB(sb
)->on_errors
& ON_ERRORS_RECOVER
)) {
644 ntfs_error(sb
, "Mount option errors=recover not used. "
645 "Aborting without trying to recover.");
648 /* Try to read NT4+ backup boot sector. */
649 if ((bh_backup
= sb_bread(sb
, nr_blocks
- 1))) {
650 if (is_boot_sector_ntfs(sb
, (NTFS_BOOT_SECTOR
*)
651 bh_backup
->b_data
, silent
))
652 goto hotfix_primary_boot_sector
;
655 ntfs_error(sb
, read_err_str
, "backup");
656 /* Try to read NT3.51- backup boot sector. */
657 if ((bh_backup
= sb_bread(sb
, nr_blocks
>> 1))) {
658 if (is_boot_sector_ntfs(sb
, (NTFS_BOOT_SECTOR
*)
659 bh_backup
->b_data
, silent
))
660 goto hotfix_primary_boot_sector
;
662 ntfs_error(sb
, "Could not find a valid backup boot "
666 ntfs_error(sb
, read_err_str
, "backup");
667 /* We failed. Cleanup and return. */
671 hotfix_primary_boot_sector
:
674 * If we managed to read sector zero and the volume is not
675 * read-only, copy the found, valid backup boot sector to the
676 * primary boot sector.
678 if (!(sb
->s_flags
& MS_RDONLY
)) {
679 ntfs_warning(sb
, "Hot-fix: Recovering invalid primary "
680 "boot sector from backup copy.");
681 memcpy(bh_primary
->b_data
, bh_backup
->b_data
,
683 mark_buffer_dirty(bh_primary
);
684 sync_dirty_buffer(bh_primary
);
685 if (buffer_uptodate(bh_primary
)) {
689 ntfs_error(sb
, "Hot-fix: Device write error while "
690 "recovering primary boot sector.");
692 ntfs_warning(sb
, "Hot-fix: Recovery of primary boot "
693 "sector failed: Read-only mount.");
697 ntfs_warning(sb
, "Using backup boot sector.");
702 * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol
703 * @vol: volume structure to initialise with data from boot sector
704 * @b: boot sector to parse
706 * Parse the ntfs boot sector @b and store all imporant information therein in
707 * the ntfs super block @vol. Return TRUE on success and FALSE on error.
709 static BOOL
parse_ntfs_boot_sector(ntfs_volume
*vol
, const NTFS_BOOT_SECTOR
*b
)
711 unsigned int sectors_per_cluster_bits
, nr_hidden_sects
;
712 int clusters_per_mft_record
, clusters_per_index_record
;
715 vol
->sector_size
= le16_to_cpu(b
->bpb
.bytes_per_sector
);
716 vol
->sector_size_bits
= ffs(vol
->sector_size
) - 1;
717 ntfs_debug("vol->sector_size = %i (0x%x)", vol
->sector_size
,
719 ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol
->sector_size_bits
,
720 vol
->sector_size_bits
);
721 if (vol
->sector_size
!= vol
->sb
->s_blocksize
)
722 ntfs_warning(vol
->sb
, "The boot sector indicates a sector size "
723 "different from the device sector size.");
724 ntfs_debug("sectors_per_cluster = 0x%x", b
->bpb
.sectors_per_cluster
);
725 sectors_per_cluster_bits
= ffs(b
->bpb
.sectors_per_cluster
) - 1;
726 ntfs_debug("sectors_per_cluster_bits = 0x%x",
727 sectors_per_cluster_bits
);
728 nr_hidden_sects
= le32_to_cpu(b
->bpb
.hidden_sectors
);
729 ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects
);
730 vol
->cluster_size
= vol
->sector_size
<< sectors_per_cluster_bits
;
731 vol
->cluster_size_mask
= vol
->cluster_size
- 1;
732 vol
->cluster_size_bits
= ffs(vol
->cluster_size
) - 1;
733 ntfs_debug("vol->cluster_size = %i (0x%x)", vol
->cluster_size
,
735 ntfs_debug("vol->cluster_size_mask = 0x%x", vol
->cluster_size_mask
);
736 ntfs_debug("vol->cluster_size_bits = %i (0x%x)",
737 vol
->cluster_size_bits
, vol
->cluster_size_bits
);
738 if (vol
->sector_size
> vol
->cluster_size
) {
739 ntfs_error(vol
->sb
, "Sector sizes above the cluster size are "
740 "not supported. Sorry.");
743 if (vol
->sb
->s_blocksize
> vol
->cluster_size
) {
744 ntfs_error(vol
->sb
, "Cluster sizes smaller than the device "
745 "sector size are not supported. Sorry.");
748 clusters_per_mft_record
= b
->clusters_per_mft_record
;
749 ntfs_debug("clusters_per_mft_record = %i (0x%x)",
750 clusters_per_mft_record
, clusters_per_mft_record
);
751 if (clusters_per_mft_record
> 0)
752 vol
->mft_record_size
= vol
->cluster_size
<<
753 (ffs(clusters_per_mft_record
) - 1);
756 * When mft_record_size < cluster_size, clusters_per_mft_record
757 * = -log2(mft_record_size) bytes. mft_record_size normaly is
758 * 1024 bytes, which is encoded as 0xF6 (-10 in decimal).
760 vol
->mft_record_size
= 1 << -clusters_per_mft_record
;
761 vol
->mft_record_size_mask
= vol
->mft_record_size
- 1;
762 vol
->mft_record_size_bits
= ffs(vol
->mft_record_size
) - 1;
763 ntfs_debug("vol->mft_record_size = %i (0x%x)", vol
->mft_record_size
,
764 vol
->mft_record_size
);
765 ntfs_debug("vol->mft_record_size_mask = 0x%x",
766 vol
->mft_record_size_mask
);
767 ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
768 vol
->mft_record_size_bits
, vol
->mft_record_size_bits
);
770 * We cannot support mft record sizes above the PAGE_CACHE_SIZE since
771 * we store $MFT/$DATA, the table of mft records in the page cache.
773 if (vol
->mft_record_size
> PAGE_CACHE_SIZE
) {
774 ntfs_error(vol
->sb
, "Mft record size %i (0x%x) exceeds the "
775 "page cache size on your system %lu (0x%lx). "
776 "This is not supported. Sorry.",
777 vol
->mft_record_size
, vol
->mft_record_size
,
778 PAGE_CACHE_SIZE
, PAGE_CACHE_SIZE
);
781 clusters_per_index_record
= b
->clusters_per_index_record
;
782 ntfs_debug("clusters_per_index_record = %i (0x%x)",
783 clusters_per_index_record
, clusters_per_index_record
);
784 if (clusters_per_index_record
> 0)
785 vol
->index_record_size
= vol
->cluster_size
<<
786 (ffs(clusters_per_index_record
) - 1);
789 * When index_record_size < cluster_size,
790 * clusters_per_index_record = -log2(index_record_size) bytes.
791 * index_record_size normaly equals 4096 bytes, which is
792 * encoded as 0xF4 (-12 in decimal).
794 vol
->index_record_size
= 1 << -clusters_per_index_record
;
795 vol
->index_record_size_mask
= vol
->index_record_size
- 1;
796 vol
->index_record_size_bits
= ffs(vol
->index_record_size
) - 1;
797 ntfs_debug("vol->index_record_size = %i (0x%x)",
798 vol
->index_record_size
, vol
->index_record_size
);
799 ntfs_debug("vol->index_record_size_mask = 0x%x",
800 vol
->index_record_size_mask
);
801 ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
802 vol
->index_record_size_bits
,
803 vol
->index_record_size_bits
);
805 * Get the size of the volume in clusters and check for 64-bit-ness.
806 * Windows currently only uses 32 bits to save the clusters so we do
807 * the same as it is much faster on 32-bit CPUs.
809 ll
= sle64_to_cpu(b
->number_of_sectors
) >> sectors_per_cluster_bits
;
810 if ((u64
)ll
>= 1ULL << 32) {
811 ntfs_error(vol
->sb
, "Cannot handle 64-bit clusters. Sorry.");
814 vol
->nr_clusters
= ll
;
815 ntfs_debug("vol->nr_clusters = 0x%llx", (long long)vol
->nr_clusters
);
817 * On an architecture where unsigned long is 32-bits, we restrict the
818 * volume size to 2TiB (2^41). On a 64-bit architecture, the compiler
819 * will hopefully optimize the whole check away.
821 if (sizeof(unsigned long) < 8) {
822 if ((ll
<< vol
->cluster_size_bits
) >= (1ULL << 41)) {
823 ntfs_error(vol
->sb
, "Volume size (%lluTiB) is too "
824 "large for this architecture. "
825 "Maximum supported is 2TiB. Sorry.",
826 (unsigned long long)ll
>> (40 -
827 vol
->cluster_size_bits
));
831 ll
= sle64_to_cpu(b
->mft_lcn
);
832 if (ll
>= vol
->nr_clusters
) {
833 ntfs_error(vol
->sb
, "MFT LCN is beyond end of volume. Weird.");
837 ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol
->mft_lcn
);
838 ll
= sle64_to_cpu(b
->mftmirr_lcn
);
839 if (ll
>= vol
->nr_clusters
) {
840 ntfs_error(vol
->sb
, "MFTMirr LCN is beyond end of volume. "
844 vol
->mftmirr_lcn
= ll
;
845 ntfs_debug("vol->mftmirr_lcn = 0x%llx", (long long)vol
->mftmirr_lcn
);
848 * Work out the size of the mft mirror in number of mft records. If the
849 * cluster size is less than or equal to the size taken by four mft
850 * records, the mft mirror stores the first four mft records. If the
851 * cluster size is bigger than the size taken by four mft records, the
852 * mft mirror contains as many mft records as will fit into one
855 if (vol
->cluster_size
<= (4 << vol
->mft_record_size_bits
))
856 vol
->mftmirr_size
= 4;
858 vol
->mftmirr_size
= vol
->cluster_size
>>
859 vol
->mft_record_size_bits
;
860 ntfs_debug("vol->mftmirr_size = %i", vol
->mftmirr_size
);
862 vol
->serial_no
= le64_to_cpu(b
->volume_serial_number
);
863 ntfs_debug("vol->serial_no = 0x%llx",
864 (unsigned long long)vol
->serial_no
);
869 * ntfs_setup_allocators - initialize the cluster and mft allocators
870 * @vol: volume structure for which to setup the allocators
872 * Setup the cluster (lcn) and mft allocators to the starting values.
874 static void ntfs_setup_allocators(ntfs_volume
*vol
)
877 LCN mft_zone_size
, mft_lcn
;
880 ntfs_debug("vol->mft_zone_multiplier = 0x%x",
881 vol
->mft_zone_multiplier
);
883 /* Determine the size of the MFT zone. */
884 mft_zone_size
= vol
->nr_clusters
;
885 switch (vol
->mft_zone_multiplier
) { /* % of volume size in clusters */
887 mft_zone_size
>>= 1; /* 50% */
890 mft_zone_size
= (mft_zone_size
+
891 (mft_zone_size
>> 1)) >> 2; /* 37.5% */
894 mft_zone_size
>>= 2; /* 25% */
898 mft_zone_size
>>= 3; /* 12.5% */
901 /* Setup the mft zone. */
902 vol
->mft_zone_start
= vol
->mft_zone_pos
= vol
->mft_lcn
;
903 ntfs_debug("vol->mft_zone_pos = 0x%llx",
904 (unsigned long long)vol
->mft_zone_pos
);
906 * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
907 * source) and if the actual mft_lcn is in the expected place or even
908 * further to the front of the volume, extend the mft_zone to cover the
909 * beginning of the volume as well. This is in order to protect the
910 * area reserved for the mft bitmap as well within the mft_zone itself.
911 * On non-standard volumes we do not protect it as the overhead would
912 * be higher than the speed increase we would get by doing it.
914 mft_lcn
= (8192 + 2 * vol
->cluster_size
- 1) / vol
->cluster_size
;
915 if (mft_lcn
* vol
->cluster_size
< 16 * 1024)
916 mft_lcn
= (16 * 1024 + vol
->cluster_size
- 1) /
918 if (vol
->mft_zone_start
<= mft_lcn
)
919 vol
->mft_zone_start
= 0;
920 ntfs_debug("vol->mft_zone_start = 0x%llx",
921 (unsigned long long)vol
->mft_zone_start
);
923 * Need to cap the mft zone on non-standard volumes so that it does
924 * not point outside the boundaries of the volume. We do this by
925 * halving the zone size until we are inside the volume.
927 vol
->mft_zone_end
= vol
->mft_lcn
+ mft_zone_size
;
928 while (vol
->mft_zone_end
>= vol
->nr_clusters
) {
930 vol
->mft_zone_end
= vol
->mft_lcn
+ mft_zone_size
;
932 ntfs_debug("vol->mft_zone_end = 0x%llx",
933 (unsigned long long)vol
->mft_zone_end
);
935 * Set the current position within each data zone to the start of the
938 vol
->data1_zone_pos
= vol
->mft_zone_end
;
939 ntfs_debug("vol->data1_zone_pos = 0x%llx",
940 (unsigned long long)vol
->data1_zone_pos
);
941 vol
->data2_zone_pos
= 0;
942 ntfs_debug("vol->data2_zone_pos = 0x%llx",
943 (unsigned long long)vol
->data2_zone_pos
);
945 /* Set the mft data allocation position to mft record 24. */
946 vol
->mft_data_pos
= 24;
947 ntfs_debug("vol->mft_data_pos = 0x%llx",
948 (unsigned long long)vol
->mft_data_pos
);
955 * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume
956 * @vol: ntfs super block describing device whose mft mirror to load
958 * Return TRUE on success or FALSE on error.
960 static BOOL
load_and_init_mft_mirror(ntfs_volume
*vol
)
962 struct inode
*tmp_ino
;
965 ntfs_debug("Entering.");
966 /* Get mft mirror inode. */
967 tmp_ino
= ntfs_iget(vol
->sb
, FILE_MFTMirr
);
968 if (IS_ERR(tmp_ino
) || is_bad_inode(tmp_ino
)) {
969 if (!IS_ERR(tmp_ino
))
971 /* Caller will display error message. */
975 * Re-initialize some specifics about $MFTMirr's inode as
976 * ntfs_read_inode() will have set up the default ones.
978 /* Set uid and gid to root. */
979 tmp_ino
->i_uid
= tmp_ino
->i_gid
= 0;
980 /* Regular file. No access for anyone. */
981 tmp_ino
->i_mode
= S_IFREG
;
982 /* No VFS initiated operations allowed for $MFTMirr. */
983 tmp_ino
->i_op
= &ntfs_empty_inode_ops
;
984 tmp_ino
->i_fop
= &ntfs_empty_file_ops
;
985 /* Put in our special address space operations. */
986 tmp_ino
->i_mapping
->a_ops
= &ntfs_mst_aops
;
987 tmp_ni
= NTFS_I(tmp_ino
);
988 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
989 NInoSetMstProtected(tmp_ni
);
990 NInoSetSparseDisabled(tmp_ni
);
992 * Set up our little cheat allowing us to reuse the async read io
993 * completion handler for directories.
995 tmp_ni
->itype
.index
.block_size
= vol
->mft_record_size
;
996 tmp_ni
->itype
.index
.block_size_bits
= vol
->mft_record_size_bits
;
997 vol
->mftmirr_ino
= tmp_ino
;
1003 * check_mft_mirror - compare contents of the mft mirror with the mft
1004 * @vol: ntfs super block describing device whose mft mirror to check
1006 * Return TRUE on success or FALSE on error.
1008 * Note, this function also results in the mft mirror runlist being completely
1009 * mapped into memory. The mft mirror write code requires this and will BUG()
1010 * should it find an unmapped runlist element.
1012 static BOOL
check_mft_mirror(ntfs_volume
*vol
)
1014 struct super_block
*sb
= vol
->sb
;
1015 ntfs_inode
*mirr_ni
;
1016 struct page
*mft_page
, *mirr_page
;
1018 runlist_element
*rl
, rl2
[2];
1020 int mrecs_per_page
, i
;
1022 ntfs_debug("Entering.");
1023 /* Compare contents of $MFT and $MFTMirr. */
1024 mrecs_per_page
= PAGE_CACHE_SIZE
/ vol
->mft_record_size
;
1025 BUG_ON(!mrecs_per_page
);
1026 BUG_ON(!vol
->mftmirr_size
);
1027 mft_page
= mirr_page
= NULL
;
1028 kmft
= kmirr
= NULL
;
1033 /* Switch pages if necessary. */
1034 if (!(i
% mrecs_per_page
)) {
1036 ntfs_unmap_page(mft_page
);
1037 ntfs_unmap_page(mirr_page
);
1039 /* Get the $MFT page. */
1040 mft_page
= ntfs_map_page(vol
->mft_ino
->i_mapping
,
1042 if (IS_ERR(mft_page
)) {
1043 ntfs_error(sb
, "Failed to read $MFT.");
1046 kmft
= page_address(mft_page
);
1047 /* Get the $MFTMirr page. */
1048 mirr_page
= ntfs_map_page(vol
->mftmirr_ino
->i_mapping
,
1050 if (IS_ERR(mirr_page
)) {
1051 ntfs_error(sb
, "Failed to read $MFTMirr.");
1054 kmirr
= page_address(mirr_page
);
1057 /* Make sure the record is ok. */
1058 if (ntfs_is_baad_recordp((le32
*)kmft
)) {
1059 ntfs_error(sb
, "Incomplete multi sector transfer "
1060 "detected in mft record %i.", i
);
1062 ntfs_unmap_page(mirr_page
);
1064 ntfs_unmap_page(mft_page
);
1067 if (ntfs_is_baad_recordp((le32
*)kmirr
)) {
1068 ntfs_error(sb
, "Incomplete multi sector transfer "
1069 "detected in mft mirror record %i.", i
);
1072 /* Get the amount of data in the current record. */
1073 bytes
= le32_to_cpu(((MFT_RECORD
*)kmft
)->bytes_in_use
);
1074 if (!bytes
|| bytes
> vol
->mft_record_size
) {
1075 bytes
= le32_to_cpu(((MFT_RECORD
*)kmirr
)->bytes_in_use
);
1076 if (!bytes
|| bytes
> vol
->mft_record_size
)
1077 bytes
= vol
->mft_record_size
;
1079 /* Compare the two records. */
1080 if (memcmp(kmft
, kmirr
, bytes
)) {
1081 ntfs_error(sb
, "$MFT and $MFTMirr (record %i) do not "
1082 "match. Run ntfsfix or chkdsk.", i
);
1085 kmft
+= vol
->mft_record_size
;
1086 kmirr
+= vol
->mft_record_size
;
1087 } while (++i
< vol
->mftmirr_size
);
1088 /* Release the last pages. */
1089 ntfs_unmap_page(mft_page
);
1090 ntfs_unmap_page(mirr_page
);
1092 /* Construct the mft mirror runlist by hand. */
1094 rl2
[0].lcn
= vol
->mftmirr_lcn
;
1095 rl2
[0].length
= (vol
->mftmirr_size
* vol
->mft_record_size
+
1096 vol
->cluster_size
- 1) / vol
->cluster_size
;
1097 rl2
[1].vcn
= rl2
[0].length
;
1098 rl2
[1].lcn
= LCN_ENOENT
;
1101 * Because we have just read all of the mft mirror, we know we have
1102 * mapped the full runlist for it.
1104 mirr_ni
= NTFS_I(vol
->mftmirr_ino
);
1105 down_read(&mirr_ni
->runlist
.lock
);
1106 rl
= mirr_ni
->runlist
.rl
;
1107 /* Compare the two runlists. They must be identical. */
1110 if (rl2
[i
].vcn
!= rl
[i
].vcn
|| rl2
[i
].lcn
!= rl
[i
].lcn
||
1111 rl2
[i
].length
!= rl
[i
].length
) {
1112 ntfs_error(sb
, "$MFTMirr location mismatch. "
1114 up_read(&mirr_ni
->runlist
.lock
);
1117 } while (rl2
[i
++].length
);
1118 up_read(&mirr_ni
->runlist
.lock
);
1119 ntfs_debug("Done.");
1124 * load_and_check_logfile - load and check the logfile inode for a volume
1125 * @vol: ntfs super block describing device whose logfile to load
1127 * Return TRUE on success or FALSE on error.
1129 static BOOL
load_and_check_logfile(ntfs_volume
*vol
)
1131 struct inode
*tmp_ino
;
1133 ntfs_debug("Entering.");
1134 tmp_ino
= ntfs_iget(vol
->sb
, FILE_LogFile
);
1135 if (IS_ERR(tmp_ino
) || is_bad_inode(tmp_ino
)) {
1136 if (!IS_ERR(tmp_ino
))
1138 /* Caller will display error message. */
1141 if (!ntfs_check_logfile(tmp_ino
)) {
1143 /* ntfs_check_logfile() will have displayed error output. */
1146 NInoSetSparseDisabled(NTFS_I(tmp_ino
));
1147 vol
->logfile_ino
= tmp_ino
;
1148 ntfs_debug("Done.");
1153 * load_and_init_quota - load and setup the quota file for a volume if present
1154 * @vol: ntfs super block describing device whose quota file to load
1156 * Return TRUE on success or FALSE on error. If $Quota is not present, we
1157 * leave vol->quota_ino as NULL and return success.
1159 static BOOL
load_and_init_quota(ntfs_volume
*vol
)
1162 struct inode
*tmp_ino
;
1163 ntfs_name
*name
= NULL
;
1164 static const ntfschar Quota
[7] = { const_cpu_to_le16('$'),
1165 const_cpu_to_le16('Q'), const_cpu_to_le16('u'),
1166 const_cpu_to_le16('o'), const_cpu_to_le16('t'),
1167 const_cpu_to_le16('a'), 0 };
1168 static ntfschar Q
[3] = { const_cpu_to_le16('$'),
1169 const_cpu_to_le16('Q'), 0 };
1171 ntfs_debug("Entering.");
1173 * Find the inode number for the quota file by looking up the filename
1174 * $Quota in the extended system files directory $Extend.
1176 down(&vol
->extend_ino
->i_sem
);
1177 mref
= ntfs_lookup_inode_by_name(NTFS_I(vol
->extend_ino
), Quota
, 6,
1179 up(&vol
->extend_ino
->i_sem
);
1180 if (IS_ERR_MREF(mref
)) {
1182 * If the file does not exist, quotas are disabled and have
1183 * never been enabled on this volume, just return success.
1185 if (MREF_ERR(mref
) == -ENOENT
) {
1186 ntfs_debug("$Quota not present. Volume does not have "
1189 * No need to try to set quotas out of date if they are
1192 NVolSetQuotaOutOfDate(vol
);
1195 /* A real error occured. */
1196 ntfs_error(vol
->sb
, "Failed to find inode number for $Quota.");
1199 /* We do not care for the type of match that was found. */
1201 /* Get the inode. */
1202 tmp_ino
= ntfs_iget(vol
->sb
, MREF(mref
));
1203 if (IS_ERR(tmp_ino
) || is_bad_inode(tmp_ino
)) {
1204 if (!IS_ERR(tmp_ino
))
1206 ntfs_error(vol
->sb
, "Failed to load $Quota.");
1209 vol
->quota_ino
= tmp_ino
;
1210 /* Get the $Q index allocation attribute. */
1211 tmp_ino
= ntfs_index_iget(vol
->quota_ino
, Q
, 2);
1212 if (IS_ERR(tmp_ino
)) {
1213 ntfs_error(vol
->sb
, "Failed to load $Quota/$Q index.");
1216 vol
->quota_q_ino
= tmp_ino
;
1217 ntfs_debug("Done.");
1222 * load_and_init_attrdef - load the attribute definitions table for a volume
1223 * @vol: ntfs super block describing device whose attrdef to load
1225 * Return TRUE on success or FALSE on error.
1227 static BOOL
load_and_init_attrdef(ntfs_volume
*vol
)
1230 struct super_block
*sb
= vol
->sb
;
1233 pgoff_t index
, max_index
;
1236 ntfs_debug("Entering.");
1237 /* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */
1238 ino
= ntfs_iget(sb
, FILE_AttrDef
);
1239 if (IS_ERR(ino
) || is_bad_inode(ino
)) {
1244 NInoSetSparseDisabled(NTFS_I(ino
));
1245 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
1246 i_size
= i_size_read(ino
);
1247 if (i_size
<= 0 || i_size
> 0x7fffffff)
1249 vol
->attrdef
= (ATTR_DEF
*)ntfs_malloc_nofs(i_size
);
1253 max_index
= i_size
>> PAGE_CACHE_SHIFT
;
1254 size
= PAGE_CACHE_SIZE
;
1255 while (index
< max_index
) {
1256 /* Read the attrdef table and copy it into the linear buffer. */
1257 read_partial_attrdef_page
:
1258 page
= ntfs_map_page(ino
->i_mapping
, index
);
1260 goto free_iput_failed
;
1261 memcpy((u8
*)vol
->attrdef
+ (index
++ << PAGE_CACHE_SHIFT
),
1262 page_address(page
), size
);
1263 ntfs_unmap_page(page
);
1265 if (size
== PAGE_CACHE_SIZE
) {
1266 size
= i_size
& ~PAGE_CACHE_MASK
;
1268 goto read_partial_attrdef_page
;
1270 vol
->attrdef_size
= i_size
;
1271 ntfs_debug("Read %llu bytes from $AttrDef.", i_size
);
1275 ntfs_free(vol
->attrdef
);
1276 vol
->attrdef
= NULL
;
1280 ntfs_error(sb
, "Failed to initialize attribute definition table.");
1284 #endif /* NTFS_RW */
1287 * load_and_init_upcase - load the upcase table for an ntfs volume
1288 * @vol: ntfs super block describing device whose upcase to load
1290 * Return TRUE on success or FALSE on error.
1292 static BOOL
load_and_init_upcase(ntfs_volume
*vol
)
1295 struct super_block
*sb
= vol
->sb
;
1298 pgoff_t index
, max_index
;
1302 ntfs_debug("Entering.");
1303 /* Read upcase table and setup vol->upcase and vol->upcase_len. */
1304 ino
= ntfs_iget(sb
, FILE_UpCase
);
1305 if (IS_ERR(ino
) || is_bad_inode(ino
)) {
1311 * The upcase size must not be above 64k Unicode characters, must not
1312 * be zero and must be a multiple of sizeof(ntfschar).
1314 i_size
= i_size_read(ino
);
1315 if (!i_size
|| i_size
& (sizeof(ntfschar
) - 1) ||
1316 i_size
> 64ULL * 1024 * sizeof(ntfschar
))
1317 goto iput_upcase_failed
;
1318 vol
->upcase
= (ntfschar
*)ntfs_malloc_nofs(i_size
);
1320 goto iput_upcase_failed
;
1322 max_index
= i_size
>> PAGE_CACHE_SHIFT
;
1323 size
= PAGE_CACHE_SIZE
;
1324 while (index
< max_index
) {
1325 /* Read the upcase table and copy it into the linear buffer. */
1326 read_partial_upcase_page
:
1327 page
= ntfs_map_page(ino
->i_mapping
, index
);
1329 goto iput_upcase_failed
;
1330 memcpy((char*)vol
->upcase
+ (index
++ << PAGE_CACHE_SHIFT
),
1331 page_address(page
), size
);
1332 ntfs_unmap_page(page
);
1334 if (size
== PAGE_CACHE_SIZE
) {
1335 size
= i_size
& ~PAGE_CACHE_MASK
;
1337 goto read_partial_upcase_page
;
1339 vol
->upcase_len
= i_size
>> UCHAR_T_SIZE_BITS
;
1340 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
1341 i_size
, 64 * 1024 * sizeof(ntfschar
));
1344 if (!default_upcase
) {
1345 ntfs_debug("Using volume specified $UpCase since default is "
1350 max
= default_upcase_len
;
1351 if (max
> vol
->upcase_len
)
1352 max
= vol
->upcase_len
;
1353 for (i
= 0; i
< max
; i
++)
1354 if (vol
->upcase
[i
] != default_upcase
[i
])
1357 ntfs_free(vol
->upcase
);
1358 vol
->upcase
= default_upcase
;
1359 vol
->upcase_len
= max
;
1360 ntfs_nr_upcase_users
++;
1362 ntfs_debug("Volume specified $UpCase matches default. Using "
1367 ntfs_debug("Using volume specified $UpCase since it does not match "
1372 ntfs_free(vol
->upcase
);
1376 if (default_upcase
) {
1377 vol
->upcase
= default_upcase
;
1378 vol
->upcase_len
= default_upcase_len
;
1379 ntfs_nr_upcase_users
++;
1381 ntfs_error(sb
, "Failed to load $UpCase from the volume. Using "
1386 ntfs_error(sb
, "Failed to initialize upcase table.");
1391 * load_system_files - open the system files using normal functions
1392 * @vol: ntfs super block describing device whose system files to load
1394 * Open the system files with normal access functions and complete setting up
1395 * the ntfs super block @vol.
1397 * Return TRUE on success or FALSE on error.
1399 static BOOL
load_system_files(ntfs_volume
*vol
)
1401 struct super_block
*sb
= vol
->sb
;
1403 VOLUME_INFORMATION
*vi
;
1404 ntfs_attr_search_ctx
*ctx
;
1406 ntfs_debug("Entering.");
1408 /* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */
1409 if (!load_and_init_mft_mirror(vol
) || !check_mft_mirror(vol
)) {
1410 static const char *es1
= "Failed to load $MFTMirr";
1411 static const char *es2
= "$MFTMirr does not match $MFT";
1412 static const char *es3
= ". Run ntfsfix and/or chkdsk.";
1414 /* If a read-write mount, convert it to a read-only mount. */
1415 if (!(sb
->s_flags
& MS_RDONLY
)) {
1416 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1417 ON_ERRORS_CONTINUE
))) {
1418 ntfs_error(sb
, "%s and neither on_errors="
1419 "continue nor on_errors="
1420 "remount-ro was specified%s",
1421 !vol
->mftmirr_ino
? es1
: es2
,
1423 goto iput_mirr_err_out
;
1425 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1426 ntfs_error(sb
, "%s. Mounting read-only%s",
1427 !vol
->mftmirr_ino
? es1
: es2
, es3
);
1429 ntfs_warning(sb
, "%s. Will not be able to remount "
1431 !vol
->mftmirr_ino
? es1
: es2
, es3
);
1432 /* This will prevent a read-write remount. */
1435 #endif /* NTFS_RW */
1436 /* Get mft bitmap attribute inode. */
1437 vol
->mftbmp_ino
= ntfs_attr_iget(vol
->mft_ino
, AT_BITMAP
, NULL
, 0);
1438 if (IS_ERR(vol
->mftbmp_ino
)) {
1439 ntfs_error(sb
, "Failed to load $MFT/$BITMAP attribute.");
1440 goto iput_mirr_err_out
;
1442 /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */
1443 if (!load_and_init_upcase(vol
))
1444 goto iput_mftbmp_err_out
;
1447 * Read attribute definitions table and setup @vol->attrdef and
1448 * @vol->attrdef_size.
1450 if (!load_and_init_attrdef(vol
))
1451 goto iput_upcase_err_out
;
1452 #endif /* NTFS_RW */
1454 * Get the cluster allocation bitmap inode and verify the size, no
1455 * need for any locking at this stage as we are already running
1456 * exclusively as we are mount in progress task.
1458 vol
->lcnbmp_ino
= ntfs_iget(sb
, FILE_Bitmap
);
1459 if (IS_ERR(vol
->lcnbmp_ino
) || is_bad_inode(vol
->lcnbmp_ino
)) {
1460 if (!IS_ERR(vol
->lcnbmp_ino
))
1461 iput(vol
->lcnbmp_ino
);
1464 NInoSetSparseDisabled(NTFS_I(vol
->lcnbmp_ino
));
1465 if ((vol
->nr_clusters
+ 7) >> 3 > i_size_read(vol
->lcnbmp_ino
)) {
1466 iput(vol
->lcnbmp_ino
);
1468 ntfs_error(sb
, "Failed to load $Bitmap.");
1469 goto iput_attrdef_err_out
;
1472 * Get the volume inode and setup our cache of the volume flags and
1475 vol
->vol_ino
= ntfs_iget(sb
, FILE_Volume
);
1476 if (IS_ERR(vol
->vol_ino
) || is_bad_inode(vol
->vol_ino
)) {
1477 if (!IS_ERR(vol
->vol_ino
))
1480 ntfs_error(sb
, "Failed to load $Volume.");
1481 goto iput_lcnbmp_err_out
;
1483 m
= map_mft_record(NTFS_I(vol
->vol_ino
));
1489 if (!(ctx
= ntfs_attr_get_search_ctx(NTFS_I(vol
->vol_ino
), m
))) {
1490 ntfs_error(sb
, "Failed to get attribute search context.");
1491 goto get_ctx_vol_failed
;
1493 if (ntfs_attr_lookup(AT_VOLUME_INFORMATION
, NULL
, 0, 0, 0, NULL
, 0,
1494 ctx
) || ctx
->attr
->non_resident
|| ctx
->attr
->flags
) {
1496 ntfs_attr_put_search_ctx(ctx
);
1498 unmap_mft_record(NTFS_I(vol
->vol_ino
));
1499 goto iput_volume_failed
;
1501 vi
= (VOLUME_INFORMATION
*)((char*)ctx
->attr
+
1502 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
));
1503 /* Some bounds checks. */
1504 if ((u8
*)vi
< (u8
*)ctx
->attr
|| (u8
*)vi
+
1505 le32_to_cpu(ctx
->attr
->data
.resident
.value_length
) >
1506 (u8
*)ctx
->attr
+ le32_to_cpu(ctx
->attr
->length
))
1508 /* Copy the volume flags and version to the ntfs_volume structure. */
1509 vol
->vol_flags
= vi
->flags
;
1510 vol
->major_ver
= vi
->major_ver
;
1511 vol
->minor_ver
= vi
->minor_ver
;
1512 ntfs_attr_put_search_ctx(ctx
);
1513 unmap_mft_record(NTFS_I(vol
->vol_ino
));
1514 printk(KERN_INFO
"NTFS volume version %i.%i.\n", vol
->major_ver
,
1516 if (vol
->major_ver
< 3 && NVolSparseEnabled(vol
)) {
1517 ntfs_warning(vol
->sb
, "Disabling sparse support due to NTFS "
1518 "volume version %i.%i (need at least version "
1519 "3.0).", vol
->major_ver
, vol
->minor_ver
);
1520 NVolClearSparseEnabled(vol
);
1523 /* Make sure that no unsupported volume flags are set. */
1524 if (vol
->vol_flags
& VOLUME_MUST_MOUNT_RO_MASK
) {
1525 static const char *es1a
= "Volume is dirty";
1526 static const char *es1b
= "Volume has unsupported flags set";
1527 static const char *es2
= ". Run chkdsk and mount in Windows.";
1530 es1
= vol
->vol_flags
& VOLUME_IS_DIRTY
? es1a
: es1b
;
1531 /* If a read-write mount, convert it to a read-only mount. */
1532 if (!(sb
->s_flags
& MS_RDONLY
)) {
1533 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1534 ON_ERRORS_CONTINUE
))) {
1535 ntfs_error(sb
, "%s and neither on_errors="
1536 "continue nor on_errors="
1537 "remount-ro was specified%s",
1539 goto iput_vol_err_out
;
1541 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1542 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
1544 ntfs_warning(sb
, "%s. Will not be able to remount "
1545 "read-write%s", es1
, es2
);
1547 * Do not set NVolErrors() because ntfs_remount() re-checks the
1548 * flags which we need to do in case any flags have changed.
1552 * Get the inode for the logfile, check it and determine if the volume
1553 * was shutdown cleanly.
1555 if (!load_and_check_logfile(vol
) ||
1556 !ntfs_is_logfile_clean(vol
->logfile_ino
)) {
1557 static const char *es1a
= "Failed to load $LogFile";
1558 static const char *es1b
= "$LogFile is not clean";
1559 static const char *es2
= ". Mount in Windows.";
1562 es1
= !vol
->logfile_ino
? es1a
: es1b
;
1563 /* If a read-write mount, convert it to a read-only mount. */
1564 if (!(sb
->s_flags
& MS_RDONLY
)) {
1565 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1566 ON_ERRORS_CONTINUE
))) {
1567 ntfs_error(sb
, "%s and neither on_errors="
1568 "continue nor on_errors="
1569 "remount-ro was specified%s",
1571 goto iput_logfile_err_out
;
1573 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1574 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
1576 ntfs_warning(sb
, "%s. Will not be able to remount "
1577 "read-write%s", es1
, es2
);
1578 /* This will prevent a read-write remount. */
1581 /* If (still) a read-write mount, mark the volume dirty. */
1582 if (!(sb
->s_flags
& MS_RDONLY
) &&
1583 ntfs_set_volume_flags(vol
, VOLUME_IS_DIRTY
)) {
1584 static const char *es1
= "Failed to set dirty bit in volume "
1585 "information flags";
1586 static const char *es2
= ". Run chkdsk.";
1588 /* Convert to a read-only mount. */
1589 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1590 ON_ERRORS_CONTINUE
))) {
1591 ntfs_error(sb
, "%s and neither on_errors=continue nor "
1592 "on_errors=remount-ro was specified%s",
1594 goto iput_logfile_err_out
;
1596 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
1597 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1599 * Do not set NVolErrors() because ntfs_remount() might manage
1600 * to set the dirty flag in which case all would be well.
1604 // TODO: Enable this code once we start modifying anything that is
1605 // different between NTFS 1.2 and 3.x...
1607 * If (still) a read-write mount, set the NT4 compatibility flag on
1608 * newer NTFS version volumes.
1610 if (!(sb
->s_flags
& MS_RDONLY
) && (vol
->major_ver
> 1) &&
1611 ntfs_set_volume_flags(vol
, VOLUME_MOUNTED_ON_NT4
)) {
1612 static const char *es1
= "Failed to set NT4 compatibility flag";
1613 static const char *es2
= ". Run chkdsk.";
1615 /* Convert to a read-only mount. */
1616 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1617 ON_ERRORS_CONTINUE
))) {
1618 ntfs_error(sb
, "%s and neither on_errors=continue nor "
1619 "on_errors=remount-ro was specified%s",
1621 goto iput_logfile_err_out
;
1623 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
1624 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1628 /* If (still) a read-write mount, empty the logfile. */
1629 if (!(sb
->s_flags
& MS_RDONLY
) &&
1630 !ntfs_empty_logfile(vol
->logfile_ino
)) {
1631 static const char *es1
= "Failed to empty $LogFile";
1632 static const char *es2
= ". Mount in Windows.";
1634 /* Convert to a read-only mount. */
1635 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1636 ON_ERRORS_CONTINUE
))) {
1637 ntfs_error(sb
, "%s and neither on_errors=continue nor "
1638 "on_errors=remount-ro was specified%s",
1640 goto iput_logfile_err_out
;
1642 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
1643 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1646 #endif /* NTFS_RW */
1647 /* Get the root directory inode. */
1648 vol
->root_ino
= ntfs_iget(sb
, FILE_root
);
1649 if (IS_ERR(vol
->root_ino
) || is_bad_inode(vol
->root_ino
)) {
1650 if (!IS_ERR(vol
->root_ino
))
1651 iput(vol
->root_ino
);
1652 ntfs_error(sb
, "Failed to load root directory.");
1653 goto iput_logfile_err_out
;
1655 /* If on NTFS versions before 3.0, we are done. */
1656 if (vol
->major_ver
< 3)
1658 /* NTFS 3.0+ specific initialization. */
1659 /* Get the security descriptors inode. */
1660 vol
->secure_ino
= ntfs_iget(sb
, FILE_Secure
);
1661 if (IS_ERR(vol
->secure_ino
) || is_bad_inode(vol
->secure_ino
)) {
1662 if (!IS_ERR(vol
->secure_ino
))
1663 iput(vol
->secure_ino
);
1664 ntfs_error(sb
, "Failed to load $Secure.");
1665 goto iput_root_err_out
;
1667 // FIXME: Initialize security.
1668 /* Get the extended system files' directory inode. */
1669 vol
->extend_ino
= ntfs_iget(sb
, FILE_Extend
);
1670 if (IS_ERR(vol
->extend_ino
) || is_bad_inode(vol
->extend_ino
)) {
1671 if (!IS_ERR(vol
->extend_ino
))
1672 iput(vol
->extend_ino
);
1673 ntfs_error(sb
, "Failed to load $Extend.");
1674 goto iput_sec_err_out
;
1677 /* Find the quota file, load it if present, and set it up. */
1678 if (!load_and_init_quota(vol
)) {
1679 static const char *es1
= "Failed to load $Quota";
1680 static const char *es2
= ". Run chkdsk.";
1682 /* If a read-write mount, convert it to a read-only mount. */
1683 if (!(sb
->s_flags
& MS_RDONLY
)) {
1684 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1685 ON_ERRORS_CONTINUE
))) {
1686 ntfs_error(sb
, "%s and neither on_errors="
1687 "continue nor on_errors="
1688 "remount-ro was specified%s",
1690 goto iput_quota_err_out
;
1692 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1693 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
1695 ntfs_warning(sb
, "%s. Will not be able to remount "
1696 "read-write%s", es1
, es2
);
1697 /* This will prevent a read-write remount. */
1700 /* If (still) a read-write mount, mark the quotas out of date. */
1701 if (!(sb
->s_flags
& MS_RDONLY
) &&
1702 !ntfs_mark_quotas_out_of_date(vol
)) {
1703 static const char *es1
= "Failed to mark quotas out of date";
1704 static const char *es2
= ". Run chkdsk.";
1706 /* Convert to a read-only mount. */
1707 if (!(vol
->on_errors
& (ON_ERRORS_REMOUNT_RO
|
1708 ON_ERRORS_CONTINUE
))) {
1709 ntfs_error(sb
, "%s and neither on_errors=continue nor "
1710 "on_errors=remount-ro was specified%s",
1712 goto iput_quota_err_out
;
1714 ntfs_error(sb
, "%s. Mounting read-only%s", es1
, es2
);
1715 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
1718 // TODO: Delete or checkpoint the $UsnJrnl if it exists.
1719 #endif /* NTFS_RW */
1723 if (vol
->quota_q_ino
)
1724 iput(vol
->quota_q_ino
);
1726 iput(vol
->quota_ino
);
1727 iput(vol
->extend_ino
);
1728 #endif /* NTFS_RW */
1730 iput(vol
->secure_ino
);
1732 iput(vol
->root_ino
);
1733 iput_logfile_err_out
:
1735 if (vol
->logfile_ino
)
1736 iput(vol
->logfile_ino
);
1738 #endif /* NTFS_RW */
1740 iput_lcnbmp_err_out
:
1741 iput(vol
->lcnbmp_ino
);
1742 iput_attrdef_err_out
:
1743 vol
->attrdef_size
= 0;
1745 ntfs_free(vol
->attrdef
);
1746 vol
->attrdef
= NULL
;
1749 iput_upcase_err_out
:
1750 #endif /* NTFS_RW */
1751 vol
->upcase_len
= 0;
1753 if (vol
->upcase
== default_upcase
) {
1754 ntfs_nr_upcase_users
--;
1759 ntfs_free(vol
->upcase
);
1762 iput_mftbmp_err_out
:
1763 iput(vol
->mftbmp_ino
);
1766 if (vol
->mftmirr_ino
)
1767 iput(vol
->mftmirr_ino
);
1768 #endif /* NTFS_RW */
1773 * ntfs_put_super - called by the vfs to unmount a volume
1774 * @sb: vfs superblock of volume to unmount
1776 * ntfs_put_super() is called by the VFS (from fs/super.c::do_umount()) when
1777 * the volume is being unmounted (umount system call has been invoked) and it
1778 * releases all inodes and memory belonging to the NTFS specific part of the
1781 static void ntfs_put_super(struct super_block
*sb
)
1783 ntfs_volume
*vol
= NTFS_SB(sb
);
1785 ntfs_debug("Entering.");
1788 * Commit all inodes while they are still open in case some of them
1789 * cause others to be dirtied.
1791 ntfs_commit_inode(vol
->vol_ino
);
1793 /* NTFS 3.0+ specific. */
1794 if (vol
->major_ver
>= 3) {
1795 if (vol
->quota_q_ino
)
1796 ntfs_commit_inode(vol
->quota_q_ino
);
1798 ntfs_commit_inode(vol
->quota_ino
);
1799 if (vol
->extend_ino
)
1800 ntfs_commit_inode(vol
->extend_ino
);
1801 if (vol
->secure_ino
)
1802 ntfs_commit_inode(vol
->secure_ino
);
1805 ntfs_commit_inode(vol
->root_ino
);
1807 down_write(&vol
->lcnbmp_lock
);
1808 ntfs_commit_inode(vol
->lcnbmp_ino
);
1809 up_write(&vol
->lcnbmp_lock
);
1811 down_write(&vol
->mftbmp_lock
);
1812 ntfs_commit_inode(vol
->mftbmp_ino
);
1813 up_write(&vol
->mftbmp_lock
);
1815 if (vol
->logfile_ino
)
1816 ntfs_commit_inode(vol
->logfile_ino
);
1818 if (vol
->mftmirr_ino
)
1819 ntfs_commit_inode(vol
->mftmirr_ino
);
1820 ntfs_commit_inode(vol
->mft_ino
);
1823 * If a read-write mount and no volume errors have occured, mark the
1824 * volume clean. Also, re-commit all affected inodes.
1826 if (!(sb
->s_flags
& MS_RDONLY
)) {
1827 if (!NVolErrors(vol
)) {
1828 if (ntfs_clear_volume_flags(vol
, VOLUME_IS_DIRTY
))
1829 ntfs_warning(sb
, "Failed to clear dirty bit "
1830 "in volume information "
1831 "flags. Run chkdsk.");
1832 ntfs_commit_inode(vol
->vol_ino
);
1833 ntfs_commit_inode(vol
->root_ino
);
1834 if (vol
->mftmirr_ino
)
1835 ntfs_commit_inode(vol
->mftmirr_ino
);
1836 ntfs_commit_inode(vol
->mft_ino
);
1838 ntfs_warning(sb
, "Volume has errors. Leaving volume "
1839 "marked dirty. Run chkdsk.");
1842 #endif /* NTFS_RW */
1845 vol
->vol_ino
= NULL
;
1847 /* NTFS 3.0+ specific clean up. */
1848 if (vol
->major_ver
>= 3) {
1850 if (vol
->quota_q_ino
) {
1851 iput(vol
->quota_q_ino
);
1852 vol
->quota_q_ino
= NULL
;
1854 if (vol
->quota_ino
) {
1855 iput(vol
->quota_ino
);
1856 vol
->quota_ino
= NULL
;
1858 #endif /* NTFS_RW */
1859 if (vol
->extend_ino
) {
1860 iput(vol
->extend_ino
);
1861 vol
->extend_ino
= NULL
;
1863 if (vol
->secure_ino
) {
1864 iput(vol
->secure_ino
);
1865 vol
->secure_ino
= NULL
;
1869 iput(vol
->root_ino
);
1870 vol
->root_ino
= NULL
;
1872 down_write(&vol
->lcnbmp_lock
);
1873 iput(vol
->lcnbmp_ino
);
1874 vol
->lcnbmp_ino
= NULL
;
1875 up_write(&vol
->lcnbmp_lock
);
1877 down_write(&vol
->mftbmp_lock
);
1878 iput(vol
->mftbmp_ino
);
1879 vol
->mftbmp_ino
= NULL
;
1880 up_write(&vol
->mftbmp_lock
);
1883 if (vol
->logfile_ino
) {
1884 iput(vol
->logfile_ino
);
1885 vol
->logfile_ino
= NULL
;
1887 if (vol
->mftmirr_ino
) {
1888 /* Re-commit the mft mirror and mft just in case. */
1889 ntfs_commit_inode(vol
->mftmirr_ino
);
1890 ntfs_commit_inode(vol
->mft_ino
);
1891 iput(vol
->mftmirr_ino
);
1892 vol
->mftmirr_ino
= NULL
;
1895 * If any dirty inodes are left, throw away all mft data page cache
1896 * pages to allow a clean umount. This should never happen any more
1897 * due to mft.c::ntfs_mft_writepage() cleaning all the dirty pages as
1898 * the underlying mft records are written out and cleaned. If it does,
1899 * happen anyway, we want to know...
1901 ntfs_commit_inode(vol
->mft_ino
);
1902 write_inode_now(vol
->mft_ino
, 1);
1903 if (!list_empty(&sb
->s_dirty
)) {
1904 const char *s1
, *s2
;
1906 down(&vol
->mft_ino
->i_sem
);
1907 truncate_inode_pages(vol
->mft_ino
->i_mapping
, 0);
1908 up(&vol
->mft_ino
->i_sem
);
1909 write_inode_now(vol
->mft_ino
, 1);
1910 if (!list_empty(&sb
->s_dirty
)) {
1911 static const char *_s1
= "inodes";
1912 static const char *_s2
= "";
1916 static const char *_s1
= "mft pages";
1917 static const char *_s2
= "They have been thrown "
1922 ntfs_error(sb
, "Dirty %s found at umount time. %sYou should "
1923 "run chkdsk. Please email "
1924 "linux-ntfs-dev@lists.sourceforge.net and say "
1925 "that you saw this message. Thank you.", s1
,
1928 #endif /* NTFS_RW */
1931 vol
->mft_ino
= NULL
;
1933 /* Throw away the table of attribute definitions. */
1934 vol
->attrdef_size
= 0;
1936 ntfs_free(vol
->attrdef
);
1937 vol
->attrdef
= NULL
;
1939 vol
->upcase_len
= 0;
1941 * Destroy the global default upcase table if necessary. Also decrease
1942 * the number of upcase users if we are a user.
1945 if (vol
->upcase
== default_upcase
) {
1946 ntfs_nr_upcase_users
--;
1949 if (!ntfs_nr_upcase_users
&& default_upcase
) {
1950 ntfs_free(default_upcase
);
1951 default_upcase
= NULL
;
1953 if (vol
->cluster_size
<= 4096 && !--ntfs_nr_compression_users
)
1954 free_compression_buffers();
1957 ntfs_free(vol
->upcase
);
1961 unload_nls(vol
->nls_map
);
1962 vol
->nls_map
= NULL
;
1964 sb
->s_fs_info
= NULL
;
1970 * get_nr_free_clusters - return the number of free clusters on a volume
1971 * @vol: ntfs volume for which to obtain free cluster count
1973 * Calculate the number of free clusters on the mounted NTFS volume @vol. We
1974 * actually calculate the number of clusters in use instead because this
1975 * allows us to not care about partial pages as these will be just zero filled
1976 * and hence not be counted as allocated clusters.
1978 * The only particularity is that clusters beyond the end of the logical ntfs
1979 * volume will be marked as allocated to prevent errors which means we have to
1980 * discount those at the end. This is important as the cluster bitmap always
1981 * has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside
1982 * the logical volume and marked in use when they are not as they do not exist.
1984 * If any pages cannot be read we assume all clusters in the erroring pages are
1985 * in use. This means we return an underestimate on errors which is better than
1988 static s64
get_nr_free_clusters(ntfs_volume
*vol
)
1990 s64 nr_free
= vol
->nr_clusters
;
1992 struct address_space
*mapping
= vol
->lcnbmp_ino
->i_mapping
;
1993 filler_t
*readpage
= (filler_t
*)mapping
->a_ops
->readpage
;
1995 pgoff_t index
, max_index
;
1997 ntfs_debug("Entering.");
1998 /* Serialize accesses to the cluster bitmap. */
1999 down_read(&vol
->lcnbmp_lock
);
2001 * Convert the number of bits into bytes rounded up, then convert into
2002 * multiples of PAGE_CACHE_SIZE, rounding up so that if we have one
2003 * full and one partial page max_index = 2.
2005 max_index
= (((vol
->nr_clusters
+ 7) >> 3) + PAGE_CACHE_SIZE
- 1) >>
2007 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2008 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.",
2009 max_index
, PAGE_CACHE_SIZE
/ 4);
2010 for (index
= 0; index
< max_index
; index
++) {
2013 * Read the page from page cache, getting it from backing store
2014 * if necessary, and increment the use count.
2016 page
= read_cache_page(mapping
, index
, (filler_t
*)readpage
,
2018 /* Ignore pages which errored synchronously. */
2020 ntfs_debug("Sync read_cache_page() error. Skipping "
2021 "page (index 0x%lx).", index
);
2022 nr_free
-= PAGE_CACHE_SIZE
* 8;
2025 wait_on_page_locked(page
);
2026 /* Ignore pages which errored asynchronously. */
2027 if (!PageUptodate(page
)) {
2028 ntfs_debug("Async read_cache_page() error. Skipping "
2029 "page (index 0x%lx).", index
);
2030 page_cache_release(page
);
2031 nr_free
-= PAGE_CACHE_SIZE
* 8;
2034 kaddr
= (u32
*)kmap_atomic(page
, KM_USER0
);
2036 * For each 4 bytes, subtract the number of set bits. If this
2037 * is the last page and it is partial we don't really care as
2038 * it just means we do a little extra work but it won't affect
2039 * the result as all out of range bytes are set to zero by
2042 for (i
= 0; i
< PAGE_CACHE_SIZE
/ 4; i
++)
2043 nr_free
-= (s64
)hweight32(kaddr
[i
]);
2044 kunmap_atomic(kaddr
, KM_USER0
);
2045 page_cache_release(page
);
2047 ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index
- 1);
2049 * Fixup for eventual bits outside logical ntfs volume (see function
2050 * description above).
2052 if (vol
->nr_clusters
& 63)
2053 nr_free
+= 64 - (vol
->nr_clusters
& 63);
2054 up_read(&vol
->lcnbmp_lock
);
2055 /* If errors occured we may well have gone below zero, fix this. */
2058 ntfs_debug("Exiting.");
2063 * __get_nr_free_mft_records - return the number of free inodes on a volume
2064 * @vol: ntfs volume for which to obtain free inode count
2065 * @nr_free: number of mft records in filesystem
2066 * @max_index: maximum number of pages containing set bits
2068 * Calculate the number of free mft records (inodes) on the mounted NTFS
2069 * volume @vol. We actually calculate the number of mft records in use instead
2070 * because this allows us to not care about partial pages as these will be just
2071 * zero filled and hence not be counted as allocated mft record.
2073 * If any pages cannot be read we assume all mft records in the erroring pages
2074 * are in use. This means we return an underestimate on errors which is better
2075 * than an overestimate.
2077 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
2079 static unsigned long __get_nr_free_mft_records(ntfs_volume
*vol
,
2080 s64 nr_free
, const pgoff_t max_index
)
2083 struct address_space
*mapping
= vol
->mftbmp_ino
->i_mapping
;
2084 filler_t
*readpage
= (filler_t
*)mapping
->a_ops
->readpage
;
2088 ntfs_debug("Entering.");
2089 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2090 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
2091 "0x%lx.", max_index
, PAGE_CACHE_SIZE
/ 4);
2092 for (index
= 0; index
< max_index
; index
++) {
2095 * Read the page from page cache, getting it from backing store
2096 * if necessary, and increment the use count.
2098 page
= read_cache_page(mapping
, index
, (filler_t
*)readpage
,
2100 /* Ignore pages which errored synchronously. */
2102 ntfs_debug("Sync read_cache_page() error. Skipping "
2103 "page (index 0x%lx).", index
);
2104 nr_free
-= PAGE_CACHE_SIZE
* 8;
2107 wait_on_page_locked(page
);
2108 /* Ignore pages which errored asynchronously. */
2109 if (!PageUptodate(page
)) {
2110 ntfs_debug("Async read_cache_page() error. Skipping "
2111 "page (index 0x%lx).", index
);
2112 page_cache_release(page
);
2113 nr_free
-= PAGE_CACHE_SIZE
* 8;
2116 kaddr
= (u32
*)kmap_atomic(page
, KM_USER0
);
2118 * For each 4 bytes, subtract the number of set bits. If this
2119 * is the last page and it is partial we don't really care as
2120 * it just means we do a little extra work but it won't affect
2121 * the result as all out of range bytes are set to zero by
2124 for (i
= 0; i
< PAGE_CACHE_SIZE
/ 4; i
++)
2125 nr_free
-= (s64
)hweight32(kaddr
[i
]);
2126 kunmap_atomic(kaddr
, KM_USER0
);
2127 page_cache_release(page
);
2129 ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.",
2131 /* If errors occured we may well have gone below zero, fix this. */
2134 ntfs_debug("Exiting.");
2139 * ntfs_statfs - return information about mounted NTFS volume
2140 * @sb: super block of mounted volume
2141 * @sfs: statfs structure in which to return the information
2143 * Return information about the mounted NTFS volume @sb in the statfs structure
2144 * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is
2145 * called). We interpret the values to be correct of the moment in time at
2146 * which we are called. Most values are variable otherwise and this isn't just
2147 * the free values but the totals as well. For example we can increase the
2148 * total number of file nodes if we run out and we can keep doing this until
2149 * there is no more space on the volume left at all.
2151 * Called from vfs_statfs which is used to handle the statfs, fstatfs, and
2152 * ustat system calls.
2154 * Return 0 on success or -errno on error.
2156 static int ntfs_statfs(struct super_block
*sb
, struct kstatfs
*sfs
)
2159 ntfs_volume
*vol
= NTFS_SB(sb
);
2160 ntfs_inode
*mft_ni
= NTFS_I(vol
->mft_ino
);
2162 unsigned long flags
;
2164 ntfs_debug("Entering.");
2165 /* Type of filesystem. */
2166 sfs
->f_type
= NTFS_SB_MAGIC
;
2167 /* Optimal transfer block size. */
2168 sfs
->f_bsize
= PAGE_CACHE_SIZE
;
2170 * Total data blocks in filesystem in units of f_bsize and since
2171 * inodes are also stored in data blocs ($MFT is a file) this is just
2172 * the total clusters.
2174 sfs
->f_blocks
= vol
->nr_clusters
<< vol
->cluster_size_bits
>>
2176 /* Free data blocks in filesystem in units of f_bsize. */
2177 size
= get_nr_free_clusters(vol
) << vol
->cluster_size_bits
>>
2181 /* Free blocks avail to non-superuser, same as above on NTFS. */
2182 sfs
->f_bavail
= sfs
->f_bfree
= size
;
2183 /* Serialize accesses to the inode bitmap. */
2184 down_read(&vol
->mftbmp_lock
);
2185 read_lock_irqsave(&mft_ni
->size_lock
, flags
);
2186 size
= i_size_read(vol
->mft_ino
) >> vol
->mft_record_size_bits
;
2188 * Convert the maximum number of set bits into bytes rounded up, then
2189 * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
2190 * have one full and one partial page max_index = 2.
2192 max_index
= ((((mft_ni
->initialized_size
>> vol
->mft_record_size_bits
)
2193 + 7) >> 3) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
2194 read_unlock_irqrestore(&mft_ni
->size_lock
, flags
);
2195 /* Number of inodes in filesystem (at this point in time). */
2196 sfs
->f_files
= size
;
2197 /* Free inodes in fs (based on current total count). */
2198 sfs
->f_ffree
= __get_nr_free_mft_records(vol
, size
, max_index
);
2199 up_read(&vol
->mftbmp_lock
);
2201 * File system id. This is extremely *nix flavour dependent and even
2202 * within Linux itself all fs do their own thing. I interpret this to
2203 * mean a unique id associated with the mounted fs and not the id
2204 * associated with the filesystem driver, the latter is already given
2205 * by the filesystem type in sfs->f_type. Thus we use the 64-bit
2206 * volume serial number splitting it into two 32-bit parts. We enter
2207 * the least significant 32-bits in f_fsid[0] and the most significant
2208 * 32-bits in f_fsid[1].
2210 sfs
->f_fsid
.val
[0] = vol
->serial_no
& 0xffffffff;
2211 sfs
->f_fsid
.val
[1] = (vol
->serial_no
>> 32) & 0xffffffff;
2212 /* Maximum length of filenames. */
2213 sfs
->f_namelen
= NTFS_MAX_NAME_LEN
;
2218 * The complete super operations.
2220 static struct super_operations ntfs_sops
= {
2221 .alloc_inode
= ntfs_alloc_big_inode
, /* VFS: Allocate new inode. */
2222 .destroy_inode
= ntfs_destroy_big_inode
, /* VFS: Deallocate inode. */
2223 .put_inode
= ntfs_put_inode
, /* VFS: Called just before
2224 the inode reference count
2227 //.dirty_inode = NULL, /* VFS: Called from
2228 // __mark_inode_dirty(). */
2229 .write_inode
= ntfs_write_inode
, /* VFS: Write dirty inode to
2231 //.drop_inode = NULL, /* VFS: Called just after the
2232 // inode reference count has
2233 // been decreased to zero.
2234 // NOTE: The inode lock is
2235 // held. See fs/inode.c::
2236 // generic_drop_inode(). */
2237 //.delete_inode = NULL, /* VFS: Delete inode from disk.
2238 // Called when i_count becomes
2239 // 0 and i_nlink is also 0. */
2240 //.write_super = NULL, /* Flush dirty super block to
2242 //.sync_fs = NULL, /* ? */
2243 //.write_super_lockfs = NULL, /* ? */
2244 //.unlockfs = NULL, /* ? */
2245 #endif /* NTFS_RW */
2246 .put_super
= ntfs_put_super
, /* Syscall: umount. */
2247 .statfs
= ntfs_statfs
, /* Syscall: statfs */
2248 .remount_fs
= ntfs_remount
, /* Syscall: mount -o remount. */
2249 .clear_inode
= ntfs_clear_big_inode
, /* VFS: Called when an inode is
2250 removed from memory. */
2251 //.umount_begin = NULL, /* Forced umount. */
2252 .show_options
= ntfs_show_options
, /* Show mount options in
2257 * ntfs_fill_super - mount an ntfs filesystem
2258 * @sb: super block of ntfs filesystem to mount
2259 * @opt: string containing the mount options
2260 * @silent: silence error output
2262 * ntfs_fill_super() is called by the VFS to mount the device described by @sb
2263 * with the mount otions in @data with the NTFS filesystem.
2265 * If @silent is true, remain silent even if errors are detected. This is used
2266 * during bootup, when the kernel tries to mount the root filesystem with all
2267 * registered filesystems one after the other until one succeeds. This implies
2268 * that all filesystems except the correct one will quite correctly and
2269 * expectedly return an error, but nobody wants to see error messages when in
2270 * fact this is what is supposed to happen.
2272 * NOTE: @sb->s_flags contains the mount options flags.
2274 static int ntfs_fill_super(struct super_block
*sb
, void *opt
, const int silent
)
2277 struct buffer_head
*bh
;
2278 struct inode
*tmp_ino
;
2281 ntfs_debug("Entering.");
2283 sb
->s_flags
|= MS_RDONLY
| MS_NOATIME
| MS_NODIRATIME
;
2284 #endif /* ! NTFS_RW */
2285 /* Allocate a new ntfs_volume and place it in sb->s_fs_info. */
2286 sb
->s_fs_info
= kmalloc(sizeof(ntfs_volume
), GFP_NOFS
);
2290 ntfs_error(sb
, "Allocation of NTFS volume structure "
2291 "failed. Aborting mount...");
2294 /* Initialize ntfs_volume structure. */
2295 memset(vol
, 0, sizeof(ntfs_volume
));
2298 vol
->attrdef
= NULL
;
2299 vol
->mft_ino
= NULL
;
2300 vol
->mftbmp_ino
= NULL
;
2301 init_rwsem(&vol
->mftbmp_lock
);
2303 vol
->mftmirr_ino
= NULL
;
2304 vol
->logfile_ino
= NULL
;
2305 #endif /* NTFS_RW */
2306 vol
->lcnbmp_ino
= NULL
;
2307 init_rwsem(&vol
->lcnbmp_lock
);
2308 vol
->vol_ino
= NULL
;
2309 vol
->root_ino
= NULL
;
2310 vol
->secure_ino
= NULL
;
2311 vol
->extend_ino
= NULL
;
2313 vol
->quota_ino
= NULL
;
2314 vol
->quota_q_ino
= NULL
;
2315 #endif /* NTFS_RW */
2316 vol
->nls_map
= NULL
;
2319 * Default is group and other don't have any access to files or
2320 * directories while owner has full access. Further, files by default
2321 * are not executable but directories are of course browseable.
2328 /* By default, enable sparse support. */
2329 NVolSetSparseEnabled(vol
);
2331 /* Important to get the mount options dealt with now. */
2332 if (!parse_options(vol
, (char*)opt
))
2336 * TODO: Fail safety check. In the future we should really be able to
2337 * cope with this being the case, but for now just bail out.
2339 if (bdev_hardsect_size(sb
->s_bdev
) > NTFS_BLOCK_SIZE
) {
2341 ntfs_error(sb
, "Device has unsupported hardsect_size.");
2345 /* Setup the device access block size to NTFS_BLOCK_SIZE. */
2346 if (sb_set_blocksize(sb
, NTFS_BLOCK_SIZE
) != NTFS_BLOCK_SIZE
) {
2348 ntfs_error(sb
, "Unable to set block size.");
2352 /* Get the size of the device in units of NTFS_BLOCK_SIZE bytes. */
2353 vol
->nr_blocks
= i_size_read(sb
->s_bdev
->bd_inode
) >>
2354 NTFS_BLOCK_SIZE_BITS
;
2356 /* Read the boot sector and return unlocked buffer head to it. */
2357 if (!(bh
= read_ntfs_boot_sector(sb
, silent
))) {
2359 ntfs_error(sb
, "Not an NTFS volume.");
2364 * Extract the data from the boot sector and setup the ntfs super block
2367 result
= parse_ntfs_boot_sector(vol
, (NTFS_BOOT_SECTOR
*)bh
->b_data
);
2369 /* Initialize the cluster and mft allocators. */
2370 ntfs_setup_allocators(vol
);
2376 ntfs_error(sb
, "Unsupported NTFS filesystem.");
2381 * TODO: When we start coping with sector sizes different from
2382 * NTFS_BLOCK_SIZE, we now probably need to set the blocksize of the
2383 * device (probably to NTFS_BLOCK_SIZE).
2386 /* Setup remaining fields in the super block. */
2387 sb
->s_magic
= NTFS_SB_MAGIC
;
2390 * Ntfs allows 63 bits for the file size, i.e. correct would be:
2391 * sb->s_maxbytes = ~0ULL >> 1;
2392 * But the kernel uses a long as the page cache page index which on
2393 * 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel
2394 * defined to the maximum the page cache page index can cope with
2395 * without overflowing the index or to 2^63 - 1, whichever is smaller.
2397 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
2399 sb
->s_time_gran
= 100;
2402 * Now load the metadata required for the page cache and our address
2403 * space operations to function. We do this by setting up a specialised
2404 * read_inode method and then just calling the normal iget() to obtain
2405 * the inode for $MFT which is sufficient to allow our normal inode
2406 * operations and associated address space operations to function.
2408 sb
->s_op
= &ntfs_sops
;
2409 tmp_ino
= new_inode(sb
);
2412 ntfs_error(sb
, "Failed to load essential metadata.");
2415 tmp_ino
->i_ino
= FILE_MFT
;
2416 insert_inode_hash(tmp_ino
);
2417 if (ntfs_read_inode_mount(tmp_ino
) < 0) {
2419 ntfs_error(sb
, "Failed to load essential metadata.");
2420 goto iput_tmp_ino_err_out_now
;
2424 * The current mount is a compression user if the cluster size is
2425 * less than or equal 4kiB.
2427 if (vol
->cluster_size
<= 4096 && !ntfs_nr_compression_users
++) {
2428 result
= allocate_compression_buffers();
2430 ntfs_error(NULL
, "Failed to allocate buffers "
2431 "for compression engine.");
2432 ntfs_nr_compression_users
--;
2434 goto iput_tmp_ino_err_out_now
;
2438 * Generate the global default upcase table if necessary. Also
2439 * temporarily increment the number of upcase users to avoid race
2440 * conditions with concurrent (u)mounts.
2442 if (!default_upcase
)
2443 default_upcase
= generate_default_upcase();
2444 ntfs_nr_upcase_users
++;
2447 * From now on, ignore @silent parameter. If we fail below this line,
2448 * it will be due to a corrupt fs or a system error, so we report it.
2451 * Open the system files with normal access functions and complete
2452 * setting up the ntfs super block.
2454 if (!load_system_files(vol
)) {
2455 ntfs_error(sb
, "Failed to load system files.");
2456 goto unl_upcase_iput_tmp_ino_err_out_now
;
2458 if ((sb
->s_root
= d_alloc_root(vol
->root_ino
))) {
2459 /* We increment i_count simulating an ntfs_iget(). */
2460 atomic_inc(&vol
->root_ino
->i_count
);
2461 ntfs_debug("Exiting, status successful.");
2462 /* Release the default upcase if it has no users. */
2464 if (!--ntfs_nr_upcase_users
&& default_upcase
) {
2465 ntfs_free(default_upcase
);
2466 default_upcase
= NULL
;
2469 sb
->s_export_op
= &ntfs_export_ops
;
2473 ntfs_error(sb
, "Failed to allocate root directory.");
2474 /* Clean up after the successful load_system_files() call from above. */
2475 // TODO: Use ntfs_put_super() instead of repeating all this code...
2476 // FIXME: Should mark the volume clean as the error is most likely
2479 vol
->vol_ino
= NULL
;
2480 /* NTFS 3.0+ specific clean up. */
2481 if (vol
->major_ver
>= 3) {
2483 if (vol
->quota_q_ino
) {
2484 iput(vol
->quota_q_ino
);
2485 vol
->quota_q_ino
= NULL
;
2487 if (vol
->quota_ino
) {
2488 iput(vol
->quota_ino
);
2489 vol
->quota_ino
= NULL
;
2491 #endif /* NTFS_RW */
2492 if (vol
->extend_ino
) {
2493 iput(vol
->extend_ino
);
2494 vol
->extend_ino
= NULL
;
2496 if (vol
->secure_ino
) {
2497 iput(vol
->secure_ino
);
2498 vol
->secure_ino
= NULL
;
2501 iput(vol
->root_ino
);
2502 vol
->root_ino
= NULL
;
2503 iput(vol
->lcnbmp_ino
);
2504 vol
->lcnbmp_ino
= NULL
;
2505 iput(vol
->mftbmp_ino
);
2506 vol
->mftbmp_ino
= NULL
;
2508 if (vol
->logfile_ino
) {
2509 iput(vol
->logfile_ino
);
2510 vol
->logfile_ino
= NULL
;
2512 if (vol
->mftmirr_ino
) {
2513 iput(vol
->mftmirr_ino
);
2514 vol
->mftmirr_ino
= NULL
;
2516 #endif /* NTFS_RW */
2517 /* Throw away the table of attribute definitions. */
2518 vol
->attrdef_size
= 0;
2520 ntfs_free(vol
->attrdef
);
2521 vol
->attrdef
= NULL
;
2523 vol
->upcase_len
= 0;
2525 if (vol
->upcase
== default_upcase
) {
2526 ntfs_nr_upcase_users
--;
2531 ntfs_free(vol
->upcase
);
2535 unload_nls(vol
->nls_map
);
2536 vol
->nls_map
= NULL
;
2538 /* Error exit code path. */
2539 unl_upcase_iput_tmp_ino_err_out_now
:
2541 * Decrease the number of upcase users and destroy the global default
2542 * upcase table if necessary.
2545 if (!--ntfs_nr_upcase_users
&& default_upcase
) {
2546 ntfs_free(default_upcase
);
2547 default_upcase
= NULL
;
2549 if (vol
->cluster_size
<= 4096 && !--ntfs_nr_compression_users
)
2550 free_compression_buffers();
2552 iput_tmp_ino_err_out_now
:
2554 if (vol
->mft_ino
&& vol
->mft_ino
!= tmp_ino
)
2556 vol
->mft_ino
= NULL
;
2558 * This is needed to get ntfs_clear_extent_inode() called for each
2559 * inode we have ever called ntfs_iget()/iput() on, otherwise we A)
2560 * leak resources and B) a subsequent mount fails automatically due to
2561 * ntfs_iget() never calling down into our ntfs_read_locked_inode()
2562 * method again... FIXME: Do we need to do this twice now because of
2563 * attribute inodes? I think not, so leave as is for now... (AIA)
2565 if (invalidate_inodes(sb
)) {
2566 ntfs_error(sb
, "Busy inodes left. This is most likely a NTFS "
2568 /* Copied from fs/super.c. I just love this message. (-; */
2569 printk("NTFS: Busy inodes after umount. Self-destruct in 5 "
2570 "seconds. Have a nice day...\n");
2572 /* Errors at this stage are irrelevant. */
2575 sb
->s_fs_info
= NULL
;
2577 ntfs_debug("Failed, returning -EINVAL.");
2582 * This is a slab cache to optimize allocations and deallocations of Unicode
2583 * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN
2584 * (255) Unicode characters + a terminating NULL Unicode character.
2586 kmem_cache_t
*ntfs_name_cache
;
2588 /* Slab caches for efficient allocation/deallocation of of inodes. */
2589 kmem_cache_t
*ntfs_inode_cache
;
2590 kmem_cache_t
*ntfs_big_inode_cache
;
2592 /* Init once constructor for the inode slab cache. */
2593 static void ntfs_big_inode_init_once(void *foo
, kmem_cache_t
*cachep
,
2594 unsigned long flags
)
2596 ntfs_inode
*ni
= (ntfs_inode
*)foo
;
2598 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
2599 SLAB_CTOR_CONSTRUCTOR
)
2600 inode_init_once(VFS_I(ni
));
2604 * Slab caches to optimize allocations and deallocations of attribute search
2605 * contexts and index contexts, respectively.
2607 kmem_cache_t
*ntfs_attr_ctx_cache
;
2608 kmem_cache_t
*ntfs_index_ctx_cache
;
2610 /* Driver wide semaphore. */
2611 DECLARE_MUTEX(ntfs_lock
);
2613 static struct super_block
*ntfs_get_sb(struct file_system_type
*fs_type
,
2614 int flags
, const char *dev_name
, void *data
)
2616 return get_sb_bdev(fs_type
, flags
, dev_name
, data
, ntfs_fill_super
);
2619 static struct file_system_type ntfs_fs_type
= {
2620 .owner
= THIS_MODULE
,
2622 .get_sb
= ntfs_get_sb
,
2623 .kill_sb
= kill_block_super
,
2624 .fs_flags
= FS_REQUIRES_DEV
,
2627 /* Stable names for the slab caches. */
2628 static const char ntfs_index_ctx_cache_name
[] = "ntfs_index_ctx_cache";
2629 static const char ntfs_attr_ctx_cache_name
[] = "ntfs_attr_ctx_cache";
2630 static const char ntfs_name_cache_name
[] = "ntfs_name_cache";
2631 static const char ntfs_inode_cache_name
[] = "ntfs_inode_cache";
2632 static const char ntfs_big_inode_cache_name
[] = "ntfs_big_inode_cache";
2634 static int __init
init_ntfs_fs(void)
2638 /* This may be ugly but it results in pretty output so who cares. (-8 */
2639 printk(KERN_INFO
"NTFS driver " NTFS_VERSION
" [Flags: R/"
2653 ntfs_debug("Debug messages are enabled.");
2655 ntfs_index_ctx_cache
= kmem_cache_create(ntfs_index_ctx_cache_name
,
2656 sizeof(ntfs_index_context
), 0 /* offset */,
2657 SLAB_HWCACHE_ALIGN
, NULL
/* ctor */, NULL
/* dtor */);
2658 if (!ntfs_index_ctx_cache
) {
2659 printk(KERN_CRIT
"NTFS: Failed to create %s!\n",
2660 ntfs_index_ctx_cache_name
);
2663 ntfs_attr_ctx_cache
= kmem_cache_create(ntfs_attr_ctx_cache_name
,
2664 sizeof(ntfs_attr_search_ctx
), 0 /* offset */,
2665 SLAB_HWCACHE_ALIGN
, NULL
/* ctor */, NULL
/* dtor */);
2666 if (!ntfs_attr_ctx_cache
) {
2667 printk(KERN_CRIT
"NTFS: Failed to create %s!\n",
2668 ntfs_attr_ctx_cache_name
);
2672 ntfs_name_cache
= kmem_cache_create(ntfs_name_cache_name
,
2673 (NTFS_MAX_NAME_LEN
+1) * sizeof(ntfschar
), 0,
2674 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
2675 if (!ntfs_name_cache
) {
2676 printk(KERN_CRIT
"NTFS: Failed to create %s!\n",
2677 ntfs_name_cache_name
);
2681 ntfs_inode_cache
= kmem_cache_create(ntfs_inode_cache_name
,
2682 sizeof(ntfs_inode
), 0,
2683 SLAB_RECLAIM_ACCOUNT
, NULL
, NULL
);
2684 if (!ntfs_inode_cache
) {
2685 printk(KERN_CRIT
"NTFS: Failed to create %s!\n",
2686 ntfs_inode_cache_name
);
2690 ntfs_big_inode_cache
= kmem_cache_create(ntfs_big_inode_cache_name
,
2691 sizeof(big_ntfs_inode
), 0,
2692 SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
,
2693 ntfs_big_inode_init_once
, NULL
);
2694 if (!ntfs_big_inode_cache
) {
2695 printk(KERN_CRIT
"NTFS: Failed to create %s!\n",
2696 ntfs_big_inode_cache_name
);
2697 goto big_inode_err_out
;
2700 /* Register the ntfs sysctls. */
2701 err
= ntfs_sysctl(1);
2703 printk(KERN_CRIT
"NTFS: Failed to register NTFS sysctls!\n");
2704 goto sysctl_err_out
;
2707 err
= register_filesystem(&ntfs_fs_type
);
2709 ntfs_debug("NTFS driver registered successfully.");
2710 return 0; /* Success! */
2712 printk(KERN_CRIT
"NTFS: Failed to register NTFS filesystem driver!\n");
2715 kmem_cache_destroy(ntfs_big_inode_cache
);
2717 kmem_cache_destroy(ntfs_inode_cache
);
2719 kmem_cache_destroy(ntfs_name_cache
);
2721 kmem_cache_destroy(ntfs_attr_ctx_cache
);
2723 kmem_cache_destroy(ntfs_index_ctx_cache
);
2726 printk(KERN_CRIT
"NTFS: Aborting NTFS filesystem driver "
2727 "registration...\n");
2733 static void __exit
exit_ntfs_fs(void)
2737 ntfs_debug("Unregistering NTFS driver.");
2739 unregister_filesystem(&ntfs_fs_type
);
2741 if (kmem_cache_destroy(ntfs_big_inode_cache
) && (err
= 1))
2742 printk(KERN_CRIT
"NTFS: Failed to destory %s.\n",
2743 ntfs_big_inode_cache_name
);
2744 if (kmem_cache_destroy(ntfs_inode_cache
) && (err
= 1))
2745 printk(KERN_CRIT
"NTFS: Failed to destory %s.\n",
2746 ntfs_inode_cache_name
);
2747 if (kmem_cache_destroy(ntfs_name_cache
) && (err
= 1))
2748 printk(KERN_CRIT
"NTFS: Failed to destory %s.\n",
2749 ntfs_name_cache_name
);
2750 if (kmem_cache_destroy(ntfs_attr_ctx_cache
) && (err
= 1))
2751 printk(KERN_CRIT
"NTFS: Failed to destory %s.\n",
2752 ntfs_attr_ctx_cache_name
);
2753 if (kmem_cache_destroy(ntfs_index_ctx_cache
) && (err
= 1))
2754 printk(KERN_CRIT
"NTFS: Failed to destory %s.\n",
2755 ntfs_index_ctx_cache_name
);
2757 printk(KERN_CRIT
"NTFS: This causes memory to leak! There is "
2758 "probably a BUG in the driver! Please report "
2759 "you saw this message to "
2760 "linux-ntfs-dev@lists.sourceforge.net\n");
2761 /* Unregister the ntfs sysctls. */
2765 MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>");
2766 MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2005 Anton Altaparmakov");
2767 MODULE_VERSION(NTFS_VERSION
);
2768 MODULE_LICENSE("GPL");
2770 module_param(debug_msgs
, bool, 0);
2771 MODULE_PARM_DESC(debug_msgs
, "Enable debug messages.");
2774 module_init(init_ntfs_fs
)
2775 module_exit(exit_ntfs_fs
)