Skip to contentSkip to conversation

btrfs: don't BUG_ON() in btrfs_drop_extents()

PATCH Superseded Johannes Thumshirn 3 participants 3 messages Nov 28, 2024 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: don't BUG_ON() in btrfs_drop_extents()

From: Johannes Thumshirn <[email protected]>

btrfs_drop_extents() calls BUG_ON() in case the counter of to be deleted extents is greater than 0 in two code paths. But both of these code paths can handle errors, so there's no need to crash the kernel, but gracefully bail out.

For developer builds with CONFIG_BTRFS_ASSERT turned on, ASSERT() that this conditon is never met.

---
 fs/btrfs/file.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
fs/btrfs/file.c +10 -2
index fbb753300071..33f0de10df5b 100644
@@ -245,7 +245,11 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
 next_slot:
 		leaf = path->nodes[0];
 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
-			BUG_ON(del_nr > 0);
+			ASSERT(del_nr == 0);
+			if (del_nr > 0) {
+				ret = -EINVAL;
+				break;
+			}
 			ret = btrfs_next_leaf(root, path);
 			if (ret < 0)
 				break;
@@ -321,7 +325,11 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
 		 *  | -------- extent -------- |
 		 */
 		if (args->start > key.offset && args->end < extent_end) {
-			BUG_ON(del_nr > 0);
+			ASSERT(del_nr == 0);
+			if (del_nr > 0) {
+				ret = -EINVAL;
+				break;
+			}
 			if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
 				ret = -EOPNOTSUPP;
 				break;
Signature · 1 line
2.47.0
#1 lore raw
Quoted 34 lines · Johannes Thumshirn

From: Johannes Thumshirn <[email protected]>

btrfs_drop_extents() calls BUG_ON() in case the counter of to be deleted extents is greater than 0 in two code paths. But both of these code paths can handle errors, so there's no need to crash the kernel, but gracefully bail out.

For developer builds with CONFIG_BTRFS_ASSERT turned on, ASSERT() that this conditon is never met.

---
 fs/btrfs/file.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
fs/btrfs/file.c +5 -1
index fbb753300071..33f0de10df5b 100644
@@ -245,7 +245,11 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
 next_slot:
                leaf = path->nodes[0];
                if (path->slots[0] >= btrfs_header_nritems(leaf)) {
-                       BUG_ON(del_nr > 0);
+                       ASSERT(del_nr == 0);
+                       if (del_nr > 0) {
+                               ret = -EINVAL;
+                               break;
+                       }
                        ret = btrfs_next_leaf(root, path);
                        if (ret < 0)
                                break;

fs/btrfs/file.c int btrfs_drop_extents(struct btrfs_trans_handle *trans, · line 332

Quoted 9 lines
fs/btrfs/file.c +5 -1
@@ -321,7 +325,11 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
                 *  | -------- extent -------- |
                 */
                if (args->start > key.offset && args->end < extent_end) {
-                       BUG_ON(del_nr > 0);
+                       ASSERT(del_nr == 0);
+                       if (del_nr > 0) {
+                               ret = -EINVAL;
+                               break;
+                       }

Why only these 2 BUG_ON()'s?

There's another BUG_ON(del_nr > 0) below.

There's also a similar one further below:

BUG_ON(del_slot + del_nr != path->slots[0]);

Also, I'd rather have a WARN_ON() in the if statements, so that in case assertions are disabled (and they are disabled on some distros by default),
we get better chances of the issue getting noticed and reported by users.

Thanks.

Quoted 7 lines
fs/btrfs/file.c +0 -1
                        if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
                                ret = -EOPNOTSUPP;
                                break;
--

2.47.0

#2 lore raw
Quoted 47 lines · Filipe Manana
Quoted 34 lines · Johannes Thumshirn

From: Johannes Thumshirn <[email protected]>

btrfs_drop_extents() calls BUG_ON() in case the counter of to be deleted extents is greater than 0 in two code paths. But both of these code paths can handle errors, so there's no need to crash the kernel, but gracefully bail out.

For developer builds with CONFIG_BTRFS_ASSERT turned on, ASSERT() that this conditon is never met.

---
  fs/btrfs/file.c | 12 ++++++++++--
  1 file changed, 10 insertions(+), 2 deletions(-)
fs/btrfs/file.c +5 -1
index fbb753300071..33f0de10df5b 100644
@@ -245,7 +245,11 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
  next_slot:
                 leaf = path->nodes[0];
                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
-                       BUG_ON(del_nr > 0);
+                       ASSERT(del_nr == 0);
+                       if (del_nr > 0) {
+                               ret = -EINVAL;
+                               break;
+                       }
                         ret = btrfs_next_leaf(root, path);
                         if (ret < 0)
                                 break;

fs/btrfs/file.c int btrfs_drop_extents(struct btrfs_trans_handle *trans, · line 332

Quoted 9 lines
fs/btrfs/file.c +5 -1
@@ -321,7 +325,11 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
                  *  | -------- extent -------- |
                  */
                 if (args->start > key.offset && args->end < extent_end) {
-                       BUG_ON(del_nr > 0);
+                       ASSERT(del_nr == 0);
+                       if (del_nr > 0) {
+                               ret = -EINVAL;
+                               break;
+                       }

Why only these 2 BUG_ON()'s?

Quoted 5 lines

There's another BUG_ON(del_nr > 0) below.

There's also a similar one further below:

BUG_ON(del_slot + del_nr != path->slots[0]);

Plain oversight. I just came across this becasuse I needed an example for a similar "punch hole" problematic in RST.

Quoted 4 lines

Also, I'd rather have a WARN_ON() in the if statements, so that in case assertions are disabled (and they are disabled on some distros by default),
we get better chances of the issue getting noticed and reported by users.

Sure I'll change it and also include the other BUG_ON()s.

Quoted 11 lines

Thanks.

Quoted 7 lines
fs/btrfs/file.c +0 -1
                         if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
                                 ret = -EOPNOTSUPP;
                                 break;
--

2.47.0