blob: 28d4e6ab6634e951499088d5c6710e4411d8a903 [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 Altaparmakov0e375792012-02-22 10:49:58 +00004 * Copyright (c) 2001-2012 Anton Altaparmakov and Tuxera Inc.
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>
Akinobu Mitac4af9642010-03-15 00:35:00 -040033#include <linux/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include "sysctl.h"
36#include "logfile.h"
37#include "quota.h"
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +010038#include "usnjrnl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "dir.h"
40#include "debug.h"
41#include "index.h"
Christoph Hellwiga9185b42010-03-05 09:21:37 +010042#include "inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "aops.h"
Anton Altaparmakovb0d23742005-04-04 16:20:14 +010044#include "layout.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "malloc.h"
46#include "ntfs.h"
47
Anton Altaparmakovc002f422005-02-03 12:02:56 +000048/* Number of mounted filesystems which have compression enabled. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static unsigned long ntfs_nr_compression_users;
50
51/* A global default upcase table and a corresponding reference count. */
52static ntfschar *default_upcase = NULL;
53static unsigned long ntfs_nr_upcase_users = 0;
54
55/* Error constants/strings used in inode.c::ntfs_show_options(). */
56typedef enum {
57 /* One of these must be present, default is ON_ERRORS_CONTINUE. */
58 ON_ERRORS_PANIC = 0x01,
59 ON_ERRORS_REMOUNT_RO = 0x02,
60 ON_ERRORS_CONTINUE = 0x04,
61 /* Optional, can be combined with any of the above. */
62 ON_ERRORS_RECOVER = 0x10,
63} ON_ERRORS_ACTIONS;
64
65const option_t on_errors_arr[] = {
66 { ON_ERRORS_PANIC, "panic" },
67 { ON_ERRORS_REMOUNT_RO, "remount-ro", },
68 { ON_ERRORS_CONTINUE, "continue", },
69 { ON_ERRORS_RECOVER, "recover" },
70 { 0, NULL }
71};
72
73/**
74 * simple_getbool -
75 *
76 * Copied from old ntfs driver (which copied from vfat driver).
77 */
Richard Knutssonc49c3112006-09-30 23:27:12 -070078static int simple_getbool(char *s, bool *setval)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 if (s) {
81 if (!strcmp(s, "1") || !strcmp(s, "yes") || !strcmp(s, "true"))
Richard Knutssonc49c3112006-09-30 23:27:12 -070082 *setval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 else if (!strcmp(s, "0") || !strcmp(s, "no") ||
84 !strcmp(s, "false"))
Richard Knutssonc49c3112006-09-30 23:27:12 -070085 *setval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 else
87 return 0;
88 } else
Richard Knutssonc49c3112006-09-30 23:27:12 -070089 *setval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return 1;
91}
92
93/**
94 * parse_options - parse the (re)mount options
95 * @vol: ntfs volume
96 * @opt: string containing the (re)mount options
97 *
98 * Parse the recognized options in @opt for the ntfs volume described by @vol.
99 */
Richard Knutssonc49c3112006-09-30 23:27:12 -0700100static bool parse_options(ntfs_volume *vol, char *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 char *p, *v, *ov;
103 static char *utf8 = "utf8";
104 int errors = 0, sloppy = 0;
105 uid_t uid = (uid_t)-1;
106 gid_t gid = (gid_t)-1;
Al Virod0c00d02011-07-26 03:20:46 -0400107 umode_t fmask = (umode_t)-1, dmask = (umode_t)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 int mft_zone_multiplier = -1, on_errors = -1;
Anton Altaparmakovc002f422005-02-03 12:02:56 +0000109 int show_sys_files = -1, case_sensitive = -1, disable_sparse = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 struct nls_table *nls_map = NULL, *old_nls;
111
112 /* I am lazy... (-8 */
113#define NTFS_GETOPT_WITH_DEFAULT(option, variable, default_value) \
114 if (!strcmp(p, option)) { \
115 if (!v || !*v) \
116 variable = default_value; \
117 else { \
118 variable = simple_strtoul(ov = v, &v, 0); \
119 if (*v) \
120 goto needs_val; \
121 } \
122 }
123#define NTFS_GETOPT(option, variable) \
124 if (!strcmp(p, option)) { \
125 if (!v || !*v) \
126 goto needs_arg; \
127 variable = simple_strtoul(ov = v, &v, 0); \
128 if (*v) \
129 goto needs_val; \
130 }
Anton Altaparmakov5d467702005-09-12 14:33:47 +0100131#define NTFS_GETOPT_OCTAL(option, variable) \
132 if (!strcmp(p, option)) { \
133 if (!v || !*v) \
134 goto needs_arg; \
135 variable = simple_strtoul(ov = v, &v, 8); \
136 if (*v) \
137 goto needs_val; \
138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define NTFS_GETOPT_BOOL(option, variable) \
140 if (!strcmp(p, option)) { \
Richard Knutssonc49c3112006-09-30 23:27:12 -0700141 bool val; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (!simple_getbool(v, &val)) \
143 goto needs_bool; \
144 variable = val; \
145 }
146#define NTFS_GETOPT_OPTIONS_ARRAY(option, variable, opt_array) \
147 if (!strcmp(p, option)) { \
148 int _i; \
149 if (!v || !*v) \
150 goto needs_arg; \
151 ov = v; \
152 if (variable == -1) \
153 variable = 0; \
154 for (_i = 0; opt_array[_i].str && *opt_array[_i].str; _i++) \
155 if (!strcmp(opt_array[_i].str, v)) { \
156 variable |= opt_array[_i].val; \
157 break; \
158 } \
159 if (!opt_array[_i].str || !*opt_array[_i].str) \
160 goto needs_val; \
161 }
162 if (!opt || !*opt)
163 goto no_mount_options;
164 ntfs_debug("Entering with mount options string: %s", opt);
165 while ((p = strsep(&opt, ","))) {
166 if ((v = strchr(p, '=')))
167 *v++ = 0;
168 NTFS_GETOPT("uid", uid)
169 else NTFS_GETOPT("gid", gid)
Anton Altaparmakov5d467702005-09-12 14:33:47 +0100170 else NTFS_GETOPT_OCTAL("umask", fmask = dmask)
171 else NTFS_GETOPT_OCTAL("fmask", fmask)
172 else NTFS_GETOPT_OCTAL("dmask", dmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 else NTFS_GETOPT("mft_zone_multiplier", mft_zone_multiplier)
Richard Knutssonc49c3112006-09-30 23:27:12 -0700174 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, true)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files)
176 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive)
Anton Altaparmakovc002f422005-02-03 12:02:56 +0000177 else NTFS_GETOPT_BOOL("disable_sparse", disable_sparse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors,
179 on_errors_arr)
180 else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes"))
181 ntfs_warning(vol->sb, "Ignoring obsolete option %s.",
182 p);
183 else if (!strcmp(p, "nls") || !strcmp(p, "iocharset")) {
184 if (!strcmp(p, "iocharset"))
185 ntfs_warning(vol->sb, "Option iocharset is "
186 "deprecated. Please use "
187 "option nls=<charsetname> in "
188 "the future.");
189 if (!v || !*v)
190 goto needs_arg;
191use_utf8:
192 old_nls = nls_map;
193 nls_map = load_nls(v);
194 if (!nls_map) {
195 if (!old_nls) {
196 ntfs_error(vol->sb, "NLS character set "
197 "%s not found.", v);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700198 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200 ntfs_error(vol->sb, "NLS character set %s not "
201 "found. Using previous one %s.",
202 v, old_nls->charset);
203 nls_map = old_nls;
204 } else /* nls_map */ {
Thomas Gleixner6d729e42009-08-16 21:05:08 +0000205 unload_nls(old_nls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207 } else if (!strcmp(p, "utf8")) {
Richard Knutssonc49c3112006-09-30 23:27:12 -0700208 bool val = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 ntfs_warning(vol->sb, "Option utf8 is no longer "
210 "supported, using option nls=utf8. Please "
211 "use option nls=utf8 in the future and "
212 "make sure utf8 is compiled either as a "
213 "module or into the kernel.");
214 if (!v || !*v)
Richard Knutssonc49c3112006-09-30 23:27:12 -0700215 val = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 else if (!simple_getbool(v, &val))
217 goto needs_bool;
218 if (val) {
219 v = utf8;
220 goto use_utf8;
221 }
222 } else {
223 ntfs_error(vol->sb, "Unrecognized mount option %s.", p);
224 if (errors < INT_MAX)
225 errors++;
226 }
227#undef NTFS_GETOPT_OPTIONS_ARRAY
228#undef NTFS_GETOPT_BOOL
229#undef NTFS_GETOPT
230#undef NTFS_GETOPT_WITH_DEFAULT
231 }
232no_mount_options:
233 if (errors && !sloppy)
Richard Knutssonc49c3112006-09-30 23:27:12 -0700234 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (sloppy)
236 ntfs_warning(vol->sb, "Sloppy option given. Ignoring "
237 "unrecognized mount option(s) and continuing.");
238 /* Keep this first! */
239 if (on_errors != -1) {
240 if (!on_errors) {
241 ntfs_error(vol->sb, "Invalid errors option argument "
242 "or bug in options parser.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700243 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245 }
246 if (nls_map) {
247 if (vol->nls_map && vol->nls_map != nls_map) {
248 ntfs_error(vol->sb, "Cannot change NLS character set "
249 "on remount.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700250 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 } /* else (!vol->nls_map) */
252 ntfs_debug("Using NLS character set %s.", nls_map->charset);
253 vol->nls_map = nls_map;
254 } else /* (!nls_map) */ {
255 if (!vol->nls_map) {
256 vol->nls_map = load_nls_default();
257 if (!vol->nls_map) {
258 ntfs_error(vol->sb, "Failed to load default "
259 "NLS character set.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700260 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262 ntfs_debug("Using default NLS character set (%s).",
263 vol->nls_map->charset);
264 }
265 }
266 if (mft_zone_multiplier != -1) {
267 if (vol->mft_zone_multiplier && vol->mft_zone_multiplier !=
268 mft_zone_multiplier) {
269 ntfs_error(vol->sb, "Cannot change mft_zone_multiplier "
270 "on remount.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700271 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273 if (mft_zone_multiplier < 1 || mft_zone_multiplier > 4) {
274 ntfs_error(vol->sb, "Invalid mft_zone_multiplier. "
275 "Using default value, i.e. 1.");
276 mft_zone_multiplier = 1;
277 }
278 vol->mft_zone_multiplier = mft_zone_multiplier;
279 }
280 if (!vol->mft_zone_multiplier)
281 vol->mft_zone_multiplier = 1;
282 if (on_errors != -1)
283 vol->on_errors = on_errors;
284 if (!vol->on_errors || vol->on_errors == ON_ERRORS_RECOVER)
285 vol->on_errors |= ON_ERRORS_CONTINUE;
286 if (uid != (uid_t)-1)
287 vol->uid = uid;
288 if (gid != (gid_t)-1)
289 vol->gid = gid;
Al Virod0c00d02011-07-26 03:20:46 -0400290 if (fmask != (umode_t)-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 vol->fmask = fmask;
Al Virod0c00d02011-07-26 03:20:46 -0400292 if (dmask != (umode_t)-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 vol->dmask = dmask;
294 if (show_sys_files != -1) {
295 if (show_sys_files)
296 NVolSetShowSystemFiles(vol);
297 else
298 NVolClearShowSystemFiles(vol);
299 }
300 if (case_sensitive != -1) {
301 if (case_sensitive)
302 NVolSetCaseSensitive(vol);
303 else
304 NVolClearCaseSensitive(vol);
305 }
Anton Altaparmakovc002f422005-02-03 12:02:56 +0000306 if (disable_sparse != -1) {
307 if (disable_sparse)
308 NVolClearSparseEnabled(vol);
309 else {
310 if (!NVolSparseEnabled(vol) &&
311 vol->major_ver && vol->major_ver < 3)
312 ntfs_warning(vol->sb, "Not enabling sparse "
313 "support due to NTFS volume "
314 "version %i.%i (need at least "
315 "version 3.0).", vol->major_ver,
316 vol->minor_ver);
317 else
318 NVolSetSparseEnabled(vol);
319 }
320 }
Richard Knutssonc49c3112006-09-30 23:27:12 -0700321 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322needs_arg:
323 ntfs_error(vol->sb, "The %s option requires an argument.", p);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700324 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325needs_bool:
326 ntfs_error(vol->sb, "The %s option requires a boolean argument.", p);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700327 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328needs_val:
329 ntfs_error(vol->sb, "Invalid %s option argument: %s", p, ov);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700330 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
333#ifdef NTFS_RW
334
335/**
336 * ntfs_write_volume_flags - write new flags to the volume information flags
337 * @vol: ntfs volume on which to modify the flags
338 * @flags: new flags value for the volume information flags
339 *
340 * Internal function. You probably want to use ntfs_{set,clear}_volume_flags()
341 * instead (see below).
342 *
343 * Replace the volume information flags on the volume @vol with the value
344 * supplied in @flags. Note, this overwrites the volume information flags, so
345 * make sure to combine the flags you want to modify with the old flags and use
346 * the result when calling ntfs_write_volume_flags().
347 *
348 * Return 0 on success and -errno on error.
349 */
350static int ntfs_write_volume_flags(ntfs_volume *vol, const VOLUME_FLAGS flags)
351{
352 ntfs_inode *ni = NTFS_I(vol->vol_ino);
353 MFT_RECORD *m;
354 VOLUME_INFORMATION *vi;
355 ntfs_attr_search_ctx *ctx;
356 int err;
357
358 ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
359 le16_to_cpu(vol->vol_flags), le16_to_cpu(flags));
360 if (vol->vol_flags == flags)
361 goto done;
362 BUG_ON(!ni);
363 m = map_mft_record(ni);
364 if (IS_ERR(m)) {
365 err = PTR_ERR(m);
366 goto err_out;
367 }
368 ctx = ntfs_attr_get_search_ctx(ni, m);
369 if (!ctx) {
370 err = -ENOMEM;
371 goto put_unm_err_out;
372 }
373 err = ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
374 ctx);
375 if (err)
376 goto put_unm_err_out;
377 vi = (VOLUME_INFORMATION*)((u8*)ctx->attr +
378 le16_to_cpu(ctx->attr->data.resident.value_offset));
379 vol->vol_flags = vi->flags = flags;
380 flush_dcache_mft_record_page(ctx->ntfs_ino);
381 mark_mft_record_dirty(ctx->ntfs_ino);
382 ntfs_attr_put_search_ctx(ctx);
383 unmap_mft_record(ni);
384done:
385 ntfs_debug("Done.");
386 return 0;
387put_unm_err_out:
388 if (ctx)
389 ntfs_attr_put_search_ctx(ctx);
390 unmap_mft_record(ni);
391err_out:
392 ntfs_error(vol->sb, "Failed with error code %i.", -err);
393 return err;
394}
395
396/**
397 * ntfs_set_volume_flags - set bits in the volume information flags
398 * @vol: ntfs volume on which to modify the flags
399 * @flags: flags to set on the volume
400 *
401 * Set the bits in @flags in the volume information flags on the volume @vol.
402 *
403 * Return 0 on success and -errno on error.
404 */
405static inline int ntfs_set_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
406{
407 flags &= VOLUME_FLAGS_MASK;
408 return ntfs_write_volume_flags(vol, vol->vol_flags | flags);
409}
410
411/**
412 * ntfs_clear_volume_flags - clear bits in the volume information flags
413 * @vol: ntfs volume on which to modify the flags
414 * @flags: flags to clear on the volume
415 *
416 * Clear the bits in @flags in the volume information flags on the volume @vol.
417 *
418 * Return 0 on success and -errno on error.
419 */
420static inline int ntfs_clear_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
421{
422 flags &= VOLUME_FLAGS_MASK;
423 flags = vol->vol_flags & cpu_to_le16(~le16_to_cpu(flags));
424 return ntfs_write_volume_flags(vol, flags);
425}
426
427#endif /* NTFS_RW */
428
429/**
430 * ntfs_remount - change the mount options of a mounted ntfs filesystem
431 * @sb: superblock of mounted ntfs filesystem
432 * @flags: remount flags
433 * @opt: remount options string
434 *
435 * Change the mount options of an already mounted ntfs filesystem.
436 *
437 * NOTE: The VFS sets the @sb->s_flags remount flags to @flags after
438 * ntfs_remount() returns successfully (i.e. returns 0). Otherwise,
439 * @sb->s_flags are not changed.
440 */
441static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
442{
443 ntfs_volume *vol = NTFS_SB(sb);
444
445 ntfs_debug("Entering with remount options string: %s", opt);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447#ifndef NTFS_RW
Christoph Hellwig24a44dc2006-01-09 20:52:14 -0800448 /* For read-only compiled driver, enforce read-only flag. */
449 *flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450#else /* NTFS_RW */
451 /*
452 * For the read-write compiled driver, if we are remounting read-write,
453 * make sure there are no volume errors and that no unsupported volume
454 * flags are set. Also, empty the logfile journal as it would become
455 * stale as soon as something is written to the volume and mark the
456 * volume dirty so that chkdsk is run if the volume is not umounted
457 * cleanly. Finally, mark the quotas out of date so Windows rescans
458 * the volume on boot and updates them.
459 *
460 * When remounting read-only, mark the volume clean if no volume errors
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300461 * have occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 */
463 if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
464 static const char *es = ". Cannot remount read-write.";
465
466 /* Remounting read-write. */
467 if (NVolErrors(vol)) {
468 ntfs_error(sb, "Volume has errors and is read-only%s",
469 es);
470 return -EROFS;
471 }
472 if (vol->vol_flags & VOLUME_IS_DIRTY) {
473 ntfs_error(sb, "Volume is dirty and read-only%s", es);
474 return -EROFS;
475 }
Anton Altaparmakov1cf3109f2006-02-24 10:48:14 +0000476 if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
477 ntfs_error(sb, "Volume has been modified by chkdsk "
478 "and is read-only%s", es);
479 return -EROFS;
480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
Anton Altaparmakov1cf3109f2006-02-24 10:48:14 +0000482 ntfs_error(sb, "Volume has unsupported flags set "
483 "(0x%x) and is read-only%s",
484 (unsigned)le16_to_cpu(vol->vol_flags),
485 es);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return -EROFS;
487 }
488 if (ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
489 ntfs_error(sb, "Failed to set dirty bit in volume "
490 "information flags%s", es);
491 return -EROFS;
492 }
493#if 0
494 // TODO: Enable this code once we start modifying anything that
495 // is different between NTFS 1.2 and 3.x...
496 /* Set NT4 compatibility flag on newer NTFS version volumes. */
497 if ((vol->major_ver > 1)) {
498 if (ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
499 ntfs_error(sb, "Failed to set NT4 "
500 "compatibility flag%s", es);
501 NVolSetErrors(vol);
502 return -EROFS;
503 }
504 }
505#endif
506 if (!ntfs_empty_logfile(vol->logfile_ino)) {
507 ntfs_error(sb, "Failed to empty journal $LogFile%s",
508 es);
509 NVolSetErrors(vol);
510 return -EROFS;
511 }
512 if (!ntfs_mark_quotas_out_of_date(vol)) {
513 ntfs_error(sb, "Failed to mark quotas out of date%s",
514 es);
515 NVolSetErrors(vol);
516 return -EROFS;
517 }
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +0100518 if (!ntfs_stamp_usnjrnl(vol)) {
519 ntfs_error(sb, "Failed to stamp transation log "
520 "($UsnJrnl)%s", es);
521 NVolSetErrors(vol);
522 return -EROFS;
523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) {
525 /* Remounting read-only. */
526 if (!NVolErrors(vol)) {
527 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
528 ntfs_warning(sb, "Failed to clear dirty bit "
529 "in volume information "
530 "flags. Run chkdsk.");
531 }
532 }
533#endif /* NTFS_RW */
534
535 // TODO: Deal with *flags.
536
Jan Blunckefdffb52010-02-24 13:25:33 +0100537 if (!parse_options(vol, opt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return -EINVAL;
Jan Blunckefdffb52010-02-24 13:25:33 +0100539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 ntfs_debug("Done.");
541 return 0;
542}
543
544/**
545 * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector
546 * @sb: Super block of the device to which @b belongs.
547 * @b: Boot sector of device @sb to check.
Richard Knutssonc49c3112006-09-30 23:27:12 -0700548 * @silent: If 'true', all output will be silenced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 *
550 * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot
Richard Knutssonc49c3112006-09-30 23:27:12 -0700551 * sector. Returns 'true' if it is valid and 'false' if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 *
553 * @sb is only needed for warning/error output, i.e. it can be NULL when silent
Richard Knutssonc49c3112006-09-30 23:27:12 -0700554 * is 'true'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 */
Richard Knutssonc49c3112006-09-30 23:27:12 -0700556static bool is_boot_sector_ntfs(const struct super_block *sb,
557 const NTFS_BOOT_SECTOR *b, const bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 /*
560 * Check that checksum == sum of u32 values from b to the checksum
Anton Altaparmakovb0d23742005-04-04 16:20:14 +0100561 * field. If checksum is zero, no checking is done. We will work when
562 * the checksum test fails, since some utilities update the boot sector
563 * ignoring the checksum which leaves the checksum out-of-date. We
564 * report a warning if this is the case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 */
Anton Altaparmakovb0d23742005-04-04 16:20:14 +0100566 if ((void*)b < (void*)&b->checksum && b->checksum && !silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 le32 *u;
568 u32 i;
569
570 for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u)
571 i += le32_to_cpup(u);
572 if (le32_to_cpu(b->checksum) != i)
Anton Altaparmakovb0d23742005-04-04 16:20:14 +0100573 ntfs_warning(sb, "Invalid boot sector checksum.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575 /* Check OEMidentifier is "NTFS " */
576 if (b->oem_id != magicNTFS)
577 goto not_ntfs;
578 /* Check bytes per sector value is between 256 and 4096. */
579 if (le16_to_cpu(b->bpb.bytes_per_sector) < 0x100 ||
580 le16_to_cpu(b->bpb.bytes_per_sector) > 0x1000)
581 goto not_ntfs;
582 /* Check sectors per cluster value is valid. */
583 switch (b->bpb.sectors_per_cluster) {
584 case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128:
585 break;
586 default:
587 goto not_ntfs;
588 }
Anton Altaparmakov7fafb8b2005-04-06 16:09:21 +0100589 /* Check the cluster size is not above the maximum (64kiB). */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) *
Anton Altaparmakov7fafb8b2005-04-06 16:09:21 +0100591 b->bpb.sectors_per_cluster > NTFS_MAX_CLUSTER_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 goto not_ntfs;
593 /* Check reserved/unused fields are really zero. */
594 if (le16_to_cpu(b->bpb.reserved_sectors) ||
595 le16_to_cpu(b->bpb.root_entries) ||
596 le16_to_cpu(b->bpb.sectors) ||
597 le16_to_cpu(b->bpb.sectors_per_fat) ||
598 le32_to_cpu(b->bpb.large_sectors) || b->bpb.fats)
599 goto not_ntfs;
600 /* Check clusters per file mft record value is valid. */
601 if ((u8)b->clusters_per_mft_record < 0xe1 ||
602 (u8)b->clusters_per_mft_record > 0xf7)
603 switch (b->clusters_per_mft_record) {
604 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
605 break;
606 default:
607 goto not_ntfs;
608 }
609 /* Check clusters per index block value is valid. */
610 if ((u8)b->clusters_per_index_record < 0xe1 ||
611 (u8)b->clusters_per_index_record > 0xf7)
612 switch (b->clusters_per_index_record) {
613 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
614 break;
615 default:
616 goto not_ntfs;
617 }
618 /*
619 * Check for valid end of sector marker. We will work without it, but
620 * many BIOSes will refuse to boot from a bootsector if the magic is
621 * incorrect, so we emit a warning.
622 */
Harvey Harrison63cd8852009-03-31 15:23:52 -0700623 if (!silent && b->end_of_sector_marker != cpu_to_le16(0xaa55))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 ntfs_warning(sb, "Invalid end of sector marker.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700625 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626not_ntfs:
Richard Knutssonc49c3112006-09-30 23:27:12 -0700627 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
630/**
631 * read_ntfs_boot_sector - read the NTFS boot sector of a device
632 * @sb: super block of device to read the boot sector from
633 * @silent: if true, suppress all output
634 *
635 * Reads the boot sector from the device and validates it. If that fails, tries
636 * to read the backup boot sector, first from the end of the device a-la NT4 and
637 * later and then from the middle of the device a-la NT3.51 and before.
638 *
639 * If a valid boot sector is found but it is not the primary boot sector, we
640 * repair the primary boot sector silently (unless the device is read-only or
641 * the primary boot sector is not accessible).
642 *
643 * NOTE: To call this function, @sb must have the fields s_dev, the ntfs super
644 * block (u.ntfs_sb), nr_blocks and the device flags (s_flags) initialized
645 * to their respective values.
646 *
647 * Return the unlocked buffer head containing the boot sector or NULL on error.
648 */
649static struct buffer_head *read_ntfs_boot_sector(struct super_block *sb,
650 const int silent)
651{
652 const char *read_err_str = "Unable to read %s boot sector.";
653 struct buffer_head *bh_primary, *bh_backup;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000654 sector_t nr_blocks = NTFS_SB(sb)->nr_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 /* Try to read primary boot sector. */
657 if ((bh_primary = sb_bread(sb, 0))) {
658 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
659 bh_primary->b_data, silent))
660 return bh_primary;
661 if (!silent)
662 ntfs_error(sb, "Primary boot sector is invalid.");
663 } else if (!silent)
664 ntfs_error(sb, read_err_str, "primary");
665 if (!(NTFS_SB(sb)->on_errors & ON_ERRORS_RECOVER)) {
666 if (bh_primary)
667 brelse(bh_primary);
668 if (!silent)
669 ntfs_error(sb, "Mount option errors=recover not used. "
670 "Aborting without trying to recover.");
671 return NULL;
672 }
673 /* Try to read NT4+ backup boot sector. */
674 if ((bh_backup = sb_bread(sb, nr_blocks - 1))) {
675 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
676 bh_backup->b_data, silent))
677 goto hotfix_primary_boot_sector;
678 brelse(bh_backup);
679 } else if (!silent)
680 ntfs_error(sb, read_err_str, "backup");
681 /* Try to read NT3.51- backup boot sector. */
682 if ((bh_backup = sb_bread(sb, nr_blocks >> 1))) {
683 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
684 bh_backup->b_data, silent))
685 goto hotfix_primary_boot_sector;
686 if (!silent)
687 ntfs_error(sb, "Could not find a valid backup boot "
688 "sector.");
689 brelse(bh_backup);
690 } else if (!silent)
691 ntfs_error(sb, read_err_str, "backup");
692 /* We failed. Cleanup and return. */
693 if (bh_primary)
694 brelse(bh_primary);
695 return NULL;
696hotfix_primary_boot_sector:
697 if (bh_primary) {
698 /*
699 * If we managed to read sector zero and the volume is not
700 * read-only, copy the found, valid backup boot sector to the
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000701 * primary boot sector. Note we only copy the actual boot
702 * sector structure, not the actual whole device sector as that
703 * may be bigger and would potentially damage the $Boot system
704 * file (FIXME: Would be nice to know if the backup boot sector
705 * on a large sector device contains the whole boot loader or
706 * just the first 512 bytes).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 */
708 if (!(sb->s_flags & MS_RDONLY)) {
709 ntfs_warning(sb, "Hot-fix: Recovering invalid primary "
710 "boot sector from backup copy.");
711 memcpy(bh_primary->b_data, bh_backup->b_data,
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000712 NTFS_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 mark_buffer_dirty(bh_primary);
714 sync_dirty_buffer(bh_primary);
715 if (buffer_uptodate(bh_primary)) {
716 brelse(bh_backup);
717 return bh_primary;
718 }
719 ntfs_error(sb, "Hot-fix: Device write error while "
720 "recovering primary boot sector.");
721 } else {
722 ntfs_warning(sb, "Hot-fix: Recovery of primary boot "
723 "sector failed: Read-only mount.");
724 }
725 brelse(bh_primary);
726 }
727 ntfs_warning(sb, "Using backup boot sector.");
728 return bh_backup;
729}
730
731/**
732 * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol
733 * @vol: volume structure to initialise with data from boot sector
734 * @b: boot sector to parse
735 *
736 * Parse the ntfs boot sector @b and store all imporant information therein in
Richard Knutssonc49c3112006-09-30 23:27:12 -0700737 * the ntfs super block @vol. Return 'true' on success and 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 */
Richard Knutssonc49c3112006-09-30 23:27:12 -0700739static bool parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 unsigned int sectors_per_cluster_bits, nr_hidden_sects;
742 int clusters_per_mft_record, clusters_per_index_record;
743 s64 ll;
744
745 vol->sector_size = le16_to_cpu(b->bpb.bytes_per_sector);
746 vol->sector_size_bits = ffs(vol->sector_size) - 1;
747 ntfs_debug("vol->sector_size = %i (0x%x)", vol->sector_size,
748 vol->sector_size);
749 ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits,
750 vol->sector_size_bits);
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000751 if (vol->sector_size < vol->sb->s_blocksize) {
752 ntfs_error(vol->sb, "Sector size (%i) is smaller than the "
753 "device block size (%lu). This is not "
754 "supported. Sorry.", vol->sector_size,
755 vol->sb->s_blocksize);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700756 return false;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster);
759 sectors_per_cluster_bits = ffs(b->bpb.sectors_per_cluster) - 1;
760 ntfs_debug("sectors_per_cluster_bits = 0x%x",
761 sectors_per_cluster_bits);
762 nr_hidden_sects = le32_to_cpu(b->bpb.hidden_sectors);
763 ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects);
764 vol->cluster_size = vol->sector_size << sectors_per_cluster_bits;
765 vol->cluster_size_mask = vol->cluster_size - 1;
766 vol->cluster_size_bits = ffs(vol->cluster_size) - 1;
767 ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size,
768 vol->cluster_size);
769 ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask);
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000770 ntfs_debug("vol->cluster_size_bits = %i", vol->cluster_size_bits);
771 if (vol->cluster_size < vol->sector_size) {
772 ntfs_error(vol->sb, "Cluster size (%i) is smaller than the "
773 "sector size (%i). This is not supported. "
774 "Sorry.", vol->cluster_size, vol->sector_size);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700775 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777 clusters_per_mft_record = b->clusters_per_mft_record;
778 ntfs_debug("clusters_per_mft_record = %i (0x%x)",
779 clusters_per_mft_record, clusters_per_mft_record);
780 if (clusters_per_mft_record > 0)
781 vol->mft_record_size = vol->cluster_size <<
782 (ffs(clusters_per_mft_record) - 1);
783 else
784 /*
785 * When mft_record_size < cluster_size, clusters_per_mft_record
786 * = -log2(mft_record_size) bytes. mft_record_size normaly is
787 * 1024 bytes, which is encoded as 0xF6 (-10 in decimal).
788 */
789 vol->mft_record_size = 1 << -clusters_per_mft_record;
790 vol->mft_record_size_mask = vol->mft_record_size - 1;
791 vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1;
792 ntfs_debug("vol->mft_record_size = %i (0x%x)", vol->mft_record_size,
793 vol->mft_record_size);
794 ntfs_debug("vol->mft_record_size_mask = 0x%x",
795 vol->mft_record_size_mask);
796 ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
797 vol->mft_record_size_bits, vol->mft_record_size_bits);
798 /*
799 * We cannot support mft record sizes above the PAGE_CACHE_SIZE since
800 * we store $MFT/$DATA, the table of mft records in the page cache.
801 */
802 if (vol->mft_record_size > PAGE_CACHE_SIZE) {
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000803 ntfs_error(vol->sb, "Mft record size (%i) exceeds the "
804 "PAGE_CACHE_SIZE on your system (%lu). "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 "This is not supported. Sorry.",
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000806 vol->mft_record_size, PAGE_CACHE_SIZE);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700807 return false;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000808 }
809 /* We cannot support mft record sizes below the sector size. */
810 if (vol->mft_record_size < vol->sector_size) {
811 ntfs_error(vol->sb, "Mft record size (%i) is smaller than the "
812 "sector size (%i). This is not supported. "
813 "Sorry.", vol->mft_record_size,
814 vol->sector_size);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700815 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817 clusters_per_index_record = b->clusters_per_index_record;
818 ntfs_debug("clusters_per_index_record = %i (0x%x)",
819 clusters_per_index_record, clusters_per_index_record);
820 if (clusters_per_index_record > 0)
821 vol->index_record_size = vol->cluster_size <<
822 (ffs(clusters_per_index_record) - 1);
823 else
824 /*
825 * When index_record_size < cluster_size,
826 * clusters_per_index_record = -log2(index_record_size) bytes.
827 * index_record_size normaly equals 4096 bytes, which is
828 * encoded as 0xF4 (-12 in decimal).
829 */
830 vol->index_record_size = 1 << -clusters_per_index_record;
831 vol->index_record_size_mask = vol->index_record_size - 1;
832 vol->index_record_size_bits = ffs(vol->index_record_size) - 1;
833 ntfs_debug("vol->index_record_size = %i (0x%x)",
834 vol->index_record_size, vol->index_record_size);
835 ntfs_debug("vol->index_record_size_mask = 0x%x",
836 vol->index_record_size_mask);
837 ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
838 vol->index_record_size_bits,
839 vol->index_record_size_bits);
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000840 /* We cannot support index record sizes below the sector size. */
841 if (vol->index_record_size < vol->sector_size) {
842 ntfs_error(vol->sb, "Index record size (%i) is smaller than "
843 "the sector size (%i). This is not "
844 "supported. Sorry.", vol->index_record_size,
845 vol->sector_size);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700846 return false;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /*
849 * Get the size of the volume in clusters and check for 64-bit-ness.
850 * Windows currently only uses 32 bits to save the clusters so we do
851 * the same as it is much faster on 32-bit CPUs.
852 */
853 ll = sle64_to_cpu(b->number_of_sectors) >> sectors_per_cluster_bits;
854 if ((u64)ll >= 1ULL << 32) {
855 ntfs_error(vol->sb, "Cannot handle 64-bit clusters. Sorry.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700856 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858 vol->nr_clusters = ll;
859 ntfs_debug("vol->nr_clusters = 0x%llx", (long long)vol->nr_clusters);
860 /*
861 * On an architecture where unsigned long is 32-bits, we restrict the
862 * volume size to 2TiB (2^41). On a 64-bit architecture, the compiler
863 * will hopefully optimize the whole check away.
864 */
865 if (sizeof(unsigned long) < 8) {
866 if ((ll << vol->cluster_size_bits) >= (1ULL << 41)) {
867 ntfs_error(vol->sb, "Volume size (%lluTiB) is too "
868 "large for this architecture. "
869 "Maximum supported is 2TiB. Sorry.",
870 (unsigned long long)ll >> (40 -
871 vol->cluster_size_bits));
Richard Knutssonc49c3112006-09-30 23:27:12 -0700872 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874 }
875 ll = sle64_to_cpu(b->mft_lcn);
876 if (ll >= vol->nr_clusters) {
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000877 ntfs_error(vol->sb, "MFT LCN (%lli, 0x%llx) is beyond end of "
878 "volume. Weird.", (unsigned long long)ll,
879 (unsigned long long)ll);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700880 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
882 vol->mft_lcn = ll;
883 ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol->mft_lcn);
884 ll = sle64_to_cpu(b->mftmirr_lcn);
885 if (ll >= vol->nr_clusters) {
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000886 ntfs_error(vol->sb, "MFTMirr LCN (%lli, 0x%llx) is beyond end "
887 "of volume. Weird.", (unsigned long long)ll,
888 (unsigned long long)ll);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700889 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891 vol->mftmirr_lcn = ll;
892 ntfs_debug("vol->mftmirr_lcn = 0x%llx", (long long)vol->mftmirr_lcn);
893#ifdef NTFS_RW
894 /*
895 * Work out the size of the mft mirror in number of mft records. If the
896 * cluster size is less than or equal to the size taken by four mft
897 * records, the mft mirror stores the first four mft records. If the
898 * cluster size is bigger than the size taken by four mft records, the
899 * mft mirror contains as many mft records as will fit into one
900 * cluster.
901 */
902 if (vol->cluster_size <= (4 << vol->mft_record_size_bits))
903 vol->mftmirr_size = 4;
904 else
905 vol->mftmirr_size = vol->cluster_size >>
906 vol->mft_record_size_bits;
907 ntfs_debug("vol->mftmirr_size = %i", vol->mftmirr_size);
908#endif /* NTFS_RW */
909 vol->serial_no = le64_to_cpu(b->volume_serial_number);
910 ntfs_debug("vol->serial_no = 0x%llx",
911 (unsigned long long)vol->serial_no);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700912 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915/**
916 * ntfs_setup_allocators - initialize the cluster and mft allocators
917 * @vol: volume structure for which to setup the allocators
918 *
919 * Setup the cluster (lcn) and mft allocators to the starting values.
920 */
921static void ntfs_setup_allocators(ntfs_volume *vol)
922{
923#ifdef NTFS_RW
924 LCN mft_zone_size, mft_lcn;
925#endif /* NTFS_RW */
926
927 ntfs_debug("vol->mft_zone_multiplier = 0x%x",
928 vol->mft_zone_multiplier);
929#ifdef NTFS_RW
930 /* Determine the size of the MFT zone. */
931 mft_zone_size = vol->nr_clusters;
932 switch (vol->mft_zone_multiplier) { /* % of volume size in clusters */
933 case 4:
934 mft_zone_size >>= 1; /* 50% */
935 break;
936 case 3:
937 mft_zone_size = (mft_zone_size +
938 (mft_zone_size >> 1)) >> 2; /* 37.5% */
939 break;
940 case 2:
941 mft_zone_size >>= 2; /* 25% */
942 break;
943 /* case 1: */
944 default:
945 mft_zone_size >>= 3; /* 12.5% */
946 break;
947 }
948 /* Setup the mft zone. */
949 vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn;
950 ntfs_debug("vol->mft_zone_pos = 0x%llx",
951 (unsigned long long)vol->mft_zone_pos);
952 /*
953 * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
954 * source) and if the actual mft_lcn is in the expected place or even
955 * further to the front of the volume, extend the mft_zone to cover the
956 * beginning of the volume as well. This is in order to protect the
957 * area reserved for the mft bitmap as well within the mft_zone itself.
958 * On non-standard volumes we do not protect it as the overhead would
959 * be higher than the speed increase we would get by doing it.
960 */
961 mft_lcn = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size;
962 if (mft_lcn * vol->cluster_size < 16 * 1024)
963 mft_lcn = (16 * 1024 + vol->cluster_size - 1) /
964 vol->cluster_size;
965 if (vol->mft_zone_start <= mft_lcn)
966 vol->mft_zone_start = 0;
967 ntfs_debug("vol->mft_zone_start = 0x%llx",
968 (unsigned long long)vol->mft_zone_start);
969 /*
970 * Need to cap the mft zone on non-standard volumes so that it does
971 * not point outside the boundaries of the volume. We do this by
972 * halving the zone size until we are inside the volume.
973 */
974 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
975 while (vol->mft_zone_end >= vol->nr_clusters) {
976 mft_zone_size >>= 1;
977 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
978 }
979 ntfs_debug("vol->mft_zone_end = 0x%llx",
980 (unsigned long long)vol->mft_zone_end);
981 /*
982 * Set the current position within each data zone to the start of the
983 * respective zone.
984 */
985 vol->data1_zone_pos = vol->mft_zone_end;
986 ntfs_debug("vol->data1_zone_pos = 0x%llx",
987 (unsigned long long)vol->data1_zone_pos);
988 vol->data2_zone_pos = 0;
989 ntfs_debug("vol->data2_zone_pos = 0x%llx",
990 (unsigned long long)vol->data2_zone_pos);
991
992 /* Set the mft data allocation position to mft record 24. */
993 vol->mft_data_pos = 24;
994 ntfs_debug("vol->mft_data_pos = 0x%llx",
995 (unsigned long long)vol->mft_data_pos);
996#endif /* NTFS_RW */
997}
998
999#ifdef NTFS_RW
1000
1001/**
1002 * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume
1003 * @vol: ntfs super block describing device whose mft mirror to load
1004 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001005 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001007static bool load_and_init_mft_mirror(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
1009 struct inode *tmp_ino;
1010 ntfs_inode *tmp_ni;
1011
1012 ntfs_debug("Entering.");
1013 /* Get mft mirror inode. */
1014 tmp_ino = ntfs_iget(vol->sb, FILE_MFTMirr);
1015 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1016 if (!IS_ERR(tmp_ino))
1017 iput(tmp_ino);
1018 /* Caller will display error message. */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001019 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021 /*
1022 * Re-initialize some specifics about $MFTMirr's inode as
1023 * ntfs_read_inode() will have set up the default ones.
1024 */
1025 /* Set uid and gid to root. */
1026 tmp_ino->i_uid = tmp_ino->i_gid = 0;
1027 /* Regular file. No access for anyone. */
1028 tmp_ino->i_mode = S_IFREG;
1029 /* No VFS initiated operations allowed for $MFTMirr. */
1030 tmp_ino->i_op = &ntfs_empty_inode_ops;
1031 tmp_ino->i_fop = &ntfs_empty_file_ops;
1032 /* Put in our special address space operations. */
1033 tmp_ino->i_mapping->a_ops = &ntfs_mst_aops;
1034 tmp_ni = NTFS_I(tmp_ino);
1035 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
1036 NInoSetMstProtected(tmp_ni);
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001037 NInoSetSparseDisabled(tmp_ni);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /*
1039 * Set up our little cheat allowing us to reuse the async read io
1040 * completion handler for directories.
1041 */
1042 tmp_ni->itype.index.block_size = vol->mft_record_size;
1043 tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits;
1044 vol->mftmirr_ino = tmp_ino;
1045 ntfs_debug("Done.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001046 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
1049/**
1050 * check_mft_mirror - compare contents of the mft mirror with the mft
1051 * @vol: ntfs super block describing device whose mft mirror to check
1052 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001053 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 *
1055 * Note, this function also results in the mft mirror runlist being completely
1056 * mapped into memory. The mft mirror write code requires this and will BUG()
1057 * should it find an unmapped runlist element.
1058 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001059static bool check_mft_mirror(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 struct super_block *sb = vol->sb;
1062 ntfs_inode *mirr_ni;
1063 struct page *mft_page, *mirr_page;
1064 u8 *kmft, *kmirr;
1065 runlist_element *rl, rl2[2];
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001066 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 int mrecs_per_page, i;
1068
1069 ntfs_debug("Entering.");
1070 /* Compare contents of $MFT and $MFTMirr. */
1071 mrecs_per_page = PAGE_CACHE_SIZE / vol->mft_record_size;
1072 BUG_ON(!mrecs_per_page);
1073 BUG_ON(!vol->mftmirr_size);
1074 mft_page = mirr_page = NULL;
1075 kmft = kmirr = NULL;
1076 index = i = 0;
1077 do {
1078 u32 bytes;
1079
1080 /* Switch pages if necessary. */
1081 if (!(i % mrecs_per_page)) {
1082 if (index) {
1083 ntfs_unmap_page(mft_page);
1084 ntfs_unmap_page(mirr_page);
1085 }
1086 /* Get the $MFT page. */
1087 mft_page = ntfs_map_page(vol->mft_ino->i_mapping,
1088 index);
1089 if (IS_ERR(mft_page)) {
1090 ntfs_error(sb, "Failed to read $MFT.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001091 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093 kmft = page_address(mft_page);
1094 /* Get the $MFTMirr page. */
1095 mirr_page = ntfs_map_page(vol->mftmirr_ino->i_mapping,
1096 index);
1097 if (IS_ERR(mirr_page)) {
1098 ntfs_error(sb, "Failed to read $MFTMirr.");
1099 goto mft_unmap_out;
1100 }
1101 kmirr = page_address(mirr_page);
1102 ++index;
1103 }
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001104 /* Do not check the record if it is not in use. */
1105 if (((MFT_RECORD*)kmft)->flags & MFT_RECORD_IN_USE) {
1106 /* Make sure the record is ok. */
1107 if (ntfs_is_baad_recordp((le32*)kmft)) {
1108 ntfs_error(sb, "Incomplete multi sector "
1109 "transfer detected in mft "
1110 "record %i.", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111mm_unmap_out:
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001112 ntfs_unmap_page(mirr_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113mft_unmap_out:
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001114 ntfs_unmap_page(mft_page);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001115 return false;
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001118 /* Do not check the mirror record if it is not in use. */
1119 if (((MFT_RECORD*)kmirr)->flags & MFT_RECORD_IN_USE) {
1120 if (ntfs_is_baad_recordp((le32*)kmirr)) {
1121 ntfs_error(sb, "Incomplete multi sector "
1122 "transfer detected in mft "
1123 "mirror record %i.", i);
1124 goto mm_unmap_out;
1125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127 /* Get the amount of data in the current record. */
1128 bytes = le32_to_cpu(((MFT_RECORD*)kmft)->bytes_in_use);
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001129 if (bytes < sizeof(MFT_RECORD_OLD) ||
1130 bytes > vol->mft_record_size ||
1131 ntfs_is_baad_recordp((le32*)kmft)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 bytes = le32_to_cpu(((MFT_RECORD*)kmirr)->bytes_in_use);
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001133 if (bytes < sizeof(MFT_RECORD_OLD) ||
1134 bytes > vol->mft_record_size ||
1135 ntfs_is_baad_recordp((le32*)kmirr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 bytes = vol->mft_record_size;
1137 }
1138 /* Compare the two records. */
1139 if (memcmp(kmft, kmirr, bytes)) {
1140 ntfs_error(sb, "$MFT and $MFTMirr (record %i) do not "
1141 "match. Run ntfsfix or chkdsk.", i);
1142 goto mm_unmap_out;
1143 }
1144 kmft += vol->mft_record_size;
1145 kmirr += vol->mft_record_size;
1146 } while (++i < vol->mftmirr_size);
1147 /* Release the last pages. */
1148 ntfs_unmap_page(mft_page);
1149 ntfs_unmap_page(mirr_page);
1150
1151 /* Construct the mft mirror runlist by hand. */
1152 rl2[0].vcn = 0;
1153 rl2[0].lcn = vol->mftmirr_lcn;
1154 rl2[0].length = (vol->mftmirr_size * vol->mft_record_size +
1155 vol->cluster_size - 1) / vol->cluster_size;
1156 rl2[1].vcn = rl2[0].length;
1157 rl2[1].lcn = LCN_ENOENT;
1158 rl2[1].length = 0;
1159 /*
1160 * Because we have just read all of the mft mirror, we know we have
1161 * mapped the full runlist for it.
1162 */
1163 mirr_ni = NTFS_I(vol->mftmirr_ino);
1164 down_read(&mirr_ni->runlist.lock);
1165 rl = mirr_ni->runlist.rl;
1166 /* Compare the two runlists. They must be identical. */
1167 i = 0;
1168 do {
1169 if (rl2[i].vcn != rl[i].vcn || rl2[i].lcn != rl[i].lcn ||
1170 rl2[i].length != rl[i].length) {
1171 ntfs_error(sb, "$MFTMirr location mismatch. "
1172 "Run chkdsk.");
1173 up_read(&mirr_ni->runlist.lock);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001174 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176 } while (rl2[i++].length);
1177 up_read(&mirr_ni->runlist.lock);
1178 ntfs_debug("Done.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001179 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
1182/**
1183 * load_and_check_logfile - load and check the logfile inode for a volume
1184 * @vol: ntfs super block describing device whose logfile to load
1185 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001186 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001188static bool load_and_check_logfile(ntfs_volume *vol,
Anton Altaparmakove7a10332005-09-08 16:12:28 +01001189 RESTART_PAGE_HEADER **rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 struct inode *tmp_ino;
1192
1193 ntfs_debug("Entering.");
1194 tmp_ino = ntfs_iget(vol->sb, FILE_LogFile);
1195 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1196 if (!IS_ERR(tmp_ino))
1197 iput(tmp_ino);
1198 /* Caller will display error message. */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001199 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
Anton Altaparmakove7a10332005-09-08 16:12:28 +01001201 if (!ntfs_check_logfile(tmp_ino, rp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 iput(tmp_ino);
1203 /* ntfs_check_logfile() will have displayed error output. */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001204 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001206 NInoSetSparseDisabled(NTFS_I(tmp_ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 vol->logfile_ino = tmp_ino;
1208 ntfs_debug("Done.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001209 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001212#define NTFS_HIBERFIL_HEADER_SIZE 4096
1213
1214/**
1215 * check_windows_hibernation_status - check if Windows is suspended on a volume
1216 * @vol: ntfs super block of device to check
1217 *
1218 * Check if Windows is hibernated on the ntfs volume @vol. This is done by
1219 * looking for the file hiberfil.sys in the root directory of the volume. If
1220 * the file is not present Windows is definitely not suspended.
1221 *
1222 * If hiberfil.sys exists and is less than 4kiB in size it means Windows is
1223 * definitely suspended (this volume is not the system volume). Caveat: on a
1224 * system with many volumes it is possible that the < 4kiB check is bogus but
1225 * for now this should do fine.
1226 *
1227 * If hiberfil.sys exists and is larger than 4kiB in size, we need to read the
1228 * hiberfil header (which is the first 4kiB). If this begins with "hibr",
1229 * Windows is definitely suspended. If it is completely full of zeroes,
1230 * Windows is definitely not hibernated. Any other case is treated as if
1231 * Windows is suspended. This caters for the above mentioned caveat of a
1232 * system with many volumes where no "hibr" magic would be present and there is
1233 * no zero header.
1234 *
1235 * Return 0 if Windows is not hibernated on the volume, >0 if Windows is
1236 * hibernated on the volume, and -errno on error.
1237 */
1238static int check_windows_hibernation_status(ntfs_volume *vol)
1239{
1240 MFT_REF mref;
1241 struct inode *vi;
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001242 struct page *page;
1243 u32 *kaddr, *kend;
1244 ntfs_name *name = NULL;
1245 int ret = 1;
Harvey Harrison63cd8852009-03-31 15:23:52 -07001246 static const ntfschar hiberfil[13] = { cpu_to_le16('h'),
1247 cpu_to_le16('i'), cpu_to_le16('b'),
1248 cpu_to_le16('e'), cpu_to_le16('r'),
1249 cpu_to_le16('f'), cpu_to_le16('i'),
1250 cpu_to_le16('l'), cpu_to_le16('.'),
1251 cpu_to_le16('s'), cpu_to_le16('y'),
1252 cpu_to_le16('s'), 0 };
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001253
1254 ntfs_debug("Entering.");
1255 /*
1256 * Find the inode number for the hibernation file by looking up the
1257 * filename hiberfil.sys in the root directory.
1258 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001259 mutex_lock(&vol->root_ino->i_mutex);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001260 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->root_ino), hiberfil, 12,
1261 &name);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001262 mutex_unlock(&vol->root_ino->i_mutex);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001263 if (IS_ERR_MREF(mref)) {
1264 ret = MREF_ERR(mref);
1265 /* If the file does not exist, Windows is not hibernated. */
1266 if (ret == -ENOENT) {
1267 ntfs_debug("hiberfil.sys not present. Windows is not "
1268 "hibernated on the volume.");
1269 return 0;
1270 }
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001271 /* A real error occurred. */
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001272 ntfs_error(vol->sb, "Failed to find inode number for "
1273 "hiberfil.sys.");
1274 return ret;
1275 }
1276 /* We do not care for the type of match that was found. */
1277 kfree(name);
1278 /* Get the inode. */
1279 vi = ntfs_iget(vol->sb, MREF(mref));
1280 if (IS_ERR(vi) || is_bad_inode(vi)) {
1281 if (!IS_ERR(vi))
1282 iput(vi);
1283 ntfs_error(vol->sb, "Failed to load hiberfil.sys.");
1284 return IS_ERR(vi) ? PTR_ERR(vi) : -EIO;
1285 }
1286 if (unlikely(i_size_read(vi) < NTFS_HIBERFIL_HEADER_SIZE)) {
1287 ntfs_debug("hiberfil.sys is smaller than 4kiB (0x%llx). "
1288 "Windows is hibernated on the volume. This "
1289 "is not the system volume.", i_size_read(vi));
1290 goto iput_out;
1291 }
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001292 page = ntfs_map_page(vi->i_mapping, 0);
1293 if (IS_ERR(page)) {
1294 ntfs_error(vol->sb, "Failed to read from hiberfil.sys.");
1295 ret = PTR_ERR(page);
1296 goto iput_out;
1297 }
1298 kaddr = (u32*)page_address(page);
Harvey Harrison63cd8852009-03-31 15:23:52 -07001299 if (*(le32*)kaddr == cpu_to_le32(0x72626968)/*'hibr'*/) {
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001300 ntfs_debug("Magic \"hibr\" found in hiberfil.sys. Windows is "
1301 "hibernated on the volume. This is the "
1302 "system volume.");
1303 goto unm_iput_out;
1304 }
1305 kend = kaddr + NTFS_HIBERFIL_HEADER_SIZE/sizeof(*kaddr);
1306 do {
1307 if (unlikely(*kaddr)) {
1308 ntfs_debug("hiberfil.sys is larger than 4kiB "
1309 "(0x%llx), does not contain the "
1310 "\"hibr\" magic, and does not have a "
1311 "zero header. Windows is hibernated "
1312 "on the volume. This is not the "
1313 "system volume.", i_size_read(vi));
1314 goto unm_iput_out;
1315 }
1316 } while (++kaddr < kend);
1317 ntfs_debug("hiberfil.sys contains a zero header. Windows is not "
1318 "hibernated on the volume. This is the system "
1319 "volume.");
1320 ret = 0;
1321unm_iput_out:
1322 ntfs_unmap_page(page);
1323iput_out:
1324 iput(vi);
1325 return ret;
1326}
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328/**
1329 * load_and_init_quota - load and setup the quota file for a volume if present
1330 * @vol: ntfs super block describing device whose quota file to load
1331 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001332 * Return 'true' on success or 'false' on error. If $Quota is not present, we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 * leave vol->quota_ino as NULL and return success.
1334 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001335static bool load_and_init_quota(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 MFT_REF mref;
1338 struct inode *tmp_ino;
1339 ntfs_name *name = NULL;
Harvey Harrison63cd8852009-03-31 15:23:52 -07001340 static const ntfschar Quota[7] = { cpu_to_le16('$'),
1341 cpu_to_le16('Q'), cpu_to_le16('u'),
1342 cpu_to_le16('o'), cpu_to_le16('t'),
1343 cpu_to_le16('a'), 0 };
1344 static ntfschar Q[3] = { cpu_to_le16('$'),
1345 cpu_to_le16('Q'), 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 ntfs_debug("Entering.");
1348 /*
1349 * Find the inode number for the quota file by looking up the filename
1350 * $Quota in the extended system files directory $Extend.
1351 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001352 mutex_lock(&vol->extend_ino->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), Quota, 6,
1354 &name);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001355 mutex_unlock(&vol->extend_ino->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (IS_ERR_MREF(mref)) {
1357 /*
1358 * If the file does not exist, quotas are disabled and have
1359 * never been enabled on this volume, just return success.
1360 */
1361 if (MREF_ERR(mref) == -ENOENT) {
1362 ntfs_debug("$Quota not present. Volume does not have "
1363 "quotas enabled.");
1364 /*
1365 * No need to try to set quotas out of date if they are
1366 * not enabled.
1367 */
1368 NVolSetQuotaOutOfDate(vol);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001369 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001371 /* A real error occurred. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 ntfs_error(vol->sb, "Failed to find inode number for $Quota.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001373 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
1375 /* We do not care for the type of match that was found. */
Jesper Juhl251c8422005-04-04 14:59:56 +01001376 kfree(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 /* Get the inode. */
1378 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1379 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1380 if (!IS_ERR(tmp_ino))
1381 iput(tmp_ino);
1382 ntfs_error(vol->sb, "Failed to load $Quota.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001383 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
1385 vol->quota_ino = tmp_ino;
1386 /* Get the $Q index allocation attribute. */
1387 tmp_ino = ntfs_index_iget(vol->quota_ino, Q, 2);
1388 if (IS_ERR(tmp_ino)) {
1389 ntfs_error(vol->sb, "Failed to load $Quota/$Q index.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001390 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392 vol->quota_q_ino = tmp_ino;
1393 ntfs_debug("Done.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001394 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
1397/**
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001398 * load_and_init_usnjrnl - load and setup the transaction log if present
1399 * @vol: ntfs super block describing device whose usnjrnl file to load
1400 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001401 * Return 'true' on success or 'false' on error.
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001402 *
1403 * If $UsnJrnl is not present or in the process of being disabled, we set
1404 * NVolUsnJrnlStamped() and return success.
1405 *
1406 * If the $UsnJrnl $DATA/$J attribute has a size equal to the lowest valid usn,
1407 * i.e. transaction logging has only just been enabled or the journal has been
1408 * stamped and nothing has been logged since, we also set NVolUsnJrnlStamped()
1409 * and return success.
1410 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001411static bool load_and_init_usnjrnl(ntfs_volume *vol)
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001412{
1413 MFT_REF mref;
1414 struct inode *tmp_ino;
1415 ntfs_inode *tmp_ni;
1416 struct page *page;
1417 ntfs_name *name = NULL;
1418 USN_HEADER *uh;
Harvey Harrison63cd8852009-03-31 15:23:52 -07001419 static const ntfschar UsnJrnl[9] = { cpu_to_le16('$'),
1420 cpu_to_le16('U'), cpu_to_le16('s'),
1421 cpu_to_le16('n'), cpu_to_le16('J'),
1422 cpu_to_le16('r'), cpu_to_le16('n'),
1423 cpu_to_le16('l'), 0 };
1424 static ntfschar Max[5] = { cpu_to_le16('$'),
1425 cpu_to_le16('M'), cpu_to_le16('a'),
1426 cpu_to_le16('x'), 0 };
1427 static ntfschar J[3] = { cpu_to_le16('$'),
1428 cpu_to_le16('J'), 0 };
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001429
1430 ntfs_debug("Entering.");
1431 /*
1432 * Find the inode number for the transaction log file by looking up the
1433 * filename $UsnJrnl in the extended system files directory $Extend.
1434 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001435 mutex_lock(&vol->extend_ino->i_mutex);
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001436 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), UsnJrnl, 8,
1437 &name);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001438 mutex_unlock(&vol->extend_ino->i_mutex);
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001439 if (IS_ERR_MREF(mref)) {
1440 /*
1441 * If the file does not exist, transaction logging is disabled,
1442 * just return success.
1443 */
1444 if (MREF_ERR(mref) == -ENOENT) {
1445 ntfs_debug("$UsnJrnl not present. Volume does not "
1446 "have transaction logging enabled.");
1447not_enabled:
1448 /*
1449 * No need to try to stamp the transaction log if
1450 * transaction logging is not enabled.
1451 */
1452 NVolSetUsnJrnlStamped(vol);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001453 return true;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001454 }
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001455 /* A real error occurred. */
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001456 ntfs_error(vol->sb, "Failed to find inode number for "
1457 "$UsnJrnl.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001458 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001459 }
1460 /* We do not care for the type of match that was found. */
1461 kfree(name);
1462 /* Get the inode. */
1463 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1464 if (unlikely(IS_ERR(tmp_ino) || is_bad_inode(tmp_ino))) {
1465 if (!IS_ERR(tmp_ino))
1466 iput(tmp_ino);
1467 ntfs_error(vol->sb, "Failed to load $UsnJrnl.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001468 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001469 }
1470 vol->usnjrnl_ino = tmp_ino;
1471 /*
1472 * If the transaction log is in the process of being deleted, we can
1473 * ignore it.
1474 */
1475 if (unlikely(vol->vol_flags & VOLUME_DELETE_USN_UNDERWAY)) {
1476 ntfs_debug("$UsnJrnl in the process of being disabled. "
1477 "Volume does not have transaction logging "
1478 "enabled.");
1479 goto not_enabled;
1480 }
1481 /* Get the $DATA/$Max attribute. */
1482 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, Max, 4);
1483 if (IS_ERR(tmp_ino)) {
1484 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$Max "
1485 "attribute.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001486 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001487 }
1488 vol->usnjrnl_max_ino = tmp_ino;
1489 if (unlikely(i_size_read(tmp_ino) < sizeof(USN_HEADER))) {
1490 ntfs_error(vol->sb, "Found corrupt $UsnJrnl/$DATA/$Max "
1491 "attribute (size is 0x%llx but should be at "
Anton Altaparmakovd04bd1f2005-10-24 08:41:24 +01001492 "least 0x%zx bytes).", i_size_read(tmp_ino),
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001493 sizeof(USN_HEADER));
Richard Knutssonc49c3112006-09-30 23:27:12 -07001494 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001495 }
1496 /* Get the $DATA/$J attribute. */
1497 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, J, 2);
1498 if (IS_ERR(tmp_ino)) {
1499 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$J "
1500 "attribute.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001501 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001502 }
1503 vol->usnjrnl_j_ino = tmp_ino;
1504 /* Verify $J is non-resident and sparse. */
1505 tmp_ni = NTFS_I(vol->usnjrnl_j_ino);
1506 if (unlikely(!NInoNonResident(tmp_ni) || !NInoSparse(tmp_ni))) {
1507 ntfs_error(vol->sb, "$UsnJrnl/$DATA/$J attribute is resident "
1508 "and/or not sparse.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001509 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001510 }
1511 /* Read the USN_HEADER from $DATA/$Max. */
1512 page = ntfs_map_page(vol->usnjrnl_max_ino->i_mapping, 0);
1513 if (IS_ERR(page)) {
1514 ntfs_error(vol->sb, "Failed to read from $UsnJrnl/$DATA/$Max "
1515 "attribute.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001516 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001517 }
1518 uh = (USN_HEADER*)page_address(page);
1519 /* Sanity check the $Max. */
1520 if (unlikely(sle64_to_cpu(uh->allocation_delta) >
1521 sle64_to_cpu(uh->maximum_size))) {
1522 ntfs_error(vol->sb, "Allocation delta (0x%llx) exceeds "
1523 "maximum size (0x%llx). $UsnJrnl is corrupt.",
1524 (long long)sle64_to_cpu(uh->allocation_delta),
1525 (long long)sle64_to_cpu(uh->maximum_size));
1526 ntfs_unmap_page(page);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001527 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001528 }
1529 /*
1530 * If the transaction log has been stamped and nothing has been written
1531 * to it since, we do not need to stamp it.
1532 */
1533 if (unlikely(sle64_to_cpu(uh->lowest_valid_usn) >=
1534 i_size_read(vol->usnjrnl_j_ino))) {
1535 if (likely(sle64_to_cpu(uh->lowest_valid_usn) ==
1536 i_size_read(vol->usnjrnl_j_ino))) {
1537 ntfs_unmap_page(page);
1538 ntfs_debug("$UsnJrnl is enabled but nothing has been "
1539 "logged since it was last stamped. "
1540 "Treating this as if the volume does "
1541 "not have transaction logging "
1542 "enabled.");
1543 goto not_enabled;
1544 }
1545 ntfs_error(vol->sb, "$UsnJrnl has lowest valid usn (0x%llx) "
1546 "which is out of bounds (0x%llx). $UsnJrnl "
1547 "is corrupt.",
1548 (long long)sle64_to_cpu(uh->lowest_valid_usn),
1549 i_size_read(vol->usnjrnl_j_ino));
1550 ntfs_unmap_page(page);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001551 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001552 }
1553 ntfs_unmap_page(page);
1554 ntfs_debug("Done.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001555 return true;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001556}
1557
1558/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 * load_and_init_attrdef - load the attribute definitions table for a volume
1560 * @vol: ntfs super block describing device whose attrdef to load
1561 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001562 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001564static bool load_and_init_attrdef(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001566 loff_t i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 struct super_block *sb = vol->sb;
1568 struct inode *ino;
1569 struct page *page;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001570 pgoff_t index, max_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 unsigned int size;
1572
1573 ntfs_debug("Entering.");
1574 /* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */
1575 ino = ntfs_iget(sb, FILE_AttrDef);
1576 if (IS_ERR(ino) || is_bad_inode(ino)) {
1577 if (!IS_ERR(ino))
1578 iput(ino);
1579 goto failed;
1580 }
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001581 NInoSetSparseDisabled(NTFS_I(ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001583 i_size = i_size_read(ino);
1584 if (i_size <= 0 || i_size > 0x7fffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 goto iput_failed;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001586 vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (!vol->attrdef)
1588 goto iput_failed;
1589 index = 0;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001590 max_index = i_size >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 size = PAGE_CACHE_SIZE;
1592 while (index < max_index) {
1593 /* Read the attrdef table and copy it into the linear buffer. */
1594read_partial_attrdef_page:
1595 page = ntfs_map_page(ino->i_mapping, index);
1596 if (IS_ERR(page))
1597 goto free_iput_failed;
1598 memcpy((u8*)vol->attrdef + (index++ << PAGE_CACHE_SHIFT),
1599 page_address(page), size);
1600 ntfs_unmap_page(page);
1601 };
1602 if (size == PAGE_CACHE_SIZE) {
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001603 size = i_size & ~PAGE_CACHE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (size)
1605 goto read_partial_attrdef_page;
1606 }
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001607 vol->attrdef_size = i_size;
1608 ntfs_debug("Read %llu bytes from $AttrDef.", i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 iput(ino);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001610 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611free_iput_failed:
1612 ntfs_free(vol->attrdef);
1613 vol->attrdef = NULL;
1614iput_failed:
1615 iput(ino);
1616failed:
1617 ntfs_error(sb, "Failed to initialize attribute definition table.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001618 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
1621#endif /* NTFS_RW */
1622
1623/**
1624 * load_and_init_upcase - load the upcase table for an ntfs volume
1625 * @vol: ntfs super block describing device whose upcase to load
1626 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001627 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001629static bool load_and_init_upcase(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001631 loff_t i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 struct super_block *sb = vol->sb;
1633 struct inode *ino;
1634 struct page *page;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001635 pgoff_t index, max_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 unsigned int size;
1637 int i, max;
1638
1639 ntfs_debug("Entering.");
1640 /* Read upcase table and setup vol->upcase and vol->upcase_len. */
1641 ino = ntfs_iget(sb, FILE_UpCase);
1642 if (IS_ERR(ino) || is_bad_inode(ino)) {
1643 if (!IS_ERR(ino))
1644 iput(ino);
1645 goto upcase_failed;
1646 }
1647 /*
1648 * The upcase size must not be above 64k Unicode characters, must not
1649 * be zero and must be a multiple of sizeof(ntfschar).
1650 */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001651 i_size = i_size_read(ino);
1652 if (!i_size || i_size & (sizeof(ntfschar) - 1) ||
1653 i_size > 64ULL * 1024 * sizeof(ntfschar))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 goto iput_upcase_failed;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001655 vol->upcase = (ntfschar*)ntfs_malloc_nofs(i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (!vol->upcase)
1657 goto iput_upcase_failed;
1658 index = 0;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001659 max_index = i_size >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 size = PAGE_CACHE_SIZE;
1661 while (index < max_index) {
1662 /* Read the upcase table and copy it into the linear buffer. */
1663read_partial_upcase_page:
1664 page = ntfs_map_page(ino->i_mapping, index);
1665 if (IS_ERR(page))
1666 goto iput_upcase_failed;
1667 memcpy((char*)vol->upcase + (index++ << PAGE_CACHE_SHIFT),
1668 page_address(page), size);
1669 ntfs_unmap_page(page);
1670 };
1671 if (size == PAGE_CACHE_SIZE) {
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001672 size = i_size & ~PAGE_CACHE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (size)
1674 goto read_partial_upcase_page;
1675 }
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001676 vol->upcase_len = i_size >> UCHAR_T_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001678 i_size, 64 * 1024 * sizeof(ntfschar));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 iput(ino);
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001680 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 if (!default_upcase) {
1682 ntfs_debug("Using volume specified $UpCase since default is "
1683 "not present.");
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001684 mutex_unlock(&ntfs_lock);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001685 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
1687 max = default_upcase_len;
1688 if (max > vol->upcase_len)
1689 max = vol->upcase_len;
1690 for (i = 0; i < max; i++)
1691 if (vol->upcase[i] != default_upcase[i])
1692 break;
1693 if (i == max) {
1694 ntfs_free(vol->upcase);
1695 vol->upcase = default_upcase;
1696 vol->upcase_len = max;
1697 ntfs_nr_upcase_users++;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001698 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 ntfs_debug("Volume specified $UpCase matches default. Using "
1700 "default.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001701 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001703 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 ntfs_debug("Using volume specified $UpCase since it does not match "
1705 "the default.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001706 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707iput_upcase_failed:
1708 iput(ino);
1709 ntfs_free(vol->upcase);
1710 vol->upcase = NULL;
1711upcase_failed:
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001712 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (default_upcase) {
1714 vol->upcase = default_upcase;
1715 vol->upcase_len = default_upcase_len;
1716 ntfs_nr_upcase_users++;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001717 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 ntfs_error(sb, "Failed to load $UpCase from the volume. Using "
1719 "default.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001720 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001722 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 ntfs_error(sb, "Failed to initialize upcase table.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001724 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
Ingo Molnar59345372006-07-03 00:25:18 -07001727/*
1728 * The lcn and mft bitmap inodes are NTFS-internal inodes with
1729 * their own special locking rules:
1730 */
1731static struct lock_class_key
1732 lcnbmp_runlist_lock_key, lcnbmp_mrec_lock_key,
1733 mftbmp_runlist_lock_key, mftbmp_mrec_lock_key;
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735/**
1736 * load_system_files - open the system files using normal functions
1737 * @vol: ntfs super block describing device whose system files to load
1738 *
1739 * Open the system files with normal access functions and complete setting up
1740 * the ntfs super block @vol.
1741 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001742 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001744static bool load_system_files(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
1746 struct super_block *sb = vol->sb;
1747 MFT_RECORD *m;
1748 VOLUME_INFORMATION *vi;
1749 ntfs_attr_search_ctx *ctx;
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001750#ifdef NTFS_RW
Anton Altaparmakov7d333d62005-09-08 23:01:16 +01001751 RESTART_PAGE_HEADER *rp;
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001752 int err;
1753#endif /* NTFS_RW */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755 ntfs_debug("Entering.");
1756#ifdef NTFS_RW
1757 /* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */
1758 if (!load_and_init_mft_mirror(vol) || !check_mft_mirror(vol)) {
1759 static const char *es1 = "Failed to load $MFTMirr";
1760 static const char *es2 = "$MFTMirr does not match $MFT";
1761 static const char *es3 = ". Run ntfsfix and/or chkdsk.";
1762
1763 /* If a read-write mount, convert it to a read-only mount. */
1764 if (!(sb->s_flags & MS_RDONLY)) {
1765 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1766 ON_ERRORS_CONTINUE))) {
1767 ntfs_error(sb, "%s and neither on_errors="
1768 "continue nor on_errors="
1769 "remount-ro was specified%s",
1770 !vol->mftmirr_ino ? es1 : es2,
1771 es3);
1772 goto iput_mirr_err_out;
1773 }
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08001774 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 ntfs_error(sb, "%s. Mounting read-only%s",
1776 !vol->mftmirr_ino ? es1 : es2, es3);
1777 } else
1778 ntfs_warning(sb, "%s. Will not be able to remount "
1779 "read-write%s",
1780 !vol->mftmirr_ino ? es1 : es2, es3);
1781 /* This will prevent a read-write remount. */
1782 NVolSetErrors(vol);
1783 }
1784#endif /* NTFS_RW */
1785 /* Get mft bitmap attribute inode. */
1786 vol->mftbmp_ino = ntfs_attr_iget(vol->mft_ino, AT_BITMAP, NULL, 0);
1787 if (IS_ERR(vol->mftbmp_ino)) {
1788 ntfs_error(sb, "Failed to load $MFT/$BITMAP attribute.");
1789 goto iput_mirr_err_out;
1790 }
Ingo Molnar59345372006-07-03 00:25:18 -07001791 lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->runlist.lock,
1792 &mftbmp_runlist_lock_key);
1793 lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->mrec_lock,
1794 &mftbmp_mrec_lock_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */
1796 if (!load_and_init_upcase(vol))
1797 goto iput_mftbmp_err_out;
1798#ifdef NTFS_RW
1799 /*
1800 * Read attribute definitions table and setup @vol->attrdef and
1801 * @vol->attrdef_size.
1802 */
1803 if (!load_and_init_attrdef(vol))
1804 goto iput_upcase_err_out;
1805#endif /* NTFS_RW */
1806 /*
1807 * Get the cluster allocation bitmap inode and verify the size, no
1808 * need for any locking at this stage as we are already running
1809 * exclusively as we are mount in progress task.
1810 */
1811 vol->lcnbmp_ino = ntfs_iget(sb, FILE_Bitmap);
1812 if (IS_ERR(vol->lcnbmp_ino) || is_bad_inode(vol->lcnbmp_ino)) {
1813 if (!IS_ERR(vol->lcnbmp_ino))
1814 iput(vol->lcnbmp_ino);
1815 goto bitmap_failed;
1816 }
Ingo Molnar59345372006-07-03 00:25:18 -07001817 lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->runlist.lock,
1818 &lcnbmp_runlist_lock_key);
1819 lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->mrec_lock,
1820 &lcnbmp_mrec_lock_key);
1821
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001822 NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino));
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001823 if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 iput(vol->lcnbmp_ino);
1825bitmap_failed:
1826 ntfs_error(sb, "Failed to load $Bitmap.");
1827 goto iput_attrdef_err_out;
1828 }
1829 /*
1830 * Get the volume inode and setup our cache of the volume flags and
1831 * version.
1832 */
1833 vol->vol_ino = ntfs_iget(sb, FILE_Volume);
1834 if (IS_ERR(vol->vol_ino) || is_bad_inode(vol->vol_ino)) {
1835 if (!IS_ERR(vol->vol_ino))
1836 iput(vol->vol_ino);
1837volume_failed:
1838 ntfs_error(sb, "Failed to load $Volume.");
1839 goto iput_lcnbmp_err_out;
1840 }
1841 m = map_mft_record(NTFS_I(vol->vol_ino));
1842 if (IS_ERR(m)) {
1843iput_volume_failed:
1844 iput(vol->vol_ino);
1845 goto volume_failed;
1846 }
1847 if (!(ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m))) {
1848 ntfs_error(sb, "Failed to get attribute search context.");
1849 goto get_ctx_vol_failed;
1850 }
1851 if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
1852 ctx) || ctx->attr->non_resident || ctx->attr->flags) {
1853err_put_vol:
1854 ntfs_attr_put_search_ctx(ctx);
1855get_ctx_vol_failed:
1856 unmap_mft_record(NTFS_I(vol->vol_ino));
1857 goto iput_volume_failed;
1858 }
1859 vi = (VOLUME_INFORMATION*)((char*)ctx->attr +
1860 le16_to_cpu(ctx->attr->data.resident.value_offset));
1861 /* Some bounds checks. */
1862 if ((u8*)vi < (u8*)ctx->attr || (u8*)vi +
1863 le32_to_cpu(ctx->attr->data.resident.value_length) >
1864 (u8*)ctx->attr + le32_to_cpu(ctx->attr->length))
1865 goto err_put_vol;
1866 /* Copy the volume flags and version to the ntfs_volume structure. */
1867 vol->vol_flags = vi->flags;
1868 vol->major_ver = vi->major_ver;
1869 vol->minor_ver = vi->minor_ver;
1870 ntfs_attr_put_search_ctx(ctx);
1871 unmap_mft_record(NTFS_I(vol->vol_ino));
1872 printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver,
1873 vol->minor_ver);
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001874 if (vol->major_ver < 3 && NVolSparseEnabled(vol)) {
1875 ntfs_warning(vol->sb, "Disabling sparse support due to NTFS "
1876 "volume version %i.%i (need at least version "
1877 "3.0).", vol->major_ver, vol->minor_ver);
1878 NVolClearSparseEnabled(vol);
1879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880#ifdef NTFS_RW
1881 /* Make sure that no unsupported volume flags are set. */
1882 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
1883 static const char *es1a = "Volume is dirty";
Anton Altaparmakov1cf3109f2006-02-24 10:48:14 +00001884 static const char *es1b = "Volume has been modified by chkdsk";
1885 static const char *es1c = "Volume has unsupported flags set";
1886 static const char *es2a = ". Run chkdsk and mount in Windows.";
1887 static const char *es2b = ". Mount in Windows.";
1888 const char *es1, *es2;
1889
1890 es2 = es2a;
1891 if (vol->vol_flags & VOLUME_IS_DIRTY)
1892 es1 = es1a;
1893 else if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
1894 es1 = es1b;
1895 es2 = es2b;
1896 } else {
1897 es1 = es1c;
1898 ntfs_warning(sb, "Unsupported volume flags 0x%x "
1899 "encountered.",
1900 (unsigned)le16_to_cpu(vol->vol_flags));
1901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 /* If a read-write mount, convert it to a read-only mount. */
1903 if (!(sb->s_flags & MS_RDONLY)) {
1904 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1905 ON_ERRORS_CONTINUE))) {
1906 ntfs_error(sb, "%s and neither on_errors="
1907 "continue nor on_errors="
1908 "remount-ro was specified%s",
1909 es1, es2);
1910 goto iput_vol_err_out;
1911 }
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08001912 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1914 } else
1915 ntfs_warning(sb, "%s. Will not be able to remount "
1916 "read-write%s", es1, es2);
1917 /*
1918 * Do not set NVolErrors() because ntfs_remount() re-checks the
1919 * flags which we need to do in case any flags have changed.
1920 */
1921 }
1922 /*
1923 * Get the inode for the logfile, check it and determine if the volume
1924 * was shutdown cleanly.
1925 */
Anton Altaparmakove7a10332005-09-08 16:12:28 +01001926 rp = NULL;
1927 if (!load_and_check_logfile(vol, &rp) ||
1928 !ntfs_is_logfile_clean(vol->logfile_ino, rp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 static const char *es1a = "Failed to load $LogFile";
1930 static const char *es1b = "$LogFile is not clean";
1931 static const char *es2 = ". Mount in Windows.";
1932 const char *es1;
1933
1934 es1 = !vol->logfile_ino ? es1a : es1b;
1935 /* If a read-write mount, convert it to a read-only mount. */
1936 if (!(sb->s_flags & MS_RDONLY)) {
1937 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1938 ON_ERRORS_CONTINUE))) {
1939 ntfs_error(sb, "%s and neither on_errors="
1940 "continue nor on_errors="
1941 "remount-ro was specified%s",
1942 es1, es2);
Anton Altaparmakove7a10332005-09-08 16:12:28 +01001943 if (vol->logfile_ino) {
1944 BUG_ON(!rp);
1945 ntfs_free(rp);
1946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 goto iput_logfile_err_out;
1948 }
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08001949 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1951 } else
1952 ntfs_warning(sb, "%s. Will not be able to remount "
1953 "read-write%s", es1, es2);
1954 /* This will prevent a read-write remount. */
1955 NVolSetErrors(vol);
1956 }
Anton Altaparmakove7a10332005-09-08 16:12:28 +01001957 ntfs_free(rp);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001958#endif /* NTFS_RW */
1959 /* Get the root directory inode so we can do path lookups. */
1960 vol->root_ino = ntfs_iget(sb, FILE_root);
1961 if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) {
1962 if (!IS_ERR(vol->root_ino))
1963 iput(vol->root_ino);
1964 ntfs_error(sb, "Failed to load root directory.");
1965 goto iput_logfile_err_out;
1966 }
1967#ifdef NTFS_RW
1968 /*
1969 * Check if Windows is suspended to disk on the target volume. If it
1970 * is hibernated, we must not write *anything* to the disk so set
1971 * NVolErrors() without setting the dirty volume flag and mount
1972 * read-only. This will prevent read-write remounting and it will also
1973 * prevent all writes.
1974 */
1975 err = check_windows_hibernation_status(vol);
1976 if (unlikely(err)) {
1977 static const char *es1a = "Failed to determine if Windows is "
1978 "hibernated";
1979 static const char *es1b = "Windows is hibernated";
1980 static const char *es2 = ". Run chkdsk.";
1981 const char *es1;
1982
1983 es1 = err < 0 ? es1a : es1b;
1984 /* If a read-write mount, convert it to a read-only mount. */
1985 if (!(sb->s_flags & MS_RDONLY)) {
1986 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1987 ON_ERRORS_CONTINUE))) {
1988 ntfs_error(sb, "%s and neither on_errors="
1989 "continue nor on_errors="
1990 "remount-ro was specified%s",
1991 es1, es2);
1992 goto iput_root_err_out;
1993 }
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08001994 sb->s_flags |= MS_RDONLY;
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001995 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1996 } else
1997 ntfs_warning(sb, "%s. Will not be able to remount "
1998 "read-write%s", es1, es2);
1999 /* This will prevent a read-write remount. */
2000 NVolSetErrors(vol);
2001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 /* If (still) a read-write mount, mark the volume dirty. */
2003 if (!(sb->s_flags & MS_RDONLY) &&
2004 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
2005 static const char *es1 = "Failed to set dirty bit in volume "
2006 "information flags";
2007 static const char *es2 = ". Run chkdsk.";
2008
2009 /* Convert to a read-only mount. */
2010 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2011 ON_ERRORS_CONTINUE))) {
2012 ntfs_error(sb, "%s and neither on_errors=continue nor "
2013 "on_errors=remount-ro was specified%s",
2014 es1, es2);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01002015 goto iput_root_err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
2017 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002018 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 /*
2020 * Do not set NVolErrors() because ntfs_remount() might manage
2021 * to set the dirty flag in which case all would be well.
2022 */
2023 }
2024#if 0
2025 // TODO: Enable this code once we start modifying anything that is
2026 // different between NTFS 1.2 and 3.x...
2027 /*
2028 * If (still) a read-write mount, set the NT4 compatibility flag on
2029 * newer NTFS version volumes.
2030 */
2031 if (!(sb->s_flags & MS_RDONLY) && (vol->major_ver > 1) &&
2032 ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
2033 static const char *es1 = "Failed to set NT4 compatibility flag";
2034 static const char *es2 = ". Run chkdsk.";
2035
2036 /* Convert to a read-only mount. */
2037 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2038 ON_ERRORS_CONTINUE))) {
2039 ntfs_error(sb, "%s and neither on_errors=continue nor "
2040 "on_errors=remount-ro was specified%s",
2041 es1, es2);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01002042 goto iput_root_err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
2044 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002045 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 NVolSetErrors(vol);
2047 }
2048#endif
2049 /* If (still) a read-write mount, empty the logfile. */
2050 if (!(sb->s_flags & MS_RDONLY) &&
2051 !ntfs_empty_logfile(vol->logfile_ino)) {
2052 static const char *es1 = "Failed to empty $LogFile";
2053 static const char *es2 = ". Mount in Windows.";
2054
2055 /* Convert to a read-only mount. */
2056 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2057 ON_ERRORS_CONTINUE))) {
2058 ntfs_error(sb, "%s and neither on_errors=continue nor "
2059 "on_errors=remount-ro was specified%s",
2060 es1, es2);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01002061 goto iput_root_err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
2063 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002064 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 NVolSetErrors(vol);
2066 }
2067#endif /* NTFS_RW */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 /* If on NTFS versions before 3.0, we are done. */
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002069 if (unlikely(vol->major_ver < 3))
Richard Knutssonc49c3112006-09-30 23:27:12 -07002070 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 /* NTFS 3.0+ specific initialization. */
2072 /* Get the security descriptors inode. */
2073 vol->secure_ino = ntfs_iget(sb, FILE_Secure);
2074 if (IS_ERR(vol->secure_ino) || is_bad_inode(vol->secure_ino)) {
2075 if (!IS_ERR(vol->secure_ino))
2076 iput(vol->secure_ino);
2077 ntfs_error(sb, "Failed to load $Secure.");
2078 goto iput_root_err_out;
2079 }
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002080 // TODO: Initialize security.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 /* Get the extended system files' directory inode. */
2082 vol->extend_ino = ntfs_iget(sb, FILE_Extend);
2083 if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) {
2084 if (!IS_ERR(vol->extend_ino))
2085 iput(vol->extend_ino);
2086 ntfs_error(sb, "Failed to load $Extend.");
2087 goto iput_sec_err_out;
2088 }
2089#ifdef NTFS_RW
2090 /* Find the quota file, load it if present, and set it up. */
2091 if (!load_and_init_quota(vol)) {
2092 static const char *es1 = "Failed to load $Quota";
2093 static const char *es2 = ". Run chkdsk.";
2094
2095 /* If a read-write mount, convert it to a read-only mount. */
2096 if (!(sb->s_flags & MS_RDONLY)) {
2097 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2098 ON_ERRORS_CONTINUE))) {
2099 ntfs_error(sb, "%s and neither on_errors="
2100 "continue nor on_errors="
2101 "remount-ro was specified%s",
2102 es1, es2);
2103 goto iput_quota_err_out;
2104 }
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002105 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2107 } else
2108 ntfs_warning(sb, "%s. Will not be able to remount "
2109 "read-write%s", es1, es2);
2110 /* This will prevent a read-write remount. */
2111 NVolSetErrors(vol);
2112 }
2113 /* If (still) a read-write mount, mark the quotas out of date. */
2114 if (!(sb->s_flags & MS_RDONLY) &&
2115 !ntfs_mark_quotas_out_of_date(vol)) {
2116 static const char *es1 = "Failed to mark quotas out of date";
2117 static const char *es2 = ". Run chkdsk.";
2118
2119 /* Convert to a read-only mount. */
2120 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2121 ON_ERRORS_CONTINUE))) {
2122 ntfs_error(sb, "%s and neither on_errors=continue nor "
2123 "on_errors=remount-ro was specified%s",
2124 es1, es2);
2125 goto iput_quota_err_out;
2126 }
2127 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002128 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 NVolSetErrors(vol);
2130 }
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002131 /*
2132 * Find the transaction log file ($UsnJrnl), load it if present, check
2133 * it, and set it up.
2134 */
2135 if (!load_and_init_usnjrnl(vol)) {
2136 static const char *es1 = "Failed to load $UsnJrnl";
2137 static const char *es2 = ". Run chkdsk.";
2138
2139 /* If a read-write mount, convert it to a read-only mount. */
2140 if (!(sb->s_flags & MS_RDONLY)) {
2141 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2142 ON_ERRORS_CONTINUE))) {
2143 ntfs_error(sb, "%s and neither on_errors="
2144 "continue nor on_errors="
2145 "remount-ro was specified%s",
2146 es1, es2);
2147 goto iput_usnjrnl_err_out;
2148 }
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002149 sb->s_flags |= MS_RDONLY;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002150 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2151 } else
2152 ntfs_warning(sb, "%s. Will not be able to remount "
2153 "read-write%s", es1, es2);
2154 /* This will prevent a read-write remount. */
2155 NVolSetErrors(vol);
2156 }
2157 /* If (still) a read-write mount, stamp the transaction log. */
2158 if (!(sb->s_flags & MS_RDONLY) && !ntfs_stamp_usnjrnl(vol)) {
2159 static const char *es1 = "Failed to stamp transaction log "
2160 "($UsnJrnl)";
2161 static const char *es2 = ". Run chkdsk.";
2162
2163 /* Convert to a read-only mount. */
2164 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2165 ON_ERRORS_CONTINUE))) {
2166 ntfs_error(sb, "%s and neither on_errors=continue nor "
2167 "on_errors=remount-ro was specified%s",
2168 es1, es2);
2169 goto iput_usnjrnl_err_out;
2170 }
2171 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002172 sb->s_flags |= MS_RDONLY;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002173 NVolSetErrors(vol);
2174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175#endif /* NTFS_RW */
Richard Knutssonc49c3112006-09-30 23:27:12 -07002176 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177#ifdef NTFS_RW
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002178iput_usnjrnl_err_out:
2179 if (vol->usnjrnl_j_ino)
2180 iput(vol->usnjrnl_j_ino);
2181 if (vol->usnjrnl_max_ino)
2182 iput(vol->usnjrnl_max_ino);
2183 if (vol->usnjrnl_ino)
2184 iput(vol->usnjrnl_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185iput_quota_err_out:
2186 if (vol->quota_q_ino)
2187 iput(vol->quota_q_ino);
2188 if (vol->quota_ino)
2189 iput(vol->quota_ino);
2190 iput(vol->extend_ino);
2191#endif /* NTFS_RW */
2192iput_sec_err_out:
2193 iput(vol->secure_ino);
2194iput_root_err_out:
2195 iput(vol->root_ino);
2196iput_logfile_err_out:
2197#ifdef NTFS_RW
2198 if (vol->logfile_ino)
2199 iput(vol->logfile_ino);
2200iput_vol_err_out:
2201#endif /* NTFS_RW */
2202 iput(vol->vol_ino);
2203iput_lcnbmp_err_out:
2204 iput(vol->lcnbmp_ino);
2205iput_attrdef_err_out:
2206 vol->attrdef_size = 0;
2207 if (vol->attrdef) {
2208 ntfs_free(vol->attrdef);
2209 vol->attrdef = NULL;
2210 }
2211#ifdef NTFS_RW
2212iput_upcase_err_out:
2213#endif /* NTFS_RW */
2214 vol->upcase_len = 0;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002215 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if (vol->upcase == default_upcase) {
2217 ntfs_nr_upcase_users--;
2218 vol->upcase = NULL;
2219 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002220 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 if (vol->upcase) {
2222 ntfs_free(vol->upcase);
2223 vol->upcase = NULL;
2224 }
2225iput_mftbmp_err_out:
2226 iput(vol->mftbmp_ino);
2227iput_mirr_err_out:
2228#ifdef NTFS_RW
2229 if (vol->mftmirr_ino)
2230 iput(vol->mftmirr_ino);
2231#endif /* NTFS_RW */
Richard Knutssonc49c3112006-09-30 23:27:12 -07002232 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
2235/**
2236 * ntfs_put_super - called by the vfs to unmount a volume
2237 * @sb: vfs superblock of volume to unmount
2238 *
2239 * ntfs_put_super() is called by the VFS (from fs/super.c::do_umount()) when
2240 * the volume is being unmounted (umount system call has been invoked) and it
2241 * releases all inodes and memory belonging to the NTFS specific part of the
2242 * super block.
2243 */
2244static void ntfs_put_super(struct super_block *sb)
2245{
2246 ntfs_volume *vol = NTFS_SB(sb);
2247
2248 ntfs_debug("Entering.");
Christoph Hellwig6cfd0142009-05-05 15:40:36 +02002249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250#ifdef NTFS_RW
2251 /*
2252 * Commit all inodes while they are still open in case some of them
2253 * cause others to be dirtied.
2254 */
2255 ntfs_commit_inode(vol->vol_ino);
2256
2257 /* NTFS 3.0+ specific. */
2258 if (vol->major_ver >= 3) {
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002259 if (vol->usnjrnl_j_ino)
2260 ntfs_commit_inode(vol->usnjrnl_j_ino);
2261 if (vol->usnjrnl_max_ino)
2262 ntfs_commit_inode(vol->usnjrnl_max_ino);
2263 if (vol->usnjrnl_ino)
2264 ntfs_commit_inode(vol->usnjrnl_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 if (vol->quota_q_ino)
2266 ntfs_commit_inode(vol->quota_q_ino);
2267 if (vol->quota_ino)
2268 ntfs_commit_inode(vol->quota_ino);
2269 if (vol->extend_ino)
2270 ntfs_commit_inode(vol->extend_ino);
2271 if (vol->secure_ino)
2272 ntfs_commit_inode(vol->secure_ino);
2273 }
2274
2275 ntfs_commit_inode(vol->root_ino);
2276
2277 down_write(&vol->lcnbmp_lock);
2278 ntfs_commit_inode(vol->lcnbmp_ino);
2279 up_write(&vol->lcnbmp_lock);
2280
2281 down_write(&vol->mftbmp_lock);
2282 ntfs_commit_inode(vol->mftbmp_ino);
2283 up_write(&vol->mftbmp_lock);
2284
2285 if (vol->logfile_ino)
2286 ntfs_commit_inode(vol->logfile_ino);
2287
2288 if (vol->mftmirr_ino)
2289 ntfs_commit_inode(vol->mftmirr_ino);
2290 ntfs_commit_inode(vol->mft_ino);
2291
2292 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002293 * If a read-write mount and no volume errors have occurred, mark the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 * volume clean. Also, re-commit all affected inodes.
2295 */
2296 if (!(sb->s_flags & MS_RDONLY)) {
2297 if (!NVolErrors(vol)) {
2298 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
2299 ntfs_warning(sb, "Failed to clear dirty bit "
2300 "in volume information "
2301 "flags. Run chkdsk.");
2302 ntfs_commit_inode(vol->vol_ino);
2303 ntfs_commit_inode(vol->root_ino);
2304 if (vol->mftmirr_ino)
2305 ntfs_commit_inode(vol->mftmirr_ino);
2306 ntfs_commit_inode(vol->mft_ino);
2307 } else {
2308 ntfs_warning(sb, "Volume has errors. Leaving volume "
2309 "marked dirty. Run chkdsk.");
2310 }
2311 }
2312#endif /* NTFS_RW */
2313
2314 iput(vol->vol_ino);
2315 vol->vol_ino = NULL;
2316
2317 /* NTFS 3.0+ specific clean up. */
2318 if (vol->major_ver >= 3) {
2319#ifdef NTFS_RW
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002320 if (vol->usnjrnl_j_ino) {
2321 iput(vol->usnjrnl_j_ino);
2322 vol->usnjrnl_j_ino = NULL;
2323 }
2324 if (vol->usnjrnl_max_ino) {
2325 iput(vol->usnjrnl_max_ino);
2326 vol->usnjrnl_max_ino = NULL;
2327 }
2328 if (vol->usnjrnl_ino) {
2329 iput(vol->usnjrnl_ino);
2330 vol->usnjrnl_ino = NULL;
2331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 if (vol->quota_q_ino) {
2333 iput(vol->quota_q_ino);
2334 vol->quota_q_ino = NULL;
2335 }
2336 if (vol->quota_ino) {
2337 iput(vol->quota_ino);
2338 vol->quota_ino = NULL;
2339 }
2340#endif /* NTFS_RW */
2341 if (vol->extend_ino) {
2342 iput(vol->extend_ino);
2343 vol->extend_ino = NULL;
2344 }
2345 if (vol->secure_ino) {
2346 iput(vol->secure_ino);
2347 vol->secure_ino = NULL;
2348 }
2349 }
2350
2351 iput(vol->root_ino);
2352 vol->root_ino = NULL;
2353
2354 down_write(&vol->lcnbmp_lock);
2355 iput(vol->lcnbmp_ino);
2356 vol->lcnbmp_ino = NULL;
2357 up_write(&vol->lcnbmp_lock);
2358
2359 down_write(&vol->mftbmp_lock);
2360 iput(vol->mftbmp_ino);
2361 vol->mftbmp_ino = NULL;
2362 up_write(&vol->mftbmp_lock);
2363
2364#ifdef NTFS_RW
2365 if (vol->logfile_ino) {
2366 iput(vol->logfile_ino);
2367 vol->logfile_ino = NULL;
2368 }
2369 if (vol->mftmirr_ino) {
2370 /* Re-commit the mft mirror and mft just in case. */
2371 ntfs_commit_inode(vol->mftmirr_ino);
2372 ntfs_commit_inode(vol->mft_ino);
2373 iput(vol->mftmirr_ino);
2374 vol->mftmirr_ino = NULL;
2375 }
2376 /*
Jens Axboe13205fb2009-05-25 09:30:45 +02002377 * We should have no dirty inodes left, due to
2378 * mft.c::ntfs_mft_writepage() cleaning all the dirty pages as
2379 * the underlying mft records are written out and cleaned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 */
2381 ntfs_commit_inode(vol->mft_ino);
2382 write_inode_now(vol->mft_ino, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383#endif /* NTFS_RW */
2384
2385 iput(vol->mft_ino);
2386 vol->mft_ino = NULL;
2387
2388 /* Throw away the table of attribute definitions. */
2389 vol->attrdef_size = 0;
2390 if (vol->attrdef) {
2391 ntfs_free(vol->attrdef);
2392 vol->attrdef = NULL;
2393 }
2394 vol->upcase_len = 0;
2395 /*
2396 * Destroy the global default upcase table if necessary. Also decrease
2397 * the number of upcase users if we are a user.
2398 */
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002399 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (vol->upcase == default_upcase) {
2401 ntfs_nr_upcase_users--;
2402 vol->upcase = NULL;
2403 }
2404 if (!ntfs_nr_upcase_users && default_upcase) {
2405 ntfs_free(default_upcase);
2406 default_upcase = NULL;
2407 }
2408 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
2409 free_compression_buffers();
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002410 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 if (vol->upcase) {
2412 ntfs_free(vol->upcase);
2413 vol->upcase = NULL;
2414 }
Thomas Gleixner6d729e42009-08-16 21:05:08 +00002415
2416 unload_nls(vol->nls_map);
2417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 sb->s_fs_info = NULL;
2419 kfree(vol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
2422/**
2423 * get_nr_free_clusters - return the number of free clusters on a volume
2424 * @vol: ntfs volume for which to obtain free cluster count
2425 *
2426 * Calculate the number of free clusters on the mounted NTFS volume @vol. We
2427 * actually calculate the number of clusters in use instead because this
2428 * allows us to not care about partial pages as these will be just zero filled
2429 * and hence not be counted as allocated clusters.
2430 *
2431 * The only particularity is that clusters beyond the end of the logical ntfs
2432 * volume will be marked as allocated to prevent errors which means we have to
2433 * discount those at the end. This is important as the cluster bitmap always
2434 * has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside
2435 * the logical volume and marked in use when they are not as they do not exist.
2436 *
2437 * If any pages cannot be read we assume all clusters in the erroring pages are
2438 * in use. This means we return an underestimate on errors which is better than
2439 * an overestimate.
2440 */
2441static s64 get_nr_free_clusters(ntfs_volume *vol)
2442{
2443 s64 nr_free = vol->nr_clusters;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 struct page *page;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002446 pgoff_t index, max_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448 ntfs_debug("Entering.");
2449 /* Serialize accesses to the cluster bitmap. */
2450 down_read(&vol->lcnbmp_lock);
2451 /*
2452 * Convert the number of bits into bytes rounded up, then convert into
2453 * multiples of PAGE_CACHE_SIZE, rounding up so that if we have one
2454 * full and one partial page max_index = 2.
2455 */
2456 max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >>
2457 PAGE_CACHE_SHIFT;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002458 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2459 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.",
2460 max_index, PAGE_CACHE_SIZE / 4);
2461 for (index = 0; index < max_index; index++) {
Akinobu Mitac4af9642010-03-15 00:35:00 -04002462 unsigned long *kaddr;
2463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 /*
2465 * Read the page from page cache, getting it from backing store
2466 * if necessary, and increment the use count.
2467 */
Nick Piggin6fe69002007-05-06 14:49:04 -07002468 page = read_mapping_page(mapping, index, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 /* Ignore pages which errored synchronously. */
2470 if (IS_ERR(page)) {
Nick Piggin6fe69002007-05-06 14:49:04 -07002471 ntfs_debug("read_mapping_page() error. Skipping "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 "page (index 0x%lx).", index);
2473 nr_free -= PAGE_CACHE_SIZE * 8;
2474 continue;
2475 }
Cong Wanga3ac1412011-11-25 23:14:34 +08002476 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 /*
Akinobu Mitac4af9642010-03-15 00:35:00 -04002478 * Subtract the number of set bits. If this
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 * is the last page and it is partial we don't really care as
2480 * it just means we do a little extra work but it won't affect
2481 * the result as all out of range bytes are set to zero by
2482 * ntfs_readpage().
2483 */
Akinobu Mitac4af9642010-03-15 00:35:00 -04002484 nr_free -= bitmap_weight(kaddr,
2485 PAGE_CACHE_SIZE * BITS_PER_BYTE);
Cong Wanga3ac1412011-11-25 23:14:34 +08002486 kunmap_atomic(kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 page_cache_release(page);
2488 }
2489 ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index - 1);
2490 /*
2491 * Fixup for eventual bits outside logical ntfs volume (see function
2492 * description above).
2493 */
2494 if (vol->nr_clusters & 63)
2495 nr_free += 64 - (vol->nr_clusters & 63);
2496 up_read(&vol->lcnbmp_lock);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002497 /* If errors occurred we may well have gone below zero, fix this. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 if (nr_free < 0)
2499 nr_free = 0;
2500 ntfs_debug("Exiting.");
2501 return nr_free;
2502}
2503
2504/**
2505 * __get_nr_free_mft_records - return the number of free inodes on a volume
2506 * @vol: ntfs volume for which to obtain free inode count
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002507 * @nr_free: number of mft records in filesystem
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002508 * @max_index: maximum number of pages containing set bits
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 *
2510 * Calculate the number of free mft records (inodes) on the mounted NTFS
2511 * volume @vol. We actually calculate the number of mft records in use instead
2512 * because this allows us to not care about partial pages as these will be just
2513 * zero filled and hence not be counted as allocated mft record.
2514 *
2515 * If any pages cannot be read we assume all mft records in the erroring pages
2516 * are in use. This means we return an underestimate on errors which is better
2517 * than an overestimate.
2518 *
2519 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
2520 */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002521static unsigned long __get_nr_free_mft_records(ntfs_volume *vol,
2522 s64 nr_free, const pgoff_t max_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 struct address_space *mapping = vol->mftbmp_ino->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 struct page *page;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002526 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
2528 ntfs_debug("Entering.");
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002529 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002531 "0x%lx.", max_index, PAGE_CACHE_SIZE / 4);
2532 for (index = 0; index < max_index; index++) {
Akinobu Mitac4af9642010-03-15 00:35:00 -04002533 unsigned long *kaddr;
2534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 /*
2536 * Read the page from page cache, getting it from backing store
2537 * if necessary, and increment the use count.
2538 */
Nick Piggin6fe69002007-05-06 14:49:04 -07002539 page = read_mapping_page(mapping, index, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 /* Ignore pages which errored synchronously. */
2541 if (IS_ERR(page)) {
Nick Piggin6fe69002007-05-06 14:49:04 -07002542 ntfs_debug("read_mapping_page() error. Skipping "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 "page (index 0x%lx).", index);
2544 nr_free -= PAGE_CACHE_SIZE * 8;
2545 continue;
2546 }
Cong Wanga3ac1412011-11-25 23:14:34 +08002547 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 /*
Akinobu Mitac4af9642010-03-15 00:35:00 -04002549 * Subtract the number of set bits. If this
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 * is the last page and it is partial we don't really care as
2551 * it just means we do a little extra work but it won't affect
2552 * the result as all out of range bytes are set to zero by
2553 * ntfs_readpage().
2554 */
Akinobu Mitac4af9642010-03-15 00:35:00 -04002555 nr_free -= bitmap_weight(kaddr,
2556 PAGE_CACHE_SIZE * BITS_PER_BYTE);
Cong Wanga3ac1412011-11-25 23:14:34 +08002557 kunmap_atomic(kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 page_cache_release(page);
2559 }
2560 ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.",
2561 index - 1);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002562 /* If errors occurred we may well have gone below zero, fix this. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if (nr_free < 0)
2564 nr_free = 0;
2565 ntfs_debug("Exiting.");
2566 return nr_free;
2567}
2568
2569/**
2570 * ntfs_statfs - return information about mounted NTFS volume
David Howells726c3342006-06-23 02:02:58 -07002571 * @dentry: dentry from mounted volume
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 * @sfs: statfs structure in which to return the information
2573 *
David Howells726c3342006-06-23 02:02:58 -07002574 * Return information about the mounted NTFS volume @dentry in the statfs structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is
2576 * called). We interpret the values to be correct of the moment in time at
2577 * which we are called. Most values are variable otherwise and this isn't just
2578 * the free values but the totals as well. For example we can increase the
2579 * total number of file nodes if we run out and we can keep doing this until
2580 * there is no more space on the volume left at all.
2581 *
2582 * Called from vfs_statfs which is used to handle the statfs, fstatfs, and
2583 * ustat system calls.
2584 *
2585 * Return 0 on success or -errno on error.
2586 */
David Howells726c3342006-06-23 02:02:58 -07002587static int ntfs_statfs(struct dentry *dentry, struct kstatfs *sfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
David Howells726c3342006-06-23 02:02:58 -07002589 struct super_block *sb = dentry->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 s64 size;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002591 ntfs_volume *vol = NTFS_SB(sb);
2592 ntfs_inode *mft_ni = NTFS_I(vol->mft_ino);
2593 pgoff_t max_index;
2594 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
2596 ntfs_debug("Entering.");
2597 /* Type of filesystem. */
2598 sfs->f_type = NTFS_SB_MAGIC;
2599 /* Optimal transfer block size. */
2600 sfs->f_bsize = PAGE_CACHE_SIZE;
2601 /*
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002602 * Total data blocks in filesystem in units of f_bsize and since
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 * inodes are also stored in data blocs ($MFT is a file) this is just
2604 * the total clusters.
2605 */
2606 sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >>
2607 PAGE_CACHE_SHIFT;
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002608 /* Free data blocks in filesystem in units of f_bsize. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 size = get_nr_free_clusters(vol) << vol->cluster_size_bits >>
2610 PAGE_CACHE_SHIFT;
2611 if (size < 0LL)
2612 size = 0LL;
2613 /* Free blocks avail to non-superuser, same as above on NTFS. */
2614 sfs->f_bavail = sfs->f_bfree = size;
2615 /* Serialize accesses to the inode bitmap. */
2616 down_read(&vol->mftbmp_lock);
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002617 read_lock_irqsave(&mft_ni->size_lock, flags);
2618 size = i_size_read(vol->mft_ino) >> vol->mft_record_size_bits;
2619 /*
2620 * Convert the maximum number of set bits into bytes rounded up, then
2621 * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
2622 * have one full and one partial page max_index = 2.
2623 */
2624 max_index = ((((mft_ni->initialized_size >> vol->mft_record_size_bits)
2625 + 7) >> 3) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2626 read_unlock_irqrestore(&mft_ni->size_lock, flags);
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002627 /* Number of inodes in filesystem (at this point in time). */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002628 sfs->f_files = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 /* Free inodes in fs (based on current total count). */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002630 sfs->f_ffree = __get_nr_free_mft_records(vol, size, max_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 up_read(&vol->mftbmp_lock);
2632 /*
2633 * File system id. This is extremely *nix flavour dependent and even
2634 * within Linux itself all fs do their own thing. I interpret this to
2635 * mean a unique id associated with the mounted fs and not the id
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002636 * associated with the filesystem driver, the latter is already given
2637 * by the filesystem type in sfs->f_type. Thus we use the 64-bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 * volume serial number splitting it into two 32-bit parts. We enter
2639 * the least significant 32-bits in f_fsid[0] and the most significant
2640 * 32-bits in f_fsid[1].
2641 */
2642 sfs->f_fsid.val[0] = vol->serial_no & 0xffffffff;
2643 sfs->f_fsid.val[1] = (vol->serial_no >> 32) & 0xffffffff;
2644 /* Maximum length of filenames. */
2645 sfs->f_namelen = NTFS_MAX_NAME_LEN;
2646 return 0;
2647}
2648
Christoph Hellwiga9185b42010-03-05 09:21:37 +01002649#ifdef NTFS_RW
2650static int ntfs_write_inode(struct inode *vi, struct writeback_control *wbc)
2651{
2652 return __ntfs_write_inode(vi, wbc->sync_mode == WB_SYNC_ALL);
2653}
2654#endif
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656/**
2657 * The complete super operations.
2658 */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08002659static const struct super_operations ntfs_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 .alloc_inode = ntfs_alloc_big_inode, /* VFS: Allocate new inode. */
2661 .destroy_inode = ntfs_destroy_big_inode, /* VFS: Deallocate inode. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662#ifdef NTFS_RW
2663 //.dirty_inode = NULL, /* VFS: Called from
2664 // __mark_inode_dirty(). */
2665 .write_inode = ntfs_write_inode, /* VFS: Write dirty inode to
2666 disk. */
2667 //.drop_inode = NULL, /* VFS: Called just after the
2668 // inode reference count has
2669 // been decreased to zero.
2670 // NOTE: The inode lock is
2671 // held. See fs/inode.c::
2672 // generic_drop_inode(). */
2673 //.delete_inode = NULL, /* VFS: Delete inode from disk.
2674 // Called when i_count becomes
2675 // 0 and i_nlink is also 0. */
2676 //.write_super = NULL, /* Flush dirty super block to
2677 // disk. */
2678 //.sync_fs = NULL, /* ? */
2679 //.write_super_lockfs = NULL, /* ? */
2680 //.unlockfs = NULL, /* ? */
2681#endif /* NTFS_RW */
2682 .put_super = ntfs_put_super, /* Syscall: umount. */
2683 .statfs = ntfs_statfs, /* Syscall: statfs */
2684 .remount_fs = ntfs_remount, /* Syscall: mount -o remount. */
Al Virob57922d2010-06-07 14:34:48 -04002685 .evict_inode = ntfs_evict_big_inode, /* VFS: Called when an inode is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 removed from memory. */
2687 //.umount_begin = NULL, /* Forced umount. */
2688 .show_options = ntfs_show_options, /* Show mount options in
2689 proc. */
2690};
2691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692/**
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002693 * ntfs_fill_super - mount an ntfs filesystem
2694 * @sb: super block of ntfs filesystem to mount
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 * @opt: string containing the mount options
2696 * @silent: silence error output
2697 *
2698 * ntfs_fill_super() is called by the VFS to mount the device described by @sb
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002699 * with the mount otions in @data with the NTFS filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 *
2701 * If @silent is true, remain silent even if errors are detected. This is used
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002702 * during bootup, when the kernel tries to mount the root filesystem with all
2703 * registered filesystems one after the other until one succeeds. This implies
2704 * that all filesystems except the correct one will quite correctly and
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 * expectedly return an error, but nobody wants to see error messages when in
2706 * fact this is what is supposed to happen.
2707 *
2708 * NOTE: @sb->s_flags contains the mount options flags.
2709 */
2710static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2711{
2712 ntfs_volume *vol;
2713 struct buffer_head *bh;
2714 struct inode *tmp_ino;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002715 int blocksize, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Ingo Molnar59345372006-07-03 00:25:18 -07002717 /*
2718 * We do a pretty difficult piece of bootstrap by reading the
2719 * MFT (and other metadata) from disk into memory. We'll only
2720 * release this metadata during umount, so the locking patterns
2721 * observed during bootstrap do not count. So turn off the
2722 * observation of locking patterns (strictly for this context
2723 * only) while mounting NTFS. [The validator is still active
2724 * otherwise, even for this context: it will for example record
2725 * lock class registrations.]
2726 */
2727 lockdep_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 ntfs_debug("Entering.");
2729#ifndef NTFS_RW
Christoph Hellwig24a44dc2006-01-09 20:52:14 -08002730 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731#endif /* ! NTFS_RW */
2732 /* Allocate a new ntfs_volume and place it in sb->s_fs_info. */
2733 sb->s_fs_info = kmalloc(sizeof(ntfs_volume), GFP_NOFS);
2734 vol = NTFS_SB(sb);
2735 if (!vol) {
2736 if (!silent)
2737 ntfs_error(sb, "Allocation of NTFS volume structure "
2738 "failed. Aborting mount...");
Ingo Molnar59345372006-07-03 00:25:18 -07002739 lockdep_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 return -ENOMEM;
2741 }
2742 /* Initialize ntfs_volume structure. */
Anton Altaparmakov442d2072005-05-27 16:42:56 +01002743 *vol = (ntfs_volume) {
2744 .sb = sb,
2745 /*
2746 * Default is group and other don't have any access to files or
2747 * directories while owner has full access. Further, files by
2748 * default are not executable but directories are of course
2749 * browseable.
2750 */
2751 .fmask = 0177,
2752 .dmask = 0077,
2753 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 init_rwsem(&vol->mftbmp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 init_rwsem(&vol->lcnbmp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002757 /* By default, enable sparse support. */
2758 NVolSetSparseEnabled(vol);
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 /* Important to get the mount options dealt with now. */
2761 if (!parse_options(vol, (char*)opt))
2762 goto err_out_now;
2763
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002764 /* We support sector sizes up to the PAGE_CACHE_SIZE. */
Martin K. Petersene1defc42009-05-22 17:17:49 -04002765 if (bdev_logical_block_size(sb->s_bdev) > PAGE_CACHE_SIZE) {
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002766 if (!silent)
2767 ntfs_error(sb, "Device has unsupported sector size "
2768 "(%i). The maximum supported sector "
2769 "size on this architecture is %lu "
2770 "bytes.",
Martin K. Petersene1defc42009-05-22 17:17:49 -04002771 bdev_logical_block_size(sb->s_bdev),
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002772 PAGE_CACHE_SIZE);
2773 goto err_out_now;
2774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 /*
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002776 * Setup the device access block size to NTFS_BLOCK_SIZE or the hard
2777 * sector size, whichever is bigger.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 */
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002779 blocksize = sb_min_blocksize(sb, NTFS_BLOCK_SIZE);
2780 if (blocksize < NTFS_BLOCK_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 if (!silent)
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002782 ntfs_error(sb, "Unable to set device block size.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 goto err_out_now;
2784 }
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002785 BUG_ON(blocksize != sb->s_blocksize);
2786 ntfs_debug("Set device block size to %i bytes (block size bits %i).",
2787 blocksize, sb->s_blocksize_bits);
2788 /* Determine the size of the device in units of block_size bytes. */
2789 if (!i_size_read(sb->s_bdev->bd_inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 if (!silent)
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002791 ntfs_error(sb, "Unable to determine device size.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 goto err_out_now;
2793 }
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002794 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002795 sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 /* Read the boot sector and return unlocked buffer head to it. */
2797 if (!(bh = read_ntfs_boot_sector(sb, silent))) {
2798 if (!silent)
2799 ntfs_error(sb, "Not an NTFS volume.");
2800 goto err_out_now;
2801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 /*
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002803 * Extract the data from the boot sector and setup the ntfs volume
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 * using it.
2805 */
2806 result = parse_ntfs_boot_sector(vol, (NTFS_BOOT_SECTOR*)bh->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if (!result) {
2809 if (!silent)
2810 ntfs_error(sb, "Unsupported NTFS filesystem.");
2811 goto err_out_now;
2812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 /*
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002814 * If the boot sector indicates a sector size bigger than the current
2815 * device block size, switch the device block size to the sector size.
2816 * TODO: It may be possible to support this case even when the set
2817 * below fails, we would just be breaking up the i/o for each sector
2818 * into multiple blocks for i/o purposes but otherwise it should just
2819 * work. However it is safer to leave disabled until someone hits this
2820 * error message and then we can get them to try it without the setting
2821 * so we know for sure that it works.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 */
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002823 if (vol->sector_size > blocksize) {
2824 blocksize = sb_set_blocksize(sb, vol->sector_size);
2825 if (blocksize != vol->sector_size) {
2826 if (!silent)
2827 ntfs_error(sb, "Unable to set device block "
2828 "size to sector size (%i).",
2829 vol->sector_size);
2830 goto err_out_now;
2831 }
2832 BUG_ON(blocksize != sb->s_blocksize);
2833 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2834 sb->s_blocksize_bits;
2835 ntfs_debug("Changed device block size to %i bytes (block size "
2836 "bits %i) to match volume sector size.",
2837 blocksize, sb->s_blocksize_bits);
2838 }
2839 /* Initialize the cluster and mft allocators. */
2840 ntfs_setup_allocators(vol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 /* Setup remaining fields in the super block. */
2842 sb->s_magic = NTFS_SB_MAGIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 /*
2844 * Ntfs allows 63 bits for the file size, i.e. correct would be:
2845 * sb->s_maxbytes = ~0ULL >> 1;
2846 * But the kernel uses a long as the page cache page index which on
2847 * 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel
2848 * defined to the maximum the page cache page index can cope with
2849 * without overflowing the index or to 2^63 - 1, whichever is smaller.
2850 */
2851 sb->s_maxbytes = MAX_LFS_FILESIZE;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002852 /* Ntfs measures time in 100ns intervals. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 sb->s_time_gran = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 /*
2855 * Now load the metadata required for the page cache and our address
2856 * space operations to function. We do this by setting up a specialised
2857 * read_inode method and then just calling the normal iget() to obtain
2858 * the inode for $MFT which is sufficient to allow our normal inode
2859 * operations and associated address space operations to function.
2860 */
2861 sb->s_op = &ntfs_sops;
2862 tmp_ino = new_inode(sb);
2863 if (!tmp_ino) {
2864 if (!silent)
2865 ntfs_error(sb, "Failed to load essential metadata.");
2866 goto err_out_now;
2867 }
2868 tmp_ino->i_ino = FILE_MFT;
2869 insert_inode_hash(tmp_ino);
2870 if (ntfs_read_inode_mount(tmp_ino) < 0) {
2871 if (!silent)
2872 ntfs_error(sb, "Failed to load essential metadata.");
2873 goto iput_tmp_ino_err_out_now;
2874 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002875 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 /*
2877 * The current mount is a compression user if the cluster size is
2878 * less than or equal 4kiB.
2879 */
2880 if (vol->cluster_size <= 4096 && !ntfs_nr_compression_users++) {
2881 result = allocate_compression_buffers();
2882 if (result) {
2883 ntfs_error(NULL, "Failed to allocate buffers "
2884 "for compression engine.");
2885 ntfs_nr_compression_users--;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002886 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 goto iput_tmp_ino_err_out_now;
2888 }
2889 }
2890 /*
2891 * Generate the global default upcase table if necessary. Also
2892 * temporarily increment the number of upcase users to avoid race
2893 * conditions with concurrent (u)mounts.
2894 */
2895 if (!default_upcase)
2896 default_upcase = generate_default_upcase();
2897 ntfs_nr_upcase_users++;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002898 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 /*
2900 * From now on, ignore @silent parameter. If we fail below this line,
2901 * it will be due to a corrupt fs or a system error, so we report it.
2902 */
2903 /*
2904 * Open the system files with normal access functions and complete
2905 * setting up the ntfs super block.
2906 */
2907 if (!load_system_files(vol)) {
2908 ntfs_error(sb, "Failed to load system files.");
2909 goto unl_upcase_iput_tmp_ino_err_out_now;
2910 }
2911 if ((sb->s_root = d_alloc_root(vol->root_ino))) {
Al Viro7de9c6e2010-10-23 11:11:40 -04002912 /* We grab a reference, simulating an ntfs_iget(). */
2913 ihold(vol->root_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 ntfs_debug("Exiting, status successful.");
2915 /* Release the default upcase if it has no users. */
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002916 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 if (!--ntfs_nr_upcase_users && default_upcase) {
2918 ntfs_free(default_upcase);
2919 default_upcase = NULL;
2920 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002921 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 sb->s_export_op = &ntfs_export_ops;
Ingo Molnar59345372006-07-03 00:25:18 -07002923 lockdep_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return 0;
2925 }
2926 ntfs_error(sb, "Failed to allocate root directory.");
2927 /* Clean up after the successful load_system_files() call from above. */
2928 // TODO: Use ntfs_put_super() instead of repeating all this code...
2929 // FIXME: Should mark the volume clean as the error is most likely
2930 // -ENOMEM.
2931 iput(vol->vol_ino);
2932 vol->vol_ino = NULL;
2933 /* NTFS 3.0+ specific clean up. */
2934 if (vol->major_ver >= 3) {
2935#ifdef NTFS_RW
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002936 if (vol->usnjrnl_j_ino) {
2937 iput(vol->usnjrnl_j_ino);
2938 vol->usnjrnl_j_ino = NULL;
2939 }
2940 if (vol->usnjrnl_max_ino) {
2941 iput(vol->usnjrnl_max_ino);
2942 vol->usnjrnl_max_ino = NULL;
2943 }
2944 if (vol->usnjrnl_ino) {
2945 iput(vol->usnjrnl_ino);
2946 vol->usnjrnl_ino = NULL;
2947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 if (vol->quota_q_ino) {
2949 iput(vol->quota_q_ino);
2950 vol->quota_q_ino = NULL;
2951 }
2952 if (vol->quota_ino) {
2953 iput(vol->quota_ino);
2954 vol->quota_ino = NULL;
2955 }
2956#endif /* NTFS_RW */
2957 if (vol->extend_ino) {
2958 iput(vol->extend_ino);
2959 vol->extend_ino = NULL;
2960 }
2961 if (vol->secure_ino) {
2962 iput(vol->secure_ino);
2963 vol->secure_ino = NULL;
2964 }
2965 }
2966 iput(vol->root_ino);
2967 vol->root_ino = NULL;
2968 iput(vol->lcnbmp_ino);
2969 vol->lcnbmp_ino = NULL;
2970 iput(vol->mftbmp_ino);
2971 vol->mftbmp_ino = NULL;
2972#ifdef NTFS_RW
2973 if (vol->logfile_ino) {
2974 iput(vol->logfile_ino);
2975 vol->logfile_ino = NULL;
2976 }
2977 if (vol->mftmirr_ino) {
2978 iput(vol->mftmirr_ino);
2979 vol->mftmirr_ino = NULL;
2980 }
2981#endif /* NTFS_RW */
2982 /* Throw away the table of attribute definitions. */
2983 vol->attrdef_size = 0;
2984 if (vol->attrdef) {
2985 ntfs_free(vol->attrdef);
2986 vol->attrdef = NULL;
2987 }
2988 vol->upcase_len = 0;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002989 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 if (vol->upcase == default_upcase) {
2991 ntfs_nr_upcase_users--;
2992 vol->upcase = NULL;
2993 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002994 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 if (vol->upcase) {
2996 ntfs_free(vol->upcase);
2997 vol->upcase = NULL;
2998 }
2999 if (vol->nls_map) {
3000 unload_nls(vol->nls_map);
3001 vol->nls_map = NULL;
3002 }
3003 /* Error exit code path. */
3004unl_upcase_iput_tmp_ino_err_out_now:
3005 /*
3006 * Decrease the number of upcase users and destroy the global default
3007 * upcase table if necessary.
3008 */
Ingo Molnar4e5e5292006-03-23 16:57:48 +00003009 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 if (!--ntfs_nr_upcase_users && default_upcase) {
3011 ntfs_free(default_upcase);
3012 default_upcase = NULL;
3013 }
3014 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
3015 free_compression_buffers();
Ingo Molnar4e5e5292006-03-23 16:57:48 +00003016 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017iput_tmp_ino_err_out_now:
3018 iput(tmp_ino);
3019 if (vol->mft_ino && vol->mft_ino != tmp_ino)
3020 iput(vol->mft_ino);
3021 vol->mft_ino = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 /* Errors at this stage are irrelevant. */
3023err_out_now:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 sb->s_fs_info = NULL;
3025 kfree(vol);
3026 ntfs_debug("Failed, returning -EINVAL.");
Ingo Molnar59345372006-07-03 00:25:18 -07003027 lockdep_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 return -EINVAL;
3029}
3030
3031/*
3032 * This is a slab cache to optimize allocations and deallocations of Unicode
3033 * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN
3034 * (255) Unicode characters + a terminating NULL Unicode character.
3035 */
Pekka Enberg64419d92006-02-05 21:43:57 +00003036struct kmem_cache *ntfs_name_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Anton Altaparmakov7fafb8b2005-04-06 16:09:21 +01003038/* Slab caches for efficient allocation/deallocation of inodes. */
Pekka Enberg64419d92006-02-05 21:43:57 +00003039struct kmem_cache *ntfs_inode_cache;
3040struct kmem_cache *ntfs_big_inode_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
3042/* Init once constructor for the inode slab cache. */
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003043static void ntfs_big_inode_init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
3045 ntfs_inode *ni = (ntfs_inode *)foo;
3046
Christoph Lametera35afb82007-05-16 22:10:57 -07003047 inode_init_once(VFS_I(ni));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048}
3049
3050/*
3051 * Slab caches to optimize allocations and deallocations of attribute search
3052 * contexts and index contexts, respectively.
3053 */
Pekka Enberg64419d92006-02-05 21:43:57 +00003054struct kmem_cache *ntfs_attr_ctx_cache;
3055struct kmem_cache *ntfs_index_ctx_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Anton Altaparmakove750d1c2006-03-23 17:04:12 +00003057/* Driver wide mutex. */
Ingo Molnar4e5e5292006-03-23 16:57:48 +00003058DEFINE_MUTEX(ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
Al Viro152a0832010-07-25 00:46:55 +04003060static struct dentry *ntfs_mount(struct file_system_type *fs_type,
3061 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
Al Viro152a0832010-07-25 00:46:55 +04003063 return mount_bdev(fs_type, flags, dev_name, data, ntfs_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064}
3065
3066static struct file_system_type ntfs_fs_type = {
3067 .owner = THIS_MODULE,
3068 .name = "ntfs",
Al Viro152a0832010-07-25 00:46:55 +04003069 .mount = ntfs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 .kill_sb = kill_block_super,
3071 .fs_flags = FS_REQUIRES_DEV,
3072};
3073
3074/* Stable names for the slab caches. */
3075static const char ntfs_index_ctx_cache_name[] = "ntfs_index_ctx_cache";
3076static const char ntfs_attr_ctx_cache_name[] = "ntfs_attr_ctx_cache";
3077static const char ntfs_name_cache_name[] = "ntfs_name_cache";
3078static const char ntfs_inode_cache_name[] = "ntfs_inode_cache";
3079static const char ntfs_big_inode_cache_name[] = "ntfs_big_inode_cache";
3080
3081static int __init init_ntfs_fs(void)
3082{
3083 int err = 0;
3084
3085 /* This may be ugly but it results in pretty output so who cares. (-8 */
3086 printk(KERN_INFO "NTFS driver " NTFS_VERSION " [Flags: R/"
3087#ifdef NTFS_RW
3088 "W"
3089#else
3090 "O"
3091#endif
3092#ifdef DEBUG
3093 " DEBUG"
3094#endif
3095#ifdef MODULE
3096 " MODULE"
3097#endif
3098 "].\n");
3099
3100 ntfs_debug("Debug messages are enabled.");
3101
3102 ntfs_index_ctx_cache = kmem_cache_create(ntfs_index_ctx_cache_name,
3103 sizeof(ntfs_index_context), 0 /* offset */,
Paul Mundt20c2df82007-07-20 10:11:58 +09003104 SLAB_HWCACHE_ALIGN, NULL /* ctor */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 if (!ntfs_index_ctx_cache) {
3106 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3107 ntfs_index_ctx_cache_name);
3108 goto ictx_err_out;
3109 }
3110 ntfs_attr_ctx_cache = kmem_cache_create(ntfs_attr_ctx_cache_name,
3111 sizeof(ntfs_attr_search_ctx), 0 /* offset */,
Paul Mundt20c2df82007-07-20 10:11:58 +09003112 SLAB_HWCACHE_ALIGN, NULL /* ctor */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 if (!ntfs_attr_ctx_cache) {
3114 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3115 ntfs_attr_ctx_cache_name);
3116 goto actx_err_out;
3117 }
3118
3119 ntfs_name_cache = kmem_cache_create(ntfs_name_cache_name,
3120 (NTFS_MAX_NAME_LEN+1) * sizeof(ntfschar), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09003121 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 if (!ntfs_name_cache) {
3123 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3124 ntfs_name_cache_name);
3125 goto name_err_out;
3126 }
3127
3128 ntfs_inode_cache = kmem_cache_create(ntfs_inode_cache_name,
3129 sizeof(ntfs_inode), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09003130 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 if (!ntfs_inode_cache) {
3132 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3133 ntfs_inode_cache_name);
3134 goto inode_err_out;
3135 }
3136
3137 ntfs_big_inode_cache = kmem_cache_create(ntfs_big_inode_cache_name,
3138 sizeof(big_ntfs_inode), 0,
Paul Jackson4b6a9312006-03-24 03:16:05 -08003139 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD,
Paul Mundt20c2df82007-07-20 10:11:58 +09003140 ntfs_big_inode_init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 if (!ntfs_big_inode_cache) {
3142 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3143 ntfs_big_inode_cache_name);
3144 goto big_inode_err_out;
3145 }
3146
3147 /* Register the ntfs sysctls. */
3148 err = ntfs_sysctl(1);
3149 if (err) {
3150 printk(KERN_CRIT "NTFS: Failed to register NTFS sysctls!\n");
3151 goto sysctl_err_out;
3152 }
3153
3154 err = register_filesystem(&ntfs_fs_type);
3155 if (!err) {
3156 ntfs_debug("NTFS driver registered successfully.");
3157 return 0; /* Success! */
3158 }
Anton Altaparmakovc002f422005-02-03 12:02:56 +00003159 printk(KERN_CRIT "NTFS: Failed to register NTFS filesystem driver!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
3161sysctl_err_out:
3162 kmem_cache_destroy(ntfs_big_inode_cache);
3163big_inode_err_out:
3164 kmem_cache_destroy(ntfs_inode_cache);
3165inode_err_out:
3166 kmem_cache_destroy(ntfs_name_cache);
3167name_err_out:
3168 kmem_cache_destroy(ntfs_attr_ctx_cache);
3169actx_err_out:
3170 kmem_cache_destroy(ntfs_index_ctx_cache);
3171ictx_err_out:
3172 if (!err) {
Anton Altaparmakovc002f422005-02-03 12:02:56 +00003173 printk(KERN_CRIT "NTFS: Aborting NTFS filesystem driver "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 "registration...\n");
3175 err = -ENOMEM;
3176 }
3177 return err;
3178}
3179
3180static void __exit exit_ntfs_fs(void)
3181{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 ntfs_debug("Unregistering NTFS driver.");
3183
3184 unregister_filesystem(&ntfs_fs_type);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003185 kmem_cache_destroy(ntfs_big_inode_cache);
3186 kmem_cache_destroy(ntfs_inode_cache);
3187 kmem_cache_destroy(ntfs_name_cache);
3188 kmem_cache_destroy(ntfs_attr_ctx_cache);
3189 kmem_cache_destroy(ntfs_index_ctx_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 /* Unregister the ntfs sysctls. */
3191 ntfs_sysctl(0);
3192}
3193
Anton Altaparmakov2818ef52011-01-12 10:34:35 +00003194MODULE_AUTHOR("Anton Altaparmakov <anton@tuxera.com>");
3195MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2011 Anton Altaparmakov and Tuxera Inc.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196MODULE_VERSION(NTFS_VERSION);
3197MODULE_LICENSE("GPL");
3198#ifdef DEBUG
Rusty Russell69116f22012-01-13 09:32:17 +10303199module_param(debug_msgs, bint, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200MODULE_PARM_DESC(debug_msgs, "Enable debug messages.");
3201#endif
3202
3203module_init(init_ntfs_fs)
3204module_exit(exit_ntfs_fs)