From: Baokun Li <libaokun@linux.alibaba.com>
To: linux-ext4@vger.kernel.org
Cc: linux-fscrypt@vger.kernel.org, linux-block@vger.kernel.org,
linux-fsdevel@vger.kernel.org, linux-xfs@vger.kernel.org,
ebiggers@kernel.org, jaegeuk@kernel.org, axboe@kernel.dk,
hch@lst.de, djwong@kernel.org, brauner@kernel.org, tytso@mit.edu,
berto@igalia.com, adilger.kernel@dilger.ca, jack@suse.cz,
yi.zhang@huawei.com, ojaswin@linux.ibm.com,
ritesh.list@gmail.com
Subject: [PATCH 07/13] iomap: align the direct I/O bios of encrypted files to the block size
Date: Mon, 12 Oct 2026 01:09:52 +0800 [thread overview]
Message-ID: <20261011170958.1759230-8-libaokun@linux.alibaba.com> (raw)
In-Reply-To: <20261011170958.1759230-1-libaokun@linux.alibaba.com>
A bio that carries an encryption context must cover a whole number of
data units: its data unit number is generated from its first data unit
(see bio_crypt_set_ctx()), and it is only split at data unit
boundaries.
iomap attaches the encryption context of an encrypted file to the bios
that it builds for direct I/O, but it only cuts them at the logical
block size of the device. A bio ends where the bio_vecs that fit into
it end, not at a fixed number of bytes: the extraction stops when the
bio is out of bio_vecs or when the iterator is exhausted, and a
bio_vec can cover a whole folio.
A bio can end in the middle of a data unit, and a data unit that is
larger than a page makes this reachable without any misaligned input
at all: the extraction then stops in the middle of a folio, and a cut
at a page granularity inside such a data unit is not data unit aligned
even when the position, the length and the buffer are.
The fallback rejects such a bio once it reaches the block layer: one
of its bio_vecs is not data unit aligned, so a direct I/O that should
work fails.
By contrast, the equivalent check on the hardware path lives in the
splitting code, which a small single segment bio skips, so a bio that
starts in the middle of a data unit can reach the hardware without
being rejected.
Fix this by cutting the bios of an encrypted file at the block size,
like the IOMAP_DIO_FSBLOCK_ALIGNED flag does for the filesystems that
pass it. fscrypt rejects a data unit size larger than the block size,
so a bio that is cut at block size boundaries covers a whole number of
data units.
This also makes the file systems that don't pass the flag follow the
contract, e.g. f2fs, which passes direct I/O that is not aligned to
the logical block size of the device on to iomap expecting it to fail
with -EINVAL, while iomap checks only the file position and the
length, so such a request can reach bio submission.
Signed-off-by: Baokun Li <libaokun@linux.alibaba.com>
---
fs/iomap/direct-io.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/fs/iomap/direct-io.c b/fs/iomap/direct-io.c
index 8b4039d16ce8..9b6cdf04dced 100644
--- a/fs/iomap/direct-io.c
+++ b/fs/iomap/direct-io.c
@@ -410,6 +410,16 @@ static inline unsigned int iomap_dio_alignment(struct inode *inode,
{
if (dio_flags & IOMAP_DIO_FSBLOCK_ALIGNED)
return i_blocksize(inode);
+
+ /*
+ * A bio of an encrypted file must cover a whole number of data
+ * units. The data unit size of an encryption policy never exceeds
+ * the block size, so cutting the bios at block size boundaries is
+ * enough.
+ */
+ if (IS_ENCRYPTED(inode))
+ return i_blocksize(inode);
+
return bdev_logical_block_size(bdev);
}
--
2.43.7
next prev parent reply other threads:[~2026-10-11 17:10 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-11 17:09 [PATCH 00/13] block, ext4: allow encryption with a block size larger than the page size Baokun Li
2026-10-11 17:09 ` [PATCH 01/13] ext4: refuse encryption when block size > " Baokun Li
2026-10-11 17:26 ` sashiko-bot
2026-10-11 17:09 ` [PATCH 02/13] ext4: reject encrypted inodes " Baokun Li
2026-10-11 17:16 ` sashiko-bot
2026-10-11 17:09 ` [PATCH 03/13] blk-crypto: reject fallback data unit sizes it can't handle Baokun Li
2026-10-11 17:20 ` sashiko-bot
2026-10-12 7:50 ` Baokun Li
2026-10-11 17:09 ` [PATCH 04/13] blk-crypto: decrypt one data unit per crypto request Baokun Li
2026-10-11 17:19 ` sashiko-bot
2026-10-11 17:09 ` [PATCH 05/13] blk-crypto: encrypt " Baokun Li
2026-10-11 17:26 ` sashiko-bot
2026-10-11 17:09 ` [PATCH 06/13] blk-crypto: support data units larger than a page Baokun Li
2026-10-11 17:09 ` Baokun Li [this message]
2026-10-11 17:25 ` [PATCH 07/13] iomap: align the direct I/O bios of encrypted files to the block size sashiko-bot
2026-10-11 17:09 ` [PATCH 08/13] block: split encrypted bios at data unit boundaries Baokun Li
2026-10-11 17:20 ` sashiko-bot
2026-10-11 17:09 ` [PATCH 09/13] block: warn about bios that can't be split Baokun Li
2026-10-11 17:18 ` sashiko-bot
2026-10-11 17:09 ` [PATCH 10/13] block: don't require data unit aligned bio_vecs for encrypted bios Baokun Li
2026-10-11 17:24 ` sashiko-bot
2026-10-12 8:09 ` Baokun Li
2026-10-11 17:09 ` [PATCH 11/13] ext4: allow encryption with a block size larger than the page size Baokun Li
2026-10-11 17:30 ` sashiko-bot
2026-10-11 17:09 ` [PATCH 12/13] ext4: send folios that cover a wraparound to the buffer_head path Baokun Li
2026-10-11 17:22 ` sashiko-bot
2026-10-11 17:09 ` [PATCH 13/13] ext4: allow large folios for encrypted files Baokun Li
2026-10-11 17:23 ` sashiko-bot
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=20261011170958.1759230-8-libaokun@linux.alibaba.com \
--to=libaokun@linux.alibaba.com \
--cc=adilger.kernel@dilger.ca \
--cc=axboe@kernel.dk \
--cc=berto@igalia.com \
--cc=brauner@kernel.org \
--cc=djwong@kernel.org \
--cc=ebiggers@kernel.org \
--cc=hch@lst.de \
--cc=jack@suse.cz \
--cc=jaegeuk@kernel.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-fscrypt@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-xfs@vger.kernel.org \
--cc=ojaswin@linux.ibm.com \
--cc=ritesh.list@gmail.com \
--cc=tytso@mit.edu \
--cc=yi.zhang@huawei.com \
/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