* [PATCH] ntfs: fix $MFTMirr write offset for MFT record sizes larger than PAGE_SIZE
@ 2026-09-05 12:35 朱 天浩
2026-09-05 14:08 ` liubaolin
2026-09-07 8:54 ` Hyunchul Lee
0 siblings, 2 replies; 6+ messages in thread
From: 朱 天浩 @ 2026-09-05 12:35 UTC (permalink / raw)
To: linkinjeon, hyc.lee; +Cc: ntfs, linux-kernel
When commit 115380f9a2f9 ("ntfs: update mft operations") refactored the
MFT code to use folios, it assumed $MFTMirr is allocated contiguously,
which is indeed what mkfs arranges. However, the folio rewrite kept a
leftover from the old buffer-head/runlist based implementation:
vol->cluster_size_mask is still applied to the mirror write offset.
In fact it is no longer needed -- and in some configurations it is
actively wrong.
For example, on the built-in 4Kn SSD (Apple SSD AP0256J) of a
MacBookPro14,1 with 4096-byte MFT records, mirror record 3 is still
written to the location of record 0. $MFTMirr therefore gets out of
sync with $MFT and the following warning is printed on remount:
ntfs: (device nvme0n1p3): check_mft_mirror(): $MFT and $MFTMirr
record 0 do not match. Run chkdsk.
Fix it by always adding the folio offset (folio->index << PAGE_SHIFT)
to the base LCN address instead of applying the cluster_size_mask
truncation. This is the standard file-offset calculation and is
correct for every combination of cluster size, page size and MFT
record size, provided $MFTMirr data is contiguous from mftmirr_lcn
(which mkfs always arranges).
While here, verify that the folio returned by read_mapping_folio() is
large enough to hold the whole mirror record (folio_ofs +
mft_record_size <= folio_size(folio)), guarding against records that
straddle a folio boundary when mft_record_size exceeds PAGE_SIZE,
although this cannot happen in practice.
Tested on the volume above: before the change mirror records 1-3 are
all written to record 0's sector; after the change the write-back
probe reports:
ntfs: NTFSDBG pre mft_no=0x3 folio_idx=0x3 ofs=0x0 mftmirr_lcn=0x2540c5 clu_bits=12 rec_bits=12 PAGE_SHIFT=12 sect=0x12a0640
ntfs: NTFSDBG pre mft_no=0x0 folio_idx=0x0 ofs=0x0 mftmirr_lcn=0x2540c5 clu_bits=12 rec_bits=12 PAGE_SHIFT=12 sect=0x12a0628
which matches the expected sectors computed as:
(NTFS_CLU_TO_B(mftmirr_lcn) + (folio->index << PAGE_SHIFT)) >> SECTOR_SHIFT
record 0: (0x2540c5 << 12) + 0x0000 = 0x2540c5000 >> 9 = 0x12a0628
record 3: (0x2540c5 << 12) + 0x3000 = 0x2540c8000 >> 9 = 0x12a0640
A 13 MB file write succeeds on this volume and the file is byte-for-byte
identical after a umount/mount cycle.
Fixes: 115380f9a2f9 ("ntfs: update mft operations")
Assisted-by: UOS-AI
Assisted-by: CodeBuddy:Hy4 preview
Signed-off-by: Zhu Tianhao <zhutianhao75@hotmail.com>
---
fs/ntfs/mft.c | 26 ++++++++++++++++----------
1 file changed, 16 insertions(+), 10 deletions(-)
diff --git a/fs/ntfs/mft.c b/fs/ntfs/mft.c
index 7e58c99f1728..a62b1d41697c 100644
--- a/fs/ntfs/mft.c
+++ b/fs/ntfs/mft.c
@@ -462,7 +462,7 @@ int ntfs_sync_mft_mirror(struct ntfs_volume *vol, const u64 mft_no,
{
u8 *kmirr;
struct folio *folio;
- unsigned int folio_ofs, lcn_folio_off = 0;
+ unsigned int folio_ofs;
int err = 0;
struct bio *bio;
@@ -482,25 +482,31 @@ int ntfs_sync_mft_mirror(struct ntfs_volume *vol, const u64 mft_no,
goto err_out;
}
+ /* Offset of the mft mirror record inside the folio. */
+ folio_ofs = NTFS_MFT_NR_TO_POFS(vol, mft_no);
+
+ /* The mirror record must fit entirely within this folio. */
+ if (folio_ofs + vol->mft_record_size > folio_size(folio)) {
+ ntfs_error(vol->sb, "Mft mirror record 0x%llx does not fit in folio.",
+ mft_no);
+ folio_put(folio);
+ err = -EIO;
+ 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. */
+ /* The address in the folio 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);
kunmap_local(kmirr);
- if (vol->cluster_size_bits > PAGE_SHIFT) {
- lcn_folio_off = folio->index << PAGE_SHIFT;
- lcn_folio_off &= vol->cluster_size_mask;
- }
-
bio = bio_alloc(vol->sb->s_bdev, 1, REQ_OP_WRITE, GFP_NOIO);
bio->bi_iter.bi_sector =
ntfs_bytes_to_bio_sector(NTFS_CLU_TO_B(vol, vol->mftmirr_lcn) +
- lcn_folio_off + folio_ofs);
+ ((u64)folio->index << PAGE_SHIFT) +
+ folio_ofs);
if (bio_add_folio(bio, folio, vol->mft_record_size, folio_ofs))
err = submit_bio_wait(bio);
--
2.51.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] ntfs: fix $MFTMirr write offset for MFT record sizes larger than PAGE_SIZE
2026-09-05 12:35 [PATCH] ntfs: fix $MFTMirr write offset for MFT record sizes larger than PAGE_SIZE 朱 天浩
@ 2026-09-05 14:08 ` liubaolin
2026-09-07 8:54 ` Hyunchul Lee
1 sibling, 0 replies; 6+ messages in thread
From: liubaolin @ 2026-09-05 14:08 UTC (permalink / raw)
To: 朱 天浩, linkinjeon, hyc.lee; +Cc: ntfs, linux-kernel
在 2026/9/5 20:35, 朱 天浩 写道:
> When commit 115380f9a2f9 ("ntfs: update mft operations") refactored the
> MFT code to use folios, it assumed $MFTMirr is allocated contiguously,
> which is indeed what mkfs arranges. However, the folio rewrite kept a
> leftover from the old buffer-head/runlist based implementation:
> vol->cluster_size_mask is still applied to the mirror write offset.
> In fact it is no longer needed -- and in some configurations it is
> actively wrong.
>
> For example, on the built-in 4Kn SSD (Apple SSD AP0256J) of a
> MacBookPro14,1 with 4096-byte MFT records, mirror record 3 is still
> written to the location of record 0. $MFTMirr therefore gets out of
> sync with $MFT and the following warning is printed on remount:
>
> ntfs: (device nvme0n1p3): check_mft_mirror(): $MFT and $MFTMirr
> record 0 do not match. Run chkdsk.
>
> Fix it by always adding the folio offset (folio->index << PAGE_SHIFT)
> to the base LCN address instead of applying the cluster_size_mask
> truncation. This is the standard file-offset calculation and is
> correct for every combination of cluster size, page size and MFT
> record size, provided $MFTMirr data is contiguous from mftmirr_lcn
> (which mkfs always arranges).
>
> While here, verify that the folio returned by read_mapping_folio() is
> large enough to hold the whole mirror record (folio_ofs +
> mft_record_size <= folio_size(folio)), guarding against records that
> straddle a folio boundary when mft_record_size exceeds PAGE_SIZE,
> although this cannot happen in practice.
>
> Tested on the volume above: before the change mirror records 1-3 are
> all written to record 0's sector; after the change the write-back
> probe reports:
>
> ntfs: NTFSDBG pre mft_no=0x3 folio_idx=0x3 ofs=0x0 mftmirr_lcn=0x2540c5 clu_bits=12 rec_bits=12 PAGE_SHIFT=12 sect=0x12a0640
> ntfs: NTFSDBG pre mft_no=0x0 folio_idx=0x0 ofs=0x0 mftmirr_lcn=0x2540c5 clu_bits=12 rec_bits=12 PAGE_SHIFT=12 sect=0x12a0628
>
> which matches the expected sectors computed as:
>
> (NTFS_CLU_TO_B(mftmirr_lcn) + (folio->index << PAGE_SHIFT)) >> SECTOR_SHIFT
>
> record 0: (0x2540c5 << 12) + 0x0000 = 0x2540c5000 >> 9 = 0x12a0628
> record 3: (0x2540c5 << 12) + 0x3000 = 0x2540c8000 >> 9 = 0x12a0640
>
> A 13 MB file write succeeds on this volume and the file is byte-for-byte
> identical after a umount/mount cycle.
>
> Fixes: 115380f9a2f9 ("ntfs: update mft operations")
> Assisted-by: UOS-AI
> Assisted-by: CodeBuddy:Hy4 preview
> Signed-off-by: Zhu Tianhao <zhutianhao75@hotmail.com>
> ---
> fs/ntfs/mft.c | 26 ++++++++++++++++----------
> 1 file changed, 16 insertions(+), 10 deletions(-)
>
> diff --git a/fs/ntfs/mft.c b/fs/ntfs/mft.c
> index 7e58c99f1728..a62b1d41697c 100644
> --- a/fs/ntfs/mft.c
> +++ b/fs/ntfs/mft.c
> @@ -462,7 +462,7 @@ int ntfs_sync_mft_mirror(struct ntfs_volume *vol, const u64 mft_no,
> {
> u8 *kmirr;
> struct folio *folio;
> - unsigned int folio_ofs, lcn_folio_off = 0;
> + unsigned int folio_ofs;
> int err = 0;
> struct bio *bio;
>
> @@ -482,25 +482,31 @@ int ntfs_sync_mft_mirror(struct ntfs_volume *vol, const u64 mft_no,
> goto err_out;
> }
>
> + /* Offset of the mft mirror record inside the folio. */
> + folio_ofs = NTFS_MFT_NR_TO_POFS(vol, mft_no);
> +
> + /* The mirror record must fit entirely within this folio. */
> + if (folio_ofs + vol->mft_record_size > folio_size(folio)) {
> + ntfs_error(vol->sb, "Mft mirror record 0x%llx does not fit in folio.",
> + mft_no);
> + folio_put(folio);
> + err = -EIO;
> + 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. */
> + /* The address in the folio 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);
> kunmap_local(kmirr);
>
> - if (vol->cluster_size_bits > PAGE_SHIFT) {
> - lcn_folio_off = folio->index << PAGE_SHIFT;
> - lcn_folio_off &= vol->cluster_size_mask;
> - }
> -
> bio = bio_alloc(vol->sb->s_bdev, 1, REQ_OP_WRITE, GFP_NOIO);
> bio->bi_iter.bi_sector =
> ntfs_bytes_to_bio_sector(NTFS_CLU_TO_B(vol, vol->mftmirr_lcn) +
> - lcn_folio_off + folio_ofs);
> + ((u64)folio->index << PAGE_SHIFT) +
> + folio_ofs);
>
> if (bio_add_folio(bio, folio, vol->mft_record_size, folio_ofs))
> err = submit_bio_wait(bio);
Hi Tianhao,
Thanks for submitting this patch.
You identified a real issue where the write offset for $MFTMirr
records can be calculated incorrectly on some configurations, which may
leave $MFTMirr inconsistent with $MFT.
I have reviewed the patch carefully, and I believe the proposed fix
is effective.
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
Thanks,
Baolin.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] ntfs: fix $MFTMirr write offset for MFT record sizes larger than PAGE_SIZE
2026-09-05 12:35 [PATCH] ntfs: fix $MFTMirr write offset for MFT record sizes larger than PAGE_SIZE 朱 天浩
2026-09-05 14:08 ` liubaolin
@ 2026-09-07 8:54 ` Hyunchul Lee
2026-09-07 20:41 ` [PATCH v2] ntfs: fix $MFTMirr write offset when it spans multiple folios 朱 天浩
1 sibling, 1 reply; 6+ messages in thread
From: Hyunchul Lee @ 2026-09-07 8:54 UTC (permalink / raw)
To: 朱 天浩; +Cc: linkinjeon, ntfs, linux-kernel
Hi Tianhao,
2026년 9월 5일 (토) 오후 9:35, 朱 天浩 <zhutianhao75@hotmail.com>님이 작성:
>
> When commit 115380f9a2f9 ("ntfs: update mft operations") refactored the
> MFT code to use folios, it assumed $MFTMirr is allocated contiguously,
> which is indeed what mkfs arranges. However, the folio rewrite kept a
> leftover from the old buffer-head/runlist based implementation:
> vol->cluster_size_mask is still applied to the mirror write offset.
> In fact it is no longer needed -- and in some configurations it is
> actively wrong.
>
> For example, on the built-in 4Kn SSD (Apple SSD AP0256J) of a
> MacBookPro14,1 with 4096-byte MFT records, mirror record 3 is still
> written to the location of record 0. $MFTMirr therefore gets out of
> sync with $MFT and the following warning is printed on remount:
>
> ntfs: (device nvme0n1p3): check_mft_mirror(): $MFT and $MFTMirr
> record 0 do not match. Run chkdsk.
>
> Fix it by always adding the folio offset (folio->index << PAGE_SHIFT)
> to the base LCN address instead of applying the cluster_size_mask
> truncation. This is the standard file-offset calculation and is
> correct for every combination of cluster size, page size and MFT
> record size, provided $MFTMirr data is contiguous from mftmirr_lcn
> (which mkfs always arranges).
>
> While here, verify that the folio returned by read_mapping_folio() is
> large enough to hold the whole mirror record (folio_ofs +
> mft_record_size <= folio_size(folio)), guarding against records that
> straddle a folio boundary when mft_record_size exceeds PAGE_SIZE,
> although this cannot happen in practice.
>
> Tested on the volume above: before the change mirror records 1-3 are
> all written to record 0's sector; after the change the write-back
> probe reports:
>
> ntfs: NTFSDBG pre mft_no=0x3 folio_idx=0x3 ofs=0x0 mftmirr_lcn=0x2540c5 clu_bits=12 rec_bits=12 PAGE_SHIFT=12 sect=0x12a0640
> ntfs: NTFSDBG pre mft_no=0x0 folio_idx=0x0 ofs=0x0 mftmirr_lcn=0x2540c5 clu_bits=12 rec_bits=12 PAGE_SHIFT=12 sect=0x12a0628
>
> which matches the expected sectors computed as:
>
> (NTFS_CLU_TO_B(mftmirr_lcn) + (folio->index << PAGE_SHIFT)) >> SECTOR_SHIFT
>
> record 0: (0x2540c5 << 12) + 0x0000 = 0x2540c5000 >> 9 = 0x12a0628
> record 3: (0x2540c5 << 12) + 0x3000 = 0x2540c8000 >> 9 = 0x12a0640
>
> A 13 MB file write succeeds on this volume and the file is byte-for-byte
> identical after a umount/mount cycle.
>
> Fixes: 115380f9a2f9 ("ntfs: update mft operations")
> Assisted-by: UOS-AI
> Assisted-by: CodeBuddy:Hy4 preview
> Signed-off-by: Zhu Tianhao <zhutianhao75@hotmail.com>
> ---
> fs/ntfs/mft.c | 26 ++++++++++++++++----------
> 1 file changed, 16 insertions(+), 10 deletions(-)
>
> diff --git a/fs/ntfs/mft.c b/fs/ntfs/mft.c
> index 7e58c99f1728..a62b1d41697c 100644
> --- a/fs/ntfs/mft.c
> +++ b/fs/ntfs/mft.c
> @@ -462,7 +462,7 @@ int ntfs_sync_mft_mirror(struct ntfs_volume *vol, const u64 mft_no,
> {
> u8 *kmirr;
> struct folio *folio;
> - unsigned int folio_ofs, lcn_folio_off = 0;
> + unsigned int folio_ofs;
> int err = 0;
> struct bio *bio;
>
> @@ -482,25 +482,31 @@ int ntfs_sync_mft_mirror(struct ntfs_volume *vol, const u64 mft_no,
> goto err_out;
> }
>
> + /* Offset of the mft mirror record inside the folio. */
> + folio_ofs = NTFS_MFT_NR_TO_POFS(vol, mft_no);
> +
> + /* The mirror record must fit entirely within this folio. */
> + if (folio_ofs + vol->mft_record_size > folio_size(folio)) {
> + ntfs_error(vol->sb, "Mft mirror record 0x%llx does not fit in folio.",
> + mft_no);
> + folio_put(folio);
> + err = -EIO;
> + goto err_out;
> + }
vol->mft_record_size < PAGE_SIZE is guaranteed by ntfs_fill_super().
Is this check necessary?
Thanks,
Hyunchul
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v2] ntfs: fix $MFTMirr write offset when it spans multiple folios
2026-09-07 8:54 ` Hyunchul Lee
@ 2026-09-07 20:41 ` 朱 天浩
2026-09-08 5:51 ` liubaolin
2026-09-08 9:41 ` Namjae Jeon
0 siblings, 2 replies; 6+ messages in thread
From: 朱 天浩 @ 2026-09-07 20:41 UTC (permalink / raw)
To: Hyunchul Lee; +Cc: linkinjeon, ntfs, linux-kernel
When commit 115380f9a2f9 ("ntfs: update mft operations") refactored the
MFT code to use folios, it assumed $MFTMirr is allocated contiguously,
which is indeed what mkfs arranges. However, the folio rewrite kept a
leftover from the old buffer-head/runlist based implementation:
vol->cluster_size_mask is still applied to the mirror write offset.
In fact it is no longer needed -- and in some configurations it is
actively wrong.
The bug triggers whenever the four mirror records do not fit in a
single folio, i.e. when mft_record_size exceeds PAGE_SIZE / 4 (for
example mft_record_size > 1KiB on 4KiB pages).
For example, on the built-in 4Kn SSD (Apple SSD AP0256J) of a
MacBookPro14,1 with 4096-byte MFT records, mirror record 3 is still
written to the location of record 0. $MFTMirr therefore gets out of
sync with $MFT and the following warning is printed on remount:
ntfs: (device nvme0n1p3): check_mft_mirror(): $MFT and $MFTMirr
record 0 do not match. Run chkdsk.
Fix it by always adding the folio offset (folio->index << PAGE_SHIFT)
to the base LCN address instead of applying the cluster_size_mask
truncation. This is the standard file-offset calculation and is
correct for every combination of cluster size, page size and MFT
record size, provided $MFTMirr data is contiguous from mftmirr_lcn
(which mkfs always arranges).
Tested on the volume above: before the change mirror records 1-3 are
all written to record 0's sector; after the change the write-back
probe reports:
ntfs: NTFSDBG pre mft_no=0x3 folio_idx=0x3 ofs=0x0 mftmirr_lcn=0x2540c5 clu_bits=12 rec_bits=12 PAGE_SHIFT=12 sect=0x12a0640
ntfs: NTFSDBG pre mft_no=0x0 folio_idx=0x0 ofs=0x0 mftmirr_lcn=0x2540c5 clu_bits=12 rec_bits=12 PAGE_SHIFT=12 sect=0x12a0628
which matches the expected sectors computed as:
(NTFS_CLU_TO_B(mftmirr_lcn) + (folio->index << PAGE_SHIFT)) >> SECTOR_SHIFT
record 0: (0x2540c5 << 12) + 0x0000 = 0x2540c5000 >> 9 = 0x12a0628
record 3: (0x2540c5 << 12) + 0x3000 = 0x2540c8000 >> 9 = 0x12a0640
A 13 MB file write succeeds on this volume and the file is byte-for-byte
identical after a umount/mount cycle.
Fixes: 115380f9a2f9 ("ntfs: update mft operations")
Assisted-by: UOS-AI
Assisted-by: CodeBuddy:Hy4 preview
Signed-off-by: Zhu Tianhao <zhutianhao75@hotmail.com>
---
v2:
- Removed the unreachable folio-size guard (thanks Hyunchul for the review)
- Dropped the "While here" paragraph from the commit message
- Clarified the trigger condition and retitled the subject
fs/ntfs/mft.c | 10 +++-------
1 file changed, 3 insertions(+), 7 deletions(-)
diff --git a/fs/ntfs/mft.c b/fs/ntfs/mft.c
index 7e58c99f1728..8399f07d45d6 100644
--- a/fs/ntfs/mft.c
+++ b/fs/ntfs/mft.c
@@ -462,7 +462,7 @@ int ntfs_sync_mft_mirror(struct ntfs_volume *vol, const u64 mft_no,
{
u8 *kmirr;
struct folio *folio;
- unsigned int folio_ofs, lcn_folio_off = 0;
+ unsigned int folio_ofs;
int err = 0;
struct bio *bio;
@@ -492,15 +492,11 @@ int ntfs_sync_mft_mirror(struct ntfs_volume *vol, const u64 mft_no,
memcpy(kmirr, m, vol->mft_record_size);
kunmap_local(kmirr);
- if (vol->cluster_size_bits > PAGE_SHIFT) {
- lcn_folio_off = folio->index << PAGE_SHIFT;
- lcn_folio_off &= vol->cluster_size_mask;
- }
-
bio = bio_alloc(vol->sb->s_bdev, 1, REQ_OP_WRITE, GFP_NOIO);
bio->bi_iter.bi_sector =
ntfs_bytes_to_bio_sector(NTFS_CLU_TO_B(vol, vol->mftmirr_lcn) +
- lcn_folio_off + folio_ofs);
+ ((u64)folio->index << PAGE_SHIFT) +
+ folio_ofs);
if (bio_add_folio(bio, folio, vol->mft_record_size, folio_ofs))
err = submit_bio_wait(bio);
--
2.51.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] ntfs: fix $MFTMirr write offset when it spans multiple folios
2026-09-07 20:41 ` [PATCH v2] ntfs: fix $MFTMirr write offset when it spans multiple folios 朱 天浩
@ 2026-09-08 5:51 ` liubaolin
2026-09-08 9:41 ` Namjae Jeon
1 sibling, 0 replies; 6+ messages in thread
From: liubaolin @ 2026-09-08 5:51 UTC (permalink / raw)
To: 朱 天浩, Hyunchul Lee; +Cc: linkinjeon, ntfs, linux-kernel
在 2026/9/8 04:41, 朱 天浩 写道:
> When commit 115380f9a2f9 ("ntfs: update mft operations") refactored the
> MFT code to use folios, it assumed $MFTMirr is allocated contiguously,
> which is indeed what mkfs arranges. However, the folio rewrite kept a
> leftover from the old buffer-head/runlist based implementation:
> vol->cluster_size_mask is still applied to the mirror write offset.
> In fact it is no longer needed -- and in some configurations it is
> actively wrong.
>
> The bug triggers whenever the four mirror records do not fit in a
> single folio, i.e. when mft_record_size exceeds PAGE_SIZE / 4 (for
> example mft_record_size > 1KiB on 4KiB pages).
>
> For example, on the built-in 4Kn SSD (Apple SSD AP0256J) of a
> MacBookPro14,1 with 4096-byte MFT records, mirror record 3 is still
> written to the location of record 0. $MFTMirr therefore gets out of
> sync with $MFT and the following warning is printed on remount:
>
> ntfs: (device nvme0n1p3): check_mft_mirror(): $MFT and $MFTMirr
> record 0 do not match. Run chkdsk.
>
> Fix it by always adding the folio offset (folio->index << PAGE_SHIFT)
> to the base LCN address instead of applying the cluster_size_mask
> truncation. This is the standard file-offset calculation and is
> correct for every combination of cluster size, page size and MFT
> record size, provided $MFTMirr data is contiguous from mftmirr_lcn
> (which mkfs always arranges).
>
> Tested on the volume above: before the change mirror records 1-3 are
> all written to record 0's sector; after the change the write-back
> probe reports:
>
> ntfs: NTFSDBG pre mft_no=0x3 folio_idx=0x3 ofs=0x0 mftmirr_lcn=0x2540c5 clu_bits=12 rec_bits=12 PAGE_SHIFT=12 sect=0x12a0640
> ntfs: NTFSDBG pre mft_no=0x0 folio_idx=0x0 ofs=0x0 mftmirr_lcn=0x2540c5 clu_bits=12 rec_bits=12 PAGE_SHIFT=12 sect=0x12a0628
>
> which matches the expected sectors computed as:
>
> (NTFS_CLU_TO_B(mftmirr_lcn) + (folio->index << PAGE_SHIFT)) >> SECTOR_SHIFT
>
> record 0: (0x2540c5 << 12) + 0x0000 = 0x2540c5000 >> 9 = 0x12a0628
> record 3: (0x2540c5 << 12) + 0x3000 = 0x2540c8000 >> 9 = 0x12a0640
>
> A 13 MB file write succeeds on this volume and the file is byte-for-byte
> identical after a umount/mount cycle.
>
> Fixes: 115380f9a2f9 ("ntfs: update mft operations")
> Assisted-by: UOS-AI
> Assisted-by: CodeBuddy:Hy4 preview
> Signed-off-by: Zhu Tianhao <zhutianhao75@hotmail.com>
> ---
> v2:
> - Removed the unreachable folio-size guard (thanks Hyunchul for the review)
> - Dropped the "While here" paragraph from the commit message
> - Clarified the trigger condition and retitled the subject
>
> fs/ntfs/mft.c | 10 +++-------
> 1 file changed, 3 insertions(+), 7 deletions(-)
>
> diff --git a/fs/ntfs/mft.c b/fs/ntfs/mft.c
> index 7e58c99f1728..8399f07d45d6 100644
> --- a/fs/ntfs/mft.c
> +++ b/fs/ntfs/mft.c
> @@ -462,7 +462,7 @@ int ntfs_sync_mft_mirror(struct ntfs_volume *vol, const u64 mft_no,
> {
> u8 *kmirr;
> struct folio *folio;
> - unsigned int folio_ofs, lcn_folio_off = 0;
> + unsigned int folio_ofs;
> int err = 0;
> struct bio *bio;
>
> @@ -492,15 +492,11 @@ int ntfs_sync_mft_mirror(struct ntfs_volume *vol, const u64 mft_no,
> memcpy(kmirr, m, vol->mft_record_size);
> kunmap_local(kmirr);
>
> - if (vol->cluster_size_bits > PAGE_SHIFT) {
> - lcn_folio_off = folio->index << PAGE_SHIFT;
> - lcn_folio_off &= vol->cluster_size_mask;
> - }
> -
> bio = bio_alloc(vol->sb->s_bdev, 1, REQ_OP_WRITE, GFP_NOIO);
> bio->bi_iter.bi_sector =
> ntfs_bytes_to_bio_sector(NTFS_CLU_TO_B(vol, vol->mftmirr_lcn) +
> - lcn_folio_off + folio_ofs);
> + ((u64)folio->index << PAGE_SHIFT) +
> + folio_ofs);
>
> if (bio_add_folio(bio, folio, vol->mft_record_size, folio_ofs))
> err = submit_bio_wait(bio);
Looks good to me.
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] ntfs: fix $MFTMirr write offset when it spans multiple folios
2026-09-07 20:41 ` [PATCH v2] ntfs: fix $MFTMirr write offset when it spans multiple folios 朱 天浩
2026-09-08 5:51 ` liubaolin
@ 2026-09-08 9:41 ` Namjae Jeon
1 sibling, 0 replies; 6+ messages in thread
From: Namjae Jeon @ 2026-09-08 9:41 UTC (permalink / raw)
To: 朱 天浩; +Cc: Hyunchul Lee, ntfs, linux-kernel
On Tue, Sep 8, 2026 at 5:41 AM 朱 天浩 <zhutianhao75@hotmail.com> wrote:
>
> When commit 115380f9a2f9 ("ntfs: update mft operations") refactored the
> MFT code to use folios, it assumed $MFTMirr is allocated contiguously,
> which is indeed what mkfs arranges. However, the folio rewrite kept a
> leftover from the old buffer-head/runlist based implementation:
> vol->cluster_size_mask is still applied to the mirror write offset.
> In fact it is no longer needed -- and in some configurations it is
> actively wrong.
>
> The bug triggers whenever the four mirror records do not fit in a
> single folio, i.e. when mft_record_size exceeds PAGE_SIZE / 4 (for
> example mft_record_size > 1KiB on 4KiB pages).
>
> For example, on the built-in 4Kn SSD (Apple SSD AP0256J) of a
> MacBookPro14,1 with 4096-byte MFT records, mirror record 3 is still
> written to the location of record 0. $MFTMirr therefore gets out of
> sync with $MFT and the following warning is printed on remount:
>
> ntfs: (device nvme0n1p3): check_mft_mirror(): $MFT and $MFTMirr
> record 0 do not match. Run chkdsk.
>
> Fix it by always adding the folio offset (folio->index << PAGE_SHIFT)
> to the base LCN address instead of applying the cluster_size_mask
> truncation. This is the standard file-offset calculation and is
> correct for every combination of cluster size, page size and MFT
> record size, provided $MFTMirr data is contiguous from mftmirr_lcn
> (which mkfs always arranges).
>
> Tested on the volume above: before the change mirror records 1-3 are
> all written to record 0's sector; after the change the write-back
> probe reports:
>
> ntfs: NTFSDBG pre mft_no=0x3 folio_idx=0x3 ofs=0x0 mftmirr_lcn=0x2540c5 clu_bits=12 rec_bits=12 PAGE_SHIFT=12 sect=0x12a0640
> ntfs: NTFSDBG pre mft_no=0x0 folio_idx=0x0 ofs=0x0 mftmirr_lcn=0x2540c5 clu_bits=12 rec_bits=12 PAGE_SHIFT=12 sect=0x12a0628
>
> which matches the expected sectors computed as:
>
> (NTFS_CLU_TO_B(mftmirr_lcn) + (folio->index << PAGE_SHIFT)) >> SECTOR_SHIFT
>
> record 0: (0x2540c5 << 12) + 0x0000 = 0x2540c5000 >> 9 = 0x12a0628
> record 3: (0x2540c5 << 12) + 0x3000 = 0x2540c8000 >> 9 = 0x12a0640
>
> A 13 MB file write succeeds on this volume and the file is byte-for-byte
> identical after a umount/mount cycle.
>
> Fixes: 115380f9a2f9 ("ntfs: update mft operations")
> Assisted-by: UOS-AI
> Assisted-by: CodeBuddy:Hy4 preview
> Signed-off-by: Zhu Tianhao <zhutianhao75@hotmail.com>
Applied it to #ntfs-next.
Thanks!
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-09-08 9:41 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-05 12:35 [PATCH] ntfs: fix $MFTMirr write offset for MFT record sizes larger than PAGE_SIZE 朱 天浩
2026-09-05 14:08 ` liubaolin
2026-09-07 8:54 ` Hyunchul Lee
2026-09-07 20:41 ` [PATCH v2] ntfs: fix $MFTMirr write offset when it spans multiple folios 朱 天浩
2026-09-08 5:51 ` liubaolin
2026-09-08 9:41 ` Namjae Jeon
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®