Skip to content

Commit b024419

Browse files
Miao XieChris Mason
authored andcommitted
Btrfs: don't wait for the completion of all the ordered extents
It is very likely that there are lots of ordered extents in the filesytem, if we wait for the completion of all of them when we want to reclaim some space for the metadata space reservation, we would be blocked for a long time. The performance would drop down suddenly for a long time. 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 9f3a074 commit b024419

8 files changed

Lines changed: 31 additions & 18 deletions

File tree

‎fs/btrfs/dev-replace.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ int btrfs_dev_replace_start(struct btrfs_root *root,
391391
args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
392392
btrfs_dev_replace_unlock(dev_replace);
393393

394-
btrfs_wait_all_ordered_extents(root->fs_info);
394+
btrfs_wait_ordered_roots(root->fs_info, -1);
395395

396396
/* force writing the updated state information to disk */
397397
trans = btrfs_start_transaction(root, 0);
@@ -466,7 +466,7 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
466466
mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
467467
return ret;
468468
}
469-
btrfs_wait_all_ordered_extents(root->fs_info);
469+
btrfs_wait_ordered_roots(root->fs_info, -1);
470470

471471
trans = btrfs_start_transaction(root, 0);
472472
if (IS_ERR(trans)) {

‎fs/btrfs/extent-tree.c‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4018,7 +4018,7 @@ static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
40184018
*/
40194019
btrfs_start_all_delalloc_inodes(root->fs_info, 0);
40204020
if (!current->journal_info)
4021-
btrfs_wait_all_ordered_extents(root->fs_info);
4021+
btrfs_wait_ordered_roots(root->fs_info, -1);
40224022
}
40234023
}
40244024

@@ -4050,11 +4050,12 @@ static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
40504050
long time_left;
40514051
unsigned long nr_pages;
40524052
int loops;
4053+
int items;
40534054
enum btrfs_reserve_flush_enum flush;
40544055

40554056
/* Calc the number of the pages we need flush for space reservation */
4056-
to_reclaim = calc_reclaim_items_nr(root, to_reclaim);
4057-
to_reclaim *= EXTENT_SIZE_PER_ITEM;
4057+
items = calc_reclaim_items_nr(root, to_reclaim);
4058+
to_reclaim = items * EXTENT_SIZE_PER_ITEM;
40584059

40594060
trans = (struct btrfs_trans_handle *)current->journal_info;
40604061
block_rsv = &root->fs_info->delalloc_block_rsv;
@@ -4066,7 +4067,7 @@ static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
40664067
if (trans)
40674068
return;
40684069
if (wait_ordered)
4069-
btrfs_wait_all_ordered_extents(root->fs_info);
4070+
btrfs_wait_ordered_roots(root->fs_info, items);
40704071
return;
40714072
}
40724073

@@ -4105,7 +4106,7 @@ static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
41054106

41064107
loops++;
41074108
if (wait_ordered && !trans) {
4108-
btrfs_wait_all_ordered_extents(root->fs_info);
4109+
btrfs_wait_ordered_roots(root->fs_info, items);
41094110
} else {
41104111
time_left = schedule_timeout_killable(1);
41114112
if (time_left)

‎fs/btrfs/ioctl.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ static int create_snapshot(struct btrfs_root *root, struct inode *dir,
572572
if (ret)
573573
return ret;
574574

575-
btrfs_wait_ordered_extents(root);
575+
btrfs_wait_ordered_extents(root, -1);
576576

577577
pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
578578
if (!pending_snapshot)

‎fs/btrfs/ordered-data.c‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -565,18 +565,19 @@ static void btrfs_run_ordered_extent_work(struct btrfs_work *work)
565565
* wait for all the ordered extents in a root. This is done when balancing
566566
* space between drives.
567567
*/
568-
void btrfs_wait_ordered_extents(struct btrfs_root *root)
568+
int btrfs_wait_ordered_extents(struct btrfs_root *root, int nr)
569569
{
570570
struct list_head splice, works;
571571
struct btrfs_ordered_extent *ordered, *next;
572+
int count = 0;
572573

573574
INIT_LIST_HEAD(&splice);
574575
INIT_LIST_HEAD(&works);
575576

576577
mutex_lock(&root->fs_info->ordered_operations_mutex);
577578
spin_lock(&root->ordered_extent_lock);
578579
list_splice_init(&root->ordered_extents, &splice);
579-
while (!list_empty(&splice)) {
580+
while (!list_empty(&splice) && nr) {
580581
ordered = list_first_entry(&splice, struct btrfs_ordered_extent,
581582
root_extent_list);
582583
list_move_tail(&ordered->root_extent_list,
@@ -591,7 +592,11 @@ void btrfs_wait_ordered_extents(struct btrfs_root *root)
591592

592593
cond_resched();
593594
spin_lock(&root->ordered_extent_lock);
595+
if (nr != -1)
596+
nr--;
597+
count++;
594598
}
599+
list_splice_tail(&splice, &root->ordered_extents);
595600
spin_unlock(&root->ordered_extent_lock);
596601

597602
list_for_each_entry_safe(ordered, next, &works, work_list) {
@@ -601,18 +606,21 @@ void btrfs_wait_ordered_extents(struct btrfs_root *root)
601606
cond_resched();
602607
}
603608
mutex_unlock(&root->fs_info->ordered_operations_mutex);
609+
610+
return count;
604611
}
605612

606-
void btrfs_wait_all_ordered_extents(struct btrfs_fs_info *fs_info)
613+
void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, int nr)
607614
{
608615
struct btrfs_root *root;
609616
struct list_head splice;
617+
int done;
610618

611619
INIT_LIST_HEAD(&splice);
612620

613621
spin_lock(&fs_info->ordered_root_lock);
614622
list_splice_init(&fs_info->ordered_roots, &splice);
615-
while (!list_empty(&splice)) {
623+
while (!list_empty(&splice) && nr) {
616624
root = list_first_entry(&splice, struct btrfs_root,
617625
ordered_root);
618626
root = btrfs_grab_fs_root(root);
@@ -621,10 +629,14 @@ void btrfs_wait_all_ordered_extents(struct btrfs_fs_info *fs_info)
621629
&fs_info->ordered_roots);
622630
spin_unlock(&fs_info->ordered_root_lock);
623631

624-
btrfs_wait_ordered_extents(root);
632+
done = btrfs_wait_ordered_extents(root, nr);
625633
btrfs_put_fs_root(root);
626634

627635
spin_lock(&fs_info->ordered_root_lock);
636+
if (nr != -1) {
637+
nr -= done;
638+
WARN_ON(nr < 0);
639+
}
628640
}
629641
spin_unlock(&fs_info->ordered_root_lock);
630642
}

‎fs/btrfs/ordered-data.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ int btrfs_run_ordered_operations(struct btrfs_trans_handle *trans,
195195
void btrfs_add_ordered_operation(struct btrfs_trans_handle *trans,
196196
struct btrfs_root *root,
197197
struct inode *inode);
198-
void btrfs_wait_ordered_extents(struct btrfs_root *root);
199-
void btrfs_wait_all_ordered_extents(struct btrfs_fs_info *fs_info);
198+
int btrfs_wait_ordered_extents(struct btrfs_root *root, int nr);
199+
void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, int nr);
200200
void btrfs_get_logged_extents(struct btrfs_root *log, struct inode *inode);
201201
void btrfs_wait_logged_extents(struct btrfs_root *log, u64 transid);
202202
void btrfs_free_logged_extents(struct btrfs_root *log, u64 transid);

‎fs/btrfs/relocation.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4227,7 +4227,7 @@ int btrfs_relocate_block_group(struct btrfs_root *extent_root, u64 group_start)
42274227
err = ret;
42284228
goto out;
42294229
}
4230-
btrfs_wait_all_ordered_extents(fs_info);
4230+
btrfs_wait_ordered_roots(fs_info, -1);
42314231

42324232
while (1) {
42334233
mutex_lock(&fs_info->cleaner_mutex);

‎fs/btrfs/super.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ int btrfs_sync_fs(struct super_block *sb, int wait)
920920
return 0;
921921
}
922922

923-
btrfs_wait_all_ordered_extents(fs_info);
923+
btrfs_wait_ordered_roots(fs_info, -1);
924924

925925
trans = btrfs_attach_transaction_barrier(root);
926926
if (IS_ERR(trans)) {

‎fs/btrfs/transaction.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1636,7 +1636,7 @@ static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
16361636
static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
16371637
{
16381638
if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
1639-
btrfs_wait_all_ordered_extents(fs_info);
1639+
btrfs_wait_ordered_roots(fs_info, -1);
16401640
}
16411641

16421642
int btrfs_commit_transaction(struct btrfs_trans_handle *trans,

0 commit comments

Comments
 (0)