blob: 512806171bfa2e2dfb73975feb60ea9560a654a7 [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>
Akinobu Mitac4af9642010-03-15 00:35:00 -040034#include <linux/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include "sysctl.h"
37#include "logfile.h"
38#include "quota.h"
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +010039#include "usnjrnl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "dir.h"
41#include "debug.h"
42#include "index.h"
Christoph Hellwiga9185b42010-03-05 09:21:37 +010043#include "inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "aops.h"
Anton Altaparmakovb0d23742005-04-04 16:20:14 +010045#include "layout.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "malloc.h"
47#include "ntfs.h"
48
Anton Altaparmakovc002f422005-02-03 12:02:56 +000049/* Number of mounted filesystems which have compression enabled. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static unsigned long ntfs_nr_compression_users;
51
52/* A global default upcase table and a corresponding reference count. */
53static ntfschar *default_upcase = NULL;
54static unsigned long ntfs_nr_upcase_users = 0;
55
56/* Error constants/strings used in inode.c::ntfs_show_options(). */
57typedef enum {
58 /* One of these must be present, default is ON_ERRORS_CONTINUE. */
59 ON_ERRORS_PANIC = 0x01,
60 ON_ERRORS_REMOUNT_RO = 0x02,
61 ON_ERRORS_CONTINUE = 0x04,
62 /* Optional, can be combined with any of the above. */
63 ON_ERRORS_RECOVER = 0x10,
64} ON_ERRORS_ACTIONS;
65
66const option_t on_errors_arr[] = {
67 { ON_ERRORS_PANIC, "panic" },
68 { ON_ERRORS_REMOUNT_RO, "remount-ro", },
69 { ON_ERRORS_CONTINUE, "continue", },
70 { ON_ERRORS_RECOVER, "recover" },
71 { 0, NULL }
72};
73
74/**
75 * simple_getbool -
76 *
77 * Copied from old ntfs driver (which copied from vfat driver).
78 */
Richard Knutssonc49c3112006-09-30 23:27:12 -070079static int simple_getbool(char *s, bool *setval)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 if (s) {
82 if (!strcmp(s, "1") || !strcmp(s, "yes") || !strcmp(s, "true"))
Richard Knutssonc49c3112006-09-30 23:27:12 -070083 *setval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 else if (!strcmp(s, "0") || !strcmp(s, "no") ||
85 !strcmp(s, "false"))
Richard Knutssonc49c3112006-09-30 23:27:12 -070086 *setval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 else
88 return 0;
89 } else
Richard Knutssonc49c3112006-09-30 23:27:12 -070090 *setval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return 1;
92}
93
94/**
95 * parse_options - parse the (re)mount options
96 * @vol: ntfs volume
97 * @opt: string containing the (re)mount options
98 *
99 * Parse the recognized options in @opt for the ntfs volume described by @vol.
100 */
Richard Knutssonc49c3112006-09-30 23:27:12 -0700101static bool parse_options(ntfs_volume *vol, char *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 char *p, *v, *ov;
104 static char *utf8 = "utf8";
105 int errors = 0, sloppy = 0;
106 uid_t uid = (uid_t)-1;
107 gid_t gid = (gid_t)-1;
108 mode_t fmask = (mode_t)-1, dmask = (mode_t)-1;
109 int mft_zone_multiplier = -1, on_errors = -1;
Anton Altaparmakovc002f422005-02-03 12:02:56 +0000110 int show_sys_files = -1, case_sensitive = -1, disable_sparse = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 struct nls_table *nls_map = NULL, *old_nls;
112
113 /* I am lazy... (-8 */
114#define NTFS_GETOPT_WITH_DEFAULT(option, variable, default_value) \
115 if (!strcmp(p, option)) { \
116 if (!v || !*v) \
117 variable = default_value; \
118 else { \
119 variable = simple_strtoul(ov = v, &v, 0); \
120 if (*v) \
121 goto needs_val; \
122 } \
123 }
124#define NTFS_GETOPT(option, variable) \
125 if (!strcmp(p, option)) { \
126 if (!v || !*v) \
127 goto needs_arg; \
128 variable = simple_strtoul(ov = v, &v, 0); \
129 if (*v) \
130 goto needs_val; \
131 }
Anton Altaparmakov5d467702005-09-12 14:33:47 +0100132#define NTFS_GETOPT_OCTAL(option, variable) \
133 if (!strcmp(p, option)) { \
134 if (!v || !*v) \
135 goto needs_arg; \
136 variable = simple_strtoul(ov = v, &v, 8); \
137 if (*v) \
138 goto needs_val; \
139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#define NTFS_GETOPT_BOOL(option, variable) \
141 if (!strcmp(p, option)) { \
Richard Knutssonc49c3112006-09-30 23:27:12 -0700142 bool val; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (!simple_getbool(v, &val)) \
144 goto needs_bool; \
145 variable = val; \
146 }
147#define NTFS_GETOPT_OPTIONS_ARRAY(option, variable, opt_array) \
148 if (!strcmp(p, option)) { \
149 int _i; \
150 if (!v || !*v) \
151 goto needs_arg; \
152 ov = v; \
153 if (variable == -1) \
154 variable = 0; \
155 for (_i = 0; opt_array[_i].str && *opt_array[_i].str; _i++) \
156 if (!strcmp(opt_array[_i].str, v)) { \
157 variable |= opt_array[_i].val; \
158 break; \
159 } \
160 if (!opt_array[_i].str || !*opt_array[_i].str) \
161 goto needs_val; \
162 }
163 if (!opt || !*opt)
164 goto no_mount_options;
165 ntfs_debug("Entering with mount options string: %s", opt);
166 while ((p = strsep(&opt, ","))) {
167 if ((v = strchr(p, '=')))
168 *v++ = 0;
169 NTFS_GETOPT("uid", uid)
170 else NTFS_GETOPT("gid", gid)
Anton Altaparmakov5d467702005-09-12 14:33:47 +0100171 else NTFS_GETOPT_OCTAL("umask", fmask = dmask)
172 else NTFS_GETOPT_OCTAL("fmask", fmask)
173 else NTFS_GETOPT_OCTAL("dmask", dmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 else NTFS_GETOPT("mft_zone_multiplier", mft_zone_multiplier)
Richard Knutssonc49c3112006-09-30 23:27:12 -0700175 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, true)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files)
177 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive)
Anton Altaparmakovc002f422005-02-03 12:02:56 +0000178 else NTFS_GETOPT_BOOL("disable_sparse", disable_sparse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors,
180 on_errors_arr)
181 else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes"))
182 ntfs_warning(vol->sb, "Ignoring obsolete option %s.",
183 p);
184 else if (!strcmp(p, "nls") || !strcmp(p, "iocharset")) {
185 if (!strcmp(p, "iocharset"))
186 ntfs_warning(vol->sb, "Option iocharset is "
187 "deprecated. Please use "
188 "option nls=<charsetname> in "
189 "the future.");
190 if (!v || !*v)
191 goto needs_arg;
192use_utf8:
193 old_nls = nls_map;
194 nls_map = load_nls(v);
195 if (!nls_map) {
196 if (!old_nls) {
197 ntfs_error(vol->sb, "NLS character set "
198 "%s not found.", v);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700199 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201 ntfs_error(vol->sb, "NLS character set %s not "
202 "found. Using previous one %s.",
203 v, old_nls->charset);
204 nls_map = old_nls;
205 } else /* nls_map */ {
Thomas Gleixner6d729e42009-08-16 21:05:08 +0000206 unload_nls(old_nls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208 } else if (!strcmp(p, "utf8")) {
Richard Knutssonc49c3112006-09-30 23:27:12 -0700209 bool val = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 ntfs_warning(vol->sb, "Option utf8 is no longer "
211 "supported, using option nls=utf8. Please "
212 "use option nls=utf8 in the future and "
213 "make sure utf8 is compiled either as a "
214 "module or into the kernel.");
215 if (!v || !*v)
Richard Knutssonc49c3112006-09-30 23:27:12 -0700216 val = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 else if (!simple_getbool(v, &val))
218 goto needs_bool;
219 if (val) {
220 v = utf8;
221 goto use_utf8;
222 }
223 } else {
224 ntfs_error(vol->sb, "Unrecognized mount option %s.", p);
225 if (errors < INT_MAX)
226 errors++;
227 }
228#undef NTFS_GETOPT_OPTIONS_ARRAY
229#undef NTFS_GETOPT_BOOL
230#undef NTFS_GETOPT
231#undef NTFS_GETOPT_WITH_DEFAULT
232 }
233no_mount_options:
234 if (errors && !sloppy)
Richard Knutssonc49c3112006-09-30 23:27:12 -0700235 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (sloppy)
237 ntfs_warning(vol->sb, "Sloppy option given. Ignoring "
238 "unrecognized mount option(s) and continuing.");
239 /* Keep this first! */
240 if (on_errors != -1) {
241 if (!on_errors) {
242 ntfs_error(vol->sb, "Invalid errors option argument "
243 "or bug in options parser.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700244 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246 }
247 if (nls_map) {
248 if (vol->nls_map && vol->nls_map != nls_map) {
249 ntfs_error(vol->sb, "Cannot change NLS character set "
250 "on remount.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700251 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 } /* else (!vol->nls_map) */
253 ntfs_debug("Using NLS character set %s.", nls_map->charset);
254 vol->nls_map = nls_map;
255 } else /* (!nls_map) */ {
256 if (!vol->nls_map) {
257 vol->nls_map = load_nls_default();
258 if (!vol->nls_map) {
259 ntfs_error(vol->sb, "Failed to load default "
260 "NLS character set.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700261 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263 ntfs_debug("Using default NLS character set (%s).",
264 vol->nls_map->charset);
265 }
266 }
267 if (mft_zone_multiplier != -1) {
268 if (vol->mft_zone_multiplier && vol->mft_zone_multiplier !=
269 mft_zone_multiplier) {
270 ntfs_error(vol->sb, "Cannot change mft_zone_multiplier "
271 "on remount.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700272 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274 if (mft_zone_multiplier < 1 || mft_zone_multiplier > 4) {
275 ntfs_error(vol->sb, "Invalid mft_zone_multiplier. "
276 "Using default value, i.e. 1.");
277 mft_zone_multiplier = 1;
278 }
279 vol->mft_zone_multiplier = mft_zone_multiplier;
280 }
281 if (!vol->mft_zone_multiplier)
282 vol->mft_zone_multiplier = 1;
283 if (on_errors != -1)
284 vol->on_errors = on_errors;
285 if (!vol->on_errors || vol->on_errors == ON_ERRORS_RECOVER)
286 vol->on_errors |= ON_ERRORS_CONTINUE;
287 if (uid != (uid_t)-1)
288 vol->uid = uid;
289 if (gid != (gid_t)-1)
290 vol->gid = gid;
291 if (fmask != (mode_t)-1)
292 vol->fmask = fmask;
293 if (dmask != (mode_t)-1)
294 vol->dmask = dmask;
295 if (show_sys_files != -1) {
296 if (show_sys_files)
297 NVolSetShowSystemFiles(vol);
298 else
299 NVolClearShowSystemFiles(vol);
300 }
301 if (case_sensitive != -1) {
302 if (case_sensitive)
303 NVolSetCaseSensitive(vol);
304 else
305 NVolClearCaseSensitive(vol);
306 }
Anton Altaparmakovc002f422005-02-03 12:02:56 +0000307 if (disable_sparse != -1) {
308 if (disable_sparse)
309 NVolClearSparseEnabled(vol);
310 else {
311 if (!NVolSparseEnabled(vol) &&
312 vol->major_ver && vol->major_ver < 3)
313 ntfs_warning(vol->sb, "Not enabling sparse "
314 "support due to NTFS volume "
315 "version %i.%i (need at least "
316 "version 3.0).", vol->major_ver,
317 vol->minor_ver);
318 else
319 NVolSetSparseEnabled(vol);
320 }
321 }
Richard Knutssonc49c3112006-09-30 23:27:12 -0700322 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323needs_arg:
324 ntfs_error(vol->sb, "The %s option requires an argument.", p);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700325 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326needs_bool:
327 ntfs_error(vol->sb, "The %s option requires a boolean argument.", p);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700328 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329needs_val:
330 ntfs_error(vol->sb, "Invalid %s option argument: %s", p, ov);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700331 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
334#ifdef NTFS_RW
335
336/**
337 * ntfs_write_volume_flags - write new flags to the volume information flags
338 * @vol: ntfs volume on which to modify the flags
339 * @flags: new flags value for the volume information flags
340 *
341 * Internal function. You probably want to use ntfs_{set,clear}_volume_flags()
342 * instead (see below).
343 *
344 * Replace the volume information flags on the volume @vol with the value
345 * supplied in @flags. Note, this overwrites the volume information flags, so
346 * make sure to combine the flags you want to modify with the old flags and use
347 * the result when calling ntfs_write_volume_flags().
348 *
349 * Return 0 on success and -errno on error.
350 */
351static int ntfs_write_volume_flags(ntfs_volume *vol, const VOLUME_FLAGS flags)
352{
353 ntfs_inode *ni = NTFS_I(vol->vol_ino);
354 MFT_RECORD *m;
355 VOLUME_INFORMATION *vi;
356 ntfs_attr_search_ctx *ctx;
357 int err;
358
359 ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
360 le16_to_cpu(vol->vol_flags), le16_to_cpu(flags));
361 if (vol->vol_flags == flags)
362 goto done;
363 BUG_ON(!ni);
364 m = map_mft_record(ni);
365 if (IS_ERR(m)) {
366 err = PTR_ERR(m);
367 goto err_out;
368 }
369 ctx = ntfs_attr_get_search_ctx(ni, m);
370 if (!ctx) {
371 err = -ENOMEM;
372 goto put_unm_err_out;
373 }
374 err = ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
375 ctx);
376 if (err)
377 goto put_unm_err_out;
378 vi = (VOLUME_INFORMATION*)((u8*)ctx->attr +
379 le16_to_cpu(ctx->attr->data.resident.value_offset));
380 vol->vol_flags = vi->flags = flags;
381 flush_dcache_mft_record_page(ctx->ntfs_ino);
382 mark_mft_record_dirty(ctx->ntfs_ino);
383 ntfs_attr_put_search_ctx(ctx);
384 unmap_mft_record(ni);
385done:
386 ntfs_debug("Done.");
387 return 0;
388put_unm_err_out:
389 if (ctx)
390 ntfs_attr_put_search_ctx(ctx);
391 unmap_mft_record(ni);
392err_out:
393 ntfs_error(vol->sb, "Failed with error code %i.", -err);
394 return err;
395}
396
397/**
398 * ntfs_set_volume_flags - set bits in the volume information flags
399 * @vol: ntfs volume on which to modify the flags
400 * @flags: flags to set on the volume
401 *
402 * Set the bits in @flags in the volume information flags on the volume @vol.
403 *
404 * Return 0 on success and -errno on error.
405 */
406static inline int ntfs_set_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
407{
408 flags &= VOLUME_FLAGS_MASK;
409 return ntfs_write_volume_flags(vol, vol->vol_flags | flags);
410}
411
412/**
413 * ntfs_clear_volume_flags - clear bits in the volume information flags
414 * @vol: ntfs volume on which to modify the flags
415 * @flags: flags to clear on the volume
416 *
417 * Clear the bits in @flags in the volume information flags on the volume @vol.
418 *
419 * Return 0 on success and -errno on error.
420 */
421static inline int ntfs_clear_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
422{
423 flags &= VOLUME_FLAGS_MASK;
424 flags = vol->vol_flags & cpu_to_le16(~le16_to_cpu(flags));
425 return ntfs_write_volume_flags(vol, flags);
426}
427
428#endif /* NTFS_RW */
429
430/**
431 * ntfs_remount - change the mount options of a mounted ntfs filesystem
432 * @sb: superblock of mounted ntfs filesystem
433 * @flags: remount flags
434 * @opt: remount options string
435 *
436 * Change the mount options of an already mounted ntfs filesystem.
437 *
438 * NOTE: The VFS sets the @sb->s_flags remount flags to @flags after
439 * ntfs_remount() returns successfully (i.e. returns 0). Otherwise,
440 * @sb->s_flags are not changed.
441 */
442static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
443{
444 ntfs_volume *vol = NTFS_SB(sb);
445
446 ntfs_debug("Entering with remount options string: %s", opt);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200447
448 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449#ifndef NTFS_RW
Christoph Hellwig24a44dc2006-01-09 20:52:14 -0800450 /* For read-only compiled driver, enforce read-only flag. */
451 *flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452#else /* NTFS_RW */
453 /*
454 * For the read-write compiled driver, if we are remounting read-write,
455 * make sure there are no volume errors and that no unsupported volume
456 * flags are set. Also, empty the logfile journal as it would become
457 * stale as soon as something is written to the volume and mark the
458 * volume dirty so that chkdsk is run if the volume is not umounted
459 * cleanly. Finally, mark the quotas out of date so Windows rescans
460 * the volume on boot and updates them.
461 *
462 * When remounting read-only, mark the volume clean if no volume errors
463 * have occured.
464 */
465 if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
466 static const char *es = ". Cannot remount read-write.";
467
468 /* Remounting read-write. */
469 if (NVolErrors(vol)) {
470 ntfs_error(sb, "Volume has errors and is read-only%s",
471 es);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200472 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return -EROFS;
474 }
475 if (vol->vol_flags & VOLUME_IS_DIRTY) {
476 ntfs_error(sb, "Volume is dirty and read-only%s", es);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200477 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return -EROFS;
479 }
Anton Altaparmakov1cf3109f2006-02-24 10:48:14 +0000480 if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
481 ntfs_error(sb, "Volume has been modified by chkdsk "
482 "and is read-only%s", es);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200483 unlock_kernel();
Anton Altaparmakov1cf3109f2006-02-24 10:48:14 +0000484 return -EROFS;
485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
Anton Altaparmakov1cf3109f2006-02-24 10:48:14 +0000487 ntfs_error(sb, "Volume has unsupported flags set "
488 "(0x%x) and is read-only%s",
489 (unsigned)le16_to_cpu(vol->vol_flags),
490 es);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200491 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -EROFS;
493 }
494 if (ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
495 ntfs_error(sb, "Failed to set dirty bit in volume "
496 "information flags%s", es);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200497 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return -EROFS;
499 }
500#if 0
501 // TODO: Enable this code once we start modifying anything that
502 // is different between NTFS 1.2 and 3.x...
503 /* Set NT4 compatibility flag on newer NTFS version volumes. */
504 if ((vol->major_ver > 1)) {
505 if (ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
506 ntfs_error(sb, "Failed to set NT4 "
507 "compatibility flag%s", es);
508 NVolSetErrors(vol);
509 return -EROFS;
510 }
511 }
512#endif
513 if (!ntfs_empty_logfile(vol->logfile_ino)) {
514 ntfs_error(sb, "Failed to empty journal $LogFile%s",
515 es);
516 NVolSetErrors(vol);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200517 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return -EROFS;
519 }
520 if (!ntfs_mark_quotas_out_of_date(vol)) {
521 ntfs_error(sb, "Failed to mark quotas out of date%s",
522 es);
523 NVolSetErrors(vol);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200524 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return -EROFS;
526 }
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +0100527 if (!ntfs_stamp_usnjrnl(vol)) {
528 ntfs_error(sb, "Failed to stamp transation log "
529 "($UsnJrnl)%s", es);
530 NVolSetErrors(vol);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200531 unlock_kernel();
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +0100532 return -EROFS;
533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) {
535 /* Remounting read-only. */
536 if (!NVolErrors(vol)) {
537 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
538 ntfs_warning(sb, "Failed to clear dirty bit "
539 "in volume information "
540 "flags. Run chkdsk.");
541 }
542 }
543#endif /* NTFS_RW */
544
545 // TODO: Deal with *flags.
546
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200547 if (!parse_options(vol, opt)) {
548 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return -EINVAL;
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200550 }
551 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 ntfs_debug("Done.");
553 return 0;
554}
555
556/**
557 * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector
558 * @sb: Super block of the device to which @b belongs.
559 * @b: Boot sector of device @sb to check.
Richard Knutssonc49c3112006-09-30 23:27:12 -0700560 * @silent: If 'true', all output will be silenced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 *
562 * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot
Richard Knutssonc49c3112006-09-30 23:27:12 -0700563 * sector. Returns 'true' if it is valid and 'false' if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 *
565 * @sb is only needed for warning/error output, i.e. it can be NULL when silent
Richard Knutssonc49c3112006-09-30 23:27:12 -0700566 * is 'true'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 */
Richard Knutssonc49c3112006-09-30 23:27:12 -0700568static bool is_boot_sector_ntfs(const struct super_block *sb,
569 const NTFS_BOOT_SECTOR *b, const bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 /*
572 * Check that checksum == sum of u32 values from b to the checksum
Anton Altaparmakovb0d23742005-04-04 16:20:14 +0100573 * field. If checksum is zero, no checking is done. We will work when
574 * the checksum test fails, since some utilities update the boot sector
575 * ignoring the checksum which leaves the checksum out-of-date. We
576 * report a warning if this is the case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
Anton Altaparmakovb0d23742005-04-04 16:20:14 +0100578 if ((void*)b < (void*)&b->checksum && b->checksum && !silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 le32 *u;
580 u32 i;
581
582 for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u)
583 i += le32_to_cpup(u);
584 if (le32_to_cpu(b->checksum) != i)
Anton Altaparmakovb0d23742005-04-04 16:20:14 +0100585 ntfs_warning(sb, "Invalid boot sector checksum.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587 /* Check OEMidentifier is "NTFS " */
588 if (b->oem_id != magicNTFS)
589 goto not_ntfs;
590 /* Check bytes per sector value is between 256 and 4096. */
591 if (le16_to_cpu(b->bpb.bytes_per_sector) < 0x100 ||
592 le16_to_cpu(b->bpb.bytes_per_sector) > 0x1000)
593 goto not_ntfs;
594 /* Check sectors per cluster value is valid. */
595 switch (b->bpb.sectors_per_cluster) {
596 case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128:
597 break;
598 default:
599 goto not_ntfs;
600 }
Anton Altaparmakov7fafb8b2005-04-06 16:09:21 +0100601 /* Check the cluster size is not above the maximum (64kiB). */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) *
Anton Altaparmakov7fafb8b2005-04-06 16:09:21 +0100603 b->bpb.sectors_per_cluster > NTFS_MAX_CLUSTER_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 goto not_ntfs;
605 /* Check reserved/unused fields are really zero. */
606 if (le16_to_cpu(b->bpb.reserved_sectors) ||
607 le16_to_cpu(b->bpb.root_entries) ||
608 le16_to_cpu(b->bpb.sectors) ||
609 le16_to_cpu(b->bpb.sectors_per_fat) ||
610 le32_to_cpu(b->bpb.large_sectors) || b->bpb.fats)
611 goto not_ntfs;
612 /* Check clusters per file mft record value is valid. */
613 if ((u8)b->clusters_per_mft_record < 0xe1 ||
614 (u8)b->clusters_per_mft_record > 0xf7)
615 switch (b->clusters_per_mft_record) {
616 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
617 break;
618 default:
619 goto not_ntfs;
620 }
621 /* Check clusters per index block value is valid. */
622 if ((u8)b->clusters_per_index_record < 0xe1 ||
623 (u8)b->clusters_per_index_record > 0xf7)
624 switch (b->clusters_per_index_record) {
625 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
626 break;
627 default:
628 goto not_ntfs;
629 }
630 /*
631 * Check for valid end of sector marker. We will work without it, but
632 * many BIOSes will refuse to boot from a bootsector if the magic is
633 * incorrect, so we emit a warning.
634 */
Harvey Harrison63cd8852009-03-31 15:23:52 -0700635 if (!silent && b->end_of_sector_marker != cpu_to_le16(0xaa55))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 ntfs_warning(sb, "Invalid end of sector marker.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700637 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638not_ntfs:
Richard Knutssonc49c3112006-09-30 23:27:12 -0700639 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642/**
643 * read_ntfs_boot_sector - read the NTFS boot sector of a device
644 * @sb: super block of device to read the boot sector from
645 * @silent: if true, suppress all output
646 *
647 * Reads the boot sector from the device and validates it. If that fails, tries
648 * to read the backup boot sector, first from the end of the device a-la NT4 and
649 * later and then from the middle of the device a-la NT3.51 and before.
650 *
651 * If a valid boot sector is found but it is not the primary boot sector, we
652 * repair the primary boot sector silently (unless the device is read-only or
653 * the primary boot sector is not accessible).
654 *
655 * NOTE: To call this function, @sb must have the fields s_dev, the ntfs super
656 * block (u.ntfs_sb), nr_blocks and the device flags (s_flags) initialized
657 * to their respective values.
658 *
659 * Return the unlocked buffer head containing the boot sector or NULL on error.
660 */
661static struct buffer_head *read_ntfs_boot_sector(struct super_block *sb,
662 const int silent)
663{
664 const char *read_err_str = "Unable to read %s boot sector.";
665 struct buffer_head *bh_primary, *bh_backup;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000666 sector_t nr_blocks = NTFS_SB(sb)->nr_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 /* Try to read primary boot sector. */
669 if ((bh_primary = sb_bread(sb, 0))) {
670 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
671 bh_primary->b_data, silent))
672 return bh_primary;
673 if (!silent)
674 ntfs_error(sb, "Primary boot sector is invalid.");
675 } else if (!silent)
676 ntfs_error(sb, read_err_str, "primary");
677 if (!(NTFS_SB(sb)->on_errors & ON_ERRORS_RECOVER)) {
678 if (bh_primary)
679 brelse(bh_primary);
680 if (!silent)
681 ntfs_error(sb, "Mount option errors=recover not used. "
682 "Aborting without trying to recover.");
683 return NULL;
684 }
685 /* Try to read NT4+ backup boot sector. */
686 if ((bh_backup = sb_bread(sb, nr_blocks - 1))) {
687 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
688 bh_backup->b_data, silent))
689 goto hotfix_primary_boot_sector;
690 brelse(bh_backup);
691 } else if (!silent)
692 ntfs_error(sb, read_err_str, "backup");
693 /* Try to read NT3.51- backup boot sector. */
694 if ((bh_backup = sb_bread(sb, nr_blocks >> 1))) {
695 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
696 bh_backup->b_data, silent))
697 goto hotfix_primary_boot_sector;
698 if (!silent)
699 ntfs_error(sb, "Could not find a valid backup boot "
700 "sector.");
701 brelse(bh_backup);
702 } else if (!silent)
703 ntfs_error(sb, read_err_str, "backup");
704 /* We failed. Cleanup and return. */
705 if (bh_primary)
706 brelse(bh_primary);
707 return NULL;
708hotfix_primary_boot_sector:
709 if (bh_primary) {
710 /*
711 * If we managed to read sector zero and the volume is not
712 * read-only, copy the found, valid backup boot sector to the
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000713 * primary boot sector. Note we only copy the actual boot
714 * sector structure, not the actual whole device sector as that
715 * may be bigger and would potentially damage the $Boot system
716 * file (FIXME: Would be nice to know if the backup boot sector
717 * on a large sector device contains the whole boot loader or
718 * just the first 512 bytes).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 */
720 if (!(sb->s_flags & MS_RDONLY)) {
721 ntfs_warning(sb, "Hot-fix: Recovering invalid primary "
722 "boot sector from backup copy.");
723 memcpy(bh_primary->b_data, bh_backup->b_data,
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000724 NTFS_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 mark_buffer_dirty(bh_primary);
726 sync_dirty_buffer(bh_primary);
727 if (buffer_uptodate(bh_primary)) {
728 brelse(bh_backup);
729 return bh_primary;
730 }
731 ntfs_error(sb, "Hot-fix: Device write error while "
732 "recovering primary boot sector.");
733 } else {
734 ntfs_warning(sb, "Hot-fix: Recovery of primary boot "
735 "sector failed: Read-only mount.");
736 }
737 brelse(bh_primary);
738 }
739 ntfs_warning(sb, "Using backup boot sector.");
740 return bh_backup;
741}
742
743/**
744 * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol
745 * @vol: volume structure to initialise with data from boot sector
746 * @b: boot sector to parse
747 *
748 * Parse the ntfs boot sector @b and store all imporant information therein in
Richard Knutssonc49c3112006-09-30 23:27:12 -0700749 * the ntfs super block @vol. Return 'true' on success and 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 */
Richard Knutssonc49c3112006-09-30 23:27:12 -0700751static bool parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 unsigned int sectors_per_cluster_bits, nr_hidden_sects;
754 int clusters_per_mft_record, clusters_per_index_record;
755 s64 ll;
756
757 vol->sector_size = le16_to_cpu(b->bpb.bytes_per_sector);
758 vol->sector_size_bits = ffs(vol->sector_size) - 1;
759 ntfs_debug("vol->sector_size = %i (0x%x)", vol->sector_size,
760 vol->sector_size);
761 ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits,
762 vol->sector_size_bits);
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000763 if (vol->sector_size < vol->sb->s_blocksize) {
764 ntfs_error(vol->sb, "Sector size (%i) is smaller than the "
765 "device block size (%lu). This is not "
766 "supported. Sorry.", vol->sector_size,
767 vol->sb->s_blocksize);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700768 return false;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster);
771 sectors_per_cluster_bits = ffs(b->bpb.sectors_per_cluster) - 1;
772 ntfs_debug("sectors_per_cluster_bits = 0x%x",
773 sectors_per_cluster_bits);
774 nr_hidden_sects = le32_to_cpu(b->bpb.hidden_sectors);
775 ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects);
776 vol->cluster_size = vol->sector_size << sectors_per_cluster_bits;
777 vol->cluster_size_mask = vol->cluster_size - 1;
778 vol->cluster_size_bits = ffs(vol->cluster_size) - 1;
779 ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size,
780 vol->cluster_size);
781 ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask);
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000782 ntfs_debug("vol->cluster_size_bits = %i", vol->cluster_size_bits);
783 if (vol->cluster_size < vol->sector_size) {
784 ntfs_error(vol->sb, "Cluster size (%i) is smaller than the "
785 "sector size (%i). This is not supported. "
786 "Sorry.", vol->cluster_size, vol->sector_size);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700787 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789 clusters_per_mft_record = b->clusters_per_mft_record;
790 ntfs_debug("clusters_per_mft_record = %i (0x%x)",
791 clusters_per_mft_record, clusters_per_mft_record);
792 if (clusters_per_mft_record > 0)
793 vol->mft_record_size = vol->cluster_size <<
794 (ffs(clusters_per_mft_record) - 1);
795 else
796 /*
797 * When mft_record_size < cluster_size, clusters_per_mft_record
798 * = -log2(mft_record_size) bytes. mft_record_size normaly is
799 * 1024 bytes, which is encoded as 0xF6 (-10 in decimal).
800 */
801 vol->mft_record_size = 1 << -clusters_per_mft_record;
802 vol->mft_record_size_mask = vol->mft_record_size - 1;
803 vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1;
804 ntfs_debug("vol->mft_record_size = %i (0x%x)", vol->mft_record_size,
805 vol->mft_record_size);
806 ntfs_debug("vol->mft_record_size_mask = 0x%x",
807 vol->mft_record_size_mask);
808 ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
809 vol->mft_record_size_bits, vol->mft_record_size_bits);
810 /*
811 * We cannot support mft record sizes above the PAGE_CACHE_SIZE since
812 * we store $MFT/$DATA, the table of mft records in the page cache.
813 */
814 if (vol->mft_record_size > PAGE_CACHE_SIZE) {
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000815 ntfs_error(vol->sb, "Mft record size (%i) exceeds the "
816 "PAGE_CACHE_SIZE on your system (%lu). "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 "This is not supported. Sorry.",
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000818 vol->mft_record_size, PAGE_CACHE_SIZE);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700819 return false;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000820 }
821 /* We cannot support mft record sizes below the sector size. */
822 if (vol->mft_record_size < vol->sector_size) {
823 ntfs_error(vol->sb, "Mft record size (%i) is smaller than the "
824 "sector size (%i). This is not supported. "
825 "Sorry.", vol->mft_record_size,
826 vol->sector_size);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700827 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829 clusters_per_index_record = b->clusters_per_index_record;
830 ntfs_debug("clusters_per_index_record = %i (0x%x)",
831 clusters_per_index_record, clusters_per_index_record);
832 if (clusters_per_index_record > 0)
833 vol->index_record_size = vol->cluster_size <<
834 (ffs(clusters_per_index_record) - 1);
835 else
836 /*
837 * When index_record_size < cluster_size,
838 * clusters_per_index_record = -log2(index_record_size) bytes.
839 * index_record_size normaly equals 4096 bytes, which is
840 * encoded as 0xF4 (-12 in decimal).
841 */
842 vol->index_record_size = 1 << -clusters_per_index_record;
843 vol->index_record_size_mask = vol->index_record_size - 1;
844 vol->index_record_size_bits = ffs(vol->index_record_size) - 1;
845 ntfs_debug("vol->index_record_size = %i (0x%x)",
846 vol->index_record_size, vol->index_record_size);
847 ntfs_debug("vol->index_record_size_mask = 0x%x",
848 vol->index_record_size_mask);
849 ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
850 vol->index_record_size_bits,
851 vol->index_record_size_bits);
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000852 /* We cannot support index record sizes below the sector size. */
853 if (vol->index_record_size < vol->sector_size) {
854 ntfs_error(vol->sb, "Index record size (%i) is smaller than "
855 "the sector size (%i). This is not "
856 "supported. Sorry.", vol->index_record_size,
857 vol->sector_size);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700858 return false;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /*
861 * Get the size of the volume in clusters and check for 64-bit-ness.
862 * Windows currently only uses 32 bits to save the clusters so we do
863 * the same as it is much faster on 32-bit CPUs.
864 */
865 ll = sle64_to_cpu(b->number_of_sectors) >> sectors_per_cluster_bits;
866 if ((u64)ll >= 1ULL << 32) {
867 ntfs_error(vol->sb, "Cannot handle 64-bit clusters. Sorry.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700868 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870 vol->nr_clusters = ll;
871 ntfs_debug("vol->nr_clusters = 0x%llx", (long long)vol->nr_clusters);
872 /*
873 * On an architecture where unsigned long is 32-bits, we restrict the
874 * volume size to 2TiB (2^41). On a 64-bit architecture, the compiler
875 * will hopefully optimize the whole check away.
876 */
877 if (sizeof(unsigned long) < 8) {
878 if ((ll << vol->cluster_size_bits) >= (1ULL << 41)) {
879 ntfs_error(vol->sb, "Volume size (%lluTiB) is too "
880 "large for this architecture. "
881 "Maximum supported is 2TiB. Sorry.",
882 (unsigned long long)ll >> (40 -
883 vol->cluster_size_bits));
Richard Knutssonc49c3112006-09-30 23:27:12 -0700884 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
886 }
887 ll = sle64_to_cpu(b->mft_lcn);
888 if (ll >= vol->nr_clusters) {
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000889 ntfs_error(vol->sb, "MFT LCN (%lli, 0x%llx) is beyond end of "
890 "volume. Weird.", (unsigned long long)ll,
891 (unsigned long long)ll);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700892 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894 vol->mft_lcn = ll;
895 ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol->mft_lcn);
896 ll = sle64_to_cpu(b->mftmirr_lcn);
897 if (ll >= vol->nr_clusters) {
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000898 ntfs_error(vol->sb, "MFTMirr LCN (%lli, 0x%llx) is beyond end "
899 "of volume. Weird.", (unsigned long long)ll,
900 (unsigned long long)ll);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700901 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903 vol->mftmirr_lcn = ll;
904 ntfs_debug("vol->mftmirr_lcn = 0x%llx", (long long)vol->mftmirr_lcn);
905#ifdef NTFS_RW
906 /*
907 * Work out the size of the mft mirror in number of mft records. If the
908 * cluster size is less than or equal to the size taken by four mft
909 * records, the mft mirror stores the first four mft records. If the
910 * cluster size is bigger than the size taken by four mft records, the
911 * mft mirror contains as many mft records as will fit into one
912 * cluster.
913 */
914 if (vol->cluster_size <= (4 << vol->mft_record_size_bits))
915 vol->mftmirr_size = 4;
916 else
917 vol->mftmirr_size = vol->cluster_size >>
918 vol->mft_record_size_bits;
919 ntfs_debug("vol->mftmirr_size = %i", vol->mftmirr_size);
920#endif /* NTFS_RW */
921 vol->serial_no = le64_to_cpu(b->volume_serial_number);
922 ntfs_debug("vol->serial_no = 0x%llx",
923 (unsigned long long)vol->serial_no);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700924 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
927/**
928 * ntfs_setup_allocators - initialize the cluster and mft allocators
929 * @vol: volume structure for which to setup the allocators
930 *
931 * Setup the cluster (lcn) and mft allocators to the starting values.
932 */
933static void ntfs_setup_allocators(ntfs_volume *vol)
934{
935#ifdef NTFS_RW
936 LCN mft_zone_size, mft_lcn;
937#endif /* NTFS_RW */
938
939 ntfs_debug("vol->mft_zone_multiplier = 0x%x",
940 vol->mft_zone_multiplier);
941#ifdef NTFS_RW
942 /* Determine the size of the MFT zone. */
943 mft_zone_size = vol->nr_clusters;
944 switch (vol->mft_zone_multiplier) { /* % of volume size in clusters */
945 case 4:
946 mft_zone_size >>= 1; /* 50% */
947 break;
948 case 3:
949 mft_zone_size = (mft_zone_size +
950 (mft_zone_size >> 1)) >> 2; /* 37.5% */
951 break;
952 case 2:
953 mft_zone_size >>= 2; /* 25% */
954 break;
955 /* case 1: */
956 default:
957 mft_zone_size >>= 3; /* 12.5% */
958 break;
959 }
960 /* Setup the mft zone. */
961 vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn;
962 ntfs_debug("vol->mft_zone_pos = 0x%llx",
963 (unsigned long long)vol->mft_zone_pos);
964 /*
965 * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
966 * source) and if the actual mft_lcn is in the expected place or even
967 * further to the front of the volume, extend the mft_zone to cover the
968 * beginning of the volume as well. This is in order to protect the
969 * area reserved for the mft bitmap as well within the mft_zone itself.
970 * On non-standard volumes we do not protect it as the overhead would
971 * be higher than the speed increase we would get by doing it.
972 */
973 mft_lcn = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size;
974 if (mft_lcn * vol->cluster_size < 16 * 1024)
975 mft_lcn = (16 * 1024 + vol->cluster_size - 1) /
976 vol->cluster_size;
977 if (vol->mft_zone_start <= mft_lcn)
978 vol->mft_zone_start = 0;
979 ntfs_debug("vol->mft_zone_start = 0x%llx",
980 (unsigned long long)vol->mft_zone_start);
981 /*
982 * Need to cap the mft zone on non-standard volumes so that it does
983 * not point outside the boundaries of the volume. We do this by
984 * halving the zone size until we are inside the volume.
985 */
986 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
987 while (vol->mft_zone_end >= vol->nr_clusters) {
988 mft_zone_size >>= 1;
989 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
990 }
991 ntfs_debug("vol->mft_zone_end = 0x%llx",
992 (unsigned long long)vol->mft_zone_end);
993 /*
994 * Set the current position within each data zone to the start of the
995 * respective zone.
996 */
997 vol->data1_zone_pos = vol->mft_zone_end;
998 ntfs_debug("vol->data1_zone_pos = 0x%llx",
999 (unsigned long long)vol->data1_zone_pos);
1000 vol->data2_zone_pos = 0;
1001 ntfs_debug("vol->data2_zone_pos = 0x%llx",
1002 (unsigned long long)vol->data2_zone_pos);
1003
1004 /* Set the mft data allocation position to mft record 24. */
1005 vol->mft_data_pos = 24;
1006 ntfs_debug("vol->mft_data_pos = 0x%llx",
1007 (unsigned long long)vol->mft_data_pos);
1008#endif /* NTFS_RW */
1009}
1010
1011#ifdef NTFS_RW
1012
1013/**
1014 * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume
1015 * @vol: ntfs super block describing device whose mft mirror to load
1016 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001017 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001019static bool load_and_init_mft_mirror(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 struct inode *tmp_ino;
1022 ntfs_inode *tmp_ni;
1023
1024 ntfs_debug("Entering.");
1025 /* Get mft mirror inode. */
1026 tmp_ino = ntfs_iget(vol->sb, FILE_MFTMirr);
1027 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1028 if (!IS_ERR(tmp_ino))
1029 iput(tmp_ino);
1030 /* Caller will display error message. */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001031 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033 /*
1034 * Re-initialize some specifics about $MFTMirr's inode as
1035 * ntfs_read_inode() will have set up the default ones.
1036 */
1037 /* Set uid and gid to root. */
1038 tmp_ino->i_uid = tmp_ino->i_gid = 0;
1039 /* Regular file. No access for anyone. */
1040 tmp_ino->i_mode = S_IFREG;
1041 /* No VFS initiated operations allowed for $MFTMirr. */
1042 tmp_ino->i_op = &ntfs_empty_inode_ops;
1043 tmp_ino->i_fop = &ntfs_empty_file_ops;
1044 /* Put in our special address space operations. */
1045 tmp_ino->i_mapping->a_ops = &ntfs_mst_aops;
1046 tmp_ni = NTFS_I(tmp_ino);
1047 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
1048 NInoSetMstProtected(tmp_ni);
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001049 NInoSetSparseDisabled(tmp_ni);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /*
1051 * Set up our little cheat allowing us to reuse the async read io
1052 * completion handler for directories.
1053 */
1054 tmp_ni->itype.index.block_size = vol->mft_record_size;
1055 tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits;
1056 vol->mftmirr_ino = tmp_ino;
1057 ntfs_debug("Done.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001058 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061/**
1062 * check_mft_mirror - compare contents of the mft mirror with the mft
1063 * @vol: ntfs super block describing device whose mft mirror to check
1064 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001065 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 *
1067 * Note, this function also results in the mft mirror runlist being completely
1068 * mapped into memory. The mft mirror write code requires this and will BUG()
1069 * should it find an unmapped runlist element.
1070 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001071static bool check_mft_mirror(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 struct super_block *sb = vol->sb;
1074 ntfs_inode *mirr_ni;
1075 struct page *mft_page, *mirr_page;
1076 u8 *kmft, *kmirr;
1077 runlist_element *rl, rl2[2];
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001078 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 int mrecs_per_page, i;
1080
1081 ntfs_debug("Entering.");
1082 /* Compare contents of $MFT and $MFTMirr. */
1083 mrecs_per_page = PAGE_CACHE_SIZE / vol->mft_record_size;
1084 BUG_ON(!mrecs_per_page);
1085 BUG_ON(!vol->mftmirr_size);
1086 mft_page = mirr_page = NULL;
1087 kmft = kmirr = NULL;
1088 index = i = 0;
1089 do {
1090 u32 bytes;
1091
1092 /* Switch pages if necessary. */
1093 if (!(i % mrecs_per_page)) {
1094 if (index) {
1095 ntfs_unmap_page(mft_page);
1096 ntfs_unmap_page(mirr_page);
1097 }
1098 /* Get the $MFT page. */
1099 mft_page = ntfs_map_page(vol->mft_ino->i_mapping,
1100 index);
1101 if (IS_ERR(mft_page)) {
1102 ntfs_error(sb, "Failed to read $MFT.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001103 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105 kmft = page_address(mft_page);
1106 /* Get the $MFTMirr page. */
1107 mirr_page = ntfs_map_page(vol->mftmirr_ino->i_mapping,
1108 index);
1109 if (IS_ERR(mirr_page)) {
1110 ntfs_error(sb, "Failed to read $MFTMirr.");
1111 goto mft_unmap_out;
1112 }
1113 kmirr = page_address(mirr_page);
1114 ++index;
1115 }
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001116 /* Do not check the record if it is not in use. */
1117 if (((MFT_RECORD*)kmft)->flags & MFT_RECORD_IN_USE) {
1118 /* Make sure the record is ok. */
1119 if (ntfs_is_baad_recordp((le32*)kmft)) {
1120 ntfs_error(sb, "Incomplete multi sector "
1121 "transfer detected in mft "
1122 "record %i.", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123mm_unmap_out:
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001124 ntfs_unmap_page(mirr_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125mft_unmap_out:
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001126 ntfs_unmap_page(mft_page);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001127 return false;
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001130 /* Do not check the mirror record if it is not in use. */
1131 if (((MFT_RECORD*)kmirr)->flags & MFT_RECORD_IN_USE) {
1132 if (ntfs_is_baad_recordp((le32*)kmirr)) {
1133 ntfs_error(sb, "Incomplete multi sector "
1134 "transfer detected in mft "
1135 "mirror record %i.", i);
1136 goto mm_unmap_out;
1137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139 /* Get the amount of data in the current record. */
1140 bytes = le32_to_cpu(((MFT_RECORD*)kmft)->bytes_in_use);
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001141 if (bytes < sizeof(MFT_RECORD_OLD) ||
1142 bytes > vol->mft_record_size ||
1143 ntfs_is_baad_recordp((le32*)kmft)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 bytes = le32_to_cpu(((MFT_RECORD*)kmirr)->bytes_in_use);
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001145 if (bytes < sizeof(MFT_RECORD_OLD) ||
1146 bytes > vol->mft_record_size ||
1147 ntfs_is_baad_recordp((le32*)kmirr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 bytes = vol->mft_record_size;
1149 }
1150 /* Compare the two records. */
1151 if (memcmp(kmft, kmirr, bytes)) {
1152 ntfs_error(sb, "$MFT and $MFTMirr (record %i) do not "
1153 "match. Run ntfsfix or chkdsk.", i);
1154 goto mm_unmap_out;
1155 }
1156 kmft += vol->mft_record_size;
1157 kmirr += vol->mft_record_size;
1158 } while (++i < vol->mftmirr_size);
1159 /* Release the last pages. */
1160 ntfs_unmap_page(mft_page);
1161 ntfs_unmap_page(mirr_page);
1162
1163 /* Construct the mft mirror runlist by hand. */
1164 rl2[0].vcn = 0;
1165 rl2[0].lcn = vol->mftmirr_lcn;
1166 rl2[0].length = (vol->mftmirr_size * vol->mft_record_size +
1167 vol->cluster_size - 1) / vol->cluster_size;
1168 rl2[1].vcn = rl2[0].length;
1169 rl2[1].lcn = LCN_ENOENT;
1170 rl2[1].length = 0;
1171 /*
1172 * Because we have just read all of the mft mirror, we know we have
1173 * mapped the full runlist for it.
1174 */
1175 mirr_ni = NTFS_I(vol->mftmirr_ino);
1176 down_read(&mirr_ni->runlist.lock);
1177 rl = mirr_ni->runlist.rl;
1178 /* Compare the two runlists. They must be identical. */
1179 i = 0;
1180 do {
1181 if (rl2[i].vcn != rl[i].vcn || rl2[i].lcn != rl[i].lcn ||
1182 rl2[i].length != rl[i].length) {
1183 ntfs_error(sb, "$MFTMirr location mismatch. "
1184 "Run chkdsk.");
1185 up_read(&mirr_ni->runlist.lock);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001186 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188 } while (rl2[i++].length);
1189 up_read(&mirr_ni->runlist.lock);
1190 ntfs_debug("Done.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001191 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
1194/**
1195 * load_and_check_logfile - load and check the logfile inode for a volume
1196 * @vol: ntfs super block describing device whose logfile to load
1197 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001198 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001200static bool load_and_check_logfile(ntfs_volume *vol,
Anton Altaparmakove7a10332005-09-08 16:12:28 +01001201 RESTART_PAGE_HEADER **rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 struct inode *tmp_ino;
1204
1205 ntfs_debug("Entering.");
1206 tmp_ino = ntfs_iget(vol->sb, FILE_LogFile);
1207 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1208 if (!IS_ERR(tmp_ino))
1209 iput(tmp_ino);
1210 /* Caller will display error message. */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001211 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
Anton Altaparmakove7a10332005-09-08 16:12:28 +01001213 if (!ntfs_check_logfile(tmp_ino, rp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 iput(tmp_ino);
1215 /* ntfs_check_logfile() will have displayed error output. */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001216 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001218 NInoSetSparseDisabled(NTFS_I(tmp_ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 vol->logfile_ino = tmp_ino;
1220 ntfs_debug("Done.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001221 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001224#define NTFS_HIBERFIL_HEADER_SIZE 4096
1225
1226/**
1227 * check_windows_hibernation_status - check if Windows is suspended on a volume
1228 * @vol: ntfs super block of device to check
1229 *
1230 * Check if Windows is hibernated on the ntfs volume @vol. This is done by
1231 * looking for the file hiberfil.sys in the root directory of the volume. If
1232 * the file is not present Windows is definitely not suspended.
1233 *
1234 * If hiberfil.sys exists and is less than 4kiB in size it means Windows is
1235 * definitely suspended (this volume is not the system volume). Caveat: on a
1236 * system with many volumes it is possible that the < 4kiB check is bogus but
1237 * for now this should do fine.
1238 *
1239 * If hiberfil.sys exists and is larger than 4kiB in size, we need to read the
1240 * hiberfil header (which is the first 4kiB). If this begins with "hibr",
1241 * Windows is definitely suspended. If it is completely full of zeroes,
1242 * Windows is definitely not hibernated. Any other case is treated as if
1243 * Windows is suspended. This caters for the above mentioned caveat of a
1244 * system with many volumes where no "hibr" magic would be present and there is
1245 * no zero header.
1246 *
1247 * Return 0 if Windows is not hibernated on the volume, >0 if Windows is
1248 * hibernated on the volume, and -errno on error.
1249 */
1250static int check_windows_hibernation_status(ntfs_volume *vol)
1251{
1252 MFT_REF mref;
1253 struct inode *vi;
1254 ntfs_inode *ni;
1255 struct page *page;
1256 u32 *kaddr, *kend;
1257 ntfs_name *name = NULL;
1258 int ret = 1;
Harvey Harrison63cd8852009-03-31 15:23:52 -07001259 static const ntfschar hiberfil[13] = { cpu_to_le16('h'),
1260 cpu_to_le16('i'), cpu_to_le16('b'),
1261 cpu_to_le16('e'), cpu_to_le16('r'),
1262 cpu_to_le16('f'), cpu_to_le16('i'),
1263 cpu_to_le16('l'), cpu_to_le16('.'),
1264 cpu_to_le16('s'), cpu_to_le16('y'),
1265 cpu_to_le16('s'), 0 };
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001266
1267 ntfs_debug("Entering.");
1268 /*
1269 * Find the inode number for the hibernation file by looking up the
1270 * filename hiberfil.sys in the root directory.
1271 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001272 mutex_lock(&vol->root_ino->i_mutex);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001273 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->root_ino), hiberfil, 12,
1274 &name);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001275 mutex_unlock(&vol->root_ino->i_mutex);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001276 if (IS_ERR_MREF(mref)) {
1277 ret = MREF_ERR(mref);
1278 /* If the file does not exist, Windows is not hibernated. */
1279 if (ret == -ENOENT) {
1280 ntfs_debug("hiberfil.sys not present. Windows is not "
1281 "hibernated on the volume.");
1282 return 0;
1283 }
1284 /* A real error occured. */
1285 ntfs_error(vol->sb, "Failed to find inode number for "
1286 "hiberfil.sys.");
1287 return ret;
1288 }
1289 /* We do not care for the type of match that was found. */
1290 kfree(name);
1291 /* Get the inode. */
1292 vi = ntfs_iget(vol->sb, MREF(mref));
1293 if (IS_ERR(vi) || is_bad_inode(vi)) {
1294 if (!IS_ERR(vi))
1295 iput(vi);
1296 ntfs_error(vol->sb, "Failed to load hiberfil.sys.");
1297 return IS_ERR(vi) ? PTR_ERR(vi) : -EIO;
1298 }
1299 if (unlikely(i_size_read(vi) < NTFS_HIBERFIL_HEADER_SIZE)) {
1300 ntfs_debug("hiberfil.sys is smaller than 4kiB (0x%llx). "
1301 "Windows is hibernated on the volume. This "
1302 "is not the system volume.", i_size_read(vi));
1303 goto iput_out;
1304 }
1305 ni = NTFS_I(vi);
1306 page = ntfs_map_page(vi->i_mapping, 0);
1307 if (IS_ERR(page)) {
1308 ntfs_error(vol->sb, "Failed to read from hiberfil.sys.");
1309 ret = PTR_ERR(page);
1310 goto iput_out;
1311 }
1312 kaddr = (u32*)page_address(page);
Harvey Harrison63cd8852009-03-31 15:23:52 -07001313 if (*(le32*)kaddr == cpu_to_le32(0x72626968)/*'hibr'*/) {
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001314 ntfs_debug("Magic \"hibr\" found in hiberfil.sys. Windows is "
1315 "hibernated on the volume. This is the "
1316 "system volume.");
1317 goto unm_iput_out;
1318 }
1319 kend = kaddr + NTFS_HIBERFIL_HEADER_SIZE/sizeof(*kaddr);
1320 do {
1321 if (unlikely(*kaddr)) {
1322 ntfs_debug("hiberfil.sys is larger than 4kiB "
1323 "(0x%llx), does not contain the "
1324 "\"hibr\" magic, and does not have a "
1325 "zero header. Windows is hibernated "
1326 "on the volume. This is not the "
1327 "system volume.", i_size_read(vi));
1328 goto unm_iput_out;
1329 }
1330 } while (++kaddr < kend);
1331 ntfs_debug("hiberfil.sys contains a zero header. Windows is not "
1332 "hibernated on the volume. This is the system "
1333 "volume.");
1334 ret = 0;
1335unm_iput_out:
1336 ntfs_unmap_page(page);
1337iput_out:
1338 iput(vi);
1339 return ret;
1340}
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342/**
1343 * load_and_init_quota - load and setup the quota file for a volume if present
1344 * @vol: ntfs super block describing device whose quota file to load
1345 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001346 * Return 'true' on success or 'false' on error. If $Quota is not present, we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 * leave vol->quota_ino as NULL and return success.
1348 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001349static bool load_and_init_quota(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 MFT_REF mref;
1352 struct inode *tmp_ino;
1353 ntfs_name *name = NULL;
Harvey Harrison63cd8852009-03-31 15:23:52 -07001354 static const ntfschar Quota[7] = { cpu_to_le16('$'),
1355 cpu_to_le16('Q'), cpu_to_le16('u'),
1356 cpu_to_le16('o'), cpu_to_le16('t'),
1357 cpu_to_le16('a'), 0 };
1358 static ntfschar Q[3] = { cpu_to_le16('$'),
1359 cpu_to_le16('Q'), 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 ntfs_debug("Entering.");
1362 /*
1363 * Find the inode number for the quota file by looking up the filename
1364 * $Quota in the extended system files directory $Extend.
1365 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001366 mutex_lock(&vol->extend_ino->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), Quota, 6,
1368 &name);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001369 mutex_unlock(&vol->extend_ino->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (IS_ERR_MREF(mref)) {
1371 /*
1372 * If the file does not exist, quotas are disabled and have
1373 * never been enabled on this volume, just return success.
1374 */
1375 if (MREF_ERR(mref) == -ENOENT) {
1376 ntfs_debug("$Quota not present. Volume does not have "
1377 "quotas enabled.");
1378 /*
1379 * No need to try to set quotas out of date if they are
1380 * not enabled.
1381 */
1382 NVolSetQuotaOutOfDate(vol);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001383 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
1385 /* A real error occured. */
1386 ntfs_error(vol->sb, "Failed to find inode number for $Quota.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001387 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389 /* We do not care for the type of match that was found. */
Jesper Juhl251c8422005-04-04 14:59:56 +01001390 kfree(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /* Get the inode. */
1392 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1393 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1394 if (!IS_ERR(tmp_ino))
1395 iput(tmp_ino);
1396 ntfs_error(vol->sb, "Failed to load $Quota.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001397 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399 vol->quota_ino = tmp_ino;
1400 /* Get the $Q index allocation attribute. */
1401 tmp_ino = ntfs_index_iget(vol->quota_ino, Q, 2);
1402 if (IS_ERR(tmp_ino)) {
1403 ntfs_error(vol->sb, "Failed to load $Quota/$Q index.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001404 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406 vol->quota_q_ino = tmp_ino;
1407 ntfs_debug("Done.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001408 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410
1411/**
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001412 * load_and_init_usnjrnl - load and setup the transaction log if present
1413 * @vol: ntfs super block describing device whose usnjrnl file to load
1414 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001415 * Return 'true' on success or 'false' on error.
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001416 *
1417 * If $UsnJrnl is not present or in the process of being disabled, we set
1418 * NVolUsnJrnlStamped() and return success.
1419 *
1420 * If the $UsnJrnl $DATA/$J attribute has a size equal to the lowest valid usn,
1421 * i.e. transaction logging has only just been enabled or the journal has been
1422 * stamped and nothing has been logged since, we also set NVolUsnJrnlStamped()
1423 * and return success.
1424 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001425static bool load_and_init_usnjrnl(ntfs_volume *vol)
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001426{
1427 MFT_REF mref;
1428 struct inode *tmp_ino;
1429 ntfs_inode *tmp_ni;
1430 struct page *page;
1431 ntfs_name *name = NULL;
1432 USN_HEADER *uh;
Harvey Harrison63cd8852009-03-31 15:23:52 -07001433 static const ntfschar UsnJrnl[9] = { cpu_to_le16('$'),
1434 cpu_to_le16('U'), cpu_to_le16('s'),
1435 cpu_to_le16('n'), cpu_to_le16('J'),
1436 cpu_to_le16('r'), cpu_to_le16('n'),
1437 cpu_to_le16('l'), 0 };
1438 static ntfschar Max[5] = { cpu_to_le16('$'),
1439 cpu_to_le16('M'), cpu_to_le16('a'),
1440 cpu_to_le16('x'), 0 };
1441 static ntfschar J[3] = { cpu_to_le16('$'),
1442 cpu_to_le16('J'), 0 };
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001443
1444 ntfs_debug("Entering.");
1445 /*
1446 * Find the inode number for the transaction log file by looking up the
1447 * filename $UsnJrnl in the extended system files directory $Extend.
1448 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001449 mutex_lock(&vol->extend_ino->i_mutex);
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001450 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), UsnJrnl, 8,
1451 &name);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001452 mutex_unlock(&vol->extend_ino->i_mutex);
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001453 if (IS_ERR_MREF(mref)) {
1454 /*
1455 * If the file does not exist, transaction logging is disabled,
1456 * just return success.
1457 */
1458 if (MREF_ERR(mref) == -ENOENT) {
1459 ntfs_debug("$UsnJrnl not present. Volume does not "
1460 "have transaction logging enabled.");
1461not_enabled:
1462 /*
1463 * No need to try to stamp the transaction log if
1464 * transaction logging is not enabled.
1465 */
1466 NVolSetUsnJrnlStamped(vol);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001467 return true;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001468 }
1469 /* A real error occured. */
1470 ntfs_error(vol->sb, "Failed to find inode number for "
1471 "$UsnJrnl.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001472 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001473 }
1474 /* We do not care for the type of match that was found. */
1475 kfree(name);
1476 /* Get the inode. */
1477 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1478 if (unlikely(IS_ERR(tmp_ino) || is_bad_inode(tmp_ino))) {
1479 if (!IS_ERR(tmp_ino))
1480 iput(tmp_ino);
1481 ntfs_error(vol->sb, "Failed to load $UsnJrnl.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001482 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001483 }
1484 vol->usnjrnl_ino = tmp_ino;
1485 /*
1486 * If the transaction log is in the process of being deleted, we can
1487 * ignore it.
1488 */
1489 if (unlikely(vol->vol_flags & VOLUME_DELETE_USN_UNDERWAY)) {
1490 ntfs_debug("$UsnJrnl in the process of being disabled. "
1491 "Volume does not have transaction logging "
1492 "enabled.");
1493 goto not_enabled;
1494 }
1495 /* Get the $DATA/$Max attribute. */
1496 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, Max, 4);
1497 if (IS_ERR(tmp_ino)) {
1498 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$Max "
1499 "attribute.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001500 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001501 }
1502 vol->usnjrnl_max_ino = tmp_ino;
1503 if (unlikely(i_size_read(tmp_ino) < sizeof(USN_HEADER))) {
1504 ntfs_error(vol->sb, "Found corrupt $UsnJrnl/$DATA/$Max "
1505 "attribute (size is 0x%llx but should be at "
Anton Altaparmakovd04bd1f2005-10-24 08:41:24 +01001506 "least 0x%zx bytes).", i_size_read(tmp_ino),
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001507 sizeof(USN_HEADER));
Richard Knutssonc49c3112006-09-30 23:27:12 -07001508 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001509 }
1510 /* Get the $DATA/$J attribute. */
1511 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, J, 2);
1512 if (IS_ERR(tmp_ino)) {
1513 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$J "
1514 "attribute.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001515 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001516 }
1517 vol->usnjrnl_j_ino = tmp_ino;
1518 /* Verify $J is non-resident and sparse. */
1519 tmp_ni = NTFS_I(vol->usnjrnl_j_ino);
1520 if (unlikely(!NInoNonResident(tmp_ni) || !NInoSparse(tmp_ni))) {
1521 ntfs_error(vol->sb, "$UsnJrnl/$DATA/$J attribute is resident "
1522 "and/or not sparse.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001523 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001524 }
1525 /* Read the USN_HEADER from $DATA/$Max. */
1526 page = ntfs_map_page(vol->usnjrnl_max_ino->i_mapping, 0);
1527 if (IS_ERR(page)) {
1528 ntfs_error(vol->sb, "Failed to read from $UsnJrnl/$DATA/$Max "
1529 "attribute.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001530 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001531 }
1532 uh = (USN_HEADER*)page_address(page);
1533 /* Sanity check the $Max. */
1534 if (unlikely(sle64_to_cpu(uh->allocation_delta) >
1535 sle64_to_cpu(uh->maximum_size))) {
1536 ntfs_error(vol->sb, "Allocation delta (0x%llx) exceeds "
1537 "maximum size (0x%llx). $UsnJrnl is corrupt.",
1538 (long long)sle64_to_cpu(uh->allocation_delta),
1539 (long long)sle64_to_cpu(uh->maximum_size));
1540 ntfs_unmap_page(page);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001541 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001542 }
1543 /*
1544 * If the transaction log has been stamped and nothing has been written
1545 * to it since, we do not need to stamp it.
1546 */
1547 if (unlikely(sle64_to_cpu(uh->lowest_valid_usn) >=
1548 i_size_read(vol->usnjrnl_j_ino))) {
1549 if (likely(sle64_to_cpu(uh->lowest_valid_usn) ==
1550 i_size_read(vol->usnjrnl_j_ino))) {
1551 ntfs_unmap_page(page);
1552 ntfs_debug("$UsnJrnl is enabled but nothing has been "
1553 "logged since it was last stamped. "
1554 "Treating this as if the volume does "
1555 "not have transaction logging "
1556 "enabled.");
1557 goto not_enabled;
1558 }
1559 ntfs_error(vol->sb, "$UsnJrnl has lowest valid usn (0x%llx) "
1560 "which is out of bounds (0x%llx). $UsnJrnl "
1561 "is corrupt.",
1562 (long long)sle64_to_cpu(uh->lowest_valid_usn),
1563 i_size_read(vol->usnjrnl_j_ino));
1564 ntfs_unmap_page(page);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001565 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001566 }
1567 ntfs_unmap_page(page);
1568 ntfs_debug("Done.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001569 return true;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001570}
1571
1572/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 * load_and_init_attrdef - load the attribute definitions table for a volume
1574 * @vol: ntfs super block describing device whose attrdef to load
1575 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001576 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001578static bool load_and_init_attrdef(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001580 loff_t i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 struct super_block *sb = vol->sb;
1582 struct inode *ino;
1583 struct page *page;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001584 pgoff_t index, max_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 unsigned int size;
1586
1587 ntfs_debug("Entering.");
1588 /* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */
1589 ino = ntfs_iget(sb, FILE_AttrDef);
1590 if (IS_ERR(ino) || is_bad_inode(ino)) {
1591 if (!IS_ERR(ino))
1592 iput(ino);
1593 goto failed;
1594 }
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001595 NInoSetSparseDisabled(NTFS_I(ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001597 i_size = i_size_read(ino);
1598 if (i_size <= 0 || i_size > 0x7fffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 goto iput_failed;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001600 vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (!vol->attrdef)
1602 goto iput_failed;
1603 index = 0;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001604 max_index = i_size >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 size = PAGE_CACHE_SIZE;
1606 while (index < max_index) {
1607 /* Read the attrdef table and copy it into the linear buffer. */
1608read_partial_attrdef_page:
1609 page = ntfs_map_page(ino->i_mapping, index);
1610 if (IS_ERR(page))
1611 goto free_iput_failed;
1612 memcpy((u8*)vol->attrdef + (index++ << PAGE_CACHE_SHIFT),
1613 page_address(page), size);
1614 ntfs_unmap_page(page);
1615 };
1616 if (size == PAGE_CACHE_SIZE) {
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001617 size = i_size & ~PAGE_CACHE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (size)
1619 goto read_partial_attrdef_page;
1620 }
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001621 vol->attrdef_size = i_size;
1622 ntfs_debug("Read %llu bytes from $AttrDef.", i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 iput(ino);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001624 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625free_iput_failed:
1626 ntfs_free(vol->attrdef);
1627 vol->attrdef = NULL;
1628iput_failed:
1629 iput(ino);
1630failed:
1631 ntfs_error(sb, "Failed to initialize attribute definition table.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001632 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
1635#endif /* NTFS_RW */
1636
1637/**
1638 * load_and_init_upcase - load the upcase table for an ntfs volume
1639 * @vol: ntfs super block describing device whose upcase to load
1640 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001641 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001643static bool load_and_init_upcase(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001645 loff_t i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 struct super_block *sb = vol->sb;
1647 struct inode *ino;
1648 struct page *page;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001649 pgoff_t index, max_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 unsigned int size;
1651 int i, max;
1652
1653 ntfs_debug("Entering.");
1654 /* Read upcase table and setup vol->upcase and vol->upcase_len. */
1655 ino = ntfs_iget(sb, FILE_UpCase);
1656 if (IS_ERR(ino) || is_bad_inode(ino)) {
1657 if (!IS_ERR(ino))
1658 iput(ino);
1659 goto upcase_failed;
1660 }
1661 /*
1662 * The upcase size must not be above 64k Unicode characters, must not
1663 * be zero and must be a multiple of sizeof(ntfschar).
1664 */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001665 i_size = i_size_read(ino);
1666 if (!i_size || i_size & (sizeof(ntfschar) - 1) ||
1667 i_size > 64ULL * 1024 * sizeof(ntfschar))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 goto iput_upcase_failed;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001669 vol->upcase = (ntfschar*)ntfs_malloc_nofs(i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (!vol->upcase)
1671 goto iput_upcase_failed;
1672 index = 0;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001673 max_index = i_size >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 size = PAGE_CACHE_SIZE;
1675 while (index < max_index) {
1676 /* Read the upcase table and copy it into the linear buffer. */
1677read_partial_upcase_page:
1678 page = ntfs_map_page(ino->i_mapping, index);
1679 if (IS_ERR(page))
1680 goto iput_upcase_failed;
1681 memcpy((char*)vol->upcase + (index++ << PAGE_CACHE_SHIFT),
1682 page_address(page), size);
1683 ntfs_unmap_page(page);
1684 };
1685 if (size == PAGE_CACHE_SIZE) {
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001686 size = i_size & ~PAGE_CACHE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (size)
1688 goto read_partial_upcase_page;
1689 }
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001690 vol->upcase_len = i_size >> UCHAR_T_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001692 i_size, 64 * 1024 * sizeof(ntfschar));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 iput(ino);
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001694 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if (!default_upcase) {
1696 ntfs_debug("Using volume specified $UpCase since default is "
1697 "not present.");
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001698 mutex_unlock(&ntfs_lock);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001699 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701 max = default_upcase_len;
1702 if (max > vol->upcase_len)
1703 max = vol->upcase_len;
1704 for (i = 0; i < max; i++)
1705 if (vol->upcase[i] != default_upcase[i])
1706 break;
1707 if (i == max) {
1708 ntfs_free(vol->upcase);
1709 vol->upcase = default_upcase;
1710 vol->upcase_len = max;
1711 ntfs_nr_upcase_users++;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001712 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 ntfs_debug("Volume specified $UpCase matches default. Using "
1714 "default.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001715 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001717 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 ntfs_debug("Using volume specified $UpCase since it does not match "
1719 "the default.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001720 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721iput_upcase_failed:
1722 iput(ino);
1723 ntfs_free(vol->upcase);
1724 vol->upcase = NULL;
1725upcase_failed:
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001726 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (default_upcase) {
1728 vol->upcase = default_upcase;
1729 vol->upcase_len = default_upcase_len;
1730 ntfs_nr_upcase_users++;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001731 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 ntfs_error(sb, "Failed to load $UpCase from the volume. Using "
1733 "default.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001734 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001736 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 ntfs_error(sb, "Failed to initialize upcase table.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001738 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
Ingo Molnar59345372006-07-03 00:25:18 -07001741/*
1742 * The lcn and mft bitmap inodes are NTFS-internal inodes with
1743 * their own special locking rules:
1744 */
1745static struct lock_class_key
1746 lcnbmp_runlist_lock_key, lcnbmp_mrec_lock_key,
1747 mftbmp_runlist_lock_key, mftbmp_mrec_lock_key;
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749/**
1750 * load_system_files - open the system files using normal functions
1751 * @vol: ntfs super block describing device whose system files to load
1752 *
1753 * Open the system files with normal access functions and complete setting up
1754 * the ntfs super block @vol.
1755 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001756 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001758static bool load_system_files(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
1760 struct super_block *sb = vol->sb;
1761 MFT_RECORD *m;
1762 VOLUME_INFORMATION *vi;
1763 ntfs_attr_search_ctx *ctx;
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001764#ifdef NTFS_RW
Anton Altaparmakov7d333d62005-09-08 23:01:16 +01001765 RESTART_PAGE_HEADER *rp;
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001766 int err;
1767#endif /* NTFS_RW */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 ntfs_debug("Entering.");
1770#ifdef NTFS_RW
1771 /* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */
1772 if (!load_and_init_mft_mirror(vol) || !check_mft_mirror(vol)) {
1773 static const char *es1 = "Failed to load $MFTMirr";
1774 static const char *es2 = "$MFTMirr does not match $MFT";
1775 static const char *es3 = ". Run ntfsfix and/or chkdsk.";
1776
1777 /* If a read-write mount, convert it to a read-only mount. */
1778 if (!(sb->s_flags & MS_RDONLY)) {
1779 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1780 ON_ERRORS_CONTINUE))) {
1781 ntfs_error(sb, "%s and neither on_errors="
1782 "continue nor on_errors="
1783 "remount-ro was specified%s",
1784 !vol->mftmirr_ino ? es1 : es2,
1785 es3);
1786 goto iput_mirr_err_out;
1787 }
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08001788 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 ntfs_error(sb, "%s. Mounting read-only%s",
1790 !vol->mftmirr_ino ? es1 : es2, es3);
1791 } else
1792 ntfs_warning(sb, "%s. Will not be able to remount "
1793 "read-write%s",
1794 !vol->mftmirr_ino ? es1 : es2, es3);
1795 /* This will prevent a read-write remount. */
1796 NVolSetErrors(vol);
1797 }
1798#endif /* NTFS_RW */
1799 /* Get mft bitmap attribute inode. */
1800 vol->mftbmp_ino = ntfs_attr_iget(vol->mft_ino, AT_BITMAP, NULL, 0);
1801 if (IS_ERR(vol->mftbmp_ino)) {
1802 ntfs_error(sb, "Failed to load $MFT/$BITMAP attribute.");
1803 goto iput_mirr_err_out;
1804 }
Ingo Molnar59345372006-07-03 00:25:18 -07001805 lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->runlist.lock,
1806 &mftbmp_runlist_lock_key);
1807 lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->mrec_lock,
1808 &mftbmp_mrec_lock_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */
1810 if (!load_and_init_upcase(vol))
1811 goto iput_mftbmp_err_out;
1812#ifdef NTFS_RW
1813 /*
1814 * Read attribute definitions table and setup @vol->attrdef and
1815 * @vol->attrdef_size.
1816 */
1817 if (!load_and_init_attrdef(vol))
1818 goto iput_upcase_err_out;
1819#endif /* NTFS_RW */
1820 /*
1821 * Get the cluster allocation bitmap inode and verify the size, no
1822 * need for any locking at this stage as we are already running
1823 * exclusively as we are mount in progress task.
1824 */
1825 vol->lcnbmp_ino = ntfs_iget(sb, FILE_Bitmap);
1826 if (IS_ERR(vol->lcnbmp_ino) || is_bad_inode(vol->lcnbmp_ino)) {
1827 if (!IS_ERR(vol->lcnbmp_ino))
1828 iput(vol->lcnbmp_ino);
1829 goto bitmap_failed;
1830 }
Ingo Molnar59345372006-07-03 00:25:18 -07001831 lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->runlist.lock,
1832 &lcnbmp_runlist_lock_key);
1833 lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->mrec_lock,
1834 &lcnbmp_mrec_lock_key);
1835
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001836 NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino));
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001837 if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 iput(vol->lcnbmp_ino);
1839bitmap_failed:
1840 ntfs_error(sb, "Failed to load $Bitmap.");
1841 goto iput_attrdef_err_out;
1842 }
1843 /*
1844 * Get the volume inode and setup our cache of the volume flags and
1845 * version.
1846 */
1847 vol->vol_ino = ntfs_iget(sb, FILE_Volume);
1848 if (IS_ERR(vol->vol_ino) || is_bad_inode(vol->vol_ino)) {
1849 if (!IS_ERR(vol->vol_ino))
1850 iput(vol->vol_ino);
1851volume_failed:
1852 ntfs_error(sb, "Failed to load $Volume.");
1853 goto iput_lcnbmp_err_out;
1854 }
1855 m = map_mft_record(NTFS_I(vol->vol_ino));
1856 if (IS_ERR(m)) {
1857iput_volume_failed:
1858 iput(vol->vol_ino);
1859 goto volume_failed;
1860 }
1861 if (!(ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m))) {
1862 ntfs_error(sb, "Failed to get attribute search context.");
1863 goto get_ctx_vol_failed;
1864 }
1865 if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
1866 ctx) || ctx->attr->non_resident || ctx->attr->flags) {
1867err_put_vol:
1868 ntfs_attr_put_search_ctx(ctx);
1869get_ctx_vol_failed:
1870 unmap_mft_record(NTFS_I(vol->vol_ino));
1871 goto iput_volume_failed;
1872 }
1873 vi = (VOLUME_INFORMATION*)((char*)ctx->attr +
1874 le16_to_cpu(ctx->attr->data.resident.value_offset));
1875 /* Some bounds checks. */
1876 if ((u8*)vi < (u8*)ctx->attr || (u8*)vi +
1877 le32_to_cpu(ctx->attr->data.resident.value_length) >
1878 (u8*)ctx->attr + le32_to_cpu(ctx->attr->length))
1879 goto err_put_vol;
1880 /* Copy the volume flags and version to the ntfs_volume structure. */
1881 vol->vol_flags = vi->flags;
1882 vol->major_ver = vi->major_ver;
1883 vol->minor_ver = vi->minor_ver;
1884 ntfs_attr_put_search_ctx(ctx);
1885 unmap_mft_record(NTFS_I(vol->vol_ino));
1886 printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver,
1887 vol->minor_ver);
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001888 if (vol->major_ver < 3 && NVolSparseEnabled(vol)) {
1889 ntfs_warning(vol->sb, "Disabling sparse support due to NTFS "
1890 "volume version %i.%i (need at least version "
1891 "3.0).", vol->major_ver, vol->minor_ver);
1892 NVolClearSparseEnabled(vol);
1893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894#ifdef NTFS_RW
1895 /* Make sure that no unsupported volume flags are set. */
1896 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
1897 static const char *es1a = "Volume is dirty";
Anton Altaparmakov1cf3109f2006-02-24 10:48:14 +00001898 static const char *es1b = "Volume has been modified by chkdsk";
1899 static const char *es1c = "Volume has unsupported flags set";
1900 static const char *es2a = ". Run chkdsk and mount in Windows.";
1901 static const char *es2b = ". Mount in Windows.";
1902 const char *es1, *es2;
1903
1904 es2 = es2a;
1905 if (vol->vol_flags & VOLUME_IS_DIRTY)
1906 es1 = es1a;
1907 else if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
1908 es1 = es1b;
1909 es2 = es2b;
1910 } else {
1911 es1 = es1c;
1912 ntfs_warning(sb, "Unsupported volume flags 0x%x "
1913 "encountered.",
1914 (unsigned)le16_to_cpu(vol->vol_flags));
1915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 /* If a read-write mount, convert it to a read-only mount. */
1917 if (!(sb->s_flags & MS_RDONLY)) {
1918 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1919 ON_ERRORS_CONTINUE))) {
1920 ntfs_error(sb, "%s and neither on_errors="
1921 "continue nor on_errors="
1922 "remount-ro was specified%s",
1923 es1, es2);
1924 goto iput_vol_err_out;
1925 }
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08001926 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1928 } else
1929 ntfs_warning(sb, "%s. Will not be able to remount "
1930 "read-write%s", es1, es2);
1931 /*
1932 * Do not set NVolErrors() because ntfs_remount() re-checks the
1933 * flags which we need to do in case any flags have changed.
1934 */
1935 }
1936 /*
1937 * Get the inode for the logfile, check it and determine if the volume
1938 * was shutdown cleanly.
1939 */
Anton Altaparmakove7a10332005-09-08 16:12:28 +01001940 rp = NULL;
1941 if (!load_and_check_logfile(vol, &rp) ||
1942 !ntfs_is_logfile_clean(vol->logfile_ino, rp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 static const char *es1a = "Failed to load $LogFile";
1944 static const char *es1b = "$LogFile is not clean";
1945 static const char *es2 = ". Mount in Windows.";
1946 const char *es1;
1947
1948 es1 = !vol->logfile_ino ? es1a : es1b;
1949 /* If a read-write mount, convert it to a read-only mount. */
1950 if (!(sb->s_flags & MS_RDONLY)) {
1951 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1952 ON_ERRORS_CONTINUE))) {
1953 ntfs_error(sb, "%s and neither on_errors="
1954 "continue nor on_errors="
1955 "remount-ro was specified%s",
1956 es1, es2);
Anton Altaparmakove7a10332005-09-08 16:12:28 +01001957 if (vol->logfile_ino) {
1958 BUG_ON(!rp);
1959 ntfs_free(rp);
1960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 goto iput_logfile_err_out;
1962 }
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08001963 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1965 } else
1966 ntfs_warning(sb, "%s. Will not be able to remount "
1967 "read-write%s", es1, es2);
1968 /* This will prevent a read-write remount. */
1969 NVolSetErrors(vol);
1970 }
Anton Altaparmakove7a10332005-09-08 16:12:28 +01001971 ntfs_free(rp);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001972#endif /* NTFS_RW */
1973 /* Get the root directory inode so we can do path lookups. */
1974 vol->root_ino = ntfs_iget(sb, FILE_root);
1975 if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) {
1976 if (!IS_ERR(vol->root_ino))
1977 iput(vol->root_ino);
1978 ntfs_error(sb, "Failed to load root directory.");
1979 goto iput_logfile_err_out;
1980 }
1981#ifdef NTFS_RW
1982 /*
1983 * Check if Windows is suspended to disk on the target volume. If it
1984 * is hibernated, we must not write *anything* to the disk so set
1985 * NVolErrors() without setting the dirty volume flag and mount
1986 * read-only. This will prevent read-write remounting and it will also
1987 * prevent all writes.
1988 */
1989 err = check_windows_hibernation_status(vol);
1990 if (unlikely(err)) {
1991 static const char *es1a = "Failed to determine if Windows is "
1992 "hibernated";
1993 static const char *es1b = "Windows is hibernated";
1994 static const char *es2 = ". Run chkdsk.";
1995 const char *es1;
1996
1997 es1 = err < 0 ? es1a : es1b;
1998 /* If a read-write mount, convert it to a read-only mount. */
1999 if (!(sb->s_flags & MS_RDONLY)) {
2000 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2001 ON_ERRORS_CONTINUE))) {
2002 ntfs_error(sb, "%s and neither on_errors="
2003 "continue nor on_errors="
2004 "remount-ro was specified%s",
2005 es1, es2);
2006 goto iput_root_err_out;
2007 }
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002008 sb->s_flags |= MS_RDONLY;
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01002009 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2010 } else
2011 ntfs_warning(sb, "%s. Will not be able to remount "
2012 "read-write%s", es1, es2);
2013 /* This will prevent a read-write remount. */
2014 NVolSetErrors(vol);
2015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 /* If (still) a read-write mount, mark the volume dirty. */
2017 if (!(sb->s_flags & MS_RDONLY) &&
2018 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
2019 static const char *es1 = "Failed to set dirty bit in volume "
2020 "information flags";
2021 static const char *es2 = ". Run chkdsk.";
2022
2023 /* Convert to a read-only mount. */
2024 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2025 ON_ERRORS_CONTINUE))) {
2026 ntfs_error(sb, "%s and neither on_errors=continue nor "
2027 "on_errors=remount-ro was specified%s",
2028 es1, es2);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01002029 goto iput_root_err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 }
2031 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002032 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 /*
2034 * Do not set NVolErrors() because ntfs_remount() might manage
2035 * to set the dirty flag in which case all would be well.
2036 */
2037 }
2038#if 0
2039 // TODO: Enable this code once we start modifying anything that is
2040 // different between NTFS 1.2 and 3.x...
2041 /*
2042 * If (still) a read-write mount, set the NT4 compatibility flag on
2043 * newer NTFS version volumes.
2044 */
2045 if (!(sb->s_flags & MS_RDONLY) && (vol->major_ver > 1) &&
2046 ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
2047 static const char *es1 = "Failed to set NT4 compatibility flag";
2048 static const char *es2 = ". Run chkdsk.";
2049
2050 /* Convert to a read-only mount. */
2051 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2052 ON_ERRORS_CONTINUE))) {
2053 ntfs_error(sb, "%s and neither on_errors=continue nor "
2054 "on_errors=remount-ro was specified%s",
2055 es1, es2);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01002056 goto iput_root_err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 }
2058 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002059 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 NVolSetErrors(vol);
2061 }
2062#endif
2063 /* If (still) a read-write mount, empty the logfile. */
2064 if (!(sb->s_flags & MS_RDONLY) &&
2065 !ntfs_empty_logfile(vol->logfile_ino)) {
2066 static const char *es1 = "Failed to empty $LogFile";
2067 static const char *es2 = ". Mount in Windows.";
2068
2069 /* Convert to a read-only mount. */
2070 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2071 ON_ERRORS_CONTINUE))) {
2072 ntfs_error(sb, "%s and neither on_errors=continue nor "
2073 "on_errors=remount-ro was specified%s",
2074 es1, es2);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01002075 goto iput_root_err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
2077 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002078 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 NVolSetErrors(vol);
2080 }
2081#endif /* NTFS_RW */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 /* If on NTFS versions before 3.0, we are done. */
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002083 if (unlikely(vol->major_ver < 3))
Richard Knutssonc49c3112006-09-30 23:27:12 -07002084 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 /* NTFS 3.0+ specific initialization. */
2086 /* Get the security descriptors inode. */
2087 vol->secure_ino = ntfs_iget(sb, FILE_Secure);
2088 if (IS_ERR(vol->secure_ino) || is_bad_inode(vol->secure_ino)) {
2089 if (!IS_ERR(vol->secure_ino))
2090 iput(vol->secure_ino);
2091 ntfs_error(sb, "Failed to load $Secure.");
2092 goto iput_root_err_out;
2093 }
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002094 // TODO: Initialize security.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 /* Get the extended system files' directory inode. */
2096 vol->extend_ino = ntfs_iget(sb, FILE_Extend);
2097 if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) {
2098 if (!IS_ERR(vol->extend_ino))
2099 iput(vol->extend_ino);
2100 ntfs_error(sb, "Failed to load $Extend.");
2101 goto iput_sec_err_out;
2102 }
2103#ifdef NTFS_RW
2104 /* Find the quota file, load it if present, and set it up. */
2105 if (!load_and_init_quota(vol)) {
2106 static const char *es1 = "Failed to load $Quota";
2107 static const char *es2 = ". Run chkdsk.";
2108
2109 /* If a read-write mount, convert it to a read-only mount. */
2110 if (!(sb->s_flags & MS_RDONLY)) {
2111 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2112 ON_ERRORS_CONTINUE))) {
2113 ntfs_error(sb, "%s and neither on_errors="
2114 "continue nor on_errors="
2115 "remount-ro was specified%s",
2116 es1, es2);
2117 goto iput_quota_err_out;
2118 }
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002119 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2121 } else
2122 ntfs_warning(sb, "%s. Will not be able to remount "
2123 "read-write%s", es1, es2);
2124 /* This will prevent a read-write remount. */
2125 NVolSetErrors(vol);
2126 }
2127 /* If (still) a read-write mount, mark the quotas out of date. */
2128 if (!(sb->s_flags & MS_RDONLY) &&
2129 !ntfs_mark_quotas_out_of_date(vol)) {
2130 static const char *es1 = "Failed to mark quotas out of date";
2131 static const char *es2 = ". Run chkdsk.";
2132
2133 /* Convert to a read-only mount. */
2134 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2135 ON_ERRORS_CONTINUE))) {
2136 ntfs_error(sb, "%s and neither on_errors=continue nor "
2137 "on_errors=remount-ro was specified%s",
2138 es1, es2);
2139 goto iput_quota_err_out;
2140 }
2141 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002142 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 NVolSetErrors(vol);
2144 }
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002145 /*
2146 * Find the transaction log file ($UsnJrnl), load it if present, check
2147 * it, and set it up.
2148 */
2149 if (!load_and_init_usnjrnl(vol)) {
2150 static const char *es1 = "Failed to load $UsnJrnl";
2151 static const char *es2 = ". Run chkdsk.";
2152
2153 /* If a read-write mount, convert it to a read-only mount. */
2154 if (!(sb->s_flags & MS_RDONLY)) {
2155 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2156 ON_ERRORS_CONTINUE))) {
2157 ntfs_error(sb, "%s and neither on_errors="
2158 "continue nor on_errors="
2159 "remount-ro was specified%s",
2160 es1, es2);
2161 goto iput_usnjrnl_err_out;
2162 }
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002163 sb->s_flags |= MS_RDONLY;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002164 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2165 } else
2166 ntfs_warning(sb, "%s. Will not be able to remount "
2167 "read-write%s", es1, es2);
2168 /* This will prevent a read-write remount. */
2169 NVolSetErrors(vol);
2170 }
2171 /* If (still) a read-write mount, stamp the transaction log. */
2172 if (!(sb->s_flags & MS_RDONLY) && !ntfs_stamp_usnjrnl(vol)) {
2173 static const char *es1 = "Failed to stamp transaction log "
2174 "($UsnJrnl)";
2175 static const char *es2 = ". Run chkdsk.";
2176
2177 /* Convert to a read-only mount. */
2178 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2179 ON_ERRORS_CONTINUE))) {
2180 ntfs_error(sb, "%s and neither on_errors=continue nor "
2181 "on_errors=remount-ro was specified%s",
2182 es1, es2);
2183 goto iput_usnjrnl_err_out;
2184 }
2185 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002186 sb->s_flags |= MS_RDONLY;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002187 NVolSetErrors(vol);
2188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189#endif /* NTFS_RW */
Richard Knutssonc49c3112006-09-30 23:27:12 -07002190 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191#ifdef NTFS_RW
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002192iput_usnjrnl_err_out:
2193 if (vol->usnjrnl_j_ino)
2194 iput(vol->usnjrnl_j_ino);
2195 if (vol->usnjrnl_max_ino)
2196 iput(vol->usnjrnl_max_ino);
2197 if (vol->usnjrnl_ino)
2198 iput(vol->usnjrnl_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199iput_quota_err_out:
2200 if (vol->quota_q_ino)
2201 iput(vol->quota_q_ino);
2202 if (vol->quota_ino)
2203 iput(vol->quota_ino);
2204 iput(vol->extend_ino);
2205#endif /* NTFS_RW */
2206iput_sec_err_out:
2207 iput(vol->secure_ino);
2208iput_root_err_out:
2209 iput(vol->root_ino);
2210iput_logfile_err_out:
2211#ifdef NTFS_RW
2212 if (vol->logfile_ino)
2213 iput(vol->logfile_ino);
2214iput_vol_err_out:
2215#endif /* NTFS_RW */
2216 iput(vol->vol_ino);
2217iput_lcnbmp_err_out:
2218 iput(vol->lcnbmp_ino);
2219iput_attrdef_err_out:
2220 vol->attrdef_size = 0;
2221 if (vol->attrdef) {
2222 ntfs_free(vol->attrdef);
2223 vol->attrdef = NULL;
2224 }
2225#ifdef NTFS_RW
2226iput_upcase_err_out:
2227#endif /* NTFS_RW */
2228 vol->upcase_len = 0;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002229 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 if (vol->upcase == default_upcase) {
2231 ntfs_nr_upcase_users--;
2232 vol->upcase = NULL;
2233 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002234 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 if (vol->upcase) {
2236 ntfs_free(vol->upcase);
2237 vol->upcase = NULL;
2238 }
2239iput_mftbmp_err_out:
2240 iput(vol->mftbmp_ino);
2241iput_mirr_err_out:
2242#ifdef NTFS_RW
2243 if (vol->mftmirr_ino)
2244 iput(vol->mftmirr_ino);
2245#endif /* NTFS_RW */
Richard Knutssonc49c3112006-09-30 23:27:12 -07002246 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247}
2248
2249/**
2250 * ntfs_put_super - called by the vfs to unmount a volume
2251 * @sb: vfs superblock of volume to unmount
2252 *
2253 * ntfs_put_super() is called by the VFS (from fs/super.c::do_umount()) when
2254 * the volume is being unmounted (umount system call has been invoked) and it
2255 * releases all inodes and memory belonging to the NTFS specific part of the
2256 * super block.
2257 */
2258static void ntfs_put_super(struct super_block *sb)
2259{
2260 ntfs_volume *vol = NTFS_SB(sb);
2261
2262 ntfs_debug("Entering.");
Christoph Hellwig6cfd0142009-05-05 15:40:36 +02002263
2264 lock_kernel();
2265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266#ifdef NTFS_RW
2267 /*
2268 * Commit all inodes while they are still open in case some of them
2269 * cause others to be dirtied.
2270 */
2271 ntfs_commit_inode(vol->vol_ino);
2272
2273 /* NTFS 3.0+ specific. */
2274 if (vol->major_ver >= 3) {
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002275 if (vol->usnjrnl_j_ino)
2276 ntfs_commit_inode(vol->usnjrnl_j_ino);
2277 if (vol->usnjrnl_max_ino)
2278 ntfs_commit_inode(vol->usnjrnl_max_ino);
2279 if (vol->usnjrnl_ino)
2280 ntfs_commit_inode(vol->usnjrnl_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (vol->quota_q_ino)
2282 ntfs_commit_inode(vol->quota_q_ino);
2283 if (vol->quota_ino)
2284 ntfs_commit_inode(vol->quota_ino);
2285 if (vol->extend_ino)
2286 ntfs_commit_inode(vol->extend_ino);
2287 if (vol->secure_ino)
2288 ntfs_commit_inode(vol->secure_ino);
2289 }
2290
2291 ntfs_commit_inode(vol->root_ino);
2292
2293 down_write(&vol->lcnbmp_lock);
2294 ntfs_commit_inode(vol->lcnbmp_ino);
2295 up_write(&vol->lcnbmp_lock);
2296
2297 down_write(&vol->mftbmp_lock);
2298 ntfs_commit_inode(vol->mftbmp_ino);
2299 up_write(&vol->mftbmp_lock);
2300
2301 if (vol->logfile_ino)
2302 ntfs_commit_inode(vol->logfile_ino);
2303
2304 if (vol->mftmirr_ino)
2305 ntfs_commit_inode(vol->mftmirr_ino);
2306 ntfs_commit_inode(vol->mft_ino);
2307
2308 /*
2309 * If a read-write mount and no volume errors have occured, mark the
2310 * volume clean. Also, re-commit all affected inodes.
2311 */
2312 if (!(sb->s_flags & MS_RDONLY)) {
2313 if (!NVolErrors(vol)) {
2314 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
2315 ntfs_warning(sb, "Failed to clear dirty bit "
2316 "in volume information "
2317 "flags. Run chkdsk.");
2318 ntfs_commit_inode(vol->vol_ino);
2319 ntfs_commit_inode(vol->root_ino);
2320 if (vol->mftmirr_ino)
2321 ntfs_commit_inode(vol->mftmirr_ino);
2322 ntfs_commit_inode(vol->mft_ino);
2323 } else {
2324 ntfs_warning(sb, "Volume has errors. Leaving volume "
2325 "marked dirty. Run chkdsk.");
2326 }
2327 }
2328#endif /* NTFS_RW */
2329
2330 iput(vol->vol_ino);
2331 vol->vol_ino = NULL;
2332
2333 /* NTFS 3.0+ specific clean up. */
2334 if (vol->major_ver >= 3) {
2335#ifdef NTFS_RW
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002336 if (vol->usnjrnl_j_ino) {
2337 iput(vol->usnjrnl_j_ino);
2338 vol->usnjrnl_j_ino = NULL;
2339 }
2340 if (vol->usnjrnl_max_ino) {
2341 iput(vol->usnjrnl_max_ino);
2342 vol->usnjrnl_max_ino = NULL;
2343 }
2344 if (vol->usnjrnl_ino) {
2345 iput(vol->usnjrnl_ino);
2346 vol->usnjrnl_ino = NULL;
2347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 if (vol->quota_q_ino) {
2349 iput(vol->quota_q_ino);
2350 vol->quota_q_ino = NULL;
2351 }
2352 if (vol->quota_ino) {
2353 iput(vol->quota_ino);
2354 vol->quota_ino = NULL;
2355 }
2356#endif /* NTFS_RW */
2357 if (vol->extend_ino) {
2358 iput(vol->extend_ino);
2359 vol->extend_ino = NULL;
2360 }
2361 if (vol->secure_ino) {
2362 iput(vol->secure_ino);
2363 vol->secure_ino = NULL;
2364 }
2365 }
2366
2367 iput(vol->root_ino);
2368 vol->root_ino = NULL;
2369
2370 down_write(&vol->lcnbmp_lock);
2371 iput(vol->lcnbmp_ino);
2372 vol->lcnbmp_ino = NULL;
2373 up_write(&vol->lcnbmp_lock);
2374
2375 down_write(&vol->mftbmp_lock);
2376 iput(vol->mftbmp_ino);
2377 vol->mftbmp_ino = NULL;
2378 up_write(&vol->mftbmp_lock);
2379
2380#ifdef NTFS_RW
2381 if (vol->logfile_ino) {
2382 iput(vol->logfile_ino);
2383 vol->logfile_ino = NULL;
2384 }
2385 if (vol->mftmirr_ino) {
2386 /* Re-commit the mft mirror and mft just in case. */
2387 ntfs_commit_inode(vol->mftmirr_ino);
2388 ntfs_commit_inode(vol->mft_ino);
2389 iput(vol->mftmirr_ino);
2390 vol->mftmirr_ino = NULL;
2391 }
2392 /*
Jens Axboe13205fb2009-05-25 09:30:45 +02002393 * We should have no dirty inodes left, due to
2394 * mft.c::ntfs_mft_writepage() cleaning all the dirty pages as
2395 * the underlying mft records are written out and cleaned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 */
2397 ntfs_commit_inode(vol->mft_ino);
2398 write_inode_now(vol->mft_ino, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399#endif /* NTFS_RW */
2400
2401 iput(vol->mft_ino);
2402 vol->mft_ino = NULL;
2403
2404 /* Throw away the table of attribute definitions. */
2405 vol->attrdef_size = 0;
2406 if (vol->attrdef) {
2407 ntfs_free(vol->attrdef);
2408 vol->attrdef = NULL;
2409 }
2410 vol->upcase_len = 0;
2411 /*
2412 * Destroy the global default upcase table if necessary. Also decrease
2413 * the number of upcase users if we are a user.
2414 */
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002415 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 if (vol->upcase == default_upcase) {
2417 ntfs_nr_upcase_users--;
2418 vol->upcase = NULL;
2419 }
2420 if (!ntfs_nr_upcase_users && default_upcase) {
2421 ntfs_free(default_upcase);
2422 default_upcase = NULL;
2423 }
2424 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
2425 free_compression_buffers();
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002426 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (vol->upcase) {
2428 ntfs_free(vol->upcase);
2429 vol->upcase = NULL;
2430 }
Thomas Gleixner6d729e42009-08-16 21:05:08 +00002431
2432 unload_nls(vol->nls_map);
2433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 struct page *page;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002464 pgoff_t index, max_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
2466 ntfs_debug("Entering.");
2467 /* Serialize accesses to the cluster bitmap. */
2468 down_read(&vol->lcnbmp_lock);
2469 /*
2470 * Convert the number of bits into bytes rounded up, then convert into
2471 * multiples of PAGE_CACHE_SIZE, rounding up so that if we have one
2472 * full and one partial page max_index = 2.
2473 */
2474 max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >>
2475 PAGE_CACHE_SHIFT;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002476 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2477 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.",
2478 max_index, PAGE_CACHE_SIZE / 4);
2479 for (index = 0; index < max_index; index++) {
Akinobu Mitac4af9642010-03-15 00:35:00 -04002480 unsigned long *kaddr;
2481
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 /*
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 }
Akinobu Mitac4af9642010-03-15 00:35:00 -04002494 kaddr = kmap_atomic(page, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 /*
Akinobu Mitac4af9642010-03-15 00:35:00 -04002496 * Subtract the number of set bits. If this
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 * 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 */
Akinobu Mitac4af9642010-03-15 00:35:00 -04002502 nr_free -= bitmap_weight(kaddr,
2503 PAGE_CACHE_SIZE * BITS_PER_BYTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 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 struct address_space *mapping = vol->mftbmp_ino->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 struct page *page;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002544 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
2546 ntfs_debug("Entering.");
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002547 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002549 "0x%lx.", max_index, PAGE_CACHE_SIZE / 4);
2550 for (index = 0; index < max_index; index++) {
Akinobu Mitac4af9642010-03-15 00:35:00 -04002551 unsigned long *kaddr;
2552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 /*
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 }
Akinobu Mitac4af9642010-03-15 00:35:00 -04002565 kaddr = kmap_atomic(page, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 /*
Akinobu Mitac4af9642010-03-15 00:35:00 -04002567 * Subtract the number of set bits. If this
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 * 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 */
Akinobu Mitac4af9642010-03-15 00:35:00 -04002573 nr_free -= bitmap_weight(kaddr,
2574 PAGE_CACHE_SIZE * BITS_PER_BYTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 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
Christoph Hellwiga9185b42010-03-05 09:21:37 +01002667#ifdef NTFS_RW
2668static int ntfs_write_inode(struct inode *vi, struct writeback_control *wbc)
2669{
2670 return __ntfs_write_inode(vi, wbc->sync_mode == WB_SYNC_ALL);
2671}
2672#endif
2673
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674/**
2675 * The complete super operations.
2676 */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08002677static const struct super_operations ntfs_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 .alloc_inode = ntfs_alloc_big_inode, /* VFS: Allocate new inode. */
2679 .destroy_inode = ntfs_destroy_big_inode, /* VFS: Deallocate inode. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680#ifdef NTFS_RW
2681 //.dirty_inode = NULL, /* VFS: Called from
2682 // __mark_inode_dirty(). */
2683 .write_inode = ntfs_write_inode, /* VFS: Write dirty inode to
2684 disk. */
2685 //.drop_inode = NULL, /* VFS: Called just after the
2686 // inode reference count has
2687 // been decreased to zero.
2688 // NOTE: The inode lock is
2689 // held. See fs/inode.c::
2690 // generic_drop_inode(). */
2691 //.delete_inode = NULL, /* VFS: Delete inode from disk.
2692 // Called when i_count becomes
2693 // 0 and i_nlink is also 0. */
2694 //.write_super = NULL, /* Flush dirty super block to
2695 // disk. */
2696 //.sync_fs = NULL, /* ? */
2697 //.write_super_lockfs = NULL, /* ? */
2698 //.unlockfs = NULL, /* ? */
2699#endif /* NTFS_RW */
2700 .put_super = ntfs_put_super, /* Syscall: umount. */
2701 .statfs = ntfs_statfs, /* Syscall: statfs */
2702 .remount_fs = ntfs_remount, /* Syscall: mount -o remount. */
Al Virob57922d2010-06-07 14:34:48 -04002703 .evict_inode = ntfs_evict_big_inode, /* VFS: Called when an inode is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 removed from memory. */
2705 //.umount_begin = NULL, /* Forced umount. */
2706 .show_options = ntfs_show_options, /* Show mount options in
2707 proc. */
2708};
2709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710/**
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002711 * ntfs_fill_super - mount an ntfs filesystem
2712 * @sb: super block of ntfs filesystem to mount
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 * @opt: string containing the mount options
2714 * @silent: silence error output
2715 *
2716 * ntfs_fill_super() is called by the VFS to mount the device described by @sb
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002717 * with the mount otions in @data with the NTFS filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 *
2719 * If @silent is true, remain silent even if errors are detected. This is used
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002720 * during bootup, when the kernel tries to mount the root filesystem with all
2721 * registered filesystems one after the other until one succeeds. This implies
2722 * that all filesystems except the correct one will quite correctly and
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 * expectedly return an error, but nobody wants to see error messages when in
2724 * fact this is what is supposed to happen.
2725 *
2726 * NOTE: @sb->s_flags contains the mount options flags.
2727 */
2728static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2729{
2730 ntfs_volume *vol;
2731 struct buffer_head *bh;
2732 struct inode *tmp_ino;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002733 int blocksize, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Ingo Molnar59345372006-07-03 00:25:18 -07002735 /*
2736 * We do a pretty difficult piece of bootstrap by reading the
2737 * MFT (and other metadata) from disk into memory. We'll only
2738 * release this metadata during umount, so the locking patterns
2739 * observed during bootstrap do not count. So turn off the
2740 * observation of locking patterns (strictly for this context
2741 * only) while mounting NTFS. [The validator is still active
2742 * otherwise, even for this context: it will for example record
2743 * lock class registrations.]
2744 */
2745 lockdep_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 ntfs_debug("Entering.");
2747#ifndef NTFS_RW
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002748 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749#endif /* ! NTFS_RW */
2750 /* Allocate a new ntfs_volume and place it in sb->s_fs_info. */
2751 sb->s_fs_info = kmalloc(sizeof(ntfs_volume), GFP_NOFS);
2752 vol = NTFS_SB(sb);
2753 if (!vol) {
2754 if (!silent)
2755 ntfs_error(sb, "Allocation of NTFS volume structure "
2756 "failed. Aborting mount...");
Ingo Molnar59345372006-07-03 00:25:18 -07002757 lockdep_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 return -ENOMEM;
2759 }
2760 /* Initialize ntfs_volume structure. */
Anton Altaparmakov442d2072005-05-27 16:42:56 +01002761 *vol = (ntfs_volume) {
2762 .sb = sb,
2763 /*
2764 * Default is group and other don't have any access to files or
2765 * directories while owner has full access. Further, files by
2766 * default are not executable but directories are of course
2767 * browseable.
2768 */
2769 .fmask = 0177,
2770 .dmask = 0077,
2771 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 init_rwsem(&vol->mftbmp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 init_rwsem(&vol->lcnbmp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
2775 unlock_kernel();
2776
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002777 /* By default, enable sparse support. */
2778 NVolSetSparseEnabled(vol);
2779
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 /* Important to get the mount options dealt with now. */
2781 if (!parse_options(vol, (char*)opt))
2782 goto err_out_now;
2783
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002784 /* We support sector sizes up to the PAGE_CACHE_SIZE. */
Martin K. Petersene1defc42009-05-22 17:17:49 -04002785 if (bdev_logical_block_size(sb->s_bdev) > PAGE_CACHE_SIZE) {
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002786 if (!silent)
2787 ntfs_error(sb, "Device has unsupported sector size "
2788 "(%i). The maximum supported sector "
2789 "size on this architecture is %lu "
2790 "bytes.",
Martin K. Petersene1defc42009-05-22 17:17:49 -04002791 bdev_logical_block_size(sb->s_bdev),
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002792 PAGE_CACHE_SIZE);
2793 goto err_out_now;
2794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 /*
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002796 * Setup the device access block size to NTFS_BLOCK_SIZE or the hard
2797 * sector size, whichever is bigger.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 */
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002799 blocksize = sb_min_blocksize(sb, NTFS_BLOCK_SIZE);
2800 if (blocksize < NTFS_BLOCK_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 if (!silent)
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002802 ntfs_error(sb, "Unable to set device block size.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 goto err_out_now;
2804 }
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002805 BUG_ON(blocksize != sb->s_blocksize);
2806 ntfs_debug("Set device block size to %i bytes (block size bits %i).",
2807 blocksize, sb->s_blocksize_bits);
2808 /* Determine the size of the device in units of block_size bytes. */
2809 if (!i_size_read(sb->s_bdev->bd_inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 if (!silent)
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002811 ntfs_error(sb, "Unable to determine device size.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 goto err_out_now;
2813 }
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002814 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002815 sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 /* Read the boot sector and return unlocked buffer head to it. */
2817 if (!(bh = read_ntfs_boot_sector(sb, silent))) {
2818 if (!silent)
2819 ntfs_error(sb, "Not an NTFS volume.");
2820 goto err_out_now;
2821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 /*
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002823 * Extract the data from the boot sector and setup the ntfs volume
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 * using it.
2825 */
2826 result = parse_ntfs_boot_sector(vol, (NTFS_BOOT_SECTOR*)bh->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 if (!result) {
2829 if (!silent)
2830 ntfs_error(sb, "Unsupported NTFS filesystem.");
2831 goto err_out_now;
2832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 /*
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002834 * If the boot sector indicates a sector size bigger than the current
2835 * device block size, switch the device block size to the sector size.
2836 * TODO: It may be possible to support this case even when the set
2837 * below fails, we would just be breaking up the i/o for each sector
2838 * into multiple blocks for i/o purposes but otherwise it should just
2839 * work. However it is safer to leave disabled until someone hits this
2840 * error message and then we can get them to try it without the setting
2841 * so we know for sure that it works.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 */
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002843 if (vol->sector_size > blocksize) {
2844 blocksize = sb_set_blocksize(sb, vol->sector_size);
2845 if (blocksize != vol->sector_size) {
2846 if (!silent)
2847 ntfs_error(sb, "Unable to set device block "
2848 "size to sector size (%i).",
2849 vol->sector_size);
2850 goto err_out_now;
2851 }
2852 BUG_ON(blocksize != sb->s_blocksize);
2853 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2854 sb->s_blocksize_bits;
2855 ntfs_debug("Changed device block size to %i bytes (block size "
2856 "bits %i) to match volume sector size.",
2857 blocksize, sb->s_blocksize_bits);
2858 }
2859 /* Initialize the cluster and mft allocators. */
2860 ntfs_setup_allocators(vol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 /* Setup remaining fields in the super block. */
2862 sb->s_magic = NTFS_SB_MAGIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 /*
2864 * Ntfs allows 63 bits for the file size, i.e. correct would be:
2865 * sb->s_maxbytes = ~0ULL >> 1;
2866 * But the kernel uses a long as the page cache page index which on
2867 * 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel
2868 * defined to the maximum the page cache page index can cope with
2869 * without overflowing the index or to 2^63 - 1, whichever is smaller.
2870 */
2871 sb->s_maxbytes = MAX_LFS_FILESIZE;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002872 /* Ntfs measures time in 100ns intervals. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 sb->s_time_gran = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 /*
2875 * Now load the metadata required for the page cache and our address
2876 * space operations to function. We do this by setting up a specialised
2877 * read_inode method and then just calling the normal iget() to obtain
2878 * the inode for $MFT which is sufficient to allow our normal inode
2879 * operations and associated address space operations to function.
2880 */
2881 sb->s_op = &ntfs_sops;
2882 tmp_ino = new_inode(sb);
2883 if (!tmp_ino) {
2884 if (!silent)
2885 ntfs_error(sb, "Failed to load essential metadata.");
2886 goto err_out_now;
2887 }
2888 tmp_ino->i_ino = FILE_MFT;
2889 insert_inode_hash(tmp_ino);
2890 if (ntfs_read_inode_mount(tmp_ino) < 0) {
2891 if (!silent)
2892 ntfs_error(sb, "Failed to load essential metadata.");
2893 goto iput_tmp_ino_err_out_now;
2894 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002895 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 /*
2897 * The current mount is a compression user if the cluster size is
2898 * less than or equal 4kiB.
2899 */
2900 if (vol->cluster_size <= 4096 && !ntfs_nr_compression_users++) {
2901 result = allocate_compression_buffers();
2902 if (result) {
2903 ntfs_error(NULL, "Failed to allocate buffers "
2904 "for compression engine.");
2905 ntfs_nr_compression_users--;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002906 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 goto iput_tmp_ino_err_out_now;
2908 }
2909 }
2910 /*
2911 * Generate the global default upcase table if necessary. Also
2912 * temporarily increment the number of upcase users to avoid race
2913 * conditions with concurrent (u)mounts.
2914 */
2915 if (!default_upcase)
2916 default_upcase = generate_default_upcase();
2917 ntfs_nr_upcase_users++;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002918 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 /*
2920 * From now on, ignore @silent parameter. If we fail below this line,
2921 * it will be due to a corrupt fs or a system error, so we report it.
2922 */
2923 /*
2924 * Open the system files with normal access functions and complete
2925 * setting up the ntfs super block.
2926 */
2927 if (!load_system_files(vol)) {
2928 ntfs_error(sb, "Failed to load system files.");
2929 goto unl_upcase_iput_tmp_ino_err_out_now;
2930 }
2931 if ((sb->s_root = d_alloc_root(vol->root_ino))) {
2932 /* We increment i_count simulating an ntfs_iget(). */
2933 atomic_inc(&vol->root_ino->i_count);
2934 ntfs_debug("Exiting, status successful.");
2935 /* Release the default upcase if it has no users. */
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002936 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 if (!--ntfs_nr_upcase_users && default_upcase) {
2938 ntfs_free(default_upcase);
2939 default_upcase = NULL;
2940 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002941 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 sb->s_export_op = &ntfs_export_ops;
2943 lock_kernel();
Ingo Molnar59345372006-07-03 00:25:18 -07002944 lockdep_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return 0;
2946 }
2947 ntfs_error(sb, "Failed to allocate root directory.");
2948 /* Clean up after the successful load_system_files() call from above. */
2949 // TODO: Use ntfs_put_super() instead of repeating all this code...
2950 // FIXME: Should mark the volume clean as the error is most likely
2951 // -ENOMEM.
2952 iput(vol->vol_ino);
2953 vol->vol_ino = NULL;
2954 /* NTFS 3.0+ specific clean up. */
2955 if (vol->major_ver >= 3) {
2956#ifdef NTFS_RW
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002957 if (vol->usnjrnl_j_ino) {
2958 iput(vol->usnjrnl_j_ino);
2959 vol->usnjrnl_j_ino = NULL;
2960 }
2961 if (vol->usnjrnl_max_ino) {
2962 iput(vol->usnjrnl_max_ino);
2963 vol->usnjrnl_max_ino = NULL;
2964 }
2965 if (vol->usnjrnl_ino) {
2966 iput(vol->usnjrnl_ino);
2967 vol->usnjrnl_ino = NULL;
2968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 if (vol->quota_q_ino) {
2970 iput(vol->quota_q_ino);
2971 vol->quota_q_ino = NULL;
2972 }
2973 if (vol->quota_ino) {
2974 iput(vol->quota_ino);
2975 vol->quota_ino = NULL;
2976 }
2977#endif /* NTFS_RW */
2978 if (vol->extend_ino) {
2979 iput(vol->extend_ino);
2980 vol->extend_ino = NULL;
2981 }
2982 if (vol->secure_ino) {
2983 iput(vol->secure_ino);
2984 vol->secure_ino = NULL;
2985 }
2986 }
2987 iput(vol->root_ino);
2988 vol->root_ino = NULL;
2989 iput(vol->lcnbmp_ino);
2990 vol->lcnbmp_ino = NULL;
2991 iput(vol->mftbmp_ino);
2992 vol->mftbmp_ino = NULL;
2993#ifdef NTFS_RW
2994 if (vol->logfile_ino) {
2995 iput(vol->logfile_ino);
2996 vol->logfile_ino = NULL;
2997 }
2998 if (vol->mftmirr_ino) {
2999 iput(vol->mftmirr_ino);
3000 vol->mftmirr_ino = NULL;
3001 }
3002#endif /* NTFS_RW */
3003 /* Throw away the table of attribute definitions. */
3004 vol->attrdef_size = 0;
3005 if (vol->attrdef) {
3006 ntfs_free(vol->attrdef);
3007 vol->attrdef = NULL;
3008 }
3009 vol->upcase_len = 0;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00003010 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 if (vol->upcase == default_upcase) {
3012 ntfs_nr_upcase_users--;
3013 vol->upcase = NULL;
3014 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00003015 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 if (vol->upcase) {
3017 ntfs_free(vol->upcase);
3018 vol->upcase = NULL;
3019 }
3020 if (vol->nls_map) {
3021 unload_nls(vol->nls_map);
3022 vol->nls_map = NULL;
3023 }
3024 /* Error exit code path. */
3025unl_upcase_iput_tmp_ino_err_out_now:
3026 /*
3027 * Decrease the number of upcase users and destroy the global default
3028 * upcase table if necessary.
3029 */
Ingo Molnar4e5e5292006-03-23 16:57:48 +00003030 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 if (!--ntfs_nr_upcase_users && default_upcase) {
3032 ntfs_free(default_upcase);
3033 default_upcase = NULL;
3034 }
3035 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
3036 free_compression_buffers();
Ingo Molnar4e5e5292006-03-23 16:57:48 +00003037 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038iput_tmp_ino_err_out_now:
3039 iput(tmp_ino);
3040 if (vol->mft_ino && vol->mft_ino != tmp_ino)
3041 iput(vol->mft_ino);
3042 vol->mft_ino = NULL;
3043 /*
3044 * This is needed to get ntfs_clear_extent_inode() called for each
3045 * inode we have ever called ntfs_iget()/iput() on, otherwise we A)
3046 * leak resources and B) a subsequent mount fails automatically due to
3047 * ntfs_iget() never calling down into our ntfs_read_locked_inode()
3048 * method again... FIXME: Do we need to do this twice now because of
3049 * attribute inodes? I think not, so leave as is for now... (AIA)
3050 */
3051 if (invalidate_inodes(sb)) {
3052 ntfs_error(sb, "Busy inodes left. This is most likely a NTFS "
3053 "driver bug.");
3054 /* Copied from fs/super.c. I just love this message. (-; */
3055 printk("NTFS: Busy inodes after umount. Self-destruct in 5 "
3056 "seconds. Have a nice day...\n");
3057 }
3058 /* Errors at this stage are irrelevant. */
3059err_out_now:
3060 lock_kernel();
3061 sb->s_fs_info = NULL;
3062 kfree(vol);
3063 ntfs_debug("Failed, returning -EINVAL.");
Ingo Molnar59345372006-07-03 00:25:18 -07003064 lockdep_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 return -EINVAL;
3066}
3067
3068/*
3069 * This is a slab cache to optimize allocations and deallocations of Unicode
3070 * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN
3071 * (255) Unicode characters + a terminating NULL Unicode character.
3072 */
Pekka Enberg64419d92006-02-05 21:43:57 +00003073struct kmem_cache *ntfs_name_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Anton Altaparmakov7fafb8b2005-04-06 16:09:21 +01003075/* Slab caches for efficient allocation/deallocation of inodes. */
Pekka Enberg64419d92006-02-05 21:43:57 +00003076struct kmem_cache *ntfs_inode_cache;
3077struct kmem_cache *ntfs_big_inode_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
3079/* Init once constructor for the inode slab cache. */
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003080static void ntfs_big_inode_init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
3082 ntfs_inode *ni = (ntfs_inode *)foo;
3083
Christoph Lametera35afb82007-05-16 22:10:57 -07003084 inode_init_once(VFS_I(ni));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085}
3086
3087/*
3088 * Slab caches to optimize allocations and deallocations of attribute search
3089 * contexts and index contexts, respectively.
3090 */
Pekka Enberg64419d92006-02-05 21:43:57 +00003091struct kmem_cache *ntfs_attr_ctx_cache;
3092struct kmem_cache *ntfs_index_ctx_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Anton Altaparmakove750d1c2006-03-23 17:04:12 +00003094/* Driver wide mutex. */
Ingo Molnar4e5e5292006-03-23 16:57:48 +00003095DEFINE_MUTEX(ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
David Howells454e2392006-06-23 02:02:57 -07003097static int ntfs_get_sb(struct file_system_type *fs_type,
3098 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
David Howells454e2392006-06-23 02:02:57 -07003100 return get_sb_bdev(fs_type, flags, dev_name, data, ntfs_fill_super,
3101 mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102}
3103
3104static struct file_system_type ntfs_fs_type = {
3105 .owner = THIS_MODULE,
3106 .name = "ntfs",
3107 .get_sb = ntfs_get_sb,
3108 .kill_sb = kill_block_super,
3109 .fs_flags = FS_REQUIRES_DEV,
3110};
3111
3112/* Stable names for the slab caches. */
3113static const char ntfs_index_ctx_cache_name[] = "ntfs_index_ctx_cache";
3114static const char ntfs_attr_ctx_cache_name[] = "ntfs_attr_ctx_cache";
3115static const char ntfs_name_cache_name[] = "ntfs_name_cache";
3116static const char ntfs_inode_cache_name[] = "ntfs_inode_cache";
3117static const char ntfs_big_inode_cache_name[] = "ntfs_big_inode_cache";
3118
3119static int __init init_ntfs_fs(void)
3120{
3121 int err = 0;
3122
3123 /* This may be ugly but it results in pretty output so who cares. (-8 */
3124 printk(KERN_INFO "NTFS driver " NTFS_VERSION " [Flags: R/"
3125#ifdef NTFS_RW
3126 "W"
3127#else
3128 "O"
3129#endif
3130#ifdef DEBUG
3131 " DEBUG"
3132#endif
3133#ifdef MODULE
3134 " MODULE"
3135#endif
3136 "].\n");
3137
3138 ntfs_debug("Debug messages are enabled.");
3139
3140 ntfs_index_ctx_cache = kmem_cache_create(ntfs_index_ctx_cache_name,
3141 sizeof(ntfs_index_context), 0 /* offset */,
Paul Mundt20c2df82007-07-20 10:11:58 +09003142 SLAB_HWCACHE_ALIGN, NULL /* ctor */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 if (!ntfs_index_ctx_cache) {
3144 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3145 ntfs_index_ctx_cache_name);
3146 goto ictx_err_out;
3147 }
3148 ntfs_attr_ctx_cache = kmem_cache_create(ntfs_attr_ctx_cache_name,
3149 sizeof(ntfs_attr_search_ctx), 0 /* offset */,
Paul Mundt20c2df82007-07-20 10:11:58 +09003150 SLAB_HWCACHE_ALIGN, NULL /* ctor */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 if (!ntfs_attr_ctx_cache) {
3152 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3153 ntfs_attr_ctx_cache_name);
3154 goto actx_err_out;
3155 }
3156
3157 ntfs_name_cache = kmem_cache_create(ntfs_name_cache_name,
3158 (NTFS_MAX_NAME_LEN+1) * sizeof(ntfschar), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09003159 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 if (!ntfs_name_cache) {
3161 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3162 ntfs_name_cache_name);
3163 goto name_err_out;
3164 }
3165
3166 ntfs_inode_cache = kmem_cache_create(ntfs_inode_cache_name,
3167 sizeof(ntfs_inode), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09003168 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 if (!ntfs_inode_cache) {
3170 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3171 ntfs_inode_cache_name);
3172 goto inode_err_out;
3173 }
3174
3175 ntfs_big_inode_cache = kmem_cache_create(ntfs_big_inode_cache_name,
3176 sizeof(big_ntfs_inode), 0,
Paul Jackson4b6a9312006-03-24 03:16:05 -08003177 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD,
Paul Mundt20c2df82007-07-20 10:11:58 +09003178 ntfs_big_inode_init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 if (!ntfs_big_inode_cache) {
3180 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3181 ntfs_big_inode_cache_name);
3182 goto big_inode_err_out;
3183 }
3184
3185 /* Register the ntfs sysctls. */
3186 err = ntfs_sysctl(1);
3187 if (err) {
3188 printk(KERN_CRIT "NTFS: Failed to register NTFS sysctls!\n");
3189 goto sysctl_err_out;
3190 }
3191
3192 err = register_filesystem(&ntfs_fs_type);
3193 if (!err) {
3194 ntfs_debug("NTFS driver registered successfully.");
3195 return 0; /* Success! */
3196 }
Anton Altaparmakovc002f422005-02-03 12:02:56 +00003197 printk(KERN_CRIT "NTFS: Failed to register NTFS filesystem driver!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
3199sysctl_err_out:
3200 kmem_cache_destroy(ntfs_big_inode_cache);
3201big_inode_err_out:
3202 kmem_cache_destroy(ntfs_inode_cache);
3203inode_err_out:
3204 kmem_cache_destroy(ntfs_name_cache);
3205name_err_out:
3206 kmem_cache_destroy(ntfs_attr_ctx_cache);
3207actx_err_out:
3208 kmem_cache_destroy(ntfs_index_ctx_cache);
3209ictx_err_out:
3210 if (!err) {
Anton Altaparmakovc002f422005-02-03 12:02:56 +00003211 printk(KERN_CRIT "NTFS: Aborting NTFS filesystem driver "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 "registration...\n");
3213 err = -ENOMEM;
3214 }
3215 return err;
3216}
3217
3218static void __exit exit_ntfs_fs(void)
3219{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 ntfs_debug("Unregistering NTFS driver.");
3221
3222 unregister_filesystem(&ntfs_fs_type);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003223 kmem_cache_destroy(ntfs_big_inode_cache);
3224 kmem_cache_destroy(ntfs_inode_cache);
3225 kmem_cache_destroy(ntfs_name_cache);
3226 kmem_cache_destroy(ntfs_attr_ctx_cache);
3227 kmem_cache_destroy(ntfs_index_ctx_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 /* Unregister the ntfs sysctls. */
3229 ntfs_sysctl(0);
3230}
3231
3232MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>");
Anton Altaparmakov83311912007-01-18 09:42:48 +00003233MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2007 Anton Altaparmakov");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234MODULE_VERSION(NTFS_VERSION);
3235MODULE_LICENSE("GPL");
3236#ifdef DEBUG
3237module_param(debug_msgs, bool, 0);
3238MODULE_PARM_DESC(debug_msgs, "Enable debug messages.");
3239#endif
3240
3241module_init(init_ntfs_fs)
3242module_exit(exit_ntfs_fs)