Skip to content

Commit f2458e1

Browse files
chrismason-xxDavid Woodhouse
authored andcommitted
Btrfs: change around extent-tree prealloc
Signed-off-by: Chris Mason <[email protected]>
1 parent f68cad0 commit f2458e1

File tree

3 files changed

+87
-27
lines changed

3 files changed

+87
-27
lines changed

fs/btrfs/ctree.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,17 @@ struct btrfs_fs_info {
244244
struct btrfs_root *extent_root;
245245
struct btrfs_root *tree_root;
246246
struct btrfs_root *dev_root;
247-
struct btrfs_key current_insert;
248247
struct btrfs_key last_insert;
249248
struct radix_tree_root fs_roots_radix;
250249
struct radix_tree_root pending_del_radix;
251250
struct radix_tree_root pinned_radix;
252251
struct radix_tree_root dev_radix;
252+
253+
u64 extent_tree_insert[BTRFS_MAX_LEVEL * 3];
254+
int extent_tree_insert_nr;
255+
u64 extent_tree_prealloc[BTRFS_MAX_LEVEL * 3];
256+
int extent_tree_prealloc_nr;
257+
253258
u64 generation;
254259
struct btrfs_transaction *running_transaction;
255260
struct btrfs_super_block *disk_super;
@@ -267,8 +272,7 @@ struct btrfs_fs_info {
267272

268273
/*
269274
* in ram representation of the tree. extent_root is used for all allocations
270-
* and for the extent tree extent_root root. current_insert is used
271-
* only for the extent tree.
275+
* and for the extent tree extent_root root.
272276
*/
273277
struct btrfs_root {
274278
struct buffer_head *node;

fs/btrfs/disk-io.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,8 @@ struct btrfs_root *open_ctree(struct super_block *sb)
542542
fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
543543
fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
544544
fs_info->do_barriers = 1;
545+
fs_info->extent_tree_insert_nr = 0;
546+
fs_info->extent_tree_prealloc_nr = 0;
545547
BTRFS_I(fs_info->btree_inode)->root = tree_root;
546548
memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
547549
sizeof(struct btrfs_key));
@@ -555,7 +557,6 @@ struct btrfs_root *open_ctree(struct super_block *sb)
555557
}
556558
mutex_init(&fs_info->trans_mutex);
557559
mutex_init(&fs_info->fs_mutex);
558-
memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
559560
memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
560561

561562
__setup_root(sb->s_blocksize, dev_root,

fs/btrfs/extent-tree.c

Lines changed: 78 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,17 @@ static int finish_current_insert(struct btrfs_trans_handle *trans, struct
169169
btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
170170
btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
171171

172-
for (i = 0; i < extent_root->fs_info->current_insert.flags; i++) {
173-
ins.objectid = extent_root->fs_info->current_insert.objectid +
174-
i;
172+
for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
173+
ins.objectid = extent_root->fs_info->extent_tree_insert[i];
175174
super_blocks_used = btrfs_super_blocks_used(info->disk_super);
176175
btrfs_set_super_blocks_used(info->disk_super,
177176
super_blocks_used + 1);
178177
ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
179178
sizeof(extent_item));
180179
BUG_ON(ret);
181180
}
182-
extent_root->fs_info->current_insert.offset = 0;
181+
extent_root->fs_info->extent_tree_insert_nr = 0;
182+
extent_root->fs_info->extent_tree_prealloc_nr = 0;
183183
return 0;
184184
}
185185

@@ -349,16 +349,23 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
349349
int start_found;
350350
struct btrfs_leaf *l;
351351
struct btrfs_root * root = orig_root->fs_info->extent_root;
352+
struct btrfs_fs_info *info = root->fs_info;
352353
int total_needed = num_blocks;
354+
int total_found = 0;
355+
int fill_prealloc = 0;
353356
int level;
354357

355358
path = btrfs_alloc_path();
356359
ins->flags = 0;
357360
btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
358361

359362
level = btrfs_header_level(btrfs_buffer_header(root->node));
360-
total_needed += (level + 2) * 3;
361-
if (root->fs_info->last_insert.objectid == 0 && search_end == (u64)-1) {
363+
if (num_blocks == 0) {
364+
fill_prealloc = 1;
365+
num_blocks = 1;
366+
total_needed = min(level + 2, BTRFS_MAX_LEVEL) * 3;
367+
}
368+
if (info->last_insert.objectid == 0 && search_end == (u64)-1) {
362369
struct btrfs_disk_key *last_key;
363370
btrfs_init_path(path);
364371
ins->objectid = (u64)-1;
@@ -373,8 +380,8 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
373380
last_key = &l->items[path->slots[0]].key;
374381
search_start = btrfs_disk_key_objectid(last_key);
375382
}
376-
if (root->fs_info->last_insert.objectid > search_start)
377-
search_start = root->fs_info->last_insert.objectid;
383+
if (info->last_insert.objectid > search_start)
384+
search_start = info->last_insert.objectid;
378385

379386
check_failed:
380387
btrfs_init_path(path);
@@ -392,20 +399,24 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
392399
l = btrfs_buffer_leaf(path->nodes[0]);
393400
slot = path->slots[0];
394401
if (slot >= btrfs_header_nritems(&l->header)) {
402+
if (fill_prealloc) {
403+
info->extent_tree_prealloc_nr = 0;
404+
total_found = 0;
405+
}
395406
ret = btrfs_next_leaf(root, path);
396407
if (ret == 0)
397408
continue;
398409
if (ret < 0)
399410
goto error;
400411
if (!start_found) {
401412
ins->objectid = search_start;
402-
ins->offset = (u64)-1;
413+
ins->offset = (u64)-1 - search_start;
403414
start_found = 1;
404415
goto check_pending;
405416
}
406417
ins->objectid = last_block > search_start ?
407418
last_block : search_start;
408-
ins->offset = (u64)-1;
419+
ins->offset = (u64)-1 - ins->objectid;
409420
goto check_pending;
410421
}
411422
btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
@@ -414,7 +425,7 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
414425
if (last_block < search_start)
415426
last_block = search_start;
416427
hole_size = key.objectid - last_block;
417-
if (hole_size > total_needed) {
428+
if (hole_size > num_blocks) {
418429
ins->objectid = last_block;
419430
ins->offset = hole_size;
420431
goto check_pending;
@@ -433,17 +444,51 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
433444
btrfs_release_path(root, path);
434445
BUG_ON(ins->objectid < search_start);
435446
for (test_block = ins->objectid;
436-
test_block < ins->objectid + total_needed; test_block++) {
437-
if (test_radix_bit(&root->fs_info->pinned_radix,
438-
test_block)) {
447+
test_block < ins->objectid + num_blocks; test_block++) {
448+
if (test_radix_bit(&info->pinned_radix, test_block)) {
439449
search_start = test_block + 1;
440450
goto check_failed;
441451
}
442452
}
443-
BUG_ON(root->fs_info->current_insert.offset);
444-
root->fs_info->current_insert.offset = total_needed - num_blocks;
445-
root->fs_info->current_insert.objectid = ins->objectid + num_blocks;
446-
root->fs_info->current_insert.flags = 0;
453+
if (!fill_prealloc && info->extent_tree_insert_nr) {
454+
u64 last =
455+
info->extent_tree_insert[info->extent_tree_insert_nr - 1];
456+
if (ins->objectid + num_blocks >
457+
info->extent_tree_insert[0] &&
458+
ins->objectid <= last) {
459+
search_start = last + 1;
460+
WARN_ON(1);
461+
goto check_failed;
462+
}
463+
}
464+
if (!fill_prealloc && info->extent_tree_prealloc_nr) {
465+
u64 first =
466+
info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
467+
if (ins->objectid + num_blocks > first &&
468+
ins->objectid <= info->extent_tree_prealloc[0]) {
469+
search_start = info->extent_tree_prealloc[0] + 1;
470+
WARN_ON(1);
471+
goto check_failed;
472+
}
473+
}
474+
if (fill_prealloc) {
475+
int nr;
476+
test_block = ins->objectid;
477+
while(test_block < ins->objectid + ins->offset &&
478+
total_found < total_needed) {
479+
nr = total_needed - total_found - 1;
480+
BUG_ON(nr < 0);
481+
root->fs_info->extent_tree_prealloc[nr] =
482+
test_block;
483+
total_found++;
484+
test_block++;
485+
}
486+
if (total_found < total_needed) {
487+
search_start = test_block;
488+
goto check_failed;
489+
}
490+
root->fs_info->extent_tree_prealloc_nr = total_found;
491+
}
447492
root->fs_info->last_insert.objectid = ins->objectid;
448493
ins->offset = num_blocks;
449494
btrfs_free_path(path);
@@ -472,25 +517,35 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
472517
struct btrfs_fs_info *info = root->fs_info;
473518
struct btrfs_root *extent_root = info->extent_root;
474519
struct btrfs_extent_item extent_item;
520+
struct btrfs_key prealloc_key;
475521

476522
btrfs_set_extent_refs(&extent_item, 1);
477523
btrfs_set_extent_owner(&extent_item, owner);
478524

479525
if (root == extent_root) {
480-
BUG_ON(extent_root->fs_info->current_insert.offset == 0);
526+
int nr;
527+
BUG_ON(info->extent_tree_prealloc_nr == 0);
481528
BUG_ON(num_blocks != 1);
482-
BUG_ON(extent_root->fs_info->current_insert.flags ==
483-
extent_root->fs_info->current_insert.offset);
484529
ins->offset = 1;
485-
ins->objectid = extent_root->fs_info->current_insert.objectid +
486-
extent_root->fs_info->current_insert.flags++;
530+
info->extent_tree_prealloc_nr--;
531+
nr = info->extent_tree_prealloc_nr;
532+
ins->objectid = info->extent_tree_prealloc[nr];
533+
info->extent_tree_insert[info->extent_tree_insert_nr++] =
534+
ins->objectid;
487535
return 0;
488536
}
537+
/* do the real allocation */
489538
ret = find_free_extent(trans, root, num_blocks, search_start,
490539
search_end, ins);
491540
if (ret)
492541
return ret;
493542

543+
/* then do prealloc for the extent tree */
544+
ret = find_free_extent(trans, root, 0, ins->objectid + ins->offset,
545+
search_end, &prealloc_key);
546+
if (ret)
547+
return ret;
548+
494549
super_blocks_used = btrfs_super_blocks_used(info->disk_super);
495550
btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
496551
num_blocks);

0 commit comments

Comments
 (0)