-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmft.c
More file actions
3539 lines (3336 loc) · 110 KB
/
mft.c
File metadata and controls
3539 lines (3336 loc) · 110 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 mft record operations.
* Part of this file is based on code from the NTFS-3G.
*
* Copyright (c) 2001-2012 Anton Altaparmakov and Tuxera Inc.
* Copyright (c) 2002 Richard Russon
* Copyright (c) 2025 LG Electronics Co., Ltd.
*/
#include <linux/writeback.h>
#include <linux/bio.h>
#include <linux/iomap.h>
#include "bitmap.h"
#include "lcnalloc.h"
#include "mft.h"
#include "ntfs.h"
/*
* ntfs_mft_record_check - Check the consistency of an MFT record
*
* Make sure its general fields are safe, then examine all its
* attributes and apply generic checks to them.
*
* Returns 0 if the checks are successful. If not, return -EIO.
*/
int ntfs_mft_record_check(const struct ntfs_volume *vol, struct mft_record *m,
unsigned long mft_no)
{
struct attr_record *a;
struct super_block *sb = vol->sb;
if (!ntfs_is_file_record(m->magic)) {
ntfs_error(sb, "Record %llu has no FILE magic (0x%x)\n",
(unsigned long long)mft_no, le32_to_cpu(*(__le32 *)m));
goto err_out;
}
if (le16_to_cpu(m->usa_ofs) & 0x1 ||
(vol->mft_record_size >> NTFS_BLOCK_SIZE_BITS) + 1 != le16_to_cpu(m->usa_count) ||
le16_to_cpu(m->usa_ofs) + le16_to_cpu(m->usa_count) * 2 > vol->mft_record_size) {
ntfs_error(sb, "Record %llu has corrupt fix-up values fields\n",
(unsigned long long)mft_no);
goto err_out;
}
if (le32_to_cpu(m->bytes_allocated) != vol->mft_record_size) {
ntfs_error(sb, "Record %llu has corrupt allocation size (%u <> %u)\n",
(unsigned long long)mft_no,
vol->mft_record_size,
le32_to_cpu(m->bytes_allocated));
goto err_out;
}
if (le32_to_cpu(m->bytes_in_use) > vol->mft_record_size) {
ntfs_error(sb, "Record %llu has corrupt in-use size (%u > %u)\n",
(unsigned long long)mft_no,
le32_to_cpu(m->bytes_in_use),
vol->mft_record_size);
goto err_out;
}
if (le16_to_cpu(m->attrs_offset) & 7) {
ntfs_error(sb, "Attributes badly aligned in record %llu\n",
(unsigned long long)mft_no);
goto err_out;
}
a = (struct attr_record *)((char *)m + le16_to_cpu(m->attrs_offset));
if ((char *)a < (char *)m || (char *)a > (char *)m + vol->mft_record_size) {
ntfs_error(sb, "Record %llu is corrupt\n",
(unsigned long long)mft_no);
goto err_out;
}
return 0;
err_out:
return -EIO;
}
/*
* map_mft_record_folio - map the folio in which a specific mft record resides
* @ni: ntfs inode whose mft record page to map
*
* This maps the folio in which the mft record of the ntfs inode @ni is
* situated.
*
* This allocates a new buffer (@ni->mrec), copies the MFT record data from
* the mapped folio into this buffer, and applies the MST (Multi Sector
* Transfer) fixups on the copy.
*
* The folio is pinned (referenced) in @ni->folio to ensure the data remains
* valid in the page cache, but the returned pointer is the allocated copy.
*
* Return: A pointer to the allocated and fixed-up mft record (@ni->mrec).
* The return value needs to be checked with IS_ERR(). If it is true,
* PTR_ERR() contains the negative error code.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
static inline struct mft_record *map_mft_record_folio(struct ntfs_inode *ni)
#else
static inline struct mft_record *map_mft_record_page(struct ntfs_inode *ni)
#endif
{
loff_t i_size;
struct ntfs_volume *vol = ni->vol;
struct inode *mft_vi = vol->mft_ino;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
struct folio *folio;
#else
struct page *page;
#endif
unsigned long index, end_index;
unsigned int ofs;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
WARN_ON(ni->folio);
#else
WARN_ON(ni->page);
#endif
/*
* The index into the page cache and the offset within the page cache
* page of the wanted mft record.
*/
index = NTFS_MFT_NR_TO_PIDX(vol, ni->mft_no);
ofs = NTFS_MFT_NR_TO_POFS(vol, ni->mft_no);
i_size = i_size_read(mft_vi);
/* The maximum valid index into the page cache for $MFT's data. */
end_index = i_size >> PAGE_SHIFT;
/* If the wanted index is out of bounds the mft record doesn't exist. */
if (unlikely(index >= end_index)) {
if (index > end_index || (i_size & ~PAGE_MASK) < ofs +
vol->mft_record_size) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
folio = ERR_PTR(-ENOENT);
#else
page = ERR_PTR(-ENOENT);
#endif
ntfs_error(vol->sb,
"Attempt to read mft record 0x%lx, which is beyond the end of the mft. This is probably a bug in the ntfs driver.",
ni->mft_no);
goto err_out;
}
}
/* Read, map, and pin the folio. */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
folio = read_mapping_folio(mft_vi->i_mapping, index, NULL);
if (!IS_ERR(folio)) {
u8 *addr;
ni->mrec = kmalloc(vol->mft_record_size, GFP_NOFS);
if (!ni->mrec) {
folio_put(folio);
folio = ERR_PTR(-ENOMEM);
goto err_out;
}
addr = kmap_local_folio(folio, 0);
memcpy(ni->mrec, addr + ofs, vol->mft_record_size);
post_read_mst_fixup((struct ntfs_record *)ni->mrec, vol->mft_record_size);
/* Catch multi sector transfer fixup errors. */
if (!ntfs_mft_record_check(vol, (struct mft_record *)ni->mrec, ni->mft_no)) {
kunmap_local(addr);
ni->folio = folio;
ni->folio_ofs = ofs;
return ni->mrec;
}
kunmap_local(addr);
folio_put(folio);
kfree(ni->mrec);
ni->mrec = NULL;
folio = ERR_PTR(-EIO);
NVolSetErrors(vol);
}
err_out:
ni->folio = NULL;
ni->folio_ofs = 0;
return (struct mft_record *)folio;
#else
page = read_mapping_page(mft_vi->i_mapping, index, NULL);
if (!IS_ERR(page)) {
ni->mrec = kmalloc(vol->mft_record_size, GFP_NOFS);
if (!ni->mrec) {
kunmap(page);
put_page(page);
page = ERR_PTR(-ENOMEM);
goto err_out;
}
memcpy(ni->mrec, page_address(page) + ofs, vol->mft_record_size);
post_read_mst_fixup((struct ntfs_record *)ni->mrec, vol->mft_record_size);
/* Catch multi sector transfer fixup errors. */
if (!ntfs_mft_record_check(vol, (struct mft_record *)ni->mrec, ni->mft_no)) {
ni->page = page;
ni->page_ofs = ofs;
return ni->mrec;
}
kunmap(page);
put_page(page);
kfree(ni->mrec);
ni->mrec = NULL;
page = ERR_PTR(-EIO);
NVolSetErrors(vol);
}
err_out:
ni->page = NULL;
ni->page_ofs = 0;
return (struct mft_record *)page;
#endif
}
/*
* map_mft_record - map and pin an mft record
* @ni: ntfs inode whose MFT record to map
*
* This function ensures the MFT record for the given inode is mapped and
* accessible.
*
* It increments the reference count of the ntfs inode. If the record is
* already mapped (@ni->folio is set), it returns the cached record
* immediately.
*
* Otherwise, it calls map_mft_record_folio() to read the folio from disk
* (if necessary via read_mapping_folio), allocate a buffer, and copy the
* record data.
*
* Return: A pointer to the mft record. You need to check the returned
* pointer with IS_ERR().
*/
struct mft_record *map_mft_record(struct ntfs_inode *ni)
{
struct mft_record *m;
if (!ni)
return ERR_PTR(-EINVAL);
ntfs_debug("Entering for mft_no 0x%lx.", ni->mft_no);
/* Make sure the ntfs inode doesn't go away. */
atomic_inc(&ni->count);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
if (ni->folio)
return (struct mft_record *)ni->mrec;
m = map_mft_record_folio(ni);
#else
if (ni->page)
return (struct mft_record *)ni->mrec;
m = map_mft_record_page(ni);
#endif
if (!IS_ERR(m))
return m;
atomic_dec(&ni->count);
ntfs_error(ni->vol->sb, "Failed with error code %lu.", -PTR_ERR(m));
return m;
}
/*
* unmap_mft_record - release a reference to a mapped mft record
* @ni: ntfs inode whose MFT record to unmap
*
* This decrements the reference count of the ntfs inode.
*
* It releases the caller's hold on the inode. If the reference count indicates
* that there are still other users (count > 1), the function returns
* immediately, keeping the resources (folio and mrec buffer) pinned for
* those users.
*
* NOTE: If caller has modified the mft record, it is imperative to set the mft
* record dirty BEFORE calling unmap_mft_record().
*/
void unmap_mft_record(struct ntfs_inode *ni)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
struct folio *folio;
if (!ni)
return;
ntfs_debug("Entering for mft_no 0x%lx.", ni->mft_no);
folio = ni->folio;
if (atomic_dec_return(&ni->count) > 1)
return;
WARN_ON(!folio);
#else
struct page *page;
if (!ni)
return;
ntfs_debug("Entering for mft_no 0x%lx.", ni->mft_no);
page = ni->page;
if (atomic_dec_return(&ni->count) > 1)
return;
WARN_ON(!page);
#endif
}
/*
* map_extent_mft_record - load an extent inode and attach it to its base
* @base_ni: base ntfs inode
* @mref: mft reference of the extent inode to load
* @ntfs_ino: on successful return, pointer to the struct ntfs_inode structure
*
* Load the extent mft record @mref and attach it to its base inode @base_ni.
* Return the mapped extent mft record if IS_ERR(result) is false. Otherwise
* PTR_ERR(result) gives the negative error code.
*
* On successful return, @ntfs_ino contains a pointer to the ntfs_inode
* structure of the mapped extent inode.
*/
struct mft_record *map_extent_mft_record(struct ntfs_inode *base_ni, u64 mref,
struct ntfs_inode **ntfs_ino)
{
struct mft_record *m;
struct ntfs_inode *ni = NULL;
struct ntfs_inode **extent_nis = NULL;
int i;
unsigned long mft_no = MREF(mref);
u16 seq_no = MSEQNO(mref);
bool destroy_ni = false;
ntfs_debug("Mapping extent mft record 0x%lx (base mft record 0x%lx).",
mft_no, base_ni->mft_no);
/* Make sure the base ntfs inode doesn't go away. */
atomic_inc(&base_ni->count);
/*
* Check if this extent inode has already been added to the base inode,
* in which case just return it. If not found, add it to the base
* inode before returning it.
*/
retry:
mutex_lock(&base_ni->extent_lock);
if (base_ni->nr_extents > 0) {
extent_nis = base_ni->ext.extent_ntfs_inos;
for (i = 0; i < base_ni->nr_extents; i++) {
if (mft_no != extent_nis[i]->mft_no)
continue;
ni = extent_nis[i];
/* Make sure the ntfs inode doesn't go away. */
atomic_inc(&ni->count);
break;
}
}
if (likely(ni != NULL)) {
mutex_unlock(&base_ni->extent_lock);
atomic_dec(&base_ni->count);
/* We found the record; just have to map and return it. */
m = map_mft_record(ni);
/* map_mft_record() has incremented this on success. */
atomic_dec(&ni->count);
if (!IS_ERR(m)) {
/* Verify the sequence number. */
if (likely(le16_to_cpu(m->sequence_number) == seq_no)) {
ntfs_debug("Done 1.");
*ntfs_ino = ni;
return m;
}
unmap_mft_record(ni);
ntfs_error(base_ni->vol->sb,
"Found stale extent mft reference! Corrupt filesystem. Run chkdsk.");
return ERR_PTR(-EIO);
}
map_err_out:
ntfs_error(base_ni->vol->sb,
"Failed to map extent mft record, error code %ld.",
-PTR_ERR(m));
return m;
}
mutex_unlock(&base_ni->extent_lock);
/* Record wasn't there. Get a new ntfs inode and initialize it. */
ni = ntfs_new_extent_inode(base_ni->vol->sb, mft_no);
if (unlikely(!ni)) {
atomic_dec(&base_ni->count);
return ERR_PTR(-ENOMEM);
}
ni->vol = base_ni->vol;
ni->seq_no = seq_no;
ni->nr_extents = -1;
ni->ext.base_ntfs_ino = base_ni;
/* Now map the record. */
m = map_mft_record(ni);
if (IS_ERR(m)) {
atomic_dec(&base_ni->count);
ntfs_clear_extent_inode(ni);
goto map_err_out;
}
/* Verify the sequence number if it is present. */
if (seq_no && (le16_to_cpu(m->sequence_number) != seq_no)) {
ntfs_error(base_ni->vol->sb,
"Found stale extent mft reference! Corrupt filesystem. Run chkdsk.");
destroy_ni = true;
m = ERR_PTR(-EIO);
goto unm_nolock_err_out;
}
mutex_lock(&base_ni->extent_lock);
for (i = 0; i < base_ni->nr_extents; i++) {
if (mft_no == extent_nis[i]->mft_no) {
mutex_unlock(&base_ni->extent_lock);
ntfs_clear_extent_inode(ni);
goto retry;
}
}
/* Attach extent inode to base inode, reallocating memory if needed. */
if (!(base_ni->nr_extents & 3)) {
struct ntfs_inode **tmp;
int new_size = (base_ni->nr_extents + 4) * sizeof(struct ntfs_inode *);
tmp = kvzalloc(new_size, GFP_NOFS);
if (unlikely(!tmp)) {
ntfs_error(base_ni->vol->sb, "Failed to allocate internal buffer.");
destroy_ni = true;
m = ERR_PTR(-ENOMEM);
goto unm_err_out;
}
if (base_ni->nr_extents) {
WARN_ON(!base_ni->ext.extent_ntfs_inos);
memcpy(tmp, base_ni->ext.extent_ntfs_inos, new_size -
4 * sizeof(struct ntfs_inode *));
kvfree(base_ni->ext.extent_ntfs_inos);
}
base_ni->ext.extent_ntfs_inos = tmp;
}
base_ni->ext.extent_ntfs_inos[base_ni->nr_extents++] = ni;
mutex_unlock(&base_ni->extent_lock);
atomic_dec(&base_ni->count);
ntfs_debug("Done 2.");
*ntfs_ino = ni;
return m;
unm_err_out:
mutex_unlock(&base_ni->extent_lock);
unm_nolock_err_out:
unmap_mft_record(ni);
atomic_dec(&base_ni->count);
/*
* If the extent inode was not attached to the base inode we need to
* release it or we will leak memory.
*/
if (destroy_ni)
ntfs_clear_extent_inode(ni);
return m;
}
/*
* __mark_mft_record_dirty - mark the base vfs inode dirty
* @ni: ntfs inode describing the mapped mft record
*
* Internal function. Users should call mark_mft_record_dirty() instead.
*
* This function determines the base ntfs inode (in case @ni is an extent
* inode) and marks the corresponding VFS inode dirty.
*
* NOTE: We only set I_DIRTY_DATASYNC (and not I_DIRTY_PAGES)
* on the base vfs inode, because even though file data may have been modified,
* it is dirty in the inode meta data rather than the data page cache of the
* inode, and thus there are no data pages that need writing out. Therefore, a
* full mark_inode_dirty() is overkill. A mark_inode_dirty_sync(), on the
* other hand, is not sufficient, because ->write_inode needs to be called even
* in case of fdatasync. This needs to happen or the file data would not
* necessarily hit the device synchronously, even though the vfs inode has the
* O_SYNC flag set. Also, I_DIRTY_DATASYNC simply "feels" better than just
* I_DIRTY_SYNC, since the file data has not actually hit the block device yet,
* which is not what I_DIRTY_SYNC on its own would suggest.
*/
void __mark_mft_record_dirty(struct ntfs_inode *ni)
{
struct ntfs_inode *base_ni;
ntfs_debug("Entering for inode 0x%lx.", ni->mft_no);
WARN_ON(NInoAttr(ni));
/* Determine the base vfs inode and mark it dirty, too. */
if (likely(ni->nr_extents >= 0))
base_ni = ni;
else
base_ni = ni->ext.base_ntfs_ino;
__mark_inode_dirty(VFS_I(base_ni), I_DIRTY_DATASYNC);
}
/*
* ntfs_bio_end_io - bio completion callback for MFT record writes
*
* Decrements the folio reference count that was incremented before
* submit_bio(). This prevents a race condition where umount could
* evict the inode and release the folio while I/O is still in flight,
* potentially causing data corruption or use-after-free.
*/
static void ntfs_bio_end_io(struct bio *bio)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
if (bio->bi_private)
folio_put((struct folio *)bio->bi_private);
#else
if (bio->bi_private)
put_page((struct page *)bio->bi_private);
#endif
bio_put(bio);
}
/*
* ntfs_sync_mft_mirror - synchronize an mft record to the mft mirror
* @vol: ntfs volume on which the mft record to synchronize resides
* @mft_no: mft record number of mft record to synchronize
* @m: mapped, mst protected (extent) mft record to synchronize
*
* Write the mapped, mst protected (extent) mft record @m with mft record
* number @mft_no to the mft mirror ($MFTMirr) of the ntfs volume @vol.
*
* On success return 0. On error return -errno and set the volume errors flag
* in the ntfs volume @vol.
*
* NOTE: We always perform synchronous i/o.
*/
int ntfs_sync_mft_mirror(struct ntfs_volume *vol, const unsigned long mft_no,
struct mft_record *m)
{
u8 *kmirr = NULL;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
struct folio *folio;
unsigned int folio_ofs, lcn_folio_off = 0;
#else
struct page *page;
unsigned int page_ofs, lcn_page_off = 0;
#endif
int err = 0;
struct bio *bio;
ntfs_debug("Entering for inode 0x%lx.", mft_no);
if (unlikely(!vol->mftmirr_ino)) {
/* This could happen during umount... */
err = -EIO;
goto err_out;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
/* Get the page containing the mirror copy of the mft record @m. */
folio = read_mapping_folio(vol->mftmirr_ino->i_mapping,
NTFS_MFT_NR_TO_PIDX(vol, mft_no), NULL);
if (IS_ERR(folio)) {
ntfs_error(vol->sb, "Failed to map mft mirror page.");
err = PTR_ERR(folio);
goto err_out;
}
folio_lock(folio);
folio_clear_uptodate(folio);
/* Offset of the mft mirror record inside the page. */
folio_ofs = NTFS_MFT_NR_TO_POFS(vol, mft_no);
/* The address in the page of the mirror copy of the mft record @m. */
kmirr = kmap_local_folio(folio, 0) + folio_ofs;
/* Copy the mst protected mft record to the mirror. */
memcpy(kmirr, m, vol->mft_record_size);
#else
/* Get the page containing the mirror copy of the mft record @m. */
page = read_mapping_page(vol->mftmirr_ino->i_mapping, mft_no >>
(PAGE_SHIFT - vol->mft_record_size_bits), NULL);
if (IS_ERR(page)) {
ntfs_error(vol->sb, "Failed to map mft mirror page.");
err = PTR_ERR(page);
goto err_out;
}
lock_page(page);
BUG_ON(!PageUptodate(page));
ClearPageUptodate(page);
/* Offset of the mft mirror record inside the page. */
page_ofs = (mft_no << vol->mft_record_size_bits) & ~PAGE_MASK;
/* The address in the page of the mirror copy of the mft record @m. */
kmirr = page_address(page) + page_ofs;
/* Copy the mst protected mft record to the mirror. */
memcpy(kmirr, m, vol->mft_record_size);
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
if (vol->cluster_size_bits > PAGE_SHIFT) {
lcn_folio_off = folio->index << PAGE_SHIFT;
lcn_folio_off &= vol->cluster_size_mask;
}
#else
if (vol->cluster_size_bits > PAGE_SHIFT) {
lcn_page_off = page->index << PAGE_SHIFT;
lcn_page_off &= vol->cluster_size_mask;
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 18, 0)
bio = bio_alloc(vol->sb->s_bdev, 1, REQ_OP_WRITE, GFP_NOIO);
#else
bio = bio_alloc(GFP_NOIO, 1);
if (!bio)
return NULL;
bio_set_dev(bio, vol->sb->s_bdev);
bio->bi_opf = REQ_OP_WRITE;
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
bio->bi_iter.bi_sector =
NTFS_B_TO_SECTOR(vol, NTFS_CLU_TO_B(vol, vol->mftmirr_lcn) +
lcn_folio_off + folio_ofs);
#else
bio->bi_iter.bi_sector =
NTFS_B_TO_SECTOR(vol, NTFS_CLU_TO_B(vol, vol->mftmirr_lcn) +
lcn_page_off + page_ofs);
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
if (!bio_add_folio(bio, folio, vol->mft_record_size, folio_ofs)) {
err = -EIO;
bio_put(bio);
goto unlock_folio;
}
#else
if (!bio_add_page(bio, page, vol->mft_record_size, page_ofs)) {
err = -EIO;
bio_put(bio);
goto unlock_page;
}
#endif
bio->bi_end_io = ntfs_bio_end_io;
submit_bio(bio);
/* Current state: all buffers are clean, unlocked, and uptodate. */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
folio_mark_uptodate(folio);
unlock_folio:
folio_unlock(folio);
kunmap_local(kmirr);
folio_put(folio);
#else
/* Current state: all buffers are clean, unlocked, and uptodate. */
SetPageUptodate(page);
unlock_page:
unlock_page(page);
kunmap(page);
put_page(page);
#endif
if (likely(!err)) {
ntfs_debug("Done.");
} else {
ntfs_error(vol->sb, "I/O error while writing mft mirror record 0x%lx!", mft_no);
err_out:
ntfs_error(vol->sb,
"Failed to synchronize $MFTMirr (error code %i). Volume will be left marked dirty on umount. Run chkdsk on the partition after umounting to correct this.",
err);
NVolSetErrors(vol);
}
return err;
}
/*
* write_mft_record_nolock - write out a mapped (extent) mft record
* @ni: ntfs inode describing the mapped (extent) mft record
* @m: mapped (extent) mft record to write
* @sync: if true, wait for i/o completion
*
* Write the mapped (extent) mft record @m described by the (regular or extent)
* ntfs inode @ni to backing store. If the mft record @m has a counterpart in
* the mft mirror, that is also updated.
*
* We only write the mft record if the ntfs inode @ni is dirty.
*
* On success, clean the mft record and return 0.
* On error (specifically ENOMEM), we redirty the record so it can be retried.
* For other errors, we mark the volume with errors.
*/
int write_mft_record_nolock(struct ntfs_inode *ni, struct mft_record *m, int sync)
{
struct ntfs_volume *vol = ni->vol;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
struct folio *folio = ni->folio;
#else
struct page *page = ni->page;
#endif
int err = 0, i = 0;
u8 *kaddr;
struct mft_record *fixup_m;
struct bio *bio;
unsigned int offset = 0, folio_size;
ntfs_debug("Entering for inode 0x%lx.", ni->mft_no);
WARN_ON(NInoAttr(ni));
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
WARN_ON(!folio_test_locked(folio));
#else
WARN_ON(!PageLocked(page));
#endif
/*
* If the struct ntfs_inode is clean no need to do anything. If it is dirty,
* mark it as clean now so that it can be redirtied later on if needed.
* There is no danger of races since the caller is holding the locks
* for the mft record @m and the page it is in.
*/
if (!NInoTestClearDirty(ni))
goto done;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
kaddr = kmap_local_folio(folio, 0);
fixup_m = (struct mft_record *)(kaddr + ni->folio_ofs);
#else
kaddr = kmap(page);
fixup_m = (struct mft_record *)(kaddr + ni->page_ofs);
#endif
memcpy(fixup_m, m, vol->mft_record_size);
/* Apply the mst protection fixups. */
err = pre_write_mst_fixup((struct ntfs_record *)fixup_m, vol->mft_record_size);
if (err) {
ntfs_error(vol->sb, "Failed to apply mst fixups!");
goto err_out;
}
folio_size = vol->mft_record_size / ni->mft_lcn_count;
while (i < ni->mft_lcn_count) {
unsigned int clu_off;
clu_off = (unsigned int)((s64)ni->mft_no * vol->mft_record_size + offset) &
vol->cluster_size_mask;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
bio = bio_alloc(vol->sb->s_bdev, 1, REQ_OP_WRITE, GFP_NOIO);
bio->bi_iter.bi_sector =
NTFS_B_TO_SECTOR(vol, NTFS_CLU_TO_B(vol, ni->mft_lcn[i]) +
clu_off);
if (!bio_add_folio(bio, folio, folio_size,
ni->folio_ofs + offset)) {
err = -EIO;
goto put_bio_out;
}
#else
bio = bio_alloc(vol->sb->s_bdev, 1, REQ_OP_WRITE, GFP_NOIO);
bio->bi_iter.bi_sector =
NTFS_B_TO_SECTOR(vol, NTFS_CLU_TO_B(vol, ni->mft_lcn[i]) +
clu_off);
if (!bio) {
err = -ENOMEM;
goto err_out;
}
if (!bio_add_page(bio, page, folio_size,
ni->page_ofs + offset)) {
err = -EIO;
goto put_bio_out;
}
#endif
/* Synchronize the mft mirror now if not @sync. */
if (!sync && ni->mft_no < vol->mftmirr_size)
ntfs_sync_mft_mirror(vol, ni->mft_no, fixup_m);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
folio_get(folio);
bio->bi_private = folio;
#else
get_page(page);
bio->bi_private = page;
#endif
bio->bi_end_io = ntfs_bio_end_io;
submit_bio(bio);
offset += vol->cluster_size;
i++;
}
/* If @sync, now synchronize the mft mirror. */
if (sync && ni->mft_no < vol->mftmirr_size)
ntfs_sync_mft_mirror(vol, ni->mft_no, fixup_m);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
kunmap_local(kaddr);
#else
kunmap(page);
#endif
if (unlikely(err)) {
/* I/O error during writing. This is really bad! */
ntfs_error(vol->sb,
"I/O error while writing mft record 0x%lx! Marking base inode as bad. You should unmount the volume and run chkdsk.",
ni->mft_no);
goto err_out;
}
done:
ntfs_debug("Done.");
return 0;
put_bio_out:
bio_put(bio);
err_out:
/*
* Current state: all buffers are clean, unlocked, and uptodate.
* The caller should mark the base inode as bad so that no more i/o
* happens. ->clear_inode() will still be invoked so all extent inodes
* and other allocated memory will be freed.
*/
if (err == -ENOMEM) {
ntfs_error(vol->sb,
"Not enough memory to write mft record. Redirtying so the write is retried later.");
mark_mft_record_dirty(ni);
err = 0;
} else
NVolSetErrors(vol);
return err;
}
static int ntfs_test_inode_wb(struct inode *vi, unsigned long ino, void *data)
{
struct ntfs_attr *na = data;
if (!ntfs_test_inode(vi, na))
return 0;
/*
* Without this, ntfs_write_mst_block() could call iput_final()
* , and ntfs_evict_big_inode() could try to unlink this inode
* and the contex could be blocked infinitly in map_mft_record().
*/
if (NInoBeingDeleted(NTFS_I(vi))) {
na->state = NI_BeingDeleted;
return -1;
}
/*
* This condition can prevent ntfs_write_mst_block()
* from applying/undo fixups while ntfs_create() being
* called
*/
spin_lock(&vi->i_lock);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 19, 0)
if (inode_state_read_once(vi) & I_CREATING) {
#else
if (vi->i_state & I_CREATING) {
#endif
spin_unlock(&vi->i_lock);
na->state = NI_BeingCreated;
return -1;
}
spin_unlock(&vi->i_lock);
return igrab(vi) ? 1 : -1;
}
/*
* ntfs_may_write_mft_record - check if an mft record may be written out
* @vol: [IN] ntfs volume on which the mft record to check resides
* @mft_no: [IN] mft record number of the mft record to check
* @m: [IN] mapped mft record to check
* @locked_ni: [OUT] caller has to unlock this ntfs inode if one is returned
* @ref_vi: [OUT] caller has to drop this vfs inode if one is returned
*
* Check if the mapped (base or extent) mft record @m with mft record number
* @mft_no belonging to the ntfs volume @vol may be written out. If necessary
* and possible the ntfs inode of the mft record is locked and the base vfs
* inode is pinned. The locked ntfs inode is then returned in @locked_ni. The
* caller is responsible for unlocking the ntfs inode and unpinning the base
* vfs inode.
*
* To avoid deadlock when the caller holds a folio lock, if the function
* returns @ref_vi it defers dropping the vfs inode reference by returning
* it in @ref_vi instead of calling iput() directly. The caller must call
* iput() on @ref_vi after releasing the folio lock.
*
* Return 'true' if the mft record may be written out and 'false' if not.
*
* The caller has locked the page and cleared the uptodate flag on it which
* means that we can safely write out any dirty mft records that do not have
* their inodes in icache as determined by find_inode_nowait().
*
* Here is a description of the tests we perform:
*
* If the inode is found in icache we know the mft record must be a base mft
* record. If it is dirty, we do not write it and return 'false' as the vfs
* inode write paths will result in the access times being updated which would
* cause the base mft record to be redirtied and written out again.
*
* If the inode is in icache and not dirty, we attempt to lock the mft record
* and if we find the lock was already taken, it is not safe to write the mft
* record and we return 'false'.
*
* If we manage to obtain the lock we have exclusive access to the mft record,
* which also allows us safe writeout of the mft record. We then set
* @locked_ni to the locked ntfs inode and return 'true'.
*
* Note we cannot just lock the mft record and sleep while waiting for the lock
* because this would deadlock due to lock reversal.
*
* If the inode is not in icache we need to perform further checks.
*
* If the mft record is not a FILE record or it is a base mft record, we can
* safely write it and return 'true'.
*
* We now know the mft record is an extent mft record. We check if the inode
* corresponding to its base mft record is in icache. If it is not, we cannot
* safely determine the state of the extent inode, so we return 'false'.
*
* We now have the base inode for the extent mft record. We check if it has an
* ntfs inode for the extent mft record attached. If not, it is safe to write
* the extent mft record and we return 'true'.
*
* If the extent inode is attached, we check if it is dirty. If so, we return
* 'false' (letting the standard write_inode path handle it).
*
* If it is not dirty, we attempt to lock the extent mft record. If the lock
* was already taken, it is not safe to write and we return 'false'.
*
* If we manage to obtain the lock we have exclusive access to the extent mft
* record. We set @locked_ni to the now locked ntfs inode and return 'true'.
*/
bool ntfs_may_write_mft_record(struct ntfs_volume *vol, const unsigned long mft_no,
const struct mft_record *m, struct ntfs_inode **locked_ni,
struct inode **ref_vi)
{
struct super_block *sb = vol->sb;
struct inode *mft_vi = vol->mft_ino;
struct inode *vi;
struct ntfs_inode *ni, *eni, **extent_nis;
int i;
struct ntfs_attr na = {0};
ntfs_debug("Entering for inode 0x%lx.", mft_no);
/*
* Normally we do not return a locked inode so set @locked_ni to NULL.
*/
*locked_ni = NULL;
*ref_vi = NULL;
/*
* Check if the inode corresponding to this mft record is in the VFS
* inode cache and obtain a reference to it if it is.
*/
ntfs_debug("Looking for inode 0x%lx in icache.", mft_no);
na.mft_no = mft_no;
na.type = AT_UNUSED;
/*
* Optimize inode 0, i.e. $MFT itself, since we have it in memory and
* we get here for it rather often.
*/
if (!mft_no) {
/* Balance the below iput(). */
vi = igrab(mft_vi);
WARN_ON(vi != mft_vi);
} else {
/*
* Have to use find_inode_nowait() since ilookup5_nowait()
* waits for inode with I_FREEING, which causes ntfs to deadlock
* when inodes are unlinked concurrently
*/
vi = find_inode_nowait(sb, mft_no, ntfs_test_inode_wb, &na);
if (na.state == NI_BeingDeleted || na.state == NI_BeingCreated)
return false;
}
if (vi) {
ntfs_debug("Base inode 0x%lx is in icache.", mft_no);
/* The inode is in icache. */
ni = NTFS_I(vi);
/* Take a reference to the ntfs inode. */
atomic_inc(&ni->count);
/* If the inode is dirty, do not write this record. */
if (NInoDirty(ni)) {
ntfs_debug("Inode 0x%lx is dirty, do not write it.",
mft_no);
atomic_dec(&ni->count);
*ref_vi = vi;
return false;
}
ntfs_debug("Inode 0x%lx is not dirty.", mft_no);
/* The inode is not dirty, try to take the mft record lock. */
if (unlikely(!mutex_trylock(&ni->mrec_lock))) {
ntfs_debug("Mft record 0x%lx is already locked, do not write it.", mft_no);
atomic_dec(&ni->count);
*ref_vi = vi;
return false;
}
ntfs_debug("Managed to lock mft record 0x%lx, write it.",
mft_no);
/*
* The write has to occur while we hold the mft record lock so
* return the locked ntfs inode.
*/
*locked_ni = ni;
return true;
}
ntfs_debug("Inode 0x%lx is not in icache.", mft_no);
/* The inode is not in icache. */
/* Write the record if it is not a mft record (type "FILE"). */
if (!ntfs_is_mft_record(m->magic)) {