blob: 773b0e929ff498ef5064933dc872594174befa61 [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 Overstreet57943512013-04-25 13:58:35 -0700477static void btree_node_write_work(struct work_struct *w)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700478{
479 struct btree *b = container_of(to_delayed_work(w), struct btree, work);
480
Kent Overstreet57943512013-04-25 13:58:35 -0700481 rw_lock(true, b, b->level);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700482
483 if (btree_node_dirty(b))
Kent Overstreet57943512013-04-25 13:58:35 -0700484 bch_btree_node_write(b, NULL);
485 rw_unlock(true, b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700486}
487
Kent Overstreetc18536a2013-07-24 17:44:17 -0700488static void bch_btree_leaf_dirty(struct btree *b, atomic_t *journal_ref)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700489{
490 struct bset *i = b->sets[b->nsets].data;
491 struct btree_write *w = btree_current_write(b);
492
Kent Overstreet57943512013-04-25 13:58:35 -0700493 BUG_ON(!b->written);
494 BUG_ON(!i->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700495
Kent Overstreet57943512013-04-25 13:58:35 -0700496 if (!btree_node_dirty(b))
497 queue_delayed_work(btree_io_wq, &b->work, 30 * HZ);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700498
Kent Overstreet57943512013-04-25 13:58:35 -0700499 set_btree_node_dirty(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700500
Kent Overstreetc18536a2013-07-24 17:44:17 -0700501 if (journal_ref) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700502 if (w->journal &&
Kent Overstreetc18536a2013-07-24 17:44:17 -0700503 journal_pin_cmp(b->c, w->journal, journal_ref)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700504 atomic_dec_bug(w->journal);
505 w->journal = NULL;
506 }
507
508 if (!w->journal) {
Kent Overstreetc18536a2013-07-24 17:44:17 -0700509 w->journal = journal_ref;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700510 atomic_inc(w->journal);
511 }
512 }
513
Kent Overstreetcafe5632013-03-23 16:11:31 -0700514 /* Force write if set is too big */
Kent Overstreet57943512013-04-25 13:58:35 -0700515 if (set_bytes(i) > PAGE_SIZE - 48 &&
516 !current->bio_list)
517 bch_btree_node_write(b, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700518}
519
520/*
521 * Btree in memory cache - allocation/freeing
522 * mca -> memory cache
523 */
524
525static void mca_reinit(struct btree *b)
526{
527 unsigned i;
528
529 b->flags = 0;
530 b->written = 0;
531 b->nsets = 0;
532
533 for (i = 0; i < MAX_BSETS; i++)
534 b->sets[i].size = 0;
535 /*
536 * Second loop starts at 1 because b->sets[0]->data is the memory we
537 * allocated
538 */
539 for (i = 1; i < MAX_BSETS; i++)
540 b->sets[i].data = NULL;
541}
542
543#define mca_reserve(c) (((c->root && c->root->level) \
544 ? c->root->level : 1) * 8 + 16)
545#define mca_can_free(c) \
546 max_t(int, 0, c->bucket_cache_used - mca_reserve(c))
547
548static void mca_data_free(struct btree *b)
549{
550 struct bset_tree *t = b->sets;
551 BUG_ON(!closure_is_unlocked(&b->io.cl));
552
553 if (bset_prev_bytes(b) < PAGE_SIZE)
554 kfree(t->prev);
555 else
556 free_pages((unsigned long) t->prev,
557 get_order(bset_prev_bytes(b)));
558
559 if (bset_tree_bytes(b) < PAGE_SIZE)
560 kfree(t->tree);
561 else
562 free_pages((unsigned long) t->tree,
563 get_order(bset_tree_bytes(b)));
564
565 free_pages((unsigned long) t->data, b->page_order);
566
567 t->prev = NULL;
568 t->tree = NULL;
569 t->data = NULL;
570 list_move(&b->list, &b->c->btree_cache_freed);
571 b->c->bucket_cache_used--;
572}
573
574static void mca_bucket_free(struct btree *b)
575{
576 BUG_ON(btree_node_dirty(b));
577
578 b->key.ptr[0] = 0;
579 hlist_del_init_rcu(&b->hash);
580 list_move(&b->list, &b->c->btree_cache_freeable);
581}
582
583static unsigned btree_order(struct bkey *k)
584{
585 return ilog2(KEY_SIZE(k) / PAGE_SECTORS ?: 1);
586}
587
588static void mca_data_alloc(struct btree *b, struct bkey *k, gfp_t gfp)
589{
590 struct bset_tree *t = b->sets;
591 BUG_ON(t->data);
592
593 b->page_order = max_t(unsigned,
594 ilog2(b->c->btree_pages),
595 btree_order(k));
596
597 t->data = (void *) __get_free_pages(gfp, b->page_order);
598 if (!t->data)
599 goto err;
600
601 t->tree = bset_tree_bytes(b) < PAGE_SIZE
602 ? kmalloc(bset_tree_bytes(b), gfp)
603 : (void *) __get_free_pages(gfp, get_order(bset_tree_bytes(b)));
604 if (!t->tree)
605 goto err;
606
607 t->prev = bset_prev_bytes(b) < PAGE_SIZE
608 ? kmalloc(bset_prev_bytes(b), gfp)
609 : (void *) __get_free_pages(gfp, get_order(bset_prev_bytes(b)));
610 if (!t->prev)
611 goto err;
612
613 list_move(&b->list, &b->c->btree_cache);
614 b->c->bucket_cache_used++;
615 return;
616err:
617 mca_data_free(b);
618}
619
620static struct btree *mca_bucket_alloc(struct cache_set *c,
621 struct bkey *k, gfp_t gfp)
622{
623 struct btree *b = kzalloc(sizeof(struct btree), gfp);
624 if (!b)
625 return NULL;
626
627 init_rwsem(&b->lock);
628 lockdep_set_novalidate_class(&b->lock);
629 INIT_LIST_HEAD(&b->list);
Kent Overstreet57943512013-04-25 13:58:35 -0700630 INIT_DELAYED_WORK(&b->work, btree_node_write_work);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700631 b->c = c;
632 closure_init_unlocked(&b->io);
633
634 mca_data_alloc(b, k, gfp);
635 return b;
636}
637
Kent Overstreete8e1d462013-07-24 17:27:07 -0700638static int mca_reap(struct btree *b, unsigned min_order, bool flush)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700639{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700640 struct closure cl;
641
642 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700643 lockdep_assert_held(&b->c->bucket_lock);
644
645 if (!down_write_trylock(&b->lock))
646 return -ENOMEM;
647
Kent Overstreete8e1d462013-07-24 17:27:07 -0700648 BUG_ON(btree_node_dirty(b) && !b->sets[0].data);
649
650 if (b->page_order < min_order ||
651 (!flush &&
652 (btree_node_dirty(b) ||
653 atomic_read(&b->io.cl.remaining) != -1))) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700654 rw_unlock(true, b);
655 return -ENOMEM;
656 }
657
Kent Overstreete8e1d462013-07-24 17:27:07 -0700658 if (btree_node_dirty(b)) {
659 bch_btree_node_write(b, &cl);
660 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700661 }
662
Kent Overstreete8e1d462013-07-24 17:27:07 -0700663 /* wait for any in flight btree write */
Kent Overstreetfaadf0c2013-11-01 18:03:08 -0700664 closure_wait_event(&b->io.wait, &cl,
665 atomic_read(&b->io.cl.remaining) == -1);
Kent Overstreete8e1d462013-07-24 17:27:07 -0700666
Kent Overstreetcafe5632013-03-23 16:11:31 -0700667 return 0;
668}
669
Dave Chinner7dc19d52013-08-28 10:18:11 +1000670static unsigned long bch_mca_scan(struct shrinker *shrink,
671 struct shrink_control *sc)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700672{
673 struct cache_set *c = container_of(shrink, struct cache_set, shrink);
674 struct btree *b, *t;
675 unsigned long i, nr = sc->nr_to_scan;
Dave Chinner7dc19d52013-08-28 10:18:11 +1000676 unsigned long freed = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700677
678 if (c->shrinker_disabled)
Dave Chinner7dc19d52013-08-28 10:18:11 +1000679 return SHRINK_STOP;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700680
681 if (c->try_harder)
Dave Chinner7dc19d52013-08-28 10:18:11 +1000682 return SHRINK_STOP;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700683
684 /* Return -1 if we can't do anything right now */
Kent Overstreeta698e082013-09-23 23:17:34 -0700685 if (sc->gfp_mask & __GFP_IO)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700686 mutex_lock(&c->bucket_lock);
687 else if (!mutex_trylock(&c->bucket_lock))
688 return -1;
689
Kent Overstreet36c9ea92013-06-03 13:04:56 -0700690 /*
691 * It's _really_ critical that we don't free too many btree nodes - we
692 * have to always leave ourselves a reserve. The reserve is how we
693 * guarantee that allocating memory for a new btree node can always
694 * succeed, so that inserting keys into the btree can always succeed and
695 * IO can always make forward progress:
696 */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700697 nr /= c->btree_pages;
698 nr = min_t(unsigned long, nr, mca_can_free(c));
699
700 i = 0;
701 list_for_each_entry_safe(b, t, &c->btree_cache_freeable, list) {
Dave Chinner7dc19d52013-08-28 10:18:11 +1000702 if (freed >= nr)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700703 break;
704
705 if (++i > 3 &&
Kent Overstreete8e1d462013-07-24 17:27:07 -0700706 !mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700707 mca_data_free(b);
708 rw_unlock(true, b);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000709 freed++;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700710 }
711 }
712
713 /*
714 * Can happen right when we first start up, before we've read in any
715 * btree nodes
716 */
717 if (list_empty(&c->btree_cache))
718 goto out;
719
Dave Chinner7dc19d52013-08-28 10:18:11 +1000720 for (i = 0; (nr--) && i < c->bucket_cache_used; i++) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700721 b = list_first_entry(&c->btree_cache, struct btree, list);
722 list_rotate_left(&c->btree_cache);
723
724 if (!b->accessed &&
Kent Overstreete8e1d462013-07-24 17:27:07 -0700725 !mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700726 mca_bucket_free(b);
727 mca_data_free(b);
728 rw_unlock(true, b);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000729 freed++;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700730 } else
731 b->accessed = 0;
732 }
733out:
Kent Overstreetcafe5632013-03-23 16:11:31 -0700734 mutex_unlock(&c->bucket_lock);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000735 return freed;
736}
737
738static unsigned long bch_mca_count(struct shrinker *shrink,
739 struct shrink_control *sc)
740{
741 struct cache_set *c = container_of(shrink, struct cache_set, shrink);
742
743 if (c->shrinker_disabled)
744 return 0;
745
746 if (c->try_harder)
747 return 0;
748
749 return mca_can_free(c) * c->btree_pages;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700750}
751
752void bch_btree_cache_free(struct cache_set *c)
753{
754 struct btree *b;
755 struct closure cl;
756 closure_init_stack(&cl);
757
758 if (c->shrink.list.next)
759 unregister_shrinker(&c->shrink);
760
761 mutex_lock(&c->bucket_lock);
762
763#ifdef CONFIG_BCACHE_DEBUG
764 if (c->verify_data)
765 list_move(&c->verify_data->list, &c->btree_cache);
766#endif
767
768 list_splice(&c->btree_cache_freeable,
769 &c->btree_cache);
770
771 while (!list_empty(&c->btree_cache)) {
772 b = list_first_entry(&c->btree_cache, struct btree, list);
773
774 if (btree_node_dirty(b))
775 btree_complete_write(b, btree_current_write(b));
776 clear_bit(BTREE_NODE_dirty, &b->flags);
777
778 mca_data_free(b);
779 }
780
781 while (!list_empty(&c->btree_cache_freed)) {
782 b = list_first_entry(&c->btree_cache_freed,
783 struct btree, list);
784 list_del(&b->list);
785 cancel_delayed_work_sync(&b->work);
786 kfree(b);
787 }
788
789 mutex_unlock(&c->bucket_lock);
790}
791
792int bch_btree_cache_alloc(struct cache_set *c)
793{
794 unsigned i;
795
Kent Overstreetcafe5632013-03-23 16:11:31 -0700796 for (i = 0; i < mca_reserve(c); i++)
Kent Overstreet72a44512013-10-24 17:19:26 -0700797 if (!mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL))
798 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700799
800 list_splice_init(&c->btree_cache,
801 &c->btree_cache_freeable);
802
803#ifdef CONFIG_BCACHE_DEBUG
804 mutex_init(&c->verify_lock);
805
806 c->verify_data = mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL);
807
808 if (c->verify_data &&
809 c->verify_data->sets[0].data)
810 list_del_init(&c->verify_data->list);
811 else
812 c->verify_data = NULL;
813#endif
814
Dave Chinner7dc19d52013-08-28 10:18:11 +1000815 c->shrink.count_objects = bch_mca_count;
816 c->shrink.scan_objects = bch_mca_scan;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700817 c->shrink.seeks = 4;
818 c->shrink.batch = c->btree_pages * 2;
819 register_shrinker(&c->shrink);
820
821 return 0;
822}
823
824/* Btree in memory cache - hash table */
825
826static struct hlist_head *mca_hash(struct cache_set *c, struct bkey *k)
827{
828 return &c->bucket_hash[hash_32(PTR_HASH(c, k), BUCKET_HASH_BITS)];
829}
830
831static struct btree *mca_find(struct cache_set *c, struct bkey *k)
832{
833 struct btree *b;
834
835 rcu_read_lock();
836 hlist_for_each_entry_rcu(b, mca_hash(c, k), hash)
837 if (PTR_HASH(c, &b->key) == PTR_HASH(c, k))
838 goto out;
839 b = NULL;
840out:
841 rcu_read_unlock();
842 return b;
843}
844
Kent Overstreete8e1d462013-07-24 17:27:07 -0700845static struct btree *mca_cannibalize(struct cache_set *c, struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700846{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700847 struct btree *b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700848
Kent Overstreetc37511b2013-04-26 15:39:55 -0700849 trace_bcache_btree_cache_cannibalize(c);
850
Kent Overstreete8e1d462013-07-24 17:27:07 -0700851 if (!c->try_harder) {
852 c->try_harder = current;
853 c->try_harder_start = local_clock();
854 } else if (c->try_harder != current)
855 return ERR_PTR(-ENOSPC);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700856
Kent Overstreete8e1d462013-07-24 17:27:07 -0700857 list_for_each_entry_reverse(b, &c->btree_cache, list)
858 if (!mca_reap(b, btree_order(k), false))
859 return b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700860
Kent Overstreete8e1d462013-07-24 17:27:07 -0700861 list_for_each_entry_reverse(b, &c->btree_cache, list)
862 if (!mca_reap(b, btree_order(k), true))
863 return b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700864
Kent Overstreete8e1d462013-07-24 17:27:07 -0700865 return ERR_PTR(-ENOMEM);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700866}
867
868/*
869 * We can only have one thread cannibalizing other cached btree nodes at a time,
870 * or we'll deadlock. We use an open coded mutex to ensure that, which a
871 * cannibalize_bucket() will take. This means every time we unlock the root of
872 * the btree, we need to release this lock if we have it held.
873 */
Kent Overstreetdf8e8972013-07-24 17:37:59 -0700874static void bch_cannibalize_unlock(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700875{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700876 if (c->try_harder == current) {
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600877 bch_time_stats_update(&c->try_harder_time, c->try_harder_start);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700878 c->try_harder = NULL;
Kent Overstreete8e1d462013-07-24 17:27:07 -0700879 wake_up(&c->try_wait);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700880 }
881}
882
Kent Overstreete8e1d462013-07-24 17:27:07 -0700883static struct btree *mca_alloc(struct cache_set *c, struct bkey *k, int level)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700884{
885 struct btree *b;
886
Kent Overstreete8e1d462013-07-24 17:27:07 -0700887 BUG_ON(current->bio_list);
888
Kent Overstreetcafe5632013-03-23 16:11:31 -0700889 lockdep_assert_held(&c->bucket_lock);
890
891 if (mca_find(c, k))
892 return NULL;
893
894 /* btree_free() doesn't free memory; it sticks the node on the end of
895 * the list. Check if there's any freed nodes there:
896 */
897 list_for_each_entry(b, &c->btree_cache_freeable, list)
Kent Overstreete8e1d462013-07-24 17:27:07 -0700898 if (!mca_reap(b, btree_order(k), false))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700899 goto out;
900
901 /* We never free struct btree itself, just the memory that holds the on
902 * disk node. Check the freed list before allocating a new one:
903 */
904 list_for_each_entry(b, &c->btree_cache_freed, list)
Kent Overstreete8e1d462013-07-24 17:27:07 -0700905 if (!mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700906 mca_data_alloc(b, k, __GFP_NOWARN|GFP_NOIO);
907 if (!b->sets[0].data)
908 goto err;
909 else
910 goto out;
911 }
912
913 b = mca_bucket_alloc(c, k, __GFP_NOWARN|GFP_NOIO);
914 if (!b)
915 goto err;
916
917 BUG_ON(!down_write_trylock(&b->lock));
918 if (!b->sets->data)
919 goto err;
920out:
921 BUG_ON(!closure_is_unlocked(&b->io.cl));
922
923 bkey_copy(&b->key, k);
924 list_move(&b->list, &c->btree_cache);
925 hlist_del_init_rcu(&b->hash);
926 hlist_add_head_rcu(&b->hash, mca_hash(c, k));
927
928 lock_set_subclass(&b->lock.dep_map, level + 1, _THIS_IP_);
929 b->level = level;
Kent Overstreetd6fd3b12013-07-24 17:20:19 -0700930 b->parent = (void *) ~0UL;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700931
932 mca_reinit(b);
933
934 return b;
935err:
936 if (b)
937 rw_unlock(true, b);
938
Kent Overstreete8e1d462013-07-24 17:27:07 -0700939 b = mca_cannibalize(c, k);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700940 if (!IS_ERR(b))
941 goto out;
942
943 return b;
944}
945
946/**
947 * bch_btree_node_get - find a btree node in the cache and lock it, reading it
948 * in from disk if necessary.
949 *
Kent Overstreetb54d6932013-07-24 18:04:18 -0700950 * If IO is necessary and running under generic_make_request, returns -EAGAIN.
Kent Overstreetcafe5632013-03-23 16:11:31 -0700951 *
952 * The btree node will have either a read or a write lock held, depending on
953 * level and op->lock.
954 */
955struct btree *bch_btree_node_get(struct cache_set *c, struct bkey *k,
Kent Overstreete8e1d462013-07-24 17:27:07 -0700956 int level, bool write)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700957{
958 int i = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700959 struct btree *b;
960
961 BUG_ON(level < 0);
962retry:
963 b = mca_find(c, k);
964
965 if (!b) {
Kent Overstreet57943512013-04-25 13:58:35 -0700966 if (current->bio_list)
967 return ERR_PTR(-EAGAIN);
968
Kent Overstreetcafe5632013-03-23 16:11:31 -0700969 mutex_lock(&c->bucket_lock);
Kent Overstreete8e1d462013-07-24 17:27:07 -0700970 b = mca_alloc(c, k, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700971 mutex_unlock(&c->bucket_lock);
972
973 if (!b)
974 goto retry;
975 if (IS_ERR(b))
976 return b;
977
Kent Overstreet57943512013-04-25 13:58:35 -0700978 bch_btree_node_read(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700979
980 if (!write)
981 downgrade_write(&b->lock);
982 } else {
983 rw_lock(write, b, level);
984 if (PTR_HASH(c, &b->key) != PTR_HASH(c, k)) {
985 rw_unlock(write, b);
986 goto retry;
987 }
988 BUG_ON(b->level != level);
989 }
990
991 b->accessed = 1;
992
993 for (; i <= b->nsets && b->sets[i].size; i++) {
994 prefetch(b->sets[i].tree);
995 prefetch(b->sets[i].data);
996 }
997
998 for (; i <= b->nsets; i++)
999 prefetch(b->sets[i].data);
1000
Kent Overstreet57943512013-04-25 13:58:35 -07001001 if (btree_node_io_error(b)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001002 rw_unlock(write, b);
Kent Overstreet57943512013-04-25 13:58:35 -07001003 return ERR_PTR(-EIO);
1004 }
1005
1006 BUG_ON(!b->written);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001007
1008 return b;
1009}
1010
1011static void btree_node_prefetch(struct cache_set *c, struct bkey *k, int level)
1012{
1013 struct btree *b;
1014
1015 mutex_lock(&c->bucket_lock);
Kent Overstreete8e1d462013-07-24 17:27:07 -07001016 b = mca_alloc(c, k, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001017 mutex_unlock(&c->bucket_lock);
1018
1019 if (!IS_ERR_OR_NULL(b)) {
Kent Overstreet57943512013-04-25 13:58:35 -07001020 bch_btree_node_read(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001021 rw_unlock(true, b);
1022 }
1023}
1024
1025/* Btree alloc */
1026
Kent Overstreete8e1d462013-07-24 17:27:07 -07001027static void btree_node_free(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001028{
1029 unsigned i;
1030
Kent Overstreetc37511b2013-04-26 15:39:55 -07001031 trace_bcache_btree_node_free(b);
1032
Kent Overstreetcafe5632013-03-23 16:11:31 -07001033 BUG_ON(b == b->c->root);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001034
1035 if (btree_node_dirty(b))
1036 btree_complete_write(b, btree_current_write(b));
1037 clear_bit(BTREE_NODE_dirty, &b->flags);
1038
Kent Overstreetcafe5632013-03-23 16:11:31 -07001039 cancel_delayed_work(&b->work);
1040
1041 mutex_lock(&b->c->bucket_lock);
1042
1043 for (i = 0; i < KEY_PTRS(&b->key); i++) {
1044 BUG_ON(atomic_read(&PTR_BUCKET(b->c, &b->key, i)->pin));
1045
1046 bch_inc_gen(PTR_CACHE(b->c, &b->key, i),
1047 PTR_BUCKET(b->c, &b->key, i));
1048 }
1049
1050 bch_bucket_free(b->c, &b->key);
1051 mca_bucket_free(b);
1052 mutex_unlock(&b->c->bucket_lock);
1053}
1054
Kent Overstreet35fcd842013-07-24 17:29:09 -07001055struct btree *bch_btree_node_alloc(struct cache_set *c, int level)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001056{
1057 BKEY_PADDED(key) k;
1058 struct btree *b = ERR_PTR(-EAGAIN);
1059
1060 mutex_lock(&c->bucket_lock);
1061retry:
Kent Overstreet35fcd842013-07-24 17:29:09 -07001062 if (__bch_bucket_alloc_set(c, WATERMARK_METADATA, &k.key, 1, true))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001063 goto err;
1064
Kent Overstreet3a3b6a42013-07-24 16:46:42 -07001065 bkey_put(c, &k.key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001066 SET_KEY_SIZE(&k.key, c->btree_pages * PAGE_SECTORS);
1067
Kent Overstreete8e1d462013-07-24 17:27:07 -07001068 b = mca_alloc(c, &k.key, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001069 if (IS_ERR(b))
1070 goto err_free;
1071
1072 if (!b) {
Kent Overstreetb1a67b02013-03-25 11:46:44 -07001073 cache_bug(c,
1074 "Tried to allocate bucket that was in btree cache");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001075 goto retry;
1076 }
1077
Kent Overstreetcafe5632013-03-23 16:11:31 -07001078 b->accessed = 1;
1079 bch_bset_init_next(b);
1080
1081 mutex_unlock(&c->bucket_lock);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001082
1083 trace_bcache_btree_node_alloc(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001084 return b;
1085err_free:
1086 bch_bucket_free(c, &k.key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001087err:
1088 mutex_unlock(&c->bucket_lock);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001089
1090 trace_bcache_btree_node_alloc_fail(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001091 return b;
1092}
1093
Kent Overstreet35fcd842013-07-24 17:29:09 -07001094static struct btree *btree_node_alloc_replacement(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001095{
Kent Overstreet35fcd842013-07-24 17:29:09 -07001096 struct btree *n = bch_btree_node_alloc(b->c, b->level);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001097 if (!IS_ERR_OR_NULL(n))
1098 bch_btree_sort_into(b, n);
1099
1100 return n;
1101}
1102
1103/* Garbage collection */
1104
1105uint8_t __bch_btree_mark_key(struct cache_set *c, int level, struct bkey *k)
1106{
1107 uint8_t stale = 0;
1108 unsigned i;
1109 struct bucket *g;
1110
1111 /*
1112 * ptr_invalid() can't return true for the keys that mark btree nodes as
1113 * freed, but since ptr_bad() returns true we'll never actually use them
1114 * for anything and thus we don't want mark their pointers here
1115 */
1116 if (!bkey_cmp(k, &ZERO_KEY))
1117 return stale;
1118
1119 for (i = 0; i < KEY_PTRS(k); i++) {
1120 if (!ptr_available(c, k, i))
1121 continue;
1122
1123 g = PTR_BUCKET(c, k, i);
1124
1125 if (gen_after(g->gc_gen, PTR_GEN(k, i)))
1126 g->gc_gen = PTR_GEN(k, i);
1127
1128 if (ptr_stale(c, k, i)) {
1129 stale = max(stale, ptr_stale(c, k, i));
1130 continue;
1131 }
1132
1133 cache_bug_on(GC_MARK(g) &&
1134 (GC_MARK(g) == GC_MARK_METADATA) != (level != 0),
1135 c, "inconsistent ptrs: mark = %llu, level = %i",
1136 GC_MARK(g), level);
1137
1138 if (level)
1139 SET_GC_MARK(g, GC_MARK_METADATA);
1140 else if (KEY_DIRTY(k))
1141 SET_GC_MARK(g, GC_MARK_DIRTY);
1142
1143 /* guard against overflow */
1144 SET_GC_SECTORS_USED(g, min_t(unsigned,
1145 GC_SECTORS_USED(g) + KEY_SIZE(k),
1146 (1 << 14) - 1));
1147
1148 BUG_ON(!GC_SECTORS_USED(g));
1149 }
1150
1151 return stale;
1152}
1153
1154#define btree_mark_key(b, k) __bch_btree_mark_key(b->c, b->level, k)
1155
1156static int btree_gc_mark_node(struct btree *b, unsigned *keys,
1157 struct gc_stat *gc)
1158{
1159 uint8_t stale = 0;
1160 unsigned last_dev = -1;
1161 struct bcache_device *d = NULL;
1162 struct bkey *k;
1163 struct btree_iter iter;
1164 struct bset_tree *t;
1165
1166 gc->nodes++;
1167
1168 for_each_key_filter(b, k, &iter, bch_ptr_invalid) {
1169 if (last_dev != KEY_INODE(k)) {
1170 last_dev = KEY_INODE(k);
1171
1172 d = KEY_INODE(k) < b->c->nr_uuids
1173 ? b->c->devices[last_dev]
1174 : NULL;
1175 }
1176
1177 stale = max(stale, btree_mark_key(b, k));
1178
1179 if (bch_ptr_bad(b, k))
1180 continue;
1181
1182 *keys += bkey_u64s(k);
1183
1184 gc->key_bytes += bkey_u64s(k);
1185 gc->nkeys++;
1186
1187 gc->data += KEY_SIZE(k);
Kent Overstreet444fc0b2013-05-11 17:07:26 -07001188 if (KEY_DIRTY(k))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001189 gc->dirty += KEY_SIZE(k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001190 }
1191
1192 for (t = b->sets; t <= &b->sets[b->nsets]; t++)
1193 btree_bug_on(t->size &&
1194 bset_written(b, t) &&
1195 bkey_cmp(&b->key, &t->end) < 0,
1196 b, "found short btree key in gc");
1197
1198 return stale;
1199}
1200
Kent Overstreete8e1d462013-07-24 17:27:07 -07001201static struct btree *btree_gc_alloc(struct btree *b, struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001202{
1203 /*
1204 * We block priorities from being written for the duration of garbage
1205 * collection, so we can't sleep in btree_alloc() ->
1206 * bch_bucket_alloc_set(), or we'd risk deadlock - so we don't pass it
1207 * our closure.
1208 */
Kent Overstreet35fcd842013-07-24 17:29:09 -07001209 struct btree *n = btree_node_alloc_replacement(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001210
1211 if (!IS_ERR_OR_NULL(n)) {
1212 swap(b, n);
1213
1214 memcpy(k->ptr, b->key.ptr,
1215 sizeof(uint64_t) * KEY_PTRS(&b->key));
1216
Kent Overstreete8e1d462013-07-24 17:27:07 -07001217 btree_node_free(n);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001218 up_write(&n->lock);
1219 }
1220
1221 return b;
1222}
1223
1224/*
1225 * Leaving this at 2 until we've got incremental garbage collection done; it
1226 * could be higher (and has been tested with 4) except that garbage collection
1227 * could take much longer, adversely affecting latency.
1228 */
1229#define GC_MERGE_NODES 2U
1230
1231struct gc_merge_info {
1232 struct btree *b;
1233 struct bkey *k;
1234 unsigned keys;
1235};
1236
Kent Overstreete8e1d462013-07-24 17:27:07 -07001237static void btree_gc_coalesce(struct btree *b, struct gc_stat *gc,
1238 struct gc_merge_info *r)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001239{
1240 unsigned nodes = 0, keys = 0, blocks;
1241 int i;
Kent Overstreetb54d6932013-07-24 18:04:18 -07001242 struct closure cl;
1243
1244 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001245
1246 while (nodes < GC_MERGE_NODES && r[nodes].b)
1247 keys += r[nodes++].keys;
1248
1249 blocks = btree_default_blocks(b->c) * 2 / 3;
1250
1251 if (nodes < 2 ||
1252 __set_blocks(b->sets[0].data, keys, b->c) > blocks * (nodes - 1))
1253 return;
1254
1255 for (i = nodes - 1; i >= 0; --i) {
1256 if (r[i].b->written)
Kent Overstreete8e1d462013-07-24 17:27:07 -07001257 r[i].b = btree_gc_alloc(r[i].b, r[i].k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001258
1259 if (r[i].b->written)
1260 return;
1261 }
1262
1263 for (i = nodes - 1; i > 0; --i) {
1264 struct bset *n1 = r[i].b->sets->data;
1265 struct bset *n2 = r[i - 1].b->sets->data;
1266 struct bkey *k, *last = NULL;
1267
1268 keys = 0;
1269
1270 if (i == 1) {
1271 /*
1272 * Last node we're not getting rid of - we're getting
1273 * rid of the node at r[0]. Have to try and fit all of
1274 * the remaining keys into this node; we can't ensure
1275 * they will always fit due to rounding and variable
1276 * length keys (shouldn't be possible in practice,
1277 * though)
1278 */
1279 if (__set_blocks(n1, n1->keys + r->keys,
1280 b->c) > btree_blocks(r[i].b))
1281 return;
1282
1283 keys = n2->keys;
1284 last = &r->b->key;
1285 } else
1286 for (k = n2->start;
1287 k < end(n2);
1288 k = bkey_next(k)) {
1289 if (__set_blocks(n1, n1->keys + keys +
1290 bkey_u64s(k), b->c) > blocks)
1291 break;
1292
1293 last = k;
1294 keys += bkey_u64s(k);
1295 }
1296
1297 BUG_ON(__set_blocks(n1, n1->keys + keys,
1298 b->c) > btree_blocks(r[i].b));
1299
1300 if (last) {
1301 bkey_copy_key(&r[i].b->key, last);
1302 bkey_copy_key(r[i].k, last);
1303 }
1304
1305 memcpy(end(n1),
1306 n2->start,
1307 (void *) node(n2, keys) - (void *) n2->start);
1308
1309 n1->keys += keys;
1310
1311 memmove(n2->start,
1312 node(n2, keys),
1313 (void *) end(n2) - (void *) node(n2, keys));
1314
1315 n2->keys -= keys;
1316
1317 r[i].keys = n1->keys;
1318 r[i - 1].keys = n2->keys;
1319 }
1320
Kent Overstreete8e1d462013-07-24 17:27:07 -07001321 btree_node_free(r->b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001322 up_write(&r->b->lock);
1323
Kent Overstreetc37511b2013-04-26 15:39:55 -07001324 trace_bcache_btree_gc_coalesce(nodes);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001325
1326 gc->nodes--;
1327 nodes--;
1328
1329 memmove(&r[0], &r[1], sizeof(struct gc_merge_info) * nodes);
1330 memset(&r[nodes], 0, sizeof(struct gc_merge_info));
1331}
1332
1333static int btree_gc_recurse(struct btree *b, struct btree_op *op,
1334 struct closure *writes, struct gc_stat *gc)
1335{
1336 void write(struct btree *r)
1337 {
Kent Overstreetb54d6932013-07-24 18:04:18 -07001338 if (!r->written || btree_node_dirty(r))
Kent Overstreet57943512013-04-25 13:58:35 -07001339 bch_btree_node_write(r, writes);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001340
1341 up_write(&r->lock);
1342 }
1343
1344 int ret = 0, stale;
1345 unsigned i;
1346 struct gc_merge_info r[GC_MERGE_NODES];
1347
1348 memset(r, 0, sizeof(r));
1349
1350 while ((r->k = bch_next_recurse_key(b, &b->c->gc_done))) {
Kent Overstreete8e1d462013-07-24 17:27:07 -07001351 r->b = bch_btree_node_get(b->c, r->k, b->level - 1, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001352
1353 if (IS_ERR(r->b)) {
1354 ret = PTR_ERR(r->b);
1355 break;
1356 }
1357
1358 r->keys = 0;
1359 stale = btree_gc_mark_node(r->b, &r->keys, gc);
1360
1361 if (!b->written &&
1362 (r->b->level || stale > 10 ||
1363 b->c->gc_always_rewrite))
Kent Overstreete8e1d462013-07-24 17:27:07 -07001364 r->b = btree_gc_alloc(r->b, r->k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001365
1366 if (r->b->level)
1367 ret = btree_gc_recurse(r->b, op, writes, gc);
1368
1369 if (ret) {
1370 write(r->b);
1371 break;
1372 }
1373
1374 bkey_copy_key(&b->c->gc_done, r->k);
1375
1376 if (!b->written)
Kent Overstreete8e1d462013-07-24 17:27:07 -07001377 btree_gc_coalesce(b, gc, r);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001378
1379 if (r[GC_MERGE_NODES - 1].b)
1380 write(r[GC_MERGE_NODES - 1].b);
1381
1382 memmove(&r[1], &r[0],
1383 sizeof(struct gc_merge_info) * (GC_MERGE_NODES - 1));
1384
1385 /* When we've got incremental GC working, we'll want to do
1386 * if (should_resched())
1387 * return -EAGAIN;
1388 */
1389 cond_resched();
1390#if 0
1391 if (need_resched()) {
1392 ret = -EAGAIN;
1393 break;
1394 }
1395#endif
1396 }
1397
1398 for (i = 1; i < GC_MERGE_NODES && r[i].b; i++)
1399 write(r[i].b);
1400
1401 /* Might have freed some children, must remove their keys */
1402 if (!b->written)
1403 bch_btree_sort(b);
1404
1405 return ret;
1406}
1407
1408static int bch_btree_gc_root(struct btree *b, struct btree_op *op,
1409 struct closure *writes, struct gc_stat *gc)
1410{
1411 struct btree *n = NULL;
1412 unsigned keys = 0;
1413 int ret = 0, stale = btree_gc_mark_node(b, &keys, gc);
Kent Overstreetb54d6932013-07-24 18:04:18 -07001414 struct closure cl;
1415
1416 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001417
1418 if (b->level || stale > 10)
Kent Overstreet35fcd842013-07-24 17:29:09 -07001419 n = btree_node_alloc_replacement(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001420
1421 if (!IS_ERR_OR_NULL(n))
1422 swap(b, n);
1423
1424 if (b->level)
1425 ret = btree_gc_recurse(b, op, writes, gc);
1426
1427 if (!b->written || btree_node_dirty(b)) {
Kent Overstreetb54d6932013-07-24 18:04:18 -07001428 bch_btree_node_write(b, n ? &cl : NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001429 }
1430
1431 if (!IS_ERR_OR_NULL(n)) {
Kent Overstreetb54d6932013-07-24 18:04:18 -07001432 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001433 bch_btree_set_root(b);
Kent Overstreete8e1d462013-07-24 17:27:07 -07001434 btree_node_free(n);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001435 rw_unlock(true, b);
1436 }
1437
1438 return ret;
1439}
1440
1441static void btree_gc_start(struct cache_set *c)
1442{
1443 struct cache *ca;
1444 struct bucket *b;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001445 unsigned i;
1446
1447 if (!c->gc_mark_valid)
1448 return;
1449
1450 mutex_lock(&c->bucket_lock);
1451
1452 c->gc_mark_valid = 0;
1453 c->gc_done = ZERO_KEY;
1454
1455 for_each_cache(ca, c, i)
1456 for_each_bucket(b, ca) {
1457 b->gc_gen = b->gen;
Kent Overstreet29ebf462013-07-11 19:43:21 -07001458 if (!atomic_read(&b->pin)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001459 SET_GC_MARK(b, GC_MARK_RECLAIMABLE);
Kent Overstreet29ebf462013-07-11 19:43:21 -07001460 SET_GC_SECTORS_USED(b, 0);
1461 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001462 }
1463
Kent Overstreetcafe5632013-03-23 16:11:31 -07001464 mutex_unlock(&c->bucket_lock);
1465}
1466
1467size_t bch_btree_gc_finish(struct cache_set *c)
1468{
1469 size_t available = 0;
1470 struct bucket *b;
1471 struct cache *ca;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001472 unsigned i;
1473
1474 mutex_lock(&c->bucket_lock);
1475
1476 set_gc_sectors(c);
1477 c->gc_mark_valid = 1;
1478 c->need_gc = 0;
1479
1480 if (c->root)
1481 for (i = 0; i < KEY_PTRS(&c->root->key); i++)
1482 SET_GC_MARK(PTR_BUCKET(c, &c->root->key, i),
1483 GC_MARK_METADATA);
1484
1485 for (i = 0; i < KEY_PTRS(&c->uuid_bucket); i++)
1486 SET_GC_MARK(PTR_BUCKET(c, &c->uuid_bucket, i),
1487 GC_MARK_METADATA);
1488
1489 for_each_cache(ca, c, i) {
1490 uint64_t *i;
1491
1492 ca->invalidate_needs_gc = 0;
1493
1494 for (i = ca->sb.d; i < ca->sb.d + ca->sb.keys; i++)
1495 SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA);
1496
1497 for (i = ca->prio_buckets;
1498 i < ca->prio_buckets + prio_buckets(ca) * 2; i++)
1499 SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA);
1500
1501 for_each_bucket(b, ca) {
1502 b->last_gc = b->gc_gen;
1503 c->need_gc = max(c->need_gc, bucket_gc_gen(b));
1504
1505 if (!atomic_read(&b->pin) &&
1506 GC_MARK(b) == GC_MARK_RECLAIMABLE) {
1507 available++;
1508 if (!GC_SECTORS_USED(b))
1509 bch_bucket_add_unused(ca, b);
1510 }
1511 }
1512 }
1513
Kent Overstreetcafe5632013-03-23 16:11:31 -07001514 mutex_unlock(&c->bucket_lock);
1515 return available;
1516}
1517
Kent Overstreet72a44512013-10-24 17:19:26 -07001518static void bch_btree_gc(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001519{
Kent Overstreetcafe5632013-03-23 16:11:31 -07001520 int ret;
1521 unsigned long available;
1522 struct gc_stat stats;
1523 struct closure writes;
1524 struct btree_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001525 uint64_t start_time = local_clock();
Kent Overstreet57943512013-04-25 13:58:35 -07001526
Kent Overstreetc37511b2013-04-26 15:39:55 -07001527 trace_bcache_gc_start(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001528
1529 memset(&stats, 0, sizeof(struct gc_stat));
1530 closure_init_stack(&writes);
Kent Overstreetb54d6932013-07-24 18:04:18 -07001531 bch_btree_op_init(&op, SHRT_MAX);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001532
1533 btree_gc_start(c);
1534
Kent Overstreet57943512013-04-25 13:58:35 -07001535 atomic_inc(&c->prio_blocked);
1536
Kent Overstreetcafe5632013-03-23 16:11:31 -07001537 ret = btree_root(gc_root, c, &op, &writes, &stats);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001538 closure_sync(&writes);
1539
1540 if (ret) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001541 pr_warn("gc failed!");
Kent Overstreet72a44512013-10-24 17:19:26 -07001542 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001543 }
1544
1545 /* Possibly wait for new UUIDs or whatever to hit disk */
Kent Overstreetb54d6932013-07-24 18:04:18 -07001546 bch_journal_meta(c, &writes);
1547 closure_sync(&writes);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001548
1549 available = bch_btree_gc_finish(c);
1550
Kent Overstreet57943512013-04-25 13:58:35 -07001551 atomic_dec(&c->prio_blocked);
1552 wake_up_allocators(c);
1553
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001554 bch_time_stats_update(&c->btree_gc_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001555
1556 stats.key_bytes *= sizeof(uint64_t);
1557 stats.dirty <<= 9;
1558 stats.data <<= 9;
1559 stats.in_use = (c->nbuckets - available) * 100 / c->nbuckets;
1560 memcpy(&c->gc_stats, &stats, sizeof(struct gc_stat));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001561
Kent Overstreetc37511b2013-04-26 15:39:55 -07001562 trace_bcache_gc_end(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001563
Kent Overstreet72a44512013-10-24 17:19:26 -07001564 bch_moving_gc(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001565}
1566
Kent Overstreet72a44512013-10-24 17:19:26 -07001567static int bch_gc_thread(void *arg)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001568{
Kent Overstreet72a44512013-10-24 17:19:26 -07001569 struct cache_set *c = arg;
1570
1571 while (1) {
1572 bch_btree_gc(c);
1573
1574 set_current_state(TASK_INTERRUPTIBLE);
1575 if (kthread_should_stop())
1576 break;
1577
1578 try_to_freeze();
1579 schedule();
1580 }
1581
1582 return 0;
1583}
1584
1585int bch_gc_thread_start(struct cache_set *c)
1586{
1587 c->gc_thread = kthread_create(bch_gc_thread, c, "bcache_gc");
1588 if (IS_ERR(c->gc_thread))
1589 return PTR_ERR(c->gc_thread);
1590
1591 set_task_state(c->gc_thread, TASK_INTERRUPTIBLE);
1592 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001593}
1594
1595/* Initial partial gc */
1596
1597static int bch_btree_check_recurse(struct btree *b, struct btree_op *op,
1598 unsigned long **seen)
1599{
1600 int ret;
1601 unsigned i;
1602 struct bkey *k;
1603 struct bucket *g;
1604 struct btree_iter iter;
1605
1606 for_each_key_filter(b, k, &iter, bch_ptr_invalid) {
1607 for (i = 0; i < KEY_PTRS(k); i++) {
1608 if (!ptr_available(b->c, k, i))
1609 continue;
1610
1611 g = PTR_BUCKET(b->c, k, i);
1612
1613 if (!__test_and_set_bit(PTR_BUCKET_NR(b->c, k, i),
1614 seen[PTR_DEV(k, i)]) ||
1615 !ptr_stale(b->c, k, i)) {
1616 g->gen = PTR_GEN(k, i);
1617
1618 if (b->level)
1619 g->prio = BTREE_PRIO;
1620 else if (g->prio == BTREE_PRIO)
1621 g->prio = INITIAL_PRIO;
1622 }
1623 }
1624
1625 btree_mark_key(b, k);
1626 }
1627
1628 if (b->level) {
1629 k = bch_next_recurse_key(b, &ZERO_KEY);
1630
1631 while (k) {
1632 struct bkey *p = bch_next_recurse_key(b, k);
1633 if (p)
1634 btree_node_prefetch(b->c, p, b->level - 1);
1635
1636 ret = btree(check_recurse, k, b, op, seen);
1637 if (ret)
1638 return ret;
1639
1640 k = p;
1641 }
1642 }
1643
1644 return 0;
1645}
1646
Kent Overstreetc18536a2013-07-24 17:44:17 -07001647int bch_btree_check(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001648{
1649 int ret = -ENOMEM;
1650 unsigned i;
1651 unsigned long *seen[MAX_CACHES_PER_SET];
Kent Overstreetc18536a2013-07-24 17:44:17 -07001652 struct btree_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001653
1654 memset(seen, 0, sizeof(seen));
Kent Overstreetb54d6932013-07-24 18:04:18 -07001655 bch_btree_op_init(&op, SHRT_MAX);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001656
1657 for (i = 0; c->cache[i]; i++) {
1658 size_t n = DIV_ROUND_UP(c->cache[i]->sb.nbuckets, 8);
1659 seen[i] = kmalloc(n, GFP_KERNEL);
1660 if (!seen[i])
1661 goto err;
1662
1663 /* Disables the seen array until prio_read() uses it too */
1664 memset(seen[i], 0xFF, n);
1665 }
1666
Kent Overstreetc18536a2013-07-24 17:44:17 -07001667 ret = btree_root(check_recurse, c, &op, seen);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001668err:
1669 for (i = 0; i < MAX_CACHES_PER_SET; i++)
1670 kfree(seen[i]);
1671 return ret;
1672}
1673
1674/* Btree insertion */
1675
1676static void shift_keys(struct btree *b, struct bkey *where, struct bkey *insert)
1677{
1678 struct bset *i = b->sets[b->nsets].data;
1679
1680 memmove((uint64_t *) where + bkey_u64s(insert),
1681 where,
1682 (void *) end(i) - (void *) where);
1683
1684 i->keys += bkey_u64s(insert);
1685 bkey_copy(where, insert);
1686 bch_bset_fix_lookup_table(b, where);
1687}
1688
Kent Overstreet1b207d82013-09-10 18:52:54 -07001689static bool fix_overlapping_extents(struct btree *b, struct bkey *insert,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001690 struct btree_iter *iter,
Kent Overstreet1b207d82013-09-10 18:52:54 -07001691 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001692{
Kent Overstreet279afba2013-06-05 06:21:07 -07001693 void subtract_dirty(struct bkey *k, uint64_t offset, int sectors)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001694 {
Kent Overstreet279afba2013-06-05 06:21:07 -07001695 if (KEY_DIRTY(k))
1696 bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k),
1697 offset, -sectors);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001698 }
1699
Kent Overstreet279afba2013-06-05 06:21:07 -07001700 uint64_t old_offset;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001701 unsigned old_size, sectors_found = 0;
1702
1703 while (1) {
1704 struct bkey *k = bch_btree_iter_next(iter);
1705 if (!k ||
1706 bkey_cmp(&START_KEY(k), insert) >= 0)
1707 break;
1708
1709 if (bkey_cmp(k, &START_KEY(insert)) <= 0)
1710 continue;
1711
Kent Overstreet279afba2013-06-05 06:21:07 -07001712 old_offset = KEY_START(k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001713 old_size = KEY_SIZE(k);
1714
1715 /*
1716 * We might overlap with 0 size extents; we can't skip these
1717 * because if they're in the set we're inserting to we have to
1718 * adjust them so they don't overlap with the key we're
Kent Overstreet1b207d82013-09-10 18:52:54 -07001719 * inserting. But we don't want to check them for replace
Kent Overstreetcafe5632013-03-23 16:11:31 -07001720 * operations.
1721 */
1722
Kent Overstreet1b207d82013-09-10 18:52:54 -07001723 if (replace_key && KEY_SIZE(k)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001724 /*
1725 * k might have been split since we inserted/found the
1726 * key we're replacing
1727 */
1728 unsigned i;
1729 uint64_t offset = KEY_START(k) -
Kent Overstreet1b207d82013-09-10 18:52:54 -07001730 KEY_START(replace_key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001731
1732 /* But it must be a subset of the replace key */
Kent Overstreet1b207d82013-09-10 18:52:54 -07001733 if (KEY_START(k) < KEY_START(replace_key) ||
1734 KEY_OFFSET(k) > KEY_OFFSET(replace_key))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001735 goto check_failed;
1736
1737 /* We didn't find a key that we were supposed to */
1738 if (KEY_START(k) > KEY_START(insert) + sectors_found)
1739 goto check_failed;
1740
Kent Overstreet1b207d82013-09-10 18:52:54 -07001741 if (KEY_PTRS(replace_key) != KEY_PTRS(k))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001742 goto check_failed;
1743
1744 /* skip past gen */
1745 offset <<= 8;
1746
Kent Overstreet1b207d82013-09-10 18:52:54 -07001747 BUG_ON(!KEY_PTRS(replace_key));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001748
Kent Overstreet1b207d82013-09-10 18:52:54 -07001749 for (i = 0; i < KEY_PTRS(replace_key); i++)
1750 if (k->ptr[i] != replace_key->ptr[i] + offset)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001751 goto check_failed;
1752
1753 sectors_found = KEY_OFFSET(k) - KEY_START(insert);
1754 }
1755
1756 if (bkey_cmp(insert, k) < 0 &&
1757 bkey_cmp(&START_KEY(insert), &START_KEY(k)) > 0) {
1758 /*
1759 * We overlapped in the middle of an existing key: that
1760 * means we have to split the old key. But we have to do
1761 * slightly different things depending on whether the
1762 * old key has been written out yet.
1763 */
1764
1765 struct bkey *top;
1766
Kent Overstreet279afba2013-06-05 06:21:07 -07001767 subtract_dirty(k, KEY_START(insert), KEY_SIZE(insert));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001768
1769 if (bkey_written(b, k)) {
1770 /*
1771 * We insert a new key to cover the top of the
1772 * old key, and the old key is modified in place
1773 * to represent the bottom split.
1774 *
1775 * It's completely arbitrary whether the new key
1776 * is the top or the bottom, but it has to match
1777 * up with what btree_sort_fixup() does - it
1778 * doesn't check for this kind of overlap, it
1779 * depends on us inserting a new key for the top
1780 * here.
1781 */
1782 top = bch_bset_search(b, &b->sets[b->nsets],
1783 insert);
1784 shift_keys(b, top, k);
1785 } else {
1786 BKEY_PADDED(key) temp;
1787 bkey_copy(&temp.key, k);
1788 shift_keys(b, k, &temp.key);
1789 top = bkey_next(k);
1790 }
1791
1792 bch_cut_front(insert, top);
1793 bch_cut_back(&START_KEY(insert), k);
1794 bch_bset_fix_invalidated_key(b, k);
1795 return false;
1796 }
1797
1798 if (bkey_cmp(insert, k) < 0) {
1799 bch_cut_front(insert, k);
1800 } else {
Kent Overstreet1fa84552013-11-10 21:55:27 -08001801 if (bkey_cmp(&START_KEY(insert), &START_KEY(k)) > 0)
1802 old_offset = KEY_START(insert);
1803
Kent Overstreetcafe5632013-03-23 16:11:31 -07001804 if (bkey_written(b, k) &&
1805 bkey_cmp(&START_KEY(insert), &START_KEY(k)) <= 0) {
1806 /*
1807 * Completely overwrote, so we don't have to
1808 * invalidate the binary search tree
1809 */
1810 bch_cut_front(k, k);
1811 } else {
1812 __bch_cut_back(&START_KEY(insert), k);
1813 bch_bset_fix_invalidated_key(b, k);
1814 }
1815 }
1816
Kent Overstreet279afba2013-06-05 06:21:07 -07001817 subtract_dirty(k, old_offset, old_size - KEY_SIZE(k));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001818 }
1819
1820check_failed:
Kent Overstreet1b207d82013-09-10 18:52:54 -07001821 if (replace_key) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001822 if (!sectors_found) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001823 return true;
1824 } else if (sectors_found < KEY_SIZE(insert)) {
1825 SET_KEY_OFFSET(insert, KEY_OFFSET(insert) -
1826 (KEY_SIZE(insert) - sectors_found));
1827 SET_KEY_SIZE(insert, sectors_found);
1828 }
1829 }
1830
1831 return false;
1832}
1833
1834static bool btree_insert_key(struct btree *b, struct btree_op *op,
Kent Overstreet1b207d82013-09-10 18:52:54 -07001835 struct bkey *k, struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001836{
1837 struct bset *i = b->sets[b->nsets].data;
1838 struct bkey *m, *prev;
Kent Overstreet85b14922013-05-14 20:33:16 -07001839 unsigned status = BTREE_INSERT_STATUS_INSERT;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001840
1841 BUG_ON(bkey_cmp(k, &b->key) > 0);
1842 BUG_ON(b->level && !KEY_PTRS(k));
1843 BUG_ON(!b->level && !KEY_OFFSET(k));
1844
1845 if (!b->level) {
1846 struct btree_iter iter;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001847
1848 /*
1849 * bset_search() returns the first key that is strictly greater
1850 * than the search key - but for back merging, we want to find
Kent Overstreet0eacac22013-07-01 19:29:05 -07001851 * the previous key.
Kent Overstreetcafe5632013-03-23 16:11:31 -07001852 */
Kent Overstreetcafe5632013-03-23 16:11:31 -07001853 prev = NULL;
Kent Overstreet0eacac22013-07-01 19:29:05 -07001854 m = bch_btree_iter_init(b, &iter, PRECEDING_KEY(&START_KEY(k)));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001855
Kent Overstreet1b207d82013-09-10 18:52:54 -07001856 if (fix_overlapping_extents(b, k, &iter, replace_key)) {
1857 op->insert_collision = true;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001858 return false;
Kent Overstreet1b207d82013-09-10 18:52:54 -07001859 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001860
Kent Overstreet1fa84552013-11-10 21:55:27 -08001861 if (KEY_DIRTY(k))
1862 bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k),
1863 KEY_START(k), KEY_SIZE(k));
1864
Kent Overstreetcafe5632013-03-23 16:11:31 -07001865 while (m != end(i) &&
1866 bkey_cmp(k, &START_KEY(m)) > 0)
1867 prev = m, m = bkey_next(m);
1868
1869 if (key_merging_disabled(b->c))
1870 goto insert;
1871
1872 /* prev is in the tree, if we merge we're done */
Kent Overstreet85b14922013-05-14 20:33:16 -07001873 status = BTREE_INSERT_STATUS_BACK_MERGE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001874 if (prev &&
1875 bch_bkey_try_merge(b, prev, k))
1876 goto merged;
1877
Kent Overstreet85b14922013-05-14 20:33:16 -07001878 status = BTREE_INSERT_STATUS_OVERWROTE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001879 if (m != end(i) &&
1880 KEY_PTRS(m) == KEY_PTRS(k) && !KEY_SIZE(m))
1881 goto copy;
1882
Kent Overstreet85b14922013-05-14 20:33:16 -07001883 status = BTREE_INSERT_STATUS_FRONT_MERGE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001884 if (m != end(i) &&
1885 bch_bkey_try_merge(b, k, m))
1886 goto copy;
Kent Overstreet1b207d82013-09-10 18:52:54 -07001887 } else {
1888 BUG_ON(replace_key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001889 m = bch_bset_search(b, &b->sets[b->nsets], k);
Kent Overstreet1b207d82013-09-10 18:52:54 -07001890 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001891
1892insert: shift_keys(b, m, k);
1893copy: bkey_copy(m, k);
1894merged:
Kent Overstreet1b207d82013-09-10 18:52:54 -07001895 bch_check_keys(b, "%u for %s", status,
1896 replace_key ? "replace" : "insert");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001897
1898 if (b->level && !KEY_OFFSET(k))
Kent Overstreet57943512013-04-25 13:58:35 -07001899 btree_current_write(b)->prio_blocked++;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001900
Kent Overstreet1b207d82013-09-10 18:52:54 -07001901 trace_bcache_btree_insert_key(b, k, replace_key != NULL, status);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001902
1903 return true;
1904}
1905
Kent Overstreet26c949f2013-09-10 18:41:15 -07001906static bool bch_btree_insert_keys(struct btree *b, struct btree_op *op,
Kent Overstreet1b207d82013-09-10 18:52:54 -07001907 struct keylist *insert_keys,
1908 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001909{
1910 bool ret = false;
Kent Overstreet280481d2013-10-24 16:36:03 -07001911 int oldsize = bch_count_data(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001912
Kent Overstreet26c949f2013-09-10 18:41:15 -07001913 while (!bch_keylist_empty(insert_keys)) {
Kent Overstreet403b6cd2013-07-24 17:22:44 -07001914 struct bset *i = write_block(b);
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07001915 struct bkey *k = insert_keys->keys;
Kent Overstreet26c949f2013-09-10 18:41:15 -07001916
Kent Overstreet403b6cd2013-07-24 17:22:44 -07001917 if (b->written + __set_blocks(i, i->keys + bkey_u64s(k), b->c)
1918 > btree_blocks(b))
1919 break;
1920
1921 if (bkey_cmp(k, &b->key) <= 0) {
Kent Overstreet3a3b6a42013-07-24 16:46:42 -07001922 if (!b->level)
1923 bkey_put(b->c, k);
Kent Overstreet26c949f2013-09-10 18:41:15 -07001924
Kent Overstreet1b207d82013-09-10 18:52:54 -07001925 ret |= btree_insert_key(b, op, k, replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07001926 bch_keylist_pop_front(insert_keys);
1927 } else if (bkey_cmp(&START_KEY(k), &b->key) < 0) {
Kent Overstreet26c949f2013-09-10 18:41:15 -07001928 BKEY_PADDED(key) temp;
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07001929 bkey_copy(&temp.key, insert_keys->keys);
Kent Overstreet26c949f2013-09-10 18:41:15 -07001930
1931 bch_cut_back(&b->key, &temp.key);
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07001932 bch_cut_front(&b->key, insert_keys->keys);
Kent Overstreet26c949f2013-09-10 18:41:15 -07001933
Kent Overstreet1b207d82013-09-10 18:52:54 -07001934 ret |= btree_insert_key(b, op, &temp.key, replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07001935 break;
1936 } else {
1937 break;
1938 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001939 }
1940
Kent Overstreet403b6cd2013-07-24 17:22:44 -07001941 BUG_ON(!bch_keylist_empty(insert_keys) && b->level);
1942
Kent Overstreetcafe5632013-03-23 16:11:31 -07001943 BUG_ON(bch_count_data(b) < oldsize);
1944 return ret;
1945}
1946
Kent Overstreet26c949f2013-09-10 18:41:15 -07001947static int btree_split(struct btree *b, struct btree_op *op,
1948 struct keylist *insert_keys,
Kent Overstreet1b207d82013-09-10 18:52:54 -07001949 struct keylist *parent_keys,
1950 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001951{
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07001952 bool split;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001953 struct btree *n1, *n2 = NULL, *n3 = NULL;
1954 uint64_t start_time = local_clock();
Kent Overstreetb54d6932013-07-24 18:04:18 -07001955 struct closure cl;
1956
1957 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001958
Kent Overstreet35fcd842013-07-24 17:29:09 -07001959 n1 = btree_node_alloc_replacement(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001960 if (IS_ERR(n1))
1961 goto err;
1962
1963 split = set_blocks(n1->sets[0].data, n1->c) > (btree_blocks(b) * 4) / 5;
1964
Kent Overstreetcafe5632013-03-23 16:11:31 -07001965 if (split) {
1966 unsigned keys = 0;
1967
Kent Overstreetc37511b2013-04-26 15:39:55 -07001968 trace_bcache_btree_node_split(b, n1->sets[0].data->keys);
1969
Kent Overstreet35fcd842013-07-24 17:29:09 -07001970 n2 = bch_btree_node_alloc(b->c, b->level);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001971 if (IS_ERR(n2))
1972 goto err_free1;
1973
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07001974 if (!b->parent) {
Kent Overstreet35fcd842013-07-24 17:29:09 -07001975 n3 = bch_btree_node_alloc(b->c, b->level + 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001976 if (IS_ERR(n3))
1977 goto err_free2;
1978 }
1979
Kent Overstreet1b207d82013-09-10 18:52:54 -07001980 bch_btree_insert_keys(n1, op, insert_keys, replace_key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001981
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07001982 /*
1983 * Has to be a linear search because we don't have an auxiliary
Kent Overstreetcafe5632013-03-23 16:11:31 -07001984 * search tree yet
1985 */
1986
1987 while (keys < (n1->sets[0].data->keys * 3) / 5)
1988 keys += bkey_u64s(node(n1->sets[0].data, keys));
1989
1990 bkey_copy_key(&n1->key, node(n1->sets[0].data, keys));
1991 keys += bkey_u64s(node(n1->sets[0].data, keys));
1992
1993 n2->sets[0].data->keys = n1->sets[0].data->keys - keys;
1994 n1->sets[0].data->keys = keys;
1995
1996 memcpy(n2->sets[0].data->start,
1997 end(n1->sets[0].data),
1998 n2->sets[0].data->keys * sizeof(uint64_t));
1999
2000 bkey_copy_key(&n2->key, &b->key);
2001
Kent Overstreet26c949f2013-09-10 18:41:15 -07002002 bch_keylist_add(parent_keys, &n2->key);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002003 bch_btree_node_write(n2, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002004 rw_unlock(true, n2);
Kent Overstreetc37511b2013-04-26 15:39:55 -07002005 } else {
2006 trace_bcache_btree_node_compact(b, n1->sets[0].data->keys);
2007
Kent Overstreet1b207d82013-09-10 18:52:54 -07002008 bch_btree_insert_keys(n1, op, insert_keys, replace_key);
Kent Overstreetc37511b2013-04-26 15:39:55 -07002009 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07002010
Kent Overstreet26c949f2013-09-10 18:41:15 -07002011 bch_keylist_add(parent_keys, &n1->key);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002012 bch_btree_node_write(n1, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002013
2014 if (n3) {
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002015 /* Depth increases, make a new root */
2016
Kent Overstreetcafe5632013-03-23 16:11:31 -07002017 bkey_copy_key(&n3->key, &MAX_KEY);
Kent Overstreet1b207d82013-09-10 18:52:54 -07002018 bch_btree_insert_keys(n3, op, parent_keys, NULL);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002019 bch_btree_node_write(n3, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002020
Kent Overstreetb54d6932013-07-24 18:04:18 -07002021 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002022 bch_btree_set_root(n3);
2023 rw_unlock(true, n3);
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002024 } else if (!b->parent) {
2025 /* Root filled up but didn't need to be split */
2026
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07002027 bch_keylist_reset(parent_keys);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002028 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002029 bch_btree_set_root(n1);
2030 } else {
2031 unsigned i;
2032
Kent Overstreet26c949f2013-09-10 18:41:15 -07002033 bkey_copy(parent_keys->top, &b->key);
2034 bkey_copy_key(parent_keys->top, &ZERO_KEY);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002035
2036 for (i = 0; i < KEY_PTRS(&b->key); i++) {
2037 uint8_t g = PTR_BUCKET(b->c, &b->key, i)->gen + 1;
2038
Kent Overstreet26c949f2013-09-10 18:41:15 -07002039 SET_PTR_GEN(parent_keys->top, i, g);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002040 }
2041
Kent Overstreet26c949f2013-09-10 18:41:15 -07002042 bch_keylist_push(parent_keys);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002043 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002044 atomic_inc(&b->c->prio_blocked);
2045 }
2046
2047 rw_unlock(true, n1);
Kent Overstreete8e1d462013-07-24 17:27:07 -07002048 btree_node_free(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002049
Kent Overstreet169ef1c2013-03-28 12:50:55 -06002050 bch_time_stats_update(&b->c->btree_split_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002051
2052 return 0;
2053err_free2:
Kent Overstreete8e1d462013-07-24 17:27:07 -07002054 btree_node_free(n2);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002055 rw_unlock(true, n2);
2056err_free1:
Kent Overstreete8e1d462013-07-24 17:27:07 -07002057 btree_node_free(n1);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002058 rw_unlock(true, n1);
2059err:
2060 if (n3 == ERR_PTR(-EAGAIN) ||
2061 n2 == ERR_PTR(-EAGAIN) ||
2062 n1 == ERR_PTR(-EAGAIN))
2063 return -EAGAIN;
2064
2065 pr_warn("couldn't split");
2066 return -ENOMEM;
2067}
2068
Kent Overstreet26c949f2013-09-10 18:41:15 -07002069static int bch_btree_insert_node(struct btree *b, struct btree_op *op,
Kent Overstreetc18536a2013-07-24 17:44:17 -07002070 struct keylist *insert_keys,
Kent Overstreet1b207d82013-09-10 18:52:54 -07002071 atomic_t *journal_ref,
2072 struct bkey *replace_key)
Kent Overstreet26c949f2013-09-10 18:41:15 -07002073{
2074 int ret = 0;
2075 struct keylist split_keys;
2076
2077 bch_keylist_init(&split_keys);
2078
2079 BUG_ON(b->level);
2080
2081 do {
Kent Overstreet1b207d82013-09-10 18:52:54 -07002082 BUG_ON(b->level && replace_key);
2083
Kent Overstreet26c949f2013-09-10 18:41:15 -07002084 if (should_split(b)) {
2085 if (current->bio_list) {
2086 op->lock = b->c->root->level + 1;
2087 ret = -EAGAIN;
2088 } else if (op->lock <= b->c->root->level) {
2089 op->lock = b->c->root->level + 1;
2090 ret = -EINTR;
2091 } else {
2092 struct btree *parent = b->parent;
2093
2094 ret = btree_split(b, op, insert_keys,
Kent Overstreet1b207d82013-09-10 18:52:54 -07002095 &split_keys, replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002096 insert_keys = &split_keys;
Kent Overstreet1b207d82013-09-10 18:52:54 -07002097 replace_key = NULL;
Kent Overstreet26c949f2013-09-10 18:41:15 -07002098 b = parent;
Kent Overstreet403b6cd2013-07-24 17:22:44 -07002099 if (!ret)
2100 ret = -EINTR;
Kent Overstreet26c949f2013-09-10 18:41:15 -07002101 }
2102 } else {
2103 BUG_ON(write_block(b) != b->sets[b->nsets].data);
2104
Kent Overstreet1b207d82013-09-10 18:52:54 -07002105 if (bch_btree_insert_keys(b, op, insert_keys,
2106 replace_key)) {
Kent Overstreetb54d6932013-07-24 18:04:18 -07002107 if (!b->level) {
Kent Overstreetc18536a2013-07-24 17:44:17 -07002108 bch_btree_leaf_dirty(b, journal_ref);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002109 } else {
2110 struct closure cl;
2111
2112 closure_init_stack(&cl);
2113 bch_btree_node_write(b, &cl);
2114 closure_sync(&cl);
2115 }
Kent Overstreet26c949f2013-09-10 18:41:15 -07002116 }
2117 }
2118 } while (!bch_keylist_empty(&split_keys));
2119
2120 return ret;
2121}
2122
Kent Overstreete7c590e2013-09-10 18:39:16 -07002123int bch_btree_insert_check_key(struct btree *b, struct btree_op *op,
2124 struct bkey *check_key)
2125{
2126 int ret = -EINTR;
2127 uint64_t btree_ptr = b->key.ptr[0];
2128 unsigned long seq = b->seq;
2129 struct keylist insert;
2130 bool upgrade = op->lock == -1;
2131
2132 bch_keylist_init(&insert);
2133
2134 if (upgrade) {
2135 rw_unlock(false, b);
2136 rw_lock(true, b, b->level);
2137
2138 if (b->key.ptr[0] != btree_ptr ||
2139 b->seq != seq + 1)
2140 goto out;
2141 }
2142
2143 SET_KEY_PTRS(check_key, 1);
2144 get_random_bytes(&check_key->ptr[0], sizeof(uint64_t));
2145
2146 SET_PTR_DEV(check_key, 0, PTR_CHECK_DEV);
2147
2148 bch_keylist_add(&insert, check_key);
2149
Kent Overstreet1b207d82013-09-10 18:52:54 -07002150 ret = bch_btree_insert_node(b, op, &insert, NULL, NULL);
Kent Overstreete7c590e2013-09-10 18:39:16 -07002151
2152 BUG_ON(!ret && !bch_keylist_empty(&insert));
2153out:
2154 if (upgrade)
2155 downgrade_write(&b->lock);
2156 return ret;
2157}
2158
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002159struct btree_insert_op {
2160 struct btree_op op;
2161 struct keylist *keys;
2162 atomic_t *journal_ref;
2163 struct bkey *replace_key;
2164};
2165
2166int btree_insert_fn(struct btree_op *b_op, struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002167{
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002168 struct btree_insert_op *op = container_of(b_op,
2169 struct btree_insert_op, op);
Kent Overstreet403b6cd2013-07-24 17:22:44 -07002170
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002171 int ret = bch_btree_insert_node(b, &op->op, op->keys,
2172 op->journal_ref, op->replace_key);
2173 if (ret && !bch_keylist_empty(op->keys))
2174 return ret;
2175 else
2176 return MAP_DONE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002177}
2178
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002179int bch_btree_insert(struct cache_set *c, struct keylist *keys,
2180 atomic_t *journal_ref, struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002181{
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002182 struct btree_insert_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002183 int ret = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002184
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002185 BUG_ON(current->bio_list);
Kent Overstreet4f3d4012013-09-10 18:46:36 -07002186 BUG_ON(bch_keylist_empty(keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002187
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002188 bch_btree_op_init(&op.op, 0);
2189 op.keys = keys;
2190 op.journal_ref = journal_ref;
2191 op.replace_key = replace_key;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002192
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002193 while (!ret && !bch_keylist_empty(keys)) {
2194 op.op.lock = 0;
2195 ret = bch_btree_map_leaf_nodes(&op.op, c,
2196 &START_KEY(keys->keys),
2197 btree_insert_fn);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002198 }
2199
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002200 if (ret) {
2201 struct bkey *k;
2202
2203 pr_err("error %i", ret);
2204
2205 while ((k = bch_keylist_pop(keys)))
Kent Overstreet3a3b6a42013-07-24 16:46:42 -07002206 bkey_put(c, k);
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002207 } else if (op.op.insert_collision)
2208 ret = -ESRCH;
Kent Overstreet6054c6d2013-07-24 18:06:22 -07002209
Kent Overstreetcafe5632013-03-23 16:11:31 -07002210 return ret;
2211}
2212
2213void bch_btree_set_root(struct btree *b)
2214{
2215 unsigned i;
Kent Overstreete49c7c32013-06-26 17:25:38 -07002216 struct closure cl;
2217
2218 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002219
Kent Overstreetc37511b2013-04-26 15:39:55 -07002220 trace_bcache_btree_set_root(b);
2221
Kent Overstreetcafe5632013-03-23 16:11:31 -07002222 BUG_ON(!b->written);
2223
2224 for (i = 0; i < KEY_PTRS(&b->key); i++)
2225 BUG_ON(PTR_BUCKET(b->c, &b->key, i)->prio != BTREE_PRIO);
2226
2227 mutex_lock(&b->c->bucket_lock);
2228 list_del_init(&b->list);
2229 mutex_unlock(&b->c->bucket_lock);
2230
2231 b->c->root = b;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002232
Kent Overstreete49c7c32013-06-26 17:25:38 -07002233 bch_journal_meta(b->c, &cl);
2234 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002235}
2236
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002237/* Map across nodes or keys */
2238
2239static int bch_btree_map_nodes_recurse(struct btree *b, struct btree_op *op,
2240 struct bkey *from,
2241 btree_map_nodes_fn *fn, int flags)
2242{
2243 int ret = MAP_CONTINUE;
2244
2245 if (b->level) {
2246 struct bkey *k;
2247 struct btree_iter iter;
2248
2249 bch_btree_iter_init(b, &iter, from);
2250
2251 while ((k = bch_btree_iter_next_filter(&iter, b,
2252 bch_ptr_bad))) {
2253 ret = btree(map_nodes_recurse, k, b,
2254 op, from, fn, flags);
2255 from = NULL;
2256
2257 if (ret != MAP_CONTINUE)
2258 return ret;
2259 }
2260 }
2261
2262 if (!b->level || flags == MAP_ALL_NODES)
2263 ret = fn(op, b);
2264
2265 return ret;
2266}
2267
2268int __bch_btree_map_nodes(struct btree_op *op, struct cache_set *c,
2269 struct bkey *from, btree_map_nodes_fn *fn, int flags)
2270{
Kent Overstreetb54d6932013-07-24 18:04:18 -07002271 return btree_root(map_nodes_recurse, c, op, from, fn, flags);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002272}
2273
2274static int bch_btree_map_keys_recurse(struct btree *b, struct btree_op *op,
2275 struct bkey *from, btree_map_keys_fn *fn,
2276 int flags)
2277{
2278 int ret = MAP_CONTINUE;
2279 struct bkey *k;
2280 struct btree_iter iter;
2281
2282 bch_btree_iter_init(b, &iter, from);
2283
2284 while ((k = bch_btree_iter_next_filter(&iter, b, bch_ptr_bad))) {
2285 ret = !b->level
2286 ? fn(op, b, k)
2287 : btree(map_keys_recurse, k, b, op, from, fn, flags);
2288 from = NULL;
2289
2290 if (ret != MAP_CONTINUE)
2291 return ret;
2292 }
2293
2294 if (!b->level && (flags & MAP_END_KEY))
2295 ret = fn(op, b, &KEY(KEY_INODE(&b->key),
2296 KEY_OFFSET(&b->key), 0));
2297
2298 return ret;
2299}
2300
2301int bch_btree_map_keys(struct btree_op *op, struct cache_set *c,
2302 struct bkey *from, btree_map_keys_fn *fn, int flags)
2303{
Kent Overstreetb54d6932013-07-24 18:04:18 -07002304 return btree_root(map_keys_recurse, c, op, from, fn, flags);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002305}
2306
Kent Overstreetcafe5632013-03-23 16:11:31 -07002307/* Keybuf code */
2308
2309static inline int keybuf_cmp(struct keybuf_key *l, struct keybuf_key *r)
2310{
2311 /* Overlapping keys compare equal */
2312 if (bkey_cmp(&l->key, &START_KEY(&r->key)) <= 0)
2313 return -1;
2314 if (bkey_cmp(&START_KEY(&l->key), &r->key) >= 0)
2315 return 1;
2316 return 0;
2317}
2318
2319static inline int keybuf_nonoverlapping_cmp(struct keybuf_key *l,
2320 struct keybuf_key *r)
2321{
2322 return clamp_t(int64_t, bkey_cmp(&l->key, &r->key), -1, 1);
2323}
2324
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002325struct refill {
2326 struct btree_op op;
2327 struct keybuf *buf;
2328 struct bkey *end;
2329 keybuf_pred_fn *pred;
2330};
2331
2332static int refill_keybuf_fn(struct btree_op *op, struct btree *b,
2333 struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002334{
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002335 struct refill *refill = container_of(op, struct refill, op);
2336 struct keybuf *buf = refill->buf;
2337 int ret = MAP_CONTINUE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002338
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002339 if (bkey_cmp(k, refill->end) >= 0) {
2340 ret = MAP_DONE;
2341 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002342 }
2343
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002344 if (!KEY_SIZE(k)) /* end key */
2345 goto out;
2346
2347 if (refill->pred(buf, k)) {
2348 struct keybuf_key *w;
2349
2350 spin_lock(&buf->lock);
2351
2352 w = array_alloc(&buf->freelist);
2353 if (!w) {
2354 spin_unlock(&buf->lock);
2355 return MAP_DONE;
2356 }
2357
2358 w->private = NULL;
2359 bkey_copy(&w->key, k);
2360
2361 if (RB_INSERT(&buf->keys, w, node, keybuf_cmp))
2362 array_free(&buf->freelist, w);
2363
2364 if (array_freelist_empty(&buf->freelist))
2365 ret = MAP_DONE;
2366
2367 spin_unlock(&buf->lock);
2368 }
2369out:
2370 buf->last_scanned = *k;
2371 return ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002372}
2373
2374void bch_refill_keybuf(struct cache_set *c, struct keybuf *buf,
Kent Overstreet72c27062013-06-05 06:24:39 -07002375 struct bkey *end, keybuf_pred_fn *pred)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002376{
2377 struct bkey start = buf->last_scanned;
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002378 struct refill refill;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002379
2380 cond_resched();
2381
Kent Overstreetb54d6932013-07-24 18:04:18 -07002382 bch_btree_op_init(&refill.op, -1);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002383 refill.buf = buf;
2384 refill.end = end;
2385 refill.pred = pred;
2386
2387 bch_btree_map_keys(&refill.op, c, &buf->last_scanned,
2388 refill_keybuf_fn, MAP_END_KEY);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002389
2390 pr_debug("found %s keys from %llu:%llu to %llu:%llu",
2391 RB_EMPTY_ROOT(&buf->keys) ? "no" :
2392 array_freelist_empty(&buf->freelist) ? "some" : "a few",
2393 KEY_INODE(&start), KEY_OFFSET(&start),
2394 KEY_INODE(&buf->last_scanned), KEY_OFFSET(&buf->last_scanned));
2395
2396 spin_lock(&buf->lock);
2397
2398 if (!RB_EMPTY_ROOT(&buf->keys)) {
2399 struct keybuf_key *w;
2400 w = RB_FIRST(&buf->keys, struct keybuf_key, node);
2401 buf->start = START_KEY(&w->key);
2402
2403 w = RB_LAST(&buf->keys, struct keybuf_key, node);
2404 buf->end = w->key;
2405 } else {
2406 buf->start = MAX_KEY;
2407 buf->end = MAX_KEY;
2408 }
2409
2410 spin_unlock(&buf->lock);
2411}
2412
2413static void __bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w)
2414{
2415 rb_erase(&w->node, &buf->keys);
2416 array_free(&buf->freelist, w);
2417}
2418
2419void bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w)
2420{
2421 spin_lock(&buf->lock);
2422 __bch_keybuf_del(buf, w);
2423 spin_unlock(&buf->lock);
2424}
2425
2426bool bch_keybuf_check_overlapping(struct keybuf *buf, struct bkey *start,
2427 struct bkey *end)
2428{
2429 bool ret = false;
2430 struct keybuf_key *p, *w, s;
2431 s.key = *start;
2432
2433 if (bkey_cmp(end, &buf->start) <= 0 ||
2434 bkey_cmp(start, &buf->end) >= 0)
2435 return false;
2436
2437 spin_lock(&buf->lock);
2438 w = RB_GREATER(&buf->keys, s, node, keybuf_nonoverlapping_cmp);
2439
2440 while (w && bkey_cmp(&START_KEY(&w->key), end) < 0) {
2441 p = w;
2442 w = RB_NEXT(w, node);
2443
2444 if (p->private)
2445 ret = true;
2446 else
2447 __bch_keybuf_del(buf, p);
2448 }
2449
2450 spin_unlock(&buf->lock);
2451 return ret;
2452}
2453
2454struct keybuf_key *bch_keybuf_next(struct keybuf *buf)
2455{
2456 struct keybuf_key *w;
2457 spin_lock(&buf->lock);
2458
2459 w = RB_FIRST(&buf->keys, struct keybuf_key, node);
2460
2461 while (w && w->private)
2462 w = RB_NEXT(w, node);
2463
2464 if (w)
2465 w->private = ERR_PTR(-EINTR);
2466
2467 spin_unlock(&buf->lock);
2468 return w;
2469}
2470
2471struct keybuf_key *bch_keybuf_next_rescan(struct cache_set *c,
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002472 struct keybuf *buf,
2473 struct bkey *end,
2474 keybuf_pred_fn *pred)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002475{
2476 struct keybuf_key *ret;
2477
2478 while (1) {
2479 ret = bch_keybuf_next(buf);
2480 if (ret)
2481 break;
2482
2483 if (bkey_cmp(&buf->last_scanned, end) >= 0) {
2484 pr_debug("scan finished");
2485 break;
2486 }
2487
Kent Overstreet72c27062013-06-05 06:24:39 -07002488 bch_refill_keybuf(c, buf, end, pred);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002489 }
2490
2491 return ret;
2492}
2493
Kent Overstreet72c27062013-06-05 06:24:39 -07002494void bch_keybuf_init(struct keybuf *buf)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002495{
Kent Overstreetcafe5632013-03-23 16:11:31 -07002496 buf->last_scanned = MAX_KEY;
2497 buf->keys = RB_ROOT;
2498
2499 spin_lock_init(&buf->lock);
2500 array_allocator_init(&buf->freelist);
2501}
2502
2503void bch_btree_exit(void)
2504{
2505 if (btree_io_wq)
2506 destroy_workqueue(btree_io_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002507}
2508
2509int __init bch_btree_init(void)
2510{
Kent Overstreet72a44512013-10-24 17:19:26 -07002511 btree_io_wq = create_singlethread_workqueue("bch_btree_io");
2512 if (!btree_io_wq)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002513 return -ENOMEM;
2514
2515 return 0;
2516}