Skip to content

Commit 881b568

Browse files
committed
Merge tag 'fscrypt-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/fscrypt
Pull fscrypt updates from Eric Biggers: "Add support for direct I/O on encrypted files when blk-crypto (inline encryption) is being used for file contents encryption" * tag 'fscrypt-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/fscrypt: fscrypt: update documentation for direct I/O support f2fs: support direct I/O with fscrypt using blk-crypto ext4: support direct I/O with fscrypt using blk-crypto iomap: support direct I/O with fscrypt using blk-crypto fscrypt: add functions for direct I/O support
2 parents 0313bc2 + cdaa1b1 commit 881b568

File tree

9 files changed

+173
-7
lines changed

9 files changed

+173
-7
lines changed

Documentation/filesystems/fscrypt.rst

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,8 +1047,8 @@ astute users may notice some differences in behavior:
10471047
may be used to overwrite the source files but isn't guaranteed to be
10481048
effective on all filesystems and storage devices.
10491049

1050-
- Direct I/O is not supported on encrypted files. Attempts to use
1051-
direct I/O on such files will fall back to buffered I/O.
1050+
- Direct I/O is supported on encrypted files only under some
1051+
circumstances. For details, see `Direct I/O support`_.
10521052

10531053
- The fallocate operations FALLOC_FL_COLLAPSE_RANGE and
10541054
FALLOC_FL_INSERT_RANGE are not supported on encrypted files and will
@@ -1179,6 +1179,27 @@ Inline encryption doesn't affect the ciphertext or other aspects of
11791179
the on-disk format, so users may freely switch back and forth between
11801180
using "inlinecrypt" and not using "inlinecrypt".
11811181

1182+
Direct I/O support
1183+
==================
1184+
1185+
For direct I/O on an encrypted file to work, the following conditions
1186+
must be met (in addition to the conditions for direct I/O on an
1187+
unencrypted file):
1188+
1189+
* The file must be using inline encryption. Usually this means that
1190+
the filesystem must be mounted with ``-o inlinecrypt`` and inline
1191+
encryption hardware must be present. However, a software fallback
1192+
is also available. For details, see `Inline encryption support`_.
1193+
1194+
* The I/O request must be fully aligned to the filesystem block size.
1195+
This means that the file position the I/O is targeting, the lengths
1196+
of all I/O segments, and the memory addresses of all I/O buffers
1197+
must be multiples of this value. Note that the filesystem block
1198+
size may be greater than the logical block size of the block device.
1199+
1200+
If either of the above conditions is not met, then direct I/O on the
1201+
encrypted file will fall back to buffered I/O.
1202+
11821203
Implementation details
11831204
======================
11841205

fs/crypto/crypto.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ void fscrypt_free_bounce_page(struct page *bounce_page)
6969
}
7070
EXPORT_SYMBOL(fscrypt_free_bounce_page);
7171

72+
/*
73+
* Generate the IV for the given logical block number within the given file.
74+
* For filenames encryption, lblk_num == 0.
75+
*
76+
* Keep this in sync with fscrypt_limit_io_blocks(). fscrypt_limit_io_blocks()
77+
* needs to know about any IV generation methods where the low bits of IV don't
78+
* simply contain the lblk_num (e.g., IV_INO_LBLK_32).
79+
*/
7280
void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num,
7381
const struct fscrypt_info *ci)
7482
{

fs/crypto/inline_crypt.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/buffer_head.h>
1818
#include <linux/sched/mm.h>
1919
#include <linux/slab.h>
20+
#include <linux/uio.h>
2021

2122
#include "fscrypt_private.h"
2223

@@ -315,6 +316,10 @@ EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx_bh);
315316
*
316317
* fscrypt_set_bio_crypt_ctx() must have already been called on the bio.
317318
*
319+
* This function isn't required in cases where crypto-mergeability is ensured in
320+
* another way, such as I/O targeting only a single file (and thus a single key)
321+
* combined with fscrypt_limit_io_blocks() to ensure DUN contiguity.
322+
*
318323
* Return: true iff the I/O is mergeable
319324
*/
320325
bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
@@ -363,3 +368,91 @@ bool fscrypt_mergeable_bio_bh(struct bio *bio,
363368
return fscrypt_mergeable_bio(bio, inode, next_lblk);
364369
}
365370
EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio_bh);
371+
372+
/**
373+
* fscrypt_dio_supported() - check whether a DIO (direct I/O) request is
374+
* supported as far as encryption is concerned
375+
* @iocb: the file and position the I/O is targeting
376+
* @iter: the I/O data segment(s)
377+
*
378+
* Return: %true if there are no encryption constraints that prevent DIO from
379+
* being supported; %false if DIO is unsupported. (Note that in the
380+
* %true case, the filesystem might have other, non-encryption-related
381+
* constraints that prevent DIO from actually being supported.)
382+
*/
383+
bool fscrypt_dio_supported(struct kiocb *iocb, struct iov_iter *iter)
384+
{
385+
const struct inode *inode = file_inode(iocb->ki_filp);
386+
const unsigned int blocksize = i_blocksize(inode);
387+
388+
/* If the file is unencrypted, no veto from us. */
389+
if (!fscrypt_needs_contents_encryption(inode))
390+
return true;
391+
392+
/* We only support DIO with inline crypto, not fs-layer crypto. */
393+
if (!fscrypt_inode_uses_inline_crypto(inode))
394+
return false;
395+
396+
/*
397+
* Since the granularity of encryption is filesystem blocks, the file
398+
* position and total I/O length must be aligned to the filesystem block
399+
* size -- not just to the block device's logical block size as is
400+
* traditionally the case for DIO on many filesystems.
401+
*
402+
* We require that the user-provided memory buffers be filesystem block
403+
* aligned too. It is simpler to have a single alignment value required
404+
* for all properties of the I/O, as is normally the case for DIO.
405+
* Also, allowing less aligned buffers would imply that data units could
406+
* cross bvecs, which would greatly complicate the I/O stack, which
407+
* assumes that bios can be split at any bvec boundary.
408+
*/
409+
if (!IS_ALIGNED(iocb->ki_pos | iov_iter_alignment(iter), blocksize))
410+
return false;
411+
412+
return true;
413+
}
414+
EXPORT_SYMBOL_GPL(fscrypt_dio_supported);
415+
416+
/**
417+
* fscrypt_limit_io_blocks() - limit I/O blocks to avoid discontiguous DUNs
418+
* @inode: the file on which I/O is being done
419+
* @lblk: the block at which the I/O is being started from
420+
* @nr_blocks: the number of blocks we want to submit starting at @lblk
421+
*
422+
* Determine the limit to the number of blocks that can be submitted in a bio
423+
* targeting @lblk without causing a data unit number (DUN) discontiguity.
424+
*
425+
* This is normally just @nr_blocks, as normally the DUNs just increment along
426+
* with the logical blocks. (Or the file is not encrypted.)
427+
*
428+
* In rare cases, fscrypt can be using an IV generation method that allows the
429+
* DUN to wrap around within logically contiguous blocks, and that wraparound
430+
* will occur. If this happens, a value less than @nr_blocks will be returned
431+
* so that the wraparound doesn't occur in the middle of a bio, which would
432+
* cause encryption/decryption to produce wrong results.
433+
*
434+
* Return: the actual number of blocks that can be submitted
435+
*/
436+
u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks)
437+
{
438+
const struct fscrypt_info *ci;
439+
u32 dun;
440+
441+
if (!fscrypt_inode_uses_inline_crypto(inode))
442+
return nr_blocks;
443+
444+
if (nr_blocks <= 1)
445+
return nr_blocks;
446+
447+
ci = inode->i_crypt_info;
448+
if (!(fscrypt_policy_flags(&ci->ci_policy) &
449+
FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32))
450+
return nr_blocks;
451+
452+
/* With IV_INO_LBLK_32, the DUN can wrap around from U32_MAX to 0. */
453+
454+
dun = ci->ci_hashed_ino + lblk;
455+
456+
return min_t(u64, nr_blocks, (u64)U32_MAX + 1 - dun);
457+
}
458+
EXPORT_SYMBOL_GPL(fscrypt_limit_io_blocks);

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;

fs/f2fs/data.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4032,6 +4032,13 @@ static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
40324032

40334033
iomap->offset = blks_to_bytes(inode, map.m_lblk);
40344034

4035+
/*
4036+
* When inline encryption is enabled, sometimes I/O to an encrypted file
4037+
* has to be broken up to guarantee DUN contiguity. Handle this by
4038+
* limiting the length of the mapping returned.
4039+
*/
4040+
map.m_len = fscrypt_limit_io_blocks(inode, map.m_lblk, map.m_len);
4041+
40354042
if (map.m_flags & (F2FS_MAP_MAPPED | F2FS_MAP_UNWRITTEN)) {
40364043
iomap->length = blks_to_bytes(inode, map.m_len);
40374044
if (map.m_flags & F2FS_MAP_MAPPED) {

fs/f2fs/f2fs.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4371,7 +4371,11 @@ static inline bool f2fs_force_buffered_io(struct inode *inode,
43714371
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
43724372
int rw = iov_iter_rw(iter);
43734373

4374-
if (f2fs_post_read_required(inode))
4374+
if (!fscrypt_dio_supported(iocb, iter))
4375+
return true;
4376+
if (fsverity_active(inode))
4377+
return true;
4378+
if (f2fs_compressed_file(inode))
43754379
return true;
43764380

43774381
/* disallow direct IO if any of devices has unaligned blksize */

fs/iomap/direct-io.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <linux/module.h>
77
#include <linux/compiler.h>
88
#include <linux/fs.h>
9+
#include <linux/fscrypt.h>
910
#include <linux/pagemap.h>
1011
#include <linux/iomap.h>
1112
#include <linux/backing-dev.h>
@@ -179,11 +180,14 @@ static void iomap_dio_bio_end_io(struct bio *bio)
179180
static void iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio,
180181
loff_t pos, unsigned len)
181182
{
183+
struct inode *inode = file_inode(dio->iocb->ki_filp);
182184
struct page *page = ZERO_PAGE(0);
183185
int flags = REQ_SYNC | REQ_IDLE;
184186
struct bio *bio;
185187

186188
bio = bio_alloc(iter->iomap.bdev, 1, REQ_OP_WRITE | flags, GFP_KERNEL);
189+
fscrypt_set_bio_crypt_ctx(bio, inode, pos >> inode->i_blkbits,
190+
GFP_KERNEL);
187191
bio->bi_iter.bi_sector = iomap_sector(&iter->iomap, pos);
188192
bio->bi_private = dio;
189193
bio->bi_end_io = iomap_dio_bio_end_io;
@@ -308,6 +312,8 @@ static loff_t iomap_dio_bio_iter(const struct iomap_iter *iter,
308312
}
309313

310314
bio = bio_alloc(iomap->bdev, nr_pages, bio_opf, GFP_KERNEL);
315+
fscrypt_set_bio_crypt_ctx(bio, inode, pos >> inode->i_blkbits,
316+
GFP_KERNEL);
311317
bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
312318
bio->bi_write_hint = dio->iocb->ki_hint;
313319
bio->bi_ioprio = dio->iocb->ki_ioprio;

include/linux/fscrypt.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,10 @@ bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
714714
bool fscrypt_mergeable_bio_bh(struct bio *bio,
715715
const struct buffer_head *next_bh);
716716

717+
bool fscrypt_dio_supported(struct kiocb *iocb, struct iov_iter *iter);
718+
719+
u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks);
720+
717721
#else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
718722

719723
static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
@@ -742,6 +746,20 @@ static inline bool fscrypt_mergeable_bio_bh(struct bio *bio,
742746
{
743747
return true;
744748
}
749+
750+
static inline bool fscrypt_dio_supported(struct kiocb *iocb,
751+
struct iov_iter *iter)
752+
{
753+
const struct inode *inode = file_inode(iocb->ki_filp);
754+
755+
return !fscrypt_needs_contents_encryption(inode);
756+
}
757+
758+
static inline u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk,
759+
u64 nr_blocks)
760+
{
761+
return nr_blocks;
762+
}
745763
#endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
746764

747765
/**

0 commit comments

Comments
 (0)