btrfs: relocation: review the call sites which can be interrupted by signal
authorQu Wenruo <wqu@suse.com>
Mon, 13 Jul 2020 01:03:21 +0000 (09:03 +0800)
committerDavid Sterba <dsterba@suse.com>
Mon, 27 Jul 2020 10:55:45 +0000 (12:55 +0200)
Since most metadata reservation calls can return -EINTR when get
interrupted by fatal signal, we need to review the all the metadata
reservation call sites.

In relocation code, the metadata reservation happens in the following
sites:

- btrfs_block_rsv_refill() in merge_reloc_root()
  merge_reloc_root() is a pretty critical section, we don't want to be
  interrupted by signal, so change the flush status to
  BTRFS_RESERVE_FLUSH_LIMIT, so it won't get interrupted by signal.
  Since such change can be ENPSPC-prone, also shrink the amount of
  metadata to reserve least amount avoid deadly ENOSPC there.

- btrfs_block_rsv_refill() in reserve_metadata_space()
  It calls with BTRFS_RESERVE_FLUSH_LIMIT, which won't get interrupted
  by signal.

- btrfs_block_rsv_refill() in prepare_to_relocate()

- btrfs_block_rsv_add() in prepare_to_relocate()

- btrfs_block_rsv_refill() in relocate_block_group()

- btrfs_delalloc_reserve_metadata() in relocate_file_extent_cluster()

- btrfs_start_transaction() in relocate_block_group()

- btrfs_start_transaction() in create_reloc_inode()
  Can be interrupted by fatal signal and we can handle it easily.
  For these call sites, just catch the -EINTR value in btrfs_balance()
  and count them as canceled.

CC: stable@vger.kernel.org # 5.4+
Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/relocation.c
fs/btrfs/volumes.c

index 2b869fb..4ba1ab9 100644 (file)
@@ -1686,12 +1686,20 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
                btrfs_unlock_up_safe(path, 0);
        }
 
-       min_reserved = fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
+       /*
+        * In merge_reloc_root(), we modify the upper level pointer to swap the
+        * tree blocks between reloc tree and subvolume tree.  Thus for tree
+        * block COW, we COW at most from level 1 to root level for each tree.
+        *
+        * Thus the needed metadata size is at most root_level * nodesize,
+        * and * 2 since we have two trees to COW.
+        */
+       min_reserved = fs_info->nodesize * btrfs_root_level(root_item) * 2;
        memset(&next_key, 0, sizeof(next_key));
 
        while (1) {
                ret = btrfs_block_rsv_refill(root, rc->block_rsv, min_reserved,
-                                            BTRFS_RESERVE_FLUSH_ALL);
+                                            BTRFS_RESERVE_FLUSH_LIMIT);
                if (ret) {
                        err = ret;
                        goto out;
index c1a4dab..537ccf6 100644 (file)
@@ -4135,7 +4135,22 @@ int btrfs_balance(struct btrfs_fs_info *fs_info,
        mutex_lock(&fs_info->balance_mutex);
        if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req))
                btrfs_info(fs_info, "balance: paused");
-       else if (ret == -ECANCELED && atomic_read(&fs_info->balance_cancel_req))
+       /*
+        * Balance can be canceled by:
+        *
+        * - Regular cancel request
+        *   Then ret == -ECANCELED and balance_cancel_req > 0
+        *
+        * - Fatal signal to "btrfs" process
+        *   Either the signal caught by wait_reserve_ticket() and callers
+        *   got -EINTR, or caught by btrfs_should_cancel_balance() and
+        *   got -ECANCELED.
+        *   Either way, in this case balance_cancel_req = 0, and
+        *   ret == -EINTR or ret == -ECANCELED.
+        *
+        * So here we only check the return value to catch canceled balance.
+        */
+       else if (ret == -ECANCELED || ret == -EINTR)
                btrfs_info(fs_info, "balance: canceled");
        else
                btrfs_info(fs_info, "balance: ended with status: %d", ret);