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