blob: 7dc3bc604f7816fd7d6f09e8a6ca7d5e8b440a02 [file] [log] [blame]
Thomas Gleixnera1d312d2019-05-22 09:51:42 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * super.c - NTFS kernel super block handling. Part of the Linux-NTFS project.
4 *
Anton Altaparmakov0e375792012-02-22 10:49:58 +00005 * Copyright (c) 2001-2012 Anton Altaparmakov and Tuxera Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (c) 2001,2002 Richard Russon
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
Fabian Frederick87c1b492014-04-07 15:36:53 -07008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
10#include <linux/stddef.h>
11#include <linux/init.h>
Anton Altaparmakov78af34f2006-02-24 10:32:33 +000012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/string.h>
14#include <linux/spinlock.h>
Martin K. Petersene1defc42009-05-22 17:17:49 -040015#include <linux/blkdev.h> /* For bdev_logical_block_size(). */
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/backing-dev.h>
17#include <linux/buffer_head.h>
18#include <linux/vfs.h>
19#include <linux/moduleparam.h>
Akinobu Mitac4af9642010-03-15 00:35:00 -040020#include <linux/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include "sysctl.h"
23#include "logfile.h"
24#include "quota.h"
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +010025#include "usnjrnl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "dir.h"
27#include "debug.h"
28#include "index.h"
Christoph Hellwiga9185b42010-03-05 09:21:37 +010029#include "inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "aops.h"
Anton Altaparmakovb0d23742005-04-04 16:20:14 +010031#include "layout.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "malloc.h"
33#include "ntfs.h"
34
Anton Altaparmakovc002f422005-02-03 12:02:56 +000035/* Number of mounted filesystems which have compression enabled. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static unsigned long ntfs_nr_compression_users;
37
38/* A global default upcase table and a corresponding reference count. */
Fabian Frederick504e0e22014-06-04 16:05:50 -070039static ntfschar *default_upcase;
40static unsigned long ntfs_nr_upcase_users;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/* Error constants/strings used in inode.c::ntfs_show_options(). */
43typedef enum {
44 /* One of these must be present, default is ON_ERRORS_CONTINUE. */
45 ON_ERRORS_PANIC = 0x01,
46 ON_ERRORS_REMOUNT_RO = 0x02,
47 ON_ERRORS_CONTINUE = 0x04,
48 /* Optional, can be combined with any of the above. */
49 ON_ERRORS_RECOVER = 0x10,
50} ON_ERRORS_ACTIONS;
51
52const option_t on_errors_arr[] = {
53 { ON_ERRORS_PANIC, "panic" },
54 { ON_ERRORS_REMOUNT_RO, "remount-ro", },
55 { ON_ERRORS_CONTINUE, "continue", },
56 { ON_ERRORS_RECOVER, "recover" },
57 { 0, NULL }
58};
59
60/**
61 * simple_getbool -
62 *
63 * Copied from old ntfs driver (which copied from vfat driver).
64 */
Richard Knutssonc49c3112006-09-30 23:27:12 -070065static int simple_getbool(char *s, bool *setval)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 if (s) {
68 if (!strcmp(s, "1") || !strcmp(s, "yes") || !strcmp(s, "true"))
Richard Knutssonc49c3112006-09-30 23:27:12 -070069 *setval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 else if (!strcmp(s, "0") || !strcmp(s, "no") ||
71 !strcmp(s, "false"))
Richard Knutssonc49c3112006-09-30 23:27:12 -070072 *setval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 else
74 return 0;
75 } else
Richard Knutssonc49c3112006-09-30 23:27:12 -070076 *setval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return 1;
78}
79
80/**
81 * parse_options - parse the (re)mount options
82 * @vol: ntfs volume
83 * @opt: string containing the (re)mount options
84 *
85 * Parse the recognized options in @opt for the ntfs volume described by @vol.
86 */
Richard Knutssonc49c3112006-09-30 23:27:12 -070087static bool parse_options(ntfs_volume *vol, char *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 char *p, *v, *ov;
90 static char *utf8 = "utf8";
91 int errors = 0, sloppy = 0;
Eric W. Biedermanb29f7752012-02-07 16:29:36 -080092 kuid_t uid = INVALID_UID;
93 kgid_t gid = INVALID_GID;
Al Virod0c00d02011-07-26 03:20:46 -040094 umode_t fmask = (umode_t)-1, dmask = (umode_t)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 int mft_zone_multiplier = -1, on_errors = -1;
Anton Altaparmakovc002f422005-02-03 12:02:56 +000096 int show_sys_files = -1, case_sensitive = -1, disable_sparse = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 struct nls_table *nls_map = NULL, *old_nls;
98
99 /* I am lazy... (-8 */
100#define NTFS_GETOPT_WITH_DEFAULT(option, variable, default_value) \
101 if (!strcmp(p, option)) { \
102 if (!v || !*v) \
103 variable = default_value; \
104 else { \
105 variable = simple_strtoul(ov = v, &v, 0); \
106 if (*v) \
107 goto needs_val; \
108 } \
109 }
110#define NTFS_GETOPT(option, variable) \
111 if (!strcmp(p, option)) { \
112 if (!v || !*v) \
113 goto needs_arg; \
114 variable = simple_strtoul(ov = v, &v, 0); \
115 if (*v) \
116 goto needs_val; \
117 }
Eric W. Biedermanb29f7752012-02-07 16:29:36 -0800118#define NTFS_GETOPT_UID(option, variable) \
119 if (!strcmp(p, option)) { \
120 uid_t uid_value; \
121 if (!v || !*v) \
122 goto needs_arg; \
123 uid_value = simple_strtoul(ov = v, &v, 0); \
124 if (*v) \
125 goto needs_val; \
126 variable = make_kuid(current_user_ns(), uid_value); \
127 if (!uid_valid(variable)) \
128 goto needs_val; \
129 }
130#define NTFS_GETOPT_GID(option, variable) \
131 if (!strcmp(p, option)) { \
132 gid_t gid_value; \
133 if (!v || !*v) \
134 goto needs_arg; \
135 gid_value = simple_strtoul(ov = v, &v, 0); \
136 if (*v) \
137 goto needs_val; \
138 variable = make_kgid(current_user_ns(), gid_value); \
139 if (!gid_valid(variable)) \
140 goto needs_val; \
141 }
Anton Altaparmakov5d467702005-09-12 14:33:47 +0100142#define NTFS_GETOPT_OCTAL(option, variable) \
143 if (!strcmp(p, option)) { \
144 if (!v || !*v) \
145 goto needs_arg; \
146 variable = simple_strtoul(ov = v, &v, 8); \
147 if (*v) \
148 goto needs_val; \
149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#define NTFS_GETOPT_BOOL(option, variable) \
151 if (!strcmp(p, option)) { \
Richard Knutssonc49c3112006-09-30 23:27:12 -0700152 bool val; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (!simple_getbool(v, &val)) \
154 goto needs_bool; \
155 variable = val; \
156 }
157#define NTFS_GETOPT_OPTIONS_ARRAY(option, variable, opt_array) \
158 if (!strcmp(p, option)) { \
159 int _i; \
160 if (!v || !*v) \
161 goto needs_arg; \
162 ov = v; \
163 if (variable == -1) \
164 variable = 0; \
165 for (_i = 0; opt_array[_i].str && *opt_array[_i].str; _i++) \
166 if (!strcmp(opt_array[_i].str, v)) { \
167 variable |= opt_array[_i].val; \
168 break; \
169 } \
170 if (!opt_array[_i].str || !*opt_array[_i].str) \
171 goto needs_val; \
172 }
173 if (!opt || !*opt)
174 goto no_mount_options;
175 ntfs_debug("Entering with mount options string: %s", opt);
176 while ((p = strsep(&opt, ","))) {
177 if ((v = strchr(p, '=')))
178 *v++ = 0;
Eric W. Biedermanb29f7752012-02-07 16:29:36 -0800179 NTFS_GETOPT_UID("uid", uid)
180 else NTFS_GETOPT_GID("gid", gid)
Anton Altaparmakov5d467702005-09-12 14:33:47 +0100181 else NTFS_GETOPT_OCTAL("umask", fmask = dmask)
182 else NTFS_GETOPT_OCTAL("fmask", fmask)
183 else NTFS_GETOPT_OCTAL("dmask", dmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 else NTFS_GETOPT("mft_zone_multiplier", mft_zone_multiplier)
Richard Knutssonc49c3112006-09-30 23:27:12 -0700185 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, true)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files)
187 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive)
Anton Altaparmakovc002f422005-02-03 12:02:56 +0000188 else NTFS_GETOPT_BOOL("disable_sparse", disable_sparse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors,
190 on_errors_arr)
191 else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes"))
192 ntfs_warning(vol->sb, "Ignoring obsolete option %s.",
193 p);
194 else if (!strcmp(p, "nls") || !strcmp(p, "iocharset")) {
195 if (!strcmp(p, "iocharset"))
196 ntfs_warning(vol->sb, "Option iocharset is "
197 "deprecated. Please use "
198 "option nls=<charsetname> in "
199 "the future.");
200 if (!v || !*v)
201 goto needs_arg;
202use_utf8:
203 old_nls = nls_map;
204 nls_map = load_nls(v);
205 if (!nls_map) {
206 if (!old_nls) {
207 ntfs_error(vol->sb, "NLS character set "
208 "%s not found.", v);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700209 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211 ntfs_error(vol->sb, "NLS character set %s not "
212 "found. Using previous one %s.",
213 v, old_nls->charset);
214 nls_map = old_nls;
215 } else /* nls_map */ {
Thomas Gleixner6d729e42009-08-16 21:05:08 +0000216 unload_nls(old_nls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218 } else if (!strcmp(p, "utf8")) {
Richard Knutssonc49c3112006-09-30 23:27:12 -0700219 bool val = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 ntfs_warning(vol->sb, "Option utf8 is no longer "
221 "supported, using option nls=utf8. Please "
222 "use option nls=utf8 in the future and "
223 "make sure utf8 is compiled either as a "
224 "module or into the kernel.");
225 if (!v || !*v)
Richard Knutssonc49c3112006-09-30 23:27:12 -0700226 val = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 else if (!simple_getbool(v, &val))
228 goto needs_bool;
229 if (val) {
230 v = utf8;
231 goto use_utf8;
232 }
233 } else {
234 ntfs_error(vol->sb, "Unrecognized mount option %s.", p);
235 if (errors < INT_MAX)
236 errors++;
237 }
238#undef NTFS_GETOPT_OPTIONS_ARRAY
239#undef NTFS_GETOPT_BOOL
240#undef NTFS_GETOPT
241#undef NTFS_GETOPT_WITH_DEFAULT
242 }
243no_mount_options:
244 if (errors && !sloppy)
Richard Knutssonc49c3112006-09-30 23:27:12 -0700245 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (sloppy)
247 ntfs_warning(vol->sb, "Sloppy option given. Ignoring "
248 "unrecognized mount option(s) and continuing.");
249 /* Keep this first! */
250 if (on_errors != -1) {
251 if (!on_errors) {
252 ntfs_error(vol->sb, "Invalid errors option argument "
253 "or bug in options parser.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700254 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256 }
257 if (nls_map) {
258 if (vol->nls_map && vol->nls_map != nls_map) {
259 ntfs_error(vol->sb, "Cannot change NLS character set "
260 "on remount.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700261 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 } /* else (!vol->nls_map) */
263 ntfs_debug("Using NLS character set %s.", nls_map->charset);
264 vol->nls_map = nls_map;
265 } else /* (!nls_map) */ {
266 if (!vol->nls_map) {
267 vol->nls_map = load_nls_default();
268 if (!vol->nls_map) {
269 ntfs_error(vol->sb, "Failed to load default "
270 "NLS character set.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700271 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273 ntfs_debug("Using default NLS character set (%s).",
274 vol->nls_map->charset);
275 }
276 }
277 if (mft_zone_multiplier != -1) {
278 if (vol->mft_zone_multiplier && vol->mft_zone_multiplier !=
279 mft_zone_multiplier) {
280 ntfs_error(vol->sb, "Cannot change mft_zone_multiplier "
281 "on remount.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700282 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284 if (mft_zone_multiplier < 1 || mft_zone_multiplier > 4) {
285 ntfs_error(vol->sb, "Invalid mft_zone_multiplier. "
286 "Using default value, i.e. 1.");
287 mft_zone_multiplier = 1;
288 }
289 vol->mft_zone_multiplier = mft_zone_multiplier;
290 }
291 if (!vol->mft_zone_multiplier)
292 vol->mft_zone_multiplier = 1;
293 if (on_errors != -1)
294 vol->on_errors = on_errors;
295 if (!vol->on_errors || vol->on_errors == ON_ERRORS_RECOVER)
296 vol->on_errors |= ON_ERRORS_CONTINUE;
Eric W. Biedermanb29f7752012-02-07 16:29:36 -0800297 if (uid_valid(uid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 vol->uid = uid;
Eric W. Biedermanb29f7752012-02-07 16:29:36 -0800299 if (gid_valid(gid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 vol->gid = gid;
Al Virod0c00d02011-07-26 03:20:46 -0400301 if (fmask != (umode_t)-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 vol->fmask = fmask;
Al Virod0c00d02011-07-26 03:20:46 -0400303 if (dmask != (umode_t)-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 vol->dmask = dmask;
305 if (show_sys_files != -1) {
306 if (show_sys_files)
307 NVolSetShowSystemFiles(vol);
308 else
309 NVolClearShowSystemFiles(vol);
310 }
311 if (case_sensitive != -1) {
312 if (case_sensitive)
313 NVolSetCaseSensitive(vol);
314 else
315 NVolClearCaseSensitive(vol);
316 }
Anton Altaparmakovc002f422005-02-03 12:02:56 +0000317 if (disable_sparse != -1) {
318 if (disable_sparse)
319 NVolClearSparseEnabled(vol);
320 else {
321 if (!NVolSparseEnabled(vol) &&
322 vol->major_ver && vol->major_ver < 3)
323 ntfs_warning(vol->sb, "Not enabling sparse "
324 "support due to NTFS volume "
325 "version %i.%i (need at least "
326 "version 3.0).", vol->major_ver,
327 vol->minor_ver);
328 else
329 NVolSetSparseEnabled(vol);
330 }
331 }
Richard Knutssonc49c3112006-09-30 23:27:12 -0700332 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333needs_arg:
334 ntfs_error(vol->sb, "The %s option requires an argument.", p);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700335 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336needs_bool:
337 ntfs_error(vol->sb, "The %s option requires a boolean argument.", p);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700338 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339needs_val:
340 ntfs_error(vol->sb, "Invalid %s option argument: %s", p, ov);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700341 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
344#ifdef NTFS_RW
345
346/**
347 * ntfs_write_volume_flags - write new flags to the volume information flags
348 * @vol: ntfs volume on which to modify the flags
349 * @flags: new flags value for the volume information flags
350 *
351 * Internal function. You probably want to use ntfs_{set,clear}_volume_flags()
352 * instead (see below).
353 *
354 * Replace the volume information flags on the volume @vol with the value
355 * supplied in @flags. Note, this overwrites the volume information flags, so
356 * make sure to combine the flags you want to modify with the old flags and use
357 * the result when calling ntfs_write_volume_flags().
358 *
359 * Return 0 on success and -errno on error.
360 */
361static int ntfs_write_volume_flags(ntfs_volume *vol, const VOLUME_FLAGS flags)
362{
363 ntfs_inode *ni = NTFS_I(vol->vol_ino);
364 MFT_RECORD *m;
365 VOLUME_INFORMATION *vi;
366 ntfs_attr_search_ctx *ctx;
367 int err;
368
369 ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
370 le16_to_cpu(vol->vol_flags), le16_to_cpu(flags));
371 if (vol->vol_flags == flags)
372 goto done;
373 BUG_ON(!ni);
374 m = map_mft_record(ni);
375 if (IS_ERR(m)) {
376 err = PTR_ERR(m);
377 goto err_out;
378 }
379 ctx = ntfs_attr_get_search_ctx(ni, m);
380 if (!ctx) {
381 err = -ENOMEM;
382 goto put_unm_err_out;
383 }
384 err = ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
385 ctx);
386 if (err)
387 goto put_unm_err_out;
388 vi = (VOLUME_INFORMATION*)((u8*)ctx->attr +
389 le16_to_cpu(ctx->attr->data.resident.value_offset));
390 vol->vol_flags = vi->flags = flags;
391 flush_dcache_mft_record_page(ctx->ntfs_ino);
392 mark_mft_record_dirty(ctx->ntfs_ino);
393 ntfs_attr_put_search_ctx(ctx);
394 unmap_mft_record(ni);
395done:
396 ntfs_debug("Done.");
397 return 0;
398put_unm_err_out:
399 if (ctx)
400 ntfs_attr_put_search_ctx(ctx);
401 unmap_mft_record(ni);
402err_out:
403 ntfs_error(vol->sb, "Failed with error code %i.", -err);
404 return err;
405}
406
407/**
408 * ntfs_set_volume_flags - set bits in the volume information flags
409 * @vol: ntfs volume on which to modify the flags
410 * @flags: flags to set on the volume
411 *
412 * Set the bits in @flags in the volume information flags on the volume @vol.
413 *
414 * Return 0 on success and -errno on error.
415 */
416static inline int ntfs_set_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
417{
418 flags &= VOLUME_FLAGS_MASK;
419 return ntfs_write_volume_flags(vol, vol->vol_flags | flags);
420}
421
422/**
423 * ntfs_clear_volume_flags - clear bits in the volume information flags
424 * @vol: ntfs volume on which to modify the flags
425 * @flags: flags to clear on the volume
426 *
427 * Clear the bits in @flags in the volume information flags on the volume @vol.
428 *
429 * Return 0 on success and -errno on error.
430 */
431static inline int ntfs_clear_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
432{
433 flags &= VOLUME_FLAGS_MASK;
434 flags = vol->vol_flags & cpu_to_le16(~le16_to_cpu(flags));
435 return ntfs_write_volume_flags(vol, flags);
436}
437
438#endif /* NTFS_RW */
439
440/**
441 * ntfs_remount - change the mount options of a mounted ntfs filesystem
442 * @sb: superblock of mounted ntfs filesystem
443 * @flags: remount flags
444 * @opt: remount options string
445 *
446 * Change the mount options of an already mounted ntfs filesystem.
447 *
448 * NOTE: The VFS sets the @sb->s_flags remount flags to @flags after
449 * ntfs_remount() returns successfully (i.e. returns 0). Otherwise,
450 * @sb->s_flags are not changed.
451 */
452static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
453{
454 ntfs_volume *vol = NTFS_SB(sb);
455
456 ntfs_debug("Entering with remount options string: %s", opt);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200457
Theodore Ts'o02b99842014-03-13 10:14:33 -0400458 sync_filesystem(sb);
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460#ifndef NTFS_RW
Christoph Hellwig24a44dc2006-01-09 20:52:14 -0800461 /* For read-only compiled driver, enforce read-only flag. */
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800462 *flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463#else /* NTFS_RW */
464 /*
465 * For the read-write compiled driver, if we are remounting read-write,
466 * make sure there are no volume errors and that no unsupported volume
467 * flags are set. Also, empty the logfile journal as it would become
468 * stale as soon as something is written to the volume and mark the
469 * volume dirty so that chkdsk is run if the volume is not umounted
470 * cleanly. Finally, mark the quotas out of date so Windows rescans
471 * the volume on boot and updates them.
472 *
473 * When remounting read-only, mark the volume clean if no volume errors
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300474 * have occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800476 if (sb_rdonly(sb) && !(*flags & SB_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 static const char *es = ". Cannot remount read-write.";
478
479 /* Remounting read-write. */
480 if (NVolErrors(vol)) {
481 ntfs_error(sb, "Volume has errors and is read-only%s",
482 es);
483 return -EROFS;
484 }
485 if (vol->vol_flags & VOLUME_IS_DIRTY) {
486 ntfs_error(sb, "Volume is dirty and read-only%s", es);
487 return -EROFS;
488 }
Anton Altaparmakov1cf3109f2006-02-24 10:48:14 +0000489 if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
490 ntfs_error(sb, "Volume has been modified by chkdsk "
491 "and is read-only%s", es);
492 return -EROFS;
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
Anton Altaparmakov1cf3109f2006-02-24 10:48:14 +0000495 ntfs_error(sb, "Volume has unsupported flags set "
496 "(0x%x) and is read-only%s",
497 (unsigned)le16_to_cpu(vol->vol_flags),
498 es);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return -EROFS;
500 }
501 if (ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
502 ntfs_error(sb, "Failed to set dirty bit in volume "
503 "information flags%s", es);
504 return -EROFS;
505 }
506#if 0
507 // TODO: Enable this code once we start modifying anything that
508 // is different between NTFS 1.2 and 3.x...
509 /* Set NT4 compatibility flag on newer NTFS version volumes. */
510 if ((vol->major_ver > 1)) {
511 if (ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
512 ntfs_error(sb, "Failed to set NT4 "
513 "compatibility flag%s", es);
514 NVolSetErrors(vol);
515 return -EROFS;
516 }
517 }
518#endif
519 if (!ntfs_empty_logfile(vol->logfile_ino)) {
520 ntfs_error(sb, "Failed to empty journal $LogFile%s",
521 es);
522 NVolSetErrors(vol);
523 return -EROFS;
524 }
525 if (!ntfs_mark_quotas_out_of_date(vol)) {
526 ntfs_error(sb, "Failed to mark quotas out of date%s",
527 es);
528 NVolSetErrors(vol);
529 return -EROFS;
530 }
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +0100531 if (!ntfs_stamp_usnjrnl(vol)) {
Nik Nybyd31e7712015-06-29 15:24:38 -0400532 ntfs_error(sb, "Failed to stamp transaction log "
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +0100533 "($UsnJrnl)%s", es);
534 NVolSetErrors(vol);
535 return -EROFS;
536 }
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800537 } else if (!sb_rdonly(sb) && (*flags & SB_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* Remounting read-only. */
539 if (!NVolErrors(vol)) {
540 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
541 ntfs_warning(sb, "Failed to clear dirty bit "
542 "in volume information "
543 "flags. Run chkdsk.");
544 }
545 }
546#endif /* NTFS_RW */
547
548 // TODO: Deal with *flags.
549
Jan Blunckefdffb52010-02-24 13:25:33 +0100550 if (!parse_options(vol, opt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return -EINVAL;
Jan Blunckefdffb52010-02-24 13:25:33 +0100552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 ntfs_debug("Done.");
554 return 0;
555}
556
557/**
558 * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector
559 * @sb: Super block of the device to which @b belongs.
560 * @b: Boot sector of device @sb to check.
Richard Knutssonc49c3112006-09-30 23:27:12 -0700561 * @silent: If 'true', all output will be silenced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 *
563 * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot
Richard Knutssonc49c3112006-09-30 23:27:12 -0700564 * sector. Returns 'true' if it is valid and 'false' if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 *
566 * @sb is only needed for warning/error output, i.e. it can be NULL when silent
Richard Knutssonc49c3112006-09-30 23:27:12 -0700567 * is 'true'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
Richard Knutssonc49c3112006-09-30 23:27:12 -0700569static bool is_boot_sector_ntfs(const struct super_block *sb,
570 const NTFS_BOOT_SECTOR *b, const bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 /*
573 * Check that checksum == sum of u32 values from b to the checksum
Anton Altaparmakovb0d23742005-04-04 16:20:14 +0100574 * field. If checksum is zero, no checking is done. We will work when
575 * the checksum test fails, since some utilities update the boot sector
576 * ignoring the checksum which leaves the checksum out-of-date. We
577 * report a warning if this is the case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 */
Anton Altaparmakovb0d23742005-04-04 16:20:14 +0100579 if ((void*)b < (void*)&b->checksum && b->checksum && !silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 le32 *u;
581 u32 i;
582
583 for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u)
584 i += le32_to_cpup(u);
585 if (le32_to_cpu(b->checksum) != i)
Anton Altaparmakovb0d23742005-04-04 16:20:14 +0100586 ntfs_warning(sb, "Invalid boot sector checksum.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588 /* Check OEMidentifier is "NTFS " */
589 if (b->oem_id != magicNTFS)
590 goto not_ntfs;
591 /* Check bytes per sector value is between 256 and 4096. */
592 if (le16_to_cpu(b->bpb.bytes_per_sector) < 0x100 ||
593 le16_to_cpu(b->bpb.bytes_per_sector) > 0x1000)
594 goto not_ntfs;
595 /* Check sectors per cluster value is valid. */
596 switch (b->bpb.sectors_per_cluster) {
597 case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128:
598 break;
599 default:
600 goto not_ntfs;
601 }
Anton Altaparmakov7fafb8b2005-04-06 16:09:21 +0100602 /* Check the cluster size is not above the maximum (64kiB). */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) *
Anton Altaparmakov7fafb8b2005-04-06 16:09:21 +0100604 b->bpb.sectors_per_cluster > NTFS_MAX_CLUSTER_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 goto not_ntfs;
606 /* Check reserved/unused fields are really zero. */
607 if (le16_to_cpu(b->bpb.reserved_sectors) ||
608 le16_to_cpu(b->bpb.root_entries) ||
609 le16_to_cpu(b->bpb.sectors) ||
610 le16_to_cpu(b->bpb.sectors_per_fat) ||
611 le32_to_cpu(b->bpb.large_sectors) || b->bpb.fats)
612 goto not_ntfs;
613 /* Check clusters per file mft record value is valid. */
614 if ((u8)b->clusters_per_mft_record < 0xe1 ||
615 (u8)b->clusters_per_mft_record > 0xf7)
616 switch (b->clusters_per_mft_record) {
617 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
618 break;
619 default:
620 goto not_ntfs;
621 }
622 /* Check clusters per index block value is valid. */
623 if ((u8)b->clusters_per_index_record < 0xe1 ||
624 (u8)b->clusters_per_index_record > 0xf7)
625 switch (b->clusters_per_index_record) {
626 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
627 break;
628 default:
629 goto not_ntfs;
630 }
631 /*
632 * Check for valid end of sector marker. We will work without it, but
633 * many BIOSes will refuse to boot from a bootsector if the magic is
634 * incorrect, so we emit a warning.
635 */
Harvey Harrison63cd88542009-03-31 15:23:52 -0700636 if (!silent && b->end_of_sector_marker != cpu_to_le16(0xaa55))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 ntfs_warning(sb, "Invalid end of sector marker.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700638 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639not_ntfs:
Richard Knutssonc49c3112006-09-30 23:27:12 -0700640 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643/**
644 * read_ntfs_boot_sector - read the NTFS boot sector of a device
645 * @sb: super block of device to read the boot sector from
646 * @silent: if true, suppress all output
647 *
648 * Reads the boot sector from the device and validates it. If that fails, tries
649 * to read the backup boot sector, first from the end of the device a-la NT4 and
650 * later and then from the middle of the device a-la NT3.51 and before.
651 *
652 * If a valid boot sector is found but it is not the primary boot sector, we
653 * repair the primary boot sector silently (unless the device is read-only or
654 * the primary boot sector is not accessible).
655 *
656 * NOTE: To call this function, @sb must have the fields s_dev, the ntfs super
657 * block (u.ntfs_sb), nr_blocks and the device flags (s_flags) initialized
658 * to their respective values.
659 *
660 * Return the unlocked buffer head containing the boot sector or NULL on error.
661 */
662static struct buffer_head *read_ntfs_boot_sector(struct super_block *sb,
663 const int silent)
664{
665 const char *read_err_str = "Unable to read %s boot sector.";
666 struct buffer_head *bh_primary, *bh_backup;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000667 sector_t nr_blocks = NTFS_SB(sb)->nr_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 /* Try to read primary boot sector. */
670 if ((bh_primary = sb_bread(sb, 0))) {
671 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
672 bh_primary->b_data, silent))
673 return bh_primary;
674 if (!silent)
675 ntfs_error(sb, "Primary boot sector is invalid.");
676 } else if (!silent)
677 ntfs_error(sb, read_err_str, "primary");
678 if (!(NTFS_SB(sb)->on_errors & ON_ERRORS_RECOVER)) {
679 if (bh_primary)
680 brelse(bh_primary);
681 if (!silent)
682 ntfs_error(sb, "Mount option errors=recover not used. "
683 "Aborting without trying to recover.");
684 return NULL;
685 }
686 /* Try to read NT4+ backup boot sector. */
687 if ((bh_backup = sb_bread(sb, nr_blocks - 1))) {
688 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
689 bh_backup->b_data, silent))
690 goto hotfix_primary_boot_sector;
691 brelse(bh_backup);
692 } else if (!silent)
693 ntfs_error(sb, read_err_str, "backup");
694 /* Try to read NT3.51- backup boot sector. */
695 if ((bh_backup = sb_bread(sb, nr_blocks >> 1))) {
696 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
697 bh_backup->b_data, silent))
698 goto hotfix_primary_boot_sector;
699 if (!silent)
700 ntfs_error(sb, "Could not find a valid backup boot "
701 "sector.");
702 brelse(bh_backup);
703 } else if (!silent)
704 ntfs_error(sb, read_err_str, "backup");
705 /* We failed. Cleanup and return. */
706 if (bh_primary)
707 brelse(bh_primary);
708 return NULL;
709hotfix_primary_boot_sector:
710 if (bh_primary) {
711 /*
712 * If we managed to read sector zero and the volume is not
713 * read-only, copy the found, valid backup boot sector to the
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000714 * primary boot sector. Note we only copy the actual boot
715 * sector structure, not the actual whole device sector as that
716 * may be bigger and would potentially damage the $Boot system
717 * file (FIXME: Would be nice to know if the backup boot sector
718 * on a large sector device contains the whole boot loader or
719 * just the first 512 bytes).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
David Howellsbc98a422017-07-17 08:45:34 +0100721 if (!sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 ntfs_warning(sb, "Hot-fix: Recovering invalid primary "
723 "boot sector from backup copy.");
724 memcpy(bh_primary->b_data, bh_backup->b_data,
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000725 NTFS_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 mark_buffer_dirty(bh_primary);
727 sync_dirty_buffer(bh_primary);
728 if (buffer_uptodate(bh_primary)) {
729 brelse(bh_backup);
730 return bh_primary;
731 }
732 ntfs_error(sb, "Hot-fix: Device write error while "
733 "recovering primary boot sector.");
734 } else {
735 ntfs_warning(sb, "Hot-fix: Recovery of primary boot "
736 "sector failed: Read-only mount.");
737 }
738 brelse(bh_primary);
739 }
740 ntfs_warning(sb, "Using backup boot sector.");
741 return bh_backup;
742}
743
744/**
745 * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol
746 * @vol: volume structure to initialise with data from boot sector
747 * @b: boot sector to parse
748 *
749 * Parse the ntfs boot sector @b and store all imporant information therein in
Richard Knutssonc49c3112006-09-30 23:27:12 -0700750 * the ntfs super block @vol. Return 'true' on success and 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 */
Richard Knutssonc49c3112006-09-30 23:27:12 -0700752static bool parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 unsigned int sectors_per_cluster_bits, nr_hidden_sects;
755 int clusters_per_mft_record, clusters_per_index_record;
756 s64 ll;
757
758 vol->sector_size = le16_to_cpu(b->bpb.bytes_per_sector);
759 vol->sector_size_bits = ffs(vol->sector_size) - 1;
760 ntfs_debug("vol->sector_size = %i (0x%x)", vol->sector_size,
761 vol->sector_size);
762 ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits,
763 vol->sector_size_bits);
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000764 if (vol->sector_size < vol->sb->s_blocksize) {
765 ntfs_error(vol->sb, "Sector size (%i) is smaller than the "
766 "device block size (%lu). This is not "
767 "supported. Sorry.", vol->sector_size,
768 vol->sb->s_blocksize);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700769 return false;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster);
772 sectors_per_cluster_bits = ffs(b->bpb.sectors_per_cluster) - 1;
773 ntfs_debug("sectors_per_cluster_bits = 0x%x",
774 sectors_per_cluster_bits);
775 nr_hidden_sects = le32_to_cpu(b->bpb.hidden_sectors);
776 ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects);
777 vol->cluster_size = vol->sector_size << sectors_per_cluster_bits;
778 vol->cluster_size_mask = vol->cluster_size - 1;
779 vol->cluster_size_bits = ffs(vol->cluster_size) - 1;
780 ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size,
781 vol->cluster_size);
782 ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask);
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000783 ntfs_debug("vol->cluster_size_bits = %i", vol->cluster_size_bits);
784 if (vol->cluster_size < vol->sector_size) {
785 ntfs_error(vol->sb, "Cluster size (%i) is smaller than the "
786 "sector size (%i). This is not supported. "
787 "Sorry.", vol->cluster_size, vol->sector_size);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700788 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790 clusters_per_mft_record = b->clusters_per_mft_record;
791 ntfs_debug("clusters_per_mft_record = %i (0x%x)",
792 clusters_per_mft_record, clusters_per_mft_record);
793 if (clusters_per_mft_record > 0)
794 vol->mft_record_size = vol->cluster_size <<
795 (ffs(clusters_per_mft_record) - 1);
796 else
797 /*
798 * When mft_record_size < cluster_size, clusters_per_mft_record
799 * = -log2(mft_record_size) bytes. mft_record_size normaly is
800 * 1024 bytes, which is encoded as 0xF6 (-10 in decimal).
801 */
802 vol->mft_record_size = 1 << -clusters_per_mft_record;
803 vol->mft_record_size_mask = vol->mft_record_size - 1;
804 vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1;
805 ntfs_debug("vol->mft_record_size = %i (0x%x)", vol->mft_record_size,
806 vol->mft_record_size);
807 ntfs_debug("vol->mft_record_size_mask = 0x%x",
808 vol->mft_record_size_mask);
809 ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
810 vol->mft_record_size_bits, vol->mft_record_size_bits);
811 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300812 * We cannot support mft record sizes above the PAGE_SIZE since
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 * we store $MFT/$DATA, the table of mft records in the page cache.
814 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300815 if (vol->mft_record_size > PAGE_SIZE) {
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000816 ntfs_error(vol->sb, "Mft record size (%i) exceeds the "
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300817 "PAGE_SIZE on your system (%lu). "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 "This is not supported. Sorry.",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300819 vol->mft_record_size, PAGE_SIZE);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700820 return false;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000821 }
822 /* We cannot support mft record sizes below the sector size. */
823 if (vol->mft_record_size < vol->sector_size) {
824 ntfs_error(vol->sb, "Mft record size (%i) is smaller than the "
825 "sector size (%i). This is not supported. "
826 "Sorry.", vol->mft_record_size,
827 vol->sector_size);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700828 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830 clusters_per_index_record = b->clusters_per_index_record;
831 ntfs_debug("clusters_per_index_record = %i (0x%x)",
832 clusters_per_index_record, clusters_per_index_record);
833 if (clusters_per_index_record > 0)
834 vol->index_record_size = vol->cluster_size <<
835 (ffs(clusters_per_index_record) - 1);
836 else
837 /*
838 * When index_record_size < cluster_size,
839 * clusters_per_index_record = -log2(index_record_size) bytes.
840 * index_record_size normaly equals 4096 bytes, which is
841 * encoded as 0xF4 (-12 in decimal).
842 */
843 vol->index_record_size = 1 << -clusters_per_index_record;
844 vol->index_record_size_mask = vol->index_record_size - 1;
845 vol->index_record_size_bits = ffs(vol->index_record_size) - 1;
846 ntfs_debug("vol->index_record_size = %i (0x%x)",
847 vol->index_record_size, vol->index_record_size);
848 ntfs_debug("vol->index_record_size_mask = 0x%x",
849 vol->index_record_size_mask);
850 ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
851 vol->index_record_size_bits,
852 vol->index_record_size_bits);
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000853 /* We cannot support index record sizes below the sector size. */
854 if (vol->index_record_size < vol->sector_size) {
855 ntfs_error(vol->sb, "Index record size (%i) is smaller than "
856 "the sector size (%i). This is not "
857 "supported. Sorry.", vol->index_record_size,
858 vol->sector_size);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700859 return false;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /*
862 * Get the size of the volume in clusters and check for 64-bit-ness.
863 * Windows currently only uses 32 bits to save the clusters so we do
864 * the same as it is much faster on 32-bit CPUs.
865 */
866 ll = sle64_to_cpu(b->number_of_sectors) >> sectors_per_cluster_bits;
867 if ((u64)ll >= 1ULL << 32) {
868 ntfs_error(vol->sb, "Cannot handle 64-bit clusters. Sorry.");
Richard Knutssonc49c3112006-09-30 23:27:12 -0700869 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871 vol->nr_clusters = ll;
872 ntfs_debug("vol->nr_clusters = 0x%llx", (long long)vol->nr_clusters);
873 /*
874 * On an architecture where unsigned long is 32-bits, we restrict the
875 * volume size to 2TiB (2^41). On a 64-bit architecture, the compiler
876 * will hopefully optimize the whole check away.
877 */
878 if (sizeof(unsigned long) < 8) {
879 if ((ll << vol->cluster_size_bits) >= (1ULL << 41)) {
880 ntfs_error(vol->sb, "Volume size (%lluTiB) is too "
881 "large for this architecture. "
882 "Maximum supported is 2TiB. Sorry.",
883 (unsigned long long)ll >> (40 -
884 vol->cluster_size_bits));
Richard Knutssonc49c3112006-09-30 23:27:12 -0700885 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887 }
888 ll = sle64_to_cpu(b->mft_lcn);
889 if (ll >= vol->nr_clusters) {
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000890 ntfs_error(vol->sb, "MFT LCN (%lli, 0x%llx) is beyond end of "
891 "volume. Weird.", (unsigned long long)ll,
892 (unsigned long long)ll);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700893 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895 vol->mft_lcn = ll;
896 ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol->mft_lcn);
897 ll = sle64_to_cpu(b->mftmirr_lcn);
898 if (ll >= vol->nr_clusters) {
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000899 ntfs_error(vol->sb, "MFTMirr LCN (%lli, 0x%llx) is beyond end "
900 "of volume. Weird.", (unsigned long long)ll,
901 (unsigned long long)ll);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700902 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904 vol->mftmirr_lcn = ll;
905 ntfs_debug("vol->mftmirr_lcn = 0x%llx", (long long)vol->mftmirr_lcn);
906#ifdef NTFS_RW
907 /*
908 * Work out the size of the mft mirror in number of mft records. If the
909 * cluster size is less than or equal to the size taken by four mft
910 * records, the mft mirror stores the first four mft records. If the
911 * cluster size is bigger than the size taken by four mft records, the
912 * mft mirror contains as many mft records as will fit into one
913 * cluster.
914 */
915 if (vol->cluster_size <= (4 << vol->mft_record_size_bits))
916 vol->mftmirr_size = 4;
917 else
918 vol->mftmirr_size = vol->cluster_size >>
919 vol->mft_record_size_bits;
920 ntfs_debug("vol->mftmirr_size = %i", vol->mftmirr_size);
921#endif /* NTFS_RW */
922 vol->serial_no = le64_to_cpu(b->volume_serial_number);
923 ntfs_debug("vol->serial_no = 0x%llx",
924 (unsigned long long)vol->serial_no);
Richard Knutssonc49c3112006-09-30 23:27:12 -0700925 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
928/**
929 * ntfs_setup_allocators - initialize the cluster and mft allocators
930 * @vol: volume structure for which to setup the allocators
931 *
932 * Setup the cluster (lcn) and mft allocators to the starting values.
933 */
934static void ntfs_setup_allocators(ntfs_volume *vol)
935{
936#ifdef NTFS_RW
937 LCN mft_zone_size, mft_lcn;
938#endif /* NTFS_RW */
939
940 ntfs_debug("vol->mft_zone_multiplier = 0x%x",
941 vol->mft_zone_multiplier);
942#ifdef NTFS_RW
943 /* Determine the size of the MFT zone. */
944 mft_zone_size = vol->nr_clusters;
945 switch (vol->mft_zone_multiplier) { /* % of volume size in clusters */
946 case 4:
947 mft_zone_size >>= 1; /* 50% */
948 break;
949 case 3:
950 mft_zone_size = (mft_zone_size +
951 (mft_zone_size >> 1)) >> 2; /* 37.5% */
952 break;
953 case 2:
954 mft_zone_size >>= 2; /* 25% */
955 break;
956 /* case 1: */
957 default:
958 mft_zone_size >>= 3; /* 12.5% */
959 break;
960 }
961 /* Setup the mft zone. */
962 vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn;
963 ntfs_debug("vol->mft_zone_pos = 0x%llx",
964 (unsigned long long)vol->mft_zone_pos);
965 /*
966 * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
967 * source) and if the actual mft_lcn is in the expected place or even
968 * further to the front of the volume, extend the mft_zone to cover the
969 * beginning of the volume as well. This is in order to protect the
970 * area reserved for the mft bitmap as well within the mft_zone itself.
971 * On non-standard volumes we do not protect it as the overhead would
972 * be higher than the speed increase we would get by doing it.
973 */
974 mft_lcn = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size;
975 if (mft_lcn * vol->cluster_size < 16 * 1024)
976 mft_lcn = (16 * 1024 + vol->cluster_size - 1) /
977 vol->cluster_size;
978 if (vol->mft_zone_start <= mft_lcn)
979 vol->mft_zone_start = 0;
980 ntfs_debug("vol->mft_zone_start = 0x%llx",
981 (unsigned long long)vol->mft_zone_start);
982 /*
983 * Need to cap the mft zone on non-standard volumes so that it does
984 * not point outside the boundaries of the volume. We do this by
985 * halving the zone size until we are inside the volume.
986 */
987 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
988 while (vol->mft_zone_end >= vol->nr_clusters) {
989 mft_zone_size >>= 1;
990 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
991 }
992 ntfs_debug("vol->mft_zone_end = 0x%llx",
993 (unsigned long long)vol->mft_zone_end);
994 /*
995 * Set the current position within each data zone to the start of the
996 * respective zone.
997 */
998 vol->data1_zone_pos = vol->mft_zone_end;
999 ntfs_debug("vol->data1_zone_pos = 0x%llx",
1000 (unsigned long long)vol->data1_zone_pos);
1001 vol->data2_zone_pos = 0;
1002 ntfs_debug("vol->data2_zone_pos = 0x%llx",
1003 (unsigned long long)vol->data2_zone_pos);
1004
1005 /* Set the mft data allocation position to mft record 24. */
1006 vol->mft_data_pos = 24;
1007 ntfs_debug("vol->mft_data_pos = 0x%llx",
1008 (unsigned long long)vol->mft_data_pos);
1009#endif /* NTFS_RW */
1010}
1011
1012#ifdef NTFS_RW
1013
1014/**
1015 * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume
1016 * @vol: ntfs super block describing device whose mft mirror to load
1017 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001018 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001020static bool load_and_init_mft_mirror(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 struct inode *tmp_ino;
1023 ntfs_inode *tmp_ni;
1024
1025 ntfs_debug("Entering.");
1026 /* Get mft mirror inode. */
1027 tmp_ino = ntfs_iget(vol->sb, FILE_MFTMirr);
1028 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1029 if (!IS_ERR(tmp_ino))
1030 iput(tmp_ino);
1031 /* Caller will display error message. */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001032 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034 /*
1035 * Re-initialize some specifics about $MFTMirr's inode as
1036 * ntfs_read_inode() will have set up the default ones.
1037 */
1038 /* Set uid and gid to root. */
Eric W. Biedermanb29f7752012-02-07 16:29:36 -08001039 tmp_ino->i_uid = GLOBAL_ROOT_UID;
1040 tmp_ino->i_gid = GLOBAL_ROOT_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /* Regular file. No access for anyone. */
1042 tmp_ino->i_mode = S_IFREG;
1043 /* No VFS initiated operations allowed for $MFTMirr. */
1044 tmp_ino->i_op = &ntfs_empty_inode_ops;
1045 tmp_ino->i_fop = &ntfs_empty_file_ops;
1046 /* Put in our special address space operations. */
1047 tmp_ino->i_mapping->a_ops = &ntfs_mst_aops;
1048 tmp_ni = NTFS_I(tmp_ino);
1049 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
1050 NInoSetMstProtected(tmp_ni);
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001051 NInoSetSparseDisabled(tmp_ni);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 /*
1053 * Set up our little cheat allowing us to reuse the async read io
1054 * completion handler for directories.
1055 */
1056 tmp_ni->itype.index.block_size = vol->mft_record_size;
1057 tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits;
1058 vol->mftmirr_ino = tmp_ino;
1059 ntfs_debug("Done.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001060 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
1063/**
1064 * check_mft_mirror - compare contents of the mft mirror with the mft
1065 * @vol: ntfs super block describing device whose mft mirror to check
1066 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001067 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 *
1069 * Note, this function also results in the mft mirror runlist being completely
1070 * mapped into memory. The mft mirror write code requires this and will BUG()
1071 * should it find an unmapped runlist element.
1072 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001073static bool check_mft_mirror(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 struct super_block *sb = vol->sb;
1076 ntfs_inode *mirr_ni;
1077 struct page *mft_page, *mirr_page;
1078 u8 *kmft, *kmirr;
1079 runlist_element *rl, rl2[2];
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001080 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 int mrecs_per_page, i;
1082
1083 ntfs_debug("Entering.");
1084 /* Compare contents of $MFT and $MFTMirr. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001085 mrecs_per_page = PAGE_SIZE / vol->mft_record_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 BUG_ON(!mrecs_per_page);
1087 BUG_ON(!vol->mftmirr_size);
1088 mft_page = mirr_page = NULL;
1089 kmft = kmirr = NULL;
1090 index = i = 0;
1091 do {
1092 u32 bytes;
1093
1094 /* Switch pages if necessary. */
1095 if (!(i % mrecs_per_page)) {
1096 if (index) {
1097 ntfs_unmap_page(mft_page);
1098 ntfs_unmap_page(mirr_page);
1099 }
1100 /* Get the $MFT page. */
1101 mft_page = ntfs_map_page(vol->mft_ino->i_mapping,
1102 index);
1103 if (IS_ERR(mft_page)) {
1104 ntfs_error(sb, "Failed to read $MFT.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001105 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107 kmft = page_address(mft_page);
1108 /* Get the $MFTMirr page. */
1109 mirr_page = ntfs_map_page(vol->mftmirr_ino->i_mapping,
1110 index);
1111 if (IS_ERR(mirr_page)) {
1112 ntfs_error(sb, "Failed to read $MFTMirr.");
1113 goto mft_unmap_out;
1114 }
1115 kmirr = page_address(mirr_page);
1116 ++index;
1117 }
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001118 /* Do not check the record if it is not in use. */
1119 if (((MFT_RECORD*)kmft)->flags & MFT_RECORD_IN_USE) {
1120 /* Make sure the record is ok. */
1121 if (ntfs_is_baad_recordp((le32*)kmft)) {
1122 ntfs_error(sb, "Incomplete multi sector "
1123 "transfer detected in mft "
1124 "record %i.", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125mm_unmap_out:
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001126 ntfs_unmap_page(mirr_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127mft_unmap_out:
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001128 ntfs_unmap_page(mft_page);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001129 return false;
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001132 /* Do not check the mirror record if it is not in use. */
1133 if (((MFT_RECORD*)kmirr)->flags & MFT_RECORD_IN_USE) {
1134 if (ntfs_is_baad_recordp((le32*)kmirr)) {
1135 ntfs_error(sb, "Incomplete multi sector "
1136 "transfer detected in mft "
1137 "mirror record %i.", i);
1138 goto mm_unmap_out;
1139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141 /* Get the amount of data in the current record. */
1142 bytes = le32_to_cpu(((MFT_RECORD*)kmft)->bytes_in_use);
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001143 if (bytes < sizeof(MFT_RECORD_OLD) ||
1144 bytes > vol->mft_record_size ||
1145 ntfs_is_baad_recordp((le32*)kmft)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 bytes = le32_to_cpu(((MFT_RECORD*)kmirr)->bytes_in_use);
Anton Altaparmakov949763b2006-03-23 15:34:13 +00001147 if (bytes < sizeof(MFT_RECORD_OLD) ||
1148 bytes > vol->mft_record_size ||
1149 ntfs_is_baad_recordp((le32*)kmirr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 bytes = vol->mft_record_size;
1151 }
1152 /* Compare the two records. */
1153 if (memcmp(kmft, kmirr, bytes)) {
1154 ntfs_error(sb, "$MFT and $MFTMirr (record %i) do not "
1155 "match. Run ntfsfix or chkdsk.", i);
1156 goto mm_unmap_out;
1157 }
1158 kmft += vol->mft_record_size;
1159 kmirr += vol->mft_record_size;
1160 } while (++i < vol->mftmirr_size);
1161 /* Release the last pages. */
1162 ntfs_unmap_page(mft_page);
1163 ntfs_unmap_page(mirr_page);
1164
1165 /* Construct the mft mirror runlist by hand. */
1166 rl2[0].vcn = 0;
1167 rl2[0].lcn = vol->mftmirr_lcn;
1168 rl2[0].length = (vol->mftmirr_size * vol->mft_record_size +
1169 vol->cluster_size - 1) / vol->cluster_size;
1170 rl2[1].vcn = rl2[0].length;
1171 rl2[1].lcn = LCN_ENOENT;
1172 rl2[1].length = 0;
1173 /*
1174 * Because we have just read all of the mft mirror, we know we have
1175 * mapped the full runlist for it.
1176 */
1177 mirr_ni = NTFS_I(vol->mftmirr_ino);
1178 down_read(&mirr_ni->runlist.lock);
1179 rl = mirr_ni->runlist.rl;
1180 /* Compare the two runlists. They must be identical. */
1181 i = 0;
1182 do {
1183 if (rl2[i].vcn != rl[i].vcn || rl2[i].lcn != rl[i].lcn ||
1184 rl2[i].length != rl[i].length) {
1185 ntfs_error(sb, "$MFTMirr location mismatch. "
1186 "Run chkdsk.");
1187 up_read(&mirr_ni->runlist.lock);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001188 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190 } while (rl2[i++].length);
1191 up_read(&mirr_ni->runlist.lock);
1192 ntfs_debug("Done.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001193 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
1196/**
1197 * load_and_check_logfile - load and check the logfile inode for a volume
1198 * @vol: ntfs super block describing device whose logfile to load
1199 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001200 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001202static bool load_and_check_logfile(ntfs_volume *vol,
Anton Altaparmakove7a10332005-09-08 16:12:28 +01001203 RESTART_PAGE_HEADER **rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
1205 struct inode *tmp_ino;
1206
1207 ntfs_debug("Entering.");
1208 tmp_ino = ntfs_iget(vol->sb, FILE_LogFile);
1209 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1210 if (!IS_ERR(tmp_ino))
1211 iput(tmp_ino);
1212 /* Caller will display error message. */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001213 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
Anton Altaparmakove7a10332005-09-08 16:12:28 +01001215 if (!ntfs_check_logfile(tmp_ino, rp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 iput(tmp_ino);
1217 /* ntfs_check_logfile() will have displayed error output. */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001218 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001220 NInoSetSparseDisabled(NTFS_I(tmp_ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 vol->logfile_ino = tmp_ino;
1222 ntfs_debug("Done.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001223 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001226#define NTFS_HIBERFIL_HEADER_SIZE 4096
1227
1228/**
1229 * check_windows_hibernation_status - check if Windows is suspended on a volume
1230 * @vol: ntfs super block of device to check
1231 *
1232 * Check if Windows is hibernated on the ntfs volume @vol. This is done by
1233 * looking for the file hiberfil.sys in the root directory of the volume. If
1234 * the file is not present Windows is definitely not suspended.
1235 *
1236 * If hiberfil.sys exists and is less than 4kiB in size it means Windows is
1237 * definitely suspended (this volume is not the system volume). Caveat: on a
1238 * system with many volumes it is possible that the < 4kiB check is bogus but
1239 * for now this should do fine.
1240 *
1241 * If hiberfil.sys exists and is larger than 4kiB in size, we need to read the
1242 * hiberfil header (which is the first 4kiB). If this begins with "hibr",
1243 * Windows is definitely suspended. If it is completely full of zeroes,
1244 * Windows is definitely not hibernated. Any other case is treated as if
1245 * Windows is suspended. This caters for the above mentioned caveat of a
1246 * system with many volumes where no "hibr" magic would be present and there is
1247 * no zero header.
1248 *
1249 * Return 0 if Windows is not hibernated on the volume, >0 if Windows is
1250 * hibernated on the volume, and -errno on error.
1251 */
1252static int check_windows_hibernation_status(ntfs_volume *vol)
1253{
1254 MFT_REF mref;
1255 struct inode *vi;
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001256 struct page *page;
1257 u32 *kaddr, *kend;
1258 ntfs_name *name = NULL;
1259 int ret = 1;
Harvey Harrison63cd88542009-03-31 15:23:52 -07001260 static const ntfschar hiberfil[13] = { cpu_to_le16('h'),
1261 cpu_to_le16('i'), cpu_to_le16('b'),
1262 cpu_to_le16('e'), cpu_to_le16('r'),
1263 cpu_to_le16('f'), cpu_to_le16('i'),
1264 cpu_to_le16('l'), cpu_to_le16('.'),
1265 cpu_to_le16('s'), cpu_to_le16('y'),
1266 cpu_to_le16('s'), 0 };
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001267
1268 ntfs_debug("Entering.");
1269 /*
1270 * Find the inode number for the hibernation file by looking up the
1271 * filename hiberfil.sys in the root directory.
1272 */
Al Viro59551022016-01-22 15:40:57 -05001273 inode_lock(vol->root_ino);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001274 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->root_ino), hiberfil, 12,
1275 &name);
Al Viro59551022016-01-22 15:40:57 -05001276 inode_unlock(vol->root_ino);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001277 if (IS_ERR_MREF(mref)) {
1278 ret = MREF_ERR(mref);
1279 /* If the file does not exist, Windows is not hibernated. */
1280 if (ret == -ENOENT) {
1281 ntfs_debug("hiberfil.sys not present. Windows is not "
1282 "hibernated on the volume.");
1283 return 0;
1284 }
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001285 /* A real error occurred. */
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001286 ntfs_error(vol->sb, "Failed to find inode number for "
1287 "hiberfil.sys.");
1288 return ret;
1289 }
1290 /* We do not care for the type of match that was found. */
1291 kfree(name);
1292 /* Get the inode. */
1293 vi = ntfs_iget(vol->sb, MREF(mref));
1294 if (IS_ERR(vi) || is_bad_inode(vi)) {
1295 if (!IS_ERR(vi))
1296 iput(vi);
1297 ntfs_error(vol->sb, "Failed to load hiberfil.sys.");
1298 return IS_ERR(vi) ? PTR_ERR(vi) : -EIO;
1299 }
1300 if (unlikely(i_size_read(vi) < NTFS_HIBERFIL_HEADER_SIZE)) {
1301 ntfs_debug("hiberfil.sys is smaller than 4kiB (0x%llx). "
1302 "Windows is hibernated on the volume. This "
1303 "is not the system volume.", i_size_read(vi));
1304 goto iput_out;
1305 }
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001306 page = ntfs_map_page(vi->i_mapping, 0);
1307 if (IS_ERR(page)) {
1308 ntfs_error(vol->sb, "Failed to read from hiberfil.sys.");
1309 ret = PTR_ERR(page);
1310 goto iput_out;
1311 }
1312 kaddr = (u32*)page_address(page);
Harvey Harrison63cd88542009-03-31 15:23:52 -07001313 if (*(le32*)kaddr == cpu_to_le32(0x72626968)/*'hibr'*/) {
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001314 ntfs_debug("Magic \"hibr\" found in hiberfil.sys. Windows is "
1315 "hibernated on the volume. This is the "
1316 "system volume.");
1317 goto unm_iput_out;
1318 }
1319 kend = kaddr + NTFS_HIBERFIL_HEADER_SIZE/sizeof(*kaddr);
1320 do {
1321 if (unlikely(*kaddr)) {
1322 ntfs_debug("hiberfil.sys is larger than 4kiB "
1323 "(0x%llx), does not contain the "
1324 "\"hibr\" magic, and does not have a "
1325 "zero header. Windows is hibernated "
1326 "on the volume. This is not the "
1327 "system volume.", i_size_read(vi));
1328 goto unm_iput_out;
1329 }
1330 } while (++kaddr < kend);
1331 ntfs_debug("hiberfil.sys contains a zero header. Windows is not "
1332 "hibernated on the volume. This is the system "
1333 "volume.");
1334 ret = 0;
1335unm_iput_out:
1336 ntfs_unmap_page(page);
1337iput_out:
1338 iput(vi);
1339 return ret;
1340}
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342/**
1343 * load_and_init_quota - load and setup the quota file for a volume if present
1344 * @vol: ntfs super block describing device whose quota file to load
1345 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001346 * Return 'true' on success or 'false' on error. If $Quota is not present, we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 * leave vol->quota_ino as NULL and return success.
1348 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001349static bool load_and_init_quota(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 MFT_REF mref;
1352 struct inode *tmp_ino;
1353 ntfs_name *name = NULL;
Harvey Harrison63cd88542009-03-31 15:23:52 -07001354 static const ntfschar Quota[7] = { cpu_to_le16('$'),
1355 cpu_to_le16('Q'), cpu_to_le16('u'),
1356 cpu_to_le16('o'), cpu_to_le16('t'),
1357 cpu_to_le16('a'), 0 };
1358 static ntfschar Q[3] = { cpu_to_le16('$'),
1359 cpu_to_le16('Q'), 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 ntfs_debug("Entering.");
1362 /*
1363 * Find the inode number for the quota file by looking up the filename
1364 * $Quota in the extended system files directory $Extend.
1365 */
Al Viro59551022016-01-22 15:40:57 -05001366 inode_lock(vol->extend_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), Quota, 6,
1368 &name);
Al Viro59551022016-01-22 15:40:57 -05001369 inode_unlock(vol->extend_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (IS_ERR_MREF(mref)) {
1371 /*
1372 * If the file does not exist, quotas are disabled and have
1373 * never been enabled on this volume, just return success.
1374 */
1375 if (MREF_ERR(mref) == -ENOENT) {
1376 ntfs_debug("$Quota not present. Volume does not have "
1377 "quotas enabled.");
1378 /*
1379 * No need to try to set quotas out of date if they are
1380 * not enabled.
1381 */
1382 NVolSetQuotaOutOfDate(vol);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001383 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001385 /* A real error occurred. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 ntfs_error(vol->sb, "Failed to find inode number for $Quota.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001387 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389 /* We do not care for the type of match that was found. */
Jesper Juhl251c8422005-04-04 14:59:56 +01001390 kfree(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /* Get the inode. */
1392 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1393 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1394 if (!IS_ERR(tmp_ino))
1395 iput(tmp_ino);
1396 ntfs_error(vol->sb, "Failed to load $Quota.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001397 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399 vol->quota_ino = tmp_ino;
1400 /* Get the $Q index allocation attribute. */
1401 tmp_ino = ntfs_index_iget(vol->quota_ino, Q, 2);
1402 if (IS_ERR(tmp_ino)) {
1403 ntfs_error(vol->sb, "Failed to load $Quota/$Q index.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001404 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406 vol->quota_q_ino = tmp_ino;
1407 ntfs_debug("Done.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001408 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410
1411/**
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001412 * load_and_init_usnjrnl - load and setup the transaction log if present
1413 * @vol: ntfs super block describing device whose usnjrnl file to load
1414 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001415 * Return 'true' on success or 'false' on error.
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001416 *
1417 * If $UsnJrnl is not present or in the process of being disabled, we set
1418 * NVolUsnJrnlStamped() and return success.
1419 *
1420 * If the $UsnJrnl $DATA/$J attribute has a size equal to the lowest valid usn,
1421 * i.e. transaction logging has only just been enabled or the journal has been
1422 * stamped and nothing has been logged since, we also set NVolUsnJrnlStamped()
1423 * and return success.
1424 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001425static bool load_and_init_usnjrnl(ntfs_volume *vol)
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001426{
1427 MFT_REF mref;
1428 struct inode *tmp_ino;
1429 ntfs_inode *tmp_ni;
1430 struct page *page;
1431 ntfs_name *name = NULL;
1432 USN_HEADER *uh;
Harvey Harrison63cd88542009-03-31 15:23:52 -07001433 static const ntfschar UsnJrnl[9] = { cpu_to_le16('$'),
1434 cpu_to_le16('U'), cpu_to_le16('s'),
1435 cpu_to_le16('n'), cpu_to_le16('J'),
1436 cpu_to_le16('r'), cpu_to_le16('n'),
1437 cpu_to_le16('l'), 0 };
1438 static ntfschar Max[5] = { cpu_to_le16('$'),
1439 cpu_to_le16('M'), cpu_to_le16('a'),
1440 cpu_to_le16('x'), 0 };
1441 static ntfschar J[3] = { cpu_to_le16('$'),
1442 cpu_to_le16('J'), 0 };
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001443
1444 ntfs_debug("Entering.");
1445 /*
1446 * Find the inode number for the transaction log file by looking up the
1447 * filename $UsnJrnl in the extended system files directory $Extend.
1448 */
Al Viro59551022016-01-22 15:40:57 -05001449 inode_lock(vol->extend_ino);
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001450 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), UsnJrnl, 8,
1451 &name);
Al Viro59551022016-01-22 15:40:57 -05001452 inode_unlock(vol->extend_ino);
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001453 if (IS_ERR_MREF(mref)) {
1454 /*
1455 * If the file does not exist, transaction logging is disabled,
1456 * just return success.
1457 */
1458 if (MREF_ERR(mref) == -ENOENT) {
1459 ntfs_debug("$UsnJrnl not present. Volume does not "
1460 "have transaction logging enabled.");
1461not_enabled:
1462 /*
1463 * No need to try to stamp the transaction log if
1464 * transaction logging is not enabled.
1465 */
1466 NVolSetUsnJrnlStamped(vol);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001467 return true;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001468 }
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001469 /* A real error occurred. */
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001470 ntfs_error(vol->sb, "Failed to find inode number for "
1471 "$UsnJrnl.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001472 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001473 }
1474 /* We do not care for the type of match that was found. */
1475 kfree(name);
1476 /* Get the inode. */
1477 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
Denis Efremovcc22c802019-09-25 16:49:43 -07001478 if (IS_ERR(tmp_ino) || unlikely(is_bad_inode(tmp_ino))) {
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001479 if (!IS_ERR(tmp_ino))
1480 iput(tmp_ino);
1481 ntfs_error(vol->sb, "Failed to load $UsnJrnl.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001482 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001483 }
1484 vol->usnjrnl_ino = tmp_ino;
1485 /*
1486 * If the transaction log is in the process of being deleted, we can
1487 * ignore it.
1488 */
1489 if (unlikely(vol->vol_flags & VOLUME_DELETE_USN_UNDERWAY)) {
1490 ntfs_debug("$UsnJrnl in the process of being disabled. "
1491 "Volume does not have transaction logging "
1492 "enabled.");
1493 goto not_enabled;
1494 }
1495 /* Get the $DATA/$Max attribute. */
1496 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, Max, 4);
1497 if (IS_ERR(tmp_ino)) {
1498 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$Max "
1499 "attribute.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001500 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001501 }
1502 vol->usnjrnl_max_ino = tmp_ino;
1503 if (unlikely(i_size_read(tmp_ino) < sizeof(USN_HEADER))) {
1504 ntfs_error(vol->sb, "Found corrupt $UsnJrnl/$DATA/$Max "
1505 "attribute (size is 0x%llx but should be at "
Anton Altaparmakovd04bd1f2005-10-24 08:41:24 +01001506 "least 0x%zx bytes).", i_size_read(tmp_ino),
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001507 sizeof(USN_HEADER));
Richard Knutssonc49c3112006-09-30 23:27:12 -07001508 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001509 }
1510 /* Get the $DATA/$J attribute. */
1511 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, J, 2);
1512 if (IS_ERR(tmp_ino)) {
1513 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$J "
1514 "attribute.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001515 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001516 }
1517 vol->usnjrnl_j_ino = tmp_ino;
1518 /* Verify $J is non-resident and sparse. */
1519 tmp_ni = NTFS_I(vol->usnjrnl_j_ino);
1520 if (unlikely(!NInoNonResident(tmp_ni) || !NInoSparse(tmp_ni))) {
1521 ntfs_error(vol->sb, "$UsnJrnl/$DATA/$J attribute is resident "
1522 "and/or not sparse.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001523 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001524 }
1525 /* Read the USN_HEADER from $DATA/$Max. */
1526 page = ntfs_map_page(vol->usnjrnl_max_ino->i_mapping, 0);
1527 if (IS_ERR(page)) {
1528 ntfs_error(vol->sb, "Failed to read from $UsnJrnl/$DATA/$Max "
1529 "attribute.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001530 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001531 }
1532 uh = (USN_HEADER*)page_address(page);
1533 /* Sanity check the $Max. */
1534 if (unlikely(sle64_to_cpu(uh->allocation_delta) >
1535 sle64_to_cpu(uh->maximum_size))) {
1536 ntfs_error(vol->sb, "Allocation delta (0x%llx) exceeds "
1537 "maximum size (0x%llx). $UsnJrnl is corrupt.",
1538 (long long)sle64_to_cpu(uh->allocation_delta),
1539 (long long)sle64_to_cpu(uh->maximum_size));
1540 ntfs_unmap_page(page);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001541 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001542 }
1543 /*
1544 * If the transaction log has been stamped and nothing has been written
1545 * to it since, we do not need to stamp it.
1546 */
1547 if (unlikely(sle64_to_cpu(uh->lowest_valid_usn) >=
1548 i_size_read(vol->usnjrnl_j_ino))) {
1549 if (likely(sle64_to_cpu(uh->lowest_valid_usn) ==
1550 i_size_read(vol->usnjrnl_j_ino))) {
1551 ntfs_unmap_page(page);
1552 ntfs_debug("$UsnJrnl is enabled but nothing has been "
1553 "logged since it was last stamped. "
1554 "Treating this as if the volume does "
1555 "not have transaction logging "
1556 "enabled.");
1557 goto not_enabled;
1558 }
1559 ntfs_error(vol->sb, "$UsnJrnl has lowest valid usn (0x%llx) "
1560 "which is out of bounds (0x%llx). $UsnJrnl "
1561 "is corrupt.",
1562 (long long)sle64_to_cpu(uh->lowest_valid_usn),
1563 i_size_read(vol->usnjrnl_j_ino));
1564 ntfs_unmap_page(page);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001565 return false;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001566 }
1567 ntfs_unmap_page(page);
1568 ntfs_debug("Done.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001569 return true;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01001570}
1571
1572/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 * load_and_init_attrdef - load the attribute definitions table for a volume
1574 * @vol: ntfs super block describing device whose attrdef to load
1575 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001576 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001578static bool load_and_init_attrdef(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001580 loff_t i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 struct super_block *sb = vol->sb;
1582 struct inode *ino;
1583 struct page *page;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001584 pgoff_t index, max_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 unsigned int size;
1586
1587 ntfs_debug("Entering.");
1588 /* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */
1589 ino = ntfs_iget(sb, FILE_AttrDef);
1590 if (IS_ERR(ino) || is_bad_inode(ino)) {
1591 if (!IS_ERR(ino))
1592 iput(ino);
1593 goto failed;
1594 }
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001595 NInoSetSparseDisabled(NTFS_I(ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001597 i_size = i_size_read(ino);
1598 if (i_size <= 0 || i_size > 0x7fffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 goto iput_failed;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001600 vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (!vol->attrdef)
1602 goto iput_failed;
1603 index = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001604 max_index = i_size >> PAGE_SHIFT;
1605 size = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 while (index < max_index) {
1607 /* Read the attrdef table and copy it into the linear buffer. */
1608read_partial_attrdef_page:
1609 page = ntfs_map_page(ino->i_mapping, index);
1610 if (IS_ERR(page))
1611 goto free_iput_failed;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001612 memcpy((u8*)vol->attrdef + (index++ << PAGE_SHIFT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 page_address(page), size);
1614 ntfs_unmap_page(page);
1615 };
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001616 if (size == PAGE_SIZE) {
1617 size = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (size)
1619 goto read_partial_attrdef_page;
1620 }
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001621 vol->attrdef_size = i_size;
1622 ntfs_debug("Read %llu bytes from $AttrDef.", i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 iput(ino);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001624 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625free_iput_failed:
1626 ntfs_free(vol->attrdef);
1627 vol->attrdef = NULL;
1628iput_failed:
1629 iput(ino);
1630failed:
1631 ntfs_error(sb, "Failed to initialize attribute definition table.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001632 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
1635#endif /* NTFS_RW */
1636
1637/**
1638 * load_and_init_upcase - load the upcase table for an ntfs volume
1639 * @vol: ntfs super block describing device whose upcase to load
1640 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001641 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001643static bool load_and_init_upcase(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001645 loff_t i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 struct super_block *sb = vol->sb;
1647 struct inode *ino;
1648 struct page *page;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001649 pgoff_t index, max_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 unsigned int size;
1651 int i, max;
1652
1653 ntfs_debug("Entering.");
1654 /* Read upcase table and setup vol->upcase and vol->upcase_len. */
1655 ino = ntfs_iget(sb, FILE_UpCase);
1656 if (IS_ERR(ino) || is_bad_inode(ino)) {
1657 if (!IS_ERR(ino))
1658 iput(ino);
1659 goto upcase_failed;
1660 }
1661 /*
1662 * The upcase size must not be above 64k Unicode characters, must not
1663 * be zero and must be a multiple of sizeof(ntfschar).
1664 */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001665 i_size = i_size_read(ino);
1666 if (!i_size || i_size & (sizeof(ntfschar) - 1) ||
1667 i_size > 64ULL * 1024 * sizeof(ntfschar))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 goto iput_upcase_failed;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001669 vol->upcase = (ntfschar*)ntfs_malloc_nofs(i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (!vol->upcase)
1671 goto iput_upcase_failed;
1672 index = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001673 max_index = i_size >> PAGE_SHIFT;
1674 size = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 while (index < max_index) {
1676 /* Read the upcase table and copy it into the linear buffer. */
1677read_partial_upcase_page:
1678 page = ntfs_map_page(ino->i_mapping, index);
1679 if (IS_ERR(page))
1680 goto iput_upcase_failed;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001681 memcpy((char*)vol->upcase + (index++ << PAGE_SHIFT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 page_address(page), size);
1683 ntfs_unmap_page(page);
1684 };
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001685 if (size == PAGE_SIZE) {
1686 size = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (size)
1688 goto read_partial_upcase_page;
1689 }
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001690 vol->upcase_len = i_size >> UCHAR_T_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001692 i_size, 64 * 1024 * sizeof(ntfschar));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 iput(ino);
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001694 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if (!default_upcase) {
1696 ntfs_debug("Using volume specified $UpCase since default is "
1697 "not present.");
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001698 mutex_unlock(&ntfs_lock);
Richard Knutssonc49c3112006-09-30 23:27:12 -07001699 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701 max = default_upcase_len;
1702 if (max > vol->upcase_len)
1703 max = vol->upcase_len;
1704 for (i = 0; i < max; i++)
1705 if (vol->upcase[i] != default_upcase[i])
1706 break;
1707 if (i == max) {
1708 ntfs_free(vol->upcase);
1709 vol->upcase = default_upcase;
1710 vol->upcase_len = max;
1711 ntfs_nr_upcase_users++;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001712 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 ntfs_debug("Volume specified $UpCase matches default. Using "
1714 "default.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001715 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001717 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 ntfs_debug("Using volume specified $UpCase since it does not match "
1719 "the default.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001720 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721iput_upcase_failed:
1722 iput(ino);
1723 ntfs_free(vol->upcase);
1724 vol->upcase = NULL;
1725upcase_failed:
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001726 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (default_upcase) {
1728 vol->upcase = default_upcase;
1729 vol->upcase_len = default_upcase_len;
1730 ntfs_nr_upcase_users++;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001731 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 ntfs_error(sb, "Failed to load $UpCase from the volume. Using "
1733 "default.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001734 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00001736 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 ntfs_error(sb, "Failed to initialize upcase table.");
Richard Knutssonc49c3112006-09-30 23:27:12 -07001738 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
Ingo Molnar59345372006-07-03 00:25:18 -07001741/*
1742 * The lcn and mft bitmap inodes are NTFS-internal inodes with
1743 * their own special locking rules:
1744 */
1745static struct lock_class_key
1746 lcnbmp_runlist_lock_key, lcnbmp_mrec_lock_key,
1747 mftbmp_runlist_lock_key, mftbmp_mrec_lock_key;
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749/**
1750 * load_system_files - open the system files using normal functions
1751 * @vol: ntfs super block describing device whose system files to load
1752 *
1753 * Open the system files with normal access functions and complete setting up
1754 * the ntfs super block @vol.
1755 *
Richard Knutssonc49c3112006-09-30 23:27:12 -07001756 * Return 'true' on success or 'false' on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 */
Richard Knutssonc49c3112006-09-30 23:27:12 -07001758static bool load_system_files(ntfs_volume *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
1760 struct super_block *sb = vol->sb;
1761 MFT_RECORD *m;
1762 VOLUME_INFORMATION *vi;
1763 ntfs_attr_search_ctx *ctx;
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001764#ifdef NTFS_RW
Anton Altaparmakov7d333d62005-09-08 23:01:16 +01001765 RESTART_PAGE_HEADER *rp;
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001766 int err;
1767#endif /* NTFS_RW */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 ntfs_debug("Entering.");
1770#ifdef NTFS_RW
1771 /* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */
1772 if (!load_and_init_mft_mirror(vol) || !check_mft_mirror(vol)) {
1773 static const char *es1 = "Failed to load $MFTMirr";
1774 static const char *es2 = "$MFTMirr does not match $MFT";
1775 static const char *es3 = ". Run ntfsfix and/or chkdsk.";
1776
1777 /* If a read-write mount, convert it to a read-only mount. */
David Howellsbc98a422017-07-17 08:45:34 +01001778 if (!sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1780 ON_ERRORS_CONTINUE))) {
1781 ntfs_error(sb, "%s and neither on_errors="
1782 "continue nor on_errors="
1783 "remount-ro was specified%s",
1784 !vol->mftmirr_ino ? es1 : es2,
1785 es3);
1786 goto iput_mirr_err_out;
1787 }
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001788 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 ntfs_error(sb, "%s. Mounting read-only%s",
1790 !vol->mftmirr_ino ? es1 : es2, es3);
1791 } else
1792 ntfs_warning(sb, "%s. Will not be able to remount "
1793 "read-write%s",
1794 !vol->mftmirr_ino ? es1 : es2, es3);
1795 /* This will prevent a read-write remount. */
1796 NVolSetErrors(vol);
1797 }
1798#endif /* NTFS_RW */
1799 /* Get mft bitmap attribute inode. */
1800 vol->mftbmp_ino = ntfs_attr_iget(vol->mft_ino, AT_BITMAP, NULL, 0);
1801 if (IS_ERR(vol->mftbmp_ino)) {
1802 ntfs_error(sb, "Failed to load $MFT/$BITMAP attribute.");
1803 goto iput_mirr_err_out;
1804 }
Ingo Molnar59345372006-07-03 00:25:18 -07001805 lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->runlist.lock,
1806 &mftbmp_runlist_lock_key);
1807 lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->mrec_lock,
1808 &mftbmp_mrec_lock_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */
1810 if (!load_and_init_upcase(vol))
1811 goto iput_mftbmp_err_out;
1812#ifdef NTFS_RW
1813 /*
1814 * Read attribute definitions table and setup @vol->attrdef and
1815 * @vol->attrdef_size.
1816 */
1817 if (!load_and_init_attrdef(vol))
1818 goto iput_upcase_err_out;
1819#endif /* NTFS_RW */
1820 /*
1821 * Get the cluster allocation bitmap inode and verify the size, no
1822 * need for any locking at this stage as we are already running
1823 * exclusively as we are mount in progress task.
1824 */
1825 vol->lcnbmp_ino = ntfs_iget(sb, FILE_Bitmap);
1826 if (IS_ERR(vol->lcnbmp_ino) || is_bad_inode(vol->lcnbmp_ino)) {
1827 if (!IS_ERR(vol->lcnbmp_ino))
1828 iput(vol->lcnbmp_ino);
1829 goto bitmap_failed;
1830 }
Ingo Molnar59345372006-07-03 00:25:18 -07001831 lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->runlist.lock,
1832 &lcnbmp_runlist_lock_key);
1833 lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->mrec_lock,
1834 &lcnbmp_mrec_lock_key);
1835
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001836 NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino));
Anton Altaparmakov218357f2004-11-18 20:34:59 +00001837 if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 iput(vol->lcnbmp_ino);
1839bitmap_failed:
1840 ntfs_error(sb, "Failed to load $Bitmap.");
1841 goto iput_attrdef_err_out;
1842 }
1843 /*
1844 * Get the volume inode and setup our cache of the volume flags and
1845 * version.
1846 */
1847 vol->vol_ino = ntfs_iget(sb, FILE_Volume);
1848 if (IS_ERR(vol->vol_ino) || is_bad_inode(vol->vol_ino)) {
1849 if (!IS_ERR(vol->vol_ino))
1850 iput(vol->vol_ino);
1851volume_failed:
1852 ntfs_error(sb, "Failed to load $Volume.");
1853 goto iput_lcnbmp_err_out;
1854 }
1855 m = map_mft_record(NTFS_I(vol->vol_ino));
1856 if (IS_ERR(m)) {
1857iput_volume_failed:
1858 iput(vol->vol_ino);
1859 goto volume_failed;
1860 }
1861 if (!(ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m))) {
1862 ntfs_error(sb, "Failed to get attribute search context.");
1863 goto get_ctx_vol_failed;
1864 }
1865 if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
1866 ctx) || ctx->attr->non_resident || ctx->attr->flags) {
1867err_put_vol:
1868 ntfs_attr_put_search_ctx(ctx);
1869get_ctx_vol_failed:
1870 unmap_mft_record(NTFS_I(vol->vol_ino));
1871 goto iput_volume_failed;
1872 }
1873 vi = (VOLUME_INFORMATION*)((char*)ctx->attr +
1874 le16_to_cpu(ctx->attr->data.resident.value_offset));
1875 /* Some bounds checks. */
1876 if ((u8*)vi < (u8*)ctx->attr || (u8*)vi +
1877 le32_to_cpu(ctx->attr->data.resident.value_length) >
1878 (u8*)ctx->attr + le32_to_cpu(ctx->attr->length))
1879 goto err_put_vol;
1880 /* Copy the volume flags and version to the ntfs_volume structure. */
1881 vol->vol_flags = vi->flags;
1882 vol->major_ver = vi->major_ver;
1883 vol->minor_ver = vi->minor_ver;
1884 ntfs_attr_put_search_ctx(ctx);
1885 unmap_mft_record(NTFS_I(vol->vol_ino));
Fabian Frederick87c1b492014-04-07 15:36:53 -07001886 pr_info("volume version %i.%i.\n", vol->major_ver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 vol->minor_ver);
Anton Altaparmakovc002f422005-02-03 12:02:56 +00001888 if (vol->major_ver < 3 && NVolSparseEnabled(vol)) {
1889 ntfs_warning(vol->sb, "Disabling sparse support due to NTFS "
1890 "volume version %i.%i (need at least version "
1891 "3.0).", vol->major_ver, vol->minor_ver);
1892 NVolClearSparseEnabled(vol);
1893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894#ifdef NTFS_RW
1895 /* Make sure that no unsupported volume flags are set. */
1896 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
1897 static const char *es1a = "Volume is dirty";
Anton Altaparmakov1cf3109f2006-02-24 10:48:14 +00001898 static const char *es1b = "Volume has been modified by chkdsk";
1899 static const char *es1c = "Volume has unsupported flags set";
1900 static const char *es2a = ". Run chkdsk and mount in Windows.";
1901 static const char *es2b = ". Mount in Windows.";
1902 const char *es1, *es2;
1903
1904 es2 = es2a;
1905 if (vol->vol_flags & VOLUME_IS_DIRTY)
1906 es1 = es1a;
1907 else if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
1908 es1 = es1b;
1909 es2 = es2b;
1910 } else {
1911 es1 = es1c;
1912 ntfs_warning(sb, "Unsupported volume flags 0x%x "
1913 "encountered.",
1914 (unsigned)le16_to_cpu(vol->vol_flags));
1915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 /* If a read-write mount, convert it to a read-only mount. */
David Howellsbc98a422017-07-17 08:45:34 +01001917 if (!sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1919 ON_ERRORS_CONTINUE))) {
1920 ntfs_error(sb, "%s and neither on_errors="
1921 "continue nor on_errors="
1922 "remount-ro was specified%s",
1923 es1, es2);
1924 goto iput_vol_err_out;
1925 }
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001926 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1928 } else
1929 ntfs_warning(sb, "%s. Will not be able to remount "
1930 "read-write%s", es1, es2);
1931 /*
1932 * Do not set NVolErrors() because ntfs_remount() re-checks the
1933 * flags which we need to do in case any flags have changed.
1934 */
1935 }
1936 /*
1937 * Get the inode for the logfile, check it and determine if the volume
1938 * was shutdown cleanly.
1939 */
Anton Altaparmakove7a10332005-09-08 16:12:28 +01001940 rp = NULL;
1941 if (!load_and_check_logfile(vol, &rp) ||
1942 !ntfs_is_logfile_clean(vol->logfile_ino, rp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 static const char *es1a = "Failed to load $LogFile";
1944 static const char *es1b = "$LogFile is not clean";
1945 static const char *es2 = ". Mount in Windows.";
1946 const char *es1;
1947
1948 es1 = !vol->logfile_ino ? es1a : es1b;
1949 /* If a read-write mount, convert it to a read-only mount. */
David Howellsbc98a422017-07-17 08:45:34 +01001950 if (!sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1952 ON_ERRORS_CONTINUE))) {
1953 ntfs_error(sb, "%s and neither on_errors="
1954 "continue nor on_errors="
1955 "remount-ro was specified%s",
1956 es1, es2);
Anton Altaparmakove7a10332005-09-08 16:12:28 +01001957 if (vol->logfile_ino) {
1958 BUG_ON(!rp);
1959 ntfs_free(rp);
1960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 goto iput_logfile_err_out;
1962 }
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001963 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1965 } else
1966 ntfs_warning(sb, "%s. Will not be able to remount "
1967 "read-write%s", es1, es2);
1968 /* This will prevent a read-write remount. */
1969 NVolSetErrors(vol);
1970 }
Anton Altaparmakove7a10332005-09-08 16:12:28 +01001971 ntfs_free(rp);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01001972#endif /* NTFS_RW */
1973 /* Get the root directory inode so we can do path lookups. */
1974 vol->root_ino = ntfs_iget(sb, FILE_root);
1975 if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) {
1976 if (!IS_ERR(vol->root_ino))
1977 iput(vol->root_ino);
1978 ntfs_error(sb, "Failed to load root directory.");
1979 goto iput_logfile_err_out;
1980 }
1981#ifdef NTFS_RW
1982 /*
1983 * Check if Windows is suspended to disk on the target volume. If it
1984 * is hibernated, we must not write *anything* to the disk so set
1985 * NVolErrors() without setting the dirty volume flag and mount
1986 * read-only. This will prevent read-write remounting and it will also
1987 * prevent all writes.
1988 */
1989 err = check_windows_hibernation_status(vol);
1990 if (unlikely(err)) {
1991 static const char *es1a = "Failed to determine if Windows is "
1992 "hibernated";
1993 static const char *es1b = "Windows is hibernated";
1994 static const char *es2 = ". Run chkdsk.";
1995 const char *es1;
1996
1997 es1 = err < 0 ? es1a : es1b;
1998 /* If a read-write mount, convert it to a read-only mount. */
David Howellsbc98a422017-07-17 08:45:34 +01001999 if (!sb_rdonly(sb)) {
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01002000 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2001 ON_ERRORS_CONTINUE))) {
2002 ntfs_error(sb, "%s and neither on_errors="
2003 "continue nor on_errors="
2004 "remount-ro was specified%s",
2005 es1, es2);
2006 goto iput_root_err_out;
2007 }
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002008 sb->s_flags |= SB_RDONLY;
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01002009 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2010 } else
2011 ntfs_warning(sb, "%s. Will not be able to remount "
2012 "read-write%s", es1, es2);
2013 /* This will prevent a read-write remount. */
2014 NVolSetErrors(vol);
2015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 /* If (still) a read-write mount, mark the volume dirty. */
David Howellsbc98a422017-07-17 08:45:34 +01002017 if (!sb_rdonly(sb) && ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 static const char *es1 = "Failed to set dirty bit in volume "
2019 "information flags";
2020 static const char *es2 = ". Run chkdsk.";
2021
2022 /* Convert to a read-only mount. */
2023 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2024 ON_ERRORS_CONTINUE))) {
2025 ntfs_error(sb, "%s and neither on_errors=continue nor "
2026 "on_errors=remount-ro was specified%s",
2027 es1, es2);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01002028 goto iput_root_err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
2030 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002031 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 /*
2033 * Do not set NVolErrors() because ntfs_remount() might manage
2034 * to set the dirty flag in which case all would be well.
2035 */
2036 }
2037#if 0
2038 // TODO: Enable this code once we start modifying anything that is
2039 // different between NTFS 1.2 and 3.x...
2040 /*
2041 * If (still) a read-write mount, set the NT4 compatibility flag on
2042 * newer NTFS version volumes.
2043 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002044 if (!(sb->s_flags & SB_RDONLY) && (vol->major_ver > 1) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
2046 static const char *es1 = "Failed to set NT4 compatibility flag";
2047 static const char *es2 = ". Run chkdsk.";
2048
2049 /* Convert to a read-only mount. */
2050 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2051 ON_ERRORS_CONTINUE))) {
2052 ntfs_error(sb, "%s and neither on_errors=continue nor "
2053 "on_errors=remount-ro was specified%s",
2054 es1, es2);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01002055 goto iput_root_err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 }
2057 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002058 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 NVolSetErrors(vol);
2060 }
2061#endif
2062 /* If (still) a read-write mount, empty the logfile. */
David Howellsbc98a422017-07-17 08:45:34 +01002063 if (!sb_rdonly(sb) && !ntfs_empty_logfile(vol->logfile_ino)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 static const char *es1 = "Failed to empty $LogFile";
2065 static const char *es2 = ". Mount in Windows.";
2066
2067 /* Convert to a read-only mount. */
2068 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2069 ON_ERRORS_CONTINUE))) {
2070 ntfs_error(sb, "%s and neither on_errors=continue nor "
2071 "on_errors=remount-ro was specified%s",
2072 es1, es2);
Anton Altaparmakovca8fd7a2005-06-25 16:31:27 +01002073 goto iput_root_err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
2075 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002076 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 NVolSetErrors(vol);
2078 }
2079#endif /* NTFS_RW */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 /* If on NTFS versions before 3.0, we are done. */
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002081 if (unlikely(vol->major_ver < 3))
Richard Knutssonc49c3112006-09-30 23:27:12 -07002082 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 /* NTFS 3.0+ specific initialization. */
2084 /* Get the security descriptors inode. */
2085 vol->secure_ino = ntfs_iget(sb, FILE_Secure);
2086 if (IS_ERR(vol->secure_ino) || is_bad_inode(vol->secure_ino)) {
2087 if (!IS_ERR(vol->secure_ino))
2088 iput(vol->secure_ino);
2089 ntfs_error(sb, "Failed to load $Secure.");
2090 goto iput_root_err_out;
2091 }
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002092 // TODO: Initialize security.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 /* Get the extended system files' directory inode. */
2094 vol->extend_ino = ntfs_iget(sb, FILE_Extend);
2095 if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) {
2096 if (!IS_ERR(vol->extend_ino))
2097 iput(vol->extend_ino);
2098 ntfs_error(sb, "Failed to load $Extend.");
2099 goto iput_sec_err_out;
2100 }
2101#ifdef NTFS_RW
2102 /* Find the quota file, load it if present, and set it up. */
2103 if (!load_and_init_quota(vol)) {
2104 static const char *es1 = "Failed to load $Quota";
2105 static const char *es2 = ". Run chkdsk.";
2106
2107 /* If a read-write mount, convert it to a read-only mount. */
David Howellsbc98a422017-07-17 08:45:34 +01002108 if (!sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2110 ON_ERRORS_CONTINUE))) {
2111 ntfs_error(sb, "%s and neither on_errors="
2112 "continue nor on_errors="
2113 "remount-ro was specified%s",
2114 es1, es2);
2115 goto iput_quota_err_out;
2116 }
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002117 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2119 } else
2120 ntfs_warning(sb, "%s. Will not be able to remount "
2121 "read-write%s", es1, es2);
2122 /* This will prevent a read-write remount. */
2123 NVolSetErrors(vol);
2124 }
2125 /* If (still) a read-write mount, mark the quotas out of date. */
David Howellsbc98a422017-07-17 08:45:34 +01002126 if (!sb_rdonly(sb) && !ntfs_mark_quotas_out_of_date(vol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 static const char *es1 = "Failed to mark quotas out of date";
2128 static const char *es2 = ". Run chkdsk.";
2129
2130 /* Convert to a read-only mount. */
2131 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2132 ON_ERRORS_CONTINUE))) {
2133 ntfs_error(sb, "%s and neither on_errors=continue nor "
2134 "on_errors=remount-ro was specified%s",
2135 es1, es2);
2136 goto iput_quota_err_out;
2137 }
2138 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002139 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 NVolSetErrors(vol);
2141 }
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002142 /*
2143 * Find the transaction log file ($UsnJrnl), load it if present, check
2144 * it, and set it up.
2145 */
2146 if (!load_and_init_usnjrnl(vol)) {
2147 static const char *es1 = "Failed to load $UsnJrnl";
2148 static const char *es2 = ". Run chkdsk.";
2149
2150 /* If a read-write mount, convert it to a read-only mount. */
David Howellsbc98a422017-07-17 08:45:34 +01002151 if (!sb_rdonly(sb)) {
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002152 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2153 ON_ERRORS_CONTINUE))) {
2154 ntfs_error(sb, "%s and neither on_errors="
2155 "continue nor on_errors="
2156 "remount-ro was specified%s",
2157 es1, es2);
2158 goto iput_usnjrnl_err_out;
2159 }
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002160 sb->s_flags |= SB_RDONLY;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002161 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2162 } else
2163 ntfs_warning(sb, "%s. Will not be able to remount "
2164 "read-write%s", es1, es2);
2165 /* This will prevent a read-write remount. */
2166 NVolSetErrors(vol);
2167 }
2168 /* If (still) a read-write mount, stamp the transaction log. */
David Howellsbc98a422017-07-17 08:45:34 +01002169 if (!sb_rdonly(sb) && !ntfs_stamp_usnjrnl(vol)) {
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002170 static const char *es1 = "Failed to stamp transaction log "
2171 "($UsnJrnl)";
2172 static const char *es2 = ". Run chkdsk.";
2173
2174 /* Convert to a read-only mount. */
2175 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2176 ON_ERRORS_CONTINUE))) {
2177 ntfs_error(sb, "%s and neither on_errors=continue nor "
2178 "on_errors=remount-ro was specified%s",
2179 es1, es2);
2180 goto iput_usnjrnl_err_out;
2181 }
2182 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002183 sb->s_flags |= SB_RDONLY;
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002184 NVolSetErrors(vol);
2185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186#endif /* NTFS_RW */
Richard Knutssonc49c3112006-09-30 23:27:12 -07002187 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188#ifdef NTFS_RW
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002189iput_usnjrnl_err_out:
SF Markus Elfring917520e2015-09-04 15:43:32 -07002190 iput(vol->usnjrnl_j_ino);
2191 iput(vol->usnjrnl_max_ino);
2192 iput(vol->usnjrnl_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193iput_quota_err_out:
SF Markus Elfring917520e2015-09-04 15:43:32 -07002194 iput(vol->quota_q_ino);
2195 iput(vol->quota_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 iput(vol->extend_ino);
2197#endif /* NTFS_RW */
2198iput_sec_err_out:
2199 iput(vol->secure_ino);
2200iput_root_err_out:
2201 iput(vol->root_ino);
2202iput_logfile_err_out:
2203#ifdef NTFS_RW
SF Markus Elfring917520e2015-09-04 15:43:32 -07002204 iput(vol->logfile_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205iput_vol_err_out:
2206#endif /* NTFS_RW */
2207 iput(vol->vol_ino);
2208iput_lcnbmp_err_out:
2209 iput(vol->lcnbmp_ino);
2210iput_attrdef_err_out:
2211 vol->attrdef_size = 0;
2212 if (vol->attrdef) {
2213 ntfs_free(vol->attrdef);
2214 vol->attrdef = NULL;
2215 }
2216#ifdef NTFS_RW
2217iput_upcase_err_out:
2218#endif /* NTFS_RW */
2219 vol->upcase_len = 0;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002220 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 if (vol->upcase == default_upcase) {
2222 ntfs_nr_upcase_users--;
2223 vol->upcase = NULL;
2224 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002225 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 if (vol->upcase) {
2227 ntfs_free(vol->upcase);
2228 vol->upcase = NULL;
2229 }
2230iput_mftbmp_err_out:
2231 iput(vol->mftbmp_ino);
2232iput_mirr_err_out:
2233#ifdef NTFS_RW
SF Markus Elfring917520e2015-09-04 15:43:32 -07002234 iput(vol->mftmirr_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235#endif /* NTFS_RW */
Richard Knutssonc49c3112006-09-30 23:27:12 -07002236 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
2238
2239/**
2240 * ntfs_put_super - called by the vfs to unmount a volume
2241 * @sb: vfs superblock of volume to unmount
2242 *
2243 * ntfs_put_super() is called by the VFS (from fs/super.c::do_umount()) when
2244 * the volume is being unmounted (umount system call has been invoked) and it
2245 * releases all inodes and memory belonging to the NTFS specific part of the
2246 * super block.
2247 */
2248static void ntfs_put_super(struct super_block *sb)
2249{
2250 ntfs_volume *vol = NTFS_SB(sb);
2251
2252 ntfs_debug("Entering.");
Christoph Hellwig6cfd0142009-05-05 15:40:36 +02002253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254#ifdef NTFS_RW
2255 /*
2256 * Commit all inodes while they are still open in case some of them
2257 * cause others to be dirtied.
2258 */
2259 ntfs_commit_inode(vol->vol_ino);
2260
2261 /* NTFS 3.0+ specific. */
2262 if (vol->major_ver >= 3) {
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002263 if (vol->usnjrnl_j_ino)
2264 ntfs_commit_inode(vol->usnjrnl_j_ino);
2265 if (vol->usnjrnl_max_ino)
2266 ntfs_commit_inode(vol->usnjrnl_max_ino);
2267 if (vol->usnjrnl_ino)
2268 ntfs_commit_inode(vol->usnjrnl_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (vol->quota_q_ino)
2270 ntfs_commit_inode(vol->quota_q_ino);
2271 if (vol->quota_ino)
2272 ntfs_commit_inode(vol->quota_ino);
2273 if (vol->extend_ino)
2274 ntfs_commit_inode(vol->extend_ino);
2275 if (vol->secure_ino)
2276 ntfs_commit_inode(vol->secure_ino);
2277 }
2278
2279 ntfs_commit_inode(vol->root_ino);
2280
2281 down_write(&vol->lcnbmp_lock);
2282 ntfs_commit_inode(vol->lcnbmp_ino);
2283 up_write(&vol->lcnbmp_lock);
2284
2285 down_write(&vol->mftbmp_lock);
2286 ntfs_commit_inode(vol->mftbmp_ino);
2287 up_write(&vol->mftbmp_lock);
2288
2289 if (vol->logfile_ino)
2290 ntfs_commit_inode(vol->logfile_ino);
2291
2292 if (vol->mftmirr_ino)
2293 ntfs_commit_inode(vol->mftmirr_ino);
2294 ntfs_commit_inode(vol->mft_ino);
2295
2296 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002297 * If a read-write mount and no volume errors have occurred, mark the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 * volume clean. Also, re-commit all affected inodes.
2299 */
David Howellsbc98a422017-07-17 08:45:34 +01002300 if (!sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 if (!NVolErrors(vol)) {
2302 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
2303 ntfs_warning(sb, "Failed to clear dirty bit "
2304 "in volume information "
2305 "flags. Run chkdsk.");
2306 ntfs_commit_inode(vol->vol_ino);
2307 ntfs_commit_inode(vol->root_ino);
2308 if (vol->mftmirr_ino)
2309 ntfs_commit_inode(vol->mftmirr_ino);
2310 ntfs_commit_inode(vol->mft_ino);
2311 } else {
2312 ntfs_warning(sb, "Volume has errors. Leaving volume "
2313 "marked dirty. Run chkdsk.");
2314 }
2315 }
2316#endif /* NTFS_RW */
2317
2318 iput(vol->vol_ino);
2319 vol->vol_ino = NULL;
2320
2321 /* NTFS 3.0+ specific clean up. */
2322 if (vol->major_ver >= 3) {
2323#ifdef NTFS_RW
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002324 if (vol->usnjrnl_j_ino) {
2325 iput(vol->usnjrnl_j_ino);
2326 vol->usnjrnl_j_ino = NULL;
2327 }
2328 if (vol->usnjrnl_max_ino) {
2329 iput(vol->usnjrnl_max_ino);
2330 vol->usnjrnl_max_ino = NULL;
2331 }
2332 if (vol->usnjrnl_ino) {
2333 iput(vol->usnjrnl_ino);
2334 vol->usnjrnl_ino = NULL;
2335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if (vol->quota_q_ino) {
2337 iput(vol->quota_q_ino);
2338 vol->quota_q_ino = NULL;
2339 }
2340 if (vol->quota_ino) {
2341 iput(vol->quota_ino);
2342 vol->quota_ino = NULL;
2343 }
2344#endif /* NTFS_RW */
2345 if (vol->extend_ino) {
2346 iput(vol->extend_ino);
2347 vol->extend_ino = NULL;
2348 }
2349 if (vol->secure_ino) {
2350 iput(vol->secure_ino);
2351 vol->secure_ino = NULL;
2352 }
2353 }
2354
2355 iput(vol->root_ino);
2356 vol->root_ino = NULL;
2357
2358 down_write(&vol->lcnbmp_lock);
2359 iput(vol->lcnbmp_ino);
2360 vol->lcnbmp_ino = NULL;
2361 up_write(&vol->lcnbmp_lock);
2362
2363 down_write(&vol->mftbmp_lock);
2364 iput(vol->mftbmp_ino);
2365 vol->mftbmp_ino = NULL;
2366 up_write(&vol->mftbmp_lock);
2367
2368#ifdef NTFS_RW
2369 if (vol->logfile_ino) {
2370 iput(vol->logfile_ino);
2371 vol->logfile_ino = NULL;
2372 }
2373 if (vol->mftmirr_ino) {
2374 /* Re-commit the mft mirror and mft just in case. */
2375 ntfs_commit_inode(vol->mftmirr_ino);
2376 ntfs_commit_inode(vol->mft_ino);
2377 iput(vol->mftmirr_ino);
2378 vol->mftmirr_ino = NULL;
2379 }
2380 /*
Jens Axboe13205fb2009-05-25 09:30:45 +02002381 * We should have no dirty inodes left, due to
2382 * mft.c::ntfs_mft_writepage() cleaning all the dirty pages as
2383 * the underlying mft records are written out and cleaned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 */
2385 ntfs_commit_inode(vol->mft_ino);
2386 write_inode_now(vol->mft_ino, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387#endif /* NTFS_RW */
2388
2389 iput(vol->mft_ino);
2390 vol->mft_ino = NULL;
2391
2392 /* Throw away the table of attribute definitions. */
2393 vol->attrdef_size = 0;
2394 if (vol->attrdef) {
2395 ntfs_free(vol->attrdef);
2396 vol->attrdef = NULL;
2397 }
2398 vol->upcase_len = 0;
2399 /*
2400 * Destroy the global default upcase table if necessary. Also decrease
2401 * the number of upcase users if we are a user.
2402 */
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002403 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if (vol->upcase == default_upcase) {
2405 ntfs_nr_upcase_users--;
2406 vol->upcase = NULL;
2407 }
2408 if (!ntfs_nr_upcase_users && default_upcase) {
2409 ntfs_free(default_upcase);
2410 default_upcase = NULL;
2411 }
2412 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
2413 free_compression_buffers();
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002414 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if (vol->upcase) {
2416 ntfs_free(vol->upcase);
2417 vol->upcase = NULL;
2418 }
Thomas Gleixner6d729e42009-08-16 21:05:08 +00002419
2420 unload_nls(vol->nls_map);
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 sb->s_fs_info = NULL;
2423 kfree(vol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
2425
2426/**
2427 * get_nr_free_clusters - return the number of free clusters on a volume
2428 * @vol: ntfs volume for which to obtain free cluster count
2429 *
2430 * Calculate the number of free clusters on the mounted NTFS volume @vol. We
2431 * actually calculate the number of clusters in use instead because this
2432 * allows us to not care about partial pages as these will be just zero filled
2433 * and hence not be counted as allocated clusters.
2434 *
2435 * The only particularity is that clusters beyond the end of the logical ntfs
2436 * volume will be marked as allocated to prevent errors which means we have to
2437 * discount those at the end. This is important as the cluster bitmap always
2438 * has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside
2439 * the logical volume and marked in use when they are not as they do not exist.
2440 *
2441 * If any pages cannot be read we assume all clusters in the erroring pages are
2442 * in use. This means we return an underestimate on errors which is better than
2443 * an overestimate.
2444 */
2445static s64 get_nr_free_clusters(ntfs_volume *vol)
2446{
2447 s64 nr_free = vol->nr_clusters;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 struct page *page;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002450 pgoff_t index, max_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
2452 ntfs_debug("Entering.");
2453 /* Serialize accesses to the cluster bitmap. */
2454 down_read(&vol->lcnbmp_lock);
2455 /*
2456 * Convert the number of bits into bytes rounded up, then convert into
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03002457 * multiples of PAGE_SIZE, rounding up so that if we have one
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 * full and one partial page max_index = 2.
2459 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002460 max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_SIZE - 1) >>
2461 PAGE_SHIFT;
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03002462 /* Use multiples of 4 bytes, thus max_size is PAGE_SIZE / 4. */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002463 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002464 max_index, PAGE_SIZE / 4);
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002465 for (index = 0; index < max_index; index++) {
Akinobu Mitac4af9642010-03-15 00:35:00 -04002466 unsigned long *kaddr;
2467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 /*
2469 * Read the page from page cache, getting it from backing store
2470 * if necessary, and increment the use count.
2471 */
Nick Piggin6fe69002007-05-06 14:49:04 -07002472 page = read_mapping_page(mapping, index, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 /* Ignore pages which errored synchronously. */
2474 if (IS_ERR(page)) {
Nick Piggin6fe69002007-05-06 14:49:04 -07002475 ntfs_debug("read_mapping_page() error. Skipping "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 "page (index 0x%lx).", index);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002477 nr_free -= PAGE_SIZE * 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 continue;
2479 }
Cong Wanga3ac1412011-11-25 23:14:34 +08002480 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 /*
Akinobu Mitac4af9642010-03-15 00:35:00 -04002482 * Subtract the number of set bits. If this
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 * is the last page and it is partial we don't really care as
2484 * it just means we do a little extra work but it won't affect
2485 * the result as all out of range bytes are set to zero by
2486 * ntfs_readpage().
2487 */
Akinobu Mitac4af9642010-03-15 00:35:00 -04002488 nr_free -= bitmap_weight(kaddr,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002489 PAGE_SIZE * BITS_PER_BYTE);
Cong Wanga3ac1412011-11-25 23:14:34 +08002490 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002491 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 }
2493 ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index - 1);
2494 /*
2495 * Fixup for eventual bits outside logical ntfs volume (see function
2496 * description above).
2497 */
2498 if (vol->nr_clusters & 63)
2499 nr_free += 64 - (vol->nr_clusters & 63);
2500 up_read(&vol->lcnbmp_lock);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002501 /* If errors occurred we may well have gone below zero, fix this. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 if (nr_free < 0)
2503 nr_free = 0;
2504 ntfs_debug("Exiting.");
2505 return nr_free;
2506}
2507
2508/**
2509 * __get_nr_free_mft_records - return the number of free inodes on a volume
2510 * @vol: ntfs volume for which to obtain free inode count
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002511 * @nr_free: number of mft records in filesystem
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002512 * @max_index: maximum number of pages containing set bits
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 *
2514 * Calculate the number of free mft records (inodes) on the mounted NTFS
2515 * volume @vol. We actually calculate the number of mft records in use instead
2516 * because this allows us to not care about partial pages as these will be just
2517 * zero filled and hence not be counted as allocated mft record.
2518 *
2519 * If any pages cannot be read we assume all mft records in the erroring pages
2520 * are in use. This means we return an underestimate on errors which is better
2521 * than an overestimate.
2522 *
2523 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
2524 */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002525static unsigned long __get_nr_free_mft_records(ntfs_volume *vol,
2526 s64 nr_free, const pgoff_t max_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 struct address_space *mapping = vol->mftbmp_ino->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 struct page *page;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002530 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
2532 ntfs_debug("Entering.");
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03002533 /* Use multiples of 4 bytes, thus max_size is PAGE_SIZE / 4. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002535 "0x%lx.", max_index, PAGE_SIZE / 4);
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002536 for (index = 0; index < max_index; index++) {
Akinobu Mitac4af9642010-03-15 00:35:00 -04002537 unsigned long *kaddr;
2538
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 /*
2540 * Read the page from page cache, getting it from backing store
2541 * if necessary, and increment the use count.
2542 */
Nick Piggin6fe69002007-05-06 14:49:04 -07002543 page = read_mapping_page(mapping, index, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 /* Ignore pages which errored synchronously. */
2545 if (IS_ERR(page)) {
Nick Piggin6fe69002007-05-06 14:49:04 -07002546 ntfs_debug("read_mapping_page() error. Skipping "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 "page (index 0x%lx).", index);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002548 nr_free -= PAGE_SIZE * 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 continue;
2550 }
Cong Wanga3ac1412011-11-25 23:14:34 +08002551 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 /*
Akinobu Mitac4af9642010-03-15 00:35:00 -04002553 * Subtract the number of set bits. If this
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 * is the last page and it is partial we don't really care as
2555 * it just means we do a little extra work but it won't affect
2556 * the result as all out of range bytes are set to zero by
2557 * ntfs_readpage().
2558 */
Akinobu Mitac4af9642010-03-15 00:35:00 -04002559 nr_free -= bitmap_weight(kaddr,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002560 PAGE_SIZE * BITS_PER_BYTE);
Cong Wanga3ac1412011-11-25 23:14:34 +08002561 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002562 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 }
2564 ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.",
2565 index - 1);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002566 /* If errors occurred we may well have gone below zero, fix this. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 if (nr_free < 0)
2568 nr_free = 0;
2569 ntfs_debug("Exiting.");
2570 return nr_free;
2571}
2572
2573/**
2574 * ntfs_statfs - return information about mounted NTFS volume
David Howells726c3342006-06-23 02:02:58 -07002575 * @dentry: dentry from mounted volume
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 * @sfs: statfs structure in which to return the information
2577 *
David Howells726c3342006-06-23 02:02:58 -07002578 * Return information about the mounted NTFS volume @dentry in the statfs structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is
2580 * called). We interpret the values to be correct of the moment in time at
2581 * which we are called. Most values are variable otherwise and this isn't just
2582 * the free values but the totals as well. For example we can increase the
2583 * total number of file nodes if we run out and we can keep doing this until
2584 * there is no more space on the volume left at all.
2585 *
2586 * Called from vfs_statfs which is used to handle the statfs, fstatfs, and
2587 * ustat system calls.
2588 *
2589 * Return 0 on success or -errno on error.
2590 */
David Howells726c3342006-06-23 02:02:58 -07002591static int ntfs_statfs(struct dentry *dentry, struct kstatfs *sfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
David Howells726c3342006-06-23 02:02:58 -07002593 struct super_block *sb = dentry->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 s64 size;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002595 ntfs_volume *vol = NTFS_SB(sb);
2596 ntfs_inode *mft_ni = NTFS_I(vol->mft_ino);
2597 pgoff_t max_index;
2598 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
2600 ntfs_debug("Entering.");
2601 /* Type of filesystem. */
2602 sfs->f_type = NTFS_SB_MAGIC;
2603 /* Optimal transfer block size. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002604 sfs->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 /*
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002606 * Total data blocks in filesystem in units of f_bsize and since
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 * inodes are also stored in data blocs ($MFT is a file) this is just
2608 * the total clusters.
2609 */
2610 sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002611 PAGE_SHIFT;
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002612 /* Free data blocks in filesystem in units of f_bsize. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 size = get_nr_free_clusters(vol) << vol->cluster_size_bits >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002614 PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 if (size < 0LL)
2616 size = 0LL;
2617 /* Free blocks avail to non-superuser, same as above on NTFS. */
2618 sfs->f_bavail = sfs->f_bfree = size;
2619 /* Serialize accesses to the inode bitmap. */
2620 down_read(&vol->mftbmp_lock);
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002621 read_lock_irqsave(&mft_ni->size_lock, flags);
2622 size = i_size_read(vol->mft_ino) >> vol->mft_record_size_bits;
2623 /*
2624 * Convert the maximum number of set bits into bytes rounded up, then
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03002625 * convert into multiples of PAGE_SIZE, rounding up so that if we
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002626 * have one full and one partial page max_index = 2.
2627 */
2628 max_index = ((((mft_ni->initialized_size >> vol->mft_record_size_bits)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002629 + 7) >> 3) + PAGE_SIZE - 1) >> PAGE_SHIFT;
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002630 read_unlock_irqrestore(&mft_ni->size_lock, flags);
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002631 /* Number of inodes in filesystem (at this point in time). */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002632 sfs->f_files = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 /* Free inodes in fs (based on current total count). */
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002634 sfs->f_ffree = __get_nr_free_mft_records(vol, size, max_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 up_read(&vol->mftbmp_lock);
2636 /*
2637 * File system id. This is extremely *nix flavour dependent and even
2638 * within Linux itself all fs do their own thing. I interpret this to
2639 * mean a unique id associated with the mounted fs and not the id
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002640 * associated with the filesystem driver, the latter is already given
2641 * by the filesystem type in sfs->f_type. Thus we use the 64-bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 * volume serial number splitting it into two 32-bit parts. We enter
2643 * the least significant 32-bits in f_fsid[0] and the most significant
2644 * 32-bits in f_fsid[1].
2645 */
2646 sfs->f_fsid.val[0] = vol->serial_no & 0xffffffff;
2647 sfs->f_fsid.val[1] = (vol->serial_no >> 32) & 0xffffffff;
2648 /* Maximum length of filenames. */
2649 sfs->f_namelen = NTFS_MAX_NAME_LEN;
2650 return 0;
2651}
2652
Christoph Hellwiga9185b42010-03-05 09:21:37 +01002653#ifdef NTFS_RW
2654static int ntfs_write_inode(struct inode *vi, struct writeback_control *wbc)
2655{
2656 return __ntfs_write_inode(vi, wbc->sync_mode == WB_SYNC_ALL);
2657}
2658#endif
2659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660/**
2661 * The complete super operations.
2662 */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08002663static const struct super_operations ntfs_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 .alloc_inode = ntfs_alloc_big_inode, /* VFS: Allocate new inode. */
Al Viroa2b757f2019-04-15 22:36:59 -04002665 .free_inode = ntfs_free_big_inode, /* VFS: Deallocate inode. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666#ifdef NTFS_RW
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 .write_inode = ntfs_write_inode, /* VFS: Write dirty inode to
2668 disk. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669#endif /* NTFS_RW */
2670 .put_super = ntfs_put_super, /* Syscall: umount. */
2671 .statfs = ntfs_statfs, /* Syscall: statfs */
2672 .remount_fs = ntfs_remount, /* Syscall: mount -o remount. */
Al Virob57922d2010-06-07 14:34:48 -04002673 .evict_inode = ntfs_evict_big_inode, /* VFS: Called when an inode is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 removed from memory. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 .show_options = ntfs_show_options, /* Show mount options in
2676 proc. */
2677};
2678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679/**
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002680 * ntfs_fill_super - mount an ntfs filesystem
2681 * @sb: super block of ntfs filesystem to mount
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 * @opt: string containing the mount options
2683 * @silent: silence error output
2684 *
2685 * ntfs_fill_super() is called by the VFS to mount the device described by @sb
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002686 * with the mount otions in @data with the NTFS filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 *
2688 * If @silent is true, remain silent even if errors are detected. This is used
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002689 * during bootup, when the kernel tries to mount the root filesystem with all
2690 * registered filesystems one after the other until one succeeds. This implies
2691 * that all filesystems except the correct one will quite correctly and
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 * expectedly return an error, but nobody wants to see error messages when in
2693 * fact this is what is supposed to happen.
2694 *
2695 * NOTE: @sb->s_flags contains the mount options flags.
2696 */
2697static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2698{
2699 ntfs_volume *vol;
2700 struct buffer_head *bh;
2701 struct inode *tmp_ino;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002702 int blocksize, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Ingo Molnar59345372006-07-03 00:25:18 -07002704 /*
2705 * We do a pretty difficult piece of bootstrap by reading the
2706 * MFT (and other metadata) from disk into memory. We'll only
2707 * release this metadata during umount, so the locking patterns
2708 * observed during bootstrap do not count. So turn off the
2709 * observation of locking patterns (strictly for this context
2710 * only) while mounting NTFS. [The validator is still active
2711 * otherwise, even for this context: it will for example record
2712 * lock class registrations.]
2713 */
2714 lockdep_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 ntfs_debug("Entering.");
2716#ifndef NTFS_RW
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002717 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718#endif /* ! NTFS_RW */
2719 /* Allocate a new ntfs_volume and place it in sb->s_fs_info. */
2720 sb->s_fs_info = kmalloc(sizeof(ntfs_volume), GFP_NOFS);
2721 vol = NTFS_SB(sb);
2722 if (!vol) {
2723 if (!silent)
2724 ntfs_error(sb, "Allocation of NTFS volume structure "
2725 "failed. Aborting mount...");
Ingo Molnar59345372006-07-03 00:25:18 -07002726 lockdep_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 return -ENOMEM;
2728 }
2729 /* Initialize ntfs_volume structure. */
Anton Altaparmakov442d2072005-05-27 16:42:56 +01002730 *vol = (ntfs_volume) {
2731 .sb = sb,
2732 /*
2733 * Default is group and other don't have any access to files or
2734 * directories while owner has full access. Further, files by
2735 * default are not executable but directories are of course
2736 * browseable.
2737 */
2738 .fmask = 0177,
2739 .dmask = 0077,
2740 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 init_rwsem(&vol->mftbmp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 init_rwsem(&vol->lcnbmp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Anton Altaparmakovc002f422005-02-03 12:02:56 +00002744 /* By default, enable sparse support. */
2745 NVolSetSparseEnabled(vol);
2746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 /* Important to get the mount options dealt with now. */
2748 if (!parse_options(vol, (char*)opt))
2749 goto err_out_now;
2750
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03002751 /* We support sector sizes up to the PAGE_SIZE. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002752 if (bdev_logical_block_size(sb->s_bdev) > PAGE_SIZE) {
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002753 if (!silent)
2754 ntfs_error(sb, "Device has unsupported sector size "
2755 "(%i). The maximum supported sector "
2756 "size on this architecture is %lu "
2757 "bytes.",
Martin K. Petersene1defc42009-05-22 17:17:49 -04002758 bdev_logical_block_size(sb->s_bdev),
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002759 PAGE_SIZE);
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002760 goto err_out_now;
2761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 /*
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002763 * Setup the device access block size to NTFS_BLOCK_SIZE or the hard
2764 * sector size, whichever is bigger.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 */
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002766 blocksize = sb_min_blocksize(sb, NTFS_BLOCK_SIZE);
2767 if (blocksize < NTFS_BLOCK_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 if (!silent)
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002769 ntfs_error(sb, "Unable to set device block size.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 goto err_out_now;
2771 }
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002772 BUG_ON(blocksize != sb->s_blocksize);
2773 ntfs_debug("Set device block size to %i bytes (block size bits %i).",
2774 blocksize, sb->s_blocksize_bits);
2775 /* Determine the size of the device in units of block_size bytes. */
2776 if (!i_size_read(sb->s_bdev->bd_inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 if (!silent)
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002778 ntfs_error(sb, "Unable to determine device size.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 goto err_out_now;
2780 }
Anton Altaparmakov218357f2004-11-18 20:34:59 +00002781 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002782 sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 /* Read the boot sector and return unlocked buffer head to it. */
2784 if (!(bh = read_ntfs_boot_sector(sb, silent))) {
2785 if (!silent)
2786 ntfs_error(sb, "Not an NTFS volume.");
2787 goto err_out_now;
2788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 /*
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002790 * Extract the data from the boot sector and setup the ntfs volume
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 * using it.
2792 */
2793 result = parse_ntfs_boot_sector(vol, (NTFS_BOOT_SECTOR*)bh->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 if (!result) {
2796 if (!silent)
2797 ntfs_error(sb, "Unsupported NTFS filesystem.");
2798 goto err_out_now;
2799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 /*
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002801 * If the boot sector indicates a sector size bigger than the current
2802 * device block size, switch the device block size to the sector size.
2803 * TODO: It may be possible to support this case even when the set
2804 * below fails, we would just be breaking up the i/o for each sector
2805 * into multiple blocks for i/o purposes but otherwise it should just
2806 * work. However it is safer to leave disabled until someone hits this
2807 * error message and then we can get them to try it without the setting
2808 * so we know for sure that it works.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 */
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002810 if (vol->sector_size > blocksize) {
2811 blocksize = sb_set_blocksize(sb, vol->sector_size);
2812 if (blocksize != vol->sector_size) {
2813 if (!silent)
2814 ntfs_error(sb, "Unable to set device block "
2815 "size to sector size (%i).",
2816 vol->sector_size);
2817 goto err_out_now;
2818 }
2819 BUG_ON(blocksize != sb->s_blocksize);
2820 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2821 sb->s_blocksize_bits;
2822 ntfs_debug("Changed device block size to %i bytes (block size "
2823 "bits %i) to match volume sector size.",
2824 blocksize, sb->s_blocksize_bits);
2825 }
2826 /* Initialize the cluster and mft allocators. */
2827 ntfs_setup_allocators(vol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 /* Setup remaining fields in the super block. */
2829 sb->s_magic = NTFS_SB_MAGIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 /*
2831 * Ntfs allows 63 bits for the file size, i.e. correct would be:
2832 * sb->s_maxbytes = ~0ULL >> 1;
2833 * But the kernel uses a long as the page cache page index which on
2834 * 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel
2835 * defined to the maximum the page cache page index can cope with
2836 * without overflowing the index or to 2^63 - 1, whichever is smaller.
2837 */
2838 sb->s_maxbytes = MAX_LFS_FILESIZE;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00002839 /* Ntfs measures time in 100ns intervals. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 sb->s_time_gran = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 /*
2842 * Now load the metadata required for the page cache and our address
2843 * space operations to function. We do this by setting up a specialised
2844 * read_inode method and then just calling the normal iget() to obtain
2845 * the inode for $MFT which is sufficient to allow our normal inode
2846 * operations and associated address space operations to function.
2847 */
2848 sb->s_op = &ntfs_sops;
2849 tmp_ino = new_inode(sb);
2850 if (!tmp_ino) {
2851 if (!silent)
2852 ntfs_error(sb, "Failed to load essential metadata.");
2853 goto err_out_now;
2854 }
2855 tmp_ino->i_ino = FILE_MFT;
2856 insert_inode_hash(tmp_ino);
2857 if (ntfs_read_inode_mount(tmp_ino) < 0) {
2858 if (!silent)
2859 ntfs_error(sb, "Failed to load essential metadata.");
2860 goto iput_tmp_ino_err_out_now;
2861 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002862 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 /*
2864 * The current mount is a compression user if the cluster size is
2865 * less than or equal 4kiB.
2866 */
2867 if (vol->cluster_size <= 4096 && !ntfs_nr_compression_users++) {
2868 result = allocate_compression_buffers();
2869 if (result) {
2870 ntfs_error(NULL, "Failed to allocate buffers "
2871 "for compression engine.");
2872 ntfs_nr_compression_users--;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002873 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 goto iput_tmp_ino_err_out_now;
2875 }
2876 }
2877 /*
2878 * Generate the global default upcase table if necessary. Also
2879 * temporarily increment the number of upcase users to avoid race
2880 * conditions with concurrent (u)mounts.
2881 */
2882 if (!default_upcase)
2883 default_upcase = generate_default_upcase();
2884 ntfs_nr_upcase_users++;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002885 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 /*
2887 * From now on, ignore @silent parameter. If we fail below this line,
2888 * it will be due to a corrupt fs or a system error, so we report it.
2889 */
2890 /*
2891 * Open the system files with normal access functions and complete
2892 * setting up the ntfs super block.
2893 */
2894 if (!load_system_files(vol)) {
2895 ntfs_error(sb, "Failed to load system files.");
2896 goto unl_upcase_iput_tmp_ino_err_out_now;
2897 }
Al Viroea29c692012-02-12 22:04:09 -05002898
2899 /* We grab a reference, simulating an ntfs_iget(). */
2900 ihold(vol->root_ino);
2901 if ((sb->s_root = d_make_root(vol->root_ino))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 ntfs_debug("Exiting, status successful.");
2903 /* Release the default upcase if it has no users. */
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002904 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 if (!--ntfs_nr_upcase_users && default_upcase) {
2906 ntfs_free(default_upcase);
2907 default_upcase = NULL;
2908 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002909 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 sb->s_export_op = &ntfs_export_ops;
Ingo Molnar59345372006-07-03 00:25:18 -07002911 lockdep_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 return 0;
2913 }
2914 ntfs_error(sb, "Failed to allocate root directory.");
2915 /* Clean up after the successful load_system_files() call from above. */
2916 // TODO: Use ntfs_put_super() instead of repeating all this code...
2917 // FIXME: Should mark the volume clean as the error is most likely
2918 // -ENOMEM.
2919 iput(vol->vol_ino);
2920 vol->vol_ino = NULL;
2921 /* NTFS 3.0+ specific clean up. */
2922 if (vol->major_ver >= 3) {
2923#ifdef NTFS_RW
Anton Altaparmakov3f2faef2005-06-25 15:28:56 +01002924 if (vol->usnjrnl_j_ino) {
2925 iput(vol->usnjrnl_j_ino);
2926 vol->usnjrnl_j_ino = NULL;
2927 }
2928 if (vol->usnjrnl_max_ino) {
2929 iput(vol->usnjrnl_max_ino);
2930 vol->usnjrnl_max_ino = NULL;
2931 }
2932 if (vol->usnjrnl_ino) {
2933 iput(vol->usnjrnl_ino);
2934 vol->usnjrnl_ino = NULL;
2935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (vol->quota_q_ino) {
2937 iput(vol->quota_q_ino);
2938 vol->quota_q_ino = NULL;
2939 }
2940 if (vol->quota_ino) {
2941 iput(vol->quota_ino);
2942 vol->quota_ino = NULL;
2943 }
2944#endif /* NTFS_RW */
2945 if (vol->extend_ino) {
2946 iput(vol->extend_ino);
2947 vol->extend_ino = NULL;
2948 }
2949 if (vol->secure_ino) {
2950 iput(vol->secure_ino);
2951 vol->secure_ino = NULL;
2952 }
2953 }
2954 iput(vol->root_ino);
2955 vol->root_ino = NULL;
2956 iput(vol->lcnbmp_ino);
2957 vol->lcnbmp_ino = NULL;
2958 iput(vol->mftbmp_ino);
2959 vol->mftbmp_ino = NULL;
2960#ifdef NTFS_RW
2961 if (vol->logfile_ino) {
2962 iput(vol->logfile_ino);
2963 vol->logfile_ino = NULL;
2964 }
2965 if (vol->mftmirr_ino) {
2966 iput(vol->mftmirr_ino);
2967 vol->mftmirr_ino = NULL;
2968 }
2969#endif /* NTFS_RW */
2970 /* Throw away the table of attribute definitions. */
2971 vol->attrdef_size = 0;
2972 if (vol->attrdef) {
2973 ntfs_free(vol->attrdef);
2974 vol->attrdef = NULL;
2975 }
2976 vol->upcase_len = 0;
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002977 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 if (vol->upcase == default_upcase) {
2979 ntfs_nr_upcase_users--;
2980 vol->upcase = NULL;
2981 }
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002982 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 if (vol->upcase) {
2984 ntfs_free(vol->upcase);
2985 vol->upcase = NULL;
2986 }
2987 if (vol->nls_map) {
2988 unload_nls(vol->nls_map);
2989 vol->nls_map = NULL;
2990 }
2991 /* Error exit code path. */
2992unl_upcase_iput_tmp_ino_err_out_now:
2993 /*
2994 * Decrease the number of upcase users and destroy the global default
2995 * upcase table if necessary.
2996 */
Ingo Molnar4e5e5292006-03-23 16:57:48 +00002997 mutex_lock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 if (!--ntfs_nr_upcase_users && default_upcase) {
2999 ntfs_free(default_upcase);
3000 default_upcase = NULL;
3001 }
3002 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
3003 free_compression_buffers();
Ingo Molnar4e5e5292006-03-23 16:57:48 +00003004 mutex_unlock(&ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005iput_tmp_ino_err_out_now:
3006 iput(tmp_ino);
3007 if (vol->mft_ino && vol->mft_ino != tmp_ino)
3008 iput(vol->mft_ino);
3009 vol->mft_ino = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 /* Errors at this stage are irrelevant. */
3011err_out_now:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 sb->s_fs_info = NULL;
3013 kfree(vol);
3014 ntfs_debug("Failed, returning -EINVAL.");
Ingo Molnar59345372006-07-03 00:25:18 -07003015 lockdep_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 return -EINVAL;
3017}
3018
3019/*
3020 * This is a slab cache to optimize allocations and deallocations of Unicode
3021 * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN
3022 * (255) Unicode characters + a terminating NULL Unicode character.
3023 */
Pekka Enberg64419d92006-02-05 21:43:57 +00003024struct kmem_cache *ntfs_name_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Anton Altaparmakov7fafb8b2005-04-06 16:09:21 +01003026/* Slab caches for efficient allocation/deallocation of inodes. */
Pekka Enberg64419d92006-02-05 21:43:57 +00003027struct kmem_cache *ntfs_inode_cache;
3028struct kmem_cache *ntfs_big_inode_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
3030/* Init once constructor for the inode slab cache. */
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003031static void ntfs_big_inode_init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
3033 ntfs_inode *ni = (ntfs_inode *)foo;
3034
Christoph Lametera35afb82007-05-16 22:10:57 -07003035 inode_init_once(VFS_I(ni));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036}
3037
3038/*
3039 * Slab caches to optimize allocations and deallocations of attribute search
3040 * contexts and index contexts, respectively.
3041 */
Pekka Enberg64419d92006-02-05 21:43:57 +00003042struct kmem_cache *ntfs_attr_ctx_cache;
3043struct kmem_cache *ntfs_index_ctx_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Anton Altaparmakove750d1c2006-03-23 17:04:12 +00003045/* Driver wide mutex. */
Ingo Molnar4e5e5292006-03-23 16:57:48 +00003046DEFINE_MUTEX(ntfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Al Viro152a0832010-07-25 00:46:55 +04003048static struct dentry *ntfs_mount(struct file_system_type *fs_type,
3049 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
Al Viro152a0832010-07-25 00:46:55 +04003051 return mount_bdev(fs_type, flags, dev_name, data, ntfs_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052}
3053
3054static struct file_system_type ntfs_fs_type = {
3055 .owner = THIS_MODULE,
3056 .name = "ntfs",
Al Viro152a0832010-07-25 00:46:55 +04003057 .mount = ntfs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 .kill_sb = kill_block_super,
3059 .fs_flags = FS_REQUIRES_DEV,
3060};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08003061MODULE_ALIAS_FS("ntfs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
3063/* Stable names for the slab caches. */
3064static const char ntfs_index_ctx_cache_name[] = "ntfs_index_ctx_cache";
3065static const char ntfs_attr_ctx_cache_name[] = "ntfs_attr_ctx_cache";
3066static const char ntfs_name_cache_name[] = "ntfs_name_cache";
3067static const char ntfs_inode_cache_name[] = "ntfs_inode_cache";
3068static const char ntfs_big_inode_cache_name[] = "ntfs_big_inode_cache";
3069
3070static int __init init_ntfs_fs(void)
3071{
3072 int err = 0;
3073
3074 /* This may be ugly but it results in pretty output so who cares. (-8 */
Fabian Frederick87c1b492014-04-07 15:36:53 -07003075 pr_info("driver " NTFS_VERSION " [Flags: R/"
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076#ifdef NTFS_RW
3077 "W"
3078#else
3079 "O"
3080#endif
3081#ifdef DEBUG
3082 " DEBUG"
3083#endif
3084#ifdef MODULE
3085 " MODULE"
3086#endif
3087 "].\n");
3088
3089 ntfs_debug("Debug messages are enabled.");
3090
3091 ntfs_index_ctx_cache = kmem_cache_create(ntfs_index_ctx_cache_name,
3092 sizeof(ntfs_index_context), 0 /* offset */,
Paul Mundt20c2df82007-07-20 10:11:58 +09003093 SLAB_HWCACHE_ALIGN, NULL /* ctor */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 if (!ntfs_index_ctx_cache) {
Fabian Frederick87c1b492014-04-07 15:36:53 -07003095 pr_crit("Failed to create %s!\n", ntfs_index_ctx_cache_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 goto ictx_err_out;
3097 }
3098 ntfs_attr_ctx_cache = kmem_cache_create(ntfs_attr_ctx_cache_name,
3099 sizeof(ntfs_attr_search_ctx), 0 /* offset */,
Paul Mundt20c2df82007-07-20 10:11:58 +09003100 SLAB_HWCACHE_ALIGN, NULL /* ctor */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 if (!ntfs_attr_ctx_cache) {
Fabian Frederick87c1b492014-04-07 15:36:53 -07003102 pr_crit("NTFS: Failed to create %s!\n",
3103 ntfs_attr_ctx_cache_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 goto actx_err_out;
3105 }
3106
3107 ntfs_name_cache = kmem_cache_create(ntfs_name_cache_name,
3108 (NTFS_MAX_NAME_LEN+1) * sizeof(ntfschar), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09003109 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 if (!ntfs_name_cache) {
Fabian Frederick87c1b492014-04-07 15:36:53 -07003111 pr_crit("Failed to create %s!\n", ntfs_name_cache_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 goto name_err_out;
3113 }
3114
3115 ntfs_inode_cache = kmem_cache_create(ntfs_inode_cache_name,
3116 sizeof(ntfs_inode), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09003117 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 if (!ntfs_inode_cache) {
Fabian Frederick87c1b492014-04-07 15:36:53 -07003119 pr_crit("Failed to create %s!\n", ntfs_inode_cache_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 goto inode_err_out;
3121 }
3122
3123 ntfs_big_inode_cache = kmem_cache_create(ntfs_big_inode_cache_name,
3124 sizeof(big_ntfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08003125 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
3126 SLAB_ACCOUNT, ntfs_big_inode_init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 if (!ntfs_big_inode_cache) {
Fabian Frederick87c1b492014-04-07 15:36:53 -07003128 pr_crit("Failed to create %s!\n", ntfs_big_inode_cache_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 goto big_inode_err_out;
3130 }
3131
3132 /* Register the ntfs sysctls. */
3133 err = ntfs_sysctl(1);
3134 if (err) {
Fabian Frederick87c1b492014-04-07 15:36:53 -07003135 pr_crit("Failed to register NTFS sysctls!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 goto sysctl_err_out;
3137 }
3138
3139 err = register_filesystem(&ntfs_fs_type);
3140 if (!err) {
3141 ntfs_debug("NTFS driver registered successfully.");
3142 return 0; /* Success! */
3143 }
Fabian Frederick87c1b492014-04-07 15:36:53 -07003144 pr_crit("Failed to register NTFS filesystem driver!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Al Virof4c54992012-03-17 18:19:57 -04003146 /* Unregister the ntfs sysctls. */
3147 ntfs_sysctl(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148sysctl_err_out:
3149 kmem_cache_destroy(ntfs_big_inode_cache);
3150big_inode_err_out:
3151 kmem_cache_destroy(ntfs_inode_cache);
3152inode_err_out:
3153 kmem_cache_destroy(ntfs_name_cache);
3154name_err_out:
3155 kmem_cache_destroy(ntfs_attr_ctx_cache);
3156actx_err_out:
3157 kmem_cache_destroy(ntfs_index_ctx_cache);
3158ictx_err_out:
3159 if (!err) {
Fabian Frederick87c1b492014-04-07 15:36:53 -07003160 pr_crit("Aborting NTFS filesystem driver registration...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 err = -ENOMEM;
3162 }
3163 return err;
3164}
3165
3166static void __exit exit_ntfs_fs(void)
3167{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 ntfs_debug("Unregistering NTFS driver.");
3169
3170 unregister_filesystem(&ntfs_fs_type);
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10003171
3172 /*
3173 * Make sure all delayed rcu free inodes are flushed before we
3174 * destroy cache.
3175 */
3176 rcu_barrier();
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003177 kmem_cache_destroy(ntfs_big_inode_cache);
3178 kmem_cache_destroy(ntfs_inode_cache);
3179 kmem_cache_destroy(ntfs_name_cache);
3180 kmem_cache_destroy(ntfs_attr_ctx_cache);
3181 kmem_cache_destroy(ntfs_index_ctx_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 /* Unregister the ntfs sysctls. */
3183 ntfs_sysctl(0);
3184}
3185
Anton Altaparmakov2818ef52011-01-12 10:34:35 +00003186MODULE_AUTHOR("Anton Altaparmakov <anton@tuxera.com>");
Anton Altaparmakov5272d032014-10-09 15:24:46 -07003187MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2014 Anton Altaparmakov and Tuxera Inc.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188MODULE_VERSION(NTFS_VERSION);
3189MODULE_LICENSE("GPL");
3190#ifdef DEBUG
Rusty Russell69116f22012-01-13 09:32:17 +10303191module_param(debug_msgs, bint, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192MODULE_PARM_DESC(debug_msgs, "Enable debug messages.");
3193#endif
3194
3195module_init(init_ntfs_fs)
3196module_exit(exit_ntfs_fs)