Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ntfs / super.c
blob212a3d0f207317f9aa07ec9d45bc770de396c19f
1 /*
2 * super.c - NTFS kernel super block handling. Part of the Linux-NTFS project.
4 * Copyright (c) 2001-2004 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 "dir.h"
38 #include "debug.h"
39 #include "index.h"
40 #include "aops.h"
41 #include "malloc.h"
42 #include "ntfs.h"
44 /* Number of mounted file systems which have compression enabled. */
45 static unsigned long ntfs_nr_compression_users;
47 /* A global default upcase table and a corresponding reference count. */
48 static ntfschar *default_upcase = NULL;
49 static unsigned long ntfs_nr_upcase_users = 0;
51 /* Error constants/strings used in inode.c::ntfs_show_options(). */
52 typedef enum {
53 /* One of these must be present, default is ON_ERRORS_CONTINUE. */
54 ON_ERRORS_PANIC = 0x01,
55 ON_ERRORS_REMOUNT_RO = 0x02,
56 ON_ERRORS_CONTINUE = 0x04,
57 /* Optional, can be combined with any of the above. */
58 ON_ERRORS_RECOVER = 0x10,
59 } ON_ERRORS_ACTIONS;
61 const option_t on_errors_arr[] = {
62 { ON_ERRORS_PANIC, "panic" },
63 { ON_ERRORS_REMOUNT_RO, "remount-ro", },
64 { ON_ERRORS_CONTINUE, "continue", },
65 { ON_ERRORS_RECOVER, "recover" },
66 { 0, NULL }
69 /**
70 * simple_getbool -
72 * Copied from old ntfs driver (which copied from vfat driver).
74 static int simple_getbool(char *s, BOOL *setval)
76 if (s) {
77 if (!strcmp(s, "1") || !strcmp(s, "yes") || !strcmp(s, "true"))
78 *setval = TRUE;
79 else if (!strcmp(s, "0") || !strcmp(s, "no") ||
80 !strcmp(s, "false"))
81 *setval = FALSE;
82 else
83 return 0;
84 } else
85 *setval = TRUE;
86 return 1;
89 /**
90 * parse_options - parse the (re)mount options
91 * @vol: ntfs volume
92 * @opt: string containing the (re)mount options
94 * Parse the recognized options in @opt for the ntfs volume described by @vol.
96 static BOOL parse_options(ntfs_volume *vol, char *opt)
98 char *p, *v, *ov;
99 static char *utf8 = "utf8";
100 int errors = 0, sloppy = 0;
101 uid_t uid = (uid_t)-1;
102 gid_t gid = (gid_t)-1;
103 mode_t fmask = (mode_t)-1, dmask = (mode_t)-1;
104 int mft_zone_multiplier = -1, on_errors = -1;
105 int show_sys_files = -1, case_sensitive = -1;
106 struct nls_table *nls_map = NULL, *old_nls;
108 /* I am lazy... (-8 */
109 #define NTFS_GETOPT_WITH_DEFAULT(option, variable, default_value) \
110 if (!strcmp(p, option)) { \
111 if (!v || !*v) \
112 variable = default_value; \
113 else { \
114 variable = simple_strtoul(ov = v, &v, 0); \
115 if (*v) \
116 goto needs_val; \
119 #define NTFS_GETOPT(option, variable) \
120 if (!strcmp(p, option)) { \
121 if (!v || !*v) \
122 goto needs_arg; \
123 variable = simple_strtoul(ov = v, &v, 0); \
124 if (*v) \
125 goto needs_val; \
127 #define NTFS_GETOPT_BOOL(option, variable) \
128 if (!strcmp(p, option)) { \
129 BOOL val; \
130 if (!simple_getbool(v, &val)) \
131 goto needs_bool; \
132 variable = val; \
134 #define NTFS_GETOPT_OPTIONS_ARRAY(option, variable, opt_array) \
135 if (!strcmp(p, option)) { \
136 int _i; \
137 if (!v || !*v) \
138 goto needs_arg; \
139 ov = v; \
140 if (variable == -1) \
141 variable = 0; \
142 for (_i = 0; opt_array[_i].str && *opt_array[_i].str; _i++) \
143 if (!strcmp(opt_array[_i].str, v)) { \
144 variable |= opt_array[_i].val; \
145 break; \
147 if (!opt_array[_i].str || !*opt_array[_i].str) \
148 goto needs_val; \
150 if (!opt || !*opt)
151 goto no_mount_options;
152 ntfs_debug("Entering with mount options string: %s", opt);
153 while ((p = strsep(&opt, ","))) {
154 if ((v = strchr(p, '=')))
155 *v++ = 0;
156 NTFS_GETOPT("uid", uid)
157 else NTFS_GETOPT("gid", gid)
158 else NTFS_GETOPT("umask", fmask = dmask)
159 else NTFS_GETOPT("fmask", fmask)
160 else NTFS_GETOPT("dmask", dmask)
161 else NTFS_GETOPT("mft_zone_multiplier", mft_zone_multiplier)
162 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, TRUE)
163 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files)
164 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive)
165 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors,
166 on_errors_arr)
167 else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes"))
168 ntfs_warning(vol->sb, "Ignoring obsolete option %s.",
170 else if (!strcmp(p, "nls") || !strcmp(p, "iocharset")) {
171 if (!strcmp(p, "iocharset"))
172 ntfs_warning(vol->sb, "Option iocharset is "
173 "deprecated. Please use "
174 "option nls=<charsetname> in "
175 "the future.");
176 if (!v || !*v)
177 goto needs_arg;
178 use_utf8:
179 old_nls = nls_map;
180 nls_map = load_nls(v);
181 if (!nls_map) {
182 if (!old_nls) {
183 ntfs_error(vol->sb, "NLS character set "
184 "%s not found.", v);
185 return FALSE;
187 ntfs_error(vol->sb, "NLS character set %s not "
188 "found. Using previous one %s.",
189 v, old_nls->charset);
190 nls_map = old_nls;
191 } else /* nls_map */ {
192 if (old_nls)
193 unload_nls(old_nls);
195 } else if (!strcmp(p, "utf8")) {
196 BOOL val = FALSE;
197 ntfs_warning(vol->sb, "Option utf8 is no longer "
198 "supported, using option nls=utf8. Please "
199 "use option nls=utf8 in the future and "
200 "make sure utf8 is compiled either as a "
201 "module or into the kernel.");
202 if (!v || !*v)
203 val = TRUE;
204 else if (!simple_getbool(v, &val))
205 goto needs_bool;
206 if (val) {
207 v = utf8;
208 goto use_utf8;
210 } else {
211 ntfs_error(vol->sb, "Unrecognized mount option %s.", p);
212 if (errors < INT_MAX)
213 errors++;
215 #undef NTFS_GETOPT_OPTIONS_ARRAY
216 #undef NTFS_GETOPT_BOOL
217 #undef NTFS_GETOPT
218 #undef NTFS_GETOPT_WITH_DEFAULT
220 no_mount_options:
221 if (errors && !sloppy)
222 return FALSE;
223 if (sloppy)
224 ntfs_warning(vol->sb, "Sloppy option given. Ignoring "
225 "unrecognized mount option(s) and continuing.");
226 /* Keep this first! */
227 if (on_errors != -1) {
228 if (!on_errors) {
229 ntfs_error(vol->sb, "Invalid errors option argument "
230 "or bug in options parser.");
231 return FALSE;
234 if (nls_map) {
235 if (vol->nls_map && vol->nls_map != nls_map) {
236 ntfs_error(vol->sb, "Cannot change NLS character set "
237 "on remount.");
238 return FALSE;
239 } /* else (!vol->nls_map) */
240 ntfs_debug("Using NLS character set %s.", nls_map->charset);
241 vol->nls_map = nls_map;
242 } else /* (!nls_map) */ {
243 if (!vol->nls_map) {
244 vol->nls_map = load_nls_default();
245 if (!vol->nls_map) {
246 ntfs_error(vol->sb, "Failed to load default "
247 "NLS character set.");
248 return FALSE;
250 ntfs_debug("Using default NLS character set (%s).",
251 vol->nls_map->charset);
254 if (mft_zone_multiplier != -1) {
255 if (vol->mft_zone_multiplier && vol->mft_zone_multiplier !=
256 mft_zone_multiplier) {
257 ntfs_error(vol->sb, "Cannot change mft_zone_multiplier "
258 "on remount.");
259 return FALSE;
261 if (mft_zone_multiplier < 1 || mft_zone_multiplier > 4) {
262 ntfs_error(vol->sb, "Invalid mft_zone_multiplier. "
263 "Using default value, i.e. 1.");
264 mft_zone_multiplier = 1;
266 vol->mft_zone_multiplier = mft_zone_multiplier;
268 if (!vol->mft_zone_multiplier)
269 vol->mft_zone_multiplier = 1;
270 if (on_errors != -1)
271 vol->on_errors = on_errors;
272 if (!vol->on_errors || vol->on_errors == ON_ERRORS_RECOVER)
273 vol->on_errors |= ON_ERRORS_CONTINUE;
274 if (uid != (uid_t)-1)
275 vol->uid = uid;
276 if (gid != (gid_t)-1)
277 vol->gid = gid;
278 if (fmask != (mode_t)-1)
279 vol->fmask = fmask;
280 if (dmask != (mode_t)-1)
281 vol->dmask = dmask;
282 if (show_sys_files != -1) {
283 if (show_sys_files)
284 NVolSetShowSystemFiles(vol);
285 else
286 NVolClearShowSystemFiles(vol);
288 if (case_sensitive != -1) {
289 if (case_sensitive)
290 NVolSetCaseSensitive(vol);
291 else
292 NVolClearCaseSensitive(vol);
294 return TRUE;
295 needs_arg:
296 ntfs_error(vol->sb, "The %s option requires an argument.", p);
297 return FALSE;
298 needs_bool:
299 ntfs_error(vol->sb, "The %s option requires a boolean argument.", p);
300 return FALSE;
301 needs_val:
302 ntfs_error(vol->sb, "Invalid %s option argument: %s", p, ov);
303 return FALSE;
306 #ifdef NTFS_RW
309 * ntfs_write_volume_flags - write new flags to the volume information flags
310 * @vol: ntfs volume on which to modify the flags
311 * @flags: new flags value for the volume information flags
313 * Internal function. You probably want to use ntfs_{set,clear}_volume_flags()
314 * instead (see below).
316 * Replace the volume information flags on the volume @vol with the value
317 * supplied in @flags. Note, this overwrites the volume information flags, so
318 * make sure to combine the flags you want to modify with the old flags and use
319 * the result when calling ntfs_write_volume_flags().
321 * Return 0 on success and -errno on error.
323 static int ntfs_write_volume_flags(ntfs_volume *vol, const VOLUME_FLAGS flags)
325 ntfs_inode *ni = NTFS_I(vol->vol_ino);
326 MFT_RECORD *m;
327 VOLUME_INFORMATION *vi;
328 ntfs_attr_search_ctx *ctx;
329 int err;
331 ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
332 le16_to_cpu(vol->vol_flags), le16_to_cpu(flags));
333 if (vol->vol_flags == flags)
334 goto done;
335 BUG_ON(!ni);
336 m = map_mft_record(ni);
337 if (IS_ERR(m)) {
338 err = PTR_ERR(m);
339 goto err_out;
341 ctx = ntfs_attr_get_search_ctx(ni, m);
342 if (!ctx) {
343 err = -ENOMEM;
344 goto put_unm_err_out;
346 err = ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
347 ctx);
348 if (err)
349 goto put_unm_err_out;
350 vi = (VOLUME_INFORMATION*)((u8*)ctx->attr +
351 le16_to_cpu(ctx->attr->data.resident.value_offset));
352 vol->vol_flags = vi->flags = flags;
353 flush_dcache_mft_record_page(ctx->ntfs_ino);
354 mark_mft_record_dirty(ctx->ntfs_ino);
355 ntfs_attr_put_search_ctx(ctx);
356 unmap_mft_record(ni);
357 done:
358 ntfs_debug("Done.");
359 return 0;
360 put_unm_err_out:
361 if (ctx)
362 ntfs_attr_put_search_ctx(ctx);
363 unmap_mft_record(ni);
364 err_out:
365 ntfs_error(vol->sb, "Failed with error code %i.", -err);
366 return err;
370 * ntfs_set_volume_flags - set bits in the volume information flags
371 * @vol: ntfs volume on which to modify the flags
372 * @flags: flags to set on the volume
374 * Set the bits in @flags in the volume information flags on the volume @vol.
376 * Return 0 on success and -errno on error.
378 static inline int ntfs_set_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
380 flags &= VOLUME_FLAGS_MASK;
381 return ntfs_write_volume_flags(vol, vol->vol_flags | flags);
385 * ntfs_clear_volume_flags - clear bits in the volume information flags
386 * @vol: ntfs volume on which to modify the flags
387 * @flags: flags to clear on the volume
389 * Clear the bits in @flags in the volume information flags on the volume @vol.
391 * Return 0 on success and -errno on error.
393 static inline int ntfs_clear_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
395 flags &= VOLUME_FLAGS_MASK;
396 flags = vol->vol_flags & cpu_to_le16(~le16_to_cpu(flags));
397 return ntfs_write_volume_flags(vol, flags);
400 #endif /* NTFS_RW */
403 * ntfs_remount - change the mount options of a mounted ntfs filesystem
404 * @sb: superblock of mounted ntfs filesystem
405 * @flags: remount flags
406 * @opt: remount options string
408 * Change the mount options of an already mounted ntfs filesystem.
410 * NOTE: The VFS sets the @sb->s_flags remount flags to @flags after
411 * ntfs_remount() returns successfully (i.e. returns 0). Otherwise,
412 * @sb->s_flags are not changed.
414 static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
416 ntfs_volume *vol = NTFS_SB(sb);
418 ntfs_debug("Entering with remount options string: %s", opt);
419 #ifndef NTFS_RW
420 /* For read-only compiled driver, enforce all read-only flags. */
421 *flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
422 #else /* NTFS_RW */
424 * For the read-write compiled driver, if we are remounting read-write,
425 * make sure there are no volume errors and that no unsupported volume
426 * flags are set. Also, empty the logfile journal as it would become
427 * stale as soon as something is written to the volume and mark the
428 * volume dirty so that chkdsk is run if the volume is not umounted
429 * cleanly. Finally, mark the quotas out of date so Windows rescans
430 * the volume on boot and updates them.
432 * When remounting read-only, mark the volume clean if no volume errors
433 * have occured.
435 if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
436 static const char *es = ". Cannot remount read-write.";
438 /* Remounting read-write. */
439 if (NVolErrors(vol)) {
440 ntfs_error(sb, "Volume has errors and is read-only%s",
441 es);
442 return -EROFS;
444 if (vol->vol_flags & VOLUME_IS_DIRTY) {
445 ntfs_error(sb, "Volume is dirty and read-only%s", es);
446 return -EROFS;
448 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
449 ntfs_error(sb, "Volume has unsupported flags set and "
450 "is read-only%s", es);
451 return -EROFS;
453 if (ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
454 ntfs_error(sb, "Failed to set dirty bit in volume "
455 "information flags%s", es);
456 return -EROFS;
458 #if 0
459 // TODO: Enable this code once we start modifying anything that
460 // is different between NTFS 1.2 and 3.x...
461 /* Set NT4 compatibility flag on newer NTFS version volumes. */
462 if ((vol->major_ver > 1)) {
463 if (ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
464 ntfs_error(sb, "Failed to set NT4 "
465 "compatibility flag%s", es);
466 NVolSetErrors(vol);
467 return -EROFS;
470 #endif
471 if (!ntfs_empty_logfile(vol->logfile_ino)) {
472 ntfs_error(sb, "Failed to empty journal $LogFile%s",
473 es);
474 NVolSetErrors(vol);
475 return -EROFS;
477 if (!ntfs_mark_quotas_out_of_date(vol)) {
478 ntfs_error(sb, "Failed to mark quotas out of date%s",
479 es);
480 NVolSetErrors(vol);
481 return -EROFS;
483 } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) {
484 /* Remounting read-only. */
485 if (!NVolErrors(vol)) {
486 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
487 ntfs_warning(sb, "Failed to clear dirty bit "
488 "in volume information "
489 "flags. Run chkdsk.");
492 #endif /* NTFS_RW */
494 // TODO: Deal with *flags.
496 if (!parse_options(vol, opt))
497 return -EINVAL;
498 ntfs_debug("Done.");
499 return 0;
503 * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector
504 * @sb: Super block of the device to which @b belongs.
505 * @b: Boot sector of device @sb to check.
506 * @silent: If TRUE, all output will be silenced.
508 * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot
509 * sector. Returns TRUE if it is valid and FALSE if not.
511 * @sb is only needed for warning/error output, i.e. it can be NULL when silent
512 * is TRUE.
514 static BOOL is_boot_sector_ntfs(const struct super_block *sb,
515 const NTFS_BOOT_SECTOR *b, const BOOL silent)
518 * Check that checksum == sum of u32 values from b to the checksum
519 * field. If checksum is zero, no checking is done.
521 if ((void*)b < (void*)&b->checksum && b->checksum) {
522 le32 *u;
523 u32 i;
525 for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u)
526 i += le32_to_cpup(u);
527 if (le32_to_cpu(b->checksum) != i)
528 goto not_ntfs;
530 /* Check OEMidentifier is "NTFS " */
531 if (b->oem_id != magicNTFS)
532 goto not_ntfs;
533 /* Check bytes per sector value is between 256 and 4096. */
534 if (le16_to_cpu(b->bpb.bytes_per_sector) < 0x100 ||
535 le16_to_cpu(b->bpb.bytes_per_sector) > 0x1000)
536 goto not_ntfs;
537 /* Check sectors per cluster value is valid. */
538 switch (b->bpb.sectors_per_cluster) {
539 case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128:
540 break;
541 default:
542 goto not_ntfs;
544 /* Check the cluster size is not above 65536 bytes. */
545 if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) *
546 b->bpb.sectors_per_cluster > 0x10000)
547 goto not_ntfs;
548 /* Check reserved/unused fields are really zero. */
549 if (le16_to_cpu(b->bpb.reserved_sectors) ||
550 le16_to_cpu(b->bpb.root_entries) ||
551 le16_to_cpu(b->bpb.sectors) ||
552 le16_to_cpu(b->bpb.sectors_per_fat) ||
553 le32_to_cpu(b->bpb.large_sectors) || b->bpb.fats)
554 goto not_ntfs;
555 /* Check clusters per file mft record value is valid. */
556 if ((u8)b->clusters_per_mft_record < 0xe1 ||
557 (u8)b->clusters_per_mft_record > 0xf7)
558 switch (b->clusters_per_mft_record) {
559 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
560 break;
561 default:
562 goto not_ntfs;
564 /* Check clusters per index block value is valid. */
565 if ((u8)b->clusters_per_index_record < 0xe1 ||
566 (u8)b->clusters_per_index_record > 0xf7)
567 switch (b->clusters_per_index_record) {
568 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
569 break;
570 default:
571 goto not_ntfs;
574 * Check for valid end of sector marker. We will work without it, but
575 * many BIOSes will refuse to boot from a bootsector if the magic is
576 * incorrect, so we emit a warning.
578 if (!silent && b->end_of_sector_marker != cpu_to_le16(0xaa55))
579 ntfs_warning(sb, "Invalid end of sector marker.");
580 return TRUE;
581 not_ntfs:
582 return FALSE;
586 * read_ntfs_boot_sector - read the NTFS boot sector of a device
587 * @sb: super block of device to read the boot sector from
588 * @silent: if true, suppress all output
590 * Reads the boot sector from the device and validates it. If that fails, tries
591 * to read the backup boot sector, first from the end of the device a-la NT4 and
592 * later and then from the middle of the device a-la NT3.51 and before.
594 * If a valid boot sector is found but it is not the primary boot sector, we
595 * repair the primary boot sector silently (unless the device is read-only or
596 * the primary boot sector is not accessible).
598 * NOTE: To call this function, @sb must have the fields s_dev, the ntfs super
599 * block (u.ntfs_sb), nr_blocks and the device flags (s_flags) initialized
600 * to their respective values.
602 * Return the unlocked buffer head containing the boot sector or NULL on error.
604 static struct buffer_head *read_ntfs_boot_sector(struct super_block *sb,
605 const int silent)
607 const char *read_err_str = "Unable to read %s boot sector.";
608 struct buffer_head *bh_primary, *bh_backup;
609 long nr_blocks = NTFS_SB(sb)->nr_blocks;
611 /* Try to read primary boot sector. */
612 if ((bh_primary = sb_bread(sb, 0))) {
613 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
614 bh_primary->b_data, silent))
615 return bh_primary;
616 if (!silent)
617 ntfs_error(sb, "Primary boot sector is invalid.");
618 } else if (!silent)
619 ntfs_error(sb, read_err_str, "primary");
620 if (!(NTFS_SB(sb)->on_errors & ON_ERRORS_RECOVER)) {
621 if (bh_primary)
622 brelse(bh_primary);
623 if (!silent)
624 ntfs_error(sb, "Mount option errors=recover not used. "
625 "Aborting without trying to recover.");
626 return NULL;
628 /* Try to read NT4+ backup boot sector. */
629 if ((bh_backup = sb_bread(sb, nr_blocks - 1))) {
630 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
631 bh_backup->b_data, silent))
632 goto hotfix_primary_boot_sector;
633 brelse(bh_backup);
634 } else if (!silent)
635 ntfs_error(sb, read_err_str, "backup");
636 /* Try to read NT3.51- backup boot sector. */
637 if ((bh_backup = sb_bread(sb, nr_blocks >> 1))) {
638 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
639 bh_backup->b_data, silent))
640 goto hotfix_primary_boot_sector;
641 if (!silent)
642 ntfs_error(sb, "Could not find a valid backup boot "
643 "sector.");
644 brelse(bh_backup);
645 } else if (!silent)
646 ntfs_error(sb, read_err_str, "backup");
647 /* We failed. Cleanup and return. */
648 if (bh_primary)
649 brelse(bh_primary);
650 return NULL;
651 hotfix_primary_boot_sector:
652 if (bh_primary) {
654 * If we managed to read sector zero and the volume is not
655 * read-only, copy the found, valid backup boot sector to the
656 * primary boot sector.
658 if (!(sb->s_flags & MS_RDONLY)) {
659 ntfs_warning(sb, "Hot-fix: Recovering invalid primary "
660 "boot sector from backup copy.");
661 memcpy(bh_primary->b_data, bh_backup->b_data,
662 sb->s_blocksize);
663 mark_buffer_dirty(bh_primary);
664 sync_dirty_buffer(bh_primary);
665 if (buffer_uptodate(bh_primary)) {
666 brelse(bh_backup);
667 return bh_primary;
669 ntfs_error(sb, "Hot-fix: Device write error while "
670 "recovering primary boot sector.");
671 } else {
672 ntfs_warning(sb, "Hot-fix: Recovery of primary boot "
673 "sector failed: Read-only mount.");
675 brelse(bh_primary);
677 ntfs_warning(sb, "Using backup boot sector.");
678 return bh_backup;
682 * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol
683 * @vol: volume structure to initialise with data from boot sector
684 * @b: boot sector to parse
686 * Parse the ntfs boot sector @b and store all imporant information therein in
687 * the ntfs super block @vol. Return TRUE on success and FALSE on error.
689 static BOOL parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
691 unsigned int sectors_per_cluster_bits, nr_hidden_sects;
692 int clusters_per_mft_record, clusters_per_index_record;
693 s64 ll;
695 vol->sector_size = le16_to_cpu(b->bpb.bytes_per_sector);
696 vol->sector_size_bits = ffs(vol->sector_size) - 1;
697 ntfs_debug("vol->sector_size = %i (0x%x)", vol->sector_size,
698 vol->sector_size);
699 ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits,
700 vol->sector_size_bits);
701 if (vol->sector_size != vol->sb->s_blocksize)
702 ntfs_warning(vol->sb, "The boot sector indicates a sector size "
703 "different from the device sector size.");
704 ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster);
705 sectors_per_cluster_bits = ffs(b->bpb.sectors_per_cluster) - 1;
706 ntfs_debug("sectors_per_cluster_bits = 0x%x",
707 sectors_per_cluster_bits);
708 nr_hidden_sects = le32_to_cpu(b->bpb.hidden_sectors);
709 ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects);
710 vol->cluster_size = vol->sector_size << sectors_per_cluster_bits;
711 vol->cluster_size_mask = vol->cluster_size - 1;
712 vol->cluster_size_bits = ffs(vol->cluster_size) - 1;
713 ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size,
714 vol->cluster_size);
715 ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask);
716 ntfs_debug("vol->cluster_size_bits = %i (0x%x)",
717 vol->cluster_size_bits, vol->cluster_size_bits);
718 if (vol->sector_size > vol->cluster_size) {
719 ntfs_error(vol->sb, "Sector sizes above the cluster size are "
720 "not supported. Sorry.");
721 return FALSE;
723 if (vol->sb->s_blocksize > vol->cluster_size) {
724 ntfs_error(vol->sb, "Cluster sizes smaller than the device "
725 "sector size are not supported. Sorry.");
726 return FALSE;
728 clusters_per_mft_record = b->clusters_per_mft_record;
729 ntfs_debug("clusters_per_mft_record = %i (0x%x)",
730 clusters_per_mft_record, clusters_per_mft_record);
731 if (clusters_per_mft_record > 0)
732 vol->mft_record_size = vol->cluster_size <<
733 (ffs(clusters_per_mft_record) - 1);
734 else
736 * When mft_record_size < cluster_size, clusters_per_mft_record
737 * = -log2(mft_record_size) bytes. mft_record_size normaly is
738 * 1024 bytes, which is encoded as 0xF6 (-10 in decimal).
740 vol->mft_record_size = 1 << -clusters_per_mft_record;
741 vol->mft_record_size_mask = vol->mft_record_size - 1;
742 vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1;
743 ntfs_debug("vol->mft_record_size = %i (0x%x)", vol->mft_record_size,
744 vol->mft_record_size);
745 ntfs_debug("vol->mft_record_size_mask = 0x%x",
746 vol->mft_record_size_mask);
747 ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
748 vol->mft_record_size_bits, vol->mft_record_size_bits);
750 * We cannot support mft record sizes above the PAGE_CACHE_SIZE since
751 * we store $MFT/$DATA, the table of mft records in the page cache.
753 if (vol->mft_record_size > PAGE_CACHE_SIZE) {
754 ntfs_error(vol->sb, "Mft record size %i (0x%x) exceeds the "
755 "page cache size on your system %lu (0x%lx). "
756 "This is not supported. Sorry.",
757 vol->mft_record_size, vol->mft_record_size,
758 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE);
759 return FALSE;
761 clusters_per_index_record = b->clusters_per_index_record;
762 ntfs_debug("clusters_per_index_record = %i (0x%x)",
763 clusters_per_index_record, clusters_per_index_record);
764 if (clusters_per_index_record > 0)
765 vol->index_record_size = vol->cluster_size <<
766 (ffs(clusters_per_index_record) - 1);
767 else
769 * When index_record_size < cluster_size,
770 * clusters_per_index_record = -log2(index_record_size) bytes.
771 * index_record_size normaly equals 4096 bytes, which is
772 * encoded as 0xF4 (-12 in decimal).
774 vol->index_record_size = 1 << -clusters_per_index_record;
775 vol->index_record_size_mask = vol->index_record_size - 1;
776 vol->index_record_size_bits = ffs(vol->index_record_size) - 1;
777 ntfs_debug("vol->index_record_size = %i (0x%x)",
778 vol->index_record_size, vol->index_record_size);
779 ntfs_debug("vol->index_record_size_mask = 0x%x",
780 vol->index_record_size_mask);
781 ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
782 vol->index_record_size_bits,
783 vol->index_record_size_bits);
785 * Get the size of the volume in clusters and check for 64-bit-ness.
786 * Windows currently only uses 32 bits to save the clusters so we do
787 * the same as it is much faster on 32-bit CPUs.
789 ll = sle64_to_cpu(b->number_of_sectors) >> sectors_per_cluster_bits;
790 if ((u64)ll >= 1ULL << 32) {
791 ntfs_error(vol->sb, "Cannot handle 64-bit clusters. Sorry.");
792 return FALSE;
794 vol->nr_clusters = ll;
795 ntfs_debug("vol->nr_clusters = 0x%llx", (long long)vol->nr_clusters);
797 * On an architecture where unsigned long is 32-bits, we restrict the
798 * volume size to 2TiB (2^41). On a 64-bit architecture, the compiler
799 * will hopefully optimize the whole check away.
801 if (sizeof(unsigned long) < 8) {
802 if ((ll << vol->cluster_size_bits) >= (1ULL << 41)) {
803 ntfs_error(vol->sb, "Volume size (%lluTiB) is too "
804 "large for this architecture. "
805 "Maximum supported is 2TiB. Sorry.",
806 (unsigned long long)ll >> (40 -
807 vol->cluster_size_bits));
808 return FALSE;
811 ll = sle64_to_cpu(b->mft_lcn);
812 if (ll >= vol->nr_clusters) {
813 ntfs_error(vol->sb, "MFT LCN is beyond end of volume. Weird.");
814 return FALSE;
816 vol->mft_lcn = ll;
817 ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol->mft_lcn);
818 ll = sle64_to_cpu(b->mftmirr_lcn);
819 if (ll >= vol->nr_clusters) {
820 ntfs_error(vol->sb, "MFTMirr LCN is beyond end of volume. "
821 "Weird.");
822 return FALSE;
824 vol->mftmirr_lcn = ll;
825 ntfs_debug("vol->mftmirr_lcn = 0x%llx", (long long)vol->mftmirr_lcn);
826 #ifdef NTFS_RW
828 * Work out the size of the mft mirror in number of mft records. If the
829 * cluster size is less than or equal to the size taken by four mft
830 * records, the mft mirror stores the first four mft records. If the
831 * cluster size is bigger than the size taken by four mft records, the
832 * mft mirror contains as many mft records as will fit into one
833 * cluster.
835 if (vol->cluster_size <= (4 << vol->mft_record_size_bits))
836 vol->mftmirr_size = 4;
837 else
838 vol->mftmirr_size = vol->cluster_size >>
839 vol->mft_record_size_bits;
840 ntfs_debug("vol->mftmirr_size = %i", vol->mftmirr_size);
841 #endif /* NTFS_RW */
842 vol->serial_no = le64_to_cpu(b->volume_serial_number);
843 ntfs_debug("vol->serial_no = 0x%llx",
844 (unsigned long long)vol->serial_no);
845 return TRUE;
849 * ntfs_setup_allocators - initialize the cluster and mft allocators
850 * @vol: volume structure for which to setup the allocators
852 * Setup the cluster (lcn) and mft allocators to the starting values.
854 static void ntfs_setup_allocators(ntfs_volume *vol)
856 #ifdef NTFS_RW
857 LCN mft_zone_size, mft_lcn;
858 #endif /* NTFS_RW */
860 ntfs_debug("vol->mft_zone_multiplier = 0x%x",
861 vol->mft_zone_multiplier);
862 #ifdef NTFS_RW
863 /* Determine the size of the MFT zone. */
864 mft_zone_size = vol->nr_clusters;
865 switch (vol->mft_zone_multiplier) { /* % of volume size in clusters */
866 case 4:
867 mft_zone_size >>= 1; /* 50% */
868 break;
869 case 3:
870 mft_zone_size = (mft_zone_size +
871 (mft_zone_size >> 1)) >> 2; /* 37.5% */
872 break;
873 case 2:
874 mft_zone_size >>= 2; /* 25% */
875 break;
876 /* case 1: */
877 default:
878 mft_zone_size >>= 3; /* 12.5% */
879 break;
881 /* Setup the mft zone. */
882 vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn;
883 ntfs_debug("vol->mft_zone_pos = 0x%llx",
884 (unsigned long long)vol->mft_zone_pos);
886 * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
887 * source) and if the actual mft_lcn is in the expected place or even
888 * further to the front of the volume, extend the mft_zone to cover the
889 * beginning of the volume as well. This is in order to protect the
890 * area reserved for the mft bitmap as well within the mft_zone itself.
891 * On non-standard volumes we do not protect it as the overhead would
892 * be higher than the speed increase we would get by doing it.
894 mft_lcn = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size;
895 if (mft_lcn * vol->cluster_size < 16 * 1024)
896 mft_lcn = (16 * 1024 + vol->cluster_size - 1) /
897 vol->cluster_size;
898 if (vol->mft_zone_start <= mft_lcn)
899 vol->mft_zone_start = 0;
900 ntfs_debug("vol->mft_zone_start = 0x%llx",
901 (unsigned long long)vol->mft_zone_start);
903 * Need to cap the mft zone on non-standard volumes so that it does
904 * not point outside the boundaries of the volume. We do this by
905 * halving the zone size until we are inside the volume.
907 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
908 while (vol->mft_zone_end >= vol->nr_clusters) {
909 mft_zone_size >>= 1;
910 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
912 ntfs_debug("vol->mft_zone_end = 0x%llx",
913 (unsigned long long)vol->mft_zone_end);
915 * Set the current position within each data zone to the start of the
916 * respective zone.
918 vol->data1_zone_pos = vol->mft_zone_end;
919 ntfs_debug("vol->data1_zone_pos = 0x%llx",
920 (unsigned long long)vol->data1_zone_pos);
921 vol->data2_zone_pos = 0;
922 ntfs_debug("vol->data2_zone_pos = 0x%llx",
923 (unsigned long long)vol->data2_zone_pos);
925 /* Set the mft data allocation position to mft record 24. */
926 vol->mft_data_pos = 24;
927 ntfs_debug("vol->mft_data_pos = 0x%llx",
928 (unsigned long long)vol->mft_data_pos);
929 #endif /* NTFS_RW */
932 #ifdef NTFS_RW
935 * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume
936 * @vol: ntfs super block describing device whose mft mirror to load
938 * Return TRUE on success or FALSE on error.
940 static BOOL load_and_init_mft_mirror(ntfs_volume *vol)
942 struct inode *tmp_ino;
943 ntfs_inode *tmp_ni;
945 ntfs_debug("Entering.");
946 /* Get mft mirror inode. */
947 tmp_ino = ntfs_iget(vol->sb, FILE_MFTMirr);
948 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
949 if (!IS_ERR(tmp_ino))
950 iput(tmp_ino);
951 /* Caller will display error message. */
952 return FALSE;
955 * Re-initialize some specifics about $MFTMirr's inode as
956 * ntfs_read_inode() will have set up the default ones.
958 /* Set uid and gid to root. */
959 tmp_ino->i_uid = tmp_ino->i_gid = 0;
960 /* Regular file. No access for anyone. */
961 tmp_ino->i_mode = S_IFREG;
962 /* No VFS initiated operations allowed for $MFTMirr. */
963 tmp_ino->i_op = &ntfs_empty_inode_ops;
964 tmp_ino->i_fop = &ntfs_empty_file_ops;
965 /* Put in our special address space operations. */
966 tmp_ino->i_mapping->a_ops = &ntfs_mst_aops;
967 tmp_ni = NTFS_I(tmp_ino);
968 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
969 NInoSetMstProtected(tmp_ni);
971 * Set up our little cheat allowing us to reuse the async read io
972 * completion handler for directories.
974 tmp_ni->itype.index.block_size = vol->mft_record_size;
975 tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits;
976 vol->mftmirr_ino = tmp_ino;
977 ntfs_debug("Done.");
978 return TRUE;
982 * check_mft_mirror - compare contents of the mft mirror with the mft
983 * @vol: ntfs super block describing device whose mft mirror to check
985 * Return TRUE on success or FALSE on error.
987 * Note, this function also results in the mft mirror runlist being completely
988 * mapped into memory. The mft mirror write code requires this and will BUG()
989 * should it find an unmapped runlist element.
991 static BOOL check_mft_mirror(ntfs_volume *vol)
993 unsigned long index;
994 struct super_block *sb = vol->sb;
995 ntfs_inode *mirr_ni;
996 struct page *mft_page, *mirr_page;
997 u8 *kmft, *kmirr;
998 runlist_element *rl, rl2[2];
999 int mrecs_per_page, i;
1001 ntfs_debug("Entering.");
1002 /* Compare contents of $MFT and $MFTMirr. */
1003 mrecs_per_page = PAGE_CACHE_SIZE / vol->mft_record_size;
1004 BUG_ON(!mrecs_per_page);
1005 BUG_ON(!vol->mftmirr_size);
1006 mft_page = mirr_page = NULL;
1007 kmft = kmirr = NULL;
1008 index = i = 0;
1009 do {
1010 u32 bytes;
1012 /* Switch pages if necessary. */
1013 if (!(i % mrecs_per_page)) {
1014 if (index) {
1015 ntfs_unmap_page(mft_page);
1016 ntfs_unmap_page(mirr_page);
1018 /* Get the $MFT page. */
1019 mft_page = ntfs_map_page(vol->mft_ino->i_mapping,
1020 index);
1021 if (IS_ERR(mft_page)) {
1022 ntfs_error(sb, "Failed to read $MFT.");
1023 return FALSE;
1025 kmft = page_address(mft_page);
1026 /* Get the $MFTMirr page. */
1027 mirr_page = ntfs_map_page(vol->mftmirr_ino->i_mapping,
1028 index);
1029 if (IS_ERR(mirr_page)) {
1030 ntfs_error(sb, "Failed to read $MFTMirr.");
1031 goto mft_unmap_out;
1033 kmirr = page_address(mirr_page);
1034 ++index;
1036 /* Make sure the record is ok. */
1037 if (ntfs_is_baad_recordp((le32*)kmft)) {
1038 ntfs_error(sb, "Incomplete multi sector transfer "
1039 "detected in mft record %i.", i);
1040 mm_unmap_out:
1041 ntfs_unmap_page(mirr_page);
1042 mft_unmap_out:
1043 ntfs_unmap_page(mft_page);
1044 return FALSE;
1046 if (ntfs_is_baad_recordp((le32*)kmirr)) {
1047 ntfs_error(sb, "Incomplete multi sector transfer "
1048 "detected in mft mirror record %i.", i);
1049 goto mm_unmap_out;
1051 /* Get the amount of data in the current record. */
1052 bytes = le32_to_cpu(((MFT_RECORD*)kmft)->bytes_in_use);
1053 if (!bytes || bytes > vol->mft_record_size) {
1054 bytes = le32_to_cpu(((MFT_RECORD*)kmirr)->bytes_in_use);
1055 if (!bytes || bytes > vol->mft_record_size)
1056 bytes = vol->mft_record_size;
1058 /* Compare the two records. */
1059 if (memcmp(kmft, kmirr, bytes)) {
1060 ntfs_error(sb, "$MFT and $MFTMirr (record %i) do not "
1061 "match. Run ntfsfix or chkdsk.", i);
1062 goto mm_unmap_out;
1064 kmft += vol->mft_record_size;
1065 kmirr += vol->mft_record_size;
1066 } while (++i < vol->mftmirr_size);
1067 /* Release the last pages. */
1068 ntfs_unmap_page(mft_page);
1069 ntfs_unmap_page(mirr_page);
1071 /* Construct the mft mirror runlist by hand. */
1072 rl2[0].vcn = 0;
1073 rl2[0].lcn = vol->mftmirr_lcn;
1074 rl2[0].length = (vol->mftmirr_size * vol->mft_record_size +
1075 vol->cluster_size - 1) / vol->cluster_size;
1076 rl2[1].vcn = rl2[0].length;
1077 rl2[1].lcn = LCN_ENOENT;
1078 rl2[1].length = 0;
1080 * Because we have just read all of the mft mirror, we know we have
1081 * mapped the full runlist for it.
1083 mirr_ni = NTFS_I(vol->mftmirr_ino);
1084 down_read(&mirr_ni->runlist.lock);
1085 rl = mirr_ni->runlist.rl;
1086 /* Compare the two runlists. They must be identical. */
1087 i = 0;
1088 do {
1089 if (rl2[i].vcn != rl[i].vcn || rl2[i].lcn != rl[i].lcn ||
1090 rl2[i].length != rl[i].length) {
1091 ntfs_error(sb, "$MFTMirr location mismatch. "
1092 "Run chkdsk.");
1093 up_read(&mirr_ni->runlist.lock);
1094 return FALSE;
1096 } while (rl2[i++].length);
1097 up_read(&mirr_ni->runlist.lock);
1098 ntfs_debug("Done.");
1099 return TRUE;
1103 * load_and_check_logfile - load and check the logfile inode for a volume
1104 * @vol: ntfs super block describing device whose logfile to load
1106 * Return TRUE on success or FALSE on error.
1108 static BOOL load_and_check_logfile(ntfs_volume *vol)
1110 struct inode *tmp_ino;
1112 ntfs_debug("Entering.");
1113 tmp_ino = ntfs_iget(vol->sb, FILE_LogFile);
1114 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1115 if (!IS_ERR(tmp_ino))
1116 iput(tmp_ino);
1117 /* Caller will display error message. */
1118 return FALSE;
1120 if (!ntfs_check_logfile(tmp_ino)) {
1121 iput(tmp_ino);
1122 /* ntfs_check_logfile() will have displayed error output. */
1123 return FALSE;
1125 vol->logfile_ino = tmp_ino;
1126 ntfs_debug("Done.");
1127 return TRUE;
1131 * load_and_init_quota - load and setup the quota file for a volume if present
1132 * @vol: ntfs super block describing device whose quota file to load
1134 * Return TRUE on success or FALSE on error. If $Quota is not present, we
1135 * leave vol->quota_ino as NULL and return success.
1137 static BOOL load_and_init_quota(ntfs_volume *vol)
1139 MFT_REF mref;
1140 struct inode *tmp_ino;
1141 ntfs_name *name = NULL;
1142 static const ntfschar Quota[7] = { const_cpu_to_le16('$'),
1143 const_cpu_to_le16('Q'), const_cpu_to_le16('u'),
1144 const_cpu_to_le16('o'), const_cpu_to_le16('t'),
1145 const_cpu_to_le16('a'), 0 };
1146 static ntfschar Q[3] = { const_cpu_to_le16('$'),
1147 const_cpu_to_le16('Q'), 0 };
1149 ntfs_debug("Entering.");
1151 * Find the inode number for the quota file by looking up the filename
1152 * $Quota in the extended system files directory $Extend.
1154 down(&vol->extend_ino->i_sem);
1155 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), Quota, 6,
1156 &name);
1157 up(&vol->extend_ino->i_sem);
1158 if (IS_ERR_MREF(mref)) {
1160 * If the file does not exist, quotas are disabled and have
1161 * never been enabled on this volume, just return success.
1163 if (MREF_ERR(mref) == -ENOENT) {
1164 ntfs_debug("$Quota not present. Volume does not have "
1165 "quotas enabled.");
1167 * No need to try to set quotas out of date if they are
1168 * not enabled.
1170 NVolSetQuotaOutOfDate(vol);
1171 return TRUE;
1173 /* A real error occured. */
1174 ntfs_error(vol->sb, "Failed to find inode number for $Quota.");
1175 return FALSE;
1177 /* We do not care for the type of match that was found. */
1178 if (name)
1179 kfree(name);
1180 /* Get the inode. */
1181 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1182 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1183 if (!IS_ERR(tmp_ino))
1184 iput(tmp_ino);
1185 ntfs_error(vol->sb, "Failed to load $Quota.");
1186 return FALSE;
1188 vol->quota_ino = tmp_ino;
1189 /* Get the $Q index allocation attribute. */
1190 tmp_ino = ntfs_index_iget(vol->quota_ino, Q, 2);
1191 if (IS_ERR(tmp_ino)) {
1192 ntfs_error(vol->sb, "Failed to load $Quota/$Q index.");
1193 return FALSE;
1195 vol->quota_q_ino = tmp_ino;
1196 ntfs_debug("Done.");
1197 return TRUE;
1201 * load_and_init_attrdef - load the attribute definitions table for a volume
1202 * @vol: ntfs super block describing device whose attrdef to load
1204 * Return TRUE on success or FALSE on error.
1206 static BOOL load_and_init_attrdef(ntfs_volume *vol)
1208 struct super_block *sb = vol->sb;
1209 struct inode *ino;
1210 struct page *page;
1211 unsigned long index, max_index;
1212 unsigned int size;
1214 ntfs_debug("Entering.");
1215 /* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */
1216 ino = ntfs_iget(sb, FILE_AttrDef);
1217 if (IS_ERR(ino) || is_bad_inode(ino)) {
1218 if (!IS_ERR(ino))
1219 iput(ino);
1220 goto failed;
1222 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
1223 if (!ino->i_size || ino->i_size > 0x7fffffff)
1224 goto iput_failed;
1225 vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(ino->i_size);
1226 if (!vol->attrdef)
1227 goto iput_failed;
1228 index = 0;
1229 max_index = ino->i_size >> PAGE_CACHE_SHIFT;
1230 size = PAGE_CACHE_SIZE;
1231 while (index < max_index) {
1232 /* Read the attrdef table and copy it into the linear buffer. */
1233 read_partial_attrdef_page:
1234 page = ntfs_map_page(ino->i_mapping, index);
1235 if (IS_ERR(page))
1236 goto free_iput_failed;
1237 memcpy((u8*)vol->attrdef + (index++ << PAGE_CACHE_SHIFT),
1238 page_address(page), size);
1239 ntfs_unmap_page(page);
1241 if (size == PAGE_CACHE_SIZE) {
1242 size = ino->i_size & ~PAGE_CACHE_MASK;
1243 if (size)
1244 goto read_partial_attrdef_page;
1246 vol->attrdef_size = ino->i_size;
1247 ntfs_debug("Read %llu bytes from $AttrDef.", ino->i_size);
1248 iput(ino);
1249 return TRUE;
1250 free_iput_failed:
1251 ntfs_free(vol->attrdef);
1252 vol->attrdef = NULL;
1253 iput_failed:
1254 iput(ino);
1255 failed:
1256 ntfs_error(sb, "Failed to initialize attribute definition table.");
1257 return FALSE;
1260 #endif /* NTFS_RW */
1263 * load_and_init_upcase - load the upcase table for an ntfs volume
1264 * @vol: ntfs super block describing device whose upcase to load
1266 * Return TRUE on success or FALSE on error.
1268 static BOOL load_and_init_upcase(ntfs_volume *vol)
1270 struct super_block *sb = vol->sb;
1271 struct inode *ino;
1272 struct page *page;
1273 unsigned long index, max_index;
1274 unsigned int size;
1275 int i, max;
1277 ntfs_debug("Entering.");
1278 /* Read upcase table and setup vol->upcase and vol->upcase_len. */
1279 ino = ntfs_iget(sb, FILE_UpCase);
1280 if (IS_ERR(ino) || is_bad_inode(ino)) {
1281 if (!IS_ERR(ino))
1282 iput(ino);
1283 goto upcase_failed;
1286 * The upcase size must not be above 64k Unicode characters, must not
1287 * be zero and must be a multiple of sizeof(ntfschar).
1289 if (!ino->i_size || ino->i_size & (sizeof(ntfschar) - 1) ||
1290 ino->i_size > 64ULL * 1024 * sizeof(ntfschar))
1291 goto iput_upcase_failed;
1292 vol->upcase = (ntfschar*)ntfs_malloc_nofs(ino->i_size);
1293 if (!vol->upcase)
1294 goto iput_upcase_failed;
1295 index = 0;
1296 max_index = ino->i_size >> PAGE_CACHE_SHIFT;
1297 size = PAGE_CACHE_SIZE;
1298 while (index < max_index) {
1299 /* Read the upcase table and copy it into the linear buffer. */
1300 read_partial_upcase_page:
1301 page = ntfs_map_page(ino->i_mapping, index);
1302 if (IS_ERR(page))
1303 goto iput_upcase_failed;
1304 memcpy((char*)vol->upcase + (index++ << PAGE_CACHE_SHIFT),
1305 page_address(page), size);
1306 ntfs_unmap_page(page);
1308 if (size == PAGE_CACHE_SIZE) {
1309 size = ino->i_size & ~PAGE_CACHE_MASK;
1310 if (size)
1311 goto read_partial_upcase_page;
1313 vol->upcase_len = ino->i_size >> UCHAR_T_SIZE_BITS;
1314 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
1315 ino->i_size, 64 * 1024 * sizeof(ntfschar));
1316 iput(ino);
1317 down(&ntfs_lock);
1318 if (!default_upcase) {
1319 ntfs_debug("Using volume specified $UpCase since default is "
1320 "not present.");
1321 up(&ntfs_lock);
1322 return TRUE;
1324 max = default_upcase_len;
1325 if (max > vol->upcase_len)
1326 max = vol->upcase_len;
1327 for (i = 0; i < max; i++)
1328 if (vol->upcase[i] != default_upcase[i])
1329 break;
1330 if (i == max) {
1331 ntfs_free(vol->upcase);
1332 vol->upcase = default_upcase;
1333 vol->upcase_len = max;
1334 ntfs_nr_upcase_users++;
1335 up(&ntfs_lock);
1336 ntfs_debug("Volume specified $UpCase matches default. Using "
1337 "default.");
1338 return TRUE;
1340 up(&ntfs_lock);
1341 ntfs_debug("Using volume specified $UpCase since it does not match "
1342 "the default.");
1343 return TRUE;
1344 iput_upcase_failed:
1345 iput(ino);
1346 ntfs_free(vol->upcase);
1347 vol->upcase = NULL;
1348 upcase_failed:
1349 down(&ntfs_lock);
1350 if (default_upcase) {
1351 vol->upcase = default_upcase;
1352 vol->upcase_len = default_upcase_len;
1353 ntfs_nr_upcase_users++;
1354 up(&ntfs_lock);
1355 ntfs_error(sb, "Failed to load $UpCase from the volume. Using "
1356 "default.");
1357 return TRUE;
1359 up(&ntfs_lock);
1360 ntfs_error(sb, "Failed to initialize upcase table.");
1361 return FALSE;
1365 * load_system_files - open the system files using normal functions
1366 * @vol: ntfs super block describing device whose system files to load
1368 * Open the system files with normal access functions and complete setting up
1369 * the ntfs super block @vol.
1371 * Return TRUE on success or FALSE on error.
1373 static BOOL load_system_files(ntfs_volume *vol)
1375 struct super_block *sb = vol->sb;
1376 MFT_RECORD *m;
1377 VOLUME_INFORMATION *vi;
1378 ntfs_attr_search_ctx *ctx;
1380 ntfs_debug("Entering.");
1381 #ifdef NTFS_RW
1382 /* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */
1383 if (!load_and_init_mft_mirror(vol) || !check_mft_mirror(vol)) {
1384 static const char *es1 = "Failed to load $MFTMirr";
1385 static const char *es2 = "$MFTMirr does not match $MFT";
1386 static const char *es3 = ". Run ntfsfix and/or chkdsk.";
1388 /* If a read-write mount, convert it to a read-only mount. */
1389 if (!(sb->s_flags & MS_RDONLY)) {
1390 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1391 ON_ERRORS_CONTINUE))) {
1392 ntfs_error(sb, "%s and neither on_errors="
1393 "continue nor on_errors="
1394 "remount-ro was specified%s",
1395 !vol->mftmirr_ino ? es1 : es2,
1396 es3);
1397 goto iput_mirr_err_out;
1399 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1400 ntfs_error(sb, "%s. Mounting read-only%s",
1401 !vol->mftmirr_ino ? es1 : es2, es3);
1402 } else
1403 ntfs_warning(sb, "%s. Will not be able to remount "
1404 "read-write%s",
1405 !vol->mftmirr_ino ? es1 : es2, es3);
1406 /* This will prevent a read-write remount. */
1407 NVolSetErrors(vol);
1409 #endif /* NTFS_RW */
1410 /* Get mft bitmap attribute inode. */
1411 vol->mftbmp_ino = ntfs_attr_iget(vol->mft_ino, AT_BITMAP, NULL, 0);
1412 if (IS_ERR(vol->mftbmp_ino)) {
1413 ntfs_error(sb, "Failed to load $MFT/$BITMAP attribute.");
1414 goto iput_mirr_err_out;
1416 /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */
1417 if (!load_and_init_upcase(vol))
1418 goto iput_mftbmp_err_out;
1419 #ifdef NTFS_RW
1421 * Read attribute definitions table and setup @vol->attrdef and
1422 * @vol->attrdef_size.
1424 if (!load_and_init_attrdef(vol))
1425 goto iput_upcase_err_out;
1426 #endif /* NTFS_RW */
1428 * Get the cluster allocation bitmap inode and verify the size, no
1429 * need for any locking at this stage as we are already running
1430 * exclusively as we are mount in progress task.
1432 vol->lcnbmp_ino = ntfs_iget(sb, FILE_Bitmap);
1433 if (IS_ERR(vol->lcnbmp_ino) || is_bad_inode(vol->lcnbmp_ino)) {
1434 if (!IS_ERR(vol->lcnbmp_ino))
1435 iput(vol->lcnbmp_ino);
1436 goto bitmap_failed;
1438 if ((vol->nr_clusters + 7) >> 3 > vol->lcnbmp_ino->i_size) {
1439 iput(vol->lcnbmp_ino);
1440 bitmap_failed:
1441 ntfs_error(sb, "Failed to load $Bitmap.");
1442 goto iput_attrdef_err_out;
1445 * Get the volume inode and setup our cache of the volume flags and
1446 * version.
1448 vol->vol_ino = ntfs_iget(sb, FILE_Volume);
1449 if (IS_ERR(vol->vol_ino) || is_bad_inode(vol->vol_ino)) {
1450 if (!IS_ERR(vol->vol_ino))
1451 iput(vol->vol_ino);
1452 volume_failed:
1453 ntfs_error(sb, "Failed to load $Volume.");
1454 goto iput_lcnbmp_err_out;
1456 m = map_mft_record(NTFS_I(vol->vol_ino));
1457 if (IS_ERR(m)) {
1458 iput_volume_failed:
1459 iput(vol->vol_ino);
1460 goto volume_failed;
1462 if (!(ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m))) {
1463 ntfs_error(sb, "Failed to get attribute search context.");
1464 goto get_ctx_vol_failed;
1466 if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
1467 ctx) || ctx->attr->non_resident || ctx->attr->flags) {
1468 err_put_vol:
1469 ntfs_attr_put_search_ctx(ctx);
1470 get_ctx_vol_failed:
1471 unmap_mft_record(NTFS_I(vol->vol_ino));
1472 goto iput_volume_failed;
1474 vi = (VOLUME_INFORMATION*)((char*)ctx->attr +
1475 le16_to_cpu(ctx->attr->data.resident.value_offset));
1476 /* Some bounds checks. */
1477 if ((u8*)vi < (u8*)ctx->attr || (u8*)vi +
1478 le32_to_cpu(ctx->attr->data.resident.value_length) >
1479 (u8*)ctx->attr + le32_to_cpu(ctx->attr->length))
1480 goto err_put_vol;
1481 /* Copy the volume flags and version to the ntfs_volume structure. */
1482 vol->vol_flags = vi->flags;
1483 vol->major_ver = vi->major_ver;
1484 vol->minor_ver = vi->minor_ver;
1485 ntfs_attr_put_search_ctx(ctx);
1486 unmap_mft_record(NTFS_I(vol->vol_ino));
1487 printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver,
1488 vol->minor_ver);
1489 #ifdef NTFS_RW
1490 /* Make sure that no unsupported volume flags are set. */
1491 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
1492 static const char *es1a = "Volume is dirty";
1493 static const char *es1b = "Volume has unsupported flags set";
1494 static const char *es2 = ". Run chkdsk and mount in Windows.";
1495 const char *es1;
1497 es1 = vol->vol_flags & VOLUME_IS_DIRTY ? es1a : es1b;
1498 /* If a read-write mount, convert it to a read-only mount. */
1499 if (!(sb->s_flags & MS_RDONLY)) {
1500 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1501 ON_ERRORS_CONTINUE))) {
1502 ntfs_error(sb, "%s and neither on_errors="
1503 "continue nor on_errors="
1504 "remount-ro was specified%s",
1505 es1, es2);
1506 goto iput_vol_err_out;
1508 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1509 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1510 } else
1511 ntfs_warning(sb, "%s. Will not be able to remount "
1512 "read-write%s", es1, es2);
1514 * Do not set NVolErrors() because ntfs_remount() re-checks the
1515 * flags which we need to do in case any flags have changed.
1519 * Get the inode for the logfile, check it and determine if the volume
1520 * was shutdown cleanly.
1522 if (!load_and_check_logfile(vol) ||
1523 !ntfs_is_logfile_clean(vol->logfile_ino)) {
1524 static const char *es1a = "Failed to load $LogFile";
1525 static const char *es1b = "$LogFile is not clean";
1526 static const char *es2 = ". Mount in Windows.";
1527 const char *es1;
1529 es1 = !vol->logfile_ino ? es1a : es1b;
1530 /* If a read-write mount, convert it to a read-only mount. */
1531 if (!(sb->s_flags & MS_RDONLY)) {
1532 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1533 ON_ERRORS_CONTINUE))) {
1534 ntfs_error(sb, "%s and neither on_errors="
1535 "continue nor on_errors="
1536 "remount-ro was specified%s",
1537 es1, es2);
1538 goto iput_logfile_err_out;
1540 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1541 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1542 } else
1543 ntfs_warning(sb, "%s. Will not be able to remount "
1544 "read-write%s", es1, es2);
1545 /* This will prevent a read-write remount. */
1546 NVolSetErrors(vol);
1548 /* If (still) a read-write mount, mark the volume dirty. */
1549 if (!(sb->s_flags & MS_RDONLY) &&
1550 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
1551 static const char *es1 = "Failed to set dirty bit in volume "
1552 "information flags";
1553 static const char *es2 = ". Run chkdsk.";
1555 /* Convert to a read-only mount. */
1556 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1557 ON_ERRORS_CONTINUE))) {
1558 ntfs_error(sb, "%s and neither on_errors=continue nor "
1559 "on_errors=remount-ro was specified%s",
1560 es1, es2);
1561 goto iput_logfile_err_out;
1563 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1564 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1566 * Do not set NVolErrors() because ntfs_remount() might manage
1567 * to set the dirty flag in which case all would be well.
1570 #if 0
1571 // TODO: Enable this code once we start modifying anything that is
1572 // different between NTFS 1.2 and 3.x...
1574 * If (still) a read-write mount, set the NT4 compatibility flag on
1575 * newer NTFS version volumes.
1577 if (!(sb->s_flags & MS_RDONLY) && (vol->major_ver > 1) &&
1578 ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
1579 static const char *es1 = "Failed to set NT4 compatibility flag";
1580 static const char *es2 = ". Run chkdsk.";
1582 /* Convert to a read-only mount. */
1583 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1584 ON_ERRORS_CONTINUE))) {
1585 ntfs_error(sb, "%s and neither on_errors=continue nor "
1586 "on_errors=remount-ro was specified%s",
1587 es1, es2);
1588 goto iput_logfile_err_out;
1590 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1591 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1592 NVolSetErrors(vol);
1594 #endif
1595 /* If (still) a read-write mount, empty the logfile. */
1596 if (!(sb->s_flags & MS_RDONLY) &&
1597 !ntfs_empty_logfile(vol->logfile_ino)) {
1598 static const char *es1 = "Failed to empty $LogFile";
1599 static const char *es2 = ". Mount in Windows.";
1601 /* Convert to a read-only mount. */
1602 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1603 ON_ERRORS_CONTINUE))) {
1604 ntfs_error(sb, "%s and neither on_errors=continue nor "
1605 "on_errors=remount-ro was specified%s",
1606 es1, es2);
1607 goto iput_logfile_err_out;
1609 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1610 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1611 NVolSetErrors(vol);
1613 #endif /* NTFS_RW */
1614 /* Get the root directory inode. */
1615 vol->root_ino = ntfs_iget(sb, FILE_root);
1616 if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) {
1617 if (!IS_ERR(vol->root_ino))
1618 iput(vol->root_ino);
1619 ntfs_error(sb, "Failed to load root directory.");
1620 goto iput_logfile_err_out;
1622 /* If on NTFS versions before 3.0, we are done. */
1623 if (vol->major_ver < 3)
1624 return TRUE;
1625 /* NTFS 3.0+ specific initialization. */
1626 /* Get the security descriptors inode. */
1627 vol->secure_ino = ntfs_iget(sb, FILE_Secure);
1628 if (IS_ERR(vol->secure_ino) || is_bad_inode(vol->secure_ino)) {
1629 if (!IS_ERR(vol->secure_ino))
1630 iput(vol->secure_ino);
1631 ntfs_error(sb, "Failed to load $Secure.");
1632 goto iput_root_err_out;
1634 // FIXME: Initialize security.
1635 /* Get the extended system files' directory inode. */
1636 vol->extend_ino = ntfs_iget(sb, FILE_Extend);
1637 if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) {
1638 if (!IS_ERR(vol->extend_ino))
1639 iput(vol->extend_ino);
1640 ntfs_error(sb, "Failed to load $Extend.");
1641 goto iput_sec_err_out;
1643 #ifdef NTFS_RW
1644 /* Find the quota file, load it if present, and set it up. */
1645 if (!load_and_init_quota(vol)) {
1646 static const char *es1 = "Failed to load $Quota";
1647 static const char *es2 = ". Run chkdsk.";
1649 /* If a read-write mount, convert it to a read-only mount. */
1650 if (!(sb->s_flags & MS_RDONLY)) {
1651 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1652 ON_ERRORS_CONTINUE))) {
1653 ntfs_error(sb, "%s and neither on_errors="
1654 "continue nor on_errors="
1655 "remount-ro was specified%s",
1656 es1, es2);
1657 goto iput_quota_err_out;
1659 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1660 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1661 } else
1662 ntfs_warning(sb, "%s. Will not be able to remount "
1663 "read-write%s", es1, es2);
1664 /* This will prevent a read-write remount. */
1665 NVolSetErrors(vol);
1667 /* If (still) a read-write mount, mark the quotas out of date. */
1668 if (!(sb->s_flags & MS_RDONLY) &&
1669 !ntfs_mark_quotas_out_of_date(vol)) {
1670 static const char *es1 = "Failed to mark quotas out of date";
1671 static const char *es2 = ". Run chkdsk.";
1673 /* Convert to a read-only mount. */
1674 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1675 ON_ERRORS_CONTINUE))) {
1676 ntfs_error(sb, "%s and neither on_errors=continue nor "
1677 "on_errors=remount-ro was specified%s",
1678 es1, es2);
1679 goto iput_quota_err_out;
1681 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1682 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1683 NVolSetErrors(vol);
1685 // TODO: Delete or checkpoint the $UsnJrnl if it exists.
1686 #endif /* NTFS_RW */
1687 return TRUE;
1688 #ifdef NTFS_RW
1689 iput_quota_err_out:
1690 if (vol->quota_q_ino)
1691 iput(vol->quota_q_ino);
1692 if (vol->quota_ino)
1693 iput(vol->quota_ino);
1694 iput(vol->extend_ino);
1695 #endif /* NTFS_RW */
1696 iput_sec_err_out:
1697 iput(vol->secure_ino);
1698 iput_root_err_out:
1699 iput(vol->root_ino);
1700 iput_logfile_err_out:
1701 #ifdef NTFS_RW
1702 if (vol->logfile_ino)
1703 iput(vol->logfile_ino);
1704 iput_vol_err_out:
1705 #endif /* NTFS_RW */
1706 iput(vol->vol_ino);
1707 iput_lcnbmp_err_out:
1708 iput(vol->lcnbmp_ino);
1709 iput_attrdef_err_out:
1710 vol->attrdef_size = 0;
1711 if (vol->attrdef) {
1712 ntfs_free(vol->attrdef);
1713 vol->attrdef = NULL;
1715 #ifdef NTFS_RW
1716 iput_upcase_err_out:
1717 #endif /* NTFS_RW */
1718 vol->upcase_len = 0;
1719 down(&ntfs_lock);
1720 if (vol->upcase == default_upcase) {
1721 ntfs_nr_upcase_users--;
1722 vol->upcase = NULL;
1724 up(&ntfs_lock);
1725 if (vol->upcase) {
1726 ntfs_free(vol->upcase);
1727 vol->upcase = NULL;
1729 iput_mftbmp_err_out:
1730 iput(vol->mftbmp_ino);
1731 iput_mirr_err_out:
1732 #ifdef NTFS_RW
1733 if (vol->mftmirr_ino)
1734 iput(vol->mftmirr_ino);
1735 #endif /* NTFS_RW */
1736 return FALSE;
1740 * ntfs_put_super - called by the vfs to unmount a volume
1741 * @sb: vfs superblock of volume to unmount
1743 * ntfs_put_super() is called by the VFS (from fs/super.c::do_umount()) when
1744 * the volume is being unmounted (umount system call has been invoked) and it
1745 * releases all inodes and memory belonging to the NTFS specific part of the
1746 * super block.
1748 static void ntfs_put_super(struct super_block *sb)
1750 ntfs_volume *vol = NTFS_SB(sb);
1752 ntfs_debug("Entering.");
1753 #ifdef NTFS_RW
1755 * Commit all inodes while they are still open in case some of them
1756 * cause others to be dirtied.
1758 ntfs_commit_inode(vol->vol_ino);
1760 /* NTFS 3.0+ specific. */
1761 if (vol->major_ver >= 3) {
1762 if (vol->quota_q_ino)
1763 ntfs_commit_inode(vol->quota_q_ino);
1764 if (vol->quota_ino)
1765 ntfs_commit_inode(vol->quota_ino);
1766 if (vol->extend_ino)
1767 ntfs_commit_inode(vol->extend_ino);
1768 if (vol->secure_ino)
1769 ntfs_commit_inode(vol->secure_ino);
1772 ntfs_commit_inode(vol->root_ino);
1774 down_write(&vol->lcnbmp_lock);
1775 ntfs_commit_inode(vol->lcnbmp_ino);
1776 up_write(&vol->lcnbmp_lock);
1778 down_write(&vol->mftbmp_lock);
1779 ntfs_commit_inode(vol->mftbmp_ino);
1780 up_write(&vol->mftbmp_lock);
1782 if (vol->logfile_ino)
1783 ntfs_commit_inode(vol->logfile_ino);
1785 if (vol->mftmirr_ino)
1786 ntfs_commit_inode(vol->mftmirr_ino);
1787 ntfs_commit_inode(vol->mft_ino);
1790 * If a read-write mount and no volume errors have occured, mark the
1791 * volume clean. Also, re-commit all affected inodes.
1793 if (!(sb->s_flags & MS_RDONLY)) {
1794 if (!NVolErrors(vol)) {
1795 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
1796 ntfs_warning(sb, "Failed to clear dirty bit "
1797 "in volume information "
1798 "flags. Run chkdsk.");
1799 ntfs_commit_inode(vol->vol_ino);
1800 ntfs_commit_inode(vol->root_ino);
1801 if (vol->mftmirr_ino)
1802 ntfs_commit_inode(vol->mftmirr_ino);
1803 ntfs_commit_inode(vol->mft_ino);
1804 } else {
1805 ntfs_warning(sb, "Volume has errors. Leaving volume "
1806 "marked dirty. Run chkdsk.");
1809 #endif /* NTFS_RW */
1811 iput(vol->vol_ino);
1812 vol->vol_ino = NULL;
1814 /* NTFS 3.0+ specific clean up. */
1815 if (vol->major_ver >= 3) {
1816 #ifdef NTFS_RW
1817 if (vol->quota_q_ino) {
1818 iput(vol->quota_q_ino);
1819 vol->quota_q_ino = NULL;
1821 if (vol->quota_ino) {
1822 iput(vol->quota_ino);
1823 vol->quota_ino = NULL;
1825 #endif /* NTFS_RW */
1826 if (vol->extend_ino) {
1827 iput(vol->extend_ino);
1828 vol->extend_ino = NULL;
1830 if (vol->secure_ino) {
1831 iput(vol->secure_ino);
1832 vol->secure_ino = NULL;
1836 iput(vol->root_ino);
1837 vol->root_ino = NULL;
1839 down_write(&vol->lcnbmp_lock);
1840 iput(vol->lcnbmp_ino);
1841 vol->lcnbmp_ino = NULL;
1842 up_write(&vol->lcnbmp_lock);
1844 down_write(&vol->mftbmp_lock);
1845 iput(vol->mftbmp_ino);
1846 vol->mftbmp_ino = NULL;
1847 up_write(&vol->mftbmp_lock);
1849 #ifdef NTFS_RW
1850 if (vol->logfile_ino) {
1851 iput(vol->logfile_ino);
1852 vol->logfile_ino = NULL;
1854 if (vol->mftmirr_ino) {
1855 /* Re-commit the mft mirror and mft just in case. */
1856 ntfs_commit_inode(vol->mftmirr_ino);
1857 ntfs_commit_inode(vol->mft_ino);
1858 iput(vol->mftmirr_ino);
1859 vol->mftmirr_ino = NULL;
1862 * If any dirty inodes are left, throw away all mft data page cache
1863 * pages to allow a clean umount. This should never happen any more
1864 * due to mft.c::ntfs_mft_writepage() cleaning all the dirty pages as
1865 * the underlying mft records are written out and cleaned. If it does,
1866 * happen anyway, we want to know...
1868 ntfs_commit_inode(vol->mft_ino);
1869 write_inode_now(vol->mft_ino, 1);
1870 if (!list_empty(&sb->s_dirty)) {
1871 const char *s1, *s2;
1873 down(&vol->mft_ino->i_sem);
1874 truncate_inode_pages(vol->mft_ino->i_mapping, 0);
1875 up(&vol->mft_ino->i_sem);
1876 write_inode_now(vol->mft_ino, 1);
1877 if (!list_empty(&sb->s_dirty)) {
1878 static const char *_s1 = "inodes";
1879 static const char *_s2 = "";
1880 s1 = _s1;
1881 s2 = _s2;
1882 } else {
1883 static const char *_s1 = "mft pages";
1884 static const char *_s2 = "They have been thrown "
1885 "away. ";
1886 s1 = _s1;
1887 s2 = _s2;
1889 ntfs_error(sb, "Dirty %s found at umount time. %sYou should "
1890 "run chkdsk. Please email "
1891 "linux-ntfs-dev@lists.sourceforge.net and say "
1892 "that you saw this message. Thank you.", s1,
1893 s2);
1895 #endif /* NTFS_RW */
1897 iput(vol->mft_ino);
1898 vol->mft_ino = NULL;
1900 /* Throw away the table of attribute definitions. */
1901 vol->attrdef_size = 0;
1902 if (vol->attrdef) {
1903 ntfs_free(vol->attrdef);
1904 vol->attrdef = NULL;
1906 vol->upcase_len = 0;
1908 * Destroy the global default upcase table if necessary. Also decrease
1909 * the number of upcase users if we are a user.
1911 down(&ntfs_lock);
1912 if (vol->upcase == default_upcase) {
1913 ntfs_nr_upcase_users--;
1914 vol->upcase = NULL;
1916 if (!ntfs_nr_upcase_users && default_upcase) {
1917 ntfs_free(default_upcase);
1918 default_upcase = NULL;
1920 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
1921 free_compression_buffers();
1922 up(&ntfs_lock);
1923 if (vol->upcase) {
1924 ntfs_free(vol->upcase);
1925 vol->upcase = NULL;
1927 if (vol->nls_map) {
1928 unload_nls(vol->nls_map);
1929 vol->nls_map = NULL;
1931 sb->s_fs_info = NULL;
1932 kfree(vol);
1933 return;
1937 * get_nr_free_clusters - return the number of free clusters on a volume
1938 * @vol: ntfs volume for which to obtain free cluster count
1940 * Calculate the number of free clusters on the mounted NTFS volume @vol. We
1941 * actually calculate the number of clusters in use instead because this
1942 * allows us to not care about partial pages as these will be just zero filled
1943 * and hence not be counted as allocated clusters.
1945 * The only particularity is that clusters beyond the end of the logical ntfs
1946 * volume will be marked as allocated to prevent errors which means we have to
1947 * discount those at the end. This is important as the cluster bitmap always
1948 * has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside
1949 * the logical volume and marked in use when they are not as they do not exist.
1951 * If any pages cannot be read we assume all clusters in the erroring pages are
1952 * in use. This means we return an underestimate on errors which is better than
1953 * an overestimate.
1955 static s64 get_nr_free_clusters(ntfs_volume *vol)
1957 s64 nr_free = vol->nr_clusters;
1958 u32 *kaddr;
1959 struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
1960 filler_t *readpage = (filler_t*)mapping->a_ops->readpage;
1961 struct page *page;
1962 unsigned long index, max_index;
1963 unsigned int max_size;
1965 ntfs_debug("Entering.");
1966 /* Serialize accesses to the cluster bitmap. */
1967 down_read(&vol->lcnbmp_lock);
1969 * Convert the number of bits into bytes rounded up, then convert into
1970 * multiples of PAGE_CACHE_SIZE, rounding up so that if we have one
1971 * full and one partial page max_index = 2.
1973 max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >>
1974 PAGE_CACHE_SHIFT;
1975 /* Use multiples of 4 bytes. */
1976 max_size = PAGE_CACHE_SIZE >> 2;
1977 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%x.",
1978 max_index, max_size);
1979 for (index = 0UL; index < max_index; index++) {
1980 unsigned int i;
1982 * Read the page from page cache, getting it from backing store
1983 * if necessary, and increment the use count.
1985 page = read_cache_page(mapping, index, (filler_t*)readpage,
1986 NULL);
1987 /* Ignore pages which errored synchronously. */
1988 if (IS_ERR(page)) {
1989 ntfs_debug("Sync read_cache_page() error. Skipping "
1990 "page (index 0x%lx).", index);
1991 nr_free -= PAGE_CACHE_SIZE * 8;
1992 continue;
1994 wait_on_page_locked(page);
1995 /* Ignore pages which errored asynchronously. */
1996 if (!PageUptodate(page)) {
1997 ntfs_debug("Async read_cache_page() error. Skipping "
1998 "page (index 0x%lx).", index);
1999 page_cache_release(page);
2000 nr_free -= PAGE_CACHE_SIZE * 8;
2001 continue;
2003 kaddr = (u32*)kmap_atomic(page, KM_USER0);
2005 * For each 4 bytes, subtract the number of set bits. If this
2006 * is the last page and it is partial we don't really care as
2007 * it just means we do a little extra work but it won't affect
2008 * the result as all out of range bytes are set to zero by
2009 * ntfs_readpage().
2011 for (i = 0; i < max_size; i++)
2012 nr_free -= (s64)hweight32(kaddr[i]);
2013 kunmap_atomic(kaddr, KM_USER0);
2014 page_cache_release(page);
2016 ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index - 1);
2018 * Fixup for eventual bits outside logical ntfs volume (see function
2019 * description above).
2021 if (vol->nr_clusters & 63)
2022 nr_free += 64 - (vol->nr_clusters & 63);
2023 up_read(&vol->lcnbmp_lock);
2024 /* If errors occured we may well have gone below zero, fix this. */
2025 if (nr_free < 0)
2026 nr_free = 0;
2027 ntfs_debug("Exiting.");
2028 return nr_free;
2032 * __get_nr_free_mft_records - return the number of free inodes on a volume
2033 * @vol: ntfs volume for which to obtain free inode count
2035 * Calculate the number of free mft records (inodes) on the mounted NTFS
2036 * volume @vol. We actually calculate the number of mft records in use instead
2037 * because this allows us to not care about partial pages as these will be just
2038 * zero filled and hence not be counted as allocated mft record.
2040 * If any pages cannot be read we assume all mft records in the erroring pages
2041 * are in use. This means we return an underestimate on errors which is better
2042 * than an overestimate.
2044 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
2046 static unsigned long __get_nr_free_mft_records(ntfs_volume *vol)
2048 s64 nr_free;
2049 u32 *kaddr;
2050 struct address_space *mapping = vol->mftbmp_ino->i_mapping;
2051 filler_t *readpage = (filler_t*)mapping->a_ops->readpage;
2052 struct page *page;
2053 unsigned long index, max_index;
2054 unsigned int max_size;
2056 ntfs_debug("Entering.");
2057 /* Number of mft records in file system (at this point in time). */
2058 nr_free = vol->mft_ino->i_size >> vol->mft_record_size_bits;
2060 * Convert the maximum number of set bits into bytes rounded up, then
2061 * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
2062 * have one full and one partial page max_index = 2.
2064 max_index = ((((NTFS_I(vol->mft_ino)->initialized_size >>
2065 vol->mft_record_size_bits) + 7) >> 3) +
2066 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2067 /* Use multiples of 4 bytes. */
2068 max_size = PAGE_CACHE_SIZE >> 2;
2069 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
2070 "0x%x.", max_index, max_size);
2071 for (index = 0UL; index < max_index; index++) {
2072 unsigned int i;
2074 * Read the page from page cache, getting it from backing store
2075 * if necessary, and increment the use count.
2077 page = read_cache_page(mapping, index, (filler_t*)readpage,
2078 NULL);
2079 /* Ignore pages which errored synchronously. */
2080 if (IS_ERR(page)) {
2081 ntfs_debug("Sync read_cache_page() error. Skipping "
2082 "page (index 0x%lx).", index);
2083 nr_free -= PAGE_CACHE_SIZE * 8;
2084 continue;
2086 wait_on_page_locked(page);
2087 /* Ignore pages which errored asynchronously. */
2088 if (!PageUptodate(page)) {
2089 ntfs_debug("Async read_cache_page() error. Skipping "
2090 "page (index 0x%lx).", index);
2091 page_cache_release(page);
2092 nr_free -= PAGE_CACHE_SIZE * 8;
2093 continue;
2095 kaddr = (u32*)kmap_atomic(page, KM_USER0);
2097 * For each 4 bytes, subtract the number of set bits. If this
2098 * is the last page and it is partial we don't really care as
2099 * it just means we do a little extra work but it won't affect
2100 * the result as all out of range bytes are set to zero by
2101 * ntfs_readpage().
2103 for (i = 0; i < max_size; i++)
2104 nr_free -= (s64)hweight32(kaddr[i]);
2105 kunmap_atomic(kaddr, KM_USER0);
2106 page_cache_release(page);
2108 ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.",
2109 index - 1);
2110 /* If errors occured we may well have gone below zero, fix this. */
2111 if (nr_free < 0)
2112 nr_free = 0;
2113 ntfs_debug("Exiting.");
2114 return nr_free;
2118 * ntfs_statfs - return information about mounted NTFS volume
2119 * @sb: super block of mounted volume
2120 * @sfs: statfs structure in which to return the information
2122 * Return information about the mounted NTFS volume @sb in the statfs structure
2123 * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is
2124 * called). We interpret the values to be correct of the moment in time at
2125 * which we are called. Most values are variable otherwise and this isn't just
2126 * the free values but the totals as well. For example we can increase the
2127 * total number of file nodes if we run out and we can keep doing this until
2128 * there is no more space on the volume left at all.
2130 * Called from vfs_statfs which is used to handle the statfs, fstatfs, and
2131 * ustat system calls.
2133 * Return 0 on success or -errno on error.
2135 static int ntfs_statfs(struct super_block *sb, struct kstatfs *sfs)
2137 ntfs_volume *vol = NTFS_SB(sb);
2138 s64 size;
2140 ntfs_debug("Entering.");
2141 /* Type of filesystem. */
2142 sfs->f_type = NTFS_SB_MAGIC;
2143 /* Optimal transfer block size. */
2144 sfs->f_bsize = PAGE_CACHE_SIZE;
2146 * Total data blocks in file system in units of f_bsize and since
2147 * inodes are also stored in data blocs ($MFT is a file) this is just
2148 * the total clusters.
2150 sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >>
2151 PAGE_CACHE_SHIFT;
2152 /* Free data blocks in file system in units of f_bsize. */
2153 size = get_nr_free_clusters(vol) << vol->cluster_size_bits >>
2154 PAGE_CACHE_SHIFT;
2155 if (size < 0LL)
2156 size = 0LL;
2157 /* Free blocks avail to non-superuser, same as above on NTFS. */
2158 sfs->f_bavail = sfs->f_bfree = size;
2159 /* Serialize accesses to the inode bitmap. */
2160 down_read(&vol->mftbmp_lock);
2161 /* Number of inodes in file system (at this point in time). */
2162 sfs->f_files = vol->mft_ino->i_size >> vol->mft_record_size_bits;
2163 /* Free inodes in fs (based on current total count). */
2164 sfs->f_ffree = __get_nr_free_mft_records(vol);
2165 up_read(&vol->mftbmp_lock);
2167 * File system id. This is extremely *nix flavour dependent and even
2168 * within Linux itself all fs do their own thing. I interpret this to
2169 * mean a unique id associated with the mounted fs and not the id
2170 * associated with the file system driver, the latter is already given
2171 * by the file system type in sfs->f_type. Thus we use the 64-bit
2172 * volume serial number splitting it into two 32-bit parts. We enter
2173 * the least significant 32-bits in f_fsid[0] and the most significant
2174 * 32-bits in f_fsid[1].
2176 sfs->f_fsid.val[0] = vol->serial_no & 0xffffffff;
2177 sfs->f_fsid.val[1] = (vol->serial_no >> 32) & 0xffffffff;
2178 /* Maximum length of filenames. */
2179 sfs->f_namelen = NTFS_MAX_NAME_LEN;
2180 return 0;
2184 * The complete super operations.
2186 static struct super_operations ntfs_sops = {
2187 .alloc_inode = ntfs_alloc_big_inode, /* VFS: Allocate new inode. */
2188 .destroy_inode = ntfs_destroy_big_inode, /* VFS: Deallocate inode. */
2189 .put_inode = ntfs_put_inode, /* VFS: Called just before
2190 the inode reference count
2191 is decreased. */
2192 #ifdef NTFS_RW
2193 //.dirty_inode = NULL, /* VFS: Called from
2194 // __mark_inode_dirty(). */
2195 .write_inode = ntfs_write_inode, /* VFS: Write dirty inode to
2196 disk. */
2197 //.drop_inode = NULL, /* VFS: Called just after the
2198 // inode reference count has
2199 // been decreased to zero.
2200 // NOTE: The inode lock is
2201 // held. See fs/inode.c::
2202 // generic_drop_inode(). */
2203 //.delete_inode = NULL, /* VFS: Delete inode from disk.
2204 // Called when i_count becomes
2205 // 0 and i_nlink is also 0. */
2206 //.write_super = NULL, /* Flush dirty super block to
2207 // disk. */
2208 //.sync_fs = NULL, /* ? */
2209 //.write_super_lockfs = NULL, /* ? */
2210 //.unlockfs = NULL, /* ? */
2211 #endif /* NTFS_RW */
2212 .put_super = ntfs_put_super, /* Syscall: umount. */
2213 .statfs = ntfs_statfs, /* Syscall: statfs */
2214 .remount_fs = ntfs_remount, /* Syscall: mount -o remount. */
2215 .clear_inode = ntfs_clear_big_inode, /* VFS: Called when an inode is
2216 removed from memory. */
2217 //.umount_begin = NULL, /* Forced umount. */
2218 .show_options = ntfs_show_options, /* Show mount options in
2219 proc. */
2224 * Declarations for NTFS specific export operations (fs/ntfs/namei.c).
2226 extern struct dentry *ntfs_get_parent(struct dentry *child_dent);
2227 extern struct dentry *ntfs_get_dentry(struct super_block *sb, void *fh);
2230 * Export operations allowing NFS exporting of mounted NTFS partitions.
2232 * We use the default ->decode_fh() and ->encode_fh() for now. Note that they
2233 * use 32 bits to store the inode number which is an unsigned long so on 64-bit
2234 * architectures is usually 64 bits so it would all fail horribly on huge
2235 * volumes. I guess we need to define our own encode and decode fh functions
2236 * that store 64-bit inode numbers at some point but for now we will ignore the
2237 * problem...
2239 * We also use the default ->get_name() helper (used by ->decode_fh() via
2240 * fs/exportfs/expfs.c::find_exported_dentry()) as that is completely fs
2241 * independent.
2243 * The default ->get_parent() just returns -EACCES so we have to provide our
2244 * own and the default ->get_dentry() is incompatible with NTFS due to not
2245 * allowing the inode number 0 which is used in NTFS for the system file $MFT
2246 * and due to using iget() whereas NTFS needs ntfs_iget().
2248 static struct export_operations ntfs_export_ops = {
2249 .get_parent = ntfs_get_parent, /* Find the parent of a given
2250 directory. */
2251 .get_dentry = ntfs_get_dentry, /* Find a dentry for the inode
2252 given a file handle
2253 sub-fragment. */
2257 * ntfs_fill_super - mount an ntfs files system
2258 * @sb: super block of ntfs file system to mount
2259 * @opt: string containing the mount options
2260 * @silent: silence error output
2262 * ntfs_fill_super() is called by the VFS to mount the device described by @sb
2263 * with the mount otions in @data with the NTFS file system.
2265 * If @silent is true, remain silent even if errors are detected. This is used
2266 * during bootup, when the kernel tries to mount the root file system with all
2267 * registered file systems one after the other until one succeeds. This implies
2268 * that all file systems except the correct one will quite correctly and
2269 * expectedly return an error, but nobody wants to see error messages when in
2270 * fact this is what is supposed to happen.
2272 * NOTE: @sb->s_flags contains the mount options flags.
2274 static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2276 ntfs_volume *vol;
2277 struct buffer_head *bh;
2278 struct inode *tmp_ino;
2279 int result;
2281 ntfs_debug("Entering.");
2282 #ifndef NTFS_RW
2283 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
2284 #endif /* ! NTFS_RW */
2285 /* Allocate a new ntfs_volume and place it in sb->s_fs_info. */
2286 sb->s_fs_info = kmalloc(sizeof(ntfs_volume), GFP_NOFS);
2287 vol = NTFS_SB(sb);
2288 if (!vol) {
2289 if (!silent)
2290 ntfs_error(sb, "Allocation of NTFS volume structure "
2291 "failed. Aborting mount...");
2292 return -ENOMEM;
2294 /* Initialize ntfs_volume structure. */
2295 memset(vol, 0, sizeof(ntfs_volume));
2296 vol->sb = sb;
2297 vol->upcase = NULL;
2298 vol->attrdef = NULL;
2299 vol->mft_ino = NULL;
2300 vol->mftbmp_ino = NULL;
2301 init_rwsem(&vol->mftbmp_lock);
2302 #ifdef NTFS_RW
2303 vol->mftmirr_ino = NULL;
2304 vol->logfile_ino = NULL;
2305 #endif /* NTFS_RW */
2306 vol->lcnbmp_ino = NULL;
2307 init_rwsem(&vol->lcnbmp_lock);
2308 vol->vol_ino = NULL;
2309 vol->root_ino = NULL;
2310 vol->secure_ino = NULL;
2311 vol->extend_ino = NULL;
2312 #ifdef NTFS_RW
2313 vol->quota_ino = NULL;
2314 vol->quota_q_ino = NULL;
2315 #endif /* NTFS_RW */
2316 vol->nls_map = NULL;
2319 * Default is group and other don't have any access to files or
2320 * directories while owner has full access. Further, files by default
2321 * are not executable but directories are of course browseable.
2323 vol->fmask = 0177;
2324 vol->dmask = 0077;
2326 unlock_kernel();
2328 /* Important to get the mount options dealt with now. */
2329 if (!parse_options(vol, (char*)opt))
2330 goto err_out_now;
2333 * TODO: Fail safety check. In the future we should really be able to
2334 * cope with this being the case, but for now just bail out.
2336 if (bdev_hardsect_size(sb->s_bdev) > NTFS_BLOCK_SIZE) {
2337 if (!silent)
2338 ntfs_error(sb, "Device has unsupported hardsect_size.");
2339 goto err_out_now;
2342 /* Setup the device access block size to NTFS_BLOCK_SIZE. */
2343 if (sb_set_blocksize(sb, NTFS_BLOCK_SIZE) != NTFS_BLOCK_SIZE) {
2344 if (!silent)
2345 ntfs_error(sb, "Unable to set block size.");
2346 goto err_out_now;
2349 /* Get the size of the device in units of NTFS_BLOCK_SIZE bytes. */
2350 vol->nr_blocks = sb->s_bdev->bd_inode->i_size >> NTFS_BLOCK_SIZE_BITS;
2352 /* Read the boot sector and return unlocked buffer head to it. */
2353 if (!(bh = read_ntfs_boot_sector(sb, silent))) {
2354 if (!silent)
2355 ntfs_error(sb, "Not an NTFS volume.");
2356 goto err_out_now;
2360 * Extract the data from the boot sector and setup the ntfs super block
2361 * using it.
2363 result = parse_ntfs_boot_sector(vol, (NTFS_BOOT_SECTOR*)bh->b_data);
2365 /* Initialize the cluster and mft allocators. */
2366 ntfs_setup_allocators(vol);
2368 brelse(bh);
2370 if (!result) {
2371 if (!silent)
2372 ntfs_error(sb, "Unsupported NTFS filesystem.");
2373 goto err_out_now;
2377 * TODO: When we start coping with sector sizes different from
2378 * NTFS_BLOCK_SIZE, we now probably need to set the blocksize of the
2379 * device (probably to NTFS_BLOCK_SIZE).
2382 /* Setup remaining fields in the super block. */
2383 sb->s_magic = NTFS_SB_MAGIC;
2386 * Ntfs allows 63 bits for the file size, i.e. correct would be:
2387 * sb->s_maxbytes = ~0ULL >> 1;
2388 * But the kernel uses a long as the page cache page index which on
2389 * 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel
2390 * defined to the maximum the page cache page index can cope with
2391 * without overflowing the index or to 2^63 - 1, whichever is smaller.
2393 sb->s_maxbytes = MAX_LFS_FILESIZE;
2395 sb->s_time_gran = 100;
2398 * Now load the metadata required for the page cache and our address
2399 * space operations to function. We do this by setting up a specialised
2400 * read_inode method and then just calling the normal iget() to obtain
2401 * the inode for $MFT which is sufficient to allow our normal inode
2402 * operations and associated address space operations to function.
2404 sb->s_op = &ntfs_sops;
2405 tmp_ino = new_inode(sb);
2406 if (!tmp_ino) {
2407 if (!silent)
2408 ntfs_error(sb, "Failed to load essential metadata.");
2409 goto err_out_now;
2411 tmp_ino->i_ino = FILE_MFT;
2412 insert_inode_hash(tmp_ino);
2413 if (ntfs_read_inode_mount(tmp_ino) < 0) {
2414 if (!silent)
2415 ntfs_error(sb, "Failed to load essential metadata.");
2416 goto iput_tmp_ino_err_out_now;
2418 down(&ntfs_lock);
2420 * The current mount is a compression user if the cluster size is
2421 * less than or equal 4kiB.
2423 if (vol->cluster_size <= 4096 && !ntfs_nr_compression_users++) {
2424 result = allocate_compression_buffers();
2425 if (result) {
2426 ntfs_error(NULL, "Failed to allocate buffers "
2427 "for compression engine.");
2428 ntfs_nr_compression_users--;
2429 up(&ntfs_lock);
2430 goto iput_tmp_ino_err_out_now;
2434 * Generate the global default upcase table if necessary. Also
2435 * temporarily increment the number of upcase users to avoid race
2436 * conditions with concurrent (u)mounts.
2438 if (!default_upcase)
2439 default_upcase = generate_default_upcase();
2440 ntfs_nr_upcase_users++;
2441 up(&ntfs_lock);
2443 * From now on, ignore @silent parameter. If we fail below this line,
2444 * it will be due to a corrupt fs or a system error, so we report it.
2447 * Open the system files with normal access functions and complete
2448 * setting up the ntfs super block.
2450 if (!load_system_files(vol)) {
2451 ntfs_error(sb, "Failed to load system files.");
2452 goto unl_upcase_iput_tmp_ino_err_out_now;
2454 if ((sb->s_root = d_alloc_root(vol->root_ino))) {
2455 /* We increment i_count simulating an ntfs_iget(). */
2456 atomic_inc(&vol->root_ino->i_count);
2457 ntfs_debug("Exiting, status successful.");
2458 /* Release the default upcase if it has no users. */
2459 down(&ntfs_lock);
2460 if (!--ntfs_nr_upcase_users && default_upcase) {
2461 ntfs_free(default_upcase);
2462 default_upcase = NULL;
2464 up(&ntfs_lock);
2465 sb->s_export_op = &ntfs_export_ops;
2466 lock_kernel();
2467 return 0;
2469 ntfs_error(sb, "Failed to allocate root directory.");
2470 /* Clean up after the successful load_system_files() call from above. */
2471 // TODO: Use ntfs_put_super() instead of repeating all this code...
2472 // FIXME: Should mark the volume clean as the error is most likely
2473 // -ENOMEM.
2474 iput(vol->vol_ino);
2475 vol->vol_ino = NULL;
2476 /* NTFS 3.0+ specific clean up. */
2477 if (vol->major_ver >= 3) {
2478 #ifdef NTFS_RW
2479 if (vol->quota_q_ino) {
2480 iput(vol->quota_q_ino);
2481 vol->quota_q_ino = NULL;
2483 if (vol->quota_ino) {
2484 iput(vol->quota_ino);
2485 vol->quota_ino = NULL;
2487 #endif /* NTFS_RW */
2488 if (vol->extend_ino) {
2489 iput(vol->extend_ino);
2490 vol->extend_ino = NULL;
2492 if (vol->secure_ino) {
2493 iput(vol->secure_ino);
2494 vol->secure_ino = NULL;
2497 iput(vol->root_ino);
2498 vol->root_ino = NULL;
2499 iput(vol->lcnbmp_ino);
2500 vol->lcnbmp_ino = NULL;
2501 iput(vol->mftbmp_ino);
2502 vol->mftbmp_ino = NULL;
2503 #ifdef NTFS_RW
2504 if (vol->logfile_ino) {
2505 iput(vol->logfile_ino);
2506 vol->logfile_ino = NULL;
2508 if (vol->mftmirr_ino) {
2509 iput(vol->mftmirr_ino);
2510 vol->mftmirr_ino = NULL;
2512 #endif /* NTFS_RW */
2513 /* Throw away the table of attribute definitions. */
2514 vol->attrdef_size = 0;
2515 if (vol->attrdef) {
2516 ntfs_free(vol->attrdef);
2517 vol->attrdef = NULL;
2519 vol->upcase_len = 0;
2520 down(&ntfs_lock);
2521 if (vol->upcase == default_upcase) {
2522 ntfs_nr_upcase_users--;
2523 vol->upcase = NULL;
2525 up(&ntfs_lock);
2526 if (vol->upcase) {
2527 ntfs_free(vol->upcase);
2528 vol->upcase = NULL;
2530 if (vol->nls_map) {
2531 unload_nls(vol->nls_map);
2532 vol->nls_map = NULL;
2534 /* Error exit code path. */
2535 unl_upcase_iput_tmp_ino_err_out_now:
2537 * Decrease the number of upcase users and destroy the global default
2538 * upcase table if necessary.
2540 down(&ntfs_lock);
2541 if (!--ntfs_nr_upcase_users && default_upcase) {
2542 ntfs_free(default_upcase);
2543 default_upcase = NULL;
2545 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
2546 free_compression_buffers();
2547 up(&ntfs_lock);
2548 iput_tmp_ino_err_out_now:
2549 iput(tmp_ino);
2550 if (vol->mft_ino && vol->mft_ino != tmp_ino)
2551 iput(vol->mft_ino);
2552 vol->mft_ino = NULL;
2554 * This is needed to get ntfs_clear_extent_inode() called for each
2555 * inode we have ever called ntfs_iget()/iput() on, otherwise we A)
2556 * leak resources and B) a subsequent mount fails automatically due to
2557 * ntfs_iget() never calling down into our ntfs_read_locked_inode()
2558 * method again... FIXME: Do we need to do this twice now because of
2559 * attribute inodes? I think not, so leave as is for now... (AIA)
2561 if (invalidate_inodes(sb)) {
2562 ntfs_error(sb, "Busy inodes left. This is most likely a NTFS "
2563 "driver bug.");
2564 /* Copied from fs/super.c. I just love this message. (-; */
2565 printk("NTFS: Busy inodes after umount. Self-destruct in 5 "
2566 "seconds. Have a nice day...\n");
2568 /* Errors at this stage are irrelevant. */
2569 err_out_now:
2570 lock_kernel();
2571 sb->s_fs_info = NULL;
2572 kfree(vol);
2573 ntfs_debug("Failed, returning -EINVAL.");
2574 return -EINVAL;
2578 * This is a slab cache to optimize allocations and deallocations of Unicode
2579 * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN
2580 * (255) Unicode characters + a terminating NULL Unicode character.
2582 kmem_cache_t *ntfs_name_cache;
2584 /* Slab caches for efficient allocation/deallocation of of inodes. */
2585 kmem_cache_t *ntfs_inode_cache;
2586 kmem_cache_t *ntfs_big_inode_cache;
2588 /* Init once constructor for the inode slab cache. */
2589 static void ntfs_big_inode_init_once(void *foo, kmem_cache_t *cachep,
2590 unsigned long flags)
2592 ntfs_inode *ni = (ntfs_inode *)foo;
2594 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
2595 SLAB_CTOR_CONSTRUCTOR)
2596 inode_init_once(VFS_I(ni));
2600 * Slab caches to optimize allocations and deallocations of attribute search
2601 * contexts and index contexts, respectively.
2603 kmem_cache_t *ntfs_attr_ctx_cache;
2604 kmem_cache_t *ntfs_index_ctx_cache;
2606 /* Driver wide semaphore. */
2607 DECLARE_MUTEX(ntfs_lock);
2609 static struct super_block *ntfs_get_sb(struct file_system_type *fs_type,
2610 int flags, const char *dev_name, void *data)
2612 return get_sb_bdev(fs_type, flags, dev_name, data, ntfs_fill_super);
2615 static struct file_system_type ntfs_fs_type = {
2616 .owner = THIS_MODULE,
2617 .name = "ntfs",
2618 .get_sb = ntfs_get_sb,
2619 .kill_sb = kill_block_super,
2620 .fs_flags = FS_REQUIRES_DEV,
2623 /* Stable names for the slab caches. */
2624 static const char ntfs_index_ctx_cache_name[] = "ntfs_index_ctx_cache";
2625 static const char ntfs_attr_ctx_cache_name[] = "ntfs_attr_ctx_cache";
2626 static const char ntfs_name_cache_name[] = "ntfs_name_cache";
2627 static const char ntfs_inode_cache_name[] = "ntfs_inode_cache";
2628 static const char ntfs_big_inode_cache_name[] = "ntfs_big_inode_cache";
2630 static int __init init_ntfs_fs(void)
2632 int err = 0;
2634 /* This may be ugly but it results in pretty output so who cares. (-8 */
2635 printk(KERN_INFO "NTFS driver " NTFS_VERSION " [Flags: R/"
2636 #ifdef NTFS_RW
2638 #else
2640 #endif
2641 #ifdef DEBUG
2642 " DEBUG"
2643 #endif
2644 #ifdef MODULE
2645 " MODULE"
2646 #endif
2647 "].\n");
2649 ntfs_debug("Debug messages are enabled.");
2651 ntfs_index_ctx_cache = kmem_cache_create(ntfs_index_ctx_cache_name,
2652 sizeof(ntfs_index_context), 0 /* offset */,
2653 SLAB_HWCACHE_ALIGN, NULL /* ctor */, NULL /* dtor */);
2654 if (!ntfs_index_ctx_cache) {
2655 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
2656 ntfs_index_ctx_cache_name);
2657 goto ictx_err_out;
2659 ntfs_attr_ctx_cache = kmem_cache_create(ntfs_attr_ctx_cache_name,
2660 sizeof(ntfs_attr_search_ctx), 0 /* offset */,
2661 SLAB_HWCACHE_ALIGN, NULL /* ctor */, NULL /* dtor */);
2662 if (!ntfs_attr_ctx_cache) {
2663 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
2664 ntfs_attr_ctx_cache_name);
2665 goto actx_err_out;
2668 ntfs_name_cache = kmem_cache_create(ntfs_name_cache_name,
2669 (NTFS_MAX_NAME_LEN+1) * sizeof(ntfschar), 0,
2670 SLAB_HWCACHE_ALIGN, NULL, NULL);
2671 if (!ntfs_name_cache) {
2672 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
2673 ntfs_name_cache_name);
2674 goto name_err_out;
2677 ntfs_inode_cache = kmem_cache_create(ntfs_inode_cache_name,
2678 sizeof(ntfs_inode), 0,
2679 SLAB_RECLAIM_ACCOUNT, NULL, NULL);
2680 if (!ntfs_inode_cache) {
2681 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
2682 ntfs_inode_cache_name);
2683 goto inode_err_out;
2686 ntfs_big_inode_cache = kmem_cache_create(ntfs_big_inode_cache_name,
2687 sizeof(big_ntfs_inode), 0,
2688 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
2689 ntfs_big_inode_init_once, NULL);
2690 if (!ntfs_big_inode_cache) {
2691 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
2692 ntfs_big_inode_cache_name);
2693 goto big_inode_err_out;
2696 /* Register the ntfs sysctls. */
2697 err = ntfs_sysctl(1);
2698 if (err) {
2699 printk(KERN_CRIT "NTFS: Failed to register NTFS sysctls!\n");
2700 goto sysctl_err_out;
2703 err = register_filesystem(&ntfs_fs_type);
2704 if (!err) {
2705 ntfs_debug("NTFS driver registered successfully.");
2706 return 0; /* Success! */
2708 printk(KERN_CRIT "NTFS: Failed to register NTFS file system driver!\n");
2710 sysctl_err_out:
2711 kmem_cache_destroy(ntfs_big_inode_cache);
2712 big_inode_err_out:
2713 kmem_cache_destroy(ntfs_inode_cache);
2714 inode_err_out:
2715 kmem_cache_destroy(ntfs_name_cache);
2716 name_err_out:
2717 kmem_cache_destroy(ntfs_attr_ctx_cache);
2718 actx_err_out:
2719 kmem_cache_destroy(ntfs_index_ctx_cache);
2720 ictx_err_out:
2721 if (!err) {
2722 printk(KERN_CRIT "NTFS: Aborting NTFS file system driver "
2723 "registration...\n");
2724 err = -ENOMEM;
2726 return err;
2729 static void __exit exit_ntfs_fs(void)
2731 int err = 0;
2733 ntfs_debug("Unregistering NTFS driver.");
2735 unregister_filesystem(&ntfs_fs_type);
2737 if (kmem_cache_destroy(ntfs_big_inode_cache) && (err = 1))
2738 printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
2739 ntfs_big_inode_cache_name);
2740 if (kmem_cache_destroy(ntfs_inode_cache) && (err = 1))
2741 printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
2742 ntfs_inode_cache_name);
2743 if (kmem_cache_destroy(ntfs_name_cache) && (err = 1))
2744 printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
2745 ntfs_name_cache_name);
2746 if (kmem_cache_destroy(ntfs_attr_ctx_cache) && (err = 1))
2747 printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
2748 ntfs_attr_ctx_cache_name);
2749 if (kmem_cache_destroy(ntfs_index_ctx_cache) && (err = 1))
2750 printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
2751 ntfs_index_ctx_cache_name);
2752 if (err)
2753 printk(KERN_CRIT "NTFS: This causes memory to leak! There is "
2754 "probably a BUG in the driver! Please report "
2755 "you saw this message to "
2756 "linux-ntfs-dev@lists.sourceforge.net\n");
2757 /* Unregister the ntfs sysctls. */
2758 ntfs_sysctl(0);
2761 MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>");
2762 MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2004 Anton Altaparmakov");
2763 MODULE_VERSION(NTFS_VERSION);
2764 MODULE_LICENSE("GPL");
2765 #ifdef DEBUG
2766 module_param(debug_msgs, bool, 0);
2767 MODULE_PARM_DESC(debug_msgs, "Enable debug messages.");
2768 #endif
2770 module_init(init_ntfs_fs)
2771 module_exit(exit_ntfs_fs)