GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / fs / ntfs / super.c
blobd06252fb848396e31f57cd7d9f2be7dad51ff792
1 /*
2 * super.c - NTFS kernel super block handling. Part of the Linux-NTFS project.
4 * Copyright (c) 2001-2007 Anton Altaparmakov
5 * Copyright (c) 2001,2002 Richard Russon
7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program/include file is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program (in the main directory of the Linux-NTFS
19 * distribution in the file COPYING); if not, write to the Free Software
20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/stddef.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
27 #include <linux/spinlock.h>
28 #include <linux/blkdev.h> /* For bdev_logical_block_size(). */
29 #include <linux/backing-dev.h>
30 #include <linux/buffer_head.h>
31 #include <linux/vfs.h>
32 #include <linux/moduleparam.h>
33 #include <linux/smp_lock.h>
34 #include <linux/bitmap.h>
36 #include "sysctl.h"
37 #include "logfile.h"
38 #include "quota.h"
39 #include "usnjrnl.h"
40 #include "dir.h"
41 #include "debug.h"
42 #include "index.h"
43 #include "inode.h"
44 #include "aops.h"
45 #include "layout.h"
46 #include "malloc.h"
47 #include "ntfs.h"
49 /* Number of mounted filesystems which have compression enabled. */
50 static unsigned long ntfs_nr_compression_users;
52 /* A global default upcase table and a corresponding reference count. */
53 static ntfschar *default_upcase = NULL;
54 static unsigned long ntfs_nr_upcase_users = 0;
56 /* Error constants/strings used in inode.c::ntfs_show_options(). */
57 typedef enum {
58 /* One of these must be present, default is ON_ERRORS_CONTINUE. */
59 ON_ERRORS_PANIC = 0x01,
60 ON_ERRORS_REMOUNT_RO = 0x02,
61 ON_ERRORS_CONTINUE = 0x04,
62 /* Optional, can be combined with any of the above. */
63 ON_ERRORS_RECOVER = 0x10,
64 } ON_ERRORS_ACTIONS;
66 const option_t on_errors_arr[] = {
67 { ON_ERRORS_PANIC, "panic" },
68 { ON_ERRORS_REMOUNT_RO, "remount-ro", },
69 { ON_ERRORS_CONTINUE, "continue", },
70 { ON_ERRORS_RECOVER, "recover" },
71 { 0, NULL }
74 /**
75 * simple_getbool -
77 * Copied from old ntfs driver (which copied from vfat driver).
79 static int simple_getbool(char *s, bool *setval)
81 if (s) {
82 if (!strcmp(s, "1") || !strcmp(s, "yes") || !strcmp(s, "true"))
83 *setval = true;
84 else if (!strcmp(s, "0") || !strcmp(s, "no") ||
85 !strcmp(s, "false"))
86 *setval = false;
87 else
88 return 0;
89 } else
90 *setval = true;
91 return 1;
94 /**
95 * parse_options - parse the (re)mount options
96 * @vol: ntfs volume
97 * @opt: string containing the (re)mount options
99 * Parse the recognized options in @opt for the ntfs volume described by @vol.
101 static bool parse_options(ntfs_volume *vol, char *opt)
103 char *p, *v, *ov;
104 static char *utf8 = "utf8";
105 int errors = 0, sloppy = 0;
106 uid_t uid = (uid_t)-1;
107 gid_t gid = (gid_t)-1;
108 mode_t fmask = (mode_t)-1, dmask = (mode_t)-1;
109 int mft_zone_multiplier = -1, on_errors = -1;
110 int show_sys_files = -1, case_sensitive = -1, disable_sparse = -1;
111 struct nls_table *nls_map = NULL, *old_nls;
113 /* I am lazy... (-8 */
114 #define NTFS_GETOPT_WITH_DEFAULT(option, variable, default_value) \
115 if (!strcmp(p, option)) { \
116 if (!v || !*v) \
117 variable = default_value; \
118 else { \
119 variable = simple_strtoul(ov = v, &v, 0); \
120 if (*v) \
121 goto needs_val; \
124 #define NTFS_GETOPT(option, variable) \
125 if (!strcmp(p, option)) { \
126 if (!v || !*v) \
127 goto needs_arg; \
128 variable = simple_strtoul(ov = v, &v, 0); \
129 if (*v) \
130 goto needs_val; \
132 #define NTFS_GETOPT_OCTAL(option, variable) \
133 if (!strcmp(p, option)) { \
134 if (!v || !*v) \
135 goto needs_arg; \
136 variable = simple_strtoul(ov = v, &v, 8); \
137 if (*v) \
138 goto needs_val; \
140 #define NTFS_GETOPT_BOOL(option, variable) \
141 if (!strcmp(p, option)) { \
142 bool val; \
143 if (!simple_getbool(v, &val)) \
144 goto needs_bool; \
145 variable = val; \
147 #define NTFS_GETOPT_OPTIONS_ARRAY(option, variable, opt_array) \
148 if (!strcmp(p, option)) { \
149 int _i; \
150 if (!v || !*v) \
151 goto needs_arg; \
152 ov = v; \
153 if (variable == -1) \
154 variable = 0; \
155 for (_i = 0; opt_array[_i].str && *opt_array[_i].str; _i++) \
156 if (!strcmp(opt_array[_i].str, v)) { \
157 variable |= opt_array[_i].val; \
158 break; \
160 if (!opt_array[_i].str || !*opt_array[_i].str) \
161 goto needs_val; \
163 if (!opt || !*opt)
164 goto no_mount_options;
165 ntfs_debug("Entering with mount options string: %s", opt);
166 while ((p = strsep(&opt, ","))) {
167 if ((v = strchr(p, '=')))
168 *v++ = 0;
169 NTFS_GETOPT("uid", uid)
170 else NTFS_GETOPT("gid", gid)
171 else NTFS_GETOPT_OCTAL("umask", fmask = dmask)
172 else NTFS_GETOPT_OCTAL("fmask", fmask)
173 else NTFS_GETOPT_OCTAL("dmask", dmask)
174 else NTFS_GETOPT("mft_zone_multiplier", mft_zone_multiplier)
175 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, true)
176 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files)
177 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive)
178 else NTFS_GETOPT_BOOL("disable_sparse", disable_sparse)
179 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors,
180 on_errors_arr)
181 else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes"))
182 ntfs_warning(vol->sb, "Ignoring obsolete option %s.",
184 else if (!strcmp(p, "nls") || !strcmp(p, "iocharset")) {
185 if (!strcmp(p, "iocharset"))
186 ntfs_warning(vol->sb, "Option iocharset is "
187 "deprecated. Please use "
188 "option nls=<charsetname> in "
189 "the future.");
190 if (!v || !*v)
191 goto needs_arg;
192 use_utf8:
193 old_nls = nls_map;
194 nls_map = load_nls(v);
195 if (!nls_map) {
196 if (!old_nls) {
197 ntfs_error(vol->sb, "NLS character set "
198 "%s not found.", v);
199 return false;
201 ntfs_error(vol->sb, "NLS character set %s not "
202 "found. Using previous one %s.",
203 v, old_nls->charset);
204 nls_map = old_nls;
205 } else /* nls_map */ {
206 unload_nls(old_nls);
208 } else if (!strcmp(p, "utf8")) {
209 bool val = false;
210 ntfs_warning(vol->sb, "Option utf8 is no longer "
211 "supported, using option nls=utf8. Please "
212 "use option nls=utf8 in the future and "
213 "make sure utf8 is compiled either as a "
214 "module or into the kernel.");
215 if (!v || !*v)
216 val = true;
217 else if (!simple_getbool(v, &val))
218 goto needs_bool;
219 if (val) {
220 v = utf8;
221 goto use_utf8;
223 } else {
224 ntfs_error(vol->sb, "Unrecognized mount option %s.", p);
225 if (errors < INT_MAX)
226 errors++;
228 #undef NTFS_GETOPT_OPTIONS_ARRAY
229 #undef NTFS_GETOPT_BOOL
230 #undef NTFS_GETOPT
231 #undef NTFS_GETOPT_WITH_DEFAULT
233 no_mount_options:
234 if (errors && !sloppy)
235 return false;
236 if (sloppy)
237 ntfs_warning(vol->sb, "Sloppy option given. Ignoring "
238 "unrecognized mount option(s) and continuing.");
239 /* Keep this first! */
240 if (on_errors != -1) {
241 if (!on_errors) {
242 ntfs_error(vol->sb, "Invalid errors option argument "
243 "or bug in options parser.");
244 return false;
247 if (nls_map) {
248 if (vol->nls_map && vol->nls_map != nls_map) {
249 ntfs_error(vol->sb, "Cannot change NLS character set "
250 "on remount.");
251 return false;
252 } /* else (!vol->nls_map) */
253 ntfs_debug("Using NLS character set %s.", nls_map->charset);
254 vol->nls_map = nls_map;
255 } else /* (!nls_map) */ {
256 if (!vol->nls_map) {
257 vol->nls_map = load_nls_default();
258 if (!vol->nls_map) {
259 ntfs_error(vol->sb, "Failed to load default "
260 "NLS character set.");
261 return false;
263 ntfs_debug("Using default NLS character set (%s).",
264 vol->nls_map->charset);
267 if (mft_zone_multiplier != -1) {
268 if (vol->mft_zone_multiplier && vol->mft_zone_multiplier !=
269 mft_zone_multiplier) {
270 ntfs_error(vol->sb, "Cannot change mft_zone_multiplier "
271 "on remount.");
272 return false;
274 if (mft_zone_multiplier < 1 || mft_zone_multiplier > 4) {
275 ntfs_error(vol->sb, "Invalid mft_zone_multiplier. "
276 "Using default value, i.e. 1.");
277 mft_zone_multiplier = 1;
279 vol->mft_zone_multiplier = mft_zone_multiplier;
281 if (!vol->mft_zone_multiplier)
282 vol->mft_zone_multiplier = 1;
283 if (on_errors != -1)
284 vol->on_errors = on_errors;
285 if (!vol->on_errors || vol->on_errors == ON_ERRORS_RECOVER)
286 vol->on_errors |= ON_ERRORS_CONTINUE;
287 if (uid != (uid_t)-1)
288 vol->uid = uid;
289 if (gid != (gid_t)-1)
290 vol->gid = gid;
291 if (fmask != (mode_t)-1)
292 vol->fmask = fmask;
293 if (dmask != (mode_t)-1)
294 vol->dmask = dmask;
295 if (show_sys_files != -1) {
296 if (show_sys_files)
297 NVolSetShowSystemFiles(vol);
298 else
299 NVolClearShowSystemFiles(vol);
301 if (case_sensitive != -1) {
302 if (case_sensitive)
303 NVolSetCaseSensitive(vol);
304 else
305 NVolClearCaseSensitive(vol);
307 if (disable_sparse != -1) {
308 if (disable_sparse)
309 NVolClearSparseEnabled(vol);
310 else {
311 if (!NVolSparseEnabled(vol) &&
312 vol->major_ver && vol->major_ver < 3)
313 ntfs_warning(vol->sb, "Not enabling sparse "
314 "support due to NTFS volume "
315 "version %i.%i (need at least "
316 "version 3.0).", vol->major_ver,
317 vol->minor_ver);
318 else
319 NVolSetSparseEnabled(vol);
322 return true;
323 needs_arg:
324 ntfs_error(vol->sb, "The %s option requires an argument.", p);
325 return false;
326 needs_bool:
327 ntfs_error(vol->sb, "The %s option requires a boolean argument.", p);
328 return false;
329 needs_val:
330 ntfs_error(vol->sb, "Invalid %s option argument: %s", p, ov);
331 return false;
334 #ifdef NTFS_RW
337 * ntfs_write_volume_flags - write new flags to the volume information flags
338 * @vol: ntfs volume on which to modify the flags
339 * @flags: new flags value for the volume information flags
341 * Internal function. You probably want to use ntfs_{set,clear}_volume_flags()
342 * instead (see below).
344 * Replace the volume information flags on the volume @vol with the value
345 * supplied in @flags. Note, this overwrites the volume information flags, so
346 * make sure to combine the flags you want to modify with the old flags and use
347 * the result when calling ntfs_write_volume_flags().
349 * Return 0 on success and -errno on error.
351 static int ntfs_write_volume_flags(ntfs_volume *vol, const VOLUME_FLAGS flags)
353 ntfs_inode *ni = NTFS_I(vol->vol_ino);
354 MFT_RECORD *m;
355 VOLUME_INFORMATION *vi;
356 ntfs_attr_search_ctx *ctx;
357 int err;
359 ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
360 le16_to_cpu(vol->vol_flags), le16_to_cpu(flags));
361 if (vol->vol_flags == flags)
362 goto done;
363 BUG_ON(!ni);
364 m = map_mft_record(ni);
365 if (IS_ERR(m)) {
366 err = PTR_ERR(m);
367 goto err_out;
369 ctx = ntfs_attr_get_search_ctx(ni, m);
370 if (!ctx) {
371 err = -ENOMEM;
372 goto put_unm_err_out;
374 err = ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
375 ctx);
376 if (err)
377 goto put_unm_err_out;
378 vi = (VOLUME_INFORMATION*)((u8*)ctx->attr +
379 le16_to_cpu(ctx->attr->data.resident.value_offset));
380 vol->vol_flags = vi->flags = flags;
381 flush_dcache_mft_record_page(ctx->ntfs_ino);
382 mark_mft_record_dirty(ctx->ntfs_ino);
383 ntfs_attr_put_search_ctx(ctx);
384 unmap_mft_record(ni);
385 done:
386 ntfs_debug("Done.");
387 return 0;
388 put_unm_err_out:
389 if (ctx)
390 ntfs_attr_put_search_ctx(ctx);
391 unmap_mft_record(ni);
392 err_out:
393 ntfs_error(vol->sb, "Failed with error code %i.", -err);
394 return err;
398 * ntfs_set_volume_flags - set bits in the volume information flags
399 * @vol: ntfs volume on which to modify the flags
400 * @flags: flags to set on the volume
402 * Set the bits in @flags in the volume information flags on the volume @vol.
404 * Return 0 on success and -errno on error.
406 static inline int ntfs_set_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
408 flags &= VOLUME_FLAGS_MASK;
409 return ntfs_write_volume_flags(vol, vol->vol_flags | flags);
413 * ntfs_clear_volume_flags - clear bits in the volume information flags
414 * @vol: ntfs volume on which to modify the flags
415 * @flags: flags to clear on the volume
417 * Clear the bits in @flags in the volume information flags on the volume @vol.
419 * Return 0 on success and -errno on error.
421 static inline int ntfs_clear_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
423 flags &= VOLUME_FLAGS_MASK;
424 flags = vol->vol_flags & cpu_to_le16(~le16_to_cpu(flags));
425 return ntfs_write_volume_flags(vol, flags);
428 #endif /* NTFS_RW */
431 * ntfs_remount - change the mount options of a mounted ntfs filesystem
432 * @sb: superblock of mounted ntfs filesystem
433 * @flags: remount flags
434 * @opt: remount options string
436 * Change the mount options of an already mounted ntfs filesystem.
438 * NOTE: The VFS sets the @sb->s_flags remount flags to @flags after
439 * ntfs_remount() returns successfully (i.e. returns 0). Otherwise,
440 * @sb->s_flags are not changed.
442 static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
444 ntfs_volume *vol = NTFS_SB(sb);
446 ntfs_debug("Entering with remount options string: %s", opt);
448 lock_kernel();
449 #ifndef NTFS_RW
450 /* For read-only compiled driver, enforce read-only flag. */
451 *flags |= MS_RDONLY;
452 #else /* NTFS_RW */
454 * For the read-write compiled driver, if we are remounting read-write,
455 * make sure there are no volume errors and that no unsupported volume
456 * flags are set. Also, empty the logfile journal as it would become
457 * stale as soon as something is written to the volume and mark the
458 * volume dirty so that chkdsk is run if the volume is not umounted
459 * cleanly. Finally, mark the quotas out of date so Windows rescans
460 * the volume on boot and updates them.
462 * When remounting read-only, mark the volume clean if no volume errors
463 * have occured.
465 if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
466 static const char *es = ". Cannot remount read-write.";
468 /* Remounting read-write. */
469 if (NVolErrors(vol)) {
470 ntfs_error(sb, "Volume has errors and is read-only%s",
471 es);
472 unlock_kernel();
473 return -EROFS;
475 if (vol->vol_flags & VOLUME_IS_DIRTY) {
476 ntfs_error(sb, "Volume is dirty and read-only%s", es);
477 unlock_kernel();
478 return -EROFS;
480 if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
481 ntfs_error(sb, "Volume has been modified by chkdsk "
482 "and is read-only%s", es);
483 unlock_kernel();
484 return -EROFS;
486 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
487 ntfs_error(sb, "Volume has unsupported flags set "
488 "(0x%x) and is read-only%s",
489 (unsigned)le16_to_cpu(vol->vol_flags),
490 es);
491 unlock_kernel();
492 return -EROFS;
494 if (ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
495 ntfs_error(sb, "Failed to set dirty bit in volume "
496 "information flags%s", es);
497 unlock_kernel();
498 return -EROFS;
500 if (!ntfs_empty_logfile(vol->logfile_ino)) {
501 ntfs_error(sb, "Failed to empty journal $LogFile%s",
502 es);
503 NVolSetErrors(vol);
504 unlock_kernel();
505 return -EROFS;
507 if (!ntfs_mark_quotas_out_of_date(vol)) {
508 ntfs_error(sb, "Failed to mark quotas out of date%s",
509 es);
510 NVolSetErrors(vol);
511 unlock_kernel();
512 return -EROFS;
514 if (!ntfs_stamp_usnjrnl(vol)) {
515 ntfs_error(sb, "Failed to stamp transation log "
516 "($UsnJrnl)%s", es);
517 NVolSetErrors(vol);
518 unlock_kernel();
519 return -EROFS;
521 } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) {
522 /* Remounting read-only. */
523 if (!NVolErrors(vol)) {
524 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
525 ntfs_warning(sb, "Failed to clear dirty bit "
526 "in volume information "
527 "flags. Run chkdsk.");
530 #endif /* NTFS_RW */
532 // TODO: Deal with *flags.
534 if (!parse_options(vol, opt)) {
535 unlock_kernel();
536 return -EINVAL;
538 unlock_kernel();
539 ntfs_debug("Done.");
540 return 0;
544 * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector
545 * @sb: Super block of the device to which @b belongs.
546 * @b: Boot sector of device @sb to check.
547 * @silent: If 'true', all output will be silenced.
549 * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot
550 * sector. Returns 'true' if it is valid and 'false' if not.
552 * @sb is only needed for warning/error output, i.e. it can be NULL when silent
553 * is 'true'.
555 static bool is_boot_sector_ntfs(const struct super_block *sb,
556 const NTFS_BOOT_SECTOR *b, const bool silent)
559 * Check that checksum == sum of u32 values from b to the checksum
560 * field. If checksum is zero, no checking is done. We will work when
561 * the checksum test fails, since some utilities update the boot sector
562 * ignoring the checksum which leaves the checksum out-of-date. We
563 * report a warning if this is the case.
565 if ((void*)b < (void*)&b->checksum && b->checksum && !silent) {
566 le32 *u;
567 u32 i;
569 for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u)
570 i += le32_to_cpup(u);
571 if (le32_to_cpu(b->checksum) != i)
572 ntfs_warning(sb, "Invalid boot sector checksum.");
574 /* Check OEMidentifier is "NTFS " */
575 if (b->oem_id != magicNTFS)
576 goto not_ntfs;
577 /* Check bytes per sector value is between 256 and 4096. */
578 if (le16_to_cpu(b->bpb.bytes_per_sector) < 0x100 ||
579 le16_to_cpu(b->bpb.bytes_per_sector) > 0x1000)
580 goto not_ntfs;
581 /* Check sectors per cluster value is valid. */
582 switch (b->bpb.sectors_per_cluster) {
583 case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128:
584 break;
585 default:
586 goto not_ntfs;
588 /* Check the cluster size is not above the maximum (64kiB). */
589 if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) *
590 b->bpb.sectors_per_cluster > NTFS_MAX_CLUSTER_SIZE)
591 goto not_ntfs;
592 /* Check reserved/unused fields are really zero. */
593 if (le16_to_cpu(b->bpb.reserved_sectors) ||
594 le16_to_cpu(b->bpb.root_entries) ||
595 le16_to_cpu(b->bpb.sectors) ||
596 le16_to_cpu(b->bpb.sectors_per_fat) ||
597 le32_to_cpu(b->bpb.large_sectors) || b->bpb.fats)
598 goto not_ntfs;
599 /* Check clusters per file mft record value is valid. */
600 if ((u8)b->clusters_per_mft_record < 0xe1 ||
601 (u8)b->clusters_per_mft_record > 0xf7)
602 switch (b->clusters_per_mft_record) {
603 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
604 break;
605 default:
606 goto not_ntfs;
608 /* Check clusters per index block value is valid. */
609 if ((u8)b->clusters_per_index_record < 0xe1 ||
610 (u8)b->clusters_per_index_record > 0xf7)
611 switch (b->clusters_per_index_record) {
612 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
613 break;
614 default:
615 goto not_ntfs;
618 * Check for valid end of sector marker. We will work without it, but
619 * many BIOSes will refuse to boot from a bootsector if the magic is
620 * incorrect, so we emit a warning.
622 if (!silent && b->end_of_sector_marker != cpu_to_le16(0xaa55))
623 ntfs_warning(sb, "Invalid end of sector marker.");
624 return true;
625 not_ntfs:
626 return false;
630 * read_ntfs_boot_sector - read the NTFS boot sector of a device
631 * @sb: super block of device to read the boot sector from
632 * @silent: if true, suppress all output
634 * Reads the boot sector from the device and validates it. If that fails, tries
635 * to read the backup boot sector, first from the end of the device a-la NT4 and
636 * later and then from the middle of the device a-la NT3.51 and before.
638 * If a valid boot sector is found but it is not the primary boot sector, we
639 * repair the primary boot sector silently (unless the device is read-only or
640 * the primary boot sector is not accessible).
642 * NOTE: To call this function, @sb must have the fields s_dev, the ntfs super
643 * block (u.ntfs_sb), nr_blocks and the device flags (s_flags) initialized
644 * to their respective values.
646 * Return the unlocked buffer head containing the boot sector or NULL on error.
648 static struct buffer_head *read_ntfs_boot_sector(struct super_block *sb,
649 const int silent)
651 const char *read_err_str = "Unable to read %s boot sector.";
652 struct buffer_head *bh_primary, *bh_backup;
653 sector_t nr_blocks = NTFS_SB(sb)->nr_blocks;
655 /* Try to read primary boot sector. */
656 if ((bh_primary = sb_bread(sb, 0))) {
657 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
658 bh_primary->b_data, silent))
659 return bh_primary;
660 if (!silent)
661 ntfs_error(sb, "Primary boot sector is invalid.");
662 } else if (!silent)
663 ntfs_error(sb, read_err_str, "primary");
664 if (!(NTFS_SB(sb)->on_errors & ON_ERRORS_RECOVER)) {
665 if (bh_primary)
666 brelse(bh_primary);
667 if (!silent)
668 ntfs_error(sb, "Mount option errors=recover not used. "
669 "Aborting without trying to recover.");
670 return NULL;
672 /* Try to read NT4+ backup boot sector. */
673 if ((bh_backup = sb_bread(sb, nr_blocks - 1))) {
674 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
675 bh_backup->b_data, silent))
676 goto hotfix_primary_boot_sector;
677 brelse(bh_backup);
678 } else if (!silent)
679 ntfs_error(sb, read_err_str, "backup");
680 /* Try to read NT3.51- backup boot sector. */
681 if ((bh_backup = sb_bread(sb, nr_blocks >> 1))) {
682 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
683 bh_backup->b_data, silent))
684 goto hotfix_primary_boot_sector;
685 if (!silent)
686 ntfs_error(sb, "Could not find a valid backup boot "
687 "sector.");
688 brelse(bh_backup);
689 } else if (!silent)
690 ntfs_error(sb, read_err_str, "backup");
691 /* We failed. Cleanup and return. */
692 if (bh_primary)
693 brelse(bh_primary);
694 return NULL;
695 hotfix_primary_boot_sector:
696 if (bh_primary) {
697 if (!(sb->s_flags & MS_RDONLY)) {
698 ntfs_warning(sb, "Hot-fix: Recovering invalid primary "
699 "boot sector from backup copy.");
700 memcpy(bh_primary->b_data, bh_backup->b_data,
701 NTFS_BLOCK_SIZE);
702 mark_buffer_dirty(bh_primary);
703 sync_dirty_buffer(bh_primary);
704 if (buffer_uptodate(bh_primary)) {
705 brelse(bh_backup);
706 return bh_primary;
708 ntfs_error(sb, "Hot-fix: Device write error while "
709 "recovering primary boot sector.");
710 } else {
711 ntfs_warning(sb, "Hot-fix: Recovery of primary boot "
712 "sector failed: Read-only mount.");
714 brelse(bh_primary);
716 ntfs_warning(sb, "Using backup boot sector.");
717 return bh_backup;
721 * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol
722 * @vol: volume structure to initialise with data from boot sector
723 * @b: boot sector to parse
725 * Parse the ntfs boot sector @b and store all imporant information therein in
726 * the ntfs super block @vol. Return 'true' on success and 'false' on error.
728 static bool parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
730 unsigned int sectors_per_cluster_bits, nr_hidden_sects;
731 int clusters_per_mft_record, clusters_per_index_record;
732 s64 ll;
734 vol->sector_size = le16_to_cpu(b->bpb.bytes_per_sector);
735 vol->sector_size_bits = ffs(vol->sector_size) - 1;
736 ntfs_debug("vol->sector_size = %i (0x%x)", vol->sector_size,
737 vol->sector_size);
738 ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits,
739 vol->sector_size_bits);
740 if (vol->sector_size < vol->sb->s_blocksize) {
741 ntfs_error(vol->sb, "Sector size (%i) is smaller than the "
742 "device block size (%lu). This is not "
743 "supported. Sorry.", vol->sector_size,
744 vol->sb->s_blocksize);
745 return false;
747 ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster);
748 sectors_per_cluster_bits = ffs(b->bpb.sectors_per_cluster) - 1;
749 ntfs_debug("sectors_per_cluster_bits = 0x%x",
750 sectors_per_cluster_bits);
751 nr_hidden_sects = le32_to_cpu(b->bpb.hidden_sectors);
752 ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects);
753 vol->cluster_size = vol->sector_size << sectors_per_cluster_bits;
754 vol->cluster_size_mask = vol->cluster_size - 1;
755 vol->cluster_size_bits = ffs(vol->cluster_size) - 1;
756 ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size,
757 vol->cluster_size);
758 ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask);
759 ntfs_debug("vol->cluster_size_bits = %i", vol->cluster_size_bits);
760 if (vol->cluster_size < vol->sector_size) {
761 ntfs_error(vol->sb, "Cluster size (%i) is smaller than the "
762 "sector size (%i). This is not supported. "
763 "Sorry.", vol->cluster_size, vol->sector_size);
764 return false;
766 clusters_per_mft_record = b->clusters_per_mft_record;
767 ntfs_debug("clusters_per_mft_record = %i (0x%x)",
768 clusters_per_mft_record, clusters_per_mft_record);
769 if (clusters_per_mft_record > 0)
770 vol->mft_record_size = vol->cluster_size <<
771 (ffs(clusters_per_mft_record) - 1);
772 else
774 * When mft_record_size < cluster_size, clusters_per_mft_record
775 * = -log2(mft_record_size) bytes. mft_record_size normaly is
776 * 1024 bytes, which is encoded as 0xF6 (-10 in decimal).
778 vol->mft_record_size = 1 << -clusters_per_mft_record;
779 vol->mft_record_size_mask = vol->mft_record_size - 1;
780 vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1;
781 ntfs_debug("vol->mft_record_size = %i (0x%x)", vol->mft_record_size,
782 vol->mft_record_size);
783 ntfs_debug("vol->mft_record_size_mask = 0x%x",
784 vol->mft_record_size_mask);
785 ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
786 vol->mft_record_size_bits, vol->mft_record_size_bits);
788 * We cannot support mft record sizes above the PAGE_CACHE_SIZE since
789 * we store $MFT/$DATA, the table of mft records in the page cache.
791 if (vol->mft_record_size > PAGE_CACHE_SIZE) {
792 ntfs_error(vol->sb, "Mft record size (%i) exceeds the "
793 "PAGE_CACHE_SIZE on your system (%lu). "
794 "This is not supported. Sorry.",
795 vol->mft_record_size, PAGE_CACHE_SIZE);
796 return false;
798 /* We cannot support mft record sizes below the sector size. */
799 if (vol->mft_record_size < vol->sector_size) {
800 ntfs_error(vol->sb, "Mft record size (%i) is smaller than the "
801 "sector size (%i). This is not supported. "
802 "Sorry.", vol->mft_record_size,
803 vol->sector_size);
804 return false;
806 clusters_per_index_record = b->clusters_per_index_record;
807 ntfs_debug("clusters_per_index_record = %i (0x%x)",
808 clusters_per_index_record, clusters_per_index_record);
809 if (clusters_per_index_record > 0)
810 vol->index_record_size = vol->cluster_size <<
811 (ffs(clusters_per_index_record) - 1);
812 else
814 * When index_record_size < cluster_size,
815 * clusters_per_index_record = -log2(index_record_size) bytes.
816 * index_record_size normaly equals 4096 bytes, which is
817 * encoded as 0xF4 (-12 in decimal).
819 vol->index_record_size = 1 << -clusters_per_index_record;
820 vol->index_record_size_mask = vol->index_record_size - 1;
821 vol->index_record_size_bits = ffs(vol->index_record_size) - 1;
822 ntfs_debug("vol->index_record_size = %i (0x%x)",
823 vol->index_record_size, vol->index_record_size);
824 ntfs_debug("vol->index_record_size_mask = 0x%x",
825 vol->index_record_size_mask);
826 ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
827 vol->index_record_size_bits,
828 vol->index_record_size_bits);
829 /* We cannot support index record sizes below the sector size. */
830 if (vol->index_record_size < vol->sector_size) {
831 ntfs_error(vol->sb, "Index record size (%i) is smaller than "
832 "the sector size (%i). This is not "
833 "supported. Sorry.", vol->index_record_size,
834 vol->sector_size);
835 return false;
838 * Get the size of the volume in clusters and check for 64-bit-ness.
839 * Windows currently only uses 32 bits to save the clusters so we do
840 * the same as it is much faster on 32-bit CPUs.
842 ll = sle64_to_cpu(b->number_of_sectors) >> sectors_per_cluster_bits;
843 if ((u64)ll >= 1ULL << 32) {
844 ntfs_error(vol->sb, "Cannot handle 64-bit clusters. Sorry.");
845 return false;
847 vol->nr_clusters = ll;
848 ntfs_debug("vol->nr_clusters = 0x%llx", (long long)vol->nr_clusters);
850 * On an architecture where unsigned long is 32-bits, we restrict the
851 * volume size to 2TiB (2^41). On a 64-bit architecture, the compiler
852 * will hopefully optimize the whole check away.
854 if (sizeof(unsigned long) < 8) {
855 if ((ll << vol->cluster_size_bits) >= (1ULL << 41)) {
856 ntfs_error(vol->sb, "Volume size (%lluTiB) is too "
857 "large for this architecture. "
858 "Maximum supported is 2TiB. Sorry.",
859 (unsigned long long)ll >> (40 -
860 vol->cluster_size_bits));
861 return false;
864 ll = sle64_to_cpu(b->mft_lcn);
865 if (ll >= vol->nr_clusters) {
866 ntfs_error(vol->sb, "MFT LCN (%lli, 0x%llx) is beyond end of "
867 "volume. Weird.", (unsigned long long)ll,
868 (unsigned long long)ll);
869 return false;
871 vol->mft_lcn = ll;
872 ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol->mft_lcn);
873 ll = sle64_to_cpu(b->mftmirr_lcn);
874 if (ll >= vol->nr_clusters) {
875 ntfs_error(vol->sb, "MFTMirr LCN (%lli, 0x%llx) is beyond end "
876 "of volume. Weird.", (unsigned long long)ll,
877 (unsigned long long)ll);
878 return false;
880 vol->mftmirr_lcn = ll;
881 ntfs_debug("vol->mftmirr_lcn = 0x%llx", (long long)vol->mftmirr_lcn);
882 #ifdef NTFS_RW
884 * Work out the size of the mft mirror in number of mft records. If the
885 * cluster size is less than or equal to the size taken by four mft
886 * records, the mft mirror stores the first four mft records. If the
887 * cluster size is bigger than the size taken by four mft records, the
888 * mft mirror contains as many mft records as will fit into one
889 * cluster.
891 if (vol->cluster_size <= (4 << vol->mft_record_size_bits))
892 vol->mftmirr_size = 4;
893 else
894 vol->mftmirr_size = vol->cluster_size >>
895 vol->mft_record_size_bits;
896 ntfs_debug("vol->mftmirr_size = %i", vol->mftmirr_size);
897 #endif /* NTFS_RW */
898 vol->serial_no = le64_to_cpu(b->volume_serial_number);
899 ntfs_debug("vol->serial_no = 0x%llx",
900 (unsigned long long)vol->serial_no);
901 return true;
905 * ntfs_setup_allocators - initialize the cluster and mft allocators
906 * @vol: volume structure for which to setup the allocators
908 * Setup the cluster (lcn) and mft allocators to the starting values.
910 static void ntfs_setup_allocators(ntfs_volume *vol)
912 #ifdef NTFS_RW
913 LCN mft_zone_size, mft_lcn;
914 #endif /* NTFS_RW */
916 ntfs_debug("vol->mft_zone_multiplier = 0x%x",
917 vol->mft_zone_multiplier);
918 #ifdef NTFS_RW
919 /* Determine the size of the MFT zone. */
920 mft_zone_size = vol->nr_clusters;
921 switch (vol->mft_zone_multiplier) { /* % of volume size in clusters */
922 case 4:
923 mft_zone_size >>= 1; /* 50% */
924 break;
925 case 3:
926 mft_zone_size = (mft_zone_size +
927 (mft_zone_size >> 1)) >> 2; /* 37.5% */
928 break;
929 case 2:
930 mft_zone_size >>= 2; /* 25% */
931 break;
932 /* case 1: */
933 default:
934 mft_zone_size >>= 3; /* 12.5% */
935 break;
937 /* Setup the mft zone. */
938 vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn;
939 ntfs_debug("vol->mft_zone_pos = 0x%llx",
940 (unsigned long long)vol->mft_zone_pos);
942 * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
943 * source) and if the actual mft_lcn is in the expected place or even
944 * further to the front of the volume, extend the mft_zone to cover the
945 * beginning of the volume as well. This is in order to protect the
946 * area reserved for the mft bitmap as well within the mft_zone itself.
947 * On non-standard volumes we do not protect it as the overhead would
948 * be higher than the speed increase we would get by doing it.
950 mft_lcn = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size;
951 if (mft_lcn * vol->cluster_size < 16 * 1024)
952 mft_lcn = (16 * 1024 + vol->cluster_size - 1) /
953 vol->cluster_size;
954 if (vol->mft_zone_start <= mft_lcn)
955 vol->mft_zone_start = 0;
956 ntfs_debug("vol->mft_zone_start = 0x%llx",
957 (unsigned long long)vol->mft_zone_start);
959 * Need to cap the mft zone on non-standard volumes so that it does
960 * not point outside the boundaries of the volume. We do this by
961 * halving the zone size until we are inside the volume.
963 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
964 while (vol->mft_zone_end >= vol->nr_clusters) {
965 mft_zone_size >>= 1;
966 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
968 ntfs_debug("vol->mft_zone_end = 0x%llx",
969 (unsigned long long)vol->mft_zone_end);
971 * Set the current position within each data zone to the start of the
972 * respective zone.
974 vol->data1_zone_pos = vol->mft_zone_end;
975 ntfs_debug("vol->data1_zone_pos = 0x%llx",
976 (unsigned long long)vol->data1_zone_pos);
977 vol->data2_zone_pos = 0;
978 ntfs_debug("vol->data2_zone_pos = 0x%llx",
979 (unsigned long long)vol->data2_zone_pos);
981 /* Set the mft data allocation position to mft record 24. */
982 vol->mft_data_pos = 24;
983 ntfs_debug("vol->mft_data_pos = 0x%llx",
984 (unsigned long long)vol->mft_data_pos);
985 #endif /* NTFS_RW */
988 #ifdef NTFS_RW
991 * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume
992 * @vol: ntfs super block describing device whose mft mirror to load
994 * Return 'true' on success or 'false' on error.
996 static bool load_and_init_mft_mirror(ntfs_volume *vol)
998 struct inode *tmp_ino;
999 ntfs_inode *tmp_ni;
1001 ntfs_debug("Entering.");
1002 /* Get mft mirror inode. */
1003 tmp_ino = ntfs_iget(vol->sb, FILE_MFTMirr);
1004 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1005 if (!IS_ERR(tmp_ino))
1006 iput(tmp_ino);
1007 /* Caller will display error message. */
1008 return false;
1011 * Re-initialize some specifics about $MFTMirr's inode as
1012 * ntfs_read_inode() will have set up the default ones.
1014 /* Set uid and gid to root. */
1015 tmp_ino->i_uid = tmp_ino->i_gid = 0;
1016 /* Regular file. No access for anyone. */
1017 tmp_ino->i_mode = S_IFREG;
1018 /* No VFS initiated operations allowed for $MFTMirr. */
1019 tmp_ino->i_op = &ntfs_empty_inode_ops;
1020 tmp_ino->i_fop = &ntfs_empty_file_ops;
1021 /* Put in our special address space operations. */
1022 tmp_ino->i_mapping->a_ops = &ntfs_mst_aops;
1023 tmp_ni = NTFS_I(tmp_ino);
1024 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
1025 NInoSetMstProtected(tmp_ni);
1026 NInoSetSparseDisabled(tmp_ni);
1028 * Set up our little cheat allowing us to reuse the async read io
1029 * completion handler for directories.
1031 tmp_ni->itype.index.block_size = vol->mft_record_size;
1032 tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits;
1033 vol->mftmirr_ino = tmp_ino;
1034 ntfs_debug("Done.");
1035 return true;
1039 * check_mft_mirror - compare contents of the mft mirror with the mft
1040 * @vol: ntfs super block describing device whose mft mirror to check
1042 * Return 'true' on success or 'false' on error.
1044 * Note, this function also results in the mft mirror runlist being completely
1045 * mapped into memory. The mft mirror write code requires this and will BUG()
1046 * should it find an unmapped runlist element.
1048 static bool check_mft_mirror(ntfs_volume *vol)
1050 struct super_block *sb = vol->sb;
1051 ntfs_inode *mirr_ni;
1052 struct page *mft_page, *mirr_page;
1053 u8 *kmft, *kmirr;
1054 runlist_element *rl, rl2[2];
1055 pgoff_t index;
1056 int mrecs_per_page, i;
1058 ntfs_debug("Entering.");
1059 /* Compare contents of $MFT and $MFTMirr. */
1060 mrecs_per_page = PAGE_CACHE_SIZE / vol->mft_record_size;
1061 BUG_ON(!mrecs_per_page);
1062 BUG_ON(!vol->mftmirr_size);
1063 mft_page = mirr_page = NULL;
1064 kmft = kmirr = NULL;
1065 index = i = 0;
1066 do {
1067 u32 bytes;
1069 /* Switch pages if necessary. */
1070 if (!(i % mrecs_per_page)) {
1071 if (index) {
1072 ntfs_unmap_page(mft_page);
1073 ntfs_unmap_page(mirr_page);
1075 /* Get the $MFT page. */
1076 mft_page = ntfs_map_page(vol->mft_ino->i_mapping,
1077 index);
1078 if (IS_ERR(mft_page)) {
1079 ntfs_error(sb, "Failed to read $MFT.");
1080 return false;
1082 kmft = page_address(mft_page);
1083 /* Get the $MFTMirr page. */
1084 mirr_page = ntfs_map_page(vol->mftmirr_ino->i_mapping,
1085 index);
1086 if (IS_ERR(mirr_page)) {
1087 ntfs_error(sb, "Failed to read $MFTMirr.");
1088 goto mft_unmap_out;
1090 kmirr = page_address(mirr_page);
1091 ++index;
1093 /* Do not check the record if it is not in use. */
1094 if (((MFT_RECORD*)kmft)->flags & MFT_RECORD_IN_USE) {
1095 /* Make sure the record is ok. */
1096 if (ntfs_is_baad_recordp((le32*)kmft)) {
1097 ntfs_error(sb, "Incomplete multi sector "
1098 "transfer detected in mft "
1099 "record %i.", i);
1100 mm_unmap_out:
1101 ntfs_unmap_page(mirr_page);
1102 mft_unmap_out:
1103 ntfs_unmap_page(mft_page);
1104 return false;
1107 /* Do not check the mirror record if it is not in use. */
1108 if (((MFT_RECORD*)kmirr)->flags & MFT_RECORD_IN_USE) {
1109 if (ntfs_is_baad_recordp((le32*)kmirr)) {
1110 ntfs_error(sb, "Incomplete multi sector "
1111 "transfer detected in mft "
1112 "mirror record %i.", i);
1113 goto mm_unmap_out;
1116 /* Get the amount of data in the current record. */
1117 bytes = le32_to_cpu(((MFT_RECORD*)kmft)->bytes_in_use);
1118 if (bytes < sizeof(MFT_RECORD_OLD) ||
1119 bytes > vol->mft_record_size ||
1120 ntfs_is_baad_recordp((le32*)kmft)) {
1121 bytes = le32_to_cpu(((MFT_RECORD*)kmirr)->bytes_in_use);
1122 if (bytes < sizeof(MFT_RECORD_OLD) ||
1123 bytes > vol->mft_record_size ||
1124 ntfs_is_baad_recordp((le32*)kmirr))
1125 bytes = vol->mft_record_size;
1127 /* Compare the two records. */
1128 if (memcmp(kmft, kmirr, bytes)) {
1129 ntfs_error(sb, "$MFT and $MFTMirr (record %i) do not "
1130 "match. Run ntfsfix or chkdsk.", i);
1131 goto mm_unmap_out;
1133 kmft += vol->mft_record_size;
1134 kmirr += vol->mft_record_size;
1135 } while (++i < vol->mftmirr_size);
1136 /* Release the last pages. */
1137 ntfs_unmap_page(mft_page);
1138 ntfs_unmap_page(mirr_page);
1140 /* Construct the mft mirror runlist by hand. */
1141 rl2[0].vcn = 0;
1142 rl2[0].lcn = vol->mftmirr_lcn;
1143 rl2[0].length = (vol->mftmirr_size * vol->mft_record_size +
1144 vol->cluster_size - 1) / vol->cluster_size;
1145 rl2[1].vcn = rl2[0].length;
1146 rl2[1].lcn = LCN_ENOENT;
1147 rl2[1].length = 0;
1149 * Because we have just read all of the mft mirror, we know we have
1150 * mapped the full runlist for it.
1152 mirr_ni = NTFS_I(vol->mftmirr_ino);
1153 down_read(&mirr_ni->runlist.lock);
1154 rl = mirr_ni->runlist.rl;
1155 /* Compare the two runlists. They must be identical. */
1156 i = 0;
1157 do {
1158 if (rl2[i].vcn != rl[i].vcn || rl2[i].lcn != rl[i].lcn ||
1159 rl2[i].length != rl[i].length) {
1160 ntfs_error(sb, "$MFTMirr location mismatch. "
1161 "Run chkdsk.");
1162 up_read(&mirr_ni->runlist.lock);
1163 return false;
1165 } while (rl2[i++].length);
1166 up_read(&mirr_ni->runlist.lock);
1167 ntfs_debug("Done.");
1168 return true;
1172 * load_and_check_logfile - load and check the logfile inode for a volume
1173 * @vol: ntfs super block describing device whose logfile to load
1175 * Return 'true' on success or 'false' on error.
1177 static bool load_and_check_logfile(ntfs_volume *vol,
1178 RESTART_PAGE_HEADER **rp)
1180 struct inode *tmp_ino;
1182 ntfs_debug("Entering.");
1183 tmp_ino = ntfs_iget(vol->sb, FILE_LogFile);
1184 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1185 if (!IS_ERR(tmp_ino))
1186 iput(tmp_ino);
1187 /* Caller will display error message. */
1188 return false;
1190 if (!ntfs_check_logfile(tmp_ino, rp)) {
1191 iput(tmp_ino);
1192 /* ntfs_check_logfile() will have displayed error output. */
1193 return false;
1195 NInoSetSparseDisabled(NTFS_I(tmp_ino));
1196 vol->logfile_ino = tmp_ino;
1197 ntfs_debug("Done.");
1198 return true;
1201 #define NTFS_HIBERFIL_HEADER_SIZE 4096
1204 * check_windows_hibernation_status - check if Windows is suspended on a volume
1205 * @vol: ntfs super block of device to check
1207 * Check if Windows is hibernated on the ntfs volume @vol. This is done by
1208 * looking for the file hiberfil.sys in the root directory of the volume. If
1209 * the file is not present Windows is definitely not suspended.
1211 * If hiberfil.sys exists and is less than 4kiB in size it means Windows is
1212 * definitely suspended (this volume is not the system volume). Caveat: on a
1213 * system with many volumes it is possible that the < 4kiB check is bogus but
1214 * for now this should do fine.
1216 * If hiberfil.sys exists and is larger than 4kiB in size, we need to read the
1217 * hiberfil header (which is the first 4kiB). If this begins with "hibr",
1218 * Windows is definitely suspended. If it is completely full of zeroes,
1219 * Windows is definitely not hibernated. Any other case is treated as if
1220 * Windows is suspended. This caters for the above mentioned caveat of a
1221 * system with many volumes where no "hibr" magic would be present and there is
1222 * no zero header.
1224 * Return 0 if Windows is not hibernated on the volume, >0 if Windows is
1225 * hibernated on the volume, and -errno on error.
1227 static int check_windows_hibernation_status(ntfs_volume *vol)
1229 MFT_REF mref;
1230 struct inode *vi;
1231 ntfs_inode *ni;
1232 struct page *page;
1233 u32 *kaddr, *kend;
1234 ntfs_name *name = NULL;
1235 int ret = 1;
1236 static const ntfschar hiberfil[13] = { cpu_to_le16('h'),
1237 cpu_to_le16('i'), cpu_to_le16('b'),
1238 cpu_to_le16('e'), cpu_to_le16('r'),
1239 cpu_to_le16('f'), cpu_to_le16('i'),
1240 cpu_to_le16('l'), cpu_to_le16('.'),
1241 cpu_to_le16('s'), cpu_to_le16('y'),
1242 cpu_to_le16('s'), 0 };
1244 ntfs_debug("Entering.");
1246 * Find the inode number for the hibernation file by looking up the
1247 * filename hiberfil.sys in the root directory.
1249 mutex_lock(&vol->root_ino->i_mutex);
1250 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->root_ino), hiberfil, 12,
1251 &name);
1252 mutex_unlock(&vol->root_ino->i_mutex);
1253 if (IS_ERR_MREF(mref)) {
1254 ret = MREF_ERR(mref);
1255 /* If the file does not exist, Windows is not hibernated. */
1256 if (ret == -ENOENT) {
1257 ntfs_debug("hiberfil.sys not present. Windows is not "
1258 "hibernated on the volume.");
1259 return 0;
1261 /* A real error occured. */
1262 ntfs_error(vol->sb, "Failed to find inode number for "
1263 "hiberfil.sys.");
1264 return ret;
1266 /* We do not care for the type of match that was found. */
1267 kfree(name);
1268 /* Get the inode. */
1269 vi = ntfs_iget(vol->sb, MREF(mref));
1270 if (IS_ERR(vi) || is_bad_inode(vi)) {
1271 if (!IS_ERR(vi))
1272 iput(vi);
1273 ntfs_error(vol->sb, "Failed to load hiberfil.sys.");
1274 return IS_ERR(vi) ? PTR_ERR(vi) : -EIO;
1276 if (unlikely(i_size_read(vi) < NTFS_HIBERFIL_HEADER_SIZE)) {
1277 ntfs_debug("hiberfil.sys is smaller than 4kiB (0x%llx). "
1278 "Windows is hibernated on the volume. This "
1279 "is not the system volume.", i_size_read(vi));
1280 goto iput_out;
1282 ni = NTFS_I(vi);
1283 page = ntfs_map_page(vi->i_mapping, 0);
1284 if (IS_ERR(page)) {
1285 ntfs_error(vol->sb, "Failed to read from hiberfil.sys.");
1286 ret = PTR_ERR(page);
1287 goto iput_out;
1289 kaddr = (u32*)page_address(page);
1290 if (*(le32*)kaddr == cpu_to_le32(0x72626968)/*'hibr'*/) {
1291 ntfs_debug("Magic \"hibr\" found in hiberfil.sys. Windows is "
1292 "hibernated on the volume. This is the "
1293 "system volume.");
1294 goto unm_iput_out;
1296 kend = kaddr + NTFS_HIBERFIL_HEADER_SIZE/sizeof(*kaddr);
1297 do {
1298 if (unlikely(*kaddr)) {
1299 ntfs_debug("hiberfil.sys is larger than 4kiB "
1300 "(0x%llx), does not contain the "
1301 "\"hibr\" magic, and does not have a "
1302 "zero header. Windows is hibernated "
1303 "on the volume. This is not the "
1304 "system volume.", i_size_read(vi));
1305 goto unm_iput_out;
1307 } while (++kaddr < kend);
1308 ntfs_debug("hiberfil.sys contains a zero header. Windows is not "
1309 "hibernated on the volume. This is the system "
1310 "volume.");
1311 ret = 0;
1312 unm_iput_out:
1313 ntfs_unmap_page(page);
1314 iput_out:
1315 iput(vi);
1316 return ret;
1320 * load_and_init_quota - load and setup the quota file for a volume if present
1321 * @vol: ntfs super block describing device whose quota file to load
1323 * Return 'true' on success or 'false' on error. If $Quota is not present, we
1324 * leave vol->quota_ino as NULL and return success.
1326 static bool load_and_init_quota(ntfs_volume *vol)
1328 MFT_REF mref;
1329 struct inode *tmp_ino;
1330 ntfs_name *name = NULL;
1331 static const ntfschar Quota[7] = { cpu_to_le16('$'),
1332 cpu_to_le16('Q'), cpu_to_le16('u'),
1333 cpu_to_le16('o'), cpu_to_le16('t'),
1334 cpu_to_le16('a'), 0 };
1335 static ntfschar Q[3] = { cpu_to_le16('$'),
1336 cpu_to_le16('Q'), 0 };
1338 ntfs_debug("Entering.");
1340 * Find the inode number for the quota file by looking up the filename
1341 * $Quota in the extended system files directory $Extend.
1343 mutex_lock(&vol->extend_ino->i_mutex);
1344 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), Quota, 6,
1345 &name);
1346 mutex_unlock(&vol->extend_ino->i_mutex);
1347 if (IS_ERR_MREF(mref)) {
1349 * If the file does not exist, quotas are disabled and have
1350 * never been enabled on this volume, just return success.
1352 if (MREF_ERR(mref) == -ENOENT) {
1353 ntfs_debug("$Quota not present. Volume does not have "
1354 "quotas enabled.");
1356 * No need to try to set quotas out of date if they are
1357 * not enabled.
1359 NVolSetQuotaOutOfDate(vol);
1360 return true;
1362 /* A real error occured. */
1363 ntfs_error(vol->sb, "Failed to find inode number for $Quota.");
1364 return false;
1366 /* We do not care for the type of match that was found. */
1367 kfree(name);
1368 /* Get the inode. */
1369 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1370 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1371 if (!IS_ERR(tmp_ino))
1372 iput(tmp_ino);
1373 ntfs_error(vol->sb, "Failed to load $Quota.");
1374 return false;
1376 vol->quota_ino = tmp_ino;
1377 /* Get the $Q index allocation attribute. */
1378 tmp_ino = ntfs_index_iget(vol->quota_ino, Q, 2);
1379 if (IS_ERR(tmp_ino)) {
1380 ntfs_error(vol->sb, "Failed to load $Quota/$Q index.");
1381 return false;
1383 vol->quota_q_ino = tmp_ino;
1384 ntfs_debug("Done.");
1385 return true;
1389 * load_and_init_usnjrnl - load and setup the transaction log if present
1390 * @vol: ntfs super block describing device whose usnjrnl file to load
1392 * Return 'true' on success or 'false' on error.
1394 * If $UsnJrnl is not present or in the process of being disabled, we set
1395 * NVolUsnJrnlStamped() and return success.
1397 * If the $UsnJrnl $DATA/$J attribute has a size equal to the lowest valid usn,
1398 * i.e. transaction logging has only just been enabled or the journal has been
1399 * stamped and nothing has been logged since, we also set NVolUsnJrnlStamped()
1400 * and return success.
1402 static bool load_and_init_usnjrnl(ntfs_volume *vol)
1404 MFT_REF mref;
1405 struct inode *tmp_ino;
1406 ntfs_inode *tmp_ni;
1407 struct page *page;
1408 ntfs_name *name = NULL;
1409 USN_HEADER *uh;
1410 static const ntfschar UsnJrnl[9] = { cpu_to_le16('$'),
1411 cpu_to_le16('U'), cpu_to_le16('s'),
1412 cpu_to_le16('n'), cpu_to_le16('J'),
1413 cpu_to_le16('r'), cpu_to_le16('n'),
1414 cpu_to_le16('l'), 0 };
1415 static ntfschar Max[5] = { cpu_to_le16('$'),
1416 cpu_to_le16('M'), cpu_to_le16('a'),
1417 cpu_to_le16('x'), 0 };
1418 static ntfschar J[3] = { cpu_to_le16('$'),
1419 cpu_to_le16('J'), 0 };
1421 ntfs_debug("Entering.");
1423 * Find the inode number for the transaction log file by looking up the
1424 * filename $UsnJrnl in the extended system files directory $Extend.
1426 mutex_lock(&vol->extend_ino->i_mutex);
1427 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), UsnJrnl, 8,
1428 &name);
1429 mutex_unlock(&vol->extend_ino->i_mutex);
1430 if (IS_ERR_MREF(mref)) {
1432 * If the file does not exist, transaction logging is disabled,
1433 * just return success.
1435 if (MREF_ERR(mref) == -ENOENT) {
1436 ntfs_debug("$UsnJrnl not present. Volume does not "
1437 "have transaction logging enabled.");
1438 not_enabled:
1440 * No need to try to stamp the transaction log if
1441 * transaction logging is not enabled.
1443 NVolSetUsnJrnlStamped(vol);
1444 return true;
1446 /* A real error occured. */
1447 ntfs_error(vol->sb, "Failed to find inode number for "
1448 "$UsnJrnl.");
1449 return false;
1451 /* We do not care for the type of match that was found. */
1452 kfree(name);
1453 /* Get the inode. */
1454 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1455 if (unlikely(IS_ERR(tmp_ino) || is_bad_inode(tmp_ino))) {
1456 if (!IS_ERR(tmp_ino))
1457 iput(tmp_ino);
1458 ntfs_error(vol->sb, "Failed to load $UsnJrnl.");
1459 return false;
1461 vol->usnjrnl_ino = tmp_ino;
1463 * If the transaction log is in the process of being deleted, we can
1464 * ignore it.
1466 if (unlikely(vol->vol_flags & VOLUME_DELETE_USN_UNDERWAY)) {
1467 ntfs_debug("$UsnJrnl in the process of being disabled. "
1468 "Volume does not have transaction logging "
1469 "enabled.");
1470 goto not_enabled;
1472 /* Get the $DATA/$Max attribute. */
1473 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, Max, 4);
1474 if (IS_ERR(tmp_ino)) {
1475 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$Max "
1476 "attribute.");
1477 return false;
1479 vol->usnjrnl_max_ino = tmp_ino;
1480 if (unlikely(i_size_read(tmp_ino) < sizeof(USN_HEADER))) {
1481 ntfs_error(vol->sb, "Found corrupt $UsnJrnl/$DATA/$Max "
1482 "attribute (size is 0x%llx but should be at "
1483 "least 0x%zx bytes).", i_size_read(tmp_ino),
1484 sizeof(USN_HEADER));
1485 return false;
1487 /* Get the $DATA/$J attribute. */
1488 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, J, 2);
1489 if (IS_ERR(tmp_ino)) {
1490 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$J "
1491 "attribute.");
1492 return false;
1494 vol->usnjrnl_j_ino = tmp_ino;
1495 /* Verify $J is non-resident and sparse. */
1496 tmp_ni = NTFS_I(vol->usnjrnl_j_ino);
1497 if (unlikely(!NInoNonResident(tmp_ni) || !NInoSparse(tmp_ni))) {
1498 ntfs_error(vol->sb, "$UsnJrnl/$DATA/$J attribute is resident "
1499 "and/or not sparse.");
1500 return false;
1502 /* Read the USN_HEADER from $DATA/$Max. */
1503 page = ntfs_map_page(vol->usnjrnl_max_ino->i_mapping, 0);
1504 if (IS_ERR(page)) {
1505 ntfs_error(vol->sb, "Failed to read from $UsnJrnl/$DATA/$Max "
1506 "attribute.");
1507 return false;
1509 uh = (USN_HEADER*)page_address(page);
1510 /* Sanity check the $Max. */
1511 if (unlikely(sle64_to_cpu(uh->allocation_delta) >
1512 sle64_to_cpu(uh->maximum_size))) {
1513 ntfs_error(vol->sb, "Allocation delta (0x%llx) exceeds "
1514 "maximum size (0x%llx). $UsnJrnl is corrupt.",
1515 (long long)sle64_to_cpu(uh->allocation_delta),
1516 (long long)sle64_to_cpu(uh->maximum_size));
1517 ntfs_unmap_page(page);
1518 return false;
1521 * If the transaction log has been stamped and nothing has been written
1522 * to it since, we do not need to stamp it.
1524 if (unlikely(sle64_to_cpu(uh->lowest_valid_usn) >=
1525 i_size_read(vol->usnjrnl_j_ino))) {
1526 if (likely(sle64_to_cpu(uh->lowest_valid_usn) ==
1527 i_size_read(vol->usnjrnl_j_ino))) {
1528 ntfs_unmap_page(page);
1529 ntfs_debug("$UsnJrnl is enabled but nothing has been "
1530 "logged since it was last stamped. "
1531 "Treating this as if the volume does "
1532 "not have transaction logging "
1533 "enabled.");
1534 goto not_enabled;
1536 ntfs_error(vol->sb, "$UsnJrnl has lowest valid usn (0x%llx) "
1537 "which is out of bounds (0x%llx). $UsnJrnl "
1538 "is corrupt.",
1539 (long long)sle64_to_cpu(uh->lowest_valid_usn),
1540 i_size_read(vol->usnjrnl_j_ino));
1541 ntfs_unmap_page(page);
1542 return false;
1544 ntfs_unmap_page(page);
1545 ntfs_debug("Done.");
1546 return true;
1550 * load_and_init_attrdef - load the attribute definitions table for a volume
1551 * @vol: ntfs super block describing device whose attrdef to load
1553 * Return 'true' on success or 'false' on error.
1555 static bool load_and_init_attrdef(ntfs_volume *vol)
1557 loff_t i_size;
1558 struct super_block *sb = vol->sb;
1559 struct inode *ino;
1560 struct page *page;
1561 pgoff_t index, max_index;
1562 unsigned int size;
1564 ntfs_debug("Entering.");
1565 /* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */
1566 ino = ntfs_iget(sb, FILE_AttrDef);
1567 if (IS_ERR(ino) || is_bad_inode(ino)) {
1568 if (!IS_ERR(ino))
1569 iput(ino);
1570 goto failed;
1572 NInoSetSparseDisabled(NTFS_I(ino));
1573 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
1574 i_size = i_size_read(ino);
1575 if (i_size <= 0 || i_size > 0x7fffffff)
1576 goto iput_failed;
1577 vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(i_size);
1578 if (!vol->attrdef)
1579 goto iput_failed;
1580 index = 0;
1581 max_index = i_size >> PAGE_CACHE_SHIFT;
1582 size = PAGE_CACHE_SIZE;
1583 while (index < max_index) {
1584 /* Read the attrdef table and copy it into the linear buffer. */
1585 read_partial_attrdef_page:
1586 page = ntfs_map_page(ino->i_mapping, index);
1587 if (IS_ERR(page))
1588 goto free_iput_failed;
1589 memcpy((u8*)vol->attrdef + (index++ << PAGE_CACHE_SHIFT),
1590 page_address(page), size);
1591 ntfs_unmap_page(page);
1593 if (size == PAGE_CACHE_SIZE) {
1594 size = i_size & ~PAGE_CACHE_MASK;
1595 if (size)
1596 goto read_partial_attrdef_page;
1598 vol->attrdef_size = i_size;
1599 ntfs_debug("Read %llu bytes from $AttrDef.", i_size);
1600 iput(ino);
1601 return true;
1602 free_iput_failed:
1603 ntfs_free(vol->attrdef);
1604 vol->attrdef = NULL;
1605 iput_failed:
1606 iput(ino);
1607 failed:
1608 ntfs_error(sb, "Failed to initialize attribute definition table.");
1609 return false;
1612 #endif /* NTFS_RW */
1615 * load_and_init_upcase - load the upcase table for an ntfs volume
1616 * @vol: ntfs super block describing device whose upcase to load
1618 * Return 'true' on success or 'false' on error.
1620 static bool load_and_init_upcase(ntfs_volume *vol)
1622 loff_t i_size;
1623 struct super_block *sb = vol->sb;
1624 struct inode *ino;
1625 struct page *page;
1626 pgoff_t index, max_index;
1627 unsigned int size;
1628 int i, max;
1630 ntfs_debug("Entering.");
1631 /* Read upcase table and setup vol->upcase and vol->upcase_len. */
1632 ino = ntfs_iget(sb, FILE_UpCase);
1633 if (IS_ERR(ino) || is_bad_inode(ino)) {
1634 if (!IS_ERR(ino))
1635 iput(ino);
1636 goto upcase_failed;
1639 * The upcase size must not be above 64k Unicode characters, must not
1640 * be zero and must be a multiple of sizeof(ntfschar).
1642 i_size = i_size_read(ino);
1643 if (!i_size || i_size & (sizeof(ntfschar) - 1) ||
1644 i_size > 64ULL * 1024 * sizeof(ntfschar))
1645 goto iput_upcase_failed;
1646 vol->upcase = (ntfschar*)ntfs_malloc_nofs(i_size);
1647 if (!vol->upcase)
1648 goto iput_upcase_failed;
1649 index = 0;
1650 max_index = i_size >> PAGE_CACHE_SHIFT;
1651 size = PAGE_CACHE_SIZE;
1652 while (index < max_index) {
1653 /* Read the upcase table and copy it into the linear buffer. */
1654 read_partial_upcase_page:
1655 page = ntfs_map_page(ino->i_mapping, index);
1656 if (IS_ERR(page))
1657 goto iput_upcase_failed;
1658 memcpy((char*)vol->upcase + (index++ << PAGE_CACHE_SHIFT),
1659 page_address(page), size);
1660 ntfs_unmap_page(page);
1662 if (size == PAGE_CACHE_SIZE) {
1663 size = i_size & ~PAGE_CACHE_MASK;
1664 if (size)
1665 goto read_partial_upcase_page;
1667 vol->upcase_len = i_size >> UCHAR_T_SIZE_BITS;
1668 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
1669 i_size, 64 * 1024 * sizeof(ntfschar));
1670 iput(ino);
1671 mutex_lock(&ntfs_lock);
1672 if (!default_upcase) {
1673 ntfs_debug("Using volume specified $UpCase since default is "
1674 "not present.");
1675 mutex_unlock(&ntfs_lock);
1676 return true;
1678 max = default_upcase_len;
1679 if (max > vol->upcase_len)
1680 max = vol->upcase_len;
1681 for (i = 0; i < max; i++)
1682 if (vol->upcase[i] != default_upcase[i])
1683 break;
1684 if (i == max) {
1685 ntfs_free(vol->upcase);
1686 vol->upcase = default_upcase;
1687 vol->upcase_len = max;
1688 ntfs_nr_upcase_users++;
1689 mutex_unlock(&ntfs_lock);
1690 ntfs_debug("Volume specified $UpCase matches default. Using "
1691 "default.");
1692 return true;
1694 mutex_unlock(&ntfs_lock);
1695 ntfs_debug("Using volume specified $UpCase since it does not match "
1696 "the default.");
1697 return true;
1698 iput_upcase_failed:
1699 iput(ino);
1700 ntfs_free(vol->upcase);
1701 vol->upcase = NULL;
1702 upcase_failed:
1703 mutex_lock(&ntfs_lock);
1704 if (default_upcase) {
1705 vol->upcase = default_upcase;
1706 vol->upcase_len = default_upcase_len;
1707 ntfs_nr_upcase_users++;
1708 mutex_unlock(&ntfs_lock);
1709 ntfs_error(sb, "Failed to load $UpCase from the volume. Using "
1710 "default.");
1711 return true;
1713 mutex_unlock(&ntfs_lock);
1714 ntfs_error(sb, "Failed to initialize upcase table.");
1715 return false;
1719 * The lcn and mft bitmap inodes are NTFS-internal inodes with
1720 * their own special locking rules:
1722 static struct lock_class_key
1723 lcnbmp_runlist_lock_key, lcnbmp_mrec_lock_key,
1724 mftbmp_runlist_lock_key, mftbmp_mrec_lock_key;
1727 * load_system_files - open the system files using normal functions
1728 * @vol: ntfs super block describing device whose system files to load
1730 * Open the system files with normal access functions and complete setting up
1731 * the ntfs super block @vol.
1733 * Return 'true' on success or 'false' on error.
1735 static bool load_system_files(ntfs_volume *vol)
1737 struct super_block *sb = vol->sb;
1738 MFT_RECORD *m;
1739 VOLUME_INFORMATION *vi;
1740 ntfs_attr_search_ctx *ctx;
1741 #ifdef NTFS_RW
1742 RESTART_PAGE_HEADER *rp;
1743 int err;
1744 #endif /* NTFS_RW */
1746 ntfs_debug("Entering.");
1747 #ifdef NTFS_RW
1748 /* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */
1749 if (!load_and_init_mft_mirror(vol) || !check_mft_mirror(vol)) {
1750 static const char *es1 = "Failed to load $MFTMirr";
1751 static const char *es2 = "$MFTMirr does not match $MFT";
1752 static const char *es3 = ". Run ntfsfix and/or chkdsk.";
1754 /* If a read-write mount, convert it to a read-only mount. */
1755 if (!(sb->s_flags & MS_RDONLY)) {
1756 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1757 ON_ERRORS_CONTINUE))) {
1758 ntfs_error(sb, "%s and neither on_errors="
1759 "continue nor on_errors="
1760 "remount-ro was specified%s",
1761 !vol->mftmirr_ino ? es1 : es2,
1762 es3);
1763 goto iput_mirr_err_out;
1765 sb->s_flags |= MS_RDONLY;
1766 ntfs_error(sb, "%s. Mounting read-only%s",
1767 !vol->mftmirr_ino ? es1 : es2, es3);
1768 } else
1769 ntfs_warning(sb, "%s. Will not be able to remount "
1770 "read-write%s",
1771 !vol->mftmirr_ino ? es1 : es2, es3);
1772 /* This will prevent a read-write remount. */
1773 NVolSetErrors(vol);
1775 #endif /* NTFS_RW */
1776 /* Get mft bitmap attribute inode. */
1777 vol->mftbmp_ino = ntfs_attr_iget(vol->mft_ino, AT_BITMAP, NULL, 0);
1778 if (IS_ERR(vol->mftbmp_ino)) {
1779 ntfs_error(sb, "Failed to load $MFT/$BITMAP attribute.");
1780 goto iput_mirr_err_out;
1782 lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->runlist.lock,
1783 &mftbmp_runlist_lock_key);
1784 lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->mrec_lock,
1785 &mftbmp_mrec_lock_key);
1786 /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */
1787 if (!load_and_init_upcase(vol))
1788 goto iput_mftbmp_err_out;
1789 #ifdef NTFS_RW
1791 * Read attribute definitions table and setup @vol->attrdef and
1792 * @vol->attrdef_size.
1794 if (!load_and_init_attrdef(vol))
1795 goto iput_upcase_err_out;
1796 #endif /* NTFS_RW */
1798 * Get the cluster allocation bitmap inode and verify the size, no
1799 * need for any locking at this stage as we are already running
1800 * exclusively as we are mount in progress task.
1802 vol->lcnbmp_ino = ntfs_iget(sb, FILE_Bitmap);
1803 if (IS_ERR(vol->lcnbmp_ino) || is_bad_inode(vol->lcnbmp_ino)) {
1804 if (!IS_ERR(vol->lcnbmp_ino))
1805 iput(vol->lcnbmp_ino);
1806 goto bitmap_failed;
1808 lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->runlist.lock,
1809 &lcnbmp_runlist_lock_key);
1810 lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->mrec_lock,
1811 &lcnbmp_mrec_lock_key);
1813 NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino));
1814 if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) {
1815 iput(vol->lcnbmp_ino);
1816 bitmap_failed:
1817 ntfs_error(sb, "Failed to load $Bitmap.");
1818 goto iput_attrdef_err_out;
1821 * Get the volume inode and setup our cache of the volume flags and
1822 * version.
1824 vol->vol_ino = ntfs_iget(sb, FILE_Volume);
1825 if (IS_ERR(vol->vol_ino) || is_bad_inode(vol->vol_ino)) {
1826 if (!IS_ERR(vol->vol_ino))
1827 iput(vol->vol_ino);
1828 volume_failed:
1829 ntfs_error(sb, "Failed to load $Volume.");
1830 goto iput_lcnbmp_err_out;
1832 m = map_mft_record(NTFS_I(vol->vol_ino));
1833 if (IS_ERR(m)) {
1834 iput_volume_failed:
1835 iput(vol->vol_ino);
1836 goto volume_failed;
1838 if (!(ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m))) {
1839 ntfs_error(sb, "Failed to get attribute search context.");
1840 goto get_ctx_vol_failed;
1842 if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
1843 ctx) || ctx->attr->non_resident || ctx->attr->flags) {
1844 err_put_vol:
1845 ntfs_attr_put_search_ctx(ctx);
1846 get_ctx_vol_failed:
1847 unmap_mft_record(NTFS_I(vol->vol_ino));
1848 goto iput_volume_failed;
1850 vi = (VOLUME_INFORMATION*)((char*)ctx->attr +
1851 le16_to_cpu(ctx->attr->data.resident.value_offset));
1852 /* Some bounds checks. */
1853 if ((u8*)vi < (u8*)ctx->attr || (u8*)vi +
1854 le32_to_cpu(ctx->attr->data.resident.value_length) >
1855 (u8*)ctx->attr + le32_to_cpu(ctx->attr->length))
1856 goto err_put_vol;
1857 /* Copy the volume flags and version to the ntfs_volume structure. */
1858 vol->vol_flags = vi->flags;
1859 vol->major_ver = vi->major_ver;
1860 vol->minor_ver = vi->minor_ver;
1861 ntfs_attr_put_search_ctx(ctx);
1862 unmap_mft_record(NTFS_I(vol->vol_ino));
1863 printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver,
1864 vol->minor_ver);
1865 if (vol->major_ver < 3 && NVolSparseEnabled(vol)) {
1866 ntfs_warning(vol->sb, "Disabling sparse support due to NTFS "
1867 "volume version %i.%i (need at least version "
1868 "3.0).", vol->major_ver, vol->minor_ver);
1869 NVolClearSparseEnabled(vol);
1871 #ifdef NTFS_RW
1872 /* Make sure that no unsupported volume flags are set. */
1873 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
1874 static const char *es1a = "Volume is dirty";
1875 static const char *es1b = "Volume has been modified by chkdsk";
1876 static const char *es1c = "Volume has unsupported flags set";
1877 static const char *es2a = ". Run chkdsk and mount in Windows.";
1878 static const char *es2b = ". Mount in Windows.";
1879 const char *es1, *es2;
1881 es2 = es2a;
1882 if (vol->vol_flags & VOLUME_IS_DIRTY)
1883 es1 = es1a;
1884 else if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
1885 es1 = es1b;
1886 es2 = es2b;
1887 } else {
1888 es1 = es1c;
1889 ntfs_warning(sb, "Unsupported volume flags 0x%x "
1890 "encountered.",
1891 (unsigned)le16_to_cpu(vol->vol_flags));
1893 /* If a read-write mount, convert it to a read-only mount. */
1894 if (!(sb->s_flags & MS_RDONLY)) {
1895 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1896 ON_ERRORS_CONTINUE))) {
1897 ntfs_error(sb, "%s and neither on_errors="
1898 "continue nor on_errors="
1899 "remount-ro was specified%s",
1900 es1, es2);
1901 goto iput_vol_err_out;
1903 sb->s_flags |= MS_RDONLY;
1904 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1905 } else
1906 ntfs_warning(sb, "%s. Will not be able to remount "
1907 "read-write%s", es1, es2);
1909 * Do not set NVolErrors() because ntfs_remount() re-checks the
1910 * flags which we need to do in case any flags have changed.
1914 * Get the inode for the logfile, check it and determine if the volume
1915 * was shutdown cleanly.
1917 rp = NULL;
1918 if (!load_and_check_logfile(vol, &rp) ||
1919 !ntfs_is_logfile_clean(vol->logfile_ino, rp)) {
1920 static const char *es1a = "Failed to load $LogFile";
1921 static const char *es1b = "$LogFile is not clean";
1922 static const char *es2 = ". Mount in Windows.";
1923 const char *es1;
1925 es1 = !vol->logfile_ino ? es1a : es1b;
1926 /* If a read-write mount, convert it to a read-only mount. */
1927 if (!(sb->s_flags & MS_RDONLY)) {
1928 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1929 ON_ERRORS_CONTINUE))) {
1930 ntfs_error(sb, "%s and neither on_errors="
1931 "continue nor on_errors="
1932 "remount-ro was specified%s",
1933 es1, es2);
1934 if (vol->logfile_ino) {
1935 BUG_ON(!rp);
1936 ntfs_free(rp);
1938 goto iput_logfile_err_out;
1940 sb->s_flags |= MS_RDONLY;
1941 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1942 } else
1943 ntfs_warning(sb, "%s. Will not be able to remount "
1944 "read-write%s", es1, es2);
1945 /* This will prevent a read-write remount. */
1946 NVolSetErrors(vol);
1948 ntfs_free(rp);
1949 #endif /* NTFS_RW */
1950 /* Get the root directory inode so we can do path lookups. */
1951 vol->root_ino = ntfs_iget(sb, FILE_root);
1952 if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) {
1953 if (!IS_ERR(vol->root_ino))
1954 iput(vol->root_ino);
1955 ntfs_error(sb, "Failed to load root directory.");
1956 goto iput_logfile_err_out;
1958 #ifdef NTFS_RW
1960 * Check if Windows is suspended to disk on the target volume. If it
1961 * is hibernated, we must not write *anything* to the disk so set
1962 * NVolErrors() without setting the dirty volume flag and mount
1963 * read-only. This will prevent read-write remounting and it will also
1964 * prevent all writes.
1966 err = check_windows_hibernation_status(vol);
1967 if (unlikely(err)) {
1968 static const char *es1a = "Failed to determine if Windows is "
1969 "hibernated";
1970 static const char *es1b = "Windows is hibernated";
1971 static const char *es2 = ". Run chkdsk.";
1972 const char *es1;
1974 es1 = err < 0 ? es1a : es1b;
1975 /* If a read-write mount, convert it to a read-only mount. */
1976 if (!(sb->s_flags & MS_RDONLY)) {
1977 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1978 ON_ERRORS_CONTINUE))) {
1979 ntfs_error(sb, "%s and neither on_errors="
1980 "continue nor on_errors="
1981 "remount-ro was specified%s",
1982 es1, es2);
1983 goto iput_root_err_out;
1985 sb->s_flags |= MS_RDONLY;
1986 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1987 } else
1988 ntfs_warning(sb, "%s. Will not be able to remount "
1989 "read-write%s", es1, es2);
1990 /* This will prevent a read-write remount. */
1991 NVolSetErrors(vol);
1993 /* If (still) a read-write mount, mark the volume dirty. */
1994 if (!(sb->s_flags & MS_RDONLY) &&
1995 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
1996 static const char *es1 = "Failed to set dirty bit in volume "
1997 "information flags";
1998 static const char *es2 = ". Run chkdsk.";
2000 /* Convert to a read-only mount. */
2001 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2002 ON_ERRORS_CONTINUE))) {
2003 ntfs_error(sb, "%s and neither on_errors=continue nor "
2004 "on_errors=remount-ro was specified%s",
2005 es1, es2);
2006 goto iput_root_err_out;
2008 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2009 sb->s_flags |= MS_RDONLY;
2011 * Do not set NVolErrors() because ntfs_remount() might manage
2012 * to set the dirty flag in which case all would be well.
2015 /* If (still) a read-write mount, empty the logfile. */
2016 if (!(sb->s_flags & MS_RDONLY) &&
2017 !ntfs_empty_logfile(vol->logfile_ino)) {
2018 static const char *es1 = "Failed to empty $LogFile";
2019 static const char *es2 = ". Mount in Windows.";
2021 /* Convert to a read-only mount. */
2022 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2023 ON_ERRORS_CONTINUE))) {
2024 ntfs_error(sb, "%s and neither on_errors=continue nor "
2025 "on_errors=remount-ro was specified%s",
2026 es1, es2);
2027 goto iput_root_err_out;
2029 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2030 sb->s_flags |= MS_RDONLY;
2031 NVolSetErrors(vol);
2033 #endif /* NTFS_RW */
2034 /* If on NTFS versions before 3.0, we are done. */
2035 if (unlikely(vol->major_ver < 3))
2036 return true;
2037 /* NTFS 3.0+ specific initialization. */
2038 /* Get the security descriptors inode. */
2039 vol->secure_ino = ntfs_iget(sb, FILE_Secure);
2040 if (IS_ERR(vol->secure_ino) || is_bad_inode(vol->secure_ino)) {
2041 if (!IS_ERR(vol->secure_ino))
2042 iput(vol->secure_ino);
2043 ntfs_error(sb, "Failed to load $Secure.");
2044 goto iput_root_err_out;
2046 // TODO: Initialize security.
2047 /* Get the extended system files' directory inode. */
2048 vol->extend_ino = ntfs_iget(sb, FILE_Extend);
2049 if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) {
2050 if (!IS_ERR(vol->extend_ino))
2051 iput(vol->extend_ino);
2052 ntfs_error(sb, "Failed to load $Extend.");
2053 goto iput_sec_err_out;
2055 #ifdef NTFS_RW
2056 /* Find the quota file, load it if present, and set it up. */
2057 if (!load_and_init_quota(vol)) {
2058 static const char *es1 = "Failed to load $Quota";
2059 static const char *es2 = ". Run chkdsk.";
2061 /* If a read-write mount, convert it to a read-only mount. */
2062 if (!(sb->s_flags & MS_RDONLY)) {
2063 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2064 ON_ERRORS_CONTINUE))) {
2065 ntfs_error(sb, "%s and neither on_errors="
2066 "continue nor on_errors="
2067 "remount-ro was specified%s",
2068 es1, es2);
2069 goto iput_quota_err_out;
2071 sb->s_flags |= MS_RDONLY;
2072 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2073 } else
2074 ntfs_warning(sb, "%s. Will not be able to remount "
2075 "read-write%s", es1, es2);
2076 /* This will prevent a read-write remount. */
2077 NVolSetErrors(vol);
2079 /* If (still) a read-write mount, mark the quotas out of date. */
2080 if (!(sb->s_flags & MS_RDONLY) &&
2081 !ntfs_mark_quotas_out_of_date(vol)) {
2082 static const char *es1 = "Failed to mark quotas out of date";
2083 static const char *es2 = ". Run chkdsk.";
2085 /* Convert to a read-only mount. */
2086 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2087 ON_ERRORS_CONTINUE))) {
2088 ntfs_error(sb, "%s and neither on_errors=continue nor "
2089 "on_errors=remount-ro was specified%s",
2090 es1, es2);
2091 goto iput_quota_err_out;
2093 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2094 sb->s_flags |= MS_RDONLY;
2095 NVolSetErrors(vol);
2098 * Find the transaction log file ($UsnJrnl), load it if present, check
2099 * it, and set it up.
2101 if (!load_and_init_usnjrnl(vol)) {
2102 static const char *es1 = "Failed to load $UsnJrnl";
2103 static const char *es2 = ". Run chkdsk.";
2105 /* If a read-write mount, convert it to a read-only mount. */
2106 if (!(sb->s_flags & MS_RDONLY)) {
2107 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2108 ON_ERRORS_CONTINUE))) {
2109 ntfs_error(sb, "%s and neither on_errors="
2110 "continue nor on_errors="
2111 "remount-ro was specified%s",
2112 es1, es2);
2113 goto iput_usnjrnl_err_out;
2115 sb->s_flags |= MS_RDONLY;
2116 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2117 } else
2118 ntfs_warning(sb, "%s. Will not be able to remount "
2119 "read-write%s", es1, es2);
2120 /* This will prevent a read-write remount. */
2121 NVolSetErrors(vol);
2123 /* If (still) a read-write mount, stamp the transaction log. */
2124 if (!(sb->s_flags & MS_RDONLY) && !ntfs_stamp_usnjrnl(vol)) {
2125 static const char *es1 = "Failed to stamp transaction log "
2126 "($UsnJrnl)";
2127 static const char *es2 = ". Run chkdsk.";
2129 /* Convert to a read-only mount. */
2130 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2131 ON_ERRORS_CONTINUE))) {
2132 ntfs_error(sb, "%s and neither on_errors=continue nor "
2133 "on_errors=remount-ro was specified%s",
2134 es1, es2);
2135 goto iput_usnjrnl_err_out;
2137 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2138 sb->s_flags |= MS_RDONLY;
2139 NVolSetErrors(vol);
2141 #endif /* NTFS_RW */
2142 return true;
2143 #ifdef NTFS_RW
2144 iput_usnjrnl_err_out:
2145 if (vol->usnjrnl_j_ino)
2146 iput(vol->usnjrnl_j_ino);
2147 if (vol->usnjrnl_max_ino)
2148 iput(vol->usnjrnl_max_ino);
2149 if (vol->usnjrnl_ino)
2150 iput(vol->usnjrnl_ino);
2151 iput_quota_err_out:
2152 if (vol->quota_q_ino)
2153 iput(vol->quota_q_ino);
2154 if (vol->quota_ino)
2155 iput(vol->quota_ino);
2156 iput(vol->extend_ino);
2157 #endif /* NTFS_RW */
2158 iput_sec_err_out:
2159 iput(vol->secure_ino);
2160 iput_root_err_out:
2161 iput(vol->root_ino);
2162 iput_logfile_err_out:
2163 #ifdef NTFS_RW
2164 if (vol->logfile_ino)
2165 iput(vol->logfile_ino);
2166 iput_vol_err_out:
2167 #endif /* NTFS_RW */
2168 iput(vol->vol_ino);
2169 iput_lcnbmp_err_out:
2170 iput(vol->lcnbmp_ino);
2171 iput_attrdef_err_out:
2172 vol->attrdef_size = 0;
2173 if (vol->attrdef) {
2174 ntfs_free(vol->attrdef);
2175 vol->attrdef = NULL;
2177 #ifdef NTFS_RW
2178 iput_upcase_err_out:
2179 #endif /* NTFS_RW */
2180 vol->upcase_len = 0;
2181 mutex_lock(&ntfs_lock);
2182 if (vol->upcase == default_upcase) {
2183 ntfs_nr_upcase_users--;
2184 vol->upcase = NULL;
2186 mutex_unlock(&ntfs_lock);
2187 if (vol->upcase) {
2188 ntfs_free(vol->upcase);
2189 vol->upcase = NULL;
2191 iput_mftbmp_err_out:
2192 iput(vol->mftbmp_ino);
2193 iput_mirr_err_out:
2194 #ifdef NTFS_RW
2195 if (vol->mftmirr_ino)
2196 iput(vol->mftmirr_ino);
2197 #endif /* NTFS_RW */
2198 return false;
2202 * ntfs_put_super - called by the vfs to unmount a volume
2203 * @sb: vfs superblock of volume to unmount
2205 * ntfs_put_super() is called by the VFS (from fs/super.c::do_umount()) when
2206 * the volume is being unmounted (umount system call has been invoked) and it
2207 * releases all inodes and memory belonging to the NTFS specific part of the
2208 * super block.
2210 static void ntfs_put_super(struct super_block *sb)
2212 ntfs_volume *vol = NTFS_SB(sb);
2214 ntfs_debug("Entering.");
2216 lock_kernel();
2218 #ifdef NTFS_RW
2220 * Commit all inodes while they are still open in case some of them
2221 * cause others to be dirtied.
2223 ntfs_commit_inode(vol->vol_ino);
2225 /* NTFS 3.0+ specific. */
2226 if (vol->major_ver >= 3) {
2227 if (vol->usnjrnl_j_ino)
2228 ntfs_commit_inode(vol->usnjrnl_j_ino);
2229 if (vol->usnjrnl_max_ino)
2230 ntfs_commit_inode(vol->usnjrnl_max_ino);
2231 if (vol->usnjrnl_ino)
2232 ntfs_commit_inode(vol->usnjrnl_ino);
2233 if (vol->quota_q_ino)
2234 ntfs_commit_inode(vol->quota_q_ino);
2235 if (vol->quota_ino)
2236 ntfs_commit_inode(vol->quota_ino);
2237 if (vol->extend_ino)
2238 ntfs_commit_inode(vol->extend_ino);
2239 if (vol->secure_ino)
2240 ntfs_commit_inode(vol->secure_ino);
2243 ntfs_commit_inode(vol->root_ino);
2245 down_write(&vol->lcnbmp_lock);
2246 ntfs_commit_inode(vol->lcnbmp_ino);
2247 up_write(&vol->lcnbmp_lock);
2249 down_write(&vol->mftbmp_lock);
2250 ntfs_commit_inode(vol->mftbmp_ino);
2251 up_write(&vol->mftbmp_lock);
2253 if (vol->logfile_ino)
2254 ntfs_commit_inode(vol->logfile_ino);
2256 if (vol->mftmirr_ino)
2257 ntfs_commit_inode(vol->mftmirr_ino);
2258 ntfs_commit_inode(vol->mft_ino);
2261 * If a read-write mount and no volume errors have occured, mark the
2262 * volume clean. Also, re-commit all affected inodes.
2264 if (!(sb->s_flags & MS_RDONLY)) {
2265 if (!NVolErrors(vol)) {
2266 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
2267 ntfs_warning(sb, "Failed to clear dirty bit "
2268 "in volume information "
2269 "flags. Run chkdsk.");
2270 ntfs_commit_inode(vol->vol_ino);
2271 ntfs_commit_inode(vol->root_ino);
2272 if (vol->mftmirr_ino)
2273 ntfs_commit_inode(vol->mftmirr_ino);
2274 ntfs_commit_inode(vol->mft_ino);
2275 } else {
2276 ntfs_warning(sb, "Volume has errors. Leaving volume "
2277 "marked dirty. Run chkdsk.");
2280 #endif /* NTFS_RW */
2282 iput(vol->vol_ino);
2283 vol->vol_ino = NULL;
2285 /* NTFS 3.0+ specific clean up. */
2286 if (vol->major_ver >= 3) {
2287 #ifdef NTFS_RW
2288 if (vol->usnjrnl_j_ino) {
2289 iput(vol->usnjrnl_j_ino);
2290 vol->usnjrnl_j_ino = NULL;
2292 if (vol->usnjrnl_max_ino) {
2293 iput(vol->usnjrnl_max_ino);
2294 vol->usnjrnl_max_ino = NULL;
2296 if (vol->usnjrnl_ino) {
2297 iput(vol->usnjrnl_ino);
2298 vol->usnjrnl_ino = NULL;
2300 if (vol->quota_q_ino) {
2301 iput(vol->quota_q_ino);
2302 vol->quota_q_ino = NULL;
2304 if (vol->quota_ino) {
2305 iput(vol->quota_ino);
2306 vol->quota_ino = NULL;
2308 #endif /* NTFS_RW */
2309 if (vol->extend_ino) {
2310 iput(vol->extend_ino);
2311 vol->extend_ino = NULL;
2313 if (vol->secure_ino) {
2314 iput(vol->secure_ino);
2315 vol->secure_ino = NULL;
2319 iput(vol->root_ino);
2320 vol->root_ino = NULL;
2322 down_write(&vol->lcnbmp_lock);
2323 iput(vol->lcnbmp_ino);
2324 vol->lcnbmp_ino = NULL;
2325 up_write(&vol->lcnbmp_lock);
2327 down_write(&vol->mftbmp_lock);
2328 iput(vol->mftbmp_ino);
2329 vol->mftbmp_ino = NULL;
2330 up_write(&vol->mftbmp_lock);
2332 #ifdef NTFS_RW
2333 if (vol->logfile_ino) {
2334 iput(vol->logfile_ino);
2335 vol->logfile_ino = NULL;
2337 if (vol->mftmirr_ino) {
2338 /* Re-commit the mft mirror and mft just in case. */
2339 ntfs_commit_inode(vol->mftmirr_ino);
2340 ntfs_commit_inode(vol->mft_ino);
2341 iput(vol->mftmirr_ino);
2342 vol->mftmirr_ino = NULL;
2345 * We should have no dirty inodes left, due to
2346 * mft.c::ntfs_mft_writepage() cleaning all the dirty pages as
2347 * the underlying mft records are written out and cleaned.
2349 ntfs_commit_inode(vol->mft_ino);
2350 write_inode_now(vol->mft_ino, 1);
2351 #endif /* NTFS_RW */
2353 iput(vol->mft_ino);
2354 vol->mft_ino = NULL;
2356 /* Throw away the table of attribute definitions. */
2357 vol->attrdef_size = 0;
2358 if (vol->attrdef) {
2359 ntfs_free(vol->attrdef);
2360 vol->attrdef = NULL;
2362 vol->upcase_len = 0;
2364 * Destroy the global default upcase table if necessary. Also decrease
2365 * the number of upcase users if we are a user.
2367 mutex_lock(&ntfs_lock);
2368 if (vol->upcase == default_upcase) {
2369 ntfs_nr_upcase_users--;
2370 vol->upcase = NULL;
2372 if (!ntfs_nr_upcase_users && default_upcase) {
2373 ntfs_free(default_upcase);
2374 default_upcase = NULL;
2376 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
2377 free_compression_buffers();
2378 mutex_unlock(&ntfs_lock);
2379 if (vol->upcase) {
2380 ntfs_free(vol->upcase);
2381 vol->upcase = NULL;
2384 unload_nls(vol->nls_map);
2386 sb->s_fs_info = NULL;
2387 kfree(vol);
2389 unlock_kernel();
2393 * get_nr_free_clusters - return the number of free clusters on a volume
2394 * @vol: ntfs volume for which to obtain free cluster count
2396 * Calculate the number of free clusters on the mounted NTFS volume @vol. We
2397 * actually calculate the number of clusters in use instead because this
2398 * allows us to not care about partial pages as these will be just zero filled
2399 * and hence not be counted as allocated clusters.
2401 * The only particularity is that clusters beyond the end of the logical ntfs
2402 * volume will be marked as allocated to prevent errors which means we have to
2403 * discount those at the end. This is important as the cluster bitmap always
2404 * has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside
2405 * the logical volume and marked in use when they are not as they do not exist.
2407 * If any pages cannot be read we assume all clusters in the erroring pages are
2408 * in use. This means we return an underestimate on errors which is better than
2409 * an overestimate.
2411 static s64 get_nr_free_clusters(ntfs_volume *vol)
2413 s64 nr_free = vol->nr_clusters;
2414 struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
2415 struct page *page;
2416 pgoff_t index, max_index;
2418 ntfs_debug("Entering.");
2419 /* Serialize accesses to the cluster bitmap. */
2420 down_read(&vol->lcnbmp_lock);
2422 * Convert the number of bits into bytes rounded up, then convert into
2423 * multiples of PAGE_CACHE_SIZE, rounding up so that if we have one
2424 * full and one partial page max_index = 2.
2426 max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >>
2427 PAGE_CACHE_SHIFT;
2428 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2429 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.",
2430 max_index, PAGE_CACHE_SIZE / 4);
2431 for (index = 0; index < max_index; index++) {
2432 unsigned long *kaddr;
2435 * Read the page from page cache, getting it from backing store
2436 * if necessary, and increment the use count.
2438 page = read_mapping_page(mapping, index, NULL);
2439 /* Ignore pages which errored synchronously. */
2440 if (IS_ERR(page)) {
2441 ntfs_debug("read_mapping_page() error. Skipping "
2442 "page (index 0x%lx).", index);
2443 nr_free -= PAGE_CACHE_SIZE * 8;
2444 continue;
2446 kaddr = kmap_atomic(page, KM_USER0);
2448 * Subtract the number of set bits. If this
2449 * is the last page and it is partial we don't really care as
2450 * it just means we do a little extra work but it won't affect
2451 * the result as all out of range bytes are set to zero by
2452 * ntfs_readpage().
2454 nr_free -= bitmap_weight(kaddr,
2455 PAGE_CACHE_SIZE * BITS_PER_BYTE);
2456 kunmap_atomic(kaddr, KM_USER0);
2457 page_cache_release(page);
2459 ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index - 1);
2461 * Fixup for eventual bits outside logical ntfs volume (see function
2462 * description above).
2464 if (vol->nr_clusters & 63)
2465 nr_free += 64 - (vol->nr_clusters & 63);
2466 up_read(&vol->lcnbmp_lock);
2467 /* If errors occured we may well have gone below zero, fix this. */
2468 if (nr_free < 0)
2469 nr_free = 0;
2470 ntfs_debug("Exiting.");
2471 return nr_free;
2475 * __get_nr_free_mft_records - return the number of free inodes on a volume
2476 * @vol: ntfs volume for which to obtain free inode count
2477 * @nr_free: number of mft records in filesystem
2478 * @max_index: maximum number of pages containing set bits
2480 * Calculate the number of free mft records (inodes) on the mounted NTFS
2481 * volume @vol. We actually calculate the number of mft records in use instead
2482 * because this allows us to not care about partial pages as these will be just
2483 * zero filled and hence not be counted as allocated mft record.
2485 * If any pages cannot be read we assume all mft records in the erroring pages
2486 * are in use. This means we return an underestimate on errors which is better
2487 * than an overestimate.
2489 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
2491 static unsigned long __get_nr_free_mft_records(ntfs_volume *vol,
2492 s64 nr_free, const pgoff_t max_index)
2494 struct address_space *mapping = vol->mftbmp_ino->i_mapping;
2495 struct page *page;
2496 pgoff_t index;
2498 ntfs_debug("Entering.");
2499 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2500 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
2501 "0x%lx.", max_index, PAGE_CACHE_SIZE / 4);
2502 for (index = 0; index < max_index; index++) {
2503 unsigned long *kaddr;
2506 * Read the page from page cache, getting it from backing store
2507 * if necessary, and increment the use count.
2509 page = read_mapping_page(mapping, index, NULL);
2510 /* Ignore pages which errored synchronously. */
2511 if (IS_ERR(page)) {
2512 ntfs_debug("read_mapping_page() error. Skipping "
2513 "page (index 0x%lx).", index);
2514 nr_free -= PAGE_CACHE_SIZE * 8;
2515 continue;
2517 kaddr = kmap_atomic(page, KM_USER0);
2519 * Subtract the number of set bits. If this
2520 * is the last page and it is partial we don't really care as
2521 * it just means we do a little extra work but it won't affect
2522 * the result as all out of range bytes are set to zero by
2523 * ntfs_readpage().
2525 nr_free -= bitmap_weight(kaddr,
2526 PAGE_CACHE_SIZE * BITS_PER_BYTE);
2527 kunmap_atomic(kaddr, KM_USER0);
2528 page_cache_release(page);
2530 ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.",
2531 index - 1);
2532 /* If errors occured we may well have gone below zero, fix this. */
2533 if (nr_free < 0)
2534 nr_free = 0;
2535 ntfs_debug("Exiting.");
2536 return nr_free;
2540 * ntfs_statfs - return information about mounted NTFS volume
2541 * @dentry: dentry from mounted volume
2542 * @sfs: statfs structure in which to return the information
2544 * Return information about the mounted NTFS volume @dentry in the statfs structure
2545 * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is
2546 * called). We interpret the values to be correct of the moment in time at
2547 * which we are called. Most values are variable otherwise and this isn't just
2548 * the free values but the totals as well. For example we can increase the
2549 * total number of file nodes if we run out and we can keep doing this until
2550 * there is no more space on the volume left at all.
2552 * Called from vfs_statfs which is used to handle the statfs, fstatfs, and
2553 * ustat system calls.
2555 * Return 0 on success or -errno on error.
2557 static int ntfs_statfs(struct dentry *dentry, struct kstatfs *sfs)
2559 struct super_block *sb = dentry->d_sb;
2560 s64 size;
2561 ntfs_volume *vol = NTFS_SB(sb);
2562 ntfs_inode *mft_ni = NTFS_I(vol->mft_ino);
2563 pgoff_t max_index;
2564 unsigned long flags;
2566 ntfs_debug("Entering.");
2567 /* Type of filesystem. */
2568 sfs->f_type = NTFS_SB_MAGIC;
2569 /* Optimal transfer block size. */
2570 sfs->f_bsize = PAGE_CACHE_SIZE;
2572 * Total data blocks in filesystem in units of f_bsize and since
2573 * inodes are also stored in data blocs ($MFT is a file) this is just
2574 * the total clusters.
2576 sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >>
2577 PAGE_CACHE_SHIFT;
2578 /* Free data blocks in filesystem in units of f_bsize. */
2579 size = get_nr_free_clusters(vol) << vol->cluster_size_bits >>
2580 PAGE_CACHE_SHIFT;
2581 if (size < 0LL)
2582 size = 0LL;
2583 /* Free blocks avail to non-superuser, same as above on NTFS. */
2584 sfs->f_bavail = sfs->f_bfree = size;
2585 /* Serialize accesses to the inode bitmap. */
2586 down_read(&vol->mftbmp_lock);
2587 read_lock_irqsave(&mft_ni->size_lock, flags);
2588 size = i_size_read(vol->mft_ino) >> vol->mft_record_size_bits;
2590 * Convert the maximum number of set bits into bytes rounded up, then
2591 * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
2592 * have one full and one partial page max_index = 2.
2594 max_index = ((((mft_ni->initialized_size >> vol->mft_record_size_bits)
2595 + 7) >> 3) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2596 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2597 /* Number of inodes in filesystem (at this point in time). */
2598 sfs->f_files = size;
2599 /* Free inodes in fs (based on current total count). */
2600 sfs->f_ffree = __get_nr_free_mft_records(vol, size, max_index);
2601 up_read(&vol->mftbmp_lock);
2603 * File system id. This is extremely *nix flavour dependent and even
2604 * within Linux itself all fs do their own thing. I interpret this to
2605 * mean a unique id associated with the mounted fs and not the id
2606 * associated with the filesystem driver, the latter is already given
2607 * by the filesystem type in sfs->f_type. Thus we use the 64-bit
2608 * volume serial number splitting it into two 32-bit parts. We enter
2609 * the least significant 32-bits in f_fsid[0] and the most significant
2610 * 32-bits in f_fsid[1].
2612 sfs->f_fsid.val[0] = vol->serial_no & 0xffffffff;
2613 sfs->f_fsid.val[1] = (vol->serial_no >> 32) & 0xffffffff;
2614 /* Maximum length of filenames. */
2615 sfs->f_namelen = NTFS_MAX_NAME_LEN;
2616 return 0;
2619 #ifdef NTFS_RW
2620 static int ntfs_write_inode(struct inode *vi, struct writeback_control *wbc)
2622 return __ntfs_write_inode(vi, wbc->sync_mode == WB_SYNC_ALL);
2624 #endif
2627 * The complete super operations.
2629 static const 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 #ifdef NTFS_RW
2633 //.dirty_inode = NULL, /* VFS: Called from
2634 // __mark_inode_dirty(). */
2635 .write_inode = ntfs_write_inode, /* VFS: Write dirty inode to
2636 disk. */
2637 //.drop_inode = NULL, /* VFS: Called just after the
2638 // inode reference count has
2639 // been decreased to zero.
2640 // NOTE: The inode lock is
2641 // held. See fs/inode.c::
2642 // generic_drop_inode(). */
2643 //.delete_inode = NULL, /* VFS: Delete inode from disk.
2644 // Called when i_count becomes
2645 // 0 and i_nlink is also 0. */
2646 //.write_super = NULL, /* Flush dirty super block to
2647 // disk. */
2648 //.sync_fs = NULL, /* ? */
2649 //.write_super_lockfs = NULL, /* ? */
2650 //.unlockfs = NULL, /* ? */
2651 #endif /* NTFS_RW */
2652 .put_super = ntfs_put_super, /* Syscall: umount. */
2653 .statfs = ntfs_statfs, /* Syscall: statfs */
2654 .remount_fs = ntfs_remount, /* Syscall: mount -o remount. */
2655 .evict_inode = ntfs_evict_big_inode, /* VFS: Called when an inode is
2656 removed from memory. */
2657 //.umount_begin = NULL, /* Forced umount. */
2658 .show_options = ntfs_show_options, /* Show mount options in
2659 proc. */
2663 * ntfs_fill_super - mount an ntfs filesystem
2664 * @sb: super block of ntfs filesystem to mount
2665 * @opt: string containing the mount options
2666 * @silent: silence error output
2668 * ntfs_fill_super() is called by the VFS to mount the device described by @sb
2669 * with the mount otions in @data with the NTFS filesystem.
2671 * If @silent is true, remain silent even if errors are detected. This is used
2672 * during bootup, when the kernel tries to mount the root filesystem with all
2673 * registered filesystems one after the other until one succeeds. This implies
2674 * that all filesystems except the correct one will quite correctly and
2675 * expectedly return an error, but nobody wants to see error messages when in
2676 * fact this is what is supposed to happen.
2678 * NOTE: @sb->s_flags contains the mount options flags.
2680 static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2682 ntfs_volume *vol;
2683 struct buffer_head *bh;
2684 struct inode *tmp_ino;
2685 int blocksize, result;
2688 * We do a pretty difficult piece of bootstrap by reading the
2689 * MFT (and other metadata) from disk into memory. We'll only
2690 * release this metadata during umount, so the locking patterns
2691 * observed during bootstrap do not count. So turn off the
2692 * observation of locking patterns (strictly for this context
2693 * only) while mounting NTFS. [The validator is still active
2694 * otherwise, even for this context: it will for example record
2695 * lock class registrations.]
2697 lockdep_off();
2698 ntfs_debug("Entering.");
2699 #ifndef NTFS_RW
2700 sb->s_flags |= MS_RDONLY;
2701 #endif /* ! NTFS_RW */
2702 /* Allocate a new ntfs_volume and place it in sb->s_fs_info. */
2703 sb->s_fs_info = kmalloc(sizeof(ntfs_volume), GFP_NOFS);
2704 vol = NTFS_SB(sb);
2705 if (!vol) {
2706 if (!silent)
2707 ntfs_error(sb, "Allocation of NTFS volume structure "
2708 "failed. Aborting mount...");
2709 lockdep_on();
2710 return -ENOMEM;
2712 /* Initialize ntfs_volume structure. */
2713 *vol = (ntfs_volume) {
2714 .sb = sb,
2716 * Default is group and other don't have any access to files or
2717 * directories while owner has full access. Further, files by
2718 * default are not executable but directories are of course
2719 * browseable.
2721 .fmask = 0177,
2722 .dmask = 0077,
2724 init_rwsem(&vol->mftbmp_lock);
2725 init_rwsem(&vol->lcnbmp_lock);
2727 unlock_kernel();
2729 /* By default, enable sparse support. */
2730 NVolSetSparseEnabled(vol);
2732 /* Important to get the mount options dealt with now. */
2733 if (!parse_options(vol, (char*)opt))
2734 goto err_out_now;
2736 /* We support sector sizes up to the PAGE_CACHE_SIZE. */
2737 if (bdev_logical_block_size(sb->s_bdev) > PAGE_CACHE_SIZE) {
2738 if (!silent)
2739 ntfs_error(sb, "Device has unsupported sector size "
2740 "(%i). The maximum supported sector "
2741 "size on this architecture is %lu "
2742 "bytes.",
2743 bdev_logical_block_size(sb->s_bdev),
2744 PAGE_CACHE_SIZE);
2745 goto err_out_now;
2748 * Setup the device access block size to NTFS_BLOCK_SIZE or the hard
2749 * sector size, whichever is bigger.
2751 blocksize = sb_min_blocksize(sb, NTFS_BLOCK_SIZE);
2752 if (blocksize < NTFS_BLOCK_SIZE) {
2753 if (!silent)
2754 ntfs_error(sb, "Unable to set device block size.");
2755 goto err_out_now;
2757 BUG_ON(blocksize != sb->s_blocksize);
2758 ntfs_debug("Set device block size to %i bytes (block size bits %i).",
2759 blocksize, sb->s_blocksize_bits);
2760 /* Determine the size of the device in units of block_size bytes. */
2761 if (!i_size_read(sb->s_bdev->bd_inode)) {
2762 if (!silent)
2763 ntfs_error(sb, "Unable to determine device size.");
2764 goto err_out_now;
2766 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2767 sb->s_blocksize_bits;
2768 /* Read the boot sector and return unlocked buffer head to it. */
2769 if (!(bh = read_ntfs_boot_sector(sb, silent))) {
2770 if (!silent)
2771 ntfs_error(sb, "Not an NTFS volume.");
2772 goto err_out_now;
2775 * Extract the data from the boot sector and setup the ntfs volume
2776 * using it.
2778 result = parse_ntfs_boot_sector(vol, (NTFS_BOOT_SECTOR*)bh->b_data);
2779 brelse(bh);
2780 if (!result) {
2781 if (!silent)
2782 ntfs_error(sb, "Unsupported NTFS filesystem.");
2783 goto err_out_now;
2786 * If the boot sector indicates a sector size bigger than the current
2787 * device block size, switch the device block size to the sector size.
2788 * TODO: It may be possible to support this case even when the set
2789 * below fails, we would just be breaking up the i/o for each sector
2790 * into multiple blocks for i/o purposes but otherwise it should just
2791 * work. However it is safer to leave disabled until someone hits this
2792 * error message and then we can get them to try it without the setting
2793 * so we know for sure that it works.
2795 if (vol->sector_size > blocksize) {
2796 blocksize = sb_set_blocksize(sb, vol->sector_size);
2797 if (blocksize != vol->sector_size) {
2798 if (!silent)
2799 ntfs_error(sb, "Unable to set device block "
2800 "size to sector size (%i).",
2801 vol->sector_size);
2802 goto err_out_now;
2804 BUG_ON(blocksize != sb->s_blocksize);
2805 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2806 sb->s_blocksize_bits;
2807 ntfs_debug("Changed device block size to %i bytes (block size "
2808 "bits %i) to match volume sector size.",
2809 blocksize, sb->s_blocksize_bits);
2811 /* Initialize the cluster and mft allocators. */
2812 ntfs_setup_allocators(vol);
2813 /* Setup remaining fields in the super block. */
2814 sb->s_magic = NTFS_SB_MAGIC;
2816 * Ntfs allows 63 bits for the file size, i.e. correct would be:
2817 * sb->s_maxbytes = ~0ULL >> 1;
2818 * But the kernel uses a long as the page cache page index which on
2819 * 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel
2820 * defined to the maximum the page cache page index can cope with
2821 * without overflowing the index or to 2^63 - 1, whichever is smaller.
2823 sb->s_maxbytes = MAX_LFS_FILESIZE;
2824 /* Ntfs measures time in 100ns intervals. */
2825 sb->s_time_gran = 100;
2827 * Now load the metadata required for the page cache and our address
2828 * space operations to function. We do this by setting up a specialised
2829 * read_inode method and then just calling the normal iget() to obtain
2830 * the inode for $MFT which is sufficient to allow our normal inode
2831 * operations and associated address space operations to function.
2833 sb->s_op = &ntfs_sops;
2834 tmp_ino = new_inode(sb);
2835 if (!tmp_ino) {
2836 if (!silent)
2837 ntfs_error(sb, "Failed to load essential metadata.");
2838 goto err_out_now;
2840 tmp_ino->i_ino = FILE_MFT;
2841 insert_inode_hash(tmp_ino);
2842 if (ntfs_read_inode_mount(tmp_ino) < 0) {
2843 if (!silent)
2844 ntfs_error(sb, "Failed to load essential metadata.");
2845 goto iput_tmp_ino_err_out_now;
2847 mutex_lock(&ntfs_lock);
2849 * The current mount is a compression user if the cluster size is
2850 * less than or equal 4kiB.
2852 if (vol->cluster_size <= 4096 && !ntfs_nr_compression_users++) {
2853 result = allocate_compression_buffers();
2854 if (result) {
2855 ntfs_error(NULL, "Failed to allocate buffers "
2856 "for compression engine.");
2857 ntfs_nr_compression_users--;
2858 mutex_unlock(&ntfs_lock);
2859 goto iput_tmp_ino_err_out_now;
2863 * Generate the global default upcase table if necessary. Also
2864 * temporarily increment the number of upcase users to avoid race
2865 * conditions with concurrent (u)mounts.
2867 if (!default_upcase)
2868 default_upcase = generate_default_upcase();
2869 ntfs_nr_upcase_users++;
2870 mutex_unlock(&ntfs_lock);
2872 * From now on, ignore @silent parameter. If we fail below this line,
2873 * it will be due to a corrupt fs or a system error, so we report it.
2876 * Open the system files with normal access functions and complete
2877 * setting up the ntfs super block.
2879 if (!load_system_files(vol)) {
2880 ntfs_error(sb, "Failed to load system files.");
2881 goto unl_upcase_iput_tmp_ino_err_out_now;
2883 if ((sb->s_root = d_alloc_root(vol->root_ino))) {
2884 /* We increment i_count simulating an ntfs_iget(). */
2885 atomic_inc(&vol->root_ino->i_count);
2886 ntfs_debug("Exiting, status successful.");
2887 /* Release the default upcase if it has no users. */
2888 mutex_lock(&ntfs_lock);
2889 if (!--ntfs_nr_upcase_users && default_upcase) {
2890 ntfs_free(default_upcase);
2891 default_upcase = NULL;
2893 mutex_unlock(&ntfs_lock);
2894 sb->s_export_op = &ntfs_export_ops;
2895 lock_kernel();
2896 lockdep_on();
2897 return 0;
2899 ntfs_error(sb, "Failed to allocate root directory.");
2900 /* Clean up after the successful load_system_files() call from above. */
2901 // TODO: Use ntfs_put_super() instead of repeating all this code...
2902 // -ENOMEM.
2903 iput(vol->vol_ino);
2904 vol->vol_ino = NULL;
2905 /* NTFS 3.0+ specific clean up. */
2906 if (vol->major_ver >= 3) {
2907 #ifdef NTFS_RW
2908 if (vol->usnjrnl_j_ino) {
2909 iput(vol->usnjrnl_j_ino);
2910 vol->usnjrnl_j_ino = NULL;
2912 if (vol->usnjrnl_max_ino) {
2913 iput(vol->usnjrnl_max_ino);
2914 vol->usnjrnl_max_ino = NULL;
2916 if (vol->usnjrnl_ino) {
2917 iput(vol->usnjrnl_ino);
2918 vol->usnjrnl_ino = NULL;
2920 if (vol->quota_q_ino) {
2921 iput(vol->quota_q_ino);
2922 vol->quota_q_ino = NULL;
2924 if (vol->quota_ino) {
2925 iput(vol->quota_ino);
2926 vol->quota_ino = NULL;
2928 #endif /* NTFS_RW */
2929 if (vol->extend_ino) {
2930 iput(vol->extend_ino);
2931 vol->extend_ino = NULL;
2933 if (vol->secure_ino) {
2934 iput(vol->secure_ino);
2935 vol->secure_ino = NULL;
2938 iput(vol->root_ino);
2939 vol->root_ino = NULL;
2940 iput(vol->lcnbmp_ino);
2941 vol->lcnbmp_ino = NULL;
2942 iput(vol->mftbmp_ino);
2943 vol->mftbmp_ino = NULL;
2944 #ifdef NTFS_RW
2945 if (vol->logfile_ino) {
2946 iput(vol->logfile_ino);
2947 vol->logfile_ino = NULL;
2949 if (vol->mftmirr_ino) {
2950 iput(vol->mftmirr_ino);
2951 vol->mftmirr_ino = NULL;
2953 #endif /* NTFS_RW */
2954 /* Throw away the table of attribute definitions. */
2955 vol->attrdef_size = 0;
2956 if (vol->attrdef) {
2957 ntfs_free(vol->attrdef);
2958 vol->attrdef = NULL;
2960 vol->upcase_len = 0;
2961 mutex_lock(&ntfs_lock);
2962 if (vol->upcase == default_upcase) {
2963 ntfs_nr_upcase_users--;
2964 vol->upcase = NULL;
2966 mutex_unlock(&ntfs_lock);
2967 if (vol->upcase) {
2968 ntfs_free(vol->upcase);
2969 vol->upcase = NULL;
2971 if (vol->nls_map) {
2972 unload_nls(vol->nls_map);
2973 vol->nls_map = NULL;
2975 /* Error exit code path. */
2976 unl_upcase_iput_tmp_ino_err_out_now:
2978 * Decrease the number of upcase users and destroy the global default
2979 * upcase table if necessary.
2981 mutex_lock(&ntfs_lock);
2982 if (!--ntfs_nr_upcase_users && default_upcase) {
2983 ntfs_free(default_upcase);
2984 default_upcase = NULL;
2986 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
2987 free_compression_buffers();
2988 mutex_unlock(&ntfs_lock);
2989 iput_tmp_ino_err_out_now:
2990 iput(tmp_ino);
2991 if (vol->mft_ino && vol->mft_ino != tmp_ino)
2992 iput(vol->mft_ino);
2993 vol->mft_ino = NULL;
2994 if (invalidate_inodes(sb)) {
2995 ntfs_error(sb, "Busy inodes left. This is most likely a NTFS "
2996 "driver bug.");
2997 /* Copied from fs/super.c. I just love this message. (-; */
2998 printk("NTFS: Busy inodes after umount. Self-destruct in 5 "
2999 "seconds. Have a nice day...\n");
3001 /* Errors at this stage are irrelevant. */
3002 err_out_now:
3003 lock_kernel();
3004 sb->s_fs_info = NULL;
3005 kfree(vol);
3006 ntfs_debug("Failed, returning -EINVAL.");
3007 lockdep_on();
3008 return -EINVAL;
3012 * This is a slab cache to optimize allocations and deallocations of Unicode
3013 * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN
3014 * (255) Unicode characters + a terminating NULL Unicode character.
3016 struct kmem_cache *ntfs_name_cache;
3018 /* Slab caches for efficient allocation/deallocation of inodes. */
3019 struct kmem_cache *ntfs_inode_cache;
3020 struct kmem_cache *ntfs_big_inode_cache;
3022 /* Init once constructor for the inode slab cache. */
3023 static void ntfs_big_inode_init_once(void *foo)
3025 ntfs_inode *ni = (ntfs_inode *)foo;
3027 inode_init_once(VFS_I(ni));
3031 * Slab caches to optimize allocations and deallocations of attribute search
3032 * contexts and index contexts, respectively.
3034 struct kmem_cache *ntfs_attr_ctx_cache;
3035 struct kmem_cache *ntfs_index_ctx_cache;
3037 /* Driver wide mutex. */
3038 DEFINE_MUTEX(ntfs_lock);
3040 static int ntfs_get_sb(struct file_system_type *fs_type,
3041 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3043 return get_sb_bdev(fs_type, flags, dev_name, data, ntfs_fill_super,
3044 mnt);
3047 static struct file_system_type ntfs_fs_type = {
3048 .owner = THIS_MODULE,
3049 .name = "ntfs",
3050 .get_sb = ntfs_get_sb,
3051 .kill_sb = kill_block_super,
3052 .fs_flags = FS_REQUIRES_DEV,
3055 /* Stable names for the slab caches. */
3056 static const char ntfs_index_ctx_cache_name[] = "ntfs_index_ctx_cache";
3057 static const char ntfs_attr_ctx_cache_name[] = "ntfs_attr_ctx_cache";
3058 static const char ntfs_name_cache_name[] = "ntfs_name_cache";
3059 static const char ntfs_inode_cache_name[] = "ntfs_inode_cache";
3060 static const char ntfs_big_inode_cache_name[] = "ntfs_big_inode_cache";
3062 static int __init init_ntfs_fs(void)
3064 int err = 0;
3066 /* This may be ugly but it results in pretty output so who cares. (-8 */
3067 printk(KERN_INFO "NTFS driver " NTFS_VERSION " [Flags: R/"
3068 #ifdef NTFS_RW
3070 #else
3072 #endif
3073 #ifdef DEBUG
3074 " DEBUG"
3075 #endif
3076 #ifdef MODULE
3077 " MODULE"
3078 #endif
3079 "].\n");
3081 ntfs_debug("Debug messages are enabled.");
3083 ntfs_index_ctx_cache = kmem_cache_create(ntfs_index_ctx_cache_name,
3084 sizeof(ntfs_index_context), 0 /* offset */,
3085 SLAB_HWCACHE_ALIGN, NULL /* ctor */);
3086 if (!ntfs_index_ctx_cache) {
3087 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3088 ntfs_index_ctx_cache_name);
3089 goto ictx_err_out;
3091 ntfs_attr_ctx_cache = kmem_cache_create(ntfs_attr_ctx_cache_name,
3092 sizeof(ntfs_attr_search_ctx), 0 /* offset */,
3093 SLAB_HWCACHE_ALIGN, NULL /* ctor */);
3094 if (!ntfs_attr_ctx_cache) {
3095 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3096 ntfs_attr_ctx_cache_name);
3097 goto actx_err_out;
3100 ntfs_name_cache = kmem_cache_create(ntfs_name_cache_name,
3101 (NTFS_MAX_NAME_LEN+1) * sizeof(ntfschar), 0,
3102 SLAB_HWCACHE_ALIGN, NULL);
3103 if (!ntfs_name_cache) {
3104 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3105 ntfs_name_cache_name);
3106 goto name_err_out;
3109 ntfs_inode_cache = kmem_cache_create(ntfs_inode_cache_name,
3110 sizeof(ntfs_inode), 0,
3111 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL);
3112 if (!ntfs_inode_cache) {
3113 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3114 ntfs_inode_cache_name);
3115 goto inode_err_out;
3118 ntfs_big_inode_cache = kmem_cache_create(ntfs_big_inode_cache_name,
3119 sizeof(big_ntfs_inode), 0,
3120 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD,
3121 ntfs_big_inode_init_once);
3122 if (!ntfs_big_inode_cache) {
3123 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3124 ntfs_big_inode_cache_name);
3125 goto big_inode_err_out;
3128 /* Register the ntfs sysctls. */
3129 err = ntfs_sysctl(1);
3130 if (err) {
3131 printk(KERN_CRIT "NTFS: Failed to register NTFS sysctls!\n");
3132 goto sysctl_err_out;
3135 err = register_filesystem(&ntfs_fs_type);
3136 if (!err) {
3137 ntfs_debug("NTFS driver registered successfully.");
3138 return 0; /* Success! */
3140 printk(KERN_CRIT "NTFS: Failed to register NTFS filesystem driver!\n");
3142 sysctl_err_out:
3143 kmem_cache_destroy(ntfs_big_inode_cache);
3144 big_inode_err_out:
3145 kmem_cache_destroy(ntfs_inode_cache);
3146 inode_err_out:
3147 kmem_cache_destroy(ntfs_name_cache);
3148 name_err_out:
3149 kmem_cache_destroy(ntfs_attr_ctx_cache);
3150 actx_err_out:
3151 kmem_cache_destroy(ntfs_index_ctx_cache);
3152 ictx_err_out:
3153 if (!err) {
3154 printk(KERN_CRIT "NTFS: Aborting NTFS filesystem driver "
3155 "registration...\n");
3156 err = -ENOMEM;
3158 return err;
3161 static void __exit exit_ntfs_fs(void)
3163 ntfs_debug("Unregistering NTFS driver.");
3165 unregister_filesystem(&ntfs_fs_type);
3166 kmem_cache_destroy(ntfs_big_inode_cache);
3167 kmem_cache_destroy(ntfs_inode_cache);
3168 kmem_cache_destroy(ntfs_name_cache);
3169 kmem_cache_destroy(ntfs_attr_ctx_cache);
3170 kmem_cache_destroy(ntfs_index_ctx_cache);
3171 /* Unregister the ntfs sysctls. */
3172 ntfs_sysctl(0);
3175 MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>");
3176 MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2007 Anton Altaparmakov");
3177 MODULE_VERSION(NTFS_VERSION);
3178 MODULE_LICENSE("GPL");
3179 #ifdef DEBUG
3180 module_param(debug_msgs, bool, 0);
3181 MODULE_PARM_DESC(debug_msgs, "Enable debug messages.");
3182 #endif
3184 module_init(init_ntfs_fs)
3185 module_exit(exit_ntfs_fs)