* [PATCH] ntfs: fix sector conversion on 4Kn devices
@ 2026-09-03 12:44 朱 天浩
2026-09-04 1:32 ` liubaolin
0 siblings, 1 reply; 3+ messages in thread
From: 朱 天浩 @ 2026-09-03 12:44 UTC (permalink / raw)
To: linkinjeon, hyc.lee; +Cc: ntfs, linux-kernel, 朱 天浩
The NTFS driver was converting byte offsets to bio sector numbers using
the filesystem block size (sb->s_blocksize_bits). However, block layer
sector numbers - bio->bi_iter.bi_sector and sector_t - are always in
units of 512 bytes, independent of the filesystem block size.
On a device with a 4096 byte logical block size (4Kn), sb_min_blocksize()
lifts s_blocksize to 4096, so every MFT writeback bio built by
ntfs_sync_mft_mirror(), write_mft_record_nolock() and
ntfs_mft_writepages() ended up with a sector number eight times too
small. The resulting LBA is not only wrong, it is usually not a
multiple of the logical block size either, and some controllers reject
the write outright, e.g.:
ntfs: (device nvme0n1p3): ntfs_sync_mft_mirror(): I/O error while
writing mft mirror record 0x3!
ntfs: (device nvme0n1p3): write_mft_record_nolock(): I/O error while
writing mft record 0x3! Marking base inode as bad.
Reproduced on an Apple SSD AP0256J (4Kn) with a volume using 4096 byte
sectors, 4096 byte clusters and clusters_per_mft_record = 1; after the
fix a 13 MB file written to the volume is byte-for-byte identical
after a umount/mount cycle.
Fixes: 40796051991d ("ntfs: update in-memory, on-disk structures and headers")
Signed-off-by: Zhu Tianhao <zhutianhao75@hotmail.com>
---
fs/ntfs/ntfs.h | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)
diff --git a/fs/ntfs/ntfs.h b/fs/ntfs/ntfs.h
index df5a75d506f6..b66ce62f67c9 100644
--- a/fs/ntfs/ntfs.h
+++ b/fs/ntfs/ntfs.h
@@ -20,6 +20,7 @@
#include <linux/smp.h>
#include <linux/pagemap.h>
#include <linux/uidgid.h>
+#include <linux/blkdev.h> /* For SECTOR_SHIFT. */
#include "volume.h"
#include "layout.h"
@@ -71,7 +72,11 @@
#define NTFS_CLU_TO_POFS(vol, clu) (((u64)(clu) << (vol)->cluster_size_bits) & \
~PAGE_MASK)
-#define NTFS_B_TO_SECTOR(vol, b) ((b) >> ((vol)->sb)->s_blocksize_bits)
+/*
+ * bio->bi_iter.bi_sector and sector_t are always in 512-byte sectors, even
+ * when the filesystem block size is larger (for example 4KiB).
+ */
+#define NTFS_B_TO_SECTOR(vol, b) ntfs_bytes_to_sector(vol, b)
enum {
NTFS_BLOCK_SIZE = 512,
@@ -154,11 +159,15 @@ static inline u64 ntfs_cluster_to_poff(const struct ntfs_volume *vol,
return (clu << vol->cluster_size_bits) & ~PAGE_MASK;
}
-/* Convert byte offset to sector (block) number. */
+/* Convert byte offset to a block layer sector number. Those are always in
+ * units of 512 bytes, never in units of sb->s_blocksize, which is raised to
+ * the device logical block size on 4Kn devices.
+ */
static inline sector_t ntfs_bytes_to_sector(const struct ntfs_volume *vol,
u64 bytes)
{
- return bytes >> vol->sb->s_blocksize_bits;
+ (void)vol;
+ return bytes >> SECTOR_SHIFT;
}
/* Global variables. */
--
2.51.0
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] ntfs: fix sector conversion on 4Kn devices
2026-09-03 12:44 [PATCH] ntfs: fix sector conversion on 4Kn devices 朱 天浩
@ 2026-09-04 1:32 ` liubaolin
2026-09-04 16:02 ` 回复: " 朱 天浩
0 siblings, 1 reply; 3+ messages in thread
From: liubaolin @ 2026-09-04 1:32 UTC (permalink / raw)
To: 朱 天浩, linkinjeon, hyc.lee; +Cc: ntfs, linux-kernel
在 2026/9/3 20:44, 朱 天浩 写道:
> The NTFS driver was converting byte offsets to bio sector numbers using
> the filesystem block size (sb->s_blocksize_bits). However, block layer
> sector numbers - bio->bi_iter.bi_sector and sector_t - are always in
> units of 512 bytes, independent of the filesystem block size.
>
> On a device with a 4096 byte logical block size (4Kn), sb_min_blocksize()
> lifts s_blocksize to 4096, so every MFT writeback bio built by
> ntfs_sync_mft_mirror(), write_mft_record_nolock() and
> ntfs_mft_writepages() ended up with a sector number eight times too
> small. The resulting LBA is not only wrong, it is usually not a
> multiple of the logical block size either, and some controllers reject
> the write outright, e.g.:
>
> ntfs: (device nvme0n1p3): ntfs_sync_mft_mirror(): I/O error while
> writing mft mirror record 0x3!
> ntfs: (device nvme0n1p3): write_mft_record_nolock(): I/O error while
> writing mft record 0x3! Marking base inode as bad.
>
> Reproduced on an Apple SSD AP0256J (4Kn) with a volume using 4096 byte
> sectors, 4096 byte clusters and clusters_per_mft_record = 1; after the
> fix a 13 MB file written to the volume is byte-for-byte identical
> after a umount/mount cycle.
>
> Fixes: 40796051991d ("ntfs: update in-memory, on-disk structures and headers")
> Signed-off-by: Zhu Tianhao <zhutianhao75@hotmail.com>
> ---
> fs/ntfs/ntfs.h | 15 ++++++++++++---
> 1 file changed, 12 insertions(+), 3 deletions(-)
>
> diff --git a/fs/ntfs/ntfs.h b/fs/ntfs/ntfs.h
> index df5a75d506f6..b66ce62f67c9 100644
> --- a/fs/ntfs/ntfs.h
> +++ b/fs/ntfs/ntfs.h
> @@ -20,6 +20,7 @@
> #include <linux/smp.h>
> #include <linux/pagemap.h>
> #include <linux/uidgid.h>
> +#include <linux/blkdev.h> /* For SECTOR_SHIFT. */
>
> #include "volume.h"
> #include "layout.h"
> @@ -71,7 +72,11 @@
> #define NTFS_CLU_TO_POFS(vol, clu) (((u64)(clu) << (vol)->cluster_size_bits) & \
> ~PAGE_MASK)
>
> -#define NTFS_B_TO_SECTOR(vol, b) ((b) >> ((vol)->sb)->s_blocksize_bits)
> +/*
> + * bio->bi_iter.bi_sector and sector_t are always in 512-byte sectors, even
> + * when the filesystem block size is larger (for example 4KiB).
> + */
> +#define NTFS_B_TO_SECTOR(vol, b) ntfs_bytes_to_sector(vol, b)
>
> enum {
> NTFS_BLOCK_SIZE = 512,
> @@ -154,11 +159,15 @@ static inline u64 ntfs_cluster_to_poff(const struct ntfs_volume *vol,
> return (clu << vol->cluster_size_bits) & ~PAGE_MASK;
> }
>
> -/* Convert byte offset to sector (block) number. */
> +/* Convert byte offset to a block layer sector number. Those are always in
> + * units of 512 bytes, never in units of sb->s_blocksize, which is raised to
> + * the device logical block size on 4Kn devices.
> + */
> static inline sector_t ntfs_bytes_to_sector(const struct ntfs_volume *vol,
> u64 bytes)
> {
> - return bytes >> vol->sb->s_blocksize_bits;
> + (void)vol;
> + return bytes >> SECTOR_SHIFT;
> }
>
> /* Global variables. */
Hi Tianhao,
Thank you for submitting this patch.
This issue has already been fixed by commit 6faa235a649e ("ntfs:
compute bi_sector in 512-byte units"), which is included in the current
ntfs-next branch. The existing fix covers the same MFT write paths, so
this patch does not need to be applied again.
Thank you for your work.
Thanks,
Baolin.
^ permalink raw reply [flat|nested] 3+ messages in thread* 回复: [PATCH] ntfs: fix sector conversion on 4Kn devices
2026-09-04 1:32 ` liubaolin
@ 2026-09-04 16:02 ` 朱 天浩
0 siblings, 0 replies; 3+ messages in thread
From: 朱 天浩 @ 2026-09-04 16:02 UTC (permalink / raw)
To: liubaolin, linkinjeon, hyc.lee; +Cc: ntfs, linux-kernel
Hi Baolin,
Thanks for your reply. Commit 6faa235a649e solves the same problem I encountered.
I did test it on MacBookPro14,1: an Apple SSD AP0256J (4Kn), with a volume
using 4096 byte sectors and clusters. Before the fix, every MFT writeback
fails with -EIO; with 6faa235a649e applied on top of Deepin's customized
6.18.38-amd64-desktop-rolling kernel (where this module is backported),
writing a 13 MB file succeeds and the file is byte-for-byte identical after a
umount/mount cycle. So for that commit, feel free to add:
Tested-by: Zhu Tianhao <zhutianhao75@hotmail.com>
Thanks,
Tianhao
________________________________________
发件人: liubaolin <liubaolin12138@163.com>
发送时间: 2026年9月4日 9:32
收件人: 朱 天浩; linkinjeon@kernel.org; hyc.lee@gmail.com
抄送: ntfs@lists.linux.dev; linux-kernel@vger.kernel.org
主题: Re: [PATCH] ntfs: fix sector conversion on 4Kn devices
在 2026/9/3 20:44, 朱 天浩 写道:
> The NTFS driver was converting byte offsets to bio sector numbers using
> the filesystem block size (sb->s_blocksize_bits). However, block layer
> sector numbers - bio->bi_iter.bi_sector and sector_t - are always in
> units of 512 bytes, independent of the filesystem block size.
>
> On a device with a 4096 byte logical block size (4Kn), sb_min_blocksize()
> lifts s_blocksize to 4096, so every MFT writeback bio built by
> ntfs_sync_mft_mirror(), write_mft_record_nolock() and
> ntfs_mft_writepages() ended up with a sector number eight times too
> small. The resulting LBA is not only wrong, it is usually not a
> multiple of the logical block size either, and some controllers reject
> the write outright, e.g.:
>
> ntfs: (device nvme0n1p3): ntfs_sync_mft_mirror(): I/O error while
> writing mft mirror record 0x3!
> ntfs: (device nvme0n1p3): write_mft_record_nolock(): I/O error while
> writing mft record 0x3! Marking base inode as bad.
>
> Reproduced on an Apple SSD AP0256J (4Kn) with a volume using 4096 byte
> sectors, 4096 byte clusters and clusters_per_mft_record = 1; after the
> fix a 13 MB file written to the volume is byte-for-byte identical
> after a umount/mount cycle.
>
> Fixes: 40796051991d ("ntfs: update in-memory, on-disk structures and headers")
> Signed-off-by: Zhu Tianhao <zhutianhao75@hotmail.com>
> ---
> fs/ntfs/ntfs.h | 15 ++++++++++++---
> 1 file changed, 12 insertions(+), 3 deletions(-)
>
> diff --git a/fs/ntfs/ntfs.h b/fs/ntfs/ntfs.h
> index df5a75d506f6..b66ce62f67c9 100644
> --- a/fs/ntfs/ntfs.h
> +++ b/fs/ntfs/ntfs.h
> @@ -20,6 +20,7 @@
> #include <linux/smp.h>
> #include <linux/pagemap.h>
> #include <linux/uidgid.h>
> +#include <linux/blkdev.h> /* For SECTOR_SHIFT. */
>
> #include "volume.h"
> #include "layout.h"
> @@ -71,7 +72,11 @@
> #define NTFS_CLU_TO_POFS(vol, clu) (((u64)(clu) << (vol)->cluster_size_bits) & \
> ~PAGE_MASK)
>
> -#define NTFS_B_TO_SECTOR(vol, b) ((b) >> ((vol)->sb)->s_blocksize_bits)
> +/*
> + * bio->bi_iter.bi_sector and sector_t are always in 512-byte sectors, even
> + * when the filesystem block size is larger (for example 4KiB).
> + */
> +#define NTFS_B_TO_SECTOR(vol, b) ntfs_bytes_to_sector(vol, b)
>
> enum {
> NTFS_BLOCK_SIZE = 512,
> @@ -154,11 +159,15 @@ static inline u64 ntfs_cluster_to_poff(const struct ntfs_volume *vol,
> return (clu << vol->cluster_size_bits) & ~PAGE_MASK;
> }
>
> -/* Convert byte offset to sector (block) number. */
> +/* Convert byte offset to a block layer sector number. Those are always in
> + * units of 512 bytes, never in units of sb->s_blocksize, which is raised to
> + * the device logical block size on 4Kn devices.
> + */
> static inline sector_t ntfs_bytes_to_sector(const struct ntfs_volume *vol,
> u64 bytes)
> {
> - return bytes >> vol->sb->s_blocksize_bits;
> + (void)vol;
> + return bytes >> SECTOR_SHIFT;
> }
>
> /* Global variables. */
Hi Tianhao,
Thank you for submitting this patch.
This issue has already been fixed by commit 6faa235a649e ("ntfs:
compute bi_sector in 512-byte units"), which is included in the current
ntfs-next branch. The existing fix covers the same MFT write paths, so
this patch does not need to be applied again.
Thank you for your work.
Thanks,
Baolin.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-09-04 16:02 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-03 12:44 [PATCH] ntfs: fix sector conversion on 4Kn devices 朱 天浩
2026-09-04 1:32 ` liubaolin
2026-09-04 16:02 ` 回复: " 朱 天浩
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®