Skip to content

Commit c6c8978

Browse files
committed
fscrypt: add functions for direct I/O support
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. In preparation for supporting this, add the following functions: - fscrypt_dio_supported() checks whether a DIO request is supported as far as encryption is concerned. Encrypted files will only support DIO when inline encryption is used and the I/O request is properly aligned; this function checks these preconditions. - fscrypt_limit_io_blocks() limits the length of a bio to avoid crossing a place in the file that a bio with an encryption context cannot cross due to a DUN discontiguity. This function is needed by filesystems that use the iomap DIO implementation (which operates directly on logical ranges, so it won't use fscrypt_mergeable_bio()) and that support FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32. Co-developed-by: Satya Tangirala <satyat@google.com> Signed-off-by: Satya Tangirala <satyat@google.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20220128233940.79464-2-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@google.com>
1 parent dfd42fa commit c6c8978

3 files changed

Lines changed: 119 additions & 0 deletions

File tree

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);

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)