blob: 04636a70ffaadece29f02874bd846741a8422fe1 [file] [log] [blame]
Kent Overstreetcafe5632013-03-23 16:11:31 -07001/*
2 * Copyright (C) 2010 Kent Overstreet <kent.overstreet@gmail.com>
3 *
4 * Uses a block device as cache for other block devices; optimized for SSDs.
5 * All allocation is done in buckets, which should match the erase block size
6 * of the device.
7 *
8 * Buckets containing cached data are kept on a heap sorted by priority;
9 * bucket priority is increased on cache hit, and periodically all the buckets
10 * on the heap have their priority scaled down. This currently is just used as
11 * an LRU but in the future should allow for more intelligent heuristics.
12 *
13 * Buckets have an 8 bit counter; freeing is accomplished by incrementing the
14 * counter. Garbage collection is used to remove stale pointers.
15 *
16 * Indexing is done via a btree; nodes are not necessarily fully sorted, rather
17 * as keys are inserted we only sort the pages that have not yet been written.
18 * When garbage collection is run, we resort the entire node.
19 *
20 * All configuration is done via sysfs; see Documentation/bcache.txt.
21 */
22
23#include "bcache.h"
24#include "btree.h"
25#include "debug.h"
26#include "request.h"
Kent Overstreet279afba2013-06-05 06:21:07 -070027#include "writeback.h"
Kent Overstreetcafe5632013-03-23 16:11:31 -070028
29#include <linux/slab.h>
30#include <linux/bitops.h>
31#include <linux/hash.h>
Geert Uytterhoevencd953ed2013-03-27 18:56:28 +010032#include <linux/prefetch.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070033#include <linux/random.h>
34#include <linux/rcupdate.h>
35#include <trace/events/bcache.h>
36
37/*
38 * Todo:
39 * register_bcache: Return errors out to userspace correctly
40 *
41 * Writeback: don't undirty key until after a cache flush
42 *
43 * Create an iterator for key pointers
44 *
45 * On btree write error, mark bucket such that it won't be freed from the cache
46 *
47 * Journalling:
48 * Check for bad keys in replay
49 * Propagate barriers
50 * Refcount journal entries in journal_replay
51 *
52 * Garbage collection:
53 * Finish incremental gc
54 * Gc should free old UUIDs, data for invalid UUIDs
55 *
56 * Provide a way to list backing device UUIDs we have data cached for, and
57 * probably how long it's been since we've seen them, and a way to invalidate
58 * dirty data for devices that will never be attached again
59 *
60 * Keep 1 min/5 min/15 min statistics of how busy a block device has been, so
61 * that based on that and how much dirty data we have we can keep writeback
62 * from being starved
63 *
64 * Add a tracepoint or somesuch to watch for writeback starvation
65 *
66 * When btree depth > 1 and splitting an interior node, we have to make sure
67 * alloc_bucket() cannot fail. This should be true but is not completely
68 * obvious.
69 *
70 * Make sure all allocations get charged to the root cgroup
71 *
72 * Plugging?
73 *
74 * If data write is less than hard sector size of ssd, round up offset in open
75 * bucket to the next whole sector
76 *
77 * Also lookup by cgroup in get_open_bucket()
78 *
79 * Superblock needs to be fleshed out for multiple cache devices
80 *
81 * Add a sysfs tunable for the number of writeback IOs in flight
82 *
83 * Add a sysfs tunable for the number of open data buckets
84 *
85 * IO tracking: Can we track when one process is doing io on behalf of another?
86 * IO tracking: Don't use just an average, weigh more recent stuff higher
87 *
88 * Test module load/unload
89 */
90
91static const char * const op_types[] = {
92 "insert", "replace"
93};
94
95static const char *op_type(struct btree_op *op)
96{
97 return op_types[op->type];
98}
99
100#define MAX_NEED_GC 64
101#define MAX_SAVE_PRIO 72
102
103#define PTR_DIRTY_BIT (((uint64_t) 1 << 36))
104
105#define PTR_HASH(c, k) \
106 (((k)->ptr[0] >> c->bucket_bits) | PTR_GEN(k, 0))
107
108struct workqueue_struct *bch_gc_wq;
109static struct workqueue_struct *btree_io_wq;
110
111void bch_btree_op_init_stack(struct btree_op *op)
112{
113 memset(op, 0, sizeof(struct btree_op));
114 closure_init_stack(&op->cl);
115 op->lock = -1;
116 bch_keylist_init(&op->keys);
117}
118
119/* Btree key manipulation */
120
121static void bkey_put(struct cache_set *c, struct bkey *k, int level)
122{
123 if ((level && KEY_OFFSET(k)) || !level)
124 __bkey_put(c, k);
125}
126
127/* Btree IO */
128
129static uint64_t btree_csum_set(struct btree *b, struct bset *i)
130{
131 uint64_t crc = b->key.ptr[0];
132 void *data = (void *) i + 8, *end = end(i);
133
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600134 crc = bch_crc64_update(crc, data, end - data);
Kent Overstreetc19ed232013-03-26 13:49:02 -0700135 return crc ^ 0xffffffffffffffffULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700136}
137
Kent Overstreet57943512013-04-25 13:58:35 -0700138void bch_btree_node_read_done(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700139{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700140 const char *err = "bad btree header";
Kent Overstreet57943512013-04-25 13:58:35 -0700141 struct bset *i = b->sets[0].data;
142 struct btree_iter *iter;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700143
Kent Overstreet57943512013-04-25 13:58:35 -0700144 iter = mempool_alloc(b->c->fill_iter, GFP_NOWAIT);
145 iter->size = b->c->sb.bucket_size / b->c->sb.block_size;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700146 iter->used = 0;
147
Kent Overstreet57943512013-04-25 13:58:35 -0700148 if (!i->seq)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700149 goto err;
150
151 for (;
152 b->written < btree_blocks(b) && i->seq == b->sets[0].data->seq;
153 i = write_block(b)) {
154 err = "unsupported bset version";
155 if (i->version > BCACHE_BSET_VERSION)
156 goto err;
157
158 err = "bad btree header";
159 if (b->written + set_blocks(i, b->c) > btree_blocks(b))
160 goto err;
161
162 err = "bad magic";
163 if (i->magic != bset_magic(b->c))
164 goto err;
165
166 err = "bad checksum";
167 switch (i->version) {
168 case 0:
169 if (i->csum != csum_set(i))
170 goto err;
171 break;
172 case BCACHE_BSET_VERSION:
173 if (i->csum != btree_csum_set(b, i))
174 goto err;
175 break;
176 }
177
178 err = "empty set";
179 if (i != b->sets[0].data && !i->keys)
180 goto err;
181
182 bch_btree_iter_push(iter, i->start, end(i));
183
184 b->written += set_blocks(i, b->c);
185 }
186
187 err = "corrupted btree";
188 for (i = write_block(b);
189 index(i, b) < btree_blocks(b);
190 i = ((void *) i) + block_bytes(b->c))
191 if (i->seq == b->sets[0].data->seq)
192 goto err;
193
194 bch_btree_sort_and_fix_extents(b, iter);
195
196 i = b->sets[0].data;
197 err = "short btree key";
198 if (b->sets[0].size &&
199 bkey_cmp(&b->key, &b->sets[0].end) < 0)
200 goto err;
201
202 if (b->written < btree_blocks(b))
203 bch_bset_init_next(b);
204out:
Kent Overstreet57943512013-04-25 13:58:35 -0700205 mempool_free(iter, b->c->fill_iter);
206 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700207err:
208 set_btree_node_io_error(b);
Kent Overstreet07e86cc2013-03-25 11:46:43 -0700209 bch_cache_set_error(b->c, "%s at bucket %zu, block %zu, %u keys",
Kent Overstreetcafe5632013-03-23 16:11:31 -0700210 err, PTR_BUCKET_NR(b->c, &b->key, 0),
211 index(i, b), i->keys);
212 goto out;
213}
214
Kent Overstreet57943512013-04-25 13:58:35 -0700215static void btree_node_read_endio(struct bio *bio, int error)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700216{
Kent Overstreet57943512013-04-25 13:58:35 -0700217 struct closure *cl = bio->bi_private;
218 closure_put(cl);
219}
Kent Overstreetcafe5632013-03-23 16:11:31 -0700220
Kent Overstreet57943512013-04-25 13:58:35 -0700221void bch_btree_node_read(struct btree *b)
222{
223 uint64_t start_time = local_clock();
224 struct closure cl;
225 struct bio *bio;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700226
Kent Overstreetc37511b2013-04-26 15:39:55 -0700227 trace_bcache_btree_read(b);
228
Kent Overstreet57943512013-04-25 13:58:35 -0700229 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700230
Kent Overstreet57943512013-04-25 13:58:35 -0700231 bio = bch_bbio_alloc(b->c);
232 bio->bi_rw = REQ_META|READ_SYNC;
233 bio->bi_size = KEY_SIZE(&b->key) << 9;
234 bio->bi_end_io = btree_node_read_endio;
235 bio->bi_private = &cl;
236
237 bch_bio_map(bio, b->sets[0].data);
238
Kent Overstreet57943512013-04-25 13:58:35 -0700239 bch_submit_bbio(bio, b->c, &b->key, 0);
240 closure_sync(&cl);
241
242 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
243 set_btree_node_io_error(b);
244
245 bch_bbio_free(bio, b->c);
246
247 if (btree_node_io_error(b))
248 goto err;
249
250 bch_btree_node_read_done(b);
251
252 spin_lock(&b->c->btree_read_time_lock);
253 bch_time_stats_update(&b->c->btree_read_time, start_time);
254 spin_unlock(&b->c->btree_read_time_lock);
255
256 return;
257err:
258 bch_cache_set_error(b->c, "io error reading bucket %lu",
259 PTR_BUCKET_NR(b->c, &b->key, 0));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700260}
261
262static void btree_complete_write(struct btree *b, struct btree_write *w)
263{
264 if (w->prio_blocked &&
265 !atomic_sub_return(w->prio_blocked, &b->c->prio_blocked))
Kent Overstreet119ba0f2013-04-24 19:01:12 -0700266 wake_up_allocators(b->c);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700267
268 if (w->journal) {
269 atomic_dec_bug(w->journal);
270 __closure_wake_up(&b->c->journal.wait);
271 }
272
Kent Overstreetcafe5632013-03-23 16:11:31 -0700273 w->prio_blocked = 0;
274 w->journal = NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700275}
276
Kent Overstreet57943512013-04-25 13:58:35 -0700277static void __btree_node_write_done(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700278{
279 struct btree *b = container_of(cl, struct btree, io.cl);
280 struct btree_write *w = btree_prev_write(b);
281
282 bch_bbio_free(b->bio, b->c);
283 b->bio = NULL;
284 btree_complete_write(b, w);
285
286 if (btree_node_dirty(b))
287 queue_delayed_work(btree_io_wq, &b->work,
288 msecs_to_jiffies(30000));
289
290 closure_return(cl);
291}
292
Kent Overstreet57943512013-04-25 13:58:35 -0700293static void btree_node_write_done(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700294{
295 struct btree *b = container_of(cl, struct btree, io.cl);
296 struct bio_vec *bv;
297 int n;
298
299 __bio_for_each_segment(bv, b->bio, n, 0)
300 __free_page(bv->bv_page);
301
Kent Overstreet57943512013-04-25 13:58:35 -0700302 __btree_node_write_done(cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700303}
304
Kent Overstreet57943512013-04-25 13:58:35 -0700305static void btree_node_write_endio(struct bio *bio, int error)
306{
307 struct closure *cl = bio->bi_private;
308 struct btree *b = container_of(cl, struct btree, io.cl);
309
310 if (error)
311 set_btree_node_io_error(b);
312
313 bch_bbio_count_io_errors(b->c, bio, error, "writing btree");
314 closure_put(cl);
315}
316
317static void do_btree_node_write(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700318{
319 struct closure *cl = &b->io.cl;
320 struct bset *i = b->sets[b->nsets].data;
321 BKEY_PADDED(key) k;
322
323 i->version = BCACHE_BSET_VERSION;
324 i->csum = btree_csum_set(b, i);
325
Kent Overstreet57943512013-04-25 13:58:35 -0700326 BUG_ON(b->bio);
327 b->bio = bch_bbio_alloc(b->c);
328
329 b->bio->bi_end_io = btree_node_write_endio;
330 b->bio->bi_private = &b->io.cl;
Kent Overstreete49c7c32013-06-26 17:25:38 -0700331 b->bio->bi_rw = REQ_META|WRITE_SYNC|REQ_FUA;
332 b->bio->bi_size = set_blocks(i, b->c) * block_bytes(b->c);
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600333 bch_bio_map(b->bio, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700334
Kent Overstreete49c7c32013-06-26 17:25:38 -0700335 /*
336 * If we're appending to a leaf node, we don't technically need FUA -
337 * this write just needs to be persisted before the next journal write,
338 * which will be marked FLUSH|FUA.
339 *
340 * Similarly if we're writing a new btree root - the pointer is going to
341 * be in the next journal entry.
342 *
343 * But if we're writing a new btree node (that isn't a root) or
344 * appending to a non leaf btree node, we need either FUA or a flush
345 * when we write the parent with the new pointer. FUA is cheaper than a
346 * flush, and writes appending to leaf nodes aren't blocking anything so
347 * just make all btree node writes FUA to keep things sane.
348 */
349
Kent Overstreetcafe5632013-03-23 16:11:31 -0700350 bkey_copy(&k.key, &b->key);
351 SET_PTR_OFFSET(&k.key, 0, PTR_OFFSET(&k.key, 0) + bset_offset(b, i));
352
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600353 if (!bch_bio_alloc_pages(b->bio, GFP_NOIO)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700354 int j;
355 struct bio_vec *bv;
356 void *base = (void *) ((unsigned long) i & ~(PAGE_SIZE - 1));
357
358 bio_for_each_segment(bv, b->bio, j)
359 memcpy(page_address(bv->bv_page),
360 base + j * PAGE_SIZE, PAGE_SIZE);
361
Kent Overstreetcafe5632013-03-23 16:11:31 -0700362 bch_submit_bbio(b->bio, b->c, &k.key, 0);
363
Kent Overstreet57943512013-04-25 13:58:35 -0700364 continue_at(cl, btree_node_write_done, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700365 } else {
366 b->bio->bi_vcnt = 0;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600367 bch_bio_map(b->bio, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700368
Kent Overstreetcafe5632013-03-23 16:11:31 -0700369 bch_submit_bbio(b->bio, b->c, &k.key, 0);
370
371 closure_sync(cl);
Kent Overstreet57943512013-04-25 13:58:35 -0700372 __btree_node_write_done(cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700373 }
374}
375
Kent Overstreet57943512013-04-25 13:58:35 -0700376void bch_btree_node_write(struct btree *b, struct closure *parent)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700377{
378 struct bset *i = b->sets[b->nsets].data;
379
Kent Overstreetc37511b2013-04-26 15:39:55 -0700380 trace_bcache_btree_write(b);
381
Kent Overstreetcafe5632013-03-23 16:11:31 -0700382 BUG_ON(current->bio_list);
Kent Overstreet57943512013-04-25 13:58:35 -0700383 BUG_ON(b->written >= btree_blocks(b));
384 BUG_ON(b->written && !i->keys);
385 BUG_ON(b->sets->data->seq != i->seq);
Kent Overstreetc37511b2013-04-26 15:39:55 -0700386 bch_check_key_order(b, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700387
Kent Overstreetcafe5632013-03-23 16:11:31 -0700388 cancel_delayed_work(&b->work);
389
Kent Overstreet57943512013-04-25 13:58:35 -0700390 /* If caller isn't waiting for write, parent refcount is cache set */
391 closure_lock(&b->io, parent ?: &b->c->cl);
392
Kent Overstreetcafe5632013-03-23 16:11:31 -0700393 clear_bit(BTREE_NODE_dirty, &b->flags);
394 change_bit(BTREE_NODE_write_idx, &b->flags);
395
Kent Overstreet57943512013-04-25 13:58:35 -0700396 do_btree_node_write(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700397
Kent Overstreetcafe5632013-03-23 16:11:31 -0700398 b->written += set_blocks(i, b->c);
399 atomic_long_add(set_blocks(i, b->c) * b->c->sb.block_size,
400 &PTR_CACHE(b->c, &b->key, 0)->btree_sectors_written);
401
402 bch_btree_sort_lazy(b);
403
404 if (b->written < btree_blocks(b))
405 bch_bset_init_next(b);
406}
407
Kent Overstreet57943512013-04-25 13:58:35 -0700408static void btree_node_write_work(struct work_struct *w)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700409{
410 struct btree *b = container_of(to_delayed_work(w), struct btree, work);
411
Kent Overstreet57943512013-04-25 13:58:35 -0700412 rw_lock(true, b, b->level);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700413
414 if (btree_node_dirty(b))
Kent Overstreet57943512013-04-25 13:58:35 -0700415 bch_btree_node_write(b, NULL);
416 rw_unlock(true, b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700417}
418
Kent Overstreet57943512013-04-25 13:58:35 -0700419static void bch_btree_leaf_dirty(struct btree *b, struct btree_op *op)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700420{
421 struct bset *i = b->sets[b->nsets].data;
422 struct btree_write *w = btree_current_write(b);
423
Kent Overstreet57943512013-04-25 13:58:35 -0700424 BUG_ON(!b->written);
425 BUG_ON(!i->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700426
Kent Overstreet57943512013-04-25 13:58:35 -0700427 if (!btree_node_dirty(b))
428 queue_delayed_work(btree_io_wq, &b->work, 30 * HZ);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700429
Kent Overstreet57943512013-04-25 13:58:35 -0700430 set_btree_node_dirty(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700431
Kent Overstreet57943512013-04-25 13:58:35 -0700432 if (op && op->journal) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700433 if (w->journal &&
434 journal_pin_cmp(b->c, w, op)) {
435 atomic_dec_bug(w->journal);
436 w->journal = NULL;
437 }
438
439 if (!w->journal) {
440 w->journal = op->journal;
441 atomic_inc(w->journal);
442 }
443 }
444
Kent Overstreetcafe5632013-03-23 16:11:31 -0700445 /* Force write if set is too big */
Kent Overstreet57943512013-04-25 13:58:35 -0700446 if (set_bytes(i) > PAGE_SIZE - 48 &&
447 !current->bio_list)
448 bch_btree_node_write(b, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700449}
450
451/*
452 * Btree in memory cache - allocation/freeing
453 * mca -> memory cache
454 */
455
456static void mca_reinit(struct btree *b)
457{
458 unsigned i;
459
460 b->flags = 0;
461 b->written = 0;
462 b->nsets = 0;
463
464 for (i = 0; i < MAX_BSETS; i++)
465 b->sets[i].size = 0;
466 /*
467 * Second loop starts at 1 because b->sets[0]->data is the memory we
468 * allocated
469 */
470 for (i = 1; i < MAX_BSETS; i++)
471 b->sets[i].data = NULL;
472}
473
474#define mca_reserve(c) (((c->root && c->root->level) \
475 ? c->root->level : 1) * 8 + 16)
476#define mca_can_free(c) \
477 max_t(int, 0, c->bucket_cache_used - mca_reserve(c))
478
479static void mca_data_free(struct btree *b)
480{
481 struct bset_tree *t = b->sets;
482 BUG_ON(!closure_is_unlocked(&b->io.cl));
483
484 if (bset_prev_bytes(b) < PAGE_SIZE)
485 kfree(t->prev);
486 else
487 free_pages((unsigned long) t->prev,
488 get_order(bset_prev_bytes(b)));
489
490 if (bset_tree_bytes(b) < PAGE_SIZE)
491 kfree(t->tree);
492 else
493 free_pages((unsigned long) t->tree,
494 get_order(bset_tree_bytes(b)));
495
496 free_pages((unsigned long) t->data, b->page_order);
497
498 t->prev = NULL;
499 t->tree = NULL;
500 t->data = NULL;
501 list_move(&b->list, &b->c->btree_cache_freed);
502 b->c->bucket_cache_used--;
503}
504
505static void mca_bucket_free(struct btree *b)
506{
507 BUG_ON(btree_node_dirty(b));
508
509 b->key.ptr[0] = 0;
510 hlist_del_init_rcu(&b->hash);
511 list_move(&b->list, &b->c->btree_cache_freeable);
512}
513
514static unsigned btree_order(struct bkey *k)
515{
516 return ilog2(KEY_SIZE(k) / PAGE_SECTORS ?: 1);
517}
518
519static void mca_data_alloc(struct btree *b, struct bkey *k, gfp_t gfp)
520{
521 struct bset_tree *t = b->sets;
522 BUG_ON(t->data);
523
524 b->page_order = max_t(unsigned,
525 ilog2(b->c->btree_pages),
526 btree_order(k));
527
528 t->data = (void *) __get_free_pages(gfp, b->page_order);
529 if (!t->data)
530 goto err;
531
532 t->tree = bset_tree_bytes(b) < PAGE_SIZE
533 ? kmalloc(bset_tree_bytes(b), gfp)
534 : (void *) __get_free_pages(gfp, get_order(bset_tree_bytes(b)));
535 if (!t->tree)
536 goto err;
537
538 t->prev = bset_prev_bytes(b) < PAGE_SIZE
539 ? kmalloc(bset_prev_bytes(b), gfp)
540 : (void *) __get_free_pages(gfp, get_order(bset_prev_bytes(b)));
541 if (!t->prev)
542 goto err;
543
544 list_move(&b->list, &b->c->btree_cache);
545 b->c->bucket_cache_used++;
546 return;
547err:
548 mca_data_free(b);
549}
550
551static struct btree *mca_bucket_alloc(struct cache_set *c,
552 struct bkey *k, gfp_t gfp)
553{
554 struct btree *b = kzalloc(sizeof(struct btree), gfp);
555 if (!b)
556 return NULL;
557
558 init_rwsem(&b->lock);
559 lockdep_set_novalidate_class(&b->lock);
560 INIT_LIST_HEAD(&b->list);
Kent Overstreet57943512013-04-25 13:58:35 -0700561 INIT_DELAYED_WORK(&b->work, btree_node_write_work);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700562 b->c = c;
563 closure_init_unlocked(&b->io);
564
565 mca_data_alloc(b, k, gfp);
566 return b;
567}
568
569static int mca_reap(struct btree *b, struct closure *cl, unsigned min_order)
570{
571 lockdep_assert_held(&b->c->bucket_lock);
572
573 if (!down_write_trylock(&b->lock))
574 return -ENOMEM;
575
576 if (b->page_order < min_order) {
577 rw_unlock(true, b);
578 return -ENOMEM;
579 }
580
581 BUG_ON(btree_node_dirty(b) && !b->sets[0].data);
582
583 if (cl && btree_node_dirty(b))
Kent Overstreet57943512013-04-25 13:58:35 -0700584 bch_btree_node_write(b, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700585
586 if (cl)
587 closure_wait_event_async(&b->io.wait, cl,
588 atomic_read(&b->io.cl.remaining) == -1);
589
590 if (btree_node_dirty(b) ||
591 !closure_is_unlocked(&b->io.cl) ||
592 work_pending(&b->work.work)) {
593 rw_unlock(true, b);
594 return -EAGAIN;
595 }
596
597 return 0;
598}
599
600static int bch_mca_shrink(struct shrinker *shrink, struct shrink_control *sc)
601{
602 struct cache_set *c = container_of(shrink, struct cache_set, shrink);
603 struct btree *b, *t;
604 unsigned long i, nr = sc->nr_to_scan;
605
606 if (c->shrinker_disabled)
607 return 0;
608
609 if (c->try_harder)
610 return 0;
611
612 /*
613 * If nr == 0, we're supposed to return the number of items we have
614 * cached. Not allowed to return -1.
615 */
616 if (!nr)
617 return mca_can_free(c) * c->btree_pages;
618
619 /* Return -1 if we can't do anything right now */
620 if (sc->gfp_mask & __GFP_WAIT)
621 mutex_lock(&c->bucket_lock);
622 else if (!mutex_trylock(&c->bucket_lock))
623 return -1;
624
Kent Overstreet36c9ea92013-06-03 13:04:56 -0700625 /*
626 * It's _really_ critical that we don't free too many btree nodes - we
627 * have to always leave ourselves a reserve. The reserve is how we
628 * guarantee that allocating memory for a new btree node can always
629 * succeed, so that inserting keys into the btree can always succeed and
630 * IO can always make forward progress:
631 */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700632 nr /= c->btree_pages;
633 nr = min_t(unsigned long, nr, mca_can_free(c));
634
635 i = 0;
636 list_for_each_entry_safe(b, t, &c->btree_cache_freeable, list) {
637 if (!nr)
638 break;
639
640 if (++i > 3 &&
641 !mca_reap(b, NULL, 0)) {
642 mca_data_free(b);
643 rw_unlock(true, b);
644 --nr;
645 }
646 }
647
648 /*
649 * Can happen right when we first start up, before we've read in any
650 * btree nodes
651 */
652 if (list_empty(&c->btree_cache))
653 goto out;
654
655 for (i = 0; nr && i < c->bucket_cache_used; i++) {
656 b = list_first_entry(&c->btree_cache, struct btree, list);
657 list_rotate_left(&c->btree_cache);
658
659 if (!b->accessed &&
660 !mca_reap(b, NULL, 0)) {
661 mca_bucket_free(b);
662 mca_data_free(b);
663 rw_unlock(true, b);
664 --nr;
665 } else
666 b->accessed = 0;
667 }
668out:
669 nr = mca_can_free(c) * c->btree_pages;
670 mutex_unlock(&c->bucket_lock);
671 return nr;
672}
673
674void bch_btree_cache_free(struct cache_set *c)
675{
676 struct btree *b;
677 struct closure cl;
678 closure_init_stack(&cl);
679
680 if (c->shrink.list.next)
681 unregister_shrinker(&c->shrink);
682
683 mutex_lock(&c->bucket_lock);
684
685#ifdef CONFIG_BCACHE_DEBUG
686 if (c->verify_data)
687 list_move(&c->verify_data->list, &c->btree_cache);
688#endif
689
690 list_splice(&c->btree_cache_freeable,
691 &c->btree_cache);
692
693 while (!list_empty(&c->btree_cache)) {
694 b = list_first_entry(&c->btree_cache, struct btree, list);
695
696 if (btree_node_dirty(b))
697 btree_complete_write(b, btree_current_write(b));
698 clear_bit(BTREE_NODE_dirty, &b->flags);
699
700 mca_data_free(b);
701 }
702
703 while (!list_empty(&c->btree_cache_freed)) {
704 b = list_first_entry(&c->btree_cache_freed,
705 struct btree, list);
706 list_del(&b->list);
707 cancel_delayed_work_sync(&b->work);
708 kfree(b);
709 }
710
711 mutex_unlock(&c->bucket_lock);
712}
713
714int bch_btree_cache_alloc(struct cache_set *c)
715{
716 unsigned i;
717
718 /* XXX: doesn't check for errors */
719
720 closure_init_unlocked(&c->gc);
721
722 for (i = 0; i < mca_reserve(c); i++)
723 mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL);
724
725 list_splice_init(&c->btree_cache,
726 &c->btree_cache_freeable);
727
728#ifdef CONFIG_BCACHE_DEBUG
729 mutex_init(&c->verify_lock);
730
731 c->verify_data = mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL);
732
733 if (c->verify_data &&
734 c->verify_data->sets[0].data)
735 list_del_init(&c->verify_data->list);
736 else
737 c->verify_data = NULL;
738#endif
739
740 c->shrink.shrink = bch_mca_shrink;
741 c->shrink.seeks = 4;
742 c->shrink.batch = c->btree_pages * 2;
743 register_shrinker(&c->shrink);
744
745 return 0;
746}
747
748/* Btree in memory cache - hash table */
749
750static struct hlist_head *mca_hash(struct cache_set *c, struct bkey *k)
751{
752 return &c->bucket_hash[hash_32(PTR_HASH(c, k), BUCKET_HASH_BITS)];
753}
754
755static struct btree *mca_find(struct cache_set *c, struct bkey *k)
756{
757 struct btree *b;
758
759 rcu_read_lock();
760 hlist_for_each_entry_rcu(b, mca_hash(c, k), hash)
761 if (PTR_HASH(c, &b->key) == PTR_HASH(c, k))
762 goto out;
763 b = NULL;
764out:
765 rcu_read_unlock();
766 return b;
767}
768
769static struct btree *mca_cannibalize(struct cache_set *c, struct bkey *k,
770 int level, struct closure *cl)
771{
772 int ret = -ENOMEM;
773 struct btree *i;
774
Kent Overstreetc37511b2013-04-26 15:39:55 -0700775 trace_bcache_btree_cache_cannibalize(c);
776
Kent Overstreetcafe5632013-03-23 16:11:31 -0700777 if (!cl)
778 return ERR_PTR(-ENOMEM);
779
780 /*
781 * Trying to free up some memory - i.e. reuse some btree nodes - may
782 * require initiating IO to flush the dirty part of the node. If we're
783 * running under generic_make_request(), that IO will never finish and
784 * we would deadlock. Returning -EAGAIN causes the cache lookup code to
785 * punt to workqueue and retry.
786 */
787 if (current->bio_list)
788 return ERR_PTR(-EAGAIN);
789
790 if (c->try_harder && c->try_harder != cl) {
791 closure_wait_event_async(&c->try_wait, cl, !c->try_harder);
792 return ERR_PTR(-EAGAIN);
793 }
794
Kent Overstreetcafe5632013-03-23 16:11:31 -0700795 c->try_harder = cl;
796 c->try_harder_start = local_clock();
797retry:
798 list_for_each_entry_reverse(i, &c->btree_cache, list) {
799 int r = mca_reap(i, cl, btree_order(k));
800 if (!r)
801 return i;
802 if (r != -ENOMEM)
803 ret = r;
804 }
805
806 if (ret == -EAGAIN &&
807 closure_blocking(cl)) {
808 mutex_unlock(&c->bucket_lock);
809 closure_sync(cl);
810 mutex_lock(&c->bucket_lock);
811 goto retry;
812 }
813
814 return ERR_PTR(ret);
815}
816
817/*
818 * We can only have one thread cannibalizing other cached btree nodes at a time,
819 * or we'll deadlock. We use an open coded mutex to ensure that, which a
820 * cannibalize_bucket() will take. This means every time we unlock the root of
821 * the btree, we need to release this lock if we have it held.
822 */
823void bch_cannibalize_unlock(struct cache_set *c, struct closure *cl)
824{
825 if (c->try_harder == cl) {
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600826 bch_time_stats_update(&c->try_harder_time, c->try_harder_start);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700827 c->try_harder = NULL;
828 __closure_wake_up(&c->try_wait);
829 }
830}
831
832static struct btree *mca_alloc(struct cache_set *c, struct bkey *k,
833 int level, struct closure *cl)
834{
835 struct btree *b;
836
837 lockdep_assert_held(&c->bucket_lock);
838
839 if (mca_find(c, k))
840 return NULL;
841
842 /* btree_free() doesn't free memory; it sticks the node on the end of
843 * the list. Check if there's any freed nodes there:
844 */
845 list_for_each_entry(b, &c->btree_cache_freeable, list)
846 if (!mca_reap(b, NULL, btree_order(k)))
847 goto out;
848
849 /* We never free struct btree itself, just the memory that holds the on
850 * disk node. Check the freed list before allocating a new one:
851 */
852 list_for_each_entry(b, &c->btree_cache_freed, list)
853 if (!mca_reap(b, NULL, 0)) {
854 mca_data_alloc(b, k, __GFP_NOWARN|GFP_NOIO);
855 if (!b->sets[0].data)
856 goto err;
857 else
858 goto out;
859 }
860
861 b = mca_bucket_alloc(c, k, __GFP_NOWARN|GFP_NOIO);
862 if (!b)
863 goto err;
864
865 BUG_ON(!down_write_trylock(&b->lock));
866 if (!b->sets->data)
867 goto err;
868out:
869 BUG_ON(!closure_is_unlocked(&b->io.cl));
870
871 bkey_copy(&b->key, k);
872 list_move(&b->list, &c->btree_cache);
873 hlist_del_init_rcu(&b->hash);
874 hlist_add_head_rcu(&b->hash, mca_hash(c, k));
875
876 lock_set_subclass(&b->lock.dep_map, level + 1, _THIS_IP_);
877 b->level = level;
878
879 mca_reinit(b);
880
881 return b;
882err:
883 if (b)
884 rw_unlock(true, b);
885
886 b = mca_cannibalize(c, k, level, cl);
887 if (!IS_ERR(b))
888 goto out;
889
890 return b;
891}
892
893/**
894 * bch_btree_node_get - find a btree node in the cache and lock it, reading it
895 * in from disk if necessary.
896 *
897 * If IO is necessary, it uses the closure embedded in struct btree_op to wait;
898 * if that closure is in non blocking mode, will return -EAGAIN.
899 *
900 * The btree node will have either a read or a write lock held, depending on
901 * level and op->lock.
902 */
903struct btree *bch_btree_node_get(struct cache_set *c, struct bkey *k,
904 int level, struct btree_op *op)
905{
906 int i = 0;
907 bool write = level <= op->lock;
908 struct btree *b;
909
910 BUG_ON(level < 0);
911retry:
912 b = mca_find(c, k);
913
914 if (!b) {
Kent Overstreet57943512013-04-25 13:58:35 -0700915 if (current->bio_list)
916 return ERR_PTR(-EAGAIN);
917
Kent Overstreetcafe5632013-03-23 16:11:31 -0700918 mutex_lock(&c->bucket_lock);
919 b = mca_alloc(c, k, level, &op->cl);
920 mutex_unlock(&c->bucket_lock);
921
922 if (!b)
923 goto retry;
924 if (IS_ERR(b))
925 return b;
926
Kent Overstreet57943512013-04-25 13:58:35 -0700927 bch_btree_node_read(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700928
929 if (!write)
930 downgrade_write(&b->lock);
931 } else {
932 rw_lock(write, b, level);
933 if (PTR_HASH(c, &b->key) != PTR_HASH(c, k)) {
934 rw_unlock(write, b);
935 goto retry;
936 }
937 BUG_ON(b->level != level);
938 }
939
940 b->accessed = 1;
941
942 for (; i <= b->nsets && b->sets[i].size; i++) {
943 prefetch(b->sets[i].tree);
944 prefetch(b->sets[i].data);
945 }
946
947 for (; i <= b->nsets; i++)
948 prefetch(b->sets[i].data);
949
Kent Overstreet57943512013-04-25 13:58:35 -0700950 if (btree_node_io_error(b)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700951 rw_unlock(write, b);
Kent Overstreet57943512013-04-25 13:58:35 -0700952 return ERR_PTR(-EIO);
953 }
954
955 BUG_ON(!b->written);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700956
957 return b;
958}
959
960static void btree_node_prefetch(struct cache_set *c, struct bkey *k, int level)
961{
962 struct btree *b;
963
964 mutex_lock(&c->bucket_lock);
965 b = mca_alloc(c, k, level, NULL);
966 mutex_unlock(&c->bucket_lock);
967
968 if (!IS_ERR_OR_NULL(b)) {
Kent Overstreet57943512013-04-25 13:58:35 -0700969 bch_btree_node_read(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700970 rw_unlock(true, b);
971 }
972}
973
974/* Btree alloc */
975
976static void btree_node_free(struct btree *b, struct btree_op *op)
977{
978 unsigned i;
979
Kent Overstreetc37511b2013-04-26 15:39:55 -0700980 trace_bcache_btree_node_free(b);
981
Kent Overstreetcafe5632013-03-23 16:11:31 -0700982 /*
983 * The BUG_ON() in btree_node_get() implies that we must have a write
984 * lock on parent to free or even invalidate a node
985 */
986 BUG_ON(op->lock <= b->level);
987 BUG_ON(b == b->c->root);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700988
989 if (btree_node_dirty(b))
990 btree_complete_write(b, btree_current_write(b));
991 clear_bit(BTREE_NODE_dirty, &b->flags);
992
Kent Overstreetcafe5632013-03-23 16:11:31 -0700993 cancel_delayed_work(&b->work);
994
995 mutex_lock(&b->c->bucket_lock);
996
997 for (i = 0; i < KEY_PTRS(&b->key); i++) {
998 BUG_ON(atomic_read(&PTR_BUCKET(b->c, &b->key, i)->pin));
999
1000 bch_inc_gen(PTR_CACHE(b->c, &b->key, i),
1001 PTR_BUCKET(b->c, &b->key, i));
1002 }
1003
1004 bch_bucket_free(b->c, &b->key);
1005 mca_bucket_free(b);
1006 mutex_unlock(&b->c->bucket_lock);
1007}
1008
1009struct btree *bch_btree_node_alloc(struct cache_set *c, int level,
1010 struct closure *cl)
1011{
1012 BKEY_PADDED(key) k;
1013 struct btree *b = ERR_PTR(-EAGAIN);
1014
1015 mutex_lock(&c->bucket_lock);
1016retry:
1017 if (__bch_bucket_alloc_set(c, WATERMARK_METADATA, &k.key, 1, cl))
1018 goto err;
1019
1020 SET_KEY_SIZE(&k.key, c->btree_pages * PAGE_SECTORS);
1021
1022 b = mca_alloc(c, &k.key, level, cl);
1023 if (IS_ERR(b))
1024 goto err_free;
1025
1026 if (!b) {
Kent Overstreetb1a67b02013-03-25 11:46:44 -07001027 cache_bug(c,
1028 "Tried to allocate bucket that was in btree cache");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001029 __bkey_put(c, &k.key);
1030 goto retry;
1031 }
1032
Kent Overstreetcafe5632013-03-23 16:11:31 -07001033 b->accessed = 1;
1034 bch_bset_init_next(b);
1035
1036 mutex_unlock(&c->bucket_lock);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001037
1038 trace_bcache_btree_node_alloc(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001039 return b;
1040err_free:
1041 bch_bucket_free(c, &k.key);
1042 __bkey_put(c, &k.key);
1043err:
1044 mutex_unlock(&c->bucket_lock);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001045
1046 trace_bcache_btree_node_alloc_fail(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001047 return b;
1048}
1049
1050static struct btree *btree_node_alloc_replacement(struct btree *b,
1051 struct closure *cl)
1052{
1053 struct btree *n = bch_btree_node_alloc(b->c, b->level, cl);
1054 if (!IS_ERR_OR_NULL(n))
1055 bch_btree_sort_into(b, n);
1056
1057 return n;
1058}
1059
1060/* Garbage collection */
1061
1062uint8_t __bch_btree_mark_key(struct cache_set *c, int level, struct bkey *k)
1063{
1064 uint8_t stale = 0;
1065 unsigned i;
1066 struct bucket *g;
1067
1068 /*
1069 * ptr_invalid() can't return true for the keys that mark btree nodes as
1070 * freed, but since ptr_bad() returns true we'll never actually use them
1071 * for anything and thus we don't want mark their pointers here
1072 */
1073 if (!bkey_cmp(k, &ZERO_KEY))
1074 return stale;
1075
1076 for (i = 0; i < KEY_PTRS(k); i++) {
1077 if (!ptr_available(c, k, i))
1078 continue;
1079
1080 g = PTR_BUCKET(c, k, i);
1081
1082 if (gen_after(g->gc_gen, PTR_GEN(k, i)))
1083 g->gc_gen = PTR_GEN(k, i);
1084
1085 if (ptr_stale(c, k, i)) {
1086 stale = max(stale, ptr_stale(c, k, i));
1087 continue;
1088 }
1089
1090 cache_bug_on(GC_MARK(g) &&
1091 (GC_MARK(g) == GC_MARK_METADATA) != (level != 0),
1092 c, "inconsistent ptrs: mark = %llu, level = %i",
1093 GC_MARK(g), level);
1094
1095 if (level)
1096 SET_GC_MARK(g, GC_MARK_METADATA);
1097 else if (KEY_DIRTY(k))
1098 SET_GC_MARK(g, GC_MARK_DIRTY);
1099
1100 /* guard against overflow */
1101 SET_GC_SECTORS_USED(g, min_t(unsigned,
1102 GC_SECTORS_USED(g) + KEY_SIZE(k),
1103 (1 << 14) - 1));
1104
1105 BUG_ON(!GC_SECTORS_USED(g));
1106 }
1107
1108 return stale;
1109}
1110
1111#define btree_mark_key(b, k) __bch_btree_mark_key(b->c, b->level, k)
1112
1113static int btree_gc_mark_node(struct btree *b, unsigned *keys,
1114 struct gc_stat *gc)
1115{
1116 uint8_t stale = 0;
1117 unsigned last_dev = -1;
1118 struct bcache_device *d = NULL;
1119 struct bkey *k;
1120 struct btree_iter iter;
1121 struct bset_tree *t;
1122
1123 gc->nodes++;
1124
1125 for_each_key_filter(b, k, &iter, bch_ptr_invalid) {
1126 if (last_dev != KEY_INODE(k)) {
1127 last_dev = KEY_INODE(k);
1128
1129 d = KEY_INODE(k) < b->c->nr_uuids
1130 ? b->c->devices[last_dev]
1131 : NULL;
1132 }
1133
1134 stale = max(stale, btree_mark_key(b, k));
1135
1136 if (bch_ptr_bad(b, k))
1137 continue;
1138
1139 *keys += bkey_u64s(k);
1140
1141 gc->key_bytes += bkey_u64s(k);
1142 gc->nkeys++;
1143
1144 gc->data += KEY_SIZE(k);
Kent Overstreet444fc0b2013-05-11 17:07:26 -07001145 if (KEY_DIRTY(k))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001146 gc->dirty += KEY_SIZE(k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001147 }
1148
1149 for (t = b->sets; t <= &b->sets[b->nsets]; t++)
1150 btree_bug_on(t->size &&
1151 bset_written(b, t) &&
1152 bkey_cmp(&b->key, &t->end) < 0,
1153 b, "found short btree key in gc");
1154
1155 return stale;
1156}
1157
1158static struct btree *btree_gc_alloc(struct btree *b, struct bkey *k,
1159 struct btree_op *op)
1160{
1161 /*
1162 * We block priorities from being written for the duration of garbage
1163 * collection, so we can't sleep in btree_alloc() ->
1164 * bch_bucket_alloc_set(), or we'd risk deadlock - so we don't pass it
1165 * our closure.
1166 */
1167 struct btree *n = btree_node_alloc_replacement(b, NULL);
1168
1169 if (!IS_ERR_OR_NULL(n)) {
1170 swap(b, n);
Kent Overstreet57943512013-04-25 13:58:35 -07001171 __bkey_put(b->c, &b->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001172
1173 memcpy(k->ptr, b->key.ptr,
1174 sizeof(uint64_t) * KEY_PTRS(&b->key));
1175
Kent Overstreetcafe5632013-03-23 16:11:31 -07001176 btree_node_free(n, op);
1177 up_write(&n->lock);
1178 }
1179
1180 return b;
1181}
1182
1183/*
1184 * Leaving this at 2 until we've got incremental garbage collection done; it
1185 * could be higher (and has been tested with 4) except that garbage collection
1186 * could take much longer, adversely affecting latency.
1187 */
1188#define GC_MERGE_NODES 2U
1189
1190struct gc_merge_info {
1191 struct btree *b;
1192 struct bkey *k;
1193 unsigned keys;
1194};
1195
1196static void btree_gc_coalesce(struct btree *b, struct btree_op *op,
1197 struct gc_stat *gc, struct gc_merge_info *r)
1198{
1199 unsigned nodes = 0, keys = 0, blocks;
1200 int i;
1201
1202 while (nodes < GC_MERGE_NODES && r[nodes].b)
1203 keys += r[nodes++].keys;
1204
1205 blocks = btree_default_blocks(b->c) * 2 / 3;
1206
1207 if (nodes < 2 ||
1208 __set_blocks(b->sets[0].data, keys, b->c) > blocks * (nodes - 1))
1209 return;
1210
1211 for (i = nodes - 1; i >= 0; --i) {
1212 if (r[i].b->written)
1213 r[i].b = btree_gc_alloc(r[i].b, r[i].k, op);
1214
1215 if (r[i].b->written)
1216 return;
1217 }
1218
1219 for (i = nodes - 1; i > 0; --i) {
1220 struct bset *n1 = r[i].b->sets->data;
1221 struct bset *n2 = r[i - 1].b->sets->data;
1222 struct bkey *k, *last = NULL;
1223
1224 keys = 0;
1225
1226 if (i == 1) {
1227 /*
1228 * Last node we're not getting rid of - we're getting
1229 * rid of the node at r[0]. Have to try and fit all of
1230 * the remaining keys into this node; we can't ensure
1231 * they will always fit due to rounding and variable
1232 * length keys (shouldn't be possible in practice,
1233 * though)
1234 */
1235 if (__set_blocks(n1, n1->keys + r->keys,
1236 b->c) > btree_blocks(r[i].b))
1237 return;
1238
1239 keys = n2->keys;
1240 last = &r->b->key;
1241 } else
1242 for (k = n2->start;
1243 k < end(n2);
1244 k = bkey_next(k)) {
1245 if (__set_blocks(n1, n1->keys + keys +
1246 bkey_u64s(k), b->c) > blocks)
1247 break;
1248
1249 last = k;
1250 keys += bkey_u64s(k);
1251 }
1252
1253 BUG_ON(__set_blocks(n1, n1->keys + keys,
1254 b->c) > btree_blocks(r[i].b));
1255
1256 if (last) {
1257 bkey_copy_key(&r[i].b->key, last);
1258 bkey_copy_key(r[i].k, last);
1259 }
1260
1261 memcpy(end(n1),
1262 n2->start,
1263 (void *) node(n2, keys) - (void *) n2->start);
1264
1265 n1->keys += keys;
1266
1267 memmove(n2->start,
1268 node(n2, keys),
1269 (void *) end(n2) - (void *) node(n2, keys));
1270
1271 n2->keys -= keys;
1272
1273 r[i].keys = n1->keys;
1274 r[i - 1].keys = n2->keys;
1275 }
1276
1277 btree_node_free(r->b, op);
1278 up_write(&r->b->lock);
1279
Kent Overstreetc37511b2013-04-26 15:39:55 -07001280 trace_bcache_btree_gc_coalesce(nodes);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001281
1282 gc->nodes--;
1283 nodes--;
1284
1285 memmove(&r[0], &r[1], sizeof(struct gc_merge_info) * nodes);
1286 memset(&r[nodes], 0, sizeof(struct gc_merge_info));
1287}
1288
1289static int btree_gc_recurse(struct btree *b, struct btree_op *op,
1290 struct closure *writes, struct gc_stat *gc)
1291{
1292 void write(struct btree *r)
1293 {
1294 if (!r->written)
Kent Overstreet57943512013-04-25 13:58:35 -07001295 bch_btree_node_write(r, &op->cl);
1296 else if (btree_node_dirty(r))
1297 bch_btree_node_write(r, writes);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001298
1299 up_write(&r->lock);
1300 }
1301
1302 int ret = 0, stale;
1303 unsigned i;
1304 struct gc_merge_info r[GC_MERGE_NODES];
1305
1306 memset(r, 0, sizeof(r));
1307
1308 while ((r->k = bch_next_recurse_key(b, &b->c->gc_done))) {
1309 r->b = bch_btree_node_get(b->c, r->k, b->level - 1, op);
1310
1311 if (IS_ERR(r->b)) {
1312 ret = PTR_ERR(r->b);
1313 break;
1314 }
1315
1316 r->keys = 0;
1317 stale = btree_gc_mark_node(r->b, &r->keys, gc);
1318
1319 if (!b->written &&
1320 (r->b->level || stale > 10 ||
1321 b->c->gc_always_rewrite))
1322 r->b = btree_gc_alloc(r->b, r->k, op);
1323
1324 if (r->b->level)
1325 ret = btree_gc_recurse(r->b, op, writes, gc);
1326
1327 if (ret) {
1328 write(r->b);
1329 break;
1330 }
1331
1332 bkey_copy_key(&b->c->gc_done, r->k);
1333
1334 if (!b->written)
1335 btree_gc_coalesce(b, op, gc, r);
1336
1337 if (r[GC_MERGE_NODES - 1].b)
1338 write(r[GC_MERGE_NODES - 1].b);
1339
1340 memmove(&r[1], &r[0],
1341 sizeof(struct gc_merge_info) * (GC_MERGE_NODES - 1));
1342
1343 /* When we've got incremental GC working, we'll want to do
1344 * if (should_resched())
1345 * return -EAGAIN;
1346 */
1347 cond_resched();
1348#if 0
1349 if (need_resched()) {
1350 ret = -EAGAIN;
1351 break;
1352 }
1353#endif
1354 }
1355
1356 for (i = 1; i < GC_MERGE_NODES && r[i].b; i++)
1357 write(r[i].b);
1358
1359 /* Might have freed some children, must remove their keys */
1360 if (!b->written)
1361 bch_btree_sort(b);
1362
1363 return ret;
1364}
1365
1366static int bch_btree_gc_root(struct btree *b, struct btree_op *op,
1367 struct closure *writes, struct gc_stat *gc)
1368{
1369 struct btree *n = NULL;
1370 unsigned keys = 0;
1371 int ret = 0, stale = btree_gc_mark_node(b, &keys, gc);
1372
1373 if (b->level || stale > 10)
1374 n = btree_node_alloc_replacement(b, NULL);
1375
1376 if (!IS_ERR_OR_NULL(n))
1377 swap(b, n);
1378
1379 if (b->level)
1380 ret = btree_gc_recurse(b, op, writes, gc);
1381
1382 if (!b->written || btree_node_dirty(b)) {
Kent Overstreet57943512013-04-25 13:58:35 -07001383 bch_btree_node_write(b, n ? &op->cl : NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001384 }
1385
1386 if (!IS_ERR_OR_NULL(n)) {
1387 closure_sync(&op->cl);
1388 bch_btree_set_root(b);
1389 btree_node_free(n, op);
1390 rw_unlock(true, b);
1391 }
1392
1393 return ret;
1394}
1395
1396static void btree_gc_start(struct cache_set *c)
1397{
1398 struct cache *ca;
1399 struct bucket *b;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001400 unsigned i;
1401
1402 if (!c->gc_mark_valid)
1403 return;
1404
1405 mutex_lock(&c->bucket_lock);
1406
1407 c->gc_mark_valid = 0;
1408 c->gc_done = ZERO_KEY;
1409
1410 for_each_cache(ca, c, i)
1411 for_each_bucket(b, ca) {
1412 b->gc_gen = b->gen;
1413 if (!atomic_read(&b->pin))
1414 SET_GC_MARK(b, GC_MARK_RECLAIMABLE);
1415 }
1416
Kent Overstreetcafe5632013-03-23 16:11:31 -07001417 mutex_unlock(&c->bucket_lock);
1418}
1419
1420size_t bch_btree_gc_finish(struct cache_set *c)
1421{
1422 size_t available = 0;
1423 struct bucket *b;
1424 struct cache *ca;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001425 unsigned i;
1426
1427 mutex_lock(&c->bucket_lock);
1428
1429 set_gc_sectors(c);
1430 c->gc_mark_valid = 1;
1431 c->need_gc = 0;
1432
1433 if (c->root)
1434 for (i = 0; i < KEY_PTRS(&c->root->key); i++)
1435 SET_GC_MARK(PTR_BUCKET(c, &c->root->key, i),
1436 GC_MARK_METADATA);
1437
1438 for (i = 0; i < KEY_PTRS(&c->uuid_bucket); i++)
1439 SET_GC_MARK(PTR_BUCKET(c, &c->uuid_bucket, i),
1440 GC_MARK_METADATA);
1441
1442 for_each_cache(ca, c, i) {
1443 uint64_t *i;
1444
1445 ca->invalidate_needs_gc = 0;
1446
1447 for (i = ca->sb.d; i < ca->sb.d + ca->sb.keys; i++)
1448 SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA);
1449
1450 for (i = ca->prio_buckets;
1451 i < ca->prio_buckets + prio_buckets(ca) * 2; i++)
1452 SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA);
1453
1454 for_each_bucket(b, ca) {
1455 b->last_gc = b->gc_gen;
1456 c->need_gc = max(c->need_gc, bucket_gc_gen(b));
1457
1458 if (!atomic_read(&b->pin) &&
1459 GC_MARK(b) == GC_MARK_RECLAIMABLE) {
1460 available++;
1461 if (!GC_SECTORS_USED(b))
1462 bch_bucket_add_unused(ca, b);
1463 }
1464 }
1465 }
1466
Kent Overstreetcafe5632013-03-23 16:11:31 -07001467 mutex_unlock(&c->bucket_lock);
1468 return available;
1469}
1470
1471static void bch_btree_gc(struct closure *cl)
1472{
1473 struct cache_set *c = container_of(cl, struct cache_set, gc.cl);
1474 int ret;
1475 unsigned long available;
1476 struct gc_stat stats;
1477 struct closure writes;
1478 struct btree_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001479 uint64_t start_time = local_clock();
Kent Overstreet57943512013-04-25 13:58:35 -07001480
Kent Overstreetc37511b2013-04-26 15:39:55 -07001481 trace_bcache_gc_start(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001482
1483 memset(&stats, 0, sizeof(struct gc_stat));
1484 closure_init_stack(&writes);
1485 bch_btree_op_init_stack(&op);
1486 op.lock = SHRT_MAX;
1487
1488 btree_gc_start(c);
1489
Kent Overstreet57943512013-04-25 13:58:35 -07001490 atomic_inc(&c->prio_blocked);
1491
Kent Overstreetcafe5632013-03-23 16:11:31 -07001492 ret = btree_root(gc_root, c, &op, &writes, &stats);
1493 closure_sync(&op.cl);
1494 closure_sync(&writes);
1495
1496 if (ret) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001497 pr_warn("gc failed!");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001498 continue_at(cl, bch_btree_gc, bch_gc_wq);
1499 }
1500
1501 /* Possibly wait for new UUIDs or whatever to hit disk */
1502 bch_journal_meta(c, &op.cl);
1503 closure_sync(&op.cl);
1504
1505 available = bch_btree_gc_finish(c);
1506
Kent Overstreet57943512013-04-25 13:58:35 -07001507 atomic_dec(&c->prio_blocked);
1508 wake_up_allocators(c);
1509
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001510 bch_time_stats_update(&c->btree_gc_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001511
1512 stats.key_bytes *= sizeof(uint64_t);
1513 stats.dirty <<= 9;
1514 stats.data <<= 9;
1515 stats.in_use = (c->nbuckets - available) * 100 / c->nbuckets;
1516 memcpy(&c->gc_stats, &stats, sizeof(struct gc_stat));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001517
Kent Overstreetc37511b2013-04-26 15:39:55 -07001518 trace_bcache_gc_end(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001519
1520 continue_at(cl, bch_moving_gc, bch_gc_wq);
1521}
1522
1523void bch_queue_gc(struct cache_set *c)
1524{
1525 closure_trylock_call(&c->gc.cl, bch_btree_gc, bch_gc_wq, &c->cl);
1526}
1527
1528/* Initial partial gc */
1529
1530static int bch_btree_check_recurse(struct btree *b, struct btree_op *op,
1531 unsigned long **seen)
1532{
1533 int ret;
1534 unsigned i;
1535 struct bkey *k;
1536 struct bucket *g;
1537 struct btree_iter iter;
1538
1539 for_each_key_filter(b, k, &iter, bch_ptr_invalid) {
1540 for (i = 0; i < KEY_PTRS(k); i++) {
1541 if (!ptr_available(b->c, k, i))
1542 continue;
1543
1544 g = PTR_BUCKET(b->c, k, i);
1545
1546 if (!__test_and_set_bit(PTR_BUCKET_NR(b->c, k, i),
1547 seen[PTR_DEV(k, i)]) ||
1548 !ptr_stale(b->c, k, i)) {
1549 g->gen = PTR_GEN(k, i);
1550
1551 if (b->level)
1552 g->prio = BTREE_PRIO;
1553 else if (g->prio == BTREE_PRIO)
1554 g->prio = INITIAL_PRIO;
1555 }
1556 }
1557
1558 btree_mark_key(b, k);
1559 }
1560
1561 if (b->level) {
1562 k = bch_next_recurse_key(b, &ZERO_KEY);
1563
1564 while (k) {
1565 struct bkey *p = bch_next_recurse_key(b, k);
1566 if (p)
1567 btree_node_prefetch(b->c, p, b->level - 1);
1568
1569 ret = btree(check_recurse, k, b, op, seen);
1570 if (ret)
1571 return ret;
1572
1573 k = p;
1574 }
1575 }
1576
1577 return 0;
1578}
1579
1580int bch_btree_check(struct cache_set *c, struct btree_op *op)
1581{
1582 int ret = -ENOMEM;
1583 unsigned i;
1584 unsigned long *seen[MAX_CACHES_PER_SET];
1585
1586 memset(seen, 0, sizeof(seen));
1587
1588 for (i = 0; c->cache[i]; i++) {
1589 size_t n = DIV_ROUND_UP(c->cache[i]->sb.nbuckets, 8);
1590 seen[i] = kmalloc(n, GFP_KERNEL);
1591 if (!seen[i])
1592 goto err;
1593
1594 /* Disables the seen array until prio_read() uses it too */
1595 memset(seen[i], 0xFF, n);
1596 }
1597
1598 ret = btree_root(check_recurse, c, op, seen);
1599err:
1600 for (i = 0; i < MAX_CACHES_PER_SET; i++)
1601 kfree(seen[i]);
1602 return ret;
1603}
1604
1605/* Btree insertion */
1606
1607static void shift_keys(struct btree *b, struct bkey *where, struct bkey *insert)
1608{
1609 struct bset *i = b->sets[b->nsets].data;
1610
1611 memmove((uint64_t *) where + bkey_u64s(insert),
1612 where,
1613 (void *) end(i) - (void *) where);
1614
1615 i->keys += bkey_u64s(insert);
1616 bkey_copy(where, insert);
1617 bch_bset_fix_lookup_table(b, where);
1618}
1619
1620static bool fix_overlapping_extents(struct btree *b,
1621 struct bkey *insert,
1622 struct btree_iter *iter,
1623 struct btree_op *op)
1624{
Kent Overstreet279afba2013-06-05 06:21:07 -07001625 void subtract_dirty(struct bkey *k, uint64_t offset, int sectors)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001626 {
Kent Overstreet279afba2013-06-05 06:21:07 -07001627 if (KEY_DIRTY(k))
1628 bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k),
1629 offset, -sectors);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001630 }
1631
Kent Overstreet279afba2013-06-05 06:21:07 -07001632 uint64_t old_offset;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001633 unsigned old_size, sectors_found = 0;
1634
1635 while (1) {
1636 struct bkey *k = bch_btree_iter_next(iter);
1637 if (!k ||
1638 bkey_cmp(&START_KEY(k), insert) >= 0)
1639 break;
1640
1641 if (bkey_cmp(k, &START_KEY(insert)) <= 0)
1642 continue;
1643
Kent Overstreet279afba2013-06-05 06:21:07 -07001644 old_offset = KEY_START(k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001645 old_size = KEY_SIZE(k);
1646
1647 /*
1648 * We might overlap with 0 size extents; we can't skip these
1649 * because if they're in the set we're inserting to we have to
1650 * adjust them so they don't overlap with the key we're
1651 * inserting. But we don't want to check them for BTREE_REPLACE
1652 * operations.
1653 */
1654
1655 if (op->type == BTREE_REPLACE &&
1656 KEY_SIZE(k)) {
1657 /*
1658 * k might have been split since we inserted/found the
1659 * key we're replacing
1660 */
1661 unsigned i;
1662 uint64_t offset = KEY_START(k) -
1663 KEY_START(&op->replace);
1664
1665 /* But it must be a subset of the replace key */
1666 if (KEY_START(k) < KEY_START(&op->replace) ||
1667 KEY_OFFSET(k) > KEY_OFFSET(&op->replace))
1668 goto check_failed;
1669
1670 /* We didn't find a key that we were supposed to */
1671 if (KEY_START(k) > KEY_START(insert) + sectors_found)
1672 goto check_failed;
1673
1674 if (KEY_PTRS(&op->replace) != KEY_PTRS(k))
1675 goto check_failed;
1676
1677 /* skip past gen */
1678 offset <<= 8;
1679
1680 BUG_ON(!KEY_PTRS(&op->replace));
1681
1682 for (i = 0; i < KEY_PTRS(&op->replace); i++)
1683 if (k->ptr[i] != op->replace.ptr[i] + offset)
1684 goto check_failed;
1685
1686 sectors_found = KEY_OFFSET(k) - KEY_START(insert);
1687 }
1688
1689 if (bkey_cmp(insert, k) < 0 &&
1690 bkey_cmp(&START_KEY(insert), &START_KEY(k)) > 0) {
1691 /*
1692 * We overlapped in the middle of an existing key: that
1693 * means we have to split the old key. But we have to do
1694 * slightly different things depending on whether the
1695 * old key has been written out yet.
1696 */
1697
1698 struct bkey *top;
1699
Kent Overstreet279afba2013-06-05 06:21:07 -07001700 subtract_dirty(k, KEY_START(insert), KEY_SIZE(insert));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001701
1702 if (bkey_written(b, k)) {
1703 /*
1704 * We insert a new key to cover the top of the
1705 * old key, and the old key is modified in place
1706 * to represent the bottom split.
1707 *
1708 * It's completely arbitrary whether the new key
1709 * is the top or the bottom, but it has to match
1710 * up with what btree_sort_fixup() does - it
1711 * doesn't check for this kind of overlap, it
1712 * depends on us inserting a new key for the top
1713 * here.
1714 */
1715 top = bch_bset_search(b, &b->sets[b->nsets],
1716 insert);
1717 shift_keys(b, top, k);
1718 } else {
1719 BKEY_PADDED(key) temp;
1720 bkey_copy(&temp.key, k);
1721 shift_keys(b, k, &temp.key);
1722 top = bkey_next(k);
1723 }
1724
1725 bch_cut_front(insert, top);
1726 bch_cut_back(&START_KEY(insert), k);
1727 bch_bset_fix_invalidated_key(b, k);
1728 return false;
1729 }
1730
1731 if (bkey_cmp(insert, k) < 0) {
1732 bch_cut_front(insert, k);
1733 } else {
1734 if (bkey_written(b, k) &&
1735 bkey_cmp(&START_KEY(insert), &START_KEY(k)) <= 0) {
1736 /*
1737 * Completely overwrote, so we don't have to
1738 * invalidate the binary search tree
1739 */
1740 bch_cut_front(k, k);
1741 } else {
1742 __bch_cut_back(&START_KEY(insert), k);
1743 bch_bset_fix_invalidated_key(b, k);
1744 }
1745 }
1746
Kent Overstreet279afba2013-06-05 06:21:07 -07001747 subtract_dirty(k, old_offset, old_size - KEY_SIZE(k));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001748 }
1749
1750check_failed:
1751 if (op->type == BTREE_REPLACE) {
1752 if (!sectors_found) {
1753 op->insert_collision = true;
1754 return true;
1755 } else if (sectors_found < KEY_SIZE(insert)) {
1756 SET_KEY_OFFSET(insert, KEY_OFFSET(insert) -
1757 (KEY_SIZE(insert) - sectors_found));
1758 SET_KEY_SIZE(insert, sectors_found);
1759 }
1760 }
1761
1762 return false;
1763}
1764
1765static bool btree_insert_key(struct btree *b, struct btree_op *op,
1766 struct bkey *k)
1767{
1768 struct bset *i = b->sets[b->nsets].data;
1769 struct bkey *m, *prev;
Kent Overstreet85b14922013-05-14 20:33:16 -07001770 unsigned status = BTREE_INSERT_STATUS_INSERT;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001771
1772 BUG_ON(bkey_cmp(k, &b->key) > 0);
1773 BUG_ON(b->level && !KEY_PTRS(k));
1774 BUG_ON(!b->level && !KEY_OFFSET(k));
1775
1776 if (!b->level) {
1777 struct btree_iter iter;
1778 struct bkey search = KEY(KEY_INODE(k), KEY_START(k), 0);
1779
1780 /*
1781 * bset_search() returns the first key that is strictly greater
1782 * than the search key - but for back merging, we want to find
1783 * the first key that is greater than or equal to KEY_START(k) -
1784 * unless KEY_START(k) is 0.
1785 */
1786 if (KEY_OFFSET(&search))
1787 SET_KEY_OFFSET(&search, KEY_OFFSET(&search) - 1);
1788
1789 prev = NULL;
1790 m = bch_btree_iter_init(b, &iter, &search);
1791
1792 if (fix_overlapping_extents(b, k, &iter, op))
1793 return false;
1794
1795 while (m != end(i) &&
1796 bkey_cmp(k, &START_KEY(m)) > 0)
1797 prev = m, m = bkey_next(m);
1798
1799 if (key_merging_disabled(b->c))
1800 goto insert;
1801
1802 /* prev is in the tree, if we merge we're done */
Kent Overstreet85b14922013-05-14 20:33:16 -07001803 status = BTREE_INSERT_STATUS_BACK_MERGE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001804 if (prev &&
1805 bch_bkey_try_merge(b, prev, k))
1806 goto merged;
1807
Kent Overstreet85b14922013-05-14 20:33:16 -07001808 status = BTREE_INSERT_STATUS_OVERWROTE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001809 if (m != end(i) &&
1810 KEY_PTRS(m) == KEY_PTRS(k) && !KEY_SIZE(m))
1811 goto copy;
1812
Kent Overstreet85b14922013-05-14 20:33:16 -07001813 status = BTREE_INSERT_STATUS_FRONT_MERGE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001814 if (m != end(i) &&
1815 bch_bkey_try_merge(b, k, m))
1816 goto copy;
1817 } else
1818 m = bch_bset_search(b, &b->sets[b->nsets], k);
1819
1820insert: shift_keys(b, m, k);
1821copy: bkey_copy(m, k);
1822merged:
Kent Overstreet279afba2013-06-05 06:21:07 -07001823 if (KEY_DIRTY(k))
1824 bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k),
1825 KEY_START(k), KEY_SIZE(k));
1826
Kent Overstreet85b14922013-05-14 20:33:16 -07001827 bch_check_keys(b, "%u for %s", status, op_type(op));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001828
1829 if (b->level && !KEY_OFFSET(k))
Kent Overstreet57943512013-04-25 13:58:35 -07001830 btree_current_write(b)->prio_blocked++;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001831
Kent Overstreet85b14922013-05-14 20:33:16 -07001832 trace_bcache_btree_insert_key(b, k, op->type, status);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001833
1834 return true;
1835}
1836
1837bool bch_btree_insert_keys(struct btree *b, struct btree_op *op)
1838{
1839 bool ret = false;
1840 struct bkey *k;
1841 unsigned oldsize = bch_count_data(b);
1842
1843 while ((k = bch_keylist_pop(&op->keys))) {
1844 bkey_put(b->c, k, b->level);
1845 ret |= btree_insert_key(b, op, k);
1846 }
1847
1848 BUG_ON(bch_count_data(b) < oldsize);
1849 return ret;
1850}
1851
1852bool bch_btree_insert_check_key(struct btree *b, struct btree_op *op,
1853 struct bio *bio)
1854{
1855 bool ret = false;
1856 uint64_t btree_ptr = b->key.ptr[0];
1857 unsigned long seq = b->seq;
1858 BKEY_PADDED(k) tmp;
1859
1860 rw_unlock(false, b);
1861 rw_lock(true, b, b->level);
1862
1863 if (b->key.ptr[0] != btree_ptr ||
1864 b->seq != seq + 1 ||
1865 should_split(b))
1866 goto out;
1867
1868 op->replace = KEY(op->inode, bio_end(bio), bio_sectors(bio));
1869
1870 SET_KEY_PTRS(&op->replace, 1);
1871 get_random_bytes(&op->replace.ptr[0], sizeof(uint64_t));
1872
1873 SET_PTR_DEV(&op->replace, 0, PTR_CHECK_DEV);
1874
1875 bkey_copy(&tmp.k, &op->replace);
1876
1877 BUG_ON(op->type != BTREE_INSERT);
1878 BUG_ON(!btree_insert_key(b, op, &tmp.k));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001879 ret = true;
1880out:
1881 downgrade_write(&b->lock);
1882 return ret;
1883}
1884
1885static int btree_split(struct btree *b, struct btree_op *op)
1886{
1887 bool split, root = b == b->c->root;
1888 struct btree *n1, *n2 = NULL, *n3 = NULL;
1889 uint64_t start_time = local_clock();
1890
1891 if (b->level)
1892 set_closure_blocking(&op->cl);
1893
1894 n1 = btree_node_alloc_replacement(b, &op->cl);
1895 if (IS_ERR(n1))
1896 goto err;
1897
1898 split = set_blocks(n1->sets[0].data, n1->c) > (btree_blocks(b) * 4) / 5;
1899
Kent Overstreetcafe5632013-03-23 16:11:31 -07001900 if (split) {
1901 unsigned keys = 0;
1902
Kent Overstreetc37511b2013-04-26 15:39:55 -07001903 trace_bcache_btree_node_split(b, n1->sets[0].data->keys);
1904
Kent Overstreetcafe5632013-03-23 16:11:31 -07001905 n2 = bch_btree_node_alloc(b->c, b->level, &op->cl);
1906 if (IS_ERR(n2))
1907 goto err_free1;
1908
1909 if (root) {
1910 n3 = bch_btree_node_alloc(b->c, b->level + 1, &op->cl);
1911 if (IS_ERR(n3))
1912 goto err_free2;
1913 }
1914
1915 bch_btree_insert_keys(n1, op);
1916
1917 /* Has to be a linear search because we don't have an auxiliary
1918 * search tree yet
1919 */
1920
1921 while (keys < (n1->sets[0].data->keys * 3) / 5)
1922 keys += bkey_u64s(node(n1->sets[0].data, keys));
1923
1924 bkey_copy_key(&n1->key, node(n1->sets[0].data, keys));
1925 keys += bkey_u64s(node(n1->sets[0].data, keys));
1926
1927 n2->sets[0].data->keys = n1->sets[0].data->keys - keys;
1928 n1->sets[0].data->keys = keys;
1929
1930 memcpy(n2->sets[0].data->start,
1931 end(n1->sets[0].data),
1932 n2->sets[0].data->keys * sizeof(uint64_t));
1933
1934 bkey_copy_key(&n2->key, &b->key);
1935
1936 bch_keylist_add(&op->keys, &n2->key);
Kent Overstreet57943512013-04-25 13:58:35 -07001937 bch_btree_node_write(n2, &op->cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001938 rw_unlock(true, n2);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001939 } else {
1940 trace_bcache_btree_node_compact(b, n1->sets[0].data->keys);
1941
Kent Overstreetcafe5632013-03-23 16:11:31 -07001942 bch_btree_insert_keys(n1, op);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001943 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001944
1945 bch_keylist_add(&op->keys, &n1->key);
Kent Overstreet57943512013-04-25 13:58:35 -07001946 bch_btree_node_write(n1, &op->cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001947
1948 if (n3) {
1949 bkey_copy_key(&n3->key, &MAX_KEY);
1950 bch_btree_insert_keys(n3, op);
Kent Overstreet57943512013-04-25 13:58:35 -07001951 bch_btree_node_write(n3, &op->cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001952
1953 closure_sync(&op->cl);
1954 bch_btree_set_root(n3);
1955 rw_unlock(true, n3);
1956 } else if (root) {
1957 op->keys.top = op->keys.bottom;
1958 closure_sync(&op->cl);
1959 bch_btree_set_root(n1);
1960 } else {
1961 unsigned i;
1962
1963 bkey_copy(op->keys.top, &b->key);
1964 bkey_copy_key(op->keys.top, &ZERO_KEY);
1965
1966 for (i = 0; i < KEY_PTRS(&b->key); i++) {
1967 uint8_t g = PTR_BUCKET(b->c, &b->key, i)->gen + 1;
1968
1969 SET_PTR_GEN(op->keys.top, i, g);
1970 }
1971
1972 bch_keylist_push(&op->keys);
1973 closure_sync(&op->cl);
1974 atomic_inc(&b->c->prio_blocked);
1975 }
1976
1977 rw_unlock(true, n1);
1978 btree_node_free(b, op);
1979
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001980 bch_time_stats_update(&b->c->btree_split_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001981
1982 return 0;
1983err_free2:
1984 __bkey_put(n2->c, &n2->key);
1985 btree_node_free(n2, op);
1986 rw_unlock(true, n2);
1987err_free1:
1988 __bkey_put(n1->c, &n1->key);
1989 btree_node_free(n1, op);
1990 rw_unlock(true, n1);
1991err:
1992 if (n3 == ERR_PTR(-EAGAIN) ||
1993 n2 == ERR_PTR(-EAGAIN) ||
1994 n1 == ERR_PTR(-EAGAIN))
1995 return -EAGAIN;
1996
1997 pr_warn("couldn't split");
1998 return -ENOMEM;
1999}
2000
2001static int bch_btree_insert_recurse(struct btree *b, struct btree_op *op,
2002 struct keylist *stack_keys)
2003{
2004 if (b->level) {
2005 int ret;
2006 struct bkey *insert = op->keys.bottom;
2007 struct bkey *k = bch_next_recurse_key(b, &START_KEY(insert));
2008
2009 if (!k) {
2010 btree_bug(b, "no key to recurse on at level %i/%i",
2011 b->level, b->c->root->level);
2012
2013 op->keys.top = op->keys.bottom;
2014 return -EIO;
2015 }
2016
2017 if (bkey_cmp(insert, k) > 0) {
2018 unsigned i;
2019
2020 if (op->type == BTREE_REPLACE) {
2021 __bkey_put(b->c, insert);
2022 op->keys.top = op->keys.bottom;
2023 op->insert_collision = true;
2024 return 0;
2025 }
2026
2027 for (i = 0; i < KEY_PTRS(insert); i++)
2028 atomic_inc(&PTR_BUCKET(b->c, insert, i)->pin);
2029
2030 bkey_copy(stack_keys->top, insert);
2031
2032 bch_cut_back(k, insert);
2033 bch_cut_front(k, stack_keys->top);
2034
2035 bch_keylist_push(stack_keys);
2036 }
2037
2038 ret = btree(insert_recurse, k, b, op, stack_keys);
2039 if (ret)
2040 return ret;
2041 }
2042
2043 if (!bch_keylist_empty(&op->keys)) {
2044 if (should_split(b)) {
2045 if (op->lock <= b->c->root->level) {
2046 BUG_ON(b->level);
2047 op->lock = b->c->root->level + 1;
2048 return -EINTR;
2049 }
2050 return btree_split(b, op);
2051 }
2052
2053 BUG_ON(write_block(b) != b->sets[b->nsets].data);
2054
Kent Overstreet57943512013-04-25 13:58:35 -07002055 if (bch_btree_insert_keys(b, op)) {
2056 if (!b->level)
2057 bch_btree_leaf_dirty(b, op);
2058 else
2059 bch_btree_node_write(b, &op->cl);
2060 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07002061 }
2062
2063 return 0;
2064}
2065
2066int bch_btree_insert(struct btree_op *op, struct cache_set *c)
2067{
2068 int ret = 0;
2069 struct keylist stack_keys;
2070
2071 /*
2072 * Don't want to block with the btree locked unless we have to,
2073 * otherwise we get deadlocks with try_harder and between split/gc
2074 */
2075 clear_closure_blocking(&op->cl);
2076
2077 BUG_ON(bch_keylist_empty(&op->keys));
2078 bch_keylist_copy(&stack_keys, &op->keys);
2079 bch_keylist_init(&op->keys);
2080
2081 while (!bch_keylist_empty(&stack_keys) ||
2082 !bch_keylist_empty(&op->keys)) {
2083 if (bch_keylist_empty(&op->keys)) {
2084 bch_keylist_add(&op->keys,
2085 bch_keylist_pop(&stack_keys));
2086 op->lock = 0;
2087 }
2088
2089 ret = btree_root(insert_recurse, c, op, &stack_keys);
2090
2091 if (ret == -EAGAIN) {
2092 ret = 0;
2093 closure_sync(&op->cl);
2094 } else if (ret) {
2095 struct bkey *k;
2096
2097 pr_err("error %i trying to insert key for %s",
2098 ret, op_type(op));
2099
2100 while ((k = bch_keylist_pop(&stack_keys) ?:
2101 bch_keylist_pop(&op->keys)))
2102 bkey_put(c, k, 0);
2103 }
2104 }
2105
2106 bch_keylist_free(&stack_keys);
2107
2108 if (op->journal)
2109 atomic_dec_bug(op->journal);
2110 op->journal = NULL;
2111 return ret;
2112}
2113
2114void bch_btree_set_root(struct btree *b)
2115{
2116 unsigned i;
Kent Overstreete49c7c32013-06-26 17:25:38 -07002117 struct closure cl;
2118
2119 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002120
Kent Overstreetc37511b2013-04-26 15:39:55 -07002121 trace_bcache_btree_set_root(b);
2122
Kent Overstreetcafe5632013-03-23 16:11:31 -07002123 BUG_ON(!b->written);
2124
2125 for (i = 0; i < KEY_PTRS(&b->key); i++)
2126 BUG_ON(PTR_BUCKET(b->c, &b->key, i)->prio != BTREE_PRIO);
2127
2128 mutex_lock(&b->c->bucket_lock);
2129 list_del_init(&b->list);
2130 mutex_unlock(&b->c->bucket_lock);
2131
2132 b->c->root = b;
2133 __bkey_put(b->c, &b->key);
2134
Kent Overstreete49c7c32013-06-26 17:25:38 -07002135 bch_journal_meta(b->c, &cl);
2136 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002137}
2138
2139/* Cache lookup */
2140
2141static int submit_partial_cache_miss(struct btree *b, struct btree_op *op,
2142 struct bkey *k)
2143{
2144 struct search *s = container_of(op, struct search, op);
2145 struct bio *bio = &s->bio.bio;
2146 int ret = 0;
2147
2148 while (!ret &&
2149 !op->lookup_done) {
2150 unsigned sectors = INT_MAX;
2151
2152 if (KEY_INODE(k) == op->inode) {
2153 if (KEY_START(k) <= bio->bi_sector)
2154 break;
2155
2156 sectors = min_t(uint64_t, sectors,
2157 KEY_START(k) - bio->bi_sector);
2158 }
2159
2160 ret = s->d->cache_miss(b, s, bio, sectors);
2161 }
2162
2163 return ret;
2164}
2165
2166/*
2167 * Read from a single key, handling the initial cache miss if the key starts in
2168 * the middle of the bio
2169 */
2170static int submit_partial_cache_hit(struct btree *b, struct btree_op *op,
2171 struct bkey *k)
2172{
2173 struct search *s = container_of(op, struct search, op);
2174 struct bio *bio = &s->bio.bio;
2175 unsigned ptr;
2176 struct bio *n;
2177
2178 int ret = submit_partial_cache_miss(b, op, k);
2179 if (ret || op->lookup_done)
2180 return ret;
2181
2182 /* XXX: figure out best pointer - for multiple cache devices */
2183 ptr = 0;
2184
2185 PTR_BUCKET(b->c, k, ptr)->prio = INITIAL_PRIO;
2186
2187 while (!op->lookup_done &&
2188 KEY_INODE(k) == op->inode &&
2189 bio->bi_sector < KEY_OFFSET(k)) {
2190 struct bkey *bio_key;
2191 sector_t sector = PTR_OFFSET(k, ptr) +
2192 (bio->bi_sector - KEY_START(k));
2193 unsigned sectors = min_t(uint64_t, INT_MAX,
2194 KEY_OFFSET(k) - bio->bi_sector);
2195
2196 n = bch_bio_split(bio, sectors, GFP_NOIO, s->d->bio_split);
2197 if (!n)
2198 return -EAGAIN;
2199
2200 if (n == bio)
2201 op->lookup_done = true;
2202
2203 bio_key = &container_of(n, struct bbio, bio)->key;
2204
2205 /*
2206 * The bucket we're reading from might be reused while our bio
2207 * is in flight, and we could then end up reading the wrong
2208 * data.
2209 *
2210 * We guard against this by checking (in cache_read_endio()) if
2211 * the pointer is stale again; if so, we treat it as an error
2212 * and reread from the backing device (but we don't pass that
2213 * error up anywhere).
2214 */
2215
2216 bch_bkey_copy_single_ptr(bio_key, k, ptr);
2217 SET_PTR_OFFSET(bio_key, 0, sector);
2218
2219 n->bi_end_io = bch_cache_read_endio;
2220 n->bi_private = &s->cl;
2221
Kent Overstreetcafe5632013-03-23 16:11:31 -07002222 __bch_submit_bbio(n, b->c);
2223 }
2224
2225 return 0;
2226}
2227
2228int bch_btree_search_recurse(struct btree *b, struct btree_op *op)
2229{
2230 struct search *s = container_of(op, struct search, op);
2231 struct bio *bio = &s->bio.bio;
2232
2233 int ret = 0;
2234 struct bkey *k;
2235 struct btree_iter iter;
2236 bch_btree_iter_init(b, &iter, &KEY(op->inode, bio->bi_sector, 0));
2237
Kent Overstreetcafe5632013-03-23 16:11:31 -07002238 do {
2239 k = bch_btree_iter_next_filter(&iter, b, bch_ptr_bad);
2240 if (!k) {
2241 /*
2242 * b->key would be exactly what we want, except that
2243 * pointers to btree nodes have nonzero size - we
2244 * wouldn't go far enough
2245 */
2246
2247 ret = submit_partial_cache_miss(b, op,
2248 &KEY(KEY_INODE(&b->key),
2249 KEY_OFFSET(&b->key), 0));
2250 break;
2251 }
2252
2253 ret = b->level
2254 ? btree(search_recurse, k, b, op)
2255 : submit_partial_cache_hit(b, op, k);
2256 } while (!ret &&
2257 !op->lookup_done);
2258
2259 return ret;
2260}
2261
2262/* Keybuf code */
2263
2264static inline int keybuf_cmp(struct keybuf_key *l, struct keybuf_key *r)
2265{
2266 /* Overlapping keys compare equal */
2267 if (bkey_cmp(&l->key, &START_KEY(&r->key)) <= 0)
2268 return -1;
2269 if (bkey_cmp(&START_KEY(&l->key), &r->key) >= 0)
2270 return 1;
2271 return 0;
2272}
2273
2274static inline int keybuf_nonoverlapping_cmp(struct keybuf_key *l,
2275 struct keybuf_key *r)
2276{
2277 return clamp_t(int64_t, bkey_cmp(&l->key, &r->key), -1, 1);
2278}
2279
2280static int bch_btree_refill_keybuf(struct btree *b, struct btree_op *op,
Kent Overstreet72c27062013-06-05 06:24:39 -07002281 struct keybuf *buf, struct bkey *end,
2282 keybuf_pred_fn *pred)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002283{
2284 struct btree_iter iter;
2285 bch_btree_iter_init(b, &iter, &buf->last_scanned);
2286
2287 while (!array_freelist_empty(&buf->freelist)) {
2288 struct bkey *k = bch_btree_iter_next_filter(&iter, b,
2289 bch_ptr_bad);
2290
2291 if (!b->level) {
2292 if (!k) {
2293 buf->last_scanned = b->key;
2294 break;
2295 }
2296
2297 buf->last_scanned = *k;
2298 if (bkey_cmp(&buf->last_scanned, end) >= 0)
2299 break;
2300
Kent Overstreet72c27062013-06-05 06:24:39 -07002301 if (pred(buf, k)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07002302 struct keybuf_key *w;
2303
Kent Overstreetcafe5632013-03-23 16:11:31 -07002304 spin_lock(&buf->lock);
2305
2306 w = array_alloc(&buf->freelist);
2307
2308 w->private = NULL;
2309 bkey_copy(&w->key, k);
2310
2311 if (RB_INSERT(&buf->keys, w, node, keybuf_cmp))
2312 array_free(&buf->freelist, w);
2313
2314 spin_unlock(&buf->lock);
2315 }
2316 } else {
2317 if (!k)
2318 break;
2319
Kent Overstreet72c27062013-06-05 06:24:39 -07002320 btree(refill_keybuf, k, b, op, buf, end, pred);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002321 /*
2322 * Might get an error here, but can't really do anything
2323 * and it'll get logged elsewhere. Just read what we
2324 * can.
2325 */
2326
2327 if (bkey_cmp(&buf->last_scanned, end) >= 0)
2328 break;
2329
2330 cond_resched();
2331 }
2332 }
2333
2334 return 0;
2335}
2336
2337void bch_refill_keybuf(struct cache_set *c, struct keybuf *buf,
Kent Overstreet72c27062013-06-05 06:24:39 -07002338 struct bkey *end, keybuf_pred_fn *pred)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002339{
2340 struct bkey start = buf->last_scanned;
2341 struct btree_op op;
2342 bch_btree_op_init_stack(&op);
2343
2344 cond_resched();
2345
Kent Overstreet72c27062013-06-05 06:24:39 -07002346 btree_root(refill_keybuf, c, &op, buf, end, pred);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002347 closure_sync(&op.cl);
2348
2349 pr_debug("found %s keys from %llu:%llu to %llu:%llu",
2350 RB_EMPTY_ROOT(&buf->keys) ? "no" :
2351 array_freelist_empty(&buf->freelist) ? "some" : "a few",
2352 KEY_INODE(&start), KEY_OFFSET(&start),
2353 KEY_INODE(&buf->last_scanned), KEY_OFFSET(&buf->last_scanned));
2354
2355 spin_lock(&buf->lock);
2356
2357 if (!RB_EMPTY_ROOT(&buf->keys)) {
2358 struct keybuf_key *w;
2359 w = RB_FIRST(&buf->keys, struct keybuf_key, node);
2360 buf->start = START_KEY(&w->key);
2361
2362 w = RB_LAST(&buf->keys, struct keybuf_key, node);
2363 buf->end = w->key;
2364 } else {
2365 buf->start = MAX_KEY;
2366 buf->end = MAX_KEY;
2367 }
2368
2369 spin_unlock(&buf->lock);
2370}
2371
2372static void __bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w)
2373{
2374 rb_erase(&w->node, &buf->keys);
2375 array_free(&buf->freelist, w);
2376}
2377
2378void bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w)
2379{
2380 spin_lock(&buf->lock);
2381 __bch_keybuf_del(buf, w);
2382 spin_unlock(&buf->lock);
2383}
2384
2385bool bch_keybuf_check_overlapping(struct keybuf *buf, struct bkey *start,
2386 struct bkey *end)
2387{
2388 bool ret = false;
2389 struct keybuf_key *p, *w, s;
2390 s.key = *start;
2391
2392 if (bkey_cmp(end, &buf->start) <= 0 ||
2393 bkey_cmp(start, &buf->end) >= 0)
2394 return false;
2395
2396 spin_lock(&buf->lock);
2397 w = RB_GREATER(&buf->keys, s, node, keybuf_nonoverlapping_cmp);
2398
2399 while (w && bkey_cmp(&START_KEY(&w->key), end) < 0) {
2400 p = w;
2401 w = RB_NEXT(w, node);
2402
2403 if (p->private)
2404 ret = true;
2405 else
2406 __bch_keybuf_del(buf, p);
2407 }
2408
2409 spin_unlock(&buf->lock);
2410 return ret;
2411}
2412
2413struct keybuf_key *bch_keybuf_next(struct keybuf *buf)
2414{
2415 struct keybuf_key *w;
2416 spin_lock(&buf->lock);
2417
2418 w = RB_FIRST(&buf->keys, struct keybuf_key, node);
2419
2420 while (w && w->private)
2421 w = RB_NEXT(w, node);
2422
2423 if (w)
2424 w->private = ERR_PTR(-EINTR);
2425
2426 spin_unlock(&buf->lock);
2427 return w;
2428}
2429
2430struct keybuf_key *bch_keybuf_next_rescan(struct cache_set *c,
2431 struct keybuf *buf,
Kent Overstreet72c27062013-06-05 06:24:39 -07002432 struct bkey *end,
2433 keybuf_pred_fn *pred)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002434{
2435 struct keybuf_key *ret;
2436
2437 while (1) {
2438 ret = bch_keybuf_next(buf);
2439 if (ret)
2440 break;
2441
2442 if (bkey_cmp(&buf->last_scanned, end) >= 0) {
2443 pr_debug("scan finished");
2444 break;
2445 }
2446
Kent Overstreet72c27062013-06-05 06:24:39 -07002447 bch_refill_keybuf(c, buf, end, pred);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002448 }
2449
2450 return ret;
2451}
2452
Kent Overstreet72c27062013-06-05 06:24:39 -07002453void bch_keybuf_init(struct keybuf *buf)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002454{
Kent Overstreetcafe5632013-03-23 16:11:31 -07002455 buf->last_scanned = MAX_KEY;
2456 buf->keys = RB_ROOT;
2457
2458 spin_lock_init(&buf->lock);
2459 array_allocator_init(&buf->freelist);
2460}
2461
2462void bch_btree_exit(void)
2463{
2464 if (btree_io_wq)
2465 destroy_workqueue(btree_io_wq);
2466 if (bch_gc_wq)
2467 destroy_workqueue(bch_gc_wq);
2468}
2469
2470int __init bch_btree_init(void)
2471{
2472 if (!(bch_gc_wq = create_singlethread_workqueue("bch_btree_gc")) ||
2473 !(btree_io_wq = create_singlethread_workqueue("bch_btree_io")))
2474 return -ENOMEM;
2475
2476 return 0;
2477}