Messages in this thread Patch in this message | | | From | Mark Harmstone <> | Subject | [RFC PATCH 09/19] btrfs: add btrfs.key property | Date | Wed, 9 Jan 2019 01:26:51 +0000 |
| |
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
| |