blob: 25fa1d1668da1dba28a2a31616cf8c4e51af0fd2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * super.c - NTFS kernel super block handling. Part of the Linux-NTFS project.
3 *
Anton Altaparmakovc002f422005-02-03 12:02:56 +00004 * Copyright (c) 2001-2005 Anton Altaparmakov
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright (c) 2001,2002 Richard Russon
6 *
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.
11 *
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.
16 *
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
21 */
22
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>
33
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"
43
Anton Altaparmakovc002f422005-02-03 12:02:56 +000044/* Number of mounted filesystems which have compression enabled. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static unsigned long ntfs_nr_compression_users;
46
47/* A global default upcase table and a corresponding reference count. */
48static ntfschar *default_upcase = NULL;
49static unsigned long ntfs_nr_upcase_users = 0;
50
51/* Error constants/strings used in inode.c::ntfs_show_options(). */
52typedef 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;
60
61const 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 }
67};
68
69/**
70 * simple_getbool -
71 *
72 * Copied from old ntfs driver (which copied from vfat driver).
73 */
74static int simple_getbool(char *s, BOOL *setval)
75{
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;
87}
88
89/**
90 * parse_options - parse the (re)mount options
91 * @vol: ntfs volume
92 * @opt: string containing the (re)mount options
93 *
94 * Parse the recognized options in @opt for the ntfs volume described by @vol.
95 */
96static BOOL parse_options(ntfs_volume *vol, char *opt)
97{
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;
Anton Altaparmakovc002f422005-02-03 12:02:56 +0000105 int show_sys_files = -1, case_sensitive = -1, disable_sparse = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct nls_table *nls_map = NULL, *old_nls;
107
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; \
117 } \
118 }
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; \
126 }
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; \
133 }
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; \
146 } \
147 if (!opt_array[_i].str || !*opt_array[_i].str) \
148 goto needs_val; \
149 }
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)
Anton Altaparmakovc002f422005-02-03 12:02:56 +0000165 else NTFS_GETOPT_BOOL("disable_sparse", disable_sparse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors,
167 on_errors_arr)
168 else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes"))
169 ntfs_warning(vol->sb, "Ignoring obsolete option %s.",
170 p);
171 else if (!strcmp(p, "nls") || !strcmp(p, "iocharset")) {
172 if (!strcmp(p, "iocharset"))
173 ntfs_warning(vol->sb, "Option iocharset is "
174 "deprecated. Please use "
175 "option nls=<charsetname> in "
176 "the future.");
177 if (!v || !*v)
178 goto needs_arg;
179use_utf8:
180 old_nls = nls_map;
181 nls_map = load_nls(v);
182 if (!nls_map) {
183 if (!old_nls) {
184 ntfs_error(vol->sb, "NLS character set "
185 "%s not found.", v);
186 return FALSE;
187 }
188 ntfs_error(vol->sb, "NLS character set %s not "
189 "found. Using previous one %s.",
190 v, old_nls->charset);
191 nls_map = old_nls;
192 } else /* nls_map */ {
193 if (old_nls)
194 unload_nls(old_nls);
195 }
196 } else if (!strcmp(p, "utf8")) {
197 BOOL val = FALSE;
198 ntfs_warning(vol->sb, "Option utf8 is no longer "
199 "supported, using option nls=utf8. Please "
200 "use option nls=utf8 in the future and "
201 "make sure utf8 is compiled either as a "
202 "module or into the kernel.");
203 if (!v || !*v)
204 val = TRUE;
205 else if (!simple_getbool(v, &val))
206 goto needs_bool;
207 if (val) {
208 v = utf8;
209 goto use_utf8;
210 }
211 } else {
212 ntfs_error(vol->sb, "Unrecognized mount option %s.", p);
213 if (errors < INT_MAX)
214 errors++;
215 }
216#undef NTFS_GETOPT_OPTIONS_ARRAY
217#undef NTFS_GETOPT_BOOL
218#undef NTFS_GETOPT
219#undef NTFS_GETOPT_WITH_DEFAULT
220 }
221no_mount_options:
222 if (errors && !sloppy)
223 return FALSE;
224 if (sloppy)
225 ntfs_warning(vol->sb, "Sloppy option given. Ignoring "
226 "unrecognized mount option(s) and continuing.");
227 /* Keep this first! */
228 if (on_errors != -1) {
229 if (!on_errors) {
230 ntfs_error(vol->sb, "Invalid errors option argument "
231 "or bug in options parser.");
232 return FALSE;
233 }
234 }
235 if (nls_map) {
236 if (vol->nls_map && vol->nls_map != nls_map) {
237 ntfs_error(vol->sb, "Cannot change NLS character set "
238 "on remount.");
239 return FALSE;
240 } /* else (!vol->nls_map) */
241 ntfs_debug("Using NLS character set %s.", nls_map->charset);
242 vol->nls_map = nls_map;
243 } else /* (!nls_map) */ {
244 if (!vol->nls_map) {
245 vol->nls_map = load_nls_default();
246 if (!vol->nls_map) {
247 ntfs_error(vol->sb, "Failed to load default "
248 "NLS character set.");
249 return FALSE;
250 }
251 ntfs_debug("Using default NLS character set (%s).",
252 vol->nls_map->charset);
253 }
254 }
255 if (mft_zone_multiplier != -1) {
256 if (vol->mft_zone_multiplier && vol->mft_zone_multiplier !=
257 mft_zone_multiplier) {
258 ntfs_error(vol->sb, "Cannot change mft_zone_multiplier "
259 "on remount.");
260 return FALSE;
261 }
262 if (mft_zone_multiplier < 1 || mft_zone_multiplier > 4) {
263 ntfs_error(vol->sb, "Invalid mft_zone_multiplier. "
264 "Using default value, i.e. 1.");
265 mft_zone_multiplier = 1;
266 }
267 vol->mft_zone_multiplier = mft_zone_multiplier;
268 }
269 if (!vol->mft_zone_multiplier)
270 vol->mft_zone_multiplier = 1;
271 if (on_errors != -1)
272 vol->on_errors = on_errors;
273 if (!vol->on_errors || vol->on_errors == ON_ERRORS_RECOVER)
274 vol->on_errors |= ON_ERRORS_CONTINUE;
275 if (uid != (uid_t)-1)
276 vol->uid = uid;
277 if (gid != (gid_t)-1)
278 vol->gid = gid;
279 if (fmask != (mode_t)-1)
280 vol->fmask = fmask;
281 if (dmask != (mode_t)-1)
282 vol->dmask = dmask;
283 if (show_sys_files != -1) {
284 if (show_sys_files)
285 NVolSetShowSystemFiles(vol);
286 else
287 NVolClearShowSystemFiles(vol);
288 }
289 if (case_sensitive != -1) {
290 if (case_sensitive)
291 NVolSetCaseSensitive(vol);
292 else
293 NVolClearCaseSensitive(vol);
294 }
Anton Altaparmakovc002f422005-02-03 12:02:56 +0000295 if (disable_sparse != -1) {
296 if (disable_sparse)
297 NVolClearSparseEnabled(vol);
298 else {
299 if (!NVolSparseEnabled(vol) &&
300 vol->major_ver && vol->major_ver < 3)
301 ntfs_warning(vol->sb, "Not enabling sparse "
302 "support due to NTFS volume "
303 "version %i.%i (need at least "
304 "version 3.0).", vol->major_ver,
305 vol->minor_ver);
306 else
307 NVolSetSparseEnabled(vol);
308 }
309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return TRUE;
311needs_arg:
312 ntfs_error(vol->sb, "The %s option requires an argument.", p);
313 return FALSE;
314needs_bool:
315 ntfs_error(vol->sb, "The %s option requires a boolean argument.", p);
316 return FALSE;
317needs_val:
318 ntfs_error(vol->sb, "Invalid %s option argument: %s", p, ov);
319 return FALSE;
320}
321
322#ifdef NTFS_RW
323
324/**
325 * ntfs_write_volume_flags - write new flags to the volume information flags
326 * @vol: ntfs volume on which to modify the flags
327 * @flags: new flags value for the volume information flags
328 *
329 * Internal function. You probably want to use ntfs_{set,clear}_volume_flags()
330 * instead (see below).
331 *
332 * Replace the volume information flags on the volume @vol with the value
333 * supplied in @flags. Note, this overwrites the volume information flags, so
334 * make sure to combine the flags you want to modify with the old flags and use
335 * the result when calling ntfs_write_volume_flags().
336 *
337 * Return 0 on success and -errno on error.
338 */
339static int ntfs_write_volume_flags(ntfs_volume *vol, const VOLUME_FLAGS flags)
340{
341 ntfs_inode *ni = NTFS_I(vol->vol_ino);
342 MFT_RECORD *m;
343 VOLUME_INFORMATION *vi;
344 ntfs_attr_search_ctx *ctx;
345 int err;
346
347 ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
348 le16_to_cpu(vol->vol_flags), le16_to_cpu(flags));
349 if (vol->vol_flags == flags)
350 goto done;
351 BUG_ON(!ni);
352 m = map_mft_record(ni);
353 if (IS_ERR(m)) {
354 err = PTR_ERR(m);
355 goto err_out;
356 }
357 ctx = ntfs_attr_get_search_ctx(ni, m);
358 if (!ctx) {
359 err = -ENOMEM;
360 goto put_unm_err_out;
361 }
362 err = ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
363 ctx);
364 if (err)
365 goto put_unm_err_out;
366 vi = (VOLUME_INFORMATION*)((u8*)ctx->attr +
367 le16_to_cpu(ctx->attr->data.resident.value_offset));
368 vol->vol_flags = vi->flags = flags;
369 flush_dcache_mft_record_page(ctx->ntfs_ino);
370 mark_mft_record_dirty(ctx->ntfs_ino);
371 ntfs_attr_put_search_ctx(ctx);
372 unmap_mft_record(ni);
373done:
374 ntfs_debug("Done.");
375 return 0;
376put_unm_err_out:
377 if (ctx)
378 ntfs_attr_put_search_ctx(ctx);
379 unmap_mft_record(ni);
380err_out:
381 ntfs_error(vol->sb, "Failed with error code %i.", -err);
382 return err;
383}
384
385/**
386 * ntfs_set_volume_flags - set bits in the volume information flags
387 * @vol: ntfs volume on which to modify the flags
388 * @flags: flags to set on the volume
389 *
390 * Set the bits in @flags in the volume information flags on the volume @vol.
391 *
392 * Return 0 on success and -errno on error.
393 */
394static inline int ntfs_set_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
395{
396 flags &= VOLUME_FLAGS_MASK;
397 return ntfs_write_volume_flags(vol, vol->vol_flags | flags);
398}
399
400/**
401 * ntfs_clear_volume_flags - clear bits in the volume information flags
402 * @vol: ntfs volume on which to modify the flags
403 * @flags: flags to clear on the volume
404 *
405 * Clear the bits in @flags in the volume information flags on the volume @vol.
406 *
407 * Return 0 on success and -errno on error.
408 */
409static inline int ntfs_clear_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
410{
411 flags &= VOLUME_FLAGS_MASK;
412 flags = vol->vol_flags & cpu_to_le16(~le16_to_cpu(flags));
413 return ntfs_write_volume_flags(vol, flags);
414}
415
416#endif /* NTFS_RW */
417
418/**
419 * ntfs_remount - change the mount options of a mounted ntfs filesystem
420 * @sb: superblock of mounted ntfs filesystem
421 * @flags: remount flags
422 * @opt: remount options string
423 *
424 * Change the mount options of an already mounted ntfs filesystem.
425 *
426 * NOTE: The VFS sets the @sb->s_flags remount flags to @flags after
427 * ntfs_remount() returns successfully (i.e. returns 0). Otherwise,
428 * @sb->s_flags are not changed.
429 */
430static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
431{
432 ntfs_volume *vol = NTFS_SB(sb);
433
434 ntfs_debug("Entering with remount options string: %s", opt);
435#ifndef NTFS_RW
436 /* For read-only compiled driver, enforce all read-only flags. */
437 *flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
438#else /* NTFS_RW */
439 /*
440 * For the read-write compiled driver, if we are remounting read-write,
441 * make sure there are no volume errors and that no unsupported volume
442 * flags are set. Also, empty the logfile journal as it would become
443 * stale as soon as something is written to the volume and mark the
444 * volume dirty so that chkdsk is run if the volume is not umounted
445 * cleanly. Finally, mark the quotas out of date so Windows rescans
446 * the volume on boot and updates them.
447 *
448 * When remounting read-only, mark the volume clean if no volume errors
449 * have occured.
450 */
451 if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
452 static const char *es = ". Cannot remount read-write.";
453
454 /* Remounting read-write. */
455 if (NVolErrors(vol)) {
456 ntfs_error(sb, "Volume has errors and is read-only%s",
457 es);
458 return -EROFS;
459 }
460 if (vol->vol_flags & VOLUME_IS_DIRTY) {
461 ntfs_error(sb, "Volume is dirty and read-only%s", es);
462 return -EROFS;
463 }
464 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
465 ntfs_error(sb, "Volume has unsupported flags set and "
466 "is read-only%s", es);
467 return -EROFS;
468 }
469 if (ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
470 ntfs_error(sb, "Failed to set dirty bit in volume "
471 "information flags%s", es);
472 return -EROFS;
473 }
474#if 0
475 // TODO: Enable this code once we start modifying anything that
476 // is different between NTFS 1.2 and 3.x...
477 /* Set NT4 compatibility flag on newer NTFS version volumes. */
478 if ((vol->major_ver > 1)) {
479 if (ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
480 ntfs_error(sb, "Failed to set NT4 "
481 "compatibility flag%s", es);
482 NVolSetErrors(vol);
483 return -EROFS;
484 }
485 }
486#endif
487 if (!ntfs_empty_logfile(vol->logfile_ino)) {
488 ntfs_error(sb, "Failed to empty journal $LogFile%s",
489 es);
490 NVolSetErrors(vol);
491 return -EROFS;
492 }
493 if (!ntfs_mark_quotas_out_of_date(vol)) {
494 ntfs_error(sb, "Failed to mark quotas out of date%s",
495 es);
496 NVolSetErrors(vol);
497 return -EROFS;
498 }
499 } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) {
500 /* Remounting read-only. */
501 if (!NVolErrors(vol)) {
502 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
503 ntfs_warning(sb, "Failed to clear dirty bit "
504 "in volume information "
505 "flags. Run chkdsk.");
506 }
507 }
508#endif /* NTFS_RW */
509
510 // TODO: Deal with *flags.
511
512 if (!parse_options(vol, opt))
513 return -EINVAL;
514 ntfs_debug("Done.");
515 return 0;
516}
517
518/**
519 * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector
520 * @sb: Super block of the device to which @b belongs.
521 * @b: Boot sector of device @sb to check.
522 * @silent: If TRUE, all output will be silenced.
523 *
524 * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot
525 * sector. Returns TRUE if it is valid and FALSE if not.
526 *
527 * @sb is only needed for warning/error output, i.e. it can be NULL when silent
528 * is TRUE.
529 */
530static BOOL is_boot_sector_ntfs(const struct super_block *sb,
531 const NTFS_BOOT_SECTOR *b, const BOOL silent)
532{
533 /*
534 * Check that checksum == sum of u32 values from b to the checksum
535 * field. If checksum is zero, no checking is done.
536 */
537 if ((void*)b < (void*)&b->checksum && b->checksum) {
538 le32 *u;
539 u32 i;
540
541 for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u)
542 i += le32_to_cpup(u);
543 if (le32_to_cpu(b->checksum) != i)
544 goto not_ntfs;
545 }
546 /* Check OEMidentifier is "NTFS " */
547 if (b->oem_id != magicNTFS)
548 goto not_ntfs;
549 /* Check bytes per sector value is between 256 and 4096. */
550 if (le16_to_cpu(b->bpb.bytes_per_sector) < 0x100 ||
551 le16_to_cpu(b->bpb.bytes_per_sector) > 0x1000)
552 goto not_ntfs;
553 /* Check sectors per cluster value is valid. */
554 switch (b->bpb.sectors_per_cluster) {
555 case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128:
556 break;
557 default:
558 goto not_ntfs;
559 }
560 /* Check the cluster size is not above 65536 bytes. */
561 if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) *
562 b->bpb.sectors_per_cluster > 0x10000)
563 goto not_ntfs;
564 /* Check reserved/unused fields are really zero. */
565 if (le16_to_cpu(b->bpb.reserved_sectors) ||
566 le16_to_cpu(b->bpb.root_entries) ||
567 le16_to_cpu(b->bpb.sectors) ||
568 le16_to_cpu(b->bpb.sectors_per_fat) ||
569 le32_to_cpu(b->bpb.large_sectors) || b->bpb.fats)
570 goto not_ntfs;
571 /* Check clusters per file mft record value is valid. */
572 if ((u8)b->clusters_per_mft_record < 0xe1 ||
573 (u8)b->clusters_per_mft_record > 0xf7)
574 switch (b->clusters_per_mft_record) {
575 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
576 break;
577 default:
578 goto not_ntfs;
579 }
580 /* Check clusters per index block value is valid. */
581 if ((u8)b->clusters_per_index_record < 0xe1 ||
582 (u8)b->clusters_per_index_record > 0xf7)
583 switch (b->clusters_per_index_record) {
584 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
585 break;
586 default:
587 goto not_ntfs;
588 }
589 /*
590 * Check for valid end of sector marker. We will work without it, but
591 * many BIOSes will refuse to boot from a bootsector if the magic is
592 * incorrect, so we emit a warning.
593 */
594 if (!silent && b->end_of_sector_marker != cpu_to_le16(0xaa55))
595 ntfs_warning(sb, "Invalid end of sector marker.");
596 return TRUE;
597not_ntfs:
598 return FALSE;
599}
600
601/**
602 * read_ntfs_boot_sector - read the NTFS boot sector of a device
603 * @sb: super block of device to read the boot sector from
604 * @silent: if true, suppress all output
605 *
606 * Reads the boot sector from the device and validates it. If that fails, tries
607 * to read the backup boot sector, first from the end of the device a-la NT4 and
608 * later and then from the middle of the device a-la NT3.51 and before.
609 *
610 * If a valid boot sector is found but it is not the primary boot sector, we
611 * repair the primary boot sector silently (unless the device is read-only or
612 * the primary boot sector is not accessible).
613 *
614 * NOTE: To call this function, @sb must have the fields s_dev, the ntfs super
615 * block (u.ntfs_sb), nr_blocks and the device flags (s_flags) initialized
616 * to their respective values.
617 *
618 * Return the unlocked buffer head containing the boot sector or NULL on error.
619 */
620static struct buffer_head *read_ntfs_boot_sector(struct super_block *sb,
621 const int silent)
622{
623 const char *read_err_str = "Unable to read %s boot sector.";
624 struct buffer_head *bh_primary, *bh_backup;
625 long nr_blocks = NTFS_SB(sb)->nr_blocks;
626
627 /* Try to read primary boot sector. */
628 if ((bh_primary = sb_bread(sb, 0))) {
629 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
630 bh_primary->b_data, silent))
631 return bh_primary;
632 if (!silent)
633 ntfs_error(sb, "Primary boot sector is invalid.");
634 } else if (!silent)
635 ntfs_error(sb, read_err_str, "primary");
636 if (!(NTFS_SB(sb)->on_errors & ON_ERRORS_RECOVER)) {
637 if (bh_primary)
638 brelse(bh_primary);
639 if (!silent)
640 ntfs_error(sb, "Mount option errors=recover not used. "
641 "Aborting without trying to recover.");
642 return NULL;
643 }
644 /* Try to read NT4+ backup boot sector. */
645 if ((bh_backup = sb_bread(sb, nr_blocks - 1))) {
646 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
647 bh_backup->b_data, silent))
648 goto hotfix_primary_boot_sector;
649 brelse(bh_backup);
650 } else if (!silent)
651 ntfs_error(sb, read_err_str, "backup");
652 /* Try to read NT3.51- backup boot sector. */
653 if ((bh_backup = sb_bread(sb, nr_blocks >> 1))) {
654 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
655 bh_backup->b_data, silent))
656 goto hotfix_primary_boot_sector;
657 if (!silent)
658 ntfs_error(sb, "Could not find a valid backup boot "
659 "sector.");
660 brelse(bh_backup);
661 } else if (!silent)
662 ntfs_error(sb, read_err_str, "backup");
663 /* We failed. Cleanup and return. */
664 if (bh_primary)
665 brelse(bh_primary);
666 return NULL;
667hotfix_primary_boot_sector:
668 if (bh_primary) {
669 /*
670 * If we managed to read sector zero and the volume is not
671 * read-only, copy the found, valid backup boot sector to the
672 * primary boot sector.
673 */
674 if (!(sb->s_flags & MS_RDONLY)) {
675 ntfs_warning(sb, "Hot-fix: Recovering invalid primary "
676 "boot sector from backup copy.");
677 memcpy(bh_primary->b_data, bh_backup->b_data,
678 sb->s_blocksize);
679 mark_buffer_dirty(bh_primary);
680 sync_dirty_buffer(bh_primary);
681 if (buffer_uptodate(bh_primary)) {
682 brelse(bh_backup);
683 return bh_primary;
684 }
685 ntfs_error(sb, "Hot-fix: Device write error while "
686 "recovering primary boot sector.");
687 } else {
688 ntfs_warning(sb, "Hot-fix: Recovery of primary boot "
689 "sector failed: Read-only mount.");
690 }
691 brelse(bh_primary);
692 }
693 ntfs_warning(sb, "Using backup boot sector.");
694 return bh_backup;
695}
696
697/**
698 * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol
699 * @vol: volume structure to initialise with data from boot sector
700 * @b: boot sector to parse
701 *
702 * Parse the ntfs boot sector @b and store all imporant information therein in
703 * the ntfs super block @vol. Return TRUE on success and FALSE on error.
704 */
705static BOOL parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
706{
707 unsigned int sectors_per_cluster_bits, nr_hidden_sects;
708 int clusters_per_mft_record, clusters_per_index_record;
709 s64 ll;
710
711 vol->sector_size = le16_to_cpu(b->bpb.bytes_per_sector);
712 vol->sector_size_bits = ffs(vol->sector_size) - 1;
713 ntfs_debug("vol->sector_size = %i (0x%x)", vol->sector_size,
714 vol->sector_size);
715 ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits,
716 vol->sector_size_bits);
717 if (vol->sector_size != vol->sb->s_blocksize)
718 ntfs_warning(vol->sb, "The boot sector indicates a sector size "
719 "different from the device sector size.");
720 ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster);
721 sectors_per_cluster_bits = ffs(b->bpb.sectors_per_cluster) - 1;
722 ntfs_debug("sectors_per_cluster_bits = 0x%x",
723 sectors_per_cluster_bits);
724 nr_hidden_sects = le32_to_cpu(b->bpb.hidden_sectors);
725 ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects);
726 vol->cluster_size = vol->sector_size << sectors_per_cluster_bits;
727 vol->cluster_size_mask = vol->cluster_size - 1;
728 vol->cluster_size_bits = ffs(vol->cluster_size) - 1;
729 ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size,
730 vol->cluster_size);
731 ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask);
732 ntfs_debug("vol->cluster_size_bits = %i (0x%x)",
733 vol->cluster_size_bits, vol->cluster_size_bits);
734 if (vol->sector_size > vol->cluster_size) {
735 ntfs_error(vol->sb, "Sector sizes above the cluster size are "
736 "not supported. Sorry.");
737 return FALSE;
738 }
739 if (vol->sb->s_blocksize > vol->cluster_size) {
740 ntfs_error(vol->sb, "Cluster sizes smaller than the device "
741 "sector size are not supported. Sorry.");
742 return FALSE;
743 }
744 clusters_per_mft_record = b->clusters_per_mft_record;
745 ntfs_debug("clusters_per_mft_record = %i (0x%x)",
746 clusters_per_mft_record, clusters_per_mft_record);
747 if (clusters_per_mft_record > 0)
748 vol->mft_record_size = vol->cluster_size <<
749 (ffs(clusters_per_mft_record) - 1);
750 else
751 /*
752 * When mft_record_size < cluster_size, clusters_per_mft_record
753 * = -log2(mft_record_size) bytes. mft_record_size normaly is
754 * 1024 bytes, which is encoded as 0xF6 (-10 in decimal).
755 */
756 vol->mft_record_size = 1 << -clusters_per_mft_record;
757 vol->mft_record_size_mask = vol->mft_record_size - 1;
758 vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1;
759 ntfs_debug("vol->mft_record_size = %i (0x%x)", vol->mft_record_size,
760 vol->mft_record_size);
761 ntfs_debug("vol->mft_record_size_mask = 0x%x",
762 vol->mft_record_size_mask);
763 ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
764 vol->mft_record_size_bits, vol->mft_record_size_bits);
765 /*
766 * We cannot support mft record sizes above the PAGE_CACHE_SIZE since
767 * we store $MFT/$DATA, the table of mft records in the page cache.
768 */
769 if (vol->mft_record_size > PAGE_CACHE_SIZE) {
770 ntfs_error(vol->sb, "Mft record size %i (0x%x) exceeds the "
771 "page cache size on your system %lu (0x%lx). "
772 "This is not supported. Sorry.",
773 vol->mft_record_size, vol->mft_record_size,
774 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE);
775 return FALSE;
776 }
777 clusters_per_index_record = b->clusters_per_index_record;
778 ntfs_debug("clusters_per_index_record = %i (0x%x)",
779 clusters_per_index_record, clusters_per_index_record);
780 if (clusters_per_index_record > 0)
781 vol->index_record_size = vol->cluster_size <<
782 (ffs(clusters_per_index_record) - 1);
783 else
784 /*
785 * When index_record_size < cluster_size,
786 * clusters_per_index_record = -log2(index_record_size) bytes.
787 * index_record_size normaly equals 4096 bytes, which is
788 * encoded as 0xF4 (-12 in decimal).
789 */
790 vol->index_record_size = 1 << -clusters_per_index_record;
791 vol->index_record_size_mask = vol->index_record_size - 1;
792 vol->index_record_size_bits = ffs(vol->index_record_size) - 1;
793 ntfs_debug("vol->index_record_size = %i (0x%x)",
794 vol->index_record_size, vol->index_record_size);
795 ntfs_debug("vol->index_record_size_mask = 0x%x",
796 vol->index_record_size_mask);
797 ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
798 vol->index_record_size_bits,
799 vol->index_record_size_bits);
800 /*
801 * Get the size of the volume in clusters and check for 64-bit-ness.
802 * Windows currently only uses 32 bits to save the clusters so we do
803 * the same as it is much faster on 32-bit CPUs.
804 */
805 ll = sle64_to_cpu(b->number_of_sectors) >> sectors_per_cluster_bits;
806 if ((u64)ll >= 1ULL << 32) {
807 ntfs_error(vol->sb, "Cannot handle 64-bit clusters. Sorry.");
808 return FALSE;
809 }
810 vol->nr_clusters = ll;
811 ntfs_debug("vol->nr_clusters = 0x%llx", (long long)vol->nr_clusters);
812 /*
813 * On an architecture where unsigned long is 32-bits, we restrict the
814 * volume size to 2TiB (2^41). On a 64-bit architecture, the compiler
815 * will hopefully optimize the whole check away.
816 */
817 if (sizeof(unsigned long) < 8) {
818 if ((ll << vol->cluster_size_bits) >= (1ULL << 41)) {
819 ntfs_error(vol->sb, "Volume size (%lluTiB) is too "
820 "large for this architecture. "
821 "Maximum supported is 2TiB. Sorry.",
822 (unsigned long long)ll >> (40 -
823 vol->cluster_size_bits));
824 return FALSE;
825 }
826 }
827 ll = sle64_to_cpu(b->mft_lcn);
828 if (ll >= vol->nr_clusters) {
829 ntfs_error(vol->sb, "MFT LCN is beyond end of volume. Weird.");
830 return FALSE;
831 }
832 vol->mft_lcn = ll;
833 ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol->mft_lcn);
834 ll = sle64_to_cpu(b->mftmirr_lcn);
835 if (ll >= vol->nr_clusters) {
836 ntfs_error(vol->sb, "MFTMirr LCN is beyond end of volume. "
837 "Weird.");
838 return FALSE;
839 }
840 vol->mftmirr_lcn = ll;
841 ntfs_debug("vol->mftmirr_lcn = 0x%llx", (long long)vol->mftmirr_lcn);
842#ifdef NTFS_RW
843 /*
844 * Work out the size of the mft mirror in number of mft records. If the
845 * cluster size is less than or equal to the size taken by four mft
846 * records, the mft mirror stores the first four mft records. If the
847 * cluster size is bigger than the size taken by four mft records, the
848 * mft mirror contains as many mft records as will fit into one
849 * cluster.
850 */
851 if (vol->cluster_size <= (4 << vol->mft_record_size_bits))
852 vol->mftmirr_size = 4;
853 else
854 vol->mftmirr_size = vol->cluster_size >>
855 vol->mft_record_size_bits;
856 ntfs_debug("vol->mftmirr_size = %i", vol->mftmirr_size);
857#endif /* NTFS_RW */
858 vol->serial_no = le64_to_cpu(b->volume_serial_number);
859 ntfs_debug("vol->serial_no = 0x%llx",
860 (unsigned long long)vol->serial_no);
861 return TRUE;
862}
863
864/**
865 * ntfs_setup_allocators - initialize the cluster and mft allocators
866 * @vol: volume structure for which to setup the allocators
867 *
868 * Setup the cluster (lcn) and mft allocators to the starting values.
869 */
870static void ntfs_setup_allocators(ntfs_volume *vol)
871{
872#ifdef NTFS_RW
873 LCN mft_zone_size, mft_lcn;
874#endif /* NTFS_RW */
875
876 ntfs_debug("vol->mft_zone_multiplier = 0x%x",
877 vol->mft_zone_multiplier);
878#ifdef NTFS_RW
879 /* Determine the size of the MFT zone. */
880 mft_zone_size = vol->nr_clusters;
881 switch (vol->mft_zone_multiplier) { /* % of volume size in clusters */
882 case 4:
883 mft_zone_size >>= 1; /* 50% */
884 break;
885 case 3:
886 mft_zone_size = (mft_zone_size +
887 (mft_zone_size >> 1)) >> 2; /* 37.5% */
888 break;
889 case 2:
890 mft_zone_size >>= 2; /* 25% */
891 break;
892 /* case 1: */
893 default:
894 mft_zone_size >>= 3; /* 12.5% */
895 break;
896 }
897 /* Setup the mft zone. */
898 vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn;
899 ntfs_debug("vol->mft_zone_pos = 0x%llx",
900 (unsigned long long)vol->mft_zone_pos);
901 /*
902 * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
903 * source) and if the actual mft_lcn is in the expected place or even
904 * further to the front of the volume, extend the mft_zone to cover the
905 * beginning of the volume as well. This is in order to protect the
906 * area reserved for the mft bitmap as well within the mft_zone itself.
907 * On non-standard volumes we do not protect it as the overhead would
908 * be higher than the speed increase we would get by doing it.
909 */
910 mft_lcn = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size;
911 if (mft_lcn * vol->cluster_size < 16 * 1024)
912 mft_lcn = (16 * 1024 + vol->cluster_size - 1) /
913 vol->cluster_size;
914 if (vol->mft_zone_start <= mft_lcn)
915 vol->mft_zone_start = 0;
916 ntfs_debug("vol->mft_zone_start = 0x%llx",
917 (unsigned long long)vol->mft_zone_start);
918 /*
919 * Need to cap the mft zone on non-standard volumes so that it does
920 * not point outside the boundaries of the volume. We do this by
921 * halving the zone size until we are inside the volume.
922 */
923 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
924 while (vol->mft_zone_end >= vol->nr_clusters) {
925 mft_zone_size >>= 1;
926 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
927 }
928 ntfs_debug("vol->mft_zone_end = 0x%llx",
929 (unsigned long long)vol->mft_zone_end);
930 /*
931 * Set the current position within each data zone to the start of the
932 * respective zone.
933 */
934 vol->data1_zone_pos = vol->mft_zone_end;
935 ntfs_debug("vol->data1_zone_pos = 0x%llx",
936 (unsigned long long)vol->data1_zone_pos);
937 vol->data2_zone_pos = 0;
938 ntfs_debug("vol->data2_zone_pos = 0x%llx",
939 (unsigned long long)vol->data2_zone_pos);
940
941 /* Set the mft data allocation position to mft record 24. */
942 vol->mft_data_pos = 24;
943 ntfs_debug("vol->mft_data_pos = 0x%llx",
944 (unsigned long long)vol->mft_data_pos);
945#endif /* NTFS_RW */
946}
947
948#ifdef NTFS_RW
949
950/**
951 * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume
952 * @vol: ntfs super block describing device whose mft mirror to load
953 *
954 * Return TRUE on success or FALSE on error.
955 */
956static BOOL load_and_init_mft_mirror(ntfs_volume *vol)
957{
958 struct inode *tmp_ino;
959 ntfs_inode *tmp_ni;
960
961 ntfs_debug("Entering.");
962 /* Get mft mirror inode. */
963 tmp_ino = ntfs_iget(vol->sb, FILE_MFTMirr);
964 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
965 if (!IS_ERR(tmp_ino))
966 iput(tmp_ino);
967 /* Caller will display error message. */
968 return FALSE;
969 }
970 /*
971 * Re-initialize some specifics about $MFTMirr's inode as
972 * ntfs_read_inode() will have set up the default ones.
973 */
974 /* Set uid and gid to root. */
975 tmp_ino->i_uid = tmp_ino->i_gid = 0;
976 /* Regular file. No access for anyone. */
977 tmp_ino->i_mode = S_IFREG;
978 /* No VFS initiated operations allowed for $MFTMirr. */
979 tmp_ino->i_op = &ntfs_empty_inode_ops;
980 tmp_ino->i_fop = &ntfs_empty_file_ops;
981 /* Put in our special address space operations. */
982 tmp_ino->i_mapping->a_ops = &ntfs_mst_aops;
983 tmp_ni = NTFS_I(tmp_ino);
984 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
985 NInoSetMstProtected(tmp_ni);
Anton Altaparmakovc002f422005-02-03 12:02:56 +0000986 NInoSetSparseDisabled(tmp_ni);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 /*
988 * Set up our little cheat allowing us to reuse the async read io
989 * completion handler for directories.
990 */
991 tmp_ni->itype.index.block_size = vol->mft_record_size;
992 tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits;
993 vol->mftmirr_ino = tmp_ino;
994 ntfs_debug("Done.");
995 return TRUE;
996}
997
998/**
999 * check_mft_mirror - compare contents of the mft mirror with the mft
1000 * @vol: ntfs super block describing device whose mft mirror to check
1001 *
1002 * Return TRUE on success or FALSE on error.
1003 *
1004 * Note, this function also results in the mft mirror runlist being completely
1005 * mapped into memory. The mft mirror write code requires this and will BUG()
1006 * should it find an unmapped runlist element.
1007 */
1008static BOOL check_mft_mirror(ntfs_volume *vol)
1009{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 struct super_block *sb = vol->sb;
1011 ntfs_inode *mirr_ni;
1012 struct page *mft_page, *mirr_page;
1013 u8 *kmft, *kmirr;
1014 runlist_element *rl, rl2[2];
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001015 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 int mrecs_per_page, i;
1017
1018 ntfs_debug("Entering.");
1019 /* Compare contents of $MFT and $MFTMirr. */
1020 mrecs_per_page = PAGE_CACHE_SIZE / vol->mft_record_size;
1021 BUG_ON(!mrecs_per_page);
1022 BUG_ON(!vol->mftmirr_size);
1023 mft_page = mirr_page = NULL;
1024 kmft = kmirr = NULL;
1025 index = i = 0;
1026 do {
1027 u32 bytes;
1028
1029 /* Switch pages if necessary. */
1030 if (!(i % mrecs_per_page)) {
1031 if (index) {
1032 ntfs_unmap_page(mft_page);
1033 ntfs_unmap_page(mirr_page);
1034 }
1035 /* Get the $MFT page. */
1036 mft_page = ntfs_map_page(vol->mft_ino->i_mapping,
1037 index);
1038 if (IS_ERR(mft_page)) {
1039 ntfs_error(sb, "Failed to read $MFT.");
1040 return FALSE;
1041 }
1042 kmft = page_address(mft_page);
1043 /* Get the $MFTMirr page. */
1044 mirr_page = ntfs_map_page(vol->mftmirr_ino->i_mapping,
1045 index);
1046 if (IS_ERR(mirr_page)) {
1047 ntfs_error(sb, "Failed to read $MFTMirr.");
1048 goto mft_unmap_out;
1049 }
1050 kmirr = page_address(mirr_page);
1051 ++index;
1052 }
1053 /* Make sure the record is ok. */
1054 if (ntfs_is_baad_recordp((le32*)kmft)) {
1055 ntfs_error(sb, "Incomplete multi sector transfer "
1056 "detected in mft record %i.", i);
1057mm_unmap_out:
1058 ntfs_unmap_page(mirr_page);
1059mft_unmap_out:
1060 ntfs_unmap_page(mft_page);
1061 return FALSE;
1062 }
1063 if (ntfs_is_baad_recordp((le32*)kmirr)) {
1064 ntfs_error(sb, "Incomplete multi sector transfer "
1065 "detected in mft mirror record %i.", i);
1066 goto mm_unmap_out;
1067 }
1068 /* Get the amount of data in the current record. */
1069 bytes = le32_to_cpu(((MFT_RECORD*)kmft)->bytes_in_use);
1070 if (!bytes || bytes > vol->mft_record_size) {
1071 bytes = le32_to_cpu(((MFT_RECORD*)kmirr)->bytes_in_use);
1072 if (!bytes || bytes > vol->mft_record_size)
1073 bytes = vol->mft_record_size;
1074 }
1075 /* Compare the two records. */
1076 if (memcmp(kmft, kmirr, bytes)) {
1077 ntfs_error(sb, "$MFT and $MFTMirr (record %i) do not "
1078 "match. Run ntfsfix or chkdsk.", i);
1079 goto mm_unmap_out;
1080 }
1081 kmft += vol->mft_record_size;
1082 kmirr += vol->mft_record_size;
1083 } while (++i < vol->mftmirr_size);
1084 /* Release the last pages. */
1085 ntfs_unmap_page(mft_page);
1086 ntfs_unmap_page(mirr_page);
1087
1088 /* Construct the mft mirror runlist by hand. */
1089 rl2[0].vcn = 0;
1090 rl2[0].lcn = vol->mftmirr_lcn;
1091 rl2[0].length = (vol->mftmirr_size * vol->mft_record_size +
1092 vol->cluster_size - 1) / vol->cluster_size;
1093 rl2[1].vcn = rl2[0].length;
1094 rl2[1].lcn = LCN_ENOENT;
1095 rl2[1].length = 0;
1096 /*
1097 * Because we have just read all of the mft mirror, we know we have
1098 * mapped the full runlist for it.
1099 */
1100 mirr_ni = NTFS_I(vol->mftmirr_ino);
1101 down_read(&mirr_ni->runlist.lock);
1102 rl = mirr_ni->runlist.rl;
1103 /* Compare the two runlists. They must be identical. */
1104 i = 0;
1105 do {
1106 if (rl2[i].vcn != rl[i].vcn || rl2[i].lcn != rl[i].lcn ||
1107 rl2[i].length != rl[i].length) {
1108 ntfs_error(sb, "$MFTMirr location mismatch. "
1109 "Run chkdsk.");
1110 up_read(&mirr_ni->runlist.lock);
1111 return FALSE;
1112 }
1113 } while (rl2[i++].length);
1114 up_read(&mirr_ni->runlist.lock);
1115 ntfs_debug("Done.");
1116 return TRUE;
1117}
1118
1119/**
1120 * load_and_check_logfile - load and check the logfile inode for a volume
1121 * @vol: ntfs super block describing device whose logfile to load
1122 *
1123 * Return TRUE on success or FALSE on error.
1124 */
1125static BOOL load_and_check_logfile(ntfs_volume *vol)
1126{
1127 struct inode *tmp_ino;
1128
1129 ntfs_debug("Entering.");
1130 tmp_ino = ntfs_iget(vol->sb, FILE_LogFile);
1131 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1132 if (!IS_ERR(tmp_ino))
1133 iput(tmp_ino);
1134 /* Caller will display error message. */
1135 return FALSE;
1136 }
1137 if (!ntfs_check_logfile(tmp_ino)) {
1138 iput(tmp_ino);
1139 /* ntfs_check_logfile() will have displayed error output. */
1140 return FALSE;
1141 }
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001142 NInoSetSparseDisabled(NTFS_I(tmp_ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 vol->logfile_ino = tmp_ino;
1144 ntfs_debug("Done.");
1145 return TRUE;
1146}
1147
1148/**
1149 * load_and_init_quota - load and setup the quota file for a volume if present
1150 * @vol: ntfs super block describing device whose quota file to load
1151 *
1152 * Return TRUE on success or FALSE on error. If $Quota is not present, we
1153 * leave vol->quota_ino as NULL and return success.
1154 */
1155static BOOL load_and_init_quota(ntfs_volume *vol)
1156{
1157 MFT_REF mref;
1158 struct inode *tmp_ino;
1159 ntfs_name *name = NULL;
1160 static const ntfschar Quota[7] = { const_cpu_to_le16('$'),
1161 const_cpu_to_le16('Q'), const_cpu_to_le16('u'),
1162 const_cpu_to_le16('o'), const_cpu_to_le16('t'),
1163 const_cpu_to_le16('a'), 0 };
1164 static ntfschar Q[3] = { const_cpu_to_le16('$'),
1165 const_cpu_to_le16('Q'), 0 };
1166
1167 ntfs_debug("Entering.");
1168 /*
1169 * Find the inode number for the quota file by looking up the filename
1170 * $Quota in the extended system files directory $Extend.
1171 */
1172 down(&vol->extend_ino->i_sem);
1173 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), Quota, 6,
1174 &name);
1175 up(&vol->extend_ino->i_sem);
1176 if (IS_ERR_MREF(mref)) {
1177 /*
1178 * If the file does not exist, quotas are disabled and have
1179 * never been enabled on this volume, just return success.
1180 */
1181 if (MREF_ERR(mref) == -ENOENT) {
1182 ntfs_debug("$Quota not present. Volume does not have "
1183 "quotas enabled.");
1184 /*
1185 * No need to try to set quotas out of date if they are
1186 * not enabled.
1187 */
1188 NVolSetQuotaOutOfDate(vol);
1189 return TRUE;
1190 }
1191 /* A real error occured. */
1192 ntfs_error(vol->sb, "Failed to find inode number for $Quota.");
1193 return FALSE;
1194 }
1195 /* We do not care for the type of match that was found. */
Jesper Juhl251c8422005-04-04 14:59:56 +01001196 kfree(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 /* Get the inode. */
1198 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1199 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1200 if (!IS_ERR(tmp_ino))
1201 iput(tmp_ino);
1202 ntfs_error(vol->sb, "Failed to load $Quota.");
1203 return FALSE;
1204 }
1205 vol->quota_ino = tmp_ino;
1206 /* Get the $Q index allocation attribute. */
1207 tmp_ino = ntfs_index_iget(vol->quota_ino, Q, 2);
1208 if (IS_ERR(tmp_ino)) {
1209 ntfs_error(vol->sb, "Failed to load $Quota/$Q index.");
1210 return FALSE;
1211 }
1212 vol->quota_q_ino = tmp_ino;
1213 ntfs_debug("Done.");
1214 return TRUE;
1215}
1216
1217/**
1218 * load_and_init_attrdef - load the attribute definitions table for a volume
1219 * @vol: ntfs super block describing device whose attrdef to load
1220 *
1221 * Return TRUE on success or FALSE on error.
1222 */
1223static BOOL load_and_init_attrdef(ntfs_volume *vol)
1224{
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001225 loff_t i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 struct super_block *sb = vol->sb;
1227 struct inode *ino;
1228 struct page *page;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001229 pgoff_t index, max_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 unsigned int size;
1231
1232 ntfs_debug("Entering.");
1233 /* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */
1234 ino = ntfs_iget(sb, FILE_AttrDef);
1235 if (IS_ERR(ino) || is_bad_inode(ino)) {
1236 if (!IS_ERR(ino))
1237 iput(ino);
1238 goto failed;
1239 }
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001240 NInoSetSparseDisabled(NTFS_I(ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001242 i_size = i_size_read(ino);
1243 if (i_size <= 0 || i_size > 0x7fffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 goto iput_failed;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001245 vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (!vol->attrdef)
1247 goto iput_failed;
1248 index = 0;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001249 max_index = i_size >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 size = PAGE_CACHE_SIZE;
1251 while (index < max_index) {
1252 /* Read the attrdef table and copy it into the linear buffer. */
1253read_partial_attrdef_page:
1254 page = ntfs_map_page(ino->i_mapping, index);
1255 if (IS_ERR(page))
1256 goto free_iput_failed;
1257 memcpy((u8*)vol->attrdef + (index++ << PAGE_CACHE_SHIFT),
1258 page_address(page), size);
1259 ntfs_unmap_page(page);
1260 };
1261 if (size == PAGE_CACHE_SIZE) {
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001262 size = i_size & ~PAGE_CACHE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (size)
1264 goto read_partial_attrdef_page;
1265 }
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001266 vol->attrdef_size = i_size;
1267 ntfs_debug("Read %llu bytes from $AttrDef.", i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 iput(ino);
1269 return TRUE;
1270free_iput_failed:
1271 ntfs_free(vol->attrdef);
1272 vol->attrdef = NULL;
1273iput_failed:
1274 iput(ino);
1275failed:
1276 ntfs_error(sb, "Failed to initialize attribute definition table.");
1277 return FALSE;
1278}
1279
1280#endif /* NTFS_RW */
1281
1282/**
1283 * load_and_init_upcase - load the upcase table for an ntfs volume
1284 * @vol: ntfs super block describing device whose upcase to load
1285 *
1286 * Return TRUE on success or FALSE on error.
1287 */
1288static BOOL load_and_init_upcase(ntfs_volume *vol)
1289{
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001290 loff_t i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 struct super_block *sb = vol->sb;
1292 struct inode *ino;
1293 struct page *page;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001294 pgoff_t index, max_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 unsigned int size;
1296 int i, max;
1297
1298 ntfs_debug("Entering.");
1299 /* Read upcase table and setup vol->upcase and vol->upcase_len. */
1300 ino = ntfs_iget(sb, FILE_UpCase);
1301 if (IS_ERR(ino) || is_bad_inode(ino)) {
1302 if (!IS_ERR(ino))
1303 iput(ino);
1304 goto upcase_failed;
1305 }
1306 /*
1307 * The upcase size must not be above 64k Unicode characters, must not
1308 * be zero and must be a multiple of sizeof(ntfschar).
1309 */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001310 i_size = i_size_read(ino);
1311 if (!i_size || i_size & (sizeof(ntfschar) - 1) ||
1312 i_size > 64ULL * 1024 * sizeof(ntfschar))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 goto iput_upcase_failed;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001314 vol->upcase = (ntfschar*)ntfs_malloc_nofs(i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (!vol->upcase)
1316 goto iput_upcase_failed;
1317 index = 0;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001318 max_index = i_size >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 size = PAGE_CACHE_SIZE;
1320 while (index < max_index) {
1321 /* Read the upcase table and copy it into the linear buffer. */
1322read_partial_upcase_page:
1323 page = ntfs_map_page(ino->i_mapping, index);
1324 if (IS_ERR(page))
1325 goto iput_upcase_failed;
1326 memcpy((char*)vol->upcase + (index++ << PAGE_CACHE_SHIFT),
1327 page_address(page), size);
1328 ntfs_unmap_page(page);
1329 };
1330 if (size == PAGE_CACHE_SIZE) {
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001331 size = i_size & ~PAGE_CACHE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (size)
1333 goto read_partial_upcase_page;
1334 }
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001335 vol->upcase_len = i_size >> UCHAR_T_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001337 i_size, 64 * 1024 * sizeof(ntfschar));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 iput(ino);
1339 down(&ntfs_lock);
1340 if (!default_upcase) {
1341 ntfs_debug("Using volume specified $UpCase since default is "
1342 "not present.");
1343 up(&ntfs_lock);
1344 return TRUE;
1345 }
1346 max = default_upcase_len;
1347 if (max > vol->upcase_len)
1348 max = vol->upcase_len;
1349 for (i = 0; i < max; i++)
1350 if (vol->upcase[i] != default_upcase[i])
1351 break;
1352 if (i == max) {
1353 ntfs_free(vol->upcase);
1354 vol->upcase = default_upcase;
1355 vol->upcase_len = max;
1356 ntfs_nr_upcase_users++;
1357 up(&ntfs_lock);
1358 ntfs_debug("Volume specified $UpCase matches default. Using "
1359 "default.");
1360 return TRUE;
1361 }
1362 up(&ntfs_lock);
1363 ntfs_debug("Using volume specified $UpCase since it does not match "
1364 "the default.");
1365 return TRUE;
1366iput_upcase_failed:
1367 iput(ino);
1368 ntfs_free(vol->upcase);
1369 vol->upcase = NULL;
1370upcase_failed:
1371 down(&ntfs_lock);
1372 if (default_upcase) {
1373 vol->upcase = default_upcase;
1374 vol->upcase_len = default_upcase_len;
1375 ntfs_nr_upcase_users++;
1376 up(&ntfs_lock);
1377 ntfs_error(sb, "Failed to load $UpCase from the volume. Using "
1378 "default.");
1379 return TRUE;
1380 }
1381 up(&ntfs_lock);
1382 ntfs_error(sb, "Failed to initialize upcase table.");
1383 return FALSE;
1384}
1385
1386/**
1387 * load_system_files - open the system files using normal functions
1388 * @vol: ntfs super block describing device whose system files to load
1389 *
1390 * Open the system files with normal access functions and complete setting up
1391 * the ntfs super block @vol.
1392 *
1393 * Return TRUE on success or FALSE on error.
1394 */
1395static BOOL load_system_files(ntfs_volume *vol)
1396{
1397 struct super_block *sb = vol->sb;
1398 MFT_RECORD *m;
1399 VOLUME_INFORMATION *vi;
1400 ntfs_attr_search_ctx *ctx;
1401
1402 ntfs_debug("Entering.");
1403#ifdef NTFS_RW
1404 /* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */
1405 if (!load_and_init_mft_mirror(vol) || !check_mft_mirror(vol)) {
1406 static const char *es1 = "Failed to load $MFTMirr";
1407 static const char *es2 = "$MFTMirr does not match $MFT";
1408 static const char *es3 = ". Run ntfsfix and/or chkdsk.";
1409
1410 /* If a read-write mount, convert it to a read-only mount. */
1411 if (!(sb->s_flags & MS_RDONLY)) {
1412 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1413 ON_ERRORS_CONTINUE))) {
1414 ntfs_error(sb, "%s and neither on_errors="
1415 "continue nor on_errors="
1416 "remount-ro was specified%s",
1417 !vol->mftmirr_ino ? es1 : es2,
1418 es3);
1419 goto iput_mirr_err_out;
1420 }
1421 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1422 ntfs_error(sb, "%s. Mounting read-only%s",
1423 !vol->mftmirr_ino ? es1 : es2, es3);
1424 } else
1425 ntfs_warning(sb, "%s. Will not be able to remount "
1426 "read-write%s",
1427 !vol->mftmirr_ino ? es1 : es2, es3);
1428 /* This will prevent a read-write remount. */
1429 NVolSetErrors(vol);
1430 }
1431#endif /* NTFS_RW */
1432 /* Get mft bitmap attribute inode. */
1433 vol->mftbmp_ino = ntfs_attr_iget(vol->mft_ino, AT_BITMAP, NULL, 0);
1434 if (IS_ERR(vol->mftbmp_ino)) {
1435 ntfs_error(sb, "Failed to load $MFT/$BITMAP attribute.");
1436 goto iput_mirr_err_out;
1437 }
1438 /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */
1439 if (!load_and_init_upcase(vol))
1440 goto iput_mftbmp_err_out;
1441#ifdef NTFS_RW
1442 /*
1443 * Read attribute definitions table and setup @vol->attrdef and
1444 * @vol->attrdef_size.
1445 */
1446 if (!load_and_init_attrdef(vol))
1447 goto iput_upcase_err_out;
1448#endif /* NTFS_RW */
1449 /*
1450 * Get the cluster allocation bitmap inode and verify the size, no
1451 * need for any locking at this stage as we are already running
1452 * exclusively as we are mount in progress task.
1453 */
1454 vol->lcnbmp_ino = ntfs_iget(sb, FILE_Bitmap);
1455 if (IS_ERR(vol->lcnbmp_ino) || is_bad_inode(vol->lcnbmp_ino)) {
1456 if (!IS_ERR(vol->lcnbmp_ino))
1457 iput(vol->lcnbmp_ino);
1458 goto bitmap_failed;
1459 }
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001460 NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino));
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001461 if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 iput(vol->lcnbmp_ino);
1463bitmap_failed:
1464 ntfs_error(sb, "Failed to load $Bitmap.");
1465 goto iput_attrdef_err_out;
1466 }
1467 /*
1468 * Get the volume inode and setup our cache of the volume flags and
1469 * version.
1470 */
1471 vol->vol_ino = ntfs_iget(sb, FILE_Volume);
1472 if (IS_ERR(vol->vol_ino) || is_bad_inode(vol->vol_ino)) {
1473 if (!IS_ERR(vol->vol_ino))
1474 iput(vol->vol_ino);
1475volume_failed:
1476 ntfs_error(sb, "Failed to load $Volume.");
1477 goto iput_lcnbmp_err_out;
1478 }
1479 m = map_mft_record(NTFS_I(vol->vol_ino));
1480 if (IS_ERR(m)) {
1481iput_volume_failed:
1482 iput(vol->vol_ino);
1483 goto volume_failed;
1484 }
1485 if (!(ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m))) {
1486 ntfs_error(sb, "Failed to get attribute search context.");
1487 goto get_ctx_vol_failed;
1488 }
1489 if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
1490 ctx) || ctx->attr->non_resident || ctx->attr->flags) {
1491err_put_vol:
1492 ntfs_attr_put_search_ctx(ctx);
1493get_ctx_vol_failed:
1494 unmap_mft_record(NTFS_I(vol->vol_ino));
1495 goto iput_volume_failed;
1496 }
1497 vi = (VOLUME_INFORMATION*)((char*)ctx->attr +
1498 le16_to_cpu(ctx->attr->data.resident.value_offset));
1499 /* Some bounds checks. */
1500 if ((u8*)vi < (u8*)ctx->attr || (u8*)vi +
1501 le32_to_cpu(ctx->attr->data.resident.value_length) >
1502 (u8*)ctx->attr + le32_to_cpu(ctx->attr->length))
1503 goto err_put_vol;
1504 /* Copy the volume flags and version to the ntfs_volume structure. */
1505 vol->vol_flags = vi->flags;
1506 vol->major_ver = vi->major_ver;
1507 vol->minor_ver = vi->minor_ver;
1508 ntfs_attr_put_search_ctx(ctx);
1509 unmap_mft_record(NTFS_I(vol->vol_ino));
1510 printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver,
1511 vol->minor_ver);
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001512 if (vol->major_ver < 3 && NVolSparseEnabled(vol)) {
1513 ntfs_warning(vol->sb, "Disabling sparse support due to NTFS "
1514 "volume version %i.%i (need at least version "
1515 "3.0).", vol->major_ver, vol->minor_ver);
1516 NVolClearSparseEnabled(vol);
1517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518#ifdef NTFS_RW
1519 /* Make sure that no unsupported volume flags are set. */
1520 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
1521 static const char *es1a = "Volume is dirty";
1522 static const char *es1b = "Volume has unsupported flags set";
1523 static const char *es2 = ". Run chkdsk and mount in Windows.";
1524 const char *es1;
1525
1526 es1 = vol->vol_flags & VOLUME_IS_DIRTY ? es1a : es1b;
1527 /* If a read-write mount, convert it to a read-only mount. */
1528 if (!(sb->s_flags & MS_RDONLY)) {
1529 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1530 ON_ERRORS_CONTINUE))) {
1531 ntfs_error(sb, "%s and neither on_errors="
1532 "continue nor on_errors="
1533 "remount-ro was specified%s",
1534 es1, es2);
1535 goto iput_vol_err_out;
1536 }
1537 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1538 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1539 } else
1540 ntfs_warning(sb, "%s. Will not be able to remount "
1541 "read-write%s", es1, es2);
1542 /*
1543 * Do not set NVolErrors() because ntfs_remount() re-checks the
1544 * flags which we need to do in case any flags have changed.
1545 */
1546 }
1547 /*
1548 * Get the inode for the logfile, check it and determine if the volume
1549 * was shutdown cleanly.
1550 */
1551 if (!load_and_check_logfile(vol) ||
1552 !ntfs_is_logfile_clean(vol->logfile_ino)) {
1553 static const char *es1a = "Failed to load $LogFile";
1554 static const char *es1b = "$LogFile is not clean";
1555 static const char *es2 = ". Mount in Windows.";
1556 const char *es1;
1557
1558 es1 = !vol->logfile_ino ? es1a : es1b;
1559 /* If a read-write mount, convert it to a read-only mount. */
1560 if (!(sb->s_flags & MS_RDONLY)) {
1561 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1562 ON_ERRORS_CONTINUE))) {
1563 ntfs_error(sb, "%s and neither on_errors="
1564 "continue nor on_errors="
1565 "remount-ro was specified%s",
1566 es1, es2);
1567 goto iput_logfile_err_out;
1568 }
1569 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1570 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1571 } else
1572 ntfs_warning(sb, "%s. Will not be able to remount "
1573 "read-write%s", es1, es2);
1574 /* This will prevent a read-write remount. */
1575 NVolSetErrors(vol);
1576 }
1577 /* If (still) a read-write mount, mark the volume dirty. */
1578 if (!(sb->s_flags & MS_RDONLY) &&
1579 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
1580 static const char *es1 = "Failed to set dirty bit in volume "
1581 "information flags";
1582 static const char *es2 = ". Run chkdsk.";
1583
1584 /* Convert to a read-only mount. */
1585 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1586 ON_ERRORS_CONTINUE))) {
1587 ntfs_error(sb, "%s and neither on_errors=continue nor "
1588 "on_errors=remount-ro was specified%s",
1589 es1, es2);
1590 goto iput_logfile_err_out;
1591 }
1592 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1593 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1594 /*
1595 * Do not set NVolErrors() because ntfs_remount() might manage
1596 * to set the dirty flag in which case all would be well.
1597 */
1598 }
1599#if 0
1600 // TODO: Enable this code once we start modifying anything that is
1601 // different between NTFS 1.2 and 3.x...
1602 /*
1603 * If (still) a read-write mount, set the NT4 compatibility flag on
1604 * newer NTFS version volumes.
1605 */
1606 if (!(sb->s_flags & MS_RDONLY) && (vol->major_ver > 1) &&
1607 ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
1608 static const char *es1 = "Failed to set NT4 compatibility flag";
1609 static const char *es2 = ". Run chkdsk.";
1610
1611 /* Convert to a read-only mount. */
1612 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1613 ON_ERRORS_CONTINUE))) {
1614 ntfs_error(sb, "%s and neither on_errors=continue nor "
1615 "on_errors=remount-ro was specified%s",
1616 es1, es2);
1617 goto iput_logfile_err_out;
1618 }
1619 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1620 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1621 NVolSetErrors(vol);
1622 }
1623#endif
1624 /* If (still) a read-write mount, empty the logfile. */
1625 if (!(sb->s_flags & MS_RDONLY) &&
1626 !ntfs_empty_logfile(vol->logfile_ino)) {
1627 static const char *es1 = "Failed to empty $LogFile";
1628 static const char *es2 = ". Mount in Windows.";
1629
1630 /* Convert to a read-only mount. */
1631 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1632 ON_ERRORS_CONTINUE))) {
1633 ntfs_error(sb, "%s and neither on_errors=continue nor "
1634 "on_errors=remount-ro was specified%s",
1635 es1, es2);
1636 goto iput_logfile_err_out;
1637 }
1638 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1639 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1640 NVolSetErrors(vol);
1641 }
1642#endif /* NTFS_RW */
1643 /* Get the root directory inode. */
1644 vol->root_ino = ntfs_iget(sb, FILE_root);
1645 if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) {
1646 if (!IS_ERR(vol->root_ino))
1647 iput(vol->root_ino);
1648 ntfs_error(sb, "Failed to load root directory.");
1649 goto iput_logfile_err_out;
1650 }
1651 /* If on NTFS versions before 3.0, we are done. */
1652 if (vol->major_ver < 3)
1653 return TRUE;
1654 /* NTFS 3.0+ specific initialization. */
1655 /* Get the security descriptors inode. */
1656 vol->secure_ino = ntfs_iget(sb, FILE_Secure);
1657 if (IS_ERR(vol->secure_ino) || is_bad_inode(vol->secure_ino)) {
1658 if (!IS_ERR(vol->secure_ino))
1659 iput(vol->secure_ino);
1660 ntfs_error(sb, "Failed to load $Secure.");
1661 goto iput_root_err_out;
1662 }
1663 // FIXME: Initialize security.
1664 /* Get the extended system files' directory inode. */
1665 vol->extend_ino = ntfs_iget(sb, FILE_Extend);
1666 if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) {
1667 if (!IS_ERR(vol->extend_ino))
1668 iput(vol->extend_ino);
1669 ntfs_error(sb, "Failed to load $Extend.");
1670 goto iput_sec_err_out;
1671 }
1672#ifdef NTFS_RW
1673 /* Find the quota file, load it if present, and set it up. */
1674 if (!load_and_init_quota(vol)) {
1675 static const char *es1 = "Failed to load $Quota";
1676 static const char *es2 = ". Run chkdsk.";
1677
1678 /* If a read-write mount, convert it to a read-only mount. */
1679 if (!(sb->s_flags & MS_RDONLY)) {
1680 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1681 ON_ERRORS_CONTINUE))) {
1682 ntfs_error(sb, "%s and neither on_errors="
1683 "continue nor on_errors="
1684 "remount-ro was specified%s",
1685 es1, es2);
1686 goto iput_quota_err_out;
1687 }
1688 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1689 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1690 } else
1691 ntfs_warning(sb, "%s. Will not be able to remount "
1692 "read-write%s", es1, es2);
1693 /* This will prevent a read-write remount. */
1694 NVolSetErrors(vol);
1695 }
1696 /* If (still) a read-write mount, mark the quotas out of date. */
1697 if (!(sb->s_flags & MS_RDONLY) &&
1698 !ntfs_mark_quotas_out_of_date(vol)) {
1699 static const char *es1 = "Failed to mark quotas out of date";
1700 static const char *es2 = ". Run chkdsk.";
1701
1702 /* Convert to a read-only mount. */
1703 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1704 ON_ERRORS_CONTINUE))) {
1705 ntfs_error(sb, "%s and neither on_errors=continue nor "
1706 "on_errors=remount-ro was specified%s",
1707 es1, es2);
1708 goto iput_quota_err_out;
1709 }
1710 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1711 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1712 NVolSetErrors(vol);
1713 }
1714 // TODO: Delete or checkpoint the $UsnJrnl if it exists.
1715#endif /* NTFS_RW */
1716 return TRUE;
1717#ifdef NTFS_RW
1718iput_quota_err_out:
1719 if (vol->quota_q_ino)
1720 iput(vol->quota_q_ino);
1721 if (vol->quota_ino)
1722 iput(vol->quota_ino);
1723 iput(vol->extend_ino);
1724#endif /* NTFS_RW */
1725iput_sec_err_out:
1726 iput(vol->secure_ino);
1727iput_root_err_out:
1728 iput(vol->root_ino);
1729iput_logfile_err_out:
1730#ifdef NTFS_RW
1731 if (vol->logfile_ino)
1732 iput(vol->logfile_ino);
1733iput_vol_err_out:
1734#endif /* NTFS_RW */
1735 iput(vol->vol_ino);
1736iput_lcnbmp_err_out:
1737 iput(vol->lcnbmp_ino);
1738iput_attrdef_err_out:
1739 vol->attrdef_size = 0;
1740 if (vol->attrdef) {
1741 ntfs_free(vol->attrdef);
1742 vol->attrdef = NULL;
1743 }
1744#ifdef NTFS_RW
1745iput_upcase_err_out:
1746#endif /* NTFS_RW */
1747 vol->upcase_len = 0;
1748 down(&ntfs_lock);
1749 if (vol->upcase == default_upcase) {
1750 ntfs_nr_upcase_users--;
1751 vol->upcase = NULL;
1752 }
1753 up(&ntfs_lock);
1754 if (vol->upcase) {
1755 ntfs_free(vol->upcase);
1756 vol->upcase = NULL;
1757 }
1758iput_mftbmp_err_out:
1759 iput(vol->mftbmp_ino);
1760iput_mirr_err_out:
1761#ifdef NTFS_RW
1762 if (vol->mftmirr_ino)
1763 iput(vol->mftmirr_ino);
1764#endif /* NTFS_RW */
1765 return FALSE;
1766}
1767
1768/**
1769 * ntfs_put_super - called by the vfs to unmount a volume
1770 * @sb: vfs superblock of volume to unmount
1771 *
1772 * ntfs_put_super() is called by the VFS (from fs/super.c::do_umount()) when
1773 * the volume is being unmounted (umount system call has been invoked) and it
1774 * releases all inodes and memory belonging to the NTFS specific part of the
1775 * super block.
1776 */
1777static void ntfs_put_super(struct super_block *sb)
1778{
1779 ntfs_volume *vol = NTFS_SB(sb);
1780
1781 ntfs_debug("Entering.");
1782#ifdef NTFS_RW
1783 /*
1784 * Commit all inodes while they are still open in case some of them
1785 * cause others to be dirtied.
1786 */
1787 ntfs_commit_inode(vol->vol_ino);
1788
1789 /* NTFS 3.0+ specific. */
1790 if (vol->major_ver >= 3) {
1791 if (vol->quota_q_ino)
1792 ntfs_commit_inode(vol->quota_q_ino);
1793 if (vol->quota_ino)
1794 ntfs_commit_inode(vol->quota_ino);
1795 if (vol->extend_ino)
1796 ntfs_commit_inode(vol->extend_ino);
1797 if (vol->secure_ino)
1798 ntfs_commit_inode(vol->secure_ino);
1799 }
1800
1801 ntfs_commit_inode(vol->root_ino);
1802
1803 down_write(&vol->lcnbmp_lock);
1804 ntfs_commit_inode(vol->lcnbmp_ino);
1805 up_write(&vol->lcnbmp_lock);
1806
1807 down_write(&vol->mftbmp_lock);
1808 ntfs_commit_inode(vol->mftbmp_ino);
1809 up_write(&vol->mftbmp_lock);
1810
1811 if (vol->logfile_ino)
1812 ntfs_commit_inode(vol->logfile_ino);
1813
1814 if (vol->mftmirr_ino)
1815 ntfs_commit_inode(vol->mftmirr_ino);
1816 ntfs_commit_inode(vol->mft_ino);
1817
1818 /*
1819 * If a read-write mount and no volume errors have occured, mark the
1820 * volume clean. Also, re-commit all affected inodes.
1821 */
1822 if (!(sb->s_flags & MS_RDONLY)) {
1823 if (!NVolErrors(vol)) {
1824 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
1825 ntfs_warning(sb, "Failed to clear dirty bit "
1826 "in volume information "
1827 "flags. Run chkdsk.");
1828 ntfs_commit_inode(vol->vol_ino);
1829 ntfs_commit_inode(vol->root_ino);
1830 if (vol->mftmirr_ino)
1831 ntfs_commit_inode(vol->mftmirr_ino);
1832 ntfs_commit_inode(vol->mft_ino);
1833 } else {
1834 ntfs_warning(sb, "Volume has errors. Leaving volume "
1835 "marked dirty. Run chkdsk.");
1836 }
1837 }
1838#endif /* NTFS_RW */
1839
1840 iput(vol->vol_ino);
1841 vol->vol_ino = NULL;
1842
1843 /* NTFS 3.0+ specific clean up. */
1844 if (vol->major_ver >= 3) {
1845#ifdef NTFS_RW
1846 if (vol->quota_q_ino) {
1847 iput(vol->quota_q_ino);
1848 vol->quota_q_ino = NULL;
1849 }
1850 if (vol->quota_ino) {
1851 iput(vol->quota_ino);
1852 vol->quota_ino = NULL;
1853 }
1854#endif /* NTFS_RW */
1855 if (vol->extend_ino) {
1856 iput(vol->extend_ino);
1857 vol->extend_ino = NULL;
1858 }
1859 if (vol->secure_ino) {
1860 iput(vol->secure_ino);
1861 vol->secure_ino = NULL;
1862 }
1863 }
1864
1865 iput(vol->root_ino);
1866 vol->root_ino = NULL;
1867
1868 down_write(&vol->lcnbmp_lock);
1869 iput(vol->lcnbmp_ino);
1870 vol->lcnbmp_ino = NULL;
1871 up_write(&vol->lcnbmp_lock);
1872
1873 down_write(&vol->mftbmp_lock);
1874 iput(vol->mftbmp_ino);
1875 vol->mftbmp_ino = NULL;
1876 up_write(&vol->mftbmp_lock);
1877
1878#ifdef NTFS_RW
1879 if (vol->logfile_ino) {
1880 iput(vol->logfile_ino);
1881 vol->logfile_ino = NULL;
1882 }
1883 if (vol->mftmirr_ino) {
1884 /* Re-commit the mft mirror and mft just in case. */
1885 ntfs_commit_inode(vol->mftmirr_ino);
1886 ntfs_commit_inode(vol->mft_ino);
1887 iput(vol->mftmirr_ino);
1888 vol->mftmirr_ino = NULL;
1889 }
1890 /*
1891 * If any dirty inodes are left, throw away all mft data page cache
1892 * pages to allow a clean umount. This should never happen any more
1893 * due to mft.c::ntfs_mft_writepage() cleaning all the dirty pages as
1894 * the underlying mft records are written out and cleaned. If it does,
1895 * happen anyway, we want to know...
1896 */
1897 ntfs_commit_inode(vol->mft_ino);
1898 write_inode_now(vol->mft_ino, 1);
1899 if (!list_empty(&sb->s_dirty)) {
1900 const char *s1, *s2;
1901
1902 down(&vol->mft_ino->i_sem);
1903 truncate_inode_pages(vol->mft_ino->i_mapping, 0);
1904 up(&vol->mft_ino->i_sem);
1905 write_inode_now(vol->mft_ino, 1);
1906 if (!list_empty(&sb->s_dirty)) {
1907 static const char *_s1 = "inodes";
1908 static const char *_s2 = "";
1909 s1 = _s1;
1910 s2 = _s2;
1911 } else {
1912 static const char *_s1 = "mft pages";
1913 static const char *_s2 = "They have been thrown "
1914 "away. ";
1915 s1 = _s1;
1916 s2 = _s2;
1917 }
1918 ntfs_error(sb, "Dirty %s found at umount time. %sYou should "
1919 "run chkdsk. Please email "
1920 "linux-ntfs-dev@lists.sourceforge.net and say "
1921 "that you saw this message. Thank you.", s1,
1922 s2);
1923 }
1924#endif /* NTFS_RW */
1925
1926 iput(vol->mft_ino);
1927 vol->mft_ino = NULL;
1928
1929 /* Throw away the table of attribute definitions. */
1930 vol->attrdef_size = 0;
1931 if (vol->attrdef) {
1932 ntfs_free(vol->attrdef);
1933 vol->attrdef = NULL;
1934 }
1935 vol->upcase_len = 0;
1936 /*
1937 * Destroy the global default upcase table if necessary. Also decrease
1938 * the number of upcase users if we are a user.
1939 */
1940 down(&ntfs_lock);
1941 if (vol->upcase == default_upcase) {
1942 ntfs_nr_upcase_users--;
1943 vol->upcase = NULL;
1944 }
1945 if (!ntfs_nr_upcase_users && default_upcase) {
1946 ntfs_free(default_upcase);
1947 default_upcase = NULL;
1948 }
1949 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
1950 free_compression_buffers();
1951 up(&ntfs_lock);
1952 if (vol->upcase) {
1953 ntfs_free(vol->upcase);
1954 vol->upcase = NULL;
1955 }
1956 if (vol->nls_map) {
1957 unload_nls(vol->nls_map);
1958 vol->nls_map = NULL;
1959 }
1960 sb->s_fs_info = NULL;
1961 kfree(vol);
1962 return;
1963}
1964
1965/**
1966 * get_nr_free_clusters - return the number of free clusters on a volume
1967 * @vol: ntfs volume for which to obtain free cluster count
1968 *
1969 * Calculate the number of free clusters on the mounted NTFS volume @vol. We
1970 * actually calculate the number of clusters in use instead because this
1971 * allows us to not care about partial pages as these will be just zero filled
1972 * and hence not be counted as allocated clusters.
1973 *
1974 * The only particularity is that clusters beyond the end of the logical ntfs
1975 * volume will be marked as allocated to prevent errors which means we have to
1976 * discount those at the end. This is important as the cluster bitmap always
1977 * has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside
1978 * the logical volume and marked in use when they are not as they do not exist.
1979 *
1980 * If any pages cannot be read we assume all clusters in the erroring pages are
1981 * in use. This means we return an underestimate on errors which is better than
1982 * an overestimate.
1983 */
1984static s64 get_nr_free_clusters(ntfs_volume *vol)
1985{
1986 s64 nr_free = vol->nr_clusters;
1987 u32 *kaddr;
1988 struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
1989 filler_t *readpage = (filler_t*)mapping->a_ops->readpage;
1990 struct page *page;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001991 pgoff_t index, max_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 ntfs_debug("Entering.");
1994 /* Serialize accesses to the cluster bitmap. */
1995 down_read(&vol->lcnbmp_lock);
1996 /*
1997 * Convert the number of bits into bytes rounded up, then convert into
1998 * multiples of PAGE_CACHE_SIZE, rounding up so that if we have one
1999 * full and one partial page max_index = 2.
2000 */
2001 max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >>
2002 PAGE_CACHE_SHIFT;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002003 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2004 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.",
2005 max_index, PAGE_CACHE_SIZE / 4);
2006 for (index = 0; index < max_index; index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 unsigned int i;
2008 /*
2009 * Read the page from page cache, getting it from backing store
2010 * if necessary, and increment the use count.
2011 */
2012 page = read_cache_page(mapping, index, (filler_t*)readpage,
2013 NULL);
2014 /* Ignore pages which errored synchronously. */
2015 if (IS_ERR(page)) {
2016 ntfs_debug("Sync read_cache_page() error. Skipping "
2017 "page (index 0x%lx).", index);
2018 nr_free -= PAGE_CACHE_SIZE * 8;
2019 continue;
2020 }
2021 wait_on_page_locked(page);
2022 /* Ignore pages which errored asynchronously. */
2023 if (!PageUptodate(page)) {
2024 ntfs_debug("Async read_cache_page() error. Skipping "
2025 "page (index 0x%lx).", index);
2026 page_cache_release(page);
2027 nr_free -= PAGE_CACHE_SIZE * 8;
2028 continue;
2029 }
2030 kaddr = (u32*)kmap_atomic(page, KM_USER0);
2031 /*
2032 * For each 4 bytes, subtract the number of set bits. If this
2033 * is the last page and it is partial we don't really care as
2034 * it just means we do a little extra work but it won't affect
2035 * the result as all out of range bytes are set to zero by
2036 * ntfs_readpage().
2037 */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002038 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 nr_free -= (s64)hweight32(kaddr[i]);
2040 kunmap_atomic(kaddr, KM_USER0);
2041 page_cache_release(page);
2042 }
2043 ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index - 1);
2044 /*
2045 * Fixup for eventual bits outside logical ntfs volume (see function
2046 * description above).
2047 */
2048 if (vol->nr_clusters & 63)
2049 nr_free += 64 - (vol->nr_clusters & 63);
2050 up_read(&vol->lcnbmp_lock);
2051 /* If errors occured we may well have gone below zero, fix this. */
2052 if (nr_free < 0)
2053 nr_free = 0;
2054 ntfs_debug("Exiting.");
2055 return nr_free;
2056}
2057
2058/**
2059 * __get_nr_free_mft_records - return the number of free inodes on a volume
2060 * @vol: ntfs volume for which to obtain free inode count
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002061 * @nr_free: number of mft records in filesystem
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002062 * @max_index: maximum number of pages containing set bits
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 *
2064 * Calculate the number of free mft records (inodes) on the mounted NTFS
2065 * volume @vol. We actually calculate the number of mft records in use instead
2066 * because this allows us to not care about partial pages as these will be just
2067 * zero filled and hence not be counted as allocated mft record.
2068 *
2069 * If any pages cannot be read we assume all mft records in the erroring pages
2070 * are in use. This means we return an underestimate on errors which is better
2071 * than an overestimate.
2072 *
2073 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
2074 */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002075static unsigned long __get_nr_free_mft_records(ntfs_volume *vol,
2076 s64 nr_free, const pgoff_t max_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 u32 *kaddr;
2079 struct address_space *mapping = vol->mftbmp_ino->i_mapping;
2080 filler_t *readpage = (filler_t*)mapping->a_ops->readpage;
2081 struct page *page;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002082 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 ntfs_debug("Entering.");
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002085 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002087 "0x%lx.", max_index, PAGE_CACHE_SIZE / 4);
2088 for (index = 0; index < max_index; index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 unsigned int i;
2090 /*
2091 * Read the page from page cache, getting it from backing store
2092 * if necessary, and increment the use count.
2093 */
2094 page = read_cache_page(mapping, index, (filler_t*)readpage,
2095 NULL);
2096 /* Ignore pages which errored synchronously. */
2097 if (IS_ERR(page)) {
2098 ntfs_debug("Sync read_cache_page() error. Skipping "
2099 "page (index 0x%lx).", index);
2100 nr_free -= PAGE_CACHE_SIZE * 8;
2101 continue;
2102 }
2103 wait_on_page_locked(page);
2104 /* Ignore pages which errored asynchronously. */
2105 if (!PageUptodate(page)) {
2106 ntfs_debug("Async read_cache_page() error. Skipping "
2107 "page (index 0x%lx).", index);
2108 page_cache_release(page);
2109 nr_free -= PAGE_CACHE_SIZE * 8;
2110 continue;
2111 }
2112 kaddr = (u32*)kmap_atomic(page, KM_USER0);
2113 /*
2114 * For each 4 bytes, subtract the number of set bits. If this
2115 * is the last page and it is partial we don't really care as
2116 * it just means we do a little extra work but it won't affect
2117 * the result as all out of range bytes are set to zero by
2118 * ntfs_readpage().
2119 */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002120 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 nr_free -= (s64)hweight32(kaddr[i]);
2122 kunmap_atomic(kaddr, KM_USER0);
2123 page_cache_release(page);
2124 }
2125 ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.",
2126 index - 1);
2127 /* If errors occured we may well have gone below zero, fix this. */
2128 if (nr_free < 0)
2129 nr_free = 0;
2130 ntfs_debug("Exiting.");
2131 return nr_free;
2132}
2133
2134/**
2135 * ntfs_statfs - return information about mounted NTFS volume
2136 * @sb: super block of mounted volume
2137 * @sfs: statfs structure in which to return the information
2138 *
2139 * Return information about the mounted NTFS volume @sb in the statfs structure
2140 * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is
2141 * called). We interpret the values to be correct of the moment in time at
2142 * which we are called. Most values are variable otherwise and this isn't just
2143 * the free values but the totals as well. For example we can increase the
2144 * total number of file nodes if we run out and we can keep doing this until
2145 * there is no more space on the volume left at all.
2146 *
2147 * Called from vfs_statfs which is used to handle the statfs, fstatfs, and
2148 * ustat system calls.
2149 *
2150 * Return 0 on success or -errno on error.
2151 */
2152static int ntfs_statfs(struct super_block *sb, struct kstatfs *sfs)
2153{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 s64 size;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002155 ntfs_volume *vol = NTFS_SB(sb);
2156 ntfs_inode *mft_ni = NTFS_I(vol->mft_ino);
2157 pgoff_t max_index;
2158 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 ntfs_debug("Entering.");
2161 /* Type of filesystem. */
2162 sfs->f_type = NTFS_SB_MAGIC;
2163 /* Optimal transfer block size. */
2164 sfs->f_bsize = PAGE_CACHE_SIZE;
2165 /*
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002166 * Total data blocks in filesystem in units of f_bsize and since
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 * inodes are also stored in data blocs ($MFT is a file) this is just
2168 * the total clusters.
2169 */
2170 sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >>
2171 PAGE_CACHE_SHIFT;
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002172 /* Free data blocks in filesystem in units of f_bsize. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 size = get_nr_free_clusters(vol) << vol->cluster_size_bits >>
2174 PAGE_CACHE_SHIFT;
2175 if (size < 0LL)
2176 size = 0LL;
2177 /* Free blocks avail to non-superuser, same as above on NTFS. */
2178 sfs->f_bavail = sfs->f_bfree = size;
2179 /* Serialize accesses to the inode bitmap. */
2180 down_read(&vol->mftbmp_lock);
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002181 read_lock_irqsave(&mft_ni->size_lock, flags);
2182 size = i_size_read(vol->mft_ino) >> vol->mft_record_size_bits;
2183 /*
2184 * Convert the maximum number of set bits into bytes rounded up, then
2185 * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
2186 * have one full and one partial page max_index = 2.
2187 */
2188 max_index = ((((mft_ni->initialized_size >> vol->mft_record_size_bits)
2189 + 7) >> 3) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2190 read_unlock_irqrestore(&mft_ni->size_lock, flags);
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002191 /* Number of inodes in filesystem (at this point in time). */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002192 sfs->f_files = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 /* Free inodes in fs (based on current total count). */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002194 sfs->f_ffree = __get_nr_free_mft_records(vol, size, max_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 up_read(&vol->mftbmp_lock);
2196 /*
2197 * File system id. This is extremely *nix flavour dependent and even
2198 * within Linux itself all fs do their own thing. I interpret this to
2199 * mean a unique id associated with the mounted fs and not the id
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002200 * associated with the filesystem driver, the latter is already given
2201 * by the filesystem type in sfs->f_type. Thus we use the 64-bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 * volume serial number splitting it into two 32-bit parts. We enter
2203 * the least significant 32-bits in f_fsid[0] and the most significant
2204 * 32-bits in f_fsid[1].
2205 */
2206 sfs->f_fsid.val[0] = vol->serial_no & 0xffffffff;
2207 sfs->f_fsid.val[1] = (vol->serial_no >> 32) & 0xffffffff;
2208 /* Maximum length of filenames. */
2209 sfs->f_namelen = NTFS_MAX_NAME_LEN;
2210 return 0;
2211}
2212
2213/**
2214 * The complete super operations.
2215 */
2216static struct super_operations ntfs_sops = {
2217 .alloc_inode = ntfs_alloc_big_inode, /* VFS: Allocate new inode. */
2218 .destroy_inode = ntfs_destroy_big_inode, /* VFS: Deallocate inode. */
2219 .put_inode = ntfs_put_inode, /* VFS: Called just before
2220 the inode reference count
2221 is decreased. */
2222#ifdef NTFS_RW
2223 //.dirty_inode = NULL, /* VFS: Called from
2224 // __mark_inode_dirty(). */
2225 .write_inode = ntfs_write_inode, /* VFS: Write dirty inode to
2226 disk. */
2227 //.drop_inode = NULL, /* VFS: Called just after the
2228 // inode reference count has
2229 // been decreased to zero.
2230 // NOTE: The inode lock is
2231 // held. See fs/inode.c::
2232 // generic_drop_inode(). */
2233 //.delete_inode = NULL, /* VFS: Delete inode from disk.
2234 // Called when i_count becomes
2235 // 0 and i_nlink is also 0. */
2236 //.write_super = NULL, /* Flush dirty super block to
2237 // disk. */
2238 //.sync_fs = NULL, /* ? */
2239 //.write_super_lockfs = NULL, /* ? */
2240 //.unlockfs = NULL, /* ? */
2241#endif /* NTFS_RW */
2242 .put_super = ntfs_put_super, /* Syscall: umount. */
2243 .statfs = ntfs_statfs, /* Syscall: statfs */
2244 .remount_fs = ntfs_remount, /* Syscall: mount -o remount. */
2245 .clear_inode = ntfs_clear_big_inode, /* VFS: Called when an inode is
2246 removed from memory. */
2247 //.umount_begin = NULL, /* Forced umount. */
2248 .show_options = ntfs_show_options, /* Show mount options in
2249 proc. */
2250};
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252/**
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002253 * ntfs_fill_super - mount an ntfs filesystem
2254 * @sb: super block of ntfs filesystem to mount
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 * @opt: string containing the mount options
2256 * @silent: silence error output
2257 *
2258 * ntfs_fill_super() is called by the VFS to mount the device described by @sb
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002259 * with the mount otions in @data with the NTFS filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 *
2261 * If @silent is true, remain silent even if errors are detected. This is used
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002262 * during bootup, when the kernel tries to mount the root filesystem with all
2263 * registered filesystems one after the other until one succeeds. This implies
2264 * that all filesystems except the correct one will quite correctly and
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 * expectedly return an error, but nobody wants to see error messages when in
2266 * fact this is what is supposed to happen.
2267 *
2268 * NOTE: @sb->s_flags contains the mount options flags.
2269 */
2270static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2271{
2272 ntfs_volume *vol;
2273 struct buffer_head *bh;
2274 struct inode *tmp_ino;
2275 int result;
2276
2277 ntfs_debug("Entering.");
2278#ifndef NTFS_RW
2279 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
2280#endif /* ! NTFS_RW */
2281 /* Allocate a new ntfs_volume and place it in sb->s_fs_info. */
2282 sb->s_fs_info = kmalloc(sizeof(ntfs_volume), GFP_NOFS);
2283 vol = NTFS_SB(sb);
2284 if (!vol) {
2285 if (!silent)
2286 ntfs_error(sb, "Allocation of NTFS volume structure "
2287 "failed. Aborting mount...");
2288 return -ENOMEM;
2289 }
2290 /* Initialize ntfs_volume structure. */
2291 memset(vol, 0, sizeof(ntfs_volume));
2292 vol->sb = sb;
2293 vol->upcase = NULL;
2294 vol->attrdef = NULL;
2295 vol->mft_ino = NULL;
2296 vol->mftbmp_ino = NULL;
2297 init_rwsem(&vol->mftbmp_lock);
2298#ifdef NTFS_RW
2299 vol->mftmirr_ino = NULL;
2300 vol->logfile_ino = NULL;
2301#endif /* NTFS_RW */
2302 vol->lcnbmp_ino = NULL;
2303 init_rwsem(&vol->lcnbmp_lock);
2304 vol->vol_ino = NULL;
2305 vol->root_ino = NULL;
2306 vol->secure_ino = NULL;
2307 vol->extend_ino = NULL;
2308#ifdef NTFS_RW
2309 vol->quota_ino = NULL;
2310 vol->quota_q_ino = NULL;
2311#endif /* NTFS_RW */
2312 vol->nls_map = NULL;
2313
2314 /*
2315 * Default is group and other don't have any access to files or
2316 * directories while owner has full access. Further, files by default
2317 * are not executable but directories are of course browseable.
2318 */
2319 vol->fmask = 0177;
2320 vol->dmask = 0077;
2321
2322 unlock_kernel();
2323
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002324 /* By default, enable sparse support. */
2325 NVolSetSparseEnabled(vol);
2326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 /* Important to get the mount options dealt with now. */
2328 if (!parse_options(vol, (char*)opt))
2329 goto err_out_now;
2330
2331 /*
2332 * TODO: Fail safety check. In the future we should really be able to
2333 * cope with this being the case, but for now just bail out.
2334 */
2335 if (bdev_hardsect_size(sb->s_bdev) > NTFS_BLOCK_SIZE) {
2336 if (!silent)
2337 ntfs_error(sb, "Device has unsupported hardsect_size.");
2338 goto err_out_now;
2339 }
2340
2341 /* Setup the device access block size to NTFS_BLOCK_SIZE. */
2342 if (sb_set_blocksize(sb, NTFS_BLOCK_SIZE) != NTFS_BLOCK_SIZE) {
2343 if (!silent)
2344 ntfs_error(sb, "Unable to set block size.");
2345 goto err_out_now;
2346 }
2347
2348 /* Get the size of the device in units of NTFS_BLOCK_SIZE bytes. */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002349 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2350 NTFS_BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
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;
2357 }
2358
2359 /*
2360 * Extract the data from the boot sector and setup the ntfs super block
2361 * using it.
2362 */
2363 result = parse_ntfs_boot_sector(vol, (NTFS_BOOT_SECTOR*)bh->b_data);
2364
2365 /* Initialize the cluster and mft allocators. */
2366 ntfs_setup_allocators(vol);
2367
2368 brelse(bh);
2369
2370 if (!result) {
2371 if (!silent)
2372 ntfs_error(sb, "Unsupported NTFS filesystem.");
2373 goto err_out_now;
2374 }
2375
2376 /*
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).
2380 */
2381
2382 /* Setup remaining fields in the super block. */
2383 sb->s_magic = NTFS_SB_MAGIC;
2384
2385 /*
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.
2392 */
2393 sb->s_maxbytes = MAX_LFS_FILESIZE;
2394
2395 sb->s_time_gran = 100;
2396
2397 /*
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.
2403 */
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;
2410 }
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;
2417 }
2418 down(&ntfs_lock);
2419 /*
2420 * The current mount is a compression user if the cluster size is
2421 * less than or equal 4kiB.
2422 */
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;
2431 }
2432 }
2433 /*
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.
2437 */
2438 if (!default_upcase)
2439 default_upcase = generate_default_upcase();
2440 ntfs_nr_upcase_users++;
2441 up(&ntfs_lock);
2442 /*
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.
2445 */
2446 /*
2447 * Open the system files with normal access functions and complete
2448 * setting up the ntfs super block.
2449 */
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;
2453 }
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;
2463 }
2464 up(&ntfs_lock);
2465 sb->s_export_op = &ntfs_export_ops;
2466 lock_kernel();
2467 return 0;
2468 }
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;
2482 }
2483 if (vol->quota_ino) {
2484 iput(vol->quota_ino);
2485 vol->quota_ino = NULL;
2486 }
2487#endif /* NTFS_RW */
2488 if (vol->extend_ino) {
2489 iput(vol->extend_ino);
2490 vol->extend_ino = NULL;
2491 }
2492 if (vol->secure_ino) {
2493 iput(vol->secure_ino);
2494 vol->secure_ino = NULL;
2495 }
2496 }
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;
2507 }
2508 if (vol->mftmirr_ino) {
2509 iput(vol->mftmirr_ino);
2510 vol->mftmirr_ino = NULL;
2511 }
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;
2518 }
2519 vol->upcase_len = 0;
2520 down(&ntfs_lock);
2521 if (vol->upcase == default_upcase) {
2522 ntfs_nr_upcase_users--;
2523 vol->upcase = NULL;
2524 }
2525 up(&ntfs_lock);
2526 if (vol->upcase) {
2527 ntfs_free(vol->upcase);
2528 vol->upcase = NULL;
2529 }
2530 if (vol->nls_map) {
2531 unload_nls(vol->nls_map);
2532 vol->nls_map = NULL;
2533 }
2534 /* Error exit code path. */
2535unl_upcase_iput_tmp_ino_err_out_now:
2536 /*
2537 * Decrease the number of upcase users and destroy the global default
2538 * upcase table if necessary.
2539 */
2540 down(&ntfs_lock);
2541 if (!--ntfs_nr_upcase_users && default_upcase) {
2542 ntfs_free(default_upcase);
2543 default_upcase = NULL;
2544 }
2545 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
2546 free_compression_buffers();
2547 up(&ntfs_lock);
2548iput_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;
2553 /*
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)
2560 */
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");
2567 }
2568 /* Errors at this stage are irrelevant. */
2569err_out_now:
2570 lock_kernel();
2571 sb->s_fs_info = NULL;
2572 kfree(vol);
2573 ntfs_debug("Failed, returning -EINVAL.");
2574 return -EINVAL;
2575}
2576
2577/*
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.
2581 */
2582kmem_cache_t *ntfs_name_cache;
2583
2584/* Slab caches for efficient allocation/deallocation of of inodes. */
2585kmem_cache_t *ntfs_inode_cache;
2586kmem_cache_t *ntfs_big_inode_cache;
2587
2588/* Init once constructor for the inode slab cache. */
2589static void ntfs_big_inode_init_once(void *foo, kmem_cache_t *cachep,
2590 unsigned long flags)
2591{
2592 ntfs_inode *ni = (ntfs_inode *)foo;
2593
2594 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
2595 SLAB_CTOR_CONSTRUCTOR)
2596 inode_init_once(VFS_I(ni));
2597}
2598
2599/*
2600 * Slab caches to optimize allocations and deallocations of attribute search
2601 * contexts and index contexts, respectively.
2602 */
2603kmem_cache_t *ntfs_attr_ctx_cache;
2604kmem_cache_t *ntfs_index_ctx_cache;
2605
2606/* Driver wide semaphore. */
2607DECLARE_MUTEX(ntfs_lock);
2608
2609static struct super_block *ntfs_get_sb(struct file_system_type *fs_type,
2610 int flags, const char *dev_name, void *data)
2611{
2612 return get_sb_bdev(fs_type, flags, dev_name, data, ntfs_fill_super);
2613}
2614
2615static 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,
2621};
2622
2623/* Stable names for the slab caches. */
2624static const char ntfs_index_ctx_cache_name[] = "ntfs_index_ctx_cache";
2625static const char ntfs_attr_ctx_cache_name[] = "ntfs_attr_ctx_cache";
2626static const char ntfs_name_cache_name[] = "ntfs_name_cache";
2627static const char ntfs_inode_cache_name[] = "ntfs_inode_cache";
2628static const char ntfs_big_inode_cache_name[] = "ntfs_big_inode_cache";
2629
2630static int __init init_ntfs_fs(void)
2631{
2632 int err = 0;
2633
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
2637 "W"
2638#else
2639 "O"
2640#endif
2641#ifdef DEBUG
2642 " DEBUG"
2643#endif
2644#ifdef MODULE
2645 " MODULE"
2646#endif
2647 "].\n");
2648
2649 ntfs_debug("Debug messages are enabled.");
2650
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;
2658 }
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;
2666 }
2667
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;
2675 }
2676
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;
2684 }
2685
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;
2694 }
2695
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;
2701 }
2702
2703 err = register_filesystem(&ntfs_fs_type);
2704 if (!err) {
2705 ntfs_debug("NTFS driver registered successfully.");
2706 return 0; /* Success! */
2707 }
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002708 printk(KERN_CRIT "NTFS: Failed to register NTFS filesystem driver!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710sysctl_err_out:
2711 kmem_cache_destroy(ntfs_big_inode_cache);
2712big_inode_err_out:
2713 kmem_cache_destroy(ntfs_inode_cache);
2714inode_err_out:
2715 kmem_cache_destroy(ntfs_name_cache);
2716name_err_out:
2717 kmem_cache_destroy(ntfs_attr_ctx_cache);
2718actx_err_out:
2719 kmem_cache_destroy(ntfs_index_ctx_cache);
2720ictx_err_out:
2721 if (!err) {
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002722 printk(KERN_CRIT "NTFS: Aborting NTFS filesystem driver "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 "registration...\n");
2724 err = -ENOMEM;
2725 }
2726 return err;
2727}
2728
2729static void __exit exit_ntfs_fs(void)
2730{
2731 int err = 0;
2732
2733 ntfs_debug("Unregistering NTFS driver.");
2734
2735 unregister_filesystem(&ntfs_fs_type);
2736
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);
2759}
2760
2761MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>");
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002762MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2005 Anton Altaparmakov");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763MODULE_VERSION(NTFS_VERSION);
2764MODULE_LICENSE("GPL");
2765#ifdef DEBUG
2766module_param(debug_msgs, bool, 0);
2767MODULE_PARM_DESC(debug_msgs, "Enable debug messages.");
2768#endif
2769
2770module_init(init_ntfs_fs)
2771module_exit(exit_ntfs_fs)