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