Skip to contentSkip to conversation

Btrfs: don't BUG() during drop snapshot

PATCH Superseded David Sterba 2 participants 4 messages Sep 22, 2016 – Sep 23, 2016 Btrfs file system

lore
Share & tools
lore

David Sterba

author maintainer
#0 lore raw

PATCH 1/1 Btrfs: don't BUG() during drop snapshot

From: Josef Bacik <[email protected]>

Really there's lots of things that can go wrong here, kill all the BUG_ON()'s and replace the logic ones with ASSERT()'s and return EIO instead.

---
 fs/btrfs/extent-tree.c | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)
fs/btrfs/extent-tree.c +23 -4
index c95f85c292a4..37aba7d00c8f 100644
@@ -8884,12 +8884,15 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
 				       &wc->flags[level - 1]);
 	if (ret < 0) {
 		btrfs_tree_unlock(next);
+		free_extent_buffer(next);
 		return ret;
 	}
 
 	if (unlikely(wc->refs[level - 1] == 0)) {
 		btrfs_err(root->fs_info, "Missing references.");
-		BUG();
+		btrfs_tree_unlock(next);
+		free_extent_buffer(next);
+		return -EIO;
 	}
 	*lookup_info = 0;
 
@@ -8941,7 +8944,13 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
 	}
 
 	level--;
-	BUG_ON(level != btrfs_header_level(next));
+	ASSERT(level == btrfs_header_level(next));
+	if (level != btrfs_header_level(next)) {
+		btrfs_err("mismatched level");
+		btrfs_tree_unlock(next);
+		free_extent_buffer(next);
+		return -EIO;
+	}
 	path->nodes[level] = next;
 	path->slots[level] = 0;
 	path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
@@ -8956,8 +8965,14 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
 		if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
 			parent = path->nodes[level]->start;
 		} else {
-			BUG_ON(root->root_key.objectid !=
+			ASSERT(root->root_key.objectid ==
 			       btrfs_header_owner(path->nodes[level]));
+			if (root->root_key.objectid !=
+			    btrfs_header_owner(path->nodes[level])) {
+				btrfs_err("mismatched block owner");
+				btrfs_tree_unlock(next);
+				free_extent_buffer(next);
+			}
 			parent = 0;
 		}
 
@@ -8972,7 +8987,11 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
 		}
 		ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
 				root->root_key.objectid, level - 1, 0);
-		BUG_ON(ret); /* -ENOMEM */
+		if (ret) {
+			btrfs_tree_unlock(next);
+			free_extent_buffer(next);
+			return ret;
+		}
 	}
 	btrfs_tree_unlock(next);
 	free_extent_buffer(next);
Signature · 1 line
2.7.1
#1 lore raw
Quoted 50 lines · David Sterba

From: Josef Bacik <[email protected]>

Really there's lots of things that can go wrong here, kill all the BUG_ON()'s and replace the logic ones with ASSERT()'s and return EIO instead.

---
 fs/btrfs/extent-tree.c | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)
fs/btrfs/extent-tree.c +11 -2
index c95f85c292a4..37aba7d00c8f 100644
@@ -8884,12 +8884,15 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
 				       &wc->flags[level - 1]);
 	if (ret < 0) {
 		btrfs_tree_unlock(next);
+		free_extent_buffer(next);
 		return ret;
 	}
 
 	if (unlikely(wc->refs[level - 1] == 0)) {
 		btrfs_err(root->fs_info, "Missing references.");
-		BUG();
+		btrfs_tree_unlock(next);
+		free_extent_buffer(next);
+		return -EIO;
 	}
 	*lookup_info = 0;
 
@@ -8941,7 +8944,13 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
 	}
 
 	level--;
-	BUG_ON(level != btrfs_header_level(next));
+	ASSERT(level == btrfs_header_level(next));
+	if (level != btrfs_header_level(next)) {
+		btrfs_err("mismatched level");
+		btrfs_tree_unlock(next);
+		free_extent_buffer(next);
+		return -EIO;
+	}
 	path->nodes[level] = next;
 	path->slots[level] = 0;
 	path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
@@ -8956,8 +8965,14 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
 		if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {

fs/btrfs/extent-tree.c static noinline int do_walk_down(struct btrfs_trans_handle *trans, · line 8974

Quoted 10 lines
fs/btrfs/extent-tree.c +6 -1
 			parent = path->nodes[level]->start;
 		} else {
-			BUG_ON(root->root_key.objectid !=
+			ASSERT(root->root_key.objectid ==
 			       btrfs_header_owner(path->nodes[level]));
+			if (root->root_key.objectid !=
+			    btrfs_header_owner(path->nodes[level])) {
+				btrfs_err("mismatched block owner");
+				btrfs_tree_unlock(next);
+				free_extent_buffer(next);

We need a 'return' here, otherwise the next 'if (need_account)' may use @next after free.

Quoted 5 lines
fs/btrfs/extent-tree.c +1 -0
+			}
 			parent = 0;
 		}
 

fs/btrfs/extent-tree.c static noinline int do_walk_down(struct btrfs_trans_handle *trans, · line 8995

Quoted 10 lines
fs/btrfs/extent-tree.c +5 -1
@@ -8972,7 +8987,11 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
 		}
 		ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
 				root->root_key.objectid, level - 1, 0);
-		BUG_ON(ret); /* -ENOMEM */
+		if (ret) {
+			btrfs_tree_unlock(next);
+			free_extent_buffer(next);
+			return ret;
+		}
 	}

Can we put a label of "out" here since most of the above cleanups use the same way to bail out?

Thanks,

-liubo

Quoted 9 lines
fs/btrfs/extent-tree.c +0 -0
 	btrfs_tree_unlock(next);
 	free_extent_buffer(next);
Signature · 6 lines
2.7.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
author maintainer
#2 lore raw

PATCH v2 1/1 Btrfs: don't BUG() during drop snapshot

In reply to David Sterba

From: Josef Bacik <[email protected]>

Really there's lots of things that can go wrong here, kill all the BUG_ON()'s and replace the logic ones with ASSERT()'s and return EIO instead.

[ switched to btrfs_err, errors go to common label ]

---

- added missing return to one branch
- merge common exit code to a label, minor reordering

 fs/btrfs/extent-tree.c | 38 +++++++++++++++++++++++++++-----------
 1 file changed, 27 insertions(+), 11 deletions(-)
fs/btrfs/extent-tree.c +27 -11
index b6e8b58f05e4..7a0b09fb18e2 100644
@@ -8922,15 +8922,13 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
 	ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
 				       &wc->refs[level - 1],
 				       &wc->flags[level - 1]);
-	if (ret < 0) {
-		btrfs_tree_unlock(next);
-		free_extent_buffer(next);
-		return ret;
-	}
+	if (ret < 0)
+		goto out_unlock;
 
 	if (unlikely(wc->refs[level - 1] == 0)) {
 		btrfs_err(root->fs_info, "Missing references.");
-		BUG();
+		ret = -EIO;
+		goto out_unlock;
 	}
 	*lookup_info = 0;
 
@@ -8982,7 +8980,12 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
 	}
 
 	level--;
-	BUG_ON(level != btrfs_header_level(next));
+	ASSERT(level == btrfs_header_level(next));
+	if (level != btrfs_header_level(next)) {
+		btrfs_err(root->fs_info, "mismatched level");
+		ret = -EIO;
+		goto out_unlock;
+	}
 	path->nodes[level] = next;
 	path->slots[level] = 0;
 	path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
@@ -8997,8 +9000,15 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
 		if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
 			parent = path->nodes[level]->start;
 		} else {
-			BUG_ON(root->root_key.objectid !=
+			ASSERT(root->root_key.objectid ==
 			       btrfs_header_owner(path->nodes[level]));
+			if (root->root_key.objectid !=
+			    btrfs_header_owner(path->nodes[level])) {
+				btrfs_err(root->fs_info,
+						"mismatched block owner");
+				ret = -EIO;
+				goto out_unlock;
+			}
 			parent = 0;
 		}
 
@@ -9013,12 +9023,18 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
 		}
 		ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
 				root->root_key.objectid, level - 1, 0);
-		BUG_ON(ret); /* -ENOMEM */
+		if (ret)
+			goto out_unlock;
 	}
+
+	*lookup_info = 1;
+	ret = 1;
+
+out_unlock:
 	btrfs_tree_unlock(next);
 	free_extent_buffer(next);
-	*lookup_info = 1;
-	return 1;
+
+	return ret;
 }
 
 /*
Signature · 1 line
2.7.1
Reviewed-by on v2 1/1
#3 lore raw
Quoted 84 lines · David Sterba

From: Josef Bacik <[email protected]>

Really there's lots of things that can go wrong here, kill all the BUG_ON()'s and replace the logic ones with ASSERT()'s and return EIO instead.

[ switched to btrfs_err, errors go to common label ]

---

- added missing return to one branch
- merge common exit code to a label, minor reordering

 fs/btrfs/extent-tree.c | 38 +++++++++++++++++++++++++++-----------
 1 file changed, 27 insertions(+), 11 deletions(-)
fs/btrfs/extent-tree.c +24 -9
index b6e8b58f05e4..7a0b09fb18e2 100644
@@ -8922,15 +8922,13 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
 	ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
 				       &wc->refs[level - 1],
 				       &wc->flags[level - 1]);
-	if (ret < 0) {
-		btrfs_tree_unlock(next);
-		free_extent_buffer(next);
-		return ret;
-	}
+	if (ret < 0)
+		goto out_unlock;
 
 	if (unlikely(wc->refs[level - 1] == 0)) {
 		btrfs_err(root->fs_info, "Missing references.");
-		BUG();
+		ret = -EIO;
+		goto out_unlock;
 	}
 	*lookup_info = 0;
 
@@ -8982,7 +8980,12 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
 	}
 
 	level--;
-	BUG_ON(level != btrfs_header_level(next));
+	ASSERT(level == btrfs_header_level(next));
+	if (level != btrfs_header_level(next)) {
+		btrfs_err(root->fs_info, "mismatched level");
+		ret = -EIO;
+		goto out_unlock;
+	}
 	path->nodes[level] = next;
 	path->slots[level] = 0;
 	path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
@@ -8997,8 +9000,15 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
 		if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
 			parent = path->nodes[level]->start;
 		} else {
-			BUG_ON(root->root_key.objectid !=
+			ASSERT(root->root_key.objectid ==
 			       btrfs_header_owner(path->nodes[level]));
+			if (root->root_key.objectid !=
+			    btrfs_header_owner(path->nodes[level])) {
+				btrfs_err(root->fs_info,
+						"mismatched block owner");
+				ret = -EIO;
+				goto out_unlock;
+			}
 			parent = 0;
 		}
 
@@ -9013,12 +9023,18 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
 		}
 		ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
 				root->root_key.objectid, level - 1, 0);
-		BUG_ON(ret); /* -ENOMEM */
+		if (ret)
+			goto out_unlock;
 	}
+
+	*lookup_info = 1;
+	ret = 1;
+

fs/btrfs/extent-tree.c static noinline int do_walk_down(struct btrfs_trans_handle *trans, · line 9040

Quoted 10 lines
fs/btrfs/extent-tree.c +3 -2
+out_unlock:
 	btrfs_tree_unlock(next);
 	free_extent_buffer(next);
-	*lookup_info = 1;
-	return 1;
+
+	return ret;
 }
 
 /*

Thanks,

-liubo