Skip to content

Commit 4f74d15

Browse files
committed
ext4: add inline encryption support
Wire up ext4 to support inline encryption via the helper functions which fs/crypto/ now provides. This includes: - Adding a mount option 'inlinecrypt' which enables inline encryption on encrypted files where it can be used. - Setting the bio_crypt_ctx on bios that will be submitted to an inline-encrypted file. Note: submit_bh_wbc() in fs/buffer.c also needed to be patched for this part, since ext4 sometimes uses ll_rw_block() on file data. - Not adding logically discontiguous data to bios that will be submitted to an inline-encrypted file. - Not doing filesystem-layer crypto on inline-encrypted files. Co-developed-by: Satya Tangirala <[email protected]> Signed-off-by: Satya Tangirala <[email protected]> Reviewed-by: Theodore Ts'o <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Eric Biggers <[email protected]>
1 parent 27aacd2 commit 4f74d15

File tree

6 files changed

+37
-10
lines changed

6 files changed

+37
-10
lines changed

Documentation/admin-guide/ext4.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,13 @@ When mounting an ext4 filesystem, the following option are accepted:
395395
Documentation/filesystems/dax.txt. Note that this option is
396396
incompatible with data=journal.
397397

398+
inlinecrypt
399+
When possible, encrypt/decrypt the contents of encrypted files using the
400+
blk-crypto framework rather than filesystem-layer encryption. This
401+
allows the use of inline encryption hardware. The on-disk format is
402+
unaffected. For more details, see
403+
Documentation/block/inline-encryption.rst.
404+
398405
Data Mode
399406
=========
400407
There are 3 different data modes:

fs/buffer.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,8 @@ static void decrypt_bh(struct work_struct *work)
320320
static void end_buffer_async_read_io(struct buffer_head *bh, int uptodate)
321321
{
322322
/* Decrypt if needed */
323-
if (uptodate && IS_ENABLED(CONFIG_FS_ENCRYPTION) &&
324-
IS_ENCRYPTED(bh->b_page->mapping->host) &&
325-
S_ISREG(bh->b_page->mapping->host->i_mode)) {
323+
if (uptodate &&
324+
fscrypt_inode_uses_fs_layer_crypto(bh->b_page->mapping->host)) {
326325
struct decrypt_bh_ctx *ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
327326

328327
if (ctx) {
@@ -3046,6 +3045,8 @@ static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh,
30463045
*/
30473046
bio = bio_alloc(GFP_NOIO, 1);
30483047

3048+
fscrypt_set_bio_crypt_ctx_bh(bio, bh, GFP_NOIO);
3049+
30493050
bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
30503051
bio_set_dev(bio, bh->b_bdev);
30513052
bio->bi_write_hint = write_hint;

fs/ext4/inode.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len,
10961096
}
10971097
if (unlikely(err)) {
10981098
page_zero_new_buffers(page, from, to);
1099-
} else if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) {
1099+
} else if (fscrypt_inode_uses_fs_layer_crypto(inode)) {
11001100
for (i = 0; i < nr_wait; i++) {
11011101
int err2;
11021102

@@ -3737,7 +3737,7 @@ static int __ext4_block_zero_page_range(handle_t *handle,
37373737
/* Uhhuh. Read error. Complain and punt. */
37383738
if (!buffer_uptodate(bh))
37393739
goto unlock;
3740-
if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode)) {
3740+
if (fscrypt_inode_uses_fs_layer_crypto(inode)) {
37413741
/* We expect the key to be set. */
37423742
BUG_ON(!fscrypt_has_encryption_key(inode));
37433743
err = fscrypt_decrypt_pagecache_blocks(page, blocksize,

fs/ext4/page-io.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ static void io_submit_init_bio(struct ext4_io_submit *io,
402402
* __GFP_DIRECT_RECLAIM is set, see comments for bio_alloc_bioset().
403403
*/
404404
bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
405+
fscrypt_set_bio_crypt_ctx_bh(bio, bh, GFP_NOIO);
405406
bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
406407
bio_set_dev(bio, bh->b_bdev);
407408
bio->bi_end_io = ext4_end_bio;
@@ -418,7 +419,8 @@ static void io_submit_add_bh(struct ext4_io_submit *io,
418419
{
419420
int ret;
420421

421-
if (io->io_bio && bh->b_blocknr != io->io_next_block) {
422+
if (io->io_bio && (bh->b_blocknr != io->io_next_block ||
423+
!fscrypt_mergeable_bio_bh(io->io_bio, bh))) {
422424
submit_and_retry:
423425
ext4_io_submit(io);
424426
}
@@ -506,7 +508,7 @@ int ext4_bio_write_page(struct ext4_io_submit *io,
506508
* (e.g. holes) to be unnecessarily encrypted, but this is rare and
507509
* can't happen in the common case of blocksize == PAGE_SIZE.
508510
*/
509-
if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode) && nr_to_submit) {
511+
if (fscrypt_inode_uses_fs_layer_crypto(inode) && nr_to_submit) {
510512
gfp_t gfp_flags = GFP_NOFS;
511513
unsigned int enc_bytes = round_up(len, i_blocksize(inode));
512514

fs/ext4/readpage.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ static void ext4_set_bio_post_read_ctx(struct bio *bio,
195195
{
196196
unsigned int post_read_steps = 0;
197197

198-
if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode))
198+
if (fscrypt_inode_uses_fs_layer_crypto(inode))
199199
post_read_steps |= 1 << STEP_DECRYPT;
200200

201201
if (ext4_need_verity(inode, first_idx))
@@ -230,6 +230,7 @@ int ext4_mpage_readpages(struct inode *inode,
230230
const unsigned blkbits = inode->i_blkbits;
231231
const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
232232
const unsigned blocksize = 1 << blkbits;
233+
sector_t next_block;
233234
sector_t block_in_file;
234235
sector_t last_block;
235236
sector_t last_block_in_file;
@@ -258,7 +259,8 @@ int ext4_mpage_readpages(struct inode *inode,
258259
if (page_has_buffers(page))
259260
goto confused;
260261

261-
block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
262+
block_in_file = next_block =
263+
(sector_t)page->index << (PAGE_SHIFT - blkbits);
262264
last_block = block_in_file + nr_pages * blocks_per_page;
263265
last_block_in_file = (ext4_readpage_limit(inode) +
264266
blocksize - 1) >> blkbits;
@@ -358,7 +360,8 @@ int ext4_mpage_readpages(struct inode *inode,
358360
* This page will go to BIO. Do we need to send this
359361
* BIO off first?
360362
*/
361-
if (bio && (last_block_in_bio != blocks[0] - 1)) {
363+
if (bio && (last_block_in_bio != blocks[0] - 1 ||
364+
!fscrypt_mergeable_bio(bio, inode, next_block))) {
362365
submit_and_realloc:
363366
submit_bio(bio);
364367
bio = NULL;
@@ -370,6 +373,8 @@ int ext4_mpage_readpages(struct inode *inode,
370373
*/
371374
bio = bio_alloc(GFP_KERNEL,
372375
min_t(int, nr_pages, BIO_MAX_PAGES));
376+
fscrypt_set_bio_crypt_ctx(bio, inode, next_block,
377+
GFP_KERNEL);
373378
ext4_set_bio_post_read_ctx(bio, inode, page->index);
374379
bio_set_dev(bio, bdev);
375380
bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9);

fs/ext4/super.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,7 @@ enum {
15081508
Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
15091509
Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
15101510
Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
1511+
Opt_inlinecrypt,
15111512
Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
15121513
Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
15131514
Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
@@ -1610,6 +1611,7 @@ static const match_table_t tokens = {
16101611
{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
16111612
{Opt_test_dummy_encryption, "test_dummy_encryption=%s"},
16121613
{Opt_test_dummy_encryption, "test_dummy_encryption"},
1614+
{Opt_inlinecrypt, "inlinecrypt"},
16131615
{Opt_nombcache, "nombcache"},
16141616
{Opt_nombcache, "no_mbcache"}, /* for backward compatibility */
16151617
{Opt_removed, "check=none"}, /* mount option from ext2/3 */
@@ -1946,6 +1948,13 @@ static int handle_mount_opt(struct super_block *sb, char *opt, int token,
19461948
case Opt_nolazytime:
19471949
sb->s_flags &= ~SB_LAZYTIME;
19481950
return 1;
1951+
case Opt_inlinecrypt:
1952+
#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
1953+
sb->s_flags |= SB_INLINECRYPT;
1954+
#else
1955+
ext4_msg(sb, KERN_ERR, "inline encryption not supported");
1956+
#endif
1957+
return 1;
19491958
}
19501959

19511960
for (m = ext4_mount_opts; m->token != Opt_err; m++)
@@ -2404,6 +2413,9 @@ static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
24042413

24052414
fscrypt_show_test_dummy_encryption(seq, sep, sb);
24062415

2416+
if (sb->s_flags & SB_INLINECRYPT)
2417+
SEQ_OPTS_PUTS("inlinecrypt");
2418+
24072419
if (test_opt(sb, DAX_ALWAYS)) {
24082420
if (IS_EXT2_SB(sb))
24092421
SEQ_OPTS_PUTS("dax");

0 commit comments

Comments
 (0)