retransmit loop bugfix
[cor_2_6_31.git] / fs / ntfs / super.c
blobabaaa1cbf8de4dac7ac9e8bfb6e143e82b06bff7
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 if (old_nls)
205 unload_nls(old_nls);
207 } else if (!strcmp(p, "utf8")) {
208 bool val = false;
209 ntfs_warning(vol->sb, "Option utf8 is no longer "
210 "supported, using option nls=utf8. Please "
211 "use option nls=utf8 in the future and "
212 "make sure utf8 is compiled either as a "
213 "module or into the kernel.");
214 if (!v || !*v)
215 val = true;
216 else if (!simple_getbool(v, &val))
217 goto needs_bool;
218 if (val) {
219 v = utf8;
220 goto use_utf8;
222 } else {
223 ntfs_error(vol->sb, "Unrecognized mount option %s.", p);
224 if (errors < INT_MAX)
225 errors++;
227 #undef NTFS_GETOPT_OPTIONS_ARRAY
228 #undef NTFS_GETOPT_BOOL
229 #undef NTFS_GETOPT
230 #undef NTFS_GETOPT_WITH_DEFAULT
232 no_mount_options:
233 if (errors && !sloppy)
234 return false;
235 if (sloppy)
236 ntfs_warning(vol->sb, "Sloppy option given. Ignoring "
237 "unrecognized mount option(s) and continuing.");
238 /* Keep this first! */
239 if (on_errors != -1) {
240 if (!on_errors) {
241 ntfs_error(vol->sb, "Invalid errors option argument "
242 "or bug in options parser.");
243 return false;
246 if (nls_map) {
247 if (vol->nls_map && vol->nls_map != nls_map) {
248 ntfs_error(vol->sb, "Cannot change NLS character set "
249 "on remount.");
250 return false;
251 } /* else (!vol->nls_map) */
252 ntfs_debug("Using NLS character set %s.", nls_map->charset);
253 vol->nls_map = nls_map;
254 } else /* (!nls_map) */ {
255 if (!vol->nls_map) {
256 vol->nls_map = load_nls_default();
257 if (!vol->nls_map) {
258 ntfs_error(vol->sb, "Failed to load default "
259 "NLS character set.");
260 return false;
262 ntfs_debug("Using default NLS character set (%s).",
263 vol->nls_map->charset);
266 if (mft_zone_multiplier != -1) {
267 if (vol->mft_zone_multiplier && vol->mft_zone_multiplier !=
268 mft_zone_multiplier) {
269 ntfs_error(vol->sb, "Cannot change mft_zone_multiplier "
270 "on remount.");
271 return false;
273 if (mft_zone_multiplier < 1 || mft_zone_multiplier > 4) {
274 ntfs_error(vol->sb, "Invalid mft_zone_multiplier. "
275 "Using default value, i.e. 1.");
276 mft_zone_multiplier = 1;
278 vol->mft_zone_multiplier = mft_zone_multiplier;
280 if (!vol->mft_zone_multiplier)
281 vol->mft_zone_multiplier = 1;
282 if (on_errors != -1)
283 vol->on_errors = on_errors;
284 if (!vol->on_errors || vol->on_errors == ON_ERRORS_RECOVER)
285 vol->on_errors |= ON_ERRORS_CONTINUE;
286 if (uid != (uid_t)-1)
287 vol->uid = uid;
288 if (gid != (gid_t)-1)
289 vol->gid = gid;
290 if (fmask != (mode_t)-1)
291 vol->fmask = fmask;
292 if (dmask != (mode_t)-1)
293 vol->dmask = dmask;
294 if (show_sys_files != -1) {
295 if (show_sys_files)
296 NVolSetShowSystemFiles(vol);
297 else
298 NVolClearShowSystemFiles(vol);
300 if (case_sensitive != -1) {
301 if (case_sensitive)
302 NVolSetCaseSensitive(vol);
303 else
304 NVolClearCaseSensitive(vol);
306 if (disable_sparse != -1) {
307 if (disable_sparse)
308 NVolClearSparseEnabled(vol);
309 else {
310 if (!NVolSparseEnabled(vol) &&
311 vol->major_ver && vol->major_ver < 3)
312 ntfs_warning(vol->sb, "Not enabling sparse "
313 "support due to NTFS volume "
314 "version %i.%i (need at least "
315 "version 3.0).", vol->major_ver,
316 vol->minor_ver);
317 else
318 NVolSetSparseEnabled(vol);
321 return true;
322 needs_arg:
323 ntfs_error(vol->sb, "The %s option requires an argument.", p);
324 return false;
325 needs_bool:
326 ntfs_error(vol->sb, "The %s option requires a boolean argument.", p);
327 return false;
328 needs_val:
329 ntfs_error(vol->sb, "Invalid %s option argument: %s", p, ov);
330 return false;
333 #ifdef NTFS_RW
336 * ntfs_write_volume_flags - write new flags to the volume information flags
337 * @vol: ntfs volume on which to modify the flags
338 * @flags: new flags value for the volume information flags
340 * Internal function. You probably want to use ntfs_{set,clear}_volume_flags()
341 * instead (see below).
343 * Replace the volume information flags on the volume @vol with the value
344 * supplied in @flags. Note, this overwrites the volume information flags, so
345 * make sure to combine the flags you want to modify with the old flags and use
346 * the result when calling ntfs_write_volume_flags().
348 * Return 0 on success and -errno on error.
350 static int ntfs_write_volume_flags(ntfs_volume *vol, const VOLUME_FLAGS flags)
352 ntfs_inode *ni = NTFS_I(vol->vol_ino);
353 MFT_RECORD *m;
354 VOLUME_INFORMATION *vi;
355 ntfs_attr_search_ctx *ctx;
356 int err;
358 ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
359 le16_to_cpu(vol->vol_flags), le16_to_cpu(flags));
360 if (vol->vol_flags == flags)
361 goto done;
362 BUG_ON(!ni);
363 m = map_mft_record(ni);
364 if (IS_ERR(m)) {
365 err = PTR_ERR(m);
366 goto err_out;
368 ctx = ntfs_attr_get_search_ctx(ni, m);
369 if (!ctx) {
370 err = -ENOMEM;
371 goto put_unm_err_out;
373 err = ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
374 ctx);
375 if (err)
376 goto put_unm_err_out;
377 vi = (VOLUME_INFORMATION*)((u8*)ctx->attr +
378 le16_to_cpu(ctx->attr->data.resident.value_offset));
379 vol->vol_flags = vi->flags = flags;
380 flush_dcache_mft_record_page(ctx->ntfs_ino);
381 mark_mft_record_dirty(ctx->ntfs_ino);
382 ntfs_attr_put_search_ctx(ctx);
383 unmap_mft_record(ni);
384 done:
385 ntfs_debug("Done.");
386 return 0;
387 put_unm_err_out:
388 if (ctx)
389 ntfs_attr_put_search_ctx(ctx);
390 unmap_mft_record(ni);
391 err_out:
392 ntfs_error(vol->sb, "Failed with error code %i.", -err);
393 return err;
397 * ntfs_set_volume_flags - set bits in the volume information flags
398 * @vol: ntfs volume on which to modify the flags
399 * @flags: flags to set on the volume
401 * Set the bits in @flags in the volume information flags on the volume @vol.
403 * Return 0 on success and -errno on error.
405 static inline int ntfs_set_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
407 flags &= VOLUME_FLAGS_MASK;
408 return ntfs_write_volume_flags(vol, vol->vol_flags | flags);
412 * ntfs_clear_volume_flags - clear bits in the volume information flags
413 * @vol: ntfs volume on which to modify the flags
414 * @flags: flags to clear on the volume
416 * Clear the bits in @flags in the volume information flags on the volume @vol.
418 * Return 0 on success and -errno on error.
420 static inline int ntfs_clear_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
422 flags &= VOLUME_FLAGS_MASK;
423 flags = vol->vol_flags & cpu_to_le16(~le16_to_cpu(flags));
424 return ntfs_write_volume_flags(vol, flags);
427 #endif /* NTFS_RW */
430 * ntfs_remount - change the mount options of a mounted ntfs filesystem
431 * @sb: superblock of mounted ntfs filesystem
432 * @flags: remount flags
433 * @opt: remount options string
435 * Change the mount options of an already mounted ntfs filesystem.
437 * NOTE: The VFS sets the @sb->s_flags remount flags to @flags after
438 * ntfs_remount() returns successfully (i.e. returns 0). Otherwise,
439 * @sb->s_flags are not changed.
441 static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
443 ntfs_volume *vol = NTFS_SB(sb);
445 ntfs_debug("Entering with remount options string: %s", opt);
447 lock_kernel();
448 #ifndef NTFS_RW
449 /* For read-only compiled driver, enforce read-only flag. */
450 *flags |= MS_RDONLY;
451 #else /* NTFS_RW */
453 * For the read-write compiled driver, if we are remounting read-write,
454 * make sure there are no volume errors and that no unsupported volume
455 * flags are set. Also, empty the logfile journal as it would become
456 * stale as soon as something is written to the volume and mark the
457 * volume dirty so that chkdsk is run if the volume is not umounted
458 * cleanly. Finally, mark the quotas out of date so Windows rescans
459 * the volume on boot and updates them.
461 * When remounting read-only, mark the volume clean if no volume errors
462 * have occured.
464 if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
465 static const char *es = ". Cannot remount read-write.";
467 /* Remounting read-write. */
468 if (NVolErrors(vol)) {
469 ntfs_error(sb, "Volume has errors and is read-only%s",
470 es);
471 unlock_kernel();
472 return -EROFS;
474 if (vol->vol_flags & VOLUME_IS_DIRTY) {
475 ntfs_error(sb, "Volume is dirty and read-only%s", es);
476 unlock_kernel();
477 return -EROFS;
479 if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
480 ntfs_error(sb, "Volume has been modified by chkdsk "
481 "and is read-only%s", es);
482 unlock_kernel();
483 return -EROFS;
485 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
486 ntfs_error(sb, "Volume has unsupported flags set "
487 "(0x%x) and is read-only%s",
488 (unsigned)le16_to_cpu(vol->vol_flags),
489 es);
490 unlock_kernel();
491 return -EROFS;
493 if (ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
494 ntfs_error(sb, "Failed to set dirty bit in volume "
495 "information flags%s", es);
496 unlock_kernel();
497 return -EROFS;
499 #if 0
500 // TODO: Enable this code once we start modifying anything that
501 // is different between NTFS 1.2 and 3.x...
502 /* Set NT4 compatibility flag on newer NTFS version volumes. */
503 if ((vol->major_ver > 1)) {
504 if (ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
505 ntfs_error(sb, "Failed to set NT4 "
506 "compatibility flag%s", es);
507 NVolSetErrors(vol);
508 return -EROFS;
511 #endif
512 if (!ntfs_empty_logfile(vol->logfile_ino)) {
513 ntfs_error(sb, "Failed to empty journal $LogFile%s",
514 es);
515 NVolSetErrors(vol);
516 unlock_kernel();
517 return -EROFS;
519 if (!ntfs_mark_quotas_out_of_date(vol)) {
520 ntfs_error(sb, "Failed to mark quotas out of date%s",
521 es);
522 NVolSetErrors(vol);
523 unlock_kernel();
524 return -EROFS;
526 if (!ntfs_stamp_usnjrnl(vol)) {
527 ntfs_error(sb, "Failed to stamp transation log "
528 "($UsnJrnl)%s", es);
529 NVolSetErrors(vol);
530 unlock_kernel();
531 return -EROFS;
533 } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) {
534 /* Remounting read-only. */
535 if (!NVolErrors(vol)) {
536 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
537 ntfs_warning(sb, "Failed to clear dirty bit "
538 "in volume information "
539 "flags. Run chkdsk.");
542 #endif /* NTFS_RW */
544 // TODO: Deal with *flags.
546 if (!parse_options(vol, opt)) {
547 unlock_kernel();
548 return -EINVAL;
550 unlock_kernel();
551 ntfs_debug("Done.");
552 return 0;
556 * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector
557 * @sb: Super block of the device to which @b belongs.
558 * @b: Boot sector of device @sb to check.
559 * @silent: If 'true', all output will be silenced.
561 * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot
562 * sector. Returns 'true' if it is valid and 'false' if not.
564 * @sb is only needed for warning/error output, i.e. it can be NULL when silent
565 * is 'true'.
567 static bool is_boot_sector_ntfs(const struct super_block *sb,
568 const NTFS_BOOT_SECTOR *b, const bool silent)
571 * Check that checksum == sum of u32 values from b to the checksum
572 * field. If checksum is zero, no checking is done. We will work when
573 * the checksum test fails, since some utilities update the boot sector
574 * ignoring the checksum which leaves the checksum out-of-date. We
575 * report a warning if this is the case.
577 if ((void*)b < (void*)&b->checksum && b->checksum && !silent) {
578 le32 *u;
579 u32 i;
581 for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u)
582 i += le32_to_cpup(u);
583 if (le32_to_cpu(b->checksum) != i)
584 ntfs_warning(sb, "Invalid boot sector checksum.");
586 /* Check OEMidentifier is "NTFS " */
587 if (b->oem_id != magicNTFS)
588 goto not_ntfs;
589 /* Check bytes per sector value is between 256 and 4096. */
590 if (le16_to_cpu(b->bpb.bytes_per_sector) < 0x100 ||
591 le16_to_cpu(b->bpb.bytes_per_sector) > 0x1000)
592 goto not_ntfs;
593 /* Check sectors per cluster value is valid. */
594 switch (b->bpb.sectors_per_cluster) {
595 case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128:
596 break;
597 default:
598 goto not_ntfs;
600 /* Check the cluster size is not above the maximum (64kiB). */
601 if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) *
602 b->bpb.sectors_per_cluster > NTFS_MAX_CLUSTER_SIZE)
603 goto not_ntfs;
604 /* Check reserved/unused fields are really zero. */
605 if (le16_to_cpu(b->bpb.reserved_sectors) ||
606 le16_to_cpu(b->bpb.root_entries) ||
607 le16_to_cpu(b->bpb.sectors) ||
608 le16_to_cpu(b->bpb.sectors_per_fat) ||
609 le32_to_cpu(b->bpb.large_sectors) || b->bpb.fats)
610 goto not_ntfs;
611 /* Check clusters per file mft record value is valid. */
612 if ((u8)b->clusters_per_mft_record < 0xe1 ||
613 (u8)b->clusters_per_mft_record > 0xf7)
614 switch (b->clusters_per_mft_record) {
615 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
616 break;
617 default:
618 goto not_ntfs;
620 /* Check clusters per index block value is valid. */
621 if ((u8)b->clusters_per_index_record < 0xe1 ||
622 (u8)b->clusters_per_index_record > 0xf7)
623 switch (b->clusters_per_index_record) {
624 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
625 break;
626 default:
627 goto not_ntfs;
630 * Check for valid end of sector marker. We will work without it, but
631 * many BIOSes will refuse to boot from a bootsector if the magic is
632 * incorrect, so we emit a warning.
634 if (!silent && b->end_of_sector_marker != cpu_to_le16(0xaa55))
635 ntfs_warning(sb, "Invalid end of sector marker.");
636 return true;
637 not_ntfs:
638 return false;
642 * read_ntfs_boot_sector - read the NTFS boot sector of a device
643 * @sb: super block of device to read the boot sector from
644 * @silent: if true, suppress all output
646 * Reads the boot sector from the device and validates it. If that fails, tries
647 * to read the backup boot sector, first from the end of the device a-la NT4 and
648 * later and then from the middle of the device a-la NT3.51 and before.
650 * If a valid boot sector is found but it is not the primary boot sector, we
651 * repair the primary boot sector silently (unless the device is read-only or
652 * the primary boot sector is not accessible).
654 * NOTE: To call this function, @sb must have the fields s_dev, the ntfs super
655 * block (u.ntfs_sb), nr_blocks and the device flags (s_flags) initialized
656 * to their respective values.
658 * Return the unlocked buffer head containing the boot sector or NULL on error.
660 static struct buffer_head *read_ntfs_boot_sector(struct super_block *sb,
661 const int silent)
663 const char *read_err_str = "Unable to read %s boot sector.";
664 struct buffer_head *bh_primary, *bh_backup;
665 sector_t nr_blocks = NTFS_SB(sb)->nr_blocks;
667 /* Try to read primary boot sector. */
668 if ((bh_primary = sb_bread(sb, 0))) {
669 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
670 bh_primary->b_data, silent))
671 return bh_primary;
672 if (!silent)
673 ntfs_error(sb, "Primary boot sector is invalid.");
674 } else if (!silent)
675 ntfs_error(sb, read_err_str, "primary");
676 if (!(NTFS_SB(sb)->on_errors & ON_ERRORS_RECOVER)) {
677 if (bh_primary)
678 brelse(bh_primary);
679 if (!silent)
680 ntfs_error(sb, "Mount option errors=recover not used. "
681 "Aborting without trying to recover.");
682 return NULL;
684 /* Try to read NT4+ backup boot sector. */
685 if ((bh_backup = sb_bread(sb, nr_blocks - 1))) {
686 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
687 bh_backup->b_data, silent))
688 goto hotfix_primary_boot_sector;
689 brelse(bh_backup);
690 } else if (!silent)
691 ntfs_error(sb, read_err_str, "backup");
692 /* Try to read NT3.51- backup boot sector. */
693 if ((bh_backup = sb_bread(sb, nr_blocks >> 1))) {
694 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
695 bh_backup->b_data, silent))
696 goto hotfix_primary_boot_sector;
697 if (!silent)
698 ntfs_error(sb, "Could not find a valid backup boot "
699 "sector.");
700 brelse(bh_backup);
701 } else if (!silent)
702 ntfs_error(sb, read_err_str, "backup");
703 /* We failed. Cleanup and return. */
704 if (bh_primary)
705 brelse(bh_primary);
706 return NULL;
707 hotfix_primary_boot_sector:
708 if (bh_primary) {
710 * If we managed to read sector zero and the volume is not
711 * read-only, copy the found, valid backup boot sector to the
712 * primary boot sector. Note we only copy the actual boot
713 * sector structure, not the actual whole device sector as that
714 * may be bigger and would potentially damage the $Boot system
715 * file (FIXME: Would be nice to know if the backup boot sector
716 * on a large sector device contains the whole boot loader or
717 * just the first 512 bytes).
719 if (!(sb->s_flags & MS_RDONLY)) {
720 ntfs_warning(sb, "Hot-fix: Recovering invalid primary "
721 "boot sector from backup copy.");
722 memcpy(bh_primary->b_data, bh_backup->b_data,
723 NTFS_BLOCK_SIZE);
724 mark_buffer_dirty(bh_primary);
725 sync_dirty_buffer(bh_primary);
726 if (buffer_uptodate(bh_primary)) {
727 brelse(bh_backup);
728 return bh_primary;
730 ntfs_error(sb, "Hot-fix: Device write error while "
731 "recovering primary boot sector.");
732 } else {
733 ntfs_warning(sb, "Hot-fix: Recovery of primary boot "
734 "sector failed: Read-only mount.");
736 brelse(bh_primary);
738 ntfs_warning(sb, "Using backup boot sector.");
739 return bh_backup;
743 * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol
744 * @vol: volume structure to initialise with data from boot sector
745 * @b: boot sector to parse
747 * Parse the ntfs boot sector @b and store all imporant information therein in
748 * the ntfs super block @vol. Return 'true' on success and 'false' on error.
750 static bool parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
752 unsigned int sectors_per_cluster_bits, nr_hidden_sects;
753 int clusters_per_mft_record, clusters_per_index_record;
754 s64 ll;
756 vol->sector_size = le16_to_cpu(b->bpb.bytes_per_sector);
757 vol->sector_size_bits = ffs(vol->sector_size) - 1;
758 ntfs_debug("vol->sector_size = %i (0x%x)", vol->sector_size,
759 vol->sector_size);
760 ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits,
761 vol->sector_size_bits);
762 if (vol->sector_size < vol->sb->s_blocksize) {
763 ntfs_error(vol->sb, "Sector size (%i) is smaller than the "
764 "device block size (%lu). This is not "
765 "supported. Sorry.", vol->sector_size,
766 vol->sb->s_blocksize);
767 return false;
769 ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster);
770 sectors_per_cluster_bits = ffs(b->bpb.sectors_per_cluster) - 1;
771 ntfs_debug("sectors_per_cluster_bits = 0x%x",
772 sectors_per_cluster_bits);
773 nr_hidden_sects = le32_to_cpu(b->bpb.hidden_sectors);
774 ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects);
775 vol->cluster_size = vol->sector_size << sectors_per_cluster_bits;
776 vol->cluster_size_mask = vol->cluster_size - 1;
777 vol->cluster_size_bits = ffs(vol->cluster_size) - 1;
778 ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size,
779 vol->cluster_size);
780 ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask);
781 ntfs_debug("vol->cluster_size_bits = %i", vol->cluster_size_bits);
782 if (vol->cluster_size < vol->sector_size) {
783 ntfs_error(vol->sb, "Cluster size (%i) is smaller than the "
784 "sector size (%i). This is not supported. "
785 "Sorry.", vol->cluster_size, vol->sector_size);
786 return false;
788 clusters_per_mft_record = b->clusters_per_mft_record;
789 ntfs_debug("clusters_per_mft_record = %i (0x%x)",
790 clusters_per_mft_record, clusters_per_mft_record);
791 if (clusters_per_mft_record > 0)
792 vol->mft_record_size = vol->cluster_size <<
793 (ffs(clusters_per_mft_record) - 1);
794 else
796 * When mft_record_size < cluster_size, clusters_per_mft_record
797 * = -log2(mft_record_size) bytes. mft_record_size normaly is
798 * 1024 bytes, which is encoded as 0xF6 (-10 in decimal).
800 vol->mft_record_size = 1 << -clusters_per_mft_record;
801 vol->mft_record_size_mask = vol->mft_record_size - 1;
802 vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1;
803 ntfs_debug("vol->mft_record_size = %i (0x%x)", vol->mft_record_size,
804 vol->mft_record_size);
805 ntfs_debug("vol->mft_record_size_mask = 0x%x",
806 vol->mft_record_size_mask);
807 ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
808 vol->mft_record_size_bits, vol->mft_record_size_bits);
810 * We cannot support mft record sizes above the PAGE_CACHE_SIZE since
811 * we store $MFT/$DATA, the table of mft records in the page cache.
813 if (vol->mft_record_size > PAGE_CACHE_SIZE) {
814 ntfs_error(vol->sb, "Mft record size (%i) exceeds the "
815 "PAGE_CACHE_SIZE on your system (%lu). "
816 "This is not supported. Sorry.",
817 vol->mft_record_size, PAGE_CACHE_SIZE);
818 return false;
820 /* We cannot support mft record sizes below the sector size. */
821 if (vol->mft_record_size < vol->sector_size) {
822 ntfs_error(vol->sb, "Mft record size (%i) is smaller than the "
823 "sector size (%i). This is not supported. "
824 "Sorry.", vol->mft_record_size,
825 vol->sector_size);
826 return false;
828 clusters_per_index_record = b->clusters_per_index_record;
829 ntfs_debug("clusters_per_index_record = %i (0x%x)",
830 clusters_per_index_record, clusters_per_index_record);
831 if (clusters_per_index_record > 0)
832 vol->index_record_size = vol->cluster_size <<
833 (ffs(clusters_per_index_record) - 1);
834 else
836 * When index_record_size < cluster_size,
837 * clusters_per_index_record = -log2(index_record_size) bytes.
838 * index_record_size normaly equals 4096 bytes, which is
839 * encoded as 0xF4 (-12 in decimal).
841 vol->index_record_size = 1 << -clusters_per_index_record;
842 vol->index_record_size_mask = vol->index_record_size - 1;
843 vol->index_record_size_bits = ffs(vol->index_record_size) - 1;
844 ntfs_debug("vol->index_record_size = %i (0x%x)",
845 vol->index_record_size, vol->index_record_size);
846 ntfs_debug("vol->index_record_size_mask = 0x%x",
847 vol->index_record_size_mask);
848 ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
849 vol->index_record_size_bits,
850 vol->index_record_size_bits);
851 /* We cannot support index record sizes below the sector size. */
852 if (vol->index_record_size < vol->sector_size) {
853 ntfs_error(vol->sb, "Index record size (%i) is smaller than "
854 "the sector size (%i). This is not "
855 "supported. Sorry.", vol->index_record_size,
856 vol->sector_size);
857 return false;
860 * Get the size of the volume in clusters and check for 64-bit-ness.
861 * Windows currently only uses 32 bits to save the clusters so we do
862 * the same as it is much faster on 32-bit CPUs.
864 ll = sle64_to_cpu(b->number_of_sectors) >> sectors_per_cluster_bits;
865 if ((u64)ll >= 1ULL << 32) {
866 ntfs_error(vol->sb, "Cannot handle 64-bit clusters. Sorry.");
867 return false;
869 vol->nr_clusters = ll;
870 ntfs_debug("vol->nr_clusters = 0x%llx", (long long)vol->nr_clusters);
872 * On an architecture where unsigned long is 32-bits, we restrict the
873 * volume size to 2TiB (2^41). On a 64-bit architecture, the compiler
874 * will hopefully optimize the whole check away.
876 if (sizeof(unsigned long) < 8) {
877 if ((ll << vol->cluster_size_bits) >= (1ULL << 41)) {
878 ntfs_error(vol->sb, "Volume size (%lluTiB) is too "
879 "large for this architecture. "
880 "Maximum supported is 2TiB. Sorry.",
881 (unsigned long long)ll >> (40 -
882 vol->cluster_size_bits));
883 return false;
886 ll = sle64_to_cpu(b->mft_lcn);
887 if (ll >= vol->nr_clusters) {
888 ntfs_error(vol->sb, "MFT LCN (%lli, 0x%llx) is beyond end of "
889 "volume. Weird.", (unsigned long long)ll,
890 (unsigned long long)ll);
891 return false;
893 vol->mft_lcn = ll;
894 ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol->mft_lcn);
895 ll = sle64_to_cpu(b->mftmirr_lcn);
896 if (ll >= vol->nr_clusters) {
897 ntfs_error(vol->sb, "MFTMirr LCN (%lli, 0x%llx) is beyond end "
898 "of volume. Weird.", (unsigned long long)ll,
899 (unsigned long long)ll);
900 return false;
902 vol->mftmirr_lcn = ll;
903 ntfs_debug("vol->mftmirr_lcn = 0x%llx", (long long)vol->mftmirr_lcn);
904 #ifdef NTFS_RW
906 * Work out the size of the mft mirror in number of mft records. If the
907 * cluster size is less than or equal to the size taken by four mft
908 * records, the mft mirror stores the first four mft records. If the
909 * cluster size is bigger than the size taken by four mft records, the
910 * mft mirror contains as many mft records as will fit into one
911 * cluster.
913 if (vol->cluster_size <= (4 << vol->mft_record_size_bits))
914 vol->mftmirr_size = 4;
915 else
916 vol->mftmirr_size = vol->cluster_size >>
917 vol->mft_record_size_bits;
918 ntfs_debug("vol->mftmirr_size = %i", vol->mftmirr_size);
919 #endif /* NTFS_RW */
920 vol->serial_no = le64_to_cpu(b->volume_serial_number);
921 ntfs_debug("vol->serial_no = 0x%llx",
922 (unsigned long long)vol->serial_no);
923 return true;
927 * ntfs_setup_allocators - initialize the cluster and mft allocators
928 * @vol: volume structure for which to setup the allocators
930 * Setup the cluster (lcn) and mft allocators to the starting values.
932 static void ntfs_setup_allocators(ntfs_volume *vol)
934 #ifdef NTFS_RW
935 LCN mft_zone_size, mft_lcn;
936 #endif /* NTFS_RW */
938 ntfs_debug("vol->mft_zone_multiplier = 0x%x",
939 vol->mft_zone_multiplier);
940 #ifdef NTFS_RW
941 /* Determine the size of the MFT zone. */
942 mft_zone_size = vol->nr_clusters;
943 switch (vol->mft_zone_multiplier) { /* % of volume size in clusters */
944 case 4:
945 mft_zone_size >>= 1; /* 50% */
946 break;
947 case 3:
948 mft_zone_size = (mft_zone_size +
949 (mft_zone_size >> 1)) >> 2; /* 37.5% */
950 break;
951 case 2:
952 mft_zone_size >>= 2; /* 25% */
953 break;
954 /* case 1: */
955 default:
956 mft_zone_size >>= 3; /* 12.5% */
957 break;
959 /* Setup the mft zone. */
960 vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn;
961 ntfs_debug("vol->mft_zone_pos = 0x%llx",
962 (unsigned long long)vol->mft_zone_pos);
964 * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
965 * source) and if the actual mft_lcn is in the expected place or even
966 * further to the front of the volume, extend the mft_zone to cover the
967 * beginning of the volume as well. This is in order to protect the
968 * area reserved for the mft bitmap as well within the mft_zone itself.
969 * On non-standard volumes we do not protect it as the overhead would
970 * be higher than the speed increase we would get by doing it.
972 mft_lcn = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size;
973 if (mft_lcn * vol->cluster_size < 16 * 1024)
974 mft_lcn = (16 * 1024 + vol->cluster_size - 1) /
975 vol->cluster_size;
976 if (vol->mft_zone_start <= mft_lcn)
977 vol->mft_zone_start = 0;
978 ntfs_debug("vol->mft_zone_start = 0x%llx",
979 (unsigned long long)vol->mft_zone_start);
981 * Need to cap the mft zone on non-standard volumes so that it does
982 * not point outside the boundaries of the volume. We do this by
983 * halving the zone size until we are inside the volume.
985 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
986 while (vol->mft_zone_end >= vol->nr_clusters) {
987 mft_zone_size >>= 1;
988 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
990 ntfs_debug("vol->mft_zone_end = 0x%llx",
991 (unsigned long long)vol->mft_zone_end);
993 * Set the current position within each data zone to the start of the
994 * respective zone.
996 vol->data1_zone_pos = vol->mft_zone_end;
997 ntfs_debug("vol->data1_zone_pos = 0x%llx",
998 (unsigned long long)vol->data1_zone_pos);
999 vol->data2_zone_pos = 0;
1000 ntfs_debug("vol->data2_zone_pos = 0x%llx",
1001 (unsigned long long)vol->data2_zone_pos);
1003 /* Set the mft data allocation position to mft record 24. */
1004 vol->mft_data_pos = 24;
1005 ntfs_debug("vol->mft_data_pos = 0x%llx",
1006 (unsigned long long)vol->mft_data_pos);
1007 #endif /* NTFS_RW */
1010 #ifdef NTFS_RW
1013 * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume
1014 * @vol: ntfs super block describing device whose mft mirror to load
1016 * Return 'true' on success or 'false' on error.
1018 static bool load_and_init_mft_mirror(ntfs_volume *vol)
1020 struct inode *tmp_ino;
1021 ntfs_inode *tmp_ni;
1023 ntfs_debug("Entering.");
1024 /* Get mft mirror inode. */
1025 tmp_ino = ntfs_iget(vol->sb, FILE_MFTMirr);
1026 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1027 if (!IS_ERR(tmp_ino))
1028 iput(tmp_ino);
1029 /* Caller will display error message. */
1030 return false;
1033 * Re-initialize some specifics about $MFTMirr's inode as
1034 * ntfs_read_inode() will have set up the default ones.
1036 /* Set uid and gid to root. */
1037 tmp_ino->i_uid = tmp_ino->i_gid = 0;
1038 /* Regular file. No access for anyone. */
1039 tmp_ino->i_mode = S_IFREG;
1040 /* No VFS initiated operations allowed for $MFTMirr. */
1041 tmp_ino->i_op = &ntfs_empty_inode_ops;
1042 tmp_ino->i_fop = &ntfs_empty_file_ops;
1043 /* Put in our special address space operations. */
1044 tmp_ino->i_mapping->a_ops = &ntfs_mst_aops;
1045 tmp_ni = NTFS_I(tmp_ino);
1046 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
1047 NInoSetMstProtected(tmp_ni);
1048 NInoSetSparseDisabled(tmp_ni);
1050 * Set up our little cheat allowing us to reuse the async read io
1051 * completion handler for directories.
1053 tmp_ni->itype.index.block_size = vol->mft_record_size;
1054 tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits;
1055 vol->mftmirr_ino = tmp_ino;
1056 ntfs_debug("Done.");
1057 return true;
1061 * check_mft_mirror - compare contents of the mft mirror with the mft
1062 * @vol: ntfs super block describing device whose mft mirror to check
1064 * Return 'true' on success or 'false' on error.
1066 * Note, this function also results in the mft mirror runlist being completely
1067 * mapped into memory. The mft mirror write code requires this and will BUG()
1068 * should it find an unmapped runlist element.
1070 static bool check_mft_mirror(ntfs_volume *vol)
1072 struct super_block *sb = vol->sb;
1073 ntfs_inode *mirr_ni;
1074 struct page *mft_page, *mirr_page;
1075 u8 *kmft, *kmirr;
1076 runlist_element *rl, rl2[2];
1077 pgoff_t index;
1078 int mrecs_per_page, i;
1080 ntfs_debug("Entering.");
1081 /* Compare contents of $MFT and $MFTMirr. */
1082 mrecs_per_page = PAGE_CACHE_SIZE / vol->mft_record_size;
1083 BUG_ON(!mrecs_per_page);
1084 BUG_ON(!vol->mftmirr_size);
1085 mft_page = mirr_page = NULL;
1086 kmft = kmirr = NULL;
1087 index = i = 0;
1088 do {
1089 u32 bytes;
1091 /* Switch pages if necessary. */
1092 if (!(i % mrecs_per_page)) {
1093 if (index) {
1094 ntfs_unmap_page(mft_page);
1095 ntfs_unmap_page(mirr_page);
1097 /* Get the $MFT page. */
1098 mft_page = ntfs_map_page(vol->mft_ino->i_mapping,
1099 index);
1100 if (IS_ERR(mft_page)) {
1101 ntfs_error(sb, "Failed to read $MFT.");
1102 return false;
1104 kmft = page_address(mft_page);
1105 /* Get the $MFTMirr page. */
1106 mirr_page = ntfs_map_page(vol->mftmirr_ino->i_mapping,
1107 index);
1108 if (IS_ERR(mirr_page)) {
1109 ntfs_error(sb, "Failed to read $MFTMirr.");
1110 goto mft_unmap_out;
1112 kmirr = page_address(mirr_page);
1113 ++index;
1115 /* Do not check the record if it is not in use. */
1116 if (((MFT_RECORD*)kmft)->flags & MFT_RECORD_IN_USE) {
1117 /* Make sure the record is ok. */
1118 if (ntfs_is_baad_recordp((le32*)kmft)) {
1119 ntfs_error(sb, "Incomplete multi sector "
1120 "transfer detected in mft "
1121 "record %i.", i);
1122 mm_unmap_out:
1123 ntfs_unmap_page(mirr_page);
1124 mft_unmap_out:
1125 ntfs_unmap_page(mft_page);
1126 return false;
1129 /* Do not check the mirror record if it is not in use. */
1130 if (((MFT_RECORD*)kmirr)->flags & MFT_RECORD_IN_USE) {
1131 if (ntfs_is_baad_recordp((le32*)kmirr)) {
1132 ntfs_error(sb, "Incomplete multi sector "
1133 "transfer detected in mft "
1134 "mirror record %i.", i);
1135 goto mm_unmap_out;
1138 /* Get the amount of data in the current record. */
1139 bytes = le32_to_cpu(((MFT_RECORD*)kmft)->bytes_in_use);
1140 if (bytes < sizeof(MFT_RECORD_OLD) ||
1141 bytes > vol->mft_record_size ||
1142 ntfs_is_baad_recordp((le32*)kmft)) {
1143 bytes = le32_to_cpu(((MFT_RECORD*)kmirr)->bytes_in_use);
1144 if (bytes < sizeof(MFT_RECORD_OLD) ||
1145 bytes > vol->mft_record_size ||
1146 ntfs_is_baad_recordp((le32*)kmirr))
1147 bytes = vol->mft_record_size;
1149 /* Compare the two records. */
1150 if (memcmp(kmft, kmirr, bytes)) {
1151 ntfs_error(sb, "$MFT and $MFTMirr (record %i) do not "
1152 "match. Run ntfsfix or chkdsk.", i);
1153 goto mm_unmap_out;
1155 kmft += vol->mft_record_size;
1156 kmirr += vol->mft_record_size;
1157 } while (++i < vol->mftmirr_size);
1158 /* Release the last pages. */
1159 ntfs_unmap_page(mft_page);
1160 ntfs_unmap_page(mirr_page);
1162 /* Construct the mft mirror runlist by hand. */
1163 rl2[0].vcn = 0;
1164 rl2[0].lcn = vol->mftmirr_lcn;
1165 rl2[0].length = (vol->mftmirr_size * vol->mft_record_size +
1166 vol->cluster_size - 1) / vol->cluster_size;
1167 rl2[1].vcn = rl2[0].length;
1168 rl2[1].lcn = LCN_ENOENT;
1169 rl2[1].length = 0;
1171 * Because we have just read all of the mft mirror, we know we have
1172 * mapped the full runlist for it.
1174 mirr_ni = NTFS_I(vol->mftmirr_ino);
1175 down_read(&mirr_ni->runlist.lock);
1176 rl = mirr_ni->runlist.rl;
1177 /* Compare the two runlists. They must be identical. */
1178 i = 0;
1179 do {
1180 if (rl2[i].vcn != rl[i].vcn || rl2[i].lcn != rl[i].lcn ||
1181 rl2[i].length != rl[i].length) {
1182 ntfs_error(sb, "$MFTMirr location mismatch. "
1183 "Run chkdsk.");
1184 up_read(&mirr_ni->runlist.lock);
1185 return false;
1187 } while (rl2[i++].length);
1188 up_read(&mirr_ni->runlist.lock);
1189 ntfs_debug("Done.");
1190 return true;
1194 * load_and_check_logfile - load and check the logfile inode for a volume
1195 * @vol: ntfs super block describing device whose logfile to load
1197 * Return 'true' on success or 'false' on error.
1199 static bool load_and_check_logfile(ntfs_volume *vol,
1200 RESTART_PAGE_HEADER **rp)
1202 struct inode *tmp_ino;
1204 ntfs_debug("Entering.");
1205 tmp_ino = ntfs_iget(vol->sb, FILE_LogFile);
1206 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1207 if (!IS_ERR(tmp_ino))
1208 iput(tmp_ino);
1209 /* Caller will display error message. */
1210 return false;
1212 if (!ntfs_check_logfile(tmp_ino, rp)) {
1213 iput(tmp_ino);
1214 /* ntfs_check_logfile() will have displayed error output. */
1215 return false;
1217 NInoSetSparseDisabled(NTFS_I(tmp_ino));
1218 vol->logfile_ino = tmp_ino;
1219 ntfs_debug("Done.");
1220 return true;
1223 #define NTFS_HIBERFIL_HEADER_SIZE 4096
1226 * check_windows_hibernation_status - check if Windows is suspended on a volume
1227 * @vol: ntfs super block of device to check
1229 * Check if Windows is hibernated on the ntfs volume @vol. This is done by
1230 * looking for the file hiberfil.sys in the root directory of the volume. If
1231 * the file is not present Windows is definitely not suspended.
1233 * If hiberfil.sys exists and is less than 4kiB in size it means Windows is
1234 * definitely suspended (this volume is not the system volume). Caveat: on a
1235 * system with many volumes it is possible that the < 4kiB check is bogus but
1236 * for now this should do fine.
1238 * If hiberfil.sys exists and is larger than 4kiB in size, we need to read the
1239 * hiberfil header (which is the first 4kiB). If this begins with "hibr",
1240 * Windows is definitely suspended. If it is completely full of zeroes,
1241 * Windows is definitely not hibernated. Any other case is treated as if
1242 * Windows is suspended. This caters for the above mentioned caveat of a
1243 * system with many volumes where no "hibr" magic would be present and there is
1244 * no zero header.
1246 * Return 0 if Windows is not hibernated on the volume, >0 if Windows is
1247 * hibernated on the volume, and -errno on error.
1249 static int check_windows_hibernation_status(ntfs_volume *vol)
1251 MFT_REF mref;
1252 struct inode *vi;
1253 ntfs_inode *ni;
1254 struct page *page;
1255 u32 *kaddr, *kend;
1256 ntfs_name *name = NULL;
1257 int ret = 1;
1258 static const ntfschar hiberfil[13] = { cpu_to_le16('h'),
1259 cpu_to_le16('i'), cpu_to_le16('b'),
1260 cpu_to_le16('e'), cpu_to_le16('r'),
1261 cpu_to_le16('f'), cpu_to_le16('i'),
1262 cpu_to_le16('l'), cpu_to_le16('.'),
1263 cpu_to_le16('s'), cpu_to_le16('y'),
1264 cpu_to_le16('s'), 0 };
1266 ntfs_debug("Entering.");
1268 * Find the inode number for the hibernation file by looking up the
1269 * filename hiberfil.sys in the root directory.
1271 mutex_lock(&vol->root_ino->i_mutex);
1272 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->root_ino), hiberfil, 12,
1273 &name);
1274 mutex_unlock(&vol->root_ino->i_mutex);
1275 if (IS_ERR_MREF(mref)) {
1276 ret = MREF_ERR(mref);
1277 /* If the file does not exist, Windows is not hibernated. */
1278 if (ret == -ENOENT) {
1279 ntfs_debug("hiberfil.sys not present. Windows is not "
1280 "hibernated on the volume.");
1281 return 0;
1283 /* A real error occured. */
1284 ntfs_error(vol->sb, "Failed to find inode number for "
1285 "hiberfil.sys.");
1286 return ret;
1288 /* We do not care for the type of match that was found. */
1289 kfree(name);
1290 /* Get the inode. */
1291 vi = ntfs_iget(vol->sb, MREF(mref));
1292 if (IS_ERR(vi) || is_bad_inode(vi)) {
1293 if (!IS_ERR(vi))
1294 iput(vi);
1295 ntfs_error(vol->sb, "Failed to load hiberfil.sys.");
1296 return IS_ERR(vi) ? PTR_ERR(vi) : -EIO;
1298 if (unlikely(i_size_read(vi) < NTFS_HIBERFIL_HEADER_SIZE)) {
1299 ntfs_debug("hiberfil.sys is smaller than 4kiB (0x%llx). "
1300 "Windows is hibernated on the volume. This "
1301 "is not the system volume.", i_size_read(vi));
1302 goto iput_out;
1304 ni = NTFS_I(vi);
1305 page = ntfs_map_page(vi->i_mapping, 0);
1306 if (IS_ERR(page)) {
1307 ntfs_error(vol->sb, "Failed to read from hiberfil.sys.");
1308 ret = PTR_ERR(page);
1309 goto iput_out;
1311 kaddr = (u32*)page_address(page);
1312 if (*(le32*)kaddr == cpu_to_le32(0x72626968)/*'hibr'*/) {
1313 ntfs_debug("Magic \"hibr\" found in hiberfil.sys. Windows is "
1314 "hibernated on the volume. This is the "
1315 "system volume.");
1316 goto unm_iput_out;
1318 kend = kaddr + NTFS_HIBERFIL_HEADER_SIZE/sizeof(*kaddr);
1319 do {
1320 if (unlikely(*kaddr)) {
1321 ntfs_debug("hiberfil.sys is larger than 4kiB "
1322 "(0x%llx), does not contain the "
1323 "\"hibr\" magic, and does not have a "
1324 "zero header. Windows is hibernated "
1325 "on the volume. This is not the "
1326 "system volume.", i_size_read(vi));
1327 goto unm_iput_out;
1329 } while (++kaddr < kend);
1330 ntfs_debug("hiberfil.sys contains a zero header. Windows is not "
1331 "hibernated on the volume. This is the system "
1332 "volume.");
1333 ret = 0;
1334 unm_iput_out:
1335 ntfs_unmap_page(page);
1336 iput_out:
1337 iput(vi);
1338 return ret;
1342 * load_and_init_quota - load and setup the quota file for a volume if present
1343 * @vol: ntfs super block describing device whose quota file to load
1345 * Return 'true' on success or 'false' on error. If $Quota is not present, we
1346 * leave vol->quota_ino as NULL and return success.
1348 static bool load_and_init_quota(ntfs_volume *vol)
1350 MFT_REF mref;
1351 struct inode *tmp_ino;
1352 ntfs_name *name = NULL;
1353 static const ntfschar Quota[7] = { cpu_to_le16('$'),
1354 cpu_to_le16('Q'), cpu_to_le16('u'),
1355 cpu_to_le16('o'), cpu_to_le16('t'),
1356 cpu_to_le16('a'), 0 };
1357 static ntfschar Q[3] = { cpu_to_le16('$'),
1358 cpu_to_le16('Q'), 0 };
1360 ntfs_debug("Entering.");
1362 * Find the inode number for the quota file by looking up the filename
1363 * $Quota in the extended system files directory $Extend.
1365 mutex_lock(&vol->extend_ino->i_mutex);
1366 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), Quota, 6,
1367 &name);
1368 mutex_unlock(&vol->extend_ino->i_mutex);
1369 if (IS_ERR_MREF(mref)) {
1371 * If the file does not exist, quotas are disabled and have
1372 * never been enabled on this volume, just return success.
1374 if (MREF_ERR(mref) == -ENOENT) {
1375 ntfs_debug("$Quota not present. Volume does not have "
1376 "quotas enabled.");
1378 * No need to try to set quotas out of date if they are
1379 * not enabled.
1381 NVolSetQuotaOutOfDate(vol);
1382 return true;
1384 /* A real error occured. */
1385 ntfs_error(vol->sb, "Failed to find inode number for $Quota.");
1386 return false;
1388 /* We do not care for the type of match that was found. */
1389 kfree(name);
1390 /* Get the inode. */
1391 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1392 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1393 if (!IS_ERR(tmp_ino))
1394 iput(tmp_ino);
1395 ntfs_error(vol->sb, "Failed to load $Quota.");
1396 return false;
1398 vol->quota_ino = tmp_ino;
1399 /* Get the $Q index allocation attribute. */
1400 tmp_ino = ntfs_index_iget(vol->quota_ino, Q, 2);
1401 if (IS_ERR(tmp_ino)) {
1402 ntfs_error(vol->sb, "Failed to load $Quota/$Q index.");
1403 return false;
1405 vol->quota_q_ino = tmp_ino;
1406 ntfs_debug("Done.");
1407 return true;
1411 * load_and_init_usnjrnl - load and setup the transaction log if present
1412 * @vol: ntfs super block describing device whose usnjrnl file to load
1414 * Return 'true' on success or 'false' on error.
1416 * If $UsnJrnl is not present or in the process of being disabled, we set
1417 * NVolUsnJrnlStamped() and return success.
1419 * If the $UsnJrnl $DATA/$J attribute has a size equal to the lowest valid usn,
1420 * i.e. transaction logging has only just been enabled or the journal has been
1421 * stamped and nothing has been logged since, we also set NVolUsnJrnlStamped()
1422 * and return success.
1424 static bool load_and_init_usnjrnl(ntfs_volume *vol)
1426 MFT_REF mref;
1427 struct inode *tmp_ino;
1428 ntfs_inode *tmp_ni;
1429 struct page *page;
1430 ntfs_name *name = NULL;
1431 USN_HEADER *uh;
1432 static const ntfschar UsnJrnl[9] = { cpu_to_le16('$'),
1433 cpu_to_le16('U'), cpu_to_le16('s'),
1434 cpu_to_le16('n'), cpu_to_le16('J'),
1435 cpu_to_le16('r'), cpu_to_le16('n'),
1436 cpu_to_le16('l'), 0 };
1437 static ntfschar Max[5] = { cpu_to_le16('$'),
1438 cpu_to_le16('M'), cpu_to_le16('a'),
1439 cpu_to_le16('x'), 0 };
1440 static ntfschar J[3] = { cpu_to_le16('$'),
1441 cpu_to_le16('J'), 0 };
1443 ntfs_debug("Entering.");
1445 * Find the inode number for the transaction log file by looking up the
1446 * filename $UsnJrnl in the extended system files directory $Extend.
1448 mutex_lock(&vol->extend_ino->i_mutex);
1449 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), UsnJrnl, 8,
1450 &name);
1451 mutex_unlock(&vol->extend_ino->i_mutex);
1452 if (IS_ERR_MREF(mref)) {
1454 * If the file does not exist, transaction logging is disabled,
1455 * just return success.
1457 if (MREF_ERR(mref) == -ENOENT) {
1458 ntfs_debug("$UsnJrnl not present. Volume does not "
1459 "have transaction logging enabled.");
1460 not_enabled:
1462 * No need to try to stamp the transaction log if
1463 * transaction logging is not enabled.
1465 NVolSetUsnJrnlStamped(vol);
1466 return true;
1468 /* A real error occured. */
1469 ntfs_error(vol->sb, "Failed to find inode number for "
1470 "$UsnJrnl.");
1471 return false;
1473 /* We do not care for the type of match that was found. */
1474 kfree(name);
1475 /* Get the inode. */
1476 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1477 if (unlikely(IS_ERR(tmp_ino) || is_bad_inode(tmp_ino))) {
1478 if (!IS_ERR(tmp_ino))
1479 iput(tmp_ino);
1480 ntfs_error(vol->sb, "Failed to load $UsnJrnl.");
1481 return false;
1483 vol->usnjrnl_ino = tmp_ino;
1485 * If the transaction log is in the process of being deleted, we can
1486 * ignore it.
1488 if (unlikely(vol->vol_flags & VOLUME_DELETE_USN_UNDERWAY)) {
1489 ntfs_debug("$UsnJrnl in the process of being disabled. "
1490 "Volume does not have transaction logging "
1491 "enabled.");
1492 goto not_enabled;
1494 /* Get the $DATA/$Max attribute. */
1495 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, Max, 4);
1496 if (IS_ERR(tmp_ino)) {
1497 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$Max "
1498 "attribute.");
1499 return false;
1501 vol->usnjrnl_max_ino = tmp_ino;
1502 if (unlikely(i_size_read(tmp_ino) < sizeof(USN_HEADER))) {
1503 ntfs_error(vol->sb, "Found corrupt $UsnJrnl/$DATA/$Max "
1504 "attribute (size is 0x%llx but should be at "
1505 "least 0x%zx bytes).", i_size_read(tmp_ino),
1506 sizeof(USN_HEADER));
1507 return false;
1509 /* Get the $DATA/$J attribute. */
1510 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, J, 2);
1511 if (IS_ERR(tmp_ino)) {
1512 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$J "
1513 "attribute.");
1514 return false;
1516 vol->usnjrnl_j_ino = tmp_ino;
1517 /* Verify $J is non-resident and sparse. */
1518 tmp_ni = NTFS_I(vol->usnjrnl_j_ino);
1519 if (unlikely(!NInoNonResident(tmp_ni) || !NInoSparse(tmp_ni))) {
1520 ntfs_error(vol->sb, "$UsnJrnl/$DATA/$J attribute is resident "
1521 "and/or not sparse.");
1522 return false;
1524 /* Read the USN_HEADER from $DATA/$Max. */
1525 page = ntfs_map_page(vol->usnjrnl_max_ino->i_mapping, 0);
1526 if (IS_ERR(page)) {
1527 ntfs_error(vol->sb, "Failed to read from $UsnJrnl/$DATA/$Max "
1528 "attribute.");
1529 return false;
1531 uh = (USN_HEADER*)page_address(page);
1532 /* Sanity check the $Max. */
1533 if (unlikely(sle64_to_cpu(uh->allocation_delta) >
1534 sle64_to_cpu(uh->maximum_size))) {
1535 ntfs_error(vol->sb, "Allocation delta (0x%llx) exceeds "
1536 "maximum size (0x%llx). $UsnJrnl is corrupt.",
1537 (long long)sle64_to_cpu(uh->allocation_delta),
1538 (long long)sle64_to_cpu(uh->maximum_size));
1539 ntfs_unmap_page(page);
1540 return false;
1543 * If the transaction log has been stamped and nothing has been written
1544 * to it since, we do not need to stamp it.
1546 if (unlikely(sle64_to_cpu(uh->lowest_valid_usn) >=
1547 i_size_read(vol->usnjrnl_j_ino))) {
1548 if (likely(sle64_to_cpu(uh->lowest_valid_usn) ==
1549 i_size_read(vol->usnjrnl_j_ino))) {
1550 ntfs_unmap_page(page);
1551 ntfs_debug("$UsnJrnl is enabled but nothing has been "
1552 "logged since it was last stamped. "
1553 "Treating this as if the volume does "
1554 "not have transaction logging "
1555 "enabled.");
1556 goto not_enabled;
1558 ntfs_error(vol->sb, "$UsnJrnl has lowest valid usn (0x%llx) "
1559 "which is out of bounds (0x%llx). $UsnJrnl "
1560 "is corrupt.",
1561 (long long)sle64_to_cpu(uh->lowest_valid_usn),
1562 i_size_read(vol->usnjrnl_j_ino));
1563 ntfs_unmap_page(page);
1564 return false;
1566 ntfs_unmap_page(page);
1567 ntfs_debug("Done.");
1568 return true;
1572 * load_and_init_attrdef - load the attribute definitions table for a volume
1573 * @vol: ntfs super block describing device whose attrdef to load
1575 * Return 'true' on success or 'false' on error.
1577 static bool load_and_init_attrdef(ntfs_volume *vol)
1579 loff_t i_size;
1580 struct super_block *sb = vol->sb;
1581 struct inode *ino;
1582 struct page *page;
1583 pgoff_t index, max_index;
1584 unsigned int size;
1586 ntfs_debug("Entering.");
1587 /* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */
1588 ino = ntfs_iget(sb, FILE_AttrDef);
1589 if (IS_ERR(ino) || is_bad_inode(ino)) {
1590 if (!IS_ERR(ino))
1591 iput(ino);
1592 goto failed;
1594 NInoSetSparseDisabled(NTFS_I(ino));
1595 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
1596 i_size = i_size_read(ino);
1597 if (i_size <= 0 || i_size > 0x7fffffff)
1598 goto iput_failed;
1599 vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(i_size);
1600 if (!vol->attrdef)
1601 goto iput_failed;
1602 index = 0;
1603 max_index = i_size >> PAGE_CACHE_SHIFT;
1604 size = PAGE_CACHE_SIZE;
1605 while (index < max_index) {
1606 /* Read the attrdef table and copy it into the linear buffer. */
1607 read_partial_attrdef_page:
1608 page = ntfs_map_page(ino->i_mapping, index);
1609 if (IS_ERR(page))
1610 goto free_iput_failed;
1611 memcpy((u8*)vol->attrdef + (index++ << PAGE_CACHE_SHIFT),
1612 page_address(page), size);
1613 ntfs_unmap_page(page);
1615 if (size == PAGE_CACHE_SIZE) {
1616 size = i_size & ~PAGE_CACHE_MASK;
1617 if (size)
1618 goto read_partial_attrdef_page;
1620 vol->attrdef_size = i_size;
1621 ntfs_debug("Read %llu bytes from $AttrDef.", i_size);
1622 iput(ino);
1623 return true;
1624 free_iput_failed:
1625 ntfs_free(vol->attrdef);
1626 vol->attrdef = NULL;
1627 iput_failed:
1628 iput(ino);
1629 failed:
1630 ntfs_error(sb, "Failed to initialize attribute definition table.");
1631 return false;
1634 #endif /* NTFS_RW */
1637 * load_and_init_upcase - load the upcase table for an ntfs volume
1638 * @vol: ntfs super block describing device whose upcase to load
1640 * Return 'true' on success or 'false' on error.
1642 static bool load_and_init_upcase(ntfs_volume *vol)
1644 loff_t i_size;
1645 struct super_block *sb = vol->sb;
1646 struct inode *ino;
1647 struct page *page;
1648 pgoff_t index, max_index;
1649 unsigned int size;
1650 int i, max;
1652 ntfs_debug("Entering.");
1653 /* Read upcase table and setup vol->upcase and vol->upcase_len. */
1654 ino = ntfs_iget(sb, FILE_UpCase);
1655 if (IS_ERR(ino) || is_bad_inode(ino)) {
1656 if (!IS_ERR(ino))
1657 iput(ino);
1658 goto upcase_failed;
1661 * The upcase size must not be above 64k Unicode characters, must not
1662 * be zero and must be a multiple of sizeof(ntfschar).
1664 i_size = i_size_read(ino);
1665 if (!i_size || i_size & (sizeof(ntfschar) - 1) ||
1666 i_size > 64ULL * 1024 * sizeof(ntfschar))
1667 goto iput_upcase_failed;
1668 vol->upcase = (ntfschar*)ntfs_malloc_nofs(i_size);
1669 if (!vol->upcase)
1670 goto iput_upcase_failed;
1671 index = 0;
1672 max_index = i_size >> PAGE_CACHE_SHIFT;
1673 size = PAGE_CACHE_SIZE;
1674 while (index < max_index) {
1675 /* Read the upcase table and copy it into the linear buffer. */
1676 read_partial_upcase_page:
1677 page = ntfs_map_page(ino->i_mapping, index);
1678 if (IS_ERR(page))
1679 goto iput_upcase_failed;
1680 memcpy((char*)vol->upcase + (index++ << PAGE_CACHE_SHIFT),
1681 page_address(page), size);
1682 ntfs_unmap_page(page);
1684 if (size == PAGE_CACHE_SIZE) {
1685 size = i_size & ~PAGE_CACHE_MASK;
1686 if (size)
1687 goto read_partial_upcase_page;
1689 vol->upcase_len = i_size >> UCHAR_T_SIZE_BITS;
1690 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
1691 i_size, 64 * 1024 * sizeof(ntfschar));
1692 iput(ino);
1693 mutex_lock(&ntfs_lock);
1694 if (!default_upcase) {
1695 ntfs_debug("Using volume specified $UpCase since default is "
1696 "not present.");
1697 mutex_unlock(&ntfs_lock);
1698 return true;
1700 max = default_upcase_len;
1701 if (max > vol->upcase_len)
1702 max = vol->upcase_len;
1703 for (i = 0; i < max; i++)
1704 if (vol->upcase[i] != default_upcase[i])
1705 break;
1706 if (i == max) {
1707 ntfs_free(vol->upcase);
1708 vol->upcase = default_upcase;
1709 vol->upcase_len = max;
1710 ntfs_nr_upcase_users++;
1711 mutex_unlock(&ntfs_lock);
1712 ntfs_debug("Volume specified $UpCase matches default. Using "
1713 "default.");
1714 return true;
1716 mutex_unlock(&ntfs_lock);
1717 ntfs_debug("Using volume specified $UpCase since it does not match "
1718 "the default.");
1719 return true;
1720 iput_upcase_failed:
1721 iput(ino);
1722 ntfs_free(vol->upcase);
1723 vol->upcase = NULL;
1724 upcase_failed:
1725 mutex_lock(&ntfs_lock);
1726 if (default_upcase) {
1727 vol->upcase = default_upcase;
1728 vol->upcase_len = default_upcase_len;
1729 ntfs_nr_upcase_users++;
1730 mutex_unlock(&ntfs_lock);
1731 ntfs_error(sb, "Failed to load $UpCase from the volume. Using "
1732 "default.");
1733 return true;
1735 mutex_unlock(&ntfs_lock);
1736 ntfs_error(sb, "Failed to initialize upcase table.");
1737 return false;
1741 * The lcn and mft bitmap inodes are NTFS-internal inodes with
1742 * their own special locking rules:
1744 static struct lock_class_key
1745 lcnbmp_runlist_lock_key, lcnbmp_mrec_lock_key,
1746 mftbmp_runlist_lock_key, mftbmp_mrec_lock_key;
1749 * load_system_files - open the system files using normal functions
1750 * @vol: ntfs super block describing device whose system files to load
1752 * Open the system files with normal access functions and complete setting up
1753 * the ntfs super block @vol.
1755 * Return 'true' on success or 'false' on error.
1757 static bool load_system_files(ntfs_volume *vol)
1759 struct super_block *sb = vol->sb;
1760 MFT_RECORD *m;
1761 VOLUME_INFORMATION *vi;
1762 ntfs_attr_search_ctx *ctx;
1763 #ifdef NTFS_RW
1764 RESTART_PAGE_HEADER *rp;
1765 int err;
1766 #endif /* NTFS_RW */
1768 ntfs_debug("Entering.");
1769 #ifdef NTFS_RW
1770 /* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */
1771 if (!load_and_init_mft_mirror(vol) || !check_mft_mirror(vol)) {
1772 static const char *es1 = "Failed to load $MFTMirr";
1773 static const char *es2 = "$MFTMirr does not match $MFT";
1774 static const char *es3 = ". Run ntfsfix and/or chkdsk.";
1776 /* If a read-write mount, convert it to a read-only mount. */
1777 if (!(sb->s_flags & MS_RDONLY)) {
1778 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1779 ON_ERRORS_CONTINUE))) {
1780 ntfs_error(sb, "%s and neither on_errors="
1781 "continue nor on_errors="
1782 "remount-ro was specified%s",
1783 !vol->mftmirr_ino ? es1 : es2,
1784 es3);
1785 goto iput_mirr_err_out;
1787 sb->s_flags |= MS_RDONLY;
1788 ntfs_error(sb, "%s. Mounting read-only%s",
1789 !vol->mftmirr_ino ? es1 : es2, es3);
1790 } else
1791 ntfs_warning(sb, "%s. Will not be able to remount "
1792 "read-write%s",
1793 !vol->mftmirr_ino ? es1 : es2, es3);
1794 /* This will prevent a read-write remount. */
1795 NVolSetErrors(vol);
1797 #endif /* NTFS_RW */
1798 /* Get mft bitmap attribute inode. */
1799 vol->mftbmp_ino = ntfs_attr_iget(vol->mft_ino, AT_BITMAP, NULL, 0);
1800 if (IS_ERR(vol->mftbmp_ino)) {
1801 ntfs_error(sb, "Failed to load $MFT/$BITMAP attribute.");
1802 goto iput_mirr_err_out;
1804 lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->runlist.lock,
1805 &mftbmp_runlist_lock_key);
1806 lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->mrec_lock,
1807 &mftbmp_mrec_lock_key);
1808 /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */
1809 if (!load_and_init_upcase(vol))
1810 goto iput_mftbmp_err_out;
1811 #ifdef NTFS_RW
1813 * Read attribute definitions table and setup @vol->attrdef and
1814 * @vol->attrdef_size.
1816 if (!load_and_init_attrdef(vol))
1817 goto iput_upcase_err_out;
1818 #endif /* NTFS_RW */
1820 * Get the cluster allocation bitmap inode and verify the size, no
1821 * need for any locking at this stage as we are already running
1822 * exclusively as we are mount in progress task.
1824 vol->lcnbmp_ino = ntfs_iget(sb, FILE_Bitmap);
1825 if (IS_ERR(vol->lcnbmp_ino) || is_bad_inode(vol->lcnbmp_ino)) {
1826 if (!IS_ERR(vol->lcnbmp_ino))
1827 iput(vol->lcnbmp_ino);
1828 goto bitmap_failed;
1830 lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->runlist.lock,
1831 &lcnbmp_runlist_lock_key);
1832 lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->mrec_lock,
1833 &lcnbmp_mrec_lock_key);
1835 NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino));
1836 if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) {
1837 iput(vol->lcnbmp_ino);
1838 bitmap_failed:
1839 ntfs_error(sb, "Failed to load $Bitmap.");
1840 goto iput_attrdef_err_out;
1843 * Get the volume inode and setup our cache of the volume flags and
1844 * version.
1846 vol->vol_ino = ntfs_iget(sb, FILE_Volume);
1847 if (IS_ERR(vol->vol_ino) || is_bad_inode(vol->vol_ino)) {
1848 if (!IS_ERR(vol->vol_ino))
1849 iput(vol->vol_ino);
1850 volume_failed:
1851 ntfs_error(sb, "Failed to load $Volume.");
1852 goto iput_lcnbmp_err_out;
1854 m = map_mft_record(NTFS_I(vol->vol_ino));
1855 if (IS_ERR(m)) {
1856 iput_volume_failed:
1857 iput(vol->vol_ino);
1858 goto volume_failed;
1860 if (!(ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m))) {
1861 ntfs_error(sb, "Failed to get attribute search context.");
1862 goto get_ctx_vol_failed;
1864 if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
1865 ctx) || ctx->attr->non_resident || ctx->attr->flags) {
1866 err_put_vol:
1867 ntfs_attr_put_search_ctx(ctx);
1868 get_ctx_vol_failed:
1869 unmap_mft_record(NTFS_I(vol->vol_ino));
1870 goto iput_volume_failed;
1872 vi = (VOLUME_INFORMATION*)((char*)ctx->attr +
1873 le16_to_cpu(ctx->attr->data.resident.value_offset));
1874 /* Some bounds checks. */
1875 if ((u8*)vi < (u8*)ctx->attr || (u8*)vi +
1876 le32_to_cpu(ctx->attr->data.resident.value_length) >
1877 (u8*)ctx->attr + le32_to_cpu(ctx->attr->length))
1878 goto err_put_vol;
1879 /* Copy the volume flags and version to the ntfs_volume structure. */
1880 vol->vol_flags = vi->flags;
1881 vol->major_ver = vi->major_ver;
1882 vol->minor_ver = vi->minor_ver;
1883 ntfs_attr_put_search_ctx(ctx);
1884 unmap_mft_record(NTFS_I(vol->vol_ino));
1885 printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver,
1886 vol->minor_ver);
1887 if (vol->major_ver < 3 && NVolSparseEnabled(vol)) {
1888 ntfs_warning(vol->sb, "Disabling sparse support due to NTFS "
1889 "volume version %i.%i (need at least version "
1890 "3.0).", vol->major_ver, vol->minor_ver);
1891 NVolClearSparseEnabled(vol);
1893 #ifdef NTFS_RW
1894 /* Make sure that no unsupported volume flags are set. */
1895 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
1896 static const char *es1a = "Volume is dirty";
1897 static const char *es1b = "Volume has been modified by chkdsk";
1898 static const char *es1c = "Volume has unsupported flags set";
1899 static const char *es2a = ". Run chkdsk and mount in Windows.";
1900 static const char *es2b = ". Mount in Windows.";
1901 const char *es1, *es2;
1903 es2 = es2a;
1904 if (vol->vol_flags & VOLUME_IS_DIRTY)
1905 es1 = es1a;
1906 else if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
1907 es1 = es1b;
1908 es2 = es2b;
1909 } else {
1910 es1 = es1c;
1911 ntfs_warning(sb, "Unsupported volume flags 0x%x "
1912 "encountered.",
1913 (unsigned)le16_to_cpu(vol->vol_flags));
1915 /* If a read-write mount, convert it to a read-only mount. */
1916 if (!(sb->s_flags & MS_RDONLY)) {
1917 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1918 ON_ERRORS_CONTINUE))) {
1919 ntfs_error(sb, "%s and neither on_errors="
1920 "continue nor on_errors="
1921 "remount-ro was specified%s",
1922 es1, es2);
1923 goto iput_vol_err_out;
1925 sb->s_flags |= MS_RDONLY;
1926 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1927 } else
1928 ntfs_warning(sb, "%s. Will not be able to remount "
1929 "read-write%s", es1, es2);
1931 * Do not set NVolErrors() because ntfs_remount() re-checks the
1932 * flags which we need to do in case any flags have changed.
1936 * Get the inode for the logfile, check it and determine if the volume
1937 * was shutdown cleanly.
1939 rp = NULL;
1940 if (!load_and_check_logfile(vol, &rp) ||
1941 !ntfs_is_logfile_clean(vol->logfile_ino, rp)) {
1942 static const char *es1a = "Failed to load $LogFile";
1943 static const char *es1b = "$LogFile is not clean";
1944 static const char *es2 = ". Mount in Windows.";
1945 const char *es1;
1947 es1 = !vol->logfile_ino ? es1a : es1b;
1948 /* If a read-write mount, convert it to a read-only mount. */
1949 if (!(sb->s_flags & MS_RDONLY)) {
1950 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1951 ON_ERRORS_CONTINUE))) {
1952 ntfs_error(sb, "%s and neither on_errors="
1953 "continue nor on_errors="
1954 "remount-ro was specified%s",
1955 es1, es2);
1956 if (vol->logfile_ino) {
1957 BUG_ON(!rp);
1958 ntfs_free(rp);
1960 goto iput_logfile_err_out;
1962 sb->s_flags |= MS_RDONLY;
1963 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1964 } else
1965 ntfs_warning(sb, "%s. Will not be able to remount "
1966 "read-write%s", es1, es2);
1967 /* This will prevent a read-write remount. */
1968 NVolSetErrors(vol);
1970 ntfs_free(rp);
1971 #endif /* NTFS_RW */
1972 /* Get the root directory inode so we can do path lookups. */
1973 vol->root_ino = ntfs_iget(sb, FILE_root);
1974 if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) {
1975 if (!IS_ERR(vol->root_ino))
1976 iput(vol->root_ino);
1977 ntfs_error(sb, "Failed to load root directory.");
1978 goto iput_logfile_err_out;
1980 #ifdef NTFS_RW
1982 * Check if Windows is suspended to disk on the target volume. If it
1983 * is hibernated, we must not write *anything* to the disk so set
1984 * NVolErrors() without setting the dirty volume flag and mount
1985 * read-only. This will prevent read-write remounting and it will also
1986 * prevent all writes.
1988 err = check_windows_hibernation_status(vol);
1989 if (unlikely(err)) {
1990 static const char *es1a = "Failed to determine if Windows is "
1991 "hibernated";
1992 static const char *es1b = "Windows is hibernated";
1993 static const char *es2 = ". Run chkdsk.";
1994 const char *es1;
1996 es1 = err < 0 ? es1a : es1b;
1997 /* If a read-write mount, convert it to a read-only mount. */
1998 if (!(sb->s_flags & MS_RDONLY)) {
1999 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2000 ON_ERRORS_CONTINUE))) {
2001 ntfs_error(sb, "%s and neither on_errors="
2002 "continue nor on_errors="
2003 "remount-ro was specified%s",
2004 es1, es2);
2005 goto iput_root_err_out;
2007 sb->s_flags |= MS_RDONLY;
2008 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2009 } else
2010 ntfs_warning(sb, "%s. Will not be able to remount "
2011 "read-write%s", es1, es2);
2012 /* This will prevent a read-write remount. */
2013 NVolSetErrors(vol);
2015 /* If (still) a read-write mount, mark the volume dirty. */
2016 if (!(sb->s_flags & MS_RDONLY) &&
2017 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
2018 static const char *es1 = "Failed to set dirty bit in volume "
2019 "information flags";
2020 static const char *es2 = ". Run chkdsk.";
2022 /* Convert to a read-only mount. */
2023 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2024 ON_ERRORS_CONTINUE))) {
2025 ntfs_error(sb, "%s and neither on_errors=continue nor "
2026 "on_errors=remount-ro was specified%s",
2027 es1, es2);
2028 goto iput_root_err_out;
2030 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2031 sb->s_flags |= MS_RDONLY;
2033 * Do not set NVolErrors() because ntfs_remount() might manage
2034 * to set the dirty flag in which case all would be well.
2037 #if 0
2038 // TODO: Enable this code once we start modifying anything that is
2039 // different between NTFS 1.2 and 3.x...
2041 * If (still) a read-write mount, set the NT4 compatibility flag on
2042 * newer NTFS version volumes.
2044 if (!(sb->s_flags & MS_RDONLY) && (vol->major_ver > 1) &&
2045 ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
2046 static const char *es1 = "Failed to set NT4 compatibility flag";
2047 static const char *es2 = ". Run chkdsk.";
2049 /* Convert to a read-only mount. */
2050 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2051 ON_ERRORS_CONTINUE))) {
2052 ntfs_error(sb, "%s and neither on_errors=continue nor "
2053 "on_errors=remount-ro was specified%s",
2054 es1, es2);
2055 goto iput_root_err_out;
2057 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2058 sb->s_flags |= MS_RDONLY;
2059 NVolSetErrors(vol);
2061 #endif
2062 /* If (still) a read-write mount, empty the logfile. */
2063 if (!(sb->s_flags & MS_RDONLY) &&
2064 !ntfs_empty_logfile(vol->logfile_ino)) {
2065 static const char *es1 = "Failed to empty $LogFile";
2066 static const char *es2 = ". Mount in Windows.";
2068 /* Convert to a read-only mount. */
2069 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2070 ON_ERRORS_CONTINUE))) {
2071 ntfs_error(sb, "%s and neither on_errors=continue nor "
2072 "on_errors=remount-ro was specified%s",
2073 es1, es2);
2074 goto iput_root_err_out;
2076 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2077 sb->s_flags |= MS_RDONLY;
2078 NVolSetErrors(vol);
2080 #endif /* NTFS_RW */
2081 /* If on NTFS versions before 3.0, we are done. */
2082 if (unlikely(vol->major_ver < 3))
2083 return true;
2084 /* NTFS 3.0+ specific initialization. */
2085 /* Get the security descriptors inode. */
2086 vol->secure_ino = ntfs_iget(sb, FILE_Secure);
2087 if (IS_ERR(vol->secure_ino) || is_bad_inode(vol->secure_ino)) {
2088 if (!IS_ERR(vol->secure_ino))
2089 iput(vol->secure_ino);
2090 ntfs_error(sb, "Failed to load $Secure.");
2091 goto iput_root_err_out;
2093 // TODO: Initialize security.
2094 /* Get the extended system files' directory inode. */
2095 vol->extend_ino = ntfs_iget(sb, FILE_Extend);
2096 if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) {
2097 if (!IS_ERR(vol->extend_ino))
2098 iput(vol->extend_ino);
2099 ntfs_error(sb, "Failed to load $Extend.");
2100 goto iput_sec_err_out;
2102 #ifdef NTFS_RW
2103 /* Find the quota file, load it if present, and set it up. */
2104 if (!load_and_init_quota(vol)) {
2105 static const char *es1 = "Failed to load $Quota";
2106 static const char *es2 = ". Run chkdsk.";
2108 /* If a read-write mount, convert it to a read-only mount. */
2109 if (!(sb->s_flags & MS_RDONLY)) {
2110 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2111 ON_ERRORS_CONTINUE))) {
2112 ntfs_error(sb, "%s and neither on_errors="
2113 "continue nor on_errors="
2114 "remount-ro was specified%s",
2115 es1, es2);
2116 goto iput_quota_err_out;
2118 sb->s_flags |= MS_RDONLY;
2119 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2120 } else
2121 ntfs_warning(sb, "%s. Will not be able to remount "
2122 "read-write%s", es1, es2);
2123 /* This will prevent a read-write remount. */
2124 NVolSetErrors(vol);
2126 /* If (still) a read-write mount, mark the quotas out of date. */
2127 if (!(sb->s_flags & MS_RDONLY) &&
2128 !ntfs_mark_quotas_out_of_date(vol)) {
2129 static const char *es1 = "Failed to mark quotas out of date";
2130 static const char *es2 = ". Run chkdsk.";
2132 /* Convert to a read-only mount. */
2133 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2134 ON_ERRORS_CONTINUE))) {
2135 ntfs_error(sb, "%s and neither on_errors=continue nor "
2136 "on_errors=remount-ro was specified%s",
2137 es1, es2);
2138 goto iput_quota_err_out;
2140 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2141 sb->s_flags |= MS_RDONLY;
2142 NVolSetErrors(vol);
2145 * Find the transaction log file ($UsnJrnl), load it if present, check
2146 * it, and set it up.
2148 if (!load_and_init_usnjrnl(vol)) {
2149 static const char *es1 = "Failed to load $UsnJrnl";
2150 static const char *es2 = ". Run chkdsk.";
2152 /* If a read-write mount, convert it to a read-only mount. */
2153 if (!(sb->s_flags & MS_RDONLY)) {
2154 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2155 ON_ERRORS_CONTINUE))) {
2156 ntfs_error(sb, "%s and neither on_errors="
2157 "continue nor on_errors="
2158 "remount-ro was specified%s",
2159 es1, es2);
2160 goto iput_usnjrnl_err_out;
2162 sb->s_flags |= MS_RDONLY;
2163 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2164 } else
2165 ntfs_warning(sb, "%s. Will not be able to remount "
2166 "read-write%s", es1, es2);
2167 /* This will prevent a read-write remount. */
2168 NVolSetErrors(vol);
2170 /* If (still) a read-write mount, stamp the transaction log. */
2171 if (!(sb->s_flags & MS_RDONLY) && !ntfs_stamp_usnjrnl(vol)) {
2172 static const char *es1 = "Failed to stamp transaction log "
2173 "($UsnJrnl)";
2174 static const char *es2 = ". Run chkdsk.";
2176 /* Convert to a read-only mount. */
2177 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2178 ON_ERRORS_CONTINUE))) {
2179 ntfs_error(sb, "%s and neither on_errors=continue nor "
2180 "on_errors=remount-ro was specified%s",
2181 es1, es2);
2182 goto iput_usnjrnl_err_out;
2184 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2185 sb->s_flags |= MS_RDONLY;
2186 NVolSetErrors(vol);
2188 #endif /* NTFS_RW */
2189 return true;
2190 #ifdef NTFS_RW
2191 iput_usnjrnl_err_out:
2192 if (vol->usnjrnl_j_ino)
2193 iput(vol->usnjrnl_j_ino);
2194 if (vol->usnjrnl_max_ino)
2195 iput(vol->usnjrnl_max_ino);
2196 if (vol->usnjrnl_ino)
2197 iput(vol->usnjrnl_ino);
2198 iput_quota_err_out:
2199 if (vol->quota_q_ino)
2200 iput(vol->quota_q_ino);
2201 if (vol->quota_ino)
2202 iput(vol->quota_ino);
2203 iput(vol->extend_ino);
2204 #endif /* NTFS_RW */
2205 iput_sec_err_out:
2206 iput(vol->secure_ino);
2207 iput_root_err_out:
2208 iput(vol->root_ino);
2209 iput_logfile_err_out:
2210 #ifdef NTFS_RW
2211 if (vol->logfile_ino)
2212 iput(vol->logfile_ino);
2213 iput_vol_err_out:
2214 #endif /* NTFS_RW */
2215 iput(vol->vol_ino);
2216 iput_lcnbmp_err_out:
2217 iput(vol->lcnbmp_ino);
2218 iput_attrdef_err_out:
2219 vol->attrdef_size = 0;
2220 if (vol->attrdef) {
2221 ntfs_free(vol->attrdef);
2222 vol->attrdef = NULL;
2224 #ifdef NTFS_RW
2225 iput_upcase_err_out:
2226 #endif /* NTFS_RW */
2227 vol->upcase_len = 0;
2228 mutex_lock(&ntfs_lock);
2229 if (vol->upcase == default_upcase) {
2230 ntfs_nr_upcase_users--;
2231 vol->upcase = NULL;
2233 mutex_unlock(&ntfs_lock);
2234 if (vol->upcase) {
2235 ntfs_free(vol->upcase);
2236 vol->upcase = NULL;
2238 iput_mftbmp_err_out:
2239 iput(vol->mftbmp_ino);
2240 iput_mirr_err_out:
2241 #ifdef NTFS_RW
2242 if (vol->mftmirr_ino)
2243 iput(vol->mftmirr_ino);
2244 #endif /* NTFS_RW */
2245 return false;
2249 * ntfs_put_super - called by the vfs to unmount a volume
2250 * @sb: vfs superblock of volume to unmount
2252 * ntfs_put_super() is called by the VFS (from fs/super.c::do_umount()) when
2253 * the volume is being unmounted (umount system call has been invoked) and it
2254 * releases all inodes and memory belonging to the NTFS specific part of the
2255 * super block.
2257 static void ntfs_put_super(struct super_block *sb)
2259 ntfs_volume *vol = NTFS_SB(sb);
2261 ntfs_debug("Entering.");
2263 lock_kernel();
2265 #ifdef NTFS_RW
2267 * Commit all inodes while they are still open in case some of them
2268 * cause others to be dirtied.
2270 ntfs_commit_inode(vol->vol_ino);
2272 /* NTFS 3.0+ specific. */
2273 if (vol->major_ver >= 3) {
2274 if (vol->usnjrnl_j_ino)
2275 ntfs_commit_inode(vol->usnjrnl_j_ino);
2276 if (vol->usnjrnl_max_ino)
2277 ntfs_commit_inode(vol->usnjrnl_max_ino);
2278 if (vol->usnjrnl_ino)
2279 ntfs_commit_inode(vol->usnjrnl_ino);
2280 if (vol->quota_q_ino)
2281 ntfs_commit_inode(vol->quota_q_ino);
2282 if (vol->quota_ino)
2283 ntfs_commit_inode(vol->quota_ino);
2284 if (vol->extend_ino)
2285 ntfs_commit_inode(vol->extend_ino);
2286 if (vol->secure_ino)
2287 ntfs_commit_inode(vol->secure_ino);
2290 ntfs_commit_inode(vol->root_ino);
2292 down_write(&vol->lcnbmp_lock);
2293 ntfs_commit_inode(vol->lcnbmp_ino);
2294 up_write(&vol->lcnbmp_lock);
2296 down_write(&vol->mftbmp_lock);
2297 ntfs_commit_inode(vol->mftbmp_ino);
2298 up_write(&vol->mftbmp_lock);
2300 if (vol->logfile_ino)
2301 ntfs_commit_inode(vol->logfile_ino);
2303 if (vol->mftmirr_ino)
2304 ntfs_commit_inode(vol->mftmirr_ino);
2305 ntfs_commit_inode(vol->mft_ino);
2308 * If a read-write mount and no volume errors have occured, mark the
2309 * volume clean. Also, re-commit all affected inodes.
2311 if (!(sb->s_flags & MS_RDONLY)) {
2312 if (!NVolErrors(vol)) {
2313 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
2314 ntfs_warning(sb, "Failed to clear dirty bit "
2315 "in volume information "
2316 "flags. Run chkdsk.");
2317 ntfs_commit_inode(vol->vol_ino);
2318 ntfs_commit_inode(vol->root_ino);
2319 if (vol->mftmirr_ino)
2320 ntfs_commit_inode(vol->mftmirr_ino);
2321 ntfs_commit_inode(vol->mft_ino);
2322 } else {
2323 ntfs_warning(sb, "Volume has errors. Leaving volume "
2324 "marked dirty. Run chkdsk.");
2327 #endif /* NTFS_RW */
2329 iput(vol->vol_ino);
2330 vol->vol_ino = NULL;
2332 /* NTFS 3.0+ specific clean up. */
2333 if (vol->major_ver >= 3) {
2334 #ifdef NTFS_RW
2335 if (vol->usnjrnl_j_ino) {
2336 iput(vol->usnjrnl_j_ino);
2337 vol->usnjrnl_j_ino = NULL;
2339 if (vol->usnjrnl_max_ino) {
2340 iput(vol->usnjrnl_max_ino);
2341 vol->usnjrnl_max_ino = NULL;
2343 if (vol->usnjrnl_ino) {
2344 iput(vol->usnjrnl_ino);
2345 vol->usnjrnl_ino = NULL;
2347 if (vol->quota_q_ino) {
2348 iput(vol->quota_q_ino);
2349 vol->quota_q_ino = NULL;
2351 if (vol->quota_ino) {
2352 iput(vol->quota_ino);
2353 vol->quota_ino = NULL;
2355 #endif /* NTFS_RW */
2356 if (vol->extend_ino) {
2357 iput(vol->extend_ino);
2358 vol->extend_ino = NULL;
2360 if (vol->secure_ino) {
2361 iput(vol->secure_ino);
2362 vol->secure_ino = NULL;
2366 iput(vol->root_ino);
2367 vol->root_ino = NULL;
2369 down_write(&vol->lcnbmp_lock);
2370 iput(vol->lcnbmp_ino);
2371 vol->lcnbmp_ino = NULL;
2372 up_write(&vol->lcnbmp_lock);
2374 down_write(&vol->mftbmp_lock);
2375 iput(vol->mftbmp_ino);
2376 vol->mftbmp_ino = NULL;
2377 up_write(&vol->mftbmp_lock);
2379 #ifdef NTFS_RW
2380 if (vol->logfile_ino) {
2381 iput(vol->logfile_ino);
2382 vol->logfile_ino = NULL;
2384 if (vol->mftmirr_ino) {
2385 /* Re-commit the mft mirror and mft just in case. */
2386 ntfs_commit_inode(vol->mftmirr_ino);
2387 ntfs_commit_inode(vol->mft_ino);
2388 iput(vol->mftmirr_ino);
2389 vol->mftmirr_ino = NULL;
2392 * We should have no dirty inodes left, due to
2393 * mft.c::ntfs_mft_writepage() cleaning all the dirty pages as
2394 * the underlying mft records are written out and cleaned.
2396 ntfs_commit_inode(vol->mft_ino);
2397 write_inode_now(vol->mft_ino, 1);
2398 #endif /* NTFS_RW */
2400 iput(vol->mft_ino);
2401 vol->mft_ino = NULL;
2403 /* Throw away the table of attribute definitions. */
2404 vol->attrdef_size = 0;
2405 if (vol->attrdef) {
2406 ntfs_free(vol->attrdef);
2407 vol->attrdef = NULL;
2409 vol->upcase_len = 0;
2411 * Destroy the global default upcase table if necessary. Also decrease
2412 * the number of upcase users if we are a user.
2414 mutex_lock(&ntfs_lock);
2415 if (vol->upcase == default_upcase) {
2416 ntfs_nr_upcase_users--;
2417 vol->upcase = NULL;
2419 if (!ntfs_nr_upcase_users && default_upcase) {
2420 ntfs_free(default_upcase);
2421 default_upcase = NULL;
2423 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
2424 free_compression_buffers();
2425 mutex_unlock(&ntfs_lock);
2426 if (vol->upcase) {
2427 ntfs_free(vol->upcase);
2428 vol->upcase = NULL;
2430 if (vol->nls_map) {
2431 unload_nls(vol->nls_map);
2432 vol->nls_map = NULL;
2434 sb->s_fs_info = NULL;
2435 kfree(vol);
2437 unlock_kernel();
2441 * get_nr_free_clusters - return the number of free clusters on a volume
2442 * @vol: ntfs volume for which to obtain free cluster count
2444 * Calculate the number of free clusters on the mounted NTFS volume @vol. We
2445 * actually calculate the number of clusters in use instead because this
2446 * allows us to not care about partial pages as these will be just zero filled
2447 * and hence not be counted as allocated clusters.
2449 * The only particularity is that clusters beyond the end of the logical ntfs
2450 * volume will be marked as allocated to prevent errors which means we have to
2451 * discount those at the end. This is important as the cluster bitmap always
2452 * has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside
2453 * the logical volume and marked in use when they are not as they do not exist.
2455 * If any pages cannot be read we assume all clusters in the erroring pages are
2456 * in use. This means we return an underestimate on errors which is better than
2457 * an overestimate.
2459 static s64 get_nr_free_clusters(ntfs_volume *vol)
2461 s64 nr_free = vol->nr_clusters;
2462 u32 *kaddr;
2463 struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
2464 struct page *page;
2465 pgoff_t index, max_index;
2467 ntfs_debug("Entering.");
2468 /* Serialize accesses to the cluster bitmap. */
2469 down_read(&vol->lcnbmp_lock);
2471 * Convert the number of bits into bytes rounded up, then convert into
2472 * multiples of PAGE_CACHE_SIZE, rounding up so that if we have one
2473 * full and one partial page max_index = 2.
2475 max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >>
2476 PAGE_CACHE_SHIFT;
2477 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2478 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.",
2479 max_index, PAGE_CACHE_SIZE / 4);
2480 for (index = 0; index < max_index; index++) {
2481 unsigned int i;
2483 * Read the page from page cache, getting it from backing store
2484 * if necessary, and increment the use count.
2486 page = read_mapping_page(mapping, index, NULL);
2487 /* Ignore pages which errored synchronously. */
2488 if (IS_ERR(page)) {
2489 ntfs_debug("read_mapping_page() error. Skipping "
2490 "page (index 0x%lx).", index);
2491 nr_free -= PAGE_CACHE_SIZE * 8;
2492 continue;
2494 kaddr = (u32*)kmap_atomic(page, KM_USER0);
2496 * For each 4 bytes, subtract the number of set bits. If this
2497 * is the last page and it is partial we don't really care as
2498 * it just means we do a little extra work but it won't affect
2499 * the result as all out of range bytes are set to zero by
2500 * ntfs_readpage().
2502 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
2503 nr_free -= (s64)hweight32(kaddr[i]);
2504 kunmap_atomic(kaddr, KM_USER0);
2505 page_cache_release(page);
2507 ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index - 1);
2509 * Fixup for eventual bits outside logical ntfs volume (see function
2510 * description above).
2512 if (vol->nr_clusters & 63)
2513 nr_free += 64 - (vol->nr_clusters & 63);
2514 up_read(&vol->lcnbmp_lock);
2515 /* If errors occured we may well have gone below zero, fix this. */
2516 if (nr_free < 0)
2517 nr_free = 0;
2518 ntfs_debug("Exiting.");
2519 return nr_free;
2523 * __get_nr_free_mft_records - return the number of free inodes on a volume
2524 * @vol: ntfs volume for which to obtain free inode count
2525 * @nr_free: number of mft records in filesystem
2526 * @max_index: maximum number of pages containing set bits
2528 * Calculate the number of free mft records (inodes) on the mounted NTFS
2529 * volume @vol. We actually calculate the number of mft records in use instead
2530 * because this allows us to not care about partial pages as these will be just
2531 * zero filled and hence not be counted as allocated mft record.
2533 * If any pages cannot be read we assume all mft records in the erroring pages
2534 * are in use. This means we return an underestimate on errors which is better
2535 * than an overestimate.
2537 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
2539 static unsigned long __get_nr_free_mft_records(ntfs_volume *vol,
2540 s64 nr_free, const pgoff_t max_index)
2542 u32 *kaddr;
2543 struct address_space *mapping = vol->mftbmp_ino->i_mapping;
2544 struct page *page;
2545 pgoff_t index;
2547 ntfs_debug("Entering.");
2548 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2549 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
2550 "0x%lx.", max_index, PAGE_CACHE_SIZE / 4);
2551 for (index = 0; index < max_index; index++) {
2552 unsigned int i;
2554 * Read the page from page cache, getting it from backing store
2555 * if necessary, and increment the use count.
2557 page = read_mapping_page(mapping, index, NULL);
2558 /* Ignore pages which errored synchronously. */
2559 if (IS_ERR(page)) {
2560 ntfs_debug("read_mapping_page() error. Skipping "
2561 "page (index 0x%lx).", index);
2562 nr_free -= PAGE_CACHE_SIZE * 8;
2563 continue;
2565 kaddr = (u32*)kmap_atomic(page, KM_USER0);
2567 * For each 4 bytes, subtract the number of set bits. If this
2568 * is the last page and it is partial we don't really care as
2569 * it just means we do a little extra work but it won't affect
2570 * the result as all out of range bytes are set to zero by
2571 * ntfs_readpage().
2573 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
2574 nr_free -= (s64)hweight32(kaddr[i]);
2575 kunmap_atomic(kaddr, KM_USER0);
2576 page_cache_release(page);
2578 ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.",
2579 index - 1);
2580 /* If errors occured we may well have gone below zero, fix this. */
2581 if (nr_free < 0)
2582 nr_free = 0;
2583 ntfs_debug("Exiting.");
2584 return nr_free;
2588 * ntfs_statfs - return information about mounted NTFS volume
2589 * @dentry: dentry from mounted volume
2590 * @sfs: statfs structure in which to return the information
2592 * Return information about the mounted NTFS volume @dentry in the statfs structure
2593 * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is
2594 * called). We interpret the values to be correct of the moment in time at
2595 * which we are called. Most values are variable otherwise and this isn't just
2596 * the free values but the totals as well. For example we can increase the
2597 * total number of file nodes if we run out and we can keep doing this until
2598 * there is no more space on the volume left at all.
2600 * Called from vfs_statfs which is used to handle the statfs, fstatfs, and
2601 * ustat system calls.
2603 * Return 0 on success or -errno on error.
2605 static int ntfs_statfs(struct dentry *dentry, struct kstatfs *sfs)
2607 struct super_block *sb = dentry->d_sb;
2608 s64 size;
2609 ntfs_volume *vol = NTFS_SB(sb);
2610 ntfs_inode *mft_ni = NTFS_I(vol->mft_ino);
2611 pgoff_t max_index;
2612 unsigned long flags;
2614 ntfs_debug("Entering.");
2615 /* Type of filesystem. */
2616 sfs->f_type = NTFS_SB_MAGIC;
2617 /* Optimal transfer block size. */
2618 sfs->f_bsize = PAGE_CACHE_SIZE;
2620 * Total data blocks in filesystem in units of f_bsize and since
2621 * inodes are also stored in data blocs ($MFT is a file) this is just
2622 * the total clusters.
2624 sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >>
2625 PAGE_CACHE_SHIFT;
2626 /* Free data blocks in filesystem in units of f_bsize. */
2627 size = get_nr_free_clusters(vol) << vol->cluster_size_bits >>
2628 PAGE_CACHE_SHIFT;
2629 if (size < 0LL)
2630 size = 0LL;
2631 /* Free blocks avail to non-superuser, same as above on NTFS. */
2632 sfs->f_bavail = sfs->f_bfree = size;
2633 /* Serialize accesses to the inode bitmap. */
2634 down_read(&vol->mftbmp_lock);
2635 read_lock_irqsave(&mft_ni->size_lock, flags);
2636 size = i_size_read(vol->mft_ino) >> vol->mft_record_size_bits;
2638 * Convert the maximum number of set bits into bytes rounded up, then
2639 * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
2640 * have one full and one partial page max_index = 2.
2642 max_index = ((((mft_ni->initialized_size >> vol->mft_record_size_bits)
2643 + 7) >> 3) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2644 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2645 /* Number of inodes in filesystem (at this point in time). */
2646 sfs->f_files = size;
2647 /* Free inodes in fs (based on current total count). */
2648 sfs->f_ffree = __get_nr_free_mft_records(vol, size, max_index);
2649 up_read(&vol->mftbmp_lock);
2651 * File system id. This is extremely *nix flavour dependent and even
2652 * within Linux itself all fs do their own thing. I interpret this to
2653 * mean a unique id associated with the mounted fs and not the id
2654 * associated with the filesystem driver, the latter is already given
2655 * by the filesystem type in sfs->f_type. Thus we use the 64-bit
2656 * volume serial number splitting it into two 32-bit parts. We enter
2657 * the least significant 32-bits in f_fsid[0] and the most significant
2658 * 32-bits in f_fsid[1].
2660 sfs->f_fsid.val[0] = vol->serial_no & 0xffffffff;
2661 sfs->f_fsid.val[1] = (vol->serial_no >> 32) & 0xffffffff;
2662 /* Maximum length of filenames. */
2663 sfs->f_namelen = NTFS_MAX_NAME_LEN;
2664 return 0;
2668 * The complete super operations.
2670 static const struct super_operations ntfs_sops = {
2671 .alloc_inode = ntfs_alloc_big_inode, /* VFS: Allocate new inode. */
2672 .destroy_inode = ntfs_destroy_big_inode, /* VFS: Deallocate inode. */
2673 #ifdef NTFS_RW
2674 //.dirty_inode = NULL, /* VFS: Called from
2675 // __mark_inode_dirty(). */
2676 .write_inode = ntfs_write_inode, /* VFS: Write dirty inode to
2677 disk. */
2678 //.drop_inode = NULL, /* VFS: Called just after the
2679 // inode reference count has
2680 // been decreased to zero.
2681 // NOTE: The inode lock is
2682 // held. See fs/inode.c::
2683 // generic_drop_inode(). */
2684 //.delete_inode = NULL, /* VFS: Delete inode from disk.
2685 // Called when i_count becomes
2686 // 0 and i_nlink is also 0. */
2687 //.write_super = NULL, /* Flush dirty super block to
2688 // disk. */
2689 //.sync_fs = NULL, /* ? */
2690 //.write_super_lockfs = NULL, /* ? */
2691 //.unlockfs = NULL, /* ? */
2692 #endif /* NTFS_RW */
2693 .put_super = ntfs_put_super, /* Syscall: umount. */
2694 .statfs = ntfs_statfs, /* Syscall: statfs */
2695 .remount_fs = ntfs_remount, /* Syscall: mount -o remount. */
2696 .clear_inode = ntfs_clear_big_inode, /* VFS: Called when an inode is
2697 removed from memory. */
2698 //.umount_begin = NULL, /* Forced umount. */
2699 .show_options = ntfs_show_options, /* Show mount options in
2700 proc. */
2704 * ntfs_fill_super - mount an ntfs filesystem
2705 * @sb: super block of ntfs filesystem to mount
2706 * @opt: string containing the mount options
2707 * @silent: silence error output
2709 * ntfs_fill_super() is called by the VFS to mount the device described by @sb
2710 * with the mount otions in @data with the NTFS filesystem.
2712 * If @silent is true, remain silent even if errors are detected. This is used
2713 * during bootup, when the kernel tries to mount the root filesystem with all
2714 * registered filesystems one after the other until one succeeds. This implies
2715 * that all filesystems except the correct one will quite correctly and
2716 * expectedly return an error, but nobody wants to see error messages when in
2717 * fact this is what is supposed to happen.
2719 * NOTE: @sb->s_flags contains the mount options flags.
2721 static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2723 ntfs_volume *vol;
2724 struct buffer_head *bh;
2725 struct inode *tmp_ino;
2726 int blocksize, result;
2729 * We do a pretty difficult piece of bootstrap by reading the
2730 * MFT (and other metadata) from disk into memory. We'll only
2731 * release this metadata during umount, so the locking patterns
2732 * observed during bootstrap do not count. So turn off the
2733 * observation of locking patterns (strictly for this context
2734 * only) while mounting NTFS. [The validator is still active
2735 * otherwise, even for this context: it will for example record
2736 * lock class registrations.]
2738 lockdep_off();
2739 ntfs_debug("Entering.");
2740 #ifndef NTFS_RW
2741 sb->s_flags |= MS_RDONLY;
2742 #endif /* ! NTFS_RW */
2743 /* Allocate a new ntfs_volume and place it in sb->s_fs_info. */
2744 sb->s_fs_info = kmalloc(sizeof(ntfs_volume), GFP_NOFS);
2745 vol = NTFS_SB(sb);
2746 if (!vol) {
2747 if (!silent)
2748 ntfs_error(sb, "Allocation of NTFS volume structure "
2749 "failed. Aborting mount...");
2750 lockdep_on();
2751 return -ENOMEM;
2753 /* Initialize ntfs_volume structure. */
2754 *vol = (ntfs_volume) {
2755 .sb = sb,
2757 * Default is group and other don't have any access to files or
2758 * directories while owner has full access. Further, files by
2759 * default are not executable but directories are of course
2760 * browseable.
2762 .fmask = 0177,
2763 .dmask = 0077,
2765 init_rwsem(&vol->mftbmp_lock);
2766 init_rwsem(&vol->lcnbmp_lock);
2768 unlock_kernel();
2770 /* By default, enable sparse support. */
2771 NVolSetSparseEnabled(vol);
2773 /* Important to get the mount options dealt with now. */
2774 if (!parse_options(vol, (char*)opt))
2775 goto err_out_now;
2777 /* We support sector sizes up to the PAGE_CACHE_SIZE. */
2778 if (bdev_logical_block_size(sb->s_bdev) > PAGE_CACHE_SIZE) {
2779 if (!silent)
2780 ntfs_error(sb, "Device has unsupported sector size "
2781 "(%i). The maximum supported sector "
2782 "size on this architecture is %lu "
2783 "bytes.",
2784 bdev_logical_block_size(sb->s_bdev),
2785 PAGE_CACHE_SIZE);
2786 goto err_out_now;
2789 * Setup the device access block size to NTFS_BLOCK_SIZE or the hard
2790 * sector size, whichever is bigger.
2792 blocksize = sb_min_blocksize(sb, NTFS_BLOCK_SIZE);
2793 if (blocksize < NTFS_BLOCK_SIZE) {
2794 if (!silent)
2795 ntfs_error(sb, "Unable to set device block size.");
2796 goto err_out_now;
2798 BUG_ON(blocksize != sb->s_blocksize);
2799 ntfs_debug("Set device block size to %i bytes (block size bits %i).",
2800 blocksize, sb->s_blocksize_bits);
2801 /* Determine the size of the device in units of block_size bytes. */
2802 if (!i_size_read(sb->s_bdev->bd_inode)) {
2803 if (!silent)
2804 ntfs_error(sb, "Unable to determine device size.");
2805 goto err_out_now;
2807 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2808 sb->s_blocksize_bits;
2809 /* Read the boot sector and return unlocked buffer head to it. */
2810 if (!(bh = read_ntfs_boot_sector(sb, silent))) {
2811 if (!silent)
2812 ntfs_error(sb, "Not an NTFS volume.");
2813 goto err_out_now;
2816 * Extract the data from the boot sector and setup the ntfs volume
2817 * using it.
2819 result = parse_ntfs_boot_sector(vol, (NTFS_BOOT_SECTOR*)bh->b_data);
2820 brelse(bh);
2821 if (!result) {
2822 if (!silent)
2823 ntfs_error(sb, "Unsupported NTFS filesystem.");
2824 goto err_out_now;
2827 * If the boot sector indicates a sector size bigger than the current
2828 * device block size, switch the device block size to the sector size.
2829 * TODO: It may be possible to support this case even when the set
2830 * below fails, we would just be breaking up the i/o for each sector
2831 * into multiple blocks for i/o purposes but otherwise it should just
2832 * work. However it is safer to leave disabled until someone hits this
2833 * error message and then we can get them to try it without the setting
2834 * so we know for sure that it works.
2836 if (vol->sector_size > blocksize) {
2837 blocksize = sb_set_blocksize(sb, vol->sector_size);
2838 if (blocksize != vol->sector_size) {
2839 if (!silent)
2840 ntfs_error(sb, "Unable to set device block "
2841 "size to sector size (%i).",
2842 vol->sector_size);
2843 goto err_out_now;
2845 BUG_ON(blocksize != sb->s_blocksize);
2846 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2847 sb->s_blocksize_bits;
2848 ntfs_debug("Changed device block size to %i bytes (block size "
2849 "bits %i) to match volume sector size.",
2850 blocksize, sb->s_blocksize_bits);
2852 /* Initialize the cluster and mft allocators. */
2853 ntfs_setup_allocators(vol);
2854 /* Setup remaining fields in the super block. */
2855 sb->s_magic = NTFS_SB_MAGIC;
2857 * Ntfs allows 63 bits for the file size, i.e. correct would be:
2858 * sb->s_maxbytes = ~0ULL >> 1;
2859 * But the kernel uses a long as the page cache page index which on
2860 * 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel
2861 * defined to the maximum the page cache page index can cope with
2862 * without overflowing the index or to 2^63 - 1, whichever is smaller.
2864 sb->s_maxbytes = MAX_LFS_FILESIZE;
2865 /* Ntfs measures time in 100ns intervals. */
2866 sb->s_time_gran = 100;
2868 * Now load the metadata required for the page cache and our address
2869 * space operations to function. We do this by setting up a specialised
2870 * read_inode method and then just calling the normal iget() to obtain
2871 * the inode for $MFT which is sufficient to allow our normal inode
2872 * operations and associated address space operations to function.
2874 sb->s_op = &ntfs_sops;
2875 tmp_ino = new_inode(sb);
2876 if (!tmp_ino) {
2877 if (!silent)
2878 ntfs_error(sb, "Failed to load essential metadata.");
2879 goto err_out_now;
2881 tmp_ino->i_ino = FILE_MFT;
2882 insert_inode_hash(tmp_ino);
2883 if (ntfs_read_inode_mount(tmp_ino) < 0) {
2884 if (!silent)
2885 ntfs_error(sb, "Failed to load essential metadata.");
2886 goto iput_tmp_ino_err_out_now;
2888 mutex_lock(&ntfs_lock);
2890 * The current mount is a compression user if the cluster size is
2891 * less than or equal 4kiB.
2893 if (vol->cluster_size <= 4096 && !ntfs_nr_compression_users++) {
2894 result = allocate_compression_buffers();
2895 if (result) {
2896 ntfs_error(NULL, "Failed to allocate buffers "
2897 "for compression engine.");
2898 ntfs_nr_compression_users--;
2899 mutex_unlock(&ntfs_lock);
2900 goto iput_tmp_ino_err_out_now;
2904 * Generate the global default upcase table if necessary. Also
2905 * temporarily increment the number of upcase users to avoid race
2906 * conditions with concurrent (u)mounts.
2908 if (!default_upcase)
2909 default_upcase = generate_default_upcase();
2910 ntfs_nr_upcase_users++;
2911 mutex_unlock(&ntfs_lock);
2913 * From now on, ignore @silent parameter. If we fail below this line,
2914 * it will be due to a corrupt fs or a system error, so we report it.
2917 * Open the system files with normal access functions and complete
2918 * setting up the ntfs super block.
2920 if (!load_system_files(vol)) {
2921 ntfs_error(sb, "Failed to load system files.");
2922 goto unl_upcase_iput_tmp_ino_err_out_now;
2924 if ((sb->s_root = d_alloc_root(vol->root_ino))) {
2925 /* We increment i_count simulating an ntfs_iget(). */
2926 atomic_inc(&vol->root_ino->i_count);
2927 ntfs_debug("Exiting, status successful.");
2928 /* Release the default upcase if it has no users. */
2929 mutex_lock(&ntfs_lock);
2930 if (!--ntfs_nr_upcase_users && default_upcase) {
2931 ntfs_free(default_upcase);
2932 default_upcase = NULL;
2934 mutex_unlock(&ntfs_lock);
2935 sb->s_export_op = &ntfs_export_ops;
2936 lock_kernel();
2937 lockdep_on();
2938 return 0;
2940 ntfs_error(sb, "Failed to allocate root directory.");
2941 /* Clean up after the successful load_system_files() call from above. */
2942 // TODO: Use ntfs_put_super() instead of repeating all this code...
2943 // FIXME: Should mark the volume clean as the error is most likely
2944 // -ENOMEM.
2945 iput(vol->vol_ino);
2946 vol->vol_ino = NULL;
2947 /* NTFS 3.0+ specific clean up. */
2948 if (vol->major_ver >= 3) {
2949 #ifdef NTFS_RW
2950 if (vol->usnjrnl_j_ino) {
2951 iput(vol->usnjrnl_j_ino);
2952 vol->usnjrnl_j_ino = NULL;
2954 if (vol->usnjrnl_max_ino) {
2955 iput(vol->usnjrnl_max_ino);
2956 vol->usnjrnl_max_ino = NULL;
2958 if (vol->usnjrnl_ino) {
2959 iput(vol->usnjrnl_ino);
2960 vol->usnjrnl_ino = NULL;
2962 if (vol->quota_q_ino) {
2963 iput(vol->quota_q_ino);
2964 vol->quota_q_ino = NULL;
2966 if (vol->quota_ino) {
2967 iput(vol->quota_ino);
2968 vol->quota_ino = NULL;
2970 #endif /* NTFS_RW */
2971 if (vol->extend_ino) {
2972 iput(vol->extend_ino);
2973 vol->extend_ino = NULL;
2975 if (vol->secure_ino) {
2976 iput(vol->secure_ino);
2977 vol->secure_ino = NULL;
2980 iput(vol->root_ino);
2981 vol->root_ino = NULL;
2982 iput(vol->lcnbmp_ino);
2983 vol->lcnbmp_ino = NULL;
2984 iput(vol->mftbmp_ino);
2985 vol->mftbmp_ino = NULL;
2986 #ifdef NTFS_RW
2987 if (vol->logfile_ino) {
2988 iput(vol->logfile_ino);
2989 vol->logfile_ino = NULL;
2991 if (vol->mftmirr_ino) {
2992 iput(vol->mftmirr_ino);
2993 vol->mftmirr_ino = NULL;
2995 #endif /* NTFS_RW */
2996 /* Throw away the table of attribute definitions. */
2997 vol->attrdef_size = 0;
2998 if (vol->attrdef) {
2999 ntfs_free(vol->attrdef);
3000 vol->attrdef = NULL;
3002 vol->upcase_len = 0;
3003 mutex_lock(&ntfs_lock);
3004 if (vol->upcase == default_upcase) {
3005 ntfs_nr_upcase_users--;
3006 vol->upcase = NULL;
3008 mutex_unlock(&ntfs_lock);
3009 if (vol->upcase) {
3010 ntfs_free(vol->upcase);
3011 vol->upcase = NULL;
3013 if (vol->nls_map) {
3014 unload_nls(vol->nls_map);
3015 vol->nls_map = NULL;
3017 /* Error exit code path. */
3018 unl_upcase_iput_tmp_ino_err_out_now:
3020 * Decrease the number of upcase users and destroy the global default
3021 * upcase table if necessary.
3023 mutex_lock(&ntfs_lock);
3024 if (!--ntfs_nr_upcase_users && default_upcase) {
3025 ntfs_free(default_upcase);
3026 default_upcase = NULL;
3028 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
3029 free_compression_buffers();
3030 mutex_unlock(&ntfs_lock);
3031 iput_tmp_ino_err_out_now:
3032 iput(tmp_ino);
3033 if (vol->mft_ino && vol->mft_ino != tmp_ino)
3034 iput(vol->mft_ino);
3035 vol->mft_ino = NULL;
3037 * This is needed to get ntfs_clear_extent_inode() called for each
3038 * inode we have ever called ntfs_iget()/iput() on, otherwise we A)
3039 * leak resources and B) a subsequent mount fails automatically due to
3040 * ntfs_iget() never calling down into our ntfs_read_locked_inode()
3041 * method again... FIXME: Do we need to do this twice now because of
3042 * attribute inodes? I think not, so leave as is for now... (AIA)
3044 if (invalidate_inodes(sb)) {
3045 ntfs_error(sb, "Busy inodes left. This is most likely a NTFS "
3046 "driver bug.");
3047 /* Copied from fs/super.c. I just love this message. (-; */
3048 printk("NTFS: Busy inodes after umount. Self-destruct in 5 "
3049 "seconds. Have a nice day...\n");
3051 /* Errors at this stage are irrelevant. */
3052 err_out_now:
3053 lock_kernel();
3054 sb->s_fs_info = NULL;
3055 kfree(vol);
3056 ntfs_debug("Failed, returning -EINVAL.");
3057 lockdep_on();
3058 return -EINVAL;
3062 * This is a slab cache to optimize allocations and deallocations of Unicode
3063 * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN
3064 * (255) Unicode characters + a terminating NULL Unicode character.
3066 struct kmem_cache *ntfs_name_cache;
3068 /* Slab caches for efficient allocation/deallocation of inodes. */
3069 struct kmem_cache *ntfs_inode_cache;
3070 struct kmem_cache *ntfs_big_inode_cache;
3072 /* Init once constructor for the inode slab cache. */
3073 static void ntfs_big_inode_init_once(void *foo)
3075 ntfs_inode *ni = (ntfs_inode *)foo;
3077 inode_init_once(VFS_I(ni));
3081 * Slab caches to optimize allocations and deallocations of attribute search
3082 * contexts and index contexts, respectively.
3084 struct kmem_cache *ntfs_attr_ctx_cache;
3085 struct kmem_cache *ntfs_index_ctx_cache;
3087 /* Driver wide mutex. */
3088 DEFINE_MUTEX(ntfs_lock);
3090 static int ntfs_get_sb(struct file_system_type *fs_type,
3091 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3093 return get_sb_bdev(fs_type, flags, dev_name, data, ntfs_fill_super,
3094 mnt);
3097 static struct file_system_type ntfs_fs_type = {
3098 .owner = THIS_MODULE,
3099 .name = "ntfs",
3100 .get_sb = ntfs_get_sb,
3101 .kill_sb = kill_block_super,
3102 .fs_flags = FS_REQUIRES_DEV,
3105 /* Stable names for the slab caches. */
3106 static const char ntfs_index_ctx_cache_name[] = "ntfs_index_ctx_cache";
3107 static const char ntfs_attr_ctx_cache_name[] = "ntfs_attr_ctx_cache";
3108 static const char ntfs_name_cache_name[] = "ntfs_name_cache";
3109 static const char ntfs_inode_cache_name[] = "ntfs_inode_cache";
3110 static const char ntfs_big_inode_cache_name[] = "ntfs_big_inode_cache";
3112 static int __init init_ntfs_fs(void)
3114 int err = 0;
3116 /* This may be ugly but it results in pretty output so who cares. (-8 */
3117 printk(KERN_INFO "NTFS driver " NTFS_VERSION " [Flags: R/"
3118 #ifdef NTFS_RW
3120 #else
3122 #endif
3123 #ifdef DEBUG
3124 " DEBUG"
3125 #endif
3126 #ifdef MODULE
3127 " MODULE"
3128 #endif
3129 "].\n");
3131 ntfs_debug("Debug messages are enabled.");
3133 ntfs_index_ctx_cache = kmem_cache_create(ntfs_index_ctx_cache_name,
3134 sizeof(ntfs_index_context), 0 /* offset */,
3135 SLAB_HWCACHE_ALIGN, NULL /* ctor */);
3136 if (!ntfs_index_ctx_cache) {
3137 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3138 ntfs_index_ctx_cache_name);
3139 goto ictx_err_out;
3141 ntfs_attr_ctx_cache = kmem_cache_create(ntfs_attr_ctx_cache_name,
3142 sizeof(ntfs_attr_search_ctx), 0 /* offset */,
3143 SLAB_HWCACHE_ALIGN, NULL /* ctor */);
3144 if (!ntfs_attr_ctx_cache) {
3145 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3146 ntfs_attr_ctx_cache_name);
3147 goto actx_err_out;
3150 ntfs_name_cache = kmem_cache_create(ntfs_name_cache_name,
3151 (NTFS_MAX_NAME_LEN+1) * sizeof(ntfschar), 0,
3152 SLAB_HWCACHE_ALIGN, NULL);
3153 if (!ntfs_name_cache) {
3154 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3155 ntfs_name_cache_name);
3156 goto name_err_out;
3159 ntfs_inode_cache = kmem_cache_create(ntfs_inode_cache_name,
3160 sizeof(ntfs_inode), 0,
3161 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL);
3162 if (!ntfs_inode_cache) {
3163 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3164 ntfs_inode_cache_name);
3165 goto inode_err_out;
3168 ntfs_big_inode_cache = kmem_cache_create(ntfs_big_inode_cache_name,
3169 sizeof(big_ntfs_inode), 0,
3170 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD,
3171 ntfs_big_inode_init_once);
3172 if (!ntfs_big_inode_cache) {
3173 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3174 ntfs_big_inode_cache_name);
3175 goto big_inode_err_out;
3178 /* Register the ntfs sysctls. */
3179 err = ntfs_sysctl(1);
3180 if (err) {
3181 printk(KERN_CRIT "NTFS: Failed to register NTFS sysctls!\n");
3182 goto sysctl_err_out;
3185 err = register_filesystem(&ntfs_fs_type);
3186 if (!err) {
3187 ntfs_debug("NTFS driver registered successfully.");
3188 return 0; /* Success! */
3190 printk(KERN_CRIT "NTFS: Failed to register NTFS filesystem driver!\n");
3192 sysctl_err_out:
3193 kmem_cache_destroy(ntfs_big_inode_cache);
3194 big_inode_err_out:
3195 kmem_cache_destroy(ntfs_inode_cache);
3196 inode_err_out:
3197 kmem_cache_destroy(ntfs_name_cache);
3198 name_err_out:
3199 kmem_cache_destroy(ntfs_attr_ctx_cache);
3200 actx_err_out:
3201 kmem_cache_destroy(ntfs_index_ctx_cache);
3202 ictx_err_out:
3203 if (!err) {
3204 printk(KERN_CRIT "NTFS: Aborting NTFS filesystem driver "
3205 "registration...\n");
3206 err = -ENOMEM;
3208 return err;
3211 static void __exit exit_ntfs_fs(void)
3213 ntfs_debug("Unregistering NTFS driver.");
3215 unregister_filesystem(&ntfs_fs_type);
3216 kmem_cache_destroy(ntfs_big_inode_cache);
3217 kmem_cache_destroy(ntfs_inode_cache);
3218 kmem_cache_destroy(ntfs_name_cache);
3219 kmem_cache_destroy(ntfs_attr_ctx_cache);
3220 kmem_cache_destroy(ntfs_index_ctx_cache);
3221 /* Unregister the ntfs sysctls. */
3222 ntfs_sysctl(0);
3225 MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>");
3226 MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2007 Anton Altaparmakov");
3227 MODULE_VERSION(NTFS_VERSION);
3228 MODULE_LICENSE("GPL");
3229 #ifdef DEBUG
3230 module_param(debug_msgs, bool, 0);
3231 MODULE_PARM_DESC(debug_msgs, "Enable debug messages.");
3232 #endif
3234 module_init(init_ntfs_fs)
3235 module_exit(exit_ntfs_fs)