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