btree.c (f26e8817b235d8764363bffcc9cbfc61867371f2) btree.c (be628be09563f8f6e81929efbd7cf3f45c344416)
1/*
2 * Copyright (C) 2010 Kent Overstreet <kent.overstreet@gmail.com>
3 *
4 * Uses a block device as cache for other block devices; optimized for SSDs.
5 * All allocation is done in buckets, which should match the erase block size
6 * of the device.
7 *
8 * Buckets containing cached data are kept on a heap sorted by priority;

--- 283 unchanged lines hidden (view full) ---

292 trace_bcache_btree_read(b);
293
294 closure_init_stack(&cl);
295
296 bio = bch_bbio_alloc(b->c);
297 bio->bi_iter.bi_size = KEY_SIZE(&b->key) << 9;
298 bio->bi_end_io = btree_node_read_endio;
299 bio->bi_private = &cl;
1/*
2 * Copyright (C) 2010 Kent Overstreet <kent.overstreet@gmail.com>
3 *
4 * Uses a block device as cache for other block devices; optimized for SSDs.
5 * All allocation is done in buckets, which should match the erase block size
6 * of the device.
7 *
8 * Buckets containing cached data are kept on a heap sorted by priority;

--- 283 unchanged lines hidden (view full) ---

292 trace_bcache_btree_read(b);
293
294 closure_init_stack(&cl);
295
296 bio = bch_bbio_alloc(b->c);
297 bio->bi_iter.bi_size = KEY_SIZE(&b->key) << 9;
298 bio->bi_end_io = btree_node_read_endio;
299 bio->bi_private = &cl;
300 bio_set_op_attrs(bio, REQ_OP_READ, REQ_META|READ_SYNC);
300 bio->bi_opf = REQ_OP_READ | REQ_META;
301
302 bch_bio_map(bio, b->keys.set[0].data);
303
304 bch_submit_bbio(bio, b->c, &b->key, 0);
305 closure_sync(&cl);
306
307 if (bio->bi_error)
308 set_btree_node_io_error(b);

--- 47 unchanged lines hidden (view full) ---

356 schedule_delayed_work(&b->work, 30 * HZ);
357
358 closure_return_with_destructor(cl, btree_node_write_unlock);
359}
360
361static void btree_node_write_done(struct closure *cl)
362{
363 struct btree *b = container_of(cl, struct btree, io);
301
302 bch_bio_map(bio, b->keys.set[0].data);
303
304 bch_submit_bbio(bio, b->c, &b->key, 0);
305 closure_sync(&cl);
306
307 if (bio->bi_error)
308 set_btree_node_io_error(b);

--- 47 unchanged lines hidden (view full) ---

356 schedule_delayed_work(&b->work, 30 * HZ);
357
358 closure_return_with_destructor(cl, btree_node_write_unlock);
359}
360
361static void btree_node_write_done(struct closure *cl)
362{
363 struct btree *b = container_of(cl, struct btree, io);
364 struct bio_vec *bv;
365 int n;
366
364
367 bio_for_each_segment_all(bv, b->bio, n)
368 __free_page(bv->bv_page);
369
365 bio_free_pages(b->bio);
370 __btree_node_write_done(cl);
371}
372
373static void btree_node_write_endio(struct bio *bio)
374{
375 struct closure *cl = bio->bi_private;
376 struct btree *b = container_of(cl, struct btree, io);
377

--- 14 unchanged lines hidden (view full) ---

392 i->csum = btree_csum_set(b, i);
393
394 BUG_ON(b->bio);
395 b->bio = bch_bbio_alloc(b->c);
396
397 b->bio->bi_end_io = btree_node_write_endio;
398 b->bio->bi_private = cl;
399 b->bio->bi_iter.bi_size = roundup(set_bytes(i), block_bytes(b->c));
366 __btree_node_write_done(cl);
367}
368
369static void btree_node_write_endio(struct bio *bio)
370{
371 struct closure *cl = bio->bi_private;
372 struct btree *b = container_of(cl, struct btree, io);
373

--- 14 unchanged lines hidden (view full) ---

388 i->csum = btree_csum_set(b, i);
389
390 BUG_ON(b->bio);
391 b->bio = bch_bbio_alloc(b->c);
392
393 b->bio->bi_end_io = btree_node_write_endio;
394 b->bio->bi_private = cl;
395 b->bio->bi_iter.bi_size = roundup(set_bytes(i), block_bytes(b->c));
400 bio_set_op_attrs(b->bio, REQ_OP_WRITE, REQ_META|WRITE_SYNC|REQ_FUA);
396 b->bio->bi_opf = REQ_OP_WRITE | REQ_META | REQ_FUA;
401 bch_bio_map(b->bio, i);
402
403 /*
404 * If we're appending to a leaf node, we don't technically need FUA -
405 * this write just needs to be persisted before the next journal write,
406 * which will be marked FLUSH|FUA.
407 *
408 * Similarly if we're writing a new btree root - the pointer is going to

--- 1347 unchanged lines hidden (view full) ---

1756 stats.in_use = (c->nbuckets - available) * 100 / c->nbuckets;
1757 memcpy(&c->gc_stats, &stats, sizeof(struct gc_stat));
1758
1759 trace_bcache_gc_end(c);
1760
1761 bch_moving_gc(c);
1762}
1763
397 bch_bio_map(b->bio, i);
398
399 /*
400 * If we're appending to a leaf node, we don't technically need FUA -
401 * this write just needs to be persisted before the next journal write,
402 * which will be marked FLUSH|FUA.
403 *
404 * Similarly if we're writing a new btree root - the pointer is going to

--- 1347 unchanged lines hidden (view full) ---

1752 stats.in_use = (c->nbuckets - available) * 100 / c->nbuckets;
1753 memcpy(&c->gc_stats, &stats, sizeof(struct gc_stat));
1754
1755 trace_bcache_gc_end(c);
1756
1757 bch_moving_gc(c);
1758}
1759
1764static int bch_gc_thread(void *arg)
1760static bool gc_should_run(struct cache_set *c)
1765{
1761{
1766 struct cache_set *c = arg;
1767 struct cache *ca;
1768 unsigned i;
1769
1762 struct cache *ca;
1763 unsigned i;
1764
1770 while (1) {
1771again:
1772 bch_btree_gc(c);
1765 for_each_cache(ca, c, i)
1766 if (ca->invalidate_needs_gc)
1767 return true;
1773
1768
1774 set_current_state(TASK_INTERRUPTIBLE);
1775 if (kthread_should_stop())
1776 break;
1769 if (atomic_read(&c->sectors_to_gc) < 0)
1770 return true;
1777
1771
1778 mutex_lock(&c->bucket_lock);
1772 return false;
1773}
1779
1774
1780 for_each_cache(ca, c, i)
1781 if (ca->invalidate_needs_gc) {
1782 mutex_unlock(&c->bucket_lock);
1783 set_current_state(TASK_RUNNING);
1784 goto again;
1785 }
1775static int bch_gc_thread(void *arg)
1776{
1777 struct cache_set *c = arg;
1786
1778
1787 mutex_unlock(&c->bucket_lock);
1779 while (1) {
1780 wait_event_interruptible(c->gc_wait,
1781 kthread_should_stop() || gc_should_run(c));
1788
1782
1789 schedule();
1783 if (kthread_should_stop())
1784 break;
1785
1786 set_gc_sectors(c);
1787 bch_btree_gc(c);
1790 }
1791
1792 return 0;
1793}
1794
1795int bch_gc_thread_start(struct cache_set *c)
1796{
1788 }
1789
1790 return 0;
1791}
1792
1793int bch_gc_thread_start(struct cache_set *c)
1794{
1797 c->gc_thread = kthread_create(bch_gc_thread, c, "bcache_gc");
1795 c->gc_thread = kthread_run(bch_gc_thread, c, "bcache_gc");
1798 if (IS_ERR(c->gc_thread))
1799 return PTR_ERR(c->gc_thread);
1800
1796 if (IS_ERR(c->gc_thread))
1797 return PTR_ERR(c->gc_thread);
1798
1801 set_task_state(c->gc_thread, TASK_INTERRUPTIBLE);
1802 return 0;
1803}
1804
1805/* Initial partial gc */
1806
1807static int bch_btree_check_recurse(struct btree *b, struct btree_op *op)
1808{
1809 int ret = 0;

--- 722 unchanged lines hidden ---
1799 return 0;
1800}
1801
1802/* Initial partial gc */
1803
1804static int bch_btree_check_recurse(struct btree *b, struct btree_op *op)
1805{
1806 int ret = 0;

--- 722 unchanged lines hidden ---