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