Skip to contentSkip to conversation

Btrfs-progs: optimize function btrfs_read_chunk_tree

PATCH Superseded Filipe David Borba Manana 2 participants 5 messages Jul 5, 2013 – Jul 31, 2013 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs-progs: optimize function btrfs_read_chunk_tree

After reading all device items from the chunk tree, don't exit the loop and then navigate down the tree again to find the chunk items. Instead, if while reading the device items we find a chunk item in the leaf keep iterating over the leaf and process the chunk items - at the moment chunk items always follow immediately the device items in the chunk tree, and if this fact changes in the future, revert back to previous behaviour of exiting the loop and navigate down the tree again to search for chunk items, but use BTRFS_FIRST_CHUNK_TREE_OBJECTID as the object id for the search key, as any chunk item has a key that has this object id.

This is a port of the corresponding kernel patch to keep both kernel and btrfs-progs identical:

https://patchwork.kernel.org/patch/2816401/

---
 volumes.c |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
volumes.c +7 -2
index 0ff2283..22d0f31 100644
@@ -1740,8 +1740,13 @@ again:
 		}
 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
 		if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
-			if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
+			if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID) {
+				if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
+					key.objectid = found_key.objectid;
+					continue;
+				}
 				break;
+			}
 			if (found_key.type == BTRFS_DEV_ITEM_KEY) {
 				struct btrfs_dev_item *dev_item;
 				dev_item = btrfs_item_ptr(leaf, slot,
@@ -1758,7 +1763,7 @@ again:
 		path->slots[0]++;
 	}
 	if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
-		key.objectid = 0;
+		key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
 		btrfs_release_path(root, path);
 		goto again;
 	}
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH v2 1/1 Btrfs-progs: optimize function btrfs_read_chunk_tree

After reading all device items from the chunk tree, don't exit the loop and then navigate down the tree again to find the chunk items. Instead just read all device items and chunk items with a single tree search. This is possible because all device items are found before any chunk item in the chunks tree.

This is a port of the corresponding kernel patch to keep both kernel and btrfs-progs identical:

https://patchwork.kernel.org/patch/2835105/

---

V2: Simplified logic inside the loop
(suggested by Josef Bacik on irc).

 volumes.c |   28 +++++++++-------------------
 1 file changed, 9 insertions(+), 19 deletions(-)
volumes.c +9 -19
index 0ff2283..2c69f28 100644
@@ -1718,14 +1718,13 @@ int btrfs_read_chunk_tree(struct btrfs_root *root)
 	if (!path)
 		return -ENOMEM;
 
-	/* first we search for all of the device items, and then we
-	 * read in all of the chunk items.  This way we can create chunk
-	 * mappings that reference all of the devices that are afound
-	 */
+	/* Read all device items, and then all the chunk items. All
+	   device items are found before any chunk item (their object id
+	   is smaller than the lowest possible object id for a chunk
+	   item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). */
 	key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
 	key.offset = 0;
 	key.type = 0;
-again:
 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
 	while(1) {
 		leaf = path->nodes[0];
@@ -1739,16 +1738,12 @@ again:
 			break;
 		}
 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
-		if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
-			if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
-				break;
-			if (found_key.type == BTRFS_DEV_ITEM_KEY) {
-				struct btrfs_dev_item *dev_item;
-				dev_item = btrfs_item_ptr(leaf, slot,
+		if (found_key.type == BTRFS_DEV_ITEM_KEY) {
+			struct btrfs_dev_item *dev_item;
+			dev_item = btrfs_item_ptr(leaf, slot,
 						  struct btrfs_dev_item);
-				ret = read_one_dev(root, leaf, dev_item);
-				BUG_ON(ret);
-			}
+			ret = read_one_dev(root, leaf, dev_item);
+			BUG_ON(ret);
 		} else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
 			struct btrfs_chunk *chunk;
 			chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
@@ -1757,11 +1752,6 @@ again:
 		}
 		path->slots[0]++;
 	}
-	if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
-		key.objectid = 0;
-		btrfs_release_path(root, path);
-		goto again;
-	}
 
 	ret = 0;
 error:
Signature · 1 line
1.7.9.5
Reviewed-by on v2 1/1
#2 lore raw
Quoted 10 lines · Filipe David Borba Manana

After reading all device items from the chunk tree, don't exit the loop and then navigate down the tree again to find the chunk items. Instead just read all device items and chunk items with a single tree search. This is possible because all device items are found before any chunk item in the chunks tree.

This is a port of the corresponding kernel patch to keep both kernel and btrfs-progs identical:

Quoted 7 lines

https://patchwork.kernel.org/patch/2835105/

---

V2: Simplified logic inside the loop
(suggested by Josef Bacik on irc).

The comment is the same to the kernel side(Coding style problem of the comment).

Quoted 62 lines
 volumes.c |   28 +++++++++-------------------
 1 file changed, 9 insertions(+), 19 deletions(-)
volumes.c +9 -19
index 0ff2283..2c69f28 100644
@@ -1718,14 +1718,13 @@ int btrfs_read_chunk_tree(struct btrfs_root *root)
 	if (!path)
 		return -ENOMEM;
 
-	/* first we search for all of the device items, and then we
-	 * read in all of the chunk items.  This way we can create chunk
-	 * mappings that reference all of the devices that are afound
-	 */
+	/* Read all device items, and then all the chunk items. All
+	   device items are found before any chunk item (their object id
+	   is smaller than the lowest possible object id for a chunk
+	   item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). */
 	key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
 	key.offset = 0;
 	key.type = 0;
-again:
 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
 	while(1) {
 		leaf = path->nodes[0];
@@ -1739,16 +1738,12 @@ again:
 			break;
 		}
 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
-		if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
-			if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
-				break;
-			if (found_key.type == BTRFS_DEV_ITEM_KEY) {
-				struct btrfs_dev_item *dev_item;
-				dev_item = btrfs_item_ptr(leaf, slot,
+		if (found_key.type == BTRFS_DEV_ITEM_KEY) {
+			struct btrfs_dev_item *dev_item;
+			dev_item = btrfs_item_ptr(leaf, slot,
 						  struct btrfs_dev_item);
-				ret = read_one_dev(root, leaf, dev_item);
-				BUG_ON(ret);
-			}
+			ret = read_one_dev(root, leaf, dev_item);
+			BUG_ON(ret);
 		} else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
 			struct btrfs_chunk *chunk;
 			chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
@@ -1757,11 +1752,6 @@ again:
 		}
 		path->slots[0]++;
 	}
-	if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
-		key.objectid = 0;
-		btrfs_release_path(root, path);
-		goto again;
-	}
 
 	ret = 0;
 error:
#3 lore raw

PATCH v3 1/1 Btrfs-progs: optimize function btrfs_read_chunk_tree

In reply to Filipe David Borba Manana

After reading all device items from the chunk tree, don't exit the loop and then navigate down the tree again to find the chunk items. Instead just read all device items and chunk items with a single tree search. This is possible because all device items are found before any chunk item in the chunks tree.

This is a port of the corresponding kernel patch to keep both kernel and btrfs-progs identical:

https://patchwork.kernel.org/patch/2835529/

---

V2: Simplified logic inside the loop
(suggested by Josef Bacik on irc).
V3: Updated comment to comply with kernel coding style.

 volumes.c |   28 ++++++++++------------------
 1 file changed, 10 insertions(+), 18 deletions(-)
volumes.c +10 -18
index 0ff2283..0cb3505 100644
@@ -1718,14 +1718,15 @@ int btrfs_read_chunk_tree(struct btrfs_root *root)
 	if (!path)
 		return -ENOMEM;
 
-	/* first we search for all of the device items, and then we
-	 * read in all of the chunk items.  This way we can create chunk
-	 * mappings that reference all of the devices that are afound
+	/*
+	 * Read all device items, and then all the chunk items. All
+	 * device items are found before any chunk item (their object id
+	 * is smaller than the lowest possible object id for a chunk
+	 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
 	 */
 	key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
 	key.offset = 0;
 	key.type = 0;
-again:
 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
 	while(1) {
 		leaf = path->nodes[0];
@@ -1739,16 +1740,12 @@ again:
 			break;
 		}
 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
-		if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
-			if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
-				break;
-			if (found_key.type == BTRFS_DEV_ITEM_KEY) {
-				struct btrfs_dev_item *dev_item;
-				dev_item = btrfs_item_ptr(leaf, slot,
+		if (found_key.type == BTRFS_DEV_ITEM_KEY) {
+			struct btrfs_dev_item *dev_item;
+			dev_item = btrfs_item_ptr(leaf, slot,
 						  struct btrfs_dev_item);
-				ret = read_one_dev(root, leaf, dev_item);
-				BUG_ON(ret);
-			}
+			ret = read_one_dev(root, leaf, dev_item);
+			BUG_ON(ret);
 		} else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
 			struct btrfs_chunk *chunk;
 			chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
@@ -1757,11 +1754,6 @@ again:
 		}
 		path->slots[0]++;
 	}
-	if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
-		key.objectid = 0;
-		btrfs_release_path(root, path);
-		goto again;
-	}
 
 	ret = 0;
 error:
Signature · 1 line
1.7.9.5
Reviewed-by on v3 1/1
#4 lore raw
Quoted 10 lines · Filipe David Borba Manana

After reading all device items from the chunk tree, don't exit the loop and then navigate down the tree again to find the chunk items. Instead just read all device items and chunk items with a single tree search. This is possible because all device items are found before any chunk item in the chunks tree.

This is a port of the corresponding kernel patch to keep both kernel and btrfs-progs identical:

Quoted 8 lines

https://patchwork.kernel.org/patch/2835529/

---

V2: Simplified logic inside the loop
(suggested by Josef Bacik on irc).
V3: Updated comment to comply with kernel coding style.

Quoted 63 lines
 volumes.c |   28 ++++++++++------------------
 1 file changed, 10 insertions(+), 18 deletions(-)
volumes.c +10 -18
index 0ff2283..0cb3505 100644
@@ -1718,14 +1718,15 @@ int btrfs_read_chunk_tree(struct btrfs_root *root)
 	if (!path)
 		return -ENOMEM;
 
-	/* first we search for all of the device items, and then we
-	 * read in all of the chunk items.  This way we can create chunk
-	 * mappings that reference all of the devices that are afound
+	/*
+	 * Read all device items, and then all the chunk items. All
+	 * device items are found before any chunk item (their object id
+	 * is smaller than the lowest possible object id for a chunk
+	 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
 	 */
 	key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
 	key.offset = 0;
 	key.type = 0;
-again:
 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
 	while(1) {
 		leaf = path->nodes[0];
@@ -1739,16 +1740,12 @@ again:
 			break;
 		}
 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
-		if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
-			if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
-				break;
-			if (found_key.type == BTRFS_DEV_ITEM_KEY) {
-				struct btrfs_dev_item *dev_item;
-				dev_item = btrfs_item_ptr(leaf, slot,
+		if (found_key.type == BTRFS_DEV_ITEM_KEY) {
+			struct btrfs_dev_item *dev_item;
+			dev_item = btrfs_item_ptr(leaf, slot,
 						  struct btrfs_dev_item);
-				ret = read_one_dev(root, leaf, dev_item);
-				BUG_ON(ret);
-			}
+			ret = read_one_dev(root, leaf, dev_item);
+			BUG_ON(ret);
 		} else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
 			struct btrfs_chunk *chunk;
 			chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
@@ -1757,11 +1754,6 @@ again:
 		}
 		path->slots[0]++;
 	}
-	if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
-		key.objectid = 0;
-		btrfs_release_path(root, path);
-		goto again;
-	}
 
 	ret = 0;
 error: