Skip to contentSkip to conversation

Btrfs: bulk delete checksum items in the same leaf

PATCH In review [email protected] 3 participants 5 messages Jan 30, 2017 – Jan 31, 2017 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs: bulk delete checksum items in the same leaf

From: Filipe Manana <[email protected]>

Very often we have the checksums for an extent spread in multiple items in the checksums tree, and currently the algorithm to delete them starts by looking for them one by one and then deleting them one by one, which is not optimal since each deletion involves shifting all the other items in the leaf and when the leaf reaches some low threshold, to move items off the leaf into its left and right neighbor leafs. Also, after each item deletion we release our search path and start a new search for other checksums items.

So optimize this by deleting in bulk all the items in the same leaf that contain checksums for the extent being freed.

---
 fs/btrfs/file-item.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
fs/btrfs/file-item.c +27 -1
index e97e322..d7d6d4a 100644
@@ -643,7 +643,33 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans,
 
 		/* delete the entire item, it is inside our range */
 		if (key.offset >= bytenr && csum_end <= end_byte) {
-			ret = btrfs_del_item(trans, root, path);
+			int del_nr = 1;
+
+			/*
+			 * Check how many csum items preceding this one in this
+			 * leaf correspond to our range and then delete them all
+			 * at once.
+			 */
+			if (key.offset > bytenr && path->slots[0] > 0) {
+				int slot = path->slots[0] - 1;
+
+				while (slot >= 0) {
+					struct btrfs_key pk;
+
+					btrfs_item_key_to_cpu(leaf, &pk, slot);
+					if (pk.offset < bytenr ||
+					    pk.type != BTRFS_EXTENT_CSUM_KEY ||
+					    pk.objectid !=
+					    BTRFS_EXTENT_CSUM_OBJECTID)
+						break;
+					path->slots[0] = slot;
+					del_nr++;
+					key.offset = pk.offset;
+					slot--;
+				}
+			}
+			ret = btrfs_del_items(trans, root, path,
+					      path->slots[0], del_nr);
 			if (ret)
 				goto out;
 			if (key.offset == bytenr)
Signature · 1 line
2.7.0.rc3
#1 lore raw
Quoted 48 lines · On Sat, Jan 28, 2017 at 06:06:32AM +0000, [email protected] wrote:

From: Filipe Manana <[email protected]>

Very often we have the checksums for an extent spread in multiple items in the checksums tree, and currently the algorithm to delete them starts by looking for them one by one and then deleting them one by one, which is not optimal since each deletion involves shifting all the other items in the leaf and when the leaf reaches some low threshold, to move items off the leaf into its left and right neighbor leafs. Also, after each item deletion we release our search path and start a new search for other checksums items.

So optimize this by deleting in bulk all the items in the same leaf that contain checksums for the extent being freed.

---
 fs/btrfs/file-item.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
fs/btrfs/file-item.c +20 -1
index e97e322..d7d6d4a 100644
@@ -643,7 +643,33 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans,
 
 		/* delete the entire item, it is inside our range */
 		if (key.offset >= bytenr && csum_end <= end_byte) {
-			ret = btrfs_del_item(trans, root, path);
+			int del_nr = 1;
+
+			/*
+			 * Check how many csum items preceding this one in this
+			 * leaf correspond to our range and then delete them all
+			 * at once.
+			 */
+			if (key.offset > bytenr && path->slots[0] > 0) {
+				int slot = path->slots[0] - 1;
+
+				while (slot >= 0) {
+					struct btrfs_key pk;
+
+					btrfs_item_key_to_cpu(leaf, &pk, slot);
+					if (pk.offset < bytenr ||
+					    pk.type != BTRFS_EXTENT_CSUM_KEY ||
+					    pk.objectid !=
+					    BTRFS_EXTENT_CSUM_OBJECTID)
+						break;
+					path->slots[0] = slot;

fs/btrfs/file-item.c int btrfs_del_csums(struct btrfs_trans_handle *trans, · line 675

Quoted 10 lines
fs/btrfs/file-item.c +7 -0
+					del_nr++;
+					key.offset = pk.offset;
+					slot--;
+				}
+			}
+			ret = btrfs_del_items(trans, root, path,
+					      path->slots[0], del_nr);
 			if (ret)
 				goto out;
 			if (key.offset == bytenr)

Hmm, this seems like the kind of operation that could use a helper. btrfs_del_item_range() or something like that, which takes the maximum key to delete. What do you think?

#2 lore raw
Quoted 55 lines · Omar Sandoval
Quoted 54 lines · On Sat, Jan 28, 2017 at 06:06:32AM +0000, [email protected] wrote:

From: Filipe Manana <[email protected]>

Very often we have the checksums for an extent spread in multiple items in the checksums tree, and currently the algorithm to delete them starts by looking for them one by one and then deleting them one by one, which is not optimal since each deletion involves shifting all the other items in the leaf and when the leaf reaches some low threshold, to move items off the leaf into its left and right neighbor leafs. Also, after each item deletion we release our search path and start a new search for other checksums items.

So optimize this by deleting in bulk all the items in the same leaf that contain checksums for the extent being freed.

---
 fs/btrfs/file-item.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
fs/btrfs/file-item.c +26 -1
index e97e322..d7d6d4a 100644
@@ -643,7 +643,33 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans,
 
 		/* delete the entire item, it is inside our range */
 		if (key.offset >= bytenr && csum_end <= end_byte) {
-			ret = btrfs_del_item(trans, root, path);
+			int del_nr = 1;
+
+			/*
+			 * Check how many csum items preceding this one in this
+			 * leaf correspond to our range and then delete them all
+			 * at once.
+			 */
+			if (key.offset > bytenr && path->slots[0] > 0) {
+				int slot = path->slots[0] - 1;
+
+				while (slot >= 0) {
+					struct btrfs_key pk;
+
+					btrfs_item_key_to_cpu(leaf, &pk, slot);
+					if (pk.offset < bytenr ||
+					    pk.type != BTRFS_EXTENT_CSUM_KEY ||
+					    pk.objectid !=
+					    BTRFS_EXTENT_CSUM_OBJECTID)
+						break;
+					path->slots[0] = slot;
+					del_nr++;
+					key.offset = pk.offset;
+					slot--;
+				}
+			}
+			ret = btrfs_del_items(trans, root, path,
Quoted 8 lines

fs/btrfs/file-item.c int btrfs_del_csums(struct btrfs_trans_handle *trans, · line 675

Quoted 4 lines
fs/btrfs/file-item.c +1 -0
+					      path->slots[0], del_nr);
 			if (ret)
 				goto out;
 			if (key.offset == bytenr)

Hmm, this seems like the kind of operation that could use a helper. btrfs_del_item_range() or something like that, which takes the maximum key to delete. What do you think?

Err, or in this case, the minimum key.

#3 lore raw
In reply to Omar Sandoval
Quoted 55 lines · Omar Sandoval
Quoted 54 lines · On Sat, Jan 28, 2017 at 06:06:32AM +0000, [email protected] wrote:

From: Filipe Manana <[email protected]>

Very often we have the checksums for an extent spread in multiple items in the checksums tree, and currently the algorithm to delete them starts by looking for them one by one and then deleting them one by one, which is not optimal since each deletion involves shifting all the other items in the leaf and when the leaf reaches some low threshold, to move items off the leaf into its left and right neighbor leafs. Also, after each item deletion we release our search path and start a new search for other checksums items.

So optimize this by deleting in bulk all the items in the same leaf that contain checksums for the extent being freed.

---
 fs/btrfs/file-item.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
fs/btrfs/file-item.c +26 -1
index e97e322..d7d6d4a 100644
@@ -643,7 +643,33 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans,
 
              /* delete the entire item, it is inside our range */
              if (key.offset >= bytenr && csum_end <= end_byte) {
-                     ret = btrfs_del_item(trans, root, path);
+                     int del_nr = 1;
+
+                     /*
+                      * Check how many csum items preceding this one in this
+                      * leaf correspond to our range and then delete them all
+                      * at once.
+                      */
+                     if (key.offset > bytenr && path->slots[0] > 0) {
+                             int slot = path->slots[0] - 1;
+
+                             while (slot >= 0) {
+                                     struct btrfs_key pk;
+
+                                     btrfs_item_key_to_cpu(leaf, &pk, slot);
+                                     if (pk.offset < bytenr ||
+                                         pk.type != BTRFS_EXTENT_CSUM_KEY ||
+                                         pk.objectid !=
+                                         BTRFS_EXTENT_CSUM_OBJECTID)
+                                             break;
+                                     path->slots[0] = slot;
+                                     del_nr++;
+                                     key.offset = pk.offset;
+                                     slot--;
+                             }
+                     }
+                     ret = btrfs_del_items(trans, root, path,
Quoted 8 lines

fs/btrfs/file-item.c int btrfs_del_csums(struct btrfs_trans_handle *trans, · line 675

Quoted 4 lines
fs/btrfs/file-item.c +1 -0
+                                           path->slots[0], del_nr);
                      if (ret)
                              goto out;
                      if (key.offset == bytenr)

Hmm, this seems like the kind of operation that could use a helper. btrfs_del_item_range() or something like that, which takes the maximum key to delete. What do you think?

I think it's not necessary. This is a very simple and isolated case with the specific pattern of iterating backwards and returning the last valid key (first in the range to delete). Shall another very similar case pop up, then it's ok (even though it's a very simple and small logic).

Reviewed-by on v1 1/1
#4 lore raw
In reply to [email protected]
Quoted 11 lines · On Sat, Jan 28, 2017 at 06:06:32AM +0000, [email protected] wrote:

From: Filipe Manana <[email protected]>

Very often we have the checksums for an extent spread in multiple items in the checksums tree, and currently the algorithm to delete them starts by looking for them one by one and then deleting them one by one, which is not optimal since each deletion involves shifting all the other items in the leaf and when the leaf reaches some low threshold, to move items off the leaf into its left and right neighbor leafs. Also, after each item deletion we release our search path and start a new search for other checksums items.

Quoted 2 lines

So optimize this by deleting in bulk all the items in the same leaf that contain checksums for the extent being freed.

Looks good.

Thanks,

-liubo

Quoted 52 lines
---
 fs/btrfs/file-item.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
fs/btrfs/file-item.c +27 -1
index e97e322..d7d6d4a 100644
@@ -643,7 +643,33 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans,
 
 		/* delete the entire item, it is inside our range */
 		if (key.offset >= bytenr && csum_end <= end_byte) {
-			ret = btrfs_del_item(trans, root, path);
+			int del_nr = 1;
+
+			/*
+			 * Check how many csum items preceding this one in this
+			 * leaf correspond to our range and then delete them all
+			 * at once.
+			 */
+			if (key.offset > bytenr && path->slots[0] > 0) {
+				int slot = path->slots[0] - 1;
+
+				while (slot >= 0) {
+					struct btrfs_key pk;
+
+					btrfs_item_key_to_cpu(leaf, &pk, slot);
+					if (pk.offset < bytenr ||
+					    pk.type != BTRFS_EXTENT_CSUM_KEY ||
+					    pk.objectid !=
+					    BTRFS_EXTENT_CSUM_OBJECTID)
+						break;
+					path->slots[0] = slot;
+					del_nr++;
+					key.offset = pk.offset;
+					slot--;
+				}
+			}
+			ret = btrfs_del_items(trans, root, path,
+					      path->slots[0], del_nr);
 			if (ret)
 				goto out;
 			if (key.offset == bytenr)
Signature · 6 lines
2.7.0.rc3

--
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