Skip to contentSkip to conversation

btrfs: propagate error from function unpin_extent_cache()

PATCH New Luís Henriques 1 participant 1 message Jul 24, 2023 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 btrfs: propagate error from function unpin_extent_cache()

Function unpin_extent_cache() currently never returns any error, even in the event of an extent lookup failure. This commit fixes this by returning -EINVAL when no extent is found. Additionally, it adds extra information that may help debug any (hopefully) rare occasion when a lookup fails. The only caller of this function is also modified to handle this error condition.

---

Changes since v2:
- As per Filipe's suggestion, provide as much debug info as possible. This required modifying the function to get the inode instead of the ext map tree.
- Since Filipe mentioned this WARN_ON() has been hit before, I guess it's better to also handle this error in the caller instead of just continue. But this is a change of the current behaviour and I may be wrong.

Changes since v1:
Instead of changing unpin_extent_cache() into a void function, make it propagate an error code instead.

 fs/btrfs/extent_map.c | 26 +++++++++++++++++++++-----
 fs/btrfs/extent_map.h |  2 +-
 fs/btrfs/inode.c      |  8 +++++---
 3 files changed, 27 insertions(+), 9 deletions(-)
fs/btrfs/extent_map.c +21 -5
index 0cdb3e86f29b..1a2e791cbfb4 100644
@@ -292,20 +292,37 @@ static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
  * to the generation that actually added the file item to the inode so we know
  * we need to sync this extent when we call fsync().
  */
-int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
+int unpin_extent_cache(struct btrfs_inode *inode, u64 start, u64 len,
 		       u64 gen)
 {
 	int ret = 0;
+	struct extent_map_tree *tree = &inode->extent_tree;
 	struct extent_map *em;
 	bool prealloc = false;
 
 	write_lock(&tree->lock);
 	em = lookup_extent_mapping(tree, start, len);
 
-	WARN_ON(!em || em->start != start);
+	if (!em || em->start != start) {
+		struct btrfs_fs_info *fs_info = inode->root->fs_info;
 
-	if (!em)
-		goto out;
+		if (!em)
+			btrfs_warn(fs_info,
+				   "failed to find extent map");
+		else
+			btrfs_warn(fs_info,
+		"extent with wrong offset: start: %llu len: %llu flags: %lu",
+				   em->start, em->len, em->flags);
+		btrfs_warn(fs_info,
+			   "root %lld inode %llu offset %llu len: %llu",
+			   inode->root->root_key.objectid, btrfs_ino(inode),
+			   start, len);
+		WARN_ON(1);
+		if (!em) {
+			ret = -EINVAL;
+			goto out;
+		}
+	}
 
 	em->generation = gen;
 	clear_bit(EXTENT_FLAG_PINNED, &em->flags);
@@ -328,7 +345,6 @@ int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
 out:
 	write_unlock(&tree->lock);
 	return ret;
-
 }
 
 void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
fs/btrfs/extent_map.h +1 -1
index 35d27c756e08..7a4ff55ee133 100644
@@ -97,7 +97,7 @@ struct extent_map *alloc_extent_map(void);
 void free_extent_map(struct extent_map *em);
 int __init extent_map_init(void);
 void __cold extent_map_exit(void);
-int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len, u64 gen);
+int unpin_extent_cache(struct btrfs_inode *inode, u64 start, u64 len, u64 gen);
 void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em);
 struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
 					 u64 start, u64 len);
fs/btrfs/inode.c +5 -3
index 49cef61f6a39..90efc2582f81 100644
@@ -3181,7 +3181,7 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent)
 	struct extent_state *cached_state = NULL;
 	u64 start, end;
 	int compress_type = 0;
-	int ret = 0;
+	int ret = 0, ret2;
 	u64 logical_len = ordered_extent->num_bytes;
 	bool freespace_inode;
 	bool truncated = false;
@@ -3273,8 +3273,10 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent)
 						ordered_extent->disk_num_bytes);
 		}
 	}
-	unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,
-			   ordered_extent->num_bytes, trans->transid);
+	ret2 = unpin_extent_cache(inode, ordered_extent->file_offset,
+				  ordered_extent->num_bytes, trans->transid);
+	if (ret >= 0)
+		ret = ret2;
 	if (ret < 0) {
 		btrfs_abort_transaction(trans, ret);
 		goto out;