ntfs: fix directory lookup on large-page systems
PATCH 1/1 ntfs: fix directory lookup on large-page systems
ntfs_lookup_inode_by_name() copies PAGE_SIZE bytes from the index allocation mapping and applies MST fixups to that entire buffer before selecting the requested INDX block.
When PAGE_SIZE is larger than the NTFS index block size, the USA count belongs to the smaller INDX block. post_read_mst_fixup() therefore treats the PAGE_SIZE call as not applicable and leaves the update sequence words in place. A directory entry crossing a sector boundary can then be returned by readdir but missed by lookup.
Select the INDX block first, apply the fixup using its actual block size, and propagate fixup failures before validating the block.
Return -EIO when post_read_mst_fixup() detects an incomplete multi sector transfer, since this indicates an on-disk metadata integrity error.
Reproduced on arm64 with 16 KiB pages and 4 KiB NTFS index blocks. The affected entry was visible in readdir but stat returned ENOENT. Both exact and case-insensitive lookup succeed after this change.
--- dir.c | 6 ++++-- mst.c | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-)
dir.c +4 -2
@@ -338,7 +338,6 @@ descend_into_child_node: }memcpy_from_folio (kaddr, folio,0 ,PAGE_SIZE ); -post_read_mst_fixup ((struct ntfs_record *)kaddr,PAGE_SIZE );folio_unlock (folio);folio_put (folio);#else
@@ -357,7 +356,6 @@ descend_into_child_node: goto unm_err_out; }memcpy (kaddr, (u8 *)page_address (page),PAGE_SIZE ); -post_read_mst_fixup ((struct ntfs_record *)kaddr,PAGE_SIZE );unlock_page (page);kunmap (page);put_page (page);
@@ -380,6 +378,10 @@ fast_descend_into_child_node: vcn, dir_ni->mft_no); goto unm_err_out; } + err =post_read_mst_fixup ((struct ntfs_record *)ia, + dir_ni->itype.index.block_size); +if (err) + goto unm_err_out; err = ntfs_index_block_inconsistent(vol, ia, dir_ni->itype.index.block_size, vcn, COLLATION_FILE_NAME,
mst.c +2 -2
@@ -19,7 +19,7 @@* magic of the ntfs record header being processed with "BAAD" (in memory only!) * and abort processing. * -* Return 0 on success and -EINVAL on error ("BAAD" magic will be present). +* Return 0 on success and -EIO on error ("BAAD" magic will be present). * * NOTE: We consider the absence / invalidity of an update sequence array to * mean that the structure is not protected at all and hence doesn't need to
@@ -71,7 +71,7 @@ int post_read_mst_fixup(struct ntfs_record *b, const u32 size)* Note that magic_BAAD is already converted to le32. */ b->magic = magic_BAAD; -return -EINVAL ; +return -EIO ; } data_pos +=NTFS_BLOCK_SIZE /sizeof (u16); }
Signature · 1 line
2.47.3
Quoted 72 lines · Marek Wesołowski
ntfs_lookup_inode_by_name()copiesPAGE_SIZEbytes from the index allocation mapping and applies MST fixups to that entire buffer before selecting the requested INDX block.When
PAGE_SIZEis larger than the NTFS index block size, the USA count belongs to the smaller INDX block.post_read_mst_fixup()therefore treats thePAGE_SIZEcall as not applicable and leaves the update sequence words in place. A directory entry crossing a sector boundary can then be returned by readdir but missed by lookup.Select the INDX block first, apply the fixup using its actual block size, and propagate fixup failures before validating the block.
Return
-EIOwhenpost_read_mst_fixup()detects an incomplete multi sector transfer, since this indicates an on-disk metadata integrity error.Reproduced on arm64 with 16 KiB pages and 4 KiB NTFS index blocks. The affected entry was visible in readdir but stat returned ENOENT. Both exact and case-insensitive lookup succeed after this change.
Fixes d3ad708fecaa ("ntfs: Initial commit")Signed-off-by Marek Wesołowski <[email protected]>--- dir.c | 6 ++++-- mst.c | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-)dir.c +4
-2@@ -338,7 +338,6 @@ descend_into_child_node: }memcpy_from_folio (kaddr, folio,0 ,PAGE_SIZE ); -post_read_mst_fixup ((struct ntfs_record *)kaddr,PAGE_SIZE );folio_unlock (folio);folio_put (folio);#else @@ -357,7 +356,6 @@ descend_into_child_node: goto unm_err_out; }memcpy (kaddr, (u8 *)page_address (page),PAGE_SIZE ); -post_read_mst_fixup ((struct ntfs_record *)kaddr,PAGE_SIZE );unlock_page (page);kunmap (page);put_page (page);@@ -380,6 +378,10 @@ fast_descend_into_child_node: vcn, dir_ni->mft_no); goto unm_err_out; } + err =post_read_mst_fixup ((struct ntfs_record *)ia, + dir_ni->itype.index.block_size); +if (err) + goto unm_err_out; err = ntfs_index_block_inconsistent(vol, ia, dir_ni->itype.index.block_size, vcn, COLLATION_FILE_NAME,mst.c +1
-1@@ -19,7 +19,7 @@* magic of the ntfs record header being processed with "BAAD" (in memory only!) * and abort processing. * -* Return 0 on success and -EINVAL on error ("BAAD" magic will be present). +* Return 0 on success and -EIO on error ("BAAD" magic will be present). * * NOTE: We consider the absence / invalidity of an update sequence array to * mean that the structure is not protected at all and hence doesn't need to
mst.c int post_read_mst_fixup(struct ntfs_record *b, const u32 size) · line 77
Quoted 8 lines
mst.c +1
-1@@ -71,7 +71,7 @@ int post_read_mst_fixup(struct ntfs_record *b, const u32 size)* Note that magic_BAAD is already converted to le32. */ b->magic = magic_BAAD; -return -EINVAL ; +return -EIO ; } data_pos +=NTFS_BLOCK_SIZE /sizeof (u16); }
Hi Marek,
Thank you for submitting the patch.
The fix itself looks reasonable, but this patch is based on the
standalone https://github.com/namjaejeon/linux-ntfs repository rather than the current kernel.org ntfs-next branch.
Please recreate the patch on top of the current kernel.org ntfs-next
branch and update the Fixes: tag to:
Thanks,
Baolin.
PATCH v2 1/1 ntfs: fix directory lookup on large-page systems
ntfs_lookup_inode_by_name() copies PAGE_SIZE bytes from the index allocation mapping and applies MST fixups to that entire buffer before selecting the requested INDX block.
When PAGE_SIZE is larger than the NTFS index block size, the USA count belongs to the smaller INDX block. post_read_mst_fixup() therefore treats the PAGE_SIZE call as not applicable and leaves the update sequence words in place. A directory entry crossing a sector boundary can then be returned by readdir but missed by lookup.
Select the INDX block first, apply the fixup using its actual block size, and propagate fixup failures before validating the block.
Return -EIO when post_read_mst_fixup() detects an incomplete multi sector transfer, since this indicates an on-disk metadata integrity error.
Reproduced on arm64 with 16 KiB pages and 4 KiB NTFS index blocks. The affected entry was visible in readdir but stat returned ENOENT. Both exact and case-insensitive lookup succeed after this change.
---
Changes in v2:
- Rebased onto the current kernel.org ntfs-next branch at 8809c3f29555.
- Updated the Fixes tag as requested.
fs/ntfs/dir.c | 5 ++++- fs/ntfs/mst.c | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-)
fs/ntfs/dir.c +4 -1
@@ -331,7 +331,6 @@ descend_into_child_node: }memcpy_from_folio (kaddr, folio,0 ,PAGE_SIZE ); -post_read_mst_fixup ((struct ntfs_record *)kaddr,PAGE_SIZE );folio_unlock (folio);folio_put (folio); fast_descend_into_child_node:
@@ -352,6 +351,10 @@ fast_descend_into_child_node: vcn, dir_ni->mft_no); goto unm_err_out; } + err =post_read_mst_fixup ((struct ntfs_record *)ia, + dir_ni->itype.index.block_size); +if (err) + goto unm_err_out; err = ntfs_index_block_inconsistent(vol, ia, dir_ni->itype.index.block_size, vcn, COLLATION_FILE_NAME,
fs/ntfs/mst.c +2 -2
@@ -19,7 +19,7 @@* magic of the ntfs record header being processed with "BAAD" (in memory only!) * and abort processing. * -* Return 0 on success and -EINVAL on error ("BAAD" magic will be present). +* Return 0 on success and -EIO on error ("BAAD" magic will be present). * * NOTE: We consider the absence / invalidity of an update sequence array to * mean that the structure is not protected at all and hence doesn't need to
@@ -71,7 +71,7 @@ int post_read_mst_fixup(struct ntfs_record *b, const u32 size)* Note that magic_BAAD is already converted to le32. */ b->magic = magic_BAAD; -return -EINVAL ; +return -EIO ; } data_pos +=NTFS_BLOCK_SIZE /sizeof (u16); }
base-commit: 8809c3f29555f9d1e66fe015080a8d4979d91eaa
Signature · 1 line
2.47.3
Quoted 44 lines · Marek Wesołowski
ntfs_lookup_inode_by_name()copiesPAGE_SIZEbytes from the index allocation mapping and applies MST fixups to that entire buffer before selecting the requested INDX block.When
PAGE_SIZEis larger than the NTFS index block size, the USA count belongs to the smaller INDX block.post_read_mst_fixup()therefore treats thePAGE_SIZEcall as not applicable and leaves the update sequence words in place. A directory entry crossing a sector boundary can then be returned by readdir but missed by lookup.Select the INDX block first, apply the fixup using its actual block size, and propagate fixup failures before validating the block.
Return
-EIOwhenpost_read_mst_fixup()detects an incomplete multi sector transfer, since this indicates an on-disk metadata integrity error.Reproduced on arm64 with 16 KiB pages and 4 KiB NTFS index blocks. The affected entry was visible in readdir but stat returned ENOENT. Both exact and case-insensitive lookup succeed after this change.
Fixes 0a8ac0c1fa0b ("ntfs: update directory operations")Signed-off-by Marek Wesołowski <[email protected]>---
Changes in v2:
- Rebased onto the current kernel.org ntfs-next branch at8809c3f29555.
- Updated the Fixes tag as requested.fs/ntfs/dir.c | 5 ++++- fs/ntfs/mst.c | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-)fs/ntfs/dir.c +0
-1@@ -331,7 +331,6 @@ descend_into_child_node: }memcpy_from_folio (kaddr, folio,0 ,PAGE_SIZE ); -post_read_mst_fixup ((struct ntfs_record *)kaddr,PAGE_SIZE );folio_unlock (folio);folio_put (folio); fast_descend_into_child_node:
fs/ntfs/dir.c fast_descend_into_child_node: · line 357
Quoted 7 lines
fs/ntfs/dir.c +4
-0@@ -352,6 +351,10 @@ fast_descend_into_child_node: vcn, dir_ni->mft_no); goto unm_err_out; } + err =post_read_mst_fixup ((struct ntfs_record *)ia, + dir_ni->itype.index.block_size); +if (err) + goto unm_err_out;
Hi Marek,
Thank you for providing this patch. I have a suggestion for this patch. post_read_mst_fixup() only logs the failure throughpr_err_ratelimited(); it does not invoke the configured errors= policy.
Previously, when it detected an incomplete multi-sector transfer, it
changed the record magic to BAAD. Its return value was ignored, so the following ntfs_index_block_inconsistent() call detected BAAD and called
ntfs_error(), which applied the configured errors= policy.
The early exit here skips that ntfs_error() path.
Could we call ntfs_error() here before jumping to the cleanup path?
For example:
if (err) {
ntfs_error(sb,
"MST fixup failed for index block vcn %lld in directory inode
0x%llx.",
vcn, dir_ni->mft_no);
goto unm_err_out;
}
Thanks,
Baolin.
Quoted 26 lines
fs/ntfs/dir.c +0
-0err = ntfs_index_block_inconsistent(vol, ia, dir_ni->itype.index.block_size, vcn, COLLATION_FILE_NAME,fs/ntfs/mst.c +2
-2@@ -19,7 +19,7 @@* magic of the ntfs record header being processed with "BAAD" (in memory only!) * and abort processing. * -* Return 0 on success and -EINVAL on error ("BAAD" magic will be present). +* Return 0 on success and -EIO on error ("BAAD" magic will be present). * * NOTE: We consider the absence / invalidity of an update sequence array to * mean that the structure is not protected at all and hence doesn't need to @@ -71,7 +71,7 @@ int post_read_mst_fixup(struct ntfs_record *b, const u32 size)* Note that magic_BAAD is already converted to le32. */ b->magic = magic_BAAD; -return -EINVAL ; +return -EIO ; } data_pos +=NTFS_BLOCK_SIZE /sizeof (u16); }base-commit:
8809c3f29555f9d1e66fe015080a8d4979d91eaa
PATCH v3 1/1 ntfs: fix directory lookup on large-page systems
ntfs_lookup_inode_by_name() copies PAGE_SIZE bytes from the index allocation mapping and applies MST fixups to that entire buffer before selecting the requested INDX block.
When PAGE_SIZE is larger than the NTFS index block size, the USA count belongs to the smaller INDX block. post_read_mst_fixup() therefore treats the PAGE_SIZE call as not applicable and leaves the update sequence words in place. A directory entry crossing a sector boundary can then be returned by readdir but missed by lookup.
Select the INDX block first, apply the fixup using its actual block size, and propagate fixup failures before validating the block. Report fixup failures through ntfs_error() so the configured errors= policy is applied.
Return -EIO when post_read_mst_fixup() detects an incomplete multi sector transfer, since this indicates an on-disk metadata integrity error.
Reproduced on arm64 with 16 KiB pages and 4 KiB NTFS index blocks. The affected entry was visible in readdir but stat returned ENOENT. Both exact and case-insensitive lookup succeed after this change.
---
Changes in v3:
- Rebased onto the current kernel.org ntfs-next branch at 65829d51fa81.
- Report MST fixup failures through ntfs_error() so the configured errors= policy is applied.
Changes in v2:
- Rebased onto the kernel.org ntfs-next branch.
- Updated the Fixes tag as requested.
fs/ntfs/dir.c | 9 ++++++++- fs/ntfs/mst.c | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-)
fs/ntfs/dir.c +8 -1
@@ -331,7 +331,6 @@ descend_into_child_node: }memcpy_from_folio (kaddr, folio,0 ,PAGE_SIZE ); -post_read_mst_fixup ((struct ntfs_record *)kaddr,PAGE_SIZE );folio_unlock (folio);folio_put (folio); fast_descend_into_child_node:
@@ -352,6 +351,14 @@ fast_descend_into_child_node: vcn, dir_ni->mft_no); goto unm_err_out; } + err =post_read_mst_fixup ((struct ntfs_record *)ia, + dir_ni->itype.index.block_size); +if (err) { +ntfs_error (sb, +"MST fixup failed for index block vcn %lld in directory inode 0x%llx." , + vcn, dir_ni->mft_no); + goto unm_err_out; + } err = ntfs_index_block_inconsistent(vol, ia, dir_ni->itype.index.block_size, vcn, COLLATION_FILE_NAME,
fs/ntfs/mst.c +2 -2
@@ -19,7 +19,7 @@* magic of the ntfs record header being processed with "BAAD" (in memory only!) * and abort processing. * -* Return 0 on success and -EINVAL on error ("BAAD" magic will be present). +* Return 0 on success and -EIO on error ("BAAD" magic will be present). * * NOTE: We consider the absence / invalidity of an update sequence array to * mean that the structure is not protected at all and hence doesn't need to
@@ -71,7 +71,7 @@ int post_read_mst_fixup(struct ntfs_record *b, const u32 size)* Note that magic_BAAD is already converted to le32. */ b->magic = magic_BAAD; -return -EINVAL ; +return -EIO ; } data_pos +=NTFS_BLOCK_SIZE /sizeof (u16); }
base-commit: 65829d51fa811ebcbf082c1e5d0cbecad2f9a836
Signature · 1 line
2.47.3
Quoted 87 lines · Marek Wesołowski
ntfs_lookup_inode_by_name()copiesPAGE_SIZEbytes from the index allocation mapping and applies MST fixups to that entire buffer before selecting the requested INDX block.When
PAGE_SIZEis larger than the NTFS index block size, the USA count belongs to the smaller INDX block.post_read_mst_fixup()therefore treats thePAGE_SIZEcall as not applicable and leaves the update sequence words in place. A directory entry crossing a sector boundary can then be returned by readdir but missed by lookup.Select the INDX block first, apply the fixup using its actual block size, and propagate fixup failures before validating the block. Report fixup failures through
ntfs_error()so the configured errors= policy is applied.Return
-EIOwhenpost_read_mst_fixup()detects an incomplete multi sector transfer, since this indicates an on-disk metadata integrity error.Reproduced on arm64 with 16 KiB pages and 4 KiB NTFS index blocks. The affected entry was visible in readdir but stat returned ENOENT. Both exact and case-insensitive lookup succeed after this change.
Fixes 0a8ac0c1fa0b ("ntfs: update directory operations")Signed-off-by Marek Wesołowski <[email protected]>---
Changes in v3:
- Rebased onto the current kernel.org ntfs-next branch at65829d51fa81.
- Report MST fixup failures throughntfs_error()so the configured errors= policy is applied.Changes in v2:
- Rebased onto the kernel.org ntfs-next branch.
- Updated the Fixes tag as requested.fs/ntfs/dir.c | 9 ++++++++- fs/ntfs/mst.c | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-)fs/ntfs/dir.c +8
-1@@ -331,7 +331,6 @@ descend_into_child_node: }memcpy_from_folio (kaddr, folio,0 ,PAGE_SIZE ); -post_read_mst_fixup ((struct ntfs_record *)kaddr,PAGE_SIZE );folio_unlock (folio);folio_put (folio); fast_descend_into_child_node:@@ -352,6 +351,14 @@ fast_descend_into_child_node: vcn, dir_ni->mft_no); goto unm_err_out; } + err =post_read_mst_fixup ((struct ntfs_record *)ia, + dir_ni->itype.index.block_size); +if (err) { +ntfs_error (sb, +"MST fixup failed for index block vcn %lld in directory inode 0x%llx." , + vcn, dir_ni->mft_no); + goto unm_err_out; + } err = ntfs_index_block_inconsistent(vol, ia, dir_ni->itype.index.block_size, vcn, COLLATION_FILE_NAME,fs/ntfs/mst.c +2
-2@@ -19,7 +19,7 @@* magic of the ntfs record header being processed with "BAAD" (in memory only!) * and abort processing. * -* Return 0 on success and -EINVAL on error ("BAAD" magic will be present). +* Return 0 on success and -EIO on error ("BAAD" magic will be present). * * NOTE: We consider the absence / invalidity of an update sequence array to * mean that the structure is not protected at all and hence doesn't need to @@ -71,7 +71,7 @@ int post_read_mst_fixup(struct ntfs_record *b, const u32 size)* Note that magic_BAAD is already converted to le32. */ b->magic = magic_BAAD; -return -EINVAL ; +return -EIO ; } data_pos +=NTFS_BLOCK_SIZE /sizeof (u16); }
Quoted 1 line
base-commit:
65829d51fa811ebcbf082c1e5d0cbecad2f9a836
Looks good to me.
2026년 9월 15일 (화) 오후 4:47, Marek Wesołowski <[email protected]>님이 작성:
Quoted 18 lines
ntfs_lookup_inode_by_name()copiesPAGE_SIZEbytes from the index allocation mapping and applies MST fixups to that entire buffer before selecting the requested INDX block.When
PAGE_SIZEis larger than the NTFS index block size, the USA count belongs to the smaller INDX block.post_read_mst_fixup()therefore treats thePAGE_SIZEcall as not applicable and leaves the update sequence words in place. A directory entry crossing a sector boundary can then be returned by readdir but missed by lookup.Select the INDX block first, apply the fixup using its actual block size, and propagate fixup failures before validating the block. Report fixup failures through
ntfs_error()so the configured errors= policy is applied.Return
-EIOwhenpost_read_mst_fixup()detects an incomplete multi sector transfer, since this indicates an on-disk metadata integrity error.
Quoted 6 lines
Reproduced on arm64 with 16 KiB pages and 4 KiB NTFS index blocks. The affected entry was visible in readdir but stat returned ENOENT. Both exact and case-insensitive lookup succeed after this change.
Fixes 0a8ac0c1fa0b ("ntfs: update directory operations")Signed-off-by Marek Wesołowski <[email protected]>
Looks good to me.
--
Thanks,
Hyunchul
Quoted 18 lines · Marek Wesołowski
ntfs_lookup_inode_by_name()copiesPAGE_SIZEbytes from the index allocation mapping and applies MST fixups to that entire buffer before selecting the requested INDX block.When
PAGE_SIZEis larger than the NTFS index block size, the USA count belongs to the smaller INDX block.post_read_mst_fixup()therefore treats thePAGE_SIZEcall as not applicable and leaves the update sequence words in place. A directory entry crossing a sector boundary can then be returned by readdir but missed by lookup.Select the INDX block first, apply the fixup using its actual block size, and propagate fixup failures before validating the block. Report fixup failures through
ntfs_error()so the configured errors= policy is applied.Return
-EIOwhenpost_read_mst_fixup()detects an incomplete multi sector transfer, since this indicates an on-disk metadata integrity error.
Quoted 6 lines
Reproduced on arm64 with 16 KiB pages and 4 KiB NTFS index blocks. The affected entry was visible in readdir but stat returned ENOENT. Both exact and case-insensitive lookup succeed after this change.
Fixes 0a8ac0c1fa0b ("ntfs: update directory operations")Signed-off-by Marek Wesołowski <[email protected]>
Applied it to #ntfs-next.
Thanks!