Thread (9 messages) 9 messages, 4 authors, 2021-09-07

Re: [PATCH v2] btrfs: replace BUG_ON() in btrfs_csum_one_bio() with proper error handling

From: Qu Wenruo <hidden>
Date: 2021-08-17 08:10:52


On 2021/8/17 下午3:55, Nikolay Borisov wrote:

On 17.08.21 г. 2:55, Qu Wenruo wrote:
quoted
There is a BUG_ON() in btrfs_csum_one_bio() to catch code logic error.

It has indeed caught several bugs during subpage development.

But the BUG_ON() itself will bring down the whole system which is
sometimes overkilled.

Replace it with a WARN() and exit gracefully, so that it won't crash the
whole system while we can still catch the code logic error.

Signed-off-by: Qu Wenruo <redacted>
---
Changelog:
v2:
- Re-send as an independent patch
- Add WARN() to catch the code logic error
---
  fs/btrfs/file-item.c | 13 ++++++++++++-
  1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
index 2673c6ba7a4e..7f58d80a480f 100644
--- a/fs/btrfs/file-item.c
+++ b/fs/btrfs/file-item.c
@@ -665,7 +665,18 @@ blk_status_t btrfs_csum_one_bio(struct btrfs_inode *inode, struct bio *bio,

  		if (!ordered) {
  			ordered = btrfs_lookup_ordered_extent(inode, offset);
-			BUG_ON(!ordered); /* Logic error */
+			/*
+			 * The bio range is not covered by any ordered extent,
+			 * must be a code logic error.
+			 */
+			if (unlikely(!ordered)) {
+				WARN(1, KERN_WARNING
+		"no ordered extent for root %llu ino %llu offset %llu\n",
+				     inode->root->root_key.objectid,
+				     btrfs_ino(inode), offset);
+				kvfree(sums);
+				return BLK_STS_IOERR;
+			}
nit: How about :

if (WARN_ON(!ordered)  {
I still remember that if (WARN_ON()) usage is not recommended by David.

Is that still the case?
btrfs_err(foo)
}

That way you get the unlikely(!ordered) 'for free' and the code is
somewhat cleaner IMO.

While at it I also have to say the structure of the inner loop is rather
iffy, because it's really if/else with an implicit 'else'. How about
converting it to https://paste.ubuntu.com/p/kyWsRrkzWq/
I have no obvious preference between the existing one and the new one.

But since you mentioned it, I would prefer a third way, exactly the code
of the existing if () branch into a function, like
switch_ordered_extent(), and add a comment before the if () line.

To me, that would look easier to read.

Thanks,
Qu
quoted
  		}

  		nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info,
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help