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