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