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