1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2cafe5635SKent Overstreet /* 3cafe5635SKent Overstreet * Copyright (C) 2010 Kent Overstreet <kent.overstreet@gmail.com> 4cafe5635SKent Overstreet * 5cafe5635SKent Overstreet * Uses a block device as cache for other block devices; optimized for SSDs. 6cafe5635SKent Overstreet * All allocation is done in buckets, which should match the erase block size 7cafe5635SKent Overstreet * of the device. 8cafe5635SKent Overstreet * 9cafe5635SKent Overstreet * Buckets containing cached data are kept on a heap sorted by priority; 10cafe5635SKent Overstreet * bucket priority is increased on cache hit, and periodically all the buckets 11cafe5635SKent Overstreet * on the heap have their priority scaled down. This currently is just used as 12cafe5635SKent Overstreet * an LRU but in the future should allow for more intelligent heuristics. 13cafe5635SKent Overstreet * 14cafe5635SKent Overstreet * Buckets have an 8 bit counter; freeing is accomplished by incrementing the 15cafe5635SKent Overstreet * counter. Garbage collection is used to remove stale pointers. 16cafe5635SKent Overstreet * 17cafe5635SKent Overstreet * Indexing is done via a btree; nodes are not necessarily fully sorted, rather 18cafe5635SKent Overstreet * as keys are inserted we only sort the pages that have not yet been written. 19cafe5635SKent Overstreet * When garbage collection is run, we resort the entire node. 20cafe5635SKent Overstreet * 215fb94e9cSMauro Carvalho Chehab * All configuration is done via sysfs; see Documentation/admin-guide/bcache.rst. 22cafe5635SKent Overstreet */ 23cafe5635SKent Overstreet 24cafe5635SKent Overstreet #include "bcache.h" 25cafe5635SKent Overstreet #include "btree.h" 26cafe5635SKent Overstreet #include "debug.h" 2765d45231SKent Overstreet #include "extents.h" 28cafe5635SKent Overstreet 29cafe5635SKent Overstreet #include <linux/slab.h> 30cafe5635SKent Overstreet #include <linux/bitops.h> 31cafe5635SKent Overstreet #include <linux/hash.h> 3272a44517SKent Overstreet #include <linux/kthread.h> 33cd953ed0SGeert Uytterhoeven #include <linux/prefetch.h> 34cafe5635SKent Overstreet #include <linux/random.h> 35cafe5635SKent Overstreet #include <linux/rcupdate.h> 36e6017571SIngo Molnar #include <linux/sched/clock.h> 37b2d09103SIngo Molnar #include <linux/rculist.h> 3850a260e8SColy Li #include <linux/delay.h> 39cafe5635SKent Overstreet #include <trace/events/bcache.h> 40cafe5635SKent Overstreet 41cafe5635SKent Overstreet /* 42cafe5635SKent Overstreet * Todo: 43cafe5635SKent Overstreet * register_bcache: Return errors out to userspace correctly 44cafe5635SKent Overstreet * 45cafe5635SKent Overstreet * Writeback: don't undirty key until after a cache flush 46cafe5635SKent Overstreet * 47cafe5635SKent Overstreet * Create an iterator for key pointers 48cafe5635SKent Overstreet * 49cafe5635SKent Overstreet * On btree write error, mark bucket such that it won't be freed from the cache 50cafe5635SKent Overstreet * 51cafe5635SKent Overstreet * Journalling: 52cafe5635SKent Overstreet * Check for bad keys in replay 53cafe5635SKent Overstreet * Propagate barriers 54cafe5635SKent Overstreet * Refcount journal entries in journal_replay 55cafe5635SKent Overstreet * 56cafe5635SKent Overstreet * Garbage collection: 57cafe5635SKent Overstreet * Finish incremental gc 58cafe5635SKent Overstreet * Gc should free old UUIDs, data for invalid UUIDs 59cafe5635SKent Overstreet * 60cafe5635SKent Overstreet * Provide a way to list backing device UUIDs we have data cached for, and 61cafe5635SKent Overstreet * probably how long it's been since we've seen them, and a way to invalidate 62cafe5635SKent Overstreet * dirty data for devices that will never be attached again 63cafe5635SKent Overstreet * 64cafe5635SKent Overstreet * Keep 1 min/5 min/15 min statistics of how busy a block device has been, so 65cafe5635SKent Overstreet * that based on that and how much dirty data we have we can keep writeback 66cafe5635SKent Overstreet * from being starved 67cafe5635SKent Overstreet * 68cafe5635SKent Overstreet * Add a tracepoint or somesuch to watch for writeback starvation 69cafe5635SKent Overstreet * 70cafe5635SKent Overstreet * When btree depth > 1 and splitting an interior node, we have to make sure 71cafe5635SKent Overstreet * alloc_bucket() cannot fail. This should be true but is not completely 72cafe5635SKent Overstreet * obvious. 73cafe5635SKent Overstreet * 74cafe5635SKent Overstreet * Plugging? 75cafe5635SKent Overstreet * 76cafe5635SKent Overstreet * If data write is less than hard sector size of ssd, round up offset in open 77cafe5635SKent Overstreet * bucket to the next whole sector 78cafe5635SKent Overstreet * 79cafe5635SKent Overstreet * Superblock needs to be fleshed out for multiple cache devices 80cafe5635SKent Overstreet * 81cafe5635SKent Overstreet * Add a sysfs tunable for the number of writeback IOs in flight 82cafe5635SKent Overstreet * 83cafe5635SKent Overstreet * Add a sysfs tunable for the number of open data buckets 84cafe5635SKent Overstreet * 85cafe5635SKent Overstreet * IO tracking: Can we track when one process is doing io on behalf of another? 86cafe5635SKent Overstreet * IO tracking: Don't use just an average, weigh more recent stuff higher 87cafe5635SKent Overstreet * 88cafe5635SKent Overstreet * Test module load/unload 89cafe5635SKent Overstreet */ 90cafe5635SKent Overstreet 91cafe5635SKent Overstreet #define MAX_NEED_GC 64 92cafe5635SKent Overstreet #define MAX_SAVE_PRIO 72 937f4a59deSTang Junhui #define MAX_GC_TIMES 100 945c25c4fcSTang Junhui #define MIN_GC_NODES 100 955c25c4fcSTang Junhui #define GC_SLEEP_MS 100 96cafe5635SKent Overstreet 97cafe5635SKent Overstreet #define PTR_DIRTY_BIT (((uint64_t) 1 << 36)) 98cafe5635SKent Overstreet 99cafe5635SKent Overstreet #define PTR_HASH(c, k) \ 100cafe5635SKent Overstreet (((k)->ptr[0] >> c->bucket_bits) | PTR_GEN(k, 0)) 101cafe5635SKent Overstreet 102df8e8970SKent Overstreet #define insert_lock(s, b) ((b)->level <= (s)->lock) 103df8e8970SKent Overstreet 104df8e8970SKent Overstreet 105a85e968eSKent Overstreet static inline struct bset *write_block(struct btree *b) 106a85e968eSKent Overstreet { 107a85e968eSKent Overstreet return ((void *) btree_bset_first(b)) + b->written * block_bytes(b->c); 108a85e968eSKent Overstreet } 109a85e968eSKent Overstreet 1102a285686SKent Overstreet static void bch_btree_init_next(struct btree *b) 1112a285686SKent Overstreet { 1122a285686SKent Overstreet /* If not a leaf node, always sort */ 1132a285686SKent Overstreet if (b->level && b->keys.nsets) 1142a285686SKent Overstreet bch_btree_sort(&b->keys, &b->c->sort); 1152a285686SKent Overstreet else 1162a285686SKent Overstreet bch_btree_sort_lazy(&b->keys, &b->c->sort); 1172a285686SKent Overstreet 1182a285686SKent Overstreet if (b->written < btree_blocks(b)) 1192a285686SKent Overstreet bch_bset_init_next(&b->keys, write_block(b), 1202a285686SKent Overstreet bset_magic(&b->c->sb)); 1212a285686SKent Overstreet 1222a285686SKent Overstreet } 1232a285686SKent Overstreet 124cafe5635SKent Overstreet /* Btree key manipulation */ 125cafe5635SKent Overstreet 1263a3b6a4eSKent Overstreet void bkey_put(struct cache_set *c, struct bkey *k) 127e7c590ebSKent Overstreet { 1286f10f7d1SColy Li unsigned int i; 129e7c590ebSKent Overstreet 130e7c590ebSKent Overstreet for (i = 0; i < KEY_PTRS(k); i++) 131e7c590ebSKent Overstreet if (ptr_available(c, k, i)) 132e7c590ebSKent Overstreet atomic_dec_bug(&PTR_BUCKET(c, k, i)->pin); 133e7c590ebSKent Overstreet } 134e7c590ebSKent Overstreet 135cafe5635SKent Overstreet /* Btree IO */ 136cafe5635SKent Overstreet 137cafe5635SKent Overstreet static uint64_t btree_csum_set(struct btree *b, struct bset *i) 138cafe5635SKent Overstreet { 139cafe5635SKent Overstreet uint64_t crc = b->key.ptr[0]; 140fafff81cSKent Overstreet void *data = (void *) i + 8, *end = bset_bkey_last(i); 141cafe5635SKent Overstreet 142169ef1cfSKent Overstreet crc = bch_crc64_update(crc, data, end - data); 143c19ed23aSKent Overstreet return crc ^ 0xffffffffffffffffULL; 144cafe5635SKent Overstreet } 145cafe5635SKent Overstreet 14678b77bf8SKent Overstreet void bch_btree_node_read_done(struct btree *b) 147cafe5635SKent Overstreet { 148cafe5635SKent Overstreet const char *err = "bad btree header"; 149ee811287SKent Overstreet struct bset *i = btree_bset_first(b); 15057943511SKent Overstreet struct btree_iter *iter; 151cafe5635SKent Overstreet 152d2f96f48SShenghui Wang /* 153d2f96f48SShenghui Wang * c->fill_iter can allocate an iterator with more memory space 154d2f96f48SShenghui Wang * than static MAX_BSETS. 155d2f96f48SShenghui Wang * See the comment arount cache_set->fill_iter. 156d2f96f48SShenghui Wang */ 157d19936a2SKent Overstreet iter = mempool_alloc(&b->c->fill_iter, GFP_NOIO); 15857943511SKent Overstreet iter->size = b->c->sb.bucket_size / b->c->sb.block_size; 159cafe5635SKent Overstreet iter->used = 0; 160cafe5635SKent Overstreet 161280481d0SKent Overstreet #ifdef CONFIG_BCACHE_DEBUG 162c052dd9aSKent Overstreet iter->b = &b->keys; 163280481d0SKent Overstreet #endif 164280481d0SKent Overstreet 16557943511SKent Overstreet if (!i->seq) 166cafe5635SKent Overstreet goto err; 167cafe5635SKent Overstreet 168cafe5635SKent Overstreet for (; 169a85e968eSKent Overstreet b->written < btree_blocks(b) && i->seq == b->keys.set[0].data->seq; 170cafe5635SKent Overstreet i = write_block(b)) { 171cafe5635SKent Overstreet err = "unsupported bset version"; 172cafe5635SKent Overstreet if (i->version > BCACHE_BSET_VERSION) 173cafe5635SKent Overstreet goto err; 174cafe5635SKent Overstreet 175cafe5635SKent Overstreet err = "bad btree header"; 176ee811287SKent Overstreet if (b->written + set_blocks(i, block_bytes(b->c)) > 177ee811287SKent Overstreet btree_blocks(b)) 178cafe5635SKent Overstreet goto err; 179cafe5635SKent Overstreet 180cafe5635SKent Overstreet err = "bad magic"; 18181ab4190SKent Overstreet if (i->magic != bset_magic(&b->c->sb)) 182cafe5635SKent Overstreet goto err; 183cafe5635SKent Overstreet 184cafe5635SKent Overstreet err = "bad checksum"; 185cafe5635SKent Overstreet switch (i->version) { 186cafe5635SKent Overstreet case 0: 187cafe5635SKent Overstreet if (i->csum != csum_set(i)) 188cafe5635SKent Overstreet goto err; 189cafe5635SKent Overstreet break; 190cafe5635SKent Overstreet case BCACHE_BSET_VERSION: 191cafe5635SKent Overstreet if (i->csum != btree_csum_set(b, i)) 192cafe5635SKent Overstreet goto err; 193cafe5635SKent Overstreet break; 194cafe5635SKent Overstreet } 195cafe5635SKent Overstreet 196cafe5635SKent Overstreet err = "empty set"; 197a85e968eSKent Overstreet if (i != b->keys.set[0].data && !i->keys) 198cafe5635SKent Overstreet goto err; 199cafe5635SKent Overstreet 200fafff81cSKent Overstreet bch_btree_iter_push(iter, i->start, bset_bkey_last(i)); 201cafe5635SKent Overstreet 202ee811287SKent Overstreet b->written += set_blocks(i, block_bytes(b->c)); 203cafe5635SKent Overstreet } 204cafe5635SKent Overstreet 205cafe5635SKent Overstreet err = "corrupted btree"; 206cafe5635SKent Overstreet for (i = write_block(b); 207a85e968eSKent Overstreet bset_sector_offset(&b->keys, i) < KEY_SIZE(&b->key); 208cafe5635SKent Overstreet i = ((void *) i) + block_bytes(b->c)) 209a85e968eSKent Overstreet if (i->seq == b->keys.set[0].data->seq) 210cafe5635SKent Overstreet goto err; 211cafe5635SKent Overstreet 212a85e968eSKent Overstreet bch_btree_sort_and_fix_extents(&b->keys, iter, &b->c->sort); 213cafe5635SKent Overstreet 214a85e968eSKent Overstreet i = b->keys.set[0].data; 215cafe5635SKent Overstreet err = "short btree key"; 216a85e968eSKent Overstreet if (b->keys.set[0].size && 217a85e968eSKent Overstreet bkey_cmp(&b->key, &b->keys.set[0].end) < 0) 218cafe5635SKent Overstreet goto err; 219cafe5635SKent Overstreet 220cafe5635SKent Overstreet if (b->written < btree_blocks(b)) 221a85e968eSKent Overstreet bch_bset_init_next(&b->keys, write_block(b), 222a85e968eSKent Overstreet bset_magic(&b->c->sb)); 223cafe5635SKent Overstreet out: 224d19936a2SKent Overstreet mempool_free(iter, &b->c->fill_iter); 22557943511SKent Overstreet return; 226cafe5635SKent Overstreet err: 227cafe5635SKent Overstreet set_btree_node_io_error(b); 22888b9f8c4SKent Overstreet bch_cache_set_error(b->c, "%s at bucket %zu, block %u, %u keys", 229cafe5635SKent Overstreet err, PTR_BUCKET_NR(b->c, &b->key, 0), 23088b9f8c4SKent Overstreet bset_block_offset(b, i), i->keys); 231cafe5635SKent Overstreet goto out; 232cafe5635SKent Overstreet } 233cafe5635SKent Overstreet 2344246a0b6SChristoph Hellwig static void btree_node_read_endio(struct bio *bio) 235cafe5635SKent Overstreet { 23657943511SKent Overstreet struct closure *cl = bio->bi_private; 2371fae7cf0SColy Li 23857943511SKent Overstreet closure_put(cl); 23957943511SKent Overstreet } 240cafe5635SKent Overstreet 24178b77bf8SKent Overstreet static void bch_btree_node_read(struct btree *b) 24257943511SKent Overstreet { 24357943511SKent Overstreet uint64_t start_time = local_clock(); 24457943511SKent Overstreet struct closure cl; 24557943511SKent Overstreet struct bio *bio; 246cafe5635SKent Overstreet 247c37511b8SKent Overstreet trace_bcache_btree_read(b); 248c37511b8SKent Overstreet 24957943511SKent Overstreet closure_init_stack(&cl); 250cafe5635SKent Overstreet 25157943511SKent Overstreet bio = bch_bbio_alloc(b->c); 2524f024f37SKent Overstreet bio->bi_iter.bi_size = KEY_SIZE(&b->key) << 9; 25357943511SKent Overstreet bio->bi_end_io = btree_node_read_endio; 25457943511SKent Overstreet bio->bi_private = &cl; 25570fd7614SChristoph Hellwig bio->bi_opf = REQ_OP_READ | REQ_META; 25657943511SKent Overstreet 257a85e968eSKent Overstreet bch_bio_map(bio, b->keys.set[0].data); 25857943511SKent Overstreet 25957943511SKent Overstreet bch_submit_bbio(bio, b->c, &b->key, 0); 26057943511SKent Overstreet closure_sync(&cl); 26157943511SKent Overstreet 2624e4cbee9SChristoph Hellwig if (bio->bi_status) 26357943511SKent Overstreet set_btree_node_io_error(b); 26457943511SKent Overstreet 26557943511SKent Overstreet bch_bbio_free(bio, b->c); 26657943511SKent Overstreet 26757943511SKent Overstreet if (btree_node_io_error(b)) 26857943511SKent Overstreet goto err; 26957943511SKent Overstreet 27057943511SKent Overstreet bch_btree_node_read_done(b); 27157943511SKent Overstreet bch_time_stats_update(&b->c->btree_read_time, start_time); 27257943511SKent Overstreet 27357943511SKent Overstreet return; 27457943511SKent Overstreet err: 27561cbd250SGeert Uytterhoeven bch_cache_set_error(b->c, "io error reading bucket %zu", 27657943511SKent Overstreet PTR_BUCKET_NR(b->c, &b->key, 0)); 277cafe5635SKent Overstreet } 278cafe5635SKent Overstreet 279cafe5635SKent Overstreet static void btree_complete_write(struct btree *b, struct btree_write *w) 280cafe5635SKent Overstreet { 281cafe5635SKent Overstreet if (w->prio_blocked && 282cafe5635SKent Overstreet !atomic_sub_return(w->prio_blocked, &b->c->prio_blocked)) 283119ba0f8SKent Overstreet wake_up_allocators(b->c); 284cafe5635SKent Overstreet 285cafe5635SKent Overstreet if (w->journal) { 286cafe5635SKent Overstreet atomic_dec_bug(w->journal); 287cafe5635SKent Overstreet __closure_wake_up(&b->c->journal.wait); 288cafe5635SKent Overstreet } 289cafe5635SKent Overstreet 290cafe5635SKent Overstreet w->prio_blocked = 0; 291cafe5635SKent Overstreet w->journal = NULL; 292cafe5635SKent Overstreet } 293cafe5635SKent Overstreet 294cb7a583eSKent Overstreet static void btree_node_write_unlock(struct closure *cl) 295cb7a583eSKent Overstreet { 296cb7a583eSKent Overstreet struct btree *b = container_of(cl, struct btree, io); 297cb7a583eSKent Overstreet 298cb7a583eSKent Overstreet up(&b->io_mutex); 299cb7a583eSKent Overstreet } 300cb7a583eSKent Overstreet 30157943511SKent Overstreet static void __btree_node_write_done(struct closure *cl) 302cafe5635SKent Overstreet { 303cb7a583eSKent Overstreet struct btree *b = container_of(cl, struct btree, io); 304cafe5635SKent Overstreet struct btree_write *w = btree_prev_write(b); 305cafe5635SKent Overstreet 306cafe5635SKent Overstreet bch_bbio_free(b->bio, b->c); 307cafe5635SKent Overstreet b->bio = NULL; 308cafe5635SKent Overstreet btree_complete_write(b, w); 309cafe5635SKent Overstreet 310cafe5635SKent Overstreet if (btree_node_dirty(b)) 31156b30770SKent Overstreet schedule_delayed_work(&b->work, 30 * HZ); 312cafe5635SKent Overstreet 313cb7a583eSKent Overstreet closure_return_with_destructor(cl, btree_node_write_unlock); 314cafe5635SKent Overstreet } 315cafe5635SKent Overstreet 31657943511SKent Overstreet static void btree_node_write_done(struct closure *cl) 317cafe5635SKent Overstreet { 318cb7a583eSKent Overstreet struct btree *b = container_of(cl, struct btree, io); 319cafe5635SKent Overstreet 320491221f8SGuoqing Jiang bio_free_pages(b->bio); 32157943511SKent Overstreet __btree_node_write_done(cl); 322cafe5635SKent Overstreet } 323cafe5635SKent Overstreet 3244246a0b6SChristoph Hellwig static void btree_node_write_endio(struct bio *bio) 32557943511SKent Overstreet { 32657943511SKent Overstreet struct closure *cl = bio->bi_private; 327cb7a583eSKent Overstreet struct btree *b = container_of(cl, struct btree, io); 32857943511SKent Overstreet 3294e4cbee9SChristoph Hellwig if (bio->bi_status) 33057943511SKent Overstreet set_btree_node_io_error(b); 33157943511SKent Overstreet 3324e4cbee9SChristoph Hellwig bch_bbio_count_io_errors(b->c, bio, bio->bi_status, "writing btree"); 33357943511SKent Overstreet closure_put(cl); 33457943511SKent Overstreet } 33557943511SKent Overstreet 33657943511SKent Overstreet static void do_btree_node_write(struct btree *b) 337cafe5635SKent Overstreet { 338cb7a583eSKent Overstreet struct closure *cl = &b->io; 339ee811287SKent Overstreet struct bset *i = btree_bset_last(b); 340cafe5635SKent Overstreet BKEY_PADDED(key) k; 341cafe5635SKent Overstreet 342cafe5635SKent Overstreet i->version = BCACHE_BSET_VERSION; 343cafe5635SKent Overstreet i->csum = btree_csum_set(b, i); 344cafe5635SKent Overstreet 34557943511SKent Overstreet BUG_ON(b->bio); 34657943511SKent Overstreet b->bio = bch_bbio_alloc(b->c); 34757943511SKent Overstreet 34857943511SKent Overstreet b->bio->bi_end_io = btree_node_write_endio; 349faadf0c9SKent Overstreet b->bio->bi_private = cl; 350ee811287SKent Overstreet b->bio->bi_iter.bi_size = roundup(set_bytes(i), block_bytes(b->c)); 35170fd7614SChristoph Hellwig b->bio->bi_opf = REQ_OP_WRITE | REQ_META | REQ_FUA; 352169ef1cfSKent Overstreet bch_bio_map(b->bio, i); 353cafe5635SKent Overstreet 354e49c7c37SKent Overstreet /* 355e49c7c37SKent Overstreet * If we're appending to a leaf node, we don't technically need FUA - 356e49c7c37SKent Overstreet * this write just needs to be persisted before the next journal write, 357e49c7c37SKent Overstreet * which will be marked FLUSH|FUA. 358e49c7c37SKent Overstreet * 359e49c7c37SKent Overstreet * Similarly if we're writing a new btree root - the pointer is going to 360e49c7c37SKent Overstreet * be in the next journal entry. 361e49c7c37SKent Overstreet * 362e49c7c37SKent Overstreet * But if we're writing a new btree node (that isn't a root) or 363e49c7c37SKent Overstreet * appending to a non leaf btree node, we need either FUA or a flush 364e49c7c37SKent Overstreet * when we write the parent with the new pointer. FUA is cheaper than a 365e49c7c37SKent Overstreet * flush, and writes appending to leaf nodes aren't blocking anything so 366e49c7c37SKent Overstreet * just make all btree node writes FUA to keep things sane. 367e49c7c37SKent Overstreet */ 368e49c7c37SKent Overstreet 369cafe5635SKent Overstreet bkey_copy(&k.key, &b->key); 370ee811287SKent Overstreet SET_PTR_OFFSET(&k.key, 0, PTR_OFFSET(&k.key, 0) + 371a85e968eSKent Overstreet bset_sector_offset(&b->keys, i)); 372cafe5635SKent Overstreet 37325d8be77SMing Lei if (!bch_bio_alloc_pages(b->bio, __GFP_NOWARN|GFP_NOWAIT)) { 374cafe5635SKent Overstreet struct bio_vec *bv; 375f936b06aSChristoph Hellwig void *addr = (void *) ((unsigned long) i & ~(PAGE_SIZE - 1)); 3766dc4f100SMing Lei struct bvec_iter_all iter_all; 377cafe5635SKent Overstreet 3782b070cfeSChristoph Hellwig bio_for_each_segment_all(bv, b->bio, iter_all) { 379f936b06aSChristoph Hellwig memcpy(page_address(bv->bv_page), addr, PAGE_SIZE); 380f936b06aSChristoph Hellwig addr += PAGE_SIZE; 381f936b06aSChristoph Hellwig } 382cafe5635SKent Overstreet 383cafe5635SKent Overstreet bch_submit_bbio(b->bio, b->c, &k.key, 0); 384cafe5635SKent Overstreet 38557943511SKent Overstreet continue_at(cl, btree_node_write_done, NULL); 386cafe5635SKent Overstreet } else { 387b0d30981SColy Li /* 388b0d30981SColy Li * No problem for multipage bvec since the bio is 389b0d30981SColy Li * just allocated 390b0d30981SColy Li */ 391cafe5635SKent Overstreet b->bio->bi_vcnt = 0; 392169ef1cfSKent Overstreet bch_bio_map(b->bio, i); 393cafe5635SKent Overstreet 394cafe5635SKent Overstreet bch_submit_bbio(b->bio, b->c, &k.key, 0); 395cafe5635SKent Overstreet 396cafe5635SKent Overstreet closure_sync(cl); 397cb7a583eSKent Overstreet continue_at_nobarrier(cl, __btree_node_write_done, NULL); 398cafe5635SKent Overstreet } 399cafe5635SKent Overstreet } 400cafe5635SKent Overstreet 4012a285686SKent Overstreet void __bch_btree_node_write(struct btree *b, struct closure *parent) 402cafe5635SKent Overstreet { 403ee811287SKent Overstreet struct bset *i = btree_bset_last(b); 404cafe5635SKent Overstreet 4052a285686SKent Overstreet lockdep_assert_held(&b->write_lock); 4062a285686SKent Overstreet 407c37511b8SKent Overstreet trace_bcache_btree_write(b); 408c37511b8SKent Overstreet 409cafe5635SKent Overstreet BUG_ON(current->bio_list); 41057943511SKent Overstreet BUG_ON(b->written >= btree_blocks(b)); 41157943511SKent Overstreet BUG_ON(b->written && !i->keys); 412ee811287SKent Overstreet BUG_ON(btree_bset_first(b)->seq != i->seq); 413dc9d98d6SKent Overstreet bch_check_keys(&b->keys, "writing"); 414cafe5635SKent Overstreet 415cafe5635SKent Overstreet cancel_delayed_work(&b->work); 416cafe5635SKent Overstreet 41757943511SKent Overstreet /* If caller isn't waiting for write, parent refcount is cache set */ 418cb7a583eSKent Overstreet down(&b->io_mutex); 419cb7a583eSKent Overstreet closure_init(&b->io, parent ?: &b->c->cl); 42057943511SKent Overstreet 421cafe5635SKent Overstreet clear_bit(BTREE_NODE_dirty, &b->flags); 422cafe5635SKent Overstreet change_bit(BTREE_NODE_write_idx, &b->flags); 423cafe5635SKent Overstreet 42457943511SKent Overstreet do_btree_node_write(b); 425cafe5635SKent Overstreet 426ee811287SKent Overstreet atomic_long_add(set_blocks(i, block_bytes(b->c)) * b->c->sb.block_size, 427cafe5635SKent Overstreet &PTR_CACHE(b->c, &b->key, 0)->btree_sectors_written); 428cafe5635SKent Overstreet 429a85e968eSKent Overstreet b->written += set_blocks(i, block_bytes(b->c)); 4302a285686SKent Overstreet } 431a85e968eSKent Overstreet 4322a285686SKent Overstreet void bch_btree_node_write(struct btree *b, struct closure *parent) 4332a285686SKent Overstreet { 4346f10f7d1SColy Li unsigned int nsets = b->keys.nsets; 4352a285686SKent Overstreet 4362a285686SKent Overstreet lockdep_assert_held(&b->lock); 4372a285686SKent Overstreet 4382a285686SKent Overstreet __bch_btree_node_write(b, parent); 439cafe5635SKent Overstreet 44078b77bf8SKent Overstreet /* 44178b77bf8SKent Overstreet * do verify if there was more than one set initially (i.e. we did a 44278b77bf8SKent Overstreet * sort) and we sorted down to a single set: 44378b77bf8SKent Overstreet */ 4442a285686SKent Overstreet if (nsets && !b->keys.nsets) 44578b77bf8SKent Overstreet bch_btree_verify(b); 44678b77bf8SKent Overstreet 4472a285686SKent Overstreet bch_btree_init_next(b); 448cafe5635SKent Overstreet } 449cafe5635SKent Overstreet 450f269af5aSKent Overstreet static void bch_btree_node_write_sync(struct btree *b) 451f269af5aSKent Overstreet { 452f269af5aSKent Overstreet struct closure cl; 453f269af5aSKent Overstreet 454f269af5aSKent Overstreet closure_init_stack(&cl); 4552a285686SKent Overstreet 4562a285686SKent Overstreet mutex_lock(&b->write_lock); 457f269af5aSKent Overstreet bch_btree_node_write(b, &cl); 4582a285686SKent Overstreet mutex_unlock(&b->write_lock); 4592a285686SKent Overstreet 460f269af5aSKent Overstreet closure_sync(&cl); 461f269af5aSKent Overstreet } 462f269af5aSKent Overstreet 46357943511SKent Overstreet static void btree_node_write_work(struct work_struct *w) 464cafe5635SKent Overstreet { 465cafe5635SKent Overstreet struct btree *b = container_of(to_delayed_work(w), struct btree, work); 466cafe5635SKent Overstreet 4672a285686SKent Overstreet mutex_lock(&b->write_lock); 468cafe5635SKent Overstreet if (btree_node_dirty(b)) 4692a285686SKent Overstreet __bch_btree_node_write(b, NULL); 4702a285686SKent Overstreet mutex_unlock(&b->write_lock); 471cafe5635SKent Overstreet } 472cafe5635SKent Overstreet 473c18536a7SKent Overstreet static void bch_btree_leaf_dirty(struct btree *b, atomic_t *journal_ref) 474cafe5635SKent Overstreet { 475ee811287SKent Overstreet struct bset *i = btree_bset_last(b); 476cafe5635SKent Overstreet struct btree_write *w = btree_current_write(b); 477cafe5635SKent Overstreet 4782a285686SKent Overstreet lockdep_assert_held(&b->write_lock); 4792a285686SKent Overstreet 48057943511SKent Overstreet BUG_ON(!b->written); 48157943511SKent Overstreet BUG_ON(!i->keys); 482cafe5635SKent Overstreet 48357943511SKent Overstreet if (!btree_node_dirty(b)) 48456b30770SKent Overstreet schedule_delayed_work(&b->work, 30 * HZ); 48557943511SKent Overstreet 486cafe5635SKent Overstreet set_btree_node_dirty(b); 487cafe5635SKent Overstreet 4885dccefd3SColy Li /* 4895dccefd3SColy Li * w->journal is always the oldest journal pin of all bkeys 4905dccefd3SColy Li * in the leaf node, to make sure the oldest jset seq won't 4915dccefd3SColy Li * be increased before this btree node is flushed. 4925dccefd3SColy Li */ 493c18536a7SKent Overstreet if (journal_ref) { 494cafe5635SKent Overstreet if (w->journal && 495c18536a7SKent Overstreet journal_pin_cmp(b->c, w->journal, journal_ref)) { 496cafe5635SKent Overstreet atomic_dec_bug(w->journal); 497cafe5635SKent Overstreet w->journal = NULL; 498cafe5635SKent Overstreet } 499cafe5635SKent Overstreet 500cafe5635SKent Overstreet if (!w->journal) { 501c18536a7SKent Overstreet w->journal = journal_ref; 502cafe5635SKent Overstreet atomic_inc(w->journal); 503cafe5635SKent Overstreet } 504cafe5635SKent Overstreet } 505cafe5635SKent Overstreet 506cafe5635SKent Overstreet /* Force write if set is too big */ 50757943511SKent Overstreet if (set_bytes(i) > PAGE_SIZE - 48 && 50857943511SKent Overstreet !current->bio_list) 50957943511SKent Overstreet bch_btree_node_write(b, NULL); 510cafe5635SKent Overstreet } 511cafe5635SKent Overstreet 512cafe5635SKent Overstreet /* 513cafe5635SKent Overstreet * Btree in memory cache - allocation/freeing 514cafe5635SKent Overstreet * mca -> memory cache 515cafe5635SKent Overstreet */ 516cafe5635SKent Overstreet 517cafe5635SKent Overstreet #define mca_reserve(c) (((c->root && c->root->level) \ 518cafe5635SKent Overstreet ? c->root->level : 1) * 8 + 16) 519cafe5635SKent Overstreet #define mca_can_free(c) \ 5200a63b66dSKent Overstreet max_t(int, 0, c->btree_cache_used - mca_reserve(c)) 521cafe5635SKent Overstreet 522cafe5635SKent Overstreet static void mca_data_free(struct btree *b) 523cafe5635SKent Overstreet { 524cb7a583eSKent Overstreet BUG_ON(b->io_mutex.count != 1); 525cafe5635SKent Overstreet 526a85e968eSKent Overstreet bch_btree_keys_free(&b->keys); 527cafe5635SKent Overstreet 5280a63b66dSKent Overstreet b->c->btree_cache_used--; 529ee811287SKent Overstreet list_move(&b->list, &b->c->btree_cache_freed); 530cafe5635SKent Overstreet } 531cafe5635SKent Overstreet 532cafe5635SKent Overstreet static void mca_bucket_free(struct btree *b) 533cafe5635SKent Overstreet { 534cafe5635SKent Overstreet BUG_ON(btree_node_dirty(b)); 535cafe5635SKent Overstreet 536cafe5635SKent Overstreet b->key.ptr[0] = 0; 537cafe5635SKent Overstreet hlist_del_init_rcu(&b->hash); 538cafe5635SKent Overstreet list_move(&b->list, &b->c->btree_cache_freeable); 539cafe5635SKent Overstreet } 540cafe5635SKent Overstreet 5416f10f7d1SColy Li static unsigned int btree_order(struct bkey *k) 542cafe5635SKent Overstreet { 543cafe5635SKent Overstreet return ilog2(KEY_SIZE(k) / PAGE_SECTORS ?: 1); 544cafe5635SKent Overstreet } 545cafe5635SKent Overstreet 546cafe5635SKent Overstreet static void mca_data_alloc(struct btree *b, struct bkey *k, gfp_t gfp) 547cafe5635SKent Overstreet { 548a85e968eSKent Overstreet if (!bch_btree_keys_alloc(&b->keys, 5496f10f7d1SColy Li max_t(unsigned int, 550cafe5635SKent Overstreet ilog2(b->c->btree_pages), 551ee811287SKent Overstreet btree_order(k)), 552ee811287SKent Overstreet gfp)) { 5530a63b66dSKent Overstreet b->c->btree_cache_used++; 554ee811287SKent Overstreet list_move(&b->list, &b->c->btree_cache); 555ee811287SKent Overstreet } else { 556ee811287SKent Overstreet list_move(&b->list, &b->c->btree_cache_freed); 557ee811287SKent Overstreet } 558cafe5635SKent Overstreet } 559cafe5635SKent Overstreet 560cafe5635SKent Overstreet static struct btree *mca_bucket_alloc(struct cache_set *c, 561cafe5635SKent Overstreet struct bkey *k, gfp_t gfp) 562cafe5635SKent Overstreet { 563bd9026c8SColy Li /* 564bd9026c8SColy Li * kzalloc() is necessary here for initialization, 565bd9026c8SColy Li * see code comments in bch_btree_keys_init(). 566bd9026c8SColy Li */ 567cafe5635SKent Overstreet struct btree *b = kzalloc(sizeof(struct btree), gfp); 5681fae7cf0SColy Li 569cafe5635SKent Overstreet if (!b) 570cafe5635SKent Overstreet return NULL; 571cafe5635SKent Overstreet 572cafe5635SKent Overstreet init_rwsem(&b->lock); 573cafe5635SKent Overstreet lockdep_set_novalidate_class(&b->lock); 5742a285686SKent Overstreet mutex_init(&b->write_lock); 5752a285686SKent Overstreet lockdep_set_novalidate_class(&b->write_lock); 576cafe5635SKent Overstreet INIT_LIST_HEAD(&b->list); 57757943511SKent Overstreet INIT_DELAYED_WORK(&b->work, btree_node_write_work); 578cafe5635SKent Overstreet b->c = c; 579cb7a583eSKent Overstreet sema_init(&b->io_mutex, 1); 580cafe5635SKent Overstreet 581cafe5635SKent Overstreet mca_data_alloc(b, k, gfp); 582cafe5635SKent Overstreet return b; 583cafe5635SKent Overstreet } 584cafe5635SKent Overstreet 5856f10f7d1SColy Li static int mca_reap(struct btree *b, unsigned int min_order, bool flush) 586cafe5635SKent Overstreet { 587e8e1d468SKent Overstreet struct closure cl; 588e8e1d468SKent Overstreet 589e8e1d468SKent Overstreet closure_init_stack(&cl); 590cafe5635SKent Overstreet lockdep_assert_held(&b->c->bucket_lock); 591cafe5635SKent Overstreet 592cafe5635SKent Overstreet if (!down_write_trylock(&b->lock)) 593cafe5635SKent Overstreet return -ENOMEM; 594cafe5635SKent Overstreet 595a85e968eSKent Overstreet BUG_ON(btree_node_dirty(b) && !b->keys.set[0].data); 596e8e1d468SKent Overstreet 597a85e968eSKent Overstreet if (b->keys.page_order < min_order) 598cb7a583eSKent Overstreet goto out_unlock; 599cb7a583eSKent Overstreet 600cb7a583eSKent Overstreet if (!flush) { 601cb7a583eSKent Overstreet if (btree_node_dirty(b)) 602cb7a583eSKent Overstreet goto out_unlock; 603cb7a583eSKent Overstreet 604cb7a583eSKent Overstreet if (down_trylock(&b->io_mutex)) 605cb7a583eSKent Overstreet goto out_unlock; 606cb7a583eSKent Overstreet up(&b->io_mutex); 607cafe5635SKent Overstreet } 608cafe5635SKent Overstreet 60950a260e8SColy Li retry: 61041508bb7SColy Li /* 61141508bb7SColy Li * BTREE_NODE_dirty might be cleared in btree_flush_btree() by 61241508bb7SColy Li * __bch_btree_node_write(). To avoid an extra flush, acquire 61341508bb7SColy Li * b->write_lock before checking BTREE_NODE_dirty bit. 61441508bb7SColy Li */ 6152a285686SKent Overstreet mutex_lock(&b->write_lock); 61650a260e8SColy Li /* 61750a260e8SColy Li * If this btree node is selected in btree_flush_write() by journal 61850a260e8SColy Li * code, delay and retry until the node is flushed by journal code 61950a260e8SColy Li * and BTREE_NODE_journal_flush bit cleared by btree_flush_write(). 62050a260e8SColy Li */ 62150a260e8SColy Li if (btree_node_journal_flush(b)) { 62250a260e8SColy Li pr_debug("bnode %p is flushing by journal, retry", b); 62350a260e8SColy Li mutex_unlock(&b->write_lock); 62450a260e8SColy Li udelay(1); 62550a260e8SColy Li goto retry; 62650a260e8SColy Li } 62750a260e8SColy Li 628f269af5aSKent Overstreet if (btree_node_dirty(b)) 6292a285686SKent Overstreet __bch_btree_node_write(b, &cl); 6302a285686SKent Overstreet mutex_unlock(&b->write_lock); 6312a285686SKent Overstreet 6322a285686SKent Overstreet closure_sync(&cl); 633cafe5635SKent Overstreet 634e8e1d468SKent Overstreet /* wait for any in flight btree write */ 635cb7a583eSKent Overstreet down(&b->io_mutex); 636cb7a583eSKent Overstreet up(&b->io_mutex); 637e8e1d468SKent Overstreet 638cafe5635SKent Overstreet return 0; 639cb7a583eSKent Overstreet out_unlock: 640cb7a583eSKent Overstreet rw_unlock(true, b); 641cb7a583eSKent Overstreet return -ENOMEM; 642cafe5635SKent Overstreet } 643cafe5635SKent Overstreet 6447dc19d5aSDave Chinner static unsigned long bch_mca_scan(struct shrinker *shrink, 6457dc19d5aSDave Chinner struct shrink_control *sc) 646cafe5635SKent Overstreet { 647cafe5635SKent Overstreet struct cache_set *c = container_of(shrink, struct cache_set, shrink); 648cafe5635SKent Overstreet struct btree *b, *t; 649cafe5635SKent Overstreet unsigned long i, nr = sc->nr_to_scan; 6507dc19d5aSDave Chinner unsigned long freed = 0; 651ca71df31STang Junhui unsigned int btree_cache_used; 652cafe5635SKent Overstreet 653cafe5635SKent Overstreet if (c->shrinker_disabled) 6547dc19d5aSDave Chinner return SHRINK_STOP; 655cafe5635SKent Overstreet 6560a63b66dSKent Overstreet if (c->btree_cache_alloc_lock) 6577dc19d5aSDave Chinner return SHRINK_STOP; 658cafe5635SKent Overstreet 659cafe5635SKent Overstreet /* Return -1 if we can't do anything right now */ 660a698e08cSKent Overstreet if (sc->gfp_mask & __GFP_IO) 661cafe5635SKent Overstreet mutex_lock(&c->bucket_lock); 662cafe5635SKent Overstreet else if (!mutex_trylock(&c->bucket_lock)) 663cafe5635SKent Overstreet return -1; 664cafe5635SKent Overstreet 66536c9ea98SKent Overstreet /* 66636c9ea98SKent Overstreet * It's _really_ critical that we don't free too many btree nodes - we 66736c9ea98SKent Overstreet * have to always leave ourselves a reserve. The reserve is how we 66836c9ea98SKent Overstreet * guarantee that allocating memory for a new btree node can always 66936c9ea98SKent Overstreet * succeed, so that inserting keys into the btree can always succeed and 67036c9ea98SKent Overstreet * IO can always make forward progress: 67136c9ea98SKent Overstreet */ 672cafe5635SKent Overstreet nr /= c->btree_pages; 6739fcc34b1SColy Li if (nr == 0) 6749fcc34b1SColy Li nr = 1; 675cafe5635SKent Overstreet nr = min_t(unsigned long, nr, mca_can_free(c)); 676cafe5635SKent Overstreet 677cafe5635SKent Overstreet i = 0; 678ca71df31STang Junhui btree_cache_used = c->btree_cache_used; 679d5c9c470SColy Li list_for_each_entry_safe_reverse(b, t, &c->btree_cache_freeable, list) { 680ca71df31STang Junhui if (nr <= 0) 681ca71df31STang Junhui goto out; 682cafe5635SKent Overstreet 683d5c9c470SColy Li if (!mca_reap(b, 0, false)) { 684cafe5635SKent Overstreet mca_data_free(b); 685cafe5635SKent Overstreet rw_unlock(true, b); 6867dc19d5aSDave Chinner freed++; 687cafe5635SKent Overstreet } 688ca71df31STang Junhui nr--; 689d5c9c470SColy Li i++; 690cafe5635SKent Overstreet } 691cafe5635SKent Overstreet 692e3de0446SColy Li list_for_each_entry_safe_reverse(b, t, &c->btree_cache, list) { 693e3de0446SColy Li if (nr <= 0 || i >= btree_cache_used) 694cafe5635SKent Overstreet goto out; 695cafe5635SKent Overstreet 696125d98edSColy Li if (!mca_reap(b, 0, false)) { 697cafe5635SKent Overstreet mca_bucket_free(b); 698cafe5635SKent Overstreet mca_data_free(b); 699cafe5635SKent Overstreet rw_unlock(true, b); 7007dc19d5aSDave Chinner freed++; 701125d98edSColy Li } 702e3de0446SColy Li 703e3de0446SColy Li nr--; 704e3de0446SColy Li i++; 705cafe5635SKent Overstreet } 706cafe5635SKent Overstreet out: 707cafe5635SKent Overstreet mutex_unlock(&c->bucket_lock); 708f3641c3aSTang Junhui return freed * c->btree_pages; 7097dc19d5aSDave Chinner } 7107dc19d5aSDave Chinner 7117dc19d5aSDave Chinner static unsigned long bch_mca_count(struct shrinker *shrink, 7127dc19d5aSDave Chinner struct shrink_control *sc) 7137dc19d5aSDave Chinner { 7147dc19d5aSDave Chinner struct cache_set *c = container_of(shrink, struct cache_set, shrink); 7157dc19d5aSDave Chinner 7167dc19d5aSDave Chinner if (c->shrinker_disabled) 7177dc19d5aSDave Chinner return 0; 7187dc19d5aSDave Chinner 7190a63b66dSKent Overstreet if (c->btree_cache_alloc_lock) 7207dc19d5aSDave Chinner return 0; 7217dc19d5aSDave Chinner 7227dc19d5aSDave Chinner return mca_can_free(c) * c->btree_pages; 723cafe5635SKent Overstreet } 724cafe5635SKent Overstreet 725cafe5635SKent Overstreet void bch_btree_cache_free(struct cache_set *c) 726cafe5635SKent Overstreet { 727cafe5635SKent Overstreet struct btree *b; 728cafe5635SKent Overstreet struct closure cl; 7291fae7cf0SColy Li 730cafe5635SKent Overstreet closure_init_stack(&cl); 731cafe5635SKent Overstreet 732cafe5635SKent Overstreet if (c->shrink.list.next) 733cafe5635SKent Overstreet unregister_shrinker(&c->shrink); 734cafe5635SKent Overstreet 735cafe5635SKent Overstreet mutex_lock(&c->bucket_lock); 736cafe5635SKent Overstreet 737cafe5635SKent Overstreet #ifdef CONFIG_BCACHE_DEBUG 738cafe5635SKent Overstreet if (c->verify_data) 739cafe5635SKent Overstreet list_move(&c->verify_data->list, &c->btree_cache); 74078b77bf8SKent Overstreet 74178b77bf8SKent Overstreet free_pages((unsigned long) c->verify_ondisk, ilog2(bucket_pages(c))); 742cafe5635SKent Overstreet #endif 743cafe5635SKent Overstreet 744cafe5635SKent Overstreet list_splice(&c->btree_cache_freeable, 745cafe5635SKent Overstreet &c->btree_cache); 746cafe5635SKent Overstreet 747cafe5635SKent Overstreet while (!list_empty(&c->btree_cache)) { 748cafe5635SKent Overstreet b = list_first_entry(&c->btree_cache, struct btree, list); 749cafe5635SKent Overstreet 75041508bb7SColy Li /* 75141508bb7SColy Li * This function is called by cache_set_free(), no I/O 75241508bb7SColy Li * request on cache now, it is unnecessary to acquire 75341508bb7SColy Li * b->write_lock before clearing BTREE_NODE_dirty anymore. 75441508bb7SColy Li */ 755e5ec5f47SColy Li if (btree_node_dirty(b)) { 756cafe5635SKent Overstreet btree_complete_write(b, btree_current_write(b)); 757cafe5635SKent Overstreet clear_bit(BTREE_NODE_dirty, &b->flags); 758e5ec5f47SColy Li } 759cafe5635SKent Overstreet mca_data_free(b); 760cafe5635SKent Overstreet } 761cafe5635SKent Overstreet 762cafe5635SKent Overstreet while (!list_empty(&c->btree_cache_freed)) { 763cafe5635SKent Overstreet b = list_first_entry(&c->btree_cache_freed, 764cafe5635SKent Overstreet struct btree, list); 765cafe5635SKent Overstreet list_del(&b->list); 766cafe5635SKent Overstreet cancel_delayed_work_sync(&b->work); 767cafe5635SKent Overstreet kfree(b); 768cafe5635SKent Overstreet } 769cafe5635SKent Overstreet 770cafe5635SKent Overstreet mutex_unlock(&c->bucket_lock); 771cafe5635SKent Overstreet } 772cafe5635SKent Overstreet 773cafe5635SKent Overstreet int bch_btree_cache_alloc(struct cache_set *c) 774cafe5635SKent Overstreet { 7756f10f7d1SColy Li unsigned int i; 776cafe5635SKent Overstreet 777cafe5635SKent Overstreet for (i = 0; i < mca_reserve(c); i++) 77872a44517SKent Overstreet if (!mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL)) 77972a44517SKent Overstreet return -ENOMEM; 780cafe5635SKent Overstreet 781cafe5635SKent Overstreet list_splice_init(&c->btree_cache, 782cafe5635SKent Overstreet &c->btree_cache_freeable); 783cafe5635SKent Overstreet 784cafe5635SKent Overstreet #ifdef CONFIG_BCACHE_DEBUG 785cafe5635SKent Overstreet mutex_init(&c->verify_lock); 786cafe5635SKent Overstreet 78778b77bf8SKent Overstreet c->verify_ondisk = (void *) 78878b77bf8SKent Overstreet __get_free_pages(GFP_KERNEL, ilog2(bucket_pages(c))); 78978b77bf8SKent Overstreet 790cafe5635SKent Overstreet c->verify_data = mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL); 791cafe5635SKent Overstreet 792cafe5635SKent Overstreet if (c->verify_data && 793a85e968eSKent Overstreet c->verify_data->keys.set->data) 794cafe5635SKent Overstreet list_del_init(&c->verify_data->list); 795cafe5635SKent Overstreet else 796cafe5635SKent Overstreet c->verify_data = NULL; 797cafe5635SKent Overstreet #endif 798cafe5635SKent Overstreet 7997dc19d5aSDave Chinner c->shrink.count_objects = bch_mca_count; 8007dc19d5aSDave Chinner c->shrink.scan_objects = bch_mca_scan; 801cafe5635SKent Overstreet c->shrink.seeks = 4; 802cafe5635SKent Overstreet c->shrink.batch = c->btree_pages * 2; 8036c4ca1e3SMichael Lyle 8046c4ca1e3SMichael Lyle if (register_shrinker(&c->shrink)) 8056c4ca1e3SMichael Lyle pr_warn("bcache: %s: could not register shrinker", 8066c4ca1e3SMichael Lyle __func__); 807cafe5635SKent Overstreet 808cafe5635SKent Overstreet return 0; 809cafe5635SKent Overstreet } 810cafe5635SKent Overstreet 811cafe5635SKent Overstreet /* Btree in memory cache - hash table */ 812cafe5635SKent Overstreet 813cafe5635SKent Overstreet static struct hlist_head *mca_hash(struct cache_set *c, struct bkey *k) 814cafe5635SKent Overstreet { 815cafe5635SKent Overstreet return &c->bucket_hash[hash_32(PTR_HASH(c, k), BUCKET_HASH_BITS)]; 816cafe5635SKent Overstreet } 817cafe5635SKent Overstreet 818cafe5635SKent Overstreet static struct btree *mca_find(struct cache_set *c, struct bkey *k) 819cafe5635SKent Overstreet { 820cafe5635SKent Overstreet struct btree *b; 821cafe5635SKent Overstreet 822cafe5635SKent Overstreet rcu_read_lock(); 823cafe5635SKent Overstreet hlist_for_each_entry_rcu(b, mca_hash(c, k), hash) 824cafe5635SKent Overstreet if (PTR_HASH(c, &b->key) == PTR_HASH(c, k)) 825cafe5635SKent Overstreet goto out; 826cafe5635SKent Overstreet b = NULL; 827cafe5635SKent Overstreet out: 828cafe5635SKent Overstreet rcu_read_unlock(); 829cafe5635SKent Overstreet return b; 830cafe5635SKent Overstreet } 831cafe5635SKent Overstreet 8320a63b66dSKent Overstreet static int mca_cannibalize_lock(struct cache_set *c, struct btree_op *op) 8330a63b66dSKent Overstreet { 83434cf78bfSGuoju Fang spin_lock(&c->btree_cannibalize_lock); 83534cf78bfSGuoju Fang if (likely(c->btree_cache_alloc_lock == NULL)) { 83634cf78bfSGuoju Fang c->btree_cache_alloc_lock = current; 83734cf78bfSGuoju Fang } else if (c->btree_cache_alloc_lock != current) { 8380a63b66dSKent Overstreet if (op) 8390a63b66dSKent Overstreet prepare_to_wait(&c->btree_cache_wait, &op->wait, 8400a63b66dSKent Overstreet TASK_UNINTERRUPTIBLE); 84134cf78bfSGuoju Fang spin_unlock(&c->btree_cannibalize_lock); 8420a63b66dSKent Overstreet return -EINTR; 8430a63b66dSKent Overstreet } 84434cf78bfSGuoju Fang spin_unlock(&c->btree_cannibalize_lock); 8450a63b66dSKent Overstreet 8460a63b66dSKent Overstreet return 0; 8470a63b66dSKent Overstreet } 8480a63b66dSKent Overstreet 8490a63b66dSKent Overstreet static struct btree *mca_cannibalize(struct cache_set *c, struct btree_op *op, 8500a63b66dSKent Overstreet struct bkey *k) 851cafe5635SKent Overstreet { 852e8e1d468SKent Overstreet struct btree *b; 853cafe5635SKent Overstreet 854c37511b8SKent Overstreet trace_bcache_btree_cache_cannibalize(c); 855c37511b8SKent Overstreet 8560a63b66dSKent Overstreet if (mca_cannibalize_lock(c, op)) 8570a63b66dSKent Overstreet return ERR_PTR(-EINTR); 858cafe5635SKent Overstreet 859e8e1d468SKent Overstreet list_for_each_entry_reverse(b, &c->btree_cache, list) 860e8e1d468SKent Overstreet if (!mca_reap(b, btree_order(k), false)) 861e8e1d468SKent Overstreet return b; 862cafe5635SKent Overstreet 863e8e1d468SKent Overstreet list_for_each_entry_reverse(b, &c->btree_cache, list) 864e8e1d468SKent Overstreet if (!mca_reap(b, btree_order(k), true)) 865e8e1d468SKent Overstreet return b; 866e8e1d468SKent Overstreet 8670a63b66dSKent Overstreet WARN(1, "btree cache cannibalize failed\n"); 868e8e1d468SKent Overstreet return ERR_PTR(-ENOMEM); 869cafe5635SKent Overstreet } 870cafe5635SKent Overstreet 871cafe5635SKent Overstreet /* 872cafe5635SKent Overstreet * We can only have one thread cannibalizing other cached btree nodes at a time, 873cafe5635SKent Overstreet * or we'll deadlock. We use an open coded mutex to ensure that, which a 874cafe5635SKent Overstreet * cannibalize_bucket() will take. This means every time we unlock the root of 875cafe5635SKent Overstreet * the btree, we need to release this lock if we have it held. 876cafe5635SKent Overstreet */ 877df8e8970SKent Overstreet static void bch_cannibalize_unlock(struct cache_set *c) 878cafe5635SKent Overstreet { 87934cf78bfSGuoju Fang spin_lock(&c->btree_cannibalize_lock); 8800a63b66dSKent Overstreet if (c->btree_cache_alloc_lock == current) { 8810a63b66dSKent Overstreet c->btree_cache_alloc_lock = NULL; 8820a63b66dSKent Overstreet wake_up(&c->btree_cache_wait); 883cafe5635SKent Overstreet } 88434cf78bfSGuoju Fang spin_unlock(&c->btree_cannibalize_lock); 885cafe5635SKent Overstreet } 886cafe5635SKent Overstreet 8870a63b66dSKent Overstreet static struct btree *mca_alloc(struct cache_set *c, struct btree_op *op, 8880a63b66dSKent Overstreet struct bkey *k, int level) 889cafe5635SKent Overstreet { 890cafe5635SKent Overstreet struct btree *b; 891cafe5635SKent Overstreet 892e8e1d468SKent Overstreet BUG_ON(current->bio_list); 893e8e1d468SKent Overstreet 894cafe5635SKent Overstreet lockdep_assert_held(&c->bucket_lock); 895cafe5635SKent Overstreet 896cafe5635SKent Overstreet if (mca_find(c, k)) 897cafe5635SKent Overstreet return NULL; 898cafe5635SKent Overstreet 899cafe5635SKent Overstreet /* btree_free() doesn't free memory; it sticks the node on the end of 900cafe5635SKent Overstreet * the list. Check if there's any freed nodes there: 901cafe5635SKent Overstreet */ 902cafe5635SKent Overstreet list_for_each_entry(b, &c->btree_cache_freeable, list) 903e8e1d468SKent Overstreet if (!mca_reap(b, btree_order(k), false)) 904cafe5635SKent Overstreet goto out; 905cafe5635SKent Overstreet 906cafe5635SKent Overstreet /* We never free struct btree itself, just the memory that holds the on 907cafe5635SKent Overstreet * disk node. Check the freed list before allocating a new one: 908cafe5635SKent Overstreet */ 909cafe5635SKent Overstreet list_for_each_entry(b, &c->btree_cache_freed, list) 910e8e1d468SKent Overstreet if (!mca_reap(b, 0, false)) { 911cafe5635SKent Overstreet mca_data_alloc(b, k, __GFP_NOWARN|GFP_NOIO); 912a85e968eSKent Overstreet if (!b->keys.set[0].data) 913cafe5635SKent Overstreet goto err; 914cafe5635SKent Overstreet else 915cafe5635SKent Overstreet goto out; 916cafe5635SKent Overstreet } 917cafe5635SKent Overstreet 918cafe5635SKent Overstreet b = mca_bucket_alloc(c, k, __GFP_NOWARN|GFP_NOIO); 919cafe5635SKent Overstreet if (!b) 920cafe5635SKent Overstreet goto err; 921cafe5635SKent Overstreet 922cafe5635SKent Overstreet BUG_ON(!down_write_trylock(&b->lock)); 923a85e968eSKent Overstreet if (!b->keys.set->data) 924cafe5635SKent Overstreet goto err; 925cafe5635SKent Overstreet out: 926cb7a583eSKent Overstreet BUG_ON(b->io_mutex.count != 1); 927cafe5635SKent Overstreet 928cafe5635SKent Overstreet bkey_copy(&b->key, k); 929cafe5635SKent Overstreet list_move(&b->list, &c->btree_cache); 930cafe5635SKent Overstreet hlist_del_init_rcu(&b->hash); 931cafe5635SKent Overstreet hlist_add_head_rcu(&b->hash, mca_hash(c, k)); 932cafe5635SKent Overstreet 933cafe5635SKent Overstreet lock_set_subclass(&b->lock.dep_map, level + 1, _THIS_IP_); 934d6fd3b11SKent Overstreet b->parent = (void *) ~0UL; 935a85e968eSKent Overstreet b->flags = 0; 936a85e968eSKent Overstreet b->written = 0; 937a85e968eSKent Overstreet b->level = level; 938cafe5635SKent Overstreet 93965d45231SKent Overstreet if (!b->level) 940a85e968eSKent Overstreet bch_btree_keys_init(&b->keys, &bch_extent_keys_ops, 941a85e968eSKent Overstreet &b->c->expensive_debug_checks); 94265d45231SKent Overstreet else 943a85e968eSKent Overstreet bch_btree_keys_init(&b->keys, &bch_btree_keys_ops, 944a85e968eSKent Overstreet &b->c->expensive_debug_checks); 945cafe5635SKent Overstreet 946cafe5635SKent Overstreet return b; 947cafe5635SKent Overstreet err: 948cafe5635SKent Overstreet if (b) 949cafe5635SKent Overstreet rw_unlock(true, b); 950cafe5635SKent Overstreet 9510a63b66dSKent Overstreet b = mca_cannibalize(c, op, k); 952cafe5635SKent Overstreet if (!IS_ERR(b)) 953cafe5635SKent Overstreet goto out; 954cafe5635SKent Overstreet 955cafe5635SKent Overstreet return b; 956cafe5635SKent Overstreet } 957cafe5635SKent Overstreet 95847344e33SBart Van Assche /* 959cafe5635SKent Overstreet * bch_btree_node_get - find a btree node in the cache and lock it, reading it 960cafe5635SKent Overstreet * in from disk if necessary. 961cafe5635SKent Overstreet * 962b54d6934SKent Overstreet * If IO is necessary and running under generic_make_request, returns -EAGAIN. 963cafe5635SKent Overstreet * 964cafe5635SKent Overstreet * The btree node will have either a read or a write lock held, depending on 965cafe5635SKent Overstreet * level and op->lock. 966cafe5635SKent Overstreet */ 9670a63b66dSKent Overstreet struct btree *bch_btree_node_get(struct cache_set *c, struct btree_op *op, 9682452cc89SSlava Pestov struct bkey *k, int level, bool write, 9692452cc89SSlava Pestov struct btree *parent) 970cafe5635SKent Overstreet { 971cafe5635SKent Overstreet int i = 0; 972cafe5635SKent Overstreet struct btree *b; 973cafe5635SKent Overstreet 974cafe5635SKent Overstreet BUG_ON(level < 0); 975cafe5635SKent Overstreet retry: 976cafe5635SKent Overstreet b = mca_find(c, k); 977cafe5635SKent Overstreet 978cafe5635SKent Overstreet if (!b) { 97957943511SKent Overstreet if (current->bio_list) 98057943511SKent Overstreet return ERR_PTR(-EAGAIN); 98157943511SKent Overstreet 982cafe5635SKent Overstreet mutex_lock(&c->bucket_lock); 9830a63b66dSKent Overstreet b = mca_alloc(c, op, k, level); 984cafe5635SKent Overstreet mutex_unlock(&c->bucket_lock); 985cafe5635SKent Overstreet 986cafe5635SKent Overstreet if (!b) 987cafe5635SKent Overstreet goto retry; 988cafe5635SKent Overstreet if (IS_ERR(b)) 989cafe5635SKent Overstreet return b; 990cafe5635SKent Overstreet 99157943511SKent Overstreet bch_btree_node_read(b); 992cafe5635SKent Overstreet 993cafe5635SKent Overstreet if (!write) 994cafe5635SKent Overstreet downgrade_write(&b->lock); 995cafe5635SKent Overstreet } else { 996cafe5635SKent Overstreet rw_lock(write, b, level); 997cafe5635SKent Overstreet if (PTR_HASH(c, &b->key) != PTR_HASH(c, k)) { 998cafe5635SKent Overstreet rw_unlock(write, b); 999cafe5635SKent Overstreet goto retry; 1000cafe5635SKent Overstreet } 1001cafe5635SKent Overstreet BUG_ON(b->level != level); 1002cafe5635SKent Overstreet } 1003cafe5635SKent Overstreet 1004c2e8dcf7SColy Li if (btree_node_io_error(b)) { 1005c2e8dcf7SColy Li rw_unlock(write, b); 1006c2e8dcf7SColy Li return ERR_PTR(-EIO); 1007c2e8dcf7SColy Li } 1008c2e8dcf7SColy Li 1009c2e8dcf7SColy Li BUG_ON(!b->written); 1010c2e8dcf7SColy Li 10112452cc89SSlava Pestov b->parent = parent; 1012cafe5635SKent Overstreet 1013a85e968eSKent Overstreet for (; i <= b->keys.nsets && b->keys.set[i].size; i++) { 1014a85e968eSKent Overstreet prefetch(b->keys.set[i].tree); 1015a85e968eSKent Overstreet prefetch(b->keys.set[i].data); 1016cafe5635SKent Overstreet } 1017cafe5635SKent Overstreet 1018a85e968eSKent Overstreet for (; i <= b->keys.nsets; i++) 1019a85e968eSKent Overstreet prefetch(b->keys.set[i].data); 1020cafe5635SKent Overstreet 1021cafe5635SKent Overstreet return b; 1022cafe5635SKent Overstreet } 1023cafe5635SKent Overstreet 10242452cc89SSlava Pestov static void btree_node_prefetch(struct btree *parent, struct bkey *k) 1025cafe5635SKent Overstreet { 1026cafe5635SKent Overstreet struct btree *b; 1027cafe5635SKent Overstreet 10282452cc89SSlava Pestov mutex_lock(&parent->c->bucket_lock); 10292452cc89SSlava Pestov b = mca_alloc(parent->c, NULL, k, parent->level - 1); 10302452cc89SSlava Pestov mutex_unlock(&parent->c->bucket_lock); 1031cafe5635SKent Overstreet 1032cafe5635SKent Overstreet if (!IS_ERR_OR_NULL(b)) { 10332452cc89SSlava Pestov b->parent = parent; 103457943511SKent Overstreet bch_btree_node_read(b); 1035cafe5635SKent Overstreet rw_unlock(true, b); 1036cafe5635SKent Overstreet } 1037cafe5635SKent Overstreet } 1038cafe5635SKent Overstreet 1039cafe5635SKent Overstreet /* Btree alloc */ 1040cafe5635SKent Overstreet 1041e8e1d468SKent Overstreet static void btree_node_free(struct btree *b) 1042cafe5635SKent Overstreet { 1043c37511b8SKent Overstreet trace_bcache_btree_node_free(b); 1044c37511b8SKent Overstreet 1045cafe5635SKent Overstreet BUG_ON(b == b->c->root); 1046cafe5635SKent Overstreet 104750a260e8SColy Li retry: 10482a285686SKent Overstreet mutex_lock(&b->write_lock); 104950a260e8SColy Li /* 105050a260e8SColy Li * If the btree node is selected and flushing in btree_flush_write(), 105150a260e8SColy Li * delay and retry until the BTREE_NODE_journal_flush bit cleared, 105250a260e8SColy Li * then it is safe to free the btree node here. Otherwise this btree 105350a260e8SColy Li * node will be in race condition. 105450a260e8SColy Li */ 105550a260e8SColy Li if (btree_node_journal_flush(b)) { 105650a260e8SColy Li mutex_unlock(&b->write_lock); 105750a260e8SColy Li pr_debug("bnode %p journal_flush set, retry", b); 105850a260e8SColy Li udelay(1); 105950a260e8SColy Li goto retry; 106050a260e8SColy Li } 10612a285686SKent Overstreet 1062e5ec5f47SColy Li if (btree_node_dirty(b)) { 1063cafe5635SKent Overstreet btree_complete_write(b, btree_current_write(b)); 1064cafe5635SKent Overstreet clear_bit(BTREE_NODE_dirty, &b->flags); 1065e5ec5f47SColy Li } 1066cafe5635SKent Overstreet 10672a285686SKent Overstreet mutex_unlock(&b->write_lock); 10682a285686SKent Overstreet 1069cafe5635SKent Overstreet cancel_delayed_work(&b->work); 1070cafe5635SKent Overstreet 1071cafe5635SKent Overstreet mutex_lock(&b->c->bucket_lock); 1072cafe5635SKent Overstreet bch_bucket_free(b->c, &b->key); 1073cafe5635SKent Overstreet mca_bucket_free(b); 1074cafe5635SKent Overstreet mutex_unlock(&b->c->bucket_lock); 1075cafe5635SKent Overstreet } 1076cafe5635SKent Overstreet 1077c5aa4a31SSlava Pestov struct btree *__bch_btree_node_alloc(struct cache_set *c, struct btree_op *op, 10782452cc89SSlava Pestov int level, bool wait, 10792452cc89SSlava Pestov struct btree *parent) 1080cafe5635SKent Overstreet { 1081cafe5635SKent Overstreet BKEY_PADDED(key) k; 1082cafe5635SKent Overstreet struct btree *b = ERR_PTR(-EAGAIN); 1083cafe5635SKent Overstreet 1084cafe5635SKent Overstreet mutex_lock(&c->bucket_lock); 1085cafe5635SKent Overstreet retry: 1086c5aa4a31SSlava Pestov if (__bch_bucket_alloc_set(c, RESERVE_BTREE, &k.key, 1, wait)) 1087cafe5635SKent Overstreet goto err; 1088cafe5635SKent Overstreet 10893a3b6a4eSKent Overstreet bkey_put(c, &k.key); 1090cafe5635SKent Overstreet SET_KEY_SIZE(&k.key, c->btree_pages * PAGE_SECTORS); 1091cafe5635SKent Overstreet 10920a63b66dSKent Overstreet b = mca_alloc(c, op, &k.key, level); 1093cafe5635SKent Overstreet if (IS_ERR(b)) 1094cafe5635SKent Overstreet goto err_free; 1095cafe5635SKent Overstreet 1096cafe5635SKent Overstreet if (!b) { 1097b1a67b0fSKent Overstreet cache_bug(c, 1098b1a67b0fSKent Overstreet "Tried to allocate bucket that was in btree cache"); 1099cafe5635SKent Overstreet goto retry; 1100cafe5635SKent Overstreet } 1101cafe5635SKent Overstreet 11022452cc89SSlava Pestov b->parent = parent; 1103a85e968eSKent Overstreet bch_bset_init_next(&b->keys, b->keys.set->data, bset_magic(&b->c->sb)); 1104cafe5635SKent Overstreet 1105cafe5635SKent Overstreet mutex_unlock(&c->bucket_lock); 1106c37511b8SKent Overstreet 1107c37511b8SKent Overstreet trace_bcache_btree_node_alloc(b); 1108cafe5635SKent Overstreet return b; 1109cafe5635SKent Overstreet err_free: 1110cafe5635SKent Overstreet bch_bucket_free(c, &k.key); 1111cafe5635SKent Overstreet err: 1112cafe5635SKent Overstreet mutex_unlock(&c->bucket_lock); 1113c37511b8SKent Overstreet 1114913dc33fSSlava Pestov trace_bcache_btree_node_alloc_fail(c); 1115cafe5635SKent Overstreet return b; 1116cafe5635SKent Overstreet } 1117cafe5635SKent Overstreet 1118c5aa4a31SSlava Pestov static struct btree *bch_btree_node_alloc(struct cache_set *c, 11192452cc89SSlava Pestov struct btree_op *op, int level, 11202452cc89SSlava Pestov struct btree *parent) 1121c5aa4a31SSlava Pestov { 11222452cc89SSlava Pestov return __bch_btree_node_alloc(c, op, level, op != NULL, parent); 1123c5aa4a31SSlava Pestov } 1124c5aa4a31SSlava Pestov 11250a63b66dSKent Overstreet static struct btree *btree_node_alloc_replacement(struct btree *b, 11260a63b66dSKent Overstreet struct btree_op *op) 1127cafe5635SKent Overstreet { 11282452cc89SSlava Pestov struct btree *n = bch_btree_node_alloc(b->c, op, b->level, b->parent); 11291fae7cf0SColy Li 113067539e85SKent Overstreet if (!IS_ERR_OR_NULL(n)) { 11312a285686SKent Overstreet mutex_lock(&n->write_lock); 113289ebb4a2SKent Overstreet bch_btree_sort_into(&b->keys, &n->keys, &b->c->sort); 113367539e85SKent Overstreet bkey_copy_key(&n->key, &b->key); 11342a285686SKent Overstreet mutex_unlock(&n->write_lock); 113567539e85SKent Overstreet } 1136cafe5635SKent Overstreet 1137cafe5635SKent Overstreet return n; 1138cafe5635SKent Overstreet } 1139cafe5635SKent Overstreet 11408835c123SKent Overstreet static void make_btree_freeing_key(struct btree *b, struct bkey *k) 11418835c123SKent Overstreet { 11426f10f7d1SColy Li unsigned int i; 11438835c123SKent Overstreet 114405335cffSKent Overstreet mutex_lock(&b->c->bucket_lock); 114505335cffSKent Overstreet 114605335cffSKent Overstreet atomic_inc(&b->c->prio_blocked); 114705335cffSKent Overstreet 11488835c123SKent Overstreet bkey_copy(k, &b->key); 11498835c123SKent Overstreet bkey_copy_key(k, &ZERO_KEY); 11508835c123SKent Overstreet 115105335cffSKent Overstreet for (i = 0; i < KEY_PTRS(k); i++) 115205335cffSKent Overstreet SET_PTR_GEN(k, i, 115305335cffSKent Overstreet bch_inc_gen(PTR_CACHE(b->c, &b->key, i), 115405335cffSKent Overstreet PTR_BUCKET(b->c, &b->key, i))); 11558835c123SKent Overstreet 115605335cffSKent Overstreet mutex_unlock(&b->c->bucket_lock); 11578835c123SKent Overstreet } 11588835c123SKent Overstreet 115978365411SKent Overstreet static int btree_check_reserve(struct btree *b, struct btree_op *op) 116078365411SKent Overstreet { 116178365411SKent Overstreet struct cache_set *c = b->c; 116278365411SKent Overstreet struct cache *ca; 11636f10f7d1SColy Li unsigned int i, reserve = (c->root->level - b->level) * 2 + 1; 116478365411SKent Overstreet 116578365411SKent Overstreet mutex_lock(&c->bucket_lock); 116678365411SKent Overstreet 116778365411SKent Overstreet for_each_cache(ca, c, i) 116878365411SKent Overstreet if (fifo_used(&ca->free[RESERVE_BTREE]) < reserve) { 116978365411SKent Overstreet if (op) 11700a63b66dSKent Overstreet prepare_to_wait(&c->btree_cache_wait, &op->wait, 117178365411SKent Overstreet TASK_UNINTERRUPTIBLE); 11720a63b66dSKent Overstreet mutex_unlock(&c->bucket_lock); 11730a63b66dSKent Overstreet return -EINTR; 117478365411SKent Overstreet } 117578365411SKent Overstreet 117678365411SKent Overstreet mutex_unlock(&c->bucket_lock); 11770a63b66dSKent Overstreet 11780a63b66dSKent Overstreet return mca_cannibalize_lock(b->c, op); 117978365411SKent Overstreet } 118078365411SKent Overstreet 1181cafe5635SKent Overstreet /* Garbage collection */ 1182cafe5635SKent Overstreet 1183487dded8SKent Overstreet static uint8_t __bch_btree_mark_key(struct cache_set *c, int level, 1184487dded8SKent Overstreet struct bkey *k) 1185cafe5635SKent Overstreet { 1186cafe5635SKent Overstreet uint8_t stale = 0; 11876f10f7d1SColy Li unsigned int i; 1188cafe5635SKent Overstreet struct bucket *g; 1189cafe5635SKent Overstreet 1190cafe5635SKent Overstreet /* 1191cafe5635SKent Overstreet * ptr_invalid() can't return true for the keys that mark btree nodes as 1192cafe5635SKent Overstreet * freed, but since ptr_bad() returns true we'll never actually use them 1193cafe5635SKent Overstreet * for anything and thus we don't want mark their pointers here 1194cafe5635SKent Overstreet */ 1195cafe5635SKent Overstreet if (!bkey_cmp(k, &ZERO_KEY)) 1196cafe5635SKent Overstreet return stale; 1197cafe5635SKent Overstreet 1198cafe5635SKent Overstreet for (i = 0; i < KEY_PTRS(k); i++) { 1199cafe5635SKent Overstreet if (!ptr_available(c, k, i)) 1200cafe5635SKent Overstreet continue; 1201cafe5635SKent Overstreet 1202cafe5635SKent Overstreet g = PTR_BUCKET(c, k, i); 1203cafe5635SKent Overstreet 12043a2fd9d5SKent Overstreet if (gen_after(g->last_gc, PTR_GEN(k, i))) 12053a2fd9d5SKent Overstreet g->last_gc = PTR_GEN(k, i); 1206cafe5635SKent Overstreet 1207cafe5635SKent Overstreet if (ptr_stale(c, k, i)) { 1208cafe5635SKent Overstreet stale = max(stale, ptr_stale(c, k, i)); 1209cafe5635SKent Overstreet continue; 1210cafe5635SKent Overstreet } 1211cafe5635SKent Overstreet 1212cafe5635SKent Overstreet cache_bug_on(GC_MARK(g) && 1213cafe5635SKent Overstreet (GC_MARK(g) == GC_MARK_METADATA) != (level != 0), 1214cafe5635SKent Overstreet c, "inconsistent ptrs: mark = %llu, level = %i", 1215cafe5635SKent Overstreet GC_MARK(g), level); 1216cafe5635SKent Overstreet 1217cafe5635SKent Overstreet if (level) 1218cafe5635SKent Overstreet SET_GC_MARK(g, GC_MARK_METADATA); 1219cafe5635SKent Overstreet else if (KEY_DIRTY(k)) 1220cafe5635SKent Overstreet SET_GC_MARK(g, GC_MARK_DIRTY); 12214fe6a816SKent Overstreet else if (!GC_MARK(g)) 12224fe6a816SKent Overstreet SET_GC_MARK(g, GC_MARK_RECLAIMABLE); 1223cafe5635SKent Overstreet 1224cafe5635SKent Overstreet /* guard against overflow */ 12256f10f7d1SColy Li SET_GC_SECTORS_USED(g, min_t(unsigned int, 1226cafe5635SKent Overstreet GC_SECTORS_USED(g) + KEY_SIZE(k), 122794717447SDarrick J. Wong MAX_GC_SECTORS_USED)); 1228cafe5635SKent Overstreet 1229cafe5635SKent Overstreet BUG_ON(!GC_SECTORS_USED(g)); 1230cafe5635SKent Overstreet } 1231cafe5635SKent Overstreet 1232cafe5635SKent Overstreet return stale; 1233cafe5635SKent Overstreet } 1234cafe5635SKent Overstreet 1235cafe5635SKent Overstreet #define btree_mark_key(b, k) __bch_btree_mark_key(b->c, b->level, k) 1236cafe5635SKent Overstreet 1237487dded8SKent Overstreet void bch_initial_mark_key(struct cache_set *c, int level, struct bkey *k) 1238487dded8SKent Overstreet { 12396f10f7d1SColy Li unsigned int i; 1240487dded8SKent Overstreet 1241487dded8SKent Overstreet for (i = 0; i < KEY_PTRS(k); i++) 1242487dded8SKent Overstreet if (ptr_available(c, k, i) && 1243487dded8SKent Overstreet !ptr_stale(c, k, i)) { 1244487dded8SKent Overstreet struct bucket *b = PTR_BUCKET(c, k, i); 1245487dded8SKent Overstreet 1246487dded8SKent Overstreet b->gen = PTR_GEN(k, i); 1247487dded8SKent Overstreet 1248487dded8SKent Overstreet if (level && bkey_cmp(k, &ZERO_KEY)) 1249487dded8SKent Overstreet b->prio = BTREE_PRIO; 1250487dded8SKent Overstreet else if (!level && b->prio == BTREE_PRIO) 1251487dded8SKent Overstreet b->prio = INITIAL_PRIO; 1252487dded8SKent Overstreet } 1253487dded8SKent Overstreet 1254487dded8SKent Overstreet __bch_btree_mark_key(c, level, k); 1255487dded8SKent Overstreet } 1256487dded8SKent Overstreet 1257d44c2f9eSTang Junhui void bch_update_bucket_in_use(struct cache_set *c, struct gc_stat *stats) 1258d44c2f9eSTang Junhui { 1259d44c2f9eSTang Junhui stats->in_use = (c->nbuckets - c->avail_nbuckets) * 100 / c->nbuckets; 1260d44c2f9eSTang Junhui } 1261d44c2f9eSTang Junhui 1262a1f0358bSKent Overstreet static bool btree_gc_mark_node(struct btree *b, struct gc_stat *gc) 1263cafe5635SKent Overstreet { 1264cafe5635SKent Overstreet uint8_t stale = 0; 12656f10f7d1SColy Li unsigned int keys = 0, good_keys = 0; 1266cafe5635SKent Overstreet struct bkey *k; 1267cafe5635SKent Overstreet struct btree_iter iter; 1268cafe5635SKent Overstreet struct bset_tree *t; 1269cafe5635SKent Overstreet 1270cafe5635SKent Overstreet gc->nodes++; 1271cafe5635SKent Overstreet 1272c052dd9aSKent Overstreet for_each_key_filter(&b->keys, k, &iter, bch_ptr_invalid) { 1273cafe5635SKent Overstreet stale = max(stale, btree_mark_key(b, k)); 1274a1f0358bSKent Overstreet keys++; 1275cafe5635SKent Overstreet 1276a85e968eSKent Overstreet if (bch_ptr_bad(&b->keys, k)) 1277cafe5635SKent Overstreet continue; 1278cafe5635SKent Overstreet 1279cafe5635SKent Overstreet gc->key_bytes += bkey_u64s(k); 1280cafe5635SKent Overstreet gc->nkeys++; 1281a1f0358bSKent Overstreet good_keys++; 1282cafe5635SKent Overstreet 1283cafe5635SKent Overstreet gc->data += KEY_SIZE(k); 1284cafe5635SKent Overstreet } 1285cafe5635SKent Overstreet 1286a85e968eSKent Overstreet for (t = b->keys.set; t <= &b->keys.set[b->keys.nsets]; t++) 1287cafe5635SKent Overstreet btree_bug_on(t->size && 1288a85e968eSKent Overstreet bset_written(&b->keys, t) && 1289cafe5635SKent Overstreet bkey_cmp(&b->key, &t->end) < 0, 1290cafe5635SKent Overstreet b, "found short btree key in gc"); 1291cafe5635SKent Overstreet 1292a1f0358bSKent Overstreet if (b->c->gc_always_rewrite) 1293a1f0358bSKent Overstreet return true; 1294a1f0358bSKent Overstreet 1295a1f0358bSKent Overstreet if (stale > 10) 1296a1f0358bSKent Overstreet return true; 1297a1f0358bSKent Overstreet 1298a1f0358bSKent Overstreet if ((keys - good_keys) * 2 > keys) 1299a1f0358bSKent Overstreet return true; 1300a1f0358bSKent Overstreet 1301a1f0358bSKent Overstreet return false; 1302cafe5635SKent Overstreet } 1303cafe5635SKent Overstreet 1304a1f0358bSKent Overstreet #define GC_MERGE_NODES 4U 1305cafe5635SKent Overstreet 1306cafe5635SKent Overstreet struct gc_merge_info { 1307cafe5635SKent Overstreet struct btree *b; 13086f10f7d1SColy Li unsigned int keys; 1309cafe5635SKent Overstreet }; 1310cafe5635SKent Overstreet 1311fc2d5988SColy Li static int bch_btree_insert_node(struct btree *b, struct btree_op *op, 1312fc2d5988SColy Li struct keylist *insert_keys, 1313fc2d5988SColy Li atomic_t *journal_ref, 1314fc2d5988SColy Li struct bkey *replace_key); 1315a1f0358bSKent Overstreet 1316a1f0358bSKent Overstreet static int btree_gc_coalesce(struct btree *b, struct btree_op *op, 13170a63b66dSKent Overstreet struct gc_stat *gc, struct gc_merge_info *r) 1318cafe5635SKent Overstreet { 13196f10f7d1SColy Li unsigned int i, nodes = 0, keys = 0, blocks; 1320a1f0358bSKent Overstreet struct btree *new_nodes[GC_MERGE_NODES]; 13210a63b66dSKent Overstreet struct keylist keylist; 1322b54d6934SKent Overstreet struct closure cl; 1323a1f0358bSKent Overstreet struct bkey *k; 1324b54d6934SKent Overstreet 13250a63b66dSKent Overstreet bch_keylist_init(&keylist); 13260a63b66dSKent Overstreet 13270a63b66dSKent Overstreet if (btree_check_reserve(b, NULL)) 13280a63b66dSKent Overstreet return 0; 13290a63b66dSKent Overstreet 1330a1f0358bSKent Overstreet memset(new_nodes, 0, sizeof(new_nodes)); 1331b54d6934SKent Overstreet closure_init_stack(&cl); 1332cafe5635SKent Overstreet 1333a1f0358bSKent Overstreet while (nodes < GC_MERGE_NODES && !IS_ERR_OR_NULL(r[nodes].b)) 1334cafe5635SKent Overstreet keys += r[nodes++].keys; 1335cafe5635SKent Overstreet 1336cafe5635SKent Overstreet blocks = btree_default_blocks(b->c) * 2 / 3; 1337cafe5635SKent Overstreet 1338cafe5635SKent Overstreet if (nodes < 2 || 1339a85e968eSKent Overstreet __set_blocks(b->keys.set[0].data, keys, 1340ee811287SKent Overstreet block_bytes(b->c)) > blocks * (nodes - 1)) 1341a1f0358bSKent Overstreet return 0; 1342cafe5635SKent Overstreet 1343a1f0358bSKent Overstreet for (i = 0; i < nodes; i++) { 13440a63b66dSKent Overstreet new_nodes[i] = btree_node_alloc_replacement(r[i].b, NULL); 1345a1f0358bSKent Overstreet if (IS_ERR_OR_NULL(new_nodes[i])) 1346a1f0358bSKent Overstreet goto out_nocoalesce; 1347cafe5635SKent Overstreet } 1348cafe5635SKent Overstreet 13490a63b66dSKent Overstreet /* 13500a63b66dSKent Overstreet * We have to check the reserve here, after we've allocated our new 13510a63b66dSKent Overstreet * nodes, to make sure the insert below will succeed - we also check 13520a63b66dSKent Overstreet * before as an optimization to potentially avoid a bunch of expensive 13530a63b66dSKent Overstreet * allocs/sorts 13540a63b66dSKent Overstreet */ 13550a63b66dSKent Overstreet if (btree_check_reserve(b, NULL)) 13560a63b66dSKent Overstreet goto out_nocoalesce; 13570a63b66dSKent Overstreet 13582a285686SKent Overstreet for (i = 0; i < nodes; i++) 13592a285686SKent Overstreet mutex_lock(&new_nodes[i]->write_lock); 13602a285686SKent Overstreet 1361cafe5635SKent Overstreet for (i = nodes - 1; i > 0; --i) { 1362ee811287SKent Overstreet struct bset *n1 = btree_bset_first(new_nodes[i]); 1363ee811287SKent Overstreet struct bset *n2 = btree_bset_first(new_nodes[i - 1]); 1364cafe5635SKent Overstreet struct bkey *k, *last = NULL; 1365cafe5635SKent Overstreet 1366cafe5635SKent Overstreet keys = 0; 1367cafe5635SKent Overstreet 1368a1f0358bSKent Overstreet if (i > 1) { 1369cafe5635SKent Overstreet for (k = n2->start; 1370fafff81cSKent Overstreet k < bset_bkey_last(n2); 1371cafe5635SKent Overstreet k = bkey_next(k)) { 1372cafe5635SKent Overstreet if (__set_blocks(n1, n1->keys + keys + 1373ee811287SKent Overstreet bkey_u64s(k), 1374ee811287SKent Overstreet block_bytes(b->c)) > blocks) 1375cafe5635SKent Overstreet break; 1376cafe5635SKent Overstreet 1377cafe5635SKent Overstreet last = k; 1378cafe5635SKent Overstreet keys += bkey_u64s(k); 1379cafe5635SKent Overstreet } 1380a1f0358bSKent Overstreet } else { 1381a1f0358bSKent Overstreet /* 1382a1f0358bSKent Overstreet * Last node we're not getting rid of - we're getting 1383a1f0358bSKent Overstreet * rid of the node at r[0]. Have to try and fit all of 1384a1f0358bSKent Overstreet * the remaining keys into this node; we can't ensure 1385a1f0358bSKent Overstreet * they will always fit due to rounding and variable 1386a1f0358bSKent Overstreet * length keys (shouldn't be possible in practice, 1387a1f0358bSKent Overstreet * though) 1388a1f0358bSKent Overstreet */ 1389a1f0358bSKent Overstreet if (__set_blocks(n1, n1->keys + n2->keys, 1390ee811287SKent Overstreet block_bytes(b->c)) > 1391ee811287SKent Overstreet btree_blocks(new_nodes[i])) 1392a1f0358bSKent Overstreet goto out_nocoalesce; 1393a1f0358bSKent Overstreet 1394a1f0358bSKent Overstreet keys = n2->keys; 1395a1f0358bSKent Overstreet /* Take the key of the node we're getting rid of */ 1396a1f0358bSKent Overstreet last = &r->b->key; 1397a1f0358bSKent Overstreet } 1398cafe5635SKent Overstreet 1399ee811287SKent Overstreet BUG_ON(__set_blocks(n1, n1->keys + keys, block_bytes(b->c)) > 1400ee811287SKent Overstreet btree_blocks(new_nodes[i])); 1401cafe5635SKent Overstreet 1402a1f0358bSKent Overstreet if (last) 1403a1f0358bSKent Overstreet bkey_copy_key(&new_nodes[i]->key, last); 1404cafe5635SKent Overstreet 1405fafff81cSKent Overstreet memcpy(bset_bkey_last(n1), 1406cafe5635SKent Overstreet n2->start, 1407fafff81cSKent Overstreet (void *) bset_bkey_idx(n2, keys) - (void *) n2->start); 1408cafe5635SKent Overstreet 1409cafe5635SKent Overstreet n1->keys += keys; 1410a1f0358bSKent Overstreet r[i].keys = n1->keys; 1411cafe5635SKent Overstreet 1412cafe5635SKent Overstreet memmove(n2->start, 1413fafff81cSKent Overstreet bset_bkey_idx(n2, keys), 1414fafff81cSKent Overstreet (void *) bset_bkey_last(n2) - 1415fafff81cSKent Overstreet (void *) bset_bkey_idx(n2, keys)); 1416cafe5635SKent Overstreet 1417cafe5635SKent Overstreet n2->keys -= keys; 1418cafe5635SKent Overstreet 14190a63b66dSKent Overstreet if (__bch_keylist_realloc(&keylist, 1420085d2a3dSKent Overstreet bkey_u64s(&new_nodes[i]->key))) 1421a1f0358bSKent Overstreet goto out_nocoalesce; 1422a1f0358bSKent Overstreet 1423a1f0358bSKent Overstreet bch_btree_node_write(new_nodes[i], &cl); 14240a63b66dSKent Overstreet bch_keylist_add(&keylist, &new_nodes[i]->key); 1425cafe5635SKent Overstreet } 1426cafe5635SKent Overstreet 14272a285686SKent Overstreet for (i = 0; i < nodes; i++) 14282a285686SKent Overstreet mutex_unlock(&new_nodes[i]->write_lock); 14292a285686SKent Overstreet 143005335cffSKent Overstreet closure_sync(&cl); 143105335cffSKent Overstreet 143205335cffSKent Overstreet /* We emptied out this node */ 143305335cffSKent Overstreet BUG_ON(btree_bset_first(new_nodes[0])->keys); 143405335cffSKent Overstreet btree_node_free(new_nodes[0]); 143505335cffSKent Overstreet rw_unlock(true, new_nodes[0]); 1436400ffaa2SSlava Pestov new_nodes[0] = NULL; 143705335cffSKent Overstreet 1438a1f0358bSKent Overstreet for (i = 0; i < nodes; i++) { 14390a63b66dSKent Overstreet if (__bch_keylist_realloc(&keylist, bkey_u64s(&r[i].b->key))) 1440a1f0358bSKent Overstreet goto out_nocoalesce; 1441a1f0358bSKent Overstreet 14420a63b66dSKent Overstreet make_btree_freeing_key(r[i].b, keylist.top); 14430a63b66dSKent Overstreet bch_keylist_push(&keylist); 1444a1f0358bSKent Overstreet } 1445a1f0358bSKent Overstreet 14460a63b66dSKent Overstreet bch_btree_insert_node(b, op, &keylist, NULL, NULL); 14470a63b66dSKent Overstreet BUG_ON(!bch_keylist_empty(&keylist)); 1448a1f0358bSKent Overstreet 1449a1f0358bSKent Overstreet for (i = 0; i < nodes; i++) { 1450a1f0358bSKent Overstreet btree_node_free(r[i].b); 1451a1f0358bSKent Overstreet rw_unlock(true, r[i].b); 1452a1f0358bSKent Overstreet 1453a1f0358bSKent Overstreet r[i].b = new_nodes[i]; 1454a1f0358bSKent Overstreet } 1455a1f0358bSKent Overstreet 1456a1f0358bSKent Overstreet memmove(r, r + 1, sizeof(r[0]) * (nodes - 1)); 1457a1f0358bSKent Overstreet r[nodes - 1].b = ERR_PTR(-EINTR); 1458cafe5635SKent Overstreet 1459c37511b8SKent Overstreet trace_bcache_btree_gc_coalesce(nodes); 1460cafe5635SKent Overstreet gc->nodes--; 1461cafe5635SKent Overstreet 14620a63b66dSKent Overstreet bch_keylist_free(&keylist); 14630a63b66dSKent Overstreet 1464a1f0358bSKent Overstreet /* Invalidated our iterator */ 1465a1f0358bSKent Overstreet return -EINTR; 1466a1f0358bSKent Overstreet 1467a1f0358bSKent Overstreet out_nocoalesce: 1468a1f0358bSKent Overstreet closure_sync(&cl); 1469a1f0358bSKent Overstreet 14700a63b66dSKent Overstreet while ((k = bch_keylist_pop(&keylist))) 1471a1f0358bSKent Overstreet if (!bkey_cmp(k, &ZERO_KEY)) 1472a1f0358bSKent Overstreet atomic_dec(&b->c->prio_blocked); 1473f16277caSShenghui Wang bch_keylist_free(&keylist); 1474a1f0358bSKent Overstreet 1475a1f0358bSKent Overstreet for (i = 0; i < nodes; i++) 1476a1f0358bSKent Overstreet if (!IS_ERR_OR_NULL(new_nodes[i])) { 1477a1f0358bSKent Overstreet btree_node_free(new_nodes[i]); 1478a1f0358bSKent Overstreet rw_unlock(true, new_nodes[i]); 1479a1f0358bSKent Overstreet } 1480a1f0358bSKent Overstreet return 0; 1481a1f0358bSKent Overstreet } 1482a1f0358bSKent Overstreet 14830a63b66dSKent Overstreet static int btree_gc_rewrite_node(struct btree *b, struct btree_op *op, 14840a63b66dSKent Overstreet struct btree *replace) 14850a63b66dSKent Overstreet { 14860a63b66dSKent Overstreet struct keylist keys; 14870a63b66dSKent Overstreet struct btree *n; 14880a63b66dSKent Overstreet 14890a63b66dSKent Overstreet if (btree_check_reserve(b, NULL)) 14900a63b66dSKent Overstreet return 0; 14910a63b66dSKent Overstreet 14920a63b66dSKent Overstreet n = btree_node_alloc_replacement(replace, NULL); 14930a63b66dSKent Overstreet 14940a63b66dSKent Overstreet /* recheck reserve after allocating replacement node */ 14950a63b66dSKent Overstreet if (btree_check_reserve(b, NULL)) { 14960a63b66dSKent Overstreet btree_node_free(n); 14970a63b66dSKent Overstreet rw_unlock(true, n); 14980a63b66dSKent Overstreet return 0; 14990a63b66dSKent Overstreet } 15000a63b66dSKent Overstreet 15010a63b66dSKent Overstreet bch_btree_node_write_sync(n); 15020a63b66dSKent Overstreet 15030a63b66dSKent Overstreet bch_keylist_init(&keys); 15040a63b66dSKent Overstreet bch_keylist_add(&keys, &n->key); 15050a63b66dSKent Overstreet 15060a63b66dSKent Overstreet make_btree_freeing_key(replace, keys.top); 15070a63b66dSKent Overstreet bch_keylist_push(&keys); 15080a63b66dSKent Overstreet 15090a63b66dSKent Overstreet bch_btree_insert_node(b, op, &keys, NULL, NULL); 15100a63b66dSKent Overstreet BUG_ON(!bch_keylist_empty(&keys)); 15110a63b66dSKent Overstreet 15120a63b66dSKent Overstreet btree_node_free(replace); 15130a63b66dSKent Overstreet rw_unlock(true, n); 15140a63b66dSKent Overstreet 15150a63b66dSKent Overstreet /* Invalidated our iterator */ 15160a63b66dSKent Overstreet return -EINTR; 15170a63b66dSKent Overstreet } 15180a63b66dSKent Overstreet 15196f10f7d1SColy Li static unsigned int btree_gc_count_keys(struct btree *b) 1520a1f0358bSKent Overstreet { 1521a1f0358bSKent Overstreet struct bkey *k; 1522a1f0358bSKent Overstreet struct btree_iter iter; 15236f10f7d1SColy Li unsigned int ret = 0; 1524a1f0358bSKent Overstreet 1525c052dd9aSKent Overstreet for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad) 1526a1f0358bSKent Overstreet ret += bkey_u64s(k); 1527a1f0358bSKent Overstreet 1528a1f0358bSKent Overstreet return ret; 1529cafe5635SKent Overstreet } 1530cafe5635SKent Overstreet 15317f4a59deSTang Junhui static size_t btree_gc_min_nodes(struct cache_set *c) 15327f4a59deSTang Junhui { 15337f4a59deSTang Junhui size_t min_nodes; 15347f4a59deSTang Junhui 15357f4a59deSTang Junhui /* 15367f4a59deSTang Junhui * Since incremental GC would stop 100ms when front 15377f4a59deSTang Junhui * side I/O comes, so when there are many btree nodes, 15387f4a59deSTang Junhui * if GC only processes constant (100) nodes each time, 15397f4a59deSTang Junhui * GC would last a long time, and the front side I/Os 15407f4a59deSTang Junhui * would run out of the buckets (since no new bucket 15417f4a59deSTang Junhui * can be allocated during GC), and be blocked again. 15427f4a59deSTang Junhui * So GC should not process constant nodes, but varied 15437f4a59deSTang Junhui * nodes according to the number of btree nodes, which 15447f4a59deSTang Junhui * realized by dividing GC into constant(100) times, 15457f4a59deSTang Junhui * so when there are many btree nodes, GC can process 15467f4a59deSTang Junhui * more nodes each time, otherwise, GC will process less 15477f4a59deSTang Junhui * nodes each time (but no less than MIN_GC_NODES) 15487f4a59deSTang Junhui */ 15497f4a59deSTang Junhui min_nodes = c->gc_stats.nodes / MAX_GC_TIMES; 15507f4a59deSTang Junhui if (min_nodes < MIN_GC_NODES) 15517f4a59deSTang Junhui min_nodes = MIN_GC_NODES; 15527f4a59deSTang Junhui 15537f4a59deSTang Junhui return min_nodes; 15547f4a59deSTang Junhui } 15557f4a59deSTang Junhui 15567f4a59deSTang Junhui 1557cafe5635SKent Overstreet static int btree_gc_recurse(struct btree *b, struct btree_op *op, 1558cafe5635SKent Overstreet struct closure *writes, struct gc_stat *gc) 1559cafe5635SKent Overstreet { 1560a1f0358bSKent Overstreet int ret = 0; 1561a1f0358bSKent Overstreet bool should_rewrite; 1562a1f0358bSKent Overstreet struct bkey *k; 1563a1f0358bSKent Overstreet struct btree_iter iter; 1564cafe5635SKent Overstreet struct gc_merge_info r[GC_MERGE_NODES]; 15652a285686SKent Overstreet struct gc_merge_info *i, *last = r + ARRAY_SIZE(r) - 1; 1566cafe5635SKent Overstreet 1567c052dd9aSKent Overstreet bch_btree_iter_init(&b->keys, &iter, &b->c->gc_done); 1568cafe5635SKent Overstreet 15692a285686SKent Overstreet for (i = r; i < r + ARRAY_SIZE(r); i++) 15702a285686SKent Overstreet i->b = ERR_PTR(-EINTR); 1571cafe5635SKent Overstreet 1572a1f0358bSKent Overstreet while (1) { 1573a85e968eSKent Overstreet k = bch_btree_iter_next_filter(&iter, &b->keys, bch_ptr_bad); 1574a1f0358bSKent Overstreet if (k) { 15750a63b66dSKent Overstreet r->b = bch_btree_node_get(b->c, op, k, b->level - 1, 15762452cc89SSlava Pestov true, b); 1577cafe5635SKent Overstreet if (IS_ERR(r->b)) { 1578cafe5635SKent Overstreet ret = PTR_ERR(r->b); 1579cafe5635SKent Overstreet break; 1580cafe5635SKent Overstreet } 1581cafe5635SKent Overstreet 1582a1f0358bSKent Overstreet r->keys = btree_gc_count_keys(r->b); 1583cafe5635SKent Overstreet 15840a63b66dSKent Overstreet ret = btree_gc_coalesce(b, op, gc, r); 1585a1f0358bSKent Overstreet if (ret) 1586cafe5635SKent Overstreet break; 1587cafe5635SKent Overstreet } 1588cafe5635SKent Overstreet 1589a1f0358bSKent Overstreet if (!last->b) 1590a1f0358bSKent Overstreet break; 1591cafe5635SKent Overstreet 1592a1f0358bSKent Overstreet if (!IS_ERR(last->b)) { 1593a1f0358bSKent Overstreet should_rewrite = btree_gc_mark_node(last->b, gc); 15940a63b66dSKent Overstreet if (should_rewrite) { 15950a63b66dSKent Overstreet ret = btree_gc_rewrite_node(b, op, last->b); 15960a63b66dSKent Overstreet if (ret) 1597a1f0358bSKent Overstreet break; 1598a1f0358bSKent Overstreet } 1599a1f0358bSKent Overstreet 1600a1f0358bSKent Overstreet if (last->b->level) { 1601a1f0358bSKent Overstreet ret = btree_gc_recurse(last->b, op, writes, gc); 1602a1f0358bSKent Overstreet if (ret) 1603a1f0358bSKent Overstreet break; 1604a1f0358bSKent Overstreet } 1605a1f0358bSKent Overstreet 1606a1f0358bSKent Overstreet bkey_copy_key(&b->c->gc_done, &last->b->key); 1607a1f0358bSKent Overstreet 1608a1f0358bSKent Overstreet /* 1609a1f0358bSKent Overstreet * Must flush leaf nodes before gc ends, since replace 1610a1f0358bSKent Overstreet * operations aren't journalled 1611cafe5635SKent Overstreet */ 16122a285686SKent Overstreet mutex_lock(&last->b->write_lock); 1613a1f0358bSKent Overstreet if (btree_node_dirty(last->b)) 1614a1f0358bSKent Overstreet bch_btree_node_write(last->b, writes); 16152a285686SKent Overstreet mutex_unlock(&last->b->write_lock); 1616a1f0358bSKent Overstreet rw_unlock(true, last->b); 1617a1f0358bSKent Overstreet } 1618a1f0358bSKent Overstreet 1619a1f0358bSKent Overstreet memmove(r + 1, r, sizeof(r[0]) * (GC_MERGE_NODES - 1)); 1620a1f0358bSKent Overstreet r->b = NULL; 1621a1f0358bSKent Overstreet 16225c25c4fcSTang Junhui if (atomic_read(&b->c->search_inflight) && 16237f4a59deSTang Junhui gc->nodes >= gc->nodes_pre + btree_gc_min_nodes(b->c)) { 16245c25c4fcSTang Junhui gc->nodes_pre = gc->nodes; 16255c25c4fcSTang Junhui ret = -EAGAIN; 16265c25c4fcSTang Junhui break; 16275c25c4fcSTang Junhui } 16285c25c4fcSTang Junhui 1629cafe5635SKent Overstreet if (need_resched()) { 1630cafe5635SKent Overstreet ret = -EAGAIN; 1631cafe5635SKent Overstreet break; 1632cafe5635SKent Overstreet } 1633cafe5635SKent Overstreet } 1634cafe5635SKent Overstreet 16352a285686SKent Overstreet for (i = r; i < r + ARRAY_SIZE(r); i++) 16362a285686SKent Overstreet if (!IS_ERR_OR_NULL(i->b)) { 16372a285686SKent Overstreet mutex_lock(&i->b->write_lock); 16382a285686SKent Overstreet if (btree_node_dirty(i->b)) 16392a285686SKent Overstreet bch_btree_node_write(i->b, writes); 16402a285686SKent Overstreet mutex_unlock(&i->b->write_lock); 16412a285686SKent Overstreet rw_unlock(true, i->b); 1642a1f0358bSKent Overstreet } 1643cafe5635SKent Overstreet 1644cafe5635SKent Overstreet return ret; 1645cafe5635SKent Overstreet } 1646cafe5635SKent Overstreet 1647cafe5635SKent Overstreet static int bch_btree_gc_root(struct btree *b, struct btree_op *op, 1648cafe5635SKent Overstreet struct closure *writes, struct gc_stat *gc) 1649cafe5635SKent Overstreet { 1650cafe5635SKent Overstreet struct btree *n = NULL; 1651a1f0358bSKent Overstreet int ret = 0; 1652a1f0358bSKent Overstreet bool should_rewrite; 1653cafe5635SKent Overstreet 1654a1f0358bSKent Overstreet should_rewrite = btree_gc_mark_node(b, gc); 1655a1f0358bSKent Overstreet if (should_rewrite) { 16560a63b66dSKent Overstreet n = btree_node_alloc_replacement(b, NULL); 1657cafe5635SKent Overstreet 1658cafe5635SKent Overstreet if (!IS_ERR_OR_NULL(n)) { 1659a1f0358bSKent Overstreet bch_btree_node_write_sync(n); 16602a285686SKent Overstreet 1661a1f0358bSKent Overstreet bch_btree_set_root(n); 1662a1f0358bSKent Overstreet btree_node_free(b); 1663a1f0358bSKent Overstreet rw_unlock(true, n); 1664a1f0358bSKent Overstreet 1665a1f0358bSKent Overstreet return -EINTR; 1666cafe5635SKent Overstreet } 1667a1f0358bSKent Overstreet } 1668a1f0358bSKent Overstreet 1669487dded8SKent Overstreet __bch_btree_mark_key(b->c, b->level + 1, &b->key); 1670487dded8SKent Overstreet 1671a1f0358bSKent Overstreet if (b->level) { 1672a1f0358bSKent Overstreet ret = btree_gc_recurse(b, op, writes, gc); 1673a1f0358bSKent Overstreet if (ret) 1674a1f0358bSKent Overstreet return ret; 1675a1f0358bSKent Overstreet } 1676a1f0358bSKent Overstreet 1677a1f0358bSKent Overstreet bkey_copy_key(&b->c->gc_done, &b->key); 1678cafe5635SKent Overstreet 1679cafe5635SKent Overstreet return ret; 1680cafe5635SKent Overstreet } 1681cafe5635SKent Overstreet 1682cafe5635SKent Overstreet static void btree_gc_start(struct cache_set *c) 1683cafe5635SKent Overstreet { 1684cafe5635SKent Overstreet struct cache *ca; 1685cafe5635SKent Overstreet struct bucket *b; 16866f10f7d1SColy Li unsigned int i; 1687cafe5635SKent Overstreet 1688cafe5635SKent Overstreet if (!c->gc_mark_valid) 1689cafe5635SKent Overstreet return; 1690cafe5635SKent Overstreet 1691cafe5635SKent Overstreet mutex_lock(&c->bucket_lock); 1692cafe5635SKent Overstreet 1693cafe5635SKent Overstreet c->gc_mark_valid = 0; 1694cafe5635SKent Overstreet c->gc_done = ZERO_KEY; 1695cafe5635SKent Overstreet 1696cafe5635SKent Overstreet for_each_cache(ca, c, i) 1697cafe5635SKent Overstreet for_each_bucket(b, ca) { 16983a2fd9d5SKent Overstreet b->last_gc = b->gen; 169929ebf465SKent Overstreet if (!atomic_read(&b->pin)) { 17004fe6a816SKent Overstreet SET_GC_MARK(b, 0); 170129ebf465SKent Overstreet SET_GC_SECTORS_USED(b, 0); 170229ebf465SKent Overstreet } 1703cafe5635SKent Overstreet } 1704cafe5635SKent Overstreet 1705cafe5635SKent Overstreet mutex_unlock(&c->bucket_lock); 1706cafe5635SKent Overstreet } 1707cafe5635SKent Overstreet 1708d44c2f9eSTang Junhui static void bch_btree_gc_finish(struct cache_set *c) 1709cafe5635SKent Overstreet { 1710cafe5635SKent Overstreet struct bucket *b; 1711cafe5635SKent Overstreet struct cache *ca; 17126f10f7d1SColy Li unsigned int i; 1713cafe5635SKent Overstreet 1714cafe5635SKent Overstreet mutex_lock(&c->bucket_lock); 1715cafe5635SKent Overstreet 1716cafe5635SKent Overstreet set_gc_sectors(c); 1717cafe5635SKent Overstreet c->gc_mark_valid = 1; 1718cafe5635SKent Overstreet c->need_gc = 0; 1719cafe5635SKent Overstreet 1720cafe5635SKent Overstreet for (i = 0; i < KEY_PTRS(&c->uuid_bucket); i++) 1721cafe5635SKent Overstreet SET_GC_MARK(PTR_BUCKET(c, &c->uuid_bucket, i), 1722cafe5635SKent Overstreet GC_MARK_METADATA); 1723cafe5635SKent Overstreet 1724bf0a628aSNicholas Swenson /* don't reclaim buckets to which writeback keys point */ 1725bf0a628aSNicholas Swenson rcu_read_lock(); 17262831231dSColy Li for (i = 0; i < c->devices_max_used; i++) { 1727bf0a628aSNicholas Swenson struct bcache_device *d = c->devices[i]; 1728bf0a628aSNicholas Swenson struct cached_dev *dc; 1729bf0a628aSNicholas Swenson struct keybuf_key *w, *n; 17306f10f7d1SColy Li unsigned int j; 1731bf0a628aSNicholas Swenson 1732bf0a628aSNicholas Swenson if (!d || UUID_FLASH_ONLY(&c->uuids[i])) 1733bf0a628aSNicholas Swenson continue; 1734bf0a628aSNicholas Swenson dc = container_of(d, struct cached_dev, disk); 1735bf0a628aSNicholas Swenson 1736bf0a628aSNicholas Swenson spin_lock(&dc->writeback_keys.lock); 1737bf0a628aSNicholas Swenson rbtree_postorder_for_each_entry_safe(w, n, 1738bf0a628aSNicholas Swenson &dc->writeback_keys.keys, node) 1739bf0a628aSNicholas Swenson for (j = 0; j < KEY_PTRS(&w->key); j++) 1740bf0a628aSNicholas Swenson SET_GC_MARK(PTR_BUCKET(c, &w->key, j), 1741bf0a628aSNicholas Swenson GC_MARK_DIRTY); 1742bf0a628aSNicholas Swenson spin_unlock(&dc->writeback_keys.lock); 1743bf0a628aSNicholas Swenson } 1744bf0a628aSNicholas Swenson rcu_read_unlock(); 1745bf0a628aSNicholas Swenson 1746d44c2f9eSTang Junhui c->avail_nbuckets = 0; 1747cafe5635SKent Overstreet for_each_cache(ca, c, i) { 1748cafe5635SKent Overstreet uint64_t *i; 1749cafe5635SKent Overstreet 1750cafe5635SKent Overstreet ca->invalidate_needs_gc = 0; 1751cafe5635SKent Overstreet 1752cafe5635SKent Overstreet for (i = ca->sb.d; i < ca->sb.d + ca->sb.keys; i++) 1753cafe5635SKent Overstreet SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA); 1754cafe5635SKent Overstreet 1755cafe5635SKent Overstreet for (i = ca->prio_buckets; 1756cafe5635SKent Overstreet i < ca->prio_buckets + prio_buckets(ca) * 2; i++) 1757cafe5635SKent Overstreet SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA); 1758cafe5635SKent Overstreet 1759cafe5635SKent Overstreet for_each_bucket(b, ca) { 1760cafe5635SKent Overstreet c->need_gc = max(c->need_gc, bucket_gc_gen(b)); 1761cafe5635SKent Overstreet 17624fe6a816SKent Overstreet if (atomic_read(&b->pin)) 17634fe6a816SKent Overstreet continue; 17644fe6a816SKent Overstreet 17654fe6a816SKent Overstreet BUG_ON(!GC_MARK(b) && GC_SECTORS_USED(b)); 17664fe6a816SKent Overstreet 17674fe6a816SKent Overstreet if (!GC_MARK(b) || GC_MARK(b) == GC_MARK_RECLAIMABLE) 1768d44c2f9eSTang Junhui c->avail_nbuckets++; 1769cafe5635SKent Overstreet } 1770cafe5635SKent Overstreet } 1771cafe5635SKent Overstreet 1772cafe5635SKent Overstreet mutex_unlock(&c->bucket_lock); 1773cafe5635SKent Overstreet } 1774cafe5635SKent Overstreet 177572a44517SKent Overstreet static void bch_btree_gc(struct cache_set *c) 1776cafe5635SKent Overstreet { 1777cafe5635SKent Overstreet int ret; 1778cafe5635SKent Overstreet struct gc_stat stats; 1779cafe5635SKent Overstreet struct closure writes; 1780cafe5635SKent Overstreet struct btree_op op; 1781cafe5635SKent Overstreet uint64_t start_time = local_clock(); 178257943511SKent Overstreet 1783c37511b8SKent Overstreet trace_bcache_gc_start(c); 1784cafe5635SKent Overstreet 1785cafe5635SKent Overstreet memset(&stats, 0, sizeof(struct gc_stat)); 1786cafe5635SKent Overstreet closure_init_stack(&writes); 1787b54d6934SKent Overstreet bch_btree_op_init(&op, SHRT_MAX); 1788cafe5635SKent Overstreet 1789cafe5635SKent Overstreet btree_gc_start(c); 1790cafe5635SKent Overstreet 1791771f393eSColy Li /* if CACHE_SET_IO_DISABLE set, gc thread should stop too */ 1792a1f0358bSKent Overstreet do { 1793*feac1a70SColy Li ret = bcache_btree_root(gc_root, c, &op, &writes, &stats); 1794cafe5635SKent Overstreet closure_sync(&writes); 1795c5f1e5adSKent Overstreet cond_resched(); 1796cafe5635SKent Overstreet 17975c25c4fcSTang Junhui if (ret == -EAGAIN) 17985c25c4fcSTang Junhui schedule_timeout_interruptible(msecs_to_jiffies 17995c25c4fcSTang Junhui (GC_SLEEP_MS)); 18005c25c4fcSTang Junhui else if (ret) 1801cafe5635SKent Overstreet pr_warn("gc failed!"); 1802771f393eSColy Li } while (ret && !test_bit(CACHE_SET_IO_DISABLE, &c->flags)); 1803cafe5635SKent Overstreet 1804d44c2f9eSTang Junhui bch_btree_gc_finish(c); 180557943511SKent Overstreet wake_up_allocators(c); 180657943511SKent Overstreet 1807169ef1cfSKent Overstreet bch_time_stats_update(&c->btree_gc_time, start_time); 1808cafe5635SKent Overstreet 1809cafe5635SKent Overstreet stats.key_bytes *= sizeof(uint64_t); 1810cafe5635SKent Overstreet stats.data <<= 9; 1811d44c2f9eSTang Junhui bch_update_bucket_in_use(c, &stats); 1812cafe5635SKent Overstreet memcpy(&c->gc_stats, &stats, sizeof(struct gc_stat)); 1813cafe5635SKent Overstreet 1814c37511b8SKent Overstreet trace_bcache_gc_end(c); 1815cafe5635SKent Overstreet 181672a44517SKent Overstreet bch_moving_gc(c); 1817cafe5635SKent Overstreet } 1818cafe5635SKent Overstreet 1819be628be0SKent Overstreet static bool gc_should_run(struct cache_set *c) 1820cafe5635SKent Overstreet { 1821a1f0358bSKent Overstreet struct cache *ca; 18226f10f7d1SColy Li unsigned int i; 182372a44517SKent Overstreet 1824be628be0SKent Overstreet for_each_cache(ca, c, i) 1825be628be0SKent Overstreet if (ca->invalidate_needs_gc) 1826be628be0SKent Overstreet return true; 182772a44517SKent Overstreet 1828be628be0SKent Overstreet if (atomic_read(&c->sectors_to_gc) < 0) 1829be628be0SKent Overstreet return true; 1830be628be0SKent Overstreet 1831be628be0SKent Overstreet return false; 1832be628be0SKent Overstreet } 1833be628be0SKent Overstreet 1834be628be0SKent Overstreet static int bch_gc_thread(void *arg) 1835be628be0SKent Overstreet { 1836be628be0SKent Overstreet struct cache_set *c = arg; 1837be628be0SKent Overstreet 1838be628be0SKent Overstreet while (1) { 1839be628be0SKent Overstreet wait_event_interruptible(c->gc_wait, 1840771f393eSColy Li kthread_should_stop() || 1841771f393eSColy Li test_bit(CACHE_SET_IO_DISABLE, &c->flags) || 1842771f393eSColy Li gc_should_run(c)); 1843be628be0SKent Overstreet 1844771f393eSColy Li if (kthread_should_stop() || 1845771f393eSColy Li test_bit(CACHE_SET_IO_DISABLE, &c->flags)) 184672a44517SKent Overstreet break; 184772a44517SKent Overstreet 1848be628be0SKent Overstreet set_gc_sectors(c); 1849be628be0SKent Overstreet bch_btree_gc(c); 185072a44517SKent Overstreet } 185172a44517SKent Overstreet 1852771f393eSColy Li wait_for_kthread_stop(); 185372a44517SKent Overstreet return 0; 185472a44517SKent Overstreet } 185572a44517SKent Overstreet 185672a44517SKent Overstreet int bch_gc_thread_start(struct cache_set *c) 185772a44517SKent Overstreet { 1858be628be0SKent Overstreet c->gc_thread = kthread_run(bch_gc_thread, c, "bcache_gc"); 18599d134117SVasyl Gomonovych return PTR_ERR_OR_ZERO(c->gc_thread); 1860cafe5635SKent Overstreet } 1861cafe5635SKent Overstreet 1862cafe5635SKent Overstreet /* Initial partial gc */ 1863cafe5635SKent Overstreet 1864487dded8SKent Overstreet static int bch_btree_check_recurse(struct btree *b, struct btree_op *op) 1865cafe5635SKent Overstreet { 186650310164SKent Overstreet int ret = 0; 186750310164SKent Overstreet struct bkey *k, *p = NULL; 1868cafe5635SKent Overstreet struct btree_iter iter; 1869cafe5635SKent Overstreet 1870487dded8SKent Overstreet for_each_key_filter(&b->keys, k, &iter, bch_ptr_invalid) 1871487dded8SKent Overstreet bch_initial_mark_key(b->c, b->level, k); 1872cafe5635SKent Overstreet 1873487dded8SKent Overstreet bch_initial_mark_key(b->c, b->level + 1, &b->key); 1874cafe5635SKent Overstreet 1875cafe5635SKent Overstreet if (b->level) { 1876c052dd9aSKent Overstreet bch_btree_iter_init(&b->keys, &iter, NULL); 1877cafe5635SKent Overstreet 187850310164SKent Overstreet do { 1879a85e968eSKent Overstreet k = bch_btree_iter_next_filter(&iter, &b->keys, 1880a85e968eSKent Overstreet bch_ptr_bad); 18817f4a59deSTang Junhui if (k) { 18822452cc89SSlava Pestov btree_node_prefetch(b, k); 18837f4a59deSTang Junhui /* 18847f4a59deSTang Junhui * initiallize c->gc_stats.nodes 18857f4a59deSTang Junhui * for incremental GC 18867f4a59deSTang Junhui */ 18877f4a59deSTang Junhui b->c->gc_stats.nodes++; 18887f4a59deSTang Junhui } 188950310164SKent Overstreet 1890cafe5635SKent Overstreet if (p) 1891*feac1a70SColy Li ret = bcache_btree(check_recurse, p, b, op); 1892cafe5635SKent Overstreet 189350310164SKent Overstreet p = k; 189450310164SKent Overstreet } while (p && !ret); 1895cafe5635SKent Overstreet } 1896cafe5635SKent Overstreet 1897487dded8SKent Overstreet return ret; 1898cafe5635SKent Overstreet } 1899cafe5635SKent Overstreet 1900c18536a7SKent Overstreet int bch_btree_check(struct cache_set *c) 1901cafe5635SKent Overstreet { 1902c18536a7SKent Overstreet struct btree_op op; 1903cafe5635SKent Overstreet 1904b54d6934SKent Overstreet bch_btree_op_init(&op, SHRT_MAX); 1905cafe5635SKent Overstreet 1906*feac1a70SColy Li return bcache_btree_root(check_recurse, c, &op); 1907cafe5635SKent Overstreet } 1908cafe5635SKent Overstreet 19092531d9eeSKent Overstreet void bch_initial_gc_finish(struct cache_set *c) 19102531d9eeSKent Overstreet { 19112531d9eeSKent Overstreet struct cache *ca; 19122531d9eeSKent Overstreet struct bucket *b; 19136f10f7d1SColy Li unsigned int i; 19142531d9eeSKent Overstreet 19152531d9eeSKent Overstreet bch_btree_gc_finish(c); 19162531d9eeSKent Overstreet 19172531d9eeSKent Overstreet mutex_lock(&c->bucket_lock); 19182531d9eeSKent Overstreet 19192531d9eeSKent Overstreet /* 19202531d9eeSKent Overstreet * We need to put some unused buckets directly on the prio freelist in 19212531d9eeSKent Overstreet * order to get the allocator thread started - it needs freed buckets in 19222531d9eeSKent Overstreet * order to rewrite the prios and gens, and it needs to rewrite prios 19232531d9eeSKent Overstreet * and gens in order to free buckets. 19242531d9eeSKent Overstreet * 19252531d9eeSKent Overstreet * This is only safe for buckets that have no live data in them, which 19262531d9eeSKent Overstreet * there should always be some of. 19272531d9eeSKent Overstreet */ 19282531d9eeSKent Overstreet for_each_cache(ca, c, i) { 19292531d9eeSKent Overstreet for_each_bucket(b, ca) { 1930682811b3STang Junhui if (fifo_full(&ca->free[RESERVE_PRIO]) && 1931682811b3STang Junhui fifo_full(&ca->free[RESERVE_BTREE])) 19322531d9eeSKent Overstreet break; 19332531d9eeSKent Overstreet 19342531d9eeSKent Overstreet if (bch_can_invalidate_bucket(ca, b) && 19352531d9eeSKent Overstreet !GC_MARK(b)) { 19362531d9eeSKent Overstreet __bch_invalidate_one_bucket(ca, b); 1937682811b3STang Junhui if (!fifo_push(&ca->free[RESERVE_PRIO], 1938682811b3STang Junhui b - ca->buckets)) 1939682811b3STang Junhui fifo_push(&ca->free[RESERVE_BTREE], 19402531d9eeSKent Overstreet b - ca->buckets); 19412531d9eeSKent Overstreet } 19422531d9eeSKent Overstreet } 19432531d9eeSKent Overstreet } 19442531d9eeSKent Overstreet 19452531d9eeSKent Overstreet mutex_unlock(&c->bucket_lock); 19462531d9eeSKent Overstreet } 19472531d9eeSKent Overstreet 1948cafe5635SKent Overstreet /* Btree insertion */ 1949cafe5635SKent Overstreet 1950829a60b9SKent Overstreet static bool btree_insert_key(struct btree *b, struct bkey *k, 19511b207d80SKent Overstreet struct bkey *replace_key) 1952cafe5635SKent Overstreet { 19536f10f7d1SColy Li unsigned int status; 1954cafe5635SKent Overstreet 1955cafe5635SKent Overstreet BUG_ON(bkey_cmp(k, &b->key) > 0); 1956cafe5635SKent Overstreet 1957829a60b9SKent Overstreet status = bch_btree_insert_key(&b->keys, k, replace_key); 1958829a60b9SKent Overstreet if (status != BTREE_INSERT_STATUS_NO_INSERT) { 1959dc9d98d6SKent Overstreet bch_check_keys(&b->keys, "%u for %s", status, 19601b207d80SKent Overstreet replace_key ? "replace" : "insert"); 1961cafe5635SKent Overstreet 1962829a60b9SKent Overstreet trace_bcache_btree_insert_key(b, k, replace_key != NULL, 1963829a60b9SKent Overstreet status); 1964cafe5635SKent Overstreet return true; 1965829a60b9SKent Overstreet } else 1966829a60b9SKent Overstreet return false; 1967cafe5635SKent Overstreet } 1968cafe5635SKent Overstreet 196959158fdeSKent Overstreet static size_t insert_u64s_remaining(struct btree *b) 197059158fdeSKent Overstreet { 19713572324aSKent Overstreet long ret = bch_btree_keys_u64s_remaining(&b->keys); 197259158fdeSKent Overstreet 197359158fdeSKent Overstreet /* 197459158fdeSKent Overstreet * Might land in the middle of an existing extent and have to split it 197559158fdeSKent Overstreet */ 197659158fdeSKent Overstreet if (b->keys.ops->is_extents) 197759158fdeSKent Overstreet ret -= KEY_MAX_U64S; 197859158fdeSKent Overstreet 197959158fdeSKent Overstreet return max(ret, 0L); 198059158fdeSKent Overstreet } 198159158fdeSKent Overstreet 198226c949f8SKent Overstreet static bool bch_btree_insert_keys(struct btree *b, struct btree_op *op, 19831b207d80SKent Overstreet struct keylist *insert_keys, 19841b207d80SKent Overstreet struct bkey *replace_key) 1985cafe5635SKent Overstreet { 1986cafe5635SKent Overstreet bool ret = false; 1987dc9d98d6SKent Overstreet int oldsize = bch_count_data(&b->keys); 1988cafe5635SKent Overstreet 198926c949f8SKent Overstreet while (!bch_keylist_empty(insert_keys)) { 1990c2f95ae2SKent Overstreet struct bkey *k = insert_keys->keys; 199126c949f8SKent Overstreet 199259158fdeSKent Overstreet if (bkey_u64s(k) > insert_u64s_remaining(b)) 1993403b6cdeSKent Overstreet break; 1994403b6cdeSKent Overstreet 1995403b6cdeSKent Overstreet if (bkey_cmp(k, &b->key) <= 0) { 19963a3b6a4eSKent Overstreet if (!b->level) 19973a3b6a4eSKent Overstreet bkey_put(b->c, k); 199826c949f8SKent Overstreet 1999829a60b9SKent Overstreet ret |= btree_insert_key(b, k, replace_key); 200026c949f8SKent Overstreet bch_keylist_pop_front(insert_keys); 200126c949f8SKent Overstreet } else if (bkey_cmp(&START_KEY(k), &b->key) < 0) { 200226c949f8SKent Overstreet BKEY_PADDED(key) temp; 2003c2f95ae2SKent Overstreet bkey_copy(&temp.key, insert_keys->keys); 200426c949f8SKent Overstreet 200526c949f8SKent Overstreet bch_cut_back(&b->key, &temp.key); 2006c2f95ae2SKent Overstreet bch_cut_front(&b->key, insert_keys->keys); 200726c949f8SKent Overstreet 2008829a60b9SKent Overstreet ret |= btree_insert_key(b, &temp.key, replace_key); 200926c949f8SKent Overstreet break; 201026c949f8SKent Overstreet } else { 201126c949f8SKent Overstreet break; 201226c949f8SKent Overstreet } 2013cafe5635SKent Overstreet } 2014cafe5635SKent Overstreet 2015829a60b9SKent Overstreet if (!ret) 2016829a60b9SKent Overstreet op->insert_collision = true; 2017829a60b9SKent Overstreet 2018403b6cdeSKent Overstreet BUG_ON(!bch_keylist_empty(insert_keys) && b->level); 2019403b6cdeSKent Overstreet 2020dc9d98d6SKent Overstreet BUG_ON(bch_count_data(&b->keys) < oldsize); 2021cafe5635SKent Overstreet return ret; 2022cafe5635SKent Overstreet } 2023cafe5635SKent Overstreet 202426c949f8SKent Overstreet static int btree_split(struct btree *b, struct btree_op *op, 202526c949f8SKent Overstreet struct keylist *insert_keys, 20261b207d80SKent Overstreet struct bkey *replace_key) 2027cafe5635SKent Overstreet { 2028d6fd3b11SKent Overstreet bool split; 2029cafe5635SKent Overstreet struct btree *n1, *n2 = NULL, *n3 = NULL; 2030cafe5635SKent Overstreet uint64_t start_time = local_clock(); 2031b54d6934SKent Overstreet struct closure cl; 203217e21a9fSKent Overstreet struct keylist parent_keys; 2033b54d6934SKent Overstreet 2034b54d6934SKent Overstreet closure_init_stack(&cl); 203517e21a9fSKent Overstreet bch_keylist_init(&parent_keys); 2036cafe5635SKent Overstreet 20370a63b66dSKent Overstreet if (btree_check_reserve(b, op)) { 20380a63b66dSKent Overstreet if (!b->level) 203978365411SKent Overstreet return -EINTR; 20400a63b66dSKent Overstreet else 20410a63b66dSKent Overstreet WARN(1, "insufficient reserve for split\n"); 20420a63b66dSKent Overstreet } 204378365411SKent Overstreet 20440a63b66dSKent Overstreet n1 = btree_node_alloc_replacement(b, op); 2045cafe5635SKent Overstreet if (IS_ERR(n1)) 2046cafe5635SKent Overstreet goto err; 2047cafe5635SKent Overstreet 2048ee811287SKent Overstreet split = set_blocks(btree_bset_first(n1), 2049ee811287SKent Overstreet block_bytes(n1->c)) > (btree_blocks(b) * 4) / 5; 2050cafe5635SKent Overstreet 2051cafe5635SKent Overstreet if (split) { 20526f10f7d1SColy Li unsigned int keys = 0; 2053cafe5635SKent Overstreet 2054ee811287SKent Overstreet trace_bcache_btree_node_split(b, btree_bset_first(n1)->keys); 2055c37511b8SKent Overstreet 20562452cc89SSlava Pestov n2 = bch_btree_node_alloc(b->c, op, b->level, b->parent); 2057cafe5635SKent Overstreet if (IS_ERR(n2)) 2058cafe5635SKent Overstreet goto err_free1; 2059cafe5635SKent Overstreet 2060d6fd3b11SKent Overstreet if (!b->parent) { 20612452cc89SSlava Pestov n3 = bch_btree_node_alloc(b->c, op, b->level + 1, NULL); 2062cafe5635SKent Overstreet if (IS_ERR(n3)) 2063cafe5635SKent Overstreet goto err_free2; 2064cafe5635SKent Overstreet } 2065cafe5635SKent Overstreet 20662a285686SKent Overstreet mutex_lock(&n1->write_lock); 20672a285686SKent Overstreet mutex_lock(&n2->write_lock); 20682a285686SKent Overstreet 20691b207d80SKent Overstreet bch_btree_insert_keys(n1, op, insert_keys, replace_key); 2070cafe5635SKent Overstreet 2071d6fd3b11SKent Overstreet /* 2072d6fd3b11SKent Overstreet * Has to be a linear search because we don't have an auxiliary 2073cafe5635SKent Overstreet * search tree yet 2074cafe5635SKent Overstreet */ 2075cafe5635SKent Overstreet 2076ee811287SKent Overstreet while (keys < (btree_bset_first(n1)->keys * 3) / 5) 2077ee811287SKent Overstreet keys += bkey_u64s(bset_bkey_idx(btree_bset_first(n1), 2078fafff81cSKent Overstreet keys)); 2079cafe5635SKent Overstreet 2080fafff81cSKent Overstreet bkey_copy_key(&n1->key, 2081ee811287SKent Overstreet bset_bkey_idx(btree_bset_first(n1), keys)); 2082ee811287SKent Overstreet keys += bkey_u64s(bset_bkey_idx(btree_bset_first(n1), keys)); 2083cafe5635SKent Overstreet 2084ee811287SKent Overstreet btree_bset_first(n2)->keys = btree_bset_first(n1)->keys - keys; 2085ee811287SKent Overstreet btree_bset_first(n1)->keys = keys; 2086cafe5635SKent Overstreet 2087ee811287SKent Overstreet memcpy(btree_bset_first(n2)->start, 2088ee811287SKent Overstreet bset_bkey_last(btree_bset_first(n1)), 2089ee811287SKent Overstreet btree_bset_first(n2)->keys * sizeof(uint64_t)); 2090cafe5635SKent Overstreet 2091cafe5635SKent Overstreet bkey_copy_key(&n2->key, &b->key); 2092cafe5635SKent Overstreet 209317e21a9fSKent Overstreet bch_keylist_add(&parent_keys, &n2->key); 2094b54d6934SKent Overstreet bch_btree_node_write(n2, &cl); 20952a285686SKent Overstreet mutex_unlock(&n2->write_lock); 2096cafe5635SKent Overstreet rw_unlock(true, n2); 2097c37511b8SKent Overstreet } else { 2098ee811287SKent Overstreet trace_bcache_btree_node_compact(b, btree_bset_first(n1)->keys); 2099c37511b8SKent Overstreet 21002a285686SKent Overstreet mutex_lock(&n1->write_lock); 21011b207d80SKent Overstreet bch_btree_insert_keys(n1, op, insert_keys, replace_key); 2102c37511b8SKent Overstreet } 2103cafe5635SKent Overstreet 210417e21a9fSKent Overstreet bch_keylist_add(&parent_keys, &n1->key); 2105b54d6934SKent Overstreet bch_btree_node_write(n1, &cl); 21062a285686SKent Overstreet mutex_unlock(&n1->write_lock); 2107cafe5635SKent Overstreet 2108cafe5635SKent Overstreet if (n3) { 2109d6fd3b11SKent Overstreet /* Depth increases, make a new root */ 21102a285686SKent Overstreet mutex_lock(&n3->write_lock); 2111cafe5635SKent Overstreet bkey_copy_key(&n3->key, &MAX_KEY); 211217e21a9fSKent Overstreet bch_btree_insert_keys(n3, op, &parent_keys, NULL); 2113b54d6934SKent Overstreet bch_btree_node_write(n3, &cl); 21142a285686SKent Overstreet mutex_unlock(&n3->write_lock); 2115cafe5635SKent Overstreet 2116b54d6934SKent Overstreet closure_sync(&cl); 2117cafe5635SKent Overstreet bch_btree_set_root(n3); 2118cafe5635SKent Overstreet rw_unlock(true, n3); 2119d6fd3b11SKent Overstreet } else if (!b->parent) { 2120d6fd3b11SKent Overstreet /* Root filled up but didn't need to be split */ 2121b54d6934SKent Overstreet closure_sync(&cl); 2122cafe5635SKent Overstreet bch_btree_set_root(n1); 2123cafe5635SKent Overstreet } else { 212417e21a9fSKent Overstreet /* Split a non root node */ 2125b54d6934SKent Overstreet closure_sync(&cl); 212617e21a9fSKent Overstreet make_btree_freeing_key(b, parent_keys.top); 212717e21a9fSKent Overstreet bch_keylist_push(&parent_keys); 212817e21a9fSKent Overstreet 212917e21a9fSKent Overstreet bch_btree_insert_node(b->parent, op, &parent_keys, NULL, NULL); 213017e21a9fSKent Overstreet BUG_ON(!bch_keylist_empty(&parent_keys)); 2131cafe5635SKent Overstreet } 2132cafe5635SKent Overstreet 213305335cffSKent Overstreet btree_node_free(b); 2134cafe5635SKent Overstreet rw_unlock(true, n1); 2135cafe5635SKent Overstreet 2136169ef1cfSKent Overstreet bch_time_stats_update(&b->c->btree_split_time, start_time); 2137cafe5635SKent Overstreet 2138cafe5635SKent Overstreet return 0; 2139cafe5635SKent Overstreet err_free2: 21405f5837d2SKent Overstreet bkey_put(b->c, &n2->key); 2141e8e1d468SKent Overstreet btree_node_free(n2); 2142cafe5635SKent Overstreet rw_unlock(true, n2); 2143cafe5635SKent Overstreet err_free1: 21445f5837d2SKent Overstreet bkey_put(b->c, &n1->key); 2145e8e1d468SKent Overstreet btree_node_free(n1); 2146cafe5635SKent Overstreet rw_unlock(true, n1); 2147cafe5635SKent Overstreet err: 21480a63b66dSKent Overstreet WARN(1, "bcache: btree split failed (level %u)", b->level); 21495f5837d2SKent Overstreet 2150cafe5635SKent Overstreet if (n3 == ERR_PTR(-EAGAIN) || 2151cafe5635SKent Overstreet n2 == ERR_PTR(-EAGAIN) || 2152cafe5635SKent Overstreet n1 == ERR_PTR(-EAGAIN)) 2153cafe5635SKent Overstreet return -EAGAIN; 2154cafe5635SKent Overstreet 2155cafe5635SKent Overstreet return -ENOMEM; 2156cafe5635SKent Overstreet } 2157cafe5635SKent Overstreet 215826c949f8SKent Overstreet static int bch_btree_insert_node(struct btree *b, struct btree_op *op, 2159c18536a7SKent Overstreet struct keylist *insert_keys, 21601b207d80SKent Overstreet atomic_t *journal_ref, 21611b207d80SKent Overstreet struct bkey *replace_key) 216226c949f8SKent Overstreet { 21632a285686SKent Overstreet struct closure cl; 21642a285686SKent Overstreet 21651b207d80SKent Overstreet BUG_ON(b->level && replace_key); 21661b207d80SKent Overstreet 21672a285686SKent Overstreet closure_init_stack(&cl); 21682a285686SKent Overstreet 21692a285686SKent Overstreet mutex_lock(&b->write_lock); 21702a285686SKent Overstreet 21712a285686SKent Overstreet if (write_block(b) != btree_bset_last(b) && 21722a285686SKent Overstreet b->keys.last_set_unwritten) 21732a285686SKent Overstreet bch_btree_init_next(b); /* just wrote a set */ 21742a285686SKent Overstreet 217559158fdeSKent Overstreet if (bch_keylist_nkeys(insert_keys) > insert_u64s_remaining(b)) { 21762a285686SKent Overstreet mutex_unlock(&b->write_lock); 21772a285686SKent Overstreet goto split; 21782a285686SKent Overstreet } 21792a285686SKent Overstreet 21802a285686SKent Overstreet BUG_ON(write_block(b) != btree_bset_last(b)); 21812a285686SKent Overstreet 21822a285686SKent Overstreet if (bch_btree_insert_keys(b, op, insert_keys, replace_key)) { 21832a285686SKent Overstreet if (!b->level) 21842a285686SKent Overstreet bch_btree_leaf_dirty(b, journal_ref); 21852a285686SKent Overstreet else 21862a285686SKent Overstreet bch_btree_node_write(b, &cl); 21872a285686SKent Overstreet } 21882a285686SKent Overstreet 21892a285686SKent Overstreet mutex_unlock(&b->write_lock); 21902a285686SKent Overstreet 21912a285686SKent Overstreet /* wait for btree node write if necessary, after unlock */ 21922a285686SKent Overstreet closure_sync(&cl); 21932a285686SKent Overstreet 21942a285686SKent Overstreet return 0; 21952a285686SKent Overstreet split: 219626c949f8SKent Overstreet if (current->bio_list) { 219726c949f8SKent Overstreet op->lock = b->c->root->level + 1; 219817e21a9fSKent Overstreet return -EAGAIN; 219926c949f8SKent Overstreet } else if (op->lock <= b->c->root->level) { 220026c949f8SKent Overstreet op->lock = b->c->root->level + 1; 220117e21a9fSKent Overstreet return -EINTR; 220226c949f8SKent Overstreet } else { 220317e21a9fSKent Overstreet /* Invalidated all iterators */ 22043b3e9e50SKent Overstreet int ret = btree_split(b, op, insert_keys, replace_key); 22053b3e9e50SKent Overstreet 22062a285686SKent Overstreet if (bch_keylist_empty(insert_keys)) 220717e21a9fSKent Overstreet return 0; 22082a285686SKent Overstreet else if (!ret) 22092a285686SKent Overstreet return -EINTR; 22102a285686SKent Overstreet return ret; 221117e21a9fSKent Overstreet } 221226c949f8SKent Overstreet } 221326c949f8SKent Overstreet 2214e7c590ebSKent Overstreet int bch_btree_insert_check_key(struct btree *b, struct btree_op *op, 2215e7c590ebSKent Overstreet struct bkey *check_key) 2216e7c590ebSKent Overstreet { 2217e7c590ebSKent Overstreet int ret = -EINTR; 2218e7c590ebSKent Overstreet uint64_t btree_ptr = b->key.ptr[0]; 2219e7c590ebSKent Overstreet unsigned long seq = b->seq; 2220e7c590ebSKent Overstreet struct keylist insert; 2221e7c590ebSKent Overstreet bool upgrade = op->lock == -1; 2222e7c590ebSKent Overstreet 2223e7c590ebSKent Overstreet bch_keylist_init(&insert); 2224e7c590ebSKent Overstreet 2225e7c590ebSKent Overstreet if (upgrade) { 2226e7c590ebSKent Overstreet rw_unlock(false, b); 2227e7c590ebSKent Overstreet rw_lock(true, b, b->level); 2228e7c590ebSKent Overstreet 2229e7c590ebSKent Overstreet if (b->key.ptr[0] != btree_ptr || 22302ef9ccbfSZheng Liu b->seq != seq + 1) { 22312ef9ccbfSZheng Liu op->lock = b->level; 2232e7c590ebSKent Overstreet goto out; 2233e7c590ebSKent Overstreet } 22342ef9ccbfSZheng Liu } 2235e7c590ebSKent Overstreet 2236e7c590ebSKent Overstreet SET_KEY_PTRS(check_key, 1); 2237e7c590ebSKent Overstreet get_random_bytes(&check_key->ptr[0], sizeof(uint64_t)); 2238e7c590ebSKent Overstreet 2239e7c590ebSKent Overstreet SET_PTR_DEV(check_key, 0, PTR_CHECK_DEV); 2240e7c590ebSKent Overstreet 2241e7c590ebSKent Overstreet bch_keylist_add(&insert, check_key); 2242e7c590ebSKent Overstreet 22431b207d80SKent Overstreet ret = bch_btree_insert_node(b, op, &insert, NULL, NULL); 2244e7c590ebSKent Overstreet 2245e7c590ebSKent Overstreet BUG_ON(!ret && !bch_keylist_empty(&insert)); 2246e7c590ebSKent Overstreet out: 2247e7c590ebSKent Overstreet if (upgrade) 2248e7c590ebSKent Overstreet downgrade_write(&b->lock); 2249e7c590ebSKent Overstreet return ret; 2250e7c590ebSKent Overstreet } 2251e7c590ebSKent Overstreet 2252cc7b8819SKent Overstreet struct btree_insert_op { 2253cc7b8819SKent Overstreet struct btree_op op; 2254cc7b8819SKent Overstreet struct keylist *keys; 2255cc7b8819SKent Overstreet atomic_t *journal_ref; 2256cc7b8819SKent Overstreet struct bkey *replace_key; 2257cc7b8819SKent Overstreet }; 2258cc7b8819SKent Overstreet 225908239ca2SWei Yongjun static int btree_insert_fn(struct btree_op *b_op, struct btree *b) 2260cafe5635SKent Overstreet { 2261cc7b8819SKent Overstreet struct btree_insert_op *op = container_of(b_op, 2262cc7b8819SKent Overstreet struct btree_insert_op, op); 2263403b6cdeSKent Overstreet 2264cc7b8819SKent Overstreet int ret = bch_btree_insert_node(b, &op->op, op->keys, 2265cc7b8819SKent Overstreet op->journal_ref, op->replace_key); 2266cc7b8819SKent Overstreet if (ret && !bch_keylist_empty(op->keys)) 2267cc7b8819SKent Overstreet return ret; 2268cc7b8819SKent Overstreet else 2269cc7b8819SKent Overstreet return MAP_DONE; 2270cafe5635SKent Overstreet } 2271cafe5635SKent Overstreet 2272cc7b8819SKent Overstreet int bch_btree_insert(struct cache_set *c, struct keylist *keys, 2273cc7b8819SKent Overstreet atomic_t *journal_ref, struct bkey *replace_key) 2274cafe5635SKent Overstreet { 2275cc7b8819SKent Overstreet struct btree_insert_op op; 2276cafe5635SKent Overstreet int ret = 0; 2277cafe5635SKent Overstreet 2278cc7b8819SKent Overstreet BUG_ON(current->bio_list); 22794f3d4014SKent Overstreet BUG_ON(bch_keylist_empty(keys)); 2280cafe5635SKent Overstreet 2281cc7b8819SKent Overstreet bch_btree_op_init(&op.op, 0); 2282cc7b8819SKent Overstreet op.keys = keys; 2283cc7b8819SKent Overstreet op.journal_ref = journal_ref; 2284cc7b8819SKent Overstreet op.replace_key = replace_key; 2285cafe5635SKent Overstreet 2286cc7b8819SKent Overstreet while (!ret && !bch_keylist_empty(keys)) { 2287cc7b8819SKent Overstreet op.op.lock = 0; 2288cc7b8819SKent Overstreet ret = bch_btree_map_leaf_nodes(&op.op, c, 2289cc7b8819SKent Overstreet &START_KEY(keys->keys), 2290cc7b8819SKent Overstreet btree_insert_fn); 2291cc7b8819SKent Overstreet } 2292cc7b8819SKent Overstreet 2293cc7b8819SKent Overstreet if (ret) { 2294cafe5635SKent Overstreet struct bkey *k; 2295cafe5635SKent Overstreet 22961b207d80SKent Overstreet pr_err("error %i", ret); 2297cafe5635SKent Overstreet 22984f3d4014SKent Overstreet while ((k = bch_keylist_pop(keys))) 22993a3b6a4eSKent Overstreet bkey_put(c, k); 2300cc7b8819SKent Overstreet } else if (op.op.insert_collision) 2301cc7b8819SKent Overstreet ret = -ESRCH; 23026054c6d4SKent Overstreet 2303cafe5635SKent Overstreet return ret; 2304cafe5635SKent Overstreet } 2305cafe5635SKent Overstreet 2306cafe5635SKent Overstreet void bch_btree_set_root(struct btree *b) 2307cafe5635SKent Overstreet { 23086f10f7d1SColy Li unsigned int i; 2309e49c7c37SKent Overstreet struct closure cl; 2310e49c7c37SKent Overstreet 2311e49c7c37SKent Overstreet closure_init_stack(&cl); 2312cafe5635SKent Overstreet 2313c37511b8SKent Overstreet trace_bcache_btree_set_root(b); 2314c37511b8SKent Overstreet 2315cafe5635SKent Overstreet BUG_ON(!b->written); 2316cafe5635SKent Overstreet 2317cafe5635SKent Overstreet for (i = 0; i < KEY_PTRS(&b->key); i++) 2318cafe5635SKent Overstreet BUG_ON(PTR_BUCKET(b->c, &b->key, i)->prio != BTREE_PRIO); 2319cafe5635SKent Overstreet 2320cafe5635SKent Overstreet mutex_lock(&b->c->bucket_lock); 2321cafe5635SKent Overstreet list_del_init(&b->list); 2322cafe5635SKent Overstreet mutex_unlock(&b->c->bucket_lock); 2323cafe5635SKent Overstreet 2324cafe5635SKent Overstreet b->c->root = b; 2325cafe5635SKent Overstreet 2326e49c7c37SKent Overstreet bch_journal_meta(b->c, &cl); 2327e49c7c37SKent Overstreet closure_sync(&cl); 2328cafe5635SKent Overstreet } 2329cafe5635SKent Overstreet 233048dad8baSKent Overstreet /* Map across nodes or keys */ 233148dad8baSKent Overstreet 233248dad8baSKent Overstreet static int bch_btree_map_nodes_recurse(struct btree *b, struct btree_op *op, 233348dad8baSKent Overstreet struct bkey *from, 233448dad8baSKent Overstreet btree_map_nodes_fn *fn, int flags) 233548dad8baSKent Overstreet { 233648dad8baSKent Overstreet int ret = MAP_CONTINUE; 233748dad8baSKent Overstreet 233848dad8baSKent Overstreet if (b->level) { 233948dad8baSKent Overstreet struct bkey *k; 234048dad8baSKent Overstreet struct btree_iter iter; 234148dad8baSKent Overstreet 2342c052dd9aSKent Overstreet bch_btree_iter_init(&b->keys, &iter, from); 234348dad8baSKent Overstreet 2344a85e968eSKent Overstreet while ((k = bch_btree_iter_next_filter(&iter, &b->keys, 234548dad8baSKent Overstreet bch_ptr_bad))) { 2346*feac1a70SColy Li ret = bcache_btree(map_nodes_recurse, k, b, 234748dad8baSKent Overstreet op, from, fn, flags); 234848dad8baSKent Overstreet from = NULL; 234948dad8baSKent Overstreet 235048dad8baSKent Overstreet if (ret != MAP_CONTINUE) 235148dad8baSKent Overstreet return ret; 235248dad8baSKent Overstreet } 235348dad8baSKent Overstreet } 235448dad8baSKent Overstreet 235548dad8baSKent Overstreet if (!b->level || flags == MAP_ALL_NODES) 235648dad8baSKent Overstreet ret = fn(op, b); 235748dad8baSKent Overstreet 235848dad8baSKent Overstreet return ret; 235948dad8baSKent Overstreet } 236048dad8baSKent Overstreet 236148dad8baSKent Overstreet int __bch_btree_map_nodes(struct btree_op *op, struct cache_set *c, 236248dad8baSKent Overstreet struct bkey *from, btree_map_nodes_fn *fn, int flags) 236348dad8baSKent Overstreet { 2364*feac1a70SColy Li return bcache_btree_root(map_nodes_recurse, c, op, from, fn, flags); 236548dad8baSKent Overstreet } 236648dad8baSKent Overstreet 2367253a99d9SColy Li int bch_btree_map_keys_recurse(struct btree *b, struct btree_op *op, 236848dad8baSKent Overstreet struct bkey *from, btree_map_keys_fn *fn, 236948dad8baSKent Overstreet int flags) 237048dad8baSKent Overstreet { 237148dad8baSKent Overstreet int ret = MAP_CONTINUE; 237248dad8baSKent Overstreet struct bkey *k; 237348dad8baSKent Overstreet struct btree_iter iter; 237448dad8baSKent Overstreet 2375c052dd9aSKent Overstreet bch_btree_iter_init(&b->keys, &iter, from); 237648dad8baSKent Overstreet 2377a85e968eSKent Overstreet while ((k = bch_btree_iter_next_filter(&iter, &b->keys, bch_ptr_bad))) { 237848dad8baSKent Overstreet ret = !b->level 237948dad8baSKent Overstreet ? fn(op, b, k) 2380*feac1a70SColy Li : bcache_btree(map_keys_recurse, k, 2381*feac1a70SColy Li b, op, from, fn, flags); 238248dad8baSKent Overstreet from = NULL; 238348dad8baSKent Overstreet 238448dad8baSKent Overstreet if (ret != MAP_CONTINUE) 238548dad8baSKent Overstreet return ret; 238648dad8baSKent Overstreet } 238748dad8baSKent Overstreet 238848dad8baSKent Overstreet if (!b->level && (flags & MAP_END_KEY)) 238948dad8baSKent Overstreet ret = fn(op, b, &KEY(KEY_INODE(&b->key), 239048dad8baSKent Overstreet KEY_OFFSET(&b->key), 0)); 239148dad8baSKent Overstreet 239248dad8baSKent Overstreet return ret; 239348dad8baSKent Overstreet } 239448dad8baSKent Overstreet 239548dad8baSKent Overstreet int bch_btree_map_keys(struct btree_op *op, struct cache_set *c, 239648dad8baSKent Overstreet struct bkey *from, btree_map_keys_fn *fn, int flags) 239748dad8baSKent Overstreet { 2398*feac1a70SColy Li return bcache_btree_root(map_keys_recurse, c, op, from, fn, flags); 239948dad8baSKent Overstreet } 240048dad8baSKent Overstreet 2401cafe5635SKent Overstreet /* Keybuf code */ 2402cafe5635SKent Overstreet 2403cafe5635SKent Overstreet static inline int keybuf_cmp(struct keybuf_key *l, struct keybuf_key *r) 2404cafe5635SKent Overstreet { 2405cafe5635SKent Overstreet /* Overlapping keys compare equal */ 2406cafe5635SKent Overstreet if (bkey_cmp(&l->key, &START_KEY(&r->key)) <= 0) 2407cafe5635SKent Overstreet return -1; 2408cafe5635SKent Overstreet if (bkey_cmp(&START_KEY(&l->key), &r->key) >= 0) 2409cafe5635SKent Overstreet return 1; 2410cafe5635SKent Overstreet return 0; 2411cafe5635SKent Overstreet } 2412cafe5635SKent Overstreet 2413cafe5635SKent Overstreet static inline int keybuf_nonoverlapping_cmp(struct keybuf_key *l, 2414cafe5635SKent Overstreet struct keybuf_key *r) 2415cafe5635SKent Overstreet { 2416cafe5635SKent Overstreet return clamp_t(int64_t, bkey_cmp(&l->key, &r->key), -1, 1); 2417cafe5635SKent Overstreet } 2418cafe5635SKent Overstreet 241948dad8baSKent Overstreet struct refill { 242048dad8baSKent Overstreet struct btree_op op; 24216f10f7d1SColy Li unsigned int nr_found; 242248dad8baSKent Overstreet struct keybuf *buf; 242348dad8baSKent Overstreet struct bkey *end; 242448dad8baSKent Overstreet keybuf_pred_fn *pred; 242548dad8baSKent Overstreet }; 242648dad8baSKent Overstreet 242748dad8baSKent Overstreet static int refill_keybuf_fn(struct btree_op *op, struct btree *b, 242848dad8baSKent Overstreet struct bkey *k) 2429cafe5635SKent Overstreet { 243048dad8baSKent Overstreet struct refill *refill = container_of(op, struct refill, op); 243148dad8baSKent Overstreet struct keybuf *buf = refill->buf; 243248dad8baSKent Overstreet int ret = MAP_CONTINUE; 2433cafe5635SKent Overstreet 24342d6cb6edSTang Junhui if (bkey_cmp(k, refill->end) > 0) { 243548dad8baSKent Overstreet ret = MAP_DONE; 243648dad8baSKent Overstreet goto out; 2437cafe5635SKent Overstreet } 2438cafe5635SKent Overstreet 243948dad8baSKent Overstreet if (!KEY_SIZE(k)) /* end key */ 244048dad8baSKent Overstreet goto out; 2441cafe5635SKent Overstreet 244248dad8baSKent Overstreet if (refill->pred(buf, k)) { 2443cafe5635SKent Overstreet struct keybuf_key *w; 2444cafe5635SKent Overstreet 2445cafe5635SKent Overstreet spin_lock(&buf->lock); 2446cafe5635SKent Overstreet 2447cafe5635SKent Overstreet w = array_alloc(&buf->freelist); 244848dad8baSKent Overstreet if (!w) { 244948dad8baSKent Overstreet spin_unlock(&buf->lock); 245048dad8baSKent Overstreet return MAP_DONE; 245148dad8baSKent Overstreet } 2452cafe5635SKent Overstreet 2453cafe5635SKent Overstreet w->private = NULL; 2454cafe5635SKent Overstreet bkey_copy(&w->key, k); 2455cafe5635SKent Overstreet 2456cafe5635SKent Overstreet if (RB_INSERT(&buf->keys, w, node, keybuf_cmp)) 2457cafe5635SKent Overstreet array_free(&buf->freelist, w); 245848a915a8SKent Overstreet else 245948a915a8SKent Overstreet refill->nr_found++; 2460cafe5635SKent Overstreet 246148dad8baSKent Overstreet if (array_freelist_empty(&buf->freelist)) 246248dad8baSKent Overstreet ret = MAP_DONE; 246348dad8baSKent Overstreet 2464cafe5635SKent Overstreet spin_unlock(&buf->lock); 2465cafe5635SKent Overstreet } 246648dad8baSKent Overstreet out: 246748dad8baSKent Overstreet buf->last_scanned = *k; 246848dad8baSKent Overstreet return ret; 2469cafe5635SKent Overstreet } 2470cafe5635SKent Overstreet 2471cafe5635SKent Overstreet void bch_refill_keybuf(struct cache_set *c, struct keybuf *buf, 247272c27061SKent Overstreet struct bkey *end, keybuf_pred_fn *pred) 2473cafe5635SKent Overstreet { 2474cafe5635SKent Overstreet struct bkey start = buf->last_scanned; 247548dad8baSKent Overstreet struct refill refill; 2476cafe5635SKent Overstreet 2477cafe5635SKent Overstreet cond_resched(); 2478cafe5635SKent Overstreet 2479b54d6934SKent Overstreet bch_btree_op_init(&refill.op, -1); 248048a915a8SKent Overstreet refill.nr_found = 0; 248148dad8baSKent Overstreet refill.buf = buf; 248248dad8baSKent Overstreet refill.end = end; 248348dad8baSKent Overstreet refill.pred = pred; 248448dad8baSKent Overstreet 248548dad8baSKent Overstreet bch_btree_map_keys(&refill.op, c, &buf->last_scanned, 248648dad8baSKent Overstreet refill_keybuf_fn, MAP_END_KEY); 2487cafe5635SKent Overstreet 248848a915a8SKent Overstreet trace_bcache_keyscan(refill.nr_found, 2489cafe5635SKent Overstreet KEY_INODE(&start), KEY_OFFSET(&start), 249048a915a8SKent Overstreet KEY_INODE(&buf->last_scanned), 249148a915a8SKent Overstreet KEY_OFFSET(&buf->last_scanned)); 2492cafe5635SKent Overstreet 2493cafe5635SKent Overstreet spin_lock(&buf->lock); 2494cafe5635SKent Overstreet 2495cafe5635SKent Overstreet if (!RB_EMPTY_ROOT(&buf->keys)) { 2496cafe5635SKent Overstreet struct keybuf_key *w; 24971fae7cf0SColy Li 2498cafe5635SKent Overstreet w = RB_FIRST(&buf->keys, struct keybuf_key, node); 2499cafe5635SKent Overstreet buf->start = START_KEY(&w->key); 2500cafe5635SKent Overstreet 2501cafe5635SKent Overstreet w = RB_LAST(&buf->keys, struct keybuf_key, node); 2502cafe5635SKent Overstreet buf->end = w->key; 2503cafe5635SKent Overstreet } else { 2504cafe5635SKent Overstreet buf->start = MAX_KEY; 2505cafe5635SKent Overstreet buf->end = MAX_KEY; 2506cafe5635SKent Overstreet } 2507cafe5635SKent Overstreet 2508cafe5635SKent Overstreet spin_unlock(&buf->lock); 2509cafe5635SKent Overstreet } 2510cafe5635SKent Overstreet 2511cafe5635SKent Overstreet static void __bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w) 2512cafe5635SKent Overstreet { 2513cafe5635SKent Overstreet rb_erase(&w->node, &buf->keys); 2514cafe5635SKent Overstreet array_free(&buf->freelist, w); 2515cafe5635SKent Overstreet } 2516cafe5635SKent Overstreet 2517cafe5635SKent Overstreet void bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w) 2518cafe5635SKent Overstreet { 2519cafe5635SKent Overstreet spin_lock(&buf->lock); 2520cafe5635SKent Overstreet __bch_keybuf_del(buf, w); 2521cafe5635SKent Overstreet spin_unlock(&buf->lock); 2522cafe5635SKent Overstreet } 2523cafe5635SKent Overstreet 2524cafe5635SKent Overstreet bool bch_keybuf_check_overlapping(struct keybuf *buf, struct bkey *start, 2525cafe5635SKent Overstreet struct bkey *end) 2526cafe5635SKent Overstreet { 2527cafe5635SKent Overstreet bool ret = false; 2528cafe5635SKent Overstreet struct keybuf_key *p, *w, s; 25291fae7cf0SColy Li 2530cafe5635SKent Overstreet s.key = *start; 2531cafe5635SKent Overstreet 2532cafe5635SKent Overstreet if (bkey_cmp(end, &buf->start) <= 0 || 2533cafe5635SKent Overstreet bkey_cmp(start, &buf->end) >= 0) 2534cafe5635SKent Overstreet return false; 2535cafe5635SKent Overstreet 2536cafe5635SKent Overstreet spin_lock(&buf->lock); 2537cafe5635SKent Overstreet w = RB_GREATER(&buf->keys, s, node, keybuf_nonoverlapping_cmp); 2538cafe5635SKent Overstreet 2539cafe5635SKent Overstreet while (w && bkey_cmp(&START_KEY(&w->key), end) < 0) { 2540cafe5635SKent Overstreet p = w; 2541cafe5635SKent Overstreet w = RB_NEXT(w, node); 2542cafe5635SKent Overstreet 2543cafe5635SKent Overstreet if (p->private) 2544cafe5635SKent Overstreet ret = true; 2545cafe5635SKent Overstreet else 2546cafe5635SKent Overstreet __bch_keybuf_del(buf, p); 2547cafe5635SKent Overstreet } 2548cafe5635SKent Overstreet 2549cafe5635SKent Overstreet spin_unlock(&buf->lock); 2550cafe5635SKent Overstreet return ret; 2551cafe5635SKent Overstreet } 2552cafe5635SKent Overstreet 2553cafe5635SKent Overstreet struct keybuf_key *bch_keybuf_next(struct keybuf *buf) 2554cafe5635SKent Overstreet { 2555cafe5635SKent Overstreet struct keybuf_key *w; 25561fae7cf0SColy Li 2557cafe5635SKent Overstreet spin_lock(&buf->lock); 2558cafe5635SKent Overstreet 2559cafe5635SKent Overstreet w = RB_FIRST(&buf->keys, struct keybuf_key, node); 2560cafe5635SKent Overstreet 2561cafe5635SKent Overstreet while (w && w->private) 2562cafe5635SKent Overstreet w = RB_NEXT(w, node); 2563cafe5635SKent Overstreet 2564cafe5635SKent Overstreet if (w) 2565cafe5635SKent Overstreet w->private = ERR_PTR(-EINTR); 2566cafe5635SKent Overstreet 2567cafe5635SKent Overstreet spin_unlock(&buf->lock); 2568cafe5635SKent Overstreet return w; 2569cafe5635SKent Overstreet } 2570cafe5635SKent Overstreet 2571cafe5635SKent Overstreet struct keybuf_key *bch_keybuf_next_rescan(struct cache_set *c, 2572cafe5635SKent Overstreet struct keybuf *buf, 257372c27061SKent Overstreet struct bkey *end, 257472c27061SKent Overstreet keybuf_pred_fn *pred) 2575cafe5635SKent Overstreet { 2576cafe5635SKent Overstreet struct keybuf_key *ret; 2577cafe5635SKent Overstreet 2578cafe5635SKent Overstreet while (1) { 2579cafe5635SKent Overstreet ret = bch_keybuf_next(buf); 2580cafe5635SKent Overstreet if (ret) 2581cafe5635SKent Overstreet break; 2582cafe5635SKent Overstreet 2583cafe5635SKent Overstreet if (bkey_cmp(&buf->last_scanned, end) >= 0) { 2584cafe5635SKent Overstreet pr_debug("scan finished"); 2585cafe5635SKent Overstreet break; 2586cafe5635SKent Overstreet } 2587cafe5635SKent Overstreet 258872c27061SKent Overstreet bch_refill_keybuf(c, buf, end, pred); 2589cafe5635SKent Overstreet } 2590cafe5635SKent Overstreet 2591cafe5635SKent Overstreet return ret; 2592cafe5635SKent Overstreet } 2593cafe5635SKent Overstreet 259472c27061SKent Overstreet void bch_keybuf_init(struct keybuf *buf) 2595cafe5635SKent Overstreet { 2596cafe5635SKent Overstreet buf->last_scanned = MAX_KEY; 2597cafe5635SKent Overstreet buf->keys = RB_ROOT; 2598cafe5635SKent Overstreet 2599cafe5635SKent Overstreet spin_lock_init(&buf->lock); 2600cafe5635SKent Overstreet array_allocator_init(&buf->freelist); 2601cafe5635SKent Overstreet } 2602