fed up with those stupid warnings
[mmotm.git] / fs / ntfs / super.c
blob80b04770e8e9c22a64a4becf0686260627e0d7e6
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>
35 #include "sysctl.h"
36 #include "logfile.h"
37 #include "quota.h"
38 #include "usnjrnl.h"
39 #include "dir.h"
40 #include "debug.h"
41 #include "index.h"
42 #include "aops.h"
43 #include "layout.h"
44 #include "malloc.h"
45 #include "ntfs.h"
47 /* Number of mounted filesystems which have compression enabled. */
48 static unsigned long ntfs_nr_compression_users;
50 /* A global default upcase table and a corresponding reference count. */
51 static ntfschar *default_upcase = NULL;
52 static unsigned long ntfs_nr_upcase_users = 0;
54 /* Error constants/strings used in inode.c::ntfs_show_options(). */
55 typedef enum {
56 /* One of these must be present, default is ON_ERRORS_CONTINUE. */
57 ON_ERRORS_PANIC = 0x01,
58 ON_ERRORS_REMOUNT_RO = 0x02,
59 ON_ERRORS_CONTINUE = 0x04,
60 /* Optional, can be combined with any of the above. */
61 ON_ERRORS_RECOVER = 0x10,
62 } ON_ERRORS_ACTIONS;
64 const option_t on_errors_arr[] = {
65 { ON_ERRORS_PANIC, "panic" },
66 { ON_ERRORS_REMOUNT_RO, "remount-ro", },
67 { ON_ERRORS_CONTINUE, "continue", },
68 { ON_ERRORS_RECOVER, "recover" },
69 { 0, NULL }
72 /**
73 * simple_getbool -
75 * Copied from old ntfs driver (which copied from vfat driver).
77 static int simple_getbool(char *s, bool *setval)
79 if (s) {
80 if (!strcmp(s, "1") || !strcmp(s, "yes") || !strcmp(s, "true"))
81 *setval = true;
82 else if (!strcmp(s, "0") || !strcmp(s, "no") ||
83 !strcmp(s, "false"))
84 *setval = false;
85 else
86 return 0;
87 } else
88 *setval = true;
89 return 1;
92 /**
93 * parse_options - parse the (re)mount options
94 * @vol: ntfs volume
95 * @opt: string containing the (re)mount options
97 * Parse the recognized options in @opt for the ntfs volume described by @vol.
99 static bool parse_options(ntfs_volume *vol, char *opt)
101 char *p, *v, *ov;
102 static char *utf8 = "utf8";
103 int errors = 0, sloppy = 0;
104 uid_t uid = (uid_t)-1;
105 gid_t gid = (gid_t)-1;
106 mode_t fmask = (mode_t)-1, dmask = (mode_t)-1;
107 int mft_zone_multiplier = -1, on_errors = -1;
108 int show_sys_files = -1, case_sensitive = -1, disable_sparse = -1;
109 struct nls_table *nls_map = NULL, *old_nls;
111 /* I am lazy... (-8 */
112 #define NTFS_GETOPT_WITH_DEFAULT(option, variable, default_value) \
113 if (!strcmp(p, option)) { \
114 if (!v || !*v) \
115 variable = default_value; \
116 else { \
117 variable = simple_strtoul(ov = v, &v, 0); \
118 if (*v) \
119 goto needs_val; \
122 #define NTFS_GETOPT(option, variable) \
123 if (!strcmp(p, option)) { \
124 if (!v || !*v) \
125 goto needs_arg; \
126 variable = simple_strtoul(ov = v, &v, 0); \
127 if (*v) \
128 goto needs_val; \
130 #define NTFS_GETOPT_OCTAL(option, variable) \
131 if (!strcmp(p, option)) { \
132 if (!v || !*v) \
133 goto needs_arg; \
134 variable = simple_strtoul(ov = v, &v, 8); \
135 if (*v) \
136 goto needs_val; \
138 #define NTFS_GETOPT_BOOL(option, variable) \
139 if (!strcmp(p, option)) { \
140 bool val; \
141 if (!simple_getbool(v, &val)) \
142 goto needs_bool; \
143 variable = val; \
145 #define NTFS_GETOPT_OPTIONS_ARRAY(option, variable, opt_array) \
146 if (!strcmp(p, option)) { \
147 int _i; \
148 if (!v || !*v) \
149 goto needs_arg; \
150 ov = v; \
151 if (variable == -1) \
152 variable = 0; \
153 for (_i = 0; opt_array[_i].str && *opt_array[_i].str; _i++) \
154 if (!strcmp(opt_array[_i].str, v)) { \
155 variable |= opt_array[_i].val; \
156 break; \
158 if (!opt_array[_i].str || !*opt_array[_i].str) \
159 goto needs_val; \
161 if (!opt || !*opt)
162 goto no_mount_options;
163 ntfs_debug("Entering with mount options string: %s", opt);
164 while ((p = strsep(&opt, ","))) {
165 if ((v = strchr(p, '=')))
166 *v++ = 0;
167 NTFS_GETOPT("uid", uid)
168 else NTFS_GETOPT("gid", gid)
169 else NTFS_GETOPT_OCTAL("umask", fmask = dmask)
170 else NTFS_GETOPT_OCTAL("fmask", fmask)
171 else NTFS_GETOPT_OCTAL("dmask", dmask)
172 else NTFS_GETOPT("mft_zone_multiplier", mft_zone_multiplier)
173 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, true)
174 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files)
175 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive)
176 else NTFS_GETOPT_BOOL("disable_sparse", disable_sparse)
177 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors,
178 on_errors_arr)
179 else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes"))
180 ntfs_warning(vol->sb, "Ignoring obsolete option %s.",
182 else if (!strcmp(p, "nls") || !strcmp(p, "iocharset")) {
183 if (!strcmp(p, "iocharset"))
184 ntfs_warning(vol->sb, "Option iocharset is "
185 "deprecated. Please use "
186 "option nls=<charsetname> in "
187 "the future.");
188 if (!v || !*v)
189 goto needs_arg;
190 use_utf8:
191 old_nls = nls_map;
192 nls_map = load_nls(v);
193 if (!nls_map) {
194 if (!old_nls) {
195 ntfs_error(vol->sb, "NLS character set "
196 "%s not found.", v);
197 return false;
199 ntfs_error(vol->sb, "NLS character set %s not "
200 "found. Using previous one %s.",
201 v, old_nls->charset);
202 nls_map = old_nls;
203 } else /* nls_map */ {
204 unload_nls(old_nls);
206 } else if (!strcmp(p, "utf8")) {
207 bool val = false;
208 ntfs_warning(vol->sb, "Option utf8 is no longer "
209 "supported, using option nls=utf8. Please "
210 "use option nls=utf8 in the future and "
211 "make sure utf8 is compiled either as a "
212 "module or into the kernel.");
213 if (!v || !*v)
214 val = true;
215 else if (!simple_getbool(v, &val))
216 goto needs_bool;
217 if (val) {
218 v = utf8;
219 goto use_utf8;
221 } else {
222 ntfs_error(vol->sb, "Unrecognized mount option %s.", p);
223 if (errors < INT_MAX)
224 errors++;
226 #undef NTFS_GETOPT_OPTIONS_ARRAY
227 #undef NTFS_GETOPT_BOOL
228 #undef NTFS_GETOPT
229 #undef NTFS_GETOPT_WITH_DEFAULT
231 no_mount_options:
232 if (errors && !sloppy)
233 return false;
234 if (sloppy)
235 ntfs_warning(vol->sb, "Sloppy option given. Ignoring "
236 "unrecognized mount option(s) and continuing.");
237 /* Keep this first! */
238 if (on_errors != -1) {
239 if (!on_errors) {
240 ntfs_error(vol->sb, "Invalid errors option argument "
241 "or bug in options parser.");
242 return false;
245 if (nls_map) {
246 if (vol->nls_map && vol->nls_map != nls_map) {
247 ntfs_error(vol->sb, "Cannot change NLS character set "
248 "on remount.");
249 return false;
250 } /* else (!vol->nls_map) */
251 ntfs_debug("Using NLS character set %s.", nls_map->charset);
252 vol->nls_map = nls_map;
253 } else /* (!nls_map) */ {
254 if (!vol->nls_map) {
255 vol->nls_map = load_nls_default();
256 if (!vol->nls_map) {
257 ntfs_error(vol->sb, "Failed to load default "
258 "NLS character set.");
259 return false;
261 ntfs_debug("Using default NLS character set (%s).",
262 vol->nls_map->charset);
265 if (mft_zone_multiplier != -1) {
266 if (vol->mft_zone_multiplier && vol->mft_zone_multiplier !=
267 mft_zone_multiplier) {
268 ntfs_error(vol->sb, "Cannot change mft_zone_multiplier "
269 "on remount.");
270 return false;
272 if (mft_zone_multiplier < 1 || mft_zone_multiplier > 4) {
273 ntfs_error(vol->sb, "Invalid mft_zone_multiplier. "
274 "Using default value, i.e. 1.");
275 mft_zone_multiplier = 1;
277 vol->mft_zone_multiplier = mft_zone_multiplier;
279 if (!vol->mft_zone_multiplier)
280 vol->mft_zone_multiplier = 1;
281 if (on_errors != -1)
282 vol->on_errors = on_errors;
283 if (!vol->on_errors || vol->on_errors == ON_ERRORS_RECOVER)
284 vol->on_errors |= ON_ERRORS_CONTINUE;
285 if (uid != (uid_t)-1)
286 vol->uid = uid;
287 if (gid != (gid_t)-1)
288 vol->gid = gid;
289 if (fmask != (mode_t)-1)
290 vol->fmask = fmask;
291 if (dmask != (mode_t)-1)
292 vol->dmask = dmask;
293 if (show_sys_files != -1) {
294 if (show_sys_files)
295 NVolSetShowSystemFiles(vol);
296 else
297 NVolClearShowSystemFiles(vol);
299 if (case_sensitive != -1) {
300 if (case_sensitive)
301 NVolSetCaseSensitive(vol);
302 else
303 NVolClearCaseSensitive(vol);
305 if (disable_sparse != -1) {
306 if (disable_sparse)
307 NVolClearSparseEnabled(vol);
308 else {
309 if (!NVolSparseEnabled(vol) &&
310 vol->major_ver && vol->major_ver < 3)
311 ntfs_warning(vol->sb, "Not enabling sparse "
312 "support due to NTFS volume "
313 "version %i.%i (need at least "
314 "version 3.0).", vol->major_ver,
315 vol->minor_ver);
316 else
317 NVolSetSparseEnabled(vol);
320 return true;
321 needs_arg:
322 ntfs_error(vol->sb, "The %s option requires an argument.", p);
323 return false;
324 needs_bool:
325 ntfs_error(vol->sb, "The %s option requires a boolean argument.", p);
326 return false;
327 needs_val:
328 ntfs_error(vol->sb, "Invalid %s option argument: %s", p, ov);
329 return false;
332 #ifdef NTFS_RW
335 * ntfs_write_volume_flags - write new flags to the volume information flags
336 * @vol: ntfs volume on which to modify the flags
337 * @flags: new flags value for the volume information flags
339 * Internal function. You probably want to use ntfs_{set,clear}_volume_flags()
340 * instead (see below).
342 * Replace the volume information flags on the volume @vol with the value
343 * supplied in @flags. Note, this overwrites the volume information flags, so
344 * make sure to combine the flags you want to modify with the old flags and use
345 * the result when calling ntfs_write_volume_flags().
347 * Return 0 on success and -errno on error.
349 static int ntfs_write_volume_flags(ntfs_volume *vol, const VOLUME_FLAGS flags)
351 ntfs_inode *ni = NTFS_I(vol->vol_ino);
352 MFT_RECORD *m;
353 VOLUME_INFORMATION *vi;
354 ntfs_attr_search_ctx *ctx;
355 int err;
357 ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
358 le16_to_cpu(vol->vol_flags), le16_to_cpu(flags));
359 if (vol->vol_flags == flags)
360 goto done;
361 BUG_ON(!ni);
362 m = map_mft_record(ni);
363 if (IS_ERR(m)) {
364 err = PTR_ERR(m);
365 goto err_out;
367 ctx = ntfs_attr_get_search_ctx(ni, m);
368 if (!ctx) {
369 err = -ENOMEM;
370 goto put_unm_err_out;
372 err = ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
373 ctx);
374 if (err)
375 goto put_unm_err_out;
376 vi = (VOLUME_INFORMATION*)((u8*)ctx->attr +
377 le16_to_cpu(ctx->attr->data.resident.value_offset));
378 vol->vol_flags = vi->flags = flags;
379 flush_dcache_mft_record_page(ctx->ntfs_ino);
380 mark_mft_record_dirty(ctx->ntfs_ino);
381 ntfs_attr_put_search_ctx(ctx);
382 unmap_mft_record(ni);
383 done:
384 ntfs_debug("Done.");
385 return 0;
386 put_unm_err_out:
387 if (ctx)
388 ntfs_attr_put_search_ctx(ctx);
389 unmap_mft_record(ni);
390 err_out:
391 ntfs_error(vol->sb, "Failed with error code %i.", -err);
392 return err;
396 * ntfs_set_volume_flags - set bits in the volume information flags
397 * @vol: ntfs volume on which to modify the flags
398 * @flags: flags to set on the volume
400 * Set the bits in @flags in the volume information flags on the volume @vol.
402 * Return 0 on success and -errno on error.
404 static inline int ntfs_set_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
406 flags &= VOLUME_FLAGS_MASK;
407 return ntfs_write_volume_flags(vol, vol->vol_flags | flags);
411 * ntfs_clear_volume_flags - clear bits in the volume information flags
412 * @vol: ntfs volume on which to modify the flags
413 * @flags: flags to clear on the volume
415 * Clear the bits in @flags in the volume information flags on the volume @vol.
417 * Return 0 on success and -errno on error.
419 static inline int ntfs_clear_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
421 flags &= VOLUME_FLAGS_MASK;
422 flags = vol->vol_flags & cpu_to_le16(~le16_to_cpu(flags));
423 return ntfs_write_volume_flags(vol, flags);
426 #endif /* NTFS_RW */
429 * ntfs_remount - change the mount options of a mounted ntfs filesystem
430 * @sb: superblock of mounted ntfs filesystem
431 * @flags: remount flags
432 * @opt: remount options string
434 * Change the mount options of an already mounted ntfs filesystem.
436 * NOTE: The VFS sets the @sb->s_flags remount flags to @flags after
437 * ntfs_remount() returns successfully (i.e. returns 0). Otherwise,
438 * @sb->s_flags are not changed.
440 static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
442 ntfs_volume *vol = NTFS_SB(sb);
444 ntfs_debug("Entering with remount options string: %s", opt);
446 lock_kernel();
447 #ifndef NTFS_RW
448 /* For read-only compiled driver, enforce read-only flag. */
449 *flags |= MS_RDONLY;
450 #else /* NTFS_RW */
452 * For the read-write compiled driver, if we are remounting read-write,
453 * make sure there are no volume errors and that no unsupported volume
454 * flags are set. Also, empty the logfile journal as it would become
455 * stale as soon as something is written to the volume and mark the
456 * volume dirty so that chkdsk is run if the volume is not umounted
457 * cleanly. Finally, mark the quotas out of date so Windows rescans
458 * the volume on boot and updates them.
460 * When remounting read-only, mark the volume clean if no volume errors
461 * have occured.
463 if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
464 static const char *es = ". Cannot remount read-write.";
466 /* Remounting read-write. */
467 if (NVolErrors(vol)) {
468 ntfs_error(sb, "Volume has errors and is read-only%s",
469 es);
470 unlock_kernel();
471 return -EROFS;
473 if (vol->vol_flags & VOLUME_IS_DIRTY) {
474 ntfs_error(sb, "Volume is dirty and read-only%s", es);
475 unlock_kernel();
476 return -EROFS;
478 if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
479 ntfs_error(sb, "Volume has been modified by chkdsk "
480 "and is read-only%s", es);
481 unlock_kernel();
482 return -EROFS;
484 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
485 ntfs_error(sb, "Volume has unsupported flags set "
486 "(0x%x) and is read-only%s",
487 (unsigned)le16_to_cpu(vol->vol_flags),
488 es);
489 unlock_kernel();
490 return -EROFS;
492 if (ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
493 ntfs_error(sb, "Failed to set dirty bit in volume "
494 "information flags%s", es);
495 unlock_kernel();
496 return -EROFS;
498 #if 0
499 // TODO: Enable this code once we start modifying anything that
500 // is different between NTFS 1.2 and 3.x...
501 /* Set NT4 compatibility flag on newer NTFS version volumes. */
502 if ((vol->major_ver > 1)) {
503 if (ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
504 ntfs_error(sb, "Failed to set NT4 "
505 "compatibility flag%s", es);
506 NVolSetErrors(vol);
507 return -EROFS;
510 #endif
511 if (!ntfs_empty_logfile(vol->logfile_ino)) {
512 ntfs_error(sb, "Failed to empty journal $LogFile%s",
513 es);
514 NVolSetErrors(vol);
515 unlock_kernel();
516 return -EROFS;
518 if (!ntfs_mark_quotas_out_of_date(vol)) {
519 ntfs_error(sb, "Failed to mark quotas out of date%s",
520 es);
521 NVolSetErrors(vol);
522 unlock_kernel();
523 return -EROFS;
525 if (!ntfs_stamp_usnjrnl(vol)) {
526 ntfs_error(sb, "Failed to stamp transation log "
527 "($UsnJrnl)%s", es);
528 NVolSetErrors(vol);
529 unlock_kernel();
530 return -EROFS;
532 } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) {
533 /* Remounting read-only. */
534 if (!NVolErrors(vol)) {
535 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
536 ntfs_warning(sb, "Failed to clear dirty bit "
537 "in volume information "
538 "flags. Run chkdsk.");
541 #endif /* NTFS_RW */
543 // TODO: Deal with *flags.
545 if (!parse_options(vol, opt)) {
546 unlock_kernel();
547 return -EINVAL;
549 unlock_kernel();
550 ntfs_debug("Done.");
551 return 0;
555 * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector
556 * @sb: Super block of the device to which @b belongs.
557 * @b: Boot sector of device @sb to check.
558 * @silent: If 'true', all output will be silenced.
560 * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot
561 * sector. Returns 'true' if it is valid and 'false' if not.
563 * @sb is only needed for warning/error output, i.e. it can be NULL when silent
564 * is 'true'.
566 static bool is_boot_sector_ntfs(const struct super_block *sb,
567 const NTFS_BOOT_SECTOR *b, const bool silent)
570 * Check that checksum == sum of u32 values from b to the checksum
571 * field. If checksum is zero, no checking is done. We will work when
572 * the checksum test fails, since some utilities update the boot sector
573 * ignoring the checksum which leaves the checksum out-of-date. We
574 * report a warning if this is the case.
576 if ((void*)b < (void*)&b->checksum && b->checksum && !silent) {
577 le32 *u;
578 u32 i;
580 for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u)
581 i += le32_to_cpup(u);
582 if (le32_to_cpu(b->checksum) != i)
583 ntfs_warning(sb, "Invalid boot sector checksum.");
585 /* Check OEMidentifier is "NTFS " */
586 if (b->oem_id != magicNTFS)
587 goto not_ntfs;
588 /* Check bytes per sector value is between 256 and 4096. */
589 if (le16_to_cpu(b->bpb.bytes_per_sector) < 0x100 ||
590 le16_to_cpu(b->bpb.bytes_per_sector) > 0x1000)
591 goto not_ntfs;
592 /* Check sectors per cluster value is valid. */
593 switch (b->bpb.sectors_per_cluster) {
594 case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128:
595 break;
596 default:
597 goto not_ntfs;
599 /* Check the cluster size is not above the maximum (64kiB). */
600 if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) *
601 b->bpb.sectors_per_cluster > NTFS_MAX_CLUSTER_SIZE)
602 goto not_ntfs;
603 /* Check reserved/unused fields are really zero. */
604 if (le16_to_cpu(b->bpb.reserved_sectors) ||
605 le16_to_cpu(b->bpb.root_entries) ||
606 le16_to_cpu(b->bpb.sectors) ||
607 le16_to_cpu(b->bpb.sectors_per_fat) ||
608 le32_to_cpu(b->bpb.large_sectors) || b->bpb.fats)
609 goto not_ntfs;
610 /* Check clusters per file mft record value is valid. */
611 if ((u8)b->clusters_per_mft_record < 0xe1 ||
612 (u8)b->clusters_per_mft_record > 0xf7)
613 switch (b->clusters_per_mft_record) {
614 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
615 break;
616 default:
617 goto not_ntfs;
619 /* Check clusters per index block value is valid. */
620 if ((u8)b->clusters_per_index_record < 0xe1 ||
621 (u8)b->clusters_per_index_record > 0xf7)
622 switch (b->clusters_per_index_record) {
623 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
624 break;
625 default:
626 goto not_ntfs;
629 * Check for valid end of sector marker. We will work without it, but
630 * many BIOSes will refuse to boot from a bootsector if the magic is
631 * incorrect, so we emit a warning.
633 if (!silent && b->end_of_sector_marker != cpu_to_le16(0xaa55))
634 ntfs_warning(sb, "Invalid end of sector marker.");
635 return true;
636 not_ntfs:
637 return false;
641 * read_ntfs_boot_sector - read the NTFS boot sector of a device
642 * @sb: super block of device to read the boot sector from
643 * @silent: if true, suppress all output
645 * Reads the boot sector from the device and validates it. If that fails, tries
646 * to read the backup boot sector, first from the end of the device a-la NT4 and
647 * later and then from the middle of the device a-la NT3.51 and before.
649 * If a valid boot sector is found but it is not the primary boot sector, we
650 * repair the primary boot sector silently (unless the device is read-only or
651 * the primary boot sector is not accessible).
653 * NOTE: To call this function, @sb must have the fields s_dev, the ntfs super
654 * block (u.ntfs_sb), nr_blocks and the device flags (s_flags) initialized
655 * to their respective values.
657 * Return the unlocked buffer head containing the boot sector or NULL on error.
659 static struct buffer_head *read_ntfs_boot_sector(struct super_block *sb,
660 const int silent)
662 const char *read_err_str = "Unable to read %s boot sector.";
663 struct buffer_head *bh_primary, *bh_backup;
664 sector_t nr_blocks = NTFS_SB(sb)->nr_blocks;
666 /* Try to read primary boot sector. */
667 if ((bh_primary = sb_bread(sb, 0))) {
668 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
669 bh_primary->b_data, silent))
670 return bh_primary;
671 if (!silent)
672 ntfs_error(sb, "Primary boot sector is invalid.");
673 } else if (!silent)
674 ntfs_error(sb, read_err_str, "primary");
675 if (!(NTFS_SB(sb)->on_errors & ON_ERRORS_RECOVER)) {
676 if (bh_primary)
677 brelse(bh_primary);
678 if (!silent)
679 ntfs_error(sb, "Mount option errors=recover not used. "
680 "Aborting without trying to recover.");
681 return NULL;
683 /* Try to read NT4+ backup boot sector. */
684 if ((bh_backup = sb_bread(sb, nr_blocks - 1))) {
685 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
686 bh_backup->b_data, silent))
687 goto hotfix_primary_boot_sector;
688 brelse(bh_backup);
689 } else if (!silent)
690 ntfs_error(sb, read_err_str, "backup");
691 /* Try to read NT3.51- backup boot sector. */
692 if ((bh_backup = sb_bread(sb, nr_blocks >> 1))) {
693 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
694 bh_backup->b_data, silent))
695 goto hotfix_primary_boot_sector;
696 if (!silent)
697 ntfs_error(sb, "Could not find a valid backup boot "
698 "sector.");
699 brelse(bh_backup);
700 } else if (!silent)
701 ntfs_error(sb, read_err_str, "backup");
702 /* We failed. Cleanup and return. */
703 if (bh_primary)
704 brelse(bh_primary);
705 return NULL;
706 hotfix_primary_boot_sector:
707 if (bh_primary) {
709 * If we managed to read sector zero and the volume is not
710 * read-only, copy the found, valid backup boot sector to the
711 * primary boot sector. Note we only copy the actual boot
712 * sector structure, not the actual whole device sector as that
713 * may be bigger and would potentially damage the $Boot system
714 * file (FIXME: Would be nice to know if the backup boot sector
715 * on a large sector device contains the whole boot loader or
716 * just the first 512 bytes).
718 if (!(sb->s_flags & MS_RDONLY)) {
719 ntfs_warning(sb, "Hot-fix: Recovering invalid primary "
720 "boot sector from backup copy.");
721 memcpy(bh_primary->b_data, bh_backup->b_data,
722 NTFS_BLOCK_SIZE);
723 mark_buffer_dirty(bh_primary);
724 sync_dirty_buffer(bh_primary);
725 if (buffer_uptodate(bh_primary)) {
726 brelse(bh_backup);
727 return bh_primary;
729 ntfs_error(sb, "Hot-fix: Device write error while "
730 "recovering primary boot sector.");
731 } else {
732 ntfs_warning(sb, "Hot-fix: Recovery of primary boot "
733 "sector failed: Read-only mount.");
735 brelse(bh_primary);
737 ntfs_warning(sb, "Using backup boot sector.");
738 return bh_backup;
742 * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol
743 * @vol: volume structure to initialise with data from boot sector
744 * @b: boot sector to parse
746 * Parse the ntfs boot sector @b and store all imporant information therein in
747 * the ntfs super block @vol. Return 'true' on success and 'false' on error.
749 static bool parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
751 unsigned int sectors_per_cluster_bits, nr_hidden_sects;
752 int clusters_per_mft_record, clusters_per_index_record;
753 s64 ll;
755 vol->sector_size = le16_to_cpu(b->bpb.bytes_per_sector);
756 vol->sector_size_bits = ffs(vol->sector_size) - 1;
757 ntfs_debug("vol->sector_size = %i (0x%x)", vol->sector_size,
758 vol->sector_size);
759 ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits,
760 vol->sector_size_bits);
761 if (vol->sector_size < vol->sb->s_blocksize) {
762 ntfs_error(vol->sb, "Sector size (%i) is smaller than the "
763 "device block size (%lu). This is not "
764 "supported. Sorry.", vol->sector_size,
765 vol->sb->s_blocksize);
766 return false;
768 ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster);
769 sectors_per_cluster_bits = ffs(b->bpb.sectors_per_cluster) - 1;
770 ntfs_debug("sectors_per_cluster_bits = 0x%x",
771 sectors_per_cluster_bits);
772 nr_hidden_sects = le32_to_cpu(b->bpb.hidden_sectors);
773 ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects);
774 vol->cluster_size = vol->sector_size << sectors_per_cluster_bits;
775 vol->cluster_size_mask = vol->cluster_size - 1;
776 vol->cluster_size_bits = ffs(vol->cluster_size) - 1;
777 ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size,
778 vol->cluster_size);
779 ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask);
780 ntfs_debug("vol->cluster_size_bits = %i", vol->cluster_size_bits);
781 if (vol->cluster_size < vol->sector_size) {
782 ntfs_error(vol->sb, "Cluster size (%i) is smaller than the "
783 "sector size (%i). This is not supported. "
784 "Sorry.", vol->cluster_size, vol->sector_size);
785 return false;
787 clusters_per_mft_record = b->clusters_per_mft_record;
788 ntfs_debug("clusters_per_mft_record = %i (0x%x)",
789 clusters_per_mft_record, clusters_per_mft_record);
790 if (clusters_per_mft_record > 0)
791 vol->mft_record_size = vol->cluster_size <<
792 (ffs(clusters_per_mft_record) - 1);
793 else
795 * When mft_record_size < cluster_size, clusters_per_mft_record
796 * = -log2(mft_record_size) bytes. mft_record_size normaly is
797 * 1024 bytes, which is encoded as 0xF6 (-10 in decimal).
799 vol->mft_record_size = 1 << -clusters_per_mft_record;
800 vol->mft_record_size_mask = vol->mft_record_size - 1;
801 vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1;
802 ntfs_debug("vol->mft_record_size = %i (0x%x)", vol->mft_record_size,
803 vol->mft_record_size);
804 ntfs_debug("vol->mft_record_size_mask = 0x%x",
805 vol->mft_record_size_mask);
806 ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
807 vol->mft_record_size_bits, vol->mft_record_size_bits);
809 * We cannot support mft record sizes above the PAGE_CACHE_SIZE since
810 * we store $MFT/$DATA, the table of mft records in the page cache.
812 if (vol->mft_record_size > PAGE_CACHE_SIZE) {
813 ntfs_error(vol->sb, "Mft record size (%i) exceeds the "
814 "PAGE_CACHE_SIZE on your system (%lu). "
815 "This is not supported. Sorry.",
816 vol->mft_record_size, PAGE_CACHE_SIZE);
817 return false;
819 /* We cannot support mft record sizes below the sector size. */
820 if (vol->mft_record_size < vol->sector_size) {
821 ntfs_error(vol->sb, "Mft record size (%i) is smaller than the "
822 "sector size (%i). This is not supported. "
823 "Sorry.", vol->mft_record_size,
824 vol->sector_size);
825 return false;
827 clusters_per_index_record = b->clusters_per_index_record;
828 ntfs_debug("clusters_per_index_record = %i (0x%x)",
829 clusters_per_index_record, clusters_per_index_record);
830 if (clusters_per_index_record > 0)
831 vol->index_record_size = vol->cluster_size <<
832 (ffs(clusters_per_index_record) - 1);
833 else
835 * When index_record_size < cluster_size,
836 * clusters_per_index_record = -log2(index_record_size) bytes.
837 * index_record_size normaly equals 4096 bytes, which is
838 * encoded as 0xF4 (-12 in decimal).
840 vol->index_record_size = 1 << -clusters_per_index_record;
841 vol->index_record_size_mask = vol->index_record_size - 1;
842 vol->index_record_size_bits = ffs(vol->index_record_size) - 1;
843 ntfs_debug("vol->index_record_size = %i (0x%x)",
844 vol->index_record_size, vol->index_record_size);
845 ntfs_debug("vol->index_record_size_mask = 0x%x",
846 vol->index_record_size_mask);
847 ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
848 vol->index_record_size_bits,
849 vol->index_record_size_bits);
850 /* We cannot support index record sizes below the sector size. */
851 if (vol->index_record_size < vol->sector_size) {
852 ntfs_error(vol->sb, "Index record size (%i) is smaller than "
853 "the sector size (%i). This is not "
854 "supported. Sorry.", vol->index_record_size,
855 vol->sector_size);
856 return false;
859 * Get the size of the volume in clusters and check for 64-bit-ness.
860 * Windows currently only uses 32 bits to save the clusters so we do
861 * the same as it is much faster on 32-bit CPUs.
863 ll = sle64_to_cpu(b->number_of_sectors) >> sectors_per_cluster_bits;
864 if ((u64)ll >= 1ULL << 32) {
865 ntfs_error(vol->sb, "Cannot handle 64-bit clusters. Sorry.");
866 return false;
868 vol->nr_clusters = ll;
869 ntfs_debug("vol->nr_clusters = 0x%llx", (long long)vol->nr_clusters);
871 * On an architecture where unsigned long is 32-bits, we restrict the
872 * volume size to 2TiB (2^41). On a 64-bit architecture, the compiler
873 * will hopefully optimize the whole check away.
875 if (sizeof(unsigned long) < 8) {
876 if ((ll << vol->cluster_size_bits) >= (1ULL << 41)) {
877 ntfs_error(vol->sb, "Volume size (%lluTiB) is too "
878 "large for this architecture. "
879 "Maximum supported is 2TiB. Sorry.",
880 (unsigned long long)ll >> (40 -
881 vol->cluster_size_bits));
882 return false;
885 ll = sle64_to_cpu(b->mft_lcn);
886 if (ll >= vol->nr_clusters) {
887 ntfs_error(vol->sb, "MFT LCN (%lli, 0x%llx) is beyond end of "
888 "volume. Weird.", (unsigned long long)ll,
889 (unsigned long long)ll);
890 return false;
892 vol->mft_lcn = ll;
893 ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol->mft_lcn);
894 ll = sle64_to_cpu(b->mftmirr_lcn);
895 if (ll >= vol->nr_clusters) {
896 ntfs_error(vol->sb, "MFTMirr LCN (%lli, 0x%llx) is beyond end "
897 "of volume. Weird.", (unsigned long long)ll,
898 (unsigned long long)ll);
899 return false;
901 vol->mftmirr_lcn = ll;
902 ntfs_debug("vol->mftmirr_lcn = 0x%llx", (long long)vol->mftmirr_lcn);
903 #ifdef NTFS_RW
905 * Work out the size of the mft mirror in number of mft records. If the
906 * cluster size is less than or equal to the size taken by four mft
907 * records, the mft mirror stores the first four mft records. If the
908 * cluster size is bigger than the size taken by four mft records, the
909 * mft mirror contains as many mft records as will fit into one
910 * cluster.
912 if (vol->cluster_size <= (4 << vol->mft_record_size_bits))
913 vol->mftmirr_size = 4;
914 else
915 vol->mftmirr_size = vol->cluster_size >>
916 vol->mft_record_size_bits;
917 ntfs_debug("vol->mftmirr_size = %i", vol->mftmirr_size);
918 #endif /* NTFS_RW */
919 vol->serial_no = le64_to_cpu(b->volume_serial_number);
920 ntfs_debug("vol->serial_no = 0x%llx",
921 (unsigned long long)vol->serial_no);
922 return true;
926 * ntfs_setup_allocators - initialize the cluster and mft allocators
927 * @vol: volume structure for which to setup the allocators
929 * Setup the cluster (lcn) and mft allocators to the starting values.
931 static void ntfs_setup_allocators(ntfs_volume *vol)
933 #ifdef NTFS_RW
934 LCN mft_zone_size, mft_lcn;
935 #endif /* NTFS_RW */
937 ntfs_debug("vol->mft_zone_multiplier = 0x%x",
938 vol->mft_zone_multiplier);
939 #ifdef NTFS_RW
940 /* Determine the size of the MFT zone. */
941 mft_zone_size = vol->nr_clusters;
942 switch (vol->mft_zone_multiplier) { /* % of volume size in clusters */
943 case 4:
944 mft_zone_size >>= 1; /* 50% */
945 break;
946 case 3:
947 mft_zone_size = (mft_zone_size +
948 (mft_zone_size >> 1)) >> 2; /* 37.5% */
949 break;
950 case 2:
951 mft_zone_size >>= 2; /* 25% */
952 break;
953 /* case 1: */
954 default:
955 mft_zone_size >>= 3; /* 12.5% */
956 break;
958 /* Setup the mft zone. */
959 vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn;
960 ntfs_debug("vol->mft_zone_pos = 0x%llx",
961 (unsigned long long)vol->mft_zone_pos);
963 * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
964 * source) and if the actual mft_lcn is in the expected place or even
965 * further to the front of the volume, extend the mft_zone to cover the
966 * beginning of the volume as well. This is in order to protect the
967 * area reserved for the mft bitmap as well within the mft_zone itself.
968 * On non-standard volumes we do not protect it as the overhead would
969 * be higher than the speed increase we would get by doing it.
971 mft_lcn = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size;
972 if (mft_lcn * vol->cluster_size < 16 * 1024)
973 mft_lcn = (16 * 1024 + vol->cluster_size - 1) /
974 vol->cluster_size;
975 if (vol->mft_zone_start <= mft_lcn)
976 vol->mft_zone_start = 0;
977 ntfs_debug("vol->mft_zone_start = 0x%llx",
978 (unsigned long long)vol->mft_zone_start);
980 * Need to cap the mft zone on non-standard volumes so that it does
981 * not point outside the boundaries of the volume. We do this by
982 * halving the zone size until we are inside the volume.
984 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
985 while (vol->mft_zone_end >= vol->nr_clusters) {
986 mft_zone_size >>= 1;
987 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
989 ntfs_debug("vol->mft_zone_end = 0x%llx",
990 (unsigned long long)vol->mft_zone_end);
992 * Set the current position within each data zone to the start of the
993 * respective zone.
995 vol->data1_zone_pos = vol->mft_zone_end;
996 ntfs_debug("vol->data1_zone_pos = 0x%llx",
997 (unsigned long long)vol->data1_zone_pos);
998 vol->data2_zone_pos = 0;
999 ntfs_debug("vol->data2_zone_pos = 0x%llx",
1000 (unsigned long long)vol->data2_zone_pos);
1002 /* Set the mft data allocation position to mft record 24. */
1003 vol->mft_data_pos = 24;
1004 ntfs_debug("vol->mft_data_pos = 0x%llx",
1005 (unsigned long long)vol->mft_data_pos);
1006 #endif /* NTFS_RW */
1009 #ifdef NTFS_RW
1012 * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume
1013 * @vol: ntfs super block describing device whose mft mirror to load
1015 * Return 'true' on success or 'false' on error.
1017 static bool load_and_init_mft_mirror(ntfs_volume *vol)
1019 struct inode *tmp_ino;
1020 ntfs_inode *tmp_ni;
1022 ntfs_debug("Entering.");
1023 /* Get mft mirror inode. */
1024 tmp_ino = ntfs_iget(vol->sb, FILE_MFTMirr);
1025 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1026 if (!IS_ERR(tmp_ino))
1027 iput(tmp_ino);
1028 /* Caller will display error message. */
1029 return false;
1032 * Re-initialize some specifics about $MFTMirr's inode as
1033 * ntfs_read_inode() will have set up the default ones.
1035 /* Set uid and gid to root. */
1036 tmp_ino->i_uid = tmp_ino->i_gid = 0;
1037 /* Regular file. No access for anyone. */
1038 tmp_ino->i_mode = S_IFREG;
1039 /* No VFS initiated operations allowed for $MFTMirr. */
1040 tmp_ino->i_op = &ntfs_empty_inode_ops;
1041 tmp_ino->i_fop = &ntfs_empty_file_ops;
1042 /* Put in our special address space operations. */
1043 tmp_ino->i_mapping->a_ops = &ntfs_mst_aops;
1044 tmp_ni = NTFS_I(tmp_ino);
1045 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
1046 NInoSetMstProtected(tmp_ni);
1047 NInoSetSparseDisabled(tmp_ni);
1049 * Set up our little cheat allowing us to reuse the async read io
1050 * completion handler for directories.
1052 tmp_ni->itype.index.block_size = vol->mft_record_size;
1053 tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits;
1054 vol->mftmirr_ino = tmp_ino;
1055 ntfs_debug("Done.");
1056 return true;
1060 * check_mft_mirror - compare contents of the mft mirror with the mft
1061 * @vol: ntfs super block describing device whose mft mirror to check
1063 * Return 'true' on success or 'false' on error.
1065 * Note, this function also results in the mft mirror runlist being completely
1066 * mapped into memory. The mft mirror write code requires this and will BUG()
1067 * should it find an unmapped runlist element.
1069 static bool check_mft_mirror(ntfs_volume *vol)
1071 struct super_block *sb = vol->sb;
1072 ntfs_inode *mirr_ni;
1073 struct page *mft_page, *mirr_page;
1074 u8 *kmft, *kmirr;
1075 runlist_element *rl, rl2[2];
1076 pgoff_t index;
1077 int mrecs_per_page, i;
1079 ntfs_debug("Entering.");
1080 /* Compare contents of $MFT and $MFTMirr. */
1081 mrecs_per_page = PAGE_CACHE_SIZE / vol->mft_record_size;
1082 BUG_ON(!mrecs_per_page);
1083 BUG_ON(!vol->mftmirr_size);
1084 mft_page = mirr_page = NULL;
1085 kmft = kmirr = NULL;
1086 index = i = 0;
1087 do {
1088 u32 bytes;
1090 /* Switch pages if necessary. */
1091 if (!(i % mrecs_per_page)) {
1092 if (index) {
1093 ntfs_unmap_page(mft_page);
1094 ntfs_unmap_page(mirr_page);
1096 /* Get the $MFT page. */
1097 mft_page = ntfs_map_page(vol->mft_ino->i_mapping,
1098 index);
1099 if (IS_ERR(mft_page)) {
1100 ntfs_error(sb, "Failed to read $MFT.");
1101 return false;
1103 kmft = page_address(mft_page);
1104 /* Get the $MFTMirr page. */
1105 mirr_page = ntfs_map_page(vol->mftmirr_ino->i_mapping,
1106 index);
1107 if (IS_ERR(mirr_page)) {
1108 ntfs_error(sb, "Failed to read $MFTMirr.");
1109 goto mft_unmap_out;
1111 kmirr = page_address(mirr_page);
1112 ++index;
1114 /* Do not check the record if it is not in use. */
1115 if (((MFT_RECORD*)kmft)->flags & MFT_RECORD_IN_USE) {
1116 /* Make sure the record is ok. */
1117 if (ntfs_is_baad_recordp((le32*)kmft)) {
1118 ntfs_error(sb, "Incomplete multi sector "
1119 "transfer detected in mft "
1120 "record %i.", i);
1121 mm_unmap_out:
1122 ntfs_unmap_page(mirr_page);
1123 mft_unmap_out:
1124 ntfs_unmap_page(mft_page);
1125 return false;
1128 /* Do not check the mirror record if it is not in use. */
1129 if (((MFT_RECORD*)kmirr)->flags & MFT_RECORD_IN_USE) {
1130 if (ntfs_is_baad_recordp((le32*)kmirr)) {
1131 ntfs_error(sb, "Incomplete multi sector "
1132 "transfer detected in mft "
1133 "mirror record %i.", i);
1134 goto mm_unmap_out;
1137 /* Get the amount of data in the current record. */
1138 bytes = le32_to_cpu(((MFT_RECORD*)kmft)->bytes_in_use);
1139 if (bytes < sizeof(MFT_RECORD_OLD) ||
1140 bytes > vol->mft_record_size ||
1141 ntfs_is_baad_recordp((le32*)kmft)) {
1142 bytes = le32_to_cpu(((MFT_RECORD*)kmirr)->bytes_in_use);
1143 if (bytes < sizeof(MFT_RECORD_OLD) ||
1144 bytes > vol->mft_record_size ||
1145 ntfs_is_baad_recordp((le32*)kmirr))
1146 bytes = vol->mft_record_size;
1148 /* Compare the two records. */
1149 if (memcmp(kmft, kmirr, bytes)) {
1150 ntfs_error(sb, "$MFT and $MFTMirr (record %i) do not "
1151 "match. Run ntfsfix or chkdsk.", i);
1152 goto mm_unmap_out;
1154 kmft += vol->mft_record_size;
1155 kmirr += vol->mft_record_size;
1156 } while (++i < vol->mftmirr_size);
1157 /* Release the last pages. */
1158 ntfs_unmap_page(mft_page);
1159 ntfs_unmap_page(mirr_page);
1161 /* Construct the mft mirror runlist by hand. */
1162 rl2[0].vcn = 0;
1163 rl2[0].lcn = vol->mftmirr_lcn;
1164 rl2[0].length = (vol->mftmirr_size * vol->mft_record_size +
1165 vol->cluster_size - 1) / vol->cluster_size;
1166 rl2[1].vcn = rl2[0].length;
1167 rl2[1].lcn = LCN_ENOENT;
1168 rl2[1].length = 0;
1170 * Because we have just read all of the mft mirror, we know we have
1171 * mapped the full runlist for it.
1173 mirr_ni = NTFS_I(vol->mftmirr_ino);
1174 down_read(&mirr_ni->runlist.lock);
1175 rl = mirr_ni->runlist.rl;
1176 /* Compare the two runlists. They must be identical. */
1177 i = 0;
1178 do {
1179 if (rl2[i].vcn != rl[i].vcn || rl2[i].lcn != rl[i].lcn ||
1180 rl2[i].length != rl[i].length) {
1181 ntfs_error(sb, "$MFTMirr location mismatch. "
1182 "Run chkdsk.");
1183 up_read(&mirr_ni->runlist.lock);
1184 return false;
1186 } while (rl2[i++].length);
1187 up_read(&mirr_ni->runlist.lock);
1188 ntfs_debug("Done.");
1189 return true;
1193 * load_and_check_logfile - load and check the logfile inode for a volume
1194 * @vol: ntfs super block describing device whose logfile to load
1196 * Return 'true' on success or 'false' on error.
1198 static bool load_and_check_logfile(ntfs_volume *vol,
1199 RESTART_PAGE_HEADER **rp)
1201 struct inode *tmp_ino;
1203 ntfs_debug("Entering.");
1204 tmp_ino = ntfs_iget(vol->sb, FILE_LogFile);
1205 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1206 if (!IS_ERR(tmp_ino))
1207 iput(tmp_ino);
1208 /* Caller will display error message. */
1209 return false;
1211 if (!ntfs_check_logfile(tmp_ino, rp)) {
1212 iput(tmp_ino);
1213 /* ntfs_check_logfile() will have displayed error output. */
1214 return false;
1216 NInoSetSparseDisabled(NTFS_I(tmp_ino));
1217 vol->logfile_ino = tmp_ino;
1218 ntfs_debug("Done.");
1219 return true;
1222 #define NTFS_HIBERFIL_HEADER_SIZE 4096
1225 * check_windows_hibernation_status - check if Windows is suspended on a volume
1226 * @vol: ntfs super block of device to check
1228 * Check if Windows is hibernated on the ntfs volume @vol. This is done by
1229 * looking for the file hiberfil.sys in the root directory of the volume. If
1230 * the file is not present Windows is definitely not suspended.
1232 * If hiberfil.sys exists and is less than 4kiB in size it means Windows is
1233 * definitely suspended (this volume is not the system volume). Caveat: on a
1234 * system with many volumes it is possible that the < 4kiB check is bogus but
1235 * for now this should do fine.
1237 * If hiberfil.sys exists and is larger than 4kiB in size, we need to read the
1238 * hiberfil header (which is the first 4kiB). If this begins with "hibr",
1239 * Windows is definitely suspended. If it is completely full of zeroes,
1240 * Windows is definitely not hibernated. Any other case is treated as if
1241 * Windows is suspended. This caters for the above mentioned caveat of a
1242 * system with many volumes where no "hibr" magic would be present and there is
1243 * no zero header.
1245 * Return 0 if Windows is not hibernated on the volume, >0 if Windows is
1246 * hibernated on the volume, and -errno on error.
1248 static int check_windows_hibernation_status(ntfs_volume *vol)
1250 MFT_REF mref;
1251 struct inode *vi;
1252 ntfs_inode *ni;
1253 struct page *page;
1254 u32 *kaddr, *kend;
1255 ntfs_name *name = NULL;
1256 int ret = 1;
1257 static const ntfschar hiberfil[13] = { cpu_to_le16('h'),
1258 cpu_to_le16('i'), cpu_to_le16('b'),
1259 cpu_to_le16('e'), cpu_to_le16('r'),
1260 cpu_to_le16('f'), cpu_to_le16('i'),
1261 cpu_to_le16('l'), cpu_to_le16('.'),
1262 cpu_to_le16('s'), cpu_to_le16('y'),
1263 cpu_to_le16('s'), 0 };
1265 ntfs_debug("Entering.");
1267 * Find the inode number for the hibernation file by looking up the
1268 * filename hiberfil.sys in the root directory.
1270 mutex_lock(&vol->root_ino->i_mutex);
1271 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->root_ino), hiberfil, 12,
1272 &name);
1273 mutex_unlock(&vol->root_ino->i_mutex);
1274 if (IS_ERR_MREF(mref)) {
1275 ret = MREF_ERR(mref);
1276 /* If the file does not exist, Windows is not hibernated. */
1277 if (ret == -ENOENT) {
1278 ntfs_debug("hiberfil.sys not present. Windows is not "
1279 "hibernated on the volume.");
1280 return 0;
1282 /* A real error occured. */
1283 ntfs_error(vol->sb, "Failed to find inode number for "
1284 "hiberfil.sys.");
1285 return ret;
1287 /* We do not care for the type of match that was found. */
1288 kfree(name);
1289 /* Get the inode. */
1290 vi = ntfs_iget(vol->sb, MREF(mref));
1291 if (IS_ERR(vi) || is_bad_inode(vi)) {
1292 if (!IS_ERR(vi))
1293 iput(vi);
1294 ntfs_error(vol->sb, "Failed to load hiberfil.sys.");
1295 return IS_ERR(vi) ? PTR_ERR(vi) : -EIO;
1297 if (unlikely(i_size_read(vi) < NTFS_HIBERFIL_HEADER_SIZE)) {
1298 ntfs_debug("hiberfil.sys is smaller than 4kiB (0x%llx). "
1299 "Windows is hibernated on the volume. This "
1300 "is not the system volume.", i_size_read(vi));
1301 goto iput_out;
1303 ni = NTFS_I(vi);
1304 page = ntfs_map_page(vi->i_mapping, 0);
1305 if (IS_ERR(page)) {
1306 ntfs_error(vol->sb, "Failed to read from hiberfil.sys.");
1307 ret = PTR_ERR(page);
1308 goto iput_out;
1310 kaddr = (u32*)page_address(page);
1311 if (*(le32*)kaddr == cpu_to_le32(0x72626968)/*'hibr'*/) {
1312 ntfs_debug("Magic \"hibr\" found in hiberfil.sys. Windows is "
1313 "hibernated on the volume. This is the "
1314 "system volume.");
1315 goto unm_iput_out;
1317 kend = kaddr + NTFS_HIBERFIL_HEADER_SIZE/sizeof(*kaddr);
1318 do {
1319 if (unlikely(*kaddr)) {
1320 ntfs_debug("hiberfil.sys is larger than 4kiB "
1321 "(0x%llx), does not contain the "
1322 "\"hibr\" magic, and does not have a "
1323 "zero header. Windows is hibernated "
1324 "on the volume. This is not the "
1325 "system volume.", i_size_read(vi));
1326 goto unm_iput_out;
1328 } while (++kaddr < kend);
1329 ntfs_debug("hiberfil.sys contains a zero header. Windows is not "
1330 "hibernated on the volume. This is the system "
1331 "volume.");
1332 ret = 0;
1333 unm_iput_out:
1334 ntfs_unmap_page(page);
1335 iput_out:
1336 iput(vi);
1337 return ret;
1341 * load_and_init_quota - load and setup the quota file for a volume if present
1342 * @vol: ntfs super block describing device whose quota file to load
1344 * Return 'true' on success or 'false' on error. If $Quota is not present, we
1345 * leave vol->quota_ino as NULL and return success.
1347 static bool load_and_init_quota(ntfs_volume *vol)
1349 MFT_REF mref;
1350 struct inode *tmp_ino;
1351 ntfs_name *name = NULL;
1352 static const ntfschar Quota[7] = { cpu_to_le16('$'),
1353 cpu_to_le16('Q'), cpu_to_le16('u'),
1354 cpu_to_le16('o'), cpu_to_le16('t'),
1355 cpu_to_le16('a'), 0 };
1356 static ntfschar Q[3] = { cpu_to_le16('$'),
1357 cpu_to_le16('Q'), 0 };
1359 ntfs_debug("Entering.");
1361 * Find the inode number for the quota file by looking up the filename
1362 * $Quota in the extended system files directory $Extend.
1364 mutex_lock(&vol->extend_ino->i_mutex);
1365 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), Quota, 6,
1366 &name);
1367 mutex_unlock(&vol->extend_ino->i_mutex);
1368 if (IS_ERR_MREF(mref)) {
1370 * If the file does not exist, quotas are disabled and have
1371 * never been enabled on this volume, just return success.
1373 if (MREF_ERR(mref) == -ENOENT) {
1374 ntfs_debug("$Quota not present. Volume does not have "
1375 "quotas enabled.");
1377 * No need to try to set quotas out of date if they are
1378 * not enabled.
1380 NVolSetQuotaOutOfDate(vol);
1381 return true;
1383 /* A real error occured. */
1384 ntfs_error(vol->sb, "Failed to find inode number for $Quota.");
1385 return false;
1387 /* We do not care for the type of match that was found. */
1388 kfree(name);
1389 /* Get the inode. */
1390 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1391 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1392 if (!IS_ERR(tmp_ino))
1393 iput(tmp_ino);
1394 ntfs_error(vol->sb, "Failed to load $Quota.");
1395 return false;
1397 vol->quota_ino = tmp_ino;
1398 /* Get the $Q index allocation attribute. */
1399 tmp_ino = ntfs_index_iget(vol->quota_ino, Q, 2);
1400 if (IS_ERR(tmp_ino)) {
1401 ntfs_error(vol->sb, "Failed to load $Quota/$Q index.");
1402 return false;
1404 vol->quota_q_ino = tmp_ino;
1405 ntfs_debug("Done.");
1406 return true;
1410 * load_and_init_usnjrnl - load and setup the transaction log if present
1411 * @vol: ntfs super block describing device whose usnjrnl file to load
1413 * Return 'true' on success or 'false' on error.
1415 * If $UsnJrnl is not present or in the process of being disabled, we set
1416 * NVolUsnJrnlStamped() and return success.
1418 * If the $UsnJrnl $DATA/$J attribute has a size equal to the lowest valid usn,
1419 * i.e. transaction logging has only just been enabled or the journal has been
1420 * stamped and nothing has been logged since, we also set NVolUsnJrnlStamped()
1421 * and return success.
1423 static bool load_and_init_usnjrnl(ntfs_volume *vol)
1425 MFT_REF mref;
1426 struct inode *tmp_ino;
1427 ntfs_inode *tmp_ni;
1428 struct page *page;
1429 ntfs_name *name = NULL;
1430 USN_HEADER *uh;
1431 static const ntfschar UsnJrnl[9] = { cpu_to_le16('$'),
1432 cpu_to_le16('U'), cpu_to_le16('s'),
1433 cpu_to_le16('n'), cpu_to_le16('J'),
1434 cpu_to_le16('r'), cpu_to_le16('n'),
1435 cpu_to_le16('l'), 0 };
1436 static ntfschar Max[5] = { cpu_to_le16('$'),
1437 cpu_to_le16('M'), cpu_to_le16('a'),
1438 cpu_to_le16('x'), 0 };
1439 static ntfschar J[3] = { cpu_to_le16('$'),
1440 cpu_to_le16('J'), 0 };
1442 ntfs_debug("Entering.");
1444 * Find the inode number for the transaction log file by looking up the
1445 * filename $UsnJrnl in the extended system files directory $Extend.
1447 mutex_lock(&vol->extend_ino->i_mutex);
1448 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), UsnJrnl, 8,
1449 &name);
1450 mutex_unlock(&vol->extend_ino->i_mutex);
1451 if (IS_ERR_MREF(mref)) {
1453 * If the file does not exist, transaction logging is disabled,
1454 * just return success.
1456 if (MREF_ERR(mref) == -ENOENT) {
1457 ntfs_debug("$UsnJrnl not present. Volume does not "
1458 "have transaction logging enabled.");
1459 not_enabled:
1461 * No need to try to stamp the transaction log if
1462 * transaction logging is not enabled.
1464 NVolSetUsnJrnlStamped(vol);
1465 return true;
1467 /* A real error occured. */
1468 ntfs_error(vol->sb, "Failed to find inode number for "
1469 "$UsnJrnl.");
1470 return false;
1472 /* We do not care for the type of match that was found. */
1473 kfree(name);
1474 /* Get the inode. */
1475 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1476 if (unlikely(IS_ERR(tmp_ino) || is_bad_inode(tmp_ino))) {
1477 if (!IS_ERR(tmp_ino))
1478 iput(tmp_ino);
1479 ntfs_error(vol->sb, "Failed to load $UsnJrnl.");
1480 return false;
1482 vol->usnjrnl_ino = tmp_ino;
1484 * If the transaction log is in the process of being deleted, we can
1485 * ignore it.
1487 if (unlikely(vol->vol_flags & VOLUME_DELETE_USN_UNDERWAY)) {
1488 ntfs_debug("$UsnJrnl in the process of being disabled. "
1489 "Volume does not have transaction logging "
1490 "enabled.");
1491 goto not_enabled;
1493 /* Get the $DATA/$Max attribute. */
1494 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, Max, 4);
1495 if (IS_ERR(tmp_ino)) {
1496 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$Max "
1497 "attribute.");
1498 return false;
1500 vol->usnjrnl_max_ino = tmp_ino;
1501 if (unlikely(i_size_read(tmp_ino) < sizeof(USN_HEADER))) {
1502 ntfs_error(vol->sb, "Found corrupt $UsnJrnl/$DATA/$Max "
1503 "attribute (size is 0x%llx but should be at "
1504 "least 0x%zx bytes).", i_size_read(tmp_ino),
1505 sizeof(USN_HEADER));
1506 return false;
1508 /* Get the $DATA/$J attribute. */
1509 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, J, 2);
1510 if (IS_ERR(tmp_ino)) {
1511 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$J "
1512 "attribute.");
1513 return false;
1515 vol->usnjrnl_j_ino = tmp_ino;
1516 /* Verify $J is non-resident and sparse. */
1517 tmp_ni = NTFS_I(vol->usnjrnl_j_ino);
1518 if (unlikely(!NInoNonResident(tmp_ni) || !NInoSparse(tmp_ni))) {
1519 ntfs_error(vol->sb, "$UsnJrnl/$DATA/$J attribute is resident "
1520 "and/or not sparse.");
1521 return false;
1523 /* Read the USN_HEADER from $DATA/$Max. */
1524 page = ntfs_map_page(vol->usnjrnl_max_ino->i_mapping, 0);
1525 if (IS_ERR(page)) {
1526 ntfs_error(vol->sb, "Failed to read from $UsnJrnl/$DATA/$Max "
1527 "attribute.");
1528 return false;
1530 uh = (USN_HEADER*)page_address(page);
1531 /* Sanity check the $Max. */
1532 if (unlikely(sle64_to_cpu(uh->allocation_delta) >
1533 sle64_to_cpu(uh->maximum_size))) {
1534 ntfs_error(vol->sb, "Allocation delta (0x%llx) exceeds "
1535 "maximum size (0x%llx). $UsnJrnl is corrupt.",
1536 (long long)sle64_to_cpu(uh->allocation_delta),
1537 (long long)sle64_to_cpu(uh->maximum_size));
1538 ntfs_unmap_page(page);
1539 return false;
1542 * If the transaction log has been stamped and nothing has been written
1543 * to it since, we do not need to stamp it.
1545 if (unlikely(sle64_to_cpu(uh->lowest_valid_usn) >=
1546 i_size_read(vol->usnjrnl_j_ino))) {
1547 if (likely(sle64_to_cpu(uh->lowest_valid_usn) ==
1548 i_size_read(vol->usnjrnl_j_ino))) {
1549 ntfs_unmap_page(page);
1550 ntfs_debug("$UsnJrnl is enabled but nothing has been "
1551 "logged since it was last stamped. "
1552 "Treating this as if the volume does "
1553 "not have transaction logging "
1554 "enabled.");
1555 goto not_enabled;
1557 ntfs_error(vol->sb, "$UsnJrnl has lowest valid usn (0x%llx) "
1558 "which is out of bounds (0x%llx). $UsnJrnl "
1559 "is corrupt.",
1560 (long long)sle64_to_cpu(uh->lowest_valid_usn),
1561 i_size_read(vol->usnjrnl_j_ino));
1562 ntfs_unmap_page(page);
1563 return false;
1565 ntfs_unmap_page(page);
1566 ntfs_debug("Done.");
1567 return true;
1571 * load_and_init_attrdef - load the attribute definitions table for a volume
1572 * @vol: ntfs super block describing device whose attrdef to load
1574 * Return 'true' on success or 'false' on error.
1576 static bool load_and_init_attrdef(ntfs_volume *vol)
1578 loff_t i_size;
1579 struct super_block *sb = vol->sb;
1580 struct inode *ino;
1581 struct page *page;
1582 pgoff_t index, max_index;
1583 unsigned int size;
1585 ntfs_debug("Entering.");
1586 /* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */
1587 ino = ntfs_iget(sb, FILE_AttrDef);
1588 if (IS_ERR(ino) || is_bad_inode(ino)) {
1589 if (!IS_ERR(ino))
1590 iput(ino);
1591 goto failed;
1593 NInoSetSparseDisabled(NTFS_I(ino));
1594 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
1595 i_size = i_size_read(ino);
1596 if (i_size <= 0 || i_size > 0x7fffffff)
1597 goto iput_failed;
1598 vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(i_size);
1599 if (!vol->attrdef)
1600 goto iput_failed;
1601 index = 0;
1602 max_index = i_size >> PAGE_CACHE_SHIFT;
1603 size = PAGE_CACHE_SIZE;
1604 while (index < max_index) {
1605 /* Read the attrdef table and copy it into the linear buffer. */
1606 read_partial_attrdef_page:
1607 page = ntfs_map_page(ino->i_mapping, index);
1608 if (IS_ERR(page))
1609 goto free_iput_failed;
1610 memcpy((u8*)vol->attrdef + (index++ << PAGE_CACHE_SHIFT),
1611 page_address(page), size);
1612 ntfs_unmap_page(page);
1614 if (size == PAGE_CACHE_SIZE) {
1615 size = i_size & ~PAGE_CACHE_MASK;
1616 if (size)
1617 goto read_partial_attrdef_page;
1619 vol->attrdef_size = i_size;
1620 ntfs_debug("Read %llu bytes from $AttrDef.", i_size);
1621 iput(ino);
1622 return true;
1623 free_iput_failed:
1624 ntfs_free(vol->attrdef);
1625 vol->attrdef = NULL;
1626 iput_failed:
1627 iput(ino);
1628 failed:
1629 ntfs_error(sb, "Failed to initialize attribute definition table.");
1630 return false;
1633 #endif /* NTFS_RW */
1636 * load_and_init_upcase - load the upcase table for an ntfs volume
1637 * @vol: ntfs super block describing device whose upcase to load
1639 * Return 'true' on success or 'false' on error.
1641 static bool load_and_init_upcase(ntfs_volume *vol)
1643 loff_t i_size;
1644 struct super_block *sb = vol->sb;
1645 struct inode *ino;
1646 struct page *page;
1647 pgoff_t index, max_index;
1648 unsigned int size;
1649 int i, max;
1651 ntfs_debug("Entering.");
1652 /* Read upcase table and setup vol->upcase and vol->upcase_len. */
1653 ino = ntfs_iget(sb, FILE_UpCase);
1654 if (IS_ERR(ino) || is_bad_inode(ino)) {
1655 if (!IS_ERR(ino))
1656 iput(ino);
1657 goto upcase_failed;
1660 * The upcase size must not be above 64k Unicode characters, must not
1661 * be zero and must be a multiple of sizeof(ntfschar).
1663 i_size = i_size_read(ino);
1664 if (!i_size || i_size & (sizeof(ntfschar) - 1) ||
1665 i_size > 64ULL * 1024 * sizeof(ntfschar))
1666 goto iput_upcase_failed;
1667 vol->upcase = (ntfschar*)ntfs_malloc_nofs(i_size);
1668 if (!vol->upcase)
1669 goto iput_upcase_failed;
1670 index = 0;
1671 max_index = i_size >> PAGE_CACHE_SHIFT;
1672 size = PAGE_CACHE_SIZE;
1673 while (index < max_index) {
1674 /* Read the upcase table and copy it into the linear buffer. */
1675 read_partial_upcase_page:
1676 page = ntfs_map_page(ino->i_mapping, index);
1677 if (IS_ERR(page))
1678 goto iput_upcase_failed;
1679 memcpy((char*)vol->upcase + (index++ << PAGE_CACHE_SHIFT),
1680 page_address(page), size);
1681 ntfs_unmap_page(page);
1683 if (size == PAGE_CACHE_SIZE) {
1684 size = i_size & ~PAGE_CACHE_MASK;
1685 if (size)
1686 goto read_partial_upcase_page;
1688 vol->upcase_len = i_size >> UCHAR_T_SIZE_BITS;
1689 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
1690 i_size, 64 * 1024 * sizeof(ntfschar));
1691 iput(ino);
1692 mutex_lock(&ntfs_lock);
1693 if (!default_upcase) {
1694 ntfs_debug("Using volume specified $UpCase since default is "
1695 "not present.");
1696 mutex_unlock(&ntfs_lock);
1697 return true;
1699 max = default_upcase_len;
1700 if (max > vol->upcase_len)
1701 max = vol->upcase_len;
1702 for (i = 0; i < max; i++)
1703 if (vol->upcase[i] != default_upcase[i])
1704 break;
1705 if (i == max) {
1706 ntfs_free(vol->upcase);
1707 vol->upcase = default_upcase;
1708 vol->upcase_len = max;
1709 ntfs_nr_upcase_users++;
1710 mutex_unlock(&ntfs_lock);
1711 ntfs_debug("Volume specified $UpCase matches default. Using "
1712 "default.");
1713 return true;
1715 mutex_unlock(&ntfs_lock);
1716 ntfs_debug("Using volume specified $UpCase since it does not match "
1717 "the default.");
1718 return true;
1719 iput_upcase_failed:
1720 iput(ino);
1721 ntfs_free(vol->upcase);
1722 vol->upcase = NULL;
1723 upcase_failed:
1724 mutex_lock(&ntfs_lock);
1725 if (default_upcase) {
1726 vol->upcase = default_upcase;
1727 vol->upcase_len = default_upcase_len;
1728 ntfs_nr_upcase_users++;
1729 mutex_unlock(&ntfs_lock);
1730 ntfs_error(sb, "Failed to load $UpCase from the volume. Using "
1731 "default.");
1732 return true;
1734 mutex_unlock(&ntfs_lock);
1735 ntfs_error(sb, "Failed to initialize upcase table.");
1736 return false;
1740 * The lcn and mft bitmap inodes are NTFS-internal inodes with
1741 * their own special locking rules:
1743 static struct lock_class_key
1744 lcnbmp_runlist_lock_key, lcnbmp_mrec_lock_key,
1745 mftbmp_runlist_lock_key, mftbmp_mrec_lock_key;
1748 * load_system_files - open the system files using normal functions
1749 * @vol: ntfs super block describing device whose system files to load
1751 * Open the system files with normal access functions and complete setting up
1752 * the ntfs super block @vol.
1754 * Return 'true' on success or 'false' on error.
1756 static bool load_system_files(ntfs_volume *vol)
1758 struct super_block *sb = vol->sb;
1759 MFT_RECORD *m;
1760 VOLUME_INFORMATION *vi;
1761 ntfs_attr_search_ctx *ctx;
1762 #ifdef NTFS_RW
1763 RESTART_PAGE_HEADER *rp;
1764 int err;
1765 #endif /* NTFS_RW */
1767 ntfs_debug("Entering.");
1768 #ifdef NTFS_RW
1769 /* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */
1770 if (!load_and_init_mft_mirror(vol) || !check_mft_mirror(vol)) {
1771 static const char *es1 = "Failed to load $MFTMirr";
1772 static const char *es2 = "$MFTMirr does not match $MFT";
1773 static const char *es3 = ". Run ntfsfix and/or chkdsk.";
1775 /* If a read-write mount, convert it to a read-only mount. */
1776 if (!(sb->s_flags & MS_RDONLY)) {
1777 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1778 ON_ERRORS_CONTINUE))) {
1779 ntfs_error(sb, "%s and neither on_errors="
1780 "continue nor on_errors="
1781 "remount-ro was specified%s",
1782 !vol->mftmirr_ino ? es1 : es2,
1783 es3);
1784 goto iput_mirr_err_out;
1786 sb->s_flags |= MS_RDONLY;
1787 ntfs_error(sb, "%s. Mounting read-only%s",
1788 !vol->mftmirr_ino ? es1 : es2, es3);
1789 } else
1790 ntfs_warning(sb, "%s. Will not be able to remount "
1791 "read-write%s",
1792 !vol->mftmirr_ino ? es1 : es2, es3);
1793 /* This will prevent a read-write remount. */
1794 NVolSetErrors(vol);
1796 #endif /* NTFS_RW */
1797 /* Get mft bitmap attribute inode. */
1798 vol->mftbmp_ino = ntfs_attr_iget(vol->mft_ino, AT_BITMAP, NULL, 0);
1799 if (IS_ERR(vol->mftbmp_ino)) {
1800 ntfs_error(sb, "Failed to load $MFT/$BITMAP attribute.");
1801 goto iput_mirr_err_out;
1803 lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->runlist.lock,
1804 &mftbmp_runlist_lock_key);
1805 lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->mrec_lock,
1806 &mftbmp_mrec_lock_key);
1807 /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */
1808 if (!load_and_init_upcase(vol))
1809 goto iput_mftbmp_err_out;
1810 #ifdef NTFS_RW
1812 * Read attribute definitions table and setup @vol->attrdef and
1813 * @vol->attrdef_size.
1815 if (!load_and_init_attrdef(vol))
1816 goto iput_upcase_err_out;
1817 #endif /* NTFS_RW */
1819 * Get the cluster allocation bitmap inode and verify the size, no
1820 * need for any locking at this stage as we are already running
1821 * exclusively as we are mount in progress task.
1823 vol->lcnbmp_ino = ntfs_iget(sb, FILE_Bitmap);
1824 if (IS_ERR(vol->lcnbmp_ino) || is_bad_inode(vol->lcnbmp_ino)) {
1825 if (!IS_ERR(vol->lcnbmp_ino))
1826 iput(vol->lcnbmp_ino);
1827 goto bitmap_failed;
1829 lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->runlist.lock,
1830 &lcnbmp_runlist_lock_key);
1831 lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->mrec_lock,
1832 &lcnbmp_mrec_lock_key);
1834 NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino));
1835 if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) {
1836 iput(vol->lcnbmp_ino);
1837 bitmap_failed:
1838 ntfs_error(sb, "Failed to load $Bitmap.");
1839 goto iput_attrdef_err_out;
1842 * Get the volume inode and setup our cache of the volume flags and
1843 * version.
1845 vol->vol_ino = ntfs_iget(sb, FILE_Volume);
1846 if (IS_ERR(vol->vol_ino) || is_bad_inode(vol->vol_ino)) {
1847 if (!IS_ERR(vol->vol_ino))
1848 iput(vol->vol_ino);
1849 volume_failed:
1850 ntfs_error(sb, "Failed to load $Volume.");
1851 goto iput_lcnbmp_err_out;
1853 m = map_mft_record(NTFS_I(vol->vol_ino));
1854 if (IS_ERR(m)) {
1855 iput_volume_failed:
1856 iput(vol->vol_ino);
1857 goto volume_failed;
1859 if (!(ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m))) {
1860 ntfs_error(sb, "Failed to get attribute search context.");
1861 goto get_ctx_vol_failed;
1863 if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
1864 ctx) || ctx->attr->non_resident || ctx->attr->flags) {
1865 err_put_vol:
1866 ntfs_attr_put_search_ctx(ctx);
1867 get_ctx_vol_failed:
1868 unmap_mft_record(NTFS_I(vol->vol_ino));
1869 goto iput_volume_failed;
1871 vi = (VOLUME_INFORMATION*)((char*)ctx->attr +
1872 le16_to_cpu(ctx->attr->data.resident.value_offset));
1873 /* Some bounds checks. */
1874 if ((u8*)vi < (u8*)ctx->attr || (u8*)vi +
1875 le32_to_cpu(ctx->attr->data.resident.value_length) >
1876 (u8*)ctx->attr + le32_to_cpu(ctx->attr->length))
1877 goto err_put_vol;
1878 /* Copy the volume flags and version to the ntfs_volume structure. */
1879 vol->vol_flags = vi->flags;
1880 vol->major_ver = vi->major_ver;
1881 vol->minor_ver = vi->minor_ver;
1882 ntfs_attr_put_search_ctx(ctx);
1883 unmap_mft_record(NTFS_I(vol->vol_ino));
1884 printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver,
1885 vol->minor_ver);
1886 if (vol->major_ver < 3 && NVolSparseEnabled(vol)) {
1887 ntfs_warning(vol->sb, "Disabling sparse support due to NTFS "
1888 "volume version %i.%i (need at least version "
1889 "3.0).", vol->major_ver, vol->minor_ver);
1890 NVolClearSparseEnabled(vol);
1892 #ifdef NTFS_RW
1893 /* Make sure that no unsupported volume flags are set. */
1894 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
1895 static const char *es1a = "Volume is dirty";
1896 static const char *es1b = "Volume has been modified by chkdsk";
1897 static const char *es1c = "Volume has unsupported flags set";
1898 static const char *es2a = ". Run chkdsk and mount in Windows.";
1899 static const char *es2b = ". Mount in Windows.";
1900 const char *es1, *es2;
1902 es2 = es2a;
1903 if (vol->vol_flags & VOLUME_IS_DIRTY)
1904 es1 = es1a;
1905 else if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
1906 es1 = es1b;
1907 es2 = es2b;
1908 } else {
1909 es1 = es1c;
1910 ntfs_warning(sb, "Unsupported volume flags 0x%x "
1911 "encountered.",
1912 (unsigned)le16_to_cpu(vol->vol_flags));
1914 /* If a read-write mount, convert it to a read-only mount. */
1915 if (!(sb->s_flags & MS_RDONLY)) {
1916 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1917 ON_ERRORS_CONTINUE))) {
1918 ntfs_error(sb, "%s and neither on_errors="
1919 "continue nor on_errors="
1920 "remount-ro was specified%s",
1921 es1, es2);
1922 goto iput_vol_err_out;
1924 sb->s_flags |= MS_RDONLY;
1925 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1926 } else
1927 ntfs_warning(sb, "%s. Will not be able to remount "
1928 "read-write%s", es1, es2);
1930 * Do not set NVolErrors() because ntfs_remount() re-checks the
1931 * flags which we need to do in case any flags have changed.
1935 * Get the inode for the logfile, check it and determine if the volume
1936 * was shutdown cleanly.
1938 rp = NULL;
1939 if (!load_and_check_logfile(vol, &rp) ||
1940 !ntfs_is_logfile_clean(vol->logfile_ino, rp)) {
1941 static const char *es1a = "Failed to load $LogFile";
1942 static const char *es1b = "$LogFile is not clean";
1943 static const char *es2 = ". Mount in Windows.";
1944 const char *es1;
1946 es1 = !vol->logfile_ino ? es1a : es1b;
1947 /* If a read-write mount, convert it to a read-only mount. */
1948 if (!(sb->s_flags & MS_RDONLY)) {
1949 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1950 ON_ERRORS_CONTINUE))) {
1951 ntfs_error(sb, "%s and neither on_errors="
1952 "continue nor on_errors="
1953 "remount-ro was specified%s",
1954 es1, es2);
1955 if (vol->logfile_ino) {
1956 BUG_ON(!rp);
1957 ntfs_free(rp);
1959 goto iput_logfile_err_out;
1961 sb->s_flags |= MS_RDONLY;
1962 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1963 } else
1964 ntfs_warning(sb, "%s. Will not be able to remount "
1965 "read-write%s", es1, es2);
1966 /* This will prevent a read-write remount. */
1967 NVolSetErrors(vol);
1969 ntfs_free(rp);
1970 #endif /* NTFS_RW */
1971 /* Get the root directory inode so we can do path lookups. */
1972 vol->root_ino = ntfs_iget(sb, FILE_root);
1973 if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) {
1974 if (!IS_ERR(vol->root_ino))
1975 iput(vol->root_ino);
1976 ntfs_error(sb, "Failed to load root directory.");
1977 goto iput_logfile_err_out;
1979 #ifdef NTFS_RW
1981 * Check if Windows is suspended to disk on the target volume. If it
1982 * is hibernated, we must not write *anything* to the disk so set
1983 * NVolErrors() without setting the dirty volume flag and mount
1984 * read-only. This will prevent read-write remounting and it will also
1985 * prevent all writes.
1987 err = check_windows_hibernation_status(vol);
1988 if (unlikely(err)) {
1989 static const char *es1a = "Failed to determine if Windows is "
1990 "hibernated";
1991 static const char *es1b = "Windows is hibernated";
1992 static const char *es2 = ". Run chkdsk.";
1993 const char *es1;
1995 es1 = err < 0 ? es1a : es1b;
1996 /* If a read-write mount, convert it to a read-only mount. */
1997 if (!(sb->s_flags & MS_RDONLY)) {
1998 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1999 ON_ERRORS_CONTINUE))) {
2000 ntfs_error(sb, "%s and neither on_errors="
2001 "continue nor on_errors="
2002 "remount-ro was specified%s",
2003 es1, es2);
2004 goto iput_root_err_out;
2006 sb->s_flags |= MS_RDONLY;
2007 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2008 } else
2009 ntfs_warning(sb, "%s. Will not be able to remount "
2010 "read-write%s", es1, es2);
2011 /* This will prevent a read-write remount. */
2012 NVolSetErrors(vol);
2014 /* If (still) a read-write mount, mark the volume dirty. */
2015 if (!(sb->s_flags & MS_RDONLY) &&
2016 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
2017 static const char *es1 = "Failed to set dirty bit in volume "
2018 "information flags";
2019 static const char *es2 = ". Run chkdsk.";
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;
2032 * Do not set NVolErrors() because ntfs_remount() might manage
2033 * to set the dirty flag in which case all would be well.
2036 #if 0
2037 // TODO: Enable this code once we start modifying anything that is
2038 // different between NTFS 1.2 and 3.x...
2040 * If (still) a read-write mount, set the NT4 compatibility flag on
2041 * newer NTFS version volumes.
2043 if (!(sb->s_flags & MS_RDONLY) && (vol->major_ver > 1) &&
2044 ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
2045 static const char *es1 = "Failed to set NT4 compatibility flag";
2046 static const char *es2 = ". Run chkdsk.";
2048 /* Convert to a read-only mount. */
2049 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2050 ON_ERRORS_CONTINUE))) {
2051 ntfs_error(sb, "%s and neither on_errors=continue nor "
2052 "on_errors=remount-ro was specified%s",
2053 es1, es2);
2054 goto iput_root_err_out;
2056 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2057 sb->s_flags |= MS_RDONLY;
2058 NVolSetErrors(vol);
2060 #endif
2061 /* If (still) a read-write mount, empty the logfile. */
2062 if (!(sb->s_flags & MS_RDONLY) &&
2063 !ntfs_empty_logfile(vol->logfile_ino)) {
2064 static const char *es1 = "Failed to empty $LogFile";
2065 static const char *es2 = ". Mount in Windows.";
2067 /* Convert to a read-only mount. */
2068 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2069 ON_ERRORS_CONTINUE))) {
2070 ntfs_error(sb, "%s and neither on_errors=continue nor "
2071 "on_errors=remount-ro was specified%s",
2072 es1, es2);
2073 goto iput_root_err_out;
2075 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2076 sb->s_flags |= MS_RDONLY;
2077 NVolSetErrors(vol);
2079 #endif /* NTFS_RW */
2080 /* If on NTFS versions before 3.0, we are done. */
2081 if (unlikely(vol->major_ver < 3))
2082 return true;
2083 /* NTFS 3.0+ specific initialization. */
2084 /* Get the security descriptors inode. */
2085 vol->secure_ino = ntfs_iget(sb, FILE_Secure);
2086 if (IS_ERR(vol->secure_ino) || is_bad_inode(vol->secure_ino)) {
2087 if (!IS_ERR(vol->secure_ino))
2088 iput(vol->secure_ino);
2089 ntfs_error(sb, "Failed to load $Secure.");
2090 goto iput_root_err_out;
2092 // TODO: Initialize security.
2093 /* Get the extended system files' directory inode. */
2094 vol->extend_ino = ntfs_iget(sb, FILE_Extend);
2095 if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) {
2096 if (!IS_ERR(vol->extend_ino))
2097 iput(vol->extend_ino);
2098 ntfs_error(sb, "Failed to load $Extend.");
2099 goto iput_sec_err_out;
2101 #ifdef NTFS_RW
2102 /* Find the quota file, load it if present, and set it up. */
2103 if (!load_and_init_quota(vol)) {
2104 static const char *es1 = "Failed to load $Quota";
2105 static const char *es2 = ". Run chkdsk.";
2107 /* If a read-write mount, convert it to a read-only mount. */
2108 if (!(sb->s_flags & MS_RDONLY)) {
2109 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2110 ON_ERRORS_CONTINUE))) {
2111 ntfs_error(sb, "%s and neither on_errors="
2112 "continue nor on_errors="
2113 "remount-ro was specified%s",
2114 es1, es2);
2115 goto iput_quota_err_out;
2117 sb->s_flags |= MS_RDONLY;
2118 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2119 } else
2120 ntfs_warning(sb, "%s. Will not be able to remount "
2121 "read-write%s", es1, es2);
2122 /* This will prevent a read-write remount. */
2123 NVolSetErrors(vol);
2125 /* If (still) a read-write mount, mark the quotas out of date. */
2126 if (!(sb->s_flags & MS_RDONLY) &&
2127 !ntfs_mark_quotas_out_of_date(vol)) {
2128 static const char *es1 = "Failed to mark quotas out of date";
2129 static const char *es2 = ". Run chkdsk.";
2131 /* Convert to a read-only mount. */
2132 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2133 ON_ERRORS_CONTINUE))) {
2134 ntfs_error(sb, "%s and neither on_errors=continue nor "
2135 "on_errors=remount-ro was specified%s",
2136 es1, es2);
2137 goto iput_quota_err_out;
2139 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2140 sb->s_flags |= MS_RDONLY;
2141 NVolSetErrors(vol);
2144 * Find the transaction log file ($UsnJrnl), load it if present, check
2145 * it, and set it up.
2147 if (!load_and_init_usnjrnl(vol)) {
2148 static const char *es1 = "Failed to load $UsnJrnl";
2149 static const char *es2 = ". Run chkdsk.";
2151 /* If a read-write mount, convert it to a read-only mount. */
2152 if (!(sb->s_flags & MS_RDONLY)) {
2153 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2154 ON_ERRORS_CONTINUE))) {
2155 ntfs_error(sb, "%s and neither on_errors="
2156 "continue nor on_errors="
2157 "remount-ro was specified%s",
2158 es1, es2);
2159 goto iput_usnjrnl_err_out;
2161 sb->s_flags |= MS_RDONLY;
2162 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2163 } else
2164 ntfs_warning(sb, "%s. Will not be able to remount "
2165 "read-write%s", es1, es2);
2166 /* This will prevent a read-write remount. */
2167 NVolSetErrors(vol);
2169 /* If (still) a read-write mount, stamp the transaction log. */
2170 if (!(sb->s_flags & MS_RDONLY) && !ntfs_stamp_usnjrnl(vol)) {
2171 static const char *es1 = "Failed to stamp transaction log "
2172 "($UsnJrnl)";
2173 static const char *es2 = ". Run chkdsk.";
2175 /* Convert to a read-only mount. */
2176 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2177 ON_ERRORS_CONTINUE))) {
2178 ntfs_error(sb, "%s and neither on_errors=continue nor "
2179 "on_errors=remount-ro was specified%s",
2180 es1, es2);
2181 goto iput_usnjrnl_err_out;
2183 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2184 sb->s_flags |= MS_RDONLY;
2185 NVolSetErrors(vol);
2187 #endif /* NTFS_RW */
2188 return true;
2189 #ifdef NTFS_RW
2190 iput_usnjrnl_err_out:
2191 if (vol->usnjrnl_j_ino)
2192 iput(vol->usnjrnl_j_ino);
2193 if (vol->usnjrnl_max_ino)
2194 iput(vol->usnjrnl_max_ino);
2195 if (vol->usnjrnl_ino)
2196 iput(vol->usnjrnl_ino);
2197 iput_quota_err_out:
2198 if (vol->quota_q_ino)
2199 iput(vol->quota_q_ino);
2200 if (vol->quota_ino)
2201 iput(vol->quota_ino);
2202 iput(vol->extend_ino);
2203 #endif /* NTFS_RW */
2204 iput_sec_err_out:
2205 iput(vol->secure_ino);
2206 iput_root_err_out:
2207 iput(vol->root_ino);
2208 iput_logfile_err_out:
2209 #ifdef NTFS_RW
2210 if (vol->logfile_ino)
2211 iput(vol->logfile_ino);
2212 iput_vol_err_out:
2213 #endif /* NTFS_RW */
2214 iput(vol->vol_ino);
2215 iput_lcnbmp_err_out:
2216 iput(vol->lcnbmp_ino);
2217 iput_attrdef_err_out:
2218 vol->attrdef_size = 0;
2219 if (vol->attrdef) {
2220 ntfs_free(vol->attrdef);
2221 vol->attrdef = NULL;
2223 #ifdef NTFS_RW
2224 iput_upcase_err_out:
2225 #endif /* NTFS_RW */
2226 vol->upcase_len = 0;
2227 mutex_lock(&ntfs_lock);
2228 if (vol->upcase == default_upcase) {
2229 ntfs_nr_upcase_users--;
2230 vol->upcase = NULL;
2232 mutex_unlock(&ntfs_lock);
2233 if (vol->upcase) {
2234 ntfs_free(vol->upcase);
2235 vol->upcase = NULL;
2237 iput_mftbmp_err_out:
2238 iput(vol->mftbmp_ino);
2239 iput_mirr_err_out:
2240 #ifdef NTFS_RW
2241 if (vol->mftmirr_ino)
2242 iput(vol->mftmirr_ino);
2243 #endif /* NTFS_RW */
2244 return false;
2248 * ntfs_put_super - called by the vfs to unmount a volume
2249 * @sb: vfs superblock of volume to unmount
2251 * ntfs_put_super() is called by the VFS (from fs/super.c::do_umount()) when
2252 * the volume is being unmounted (umount system call has been invoked) and it
2253 * releases all inodes and memory belonging to the NTFS specific part of the
2254 * super block.
2256 static void ntfs_put_super(struct super_block *sb)
2258 ntfs_volume *vol = NTFS_SB(sb);
2260 ntfs_debug("Entering.");
2262 lock_kernel();
2264 #ifdef NTFS_RW
2266 * Commit all inodes while they are still open in case some of them
2267 * cause others to be dirtied.
2269 ntfs_commit_inode(vol->vol_ino);
2271 /* NTFS 3.0+ specific. */
2272 if (vol->major_ver >= 3) {
2273 if (vol->usnjrnl_j_ino)
2274 ntfs_commit_inode(vol->usnjrnl_j_ino);
2275 if (vol->usnjrnl_max_ino)
2276 ntfs_commit_inode(vol->usnjrnl_max_ino);
2277 if (vol->usnjrnl_ino)
2278 ntfs_commit_inode(vol->usnjrnl_ino);
2279 if (vol->quota_q_ino)
2280 ntfs_commit_inode(vol->quota_q_ino);
2281 if (vol->quota_ino)
2282 ntfs_commit_inode(vol->quota_ino);
2283 if (vol->extend_ino)
2284 ntfs_commit_inode(vol->extend_ino);
2285 if (vol->secure_ino)
2286 ntfs_commit_inode(vol->secure_ino);
2289 ntfs_commit_inode(vol->root_ino);
2291 down_write(&vol->lcnbmp_lock);
2292 ntfs_commit_inode(vol->lcnbmp_ino);
2293 up_write(&vol->lcnbmp_lock);
2295 down_write(&vol->mftbmp_lock);
2296 ntfs_commit_inode(vol->mftbmp_ino);
2297 up_write(&vol->mftbmp_lock);
2299 if (vol->logfile_ino)
2300 ntfs_commit_inode(vol->logfile_ino);
2302 if (vol->mftmirr_ino)
2303 ntfs_commit_inode(vol->mftmirr_ino);
2304 ntfs_commit_inode(vol->mft_ino);
2307 * If a read-write mount and no volume errors have occured, mark the
2308 * volume clean. Also, re-commit all affected inodes.
2310 if (!(sb->s_flags & MS_RDONLY)) {
2311 if (!NVolErrors(vol)) {
2312 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
2313 ntfs_warning(sb, "Failed to clear dirty bit "
2314 "in volume information "
2315 "flags. Run chkdsk.");
2316 ntfs_commit_inode(vol->vol_ino);
2317 ntfs_commit_inode(vol->root_ino);
2318 if (vol->mftmirr_ino)
2319 ntfs_commit_inode(vol->mftmirr_ino);
2320 ntfs_commit_inode(vol->mft_ino);
2321 } else {
2322 ntfs_warning(sb, "Volume has errors. Leaving volume "
2323 "marked dirty. Run chkdsk.");
2326 #endif /* NTFS_RW */
2328 iput(vol->vol_ino);
2329 vol->vol_ino = NULL;
2331 /* NTFS 3.0+ specific clean up. */
2332 if (vol->major_ver >= 3) {
2333 #ifdef NTFS_RW
2334 if (vol->usnjrnl_j_ino) {
2335 iput(vol->usnjrnl_j_ino);
2336 vol->usnjrnl_j_ino = NULL;
2338 if (vol->usnjrnl_max_ino) {
2339 iput(vol->usnjrnl_max_ino);
2340 vol->usnjrnl_max_ino = NULL;
2342 if (vol->usnjrnl_ino) {
2343 iput(vol->usnjrnl_ino);
2344 vol->usnjrnl_ino = NULL;
2346 if (vol->quota_q_ino) {
2347 iput(vol->quota_q_ino);
2348 vol->quota_q_ino = NULL;
2350 if (vol->quota_ino) {
2351 iput(vol->quota_ino);
2352 vol->quota_ino = NULL;
2354 #endif /* NTFS_RW */
2355 if (vol->extend_ino) {
2356 iput(vol->extend_ino);
2357 vol->extend_ino = NULL;
2359 if (vol->secure_ino) {
2360 iput(vol->secure_ino);
2361 vol->secure_ino = NULL;
2365 iput(vol->root_ino);
2366 vol->root_ino = NULL;
2368 down_write(&vol->lcnbmp_lock);
2369 iput(vol->lcnbmp_ino);
2370 vol->lcnbmp_ino = NULL;
2371 up_write(&vol->lcnbmp_lock);
2373 down_write(&vol->mftbmp_lock);
2374 iput(vol->mftbmp_ino);
2375 vol->mftbmp_ino = NULL;
2376 up_write(&vol->mftbmp_lock);
2378 #ifdef NTFS_RW
2379 if (vol->logfile_ino) {
2380 iput(vol->logfile_ino);
2381 vol->logfile_ino = NULL;
2383 if (vol->mftmirr_ino) {
2384 /* Re-commit the mft mirror and mft just in case. */
2385 ntfs_commit_inode(vol->mftmirr_ino);
2386 ntfs_commit_inode(vol->mft_ino);
2387 iput(vol->mftmirr_ino);
2388 vol->mftmirr_ino = NULL;
2391 * We should have no dirty inodes left, due to
2392 * mft.c::ntfs_mft_writepage() cleaning all the dirty pages as
2393 * the underlying mft records are written out and cleaned.
2395 ntfs_commit_inode(vol->mft_ino);
2396 write_inode_now(vol->mft_ino, 1);
2397 #endif /* NTFS_RW */
2399 iput(vol->mft_ino);
2400 vol->mft_ino = NULL;
2402 /* Throw away the table of attribute definitions. */
2403 vol->attrdef_size = 0;
2404 if (vol->attrdef) {
2405 ntfs_free(vol->attrdef);
2406 vol->attrdef = NULL;
2408 vol->upcase_len = 0;
2410 * Destroy the global default upcase table if necessary. Also decrease
2411 * the number of upcase users if we are a user.
2413 mutex_lock(&ntfs_lock);
2414 if (vol->upcase == default_upcase) {
2415 ntfs_nr_upcase_users--;
2416 vol->upcase = NULL;
2418 if (!ntfs_nr_upcase_users && default_upcase) {
2419 ntfs_free(default_upcase);
2420 default_upcase = NULL;
2422 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
2423 free_compression_buffers();
2424 mutex_unlock(&ntfs_lock);
2425 if (vol->upcase) {
2426 ntfs_free(vol->upcase);
2427 vol->upcase = NULL;
2430 unload_nls(vol->nls_map);
2432 sb->s_fs_info = NULL;
2433 kfree(vol);
2435 unlock_kernel();
2439 * get_nr_free_clusters - return the number of free clusters on a volume
2440 * @vol: ntfs volume for which to obtain free cluster count
2442 * Calculate the number of free clusters on the mounted NTFS volume @vol. We
2443 * actually calculate the number of clusters in use instead because this
2444 * allows us to not care about partial pages as these will be just zero filled
2445 * and hence not be counted as allocated clusters.
2447 * The only particularity is that clusters beyond the end of the logical ntfs
2448 * volume will be marked as allocated to prevent errors which means we have to
2449 * discount those at the end. This is important as the cluster bitmap always
2450 * has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside
2451 * the logical volume and marked in use when they are not as they do not exist.
2453 * If any pages cannot be read we assume all clusters in the erroring pages are
2454 * in use. This means we return an underestimate on errors which is better than
2455 * an overestimate.
2457 static s64 get_nr_free_clusters(ntfs_volume *vol)
2459 s64 nr_free = vol->nr_clusters;
2460 u32 *kaddr;
2461 struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
2462 struct page *page;
2463 pgoff_t index, max_index;
2465 ntfs_debug("Entering.");
2466 /* Serialize accesses to the cluster bitmap. */
2467 down_read(&vol->lcnbmp_lock);
2469 * Convert the number of bits into bytes rounded up, then convert into
2470 * multiples of PAGE_CACHE_SIZE, rounding up so that if we have one
2471 * full and one partial page max_index = 2.
2473 max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >>
2474 PAGE_CACHE_SHIFT;
2475 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2476 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.",
2477 max_index, PAGE_CACHE_SIZE / 4);
2478 for (index = 0; index < max_index; index++) {
2479 unsigned int i;
2481 * Read the page from page cache, getting it from backing store
2482 * if necessary, and increment the use count.
2484 page = read_mapping_page(mapping, index, NULL);
2485 /* Ignore pages which errored synchronously. */
2486 if (IS_ERR(page)) {
2487 ntfs_debug("read_mapping_page() error. Skipping "
2488 "page (index 0x%lx).", index);
2489 nr_free -= PAGE_CACHE_SIZE * 8;
2490 continue;
2492 kaddr = (u32*)kmap_atomic(page, KM_USER0);
2494 * For each 4 bytes, subtract the number of set bits. If this
2495 * is the last page and it is partial we don't really care as
2496 * it just means we do a little extra work but it won't affect
2497 * the result as all out of range bytes are set to zero by
2498 * ntfs_readpage().
2500 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
2501 nr_free -= (s64)hweight32(kaddr[i]);
2502 kunmap_atomic(kaddr, KM_USER0);
2503 page_cache_release(page);
2505 ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index - 1);
2507 * Fixup for eventual bits outside logical ntfs volume (see function
2508 * description above).
2510 if (vol->nr_clusters & 63)
2511 nr_free += 64 - (vol->nr_clusters & 63);
2512 up_read(&vol->lcnbmp_lock);
2513 /* If errors occured we may well have gone below zero, fix this. */
2514 if (nr_free < 0)
2515 nr_free = 0;
2516 ntfs_debug("Exiting.");
2517 return nr_free;
2521 * __get_nr_free_mft_records - return the number of free inodes on a volume
2522 * @vol: ntfs volume for which to obtain free inode count
2523 * @nr_free: number of mft records in filesystem
2524 * @max_index: maximum number of pages containing set bits
2526 * Calculate the number of free mft records (inodes) on the mounted NTFS
2527 * volume @vol. We actually calculate the number of mft records in use instead
2528 * because this allows us to not care about partial pages as these will be just
2529 * zero filled and hence not be counted as allocated mft record.
2531 * If any pages cannot be read we assume all mft records in the erroring pages
2532 * are in use. This means we return an underestimate on errors which is better
2533 * than an overestimate.
2535 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
2537 static unsigned long __get_nr_free_mft_records(ntfs_volume *vol,
2538 s64 nr_free, const pgoff_t max_index)
2540 u32 *kaddr;
2541 struct address_space *mapping = vol->mftbmp_ino->i_mapping;
2542 struct page *page;
2543 pgoff_t index;
2545 ntfs_debug("Entering.");
2546 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2547 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
2548 "0x%lx.", max_index, PAGE_CACHE_SIZE / 4);
2549 for (index = 0; index < max_index; index++) {
2550 unsigned int i;
2552 * Read the page from page cache, getting it from backing store
2553 * if necessary, and increment the use count.
2555 page = read_mapping_page(mapping, index, NULL);
2556 /* Ignore pages which errored synchronously. */
2557 if (IS_ERR(page)) {
2558 ntfs_debug("read_mapping_page() error. Skipping "
2559 "page (index 0x%lx).", index);
2560 nr_free -= PAGE_CACHE_SIZE * 8;
2561 continue;
2563 kaddr = (u32*)kmap_atomic(page, KM_USER0);
2565 * For each 4 bytes, subtract the number of set bits. If this
2566 * is the last page and it is partial we don't really care as
2567 * it just means we do a little extra work but it won't affect
2568 * the result as all out of range bytes are set to zero by
2569 * ntfs_readpage().
2571 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
2572 nr_free -= (s64)hweight32(kaddr[i]);
2573 kunmap_atomic(kaddr, KM_USER0);
2574 page_cache_release(page);
2576 ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.",
2577 index - 1);
2578 /* If errors occured we may well have gone below zero, fix this. */
2579 if (nr_free < 0)
2580 nr_free = 0;
2581 ntfs_debug("Exiting.");
2582 return nr_free;
2586 * ntfs_statfs - return information about mounted NTFS volume
2587 * @dentry: dentry from mounted volume
2588 * @sfs: statfs structure in which to return the information
2590 * Return information about the mounted NTFS volume @dentry in the statfs structure
2591 * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is
2592 * called). We interpret the values to be correct of the moment in time at
2593 * which we are called. Most values are variable otherwise and this isn't just
2594 * the free values but the totals as well. For example we can increase the
2595 * total number of file nodes if we run out and we can keep doing this until
2596 * there is no more space on the volume left at all.
2598 * Called from vfs_statfs which is used to handle the statfs, fstatfs, and
2599 * ustat system calls.
2601 * Return 0 on success or -errno on error.
2603 static int ntfs_statfs(struct dentry *dentry, struct kstatfs *sfs)
2605 struct super_block *sb = dentry->d_sb;
2606 s64 size;
2607 ntfs_volume *vol = NTFS_SB(sb);
2608 ntfs_inode *mft_ni = NTFS_I(vol->mft_ino);
2609 pgoff_t max_index;
2610 unsigned long flags;
2612 ntfs_debug("Entering.");
2613 /* Type of filesystem. */
2614 sfs->f_type = NTFS_SB_MAGIC;
2615 /* Optimal transfer block size. */
2616 sfs->f_bsize = PAGE_CACHE_SIZE;
2618 * Total data blocks in filesystem in units of f_bsize and since
2619 * inodes are also stored in data blocs ($MFT is a file) this is just
2620 * the total clusters.
2622 sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >>
2623 PAGE_CACHE_SHIFT;
2624 /* Free data blocks in filesystem in units of f_bsize. */
2625 size = get_nr_free_clusters(vol) << vol->cluster_size_bits >>
2626 PAGE_CACHE_SHIFT;
2627 if (size < 0LL)
2628 size = 0LL;
2629 /* Free blocks avail to non-superuser, same as above on NTFS. */
2630 sfs->f_bavail = sfs->f_bfree = size;
2631 /* Serialize accesses to the inode bitmap. */
2632 down_read(&vol->mftbmp_lock);
2633 read_lock_irqsave(&mft_ni->size_lock, flags);
2634 size = i_size_read(vol->mft_ino) >> vol->mft_record_size_bits;
2636 * Convert the maximum number of set bits into bytes rounded up, then
2637 * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
2638 * have one full and one partial page max_index = 2.
2640 max_index = ((((mft_ni->initialized_size >> vol->mft_record_size_bits)
2641 + 7) >> 3) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2642 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2643 /* Number of inodes in filesystem (at this point in time). */
2644 sfs->f_files = size;
2645 /* Free inodes in fs (based on current total count). */
2646 sfs->f_ffree = __get_nr_free_mft_records(vol, size, max_index);
2647 up_read(&vol->mftbmp_lock);
2649 * File system id. This is extremely *nix flavour dependent and even
2650 * within Linux itself all fs do their own thing. I interpret this to
2651 * mean a unique id associated with the mounted fs and not the id
2652 * associated with the filesystem driver, the latter is already given
2653 * by the filesystem type in sfs->f_type. Thus we use the 64-bit
2654 * volume serial number splitting it into two 32-bit parts. We enter
2655 * the least significant 32-bits in f_fsid[0] and the most significant
2656 * 32-bits in f_fsid[1].
2658 sfs->f_fsid.val[0] = vol->serial_no & 0xffffffff;
2659 sfs->f_fsid.val[1] = (vol->serial_no >> 32) & 0xffffffff;
2660 /* Maximum length of filenames. */
2661 sfs->f_namelen = NTFS_MAX_NAME_LEN;
2662 return 0;
2666 * The complete super operations.
2668 static const struct super_operations ntfs_sops = {
2669 .alloc_inode = ntfs_alloc_big_inode, /* VFS: Allocate new inode. */
2670 .destroy_inode = ntfs_destroy_big_inode, /* VFS: Deallocate inode. */
2671 #ifdef NTFS_RW
2672 //.dirty_inode = NULL, /* VFS: Called from
2673 // __mark_inode_dirty(). */
2674 .write_inode = ntfs_write_inode, /* VFS: Write dirty inode to
2675 disk. */
2676 //.drop_inode = NULL, /* VFS: Called just after the
2677 // inode reference count has
2678 // been decreased to zero.
2679 // NOTE: The inode lock is
2680 // held. See fs/inode.c::
2681 // generic_drop_inode(). */
2682 //.delete_inode = NULL, /* VFS: Delete inode from disk.
2683 // Called when i_count becomes
2684 // 0 and i_nlink is also 0. */
2685 //.write_super = NULL, /* Flush dirty super block to
2686 // disk. */
2687 //.sync_fs = NULL, /* ? */
2688 //.write_super_lockfs = NULL, /* ? */
2689 //.unlockfs = NULL, /* ? */
2690 #endif /* NTFS_RW */
2691 .put_super = ntfs_put_super, /* Syscall: umount. */
2692 .statfs = ntfs_statfs, /* Syscall: statfs */
2693 .remount_fs = ntfs_remount, /* Syscall: mount -o remount. */
2694 .clear_inode = ntfs_clear_big_inode, /* VFS: Called when an inode is
2695 removed from memory. */
2696 //.umount_begin = NULL, /* Forced umount. */
2697 .show_options = ntfs_show_options, /* Show mount options in
2698 proc. */
2702 * ntfs_fill_super - mount an ntfs filesystem
2703 * @sb: super block of ntfs filesystem to mount
2704 * @opt: string containing the mount options
2705 * @silent: silence error output
2707 * ntfs_fill_super() is called by the VFS to mount the device described by @sb
2708 * with the mount otions in @data with the NTFS filesystem.
2710 * If @silent is true, remain silent even if errors are detected. This is used
2711 * during bootup, when the kernel tries to mount the root filesystem with all
2712 * registered filesystems one after the other until one succeeds. This implies
2713 * that all filesystems except the correct one will quite correctly and
2714 * expectedly return an error, but nobody wants to see error messages when in
2715 * fact this is what is supposed to happen.
2717 * NOTE: @sb->s_flags contains the mount options flags.
2719 static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2721 ntfs_volume *vol;
2722 struct buffer_head *bh;
2723 struct inode *tmp_ino;
2724 int blocksize, result;
2727 * We do a pretty difficult piece of bootstrap by reading the
2728 * MFT (and other metadata) from disk into memory. We'll only
2729 * release this metadata during umount, so the locking patterns
2730 * observed during bootstrap do not count. So turn off the
2731 * observation of locking patterns (strictly for this context
2732 * only) while mounting NTFS. [The validator is still active
2733 * otherwise, even for this context: it will for example record
2734 * lock class registrations.]
2736 lockdep_off();
2737 ntfs_debug("Entering.");
2738 #ifndef NTFS_RW
2739 sb->s_flags |= MS_RDONLY;
2740 #endif /* ! NTFS_RW */
2741 /* Allocate a new ntfs_volume and place it in sb->s_fs_info. */
2742 sb->s_fs_info = kmalloc(sizeof(ntfs_volume), GFP_NOFS);
2743 vol = NTFS_SB(sb);
2744 if (!vol) {
2745 if (!silent)
2746 ntfs_error(sb, "Allocation of NTFS volume structure "
2747 "failed. Aborting mount...");
2748 lockdep_on();
2749 return -ENOMEM;
2751 /* Initialize ntfs_volume structure. */
2752 *vol = (ntfs_volume) {
2753 .sb = sb,
2755 * Default is group and other don't have any access to files or
2756 * directories while owner has full access. Further, files by
2757 * default are not executable but directories are of course
2758 * browseable.
2760 .fmask = 0177,
2761 .dmask = 0077,
2763 init_rwsem(&vol->mftbmp_lock);
2764 init_rwsem(&vol->lcnbmp_lock);
2766 unlock_kernel();
2768 /* By default, enable sparse support. */
2769 NVolSetSparseEnabled(vol);
2771 /* Important to get the mount options dealt with now. */
2772 if (!parse_options(vol, (char*)opt))
2773 goto err_out_now;
2775 /* We support sector sizes up to the PAGE_CACHE_SIZE. */
2776 if (bdev_logical_block_size(sb->s_bdev) > PAGE_CACHE_SIZE) {
2777 if (!silent)
2778 ntfs_error(sb, "Device has unsupported sector size "
2779 "(%i). The maximum supported sector "
2780 "size on this architecture is %lu "
2781 "bytes.",
2782 bdev_logical_block_size(sb->s_bdev),
2783 PAGE_CACHE_SIZE);
2784 goto err_out_now;
2787 * Setup the device access block size to NTFS_BLOCK_SIZE or the hard
2788 * sector size, whichever is bigger.
2790 blocksize = sb_min_blocksize(sb, NTFS_BLOCK_SIZE);
2791 if (blocksize < NTFS_BLOCK_SIZE) {
2792 if (!silent)
2793 ntfs_error(sb, "Unable to set device block size.");
2794 goto err_out_now;
2796 BUG_ON(blocksize != sb->s_blocksize);
2797 ntfs_debug("Set device block size to %i bytes (block size bits %i).",
2798 blocksize, sb->s_blocksize_bits);
2799 /* Determine the size of the device in units of block_size bytes. */
2800 if (!i_size_read(sb->s_bdev->bd_inode)) {
2801 if (!silent)
2802 ntfs_error(sb, "Unable to determine device size.");
2803 goto err_out_now;
2805 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2806 sb->s_blocksize_bits;
2807 /* Read the boot sector and return unlocked buffer head to it. */
2808 if (!(bh = read_ntfs_boot_sector(sb, silent))) {
2809 if (!silent)
2810 ntfs_error(sb, "Not an NTFS volume.");
2811 goto err_out_now;
2814 * Extract the data from the boot sector and setup the ntfs volume
2815 * using it.
2817 result = parse_ntfs_boot_sector(vol, (NTFS_BOOT_SECTOR*)bh->b_data);
2818 brelse(bh);
2819 if (!result) {
2820 if (!silent)
2821 ntfs_error(sb, "Unsupported NTFS filesystem.");
2822 goto err_out_now;
2825 * If the boot sector indicates a sector size bigger than the current
2826 * device block size, switch the device block size to the sector size.
2827 * TODO: It may be possible to support this case even when the set
2828 * below fails, we would just be breaking up the i/o for each sector
2829 * into multiple blocks for i/o purposes but otherwise it should just
2830 * work. However it is safer to leave disabled until someone hits this
2831 * error message and then we can get them to try it without the setting
2832 * so we know for sure that it works.
2834 if (vol->sector_size > blocksize) {
2835 blocksize = sb_set_blocksize(sb, vol->sector_size);
2836 if (blocksize != vol->sector_size) {
2837 if (!silent)
2838 ntfs_error(sb, "Unable to set device block "
2839 "size to sector size (%i).",
2840 vol->sector_size);
2841 goto err_out_now;
2843 BUG_ON(blocksize != sb->s_blocksize);
2844 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2845 sb->s_blocksize_bits;
2846 ntfs_debug("Changed device block size to %i bytes (block size "
2847 "bits %i) to match volume sector size.",
2848 blocksize, sb->s_blocksize_bits);
2850 /* Initialize the cluster and mft allocators. */
2851 ntfs_setup_allocators(vol);
2852 /* Setup remaining fields in the super block. */
2853 sb->s_magic = NTFS_SB_MAGIC;
2855 * Ntfs allows 63 bits for the file size, i.e. correct would be:
2856 * sb->s_maxbytes = ~0ULL >> 1;
2857 * But the kernel uses a long as the page cache page index which on
2858 * 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel
2859 * defined to the maximum the page cache page index can cope with
2860 * without overflowing the index or to 2^63 - 1, whichever is smaller.
2862 sb->s_maxbytes = MAX_LFS_FILESIZE;
2863 /* Ntfs measures time in 100ns intervals. */
2864 sb->s_time_gran = 100;
2866 * Now load the metadata required for the page cache and our address
2867 * space operations to function. We do this by setting up a specialised
2868 * read_inode method and then just calling the normal iget() to obtain
2869 * the inode for $MFT which is sufficient to allow our normal inode
2870 * operations and associated address space operations to function.
2872 sb->s_op = &ntfs_sops;
2873 tmp_ino = new_inode(sb);
2874 if (!tmp_ino) {
2875 if (!silent)
2876 ntfs_error(sb, "Failed to load essential metadata.");
2877 goto err_out_now;
2879 tmp_ino->i_ino = FILE_MFT;
2880 insert_inode_hash(tmp_ino);
2881 if (ntfs_read_inode_mount(tmp_ino) < 0) {
2882 if (!silent)
2883 ntfs_error(sb, "Failed to load essential metadata.");
2884 goto iput_tmp_ino_err_out_now;
2886 mutex_lock(&ntfs_lock);
2888 * The current mount is a compression user if the cluster size is
2889 * less than or equal 4kiB.
2891 if (vol->cluster_size <= 4096 && !ntfs_nr_compression_users++) {
2892 result = allocate_compression_buffers();
2893 if (result) {
2894 ntfs_error(NULL, "Failed to allocate buffers "
2895 "for compression engine.");
2896 ntfs_nr_compression_users--;
2897 mutex_unlock(&ntfs_lock);
2898 goto iput_tmp_ino_err_out_now;
2902 * Generate the global default upcase table if necessary. Also
2903 * temporarily increment the number of upcase users to avoid race
2904 * conditions with concurrent (u)mounts.
2906 if (!default_upcase)
2907 default_upcase = generate_default_upcase();
2908 ntfs_nr_upcase_users++;
2909 mutex_unlock(&ntfs_lock);
2911 * From now on, ignore @silent parameter. If we fail below this line,
2912 * it will be due to a corrupt fs or a system error, so we report it.
2915 * Open the system files with normal access functions and complete
2916 * setting up the ntfs super block.
2918 if (!load_system_files(vol)) {
2919 ntfs_error(sb, "Failed to load system files.");
2920 goto unl_upcase_iput_tmp_ino_err_out_now;
2922 if ((sb->s_root = d_alloc_root(vol->root_ino))) {
2923 /* We increment i_count simulating an ntfs_iget(). */
2924 atomic_inc(&vol->root_ino->i_count);
2925 ntfs_debug("Exiting, status successful.");
2926 /* Release the default upcase if it has no users. */
2927 mutex_lock(&ntfs_lock);
2928 if (!--ntfs_nr_upcase_users && default_upcase) {
2929 ntfs_free(default_upcase);
2930 default_upcase = NULL;
2932 mutex_unlock(&ntfs_lock);
2933 sb->s_export_op = &ntfs_export_ops;
2934 lock_kernel();
2935 lockdep_on();
2936 return 0;
2938 ntfs_error(sb, "Failed to allocate root directory.");
2939 /* Clean up after the successful load_system_files() call from above. */
2940 // TODO: Use ntfs_put_super() instead of repeating all this code...
2941 // FIXME: Should mark the volume clean as the error is most likely
2942 // -ENOMEM.
2943 iput(vol->vol_ino);
2944 vol->vol_ino = NULL;
2945 /* NTFS 3.0+ specific clean up. */
2946 if (vol->major_ver >= 3) {
2947 #ifdef NTFS_RW
2948 if (vol->usnjrnl_j_ino) {
2949 iput(vol->usnjrnl_j_ino);
2950 vol->usnjrnl_j_ino = NULL;
2952 if (vol->usnjrnl_max_ino) {
2953 iput(vol->usnjrnl_max_ino);
2954 vol->usnjrnl_max_ino = NULL;
2956 if (vol->usnjrnl_ino) {
2957 iput(vol->usnjrnl_ino);
2958 vol->usnjrnl_ino = NULL;
2960 if (vol->quota_q_ino) {
2961 iput(vol->quota_q_ino);
2962 vol->quota_q_ino = NULL;
2964 if (vol->quota_ino) {
2965 iput(vol->quota_ino);
2966 vol->quota_ino = NULL;
2968 #endif /* NTFS_RW */
2969 if (vol->extend_ino) {
2970 iput(vol->extend_ino);
2971 vol->extend_ino = NULL;
2973 if (vol->secure_ino) {
2974 iput(vol->secure_ino);
2975 vol->secure_ino = NULL;
2978 iput(vol->root_ino);
2979 vol->root_ino = NULL;
2980 iput(vol->lcnbmp_ino);
2981 vol->lcnbmp_ino = NULL;
2982 iput(vol->mftbmp_ino);
2983 vol->mftbmp_ino = NULL;
2984 #ifdef NTFS_RW
2985 if (vol->logfile_ino) {
2986 iput(vol->logfile_ino);
2987 vol->logfile_ino = NULL;
2989 if (vol->mftmirr_ino) {
2990 iput(vol->mftmirr_ino);
2991 vol->mftmirr_ino = NULL;
2993 #endif /* NTFS_RW */
2994 /* Throw away the table of attribute definitions. */
2995 vol->attrdef_size = 0;
2996 if (vol->attrdef) {
2997 ntfs_free(vol->attrdef);
2998 vol->attrdef = NULL;
3000 vol->upcase_len = 0;
3001 mutex_lock(&ntfs_lock);
3002 if (vol->upcase == default_upcase) {
3003 ntfs_nr_upcase_users--;
3004 vol->upcase = NULL;
3006 mutex_unlock(&ntfs_lock);
3007 if (vol->upcase) {
3008 ntfs_free(vol->upcase);
3009 vol->upcase = NULL;
3011 if (vol->nls_map) {
3012 unload_nls(vol->nls_map);
3013 vol->nls_map = NULL;
3015 /* Error exit code path. */
3016 unl_upcase_iput_tmp_ino_err_out_now:
3018 * Decrease the number of upcase users and destroy the global default
3019 * upcase table if necessary.
3021 mutex_lock(&ntfs_lock);
3022 if (!--ntfs_nr_upcase_users && default_upcase) {
3023 ntfs_free(default_upcase);
3024 default_upcase = NULL;
3026 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
3027 free_compression_buffers();
3028 mutex_unlock(&ntfs_lock);
3029 iput_tmp_ino_err_out_now:
3030 iput(tmp_ino);
3031 if (vol->mft_ino && vol->mft_ino != tmp_ino)
3032 iput(vol->mft_ino);
3033 vol->mft_ino = NULL;
3035 * This is needed to get ntfs_clear_extent_inode() called for each
3036 * inode we have ever called ntfs_iget()/iput() on, otherwise we A)
3037 * leak resources and B) a subsequent mount fails automatically due to
3038 * ntfs_iget() never calling down into our ntfs_read_locked_inode()
3039 * method again... FIXME: Do we need to do this twice now because of
3040 * attribute inodes? I think not, so leave as is for now... (AIA)
3042 if (invalidate_inodes(sb)) {
3043 ntfs_error(sb, "Busy inodes left. This is most likely a NTFS "
3044 "driver bug.");
3045 /* Copied from fs/super.c. I just love this message. (-; */
3046 printk("NTFS: Busy inodes after umount. Self-destruct in 5 "
3047 "seconds. Have a nice day...\n");
3049 /* Errors at this stage are irrelevant. */
3050 err_out_now:
3051 lock_kernel();
3052 sb->s_fs_info = NULL;
3053 kfree(vol);
3054 ntfs_debug("Failed, returning -EINVAL.");
3055 lockdep_on();
3056 return -EINVAL;
3060 * This is a slab cache to optimize allocations and deallocations of Unicode
3061 * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN
3062 * (255) Unicode characters + a terminating NULL Unicode character.
3064 struct kmem_cache *ntfs_name_cache;
3066 /* Slab caches for efficient allocation/deallocation of inodes. */
3067 struct kmem_cache *ntfs_inode_cache;
3068 struct kmem_cache *ntfs_big_inode_cache;
3070 /* Init once constructor for the inode slab cache. */
3071 static void ntfs_big_inode_init_once(void *foo)
3073 ntfs_inode *ni = (ntfs_inode *)foo;
3075 inode_init_once(VFS_I(ni));
3079 * Slab caches to optimize allocations and deallocations of attribute search
3080 * contexts and index contexts, respectively.
3082 struct kmem_cache *ntfs_attr_ctx_cache;
3083 struct kmem_cache *ntfs_index_ctx_cache;
3085 /* Driver wide mutex. */
3086 DEFINE_MUTEX(ntfs_lock);
3088 static int ntfs_get_sb(struct file_system_type *fs_type,
3089 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3091 return get_sb_bdev(fs_type, flags, dev_name, data, ntfs_fill_super,
3092 mnt);
3095 static struct file_system_type ntfs_fs_type = {
3096 .owner = THIS_MODULE,
3097 .name = "ntfs",
3098 .get_sb = ntfs_get_sb,
3099 .kill_sb = kill_block_super,
3100 .fs_flags = FS_REQUIRES_DEV,
3103 /* Stable names for the slab caches. */
3104 static const char ntfs_index_ctx_cache_name[] = "ntfs_index_ctx_cache";
3105 static const char ntfs_attr_ctx_cache_name[] = "ntfs_attr_ctx_cache";
3106 static const char ntfs_name_cache_name[] = "ntfs_name_cache";
3107 static const char ntfs_inode_cache_name[] = "ntfs_inode_cache";
3108 static const char ntfs_big_inode_cache_name[] = "ntfs_big_inode_cache";
3110 static int __init init_ntfs_fs(void)
3112 int err = 0;
3114 /* This may be ugly but it results in pretty output so who cares. (-8 */
3115 printk(KERN_INFO "NTFS driver " NTFS_VERSION " [Flags: R/"
3116 #ifdef NTFS_RW
3118 #else
3120 #endif
3121 #ifdef DEBUG
3122 " DEBUG"
3123 #endif
3124 #ifdef MODULE
3125 " MODULE"
3126 #endif
3127 "].\n");
3129 ntfs_debug("Debug messages are enabled.");
3131 ntfs_index_ctx_cache = kmem_cache_create(ntfs_index_ctx_cache_name,
3132 sizeof(ntfs_index_context), 0 /* offset */,
3133 SLAB_HWCACHE_ALIGN, NULL /* ctor */);
3134 if (!ntfs_index_ctx_cache) {
3135 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3136 ntfs_index_ctx_cache_name);
3137 goto ictx_err_out;
3139 ntfs_attr_ctx_cache = kmem_cache_create(ntfs_attr_ctx_cache_name,
3140 sizeof(ntfs_attr_search_ctx), 0 /* offset */,
3141 SLAB_HWCACHE_ALIGN, NULL /* ctor */);
3142 if (!ntfs_attr_ctx_cache) {
3143 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3144 ntfs_attr_ctx_cache_name);
3145 goto actx_err_out;
3148 ntfs_name_cache = kmem_cache_create(ntfs_name_cache_name,
3149 (NTFS_MAX_NAME_LEN+1) * sizeof(ntfschar), 0,
3150 SLAB_HWCACHE_ALIGN, NULL);
3151 if (!ntfs_name_cache) {
3152 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3153 ntfs_name_cache_name);
3154 goto name_err_out;
3157 ntfs_inode_cache = kmem_cache_create(ntfs_inode_cache_name,
3158 sizeof(ntfs_inode), 0,
3159 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL);
3160 if (!ntfs_inode_cache) {
3161 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3162 ntfs_inode_cache_name);
3163 goto inode_err_out;
3166 ntfs_big_inode_cache = kmem_cache_create(ntfs_big_inode_cache_name,
3167 sizeof(big_ntfs_inode), 0,
3168 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD,
3169 ntfs_big_inode_init_once);
3170 if (!ntfs_big_inode_cache) {
3171 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3172 ntfs_big_inode_cache_name);
3173 goto big_inode_err_out;
3176 /* Register the ntfs sysctls. */
3177 err = ntfs_sysctl(1);
3178 if (err) {
3179 printk(KERN_CRIT "NTFS: Failed to register NTFS sysctls!\n");
3180 goto sysctl_err_out;
3183 err = register_filesystem(&ntfs_fs_type);
3184 if (!err) {
3185 ntfs_debug("NTFS driver registered successfully.");
3186 return 0; /* Success! */
3188 printk(KERN_CRIT "NTFS: Failed to register NTFS filesystem driver!\n");
3190 sysctl_err_out:
3191 kmem_cache_destroy(ntfs_big_inode_cache);
3192 big_inode_err_out:
3193 kmem_cache_destroy(ntfs_inode_cache);
3194 inode_err_out:
3195 kmem_cache_destroy(ntfs_name_cache);
3196 name_err_out:
3197 kmem_cache_destroy(ntfs_attr_ctx_cache);
3198 actx_err_out:
3199 kmem_cache_destroy(ntfs_index_ctx_cache);
3200 ictx_err_out:
3201 if (!err) {
3202 printk(KERN_CRIT "NTFS: Aborting NTFS filesystem driver "
3203 "registration...\n");
3204 err = -ENOMEM;
3206 return err;
3209 static void __exit exit_ntfs_fs(void)
3211 ntfs_debug("Unregistering NTFS driver.");
3213 unregister_filesystem(&ntfs_fs_type);
3214 kmem_cache_destroy(ntfs_big_inode_cache);
3215 kmem_cache_destroy(ntfs_inode_cache);
3216 kmem_cache_destroy(ntfs_name_cache);
3217 kmem_cache_destroy(ntfs_attr_ctx_cache);
3218 kmem_cache_destroy(ntfs_index_ctx_cache);
3219 /* Unregister the ntfs sysctls. */
3220 ntfs_sysctl(0);
3223 MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>");
3224 MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2007 Anton Altaparmakov");
3225 MODULE_VERSION(NTFS_VERSION);
3226 MODULE_LICENSE("GPL");
3227 #ifdef DEBUG
3228 module_param(debug_msgs, bool, 0);
3229 MODULE_PARM_DESC(debug_msgs, "Enable debug messages.");
3230 #endif
3232 module_init(init_ntfs_fs)
3233 module_exit(exit_ntfs_fs)