Skip to contentSkip to conversation

btrfs-progs: fscrypt updates

PATCH In review Daniel Vacek 4 participants 16 messages Jul 7 – Jul 11

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover btrfs-progs: fscrypt updates

This series is a rebase of an older set of fscrypt related changes from Sweet Tea Dorminy and Josef Bacik found here:
https://github.com/josefbacik/btrfs-progs/tree/fscrypt

It passed all my tests. Hopefully nothing blows. Enjoy testing.

v3:
* dropped first patch and improved inline extent length checking
* correctly squashed the context key definitions into "btrfs-progs: add inode encryption contexts"
* inline extents also show the encryption field now in tree dump

v2: https://lore.kernel.org/linux-btrfs/[email protected]/
* works with v7 of the kernel fscrypt series
* the on-disk format changed and parts of the series had to be reworked
- particularly the encryption context is now stored as dedicated item and not glued onto extent data item
* also parses the ENCRYPT inode item flag

Daniel Vacek (1):
btrfs-progs: recognize ENCRYPT inode item flag

Sweet Tea Dorminy (6):
btrfs-progs: add new FEATURE_INCOMPAT_ENCRYPT flag
btrfs-progs: start tracking extent encryption context info
btrfs-progs: add inode encryption contexts
btrfs-progs: print encryptin type field of file extents
btrfs-progs: handle fscrypt context items
btrfs-progs: check: update inline extent length checking

 check/main.c                    | 34 ++++++++++++++++++---------------
 kernel-shared/ctree.h           |  1 +
 kernel-shared/print-tree.c      | 28 +++++++++++++++++++++++++--
 kernel-shared/tree-checker.c    | 17 ++++++++++-------
 kernel-shared/uapi/btrfs.h      |  1 +
 kernel-shared/uapi/btrfs_tree.h | 11 +++++++++++
 libbtrfsutil/btrfs.h            |  1 +
 7 files changed, 69 insertions(+), 24 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH v3 1/7 btrfs-progs: add new FEATURE_INCOMPAT_ENCRYPT flag

From: Sweet Tea Dorminy <[email protected]>

Matches kernel change by the same name.

---
 kernel-shared/ctree.h      | 1 +
 kernel-shared/uapi/btrfs.h | 1 +
 libbtrfsutil/btrfs.h       | 1 +
 3 files changed, 3 insertions(+)
kernel-shared/ctree.h +1 -0
index cb5a5f3f..177163f8 100644
@@ -99,6 +99,7 @@ static inline u32 __BTRFS_LEAF_DATA_SIZE(u32 nodesize)
 	 BTRFS_FEATURE_INCOMPAT_ZONED |			\
 	 BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2 |	\
 	 BTRFS_FEATURE_INCOMPAT_RAID_STRIPE_TREE |	\
+	 BTRFS_FEATURE_INCOMPAT_ENCRYPT |		\
 	 BTRFS_FEATURE_INCOMPAT_SIMPLE_QUOTA |		\
 	 BTRFS_FEATURE_INCOMPAT_REMAP_TREE)
 #else
kernel-shared/uapi/btrfs.h +1 -0
index a765fbc4..7144f3ba 100644
@@ -359,6 +359,7 @@ _static_assert(sizeof(struct btrfs_ioctl_fs_info_args) == 1024);
 #define BTRFS_FEATURE_INCOMPAT_ZONED		(1ULL << 12)
 #define BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2	(1ULL << 13)
 #define BTRFS_FEATURE_INCOMPAT_RAID_STRIPE_TREE (1ULL << 14)
+#define BTRFS_FEATURE_INCOMPAT_ENCRYPT		(1ULL << 15)
 #define BTRFS_FEATURE_INCOMPAT_SIMPLE_QUOTA	(1ULL << 16)
 #define BTRFS_FEATURE_INCOMPAT_REMAP_TREE	(1ULL << 17)
 
libbtrfsutil/btrfs.h +1 -0
index 47d9ebf8..eac42473 100644
@@ -328,6 +328,7 @@ struct btrfs_ioctl_fs_info_args {
 #define BTRFS_FEATURE_INCOMPAT_ZONED		(1ULL << 12)
 #define BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2	(1ULL << 13)
 #define BTRFS_FEATURE_INCOMPAT_RAID_STRIPE_TREE (1ULL << 14)
+#define BTRFS_FEATURE_INCOMPAT_ENCRYPT		(1ULL << 15)
 #define BTRFS_FEATURE_INCOMPAT_SIMPLE_QUOTA	(1ULL << 16)
 
 struct btrfs_ioctl_feature_flags {
Signature · 1 line
2.53.0
#2 lore raw

PATCH v3 2/7 btrfs-progs: start tracking extent encryption context info

In reply to Daniel Vacek

From: Sweet Tea Dorminy <[email protected]>

This recapitulates the kernel change named 'btrfs: start tracking extent encryption context info".

---
 kernel-shared/tree-checker.c    | 17 ++++++++++-------
 kernel-shared/uapi/btrfs_tree.h |  6 ++++++
 2 files changed, 16 insertions(+), 7 deletions(-)
kernel-shared/tree-checker.c +10 -7
index 5e716422..cd3d5d27 100644
@@ -239,6 +239,8 @@ static int check_extent_data_item(struct extent_buffer *leaf,
 	u32 sectorsize = fs_info->sectorsize;
 	u32 item_size = btrfs_item_size(leaf, slot);
 	u64 extent_end;
+	u8 policy;
+	u8 fe_type;
 
 	if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
 		file_extent_err(leaf, slot,
@@ -269,12 +271,12 @@ static int check_extent_data_item(struct extent_buffer *leaf,
 				SZ_4K);
 		return -EUCLEAN;
 	}
-	if (unlikely(btrfs_file_extent_type(leaf, fi) >=
-		     BTRFS_NR_FILE_EXTENT_TYPES)) {
+
+	fe_type = btrfs_file_extent_type(leaf, fi);
+	if (unlikely(fe_type >= BTRFS_NR_FILE_EXTENT_TYPES)) {
 		file_extent_err(leaf, slot,
 		"invalid type for file extent, have %u expect range [0, %u]",
-			btrfs_file_extent_type(leaf, fi),
-			BTRFS_NR_FILE_EXTENT_TYPES - 1);
+			fe_type, BTRFS_NR_FILE_EXTENT_TYPES - 1);
 		return -EUCLEAN;
 	}
 
@@ -290,10 +292,11 @@ static int check_extent_data_item(struct extent_buffer *leaf,
 			BTRFS_NR_COMPRESS_TYPES - 1);
 		return -EUCLEAN;
 	}
-	if (unlikely(btrfs_file_extent_encryption(leaf, fi))) {
+	policy = btrfs_file_extent_encryption(leaf, fi);
+	if (unlikely(policy >= BTRFS_NR_ENCRYPTION_TYPES)) {
 		file_extent_err(leaf, slot,
-			"invalid encryption for file extent, have %u expect 0",
-			btrfs_file_extent_encryption(leaf, fi));
+			"invalid encryption for file extent, have %u expect range [0, %u]",
+			policy, BTRFS_NR_ENCRYPTION_TYPES - 1);
 		return -EUCLEAN;
 	}
 	if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
kernel-shared/uapi/btrfs_tree.h +6 -0
index 1ca63711..74d307f1 100644
@@ -1072,6 +1072,12 @@ enum {
 	BTRFS_NR_FILE_EXTENT_TYPES = 3,
 };
 
+enum {
+	BTRFS_ENCRYPTION_NONE,
+	BTRFS_ENCRYPTION_FSCRYPT,
+	BTRFS_NR_ENCRYPTION_TYPES,
+};
+
 struct btrfs_file_extent_item {
 	/*
 	 * transaction id that created this extent
Signature · 1 line
2.53.0
#3 lore raw

PATCH v3 3/7 btrfs-progs: add inode encryption contexts

In reply to Daniel Vacek

From: Sweet Tea Dorminy <[email protected]>

Recapitulates relevant parts of kernel change 'btrfs: add inode encryption contexts'.

---
 kernel-shared/uapi/btrfs_tree.h | 5 +++++
 1 file changed, 5 insertions(+)
kernel-shared/uapi/btrfs_tree.h +5 -0
index 74d307f1..b4532237 100644
@@ -168,6 +168,9 @@
 #define BTRFS_VERITY_DESC_ITEM_KEY	36
 #define BTRFS_VERITY_MERKLE_ITEM_KEY	37
 
+#define BTRFS_FSCRYPT_INODE_CTX_KEY	41
+#define BTRFS_FSCRYPT_CTX_KEY		42
+
 #define BTRFS_ORPHAN_ITEM_KEY		48
 /* reserve 2-15 close to the inode for later flexibility */
 
@@ -428,6 +431,7 @@ static inline __u8 btrfs_dir_flags_to_ftype(__u8 flags)
 #define BTRFS_INODE_NOATIME		(1U << 9)
 #define BTRFS_INODE_DIRSYNC		(1U << 10)
 #define BTRFS_INODE_COMPRESS		(1U << 11)
+#define BTRFS_INODE_ENCRYPT		(1U << 12)
 
 #define BTRFS_INODE_ROOT_ITEM_INIT	(1U << 31)
 
@@ -444,6 +448,7 @@ static inline __u8 btrfs_dir_flags_to_ftype(__u8 flags)
 	 BTRFS_INODE_NOATIME |						\
 	 BTRFS_INODE_DIRSYNC |						\
 	 BTRFS_INODE_COMPRESS |						\
+	 BTRFS_INODE_ENCRYPT |						\
 	 BTRFS_INODE_ROOT_ITEM_INIT)
 
 #define BTRFS_INODE_RO_VERITY		(1U << 0)
Signature · 1 line
2.53.0
#4 lore raw

PATCH v3 4/7 btrfs-progs: print encryptin type field of file extents

In reply to Daniel Vacek

From: Sweet Tea Dorminy <[email protected]>

Encrypted file extents now have the 'encryption' field set to an encryption type. Let's print it.

---
 check/main.c               | 1 -
 kernel-shared/print-tree.c | 7 +++++--
 2 files changed, 5 insertions(+), 3 deletions(-)
check/main.c +0 -1
index 5e29e2c5..7f438302 100644
@@ -1778,7 +1778,6 @@ static int process_file_extent(struct btrfs_root *root,
 			rec->errors |= I_ERR_BAD_FILE_EXTENT;
 		if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
 		    (btrfs_file_extent_compression(eb, fi) ||
-		     btrfs_file_extent_encryption(eb, fi) ||
 		     btrfs_file_extent_other_encoding(eb, fi)))
 			rec->errors |= I_ERR_BAD_FILE_EXTENT;
 		if (compression && rec->nodatasum)
kernel-shared/print-tree.c +5 -2
index 0afa3696..2c0168b0 100644
@@ -445,11 +445,12 @@ static void print_file_extent_item(struct extent_buffer *eb,
 			extent_type, file_extent_type_to_str(extent_type));
 
 	if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
-		printf("\t\tinline extent data size %u ram_bytes %llu compression %hhu (%s)\n",
+		printf("\t\tinline extent data size %u ram_bytes %llu compression %hhu (%s) encryption %hhu\n",
 				btrfs_file_extent_inline_item_len(eb, slot),
 				btrfs_file_extent_ram_bytes(eb, fi),
 				btrfs_file_extent_compression(eb, fi),
-				compress_str);
+				compress_str,
+				btrfs_file_extent_encryption(eb, fi));
 		return;
 	}
 	if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
@@ -471,6 +472,8 @@ static void print_file_extent_item(struct extent_buffer *eb,
 	printf("\t\textent compression %hhu (%s)\n",
 			btrfs_file_extent_compression(eb, fi),
 			compress_str);
+	printf("\t\textent encryption %hhu\n",
+			btrfs_file_extent_encryption(eb, fi));
 }
 
 /* Caller should ensure sizeof(*ret) >= 16("DATA|TREE_BLOCK") */
Signature · 1 line
2.53.0
#5 lore raw

Re: [PATCH v3 4/7] btrfs-progs: print encryptin type field of file extents

Quoted 17 lines · Daniel Vacek

From: Sweet Tea Dorminy <[email protected]>

Encrypted file extents now have the 'encryption' field set to an encryption type. Let's print it.

---
  check/main.c               | 1 -
  kernel-shared/print-tree.c | 7 +++++--
  2 files changed, 5 insertions(+), 3 deletions(-)
check/main.c +0 -0
index 5e29e2c5..7f438302 100644

check/main.c static int process_file_extent(struct btrfs_root *root, · line 1781

Quoted 4 lines
check/main.c +0 -1
@@ -1778,7 +1778,6 @@ static int process_file_extent(struct btrfs_root *root,
  			rec->errors |= I_ERR_BAD_FILE_EXTENT;
  		if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
  		    (btrfs_file_extent_compression(eb, fi) ||
-		     btrfs_file_extent_encryption(eb, fi) ||

I think this is a leaf-over change?

Thanks,
Qu

Quoted 31 lines
check/main.c +0 -0
  		     btrfs_file_extent_other_encoding(eb, fi)))
  			rec->errors |= I_ERR_BAD_FILE_EXTENT;
  		if (compression && rec->nodatasum)
kernel-shared/print-tree.c +5 -2
index 0afa3696..2c0168b0 100644
@@ -445,11 +445,12 @@ static void print_file_extent_item(struct extent_buffer *eb,
  			extent_type, file_extent_type_to_str(extent_type));
 
  	if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
-		printf("\t\tinline extent data size %u ram_bytes %llu compression %hhu (%s)\n",
+		printf("\t\tinline extent data size %u ram_bytes %llu compression %hhu (%s) encryption %hhu\n",
  				btrfs_file_extent_inline_item_len(eb, slot),
  				btrfs_file_extent_ram_bytes(eb, fi),
  				btrfs_file_extent_compression(eb, fi),
-				compress_str);
+				compress_str,
+				btrfs_file_extent_encryption(eb, fi));
  		return;
  	}
  	if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
@@ -471,6 +472,8 @@ static void print_file_extent_item(struct extent_buffer *eb,
  	printf("\t\textent compression %hhu (%s)\n",
  			btrfs_file_extent_compression(eb, fi),
  			compress_str);
+	printf("\t\textent encryption %hhu\n",
+			btrfs_file_extent_encryption(eb, fi));
  }
 
  /* Caller should ensure sizeof(*ret) >= 16("DATA|TREE_BLOCK") */
#6 lore raw

Re: [PATCH v3 4/7] btrfs-progs: print encryptin type field of file extents

Quoted 13 lines · Qu Wenruo
Quoted 12 lines · Daniel Vacek

From: Sweet Tea Dorminy <[email protected]>

Encrypted file extents now have the 'encryption' field set to an encryption type. Let's print it.

---
  check/main.c               | 1 -
  kernel-shared/print-tree.c | 7 +++++--
  2 files changed, 5 insertions(+), 3 deletions(-)
Quoted 11 lines

check/main.c static int process_file_extent(struct btrfs_root *root, · line 1781

Quoted 9 lines
check/main.c +0 -1
index 5e29e2c5..7f438302 100644
@@ -1778,7 +1778,6 @@ static int process_file_extent(struct btrfs_root *root,
                      rec->errors |= I_ERR_BAD_FILE_EXTENT;
              if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
                  (btrfs_file_extent_compression(eb, fi) ||
-                  btrfs_file_extent_encryption(eb, fi) ||

I think this is a leaf-over change?

For now it is to match the kernel part to allow for wider testing in the open. If we decide to change the kernel we can revert this later.

--nX

Quoted 35 lines

Thanks,
Qu

Quoted 31 lines
check/main.c +0 -0
                   btrfs_file_extent_other_encoding(eb, fi)))
                      rec->errors |= I_ERR_BAD_FILE_EXTENT;
              if (compression && rec->nodatasum)
kernel-shared/print-tree.c +5 -2
index 0afa3696..2c0168b0 100644
@@ -445,11 +445,12 @@ static void print_file_extent_item(struct extent_buffer *eb,
                      extent_type, file_extent_type_to_str(extent_type));
 
      if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
-             printf("\t\tinline extent data size %u ram_bytes %llu compression %hhu (%s)\n",
+             printf("\t\tinline extent data size %u ram_bytes %llu compression %hhu (%s) encryption %hhu\n",
                              btrfs_file_extent_inline_item_len(eb, slot),
                              btrfs_file_extent_ram_bytes(eb, fi),
                              btrfs_file_extent_compression(eb, fi),
-                             compress_str);
+                             compress_str,
+                             btrfs_file_extent_encryption(eb, fi));
              return;
      }
      if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
@@ -471,6 +472,8 @@ static void print_file_extent_item(struct extent_buffer *eb,
      printf("\t\textent compression %hhu (%s)\n",
                      btrfs_file_extent_compression(eb, fi),
                      compress_str);
+     printf("\t\textent encryption %hhu\n",
+                     btrfs_file_extent_encryption(eb, fi));
  }
 
  /* Caller should ensure sizeof(*ret) >= 16("DATA|TREE_BLOCK") */
#7 lore raw

PATCH v3 5/7 btrfs-progs: handle fscrypt context items

In reply to Daniel Vacek

From: Sweet Tea Dorminy <[email protected]>

Encrypted inodes have a new associated item, the fscrypt context, which can be printed as a pure hex string in dump-tree.

---
 check/main.c               |  2 ++
 kernel-shared/print-tree.c | 20 ++++++++++++++++++++
 2 files changed, 22 insertions(+)
check/main.c +2 -0
index 7f438302..9447b01e 100644
@@ -1896,6 +1896,8 @@ static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb,
 			break;
 		case BTRFS_VERITY_DESC_ITEM_KEY:
 		case BTRFS_VERITY_MERKLE_ITEM_KEY:
+		case BTRFS_FSCRYPT_INODE_CTX_KEY:
+		case BTRFS_FSCRYPT_CTX_KEY:
 			break;
 		default:
 			error("unknown key (%llu %u %llu) found in leaf %llu",
kernel-shared/print-tree.c +20 -0
index 2c0168b0..6a4eee48 100644
@@ -117,6 +117,20 @@ static void print_dir_item(struct extent_buffer *eb, u32 size,
 	}
 }
 
+static void print_fscrypt_context(struct extent_buffer *eb, int slot)
+{
+	int i;
+	unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
+	u32 item_size = btrfs_item_size(eb, slot);
+	u8 ctx_buf[item_size];
+
+	read_extent_buffer(eb, ctx_buf, ptr, item_size);
+	printf("\t\tvalue: ");
+	for(i = 0; i < item_size; i++)
+		printf("%02x", ctx_buf[i]);
+	printf("\n");
+}
+
 static void print_inode_extref_item(struct extent_buffer *eb, u32 size,
 		struct btrfs_inode_extref *extref)
 {
@@ -741,6 +755,8 @@ void print_key_type(FILE *stream, u64 objectid, u8 type)
 		[BTRFS_DIR_LOG_ITEM_KEY]	= "DIR_LOG_ITEM",
 		[BTRFS_DIR_LOG_INDEX_KEY]	= "DIR_LOG_INDEX",
 		[BTRFS_XATTR_ITEM_KEY]		= "XATTR_ITEM",
+		[BTRFS_FSCRYPT_INODE_CTX_KEY]   = "FSCRYPT_INODE_CTX",
+		[BTRFS_FSCRYPT_CTX_KEY]         = "FSCRYPT_CTX",
 		[BTRFS_VERITY_DESC_ITEM_KEY]	= "VERITY_DESC_ITEM",
 		[BTRFS_VERITY_MERKLE_ITEM_KEY]	= "VERITY_MERKLE_ITEM",
 		[BTRFS_ORPHAN_ITEM_KEY]		= "ORPHAN_ITEM",
@@ -1557,6 +1573,10 @@ void __btrfs_print_leaf(struct extent_buffer *eb, unsigned int mode)
 		case BTRFS_XATTR_ITEM_KEY:
 			print_dir_item(eb, item_size, ptr);
 			break;
+		case BTRFS_FSCRYPT_INODE_CTX_KEY:
+		case BTRFS_FSCRYPT_CTX_KEY:
+			print_fscrypt_context(eb, i);
+			break;
 		case BTRFS_DIR_LOG_INDEX_KEY:
 		case BTRFS_DIR_LOG_ITEM_KEY: {
 			struct btrfs_dir_log_item *dlog;
Signature · 1 line
2.53.0
#8 lore raw

PATCH v3 6/7 btrfs-progs: check: update inline extent length checking

In reply to Daniel Vacek

From: Sweet Tea Dorminy <[email protected]>

As part of the encryption changes, encrypted inline file extents record their actual data length in ram_bytes, like compressed inline file extents, while the item's length records the actual size. As such, encrypted inline extents must be treated like compressed ones for inode length consistency checking.

---
 check/main.c | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)
check/main.c +17 -14
index 9447b01e..cadcfef0 100644
@@ -1720,9 +1720,7 @@ static int process_file_extent(struct btrfs_root *root,
 	u64 disk_bytenr = 0;
 	u64 extent_offset = 0;
 	u64 mask = gfs_info->sectorsize - 1;
-	u32 max_inline_size = min_t(u32, mask,
-				BTRFS_MAX_INLINE_DATA_SIZE(gfs_info));
-	u8 compression;
+	u8 compression, encryption;
 	int extent_type;
 	int ret;
 
@@ -1747,25 +1745,30 @@ static int process_file_extent(struct btrfs_root *root,
 	fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
 	extent_type = btrfs_file_extent_type(eb, fi);
 	compression = btrfs_file_extent_compression(eb, fi);
+	encryption  = btrfs_file_extent_encryption(eb, fi);
 
 	if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
-		num_bytes = btrfs_file_extent_ram_bytes(eb, fi);
-		if (num_bytes == 0)
+		u32 max_inline_size = min_t(u32, mask,
+					BTRFS_MAX_INLINE_DATA_SIZE(gfs_info));
+		u64 num_disk_bytes = btrfs_file_extent_inline_item_len(eb, slot);
+		u64 num_decoded_bytes = btrfs_file_extent_ram_bytes(eb, fi);
+		if (num_decoded_bytes == 0)
 			rec->errors |= I_ERR_BAD_FILE_EXTENT;
-		if (compression) {
-			if (btrfs_file_extent_inline_item_len(eb, slot) >
-			    max_inline_size ||
-			    num_bytes > gfs_info->sectorsize)
+		if (compression || encryption) {
+			if (encryption)
+				max_inline_size = min_t(u32, gfs_info->sectorsize,
+					BTRFS_MAX_INLINE_DATA_SIZE(gfs_info));
+			if (num_disk_bytes > max_inline_size ||
+			    num_decoded_bytes > gfs_info->sectorsize)
 				rec->errors |= I_ERR_FILE_EXTENT_TOO_LARGE;
 		} else {
-			if (num_bytes > max_inline_size)
+			if (num_decoded_bytes > max_inline_size)
 				rec->errors |= I_ERR_FILE_EXTENT_TOO_LARGE;
-			if (btrfs_file_extent_inline_item_len(eb, slot) !=
-			    num_bytes)
+			if (num_disk_bytes != num_decoded_bytes)
 				rec->errors |= I_ERR_INLINE_RAM_BYTES_WRONG;
 		}
-		rec->found_size += num_bytes;
-		num_bytes = (num_bytes + mask) & ~mask;
+		rec->found_size += num_decoded_bytes;
+		num_bytes = (num_decoded_bytes + mask) & ~mask;
 	} else if (extent_type == BTRFS_FILE_EXTENT_REG ||
 		   extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
 		num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Signature · 1 line
2.53.0
#9 lore raw

Re: [PATCH v3 6/7] btrfs-progs: check: update inline extent length checking

Quoted 42 lines · Daniel Vacek

From: Sweet Tea Dorminy <[email protected]>

As part of the encryption changes, encrypted inline file extents record their actual data length in ram_bytes, like compressed inline file extents, while the item's length records the actual size. As such, encrypted inline extents must be treated like compressed ones for inode length consistency checking.

---
  check/main.c | 31 +++++++++++++++++--------------
  1 file changed, 17 insertions(+), 14 deletions(-)
check/main.c +6 -5
index 9447b01e..cadcfef0 100644
@@ -1720,9 +1720,7 @@ static int process_file_extent(struct btrfs_root *root,
  	u64 disk_bytenr = 0;
  	u64 extent_offset = 0;
  	u64 mask = gfs_info->sectorsize - 1;
-	u32 max_inline_size = min_t(u32, mask,
-				BTRFS_MAX_INLINE_DATA_SIZE(gfs_info));
-	u8 compression;
+	u8 compression, encryption;
  	int extent_type;
  	int ret;
 
@@ -1747,25 +1745,30 @@ static int process_file_extent(struct btrfs_root *root,
  	fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  	extent_type = btrfs_file_extent_type(eb, fi);
  	compression = btrfs_file_extent_compression(eb, fi);
+	encryption  = btrfs_file_extent_encryption(eb, fi);
 
  	if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
-		num_bytes = btrfs_file_extent_ram_bytes(eb, fi);
-		if (num_bytes == 0)
+		u32 max_inline_size = min_t(u32, mask,
+					BTRFS_MAX_INLINE_DATA_SIZE(gfs_info));
+		u64 num_disk_bytes = btrfs_file_extent_inline_item_len(eb, slot);
+		u64 num_decoded_bytes = btrfs_file_extent_ram_bytes(eb, fi);

check/main.c static int process_file_extent(struct btrfs_root *root, · line 1760

Quoted 10 lines
check/main.c +5 -4
+		if (num_decoded_bytes == 0)
  			rec->errors |= I_ERR_BAD_FILE_EXTENT;
-		if (compression) {
-			if (btrfs_file_extent_inline_item_len(eb, slot) >
-			    max_inline_size ||
-			    num_bytes > gfs_info->sectorsize)
+		if (compression || encryption) {
+			if (encryption)
+				max_inline_size = min_t(u32, gfs_info->sectorsize,
+					BTRFS_MAX_INLINE_DATA_SIZE(gfs_info));

The change looks good to me now.

However I'm just curious, is it possible to limit the encrypted data size to sectorsize-1?

Or it is some fscrypt limit internal requiring a power-of-2 size or just lack of interface?

Anyway I won't object this new change.

Thanks,
Qu

Quoted 19 lines
check/main.c +6 -5
+			if (num_disk_bytes > max_inline_size ||
+			    num_decoded_bytes > gfs_info->sectorsize)
  				rec->errors |= I_ERR_FILE_EXTENT_TOO_LARGE;
  		} else {
-			if (num_bytes > max_inline_size)
+			if (num_decoded_bytes > max_inline_size)
  				rec->errors |= I_ERR_FILE_EXTENT_TOO_LARGE;
-			if (btrfs_file_extent_inline_item_len(eb, slot) !=
-			    num_bytes)
+			if (num_disk_bytes != num_decoded_bytes)
  				rec->errors |= I_ERR_INLINE_RAM_BYTES_WRONG;
  		}
-		rec->found_size += num_bytes;
-		num_bytes = (num_bytes + mask) & ~mask;
+		rec->found_size += num_decoded_bytes;
+		num_bytes = (num_decoded_bytes + mask) & ~mask;
  	} else if (extent_type == BTRFS_FILE_EXTENT_REG ||
  		   extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  		num_bytes = btrfs_file_extent_num_bytes(eb, fi);
#10 lore raw

Re: [PATCH v3 6/7] btrfs-progs: check: update inline extent length checking

Quoted 56 lines · Qu Wenruo
Quoted 42 lines · Daniel Vacek

From: Sweet Tea Dorminy <[email protected]>

As part of the encryption changes, encrypted inline file extents record their actual data length in ram_bytes, like compressed inline file extents, while the item's length records the actual size. As such, encrypted inline extents must be treated like compressed ones for inode length consistency checking.

---
  check/main.c | 31 +++++++++++++++++--------------
  1 file changed, 17 insertions(+), 14 deletions(-)
check/main.c +6 -5
index 9447b01e..cadcfef0 100644
@@ -1720,9 +1720,7 @@ static int process_file_extent(struct btrfs_root *root,
      u64 disk_bytenr = 0;
      u64 extent_offset = 0;
      u64 mask = gfs_info->sectorsize - 1;
-     u32 max_inline_size = min_t(u32, mask,
-                             BTRFS_MAX_INLINE_DATA_SIZE(gfs_info));
-     u8 compression;
+     u8 compression, encryption;
      int extent_type;
      int ret;
 
@@ -1747,25 +1745,30 @@ static int process_file_extent(struct btrfs_root *root,
      fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
      extent_type = btrfs_file_extent_type(eb, fi);
      compression = btrfs_file_extent_compression(eb, fi);
+     encryption  = btrfs_file_extent_encryption(eb, fi);
 
      if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
-             num_bytes = btrfs_file_extent_ram_bytes(eb, fi);
-             if (num_bytes == 0)
+             u32 max_inline_size = min_t(u32, mask,
+                                     BTRFS_MAX_INLINE_DATA_SIZE(gfs_info));
+             u64 num_disk_bytes = btrfs_file_extent_inline_item_len(eb, slot);
+             u64 num_decoded_bytes = btrfs_file_extent_ram_bytes(eb, fi);

check/main.c static int process_file_extent(struct btrfs_root *root, · line 1760

Quoted 10 lines
check/main.c +5 -4
+             if (num_decoded_bytes == 0)
                      rec->errors |= I_ERR_BAD_FILE_EXTENT;
-             if (compression) {
-                     if (btrfs_file_extent_inline_item_len(eb, slot) >
-                         max_inline_size ||
-                         num_bytes > gfs_info->sectorsize)
+             if (compression || encryption) {
+                     if (encryption)
+                             max_inline_size = min_t(u32, gfs_info->sectorsize,
+                                     BTRFS_MAX_INLINE_DATA_SIZE(gfs_info));

The change looks good to me now.

Quoted 5 lines

However I'm just curious, is it possible to limit the encrypted data size to sectorsize-1?

Or it is some fscrypt limit internal requiring a power-of-2 size or just lack of interface?

The encrypted data has the granularity of the cipher block size. With AES, it's 16 bytes. Hence why.
Eventually the best we could do would be sectorsize-16. But then, if the cipher changed in the future...

--nX

Quoted 25 lines

Anyway I won't object this new change.

Thanks,
Qu

Quoted 19 lines
check/main.c +6 -5
+                     if (num_disk_bytes > max_inline_size ||
+                         num_decoded_bytes > gfs_info->sectorsize)
                              rec->errors |= I_ERR_FILE_EXTENT_TOO_LARGE;
              } else {
-                     if (num_bytes > max_inline_size)
+                     if (num_decoded_bytes > max_inline_size)
                              rec->errors |= I_ERR_FILE_EXTENT_TOO_LARGE;
-                     if (btrfs_file_extent_inline_item_len(eb, slot) !=
-                         num_bytes)
+                     if (num_disk_bytes != num_decoded_bytes)
                              rec->errors |= I_ERR_INLINE_RAM_BYTES_WRONG;
              }
-             rec->found_size += num_bytes;
-             num_bytes = (num_bytes + mask) & ~mask;
+             rec->found_size += num_decoded_bytes;
+             num_bytes = (num_decoded_bytes + mask) & ~mask;
      } else if (extent_type == BTRFS_FILE_EXTENT_REG ||
                 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
              num_bytes = btrfs_file_extent_num_bytes(eb, fi);
#11 lore raw

Re: [PATCH v3 6/7] btrfs-progs: check: update inline extent length checking

Quoted 60 lines · Daniel Vacek
Quoted 59 lines · Qu Wenruo
Quoted 42 lines · Daniel Vacek

From: Sweet Tea Dorminy <[email protected]>

As part of the encryption changes, encrypted inline file extents record their actual data length in ram_bytes, like compressed inline file extents, while the item's length records the actual size. As such, encrypted inline extents must be treated like compressed ones for inode length consistency checking.

---
   check/main.c | 31 +++++++++++++++++--------------
   1 file changed, 17 insertions(+), 14 deletions(-)
check/main.c +6 -5
index 9447b01e..cadcfef0 100644
@@ -1720,9 +1720,7 @@ static int process_file_extent(struct btrfs_root *root,
       u64 disk_bytenr = 0;
       u64 extent_offset = 0;
       u64 mask = gfs_info->sectorsize - 1;
-     u32 max_inline_size = min_t(u32, mask,
-                             BTRFS_MAX_INLINE_DATA_SIZE(gfs_info));
-     u8 compression;
+     u8 compression, encryption;
       int extent_type;
       int ret;
 
@@ -1747,25 +1745,30 @@ static int process_file_extent(struct btrfs_root *root,
       fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
       extent_type = btrfs_file_extent_type(eb, fi);
       compression = btrfs_file_extent_compression(eb, fi);
+     encryption  = btrfs_file_extent_encryption(eb, fi);
 
       if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
-             num_bytes = btrfs_file_extent_ram_bytes(eb, fi);
-             if (num_bytes == 0)
+             u32 max_inline_size = min_t(u32, mask,
+                                     BTRFS_MAX_INLINE_DATA_SIZE(gfs_info));
+             u64 num_disk_bytes = btrfs_file_extent_inline_item_len(eb, slot);
+             u64 num_decoded_bytes = btrfs_file_extent_ram_bytes(eb, fi);

check/main.c static int process_file_extent(struct btrfs_root *root, · line 1760

Quoted 10 lines
check/main.c +5 -4
+             if (num_decoded_bytes == 0)
                       rec->errors |= I_ERR_BAD_FILE_EXTENT;
-             if (compression) {
-                     if (btrfs_file_extent_inline_item_len(eb, slot) >
-                         max_inline_size ||
-                         num_bytes > gfs_info->sectorsize)
+             if (compression || encryption) {
+                     if (encryption)
+                             max_inline_size = min_t(u32, gfs_info->sectorsize,
+                                     BTRFS_MAX_INLINE_DATA_SIZE(gfs_info));

The change looks good to me now.

However I'm just curious, is it possible to limit the encrypted data size to sectorsize-1?

Quoted 7 lines
Quoted 2 lines

Or it is some fscrypt limit internal requiring a power-of-2 size or just lack of interface?

The encrypted data has the granularity of the cipher block size. With AES, it's 16 bytes. Hence why.
Eventually the best we could do would be sectorsize-16. But then, if the cipher changed in the future...

Thanks a lot, that explains the reason why we can not follow the old sectorsize - 1 limit.

Thanks,
Qu

Quoted 29 lines

--nX

Quoted 25 lines

Anyway I won't object this new change.

Thanks,
Qu

Quoted 19 lines
check/main.c +6 -5
+                     if (num_disk_bytes > max_inline_size ||
+                         num_decoded_bytes > gfs_info->sectorsize)
                               rec->errors |= I_ERR_FILE_EXTENT_TOO_LARGE;
               } else {
-                     if (num_bytes > max_inline_size)
+                     if (num_decoded_bytes > max_inline_size)
                               rec->errors |= I_ERR_FILE_EXTENT_TOO_LARGE;
-                     if (btrfs_file_extent_inline_item_len(eb, slot) !=
-                         num_bytes)
+                     if (num_disk_bytes != num_decoded_bytes)
                               rec->errors |= I_ERR_INLINE_RAM_BYTES_WRONG;
               }
-             rec->found_size += num_bytes;
-             num_bytes = (num_bytes + mask) & ~mask;
+             rec->found_size += num_decoded_bytes;
+             num_bytes = (num_decoded_bytes + mask) & ~mask;
       } else if (extent_type == BTRFS_FILE_EXTENT_REG ||
                  extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
               num_bytes = btrfs_file_extent_num_bytes(eb, fi);
#12 lore raw

PATCH v3 7/7 btrfs-progs: recognize ENCRYPT inode item flag

In reply to Daniel Vacek
---
 kernel-shared/print-tree.c | 1 +
 1 file changed, 1 insertion(+)
kernel-shared/print-tree.c +1 -0
index 6a4eee48..cae5c6df 100644
@@ -1012,6 +1012,7 @@ static struct readable_flag_entry inode_flags_array[] = {
 	DEF_INODE_FLAG_ENTRY(NOATIME),
 	DEF_INODE_FLAG_ENTRY(DIRSYNC),
 	DEF_INODE_FLAG_ENTRY(COMPRESS),
+	DEF_INODE_FLAG_ENTRY(ENCRYPT),
 	DEF_INODE_FLAG_ENTRY(ROOT_ITEM_INIT),
 };
 static const int inode_flags_num = ARRAY_SIZE(inode_flags_array);
Signature · 1 line
2.53.0
#13 lore raw
In reply to Daniel Vacek
Quoted 11 lines · Daniel Vacek

This series is a rebase of an older set of fscrypt related changes from Sweet Tea Dorminy and Josef Bacik found here:
https://github.com/josefbacik/btrfs-progs/tree/fscrypt

It passed all my tests. Hopefully nothing blows. Enjoy testing.

v3:
* dropped first patch and improved inline extent length checking
* correctly squashed the context key definitions into "btrfs-progs: add inode encryption contexts"
* inline extents also show the encryption field now in tree dump

And I forgot to explicitly CC Qu as most of these changes are thanks to his review of v2.

--nX

Quoted 30 lines

v2: https://lore.kernel.org/linux-btrfs/[email protected]/
* works with v7 of the kernel fscrypt series
* the on-disk format changed and parts of the series had to be reworked
- particularly the encryption context is now stored as dedicated item and not glued onto extent data item
* also parses the ENCRYPT inode item flag

Daniel Vacek (1):
btrfs-progs: recognize ENCRYPT inode item flag

Sweet Tea Dorminy (6):
btrfs-progs: add new FEATURE_INCOMPAT_ENCRYPT flag
btrfs-progs: start tracking extent encryption context info
btrfs-progs: add inode encryption contexts
btrfs-progs: print encryptin type field of file extents
btrfs-progs: handle fscrypt context items
btrfs-progs: check: update inline extent length checking

 check/main.c                    | 34 ++++++++++++++++++---------------
 kernel-shared/ctree.h           |  1 +
 kernel-shared/print-tree.c      | 28 +++++++++++++++++++++++++--
 kernel-shared/tree-checker.c    | 17 ++++++++++-------
 kernel-shared/uapi/btrfs.h      |  1 +
 kernel-shared/uapi/btrfs_tree.h | 11 +++++++++++
 libbtrfsutil/btrfs.h            |  1 +
 7 files changed, 69 insertions(+), 24 deletions(-)

--
2.53.0

Reviewed-by on the v3 series
#14 lore raw
In reply to Daniel Vacek
Quoted 5 lines · Daniel Vacek

This series is a rebase of an older set of fscrypt related changes from Sweet Tea Dorminy and Josef Bacik found here:
https://github.com/josefbacik/btrfs-progs/tree/fscrypt

It passed all my tests. Hopefully nothing blows. Enjoy testing.

Although I still think some corner cases may change in the future, but considering it's already hidden behind experimental, we should have plenty time before pushing it to end users.

Will push it to devel after a full selftest.

Thanks,
Qu

Quoted 34 lines

v3:
* dropped first patch and improved inline extent length checking
* correctly squashed the context key definitions into "btrfs-progs: add inode encryption contexts"
* inline extents also show the encryption field now in tree dump

v2: https://lore.kernel.org/linux-btrfs/[email protected]/
* works with v7 of the kernel fscrypt series
* the on-disk format changed and parts of the series had to be reworked
- particularly the encryption context is now stored as dedicated item and not glued onto extent data item
* also parses the ENCRYPT inode item flag

Daniel Vacek (1):
btrfs-progs: recognize ENCRYPT inode item flag

Sweet Tea Dorminy (6):
btrfs-progs: add new FEATURE_INCOMPAT_ENCRYPT flag
btrfs-progs: start tracking extent encryption context info
btrfs-progs: add inode encryption contexts
btrfs-progs: print encryptin type field of file extents
btrfs-progs: handle fscrypt context items
btrfs-progs: check: update inline extent length checking

  check/main.c                    | 34 ++++++++++++++++++---------------
  kernel-shared/ctree.h           |  1 +
  kernel-shared/print-tree.c      | 28 +++++++++++++++++++++++++--
  kernel-shared/tree-checker.c    | 17 ++++++++++-------
  kernel-shared/uapi/btrfs.h      |  1 +
  kernel-shared/uapi/btrfs_tree.h | 11 +++++++++++
  libbtrfsutil/btrfs.h            |  1 +
  7 files changed, 69 insertions(+), 24 deletions(-)
Reviewed-by on the v3 series
#15 lore raw
In reply to Daniel Vacek
Quoted 31 lines · Daniel Vacek

This series is a rebase of an older set of fscrypt related changes from Sweet Tea Dorminy and Josef Bacik found here:
https://github.com/josefbacik/btrfs-progs/tree/fscrypt

It passed all my tests. Hopefully nothing blows. Enjoy testing.

v3:
* dropped first patch and improved inline extent length checking
* correctly squashed the context key definitions into "btrfs-progs: add inode encryption contexts"
* inline extents also show the encryption field now in tree dump

v2: https://lore.kernel.org/linux-btrfs/[email protected]/
* works with v7 of the kernel fscrypt series
* the on-disk format changed and parts of the series had to be reworked
- particularly the encryption context is now stored as dedicated item and not glued onto extent data item
* also parses the ENCRYPT inode item flag

Daniel Vacek (1):
btrfs-progs: recognize ENCRYPT inode item flag

Sweet Tea Dorminy (6):
btrfs-progs: add new FEATURE_INCOMPAT_ENCRYPT flag
btrfs-progs: start tracking extent encryption context info
btrfs-progs: add inode encryption contexts
btrfs-progs: print encryptin type field of file extents
btrfs-progs: handle fscrypt context items
btrfs-progs: check: update inline extent length checking

Quoted 9 lines
 check/main.c                    | 34 ++++++++++++++++++---------------
 kernel-shared/ctree.h           |  1 +
 kernel-shared/print-tree.c      | 28 +++++++++++++++++++++++++--
 kernel-shared/tree-checker.c    | 17 ++++++++++-------
 kernel-shared/uapi/btrfs.h      |  1 +
 kernel-shared/uapi/btrfs_tree.h | 11 +++++++++++
 libbtrfsutil/btrfs.h            |  1 +
 7 files changed, 69 insertions(+), 24 deletions(-)

These look reasonable to me too.

Signature · 1 line
真実はいつも一つ!/ Always, there's only one truth!