Skip to content

Commit 38ea50d

Browse files
committed
ext4: support direct I/O with fscrypt using blk-crypto
Encrypted files traditionally haven't supported DIO, due to the need to encrypt/decrypt the data. However, when the encryption is implemented using inline encryption (blk-crypto) instead of the traditional filesystem-layer encryption, it is straightforward to support DIO. Therefore, make ext4 support DIO on files that are using inline encryption. Since ext4 uses iomap for DIO, and fscrypt support was already added to iomap DIO, this just requires two small changes: - Let DIO proceed when supported, by checking fscrypt_dio_supported() instead of assuming that encrypted files never support DIO. - In ext4_iomap_begin(), use fscrypt_limit_io_blocks() to limit the length of the mapping in the rare case where a DUN discontiguity occurs in the middle of an extent. The iomap DIO implementation requires this, since it assumes that it can submit a bio covering (up to) the whole mapping, without checking fscrypt constraints itself. Co-developed-by: Satya Tangirala <[email protected]> Signed-off-by: Satya Tangirala <[email protected]> Acked-by: Theodore Ts'o <[email protected]> Reviewed-by: Jaegeuk Kim <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Eric Biggers <[email protected]>
1 parent 489734e commit 38ea50d

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

fs/ext4/file.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@
3636
#include "acl.h"
3737
#include "truncate.h"
3838

39-
static bool ext4_dio_supported(struct inode *inode)
39+
static bool ext4_dio_supported(struct kiocb *iocb, struct iov_iter *iter)
4040
{
41-
if (IS_ENABLED(CONFIG_FS_ENCRYPTION) && IS_ENCRYPTED(inode))
41+
struct inode *inode = file_inode(iocb->ki_filp);
42+
43+
if (!fscrypt_dio_supported(iocb, iter))
4244
return false;
4345
if (fsverity_active(inode))
4446
return false;
@@ -61,7 +63,7 @@ static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
6163
inode_lock_shared(inode);
6264
}
6365

64-
if (!ext4_dio_supported(inode)) {
66+
if (!ext4_dio_supported(iocb, to)) {
6567
inode_unlock_shared(inode);
6668
/*
6769
* Fallback to buffered I/O if the operation being performed on
@@ -509,7 +511,7 @@ static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
509511
}
510512

511513
/* Fallback to buffered I/O if the inode does not support direct I/O. */
512-
if (!ext4_dio_supported(inode)) {
514+
if (!ext4_dio_supported(iocb, from)) {
513515
if (ilock_shared)
514516
inode_unlock_shared(inode);
515517
else

fs/ext4/inode.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3409,6 +3409,13 @@ static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
34093409
if (ret < 0)
34103410
return ret;
34113411
out:
3412+
/*
3413+
* When inline encryption is enabled, sometimes I/O to an encrypted file
3414+
* has to be broken up to guarantee DUN contiguity. Handle this by
3415+
* limiting the length of the mapping returned.
3416+
*/
3417+
map.m_len = fscrypt_limit_io_blocks(inode, map.m_lblk, map.m_len);
3418+
34123419
ext4_set_iomap(inode, iomap, &map, offset, length, flags);
34133420

34143421
return 0;

0 commit comments

Comments
 (0)