blob: 7d283d217438fe37d545b19abbd39f75060eba3c [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 Overstreet3a3b6a42013-07-24 16:46:42 -0700183void bkey_put(struct cache_set *c, struct bkey *k)
Kent Overstreete7c590e2013-09-10 18:39:16 -0700184{
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 -0700192/* Btree IO */
193
194static uint64_t btree_csum_set(struct btree *b, struct bset *i)
195{
196 uint64_t crc = b->key.ptr[0];
197 void *data = (void *) i + 8, *end = end(i);
198
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600199 crc = bch_crc64_update(crc, data, end - data);
Kent Overstreetc19ed232013-03-26 13:49:02 -0700200 return crc ^ 0xffffffffffffffffULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700201}
202
Kent Overstreetf3059a52013-05-15 17:13:45 -0700203static void bch_btree_node_read_done(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700204{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700205 const char *err = "bad btree header";
Kent Overstreet57943512013-04-25 13:58:35 -0700206 struct bset *i = b->sets[0].data;
207 struct btree_iter *iter;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700208
Kent Overstreet57943512013-04-25 13:58:35 -0700209 iter = mempool_alloc(b->c->fill_iter, GFP_NOWAIT);
210 iter->size = b->c->sb.bucket_size / b->c->sb.block_size;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700211 iter->used = 0;
212
Kent Overstreet280481d2013-10-24 16:36:03 -0700213#ifdef CONFIG_BCACHE_DEBUG
214 iter->b = b;
215#endif
216
Kent Overstreet57943512013-04-25 13:58:35 -0700217 if (!i->seq)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700218 goto err;
219
220 for (;
221 b->written < btree_blocks(b) && i->seq == b->sets[0].data->seq;
222 i = write_block(b)) {
223 err = "unsupported bset version";
224 if (i->version > BCACHE_BSET_VERSION)
225 goto err;
226
227 err = "bad btree header";
228 if (b->written + set_blocks(i, b->c) > btree_blocks(b))
229 goto err;
230
231 err = "bad magic";
Kent Overstreet81ab4192013-10-31 15:46:42 -0700232 if (i->magic != bset_magic(&b->c->sb))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700233 goto err;
234
235 err = "bad checksum";
236 switch (i->version) {
237 case 0:
238 if (i->csum != csum_set(i))
239 goto err;
240 break;
241 case BCACHE_BSET_VERSION:
242 if (i->csum != btree_csum_set(b, i))
243 goto err;
244 break;
245 }
246
247 err = "empty set";
248 if (i != b->sets[0].data && !i->keys)
249 goto err;
250
251 bch_btree_iter_push(iter, i->start, end(i));
252
253 b->written += set_blocks(i, b->c);
254 }
255
256 err = "corrupted btree";
257 for (i = write_block(b);
258 index(i, b) < btree_blocks(b);
259 i = ((void *) i) + block_bytes(b->c))
260 if (i->seq == b->sets[0].data->seq)
261 goto err;
262
263 bch_btree_sort_and_fix_extents(b, iter);
264
265 i = b->sets[0].data;
266 err = "short btree key";
267 if (b->sets[0].size &&
268 bkey_cmp(&b->key, &b->sets[0].end) < 0)
269 goto err;
270
271 if (b->written < btree_blocks(b))
272 bch_bset_init_next(b);
273out:
Kent Overstreet57943512013-04-25 13:58:35 -0700274 mempool_free(iter, b->c->fill_iter);
275 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700276err:
277 set_btree_node_io_error(b);
Kent Overstreet07e86cc2013-03-25 11:46:43 -0700278 bch_cache_set_error(b->c, "%s at bucket %zu, block %zu, %u keys",
Kent Overstreetcafe5632013-03-23 16:11:31 -0700279 err, PTR_BUCKET_NR(b->c, &b->key, 0),
280 index(i, b), i->keys);
281 goto out;
282}
283
Kent Overstreet57943512013-04-25 13:58:35 -0700284static void btree_node_read_endio(struct bio *bio, int error)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700285{
Kent Overstreet57943512013-04-25 13:58:35 -0700286 struct closure *cl = bio->bi_private;
287 closure_put(cl);
288}
Kent Overstreetcafe5632013-03-23 16:11:31 -0700289
Kent Overstreet57943512013-04-25 13:58:35 -0700290void bch_btree_node_read(struct btree *b)
291{
292 uint64_t start_time = local_clock();
293 struct closure cl;
294 struct bio *bio;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700295
Kent Overstreetc37511b2013-04-26 15:39:55 -0700296 trace_bcache_btree_read(b);
297
Kent Overstreet57943512013-04-25 13:58:35 -0700298 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700299
Kent Overstreet57943512013-04-25 13:58:35 -0700300 bio = bch_bbio_alloc(b->c);
301 bio->bi_rw = REQ_META|READ_SYNC;
302 bio->bi_size = KEY_SIZE(&b->key) << 9;
303 bio->bi_end_io = btree_node_read_endio;
304 bio->bi_private = &cl;
305
306 bch_bio_map(bio, b->sets[0].data);
307
Kent Overstreet57943512013-04-25 13:58:35 -0700308 bch_submit_bbio(bio, b->c, &b->key, 0);
309 closure_sync(&cl);
310
311 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
312 set_btree_node_io_error(b);
313
314 bch_bbio_free(bio, b->c);
315
316 if (btree_node_io_error(b))
317 goto err;
318
319 bch_btree_node_read_done(b);
320
321 spin_lock(&b->c->btree_read_time_lock);
322 bch_time_stats_update(&b->c->btree_read_time, start_time);
323 spin_unlock(&b->c->btree_read_time_lock);
324
325 return;
326err:
Geert Uytterhoeven61cbd252013-09-23 23:17:30 -0700327 bch_cache_set_error(b->c, "io error reading bucket %zu",
Kent Overstreet57943512013-04-25 13:58:35 -0700328 PTR_BUCKET_NR(b->c, &b->key, 0));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700329}
330
331static void btree_complete_write(struct btree *b, struct btree_write *w)
332{
333 if (w->prio_blocked &&
334 !atomic_sub_return(w->prio_blocked, &b->c->prio_blocked))
Kent Overstreet119ba0f2013-04-24 19:01:12 -0700335 wake_up_allocators(b->c);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700336
337 if (w->journal) {
338 atomic_dec_bug(w->journal);
339 __closure_wake_up(&b->c->journal.wait);
340 }
341
Kent Overstreetcafe5632013-03-23 16:11:31 -0700342 w->prio_blocked = 0;
343 w->journal = NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700344}
345
Kent Overstreet57943512013-04-25 13:58:35 -0700346static void __btree_node_write_done(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700347{
348 struct btree *b = container_of(cl, struct btree, io.cl);
349 struct btree_write *w = btree_prev_write(b);
350
351 bch_bbio_free(b->bio, b->c);
352 b->bio = NULL;
353 btree_complete_write(b, w);
354
355 if (btree_node_dirty(b))
356 queue_delayed_work(btree_io_wq, &b->work,
357 msecs_to_jiffies(30000));
358
359 closure_return(cl);
360}
361
Kent Overstreet57943512013-04-25 13:58:35 -0700362static void btree_node_write_done(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700363{
364 struct btree *b = container_of(cl, struct btree, io.cl);
365 struct bio_vec *bv;
366 int n;
367
368 __bio_for_each_segment(bv, b->bio, n, 0)
369 __free_page(bv->bv_page);
370
Kent Overstreet57943512013-04-25 13:58:35 -0700371 __btree_node_write_done(cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700372}
373
Kent Overstreet57943512013-04-25 13:58:35 -0700374static void btree_node_write_endio(struct bio *bio, int error)
375{
376 struct closure *cl = bio->bi_private;
377 struct btree *b = container_of(cl, struct btree, io.cl);
378
379 if (error)
380 set_btree_node_io_error(b);
381
382 bch_bbio_count_io_errors(b->c, bio, error, "writing btree");
383 closure_put(cl);
384}
385
386static void do_btree_node_write(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700387{
388 struct closure *cl = &b->io.cl;
389 struct bset *i = b->sets[b->nsets].data;
390 BKEY_PADDED(key) k;
391
392 i->version = BCACHE_BSET_VERSION;
393 i->csum = btree_csum_set(b, i);
394
Kent Overstreet57943512013-04-25 13:58:35 -0700395 BUG_ON(b->bio);
396 b->bio = bch_bbio_alloc(b->c);
397
398 b->bio->bi_end_io = btree_node_write_endio;
Kent Overstreetfaadf0c2013-11-01 18:03:08 -0700399 b->bio->bi_private = cl;
Kent Overstreete49c7c32013-06-26 17:25:38 -0700400 b->bio->bi_rw = REQ_META|WRITE_SYNC|REQ_FUA;
401 b->bio->bi_size = set_blocks(i, b->c) * block_bytes(b->c);
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600402 bch_bio_map(b->bio, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700403
Kent Overstreete49c7c32013-06-26 17:25:38 -0700404 /*
405 * If we're appending to a leaf node, we don't technically need FUA -
406 * this write just needs to be persisted before the next journal write,
407 * which will be marked FLUSH|FUA.
408 *
409 * Similarly if we're writing a new btree root - the pointer is going to
410 * be in the next journal entry.
411 *
412 * But if we're writing a new btree node (that isn't a root) or
413 * appending to a non leaf btree node, we need either FUA or a flush
414 * when we write the parent with the new pointer. FUA is cheaper than a
415 * flush, and writes appending to leaf nodes aren't blocking anything so
416 * just make all btree node writes FUA to keep things sane.
417 */
418
Kent Overstreetcafe5632013-03-23 16:11:31 -0700419 bkey_copy(&k.key, &b->key);
420 SET_PTR_OFFSET(&k.key, 0, PTR_OFFSET(&k.key, 0) + bset_offset(b, i));
421
Kent Overstreet8e51e412013-06-06 18:15:57 -0700422 if (!bio_alloc_pages(b->bio, GFP_NOIO)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700423 int j;
424 struct bio_vec *bv;
425 void *base = (void *) ((unsigned long) i & ~(PAGE_SIZE - 1));
426
427 bio_for_each_segment(bv, b->bio, j)
428 memcpy(page_address(bv->bv_page),
429 base + j * PAGE_SIZE, PAGE_SIZE);
430
Kent Overstreetcafe5632013-03-23 16:11:31 -0700431 bch_submit_bbio(b->bio, b->c, &k.key, 0);
432
Kent Overstreet57943512013-04-25 13:58:35 -0700433 continue_at(cl, btree_node_write_done, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700434 } else {
435 b->bio->bi_vcnt = 0;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600436 bch_bio_map(b->bio, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700437
Kent Overstreetcafe5632013-03-23 16:11:31 -0700438 bch_submit_bbio(b->bio, b->c, &k.key, 0);
439
440 closure_sync(cl);
Kent Overstreet57943512013-04-25 13:58:35 -0700441 __btree_node_write_done(cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700442 }
443}
444
Kent Overstreet57943512013-04-25 13:58:35 -0700445void bch_btree_node_write(struct btree *b, struct closure *parent)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700446{
447 struct bset *i = b->sets[b->nsets].data;
448
Kent Overstreetc37511b2013-04-26 15:39:55 -0700449 trace_bcache_btree_write(b);
450
Kent Overstreetcafe5632013-03-23 16:11:31 -0700451 BUG_ON(current->bio_list);
Kent Overstreet57943512013-04-25 13:58:35 -0700452 BUG_ON(b->written >= btree_blocks(b));
453 BUG_ON(b->written && !i->keys);
454 BUG_ON(b->sets->data->seq != i->seq);
Kent Overstreet280481d2013-10-24 16:36:03 -0700455 bch_check_keys(b, "writing");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700456
Kent Overstreetcafe5632013-03-23 16:11:31 -0700457 cancel_delayed_work(&b->work);
458
Kent Overstreet57943512013-04-25 13:58:35 -0700459 /* If caller isn't waiting for write, parent refcount is cache set */
460 closure_lock(&b->io, parent ?: &b->c->cl);
461
Kent Overstreetcafe5632013-03-23 16:11:31 -0700462 clear_bit(BTREE_NODE_dirty, &b->flags);
463 change_bit(BTREE_NODE_write_idx, &b->flags);
464
Kent Overstreet57943512013-04-25 13:58:35 -0700465 do_btree_node_write(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700466
Kent Overstreetcafe5632013-03-23 16:11:31 -0700467 b->written += set_blocks(i, b->c);
468 atomic_long_add(set_blocks(i, b->c) * b->c->sb.block_size,
469 &PTR_CACHE(b->c, &b->key, 0)->btree_sectors_written);
470
471 bch_btree_sort_lazy(b);
472
473 if (b->written < btree_blocks(b))
474 bch_bset_init_next(b);
475}
476
Kent Overstreetf269af52013-07-23 20:48:29 -0700477static void bch_btree_node_write_sync(struct btree *b)
478{
479 struct closure cl;
480
481 closure_init_stack(&cl);
482 bch_btree_node_write(b, &cl);
483 closure_sync(&cl);
484}
485
Kent Overstreet57943512013-04-25 13:58:35 -0700486static void btree_node_write_work(struct work_struct *w)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700487{
488 struct btree *b = container_of(to_delayed_work(w), struct btree, work);
489
Kent Overstreet57943512013-04-25 13:58:35 -0700490 rw_lock(true, b, b->level);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700491
492 if (btree_node_dirty(b))
Kent Overstreet57943512013-04-25 13:58:35 -0700493 bch_btree_node_write(b, NULL);
494 rw_unlock(true, b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700495}
496
Kent Overstreetc18536a2013-07-24 17:44:17 -0700497static void bch_btree_leaf_dirty(struct btree *b, atomic_t *journal_ref)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700498{
499 struct bset *i = b->sets[b->nsets].data;
500 struct btree_write *w = btree_current_write(b);
501
Kent Overstreet57943512013-04-25 13:58:35 -0700502 BUG_ON(!b->written);
503 BUG_ON(!i->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700504
Kent Overstreet57943512013-04-25 13:58:35 -0700505 if (!btree_node_dirty(b))
506 queue_delayed_work(btree_io_wq, &b->work, 30 * HZ);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700507
Kent Overstreet57943512013-04-25 13:58:35 -0700508 set_btree_node_dirty(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700509
Kent Overstreetc18536a2013-07-24 17:44:17 -0700510 if (journal_ref) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700511 if (w->journal &&
Kent Overstreetc18536a2013-07-24 17:44:17 -0700512 journal_pin_cmp(b->c, w->journal, journal_ref)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700513 atomic_dec_bug(w->journal);
514 w->journal = NULL;
515 }
516
517 if (!w->journal) {
Kent Overstreetc18536a2013-07-24 17:44:17 -0700518 w->journal = journal_ref;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700519 atomic_inc(w->journal);
520 }
521 }
522
Kent Overstreetcafe5632013-03-23 16:11:31 -0700523 /* Force write if set is too big */
Kent Overstreet57943512013-04-25 13:58:35 -0700524 if (set_bytes(i) > PAGE_SIZE - 48 &&
525 !current->bio_list)
526 bch_btree_node_write(b, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700527}
528
529/*
530 * Btree in memory cache - allocation/freeing
531 * mca -> memory cache
532 */
533
534static void mca_reinit(struct btree *b)
535{
536 unsigned i;
537
538 b->flags = 0;
539 b->written = 0;
540 b->nsets = 0;
541
542 for (i = 0; i < MAX_BSETS; i++)
543 b->sets[i].size = 0;
544 /*
545 * Second loop starts at 1 because b->sets[0]->data is the memory we
546 * allocated
547 */
548 for (i = 1; i < MAX_BSETS; i++)
549 b->sets[i].data = NULL;
550}
551
552#define mca_reserve(c) (((c->root && c->root->level) \
553 ? c->root->level : 1) * 8 + 16)
554#define mca_can_free(c) \
555 max_t(int, 0, c->bucket_cache_used - mca_reserve(c))
556
557static void mca_data_free(struct btree *b)
558{
559 struct bset_tree *t = b->sets;
560 BUG_ON(!closure_is_unlocked(&b->io.cl));
561
562 if (bset_prev_bytes(b) < PAGE_SIZE)
563 kfree(t->prev);
564 else
565 free_pages((unsigned long) t->prev,
566 get_order(bset_prev_bytes(b)));
567
568 if (bset_tree_bytes(b) < PAGE_SIZE)
569 kfree(t->tree);
570 else
571 free_pages((unsigned long) t->tree,
572 get_order(bset_tree_bytes(b)));
573
574 free_pages((unsigned long) t->data, b->page_order);
575
576 t->prev = NULL;
577 t->tree = NULL;
578 t->data = NULL;
579 list_move(&b->list, &b->c->btree_cache_freed);
580 b->c->bucket_cache_used--;
581}
582
583static void mca_bucket_free(struct btree *b)
584{
585 BUG_ON(btree_node_dirty(b));
586
587 b->key.ptr[0] = 0;
588 hlist_del_init_rcu(&b->hash);
589 list_move(&b->list, &b->c->btree_cache_freeable);
590}
591
592static unsigned btree_order(struct bkey *k)
593{
594 return ilog2(KEY_SIZE(k) / PAGE_SECTORS ?: 1);
595}
596
597static void mca_data_alloc(struct btree *b, struct bkey *k, gfp_t gfp)
598{
599 struct bset_tree *t = b->sets;
600 BUG_ON(t->data);
601
602 b->page_order = max_t(unsigned,
603 ilog2(b->c->btree_pages),
604 btree_order(k));
605
606 t->data = (void *) __get_free_pages(gfp, b->page_order);
607 if (!t->data)
608 goto err;
609
610 t->tree = bset_tree_bytes(b) < PAGE_SIZE
611 ? kmalloc(bset_tree_bytes(b), gfp)
612 : (void *) __get_free_pages(gfp, get_order(bset_tree_bytes(b)));
613 if (!t->tree)
614 goto err;
615
616 t->prev = bset_prev_bytes(b) < PAGE_SIZE
617 ? kmalloc(bset_prev_bytes(b), gfp)
618 : (void *) __get_free_pages(gfp, get_order(bset_prev_bytes(b)));
619 if (!t->prev)
620 goto err;
621
622 list_move(&b->list, &b->c->btree_cache);
623 b->c->bucket_cache_used++;
624 return;
625err:
626 mca_data_free(b);
627}
628
629static struct btree *mca_bucket_alloc(struct cache_set *c,
630 struct bkey *k, gfp_t gfp)
631{
632 struct btree *b = kzalloc(sizeof(struct btree), gfp);
633 if (!b)
634 return NULL;
635
636 init_rwsem(&b->lock);
637 lockdep_set_novalidate_class(&b->lock);
638 INIT_LIST_HEAD(&b->list);
Kent Overstreet57943512013-04-25 13:58:35 -0700639 INIT_DELAYED_WORK(&b->work, btree_node_write_work);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700640 b->c = c;
641 closure_init_unlocked(&b->io);
642
643 mca_data_alloc(b, k, gfp);
644 return b;
645}
646
Kent Overstreete8e1d462013-07-24 17:27:07 -0700647static int mca_reap(struct btree *b, unsigned min_order, bool flush)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700648{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700649 struct closure cl;
650
651 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700652 lockdep_assert_held(&b->c->bucket_lock);
653
654 if (!down_write_trylock(&b->lock))
655 return -ENOMEM;
656
Kent Overstreete8e1d462013-07-24 17:27:07 -0700657 BUG_ON(btree_node_dirty(b) && !b->sets[0].data);
658
659 if (b->page_order < min_order ||
660 (!flush &&
661 (btree_node_dirty(b) ||
662 atomic_read(&b->io.cl.remaining) != -1))) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700663 rw_unlock(true, b);
664 return -ENOMEM;
665 }
666
Kent Overstreetf269af52013-07-23 20:48:29 -0700667 if (btree_node_dirty(b))
668 bch_btree_node_write_sync(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700669
Kent Overstreete8e1d462013-07-24 17:27:07 -0700670 /* wait for any in flight btree write */
Kent Overstreetfaadf0c2013-11-01 18:03:08 -0700671 closure_wait_event(&b->io.wait, &cl,
672 atomic_read(&b->io.cl.remaining) == -1);
Kent Overstreete8e1d462013-07-24 17:27:07 -0700673
Kent Overstreetcafe5632013-03-23 16:11:31 -0700674 return 0;
675}
676
Dave Chinner7dc19d52013-08-28 10:18:11 +1000677static unsigned long bch_mca_scan(struct shrinker *shrink,
678 struct shrink_control *sc)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700679{
680 struct cache_set *c = container_of(shrink, struct cache_set, shrink);
681 struct btree *b, *t;
682 unsigned long i, nr = sc->nr_to_scan;
Dave Chinner7dc19d52013-08-28 10:18:11 +1000683 unsigned long freed = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700684
685 if (c->shrinker_disabled)
Dave Chinner7dc19d52013-08-28 10:18:11 +1000686 return SHRINK_STOP;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700687
688 if (c->try_harder)
Dave Chinner7dc19d52013-08-28 10:18:11 +1000689 return SHRINK_STOP;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700690
691 /* Return -1 if we can't do anything right now */
Kent Overstreeta698e082013-09-23 23:17:34 -0700692 if (sc->gfp_mask & __GFP_IO)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700693 mutex_lock(&c->bucket_lock);
694 else if (!mutex_trylock(&c->bucket_lock))
695 return -1;
696
Kent Overstreet36c9ea92013-06-03 13:04:56 -0700697 /*
698 * It's _really_ critical that we don't free too many btree nodes - we
699 * have to always leave ourselves a reserve. The reserve is how we
700 * guarantee that allocating memory for a new btree node can always
701 * succeed, so that inserting keys into the btree can always succeed and
702 * IO can always make forward progress:
703 */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700704 nr /= c->btree_pages;
705 nr = min_t(unsigned long, nr, mca_can_free(c));
706
707 i = 0;
708 list_for_each_entry_safe(b, t, &c->btree_cache_freeable, list) {
Dave Chinner7dc19d52013-08-28 10:18:11 +1000709 if (freed >= nr)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700710 break;
711
712 if (++i > 3 &&
Kent Overstreete8e1d462013-07-24 17:27:07 -0700713 !mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700714 mca_data_free(b);
715 rw_unlock(true, b);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000716 freed++;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700717 }
718 }
719
720 /*
721 * Can happen right when we first start up, before we've read in any
722 * btree nodes
723 */
724 if (list_empty(&c->btree_cache))
725 goto out;
726
Dave Chinner7dc19d52013-08-28 10:18:11 +1000727 for (i = 0; (nr--) && i < c->bucket_cache_used; i++) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700728 b = list_first_entry(&c->btree_cache, struct btree, list);
729 list_rotate_left(&c->btree_cache);
730
731 if (!b->accessed &&
Kent Overstreete8e1d462013-07-24 17:27:07 -0700732 !mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700733 mca_bucket_free(b);
734 mca_data_free(b);
735 rw_unlock(true, b);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000736 freed++;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700737 } else
738 b->accessed = 0;
739 }
740out:
Kent Overstreetcafe5632013-03-23 16:11:31 -0700741 mutex_unlock(&c->bucket_lock);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000742 return freed;
743}
744
745static unsigned long bch_mca_count(struct shrinker *shrink,
746 struct shrink_control *sc)
747{
748 struct cache_set *c = container_of(shrink, struct cache_set, shrink);
749
750 if (c->shrinker_disabled)
751 return 0;
752
753 if (c->try_harder)
754 return 0;
755
756 return mca_can_free(c) * c->btree_pages;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700757}
758
759void bch_btree_cache_free(struct cache_set *c)
760{
761 struct btree *b;
762 struct closure cl;
763 closure_init_stack(&cl);
764
765 if (c->shrink.list.next)
766 unregister_shrinker(&c->shrink);
767
768 mutex_lock(&c->bucket_lock);
769
770#ifdef CONFIG_BCACHE_DEBUG
771 if (c->verify_data)
772 list_move(&c->verify_data->list, &c->btree_cache);
773#endif
774
775 list_splice(&c->btree_cache_freeable,
776 &c->btree_cache);
777
778 while (!list_empty(&c->btree_cache)) {
779 b = list_first_entry(&c->btree_cache, struct btree, list);
780
781 if (btree_node_dirty(b))
782 btree_complete_write(b, btree_current_write(b));
783 clear_bit(BTREE_NODE_dirty, &b->flags);
784
785 mca_data_free(b);
786 }
787
788 while (!list_empty(&c->btree_cache_freed)) {
789 b = list_first_entry(&c->btree_cache_freed,
790 struct btree, list);
791 list_del(&b->list);
792 cancel_delayed_work_sync(&b->work);
793 kfree(b);
794 }
795
796 mutex_unlock(&c->bucket_lock);
797}
798
799int bch_btree_cache_alloc(struct cache_set *c)
800{
801 unsigned i;
802
Kent Overstreetcafe5632013-03-23 16:11:31 -0700803 for (i = 0; i < mca_reserve(c); i++)
Kent Overstreet72a44512013-10-24 17:19:26 -0700804 if (!mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL))
805 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700806
807 list_splice_init(&c->btree_cache,
808 &c->btree_cache_freeable);
809
810#ifdef CONFIG_BCACHE_DEBUG
811 mutex_init(&c->verify_lock);
812
813 c->verify_data = mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL);
814
815 if (c->verify_data &&
816 c->verify_data->sets[0].data)
817 list_del_init(&c->verify_data->list);
818 else
819 c->verify_data = NULL;
820#endif
821
Dave Chinner7dc19d52013-08-28 10:18:11 +1000822 c->shrink.count_objects = bch_mca_count;
823 c->shrink.scan_objects = bch_mca_scan;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700824 c->shrink.seeks = 4;
825 c->shrink.batch = c->btree_pages * 2;
826 register_shrinker(&c->shrink);
827
828 return 0;
829}
830
831/* Btree in memory cache - hash table */
832
833static struct hlist_head *mca_hash(struct cache_set *c, struct bkey *k)
834{
835 return &c->bucket_hash[hash_32(PTR_HASH(c, k), BUCKET_HASH_BITS)];
836}
837
838static struct btree *mca_find(struct cache_set *c, struct bkey *k)
839{
840 struct btree *b;
841
842 rcu_read_lock();
843 hlist_for_each_entry_rcu(b, mca_hash(c, k), hash)
844 if (PTR_HASH(c, &b->key) == PTR_HASH(c, k))
845 goto out;
846 b = NULL;
847out:
848 rcu_read_unlock();
849 return b;
850}
851
Kent Overstreete8e1d462013-07-24 17:27:07 -0700852static struct btree *mca_cannibalize(struct cache_set *c, struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700853{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700854 struct btree *b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700855
Kent Overstreetc37511b2013-04-26 15:39:55 -0700856 trace_bcache_btree_cache_cannibalize(c);
857
Kent Overstreete8e1d462013-07-24 17:27:07 -0700858 if (!c->try_harder) {
859 c->try_harder = current;
860 c->try_harder_start = local_clock();
861 } else if (c->try_harder != current)
862 return ERR_PTR(-ENOSPC);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700863
Kent Overstreete8e1d462013-07-24 17:27:07 -0700864 list_for_each_entry_reverse(b, &c->btree_cache, list)
865 if (!mca_reap(b, btree_order(k), false))
866 return b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700867
Kent Overstreete8e1d462013-07-24 17:27:07 -0700868 list_for_each_entry_reverse(b, &c->btree_cache, list)
869 if (!mca_reap(b, btree_order(k), true))
870 return b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700871
Kent Overstreete8e1d462013-07-24 17:27:07 -0700872 return ERR_PTR(-ENOMEM);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700873}
874
875/*
876 * We can only have one thread cannibalizing other cached btree nodes at a time,
877 * or we'll deadlock. We use an open coded mutex to ensure that, which a
878 * cannibalize_bucket() will take. This means every time we unlock the root of
879 * the btree, we need to release this lock if we have it held.
880 */
Kent Overstreetdf8e8972013-07-24 17:37:59 -0700881static void bch_cannibalize_unlock(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700882{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700883 if (c->try_harder == current) {
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600884 bch_time_stats_update(&c->try_harder_time, c->try_harder_start);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700885 c->try_harder = NULL;
Kent Overstreete8e1d462013-07-24 17:27:07 -0700886 wake_up(&c->try_wait);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700887 }
888}
889
Kent Overstreete8e1d462013-07-24 17:27:07 -0700890static struct btree *mca_alloc(struct cache_set *c, struct bkey *k, int level)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700891{
892 struct btree *b;
893
Kent Overstreete8e1d462013-07-24 17:27:07 -0700894 BUG_ON(current->bio_list);
895
Kent Overstreetcafe5632013-03-23 16:11:31 -0700896 lockdep_assert_held(&c->bucket_lock);
897
898 if (mca_find(c, k))
899 return NULL;
900
901 /* btree_free() doesn't free memory; it sticks the node on the end of
902 * the list. Check if there's any freed nodes there:
903 */
904 list_for_each_entry(b, &c->btree_cache_freeable, list)
Kent Overstreete8e1d462013-07-24 17:27:07 -0700905 if (!mca_reap(b, btree_order(k), false))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700906 goto out;
907
908 /* We never free struct btree itself, just the memory that holds the on
909 * disk node. Check the freed list before allocating a new one:
910 */
911 list_for_each_entry(b, &c->btree_cache_freed, list)
Kent Overstreete8e1d462013-07-24 17:27:07 -0700912 if (!mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700913 mca_data_alloc(b, k, __GFP_NOWARN|GFP_NOIO);
914 if (!b->sets[0].data)
915 goto err;
916 else
917 goto out;
918 }
919
920 b = mca_bucket_alloc(c, k, __GFP_NOWARN|GFP_NOIO);
921 if (!b)
922 goto err;
923
924 BUG_ON(!down_write_trylock(&b->lock));
925 if (!b->sets->data)
926 goto err;
927out:
928 BUG_ON(!closure_is_unlocked(&b->io.cl));
929
930 bkey_copy(&b->key, k);
931 list_move(&b->list, &c->btree_cache);
932 hlist_del_init_rcu(&b->hash);
933 hlist_add_head_rcu(&b->hash, mca_hash(c, k));
934
935 lock_set_subclass(&b->lock.dep_map, level + 1, _THIS_IP_);
936 b->level = level;
Kent Overstreetd6fd3b12013-07-24 17:20:19 -0700937 b->parent = (void *) ~0UL;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700938
939 mca_reinit(b);
940
941 return b;
942err:
943 if (b)
944 rw_unlock(true, b);
945
Kent Overstreete8e1d462013-07-24 17:27:07 -0700946 b = mca_cannibalize(c, k);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700947 if (!IS_ERR(b))
948 goto out;
949
950 return b;
951}
952
953/**
954 * bch_btree_node_get - find a btree node in the cache and lock it, reading it
955 * in from disk if necessary.
956 *
Kent Overstreetb54d6932013-07-24 18:04:18 -0700957 * If IO is necessary and running under generic_make_request, returns -EAGAIN.
Kent Overstreetcafe5632013-03-23 16:11:31 -0700958 *
959 * The btree node will have either a read or a write lock held, depending on
960 * level and op->lock.
961 */
962struct btree *bch_btree_node_get(struct cache_set *c, struct bkey *k,
Kent Overstreete8e1d462013-07-24 17:27:07 -0700963 int level, bool write)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700964{
965 int i = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700966 struct btree *b;
967
968 BUG_ON(level < 0);
969retry:
970 b = mca_find(c, k);
971
972 if (!b) {
Kent Overstreet57943512013-04-25 13:58:35 -0700973 if (current->bio_list)
974 return ERR_PTR(-EAGAIN);
975
Kent Overstreetcafe5632013-03-23 16:11:31 -0700976 mutex_lock(&c->bucket_lock);
Kent Overstreete8e1d462013-07-24 17:27:07 -0700977 b = mca_alloc(c, k, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700978 mutex_unlock(&c->bucket_lock);
979
980 if (!b)
981 goto retry;
982 if (IS_ERR(b))
983 return b;
984
Kent Overstreet57943512013-04-25 13:58:35 -0700985 bch_btree_node_read(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700986
987 if (!write)
988 downgrade_write(&b->lock);
989 } else {
990 rw_lock(write, b, level);
991 if (PTR_HASH(c, &b->key) != PTR_HASH(c, k)) {
992 rw_unlock(write, b);
993 goto retry;
994 }
995 BUG_ON(b->level != level);
996 }
997
998 b->accessed = 1;
999
1000 for (; i <= b->nsets && b->sets[i].size; i++) {
1001 prefetch(b->sets[i].tree);
1002 prefetch(b->sets[i].data);
1003 }
1004
1005 for (; i <= b->nsets; i++)
1006 prefetch(b->sets[i].data);
1007
Kent Overstreet57943512013-04-25 13:58:35 -07001008 if (btree_node_io_error(b)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001009 rw_unlock(write, b);
Kent Overstreet57943512013-04-25 13:58:35 -07001010 return ERR_PTR(-EIO);
1011 }
1012
1013 BUG_ON(!b->written);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001014
1015 return b;
1016}
1017
1018static void btree_node_prefetch(struct cache_set *c, struct bkey *k, int level)
1019{
1020 struct btree *b;
1021
1022 mutex_lock(&c->bucket_lock);
Kent Overstreete8e1d462013-07-24 17:27:07 -07001023 b = mca_alloc(c, k, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001024 mutex_unlock(&c->bucket_lock);
1025
1026 if (!IS_ERR_OR_NULL(b)) {
Kent Overstreet57943512013-04-25 13:58:35 -07001027 bch_btree_node_read(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001028 rw_unlock(true, b);
1029 }
1030}
1031
1032/* Btree alloc */
1033
Kent Overstreete8e1d462013-07-24 17:27:07 -07001034static void btree_node_free(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001035{
1036 unsigned i;
1037
Kent Overstreetc37511b2013-04-26 15:39:55 -07001038 trace_bcache_btree_node_free(b);
1039
Kent Overstreetcafe5632013-03-23 16:11:31 -07001040 BUG_ON(b == b->c->root);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001041
1042 if (btree_node_dirty(b))
1043 btree_complete_write(b, btree_current_write(b));
1044 clear_bit(BTREE_NODE_dirty, &b->flags);
1045
Kent Overstreetcafe5632013-03-23 16:11:31 -07001046 cancel_delayed_work(&b->work);
1047
1048 mutex_lock(&b->c->bucket_lock);
1049
1050 for (i = 0; i < KEY_PTRS(&b->key); i++) {
1051 BUG_ON(atomic_read(&PTR_BUCKET(b->c, &b->key, i)->pin));
1052
1053 bch_inc_gen(PTR_CACHE(b->c, &b->key, i),
1054 PTR_BUCKET(b->c, &b->key, i));
1055 }
1056
1057 bch_bucket_free(b->c, &b->key);
1058 mca_bucket_free(b);
1059 mutex_unlock(&b->c->bucket_lock);
1060}
1061
Kent Overstreet35fcd842013-07-24 17:29:09 -07001062struct btree *bch_btree_node_alloc(struct cache_set *c, int level)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001063{
1064 BKEY_PADDED(key) k;
1065 struct btree *b = ERR_PTR(-EAGAIN);
1066
1067 mutex_lock(&c->bucket_lock);
1068retry:
Kent Overstreet35fcd842013-07-24 17:29:09 -07001069 if (__bch_bucket_alloc_set(c, WATERMARK_METADATA, &k.key, 1, true))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001070 goto err;
1071
Kent Overstreet3a3b6a42013-07-24 16:46:42 -07001072 bkey_put(c, &k.key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001073 SET_KEY_SIZE(&k.key, c->btree_pages * PAGE_SECTORS);
1074
Kent Overstreete8e1d462013-07-24 17:27:07 -07001075 b = mca_alloc(c, &k.key, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001076 if (IS_ERR(b))
1077 goto err_free;
1078
1079 if (!b) {
Kent Overstreetb1a67b02013-03-25 11:46:44 -07001080 cache_bug(c,
1081 "Tried to allocate bucket that was in btree cache");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001082 goto retry;
1083 }
1084
Kent Overstreetcafe5632013-03-23 16:11:31 -07001085 b->accessed = 1;
1086 bch_bset_init_next(b);
1087
1088 mutex_unlock(&c->bucket_lock);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001089
1090 trace_bcache_btree_node_alloc(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001091 return b;
1092err_free:
1093 bch_bucket_free(c, &k.key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001094err:
1095 mutex_unlock(&c->bucket_lock);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001096
1097 trace_bcache_btree_node_alloc_fail(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001098 return b;
1099}
1100
Kent Overstreet35fcd842013-07-24 17:29:09 -07001101static struct btree *btree_node_alloc_replacement(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001102{
Kent Overstreet35fcd842013-07-24 17:29:09 -07001103 struct btree *n = bch_btree_node_alloc(b->c, b->level);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001104 if (!IS_ERR_OR_NULL(n))
1105 bch_btree_sort_into(b, n);
1106
1107 return n;
1108}
1109
Kent Overstreet8835c122013-07-24 23:18:05 -07001110static void make_btree_freeing_key(struct btree *b, struct bkey *k)
1111{
1112 unsigned i;
1113
1114 bkey_copy(k, &b->key);
1115 bkey_copy_key(k, &ZERO_KEY);
1116
1117 for (i = 0; i < KEY_PTRS(k); i++) {
1118 uint8_t g = PTR_BUCKET(b->c, k, i)->gen + 1;
1119
1120 SET_PTR_GEN(k, i, g);
1121 }
1122
1123 atomic_inc(&b->c->prio_blocked);
1124}
1125
Kent Overstreetcafe5632013-03-23 16:11:31 -07001126/* Garbage collection */
1127
1128uint8_t __bch_btree_mark_key(struct cache_set *c, int level, struct bkey *k)
1129{
1130 uint8_t stale = 0;
1131 unsigned i;
1132 struct bucket *g;
1133
1134 /*
1135 * ptr_invalid() can't return true for the keys that mark btree nodes as
1136 * freed, but since ptr_bad() returns true we'll never actually use them
1137 * for anything and thus we don't want mark their pointers here
1138 */
1139 if (!bkey_cmp(k, &ZERO_KEY))
1140 return stale;
1141
1142 for (i = 0; i < KEY_PTRS(k); i++) {
1143 if (!ptr_available(c, k, i))
1144 continue;
1145
1146 g = PTR_BUCKET(c, k, i);
1147
1148 if (gen_after(g->gc_gen, PTR_GEN(k, i)))
1149 g->gc_gen = PTR_GEN(k, i);
1150
1151 if (ptr_stale(c, k, i)) {
1152 stale = max(stale, ptr_stale(c, k, i));
1153 continue;
1154 }
1155
1156 cache_bug_on(GC_MARK(g) &&
1157 (GC_MARK(g) == GC_MARK_METADATA) != (level != 0),
1158 c, "inconsistent ptrs: mark = %llu, level = %i",
1159 GC_MARK(g), level);
1160
1161 if (level)
1162 SET_GC_MARK(g, GC_MARK_METADATA);
1163 else if (KEY_DIRTY(k))
1164 SET_GC_MARK(g, GC_MARK_DIRTY);
1165
1166 /* guard against overflow */
1167 SET_GC_SECTORS_USED(g, min_t(unsigned,
1168 GC_SECTORS_USED(g) + KEY_SIZE(k),
1169 (1 << 14) - 1));
1170
1171 BUG_ON(!GC_SECTORS_USED(g));
1172 }
1173
1174 return stale;
1175}
1176
1177#define btree_mark_key(b, k) __bch_btree_mark_key(b->c, b->level, k)
1178
Kent Overstreeta1f03582013-09-10 19:07:00 -07001179static bool btree_gc_mark_node(struct btree *b, struct gc_stat *gc)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001180{
1181 uint8_t stale = 0;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001182 unsigned keys = 0, good_keys = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001183 struct bkey *k;
1184 struct btree_iter iter;
1185 struct bset_tree *t;
1186
1187 gc->nodes++;
1188
1189 for_each_key_filter(b, k, &iter, bch_ptr_invalid) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001190 stale = max(stale, btree_mark_key(b, k));
Kent Overstreeta1f03582013-09-10 19:07:00 -07001191 keys++;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001192
1193 if (bch_ptr_bad(b, k))
1194 continue;
1195
Kent Overstreetcafe5632013-03-23 16:11:31 -07001196 gc->key_bytes += bkey_u64s(k);
1197 gc->nkeys++;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001198 good_keys++;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001199
1200 gc->data += KEY_SIZE(k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001201 }
1202
1203 for (t = b->sets; t <= &b->sets[b->nsets]; t++)
1204 btree_bug_on(t->size &&
1205 bset_written(b, t) &&
1206 bkey_cmp(&b->key, &t->end) < 0,
1207 b, "found short btree key in gc");
1208
Kent Overstreeta1f03582013-09-10 19:07:00 -07001209 if (b->c->gc_always_rewrite)
1210 return true;
1211
1212 if (stale > 10)
1213 return true;
1214
1215 if ((keys - good_keys) * 2 > keys)
1216 return true;
1217
1218 return false;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001219}
1220
Kent Overstreeta1f03582013-09-10 19:07:00 -07001221#define GC_MERGE_NODES 4U
Kent Overstreetcafe5632013-03-23 16:11:31 -07001222
1223struct gc_merge_info {
1224 struct btree *b;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001225 unsigned keys;
1226};
1227
Kent Overstreeta1f03582013-09-10 19:07:00 -07001228static int bch_btree_insert_node(struct btree *, struct btree_op *,
1229 struct keylist *, atomic_t *, struct bkey *);
Kent Overstreetb54d6932013-07-24 18:04:18 -07001230
Kent Overstreeta1f03582013-09-10 19:07:00 -07001231static int btree_gc_coalesce(struct btree *b, struct btree_op *op,
1232 struct keylist *keylist, struct gc_stat *gc,
1233 struct gc_merge_info *r)
1234{
1235 unsigned i, nodes = 0, keys = 0, blocks;
1236 struct btree *new_nodes[GC_MERGE_NODES];
1237 struct closure cl;
1238 struct bkey *k;
1239
1240 memset(new_nodes, 0, sizeof(new_nodes));
Kent Overstreetb54d6932013-07-24 18:04:18 -07001241 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001242
Kent Overstreeta1f03582013-09-10 19:07:00 -07001243 while (nodes < GC_MERGE_NODES && !IS_ERR_OR_NULL(r[nodes].b))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001244 keys += r[nodes++].keys;
1245
1246 blocks = btree_default_blocks(b->c) * 2 / 3;
1247
1248 if (nodes < 2 ||
1249 __set_blocks(b->sets[0].data, keys, b->c) > blocks * (nodes - 1))
Kent Overstreeta1f03582013-09-10 19:07:00 -07001250 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001251
Kent Overstreeta1f03582013-09-10 19:07:00 -07001252 for (i = 0; i < nodes; i++) {
1253 new_nodes[i] = btree_node_alloc_replacement(r[i].b);
1254 if (IS_ERR_OR_NULL(new_nodes[i]))
1255 goto out_nocoalesce;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001256 }
1257
1258 for (i = nodes - 1; i > 0; --i) {
Kent Overstreeta1f03582013-09-10 19:07:00 -07001259 struct bset *n1 = new_nodes[i]->sets->data;
1260 struct bset *n2 = new_nodes[i - 1]->sets->data;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001261 struct bkey *k, *last = NULL;
1262
1263 keys = 0;
1264
Kent Overstreeta1f03582013-09-10 19:07:00 -07001265 if (i > 1) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001266 for (k = n2->start;
1267 k < end(n2);
1268 k = bkey_next(k)) {
1269 if (__set_blocks(n1, n1->keys + keys +
1270 bkey_u64s(k), b->c) > blocks)
1271 break;
1272
1273 last = k;
1274 keys += bkey_u64s(k);
1275 }
Kent Overstreeta1f03582013-09-10 19:07:00 -07001276 } else {
1277 /*
1278 * Last node we're not getting rid of - we're getting
1279 * rid of the node at r[0]. Have to try and fit all of
1280 * the remaining keys into this node; we can't ensure
1281 * they will always fit due to rounding and variable
1282 * length keys (shouldn't be possible in practice,
1283 * though)
1284 */
1285 if (__set_blocks(n1, n1->keys + n2->keys,
1286 b->c) > btree_blocks(new_nodes[i]))
1287 goto out_nocoalesce;
1288
1289 keys = n2->keys;
1290 /* Take the key of the node we're getting rid of */
1291 last = &r->b->key;
1292 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001293
1294 BUG_ON(__set_blocks(n1, n1->keys + keys,
Kent Overstreeta1f03582013-09-10 19:07:00 -07001295 b->c) > btree_blocks(new_nodes[i]));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001296
Kent Overstreeta1f03582013-09-10 19:07:00 -07001297 if (last)
1298 bkey_copy_key(&new_nodes[i]->key, last);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001299
1300 memcpy(end(n1),
1301 n2->start,
1302 (void *) node(n2, keys) - (void *) n2->start);
1303
1304 n1->keys += keys;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001305 r[i].keys = n1->keys;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001306
1307 memmove(n2->start,
1308 node(n2, keys),
1309 (void *) end(n2) - (void *) node(n2, keys));
1310
1311 n2->keys -= keys;
1312
Kent Overstreeta1f03582013-09-10 19:07:00 -07001313 if (bch_keylist_realloc(keylist,
1314 KEY_PTRS(&new_nodes[i]->key), b->c))
1315 goto out_nocoalesce;
1316
1317 bch_btree_node_write(new_nodes[i], &cl);
1318 bch_keylist_add(keylist, &new_nodes[i]->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001319 }
1320
Kent Overstreeta1f03582013-09-10 19:07:00 -07001321 for (i = 0; i < nodes; i++) {
1322 if (bch_keylist_realloc(keylist, KEY_PTRS(&r[i].b->key), b->c))
1323 goto out_nocoalesce;
1324
1325 make_btree_freeing_key(r[i].b, keylist->top);
1326 bch_keylist_push(keylist);
1327 }
1328
1329 /* We emptied out this node */
1330 BUG_ON(new_nodes[0]->sets->data->keys);
1331 btree_node_free(new_nodes[0]);
1332 rw_unlock(true, new_nodes[0]);
1333
1334 closure_sync(&cl);
1335
1336 for (i = 0; i < nodes; i++) {
1337 btree_node_free(r[i].b);
1338 rw_unlock(true, r[i].b);
1339
1340 r[i].b = new_nodes[i];
1341 }
1342
1343 bch_btree_insert_node(b, op, keylist, NULL, NULL);
1344 BUG_ON(!bch_keylist_empty(keylist));
1345
1346 memmove(r, r + 1, sizeof(r[0]) * (nodes - 1));
1347 r[nodes - 1].b = ERR_PTR(-EINTR);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001348
Kent Overstreetc37511b2013-04-26 15:39:55 -07001349 trace_bcache_btree_gc_coalesce(nodes);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001350 gc->nodes--;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001351
Kent Overstreeta1f03582013-09-10 19:07:00 -07001352 /* Invalidated our iterator */
1353 return -EINTR;
1354
1355out_nocoalesce:
1356 closure_sync(&cl);
1357
1358 while ((k = bch_keylist_pop(keylist)))
1359 if (!bkey_cmp(k, &ZERO_KEY))
1360 atomic_dec(&b->c->prio_blocked);
1361
1362 for (i = 0; i < nodes; i++)
1363 if (!IS_ERR_OR_NULL(new_nodes[i])) {
1364 btree_node_free(new_nodes[i]);
1365 rw_unlock(true, new_nodes[i]);
1366 }
1367 return 0;
1368}
1369
1370static unsigned btree_gc_count_keys(struct btree *b)
1371{
1372 struct bkey *k;
1373 struct btree_iter iter;
1374 unsigned ret = 0;
1375
1376 for_each_key_filter(b, k, &iter, bch_ptr_bad)
1377 ret += bkey_u64s(k);
1378
1379 return ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001380}
1381
1382static int btree_gc_recurse(struct btree *b, struct btree_op *op,
1383 struct closure *writes, struct gc_stat *gc)
1384{
Kent Overstreetcafe5632013-03-23 16:11:31 -07001385 unsigned i;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001386 int ret = 0;
1387 bool should_rewrite;
1388 struct btree *n;
1389 struct bkey *k;
1390 struct keylist keys;
1391 struct btree_iter iter;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001392 struct gc_merge_info r[GC_MERGE_NODES];
Kent Overstreeta1f03582013-09-10 19:07:00 -07001393 struct gc_merge_info *last = r + GC_MERGE_NODES - 1;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001394
Kent Overstreeta1f03582013-09-10 19:07:00 -07001395 bch_keylist_init(&keys);
1396 bch_btree_iter_init(b, &iter, &b->c->gc_done);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001397
Kent Overstreeta1f03582013-09-10 19:07:00 -07001398 for (i = 0; i < GC_MERGE_NODES; i++)
1399 r[i].b = ERR_PTR(-EINTR);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001400
Kent Overstreeta1f03582013-09-10 19:07:00 -07001401 while (1) {
1402 k = bch_btree_iter_next_filter(&iter, b, bch_ptr_bad);
1403 if (k) {
1404 r->b = bch_btree_node_get(b->c, k, b->level - 1, true);
1405 if (IS_ERR(r->b)) {
1406 ret = PTR_ERR(r->b);
1407 break;
1408 }
1409
1410 r->keys = btree_gc_count_keys(r->b);
1411
1412 ret = btree_gc_coalesce(b, op, &keys, gc, r);
1413 if (ret)
1414 break;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001415 }
1416
Kent Overstreeta1f03582013-09-10 19:07:00 -07001417 if (!last->b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001418 break;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001419
1420 if (!IS_ERR(last->b)) {
1421 should_rewrite = btree_gc_mark_node(last->b, gc);
1422 if (should_rewrite) {
1423 n = btree_node_alloc_replacement(last->b);
1424
1425 if (!IS_ERR_OR_NULL(n)) {
1426 bch_btree_node_write_sync(n);
1427 bch_keylist_add(&keys, &n->key);
1428
1429 make_btree_freeing_key(last->b,
1430 keys.top);
1431 bch_keylist_push(&keys);
1432
1433 btree_node_free(last->b);
1434
1435 bch_btree_insert_node(b, op, &keys,
1436 NULL, NULL);
1437 BUG_ON(!bch_keylist_empty(&keys));
1438
1439 rw_unlock(true, last->b);
1440 last->b = n;
1441
1442 /* Invalidated our iterator */
1443 ret = -EINTR;
1444 break;
1445 }
1446 }
1447
1448 if (last->b->level) {
1449 ret = btree_gc_recurse(last->b, op, writes, gc);
1450 if (ret)
1451 break;
1452 }
1453
1454 bkey_copy_key(&b->c->gc_done, &last->b->key);
1455
1456 /*
1457 * Must flush leaf nodes before gc ends, since replace
1458 * operations aren't journalled
1459 */
1460 if (btree_node_dirty(last->b))
1461 bch_btree_node_write(last->b, writes);
1462 rw_unlock(true, last->b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001463 }
1464
Kent Overstreeta1f03582013-09-10 19:07:00 -07001465 memmove(r + 1, r, sizeof(r[0]) * (GC_MERGE_NODES - 1));
1466 r->b = NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001467
Kent Overstreetcafe5632013-03-23 16:11:31 -07001468 if (need_resched()) {
1469 ret = -EAGAIN;
1470 break;
1471 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001472 }
1473
Kent Overstreeta1f03582013-09-10 19:07:00 -07001474 for (i = 0; i < GC_MERGE_NODES; i++)
1475 if (!IS_ERR_OR_NULL(r[i].b)) {
1476 if (btree_node_dirty(r[i].b))
1477 bch_btree_node_write(r[i].b, writes);
1478 rw_unlock(true, r[i].b);
1479 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001480
Kent Overstreeta1f03582013-09-10 19:07:00 -07001481 bch_keylist_free(&keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001482
1483 return ret;
1484}
1485
1486static int bch_btree_gc_root(struct btree *b, struct btree_op *op,
1487 struct closure *writes, struct gc_stat *gc)
1488{
1489 struct btree *n = NULL;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001490 int ret = 0;
1491 bool should_rewrite;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001492
Kent Overstreeta1f03582013-09-10 19:07:00 -07001493 should_rewrite = btree_gc_mark_node(b, gc);
1494 if (should_rewrite) {
Kent Overstreet35fcd842013-07-24 17:29:09 -07001495 n = btree_node_alloc_replacement(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001496
Kent Overstreeta1f03582013-09-10 19:07:00 -07001497 if (!IS_ERR_OR_NULL(n)) {
1498 bch_btree_node_write_sync(n);
1499 bch_btree_set_root(n);
1500 btree_node_free(b);
1501 rw_unlock(true, n);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001502
Kent Overstreeta1f03582013-09-10 19:07:00 -07001503 return -EINTR;
1504 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001505 }
1506
Kent Overstreeta1f03582013-09-10 19:07:00 -07001507 if (b->level) {
1508 ret = btree_gc_recurse(b, op, writes, gc);
1509 if (ret)
1510 return ret;
1511 }
1512
1513 bkey_copy_key(&b->c->gc_done, &b->key);
1514
Kent Overstreetcafe5632013-03-23 16:11:31 -07001515 return ret;
1516}
1517
1518static void btree_gc_start(struct cache_set *c)
1519{
1520 struct cache *ca;
1521 struct bucket *b;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001522 unsigned i;
1523
1524 if (!c->gc_mark_valid)
1525 return;
1526
1527 mutex_lock(&c->bucket_lock);
1528
1529 c->gc_mark_valid = 0;
1530 c->gc_done = ZERO_KEY;
1531
1532 for_each_cache(ca, c, i)
1533 for_each_bucket(b, ca) {
1534 b->gc_gen = b->gen;
Kent Overstreet29ebf462013-07-11 19:43:21 -07001535 if (!atomic_read(&b->pin)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001536 SET_GC_MARK(b, GC_MARK_RECLAIMABLE);
Kent Overstreet29ebf462013-07-11 19:43:21 -07001537 SET_GC_SECTORS_USED(b, 0);
1538 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001539 }
1540
Kent Overstreetcafe5632013-03-23 16:11:31 -07001541 mutex_unlock(&c->bucket_lock);
1542}
1543
1544size_t bch_btree_gc_finish(struct cache_set *c)
1545{
1546 size_t available = 0;
1547 struct bucket *b;
1548 struct cache *ca;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001549 unsigned i;
1550
1551 mutex_lock(&c->bucket_lock);
1552
1553 set_gc_sectors(c);
1554 c->gc_mark_valid = 1;
1555 c->need_gc = 0;
1556
1557 if (c->root)
1558 for (i = 0; i < KEY_PTRS(&c->root->key); i++)
1559 SET_GC_MARK(PTR_BUCKET(c, &c->root->key, i),
1560 GC_MARK_METADATA);
1561
1562 for (i = 0; i < KEY_PTRS(&c->uuid_bucket); i++)
1563 SET_GC_MARK(PTR_BUCKET(c, &c->uuid_bucket, i),
1564 GC_MARK_METADATA);
1565
1566 for_each_cache(ca, c, i) {
1567 uint64_t *i;
1568
1569 ca->invalidate_needs_gc = 0;
1570
1571 for (i = ca->sb.d; i < ca->sb.d + ca->sb.keys; i++)
1572 SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA);
1573
1574 for (i = ca->prio_buckets;
1575 i < ca->prio_buckets + prio_buckets(ca) * 2; i++)
1576 SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA);
1577
1578 for_each_bucket(b, ca) {
1579 b->last_gc = b->gc_gen;
1580 c->need_gc = max(c->need_gc, bucket_gc_gen(b));
1581
1582 if (!atomic_read(&b->pin) &&
1583 GC_MARK(b) == GC_MARK_RECLAIMABLE) {
1584 available++;
1585 if (!GC_SECTORS_USED(b))
1586 bch_bucket_add_unused(ca, b);
1587 }
1588 }
1589 }
1590
Kent Overstreetcafe5632013-03-23 16:11:31 -07001591 mutex_unlock(&c->bucket_lock);
1592 return available;
1593}
1594
Kent Overstreet72a44512013-10-24 17:19:26 -07001595static void bch_btree_gc(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001596{
Kent Overstreetcafe5632013-03-23 16:11:31 -07001597 int ret;
1598 unsigned long available;
1599 struct gc_stat stats;
1600 struct closure writes;
1601 struct btree_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001602 uint64_t start_time = local_clock();
Kent Overstreet57943512013-04-25 13:58:35 -07001603
Kent Overstreetc37511b2013-04-26 15:39:55 -07001604 trace_bcache_gc_start(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001605
1606 memset(&stats, 0, sizeof(struct gc_stat));
1607 closure_init_stack(&writes);
Kent Overstreetb54d6932013-07-24 18:04:18 -07001608 bch_btree_op_init(&op, SHRT_MAX);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001609
1610 btree_gc_start(c);
1611
Kent Overstreeta1f03582013-09-10 19:07:00 -07001612 do {
1613 ret = btree_root(gc_root, c, &op, &writes, &stats);
1614 closure_sync(&writes);
Kent Overstreet57943512013-04-25 13:58:35 -07001615
Kent Overstreeta1f03582013-09-10 19:07:00 -07001616 if (ret && ret != -EAGAIN)
1617 pr_warn("gc failed!");
1618 } while (ret);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001619
1620 available = bch_btree_gc_finish(c);
Kent Overstreet57943512013-04-25 13:58:35 -07001621 wake_up_allocators(c);
1622
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001623 bch_time_stats_update(&c->btree_gc_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001624
1625 stats.key_bytes *= sizeof(uint64_t);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001626 stats.data <<= 9;
1627 stats.in_use = (c->nbuckets - available) * 100 / c->nbuckets;
1628 memcpy(&c->gc_stats, &stats, sizeof(struct gc_stat));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001629
Kent Overstreetc37511b2013-04-26 15:39:55 -07001630 trace_bcache_gc_end(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001631
Kent Overstreet72a44512013-10-24 17:19:26 -07001632 bch_moving_gc(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001633}
1634
Kent Overstreet72a44512013-10-24 17:19:26 -07001635static int bch_gc_thread(void *arg)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001636{
Kent Overstreet72a44512013-10-24 17:19:26 -07001637 struct cache_set *c = arg;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001638 struct cache *ca;
1639 unsigned i;
Kent Overstreet72a44512013-10-24 17:19:26 -07001640
1641 while (1) {
Kent Overstreeta1f03582013-09-10 19:07:00 -07001642again:
Kent Overstreet72a44512013-10-24 17:19:26 -07001643 bch_btree_gc(c);
1644
1645 set_current_state(TASK_INTERRUPTIBLE);
1646 if (kthread_should_stop())
1647 break;
1648
Kent Overstreeta1f03582013-09-10 19:07:00 -07001649 mutex_lock(&c->bucket_lock);
1650
1651 for_each_cache(ca, c, i)
1652 if (ca->invalidate_needs_gc) {
1653 mutex_unlock(&c->bucket_lock);
1654 set_current_state(TASK_RUNNING);
1655 goto again;
1656 }
1657
1658 mutex_unlock(&c->bucket_lock);
1659
Kent Overstreet72a44512013-10-24 17:19:26 -07001660 try_to_freeze();
1661 schedule();
1662 }
1663
1664 return 0;
1665}
1666
1667int bch_gc_thread_start(struct cache_set *c)
1668{
1669 c->gc_thread = kthread_create(bch_gc_thread, c, "bcache_gc");
1670 if (IS_ERR(c->gc_thread))
1671 return PTR_ERR(c->gc_thread);
1672
1673 set_task_state(c->gc_thread, TASK_INTERRUPTIBLE);
1674 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001675}
1676
1677/* Initial partial gc */
1678
1679static int bch_btree_check_recurse(struct btree *b, struct btree_op *op,
1680 unsigned long **seen)
1681{
1682 int ret;
1683 unsigned i;
1684 struct bkey *k;
1685 struct bucket *g;
1686 struct btree_iter iter;
1687
1688 for_each_key_filter(b, k, &iter, bch_ptr_invalid) {
1689 for (i = 0; i < KEY_PTRS(k); i++) {
1690 if (!ptr_available(b->c, k, i))
1691 continue;
1692
1693 g = PTR_BUCKET(b->c, k, i);
1694
1695 if (!__test_and_set_bit(PTR_BUCKET_NR(b->c, k, i),
1696 seen[PTR_DEV(k, i)]) ||
1697 !ptr_stale(b->c, k, i)) {
1698 g->gen = PTR_GEN(k, i);
1699
1700 if (b->level)
1701 g->prio = BTREE_PRIO;
1702 else if (g->prio == BTREE_PRIO)
1703 g->prio = INITIAL_PRIO;
1704 }
1705 }
1706
1707 btree_mark_key(b, k);
1708 }
1709
1710 if (b->level) {
1711 k = bch_next_recurse_key(b, &ZERO_KEY);
1712
1713 while (k) {
1714 struct bkey *p = bch_next_recurse_key(b, k);
1715 if (p)
1716 btree_node_prefetch(b->c, p, b->level - 1);
1717
1718 ret = btree(check_recurse, k, b, op, seen);
1719 if (ret)
1720 return ret;
1721
1722 k = p;
1723 }
1724 }
1725
1726 return 0;
1727}
1728
Kent Overstreetc18536a2013-07-24 17:44:17 -07001729int bch_btree_check(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001730{
1731 int ret = -ENOMEM;
1732 unsigned i;
1733 unsigned long *seen[MAX_CACHES_PER_SET];
Kent Overstreetc18536a2013-07-24 17:44:17 -07001734 struct btree_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001735
1736 memset(seen, 0, sizeof(seen));
Kent Overstreetb54d6932013-07-24 18:04:18 -07001737 bch_btree_op_init(&op, SHRT_MAX);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001738
1739 for (i = 0; c->cache[i]; i++) {
1740 size_t n = DIV_ROUND_UP(c->cache[i]->sb.nbuckets, 8);
1741 seen[i] = kmalloc(n, GFP_KERNEL);
1742 if (!seen[i])
1743 goto err;
1744
1745 /* Disables the seen array until prio_read() uses it too */
1746 memset(seen[i], 0xFF, n);
1747 }
1748
Kent Overstreetc18536a2013-07-24 17:44:17 -07001749 ret = btree_root(check_recurse, c, &op, seen);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001750err:
1751 for (i = 0; i < MAX_CACHES_PER_SET; i++)
1752 kfree(seen[i]);
1753 return ret;
1754}
1755
1756/* Btree insertion */
1757
1758static void shift_keys(struct btree *b, struct bkey *where, struct bkey *insert)
1759{
1760 struct bset *i = b->sets[b->nsets].data;
1761
1762 memmove((uint64_t *) where + bkey_u64s(insert),
1763 where,
1764 (void *) end(i) - (void *) where);
1765
1766 i->keys += bkey_u64s(insert);
1767 bkey_copy(where, insert);
1768 bch_bset_fix_lookup_table(b, where);
1769}
1770
Kent Overstreet1b207d82013-09-10 18:52:54 -07001771static bool fix_overlapping_extents(struct btree *b, struct bkey *insert,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001772 struct btree_iter *iter,
Kent Overstreet1b207d82013-09-10 18:52:54 -07001773 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001774{
Kent Overstreet279afba2013-06-05 06:21:07 -07001775 void subtract_dirty(struct bkey *k, uint64_t offset, int sectors)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001776 {
Kent Overstreet279afba2013-06-05 06:21:07 -07001777 if (KEY_DIRTY(k))
1778 bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k),
1779 offset, -sectors);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001780 }
1781
Kent Overstreet279afba2013-06-05 06:21:07 -07001782 uint64_t old_offset;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001783 unsigned old_size, sectors_found = 0;
1784
1785 while (1) {
1786 struct bkey *k = bch_btree_iter_next(iter);
1787 if (!k ||
1788 bkey_cmp(&START_KEY(k), insert) >= 0)
1789 break;
1790
1791 if (bkey_cmp(k, &START_KEY(insert)) <= 0)
1792 continue;
1793
Kent Overstreet279afba2013-06-05 06:21:07 -07001794 old_offset = KEY_START(k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001795 old_size = KEY_SIZE(k);
1796
1797 /*
1798 * We might overlap with 0 size extents; we can't skip these
1799 * because if they're in the set we're inserting to we have to
1800 * adjust them so they don't overlap with the key we're
Kent Overstreet1b207d82013-09-10 18:52:54 -07001801 * inserting. But we don't want to check them for replace
Kent Overstreetcafe5632013-03-23 16:11:31 -07001802 * operations.
1803 */
1804
Kent Overstreet1b207d82013-09-10 18:52:54 -07001805 if (replace_key && KEY_SIZE(k)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001806 /*
1807 * k might have been split since we inserted/found the
1808 * key we're replacing
1809 */
1810 unsigned i;
1811 uint64_t offset = KEY_START(k) -
Kent Overstreet1b207d82013-09-10 18:52:54 -07001812 KEY_START(replace_key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001813
1814 /* But it must be a subset of the replace key */
Kent Overstreet1b207d82013-09-10 18:52:54 -07001815 if (KEY_START(k) < KEY_START(replace_key) ||
1816 KEY_OFFSET(k) > KEY_OFFSET(replace_key))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001817 goto check_failed;
1818
1819 /* We didn't find a key that we were supposed to */
1820 if (KEY_START(k) > KEY_START(insert) + sectors_found)
1821 goto check_failed;
1822
Kent Overstreet1b207d82013-09-10 18:52:54 -07001823 if (KEY_PTRS(replace_key) != KEY_PTRS(k))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001824 goto check_failed;
1825
1826 /* skip past gen */
1827 offset <<= 8;
1828
Kent Overstreet1b207d82013-09-10 18:52:54 -07001829 BUG_ON(!KEY_PTRS(replace_key));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001830
Kent Overstreet1b207d82013-09-10 18:52:54 -07001831 for (i = 0; i < KEY_PTRS(replace_key); i++)
1832 if (k->ptr[i] != replace_key->ptr[i] + offset)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001833 goto check_failed;
1834
1835 sectors_found = KEY_OFFSET(k) - KEY_START(insert);
1836 }
1837
1838 if (bkey_cmp(insert, k) < 0 &&
1839 bkey_cmp(&START_KEY(insert), &START_KEY(k)) > 0) {
1840 /*
1841 * We overlapped in the middle of an existing key: that
1842 * means we have to split the old key. But we have to do
1843 * slightly different things depending on whether the
1844 * old key has been written out yet.
1845 */
1846
1847 struct bkey *top;
1848
Kent Overstreet279afba2013-06-05 06:21:07 -07001849 subtract_dirty(k, KEY_START(insert), KEY_SIZE(insert));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001850
1851 if (bkey_written(b, k)) {
1852 /*
1853 * We insert a new key to cover the top of the
1854 * old key, and the old key is modified in place
1855 * to represent the bottom split.
1856 *
1857 * It's completely arbitrary whether the new key
1858 * is the top or the bottom, but it has to match
1859 * up with what btree_sort_fixup() does - it
1860 * doesn't check for this kind of overlap, it
1861 * depends on us inserting a new key for the top
1862 * here.
1863 */
1864 top = bch_bset_search(b, &b->sets[b->nsets],
1865 insert);
1866 shift_keys(b, top, k);
1867 } else {
1868 BKEY_PADDED(key) temp;
1869 bkey_copy(&temp.key, k);
1870 shift_keys(b, k, &temp.key);
1871 top = bkey_next(k);
1872 }
1873
1874 bch_cut_front(insert, top);
1875 bch_cut_back(&START_KEY(insert), k);
1876 bch_bset_fix_invalidated_key(b, k);
1877 return false;
1878 }
1879
1880 if (bkey_cmp(insert, k) < 0) {
1881 bch_cut_front(insert, k);
1882 } else {
Kent Overstreet1fa84552013-11-10 21:55:27 -08001883 if (bkey_cmp(&START_KEY(insert), &START_KEY(k)) > 0)
1884 old_offset = KEY_START(insert);
1885
Kent Overstreetcafe5632013-03-23 16:11:31 -07001886 if (bkey_written(b, k) &&
1887 bkey_cmp(&START_KEY(insert), &START_KEY(k)) <= 0) {
1888 /*
1889 * Completely overwrote, so we don't have to
1890 * invalidate the binary search tree
1891 */
1892 bch_cut_front(k, k);
1893 } else {
1894 __bch_cut_back(&START_KEY(insert), k);
1895 bch_bset_fix_invalidated_key(b, k);
1896 }
1897 }
1898
Kent Overstreet279afba2013-06-05 06:21:07 -07001899 subtract_dirty(k, old_offset, old_size - KEY_SIZE(k));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001900 }
1901
1902check_failed:
Kent Overstreet1b207d82013-09-10 18:52:54 -07001903 if (replace_key) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001904 if (!sectors_found) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001905 return true;
1906 } else if (sectors_found < KEY_SIZE(insert)) {
1907 SET_KEY_OFFSET(insert, KEY_OFFSET(insert) -
1908 (KEY_SIZE(insert) - sectors_found));
1909 SET_KEY_SIZE(insert, sectors_found);
1910 }
1911 }
1912
1913 return false;
1914}
1915
1916static bool btree_insert_key(struct btree *b, struct btree_op *op,
Kent Overstreet1b207d82013-09-10 18:52:54 -07001917 struct bkey *k, struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001918{
1919 struct bset *i = b->sets[b->nsets].data;
1920 struct bkey *m, *prev;
Kent Overstreet85b14922013-05-14 20:33:16 -07001921 unsigned status = BTREE_INSERT_STATUS_INSERT;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001922
1923 BUG_ON(bkey_cmp(k, &b->key) > 0);
1924 BUG_ON(b->level && !KEY_PTRS(k));
1925 BUG_ON(!b->level && !KEY_OFFSET(k));
1926
1927 if (!b->level) {
1928 struct btree_iter iter;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001929
1930 /*
1931 * bset_search() returns the first key that is strictly greater
1932 * than the search key - but for back merging, we want to find
Kent Overstreet0eacac22013-07-01 19:29:05 -07001933 * the previous key.
Kent Overstreetcafe5632013-03-23 16:11:31 -07001934 */
Kent Overstreetcafe5632013-03-23 16:11:31 -07001935 prev = NULL;
Kent Overstreet0eacac22013-07-01 19:29:05 -07001936 m = bch_btree_iter_init(b, &iter, PRECEDING_KEY(&START_KEY(k)));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001937
Kent Overstreet1b207d82013-09-10 18:52:54 -07001938 if (fix_overlapping_extents(b, k, &iter, replace_key)) {
1939 op->insert_collision = true;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001940 return false;
Kent Overstreet1b207d82013-09-10 18:52:54 -07001941 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001942
Kent Overstreet1fa84552013-11-10 21:55:27 -08001943 if (KEY_DIRTY(k))
1944 bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k),
1945 KEY_START(k), KEY_SIZE(k));
1946
Kent Overstreetcafe5632013-03-23 16:11:31 -07001947 while (m != end(i) &&
1948 bkey_cmp(k, &START_KEY(m)) > 0)
1949 prev = m, m = bkey_next(m);
1950
1951 if (key_merging_disabled(b->c))
1952 goto insert;
1953
1954 /* prev is in the tree, if we merge we're done */
Kent Overstreet85b14922013-05-14 20:33:16 -07001955 status = BTREE_INSERT_STATUS_BACK_MERGE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001956 if (prev &&
1957 bch_bkey_try_merge(b, prev, k))
1958 goto merged;
1959
Kent Overstreet85b14922013-05-14 20:33:16 -07001960 status = BTREE_INSERT_STATUS_OVERWROTE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001961 if (m != end(i) &&
1962 KEY_PTRS(m) == KEY_PTRS(k) && !KEY_SIZE(m))
1963 goto copy;
1964
Kent Overstreet85b14922013-05-14 20:33:16 -07001965 status = BTREE_INSERT_STATUS_FRONT_MERGE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001966 if (m != end(i) &&
1967 bch_bkey_try_merge(b, k, m))
1968 goto copy;
Kent Overstreet1b207d82013-09-10 18:52:54 -07001969 } else {
1970 BUG_ON(replace_key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001971 m = bch_bset_search(b, &b->sets[b->nsets], k);
Kent Overstreet1b207d82013-09-10 18:52:54 -07001972 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001973
1974insert: shift_keys(b, m, k);
1975copy: bkey_copy(m, k);
1976merged:
Kent Overstreet1b207d82013-09-10 18:52:54 -07001977 bch_check_keys(b, "%u for %s", status,
1978 replace_key ? "replace" : "insert");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001979
1980 if (b->level && !KEY_OFFSET(k))
Kent Overstreet57943512013-04-25 13:58:35 -07001981 btree_current_write(b)->prio_blocked++;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001982
Kent Overstreet1b207d82013-09-10 18:52:54 -07001983 trace_bcache_btree_insert_key(b, k, replace_key != NULL, status);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001984
1985 return true;
1986}
1987
Kent Overstreet26c949f2013-09-10 18:41:15 -07001988static bool bch_btree_insert_keys(struct btree *b, struct btree_op *op,
Kent Overstreet1b207d82013-09-10 18:52:54 -07001989 struct keylist *insert_keys,
1990 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001991{
1992 bool ret = false;
Kent Overstreet280481d2013-10-24 16:36:03 -07001993 int oldsize = bch_count_data(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001994
Kent Overstreet26c949f2013-09-10 18:41:15 -07001995 while (!bch_keylist_empty(insert_keys)) {
Kent Overstreet403b6cd2013-07-24 17:22:44 -07001996 struct bset *i = write_block(b);
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07001997 struct bkey *k = insert_keys->keys;
Kent Overstreet26c949f2013-09-10 18:41:15 -07001998
Kent Overstreet403b6cd2013-07-24 17:22:44 -07001999 if (b->written + __set_blocks(i, i->keys + bkey_u64s(k), b->c)
2000 > btree_blocks(b))
2001 break;
2002
2003 if (bkey_cmp(k, &b->key) <= 0) {
Kent Overstreet3a3b6a42013-07-24 16:46:42 -07002004 if (!b->level)
2005 bkey_put(b->c, k);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002006
Kent Overstreet1b207d82013-09-10 18:52:54 -07002007 ret |= btree_insert_key(b, op, k, replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002008 bch_keylist_pop_front(insert_keys);
2009 } else if (bkey_cmp(&START_KEY(k), &b->key) < 0) {
Kent Overstreet26c949f2013-09-10 18:41:15 -07002010 BKEY_PADDED(key) temp;
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07002011 bkey_copy(&temp.key, insert_keys->keys);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002012
2013 bch_cut_back(&b->key, &temp.key);
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07002014 bch_cut_front(&b->key, insert_keys->keys);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002015
Kent Overstreet1b207d82013-09-10 18:52:54 -07002016 ret |= btree_insert_key(b, op, &temp.key, replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002017 break;
2018 } else {
2019 break;
2020 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07002021 }
2022
Kent Overstreet403b6cd2013-07-24 17:22:44 -07002023 BUG_ON(!bch_keylist_empty(insert_keys) && b->level);
2024
Kent Overstreetcafe5632013-03-23 16:11:31 -07002025 BUG_ON(bch_count_data(b) < oldsize);
2026 return ret;
2027}
2028
Kent Overstreet26c949f2013-09-10 18:41:15 -07002029static int btree_split(struct btree *b, struct btree_op *op,
2030 struct keylist *insert_keys,
Kent Overstreet1b207d82013-09-10 18:52:54 -07002031 struct keylist *parent_keys,
2032 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002033{
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002034 bool split;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002035 struct btree *n1, *n2 = NULL, *n3 = NULL;
2036 uint64_t start_time = local_clock();
Kent Overstreetb54d6932013-07-24 18:04:18 -07002037 struct closure cl;
2038
2039 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002040
Kent Overstreet35fcd842013-07-24 17:29:09 -07002041 n1 = btree_node_alloc_replacement(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002042 if (IS_ERR(n1))
2043 goto err;
2044
2045 split = set_blocks(n1->sets[0].data, n1->c) > (btree_blocks(b) * 4) / 5;
2046
Kent Overstreetcafe5632013-03-23 16:11:31 -07002047 if (split) {
2048 unsigned keys = 0;
2049
Kent Overstreetc37511b2013-04-26 15:39:55 -07002050 trace_bcache_btree_node_split(b, n1->sets[0].data->keys);
2051
Kent Overstreet35fcd842013-07-24 17:29:09 -07002052 n2 = bch_btree_node_alloc(b->c, b->level);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002053 if (IS_ERR(n2))
2054 goto err_free1;
2055
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002056 if (!b->parent) {
Kent Overstreet35fcd842013-07-24 17:29:09 -07002057 n3 = bch_btree_node_alloc(b->c, b->level + 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002058 if (IS_ERR(n3))
2059 goto err_free2;
2060 }
2061
Kent Overstreet1b207d82013-09-10 18:52:54 -07002062 bch_btree_insert_keys(n1, op, insert_keys, replace_key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002063
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002064 /*
2065 * Has to be a linear search because we don't have an auxiliary
Kent Overstreetcafe5632013-03-23 16:11:31 -07002066 * search tree yet
2067 */
2068
2069 while (keys < (n1->sets[0].data->keys * 3) / 5)
2070 keys += bkey_u64s(node(n1->sets[0].data, keys));
2071
2072 bkey_copy_key(&n1->key, node(n1->sets[0].data, keys));
2073 keys += bkey_u64s(node(n1->sets[0].data, keys));
2074
2075 n2->sets[0].data->keys = n1->sets[0].data->keys - keys;
2076 n1->sets[0].data->keys = keys;
2077
2078 memcpy(n2->sets[0].data->start,
2079 end(n1->sets[0].data),
2080 n2->sets[0].data->keys * sizeof(uint64_t));
2081
2082 bkey_copy_key(&n2->key, &b->key);
2083
Kent Overstreet26c949f2013-09-10 18:41:15 -07002084 bch_keylist_add(parent_keys, &n2->key);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002085 bch_btree_node_write(n2, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002086 rw_unlock(true, n2);
Kent Overstreetc37511b2013-04-26 15:39:55 -07002087 } else {
2088 trace_bcache_btree_node_compact(b, n1->sets[0].data->keys);
2089
Kent Overstreet1b207d82013-09-10 18:52:54 -07002090 bch_btree_insert_keys(n1, op, insert_keys, replace_key);
Kent Overstreetc37511b2013-04-26 15:39:55 -07002091 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07002092
Kent Overstreet26c949f2013-09-10 18:41:15 -07002093 bch_keylist_add(parent_keys, &n1->key);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002094 bch_btree_node_write(n1, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002095
2096 if (n3) {
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002097 /* Depth increases, make a new root */
2098
Kent Overstreetcafe5632013-03-23 16:11:31 -07002099 bkey_copy_key(&n3->key, &MAX_KEY);
Kent Overstreet1b207d82013-09-10 18:52:54 -07002100 bch_btree_insert_keys(n3, op, parent_keys, NULL);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002101 bch_btree_node_write(n3, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002102
Kent Overstreetb54d6932013-07-24 18:04:18 -07002103 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002104 bch_btree_set_root(n3);
2105 rw_unlock(true, n3);
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002106 } else if (!b->parent) {
2107 /* Root filled up but didn't need to be split */
2108
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07002109 bch_keylist_reset(parent_keys);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002110 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002111 bch_btree_set_root(n1);
2112 } else {
Kent Overstreetb54d6932013-07-24 18:04:18 -07002113 closure_sync(&cl);
Kent Overstreet8835c122013-07-24 23:18:05 -07002114 make_btree_freeing_key(b, parent_keys->top);
2115 bch_keylist_push(parent_keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002116 }
2117
2118 rw_unlock(true, n1);
Kent Overstreete8e1d462013-07-24 17:27:07 -07002119 btree_node_free(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002120
Kent Overstreet169ef1c2013-03-28 12:50:55 -06002121 bch_time_stats_update(&b->c->btree_split_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002122
2123 return 0;
2124err_free2:
Kent Overstreete8e1d462013-07-24 17:27:07 -07002125 btree_node_free(n2);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002126 rw_unlock(true, n2);
2127err_free1:
Kent Overstreete8e1d462013-07-24 17:27:07 -07002128 btree_node_free(n1);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002129 rw_unlock(true, n1);
2130err:
2131 if (n3 == ERR_PTR(-EAGAIN) ||
2132 n2 == ERR_PTR(-EAGAIN) ||
2133 n1 == ERR_PTR(-EAGAIN))
2134 return -EAGAIN;
2135
2136 pr_warn("couldn't split");
2137 return -ENOMEM;
2138}
2139
Kent Overstreet26c949f2013-09-10 18:41:15 -07002140static int bch_btree_insert_node(struct btree *b, struct btree_op *op,
Kent Overstreetc18536a2013-07-24 17:44:17 -07002141 struct keylist *insert_keys,
Kent Overstreet1b207d82013-09-10 18:52:54 -07002142 atomic_t *journal_ref,
2143 struct bkey *replace_key)
Kent Overstreet26c949f2013-09-10 18:41:15 -07002144{
2145 int ret = 0;
2146 struct keylist split_keys;
2147
2148 bch_keylist_init(&split_keys);
2149
Kent Overstreet26c949f2013-09-10 18:41:15 -07002150 do {
Kent Overstreet1b207d82013-09-10 18:52:54 -07002151 BUG_ON(b->level && replace_key);
2152
Kent Overstreet26c949f2013-09-10 18:41:15 -07002153 if (should_split(b)) {
2154 if (current->bio_list) {
2155 op->lock = b->c->root->level + 1;
2156 ret = -EAGAIN;
2157 } else if (op->lock <= b->c->root->level) {
2158 op->lock = b->c->root->level + 1;
2159 ret = -EINTR;
2160 } else {
2161 struct btree *parent = b->parent;
2162
2163 ret = btree_split(b, op, insert_keys,
Kent Overstreet1b207d82013-09-10 18:52:54 -07002164 &split_keys, replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002165 insert_keys = &split_keys;
Kent Overstreet1b207d82013-09-10 18:52:54 -07002166 replace_key = NULL;
Kent Overstreet26c949f2013-09-10 18:41:15 -07002167 b = parent;
Kent Overstreet403b6cd2013-07-24 17:22:44 -07002168 if (!ret)
2169 ret = -EINTR;
Kent Overstreet26c949f2013-09-10 18:41:15 -07002170 }
2171 } else {
2172 BUG_ON(write_block(b) != b->sets[b->nsets].data);
2173
Kent Overstreet1b207d82013-09-10 18:52:54 -07002174 if (bch_btree_insert_keys(b, op, insert_keys,
2175 replace_key)) {
Kent Overstreetf269af52013-07-23 20:48:29 -07002176 if (!b->level)
Kent Overstreetc18536a2013-07-24 17:44:17 -07002177 bch_btree_leaf_dirty(b, journal_ref);
Kent Overstreetf269af52013-07-23 20:48:29 -07002178 else
2179 bch_btree_node_write_sync(b);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002180 }
2181 }
2182 } while (!bch_keylist_empty(&split_keys));
2183
2184 return ret;
2185}
2186
Kent Overstreete7c590e2013-09-10 18:39:16 -07002187int bch_btree_insert_check_key(struct btree *b, struct btree_op *op,
2188 struct bkey *check_key)
2189{
2190 int ret = -EINTR;
2191 uint64_t btree_ptr = b->key.ptr[0];
2192 unsigned long seq = b->seq;
2193 struct keylist insert;
2194 bool upgrade = op->lock == -1;
2195
2196 bch_keylist_init(&insert);
2197
2198 if (upgrade) {
2199 rw_unlock(false, b);
2200 rw_lock(true, b, b->level);
2201
2202 if (b->key.ptr[0] != btree_ptr ||
2203 b->seq != seq + 1)
2204 goto out;
2205 }
2206
2207 SET_KEY_PTRS(check_key, 1);
2208 get_random_bytes(&check_key->ptr[0], sizeof(uint64_t));
2209
2210 SET_PTR_DEV(check_key, 0, PTR_CHECK_DEV);
2211
2212 bch_keylist_add(&insert, check_key);
2213
Kent Overstreet1b207d82013-09-10 18:52:54 -07002214 ret = bch_btree_insert_node(b, op, &insert, NULL, NULL);
Kent Overstreete7c590e2013-09-10 18:39:16 -07002215
2216 BUG_ON(!ret && !bch_keylist_empty(&insert));
2217out:
2218 if (upgrade)
2219 downgrade_write(&b->lock);
2220 return ret;
2221}
2222
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002223struct btree_insert_op {
2224 struct btree_op op;
2225 struct keylist *keys;
2226 atomic_t *journal_ref;
2227 struct bkey *replace_key;
2228};
2229
2230int btree_insert_fn(struct btree_op *b_op, struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002231{
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002232 struct btree_insert_op *op = container_of(b_op,
2233 struct btree_insert_op, op);
Kent Overstreet403b6cd2013-07-24 17:22:44 -07002234
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002235 int ret = bch_btree_insert_node(b, &op->op, op->keys,
2236 op->journal_ref, op->replace_key);
2237 if (ret && !bch_keylist_empty(op->keys))
2238 return ret;
2239 else
2240 return MAP_DONE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002241}
2242
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002243int bch_btree_insert(struct cache_set *c, struct keylist *keys,
2244 atomic_t *journal_ref, struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002245{
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002246 struct btree_insert_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002247 int ret = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002248
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002249 BUG_ON(current->bio_list);
Kent Overstreet4f3d4012013-09-10 18:46:36 -07002250 BUG_ON(bch_keylist_empty(keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002251
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002252 bch_btree_op_init(&op.op, 0);
2253 op.keys = keys;
2254 op.journal_ref = journal_ref;
2255 op.replace_key = replace_key;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002256
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002257 while (!ret && !bch_keylist_empty(keys)) {
2258 op.op.lock = 0;
2259 ret = bch_btree_map_leaf_nodes(&op.op, c,
2260 &START_KEY(keys->keys),
2261 btree_insert_fn);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002262 }
2263
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002264 if (ret) {
2265 struct bkey *k;
2266
2267 pr_err("error %i", ret);
2268
2269 while ((k = bch_keylist_pop(keys)))
Kent Overstreet3a3b6a42013-07-24 16:46:42 -07002270 bkey_put(c, k);
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002271 } else if (op.op.insert_collision)
2272 ret = -ESRCH;
Kent Overstreet6054c6d2013-07-24 18:06:22 -07002273
Kent Overstreetcafe5632013-03-23 16:11:31 -07002274 return ret;
2275}
2276
2277void bch_btree_set_root(struct btree *b)
2278{
2279 unsigned i;
Kent Overstreete49c7c32013-06-26 17:25:38 -07002280 struct closure cl;
2281
2282 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002283
Kent Overstreetc37511b2013-04-26 15:39:55 -07002284 trace_bcache_btree_set_root(b);
2285
Kent Overstreetcafe5632013-03-23 16:11:31 -07002286 BUG_ON(!b->written);
2287
2288 for (i = 0; i < KEY_PTRS(&b->key); i++)
2289 BUG_ON(PTR_BUCKET(b->c, &b->key, i)->prio != BTREE_PRIO);
2290
2291 mutex_lock(&b->c->bucket_lock);
2292 list_del_init(&b->list);
2293 mutex_unlock(&b->c->bucket_lock);
2294
2295 b->c->root = b;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002296
Kent Overstreete49c7c32013-06-26 17:25:38 -07002297 bch_journal_meta(b->c, &cl);
2298 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002299}
2300
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002301/* Map across nodes or keys */
2302
2303static int bch_btree_map_nodes_recurse(struct btree *b, struct btree_op *op,
2304 struct bkey *from,
2305 btree_map_nodes_fn *fn, int flags)
2306{
2307 int ret = MAP_CONTINUE;
2308
2309 if (b->level) {
2310 struct bkey *k;
2311 struct btree_iter iter;
2312
2313 bch_btree_iter_init(b, &iter, from);
2314
2315 while ((k = bch_btree_iter_next_filter(&iter, b,
2316 bch_ptr_bad))) {
2317 ret = btree(map_nodes_recurse, k, b,
2318 op, from, fn, flags);
2319 from = NULL;
2320
2321 if (ret != MAP_CONTINUE)
2322 return ret;
2323 }
2324 }
2325
2326 if (!b->level || flags == MAP_ALL_NODES)
2327 ret = fn(op, b);
2328
2329 return ret;
2330}
2331
2332int __bch_btree_map_nodes(struct btree_op *op, struct cache_set *c,
2333 struct bkey *from, btree_map_nodes_fn *fn, int flags)
2334{
Kent Overstreetb54d6932013-07-24 18:04:18 -07002335 return btree_root(map_nodes_recurse, c, op, from, fn, flags);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002336}
2337
2338static int bch_btree_map_keys_recurse(struct btree *b, struct btree_op *op,
2339 struct bkey *from, btree_map_keys_fn *fn,
2340 int flags)
2341{
2342 int ret = MAP_CONTINUE;
2343 struct bkey *k;
2344 struct btree_iter iter;
2345
2346 bch_btree_iter_init(b, &iter, from);
2347
2348 while ((k = bch_btree_iter_next_filter(&iter, b, bch_ptr_bad))) {
2349 ret = !b->level
2350 ? fn(op, b, k)
2351 : btree(map_keys_recurse, k, b, op, from, fn, flags);
2352 from = NULL;
2353
2354 if (ret != MAP_CONTINUE)
2355 return ret;
2356 }
2357
2358 if (!b->level && (flags & MAP_END_KEY))
2359 ret = fn(op, b, &KEY(KEY_INODE(&b->key),
2360 KEY_OFFSET(&b->key), 0));
2361
2362 return ret;
2363}
2364
2365int bch_btree_map_keys(struct btree_op *op, struct cache_set *c,
2366 struct bkey *from, btree_map_keys_fn *fn, int flags)
2367{
Kent Overstreetb54d6932013-07-24 18:04:18 -07002368 return btree_root(map_keys_recurse, c, op, from, fn, flags);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002369}
2370
Kent Overstreetcafe5632013-03-23 16:11:31 -07002371/* Keybuf code */
2372
2373static inline int keybuf_cmp(struct keybuf_key *l, struct keybuf_key *r)
2374{
2375 /* Overlapping keys compare equal */
2376 if (bkey_cmp(&l->key, &START_KEY(&r->key)) <= 0)
2377 return -1;
2378 if (bkey_cmp(&START_KEY(&l->key), &r->key) >= 0)
2379 return 1;
2380 return 0;
2381}
2382
2383static inline int keybuf_nonoverlapping_cmp(struct keybuf_key *l,
2384 struct keybuf_key *r)
2385{
2386 return clamp_t(int64_t, bkey_cmp(&l->key, &r->key), -1, 1);
2387}
2388
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002389struct refill {
2390 struct btree_op op;
2391 struct keybuf *buf;
2392 struct bkey *end;
2393 keybuf_pred_fn *pred;
2394};
2395
2396static int refill_keybuf_fn(struct btree_op *op, struct btree *b,
2397 struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002398{
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002399 struct refill *refill = container_of(op, struct refill, op);
2400 struct keybuf *buf = refill->buf;
2401 int ret = MAP_CONTINUE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002402
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002403 if (bkey_cmp(k, refill->end) >= 0) {
2404 ret = MAP_DONE;
2405 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002406 }
2407
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002408 if (!KEY_SIZE(k)) /* end key */
2409 goto out;
2410
2411 if (refill->pred(buf, k)) {
2412 struct keybuf_key *w;
2413
2414 spin_lock(&buf->lock);
2415
2416 w = array_alloc(&buf->freelist);
2417 if (!w) {
2418 spin_unlock(&buf->lock);
2419 return MAP_DONE;
2420 }
2421
2422 w->private = NULL;
2423 bkey_copy(&w->key, k);
2424
2425 if (RB_INSERT(&buf->keys, w, node, keybuf_cmp))
2426 array_free(&buf->freelist, w);
2427
2428 if (array_freelist_empty(&buf->freelist))
2429 ret = MAP_DONE;
2430
2431 spin_unlock(&buf->lock);
2432 }
2433out:
2434 buf->last_scanned = *k;
2435 return ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002436}
2437
2438void bch_refill_keybuf(struct cache_set *c, struct keybuf *buf,
Kent Overstreet72c27062013-06-05 06:24:39 -07002439 struct bkey *end, keybuf_pred_fn *pred)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002440{
2441 struct bkey start = buf->last_scanned;
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002442 struct refill refill;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002443
2444 cond_resched();
2445
Kent Overstreetb54d6932013-07-24 18:04:18 -07002446 bch_btree_op_init(&refill.op, -1);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002447 refill.buf = buf;
2448 refill.end = end;
2449 refill.pred = pred;
2450
2451 bch_btree_map_keys(&refill.op, c, &buf->last_scanned,
2452 refill_keybuf_fn, MAP_END_KEY);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002453
2454 pr_debug("found %s keys from %llu:%llu to %llu:%llu",
2455 RB_EMPTY_ROOT(&buf->keys) ? "no" :
2456 array_freelist_empty(&buf->freelist) ? "some" : "a few",
2457 KEY_INODE(&start), KEY_OFFSET(&start),
2458 KEY_INODE(&buf->last_scanned), KEY_OFFSET(&buf->last_scanned));
2459
2460 spin_lock(&buf->lock);
2461
2462 if (!RB_EMPTY_ROOT(&buf->keys)) {
2463 struct keybuf_key *w;
2464 w = RB_FIRST(&buf->keys, struct keybuf_key, node);
2465 buf->start = START_KEY(&w->key);
2466
2467 w = RB_LAST(&buf->keys, struct keybuf_key, node);
2468 buf->end = w->key;
2469 } else {
2470 buf->start = MAX_KEY;
2471 buf->end = MAX_KEY;
2472 }
2473
2474 spin_unlock(&buf->lock);
2475}
2476
2477static void __bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w)
2478{
2479 rb_erase(&w->node, &buf->keys);
2480 array_free(&buf->freelist, w);
2481}
2482
2483void bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w)
2484{
2485 spin_lock(&buf->lock);
2486 __bch_keybuf_del(buf, w);
2487 spin_unlock(&buf->lock);
2488}
2489
2490bool bch_keybuf_check_overlapping(struct keybuf *buf, struct bkey *start,
2491 struct bkey *end)
2492{
2493 bool ret = false;
2494 struct keybuf_key *p, *w, s;
2495 s.key = *start;
2496
2497 if (bkey_cmp(end, &buf->start) <= 0 ||
2498 bkey_cmp(start, &buf->end) >= 0)
2499 return false;
2500
2501 spin_lock(&buf->lock);
2502 w = RB_GREATER(&buf->keys, s, node, keybuf_nonoverlapping_cmp);
2503
2504 while (w && bkey_cmp(&START_KEY(&w->key), end) < 0) {
2505 p = w;
2506 w = RB_NEXT(w, node);
2507
2508 if (p->private)
2509 ret = true;
2510 else
2511 __bch_keybuf_del(buf, p);
2512 }
2513
2514 spin_unlock(&buf->lock);
2515 return ret;
2516}
2517
2518struct keybuf_key *bch_keybuf_next(struct keybuf *buf)
2519{
2520 struct keybuf_key *w;
2521 spin_lock(&buf->lock);
2522
2523 w = RB_FIRST(&buf->keys, struct keybuf_key, node);
2524
2525 while (w && w->private)
2526 w = RB_NEXT(w, node);
2527
2528 if (w)
2529 w->private = ERR_PTR(-EINTR);
2530
2531 spin_unlock(&buf->lock);
2532 return w;
2533}
2534
2535struct keybuf_key *bch_keybuf_next_rescan(struct cache_set *c,
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002536 struct keybuf *buf,
2537 struct bkey *end,
2538 keybuf_pred_fn *pred)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002539{
2540 struct keybuf_key *ret;
2541
2542 while (1) {
2543 ret = bch_keybuf_next(buf);
2544 if (ret)
2545 break;
2546
2547 if (bkey_cmp(&buf->last_scanned, end) >= 0) {
2548 pr_debug("scan finished");
2549 break;
2550 }
2551
Kent Overstreet72c27062013-06-05 06:24:39 -07002552 bch_refill_keybuf(c, buf, end, pred);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002553 }
2554
2555 return ret;
2556}
2557
Kent Overstreet72c27062013-06-05 06:24:39 -07002558void bch_keybuf_init(struct keybuf *buf)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002559{
Kent Overstreetcafe5632013-03-23 16:11:31 -07002560 buf->last_scanned = MAX_KEY;
2561 buf->keys = RB_ROOT;
2562
2563 spin_lock_init(&buf->lock);
2564 array_allocator_init(&buf->freelist);
2565}
2566
2567void bch_btree_exit(void)
2568{
2569 if (btree_io_wq)
2570 destroy_workqueue(btree_io_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002571}
2572
2573int __init bch_btree_init(void)
2574{
Kent Overstreet72a44512013-10-24 17:19:26 -07002575 btree_io_wq = create_singlethread_workqueue("bch_btree_io");
2576 if (!btree_io_wq)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002577 return -ENOMEM;
2578
2579 return 0;
2580}