blob: aba787d954e5739e82e1c2b5c9794d4023f2917f [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"
Kent Overstreet279afba2013-06-05 06:21:07 -070026#include "writeback.h"
Kent Overstreetcafe5632013-03-23 16:11:31 -070027
28#include <linux/slab.h>
29#include <linux/bitops.h>
Kent Overstreet72a44512013-10-24 17:19:26 -070030#include <linux/freezer.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070031#include <linux/hash.h>
Kent Overstreet72a44512013-10-24 17:19:26 -070032#include <linux/kthread.h>
Geert Uytterhoevencd953ed2013-03-27 18:56:28 +010033#include <linux/prefetch.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070034#include <linux/random.h>
35#include <linux/rcupdate.h>
36#include <trace/events/bcache.h>
37
38/*
39 * Todo:
40 * register_bcache: Return errors out to userspace correctly
41 *
42 * Writeback: don't undirty key until after a cache flush
43 *
44 * Create an iterator for key pointers
45 *
46 * On btree write error, mark bucket such that it won't be freed from the cache
47 *
48 * Journalling:
49 * Check for bad keys in replay
50 * Propagate barriers
51 * Refcount journal entries in journal_replay
52 *
53 * Garbage collection:
54 * Finish incremental gc
55 * Gc should free old UUIDs, data for invalid UUIDs
56 *
57 * Provide a way to list backing device UUIDs we have data cached for, and
58 * probably how long it's been since we've seen them, and a way to invalidate
59 * dirty data for devices that will never be attached again
60 *
61 * Keep 1 min/5 min/15 min statistics of how busy a block device has been, so
62 * that based on that and how much dirty data we have we can keep writeback
63 * from being starved
64 *
65 * Add a tracepoint or somesuch to watch for writeback starvation
66 *
67 * When btree depth > 1 and splitting an interior node, we have to make sure
68 * alloc_bucket() cannot fail. This should be true but is not completely
69 * obvious.
70 *
71 * Make sure all allocations get charged to the root cgroup
72 *
73 * Plugging?
74 *
75 * If data write is less than hard sector size of ssd, round up offset in open
76 * bucket to the next whole sector
77 *
78 * Also lookup by cgroup in get_open_bucket()
79 *
80 * Superblock needs to be fleshed out for multiple cache devices
81 *
82 * Add a sysfs tunable for the number of writeback IOs in flight
83 *
84 * Add a sysfs tunable for the number of open data buckets
85 *
86 * IO tracking: Can we track when one process is doing io on behalf of another?
87 * IO tracking: Don't use just an average, weigh more recent stuff higher
88 *
89 * Test module load/unload
90 */
91
Kent Overstreetdf8e8972013-07-24 17:37:59 -070092enum {
93 BTREE_INSERT_STATUS_INSERT,
94 BTREE_INSERT_STATUS_BACK_MERGE,
95 BTREE_INSERT_STATUS_OVERWROTE,
96 BTREE_INSERT_STATUS_FRONT_MERGE,
97};
98
Kent Overstreetcafe5632013-03-23 16:11:31 -070099#define MAX_NEED_GC 64
100#define MAX_SAVE_PRIO 72
101
102#define PTR_DIRTY_BIT (((uint64_t) 1 << 36))
103
104#define PTR_HASH(c, k) \
105 (((k)->ptr[0] >> c->bucket_bits) | PTR_GEN(k, 0))
106
Kent Overstreetcafe5632013-03-23 16:11:31 -0700107static struct workqueue_struct *btree_io_wq;
108
Kent Overstreetdf8e8972013-07-24 17:37:59 -0700109static inline bool should_split(struct btree *b)
110{
111 struct bset *i = write_block(b);
112 return b->written >= btree_blocks(b) ||
113 (b->written + __set_blocks(i, i->keys + 15, b->c)
114 > btree_blocks(b));
115}
116
117#define insert_lock(s, b) ((b)->level <= (s)->lock)
118
119/*
120 * These macros are for recursing down the btree - they handle the details of
121 * locking and looking up nodes in the cache for you. They're best treated as
122 * mere syntax when reading code that uses them.
123 *
124 * op->lock determines whether we take a read or a write lock at a given depth.
125 * If you've got a read lock and find that you need a write lock (i.e. you're
126 * going to have to split), set op->lock and return -EINTR; btree_root() will
127 * call you again and you'll have the correct lock.
128 */
129
130/**
131 * btree - recurse down the btree on a specified key
132 * @fn: function to call, which will be passed the child node
133 * @key: key to recurse on
134 * @b: parent btree node
135 * @op: pointer to struct btree_op
136 */
137#define btree(fn, key, b, op, ...) \
138({ \
139 int _r, l = (b)->level - 1; \
140 bool _w = l <= (op)->lock; \
141 struct btree *_child = bch_btree_node_get((b)->c, key, l, _w); \
142 if (!IS_ERR(_child)) { \
143 _child->parent = (b); \
144 _r = bch_btree_ ## fn(_child, op, ##__VA_ARGS__); \
145 rw_unlock(_w, _child); \
146 } else \
147 _r = PTR_ERR(_child); \
148 _r; \
149})
150
151/**
152 * btree_root - call a function on the root of the btree
153 * @fn: function to call, which will be passed the child node
154 * @c: cache set
155 * @op: pointer to struct btree_op
156 */
157#define btree_root(fn, c, op, ...) \
158({ \
159 int _r = -EINTR; \
160 do { \
161 struct btree *_b = (c)->root; \
162 bool _w = insert_lock(op, _b); \
163 rw_lock(_w, _b, _b->level); \
164 if (_b == (c)->root && \
165 _w == insert_lock(op, _b)) { \
166 _b->parent = NULL; \
167 _r = bch_btree_ ## fn(_b, op, ##__VA_ARGS__); \
168 } \
169 rw_unlock(_w, _b); \
170 bch_cannibalize_unlock(c); \
171 if (_r == -ENOSPC) { \
172 wait_event((c)->try_wait, \
173 !(c)->try_harder); \
174 _r = -EINTR; \
175 } \
176 } while (_r == -EINTR); \
177 \
178 _r; \
179})
180
Kent Overstreetcafe5632013-03-23 16:11:31 -0700181/* Btree key manipulation */
182
Kent Overstreete7c590e2013-09-10 18:39:16 -0700183void __bkey_put(struct cache_set *c, struct bkey *k)
184{
185 unsigned i;
186
187 for (i = 0; i < KEY_PTRS(k); i++)
188 if (ptr_available(c, k, i))
189 atomic_dec_bug(&PTR_BUCKET(c, k, i)->pin);
190}
191
Kent Overstreetcafe5632013-03-23 16:11:31 -0700192static void bkey_put(struct cache_set *c, struct bkey *k, int level)
193{
194 if ((level && KEY_OFFSET(k)) || !level)
195 __bkey_put(c, k);
196}
197
198/* Btree IO */
199
200static uint64_t btree_csum_set(struct btree *b, struct bset *i)
201{
202 uint64_t crc = b->key.ptr[0];
203 void *data = (void *) i + 8, *end = end(i);
204
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600205 crc = bch_crc64_update(crc, data, end - data);
Kent Overstreetc19ed232013-03-26 13:49:02 -0700206 return crc ^ 0xffffffffffffffffULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700207}
208
Kent Overstreetf3059a52013-05-15 17:13:45 -0700209static void bch_btree_node_read_done(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700210{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700211 const char *err = "bad btree header";
Kent Overstreet57943512013-04-25 13:58:35 -0700212 struct bset *i = b->sets[0].data;
213 struct btree_iter *iter;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700214
Kent Overstreet57943512013-04-25 13:58:35 -0700215 iter = mempool_alloc(b->c->fill_iter, GFP_NOWAIT);
216 iter->size = b->c->sb.bucket_size / b->c->sb.block_size;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700217 iter->used = 0;
218
Kent Overstreet57943512013-04-25 13:58:35 -0700219 if (!i->seq)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700220 goto err;
221
222 for (;
223 b->written < btree_blocks(b) && i->seq == b->sets[0].data->seq;
224 i = write_block(b)) {
225 err = "unsupported bset version";
226 if (i->version > BCACHE_BSET_VERSION)
227 goto err;
228
229 err = "bad btree header";
230 if (b->written + set_blocks(i, b->c) > btree_blocks(b))
231 goto err;
232
233 err = "bad magic";
Kent Overstreet81ab4192013-10-31 15:46:42 -0700234 if (i->magic != bset_magic(&b->c->sb))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700235 goto err;
236
237 err = "bad checksum";
238 switch (i->version) {
239 case 0:
240 if (i->csum != csum_set(i))
241 goto err;
242 break;
243 case BCACHE_BSET_VERSION:
244 if (i->csum != btree_csum_set(b, i))
245 goto err;
246 break;
247 }
248
249 err = "empty set";
250 if (i != b->sets[0].data && !i->keys)
251 goto err;
252
253 bch_btree_iter_push(iter, i->start, end(i));
254
255 b->written += set_blocks(i, b->c);
256 }
257
258 err = "corrupted btree";
259 for (i = write_block(b);
260 index(i, b) < btree_blocks(b);
261 i = ((void *) i) + block_bytes(b->c))
262 if (i->seq == b->sets[0].data->seq)
263 goto err;
264
265 bch_btree_sort_and_fix_extents(b, iter);
266
267 i = b->sets[0].data;
268 err = "short btree key";
269 if (b->sets[0].size &&
270 bkey_cmp(&b->key, &b->sets[0].end) < 0)
271 goto err;
272
273 if (b->written < btree_blocks(b))
274 bch_bset_init_next(b);
275out:
Kent Overstreet57943512013-04-25 13:58:35 -0700276 mempool_free(iter, b->c->fill_iter);
277 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700278err:
279 set_btree_node_io_error(b);
Kent Overstreet07e86cc2013-03-25 11:46:43 -0700280 bch_cache_set_error(b->c, "%s at bucket %zu, block %zu, %u keys",
Kent Overstreetcafe5632013-03-23 16:11:31 -0700281 err, PTR_BUCKET_NR(b->c, &b->key, 0),
282 index(i, b), i->keys);
283 goto out;
284}
285
Kent Overstreet57943512013-04-25 13:58:35 -0700286static void btree_node_read_endio(struct bio *bio, int error)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700287{
Kent Overstreet57943512013-04-25 13:58:35 -0700288 struct closure *cl = bio->bi_private;
289 closure_put(cl);
290}
Kent Overstreetcafe5632013-03-23 16:11:31 -0700291
Kent Overstreet57943512013-04-25 13:58:35 -0700292void bch_btree_node_read(struct btree *b)
293{
294 uint64_t start_time = local_clock();
295 struct closure cl;
296 struct bio *bio;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700297
Kent Overstreetc37511b2013-04-26 15:39:55 -0700298 trace_bcache_btree_read(b);
299
Kent Overstreet57943512013-04-25 13:58:35 -0700300 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700301
Kent Overstreet57943512013-04-25 13:58:35 -0700302 bio = bch_bbio_alloc(b->c);
303 bio->bi_rw = REQ_META|READ_SYNC;
304 bio->bi_size = KEY_SIZE(&b->key) << 9;
305 bio->bi_end_io = btree_node_read_endio;
306 bio->bi_private = &cl;
307
308 bch_bio_map(bio, b->sets[0].data);
309
Kent Overstreet57943512013-04-25 13:58:35 -0700310 bch_submit_bbio(bio, b->c, &b->key, 0);
311 closure_sync(&cl);
312
313 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
314 set_btree_node_io_error(b);
315
316 bch_bbio_free(bio, b->c);
317
318 if (btree_node_io_error(b))
319 goto err;
320
321 bch_btree_node_read_done(b);
322
323 spin_lock(&b->c->btree_read_time_lock);
324 bch_time_stats_update(&b->c->btree_read_time, start_time);
325 spin_unlock(&b->c->btree_read_time_lock);
326
327 return;
328err:
Geert Uytterhoeven61cbd252013-09-23 23:17:30 -0700329 bch_cache_set_error(b->c, "io error reading bucket %zu",
Kent Overstreet57943512013-04-25 13:58:35 -0700330 PTR_BUCKET_NR(b->c, &b->key, 0));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700331}
332
333static void btree_complete_write(struct btree *b, struct btree_write *w)
334{
335 if (w->prio_blocked &&
336 !atomic_sub_return(w->prio_blocked, &b->c->prio_blocked))
Kent Overstreet119ba0f2013-04-24 19:01:12 -0700337 wake_up_allocators(b->c);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700338
339 if (w->journal) {
340 atomic_dec_bug(w->journal);
341 __closure_wake_up(&b->c->journal.wait);
342 }
343
Kent Overstreetcafe5632013-03-23 16:11:31 -0700344 w->prio_blocked = 0;
345 w->journal = NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700346}
347
Kent Overstreet57943512013-04-25 13:58:35 -0700348static void __btree_node_write_done(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700349{
350 struct btree *b = container_of(cl, struct btree, io.cl);
351 struct btree_write *w = btree_prev_write(b);
352
353 bch_bbio_free(b->bio, b->c);
354 b->bio = NULL;
355 btree_complete_write(b, w);
356
357 if (btree_node_dirty(b))
358 queue_delayed_work(btree_io_wq, &b->work,
359 msecs_to_jiffies(30000));
360
361 closure_return(cl);
362}
363
Kent Overstreet57943512013-04-25 13:58:35 -0700364static void btree_node_write_done(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700365{
366 struct btree *b = container_of(cl, struct btree, io.cl);
367 struct bio_vec *bv;
368 int n;
369
370 __bio_for_each_segment(bv, b->bio, n, 0)
371 __free_page(bv->bv_page);
372
Kent Overstreet57943512013-04-25 13:58:35 -0700373 __btree_node_write_done(cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700374}
375
Kent Overstreet57943512013-04-25 13:58:35 -0700376static void btree_node_write_endio(struct bio *bio, int error)
377{
378 struct closure *cl = bio->bi_private;
379 struct btree *b = container_of(cl, struct btree, io.cl);
380
381 if (error)
382 set_btree_node_io_error(b);
383
384 bch_bbio_count_io_errors(b->c, bio, error, "writing btree");
385 closure_put(cl);
386}
387
388static void do_btree_node_write(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700389{
390 struct closure *cl = &b->io.cl;
391 struct bset *i = b->sets[b->nsets].data;
392 BKEY_PADDED(key) k;
393
394 i->version = BCACHE_BSET_VERSION;
395 i->csum = btree_csum_set(b, i);
396
Kent Overstreet57943512013-04-25 13:58:35 -0700397 BUG_ON(b->bio);
398 b->bio = bch_bbio_alloc(b->c);
399
400 b->bio->bi_end_io = btree_node_write_endio;
Kent Overstreetfaadf0c2013-11-01 18:03:08 -0700401 b->bio->bi_private = cl;
Kent Overstreete49c7c32013-06-26 17:25:38 -0700402 b->bio->bi_rw = REQ_META|WRITE_SYNC|REQ_FUA;
403 b->bio->bi_size = set_blocks(i, b->c) * block_bytes(b->c);
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600404 bch_bio_map(b->bio, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700405
Kent Overstreete49c7c32013-06-26 17:25:38 -0700406 /*
407 * If we're appending to a leaf node, we don't technically need FUA -
408 * this write just needs to be persisted before the next journal write,
409 * which will be marked FLUSH|FUA.
410 *
411 * Similarly if we're writing a new btree root - the pointer is going to
412 * be in the next journal entry.
413 *
414 * But if we're writing a new btree node (that isn't a root) or
415 * appending to a non leaf btree node, we need either FUA or a flush
416 * when we write the parent with the new pointer. FUA is cheaper than a
417 * flush, and writes appending to leaf nodes aren't blocking anything so
418 * just make all btree node writes FUA to keep things sane.
419 */
420
Kent Overstreetcafe5632013-03-23 16:11:31 -0700421 bkey_copy(&k.key, &b->key);
422 SET_PTR_OFFSET(&k.key, 0, PTR_OFFSET(&k.key, 0) + bset_offset(b, i));
423
Kent Overstreet8e51e412013-06-06 18:15:57 -0700424 if (!bio_alloc_pages(b->bio, GFP_NOIO)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700425 int j;
426 struct bio_vec *bv;
427 void *base = (void *) ((unsigned long) i & ~(PAGE_SIZE - 1));
428
429 bio_for_each_segment(bv, b->bio, j)
430 memcpy(page_address(bv->bv_page),
431 base + j * PAGE_SIZE, PAGE_SIZE);
432
Kent Overstreetcafe5632013-03-23 16:11:31 -0700433 bch_submit_bbio(b->bio, b->c, &k.key, 0);
434
Kent Overstreet57943512013-04-25 13:58:35 -0700435 continue_at(cl, btree_node_write_done, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700436 } else {
437 b->bio->bi_vcnt = 0;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600438 bch_bio_map(b->bio, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700439
Kent Overstreetcafe5632013-03-23 16:11:31 -0700440 bch_submit_bbio(b->bio, b->c, &k.key, 0);
441
442 closure_sync(cl);
Kent Overstreet57943512013-04-25 13:58:35 -0700443 __btree_node_write_done(cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700444 }
445}
446
Kent Overstreet57943512013-04-25 13:58:35 -0700447void bch_btree_node_write(struct btree *b, struct closure *parent)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700448{
449 struct bset *i = b->sets[b->nsets].data;
450
Kent Overstreetc37511b2013-04-26 15:39:55 -0700451 trace_bcache_btree_write(b);
452
Kent Overstreetcafe5632013-03-23 16:11:31 -0700453 BUG_ON(current->bio_list);
Kent Overstreet57943512013-04-25 13:58:35 -0700454 BUG_ON(b->written >= btree_blocks(b));
455 BUG_ON(b->written && !i->keys);
456 BUG_ON(b->sets->data->seq != i->seq);
Kent Overstreetc37511b2013-04-26 15:39:55 -0700457 bch_check_key_order(b, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700458
Kent Overstreetcafe5632013-03-23 16:11:31 -0700459 cancel_delayed_work(&b->work);
460
Kent Overstreet57943512013-04-25 13:58:35 -0700461 /* If caller isn't waiting for write, parent refcount is cache set */
462 closure_lock(&b->io, parent ?: &b->c->cl);
463
Kent Overstreetcafe5632013-03-23 16:11:31 -0700464 clear_bit(BTREE_NODE_dirty, &b->flags);
465 change_bit(BTREE_NODE_write_idx, &b->flags);
466
Kent Overstreet57943512013-04-25 13:58:35 -0700467 do_btree_node_write(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700468
Kent Overstreetcafe5632013-03-23 16:11:31 -0700469 b->written += set_blocks(i, b->c);
470 atomic_long_add(set_blocks(i, b->c) * b->c->sb.block_size,
471 &PTR_CACHE(b->c, &b->key, 0)->btree_sectors_written);
472
473 bch_btree_sort_lazy(b);
474
475 if (b->written < btree_blocks(b))
476 bch_bset_init_next(b);
477}
478
Kent Overstreet57943512013-04-25 13:58:35 -0700479static void btree_node_write_work(struct work_struct *w)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700480{
481 struct btree *b = container_of(to_delayed_work(w), struct btree, work);
482
Kent Overstreet57943512013-04-25 13:58:35 -0700483 rw_lock(true, b, b->level);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700484
485 if (btree_node_dirty(b))
Kent Overstreet57943512013-04-25 13:58:35 -0700486 bch_btree_node_write(b, NULL);
487 rw_unlock(true, b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700488}
489
Kent Overstreetc18536a2013-07-24 17:44:17 -0700490static void bch_btree_leaf_dirty(struct btree *b, atomic_t *journal_ref)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700491{
492 struct bset *i = b->sets[b->nsets].data;
493 struct btree_write *w = btree_current_write(b);
494
Kent Overstreet57943512013-04-25 13:58:35 -0700495 BUG_ON(!b->written);
496 BUG_ON(!i->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700497
Kent Overstreet57943512013-04-25 13:58:35 -0700498 if (!btree_node_dirty(b))
499 queue_delayed_work(btree_io_wq, &b->work, 30 * HZ);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700500
Kent Overstreet57943512013-04-25 13:58:35 -0700501 set_btree_node_dirty(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700502
Kent Overstreetc18536a2013-07-24 17:44:17 -0700503 if (journal_ref) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700504 if (w->journal &&
Kent Overstreetc18536a2013-07-24 17:44:17 -0700505 journal_pin_cmp(b->c, w->journal, journal_ref)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700506 atomic_dec_bug(w->journal);
507 w->journal = NULL;
508 }
509
510 if (!w->journal) {
Kent Overstreetc18536a2013-07-24 17:44:17 -0700511 w->journal = journal_ref;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700512 atomic_inc(w->journal);
513 }
514 }
515
Kent Overstreetcafe5632013-03-23 16:11:31 -0700516 /* Force write if set is too big */
Kent Overstreet57943512013-04-25 13:58:35 -0700517 if (set_bytes(i) > PAGE_SIZE - 48 &&
518 !current->bio_list)
519 bch_btree_node_write(b, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700520}
521
522/*
523 * Btree in memory cache - allocation/freeing
524 * mca -> memory cache
525 */
526
527static void mca_reinit(struct btree *b)
528{
529 unsigned i;
530
531 b->flags = 0;
532 b->written = 0;
533 b->nsets = 0;
534
535 for (i = 0; i < MAX_BSETS; i++)
536 b->sets[i].size = 0;
537 /*
538 * Second loop starts at 1 because b->sets[0]->data is the memory we
539 * allocated
540 */
541 for (i = 1; i < MAX_BSETS; i++)
542 b->sets[i].data = NULL;
543}
544
545#define mca_reserve(c) (((c->root && c->root->level) \
546 ? c->root->level : 1) * 8 + 16)
547#define mca_can_free(c) \
548 max_t(int, 0, c->bucket_cache_used - mca_reserve(c))
549
550static void mca_data_free(struct btree *b)
551{
552 struct bset_tree *t = b->sets;
553 BUG_ON(!closure_is_unlocked(&b->io.cl));
554
555 if (bset_prev_bytes(b) < PAGE_SIZE)
556 kfree(t->prev);
557 else
558 free_pages((unsigned long) t->prev,
559 get_order(bset_prev_bytes(b)));
560
561 if (bset_tree_bytes(b) < PAGE_SIZE)
562 kfree(t->tree);
563 else
564 free_pages((unsigned long) t->tree,
565 get_order(bset_tree_bytes(b)));
566
567 free_pages((unsigned long) t->data, b->page_order);
568
569 t->prev = NULL;
570 t->tree = NULL;
571 t->data = NULL;
572 list_move(&b->list, &b->c->btree_cache_freed);
573 b->c->bucket_cache_used--;
574}
575
576static void mca_bucket_free(struct btree *b)
577{
578 BUG_ON(btree_node_dirty(b));
579
580 b->key.ptr[0] = 0;
581 hlist_del_init_rcu(&b->hash);
582 list_move(&b->list, &b->c->btree_cache_freeable);
583}
584
585static unsigned btree_order(struct bkey *k)
586{
587 return ilog2(KEY_SIZE(k) / PAGE_SECTORS ?: 1);
588}
589
590static void mca_data_alloc(struct btree *b, struct bkey *k, gfp_t gfp)
591{
592 struct bset_tree *t = b->sets;
593 BUG_ON(t->data);
594
595 b->page_order = max_t(unsigned,
596 ilog2(b->c->btree_pages),
597 btree_order(k));
598
599 t->data = (void *) __get_free_pages(gfp, b->page_order);
600 if (!t->data)
601 goto err;
602
603 t->tree = bset_tree_bytes(b) < PAGE_SIZE
604 ? kmalloc(bset_tree_bytes(b), gfp)
605 : (void *) __get_free_pages(gfp, get_order(bset_tree_bytes(b)));
606 if (!t->tree)
607 goto err;
608
609 t->prev = bset_prev_bytes(b) < PAGE_SIZE
610 ? kmalloc(bset_prev_bytes(b), gfp)
611 : (void *) __get_free_pages(gfp, get_order(bset_prev_bytes(b)));
612 if (!t->prev)
613 goto err;
614
615 list_move(&b->list, &b->c->btree_cache);
616 b->c->bucket_cache_used++;
617 return;
618err:
619 mca_data_free(b);
620}
621
622static struct btree *mca_bucket_alloc(struct cache_set *c,
623 struct bkey *k, gfp_t gfp)
624{
625 struct btree *b = kzalloc(sizeof(struct btree), gfp);
626 if (!b)
627 return NULL;
628
629 init_rwsem(&b->lock);
630 lockdep_set_novalidate_class(&b->lock);
631 INIT_LIST_HEAD(&b->list);
Kent Overstreet57943512013-04-25 13:58:35 -0700632 INIT_DELAYED_WORK(&b->work, btree_node_write_work);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700633 b->c = c;
634 closure_init_unlocked(&b->io);
635
636 mca_data_alloc(b, k, gfp);
637 return b;
638}
639
Kent Overstreete8e1d462013-07-24 17:27:07 -0700640static int mca_reap(struct btree *b, unsigned min_order, bool flush)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700641{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700642 struct closure cl;
643
644 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700645 lockdep_assert_held(&b->c->bucket_lock);
646
647 if (!down_write_trylock(&b->lock))
648 return -ENOMEM;
649
Kent Overstreete8e1d462013-07-24 17:27:07 -0700650 BUG_ON(btree_node_dirty(b) && !b->sets[0].data);
651
652 if (b->page_order < min_order ||
653 (!flush &&
654 (btree_node_dirty(b) ||
655 atomic_read(&b->io.cl.remaining) != -1))) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700656 rw_unlock(true, b);
657 return -ENOMEM;
658 }
659
Kent Overstreete8e1d462013-07-24 17:27:07 -0700660 if (btree_node_dirty(b)) {
661 bch_btree_node_write(b, &cl);
662 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700663 }
664
Kent Overstreete8e1d462013-07-24 17:27:07 -0700665 /* wait for any in flight btree write */
Kent Overstreetfaadf0c2013-11-01 18:03:08 -0700666 closure_wait_event(&b->io.wait, &cl,
667 atomic_read(&b->io.cl.remaining) == -1);
Kent Overstreete8e1d462013-07-24 17:27:07 -0700668
Kent Overstreetcafe5632013-03-23 16:11:31 -0700669 return 0;
670}
671
Dave Chinner7dc19d52013-08-28 10:18:11 +1000672static unsigned long bch_mca_scan(struct shrinker *shrink,
673 struct shrink_control *sc)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700674{
675 struct cache_set *c = container_of(shrink, struct cache_set, shrink);
676 struct btree *b, *t;
677 unsigned long i, nr = sc->nr_to_scan;
Dave Chinner7dc19d52013-08-28 10:18:11 +1000678 unsigned long freed = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700679
680 if (c->shrinker_disabled)
Dave Chinner7dc19d52013-08-28 10:18:11 +1000681 return SHRINK_STOP;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700682
683 if (c->try_harder)
Dave Chinner7dc19d52013-08-28 10:18:11 +1000684 return SHRINK_STOP;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700685
686 /* Return -1 if we can't do anything right now */
Kent Overstreeta698e082013-09-23 23:17:34 -0700687 if (sc->gfp_mask & __GFP_IO)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700688 mutex_lock(&c->bucket_lock);
689 else if (!mutex_trylock(&c->bucket_lock))
690 return -1;
691
Kent Overstreet36c9ea92013-06-03 13:04:56 -0700692 /*
693 * It's _really_ critical that we don't free too many btree nodes - we
694 * have to always leave ourselves a reserve. The reserve is how we
695 * guarantee that allocating memory for a new btree node can always
696 * succeed, so that inserting keys into the btree can always succeed and
697 * IO can always make forward progress:
698 */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700699 nr /= c->btree_pages;
700 nr = min_t(unsigned long, nr, mca_can_free(c));
701
702 i = 0;
703 list_for_each_entry_safe(b, t, &c->btree_cache_freeable, list) {
Dave Chinner7dc19d52013-08-28 10:18:11 +1000704 if (freed >= nr)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700705 break;
706
707 if (++i > 3 &&
Kent Overstreete8e1d462013-07-24 17:27:07 -0700708 !mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700709 mca_data_free(b);
710 rw_unlock(true, b);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000711 freed++;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700712 }
713 }
714
715 /*
716 * Can happen right when we first start up, before we've read in any
717 * btree nodes
718 */
719 if (list_empty(&c->btree_cache))
720 goto out;
721
Dave Chinner7dc19d52013-08-28 10:18:11 +1000722 for (i = 0; (nr--) && i < c->bucket_cache_used; i++) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700723 b = list_first_entry(&c->btree_cache, struct btree, list);
724 list_rotate_left(&c->btree_cache);
725
726 if (!b->accessed &&
Kent Overstreete8e1d462013-07-24 17:27:07 -0700727 !mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700728 mca_bucket_free(b);
729 mca_data_free(b);
730 rw_unlock(true, b);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000731 freed++;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700732 } else
733 b->accessed = 0;
734 }
735out:
Kent Overstreetcafe5632013-03-23 16:11:31 -0700736 mutex_unlock(&c->bucket_lock);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000737 return freed;
738}
739
740static unsigned long bch_mca_count(struct shrinker *shrink,
741 struct shrink_control *sc)
742{
743 struct cache_set *c = container_of(shrink, struct cache_set, shrink);
744
745 if (c->shrinker_disabled)
746 return 0;
747
748 if (c->try_harder)
749 return 0;
750
751 return mca_can_free(c) * c->btree_pages;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700752}
753
754void bch_btree_cache_free(struct cache_set *c)
755{
756 struct btree *b;
757 struct closure cl;
758 closure_init_stack(&cl);
759
760 if (c->shrink.list.next)
761 unregister_shrinker(&c->shrink);
762
763 mutex_lock(&c->bucket_lock);
764
765#ifdef CONFIG_BCACHE_DEBUG
766 if (c->verify_data)
767 list_move(&c->verify_data->list, &c->btree_cache);
768#endif
769
770 list_splice(&c->btree_cache_freeable,
771 &c->btree_cache);
772
773 while (!list_empty(&c->btree_cache)) {
774 b = list_first_entry(&c->btree_cache, struct btree, list);
775
776 if (btree_node_dirty(b))
777 btree_complete_write(b, btree_current_write(b));
778 clear_bit(BTREE_NODE_dirty, &b->flags);
779
780 mca_data_free(b);
781 }
782
783 while (!list_empty(&c->btree_cache_freed)) {
784 b = list_first_entry(&c->btree_cache_freed,
785 struct btree, list);
786 list_del(&b->list);
787 cancel_delayed_work_sync(&b->work);
788 kfree(b);
789 }
790
791 mutex_unlock(&c->bucket_lock);
792}
793
794int bch_btree_cache_alloc(struct cache_set *c)
795{
796 unsigned i;
797
Kent Overstreetcafe5632013-03-23 16:11:31 -0700798 for (i = 0; i < mca_reserve(c); i++)
Kent Overstreet72a44512013-10-24 17:19:26 -0700799 if (!mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL))
800 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700801
802 list_splice_init(&c->btree_cache,
803 &c->btree_cache_freeable);
804
805#ifdef CONFIG_BCACHE_DEBUG
806 mutex_init(&c->verify_lock);
807
808 c->verify_data = mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL);
809
810 if (c->verify_data &&
811 c->verify_data->sets[0].data)
812 list_del_init(&c->verify_data->list);
813 else
814 c->verify_data = NULL;
815#endif
816
Dave Chinner7dc19d52013-08-28 10:18:11 +1000817 c->shrink.count_objects = bch_mca_count;
818 c->shrink.scan_objects = bch_mca_scan;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700819 c->shrink.seeks = 4;
820 c->shrink.batch = c->btree_pages * 2;
821 register_shrinker(&c->shrink);
822
823 return 0;
824}
825
826/* Btree in memory cache - hash table */
827
828static struct hlist_head *mca_hash(struct cache_set *c, struct bkey *k)
829{
830 return &c->bucket_hash[hash_32(PTR_HASH(c, k), BUCKET_HASH_BITS)];
831}
832
833static struct btree *mca_find(struct cache_set *c, struct bkey *k)
834{
835 struct btree *b;
836
837 rcu_read_lock();
838 hlist_for_each_entry_rcu(b, mca_hash(c, k), hash)
839 if (PTR_HASH(c, &b->key) == PTR_HASH(c, k))
840 goto out;
841 b = NULL;
842out:
843 rcu_read_unlock();
844 return b;
845}
846
Kent Overstreete8e1d462013-07-24 17:27:07 -0700847static struct btree *mca_cannibalize(struct cache_set *c, struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700848{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700849 struct btree *b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700850
Kent Overstreetc37511b2013-04-26 15:39:55 -0700851 trace_bcache_btree_cache_cannibalize(c);
852
Kent Overstreete8e1d462013-07-24 17:27:07 -0700853 if (!c->try_harder) {
854 c->try_harder = current;
855 c->try_harder_start = local_clock();
856 } else if (c->try_harder != current)
857 return ERR_PTR(-ENOSPC);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700858
Kent Overstreete8e1d462013-07-24 17:27:07 -0700859 list_for_each_entry_reverse(b, &c->btree_cache, list)
860 if (!mca_reap(b, btree_order(k), false))
861 return b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700862
Kent Overstreete8e1d462013-07-24 17:27:07 -0700863 list_for_each_entry_reverse(b, &c->btree_cache, list)
864 if (!mca_reap(b, btree_order(k), true))
865 return b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700866
Kent Overstreete8e1d462013-07-24 17:27:07 -0700867 return ERR_PTR(-ENOMEM);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700868}
869
870/*
871 * We can only have one thread cannibalizing other cached btree nodes at a time,
872 * or we'll deadlock. We use an open coded mutex to ensure that, which a
873 * cannibalize_bucket() will take. This means every time we unlock the root of
874 * the btree, we need to release this lock if we have it held.
875 */
Kent Overstreetdf8e8972013-07-24 17:37:59 -0700876static void bch_cannibalize_unlock(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700877{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700878 if (c->try_harder == current) {
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600879 bch_time_stats_update(&c->try_harder_time, c->try_harder_start);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700880 c->try_harder = NULL;
Kent Overstreete8e1d462013-07-24 17:27:07 -0700881 wake_up(&c->try_wait);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700882 }
883}
884
Kent Overstreete8e1d462013-07-24 17:27:07 -0700885static struct btree *mca_alloc(struct cache_set *c, struct bkey *k, int level)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700886{
887 struct btree *b;
888
Kent Overstreete8e1d462013-07-24 17:27:07 -0700889 BUG_ON(current->bio_list);
890
Kent Overstreetcafe5632013-03-23 16:11:31 -0700891 lockdep_assert_held(&c->bucket_lock);
892
893 if (mca_find(c, k))
894 return NULL;
895
896 /* btree_free() doesn't free memory; it sticks the node on the end of
897 * the list. Check if there's any freed nodes there:
898 */
899 list_for_each_entry(b, &c->btree_cache_freeable, list)
Kent Overstreete8e1d462013-07-24 17:27:07 -0700900 if (!mca_reap(b, btree_order(k), false))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700901 goto out;
902
903 /* We never free struct btree itself, just the memory that holds the on
904 * disk node. Check the freed list before allocating a new one:
905 */
906 list_for_each_entry(b, &c->btree_cache_freed, list)
Kent Overstreete8e1d462013-07-24 17:27:07 -0700907 if (!mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700908 mca_data_alloc(b, k, __GFP_NOWARN|GFP_NOIO);
909 if (!b->sets[0].data)
910 goto err;
911 else
912 goto out;
913 }
914
915 b = mca_bucket_alloc(c, k, __GFP_NOWARN|GFP_NOIO);
916 if (!b)
917 goto err;
918
919 BUG_ON(!down_write_trylock(&b->lock));
920 if (!b->sets->data)
921 goto err;
922out:
923 BUG_ON(!closure_is_unlocked(&b->io.cl));
924
925 bkey_copy(&b->key, k);
926 list_move(&b->list, &c->btree_cache);
927 hlist_del_init_rcu(&b->hash);
928 hlist_add_head_rcu(&b->hash, mca_hash(c, k));
929
930 lock_set_subclass(&b->lock.dep_map, level + 1, _THIS_IP_);
931 b->level = level;
Kent Overstreetd6fd3b12013-07-24 17:20:19 -0700932 b->parent = (void *) ~0UL;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700933
934 mca_reinit(b);
935
936 return b;
937err:
938 if (b)
939 rw_unlock(true, b);
940
Kent Overstreete8e1d462013-07-24 17:27:07 -0700941 b = mca_cannibalize(c, k);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700942 if (!IS_ERR(b))
943 goto out;
944
945 return b;
946}
947
948/**
949 * bch_btree_node_get - find a btree node in the cache and lock it, reading it
950 * in from disk if necessary.
951 *
Kent Overstreetb54d6932013-07-24 18:04:18 -0700952 * If IO is necessary and running under generic_make_request, returns -EAGAIN.
Kent Overstreetcafe5632013-03-23 16:11:31 -0700953 *
954 * The btree node will have either a read or a write lock held, depending on
955 * level and op->lock.
956 */
957struct btree *bch_btree_node_get(struct cache_set *c, struct bkey *k,
Kent Overstreete8e1d462013-07-24 17:27:07 -0700958 int level, bool write)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700959{
960 int i = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700961 struct btree *b;
962
963 BUG_ON(level < 0);
964retry:
965 b = mca_find(c, k);
966
967 if (!b) {
Kent Overstreet57943512013-04-25 13:58:35 -0700968 if (current->bio_list)
969 return ERR_PTR(-EAGAIN);
970
Kent Overstreetcafe5632013-03-23 16:11:31 -0700971 mutex_lock(&c->bucket_lock);
Kent Overstreete8e1d462013-07-24 17:27:07 -0700972 b = mca_alloc(c, k, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700973 mutex_unlock(&c->bucket_lock);
974
975 if (!b)
976 goto retry;
977 if (IS_ERR(b))
978 return b;
979
Kent Overstreet57943512013-04-25 13:58:35 -0700980 bch_btree_node_read(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700981
982 if (!write)
983 downgrade_write(&b->lock);
984 } else {
985 rw_lock(write, b, level);
986 if (PTR_HASH(c, &b->key) != PTR_HASH(c, k)) {
987 rw_unlock(write, b);
988 goto retry;
989 }
990 BUG_ON(b->level != level);
991 }
992
993 b->accessed = 1;
994
995 for (; i <= b->nsets && b->sets[i].size; i++) {
996 prefetch(b->sets[i].tree);
997 prefetch(b->sets[i].data);
998 }
999
1000 for (; i <= b->nsets; i++)
1001 prefetch(b->sets[i].data);
1002
Kent Overstreet57943512013-04-25 13:58:35 -07001003 if (btree_node_io_error(b)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001004 rw_unlock(write, b);
Kent Overstreet57943512013-04-25 13:58:35 -07001005 return ERR_PTR(-EIO);
1006 }
1007
1008 BUG_ON(!b->written);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001009
1010 return b;
1011}
1012
1013static void btree_node_prefetch(struct cache_set *c, struct bkey *k, int level)
1014{
1015 struct btree *b;
1016
1017 mutex_lock(&c->bucket_lock);
Kent Overstreete8e1d462013-07-24 17:27:07 -07001018 b = mca_alloc(c, k, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001019 mutex_unlock(&c->bucket_lock);
1020
1021 if (!IS_ERR_OR_NULL(b)) {
Kent Overstreet57943512013-04-25 13:58:35 -07001022 bch_btree_node_read(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001023 rw_unlock(true, b);
1024 }
1025}
1026
1027/* Btree alloc */
1028
Kent Overstreete8e1d462013-07-24 17:27:07 -07001029static void btree_node_free(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001030{
1031 unsigned i;
1032
Kent Overstreetc37511b2013-04-26 15:39:55 -07001033 trace_bcache_btree_node_free(b);
1034
Kent Overstreetcafe5632013-03-23 16:11:31 -07001035 BUG_ON(b == b->c->root);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001036
1037 if (btree_node_dirty(b))
1038 btree_complete_write(b, btree_current_write(b));
1039 clear_bit(BTREE_NODE_dirty, &b->flags);
1040
Kent Overstreetcafe5632013-03-23 16:11:31 -07001041 cancel_delayed_work(&b->work);
1042
1043 mutex_lock(&b->c->bucket_lock);
1044
1045 for (i = 0; i < KEY_PTRS(&b->key); i++) {
1046 BUG_ON(atomic_read(&PTR_BUCKET(b->c, &b->key, i)->pin));
1047
1048 bch_inc_gen(PTR_CACHE(b->c, &b->key, i),
1049 PTR_BUCKET(b->c, &b->key, i));
1050 }
1051
1052 bch_bucket_free(b->c, &b->key);
1053 mca_bucket_free(b);
1054 mutex_unlock(&b->c->bucket_lock);
1055}
1056
Kent Overstreet35fcd842013-07-24 17:29:09 -07001057struct btree *bch_btree_node_alloc(struct cache_set *c, int level)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001058{
1059 BKEY_PADDED(key) k;
1060 struct btree *b = ERR_PTR(-EAGAIN);
1061
1062 mutex_lock(&c->bucket_lock);
1063retry:
Kent Overstreet35fcd842013-07-24 17:29:09 -07001064 if (__bch_bucket_alloc_set(c, WATERMARK_METADATA, &k.key, 1, true))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001065 goto err;
1066
1067 SET_KEY_SIZE(&k.key, c->btree_pages * PAGE_SECTORS);
1068
Kent Overstreete8e1d462013-07-24 17:27:07 -07001069 b = mca_alloc(c, &k.key, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001070 if (IS_ERR(b))
1071 goto err_free;
1072
1073 if (!b) {
Kent Overstreetb1a67b02013-03-25 11:46:44 -07001074 cache_bug(c,
1075 "Tried to allocate bucket that was in btree cache");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001076 __bkey_put(c, &k.key);
1077 goto retry;
1078 }
1079
Kent Overstreetcafe5632013-03-23 16:11:31 -07001080 b->accessed = 1;
1081 bch_bset_init_next(b);
1082
1083 mutex_unlock(&c->bucket_lock);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001084
1085 trace_bcache_btree_node_alloc(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001086 return b;
1087err_free:
1088 bch_bucket_free(c, &k.key);
1089 __bkey_put(c, &k.key);
1090err:
1091 mutex_unlock(&c->bucket_lock);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001092
1093 trace_bcache_btree_node_alloc_fail(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001094 return b;
1095}
1096
Kent Overstreet35fcd842013-07-24 17:29:09 -07001097static struct btree *btree_node_alloc_replacement(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001098{
Kent Overstreet35fcd842013-07-24 17:29:09 -07001099 struct btree *n = bch_btree_node_alloc(b->c, b->level);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001100 if (!IS_ERR_OR_NULL(n))
1101 bch_btree_sort_into(b, n);
1102
1103 return n;
1104}
1105
1106/* Garbage collection */
1107
1108uint8_t __bch_btree_mark_key(struct cache_set *c, int level, struct bkey *k)
1109{
1110 uint8_t stale = 0;
1111 unsigned i;
1112 struct bucket *g;
1113
1114 /*
1115 * ptr_invalid() can't return true for the keys that mark btree nodes as
1116 * freed, but since ptr_bad() returns true we'll never actually use them
1117 * for anything and thus we don't want mark their pointers here
1118 */
1119 if (!bkey_cmp(k, &ZERO_KEY))
1120 return stale;
1121
1122 for (i = 0; i < KEY_PTRS(k); i++) {
1123 if (!ptr_available(c, k, i))
1124 continue;
1125
1126 g = PTR_BUCKET(c, k, i);
1127
1128 if (gen_after(g->gc_gen, PTR_GEN(k, i)))
1129 g->gc_gen = PTR_GEN(k, i);
1130
1131 if (ptr_stale(c, k, i)) {
1132 stale = max(stale, ptr_stale(c, k, i));
1133 continue;
1134 }
1135
1136 cache_bug_on(GC_MARK(g) &&
1137 (GC_MARK(g) == GC_MARK_METADATA) != (level != 0),
1138 c, "inconsistent ptrs: mark = %llu, level = %i",
1139 GC_MARK(g), level);
1140
1141 if (level)
1142 SET_GC_MARK(g, GC_MARK_METADATA);
1143 else if (KEY_DIRTY(k))
1144 SET_GC_MARK(g, GC_MARK_DIRTY);
1145
1146 /* guard against overflow */
1147 SET_GC_SECTORS_USED(g, min_t(unsigned,
1148 GC_SECTORS_USED(g) + KEY_SIZE(k),
1149 (1 << 14) - 1));
1150
1151 BUG_ON(!GC_SECTORS_USED(g));
1152 }
1153
1154 return stale;
1155}
1156
1157#define btree_mark_key(b, k) __bch_btree_mark_key(b->c, b->level, k)
1158
1159static int btree_gc_mark_node(struct btree *b, unsigned *keys,
1160 struct gc_stat *gc)
1161{
1162 uint8_t stale = 0;
1163 unsigned last_dev = -1;
1164 struct bcache_device *d = NULL;
1165 struct bkey *k;
1166 struct btree_iter iter;
1167 struct bset_tree *t;
1168
1169 gc->nodes++;
1170
1171 for_each_key_filter(b, k, &iter, bch_ptr_invalid) {
1172 if (last_dev != KEY_INODE(k)) {
1173 last_dev = KEY_INODE(k);
1174
1175 d = KEY_INODE(k) < b->c->nr_uuids
1176 ? b->c->devices[last_dev]
1177 : NULL;
1178 }
1179
1180 stale = max(stale, btree_mark_key(b, k));
1181
1182 if (bch_ptr_bad(b, k))
1183 continue;
1184
1185 *keys += bkey_u64s(k);
1186
1187 gc->key_bytes += bkey_u64s(k);
1188 gc->nkeys++;
1189
1190 gc->data += KEY_SIZE(k);
Kent Overstreet444fc0b2013-05-11 17:07:26 -07001191 if (KEY_DIRTY(k))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001192 gc->dirty += KEY_SIZE(k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001193 }
1194
1195 for (t = b->sets; t <= &b->sets[b->nsets]; t++)
1196 btree_bug_on(t->size &&
1197 bset_written(b, t) &&
1198 bkey_cmp(&b->key, &t->end) < 0,
1199 b, "found short btree key in gc");
1200
1201 return stale;
1202}
1203
Kent Overstreete8e1d462013-07-24 17:27:07 -07001204static struct btree *btree_gc_alloc(struct btree *b, struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001205{
1206 /*
1207 * We block priorities from being written for the duration of garbage
1208 * collection, so we can't sleep in btree_alloc() ->
1209 * bch_bucket_alloc_set(), or we'd risk deadlock - so we don't pass it
1210 * our closure.
1211 */
Kent Overstreet35fcd842013-07-24 17:29:09 -07001212 struct btree *n = btree_node_alloc_replacement(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001213
1214 if (!IS_ERR_OR_NULL(n)) {
1215 swap(b, n);
Kent Overstreet57943512013-04-25 13:58:35 -07001216 __bkey_put(b->c, &b->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001217
1218 memcpy(k->ptr, b->key.ptr,
1219 sizeof(uint64_t) * KEY_PTRS(&b->key));
1220
Kent Overstreete8e1d462013-07-24 17:27:07 -07001221 btree_node_free(n);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001222 up_write(&n->lock);
1223 }
1224
1225 return b;
1226}
1227
1228/*
1229 * Leaving this at 2 until we've got incremental garbage collection done; it
1230 * could be higher (and has been tested with 4) except that garbage collection
1231 * could take much longer, adversely affecting latency.
1232 */
1233#define GC_MERGE_NODES 2U
1234
1235struct gc_merge_info {
1236 struct btree *b;
1237 struct bkey *k;
1238 unsigned keys;
1239};
1240
Kent Overstreete8e1d462013-07-24 17:27:07 -07001241static void btree_gc_coalesce(struct btree *b, struct gc_stat *gc,
1242 struct gc_merge_info *r)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001243{
1244 unsigned nodes = 0, keys = 0, blocks;
1245 int i;
Kent Overstreetb54d6932013-07-24 18:04:18 -07001246 struct closure cl;
1247
1248 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001249
1250 while (nodes < GC_MERGE_NODES && r[nodes].b)
1251 keys += r[nodes++].keys;
1252
1253 blocks = btree_default_blocks(b->c) * 2 / 3;
1254
1255 if (nodes < 2 ||
1256 __set_blocks(b->sets[0].data, keys, b->c) > blocks * (nodes - 1))
1257 return;
1258
1259 for (i = nodes - 1; i >= 0; --i) {
1260 if (r[i].b->written)
Kent Overstreete8e1d462013-07-24 17:27:07 -07001261 r[i].b = btree_gc_alloc(r[i].b, r[i].k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001262
1263 if (r[i].b->written)
1264 return;
1265 }
1266
1267 for (i = nodes - 1; i > 0; --i) {
1268 struct bset *n1 = r[i].b->sets->data;
1269 struct bset *n2 = r[i - 1].b->sets->data;
1270 struct bkey *k, *last = NULL;
1271
1272 keys = 0;
1273
1274 if (i == 1) {
1275 /*
1276 * Last node we're not getting rid of - we're getting
1277 * rid of the node at r[0]. Have to try and fit all of
1278 * the remaining keys into this node; we can't ensure
1279 * they will always fit due to rounding and variable
1280 * length keys (shouldn't be possible in practice,
1281 * though)
1282 */
1283 if (__set_blocks(n1, n1->keys + r->keys,
1284 b->c) > btree_blocks(r[i].b))
1285 return;
1286
1287 keys = n2->keys;
1288 last = &r->b->key;
1289 } else
1290 for (k = n2->start;
1291 k < end(n2);
1292 k = bkey_next(k)) {
1293 if (__set_blocks(n1, n1->keys + keys +
1294 bkey_u64s(k), b->c) > blocks)
1295 break;
1296
1297 last = k;
1298 keys += bkey_u64s(k);
1299 }
1300
1301 BUG_ON(__set_blocks(n1, n1->keys + keys,
1302 b->c) > btree_blocks(r[i].b));
1303
1304 if (last) {
1305 bkey_copy_key(&r[i].b->key, last);
1306 bkey_copy_key(r[i].k, last);
1307 }
1308
1309 memcpy(end(n1),
1310 n2->start,
1311 (void *) node(n2, keys) - (void *) n2->start);
1312
1313 n1->keys += keys;
1314
1315 memmove(n2->start,
1316 node(n2, keys),
1317 (void *) end(n2) - (void *) node(n2, keys));
1318
1319 n2->keys -= keys;
1320
1321 r[i].keys = n1->keys;
1322 r[i - 1].keys = n2->keys;
1323 }
1324
Kent Overstreete8e1d462013-07-24 17:27:07 -07001325 btree_node_free(r->b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001326 up_write(&r->b->lock);
1327
Kent Overstreetc37511b2013-04-26 15:39:55 -07001328 trace_bcache_btree_gc_coalesce(nodes);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001329
1330 gc->nodes--;
1331 nodes--;
1332
1333 memmove(&r[0], &r[1], sizeof(struct gc_merge_info) * nodes);
1334 memset(&r[nodes], 0, sizeof(struct gc_merge_info));
1335}
1336
1337static int btree_gc_recurse(struct btree *b, struct btree_op *op,
1338 struct closure *writes, struct gc_stat *gc)
1339{
1340 void write(struct btree *r)
1341 {
Kent Overstreetb54d6932013-07-24 18:04:18 -07001342 if (!r->written || btree_node_dirty(r))
Kent Overstreet57943512013-04-25 13:58:35 -07001343 bch_btree_node_write(r, writes);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001344
1345 up_write(&r->lock);
1346 }
1347
1348 int ret = 0, stale;
1349 unsigned i;
1350 struct gc_merge_info r[GC_MERGE_NODES];
1351
1352 memset(r, 0, sizeof(r));
1353
1354 while ((r->k = bch_next_recurse_key(b, &b->c->gc_done))) {
Kent Overstreete8e1d462013-07-24 17:27:07 -07001355 r->b = bch_btree_node_get(b->c, r->k, b->level - 1, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001356
1357 if (IS_ERR(r->b)) {
1358 ret = PTR_ERR(r->b);
1359 break;
1360 }
1361
1362 r->keys = 0;
1363 stale = btree_gc_mark_node(r->b, &r->keys, gc);
1364
1365 if (!b->written &&
1366 (r->b->level || stale > 10 ||
1367 b->c->gc_always_rewrite))
Kent Overstreete8e1d462013-07-24 17:27:07 -07001368 r->b = btree_gc_alloc(r->b, r->k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001369
1370 if (r->b->level)
1371 ret = btree_gc_recurse(r->b, op, writes, gc);
1372
1373 if (ret) {
1374 write(r->b);
1375 break;
1376 }
1377
1378 bkey_copy_key(&b->c->gc_done, r->k);
1379
1380 if (!b->written)
Kent Overstreete8e1d462013-07-24 17:27:07 -07001381 btree_gc_coalesce(b, gc, r);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001382
1383 if (r[GC_MERGE_NODES - 1].b)
1384 write(r[GC_MERGE_NODES - 1].b);
1385
1386 memmove(&r[1], &r[0],
1387 sizeof(struct gc_merge_info) * (GC_MERGE_NODES - 1));
1388
1389 /* When we've got incremental GC working, we'll want to do
1390 * if (should_resched())
1391 * return -EAGAIN;
1392 */
1393 cond_resched();
1394#if 0
1395 if (need_resched()) {
1396 ret = -EAGAIN;
1397 break;
1398 }
1399#endif
1400 }
1401
1402 for (i = 1; i < GC_MERGE_NODES && r[i].b; i++)
1403 write(r[i].b);
1404
1405 /* Might have freed some children, must remove their keys */
1406 if (!b->written)
1407 bch_btree_sort(b);
1408
1409 return ret;
1410}
1411
1412static int bch_btree_gc_root(struct btree *b, struct btree_op *op,
1413 struct closure *writes, struct gc_stat *gc)
1414{
1415 struct btree *n = NULL;
1416 unsigned keys = 0;
1417 int ret = 0, stale = btree_gc_mark_node(b, &keys, gc);
Kent Overstreetb54d6932013-07-24 18:04:18 -07001418 struct closure cl;
1419
1420 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001421
1422 if (b->level || stale > 10)
Kent Overstreet35fcd842013-07-24 17:29:09 -07001423 n = btree_node_alloc_replacement(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001424
1425 if (!IS_ERR_OR_NULL(n))
1426 swap(b, n);
1427
1428 if (b->level)
1429 ret = btree_gc_recurse(b, op, writes, gc);
1430
1431 if (!b->written || btree_node_dirty(b)) {
Kent Overstreetb54d6932013-07-24 18:04:18 -07001432 bch_btree_node_write(b, n ? &cl : NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001433 }
1434
1435 if (!IS_ERR_OR_NULL(n)) {
Kent Overstreetb54d6932013-07-24 18:04:18 -07001436 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001437 bch_btree_set_root(b);
Kent Overstreete8e1d462013-07-24 17:27:07 -07001438 btree_node_free(n);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001439 rw_unlock(true, b);
1440 }
1441
1442 return ret;
1443}
1444
1445static void btree_gc_start(struct cache_set *c)
1446{
1447 struct cache *ca;
1448 struct bucket *b;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001449 unsigned i;
1450
1451 if (!c->gc_mark_valid)
1452 return;
1453
1454 mutex_lock(&c->bucket_lock);
1455
1456 c->gc_mark_valid = 0;
1457 c->gc_done = ZERO_KEY;
1458
1459 for_each_cache(ca, c, i)
1460 for_each_bucket(b, ca) {
1461 b->gc_gen = b->gen;
Kent Overstreet29ebf462013-07-11 19:43:21 -07001462 if (!atomic_read(&b->pin)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001463 SET_GC_MARK(b, GC_MARK_RECLAIMABLE);
Kent Overstreet29ebf462013-07-11 19:43:21 -07001464 SET_GC_SECTORS_USED(b, 0);
1465 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001466 }
1467
Kent Overstreetcafe5632013-03-23 16:11:31 -07001468 mutex_unlock(&c->bucket_lock);
1469}
1470
1471size_t bch_btree_gc_finish(struct cache_set *c)
1472{
1473 size_t available = 0;
1474 struct bucket *b;
1475 struct cache *ca;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001476 unsigned i;
1477
1478 mutex_lock(&c->bucket_lock);
1479
1480 set_gc_sectors(c);
1481 c->gc_mark_valid = 1;
1482 c->need_gc = 0;
1483
1484 if (c->root)
1485 for (i = 0; i < KEY_PTRS(&c->root->key); i++)
1486 SET_GC_MARK(PTR_BUCKET(c, &c->root->key, i),
1487 GC_MARK_METADATA);
1488
1489 for (i = 0; i < KEY_PTRS(&c->uuid_bucket); i++)
1490 SET_GC_MARK(PTR_BUCKET(c, &c->uuid_bucket, i),
1491 GC_MARK_METADATA);
1492
1493 for_each_cache(ca, c, i) {
1494 uint64_t *i;
1495
1496 ca->invalidate_needs_gc = 0;
1497
1498 for (i = ca->sb.d; i < ca->sb.d + ca->sb.keys; i++)
1499 SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA);
1500
1501 for (i = ca->prio_buckets;
1502 i < ca->prio_buckets + prio_buckets(ca) * 2; i++)
1503 SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA);
1504
1505 for_each_bucket(b, ca) {
1506 b->last_gc = b->gc_gen;
1507 c->need_gc = max(c->need_gc, bucket_gc_gen(b));
1508
1509 if (!atomic_read(&b->pin) &&
1510 GC_MARK(b) == GC_MARK_RECLAIMABLE) {
1511 available++;
1512 if (!GC_SECTORS_USED(b))
1513 bch_bucket_add_unused(ca, b);
1514 }
1515 }
1516 }
1517
Kent Overstreetcafe5632013-03-23 16:11:31 -07001518 mutex_unlock(&c->bucket_lock);
1519 return available;
1520}
1521
Kent Overstreet72a44512013-10-24 17:19:26 -07001522static void bch_btree_gc(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001523{
Kent Overstreetcafe5632013-03-23 16:11:31 -07001524 int ret;
1525 unsigned long available;
1526 struct gc_stat stats;
1527 struct closure writes;
1528 struct btree_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001529 uint64_t start_time = local_clock();
Kent Overstreet57943512013-04-25 13:58:35 -07001530
Kent Overstreetc37511b2013-04-26 15:39:55 -07001531 trace_bcache_gc_start(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001532
1533 memset(&stats, 0, sizeof(struct gc_stat));
1534 closure_init_stack(&writes);
Kent Overstreetb54d6932013-07-24 18:04:18 -07001535 bch_btree_op_init(&op, SHRT_MAX);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001536
1537 btree_gc_start(c);
1538
Kent Overstreet57943512013-04-25 13:58:35 -07001539 atomic_inc(&c->prio_blocked);
1540
Kent Overstreetcafe5632013-03-23 16:11:31 -07001541 ret = btree_root(gc_root, c, &op, &writes, &stats);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001542 closure_sync(&writes);
1543
1544 if (ret) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001545 pr_warn("gc failed!");
Kent Overstreet72a44512013-10-24 17:19:26 -07001546 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001547 }
1548
1549 /* Possibly wait for new UUIDs or whatever to hit disk */
Kent Overstreetb54d6932013-07-24 18:04:18 -07001550 bch_journal_meta(c, &writes);
1551 closure_sync(&writes);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001552
1553 available = bch_btree_gc_finish(c);
1554
Kent Overstreet57943512013-04-25 13:58:35 -07001555 atomic_dec(&c->prio_blocked);
1556 wake_up_allocators(c);
1557
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001558 bch_time_stats_update(&c->btree_gc_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001559
1560 stats.key_bytes *= sizeof(uint64_t);
1561 stats.dirty <<= 9;
1562 stats.data <<= 9;
1563 stats.in_use = (c->nbuckets - available) * 100 / c->nbuckets;
1564 memcpy(&c->gc_stats, &stats, sizeof(struct gc_stat));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001565
Kent Overstreetc37511b2013-04-26 15:39:55 -07001566 trace_bcache_gc_end(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001567
Kent Overstreet72a44512013-10-24 17:19:26 -07001568 bch_moving_gc(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001569}
1570
Kent Overstreet72a44512013-10-24 17:19:26 -07001571static int bch_gc_thread(void *arg)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001572{
Kent Overstreet72a44512013-10-24 17:19:26 -07001573 struct cache_set *c = arg;
1574
1575 while (1) {
1576 bch_btree_gc(c);
1577
1578 set_current_state(TASK_INTERRUPTIBLE);
1579 if (kthread_should_stop())
1580 break;
1581
1582 try_to_freeze();
1583 schedule();
1584 }
1585
1586 return 0;
1587}
1588
1589int bch_gc_thread_start(struct cache_set *c)
1590{
1591 c->gc_thread = kthread_create(bch_gc_thread, c, "bcache_gc");
1592 if (IS_ERR(c->gc_thread))
1593 return PTR_ERR(c->gc_thread);
1594
1595 set_task_state(c->gc_thread, TASK_INTERRUPTIBLE);
1596 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001597}
1598
1599/* Initial partial gc */
1600
1601static int bch_btree_check_recurse(struct btree *b, struct btree_op *op,
1602 unsigned long **seen)
1603{
1604 int ret;
1605 unsigned i;
1606 struct bkey *k;
1607 struct bucket *g;
1608 struct btree_iter iter;
1609
1610 for_each_key_filter(b, k, &iter, bch_ptr_invalid) {
1611 for (i = 0; i < KEY_PTRS(k); i++) {
1612 if (!ptr_available(b->c, k, i))
1613 continue;
1614
1615 g = PTR_BUCKET(b->c, k, i);
1616
1617 if (!__test_and_set_bit(PTR_BUCKET_NR(b->c, k, i),
1618 seen[PTR_DEV(k, i)]) ||
1619 !ptr_stale(b->c, k, i)) {
1620 g->gen = PTR_GEN(k, i);
1621
1622 if (b->level)
1623 g->prio = BTREE_PRIO;
1624 else if (g->prio == BTREE_PRIO)
1625 g->prio = INITIAL_PRIO;
1626 }
1627 }
1628
1629 btree_mark_key(b, k);
1630 }
1631
1632 if (b->level) {
1633 k = bch_next_recurse_key(b, &ZERO_KEY);
1634
1635 while (k) {
1636 struct bkey *p = bch_next_recurse_key(b, k);
1637 if (p)
1638 btree_node_prefetch(b->c, p, b->level - 1);
1639
1640 ret = btree(check_recurse, k, b, op, seen);
1641 if (ret)
1642 return ret;
1643
1644 k = p;
1645 }
1646 }
1647
1648 return 0;
1649}
1650
Kent Overstreetc18536a2013-07-24 17:44:17 -07001651int bch_btree_check(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001652{
1653 int ret = -ENOMEM;
1654 unsigned i;
1655 unsigned long *seen[MAX_CACHES_PER_SET];
Kent Overstreetc18536a2013-07-24 17:44:17 -07001656 struct btree_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001657
1658 memset(seen, 0, sizeof(seen));
Kent Overstreetb54d6932013-07-24 18:04:18 -07001659 bch_btree_op_init(&op, SHRT_MAX);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001660
1661 for (i = 0; c->cache[i]; i++) {
1662 size_t n = DIV_ROUND_UP(c->cache[i]->sb.nbuckets, 8);
1663 seen[i] = kmalloc(n, GFP_KERNEL);
1664 if (!seen[i])
1665 goto err;
1666
1667 /* Disables the seen array until prio_read() uses it too */
1668 memset(seen[i], 0xFF, n);
1669 }
1670
Kent Overstreetc18536a2013-07-24 17:44:17 -07001671 ret = btree_root(check_recurse, c, &op, seen);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001672err:
1673 for (i = 0; i < MAX_CACHES_PER_SET; i++)
1674 kfree(seen[i]);
1675 return ret;
1676}
1677
1678/* Btree insertion */
1679
1680static void shift_keys(struct btree *b, struct bkey *where, struct bkey *insert)
1681{
1682 struct bset *i = b->sets[b->nsets].data;
1683
1684 memmove((uint64_t *) where + bkey_u64s(insert),
1685 where,
1686 (void *) end(i) - (void *) where);
1687
1688 i->keys += bkey_u64s(insert);
1689 bkey_copy(where, insert);
1690 bch_bset_fix_lookup_table(b, where);
1691}
1692
Kent Overstreet1b207d82013-09-10 18:52:54 -07001693static bool fix_overlapping_extents(struct btree *b, struct bkey *insert,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001694 struct btree_iter *iter,
Kent Overstreet1b207d82013-09-10 18:52:54 -07001695 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001696{
Kent Overstreet279afba2013-06-05 06:21:07 -07001697 void subtract_dirty(struct bkey *k, uint64_t offset, int sectors)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001698 {
Kent Overstreet279afba2013-06-05 06:21:07 -07001699 if (KEY_DIRTY(k))
1700 bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k),
1701 offset, -sectors);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001702 }
1703
Kent Overstreet279afba2013-06-05 06:21:07 -07001704 uint64_t old_offset;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001705 unsigned old_size, sectors_found = 0;
1706
1707 while (1) {
1708 struct bkey *k = bch_btree_iter_next(iter);
1709 if (!k ||
1710 bkey_cmp(&START_KEY(k), insert) >= 0)
1711 break;
1712
1713 if (bkey_cmp(k, &START_KEY(insert)) <= 0)
1714 continue;
1715
Kent Overstreet279afba2013-06-05 06:21:07 -07001716 old_offset = KEY_START(k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001717 old_size = KEY_SIZE(k);
1718
1719 /*
1720 * We might overlap with 0 size extents; we can't skip these
1721 * because if they're in the set we're inserting to we have to
1722 * adjust them so they don't overlap with the key we're
Kent Overstreet1b207d82013-09-10 18:52:54 -07001723 * inserting. But we don't want to check them for replace
Kent Overstreetcafe5632013-03-23 16:11:31 -07001724 * operations.
1725 */
1726
Kent Overstreet1b207d82013-09-10 18:52:54 -07001727 if (replace_key && KEY_SIZE(k)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001728 /*
1729 * k might have been split since we inserted/found the
1730 * key we're replacing
1731 */
1732 unsigned i;
1733 uint64_t offset = KEY_START(k) -
Kent Overstreet1b207d82013-09-10 18:52:54 -07001734 KEY_START(replace_key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001735
1736 /* But it must be a subset of the replace key */
Kent Overstreet1b207d82013-09-10 18:52:54 -07001737 if (KEY_START(k) < KEY_START(replace_key) ||
1738 KEY_OFFSET(k) > KEY_OFFSET(replace_key))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001739 goto check_failed;
1740
1741 /* We didn't find a key that we were supposed to */
1742 if (KEY_START(k) > KEY_START(insert) + sectors_found)
1743 goto check_failed;
1744
Kent Overstreet1b207d82013-09-10 18:52:54 -07001745 if (KEY_PTRS(replace_key) != KEY_PTRS(k))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001746 goto check_failed;
1747
1748 /* skip past gen */
1749 offset <<= 8;
1750
Kent Overstreet1b207d82013-09-10 18:52:54 -07001751 BUG_ON(!KEY_PTRS(replace_key));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001752
Kent Overstreet1b207d82013-09-10 18:52:54 -07001753 for (i = 0; i < KEY_PTRS(replace_key); i++)
1754 if (k->ptr[i] != replace_key->ptr[i] + offset)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001755 goto check_failed;
1756
1757 sectors_found = KEY_OFFSET(k) - KEY_START(insert);
1758 }
1759
1760 if (bkey_cmp(insert, k) < 0 &&
1761 bkey_cmp(&START_KEY(insert), &START_KEY(k)) > 0) {
1762 /*
1763 * We overlapped in the middle of an existing key: that
1764 * means we have to split the old key. But we have to do
1765 * slightly different things depending on whether the
1766 * old key has been written out yet.
1767 */
1768
1769 struct bkey *top;
1770
Kent Overstreet279afba2013-06-05 06:21:07 -07001771 subtract_dirty(k, KEY_START(insert), KEY_SIZE(insert));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001772
1773 if (bkey_written(b, k)) {
1774 /*
1775 * We insert a new key to cover the top of the
1776 * old key, and the old key is modified in place
1777 * to represent the bottom split.
1778 *
1779 * It's completely arbitrary whether the new key
1780 * is the top or the bottom, but it has to match
1781 * up with what btree_sort_fixup() does - it
1782 * doesn't check for this kind of overlap, it
1783 * depends on us inserting a new key for the top
1784 * here.
1785 */
1786 top = bch_bset_search(b, &b->sets[b->nsets],
1787 insert);
1788 shift_keys(b, top, k);
1789 } else {
1790 BKEY_PADDED(key) temp;
1791 bkey_copy(&temp.key, k);
1792 shift_keys(b, k, &temp.key);
1793 top = bkey_next(k);
1794 }
1795
1796 bch_cut_front(insert, top);
1797 bch_cut_back(&START_KEY(insert), k);
1798 bch_bset_fix_invalidated_key(b, k);
1799 return false;
1800 }
1801
1802 if (bkey_cmp(insert, k) < 0) {
1803 bch_cut_front(insert, k);
1804 } else {
Kent Overstreet1fa84552013-11-10 21:55:27 -08001805 if (bkey_cmp(&START_KEY(insert), &START_KEY(k)) > 0)
1806 old_offset = KEY_START(insert);
1807
Kent Overstreetcafe5632013-03-23 16:11:31 -07001808 if (bkey_written(b, k) &&
1809 bkey_cmp(&START_KEY(insert), &START_KEY(k)) <= 0) {
1810 /*
1811 * Completely overwrote, so we don't have to
1812 * invalidate the binary search tree
1813 */
1814 bch_cut_front(k, k);
1815 } else {
1816 __bch_cut_back(&START_KEY(insert), k);
1817 bch_bset_fix_invalidated_key(b, k);
1818 }
1819 }
1820
Kent Overstreet279afba2013-06-05 06:21:07 -07001821 subtract_dirty(k, old_offset, old_size - KEY_SIZE(k));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001822 }
1823
1824check_failed:
Kent Overstreet1b207d82013-09-10 18:52:54 -07001825 if (replace_key) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001826 if (!sectors_found) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001827 return true;
1828 } else if (sectors_found < KEY_SIZE(insert)) {
1829 SET_KEY_OFFSET(insert, KEY_OFFSET(insert) -
1830 (KEY_SIZE(insert) - sectors_found));
1831 SET_KEY_SIZE(insert, sectors_found);
1832 }
1833 }
1834
1835 return false;
1836}
1837
1838static bool btree_insert_key(struct btree *b, struct btree_op *op,
Kent Overstreet1b207d82013-09-10 18:52:54 -07001839 struct bkey *k, struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001840{
1841 struct bset *i = b->sets[b->nsets].data;
1842 struct bkey *m, *prev;
Kent Overstreet85b14922013-05-14 20:33:16 -07001843 unsigned status = BTREE_INSERT_STATUS_INSERT;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001844
1845 BUG_ON(bkey_cmp(k, &b->key) > 0);
1846 BUG_ON(b->level && !KEY_PTRS(k));
1847 BUG_ON(!b->level && !KEY_OFFSET(k));
1848
1849 if (!b->level) {
1850 struct btree_iter iter;
1851 struct bkey search = KEY(KEY_INODE(k), KEY_START(k), 0);
1852
1853 /*
1854 * bset_search() returns the first key that is strictly greater
1855 * than the search key - but for back merging, we want to find
1856 * the first key that is greater than or equal to KEY_START(k) -
1857 * unless KEY_START(k) is 0.
1858 */
1859 if (KEY_OFFSET(&search))
1860 SET_KEY_OFFSET(&search, KEY_OFFSET(&search) - 1);
1861
1862 prev = NULL;
1863 m = bch_btree_iter_init(b, &iter, &search);
1864
Kent Overstreet1b207d82013-09-10 18:52:54 -07001865 if (fix_overlapping_extents(b, k, &iter, replace_key)) {
1866 op->insert_collision = true;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001867 return false;
Kent Overstreet1b207d82013-09-10 18:52:54 -07001868 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001869
Kent Overstreet1fa84552013-11-10 21:55:27 -08001870 if (KEY_DIRTY(k))
1871 bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k),
1872 KEY_START(k), KEY_SIZE(k));
1873
Kent Overstreetcafe5632013-03-23 16:11:31 -07001874 while (m != end(i) &&
1875 bkey_cmp(k, &START_KEY(m)) > 0)
1876 prev = m, m = bkey_next(m);
1877
1878 if (key_merging_disabled(b->c))
1879 goto insert;
1880
1881 /* prev is in the tree, if we merge we're done */
Kent Overstreet85b14922013-05-14 20:33:16 -07001882 status = BTREE_INSERT_STATUS_BACK_MERGE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001883 if (prev &&
1884 bch_bkey_try_merge(b, prev, k))
1885 goto merged;
1886
Kent Overstreet85b14922013-05-14 20:33:16 -07001887 status = BTREE_INSERT_STATUS_OVERWROTE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001888 if (m != end(i) &&
1889 KEY_PTRS(m) == KEY_PTRS(k) && !KEY_SIZE(m))
1890 goto copy;
1891
Kent Overstreet85b14922013-05-14 20:33:16 -07001892 status = BTREE_INSERT_STATUS_FRONT_MERGE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001893 if (m != end(i) &&
1894 bch_bkey_try_merge(b, k, m))
1895 goto copy;
Kent Overstreet1b207d82013-09-10 18:52:54 -07001896 } else {
1897 BUG_ON(replace_key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001898 m = bch_bset_search(b, &b->sets[b->nsets], k);
Kent Overstreet1b207d82013-09-10 18:52:54 -07001899 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001900
1901insert: shift_keys(b, m, k);
1902copy: bkey_copy(m, k);
1903merged:
Kent Overstreet1b207d82013-09-10 18:52:54 -07001904 bch_check_keys(b, "%u for %s", status,
1905 replace_key ? "replace" : "insert");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001906
1907 if (b->level && !KEY_OFFSET(k))
Kent Overstreet57943512013-04-25 13:58:35 -07001908 btree_current_write(b)->prio_blocked++;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001909
Kent Overstreet1b207d82013-09-10 18:52:54 -07001910 trace_bcache_btree_insert_key(b, k, replace_key != NULL, status);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001911
1912 return true;
1913}
1914
Kent Overstreet26c949f2013-09-10 18:41:15 -07001915static bool bch_btree_insert_keys(struct btree *b, struct btree_op *op,
Kent Overstreet1b207d82013-09-10 18:52:54 -07001916 struct keylist *insert_keys,
1917 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001918{
1919 bool ret = false;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001920 unsigned oldsize = bch_count_data(b);
1921
Kent Overstreet26c949f2013-09-10 18:41:15 -07001922 while (!bch_keylist_empty(insert_keys)) {
Kent Overstreet403b6cd2013-07-24 17:22:44 -07001923 struct bset *i = write_block(b);
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07001924 struct bkey *k = insert_keys->keys;
Kent Overstreet26c949f2013-09-10 18:41:15 -07001925
Kent Overstreet403b6cd2013-07-24 17:22:44 -07001926 if (b->written + __set_blocks(i, i->keys + bkey_u64s(k), b->c)
1927 > btree_blocks(b))
1928 break;
1929
1930 if (bkey_cmp(k, &b->key) <= 0) {
Kent Overstreet26c949f2013-09-10 18:41:15 -07001931 bkey_put(b->c, k, b->level);
1932
Kent Overstreet1b207d82013-09-10 18:52:54 -07001933 ret |= btree_insert_key(b, op, k, replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07001934 bch_keylist_pop_front(insert_keys);
1935 } else if (bkey_cmp(&START_KEY(k), &b->key) < 0) {
1936#if 0
Kent Overstreet1b207d82013-09-10 18:52:54 -07001937 if (replace_key) {
Kent Overstreet26c949f2013-09-10 18:41:15 -07001938 bkey_put(b->c, k, b->level);
1939 bch_keylist_pop_front(insert_keys);
1940 op->insert_collision = true;
1941 break;
1942 }
1943#endif
1944 BKEY_PADDED(key) temp;
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07001945 bkey_copy(&temp.key, insert_keys->keys);
Kent Overstreet26c949f2013-09-10 18:41:15 -07001946
1947 bch_cut_back(&b->key, &temp.key);
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07001948 bch_cut_front(&b->key, insert_keys->keys);
Kent Overstreet26c949f2013-09-10 18:41:15 -07001949
Kent Overstreet1b207d82013-09-10 18:52:54 -07001950 ret |= btree_insert_key(b, op, &temp.key, replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07001951 break;
1952 } else {
1953 break;
1954 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001955 }
1956
Kent Overstreet403b6cd2013-07-24 17:22:44 -07001957 BUG_ON(!bch_keylist_empty(insert_keys) && b->level);
1958
Kent Overstreetcafe5632013-03-23 16:11:31 -07001959 BUG_ON(bch_count_data(b) < oldsize);
1960 return ret;
1961}
1962
Kent Overstreet26c949f2013-09-10 18:41:15 -07001963static int btree_split(struct btree *b, struct btree_op *op,
1964 struct keylist *insert_keys,
Kent Overstreet1b207d82013-09-10 18:52:54 -07001965 struct keylist *parent_keys,
1966 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001967{
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07001968 bool split;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001969 struct btree *n1, *n2 = NULL, *n3 = NULL;
1970 uint64_t start_time = local_clock();
Kent Overstreetb54d6932013-07-24 18:04:18 -07001971 struct closure cl;
1972
1973 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001974
Kent Overstreet35fcd842013-07-24 17:29:09 -07001975 n1 = btree_node_alloc_replacement(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001976 if (IS_ERR(n1))
1977 goto err;
1978
1979 split = set_blocks(n1->sets[0].data, n1->c) > (btree_blocks(b) * 4) / 5;
1980
Kent Overstreetcafe5632013-03-23 16:11:31 -07001981 if (split) {
1982 unsigned keys = 0;
1983
Kent Overstreetc37511b2013-04-26 15:39:55 -07001984 trace_bcache_btree_node_split(b, n1->sets[0].data->keys);
1985
Kent Overstreet35fcd842013-07-24 17:29:09 -07001986 n2 = bch_btree_node_alloc(b->c, b->level);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001987 if (IS_ERR(n2))
1988 goto err_free1;
1989
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07001990 if (!b->parent) {
Kent Overstreet35fcd842013-07-24 17:29:09 -07001991 n3 = bch_btree_node_alloc(b->c, b->level + 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001992 if (IS_ERR(n3))
1993 goto err_free2;
1994 }
1995
Kent Overstreet1b207d82013-09-10 18:52:54 -07001996 bch_btree_insert_keys(n1, op, insert_keys, replace_key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001997
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07001998 /*
1999 * Has to be a linear search because we don't have an auxiliary
Kent Overstreetcafe5632013-03-23 16:11:31 -07002000 * search tree yet
2001 */
2002
2003 while (keys < (n1->sets[0].data->keys * 3) / 5)
2004 keys += bkey_u64s(node(n1->sets[0].data, keys));
2005
2006 bkey_copy_key(&n1->key, node(n1->sets[0].data, keys));
2007 keys += bkey_u64s(node(n1->sets[0].data, keys));
2008
2009 n2->sets[0].data->keys = n1->sets[0].data->keys - keys;
2010 n1->sets[0].data->keys = keys;
2011
2012 memcpy(n2->sets[0].data->start,
2013 end(n1->sets[0].data),
2014 n2->sets[0].data->keys * sizeof(uint64_t));
2015
2016 bkey_copy_key(&n2->key, &b->key);
2017
Kent Overstreet26c949f2013-09-10 18:41:15 -07002018 bch_keylist_add(parent_keys, &n2->key);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002019 bch_btree_node_write(n2, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002020 rw_unlock(true, n2);
Kent Overstreetc37511b2013-04-26 15:39:55 -07002021 } else {
2022 trace_bcache_btree_node_compact(b, n1->sets[0].data->keys);
2023
Kent Overstreet1b207d82013-09-10 18:52:54 -07002024 bch_btree_insert_keys(n1, op, insert_keys, replace_key);
Kent Overstreetc37511b2013-04-26 15:39:55 -07002025 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07002026
Kent Overstreet26c949f2013-09-10 18:41:15 -07002027 bch_keylist_add(parent_keys, &n1->key);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002028 bch_btree_node_write(n1, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002029
2030 if (n3) {
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002031 /* Depth increases, make a new root */
2032
Kent Overstreetcafe5632013-03-23 16:11:31 -07002033 bkey_copy_key(&n3->key, &MAX_KEY);
Kent Overstreet1b207d82013-09-10 18:52:54 -07002034 bch_btree_insert_keys(n3, op, parent_keys, NULL);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002035 bch_btree_node_write(n3, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002036
Kent Overstreetb54d6932013-07-24 18:04:18 -07002037 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002038 bch_btree_set_root(n3);
2039 rw_unlock(true, n3);
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002040 } else if (!b->parent) {
2041 /* Root filled up but didn't need to be split */
2042
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07002043 bch_keylist_reset(parent_keys);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002044 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002045 bch_btree_set_root(n1);
2046 } else {
2047 unsigned i;
2048
Kent Overstreet26c949f2013-09-10 18:41:15 -07002049 bkey_copy(parent_keys->top, &b->key);
2050 bkey_copy_key(parent_keys->top, &ZERO_KEY);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002051
2052 for (i = 0; i < KEY_PTRS(&b->key); i++) {
2053 uint8_t g = PTR_BUCKET(b->c, &b->key, i)->gen + 1;
2054
Kent Overstreet26c949f2013-09-10 18:41:15 -07002055 SET_PTR_GEN(parent_keys->top, i, g);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002056 }
2057
Kent Overstreet26c949f2013-09-10 18:41:15 -07002058 bch_keylist_push(parent_keys);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002059 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002060 atomic_inc(&b->c->prio_blocked);
2061 }
2062
2063 rw_unlock(true, n1);
Kent Overstreete8e1d462013-07-24 17:27:07 -07002064 btree_node_free(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002065
Kent Overstreet169ef1c2013-03-28 12:50:55 -06002066 bch_time_stats_update(&b->c->btree_split_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002067
2068 return 0;
2069err_free2:
2070 __bkey_put(n2->c, &n2->key);
Kent Overstreete8e1d462013-07-24 17:27:07 -07002071 btree_node_free(n2);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002072 rw_unlock(true, n2);
2073err_free1:
2074 __bkey_put(n1->c, &n1->key);
Kent Overstreete8e1d462013-07-24 17:27:07 -07002075 btree_node_free(n1);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002076 rw_unlock(true, n1);
2077err:
2078 if (n3 == ERR_PTR(-EAGAIN) ||
2079 n2 == ERR_PTR(-EAGAIN) ||
2080 n1 == ERR_PTR(-EAGAIN))
2081 return -EAGAIN;
2082
2083 pr_warn("couldn't split");
2084 return -ENOMEM;
2085}
2086
Kent Overstreet26c949f2013-09-10 18:41:15 -07002087static int bch_btree_insert_node(struct btree *b, struct btree_op *op,
Kent Overstreetc18536a2013-07-24 17:44:17 -07002088 struct keylist *insert_keys,
Kent Overstreet1b207d82013-09-10 18:52:54 -07002089 atomic_t *journal_ref,
2090 struct bkey *replace_key)
Kent Overstreet26c949f2013-09-10 18:41:15 -07002091{
2092 int ret = 0;
2093 struct keylist split_keys;
2094
2095 bch_keylist_init(&split_keys);
2096
2097 BUG_ON(b->level);
2098
2099 do {
Kent Overstreet1b207d82013-09-10 18:52:54 -07002100 BUG_ON(b->level && replace_key);
2101
Kent Overstreet26c949f2013-09-10 18:41:15 -07002102 if (should_split(b)) {
2103 if (current->bio_list) {
2104 op->lock = b->c->root->level + 1;
2105 ret = -EAGAIN;
2106 } else if (op->lock <= b->c->root->level) {
2107 op->lock = b->c->root->level + 1;
2108 ret = -EINTR;
2109 } else {
2110 struct btree *parent = b->parent;
2111
2112 ret = btree_split(b, op, insert_keys,
Kent Overstreet1b207d82013-09-10 18:52:54 -07002113 &split_keys, replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002114 insert_keys = &split_keys;
Kent Overstreet1b207d82013-09-10 18:52:54 -07002115 replace_key = NULL;
Kent Overstreet26c949f2013-09-10 18:41:15 -07002116 b = parent;
Kent Overstreet403b6cd2013-07-24 17:22:44 -07002117 if (!ret)
2118 ret = -EINTR;
Kent Overstreet26c949f2013-09-10 18:41:15 -07002119 }
2120 } else {
2121 BUG_ON(write_block(b) != b->sets[b->nsets].data);
2122
Kent Overstreet1b207d82013-09-10 18:52:54 -07002123 if (bch_btree_insert_keys(b, op, insert_keys,
2124 replace_key)) {
Kent Overstreetb54d6932013-07-24 18:04:18 -07002125 if (!b->level) {
Kent Overstreetc18536a2013-07-24 17:44:17 -07002126 bch_btree_leaf_dirty(b, journal_ref);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002127 } else {
2128 struct closure cl;
2129
2130 closure_init_stack(&cl);
2131 bch_btree_node_write(b, &cl);
2132 closure_sync(&cl);
2133 }
Kent Overstreet26c949f2013-09-10 18:41:15 -07002134 }
2135 }
2136 } while (!bch_keylist_empty(&split_keys));
2137
2138 return ret;
2139}
2140
Kent Overstreete7c590e2013-09-10 18:39:16 -07002141int bch_btree_insert_check_key(struct btree *b, struct btree_op *op,
2142 struct bkey *check_key)
2143{
2144 int ret = -EINTR;
2145 uint64_t btree_ptr = b->key.ptr[0];
2146 unsigned long seq = b->seq;
2147 struct keylist insert;
2148 bool upgrade = op->lock == -1;
2149
2150 bch_keylist_init(&insert);
2151
2152 if (upgrade) {
2153 rw_unlock(false, b);
2154 rw_lock(true, b, b->level);
2155
2156 if (b->key.ptr[0] != btree_ptr ||
2157 b->seq != seq + 1)
2158 goto out;
2159 }
2160
2161 SET_KEY_PTRS(check_key, 1);
2162 get_random_bytes(&check_key->ptr[0], sizeof(uint64_t));
2163
2164 SET_PTR_DEV(check_key, 0, PTR_CHECK_DEV);
2165
2166 bch_keylist_add(&insert, check_key);
2167
Kent Overstreet1b207d82013-09-10 18:52:54 -07002168 ret = bch_btree_insert_node(b, op, &insert, NULL, NULL);
Kent Overstreete7c590e2013-09-10 18:39:16 -07002169
2170 BUG_ON(!ret && !bch_keylist_empty(&insert));
2171out:
2172 if (upgrade)
2173 downgrade_write(&b->lock);
2174 return ret;
2175}
2176
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002177struct btree_insert_op {
2178 struct btree_op op;
2179 struct keylist *keys;
2180 atomic_t *journal_ref;
2181 struct bkey *replace_key;
2182};
2183
2184int btree_insert_fn(struct btree_op *b_op, struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002185{
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002186 struct btree_insert_op *op = container_of(b_op,
2187 struct btree_insert_op, op);
Kent Overstreet403b6cd2013-07-24 17:22:44 -07002188
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002189 int ret = bch_btree_insert_node(b, &op->op, op->keys,
2190 op->journal_ref, op->replace_key);
2191 if (ret && !bch_keylist_empty(op->keys))
2192 return ret;
2193 else
2194 return MAP_DONE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002195}
2196
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002197int bch_btree_insert(struct cache_set *c, struct keylist *keys,
2198 atomic_t *journal_ref, struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002199{
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002200 struct btree_insert_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002201 int ret = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002202
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002203 BUG_ON(current->bio_list);
Kent Overstreet4f3d4012013-09-10 18:46:36 -07002204 BUG_ON(bch_keylist_empty(keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002205
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002206 bch_btree_op_init(&op.op, 0);
2207 op.keys = keys;
2208 op.journal_ref = journal_ref;
2209 op.replace_key = replace_key;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002210
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002211 while (!ret && !bch_keylist_empty(keys)) {
2212 op.op.lock = 0;
2213 ret = bch_btree_map_leaf_nodes(&op.op, c,
2214 &START_KEY(keys->keys),
2215 btree_insert_fn);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002216 }
2217
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002218 if (ret) {
2219 struct bkey *k;
2220
2221 pr_err("error %i", ret);
2222
2223 while ((k = bch_keylist_pop(keys)))
2224 bkey_put(c, k, 0);
2225 } else if (op.op.insert_collision)
2226 ret = -ESRCH;
Kent Overstreet6054c6d2013-07-24 18:06:22 -07002227
Kent Overstreetcafe5632013-03-23 16:11:31 -07002228 return ret;
2229}
2230
2231void bch_btree_set_root(struct btree *b)
2232{
2233 unsigned i;
Kent Overstreete49c7c32013-06-26 17:25:38 -07002234 struct closure cl;
2235
2236 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002237
Kent Overstreetc37511b2013-04-26 15:39:55 -07002238 trace_bcache_btree_set_root(b);
2239
Kent Overstreetcafe5632013-03-23 16:11:31 -07002240 BUG_ON(!b->written);
2241
2242 for (i = 0; i < KEY_PTRS(&b->key); i++)
2243 BUG_ON(PTR_BUCKET(b->c, &b->key, i)->prio != BTREE_PRIO);
2244
2245 mutex_lock(&b->c->bucket_lock);
2246 list_del_init(&b->list);
2247 mutex_unlock(&b->c->bucket_lock);
2248
2249 b->c->root = b;
2250 __bkey_put(b->c, &b->key);
2251
Kent Overstreete49c7c32013-06-26 17:25:38 -07002252 bch_journal_meta(b->c, &cl);
2253 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002254}
2255
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002256/* Map across nodes or keys */
2257
2258static int bch_btree_map_nodes_recurse(struct btree *b, struct btree_op *op,
2259 struct bkey *from,
2260 btree_map_nodes_fn *fn, int flags)
2261{
2262 int ret = MAP_CONTINUE;
2263
2264 if (b->level) {
2265 struct bkey *k;
2266 struct btree_iter iter;
2267
2268 bch_btree_iter_init(b, &iter, from);
2269
2270 while ((k = bch_btree_iter_next_filter(&iter, b,
2271 bch_ptr_bad))) {
2272 ret = btree(map_nodes_recurse, k, b,
2273 op, from, fn, flags);
2274 from = NULL;
2275
2276 if (ret != MAP_CONTINUE)
2277 return ret;
2278 }
2279 }
2280
2281 if (!b->level || flags == MAP_ALL_NODES)
2282 ret = fn(op, b);
2283
2284 return ret;
2285}
2286
2287int __bch_btree_map_nodes(struct btree_op *op, struct cache_set *c,
2288 struct bkey *from, btree_map_nodes_fn *fn, int flags)
2289{
Kent Overstreetb54d6932013-07-24 18:04:18 -07002290 return btree_root(map_nodes_recurse, c, op, from, fn, flags);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002291}
2292
2293static int bch_btree_map_keys_recurse(struct btree *b, struct btree_op *op,
2294 struct bkey *from, btree_map_keys_fn *fn,
2295 int flags)
2296{
2297 int ret = MAP_CONTINUE;
2298 struct bkey *k;
2299 struct btree_iter iter;
2300
2301 bch_btree_iter_init(b, &iter, from);
2302
2303 while ((k = bch_btree_iter_next_filter(&iter, b, bch_ptr_bad))) {
2304 ret = !b->level
2305 ? fn(op, b, k)
2306 : btree(map_keys_recurse, k, b, op, from, fn, flags);
2307 from = NULL;
2308
2309 if (ret != MAP_CONTINUE)
2310 return ret;
2311 }
2312
2313 if (!b->level && (flags & MAP_END_KEY))
2314 ret = fn(op, b, &KEY(KEY_INODE(&b->key),
2315 KEY_OFFSET(&b->key), 0));
2316
2317 return ret;
2318}
2319
2320int bch_btree_map_keys(struct btree_op *op, struct cache_set *c,
2321 struct bkey *from, btree_map_keys_fn *fn, int flags)
2322{
Kent Overstreetb54d6932013-07-24 18:04:18 -07002323 return btree_root(map_keys_recurse, c, op, from, fn, flags);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002324}
2325
Kent Overstreetcafe5632013-03-23 16:11:31 -07002326/* Keybuf code */
2327
2328static inline int keybuf_cmp(struct keybuf_key *l, struct keybuf_key *r)
2329{
2330 /* Overlapping keys compare equal */
2331 if (bkey_cmp(&l->key, &START_KEY(&r->key)) <= 0)
2332 return -1;
2333 if (bkey_cmp(&START_KEY(&l->key), &r->key) >= 0)
2334 return 1;
2335 return 0;
2336}
2337
2338static inline int keybuf_nonoverlapping_cmp(struct keybuf_key *l,
2339 struct keybuf_key *r)
2340{
2341 return clamp_t(int64_t, bkey_cmp(&l->key, &r->key), -1, 1);
2342}
2343
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002344struct refill {
2345 struct btree_op op;
2346 struct keybuf *buf;
2347 struct bkey *end;
2348 keybuf_pred_fn *pred;
2349};
2350
2351static int refill_keybuf_fn(struct btree_op *op, struct btree *b,
2352 struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002353{
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002354 struct refill *refill = container_of(op, struct refill, op);
2355 struct keybuf *buf = refill->buf;
2356 int ret = MAP_CONTINUE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002357
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002358 if (bkey_cmp(k, refill->end) >= 0) {
2359 ret = MAP_DONE;
2360 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002361 }
2362
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002363 if (!KEY_SIZE(k)) /* end key */
2364 goto out;
2365
2366 if (refill->pred(buf, k)) {
2367 struct keybuf_key *w;
2368
2369 spin_lock(&buf->lock);
2370
2371 w = array_alloc(&buf->freelist);
2372 if (!w) {
2373 spin_unlock(&buf->lock);
2374 return MAP_DONE;
2375 }
2376
2377 w->private = NULL;
2378 bkey_copy(&w->key, k);
2379
2380 if (RB_INSERT(&buf->keys, w, node, keybuf_cmp))
2381 array_free(&buf->freelist, w);
2382
2383 if (array_freelist_empty(&buf->freelist))
2384 ret = MAP_DONE;
2385
2386 spin_unlock(&buf->lock);
2387 }
2388out:
2389 buf->last_scanned = *k;
2390 return ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002391}
2392
2393void bch_refill_keybuf(struct cache_set *c, struct keybuf *buf,
Kent Overstreet72c27062013-06-05 06:24:39 -07002394 struct bkey *end, keybuf_pred_fn *pred)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002395{
2396 struct bkey start = buf->last_scanned;
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002397 struct refill refill;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002398
2399 cond_resched();
2400
Kent Overstreetb54d6932013-07-24 18:04:18 -07002401 bch_btree_op_init(&refill.op, -1);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002402 refill.buf = buf;
2403 refill.end = end;
2404 refill.pred = pred;
2405
2406 bch_btree_map_keys(&refill.op, c, &buf->last_scanned,
2407 refill_keybuf_fn, MAP_END_KEY);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002408
2409 pr_debug("found %s keys from %llu:%llu to %llu:%llu",
2410 RB_EMPTY_ROOT(&buf->keys) ? "no" :
2411 array_freelist_empty(&buf->freelist) ? "some" : "a few",
2412 KEY_INODE(&start), KEY_OFFSET(&start),
2413 KEY_INODE(&buf->last_scanned), KEY_OFFSET(&buf->last_scanned));
2414
2415 spin_lock(&buf->lock);
2416
2417 if (!RB_EMPTY_ROOT(&buf->keys)) {
2418 struct keybuf_key *w;
2419 w = RB_FIRST(&buf->keys, struct keybuf_key, node);
2420 buf->start = START_KEY(&w->key);
2421
2422 w = RB_LAST(&buf->keys, struct keybuf_key, node);
2423 buf->end = w->key;
2424 } else {
2425 buf->start = MAX_KEY;
2426 buf->end = MAX_KEY;
2427 }
2428
2429 spin_unlock(&buf->lock);
2430}
2431
2432static void __bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w)
2433{
2434 rb_erase(&w->node, &buf->keys);
2435 array_free(&buf->freelist, w);
2436}
2437
2438void bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w)
2439{
2440 spin_lock(&buf->lock);
2441 __bch_keybuf_del(buf, w);
2442 spin_unlock(&buf->lock);
2443}
2444
2445bool bch_keybuf_check_overlapping(struct keybuf *buf, struct bkey *start,
2446 struct bkey *end)
2447{
2448 bool ret = false;
2449 struct keybuf_key *p, *w, s;
2450 s.key = *start;
2451
2452 if (bkey_cmp(end, &buf->start) <= 0 ||
2453 bkey_cmp(start, &buf->end) >= 0)
2454 return false;
2455
2456 spin_lock(&buf->lock);
2457 w = RB_GREATER(&buf->keys, s, node, keybuf_nonoverlapping_cmp);
2458
2459 while (w && bkey_cmp(&START_KEY(&w->key), end) < 0) {
2460 p = w;
2461 w = RB_NEXT(w, node);
2462
2463 if (p->private)
2464 ret = true;
2465 else
2466 __bch_keybuf_del(buf, p);
2467 }
2468
2469 spin_unlock(&buf->lock);
2470 return ret;
2471}
2472
2473struct keybuf_key *bch_keybuf_next(struct keybuf *buf)
2474{
2475 struct keybuf_key *w;
2476 spin_lock(&buf->lock);
2477
2478 w = RB_FIRST(&buf->keys, struct keybuf_key, node);
2479
2480 while (w && w->private)
2481 w = RB_NEXT(w, node);
2482
2483 if (w)
2484 w->private = ERR_PTR(-EINTR);
2485
2486 spin_unlock(&buf->lock);
2487 return w;
2488}
2489
2490struct keybuf_key *bch_keybuf_next_rescan(struct cache_set *c,
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002491 struct keybuf *buf,
2492 struct bkey *end,
2493 keybuf_pred_fn *pred)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002494{
2495 struct keybuf_key *ret;
2496
2497 while (1) {
2498 ret = bch_keybuf_next(buf);
2499 if (ret)
2500 break;
2501
2502 if (bkey_cmp(&buf->last_scanned, end) >= 0) {
2503 pr_debug("scan finished");
2504 break;
2505 }
2506
Kent Overstreet72c27062013-06-05 06:24:39 -07002507 bch_refill_keybuf(c, buf, end, pred);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002508 }
2509
2510 return ret;
2511}
2512
Kent Overstreet72c27062013-06-05 06:24:39 -07002513void bch_keybuf_init(struct keybuf *buf)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002514{
Kent Overstreetcafe5632013-03-23 16:11:31 -07002515 buf->last_scanned = MAX_KEY;
2516 buf->keys = RB_ROOT;
2517
2518 spin_lock_init(&buf->lock);
2519 array_allocator_init(&buf->freelist);
2520}
2521
2522void bch_btree_exit(void)
2523{
2524 if (btree_io_wq)
2525 destroy_workqueue(btree_io_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002526}
2527
2528int __init bch_btree_init(void)
2529{
Kent Overstreet72a44512013-10-24 17:19:26 -07002530 btree_io_wq = create_singlethread_workqueue("bch_btree_io");
2531 if (!btree_io_wq)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002532 return -ENOMEM;
2533
2534 return 0;
2535}