Skip to content

Commit 771ed68

Browse files
committed
Btrfs: Optimize compressed writeback and reads
When reading compressed extents, try to put pages into the page cache for any pages covered by the compressed extent that readpages didn't already preload. Add an async work queue to handle transformations at delayed allocation processing time. Right now this is just compression. The workflow is: 1) Find offsets in the file marked for delayed allocation 2) Lock the pages 3) Lock the state bits 4) Call the async delalloc code The async delalloc code clears the state lock bits and delalloc bits. It is important this happens before the range goes into the work queue because otherwise it might deadlock with other work queue items that try to lock those extent bits. The file pages are compressed, and if the compression doesn't work the pages are written back directly. An ordered work queue is used to make sure the inodes are written in the same order that pdflush or writepages sent them down. This changes extent_write_cache_pages to let the writepage function update the wbc nr_written count. Signed-off-by: Chris Mason <chris.mason@oracle.com>
1 parent 4a69a41 commit 771ed68

11 files changed

Lines changed: 849 additions & 207 deletions

File tree

‎fs/btrfs/compression.c‎

Lines changed: 145 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <linux/writeback.h>
3434
#include <linux/bit_spinlock.h>
3535
#include <linux/version.h>
36+
#include <linux/pagevec.h>
3637
#include "ctree.h"
3738
#include "disk-io.h"
3839
#include "transaction.h"
@@ -145,9 +146,9 @@ static void end_compressed_bio_read(struct bio *bio, int err)
145146
}
146147

147148
/* do io completion on the original bio */
148-
if (cb->errors)
149+
if (cb->errors) {
149150
bio_io_error(cb->orig_bio);
150-
else
151+
} else
151152
bio_endio(cb->orig_bio, 0);
152153

153154
/* finally free the cb struct */
@@ -333,6 +334,7 @@ int btrfs_submit_compressed_write(struct inode *inode, u64 start,
333334
}
334335
bytes_left -= PAGE_CACHE_SIZE;
335336
first_byte += PAGE_CACHE_SIZE;
337+
cond_resched();
336338
}
337339
bio_get(bio);
338340

@@ -346,6 +348,130 @@ int btrfs_submit_compressed_write(struct inode *inode, u64 start,
346348
return 0;
347349
}
348350

351+
static noinline int add_ra_bio_pages(struct inode *inode,
352+
u64 compressed_end,
353+
struct compressed_bio *cb)
354+
{
355+
unsigned long end_index;
356+
unsigned long page_index;
357+
u64 last_offset;
358+
u64 isize = i_size_read(inode);
359+
int ret;
360+
struct page *page;
361+
unsigned long nr_pages = 0;
362+
struct extent_map *em;
363+
struct address_space *mapping = inode->i_mapping;
364+
struct pagevec pvec;
365+
struct extent_map_tree *em_tree;
366+
struct extent_io_tree *tree;
367+
u64 end;
368+
int misses = 0;
369+
370+
page = cb->orig_bio->bi_io_vec[cb->orig_bio->bi_vcnt - 1].bv_page;
371+
last_offset = (page_offset(page) + PAGE_CACHE_SIZE);
372+
em_tree = &BTRFS_I(inode)->extent_tree;
373+
tree = &BTRFS_I(inode)->io_tree;
374+
375+
if (isize == 0)
376+
return 0;
377+
378+
end_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
379+
380+
pagevec_init(&pvec, 0);
381+
while(last_offset < compressed_end) {
382+
page_index = last_offset >> PAGE_CACHE_SHIFT;
383+
384+
if (page_index > end_index)
385+
break;
386+
387+
rcu_read_lock();
388+
page = radix_tree_lookup(&mapping->page_tree, page_index);
389+
rcu_read_unlock();
390+
if (page) {
391+
misses++;
392+
if (misses > 4)
393+
break;
394+
goto next;
395+
}
396+
397+
page = alloc_page(mapping_gfp_mask(mapping) | GFP_NOFS);
398+
if (!page)
399+
break;
400+
401+
page->index = page_index;
402+
/*
403+
* what we want to do here is call add_to_page_cache_lru,
404+
* but that isn't exported, so we reproduce it here
405+
*/
406+
if (add_to_page_cache(page, mapping,
407+
page->index, GFP_NOFS)) {
408+
page_cache_release(page);
409+
goto next;
410+
}
411+
412+
/* open coding of lru_cache_add, also not exported */
413+
page_cache_get(page);
414+
if (!pagevec_add(&pvec, page))
415+
__pagevec_lru_add(&pvec);
416+
417+
end = last_offset + PAGE_CACHE_SIZE - 1;
418+
/*
419+
* at this point, we have a locked page in the page cache
420+
* for these bytes in the file. But, we have to make
421+
* sure they map to this compressed extent on disk.
422+
*/
423+
set_page_extent_mapped(page);
424+
lock_extent(tree, last_offset, end, GFP_NOFS);
425+
spin_lock(&em_tree->lock);
426+
em = lookup_extent_mapping(em_tree, last_offset,
427+
PAGE_CACHE_SIZE);
428+
spin_unlock(&em_tree->lock);
429+
430+
if (!em || last_offset < em->start ||
431+
(last_offset + PAGE_CACHE_SIZE > extent_map_end(em)) ||
432+
(em->block_start >> 9) != cb->orig_bio->bi_sector) {
433+
free_extent_map(em);
434+
unlock_extent(tree, last_offset, end, GFP_NOFS);
435+
unlock_page(page);
436+
page_cache_release(page);
437+
break;
438+
}
439+
free_extent_map(em);
440+
441+
if (page->index == end_index) {
442+
char *userpage;
443+
size_t zero_offset = isize & (PAGE_CACHE_SIZE - 1);
444+
445+
if (zero_offset) {
446+
int zeros;
447+
zeros = PAGE_CACHE_SIZE - zero_offset;
448+
userpage = kmap_atomic(page, KM_USER0);
449+
memset(userpage + zero_offset, 0, zeros);
450+
flush_dcache_page(page);
451+
kunmap_atomic(userpage, KM_USER0);
452+
}
453+
}
454+
455+
ret = bio_add_page(cb->orig_bio, page,
456+
PAGE_CACHE_SIZE, 0);
457+
458+
if (ret == PAGE_CACHE_SIZE) {
459+
nr_pages++;
460+
page_cache_release(page);
461+
} else {
462+
unlock_extent(tree, last_offset, end, GFP_NOFS);
463+
unlock_page(page);
464+
page_cache_release(page);
465+
break;
466+
}
467+
next:
468+
last_offset += PAGE_CACHE_SIZE;
469+
}
470+
if (pagevec_count(&pvec))
471+
__pagevec_lru_add(&pvec);
472+
return 0;
473+
}
474+
349475
/*
350476
* for a compressed read, the bio we get passed has all the inode pages
351477
* in it. We don't actually do IO on those pages but allocate new ones
@@ -373,6 +499,7 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
373499
struct block_device *bdev;
374500
struct bio *comp_bio;
375501
u64 cur_disk_byte = (u64)bio->bi_sector << 9;
502+
u64 em_len;
376503
struct extent_map *em;
377504
int ret;
378505

@@ -393,6 +520,7 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
393520

394521
cb->start = em->start;
395522
compressed_len = em->block_len;
523+
em_len = em->len;
396524
free_extent_map(em);
397525

398526
cb->len = uncompressed_len;
@@ -411,6 +539,17 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
411539
}
412540
cb->nr_pages = nr_pages;
413541

542+
add_ra_bio_pages(inode, cb->start + em_len, cb);
543+
544+
if (!btrfs_test_opt(root, NODATASUM) &&
545+
!btrfs_test_flag(inode, NODATASUM)) {
546+
btrfs_lookup_bio_sums(root, inode, cb->orig_bio);
547+
}
548+
549+
/* include any pages we added in add_ra-bio_pages */
550+
uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
551+
cb->len = uncompressed_len;
552+
414553
comp_bio = compressed_bio_alloc(bdev, cur_disk_byte, GFP_NOFS);
415554
comp_bio->bi_private = cb;
416555
comp_bio->bi_end_io = end_compressed_bio_read;
@@ -442,9 +581,10 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
442581
comp_bio = compressed_bio_alloc(bdev, cur_disk_byte,
443582
GFP_NOFS);
444583
atomic_inc(&cb->pending_bios);
445-
bio->bi_private = cb;
446-
bio->bi_end_io = end_compressed_bio_write;
447-
bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
584+
comp_bio->bi_private = cb;
585+
comp_bio->bi_end_io = end_compressed_bio_read;
586+
587+
bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0);
448588
}
449589
cur_disk_byte += PAGE_CACHE_SIZE;
450590
}

‎fs/btrfs/ctree.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,8 @@ struct btrfs_fs_info {
625625
struct btrfs_transaction *running_transaction;
626626
wait_queue_head_t transaction_throttle;
627627
wait_queue_head_t transaction_wait;
628-
wait_queue_head_t async_submit_wait;
629628

629+
wait_queue_head_t async_submit_wait;
630630
wait_queue_head_t tree_log_wait;
631631

632632
struct btrfs_super_block super_copy;
@@ -653,6 +653,7 @@ struct btrfs_fs_info {
653653
atomic_t nr_async_submits;
654654
atomic_t async_submit_draining;
655655
atomic_t nr_async_bios;
656+
atomic_t async_delalloc_pages;
656657
atomic_t tree_log_writers;
657658
atomic_t tree_log_commit;
658659
unsigned long tree_log_batch;
@@ -677,6 +678,7 @@ struct btrfs_fs_info {
677678
* two
678679
*/
679680
struct btrfs_workers workers;
681+
struct btrfs_workers delalloc_workers;
680682
struct btrfs_workers endio_workers;
681683
struct btrfs_workers endio_write_workers;
682684
struct btrfs_workers submit_workers;

‎fs/btrfs/disk-io.c‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,13 @@ int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
539539
(atomic_read(&fs_info->nr_async_bios) < limit),
540540
HZ/10);
541541
}
542+
543+
while(atomic_read(&fs_info->async_submit_draining) &&
544+
atomic_read(&fs_info->nr_async_submits)) {
545+
wait_event(fs_info->async_submit_wait,
546+
(atomic_read(&fs_info->nr_async_submits) == 0));
547+
}
548+
542549
return 0;
543550
}
544551

@@ -1437,6 +1444,7 @@ struct btrfs_root *open_ctree(struct super_block *sb,
14371444
INIT_LIST_HEAD(&fs_info->space_info);
14381445
btrfs_mapping_init(&fs_info->mapping_tree);
14391446
atomic_set(&fs_info->nr_async_submits, 0);
1447+
atomic_set(&fs_info->async_delalloc_pages, 0);
14401448
atomic_set(&fs_info->async_submit_draining, 0);
14411449
atomic_set(&fs_info->nr_async_bios, 0);
14421450
atomic_set(&fs_info->throttles, 0);
@@ -1550,6 +1558,9 @@ struct btrfs_root *open_ctree(struct super_block *sb,
15501558
btrfs_init_workers(&fs_info->workers, "worker",
15511559
fs_info->thread_pool_size);
15521560

1561+
btrfs_init_workers(&fs_info->delalloc_workers, "delalloc",
1562+
fs_info->thread_pool_size);
1563+
15531564
btrfs_init_workers(&fs_info->submit_workers, "submit",
15541565
min_t(u64, fs_devices->num_devices,
15551566
fs_info->thread_pool_size));
@@ -1560,15 +1571,12 @@ struct btrfs_root *open_ctree(struct super_block *sb,
15601571
*/
15611572
fs_info->submit_workers.idle_thresh = 64;
15621573

1563-
/* fs_info->workers is responsible for checksumming file data
1564-
* blocks and metadata. Using a larger idle thresh allows each
1565-
* worker thread to operate on things in roughly the order they
1566-
* were sent by the writeback daemons, improving overall locality
1567-
* of the IO going down the pipe.
1568-
*/
1569-
fs_info->workers.idle_thresh = 8;
1574+
fs_info->workers.idle_thresh = 16;
15701575
fs_info->workers.ordered = 1;
15711576

1577+
fs_info->delalloc_workers.idle_thresh = 2;
1578+
fs_info->delalloc_workers.ordered = 1;
1579+
15721580
btrfs_init_workers(&fs_info->fixup_workers, "fixup", 1);
15731581
btrfs_init_workers(&fs_info->endio_workers, "endio",
15741582
fs_info->thread_pool_size);
@@ -1584,6 +1592,7 @@ struct btrfs_root *open_ctree(struct super_block *sb,
15841592

15851593
btrfs_start_workers(&fs_info->workers, 1);
15861594
btrfs_start_workers(&fs_info->submit_workers, 1);
1595+
btrfs_start_workers(&fs_info->delalloc_workers, 1);
15871596
btrfs_start_workers(&fs_info->fixup_workers, 1);
15881597
btrfs_start_workers(&fs_info->endio_workers, fs_info->thread_pool_size);
15891598
btrfs_start_workers(&fs_info->endio_write_workers,
@@ -1732,6 +1741,7 @@ struct btrfs_root *open_ctree(struct super_block *sb,
17321741
fail_sys_array:
17331742
fail_sb_buffer:
17341743
btrfs_stop_workers(&fs_info->fixup_workers);
1744+
btrfs_stop_workers(&fs_info->delalloc_workers);
17351745
btrfs_stop_workers(&fs_info->workers);
17361746
btrfs_stop_workers(&fs_info->endio_workers);
17371747
btrfs_stop_workers(&fs_info->endio_write_workers);
@@ -1988,6 +1998,7 @@ int close_ctree(struct btrfs_root *root)
19881998
truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
19891999

19902000
btrfs_stop_workers(&fs_info->fixup_workers);
2001+
btrfs_stop_workers(&fs_info->delalloc_workers);
19912002
btrfs_stop_workers(&fs_info->workers);
19922003
btrfs_stop_workers(&fs_info->endio_workers);
19932004
btrfs_stop_workers(&fs_info->endio_write_workers);
@@ -2062,7 +2073,7 @@ void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
20622073
struct extent_io_tree *tree;
20632074
u64 num_dirty;
20642075
u64 start = 0;
2065-
unsigned long thresh = 96 * 1024 * 1024;
2076+
unsigned long thresh = 32 * 1024 * 1024;
20662077
tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
20672078

20682079
if (current_is_pdflush() || current->flags & PF_MEMALLOC)

‎fs/btrfs/extent-tree.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,11 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
768768
l = path->nodes[0];
769769

770770
btrfs_item_key_to_cpu(l, &key, path->slots[0]);
771-
BUG_ON(key.objectid != bytenr);
771+
if (key.objectid != bytenr) {
772+
btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
773+
printk("wanted %Lu found %Lu\n", bytenr, key.objectid);
774+
BUG();
775+
}
772776
BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
773777

774778
item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);

0 commit comments

Comments
 (0)