@@ -65,6 +65,8 @@ void free_extent_map(struct extent_map *em)
if (!em)
return;
if (refcount_dec_and_test(&em->refs)) {
+ if (em->fscrypt_info)
+ fscrypt_free_extent_info(&em->fscrypt_info);
WARN_ON(extent_map_in_tree(em));
WARN_ON(!list_empty(&em->list));
if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
@@ -207,6 +209,13 @@ static int mergable_maps(struct extent_map *prev, struct extent_map *next)
if (!list_empty(&prev->list) || !list_empty(&next->list))
return 0;
+ /*
+ * Don't merge adjacent maps with different fscrypt_contexts.
+ */
+ if (!memcmp(&prev->fscrypt_info, &next->fscrypt_info,
+ sizeof(next->fscrypt_info)))
+ return 0;
+
ASSERT(next->block_start != EXTENT_MAP_DELALLOC &&
prev->block_start != EXTENT_MAP_DELALLOC);
@@ -27,6 +27,8 @@ enum {
EXTENT_FLAG_FS_MAPPING,
/* This em is merged from two or more physically adjacent ems */
EXTENT_FLAG_MERGED,
+ /* This em has a fscrypt info */
+ EXTENT_FLAG_ENCRYPTED,
};
struct extent_map {
@@ -50,6 +52,7 @@ struct extent_map {
*/
u64 generation;
unsigned long flags;
+ struct fscrypt_info *fscrypt_info;
/* Used for chunk mappings, flag EXTENT_FLAG_FS_MAPPING must be set */
struct map_lookup *map_lookup;
refcount_t refs;
@@ -7436,6 +7436,14 @@ static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
em->compress_type = compress_type;
}
+ ret = fscrypt_prepare_new_extent(&inode->vfs_inode, &em->fscrypt_info);
+ if (ret < 0) {
+ free_extent_map(em);
+ return ERR_PTR(ret);
+ }
+ if (em->fscrypt_info)
+ set_bit(EXTENT_FLAG_ENCRYPTED, &em->flags);
+
ret = btrfs_replace_extent_map_range(inode, em, true);
if (ret) {
free_extent_map(em);
Each extent_map will end up with a pointer to its associated fscrypt_info if any, which should have the same lifetime as the extent_map. Add creation of fscrypt_infos for new extent_maps, and freeing as appropriate. Signed-off-by: Sweet Tea Dorminy <sweettea-kernel@dorminy.me> --- fs/btrfs/extent_map.c | 9 +++++++++ fs/btrfs/extent_map.h | 3 +++ fs/btrfs/inode.c | 8 ++++++++ 3 files changed, 20 insertions(+)