Skip to contentSkip to conversation

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

PATCH In review Johannes Thumshirn 3 participants 3 messages Nov 29, 2024 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH v2 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. But all of these code paths can handle errors, so there's no need to crash the kernel. Instead WARN() that the condition has been met and gracefully bail out.

---
Changes to v1:
- Fix spelling error in commit message
- Change ASSERT() to WARN_ON()
- Take care of the other BUG_ON() cases as well

Link to v1:
- https://lore.kernel.org/linux-btrfs/[email protected]

---
 fs/btrfs/file.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
fs/btrfs/file.c +16 -4
index fbb753300071..f70ce6c65d12 100644
@@ -245,7 +245,10 @@ 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);
+			if (WARN_ON(del_nr > 0)) {
+				ret = -EINVAL;
+				break;
+			}
 			ret = btrfs_next_leaf(root, path);
 			if (ret < 0)
 				break;
@@ -321,7 +324,10 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
 		 *  | -------- extent -------- |
 		 */
 		if (args->start > key.offset && args->end < extent_end) {
-			BUG_ON(del_nr > 0);
+			if (WARN_ON(del_nr > 0)) {
+				ret = -EINVAL;
+				break;
+			}
 			if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
 				ret = -EOPNOTSUPP;
 				break;
@@ -409,7 +415,10 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
 		 *  | -------- extent -------- |
 		 */
 		if (args->start > key.offset && args->end >= extent_end) {
-			BUG_ON(del_nr > 0);
+			if (WARN_ON(del_nr > 0)) {
+				ret = -EINVAL;
+				break;
+			}
 			if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
 				ret = -EOPNOTSUPP;
 				break;
@@ -437,7 +446,10 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
 				del_slot = path->slots[0];
 				del_nr = 1;
 			} else {
-				BUG_ON(del_slot + del_nr != path->slots[0]);
+				if (WARN_ON(del_slot + del_nr != path->slots[0])) {
+					ret = -EINVAL;
+					break;
+				}
 				del_nr++;
 			}
 
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 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. But all of these code paths can handle errors, so there's no need to crash the kernel. Instead WARN() that the condition has been met and gracefully bail out.

Looks good, thanks.

Quoted 68 lines

---
Changes to v1:
- Fix spelling error in commit message
- Change ASSERT() to WARN_ON()
- Take care of the other BUG_ON() cases as well

Link to v1:
- https://lore.kernel.org/linux-btrfs/[email protected]

---
 fs/btrfs/file.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
fs/btrfs/file.c +16 -5
index fbb753300071..f70ce6c65d12 100644
@@ -245,7 +245,10 @@ 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);
+                       if (WARN_ON(del_nr > 0)) {
+                               ret = -EINVAL;
+                               break;
+                       }
                        ret = btrfs_next_leaf(root, path);
                        if (ret < 0)
                                break;
@@ -321,7 +324,10 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
                 *  | -------- extent -------- |
                 */
                if (args->start > key.offset && args->end < extent_end) {
-                       BUG_ON(del_nr > 0);
+                       if (WARN_ON(del_nr > 0)) {
+                               ret = -EINVAL;
+                               break;
+                       }
                        if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
                                ret = -EOPNOTSUPP;
                                break;
@@ -409,7 +415,10 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
                 *  | -------- extent -------- |
                 */
                if (args->start > key.offset && args->end >= extent_end) {
-                       BUG_ON(del_nr > 0);
+                       if (WARN_ON(del_nr > 0)) {
+                               ret = -EINVAL;
+                               break;
+                       }
                        if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
                                ret = -EOPNOTSUPP;
                                break;
@@ -437,7 +446,10 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
                                del_slot = path->slots[0];
                                del_nr = 1;
                        } else {
-                               BUG_ON(del_slot + del_nr != path->slots[0]);
+                               if (WARN_ON(del_slot + del_nr != path->slots[0])) {
+                                       ret = -EINVAL;
+                                       break;
+                               }
                                del_nr++;
                        }
 
--

2.43.0

#2 lore raw
In reply to Johannes Thumshirn
Quoted 37 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. But all of these code paths can handle errors, so there's no need to crash the kernel. Instead WARN() that the condition has been met and gracefully bail out.

---
Changes to v1:
- Fix spelling error in commit message
- Change ASSERT() to WARN_ON()
- Take care of the other BUG_ON() cases as well

Link to v1:
- https://lore.kernel.org/linux-btrfs/[email protected]

---
  fs/btrfs/file.c | 20 ++++++++++++++++----
  1 file changed, 16 insertions(+), 4 deletions(-)
fs/btrfs/file.c +4 -1
index fbb753300071..f70ce6c65d12 100644
@@ -245,7 +245,10 @@ 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);
+			if (WARN_ON(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 328

Quoted 6 lines
fs/btrfs/file.c +2 -1
@@ -321,7 +324,10 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
  		 *  | -------- extent -------- |
  		 */
  		if (args->start > key.offset && args->end < extent_end) {
-			BUG_ON(del_nr > 0);
+			if (WARN_ON(del_nr > 0)) {
+				ret = -EINVAL;

Do we also want to dump the leaf and output a more readable error message?

Just like what we did inside lookup_inlin_extent_backref()/update_inline_extent_backref()/insert_inline_extent_backref()/__btrfs_free_extent()/...?

Thanks,
Qu

Quoted 29 lines
fs/btrfs/file.c +10 -2
+				break;
+			}
  			if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  				ret = -EOPNOTSUPP;
  				break;
@@ -409,7 +415,10 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
  		 *  | -------- extent -------- |
  		 */
  		if (args->start > key.offset && args->end >= extent_end) {
-			BUG_ON(del_nr > 0);
+			if (WARN_ON(del_nr > 0)) {
+				ret = -EINVAL;
+				break;
+			}
  			if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  				ret = -EOPNOTSUPP;
  				break;
@@ -437,7 +446,10 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
  				del_slot = path->slots[0];
  				del_nr = 1;
  			} else {
-				BUG_ON(del_slot + del_nr != path->slots[0]);
+				if (WARN_ON(del_slot + del_nr != path->slots[0])) {
+					ret = -EINVAL;
+					break;
+				}
  				del_nr++;
  			}