Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f2ca2620 authored by Eric Biggers's avatar Eric Biggers Committed by Alistair Delva
Browse files

BACKPORT: FROMLIST: 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.

Bug: 137270441
Test: tested as series; see I26aac0ac7845a9064f28bb1421eb2522828a6dec
Change-Id: I54a8efe388289918f4144d8138fb87aa507ae760
Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
Signed-off-by: default avatarSatya Tangirala <satyat@google.com>
Link: https://patchwork.kernel.org/patch/11214781/
parent e274bd38
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@
#include <linux/pagevec.h>
#include <linux/sched/mm.h>
#include <trace/events/block.h>
#include <linux/fscrypt.h>

static int fsync_buffers_list(spinlock_t *lock, struct list_head *list);
static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh,
@@ -3068,6 +3069,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 | __GFP_NOFAIL);

	if (wbc) {
		wbc_init_bio(wbc, bio);
		wbc_account_io(wbc, bh->b_page, bh->b_size);
+1 −0
Original line number Diff line number Diff line
@@ -1140,6 +1140,7 @@ struct ext4_inode_info {
#define EXT4_MOUNT_JOURNAL_CHECKSUM	0x800000 /* Journal checksums */
#define EXT4_MOUNT_JOURNAL_ASYNC_COMMIT	0x1000000 /* Journal Async Commit */
#define EXT4_MOUNT_WARN_ON_ERROR	0x2000000 /* Trigger WARN_ON on error */
#define EXT4_MOUNT_INLINECRYPT		0x4000000 /* Inline encryption support */
#define EXT4_MOUNT_DELALLOC		0x8000000 /* Delalloc support */
#define EXT4_MOUNT_DATA_ERR_ABORT	0x10000000 /* Abort on file data write */
#define EXT4_MOUNT_BLOCK_VALIDITY	0x20000000 /* Block validity checking */
+2 −2
Original line number Diff line number Diff line
@@ -1228,7 +1228,7 @@ static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len,
		    (block_start < from || block_end > to)) {
			ll_rw_block(REQ_OP_READ, 0, 1, &bh);
			*wait_bh++ = bh;
			decrypt = IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
			decrypt = fscrypt_inode_uses_fs_layer_crypto(inode);
		}
	}
	/*
@@ -4114,7 +4114,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));
			BUG_ON(blocksize != PAGE_SIZE);
+9 −2
Original line number Diff line number Diff line
@@ -362,10 +362,16 @@ static int io_submit_init_bio(struct ext4_io_submit *io,
			      struct buffer_head *bh)
{
	struct bio *bio;
	int err;

	bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
	if (!bio)
		return -ENOMEM;
	err = fscrypt_set_bio_crypt_ctx_bh(bio, bh, GFP_NOIO);
	if (err) {
		bio_put(bio);
		return err;
	}
	wbc_init_bio(io->io_wbc, bio);
	bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
	bio_set_dev(bio, bh->b_bdev);
@@ -383,7 +389,8 @@ static int 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);
	}
@@ -469,7 +476,7 @@ int ext4_bio_write_page(struct ext4_io_submit *io,

	bh = head = page_buffers(page);

	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;

	retry_encrypt:
+12 −3
Original line number Diff line number Diff line
@@ -198,7 +198,7 @@ static struct bio_post_read_ctx *get_bio_post_read_ctx(struct inode *inode,
	unsigned int post_read_steps = 0;
	struct bio_post_read_ctx *ctx = NULL;

	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))
@@ -259,6 +259,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;
@@ -290,7 +291,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;
@@ -390,7 +392,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:
			ext4_submit_bio_read(bio);
			bio = NULL;
@@ -402,6 +405,12 @@ int ext4_mpage_readpages(struct address_space *mapping,
				min_t(int, nr_pages, BIO_MAX_PAGES));
			if (!bio)
				goto set_error_page;
			if (fscrypt_set_bio_crypt_ctx(bio, inode, next_block,
						      GFP_KERNEL) != 0) {
				bio_put(bio);
				bio = NULL;
				goto set_error_page;
			}
			ctx = get_bio_post_read_ctx(inode, bio, page->index);
			if (IS_ERR(ctx)) {
				bio_put(bio);
Loading