btrfs-progs/kernel-shared
Qu Wenruo 08bb354a1c btrfs-progs: properly handle write error when writing back tree blocks
[BUG]
If we emulate a write error during commit transaction, by setting the
block device read-only, then we can easily have the following crash
using "btrfs check --clear-space-cache v2":

  Opening filesystem to check...
  Checking filesystem on /dev/test/scratch1
  UUID: 5945915b-37f1-4bfa-9f64-684b318b8f73
  Clear free space cache v2
  Error writing to device 1
  kernel-shared/transaction.c:156: __commit_transaction: BUG_ON `ret` triggered, value 1
  ./btrfs(+0x570c9)[0x562ec894f0c9]
  ./btrfs(+0x57167)[0x562ec894f167]
  ./btrfs(__commit_transaction+0x13b)[0x562ec894f7f2]
  ./btrfs(btrfs_commit_transaction+0x214)[0x562ec894fa64]
  ./btrfs(btrfs_clear_free_space_tree+0x177)[0x562ec8941ae6]
  ./btrfs(+0xc8958)[0x562ec89c0958]
  ./btrfs(+0xc9d53)[0x562ec89c1d53]
  ./btrfs(+0x17ec7)[0x562ec890fec7]
  ./btrfs(main+0x12f)[0x562ec8910908]
  /usr/lib/libc.so.6(+0x232d0)[0x7ff917ee82d0]
  /usr/lib/libc.so.6(__libc_start_main+0x8a)[0x7ff917ee838a]
  ./btrfs(_start+0x25)[0x562ec890fdc5]
  Aborted (core dumped)

[CAUSE]
The call trace has shown it's a BUG_ON(), and it's from
__commit_transaction(), which is writing tree blocks back.

[FIX]
The fix is pretty simple, just return error.

In fact we even have an error value check in btrfs_commit_transaction()
just after __commit_transaction() call (although not catching the return
value from it).

And since we're here, also call btrfs_abort_transaction() to prevent
newer transactions from being started.

Now we won't have a full crash:

  Opening filesystem to check...
  Checking filesystem on /dev/test/scratch1
  UUID: 5945915b-37f1-4bfa-9f64-684b318b8f73
  Clear free space cache v2
  Error writing to device 1
  ERROR: failed to write bytenr 30425088 length 16384: Operation not permitted
  ERROR: failed to write tree block 30425088: Operation not permitted
  ERROR: failed to clear free space cache v2: -1
  extent buffer leak: start 30720000 len 16384

Reported-by: Christoph Anton Mitterer <calestyo@scientia.org>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-11 09:08:08 +02:00
..
backref.c btrfs-progs: remove the _nr from the item helpers 2022-03-09 15:13:13 +01:00
backref.h btrfs-progs: move extent_io.c to kernel-shared/ 2020-08-31 17:01:04 +02:00
ctree.c btrfs-progs: btrfstune: add the ability to convert to block group tree feature 2022-09-12 18:25:32 +02:00
ctree.h btrfs-progs: kernel-lib: remove radix-tree 2022-10-11 09:08:07 +02:00
delayed-ref.c btrfs-progs: don't include btrfs-list.h unless necessary 2021-10-08 20:47:03 +02:00
delayed-ref.h btrfs-progs: unify GPL header comments 2021-09-07 13:58:44 +02:00
dir-item.c btrfs-progs: remove the _nr from the item helpers 2022-03-09 15:13:13 +01:00
disk-io.c btrfs-progs: kernel-lib: remove radix-tree 2022-10-11 09:08:07 +02:00
disk-io.h btrfs-progs: btrfstune: add the ability to convert to block group tree feature 2022-09-12 18:25:32 +02:00
extent_io.c btrfs-progs: properly handle write error when writing back tree blocks 2022-10-11 09:08:08 +02:00
extent_io.h btrfs-progs: remove unused function extent_io_tree_init_cache_max() 2022-10-11 09:08:08 +02:00
extent-tree.c btrfs-progs: kernel-lib: remove radix-tree 2022-10-11 09:08:07 +02:00
file-item.c btrfs-progs: kernel-lib: remove radix-tree 2022-10-11 09:08:07 +02:00
file.c btrfs-progs: use read_data_from_disk() to replace read_extent_from_disk() and replace read_extent_data() 2022-04-25 19:08:30 +02:00
free-space-cache.c btrfs-progs: use read_data_from_disk() to replace read_extent_from_disk() and replace read_extent_data() 2022-04-25 19:08:30 +02:00
free-space-cache.h btrfs-progs: check: batch v1 space cache inodes when clearing 2021-07-22 16:26:05 +02:00
free-space-tree.c btrfs-progs: fix may be unused warning in load_free_space_extents 2022-10-11 09:06:11 +02:00
free-space-tree.h btrfs-progs: properly initialize block group thresholds 2022-05-20 15:54:20 +02:00
inode-item.c btrfs-progs: remove the _nr from the item helpers 2022-03-09 15:13:13 +01:00
inode.c btrfs-progs: make btrfs_lookup_dir_index in parity with kernel code 2020-08-31 17:09:49 +02:00
print-tree.c btrfs-progs: kernel-lib: remove radix-tree 2022-10-11 09:08:07 +02:00
print-tree.h btrfs-progs: dump-tree: add options to dump checksums 2021-06-19 22:07:49 +02:00
root-tree.c btrfs-progs: remove the _nr from the item helpers 2022-03-09 15:13:13 +01:00
send.h btrfs-progs: receive: add support for fs-verity 2022-10-11 09:08:08 +02:00
transaction.c btrfs-progs: properly handle write error when writing back tree blocks 2022-10-11 09:08:08 +02:00
transaction.h btrfs-progs: move transaction.c to kernel-shared/ 2020-08-31 17:01:06 +02:00
ulist.c btrfs-progs: unify GPL header comments 2021-09-07 13:58:44 +02:00
ulist.h btrfs-progs: unify GPL header comments 2021-09-07 13:58:44 +02:00
uuid-tree.c btrfs-progs: remove the _nr from the item helpers 2022-03-09 15:13:13 +01:00
volumes.c btrfs-progs: rescue: allow fix-device-size to shrink device item 2022-09-12 15:31:21 +02:00
volumes.h btrfs-progs: remove the unused btrfs_fs_info::seeding member 2022-04-29 22:13:22 +02:00
zoned.c btrfs-progs: zoned: add upper and lower zone size boundaries 2022-06-06 15:47:50 +02:00
zoned.h btrfs-progs: zoned: export sb_zone_number() and related constants 2022-04-08 23:17:35 +02:00