Skip to content

Commit 20dd2cb

Browse files
Miao XieChris Mason
authored andcommitted
Btrfs: fix BUG_ON() casued by the reserved space migration
When we did space balance and snapshot creation at the same time, we might meet the following oops: kernel BUG at fs/btrfs/inode.c:3038! [SNIP] Call Trace: [<ffffffffa0411ec7>] btrfs_orphan_cleanup+0x293/0x407 [btrfs] [<ffffffffa042dc45>] btrfs_mksubvol.isra.28+0x259/0x373 [btrfs] [<ffffffffa042de85>] btrfs_ioctl_snap_create_transid+0x126/0x156 [btrfs] [<ffffffffa042dff1>] btrfs_ioctl_snap_create_v2+0xd0/0x121 [btrfs] [<ffffffffa0430b2c>] btrfs_ioctl+0x414/0x1854 [btrfs] [<ffffffff813b60b7>] ? __do_page_fault+0x305/0x379 [<ffffffff811215a9>] vfs_ioctl+0x1d/0x39 [<ffffffff81121d7c>] do_vfs_ioctl+0x32d/0x3e2 [<ffffffff81057fe7>] ? finish_task_switch+0x80/0xb8 [<ffffffff81121e88>] SyS_ioctl+0x57/0x83 [<ffffffff813b39ff>] ? do_device_not_available+0x12/0x14 [<ffffffff813b99c2>] system_call_fastpath+0x16/0x1b [SNIP] RIP [<ffffffffa040da40>] btrfs_orphan_add+0xc3/0x126 [btrfs] The reason of the problem is that the relocation root creation stole the reserved space, which was reserved for orphan item deletion. There are several ways to fix this problem, one is to increasing the reserved space size of the space balace, and then we can use that space to create the relocation tree for each fs/file trees. But it is hard to calculate the suitable size because we doesn't know how many fs/file trees we need relocate. We fixed this problem by reserving the space for relocation root creation actively since the space it need is very small (one tree block, used for root node copy), then we use that reserved space to create the relocation tree. If we don't reserve space for relocation tree creation, we will use the reserved space of the balance. Signed-off-by: Miao Xie <miaox@cn.fujitsu.com> Signed-off-by: Josef Bacik <jbacik@fusionio.com> Signed-off-by: Chris Mason <chris.mason@fusionio.com>
1 parent 0a4e558 commit 20dd2cb

3 files changed

Lines changed: 28 additions & 3 deletions

File tree

‎fs/btrfs/relocation.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,7 @@ int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
13831383
{
13841384
struct btrfs_root *reloc_root;
13851385
struct reloc_control *rc = root->fs_info->reloc_ctl;
1386+
struct btrfs_block_rsv *rsv;
13861387
int clear_rsv = 0;
13871388
int ret;
13881389

@@ -1396,13 +1397,14 @@ int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
13961397
root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
13971398
return 0;
13981399

1399-
if (!trans->block_rsv) {
1400+
if (!trans->reloc_reserved) {
1401+
rsv = trans->block_rsv;
14001402
trans->block_rsv = rc->block_rsv;
14011403
clear_rsv = 1;
14021404
}
14031405
reloc_root = create_reloc_root(trans, root, root->root_key.objectid);
14041406
if (clear_rsv)
1405-
trans->block_rsv = NULL;
1407+
trans->block_rsv = rsv;
14061408

14071409
ret = __add_reloc_root(reloc_root);
14081410
BUG_ON(ret < 0);

‎fs/btrfs/transaction.c‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,15 +353,27 @@ static int may_wait_transaction(struct btrfs_root *root, int type)
353353
return 0;
354354
}
355355

356+
static inline bool need_reserve_reloc_root(struct btrfs_root *root)
357+
{
358+
if (!root->fs_info->reloc_ctl ||
359+
!root->ref_cows ||
360+
root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
361+
root->reloc_root)
362+
return false;
363+
364+
return true;
365+
}
366+
356367
static struct btrfs_trans_handle *
357368
start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type,
358369
enum btrfs_reserve_flush_enum flush)
359370
{
360371
struct btrfs_trans_handle *h;
361372
struct btrfs_transaction *cur_trans;
362373
u64 num_bytes = 0;
363-
int ret;
364374
u64 qgroup_reserved = 0;
375+
bool reloc_reserved = false;
376+
int ret;
365377

366378
if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
367379
return ERR_PTR(-EROFS);
@@ -390,6 +402,14 @@ start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type,
390402
}
391403

392404
num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
405+
/*
406+
* Do the reservation for the relocation root creation
407+
*/
408+
if (unlikely(need_reserve_reloc_root(root))) {
409+
num_bytes += root->nodesize;
410+
reloc_reserved = true;
411+
}
412+
393413
ret = btrfs_block_rsv_add(root,
394414
&root->fs_info->trans_block_rsv,
395415
num_bytes, flush);
@@ -451,6 +471,7 @@ start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type,
451471
h->delayed_ref_elem.seq = 0;
452472
h->type = type;
453473
h->allocating_chunk = false;
474+
h->reloc_reserved = false;
454475
INIT_LIST_HEAD(&h->qgroup_ref_list);
455476
INIT_LIST_HEAD(&h->new_bgs);
456477

@@ -466,6 +487,7 @@ start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type,
466487
h->transid, num_bytes, 1);
467488
h->block_rsv = &root->fs_info->trans_block_rsv;
468489
h->bytes_reserved = num_bytes;
490+
h->reloc_reserved = reloc_reserved;
469491
}
470492
h->qgroup_reserved = qgroup_reserved;
471493

‎fs/btrfs/transaction.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ struct btrfs_trans_handle {
9292
short aborted;
9393
short adding_csums;
9494
bool allocating_chunk;
95+
bool reloc_reserved;
9596
unsigned int type;
9697
/*
9798
* this root is only needed to validate that the root passed to

0 commit comments

Comments
 (0)