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 39/46] btrfs: add test_dummy_encryption support
Date: Fri,  1 Dec 2023 17:11:36 -0500	[thread overview]
Message-ID: <b50d128bfbae6011473f2eec74fdf7c7e68ea6b7.1701468306.git.josef@toxicpanda.com> (raw)
In-Reply-To: <cover.1701468305.git.josef@toxicpanda.com>

In order to enable more thorough testing of fscrypt enable the
test_dummy_encryption mount option.  This is used by fscrypt users to
easily enable fscrypt on the file system for testing without needing to
do the key setup and everything.

The only deviation from other file systems we make is we only support
the fsparam_flag version of this mount option, as it defaults to v2.  We
don't want to have to bother with rejecting v1 related mount options.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 fs/btrfs/disk-io.c |  1 +
 fs/btrfs/fs.h      |  3 +++
 fs/btrfs/fscrypt.c |  6 +++++
 fs/btrfs/super.c   | 58 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 68 insertions(+)

diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 719245d73b99..aedd8a0e4962 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -1272,6 +1272,7 @@ void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
 	kfree(fs_info->super_copy);
 	kfree(fs_info->super_for_commit);
 	kfree(fs_info->subpage_info);
+	fscrypt_free_dummy_policy(&fs_info->dummy_enc_policy);
 	kvfree(fs_info);
 }
 
diff --git a/fs/btrfs/fs.h b/fs/btrfs/fs.h
index 1340e71d026c..74752204f3ab 100644
--- a/fs/btrfs/fs.h
+++ b/fs/btrfs/fs.h
@@ -7,6 +7,7 @@
 #include <linux/fs.h>
 #include <linux/btrfs_tree.h>
 #include <linux/sizes.h>
+#include <linux/fscrypt.h>
 #include "extent-io-tree.h"
 #include "extent_map.h"
 #include "async-thread.h"
@@ -189,6 +190,7 @@ enum {
 	BTRFS_MOUNT_IGNOREDATACSUMS		= (1UL << 28),
 	BTRFS_MOUNT_NODISCARD			= (1UL << 29),
 	BTRFS_MOUNT_NOSPACECACHE		= (1UL << 30),
+	BTRFS_MOUNT_TEST_DUMMY_ENCRYPTION	= (1UL << 31),
 };
 
 /*
@@ -828,6 +830,7 @@ struct btrfs_fs_info {
 	spinlock_t eb_leak_lock;
 	struct list_head allocated_ebs;
 #endif
+	struct fscrypt_dummy_policy dummy_enc_policy;
 };
 
 static inline u64 btrfs_get_fs_generation(const struct btrfs_fs_info *fs_info)
diff --git a/fs/btrfs/fscrypt.c b/fs/btrfs/fscrypt.c
index 85b596711371..2fbceec62dc7 100644
--- a/fs/btrfs/fscrypt.c
+++ b/fs/btrfs/fscrypt.c
@@ -242,6 +242,11 @@ static blk_status_t btrfs_process_encrypted_bio(struct bio *orig_bio,
 	return btrfs_csum_one_bio(bbio, enc_bio);
 }
 
+static const union fscrypt_policy *btrfs_get_dummy_policy(struct super_block *sb)
+{
+	return btrfs_sb(sb)->dummy_enc_policy.policy;
+}
+
 int btrfs_fscrypt_load_extent_info(struct btrfs_inode *inode,
 				   struct extent_map *em,
 				   struct btrfs_fscrypt_ctx *ctx)
@@ -357,4 +362,5 @@ const struct fscrypt_operations btrfs_fscrypt_ops = {
 	.empty_dir = btrfs_fscrypt_empty_dir,
 	.get_devices = btrfs_fscrypt_get_devices,
 	.process_bio = btrfs_process_encrypted_bio,
+	.get_dummy_policy = btrfs_get_dummy_policy,
 };
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 462a23db26af..2a7789519a6c 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -88,6 +88,7 @@ struct btrfs_fs_context {
 	unsigned long compress_type:4;
 	unsigned int compress_level;
 	refcount_t refs;
+	struct fscrypt_dummy_policy dummy_enc_policy;
 };
 
 enum {
@@ -122,6 +123,7 @@ enum {
 	Opt_thread_pool,
 	Opt_treelog,
 	Opt_user_subvol_rm_allowed,
+	Opt_test_dummy_encryption,
 
 	/* Rescue options */
 	Opt_rescue,
@@ -255,6 +257,13 @@ static const struct fs_parameter_spec btrfs_fs_parameters[] = {
 	fsparam_flag_no("enospc_debug", Opt_enospc_debug),
 #ifdef CONFIG_BTRFS_DEBUG
 	fsparam_enum("fragment", Opt_fragment, btrfs_parameter_fragment),
+
+	/*
+	 * With -o test_dummy_encryption we default to v2, only allow the flag
+	 * version, since we don't support v1.
+	 */
+	fsparam_flag("test_dummy_encryption", Opt_test_dummy_encryption),
+
 #endif
 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
 	fsparam_flag("ref_verify", Opt_ref_verify),
@@ -268,6 +277,7 @@ static int btrfs_parse_param(struct fs_context *fc,
 	struct btrfs_fs_context *ctx = fc->fs_private;
 	struct fs_parse_result result;
 	int opt;
+	int ret;
 
 	opt = fs_parse(fc, btrfs_fs_parameters, param, &result);
 	if (opt < 0)
@@ -598,6 +608,13 @@ static int btrfs_parse_param(struct fs_context *fc,
 			return -EINVAL;
 		}
 		break;
+	case Opt_test_dummy_encryption:
+		btrfs_set_opt(ctx->mount_opt, TEST_DUMMY_ENCRYPTION);
+		ret = fscrypt_parse_test_dummy_encryption(param,
+							  &ctx->dummy_enc_policy);
+		if (ret)
+			return ret;
+		break;
 #endif
 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
 	case Opt_ref_verify:
@@ -946,6 +963,9 @@ static int btrfs_fill_super(struct super_block *sb,
 		return err;
 	}
 
+	if (fscrypt_is_dummy_policy_set(&fs_info->dummy_enc_policy))
+		btrfs_set_fs_incompat(fs_info, ENCRYPT);
+
 	if (btrfs_fs_incompat(fs_info, ENCRYPT)) {
 		if (IS_ENABLED(CONFIG_FS_ENCRYPTION_INLINE_CRYPT)) {
 			sb->s_flags |= SB_INLINECRYPT;
@@ -1112,6 +1132,9 @@ static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
 #endif
 	if (btrfs_test_opt(info, REF_VERIFY))
 		seq_puts(seq, ",ref_verify");
+	if (btrfs_test_opt(info, TEST_DUMMY_ENCRYPTION))
+		fscrypt_show_test_dummy_encryption(seq, ',', dentry->d_sb);
+
 	seq_printf(seq, ",subvolid=%llu",
 		  BTRFS_I(d_inode(dentry))->root->root_key.objectid);
 	subvol_name = btrfs_get_subvol_name_from_objectid(info,
@@ -1384,6 +1407,18 @@ static void btrfs_ctx_to_info(struct btrfs_fs_info *fs_info,
 	fs_info->mount_opt = ctx->mount_opt;
 	fs_info->compress_type = ctx->compress_type;
 	fs_info->compress_level = ctx->compress_level;
+
+	/*
+	 * If there's nothing set, or if the fs_info already has one set, don't
+	 * do anything.  If the fs_info is set we'll free the dummy one when we
+	 * free the ctx.
+	 */
+	if (!fscrypt_is_dummy_policy_set(&ctx->dummy_enc_policy) ||
+	    fscrypt_is_dummy_policy_set(&fs_info->dummy_enc_policy))
+		return;
+
+	fs_info->dummy_enc_policy = ctx->dummy_enc_policy;
+	memset(&ctx->dummy_enc_policy, 0, sizeof(ctx->dummy_enc_policy));
 }
 
 static void btrfs_info_to_ctx(struct btrfs_fs_info *fs_info,
@@ -1436,6 +1471,7 @@ static void btrfs_emit_options(struct btrfs_fs_info *info,
 	btrfs_info_if_set(info, old, USEBACKUPROOT, "trying to use backup root at mount time");
 	btrfs_info_if_set(info, old, IGNOREBADROOTS, "ignoring bad roots");
 	btrfs_info_if_set(info, old, IGNOREDATACSUMS, "ignoring data csums");
+	btrfs_info_if_set(info, old, TEST_DUMMY_ENCRYPTION, "test dummy encryption mode enabled");
 
 	btrfs_info_if_unset(info, old, NODATACOW, "setting datacow");
 	btrfs_info_if_unset(info, old, SSD, "not using ssd optimizations");
@@ -1467,6 +1503,23 @@ static void btrfs_emit_options(struct btrfs_fs_info *info,
 			   info->max_inline);
 }
 
+static bool btrfs_check_test_dummy_encryption(struct fs_context *fc)
+{
+	struct btrfs_fs_context *ctx = fc->fs_private;
+	struct btrfs_fs_info *fs_info = btrfs_sb(fc->root->d_sb);
+
+	if (!fscrypt_is_dummy_policy_set(&ctx->dummy_enc_policy))
+		return true;
+
+	if (fscrypt_dummy_policies_equal(&fs_info->dummy_enc_policy,
+					 &ctx->dummy_enc_policy))
+		return true;
+
+	btrfs_warn(fs_info,
+		   "Can't set or change test_dummy_encryption on remount");
+	return false;
+}
+
 static int btrfs_reconfigure(struct fs_context *fc)
 {
 	struct super_block *sb = fc->root->d_sb;
@@ -1485,6 +1538,10 @@ static int btrfs_reconfigure(struct fs_context *fc)
 	    !btrfs_check_options(fs_info, &ctx->mount_opt, fc->sb_flags))
 		return -EINVAL;
 
+	if (!mount_reconfigure &&
+	    !btrfs_check_test_dummy_encryption(fc))
+		return -EINVAL;
+
 	ret = btrfs_check_features(fs_info, !(fc->sb_flags & SB_RDONLY));
 	if (ret < 0)
 		return ret;
@@ -2121,6 +2178,7 @@ static void btrfs_free_fs_context(struct fs_context *fc)
 		btrfs_free_fs_info(fs_info);
 
 	if (ctx && refcount_dec_and_test(&ctx->refs)) {
+		fscrypt_free_dummy_policy(&ctx->dummy_enc_policy);
 		kfree(ctx->subvol_name);
 		kfree(ctx);
 	}
-- 
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 ` [PATCH v4 34/46] btrfs: set the bio fscrypt context when applicable Josef Bacik
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 ` Josef Bacik [this message]
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=b50d128bfbae6011473f2eec74fdf7c7e68ea6b7.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.