* [PATCH] ufs: mount filesystems on disks with 4K logical blocks
@ 2026-11-25 11:20 Showta Ishizaki
0 siblings, 0 replies; only message in thread
From: Showta Ishizaki @ 2026-11-25 11:20 UTC (permalink / raw)
To: Alexander Viro, Christian Brauner
Cc: Jan Kara, linux-fsdevel, linux-kernel, Showta Ishizaki
A UFS made by FreeBSD with its default 4K fragments does not mount when
the disk has 4K logical blocks:
# losetup --sector-size 4096 /dev/loop0 ufs2.img
# mount -t ufs -o ro,ufstype=ufs2 /dev/loop0 /mnt
fails, and the kernel logs "ufs: failed to set blocksize".
ufs_fill_super() starts from the block size each flavour usually has,
512 bytes for 44bsd and ufs2, only to read the superblock, and
sb_set_blocksize() cannot go below the logical block size of the
device. Start from the logical block size where it is the larger of
the two. The fragment size in the superblock takes over afterwards, as
it did before.
A filesystem whose fragments are smaller than a logical block still
cannot be used on such a disk. Say so, instead of failing to set the
block size.
I tried it with loop devices of 512 and 4096 byte sectors. With 4096,
UFS1 and UFS2 images made on FreeBSD 15.0 now mount, and the contents of
all 5037 files match the SHA-256 recorded on FreeBSD. Files written
there were the same after a remount. fsck_ffs on FreeBSD finds nothing
wrong in the UFS1 one. In the UFS2 one it reports the check hashes,
which Linux does not keep, and the cylinder group it rebuilds for that;
it reports the same for one written with 512 byte sectors without the
patch. Images with 1K fragments from NetBSD are refused with the new
message. With 512 the results are the same with and without the patch.
Signed-off-by: Showta Ishizaki <zakinko@snowrabbit.org>
---
fs/ufs/super.c | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/fs/ufs/super.c b/fs/ufs/super.c
index 3569ac92b..da7520b5f 100644
--- a/fs/ufs/super.c
+++ b/fs/ufs/super.c
@@ -725,6 +725,7 @@ static int ufs_fill_super(struct super_block *sb, struct fs_context *fc)
struct ufs_buffer_head * ubh;
struct inode *inode;
unsigned block_size, super_block_size;
+ unsigned int logical_size;
unsigned flags;
unsigned super_block_offset;
unsigned maxsymlen;
@@ -922,6 +923,25 @@ static int ufs_fill_super(struct super_block *sb, struct fs_context *fc)
goto failed;
}
+ /*
+ * The sizes above are what each flavour usually has, and are only
+ * used to read the superblock. The buffer cache cannot use a block
+ * smaller than the device's logical block, so on a disk with 4K
+ * logical blocks they could not even do that, and a filesystem with
+ * 4K fragments was refused before its superblock was looked at.
+ * Read it in logical blocks instead; the fragment size it gives then
+ * takes over below, as before.
+ */
+ logical_size = bdev_logical_block_size(sb->s_bdev);
+ if (block_size < logical_size) {
+ block_size = logical_size;
+ super_block_size = max(super_block_size, logical_size);
+ uspi->s_fsize = block_size;
+ uspi->s_fmask = ~(block_size - 1);
+ uspi->s_fshift = (unsigned int)ilog2(block_size);
+ uspi->s_sbsize = super_block_size;
+ }
+
again:
if (!sb_set_blocksize(sb, block_size)) {
pr_err("failed to set blocksize\n");
@@ -1033,6 +1053,11 @@ magic_found:
__func__, uspi->s_bsize / uspi->s_fsize);
goto failed;
}
+ if (uspi->s_fsize < logical_size) {
+ pr_err("%s(): fragment size %u is smaller than the device's logical block size %u\n",
+ __func__, uspi->s_fsize, logical_size);
+ goto failed;
+ }
if (uspi->s_fsize != block_size || uspi->s_sbsize != super_block_size) {
ubh_brelse_uspi(uspi);
ubh = NULL;
base-commit: 4dce97c6c265544342e716e62fd24bbb232590c5
--
2.54.0 (Apple Git-157)
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-25 11:24 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 11:20 [PATCH] ufs: mount filesystems on disks with 4K logical blocks Showta Ishizaki
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®