Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp1280439ybb; Wed, 25 Mar 2020 20:09:18 -0700 (PDT) X-Google-Smtp-Source: ADFU+vvE66w8jYVUWHtgr4w+YWqT19Dh8FRY2UGeE+SPR9b2pdIotGfQjstinrufzA6KepsTrJqR X-Received: by 2002:a05:6830:10b:: with SMTP id i11mr4325940otp.99.1585192158546; Wed, 25 Mar 2020 20:09:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585192158; cv=none; d=google.com; s=arc-20160816; b=KgxGYeOMI4lEYPakceORxWPj39NNQluTPDs/Eb7Tx9razbzIze4hXSyNBABNhx9aCS XOBLQaCR7m4GzKL5GhtnXsqkH/A9Au+7JjyQ+kMVs4yb8PuFJ0IlNjwFIFjqSIrYgWwU opPrUXK5w1u3u1WuaC7hUFYiCaM0zuCx9dD1Ffoj2pFXAOqJWtSLO1KmX4VMvHHjjS+0 QRNmr09sBWCVZgscDj5ZrFdeEoLelilwONL7J7NBI9+LbA+E7K7khy0J+hBpj9+oapoy 2dVSSxnkgaoElEp0fo9crh+1Xd6/Iei4ULEwkYVYvo83cAaSNNIkvqGblwGvYQVh4IMy Vtmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=zm76QeF6taktnh5e54BxZA9RAiJwG0+jPa+LHHPF+mE=; b=EFIrH8SDlwk734FH0LapdwvSobe1rItlzogkaWZP86QACuW0Y0df4gE4q+qkky7Cq8 zWP9YKFHF9O8N6l0WsxSK3Yv+8dxHhXuOBd2R41q5ZBko6qA57QQFISKbhIks7+99C5c rZ1usXDsxWwkRSAqcBbrqTuYMbGZJBP598B3H/ZZWSttWvef5cT2pE5lgT2/Xc4qqb2z L9eMpTE7whoctzc8J5JMh5Ru/Ki5Dedurl+fZ0ApVWGU4XuVk7js15XoiocNNqBxFBrI jmexjUv0wkisdWsPZ9EcYgtJ+FFbQSUpxy3eQdr9GTK3Qz5c368utDiNilRyE+YHFslr g6JQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=r1xlYYkF; spf=pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 53si503597otd.15.2020.03.25.20.09.07; Wed, 25 Mar 2020 20:09:18 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=r1xlYYkF; spf=pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727781AbgCZDIm (ORCPT + 99 others); Wed, 25 Mar 2020 23:08:42 -0400 Received: from mail-pf1-f201.google.com ([209.85.210.201]:42070 "EHLO mail-pf1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727703AbgCZDIi (ORCPT ); Wed, 25 Mar 2020 23:08:38 -0400 Received: by mail-pf1-f201.google.com with SMTP id j2so4030654pfh.9 for ; Wed, 25 Mar 2020 20:08:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=zm76QeF6taktnh5e54BxZA9RAiJwG0+jPa+LHHPF+mE=; b=r1xlYYkFZCORE7cKTB1lZ6rdH/MjVnk8XIfSk/rZg+D2UCESzFUQutqOSVRm647NAh s+uHERM165tiPLOr7sZGwQUeM19ND5GyE4Mn3XIZCTahGTTeRE0lX5NBdpo0sy0Gqzv3 58G4Fu/wjgFDHDg7DNpJ+C1xTXCUuI3ul31GVQsEz4f04W3fibjSfCo7dE8L33j7XMQX kSY/C7KdBkZYsMo0pGoIKsRQeb5qW99WVRoPTd/MY08pVchKEkDpZwqPTm47Ex+n4J/u 84PYC9W6H/Mutc4lMejS7YpADzlt7zwBO4p/Orv3tsNJQXwFJzk0Q/xRJC/Frp88i9aG 5Hyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=zm76QeF6taktnh5e54BxZA9RAiJwG0+jPa+LHHPF+mE=; b=PtF39FC3WBIBBEUlHj2+N/VYPma9xDi1U7n00p0KHxQQ29AvnlC1pRRdmw1rFfAtji 8Iv+Ytl9g7wlyAOIz1gvbfOGVUV9RnsYaYnMyn5LN4CJk+ynieq55UAn1XSHiDk5+82N ZuqKsJzJlJX7OWrIzUA559eU4lHyZVu8StnOeaDyRzNLDrMc6l7gLYnarAOOYPLzRUE1 XLFQtKfiPgBnfpuy8AHorhb0fAKIhMNXx8vPYFGgnUDO9X5fEm4ZR8dt5Y+drV3Q2uq3 70AQrDtr8WL7dSmcCGQ74gfskSqaKbjkVHAGZnqOcgtcD/xXOT4hHD//r9FRXiqhXn0Y uVzQ== X-Gm-Message-State: ANhLgQ0qD9COycwlrStwV6KJQhRaqgHVT1HShl6mRhwh9ZDvaudXuR58 67bFqjMh0Oc/3A/BEq5BNkHJAmxdt9Q= X-Received: by 2002:a17:90a:240a:: with SMTP id h10mr709109pje.123.1585192116205; Wed, 25 Mar 2020 20:08:36 -0700 (PDT) Date: Wed, 25 Mar 2020 20:07:02 -0700 In-Reply-To: <20200326030702.223233-1-satyat@google.com> Message-Id: <20200326030702.223233-12-satyat@google.com> Mime-Version: 1.0 References: <20200326030702.223233-1-satyat@google.com> X-Mailer: git-send-email 2.25.1.696.g5e7596f4ac-goog Subject: [PATCH v9 11/11] ext4: add inline encryption support From: Satya Tangirala To: linux-block@vger.kernel.org, linux-scsi@vger.kernel.org, linux-fscrypt@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-ext4@vger.kernel.org Cc: Barani Muthukumaran , Kuohong Wang , Kim Boojin , Eric Biggers , Satya Tangirala Content-Type: text/plain; charset="UTF-8" Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org From: Eric Biggers 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. Signed-off-by: Eric Biggers Co-developed-by: Satya Tangirala Signed-off-by: Satya Tangirala --- Documentation/admin-guide/ext4.rst | 6 ++++++ fs/buffer.c | 7 ++++--- fs/ext4/inode.c | 4 ++-- fs/ext4/page-io.c | 6 ++++-- fs/ext4/readpage.c | 11 ++++++++--- fs/ext4/super.c | 9 +++++++++ 6 files changed, 33 insertions(+), 10 deletions(-) diff --git a/Documentation/admin-guide/ext4.rst b/Documentation/admin-guide/ext4.rst index 9443fcef18760..ed997e3766781 100644 --- a/Documentation/admin-guide/ext4.rst +++ b/Documentation/admin-guide/ext4.rst @@ -395,6 +395,12 @@ When mounting an ext4 filesystem, the following option are accepted: Documentation/filesystems/dax.txt. Note that this option is incompatible with data=journal. + inlinecrypt + Encrypt/decrypt the contents of encrypted files using the blk-crypto + framework rather than filesystem-layer encryption. This allows the use + of inline encryption hardware. The on-disk format is unaffected. For + more details, see Documentation/block/inline-encryption.rst. + Data Mode ========= There are 3 different data modes: diff --git a/fs/buffer.c b/fs/buffer.c index b8d28370cfd7f..226f1784eda74 100644 --- a/fs/buffer.c +++ b/fs/buffer.c @@ -331,9 +331,8 @@ static void decrypt_bh(struct work_struct *work) static void end_buffer_async_read_io(struct buffer_head *bh, int uptodate) { /* Decrypt if needed */ - if (uptodate && IS_ENABLED(CONFIG_FS_ENCRYPTION) && - IS_ENCRYPTED(bh->b_page->mapping->host) && - S_ISREG(bh->b_page->mapping->host->i_mode)) { + if (uptodate && + fscrypt_inode_uses_fs_layer_crypto(bh->b_page->mapping->host)) { struct decrypt_bh_ctx *ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC); if (ctx) { @@ -3085,6 +3084,8 @@ static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh, */ bio = bio_alloc(GFP_NOIO, 1); + fscrypt_set_bio_crypt_ctx_bh(bio, bh, GFP_NOIO); + bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9); bio_set_dev(bio, bh->b_bdev); bio->bi_write_hint = write_hint; diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index fa0ff78dc033f..5714ff6395a1c 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -1089,7 +1089,7 @@ static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len, } if (unlikely(err)) { page_zero_new_buffers(page, from, to); - } else if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) { + } else if (fscrypt_inode_uses_fs_layer_crypto(inode)) { for (i = 0; i < nr_wait; i++) { int err2; @@ -3720,7 +3720,7 @@ static int __ext4_block_zero_page_range(handle_t *handle, /* Uhhuh. Read error. Complain and punt. */ if (!buffer_uptodate(bh)) goto unlock; - if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode)) { + if (fscrypt_inode_uses_fs_layer_crypto(inode)) { /* We expect the key to be set. */ BUG_ON(!fscrypt_has_encryption_key(inode)); err = fscrypt_decrypt_pagecache_blocks(page, blocksize, diff --git a/fs/ext4/page-io.c b/fs/ext4/page-io.c index 68b39e75446a1..0757145a31b26 100644 --- a/fs/ext4/page-io.c +++ b/fs/ext4/page-io.c @@ -404,6 +404,7 @@ static void io_submit_init_bio(struct ext4_io_submit *io, * __GFP_DIRECT_RECLAIM is set, see comments for bio_alloc_bioset(). */ bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES); + fscrypt_set_bio_crypt_ctx_bh(bio, bh, GFP_NOIO); bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9); bio_set_dev(bio, bh->b_bdev); bio->bi_end_io = ext4_end_bio; @@ -420,7 +421,8 @@ static void io_submit_add_bh(struct ext4_io_submit *io, { int ret; - if (io->io_bio && bh->b_blocknr != io->io_next_block) { + if (io->io_bio && (bh->b_blocknr != io->io_next_block || + !fscrypt_mergeable_bio_bh(io->io_bio, bh))) { submit_and_retry: ext4_io_submit(io); } @@ -508,7 +510,7 @@ int ext4_bio_write_page(struct ext4_io_submit *io, * (e.g. holes) to be unnecessarily encrypted, but this is rare and * can't happen in the common case of blocksize == PAGE_SIZE. */ - if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode) && nr_to_submit) { + if (fscrypt_inode_uses_fs_layer_crypto(inode) && nr_to_submit) { gfp_t gfp_flags = GFP_NOFS; unsigned int enc_bytes = round_up(len, i_blocksize(inode)); diff --git a/fs/ext4/readpage.c b/fs/ext4/readpage.c index c1769afbf7995..68eac0aeffad3 100644 --- a/fs/ext4/readpage.c +++ b/fs/ext4/readpage.c @@ -195,7 +195,7 @@ static void ext4_set_bio_post_read_ctx(struct bio *bio, { unsigned int post_read_steps = 0; - if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) + if (fscrypt_inode_uses_fs_layer_crypto(inode)) post_read_steps |= 1 << STEP_DECRYPT; if (ext4_need_verity(inode, first_idx)) @@ -232,6 +232,7 @@ int ext4_mpage_readpages(struct address_space *mapping, const unsigned blkbits = inode->i_blkbits; const unsigned blocks_per_page = PAGE_SIZE >> blkbits; const unsigned blocksize = 1 << blkbits; + sector_t next_block; sector_t block_in_file; sector_t last_block; sector_t last_block_in_file; @@ -264,7 +265,8 @@ int ext4_mpage_readpages(struct address_space *mapping, if (page_has_buffers(page)) goto confused; - block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits); + block_in_file = next_block = + (sector_t)page->index << (PAGE_SHIFT - blkbits); last_block = block_in_file + nr_pages * blocks_per_page; last_block_in_file = (ext4_readpage_limit(inode) + blocksize - 1) >> blkbits; @@ -364,7 +366,8 @@ int ext4_mpage_readpages(struct address_space *mapping, * This page will go to BIO. Do we need to send this * BIO off first? */ - if (bio && (last_block_in_bio != blocks[0] - 1)) { + if (bio && (last_block_in_bio != blocks[0] - 1 || + !fscrypt_mergeable_bio(bio, inode, next_block))) { submit_and_realloc: submit_bio(bio); bio = NULL; @@ -376,6 +379,8 @@ int ext4_mpage_readpages(struct address_space *mapping, */ bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES)); + fscrypt_set_bio_crypt_ctx(bio, inode, next_block, + GFP_KERNEL); ext4_set_bio_post_read_ctx(bio, inode, page->index); bio_set_dev(bio, bdev); bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9); diff --git a/fs/ext4/super.c b/fs/ext4/super.c index 0c7c4adb664ec..2054da2ddde42 100644 --- a/fs/ext4/super.c +++ b/fs/ext4/super.c @@ -1514,6 +1514,7 @@ enum { Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit, Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback, Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption, + Opt_inlinecrypt, Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota, Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota, Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err, @@ -1611,6 +1612,7 @@ static const match_table_t tokens = { {Opt_noinit_itable, "noinit_itable"}, {Opt_max_dir_size_kb, "max_dir_size_kb=%u"}, {Opt_test_dummy_encryption, "test_dummy_encryption"}, + {Opt_inlinecrypt, "inlinecrypt"}, {Opt_nombcache, "nombcache"}, {Opt_nombcache, "no_mbcache"}, /* for backward compatibility */ {Opt_removed, "check=none"}, /* mount option from ext2/3 */ @@ -1898,6 +1900,13 @@ static int handle_mount_opt(struct super_block *sb, char *opt, int token, case Opt_nolazytime: sb->s_flags &= ~SB_LAZYTIME; return 1; + case Opt_inlinecrypt: +#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT + sb->s_flags |= SB_INLINECRYPT; +#else + ext4_msg(sb, KERN_ERR, "inline encryption not supported"); +#endif + return 1; } for (m = ext4_mount_opts; m->token != Opt_err; m++) -- 2.25.1.696.g5e7596f4ac-goog