public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Mark Harmstone <mark@harmstone.com>
To: unlisted-recipients:; (no To-header on input)
Cc: mark@harmstone.com, Chris Mason <clm@fb.com>,
	Josef Bacik <josef@toxicpanda.com>,
	David Sterba <dsterba@suse.com>,
	linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [RFC PATCH 09/19] btrfs: add btrfs.key property
Date: Wed,  9 Jan 2019 01:26:51 +0000	[thread overview]
Message-ID: <20190109012701.26441-9-mark@harmstone.com> (raw)
In-Reply-To: <20190109012701.26441-1-mark@harmstone.com>

Signed-off-by: Mark Harmstone <mark@harmstone.com>
---
 fs/btrfs/btrfs_inode.h |  5 ++++
 fs/btrfs/ctree.h       |  1 +
 fs/btrfs/inode.c       |  1 +
 fs/btrfs/props.c       | 68 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 75 insertions(+)

diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h
index 97d91e55b70a..cf9f50bbe4ef 100644
--- a/fs/btrfs/btrfs_inode.h
+++ b/fs/btrfs/btrfs_inode.h
@@ -175,6 +175,11 @@ struct btrfs_inode {
 	 */
 	unsigned defrag_compress;
 
+	/*
+	 * Salt of preferred encryption key
+	 */
+	u64 prop_key;
+
 	struct btrfs_delayed_node *delayed_node;
 
 	/* File creation time. */
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 4bab57e01e21..a1368c5c1236 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -1453,6 +1453,7 @@ do {                                                                   \
 #define BTRFS_INODE_NOATIME		(1 << 9)
 #define BTRFS_INODE_DIRSYNC		(1 << 10)
 #define BTRFS_INODE_COMPRESS		(1 << 11)
+#define BTRFS_INODE_ENCRYPT		(1 << 12)
 
 #define BTRFS_INODE_ROOT_ITEM_INIT	(1 << 31)
 
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 9ea4c6f0352f..1d1084cf9289 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -9167,6 +9167,7 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
 	ei->runtime_flags = 0;
 	ei->prop_compress = BTRFS_COMPRESS_NONE;
 	ei->defrag_compress = BTRFS_COMPRESS_NONE;
+	ei->prop_key = 0;
 
 	ei->delayed_node = NULL;
 
diff --git a/fs/btrfs/props.c b/fs/btrfs/props.c
index e57a4edc88c4..ebe04194acbd 100644
--- a/fs/btrfs/props.c
+++ b/fs/btrfs/props.c
@@ -29,6 +29,12 @@ static int prop_compression_apply(struct inode *inode,
 				  size_t len);
 static int prop_compression_extract(struct inode *inode, char *value);
 
+static int prop_key_validate(const char *value, size_t len);
+static int prop_key_apply(struct inode *inode,
+			  const char *value,
+			  size_t len);
+static int prop_key_extract(struct inode *inode, char *value);
+
 static struct prop_handler prop_handlers[] = {
 	{
 		.xattr_name = XATTR_BTRFS_PREFIX "compression",
@@ -37,6 +43,13 @@ static struct prop_handler prop_handlers[] = {
 		.extract = prop_compression_extract,
 		.inheritable = 1
 	},
+	{
+		.xattr_name = XATTR_BTRFS_PREFIX "key",
+		.validate = prop_key_validate,
+		.apply = prop_key_apply,
+		.extract = prop_key_extract,
+		.inheritable = 1
+	},
 };
 
 void __init btrfs_props_init(void)
@@ -435,4 +448,59 @@ static int prop_compression_extract(struct inode *inode, char *value)
 	return -EINVAL;
 }
 
+static int prop_key_validate(const char *value, size_t len)
+{
+	unsigned int i;
+
+	if (len != 16)
+		return -EINVAL;
+
+	for (i = 0; i < 16; i++) {
+		if (hex_to_bin(value[i]) == -1)
+			return -EINVAL;
+	}
 
+	return 0;
+}
+
+static int prop_key_apply(struct inode *inode,
+			  const char *value,
+			  size_t len)
+{
+	u64 salt;
+
+	if (len == 0) {
+		BTRFS_I(inode)->flags &= ~BTRFS_INODE_ENCRYPT;
+		BTRFS_I(inode)->prop_key = 0;
+
+		return 0;
+	}
+
+	if (len != 16)
+		return -EINVAL;
+
+	if (hex2bin((u8 *)&salt, value, len / 2))
+		return -EINVAL;
+
+	salt = be64_to_cpu(salt);
+
+	BTRFS_I(inode)->flags |= BTRFS_INODE_ENCRYPT;
+	BTRFS_I(inode)->prop_key = salt;
+
+	return 0;
+}
+
+static int prop_key_extract(struct inode *inode, char *value)
+{
+	u64 salt;
+
+	if (BTRFS_I(inode)->prop_key == 0)
+		return -EINVAL;
+
+	salt = cpu_to_be64(BTRFS_I(inode)->prop_key);
+	bin2hex(value, &salt, sizeof(salt));
+
+	value[16] = 0;
+
+	return 0;
+}
-- 
2.19.2


  parent reply	other threads:[~2019-01-09  1:29 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-09  1:26 [RFC PATCH 01/19] btrfs: add encryption structs and constants Mark Harmstone
2019-01-09  1:26 ` [RFC PATCH 02/19] btrfs: add encryption dependencies to Kconfig Mark Harmstone
2019-01-09  1:26 ` [RFC PATCH 03/19] btrfs: load key tree Mark Harmstone
2019-01-09  1:26 ` [RFC PATCH 04/19] btrfs: allow encrypted volumes to be mounted Mark Harmstone
2019-01-09  1:26 ` [RFC PATCH 05/19] btrfs: add key list Mark Harmstone
2019-01-09  1:26 ` [RFC PATCH 06/19] btrfs: add ioctl BTRFS_IOC_GET_KEY_SALT Mark Harmstone
2019-01-09  1:26 ` [RFC PATCH 07/19] btrfs: add new keys to key root when flushed Mark Harmstone
2019-01-09  1:26 ` [RFC PATCH 08/19] btrfs: change extract in prop_handler to write into string Mark Harmstone
2019-01-09  1:26 ` Mark Harmstone [this message]
2019-01-09  1:26 ` [RFC PATCH 10/19] btrfs: allow reading encrypted inline extents Mark Harmstone
2019-01-09  1:26 ` [RFC PATCH 11/19] btrfs: allow writing " Mark Harmstone
2019-01-09  1:26 ` [RFC PATCH 12/19] btrfs: allow reading normal encrypted extents Mark Harmstone
2019-01-09  1:26 ` [RFC PATCH 13/19] btrfs: allow writing normal and compressed " Mark Harmstone
2019-01-09  1:26 ` [RFC PATCH 14/19] btrfs: allow reading " Mark Harmstone
2019-01-09  1:26 ` [RFC PATCH 15/19] btrfs: allow writing compressed, encrypted, inline extents Mark Harmstone
2019-01-09  1:26 ` [RFC PATCH 16/19] btrfs: add encryption incompat flag to sysfs Mark Harmstone
2019-01-09  1:26 ` [RFC PATCH 17/19] btrfs: don't allow direct IO of encrypted extents Mark Harmstone
2019-01-09  1:27 ` [RFC PATCH 18/19] btrfs: return encrypted flag to statx Mark Harmstone
2019-01-09  1:27 ` [RFC PATCH 19/19] btrfs: translate encryption flag to FS_ENCRYPT_FL Mark Harmstone

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=20190109012701.26441-9-mark@harmstone.com \
    --to=mark@harmstone.com \
    --cc=clm@fb.com \
    --cc=dsterba@suse.com \
    --cc=josef@toxicpanda.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox