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