All of lore.kernel.org
 help / color / mirror / Atom feed
From: Josef Bacik <josef@toxicpanda.com>
To: linux-btrfs@vger.kernel.org, kernel-team@fb.com,
	linux-fsdevel@vger.kernel.org
Subject: [PATCH v4 34/46] btrfs: set the bio fscrypt context when applicable
Date: Fri,  1 Dec 2023 17:11:31 -0500	[thread overview]
Message-ID: <cfb67d4a11cdc6c9d0685b5c505b1170bb7b9bb4.1701468306.git.josef@toxicpanda.com> (raw)
In-Reply-To: <cover.1701468305.git.josef@toxicpanda.com>

Now that we have the fscrypt_info plumbed through everywhere, add the
code to setup the bio encryption context from the extent context.

We use the per-extent fscrypt_extent_info for encryption/decryption.
We use the offset into the extent as the lblk for fscrypt.  So the start
of the extent has the lblk of 0, 4k into the extent has the lblk of 4k,
etc.  This is done to allow things like relocation to continue to work
properly.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 fs/btrfs/compression.c |  6 ++++
 fs/btrfs/extent_io.c   | 76 +++++++++++++++++++++++++++++++++++++++++-
 fs/btrfs/fscrypt.c     | 36 ++++++++++++++++++++
 fs/btrfs/fscrypt.h     | 22 ++++++++++++
 fs/btrfs/inode.c       | 10 ++++++
 5 files changed, 149 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
index 05595d113ff8..a71614359c33 100644
--- a/fs/btrfs/compression.c
+++ b/fs/btrfs/compression.c
@@ -37,6 +37,7 @@
 #include "zoned.h"
 #include "file-item.h"
 #include "super.h"
+#include "fscrypt.h"
 
 static struct bio_set btrfs_compressed_bioset;
 
@@ -396,6 +397,9 @@ void btrfs_submit_compressed_write(struct btrfs_ordered_extent *ordered,
 	cb->bbio.ordered = ordered;
 	btrfs_add_compressed_bio_pages(cb);
 
+	btrfs_set_bio_crypt_ctx_from_extent(&cb->bbio.bio, inode,
+					    ordered->fscrypt_info, 0);
+
 	btrfs_submit_bio(&cb->bbio, 0);
 }
 
@@ -599,6 +603,8 @@ void btrfs_submit_compressed_read(struct btrfs_bio *bbio)
 	cb->compress_type = em->compress_type;
 	cb->orig_bbio = bbio;
 
+	btrfs_set_bio_crypt_ctx_from_extent(&cb->bbio.bio, inode,
+					    em->fscrypt_info, 0);
 	free_extent_map(em);
 
 	cb->nr_pages = DIV_ROUND_UP(compressed_len, PAGE_SIZE);
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index 42544c0d9ee1..9824dd356e3c 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -36,6 +36,7 @@
 #include "dev-replace.h"
 #include "super.h"
 #include "transaction.h"
+#include "fscrypt.h"
 
 static struct kmem_cache *extent_buffer_cache;
 
@@ -102,6 +103,10 @@ struct btrfs_bio_ctrl {
 	blk_opf_t opf;
 	btrfs_bio_end_io_t end_io_func;
 	struct writeback_control *wbc;
+
+	/* This is set for reads and we have encryption. */
+	struct fscrypt_extent_info *fscrypt_info;
+	u64 orig_start;
 };
 
 static void submit_one_bio(struct btrfs_bio_ctrl *bio_ctrl)
@@ -708,10 +713,31 @@ static bool btrfs_bio_is_contig(struct btrfs_bio_ctrl *bio_ctrl,
 				struct page *page, u64 disk_bytenr,
 				unsigned int pg_offset)
 {
-	struct bio *bio = &bio_ctrl->bbio->bio;
+	struct inode *inode = page->mapping->host;
+	struct btrfs_bio *bbio = bio_ctrl->bbio;
+	struct bio *bio = &bbio->bio;
 	struct bio_vec *bvec = bio_last_bvec_all(bio);
 	const sector_t sector = disk_bytenr >> SECTOR_SHIFT;
 
+	if (IS_ENCRYPTED(inode)) {
+		u64 file_offset = page_offset(page) + pg_offset;
+		u64 offset = 0;
+		struct fscrypt_extent_info *fscrypt_info = NULL;
+
+		/* bio_ctrl->fscrypt_info is only set in the READ case. */
+		if (bio_ctrl->fscrypt_info) {
+			offset = file_offset - bio_ctrl->orig_start;
+			fscrypt_info = bio_ctrl->fscrypt_info;
+		} else if (bbio->ordered) {
+			fscrypt_info = bbio->ordered->fscrypt_info;
+			offset = file_offset - bbio->ordered->orig_offset;
+		}
+
+		if (!btrfs_mergeable_encrypted_bio(bio, inode, fscrypt_info,
+						   offset))
+			return false;
+	}
+
 	if (bio_ctrl->compress_type != BTRFS_COMPRESS_NONE) {
 		/*
 		 * For compression, all IO should have its logical bytenr set
@@ -742,6 +768,8 @@ static void alloc_new_bio(struct btrfs_inode *inode,
 {
 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
 	struct btrfs_bio *bbio;
+	struct fscrypt_extent_info *fscrypt_info = NULL;
+	u64 offset = 0;
 
 	bbio = btrfs_bio_alloc(BIO_MAX_VECS, bio_ctrl->opf, fs_info,
 			       bio_ctrl->end_io_func, NULL);
@@ -761,6 +789,8 @@ static void alloc_new_bio(struct btrfs_inode *inode,
 					ordered->file_offset +
 					ordered->disk_num_bytes - file_offset);
 			bbio->ordered = ordered;
+			fscrypt_info = ordered->fscrypt_info;
+			offset = file_offset - ordered->orig_offset;
 		}
 
 		/*
@@ -771,7 +801,13 @@ static void alloc_new_bio(struct btrfs_inode *inode,
 		 */
 		bio_set_dev(&bbio->bio, fs_info->fs_devices->latest_dev->bdev);
 		wbc_init_bio(bio_ctrl->wbc, &bbio->bio);
+	} else {
+		fscrypt_info = bio_ctrl->fscrypt_info;
+		offset = file_offset - bio_ctrl->orig_start;
 	}
+
+	btrfs_set_bio_crypt_ctx_from_extent(&bbio->bio, inode, fscrypt_info,
+					    offset);
 }
 
 /*
@@ -815,6 +851,19 @@ static void submit_extent_page(struct btrfs_bio_ctrl *bio_ctrl,
 			len = bio_ctrl->len_to_oe_boundary;
 		}
 
+		/*
+		 * Encryption has to allocate bounce buffers to encrypt the bio,
+		 * and we need to make sure that it doesn't split the bio so we
+		 * retain all of our special info in the btrfs_bio, so submit
+		 * any bio that gets up to BIO_MAX_VECS worth of segments.
+		 */
+		if (IS_ENCRYPTED(&inode->vfs_inode) &&
+		    bio_data_dir(&bio_ctrl->bbio->bio) == WRITE &&
+		    bio_segments(&bio_ctrl->bbio->bio) == BIO_MAX_VECS) {
+			submit_one_bio(bio_ctrl);
+			continue;
+		}
+
 		if (bio_add_page(&bio_ctrl->bbio->bio, page, len, pg_offset) != len) {
 			/* bio full: move on to a new one */
 			submit_one_bio(bio_ctrl);
@@ -1008,6 +1057,8 @@ static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached,
 		bool force_bio_submit = false;
 		u64 disk_bytenr;
 
+		bio_ctrl->fscrypt_info = NULL;
+
 		ASSERT(IS_ALIGNED(cur, fs_info->sectorsize));
 		if (cur >= last_byte) {
 			iosize = PAGE_SIZE - pg_offset;
@@ -1082,6 +1133,22 @@ static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached,
 		if (prev_em_start)
 			*prev_em_start = em->start;
 
+		/*
+		 * We use the extent offset for the IV when decrypting the page,
+		 * so we have to set the extent_offset based on the orig_start
+		 * for this extent.  Also save the fscrypt_info so the bio ctx
+		 * can be set properly.  If this inode isn't encrypted this
+		 * won't do anything.
+		 *
+		 * If we're compressed we'll handle all of this in
+		 * btrfs_submit_compressed_read.
+		 */
+		if (compress_type == BTRFS_COMPRESS_NONE) {
+			bio_ctrl->orig_start = em->orig_start;
+			bio_ctrl->fscrypt_info =
+				fscrypt_get_extent_info(em->fscrypt_info);
+		}
+
 		free_extent_map(em);
 		em = NULL;
 
@@ -1093,6 +1160,9 @@ static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached,
 			end_page_read(page, true, cur, iosize);
 			cur = cur + iosize;
 			pg_offset += iosize;
+
+			/* This shouldn't be set, but clear it just in case. */
+			fscrypt_put_extent_info(bio_ctrl->fscrypt_info);
 			continue;
 		}
 		/* the get_extent function already copied into the page */
@@ -1101,6 +1171,9 @@ static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached,
 			end_page_read(page, true, cur, iosize);
 			cur = cur + iosize;
 			pg_offset += iosize;
+
+			/* This shouldn't be set, but clear it just in case. */
+			fscrypt_put_extent_info(bio_ctrl->fscrypt_info);
 			continue;
 		}
 
@@ -1113,6 +1186,7 @@ static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached,
 			submit_one_bio(bio_ctrl);
 		submit_extent_page(bio_ctrl, disk_bytenr, page, iosize,
 				   pg_offset);
+		fscrypt_put_extent_info(bio_ctrl->fscrypt_info);
 		cur = cur + iosize;
 		pg_offset += iosize;
 	}
diff --git a/fs/btrfs/fscrypt.c b/fs/btrfs/fscrypt.c
index 7a7272cb83ec..726cb6121934 100644
--- a/fs/btrfs/fscrypt.c
+++ b/fs/btrfs/fscrypt.c
@@ -262,6 +262,42 @@ size_t btrfs_fscrypt_extent_context_size(struct btrfs_inode *inode)
 		fscrypt_extent_context_size(&inode->vfs_inode);
 }
 
+void btrfs_set_bio_crypt_ctx_from_extent(struct bio *bio,
+					 struct btrfs_inode *inode,
+					 struct fscrypt_extent_info *fi,
+					 u64 logical_offset)
+{
+	if (!fi)
+		return;
+
+	/*
+	 * fscrypt uses bytes >> s_blocksize_bits for the block numbers, so we
+	 * have to adjust everything based on our sectorsize so that the DUN
+	 * calculations are correct.
+	 */
+	logical_offset = div64_u64(logical_offset,
+				   inode->root->fs_info->sectorsize);
+	fscrypt_set_bio_crypt_ctx_from_extent(bio, &inode->vfs_inode, fi,
+					      logical_offset, GFP_NOFS);
+}
+
+bool btrfs_mergeable_encrypted_bio(struct bio *bio, struct inode *inode,
+				   struct fscrypt_extent_info *fi,
+				   u64 logical_offset)
+{
+	if (!fi)
+		return true;
+
+	/*
+	 * fscrypt uses bytes >> s_blocksize_bits for the block numbers, so we
+	 * have to adjust everything based on our sectorsize so that the DUN
+	 * calculations are correct.
+	 */
+	logical_offset = div64_u64(logical_offset,
+				   BTRFS_I(inode)->root->fs_info->sectorsize);
+	return fscrypt_mergeable_extent_bio(bio, inode, fi, logical_offset);
+}
+
 const struct fscrypt_operations btrfs_fscrypt_ops = {
 	.has_per_extent_encryption = 1,
 	.get_context = btrfs_fscrypt_get_context,
diff --git a/fs/btrfs/fscrypt.h b/fs/btrfs/fscrypt.h
index 2882a4a9d978..756375ade0b6 100644
--- a/fs/btrfs/fscrypt.h
+++ b/fs/btrfs/fscrypt.h
@@ -28,6 +28,13 @@ int btrfs_fscrypt_save_extent_info(struct btrfs_inode *inode,
 				   struct btrfs_path *path,
 				   struct fscrypt_extent_info *fi);
 size_t btrfs_fscrypt_extent_context_size(struct btrfs_inode *inode);
+void btrfs_set_bio_crypt_ctx_from_extent(struct bio *bio,
+					 struct btrfs_inode *inode,
+					 struct fscrypt_extent_info *fi,
+					 u64 logical_offset);
+bool btrfs_mergeable_encrypted_bio(struct bio *bio, struct inode *inode,
+				   struct fscrypt_extent_info *fi,
+				   u64 logical_offset);
 
 #else
 static inline int btrfs_fscrypt_save_extent_info(struct btrfs_inode *inode,
@@ -66,6 +73,21 @@ static inline size_t btrfs_fscrypt_extent_context_size(struct btrfs_inode *inode
 {
 	return 0;
 }
+
+static inline void btrfs_set_bio_crypt_ctx_from_extent(struct bio *bio,
+						       struct btrfs_inode *inode,
+						       struct fscrypt_extent_info *fi,
+						       u64 logical_offset)
+{
+}
+
+static inline bool btrfs_mergeable_encrypted_bio(struct bio *bio,
+						 struct inode *inode,
+						 struct fscrypt_extent_info *fi,
+						 u64 logical_offset)
+{
+	return true;
+}
 #endif /* CONFIG_FS_ENCRYPTION */
 
 extern const struct fscrypt_operations btrfs_fscrypt_ops;
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 3dce53601915..c5878da937d3 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -7932,6 +7932,8 @@ static void btrfs_dio_submit_io(const struct iomap_iter *iter, struct bio *bio,
 	struct btrfs_dio_private *dip =
 		container_of(bbio, struct btrfs_dio_private, bbio);
 	struct btrfs_dio_data *dio_data = iter->private;
+	struct fscrypt_extent_info *fscrypt_info = NULL;
+	u64 offset = 0;
 
 	btrfs_bio_init(bbio, BTRFS_I(iter->inode)->root->fs_info,
 		       btrfs_dio_end_io, bio->bi_private);
@@ -7953,6 +7955,9 @@ static void btrfs_dio_submit_io(const struct iomap_iter *iter, struct bio *bio,
 	if (iter->flags & IOMAP_WRITE) {
 		int ret;
 
+		offset = file_offset - dio_data->ordered->orig_offset;
+		fscrypt_info = dio_data->ordered->fscrypt_info;
+
 		ret = btrfs_extract_ordered_extent(bbio, dio_data->ordered);
 		if (ret) {
 			btrfs_finish_ordered_extent(dio_data->ordered, NULL,
@@ -7962,8 +7967,13 @@ static void btrfs_dio_submit_io(const struct iomap_iter *iter, struct bio *bio,
 			iomap_dio_bio_end_io(bio);
 			return;
 		}
+	} else {
+		fscrypt_info = dio_data->fscrypt_info;
+		offset = file_offset - dio_data->orig_start;
 	}
 
+	btrfs_set_bio_crypt_ctx_from_extent(&bbio->bio, bbio->inode,
+					    fscrypt_info, offset);
 	btrfs_submit_bio(bbio, 0);
 }
 
-- 
2.41.0


  parent reply	other threads:[~2023-12-01 22:12 UTC|newest]

Thread overview: 70+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-01 22:10 [PATCH v4 00/46] btrfs: add fscrypt support Josef Bacik
2023-12-01 22:10 ` [PATCH v4 01/46] fs: move fscrypt keyring destruction to after ->put_super Josef Bacik
2023-12-05  1:58   ` Eric Biggers
2023-12-05 22:48     ` Josef Bacik
2023-12-06  0:01       ` Eric Biggers
2023-12-01 22:10 ` [PATCH v4 02/46] fscrypt: add per-extent encryption support Josef Bacik
2023-12-05  3:58   ` Eric Biggers
2023-12-05 22:48     ` Josef Bacik
2023-12-05 23:57       ` Eric Biggers
2023-12-13  4:16     ` Eric Biggers
2023-12-01 22:11 ` [PATCH v4 03/46] fscrypt: add a fscrypt_inode_open helper Josef Bacik
2023-12-05  4:14   ` Eric Biggers
2023-12-01 22:11 ` [PATCH v4 04/46] fscrypt: conditionally don't wipe mk secret until the last active user is done Josef Bacik
2023-12-01 22:11 ` [PATCH v4 05/46] blk-crypto: add a process bio callback Josef Bacik
2023-12-05  4:54   ` Eric Biggers
2023-12-01 22:11 ` [PATCH v4 06/46] fscrypt: expose fscrypt_nokey_name Josef Bacik
2023-12-05  5:03   ` Eric Biggers
2023-12-01 22:11 ` [PATCH v4 07/46] fscrypt: add documentation about extent encryption Josef Bacik
2023-12-01 22:11 ` [PATCH v4 08/46] btrfs: add infrastructure for safe em freeing Josef Bacik
2023-12-01 22:11 ` [PATCH v4 09/46] btrfs: disable various operations on encrypted inodes Josef Bacik
2023-12-01 22:11 ` [PATCH v4 10/46] btrfs: disable verity " Josef Bacik
2023-12-05  5:07   ` Eric Biggers
2023-12-01 22:11 ` [PATCH v4 11/46] btrfs: start using fscrypt hooks Josef Bacik
2023-12-01 22:11 ` [PATCH v4 12/46] btrfs: add inode encryption contexts Josef Bacik
2023-12-05  5:22   ` Eric Biggers
2023-12-01 22:11 ` [PATCH v4 13/46] btrfs: add new FEATURE_INCOMPAT_ENCRYPT flag Josef Bacik
2023-12-01 22:11 ` [PATCH v4 14/46] btrfs: adapt readdir for encrypted and nokey names Josef Bacik
2023-12-01 22:11 ` [PATCH v4 15/46] btrfs: handle " Josef Bacik
2023-12-05  5:29   ` Eric Biggers
2023-12-01 22:11 ` [PATCH v4 16/46] btrfs: implement fscrypt ioctls Josef Bacik
2023-12-01 22:11 ` [PATCH v4 17/46] btrfs: add encryption to CONFIG_BTRFS_DEBUG Josef Bacik
2023-12-05  5:11   ` Eric Biggers
2023-12-01 22:11 ` [PATCH v4 18/46] btrfs: add get_devices hook for fscrypt Josef Bacik
2023-12-01 22:11 ` [PATCH v4 19/46] btrfs: turn on inlinecrypt mount option for encrypt Josef Bacik
2023-12-05  5:41   ` Eric Biggers
2023-12-01 22:11 ` [PATCH v4 20/46] btrfs: set file extent encryption excplicitly Josef Bacik
2023-12-01 22:11 ` [PATCH v4 21/46] btrfs: add fscrypt_info and encryption_type to extent_map Josef Bacik
2023-12-01 22:11 ` [PATCH v4 22/46] btrfs: add fscrypt_info and encryption_type to ordered_extent Josef Bacik
2023-12-01 22:11 ` [PATCH v4 23/46] btrfs: plumb through setting the fscrypt_info for ordered extents Josef Bacik
2023-12-01 22:11 ` [PATCH v4 24/46] btrfs: plumb the fscrypt extent context through create_io_em Josef Bacik
2023-12-01 22:11 ` [PATCH v4 25/46] btrfs: populate the ordered_extent with the fscrypt context Josef Bacik
2023-12-01 22:11 ` [PATCH v4 26/46] btrfs: keep track of fscrypt info and orig_start for dio reads Josef Bacik
2023-12-05  5:44   ` Eric Biggers
2023-12-01 22:11 ` [PATCH v4 27/46] btrfs: add an optional encryption context to the end of file extents Josef Bacik
2023-12-01 22:11 ` [PATCH v4 28/46] btrfs: explicitly track file extent length for replace and drop Josef Bacik
2023-12-01 22:11 ` [PATCH v4 29/46] btrfs: pass through fscrypt_extent_info to the file extent helpers Josef Bacik
2023-12-01 22:11 ` [PATCH v4 30/46] btrfs: pass the fscrypt_info through the replace extent infrastructure Josef Bacik
2023-12-01 22:11 ` [PATCH v4 31/46] btrfs: implement the fscrypt extent encryption hooks Josef Bacik
2023-12-01 22:11 ` [PATCH v4 32/46] btrfs: setup fscrypt_extent_info for new extents Josef Bacik
2023-12-01 22:11 ` [PATCH v4 33/46] btrfs: populate ordered_extent with the orig offset Josef Bacik
2023-12-01 22:11 ` Josef Bacik [this message]
2023-12-01 22:11 ` [PATCH v4 35/46] btrfs: add a bio argument to btrfs_csum_one_bio Josef Bacik
2023-12-01 22:11 ` [PATCH v4 36/46] btrfs: add orig_logical to btrfs_bio Josef Bacik
2023-12-01 22:11 ` [PATCH v4 37/46] btrfs: limit encrypted writes to 256 segments Josef Bacik
2023-12-01 22:11 ` [PATCH v4 38/46] btrfs: implement process_bio cb for fscrypt Josef Bacik
2023-12-01 22:11 ` [PATCH v4 39/46] btrfs: add test_dummy_encryption support Josef Bacik
2023-12-01 22:11 ` [PATCH v4 40/46] btrfs: don't rewrite ret from inode_permission Josef Bacik
2023-12-01 22:11 ` [PATCH v4 41/46] btrfs: move inode_to_path higher in backref.c Josef Bacik
2023-12-01 22:11 ` [PATCH v4 42/46] btrfs: make btrfs_ref_to_path handle encrypted filenames Josef Bacik
2023-12-01 22:11 ` [PATCH v4 43/46] btrfs: don't search back for dir inode item in INO_LOOKUP_USER Josef Bacik
2023-12-01 22:11 ` [PATCH v4 44/46] btrfs: deal with encrypted symlinks in send Josef Bacik
2023-12-01 22:11 ` [PATCH v4 45/46] btrfs: decrypt file names for send Josef Bacik
2023-12-01 22:11 ` [PATCH v4 46/46] btrfs: load the inode context before sending writes Josef Bacik
2023-12-05  5:54   ` Eric Biggers
2023-12-01 22:15 ` [PATCH v4 00/46] btrfs: add fscrypt support Josef Bacik
2023-12-05  1:49 ` Eric Biggers
2023-12-05 14:16   ` David Sterba
2023-12-05 20:02     ` Eric Biggers
2024-04-09 23:42 ` Eric Biggers
2024-04-11 18:45   ` Josef Bacik

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cfb67d4a11cdc6c9d0685b5c505b1170bb7b9bb4.1701468306.git.josef@toxicpanda.com \
    --to=josef@toxicpanda.com \
    --cc=kernel-team@fb.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.