Skip to contentSkip to conversation

Btrfs: make fsync work after cloning into a file

PATCH Superseded Filipe David Borba Manana 3 participants 7 messages Jun 6, 2014 – Jun 29, 2014 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs: make fsync work after cloning into a file

When cloning into a file, we were correctly replacing the extent items in the target range and removing the extent maps. However we weren't replacing the extent maps with new ones that point to the new extents - as a consequence, an incremental fsync (when the inode doesn't have the full sync flag) was a NOOP, since it relies on the existence of extent maps in the modified list of the inode's extent map tree, which was empty. Therefore add new extent maps to reflect the target clone range.

A test case for xfstests follows.

---
 fs/btrfs/ioctl.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 114 insertions(+)
fs/btrfs/ioctl.c +114 -0
index 44dcfd0..1197478 100644
@@ -3043,6 +3043,104 @@ out:
 	return ret;
 }
 
+static void clone_update_extent_map(struct inode *inode,
+				    const struct btrfs_trans_handle *trans,
+				    const struct btrfs_path *path,
+				    const struct btrfs_key *key,
+				    struct btrfs_file_extent_item *fi,
+				    const u64 hole_offset,
+				    const u64 hole_len)
+{
+	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
+	struct btrfs_root *root = BTRFS_I(inode)->root;
+	struct extent_buffer *leaf = path->nodes[0];
+	const int slot = path->slots[0];
+	struct extent_map *em;
+	u64 extent_start, extent_end;
+	u64 bytenr;
+	u8 type;
+	int ret;
+
+	em = alloc_extent_map();
+	if (!em) {
+		set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
+			&BTRFS_I(inode)->runtime_flags);
+		return;
+	}
+
+	em->bdev = root->fs_info->fs_devices->latest_bdev;
+	if (!fi) {
+		em->start = hole_offset;
+		em->len = hole_len;
+		em->ram_bytes = em->len;
+		em->orig_start = hole_offset;
+		em->block_start = EXTENT_MAP_HOLE;
+		em->block_len = 0;
+		em->orig_block_len = 0;
+		em->compress_type = BTRFS_COMPRESS_NONE;
+		em->generation = trans->transid;
+		goto insert_em;
+	}
+
+	em->generation = -1;
+	extent_start = key->offset;
+	extent_end = extent_start + btrfs_file_extent_num_bytes(leaf, fi);
+	bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
+	type = btrfs_file_extent_type(leaf, fi);
+
+	em->start = extent_start;
+	em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
+	em->compress_type = btrfs_file_extent_compression(leaf, fi);
+
+	if (em->compress_type != BTRFS_COMPRESS_NONE)
+		set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
+
+	if (type == BTRFS_FILE_EXTENT_INLINE) {
+		em->len = ALIGN(btrfs_file_extent_inline_len(leaf, slot, fi),
+				root->sectorsize);
+		em->orig_block_len = em->len;
+		em->orig_start = em->start;
+		em->block_start = EXTENT_MAP_INLINE;
+		em->block_len = (u64)-1;
+		goto insert_em;
+	}
+
+	em->len = extent_end - extent_start;
+	em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
+	em->orig_start = extent_start - btrfs_file_extent_offset(leaf, fi);
+	if (bytenr == 0)
+		em->block_start = EXTENT_MAP_HOLE;
+	else
+		em->block_start = bytenr;
+
+	if (em->compress_type == BTRFS_COMPRESS_NONE) {
+		em->block_start += btrfs_file_extent_offset(leaf, fi);
+		em->block_len = em->len;
+	} else {
+		em->block_len = em->orig_block_len;
+	}
+
+	if (type == BTRFS_FILE_EXTENT_PREALLOC)
+		set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
+
+insert_em:
+	while (1) {
+		write_lock(&em_tree->lock);
+		ret = add_extent_mapping(em_tree, em, 1);
+		write_unlock(&em_tree->lock);
+		if (ret != -EEXIST) {
+			free_extent_map(em);
+			break;
+		}
+		btrfs_drop_extent_cache(inode, em->start,
+					em->start + em->len - 1, 0);
+	}
+
+	if (unlikely(ret))
+		set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
+			&BTRFS_I(inode)->runtime_flags);
+}
+
 /**
  * btrfs_clone() - clone a range from inode file to another
  *
@@ -3361,8 +3459,19 @@ process_slot:
 					    btrfs_item_ptr_offset(leaf, slot),
 					    size);
 				inode_add_bytes(inode, datal);
+				extent = btrfs_item_ptr(leaf, slot,
+						struct btrfs_file_extent_item);
 			}
 
+			/* If we have an implicit hole (NO_HOLES feature). */
+			if (drop_start < new_key.offset)
+				clone_update_extent_map(inode, trans,
+						path, NULL, NULL, drop_start,
+						new_key.offset - drop_start);
+
+			clone_update_extent_map(inode, trans, path,
+						&new_key, extent, 0, 0);
+
 			btrfs_mark_buffer_dirty(leaf);
 			btrfs_release_path(path);
 
@@ -3406,6 +3515,11 @@ process_slot:
 		}
 		ret = clone_finish_inode_update(trans, inode, destoff + len,
 						destoff, olen);
+		if (ret)
+			goto out;
+		clone_update_extent_map(inode, trans,
+					path, NULL, NULL, last_dest_end,
+					destoff + len - last_dest_end);
 	}
 
 out:
Signature · 1 line
1.9.1
#1 lore raw
Quoted 112 lines · Filipe David Borba Manana

When cloning into a file, we were correctly replacing the extent items in the target range and removing the extent maps. However we weren't replacing the extent maps with new ones that point to the new extents - as a consequence, an incremental fsync (when the inode doesn't have the full sync flag) was a NOOP, since it relies on the existence of extent maps in the modified list of the inode's extent map tree, which was empty. Therefore add new extent maps to reflect the target clone range.

A test case for xfstests follows.

---
 fs/btrfs/ioctl.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 114 insertions(+)
fs/btrfs/ioctl.c +88 -0
index 44dcfd0..1197478 100644
@@ -3043,6 +3043,104 @@ out:
 	return ret;
 }
 
+static void clone_update_extent_map(struct inode *inode,
+				    const struct btrfs_trans_handle *trans,
+				    const struct btrfs_path *path,
+				    const struct btrfs_key *key,
+				    struct btrfs_file_extent_item *fi,
+				    const u64 hole_offset,
+				    const u64 hole_len)
+{
+	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
+	struct btrfs_root *root = BTRFS_I(inode)->root;
+	struct extent_buffer *leaf = path->nodes[0];
+	const int slot = path->slots[0];
+	struct extent_map *em;
+	u64 extent_start, extent_end;
+	u64 bytenr;
+	u8 type;
+	int ret;
+
+	em = alloc_extent_map();
+	if (!em) {
+		set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
+			&BTRFS_I(inode)->runtime_flags);
+		return;
+	}
+
+	em->bdev = root->fs_info->fs_devices->latest_bdev;
+	if (!fi) {
+		em->start = hole_offset;
+		em->len = hole_len;
+		em->ram_bytes = em->len;
+		em->orig_start = hole_offset;
+		em->block_start = EXTENT_MAP_HOLE;
+		em->block_len = 0;
+		em->orig_block_len = 0;
+		em->compress_type = BTRFS_COMPRESS_NONE;
+		em->generation = trans->transid;
+		goto insert_em;
+	}
+
+	em->generation = -1;
+	extent_start = key->offset;
+	extent_end = extent_start + btrfs_file_extent_num_bytes(leaf, fi);
+	bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
+	type = btrfs_file_extent_type(leaf, fi);
+
+	em->start = extent_start;
+	em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
+	em->compress_type = btrfs_file_extent_compression(leaf, fi);
+
+	if (em->compress_type != BTRFS_COMPRESS_NONE)
+		set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
+
+	if (type == BTRFS_FILE_EXTENT_INLINE) {
+		em->len = ALIGN(btrfs_file_extent_inline_len(leaf, slot, fi),
+				root->sectorsize);
+		em->orig_block_len = em->len;
+		em->orig_start = em->start;
+		em->block_start = EXTENT_MAP_INLINE;
+		em->block_len = (u64)-1;
+		goto insert_em;
+	}
+
+	em->len = extent_end - extent_start;
+	em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
+	em->orig_start = extent_start - btrfs_file_extent_offset(leaf, fi);
+	if (bytenr == 0)
+		em->block_start = EXTENT_MAP_HOLE;
+	else
+		em->block_start = bytenr;
+
+	if (em->compress_type == BTRFS_COMPRESS_NONE) {
+		em->block_start += btrfs_file_extent_offset(leaf, fi);
+		em->block_len = em->len;
+	} else {
+		em->block_len = em->orig_block_len;
+	}
+
+	if (type == BTRFS_FILE_EXTENT_PREALLOC)
+		set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
+
+insert_em:
+	while (1) {
+		write_lock(&em_tree->lock);
+		ret = add_extent_mapping(em_tree, em, 1);
+		write_unlock(&em_tree->lock);
+		if (ret != -EEXIST) {
+			free_extent_map(em);
+			break;

fs/btrfs/ioctl.c out: · line 3143

Quoted 10 lines
fs/btrfs/ioctl.c +10 -0
+		}
+		btrfs_drop_extent_cache(inode, em->start,
+					em->start + em->len - 1, 0);
+	}
+
+	if (unlikely(ret))
+		set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
+			&BTRFS_I(inode)->runtime_flags);
+}
+

Similar code to btrfs_get_extent(), can we use that instead?

-liubo

Quoted 42 lines
fs/btrfs/ioctl.c +16 -0
 /**
  * btrfs_clone() - clone a range from inode file to another
  *
@@ -3361,8 +3459,19 @@ process_slot:
 					    btrfs_item_ptr_offset(leaf, slot),
 					    size);
 				inode_add_bytes(inode, datal);
+				extent = btrfs_item_ptr(leaf, slot,
+						struct btrfs_file_extent_item);
 			}
 
+			/* If we have an implicit hole (NO_HOLES feature). */
+			if (drop_start < new_key.offset)
+				clone_update_extent_map(inode, trans,
+						path, NULL, NULL, drop_start,
+						new_key.offset - drop_start);
+
+			clone_update_extent_map(inode, trans, path,
+						&new_key, extent, 0, 0);
+
 			btrfs_mark_buffer_dirty(leaf);
 			btrfs_release_path(path);
 
@@ -3406,6 +3515,11 @@ process_slot:
 		}
 		ret = clone_finish_inode_update(trans, inode, destoff + len,
 						destoff, olen);
+		if (ret)
+			goto out;
+		clone_update_extent_map(inode, trans,
+					path, NULL, NULL, last_dest_end,
+					destoff + len - last_dest_end);
 	}
 
 out:
Signature · 6 lines
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw

PATCH v2 1/1 Btrfs: make fsync work after cloning into a file

In reply to Filipe David Borba Manana

When cloning into a file, we were correctly replacing the extent items in the target range and removing the extent maps. However we weren't replacing the extent maps with new ones that point to the new extents - as a consequence, an incremental fsync (when the inode doesn't have the full sync flag) was a NOOP, since it relies on the existence of extent maps in the modified list of the inode's extent map tree, which was empty. Therefore add new extent maps to reflect the target clone range.

A test case for xfstests follows.

---

V2: Factored out needed code from inode.c:btrfs_get_extent() into a
separate function so that it can be reused by the cloning code, avoiding some duplicated and non-trivial logic when populating an extent map from a file extent item.

 fs/btrfs/ctree.h     |  5 ++++
 fs/btrfs/file-item.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/inode.c     | 41 +++----------------------------
 fs/btrfs/ioctl.c     | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 143 insertions(+), 38 deletions(-)
fs/btrfs/ctree.h +5 -0
index af523d6..a668fd9 100644
@@ -3749,6 +3749,11 @@ int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
 		       struct bio *bio, u64 file_start, int contig);
 int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
 			     struct list_head *list, int search_commit);
+void btrfs_extent_item_to_extent_map(struct inode *inode,
+				     const struct btrfs_path *path,
+				     struct btrfs_file_extent_item *fi,
+				     struct extent_map *em);
+
 /* inode.c */
 struct btrfs_delalloc_work {
 	struct inode *inode;
fs/btrfs/file-item.c +68 -0

71 lines not on this page · raw message

fs/btrfs/inode.c +3 -38

86 lines not on this page · raw message

fs/btrfs/ioctl.c +67 -0

87 lines not on this page · raw message

Signature · 1 line
1.9.1
#3 lore raw

PATCH v3 1/1 Btrfs: make fsync work after cloning into a file

In reply to Filipe David Borba Manana

When cloning into a file, we were correctly replacing the extent items in the target range and removing the extent maps. However we weren't replacing the extent maps with new ones that point to the new extents - as a consequence, an incremental fsync (when the inode doesn't have the full sync flag) was a NOOP, since it relies on the existence of extent maps in the modified list of the inode's extent map tree, which was empty. Therefore add new extent maps to reflect the target clone range.

A test case for xfstests follows.

---

V2: Factored out needed code from inode.c:btrfs_get_extent() into a
separate function so that it can be reused by the cloning code, avoiding some duplicated and non-trivial logic when populating an extent map from a file extent item.

V3: Removed unused function parameter, leftover from V1.

 fs/btrfs/ctree.h     |  5 ++++
 fs/btrfs/file-item.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/inode.c     | 41 +++----------------------------
 fs/btrfs/ioctl.c     | 65 +++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 141 insertions(+), 38 deletions(-)
fs/btrfs/ctree.h +5 -0
index af523d6..a668fd9 100644
@@ -3749,6 +3749,11 @@ int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
 		       struct bio *bio, u64 file_start, int contig);
 int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
 			     struct list_head *list, int search_commit);
+void btrfs_extent_item_to_extent_map(struct inode *inode,
+				     const struct btrfs_path *path,
+				     struct btrfs_file_extent_item *fi,
+				     struct extent_map *em);
+
 /* inode.c */
 struct btrfs_delalloc_work {
 	struct inode *inode;
fs/btrfs/file-item.c +68 -0

71 lines not on this page · raw message

fs/btrfs/inode.c +3 -38

86 lines not on this page · raw message

fs/btrfs/ioctl.c +65 -0

85 lines not on this page · raw message

Signature · 1 line
1.9.1
#4 lore raw

PATCH v4 1/1 Btrfs: make fsync work after cloning into a file

In reply to Filipe David Borba Manana

When cloning into a file, we were correctly replacing the extent items in the target range and removing the extent maps. However we weren't replacing the extent maps with new ones that point to the new extents - as a consequence, an incremental fsync (when the inode doesn't have the full sync flag) was a NOOP, since it relies on the existence of extent maps in the modified list of the inode's extent map tree, which was empty. Therefore add new extent maps to reflect the target clone range.

A test case for xfstests follows.

---

V2: Factored out needed code from inode.c:btrfs_get_extent() into a
separate function so that it can be reused by the cloning code, avoiding some duplicated and non-trivial logic when populating an extent map from a file extent item.

V3: Removed unused function parameter, leftover from V1.

V4: Simplified some code in mapping from file extent item to extent map.

 fs/btrfs/ctree.h     |  5 ++++
 fs/btrfs/file-item.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/inode.c     | 41 +++-----------------------------
 fs/btrfs/ioctl.c     | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 139 insertions(+), 38 deletions(-)
fs/btrfs/ctree.h +5 -0
index af523d6..a668fd9 100644
@@ -3749,6 +3749,11 @@ int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
 		       struct bio *bio, u64 file_start, int contig);
 int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
 			     struct list_head *list, int search_commit);
+void btrfs_extent_item_to_extent_map(struct inode *inode,
+				     const struct btrfs_path *path,
+				     struct btrfs_file_extent_item *fi,
+				     struct extent_map *em);
+
 /* inode.c */
 struct btrfs_delalloc_work {
 	struct inode *inode;
fs/btrfs/file-item.c +66 -0

69 lines not on this page · raw message

fs/btrfs/inode.c +3 -38

86 lines not on this page · raw message

fs/btrfs/ioctl.c +65 -0

85 lines not on this page · raw message

Signature · 1 line
1.9.1
#5 lore raw

PATCH v5 1/1 Btrfs: make fsync work after cloning into a file

In reply to Filipe David Borba Manana

When cloning into a file, we were correctly replacing the extent items in the target range and removing the extent maps. However we weren't replacing the extent maps with new ones that point to the new extents - as a consequence, an incremental fsync (when the inode doesn't have the full sync flag) was a NOOP, since it relies on the existence of extent maps in the modified list of the inode's extent map tree, which was empty. Therefore add new extent maps to reflect the target clone range.

A test case for xfstests follows.

---

V2: Factored out needed code from inode.c:btrfs_get_extent() into a
separate function so that it can be reused by the cloning code, avoiding some duplicated and non-trivial logic when populating an extent map from a file extent item.

V3: Removed unused function parameter, leftover from V1.

V4: Simplified some code in mapping from file extent item to extent map.

V5: Corrected refactoring to have the same exact behaviour as before (in
btrfs_get_extent) for new inline extents. Fixed an issue introduced in V4 that made xfstests/generic/269 trigger an error and the following warning in dmesg:

[13229.752008] WARNING: CPU: 1 PID: 13326 at fs/btrfs/extent_io.c:5097 map_private_extent_buffer+0xd4/0xe0 [btrfs]() [13229.752383] btrfs bad mapping eb start 78897152 len 4096, wanted 4098 8

Updated the corresponding test case for xfstests to test for inline file extents.

 fs/btrfs/ctree.h     |  6 +++++
 fs/btrfs/file-item.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/inode.c     | 42 +++--------------------------
 fs/btrfs/ioctl.c     | 69 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 155 insertions(+), 38 deletions(-)
fs/btrfs/ctree.h +6 -0
index af523d6..b7e2c1c 100644
@@ -3749,6 +3749,12 @@ int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
 		       struct bio *bio, u64 file_start, int contig);
 int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
 			     struct list_head *list, int search_commit);
+void btrfs_extent_item_to_extent_map(struct inode *inode,
+				     const struct btrfs_path *path,
+				     struct btrfs_file_extent_item *fi,
+				     const bool new_inline,
+				     struct extent_map *em);
+
 /* inode.c */
 struct btrfs_delalloc_work {
 	struct inode *inode;
fs/btrfs/file-item.c +76 -0

79 lines not on this page · raw message

fs/btrfs/inode.c +4 -38

87 lines not on this page · raw message

fs/btrfs/ioctl.c +69 -0

89 lines not on this page · raw message

Signature · 1 line
1.9.1
#6 lore raw
Quoted 10 lines · Filipe David Borba Manana

When cloning into a file, we were correctly replacing the extent items in the target range and removing the extent maps. However we weren't replacing the extent maps with new ones that point to the new extents - as a consequence, an incremental fsync (when the inode doesn't have the full sync flag) was a NOOP, since it relies on the existence of extent maps in the modified list of the inode's extent map tree, which was empty. Therefore add new extent maps to reflect the target clone range.

A test case for xfstests follows.

[ ... ]

fs/btrfs/ioctl.c process_slot: · line 3480

Quoted 12 lines
fs/btrfs/ioctl.c +4 -0
@@ -3406,6 +3471,10 @@ process_slot:
 		}
 		ret = clone_finish_inode_update(trans, inode, destoff + len,
 						destoff, olen);
+		if (ret)
+			goto out;
+		clone_update_extent_map(inode, trans, path, NULL, last_dest_end,
+					destoff + len - last_dest_end);
 	}
 
 out:

path has already been released by this point, I'm getting use-fater-free crashes during xfstests.

-chris