Skip to content

Commit 0aefda3

Browse files
committed
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux-btrfs
Pull btrfs fixes from Chris Mason: "These are scattered fixes and one performance improvement. The biggest functional change is in how we throttle metadata changes. The new code bumps our average file creation rate up by ~13% in fs_mark, and lowers CPU usage. Stefan bisected out a regression in our allocation code that made balance loop on extents larger than 256MB." * 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux-btrfs: Btrfs: improve the delayed inode throttling Btrfs: fix a mismerge in btrfs_balance() Btrfs: enforce min_bytes parameter during extent allocation Btrfs: allow running defrag in parallel to administrative tasks Btrfs: avoid deadlock on transaction waiting list Btrfs: do not BUG_ON on aborted situation Btrfs: do not BUG_ON in prepare_to_reloc Btrfs: free all recorded tree blocks on error Btrfs: build up error handling for merge_reloc_roots Btrfs: check for NULL pointer in updating reloc roots Btrfs: fix unclosed transaction handler when the async transaction commitment fails Btrfs: fix wrong handle at error path of create_snapshot() when the commit fails Btrfs: use set_nlink if our i_nlink is 0
2 parents 2ef3920 + de3cb94 commit 0aefda3

9 files changed

Lines changed: 221 additions & 130 deletions

File tree

‎fs/btrfs/delayed-inode.c‎

Lines changed: 90 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
#include "disk-io.h"
2323
#include "transaction.h"
2424

25-
#define BTRFS_DELAYED_WRITEBACK 400
26-
#define BTRFS_DELAYED_BACKGROUND 100
25+
#define BTRFS_DELAYED_WRITEBACK 512
26+
#define BTRFS_DELAYED_BACKGROUND 128
27+
#define BTRFS_DELAYED_BATCH 16
2728

2829
static struct kmem_cache *delayed_node_cache;
2930

@@ -494,6 +495,15 @@ static int __btrfs_add_delayed_deletion_item(struct btrfs_delayed_node *node,
494495
BTRFS_DELAYED_DELETION_ITEM);
495496
}
496497

498+
static void finish_one_item(struct btrfs_delayed_root *delayed_root)
499+
{
500+
int seq = atomic_inc_return(&delayed_root->items_seq);
501+
if ((atomic_dec_return(&delayed_root->items) <
502+
BTRFS_DELAYED_BACKGROUND || seq % BTRFS_DELAYED_BATCH == 0) &&
503+
waitqueue_active(&delayed_root->wait))
504+
wake_up(&delayed_root->wait);
505+
}
506+
497507
static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item)
498508
{
499509
struct rb_root *root;
@@ -512,10 +522,8 @@ static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item)
512522

513523
rb_erase(&delayed_item->rb_node, root);
514524
delayed_item->delayed_node->count--;
515-
if (atomic_dec_return(&delayed_root->items) <
516-
BTRFS_DELAYED_BACKGROUND &&
517-
waitqueue_active(&delayed_root->wait))
518-
wake_up(&delayed_root->wait);
525+
526+
finish_one_item(delayed_root);
519527
}
520528

521529
static void btrfs_release_delayed_item(struct btrfs_delayed_item *item)
@@ -1056,10 +1064,7 @@ static void btrfs_release_delayed_inode(struct btrfs_delayed_node *delayed_node)
10561064
delayed_node->count--;
10571065

10581066
delayed_root = delayed_node->root->fs_info->delayed_root;
1059-
if (atomic_dec_return(&delayed_root->items) <
1060-
BTRFS_DELAYED_BACKGROUND &&
1061-
waitqueue_active(&delayed_root->wait))
1062-
wake_up(&delayed_root->wait);
1067+
finish_one_item(delayed_root);
10631068
}
10641069
}
10651070

@@ -1304,35 +1309,44 @@ void btrfs_remove_delayed_node(struct inode *inode)
13041309
btrfs_release_delayed_node(delayed_node);
13051310
}
13061311

1307-
struct btrfs_async_delayed_node {
1308-
struct btrfs_root *root;
1309-
struct btrfs_delayed_node *delayed_node;
1312+
struct btrfs_async_delayed_work {
1313+
struct btrfs_delayed_root *delayed_root;
1314+
int nr;
13101315
struct btrfs_work work;
13111316
};
13121317

1313-
static void btrfs_async_run_delayed_node_done(struct btrfs_work *work)
1318+
static void btrfs_async_run_delayed_root(struct btrfs_work *work)
13141319
{
1315-
struct btrfs_async_delayed_node *async_node;
1320+
struct btrfs_async_delayed_work *async_work;
1321+
struct btrfs_delayed_root *delayed_root;
13161322
struct btrfs_trans_handle *trans;
13171323
struct btrfs_path *path;
13181324
struct btrfs_delayed_node *delayed_node = NULL;
13191325
struct btrfs_root *root;
13201326
struct btrfs_block_rsv *block_rsv;
1321-
int need_requeue = 0;
1327+
int total_done = 0;
13221328

1323-
async_node = container_of(work, struct btrfs_async_delayed_node, work);
1329+
async_work = container_of(work, struct btrfs_async_delayed_work, work);
1330+
delayed_root = async_work->delayed_root;
13241331

13251332
path = btrfs_alloc_path();
13261333
if (!path)
13271334
goto out;
1328-
path->leave_spinning = 1;
13291335

1330-
delayed_node = async_node->delayed_node;
1336+
again:
1337+
if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND / 2)
1338+
goto free_path;
1339+
1340+
delayed_node = btrfs_first_prepared_delayed_node(delayed_root);
1341+
if (!delayed_node)
1342+
goto free_path;
1343+
1344+
path->leave_spinning = 1;
13311345
root = delayed_node->root;
13321346

13331347
trans = btrfs_join_transaction(root);
13341348
if (IS_ERR(trans))
1335-
goto free_path;
1349+
goto release_path;
13361350

13371351
block_rsv = trans->block_rsv;
13381352
trans->block_rsv = &root->fs_info->delayed_block_rsv;
@@ -1363,57 +1377,47 @@ static void btrfs_async_run_delayed_node_done(struct btrfs_work *work)
13631377
* Task1 will sleep until the transaction is commited.
13641378
*/
13651379
mutex_lock(&delayed_node->mutex);
1366-
if (delayed_node->count)
1367-
need_requeue = 1;
1368-
else
1369-
btrfs_dequeue_delayed_node(root->fs_info->delayed_root,
1370-
delayed_node);
1380+
btrfs_dequeue_delayed_node(root->fs_info->delayed_root, delayed_node);
13711381
mutex_unlock(&delayed_node->mutex);
13721382

13731383
trans->block_rsv = block_rsv;
13741384
btrfs_end_transaction_dmeta(trans, root);
13751385
btrfs_btree_balance_dirty_nodelay(root);
1386+
1387+
release_path:
1388+
btrfs_release_path(path);
1389+
total_done++;
1390+
1391+
btrfs_release_prepared_delayed_node(delayed_node);
1392+
if (async_work->nr == 0 || total_done < async_work->nr)
1393+
goto again;
1394+
13761395
free_path:
13771396
btrfs_free_path(path);
13781397
out:
1379-
if (need_requeue)
1380-
btrfs_requeue_work(&async_node->work);
1381-
else {
1382-
btrfs_release_prepared_delayed_node(delayed_node);
1383-
kfree(async_node);
1384-
}
1398+
wake_up(&delayed_root->wait);
1399+
kfree(async_work);
13851400
}
13861401

1402+
13871403
static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root,
1388-
struct btrfs_root *root, int all)
1404+
struct btrfs_root *root, int nr)
13891405
{
1390-
struct btrfs_async_delayed_node *async_node;
1391-
struct btrfs_delayed_node *curr;
1392-
int count = 0;
1406+
struct btrfs_async_delayed_work *async_work;
13931407

1394-
again:
1395-
curr = btrfs_first_prepared_delayed_node(delayed_root);
1396-
if (!curr)
1408+
if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
13971409
return 0;
13981410

1399-
async_node = kmalloc(sizeof(*async_node), GFP_NOFS);
1400-
if (!async_node) {
1401-
btrfs_release_prepared_delayed_node(curr);
1411+
async_work = kmalloc(sizeof(*async_work), GFP_NOFS);
1412+
if (!async_work)
14021413
return -ENOMEM;
1403-
}
1404-
1405-
async_node->root = root;
1406-
async_node->delayed_node = curr;
1407-
1408-
async_node->work.func = btrfs_async_run_delayed_node_done;
1409-
async_node->work.flags = 0;
14101414

1411-
btrfs_queue_worker(&root->fs_info->delayed_workers, &async_node->work);
1412-
count++;
1413-
1414-
if (all || count < 4)
1415-
goto again;
1415+
async_work->delayed_root = delayed_root;
1416+
async_work->work.func = btrfs_async_run_delayed_root;
1417+
async_work->work.flags = 0;
1418+
async_work->nr = nr;
14161419

1420+
btrfs_queue_worker(&root->fs_info->delayed_workers, &async_work->work);
14171421
return 0;
14181422
}
14191423

@@ -1424,30 +1428,55 @@ void btrfs_assert_delayed_root_empty(struct btrfs_root *root)
14241428
WARN_ON(btrfs_first_delayed_node(delayed_root));
14251429
}
14261430

1431+
static int refs_newer(struct btrfs_delayed_root *delayed_root,
1432+
int seq, int count)
1433+
{
1434+
int val = atomic_read(&delayed_root->items_seq);
1435+
1436+
if (val < seq || val >= seq + count)
1437+
return 1;
1438+
return 0;
1439+
}
1440+
14271441
void btrfs_balance_delayed_items(struct btrfs_root *root)
14281442
{
14291443
struct btrfs_delayed_root *delayed_root;
1444+
int seq;
14301445

14311446
delayed_root = btrfs_get_delayed_root(root);
14321447

14331448
if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
14341449
return;
14351450

1451+
seq = atomic_read(&delayed_root->items_seq);
1452+
14361453
if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) {
14371454
int ret;
1438-
ret = btrfs_wq_run_delayed_node(delayed_root, root, 1);
1455+
DEFINE_WAIT(__wait);
1456+
1457+
ret = btrfs_wq_run_delayed_node(delayed_root, root, 0);
14391458
if (ret)
14401459
return;
14411460

1442-
wait_event_interruptible_timeout(
1443-
delayed_root->wait,
1444-
(atomic_read(&delayed_root->items) <
1445-
BTRFS_DELAYED_BACKGROUND),
1446-
HZ);
1447-
return;
1461+
while (1) {
1462+
prepare_to_wait(&delayed_root->wait, &__wait,
1463+
TASK_INTERRUPTIBLE);
1464+
1465+
if (refs_newer(delayed_root, seq,
1466+
BTRFS_DELAYED_BATCH) ||
1467+
atomic_read(&delayed_root->items) <
1468+
BTRFS_DELAYED_BACKGROUND) {
1469+
break;
1470+
}
1471+
if (!signal_pending(current))
1472+
schedule();
1473+
else
1474+
break;
1475+
}
1476+
finish_wait(&delayed_root->wait, &__wait);
14481477
}
14491478

1450-
btrfs_wq_run_delayed_node(delayed_root, root, 0);
1479+
btrfs_wq_run_delayed_node(delayed_root, root, BTRFS_DELAYED_BATCH);
14511480
}
14521481

14531482
/* Will return 0 or -ENOMEM */

‎fs/btrfs/delayed-inode.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct btrfs_delayed_root {
4343
*/
4444
struct list_head prepare_list;
4545
atomic_t items; /* for delayed items */
46+
atomic_t items_seq; /* for delayed items */
4647
int nodes; /* for delayed nodes */
4748
wait_queue_head_t wait;
4849
};
@@ -86,6 +87,7 @@ static inline void btrfs_init_delayed_root(
8687
struct btrfs_delayed_root *delayed_root)
8788
{
8889
atomic_set(&delayed_root->items, 0);
90+
atomic_set(&delayed_root->items_seq, 0);
8991
delayed_root->nodes = 0;
9092
spin_lock_init(&delayed_root->lock);
9193
init_waitqueue_head(&delayed_root->wait);

‎fs/btrfs/disk-io.c‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static void btrfs_destroy_ordered_operations(struct btrfs_transaction *t,
6262
static void btrfs_destroy_ordered_extents(struct btrfs_root *root);
6363
static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
6464
struct btrfs_root *root);
65-
static void btrfs_destroy_pending_snapshots(struct btrfs_transaction *t);
65+
static void btrfs_evict_pending_snapshots(struct btrfs_transaction *t);
6666
static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root);
6767
static int btrfs_destroy_marked_extents(struct btrfs_root *root,
6868
struct extent_io_tree *dirty_pages,
@@ -3687,7 +3687,7 @@ int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
36873687
return ret;
36883688
}
36893689

3690-
static void btrfs_destroy_pending_snapshots(struct btrfs_transaction *t)
3690+
static void btrfs_evict_pending_snapshots(struct btrfs_transaction *t)
36913691
{
36923692
struct btrfs_pending_snapshot *snapshot;
36933693
struct list_head splice;
@@ -3700,10 +3700,8 @@ static void btrfs_destroy_pending_snapshots(struct btrfs_transaction *t)
37003700
snapshot = list_entry(splice.next,
37013701
struct btrfs_pending_snapshot,
37023702
list);
3703-
3703+
snapshot->error = -ECANCELED;
37043704
list_del_init(&snapshot->list);
3705-
3706-
kfree(snapshot);
37073705
}
37083706
}
37093707

@@ -3840,6 +3838,8 @@ void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
38403838
cur_trans->blocked = 1;
38413839
wake_up(&root->fs_info->transaction_blocked_wait);
38423840

3841+
btrfs_evict_pending_snapshots(cur_trans);
3842+
38433843
cur_trans->blocked = 0;
38443844
wake_up(&root->fs_info->transaction_wait);
38453845

@@ -3849,8 +3849,6 @@ void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
38493849
btrfs_destroy_delayed_inodes(root);
38503850
btrfs_assert_delayed_root_empty(root);
38513851

3852-
btrfs_destroy_pending_snapshots(cur_trans);
3853-
38543852
btrfs_destroy_marked_extents(root, &cur_trans->dirty_pages,
38553853
EXTENT_DIRTY);
38563854
btrfs_destroy_pinned_extent(root,
@@ -3894,6 +3892,8 @@ int btrfs_cleanup_transaction(struct btrfs_root *root)
38943892
if (waitqueue_active(&root->fs_info->transaction_blocked_wait))
38953893
wake_up(&root->fs_info->transaction_blocked_wait);
38963894

3895+
btrfs_evict_pending_snapshots(t);
3896+
38973897
t->blocked = 0;
38983898
smp_mb();
38993899
if (waitqueue_active(&root->fs_info->transaction_wait))
@@ -3907,8 +3907,6 @@ int btrfs_cleanup_transaction(struct btrfs_root *root)
39073907
btrfs_destroy_delayed_inodes(root);
39083908
btrfs_assert_delayed_root_empty(root);
39093909

3910-
btrfs_destroy_pending_snapshots(t);
3911-
39123910
btrfs_destroy_delalloc_inodes(root);
39133911

39143912
spin_lock(&root->fs_info->trans_lock);

‎fs/btrfs/inode.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8502,6 +8502,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
85028502
struct btrfs_key ins;
85038503
u64 cur_offset = start;
85048504
u64 i_size;
8505+
u64 cur_bytes;
85058506
int ret = 0;
85068507
bool own_trans = true;
85078508

@@ -8516,8 +8517,9 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
85168517
}
85178518
}
85188519

8519-
ret = btrfs_reserve_extent(trans, root,
8520-
min(num_bytes, 256ULL * 1024 * 1024),
8520+
cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8521+
cur_bytes = max(cur_bytes, min_size);
8522+
ret = btrfs_reserve_extent(trans, root, cur_bytes,
85218523
min_size, 0, *alloc_hint, &ins, 1);
85228524
if (ret) {
85238525
if (own_trans)

0 commit comments

Comments
 (0)