xref: /openbmc/linux/fs/ntfs/super.c (revision 67b1dfe7)
1 /*
2  * super.c - NTFS kernel super block handling. Part of the Linux-NTFS project.
3  *
4  * Copyright (c) 2001-2006 Anton Altaparmakov
5  * Copyright (c) 2001,2002 Richard Russon
6  *
7  * This program/include file is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as published
9  * by the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program/include file is distributed in the hope that it will be
13  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program (in the main directory of the Linux-NTFS
19  * distribution in the file COPYING); if not, write to the Free Software
20  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 
23 #include <linux/stddef.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
27 #include <linux/spinlock.h>
28 #include <linux/blkdev.h>	/* For bdev_hardsect_size(). */
29 #include <linux/backing-dev.h>
30 #include <linux/buffer_head.h>
31 #include <linux/vfs.h>
32 #include <linux/moduleparam.h>
33 #include <linux/smp_lock.h>
34 
35 #include "sysctl.h"
36 #include "logfile.h"
37 #include "quota.h"
38 #include "usnjrnl.h"
39 #include "dir.h"
40 #include "debug.h"
41 #include "index.h"
42 #include "aops.h"
43 #include "layout.h"
44 #include "malloc.h"
45 #include "ntfs.h"
46 
47 /* Number of mounted filesystems which have compression enabled. */
48 static unsigned long ntfs_nr_compression_users;
49 
50 /* A global default upcase table and a corresponding reference count. */
51 static ntfschar *default_upcase = NULL;
52 static unsigned long ntfs_nr_upcase_users = 0;
53 
54 /* Error constants/strings used in inode.c::ntfs_show_options(). */
55 typedef enum {
56 	/* One of these must be present, default is ON_ERRORS_CONTINUE. */
57 	ON_ERRORS_PANIC			= 0x01,
58 	ON_ERRORS_REMOUNT_RO		= 0x02,
59 	ON_ERRORS_CONTINUE		= 0x04,
60 	/* Optional, can be combined with any of the above. */
61 	ON_ERRORS_RECOVER		= 0x10,
62 } ON_ERRORS_ACTIONS;
63 
64 const option_t on_errors_arr[] = {
65 	{ ON_ERRORS_PANIC,	"panic" },
66 	{ ON_ERRORS_REMOUNT_RO,	"remount-ro", },
67 	{ ON_ERRORS_CONTINUE,	"continue", },
68 	{ ON_ERRORS_RECOVER,	"recover" },
69 	{ 0,			NULL }
70 };
71 
72 /**
73  * simple_getbool -
74  *
75  * Copied from old ntfs driver (which copied from vfat driver).
76  */
77 static int simple_getbool(char *s, BOOL *setval)
78 {
79 	if (s) {
80 		if (!strcmp(s, "1") || !strcmp(s, "yes") || !strcmp(s, "true"))
81 			*setval = TRUE;
82 		else if (!strcmp(s, "0") || !strcmp(s, "no") ||
83 							!strcmp(s, "false"))
84 			*setval = FALSE;
85 		else
86 			return 0;
87 	} else
88 		*setval = TRUE;
89 	return 1;
90 }
91 
92 /**
93  * parse_options - parse the (re)mount options
94  * @vol:	ntfs volume
95  * @opt:	string containing the (re)mount options
96  *
97  * Parse the recognized options in @opt for the ntfs volume described by @vol.
98  */
99 static BOOL parse_options(ntfs_volume *vol, char *opt)
100 {
101 	char *p, *v, *ov;
102 	static char *utf8 = "utf8";
103 	int errors = 0, sloppy = 0;
104 	uid_t uid = (uid_t)-1;
105 	gid_t gid = (gid_t)-1;
106 	mode_t fmask = (mode_t)-1, dmask = (mode_t)-1;
107 	int mft_zone_multiplier = -1, on_errors = -1;
108 	int show_sys_files = -1, case_sensitive = -1, disable_sparse = -1;
109 	struct nls_table *nls_map = NULL, *old_nls;
110 
111 	/* I am lazy... (-8 */
112 #define NTFS_GETOPT_WITH_DEFAULT(option, variable, default_value)	\
113 	if (!strcmp(p, option)) {					\
114 		if (!v || !*v)						\
115 			variable = default_value;			\
116 		else {							\
117 			variable = simple_strtoul(ov = v, &v, 0);	\
118 			if (*v)						\
119 				goto needs_val;				\
120 		}							\
121 	}
122 #define NTFS_GETOPT(option, variable)					\
123 	if (!strcmp(p, option)) {					\
124 		if (!v || !*v)						\
125 			goto needs_arg;					\
126 		variable = simple_strtoul(ov = v, &v, 0);		\
127 		if (*v)							\
128 			goto needs_val;					\
129 	}
130 #define NTFS_GETOPT_OCTAL(option, variable)				\
131 	if (!strcmp(p, option)) {					\
132 		if (!v || !*v)						\
133 			goto needs_arg;					\
134 		variable = simple_strtoul(ov = v, &v, 8);		\
135 		if (*v)							\
136 			goto needs_val;					\
137 	}
138 #define NTFS_GETOPT_BOOL(option, variable)				\
139 	if (!strcmp(p, option)) {					\
140 		BOOL val;						\
141 		if (!simple_getbool(v, &val))				\
142 			goto needs_bool;				\
143 		variable = val;						\
144 	}
145 #define NTFS_GETOPT_OPTIONS_ARRAY(option, variable, opt_array)		\
146 	if (!strcmp(p, option)) {					\
147 		int _i;							\
148 		if (!v || !*v)						\
149 			goto needs_arg;					\
150 		ov = v;							\
151 		if (variable == -1)					\
152 			variable = 0;					\
153 		for (_i = 0; opt_array[_i].str && *opt_array[_i].str; _i++) \
154 			if (!strcmp(opt_array[_i].str, v)) {		\
155 				variable |= opt_array[_i].val;		\
156 				break;					\
157 			}						\
158 		if (!opt_array[_i].str || !*opt_array[_i].str)		\
159 			goto needs_val;					\
160 	}
161 	if (!opt || !*opt)
162 		goto no_mount_options;
163 	ntfs_debug("Entering with mount options string: %s", opt);
164 	while ((p = strsep(&opt, ","))) {
165 		if ((v = strchr(p, '=')))
166 			*v++ = 0;
167 		NTFS_GETOPT("uid", uid)
168 		else NTFS_GETOPT("gid", gid)
169 		else NTFS_GETOPT_OCTAL("umask", fmask = dmask)
170 		else NTFS_GETOPT_OCTAL("fmask", fmask)
171 		else NTFS_GETOPT_OCTAL("dmask", dmask)
172 		else NTFS_GETOPT("mft_zone_multiplier", mft_zone_multiplier)
173 		else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, TRUE)
174 		else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files)
175 		else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive)
176 		else NTFS_GETOPT_BOOL("disable_sparse", disable_sparse)
177 		else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors,
178 				on_errors_arr)
179 		else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes"))
180 			ntfs_warning(vol->sb, "Ignoring obsolete option %s.",
181 					p);
182 		else if (!strcmp(p, "nls") || !strcmp(p, "iocharset")) {
183 			if (!strcmp(p, "iocharset"))
184 				ntfs_warning(vol->sb, "Option iocharset is "
185 						"deprecated. Please use "
186 						"option nls=<charsetname> in "
187 						"the future.");
188 			if (!v || !*v)
189 				goto needs_arg;
190 use_utf8:
191 			old_nls = nls_map;
192 			nls_map = load_nls(v);
193 			if (!nls_map) {
194 				if (!old_nls) {
195 					ntfs_error(vol->sb, "NLS character set "
196 							"%s not found.", v);
197 					return FALSE;
198 				}
199 				ntfs_error(vol->sb, "NLS character set %s not "
200 						"found. Using previous one %s.",
201 						v, old_nls->charset);
202 				nls_map = old_nls;
203 			} else /* nls_map */ {
204 				if (old_nls)
205 					unload_nls(old_nls);
206 			}
207 		} else if (!strcmp(p, "utf8")) {
208 			BOOL val = FALSE;
209 			ntfs_warning(vol->sb, "Option utf8 is no longer "
210 				   "supported, using option nls=utf8. Please "
211 				   "use option nls=utf8 in the future and "
212 				   "make sure utf8 is compiled either as a "
213 				   "module or into the kernel.");
214 			if (!v || !*v)
215 				val = TRUE;
216 			else if (!simple_getbool(v, &val))
217 				goto needs_bool;
218 			if (val) {
219 				v = utf8;
220 				goto use_utf8;
221 			}
222 		} else {
223 			ntfs_error(vol->sb, "Unrecognized mount option %s.", p);
224 			if (errors < INT_MAX)
225 				errors++;
226 		}
227 #undef NTFS_GETOPT_OPTIONS_ARRAY
228 #undef NTFS_GETOPT_BOOL
229 #undef NTFS_GETOPT
230 #undef NTFS_GETOPT_WITH_DEFAULT
231 	}
232 no_mount_options:
233 	if (errors && !sloppy)
234 		return FALSE;
235 	if (sloppy)
236 		ntfs_warning(vol->sb, "Sloppy option given. Ignoring "
237 				"unrecognized mount option(s) and continuing.");
238 	/* Keep this first! */
239 	if (on_errors != -1) {
240 		if (!on_errors) {
241 			ntfs_error(vol->sb, "Invalid errors option argument "
242 					"or bug in options parser.");
243 			return FALSE;
244 		}
245 	}
246 	if (nls_map) {
247 		if (vol->nls_map && vol->nls_map != nls_map) {
248 			ntfs_error(vol->sb, "Cannot change NLS character set "
249 					"on remount.");
250 			return FALSE;
251 		} /* else (!vol->nls_map) */
252 		ntfs_debug("Using NLS character set %s.", nls_map->charset);
253 		vol->nls_map = nls_map;
254 	} else /* (!nls_map) */ {
255 		if (!vol->nls_map) {
256 			vol->nls_map = load_nls_default();
257 			if (!vol->nls_map) {
258 				ntfs_error(vol->sb, "Failed to load default "
259 						"NLS character set.");
260 				return FALSE;
261 			}
262 			ntfs_debug("Using default NLS character set (%s).",
263 					vol->nls_map->charset);
264 		}
265 	}
266 	if (mft_zone_multiplier != -1) {
267 		if (vol->mft_zone_multiplier && vol->mft_zone_multiplier !=
268 				mft_zone_multiplier) {
269 			ntfs_error(vol->sb, "Cannot change mft_zone_multiplier "
270 					"on remount.");
271 			return FALSE;
272 		}
273 		if (mft_zone_multiplier < 1 || mft_zone_multiplier > 4) {
274 			ntfs_error(vol->sb, "Invalid mft_zone_multiplier. "
275 					"Using default value, i.e. 1.");
276 			mft_zone_multiplier = 1;
277 		}
278 		vol->mft_zone_multiplier = mft_zone_multiplier;
279 	}
280 	if (!vol->mft_zone_multiplier)
281 		vol->mft_zone_multiplier = 1;
282 	if (on_errors != -1)
283 		vol->on_errors = on_errors;
284 	if (!vol->on_errors || vol->on_errors == ON_ERRORS_RECOVER)
285 		vol->on_errors |= ON_ERRORS_CONTINUE;
286 	if (uid != (uid_t)-1)
287 		vol->uid = uid;
288 	if (gid != (gid_t)-1)
289 		vol->gid = gid;
290 	if (fmask != (mode_t)-1)
291 		vol->fmask = fmask;
292 	if (dmask != (mode_t)-1)
293 		vol->dmask = dmask;
294 	if (show_sys_files != -1) {
295 		if (show_sys_files)
296 			NVolSetShowSystemFiles(vol);
297 		else
298 			NVolClearShowSystemFiles(vol);
299 	}
300 	if (case_sensitive != -1) {
301 		if (case_sensitive)
302 			NVolSetCaseSensitive(vol);
303 		else
304 			NVolClearCaseSensitive(vol);
305 	}
306 	if (disable_sparse != -1) {
307 		if (disable_sparse)
308 			NVolClearSparseEnabled(vol);
309 		else {
310 			if (!NVolSparseEnabled(vol) &&
311 					vol->major_ver && vol->major_ver < 3)
312 				ntfs_warning(vol->sb, "Not enabling sparse "
313 						"support due to NTFS volume "
314 						"version %i.%i (need at least "
315 						"version 3.0).", vol->major_ver,
316 						vol->minor_ver);
317 			else
318 				NVolSetSparseEnabled(vol);
319 		}
320 	}
321 	return TRUE;
322 needs_arg:
323 	ntfs_error(vol->sb, "The %s option requires an argument.", p);
324 	return FALSE;
325 needs_bool:
326 	ntfs_error(vol->sb, "The %s option requires a boolean argument.", p);
327 	return FALSE;
328 needs_val:
329 	ntfs_error(vol->sb, "Invalid %s option argument: %s", p, ov);
330 	return FALSE;
331 }
332 
333 #ifdef NTFS_RW
334 
335 /**
336  * ntfs_write_volume_flags - write new flags to the volume information flags
337  * @vol:	ntfs volume on which to modify the flags
338  * @flags:	new flags value for the volume information flags
339  *
340  * Internal function.  You probably want to use ntfs_{set,clear}_volume_flags()
341  * instead (see below).
342  *
343  * Replace the volume information flags on the volume @vol with the value
344  * supplied in @flags.  Note, this overwrites the volume information flags, so
345  * make sure to combine the flags you want to modify with the old flags and use
346  * the result when calling ntfs_write_volume_flags().
347  *
348  * Return 0 on success and -errno on error.
349  */
350 static int ntfs_write_volume_flags(ntfs_volume *vol, const VOLUME_FLAGS flags)
351 {
352 	ntfs_inode *ni = NTFS_I(vol->vol_ino);
353 	MFT_RECORD *m;
354 	VOLUME_INFORMATION *vi;
355 	ntfs_attr_search_ctx *ctx;
356 	int err;
357 
358 	ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
359 			le16_to_cpu(vol->vol_flags), le16_to_cpu(flags));
360 	if (vol->vol_flags == flags)
361 		goto done;
362 	BUG_ON(!ni);
363 	m = map_mft_record(ni);
364 	if (IS_ERR(m)) {
365 		err = PTR_ERR(m);
366 		goto err_out;
367 	}
368 	ctx = ntfs_attr_get_search_ctx(ni, m);
369 	if (!ctx) {
370 		err = -ENOMEM;
371 		goto put_unm_err_out;
372 	}
373 	err = ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
374 			ctx);
375 	if (err)
376 		goto put_unm_err_out;
377 	vi = (VOLUME_INFORMATION*)((u8*)ctx->attr +
378 			le16_to_cpu(ctx->attr->data.resident.value_offset));
379 	vol->vol_flags = vi->flags = flags;
380 	flush_dcache_mft_record_page(ctx->ntfs_ino);
381 	mark_mft_record_dirty(ctx->ntfs_ino);
382 	ntfs_attr_put_search_ctx(ctx);
383 	unmap_mft_record(ni);
384 done:
385 	ntfs_debug("Done.");
386 	return 0;
387 put_unm_err_out:
388 	if (ctx)
389 		ntfs_attr_put_search_ctx(ctx);
390 	unmap_mft_record(ni);
391 err_out:
392 	ntfs_error(vol->sb, "Failed with error code %i.", -err);
393 	return err;
394 }
395 
396 /**
397  * ntfs_set_volume_flags - set bits in the volume information flags
398  * @vol:	ntfs volume on which to modify the flags
399  * @flags:	flags to set on the volume
400  *
401  * Set the bits in @flags in the volume information flags on the volume @vol.
402  *
403  * Return 0 on success and -errno on error.
404  */
405 static inline int ntfs_set_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
406 {
407 	flags &= VOLUME_FLAGS_MASK;
408 	return ntfs_write_volume_flags(vol, vol->vol_flags | flags);
409 }
410 
411 /**
412  * ntfs_clear_volume_flags - clear bits in the volume information flags
413  * @vol:	ntfs volume on which to modify the flags
414  * @flags:	flags to clear on the volume
415  *
416  * Clear the bits in @flags in the volume information flags on the volume @vol.
417  *
418  * Return 0 on success and -errno on error.
419  */
420 static inline int ntfs_clear_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
421 {
422 	flags &= VOLUME_FLAGS_MASK;
423 	flags = vol->vol_flags & cpu_to_le16(~le16_to_cpu(flags));
424 	return ntfs_write_volume_flags(vol, flags);
425 }
426 
427 #endif /* NTFS_RW */
428 
429 /**
430  * ntfs_remount - change the mount options of a mounted ntfs filesystem
431  * @sb:		superblock of mounted ntfs filesystem
432  * @flags:	remount flags
433  * @opt:	remount options string
434  *
435  * Change the mount options of an already mounted ntfs filesystem.
436  *
437  * NOTE:  The VFS sets the @sb->s_flags remount flags to @flags after
438  * ntfs_remount() returns successfully (i.e. returns 0).  Otherwise,
439  * @sb->s_flags are not changed.
440  */
441 static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
442 {
443 	ntfs_volume *vol = NTFS_SB(sb);
444 
445 	ntfs_debug("Entering with remount options string: %s", opt);
446 #ifndef NTFS_RW
447 	/* For read-only compiled driver, enforce read-only flag. */
448 	*flags |= MS_RDONLY;
449 #else /* NTFS_RW */
450 	/*
451 	 * For the read-write compiled driver, if we are remounting read-write,
452 	 * make sure there are no volume errors and that no unsupported volume
453 	 * flags are set.  Also, empty the logfile journal as it would become
454 	 * stale as soon as something is written to the volume and mark the
455 	 * volume dirty so that chkdsk is run if the volume is not umounted
456 	 * cleanly.  Finally, mark the quotas out of date so Windows rescans
457 	 * the volume on boot and updates them.
458 	 *
459 	 * When remounting read-only, mark the volume clean if no volume errors
460 	 * have occured.
461 	 */
462 	if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
463 		static const char *es = ".  Cannot remount read-write.";
464 
465 		/* Remounting read-write. */
466 		if (NVolErrors(vol)) {
467 			ntfs_error(sb, "Volume has errors and is read-only%s",
468 					es);
469 			return -EROFS;
470 		}
471 		if (vol->vol_flags & VOLUME_IS_DIRTY) {
472 			ntfs_error(sb, "Volume is dirty and read-only%s", es);
473 			return -EROFS;
474 		}
475 		if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
476 			ntfs_error(sb, "Volume has been modified by chkdsk "
477 					"and is read-only%s", es);
478 			return -EROFS;
479 		}
480 		if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
481 			ntfs_error(sb, "Volume has unsupported flags set "
482 					"(0x%x) and is read-only%s",
483 					(unsigned)le16_to_cpu(vol->vol_flags),
484 					es);
485 			return -EROFS;
486 		}
487 		if (ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
488 			ntfs_error(sb, "Failed to set dirty bit in volume "
489 					"information flags%s", es);
490 			return -EROFS;
491 		}
492 #if 0
493 		// TODO: Enable this code once we start modifying anything that
494 		//	 is different between NTFS 1.2 and 3.x...
495 		/* Set NT4 compatibility flag on newer NTFS version volumes. */
496 		if ((vol->major_ver > 1)) {
497 			if (ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
498 				ntfs_error(sb, "Failed to set NT4 "
499 						"compatibility flag%s", es);
500 				NVolSetErrors(vol);
501 				return -EROFS;
502 			}
503 		}
504 #endif
505 		if (!ntfs_empty_logfile(vol->logfile_ino)) {
506 			ntfs_error(sb, "Failed to empty journal $LogFile%s",
507 					es);
508 			NVolSetErrors(vol);
509 			return -EROFS;
510 		}
511 		if (!ntfs_mark_quotas_out_of_date(vol)) {
512 			ntfs_error(sb, "Failed to mark quotas out of date%s",
513 					es);
514 			NVolSetErrors(vol);
515 			return -EROFS;
516 		}
517 		if (!ntfs_stamp_usnjrnl(vol)) {
518 			ntfs_error(sb, "Failed to stamp transation log "
519 					"($UsnJrnl)%s", es);
520 			NVolSetErrors(vol);
521 			return -EROFS;
522 		}
523 	} else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) {
524 		/* Remounting read-only. */
525 		if (!NVolErrors(vol)) {
526 			if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
527 				ntfs_warning(sb, "Failed to clear dirty bit "
528 						"in volume information "
529 						"flags.  Run chkdsk.");
530 		}
531 	}
532 #endif /* NTFS_RW */
533 
534 	// TODO: Deal with *flags.
535 
536 	if (!parse_options(vol, opt))
537 		return -EINVAL;
538 	ntfs_debug("Done.");
539 	return 0;
540 }
541 
542 /**
543  * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector
544  * @sb:		Super block of the device to which @b belongs.
545  * @b:		Boot sector of device @sb to check.
546  * @silent:	If TRUE, all output will be silenced.
547  *
548  * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot
549  * sector. Returns TRUE if it is valid and FALSE if not.
550  *
551  * @sb is only needed for warning/error output, i.e. it can be NULL when silent
552  * is TRUE.
553  */
554 static BOOL is_boot_sector_ntfs(const struct super_block *sb,
555 		const NTFS_BOOT_SECTOR *b, const BOOL silent)
556 {
557 	/*
558 	 * Check that checksum == sum of u32 values from b to the checksum
559 	 * field.  If checksum is zero, no checking is done.  We will work when
560 	 * the checksum test fails, since some utilities update the boot sector
561 	 * ignoring the checksum which leaves the checksum out-of-date.  We
562 	 * report a warning if this is the case.
563 	 */
564 	if ((void*)b < (void*)&b->checksum && b->checksum && !silent) {
565 		le32 *u;
566 		u32 i;
567 
568 		for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u)
569 			i += le32_to_cpup(u);
570 		if (le32_to_cpu(b->checksum) != i)
571 			ntfs_warning(sb, "Invalid boot sector checksum.");
572 	}
573 	/* Check OEMidentifier is "NTFS    " */
574 	if (b->oem_id != magicNTFS)
575 		goto not_ntfs;
576 	/* Check bytes per sector value is between 256 and 4096. */
577 	if (le16_to_cpu(b->bpb.bytes_per_sector) < 0x100 ||
578 			le16_to_cpu(b->bpb.bytes_per_sector) > 0x1000)
579 		goto not_ntfs;
580 	/* Check sectors per cluster value is valid. */
581 	switch (b->bpb.sectors_per_cluster) {
582 	case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128:
583 		break;
584 	default:
585 		goto not_ntfs;
586 	}
587 	/* Check the cluster size is not above the maximum (64kiB). */
588 	if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) *
589 			b->bpb.sectors_per_cluster > NTFS_MAX_CLUSTER_SIZE)
590 		goto not_ntfs;
591 	/* Check reserved/unused fields are really zero. */
592 	if (le16_to_cpu(b->bpb.reserved_sectors) ||
593 			le16_to_cpu(b->bpb.root_entries) ||
594 			le16_to_cpu(b->bpb.sectors) ||
595 			le16_to_cpu(b->bpb.sectors_per_fat) ||
596 			le32_to_cpu(b->bpb.large_sectors) || b->bpb.fats)
597 		goto not_ntfs;
598 	/* Check clusters per file mft record value is valid. */
599 	if ((u8)b->clusters_per_mft_record < 0xe1 ||
600 			(u8)b->clusters_per_mft_record > 0xf7)
601 		switch (b->clusters_per_mft_record) {
602 		case 1: case 2: case 4: case 8: case 16: case 32: case 64:
603 			break;
604 		default:
605 			goto not_ntfs;
606 		}
607 	/* Check clusters per index block value is valid. */
608 	if ((u8)b->clusters_per_index_record < 0xe1 ||
609 			(u8)b->clusters_per_index_record > 0xf7)
610 		switch (b->clusters_per_index_record) {
611 		case 1: case 2: case 4: case 8: case 16: case 32: case 64:
612 			break;
613 		default:
614 			goto not_ntfs;
615 		}
616 	/*
617 	 * Check for valid end of sector marker. We will work without it, but
618 	 * many BIOSes will refuse to boot from a bootsector if the magic is
619 	 * incorrect, so we emit a warning.
620 	 */
621 	if (!silent && b->end_of_sector_marker != const_cpu_to_le16(0xaa55))
622 		ntfs_warning(sb, "Invalid end of sector marker.");
623 	return TRUE;
624 not_ntfs:
625 	return FALSE;
626 }
627 
628 /**
629  * read_ntfs_boot_sector - read the NTFS boot sector of a device
630  * @sb:		super block of device to read the boot sector from
631  * @silent:	if true, suppress all output
632  *
633  * Reads the boot sector from the device and validates it. If that fails, tries
634  * to read the backup boot sector, first from the end of the device a-la NT4 and
635  * later and then from the middle of the device a-la NT3.51 and before.
636  *
637  * If a valid boot sector is found but it is not the primary boot sector, we
638  * repair the primary boot sector silently (unless the device is read-only or
639  * the primary boot sector is not accessible).
640  *
641  * NOTE: To call this function, @sb must have the fields s_dev, the ntfs super
642  * block (u.ntfs_sb), nr_blocks and the device flags (s_flags) initialized
643  * to their respective values.
644  *
645  * Return the unlocked buffer head containing the boot sector or NULL on error.
646  */
647 static struct buffer_head *read_ntfs_boot_sector(struct super_block *sb,
648 		const int silent)
649 {
650 	const char *read_err_str = "Unable to read %s boot sector.";
651 	struct buffer_head *bh_primary, *bh_backup;
652 	sector_t nr_blocks = NTFS_SB(sb)->nr_blocks;
653 
654 	/* Try to read primary boot sector. */
655 	if ((bh_primary = sb_bread(sb, 0))) {
656 		if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
657 				bh_primary->b_data, silent))
658 			return bh_primary;
659 		if (!silent)
660 			ntfs_error(sb, "Primary boot sector is invalid.");
661 	} else if (!silent)
662 		ntfs_error(sb, read_err_str, "primary");
663 	if (!(NTFS_SB(sb)->on_errors & ON_ERRORS_RECOVER)) {
664 		if (bh_primary)
665 			brelse(bh_primary);
666 		if (!silent)
667 			ntfs_error(sb, "Mount option errors=recover not used. "
668 					"Aborting without trying to recover.");
669 		return NULL;
670 	}
671 	/* Try to read NT4+ backup boot sector. */
672 	if ((bh_backup = sb_bread(sb, nr_blocks - 1))) {
673 		if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
674 				bh_backup->b_data, silent))
675 			goto hotfix_primary_boot_sector;
676 		brelse(bh_backup);
677 	} else if (!silent)
678 		ntfs_error(sb, read_err_str, "backup");
679 	/* Try to read NT3.51- backup boot sector. */
680 	if ((bh_backup = sb_bread(sb, nr_blocks >> 1))) {
681 		if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
682 				bh_backup->b_data, silent))
683 			goto hotfix_primary_boot_sector;
684 		if (!silent)
685 			ntfs_error(sb, "Could not find a valid backup boot "
686 					"sector.");
687 		brelse(bh_backup);
688 	} else if (!silent)
689 		ntfs_error(sb, read_err_str, "backup");
690 	/* We failed. Cleanup and return. */
691 	if (bh_primary)
692 		brelse(bh_primary);
693 	return NULL;
694 hotfix_primary_boot_sector:
695 	if (bh_primary) {
696 		/*
697 		 * If we managed to read sector zero and the volume is not
698 		 * read-only, copy the found, valid backup boot sector to the
699 		 * primary boot sector.  Note we only copy the actual boot
700 		 * sector structure, not the actual whole device sector as that
701 		 * may be bigger and would potentially damage the $Boot system
702 		 * file (FIXME: Would be nice to know if the backup boot sector
703 		 * on a large sector device contains the whole boot loader or
704 		 * just the first 512 bytes).
705 		 */
706 		if (!(sb->s_flags & MS_RDONLY)) {
707 			ntfs_warning(sb, "Hot-fix: Recovering invalid primary "
708 					"boot sector from backup copy.");
709 			memcpy(bh_primary->b_data, bh_backup->b_data,
710 					NTFS_BLOCK_SIZE);
711 			mark_buffer_dirty(bh_primary);
712 			sync_dirty_buffer(bh_primary);
713 			if (buffer_uptodate(bh_primary)) {
714 				brelse(bh_backup);
715 				return bh_primary;
716 			}
717 			ntfs_error(sb, "Hot-fix: Device write error while "
718 					"recovering primary boot sector.");
719 		} else {
720 			ntfs_warning(sb, "Hot-fix: Recovery of primary boot "
721 					"sector failed: Read-only mount.");
722 		}
723 		brelse(bh_primary);
724 	}
725 	ntfs_warning(sb, "Using backup boot sector.");
726 	return bh_backup;
727 }
728 
729 /**
730  * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol
731  * @vol:	volume structure to initialise with data from boot sector
732  * @b:		boot sector to parse
733  *
734  * Parse the ntfs boot sector @b and store all imporant information therein in
735  * the ntfs super block @vol.  Return TRUE on success and FALSE on error.
736  */
737 static BOOL parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
738 {
739 	unsigned int sectors_per_cluster_bits, nr_hidden_sects;
740 	int clusters_per_mft_record, clusters_per_index_record;
741 	s64 ll;
742 
743 	vol->sector_size = le16_to_cpu(b->bpb.bytes_per_sector);
744 	vol->sector_size_bits = ffs(vol->sector_size) - 1;
745 	ntfs_debug("vol->sector_size = %i (0x%x)", vol->sector_size,
746 			vol->sector_size);
747 	ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits,
748 			vol->sector_size_bits);
749 	if (vol->sector_size < vol->sb->s_blocksize) {
750 		ntfs_error(vol->sb, "Sector size (%i) is smaller than the "
751 				"device block size (%lu).  This is not "
752 				"supported.  Sorry.", vol->sector_size,
753 				vol->sb->s_blocksize);
754 		return FALSE;
755 	}
756 	ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster);
757 	sectors_per_cluster_bits = ffs(b->bpb.sectors_per_cluster) - 1;
758 	ntfs_debug("sectors_per_cluster_bits = 0x%x",
759 			sectors_per_cluster_bits);
760 	nr_hidden_sects = le32_to_cpu(b->bpb.hidden_sectors);
761 	ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects);
762 	vol->cluster_size = vol->sector_size << sectors_per_cluster_bits;
763 	vol->cluster_size_mask = vol->cluster_size - 1;
764 	vol->cluster_size_bits = ffs(vol->cluster_size) - 1;
765 	ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size,
766 			vol->cluster_size);
767 	ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask);
768 	ntfs_debug("vol->cluster_size_bits = %i", vol->cluster_size_bits);
769 	if (vol->cluster_size < vol->sector_size) {
770 		ntfs_error(vol->sb, "Cluster size (%i) is smaller than the "
771 				"sector size (%i).  This is not supported.  "
772 				"Sorry.", vol->cluster_size, vol->sector_size);
773 		return FALSE;
774 	}
775 	clusters_per_mft_record = b->clusters_per_mft_record;
776 	ntfs_debug("clusters_per_mft_record = %i (0x%x)",
777 			clusters_per_mft_record, clusters_per_mft_record);
778 	if (clusters_per_mft_record > 0)
779 		vol->mft_record_size = vol->cluster_size <<
780 				(ffs(clusters_per_mft_record) - 1);
781 	else
782 		/*
783 		 * When mft_record_size < cluster_size, clusters_per_mft_record
784 		 * = -log2(mft_record_size) bytes. mft_record_size normaly is
785 		 * 1024 bytes, which is encoded as 0xF6 (-10 in decimal).
786 		 */
787 		vol->mft_record_size = 1 << -clusters_per_mft_record;
788 	vol->mft_record_size_mask = vol->mft_record_size - 1;
789 	vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1;
790 	ntfs_debug("vol->mft_record_size = %i (0x%x)", vol->mft_record_size,
791 			vol->mft_record_size);
792 	ntfs_debug("vol->mft_record_size_mask = 0x%x",
793 			vol->mft_record_size_mask);
794 	ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
795 			vol->mft_record_size_bits, vol->mft_record_size_bits);
796 	/*
797 	 * We cannot support mft record sizes above the PAGE_CACHE_SIZE since
798 	 * we store $MFT/$DATA, the table of mft records in the page cache.
799 	 */
800 	if (vol->mft_record_size > PAGE_CACHE_SIZE) {
801 		ntfs_error(vol->sb, "Mft record size (%i) exceeds the "
802 				"PAGE_CACHE_SIZE on your system (%lu).  "
803 				"This is not supported.  Sorry.",
804 				vol->mft_record_size, PAGE_CACHE_SIZE);
805 		return FALSE;
806 	}
807 	/* We cannot support mft record sizes below the sector size. */
808 	if (vol->mft_record_size < vol->sector_size) {
809 		ntfs_error(vol->sb, "Mft record size (%i) is smaller than the "
810 				"sector size (%i).  This is not supported.  "
811 				"Sorry.", vol->mft_record_size,
812 				vol->sector_size);
813 		return FALSE;
814 	}
815 	clusters_per_index_record = b->clusters_per_index_record;
816 	ntfs_debug("clusters_per_index_record = %i (0x%x)",
817 			clusters_per_index_record, clusters_per_index_record);
818 	if (clusters_per_index_record > 0)
819 		vol->index_record_size = vol->cluster_size <<
820 				(ffs(clusters_per_index_record) - 1);
821 	else
822 		/*
823 		 * When index_record_size < cluster_size,
824 		 * clusters_per_index_record = -log2(index_record_size) bytes.
825 		 * index_record_size normaly equals 4096 bytes, which is
826 		 * encoded as 0xF4 (-12 in decimal).
827 		 */
828 		vol->index_record_size = 1 << -clusters_per_index_record;
829 	vol->index_record_size_mask = vol->index_record_size - 1;
830 	vol->index_record_size_bits = ffs(vol->index_record_size) - 1;
831 	ntfs_debug("vol->index_record_size = %i (0x%x)",
832 			vol->index_record_size, vol->index_record_size);
833 	ntfs_debug("vol->index_record_size_mask = 0x%x",
834 			vol->index_record_size_mask);
835 	ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
836 			vol->index_record_size_bits,
837 			vol->index_record_size_bits);
838 	/* We cannot support index record sizes below the sector size. */
839 	if (vol->index_record_size < vol->sector_size) {
840 		ntfs_error(vol->sb, "Index record size (%i) is smaller than "
841 				"the sector size (%i).  This is not "
842 				"supported.  Sorry.", vol->index_record_size,
843 				vol->sector_size);
844 		return FALSE;
845 	}
846 	/*
847 	 * Get the size of the volume in clusters and check for 64-bit-ness.
848 	 * Windows currently only uses 32 bits to save the clusters so we do
849 	 * the same as it is much faster on 32-bit CPUs.
850 	 */
851 	ll = sle64_to_cpu(b->number_of_sectors) >> sectors_per_cluster_bits;
852 	if ((u64)ll >= 1ULL << 32) {
853 		ntfs_error(vol->sb, "Cannot handle 64-bit clusters.  Sorry.");
854 		return FALSE;
855 	}
856 	vol->nr_clusters = ll;
857 	ntfs_debug("vol->nr_clusters = 0x%llx", (long long)vol->nr_clusters);
858 	/*
859 	 * On an architecture where unsigned long is 32-bits, we restrict the
860 	 * volume size to 2TiB (2^41). On a 64-bit architecture, the compiler
861 	 * will hopefully optimize the whole check away.
862 	 */
863 	if (sizeof(unsigned long) < 8) {
864 		if ((ll << vol->cluster_size_bits) >= (1ULL << 41)) {
865 			ntfs_error(vol->sb, "Volume size (%lluTiB) is too "
866 					"large for this architecture.  "
867 					"Maximum supported is 2TiB.  Sorry.",
868 					(unsigned long long)ll >> (40 -
869 					vol->cluster_size_bits));
870 			return FALSE;
871 		}
872 	}
873 	ll = sle64_to_cpu(b->mft_lcn);
874 	if (ll >= vol->nr_clusters) {
875 		ntfs_error(vol->sb, "MFT LCN (%lli, 0x%llx) is beyond end of "
876 				"volume.  Weird.", (unsigned long long)ll,
877 				(unsigned long long)ll);
878 		return FALSE;
879 	}
880 	vol->mft_lcn = ll;
881 	ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol->mft_lcn);
882 	ll = sle64_to_cpu(b->mftmirr_lcn);
883 	if (ll >= vol->nr_clusters) {
884 		ntfs_error(vol->sb, "MFTMirr LCN (%lli, 0x%llx) is beyond end "
885 				"of volume.  Weird.", (unsigned long long)ll,
886 				(unsigned long long)ll);
887 		return FALSE;
888 	}
889 	vol->mftmirr_lcn = ll;
890 	ntfs_debug("vol->mftmirr_lcn = 0x%llx", (long long)vol->mftmirr_lcn);
891 #ifdef NTFS_RW
892 	/*
893 	 * Work out the size of the mft mirror in number of mft records. If the
894 	 * cluster size is less than or equal to the size taken by four mft
895 	 * records, the mft mirror stores the first four mft records. If the
896 	 * cluster size is bigger than the size taken by four mft records, the
897 	 * mft mirror contains as many mft records as will fit into one
898 	 * cluster.
899 	 */
900 	if (vol->cluster_size <= (4 << vol->mft_record_size_bits))
901 		vol->mftmirr_size = 4;
902 	else
903 		vol->mftmirr_size = vol->cluster_size >>
904 				vol->mft_record_size_bits;
905 	ntfs_debug("vol->mftmirr_size = %i", vol->mftmirr_size);
906 #endif /* NTFS_RW */
907 	vol->serial_no = le64_to_cpu(b->volume_serial_number);
908 	ntfs_debug("vol->serial_no = 0x%llx",
909 			(unsigned long long)vol->serial_no);
910 	return TRUE;
911 }
912 
913 /**
914  * ntfs_setup_allocators - initialize the cluster and mft allocators
915  * @vol:	volume structure for which to setup the allocators
916  *
917  * Setup the cluster (lcn) and mft allocators to the starting values.
918  */
919 static void ntfs_setup_allocators(ntfs_volume *vol)
920 {
921 #ifdef NTFS_RW
922 	LCN mft_zone_size, mft_lcn;
923 #endif /* NTFS_RW */
924 
925 	ntfs_debug("vol->mft_zone_multiplier = 0x%x",
926 			vol->mft_zone_multiplier);
927 #ifdef NTFS_RW
928 	/* Determine the size of the MFT zone. */
929 	mft_zone_size = vol->nr_clusters;
930 	switch (vol->mft_zone_multiplier) {  /* % of volume size in clusters */
931 	case 4:
932 		mft_zone_size >>= 1;			/* 50%   */
933 		break;
934 	case 3:
935 		mft_zone_size = (mft_zone_size +
936 				(mft_zone_size >> 1)) >> 2;	/* 37.5% */
937 		break;
938 	case 2:
939 		mft_zone_size >>= 2;			/* 25%   */
940 		break;
941 	/* case 1: */
942 	default:
943 		mft_zone_size >>= 3;			/* 12.5% */
944 		break;
945 	}
946 	/* Setup the mft zone. */
947 	vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn;
948 	ntfs_debug("vol->mft_zone_pos = 0x%llx",
949 			(unsigned long long)vol->mft_zone_pos);
950 	/*
951 	 * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
952 	 * source) and if the actual mft_lcn is in the expected place or even
953 	 * further to the front of the volume, extend the mft_zone to cover the
954 	 * beginning of the volume as well.  This is in order to protect the
955 	 * area reserved for the mft bitmap as well within the mft_zone itself.
956 	 * On non-standard volumes we do not protect it as the overhead would
957 	 * be higher than the speed increase we would get by doing it.
958 	 */
959 	mft_lcn = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size;
960 	if (mft_lcn * vol->cluster_size < 16 * 1024)
961 		mft_lcn = (16 * 1024 + vol->cluster_size - 1) /
962 				vol->cluster_size;
963 	if (vol->mft_zone_start <= mft_lcn)
964 		vol->mft_zone_start = 0;
965 	ntfs_debug("vol->mft_zone_start = 0x%llx",
966 			(unsigned long long)vol->mft_zone_start);
967 	/*
968 	 * Need to cap the mft zone on non-standard volumes so that it does
969 	 * not point outside the boundaries of the volume.  We do this by
970 	 * halving the zone size until we are inside the volume.
971 	 */
972 	vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
973 	while (vol->mft_zone_end >= vol->nr_clusters) {
974 		mft_zone_size >>= 1;
975 		vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
976 	}
977 	ntfs_debug("vol->mft_zone_end = 0x%llx",
978 			(unsigned long long)vol->mft_zone_end);
979 	/*
980 	 * Set the current position within each data zone to the start of the
981 	 * respective zone.
982 	 */
983 	vol->data1_zone_pos = vol->mft_zone_end;
984 	ntfs_debug("vol->data1_zone_pos = 0x%llx",
985 			(unsigned long long)vol->data1_zone_pos);
986 	vol->data2_zone_pos = 0;
987 	ntfs_debug("vol->data2_zone_pos = 0x%llx",
988 			(unsigned long long)vol->data2_zone_pos);
989 
990 	/* Set the mft data allocation position to mft record 24. */
991 	vol->mft_data_pos = 24;
992 	ntfs_debug("vol->mft_data_pos = 0x%llx",
993 			(unsigned long long)vol->mft_data_pos);
994 #endif /* NTFS_RW */
995 }
996 
997 #ifdef NTFS_RW
998 
999 /**
1000  * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume
1001  * @vol:	ntfs super block describing device whose mft mirror to load
1002  *
1003  * Return TRUE on success or FALSE on error.
1004  */
1005 static BOOL load_and_init_mft_mirror(ntfs_volume *vol)
1006 {
1007 	struct inode *tmp_ino;
1008 	ntfs_inode *tmp_ni;
1009 
1010 	ntfs_debug("Entering.");
1011 	/* Get mft mirror inode. */
1012 	tmp_ino = ntfs_iget(vol->sb, FILE_MFTMirr);
1013 	if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1014 		if (!IS_ERR(tmp_ino))
1015 			iput(tmp_ino);
1016 		/* Caller will display error message. */
1017 		return FALSE;
1018 	}
1019 	/*
1020 	 * Re-initialize some specifics about $MFTMirr's inode as
1021 	 * ntfs_read_inode() will have set up the default ones.
1022 	 */
1023 	/* Set uid and gid to root. */
1024 	tmp_ino->i_uid = tmp_ino->i_gid = 0;
1025 	/* Regular file.  No access for anyone. */
1026 	tmp_ino->i_mode = S_IFREG;
1027 	/* No VFS initiated operations allowed for $MFTMirr. */
1028 	tmp_ino->i_op = &ntfs_empty_inode_ops;
1029 	tmp_ino->i_fop = &ntfs_empty_file_ops;
1030 	/* Put in our special address space operations. */
1031 	tmp_ino->i_mapping->a_ops = &ntfs_mst_aops;
1032 	tmp_ni = NTFS_I(tmp_ino);
1033 	/* The $MFTMirr, like the $MFT is multi sector transfer protected. */
1034 	NInoSetMstProtected(tmp_ni);
1035 	NInoSetSparseDisabled(tmp_ni);
1036 	/*
1037 	 * Set up our little cheat allowing us to reuse the async read io
1038 	 * completion handler for directories.
1039 	 */
1040 	tmp_ni->itype.index.block_size = vol->mft_record_size;
1041 	tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits;
1042 	vol->mftmirr_ino = tmp_ino;
1043 	ntfs_debug("Done.");
1044 	return TRUE;
1045 }
1046 
1047 /**
1048  * check_mft_mirror - compare contents of the mft mirror with the mft
1049  * @vol:	ntfs super block describing device whose mft mirror to check
1050  *
1051  * Return TRUE on success or FALSE on error.
1052  *
1053  * Note, this function also results in the mft mirror runlist being completely
1054  * mapped into memory.  The mft mirror write code requires this and will BUG()
1055  * should it find an unmapped runlist element.
1056  */
1057 static BOOL check_mft_mirror(ntfs_volume *vol)
1058 {
1059 	struct super_block *sb = vol->sb;
1060 	ntfs_inode *mirr_ni;
1061 	struct page *mft_page, *mirr_page;
1062 	u8 *kmft, *kmirr;
1063 	runlist_element *rl, rl2[2];
1064 	pgoff_t index;
1065 	int mrecs_per_page, i;
1066 
1067 	ntfs_debug("Entering.");
1068 	/* Compare contents of $MFT and $MFTMirr. */
1069 	mrecs_per_page = PAGE_CACHE_SIZE / vol->mft_record_size;
1070 	BUG_ON(!mrecs_per_page);
1071 	BUG_ON(!vol->mftmirr_size);
1072 	mft_page = mirr_page = NULL;
1073 	kmft = kmirr = NULL;
1074 	index = i = 0;
1075 	do {
1076 		u32 bytes;
1077 
1078 		/* Switch pages if necessary. */
1079 		if (!(i % mrecs_per_page)) {
1080 			if (index) {
1081 				ntfs_unmap_page(mft_page);
1082 				ntfs_unmap_page(mirr_page);
1083 			}
1084 			/* Get the $MFT page. */
1085 			mft_page = ntfs_map_page(vol->mft_ino->i_mapping,
1086 					index);
1087 			if (IS_ERR(mft_page)) {
1088 				ntfs_error(sb, "Failed to read $MFT.");
1089 				return FALSE;
1090 			}
1091 			kmft = page_address(mft_page);
1092 			/* Get the $MFTMirr page. */
1093 			mirr_page = ntfs_map_page(vol->mftmirr_ino->i_mapping,
1094 					index);
1095 			if (IS_ERR(mirr_page)) {
1096 				ntfs_error(sb, "Failed to read $MFTMirr.");
1097 				goto mft_unmap_out;
1098 			}
1099 			kmirr = page_address(mirr_page);
1100 			++index;
1101 		}
1102 		/* Make sure the record is ok. */
1103 		if (ntfs_is_baad_recordp((le32*)kmft)) {
1104 			ntfs_error(sb, "Incomplete multi sector transfer "
1105 					"detected in mft record %i.", i);
1106 mm_unmap_out:
1107 			ntfs_unmap_page(mirr_page);
1108 mft_unmap_out:
1109 			ntfs_unmap_page(mft_page);
1110 			return FALSE;
1111 		}
1112 		if (ntfs_is_baad_recordp((le32*)kmirr)) {
1113 			ntfs_error(sb, "Incomplete multi sector transfer "
1114 					"detected in mft mirror record %i.", i);
1115 			goto mm_unmap_out;
1116 		}
1117 		/* Get the amount of data in the current record. */
1118 		bytes = le32_to_cpu(((MFT_RECORD*)kmft)->bytes_in_use);
1119 		if (!bytes || bytes > vol->mft_record_size) {
1120 			bytes = le32_to_cpu(((MFT_RECORD*)kmirr)->bytes_in_use);
1121 			if (!bytes || bytes > vol->mft_record_size)
1122 				bytes = vol->mft_record_size;
1123 		}
1124 		/* Compare the two records. */
1125 		if (memcmp(kmft, kmirr, bytes)) {
1126 			ntfs_error(sb, "$MFT and $MFTMirr (record %i) do not "
1127 					"match.  Run ntfsfix or chkdsk.", i);
1128 			goto mm_unmap_out;
1129 		}
1130 		kmft += vol->mft_record_size;
1131 		kmirr += vol->mft_record_size;
1132 	} while (++i < vol->mftmirr_size);
1133 	/* Release the last pages. */
1134 	ntfs_unmap_page(mft_page);
1135 	ntfs_unmap_page(mirr_page);
1136 
1137 	/* Construct the mft mirror runlist by hand. */
1138 	rl2[0].vcn = 0;
1139 	rl2[0].lcn = vol->mftmirr_lcn;
1140 	rl2[0].length = (vol->mftmirr_size * vol->mft_record_size +
1141 			vol->cluster_size - 1) / vol->cluster_size;
1142 	rl2[1].vcn = rl2[0].length;
1143 	rl2[1].lcn = LCN_ENOENT;
1144 	rl2[1].length = 0;
1145 	/*
1146 	 * Because we have just read all of the mft mirror, we know we have
1147 	 * mapped the full runlist for it.
1148 	 */
1149 	mirr_ni = NTFS_I(vol->mftmirr_ino);
1150 	down_read(&mirr_ni->runlist.lock);
1151 	rl = mirr_ni->runlist.rl;
1152 	/* Compare the two runlists.  They must be identical. */
1153 	i = 0;
1154 	do {
1155 		if (rl2[i].vcn != rl[i].vcn || rl2[i].lcn != rl[i].lcn ||
1156 				rl2[i].length != rl[i].length) {
1157 			ntfs_error(sb, "$MFTMirr location mismatch.  "
1158 					"Run chkdsk.");
1159 			up_read(&mirr_ni->runlist.lock);
1160 			return FALSE;
1161 		}
1162 	} while (rl2[i++].length);
1163 	up_read(&mirr_ni->runlist.lock);
1164 	ntfs_debug("Done.");
1165 	return TRUE;
1166 }
1167 
1168 /**
1169  * load_and_check_logfile - load and check the logfile inode for a volume
1170  * @vol:	ntfs super block describing device whose logfile to load
1171  *
1172  * Return TRUE on success or FALSE on error.
1173  */
1174 static BOOL load_and_check_logfile(ntfs_volume *vol,
1175 		RESTART_PAGE_HEADER **rp)
1176 {
1177 	struct inode *tmp_ino;
1178 
1179 	ntfs_debug("Entering.");
1180 	tmp_ino = ntfs_iget(vol->sb, FILE_LogFile);
1181 	if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1182 		if (!IS_ERR(tmp_ino))
1183 			iput(tmp_ino);
1184 		/* Caller will display error message. */
1185 		return FALSE;
1186 	}
1187 	if (!ntfs_check_logfile(tmp_ino, rp)) {
1188 		iput(tmp_ino);
1189 		/* ntfs_check_logfile() will have displayed error output. */
1190 		return FALSE;
1191 	}
1192 	NInoSetSparseDisabled(NTFS_I(tmp_ino));
1193 	vol->logfile_ino = tmp_ino;
1194 	ntfs_debug("Done.");
1195 	return TRUE;
1196 }
1197 
1198 #define NTFS_HIBERFIL_HEADER_SIZE	4096
1199 
1200 /**
1201  * check_windows_hibernation_status - check if Windows is suspended on a volume
1202  * @vol:	ntfs super block of device to check
1203  *
1204  * Check if Windows is hibernated on the ntfs volume @vol.  This is done by
1205  * looking for the file hiberfil.sys in the root directory of the volume.  If
1206  * the file is not present Windows is definitely not suspended.
1207  *
1208  * If hiberfil.sys exists and is less than 4kiB in size it means Windows is
1209  * definitely suspended (this volume is not the system volume).  Caveat:  on a
1210  * system with many volumes it is possible that the < 4kiB check is bogus but
1211  * for now this should do fine.
1212  *
1213  * If hiberfil.sys exists and is larger than 4kiB in size, we need to read the
1214  * hiberfil header (which is the first 4kiB).  If this begins with "hibr",
1215  * Windows is definitely suspended.  If it is completely full of zeroes,
1216  * Windows is definitely not hibernated.  Any other case is treated as if
1217  * Windows is suspended.  This caters for the above mentioned caveat of a
1218  * system with many volumes where no "hibr" magic would be present and there is
1219  * no zero header.
1220  *
1221  * Return 0 if Windows is not hibernated on the volume, >0 if Windows is
1222  * hibernated on the volume, and -errno on error.
1223  */
1224 static int check_windows_hibernation_status(ntfs_volume *vol)
1225 {
1226 	MFT_REF mref;
1227 	struct inode *vi;
1228 	ntfs_inode *ni;
1229 	struct page *page;
1230 	u32 *kaddr, *kend;
1231 	ntfs_name *name = NULL;
1232 	int ret = 1;
1233 	static const ntfschar hiberfil[13] = { const_cpu_to_le16('h'),
1234 			const_cpu_to_le16('i'), const_cpu_to_le16('b'),
1235 			const_cpu_to_le16('e'), const_cpu_to_le16('r'),
1236 			const_cpu_to_le16('f'), const_cpu_to_le16('i'),
1237 			const_cpu_to_le16('l'), const_cpu_to_le16('.'),
1238 			const_cpu_to_le16('s'), const_cpu_to_le16('y'),
1239 			const_cpu_to_le16('s'), 0 };
1240 
1241 	ntfs_debug("Entering.");
1242 	/*
1243 	 * Find the inode number for the hibernation file by looking up the
1244 	 * filename hiberfil.sys in the root directory.
1245 	 */
1246 	mutex_lock(&vol->root_ino->i_mutex);
1247 	mref = ntfs_lookup_inode_by_name(NTFS_I(vol->root_ino), hiberfil, 12,
1248 			&name);
1249 	mutex_unlock(&vol->root_ino->i_mutex);
1250 	if (IS_ERR_MREF(mref)) {
1251 		ret = MREF_ERR(mref);
1252 		/* If the file does not exist, Windows is not hibernated. */
1253 		if (ret == -ENOENT) {
1254 			ntfs_debug("hiberfil.sys not present.  Windows is not "
1255 					"hibernated on the volume.");
1256 			return 0;
1257 		}
1258 		/* A real error occured. */
1259 		ntfs_error(vol->sb, "Failed to find inode number for "
1260 				"hiberfil.sys.");
1261 		return ret;
1262 	}
1263 	/* We do not care for the type of match that was found. */
1264 	kfree(name);
1265 	/* Get the inode. */
1266 	vi = ntfs_iget(vol->sb, MREF(mref));
1267 	if (IS_ERR(vi) || is_bad_inode(vi)) {
1268 		if (!IS_ERR(vi))
1269 			iput(vi);
1270 		ntfs_error(vol->sb, "Failed to load hiberfil.sys.");
1271 		return IS_ERR(vi) ? PTR_ERR(vi) : -EIO;
1272 	}
1273 	if (unlikely(i_size_read(vi) < NTFS_HIBERFIL_HEADER_SIZE)) {
1274 		ntfs_debug("hiberfil.sys is smaller than 4kiB (0x%llx).  "
1275 				"Windows is hibernated on the volume.  This "
1276 				"is not the system volume.", i_size_read(vi));
1277 		goto iput_out;
1278 	}
1279 	ni = NTFS_I(vi);
1280 	page = ntfs_map_page(vi->i_mapping, 0);
1281 	if (IS_ERR(page)) {
1282 		ntfs_error(vol->sb, "Failed to read from hiberfil.sys.");
1283 		ret = PTR_ERR(page);
1284 		goto iput_out;
1285 	}
1286 	kaddr = (u32*)page_address(page);
1287 	if (*(le32*)kaddr == const_cpu_to_le32(0x72626968)/*'hibr'*/) {
1288 		ntfs_debug("Magic \"hibr\" found in hiberfil.sys.  Windows is "
1289 				"hibernated on the volume.  This is the "
1290 				"system volume.");
1291 		goto unm_iput_out;
1292 	}
1293 	kend = kaddr + NTFS_HIBERFIL_HEADER_SIZE/sizeof(*kaddr);
1294 	do {
1295 		if (unlikely(*kaddr)) {
1296 			ntfs_debug("hiberfil.sys is larger than 4kiB "
1297 					"(0x%llx), does not contain the "
1298 					"\"hibr\" magic, and does not have a "
1299 					"zero header.  Windows is hibernated "
1300 					"on the volume.  This is not the "
1301 					"system volume.", i_size_read(vi));
1302 			goto unm_iput_out;
1303 		}
1304 	} while (++kaddr < kend);
1305 	ntfs_debug("hiberfil.sys contains a zero header.  Windows is not "
1306 			"hibernated on the volume.  This is the system "
1307 			"volume.");
1308 	ret = 0;
1309 unm_iput_out:
1310 	ntfs_unmap_page(page);
1311 iput_out:
1312 	iput(vi);
1313 	return ret;
1314 }
1315 
1316 /**
1317  * load_and_init_quota - load and setup the quota file for a volume if present
1318  * @vol:	ntfs super block describing device whose quota file to load
1319  *
1320  * Return TRUE on success or FALSE on error.  If $Quota is not present, we
1321  * leave vol->quota_ino as NULL and return success.
1322  */
1323 static BOOL load_and_init_quota(ntfs_volume *vol)
1324 {
1325 	MFT_REF mref;
1326 	struct inode *tmp_ino;
1327 	ntfs_name *name = NULL;
1328 	static const ntfschar Quota[7] = { const_cpu_to_le16('$'),
1329 			const_cpu_to_le16('Q'), const_cpu_to_le16('u'),
1330 			const_cpu_to_le16('o'), const_cpu_to_le16('t'),
1331 			const_cpu_to_le16('a'), 0 };
1332 	static ntfschar Q[3] = { const_cpu_to_le16('$'),
1333 			const_cpu_to_le16('Q'), 0 };
1334 
1335 	ntfs_debug("Entering.");
1336 	/*
1337 	 * Find the inode number for the quota file by looking up the filename
1338 	 * $Quota in the extended system files directory $Extend.
1339 	 */
1340 	mutex_lock(&vol->extend_ino->i_mutex);
1341 	mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), Quota, 6,
1342 			&name);
1343 	mutex_unlock(&vol->extend_ino->i_mutex);
1344 	if (IS_ERR_MREF(mref)) {
1345 		/*
1346 		 * If the file does not exist, quotas are disabled and have
1347 		 * never been enabled on this volume, just return success.
1348 		 */
1349 		if (MREF_ERR(mref) == -ENOENT) {
1350 			ntfs_debug("$Quota not present.  Volume does not have "
1351 					"quotas enabled.");
1352 			/*
1353 			 * No need to try to set quotas out of date if they are
1354 			 * not enabled.
1355 			 */
1356 			NVolSetQuotaOutOfDate(vol);
1357 			return TRUE;
1358 		}
1359 		/* A real error occured. */
1360 		ntfs_error(vol->sb, "Failed to find inode number for $Quota.");
1361 		return FALSE;
1362 	}
1363 	/* We do not care for the type of match that was found. */
1364 	kfree(name);
1365 	/* Get the inode. */
1366 	tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1367 	if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1368 		if (!IS_ERR(tmp_ino))
1369 			iput(tmp_ino);
1370 		ntfs_error(vol->sb, "Failed to load $Quota.");
1371 		return FALSE;
1372 	}
1373 	vol->quota_ino = tmp_ino;
1374 	/* Get the $Q index allocation attribute. */
1375 	tmp_ino = ntfs_index_iget(vol->quota_ino, Q, 2);
1376 	if (IS_ERR(tmp_ino)) {
1377 		ntfs_error(vol->sb, "Failed to load $Quota/$Q index.");
1378 		return FALSE;
1379 	}
1380 	vol->quota_q_ino = tmp_ino;
1381 	ntfs_debug("Done.");
1382 	return TRUE;
1383 }
1384 
1385 /**
1386  * load_and_init_usnjrnl - load and setup the transaction log if present
1387  * @vol:	ntfs super block describing device whose usnjrnl file to load
1388  *
1389  * Return TRUE on success or FALSE on error.
1390  *
1391  * If $UsnJrnl is not present or in the process of being disabled, we set
1392  * NVolUsnJrnlStamped() and return success.
1393  *
1394  * If the $UsnJrnl $DATA/$J attribute has a size equal to the lowest valid usn,
1395  * i.e. transaction logging has only just been enabled or the journal has been
1396  * stamped and nothing has been logged since, we also set NVolUsnJrnlStamped()
1397  * and return success.
1398  */
1399 static BOOL load_and_init_usnjrnl(ntfs_volume *vol)
1400 {
1401 	MFT_REF mref;
1402 	struct inode *tmp_ino;
1403 	ntfs_inode *tmp_ni;
1404 	struct page *page;
1405 	ntfs_name *name = NULL;
1406 	USN_HEADER *uh;
1407 	static const ntfschar UsnJrnl[9] = { const_cpu_to_le16('$'),
1408 			const_cpu_to_le16('U'), const_cpu_to_le16('s'),
1409 			const_cpu_to_le16('n'), const_cpu_to_le16('J'),
1410 			const_cpu_to_le16('r'), const_cpu_to_le16('n'),
1411 			const_cpu_to_le16('l'), 0 };
1412 	static ntfschar Max[5] = { const_cpu_to_le16('$'),
1413 			const_cpu_to_le16('M'), const_cpu_to_le16('a'),
1414 			const_cpu_to_le16('x'), 0 };
1415 	static ntfschar J[3] = { const_cpu_to_le16('$'),
1416 			const_cpu_to_le16('J'), 0 };
1417 
1418 	ntfs_debug("Entering.");
1419 	/*
1420 	 * Find the inode number for the transaction log file by looking up the
1421 	 * filename $UsnJrnl in the extended system files directory $Extend.
1422 	 */
1423 	mutex_lock(&vol->extend_ino->i_mutex);
1424 	mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), UsnJrnl, 8,
1425 			&name);
1426 	mutex_unlock(&vol->extend_ino->i_mutex);
1427 	if (IS_ERR_MREF(mref)) {
1428 		/*
1429 		 * If the file does not exist, transaction logging is disabled,
1430 		 * just return success.
1431 		 */
1432 		if (MREF_ERR(mref) == -ENOENT) {
1433 			ntfs_debug("$UsnJrnl not present.  Volume does not "
1434 					"have transaction logging enabled.");
1435 not_enabled:
1436 			/*
1437 			 * No need to try to stamp the transaction log if
1438 			 * transaction logging is not enabled.
1439 			 */
1440 			NVolSetUsnJrnlStamped(vol);
1441 			return TRUE;
1442 		}
1443 		/* A real error occured. */
1444 		ntfs_error(vol->sb, "Failed to find inode number for "
1445 				"$UsnJrnl.");
1446 		return FALSE;
1447 	}
1448 	/* We do not care for the type of match that was found. */
1449 	kfree(name);
1450 	/* Get the inode. */
1451 	tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1452 	if (unlikely(IS_ERR(tmp_ino) || is_bad_inode(tmp_ino))) {
1453 		if (!IS_ERR(tmp_ino))
1454 			iput(tmp_ino);
1455 		ntfs_error(vol->sb, "Failed to load $UsnJrnl.");
1456 		return FALSE;
1457 	}
1458 	vol->usnjrnl_ino = tmp_ino;
1459 	/*
1460 	 * If the transaction log is in the process of being deleted, we can
1461 	 * ignore it.
1462 	 */
1463 	if (unlikely(vol->vol_flags & VOLUME_DELETE_USN_UNDERWAY)) {
1464 		ntfs_debug("$UsnJrnl in the process of being disabled.  "
1465 				"Volume does not have transaction logging "
1466 				"enabled.");
1467 		goto not_enabled;
1468 	}
1469 	/* Get the $DATA/$Max attribute. */
1470 	tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, Max, 4);
1471 	if (IS_ERR(tmp_ino)) {
1472 		ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$Max "
1473 				"attribute.");
1474 		return FALSE;
1475 	}
1476 	vol->usnjrnl_max_ino = tmp_ino;
1477 	if (unlikely(i_size_read(tmp_ino) < sizeof(USN_HEADER))) {
1478 		ntfs_error(vol->sb, "Found corrupt $UsnJrnl/$DATA/$Max "
1479 				"attribute (size is 0x%llx but should be at "
1480 				"least 0x%zx bytes).", i_size_read(tmp_ino),
1481 				sizeof(USN_HEADER));
1482 		return FALSE;
1483 	}
1484 	/* Get the $DATA/$J attribute. */
1485 	tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, J, 2);
1486 	if (IS_ERR(tmp_ino)) {
1487 		ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$J "
1488 				"attribute.");
1489 		return FALSE;
1490 	}
1491 	vol->usnjrnl_j_ino = tmp_ino;
1492 	/* Verify $J is non-resident and sparse. */
1493 	tmp_ni = NTFS_I(vol->usnjrnl_j_ino);
1494 	if (unlikely(!NInoNonResident(tmp_ni) || !NInoSparse(tmp_ni))) {
1495 		ntfs_error(vol->sb, "$UsnJrnl/$DATA/$J attribute is resident "
1496 				"and/or not sparse.");
1497 		return FALSE;
1498 	}
1499 	/* Read the USN_HEADER from $DATA/$Max. */
1500 	page = ntfs_map_page(vol->usnjrnl_max_ino->i_mapping, 0);
1501 	if (IS_ERR(page)) {
1502 		ntfs_error(vol->sb, "Failed to read from $UsnJrnl/$DATA/$Max "
1503 				"attribute.");
1504 		return FALSE;
1505 	}
1506 	uh = (USN_HEADER*)page_address(page);
1507 	/* Sanity check the $Max. */
1508 	if (unlikely(sle64_to_cpu(uh->allocation_delta) >
1509 			sle64_to_cpu(uh->maximum_size))) {
1510 		ntfs_error(vol->sb, "Allocation delta (0x%llx) exceeds "
1511 				"maximum size (0x%llx).  $UsnJrnl is corrupt.",
1512 				(long long)sle64_to_cpu(uh->allocation_delta),
1513 				(long long)sle64_to_cpu(uh->maximum_size));
1514 		ntfs_unmap_page(page);
1515 		return FALSE;
1516 	}
1517 	/*
1518 	 * If the transaction log has been stamped and nothing has been written
1519 	 * to it since, we do not need to stamp it.
1520 	 */
1521 	if (unlikely(sle64_to_cpu(uh->lowest_valid_usn) >=
1522 			i_size_read(vol->usnjrnl_j_ino))) {
1523 		if (likely(sle64_to_cpu(uh->lowest_valid_usn) ==
1524 				i_size_read(vol->usnjrnl_j_ino))) {
1525 			ntfs_unmap_page(page);
1526 			ntfs_debug("$UsnJrnl is enabled but nothing has been "
1527 					"logged since it was last stamped.  "
1528 					"Treating this as if the volume does "
1529 					"not have transaction logging "
1530 					"enabled.");
1531 			goto not_enabled;
1532 		}
1533 		ntfs_error(vol->sb, "$UsnJrnl has lowest valid usn (0x%llx) "
1534 				"which is out of bounds (0x%llx).  $UsnJrnl "
1535 				"is corrupt.",
1536 				(long long)sle64_to_cpu(uh->lowest_valid_usn),
1537 				i_size_read(vol->usnjrnl_j_ino));
1538 		ntfs_unmap_page(page);
1539 		return FALSE;
1540 	}
1541 	ntfs_unmap_page(page);
1542 	ntfs_debug("Done.");
1543 	return TRUE;
1544 }
1545 
1546 /**
1547  * load_and_init_attrdef - load the attribute definitions table for a volume
1548  * @vol:	ntfs super block describing device whose attrdef to load
1549  *
1550  * Return TRUE on success or FALSE on error.
1551  */
1552 static BOOL load_and_init_attrdef(ntfs_volume *vol)
1553 {
1554 	loff_t i_size;
1555 	struct super_block *sb = vol->sb;
1556 	struct inode *ino;
1557 	struct page *page;
1558 	pgoff_t index, max_index;
1559 	unsigned int size;
1560 
1561 	ntfs_debug("Entering.");
1562 	/* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */
1563 	ino = ntfs_iget(sb, FILE_AttrDef);
1564 	if (IS_ERR(ino) || is_bad_inode(ino)) {
1565 		if (!IS_ERR(ino))
1566 			iput(ino);
1567 		goto failed;
1568 	}
1569 	NInoSetSparseDisabled(NTFS_I(ino));
1570 	/* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
1571 	i_size = i_size_read(ino);
1572 	if (i_size <= 0 || i_size > 0x7fffffff)
1573 		goto iput_failed;
1574 	vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(i_size);
1575 	if (!vol->attrdef)
1576 		goto iput_failed;
1577 	index = 0;
1578 	max_index = i_size >> PAGE_CACHE_SHIFT;
1579 	size = PAGE_CACHE_SIZE;
1580 	while (index < max_index) {
1581 		/* Read the attrdef table and copy it into the linear buffer. */
1582 read_partial_attrdef_page:
1583 		page = ntfs_map_page(ino->i_mapping, index);
1584 		if (IS_ERR(page))
1585 			goto free_iput_failed;
1586 		memcpy((u8*)vol->attrdef + (index++ << PAGE_CACHE_SHIFT),
1587 				page_address(page), size);
1588 		ntfs_unmap_page(page);
1589 	};
1590 	if (size == PAGE_CACHE_SIZE) {
1591 		size = i_size & ~PAGE_CACHE_MASK;
1592 		if (size)
1593 			goto read_partial_attrdef_page;
1594 	}
1595 	vol->attrdef_size = i_size;
1596 	ntfs_debug("Read %llu bytes from $AttrDef.", i_size);
1597 	iput(ino);
1598 	return TRUE;
1599 free_iput_failed:
1600 	ntfs_free(vol->attrdef);
1601 	vol->attrdef = NULL;
1602 iput_failed:
1603 	iput(ino);
1604 failed:
1605 	ntfs_error(sb, "Failed to initialize attribute definition table.");
1606 	return FALSE;
1607 }
1608 
1609 #endif /* NTFS_RW */
1610 
1611 /**
1612  * load_and_init_upcase - load the upcase table for an ntfs volume
1613  * @vol:	ntfs super block describing device whose upcase to load
1614  *
1615  * Return TRUE on success or FALSE on error.
1616  */
1617 static BOOL load_and_init_upcase(ntfs_volume *vol)
1618 {
1619 	loff_t i_size;
1620 	struct super_block *sb = vol->sb;
1621 	struct inode *ino;
1622 	struct page *page;
1623 	pgoff_t index, max_index;
1624 	unsigned int size;
1625 	int i, max;
1626 
1627 	ntfs_debug("Entering.");
1628 	/* Read upcase table and setup vol->upcase and vol->upcase_len. */
1629 	ino = ntfs_iget(sb, FILE_UpCase);
1630 	if (IS_ERR(ino) || is_bad_inode(ino)) {
1631 		if (!IS_ERR(ino))
1632 			iput(ino);
1633 		goto upcase_failed;
1634 	}
1635 	/*
1636 	 * The upcase size must not be above 64k Unicode characters, must not
1637 	 * be zero and must be a multiple of sizeof(ntfschar).
1638 	 */
1639 	i_size = i_size_read(ino);
1640 	if (!i_size || i_size & (sizeof(ntfschar) - 1) ||
1641 			i_size > 64ULL * 1024 * sizeof(ntfschar))
1642 		goto iput_upcase_failed;
1643 	vol->upcase = (ntfschar*)ntfs_malloc_nofs(i_size);
1644 	if (!vol->upcase)
1645 		goto iput_upcase_failed;
1646 	index = 0;
1647 	max_index = i_size >> PAGE_CACHE_SHIFT;
1648 	size = PAGE_CACHE_SIZE;
1649 	while (index < max_index) {
1650 		/* Read the upcase table and copy it into the linear buffer. */
1651 read_partial_upcase_page:
1652 		page = ntfs_map_page(ino->i_mapping, index);
1653 		if (IS_ERR(page))
1654 			goto iput_upcase_failed;
1655 		memcpy((char*)vol->upcase + (index++ << PAGE_CACHE_SHIFT),
1656 				page_address(page), size);
1657 		ntfs_unmap_page(page);
1658 	};
1659 	if (size == PAGE_CACHE_SIZE) {
1660 		size = i_size & ~PAGE_CACHE_MASK;
1661 		if (size)
1662 			goto read_partial_upcase_page;
1663 	}
1664 	vol->upcase_len = i_size >> UCHAR_T_SIZE_BITS;
1665 	ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
1666 			i_size, 64 * 1024 * sizeof(ntfschar));
1667 	iput(ino);
1668 	down(&ntfs_lock);
1669 	if (!default_upcase) {
1670 		ntfs_debug("Using volume specified $UpCase since default is "
1671 				"not present.");
1672 		up(&ntfs_lock);
1673 		return TRUE;
1674 	}
1675 	max = default_upcase_len;
1676 	if (max > vol->upcase_len)
1677 		max = vol->upcase_len;
1678 	for (i = 0; i < max; i++)
1679 		if (vol->upcase[i] != default_upcase[i])
1680 			break;
1681 	if (i == max) {
1682 		ntfs_free(vol->upcase);
1683 		vol->upcase = default_upcase;
1684 		vol->upcase_len = max;
1685 		ntfs_nr_upcase_users++;
1686 		up(&ntfs_lock);
1687 		ntfs_debug("Volume specified $UpCase matches default. Using "
1688 				"default.");
1689 		return TRUE;
1690 	}
1691 	up(&ntfs_lock);
1692 	ntfs_debug("Using volume specified $UpCase since it does not match "
1693 			"the default.");
1694 	return TRUE;
1695 iput_upcase_failed:
1696 	iput(ino);
1697 	ntfs_free(vol->upcase);
1698 	vol->upcase = NULL;
1699 upcase_failed:
1700 	down(&ntfs_lock);
1701 	if (default_upcase) {
1702 		vol->upcase = default_upcase;
1703 		vol->upcase_len = default_upcase_len;
1704 		ntfs_nr_upcase_users++;
1705 		up(&ntfs_lock);
1706 		ntfs_error(sb, "Failed to load $UpCase from the volume. Using "
1707 				"default.");
1708 		return TRUE;
1709 	}
1710 	up(&ntfs_lock);
1711 	ntfs_error(sb, "Failed to initialize upcase table.");
1712 	return FALSE;
1713 }
1714 
1715 /**
1716  * load_system_files - open the system files using normal functions
1717  * @vol:	ntfs super block describing device whose system files to load
1718  *
1719  * Open the system files with normal access functions and complete setting up
1720  * the ntfs super block @vol.
1721  *
1722  * Return TRUE on success or FALSE on error.
1723  */
1724 static BOOL load_system_files(ntfs_volume *vol)
1725 {
1726 	struct super_block *sb = vol->sb;
1727 	MFT_RECORD *m;
1728 	VOLUME_INFORMATION *vi;
1729 	ntfs_attr_search_ctx *ctx;
1730 #ifdef NTFS_RW
1731 	RESTART_PAGE_HEADER *rp;
1732 	int err;
1733 #endif /* NTFS_RW */
1734 
1735 	ntfs_debug("Entering.");
1736 #ifdef NTFS_RW
1737 	/* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */
1738 	if (!load_and_init_mft_mirror(vol) || !check_mft_mirror(vol)) {
1739 		static const char *es1 = "Failed to load $MFTMirr";
1740 		static const char *es2 = "$MFTMirr does not match $MFT";
1741 		static const char *es3 = ".  Run ntfsfix and/or chkdsk.";
1742 
1743 		/* If a read-write mount, convert it to a read-only mount. */
1744 		if (!(sb->s_flags & MS_RDONLY)) {
1745 			if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1746 					ON_ERRORS_CONTINUE))) {
1747 				ntfs_error(sb, "%s and neither on_errors="
1748 						"continue nor on_errors="
1749 						"remount-ro was specified%s",
1750 						!vol->mftmirr_ino ? es1 : es2,
1751 						es3);
1752 				goto iput_mirr_err_out;
1753 			}
1754 			sb->s_flags |= MS_RDONLY;
1755 			ntfs_error(sb, "%s.  Mounting read-only%s",
1756 					!vol->mftmirr_ino ? es1 : es2, es3);
1757 		} else
1758 			ntfs_warning(sb, "%s.  Will not be able to remount "
1759 					"read-write%s",
1760 					!vol->mftmirr_ino ? es1 : es2, es3);
1761 		/* This will prevent a read-write remount. */
1762 		NVolSetErrors(vol);
1763 	}
1764 #endif /* NTFS_RW */
1765 	/* Get mft bitmap attribute inode. */
1766 	vol->mftbmp_ino = ntfs_attr_iget(vol->mft_ino, AT_BITMAP, NULL, 0);
1767 	if (IS_ERR(vol->mftbmp_ino)) {
1768 		ntfs_error(sb, "Failed to load $MFT/$BITMAP attribute.");
1769 		goto iput_mirr_err_out;
1770 	}
1771 	/* Read upcase table and setup @vol->upcase and @vol->upcase_len. */
1772 	if (!load_and_init_upcase(vol))
1773 		goto iput_mftbmp_err_out;
1774 #ifdef NTFS_RW
1775 	/*
1776 	 * Read attribute definitions table and setup @vol->attrdef and
1777 	 * @vol->attrdef_size.
1778 	 */
1779 	if (!load_and_init_attrdef(vol))
1780 		goto iput_upcase_err_out;
1781 #endif /* NTFS_RW */
1782 	/*
1783 	 * Get the cluster allocation bitmap inode and verify the size, no
1784 	 * need for any locking at this stage as we are already running
1785 	 * exclusively as we are mount in progress task.
1786 	 */
1787 	vol->lcnbmp_ino = ntfs_iget(sb, FILE_Bitmap);
1788 	if (IS_ERR(vol->lcnbmp_ino) || is_bad_inode(vol->lcnbmp_ino)) {
1789 		if (!IS_ERR(vol->lcnbmp_ino))
1790 			iput(vol->lcnbmp_ino);
1791 		goto bitmap_failed;
1792 	}
1793 	NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino));
1794 	if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) {
1795 		iput(vol->lcnbmp_ino);
1796 bitmap_failed:
1797 		ntfs_error(sb, "Failed to load $Bitmap.");
1798 		goto iput_attrdef_err_out;
1799 	}
1800 	/*
1801 	 * Get the volume inode and setup our cache of the volume flags and
1802 	 * version.
1803 	 */
1804 	vol->vol_ino = ntfs_iget(sb, FILE_Volume);
1805 	if (IS_ERR(vol->vol_ino) || is_bad_inode(vol->vol_ino)) {
1806 		if (!IS_ERR(vol->vol_ino))
1807 			iput(vol->vol_ino);
1808 volume_failed:
1809 		ntfs_error(sb, "Failed to load $Volume.");
1810 		goto iput_lcnbmp_err_out;
1811 	}
1812 	m = map_mft_record(NTFS_I(vol->vol_ino));
1813 	if (IS_ERR(m)) {
1814 iput_volume_failed:
1815 		iput(vol->vol_ino);
1816 		goto volume_failed;
1817 	}
1818 	if (!(ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m))) {
1819 		ntfs_error(sb, "Failed to get attribute search context.");
1820 		goto get_ctx_vol_failed;
1821 	}
1822 	if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
1823 			ctx) || ctx->attr->non_resident || ctx->attr->flags) {
1824 err_put_vol:
1825 		ntfs_attr_put_search_ctx(ctx);
1826 get_ctx_vol_failed:
1827 		unmap_mft_record(NTFS_I(vol->vol_ino));
1828 		goto iput_volume_failed;
1829 	}
1830 	vi = (VOLUME_INFORMATION*)((char*)ctx->attr +
1831 			le16_to_cpu(ctx->attr->data.resident.value_offset));
1832 	/* Some bounds checks. */
1833 	if ((u8*)vi < (u8*)ctx->attr || (u8*)vi +
1834 			le32_to_cpu(ctx->attr->data.resident.value_length) >
1835 			(u8*)ctx->attr + le32_to_cpu(ctx->attr->length))
1836 		goto err_put_vol;
1837 	/* Copy the volume flags and version to the ntfs_volume structure. */
1838 	vol->vol_flags = vi->flags;
1839 	vol->major_ver = vi->major_ver;
1840 	vol->minor_ver = vi->minor_ver;
1841 	ntfs_attr_put_search_ctx(ctx);
1842 	unmap_mft_record(NTFS_I(vol->vol_ino));
1843 	printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver,
1844 			vol->minor_ver);
1845 	if (vol->major_ver < 3 && NVolSparseEnabled(vol)) {
1846 		ntfs_warning(vol->sb, "Disabling sparse support due to NTFS "
1847 				"volume version %i.%i (need at least version "
1848 				"3.0).", vol->major_ver, vol->minor_ver);
1849 		NVolClearSparseEnabled(vol);
1850 	}
1851 #ifdef NTFS_RW
1852 	/* Make sure that no unsupported volume flags are set. */
1853 	if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
1854 		static const char *es1a = "Volume is dirty";
1855 		static const char *es1b = "Volume has been modified by chkdsk";
1856 		static const char *es1c = "Volume has unsupported flags set";
1857 		static const char *es2a = ".  Run chkdsk and mount in Windows.";
1858 		static const char *es2b = ".  Mount in Windows.";
1859 		const char *es1, *es2;
1860 
1861 		es2 = es2a;
1862 		if (vol->vol_flags & VOLUME_IS_DIRTY)
1863 			es1 = es1a;
1864 		else if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
1865 			es1 = es1b;
1866 			es2 = es2b;
1867 		} else {
1868 			es1 = es1c;
1869 			ntfs_warning(sb, "Unsupported volume flags 0x%x "
1870 					"encountered.",
1871 					(unsigned)le16_to_cpu(vol->vol_flags));
1872 		}
1873 		/* If a read-write mount, convert it to a read-only mount. */
1874 		if (!(sb->s_flags & MS_RDONLY)) {
1875 			if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1876 					ON_ERRORS_CONTINUE))) {
1877 				ntfs_error(sb, "%s and neither on_errors="
1878 						"continue nor on_errors="
1879 						"remount-ro was specified%s",
1880 						es1, es2);
1881 				goto iput_vol_err_out;
1882 			}
1883 			sb->s_flags |= MS_RDONLY;
1884 			ntfs_error(sb, "%s.  Mounting read-only%s", es1, es2);
1885 		} else
1886 			ntfs_warning(sb, "%s.  Will not be able to remount "
1887 					"read-write%s", es1, es2);
1888 		/*
1889 		 * Do not set NVolErrors() because ntfs_remount() re-checks the
1890 		 * flags which we need to do in case any flags have changed.
1891 		 */
1892 	}
1893 	/*
1894 	 * Get the inode for the logfile, check it and determine if the volume
1895 	 * was shutdown cleanly.
1896 	 */
1897 	rp = NULL;
1898 	if (!load_and_check_logfile(vol, &rp) ||
1899 			!ntfs_is_logfile_clean(vol->logfile_ino, rp)) {
1900 		static const char *es1a = "Failed to load $LogFile";
1901 		static const char *es1b = "$LogFile is not clean";
1902 		static const char *es2 = ".  Mount in Windows.";
1903 		const char *es1;
1904 
1905 		es1 = !vol->logfile_ino ? es1a : es1b;
1906 		/* If a read-write mount, convert it to a read-only mount. */
1907 		if (!(sb->s_flags & MS_RDONLY)) {
1908 			if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1909 					ON_ERRORS_CONTINUE))) {
1910 				ntfs_error(sb, "%s and neither on_errors="
1911 						"continue nor on_errors="
1912 						"remount-ro was specified%s",
1913 						es1, es2);
1914 				if (vol->logfile_ino) {
1915 					BUG_ON(!rp);
1916 					ntfs_free(rp);
1917 				}
1918 				goto iput_logfile_err_out;
1919 			}
1920 			sb->s_flags |= MS_RDONLY;
1921 			ntfs_error(sb, "%s.  Mounting read-only%s", es1, es2);
1922 		} else
1923 			ntfs_warning(sb, "%s.  Will not be able to remount "
1924 					"read-write%s", es1, es2);
1925 		/* This will prevent a read-write remount. */
1926 		NVolSetErrors(vol);
1927 	}
1928 	ntfs_free(rp);
1929 #endif /* NTFS_RW */
1930 	/* Get the root directory inode so we can do path lookups. */
1931 	vol->root_ino = ntfs_iget(sb, FILE_root);
1932 	if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) {
1933 		if (!IS_ERR(vol->root_ino))
1934 			iput(vol->root_ino);
1935 		ntfs_error(sb, "Failed to load root directory.");
1936 		goto iput_logfile_err_out;
1937 	}
1938 #ifdef NTFS_RW
1939 	/*
1940 	 * Check if Windows is suspended to disk on the target volume.  If it
1941 	 * is hibernated, we must not write *anything* to the disk so set
1942 	 * NVolErrors() without setting the dirty volume flag and mount
1943 	 * read-only.  This will prevent read-write remounting and it will also
1944 	 * prevent all writes.
1945 	 */
1946 	err = check_windows_hibernation_status(vol);
1947 	if (unlikely(err)) {
1948 		static const char *es1a = "Failed to determine if Windows is "
1949 				"hibernated";
1950 		static const char *es1b = "Windows is hibernated";
1951 		static const char *es2 = ".  Run chkdsk.";
1952 		const char *es1;
1953 
1954 		es1 = err < 0 ? es1a : es1b;
1955 		/* If a read-write mount, convert it to a read-only mount. */
1956 		if (!(sb->s_flags & MS_RDONLY)) {
1957 			if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1958 					ON_ERRORS_CONTINUE))) {
1959 				ntfs_error(sb, "%s and neither on_errors="
1960 						"continue nor on_errors="
1961 						"remount-ro was specified%s",
1962 						es1, es2);
1963 				goto iput_root_err_out;
1964 			}
1965 			sb->s_flags |= MS_RDONLY;
1966 			ntfs_error(sb, "%s.  Mounting read-only%s", es1, es2);
1967 		} else
1968 			ntfs_warning(sb, "%s.  Will not be able to remount "
1969 					"read-write%s", es1, es2);
1970 		/* This will prevent a read-write remount. */
1971 		NVolSetErrors(vol);
1972 	}
1973 	/* If (still) a read-write mount, mark the volume dirty. */
1974 	if (!(sb->s_flags & MS_RDONLY) &&
1975 			ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
1976 		static const char *es1 = "Failed to set dirty bit in volume "
1977 				"information flags";
1978 		static const char *es2 = ".  Run chkdsk.";
1979 
1980 		/* Convert to a read-only mount. */
1981 		if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1982 				ON_ERRORS_CONTINUE))) {
1983 			ntfs_error(sb, "%s and neither on_errors=continue nor "
1984 					"on_errors=remount-ro was specified%s",
1985 					es1, es2);
1986 			goto iput_root_err_out;
1987 		}
1988 		ntfs_error(sb, "%s.  Mounting read-only%s", es1, es2);
1989 		sb->s_flags |= MS_RDONLY;
1990 		/*
1991 		 * Do not set NVolErrors() because ntfs_remount() might manage
1992 		 * to set the dirty flag in which case all would be well.
1993 		 */
1994 	}
1995 #if 0
1996 	// TODO: Enable this code once we start modifying anything that is
1997 	//	 different between NTFS 1.2 and 3.x...
1998 	/*
1999 	 * If (still) a read-write mount, set the NT4 compatibility flag on
2000 	 * newer NTFS version volumes.
2001 	 */
2002 	if (!(sb->s_flags & MS_RDONLY) && (vol->major_ver > 1) &&
2003 			ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
2004 		static const char *es1 = "Failed to set NT4 compatibility flag";
2005 		static const char *es2 = ".  Run chkdsk.";
2006 
2007 		/* Convert to a read-only mount. */
2008 		if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2009 				ON_ERRORS_CONTINUE))) {
2010 			ntfs_error(sb, "%s and neither on_errors=continue nor "
2011 					"on_errors=remount-ro was specified%s",
2012 					es1, es2);
2013 			goto iput_root_err_out;
2014 		}
2015 		ntfs_error(sb, "%s.  Mounting read-only%s", es1, es2);
2016 		sb->s_flags |= MS_RDONLY;
2017 		NVolSetErrors(vol);
2018 	}
2019 #endif
2020 	/* If (still) a read-write mount, empty the logfile. */
2021 	if (!(sb->s_flags & MS_RDONLY) &&
2022 			!ntfs_empty_logfile(vol->logfile_ino)) {
2023 		static const char *es1 = "Failed to empty $LogFile";
2024 		static const char *es2 = ".  Mount in Windows.";
2025 
2026 		/* Convert to a read-only mount. */
2027 		if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2028 				ON_ERRORS_CONTINUE))) {
2029 			ntfs_error(sb, "%s and neither on_errors=continue nor "
2030 					"on_errors=remount-ro was specified%s",
2031 					es1, es2);
2032 			goto iput_root_err_out;
2033 		}
2034 		ntfs_error(sb, "%s.  Mounting read-only%s", es1, es2);
2035 		sb->s_flags |= MS_RDONLY;
2036 		NVolSetErrors(vol);
2037 	}
2038 #endif /* NTFS_RW */
2039 	/* If on NTFS versions before 3.0, we are done. */
2040 	if (unlikely(vol->major_ver < 3))
2041 		return TRUE;
2042 	/* NTFS 3.0+ specific initialization. */
2043 	/* Get the security descriptors inode. */
2044 	vol->secure_ino = ntfs_iget(sb, FILE_Secure);
2045 	if (IS_ERR(vol->secure_ino) || is_bad_inode(vol->secure_ino)) {
2046 		if (!IS_ERR(vol->secure_ino))
2047 			iput(vol->secure_ino);
2048 		ntfs_error(sb, "Failed to load $Secure.");
2049 		goto iput_root_err_out;
2050 	}
2051 	// TODO: Initialize security.
2052 	/* Get the extended system files' directory inode. */
2053 	vol->extend_ino = ntfs_iget(sb, FILE_Extend);
2054 	if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) {
2055 		if (!IS_ERR(vol->extend_ino))
2056 			iput(vol->extend_ino);
2057 		ntfs_error(sb, "Failed to load $Extend.");
2058 		goto iput_sec_err_out;
2059 	}
2060 #ifdef NTFS_RW
2061 	/* Find the quota file, load it if present, and set it up. */
2062 	if (!load_and_init_quota(vol)) {
2063 		static const char *es1 = "Failed to load $Quota";
2064 		static const char *es2 = ".  Run chkdsk.";
2065 
2066 		/* If a read-write mount, convert it to a read-only mount. */
2067 		if (!(sb->s_flags & MS_RDONLY)) {
2068 			if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2069 					ON_ERRORS_CONTINUE))) {
2070 				ntfs_error(sb, "%s and neither on_errors="
2071 						"continue nor on_errors="
2072 						"remount-ro was specified%s",
2073 						es1, es2);
2074 				goto iput_quota_err_out;
2075 			}
2076 			sb->s_flags |= MS_RDONLY;
2077 			ntfs_error(sb, "%s.  Mounting read-only%s", es1, es2);
2078 		} else
2079 			ntfs_warning(sb, "%s.  Will not be able to remount "
2080 					"read-write%s", es1, es2);
2081 		/* This will prevent a read-write remount. */
2082 		NVolSetErrors(vol);
2083 	}
2084 	/* If (still) a read-write mount, mark the quotas out of date. */
2085 	if (!(sb->s_flags & MS_RDONLY) &&
2086 			!ntfs_mark_quotas_out_of_date(vol)) {
2087 		static const char *es1 = "Failed to mark quotas out of date";
2088 		static const char *es2 = ".  Run chkdsk.";
2089 
2090 		/* Convert to a read-only mount. */
2091 		if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2092 				ON_ERRORS_CONTINUE))) {
2093 			ntfs_error(sb, "%s and neither on_errors=continue nor "
2094 					"on_errors=remount-ro was specified%s",
2095 					es1, es2);
2096 			goto iput_quota_err_out;
2097 		}
2098 		ntfs_error(sb, "%s.  Mounting read-only%s", es1, es2);
2099 		sb->s_flags |= MS_RDONLY;
2100 		NVolSetErrors(vol);
2101 	}
2102 	/*
2103 	 * Find the transaction log file ($UsnJrnl), load it if present, check
2104 	 * it, and set it up.
2105 	 */
2106 	if (!load_and_init_usnjrnl(vol)) {
2107 		static const char *es1 = "Failed to load $UsnJrnl";
2108 		static const char *es2 = ".  Run chkdsk.";
2109 
2110 		/* If a read-write mount, convert it to a read-only mount. */
2111 		if (!(sb->s_flags & MS_RDONLY)) {
2112 			if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2113 					ON_ERRORS_CONTINUE))) {
2114 				ntfs_error(sb, "%s and neither on_errors="
2115 						"continue nor on_errors="
2116 						"remount-ro was specified%s",
2117 						es1, es2);
2118 				goto iput_usnjrnl_err_out;
2119 			}
2120 			sb->s_flags |= MS_RDONLY;
2121 			ntfs_error(sb, "%s.  Mounting read-only%s", es1, es2);
2122 		} else
2123 			ntfs_warning(sb, "%s.  Will not be able to remount "
2124 					"read-write%s", es1, es2);
2125 		/* This will prevent a read-write remount. */
2126 		NVolSetErrors(vol);
2127 	}
2128 	/* If (still) a read-write mount, stamp the transaction log. */
2129 	if (!(sb->s_flags & MS_RDONLY) && !ntfs_stamp_usnjrnl(vol)) {
2130 		static const char *es1 = "Failed to stamp transaction log "
2131 				"($UsnJrnl)";
2132 		static const char *es2 = ".  Run chkdsk.";
2133 
2134 		/* Convert to a read-only mount. */
2135 		if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2136 				ON_ERRORS_CONTINUE))) {
2137 			ntfs_error(sb, "%s and neither on_errors=continue nor "
2138 					"on_errors=remount-ro was specified%s",
2139 					es1, es2);
2140 			goto iput_usnjrnl_err_out;
2141 		}
2142 		ntfs_error(sb, "%s.  Mounting read-only%s", es1, es2);
2143 		sb->s_flags |= MS_RDONLY;
2144 		NVolSetErrors(vol);
2145 	}
2146 #endif /* NTFS_RW */
2147 	return TRUE;
2148 #ifdef NTFS_RW
2149 iput_usnjrnl_err_out:
2150 	if (vol->usnjrnl_j_ino)
2151 		iput(vol->usnjrnl_j_ino);
2152 	if (vol->usnjrnl_max_ino)
2153 		iput(vol->usnjrnl_max_ino);
2154 	if (vol->usnjrnl_ino)
2155 		iput(vol->usnjrnl_ino);
2156 iput_quota_err_out:
2157 	if (vol->quota_q_ino)
2158 		iput(vol->quota_q_ino);
2159 	if (vol->quota_ino)
2160 		iput(vol->quota_ino);
2161 	iput(vol->extend_ino);
2162 #endif /* NTFS_RW */
2163 iput_sec_err_out:
2164 	iput(vol->secure_ino);
2165 iput_root_err_out:
2166 	iput(vol->root_ino);
2167 iput_logfile_err_out:
2168 #ifdef NTFS_RW
2169 	if (vol->logfile_ino)
2170 		iput(vol->logfile_ino);
2171 iput_vol_err_out:
2172 #endif /* NTFS_RW */
2173 	iput(vol->vol_ino);
2174 iput_lcnbmp_err_out:
2175 	iput(vol->lcnbmp_ino);
2176 iput_attrdef_err_out:
2177 	vol->attrdef_size = 0;
2178 	if (vol->attrdef) {
2179 		ntfs_free(vol->attrdef);
2180 		vol->attrdef = NULL;
2181 	}
2182 #ifdef NTFS_RW
2183 iput_upcase_err_out:
2184 #endif /* NTFS_RW */
2185 	vol->upcase_len = 0;
2186 	down(&ntfs_lock);
2187 	if (vol->upcase == default_upcase) {
2188 		ntfs_nr_upcase_users--;
2189 		vol->upcase = NULL;
2190 	}
2191 	up(&ntfs_lock);
2192 	if (vol->upcase) {
2193 		ntfs_free(vol->upcase);
2194 		vol->upcase = NULL;
2195 	}
2196 iput_mftbmp_err_out:
2197 	iput(vol->mftbmp_ino);
2198 iput_mirr_err_out:
2199 #ifdef NTFS_RW
2200 	if (vol->mftmirr_ino)
2201 		iput(vol->mftmirr_ino);
2202 #endif /* NTFS_RW */
2203 	return FALSE;
2204 }
2205 
2206 /**
2207  * ntfs_put_super - called by the vfs to unmount a volume
2208  * @sb:		vfs superblock of volume to unmount
2209  *
2210  * ntfs_put_super() is called by the VFS (from fs/super.c::do_umount()) when
2211  * the volume is being unmounted (umount system call has been invoked) and it
2212  * releases all inodes and memory belonging to the NTFS specific part of the
2213  * super block.
2214  */
2215 static void ntfs_put_super(struct super_block *sb)
2216 {
2217 	ntfs_volume *vol = NTFS_SB(sb);
2218 
2219 	ntfs_debug("Entering.");
2220 #ifdef NTFS_RW
2221 	/*
2222 	 * Commit all inodes while they are still open in case some of them
2223 	 * cause others to be dirtied.
2224 	 */
2225 	ntfs_commit_inode(vol->vol_ino);
2226 
2227 	/* NTFS 3.0+ specific. */
2228 	if (vol->major_ver >= 3) {
2229 		if (vol->usnjrnl_j_ino)
2230 			ntfs_commit_inode(vol->usnjrnl_j_ino);
2231 		if (vol->usnjrnl_max_ino)
2232 			ntfs_commit_inode(vol->usnjrnl_max_ino);
2233 		if (vol->usnjrnl_ino)
2234 			ntfs_commit_inode(vol->usnjrnl_ino);
2235 		if (vol->quota_q_ino)
2236 			ntfs_commit_inode(vol->quota_q_ino);
2237 		if (vol->quota_ino)
2238 			ntfs_commit_inode(vol->quota_ino);
2239 		if (vol->extend_ino)
2240 			ntfs_commit_inode(vol->extend_ino);
2241 		if (vol->secure_ino)
2242 			ntfs_commit_inode(vol->secure_ino);
2243 	}
2244 
2245 	ntfs_commit_inode(vol->root_ino);
2246 
2247 	down_write(&vol->lcnbmp_lock);
2248 	ntfs_commit_inode(vol->lcnbmp_ino);
2249 	up_write(&vol->lcnbmp_lock);
2250 
2251 	down_write(&vol->mftbmp_lock);
2252 	ntfs_commit_inode(vol->mftbmp_ino);
2253 	up_write(&vol->mftbmp_lock);
2254 
2255 	if (vol->logfile_ino)
2256 		ntfs_commit_inode(vol->logfile_ino);
2257 
2258 	if (vol->mftmirr_ino)
2259 		ntfs_commit_inode(vol->mftmirr_ino);
2260 	ntfs_commit_inode(vol->mft_ino);
2261 
2262 	/*
2263 	 * If a read-write mount and no volume errors have occured, mark the
2264 	 * volume clean.  Also, re-commit all affected inodes.
2265 	 */
2266 	if (!(sb->s_flags & MS_RDONLY)) {
2267 		if (!NVolErrors(vol)) {
2268 			if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
2269 				ntfs_warning(sb, "Failed to clear dirty bit "
2270 						"in volume information "
2271 						"flags.  Run chkdsk.");
2272 			ntfs_commit_inode(vol->vol_ino);
2273 			ntfs_commit_inode(vol->root_ino);
2274 			if (vol->mftmirr_ino)
2275 				ntfs_commit_inode(vol->mftmirr_ino);
2276 			ntfs_commit_inode(vol->mft_ino);
2277 		} else {
2278 			ntfs_warning(sb, "Volume has errors.  Leaving volume "
2279 					"marked dirty.  Run chkdsk.");
2280 		}
2281 	}
2282 #endif /* NTFS_RW */
2283 
2284 	iput(vol->vol_ino);
2285 	vol->vol_ino = NULL;
2286 
2287 	/* NTFS 3.0+ specific clean up. */
2288 	if (vol->major_ver >= 3) {
2289 #ifdef NTFS_RW
2290 		if (vol->usnjrnl_j_ino) {
2291 			iput(vol->usnjrnl_j_ino);
2292 			vol->usnjrnl_j_ino = NULL;
2293 		}
2294 		if (vol->usnjrnl_max_ino) {
2295 			iput(vol->usnjrnl_max_ino);
2296 			vol->usnjrnl_max_ino = NULL;
2297 		}
2298 		if (vol->usnjrnl_ino) {
2299 			iput(vol->usnjrnl_ino);
2300 			vol->usnjrnl_ino = NULL;
2301 		}
2302 		if (vol->quota_q_ino) {
2303 			iput(vol->quota_q_ino);
2304 			vol->quota_q_ino = NULL;
2305 		}
2306 		if (vol->quota_ino) {
2307 			iput(vol->quota_ino);
2308 			vol->quota_ino = NULL;
2309 		}
2310 #endif /* NTFS_RW */
2311 		if (vol->extend_ino) {
2312 			iput(vol->extend_ino);
2313 			vol->extend_ino = NULL;
2314 		}
2315 		if (vol->secure_ino) {
2316 			iput(vol->secure_ino);
2317 			vol->secure_ino = NULL;
2318 		}
2319 	}
2320 
2321 	iput(vol->root_ino);
2322 	vol->root_ino = NULL;
2323 
2324 	down_write(&vol->lcnbmp_lock);
2325 	iput(vol->lcnbmp_ino);
2326 	vol->lcnbmp_ino = NULL;
2327 	up_write(&vol->lcnbmp_lock);
2328 
2329 	down_write(&vol->mftbmp_lock);
2330 	iput(vol->mftbmp_ino);
2331 	vol->mftbmp_ino = NULL;
2332 	up_write(&vol->mftbmp_lock);
2333 
2334 #ifdef NTFS_RW
2335 	if (vol->logfile_ino) {
2336 		iput(vol->logfile_ino);
2337 		vol->logfile_ino = NULL;
2338 	}
2339 	if (vol->mftmirr_ino) {
2340 		/* Re-commit the mft mirror and mft just in case. */
2341 		ntfs_commit_inode(vol->mftmirr_ino);
2342 		ntfs_commit_inode(vol->mft_ino);
2343 		iput(vol->mftmirr_ino);
2344 		vol->mftmirr_ino = NULL;
2345 	}
2346 	/*
2347 	 * If any dirty inodes are left, throw away all mft data page cache
2348 	 * pages to allow a clean umount.  This should never happen any more
2349 	 * due to mft.c::ntfs_mft_writepage() cleaning all the dirty pages as
2350 	 * the underlying mft records are written out and cleaned.  If it does,
2351 	 * happen anyway, we want to know...
2352 	 */
2353 	ntfs_commit_inode(vol->mft_ino);
2354 	write_inode_now(vol->mft_ino, 1);
2355 	if (!list_empty(&sb->s_dirty)) {
2356 		const char *s1, *s2;
2357 
2358 		mutex_lock(&vol->mft_ino->i_mutex);
2359 		truncate_inode_pages(vol->mft_ino->i_mapping, 0);
2360 		mutex_unlock(&vol->mft_ino->i_mutex);
2361 		write_inode_now(vol->mft_ino, 1);
2362 		if (!list_empty(&sb->s_dirty)) {
2363 			static const char *_s1 = "inodes";
2364 			static const char *_s2 = "";
2365 			s1 = _s1;
2366 			s2 = _s2;
2367 		} else {
2368 			static const char *_s1 = "mft pages";
2369 			static const char *_s2 = "They have been thrown "
2370 					"away.  ";
2371 			s1 = _s1;
2372 			s2 = _s2;
2373 		}
2374 		ntfs_error(sb, "Dirty %s found at umount time.  %sYou should "
2375 				"run chkdsk.  Please email "
2376 				"linux-ntfs-dev@lists.sourceforge.net and say "
2377 				"that you saw this message.  Thank you.", s1,
2378 				s2);
2379 	}
2380 #endif /* NTFS_RW */
2381 
2382 	iput(vol->mft_ino);
2383 	vol->mft_ino = NULL;
2384 
2385 	/* Throw away the table of attribute definitions. */
2386 	vol->attrdef_size = 0;
2387 	if (vol->attrdef) {
2388 		ntfs_free(vol->attrdef);
2389 		vol->attrdef = NULL;
2390 	}
2391 	vol->upcase_len = 0;
2392 	/*
2393 	 * Destroy the global default upcase table if necessary.  Also decrease
2394 	 * the number of upcase users if we are a user.
2395 	 */
2396 	down(&ntfs_lock);
2397 	if (vol->upcase == default_upcase) {
2398 		ntfs_nr_upcase_users--;
2399 		vol->upcase = NULL;
2400 	}
2401 	if (!ntfs_nr_upcase_users && default_upcase) {
2402 		ntfs_free(default_upcase);
2403 		default_upcase = NULL;
2404 	}
2405 	if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
2406 		free_compression_buffers();
2407 	up(&ntfs_lock);
2408 	if (vol->upcase) {
2409 		ntfs_free(vol->upcase);
2410 		vol->upcase = NULL;
2411 	}
2412 	if (vol->nls_map) {
2413 		unload_nls(vol->nls_map);
2414 		vol->nls_map = NULL;
2415 	}
2416 	sb->s_fs_info = NULL;
2417 	kfree(vol);
2418 	return;
2419 }
2420 
2421 /**
2422  * get_nr_free_clusters - return the number of free clusters on a volume
2423  * @vol:	ntfs volume for which to obtain free cluster count
2424  *
2425  * Calculate the number of free clusters on the mounted NTFS volume @vol. We
2426  * actually calculate the number of clusters in use instead because this
2427  * allows us to not care about partial pages as these will be just zero filled
2428  * and hence not be counted as allocated clusters.
2429  *
2430  * The only particularity is that clusters beyond the end of the logical ntfs
2431  * volume will be marked as allocated to prevent errors which means we have to
2432  * discount those at the end. This is important as the cluster bitmap always
2433  * has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside
2434  * the logical volume and marked in use when they are not as they do not exist.
2435  *
2436  * If any pages cannot be read we assume all clusters in the erroring pages are
2437  * in use. This means we return an underestimate on errors which is better than
2438  * an overestimate.
2439  */
2440 static s64 get_nr_free_clusters(ntfs_volume *vol)
2441 {
2442 	s64 nr_free = vol->nr_clusters;
2443 	u32 *kaddr;
2444 	struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
2445 	filler_t *readpage = (filler_t*)mapping->a_ops->readpage;
2446 	struct page *page;
2447 	pgoff_t index, max_index;
2448 
2449 	ntfs_debug("Entering.");
2450 	/* Serialize accesses to the cluster bitmap. */
2451 	down_read(&vol->lcnbmp_lock);
2452 	/*
2453 	 * Convert the number of bits into bytes rounded up, then convert into
2454 	 * multiples of PAGE_CACHE_SIZE, rounding up so that if we have one
2455 	 * full and one partial page max_index = 2.
2456 	 */
2457 	max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >>
2458 			PAGE_CACHE_SHIFT;
2459 	/* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2460 	ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.",
2461 			max_index, PAGE_CACHE_SIZE / 4);
2462 	for (index = 0; index < max_index; index++) {
2463 		unsigned int i;
2464 		/*
2465 		 * Read the page from page cache, getting it from backing store
2466 		 * if necessary, and increment the use count.
2467 		 */
2468 		page = read_cache_page(mapping, index, (filler_t*)readpage,
2469 				NULL);
2470 		/* Ignore pages which errored synchronously. */
2471 		if (IS_ERR(page)) {
2472 			ntfs_debug("Sync read_cache_page() error. Skipping "
2473 					"page (index 0x%lx).", index);
2474 			nr_free -= PAGE_CACHE_SIZE * 8;
2475 			continue;
2476 		}
2477 		wait_on_page_locked(page);
2478 		/* Ignore pages which errored asynchronously. */
2479 		if (!PageUptodate(page)) {
2480 			ntfs_debug("Async read_cache_page() error. Skipping "
2481 					"page (index 0x%lx).", index);
2482 			page_cache_release(page);
2483 			nr_free -= PAGE_CACHE_SIZE * 8;
2484 			continue;
2485 		}
2486 		kaddr = (u32*)kmap_atomic(page, KM_USER0);
2487 		/*
2488 		 * For each 4 bytes, subtract the number of set bits. If this
2489 		 * is the last page and it is partial we don't really care as
2490 		 * it just means we do a little extra work but it won't affect
2491 		 * the result as all out of range bytes are set to zero by
2492 		 * ntfs_readpage().
2493 		 */
2494 	  	for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
2495 			nr_free -= (s64)hweight32(kaddr[i]);
2496 		kunmap_atomic(kaddr, KM_USER0);
2497 		page_cache_release(page);
2498 	}
2499 	ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index - 1);
2500 	/*
2501 	 * Fixup for eventual bits outside logical ntfs volume (see function
2502 	 * description above).
2503 	 */
2504 	if (vol->nr_clusters & 63)
2505 		nr_free += 64 - (vol->nr_clusters & 63);
2506 	up_read(&vol->lcnbmp_lock);
2507 	/* If errors occured we may well have gone below zero, fix this. */
2508 	if (nr_free < 0)
2509 		nr_free = 0;
2510 	ntfs_debug("Exiting.");
2511 	return nr_free;
2512 }
2513 
2514 /**
2515  * __get_nr_free_mft_records - return the number of free inodes on a volume
2516  * @vol:	ntfs volume for which to obtain free inode count
2517  * @nr_free:	number of mft records in filesystem
2518  * @max_index:	maximum number of pages containing set bits
2519  *
2520  * Calculate the number of free mft records (inodes) on the mounted NTFS
2521  * volume @vol. We actually calculate the number of mft records in use instead
2522  * because this allows us to not care about partial pages as these will be just
2523  * zero filled and hence not be counted as allocated mft record.
2524  *
2525  * If any pages cannot be read we assume all mft records in the erroring pages
2526  * are in use. This means we return an underestimate on errors which is better
2527  * than an overestimate.
2528  *
2529  * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
2530  */
2531 static unsigned long __get_nr_free_mft_records(ntfs_volume *vol,
2532 		s64 nr_free, const pgoff_t max_index)
2533 {
2534 	u32 *kaddr;
2535 	struct address_space *mapping = vol->mftbmp_ino->i_mapping;
2536 	filler_t *readpage = (filler_t*)mapping->a_ops->readpage;
2537 	struct page *page;
2538 	pgoff_t index;
2539 
2540 	ntfs_debug("Entering.");
2541 	/* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2542 	ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
2543 			"0x%lx.", max_index, PAGE_CACHE_SIZE / 4);
2544 	for (index = 0; index < max_index; index++) {
2545 		unsigned int i;
2546 		/*
2547 		 * Read the page from page cache, getting it from backing store
2548 		 * if necessary, and increment the use count.
2549 		 */
2550 		page = read_cache_page(mapping, index, (filler_t*)readpage,
2551 				NULL);
2552 		/* Ignore pages which errored synchronously. */
2553 		if (IS_ERR(page)) {
2554 			ntfs_debug("Sync read_cache_page() error. Skipping "
2555 					"page (index 0x%lx).", index);
2556 			nr_free -= PAGE_CACHE_SIZE * 8;
2557 			continue;
2558 		}
2559 		wait_on_page_locked(page);
2560 		/* Ignore pages which errored asynchronously. */
2561 		if (!PageUptodate(page)) {
2562 			ntfs_debug("Async read_cache_page() error. Skipping "
2563 					"page (index 0x%lx).", index);
2564 			page_cache_release(page);
2565 			nr_free -= PAGE_CACHE_SIZE * 8;
2566 			continue;
2567 		}
2568 		kaddr = (u32*)kmap_atomic(page, KM_USER0);
2569 		/*
2570 		 * For each 4 bytes, subtract the number of set bits. If this
2571 		 * is the last page and it is partial we don't really care as
2572 		 * it just means we do a little extra work but it won't affect
2573 		 * the result as all out of range bytes are set to zero by
2574 		 * ntfs_readpage().
2575 		 */
2576 	  	for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
2577 			nr_free -= (s64)hweight32(kaddr[i]);
2578 		kunmap_atomic(kaddr, KM_USER0);
2579 		page_cache_release(page);
2580 	}
2581 	ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.",
2582 			index - 1);
2583 	/* If errors occured we may well have gone below zero, fix this. */
2584 	if (nr_free < 0)
2585 		nr_free = 0;
2586 	ntfs_debug("Exiting.");
2587 	return nr_free;
2588 }
2589 
2590 /**
2591  * ntfs_statfs - return information about mounted NTFS volume
2592  * @sb:		super block of mounted volume
2593  * @sfs:	statfs structure in which to return the information
2594  *
2595  * Return information about the mounted NTFS volume @sb in the statfs structure
2596  * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is
2597  * called). We interpret the values to be correct of the moment in time at
2598  * which we are called. Most values are variable otherwise and this isn't just
2599  * the free values but the totals as well. For example we can increase the
2600  * total number of file nodes if we run out and we can keep doing this until
2601  * there is no more space on the volume left at all.
2602  *
2603  * Called from vfs_statfs which is used to handle the statfs, fstatfs, and
2604  * ustat system calls.
2605  *
2606  * Return 0 on success or -errno on error.
2607  */
2608 static int ntfs_statfs(struct super_block *sb, struct kstatfs *sfs)
2609 {
2610 	s64 size;
2611 	ntfs_volume *vol = NTFS_SB(sb);
2612 	ntfs_inode *mft_ni = NTFS_I(vol->mft_ino);
2613 	pgoff_t max_index;
2614 	unsigned long flags;
2615 
2616 	ntfs_debug("Entering.");
2617 	/* Type of filesystem. */
2618 	sfs->f_type   = NTFS_SB_MAGIC;
2619 	/* Optimal transfer block size. */
2620 	sfs->f_bsize  = PAGE_CACHE_SIZE;
2621 	/*
2622 	 * Total data blocks in filesystem in units of f_bsize and since
2623 	 * inodes are also stored in data blocs ($MFT is a file) this is just
2624 	 * the total clusters.
2625 	 */
2626 	sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >>
2627 				PAGE_CACHE_SHIFT;
2628 	/* Free data blocks in filesystem in units of f_bsize. */
2629 	size	      = get_nr_free_clusters(vol) << vol->cluster_size_bits >>
2630 				PAGE_CACHE_SHIFT;
2631 	if (size < 0LL)
2632 		size = 0LL;
2633 	/* Free blocks avail to non-superuser, same as above on NTFS. */
2634 	sfs->f_bavail = sfs->f_bfree = size;
2635 	/* Serialize accesses to the inode bitmap. */
2636 	down_read(&vol->mftbmp_lock);
2637 	read_lock_irqsave(&mft_ni->size_lock, flags);
2638 	size = i_size_read(vol->mft_ino) >> vol->mft_record_size_bits;
2639 	/*
2640 	 * Convert the maximum number of set bits into bytes rounded up, then
2641 	 * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
2642 	 * have one full and one partial page max_index = 2.
2643 	 */
2644 	max_index = ((((mft_ni->initialized_size >> vol->mft_record_size_bits)
2645 			+ 7) >> 3) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2646 	read_unlock_irqrestore(&mft_ni->size_lock, flags);
2647 	/* Number of inodes in filesystem (at this point in time). */
2648 	sfs->f_files = size;
2649 	/* Free inodes in fs (based on current total count). */
2650 	sfs->f_ffree = __get_nr_free_mft_records(vol, size, max_index);
2651 	up_read(&vol->mftbmp_lock);
2652 	/*
2653 	 * File system id. This is extremely *nix flavour dependent and even
2654 	 * within Linux itself all fs do their own thing. I interpret this to
2655 	 * mean a unique id associated with the mounted fs and not the id
2656 	 * associated with the filesystem driver, the latter is already given
2657 	 * by the filesystem type in sfs->f_type. Thus we use the 64-bit
2658 	 * volume serial number splitting it into two 32-bit parts. We enter
2659 	 * the least significant 32-bits in f_fsid[0] and the most significant
2660 	 * 32-bits in f_fsid[1].
2661 	 */
2662 	sfs->f_fsid.val[0] = vol->serial_no & 0xffffffff;
2663 	sfs->f_fsid.val[1] = (vol->serial_no >> 32) & 0xffffffff;
2664 	/* Maximum length of filenames. */
2665 	sfs->f_namelen	   = NTFS_MAX_NAME_LEN;
2666 	return 0;
2667 }
2668 
2669 /**
2670  * The complete super operations.
2671  */
2672 static struct super_operations ntfs_sops = {
2673 	.alloc_inode	= ntfs_alloc_big_inode,	  /* VFS: Allocate new inode. */
2674 	.destroy_inode	= ntfs_destroy_big_inode, /* VFS: Deallocate inode. */
2675 	.put_inode	= ntfs_put_inode,	  /* VFS: Called just before
2676 						     the inode reference count
2677 						     is decreased. */
2678 #ifdef NTFS_RW
2679 	//.dirty_inode	= NULL,			/* VFS: Called from
2680 	//					   __mark_inode_dirty(). */
2681 	.write_inode	= ntfs_write_inode,	/* VFS: Write dirty inode to
2682 						   disk. */
2683 	//.drop_inode	= NULL,			/* VFS: Called just after the
2684 	//					   inode reference count has
2685 	//					   been decreased to zero.
2686 	//					   NOTE: The inode lock is
2687 	//					   held. See fs/inode.c::
2688 	//					   generic_drop_inode(). */
2689 	//.delete_inode	= NULL,			/* VFS: Delete inode from disk.
2690 	//					   Called when i_count becomes
2691 	//					   0 and i_nlink is also 0. */
2692 	//.write_super	= NULL,			/* Flush dirty super block to
2693 	//					   disk. */
2694 	//.sync_fs	= NULL,			/* ? */
2695 	//.write_super_lockfs	= NULL,		/* ? */
2696 	//.unlockfs	= NULL,			/* ? */
2697 #endif /* NTFS_RW */
2698 	.put_super	= ntfs_put_super,	/* Syscall: umount. */
2699 	.statfs		= ntfs_statfs,		/* Syscall: statfs */
2700 	.remount_fs	= ntfs_remount,		/* Syscall: mount -o remount. */
2701 	.clear_inode	= ntfs_clear_big_inode,	/* VFS: Called when an inode is
2702 						   removed from memory. */
2703 	//.umount_begin	= NULL,			/* Forced umount. */
2704 	.show_options	= ntfs_show_options,	/* Show mount options in
2705 						   proc. */
2706 };
2707 
2708 /**
2709  * ntfs_fill_super - mount an ntfs filesystem
2710  * @sb:		super block of ntfs filesystem to mount
2711  * @opt:	string containing the mount options
2712  * @silent:	silence error output
2713  *
2714  * ntfs_fill_super() is called by the VFS to mount the device described by @sb
2715  * with the mount otions in @data with the NTFS filesystem.
2716  *
2717  * If @silent is true, remain silent even if errors are detected. This is used
2718  * during bootup, when the kernel tries to mount the root filesystem with all
2719  * registered filesystems one after the other until one succeeds. This implies
2720  * that all filesystems except the correct one will quite correctly and
2721  * expectedly return an error, but nobody wants to see error messages when in
2722  * fact this is what is supposed to happen.
2723  *
2724  * NOTE: @sb->s_flags contains the mount options flags.
2725  */
2726 static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2727 {
2728 	ntfs_volume *vol;
2729 	struct buffer_head *bh;
2730 	struct inode *tmp_ino;
2731 	int blocksize, result;
2732 
2733 	ntfs_debug("Entering.");
2734 #ifndef NTFS_RW
2735 	sb->s_flags |= MS_RDONLY;
2736 #endif /* ! NTFS_RW */
2737 	/* Allocate a new ntfs_volume and place it in sb->s_fs_info. */
2738 	sb->s_fs_info = kmalloc(sizeof(ntfs_volume), GFP_NOFS);
2739 	vol = NTFS_SB(sb);
2740 	if (!vol) {
2741 		if (!silent)
2742 			ntfs_error(sb, "Allocation of NTFS volume structure "
2743 					"failed. Aborting mount...");
2744 		return -ENOMEM;
2745 	}
2746 	/* Initialize ntfs_volume structure. */
2747 	*vol = (ntfs_volume) {
2748 		.sb = sb,
2749 		/*
2750 		 * Default is group and other don't have any access to files or
2751 		 * directories while owner has full access. Further, files by
2752 		 * default are not executable but directories are of course
2753 		 * browseable.
2754 		 */
2755 		.fmask = 0177,
2756 		.dmask = 0077,
2757 	};
2758 	init_rwsem(&vol->mftbmp_lock);
2759 	init_rwsem(&vol->lcnbmp_lock);
2760 
2761 	unlock_kernel();
2762 
2763 	/* By default, enable sparse support. */
2764 	NVolSetSparseEnabled(vol);
2765 
2766 	/* Important to get the mount options dealt with now. */
2767 	if (!parse_options(vol, (char*)opt))
2768 		goto err_out_now;
2769 
2770 	/* We support sector sizes up to the PAGE_CACHE_SIZE. */
2771 	if (bdev_hardsect_size(sb->s_bdev) > PAGE_CACHE_SIZE) {
2772 		if (!silent)
2773 			ntfs_error(sb, "Device has unsupported sector size "
2774 					"(%i).  The maximum supported sector "
2775 					"size on this architecture is %lu "
2776 					"bytes.",
2777 					bdev_hardsect_size(sb->s_bdev),
2778 					PAGE_CACHE_SIZE);
2779 		goto err_out_now;
2780 	}
2781 	/*
2782 	 * Setup the device access block size to NTFS_BLOCK_SIZE or the hard
2783 	 * sector size, whichever is bigger.
2784 	 */
2785 	blocksize = sb_min_blocksize(sb, NTFS_BLOCK_SIZE);
2786 	if (blocksize < NTFS_BLOCK_SIZE) {
2787 		if (!silent)
2788 			ntfs_error(sb, "Unable to set device block size.");
2789 		goto err_out_now;
2790 	}
2791 	BUG_ON(blocksize != sb->s_blocksize);
2792 	ntfs_debug("Set device block size to %i bytes (block size bits %i).",
2793 			blocksize, sb->s_blocksize_bits);
2794 	/* Determine the size of the device in units of block_size bytes. */
2795 	if (!i_size_read(sb->s_bdev->bd_inode)) {
2796 		if (!silent)
2797 			ntfs_error(sb, "Unable to determine device size.");
2798 		goto err_out_now;
2799 	}
2800 	vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2801 			sb->s_blocksize_bits;
2802 	/* Read the boot sector and return unlocked buffer head to it. */
2803 	if (!(bh = read_ntfs_boot_sector(sb, silent))) {
2804 		if (!silent)
2805 			ntfs_error(sb, "Not an NTFS volume.");
2806 		goto err_out_now;
2807 	}
2808 	/*
2809 	 * Extract the data from the boot sector and setup the ntfs volume
2810 	 * using it.
2811 	 */
2812 	result = parse_ntfs_boot_sector(vol, (NTFS_BOOT_SECTOR*)bh->b_data);
2813 	brelse(bh);
2814 	if (!result) {
2815 		if (!silent)
2816 			ntfs_error(sb, "Unsupported NTFS filesystem.");
2817 		goto err_out_now;
2818 	}
2819 	/*
2820 	 * If the boot sector indicates a sector size bigger than the current
2821 	 * device block size, switch the device block size to the sector size.
2822 	 * TODO: It may be possible to support this case even when the set
2823 	 * below fails, we would just be breaking up the i/o for each sector
2824 	 * into multiple blocks for i/o purposes but otherwise it should just
2825 	 * work.  However it is safer to leave disabled until someone hits this
2826 	 * error message and then we can get them to try it without the setting
2827 	 * so we know for sure that it works.
2828 	 */
2829 	if (vol->sector_size > blocksize) {
2830 		blocksize = sb_set_blocksize(sb, vol->sector_size);
2831 		if (blocksize != vol->sector_size) {
2832 			if (!silent)
2833 				ntfs_error(sb, "Unable to set device block "
2834 						"size to sector size (%i).",
2835 						vol->sector_size);
2836 			goto err_out_now;
2837 		}
2838 		BUG_ON(blocksize != sb->s_blocksize);
2839 		vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2840 				sb->s_blocksize_bits;
2841 		ntfs_debug("Changed device block size to %i bytes (block size "
2842 				"bits %i) to match volume sector size.",
2843 				blocksize, sb->s_blocksize_bits);
2844 	}
2845 	/* Initialize the cluster and mft allocators. */
2846 	ntfs_setup_allocators(vol);
2847 	/* Setup remaining fields in the super block. */
2848 	sb->s_magic = NTFS_SB_MAGIC;
2849 	/*
2850 	 * Ntfs allows 63 bits for the file size, i.e. correct would be:
2851 	 *	sb->s_maxbytes = ~0ULL >> 1;
2852 	 * But the kernel uses a long as the page cache page index which on
2853 	 * 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel
2854 	 * defined to the maximum the page cache page index can cope with
2855 	 * without overflowing the index or to 2^63 - 1, whichever is smaller.
2856 	 */
2857 	sb->s_maxbytes = MAX_LFS_FILESIZE;
2858 	/* Ntfs measures time in 100ns intervals. */
2859 	sb->s_time_gran = 100;
2860 	/*
2861 	 * Now load the metadata required for the page cache and our address
2862 	 * space operations to function. We do this by setting up a specialised
2863 	 * read_inode method and then just calling the normal iget() to obtain
2864 	 * the inode for $MFT which is sufficient to allow our normal inode
2865 	 * operations and associated address space operations to function.
2866 	 */
2867 	sb->s_op = &ntfs_sops;
2868 	tmp_ino = new_inode(sb);
2869 	if (!tmp_ino) {
2870 		if (!silent)
2871 			ntfs_error(sb, "Failed to load essential metadata.");
2872 		goto err_out_now;
2873 	}
2874 	tmp_ino->i_ino = FILE_MFT;
2875 	insert_inode_hash(tmp_ino);
2876 	if (ntfs_read_inode_mount(tmp_ino) < 0) {
2877 		if (!silent)
2878 			ntfs_error(sb, "Failed to load essential metadata.");
2879 		goto iput_tmp_ino_err_out_now;
2880 	}
2881 	down(&ntfs_lock);
2882 	/*
2883 	 * The current mount is a compression user if the cluster size is
2884 	 * less than or equal 4kiB.
2885 	 */
2886 	if (vol->cluster_size <= 4096 && !ntfs_nr_compression_users++) {
2887 		result = allocate_compression_buffers();
2888 		if (result) {
2889 			ntfs_error(NULL, "Failed to allocate buffers "
2890 					"for compression engine.");
2891 			ntfs_nr_compression_users--;
2892 			up(&ntfs_lock);
2893 			goto iput_tmp_ino_err_out_now;
2894 		}
2895 	}
2896 	/*
2897 	 * Generate the global default upcase table if necessary.  Also
2898 	 * temporarily increment the number of upcase users to avoid race
2899 	 * conditions with concurrent (u)mounts.
2900 	 */
2901 	if (!default_upcase)
2902 		default_upcase = generate_default_upcase();
2903 	ntfs_nr_upcase_users++;
2904 	up(&ntfs_lock);
2905 	/*
2906 	 * From now on, ignore @silent parameter. If we fail below this line,
2907 	 * it will be due to a corrupt fs or a system error, so we report it.
2908 	 */
2909 	/*
2910 	 * Open the system files with normal access functions and complete
2911 	 * setting up the ntfs super block.
2912 	 */
2913 	if (!load_system_files(vol)) {
2914 		ntfs_error(sb, "Failed to load system files.");
2915 		goto unl_upcase_iput_tmp_ino_err_out_now;
2916 	}
2917 	if ((sb->s_root = d_alloc_root(vol->root_ino))) {
2918 		/* We increment i_count simulating an ntfs_iget(). */
2919 		atomic_inc(&vol->root_ino->i_count);
2920 		ntfs_debug("Exiting, status successful.");
2921 		/* Release the default upcase if it has no users. */
2922 		down(&ntfs_lock);
2923 		if (!--ntfs_nr_upcase_users && default_upcase) {
2924 			ntfs_free(default_upcase);
2925 			default_upcase = NULL;
2926 		}
2927 		up(&ntfs_lock);
2928 		sb->s_export_op = &ntfs_export_ops;
2929 		lock_kernel();
2930 		return 0;
2931 	}
2932 	ntfs_error(sb, "Failed to allocate root directory.");
2933 	/* Clean up after the successful load_system_files() call from above. */
2934 	// TODO: Use ntfs_put_super() instead of repeating all this code...
2935 	// FIXME: Should mark the volume clean as the error is most likely
2936 	// 	  -ENOMEM.
2937 	iput(vol->vol_ino);
2938 	vol->vol_ino = NULL;
2939 	/* NTFS 3.0+ specific clean up. */
2940 	if (vol->major_ver >= 3) {
2941 #ifdef NTFS_RW
2942 		if (vol->usnjrnl_j_ino) {
2943 			iput(vol->usnjrnl_j_ino);
2944 			vol->usnjrnl_j_ino = NULL;
2945 		}
2946 		if (vol->usnjrnl_max_ino) {
2947 			iput(vol->usnjrnl_max_ino);
2948 			vol->usnjrnl_max_ino = NULL;
2949 		}
2950 		if (vol->usnjrnl_ino) {
2951 			iput(vol->usnjrnl_ino);
2952 			vol->usnjrnl_ino = NULL;
2953 		}
2954 		if (vol->quota_q_ino) {
2955 			iput(vol->quota_q_ino);
2956 			vol->quota_q_ino = NULL;
2957 		}
2958 		if (vol->quota_ino) {
2959 			iput(vol->quota_ino);
2960 			vol->quota_ino = NULL;
2961 		}
2962 #endif /* NTFS_RW */
2963 		if (vol->extend_ino) {
2964 			iput(vol->extend_ino);
2965 			vol->extend_ino = NULL;
2966 		}
2967 		if (vol->secure_ino) {
2968 			iput(vol->secure_ino);
2969 			vol->secure_ino = NULL;
2970 		}
2971 	}
2972 	iput(vol->root_ino);
2973 	vol->root_ino = NULL;
2974 	iput(vol->lcnbmp_ino);
2975 	vol->lcnbmp_ino = NULL;
2976 	iput(vol->mftbmp_ino);
2977 	vol->mftbmp_ino = NULL;
2978 #ifdef NTFS_RW
2979 	if (vol->logfile_ino) {
2980 		iput(vol->logfile_ino);
2981 		vol->logfile_ino = NULL;
2982 	}
2983 	if (vol->mftmirr_ino) {
2984 		iput(vol->mftmirr_ino);
2985 		vol->mftmirr_ino = NULL;
2986 	}
2987 #endif /* NTFS_RW */
2988 	/* Throw away the table of attribute definitions. */
2989 	vol->attrdef_size = 0;
2990 	if (vol->attrdef) {
2991 		ntfs_free(vol->attrdef);
2992 		vol->attrdef = NULL;
2993 	}
2994 	vol->upcase_len = 0;
2995 	down(&ntfs_lock);
2996 	if (vol->upcase == default_upcase) {
2997 		ntfs_nr_upcase_users--;
2998 		vol->upcase = NULL;
2999 	}
3000 	up(&ntfs_lock);
3001 	if (vol->upcase) {
3002 		ntfs_free(vol->upcase);
3003 		vol->upcase = NULL;
3004 	}
3005 	if (vol->nls_map) {
3006 		unload_nls(vol->nls_map);
3007 		vol->nls_map = NULL;
3008 	}
3009 	/* Error exit code path. */
3010 unl_upcase_iput_tmp_ino_err_out_now:
3011 	/*
3012 	 * Decrease the number of upcase users and destroy the global default
3013 	 * upcase table if necessary.
3014 	 */
3015 	down(&ntfs_lock);
3016 	if (!--ntfs_nr_upcase_users && default_upcase) {
3017 		ntfs_free(default_upcase);
3018 		default_upcase = NULL;
3019 	}
3020 	if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
3021 		free_compression_buffers();
3022 	up(&ntfs_lock);
3023 iput_tmp_ino_err_out_now:
3024 	iput(tmp_ino);
3025 	if (vol->mft_ino && vol->mft_ino != tmp_ino)
3026 		iput(vol->mft_ino);
3027 	vol->mft_ino = NULL;
3028 	/*
3029 	 * This is needed to get ntfs_clear_extent_inode() called for each
3030 	 * inode we have ever called ntfs_iget()/iput() on, otherwise we A)
3031 	 * leak resources and B) a subsequent mount fails automatically due to
3032 	 * ntfs_iget() never calling down into our ntfs_read_locked_inode()
3033 	 * method again... FIXME: Do we need to do this twice now because of
3034 	 * attribute inodes? I think not, so leave as is for now... (AIA)
3035 	 */
3036 	if (invalidate_inodes(sb)) {
3037 		ntfs_error(sb, "Busy inodes left. This is most likely a NTFS "
3038 				"driver bug.");
3039 		/* Copied from fs/super.c. I just love this message. (-; */
3040 		printk("NTFS: Busy inodes after umount. Self-destruct in 5 "
3041 				"seconds.  Have a nice day...\n");
3042 	}
3043 	/* Errors at this stage are irrelevant. */
3044 err_out_now:
3045 	lock_kernel();
3046 	sb->s_fs_info = NULL;
3047 	kfree(vol);
3048 	ntfs_debug("Failed, returning -EINVAL.");
3049 	return -EINVAL;
3050 }
3051 
3052 /*
3053  * This is a slab cache to optimize allocations and deallocations of Unicode
3054  * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN
3055  * (255) Unicode characters + a terminating NULL Unicode character.
3056  */
3057 struct kmem_cache *ntfs_name_cache;
3058 
3059 /* Slab caches for efficient allocation/deallocation of inodes. */
3060 struct kmem_cache *ntfs_inode_cache;
3061 struct kmem_cache *ntfs_big_inode_cache;
3062 
3063 /* Init once constructor for the inode slab cache. */
3064 static void ntfs_big_inode_init_once(void *foo, struct kmem_cache *cachep,
3065 		unsigned long flags)
3066 {
3067 	ntfs_inode *ni = (ntfs_inode *)foo;
3068 
3069 	if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
3070 			SLAB_CTOR_CONSTRUCTOR)
3071 		inode_init_once(VFS_I(ni));
3072 }
3073 
3074 /*
3075  * Slab caches to optimize allocations and deallocations of attribute search
3076  * contexts and index contexts, respectively.
3077  */
3078 struct kmem_cache *ntfs_attr_ctx_cache;
3079 struct kmem_cache *ntfs_index_ctx_cache;
3080 
3081 /* Driver wide semaphore. */
3082 DECLARE_MUTEX(ntfs_lock);
3083 
3084 static struct super_block *ntfs_get_sb(struct file_system_type *fs_type,
3085 	int flags, const char *dev_name, void *data)
3086 {
3087 	return get_sb_bdev(fs_type, flags, dev_name, data, ntfs_fill_super);
3088 }
3089 
3090 static struct file_system_type ntfs_fs_type = {
3091 	.owner		= THIS_MODULE,
3092 	.name		= "ntfs",
3093 	.get_sb		= ntfs_get_sb,
3094 	.kill_sb	= kill_block_super,
3095 	.fs_flags	= FS_REQUIRES_DEV,
3096 };
3097 
3098 /* Stable names for the slab caches. */
3099 static const char ntfs_index_ctx_cache_name[] = "ntfs_index_ctx_cache";
3100 static const char ntfs_attr_ctx_cache_name[] = "ntfs_attr_ctx_cache";
3101 static const char ntfs_name_cache_name[] = "ntfs_name_cache";
3102 static const char ntfs_inode_cache_name[] = "ntfs_inode_cache";
3103 static const char ntfs_big_inode_cache_name[] = "ntfs_big_inode_cache";
3104 
3105 static int __init init_ntfs_fs(void)
3106 {
3107 	int err = 0;
3108 
3109 	/* This may be ugly but it results in pretty output so who cares. (-8 */
3110 	printk(KERN_INFO "NTFS driver " NTFS_VERSION " [Flags: R/"
3111 #ifdef NTFS_RW
3112 			"W"
3113 #else
3114 			"O"
3115 #endif
3116 #ifdef DEBUG
3117 			" DEBUG"
3118 #endif
3119 #ifdef MODULE
3120 			" MODULE"
3121 #endif
3122 			"].\n");
3123 
3124 	ntfs_debug("Debug messages are enabled.");
3125 
3126 	ntfs_index_ctx_cache = kmem_cache_create(ntfs_index_ctx_cache_name,
3127 			sizeof(ntfs_index_context), 0 /* offset */,
3128 			SLAB_HWCACHE_ALIGN, NULL /* ctor */, NULL /* dtor */);
3129 	if (!ntfs_index_ctx_cache) {
3130 		printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3131 				ntfs_index_ctx_cache_name);
3132 		goto ictx_err_out;
3133 	}
3134 	ntfs_attr_ctx_cache = kmem_cache_create(ntfs_attr_ctx_cache_name,
3135 			sizeof(ntfs_attr_search_ctx), 0 /* offset */,
3136 			SLAB_HWCACHE_ALIGN, NULL /* ctor */, NULL /* dtor */);
3137 	if (!ntfs_attr_ctx_cache) {
3138 		printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3139 				ntfs_attr_ctx_cache_name);
3140 		goto actx_err_out;
3141 	}
3142 
3143 	ntfs_name_cache = kmem_cache_create(ntfs_name_cache_name,
3144 			(NTFS_MAX_NAME_LEN+1) * sizeof(ntfschar), 0,
3145 			SLAB_HWCACHE_ALIGN, NULL, NULL);
3146 	if (!ntfs_name_cache) {
3147 		printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3148 				ntfs_name_cache_name);
3149 		goto name_err_out;
3150 	}
3151 
3152 	ntfs_inode_cache = kmem_cache_create(ntfs_inode_cache_name,
3153 			sizeof(ntfs_inode), 0,
3154 			SLAB_RECLAIM_ACCOUNT, NULL, NULL);
3155 	if (!ntfs_inode_cache) {
3156 		printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3157 				ntfs_inode_cache_name);
3158 		goto inode_err_out;
3159 	}
3160 
3161 	ntfs_big_inode_cache = kmem_cache_create(ntfs_big_inode_cache_name,
3162 			sizeof(big_ntfs_inode), 0,
3163 			SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
3164 			ntfs_big_inode_init_once, NULL);
3165 	if (!ntfs_big_inode_cache) {
3166 		printk(KERN_CRIT "NTFS: Failed to create %s!\n",
3167 				ntfs_big_inode_cache_name);
3168 		goto big_inode_err_out;
3169 	}
3170 
3171 	/* Register the ntfs sysctls. */
3172 	err = ntfs_sysctl(1);
3173 	if (err) {
3174 		printk(KERN_CRIT "NTFS: Failed to register NTFS sysctls!\n");
3175 		goto sysctl_err_out;
3176 	}
3177 
3178 	err = register_filesystem(&ntfs_fs_type);
3179 	if (!err) {
3180 		ntfs_debug("NTFS driver registered successfully.");
3181 		return 0; /* Success! */
3182 	}
3183 	printk(KERN_CRIT "NTFS: Failed to register NTFS filesystem driver!\n");
3184 
3185 sysctl_err_out:
3186 	kmem_cache_destroy(ntfs_big_inode_cache);
3187 big_inode_err_out:
3188 	kmem_cache_destroy(ntfs_inode_cache);
3189 inode_err_out:
3190 	kmem_cache_destroy(ntfs_name_cache);
3191 name_err_out:
3192 	kmem_cache_destroy(ntfs_attr_ctx_cache);
3193 actx_err_out:
3194 	kmem_cache_destroy(ntfs_index_ctx_cache);
3195 ictx_err_out:
3196 	if (!err) {
3197 		printk(KERN_CRIT "NTFS: Aborting NTFS filesystem driver "
3198 				"registration...\n");
3199 		err = -ENOMEM;
3200 	}
3201 	return err;
3202 }
3203 
3204 static void __exit exit_ntfs_fs(void)
3205 {
3206 	int err = 0;
3207 
3208 	ntfs_debug("Unregistering NTFS driver.");
3209 
3210 	unregister_filesystem(&ntfs_fs_type);
3211 
3212 	if (kmem_cache_destroy(ntfs_big_inode_cache) && (err = 1))
3213 		printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
3214 				ntfs_big_inode_cache_name);
3215 	if (kmem_cache_destroy(ntfs_inode_cache) && (err = 1))
3216 		printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
3217 				ntfs_inode_cache_name);
3218 	if (kmem_cache_destroy(ntfs_name_cache) && (err = 1))
3219 		printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
3220 				ntfs_name_cache_name);
3221 	if (kmem_cache_destroy(ntfs_attr_ctx_cache) && (err = 1))
3222 		printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
3223 				ntfs_attr_ctx_cache_name);
3224 	if (kmem_cache_destroy(ntfs_index_ctx_cache) && (err = 1))
3225 		printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
3226 				ntfs_index_ctx_cache_name);
3227 	if (err)
3228 		printk(KERN_CRIT "NTFS: This causes memory to leak! There is "
3229 				"probably a BUG in the driver! Please report "
3230 				"you saw this message to "
3231 				"linux-ntfs-dev@lists.sourceforge.net\n");
3232 	/* Unregister the ntfs sysctls. */
3233 	ntfs_sysctl(0);
3234 }
3235 
3236 MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>");
3237 MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2006 Anton Altaparmakov");
3238 MODULE_VERSION(NTFS_VERSION);
3239 MODULE_LICENSE("GPL");
3240 #ifdef DEBUG
3241 module_param(debug_msgs, bool, 0);
3242 MODULE_PARM_DESC(debug_msgs, "Enable debug messages.");
3243 #endif
3244 
3245 module_init(init_ntfs_fs)
3246 module_exit(exit_ntfs_fs)
3247