blob: 8aaaf16637a09236ba95cfd617fadf40806293c7 [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); \
Kent Overstreet78365412013-12-17 01:29:34 -0800170 if (_r == -EINTR) \
171 schedule(); \
Kent Overstreetdf8e8972013-07-24 17:37:59 -0700172 bch_cannibalize_unlock(c); \
173 if (_r == -ENOSPC) { \
174 wait_event((c)->try_wait, \
175 !(c)->try_harder); \
176 _r = -EINTR; \
177 } \
178 } while (_r == -EINTR); \
179 \
Kent Overstreet78365412013-12-17 01:29:34 -0800180 finish_wait(&(c)->bucket_wait, &(op)->wait); \
Kent Overstreetdf8e8972013-07-24 17:37:59 -0700181 _r; \
182})
183
Kent Overstreetcafe5632013-03-23 16:11:31 -0700184/* Btree key manipulation */
185
Kent Overstreet3a3b6a42013-07-24 16:46:42 -0700186void bkey_put(struct cache_set *c, struct bkey *k)
Kent Overstreete7c590e2013-09-10 18:39:16 -0700187{
188 unsigned i;
189
190 for (i = 0; i < KEY_PTRS(k); i++)
191 if (ptr_available(c, k, i))
192 atomic_dec_bug(&PTR_BUCKET(c, k, i)->pin);
193}
194
Kent Overstreetcafe5632013-03-23 16:11:31 -0700195/* Btree IO */
196
197static uint64_t btree_csum_set(struct btree *b, struct bset *i)
198{
199 uint64_t crc = b->key.ptr[0];
Kent Overstreetfafff812013-12-17 21:56:21 -0800200 void *data = (void *) i + 8, *end = bset_bkey_last(i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700201
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600202 crc = bch_crc64_update(crc, data, end - data);
Kent Overstreetc19ed232013-03-26 13:49:02 -0700203 return crc ^ 0xffffffffffffffffULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700204}
205
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800206void bch_btree_node_read_done(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700207{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700208 const char *err = "bad btree header";
Kent Overstreet57943512013-04-25 13:58:35 -0700209 struct bset *i = b->sets[0].data;
210 struct btree_iter *iter;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700211
Kent Overstreet57943512013-04-25 13:58:35 -0700212 iter = mempool_alloc(b->c->fill_iter, GFP_NOWAIT);
213 iter->size = b->c->sb.bucket_size / b->c->sb.block_size;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700214 iter->used = 0;
215
Kent Overstreet280481d2013-10-24 16:36:03 -0700216#ifdef CONFIG_BCACHE_DEBUG
217 iter->b = b;
218#endif
219
Kent Overstreet57943512013-04-25 13:58:35 -0700220 if (!i->seq)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700221 goto err;
222
223 for (;
224 b->written < btree_blocks(b) && i->seq == b->sets[0].data->seq;
225 i = write_block(b)) {
226 err = "unsupported bset version";
227 if (i->version > BCACHE_BSET_VERSION)
228 goto err;
229
230 err = "bad btree header";
231 if (b->written + set_blocks(i, b->c) > btree_blocks(b))
232 goto err;
233
234 err = "bad magic";
Kent Overstreet81ab4192013-10-31 15:46:42 -0700235 if (i->magic != bset_magic(&b->c->sb))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700236 goto err;
237
238 err = "bad checksum";
239 switch (i->version) {
240 case 0:
241 if (i->csum != csum_set(i))
242 goto err;
243 break;
244 case BCACHE_BSET_VERSION:
245 if (i->csum != btree_csum_set(b, i))
246 goto err;
247 break;
248 }
249
250 err = "empty set";
251 if (i != b->sets[0].data && !i->keys)
252 goto err;
253
Kent Overstreetfafff812013-12-17 21:56:21 -0800254 bch_btree_iter_push(iter, i->start, bset_bkey_last(i));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700255
256 b->written += set_blocks(i, b->c);
257 }
258
259 err = "corrupted btree";
260 for (i = write_block(b);
Kent Overstreet88b9f8c2013-12-17 21:46:35 -0800261 bset_sector_offset(b, i) < KEY_SIZE(&b->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700262 i = ((void *) i) + block_bytes(b->c))
263 if (i->seq == b->sets[0].data->seq)
264 goto err;
265
266 bch_btree_sort_and_fix_extents(b, iter);
267
268 i = b->sets[0].data;
269 err = "short btree key";
270 if (b->sets[0].size &&
271 bkey_cmp(&b->key, &b->sets[0].end) < 0)
272 goto err;
273
274 if (b->written < btree_blocks(b))
275 bch_bset_init_next(b);
276out:
Kent Overstreet57943512013-04-25 13:58:35 -0700277 mempool_free(iter, b->c->fill_iter);
278 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700279err:
280 set_btree_node_io_error(b);
Kent Overstreet88b9f8c2013-12-17 21:46:35 -0800281 bch_cache_set_error(b->c, "%s at bucket %zu, block %u, %u keys",
Kent Overstreetcafe5632013-03-23 16:11:31 -0700282 err, PTR_BUCKET_NR(b->c, &b->key, 0),
Kent Overstreet88b9f8c2013-12-17 21:46:35 -0800283 bset_block_offset(b, i), i->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700284 goto out;
285}
286
Kent Overstreet57943512013-04-25 13:58:35 -0700287static void btree_node_read_endio(struct bio *bio, int error)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700288{
Kent Overstreet57943512013-04-25 13:58:35 -0700289 struct closure *cl = bio->bi_private;
290 closure_put(cl);
291}
Kent Overstreetcafe5632013-03-23 16:11:31 -0700292
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800293static void bch_btree_node_read(struct btree *b)
Kent Overstreet57943512013-04-25 13:58:35 -0700294{
295 uint64_t start_time = local_clock();
296 struct closure cl;
297 struct bio *bio;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700298
Kent Overstreetc37511b2013-04-26 15:39:55 -0700299 trace_bcache_btree_read(b);
300
Kent Overstreet57943512013-04-25 13:58:35 -0700301 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700302
Kent Overstreet57943512013-04-25 13:58:35 -0700303 bio = bch_bbio_alloc(b->c);
304 bio->bi_rw = REQ_META|READ_SYNC;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700305 bio->bi_iter.bi_size = KEY_SIZE(&b->key) << 9;
Kent Overstreet57943512013-04-25 13:58:35 -0700306 bio->bi_end_io = btree_node_read_endio;
307 bio->bi_private = &cl;
308
309 bch_bio_map(bio, b->sets[0].data);
310
Kent Overstreet57943512013-04-25 13:58:35 -0700311 bch_submit_bbio(bio, b->c, &b->key, 0);
312 closure_sync(&cl);
313
314 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
315 set_btree_node_io_error(b);
316
317 bch_bbio_free(bio, b->c);
318
319 if (btree_node_io_error(b))
320 goto err;
321
322 bch_btree_node_read_done(b);
Kent Overstreet57943512013-04-25 13:58:35 -0700323 bch_time_stats_update(&b->c->btree_read_time, start_time);
Kent Overstreet57943512013-04-25 13:58:35 -0700324
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 Overstreetcb7a5832013-12-16 15:27:25 -0800346static void btree_node_write_unlock(struct closure *cl)
347{
348 struct btree *b = container_of(cl, struct btree, io);
349
350 up(&b->io_mutex);
351}
352
Kent Overstreet57943512013-04-25 13:58:35 -0700353static void __btree_node_write_done(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700354{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800355 struct btree *b = container_of(cl, struct btree, io);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700356 struct btree_write *w = btree_prev_write(b);
357
358 bch_bbio_free(b->bio, b->c);
359 b->bio = NULL;
360 btree_complete_write(b, w);
361
362 if (btree_node_dirty(b))
363 queue_delayed_work(btree_io_wq, &b->work,
364 msecs_to_jiffies(30000));
365
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800366 closure_return_with_destructor(cl, btree_node_write_unlock);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700367}
368
Kent Overstreet57943512013-04-25 13:58:35 -0700369static void btree_node_write_done(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700370{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800371 struct btree *b = container_of(cl, struct btree, io);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700372 struct bio_vec *bv;
373 int n;
374
Kent Overstreet79886132013-11-23 17:19:00 -0800375 bio_for_each_segment_all(bv, b->bio, n)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700376 __free_page(bv->bv_page);
377
Kent Overstreet57943512013-04-25 13:58:35 -0700378 __btree_node_write_done(cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700379}
380
Kent Overstreet57943512013-04-25 13:58:35 -0700381static void btree_node_write_endio(struct bio *bio, int error)
382{
383 struct closure *cl = bio->bi_private;
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800384 struct btree *b = container_of(cl, struct btree, io);
Kent Overstreet57943512013-04-25 13:58:35 -0700385
386 if (error)
387 set_btree_node_io_error(b);
388
389 bch_bbio_count_io_errors(b->c, bio, error, "writing btree");
390 closure_put(cl);
391}
392
393static void do_btree_node_write(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700394{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800395 struct closure *cl = &b->io;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700396 struct bset *i = b->sets[b->nsets].data;
397 BKEY_PADDED(key) k;
398
399 i->version = BCACHE_BSET_VERSION;
400 i->csum = btree_csum_set(b, i);
401
Kent Overstreet57943512013-04-25 13:58:35 -0700402 BUG_ON(b->bio);
403 b->bio = bch_bbio_alloc(b->c);
404
405 b->bio->bi_end_io = btree_node_write_endio;
Kent Overstreetfaadf0c2013-11-01 18:03:08 -0700406 b->bio->bi_private = cl;
Kent Overstreete49c7c32013-06-26 17:25:38 -0700407 b->bio->bi_rw = REQ_META|WRITE_SYNC|REQ_FUA;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700408 b->bio->bi_iter.bi_size = set_blocks(i, b->c) * block_bytes(b->c);
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600409 bch_bio_map(b->bio, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700410
Kent Overstreete49c7c32013-06-26 17:25:38 -0700411 /*
412 * If we're appending to a leaf node, we don't technically need FUA -
413 * this write just needs to be persisted before the next journal write,
414 * which will be marked FLUSH|FUA.
415 *
416 * Similarly if we're writing a new btree root - the pointer is going to
417 * be in the next journal entry.
418 *
419 * But if we're writing a new btree node (that isn't a root) or
420 * appending to a non leaf btree node, we need either FUA or a flush
421 * when we write the parent with the new pointer. FUA is cheaper than a
422 * flush, and writes appending to leaf nodes aren't blocking anything so
423 * just make all btree node writes FUA to keep things sane.
424 */
425
Kent Overstreetcafe5632013-03-23 16:11:31 -0700426 bkey_copy(&k.key, &b->key);
427 SET_PTR_OFFSET(&k.key, 0, PTR_OFFSET(&k.key, 0) + bset_offset(b, i));
428
Kent Overstreet8e51e412013-06-06 18:15:57 -0700429 if (!bio_alloc_pages(b->bio, GFP_NOIO)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700430 int j;
431 struct bio_vec *bv;
432 void *base = (void *) ((unsigned long) i & ~(PAGE_SIZE - 1));
433
Kent Overstreet79886132013-11-23 17:19:00 -0800434 bio_for_each_segment_all(bv, b->bio, j)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700435 memcpy(page_address(bv->bv_page),
436 base + j * PAGE_SIZE, PAGE_SIZE);
437
Kent Overstreetcafe5632013-03-23 16:11:31 -0700438 bch_submit_bbio(b->bio, b->c, &k.key, 0);
439
Kent Overstreet57943512013-04-25 13:58:35 -0700440 continue_at(cl, btree_node_write_done, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700441 } else {
442 b->bio->bi_vcnt = 0;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600443 bch_bio_map(b->bio, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700444
Kent Overstreetcafe5632013-03-23 16:11:31 -0700445 bch_submit_bbio(b->bio, b->c, &k.key, 0);
446
447 closure_sync(cl);
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800448 continue_at_nobarrier(cl, __btree_node_write_done, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700449 }
450}
451
Kent Overstreet57943512013-04-25 13:58:35 -0700452void bch_btree_node_write(struct btree *b, struct closure *parent)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700453{
454 struct bset *i = b->sets[b->nsets].data;
455
Kent Overstreetc37511b2013-04-26 15:39:55 -0700456 trace_bcache_btree_write(b);
457
Kent Overstreetcafe5632013-03-23 16:11:31 -0700458 BUG_ON(current->bio_list);
Kent Overstreet57943512013-04-25 13:58:35 -0700459 BUG_ON(b->written >= btree_blocks(b));
460 BUG_ON(b->written && !i->keys);
461 BUG_ON(b->sets->data->seq != i->seq);
Kent Overstreet280481d2013-10-24 16:36:03 -0700462 bch_check_keys(b, "writing");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700463
Kent Overstreetcafe5632013-03-23 16:11:31 -0700464 cancel_delayed_work(&b->work);
465
Kent Overstreet57943512013-04-25 13:58:35 -0700466 /* If caller isn't waiting for write, parent refcount is cache set */
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800467 down(&b->io_mutex);
468 closure_init(&b->io, parent ?: &b->c->cl);
Kent Overstreet57943512013-04-25 13:58:35 -0700469
Kent Overstreetcafe5632013-03-23 16:11:31 -0700470 clear_bit(BTREE_NODE_dirty, &b->flags);
471 change_bit(BTREE_NODE_write_idx, &b->flags);
472
Kent Overstreet57943512013-04-25 13:58:35 -0700473 do_btree_node_write(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700474
Kent Overstreetcafe5632013-03-23 16:11:31 -0700475 b->written += set_blocks(i, b->c);
476 atomic_long_add(set_blocks(i, b->c) * b->c->sb.block_size,
477 &PTR_CACHE(b->c, &b->key, 0)->btree_sectors_written);
478
479 bch_btree_sort_lazy(b);
480
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800481 /*
482 * do verify if there was more than one set initially (i.e. we did a
483 * sort) and we sorted down to a single set:
484 */
485 if (i != b->sets->data && !b->nsets)
486 bch_btree_verify(b);
487
Kent Overstreetcafe5632013-03-23 16:11:31 -0700488 if (b->written < btree_blocks(b))
489 bch_bset_init_next(b);
490}
491
Kent Overstreetf269af52013-07-23 20:48:29 -0700492static void bch_btree_node_write_sync(struct btree *b)
493{
494 struct closure cl;
495
496 closure_init_stack(&cl);
497 bch_btree_node_write(b, &cl);
498 closure_sync(&cl);
499}
500
Kent Overstreet57943512013-04-25 13:58:35 -0700501static void btree_node_write_work(struct work_struct *w)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700502{
503 struct btree *b = container_of(to_delayed_work(w), struct btree, work);
504
Kent Overstreet57943512013-04-25 13:58:35 -0700505 rw_lock(true, b, b->level);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700506
507 if (btree_node_dirty(b))
Kent Overstreet57943512013-04-25 13:58:35 -0700508 bch_btree_node_write(b, NULL);
509 rw_unlock(true, b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700510}
511
Kent Overstreetc18536a2013-07-24 17:44:17 -0700512static void bch_btree_leaf_dirty(struct btree *b, atomic_t *journal_ref)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700513{
514 struct bset *i = b->sets[b->nsets].data;
515 struct btree_write *w = btree_current_write(b);
516
Kent Overstreet57943512013-04-25 13:58:35 -0700517 BUG_ON(!b->written);
518 BUG_ON(!i->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700519
Kent Overstreet57943512013-04-25 13:58:35 -0700520 if (!btree_node_dirty(b))
521 queue_delayed_work(btree_io_wq, &b->work, 30 * HZ);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700522
Kent Overstreet57943512013-04-25 13:58:35 -0700523 set_btree_node_dirty(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700524
Kent Overstreetc18536a2013-07-24 17:44:17 -0700525 if (journal_ref) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700526 if (w->journal &&
Kent Overstreetc18536a2013-07-24 17:44:17 -0700527 journal_pin_cmp(b->c, w->journal, journal_ref)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700528 atomic_dec_bug(w->journal);
529 w->journal = NULL;
530 }
531
532 if (!w->journal) {
Kent Overstreetc18536a2013-07-24 17:44:17 -0700533 w->journal = journal_ref;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700534 atomic_inc(w->journal);
535 }
536 }
537
Kent Overstreetcafe5632013-03-23 16:11:31 -0700538 /* Force write if set is too big */
Kent Overstreet57943512013-04-25 13:58:35 -0700539 if (set_bytes(i) > PAGE_SIZE - 48 &&
540 !current->bio_list)
541 bch_btree_node_write(b, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700542}
543
544/*
545 * Btree in memory cache - allocation/freeing
546 * mca -> memory cache
547 */
548
549static void mca_reinit(struct btree *b)
550{
551 unsigned i;
552
553 b->flags = 0;
554 b->written = 0;
555 b->nsets = 0;
556
557 for (i = 0; i < MAX_BSETS; i++)
558 b->sets[i].size = 0;
559 /*
560 * Second loop starts at 1 because b->sets[0]->data is the memory we
561 * allocated
562 */
563 for (i = 1; i < MAX_BSETS; i++)
564 b->sets[i].data = NULL;
565}
566
567#define mca_reserve(c) (((c->root && c->root->level) \
568 ? c->root->level : 1) * 8 + 16)
569#define mca_can_free(c) \
570 max_t(int, 0, c->bucket_cache_used - mca_reserve(c))
571
572static void mca_data_free(struct btree *b)
573{
574 struct bset_tree *t = b->sets;
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800575
576 BUG_ON(b->io_mutex.count != 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700577
578 if (bset_prev_bytes(b) < PAGE_SIZE)
579 kfree(t->prev);
580 else
581 free_pages((unsigned long) t->prev,
582 get_order(bset_prev_bytes(b)));
583
584 if (bset_tree_bytes(b) < PAGE_SIZE)
585 kfree(t->tree);
586 else
587 free_pages((unsigned long) t->tree,
588 get_order(bset_tree_bytes(b)));
589
590 free_pages((unsigned long) t->data, b->page_order);
591
592 t->prev = NULL;
593 t->tree = NULL;
594 t->data = NULL;
595 list_move(&b->list, &b->c->btree_cache_freed);
596 b->c->bucket_cache_used--;
597}
598
599static void mca_bucket_free(struct btree *b)
600{
601 BUG_ON(btree_node_dirty(b));
602
603 b->key.ptr[0] = 0;
604 hlist_del_init_rcu(&b->hash);
605 list_move(&b->list, &b->c->btree_cache_freeable);
606}
607
608static unsigned btree_order(struct bkey *k)
609{
610 return ilog2(KEY_SIZE(k) / PAGE_SECTORS ?: 1);
611}
612
613static void mca_data_alloc(struct btree *b, struct bkey *k, gfp_t gfp)
614{
615 struct bset_tree *t = b->sets;
616 BUG_ON(t->data);
617
618 b->page_order = max_t(unsigned,
619 ilog2(b->c->btree_pages),
620 btree_order(k));
621
622 t->data = (void *) __get_free_pages(gfp, b->page_order);
623 if (!t->data)
624 goto err;
625
626 t->tree = bset_tree_bytes(b) < PAGE_SIZE
627 ? kmalloc(bset_tree_bytes(b), gfp)
628 : (void *) __get_free_pages(gfp, get_order(bset_tree_bytes(b)));
629 if (!t->tree)
630 goto err;
631
632 t->prev = bset_prev_bytes(b) < PAGE_SIZE
633 ? kmalloc(bset_prev_bytes(b), gfp)
634 : (void *) __get_free_pages(gfp, get_order(bset_prev_bytes(b)));
635 if (!t->prev)
636 goto err;
637
638 list_move(&b->list, &b->c->btree_cache);
639 b->c->bucket_cache_used++;
640 return;
641err:
642 mca_data_free(b);
643}
644
645static struct btree *mca_bucket_alloc(struct cache_set *c,
646 struct bkey *k, gfp_t gfp)
647{
648 struct btree *b = kzalloc(sizeof(struct btree), gfp);
649 if (!b)
650 return NULL;
651
652 init_rwsem(&b->lock);
653 lockdep_set_novalidate_class(&b->lock);
654 INIT_LIST_HEAD(&b->list);
Kent Overstreet57943512013-04-25 13:58:35 -0700655 INIT_DELAYED_WORK(&b->work, btree_node_write_work);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700656 b->c = c;
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800657 sema_init(&b->io_mutex, 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700658
659 mca_data_alloc(b, k, gfp);
660 return b;
661}
662
Kent Overstreete8e1d462013-07-24 17:27:07 -0700663static int mca_reap(struct btree *b, unsigned min_order, bool flush)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700664{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700665 struct closure cl;
666
667 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700668 lockdep_assert_held(&b->c->bucket_lock);
669
670 if (!down_write_trylock(&b->lock))
671 return -ENOMEM;
672
Kent Overstreete8e1d462013-07-24 17:27:07 -0700673 BUG_ON(btree_node_dirty(b) && !b->sets[0].data);
674
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800675 if (b->page_order < min_order)
676 goto out_unlock;
677
678 if (!flush) {
679 if (btree_node_dirty(b))
680 goto out_unlock;
681
682 if (down_trylock(&b->io_mutex))
683 goto out_unlock;
684 up(&b->io_mutex);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700685 }
686
Kent Overstreetf269af52013-07-23 20:48:29 -0700687 if (btree_node_dirty(b))
688 bch_btree_node_write_sync(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700689
Kent Overstreete8e1d462013-07-24 17:27:07 -0700690 /* wait for any in flight btree write */
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800691 down(&b->io_mutex);
692 up(&b->io_mutex);
Kent Overstreete8e1d462013-07-24 17:27:07 -0700693
Kent Overstreetcafe5632013-03-23 16:11:31 -0700694 return 0;
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800695out_unlock:
696 rw_unlock(true, b);
697 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700698}
699
Dave Chinner7dc19d52013-08-28 10:18:11 +1000700static unsigned long bch_mca_scan(struct shrinker *shrink,
701 struct shrink_control *sc)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700702{
703 struct cache_set *c = container_of(shrink, struct cache_set, shrink);
704 struct btree *b, *t;
705 unsigned long i, nr = sc->nr_to_scan;
Dave Chinner7dc19d52013-08-28 10:18:11 +1000706 unsigned long freed = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700707
708 if (c->shrinker_disabled)
Dave Chinner7dc19d52013-08-28 10:18:11 +1000709 return SHRINK_STOP;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700710
711 if (c->try_harder)
Dave Chinner7dc19d52013-08-28 10:18:11 +1000712 return SHRINK_STOP;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700713
714 /* Return -1 if we can't do anything right now */
Kent Overstreeta698e082013-09-23 23:17:34 -0700715 if (sc->gfp_mask & __GFP_IO)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700716 mutex_lock(&c->bucket_lock);
717 else if (!mutex_trylock(&c->bucket_lock))
718 return -1;
719
Kent Overstreet36c9ea92013-06-03 13:04:56 -0700720 /*
721 * It's _really_ critical that we don't free too many btree nodes - we
722 * have to always leave ourselves a reserve. The reserve is how we
723 * guarantee that allocating memory for a new btree node can always
724 * succeed, so that inserting keys into the btree can always succeed and
725 * IO can always make forward progress:
726 */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700727 nr /= c->btree_pages;
728 nr = min_t(unsigned long, nr, mca_can_free(c));
729
730 i = 0;
731 list_for_each_entry_safe(b, t, &c->btree_cache_freeable, list) {
Dave Chinner7dc19d52013-08-28 10:18:11 +1000732 if (freed >= nr)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700733 break;
734
735 if (++i > 3 &&
Kent Overstreete8e1d462013-07-24 17:27:07 -0700736 !mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700737 mca_data_free(b);
738 rw_unlock(true, b);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000739 freed++;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700740 }
741 }
742
Dave Chinner7dc19d52013-08-28 10:18:11 +1000743 for (i = 0; (nr--) && i < c->bucket_cache_used; i++) {
Kent Overstreetb0f32a52013-12-10 13:24:26 -0800744 if (list_empty(&c->btree_cache))
745 goto out;
746
Kent Overstreetcafe5632013-03-23 16:11:31 -0700747 b = list_first_entry(&c->btree_cache, struct btree, list);
748 list_rotate_left(&c->btree_cache);
749
750 if (!b->accessed &&
Kent Overstreete8e1d462013-07-24 17:27:07 -0700751 !mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700752 mca_bucket_free(b);
753 mca_data_free(b);
754 rw_unlock(true, b);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000755 freed++;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700756 } else
757 b->accessed = 0;
758 }
759out:
Kent Overstreetcafe5632013-03-23 16:11:31 -0700760 mutex_unlock(&c->bucket_lock);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000761 return freed;
762}
763
764static unsigned long bch_mca_count(struct shrinker *shrink,
765 struct shrink_control *sc)
766{
767 struct cache_set *c = container_of(shrink, struct cache_set, shrink);
768
769 if (c->shrinker_disabled)
770 return 0;
771
772 if (c->try_harder)
773 return 0;
774
775 return mca_can_free(c) * c->btree_pages;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700776}
777
778void bch_btree_cache_free(struct cache_set *c)
779{
780 struct btree *b;
781 struct closure cl;
782 closure_init_stack(&cl);
783
784 if (c->shrink.list.next)
785 unregister_shrinker(&c->shrink);
786
787 mutex_lock(&c->bucket_lock);
788
789#ifdef CONFIG_BCACHE_DEBUG
790 if (c->verify_data)
791 list_move(&c->verify_data->list, &c->btree_cache);
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800792
793 free_pages((unsigned long) c->verify_ondisk, ilog2(bucket_pages(c)));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700794#endif
795
796 list_splice(&c->btree_cache_freeable,
797 &c->btree_cache);
798
799 while (!list_empty(&c->btree_cache)) {
800 b = list_first_entry(&c->btree_cache, struct btree, list);
801
802 if (btree_node_dirty(b))
803 btree_complete_write(b, btree_current_write(b));
804 clear_bit(BTREE_NODE_dirty, &b->flags);
805
806 mca_data_free(b);
807 }
808
809 while (!list_empty(&c->btree_cache_freed)) {
810 b = list_first_entry(&c->btree_cache_freed,
811 struct btree, list);
812 list_del(&b->list);
813 cancel_delayed_work_sync(&b->work);
814 kfree(b);
815 }
816
817 mutex_unlock(&c->bucket_lock);
818}
819
820int bch_btree_cache_alloc(struct cache_set *c)
821{
822 unsigned i;
823
Kent Overstreetcafe5632013-03-23 16:11:31 -0700824 for (i = 0; i < mca_reserve(c); i++)
Kent Overstreet72a44512013-10-24 17:19:26 -0700825 if (!mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL))
826 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700827
828 list_splice_init(&c->btree_cache,
829 &c->btree_cache_freeable);
830
831#ifdef CONFIG_BCACHE_DEBUG
832 mutex_init(&c->verify_lock);
833
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800834 c->verify_ondisk = (void *)
835 __get_free_pages(GFP_KERNEL, ilog2(bucket_pages(c)));
836
Kent Overstreetcafe5632013-03-23 16:11:31 -0700837 c->verify_data = mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL);
838
839 if (c->verify_data &&
840 c->verify_data->sets[0].data)
841 list_del_init(&c->verify_data->list);
842 else
843 c->verify_data = NULL;
844#endif
845
Dave Chinner7dc19d52013-08-28 10:18:11 +1000846 c->shrink.count_objects = bch_mca_count;
847 c->shrink.scan_objects = bch_mca_scan;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700848 c->shrink.seeks = 4;
849 c->shrink.batch = c->btree_pages * 2;
850 register_shrinker(&c->shrink);
851
852 return 0;
853}
854
855/* Btree in memory cache - hash table */
856
857static struct hlist_head *mca_hash(struct cache_set *c, struct bkey *k)
858{
859 return &c->bucket_hash[hash_32(PTR_HASH(c, k), BUCKET_HASH_BITS)];
860}
861
862static struct btree *mca_find(struct cache_set *c, struct bkey *k)
863{
864 struct btree *b;
865
866 rcu_read_lock();
867 hlist_for_each_entry_rcu(b, mca_hash(c, k), hash)
868 if (PTR_HASH(c, &b->key) == PTR_HASH(c, k))
869 goto out;
870 b = NULL;
871out:
872 rcu_read_unlock();
873 return b;
874}
875
Kent Overstreete8e1d462013-07-24 17:27:07 -0700876static struct btree *mca_cannibalize(struct cache_set *c, struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700877{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700878 struct btree *b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700879
Kent Overstreetc37511b2013-04-26 15:39:55 -0700880 trace_bcache_btree_cache_cannibalize(c);
881
Kent Overstreete8e1d462013-07-24 17:27:07 -0700882 if (!c->try_harder) {
883 c->try_harder = current;
884 c->try_harder_start = local_clock();
885 } else if (c->try_harder != current)
886 return ERR_PTR(-ENOSPC);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700887
Kent Overstreete8e1d462013-07-24 17:27:07 -0700888 list_for_each_entry_reverse(b, &c->btree_cache, list)
889 if (!mca_reap(b, btree_order(k), false))
890 return b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700891
Kent Overstreete8e1d462013-07-24 17:27:07 -0700892 list_for_each_entry_reverse(b, &c->btree_cache, list)
893 if (!mca_reap(b, btree_order(k), true))
894 return b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700895
Kent Overstreete8e1d462013-07-24 17:27:07 -0700896 return ERR_PTR(-ENOMEM);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700897}
898
899/*
900 * We can only have one thread cannibalizing other cached btree nodes at a time,
901 * or we'll deadlock. We use an open coded mutex to ensure that, which a
902 * cannibalize_bucket() will take. This means every time we unlock the root of
903 * the btree, we need to release this lock if we have it held.
904 */
Kent Overstreetdf8e8972013-07-24 17:37:59 -0700905static void bch_cannibalize_unlock(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700906{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700907 if (c->try_harder == current) {
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600908 bch_time_stats_update(&c->try_harder_time, c->try_harder_start);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700909 c->try_harder = NULL;
Kent Overstreete8e1d462013-07-24 17:27:07 -0700910 wake_up(&c->try_wait);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700911 }
912}
913
Kent Overstreete8e1d462013-07-24 17:27:07 -0700914static struct btree *mca_alloc(struct cache_set *c, struct bkey *k, int level)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700915{
916 struct btree *b;
917
Kent Overstreete8e1d462013-07-24 17:27:07 -0700918 BUG_ON(current->bio_list);
919
Kent Overstreetcafe5632013-03-23 16:11:31 -0700920 lockdep_assert_held(&c->bucket_lock);
921
922 if (mca_find(c, k))
923 return NULL;
924
925 /* btree_free() doesn't free memory; it sticks the node on the end of
926 * the list. Check if there's any freed nodes there:
927 */
928 list_for_each_entry(b, &c->btree_cache_freeable, list)
Kent Overstreete8e1d462013-07-24 17:27:07 -0700929 if (!mca_reap(b, btree_order(k), false))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700930 goto out;
931
932 /* We never free struct btree itself, just the memory that holds the on
933 * disk node. Check the freed list before allocating a new one:
934 */
935 list_for_each_entry(b, &c->btree_cache_freed, list)
Kent Overstreete8e1d462013-07-24 17:27:07 -0700936 if (!mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700937 mca_data_alloc(b, k, __GFP_NOWARN|GFP_NOIO);
938 if (!b->sets[0].data)
939 goto err;
940 else
941 goto out;
942 }
943
944 b = mca_bucket_alloc(c, k, __GFP_NOWARN|GFP_NOIO);
945 if (!b)
946 goto err;
947
948 BUG_ON(!down_write_trylock(&b->lock));
949 if (!b->sets->data)
950 goto err;
951out:
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800952 BUG_ON(b->io_mutex.count != 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700953
954 bkey_copy(&b->key, k);
955 list_move(&b->list, &c->btree_cache);
956 hlist_del_init_rcu(&b->hash);
957 hlist_add_head_rcu(&b->hash, mca_hash(c, k));
958
959 lock_set_subclass(&b->lock.dep_map, level + 1, _THIS_IP_);
960 b->level = level;
Kent Overstreetd6fd3b12013-07-24 17:20:19 -0700961 b->parent = (void *) ~0UL;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700962
963 mca_reinit(b);
964
965 return b;
966err:
967 if (b)
968 rw_unlock(true, b);
969
Kent Overstreete8e1d462013-07-24 17:27:07 -0700970 b = mca_cannibalize(c, k);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700971 if (!IS_ERR(b))
972 goto out;
973
974 return b;
975}
976
977/**
978 * bch_btree_node_get - find a btree node in the cache and lock it, reading it
979 * in from disk if necessary.
980 *
Kent Overstreetb54d6932013-07-24 18:04:18 -0700981 * If IO is necessary and running under generic_make_request, returns -EAGAIN.
Kent Overstreetcafe5632013-03-23 16:11:31 -0700982 *
983 * The btree node will have either a read or a write lock held, depending on
984 * level and op->lock.
985 */
986struct btree *bch_btree_node_get(struct cache_set *c, struct bkey *k,
Kent Overstreete8e1d462013-07-24 17:27:07 -0700987 int level, bool write)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700988{
989 int i = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700990 struct btree *b;
991
992 BUG_ON(level < 0);
993retry:
994 b = mca_find(c, k);
995
996 if (!b) {
Kent Overstreet57943512013-04-25 13:58:35 -0700997 if (current->bio_list)
998 return ERR_PTR(-EAGAIN);
999
Kent Overstreetcafe5632013-03-23 16:11:31 -07001000 mutex_lock(&c->bucket_lock);
Kent Overstreete8e1d462013-07-24 17:27:07 -07001001 b = mca_alloc(c, k, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001002 mutex_unlock(&c->bucket_lock);
1003
1004 if (!b)
1005 goto retry;
1006 if (IS_ERR(b))
1007 return b;
1008
Kent Overstreet57943512013-04-25 13:58:35 -07001009 bch_btree_node_read(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001010
1011 if (!write)
1012 downgrade_write(&b->lock);
1013 } else {
1014 rw_lock(write, b, level);
1015 if (PTR_HASH(c, &b->key) != PTR_HASH(c, k)) {
1016 rw_unlock(write, b);
1017 goto retry;
1018 }
1019 BUG_ON(b->level != level);
1020 }
1021
1022 b->accessed = 1;
1023
1024 for (; i <= b->nsets && b->sets[i].size; i++) {
1025 prefetch(b->sets[i].tree);
1026 prefetch(b->sets[i].data);
1027 }
1028
1029 for (; i <= b->nsets; i++)
1030 prefetch(b->sets[i].data);
1031
Kent Overstreet57943512013-04-25 13:58:35 -07001032 if (btree_node_io_error(b)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001033 rw_unlock(write, b);
Kent Overstreet57943512013-04-25 13:58:35 -07001034 return ERR_PTR(-EIO);
1035 }
1036
1037 BUG_ON(!b->written);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001038
1039 return b;
1040}
1041
1042static void btree_node_prefetch(struct cache_set *c, struct bkey *k, int level)
1043{
1044 struct btree *b;
1045
1046 mutex_lock(&c->bucket_lock);
Kent Overstreete8e1d462013-07-24 17:27:07 -07001047 b = mca_alloc(c, k, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001048 mutex_unlock(&c->bucket_lock);
1049
1050 if (!IS_ERR_OR_NULL(b)) {
Kent Overstreet57943512013-04-25 13:58:35 -07001051 bch_btree_node_read(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001052 rw_unlock(true, b);
1053 }
1054}
1055
1056/* Btree alloc */
1057
Kent Overstreete8e1d462013-07-24 17:27:07 -07001058static void btree_node_free(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001059{
1060 unsigned i;
1061
Kent Overstreetc37511b2013-04-26 15:39:55 -07001062 trace_bcache_btree_node_free(b);
1063
Kent Overstreetcafe5632013-03-23 16:11:31 -07001064 BUG_ON(b == b->c->root);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001065
1066 if (btree_node_dirty(b))
1067 btree_complete_write(b, btree_current_write(b));
1068 clear_bit(BTREE_NODE_dirty, &b->flags);
1069
Kent Overstreetcafe5632013-03-23 16:11:31 -07001070 cancel_delayed_work(&b->work);
1071
1072 mutex_lock(&b->c->bucket_lock);
1073
1074 for (i = 0; i < KEY_PTRS(&b->key); i++) {
1075 BUG_ON(atomic_read(&PTR_BUCKET(b->c, &b->key, i)->pin));
1076
1077 bch_inc_gen(PTR_CACHE(b->c, &b->key, i),
1078 PTR_BUCKET(b->c, &b->key, i));
1079 }
1080
1081 bch_bucket_free(b->c, &b->key);
1082 mca_bucket_free(b);
1083 mutex_unlock(&b->c->bucket_lock);
1084}
1085
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001086struct btree *bch_btree_node_alloc(struct cache_set *c, int level, bool wait)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001087{
1088 BKEY_PADDED(key) k;
1089 struct btree *b = ERR_PTR(-EAGAIN);
1090
1091 mutex_lock(&c->bucket_lock);
1092retry:
Kent Overstreet78365412013-12-17 01:29:34 -08001093 if (__bch_bucket_alloc_set(c, RESERVE_BTREE, &k.key, 1, wait))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001094 goto err;
1095
Kent Overstreet3a3b6a42013-07-24 16:46:42 -07001096 bkey_put(c, &k.key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001097 SET_KEY_SIZE(&k.key, c->btree_pages * PAGE_SECTORS);
1098
Kent Overstreete8e1d462013-07-24 17:27:07 -07001099 b = mca_alloc(c, &k.key, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001100 if (IS_ERR(b))
1101 goto err_free;
1102
1103 if (!b) {
Kent Overstreetb1a67b02013-03-25 11:46:44 -07001104 cache_bug(c,
1105 "Tried to allocate bucket that was in btree cache");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001106 goto retry;
1107 }
1108
Kent Overstreetcafe5632013-03-23 16:11:31 -07001109 b->accessed = 1;
1110 bch_bset_init_next(b);
1111
1112 mutex_unlock(&c->bucket_lock);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001113
1114 trace_bcache_btree_node_alloc(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001115 return b;
1116err_free:
1117 bch_bucket_free(c, &k.key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001118err:
1119 mutex_unlock(&c->bucket_lock);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001120
1121 trace_bcache_btree_node_alloc_fail(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001122 return b;
1123}
1124
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001125static struct btree *btree_node_alloc_replacement(struct btree *b, bool wait)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001126{
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001127 struct btree *n = bch_btree_node_alloc(b->c, b->level, wait);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001128 if (!IS_ERR_OR_NULL(n))
1129 bch_btree_sort_into(b, n);
1130
1131 return n;
1132}
1133
Kent Overstreet8835c122013-07-24 23:18:05 -07001134static void make_btree_freeing_key(struct btree *b, struct bkey *k)
1135{
1136 unsigned i;
1137
1138 bkey_copy(k, &b->key);
1139 bkey_copy_key(k, &ZERO_KEY);
1140
1141 for (i = 0; i < KEY_PTRS(k); i++) {
1142 uint8_t g = PTR_BUCKET(b->c, k, i)->gen + 1;
1143
1144 SET_PTR_GEN(k, i, g);
1145 }
1146
1147 atomic_inc(&b->c->prio_blocked);
1148}
1149
Kent Overstreet78365412013-12-17 01:29:34 -08001150static int btree_check_reserve(struct btree *b, struct btree_op *op)
1151{
1152 struct cache_set *c = b->c;
1153 struct cache *ca;
1154 unsigned i, reserve = c->root->level * 2 + 1;
1155 int ret = 0;
1156
1157 mutex_lock(&c->bucket_lock);
1158
1159 for_each_cache(ca, c, i)
1160 if (fifo_used(&ca->free[RESERVE_BTREE]) < reserve) {
1161 if (op)
1162 prepare_to_wait(&c->bucket_wait, &op->wait,
1163 TASK_UNINTERRUPTIBLE);
1164 ret = -EINTR;
1165 break;
1166 }
1167
1168 mutex_unlock(&c->bucket_lock);
1169 return ret;
1170}
1171
Kent Overstreetcafe5632013-03-23 16:11:31 -07001172/* Garbage collection */
1173
1174uint8_t __bch_btree_mark_key(struct cache_set *c, int level, struct bkey *k)
1175{
1176 uint8_t stale = 0;
1177 unsigned i;
1178 struct bucket *g;
1179
1180 /*
1181 * ptr_invalid() can't return true for the keys that mark btree nodes as
1182 * freed, but since ptr_bad() returns true we'll never actually use them
1183 * for anything and thus we don't want mark their pointers here
1184 */
1185 if (!bkey_cmp(k, &ZERO_KEY))
1186 return stale;
1187
1188 for (i = 0; i < KEY_PTRS(k); i++) {
1189 if (!ptr_available(c, k, i))
1190 continue;
1191
1192 g = PTR_BUCKET(c, k, i);
1193
1194 if (gen_after(g->gc_gen, PTR_GEN(k, i)))
1195 g->gc_gen = PTR_GEN(k, i);
1196
1197 if (ptr_stale(c, k, i)) {
1198 stale = max(stale, ptr_stale(c, k, i));
1199 continue;
1200 }
1201
1202 cache_bug_on(GC_MARK(g) &&
1203 (GC_MARK(g) == GC_MARK_METADATA) != (level != 0),
1204 c, "inconsistent ptrs: mark = %llu, level = %i",
1205 GC_MARK(g), level);
1206
1207 if (level)
1208 SET_GC_MARK(g, GC_MARK_METADATA);
1209 else if (KEY_DIRTY(k))
1210 SET_GC_MARK(g, GC_MARK_DIRTY);
1211
1212 /* guard against overflow */
1213 SET_GC_SECTORS_USED(g, min_t(unsigned,
1214 GC_SECTORS_USED(g) + KEY_SIZE(k),
1215 (1 << 14) - 1));
1216
1217 BUG_ON(!GC_SECTORS_USED(g));
1218 }
1219
1220 return stale;
1221}
1222
1223#define btree_mark_key(b, k) __bch_btree_mark_key(b->c, b->level, k)
1224
Kent Overstreeta1f03582013-09-10 19:07:00 -07001225static bool btree_gc_mark_node(struct btree *b, struct gc_stat *gc)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001226{
1227 uint8_t stale = 0;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001228 unsigned keys = 0, good_keys = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001229 struct bkey *k;
1230 struct btree_iter iter;
1231 struct bset_tree *t;
1232
1233 gc->nodes++;
1234
1235 for_each_key_filter(b, k, &iter, bch_ptr_invalid) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001236 stale = max(stale, btree_mark_key(b, k));
Kent Overstreeta1f03582013-09-10 19:07:00 -07001237 keys++;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001238
1239 if (bch_ptr_bad(b, k))
1240 continue;
1241
Kent Overstreetcafe5632013-03-23 16:11:31 -07001242 gc->key_bytes += bkey_u64s(k);
1243 gc->nkeys++;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001244 good_keys++;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001245
1246 gc->data += KEY_SIZE(k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001247 }
1248
1249 for (t = b->sets; t <= &b->sets[b->nsets]; t++)
1250 btree_bug_on(t->size &&
1251 bset_written(b, t) &&
1252 bkey_cmp(&b->key, &t->end) < 0,
1253 b, "found short btree key in gc");
1254
Kent Overstreeta1f03582013-09-10 19:07:00 -07001255 if (b->c->gc_always_rewrite)
1256 return true;
1257
1258 if (stale > 10)
1259 return true;
1260
1261 if ((keys - good_keys) * 2 > keys)
1262 return true;
1263
1264 return false;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001265}
1266
Kent Overstreeta1f03582013-09-10 19:07:00 -07001267#define GC_MERGE_NODES 4U
Kent Overstreetcafe5632013-03-23 16:11:31 -07001268
1269struct gc_merge_info {
1270 struct btree *b;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001271 unsigned keys;
1272};
1273
Kent Overstreeta1f03582013-09-10 19:07:00 -07001274static int bch_btree_insert_node(struct btree *, struct btree_op *,
1275 struct keylist *, atomic_t *, struct bkey *);
Kent Overstreetb54d6932013-07-24 18:04:18 -07001276
Kent Overstreeta1f03582013-09-10 19:07:00 -07001277static int btree_gc_coalesce(struct btree *b, struct btree_op *op,
1278 struct keylist *keylist, struct gc_stat *gc,
1279 struct gc_merge_info *r)
1280{
1281 unsigned i, nodes = 0, keys = 0, blocks;
1282 struct btree *new_nodes[GC_MERGE_NODES];
1283 struct closure cl;
1284 struct bkey *k;
1285
1286 memset(new_nodes, 0, sizeof(new_nodes));
Kent Overstreetb54d6932013-07-24 18:04:18 -07001287 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001288
Kent Overstreeta1f03582013-09-10 19:07:00 -07001289 while (nodes < GC_MERGE_NODES && !IS_ERR_OR_NULL(r[nodes].b))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001290 keys += r[nodes++].keys;
1291
1292 blocks = btree_default_blocks(b->c) * 2 / 3;
1293
1294 if (nodes < 2 ||
1295 __set_blocks(b->sets[0].data, keys, b->c) > blocks * (nodes - 1))
Kent Overstreeta1f03582013-09-10 19:07:00 -07001296 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001297
Kent Overstreeta1f03582013-09-10 19:07:00 -07001298 for (i = 0; i < nodes; i++) {
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001299 new_nodes[i] = btree_node_alloc_replacement(r[i].b, false);
Kent Overstreeta1f03582013-09-10 19:07:00 -07001300 if (IS_ERR_OR_NULL(new_nodes[i]))
1301 goto out_nocoalesce;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001302 }
1303
1304 for (i = nodes - 1; i > 0; --i) {
Kent Overstreeta1f03582013-09-10 19:07:00 -07001305 struct bset *n1 = new_nodes[i]->sets->data;
1306 struct bset *n2 = new_nodes[i - 1]->sets->data;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001307 struct bkey *k, *last = NULL;
1308
1309 keys = 0;
1310
Kent Overstreeta1f03582013-09-10 19:07:00 -07001311 if (i > 1) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001312 for (k = n2->start;
Kent Overstreetfafff812013-12-17 21:56:21 -08001313 k < bset_bkey_last(n2);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001314 k = bkey_next(k)) {
1315 if (__set_blocks(n1, n1->keys + keys +
1316 bkey_u64s(k), b->c) > blocks)
1317 break;
1318
1319 last = k;
1320 keys += bkey_u64s(k);
1321 }
Kent Overstreeta1f03582013-09-10 19:07:00 -07001322 } else {
1323 /*
1324 * Last node we're not getting rid of - we're getting
1325 * rid of the node at r[0]. Have to try and fit all of
1326 * the remaining keys into this node; we can't ensure
1327 * they will always fit due to rounding and variable
1328 * length keys (shouldn't be possible in practice,
1329 * though)
1330 */
1331 if (__set_blocks(n1, n1->keys + n2->keys,
1332 b->c) > btree_blocks(new_nodes[i]))
1333 goto out_nocoalesce;
1334
1335 keys = n2->keys;
1336 /* Take the key of the node we're getting rid of */
1337 last = &r->b->key;
1338 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001339
1340 BUG_ON(__set_blocks(n1, n1->keys + keys,
Kent Overstreeta1f03582013-09-10 19:07:00 -07001341 b->c) > btree_blocks(new_nodes[i]));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001342
Kent Overstreeta1f03582013-09-10 19:07:00 -07001343 if (last)
1344 bkey_copy_key(&new_nodes[i]->key, last);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001345
Kent Overstreetfafff812013-12-17 21:56:21 -08001346 memcpy(bset_bkey_last(n1),
Kent Overstreetcafe5632013-03-23 16:11:31 -07001347 n2->start,
Kent Overstreetfafff812013-12-17 21:56:21 -08001348 (void *) bset_bkey_idx(n2, keys) - (void *) n2->start);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001349
1350 n1->keys += keys;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001351 r[i].keys = n1->keys;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001352
1353 memmove(n2->start,
Kent Overstreetfafff812013-12-17 21:56:21 -08001354 bset_bkey_idx(n2, keys),
1355 (void *) bset_bkey_last(n2) -
1356 (void *) bset_bkey_idx(n2, keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001357
1358 n2->keys -= keys;
1359
Kent Overstreet085d2a32013-11-11 18:20:51 -08001360 if (__bch_keylist_realloc(keylist,
1361 bkey_u64s(&new_nodes[i]->key)))
Kent Overstreeta1f03582013-09-10 19:07:00 -07001362 goto out_nocoalesce;
1363
1364 bch_btree_node_write(new_nodes[i], &cl);
1365 bch_keylist_add(keylist, &new_nodes[i]->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001366 }
1367
Kent Overstreeta1f03582013-09-10 19:07:00 -07001368 for (i = 0; i < nodes; i++) {
Kent Overstreet085d2a32013-11-11 18:20:51 -08001369 if (__bch_keylist_realloc(keylist, bkey_u64s(&r[i].b->key)))
Kent Overstreeta1f03582013-09-10 19:07:00 -07001370 goto out_nocoalesce;
1371
1372 make_btree_freeing_key(r[i].b, keylist->top);
1373 bch_keylist_push(keylist);
1374 }
1375
1376 /* We emptied out this node */
1377 BUG_ON(new_nodes[0]->sets->data->keys);
1378 btree_node_free(new_nodes[0]);
1379 rw_unlock(true, new_nodes[0]);
1380
1381 closure_sync(&cl);
1382
1383 for (i = 0; i < nodes; i++) {
1384 btree_node_free(r[i].b);
1385 rw_unlock(true, r[i].b);
1386
1387 r[i].b = new_nodes[i];
1388 }
1389
1390 bch_btree_insert_node(b, op, keylist, NULL, NULL);
1391 BUG_ON(!bch_keylist_empty(keylist));
1392
1393 memmove(r, r + 1, sizeof(r[0]) * (nodes - 1));
1394 r[nodes - 1].b = ERR_PTR(-EINTR);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001395
Kent Overstreetc37511b2013-04-26 15:39:55 -07001396 trace_bcache_btree_gc_coalesce(nodes);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001397 gc->nodes--;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001398
Kent Overstreeta1f03582013-09-10 19:07:00 -07001399 /* Invalidated our iterator */
1400 return -EINTR;
1401
1402out_nocoalesce:
1403 closure_sync(&cl);
1404
1405 while ((k = bch_keylist_pop(keylist)))
1406 if (!bkey_cmp(k, &ZERO_KEY))
1407 atomic_dec(&b->c->prio_blocked);
1408
1409 for (i = 0; i < nodes; i++)
1410 if (!IS_ERR_OR_NULL(new_nodes[i])) {
1411 btree_node_free(new_nodes[i]);
1412 rw_unlock(true, new_nodes[i]);
1413 }
1414 return 0;
1415}
1416
1417static unsigned btree_gc_count_keys(struct btree *b)
1418{
1419 struct bkey *k;
1420 struct btree_iter iter;
1421 unsigned ret = 0;
1422
1423 for_each_key_filter(b, k, &iter, bch_ptr_bad)
1424 ret += bkey_u64s(k);
1425
1426 return ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001427}
1428
1429static int btree_gc_recurse(struct btree *b, struct btree_op *op,
1430 struct closure *writes, struct gc_stat *gc)
1431{
Kent Overstreetcafe5632013-03-23 16:11:31 -07001432 unsigned i;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001433 int ret = 0;
1434 bool should_rewrite;
1435 struct btree *n;
1436 struct bkey *k;
1437 struct keylist keys;
1438 struct btree_iter iter;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001439 struct gc_merge_info r[GC_MERGE_NODES];
Kent Overstreeta1f03582013-09-10 19:07:00 -07001440 struct gc_merge_info *last = r + GC_MERGE_NODES - 1;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001441
Kent Overstreeta1f03582013-09-10 19:07:00 -07001442 bch_keylist_init(&keys);
1443 bch_btree_iter_init(b, &iter, &b->c->gc_done);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001444
Kent Overstreeta1f03582013-09-10 19:07:00 -07001445 for (i = 0; i < GC_MERGE_NODES; i++)
1446 r[i].b = ERR_PTR(-EINTR);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001447
Kent Overstreeta1f03582013-09-10 19:07:00 -07001448 while (1) {
1449 k = bch_btree_iter_next_filter(&iter, b, bch_ptr_bad);
1450 if (k) {
1451 r->b = bch_btree_node_get(b->c, k, b->level - 1, true);
1452 if (IS_ERR(r->b)) {
1453 ret = PTR_ERR(r->b);
1454 break;
1455 }
1456
1457 r->keys = btree_gc_count_keys(r->b);
1458
1459 ret = btree_gc_coalesce(b, op, &keys, gc, r);
1460 if (ret)
1461 break;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001462 }
1463
Kent Overstreeta1f03582013-09-10 19:07:00 -07001464 if (!last->b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001465 break;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001466
1467 if (!IS_ERR(last->b)) {
1468 should_rewrite = btree_gc_mark_node(last->b, gc);
Kent Overstreet78365412013-12-17 01:29:34 -08001469 if (should_rewrite &&
1470 !btree_check_reserve(b, NULL)) {
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001471 n = btree_node_alloc_replacement(last->b,
1472 false);
Kent Overstreeta1f03582013-09-10 19:07:00 -07001473
1474 if (!IS_ERR_OR_NULL(n)) {
1475 bch_btree_node_write_sync(n);
1476 bch_keylist_add(&keys, &n->key);
1477
1478 make_btree_freeing_key(last->b,
1479 keys.top);
1480 bch_keylist_push(&keys);
1481
1482 btree_node_free(last->b);
1483
1484 bch_btree_insert_node(b, op, &keys,
1485 NULL, NULL);
1486 BUG_ON(!bch_keylist_empty(&keys));
1487
1488 rw_unlock(true, last->b);
1489 last->b = n;
1490
1491 /* Invalidated our iterator */
1492 ret = -EINTR;
1493 break;
1494 }
1495 }
1496
1497 if (last->b->level) {
1498 ret = btree_gc_recurse(last->b, op, writes, gc);
1499 if (ret)
1500 break;
1501 }
1502
1503 bkey_copy_key(&b->c->gc_done, &last->b->key);
1504
1505 /*
1506 * Must flush leaf nodes before gc ends, since replace
1507 * operations aren't journalled
1508 */
1509 if (btree_node_dirty(last->b))
1510 bch_btree_node_write(last->b, writes);
1511 rw_unlock(true, last->b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001512 }
1513
Kent Overstreeta1f03582013-09-10 19:07:00 -07001514 memmove(r + 1, r, sizeof(r[0]) * (GC_MERGE_NODES - 1));
1515 r->b = NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001516
Kent Overstreetcafe5632013-03-23 16:11:31 -07001517 if (need_resched()) {
1518 ret = -EAGAIN;
1519 break;
1520 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001521 }
1522
Kent Overstreeta1f03582013-09-10 19:07:00 -07001523 for (i = 0; i < GC_MERGE_NODES; i++)
1524 if (!IS_ERR_OR_NULL(r[i].b)) {
1525 if (btree_node_dirty(r[i].b))
1526 bch_btree_node_write(r[i].b, writes);
1527 rw_unlock(true, r[i].b);
1528 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001529
Kent Overstreeta1f03582013-09-10 19:07:00 -07001530 bch_keylist_free(&keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001531
1532 return ret;
1533}
1534
1535static int bch_btree_gc_root(struct btree *b, struct btree_op *op,
1536 struct closure *writes, struct gc_stat *gc)
1537{
1538 struct btree *n = NULL;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001539 int ret = 0;
1540 bool should_rewrite;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001541
Kent Overstreeta1f03582013-09-10 19:07:00 -07001542 should_rewrite = btree_gc_mark_node(b, gc);
1543 if (should_rewrite) {
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001544 n = btree_node_alloc_replacement(b, false);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001545
Kent Overstreeta1f03582013-09-10 19:07:00 -07001546 if (!IS_ERR_OR_NULL(n)) {
1547 bch_btree_node_write_sync(n);
1548 bch_btree_set_root(n);
1549 btree_node_free(b);
1550 rw_unlock(true, n);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001551
Kent Overstreeta1f03582013-09-10 19:07:00 -07001552 return -EINTR;
1553 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001554 }
1555
Kent Overstreeta1f03582013-09-10 19:07:00 -07001556 if (b->level) {
1557 ret = btree_gc_recurse(b, op, writes, gc);
1558 if (ret)
1559 return ret;
1560 }
1561
1562 bkey_copy_key(&b->c->gc_done, &b->key);
1563
Kent Overstreetcafe5632013-03-23 16:11:31 -07001564 return ret;
1565}
1566
1567static void btree_gc_start(struct cache_set *c)
1568{
1569 struct cache *ca;
1570 struct bucket *b;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001571 unsigned i;
1572
1573 if (!c->gc_mark_valid)
1574 return;
1575
1576 mutex_lock(&c->bucket_lock);
1577
1578 c->gc_mark_valid = 0;
1579 c->gc_done = ZERO_KEY;
1580
1581 for_each_cache(ca, c, i)
1582 for_each_bucket(b, ca) {
1583 b->gc_gen = b->gen;
Kent Overstreet29ebf462013-07-11 19:43:21 -07001584 if (!atomic_read(&b->pin)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001585 SET_GC_MARK(b, GC_MARK_RECLAIMABLE);
Kent Overstreet29ebf462013-07-11 19:43:21 -07001586 SET_GC_SECTORS_USED(b, 0);
1587 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001588 }
1589
Kent Overstreetcafe5632013-03-23 16:11:31 -07001590 mutex_unlock(&c->bucket_lock);
1591}
1592
1593size_t bch_btree_gc_finish(struct cache_set *c)
1594{
1595 size_t available = 0;
1596 struct bucket *b;
1597 struct cache *ca;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001598 unsigned i;
1599
1600 mutex_lock(&c->bucket_lock);
1601
1602 set_gc_sectors(c);
1603 c->gc_mark_valid = 1;
1604 c->need_gc = 0;
1605
1606 if (c->root)
1607 for (i = 0; i < KEY_PTRS(&c->root->key); i++)
1608 SET_GC_MARK(PTR_BUCKET(c, &c->root->key, i),
1609 GC_MARK_METADATA);
1610
1611 for (i = 0; i < KEY_PTRS(&c->uuid_bucket); i++)
1612 SET_GC_MARK(PTR_BUCKET(c, &c->uuid_bucket, i),
1613 GC_MARK_METADATA);
1614
Nicholas Swensonbf0a6282013-11-26 19:14:23 -08001615 /* don't reclaim buckets to which writeback keys point */
1616 rcu_read_lock();
1617 for (i = 0; i < c->nr_uuids; i++) {
1618 struct bcache_device *d = c->devices[i];
1619 struct cached_dev *dc;
1620 struct keybuf_key *w, *n;
1621 unsigned j;
1622
1623 if (!d || UUID_FLASH_ONLY(&c->uuids[i]))
1624 continue;
1625 dc = container_of(d, struct cached_dev, disk);
1626
1627 spin_lock(&dc->writeback_keys.lock);
1628 rbtree_postorder_for_each_entry_safe(w, n,
1629 &dc->writeback_keys.keys, node)
1630 for (j = 0; j < KEY_PTRS(&w->key); j++)
1631 SET_GC_MARK(PTR_BUCKET(c, &w->key, j),
1632 GC_MARK_DIRTY);
1633 spin_unlock(&dc->writeback_keys.lock);
1634 }
1635 rcu_read_unlock();
1636
Kent Overstreetcafe5632013-03-23 16:11:31 -07001637 for_each_cache(ca, c, i) {
1638 uint64_t *i;
1639
1640 ca->invalidate_needs_gc = 0;
1641
1642 for (i = ca->sb.d; i < ca->sb.d + ca->sb.keys; i++)
1643 SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA);
1644
1645 for (i = ca->prio_buckets;
1646 i < ca->prio_buckets + prio_buckets(ca) * 2; i++)
1647 SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA);
1648
1649 for_each_bucket(b, ca) {
1650 b->last_gc = b->gc_gen;
1651 c->need_gc = max(c->need_gc, bucket_gc_gen(b));
1652
1653 if (!atomic_read(&b->pin) &&
1654 GC_MARK(b) == GC_MARK_RECLAIMABLE) {
1655 available++;
1656 if (!GC_SECTORS_USED(b))
1657 bch_bucket_add_unused(ca, b);
1658 }
1659 }
1660 }
1661
Kent Overstreetcafe5632013-03-23 16:11:31 -07001662 mutex_unlock(&c->bucket_lock);
1663 return available;
1664}
1665
Kent Overstreet72a44512013-10-24 17:19:26 -07001666static void bch_btree_gc(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001667{
Kent Overstreetcafe5632013-03-23 16:11:31 -07001668 int ret;
1669 unsigned long available;
1670 struct gc_stat stats;
1671 struct closure writes;
1672 struct btree_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001673 uint64_t start_time = local_clock();
Kent Overstreet57943512013-04-25 13:58:35 -07001674
Kent Overstreetc37511b2013-04-26 15:39:55 -07001675 trace_bcache_gc_start(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001676
1677 memset(&stats, 0, sizeof(struct gc_stat));
1678 closure_init_stack(&writes);
Kent Overstreetb54d6932013-07-24 18:04:18 -07001679 bch_btree_op_init(&op, SHRT_MAX);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001680
1681 btree_gc_start(c);
1682
Kent Overstreeta1f03582013-09-10 19:07:00 -07001683 do {
1684 ret = btree_root(gc_root, c, &op, &writes, &stats);
1685 closure_sync(&writes);
Kent Overstreet57943512013-04-25 13:58:35 -07001686
Kent Overstreeta1f03582013-09-10 19:07:00 -07001687 if (ret && ret != -EAGAIN)
1688 pr_warn("gc failed!");
1689 } while (ret);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001690
1691 available = bch_btree_gc_finish(c);
Kent Overstreet57943512013-04-25 13:58:35 -07001692 wake_up_allocators(c);
1693
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001694 bch_time_stats_update(&c->btree_gc_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001695
1696 stats.key_bytes *= sizeof(uint64_t);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001697 stats.data <<= 9;
1698 stats.in_use = (c->nbuckets - available) * 100 / c->nbuckets;
1699 memcpy(&c->gc_stats, &stats, sizeof(struct gc_stat));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001700
Kent Overstreetc37511b2013-04-26 15:39:55 -07001701 trace_bcache_gc_end(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001702
Kent Overstreet72a44512013-10-24 17:19:26 -07001703 bch_moving_gc(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001704}
1705
Kent Overstreet72a44512013-10-24 17:19:26 -07001706static int bch_gc_thread(void *arg)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001707{
Kent Overstreet72a44512013-10-24 17:19:26 -07001708 struct cache_set *c = arg;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001709 struct cache *ca;
1710 unsigned i;
Kent Overstreet72a44512013-10-24 17:19:26 -07001711
1712 while (1) {
Kent Overstreeta1f03582013-09-10 19:07:00 -07001713again:
Kent Overstreet72a44512013-10-24 17:19:26 -07001714 bch_btree_gc(c);
1715
1716 set_current_state(TASK_INTERRUPTIBLE);
1717 if (kthread_should_stop())
1718 break;
1719
Kent Overstreeta1f03582013-09-10 19:07:00 -07001720 mutex_lock(&c->bucket_lock);
1721
1722 for_each_cache(ca, c, i)
1723 if (ca->invalidate_needs_gc) {
1724 mutex_unlock(&c->bucket_lock);
1725 set_current_state(TASK_RUNNING);
1726 goto again;
1727 }
1728
1729 mutex_unlock(&c->bucket_lock);
1730
Kent Overstreet72a44512013-10-24 17:19:26 -07001731 try_to_freeze();
1732 schedule();
1733 }
1734
1735 return 0;
1736}
1737
1738int bch_gc_thread_start(struct cache_set *c)
1739{
1740 c->gc_thread = kthread_create(bch_gc_thread, c, "bcache_gc");
1741 if (IS_ERR(c->gc_thread))
1742 return PTR_ERR(c->gc_thread);
1743
1744 set_task_state(c->gc_thread, TASK_INTERRUPTIBLE);
1745 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001746}
1747
1748/* Initial partial gc */
1749
1750static int bch_btree_check_recurse(struct btree *b, struct btree_op *op,
1751 unsigned long **seen)
1752{
Kent Overstreet50310162013-09-10 17:18:59 -07001753 int ret = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001754 unsigned i;
Kent Overstreet50310162013-09-10 17:18:59 -07001755 struct bkey *k, *p = NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001756 struct bucket *g;
1757 struct btree_iter iter;
1758
1759 for_each_key_filter(b, k, &iter, bch_ptr_invalid) {
1760 for (i = 0; i < KEY_PTRS(k); i++) {
1761 if (!ptr_available(b->c, k, i))
1762 continue;
1763
1764 g = PTR_BUCKET(b->c, k, i);
1765
1766 if (!__test_and_set_bit(PTR_BUCKET_NR(b->c, k, i),
1767 seen[PTR_DEV(k, i)]) ||
1768 !ptr_stale(b->c, k, i)) {
1769 g->gen = PTR_GEN(k, i);
1770
1771 if (b->level)
1772 g->prio = BTREE_PRIO;
1773 else if (g->prio == BTREE_PRIO)
1774 g->prio = INITIAL_PRIO;
1775 }
1776 }
1777
1778 btree_mark_key(b, k);
1779 }
1780
1781 if (b->level) {
Kent Overstreet50310162013-09-10 17:18:59 -07001782 bch_btree_iter_init(b, &iter, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001783
Kent Overstreet50310162013-09-10 17:18:59 -07001784 do {
1785 k = bch_btree_iter_next_filter(&iter, b, bch_ptr_bad);
1786 if (k)
1787 btree_node_prefetch(b->c, k, b->level - 1);
1788
Kent Overstreetcafe5632013-03-23 16:11:31 -07001789 if (p)
Kent Overstreet50310162013-09-10 17:18:59 -07001790 ret = btree(check_recurse, p, b, op, seen);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001791
Kent Overstreet50310162013-09-10 17:18:59 -07001792 p = k;
1793 } while (p && !ret);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001794 }
1795
1796 return 0;
1797}
1798
Kent Overstreetc18536a2013-07-24 17:44:17 -07001799int bch_btree_check(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001800{
1801 int ret = -ENOMEM;
1802 unsigned i;
1803 unsigned long *seen[MAX_CACHES_PER_SET];
Kent Overstreetc18536a2013-07-24 17:44:17 -07001804 struct btree_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001805
1806 memset(seen, 0, sizeof(seen));
Kent Overstreetb54d6932013-07-24 18:04:18 -07001807 bch_btree_op_init(&op, SHRT_MAX);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001808
1809 for (i = 0; c->cache[i]; i++) {
1810 size_t n = DIV_ROUND_UP(c->cache[i]->sb.nbuckets, 8);
1811 seen[i] = kmalloc(n, GFP_KERNEL);
1812 if (!seen[i])
1813 goto err;
1814
1815 /* Disables the seen array until prio_read() uses it too */
1816 memset(seen[i], 0xFF, n);
1817 }
1818
Kent Overstreetc18536a2013-07-24 17:44:17 -07001819 ret = btree_root(check_recurse, c, &op, seen);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001820err:
1821 for (i = 0; i < MAX_CACHES_PER_SET; i++)
1822 kfree(seen[i]);
1823 return ret;
1824}
1825
1826/* Btree insertion */
1827
1828static void shift_keys(struct btree *b, struct bkey *where, struct bkey *insert)
1829{
1830 struct bset *i = b->sets[b->nsets].data;
1831
1832 memmove((uint64_t *) where + bkey_u64s(insert),
1833 where,
Kent Overstreetfafff812013-12-17 21:56:21 -08001834 (void *) bset_bkey_last(i) - (void *) where);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001835
1836 i->keys += bkey_u64s(insert);
1837 bkey_copy(where, insert);
1838 bch_bset_fix_lookup_table(b, where);
1839}
1840
Kent Overstreet1b207d82013-09-10 18:52:54 -07001841static bool fix_overlapping_extents(struct btree *b, struct bkey *insert,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001842 struct btree_iter *iter,
Kent Overstreet1b207d82013-09-10 18:52:54 -07001843 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001844{
Kent Overstreet279afba2013-06-05 06:21:07 -07001845 void subtract_dirty(struct bkey *k, uint64_t offset, int sectors)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001846 {
Kent Overstreet279afba2013-06-05 06:21:07 -07001847 if (KEY_DIRTY(k))
1848 bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k),
1849 offset, -sectors);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001850 }
1851
Kent Overstreet279afba2013-06-05 06:21:07 -07001852 uint64_t old_offset;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001853 unsigned old_size, sectors_found = 0;
1854
1855 while (1) {
1856 struct bkey *k = bch_btree_iter_next(iter);
1857 if (!k ||
1858 bkey_cmp(&START_KEY(k), insert) >= 0)
1859 break;
1860
1861 if (bkey_cmp(k, &START_KEY(insert)) <= 0)
1862 continue;
1863
Kent Overstreet279afba2013-06-05 06:21:07 -07001864 old_offset = KEY_START(k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001865 old_size = KEY_SIZE(k);
1866
1867 /*
1868 * We might overlap with 0 size extents; we can't skip these
1869 * because if they're in the set we're inserting to we have to
1870 * adjust them so they don't overlap with the key we're
Kent Overstreet1b207d82013-09-10 18:52:54 -07001871 * inserting. But we don't want to check them for replace
Kent Overstreetcafe5632013-03-23 16:11:31 -07001872 * operations.
1873 */
1874
Kent Overstreet1b207d82013-09-10 18:52:54 -07001875 if (replace_key && KEY_SIZE(k)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001876 /*
1877 * k might have been split since we inserted/found the
1878 * key we're replacing
1879 */
1880 unsigned i;
1881 uint64_t offset = KEY_START(k) -
Kent Overstreet1b207d82013-09-10 18:52:54 -07001882 KEY_START(replace_key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001883
1884 /* But it must be a subset of the replace key */
Kent Overstreet1b207d82013-09-10 18:52:54 -07001885 if (KEY_START(k) < KEY_START(replace_key) ||
1886 KEY_OFFSET(k) > KEY_OFFSET(replace_key))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001887 goto check_failed;
1888
1889 /* We didn't find a key that we were supposed to */
1890 if (KEY_START(k) > KEY_START(insert) + sectors_found)
1891 goto check_failed;
1892
Kent Overstreetd24a6e12013-11-10 21:55:27 -08001893 if (KEY_PTRS(k) != KEY_PTRS(replace_key) ||
1894 KEY_DIRTY(k) != KEY_DIRTY(replace_key))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001895 goto check_failed;
1896
1897 /* skip past gen */
1898 offset <<= 8;
1899
Kent Overstreet1b207d82013-09-10 18:52:54 -07001900 BUG_ON(!KEY_PTRS(replace_key));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001901
Kent Overstreet1b207d82013-09-10 18:52:54 -07001902 for (i = 0; i < KEY_PTRS(replace_key); i++)
1903 if (k->ptr[i] != replace_key->ptr[i] + offset)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001904 goto check_failed;
1905
1906 sectors_found = KEY_OFFSET(k) - KEY_START(insert);
1907 }
1908
1909 if (bkey_cmp(insert, k) < 0 &&
1910 bkey_cmp(&START_KEY(insert), &START_KEY(k)) > 0) {
1911 /*
1912 * We overlapped in the middle of an existing key: that
1913 * means we have to split the old key. But we have to do
1914 * slightly different things depending on whether the
1915 * old key has been written out yet.
1916 */
1917
1918 struct bkey *top;
1919
Kent Overstreet279afba2013-06-05 06:21:07 -07001920 subtract_dirty(k, KEY_START(insert), KEY_SIZE(insert));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001921
1922 if (bkey_written(b, k)) {
1923 /*
1924 * We insert a new key to cover the top of the
1925 * old key, and the old key is modified in place
1926 * to represent the bottom split.
1927 *
1928 * It's completely arbitrary whether the new key
1929 * is the top or the bottom, but it has to match
1930 * up with what btree_sort_fixup() does - it
1931 * doesn't check for this kind of overlap, it
1932 * depends on us inserting a new key for the top
1933 * here.
1934 */
1935 top = bch_bset_search(b, &b->sets[b->nsets],
1936 insert);
1937 shift_keys(b, top, k);
1938 } else {
1939 BKEY_PADDED(key) temp;
1940 bkey_copy(&temp.key, k);
1941 shift_keys(b, k, &temp.key);
1942 top = bkey_next(k);
1943 }
1944
1945 bch_cut_front(insert, top);
1946 bch_cut_back(&START_KEY(insert), k);
1947 bch_bset_fix_invalidated_key(b, k);
1948 return false;
1949 }
1950
1951 if (bkey_cmp(insert, k) < 0) {
1952 bch_cut_front(insert, k);
1953 } else {
Kent Overstreet1fa84552013-11-10 21:55:27 -08001954 if (bkey_cmp(&START_KEY(insert), &START_KEY(k)) > 0)
1955 old_offset = KEY_START(insert);
1956
Kent Overstreetcafe5632013-03-23 16:11:31 -07001957 if (bkey_written(b, k) &&
1958 bkey_cmp(&START_KEY(insert), &START_KEY(k)) <= 0) {
1959 /*
1960 * Completely overwrote, so we don't have to
1961 * invalidate the binary search tree
1962 */
1963 bch_cut_front(k, k);
1964 } else {
1965 __bch_cut_back(&START_KEY(insert), k);
1966 bch_bset_fix_invalidated_key(b, k);
1967 }
1968 }
1969
Kent Overstreet279afba2013-06-05 06:21:07 -07001970 subtract_dirty(k, old_offset, old_size - KEY_SIZE(k));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001971 }
1972
1973check_failed:
Kent Overstreet1b207d82013-09-10 18:52:54 -07001974 if (replace_key) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001975 if (!sectors_found) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001976 return true;
1977 } else if (sectors_found < KEY_SIZE(insert)) {
1978 SET_KEY_OFFSET(insert, KEY_OFFSET(insert) -
1979 (KEY_SIZE(insert) - sectors_found));
1980 SET_KEY_SIZE(insert, sectors_found);
1981 }
1982 }
1983
1984 return false;
1985}
1986
1987static bool btree_insert_key(struct btree *b, struct btree_op *op,
Kent Overstreet1b207d82013-09-10 18:52:54 -07001988 struct bkey *k, struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001989{
1990 struct bset *i = b->sets[b->nsets].data;
1991 struct bkey *m, *prev;
Kent Overstreet85b14922013-05-14 20:33:16 -07001992 unsigned status = BTREE_INSERT_STATUS_INSERT;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001993
1994 BUG_ON(bkey_cmp(k, &b->key) > 0);
1995 BUG_ON(b->level && !KEY_PTRS(k));
1996 BUG_ON(!b->level && !KEY_OFFSET(k));
1997
1998 if (!b->level) {
1999 struct btree_iter iter;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002000
2001 /*
2002 * bset_search() returns the first key that is strictly greater
2003 * than the search key - but for back merging, we want to find
Kent Overstreet0eacac22013-07-01 19:29:05 -07002004 * the previous key.
Kent Overstreetcafe5632013-03-23 16:11:31 -07002005 */
Kent Overstreetcafe5632013-03-23 16:11:31 -07002006 prev = NULL;
Kent Overstreet0eacac22013-07-01 19:29:05 -07002007 m = bch_btree_iter_init(b, &iter, PRECEDING_KEY(&START_KEY(k)));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002008
Kent Overstreet1b207d82013-09-10 18:52:54 -07002009 if (fix_overlapping_extents(b, k, &iter, replace_key)) {
2010 op->insert_collision = true;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002011 return false;
Kent Overstreet1b207d82013-09-10 18:52:54 -07002012 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07002013
Kent Overstreet1fa84552013-11-10 21:55:27 -08002014 if (KEY_DIRTY(k))
2015 bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k),
2016 KEY_START(k), KEY_SIZE(k));
2017
Kent Overstreetfafff812013-12-17 21:56:21 -08002018 while (m != bset_bkey_last(i) &&
Kent Overstreetcafe5632013-03-23 16:11:31 -07002019 bkey_cmp(k, &START_KEY(m)) > 0)
2020 prev = m, m = bkey_next(m);
2021
2022 if (key_merging_disabled(b->c))
2023 goto insert;
2024
2025 /* prev is in the tree, if we merge we're done */
Kent Overstreet85b14922013-05-14 20:33:16 -07002026 status = BTREE_INSERT_STATUS_BACK_MERGE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002027 if (prev &&
2028 bch_bkey_try_merge(b, prev, k))
2029 goto merged;
2030
Kent Overstreet85b14922013-05-14 20:33:16 -07002031 status = BTREE_INSERT_STATUS_OVERWROTE;
Kent Overstreetfafff812013-12-17 21:56:21 -08002032 if (m != bset_bkey_last(i) &&
Kent Overstreetcafe5632013-03-23 16:11:31 -07002033 KEY_PTRS(m) == KEY_PTRS(k) && !KEY_SIZE(m))
2034 goto copy;
2035
Kent Overstreet85b14922013-05-14 20:33:16 -07002036 status = BTREE_INSERT_STATUS_FRONT_MERGE;
Kent Overstreetfafff812013-12-17 21:56:21 -08002037 if (m != bset_bkey_last(i) &&
Kent Overstreetcafe5632013-03-23 16:11:31 -07002038 bch_bkey_try_merge(b, k, m))
2039 goto copy;
Kent Overstreet1b207d82013-09-10 18:52:54 -07002040 } else {
2041 BUG_ON(replace_key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002042 m = bch_bset_search(b, &b->sets[b->nsets], k);
Kent Overstreet1b207d82013-09-10 18:52:54 -07002043 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07002044
2045insert: shift_keys(b, m, k);
2046copy: bkey_copy(m, k);
2047merged:
Kent Overstreet1b207d82013-09-10 18:52:54 -07002048 bch_check_keys(b, "%u for %s", status,
2049 replace_key ? "replace" : "insert");
Kent Overstreetcafe5632013-03-23 16:11:31 -07002050
2051 if (b->level && !KEY_OFFSET(k))
Kent Overstreet57943512013-04-25 13:58:35 -07002052 btree_current_write(b)->prio_blocked++;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002053
Kent Overstreet1b207d82013-09-10 18:52:54 -07002054 trace_bcache_btree_insert_key(b, k, replace_key != NULL, status);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002055
2056 return true;
2057}
2058
Kent Overstreet26c949f2013-09-10 18:41:15 -07002059static bool bch_btree_insert_keys(struct btree *b, struct btree_op *op,
Kent Overstreet1b207d82013-09-10 18:52:54 -07002060 struct keylist *insert_keys,
2061 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002062{
2063 bool ret = false;
Kent Overstreet280481d2013-10-24 16:36:03 -07002064 int oldsize = bch_count_data(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002065
Kent Overstreet26c949f2013-09-10 18:41:15 -07002066 while (!bch_keylist_empty(insert_keys)) {
Kent Overstreet403b6cd2013-07-24 17:22:44 -07002067 struct bset *i = write_block(b);
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07002068 struct bkey *k = insert_keys->keys;
Kent Overstreet26c949f2013-09-10 18:41:15 -07002069
Kent Overstreet403b6cd2013-07-24 17:22:44 -07002070 if (b->written + __set_blocks(i, i->keys + bkey_u64s(k), b->c)
2071 > btree_blocks(b))
2072 break;
2073
2074 if (bkey_cmp(k, &b->key) <= 0) {
Kent Overstreet3a3b6a42013-07-24 16:46:42 -07002075 if (!b->level)
2076 bkey_put(b->c, k);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002077
Kent Overstreet1b207d82013-09-10 18:52:54 -07002078 ret |= btree_insert_key(b, op, k, replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002079 bch_keylist_pop_front(insert_keys);
2080 } else if (bkey_cmp(&START_KEY(k), &b->key) < 0) {
Kent Overstreet26c949f2013-09-10 18:41:15 -07002081 BKEY_PADDED(key) temp;
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07002082 bkey_copy(&temp.key, insert_keys->keys);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002083
2084 bch_cut_back(&b->key, &temp.key);
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07002085 bch_cut_front(&b->key, insert_keys->keys);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002086
Kent Overstreet1b207d82013-09-10 18:52:54 -07002087 ret |= btree_insert_key(b, op, &temp.key, replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002088 break;
2089 } else {
2090 break;
2091 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07002092 }
2093
Kent Overstreet403b6cd2013-07-24 17:22:44 -07002094 BUG_ON(!bch_keylist_empty(insert_keys) && b->level);
2095
Kent Overstreetcafe5632013-03-23 16:11:31 -07002096 BUG_ON(bch_count_data(b) < oldsize);
2097 return ret;
2098}
2099
Kent Overstreet26c949f2013-09-10 18:41:15 -07002100static int btree_split(struct btree *b, struct btree_op *op,
2101 struct keylist *insert_keys,
Kent Overstreet1b207d82013-09-10 18:52:54 -07002102 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002103{
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002104 bool split;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002105 struct btree *n1, *n2 = NULL, *n3 = NULL;
2106 uint64_t start_time = local_clock();
Kent Overstreetb54d6932013-07-24 18:04:18 -07002107 struct closure cl;
Kent Overstreet17e21a92013-07-26 12:32:38 -07002108 struct keylist parent_keys;
Kent Overstreetb54d6932013-07-24 18:04:18 -07002109
2110 closure_init_stack(&cl);
Kent Overstreet17e21a92013-07-26 12:32:38 -07002111 bch_keylist_init(&parent_keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002112
Kent Overstreet78365412013-12-17 01:29:34 -08002113 if (!b->level &&
2114 btree_check_reserve(b, op))
2115 return -EINTR;
2116
Kent Overstreetbc9389e2013-09-10 19:07:35 -07002117 n1 = btree_node_alloc_replacement(b, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002118 if (IS_ERR(n1))
2119 goto err;
2120
2121 split = set_blocks(n1->sets[0].data, n1->c) > (btree_blocks(b) * 4) / 5;
2122
Kent Overstreetcafe5632013-03-23 16:11:31 -07002123 if (split) {
2124 unsigned keys = 0;
2125
Kent Overstreetc37511b2013-04-26 15:39:55 -07002126 trace_bcache_btree_node_split(b, n1->sets[0].data->keys);
2127
Kent Overstreetbc9389e2013-09-10 19:07:35 -07002128 n2 = bch_btree_node_alloc(b->c, b->level, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002129 if (IS_ERR(n2))
2130 goto err_free1;
2131
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002132 if (!b->parent) {
Kent Overstreetbc9389e2013-09-10 19:07:35 -07002133 n3 = bch_btree_node_alloc(b->c, b->level + 1, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002134 if (IS_ERR(n3))
2135 goto err_free2;
2136 }
2137
Kent Overstreet1b207d82013-09-10 18:52:54 -07002138 bch_btree_insert_keys(n1, op, insert_keys, replace_key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002139
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002140 /*
2141 * Has to be a linear search because we don't have an auxiliary
Kent Overstreetcafe5632013-03-23 16:11:31 -07002142 * search tree yet
2143 */
2144
2145 while (keys < (n1->sets[0].data->keys * 3) / 5)
Kent Overstreetfafff812013-12-17 21:56:21 -08002146 keys += bkey_u64s(bset_bkey_idx(n1->sets[0].data,
2147 keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002148
Kent Overstreetfafff812013-12-17 21:56:21 -08002149 bkey_copy_key(&n1->key,
2150 bset_bkey_idx(n1->sets[0].data, keys));
2151 keys += bkey_u64s(bset_bkey_idx(n1->sets[0].data, keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002152
2153 n2->sets[0].data->keys = n1->sets[0].data->keys - keys;
2154 n1->sets[0].data->keys = keys;
2155
2156 memcpy(n2->sets[0].data->start,
Kent Overstreetfafff812013-12-17 21:56:21 -08002157 bset_bkey_last(n1->sets[0].data),
Kent Overstreetcafe5632013-03-23 16:11:31 -07002158 n2->sets[0].data->keys * sizeof(uint64_t));
2159
2160 bkey_copy_key(&n2->key, &b->key);
2161
Kent Overstreet17e21a92013-07-26 12:32:38 -07002162 bch_keylist_add(&parent_keys, &n2->key);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002163 bch_btree_node_write(n2, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002164 rw_unlock(true, n2);
Kent Overstreetc37511b2013-04-26 15:39:55 -07002165 } else {
2166 trace_bcache_btree_node_compact(b, n1->sets[0].data->keys);
2167
Kent Overstreet1b207d82013-09-10 18:52:54 -07002168 bch_btree_insert_keys(n1, op, insert_keys, replace_key);
Kent Overstreetc37511b2013-04-26 15:39:55 -07002169 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07002170
Kent Overstreet17e21a92013-07-26 12:32:38 -07002171 bch_keylist_add(&parent_keys, &n1->key);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002172 bch_btree_node_write(n1, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002173
2174 if (n3) {
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002175 /* Depth increases, make a new root */
Kent Overstreetcafe5632013-03-23 16:11:31 -07002176 bkey_copy_key(&n3->key, &MAX_KEY);
Kent Overstreet17e21a92013-07-26 12:32:38 -07002177 bch_btree_insert_keys(n3, op, &parent_keys, NULL);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002178 bch_btree_node_write(n3, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002179
Kent Overstreetb54d6932013-07-24 18:04:18 -07002180 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002181 bch_btree_set_root(n3);
2182 rw_unlock(true, n3);
Kent Overstreet17e21a92013-07-26 12:32:38 -07002183
2184 btree_node_free(b);
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002185 } else if (!b->parent) {
2186 /* Root filled up but didn't need to be split */
Kent Overstreetb54d6932013-07-24 18:04:18 -07002187 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002188 bch_btree_set_root(n1);
Kent Overstreet17e21a92013-07-26 12:32:38 -07002189
2190 btree_node_free(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002191 } else {
Kent Overstreet17e21a92013-07-26 12:32:38 -07002192 /* Split a non root node */
Kent Overstreetb54d6932013-07-24 18:04:18 -07002193 closure_sync(&cl);
Kent Overstreet17e21a92013-07-26 12:32:38 -07002194 make_btree_freeing_key(b, parent_keys.top);
2195 bch_keylist_push(&parent_keys);
2196
2197 btree_node_free(b);
2198
2199 bch_btree_insert_node(b->parent, op, &parent_keys, NULL, NULL);
2200 BUG_ON(!bch_keylist_empty(&parent_keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002201 }
2202
2203 rw_unlock(true, n1);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002204
Kent Overstreet169ef1c2013-03-28 12:50:55 -06002205 bch_time_stats_update(&b->c->btree_split_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002206
2207 return 0;
2208err_free2:
Kent Overstreet5f5837d2013-12-16 16:38:49 -08002209 bkey_put(b->c, &n2->key);
Kent Overstreete8e1d462013-07-24 17:27:07 -07002210 btree_node_free(n2);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002211 rw_unlock(true, n2);
2212err_free1:
Kent Overstreet5f5837d2013-12-16 16:38:49 -08002213 bkey_put(b->c, &n1->key);
Kent Overstreete8e1d462013-07-24 17:27:07 -07002214 btree_node_free(n1);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002215 rw_unlock(true, n1);
2216err:
Kent Overstreet5f5837d2013-12-16 16:38:49 -08002217 WARN(1, "bcache: btree split failed");
2218
Kent Overstreetcafe5632013-03-23 16:11:31 -07002219 if (n3 == ERR_PTR(-EAGAIN) ||
2220 n2 == ERR_PTR(-EAGAIN) ||
2221 n1 == ERR_PTR(-EAGAIN))
2222 return -EAGAIN;
2223
Kent Overstreetcafe5632013-03-23 16:11:31 -07002224 return -ENOMEM;
2225}
2226
Kent Overstreet26c949f2013-09-10 18:41:15 -07002227static int bch_btree_insert_node(struct btree *b, struct btree_op *op,
Kent Overstreetc18536a2013-07-24 17:44:17 -07002228 struct keylist *insert_keys,
Kent Overstreet1b207d82013-09-10 18:52:54 -07002229 atomic_t *journal_ref,
2230 struct bkey *replace_key)
Kent Overstreet26c949f2013-09-10 18:41:15 -07002231{
Kent Overstreet17e21a92013-07-26 12:32:38 -07002232 BUG_ON(b->level && replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002233
Kent Overstreet17e21a92013-07-26 12:32:38 -07002234 if (should_split(b)) {
2235 if (current->bio_list) {
2236 op->lock = b->c->root->level + 1;
2237 return -EAGAIN;
2238 } else if (op->lock <= b->c->root->level) {
2239 op->lock = b->c->root->level + 1;
2240 return -EINTR;
Kent Overstreet26c949f2013-09-10 18:41:15 -07002241 } else {
Kent Overstreet17e21a92013-07-26 12:32:38 -07002242 /* Invalidated all iterators */
2243 return btree_split(b, op, insert_keys, replace_key) ?:
2244 -EINTR;
Kent Overstreet26c949f2013-09-10 18:41:15 -07002245 }
Kent Overstreet17e21a92013-07-26 12:32:38 -07002246 } else {
2247 BUG_ON(write_block(b) != b->sets[b->nsets].data);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002248
Kent Overstreet17e21a92013-07-26 12:32:38 -07002249 if (bch_btree_insert_keys(b, op, insert_keys, replace_key)) {
2250 if (!b->level)
2251 bch_btree_leaf_dirty(b, journal_ref);
2252 else
2253 bch_btree_node_write_sync(b);
2254 }
2255
2256 return 0;
2257 }
Kent Overstreet26c949f2013-09-10 18:41:15 -07002258}
2259
Kent Overstreete7c590e2013-09-10 18:39:16 -07002260int bch_btree_insert_check_key(struct btree *b, struct btree_op *op,
2261 struct bkey *check_key)
2262{
2263 int ret = -EINTR;
2264 uint64_t btree_ptr = b->key.ptr[0];
2265 unsigned long seq = b->seq;
2266 struct keylist insert;
2267 bool upgrade = op->lock == -1;
2268
2269 bch_keylist_init(&insert);
2270
2271 if (upgrade) {
2272 rw_unlock(false, b);
2273 rw_lock(true, b, b->level);
2274
2275 if (b->key.ptr[0] != btree_ptr ||
2276 b->seq != seq + 1)
2277 goto out;
2278 }
2279
2280 SET_KEY_PTRS(check_key, 1);
2281 get_random_bytes(&check_key->ptr[0], sizeof(uint64_t));
2282
2283 SET_PTR_DEV(check_key, 0, PTR_CHECK_DEV);
2284
2285 bch_keylist_add(&insert, check_key);
2286
Kent Overstreet1b207d82013-09-10 18:52:54 -07002287 ret = bch_btree_insert_node(b, op, &insert, NULL, NULL);
Kent Overstreete7c590e2013-09-10 18:39:16 -07002288
2289 BUG_ON(!ret && !bch_keylist_empty(&insert));
2290out:
2291 if (upgrade)
2292 downgrade_write(&b->lock);
2293 return ret;
2294}
2295
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002296struct btree_insert_op {
2297 struct btree_op op;
2298 struct keylist *keys;
2299 atomic_t *journal_ref;
2300 struct bkey *replace_key;
2301};
2302
Wei Yongjun08239ca2013-11-28 10:31:35 +08002303static int btree_insert_fn(struct btree_op *b_op, struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002304{
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002305 struct btree_insert_op *op = container_of(b_op,
2306 struct btree_insert_op, op);
Kent Overstreet403b6cd2013-07-24 17:22:44 -07002307
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002308 int ret = bch_btree_insert_node(b, &op->op, op->keys,
2309 op->journal_ref, op->replace_key);
2310 if (ret && !bch_keylist_empty(op->keys))
2311 return ret;
2312 else
2313 return MAP_DONE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002314}
2315
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002316int bch_btree_insert(struct cache_set *c, struct keylist *keys,
2317 atomic_t *journal_ref, struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002318{
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002319 struct btree_insert_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002320 int ret = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002321
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002322 BUG_ON(current->bio_list);
Kent Overstreet4f3d4012013-09-10 18:46:36 -07002323 BUG_ON(bch_keylist_empty(keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002324
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002325 bch_btree_op_init(&op.op, 0);
2326 op.keys = keys;
2327 op.journal_ref = journal_ref;
2328 op.replace_key = replace_key;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002329
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002330 while (!ret && !bch_keylist_empty(keys)) {
2331 op.op.lock = 0;
2332 ret = bch_btree_map_leaf_nodes(&op.op, c,
2333 &START_KEY(keys->keys),
2334 btree_insert_fn);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002335 }
2336
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002337 if (ret) {
2338 struct bkey *k;
2339
2340 pr_err("error %i", ret);
2341
2342 while ((k = bch_keylist_pop(keys)))
Kent Overstreet3a3b6a42013-07-24 16:46:42 -07002343 bkey_put(c, k);
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002344 } else if (op.op.insert_collision)
2345 ret = -ESRCH;
Kent Overstreet6054c6d2013-07-24 18:06:22 -07002346
Kent Overstreetcafe5632013-03-23 16:11:31 -07002347 return ret;
2348}
2349
2350void bch_btree_set_root(struct btree *b)
2351{
2352 unsigned i;
Kent Overstreete49c7c32013-06-26 17:25:38 -07002353 struct closure cl;
2354
2355 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002356
Kent Overstreetc37511b2013-04-26 15:39:55 -07002357 trace_bcache_btree_set_root(b);
2358
Kent Overstreetcafe5632013-03-23 16:11:31 -07002359 BUG_ON(!b->written);
2360
2361 for (i = 0; i < KEY_PTRS(&b->key); i++)
2362 BUG_ON(PTR_BUCKET(b->c, &b->key, i)->prio != BTREE_PRIO);
2363
2364 mutex_lock(&b->c->bucket_lock);
2365 list_del_init(&b->list);
2366 mutex_unlock(&b->c->bucket_lock);
2367
2368 b->c->root = b;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002369
Kent Overstreete49c7c32013-06-26 17:25:38 -07002370 bch_journal_meta(b->c, &cl);
2371 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002372}
2373
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002374/* Map across nodes or keys */
2375
2376static int bch_btree_map_nodes_recurse(struct btree *b, struct btree_op *op,
2377 struct bkey *from,
2378 btree_map_nodes_fn *fn, int flags)
2379{
2380 int ret = MAP_CONTINUE;
2381
2382 if (b->level) {
2383 struct bkey *k;
2384 struct btree_iter iter;
2385
2386 bch_btree_iter_init(b, &iter, from);
2387
2388 while ((k = bch_btree_iter_next_filter(&iter, b,
2389 bch_ptr_bad))) {
2390 ret = btree(map_nodes_recurse, k, b,
2391 op, from, fn, flags);
2392 from = NULL;
2393
2394 if (ret != MAP_CONTINUE)
2395 return ret;
2396 }
2397 }
2398
2399 if (!b->level || flags == MAP_ALL_NODES)
2400 ret = fn(op, b);
2401
2402 return ret;
2403}
2404
2405int __bch_btree_map_nodes(struct btree_op *op, struct cache_set *c,
2406 struct bkey *from, btree_map_nodes_fn *fn, int flags)
2407{
Kent Overstreetb54d6932013-07-24 18:04:18 -07002408 return btree_root(map_nodes_recurse, c, op, from, fn, flags);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002409}
2410
2411static int bch_btree_map_keys_recurse(struct btree *b, struct btree_op *op,
2412 struct bkey *from, btree_map_keys_fn *fn,
2413 int flags)
2414{
2415 int ret = MAP_CONTINUE;
2416 struct bkey *k;
2417 struct btree_iter iter;
2418
2419 bch_btree_iter_init(b, &iter, from);
2420
2421 while ((k = bch_btree_iter_next_filter(&iter, b, bch_ptr_bad))) {
2422 ret = !b->level
2423 ? fn(op, b, k)
2424 : btree(map_keys_recurse, k, b, op, from, fn, flags);
2425 from = NULL;
2426
2427 if (ret != MAP_CONTINUE)
2428 return ret;
2429 }
2430
2431 if (!b->level && (flags & MAP_END_KEY))
2432 ret = fn(op, b, &KEY(KEY_INODE(&b->key),
2433 KEY_OFFSET(&b->key), 0));
2434
2435 return ret;
2436}
2437
2438int bch_btree_map_keys(struct btree_op *op, struct cache_set *c,
2439 struct bkey *from, btree_map_keys_fn *fn, int flags)
2440{
Kent Overstreetb54d6932013-07-24 18:04:18 -07002441 return btree_root(map_keys_recurse, c, op, from, fn, flags);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002442}
2443
Kent Overstreetcafe5632013-03-23 16:11:31 -07002444/* Keybuf code */
2445
2446static inline int keybuf_cmp(struct keybuf_key *l, struct keybuf_key *r)
2447{
2448 /* Overlapping keys compare equal */
2449 if (bkey_cmp(&l->key, &START_KEY(&r->key)) <= 0)
2450 return -1;
2451 if (bkey_cmp(&START_KEY(&l->key), &r->key) >= 0)
2452 return 1;
2453 return 0;
2454}
2455
2456static inline int keybuf_nonoverlapping_cmp(struct keybuf_key *l,
2457 struct keybuf_key *r)
2458{
2459 return clamp_t(int64_t, bkey_cmp(&l->key, &r->key), -1, 1);
2460}
2461
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002462struct refill {
2463 struct btree_op op;
Kent Overstreet48a915a2013-10-31 15:43:22 -07002464 unsigned nr_found;
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002465 struct keybuf *buf;
2466 struct bkey *end;
2467 keybuf_pred_fn *pred;
2468};
2469
2470static int refill_keybuf_fn(struct btree_op *op, struct btree *b,
2471 struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002472{
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002473 struct refill *refill = container_of(op, struct refill, op);
2474 struct keybuf *buf = refill->buf;
2475 int ret = MAP_CONTINUE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002476
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002477 if (bkey_cmp(k, refill->end) >= 0) {
2478 ret = MAP_DONE;
2479 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002480 }
2481
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002482 if (!KEY_SIZE(k)) /* end key */
2483 goto out;
2484
2485 if (refill->pred(buf, k)) {
2486 struct keybuf_key *w;
2487
2488 spin_lock(&buf->lock);
2489
2490 w = array_alloc(&buf->freelist);
2491 if (!w) {
2492 spin_unlock(&buf->lock);
2493 return MAP_DONE;
2494 }
2495
2496 w->private = NULL;
2497 bkey_copy(&w->key, k);
2498
2499 if (RB_INSERT(&buf->keys, w, node, keybuf_cmp))
2500 array_free(&buf->freelist, w);
Kent Overstreet48a915a2013-10-31 15:43:22 -07002501 else
2502 refill->nr_found++;
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002503
2504 if (array_freelist_empty(&buf->freelist))
2505 ret = MAP_DONE;
2506
2507 spin_unlock(&buf->lock);
2508 }
2509out:
2510 buf->last_scanned = *k;
2511 return ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002512}
2513
2514void bch_refill_keybuf(struct cache_set *c, struct keybuf *buf,
Kent Overstreet72c27062013-06-05 06:24:39 -07002515 struct bkey *end, keybuf_pred_fn *pred)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002516{
2517 struct bkey start = buf->last_scanned;
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002518 struct refill refill;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002519
2520 cond_resched();
2521
Kent Overstreetb54d6932013-07-24 18:04:18 -07002522 bch_btree_op_init(&refill.op, -1);
Kent Overstreet48a915a2013-10-31 15:43:22 -07002523 refill.nr_found = 0;
2524 refill.buf = buf;
2525 refill.end = end;
2526 refill.pred = pred;
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002527
2528 bch_btree_map_keys(&refill.op, c, &buf->last_scanned,
2529 refill_keybuf_fn, MAP_END_KEY);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002530
Kent Overstreet48a915a2013-10-31 15:43:22 -07002531 trace_bcache_keyscan(refill.nr_found,
2532 KEY_INODE(&start), KEY_OFFSET(&start),
2533 KEY_INODE(&buf->last_scanned),
2534 KEY_OFFSET(&buf->last_scanned));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002535
2536 spin_lock(&buf->lock);
2537
2538 if (!RB_EMPTY_ROOT(&buf->keys)) {
2539 struct keybuf_key *w;
2540 w = RB_FIRST(&buf->keys, struct keybuf_key, node);
2541 buf->start = START_KEY(&w->key);
2542
2543 w = RB_LAST(&buf->keys, struct keybuf_key, node);
2544 buf->end = w->key;
2545 } else {
2546 buf->start = MAX_KEY;
2547 buf->end = MAX_KEY;
2548 }
2549
2550 spin_unlock(&buf->lock);
2551}
2552
2553static void __bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w)
2554{
2555 rb_erase(&w->node, &buf->keys);
2556 array_free(&buf->freelist, w);
2557}
2558
2559void bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w)
2560{
2561 spin_lock(&buf->lock);
2562 __bch_keybuf_del(buf, w);
2563 spin_unlock(&buf->lock);
2564}
2565
2566bool bch_keybuf_check_overlapping(struct keybuf *buf, struct bkey *start,
2567 struct bkey *end)
2568{
2569 bool ret = false;
2570 struct keybuf_key *p, *w, s;
2571 s.key = *start;
2572
2573 if (bkey_cmp(end, &buf->start) <= 0 ||
2574 bkey_cmp(start, &buf->end) >= 0)
2575 return false;
2576
2577 spin_lock(&buf->lock);
2578 w = RB_GREATER(&buf->keys, s, node, keybuf_nonoverlapping_cmp);
2579
2580 while (w && bkey_cmp(&START_KEY(&w->key), end) < 0) {
2581 p = w;
2582 w = RB_NEXT(w, node);
2583
2584 if (p->private)
2585 ret = true;
2586 else
2587 __bch_keybuf_del(buf, p);
2588 }
2589
2590 spin_unlock(&buf->lock);
2591 return ret;
2592}
2593
2594struct keybuf_key *bch_keybuf_next(struct keybuf *buf)
2595{
2596 struct keybuf_key *w;
2597 spin_lock(&buf->lock);
2598
2599 w = RB_FIRST(&buf->keys, struct keybuf_key, node);
2600
2601 while (w && w->private)
2602 w = RB_NEXT(w, node);
2603
2604 if (w)
2605 w->private = ERR_PTR(-EINTR);
2606
2607 spin_unlock(&buf->lock);
2608 return w;
2609}
2610
2611struct keybuf_key *bch_keybuf_next_rescan(struct cache_set *c,
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002612 struct keybuf *buf,
2613 struct bkey *end,
2614 keybuf_pred_fn *pred)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002615{
2616 struct keybuf_key *ret;
2617
2618 while (1) {
2619 ret = bch_keybuf_next(buf);
2620 if (ret)
2621 break;
2622
2623 if (bkey_cmp(&buf->last_scanned, end) >= 0) {
2624 pr_debug("scan finished");
2625 break;
2626 }
2627
Kent Overstreet72c27062013-06-05 06:24:39 -07002628 bch_refill_keybuf(c, buf, end, pred);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002629 }
2630
2631 return ret;
2632}
2633
Kent Overstreet72c27062013-06-05 06:24:39 -07002634void bch_keybuf_init(struct keybuf *buf)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002635{
Kent Overstreetcafe5632013-03-23 16:11:31 -07002636 buf->last_scanned = MAX_KEY;
2637 buf->keys = RB_ROOT;
2638
2639 spin_lock_init(&buf->lock);
2640 array_allocator_init(&buf->freelist);
2641}
2642
2643void bch_btree_exit(void)
2644{
2645 if (btree_io_wq)
2646 destroy_workqueue(btree_io_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002647}
2648
2649int __init bch_btree_init(void)
2650{
Kent Overstreet72a44512013-10-24 17:19:26 -07002651 btree_io_wq = create_singlethread_workqueue("bch_btree_io");
2652 if (!btree_io_wq)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002653 return -ENOMEM;
2654
2655 return 0;
2656}