NTFS: Support more clean journal ($LogFile) states.
[linux-2.6/cjktty.git] / fs / ntfs / super.c
blobbf8569d503a6450847ef4f703862b3abbf5e5a10
1 /*
2 * super.c - NTFS kernel super block handling. Part of the Linux-NTFS project.
4 * Copyright (c) 2001-2005 Anton Altaparmakov
5 * Copyright (c) 2001,2002 Richard Russon
7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program/include file is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program (in the main directory of the Linux-NTFS
19 * distribution in the file COPYING); if not, write to the Free Software
20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/stddef.h>
24 #include <linux/init.h>
25 #include <linux/string.h>
26 #include <linux/spinlock.h>
27 #include <linux/blkdev.h> /* For bdev_hardsect_size(). */
28 #include <linux/backing-dev.h>
29 #include <linux/buffer_head.h>
30 #include <linux/vfs.h>
31 #include <linux/moduleparam.h>
32 #include <linux/smp_lock.h>
34 #include "sysctl.h"
35 #include "logfile.h"
36 #include "quota.h"
37 #include "usnjrnl.h"
38 #include "dir.h"
39 #include "debug.h"
40 #include "index.h"
41 #include "aops.h"
42 #include "layout.h"
43 #include "malloc.h"
44 #include "ntfs.h"
46 /* Number of mounted filesystems which have compression enabled. */
47 static unsigned long ntfs_nr_compression_users;
49 /* A global default upcase table and a corresponding reference count. */
50 static ntfschar *default_upcase = NULL;
51 static unsigned long ntfs_nr_upcase_users = 0;
53 /* Error constants/strings used in inode.c::ntfs_show_options(). */
54 typedef enum {
55 /* One of these must be present, default is ON_ERRORS_CONTINUE. */
56 ON_ERRORS_PANIC = 0x01,
57 ON_ERRORS_REMOUNT_RO = 0x02,
58 ON_ERRORS_CONTINUE = 0x04,
59 /* Optional, can be combined with any of the above. */
60 ON_ERRORS_RECOVER = 0x10,
61 } ON_ERRORS_ACTIONS;
63 const option_t on_errors_arr[] = {
64 { ON_ERRORS_PANIC, "panic" },
65 { ON_ERRORS_REMOUNT_RO, "remount-ro", },
66 { ON_ERRORS_CONTINUE, "continue", },
67 { ON_ERRORS_RECOVER, "recover" },
68 { 0, NULL }
71 /**
72 * simple_getbool -
74 * Copied from old ntfs driver (which copied from vfat driver).
76 static int simple_getbool(char *s, BOOL *setval)
78 if (s) {
79 if (!strcmp(s, "1") || !strcmp(s, "yes") || !strcmp(s, "true"))
80 *setval = TRUE;
81 else if (!strcmp(s, "0") || !strcmp(s, "no") ||
82 !strcmp(s, "false"))
83 *setval = FALSE;
84 else
85 return 0;
86 } else
87 *setval = TRUE;
88 return 1;
91 /**
92 * parse_options - parse the (re)mount options
93 * @vol: ntfs volume
94 * @opt: string containing the (re)mount options
96 * Parse the recognized options in @opt for the ntfs volume described by @vol.
98 static BOOL parse_options(ntfs_volume *vol, char *opt)
100 char *p, *v, *ov;
101 static char *utf8 = "utf8";
102 int errors = 0, sloppy = 0;
103 uid_t uid = (uid_t)-1;
104 gid_t gid = (gid_t)-1;
105 mode_t fmask = (mode_t)-1, dmask = (mode_t)-1;
106 int mft_zone_multiplier = -1, on_errors = -1;
107 int show_sys_files = -1, case_sensitive = -1, disable_sparse = -1;
108 struct nls_table *nls_map = NULL, *old_nls;
110 /* I am lazy... (-8 */
111 #define NTFS_GETOPT_WITH_DEFAULT(option, variable, default_value) \
112 if (!strcmp(p, option)) { \
113 if (!v || !*v) \
114 variable = default_value; \
115 else { \
116 variable = simple_strtoul(ov = v, &v, 0); \
117 if (*v) \
118 goto needs_val; \
121 #define NTFS_GETOPT(option, variable) \
122 if (!strcmp(p, option)) { \
123 if (!v || !*v) \
124 goto needs_arg; \
125 variable = simple_strtoul(ov = v, &v, 0); \
126 if (*v) \
127 goto needs_val; \
129 #define NTFS_GETOPT_BOOL(option, variable) \
130 if (!strcmp(p, option)) { \
131 BOOL val; \
132 if (!simple_getbool(v, &val)) \
133 goto needs_bool; \
134 variable = val; \
136 #define NTFS_GETOPT_OPTIONS_ARRAY(option, variable, opt_array) \
137 if (!strcmp(p, option)) { \
138 int _i; \
139 if (!v || !*v) \
140 goto needs_arg; \
141 ov = v; \
142 if (variable == -1) \
143 variable = 0; \
144 for (_i = 0; opt_array[_i].str && *opt_array[_i].str; _i++) \
145 if (!strcmp(opt_array[_i].str, v)) { \
146 variable |= opt_array[_i].val; \
147 break; \
149 if (!opt_array[_i].str || !*opt_array[_i].str) \
150 goto needs_val; \
152 if (!opt || !*opt)
153 goto no_mount_options;
154 ntfs_debug("Entering with mount options string: %s", opt);
155 while ((p = strsep(&opt, ","))) {
156 if ((v = strchr(p, '=')))
157 *v++ = 0;
158 NTFS_GETOPT("uid", uid)
159 else NTFS_GETOPT("gid", gid)
160 else NTFS_GETOPT("umask", fmask = dmask)
161 else NTFS_GETOPT("fmask", fmask)
162 else NTFS_GETOPT("dmask", dmask)
163 else NTFS_GETOPT("mft_zone_multiplier", mft_zone_multiplier)
164 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, TRUE)
165 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files)
166 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive)
167 else NTFS_GETOPT_BOOL("disable_sparse", disable_sparse)
168 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors,
169 on_errors_arr)
170 else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes"))
171 ntfs_warning(vol->sb, "Ignoring obsolete option %s.",
173 else if (!strcmp(p, "nls") || !strcmp(p, "iocharset")) {
174 if (!strcmp(p, "iocharset"))
175 ntfs_warning(vol->sb, "Option iocharset is "
176 "deprecated. Please use "
177 "option nls=<charsetname> in "
178 "the future.");
179 if (!v || !*v)
180 goto needs_arg;
181 use_utf8:
182 old_nls = nls_map;
183 nls_map = load_nls(v);
184 if (!nls_map) {
185 if (!old_nls) {
186 ntfs_error(vol->sb, "NLS character set "
187 "%s not found.", v);
188 return FALSE;
190 ntfs_error(vol->sb, "NLS character set %s not "
191 "found. Using previous one %s.",
192 v, old_nls->charset);
193 nls_map = old_nls;
194 } else /* nls_map */ {
195 if (old_nls)
196 unload_nls(old_nls);
198 } else if (!strcmp(p, "utf8")) {
199 BOOL val = FALSE;
200 ntfs_warning(vol->sb, "Option utf8 is no longer "
201 "supported, using option nls=utf8. Please "
202 "use option nls=utf8 in the future and "
203 "make sure utf8 is compiled either as a "
204 "module or into the kernel.");
205 if (!v || !*v)
206 val = TRUE;
207 else if (!simple_getbool(v, &val))
208 goto needs_bool;
209 if (val) {
210 v = utf8;
211 goto use_utf8;
213 } else {
214 ntfs_error(vol->sb, "Unrecognized mount option %s.", p);
215 if (errors < INT_MAX)
216 errors++;
218 #undef NTFS_GETOPT_OPTIONS_ARRAY
219 #undef NTFS_GETOPT_BOOL
220 #undef NTFS_GETOPT
221 #undef NTFS_GETOPT_WITH_DEFAULT
223 no_mount_options:
224 if (errors && !sloppy)
225 return FALSE;
226 if (sloppy)
227 ntfs_warning(vol->sb, "Sloppy option given. Ignoring "
228 "unrecognized mount option(s) and continuing.");
229 /* Keep this first! */
230 if (on_errors != -1) {
231 if (!on_errors) {
232 ntfs_error(vol->sb, "Invalid errors option argument "
233 "or bug in options parser.");
234 return FALSE;
237 if (nls_map) {
238 if (vol->nls_map && vol->nls_map != nls_map) {
239 ntfs_error(vol->sb, "Cannot change NLS character set "
240 "on remount.");
241 return FALSE;
242 } /* else (!vol->nls_map) */
243 ntfs_debug("Using NLS character set %s.", nls_map->charset);
244 vol->nls_map = nls_map;
245 } else /* (!nls_map) */ {
246 if (!vol->nls_map) {
247 vol->nls_map = load_nls_default();
248 if (!vol->nls_map) {
249 ntfs_error(vol->sb, "Failed to load default "
250 "NLS character set.");
251 return FALSE;
253 ntfs_debug("Using default NLS character set (%s).",
254 vol->nls_map->charset);
257 if (mft_zone_multiplier != -1) {
258 if (vol->mft_zone_multiplier && vol->mft_zone_multiplier !=
259 mft_zone_multiplier) {
260 ntfs_error(vol->sb, "Cannot change mft_zone_multiplier "
261 "on remount.");
262 return FALSE;
264 if (mft_zone_multiplier < 1 || mft_zone_multiplier > 4) {
265 ntfs_error(vol->sb, "Invalid mft_zone_multiplier. "
266 "Using default value, i.e. 1.");
267 mft_zone_multiplier = 1;
269 vol->mft_zone_multiplier = mft_zone_multiplier;
271 if (!vol->mft_zone_multiplier)
272 vol->mft_zone_multiplier = 1;
273 if (on_errors != -1)
274 vol->on_errors = on_errors;
275 if (!vol->on_errors || vol->on_errors == ON_ERRORS_RECOVER)
276 vol->on_errors |= ON_ERRORS_CONTINUE;
277 if (uid != (uid_t)-1)
278 vol->uid = uid;
279 if (gid != (gid_t)-1)
280 vol->gid = gid;
281 if (fmask != (mode_t)-1)
282 vol->fmask = fmask;
283 if (dmask != (mode_t)-1)
284 vol->dmask = dmask;
285 if (show_sys_files != -1) {
286 if (show_sys_files)
287 NVolSetShowSystemFiles(vol);
288 else
289 NVolClearShowSystemFiles(vol);
291 if (case_sensitive != -1) {
292 if (case_sensitive)
293 NVolSetCaseSensitive(vol);
294 else
295 NVolClearCaseSensitive(vol);
297 if (disable_sparse != -1) {
298 if (disable_sparse)
299 NVolClearSparseEnabled(vol);
300 else {
301 if (!NVolSparseEnabled(vol) &&
302 vol->major_ver && vol->major_ver < 3)
303 ntfs_warning(vol->sb, "Not enabling sparse "
304 "support due to NTFS volume "
305 "version %i.%i (need at least "
306 "version 3.0).", vol->major_ver,
307 vol->minor_ver);
308 else
309 NVolSetSparseEnabled(vol);
312 return TRUE;
313 needs_arg:
314 ntfs_error(vol->sb, "The %s option requires an argument.", p);
315 return FALSE;
316 needs_bool:
317 ntfs_error(vol->sb, "The %s option requires a boolean argument.", p);
318 return FALSE;
319 needs_val:
320 ntfs_error(vol->sb, "Invalid %s option argument: %s", p, ov);
321 return FALSE;
324 #ifdef NTFS_RW
327 * ntfs_write_volume_flags - write new flags to the volume information flags
328 * @vol: ntfs volume on which to modify the flags
329 * @flags: new flags value for the volume information flags
331 * Internal function. You probably want to use ntfs_{set,clear}_volume_flags()
332 * instead (see below).
334 * Replace the volume information flags on the volume @vol with the value
335 * supplied in @flags. Note, this overwrites the volume information flags, so
336 * make sure to combine the flags you want to modify with the old flags and use
337 * the result when calling ntfs_write_volume_flags().
339 * Return 0 on success and -errno on error.
341 static int ntfs_write_volume_flags(ntfs_volume *vol, const VOLUME_FLAGS flags)
343 ntfs_inode *ni = NTFS_I(vol->vol_ino);
344 MFT_RECORD *m;
345 VOLUME_INFORMATION *vi;
346 ntfs_attr_search_ctx *ctx;
347 int err;
349 ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
350 le16_to_cpu(vol->vol_flags), le16_to_cpu(flags));
351 if (vol->vol_flags == flags)
352 goto done;
353 BUG_ON(!ni);
354 m = map_mft_record(ni);
355 if (IS_ERR(m)) {
356 err = PTR_ERR(m);
357 goto err_out;
359 ctx = ntfs_attr_get_search_ctx(ni, m);
360 if (!ctx) {
361 err = -ENOMEM;
362 goto put_unm_err_out;
364 err = ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
365 ctx);
366 if (err)
367 goto put_unm_err_out;
368 vi = (VOLUME_INFORMATION*)((u8*)ctx->attr +
369 le16_to_cpu(ctx->attr->data.resident.value_offset));
370 vol->vol_flags = vi->flags = flags;
371 flush_dcache_mft_record_page(ctx->ntfs_ino);
372 mark_mft_record_dirty(ctx->ntfs_ino);
373 ntfs_attr_put_search_ctx(ctx);
374 unmap_mft_record(ni);
375 done:
376 ntfs_debug("Done.");
377 return 0;
378 put_unm_err_out:
379 if (ctx)
380 ntfs_attr_put_search_ctx(ctx);
381 unmap_mft_record(ni);
382 err_out:
383 ntfs_error(vol->sb, "Failed with error code %i.", -err);
384 return err;
388 * ntfs_set_volume_flags - set bits in the volume information flags
389 * @vol: ntfs volume on which to modify the flags
390 * @flags: flags to set on the volume
392 * Set the bits in @flags in the volume information flags on the volume @vol.
394 * Return 0 on success and -errno on error.
396 static inline int ntfs_set_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
398 flags &= VOLUME_FLAGS_MASK;
399 return ntfs_write_volume_flags(vol, vol->vol_flags | flags);
403 * ntfs_clear_volume_flags - clear bits in the volume information flags
404 * @vol: ntfs volume on which to modify the flags
405 * @flags: flags to clear on the volume
407 * Clear the bits in @flags in the volume information flags on the volume @vol.
409 * Return 0 on success and -errno on error.
411 static inline int ntfs_clear_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
413 flags &= VOLUME_FLAGS_MASK;
414 flags = vol->vol_flags & cpu_to_le16(~le16_to_cpu(flags));
415 return ntfs_write_volume_flags(vol, flags);
418 #endif /* NTFS_RW */
421 * ntfs_remount - change the mount options of a mounted ntfs filesystem
422 * @sb: superblock of mounted ntfs filesystem
423 * @flags: remount flags
424 * @opt: remount options string
426 * Change the mount options of an already mounted ntfs filesystem.
428 * NOTE: The VFS sets the @sb->s_flags remount flags to @flags after
429 * ntfs_remount() returns successfully (i.e. returns 0). Otherwise,
430 * @sb->s_flags are not changed.
432 static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
434 ntfs_volume *vol = NTFS_SB(sb);
436 ntfs_debug("Entering with remount options string: %s", opt);
437 #ifndef NTFS_RW
438 /* For read-only compiled driver, enforce all read-only flags. */
439 *flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
440 #else /* NTFS_RW */
442 * For the read-write compiled driver, if we are remounting read-write,
443 * make sure there are no volume errors and that no unsupported volume
444 * flags are set. Also, empty the logfile journal as it would become
445 * stale as soon as something is written to the volume and mark the
446 * volume dirty so that chkdsk is run if the volume is not umounted
447 * cleanly. Finally, mark the quotas out of date so Windows rescans
448 * the volume on boot and updates them.
450 * When remounting read-only, mark the volume clean if no volume errors
451 * have occured.
453 if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
454 static const char *es = ". Cannot remount read-write.";
456 /* Remounting read-write. */
457 if (NVolErrors(vol)) {
458 ntfs_error(sb, "Volume has errors and is read-only%s",
459 es);
460 return -EROFS;
462 if (vol->vol_flags & VOLUME_IS_DIRTY) {
463 ntfs_error(sb, "Volume is dirty and read-only%s", es);
464 return -EROFS;
466 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
467 ntfs_error(sb, "Volume has unsupported flags set and "
468 "is read-only%s", es);
469 return -EROFS;
471 if (ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
472 ntfs_error(sb, "Failed to set dirty bit in volume "
473 "information flags%s", es);
474 return -EROFS;
476 #if 0
477 // TODO: Enable this code once we start modifying anything that
478 // is different between NTFS 1.2 and 3.x...
479 /* Set NT4 compatibility flag on newer NTFS version volumes. */
480 if ((vol->major_ver > 1)) {
481 if (ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
482 ntfs_error(sb, "Failed to set NT4 "
483 "compatibility flag%s", es);
484 NVolSetErrors(vol);
485 return -EROFS;
488 #endif
489 if (!ntfs_empty_logfile(vol->logfile_ino)) {
490 ntfs_error(sb, "Failed to empty journal $LogFile%s",
491 es);
492 NVolSetErrors(vol);
493 return -EROFS;
495 if (!ntfs_mark_quotas_out_of_date(vol)) {
496 ntfs_error(sb, "Failed to mark quotas out of date%s",
497 es);
498 NVolSetErrors(vol);
499 return -EROFS;
501 if (!ntfs_stamp_usnjrnl(vol)) {
502 ntfs_error(sb, "Failed to stamp transation log "
503 "($UsnJrnl)%s", es);
504 NVolSetErrors(vol);
505 return -EROFS;
507 } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) {
508 /* Remounting read-only. */
509 if (!NVolErrors(vol)) {
510 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
511 ntfs_warning(sb, "Failed to clear dirty bit "
512 "in volume information "
513 "flags. Run chkdsk.");
516 #endif /* NTFS_RW */
518 // TODO: Deal with *flags.
520 if (!parse_options(vol, opt))
521 return -EINVAL;
522 ntfs_debug("Done.");
523 return 0;
527 * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector
528 * @sb: Super block of the device to which @b belongs.
529 * @b: Boot sector of device @sb to check.
530 * @silent: If TRUE, all output will be silenced.
532 * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot
533 * sector. Returns TRUE if it is valid and FALSE if not.
535 * @sb is only needed for warning/error output, i.e. it can be NULL when silent
536 * is TRUE.
538 static BOOL is_boot_sector_ntfs(const struct super_block *sb,
539 const NTFS_BOOT_SECTOR *b, const BOOL silent)
542 * Check that checksum == sum of u32 values from b to the checksum
543 * field. If checksum is zero, no checking is done. We will work when
544 * the checksum test fails, since some utilities update the boot sector
545 * ignoring the checksum which leaves the checksum out-of-date. We
546 * report a warning if this is the case.
548 if ((void*)b < (void*)&b->checksum && b->checksum && !silent) {
549 le32 *u;
550 u32 i;
552 for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u)
553 i += le32_to_cpup(u);
554 if (le32_to_cpu(b->checksum) != i)
555 ntfs_warning(sb, "Invalid boot sector checksum.");
557 /* Check OEMidentifier is "NTFS " */
558 if (b->oem_id != magicNTFS)
559 goto not_ntfs;
560 /* Check bytes per sector value is between 256 and 4096. */
561 if (le16_to_cpu(b->bpb.bytes_per_sector) < 0x100 ||
562 le16_to_cpu(b->bpb.bytes_per_sector) > 0x1000)
563 goto not_ntfs;
564 /* Check sectors per cluster value is valid. */
565 switch (b->bpb.sectors_per_cluster) {
566 case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128:
567 break;
568 default:
569 goto not_ntfs;
571 /* Check the cluster size is not above the maximum (64kiB). */
572 if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) *
573 b->bpb.sectors_per_cluster > NTFS_MAX_CLUSTER_SIZE)
574 goto not_ntfs;
575 /* Check reserved/unused fields are really zero. */
576 if (le16_to_cpu(b->bpb.reserved_sectors) ||
577 le16_to_cpu(b->bpb.root_entries) ||
578 le16_to_cpu(b->bpb.sectors) ||
579 le16_to_cpu(b->bpb.sectors_per_fat) ||
580 le32_to_cpu(b->bpb.large_sectors) || b->bpb.fats)
581 goto not_ntfs;
582 /* Check clusters per file mft record value is valid. */
583 if ((u8)b->clusters_per_mft_record < 0xe1 ||
584 (u8)b->clusters_per_mft_record > 0xf7)
585 switch (b->clusters_per_mft_record) {
586 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
587 break;
588 default:
589 goto not_ntfs;
591 /* Check clusters per index block value is valid. */
592 if ((u8)b->clusters_per_index_record < 0xe1 ||
593 (u8)b->clusters_per_index_record > 0xf7)
594 switch (b->clusters_per_index_record) {
595 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
596 break;
597 default:
598 goto not_ntfs;
601 * Check for valid end of sector marker. We will work without it, but
602 * many BIOSes will refuse to boot from a bootsector if the magic is
603 * incorrect, so we emit a warning.
605 if (!silent && b->end_of_sector_marker != const_cpu_to_le16(0xaa55))
606 ntfs_warning(sb, "Invalid end of sector marker.");
607 return TRUE;
608 not_ntfs:
609 return FALSE;
613 * read_ntfs_boot_sector - read the NTFS boot sector of a device
614 * @sb: super block of device to read the boot sector from
615 * @silent: if true, suppress all output
617 * Reads the boot sector from the device and validates it. If that fails, tries
618 * to read the backup boot sector, first from the end of the device a-la NT4 and
619 * later and then from the middle of the device a-la NT3.51 and before.
621 * If a valid boot sector is found but it is not the primary boot sector, we
622 * repair the primary boot sector silently (unless the device is read-only or
623 * the primary boot sector is not accessible).
625 * NOTE: To call this function, @sb must have the fields s_dev, the ntfs super
626 * block (u.ntfs_sb), nr_blocks and the device flags (s_flags) initialized
627 * to their respective values.
629 * Return the unlocked buffer head containing the boot sector or NULL on error.
631 static struct buffer_head *read_ntfs_boot_sector(struct super_block *sb,
632 const int silent)
634 const char *read_err_str = "Unable to read %s boot sector.";
635 struct buffer_head *bh_primary, *bh_backup;
636 long nr_blocks = NTFS_SB(sb)->nr_blocks;
638 /* Try to read primary boot sector. */
639 if ((bh_primary = sb_bread(sb, 0))) {
640 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
641 bh_primary->b_data, silent))
642 return bh_primary;
643 if (!silent)
644 ntfs_error(sb, "Primary boot sector is invalid.");
645 } else if (!silent)
646 ntfs_error(sb, read_err_str, "primary");
647 if (!(NTFS_SB(sb)->on_errors & ON_ERRORS_RECOVER)) {
648 if (bh_primary)
649 brelse(bh_primary);
650 if (!silent)
651 ntfs_error(sb, "Mount option errors=recover not used. "
652 "Aborting without trying to recover.");
653 return NULL;
655 /* Try to read NT4+ backup boot sector. */
656 if ((bh_backup = sb_bread(sb, nr_blocks - 1))) {
657 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
658 bh_backup->b_data, silent))
659 goto hotfix_primary_boot_sector;
660 brelse(bh_backup);
661 } else if (!silent)
662 ntfs_error(sb, read_err_str, "backup");
663 /* Try to read NT3.51- backup boot sector. */
664 if ((bh_backup = sb_bread(sb, nr_blocks >> 1))) {
665 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
666 bh_backup->b_data, silent))
667 goto hotfix_primary_boot_sector;
668 if (!silent)
669 ntfs_error(sb, "Could not find a valid backup boot "
670 "sector.");
671 brelse(bh_backup);
672 } else if (!silent)
673 ntfs_error(sb, read_err_str, "backup");
674 /* We failed. Cleanup and return. */
675 if (bh_primary)
676 brelse(bh_primary);
677 return NULL;
678 hotfix_primary_boot_sector:
679 if (bh_primary) {
681 * If we managed to read sector zero and the volume is not
682 * read-only, copy the found, valid backup boot sector to the
683 * primary boot sector.
685 if (!(sb->s_flags & MS_RDONLY)) {
686 ntfs_warning(sb, "Hot-fix: Recovering invalid primary "
687 "boot sector from backup copy.");
688 memcpy(bh_primary->b_data, bh_backup->b_data,
689 sb->s_blocksize);
690 mark_buffer_dirty(bh_primary);
691 sync_dirty_buffer(bh_primary);
692 if (buffer_uptodate(bh_primary)) {
693 brelse(bh_backup);
694 return bh_primary;
696 ntfs_error(sb, "Hot-fix: Device write error while "
697 "recovering primary boot sector.");
698 } else {
699 ntfs_warning(sb, "Hot-fix: Recovery of primary boot "
700 "sector failed: Read-only mount.");
702 brelse(bh_primary);
704 ntfs_warning(sb, "Using backup boot sector.");
705 return bh_backup;
709 * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol
710 * @vol: volume structure to initialise with data from boot sector
711 * @b: boot sector to parse
713 * Parse the ntfs boot sector @b and store all imporant information therein in
714 * the ntfs super block @vol. Return TRUE on success and FALSE on error.
716 static BOOL parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
718 unsigned int sectors_per_cluster_bits, nr_hidden_sects;
719 int clusters_per_mft_record, clusters_per_index_record;
720 s64 ll;
722 vol->sector_size = le16_to_cpu(b->bpb.bytes_per_sector);
723 vol->sector_size_bits = ffs(vol->sector_size) - 1;
724 ntfs_debug("vol->sector_size = %i (0x%x)", vol->sector_size,
725 vol->sector_size);
726 ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits,
727 vol->sector_size_bits);
728 if (vol->sector_size != vol->sb->s_blocksize)
729 ntfs_warning(vol->sb, "The boot sector indicates a sector size "
730 "different from the device sector size.");
731 ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster);
732 sectors_per_cluster_bits = ffs(b->bpb.sectors_per_cluster) - 1;
733 ntfs_debug("sectors_per_cluster_bits = 0x%x",
734 sectors_per_cluster_bits);
735 nr_hidden_sects = le32_to_cpu(b->bpb.hidden_sectors);
736 ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects);
737 vol->cluster_size = vol->sector_size << sectors_per_cluster_bits;
738 vol->cluster_size_mask = vol->cluster_size - 1;
739 vol->cluster_size_bits = ffs(vol->cluster_size) - 1;
740 ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size,
741 vol->cluster_size);
742 ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask);
743 ntfs_debug("vol->cluster_size_bits = %i (0x%x)",
744 vol->cluster_size_bits, vol->cluster_size_bits);
745 if (vol->sector_size > vol->cluster_size) {
746 ntfs_error(vol->sb, "Sector sizes above the cluster size are "
747 "not supported. Sorry.");
748 return FALSE;
750 if (vol->sb->s_blocksize > vol->cluster_size) {
751 ntfs_error(vol->sb, "Cluster sizes smaller than the device "
752 "sector size are not supported. Sorry.");
753 return FALSE;
755 clusters_per_mft_record = b->clusters_per_mft_record;
756 ntfs_debug("clusters_per_mft_record = %i (0x%x)",
757 clusters_per_mft_record, clusters_per_mft_record);
758 if (clusters_per_mft_record > 0)
759 vol->mft_record_size = vol->cluster_size <<
760 (ffs(clusters_per_mft_record) - 1);
761 else
763 * When mft_record_size < cluster_size, clusters_per_mft_record
764 * = -log2(mft_record_size) bytes. mft_record_size normaly is
765 * 1024 bytes, which is encoded as 0xF6 (-10 in decimal).
767 vol->mft_record_size = 1 << -clusters_per_mft_record;
768 vol->mft_record_size_mask = vol->mft_record_size - 1;
769 vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1;
770 ntfs_debug("vol->mft_record_size = %i (0x%x)", vol->mft_record_size,
771 vol->mft_record_size);
772 ntfs_debug("vol->mft_record_size_mask = 0x%x",
773 vol->mft_record_size_mask);
774 ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
775 vol->mft_record_size_bits, vol->mft_record_size_bits);
777 * We cannot support mft record sizes above the PAGE_CACHE_SIZE since
778 * we store $MFT/$DATA, the table of mft records in the page cache.
780 if (vol->mft_record_size > PAGE_CACHE_SIZE) {
781 ntfs_error(vol->sb, "Mft record size %i (0x%x) exceeds the "
782 "page cache size on your system %lu (0x%lx). "
783 "This is not supported. Sorry.",
784 vol->mft_record_size, vol->mft_record_size,
785 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE);
786 return FALSE;
788 clusters_per_index_record = b->clusters_per_index_record;
789 ntfs_debug("clusters_per_index_record = %i (0x%x)",
790 clusters_per_index_record, clusters_per_index_record);
791 if (clusters_per_index_record > 0)
792 vol->index_record_size = vol->cluster_size <<
793 (ffs(clusters_per_index_record) - 1);
794 else
796 * When index_record_size < cluster_size,
797 * clusters_per_index_record = -log2(index_record_size) bytes.
798 * index_record_size normaly equals 4096 bytes, which is
799 * encoded as 0xF4 (-12 in decimal).
801 vol->index_record_size = 1 << -clusters_per_index_record;
802 vol->index_record_size_mask = vol->index_record_size - 1;
803 vol->index_record_size_bits = ffs(vol->index_record_size) - 1;
804 ntfs_debug("vol->index_record_size = %i (0x%x)",
805 vol->index_record_size, vol->index_record_size);
806 ntfs_debug("vol->index_record_size_mask = 0x%x",
807 vol->index_record_size_mask);
808 ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
809 vol->index_record_size_bits,
810 vol->index_record_size_bits);
812 * Get the size of the volume in clusters and check for 64-bit-ness.
813 * Windows currently only uses 32 bits to save the clusters so we do
814 * the same as it is much faster on 32-bit CPUs.
816 ll = sle64_to_cpu(b->number_of_sectors) >> sectors_per_cluster_bits;
817 if ((u64)ll >= 1ULL << 32) {
818 ntfs_error(vol->sb, "Cannot handle 64-bit clusters. Sorry.");
819 return FALSE;
821 vol->nr_clusters = ll;
822 ntfs_debug("vol->nr_clusters = 0x%llx", (long long)vol->nr_clusters);
824 * On an architecture where unsigned long is 32-bits, we restrict the
825 * volume size to 2TiB (2^41). On a 64-bit architecture, the compiler
826 * will hopefully optimize the whole check away.
828 if (sizeof(unsigned long) < 8) {
829 if ((ll << vol->cluster_size_bits) >= (1ULL << 41)) {
830 ntfs_error(vol->sb, "Volume size (%lluTiB) is too "
831 "large for this architecture. "
832 "Maximum supported is 2TiB. Sorry.",
833 (unsigned long long)ll >> (40 -
834 vol->cluster_size_bits));
835 return FALSE;
838 ll = sle64_to_cpu(b->mft_lcn);
839 if (ll >= vol->nr_clusters) {
840 ntfs_error(vol->sb, "MFT LCN is beyond end of volume. Weird.");
841 return FALSE;
843 vol->mft_lcn = ll;
844 ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol->mft_lcn);
845 ll = sle64_to_cpu(b->mftmirr_lcn);
846 if (ll >= vol->nr_clusters) {
847 ntfs_error(vol->sb, "MFTMirr LCN is beyond end of volume. "
848 "Weird.");
849 return FALSE;
851 vol->mftmirr_lcn = ll;
852 ntfs_debug("vol->mftmirr_lcn = 0x%llx", (long long)vol->mftmirr_lcn);
853 #ifdef NTFS_RW
855 * Work out the size of the mft mirror in number of mft records. If the
856 * cluster size is less than or equal to the size taken by four mft
857 * records, the mft mirror stores the first four mft records. If the
858 * cluster size is bigger than the size taken by four mft records, the
859 * mft mirror contains as many mft records as will fit into one
860 * cluster.
862 if (vol->cluster_size <= (4 << vol->mft_record_size_bits))
863 vol->mftmirr_size = 4;
864 else
865 vol->mftmirr_size = vol->cluster_size >>
866 vol->mft_record_size_bits;
867 ntfs_debug("vol->mftmirr_size = %i", vol->mftmirr_size);
868 #endif /* NTFS_RW */
869 vol->serial_no = le64_to_cpu(b->volume_serial_number);
870 ntfs_debug("vol->serial_no = 0x%llx",
871 (unsigned long long)vol->serial_no);
872 return TRUE;
876 * ntfs_setup_allocators - initialize the cluster and mft allocators
877 * @vol: volume structure for which to setup the allocators
879 * Setup the cluster (lcn) and mft allocators to the starting values.
881 static void ntfs_setup_allocators(ntfs_volume *vol)
883 #ifdef NTFS_RW
884 LCN mft_zone_size, mft_lcn;
885 #endif /* NTFS_RW */
887 ntfs_debug("vol->mft_zone_multiplier = 0x%x",
888 vol->mft_zone_multiplier);
889 #ifdef NTFS_RW
890 /* Determine the size of the MFT zone. */
891 mft_zone_size = vol->nr_clusters;
892 switch (vol->mft_zone_multiplier) { /* % of volume size in clusters */
893 case 4:
894 mft_zone_size >>= 1; /* 50% */
895 break;
896 case 3:
897 mft_zone_size = (mft_zone_size +
898 (mft_zone_size >> 1)) >> 2; /* 37.5% */
899 break;
900 case 2:
901 mft_zone_size >>= 2; /* 25% */
902 break;
903 /* case 1: */
904 default:
905 mft_zone_size >>= 3; /* 12.5% */
906 break;
908 /* Setup the mft zone. */
909 vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn;
910 ntfs_debug("vol->mft_zone_pos = 0x%llx",
911 (unsigned long long)vol->mft_zone_pos);
913 * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
914 * source) and if the actual mft_lcn is in the expected place or even
915 * further to the front of the volume, extend the mft_zone to cover the
916 * beginning of the volume as well. This is in order to protect the
917 * area reserved for the mft bitmap as well within the mft_zone itself.
918 * On non-standard volumes we do not protect it as the overhead would
919 * be higher than the speed increase we would get by doing it.
921 mft_lcn = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size;
922 if (mft_lcn * vol->cluster_size < 16 * 1024)
923 mft_lcn = (16 * 1024 + vol->cluster_size - 1) /
924 vol->cluster_size;
925 if (vol->mft_zone_start <= mft_lcn)
926 vol->mft_zone_start = 0;
927 ntfs_debug("vol->mft_zone_start = 0x%llx",
928 (unsigned long long)vol->mft_zone_start);
930 * Need to cap the mft zone on non-standard volumes so that it does
931 * not point outside the boundaries of the volume. We do this by
932 * halving the zone size until we are inside the volume.
934 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
935 while (vol->mft_zone_end >= vol->nr_clusters) {
936 mft_zone_size >>= 1;
937 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
939 ntfs_debug("vol->mft_zone_end = 0x%llx",
940 (unsigned long long)vol->mft_zone_end);
942 * Set the current position within each data zone to the start of the
943 * respective zone.
945 vol->data1_zone_pos = vol->mft_zone_end;
946 ntfs_debug("vol->data1_zone_pos = 0x%llx",
947 (unsigned long long)vol->data1_zone_pos);
948 vol->data2_zone_pos = 0;
949 ntfs_debug("vol->data2_zone_pos = 0x%llx",
950 (unsigned long long)vol->data2_zone_pos);
952 /* Set the mft data allocation position to mft record 24. */
953 vol->mft_data_pos = 24;
954 ntfs_debug("vol->mft_data_pos = 0x%llx",
955 (unsigned long long)vol->mft_data_pos);
956 #endif /* NTFS_RW */
959 #ifdef NTFS_RW
962 * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume
963 * @vol: ntfs super block describing device whose mft mirror to load
965 * Return TRUE on success or FALSE on error.
967 static BOOL load_and_init_mft_mirror(ntfs_volume *vol)
969 struct inode *tmp_ino;
970 ntfs_inode *tmp_ni;
972 ntfs_debug("Entering.");
973 /* Get mft mirror inode. */
974 tmp_ino = ntfs_iget(vol->sb, FILE_MFTMirr);
975 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
976 if (!IS_ERR(tmp_ino))
977 iput(tmp_ino);
978 /* Caller will display error message. */
979 return FALSE;
982 * Re-initialize some specifics about $MFTMirr's inode as
983 * ntfs_read_inode() will have set up the default ones.
985 /* Set uid and gid to root. */
986 tmp_ino->i_uid = tmp_ino->i_gid = 0;
987 /* Regular file. No access for anyone. */
988 tmp_ino->i_mode = S_IFREG;
989 /* No VFS initiated operations allowed for $MFTMirr. */
990 tmp_ino->i_op = &ntfs_empty_inode_ops;
991 tmp_ino->i_fop = &ntfs_empty_file_ops;
992 /* Put in our special address space operations. */
993 tmp_ino->i_mapping->a_ops = &ntfs_mst_aops;
994 tmp_ni = NTFS_I(tmp_ino);
995 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
996 NInoSetMstProtected(tmp_ni);
997 NInoSetSparseDisabled(tmp_ni);
999 * Set up our little cheat allowing us to reuse the async read io
1000 * completion handler for directories.
1002 tmp_ni->itype.index.block_size = vol->mft_record_size;
1003 tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits;
1004 vol->mftmirr_ino = tmp_ino;
1005 ntfs_debug("Done.");
1006 return TRUE;
1010 * check_mft_mirror - compare contents of the mft mirror with the mft
1011 * @vol: ntfs super block describing device whose mft mirror to check
1013 * Return TRUE on success or FALSE on error.
1015 * Note, this function also results in the mft mirror runlist being completely
1016 * mapped into memory. The mft mirror write code requires this and will BUG()
1017 * should it find an unmapped runlist element.
1019 static BOOL check_mft_mirror(ntfs_volume *vol)
1021 struct super_block *sb = vol->sb;
1022 ntfs_inode *mirr_ni;
1023 struct page *mft_page, *mirr_page;
1024 u8 *kmft, *kmirr;
1025 runlist_element *rl, rl2[2];
1026 pgoff_t index;
1027 int mrecs_per_page, i;
1029 ntfs_debug("Entering.");
1030 /* Compare contents of $MFT and $MFTMirr. */
1031 mrecs_per_page = PAGE_CACHE_SIZE / vol->mft_record_size;
1032 BUG_ON(!mrecs_per_page);
1033 BUG_ON(!vol->mftmirr_size);
1034 mft_page = mirr_page = NULL;
1035 kmft = kmirr = NULL;
1036 index = i = 0;
1037 do {
1038 u32 bytes;
1040 /* Switch pages if necessary. */
1041 if (!(i % mrecs_per_page)) {
1042 if (index) {
1043 ntfs_unmap_page(mft_page);
1044 ntfs_unmap_page(mirr_page);
1046 /* Get the $MFT page. */
1047 mft_page = ntfs_map_page(vol->mft_ino->i_mapping,
1048 index);
1049 if (IS_ERR(mft_page)) {
1050 ntfs_error(sb, "Failed to read $MFT.");
1051 return FALSE;
1053 kmft = page_address(mft_page);
1054 /* Get the $MFTMirr page. */
1055 mirr_page = ntfs_map_page(vol->mftmirr_ino->i_mapping,
1056 index);
1057 if (IS_ERR(mirr_page)) {
1058 ntfs_error(sb, "Failed to read $MFTMirr.");
1059 goto mft_unmap_out;
1061 kmirr = page_address(mirr_page);
1062 ++index;
1064 /* Make sure the record is ok. */
1065 if (ntfs_is_baad_recordp((le32*)kmft)) {
1066 ntfs_error(sb, "Incomplete multi sector transfer "
1067 "detected in mft record %i.", i);
1068 mm_unmap_out:
1069 ntfs_unmap_page(mirr_page);
1070 mft_unmap_out:
1071 ntfs_unmap_page(mft_page);
1072 return FALSE;
1074 if (ntfs_is_baad_recordp((le32*)kmirr)) {
1075 ntfs_error(sb, "Incomplete multi sector transfer "
1076 "detected in mft mirror record %i.", i);
1077 goto mm_unmap_out;
1079 /* Get the amount of data in the current record. */
1080 bytes = le32_to_cpu(((MFT_RECORD*)kmft)->bytes_in_use);
1081 if (!bytes || bytes > vol->mft_record_size) {
1082 bytes = le32_to_cpu(((MFT_RECORD*)kmirr)->bytes_in_use);
1083 if (!bytes || bytes > vol->mft_record_size)
1084 bytes = vol->mft_record_size;
1086 /* Compare the two records. */
1087 if (memcmp(kmft, kmirr, bytes)) {
1088 ntfs_error(sb, "$MFT and $MFTMirr (record %i) do not "
1089 "match. Run ntfsfix or chkdsk.", i);
1090 goto mm_unmap_out;
1092 kmft += vol->mft_record_size;
1093 kmirr += vol->mft_record_size;
1094 } while (++i < vol->mftmirr_size);
1095 /* Release the last pages. */
1096 ntfs_unmap_page(mft_page);
1097 ntfs_unmap_page(mirr_page);
1099 /* Construct the mft mirror runlist by hand. */
1100 rl2[0].vcn = 0;
1101 rl2[0].lcn = vol->mftmirr_lcn;
1102 rl2[0].length = (vol->mftmirr_size * vol->mft_record_size +
1103 vol->cluster_size - 1) / vol->cluster_size;
1104 rl2[1].vcn = rl2[0].length;
1105 rl2[1].lcn = LCN_ENOENT;
1106 rl2[1].length = 0;
1108 * Because we have just read all of the mft mirror, we know we have
1109 * mapped the full runlist for it.
1111 mirr_ni = NTFS_I(vol->mftmirr_ino);
1112 down_read(&mirr_ni->runlist.lock);
1113 rl = mirr_ni->runlist.rl;
1114 /* Compare the two runlists. They must be identical. */
1115 i = 0;
1116 do {
1117 if (rl2[i].vcn != rl[i].vcn || rl2[i].lcn != rl[i].lcn ||
1118 rl2[i].length != rl[i].length) {
1119 ntfs_error(sb, "$MFTMirr location mismatch. "
1120 "Run chkdsk.");
1121 up_read(&mirr_ni->runlist.lock);
1122 return FALSE;
1124 } while (rl2[i++].length);
1125 up_read(&mirr_ni->runlist.lock);
1126 ntfs_debug("Done.");
1127 return TRUE;
1131 * load_and_check_logfile - load and check the logfile inode for a volume
1132 * @vol: ntfs super block describing device whose logfile to load
1134 * Return TRUE on success or FALSE on error.
1136 static BOOL load_and_check_logfile(ntfs_volume *vol,
1137 RESTART_PAGE_HEADER **rp)
1139 struct inode *tmp_ino;
1141 ntfs_debug("Entering.");
1142 tmp_ino = ntfs_iget(vol->sb, FILE_LogFile);
1143 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1144 if (!IS_ERR(tmp_ino))
1145 iput(tmp_ino);
1146 /* Caller will display error message. */
1147 return FALSE;
1149 if (!ntfs_check_logfile(tmp_ino, rp)) {
1150 iput(tmp_ino);
1151 /* ntfs_check_logfile() will have displayed error output. */
1152 return FALSE;
1154 NInoSetSparseDisabled(NTFS_I(tmp_ino));
1155 vol->logfile_ino = tmp_ino;
1156 ntfs_debug("Done.");
1157 return TRUE;
1160 #define NTFS_HIBERFIL_HEADER_SIZE 4096
1163 * check_windows_hibernation_status - check if Windows is suspended on a volume
1164 * @vol: ntfs super block of device to check
1166 * Check if Windows is hibernated on the ntfs volume @vol. This is done by
1167 * looking for the file hiberfil.sys in the root directory of the volume. If
1168 * the file is not present Windows is definitely not suspended.
1170 * If hiberfil.sys exists and is less than 4kiB in size it means Windows is
1171 * definitely suspended (this volume is not the system volume). Caveat: on a
1172 * system with many volumes it is possible that the < 4kiB check is bogus but
1173 * for now this should do fine.
1175 * If hiberfil.sys exists and is larger than 4kiB in size, we need to read the
1176 * hiberfil header (which is the first 4kiB). If this begins with "hibr",
1177 * Windows is definitely suspended. If it is completely full of zeroes,
1178 * Windows is definitely not hibernated. Any other case is treated as if
1179 * Windows is suspended. This caters for the above mentioned caveat of a
1180 * system with many volumes where no "hibr" magic would be present and there is
1181 * no zero header.
1183 * Return 0 if Windows is not hibernated on the volume, >0 if Windows is
1184 * hibernated on the volume, and -errno on error.
1186 static int check_windows_hibernation_status(ntfs_volume *vol)
1188 MFT_REF mref;
1189 struct inode *vi;
1190 ntfs_inode *ni;
1191 struct page *page;
1192 u32 *kaddr, *kend;
1193 ntfs_name *name = NULL;
1194 int ret = 1;
1195 static const ntfschar hiberfil[13] = { const_cpu_to_le16('h'),
1196 const_cpu_to_le16('i'), const_cpu_to_le16('b'),
1197 const_cpu_to_le16('e'), const_cpu_to_le16('r'),
1198 const_cpu_to_le16('f'), const_cpu_to_le16('i'),
1199 const_cpu_to_le16('l'), const_cpu_to_le16('.'),
1200 const_cpu_to_le16('s'), const_cpu_to_le16('y'),
1201 const_cpu_to_le16('s'), 0 };
1203 ntfs_debug("Entering.");
1205 * Find the inode number for the hibernation file by looking up the
1206 * filename hiberfil.sys in the root directory.
1208 down(&vol->root_ino->i_sem);
1209 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->root_ino), hiberfil, 12,
1210 &name);
1211 up(&vol->root_ino->i_sem);
1212 if (IS_ERR_MREF(mref)) {
1213 ret = MREF_ERR(mref);
1214 /* If the file does not exist, Windows is not hibernated. */
1215 if (ret == -ENOENT) {
1216 ntfs_debug("hiberfil.sys not present. Windows is not "
1217 "hibernated on the volume.");
1218 return 0;
1220 /* A real error occured. */
1221 ntfs_error(vol->sb, "Failed to find inode number for "
1222 "hiberfil.sys.");
1223 return ret;
1225 /* We do not care for the type of match that was found. */
1226 kfree(name);
1227 /* Get the inode. */
1228 vi = ntfs_iget(vol->sb, MREF(mref));
1229 if (IS_ERR(vi) || is_bad_inode(vi)) {
1230 if (!IS_ERR(vi))
1231 iput(vi);
1232 ntfs_error(vol->sb, "Failed to load hiberfil.sys.");
1233 return IS_ERR(vi) ? PTR_ERR(vi) : -EIO;
1235 if (unlikely(i_size_read(vi) < NTFS_HIBERFIL_HEADER_SIZE)) {
1236 ntfs_debug("hiberfil.sys is smaller than 4kiB (0x%llx). "
1237 "Windows is hibernated on the volume. This "
1238 "is not the system volume.", i_size_read(vi));
1239 goto iput_out;
1241 ni = NTFS_I(vi);
1242 page = ntfs_map_page(vi->i_mapping, 0);
1243 if (IS_ERR(page)) {
1244 ntfs_error(vol->sb, "Failed to read from hiberfil.sys.");
1245 ret = PTR_ERR(page);
1246 goto iput_out;
1248 kaddr = (u32*)page_address(page);
1249 if (*(le32*)kaddr == const_cpu_to_le32(0x72626968)/*'hibr'*/) {
1250 ntfs_debug("Magic \"hibr\" found in hiberfil.sys. Windows is "
1251 "hibernated on the volume. This is the "
1252 "system volume.");
1253 goto unm_iput_out;
1255 kend = kaddr + NTFS_HIBERFIL_HEADER_SIZE/sizeof(*kaddr);
1256 do {
1257 if (unlikely(*kaddr)) {
1258 ntfs_debug("hiberfil.sys is larger than 4kiB "
1259 "(0x%llx), does not contain the "
1260 "\"hibr\" magic, and does not have a "
1261 "zero header. Windows is hibernated "
1262 "on the volume. This is not the "
1263 "system volume.", i_size_read(vi));
1264 goto unm_iput_out;
1266 } while (++kaddr < kend);
1267 ntfs_debug("hiberfil.sys contains a zero header. Windows is not "
1268 "hibernated on the volume. This is the system "
1269 "volume.");
1270 ret = 0;
1271 unm_iput_out:
1272 ntfs_unmap_page(page);
1273 iput_out:
1274 iput(vi);
1275 return ret;
1279 * load_and_init_quota - load and setup the quota file for a volume if present
1280 * @vol: ntfs super block describing device whose quota file to load
1282 * Return TRUE on success or FALSE on error. If $Quota is not present, we
1283 * leave vol->quota_ino as NULL and return success.
1285 static BOOL load_and_init_quota(ntfs_volume *vol)
1287 MFT_REF mref;
1288 struct inode *tmp_ino;
1289 ntfs_name *name = NULL;
1290 static const ntfschar Quota[7] = { const_cpu_to_le16('$'),
1291 const_cpu_to_le16('Q'), const_cpu_to_le16('u'),
1292 const_cpu_to_le16('o'), const_cpu_to_le16('t'),
1293 const_cpu_to_le16('a'), 0 };
1294 static ntfschar Q[3] = { const_cpu_to_le16('$'),
1295 const_cpu_to_le16('Q'), 0 };
1297 ntfs_debug("Entering.");
1299 * Find the inode number for the quota file by looking up the filename
1300 * $Quota in the extended system files directory $Extend.
1302 down(&vol->extend_ino->i_sem);
1303 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), Quota, 6,
1304 &name);
1305 up(&vol->extend_ino->i_sem);
1306 if (IS_ERR_MREF(mref)) {
1308 * If the file does not exist, quotas are disabled and have
1309 * never been enabled on this volume, just return success.
1311 if (MREF_ERR(mref) == -ENOENT) {
1312 ntfs_debug("$Quota not present. Volume does not have "
1313 "quotas enabled.");
1315 * No need to try to set quotas out of date if they are
1316 * not enabled.
1318 NVolSetQuotaOutOfDate(vol);
1319 return TRUE;
1321 /* A real error occured. */
1322 ntfs_error(vol->sb, "Failed to find inode number for $Quota.");
1323 return FALSE;
1325 /* We do not care for the type of match that was found. */
1326 kfree(name);
1327 /* Get the inode. */
1328 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1329 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1330 if (!IS_ERR(tmp_ino))
1331 iput(tmp_ino);
1332 ntfs_error(vol->sb, "Failed to load $Quota.");
1333 return FALSE;
1335 vol->quota_ino = tmp_ino;
1336 /* Get the $Q index allocation attribute. */
1337 tmp_ino = ntfs_index_iget(vol->quota_ino, Q, 2);
1338 if (IS_ERR(tmp_ino)) {
1339 ntfs_error(vol->sb, "Failed to load $Quota/$Q index.");
1340 return FALSE;
1342 vol->quota_q_ino = tmp_ino;
1343 ntfs_debug("Done.");
1344 return TRUE;
1348 * load_and_init_usnjrnl - load and setup the transaction log if present
1349 * @vol: ntfs super block describing device whose usnjrnl file to load
1351 * Return TRUE on success or FALSE on error.
1353 * If $UsnJrnl is not present or in the process of being disabled, we set
1354 * NVolUsnJrnlStamped() and return success.
1356 * If the $UsnJrnl $DATA/$J attribute has a size equal to the lowest valid usn,
1357 * i.e. transaction logging has only just been enabled or the journal has been
1358 * stamped and nothing has been logged since, we also set NVolUsnJrnlStamped()
1359 * and return success.
1361 static BOOL load_and_init_usnjrnl(ntfs_volume *vol)
1363 MFT_REF mref;
1364 struct inode *tmp_ino;
1365 ntfs_inode *tmp_ni;
1366 struct page *page;
1367 ntfs_name *name = NULL;
1368 USN_HEADER *uh;
1369 static const ntfschar UsnJrnl[9] = { const_cpu_to_le16('$'),
1370 const_cpu_to_le16('U'), const_cpu_to_le16('s'),
1371 const_cpu_to_le16('n'), const_cpu_to_le16('J'),
1372 const_cpu_to_le16('r'), const_cpu_to_le16('n'),
1373 const_cpu_to_le16('l'), 0 };
1374 static ntfschar Max[5] = { const_cpu_to_le16('$'),
1375 const_cpu_to_le16('M'), const_cpu_to_le16('a'),
1376 const_cpu_to_le16('x'), 0 };
1377 static ntfschar J[3] = { const_cpu_to_le16('$'),
1378 const_cpu_to_le16('J'), 0 };
1380 ntfs_debug("Entering.");
1382 * Find the inode number for the transaction log file by looking up the
1383 * filename $UsnJrnl in the extended system files directory $Extend.
1385 down(&vol->extend_ino->i_sem);
1386 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), UsnJrnl, 8,
1387 &name);
1388 up(&vol->extend_ino->i_sem);
1389 if (IS_ERR_MREF(mref)) {
1391 * If the file does not exist, transaction logging is disabled,
1392 * just return success.
1394 if (MREF_ERR(mref) == -ENOENT) {
1395 ntfs_debug("$UsnJrnl not present. Volume does not "
1396 "have transaction logging enabled.");
1397 not_enabled:
1399 * No need to try to stamp the transaction log if
1400 * transaction logging is not enabled.
1402 NVolSetUsnJrnlStamped(vol);
1403 return TRUE;
1405 /* A real error occured. */
1406 ntfs_error(vol->sb, "Failed to find inode number for "
1407 "$UsnJrnl.");
1408 return FALSE;
1410 /* We do not care for the type of match that was found. */
1411 kfree(name);
1412 /* Get the inode. */
1413 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1414 if (unlikely(IS_ERR(tmp_ino) || is_bad_inode(tmp_ino))) {
1415 if (!IS_ERR(tmp_ino))
1416 iput(tmp_ino);
1417 ntfs_error(vol->sb, "Failed to load $UsnJrnl.");
1418 return FALSE;
1420 vol->usnjrnl_ino = tmp_ino;
1422 * If the transaction log is in the process of being deleted, we can
1423 * ignore it.
1425 if (unlikely(vol->vol_flags & VOLUME_DELETE_USN_UNDERWAY)) {
1426 ntfs_debug("$UsnJrnl in the process of being disabled. "
1427 "Volume does not have transaction logging "
1428 "enabled.");
1429 goto not_enabled;
1431 /* Get the $DATA/$Max attribute. */
1432 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, Max, 4);
1433 if (IS_ERR(tmp_ino)) {
1434 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$Max "
1435 "attribute.");
1436 return FALSE;
1438 vol->usnjrnl_max_ino = tmp_ino;
1439 if (unlikely(i_size_read(tmp_ino) < sizeof(USN_HEADER))) {
1440 ntfs_error(vol->sb, "Found corrupt $UsnJrnl/$DATA/$Max "
1441 "attribute (size is 0x%llx but should be at "
1442 "least 0x%x bytes).", i_size_read(tmp_ino),
1443 sizeof(USN_HEADER));
1444 return FALSE;
1446 /* Get the $DATA/$J attribute. */
1447 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, J, 2);
1448 if (IS_ERR(tmp_ino)) {
1449 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$J "
1450 "attribute.");
1451 return FALSE;
1453 vol->usnjrnl_j_ino = tmp_ino;
1454 /* Verify $J is non-resident and sparse. */
1455 tmp_ni = NTFS_I(vol->usnjrnl_j_ino);
1456 if (unlikely(!NInoNonResident(tmp_ni) || !NInoSparse(tmp_ni))) {
1457 ntfs_error(vol->sb, "$UsnJrnl/$DATA/$J attribute is resident "
1458 "and/or not sparse.");
1459 return FALSE;
1461 /* Read the USN_HEADER from $DATA/$Max. */
1462 page = ntfs_map_page(vol->usnjrnl_max_ino->i_mapping, 0);
1463 if (IS_ERR(page)) {
1464 ntfs_error(vol->sb, "Failed to read from $UsnJrnl/$DATA/$Max "
1465 "attribute.");
1466 return FALSE;
1468 uh = (USN_HEADER*)page_address(page);
1469 /* Sanity check the $Max. */
1470 if (unlikely(sle64_to_cpu(uh->allocation_delta) >
1471 sle64_to_cpu(uh->maximum_size))) {
1472 ntfs_error(vol->sb, "Allocation delta (0x%llx) exceeds "
1473 "maximum size (0x%llx). $UsnJrnl is corrupt.",
1474 (long long)sle64_to_cpu(uh->allocation_delta),
1475 (long long)sle64_to_cpu(uh->maximum_size));
1476 ntfs_unmap_page(page);
1477 return FALSE;
1480 * If the transaction log has been stamped and nothing has been written
1481 * to it since, we do not need to stamp it.
1483 if (unlikely(sle64_to_cpu(uh->lowest_valid_usn) >=
1484 i_size_read(vol->usnjrnl_j_ino))) {
1485 if (likely(sle64_to_cpu(uh->lowest_valid_usn) ==
1486 i_size_read(vol->usnjrnl_j_ino))) {
1487 ntfs_unmap_page(page);
1488 ntfs_debug("$UsnJrnl is enabled but nothing has been "
1489 "logged since it was last stamped. "
1490 "Treating this as if the volume does "
1491 "not have transaction logging "
1492 "enabled.");
1493 goto not_enabled;
1495 ntfs_error(vol->sb, "$UsnJrnl has lowest valid usn (0x%llx) "
1496 "which is out of bounds (0x%llx). $UsnJrnl "
1497 "is corrupt.",
1498 (long long)sle64_to_cpu(uh->lowest_valid_usn),
1499 i_size_read(vol->usnjrnl_j_ino));
1500 ntfs_unmap_page(page);
1501 return FALSE;
1503 ntfs_unmap_page(page);
1504 ntfs_debug("Done.");
1505 return TRUE;
1509 * load_and_init_attrdef - load the attribute definitions table for a volume
1510 * @vol: ntfs super block describing device whose attrdef to load
1512 * Return TRUE on success or FALSE on error.
1514 static BOOL load_and_init_attrdef(ntfs_volume *vol)
1516 loff_t i_size;
1517 struct super_block *sb = vol->sb;
1518 struct inode *ino;
1519 struct page *page;
1520 pgoff_t index, max_index;
1521 unsigned int size;
1523 ntfs_debug("Entering.");
1524 /* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */
1525 ino = ntfs_iget(sb, FILE_AttrDef);
1526 if (IS_ERR(ino) || is_bad_inode(ino)) {
1527 if (!IS_ERR(ino))
1528 iput(ino);
1529 goto failed;
1531 NInoSetSparseDisabled(NTFS_I(ino));
1532 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
1533 i_size = i_size_read(ino);
1534 if (i_size <= 0 || i_size > 0x7fffffff)
1535 goto iput_failed;
1536 vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(i_size);
1537 if (!vol->attrdef)
1538 goto iput_failed;
1539 index = 0;
1540 max_index = i_size >> PAGE_CACHE_SHIFT;
1541 size = PAGE_CACHE_SIZE;
1542 while (index < max_index) {
1543 /* Read the attrdef table and copy it into the linear buffer. */
1544 read_partial_attrdef_page:
1545 page = ntfs_map_page(ino->i_mapping, index);
1546 if (IS_ERR(page))
1547 goto free_iput_failed;
1548 memcpy((u8*)vol->attrdef + (index++ << PAGE_CACHE_SHIFT),
1549 page_address(page), size);
1550 ntfs_unmap_page(page);
1552 if (size == PAGE_CACHE_SIZE) {
1553 size = i_size & ~PAGE_CACHE_MASK;
1554 if (size)
1555 goto read_partial_attrdef_page;
1557 vol->attrdef_size = i_size;
1558 ntfs_debug("Read %llu bytes from $AttrDef.", i_size);
1559 iput(ino);
1560 return TRUE;
1561 free_iput_failed:
1562 ntfs_free(vol->attrdef);
1563 vol->attrdef = NULL;
1564 iput_failed:
1565 iput(ino);
1566 failed:
1567 ntfs_error(sb, "Failed to initialize attribute definition table.");
1568 return FALSE;
1571 #endif /* NTFS_RW */
1574 * load_and_init_upcase - load the upcase table for an ntfs volume
1575 * @vol: ntfs super block describing device whose upcase to load
1577 * Return TRUE on success or FALSE on error.
1579 static BOOL load_and_init_upcase(ntfs_volume *vol)
1581 loff_t i_size;
1582 struct super_block *sb = vol->sb;
1583 struct inode *ino;
1584 struct page *page;
1585 pgoff_t index, max_index;
1586 unsigned int size;
1587 int i, max;
1589 ntfs_debug("Entering.");
1590 /* Read upcase table and setup vol->upcase and vol->upcase_len. */
1591 ino = ntfs_iget(sb, FILE_UpCase);
1592 if (IS_ERR(ino) || is_bad_inode(ino)) {
1593 if (!IS_ERR(ino))
1594 iput(ino);
1595 goto upcase_failed;
1598 * The upcase size must not be above 64k Unicode characters, must not
1599 * be zero and must be a multiple of sizeof(ntfschar).
1601 i_size = i_size_read(ino);
1602 if (!i_size || i_size & (sizeof(ntfschar) - 1) ||
1603 i_size > 64ULL * 1024 * sizeof(ntfschar))
1604 goto iput_upcase_failed;
1605 vol->upcase = (ntfschar*)ntfs_malloc_nofs(i_size);
1606 if (!vol->upcase)
1607 goto iput_upcase_failed;
1608 index = 0;
1609 max_index = i_size >> PAGE_CACHE_SHIFT;
1610 size = PAGE_CACHE_SIZE;
1611 while (index < max_index) {
1612 /* Read the upcase table and copy it into the linear buffer. */
1613 read_partial_upcase_page:
1614 page = ntfs_map_page(ino->i_mapping, index);
1615 if (IS_ERR(page))
1616 goto iput_upcase_failed;
1617 memcpy((char*)vol->upcase + (index++ << PAGE_CACHE_SHIFT),
1618 page_address(page), size);
1619 ntfs_unmap_page(page);
1621 if (size == PAGE_CACHE_SIZE) {
1622 size = i_size & ~PAGE_CACHE_MASK;
1623 if (size)
1624 goto read_partial_upcase_page;
1626 vol->upcase_len = i_size >> UCHAR_T_SIZE_BITS;
1627 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
1628 i_size, 64 * 1024 * sizeof(ntfschar));
1629 iput(ino);
1630 down(&ntfs_lock);
1631 if (!default_upcase) {
1632 ntfs_debug("Using volume specified $UpCase since default is "
1633 "not present.");
1634 up(&ntfs_lock);
1635 return TRUE;
1637 max = default_upcase_len;
1638 if (max > vol->upcase_len)
1639 max = vol->upcase_len;
1640 for (i = 0; i < max; i++)
1641 if (vol->upcase[i] != default_upcase[i])
1642 break;
1643 if (i == max) {
1644 ntfs_free(vol->upcase);
1645 vol->upcase = default_upcase;
1646 vol->upcase_len = max;
1647 ntfs_nr_upcase_users++;
1648 up(&ntfs_lock);
1649 ntfs_debug("Volume specified $UpCase matches default. Using "
1650 "default.");
1651 return TRUE;
1653 up(&ntfs_lock);
1654 ntfs_debug("Using volume specified $UpCase since it does not match "
1655 "the default.");
1656 return TRUE;
1657 iput_upcase_failed:
1658 iput(ino);
1659 ntfs_free(vol->upcase);
1660 vol->upcase = NULL;
1661 upcase_failed:
1662 down(&ntfs_lock);
1663 if (default_upcase) {
1664 vol->upcase = default_upcase;
1665 vol->upcase_len = default_upcase_len;
1666 ntfs_nr_upcase_users++;
1667 up(&ntfs_lock);
1668 ntfs_error(sb, "Failed to load $UpCase from the volume. Using "
1669 "default.");
1670 return TRUE;
1672 up(&ntfs_lock);
1673 ntfs_error(sb, "Failed to initialize upcase table.");
1674 return FALSE;
1678 * load_system_files - open the system files using normal functions
1679 * @vol: ntfs super block describing device whose system files to load
1681 * Open the system files with normal access functions and complete setting up
1682 * the ntfs super block @vol.
1684 * Return TRUE on success or FALSE on error.
1686 static BOOL load_system_files(ntfs_volume *vol)
1688 struct super_block *sb = vol->sb;
1689 MFT_RECORD *m;
1690 VOLUME_INFORMATION *vi;
1691 RESTART_PAGE_HEADER *rp;
1692 ntfs_attr_search_ctx *ctx;
1693 #ifdef NTFS_RW
1694 int err;
1695 #endif /* NTFS_RW */
1697 ntfs_debug("Entering.");
1698 #ifdef NTFS_RW
1699 /* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */
1700 if (!load_and_init_mft_mirror(vol) || !check_mft_mirror(vol)) {
1701 static const char *es1 = "Failed to load $MFTMirr";
1702 static const char *es2 = "$MFTMirr does not match $MFT";
1703 static const char *es3 = ". Run ntfsfix and/or chkdsk.";
1705 /* If a read-write mount, convert it to a read-only mount. */
1706 if (!(sb->s_flags & MS_RDONLY)) {
1707 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1708 ON_ERRORS_CONTINUE))) {
1709 ntfs_error(sb, "%s and neither on_errors="
1710 "continue nor on_errors="
1711 "remount-ro was specified%s",
1712 !vol->mftmirr_ino ? es1 : es2,
1713 es3);
1714 goto iput_mirr_err_out;
1716 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1717 ntfs_error(sb, "%s. Mounting read-only%s",
1718 !vol->mftmirr_ino ? es1 : es2, es3);
1719 } else
1720 ntfs_warning(sb, "%s. Will not be able to remount "
1721 "read-write%s",
1722 !vol->mftmirr_ino ? es1 : es2, es3);
1723 /* This will prevent a read-write remount. */
1724 NVolSetErrors(vol);
1726 #endif /* NTFS_RW */
1727 /* Get mft bitmap attribute inode. */
1728 vol->mftbmp_ino = ntfs_attr_iget(vol->mft_ino, AT_BITMAP, NULL, 0);
1729 if (IS_ERR(vol->mftbmp_ino)) {
1730 ntfs_error(sb, "Failed to load $MFT/$BITMAP attribute.");
1731 goto iput_mirr_err_out;
1733 /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */
1734 if (!load_and_init_upcase(vol))
1735 goto iput_mftbmp_err_out;
1736 #ifdef NTFS_RW
1738 * Read attribute definitions table and setup @vol->attrdef and
1739 * @vol->attrdef_size.
1741 if (!load_and_init_attrdef(vol))
1742 goto iput_upcase_err_out;
1743 #endif /* NTFS_RW */
1745 * Get the cluster allocation bitmap inode and verify the size, no
1746 * need for any locking at this stage as we are already running
1747 * exclusively as we are mount in progress task.
1749 vol->lcnbmp_ino = ntfs_iget(sb, FILE_Bitmap);
1750 if (IS_ERR(vol->lcnbmp_ino) || is_bad_inode(vol->lcnbmp_ino)) {
1751 if (!IS_ERR(vol->lcnbmp_ino))
1752 iput(vol->lcnbmp_ino);
1753 goto bitmap_failed;
1755 NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino));
1756 if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) {
1757 iput(vol->lcnbmp_ino);
1758 bitmap_failed:
1759 ntfs_error(sb, "Failed to load $Bitmap.");
1760 goto iput_attrdef_err_out;
1763 * Get the volume inode and setup our cache of the volume flags and
1764 * version.
1766 vol->vol_ino = ntfs_iget(sb, FILE_Volume);
1767 if (IS_ERR(vol->vol_ino) || is_bad_inode(vol->vol_ino)) {
1768 if (!IS_ERR(vol->vol_ino))
1769 iput(vol->vol_ino);
1770 volume_failed:
1771 ntfs_error(sb, "Failed to load $Volume.");
1772 goto iput_lcnbmp_err_out;
1774 m = map_mft_record(NTFS_I(vol->vol_ino));
1775 if (IS_ERR(m)) {
1776 iput_volume_failed:
1777 iput(vol->vol_ino);
1778 goto volume_failed;
1780 if (!(ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m))) {
1781 ntfs_error(sb, "Failed to get attribute search context.");
1782 goto get_ctx_vol_failed;
1784 if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
1785 ctx) || ctx->attr->non_resident || ctx->attr->flags) {
1786 err_put_vol:
1787 ntfs_attr_put_search_ctx(ctx);
1788 get_ctx_vol_failed:
1789 unmap_mft_record(NTFS_I(vol->vol_ino));
1790 goto iput_volume_failed;
1792 vi = (VOLUME_INFORMATION*)((char*)ctx->attr +
1793 le16_to_cpu(ctx->attr->data.resident.value_offset));
1794 /* Some bounds checks. */
1795 if ((u8*)vi < (u8*)ctx->attr || (u8*)vi +
1796 le32_to_cpu(ctx->attr->data.resident.value_length) >
1797 (u8*)ctx->attr + le32_to_cpu(ctx->attr->length))
1798 goto err_put_vol;
1799 /* Copy the volume flags and version to the ntfs_volume structure. */
1800 vol->vol_flags = vi->flags;
1801 vol->major_ver = vi->major_ver;
1802 vol->minor_ver = vi->minor_ver;
1803 ntfs_attr_put_search_ctx(ctx);
1804 unmap_mft_record(NTFS_I(vol->vol_ino));
1805 printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver,
1806 vol->minor_ver);
1807 if (vol->major_ver < 3 && NVolSparseEnabled(vol)) {
1808 ntfs_warning(vol->sb, "Disabling sparse support due to NTFS "
1809 "volume version %i.%i (need at least version "
1810 "3.0).", vol->major_ver, vol->minor_ver);
1811 NVolClearSparseEnabled(vol);
1813 #ifdef NTFS_RW
1814 /* Make sure that no unsupported volume flags are set. */
1815 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
1816 static const char *es1a = "Volume is dirty";
1817 static const char *es1b = "Volume has unsupported flags set";
1818 static const char *es2 = ". Run chkdsk and mount in Windows.";
1819 const char *es1;
1821 es1 = vol->vol_flags & VOLUME_IS_DIRTY ? es1a : es1b;
1822 /* If a read-write mount, convert it to a read-only mount. */
1823 if (!(sb->s_flags & MS_RDONLY)) {
1824 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1825 ON_ERRORS_CONTINUE))) {
1826 ntfs_error(sb, "%s and neither on_errors="
1827 "continue nor on_errors="
1828 "remount-ro was specified%s",
1829 es1, es2);
1830 goto iput_vol_err_out;
1832 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1833 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1834 } else
1835 ntfs_warning(sb, "%s. Will not be able to remount "
1836 "read-write%s", es1, es2);
1838 * Do not set NVolErrors() because ntfs_remount() re-checks the
1839 * flags which we need to do in case any flags have changed.
1843 * Get the inode for the logfile, check it and determine if the volume
1844 * was shutdown cleanly.
1846 rp = NULL;
1847 if (!load_and_check_logfile(vol, &rp) ||
1848 !ntfs_is_logfile_clean(vol->logfile_ino, rp)) {
1849 static const char *es1a = "Failed to load $LogFile";
1850 static const char *es1b = "$LogFile is not clean";
1851 static const char *es2 = ". Mount in Windows.";
1852 const char *es1;
1854 es1 = !vol->logfile_ino ? es1a : es1b;
1855 /* If a read-write mount, convert it to a read-only mount. */
1856 if (!(sb->s_flags & MS_RDONLY)) {
1857 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1858 ON_ERRORS_CONTINUE))) {
1859 ntfs_error(sb, "%s and neither on_errors="
1860 "continue nor on_errors="
1861 "remount-ro was specified%s",
1862 es1, es2);
1863 if (vol->logfile_ino) {
1864 BUG_ON(!rp);
1865 ntfs_free(rp);
1867 goto iput_logfile_err_out;
1869 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1870 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1871 } else
1872 ntfs_warning(sb, "%s. Will not be able to remount "
1873 "read-write%s", es1, es2);
1874 /* This will prevent a read-write remount. */
1875 NVolSetErrors(vol);
1877 ntfs_free(rp);
1878 #endif /* NTFS_RW */
1879 /* Get the root directory inode so we can do path lookups. */
1880 vol->root_ino = ntfs_iget(sb, FILE_root);
1881 if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) {
1882 if (!IS_ERR(vol->root_ino))
1883 iput(vol->root_ino);
1884 ntfs_error(sb, "Failed to load root directory.");
1885 goto iput_logfile_err_out;
1887 #ifdef NTFS_RW
1889 * Check if Windows is suspended to disk on the target volume. If it
1890 * is hibernated, we must not write *anything* to the disk so set
1891 * NVolErrors() without setting the dirty volume flag and mount
1892 * read-only. This will prevent read-write remounting and it will also
1893 * prevent all writes.
1895 err = check_windows_hibernation_status(vol);
1896 if (unlikely(err)) {
1897 static const char *es1a = "Failed to determine if Windows is "
1898 "hibernated";
1899 static const char *es1b = "Windows is hibernated";
1900 static const char *es2 = ". Run chkdsk.";
1901 const char *es1;
1903 es1 = err < 0 ? es1a : es1b;
1904 /* If a read-write mount, convert it to a read-only mount. */
1905 if (!(sb->s_flags & MS_RDONLY)) {
1906 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1907 ON_ERRORS_CONTINUE))) {
1908 ntfs_error(sb, "%s and neither on_errors="
1909 "continue nor on_errors="
1910 "remount-ro was specified%s",
1911 es1, es2);
1912 goto iput_root_err_out;
1914 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1915 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1916 } else
1917 ntfs_warning(sb, "%s. Will not be able to remount "
1918 "read-write%s", es1, es2);
1919 /* This will prevent a read-write remount. */
1920 NVolSetErrors(vol);
1922 /* If (still) a read-write mount, mark the volume dirty. */
1923 if (!(sb->s_flags & MS_RDONLY) &&
1924 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
1925 static const char *es1 = "Failed to set dirty bit in volume "
1926 "information flags";
1927 static const char *es2 = ". Run chkdsk.";
1929 /* Convert to a read-only mount. */
1930 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1931 ON_ERRORS_CONTINUE))) {
1932 ntfs_error(sb, "%s and neither on_errors=continue nor "
1933 "on_errors=remount-ro was specified%s",
1934 es1, es2);
1935 goto iput_root_err_out;
1937 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1938 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1940 * Do not set NVolErrors() because ntfs_remount() might manage
1941 * to set the dirty flag in which case all would be well.
1944 #if 0
1945 // TODO: Enable this code once we start modifying anything that is
1946 // different between NTFS 1.2 and 3.x...
1948 * If (still) a read-write mount, set the NT4 compatibility flag on
1949 * newer NTFS version volumes.
1951 if (!(sb->s_flags & MS_RDONLY) && (vol->major_ver > 1) &&
1952 ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
1953 static const char *es1 = "Failed to set NT4 compatibility flag";
1954 static const char *es2 = ". Run chkdsk.";
1956 /* Convert to a read-only mount. */
1957 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1958 ON_ERRORS_CONTINUE))) {
1959 ntfs_error(sb, "%s and neither on_errors=continue nor "
1960 "on_errors=remount-ro was specified%s",
1961 es1, es2);
1962 goto iput_root_err_out;
1964 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1965 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1966 NVolSetErrors(vol);
1968 #endif
1969 /* If (still) a read-write mount, empty the logfile. */
1970 if (!(sb->s_flags & MS_RDONLY) &&
1971 !ntfs_empty_logfile(vol->logfile_ino)) {
1972 static const char *es1 = "Failed to empty $LogFile";
1973 static const char *es2 = ". Mount in Windows.";
1975 /* Convert to a read-only mount. */
1976 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1977 ON_ERRORS_CONTINUE))) {
1978 ntfs_error(sb, "%s and neither on_errors=continue nor "
1979 "on_errors=remount-ro was specified%s",
1980 es1, es2);
1981 goto iput_root_err_out;
1983 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1984 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1985 NVolSetErrors(vol);
1987 #endif /* NTFS_RW */
1988 /* If on NTFS versions before 3.0, we are done. */
1989 if (unlikely(vol->major_ver < 3))
1990 return TRUE;
1991 /* NTFS 3.0+ specific initialization. */
1992 /* Get the security descriptors inode. */
1993 vol->secure_ino = ntfs_iget(sb, FILE_Secure);
1994 if (IS_ERR(vol->secure_ino) || is_bad_inode(vol->secure_ino)) {
1995 if (!IS_ERR(vol->secure_ino))
1996 iput(vol->secure_ino);
1997 ntfs_error(sb, "Failed to load $Secure.");
1998 goto iput_root_err_out;
2000 // TODO: Initialize security.
2001 /* Get the extended system files' directory inode. */
2002 vol->extend_ino = ntfs_iget(sb, FILE_Extend);
2003 if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) {
2004 if (!IS_ERR(vol->extend_ino))
2005 iput(vol->extend_ino);
2006 ntfs_error(sb, "Failed to load $Extend.");
2007 goto iput_sec_err_out;
2009 #ifdef NTFS_RW
2010 /* Find the quota file, load it if present, and set it up. */
2011 if (!load_and_init_quota(vol)) {
2012 static const char *es1 = "Failed to load $Quota";
2013 static const char *es2 = ". Run chkdsk.";
2015 /* If a read-write mount, convert it to a read-only mount. */
2016 if (!(sb->s_flags & MS_RDONLY)) {
2017 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2018 ON_ERRORS_CONTINUE))) {
2019 ntfs_error(sb, "%s and neither on_errors="
2020 "continue nor on_errors="
2021 "remount-ro was specified%s",
2022 es1, es2);
2023 goto iput_quota_err_out;
2025 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
2026 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2027 } else
2028 ntfs_warning(sb, "%s. Will not be able to remount "
2029 "read-write%s", es1, es2);
2030 /* This will prevent a read-write remount. */
2031 NVolSetErrors(vol);
2033 /* If (still) a read-write mount, mark the quotas out of date. */
2034 if (!(sb->s_flags & MS_RDONLY) &&
2035 !ntfs_mark_quotas_out_of_date(vol)) {
2036 static const char *es1 = "Failed to mark quotas out of date";
2037 static const char *es2 = ". Run chkdsk.";
2039 /* Convert to a read-only mount. */
2040 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2041 ON_ERRORS_CONTINUE))) {
2042 ntfs_error(sb, "%s and neither on_errors=continue nor "
2043 "on_errors=remount-ro was specified%s",
2044 es1, es2);
2045 goto iput_quota_err_out;
2047 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2048 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
2049 NVolSetErrors(vol);
2052 * Find the transaction log file ($UsnJrnl), load it if present, check
2053 * it, and set it up.
2055 if (!load_and_init_usnjrnl(vol)) {
2056 static const char *es1 = "Failed to load $UsnJrnl";
2057 static const char *es2 = ". Run chkdsk.";
2059 /* If a read-write mount, convert it to a read-only mount. */
2060 if (!(sb->s_flags & MS_RDONLY)) {
2061 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2062 ON_ERRORS_CONTINUE))) {
2063 ntfs_error(sb, "%s and neither on_errors="
2064 "continue nor on_errors="
2065 "remount-ro was specified%s",
2066 es1, es2);
2067 goto iput_usnjrnl_err_out;
2069 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
2070 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2071 } else
2072 ntfs_warning(sb, "%s. Will not be able to remount "
2073 "read-write%s", es1, es2);
2074 /* This will prevent a read-write remount. */
2075 NVolSetErrors(vol);
2077 /* If (still) a read-write mount, stamp the transaction log. */
2078 if (!(sb->s_flags & MS_RDONLY) && !ntfs_stamp_usnjrnl(vol)) {
2079 static const char *es1 = "Failed to stamp transaction log "
2080 "($UsnJrnl)";
2081 static const char *es2 = ". Run chkdsk.";
2083 /* Convert to a read-only mount. */
2084 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2085 ON_ERRORS_CONTINUE))) {
2086 ntfs_error(sb, "%s and neither on_errors=continue nor "
2087 "on_errors=remount-ro was specified%s",
2088 es1, es2);
2089 goto iput_usnjrnl_err_out;
2091 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2092 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
2093 NVolSetErrors(vol);
2095 #endif /* NTFS_RW */
2096 return TRUE;
2097 #ifdef NTFS_RW
2098 iput_usnjrnl_err_out:
2099 if (vol->usnjrnl_j_ino)
2100 iput(vol->usnjrnl_j_ino);
2101 if (vol->usnjrnl_max_ino)
2102 iput(vol->usnjrnl_max_ino);
2103 if (vol->usnjrnl_ino)
2104 iput(vol->usnjrnl_ino);
2105 iput_quota_err_out:
2106 if (vol->quota_q_ino)
2107 iput(vol->quota_q_ino);
2108 if (vol->quota_ino)
2109 iput(vol->quota_ino);
2110 iput(vol->extend_ino);
2111 #endif /* NTFS_RW */
2112 iput_sec_err_out:
2113 iput(vol->secure_ino);
2114 iput_root_err_out:
2115 iput(vol->root_ino);
2116 iput_logfile_err_out:
2117 #ifdef NTFS_RW
2118 if (vol->logfile_ino)
2119 iput(vol->logfile_ino);
2120 iput_vol_err_out:
2121 #endif /* NTFS_RW */
2122 iput(vol->vol_ino);
2123 iput_lcnbmp_err_out:
2124 iput(vol->lcnbmp_ino);
2125 iput_attrdef_err_out:
2126 vol->attrdef_size = 0;
2127 if (vol->attrdef) {
2128 ntfs_free(vol->attrdef);
2129 vol->attrdef = NULL;
2131 #ifdef NTFS_RW
2132 iput_upcase_err_out:
2133 #endif /* NTFS_RW */
2134 vol->upcase_len = 0;
2135 down(&ntfs_lock);
2136 if (vol->upcase == default_upcase) {
2137 ntfs_nr_upcase_users--;
2138 vol->upcase = NULL;
2140 up(&ntfs_lock);
2141 if (vol->upcase) {
2142 ntfs_free(vol->upcase);
2143 vol->upcase = NULL;
2145 iput_mftbmp_err_out:
2146 iput(vol->mftbmp_ino);
2147 iput_mirr_err_out:
2148 #ifdef NTFS_RW
2149 if (vol->mftmirr_ino)
2150 iput(vol->mftmirr_ino);
2151 #endif /* NTFS_RW */
2152 return FALSE;
2156 * ntfs_put_super - called by the vfs to unmount a volume
2157 * @sb: vfs superblock of volume to unmount
2159 * ntfs_put_super() is called by the VFS (from fs/super.c::do_umount()) when
2160 * the volume is being unmounted (umount system call has been invoked) and it
2161 * releases all inodes and memory belonging to the NTFS specific part of the
2162 * super block.
2164 static void ntfs_put_super(struct super_block *sb)
2166 ntfs_volume *vol = NTFS_SB(sb);
2168 ntfs_debug("Entering.");
2169 #ifdef NTFS_RW
2171 * Commit all inodes while they are still open in case some of them
2172 * cause others to be dirtied.
2174 ntfs_commit_inode(vol->vol_ino);
2176 /* NTFS 3.0+ specific. */
2177 if (vol->major_ver >= 3) {
2178 if (vol->usnjrnl_j_ino)
2179 ntfs_commit_inode(vol->usnjrnl_j_ino);
2180 if (vol->usnjrnl_max_ino)
2181 ntfs_commit_inode(vol->usnjrnl_max_ino);
2182 if (vol->usnjrnl_ino)
2183 ntfs_commit_inode(vol->usnjrnl_ino);
2184 if (vol->quota_q_ino)
2185 ntfs_commit_inode(vol->quota_q_ino);
2186 if (vol->quota_ino)
2187 ntfs_commit_inode(vol->quota_ino);
2188 if (vol->extend_ino)
2189 ntfs_commit_inode(vol->extend_ino);
2190 if (vol->secure_ino)
2191 ntfs_commit_inode(vol->secure_ino);
2194 ntfs_commit_inode(vol->root_ino);
2196 down_write(&vol->lcnbmp_lock);
2197 ntfs_commit_inode(vol->lcnbmp_ino);
2198 up_write(&vol->lcnbmp_lock);
2200 down_write(&vol->mftbmp_lock);
2201 ntfs_commit_inode(vol->mftbmp_ino);
2202 up_write(&vol->mftbmp_lock);
2204 if (vol->logfile_ino)
2205 ntfs_commit_inode(vol->logfile_ino);
2207 if (vol->mftmirr_ino)
2208 ntfs_commit_inode(vol->mftmirr_ino);
2209 ntfs_commit_inode(vol->mft_ino);
2212 * If a read-write mount and no volume errors have occured, mark the
2213 * volume clean. Also, re-commit all affected inodes.
2215 if (!(sb->s_flags & MS_RDONLY)) {
2216 if (!NVolErrors(vol)) {
2217 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
2218 ntfs_warning(sb, "Failed to clear dirty bit "
2219 "in volume information "
2220 "flags. Run chkdsk.");
2221 ntfs_commit_inode(vol->vol_ino);
2222 ntfs_commit_inode(vol->root_ino);
2223 if (vol->mftmirr_ino)
2224 ntfs_commit_inode(vol->mftmirr_ino);
2225 ntfs_commit_inode(vol->mft_ino);
2226 } else {
2227 ntfs_warning(sb, "Volume has errors. Leaving volume "
2228 "marked dirty. Run chkdsk.");
2231 #endif /* NTFS_RW */
2233 iput(vol->vol_ino);
2234 vol->vol_ino = NULL;
2236 /* NTFS 3.0+ specific clean up. */
2237 if (vol->major_ver >= 3) {
2238 #ifdef NTFS_RW
2239 if (vol->usnjrnl_j_ino) {
2240 iput(vol->usnjrnl_j_ino);
2241 vol->usnjrnl_j_ino = NULL;
2243 if (vol->usnjrnl_max_ino) {
2244 iput(vol->usnjrnl_max_ino);
2245 vol->usnjrnl_max_ino = NULL;
2247 if (vol->usnjrnl_ino) {
2248 iput(vol->usnjrnl_ino);
2249 vol->usnjrnl_ino = NULL;
2251 if (vol->quota_q_ino) {
2252 iput(vol->quota_q_ino);
2253 vol->quota_q_ino = NULL;
2255 if (vol->quota_ino) {
2256 iput(vol->quota_ino);
2257 vol->quota_ino = NULL;
2259 #endif /* NTFS_RW */
2260 if (vol->extend_ino) {
2261 iput(vol->extend_ino);
2262 vol->extend_ino = NULL;
2264 if (vol->secure_ino) {
2265 iput(vol->secure_ino);
2266 vol->secure_ino = NULL;
2270 iput(vol->root_ino);
2271 vol->root_ino = NULL;
2273 down_write(&vol->lcnbmp_lock);
2274 iput(vol->lcnbmp_ino);
2275 vol->lcnbmp_ino = NULL;
2276 up_write(&vol->lcnbmp_lock);
2278 down_write(&vol->mftbmp_lock);
2279 iput(vol->mftbmp_ino);
2280 vol->mftbmp_ino = NULL;
2281 up_write(&vol->mftbmp_lock);
2283 #ifdef NTFS_RW
2284 if (vol->logfile_ino) {
2285 iput(vol->logfile_ino);
2286 vol->logfile_ino = NULL;
2288 if (vol->mftmirr_ino) {
2289 /* Re-commit the mft mirror and mft just in case. */
2290 ntfs_commit_inode(vol->mftmirr_ino);
2291 ntfs_commit_inode(vol->mft_ino);
2292 iput(vol->mftmirr_ino);
2293 vol->mftmirr_ino = NULL;
2296 * If any dirty inodes are left, throw away all mft data page cache
2297 * pages to allow a clean umount. This should never happen any more
2298 * due to mft.c::ntfs_mft_writepage() cleaning all the dirty pages as
2299 * the underlying mft records are written out and cleaned. If it does,
2300 * happen anyway, we want to know...
2302 ntfs_commit_inode(vol->mft_ino);
2303 write_inode_now(vol->mft_ino, 1);
2304 if (!list_empty(&sb->s_dirty)) {
2305 const char *s1, *s2;
2307 down(&vol->mft_ino->i_sem);
2308 truncate_inode_pages(vol->mft_ino->i_mapping, 0);
2309 up(&vol->mft_ino->i_sem);
2310 write_inode_now(vol->mft_ino, 1);
2311 if (!list_empty(&sb->s_dirty)) {
2312 static const char *_s1 = "inodes";
2313 static const char *_s2 = "";
2314 s1 = _s1;
2315 s2 = _s2;
2316 } else {
2317 static const char *_s1 = "mft pages";
2318 static const char *_s2 = "They have been thrown "
2319 "away. ";
2320 s1 = _s1;
2321 s2 = _s2;
2323 ntfs_error(sb, "Dirty %s found at umount time. %sYou should "
2324 "run chkdsk. Please email "
2325 "linux-ntfs-dev@lists.sourceforge.net and say "
2326 "that you saw this message. Thank you.", s1,
2327 s2);
2329 #endif /* NTFS_RW */
2331 iput(vol->mft_ino);
2332 vol->mft_ino = NULL;
2334 /* Throw away the table of attribute definitions. */
2335 vol->attrdef_size = 0;
2336 if (vol->attrdef) {
2337 ntfs_free(vol->attrdef);
2338 vol->attrdef = NULL;
2340 vol->upcase_len = 0;
2342 * Destroy the global default upcase table if necessary. Also decrease
2343 * the number of upcase users if we are a user.
2345 down(&ntfs_lock);
2346 if (vol->upcase == default_upcase) {
2347 ntfs_nr_upcase_users--;
2348 vol->upcase = NULL;
2350 if (!ntfs_nr_upcase_users && default_upcase) {
2351 ntfs_free(default_upcase);
2352 default_upcase = NULL;
2354 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
2355 free_compression_buffers();
2356 up(&ntfs_lock);
2357 if (vol->upcase) {
2358 ntfs_free(vol->upcase);
2359 vol->upcase = NULL;
2361 if (vol->nls_map) {
2362 unload_nls(vol->nls_map);
2363 vol->nls_map = NULL;
2365 sb->s_fs_info = NULL;
2366 kfree(vol);
2367 return;
2371 * get_nr_free_clusters - return the number of free clusters on a volume
2372 * @vol: ntfs volume for which to obtain free cluster count
2374 * Calculate the number of free clusters on the mounted NTFS volume @vol. We
2375 * actually calculate the number of clusters in use instead because this
2376 * allows us to not care about partial pages as these will be just zero filled
2377 * and hence not be counted as allocated clusters.
2379 * The only particularity is that clusters beyond the end of the logical ntfs
2380 * volume will be marked as allocated to prevent errors which means we have to
2381 * discount those at the end. This is important as the cluster bitmap always
2382 * has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside
2383 * the logical volume and marked in use when they are not as they do not exist.
2385 * If any pages cannot be read we assume all clusters in the erroring pages are
2386 * in use. This means we return an underestimate on errors which is better than
2387 * an overestimate.
2389 static s64 get_nr_free_clusters(ntfs_volume *vol)
2391 s64 nr_free = vol->nr_clusters;
2392 u32 *kaddr;
2393 struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
2394 filler_t *readpage = (filler_t*)mapping->a_ops->readpage;
2395 struct page *page;
2396 pgoff_t index, max_index;
2398 ntfs_debug("Entering.");
2399 /* Serialize accesses to the cluster bitmap. */
2400 down_read(&vol->lcnbmp_lock);
2402 * Convert the number of bits into bytes rounded up, then convert into
2403 * multiples of PAGE_CACHE_SIZE, rounding up so that if we have one
2404 * full and one partial page max_index = 2.
2406 max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >>
2407 PAGE_CACHE_SHIFT;
2408 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2409 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.",
2410 max_index, PAGE_CACHE_SIZE / 4);
2411 for (index = 0; index < max_index; index++) {
2412 unsigned int i;
2414 * Read the page from page cache, getting it from backing store
2415 * if necessary, and increment the use count.
2417 page = read_cache_page(mapping, index, (filler_t*)readpage,
2418 NULL);
2419 /* Ignore pages which errored synchronously. */
2420 if (IS_ERR(page)) {
2421 ntfs_debug("Sync read_cache_page() error. Skipping "
2422 "page (index 0x%lx).", index);
2423 nr_free -= PAGE_CACHE_SIZE * 8;
2424 continue;
2426 wait_on_page_locked(page);
2427 /* Ignore pages which errored asynchronously. */
2428 if (!PageUptodate(page)) {
2429 ntfs_debug("Async read_cache_page() error. Skipping "
2430 "page (index 0x%lx).", index);
2431 page_cache_release(page);
2432 nr_free -= PAGE_CACHE_SIZE * 8;
2433 continue;
2435 kaddr = (u32*)kmap_atomic(page, KM_USER0);
2437 * For each 4 bytes, subtract the number of set bits. If this
2438 * is the last page and it is partial we don't really care as
2439 * it just means we do a little extra work but it won't affect
2440 * the result as all out of range bytes are set to zero by
2441 * ntfs_readpage().
2443 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
2444 nr_free -= (s64)hweight32(kaddr[i]);
2445 kunmap_atomic(kaddr, KM_USER0);
2446 page_cache_release(page);
2448 ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index - 1);
2450 * Fixup for eventual bits outside logical ntfs volume (see function
2451 * description above).
2453 if (vol->nr_clusters & 63)
2454 nr_free += 64 - (vol->nr_clusters & 63);
2455 up_read(&vol->lcnbmp_lock);
2456 /* If errors occured we may well have gone below zero, fix this. */
2457 if (nr_free < 0)
2458 nr_free = 0;
2459 ntfs_debug("Exiting.");
2460 return nr_free;
2464 * __get_nr_free_mft_records - return the number of free inodes on a volume
2465 * @vol: ntfs volume for which to obtain free inode count
2466 * @nr_free: number of mft records in filesystem
2467 * @max_index: maximum number of pages containing set bits
2469 * Calculate the number of free mft records (inodes) on the mounted NTFS
2470 * volume @vol. We actually calculate the number of mft records in use instead
2471 * because this allows us to not care about partial pages as these will be just
2472 * zero filled and hence not be counted as allocated mft record.
2474 * If any pages cannot be read we assume all mft records in the erroring pages
2475 * are in use. This means we return an underestimate on errors which is better
2476 * than an overestimate.
2478 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
2480 static unsigned long __get_nr_free_mft_records(ntfs_volume *vol,
2481 s64 nr_free, const pgoff_t max_index)
2483 u32 *kaddr;
2484 struct address_space *mapping = vol->mftbmp_ino->i_mapping;
2485 filler_t *readpage = (filler_t*)mapping->a_ops->readpage;
2486 struct page *page;
2487 pgoff_t index;
2489 ntfs_debug("Entering.");
2490 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2491 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
2492 "0x%lx.", max_index, PAGE_CACHE_SIZE / 4);
2493 for (index = 0; index < max_index; index++) {
2494 unsigned int i;
2496 * Read the page from page cache, getting it from backing store
2497 * if necessary, and increment the use count.
2499 page = read_cache_page(mapping, index, (filler_t*)readpage,
2500 NULL);
2501 /* Ignore pages which errored synchronously. */
2502 if (IS_ERR(page)) {
2503 ntfs_debug("Sync read_cache_page() error. Skipping "
2504 "page (index 0x%lx).", index);
2505 nr_free -= PAGE_CACHE_SIZE * 8;
2506 continue;
2508 wait_on_page_locked(page);
2509 /* Ignore pages which errored asynchronously. */
2510 if (!PageUptodate(page)) {
2511 ntfs_debug("Async read_cache_page() error. Skipping "
2512 "page (index 0x%lx).", index);
2513 page_cache_release(page);
2514 nr_free -= PAGE_CACHE_SIZE * 8;
2515 continue;
2517 kaddr = (u32*)kmap_atomic(page, KM_USER0);
2519 * For each 4 bytes, subtract the number of set bits. If this
2520 * is the last page and it is partial we don't really care as
2521 * it just means we do a little extra work but it won't affect
2522 * the result as all out of range bytes are set to zero by
2523 * ntfs_readpage().
2525 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
2526 nr_free -= (s64)hweight32(kaddr[i]);
2527 kunmap_atomic(kaddr, KM_USER0);
2528 page_cache_release(page);
2530 ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.",
2531 index - 1);
2532 /* If errors occured we may well have gone below zero, fix this. */
2533 if (nr_free < 0)
2534 nr_free = 0;
2535 ntfs_debug("Exiting.");
2536 return nr_free;
2540 * ntfs_statfs - return information about mounted NTFS volume
2541 * @sb: super block of mounted volume
2542 * @sfs: statfs structure in which to return the information
2544 * Return information about the mounted NTFS volume @sb in the statfs structure
2545 * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is
2546 * called). We interpret the values to be correct of the moment in time at
2547 * which we are called. Most values are variable otherwise and this isn't just
2548 * the free values but the totals as well. For example we can increase the
2549 * total number of file nodes if we run out and we can keep doing this until
2550 * there is no more space on the volume left at all.
2552 * Called from vfs_statfs which is used to handle the statfs, fstatfs, and
2553 * ustat system calls.
2555 * Return 0 on success or -errno on error.
2557 static int ntfs_statfs(struct super_block *sb, struct kstatfs *sfs)
2559 s64 size;
2560 ntfs_volume *vol = NTFS_SB(sb);
2561 ntfs_inode *mft_ni = NTFS_I(vol->mft_ino);
2562 pgoff_t max_index;
2563 unsigned long flags;
2565 ntfs_debug("Entering.");
2566 /* Type of filesystem. */
2567 sfs->f_type = NTFS_SB_MAGIC;
2568 /* Optimal transfer block size. */
2569 sfs->f_bsize = PAGE_CACHE_SIZE;
2571 * Total data blocks in filesystem in units of f_bsize and since
2572 * inodes are also stored in data blocs ($MFT is a file) this is just
2573 * the total clusters.
2575 sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >>
2576 PAGE_CACHE_SHIFT;
2577 /* Free data blocks in filesystem in units of f_bsize. */
2578 size = get_nr_free_clusters(vol) << vol->cluster_size_bits >>
2579 PAGE_CACHE_SHIFT;
2580 if (size < 0LL)
2581 size = 0LL;
2582 /* Free blocks avail to non-superuser, same as above on NTFS. */
2583 sfs->f_bavail = sfs->f_bfree = size;
2584 /* Serialize accesses to the inode bitmap. */
2585 down_read(&vol->mftbmp_lock);
2586 read_lock_irqsave(&mft_ni->size_lock, flags);
2587 size = i_size_read(vol->mft_ino) >> vol->mft_record_size_bits;
2589 * Convert the maximum number of set bits into bytes rounded up, then
2590 * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
2591 * have one full and one partial page max_index = 2.
2593 max_index = ((((mft_ni->initialized_size >> vol->mft_record_size_bits)
2594 + 7) >> 3) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2595 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2596 /* Number of inodes in filesystem (at this point in time). */
2597 sfs->f_files = size;
2598 /* Free inodes in fs (based on current total count). */
2599 sfs->f_ffree = __get_nr_free_mft_records(vol, size, max_index);
2600 up_read(&vol->mftbmp_lock);
2602 * File system id. This is extremely *nix flavour dependent and even
2603 * within Linux itself all fs do their own thing. I interpret this to
2604 * mean a unique id associated with the mounted fs and not the id
2605 * associated with the filesystem driver, the latter is already given
2606 * by the filesystem type in sfs->f_type. Thus we use the 64-bit
2607 * volume serial number splitting it into two 32-bit parts. We enter
2608 * the least significant 32-bits in f_fsid[0] and the most significant
2609 * 32-bits in f_fsid[1].
2611 sfs->f_fsid.val[0] = vol->serial_no & 0xffffffff;
2612 sfs->f_fsid.val[1] = (vol->serial_no >> 32) & 0xffffffff;
2613 /* Maximum length of filenames. */
2614 sfs->f_namelen = NTFS_MAX_NAME_LEN;
2615 return 0;
2619 * The complete super operations.
2621 static struct super_operations ntfs_sops = {
2622 .alloc_inode = ntfs_alloc_big_inode, /* VFS: Allocate new inode. */
2623 .destroy_inode = ntfs_destroy_big_inode, /* VFS: Deallocate inode. */
2624 .put_inode = ntfs_put_inode, /* VFS: Called just before
2625 the inode reference count
2626 is decreased. */
2627 #ifdef NTFS_RW
2628 //.dirty_inode = NULL, /* VFS: Called from
2629 // __mark_inode_dirty(). */
2630 .write_inode = ntfs_write_inode, /* VFS: Write dirty inode to
2631 disk. */
2632 //.drop_inode = NULL, /* VFS: Called just after the
2633 // inode reference count has
2634 // been decreased to zero.
2635 // NOTE: The inode lock is
2636 // held. See fs/inode.c::
2637 // generic_drop_inode(). */
2638 //.delete_inode = NULL, /* VFS: Delete inode from disk.
2639 // Called when i_count becomes
2640 // 0 and i_nlink is also 0. */
2641 //.write_super = NULL, /* Flush dirty super block to
2642 // disk. */
2643 //.sync_fs = NULL, /* ? */
2644 //.write_super_lockfs = NULL, /* ? */
2645 //.unlockfs = NULL, /* ? */
2646 #endif /* NTFS_RW */
2647 .put_super = ntfs_put_super, /* Syscall: umount. */
2648 .statfs = ntfs_statfs, /* Syscall: statfs */
2649 .remount_fs = ntfs_remount, /* Syscall: mount -o remount. */
2650 .clear_inode = ntfs_clear_big_inode, /* VFS: Called when an inode is
2651 removed from memory. */
2652 //.umount_begin = NULL, /* Forced umount. */
2653 .show_options = ntfs_show_options, /* Show mount options in
2654 proc. */
2658 * ntfs_fill_super - mount an ntfs filesystem
2659 * @sb: super block of ntfs filesystem to mount
2660 * @opt: string containing the mount options
2661 * @silent: silence error output
2663 * ntfs_fill_super() is called by the VFS to mount the device described by @sb
2664 * with the mount otions in @data with the NTFS filesystem.
2666 * If @silent is true, remain silent even if errors are detected. This is used
2667 * during bootup, when the kernel tries to mount the root filesystem with all
2668 * registered filesystems one after the other until one succeeds. This implies
2669 * that all filesystems except the correct one will quite correctly and
2670 * expectedly return an error, but nobody wants to see error messages when in
2671 * fact this is what is supposed to happen.
2673 * NOTE: @sb->s_flags contains the mount options flags.
2675 static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2677 ntfs_volume *vol;
2678 struct buffer_head *bh;
2679 struct inode *tmp_ino;
2680 int result;
2682 ntfs_debug("Entering.");
2683 #ifndef NTFS_RW
2684 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
2685 #endif /* ! NTFS_RW */
2686 /* Allocate a new ntfs_volume and place it in sb->s_fs_info. */
2687 sb->s_fs_info = kmalloc(sizeof(ntfs_volume), GFP_NOFS);
2688 vol = NTFS_SB(sb);
2689 if (!vol) {
2690 if (!silent)
2691 ntfs_error(sb, "Allocation of NTFS volume structure "
2692 "failed. Aborting mount...");
2693 return -ENOMEM;
2695 /* Initialize ntfs_volume structure. */
2696 *vol = (ntfs_volume) {
2697 .sb = sb,
2699 * Default is group and other don't have any access to files or
2700 * directories while owner has full access. Further, files by
2701 * default are not executable but directories are of course
2702 * browseable.
2704 .fmask = 0177,
2705 .dmask = 0077,
2707 init_rwsem(&vol->mftbmp_lock);
2708 init_rwsem(&vol->lcnbmp_lock);
2710 unlock_kernel();
2712 /* By default, enable sparse support. */
2713 NVolSetSparseEnabled(vol);
2715 /* Important to get the mount options dealt with now. */
2716 if (!parse_options(vol, (char*)opt))
2717 goto err_out_now;
2720 * TODO: Fail safety check. In the future we should really be able to
2721 * cope with this being the case, but for now just bail out.
2723 if (bdev_hardsect_size(sb->s_bdev) > NTFS_BLOCK_SIZE) {
2724 if (!silent)
2725 ntfs_error(sb, "Device has unsupported hardsect_size.");
2726 goto err_out_now;
2729 /* Setup the device access block size to NTFS_BLOCK_SIZE. */
2730 if (sb_set_blocksize(sb, NTFS_BLOCK_SIZE) != NTFS_BLOCK_SIZE) {
2731 if (!silent)
2732 ntfs_error(sb, "Unable to set block size.");
2733 goto err_out_now;
2736 /* Get the size of the device in units of NTFS_BLOCK_SIZE bytes. */
2737 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2738 NTFS_BLOCK_SIZE_BITS;
2740 /* Read the boot sector and return unlocked buffer head to it. */
2741 if (!(bh = read_ntfs_boot_sector(sb, silent))) {
2742 if (!silent)
2743 ntfs_error(sb, "Not an NTFS volume.");
2744 goto err_out_now;
2748 * Extract the data from the boot sector and setup the ntfs super block
2749 * using it.
2751 result = parse_ntfs_boot_sector(vol, (NTFS_BOOT_SECTOR*)bh->b_data);
2753 /* Initialize the cluster and mft allocators. */
2754 ntfs_setup_allocators(vol);
2756 brelse(bh);
2758 if (!result) {
2759 if (!silent)
2760 ntfs_error(sb, "Unsupported NTFS filesystem.");
2761 goto err_out_now;
2765 * TODO: When we start coping with sector sizes different from
2766 * NTFS_BLOCK_SIZE, we now probably need to set the blocksize of the
2767 * device (probably to NTFS_BLOCK_SIZE).
2770 /* Setup remaining fields in the super block. */
2771 sb->s_magic = NTFS_SB_MAGIC;
2774 * Ntfs allows 63 bits for the file size, i.e. correct would be:
2775 * sb->s_maxbytes = ~0ULL >> 1;
2776 * But the kernel uses a long as the page cache page index which on
2777 * 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel
2778 * defined to the maximum the page cache page index can cope with
2779 * without overflowing the index or to 2^63 - 1, whichever is smaller.
2781 sb->s_maxbytes = MAX_LFS_FILESIZE;
2783 sb->s_time_gran = 100;
2786 * Now load the metadata required for the page cache and our address
2787 * space operations to function. We do this by setting up a specialised
2788 * read_inode method and then just calling the normal iget() to obtain
2789 * the inode for $MFT which is sufficient to allow our normal inode
2790 * operations and associated address space operations to function.
2792 sb->s_op = &ntfs_sops;
2793 tmp_ino = new_inode(sb);
2794 if (!tmp_ino) {
2795 if (!silent)
2796 ntfs_error(sb, "Failed to load essential metadata.");
2797 goto err_out_now;
2799 tmp_ino->i_ino = FILE_MFT;
2800 insert_inode_hash(tmp_ino);
2801 if (ntfs_read_inode_mount(tmp_ino) < 0) {
2802 if (!silent)
2803 ntfs_error(sb, "Failed to load essential metadata.");
2804 goto iput_tmp_ino_err_out_now;
2806 down(&ntfs_lock);
2808 * The current mount is a compression user if the cluster size is
2809 * less than or equal 4kiB.
2811 if (vol->cluster_size <= 4096 && !ntfs_nr_compression_users++) {
2812 result = allocate_compression_buffers();
2813 if (result) {
2814 ntfs_error(NULL, "Failed to allocate buffers "
2815 "for compression engine.");
2816 ntfs_nr_compression_users--;
2817 up(&ntfs_lock);
2818 goto iput_tmp_ino_err_out_now;
2822 * Generate the global default upcase table if necessary. Also
2823 * temporarily increment the number of upcase users to avoid race
2824 * conditions with concurrent (u)mounts.
2826 if (!default_upcase)
2827 default_upcase = generate_default_upcase();
2828 ntfs_nr_upcase_users++;
2829 up(&ntfs_lock);
2831 * From now on, ignore @silent parameter. If we fail below this line,
2832 * it will be due to a corrupt fs or a system error, so we report it.
2835 * Open the system files with normal access functions and complete
2836 * setting up the ntfs super block.
2838 if (!load_system_files(vol)) {
2839 ntfs_error(sb, "Failed to load system files.");
2840 goto unl_upcase_iput_tmp_ino_err_out_now;
2842 if ((sb->s_root = d_alloc_root(vol->root_ino))) {
2843 /* We increment i_count simulating an ntfs_iget(). */
2844 atomic_inc(&vol->root_ino->i_count);
2845 ntfs_debug("Exiting, status successful.");
2846 /* Release the default upcase if it has no users. */
2847 down(&ntfs_lock);
2848 if (!--ntfs_nr_upcase_users && default_upcase) {
2849 ntfs_free(default_upcase);
2850 default_upcase = NULL;
2852 up(&ntfs_lock);
2853 sb->s_export_op = &ntfs_export_ops;
2854 lock_kernel();
2855 return 0;
2857 ntfs_error(sb, "Failed to allocate root directory.");
2858 /* Clean up after the successful load_system_files() call from above. */
2859 // TODO: Use ntfs_put_super() instead of repeating all this code...
2860 // FIXME: Should mark the volume clean as the error is most likely
2861 // -ENOMEM.
2862 iput(vol->vol_ino);
2863 vol->vol_ino = NULL;
2864 /* NTFS 3.0+ specific clean up. */
2865 if (vol->major_ver >= 3) {
2866 #ifdef NTFS_RW
2867 if (vol->usnjrnl_j_ino) {
2868 iput(vol->usnjrnl_j_ino);
2869 vol->usnjrnl_j_ino = NULL;
2871 if (vol->usnjrnl_max_ino) {
2872 iput(vol->usnjrnl_max_ino);
2873 vol->usnjrnl_max_ino = NULL;
2875 if (vol->usnjrnl_ino) {
2876 iput(vol->usnjrnl_ino);
2877 vol->usnjrnl_ino = NULL;
2879 if (vol->quota_q_ino) {
2880 iput(vol->quota_q_ino);
2881 vol->quota_q_ino = NULL;
2883 if (vol->quota_ino) {
2884 iput(vol->quota_ino);
2885 vol->quota_ino = NULL;
2887 #endif /* NTFS_RW */
2888 if (vol->extend_ino) {
2889 iput(vol->extend_ino);
2890 vol->extend_ino = NULL;
2892 if (vol->secure_ino) {
2893 iput(vol->secure_ino);
2894 vol->secure_ino = NULL;
2897 iput(vol->root_ino);
2898 vol->root_ino = NULL;
2899 iput(vol->lcnbmp_ino);
2900 vol->lcnbmp_ino = NULL;
2901 iput(vol->mftbmp_ino);
2902 vol->mftbmp_ino = NULL;
2903 #ifdef NTFS_RW
2904 if (vol->logfile_ino) {
2905 iput(vol->logfile_ino);
2906 vol->logfile_ino = NULL;
2908 if (vol->mftmirr_ino) {
2909 iput(vol->mftmirr_ino);
2910 vol->mftmirr_ino = NULL;
2912 #endif /* NTFS_RW */
2913 /* Throw away the table of attribute definitions. */
2914 vol->attrdef_size = 0;
2915 if (vol->attrdef) {
2916 ntfs_free(vol->attrdef);
2917 vol->attrdef = NULL;
2919 vol->upcase_len = 0;
2920 down(&ntfs_lock);
2921 if (vol->upcase == default_upcase) {
2922 ntfs_nr_upcase_users--;
2923 vol->upcase = NULL;
2925 up(&ntfs_lock);
2926 if (vol->upcase) {
2927 ntfs_free(vol->upcase);
2928 vol->upcase = NULL;
2930 if (vol->nls_map) {
2931 unload_nls(vol->nls_map);
2932 vol->nls_map = NULL;
2934 /* Error exit code path. */
2935 unl_upcase_iput_tmp_ino_err_out_now:
2937 * Decrease the number of upcase users and destroy the global default
2938 * upcase table if necessary.
2940 down(&ntfs_lock);
2941 if (!--ntfs_nr_upcase_users && default_upcase) {
2942 ntfs_free(default_upcase);
2943 default_upcase = NULL;
2945 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
2946 free_compression_buffers();
2947 up(&ntfs_lock);
2948 iput_tmp_ino_err_out_now:
2949 iput(tmp_ino);
2950 if (vol->mft_ino && vol->mft_ino != tmp_ino)
2951 iput(vol->mft_ino);
2952 vol->mft_ino = NULL;
2954 * This is needed to get ntfs_clear_extent_inode() called for each
2955 * inode we have ever called ntfs_iget()/iput() on, otherwise we A)
2956 * leak resources and B) a subsequent mount fails automatically due to
2957 * ntfs_iget() never calling down into our ntfs_read_locked_inode()
2958 * method again... FIXME: Do we need to do this twice now because of
2959 * attribute inodes? I think not, so leave as is for now... (AIA)
2961 if (invalidate_inodes(sb)) {
2962 ntfs_error(sb, "Busy inodes left. This is most likely a NTFS "
2963 "driver bug.");
2964 /* Copied from fs/super.c. I just love this message. (-; */
2965 printk("NTFS: Busy inodes after umount. Self-destruct in 5 "
2966 "seconds. Have a nice day...\n");
2968 /* Errors at this stage are irrelevant. */
2969 err_out_now:
2970 lock_kernel();
2971 sb->s_fs_info = NULL;
2972 kfree(vol);
2973 ntfs_debug("Failed, returning -EINVAL.");
2974 return -EINVAL;
2978 * This is a slab cache to optimize allocations and deallocations of Unicode
2979 * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN
2980 * (255) Unicode characters + a terminating NULL Unicode character.
2982 kmem_cache_t *ntfs_name_cache;
2984 /* Slab caches for efficient allocation/deallocation of inodes. */
2985 kmem_cache_t *ntfs_inode_cache;
2986 kmem_cache_t *ntfs_big_inode_cache;
2988 /* Init once constructor for the inode slab cache. */
2989 static void ntfs_big_inode_init_once(void *foo, kmem_cache_t *cachep,
2990 unsigned long flags)
2992 ntfs_inode *ni = (ntfs_inode *)foo;
2994 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
2995 SLAB_CTOR_CONSTRUCTOR)
2996 inode_init_once(VFS_I(ni));
3000 * Slab caches to optimize allocations and deallocations of attribute search
3001 * contexts and index contexts, respectively.
3003 kmem_cache_t *ntfs_attr_ctx_cache;
3004 kmem_cache_t *ntfs_index_ctx_cache;
3006 /* Driver wide semaphore. */
3007 DECLARE_MUTEX(ntfs_lock);
3009 static struct super_block *ntfs_get_sb(struct file_system_type *fs_type,
3010 int flags, const char *dev_name, void *data)
3012 return get_sb_bdev(fs_type, flags, dev_name, data, ntfs_fill_super);
3015 static struct file_system_type ntfs_fs_type = {
3016 .owner = THIS_MODULE,
3017 .name = "ntfs",
3018 .get_sb = ntfs_get_sb,
3019 .kill_sb = kill_block_super,
3020 .fs_flags = FS_REQUIRES_DEV,
3023 /* Stable names for the slab caches. */
3024 static const char ntfs_index_ctx_cache_name[] = "ntfs_index_ctx_cache";
3025 static const char ntfs_attr_ctx_cache_name[] = "ntfs_attr_ctx_cache";
3026 static const char ntfs_name_cache_name[] = "ntfs_name_cache";
3027 static const char ntfs_inode_cache_name[] = "ntfs_inode_cache";
3028 static const char ntfs_big_inode_cache_name[] = "ntfs_big_inode_cache";
3030 static int __init init_ntfs_fs(void)
3032 int err = 0;
3034 /* This may be ugly but it results in pretty output so who cares. (-8 */
3035 printk(KERN_INFO "NTFS driver " NTFS_VERSION " [Flags: R/"
3036 #ifdef NTFS_RW
3038 #else
3040 #endif
3041 #ifdef DEBUG
3042 " DEBUG"
3043 #endif
3044 #ifdef MODULE
3045 " MODULE"
3046 #endif
3047 "].\n");
3049 ntfs_debug("Debug messages are enabled.");
3051 ntfs_index_ctx_cache = kmem_cache_create(ntfs_index_ctx_cache_name,
3052 sizeof(ntfs_index_context), 0 /* offset */,
3053 SLAB_HWCACHE_ALIGN, NULL /* ctor */, NULL /* dtor */);
3054 if (!ntfs_index_ctx_cache) {
3055 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3056 ntfs_index_ctx_cache_name);
3057 goto ictx_err_out;
3059 ntfs_attr_ctx_cache = kmem_cache_create(ntfs_attr_ctx_cache_name,
3060 sizeof(ntfs_attr_search_ctx), 0 /* offset */,
3061 SLAB_HWCACHE_ALIGN, NULL /* ctor */, NULL /* dtor */);
3062 if (!ntfs_attr_ctx_cache) {
3063 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3064 ntfs_attr_ctx_cache_name);
3065 goto actx_err_out;
3068 ntfs_name_cache = kmem_cache_create(ntfs_name_cache_name,
3069 (NTFS_MAX_NAME_LEN+1) * sizeof(ntfschar), 0,
3070 SLAB_HWCACHE_ALIGN, NULL, NULL);
3071 if (!ntfs_name_cache) {
3072 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3073 ntfs_name_cache_name);
3074 goto name_err_out;
3077 ntfs_inode_cache = kmem_cache_create(ntfs_inode_cache_name,
3078 sizeof(ntfs_inode), 0,
3079 SLAB_RECLAIM_ACCOUNT, NULL, NULL);
3080 if (!ntfs_inode_cache) {
3081 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3082 ntfs_inode_cache_name);
3083 goto inode_err_out;
3086 ntfs_big_inode_cache = kmem_cache_create(ntfs_big_inode_cache_name,
3087 sizeof(big_ntfs_inode), 0,
3088 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
3089 ntfs_big_inode_init_once, NULL);
3090 if (!ntfs_big_inode_cache) {
3091 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3092 ntfs_big_inode_cache_name);
3093 goto big_inode_err_out;
3096 /* Register the ntfs sysctls. */
3097 err = ntfs_sysctl(1);
3098 if (err) {
3099 printk(KERN_CRIT "NTFS: Failed to register NTFS sysctls!\n");
3100 goto sysctl_err_out;
3103 err = register_filesystem(&ntfs_fs_type);
3104 if (!err) {
3105 ntfs_debug("NTFS driver registered successfully.");
3106 return 0; /* Success! */
3108 printk(KERN_CRIT "NTFS: Failed to register NTFS filesystem driver!\n");
3110 sysctl_err_out:
3111 kmem_cache_destroy(ntfs_big_inode_cache);
3112 big_inode_err_out:
3113 kmem_cache_destroy(ntfs_inode_cache);
3114 inode_err_out:
3115 kmem_cache_destroy(ntfs_name_cache);
3116 name_err_out:
3117 kmem_cache_destroy(ntfs_attr_ctx_cache);
3118 actx_err_out:
3119 kmem_cache_destroy(ntfs_index_ctx_cache);
3120 ictx_err_out:
3121 if (!err) {
3122 printk(KERN_CRIT "NTFS: Aborting NTFS filesystem driver "
3123 "registration...\n");
3124 err = -ENOMEM;
3126 return err;
3129 static void __exit exit_ntfs_fs(void)
3131 int err = 0;
3133 ntfs_debug("Unregistering NTFS driver.");
3135 unregister_filesystem(&ntfs_fs_type);
3137 if (kmem_cache_destroy(ntfs_big_inode_cache) && (err = 1))
3138 printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
3139 ntfs_big_inode_cache_name);
3140 if (kmem_cache_destroy(ntfs_inode_cache) && (err = 1))
3141 printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
3142 ntfs_inode_cache_name);
3143 if (kmem_cache_destroy(ntfs_name_cache) && (err = 1))
3144 printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
3145 ntfs_name_cache_name);
3146 if (kmem_cache_destroy(ntfs_attr_ctx_cache) && (err = 1))
3147 printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
3148 ntfs_attr_ctx_cache_name);
3149 if (kmem_cache_destroy(ntfs_index_ctx_cache) && (err = 1))
3150 printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
3151 ntfs_index_ctx_cache_name);
3152 if (err)
3153 printk(KERN_CRIT "NTFS: This causes memory to leak! There is "
3154 "probably a BUG in the driver! Please report "
3155 "you saw this message to "
3156 "linux-ntfs-dev@lists.sourceforge.net\n");
3157 /* Unregister the ntfs sysctls. */
3158 ntfs_sysctl(0);
3161 MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>");
3162 MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2005 Anton Altaparmakov");
3163 MODULE_VERSION(NTFS_VERSION);
3164 MODULE_LICENSE("GPL");
3165 #ifdef DEBUG
3166 module_param(debug_msgs, bool, 0);
3167 MODULE_PARM_DESC(debug_msgs, "Enable debug messages.");
3168 #endif
3170 module_init(init_ntfs_fs)
3171 module_exit(exit_ntfs_fs)