Skip to contentSkip to conversation

Misc cleanups of btrfs_get_extent

PATCH In review Nikolay Borisov 4 participants 21 messages Dec 17, 2018 – Jan 8, 2019 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover Misc cleanups of btrfs_get_extent

Here is a series that hopefully makes the code a bit more obvious. First patch gives a variable a more appropraite name, since it's going to be used only for holding an extent type and not the type of a found item.

Patch 2 simply consolidated separate 'if' statements that check the retval of the same function into a single 'if() {} else if () {}' statement. IMO this is cleaner.

Patch 3 massages the abhorrent code that deals with btrfs_lookup_file_extent retval. It groups everything in a coherent 'if() {} else if () {} else {}' construct and now it's obvious under what conditions specific code is executed.

Finally, Patch 4 removes the not_found_em labelin the same function.

Nikolay Borisov (4):
btrfs: Rename found_type to extent_type
btrfs: Consolidate retval checking of core btree functions
btrfs: Refactor retval handling of btrfs_lookup_file_extent in
btrfs_get_extent
btrfs: Remove not_found_em label from btrfs_get_extent

 fs/btrfs/inode.c | 67 ++++++++++++++++++++++++------------------------
 1 file changed, 34 insertions(+), 33 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/4 btrfs: Rename found_type to extent_type

found_type really holds the type of extent and is guaranteed to to have a value between [0, 2]. The only time it can contain anything different is if btrfs_lookup_file_extent returned a positive value and the previous item is different than an extent. Avoid this situation by simply checking found_key.type rather than assigning the item type to found_type intermittently. Also make the variable an u8 to reduce stack usage. No functional changes.

---
 fs/btrfs/inode.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)
fs/btrfs/inode.c +9 -11
index 73ae4428d0df..fe25f66a98d9 100644
@@ -6746,7 +6746,7 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 	u64 extent_start = 0;
 	u64 extent_end = 0;
 	u64 objectid = btrfs_ino(inode);
-	u32 found_type;
+	u8 extent_type;
 	struct btrfs_path *path = NULL;
 	struct btrfs_root *root = inode->root;
 	struct btrfs_file_extent_item *item;
@@ -6812,11 +6812,9 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 	leaf = path->nodes[0];
 	item = btrfs_item_ptr(leaf, path->slots[0],
 			      struct btrfs_file_extent_item);
-	/* are we inside the extent that was found? */
 	btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
-	found_type = found_key.type;
 	if (found_key.objectid != objectid ||
-	    found_type != BTRFS_EXTENT_DATA_KEY) {
+	    found_key.type != BTRFS_EXTENT_DATA_KEY) {
 		/*
 		 * If we backup past the first extent we want to move forward
 		 * and see if there is an extent in front of us, otherwise we'll
@@ -6827,16 +6825,16 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 		goto next;
 	}
 
-	found_type = btrfs_file_extent_type(leaf, item);
+	extent_type = btrfs_file_extent_type(leaf, item);
 	extent_start = found_key.offset;
-	if (found_type == BTRFS_FILE_EXTENT_REG ||
-	    found_type == BTRFS_FILE_EXTENT_PREALLOC) {
+	if (extent_type == BTRFS_FILE_EXTENT_REG ||
+	    extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
 		extent_end = extent_start +
 		       btrfs_file_extent_num_bytes(leaf, item);
 
 		trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
 						       extent_start);
-	} else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
+	} else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
 		size_t size;
 
 		size = btrfs_file_extent_ram_bytes(leaf, item);
@@ -6877,10 +6875,10 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 	btrfs_extent_item_to_extent_map(inode, path, item,
 			new_inline, em);
 
-	if (found_type == BTRFS_FILE_EXTENT_REG ||
-	    found_type == BTRFS_FILE_EXTENT_PREALLOC) {
+	if (extent_type == BTRFS_FILE_EXTENT_REG ||
+	    extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
 		goto insert;
-	} else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
+	} else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
 		unsigned long ptr;
 		char *map;
 		size_t size;
Signature · 1 line
2.17.1
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] btrfs: Rename found_type to extent_type

Looks good,

Signature · 6 lines
Johannes Thumshirn                            SUSE Labs Filesystems
[email protected]                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
Reviewed-by on v1 1/4
#3 lore raw

Re: [PATCH 1/4] btrfs: Rename found_type to extent_type

In reply to Nikolay Borisov
Quoted 9 lines · Nikolay Borisov

found_type really holds the type of extent and is guaranteed to to have a value between [0, 2]. The only time it can contain anything different is if btrfs_lookup_file_extent returned a positive value and the previous item is different than an extent. Avoid this situation by simply checking found_key.type rather than assigning the item type to found_type intermittently. Also make the variable an u8 to reduce stack usage. No functional changes.

Indeed, abusing u64 for u8 makes it pretty confusing.

Now looks much better.

Thanks,
Qu

Quoted 66 lines
---
 fs/btrfs/inode.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)
fs/btrfs/inode.c +9 -11
index 73ae4428d0df..fe25f66a98d9 100644
@@ -6746,7 +6746,7 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 	u64 extent_start = 0;
 	u64 extent_end = 0;
 	u64 objectid = btrfs_ino(inode);
-	u32 found_type;
+	u8 extent_type;
 	struct btrfs_path *path = NULL;
 	struct btrfs_root *root = inode->root;
 	struct btrfs_file_extent_item *item;
@@ -6812,11 +6812,9 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 	leaf = path->nodes[0];
 	item = btrfs_item_ptr(leaf, path->slots[0],
 			      struct btrfs_file_extent_item);
-	/* are we inside the extent that was found? */
 	btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
-	found_type = found_key.type;
 	if (found_key.objectid != objectid ||
-	    found_type != BTRFS_EXTENT_DATA_KEY) {
+	    found_key.type != BTRFS_EXTENT_DATA_KEY) {
 		/*
 		 * If we backup past the first extent we want to move forward
 		 * and see if there is an extent in front of us, otherwise we'll
@@ -6827,16 +6825,16 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 		goto next;
 	}
 
-	found_type = btrfs_file_extent_type(leaf, item);
+	extent_type = btrfs_file_extent_type(leaf, item);
 	extent_start = found_key.offset;
-	if (found_type == BTRFS_FILE_EXTENT_REG ||
-	    found_type == BTRFS_FILE_EXTENT_PREALLOC) {
+	if (extent_type == BTRFS_FILE_EXTENT_REG ||
+	    extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
 		extent_end = extent_start +
 		       btrfs_file_extent_num_bytes(leaf, item);
 
 		trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
 						       extent_start);
-	} else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
+	} else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
 		size_t size;
 
 		size = btrfs_file_extent_ram_bytes(leaf, item);
@@ -6877,10 +6875,10 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 	btrfs_extent_item_to_extent_map(inode, path, item,
 			new_inline, em);
 
-	if (found_type == BTRFS_FILE_EXTENT_REG ||
-	    found_type == BTRFS_FILE_EXTENT_PREALLOC) {
+	if (extent_type == BTRFS_FILE_EXTENT_REG ||
+	    extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
 		goto insert;
-	} else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
+	} else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
 		unsigned long ptr;
 		char *map;
 		size_t size;
#4 lore raw

PATCH 2/4 btrfs: Consolidate retval checking of core btree functions

In reply to Nikolay Borisov

Core btree functions in btrfs generally return 0 for error and 1 in case the sought item cannot be found. Consolidate the checks for those conditions in one 'if () {} else if () {}' struct rather than 2 separate 'if () {}' statements. This emphasizes that the handling code pertains to a single function. No functional changes.

---
 fs/btrfs/inode.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
fs/btrfs/inode.c +2 -5
index fe25f66a98d9..511d3b314af2 100644
@@ -6801,9 +6801,7 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 	if (ret < 0) {
 		err = ret;
 		goto out;
-	}
-
-	if (ret != 0) {
+	} else if (ret > 0) {
 		if (path->slots[0] == 0)
 			goto not_found;
 		path->slots[0]--;
@@ -6853,8 +6851,7 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 			if (ret < 0) {
 				err = ret;
 				goto out;
-			}
-			if (ret > 0)
+			} else if (ret > 0)
 				goto not_found;
 			leaf = path->nodes[0];
 		}
Signature · 1 line
2.17.1
Reviewed-by on v1 2/4
#5 lore raw

Re: [PATCH 2/4] btrfs: Consolidate retval checking of core btree functions

Quoted 3 lines · Nikolay Borisov

Core btree functions in btrfs generally return 0 for error and 1 in case the sought item cannot be found. Consolidate the checks for those conditions in one 'if () {} else if () {}' struct rather than 2 separate

construct? ^

Otherwise,

Signature · 6 lines
Johannes Thumshirn                            SUSE Labs Filesystems
[email protected]                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
#6 lore raw

Re: [PATCH 2/4] btrfs: Consolidate retval checking of core btree functions

Quoted 1 line · Johannes Thumshirn

construct? ^

Good catch :) David, will you fix it on the way in to the tree or shall I resend.

#7 lore raw

Re: [PATCH 2/4] btrfs: Consolidate retval checking of core btree functions

In reply to Nikolay Borisov
Quoted 1 line · Nikolay Borisov

Core btree functions in btrfs generally return 0 for error

Isn't 0 for key found? It's <0 for error.

and 1 in case

Quoted 6 lines

the sought item cannot be found. Consolidate the checks for those conditions in one 'if () {} else if () {}' struct rather than 2 separate 'if () {}' statements. This emphasizes that the handling code pertains to a single function. No functional changes.

The code itself is OK, but the commit message is really confusing.

Thanks,
Qu

Quoted 30 lines
---
 fs/btrfs/inode.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
fs/btrfs/inode.c +2 -5
index fe25f66a98d9..511d3b314af2 100644
@@ -6801,9 +6801,7 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 	if (ret < 0) {
 		err = ret;
 		goto out;
-	}
-
-	if (ret != 0) {
+	} else if (ret > 0) {
 		if (path->slots[0] == 0)
 			goto not_found;
 		path->slots[0]--;
@@ -6853,8 +6851,7 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 			if (ret < 0) {
 				err = ret;
 				goto out;
-			}
-			if (ret > 0)
+			} else if (ret > 0)
 				goto not_found;
 			leaf = path->nodes[0];
 		}
#8 lore raw

Re: [PATCH 2/4] btrfs: Consolidate retval checking of core btree functions

Quoted 8 lines · Qu Wenruo
Quoted 1 line · Nikolay Borisov

Core btree functions in btrfs generally return 0 for error

Isn't 0 for key found? It's <0 for error.

and 1 in case

Shit, you are right, 0 found, < 0 error, >0 where it must be inserted

Quoted 42 lines
Quoted 6 lines

the sought item cannot be found. Consolidate the checks for those conditions in one 'if () {} else if () {}' struct rather than 2 separate 'if () {}' statements. This emphasizes that the handling code pertains to a single function. No functional changes.

The code itself is OK, but the commit message is really confusing.

Thanks,
Qu

Quoted 30 lines
---
 fs/btrfs/inode.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
fs/btrfs/inode.c +2 -5
index fe25f66a98d9..511d3b314af2 100644
@@ -6801,9 +6801,7 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 	if (ret < 0) {
 		err = ret;
 		goto out;
-	}
-
-	if (ret != 0) {
+	} else if (ret > 0) {
 		if (path->slots[0] == 0)
 			goto not_found;
 		path->slots[0]--;
@@ -6853,8 +6851,7 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 			if (ret < 0) {
 				err = ret;
 				goto out;
-			}
-			if (ret > 0)
+			} else if (ret > 0)
 				goto not_found;
 			leaf = path->nodes[0];
 		}
#9 lore raw

PATCH v2 2/4 btrfs: Consolidate retval checking of core btree functions

In reply to Nikolay Borisov

Core btree functions in btrfs generally return 0 when an item is found, 1 in case the sought item cannot be found and <0 when an error happens. Consolidate the checks for those conditions in one 'if () {} else if () {}' struct rather than 2 separate 'if () {}' statements. This emphasizes that the handling code pertains to a single function. No functional changes.

---

V2:
* Properly describe return value convention in changelog no other changes.

 fs/btrfs/inode.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
fs/btrfs/inode.c +2 -5
index fe25f66a98d9..511d3b314af2 100644
@@ -6801,9 +6801,7 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 	if (ret < 0) {
 		err = ret;
 		goto out;
-	}
-
-	if (ret != 0) {
+	} else if (ret > 0) {
 		if (path->slots[0] == 0)
 			goto not_found;
 		path->slots[0]--;
@@ -6853,8 +6851,7 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 			if (ret < 0) {
 				err = ret;
 				goto out;
-			}
-			if (ret > 0)
+			} else if (ret > 0)
 				goto not_found;
 			leaf = path->nodes[0];
 		}
Signature · 1 line
2.17.1
Reviewed-by on v2 2/4
#10 lore raw

Re: [PATCH v2 2/4] btrfs: Consolidate retval checking of core btree functions

Quoted 7 lines · Nikolay Borisov

Core btree functions in btrfs generally return 0 when an item is found, 1 in case the sought item cannot be found and <0 when an error happens. Consolidate the checks for those conditions in one 'if () {} else if () {}' struct rather than 2 separate 'if () {}' statements. This emphasizes that the handling code pertains to a single function. No functional changes.

Thanks,
Qu

Quoted 34 lines

---

V2:
* Properly describe return value convention in changelog no other changes.

 fs/btrfs/inode.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
fs/btrfs/inode.c +2 -5
index fe25f66a98d9..511d3b314af2 100644
@@ -6801,9 +6801,7 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 	if (ret < 0) {
 		err = ret;
 		goto out;
-	}
-
-	if (ret != 0) {
+	} else if (ret > 0) {
 		if (path->slots[0] == 0)
 			goto not_found;
 		path->slots[0]--;
@@ -6853,8 +6851,7 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 			if (ret < 0) {
 				err = ret;
 				goto out;
-			}
-			if (ret > 0)
+			} else if (ret > 0)
 				goto not_found;
 			leaf = path->nodes[0];
 		}
#11 lore raw

PATCH 3/4 btrfs: Refactor retval handling of btrfs_lookup_file_extent in btrfs_get_extent

In reply to Nikolay Borisov

The code which executes if path->slots[0] points to an item not belonging to the inode we are interested in or is not na extent at all could really trigger if btrfs_lookup_file_extent returned > 0 and subsequently path->slots[0] was decremented. Make this explicit by moving the respective code in the correct if branch. No functional changes.

---
 fs/btrfs/inode.c | 36 ++++++++++++++++++++----------------
 1 file changed, 20 insertions(+), 16 deletions(-)
fs/btrfs/inode.c +20 -16
index 511d3b314af2..e8284cd0a122 100644
@@ -6805,22 +6805,26 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 		if (path->slots[0] == 0)
 			goto not_found;
 		path->slots[0]--;
-	}
-
-	leaf = path->nodes[0];
-	item = btrfs_item_ptr(leaf, path->slots[0],
-			      struct btrfs_file_extent_item);
-	btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
-	if (found_key.objectid != objectid ||
-	    found_key.type != BTRFS_EXTENT_DATA_KEY) {
-		/*
-		 * If we backup past the first extent we want to move forward
-		 * and see if there is an extent in front of us, otherwise we'll
-		 * say there is a hole for our whole search range which can
-		 * cause problems.
-		 */
-		extent_end = start;
-		goto next;
+		leaf = path->nodes[0];
+		item = btrfs_item_ptr(leaf, path->slots[0],
+				      struct btrfs_file_extent_item);
+		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+		if (found_key.objectid != objectid ||
+		    found_key.type != BTRFS_EXTENT_DATA_KEY) {
+			/*
+			 * If we backup past the first extent we want to move
+			 * forward and see if there is an extent in front of us,
+			 * otherwise we'll say there is a hole for our whole
+			 * search range which can cause problems.
+			 */
+			extent_end = start;
+			goto next;
+		}
+	} else {
+		leaf = path->nodes[0];
+		item = btrfs_item_ptr(leaf, path->slots[0],
+				      struct btrfs_file_extent_item);
+		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
 	}
 
 	extent_type = btrfs_file_extent_type(leaf, item);
Signature · 1 line
2.17.1
Reviewed-by on v1 3/4
#12 lore raw

Re: [PATCH 3/4] btrfs: Refactor retval handling of btrfs_lookup_file_extent in btrfs_get_extent

Signature · 6 lines
Johannes Thumshirn                            SUSE Labs Filesystems
[email protected]                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
#13 lore raw

Re: [PATCH 3/4] btrfs: Refactor retval handling of btrfs_lookup_file_extent in btrfs_get_extent

In reply to Nikolay Borisov
Quoted 45 lines · Nikolay Borisov

The code which executes if path->slots[0] points to an item not belonging to the inode we are interested in or is not na extent at all could really trigger if btrfs_lookup_file_extent returned > 0 and subsequently path->slots[0] was decremented. Make this explicit by moving the respective code in the correct if branch. No functional changes.

---
 fs/btrfs/inode.c | 36 ++++++++++++++++++++----------------
 1 file changed, 20 insertions(+), 16 deletions(-)
fs/btrfs/inode.c +9 -16
index 511d3b314af2..e8284cd0a122 100644
@@ -6805,22 +6805,26 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 		if (path->slots[0] == 0)
 			goto not_found;
 		path->slots[0]--;
-	}
-
-	leaf = path->nodes[0];
-	item = btrfs_item_ptr(leaf, path->slots[0],
-			      struct btrfs_file_extent_item);
-	btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
-	if (found_key.objectid != objectid ||
-	    found_key.type != BTRFS_EXTENT_DATA_KEY) {
-		/*
-		 * If we backup past the first extent we want to move forward
-		 * and see if there is an extent in front of us, otherwise we'll
-		 * say there is a hole for our whole search range which can
-		 * cause problems.
-		 */
-		extent_end = start;
-		goto next;
+		leaf = path->nodes[0];
+		item = btrfs_item_ptr(leaf, path->slots[0],
+				      struct btrfs_file_extent_item);
+		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+		if (found_key.objectid != objectid ||
+		    found_key.type != BTRFS_EXTENT_DATA_KEY) {
+			/*
+			 * If we backup past the first extent we want to move
+			 * forward and see if there is an extent in front of us,

fs/btrfs/inode.c struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, · line 6826

Quoted 10 lines
fs/btrfs/inode.c +10 -0
+			 * otherwise we'll say there is a hole for our whole
+			 * search range which can cause problems.
+			 */
+			extent_end = start;
+			goto next;
+		}
+	} else {
+		leaf = path->nodes[0];
+		item = btrfs_item_ptr(leaf, path->slots[0],
+				      struct btrfs_file_extent_item);

This is repeating code and could be simplified ... to the original code. I'm not sure this patch is an improvement.

#14 lore raw

Re: [PATCH 3/4] btrfs: Refactor retval handling of btrfs_lookup_file_extent in btrfs_get_extent

Quoted 1 line · David Sterba

This is repeating code and could be simplified ... to the original code.

It does and this is intentional. What I've strived to do is make the idea of the code obvious and not try to reduce the total line of code. It is a massive improvement given the code which modifies extent_end triggers only in case ret is > 0. I discussed this with Johannes and he agreed with my assessment.

Quoted 1 line

I'm not sure this patch is an improvement.

#15 lore raw

Re: [PATCH 3/4] btrfs: Refactor retval handling of btrfs_lookup_file_extent in btrfs_get_extent

Quoted 12 lines · Nikolay Borisov
Quoted 1 line · David Sterba

This is repeating code and could be simplified ... to the original code.

It does and this is intentional. What I've strived to do is make the idea of the code obvious and not try to reduce the total line of code. It is a massive improvement given the code which modifies extent_end triggers only in case ret is > 0. I discussed this with Johannes and he agreed with my assessment.

Quoted 1 line

I'm not sure this patch is an improvement.

I think it really helps when trying to understand the code.

This is why the patch was created in the first place, I asked Nik about something and we went over the code together.

In the end it's your call, obviously.

Byte,
Johannes

Signature · 6 lines
Johannes Thumshirn                            SUSE Labs Filesystems
[email protected]                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
#16 lore raw

Re: [PATCH 3/4] btrfs: Refactor retval handling of btrfs_lookup_file_extent in btrfs_get_extent

Quoted 10 lines · Johannes Thumshirn
Quoted 9 lines · Nikolay Borisov
Quoted 1 line · David Sterba

This is repeating code and could be simplified ... to the original code.

It does and this is intentional. What I've strived to do is make the idea of the code obvious and not try to reduce the total line of code. It is a massive improvement given the code which modifies extent_end triggers only in case ret is > 0. I discussed this with Johannes and he agreed with my assessment.

Quoted 6 lines
Quoted 1 line
Quoted 1 line

I'm not sure this patch is an improvement.

I think it really helps when trying to understand the code.

This is why the patch was created in the first place, I asked Nik about something and we went over the code together.

I've tried to read the new version again with fresh eyes and still don't see any improvement in readability. Not that the whole function is easy to follow, quite the opposite, but I'd rather see continued refactoring that untangles the gotos.

The return codes of btrfs_lookup_file_extent follow the common btrfs_search_slot pattern < 0 error, 0 found, > 0 not found. And in the last case there's an adjustment needed. Read the item type, check and branch if necessary. No surprises there.

The amount of duplicated code is also not something trivial like a variable asisgnment, there are arguments passed etc.

#17 lore raw

Re: [PATCH 3/4] btrfs: Refactor retval handling of btrfs_lookup_file_extent in btrfs_get_extent

Quoted 23 lines · David Sterba
Quoted 10 lines · Johannes Thumshirn
Quoted 9 lines · Nikolay Borisov
Quoted 1 line · David Sterba

This is repeating code and could be simplified ... to the original code.

It does and this is intentional. What I've strived to do is make the idea of the code obvious and not try to reduce the total line of code. It is a massive improvement given the code which modifies extent_end triggers only in case ret is > 0. I discussed this with Johannes and he agreed with my assessment.

Quoted 6 lines
Quoted 1 line
Quoted 1 line

I'm not sure this patch is an improvement.

I think it really helps when trying to understand the code.

This is why the patch was created in the first place, I asked Nik about something and we went over the code together.

I've tried to read the new version again with fresh eyes and still don't see any improvement in readability. Not that the whole function is easy to follow, quite the opposite, but I'd rather see continued refactoring that untangles the gotos.

Quoted 4 lines

The return codes of btrfs_lookup_file_extent follow the common btrfs_search_slot pattern < 0 error, 0 found, > 0 not found. And in the last case there's an adjustment needed. Read the item type, check and branch if necessary. No surprises there.

Actually there is a surprise, this surprise is due to the fact that it's not explicitly clear the adjustments happens when > 0 is returned. With my patch it's clear which code gets executed only when the respective extent item wasn't found.

Quoted 3 lines

The amount of duplicated code is also not something trivial like a variable asisgnment, there are arguments passed etc.

The amount of duplication are 3 lines of linear code, I think this is just being overly nitpicky given the (IMO) upside of this change.

Quoted 1 line
#18 lore raw

PATCH 4/4 btrfs: Remove not_found_em label from btrfs_get_extent

In reply to Nikolay Borisov

In order to avoid duplicating init code for em there is an additional label, not_found_em, which is used to only set ->block_start. The only case when it will be used is if the extent we are adding overlaps with an existing extent. Make that case more obvious by :
1. Adding a comment hinting at what's going on
2. Assigning EXTENT_MAP_HOLE and directly going to insert.

No functional changes.

---
 fs/btrfs/inode.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/btrfs/inode.c +4 -2
index e8284cd0a122..0b9855c2df21 100644
@@ -6867,10 +6867,13 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 			goto not_found;
 		if (start > found_key.offset)
 			goto next;
+
+		/* New extent overlaps with existing one */
 		em->start = start;
 		em->orig_start = start;
 		em->len = found_key.offset - start;
-		goto not_found_em;
+		em->block_start = EXTENT_MAP_HOLE;
+		goto insert;
 	}
 
 	btrfs_extent_item_to_extent_map(inode, path, item,
@@ -6930,7 +6933,6 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 	em->start = start;
 	em->orig_start = start;
 	em->len = len;
-not_found_em:
 	em->block_start = EXTENT_MAP_HOLE;
 insert:
 	btrfs_release_path(path);
Signature · 1 line
2.17.1
Reviewed-by on v1 4/4
#19 lore raw

Re: [PATCH 4/4] btrfs: Remove not_found_em label from btrfs_get_extent

Looks good,

Signature · 6 lines
Johannes Thumshirn                            SUSE Labs Filesystems
[email protected]                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
#20 lore raw
In reply to Nikolay Borisov
Quoted 12 lines · Nikolay Borisov

Here is a series that hopefully makes the code a bit more obvious. First patch gives a variable a more appropraite name, since it's going to be used only for holding an extent type and not the type of a found item.

Patch 2 simply consolidated separate 'if' statements that check the retval of the same function into a single 'if() {} else if () {}' statement. IMO this is cleaner.

Patch 3 massages the abhorrent code that deals with btrfs_lookup_file_extent retval. It groups everything in a coherent 'if() {} else if () {} else {}' construct and now it's obvious under what conditions specific code is executed.

Quoted 8 lines

Finally, Patch 4 removes the not_found_em labelin the same function.

Nikolay Borisov (4):
btrfs: Rename found_type to extent_type
btrfs: Consolidate retval checking of core btree functions
btrfs: Refactor retval handling of btrfs_lookup_file_extent in
btrfs_get_extent
btrfs: Remove not_found_em label from btrfs_get_extent

1, 2 and 4 now added to for-next, thanks.