From: Showta Ishizaki <zakinko@snowrabbit.org>
To: Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>
Cc: Jan Kara <jack@suse.cz>,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
Showta Ishizaki <zakinko@snowrabbit.org>
Subject: [PATCH v2] ufs: mount filesystems on disks with 4K logical blocks
Date: Thu, 26 Nov 2026 00:26:47 +0900 [thread overview]
Message-ID: <20261126-ufs-06-v2@snowrabbit.org> (raw)
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, 1024, 2048 and 4096 byte sectors.
UFS1 and UFS2 images made on FreeBSD 15.0 with 4K fragments mount on all
of them, and ones made on NetBSD with 1K fragments on the first two.
The contents of all 5037 files match the SHA-256 recorded where the
image was made, and files written were the same after a remount. On
2048 and 4096 the NetBSD ones are refused with the new message. With
512 the results are the same with and without the patch.
fsck_ffs on FreeBSD finds nothing wrong in the UFS1 one written with
4096 byte sectors. 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.
Signed-off-by: Showta Ishizaki <zakinko@snowrabbit.org>
---
v2: round the superblock size up instead of taking the larger one. With
1024 byte logical blocks 1536 is not a multiple of the block size and
the mount failed without a message. With 512, 2048 and 4096 it gives
the same sizes as v1; fsck_ffs was run with v1.
v1: https://lore.kernel.org/linux-fsdevel/20261125112449.392FFC9314@bambi.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..4e1bb7578 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 = round_up(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 @@ static int ufs_fill_super(struct super_block *sb, struct fs_context *fc)
__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
reply other threads:[~2026-11-25 15:27 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261126-ufs-06-v2@snowrabbit.org \
--to=zakinko@snowrabbit.org \
--cc=brauner@kernel.org \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®