blob: abaaa1cbf8de4dac7ac9e8bfb6e143e82b06bff7 [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 Altaparmakov83311912007-01-18 09:42:48 +00004 * Copyright (c) 2001-2007 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>
Anton Altaparmakov78af34f2006-02-24 10:32:33 +000025#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/string.h>
27#include <linux/spinlock.h>
Martin K. Petersene1defc42009-05-22 17:17:49 -040028#include <linux/blkdev.h> /* For bdev_logical_block_size(). */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/backing-dev.h>
30#include <linux/buffer_head.h>
31#include <linux/vfs.h>
32#include <linux/moduleparam.h>
33#include <linux/smp_lock.h>
34
35#include "sysctl.h"
36#include "logfile.h"
37#include "quota.h"
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +010038#include "usnjrnl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "dir.h"
40#include "debug.h"
41#include "index.h"
42#include "aops.h"
Anton Altaparmakovb0d23742005-04-04 16:20:14 +010043#include "layout.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "malloc.h"
45#include "ntfs.h"
46
Anton Altaparmakovc002f422005-02-03 12:02:56 +000047/* Number of mounted filesystems which have compression enabled. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static unsigned long ntfs_nr_compression_users;
49
50/* A global default upcase table and a corresponding reference count. */
51static ntfschar *default_upcase = NULL;
52static unsigned long ntfs_nr_upcase_users = 0;
53
54/* Error constants/strings used in inode.c::ntfs_show_options(). */
55typedef enum {
56 /* One of these must be present, default is ON_ERRORS_CONTINUE. */
57 ON_ERRORS_PANIC = 0x01,
58 ON_ERRORS_REMOUNT_RO = 0x02,
59 ON_ERRORS_CONTINUE = 0x04,
60 /* Optional, can be combined with any of the above. */
61 ON_ERRORS_RECOVER = 0x10,
62} ON_ERRORS_ACTIONS;
63
64const option_t on_errors_arr[] = {
65 { ON_ERRORS_PANIC, "panic" },
66 { ON_ERRORS_REMOUNT_RO, "remount-ro", },
67 { ON_ERRORS_CONTINUE, "continue", },
68 { ON_ERRORS_RECOVER, "recover" },
69 { 0, NULL }
70};
71
72/**
73 * simple_getbool -
74 *
75 * Copied from old ntfs driver (which copied from vfat driver).
76 */
Richard Knutssonc49c3112006-09-30 23:27:12 -070077static int simple_getbool(char *s, bool *setval)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 if (s) {
80 if (!strcmp(s, "1") || !strcmp(s, "yes") || !strcmp(s, "true"))
Richard Knutssonc49c3112006-09-30 23:27:12 -070081 *setval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 else if (!strcmp(s, "0") || !strcmp(s, "no") ||
83 !strcmp(s, "false"))
Richard Knutssonc49c3112006-09-30 23:27:12 -070084 *setval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 else
86 return 0;
87 } else
Richard Knutssonc49c3112006-09-30 23:27:12 -070088 *setval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return 1;
90}
91
92/**
93 * parse_options - parse the (re)mount options
94 * @vol: ntfs volume
95 * @opt: string containing the (re)mount options
96 *
97 * Parse the recognized options in @opt for the ntfs volume described by @vol.
98 */
Richard Knutssonc49c3112006-09-30 23:27:12 -070099static bool parse_options(ntfs_volume *vol, char *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 char *p, *v, *ov;
102 static char *utf8 = "utf8";
103 int errors = 0, sloppy = 0;
104 uid_t uid = (uid_t)-1;
105 gid_t gid = (gid_t)-1;
106 mode_t fmask = (mode_t)-1, dmask = (mode_t)-1;
107 int mft_zone_multiplier = -1, on_errors = -1;
Anton Altaparmakovc002f422005-02-03 12:02:56 +0000108 int show_sys_files = -1, case_sensitive = -1, disable_sparse = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 struct nls_table *nls_map = NULL, *old_nls;
110
111 /* I am lazy... (-8 */
112#define NTFS_GETOPT_WITH_DEFAULT(option, variable, default_value) \
113 if (!strcmp(p, option)) { \
114 if (!v || !*v) \
115 variable = default_value; \
116 else { \
117 variable = simple_strtoul(ov = v, &v, 0); \
118 if (*v) \
119 goto needs_val; \
120 } \
121 }
122#define NTFS_GETOPT(option, variable) \
123 if (!strcmp(p, option)) { \
124 if (!v || !*v) \
125 goto needs_arg; \
126 variable = simple_strtoul(ov = v, &v, 0); \
127 if (*v) \
128 goto needs_val; \
129 }
Anton Altaparmakov5d467702005-09-12 14:33:47 +0100130#define NTFS_GETOPT_OCTAL(option, variable) \
131 if (!strcmp(p, option)) { \
132 if (!v || !*v) \
133 goto needs_arg; \
134 variable = simple_strtoul(ov = v, &v, 8); \
135 if (*v) \
136 goto needs_val; \
137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#define NTFS_GETOPT_BOOL(option, variable) \
139 if (!strcmp(p, option)) { \
Richard Knutssonc49c3112006-09-30 23:27:12 -0700140 bool val; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (!simple_getbool(v, &val)) \
142 goto needs_bool; \
143 variable = val; \
144 }
145#define NTFS_GETOPT_OPTIONS_ARRAY(option, variable, opt_array) \
146 if (!strcmp(p, option)) { \
147 int _i; \
148 if (!v || !*v) \
149 goto needs_arg; \
150 ov = v; \
151 if (variable == -1) \
152 variable = 0; \
153 for (_i = 0; opt_array[_i].str && *opt_array[_i].str; _i++) \
154 if (!strcmp(opt_array[_i].str, v)) { \
155 variable |= opt_array[_i].val; \
156 break; \
157 } \
158 if (!opt_array[_i].str || !*opt_array[_i].str) \
159 goto needs_val; \
160 }
161 if (!opt || !*opt)
162 goto no_mount_options;
163 ntfs_debug("Entering with mount options string: %s", opt);
164 while ((p = strsep(&opt, ","))) {
165 if ((v = strchr(p, '=')))
166 *v++ = 0;
167 NTFS_GETOPT("uid", uid)
168 else NTFS_GETOPT("gid", gid)
Anton Altaparmakov5d467702005-09-12 14:33:47 +0100169 else NTFS_GETOPT_OCTAL("umask", fmask = dmask)
170 else NTFS_GETOPT_OCTAL("fmask", fmask)
171 else NTFS_GETOPT_OCTAL("dmask", dmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 else NTFS_GETOPT("mft_zone_multiplier", mft_zone_multiplier)
Richard Knutssonc49c3112006-09-30 23:27:12 -0700173 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, true)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files)
175 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive)
Anton Altaparmakovc002f422005-02-03 12:02:56 +0000176 else NTFS_GETOPT_BOOL("disable_sparse", disable_sparse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors,
178 on_errors_arr)
179 else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes"))
180 ntfs_warning(vol->sb, "Ignoring obsolete option %s.",
181 p);
182 else if (!strcmp(p, "nls") || !strcmp(p, "iocharset")) {
183 if (!strcmp(p, "iocharset"))
184 ntfs_warning(vol->sb, "Option iocharset is "
185 "deprecated. Please use "
186 "option nls=<charsetname> in "
187 "the future.");
188 if (!v || !*v)
189 goto needs_arg;
190use_utf8:
191 old_nls = nls_map;
192 nls_map = load_nls(v);
193 if (!nls_map) {
194 if (!old_nls) {
195 ntfs_error(vol->sb, "NLS character set "
196 "%s not found.", v);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700197 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199 ntfs_error(vol->sb, "NLS character set %s not "
200 "found. Using previous one %s.",
201 v, old_nls->charset);
202 nls_map = old_nls;
203 } else /* nls_map */ {
204 if (old_nls)
205 unload_nls(old_nls);
206 }
207 } else if (!strcmp(p, "utf8")) {
Richard Knutssonc49c3112006-09-30 23:27:12 -0700208 bool val = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 ntfs_warning(vol->sb, "Option utf8 is no longer "
210 "supported, using option nls=utf8. Please "
211 "use option nls=utf8 in the future and "
212 "make sure utf8 is compiled either as a "
213 "module or into the kernel.");
214 if (!v || !*v)
Richard Knutssonc49c3112006-09-30 23:27:12 -0700215 val = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 else if (!simple_getbool(v, &val))
217 goto needs_bool;
218 if (val) {
219 v = utf8;
220 goto use_utf8;
221 }
222 } else {
223 ntfs_error(vol->sb, "Unrecognized mount option %s.", p);
224 if (errors < INT_MAX)
225 errors++;
226 }
227#undef NTFS_GETOPT_OPTIONS_ARRAY
228#undef NTFS_GETOPT_BOOL
229#undef NTFS_GETOPT
230#undef NTFS_GETOPT_WITH_DEFAULT
231 }
232no_mount_options:
233 if (errors && !sloppy)
Richard Knutssonc49c3112006-09-30 23:27:12 -0700234 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (sloppy)
236 ntfs_warning(vol->sb, "Sloppy option given. Ignoring "
237 "unrecognized mount option(s) and continuing.");
238 /* Keep this first! */
239 if (on_errors != -1) {
240 if (!on_errors) {
241 ntfs_error(vol->sb, "Invalid errors option argument "
242 "or bug in options parser.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700243 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245 }
246 if (nls_map) {
247 if (vol->nls_map && vol->nls_map != nls_map) {
248 ntfs_error(vol->sb, "Cannot change NLS character set "
249 "on remount.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700250 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 } /* else (!vol->nls_map) */
252 ntfs_debug("Using NLS character set %s.", nls_map->charset);
253 vol->nls_map = nls_map;
254 } else /* (!nls_map) */ {
255 if (!vol->nls_map) {
256 vol->nls_map = load_nls_default();
257 if (!vol->nls_map) {
258 ntfs_error(vol->sb, "Failed to load default "
259 "NLS character set.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700260 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262 ntfs_debug("Using default NLS character set (%s).",
263 vol->nls_map->charset);
264 }
265 }
266 if (mft_zone_multiplier != -1) {
267 if (vol->mft_zone_multiplier && vol->mft_zone_multiplier !=
268 mft_zone_multiplier) {
269 ntfs_error(vol->sb, "Cannot change mft_zone_multiplier "
270 "on remount.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700271 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273 if (mft_zone_multiplier < 1 || mft_zone_multiplier > 4) {
274 ntfs_error(vol->sb, "Invalid mft_zone_multiplier. "
275 "Using default value, i.e. 1.");
276 mft_zone_multiplier = 1;
277 }
278 vol->mft_zone_multiplier = mft_zone_multiplier;
279 }
280 if (!vol->mft_zone_multiplier)
281 vol->mft_zone_multiplier = 1;
282 if (on_errors != -1)
283 vol->on_errors = on_errors;
284 if (!vol->on_errors || vol->on_errors == ON_ERRORS_RECOVER)
285 vol->on_errors |= ON_ERRORS_CONTINUE;
286 if (uid != (uid_t)-1)
287 vol->uid = uid;
288 if (gid != (gid_t)-1)
289 vol->gid = gid;
290 if (fmask != (mode_t)-1)
291 vol->fmask = fmask;
292 if (dmask != (mode_t)-1)
293 vol->dmask = dmask;
294 if (show_sys_files != -1) {
295 if (show_sys_files)
296 NVolSetShowSystemFiles(vol);
297 else
298 NVolClearShowSystemFiles(vol);
299 }
300 if (case_sensitive != -1) {
301 if (case_sensitive)
302 NVolSetCaseSensitive(vol);
303 else
304 NVolClearCaseSensitive(vol);
305 }
Anton Altaparmakovc002f422005-02-03 12:02:56 +0000306 if (disable_sparse != -1) {
307 if (disable_sparse)
308 NVolClearSparseEnabled(vol);
309 else {
310 if (!NVolSparseEnabled(vol) &&
311 vol->major_ver && vol->major_ver < 3)
312 ntfs_warning(vol->sb, "Not enabling sparse "
313 "support due to NTFS volume "
314 "version %i.%i (need at least "
315 "version 3.0).", vol->major_ver,
316 vol->minor_ver);
317 else
318 NVolSetSparseEnabled(vol);
319 }
320 }
Richard Knutssonc49c3112006-09-30 23:27:12 -0700321 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322needs_arg:
323 ntfs_error(vol->sb, "The %s option requires an argument.", p);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700324 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325needs_bool:
326 ntfs_error(vol->sb, "The %s option requires a boolean argument.", p);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700327 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328needs_val:
329 ntfs_error(vol->sb, "Invalid %s option argument: %s", p, ov);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700330 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
333#ifdef NTFS_RW
334
335/**
336 * ntfs_write_volume_flags - write new flags to the volume information flags
337 * @vol: ntfs volume on which to modify the flags
338 * @flags: new flags value for the volume information flags
339 *
340 * Internal function. You probably want to use ntfs_{set,clear}_volume_flags()
341 * instead (see below).
342 *
343 * Replace the volume information flags on the volume @vol with the value
344 * supplied in @flags. Note, this overwrites the volume information flags, so
345 * make sure to combine the flags you want to modify with the old flags and use
346 * the result when calling ntfs_write_volume_flags().
347 *
348 * Return 0 on success and -errno on error.
349 */
350static int ntfs_write_volume_flags(ntfs_volume *vol, const VOLUME_FLAGS flags)
351{
352 ntfs_inode *ni = NTFS_I(vol->vol_ino);
353 MFT_RECORD *m;
354 VOLUME_INFORMATION *vi;
355 ntfs_attr_search_ctx *ctx;
356 int err;
357
358 ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
359 le16_to_cpu(vol->vol_flags), le16_to_cpu(flags));
360 if (vol->vol_flags == flags)
361 goto done;
362 BUG_ON(!ni);
363 m = map_mft_record(ni);
364 if (IS_ERR(m)) {
365 err = PTR_ERR(m);
366 goto err_out;
367 }
368 ctx = ntfs_attr_get_search_ctx(ni, m);
369 if (!ctx) {
370 err = -ENOMEM;
371 goto put_unm_err_out;
372 }
373 err = ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
374 ctx);
375 if (err)
376 goto put_unm_err_out;
377 vi = (VOLUME_INFORMATION*)((u8*)ctx->attr +
378 le16_to_cpu(ctx->attr->data.resident.value_offset));
379 vol->vol_flags = vi->flags = flags;
380 flush_dcache_mft_record_page(ctx->ntfs_ino);
381 mark_mft_record_dirty(ctx->ntfs_ino);
382 ntfs_attr_put_search_ctx(ctx);
383 unmap_mft_record(ni);
384done:
385 ntfs_debug("Done.");
386 return 0;
387put_unm_err_out:
388 if (ctx)
389 ntfs_attr_put_search_ctx(ctx);
390 unmap_mft_record(ni);
391err_out:
392 ntfs_error(vol->sb, "Failed with error code %i.", -err);
393 return err;
394}
395
396/**
397 * ntfs_set_volume_flags - set bits in the volume information flags
398 * @vol: ntfs volume on which to modify the flags
399 * @flags: flags to set on the volume
400 *
401 * Set the bits in @flags in the volume information flags on the volume @vol.
402 *
403 * Return 0 on success and -errno on error.
404 */
405static inline int ntfs_set_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
406{
407 flags &= VOLUME_FLAGS_MASK;
408 return ntfs_write_volume_flags(vol, vol->vol_flags | flags);
409}
410
411/**
412 * ntfs_clear_volume_flags - clear bits in the volume information flags
413 * @vol: ntfs volume on which to modify the flags
414 * @flags: flags to clear on the volume
415 *
416 * Clear the bits in @flags in the volume information flags on the volume @vol.
417 *
418 * Return 0 on success and -errno on error.
419 */
420static inline int ntfs_clear_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
421{
422 flags &= VOLUME_FLAGS_MASK;
423 flags = vol->vol_flags & cpu_to_le16(~le16_to_cpu(flags));
424 return ntfs_write_volume_flags(vol, flags);
425}
426
427#endif /* NTFS_RW */
428
429/**
430 * ntfs_remount - change the mount options of a mounted ntfs filesystem
431 * @sb: superblock of mounted ntfs filesystem
432 * @flags: remount flags
433 * @opt: remount options string
434 *
435 * Change the mount options of an already mounted ntfs filesystem.
436 *
437 * NOTE: The VFS sets the @sb->s_flags remount flags to @flags after
438 * ntfs_remount() returns successfully (i.e. returns 0). Otherwise,
439 * @sb->s_flags are not changed.
440 */
441static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
442{
443 ntfs_volume *vol = NTFS_SB(sb);
444
445 ntfs_debug("Entering with remount options string: %s", opt);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200446
447 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448#ifndef NTFS_RW
Christoph Hellwig24a44dc2006-01-09 20:52:14 -0800449 /* For read-only compiled driver, enforce read-only flag. */
450 *flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451#else /* NTFS_RW */
452 /*
453 * For the read-write compiled driver, if we are remounting read-write,
454 * make sure there are no volume errors and that no unsupported volume
455 * flags are set. Also, empty the logfile journal as it would become
456 * stale as soon as something is written to the volume and mark the
457 * volume dirty so that chkdsk is run if the volume is not umounted
458 * cleanly. Finally, mark the quotas out of date so Windows rescans
459 * the volume on boot and updates them.
460 *
461 * When remounting read-only, mark the volume clean if no volume errors
462 * have occured.
463 */
464 if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
465 static const char *es = ". Cannot remount read-write.";
466
467 /* Remounting read-write. */
468 if (NVolErrors(vol)) {
469 ntfs_error(sb, "Volume has errors and is read-only%s",
470 es);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200471 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return -EROFS;
473 }
474 if (vol->vol_flags & VOLUME_IS_DIRTY) {
475 ntfs_error(sb, "Volume is dirty and read-only%s", es);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200476 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return -EROFS;
478 }
Anton Altaparmakov1cf3109f2006-02-24 10:48:14 +0000479 if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
480 ntfs_error(sb, "Volume has been modified by chkdsk "
481 "and is read-only%s", es);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200482 unlock_kernel();
Anton Altaparmakov1cf3109f2006-02-24 10:48:14 +0000483 return -EROFS;
484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
Anton Altaparmakov1cf3109f2006-02-24 10:48:14 +0000486 ntfs_error(sb, "Volume has unsupported flags set "
487 "(0x%x) and is read-only%s",
488 (unsigned)le16_to_cpu(vol->vol_flags),
489 es);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200490 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return -EROFS;
492 }
493 if (ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
494 ntfs_error(sb, "Failed to set dirty bit in volume "
495 "information flags%s", es);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200496 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return -EROFS;
498 }
499#if 0
500 // TODO: Enable this code once we start modifying anything that
501 // is different between NTFS 1.2 and 3.x...
502 /* Set NT4 compatibility flag on newer NTFS version volumes. */
503 if ((vol->major_ver > 1)) {
504 if (ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
505 ntfs_error(sb, "Failed to set NT4 "
506 "compatibility flag%s", es);
507 NVolSetErrors(vol);
508 return -EROFS;
509 }
510 }
511#endif
512 if (!ntfs_empty_logfile(vol->logfile_ino)) {
513 ntfs_error(sb, "Failed to empty journal $LogFile%s",
514 es);
515 NVolSetErrors(vol);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200516 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return -EROFS;
518 }
519 if (!ntfs_mark_quotas_out_of_date(vol)) {
520 ntfs_error(sb, "Failed to mark quotas out of date%s",
521 es);
522 NVolSetErrors(vol);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200523 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return -EROFS;
525 }
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +0100526 if (!ntfs_stamp_usnjrnl(vol)) {
527 ntfs_error(sb, "Failed to stamp transation log "
528 "($UsnJrnl)%s", es);
529 NVolSetErrors(vol);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200530 unlock_kernel();
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +0100531 return -EROFS;
532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) {
534 /* Remounting read-only. */
535 if (!NVolErrors(vol)) {
536 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
537 ntfs_warning(sb, "Failed to clear dirty bit "
538 "in volume information "
539 "flags. Run chkdsk.");
540 }
541 }
542#endif /* NTFS_RW */
543
544 // TODO: Deal with *flags.
545
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200546 if (!parse_options(vol, opt)) {
547 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return -EINVAL;
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200549 }
550 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 ntfs_debug("Done.");
552 return 0;
553}
554
555/**
556 * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector
557 * @sb: Super block of the device to which @b belongs.
558 * @b: Boot sector of device @sb to check.
Richard Knutssonc49c3112006-09-30 23:27:12 -0700559 * @silent: If 'true', all output will be silenced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 *
561 * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot
Richard Knutssonc49c3112006-09-30 23:27:12 -0700562 * sector. Returns 'true' if it is valid and 'false' if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 *
564 * @sb is only needed for warning/error output, i.e. it can be NULL when silent
Richard Knutssonc49c3112006-09-30 23:27:12 -0700565 * is 'true'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 */
Richard Knutssonc49c3112006-09-30 23:27:12 -0700567static bool is_boot_sector_ntfs(const struct super_block *sb,
568 const NTFS_BOOT_SECTOR *b, const bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 /*
571 * Check that checksum == sum of u32 values from b to the checksum
Anton Altaparmakovb0d23742005-04-04 16:20:14 +0100572 * field. If checksum is zero, no checking is done. We will work when
573 * the checksum test fails, since some utilities update the boot sector
574 * ignoring the checksum which leaves the checksum out-of-date. We
575 * report a warning if this is the case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 */
Anton Altaparmakovb0d23742005-04-04 16:20:14 +0100577 if ((void*)b < (void*)&b->checksum && b->checksum && !silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 le32 *u;
579 u32 i;
580
581 for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u)
582 i += le32_to_cpup(u);
583 if (le32_to_cpu(b->checksum) != i)
Anton Altaparmakovb0d23742005-04-04 16:20:14 +0100584 ntfs_warning(sb, "Invalid boot sector checksum.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586 /* Check OEMidentifier is "NTFS " */
587 if (b->oem_id != magicNTFS)
588 goto not_ntfs;
589 /* Check bytes per sector value is between 256 and 4096. */
590 if (le16_to_cpu(b->bpb.bytes_per_sector) < 0x100 ||
591 le16_to_cpu(b->bpb.bytes_per_sector) > 0x1000)
592 goto not_ntfs;
593 /* Check sectors per cluster value is valid. */
594 switch (b->bpb.sectors_per_cluster) {
595 case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128:
596 break;
597 default:
598 goto not_ntfs;
599 }
Anton Altaparmakov7fafb8b2005-04-06 16:09:21 +0100600 /* Check the cluster size is not above the maximum (64kiB). */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) *
Anton Altaparmakov7fafb8b2005-04-06 16:09:21 +0100602 b->bpb.sectors_per_cluster > NTFS_MAX_CLUSTER_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 goto not_ntfs;
604 /* Check reserved/unused fields are really zero. */
605 if (le16_to_cpu(b->bpb.reserved_sectors) ||
606 le16_to_cpu(b->bpb.root_entries) ||
607 le16_to_cpu(b->bpb.sectors) ||
608 le16_to_cpu(b->bpb.sectors_per_fat) ||
609 le32_to_cpu(b->bpb.large_sectors) || b->bpb.fats)
610 goto not_ntfs;
611 /* Check clusters per file mft record value is valid. */
612 if ((u8)b->clusters_per_mft_record < 0xe1 ||
613 (u8)b->clusters_per_mft_record > 0xf7)
614 switch (b->clusters_per_mft_record) {
615 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
616 break;
617 default:
618 goto not_ntfs;
619 }
620 /* Check clusters per index block value is valid. */
621 if ((u8)b->clusters_per_index_record < 0xe1 ||
622 (u8)b->clusters_per_index_record > 0xf7)
623 switch (b->clusters_per_index_record) {
624 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
625 break;
626 default:
627 goto not_ntfs;
628 }
629 /*
630 * Check for valid end of sector marker. We will work without it, but
631 * many BIOSes will refuse to boot from a bootsector if the magic is
632 * incorrect, so we emit a warning.
633 */
Harvey Harrison63cd8852009-03-31 15:23:52 -0700634 if (!silent && b->end_of_sector_marker != cpu_to_le16(0xaa55))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 ntfs_warning(sb, "Invalid end of sector marker.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700636 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637not_ntfs:
Richard Knutssonc49c3112006-09-30 23:27:12 -0700638 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
641/**
642 * read_ntfs_boot_sector - read the NTFS boot sector of a device
643 * @sb: super block of device to read the boot sector from
644 * @silent: if true, suppress all output
645 *
646 * Reads the boot sector from the device and validates it. If that fails, tries
647 * to read the backup boot sector, first from the end of the device a-la NT4 and
648 * later and then from the middle of the device a-la NT3.51 and before.
649 *
650 * If a valid boot sector is found but it is not the primary boot sector, we
651 * repair the primary boot sector silently (unless the device is read-only or
652 * the primary boot sector is not accessible).
653 *
654 * NOTE: To call this function, @sb must have the fields s_dev, the ntfs super
655 * block (u.ntfs_sb), nr_blocks and the device flags (s_flags) initialized
656 * to their respective values.
657 *
658 * Return the unlocked buffer head containing the boot sector or NULL on error.
659 */
660static struct buffer_head *read_ntfs_boot_sector(struct super_block *sb,
661 const int silent)
662{
663 const char *read_err_str = "Unable to read %s boot sector.";
664 struct buffer_head *bh_primary, *bh_backup;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000665 sector_t nr_blocks = NTFS_SB(sb)->nr_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /* Try to read primary boot sector. */
668 if ((bh_primary = sb_bread(sb, 0))) {
669 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
670 bh_primary->b_data, silent))
671 return bh_primary;
672 if (!silent)
673 ntfs_error(sb, "Primary boot sector is invalid.");
674 } else if (!silent)
675 ntfs_error(sb, read_err_str, "primary");
676 if (!(NTFS_SB(sb)->on_errors & ON_ERRORS_RECOVER)) {
677 if (bh_primary)
678 brelse(bh_primary);
679 if (!silent)
680 ntfs_error(sb, "Mount option errors=recover not used. "
681 "Aborting without trying to recover.");
682 return NULL;
683 }
684 /* Try to read NT4+ backup boot sector. */
685 if ((bh_backup = sb_bread(sb, nr_blocks - 1))) {
686 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
687 bh_backup->b_data, silent))
688 goto hotfix_primary_boot_sector;
689 brelse(bh_backup);
690 } else if (!silent)
691 ntfs_error(sb, read_err_str, "backup");
692 /* Try to read NT3.51- backup boot sector. */
693 if ((bh_backup = sb_bread(sb, nr_blocks >> 1))) {
694 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
695 bh_backup->b_data, silent))
696 goto hotfix_primary_boot_sector;
697 if (!silent)
698 ntfs_error(sb, "Could not find a valid backup boot "
699 "sector.");
700 brelse(bh_backup);
701 } else if (!silent)
702 ntfs_error(sb, read_err_str, "backup");
703 /* We failed. Cleanup and return. */
704 if (bh_primary)
705 brelse(bh_primary);
706 return NULL;
707hotfix_primary_boot_sector:
708 if (bh_primary) {
709 /*
710 * If we managed to read sector zero and the volume is not
711 * read-only, copy the found, valid backup boot sector to the
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000712 * primary boot sector. Note we only copy the actual boot
713 * sector structure, not the actual whole device sector as that
714 * may be bigger and would potentially damage the $Boot system
715 * file (FIXME: Would be nice to know if the backup boot sector
716 * on a large sector device contains the whole boot loader or
717 * just the first 512 bytes).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 */
719 if (!(sb->s_flags & MS_RDONLY)) {
720 ntfs_warning(sb, "Hot-fix: Recovering invalid primary "
721 "boot sector from backup copy.");
722 memcpy(bh_primary->b_data, bh_backup->b_data,
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000723 NTFS_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 mark_buffer_dirty(bh_primary);
725 sync_dirty_buffer(bh_primary);
726 if (buffer_uptodate(bh_primary)) {
727 brelse(bh_backup);
728 return bh_primary;
729 }
730 ntfs_error(sb, "Hot-fix: Device write error while "
731 "recovering primary boot sector.");
732 } else {
733 ntfs_warning(sb, "Hot-fix: Recovery of primary boot "
734 "sector failed: Read-only mount.");
735 }
736 brelse(bh_primary);
737 }
738 ntfs_warning(sb, "Using backup boot sector.");
739 return bh_backup;
740}
741
742/**
743 * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol
744 * @vol: volume structure to initialise with data from boot sector
745 * @b: boot sector to parse
746 *
747 * Parse the ntfs boot sector @b and store all imporant information therein in
Richard Knutssonc49c3112006-09-30 23:27:12 -0700748 * the ntfs super block @vol. Return 'true' on success and 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 */
Richard Knutssonc49c3112006-09-30 23:27:12 -0700750static bool parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 unsigned int sectors_per_cluster_bits, nr_hidden_sects;
753 int clusters_per_mft_record, clusters_per_index_record;
754 s64 ll;
755
756 vol->sector_size = le16_to_cpu(b->bpb.bytes_per_sector);
757 vol->sector_size_bits = ffs(vol->sector_size) - 1;
758 ntfs_debug("vol->sector_size = %i (0x%x)", vol->sector_size,
759 vol->sector_size);
760 ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits,
761 vol->sector_size_bits);
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000762 if (vol->sector_size < vol->sb->s_blocksize) {
763 ntfs_error(vol->sb, "Sector size (%i) is smaller than the "
764 "device block size (%lu). This is not "
765 "supported. Sorry.", vol->sector_size,
766 vol->sb->s_blocksize);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700767 return false;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster);
770 sectors_per_cluster_bits = ffs(b->bpb.sectors_per_cluster) - 1;
771 ntfs_debug("sectors_per_cluster_bits = 0x%x",
772 sectors_per_cluster_bits);
773 nr_hidden_sects = le32_to_cpu(b->bpb.hidden_sectors);
774 ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects);
775 vol->cluster_size = vol->sector_size << sectors_per_cluster_bits;
776 vol->cluster_size_mask = vol->cluster_size - 1;
777 vol->cluster_size_bits = ffs(vol->cluster_size) - 1;
778 ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size,
779 vol->cluster_size);
780 ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask);
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000781 ntfs_debug("vol->cluster_size_bits = %i", vol->cluster_size_bits);
782 if (vol->cluster_size < vol->sector_size) {
783 ntfs_error(vol->sb, "Cluster size (%i) is smaller than the "
784 "sector size (%i). This is not supported. "
785 "Sorry.", vol->cluster_size, vol->sector_size);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700786 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788 clusters_per_mft_record = b->clusters_per_mft_record;
789 ntfs_debug("clusters_per_mft_record = %i (0x%x)",
790 clusters_per_mft_record, clusters_per_mft_record);
791 if (clusters_per_mft_record > 0)
792 vol->mft_record_size = vol->cluster_size <<
793 (ffs(clusters_per_mft_record) - 1);
794 else
795 /*
796 * When mft_record_size < cluster_size, clusters_per_mft_record
797 * = -log2(mft_record_size) bytes. mft_record_size normaly is
798 * 1024 bytes, which is encoded as 0xF6 (-10 in decimal).
799 */
800 vol->mft_record_size = 1 << -clusters_per_mft_record;
801 vol->mft_record_size_mask = vol->mft_record_size - 1;
802 vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1;
803 ntfs_debug("vol->mft_record_size = %i (0x%x)", vol->mft_record_size,
804 vol->mft_record_size);
805 ntfs_debug("vol->mft_record_size_mask = 0x%x",
806 vol->mft_record_size_mask);
807 ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
808 vol->mft_record_size_bits, vol->mft_record_size_bits);
809 /*
810 * We cannot support mft record sizes above the PAGE_CACHE_SIZE since
811 * we store $MFT/$DATA, the table of mft records in the page cache.
812 */
813 if (vol->mft_record_size > PAGE_CACHE_SIZE) {
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000814 ntfs_error(vol->sb, "Mft record size (%i) exceeds the "
815 "PAGE_CACHE_SIZE on your system (%lu). "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 "This is not supported. Sorry.",
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000817 vol->mft_record_size, PAGE_CACHE_SIZE);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700818 return false;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000819 }
820 /* We cannot support mft record sizes below the sector size. */
821 if (vol->mft_record_size < vol->sector_size) {
822 ntfs_error(vol->sb, "Mft record size (%i) is smaller than the "
823 "sector size (%i). This is not supported. "
824 "Sorry.", vol->mft_record_size,
825 vol->sector_size);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700826 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828 clusters_per_index_record = b->clusters_per_index_record;
829 ntfs_debug("clusters_per_index_record = %i (0x%x)",
830 clusters_per_index_record, clusters_per_index_record);
831 if (clusters_per_index_record > 0)
832 vol->index_record_size = vol->cluster_size <<
833 (ffs(clusters_per_index_record) - 1);
834 else
835 /*
836 * When index_record_size < cluster_size,
837 * clusters_per_index_record = -log2(index_record_size) bytes.
838 * index_record_size normaly equals 4096 bytes, which is
839 * encoded as 0xF4 (-12 in decimal).
840 */
841 vol->index_record_size = 1 << -clusters_per_index_record;
842 vol->index_record_size_mask = vol->index_record_size - 1;
843 vol->index_record_size_bits = ffs(vol->index_record_size) - 1;
844 ntfs_debug("vol->index_record_size = %i (0x%x)",
845 vol->index_record_size, vol->index_record_size);
846 ntfs_debug("vol->index_record_size_mask = 0x%x",
847 vol->index_record_size_mask);
848 ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
849 vol->index_record_size_bits,
850 vol->index_record_size_bits);
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000851 /* We cannot support index record sizes below the sector size. */
852 if (vol->index_record_size < vol->sector_size) {
853 ntfs_error(vol->sb, "Index record size (%i) is smaller than "
854 "the sector size (%i). This is not "
855 "supported. Sorry.", vol->index_record_size,
856 vol->sector_size);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700857 return false;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 /*
860 * Get the size of the volume in clusters and check for 64-bit-ness.
861 * Windows currently only uses 32 bits to save the clusters so we do
862 * the same as it is much faster on 32-bit CPUs.
863 */
864 ll = sle64_to_cpu(b->number_of_sectors) >> sectors_per_cluster_bits;
865 if ((u64)ll >= 1ULL << 32) {
866 ntfs_error(vol->sb, "Cannot handle 64-bit clusters. Sorry.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700867 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869 vol->nr_clusters = ll;
870 ntfs_debug("vol->nr_clusters = 0x%llx", (long long)vol->nr_clusters);
871 /*
872 * On an architecture where unsigned long is 32-bits, we restrict the
873 * volume size to 2TiB (2^41). On a 64-bit architecture, the compiler
874 * will hopefully optimize the whole check away.
875 */
876 if (sizeof(unsigned long) < 8) {
877 if ((ll << vol->cluster_size_bits) >= (1ULL << 41)) {
878 ntfs_error(vol->sb, "Volume size (%lluTiB) is too "
879 "large for this architecture. "
880 "Maximum supported is 2TiB. Sorry.",
881 (unsigned long long)ll >> (40 -
882 vol->cluster_size_bits));
Richard Knutssonc49c3112006-09-30 23:27:12 -0700883 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885 }
886 ll = sle64_to_cpu(b->mft_lcn);
887 if (ll >= vol->nr_clusters) {
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000888 ntfs_error(vol->sb, "MFT LCN (%lli, 0x%llx) is beyond end of "
889 "volume. Weird.", (unsigned long long)ll,
890 (unsigned long long)ll);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700891 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893 vol->mft_lcn = ll;
894 ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol->mft_lcn);
895 ll = sle64_to_cpu(b->mftmirr_lcn);
896 if (ll >= vol->nr_clusters) {
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000897 ntfs_error(vol->sb, "MFTMirr LCN (%lli, 0x%llx) is beyond end "
898 "of volume. Weird.", (unsigned long long)ll,
899 (unsigned long long)ll);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700900 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902 vol->mftmirr_lcn = ll;
903 ntfs_debug("vol->mftmirr_lcn = 0x%llx", (long long)vol->mftmirr_lcn);
904#ifdef NTFS_RW
905 /*
906 * Work out the size of the mft mirror in number of mft records. If the
907 * cluster size is less than or equal to the size taken by four mft
908 * records, the mft mirror stores the first four mft records. If the
909 * cluster size is bigger than the size taken by four mft records, the
910 * mft mirror contains as many mft records as will fit into one
911 * cluster.
912 */
913 if (vol->cluster_size <= (4 << vol->mft_record_size_bits))
914 vol->mftmirr_size = 4;
915 else
916 vol->mftmirr_size = vol->cluster_size >>
917 vol->mft_record_size_bits;
918 ntfs_debug("vol->mftmirr_size = %i", vol->mftmirr_size);
919#endif /* NTFS_RW */
920 vol->serial_no = le64_to_cpu(b->volume_serial_number);
921 ntfs_debug("vol->serial_no = 0x%llx",
922 (unsigned long long)vol->serial_no);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700923 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
926/**
927 * ntfs_setup_allocators - initialize the cluster and mft allocators
928 * @vol: volume structure for which to setup the allocators
929 *
930 * Setup the cluster (lcn) and mft allocators to the starting values.
931 */
932static void ntfs_setup_allocators(ntfs_volume *vol)
933{
934#ifdef NTFS_RW
935 LCN mft_zone_size, mft_lcn;
936#endif /* NTFS_RW */
937
938 ntfs_debug("vol->mft_zone_multiplier = 0x%x",
939 vol->mft_zone_multiplier);
940#ifdef NTFS_RW
941 /* Determine the size of the MFT zone. */
942 mft_zone_size = vol->nr_clusters;
943 switch (vol->mft_zone_multiplier) { /* % of volume size in clusters */
944 case 4:
945 mft_zone_size >>= 1; /* 50% */
946 break;
947 case 3:
948 mft_zone_size = (mft_zone_size +
949 (mft_zone_size >> 1)) >> 2; /* 37.5% */
950 break;
951 case 2:
952 mft_zone_size >>= 2; /* 25% */
953 break;
954 /* case 1: */
955 default:
956 mft_zone_size >>= 3; /* 12.5% */
957 break;
958 }
959 /* Setup the mft zone. */
960 vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn;
961 ntfs_debug("vol->mft_zone_pos = 0x%llx",
962 (unsigned long long)vol->mft_zone_pos);
963 /*
964 * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
965 * source) and if the actual mft_lcn is in the expected place or even
966 * further to the front of the volume, extend the mft_zone to cover the
967 * beginning of the volume as well. This is in order to protect the
968 * area reserved for the mft bitmap as well within the mft_zone itself.
969 * On non-standard volumes we do not protect it as the overhead would
970 * be higher than the speed increase we would get by doing it.
971 */
972 mft_lcn = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size;
973 if (mft_lcn * vol->cluster_size < 16 * 1024)
974 mft_lcn = (16 * 1024 + vol->cluster_size - 1) /
975 vol->cluster_size;
976 if (vol->mft_zone_start <= mft_lcn)
977 vol->mft_zone_start = 0;
978 ntfs_debug("vol->mft_zone_start = 0x%llx",
979 (unsigned long long)vol->mft_zone_start);
980 /*
981 * Need to cap the mft zone on non-standard volumes so that it does
982 * not point outside the boundaries of the volume. We do this by
983 * halving the zone size until we are inside the volume.
984 */
985 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
986 while (vol->mft_zone_end >= vol->nr_clusters) {
987 mft_zone_size >>= 1;
988 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
989 }
990 ntfs_debug("vol->mft_zone_end = 0x%llx",
991 (unsigned long long)vol->mft_zone_end);
992 /*
993 * Set the current position within each data zone to the start of the
994 * respective zone.
995 */
996 vol->data1_zone_pos = vol->mft_zone_end;
997 ntfs_debug("vol->data1_zone_pos = 0x%llx",
998 (unsigned long long)vol->data1_zone_pos);
999 vol->data2_zone_pos = 0;
1000 ntfs_debug("vol->data2_zone_pos = 0x%llx",
1001 (unsigned long long)vol->data2_zone_pos);
1002
1003 /* Set the mft data allocation position to mft record 24. */
1004 vol->mft_data_pos = 24;
1005 ntfs_debug("vol->mft_data_pos = 0x%llx",
1006 (unsigned long long)vol->mft_data_pos);
1007#endif /* NTFS_RW */
1008}
1009
1010#ifdef NTFS_RW
1011
1012/**
1013 * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume
1014 * @vol: ntfs super block describing device whose mft mirror to load
1015 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001016 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001018static bool load_and_init_mft_mirror(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 struct inode *tmp_ino;
1021 ntfs_inode *tmp_ni;
1022
1023 ntfs_debug("Entering.");
1024 /* Get mft mirror inode. */
1025 tmp_ino = ntfs_iget(vol->sb, FILE_MFTMirr);
1026 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1027 if (!IS_ERR(tmp_ino))
1028 iput(tmp_ino);
1029 /* Caller will display error message. */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001030 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032 /*
1033 * Re-initialize some specifics about $MFTMirr's inode as
1034 * ntfs_read_inode() will have set up the default ones.
1035 */
1036 /* Set uid and gid to root. */
1037 tmp_ino->i_uid = tmp_ino->i_gid = 0;
1038 /* Regular file. No access for anyone. */
1039 tmp_ino->i_mode = S_IFREG;
1040 /* No VFS initiated operations allowed for $MFTMirr. */
1041 tmp_ino->i_op = &ntfs_empty_inode_ops;
1042 tmp_ino->i_fop = &ntfs_empty_file_ops;
1043 /* Put in our special address space operations. */
1044 tmp_ino->i_mapping->a_ops = &ntfs_mst_aops;
1045 tmp_ni = NTFS_I(tmp_ino);
1046 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
1047 NInoSetMstProtected(tmp_ni);
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001048 NInoSetSparseDisabled(tmp_ni);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /*
1050 * Set up our little cheat allowing us to reuse the async read io
1051 * completion handler for directories.
1052 */
1053 tmp_ni->itype.index.block_size = vol->mft_record_size;
1054 tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits;
1055 vol->mftmirr_ino = tmp_ino;
1056 ntfs_debug("Done.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001057 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
1060/**
1061 * check_mft_mirror - compare contents of the mft mirror with the mft
1062 * @vol: ntfs super block describing device whose mft mirror to check
1063 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001064 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 *
1066 * Note, this function also results in the mft mirror runlist being completely
1067 * mapped into memory. The mft mirror write code requires this and will BUG()
1068 * should it find an unmapped runlist element.
1069 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001070static bool check_mft_mirror(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 struct super_block *sb = vol->sb;
1073 ntfs_inode *mirr_ni;
1074 struct page *mft_page, *mirr_page;
1075 u8 *kmft, *kmirr;
1076 runlist_element *rl, rl2[2];
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001077 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 int mrecs_per_page, i;
1079
1080 ntfs_debug("Entering.");
1081 /* Compare contents of $MFT and $MFTMirr. */
1082 mrecs_per_page = PAGE_CACHE_SIZE / vol->mft_record_size;
1083 BUG_ON(!mrecs_per_page);
1084 BUG_ON(!vol->mftmirr_size);
1085 mft_page = mirr_page = NULL;
1086 kmft = kmirr = NULL;
1087 index = i = 0;
1088 do {
1089 u32 bytes;
1090
1091 /* Switch pages if necessary. */
1092 if (!(i % mrecs_per_page)) {
1093 if (index) {
1094 ntfs_unmap_page(mft_page);
1095 ntfs_unmap_page(mirr_page);
1096 }
1097 /* Get the $MFT page. */
1098 mft_page = ntfs_map_page(vol->mft_ino->i_mapping,
1099 index);
1100 if (IS_ERR(mft_page)) {
1101 ntfs_error(sb, "Failed to read $MFT.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001102 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104 kmft = page_address(mft_page);
1105 /* Get the $MFTMirr page. */
1106 mirr_page = ntfs_map_page(vol->mftmirr_ino->i_mapping,
1107 index);
1108 if (IS_ERR(mirr_page)) {
1109 ntfs_error(sb, "Failed to read $MFTMirr.");
1110 goto mft_unmap_out;
1111 }
1112 kmirr = page_address(mirr_page);
1113 ++index;
1114 }
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001115 /* Do not check the record if it is not in use. */
1116 if (((MFT_RECORD*)kmft)->flags & MFT_RECORD_IN_USE) {
1117 /* Make sure the record is ok. */
1118 if (ntfs_is_baad_recordp((le32*)kmft)) {
1119 ntfs_error(sb, "Incomplete multi sector "
1120 "transfer detected in mft "
1121 "record %i.", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122mm_unmap_out:
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001123 ntfs_unmap_page(mirr_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124mft_unmap_out:
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001125 ntfs_unmap_page(mft_page);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001126 return false;
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001129 /* Do not check the mirror record if it is not in use. */
1130 if (((MFT_RECORD*)kmirr)->flags & MFT_RECORD_IN_USE) {
1131 if (ntfs_is_baad_recordp((le32*)kmirr)) {
1132 ntfs_error(sb, "Incomplete multi sector "
1133 "transfer detected in mft "
1134 "mirror record %i.", i);
1135 goto mm_unmap_out;
1136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138 /* Get the amount of data in the current record. */
1139 bytes = le32_to_cpu(((MFT_RECORD*)kmft)->bytes_in_use);
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001140 if (bytes < sizeof(MFT_RECORD_OLD) ||
1141 bytes > vol->mft_record_size ||
1142 ntfs_is_baad_recordp((le32*)kmft)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 bytes = le32_to_cpu(((MFT_RECORD*)kmirr)->bytes_in_use);
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001144 if (bytes < sizeof(MFT_RECORD_OLD) ||
1145 bytes > vol->mft_record_size ||
1146 ntfs_is_baad_recordp((le32*)kmirr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 bytes = vol->mft_record_size;
1148 }
1149 /* Compare the two records. */
1150 if (memcmp(kmft, kmirr, bytes)) {
1151 ntfs_error(sb, "$MFT and $MFTMirr (record %i) do not "
1152 "match. Run ntfsfix or chkdsk.", i);
1153 goto mm_unmap_out;
1154 }
1155 kmft += vol->mft_record_size;
1156 kmirr += vol->mft_record_size;
1157 } while (++i < vol->mftmirr_size);
1158 /* Release the last pages. */
1159 ntfs_unmap_page(mft_page);
1160 ntfs_unmap_page(mirr_page);
1161
1162 /* Construct the mft mirror runlist by hand. */
1163 rl2[0].vcn = 0;
1164 rl2[0].lcn = vol->mftmirr_lcn;
1165 rl2[0].length = (vol->mftmirr_size * vol->mft_record_size +
1166 vol->cluster_size - 1) / vol->cluster_size;
1167 rl2[1].vcn = rl2[0].length;
1168 rl2[1].lcn = LCN_ENOENT;
1169 rl2[1].length = 0;
1170 /*
1171 * Because we have just read all of the mft mirror, we know we have
1172 * mapped the full runlist for it.
1173 */
1174 mirr_ni = NTFS_I(vol->mftmirr_ino);
1175 down_read(&mirr_ni->runlist.lock);
1176 rl = mirr_ni->runlist.rl;
1177 /* Compare the two runlists. They must be identical. */
1178 i = 0;
1179 do {
1180 if (rl2[i].vcn != rl[i].vcn || rl2[i].lcn != rl[i].lcn ||
1181 rl2[i].length != rl[i].length) {
1182 ntfs_error(sb, "$MFTMirr location mismatch. "
1183 "Run chkdsk.");
1184 up_read(&mirr_ni->runlist.lock);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001185 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187 } while (rl2[i++].length);
1188 up_read(&mirr_ni->runlist.lock);
1189 ntfs_debug("Done.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001190 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
1193/**
1194 * load_and_check_logfile - load and check the logfile inode for a volume
1195 * @vol: ntfs super block describing device whose logfile to load
1196 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001197 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001199static bool load_and_check_logfile(ntfs_volume *vol,
Anton Altaparmakove7a10332005-09-08 16:12:28 +01001200 RESTART_PAGE_HEADER **rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 struct inode *tmp_ino;
1203
1204 ntfs_debug("Entering.");
1205 tmp_ino = ntfs_iget(vol->sb, FILE_LogFile);
1206 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1207 if (!IS_ERR(tmp_ino))
1208 iput(tmp_ino);
1209 /* Caller will display error message. */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001210 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
Anton Altaparmakove7a10332005-09-08 16:12:28 +01001212 if (!ntfs_check_logfile(tmp_ino, rp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 iput(tmp_ino);
1214 /* ntfs_check_logfile() will have displayed error output. */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001215 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001217 NInoSetSparseDisabled(NTFS_I(tmp_ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 vol->logfile_ino = tmp_ino;
1219 ntfs_debug("Done.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001220 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001223#define NTFS_HIBERFIL_HEADER_SIZE 4096
1224
1225/**
1226 * check_windows_hibernation_status - check if Windows is suspended on a volume
1227 * @vol: ntfs super block of device to check
1228 *
1229 * Check if Windows is hibernated on the ntfs volume @vol. This is done by
1230 * looking for the file hiberfil.sys in the root directory of the volume. If
1231 * the file is not present Windows is definitely not suspended.
1232 *
1233 * If hiberfil.sys exists and is less than 4kiB in size it means Windows is
1234 * definitely suspended (this volume is not the system volume). Caveat: on a
1235 * system with many volumes it is possible that the < 4kiB check is bogus but
1236 * for now this should do fine.
1237 *
1238 * If hiberfil.sys exists and is larger than 4kiB in size, we need to read the
1239 * hiberfil header (which is the first 4kiB). If this begins with "hibr",
1240 * Windows is definitely suspended. If it is completely full of zeroes,
1241 * Windows is definitely not hibernated. Any other case is treated as if
1242 * Windows is suspended. This caters for the above mentioned caveat of a
1243 * system with many volumes where no "hibr" magic would be present and there is
1244 * no zero header.
1245 *
1246 * Return 0 if Windows is not hibernated on the volume, >0 if Windows is
1247 * hibernated on the volume, and -errno on error.
1248 */
1249static int check_windows_hibernation_status(ntfs_volume *vol)
1250{
1251 MFT_REF mref;
1252 struct inode *vi;
1253 ntfs_inode *ni;
1254 struct page *page;
1255 u32 *kaddr, *kend;
1256 ntfs_name *name = NULL;
1257 int ret = 1;
Harvey Harrison63cd8852009-03-31 15:23:52 -07001258 static const ntfschar hiberfil[13] = { cpu_to_le16('h'),
1259 cpu_to_le16('i'), cpu_to_le16('b'),
1260 cpu_to_le16('e'), cpu_to_le16('r'),
1261 cpu_to_le16('f'), cpu_to_le16('i'),
1262 cpu_to_le16('l'), cpu_to_le16('.'),
1263 cpu_to_le16('s'), cpu_to_le16('y'),
1264 cpu_to_le16('s'), 0 };
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001265
1266 ntfs_debug("Entering.");
1267 /*
1268 * Find the inode number for the hibernation file by looking up the
1269 * filename hiberfil.sys in the root directory.
1270 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001271 mutex_lock(&vol->root_ino->i_mutex);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001272 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->root_ino), hiberfil, 12,
1273 &name);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001274 mutex_unlock(&vol->root_ino->i_mutex);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001275 if (IS_ERR_MREF(mref)) {
1276 ret = MREF_ERR(mref);
1277 /* If the file does not exist, Windows is not hibernated. */
1278 if (ret == -ENOENT) {
1279 ntfs_debug("hiberfil.sys not present. Windows is not "
1280 "hibernated on the volume.");
1281 return 0;
1282 }
1283 /* A real error occured. */
1284 ntfs_error(vol->sb, "Failed to find inode number for "
1285 "hiberfil.sys.");
1286 return ret;
1287 }
1288 /* We do not care for the type of match that was found. */
1289 kfree(name);
1290 /* Get the inode. */
1291 vi = ntfs_iget(vol->sb, MREF(mref));
1292 if (IS_ERR(vi) || is_bad_inode(vi)) {
1293 if (!IS_ERR(vi))
1294 iput(vi);
1295 ntfs_error(vol->sb, "Failed to load hiberfil.sys.");
1296 return IS_ERR(vi) ? PTR_ERR(vi) : -EIO;
1297 }
1298 if (unlikely(i_size_read(vi) < NTFS_HIBERFIL_HEADER_SIZE)) {
1299 ntfs_debug("hiberfil.sys is smaller than 4kiB (0x%llx). "
1300 "Windows is hibernated on the volume. This "
1301 "is not the system volume.", i_size_read(vi));
1302 goto iput_out;
1303 }
1304 ni = NTFS_I(vi);
1305 page = ntfs_map_page(vi->i_mapping, 0);
1306 if (IS_ERR(page)) {
1307 ntfs_error(vol->sb, "Failed to read from hiberfil.sys.");
1308 ret = PTR_ERR(page);
1309 goto iput_out;
1310 }
1311 kaddr = (u32*)page_address(page);
Harvey Harrison63cd8852009-03-31 15:23:52 -07001312 if (*(le32*)kaddr == cpu_to_le32(0x72626968)/*'hibr'*/) {
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001313 ntfs_debug("Magic \"hibr\" found in hiberfil.sys. Windows is "
1314 "hibernated on the volume. This is the "
1315 "system volume.");
1316 goto unm_iput_out;
1317 }
1318 kend = kaddr + NTFS_HIBERFIL_HEADER_SIZE/sizeof(*kaddr);
1319 do {
1320 if (unlikely(*kaddr)) {
1321 ntfs_debug("hiberfil.sys is larger than 4kiB "
1322 "(0x%llx), does not contain the "
1323 "\"hibr\" magic, and does not have a "
1324 "zero header. Windows is hibernated "
1325 "on the volume. This is not the "
1326 "system volume.", i_size_read(vi));
1327 goto unm_iput_out;
1328 }
1329 } while (++kaddr < kend);
1330 ntfs_debug("hiberfil.sys contains a zero header. Windows is not "
1331 "hibernated on the volume. This is the system "
1332 "volume.");
1333 ret = 0;
1334unm_iput_out:
1335 ntfs_unmap_page(page);
1336iput_out:
1337 iput(vi);
1338 return ret;
1339}
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341/**
1342 * load_and_init_quota - load and setup the quota file for a volume if present
1343 * @vol: ntfs super block describing device whose quota file to load
1344 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001345 * Return 'true' on success or 'false' on error. If $Quota is not present, we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 * leave vol->quota_ino as NULL and return success.
1347 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001348static bool load_and_init_quota(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 MFT_REF mref;
1351 struct inode *tmp_ino;
1352 ntfs_name *name = NULL;
Harvey Harrison63cd8852009-03-31 15:23:52 -07001353 static const ntfschar Quota[7] = { cpu_to_le16('$'),
1354 cpu_to_le16('Q'), cpu_to_le16('u'),
1355 cpu_to_le16('o'), cpu_to_le16('t'),
1356 cpu_to_le16('a'), 0 };
1357 static ntfschar Q[3] = { cpu_to_le16('$'),
1358 cpu_to_le16('Q'), 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 ntfs_debug("Entering.");
1361 /*
1362 * Find the inode number for the quota file by looking up the filename
1363 * $Quota in the extended system files directory $Extend.
1364 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001365 mutex_lock(&vol->extend_ino->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), Quota, 6,
1367 &name);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001368 mutex_unlock(&vol->extend_ino->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (IS_ERR_MREF(mref)) {
1370 /*
1371 * If the file does not exist, quotas are disabled and have
1372 * never been enabled on this volume, just return success.
1373 */
1374 if (MREF_ERR(mref) == -ENOENT) {
1375 ntfs_debug("$Quota not present. Volume does not have "
1376 "quotas enabled.");
1377 /*
1378 * No need to try to set quotas out of date if they are
1379 * not enabled.
1380 */
1381 NVolSetQuotaOutOfDate(vol);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001382 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384 /* A real error occured. */
1385 ntfs_error(vol->sb, "Failed to find inode number for $Quota.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001386 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388 /* We do not care for the type of match that was found. */
Jesper Juhl251c8422005-04-04 14:59:56 +01001389 kfree(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /* Get the inode. */
1391 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1392 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1393 if (!IS_ERR(tmp_ino))
1394 iput(tmp_ino);
1395 ntfs_error(vol->sb, "Failed to load $Quota.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001396 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398 vol->quota_ino = tmp_ino;
1399 /* Get the $Q index allocation attribute. */
1400 tmp_ino = ntfs_index_iget(vol->quota_ino, Q, 2);
1401 if (IS_ERR(tmp_ino)) {
1402 ntfs_error(vol->sb, "Failed to load $Quota/$Q index.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001403 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405 vol->quota_q_ino = tmp_ino;
1406 ntfs_debug("Done.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001407 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
1410/**
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001411 * load_and_init_usnjrnl - load and setup the transaction log if present
1412 * @vol: ntfs super block describing device whose usnjrnl file to load
1413 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001414 * Return 'true' on success or 'false' on error.
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001415 *
1416 * If $UsnJrnl is not present or in the process of being disabled, we set
1417 * NVolUsnJrnlStamped() and return success.
1418 *
1419 * If the $UsnJrnl $DATA/$J attribute has a size equal to the lowest valid usn,
1420 * i.e. transaction logging has only just been enabled or the journal has been
1421 * stamped and nothing has been logged since, we also set NVolUsnJrnlStamped()
1422 * and return success.
1423 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001424static bool load_and_init_usnjrnl(ntfs_volume *vol)
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001425{
1426 MFT_REF mref;
1427 struct inode *tmp_ino;
1428 ntfs_inode *tmp_ni;
1429 struct page *page;
1430 ntfs_name *name = NULL;
1431 USN_HEADER *uh;
Harvey Harrison63cd8852009-03-31 15:23:52 -07001432 static const ntfschar UsnJrnl[9] = { cpu_to_le16('$'),
1433 cpu_to_le16('U'), cpu_to_le16('s'),
1434 cpu_to_le16('n'), cpu_to_le16('J'),
1435 cpu_to_le16('r'), cpu_to_le16('n'),
1436 cpu_to_le16('l'), 0 };
1437 static ntfschar Max[5] = { cpu_to_le16('$'),
1438 cpu_to_le16('M'), cpu_to_le16('a'),
1439 cpu_to_le16('x'), 0 };
1440 static ntfschar J[3] = { cpu_to_le16('$'),
1441 cpu_to_le16('J'), 0 };
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001442
1443 ntfs_debug("Entering.");
1444 /*
1445 * Find the inode number for the transaction log file by looking up the
1446 * filename $UsnJrnl in the extended system files directory $Extend.
1447 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001448 mutex_lock(&vol->extend_ino->i_mutex);
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001449 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), UsnJrnl, 8,
1450 &name);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001451 mutex_unlock(&vol->extend_ino->i_mutex);
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001452 if (IS_ERR_MREF(mref)) {
1453 /*
1454 * If the file does not exist, transaction logging is disabled,
1455 * just return success.
1456 */
1457 if (MREF_ERR(mref) == -ENOENT) {
1458 ntfs_debug("$UsnJrnl not present. Volume does not "
1459 "have transaction logging enabled.");
1460not_enabled:
1461 /*
1462 * No need to try to stamp the transaction log if
1463 * transaction logging is not enabled.
1464 */
1465 NVolSetUsnJrnlStamped(vol);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001466 return true;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001467 }
1468 /* A real error occured. */
1469 ntfs_error(vol->sb, "Failed to find inode number for "
1470 "$UsnJrnl.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001471 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001472 }
1473 /* We do not care for the type of match that was found. */
1474 kfree(name);
1475 /* Get the inode. */
1476 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1477 if (unlikely(IS_ERR(tmp_ino) || is_bad_inode(tmp_ino))) {
1478 if (!IS_ERR(tmp_ino))
1479 iput(tmp_ino);
1480 ntfs_error(vol->sb, "Failed to load $UsnJrnl.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001481 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001482 }
1483 vol->usnjrnl_ino = tmp_ino;
1484 /*
1485 * If the transaction log is in the process of being deleted, we can
1486 * ignore it.
1487 */
1488 if (unlikely(vol->vol_flags & VOLUME_DELETE_USN_UNDERWAY)) {
1489 ntfs_debug("$UsnJrnl in the process of being disabled. "
1490 "Volume does not have transaction logging "
1491 "enabled.");
1492 goto not_enabled;
1493 }
1494 /* Get the $DATA/$Max attribute. */
1495 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, Max, 4);
1496 if (IS_ERR(tmp_ino)) {
1497 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$Max "
1498 "attribute.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001499 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001500 }
1501 vol->usnjrnl_max_ino = tmp_ino;
1502 if (unlikely(i_size_read(tmp_ino) < sizeof(USN_HEADER))) {
1503 ntfs_error(vol->sb, "Found corrupt $UsnJrnl/$DATA/$Max "
1504 "attribute (size is 0x%llx but should be at "
Anton Altaparmakovd04bd1f2005-10-24 08:41:24 +01001505 "least 0x%zx bytes).", i_size_read(tmp_ino),
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001506 sizeof(USN_HEADER));
Richard Knutssonc49c3112006-09-30 23:27:12 -07001507 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001508 }
1509 /* Get the $DATA/$J attribute. */
1510 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, J, 2);
1511 if (IS_ERR(tmp_ino)) {
1512 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$J "
1513 "attribute.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001514 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001515 }
1516 vol->usnjrnl_j_ino = tmp_ino;
1517 /* Verify $J is non-resident and sparse. */
1518 tmp_ni = NTFS_I(vol->usnjrnl_j_ino);
1519 if (unlikely(!NInoNonResident(tmp_ni) || !NInoSparse(tmp_ni))) {
1520 ntfs_error(vol->sb, "$UsnJrnl/$DATA/$J attribute is resident "
1521 "and/or not sparse.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001522 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001523 }
1524 /* Read the USN_HEADER from $DATA/$Max. */
1525 page = ntfs_map_page(vol->usnjrnl_max_ino->i_mapping, 0);
1526 if (IS_ERR(page)) {
1527 ntfs_error(vol->sb, "Failed to read from $UsnJrnl/$DATA/$Max "
1528 "attribute.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001529 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001530 }
1531 uh = (USN_HEADER*)page_address(page);
1532 /* Sanity check the $Max. */
1533 if (unlikely(sle64_to_cpu(uh->allocation_delta) >
1534 sle64_to_cpu(uh->maximum_size))) {
1535 ntfs_error(vol->sb, "Allocation delta (0x%llx) exceeds "
1536 "maximum size (0x%llx). $UsnJrnl is corrupt.",
1537 (long long)sle64_to_cpu(uh->allocation_delta),
1538 (long long)sle64_to_cpu(uh->maximum_size));
1539 ntfs_unmap_page(page);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001540 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001541 }
1542 /*
1543 * If the transaction log has been stamped and nothing has been written
1544 * to it since, we do not need to stamp it.
1545 */
1546 if (unlikely(sle64_to_cpu(uh->lowest_valid_usn) >=
1547 i_size_read(vol->usnjrnl_j_ino))) {
1548 if (likely(sle64_to_cpu(uh->lowest_valid_usn) ==
1549 i_size_read(vol->usnjrnl_j_ino))) {
1550 ntfs_unmap_page(page);
1551 ntfs_debug("$UsnJrnl is enabled but nothing has been "
1552 "logged since it was last stamped. "
1553 "Treating this as if the volume does "
1554 "not have transaction logging "
1555 "enabled.");
1556 goto not_enabled;
1557 }
1558 ntfs_error(vol->sb, "$UsnJrnl has lowest valid usn (0x%llx) "
1559 "which is out of bounds (0x%llx). $UsnJrnl "
1560 "is corrupt.",
1561 (long long)sle64_to_cpu(uh->lowest_valid_usn),
1562 i_size_read(vol->usnjrnl_j_ino));
1563 ntfs_unmap_page(page);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001564 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001565 }
1566 ntfs_unmap_page(page);
1567 ntfs_debug("Done.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001568 return true;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001569}
1570
1571/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 * load_and_init_attrdef - load the attribute definitions table for a volume
1573 * @vol: ntfs super block describing device whose attrdef to load
1574 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001575 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001577static bool load_and_init_attrdef(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001579 loff_t i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 struct super_block *sb = vol->sb;
1581 struct inode *ino;
1582 struct page *page;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001583 pgoff_t index, max_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 unsigned int size;
1585
1586 ntfs_debug("Entering.");
1587 /* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */
1588 ino = ntfs_iget(sb, FILE_AttrDef);
1589 if (IS_ERR(ino) || is_bad_inode(ino)) {
1590 if (!IS_ERR(ino))
1591 iput(ino);
1592 goto failed;
1593 }
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001594 NInoSetSparseDisabled(NTFS_I(ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001596 i_size = i_size_read(ino);
1597 if (i_size <= 0 || i_size > 0x7fffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 goto iput_failed;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001599 vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (!vol->attrdef)
1601 goto iput_failed;
1602 index = 0;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001603 max_index = i_size >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 size = PAGE_CACHE_SIZE;
1605 while (index < max_index) {
1606 /* Read the attrdef table and copy it into the linear buffer. */
1607read_partial_attrdef_page:
1608 page = ntfs_map_page(ino->i_mapping, index);
1609 if (IS_ERR(page))
1610 goto free_iput_failed;
1611 memcpy((u8*)vol->attrdef + (index++ << PAGE_CACHE_SHIFT),
1612 page_address(page), size);
1613 ntfs_unmap_page(page);
1614 };
1615 if (size == PAGE_CACHE_SIZE) {
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001616 size = i_size & ~PAGE_CACHE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (size)
1618 goto read_partial_attrdef_page;
1619 }
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001620 vol->attrdef_size = i_size;
1621 ntfs_debug("Read %llu bytes from $AttrDef.", i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 iput(ino);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001623 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624free_iput_failed:
1625 ntfs_free(vol->attrdef);
1626 vol->attrdef = NULL;
1627iput_failed:
1628 iput(ino);
1629failed:
1630 ntfs_error(sb, "Failed to initialize attribute definition table.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001631 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633
1634#endif /* NTFS_RW */
1635
1636/**
1637 * load_and_init_upcase - load the upcase table for an ntfs volume
1638 * @vol: ntfs super block describing device whose upcase to load
1639 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001640 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001642static bool load_and_init_upcase(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001644 loff_t i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 struct super_block *sb = vol->sb;
1646 struct inode *ino;
1647 struct page *page;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001648 pgoff_t index, max_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 unsigned int size;
1650 int i, max;
1651
1652 ntfs_debug("Entering.");
1653 /* Read upcase table and setup vol->upcase and vol->upcase_len. */
1654 ino = ntfs_iget(sb, FILE_UpCase);
1655 if (IS_ERR(ino) || is_bad_inode(ino)) {
1656 if (!IS_ERR(ino))
1657 iput(ino);
1658 goto upcase_failed;
1659 }
1660 /*
1661 * The upcase size must not be above 64k Unicode characters, must not
1662 * be zero and must be a multiple of sizeof(ntfschar).
1663 */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001664 i_size = i_size_read(ino);
1665 if (!i_size || i_size & (sizeof(ntfschar) - 1) ||
1666 i_size > 64ULL * 1024 * sizeof(ntfschar))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 goto iput_upcase_failed;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001668 vol->upcase = (ntfschar*)ntfs_malloc_nofs(i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if (!vol->upcase)
1670 goto iput_upcase_failed;
1671 index = 0;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001672 max_index = i_size >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 size = PAGE_CACHE_SIZE;
1674 while (index < max_index) {
1675 /* Read the upcase table and copy it into the linear buffer. */
1676read_partial_upcase_page:
1677 page = ntfs_map_page(ino->i_mapping, index);
1678 if (IS_ERR(page))
1679 goto iput_upcase_failed;
1680 memcpy((char*)vol->upcase + (index++ << PAGE_CACHE_SHIFT),
1681 page_address(page), size);
1682 ntfs_unmap_page(page);
1683 };
1684 if (size == PAGE_CACHE_SIZE) {
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001685 size = i_size & ~PAGE_CACHE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (size)
1687 goto read_partial_upcase_page;
1688 }
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001689 vol->upcase_len = i_size >> UCHAR_T_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001691 i_size, 64 * 1024 * sizeof(ntfschar));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 iput(ino);
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001693 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (!default_upcase) {
1695 ntfs_debug("Using volume specified $UpCase since default is "
1696 "not present.");
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001697 mutex_unlock(&ntfs_lock);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001698 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700 max = default_upcase_len;
1701 if (max > vol->upcase_len)
1702 max = vol->upcase_len;
1703 for (i = 0; i < max; i++)
1704 if (vol->upcase[i] != default_upcase[i])
1705 break;
1706 if (i == max) {
1707 ntfs_free(vol->upcase);
1708 vol->upcase = default_upcase;
1709 vol->upcase_len = max;
1710 ntfs_nr_upcase_users++;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001711 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 ntfs_debug("Volume specified $UpCase matches default. Using "
1713 "default.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001714 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001716 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 ntfs_debug("Using volume specified $UpCase since it does not match "
1718 "the default.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001719 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720iput_upcase_failed:
1721 iput(ino);
1722 ntfs_free(vol->upcase);
1723 vol->upcase = NULL;
1724upcase_failed:
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001725 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (default_upcase) {
1727 vol->upcase = default_upcase;
1728 vol->upcase_len = default_upcase_len;
1729 ntfs_nr_upcase_users++;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001730 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 ntfs_error(sb, "Failed to load $UpCase from the volume. Using "
1732 "default.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001733 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001735 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 ntfs_error(sb, "Failed to initialize upcase table.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001737 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}
1739
Ingo Molnar59345372006-07-03 00:25:18 -07001740/*
1741 * The lcn and mft bitmap inodes are NTFS-internal inodes with
1742 * their own special locking rules:
1743 */
1744static struct lock_class_key
1745 lcnbmp_runlist_lock_key, lcnbmp_mrec_lock_key,
1746 mftbmp_runlist_lock_key, mftbmp_mrec_lock_key;
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748/**
1749 * load_system_files - open the system files using normal functions
1750 * @vol: ntfs super block describing device whose system files to load
1751 *
1752 * Open the system files with normal access functions and complete setting up
1753 * the ntfs super block @vol.
1754 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001755 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001757static bool load_system_files(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
1759 struct super_block *sb = vol->sb;
1760 MFT_RECORD *m;
1761 VOLUME_INFORMATION *vi;
1762 ntfs_attr_search_ctx *ctx;
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001763#ifdef NTFS_RW
Anton Altaparmakov7d333d62005-09-08 23:01:16 +01001764 RESTART_PAGE_HEADER *rp;
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001765 int err;
1766#endif /* NTFS_RW */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 ntfs_debug("Entering.");
1769#ifdef NTFS_RW
1770 /* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */
1771 if (!load_and_init_mft_mirror(vol) || !check_mft_mirror(vol)) {
1772 static const char *es1 = "Failed to load $MFTMirr";
1773 static const char *es2 = "$MFTMirr does not match $MFT";
1774 static const char *es3 = ". Run ntfsfix and/or chkdsk.";
1775
1776 /* If a read-write mount, convert it to a read-only mount. */
1777 if (!(sb->s_flags & MS_RDONLY)) {
1778 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1779 ON_ERRORS_CONTINUE))) {
1780 ntfs_error(sb, "%s and neither on_errors="
1781 "continue nor on_errors="
1782 "remount-ro was specified%s",
1783 !vol->mftmirr_ino ? es1 : es2,
1784 es3);
1785 goto iput_mirr_err_out;
1786 }
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08001787 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 ntfs_error(sb, "%s. Mounting read-only%s",
1789 !vol->mftmirr_ino ? es1 : es2, es3);
1790 } else
1791 ntfs_warning(sb, "%s. Will not be able to remount "
1792 "read-write%s",
1793 !vol->mftmirr_ino ? es1 : es2, es3);
1794 /* This will prevent a read-write remount. */
1795 NVolSetErrors(vol);
1796 }
1797#endif /* NTFS_RW */
1798 /* Get mft bitmap attribute inode. */
1799 vol->mftbmp_ino = ntfs_attr_iget(vol->mft_ino, AT_BITMAP, NULL, 0);
1800 if (IS_ERR(vol->mftbmp_ino)) {
1801 ntfs_error(sb, "Failed to load $MFT/$BITMAP attribute.");
1802 goto iput_mirr_err_out;
1803 }
Ingo Molnar59345372006-07-03 00:25:18 -07001804 lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->runlist.lock,
1805 &mftbmp_runlist_lock_key);
1806 lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->mrec_lock,
1807 &mftbmp_mrec_lock_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */
1809 if (!load_and_init_upcase(vol))
1810 goto iput_mftbmp_err_out;
1811#ifdef NTFS_RW
1812 /*
1813 * Read attribute definitions table and setup @vol->attrdef and
1814 * @vol->attrdef_size.
1815 */
1816 if (!load_and_init_attrdef(vol))
1817 goto iput_upcase_err_out;
1818#endif /* NTFS_RW */
1819 /*
1820 * Get the cluster allocation bitmap inode and verify the size, no
1821 * need for any locking at this stage as we are already running
1822 * exclusively as we are mount in progress task.
1823 */
1824 vol->lcnbmp_ino = ntfs_iget(sb, FILE_Bitmap);
1825 if (IS_ERR(vol->lcnbmp_ino) || is_bad_inode(vol->lcnbmp_ino)) {
1826 if (!IS_ERR(vol->lcnbmp_ino))
1827 iput(vol->lcnbmp_ino);
1828 goto bitmap_failed;
1829 }
Ingo Molnar59345372006-07-03 00:25:18 -07001830 lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->runlist.lock,
1831 &lcnbmp_runlist_lock_key);
1832 lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->mrec_lock,
1833 &lcnbmp_mrec_lock_key);
1834
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001835 NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino));
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001836 if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 iput(vol->lcnbmp_ino);
1838bitmap_failed:
1839 ntfs_error(sb, "Failed to load $Bitmap.");
1840 goto iput_attrdef_err_out;
1841 }
1842 /*
1843 * Get the volume inode and setup our cache of the volume flags and
1844 * version.
1845 */
1846 vol->vol_ino = ntfs_iget(sb, FILE_Volume);
1847 if (IS_ERR(vol->vol_ino) || is_bad_inode(vol->vol_ino)) {
1848 if (!IS_ERR(vol->vol_ino))
1849 iput(vol->vol_ino);
1850volume_failed:
1851 ntfs_error(sb, "Failed to load $Volume.");
1852 goto iput_lcnbmp_err_out;
1853 }
1854 m = map_mft_record(NTFS_I(vol->vol_ino));
1855 if (IS_ERR(m)) {
1856iput_volume_failed:
1857 iput(vol->vol_ino);
1858 goto volume_failed;
1859 }
1860 if (!(ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m))) {
1861 ntfs_error(sb, "Failed to get attribute search context.");
1862 goto get_ctx_vol_failed;
1863 }
1864 if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
1865 ctx) || ctx->attr->non_resident || ctx->attr->flags) {
1866err_put_vol:
1867 ntfs_attr_put_search_ctx(ctx);
1868get_ctx_vol_failed:
1869 unmap_mft_record(NTFS_I(vol->vol_ino));
1870 goto iput_volume_failed;
1871 }
1872 vi = (VOLUME_INFORMATION*)((char*)ctx->attr +
1873 le16_to_cpu(ctx->attr->data.resident.value_offset));
1874 /* Some bounds checks. */
1875 if ((u8*)vi < (u8*)ctx->attr || (u8*)vi +
1876 le32_to_cpu(ctx->attr->data.resident.value_length) >
1877 (u8*)ctx->attr + le32_to_cpu(ctx->attr->length))
1878 goto err_put_vol;
1879 /* Copy the volume flags and version to the ntfs_volume structure. */
1880 vol->vol_flags = vi->flags;
1881 vol->major_ver = vi->major_ver;
1882 vol->minor_ver = vi->minor_ver;
1883 ntfs_attr_put_search_ctx(ctx);
1884 unmap_mft_record(NTFS_I(vol->vol_ino));
1885 printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver,
1886 vol->minor_ver);
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001887 if (vol->major_ver < 3 && NVolSparseEnabled(vol)) {
1888 ntfs_warning(vol->sb, "Disabling sparse support due to NTFS "
1889 "volume version %i.%i (need at least version "
1890 "3.0).", vol->major_ver, vol->minor_ver);
1891 NVolClearSparseEnabled(vol);
1892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893#ifdef NTFS_RW
1894 /* Make sure that no unsupported volume flags are set. */
1895 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
1896 static const char *es1a = "Volume is dirty";
Anton Altaparmakov1cf3109f2006-02-24 10:48:14 +00001897 static const char *es1b = "Volume has been modified by chkdsk";
1898 static const char *es1c = "Volume has unsupported flags set";
1899 static const char *es2a = ". Run chkdsk and mount in Windows.";
1900 static const char *es2b = ". Mount in Windows.";
1901 const char *es1, *es2;
1902
1903 es2 = es2a;
1904 if (vol->vol_flags & VOLUME_IS_DIRTY)
1905 es1 = es1a;
1906 else if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
1907 es1 = es1b;
1908 es2 = es2b;
1909 } else {
1910 es1 = es1c;
1911 ntfs_warning(sb, "Unsupported volume flags 0x%x "
1912 "encountered.",
1913 (unsigned)le16_to_cpu(vol->vol_flags));
1914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 /* If a read-write mount, convert it to a read-only mount. */
1916 if (!(sb->s_flags & MS_RDONLY)) {
1917 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1918 ON_ERRORS_CONTINUE))) {
1919 ntfs_error(sb, "%s and neither on_errors="
1920 "continue nor on_errors="
1921 "remount-ro was specified%s",
1922 es1, es2);
1923 goto iput_vol_err_out;
1924 }
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08001925 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1927 } else
1928 ntfs_warning(sb, "%s. Will not be able to remount "
1929 "read-write%s", es1, es2);
1930 /*
1931 * Do not set NVolErrors() because ntfs_remount() re-checks the
1932 * flags which we need to do in case any flags have changed.
1933 */
1934 }
1935 /*
1936 * Get the inode for the logfile, check it and determine if the volume
1937 * was shutdown cleanly.
1938 */
Anton Altaparmakove7a10332005-09-08 16:12:28 +01001939 rp = NULL;
1940 if (!load_and_check_logfile(vol, &rp) ||
1941 !ntfs_is_logfile_clean(vol->logfile_ino, rp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 static const char *es1a = "Failed to load $LogFile";
1943 static const char *es1b = "$LogFile is not clean";
1944 static const char *es2 = ". Mount in Windows.";
1945 const char *es1;
1946
1947 es1 = !vol->logfile_ino ? es1a : es1b;
1948 /* If a read-write mount, convert it to a read-only mount. */
1949 if (!(sb->s_flags & MS_RDONLY)) {
1950 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1951 ON_ERRORS_CONTINUE))) {
1952 ntfs_error(sb, "%s and neither on_errors="
1953 "continue nor on_errors="
1954 "remount-ro was specified%s",
1955 es1, es2);
Anton Altaparmakove7a10332005-09-08 16:12:28 +01001956 if (vol->logfile_ino) {
1957 BUG_ON(!rp);
1958 ntfs_free(rp);
1959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 goto iput_logfile_err_out;
1961 }
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08001962 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1964 } else
1965 ntfs_warning(sb, "%s. Will not be able to remount "
1966 "read-write%s", es1, es2);
1967 /* This will prevent a read-write remount. */
1968 NVolSetErrors(vol);
1969 }
Anton Altaparmakove7a10332005-09-08 16:12:28 +01001970 ntfs_free(rp);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001971#endif /* NTFS_RW */
1972 /* Get the root directory inode so we can do path lookups. */
1973 vol->root_ino = ntfs_iget(sb, FILE_root);
1974 if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) {
1975 if (!IS_ERR(vol->root_ino))
1976 iput(vol->root_ino);
1977 ntfs_error(sb, "Failed to load root directory.");
1978 goto iput_logfile_err_out;
1979 }
1980#ifdef NTFS_RW
1981 /*
1982 * Check if Windows is suspended to disk on the target volume. If it
1983 * is hibernated, we must not write *anything* to the disk so set
1984 * NVolErrors() without setting the dirty volume flag and mount
1985 * read-only. This will prevent read-write remounting and it will also
1986 * prevent all writes.
1987 */
1988 err = check_windows_hibernation_status(vol);
1989 if (unlikely(err)) {
1990 static const char *es1a = "Failed to determine if Windows is "
1991 "hibernated";
1992 static const char *es1b = "Windows is hibernated";
1993 static const char *es2 = ". Run chkdsk.";
1994 const char *es1;
1995
1996 es1 = err < 0 ? es1a : es1b;
1997 /* If a read-write mount, convert it to a read-only mount. */
1998 if (!(sb->s_flags & MS_RDONLY)) {
1999 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2000 ON_ERRORS_CONTINUE))) {
2001 ntfs_error(sb, "%s and neither on_errors="
2002 "continue nor on_errors="
2003 "remount-ro was specified%s",
2004 es1, es2);
2005 goto iput_root_err_out;
2006 }
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002007 sb->s_flags |= MS_RDONLY;
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01002008 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2009 } else
2010 ntfs_warning(sb, "%s. Will not be able to remount "
2011 "read-write%s", es1, es2);
2012 /* This will prevent a read-write remount. */
2013 NVolSetErrors(vol);
2014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 /* If (still) a read-write mount, mark the volume dirty. */
2016 if (!(sb->s_flags & MS_RDONLY) &&
2017 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
2018 static const char *es1 = "Failed to set dirty bit in volume "
2019 "information flags";
2020 static const char *es2 = ". Run chkdsk.";
2021
2022 /* Convert to a read-only mount. */
2023 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2024 ON_ERRORS_CONTINUE))) {
2025 ntfs_error(sb, "%s and neither on_errors=continue nor "
2026 "on_errors=remount-ro was specified%s",
2027 es1, es2);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01002028 goto iput_root_err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
2030 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002031 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 /*
2033 * Do not set NVolErrors() because ntfs_remount() might manage
2034 * to set the dirty flag in which case all would be well.
2035 */
2036 }
2037#if 0
2038 // TODO: Enable this code once we start modifying anything that is
2039 // different between NTFS 1.2 and 3.x...
2040 /*
2041 * If (still) a read-write mount, set the NT4 compatibility flag on
2042 * newer NTFS version volumes.
2043 */
2044 if (!(sb->s_flags & MS_RDONLY) && (vol->major_ver > 1) &&
2045 ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
2046 static const char *es1 = "Failed to set NT4 compatibility flag";
2047 static const char *es2 = ". Run chkdsk.";
2048
2049 /* Convert to a read-only mount. */
2050 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2051 ON_ERRORS_CONTINUE))) {
2052 ntfs_error(sb, "%s and neither on_errors=continue nor "
2053 "on_errors=remount-ro was specified%s",
2054 es1, es2);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01002055 goto iput_root_err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 }
2057 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002058 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 NVolSetErrors(vol);
2060 }
2061#endif
2062 /* If (still) a read-write mount, empty the logfile. */
2063 if (!(sb->s_flags & MS_RDONLY) &&
2064 !ntfs_empty_logfile(vol->logfile_ino)) {
2065 static const char *es1 = "Failed to empty $LogFile";
2066 static const char *es2 = ". Mount in Windows.";
2067
2068 /* Convert to a read-only mount. */
2069 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2070 ON_ERRORS_CONTINUE))) {
2071 ntfs_error(sb, "%s and neither on_errors=continue nor "
2072 "on_errors=remount-ro was specified%s",
2073 es1, es2);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01002074 goto iput_root_err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 }
2076 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002077 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 NVolSetErrors(vol);
2079 }
2080#endif /* NTFS_RW */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 /* If on NTFS versions before 3.0, we are done. */
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002082 if (unlikely(vol->major_ver < 3))
Richard Knutssonc49c3112006-09-30 23:27:12 -07002083 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 /* NTFS 3.0+ specific initialization. */
2085 /* Get the security descriptors inode. */
2086 vol->secure_ino = ntfs_iget(sb, FILE_Secure);
2087 if (IS_ERR(vol->secure_ino) || is_bad_inode(vol->secure_ino)) {
2088 if (!IS_ERR(vol->secure_ino))
2089 iput(vol->secure_ino);
2090 ntfs_error(sb, "Failed to load $Secure.");
2091 goto iput_root_err_out;
2092 }
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002093 // TODO: Initialize security.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 /* Get the extended system files' directory inode. */
2095 vol->extend_ino = ntfs_iget(sb, FILE_Extend);
2096 if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) {
2097 if (!IS_ERR(vol->extend_ino))
2098 iput(vol->extend_ino);
2099 ntfs_error(sb, "Failed to load $Extend.");
2100 goto iput_sec_err_out;
2101 }
2102#ifdef NTFS_RW
2103 /* Find the quota file, load it if present, and set it up. */
2104 if (!load_and_init_quota(vol)) {
2105 static const char *es1 = "Failed to load $Quota";
2106 static const char *es2 = ". Run chkdsk.";
2107
2108 /* If a read-write mount, convert it to a read-only mount. */
2109 if (!(sb->s_flags & MS_RDONLY)) {
2110 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2111 ON_ERRORS_CONTINUE))) {
2112 ntfs_error(sb, "%s and neither on_errors="
2113 "continue nor on_errors="
2114 "remount-ro was specified%s",
2115 es1, es2);
2116 goto iput_quota_err_out;
2117 }
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002118 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2120 } else
2121 ntfs_warning(sb, "%s. Will not be able to remount "
2122 "read-write%s", es1, es2);
2123 /* This will prevent a read-write remount. */
2124 NVolSetErrors(vol);
2125 }
2126 /* If (still) a read-write mount, mark the quotas out of date. */
2127 if (!(sb->s_flags & MS_RDONLY) &&
2128 !ntfs_mark_quotas_out_of_date(vol)) {
2129 static const char *es1 = "Failed to mark quotas out of date";
2130 static const char *es2 = ". Run chkdsk.";
2131
2132 /* Convert to a read-only mount. */
2133 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2134 ON_ERRORS_CONTINUE))) {
2135 ntfs_error(sb, "%s and neither on_errors=continue nor "
2136 "on_errors=remount-ro was specified%s",
2137 es1, es2);
2138 goto iput_quota_err_out;
2139 }
2140 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002141 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 NVolSetErrors(vol);
2143 }
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002144 /*
2145 * Find the transaction log file ($UsnJrnl), load it if present, check
2146 * it, and set it up.
2147 */
2148 if (!load_and_init_usnjrnl(vol)) {
2149 static const char *es1 = "Failed to load $UsnJrnl";
2150 static const char *es2 = ". Run chkdsk.";
2151
2152 /* If a read-write mount, convert it to a read-only mount. */
2153 if (!(sb->s_flags & MS_RDONLY)) {
2154 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2155 ON_ERRORS_CONTINUE))) {
2156 ntfs_error(sb, "%s and neither on_errors="
2157 "continue nor on_errors="
2158 "remount-ro was specified%s",
2159 es1, es2);
2160 goto iput_usnjrnl_err_out;
2161 }
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002162 sb->s_flags |= MS_RDONLY;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002163 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2164 } else
2165 ntfs_warning(sb, "%s. Will not be able to remount "
2166 "read-write%s", es1, es2);
2167 /* This will prevent a read-write remount. */
2168 NVolSetErrors(vol);
2169 }
2170 /* If (still) a read-write mount, stamp the transaction log. */
2171 if (!(sb->s_flags & MS_RDONLY) && !ntfs_stamp_usnjrnl(vol)) {
2172 static const char *es1 = "Failed to stamp transaction log "
2173 "($UsnJrnl)";
2174 static const char *es2 = ". Run chkdsk.";
2175
2176 /* Convert to a read-only mount. */
2177 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2178 ON_ERRORS_CONTINUE))) {
2179 ntfs_error(sb, "%s and neither on_errors=continue nor "
2180 "on_errors=remount-ro was specified%s",
2181 es1, es2);
2182 goto iput_usnjrnl_err_out;
2183 }
2184 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002185 sb->s_flags |= MS_RDONLY;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002186 NVolSetErrors(vol);
2187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188#endif /* NTFS_RW */
Richard Knutssonc49c3112006-09-30 23:27:12 -07002189 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190#ifdef NTFS_RW
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002191iput_usnjrnl_err_out:
2192 if (vol->usnjrnl_j_ino)
2193 iput(vol->usnjrnl_j_ino);
2194 if (vol->usnjrnl_max_ino)
2195 iput(vol->usnjrnl_max_ino);
2196 if (vol->usnjrnl_ino)
2197 iput(vol->usnjrnl_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198iput_quota_err_out:
2199 if (vol->quota_q_ino)
2200 iput(vol->quota_q_ino);
2201 if (vol->quota_ino)
2202 iput(vol->quota_ino);
2203 iput(vol->extend_ino);
2204#endif /* NTFS_RW */
2205iput_sec_err_out:
2206 iput(vol->secure_ino);
2207iput_root_err_out:
2208 iput(vol->root_ino);
2209iput_logfile_err_out:
2210#ifdef NTFS_RW
2211 if (vol->logfile_ino)
2212 iput(vol->logfile_ino);
2213iput_vol_err_out:
2214#endif /* NTFS_RW */
2215 iput(vol->vol_ino);
2216iput_lcnbmp_err_out:
2217 iput(vol->lcnbmp_ino);
2218iput_attrdef_err_out:
2219 vol->attrdef_size = 0;
2220 if (vol->attrdef) {
2221 ntfs_free(vol->attrdef);
2222 vol->attrdef = NULL;
2223 }
2224#ifdef NTFS_RW
2225iput_upcase_err_out:
2226#endif /* NTFS_RW */
2227 vol->upcase_len = 0;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002228 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (vol->upcase == default_upcase) {
2230 ntfs_nr_upcase_users--;
2231 vol->upcase = NULL;
2232 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002233 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 if (vol->upcase) {
2235 ntfs_free(vol->upcase);
2236 vol->upcase = NULL;
2237 }
2238iput_mftbmp_err_out:
2239 iput(vol->mftbmp_ino);
2240iput_mirr_err_out:
2241#ifdef NTFS_RW
2242 if (vol->mftmirr_ino)
2243 iput(vol->mftmirr_ino);
2244#endif /* NTFS_RW */
Richard Knutssonc49c3112006-09-30 23:27:12 -07002245 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
2247
2248/**
2249 * ntfs_put_super - called by the vfs to unmount a volume
2250 * @sb: vfs superblock of volume to unmount
2251 *
2252 * ntfs_put_super() is called by the VFS (from fs/super.c::do_umount()) when
2253 * the volume is being unmounted (umount system call has been invoked) and it
2254 * releases all inodes and memory belonging to the NTFS specific part of the
2255 * super block.
2256 */
2257static void ntfs_put_super(struct super_block *sb)
2258{
2259 ntfs_volume *vol = NTFS_SB(sb);
2260
2261 ntfs_debug("Entering.");
Christoph Hellwig6cfd0142009-05-05 15:40:36 +02002262
2263 lock_kernel();
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265#ifdef NTFS_RW
2266 /*
2267 * Commit all inodes while they are still open in case some of them
2268 * cause others to be dirtied.
2269 */
2270 ntfs_commit_inode(vol->vol_ino);
2271
2272 /* NTFS 3.0+ specific. */
2273 if (vol->major_ver >= 3) {
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002274 if (vol->usnjrnl_j_ino)
2275 ntfs_commit_inode(vol->usnjrnl_j_ino);
2276 if (vol->usnjrnl_max_ino)
2277 ntfs_commit_inode(vol->usnjrnl_max_ino);
2278 if (vol->usnjrnl_ino)
2279 ntfs_commit_inode(vol->usnjrnl_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 if (vol->quota_q_ino)
2281 ntfs_commit_inode(vol->quota_q_ino);
2282 if (vol->quota_ino)
2283 ntfs_commit_inode(vol->quota_ino);
2284 if (vol->extend_ino)
2285 ntfs_commit_inode(vol->extend_ino);
2286 if (vol->secure_ino)
2287 ntfs_commit_inode(vol->secure_ino);
2288 }
2289
2290 ntfs_commit_inode(vol->root_ino);
2291
2292 down_write(&vol->lcnbmp_lock);
2293 ntfs_commit_inode(vol->lcnbmp_ino);
2294 up_write(&vol->lcnbmp_lock);
2295
2296 down_write(&vol->mftbmp_lock);
2297 ntfs_commit_inode(vol->mftbmp_ino);
2298 up_write(&vol->mftbmp_lock);
2299
2300 if (vol->logfile_ino)
2301 ntfs_commit_inode(vol->logfile_ino);
2302
2303 if (vol->mftmirr_ino)
2304 ntfs_commit_inode(vol->mftmirr_ino);
2305 ntfs_commit_inode(vol->mft_ino);
2306
2307 /*
2308 * If a read-write mount and no volume errors have occured, mark the
2309 * volume clean. Also, re-commit all affected inodes.
2310 */
2311 if (!(sb->s_flags & MS_RDONLY)) {
2312 if (!NVolErrors(vol)) {
2313 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
2314 ntfs_warning(sb, "Failed to clear dirty bit "
2315 "in volume information "
2316 "flags. Run chkdsk.");
2317 ntfs_commit_inode(vol->vol_ino);
2318 ntfs_commit_inode(vol->root_ino);
2319 if (vol->mftmirr_ino)
2320 ntfs_commit_inode(vol->mftmirr_ino);
2321 ntfs_commit_inode(vol->mft_ino);
2322 } else {
2323 ntfs_warning(sb, "Volume has errors. Leaving volume "
2324 "marked dirty. Run chkdsk.");
2325 }
2326 }
2327#endif /* NTFS_RW */
2328
2329 iput(vol->vol_ino);
2330 vol->vol_ino = NULL;
2331
2332 /* NTFS 3.0+ specific clean up. */
2333 if (vol->major_ver >= 3) {
2334#ifdef NTFS_RW
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002335 if (vol->usnjrnl_j_ino) {
2336 iput(vol->usnjrnl_j_ino);
2337 vol->usnjrnl_j_ino = NULL;
2338 }
2339 if (vol->usnjrnl_max_ino) {
2340 iput(vol->usnjrnl_max_ino);
2341 vol->usnjrnl_max_ino = NULL;
2342 }
2343 if (vol->usnjrnl_ino) {
2344 iput(vol->usnjrnl_ino);
2345 vol->usnjrnl_ino = NULL;
2346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 if (vol->quota_q_ino) {
2348 iput(vol->quota_q_ino);
2349 vol->quota_q_ino = NULL;
2350 }
2351 if (vol->quota_ino) {
2352 iput(vol->quota_ino);
2353 vol->quota_ino = NULL;
2354 }
2355#endif /* NTFS_RW */
2356 if (vol->extend_ino) {
2357 iput(vol->extend_ino);
2358 vol->extend_ino = NULL;
2359 }
2360 if (vol->secure_ino) {
2361 iput(vol->secure_ino);
2362 vol->secure_ino = NULL;
2363 }
2364 }
2365
2366 iput(vol->root_ino);
2367 vol->root_ino = NULL;
2368
2369 down_write(&vol->lcnbmp_lock);
2370 iput(vol->lcnbmp_ino);
2371 vol->lcnbmp_ino = NULL;
2372 up_write(&vol->lcnbmp_lock);
2373
2374 down_write(&vol->mftbmp_lock);
2375 iput(vol->mftbmp_ino);
2376 vol->mftbmp_ino = NULL;
2377 up_write(&vol->mftbmp_lock);
2378
2379#ifdef NTFS_RW
2380 if (vol->logfile_ino) {
2381 iput(vol->logfile_ino);
2382 vol->logfile_ino = NULL;
2383 }
2384 if (vol->mftmirr_ino) {
2385 /* Re-commit the mft mirror and mft just in case. */
2386 ntfs_commit_inode(vol->mftmirr_ino);
2387 ntfs_commit_inode(vol->mft_ino);
2388 iput(vol->mftmirr_ino);
2389 vol->mftmirr_ino = NULL;
2390 }
2391 /*
Jens Axboe13205fb2009-05-25 09:30:45 +02002392 * We should have no dirty inodes left, due to
2393 * mft.c::ntfs_mft_writepage() cleaning all the dirty pages as
2394 * the underlying mft records are written out and cleaned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 */
2396 ntfs_commit_inode(vol->mft_ino);
2397 write_inode_now(vol->mft_ino, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398#endif /* NTFS_RW */
2399
2400 iput(vol->mft_ino);
2401 vol->mft_ino = NULL;
2402
2403 /* Throw away the table of attribute definitions. */
2404 vol->attrdef_size = 0;
2405 if (vol->attrdef) {
2406 ntfs_free(vol->attrdef);
2407 vol->attrdef = NULL;
2408 }
2409 vol->upcase_len = 0;
2410 /*
2411 * Destroy the global default upcase table if necessary. Also decrease
2412 * the number of upcase users if we are a user.
2413 */
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002414 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if (vol->upcase == default_upcase) {
2416 ntfs_nr_upcase_users--;
2417 vol->upcase = NULL;
2418 }
2419 if (!ntfs_nr_upcase_users && default_upcase) {
2420 ntfs_free(default_upcase);
2421 default_upcase = NULL;
2422 }
2423 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
2424 free_compression_buffers();
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002425 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if (vol->upcase) {
2427 ntfs_free(vol->upcase);
2428 vol->upcase = NULL;
2429 }
2430 if (vol->nls_map) {
2431 unload_nls(vol->nls_map);
2432 vol->nls_map = NULL;
2433 }
2434 sb->s_fs_info = NULL;
2435 kfree(vol);
Christoph Hellwig6cfd0142009-05-05 15:40:36 +02002436
2437 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438}
2439
2440/**
2441 * get_nr_free_clusters - return the number of free clusters on a volume
2442 * @vol: ntfs volume for which to obtain free cluster count
2443 *
2444 * Calculate the number of free clusters on the mounted NTFS volume @vol. We
2445 * actually calculate the number of clusters in use instead because this
2446 * allows us to not care about partial pages as these will be just zero filled
2447 * and hence not be counted as allocated clusters.
2448 *
2449 * The only particularity is that clusters beyond the end of the logical ntfs
2450 * volume will be marked as allocated to prevent errors which means we have to
2451 * discount those at the end. This is important as the cluster bitmap always
2452 * has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside
2453 * the logical volume and marked in use when they are not as they do not exist.
2454 *
2455 * If any pages cannot be read we assume all clusters in the erroring pages are
2456 * in use. This means we return an underestimate on errors which is better than
2457 * an overestimate.
2458 */
2459static s64 get_nr_free_clusters(ntfs_volume *vol)
2460{
2461 s64 nr_free = vol->nr_clusters;
2462 u32 *kaddr;
2463 struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 struct page *page;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002465 pgoff_t index, max_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
2467 ntfs_debug("Entering.");
2468 /* Serialize accesses to the cluster bitmap. */
2469 down_read(&vol->lcnbmp_lock);
2470 /*
2471 * Convert the number of bits into bytes rounded up, then convert into
2472 * multiples of PAGE_CACHE_SIZE, rounding up so that if we have one
2473 * full and one partial page max_index = 2.
2474 */
2475 max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >>
2476 PAGE_CACHE_SHIFT;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002477 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2478 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.",
2479 max_index, PAGE_CACHE_SIZE / 4);
2480 for (index = 0; index < max_index; index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 unsigned int i;
2482 /*
2483 * Read the page from page cache, getting it from backing store
2484 * if necessary, and increment the use count.
2485 */
Nick Piggin6fe69002007-05-06 14:49:04 -07002486 page = read_mapping_page(mapping, index, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 /* Ignore pages which errored synchronously. */
2488 if (IS_ERR(page)) {
Nick Piggin6fe69002007-05-06 14:49:04 -07002489 ntfs_debug("read_mapping_page() error. Skipping "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 "page (index 0x%lx).", index);
2491 nr_free -= PAGE_CACHE_SIZE * 8;
2492 continue;
2493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 kaddr = (u32*)kmap_atomic(page, KM_USER0);
2495 /*
2496 * For each 4 bytes, subtract the number of set bits. If this
2497 * is the last page and it is partial we don't really care as
2498 * it just means we do a little extra work but it won't affect
2499 * the result as all out of range bytes are set to zero by
2500 * ntfs_readpage().
2501 */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002502 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 nr_free -= (s64)hweight32(kaddr[i]);
2504 kunmap_atomic(kaddr, KM_USER0);
2505 page_cache_release(page);
2506 }
2507 ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index - 1);
2508 /*
2509 * Fixup for eventual bits outside logical ntfs volume (see function
2510 * description above).
2511 */
2512 if (vol->nr_clusters & 63)
2513 nr_free += 64 - (vol->nr_clusters & 63);
2514 up_read(&vol->lcnbmp_lock);
2515 /* If errors occured we may well have gone below zero, fix this. */
2516 if (nr_free < 0)
2517 nr_free = 0;
2518 ntfs_debug("Exiting.");
2519 return nr_free;
2520}
2521
2522/**
2523 * __get_nr_free_mft_records - return the number of free inodes on a volume
2524 * @vol: ntfs volume for which to obtain free inode count
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002525 * @nr_free: number of mft records in filesystem
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002526 * @max_index: maximum number of pages containing set bits
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 *
2528 * Calculate the number of free mft records (inodes) on the mounted NTFS
2529 * volume @vol. We actually calculate the number of mft records in use instead
2530 * because this allows us to not care about partial pages as these will be just
2531 * zero filled and hence not be counted as allocated mft record.
2532 *
2533 * If any pages cannot be read we assume all mft records in the erroring pages
2534 * are in use. This means we return an underestimate on errors which is better
2535 * than an overestimate.
2536 *
2537 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
2538 */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002539static unsigned long __get_nr_free_mft_records(ntfs_volume *vol,
2540 s64 nr_free, const pgoff_t max_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 u32 *kaddr;
2543 struct address_space *mapping = vol->mftbmp_ino->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 struct page *page;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002545 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
2547 ntfs_debug("Entering.");
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002548 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002550 "0x%lx.", max_index, PAGE_CACHE_SIZE / 4);
2551 for (index = 0; index < max_index; index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 unsigned int i;
2553 /*
2554 * Read the page from page cache, getting it from backing store
2555 * if necessary, and increment the use count.
2556 */
Nick Piggin6fe69002007-05-06 14:49:04 -07002557 page = read_mapping_page(mapping, index, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 /* Ignore pages which errored synchronously. */
2559 if (IS_ERR(page)) {
Nick Piggin6fe69002007-05-06 14:49:04 -07002560 ntfs_debug("read_mapping_page() error. Skipping "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 "page (index 0x%lx).", index);
2562 nr_free -= PAGE_CACHE_SIZE * 8;
2563 continue;
2564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 kaddr = (u32*)kmap_atomic(page, KM_USER0);
2566 /*
2567 * For each 4 bytes, subtract the number of set bits. If this
2568 * is the last page and it is partial we don't really care as
2569 * it just means we do a little extra work but it won't affect
2570 * the result as all out of range bytes are set to zero by
2571 * ntfs_readpage().
2572 */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002573 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 nr_free -= (s64)hweight32(kaddr[i]);
2575 kunmap_atomic(kaddr, KM_USER0);
2576 page_cache_release(page);
2577 }
2578 ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.",
2579 index - 1);
2580 /* If errors occured we may well have gone below zero, fix this. */
2581 if (nr_free < 0)
2582 nr_free = 0;
2583 ntfs_debug("Exiting.");
2584 return nr_free;
2585}
2586
2587/**
2588 * ntfs_statfs - return information about mounted NTFS volume
David Howells726c3342006-06-23 02:02:58 -07002589 * @dentry: dentry from mounted volume
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 * @sfs: statfs structure in which to return the information
2591 *
David Howells726c3342006-06-23 02:02:58 -07002592 * Return information about the mounted NTFS volume @dentry in the statfs structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is
2594 * called). We interpret the values to be correct of the moment in time at
2595 * which we are called. Most values are variable otherwise and this isn't just
2596 * the free values but the totals as well. For example we can increase the
2597 * total number of file nodes if we run out and we can keep doing this until
2598 * there is no more space on the volume left at all.
2599 *
2600 * Called from vfs_statfs which is used to handle the statfs, fstatfs, and
2601 * ustat system calls.
2602 *
2603 * Return 0 on success or -errno on error.
2604 */
David Howells726c3342006-06-23 02:02:58 -07002605static int ntfs_statfs(struct dentry *dentry, struct kstatfs *sfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
David Howells726c3342006-06-23 02:02:58 -07002607 struct super_block *sb = dentry->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 s64 size;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002609 ntfs_volume *vol = NTFS_SB(sb);
2610 ntfs_inode *mft_ni = NTFS_I(vol->mft_ino);
2611 pgoff_t max_index;
2612 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
2614 ntfs_debug("Entering.");
2615 /* Type of filesystem. */
2616 sfs->f_type = NTFS_SB_MAGIC;
2617 /* Optimal transfer block size. */
2618 sfs->f_bsize = PAGE_CACHE_SIZE;
2619 /*
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002620 * Total data blocks in filesystem in units of f_bsize and since
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 * inodes are also stored in data blocs ($MFT is a file) this is just
2622 * the total clusters.
2623 */
2624 sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >>
2625 PAGE_CACHE_SHIFT;
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002626 /* Free data blocks in filesystem in units of f_bsize. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 size = get_nr_free_clusters(vol) << vol->cluster_size_bits >>
2628 PAGE_CACHE_SHIFT;
2629 if (size < 0LL)
2630 size = 0LL;
2631 /* Free blocks avail to non-superuser, same as above on NTFS. */
2632 sfs->f_bavail = sfs->f_bfree = size;
2633 /* Serialize accesses to the inode bitmap. */
2634 down_read(&vol->mftbmp_lock);
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002635 read_lock_irqsave(&mft_ni->size_lock, flags);
2636 size = i_size_read(vol->mft_ino) >> vol->mft_record_size_bits;
2637 /*
2638 * Convert the maximum number of set bits into bytes rounded up, then
2639 * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
2640 * have one full and one partial page max_index = 2.
2641 */
2642 max_index = ((((mft_ni->initialized_size >> vol->mft_record_size_bits)
2643 + 7) >> 3) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2644 read_unlock_irqrestore(&mft_ni->size_lock, flags);
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002645 /* Number of inodes in filesystem (at this point in time). */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002646 sfs->f_files = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 /* Free inodes in fs (based on current total count). */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002648 sfs->f_ffree = __get_nr_free_mft_records(vol, size, max_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 up_read(&vol->mftbmp_lock);
2650 /*
2651 * File system id. This is extremely *nix flavour dependent and even
2652 * within Linux itself all fs do their own thing. I interpret this to
2653 * mean a unique id associated with the mounted fs and not the id
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002654 * associated with the filesystem driver, the latter is already given
2655 * by the filesystem type in sfs->f_type. Thus we use the 64-bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 * volume serial number splitting it into two 32-bit parts. We enter
2657 * the least significant 32-bits in f_fsid[0] and the most significant
2658 * 32-bits in f_fsid[1].
2659 */
2660 sfs->f_fsid.val[0] = vol->serial_no & 0xffffffff;
2661 sfs->f_fsid.val[1] = (vol->serial_no >> 32) & 0xffffffff;
2662 /* Maximum length of filenames. */
2663 sfs->f_namelen = NTFS_MAX_NAME_LEN;
2664 return 0;
2665}
2666
2667/**
2668 * The complete super operations.
2669 */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08002670static const struct super_operations ntfs_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 .alloc_inode = ntfs_alloc_big_inode, /* VFS: Allocate new inode. */
2672 .destroy_inode = ntfs_destroy_big_inode, /* VFS: Deallocate inode. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673#ifdef NTFS_RW
2674 //.dirty_inode = NULL, /* VFS: Called from
2675 // __mark_inode_dirty(). */
2676 .write_inode = ntfs_write_inode, /* VFS: Write dirty inode to
2677 disk. */
2678 //.drop_inode = NULL, /* VFS: Called just after the
2679 // inode reference count has
2680 // been decreased to zero.
2681 // NOTE: The inode lock is
2682 // held. See fs/inode.c::
2683 // generic_drop_inode(). */
2684 //.delete_inode = NULL, /* VFS: Delete inode from disk.
2685 // Called when i_count becomes
2686 // 0 and i_nlink is also 0. */
2687 //.write_super = NULL, /* Flush dirty super block to
2688 // disk. */
2689 //.sync_fs = NULL, /* ? */
2690 //.write_super_lockfs = NULL, /* ? */
2691 //.unlockfs = NULL, /* ? */
2692#endif /* NTFS_RW */
2693 .put_super = ntfs_put_super, /* Syscall: umount. */
2694 .statfs = ntfs_statfs, /* Syscall: statfs */
2695 .remount_fs = ntfs_remount, /* Syscall: mount -o remount. */
2696 .clear_inode = ntfs_clear_big_inode, /* VFS: Called when an inode is
2697 removed from memory. */
2698 //.umount_begin = NULL, /* Forced umount. */
2699 .show_options = ntfs_show_options, /* Show mount options in
2700 proc. */
2701};
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703/**
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002704 * ntfs_fill_super - mount an ntfs filesystem
2705 * @sb: super block of ntfs filesystem to mount
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 * @opt: string containing the mount options
2707 * @silent: silence error output
2708 *
2709 * ntfs_fill_super() is called by the VFS to mount the device described by @sb
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002710 * with the mount otions in @data with the NTFS filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 *
2712 * If @silent is true, remain silent even if errors are detected. This is used
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002713 * during bootup, when the kernel tries to mount the root filesystem with all
2714 * registered filesystems one after the other until one succeeds. This implies
2715 * that all filesystems except the correct one will quite correctly and
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 * expectedly return an error, but nobody wants to see error messages when in
2717 * fact this is what is supposed to happen.
2718 *
2719 * NOTE: @sb->s_flags contains the mount options flags.
2720 */
2721static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2722{
2723 ntfs_volume *vol;
2724 struct buffer_head *bh;
2725 struct inode *tmp_ino;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002726 int blocksize, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Ingo Molnar59345372006-07-03 00:25:18 -07002728 /*
2729 * We do a pretty difficult piece of bootstrap by reading the
2730 * MFT (and other metadata) from disk into memory. We'll only
2731 * release this metadata during umount, so the locking patterns
2732 * observed during bootstrap do not count. So turn off the
2733 * observation of locking patterns (strictly for this context
2734 * only) while mounting NTFS. [The validator is still active
2735 * otherwise, even for this context: it will for example record
2736 * lock class registrations.]
2737 */
2738 lockdep_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 ntfs_debug("Entering.");
2740#ifndef NTFS_RW
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002741 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742#endif /* ! NTFS_RW */
2743 /* Allocate a new ntfs_volume and place it in sb->s_fs_info. */
2744 sb->s_fs_info = kmalloc(sizeof(ntfs_volume), GFP_NOFS);
2745 vol = NTFS_SB(sb);
2746 if (!vol) {
2747 if (!silent)
2748 ntfs_error(sb, "Allocation of NTFS volume structure "
2749 "failed. Aborting mount...");
Ingo Molnar59345372006-07-03 00:25:18 -07002750 lockdep_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 return -ENOMEM;
2752 }
2753 /* Initialize ntfs_volume structure. */
Anton Altaparmakov442d2072005-05-27 16:42:56 +01002754 *vol = (ntfs_volume) {
2755 .sb = sb,
2756 /*
2757 * Default is group and other don't have any access to files or
2758 * directories while owner has full access. Further, files by
2759 * default are not executable but directories are of course
2760 * browseable.
2761 */
2762 .fmask = 0177,
2763 .dmask = 0077,
2764 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 init_rwsem(&vol->mftbmp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 init_rwsem(&vol->lcnbmp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
2768 unlock_kernel();
2769
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002770 /* By default, enable sparse support. */
2771 NVolSetSparseEnabled(vol);
2772
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 /* Important to get the mount options dealt with now. */
2774 if (!parse_options(vol, (char*)opt))
2775 goto err_out_now;
2776
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002777 /* We support sector sizes up to the PAGE_CACHE_SIZE. */
Martin K. Petersene1defc42009-05-22 17:17:49 -04002778 if (bdev_logical_block_size(sb->s_bdev) > PAGE_CACHE_SIZE) {
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002779 if (!silent)
2780 ntfs_error(sb, "Device has unsupported sector size "
2781 "(%i). The maximum supported sector "
2782 "size on this architecture is %lu "
2783 "bytes.",
Martin K. Petersene1defc42009-05-22 17:17:49 -04002784 bdev_logical_block_size(sb->s_bdev),
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002785 PAGE_CACHE_SIZE);
2786 goto err_out_now;
2787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 /*
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002789 * Setup the device access block size to NTFS_BLOCK_SIZE or the hard
2790 * sector size, whichever is bigger.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 */
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002792 blocksize = sb_min_blocksize(sb, NTFS_BLOCK_SIZE);
2793 if (blocksize < NTFS_BLOCK_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 if (!silent)
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002795 ntfs_error(sb, "Unable to set device block size.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 goto err_out_now;
2797 }
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002798 BUG_ON(blocksize != sb->s_blocksize);
2799 ntfs_debug("Set device block size to %i bytes (block size bits %i).",
2800 blocksize, sb->s_blocksize_bits);
2801 /* Determine the size of the device in units of block_size bytes. */
2802 if (!i_size_read(sb->s_bdev->bd_inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 if (!silent)
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002804 ntfs_error(sb, "Unable to determine device size.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 goto err_out_now;
2806 }
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002807 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002808 sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 /* Read the boot sector and return unlocked buffer head to it. */
2810 if (!(bh = read_ntfs_boot_sector(sb, silent))) {
2811 if (!silent)
2812 ntfs_error(sb, "Not an NTFS volume.");
2813 goto err_out_now;
2814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 /*
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002816 * Extract the data from the boot sector and setup the ntfs volume
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 * using it.
2818 */
2819 result = parse_ntfs_boot_sector(vol, (NTFS_BOOT_SECTOR*)bh->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 if (!result) {
2822 if (!silent)
2823 ntfs_error(sb, "Unsupported NTFS filesystem.");
2824 goto err_out_now;
2825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 /*
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002827 * If the boot sector indicates a sector size bigger than the current
2828 * device block size, switch the device block size to the sector size.
2829 * TODO: It may be possible to support this case even when the set
2830 * below fails, we would just be breaking up the i/o for each sector
2831 * into multiple blocks for i/o purposes but otherwise it should just
2832 * work. However it is safer to leave disabled until someone hits this
2833 * error message and then we can get them to try it without the setting
2834 * so we know for sure that it works.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 */
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002836 if (vol->sector_size > blocksize) {
2837 blocksize = sb_set_blocksize(sb, vol->sector_size);
2838 if (blocksize != vol->sector_size) {
2839 if (!silent)
2840 ntfs_error(sb, "Unable to set device block "
2841 "size to sector size (%i).",
2842 vol->sector_size);
2843 goto err_out_now;
2844 }
2845 BUG_ON(blocksize != sb->s_blocksize);
2846 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2847 sb->s_blocksize_bits;
2848 ntfs_debug("Changed device block size to %i bytes (block size "
2849 "bits %i) to match volume sector size.",
2850 blocksize, sb->s_blocksize_bits);
2851 }
2852 /* Initialize the cluster and mft allocators. */
2853 ntfs_setup_allocators(vol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 /* Setup remaining fields in the super block. */
2855 sb->s_magic = NTFS_SB_MAGIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 /*
2857 * Ntfs allows 63 bits for the file size, i.e. correct would be:
2858 * sb->s_maxbytes = ~0ULL >> 1;
2859 * But the kernel uses a long as the page cache page index which on
2860 * 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel
2861 * defined to the maximum the page cache page index can cope with
2862 * without overflowing the index or to 2^63 - 1, whichever is smaller.
2863 */
2864 sb->s_maxbytes = MAX_LFS_FILESIZE;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002865 /* Ntfs measures time in 100ns intervals. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 sb->s_time_gran = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 /*
2868 * Now load the metadata required for the page cache and our address
2869 * space operations to function. We do this by setting up a specialised
2870 * read_inode method and then just calling the normal iget() to obtain
2871 * the inode for $MFT which is sufficient to allow our normal inode
2872 * operations and associated address space operations to function.
2873 */
2874 sb->s_op = &ntfs_sops;
2875 tmp_ino = new_inode(sb);
2876 if (!tmp_ino) {
2877 if (!silent)
2878 ntfs_error(sb, "Failed to load essential metadata.");
2879 goto err_out_now;
2880 }
2881 tmp_ino->i_ino = FILE_MFT;
2882 insert_inode_hash(tmp_ino);
2883 if (ntfs_read_inode_mount(tmp_ino) < 0) {
2884 if (!silent)
2885 ntfs_error(sb, "Failed to load essential metadata.");
2886 goto iput_tmp_ino_err_out_now;
2887 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002888 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 /*
2890 * The current mount is a compression user if the cluster size is
2891 * less than or equal 4kiB.
2892 */
2893 if (vol->cluster_size <= 4096 && !ntfs_nr_compression_users++) {
2894 result = allocate_compression_buffers();
2895 if (result) {
2896 ntfs_error(NULL, "Failed to allocate buffers "
2897 "for compression engine.");
2898 ntfs_nr_compression_users--;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002899 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 goto iput_tmp_ino_err_out_now;
2901 }
2902 }
2903 /*
2904 * Generate the global default upcase table if necessary. Also
2905 * temporarily increment the number of upcase users to avoid race
2906 * conditions with concurrent (u)mounts.
2907 */
2908 if (!default_upcase)
2909 default_upcase = generate_default_upcase();
2910 ntfs_nr_upcase_users++;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002911 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 /*
2913 * From now on, ignore @silent parameter. If we fail below this line,
2914 * it will be due to a corrupt fs or a system error, so we report it.
2915 */
2916 /*
2917 * Open the system files with normal access functions and complete
2918 * setting up the ntfs super block.
2919 */
2920 if (!load_system_files(vol)) {
2921 ntfs_error(sb, "Failed to load system files.");
2922 goto unl_upcase_iput_tmp_ino_err_out_now;
2923 }
2924 if ((sb->s_root = d_alloc_root(vol->root_ino))) {
2925 /* We increment i_count simulating an ntfs_iget(). */
2926 atomic_inc(&vol->root_ino->i_count);
2927 ntfs_debug("Exiting, status successful.");
2928 /* Release the default upcase if it has no users. */
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002929 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 if (!--ntfs_nr_upcase_users && default_upcase) {
2931 ntfs_free(default_upcase);
2932 default_upcase = NULL;
2933 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002934 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 sb->s_export_op = &ntfs_export_ops;
2936 lock_kernel();
Ingo Molnar59345372006-07-03 00:25:18 -07002937 lockdep_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 return 0;
2939 }
2940 ntfs_error(sb, "Failed to allocate root directory.");
2941 /* Clean up after the successful load_system_files() call from above. */
2942 // TODO: Use ntfs_put_super() instead of repeating all this code...
2943 // FIXME: Should mark the volume clean as the error is most likely
2944 // -ENOMEM.
2945 iput(vol->vol_ino);
2946 vol->vol_ino = NULL;
2947 /* NTFS 3.0+ specific clean up. */
2948 if (vol->major_ver >= 3) {
2949#ifdef NTFS_RW
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002950 if (vol->usnjrnl_j_ino) {
2951 iput(vol->usnjrnl_j_ino);
2952 vol->usnjrnl_j_ino = NULL;
2953 }
2954 if (vol->usnjrnl_max_ino) {
2955 iput(vol->usnjrnl_max_ino);
2956 vol->usnjrnl_max_ino = NULL;
2957 }
2958 if (vol->usnjrnl_ino) {
2959 iput(vol->usnjrnl_ino);
2960 vol->usnjrnl_ino = NULL;
2961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 if (vol->quota_q_ino) {
2963 iput(vol->quota_q_ino);
2964 vol->quota_q_ino = NULL;
2965 }
2966 if (vol->quota_ino) {
2967 iput(vol->quota_ino);
2968 vol->quota_ino = NULL;
2969 }
2970#endif /* NTFS_RW */
2971 if (vol->extend_ino) {
2972 iput(vol->extend_ino);
2973 vol->extend_ino = NULL;
2974 }
2975 if (vol->secure_ino) {
2976 iput(vol->secure_ino);
2977 vol->secure_ino = NULL;
2978 }
2979 }
2980 iput(vol->root_ino);
2981 vol->root_ino = NULL;
2982 iput(vol->lcnbmp_ino);
2983 vol->lcnbmp_ino = NULL;
2984 iput(vol->mftbmp_ino);
2985 vol->mftbmp_ino = NULL;
2986#ifdef NTFS_RW
2987 if (vol->logfile_ino) {
2988 iput(vol->logfile_ino);
2989 vol->logfile_ino = NULL;
2990 }
2991 if (vol->mftmirr_ino) {
2992 iput(vol->mftmirr_ino);
2993 vol->mftmirr_ino = NULL;
2994 }
2995#endif /* NTFS_RW */
2996 /* Throw away the table of attribute definitions. */
2997 vol->attrdef_size = 0;
2998 if (vol->attrdef) {
2999 ntfs_free(vol->attrdef);
3000 vol->attrdef = NULL;
3001 }
3002 vol->upcase_len = 0;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00003003 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 if (vol->upcase == default_upcase) {
3005 ntfs_nr_upcase_users--;
3006 vol->upcase = NULL;
3007 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00003008 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 if (vol->upcase) {
3010 ntfs_free(vol->upcase);
3011 vol->upcase = NULL;
3012 }
3013 if (vol->nls_map) {
3014 unload_nls(vol->nls_map);
3015 vol->nls_map = NULL;
3016 }
3017 /* Error exit code path. */
3018unl_upcase_iput_tmp_ino_err_out_now:
3019 /*
3020 * Decrease the number of upcase users and destroy the global default
3021 * upcase table if necessary.
3022 */
Ingo Molnar4e5e5292006-03-23 16:57:48 +00003023 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 if (!--ntfs_nr_upcase_users && default_upcase) {
3025 ntfs_free(default_upcase);
3026 default_upcase = NULL;
3027 }
3028 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
3029 free_compression_buffers();
Ingo Molnar4e5e5292006-03-23 16:57:48 +00003030 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031iput_tmp_ino_err_out_now:
3032 iput(tmp_ino);
3033 if (vol->mft_ino && vol->mft_ino != tmp_ino)
3034 iput(vol->mft_ino);
3035 vol->mft_ino = NULL;
3036 /*
3037 * This is needed to get ntfs_clear_extent_inode() called for each
3038 * inode we have ever called ntfs_iget()/iput() on, otherwise we A)
3039 * leak resources and B) a subsequent mount fails automatically due to
3040 * ntfs_iget() never calling down into our ntfs_read_locked_inode()
3041 * method again... FIXME: Do we need to do this twice now because of
3042 * attribute inodes? I think not, so leave as is for now... (AIA)
3043 */
3044 if (invalidate_inodes(sb)) {
3045 ntfs_error(sb, "Busy inodes left. This is most likely a NTFS "
3046 "driver bug.");
3047 /* Copied from fs/super.c. I just love this message. (-; */
3048 printk("NTFS: Busy inodes after umount. Self-destruct in 5 "
3049 "seconds. Have a nice day...\n");
3050 }
3051 /* Errors at this stage are irrelevant. */
3052err_out_now:
3053 lock_kernel();
3054 sb->s_fs_info = NULL;
3055 kfree(vol);
3056 ntfs_debug("Failed, returning -EINVAL.");
Ingo Molnar59345372006-07-03 00:25:18 -07003057 lockdep_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 return -EINVAL;
3059}
3060
3061/*
3062 * This is a slab cache to optimize allocations and deallocations of Unicode
3063 * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN
3064 * (255) Unicode characters + a terminating NULL Unicode character.
3065 */
Pekka Enberg64419d92006-02-05 21:43:57 +00003066struct kmem_cache *ntfs_name_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
Anton Altaparmakov7fafb8b2005-04-06 16:09:21 +01003068/* Slab caches for efficient allocation/deallocation of inodes. */
Pekka Enberg64419d92006-02-05 21:43:57 +00003069struct kmem_cache *ntfs_inode_cache;
3070struct kmem_cache *ntfs_big_inode_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
3072/* Init once constructor for the inode slab cache. */
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003073static void ntfs_big_inode_init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
3075 ntfs_inode *ni = (ntfs_inode *)foo;
3076
Christoph Lametera35afb82007-05-16 22:10:57 -07003077 inode_init_once(VFS_I(ni));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078}
3079
3080/*
3081 * Slab caches to optimize allocations and deallocations of attribute search
3082 * contexts and index contexts, respectively.
3083 */
Pekka Enberg64419d92006-02-05 21:43:57 +00003084struct kmem_cache *ntfs_attr_ctx_cache;
3085struct kmem_cache *ntfs_index_ctx_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Anton Altaparmakove750d1c2006-03-23 17:04:12 +00003087/* Driver wide mutex. */
Ingo Molnar4e5e5292006-03-23 16:57:48 +00003088DEFINE_MUTEX(ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
David Howells454e2392006-06-23 02:02:57 -07003090static int ntfs_get_sb(struct file_system_type *fs_type,
3091 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092{
David Howells454e2392006-06-23 02:02:57 -07003093 return get_sb_bdev(fs_type, flags, dev_name, data, ntfs_fill_super,
3094 mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095}
3096
3097static struct file_system_type ntfs_fs_type = {
3098 .owner = THIS_MODULE,
3099 .name = "ntfs",
3100 .get_sb = ntfs_get_sb,
3101 .kill_sb = kill_block_super,
3102 .fs_flags = FS_REQUIRES_DEV,
3103};
3104
3105/* Stable names for the slab caches. */
3106static const char ntfs_index_ctx_cache_name[] = "ntfs_index_ctx_cache";
3107static const char ntfs_attr_ctx_cache_name[] = "ntfs_attr_ctx_cache";
3108static const char ntfs_name_cache_name[] = "ntfs_name_cache";
3109static const char ntfs_inode_cache_name[] = "ntfs_inode_cache";
3110static const char ntfs_big_inode_cache_name[] = "ntfs_big_inode_cache";
3111
3112static int __init init_ntfs_fs(void)
3113{
3114 int err = 0;
3115
3116 /* This may be ugly but it results in pretty output so who cares. (-8 */
3117 printk(KERN_INFO "NTFS driver " NTFS_VERSION " [Flags: R/"
3118#ifdef NTFS_RW
3119 "W"
3120#else
3121 "O"
3122#endif
3123#ifdef DEBUG
3124 " DEBUG"
3125#endif
3126#ifdef MODULE
3127 " MODULE"
3128#endif
3129 "].\n");
3130
3131 ntfs_debug("Debug messages are enabled.");
3132
3133 ntfs_index_ctx_cache = kmem_cache_create(ntfs_index_ctx_cache_name,
3134 sizeof(ntfs_index_context), 0 /* offset */,
Paul Mundt20c2df82007-07-20 10:11:58 +09003135 SLAB_HWCACHE_ALIGN, NULL /* ctor */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 if (!ntfs_index_ctx_cache) {
3137 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3138 ntfs_index_ctx_cache_name);
3139 goto ictx_err_out;
3140 }
3141 ntfs_attr_ctx_cache = kmem_cache_create(ntfs_attr_ctx_cache_name,
3142 sizeof(ntfs_attr_search_ctx), 0 /* offset */,
Paul Mundt20c2df82007-07-20 10:11:58 +09003143 SLAB_HWCACHE_ALIGN, NULL /* ctor */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 if (!ntfs_attr_ctx_cache) {
3145 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3146 ntfs_attr_ctx_cache_name);
3147 goto actx_err_out;
3148 }
3149
3150 ntfs_name_cache = kmem_cache_create(ntfs_name_cache_name,
3151 (NTFS_MAX_NAME_LEN+1) * sizeof(ntfschar), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09003152 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 if (!ntfs_name_cache) {
3154 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3155 ntfs_name_cache_name);
3156 goto name_err_out;
3157 }
3158
3159 ntfs_inode_cache = kmem_cache_create(ntfs_inode_cache_name,
3160 sizeof(ntfs_inode), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09003161 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 if (!ntfs_inode_cache) {
3163 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3164 ntfs_inode_cache_name);
3165 goto inode_err_out;
3166 }
3167
3168 ntfs_big_inode_cache = kmem_cache_create(ntfs_big_inode_cache_name,
3169 sizeof(big_ntfs_inode), 0,
Paul Jackson4b6a9312006-03-24 03:16:05 -08003170 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD,
Paul Mundt20c2df82007-07-20 10:11:58 +09003171 ntfs_big_inode_init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 if (!ntfs_big_inode_cache) {
3173 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3174 ntfs_big_inode_cache_name);
3175 goto big_inode_err_out;
3176 }
3177
3178 /* Register the ntfs sysctls. */
3179 err = ntfs_sysctl(1);
3180 if (err) {
3181 printk(KERN_CRIT "NTFS: Failed to register NTFS sysctls!\n");
3182 goto sysctl_err_out;
3183 }
3184
3185 err = register_filesystem(&ntfs_fs_type);
3186 if (!err) {
3187 ntfs_debug("NTFS driver registered successfully.");
3188 return 0; /* Success! */
3189 }
Anton Altaparmakovc002f422005-02-03 12:02:56 +00003190 printk(KERN_CRIT "NTFS: Failed to register NTFS filesystem driver!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
3192sysctl_err_out:
3193 kmem_cache_destroy(ntfs_big_inode_cache);
3194big_inode_err_out:
3195 kmem_cache_destroy(ntfs_inode_cache);
3196inode_err_out:
3197 kmem_cache_destroy(ntfs_name_cache);
3198name_err_out:
3199 kmem_cache_destroy(ntfs_attr_ctx_cache);
3200actx_err_out:
3201 kmem_cache_destroy(ntfs_index_ctx_cache);
3202ictx_err_out:
3203 if (!err) {
Anton Altaparmakovc002f422005-02-03 12:02:56 +00003204 printk(KERN_CRIT "NTFS: Aborting NTFS filesystem driver "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 "registration...\n");
3206 err = -ENOMEM;
3207 }
3208 return err;
3209}
3210
3211static void __exit exit_ntfs_fs(void)
3212{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 ntfs_debug("Unregistering NTFS driver.");
3214
3215 unregister_filesystem(&ntfs_fs_type);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003216 kmem_cache_destroy(ntfs_big_inode_cache);
3217 kmem_cache_destroy(ntfs_inode_cache);
3218 kmem_cache_destroy(ntfs_name_cache);
3219 kmem_cache_destroy(ntfs_attr_ctx_cache);
3220 kmem_cache_destroy(ntfs_index_ctx_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 /* Unregister the ntfs sysctls. */
3222 ntfs_sysctl(0);
3223}
3224
3225MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>");
Anton Altaparmakov83311912007-01-18 09:42:48 +00003226MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2007 Anton Altaparmakov");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227MODULE_VERSION(NTFS_VERSION);
3228MODULE_LICENSE("GPL");
3229#ifdef DEBUG
3230module_param(debug_msgs, bool, 0);
3231MODULE_PARM_DESC(debug_msgs, "Enable debug messages.");
3232#endif
3233
3234module_init(init_ntfs_fs)
3235module_exit(exit_ntfs_fs)