[PATCH v2] ntfs: mount read-only when mft records are smaller than the device block

Dennis Tighe posted 1 patch 1 month ago
fs/ntfs/super.c | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
[PATCH v2] ntfs: mount read-only when mft records are smaller than the device block
Posted by Dennis Tighe 1 month ago
An mft record is written with a single bio of exactly mft_record_size
bytes. bio_unaligned() rejects a bio whose size is not a multiple of the
device's logical block size, so on a volume whose mft records are smaller
than that block no mft record can be written at all.

On 512n and 512e drives this is a non-issue, since both report a 512-byte
logical block and a 1k record is a clean multiple of it. On 4Kn drives the
mft record (1k) is smaller than the 4k logical block, so every mft write
fails. At the same time, other writes (like directory index entries) that 
are 4k will succeed leading references to unwritten mft records and a 
corruption situation.

Reads are unaffected, so mount read-only rather than refusing outright,
and refuse a later remount read-write for the same reason.

This is a stopgap. Writing these volumes needs read-modify-write at the
device's block size, which this does not implement.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Dennis Tighe <dennis.tighe@gmail.com>
---
Changes in v2:
  * Add the same check to ntfs_reconfigure(), so a later
    "mount -o remount,rw" is refused rather than silently re-enabling the
    writes. Thanks to Hyunchul Lee for catching this.

 fs/ntfs/super.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/fs/ntfs/super.c b/fs/ntfs/super.c
index 30481e5d5dd4..5de07fab101c 100644
--- a/fs/ntfs/super.c
+++ b/fs/ntfs/super.c
@@ -304,6 +304,12 @@ static int ntfs_reconfigure(struct fs_context *fc)
 					le16_to_cpu(vol->vol_flags), es);
 			return -EROFS;
 		}
+		if (vol->mft_record_size < bdev_logical_block_size(sb->s_bdev)) {
+			ntfs_error(sb, "mft record size (%i) is below the device block size (%u)%s",
+					vol->mft_record_size,
+					bdev_logical_block_size(sb->s_bdev), es);
+			return -EROFS;
+		}
 		if (vol->logfile_ino && !ntfs_empty_logfile(vol->logfile_ino)) {
 			ntfs_error(sb, "Failed to empty journal LogFile%s",
 					es);
@@ -2298,6 +2304,22 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
 		ntfs_debug("Changed device block size to %i bytes (block size bits %i) to match volume sector size.",
 				blocksize, sb->s_blocksize_bits);
 	}
+
+	/*
+	 * If the device's logical block size is larger than the mft record (1k)
+	 * writes to it will currently fail. Reads are unaffected, so we can still
+	 * mount the volume as read-only.
+	 */
+	if (vol->mft_record_size < bdev_logical_block_size(sb->s_bdev)) {
+		if (!sb_rdonly(sb)) {
+			ntfs_error(sb,
+				   "mft record size (%i) is smaller than the device logical block size (%u).  Mft records cannot be written.  Mounting read-only.",
+				   vol->mft_record_size,
+				   bdev_logical_block_size(sb->s_bdev));
+			sb->s_flags |= SB_RDONLY;
+		}
+	}
+
 	/* Initialize the cluster and mft allocators. */
 	ntfs_setup_allocators(vol);
 	/* Setup remaining fields in the super block. */
-- 
2.43.0
Re: [PATCH v2] ntfs: mount read-only when mft records are smaller than the device block
Posted by Hyunchul Lee 1 month ago
2026년 8월 24일 (월) 오후 11:42, Dennis Tighe <dennis.tighe@gmail.com>님이 작성:
>
> An mft record is written with a single bio of exactly mft_record_size
> bytes. bio_unaligned() rejects a bio whose size is not a multiple of the
> device's logical block size, so on a volume whose mft records are smaller
> than that block no mft record can be written at all.
>
> On 512n and 512e drives this is a non-issue, since both report a 512-byte
> logical block and a 1k record is a clean multiple of it. On 4Kn drives the
> mft record (1k) is smaller than the 4k logical block, so every mft write
> fails. At the same time, other writes (like directory index entries) that
> are 4k will succeed leading references to unwritten mft records and a
> corruption situation.
>
> Reads are unaffected, so mount read-only rather than refusing outright,
> and refuse a later remount read-write for the same reason.
>
> This is a stopgap. Writing these volumes needs read-modify-write at the
> device's block size, which this does not implement.
>
> Assisted-by: Claude:claude-opus-5
> Signed-off-by: Dennis Tighe <dennis.tighe@gmail.com>

Looks good to me.

Reviewed-by: Hyunchul Lee <hyc.lee@gmail.com>

-- 
Thanks,
Hyunchul