-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsuper.c
More file actions
3014 lines (2793 loc) · 88.2 KB
/
Copy pathsuper.c
File metadata and controls
3014 lines (2793 loc) · 88.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* NTFS kernel super block handling.
*
* Copyright (c) 2001-2012 Anton Altaparmakov and Tuxera Inc.
* Copyright (c) 2001,2002 Richard Russon
* Copyright (c) 2025 LG Electronics Co., Ltd.
*/
#include <linux/blkdev.h> /* For bdev_logical_block_size(). */
#include <linux/backing-dev.h>
#include <linux/vfs.h>
#include <linux/fs_struct.h>
#include <linux/sched/mm.h>
#include <linux/fs_context.h>
#include <linux/fs_parser.h>
#include <linux/version.h>
#include <linux/fs_context.h>
#include <linux/fs_parser.h>
#include "sysctl.h"
#include "logfile.h"
#include "quota.h"
#include "index.h"
#include "ntfs.h"
#include "ea.h"
#include "volume.h"
#include "uapi_ntfs.h"
/* A global default upcase table and a corresponding reference count. */
static __le16 *default_upcase;
static unsigned long ntfs_nr_upcase_users;
static struct workqueue_struct *ntfs_wq;
/* Error constants/strings used in inode.c::ntfs_show_options(). */
enum {
/* One of these must be present, default is ON_ERRORS_CONTINUE. */
ON_ERRORS_PANIC = 0x01,
ON_ERRORS_REMOUNT_RO = 0x02,
ON_ERRORS_CONTINUE = 0x04,
};
static const struct constant_table ntfs_param_enums[] = {
{ "panic", ON_ERRORS_PANIC },
{ "remount-ro", ON_ERRORS_REMOUNT_RO },
{ "continue", ON_ERRORS_CONTINUE },
{}
};
enum {
Opt_uid,
Opt_gid,
Opt_umask,
Opt_dmask,
Opt_fmask,
Opt_errors,
Opt_nls,
Opt_charset,
Opt_show_sys_files,
Opt_show_meta,
Opt_case_sensitive,
Opt_disable_sparse,
Opt_sparse,
Opt_mft_zone_multiplier,
Opt_preallocated_size,
Opt_sys_immutable,
Opt_nohidden,
Opt_hide_dot_files,
Opt_check_windows_names,
Opt_acl,
Opt_discard,
Opt_nocase,
};
static const struct fs_parameter_spec ntfs_parameters[] = {
fsparam_u32("uid", Opt_uid),
fsparam_u32("gid", Opt_gid),
fsparam_u32oct("umask", Opt_umask),
fsparam_u32oct("dmask", Opt_dmask),
fsparam_u32oct("fmask", Opt_fmask),
fsparam_string("nls", Opt_nls),
fsparam_string("iocharset", Opt_charset),
fsparam_enum("errors", Opt_errors, ntfs_param_enums),
fsparam_flag("show_sys_files", Opt_show_sys_files),
fsparam_flag("showmeta", Opt_show_meta),
fsparam_flag("case_sensitive", Opt_case_sensitive),
fsparam_flag("disable_sparse", Opt_disable_sparse),
fsparam_s32("mft_zone_multiplier", Opt_mft_zone_multiplier),
fsparam_u64("preallocated_size", Opt_preallocated_size),
fsparam_flag("sys_immutable", Opt_sys_immutable),
fsparam_flag("nohidden", Opt_nohidden),
fsparam_flag("hide_dot_files", Opt_hide_dot_files),
fsparam_flag("windows_names", Opt_check_windows_names),
fsparam_flag("acl", Opt_acl),
fsparam_flag("discard", Opt_discard),
fsparam_flag("sparse", Opt_sparse),
fsparam_flag("nocase", Opt_nocase),
{}
};
static int ntfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
{
struct ntfs_volume *vol = fc->s_fs_info;
struct fs_parse_result result;
int opt;
opt = fs_parse(fc, ntfs_parameters, param, &result);
if (opt < 0)
return opt;
switch (opt) {
case Opt_uid:
vol->uid = make_kuid(current_user_ns(), result.uint_32);
break;
case Opt_gid:
vol->gid = make_kgid(current_user_ns(), result.uint_32);
break;
case Opt_umask:
vol->fmask = vol->dmask = result.uint_32;
break;
case Opt_dmask:
vol->dmask = result.uint_32;
break;
case Opt_fmask:
vol->fmask = result.uint_32;
break;
case Opt_errors:
vol->on_errors = result.uint_32;
break;
case Opt_nls:
case Opt_charset:
if (vol->nls_map)
unload_nls(vol->nls_map);
vol->nls_map = load_nls(param->string);
if (!vol->nls_map) {
ntfs_error(vol->sb, "Failed to load NLS table '%s'.",
param->string);
return -EINVAL;
}
break;
case Opt_mft_zone_multiplier:
if (vol->mft_zone_multiplier && vol->mft_zone_multiplier !=
result.int_32) {
ntfs_error(vol->sb, "Cannot change mft_zone_multiplier on remount.");
return -EINVAL;
}
if (result.int_32 < 1 || result.int_32 > 4) {
ntfs_error(vol->sb,
"Invalid mft_zone_multiplier. Using default value, i.e. 1.");
vol->mft_zone_multiplier = 1;
} else
vol->mft_zone_multiplier = result.int_32;
break;
case Opt_show_sys_files:
case Opt_show_meta:
if (result.boolean)
NVolSetShowSystemFiles(vol);
else
NVolClearShowSystemFiles(vol);
break;
case Opt_case_sensitive:
if (result.boolean)
NVolSetCaseSensitive(vol);
else
NVolClearCaseSensitive(vol);
break;
case Opt_nocase:
if (result.boolean)
NVolClearCaseSensitive(vol);
else
NVolSetCaseSensitive(vol);
break;
case Opt_preallocated_size:
vol->preallocated_size = (loff_t)result.uint_64;
break;
case Opt_sys_immutable:
if (result.boolean)
NVolSetSysImmutable(vol);
else
NVolClearSysImmutable(vol);
break;
case Opt_nohidden:
if (result.boolean)
NVolClearShowHiddenFiles(vol);
else
NVolSetShowHiddenFiles(vol);
break;
case Opt_hide_dot_files:
if (result.boolean)
NVolSetHideDotFiles(vol);
else
NVolClearHideDotFiles(vol);
break;
case Opt_check_windows_names:
if (result.boolean)
NVolSetCheckWindowsNames(vol);
else
NVolClearCheckWindowsNames(vol);
break;
case Opt_acl:
#ifdef CONFIG_NTFS_FS_POSIX_ACL
if (result.boolean)
fc->sb_flags |= SB_POSIXACL;
else
fc->sb_flags &= ~SB_POSIXACL;
break;
#else
return -EINVAL;
#endif
case Opt_discard:
if (result.boolean)
NVolSetDiscard(vol);
else
NVolClearDiscard(vol);
break;
case Opt_disable_sparse:
if (result.boolean)
NVolSetDisableSparse(vol);
else
NVolClearDisableSparse(vol);
break;
case Opt_sparse:
break;
default:
return -EINVAL;
}
return 0;
}
static int ntfs_reconfigure(struct fs_context *fc)
{
struct super_block *sb = fc->root->d_sb;
struct ntfs_volume *vol = NTFS_SB(sb);
ntfs_debug("Entering with remount");
sync_filesystem(sb);
/*
* For the read-write compiled driver, if we are remounting read-write,
* make sure there are no volume errors and that no unsupported volume
* flags are set. Also, empty the logfile journal as it would become
* stale as soon as something is written to the volume and mark the
* volume dirty so that chkdsk is run if the volume is not umounted
* cleanly. Finally, mark the quotas out of date so Windows rescans
* the volume on boot and updates them.
*
* When remounting read-only, mark the volume clean if no volume errors
* have occurred.
*/
if (sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY)) {
static const char *es = ". Cannot remount read-write.";
/* Remounting read-write. */
if (NVolErrors(vol)) {
ntfs_error(sb, "Volume has errors and is read-only%s",
es);
return -EROFS;
}
if (vol->vol_flags & VOLUME_IS_DIRTY) {
ntfs_error(sb, "Volume is dirty and read-only%s", es);
return -EROFS;
}
if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
ntfs_error(sb, "Volume has been modified by chkdsk and is read-only%s", es);
return -EROFS;
}
if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
ntfs_error(sb, "Volume has unsupported flags set (0x%x) and is read-only%s",
le16_to_cpu(vol->vol_flags), es);
return -EROFS;
}
if (vol->logfile_ino && !ntfs_empty_logfile(vol->logfile_ino)) {
ntfs_error(sb, "Failed to empty journal LogFile%s",
es);
NVolSetErrors(vol);
return -EROFS;
}
if (!ntfs_mark_quotas_out_of_date(vol)) {
ntfs_error(sb, "Failed to mark quotas out of date%s",
es);
NVolSetErrors(vol);
return -EROFS;
}
} else if (!sb_rdonly(sb) && (fc->sb_flags & SB_RDONLY)) {
/* Remounting read-only. */
if (!NVolErrors(vol)) {
if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
ntfs_warning(sb,
"Failed to clear dirty bit in volume information flags. Run chkdsk.");
}
}
ntfs_debug("Done.");
return 0;
}
const struct option_t on_errors_arr[] = {
{ ON_ERRORS_PANIC, "panic" },
{ ON_ERRORS_REMOUNT_RO, "remount-ro", },
{ ON_ERRORS_CONTINUE, "continue", },
{ 0, NULL }
};
void ntfs_handle_error(struct super_block *sb)
{
struct ntfs_volume *vol = NTFS_SB(sb);
if (sb_rdonly(sb))
return;
if (vol->on_errors == ON_ERRORS_REMOUNT_RO) {
sb->s_flags |= SB_RDONLY;
pr_crit("(device %s): Filesystem has been set read-only\n",
sb->s_id);
} else if (vol->on_errors == ON_ERRORS_PANIC) {
panic("ntfs: (device %s): panic from previous error\n",
sb->s_id);
} else if (vol->on_errors == ON_ERRORS_CONTINUE) {
if (errseq_check(&sb->s_wb_err, vol->wb_err) == -ENODEV) {
NVolSetShutdown(vol);
vol->wb_err = sb->s_wb_err;
}
}
}
/*
* ntfs_write_volume_flags - write new flags to the volume information flags
* @vol: ntfs volume on which to modify the flags
* @flags: new flags value for the volume information flags
*
* Internal function. You probably want to use ntfs_{set,clear}_volume_flags()
* instead (see below).
*
* Replace the volume information flags on the volume @vol with the value
* supplied in @flags. Note, this overwrites the volume information flags, so
* make sure to combine the flags you want to modify with the old flags and use
* the result when calling ntfs_write_volume_flags().
*
* Return 0 on success and -errno on error.
*/
static int ntfs_write_volume_flags(struct ntfs_volume *vol, const __le16 flags)
{
struct ntfs_inode *ni = NTFS_I(vol->vol_ino);
struct volume_information *vi;
struct ntfs_attr_search_ctx *ctx;
int err;
ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
le16_to_cpu(vol->vol_flags), le16_to_cpu(flags));
mutex_lock(&ni->mrec_lock);
if (vol->vol_flags == flags)
goto done;
ctx = ntfs_attr_get_search_ctx(ni, NULL);
if (!ctx) {
err = -ENOMEM;
goto put_unm_err_out;
}
err = ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
ctx);
if (err)
goto put_unm_err_out;
vi = (struct volume_information *)((u8 *)ctx->attr +
le16_to_cpu(ctx->attr->data.resident.value_offset));
vol->vol_flags = vi->flags = flags;
mark_mft_record_dirty(ctx->ntfs_ino);
ntfs_attr_put_search_ctx(ctx);
done:
mutex_unlock(&ni->mrec_lock);
ntfs_debug("Done.");
return 0;
put_unm_err_out:
if (ctx)
ntfs_attr_put_search_ctx(ctx);
mutex_unlock(&ni->mrec_lock);
ntfs_error(vol->sb, "Failed with error code %i.", -err);
return err;
}
/*
* ntfs_set_volume_flags - set bits in the volume information flags
* @vol: ntfs volume on which to modify the flags
* @flags: flags to set on the volume
*
* Set the bits in @flags in the volume information flags on the volume @vol.
*
* Return 0 on success and -errno on error.
*/
int ntfs_set_volume_flags(struct ntfs_volume *vol, __le16 flags)
{
flags &= VOLUME_FLAGS_MASK;
return ntfs_write_volume_flags(vol, vol->vol_flags | flags);
}
/*
* ntfs_clear_volume_flags - clear bits in the volume information flags
* @vol: ntfs volume on which to modify the flags
* @flags: flags to clear on the volume
*
* Clear the bits in @flags in the volume information flags on the volume @vol.
*
* Return 0 on success and -errno on error.
*/
int ntfs_clear_volume_flags(struct ntfs_volume *vol, __le16 flags)
{
flags &= VOLUME_FLAGS_MASK;
flags = vol->vol_flags & cpu_to_le16(~le16_to_cpu(flags));
return ntfs_write_volume_flags(vol, flags);
}
int ntfs_write_volume_label(struct ntfs_volume *vol, char *label)
{
struct ntfs_inode *vol_ni = NTFS_I(vol->vol_ino);
struct ntfs_attr_search_ctx *ctx;
char *new_label;
__le16 *uname;
int uname_len, ret;
uname_len = ntfs_nlstoucs(vol, label, strlen(label),
&uname, FSLABEL_MAX);
if (uname_len < 0) {
ntfs_error(vol->sb,
"Failed to convert volume label '%s' to Unicode.",
label);
return uname_len;
}
if (uname_len > NTFS_MAX_LABEL_LEN) {
ntfs_error(vol->sb,
"Volume label is too long (max %d characters).",
NTFS_MAX_LABEL_LEN);
kvfree(uname);
return -EINVAL;
}
/*
* Allocate the in-memory label copy up front. If kstrdup() fails we
* bail out before touching on-disk metadata, so the in-memory label
* and the on-disk label stay in sync.
*/
new_label = kstrdup(label, GFP_KERNEL);
if (!new_label) {
kvfree(uname);
return -ENOMEM;
}
mutex_lock(&vol_ni->mrec_lock);
ctx = ntfs_attr_get_search_ctx(vol_ni, NULL);
if (!ctx) {
ret = -ENOMEM;
goto out;
}
if (!ntfs_attr_lookup(AT_VOLUME_NAME, NULL, 0, 0, 0, NULL, 0,
ctx))
ntfs_attr_record_rm(ctx);
ntfs_attr_put_search_ctx(ctx);
ret = ntfs_resident_attr_record_add(vol_ni, AT_VOLUME_NAME, AT_UNNAMED, 0,
(u8 *)uname, uname_len * sizeof(__le16), 0);
out:
mutex_unlock(&vol_ni->mrec_lock);
kvfree(uname);
if (ret >= 0) {
kfree(vol->volume_label);
vol->volume_label = new_label;
mark_inode_dirty_sync(vol->vol_ino);
ret = 0;
} else {
kfree(new_label);
}
return ret;
}
/*
* is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector
* @sb: Super block of the device to which @b belongs.
* @b: Boot sector of device @sb to check.
* @silent: If 'true', all output will be silenced.
*
* is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot
* sector. Returns 'true' if it is valid and 'false' if not.
*
* @sb is only needed for warning/error output, i.e. it can be NULL when silent
* is 'true'.
*/
static bool is_boot_sector_ntfs(const struct super_block *sb,
const struct ntfs_boot_sector *b, const bool silent)
{
/*
* Check that checksum == sum of u32 values from b to the checksum
* field. If checksum is zero, no checking is done. We will work when
* the checksum test fails, since some utilities update the boot sector
* ignoring the checksum which leaves the checksum out-of-date. We
* report a warning if this is the case.
*/
if ((void *)b < (void *)&b->checksum && b->checksum && !silent) {
__le32 *u;
u32 i;
for (i = 0, u = (__le32 *)b; u < (__le32 *)(&b->checksum); ++u)
i += le32_to_cpup(u);
if (le32_to_cpu(b->checksum) != i)
ntfs_warning(sb, "Invalid boot sector checksum.");
}
/* Check OEMidentifier is "NTFS " */
if (b->oem_id != magicNTFS)
goto not_ntfs;
/* Check bytes per sector value is between 256 and 4096. */
if (le16_to_cpu(b->bpb.bytes_per_sector) < 0x100 ||
le16_to_cpu(b->bpb.bytes_per_sector) > 0x1000)
goto not_ntfs;
/*
* Check sectors per cluster value is valid and the cluster size
* is not above the maximum (2MB).
*/
if (b->bpb.sectors_per_cluster > 0x80 &&
b->bpb.sectors_per_cluster < 0xf4)
goto not_ntfs;
/* Check reserved/unused fields are really zero. */
if (le16_to_cpu(b->bpb.reserved_sectors) ||
le16_to_cpu(b->bpb.root_entries) ||
le16_to_cpu(b->bpb.sectors) ||
le16_to_cpu(b->bpb.sectors_per_fat) ||
le32_to_cpu(b->bpb.large_sectors) || b->bpb.fats)
goto not_ntfs;
/* Check clusters per file mft record value is valid. */
if ((u8)b->clusters_per_mft_record < 0xe1 ||
(u8)b->clusters_per_mft_record > 0xf7)
switch (b->clusters_per_mft_record) {
case 1: case 2: case 4: case 8: case 16: case 32: case 64:
break;
default:
goto not_ntfs;
}
/* Check clusters per index block value is valid. */
if ((u8)b->clusters_per_index_record < 0xe1 ||
(u8)b->clusters_per_index_record > 0xf7)
switch (b->clusters_per_index_record) {
case 1: case 2: case 4: case 8: case 16: case 32: case 64:
break;
default:
goto not_ntfs;
}
/*
* Check for valid end of sector marker. We will work without it, but
* many BIOSes will refuse to boot from a bootsector if the magic is
* incorrect, so we emit a warning.
*/
if (!silent && b->end_of_sector_marker != cpu_to_le16(0xaa55))
ntfs_warning(sb, "Invalid end of sector marker.");
return true;
not_ntfs:
return false;
}
/*
* read_ntfs_boot_sector - read the NTFS boot sector of a device
* @sb: super block of device to read the boot sector from
* @silent: if true, suppress all output
*
* Reads the boot sector from the device and validates it.
*/
static char *read_ntfs_boot_sector(struct super_block *sb,
const int silent)
{
char *boot_sector;
boot_sector = kzalloc(PAGE_SIZE, GFP_NOFS);
if (!boot_sector)
return NULL;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 16, 0)
if (ntfs_bdev_read(sb->s_bdev, boot_sector, 0, PAGE_SIZE)) {
#else
if (ntfs_dev_read(sb, boot_sector, 0, PAGE_SIZE)) {
#endif
if (!silent)
ntfs_error(sb, "Unable to read primary boot sector.");
kfree(boot_sector);
return NULL;
}
if (!is_boot_sector_ntfs(sb, (struct ntfs_boot_sector *)boot_sector,
silent)) {
if (!silent)
ntfs_error(sb, "Primary boot sector is invalid.");
kfree(boot_sector);
return NULL;
}
return boot_sector;
}
/*
* parse_ntfs_boot_sector - parse the boot sector and store the data in @vol
* @vol: volume structure to initialise with data from boot sector
* @b: boot sector to parse
*
* Parse the ntfs boot sector @b and store all imporant information therein in
* the ntfs super block @vol. Return 'true' on success and 'false' on error.
*/
static bool parse_ntfs_boot_sector(struct ntfs_volume *vol,
const struct ntfs_boot_sector *b)
{
unsigned int sectors_per_cluster, sectors_per_cluster_bits, nr_hidden_sects;
int clusters_per_mft_record, clusters_per_index_record;
s64 ll;
vol->sector_size = le16_to_cpu(b->bpb.bytes_per_sector);
vol->sector_size_bits = ffs(vol->sector_size) - 1;
ntfs_debug("vol->sector_size = %i (0x%x)", vol->sector_size,
vol->sector_size);
ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits,
vol->sector_size_bits);
if (vol->sector_size < vol->sb->s_blocksize) {
ntfs_error(vol->sb,
"Sector size (%i) is smaller than the device block size (%lu). This is not supported.",
vol->sector_size, vol->sb->s_blocksize);
return false;
}
if (b->bpb.sectors_per_cluster >= 0xf4)
sectors_per_cluster = 1U << -(s8)b->bpb.sectors_per_cluster;
else
sectors_per_cluster = b->bpb.sectors_per_cluster;
ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster);
sectors_per_cluster_bits = ffs(sectors_per_cluster) - 1;
ntfs_debug("sectors_per_cluster_bits = 0x%x",
sectors_per_cluster_bits);
nr_hidden_sects = le32_to_cpu(b->bpb.hidden_sectors);
ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects);
vol->cluster_size = vol->sector_size << sectors_per_cluster_bits;
vol->cluster_size_mask = vol->cluster_size - 1;
vol->cluster_size_bits = ffs(vol->cluster_size) - 1;
ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size,
vol->cluster_size);
ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask);
ntfs_debug("vol->cluster_size_bits = %i", vol->cluster_size_bits);
if (vol->cluster_size < vol->sector_size) {
ntfs_error(vol->sb,
"Cluster size (%i) is smaller than the sector size (%i). This is not supported.",
vol->cluster_size, vol->sector_size);
return false;
}
clusters_per_mft_record = b->clusters_per_mft_record;
ntfs_debug("clusters_per_mft_record = %i (0x%x)",
clusters_per_mft_record, clusters_per_mft_record);
if (clusters_per_mft_record > 0)
vol->mft_record_size = vol->cluster_size <<
(ffs(clusters_per_mft_record) - 1);
else
/*
* When mft_record_size < cluster_size, clusters_per_mft_record
* = -log2(mft_record_size) bytes. mft_record_size normaly is
* 1024 bytes, which is encoded as 0xF6 (-10 in decimal).
*/
vol->mft_record_size = 1 << -clusters_per_mft_record;
vol->mft_record_size_mask = vol->mft_record_size - 1;
vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1;
ntfs_debug("vol->mft_record_size = %i (0x%x)", vol->mft_record_size,
vol->mft_record_size);
ntfs_debug("vol->mft_record_size_mask = 0x%x",
vol->mft_record_size_mask);
ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
vol->mft_record_size_bits, vol->mft_record_size_bits);
/*
* We cannot support mft record sizes above the PAGE_SIZE since
* we store $MFT/$DATA, the table of mft records in the page cache.
*/
if (vol->mft_record_size > PAGE_SIZE) {
ntfs_error(vol->sb,
"Mft record size (%i) exceeds the PAGE_SIZE on your system (%lu). This is not supported.",
vol->mft_record_size, PAGE_SIZE);
return false;
}
/* We cannot support mft record sizes below the sector size. */
if (vol->mft_record_size < vol->sector_size) {
ntfs_warning(vol->sb, "Mft record size (%i) is smaller than the sector size (%i).",
vol->mft_record_size, vol->sector_size);
}
clusters_per_index_record = b->clusters_per_index_record;
ntfs_debug("clusters_per_index_record = %i (0x%x)",
clusters_per_index_record, clusters_per_index_record);
if (clusters_per_index_record > 0)
vol->index_record_size = vol->cluster_size <<
(ffs(clusters_per_index_record) - 1);
else
/*
* When index_record_size < cluster_size,
* clusters_per_index_record = -log2(index_record_size) bytes.
* index_record_size normaly equals 4096 bytes, which is
* encoded as 0xF4 (-12 in decimal).
*/
vol->index_record_size = 1 << -clusters_per_index_record;
vol->index_record_size_mask = vol->index_record_size - 1;
vol->index_record_size_bits = ffs(vol->index_record_size) - 1;
ntfs_debug("vol->index_record_size = %i (0x%x)",
vol->index_record_size, vol->index_record_size);
ntfs_debug("vol->index_record_size_mask = 0x%x",
vol->index_record_size_mask);
ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
vol->index_record_size_bits,
vol->index_record_size_bits);
/* We cannot support index record sizes below the sector size. */
if (vol->index_record_size < vol->sector_size) {
ntfs_error(vol->sb,
"Index record size (%i) is smaller than the sector size (%i). This is not supported.",
vol->index_record_size, vol->sector_size);
return false;
}
/*
* Get the size of the volume in clusters and check for 64-bit-ness.
* Windows currently only uses 32 bits to save the clusters so we do
* the same as it is much faster on 32-bit CPUs.
*/
ll = le64_to_cpu(b->number_of_sectors) >> sectors_per_cluster_bits;
if ((u64)ll >= 1ULL << 32) {
ntfs_error(vol->sb, "Cannot handle 64-bit clusters.");
return false;
}
vol->nr_clusters = ll;
ntfs_debug("vol->nr_clusters = 0x%llx", vol->nr_clusters);
ll = le64_to_cpu(b->mft_lcn);
if (ll >= vol->nr_clusters) {
ntfs_error(vol->sb, "MFT LCN (%lli, 0x%llx) is beyond end of volume. Weird.",
ll, ll);
return false;
}
vol->mft_lcn = ll;
ntfs_debug("vol->mft_lcn = 0x%llx", vol->mft_lcn);
ll = le64_to_cpu(b->mftmirr_lcn);
if (ll >= vol->nr_clusters) {
ntfs_error(vol->sb, "MFTMirr LCN (%lli, 0x%llx) is beyond end of volume. Weird.",
ll, ll);
return false;
}
vol->mftmirr_lcn = ll;
ntfs_debug("vol->mftmirr_lcn = 0x%llx", vol->mftmirr_lcn);
/*
* Work out the size of the mft mirror in number of mft records. If the
* cluster size is less than or equal to the size taken by four mft
* records, the mft mirror stores the first four mft records. If the
* cluster size is bigger than the size taken by four mft records, the
* mft mirror contains as many mft records as will fit into one
* cluster.
*/
if (vol->cluster_size <= (4 << vol->mft_record_size_bits))
vol->mftmirr_size = 4;
else
vol->mftmirr_size = vol->cluster_size >>
vol->mft_record_size_bits;
ntfs_debug("vol->mftmirr_size = %i", vol->mftmirr_size);
vol->serial_no = le64_to_cpu(b->volume_serial_number);
ntfs_debug("vol->serial_no = 0x%llx", vol->serial_no);
vol->sparse_compression_unit = 4;
if (vol->cluster_size > 4096) {
switch (vol->cluster_size) {
case 65536:
vol->sparse_compression_unit = 0;
break;
case 32768:
vol->sparse_compression_unit = 1;
break;
case 16384:
vol->sparse_compression_unit = 2;
break;
case 8192:
vol->sparse_compression_unit = 3;
break;
}
}
return true;
}
/*
* ntfs_setup_allocators - initialize the cluster and mft allocators
* @vol: volume structure for which to setup the allocators
*
* Setup the cluster (lcn) and mft allocators to the starting values.
*/
static void ntfs_setup_allocators(struct ntfs_volume *vol)
{
s64 mft_zone_size, mft_lcn;
ntfs_debug("vol->mft_zone_multiplier = 0x%x",
vol->mft_zone_multiplier);
/* Determine the size of the MFT zone. */
mft_zone_size = vol->nr_clusters;
switch (vol->mft_zone_multiplier) { /* % of volume size in clusters */
case 4:
mft_zone_size >>= 1; /* 50% */
break;
case 3:
mft_zone_size = (mft_zone_size +
(mft_zone_size >> 1)) >> 2; /* 37.5% */
break;
case 2:
mft_zone_size >>= 2; /* 25% */
break;
/* case 1: */
default:
mft_zone_size >>= 3; /* 12.5% */
break;
}
/* Setup the mft zone. */
vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn;
ntfs_debug("vol->mft_zone_pos = 0x%llx", vol->mft_zone_pos);
/*
* Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
* source) and if the actual mft_lcn is in the expected place or even
* further to the front of the volume, extend the mft_zone to cover the
* beginning of the volume as well. This is in order to protect the
* area reserved for the mft bitmap as well within the mft_zone itself.
* On non-standard volumes we do not protect it as the overhead would
* be higher than the speed increase we would get by doing it.
*/
mft_lcn = NTFS_B_TO_CLU(vol, 8192 + 2 * vol->cluster_size - 1);
if (mft_lcn * vol->cluster_size < 16 * 1024)
mft_lcn = (16 * 1024 + vol->cluster_size - 1) >>
vol->cluster_size_bits;
if (vol->mft_zone_start <= mft_lcn)
vol->mft_zone_start = 0;
ntfs_debug("vol->mft_zone_start = 0x%llx", vol->mft_zone_start);
/*
* Need to cap the mft zone on non-standard volumes so that it does
* not point outside the boundaries of the volume. We do this by
* halving the zone size until we are inside the volume.
*/
vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
while (vol->mft_zone_end >= vol->nr_clusters) {
mft_zone_size >>= 1;
vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
}
ntfs_debug("vol->mft_zone_end = 0x%llx", vol->mft_zone_end);
/*
* Set the current position within each data zone to the start of the
* respective zone.
*/
vol->data1_zone_pos = vol->mft_zone_end;
ntfs_debug("vol->data1_zone_pos = 0x%llx", vol->data1_zone_pos);
vol->data2_zone_pos = 0;
ntfs_debug("vol->data2_zone_pos = 0x%llx", vol->data2_zone_pos);
/* Set the mft data allocation position to mft record 24. */
vol->mft_data_pos = 24;
ntfs_debug("vol->mft_data_pos = 0x%llx", vol->mft_data_pos);
}
static struct lock_class_key mftmirr_runlist_lock_key,
mftmirr_mrec_lock_key;
/*
* load_and_init_mft_mirror - load and setup the mft mirror inode for a volume
* @vol: ntfs super block describing device whose mft mirror to load
*
* Return 'true' on success or 'false' on error.
*/
static bool load_and_init_mft_mirror(struct ntfs_volume *vol)
{
struct inode *tmp_ino;
struct ntfs_inode *tmp_ni;
ntfs_debug("Entering.");
/* Get mft mirror inode. */
tmp_ino = ntfs_iget(vol->sb, FILE_MFTMirr);
if (IS_ERR(tmp_ino)) {
if (!IS_ERR(tmp_ino))
iput(tmp_ino);
/* Caller will display error message. */
return false;
}
lockdep_set_class(&NTFS_I(tmp_ino)->runlist.lock,
&mftmirr_runlist_lock_key);
lockdep_set_class(&NTFS_I(tmp_ino)->mrec_lock,
&mftmirr_mrec_lock_key);
/*
* Re-initialize some specifics about $MFTMirr's inode as
* ntfs_read_inode() will have set up the default ones.
*/
/* Set uid and gid to root. */
tmp_ino->i_uid = GLOBAL_ROOT_UID;
tmp_ino->i_gid = GLOBAL_ROOT_GID;
/* Regular file. No access for anyone. */
tmp_ino->i_mode = S_IFREG;
/* No VFS initiated operations allowed for $MFTMirr. */
tmp_ino->i_op = &ntfs_empty_inode_ops;
tmp_ino->i_fop = &ntfs_empty_file_ops;
/* Put in our special address space operations. */
tmp_ino->i_mapping->a_ops = &ntfs_aops;
tmp_ni = NTFS_I(tmp_ino);
/* The $MFTMirr, like the $MFT is multi sector transfer protected. */
NInoSetMstProtected(tmp_ni);
NInoSetSparseDisabled(tmp_ni);
/*
* Set up our little cheat allowing us to reuse the async read io
* completion handler for directories.
*/
tmp_ni->itype.index.block_size = vol->mft_record_size;
tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits;
vol->mftmirr_ino = tmp_ino;
ntfs_debug("Done.");
return true;
}
/*
* check_mft_mirror - compare contents of the mft mirror with the mft
* @vol: ntfs super block describing device whose mft mirror to check
*
* Return 'true' on success or 'false' on error.
*
* Note, this function also results in the mft mirror runlist being completely
* mapped into memory. The mft mirror write code requires this and will BUG()
* should it find an unmapped runlist element.
*/
static bool check_mft_mirror(struct ntfs_volume *vol)
{
struct super_block *sb = vol->sb;
struct ntfs_inode *mirr_ni;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
struct folio *mft_folio = NULL, *mirr_folio = NULL;
#else
struct page *mft_page = NULL, *mirr_page = NULL;
#endif
u8 *kmft = NULL, *kmirr = NULL;
struct runlist_element *rl, rl2[2];
pgoff_t index;
int mrecs_per_page, i;
ntfs_debug("Entering.");
/* Compare contents of $MFT and $MFTMirr. */
mrecs_per_page = PAGE_SIZE / vol->mft_record_size;
index = i = 0;
do {
u32 bytes;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
/* Switch pages if necessary. */
if (!(i % mrecs_per_page)) {
if (index) {
kunmap_local(kmirr);
folio_put(mirr_folio);
kunmap_local(kmft);
folio_put(mft_folio);
}
/* Get the $MFT page. */
mft_folio = read_mapping_folio(vol->mft_ino->i_mapping,
index, NULL);
if (IS_ERR(mft_folio)) {
ntfs_error(sb, "Failed to read $MFT.");
return false;
}
kmft = kmap_local_folio(mft_folio, 0);
/* Get the $MFTMirr page. */
mirr_folio = read_mapping_folio(vol->mftmirr_ino->i_mapping,
index, NULL);
if (IS_ERR(mirr_folio)) {
ntfs_error(sb, "Failed to read $MFTMirr.");
goto mft_unmap_out;
}
kmirr = kmap_local_folio(mirr_folio, 0);
++index;
}
#else
/* Switch pages if necessary. */
if (!(i % mrecs_per_page)) {
if (index) {
kunmap(mft_page);
put_page(mft_page);
kunmap(mirr_page);
put_page(mirr_page);
}
/* Get the $MFT page. */
mft_page = read_mapping_page(vol->mft_ino->i_mapping,
index, NULL);
if (IS_ERR(mft_page)) {
ntfs_error(sb, "Failed to read $MFT.");
return false;
}
kmft = page_address(mft_page);
/* Get the $MFTMirr page. */
mirr_page = read_mapping_page(vol->mftmirr_ino->i_mapping,
index, NULL);
if (IS_ERR(mirr_page)) {
ntfs_error(sb, "Failed to read $MFTMirr.");
goto mft_unmap_out;
}
kmirr = page_address(mirr_page);
++index;
}
#endif