blob: e83732e2d912d6f1ee46334ae63e7fc92485b56f [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 Overstreet65d45232013-12-20 17:22:05 -080026#include "extents.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 *
Kent Overstreetcafe5632013-03-23 16:11:31 -070071 * Plugging?
72 *
73 * If data write is less than hard sector size of ssd, round up offset in open
74 * bucket to the next whole sector
75 *
Kent Overstreetcafe5632013-03-23 16:11:31 -070076 * Superblock needs to be fleshed out for multiple cache devices
77 *
78 * Add a sysfs tunable for the number of writeback IOs in flight
79 *
80 * Add a sysfs tunable for the number of open data buckets
81 *
82 * IO tracking: Can we track when one process is doing io on behalf of another?
83 * IO tracking: Don't use just an average, weigh more recent stuff higher
84 *
85 * Test module load/unload
86 */
87
Kent Overstreetcafe5632013-03-23 16:11:31 -070088#define MAX_NEED_GC 64
89#define MAX_SAVE_PRIO 72
90
91#define PTR_DIRTY_BIT (((uint64_t) 1 << 36))
92
93#define PTR_HASH(c, k) \
94 (((k)->ptr[0] >> c->bucket_bits) | PTR_GEN(k, 0))
95
Kent Overstreetcafe5632013-03-23 16:11:31 -070096static struct workqueue_struct *btree_io_wq;
97
Kent Overstreetdf8e8972013-07-24 17:37:59 -070098#define insert_lock(s, b) ((b)->level <= (s)->lock)
99
100/*
101 * These macros are for recursing down the btree - they handle the details of
102 * locking and looking up nodes in the cache for you. They're best treated as
103 * mere syntax when reading code that uses them.
104 *
105 * op->lock determines whether we take a read or a write lock at a given depth.
106 * If you've got a read lock and find that you need a write lock (i.e. you're
107 * going to have to split), set op->lock and return -EINTR; btree_root() will
108 * call you again and you'll have the correct lock.
109 */
110
111/**
112 * btree - recurse down the btree on a specified key
113 * @fn: function to call, which will be passed the child node
114 * @key: key to recurse on
115 * @b: parent btree node
116 * @op: pointer to struct btree_op
117 */
118#define btree(fn, key, b, op, ...) \
119({ \
120 int _r, l = (b)->level - 1; \
121 bool _w = l <= (op)->lock; \
122 struct btree *_child = bch_btree_node_get((b)->c, key, l, _w); \
123 if (!IS_ERR(_child)) { \
124 _child->parent = (b); \
125 _r = bch_btree_ ## fn(_child, op, ##__VA_ARGS__); \
126 rw_unlock(_w, _child); \
127 } else \
128 _r = PTR_ERR(_child); \
129 _r; \
130})
131
132/**
133 * btree_root - call a function on the root of the btree
134 * @fn: function to call, which will be passed the child node
135 * @c: cache set
136 * @op: pointer to struct btree_op
137 */
138#define btree_root(fn, c, op, ...) \
139({ \
140 int _r = -EINTR; \
141 do { \
142 struct btree *_b = (c)->root; \
143 bool _w = insert_lock(op, _b); \
144 rw_lock(_w, _b, _b->level); \
145 if (_b == (c)->root && \
146 _w == insert_lock(op, _b)) { \
147 _b->parent = NULL; \
148 _r = bch_btree_ ## fn(_b, op, ##__VA_ARGS__); \
149 } \
150 rw_unlock(_w, _b); \
Kent Overstreet78365412013-12-17 01:29:34 -0800151 if (_r == -EINTR) \
152 schedule(); \
Kent Overstreetdf8e8972013-07-24 17:37:59 -0700153 bch_cannibalize_unlock(c); \
154 if (_r == -ENOSPC) { \
155 wait_event((c)->try_wait, \
156 !(c)->try_harder); \
157 _r = -EINTR; \
158 } \
159 } while (_r == -EINTR); \
160 \
Kent Overstreet78365412013-12-17 01:29:34 -0800161 finish_wait(&(c)->bucket_wait, &(op)->wait); \
Kent Overstreetdf8e8972013-07-24 17:37:59 -0700162 _r; \
163})
164
Kent Overstreeta85e9682013-12-20 17:28:16 -0800165static inline struct bset *write_block(struct btree *b)
166{
167 return ((void *) btree_bset_first(b)) + b->written * block_bytes(b->c);
168}
169
Kent Overstreetcafe5632013-03-23 16:11:31 -0700170/* Btree key manipulation */
171
Kent Overstreet3a3b6a42013-07-24 16:46:42 -0700172void bkey_put(struct cache_set *c, struct bkey *k)
Kent Overstreete7c590e2013-09-10 18:39:16 -0700173{
174 unsigned i;
175
176 for (i = 0; i < KEY_PTRS(k); i++)
177 if (ptr_available(c, k, i))
178 atomic_dec_bug(&PTR_BUCKET(c, k, i)->pin);
179}
180
Kent Overstreetcafe5632013-03-23 16:11:31 -0700181/* Btree IO */
182
183static uint64_t btree_csum_set(struct btree *b, struct bset *i)
184{
185 uint64_t crc = b->key.ptr[0];
Kent Overstreetfafff812013-12-17 21:56:21 -0800186 void *data = (void *) i + 8, *end = bset_bkey_last(i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700187
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600188 crc = bch_crc64_update(crc, data, end - data);
Kent Overstreetc19ed232013-03-26 13:49:02 -0700189 return crc ^ 0xffffffffffffffffULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700190}
191
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800192void bch_btree_node_read_done(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700193{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700194 const char *err = "bad btree header";
Kent Overstreetee811282013-12-17 23:49:49 -0800195 struct bset *i = btree_bset_first(b);
Kent Overstreet57943512013-04-25 13:58:35 -0700196 struct btree_iter *iter;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700197
Kent Overstreet57943512013-04-25 13:58:35 -0700198 iter = mempool_alloc(b->c->fill_iter, GFP_NOWAIT);
199 iter->size = b->c->sb.bucket_size / b->c->sb.block_size;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700200 iter->used = 0;
201
Kent Overstreet280481d2013-10-24 16:36:03 -0700202#ifdef CONFIG_BCACHE_DEBUG
Kent Overstreetc052dd92013-11-11 17:35:24 -0800203 iter->b = &b->keys;
Kent Overstreet280481d2013-10-24 16:36:03 -0700204#endif
205
Kent Overstreet57943512013-04-25 13:58:35 -0700206 if (!i->seq)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700207 goto err;
208
209 for (;
Kent Overstreeta85e9682013-12-20 17:28:16 -0800210 b->written < btree_blocks(b) && i->seq == b->keys.set[0].data->seq;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700211 i = write_block(b)) {
212 err = "unsupported bset version";
213 if (i->version > BCACHE_BSET_VERSION)
214 goto err;
215
216 err = "bad btree header";
Kent Overstreetee811282013-12-17 23:49:49 -0800217 if (b->written + set_blocks(i, block_bytes(b->c)) >
218 btree_blocks(b))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700219 goto err;
220
221 err = "bad magic";
Kent Overstreet81ab4192013-10-31 15:46:42 -0700222 if (i->magic != bset_magic(&b->c->sb))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700223 goto err;
224
225 err = "bad checksum";
226 switch (i->version) {
227 case 0:
228 if (i->csum != csum_set(i))
229 goto err;
230 break;
231 case BCACHE_BSET_VERSION:
232 if (i->csum != btree_csum_set(b, i))
233 goto err;
234 break;
235 }
236
237 err = "empty set";
Kent Overstreeta85e9682013-12-20 17:28:16 -0800238 if (i != b->keys.set[0].data && !i->keys)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700239 goto err;
240
Kent Overstreetfafff812013-12-17 21:56:21 -0800241 bch_btree_iter_push(iter, i->start, bset_bkey_last(i));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700242
Kent Overstreetee811282013-12-17 23:49:49 -0800243 b->written += set_blocks(i, block_bytes(b->c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700244 }
245
246 err = "corrupted btree";
247 for (i = write_block(b);
Kent Overstreeta85e9682013-12-20 17:28:16 -0800248 bset_sector_offset(&b->keys, i) < KEY_SIZE(&b->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700249 i = ((void *) i) + block_bytes(b->c))
Kent Overstreeta85e9682013-12-20 17:28:16 -0800250 if (i->seq == b->keys.set[0].data->seq)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700251 goto err;
252
Kent Overstreeta85e9682013-12-20 17:28:16 -0800253 bch_btree_sort_and_fix_extents(&b->keys, iter, &b->c->sort);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700254
Kent Overstreeta85e9682013-12-20 17:28:16 -0800255 i = b->keys.set[0].data;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700256 err = "short btree key";
Kent Overstreeta85e9682013-12-20 17:28:16 -0800257 if (b->keys.set[0].size &&
258 bkey_cmp(&b->key, &b->keys.set[0].end) < 0)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700259 goto err;
260
261 if (b->written < btree_blocks(b))
Kent Overstreeta85e9682013-12-20 17:28:16 -0800262 bch_bset_init_next(&b->keys, write_block(b),
263 bset_magic(&b->c->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700264out:
Kent Overstreet57943512013-04-25 13:58:35 -0700265 mempool_free(iter, b->c->fill_iter);
266 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700267err:
268 set_btree_node_io_error(b);
Kent Overstreet88b9f8c2013-12-17 21:46:35 -0800269 bch_cache_set_error(b->c, "%s at bucket %zu, block %u, %u keys",
Kent Overstreetcafe5632013-03-23 16:11:31 -0700270 err, PTR_BUCKET_NR(b->c, &b->key, 0),
Kent Overstreet88b9f8c2013-12-17 21:46:35 -0800271 bset_block_offset(b, i), i->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700272 goto out;
273}
274
Kent Overstreet57943512013-04-25 13:58:35 -0700275static void btree_node_read_endio(struct bio *bio, int error)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700276{
Kent Overstreet57943512013-04-25 13:58:35 -0700277 struct closure *cl = bio->bi_private;
278 closure_put(cl);
279}
Kent Overstreetcafe5632013-03-23 16:11:31 -0700280
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800281static void bch_btree_node_read(struct btree *b)
Kent Overstreet57943512013-04-25 13:58:35 -0700282{
283 uint64_t start_time = local_clock();
284 struct closure cl;
285 struct bio *bio;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700286
Kent Overstreetc37511b2013-04-26 15:39:55 -0700287 trace_bcache_btree_read(b);
288
Kent Overstreet57943512013-04-25 13:58:35 -0700289 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700290
Kent Overstreet57943512013-04-25 13:58:35 -0700291 bio = bch_bbio_alloc(b->c);
292 bio->bi_rw = REQ_META|READ_SYNC;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700293 bio->bi_iter.bi_size = KEY_SIZE(&b->key) << 9;
Kent Overstreet57943512013-04-25 13:58:35 -0700294 bio->bi_end_io = btree_node_read_endio;
295 bio->bi_private = &cl;
296
Kent Overstreeta85e9682013-12-20 17:28:16 -0800297 bch_bio_map(bio, b->keys.set[0].data);
Kent Overstreet57943512013-04-25 13:58:35 -0700298
Kent Overstreet57943512013-04-25 13:58:35 -0700299 bch_submit_bbio(bio, b->c, &b->key, 0);
300 closure_sync(&cl);
301
302 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
303 set_btree_node_io_error(b);
304
305 bch_bbio_free(bio, b->c);
306
307 if (btree_node_io_error(b))
308 goto err;
309
310 bch_btree_node_read_done(b);
Kent Overstreet57943512013-04-25 13:58:35 -0700311 bch_time_stats_update(&b->c->btree_read_time, start_time);
Kent Overstreet57943512013-04-25 13:58:35 -0700312
313 return;
314err:
Geert Uytterhoeven61cbd252013-09-23 23:17:30 -0700315 bch_cache_set_error(b->c, "io error reading bucket %zu",
Kent Overstreet57943512013-04-25 13:58:35 -0700316 PTR_BUCKET_NR(b->c, &b->key, 0));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700317}
318
319static void btree_complete_write(struct btree *b, struct btree_write *w)
320{
321 if (w->prio_blocked &&
322 !atomic_sub_return(w->prio_blocked, &b->c->prio_blocked))
Kent Overstreet119ba0f2013-04-24 19:01:12 -0700323 wake_up_allocators(b->c);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700324
325 if (w->journal) {
326 atomic_dec_bug(w->journal);
327 __closure_wake_up(&b->c->journal.wait);
328 }
329
Kent Overstreetcafe5632013-03-23 16:11:31 -0700330 w->prio_blocked = 0;
331 w->journal = NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700332}
333
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800334static void btree_node_write_unlock(struct closure *cl)
335{
336 struct btree *b = container_of(cl, struct btree, io);
337
338 up(&b->io_mutex);
339}
340
Kent Overstreet57943512013-04-25 13:58:35 -0700341static void __btree_node_write_done(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700342{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800343 struct btree *b = container_of(cl, struct btree, io);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700344 struct btree_write *w = btree_prev_write(b);
345
346 bch_bbio_free(b->bio, b->c);
347 b->bio = NULL;
348 btree_complete_write(b, w);
349
350 if (btree_node_dirty(b))
351 queue_delayed_work(btree_io_wq, &b->work,
352 msecs_to_jiffies(30000));
353
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800354 closure_return_with_destructor(cl, btree_node_write_unlock);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700355}
356
Kent Overstreet57943512013-04-25 13:58:35 -0700357static void btree_node_write_done(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700358{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800359 struct btree *b = container_of(cl, struct btree, io);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700360 struct bio_vec *bv;
361 int n;
362
Kent Overstreet79886132013-11-23 17:19:00 -0800363 bio_for_each_segment_all(bv, b->bio, n)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700364 __free_page(bv->bv_page);
365
Kent Overstreet57943512013-04-25 13:58:35 -0700366 __btree_node_write_done(cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700367}
368
Kent Overstreet57943512013-04-25 13:58:35 -0700369static void btree_node_write_endio(struct bio *bio, int error)
370{
371 struct closure *cl = bio->bi_private;
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800372 struct btree *b = container_of(cl, struct btree, io);
Kent Overstreet57943512013-04-25 13:58:35 -0700373
374 if (error)
375 set_btree_node_io_error(b);
376
377 bch_bbio_count_io_errors(b->c, bio, error, "writing btree");
378 closure_put(cl);
379}
380
381static void do_btree_node_write(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700382{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800383 struct closure *cl = &b->io;
Kent Overstreetee811282013-12-17 23:49:49 -0800384 struct bset *i = btree_bset_last(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700385 BKEY_PADDED(key) k;
386
387 i->version = BCACHE_BSET_VERSION;
388 i->csum = btree_csum_set(b, i);
389
Kent Overstreet57943512013-04-25 13:58:35 -0700390 BUG_ON(b->bio);
391 b->bio = bch_bbio_alloc(b->c);
392
393 b->bio->bi_end_io = btree_node_write_endio;
Kent Overstreetfaadf0c2013-11-01 18:03:08 -0700394 b->bio->bi_private = cl;
Kent Overstreete49c7c32013-06-26 17:25:38 -0700395 b->bio->bi_rw = REQ_META|WRITE_SYNC|REQ_FUA;
Kent Overstreetee811282013-12-17 23:49:49 -0800396 b->bio->bi_iter.bi_size = roundup(set_bytes(i), block_bytes(b->c));
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600397 bch_bio_map(b->bio, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700398
Kent Overstreete49c7c32013-06-26 17:25:38 -0700399 /*
400 * If we're appending to a leaf node, we don't technically need FUA -
401 * this write just needs to be persisted before the next journal write,
402 * which will be marked FLUSH|FUA.
403 *
404 * Similarly if we're writing a new btree root - the pointer is going to
405 * be in the next journal entry.
406 *
407 * But if we're writing a new btree node (that isn't a root) or
408 * appending to a non leaf btree node, we need either FUA or a flush
409 * when we write the parent with the new pointer. FUA is cheaper than a
410 * flush, and writes appending to leaf nodes aren't blocking anything so
411 * just make all btree node writes FUA to keep things sane.
412 */
413
Kent Overstreetcafe5632013-03-23 16:11:31 -0700414 bkey_copy(&k.key, &b->key);
Kent Overstreetee811282013-12-17 23:49:49 -0800415 SET_PTR_OFFSET(&k.key, 0, PTR_OFFSET(&k.key, 0) +
Kent Overstreeta85e9682013-12-20 17:28:16 -0800416 bset_sector_offset(&b->keys, i));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700417
Kent Overstreet8e51e412013-06-06 18:15:57 -0700418 if (!bio_alloc_pages(b->bio, GFP_NOIO)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700419 int j;
420 struct bio_vec *bv;
421 void *base = (void *) ((unsigned long) i & ~(PAGE_SIZE - 1));
422
Kent Overstreet79886132013-11-23 17:19:00 -0800423 bio_for_each_segment_all(bv, b->bio, j)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700424 memcpy(page_address(bv->bv_page),
425 base + j * PAGE_SIZE, PAGE_SIZE);
426
Kent Overstreetcafe5632013-03-23 16:11:31 -0700427 bch_submit_bbio(b->bio, b->c, &k.key, 0);
428
Kent Overstreet57943512013-04-25 13:58:35 -0700429 continue_at(cl, btree_node_write_done, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700430 } else {
431 b->bio->bi_vcnt = 0;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600432 bch_bio_map(b->bio, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700433
Kent Overstreetcafe5632013-03-23 16:11:31 -0700434 bch_submit_bbio(b->bio, b->c, &k.key, 0);
435
436 closure_sync(cl);
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800437 continue_at_nobarrier(cl, __btree_node_write_done, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700438 }
439}
440
Kent Overstreet57943512013-04-25 13:58:35 -0700441void bch_btree_node_write(struct btree *b, struct closure *parent)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700442{
Kent Overstreetee811282013-12-17 23:49:49 -0800443 struct bset *i = btree_bset_last(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700444
Kent Overstreetc37511b2013-04-26 15:39:55 -0700445 trace_bcache_btree_write(b);
446
Kent Overstreetcafe5632013-03-23 16:11:31 -0700447 BUG_ON(current->bio_list);
Kent Overstreet57943512013-04-25 13:58:35 -0700448 BUG_ON(b->written >= btree_blocks(b));
449 BUG_ON(b->written && !i->keys);
Kent Overstreetee811282013-12-17 23:49:49 -0800450 BUG_ON(btree_bset_first(b)->seq != i->seq);
Kent Overstreetdc9d98d2013-12-17 23:47:33 -0800451 bch_check_keys(&b->keys, "writing");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700452
Kent Overstreetcafe5632013-03-23 16:11:31 -0700453 cancel_delayed_work(&b->work);
454
Kent Overstreet57943512013-04-25 13:58:35 -0700455 /* If caller isn't waiting for write, parent refcount is cache set */
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800456 down(&b->io_mutex);
457 closure_init(&b->io, parent ?: &b->c->cl);
Kent Overstreet57943512013-04-25 13:58:35 -0700458
Kent Overstreetcafe5632013-03-23 16:11:31 -0700459 clear_bit(BTREE_NODE_dirty, &b->flags);
460 change_bit(BTREE_NODE_write_idx, &b->flags);
461
Kent Overstreet57943512013-04-25 13:58:35 -0700462 do_btree_node_write(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700463
Kent Overstreetee811282013-12-17 23:49:49 -0800464 atomic_long_add(set_blocks(i, block_bytes(b->c)) * b->c->sb.block_size,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700465 &PTR_CACHE(b->c, &b->key, 0)->btree_sectors_written);
466
Kent Overstreeta85e9682013-12-20 17:28:16 -0800467 b->written += set_blocks(i, block_bytes(b->c));
468
Kent Overstreet67539e82013-09-10 22:53:34 -0700469 /* If not a leaf node, always sort */
Kent Overstreeta85e9682013-12-20 17:28:16 -0800470 if (b->level && b->keys.nsets)
Kent Overstreet89ebb4a2013-11-11 18:38:51 -0800471 bch_btree_sort(&b->keys, &b->c->sort);
Kent Overstreet67539e82013-09-10 22:53:34 -0700472 else
Kent Overstreet89ebb4a2013-11-11 18:38:51 -0800473 bch_btree_sort_lazy(&b->keys, &b->c->sort);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700474
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800475 /*
476 * do verify if there was more than one set initially (i.e. we did a
477 * sort) and we sorted down to a single set:
478 */
Kent Overstreeta85e9682013-12-20 17:28:16 -0800479 if (i != b->keys.set->data && !b->keys.nsets)
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800480 bch_btree_verify(b);
481
Kent Overstreetcafe5632013-03-23 16:11:31 -0700482 if (b->written < btree_blocks(b))
Kent Overstreeta85e9682013-12-20 17:28:16 -0800483 bch_bset_init_next(&b->keys, write_block(b),
484 bset_magic(&b->c->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700485}
486
Kent Overstreetf269af52013-07-23 20:48:29 -0700487static void bch_btree_node_write_sync(struct btree *b)
488{
489 struct closure cl;
490
491 closure_init_stack(&cl);
492 bch_btree_node_write(b, &cl);
493 closure_sync(&cl);
494}
495
Kent Overstreet57943512013-04-25 13:58:35 -0700496static void btree_node_write_work(struct work_struct *w)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700497{
498 struct btree *b = container_of(to_delayed_work(w), struct btree, work);
499
Kent Overstreet57943512013-04-25 13:58:35 -0700500 rw_lock(true, b, b->level);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700501
502 if (btree_node_dirty(b))
Kent Overstreet57943512013-04-25 13:58:35 -0700503 bch_btree_node_write(b, NULL);
504 rw_unlock(true, b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700505}
506
Kent Overstreetc18536a2013-07-24 17:44:17 -0700507static void bch_btree_leaf_dirty(struct btree *b, atomic_t *journal_ref)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700508{
Kent Overstreetee811282013-12-17 23:49:49 -0800509 struct bset *i = btree_bset_last(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700510 struct btree_write *w = btree_current_write(b);
511
Kent Overstreet57943512013-04-25 13:58:35 -0700512 BUG_ON(!b->written);
513 BUG_ON(!i->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700514
Kent Overstreet57943512013-04-25 13:58:35 -0700515 if (!btree_node_dirty(b))
516 queue_delayed_work(btree_io_wq, &b->work, 30 * HZ);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700517
Kent Overstreet57943512013-04-25 13:58:35 -0700518 set_btree_node_dirty(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700519
Kent Overstreetc18536a2013-07-24 17:44:17 -0700520 if (journal_ref) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700521 if (w->journal &&
Kent Overstreetc18536a2013-07-24 17:44:17 -0700522 journal_pin_cmp(b->c, w->journal, journal_ref)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700523 atomic_dec_bug(w->journal);
524 w->journal = NULL;
525 }
526
527 if (!w->journal) {
Kent Overstreetc18536a2013-07-24 17:44:17 -0700528 w->journal = journal_ref;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700529 atomic_inc(w->journal);
530 }
531 }
532
Kent Overstreetcafe5632013-03-23 16:11:31 -0700533 /* Force write if set is too big */
Kent Overstreet57943512013-04-25 13:58:35 -0700534 if (set_bytes(i) > PAGE_SIZE - 48 &&
535 !current->bio_list)
536 bch_btree_node_write(b, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700537}
538
539/*
540 * Btree in memory cache - allocation/freeing
541 * mca -> memory cache
542 */
543
Kent Overstreetcafe5632013-03-23 16:11:31 -0700544#define mca_reserve(c) (((c->root && c->root->level) \
545 ? c->root->level : 1) * 8 + 16)
546#define mca_can_free(c) \
547 max_t(int, 0, c->bucket_cache_used - mca_reserve(c))
548
549static void mca_data_free(struct btree *b)
550{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800551 BUG_ON(b->io_mutex.count != 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700552
Kent Overstreeta85e9682013-12-20 17:28:16 -0800553 bch_btree_keys_free(&b->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700554
Kent Overstreetcafe5632013-03-23 16:11:31 -0700555 b->c->bucket_cache_used--;
Kent Overstreetee811282013-12-17 23:49:49 -0800556 list_move(&b->list, &b->c->btree_cache_freed);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700557}
558
559static void mca_bucket_free(struct btree *b)
560{
561 BUG_ON(btree_node_dirty(b));
562
563 b->key.ptr[0] = 0;
564 hlist_del_init_rcu(&b->hash);
565 list_move(&b->list, &b->c->btree_cache_freeable);
566}
567
568static unsigned btree_order(struct bkey *k)
569{
570 return ilog2(KEY_SIZE(k) / PAGE_SECTORS ?: 1);
571}
572
573static void mca_data_alloc(struct btree *b, struct bkey *k, gfp_t gfp)
574{
Kent Overstreeta85e9682013-12-20 17:28:16 -0800575 if (!bch_btree_keys_alloc(&b->keys,
Kent Overstreetee811282013-12-17 23:49:49 -0800576 max_t(unsigned,
577 ilog2(b->c->btree_pages),
578 btree_order(k)),
579 gfp)) {
580 b->c->bucket_cache_used++;
581 list_move(&b->list, &b->c->btree_cache);
582 } else {
583 list_move(&b->list, &b->c->btree_cache_freed);
584 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700585}
586
587static struct btree *mca_bucket_alloc(struct cache_set *c,
588 struct bkey *k, gfp_t gfp)
589{
590 struct btree *b = kzalloc(sizeof(struct btree), gfp);
591 if (!b)
592 return NULL;
593
594 init_rwsem(&b->lock);
595 lockdep_set_novalidate_class(&b->lock);
596 INIT_LIST_HEAD(&b->list);
Kent Overstreet57943512013-04-25 13:58:35 -0700597 INIT_DELAYED_WORK(&b->work, btree_node_write_work);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700598 b->c = c;
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800599 sema_init(&b->io_mutex, 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700600
601 mca_data_alloc(b, k, gfp);
602 return b;
603}
604
Kent Overstreete8e1d462013-07-24 17:27:07 -0700605static int mca_reap(struct btree *b, unsigned min_order, bool flush)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700606{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700607 struct closure cl;
608
609 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700610 lockdep_assert_held(&b->c->bucket_lock);
611
612 if (!down_write_trylock(&b->lock))
613 return -ENOMEM;
614
Kent Overstreeta85e9682013-12-20 17:28:16 -0800615 BUG_ON(btree_node_dirty(b) && !b->keys.set[0].data);
Kent Overstreete8e1d462013-07-24 17:27:07 -0700616
Kent Overstreeta85e9682013-12-20 17:28:16 -0800617 if (b->keys.page_order < min_order)
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800618 goto out_unlock;
619
620 if (!flush) {
621 if (btree_node_dirty(b))
622 goto out_unlock;
623
624 if (down_trylock(&b->io_mutex))
625 goto out_unlock;
626 up(&b->io_mutex);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700627 }
628
Kent Overstreetf269af52013-07-23 20:48:29 -0700629 if (btree_node_dirty(b))
630 bch_btree_node_write_sync(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700631
Kent Overstreete8e1d462013-07-24 17:27:07 -0700632 /* wait for any in flight btree write */
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800633 down(&b->io_mutex);
634 up(&b->io_mutex);
Kent Overstreete8e1d462013-07-24 17:27:07 -0700635
Kent Overstreetcafe5632013-03-23 16:11:31 -0700636 return 0;
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800637out_unlock:
638 rw_unlock(true, b);
639 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700640}
641
Dave Chinner7dc19d52013-08-28 10:18:11 +1000642static unsigned long bch_mca_scan(struct shrinker *shrink,
643 struct shrink_control *sc)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700644{
645 struct cache_set *c = container_of(shrink, struct cache_set, shrink);
646 struct btree *b, *t;
647 unsigned long i, nr = sc->nr_to_scan;
Dave Chinner7dc19d52013-08-28 10:18:11 +1000648 unsigned long freed = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700649
650 if (c->shrinker_disabled)
Dave Chinner7dc19d52013-08-28 10:18:11 +1000651 return SHRINK_STOP;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700652
653 if (c->try_harder)
Dave Chinner7dc19d52013-08-28 10:18:11 +1000654 return SHRINK_STOP;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700655
656 /* Return -1 if we can't do anything right now */
Kent Overstreeta698e082013-09-23 23:17:34 -0700657 if (sc->gfp_mask & __GFP_IO)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700658 mutex_lock(&c->bucket_lock);
659 else if (!mutex_trylock(&c->bucket_lock))
660 return -1;
661
Kent Overstreet36c9ea92013-06-03 13:04:56 -0700662 /*
663 * It's _really_ critical that we don't free too many btree nodes - we
664 * have to always leave ourselves a reserve. The reserve is how we
665 * guarantee that allocating memory for a new btree node can always
666 * succeed, so that inserting keys into the btree can always succeed and
667 * IO can always make forward progress:
668 */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700669 nr /= c->btree_pages;
670 nr = min_t(unsigned long, nr, mca_can_free(c));
671
672 i = 0;
673 list_for_each_entry_safe(b, t, &c->btree_cache_freeable, list) {
Dave Chinner7dc19d52013-08-28 10:18:11 +1000674 if (freed >= nr)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700675 break;
676
677 if (++i > 3 &&
Kent Overstreete8e1d462013-07-24 17:27:07 -0700678 !mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700679 mca_data_free(b);
680 rw_unlock(true, b);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000681 freed++;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700682 }
683 }
684
Dave Chinner7dc19d52013-08-28 10:18:11 +1000685 for (i = 0; (nr--) && i < c->bucket_cache_used; i++) {
Kent Overstreetb0f32a52013-12-10 13:24:26 -0800686 if (list_empty(&c->btree_cache))
687 goto out;
688
Kent Overstreetcafe5632013-03-23 16:11:31 -0700689 b = list_first_entry(&c->btree_cache, struct btree, list);
690 list_rotate_left(&c->btree_cache);
691
692 if (!b->accessed &&
Kent Overstreete8e1d462013-07-24 17:27:07 -0700693 !mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700694 mca_bucket_free(b);
695 mca_data_free(b);
696 rw_unlock(true, b);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000697 freed++;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700698 } else
699 b->accessed = 0;
700 }
701out:
Kent Overstreetcafe5632013-03-23 16:11:31 -0700702 mutex_unlock(&c->bucket_lock);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000703 return freed;
704}
705
706static unsigned long bch_mca_count(struct shrinker *shrink,
707 struct shrink_control *sc)
708{
709 struct cache_set *c = container_of(shrink, struct cache_set, shrink);
710
711 if (c->shrinker_disabled)
712 return 0;
713
714 if (c->try_harder)
715 return 0;
716
717 return mca_can_free(c) * c->btree_pages;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700718}
719
720void bch_btree_cache_free(struct cache_set *c)
721{
722 struct btree *b;
723 struct closure cl;
724 closure_init_stack(&cl);
725
726 if (c->shrink.list.next)
727 unregister_shrinker(&c->shrink);
728
729 mutex_lock(&c->bucket_lock);
730
731#ifdef CONFIG_BCACHE_DEBUG
732 if (c->verify_data)
733 list_move(&c->verify_data->list, &c->btree_cache);
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800734
735 free_pages((unsigned long) c->verify_ondisk, ilog2(bucket_pages(c)));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700736#endif
737
738 list_splice(&c->btree_cache_freeable,
739 &c->btree_cache);
740
741 while (!list_empty(&c->btree_cache)) {
742 b = list_first_entry(&c->btree_cache, struct btree, list);
743
744 if (btree_node_dirty(b))
745 btree_complete_write(b, btree_current_write(b));
746 clear_bit(BTREE_NODE_dirty, &b->flags);
747
748 mca_data_free(b);
749 }
750
751 while (!list_empty(&c->btree_cache_freed)) {
752 b = list_first_entry(&c->btree_cache_freed,
753 struct btree, list);
754 list_del(&b->list);
755 cancel_delayed_work_sync(&b->work);
756 kfree(b);
757 }
758
759 mutex_unlock(&c->bucket_lock);
760}
761
762int bch_btree_cache_alloc(struct cache_set *c)
763{
764 unsigned i;
765
Kent Overstreetcafe5632013-03-23 16:11:31 -0700766 for (i = 0; i < mca_reserve(c); i++)
Kent Overstreet72a44512013-10-24 17:19:26 -0700767 if (!mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL))
768 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700769
770 list_splice_init(&c->btree_cache,
771 &c->btree_cache_freeable);
772
773#ifdef CONFIG_BCACHE_DEBUG
774 mutex_init(&c->verify_lock);
775
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800776 c->verify_ondisk = (void *)
777 __get_free_pages(GFP_KERNEL, ilog2(bucket_pages(c)));
778
Kent Overstreetcafe5632013-03-23 16:11:31 -0700779 c->verify_data = mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL);
780
781 if (c->verify_data &&
Kent Overstreeta85e9682013-12-20 17:28:16 -0800782 c->verify_data->keys.set->data)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700783 list_del_init(&c->verify_data->list);
784 else
785 c->verify_data = NULL;
786#endif
787
Dave Chinner7dc19d52013-08-28 10:18:11 +1000788 c->shrink.count_objects = bch_mca_count;
789 c->shrink.scan_objects = bch_mca_scan;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700790 c->shrink.seeks = 4;
791 c->shrink.batch = c->btree_pages * 2;
792 register_shrinker(&c->shrink);
793
794 return 0;
795}
796
797/* Btree in memory cache - hash table */
798
799static struct hlist_head *mca_hash(struct cache_set *c, struct bkey *k)
800{
801 return &c->bucket_hash[hash_32(PTR_HASH(c, k), BUCKET_HASH_BITS)];
802}
803
804static struct btree *mca_find(struct cache_set *c, struct bkey *k)
805{
806 struct btree *b;
807
808 rcu_read_lock();
809 hlist_for_each_entry_rcu(b, mca_hash(c, k), hash)
810 if (PTR_HASH(c, &b->key) == PTR_HASH(c, k))
811 goto out;
812 b = NULL;
813out:
814 rcu_read_unlock();
815 return b;
816}
817
Kent Overstreete8e1d462013-07-24 17:27:07 -0700818static struct btree *mca_cannibalize(struct cache_set *c, struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700819{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700820 struct btree *b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700821
Kent Overstreetc37511b2013-04-26 15:39:55 -0700822 trace_bcache_btree_cache_cannibalize(c);
823
Kent Overstreete8e1d462013-07-24 17:27:07 -0700824 if (!c->try_harder) {
825 c->try_harder = current;
826 c->try_harder_start = local_clock();
827 } else if (c->try_harder != current)
828 return ERR_PTR(-ENOSPC);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700829
Kent Overstreete8e1d462013-07-24 17:27:07 -0700830 list_for_each_entry_reverse(b, &c->btree_cache, list)
831 if (!mca_reap(b, btree_order(k), false))
832 return b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700833
Kent Overstreete8e1d462013-07-24 17:27:07 -0700834 list_for_each_entry_reverse(b, &c->btree_cache, list)
835 if (!mca_reap(b, btree_order(k), true))
836 return b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700837
Kent Overstreete8e1d462013-07-24 17:27:07 -0700838 return ERR_PTR(-ENOMEM);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700839}
840
841/*
842 * We can only have one thread cannibalizing other cached btree nodes at a time,
843 * or we'll deadlock. We use an open coded mutex to ensure that, which a
844 * cannibalize_bucket() will take. This means every time we unlock the root of
845 * the btree, we need to release this lock if we have it held.
846 */
Kent Overstreetdf8e8972013-07-24 17:37:59 -0700847static void bch_cannibalize_unlock(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700848{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700849 if (c->try_harder == current) {
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600850 bch_time_stats_update(&c->try_harder_time, c->try_harder_start);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700851 c->try_harder = NULL;
Kent Overstreete8e1d462013-07-24 17:27:07 -0700852 wake_up(&c->try_wait);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700853 }
854}
855
Kent Overstreete8e1d462013-07-24 17:27:07 -0700856static struct btree *mca_alloc(struct cache_set *c, struct bkey *k, int level)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700857{
858 struct btree *b;
859
Kent Overstreete8e1d462013-07-24 17:27:07 -0700860 BUG_ON(current->bio_list);
861
Kent Overstreetcafe5632013-03-23 16:11:31 -0700862 lockdep_assert_held(&c->bucket_lock);
863
864 if (mca_find(c, k))
865 return NULL;
866
867 /* btree_free() doesn't free memory; it sticks the node on the end of
868 * the list. Check if there's any freed nodes there:
869 */
870 list_for_each_entry(b, &c->btree_cache_freeable, list)
Kent Overstreete8e1d462013-07-24 17:27:07 -0700871 if (!mca_reap(b, btree_order(k), false))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700872 goto out;
873
874 /* We never free struct btree itself, just the memory that holds the on
875 * disk node. Check the freed list before allocating a new one:
876 */
877 list_for_each_entry(b, &c->btree_cache_freed, list)
Kent Overstreete8e1d462013-07-24 17:27:07 -0700878 if (!mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700879 mca_data_alloc(b, k, __GFP_NOWARN|GFP_NOIO);
Kent Overstreeta85e9682013-12-20 17:28:16 -0800880 if (!b->keys.set[0].data)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700881 goto err;
882 else
883 goto out;
884 }
885
886 b = mca_bucket_alloc(c, k, __GFP_NOWARN|GFP_NOIO);
887 if (!b)
888 goto err;
889
890 BUG_ON(!down_write_trylock(&b->lock));
Kent Overstreeta85e9682013-12-20 17:28:16 -0800891 if (!b->keys.set->data)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700892 goto err;
893out:
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800894 BUG_ON(b->io_mutex.count != 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700895
896 bkey_copy(&b->key, k);
897 list_move(&b->list, &c->btree_cache);
898 hlist_del_init_rcu(&b->hash);
899 hlist_add_head_rcu(&b->hash, mca_hash(c, k));
900
901 lock_set_subclass(&b->lock.dep_map, level + 1, _THIS_IP_);
Kent Overstreetd6fd3b12013-07-24 17:20:19 -0700902 b->parent = (void *) ~0UL;
Kent Overstreeta85e9682013-12-20 17:28:16 -0800903 b->flags = 0;
904 b->written = 0;
905 b->level = level;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700906
Kent Overstreet65d45232013-12-20 17:22:05 -0800907 if (!b->level)
Kent Overstreeta85e9682013-12-20 17:28:16 -0800908 bch_btree_keys_init(&b->keys, &bch_extent_keys_ops,
909 &b->c->expensive_debug_checks);
Kent Overstreet65d45232013-12-20 17:22:05 -0800910 else
Kent Overstreeta85e9682013-12-20 17:28:16 -0800911 bch_btree_keys_init(&b->keys, &bch_btree_keys_ops,
912 &b->c->expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700913
914 return b;
915err:
916 if (b)
917 rw_unlock(true, b);
918
Kent Overstreete8e1d462013-07-24 17:27:07 -0700919 b = mca_cannibalize(c, k);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700920 if (!IS_ERR(b))
921 goto out;
922
923 return b;
924}
925
926/**
927 * bch_btree_node_get - find a btree node in the cache and lock it, reading it
928 * in from disk if necessary.
929 *
Kent Overstreetb54d6932013-07-24 18:04:18 -0700930 * If IO is necessary and running under generic_make_request, returns -EAGAIN.
Kent Overstreetcafe5632013-03-23 16:11:31 -0700931 *
932 * The btree node will have either a read or a write lock held, depending on
933 * level and op->lock.
934 */
935struct btree *bch_btree_node_get(struct cache_set *c, struct bkey *k,
Kent Overstreete8e1d462013-07-24 17:27:07 -0700936 int level, bool write)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700937{
938 int i = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700939 struct btree *b;
940
941 BUG_ON(level < 0);
942retry:
943 b = mca_find(c, k);
944
945 if (!b) {
Kent Overstreet57943512013-04-25 13:58:35 -0700946 if (current->bio_list)
947 return ERR_PTR(-EAGAIN);
948
Kent Overstreetcafe5632013-03-23 16:11:31 -0700949 mutex_lock(&c->bucket_lock);
Kent Overstreete8e1d462013-07-24 17:27:07 -0700950 b = mca_alloc(c, k, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700951 mutex_unlock(&c->bucket_lock);
952
953 if (!b)
954 goto retry;
955 if (IS_ERR(b))
956 return b;
957
Kent Overstreet57943512013-04-25 13:58:35 -0700958 bch_btree_node_read(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700959
960 if (!write)
961 downgrade_write(&b->lock);
962 } else {
963 rw_lock(write, b, level);
964 if (PTR_HASH(c, &b->key) != PTR_HASH(c, k)) {
965 rw_unlock(write, b);
966 goto retry;
967 }
968 BUG_ON(b->level != level);
969 }
970
971 b->accessed = 1;
972
Kent Overstreeta85e9682013-12-20 17:28:16 -0800973 for (; i <= b->keys.nsets && b->keys.set[i].size; i++) {
974 prefetch(b->keys.set[i].tree);
975 prefetch(b->keys.set[i].data);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700976 }
977
Kent Overstreeta85e9682013-12-20 17:28:16 -0800978 for (; i <= b->keys.nsets; i++)
979 prefetch(b->keys.set[i].data);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700980
Kent Overstreet57943512013-04-25 13:58:35 -0700981 if (btree_node_io_error(b)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700982 rw_unlock(write, b);
Kent Overstreet57943512013-04-25 13:58:35 -0700983 return ERR_PTR(-EIO);
984 }
985
986 BUG_ON(!b->written);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700987
988 return b;
989}
990
991static void btree_node_prefetch(struct cache_set *c, struct bkey *k, int level)
992{
993 struct btree *b;
994
995 mutex_lock(&c->bucket_lock);
Kent Overstreete8e1d462013-07-24 17:27:07 -0700996 b = mca_alloc(c, k, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700997 mutex_unlock(&c->bucket_lock);
998
999 if (!IS_ERR_OR_NULL(b)) {
Kent Overstreet57943512013-04-25 13:58:35 -07001000 bch_btree_node_read(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001001 rw_unlock(true, b);
1002 }
1003}
1004
1005/* Btree alloc */
1006
Kent Overstreete8e1d462013-07-24 17:27:07 -07001007static void btree_node_free(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001008{
Kent Overstreetc37511b2013-04-26 15:39:55 -07001009 trace_bcache_btree_node_free(b);
1010
Kent Overstreetcafe5632013-03-23 16:11:31 -07001011 BUG_ON(b == b->c->root);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001012
1013 if (btree_node_dirty(b))
1014 btree_complete_write(b, btree_current_write(b));
1015 clear_bit(BTREE_NODE_dirty, &b->flags);
1016
Kent Overstreetcafe5632013-03-23 16:11:31 -07001017 cancel_delayed_work(&b->work);
1018
1019 mutex_lock(&b->c->bucket_lock);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001020 bch_bucket_free(b->c, &b->key);
1021 mca_bucket_free(b);
1022 mutex_unlock(&b->c->bucket_lock);
1023}
1024
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001025struct btree *bch_btree_node_alloc(struct cache_set *c, int level, bool wait)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001026{
1027 BKEY_PADDED(key) k;
1028 struct btree *b = ERR_PTR(-EAGAIN);
1029
1030 mutex_lock(&c->bucket_lock);
1031retry:
Kent Overstreet78365412013-12-17 01:29:34 -08001032 if (__bch_bucket_alloc_set(c, RESERVE_BTREE, &k.key, 1, wait))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001033 goto err;
1034
Kent Overstreet3a3b6a42013-07-24 16:46:42 -07001035 bkey_put(c, &k.key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001036 SET_KEY_SIZE(&k.key, c->btree_pages * PAGE_SECTORS);
1037
Kent Overstreete8e1d462013-07-24 17:27:07 -07001038 b = mca_alloc(c, &k.key, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001039 if (IS_ERR(b))
1040 goto err_free;
1041
1042 if (!b) {
Kent Overstreetb1a67b02013-03-25 11:46:44 -07001043 cache_bug(c,
1044 "Tried to allocate bucket that was in btree cache");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001045 goto retry;
1046 }
1047
Kent Overstreetcafe5632013-03-23 16:11:31 -07001048 b->accessed = 1;
Kent Overstreeta85e9682013-12-20 17:28:16 -08001049 bch_bset_init_next(&b->keys, b->keys.set->data, bset_magic(&b->c->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001050
1051 mutex_unlock(&c->bucket_lock);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001052
1053 trace_bcache_btree_node_alloc(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001054 return b;
1055err_free:
1056 bch_bucket_free(c, &k.key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001057err:
1058 mutex_unlock(&c->bucket_lock);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001059
1060 trace_bcache_btree_node_alloc_fail(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001061 return b;
1062}
1063
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001064static struct btree *btree_node_alloc_replacement(struct btree *b, bool wait)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001065{
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001066 struct btree *n = bch_btree_node_alloc(b->c, b->level, wait);
Kent Overstreet67539e82013-09-10 22:53:34 -07001067 if (!IS_ERR_OR_NULL(n)) {
Kent Overstreet89ebb4a2013-11-11 18:38:51 -08001068 bch_btree_sort_into(&b->keys, &n->keys, &b->c->sort);
Kent Overstreet67539e82013-09-10 22:53:34 -07001069 bkey_copy_key(&n->key, &b->key);
1070 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001071
1072 return n;
1073}
1074
Kent Overstreet8835c122013-07-24 23:18:05 -07001075static void make_btree_freeing_key(struct btree *b, struct bkey *k)
1076{
1077 unsigned i;
1078
Kent Overstreet05335cf2014-03-17 18:22:34 -07001079 mutex_lock(&b->c->bucket_lock);
1080
1081 atomic_inc(&b->c->prio_blocked);
1082
Kent Overstreet8835c122013-07-24 23:18:05 -07001083 bkey_copy(k, &b->key);
1084 bkey_copy_key(k, &ZERO_KEY);
1085
Kent Overstreet05335cf2014-03-17 18:22:34 -07001086 for (i = 0; i < KEY_PTRS(k); i++)
1087 SET_PTR_GEN(k, i,
1088 bch_inc_gen(PTR_CACHE(b->c, &b->key, i),
1089 PTR_BUCKET(b->c, &b->key, i)));
Kent Overstreet8835c122013-07-24 23:18:05 -07001090
Kent Overstreet05335cf2014-03-17 18:22:34 -07001091 mutex_unlock(&b->c->bucket_lock);
Kent Overstreet8835c122013-07-24 23:18:05 -07001092}
1093
Kent Overstreet78365412013-12-17 01:29:34 -08001094static int btree_check_reserve(struct btree *b, struct btree_op *op)
1095{
1096 struct cache_set *c = b->c;
1097 struct cache *ca;
1098 unsigned i, reserve = c->root->level * 2 + 1;
1099 int ret = 0;
1100
1101 mutex_lock(&c->bucket_lock);
1102
1103 for_each_cache(ca, c, i)
1104 if (fifo_used(&ca->free[RESERVE_BTREE]) < reserve) {
1105 if (op)
1106 prepare_to_wait(&c->bucket_wait, &op->wait,
1107 TASK_UNINTERRUPTIBLE);
1108 ret = -EINTR;
1109 break;
1110 }
1111
1112 mutex_unlock(&c->bucket_lock);
1113 return ret;
1114}
1115
Kent Overstreetcafe5632013-03-23 16:11:31 -07001116/* Garbage collection */
1117
Kent Overstreet487dded2014-03-17 15:13:26 -07001118static uint8_t __bch_btree_mark_key(struct cache_set *c, int level,
1119 struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001120{
1121 uint8_t stale = 0;
1122 unsigned i;
1123 struct bucket *g;
1124
1125 /*
1126 * ptr_invalid() can't return true for the keys that mark btree nodes as
1127 * freed, but since ptr_bad() returns true we'll never actually use them
1128 * for anything and thus we don't want mark their pointers here
1129 */
1130 if (!bkey_cmp(k, &ZERO_KEY))
1131 return stale;
1132
1133 for (i = 0; i < KEY_PTRS(k); i++) {
1134 if (!ptr_available(c, k, i))
1135 continue;
1136
1137 g = PTR_BUCKET(c, k, i);
1138
1139 if (gen_after(g->gc_gen, PTR_GEN(k, i)))
1140 g->gc_gen = PTR_GEN(k, i);
1141
1142 if (ptr_stale(c, k, i)) {
1143 stale = max(stale, ptr_stale(c, k, i));
1144 continue;
1145 }
1146
1147 cache_bug_on(GC_MARK(g) &&
1148 (GC_MARK(g) == GC_MARK_METADATA) != (level != 0),
1149 c, "inconsistent ptrs: mark = %llu, level = %i",
1150 GC_MARK(g), level);
1151
1152 if (level)
1153 SET_GC_MARK(g, GC_MARK_METADATA);
1154 else if (KEY_DIRTY(k))
1155 SET_GC_MARK(g, GC_MARK_DIRTY);
Kent Overstreet4fe6a812014-03-13 13:46:29 -07001156 else if (!GC_MARK(g))
1157 SET_GC_MARK(g, GC_MARK_RECLAIMABLE);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001158
1159 /* guard against overflow */
1160 SET_GC_SECTORS_USED(g, min_t(unsigned,
1161 GC_SECTORS_USED(g) + KEY_SIZE(k),
Darrick J. Wong94717442014-01-28 16:57:39 -08001162 MAX_GC_SECTORS_USED));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001163
1164 BUG_ON(!GC_SECTORS_USED(g));
1165 }
1166
1167 return stale;
1168}
1169
1170#define btree_mark_key(b, k) __bch_btree_mark_key(b->c, b->level, k)
1171
Kent Overstreet487dded2014-03-17 15:13:26 -07001172void bch_initial_mark_key(struct cache_set *c, int level, struct bkey *k)
1173{
1174 unsigned i;
1175
1176 for (i = 0; i < KEY_PTRS(k); i++)
1177 if (ptr_available(c, k, i) &&
1178 !ptr_stale(c, k, i)) {
1179 struct bucket *b = PTR_BUCKET(c, k, i);
1180
1181 b->gen = PTR_GEN(k, i);
1182
1183 if (level && bkey_cmp(k, &ZERO_KEY))
1184 b->prio = BTREE_PRIO;
1185 else if (!level && b->prio == BTREE_PRIO)
1186 b->prio = INITIAL_PRIO;
1187 }
1188
1189 __bch_btree_mark_key(c, level, k);
1190}
1191
Kent Overstreeta1f03582013-09-10 19:07:00 -07001192static bool btree_gc_mark_node(struct btree *b, struct gc_stat *gc)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001193{
1194 uint8_t stale = 0;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001195 unsigned keys = 0, good_keys = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001196 struct bkey *k;
1197 struct btree_iter iter;
1198 struct bset_tree *t;
1199
1200 gc->nodes++;
1201
Kent Overstreetc052dd92013-11-11 17:35:24 -08001202 for_each_key_filter(&b->keys, k, &iter, bch_ptr_invalid) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001203 stale = max(stale, btree_mark_key(b, k));
Kent Overstreeta1f03582013-09-10 19:07:00 -07001204 keys++;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001205
Kent Overstreeta85e9682013-12-20 17:28:16 -08001206 if (bch_ptr_bad(&b->keys, k))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001207 continue;
1208
Kent Overstreetcafe5632013-03-23 16:11:31 -07001209 gc->key_bytes += bkey_u64s(k);
1210 gc->nkeys++;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001211 good_keys++;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001212
1213 gc->data += KEY_SIZE(k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001214 }
1215
Kent Overstreeta85e9682013-12-20 17:28:16 -08001216 for (t = b->keys.set; t <= &b->keys.set[b->keys.nsets]; t++)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001217 btree_bug_on(t->size &&
Kent Overstreeta85e9682013-12-20 17:28:16 -08001218 bset_written(&b->keys, t) &&
Kent Overstreetcafe5632013-03-23 16:11:31 -07001219 bkey_cmp(&b->key, &t->end) < 0,
1220 b, "found short btree key in gc");
1221
Kent Overstreeta1f03582013-09-10 19:07:00 -07001222 if (b->c->gc_always_rewrite)
1223 return true;
1224
1225 if (stale > 10)
1226 return true;
1227
1228 if ((keys - good_keys) * 2 > keys)
1229 return true;
1230
1231 return false;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001232}
1233
Kent Overstreeta1f03582013-09-10 19:07:00 -07001234#define GC_MERGE_NODES 4U
Kent Overstreetcafe5632013-03-23 16:11:31 -07001235
1236struct gc_merge_info {
1237 struct btree *b;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001238 unsigned keys;
1239};
1240
Kent Overstreeta1f03582013-09-10 19:07:00 -07001241static int bch_btree_insert_node(struct btree *, struct btree_op *,
1242 struct keylist *, atomic_t *, struct bkey *);
Kent Overstreetb54d6932013-07-24 18:04:18 -07001243
Kent Overstreeta1f03582013-09-10 19:07:00 -07001244static int btree_gc_coalesce(struct btree *b, struct btree_op *op,
1245 struct keylist *keylist, struct gc_stat *gc,
1246 struct gc_merge_info *r)
1247{
1248 unsigned i, nodes = 0, keys = 0, blocks;
1249 struct btree *new_nodes[GC_MERGE_NODES];
1250 struct closure cl;
1251 struct bkey *k;
1252
1253 memset(new_nodes, 0, sizeof(new_nodes));
Kent Overstreetb54d6932013-07-24 18:04:18 -07001254 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001255
Kent Overstreeta1f03582013-09-10 19:07:00 -07001256 while (nodes < GC_MERGE_NODES && !IS_ERR_OR_NULL(r[nodes].b))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001257 keys += r[nodes++].keys;
1258
1259 blocks = btree_default_blocks(b->c) * 2 / 3;
1260
1261 if (nodes < 2 ||
Kent Overstreeta85e9682013-12-20 17:28:16 -08001262 __set_blocks(b->keys.set[0].data, keys,
Kent Overstreetee811282013-12-17 23:49:49 -08001263 block_bytes(b->c)) > blocks * (nodes - 1))
Kent Overstreeta1f03582013-09-10 19:07:00 -07001264 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001265
Kent Overstreeta1f03582013-09-10 19:07:00 -07001266 for (i = 0; i < nodes; i++) {
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001267 new_nodes[i] = btree_node_alloc_replacement(r[i].b, false);
Kent Overstreeta1f03582013-09-10 19:07:00 -07001268 if (IS_ERR_OR_NULL(new_nodes[i]))
1269 goto out_nocoalesce;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001270 }
1271
1272 for (i = nodes - 1; i > 0; --i) {
Kent Overstreetee811282013-12-17 23:49:49 -08001273 struct bset *n1 = btree_bset_first(new_nodes[i]);
1274 struct bset *n2 = btree_bset_first(new_nodes[i - 1]);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001275 struct bkey *k, *last = NULL;
1276
1277 keys = 0;
1278
Kent Overstreeta1f03582013-09-10 19:07:00 -07001279 if (i > 1) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001280 for (k = n2->start;
Kent Overstreetfafff812013-12-17 21:56:21 -08001281 k < bset_bkey_last(n2);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001282 k = bkey_next(k)) {
1283 if (__set_blocks(n1, n1->keys + keys +
Kent Overstreetee811282013-12-17 23:49:49 -08001284 bkey_u64s(k),
1285 block_bytes(b->c)) > blocks)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001286 break;
1287
1288 last = k;
1289 keys += bkey_u64s(k);
1290 }
Kent Overstreeta1f03582013-09-10 19:07:00 -07001291 } else {
1292 /*
1293 * Last node we're not getting rid of - we're getting
1294 * rid of the node at r[0]. Have to try and fit all of
1295 * the remaining keys into this node; we can't ensure
1296 * they will always fit due to rounding and variable
1297 * length keys (shouldn't be possible in practice,
1298 * though)
1299 */
1300 if (__set_blocks(n1, n1->keys + n2->keys,
Kent Overstreetee811282013-12-17 23:49:49 -08001301 block_bytes(b->c)) >
1302 btree_blocks(new_nodes[i]))
Kent Overstreeta1f03582013-09-10 19:07:00 -07001303 goto out_nocoalesce;
1304
1305 keys = n2->keys;
1306 /* Take the key of the node we're getting rid of */
1307 last = &r->b->key;
1308 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001309
Kent Overstreetee811282013-12-17 23:49:49 -08001310 BUG_ON(__set_blocks(n1, n1->keys + keys, block_bytes(b->c)) >
1311 btree_blocks(new_nodes[i]));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001312
Kent Overstreeta1f03582013-09-10 19:07:00 -07001313 if (last)
1314 bkey_copy_key(&new_nodes[i]->key, last);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001315
Kent Overstreetfafff812013-12-17 21:56:21 -08001316 memcpy(bset_bkey_last(n1),
Kent Overstreetcafe5632013-03-23 16:11:31 -07001317 n2->start,
Kent Overstreetfafff812013-12-17 21:56:21 -08001318 (void *) bset_bkey_idx(n2, keys) - (void *) n2->start);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001319
1320 n1->keys += keys;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001321 r[i].keys = n1->keys;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001322
1323 memmove(n2->start,
Kent Overstreetfafff812013-12-17 21:56:21 -08001324 bset_bkey_idx(n2, keys),
1325 (void *) bset_bkey_last(n2) -
1326 (void *) bset_bkey_idx(n2, keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001327
1328 n2->keys -= keys;
1329
Kent Overstreet085d2a32013-11-11 18:20:51 -08001330 if (__bch_keylist_realloc(keylist,
1331 bkey_u64s(&new_nodes[i]->key)))
Kent Overstreeta1f03582013-09-10 19:07:00 -07001332 goto out_nocoalesce;
1333
1334 bch_btree_node_write(new_nodes[i], &cl);
1335 bch_keylist_add(keylist, &new_nodes[i]->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001336 }
1337
Kent Overstreet05335cf2014-03-17 18:22:34 -07001338 closure_sync(&cl);
1339
1340 /* We emptied out this node */
1341 BUG_ON(btree_bset_first(new_nodes[0])->keys);
1342 btree_node_free(new_nodes[0]);
1343 rw_unlock(true, new_nodes[0]);
1344
Kent Overstreeta1f03582013-09-10 19:07:00 -07001345 for (i = 0; i < nodes; i++) {
Kent Overstreet085d2a32013-11-11 18:20:51 -08001346 if (__bch_keylist_realloc(keylist, bkey_u64s(&r[i].b->key)))
Kent Overstreeta1f03582013-09-10 19:07:00 -07001347 goto out_nocoalesce;
1348
1349 make_btree_freeing_key(r[i].b, keylist->top);
1350 bch_keylist_push(keylist);
1351 }
1352
Kent Overstreet05335cf2014-03-17 18:22:34 -07001353 bch_btree_insert_node(b, op, keylist, NULL, NULL);
1354 BUG_ON(!bch_keylist_empty(keylist));
Kent Overstreeta1f03582013-09-10 19:07:00 -07001355
1356 for (i = 0; i < nodes; i++) {
1357 btree_node_free(r[i].b);
1358 rw_unlock(true, r[i].b);
1359
1360 r[i].b = new_nodes[i];
1361 }
1362
Kent Overstreeta1f03582013-09-10 19:07:00 -07001363 memmove(r, r + 1, sizeof(r[0]) * (nodes - 1));
1364 r[nodes - 1].b = ERR_PTR(-EINTR);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001365
Kent Overstreetc37511b2013-04-26 15:39:55 -07001366 trace_bcache_btree_gc_coalesce(nodes);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001367 gc->nodes--;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001368
Kent Overstreeta1f03582013-09-10 19:07:00 -07001369 /* Invalidated our iterator */
1370 return -EINTR;
1371
1372out_nocoalesce:
1373 closure_sync(&cl);
1374
1375 while ((k = bch_keylist_pop(keylist)))
1376 if (!bkey_cmp(k, &ZERO_KEY))
1377 atomic_dec(&b->c->prio_blocked);
1378
1379 for (i = 0; i < nodes; i++)
1380 if (!IS_ERR_OR_NULL(new_nodes[i])) {
1381 btree_node_free(new_nodes[i]);
1382 rw_unlock(true, new_nodes[i]);
1383 }
1384 return 0;
1385}
1386
1387static unsigned btree_gc_count_keys(struct btree *b)
1388{
1389 struct bkey *k;
1390 struct btree_iter iter;
1391 unsigned ret = 0;
1392
Kent Overstreetc052dd92013-11-11 17:35:24 -08001393 for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad)
Kent Overstreeta1f03582013-09-10 19:07:00 -07001394 ret += bkey_u64s(k);
1395
1396 return ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001397}
1398
1399static int btree_gc_recurse(struct btree *b, struct btree_op *op,
1400 struct closure *writes, struct gc_stat *gc)
1401{
Kent Overstreetcafe5632013-03-23 16:11:31 -07001402 unsigned i;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001403 int ret = 0;
1404 bool should_rewrite;
1405 struct btree *n;
1406 struct bkey *k;
1407 struct keylist keys;
1408 struct btree_iter iter;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001409 struct gc_merge_info r[GC_MERGE_NODES];
Kent Overstreeta1f03582013-09-10 19:07:00 -07001410 struct gc_merge_info *last = r + GC_MERGE_NODES - 1;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001411
Kent Overstreeta1f03582013-09-10 19:07:00 -07001412 bch_keylist_init(&keys);
Kent Overstreetc052dd92013-11-11 17:35:24 -08001413 bch_btree_iter_init(&b->keys, &iter, &b->c->gc_done);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001414
Kent Overstreeta1f03582013-09-10 19:07:00 -07001415 for (i = 0; i < GC_MERGE_NODES; i++)
1416 r[i].b = ERR_PTR(-EINTR);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001417
Kent Overstreeta1f03582013-09-10 19:07:00 -07001418 while (1) {
Kent Overstreeta85e9682013-12-20 17:28:16 -08001419 k = bch_btree_iter_next_filter(&iter, &b->keys, bch_ptr_bad);
Kent Overstreeta1f03582013-09-10 19:07:00 -07001420 if (k) {
1421 r->b = bch_btree_node_get(b->c, k, b->level - 1, true);
1422 if (IS_ERR(r->b)) {
1423 ret = PTR_ERR(r->b);
1424 break;
1425 }
1426
1427 r->keys = btree_gc_count_keys(r->b);
1428
1429 ret = btree_gc_coalesce(b, op, &keys, gc, r);
1430 if (ret)
1431 break;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001432 }
1433
Kent Overstreeta1f03582013-09-10 19:07:00 -07001434 if (!last->b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001435 break;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001436
1437 if (!IS_ERR(last->b)) {
1438 should_rewrite = btree_gc_mark_node(last->b, gc);
Kent Overstreet78365412013-12-17 01:29:34 -08001439 if (should_rewrite &&
1440 !btree_check_reserve(b, NULL)) {
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001441 n = btree_node_alloc_replacement(last->b,
1442 false);
Kent Overstreeta1f03582013-09-10 19:07:00 -07001443
1444 if (!IS_ERR_OR_NULL(n)) {
1445 bch_btree_node_write_sync(n);
1446 bch_keylist_add(&keys, &n->key);
1447
1448 make_btree_freeing_key(last->b,
1449 keys.top);
1450 bch_keylist_push(&keys);
1451
Kent Overstreeta1f03582013-09-10 19:07:00 -07001452 bch_btree_insert_node(b, op, &keys,
1453 NULL, NULL);
1454 BUG_ON(!bch_keylist_empty(&keys));
1455
Kent Overstreet05335cf2014-03-17 18:22:34 -07001456 btree_node_free(last->b);
Kent Overstreeta1f03582013-09-10 19:07:00 -07001457 rw_unlock(true, last->b);
1458 last->b = n;
1459
1460 /* Invalidated our iterator */
1461 ret = -EINTR;
1462 break;
1463 }
1464 }
1465
1466 if (last->b->level) {
1467 ret = btree_gc_recurse(last->b, op, writes, gc);
1468 if (ret)
1469 break;
1470 }
1471
1472 bkey_copy_key(&b->c->gc_done, &last->b->key);
1473
1474 /*
1475 * Must flush leaf nodes before gc ends, since replace
1476 * operations aren't journalled
1477 */
1478 if (btree_node_dirty(last->b))
1479 bch_btree_node_write(last->b, writes);
1480 rw_unlock(true, last->b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001481 }
1482
Kent Overstreeta1f03582013-09-10 19:07:00 -07001483 memmove(r + 1, r, sizeof(r[0]) * (GC_MERGE_NODES - 1));
1484 r->b = NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001485
Kent Overstreetcafe5632013-03-23 16:11:31 -07001486 if (need_resched()) {
1487 ret = -EAGAIN;
1488 break;
1489 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001490 }
1491
Kent Overstreeta1f03582013-09-10 19:07:00 -07001492 for (i = 0; i < GC_MERGE_NODES; i++)
1493 if (!IS_ERR_OR_NULL(r[i].b)) {
1494 if (btree_node_dirty(r[i].b))
1495 bch_btree_node_write(r[i].b, writes);
1496 rw_unlock(true, r[i].b);
1497 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001498
Kent Overstreeta1f03582013-09-10 19:07:00 -07001499 bch_keylist_free(&keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001500
1501 return ret;
1502}
1503
1504static int bch_btree_gc_root(struct btree *b, struct btree_op *op,
1505 struct closure *writes, struct gc_stat *gc)
1506{
1507 struct btree *n = NULL;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001508 int ret = 0;
1509 bool should_rewrite;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001510
Kent Overstreeta1f03582013-09-10 19:07:00 -07001511 should_rewrite = btree_gc_mark_node(b, gc);
1512 if (should_rewrite) {
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001513 n = btree_node_alloc_replacement(b, false);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001514
Kent Overstreeta1f03582013-09-10 19:07:00 -07001515 if (!IS_ERR_OR_NULL(n)) {
1516 bch_btree_node_write_sync(n);
1517 bch_btree_set_root(n);
1518 btree_node_free(b);
1519 rw_unlock(true, n);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001520
Kent Overstreeta1f03582013-09-10 19:07:00 -07001521 return -EINTR;
1522 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001523 }
1524
Kent Overstreet487dded2014-03-17 15:13:26 -07001525 __bch_btree_mark_key(b->c, b->level + 1, &b->key);
1526
Kent Overstreeta1f03582013-09-10 19:07:00 -07001527 if (b->level) {
1528 ret = btree_gc_recurse(b, op, writes, gc);
1529 if (ret)
1530 return ret;
1531 }
1532
1533 bkey_copy_key(&b->c->gc_done, &b->key);
1534
Kent Overstreetcafe5632013-03-23 16:11:31 -07001535 return ret;
1536}
1537
1538static void btree_gc_start(struct cache_set *c)
1539{
1540 struct cache *ca;
1541 struct bucket *b;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001542 unsigned i;
1543
1544 if (!c->gc_mark_valid)
1545 return;
1546
1547 mutex_lock(&c->bucket_lock);
1548
1549 c->gc_mark_valid = 0;
1550 c->gc_done = ZERO_KEY;
1551
1552 for_each_cache(ca, c, i)
1553 for_each_bucket(b, ca) {
1554 b->gc_gen = b->gen;
Kent Overstreet29ebf462013-07-11 19:43:21 -07001555 if (!atomic_read(&b->pin)) {
Kent Overstreet4fe6a812014-03-13 13:46:29 -07001556 SET_GC_MARK(b, 0);
Kent Overstreet29ebf462013-07-11 19:43:21 -07001557 SET_GC_SECTORS_USED(b, 0);
1558 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001559 }
1560
Kent Overstreetcafe5632013-03-23 16:11:31 -07001561 mutex_unlock(&c->bucket_lock);
1562}
1563
1564size_t bch_btree_gc_finish(struct cache_set *c)
1565{
1566 size_t available = 0;
1567 struct bucket *b;
1568 struct cache *ca;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001569 unsigned i;
1570
1571 mutex_lock(&c->bucket_lock);
1572
1573 set_gc_sectors(c);
1574 c->gc_mark_valid = 1;
1575 c->need_gc = 0;
1576
Kent Overstreetcafe5632013-03-23 16:11:31 -07001577 for (i = 0; i < KEY_PTRS(&c->uuid_bucket); i++)
1578 SET_GC_MARK(PTR_BUCKET(c, &c->uuid_bucket, i),
1579 GC_MARK_METADATA);
1580
Nicholas Swensonbf0a6282013-11-26 19:14:23 -08001581 /* don't reclaim buckets to which writeback keys point */
1582 rcu_read_lock();
1583 for (i = 0; i < c->nr_uuids; i++) {
1584 struct bcache_device *d = c->devices[i];
1585 struct cached_dev *dc;
1586 struct keybuf_key *w, *n;
1587 unsigned j;
1588
1589 if (!d || UUID_FLASH_ONLY(&c->uuids[i]))
1590 continue;
1591 dc = container_of(d, struct cached_dev, disk);
1592
1593 spin_lock(&dc->writeback_keys.lock);
1594 rbtree_postorder_for_each_entry_safe(w, n,
1595 &dc->writeback_keys.keys, node)
1596 for (j = 0; j < KEY_PTRS(&w->key); j++)
1597 SET_GC_MARK(PTR_BUCKET(c, &w->key, j),
1598 GC_MARK_DIRTY);
1599 spin_unlock(&dc->writeback_keys.lock);
1600 }
1601 rcu_read_unlock();
1602
Kent Overstreetcafe5632013-03-23 16:11:31 -07001603 for_each_cache(ca, c, i) {
1604 uint64_t *i;
1605
1606 ca->invalidate_needs_gc = 0;
1607
1608 for (i = ca->sb.d; i < ca->sb.d + ca->sb.keys; i++)
1609 SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA);
1610
1611 for (i = ca->prio_buckets;
1612 i < ca->prio_buckets + prio_buckets(ca) * 2; i++)
1613 SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA);
1614
1615 for_each_bucket(b, ca) {
1616 b->last_gc = b->gc_gen;
1617 c->need_gc = max(c->need_gc, bucket_gc_gen(b));
1618
Kent Overstreet4fe6a812014-03-13 13:46:29 -07001619 if (atomic_read(&b->pin))
1620 continue;
1621
1622 BUG_ON(!GC_MARK(b) && GC_SECTORS_USED(b));
1623
1624 if (!GC_MARK(b) || GC_MARK(b) == GC_MARK_RECLAIMABLE)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001625 available++;
Kent Overstreet4fe6a812014-03-13 13:46:29 -07001626
1627 if (!GC_MARK(b))
1628 bch_bucket_add_unused(ca, b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001629 }
1630 }
1631
Kent Overstreetcafe5632013-03-23 16:11:31 -07001632 mutex_unlock(&c->bucket_lock);
1633 return available;
1634}
1635
Kent Overstreet72a44512013-10-24 17:19:26 -07001636static void bch_btree_gc(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001637{
Kent Overstreetcafe5632013-03-23 16:11:31 -07001638 int ret;
1639 unsigned long available;
1640 struct gc_stat stats;
1641 struct closure writes;
1642 struct btree_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001643 uint64_t start_time = local_clock();
Kent Overstreet57943512013-04-25 13:58:35 -07001644
Kent Overstreetc37511b2013-04-26 15:39:55 -07001645 trace_bcache_gc_start(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001646
1647 memset(&stats, 0, sizeof(struct gc_stat));
1648 closure_init_stack(&writes);
Kent Overstreetb54d6932013-07-24 18:04:18 -07001649 bch_btree_op_init(&op, SHRT_MAX);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001650
1651 btree_gc_start(c);
1652
Kent Overstreeta1f03582013-09-10 19:07:00 -07001653 do {
1654 ret = btree_root(gc_root, c, &op, &writes, &stats);
1655 closure_sync(&writes);
Kent Overstreet57943512013-04-25 13:58:35 -07001656
Kent Overstreeta1f03582013-09-10 19:07:00 -07001657 if (ret && ret != -EAGAIN)
1658 pr_warn("gc failed!");
1659 } while (ret);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001660
1661 available = bch_btree_gc_finish(c);
Kent Overstreet57943512013-04-25 13:58:35 -07001662 wake_up_allocators(c);
1663
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001664 bch_time_stats_update(&c->btree_gc_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001665
1666 stats.key_bytes *= sizeof(uint64_t);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001667 stats.data <<= 9;
1668 stats.in_use = (c->nbuckets - available) * 100 / c->nbuckets;
1669 memcpy(&c->gc_stats, &stats, sizeof(struct gc_stat));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001670
Kent Overstreetc37511b2013-04-26 15:39:55 -07001671 trace_bcache_gc_end(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001672
Kent Overstreet72a44512013-10-24 17:19:26 -07001673 bch_moving_gc(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001674}
1675
Kent Overstreet72a44512013-10-24 17:19:26 -07001676static int bch_gc_thread(void *arg)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001677{
Kent Overstreet72a44512013-10-24 17:19:26 -07001678 struct cache_set *c = arg;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001679 struct cache *ca;
1680 unsigned i;
Kent Overstreet72a44512013-10-24 17:19:26 -07001681
1682 while (1) {
Kent Overstreeta1f03582013-09-10 19:07:00 -07001683again:
Kent Overstreet72a44512013-10-24 17:19:26 -07001684 bch_btree_gc(c);
1685
1686 set_current_state(TASK_INTERRUPTIBLE);
1687 if (kthread_should_stop())
1688 break;
1689
Kent Overstreeta1f03582013-09-10 19:07:00 -07001690 mutex_lock(&c->bucket_lock);
1691
1692 for_each_cache(ca, c, i)
1693 if (ca->invalidate_needs_gc) {
1694 mutex_unlock(&c->bucket_lock);
1695 set_current_state(TASK_RUNNING);
1696 goto again;
1697 }
1698
1699 mutex_unlock(&c->bucket_lock);
1700
Kent Overstreet72a44512013-10-24 17:19:26 -07001701 try_to_freeze();
1702 schedule();
1703 }
1704
1705 return 0;
1706}
1707
1708int bch_gc_thread_start(struct cache_set *c)
1709{
1710 c->gc_thread = kthread_create(bch_gc_thread, c, "bcache_gc");
1711 if (IS_ERR(c->gc_thread))
1712 return PTR_ERR(c->gc_thread);
1713
1714 set_task_state(c->gc_thread, TASK_INTERRUPTIBLE);
1715 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001716}
1717
1718/* Initial partial gc */
1719
Kent Overstreet487dded2014-03-17 15:13:26 -07001720static int bch_btree_check_recurse(struct btree *b, struct btree_op *op)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001721{
Kent Overstreet50310162013-09-10 17:18:59 -07001722 int ret = 0;
Kent Overstreet50310162013-09-10 17:18:59 -07001723 struct bkey *k, *p = NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001724 struct btree_iter iter;
1725
Kent Overstreet487dded2014-03-17 15:13:26 -07001726 for_each_key_filter(&b->keys, k, &iter, bch_ptr_invalid)
1727 bch_initial_mark_key(b->c, b->level, k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001728
Kent Overstreet487dded2014-03-17 15:13:26 -07001729 bch_initial_mark_key(b->c, b->level + 1, &b->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001730
1731 if (b->level) {
Kent Overstreetc052dd92013-11-11 17:35:24 -08001732 bch_btree_iter_init(&b->keys, &iter, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001733
Kent Overstreet50310162013-09-10 17:18:59 -07001734 do {
Kent Overstreeta85e9682013-12-20 17:28:16 -08001735 k = bch_btree_iter_next_filter(&iter, &b->keys,
1736 bch_ptr_bad);
Kent Overstreet50310162013-09-10 17:18:59 -07001737 if (k)
1738 btree_node_prefetch(b->c, k, b->level - 1);
1739
Kent Overstreetcafe5632013-03-23 16:11:31 -07001740 if (p)
Kent Overstreet487dded2014-03-17 15:13:26 -07001741 ret = btree(check_recurse, p, b, op);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001742
Kent Overstreet50310162013-09-10 17:18:59 -07001743 p = k;
1744 } while (p && !ret);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001745 }
1746
Kent Overstreet487dded2014-03-17 15:13:26 -07001747 return ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001748}
1749
Kent Overstreetc18536a2013-07-24 17:44:17 -07001750int bch_btree_check(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001751{
Kent Overstreetc18536a2013-07-24 17:44:17 -07001752 struct btree_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001753
Kent Overstreetb54d6932013-07-24 18:04:18 -07001754 bch_btree_op_init(&op, SHRT_MAX);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001755
Kent Overstreet487dded2014-03-17 15:13:26 -07001756 return btree_root(check_recurse, c, &op);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001757}
1758
1759/* Btree insertion */
1760
Kent Overstreet829a60b2013-11-11 17:02:31 -08001761static bool btree_insert_key(struct btree *b, struct bkey *k,
1762 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001763{
Kent Overstreet829a60b2013-11-11 17:02:31 -08001764 unsigned status;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001765
1766 BUG_ON(bkey_cmp(k, &b->key) > 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001767
Kent Overstreet829a60b2013-11-11 17:02:31 -08001768 status = bch_btree_insert_key(&b->keys, k, replace_key);
1769 if (status != BTREE_INSERT_STATUS_NO_INSERT) {
1770 bch_check_keys(&b->keys, "%u for %s", status,
1771 replace_key ? "replace" : "insert");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001772
Kent Overstreet829a60b2013-11-11 17:02:31 -08001773 trace_bcache_btree_insert_key(b, k, replace_key != NULL,
1774 status);
1775 return true;
1776 } else
1777 return false;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001778}
1779
Kent Overstreet59158fd2013-11-11 19:03:54 -08001780static size_t insert_u64s_remaining(struct btree *b)
1781{
Kent Overstreet35723242014-01-10 18:53:02 -08001782 long ret = bch_btree_keys_u64s_remaining(&b->keys);
Kent Overstreet59158fd2013-11-11 19:03:54 -08001783
1784 /*
1785 * Might land in the middle of an existing extent and have to split it
1786 */
1787 if (b->keys.ops->is_extents)
1788 ret -= KEY_MAX_U64S;
1789
1790 return max(ret, 0L);
1791}
1792
Kent Overstreet26c949f2013-09-10 18:41:15 -07001793static bool bch_btree_insert_keys(struct btree *b, struct btree_op *op,
Kent Overstreet1b207d82013-09-10 18:52:54 -07001794 struct keylist *insert_keys,
1795 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001796{
1797 bool ret = false;
Kent Overstreetdc9d98d2013-12-17 23:47:33 -08001798 int oldsize = bch_count_data(&b->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001799
Kent Overstreet26c949f2013-09-10 18:41:15 -07001800 while (!bch_keylist_empty(insert_keys)) {
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07001801 struct bkey *k = insert_keys->keys;
Kent Overstreet26c949f2013-09-10 18:41:15 -07001802
Kent Overstreet59158fd2013-11-11 19:03:54 -08001803 if (bkey_u64s(k) > insert_u64s_remaining(b))
Kent Overstreet403b6cd2013-07-24 17:22:44 -07001804 break;
1805
1806 if (bkey_cmp(k, &b->key) <= 0) {
Kent Overstreet3a3b6a42013-07-24 16:46:42 -07001807 if (!b->level)
1808 bkey_put(b->c, k);
Kent Overstreet26c949f2013-09-10 18:41:15 -07001809
Kent Overstreet829a60b2013-11-11 17:02:31 -08001810 ret |= btree_insert_key(b, k, replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07001811 bch_keylist_pop_front(insert_keys);
1812 } else if (bkey_cmp(&START_KEY(k), &b->key) < 0) {
Kent Overstreet26c949f2013-09-10 18:41:15 -07001813 BKEY_PADDED(key) temp;
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07001814 bkey_copy(&temp.key, insert_keys->keys);
Kent Overstreet26c949f2013-09-10 18:41:15 -07001815
1816 bch_cut_back(&b->key, &temp.key);
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07001817 bch_cut_front(&b->key, insert_keys->keys);
Kent Overstreet26c949f2013-09-10 18:41:15 -07001818
Kent Overstreet829a60b2013-11-11 17:02:31 -08001819 ret |= btree_insert_key(b, &temp.key, replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07001820 break;
1821 } else {
1822 break;
1823 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001824 }
1825
Kent Overstreet829a60b2013-11-11 17:02:31 -08001826 if (!ret)
1827 op->insert_collision = true;
1828
Kent Overstreet403b6cd2013-07-24 17:22:44 -07001829 BUG_ON(!bch_keylist_empty(insert_keys) && b->level);
1830
Kent Overstreetdc9d98d2013-12-17 23:47:33 -08001831 BUG_ON(bch_count_data(&b->keys) < oldsize);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001832 return ret;
1833}
1834
Kent Overstreet26c949f2013-09-10 18:41:15 -07001835static int btree_split(struct btree *b, struct btree_op *op,
1836 struct keylist *insert_keys,
Kent Overstreet1b207d82013-09-10 18:52:54 -07001837 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001838{
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07001839 bool split;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001840 struct btree *n1, *n2 = NULL, *n3 = NULL;
1841 uint64_t start_time = local_clock();
Kent Overstreetb54d6932013-07-24 18:04:18 -07001842 struct closure cl;
Kent Overstreet17e21a92013-07-26 12:32:38 -07001843 struct keylist parent_keys;
Kent Overstreetb54d6932013-07-24 18:04:18 -07001844
1845 closure_init_stack(&cl);
Kent Overstreet17e21a92013-07-26 12:32:38 -07001846 bch_keylist_init(&parent_keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001847
Kent Overstreet78365412013-12-17 01:29:34 -08001848 if (!b->level &&
1849 btree_check_reserve(b, op))
1850 return -EINTR;
1851
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001852 n1 = btree_node_alloc_replacement(b, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001853 if (IS_ERR(n1))
1854 goto err;
1855
Kent Overstreetee811282013-12-17 23:49:49 -08001856 split = set_blocks(btree_bset_first(n1),
1857 block_bytes(n1->c)) > (btree_blocks(b) * 4) / 5;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001858
Kent Overstreetcafe5632013-03-23 16:11:31 -07001859 if (split) {
1860 unsigned keys = 0;
1861
Kent Overstreetee811282013-12-17 23:49:49 -08001862 trace_bcache_btree_node_split(b, btree_bset_first(n1)->keys);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001863
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001864 n2 = bch_btree_node_alloc(b->c, b->level, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001865 if (IS_ERR(n2))
1866 goto err_free1;
1867
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07001868 if (!b->parent) {
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001869 n3 = bch_btree_node_alloc(b->c, b->level + 1, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001870 if (IS_ERR(n3))
1871 goto err_free2;
1872 }
1873
Kent Overstreet1b207d82013-09-10 18:52:54 -07001874 bch_btree_insert_keys(n1, op, insert_keys, replace_key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001875
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07001876 /*
1877 * Has to be a linear search because we don't have an auxiliary
Kent Overstreetcafe5632013-03-23 16:11:31 -07001878 * search tree yet
1879 */
1880
Kent Overstreetee811282013-12-17 23:49:49 -08001881 while (keys < (btree_bset_first(n1)->keys * 3) / 5)
1882 keys += bkey_u64s(bset_bkey_idx(btree_bset_first(n1),
Kent Overstreetfafff812013-12-17 21:56:21 -08001883 keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001884
Kent Overstreetfafff812013-12-17 21:56:21 -08001885 bkey_copy_key(&n1->key,
Kent Overstreetee811282013-12-17 23:49:49 -08001886 bset_bkey_idx(btree_bset_first(n1), keys));
1887 keys += bkey_u64s(bset_bkey_idx(btree_bset_first(n1), keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001888
Kent Overstreetee811282013-12-17 23:49:49 -08001889 btree_bset_first(n2)->keys = btree_bset_first(n1)->keys - keys;
1890 btree_bset_first(n1)->keys = keys;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001891
Kent Overstreetee811282013-12-17 23:49:49 -08001892 memcpy(btree_bset_first(n2)->start,
1893 bset_bkey_last(btree_bset_first(n1)),
1894 btree_bset_first(n2)->keys * sizeof(uint64_t));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001895
1896 bkey_copy_key(&n2->key, &b->key);
1897
Kent Overstreet17e21a92013-07-26 12:32:38 -07001898 bch_keylist_add(&parent_keys, &n2->key);
Kent Overstreetb54d6932013-07-24 18:04:18 -07001899 bch_btree_node_write(n2, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001900 rw_unlock(true, n2);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001901 } else {
Kent Overstreetee811282013-12-17 23:49:49 -08001902 trace_bcache_btree_node_compact(b, btree_bset_first(n1)->keys);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001903
Kent Overstreet1b207d82013-09-10 18:52:54 -07001904 bch_btree_insert_keys(n1, op, insert_keys, replace_key);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001905 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001906
Kent Overstreet17e21a92013-07-26 12:32:38 -07001907 bch_keylist_add(&parent_keys, &n1->key);
Kent Overstreetb54d6932013-07-24 18:04:18 -07001908 bch_btree_node_write(n1, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001909
1910 if (n3) {
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07001911 /* Depth increases, make a new root */
Kent Overstreetcafe5632013-03-23 16:11:31 -07001912 bkey_copy_key(&n3->key, &MAX_KEY);
Kent Overstreet17e21a92013-07-26 12:32:38 -07001913 bch_btree_insert_keys(n3, op, &parent_keys, NULL);
Kent Overstreetb54d6932013-07-24 18:04:18 -07001914 bch_btree_node_write(n3, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001915
Kent Overstreetb54d6932013-07-24 18:04:18 -07001916 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001917 bch_btree_set_root(n3);
1918 rw_unlock(true, n3);
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07001919 } else if (!b->parent) {
1920 /* Root filled up but didn't need to be split */
Kent Overstreetb54d6932013-07-24 18:04:18 -07001921 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001922 bch_btree_set_root(n1);
1923 } else {
Kent Overstreet17e21a92013-07-26 12:32:38 -07001924 /* Split a non root node */
Kent Overstreetb54d6932013-07-24 18:04:18 -07001925 closure_sync(&cl);
Kent Overstreet17e21a92013-07-26 12:32:38 -07001926 make_btree_freeing_key(b, parent_keys.top);
1927 bch_keylist_push(&parent_keys);
1928
Kent Overstreet17e21a92013-07-26 12:32:38 -07001929 bch_btree_insert_node(b->parent, op, &parent_keys, NULL, NULL);
1930 BUG_ON(!bch_keylist_empty(&parent_keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001931 }
1932
Kent Overstreet05335cf2014-03-17 18:22:34 -07001933 btree_node_free(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001934 rw_unlock(true, n1);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001935
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001936 bch_time_stats_update(&b->c->btree_split_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001937
1938 return 0;
1939err_free2:
Kent Overstreet5f5837d2013-12-16 16:38:49 -08001940 bkey_put(b->c, &n2->key);
Kent Overstreete8e1d462013-07-24 17:27:07 -07001941 btree_node_free(n2);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001942 rw_unlock(true, n2);
1943err_free1:
Kent Overstreet5f5837d2013-12-16 16:38:49 -08001944 bkey_put(b->c, &n1->key);
Kent Overstreete8e1d462013-07-24 17:27:07 -07001945 btree_node_free(n1);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001946 rw_unlock(true, n1);
1947err:
Kent Overstreet5f5837d2013-12-16 16:38:49 -08001948 WARN(1, "bcache: btree split failed");
1949
Kent Overstreetcafe5632013-03-23 16:11:31 -07001950 if (n3 == ERR_PTR(-EAGAIN) ||
1951 n2 == ERR_PTR(-EAGAIN) ||
1952 n1 == ERR_PTR(-EAGAIN))
1953 return -EAGAIN;
1954
Kent Overstreetcafe5632013-03-23 16:11:31 -07001955 return -ENOMEM;
1956}
1957
Kent Overstreet26c949f2013-09-10 18:41:15 -07001958static int bch_btree_insert_node(struct btree *b, struct btree_op *op,
Kent Overstreetc18536a2013-07-24 17:44:17 -07001959 struct keylist *insert_keys,
Kent Overstreet1b207d82013-09-10 18:52:54 -07001960 atomic_t *journal_ref,
1961 struct bkey *replace_key)
Kent Overstreet26c949f2013-09-10 18:41:15 -07001962{
Kent Overstreet17e21a92013-07-26 12:32:38 -07001963 BUG_ON(b->level && replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07001964
Kent Overstreet59158fd2013-11-11 19:03:54 -08001965 if (bch_keylist_nkeys(insert_keys) > insert_u64s_remaining(b)) {
Kent Overstreet17e21a92013-07-26 12:32:38 -07001966 if (current->bio_list) {
1967 op->lock = b->c->root->level + 1;
1968 return -EAGAIN;
1969 } else if (op->lock <= b->c->root->level) {
1970 op->lock = b->c->root->level + 1;
1971 return -EINTR;
Kent Overstreet26c949f2013-09-10 18:41:15 -07001972 } else {
Kent Overstreet17e21a92013-07-26 12:32:38 -07001973 /* Invalidated all iterators */
Kent Overstreet3b3e9e52013-12-07 03:57:58 -08001974 int ret = btree_split(b, op, insert_keys, replace_key);
1975
1976 return bch_keylist_empty(insert_keys) ?
1977 0 : ret ?: -EINTR;
Kent Overstreet26c949f2013-09-10 18:41:15 -07001978 }
Kent Overstreet17e21a92013-07-26 12:32:38 -07001979 } else {
Kent Overstreetee811282013-12-17 23:49:49 -08001980 BUG_ON(write_block(b) != btree_bset_last(b));
Kent Overstreet26c949f2013-09-10 18:41:15 -07001981
Kent Overstreet17e21a92013-07-26 12:32:38 -07001982 if (bch_btree_insert_keys(b, op, insert_keys, replace_key)) {
1983 if (!b->level)
1984 bch_btree_leaf_dirty(b, journal_ref);
1985 else
1986 bch_btree_node_write_sync(b);
1987 }
1988
1989 return 0;
1990 }
Kent Overstreet26c949f2013-09-10 18:41:15 -07001991}
1992
Kent Overstreete7c590e2013-09-10 18:39:16 -07001993int bch_btree_insert_check_key(struct btree *b, struct btree_op *op,
1994 struct bkey *check_key)
1995{
1996 int ret = -EINTR;
1997 uint64_t btree_ptr = b->key.ptr[0];
1998 unsigned long seq = b->seq;
1999 struct keylist insert;
2000 bool upgrade = op->lock == -1;
2001
2002 bch_keylist_init(&insert);
2003
2004 if (upgrade) {
2005 rw_unlock(false, b);
2006 rw_lock(true, b, b->level);
2007
2008 if (b->key.ptr[0] != btree_ptr ||
2009 b->seq != seq + 1)
2010 goto out;
2011 }
2012
2013 SET_KEY_PTRS(check_key, 1);
2014 get_random_bytes(&check_key->ptr[0], sizeof(uint64_t));
2015
2016 SET_PTR_DEV(check_key, 0, PTR_CHECK_DEV);
2017
2018 bch_keylist_add(&insert, check_key);
2019
Kent Overstreet1b207d82013-09-10 18:52:54 -07002020 ret = bch_btree_insert_node(b, op, &insert, NULL, NULL);
Kent Overstreete7c590e2013-09-10 18:39:16 -07002021
2022 BUG_ON(!ret && !bch_keylist_empty(&insert));
2023out:
2024 if (upgrade)
2025 downgrade_write(&b->lock);
2026 return ret;
2027}
2028
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002029struct btree_insert_op {
2030 struct btree_op op;
2031 struct keylist *keys;
2032 atomic_t *journal_ref;
2033 struct bkey *replace_key;
2034};
2035
Wei Yongjun08239ca2013-11-28 10:31:35 +08002036static int btree_insert_fn(struct btree_op *b_op, struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002037{
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002038 struct btree_insert_op *op = container_of(b_op,
2039 struct btree_insert_op, op);
Kent Overstreet403b6cd2013-07-24 17:22:44 -07002040
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002041 int ret = bch_btree_insert_node(b, &op->op, op->keys,
2042 op->journal_ref, op->replace_key);
2043 if (ret && !bch_keylist_empty(op->keys))
2044 return ret;
2045 else
2046 return MAP_DONE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002047}
2048
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002049int bch_btree_insert(struct cache_set *c, struct keylist *keys,
2050 atomic_t *journal_ref, struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002051{
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002052 struct btree_insert_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002053 int ret = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002054
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002055 BUG_ON(current->bio_list);
Kent Overstreet4f3d4012013-09-10 18:46:36 -07002056 BUG_ON(bch_keylist_empty(keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002057
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002058 bch_btree_op_init(&op.op, 0);
2059 op.keys = keys;
2060 op.journal_ref = journal_ref;
2061 op.replace_key = replace_key;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002062
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002063 while (!ret && !bch_keylist_empty(keys)) {
2064 op.op.lock = 0;
2065 ret = bch_btree_map_leaf_nodes(&op.op, c,
2066 &START_KEY(keys->keys),
2067 btree_insert_fn);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002068 }
2069
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002070 if (ret) {
2071 struct bkey *k;
2072
2073 pr_err("error %i", ret);
2074
2075 while ((k = bch_keylist_pop(keys)))
Kent Overstreet3a3b6a42013-07-24 16:46:42 -07002076 bkey_put(c, k);
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002077 } else if (op.op.insert_collision)
2078 ret = -ESRCH;
Kent Overstreet6054c6d2013-07-24 18:06:22 -07002079
Kent Overstreetcafe5632013-03-23 16:11:31 -07002080 return ret;
2081}
2082
2083void bch_btree_set_root(struct btree *b)
2084{
2085 unsigned i;
Kent Overstreete49c7c32013-06-26 17:25:38 -07002086 struct closure cl;
2087
2088 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002089
Kent Overstreetc37511b2013-04-26 15:39:55 -07002090 trace_bcache_btree_set_root(b);
2091
Kent Overstreetcafe5632013-03-23 16:11:31 -07002092 BUG_ON(!b->written);
2093
2094 for (i = 0; i < KEY_PTRS(&b->key); i++)
2095 BUG_ON(PTR_BUCKET(b->c, &b->key, i)->prio != BTREE_PRIO);
2096
2097 mutex_lock(&b->c->bucket_lock);
2098 list_del_init(&b->list);
2099 mutex_unlock(&b->c->bucket_lock);
2100
2101 b->c->root = b;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002102
Kent Overstreete49c7c32013-06-26 17:25:38 -07002103 bch_journal_meta(b->c, &cl);
2104 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002105}
2106
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002107/* Map across nodes or keys */
2108
2109static int bch_btree_map_nodes_recurse(struct btree *b, struct btree_op *op,
2110 struct bkey *from,
2111 btree_map_nodes_fn *fn, int flags)
2112{
2113 int ret = MAP_CONTINUE;
2114
2115 if (b->level) {
2116 struct bkey *k;
2117 struct btree_iter iter;
2118
Kent Overstreetc052dd92013-11-11 17:35:24 -08002119 bch_btree_iter_init(&b->keys, &iter, from);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002120
Kent Overstreeta85e9682013-12-20 17:28:16 -08002121 while ((k = bch_btree_iter_next_filter(&iter, &b->keys,
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002122 bch_ptr_bad))) {
2123 ret = btree(map_nodes_recurse, k, b,
2124 op, from, fn, flags);
2125 from = NULL;
2126
2127 if (ret != MAP_CONTINUE)
2128 return ret;
2129 }
2130 }
2131
2132 if (!b->level || flags == MAP_ALL_NODES)
2133 ret = fn(op, b);
2134
2135 return ret;
2136}
2137
2138int __bch_btree_map_nodes(struct btree_op *op, struct cache_set *c,
2139 struct bkey *from, btree_map_nodes_fn *fn, int flags)
2140{
Kent Overstreetb54d6932013-07-24 18:04:18 -07002141 return btree_root(map_nodes_recurse, c, op, from, fn, flags);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002142}
2143
2144static int bch_btree_map_keys_recurse(struct btree *b, struct btree_op *op,
2145 struct bkey *from, btree_map_keys_fn *fn,
2146 int flags)
2147{
2148 int ret = MAP_CONTINUE;
2149 struct bkey *k;
2150 struct btree_iter iter;
2151
Kent Overstreetc052dd92013-11-11 17:35:24 -08002152 bch_btree_iter_init(&b->keys, &iter, from);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002153
Kent Overstreeta85e9682013-12-20 17:28:16 -08002154 while ((k = bch_btree_iter_next_filter(&iter, &b->keys, bch_ptr_bad))) {
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002155 ret = !b->level
2156 ? fn(op, b, k)
2157 : btree(map_keys_recurse, k, b, op, from, fn, flags);
2158 from = NULL;
2159
2160 if (ret != MAP_CONTINUE)
2161 return ret;
2162 }
2163
2164 if (!b->level && (flags & MAP_END_KEY))
2165 ret = fn(op, b, &KEY(KEY_INODE(&b->key),
2166 KEY_OFFSET(&b->key), 0));
2167
2168 return ret;
2169}
2170
2171int bch_btree_map_keys(struct btree_op *op, struct cache_set *c,
2172 struct bkey *from, btree_map_keys_fn *fn, int flags)
2173{
Kent Overstreetb54d6932013-07-24 18:04:18 -07002174 return btree_root(map_keys_recurse, c, op, from, fn, flags);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002175}
2176
Kent Overstreetcafe5632013-03-23 16:11:31 -07002177/* Keybuf code */
2178
2179static inline int keybuf_cmp(struct keybuf_key *l, struct keybuf_key *r)
2180{
2181 /* Overlapping keys compare equal */
2182 if (bkey_cmp(&l->key, &START_KEY(&r->key)) <= 0)
2183 return -1;
2184 if (bkey_cmp(&START_KEY(&l->key), &r->key) >= 0)
2185 return 1;
2186 return 0;
2187}
2188
2189static inline int keybuf_nonoverlapping_cmp(struct keybuf_key *l,
2190 struct keybuf_key *r)
2191{
2192 return clamp_t(int64_t, bkey_cmp(&l->key, &r->key), -1, 1);
2193}
2194
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002195struct refill {
2196 struct btree_op op;
Kent Overstreet48a915a2013-10-31 15:43:22 -07002197 unsigned nr_found;
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002198 struct keybuf *buf;
2199 struct bkey *end;
2200 keybuf_pred_fn *pred;
2201};
2202
2203static int refill_keybuf_fn(struct btree_op *op, struct btree *b,
2204 struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002205{
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002206 struct refill *refill = container_of(op, struct refill, op);
2207 struct keybuf *buf = refill->buf;
2208 int ret = MAP_CONTINUE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002209
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002210 if (bkey_cmp(k, refill->end) >= 0) {
2211 ret = MAP_DONE;
2212 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002213 }
2214
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002215 if (!KEY_SIZE(k)) /* end key */
2216 goto out;
2217
2218 if (refill->pred(buf, k)) {
2219 struct keybuf_key *w;
2220
2221 spin_lock(&buf->lock);
2222
2223 w = array_alloc(&buf->freelist);
2224 if (!w) {
2225 spin_unlock(&buf->lock);
2226 return MAP_DONE;
2227 }
2228
2229 w->private = NULL;
2230 bkey_copy(&w->key, k);
2231
2232 if (RB_INSERT(&buf->keys, w, node, keybuf_cmp))
2233 array_free(&buf->freelist, w);
Kent Overstreet48a915a2013-10-31 15:43:22 -07002234 else
2235 refill->nr_found++;
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002236
2237 if (array_freelist_empty(&buf->freelist))
2238 ret = MAP_DONE;
2239
2240 spin_unlock(&buf->lock);
2241 }
2242out:
2243 buf->last_scanned = *k;
2244 return ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002245}
2246
2247void bch_refill_keybuf(struct cache_set *c, struct keybuf *buf,
Kent Overstreet72c27062013-06-05 06:24:39 -07002248 struct bkey *end, keybuf_pred_fn *pred)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002249{
2250 struct bkey start = buf->last_scanned;
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002251 struct refill refill;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002252
2253 cond_resched();
2254
Kent Overstreetb54d6932013-07-24 18:04:18 -07002255 bch_btree_op_init(&refill.op, -1);
Kent Overstreet48a915a2013-10-31 15:43:22 -07002256 refill.nr_found = 0;
2257 refill.buf = buf;
2258 refill.end = end;
2259 refill.pred = pred;
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002260
2261 bch_btree_map_keys(&refill.op, c, &buf->last_scanned,
2262 refill_keybuf_fn, MAP_END_KEY);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002263
Kent Overstreet48a915a2013-10-31 15:43:22 -07002264 trace_bcache_keyscan(refill.nr_found,
2265 KEY_INODE(&start), KEY_OFFSET(&start),
2266 KEY_INODE(&buf->last_scanned),
2267 KEY_OFFSET(&buf->last_scanned));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002268
2269 spin_lock(&buf->lock);
2270
2271 if (!RB_EMPTY_ROOT(&buf->keys)) {
2272 struct keybuf_key *w;
2273 w = RB_FIRST(&buf->keys, struct keybuf_key, node);
2274 buf->start = START_KEY(&w->key);
2275
2276 w = RB_LAST(&buf->keys, struct keybuf_key, node);
2277 buf->end = w->key;
2278 } else {
2279 buf->start = MAX_KEY;
2280 buf->end = MAX_KEY;
2281 }
2282
2283 spin_unlock(&buf->lock);
2284}
2285
2286static void __bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w)
2287{
2288 rb_erase(&w->node, &buf->keys);
2289 array_free(&buf->freelist, w);
2290}
2291
2292void bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w)
2293{
2294 spin_lock(&buf->lock);
2295 __bch_keybuf_del(buf, w);
2296 spin_unlock(&buf->lock);
2297}
2298
2299bool bch_keybuf_check_overlapping(struct keybuf *buf, struct bkey *start,
2300 struct bkey *end)
2301{
2302 bool ret = false;
2303 struct keybuf_key *p, *w, s;
2304 s.key = *start;
2305
2306 if (bkey_cmp(end, &buf->start) <= 0 ||
2307 bkey_cmp(start, &buf->end) >= 0)
2308 return false;
2309
2310 spin_lock(&buf->lock);
2311 w = RB_GREATER(&buf->keys, s, node, keybuf_nonoverlapping_cmp);
2312
2313 while (w && bkey_cmp(&START_KEY(&w->key), end) < 0) {
2314 p = w;
2315 w = RB_NEXT(w, node);
2316
2317 if (p->private)
2318 ret = true;
2319 else
2320 __bch_keybuf_del(buf, p);
2321 }
2322
2323 spin_unlock(&buf->lock);
2324 return ret;
2325}
2326
2327struct keybuf_key *bch_keybuf_next(struct keybuf *buf)
2328{
2329 struct keybuf_key *w;
2330 spin_lock(&buf->lock);
2331
2332 w = RB_FIRST(&buf->keys, struct keybuf_key, node);
2333
2334 while (w && w->private)
2335 w = RB_NEXT(w, node);
2336
2337 if (w)
2338 w->private = ERR_PTR(-EINTR);
2339
2340 spin_unlock(&buf->lock);
2341 return w;
2342}
2343
2344struct keybuf_key *bch_keybuf_next_rescan(struct cache_set *c,
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002345 struct keybuf *buf,
2346 struct bkey *end,
2347 keybuf_pred_fn *pred)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002348{
2349 struct keybuf_key *ret;
2350
2351 while (1) {
2352 ret = bch_keybuf_next(buf);
2353 if (ret)
2354 break;
2355
2356 if (bkey_cmp(&buf->last_scanned, end) >= 0) {
2357 pr_debug("scan finished");
2358 break;
2359 }
2360
Kent Overstreet72c27062013-06-05 06:24:39 -07002361 bch_refill_keybuf(c, buf, end, pred);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002362 }
2363
2364 return ret;
2365}
2366
Kent Overstreet72c27062013-06-05 06:24:39 -07002367void bch_keybuf_init(struct keybuf *buf)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002368{
Kent Overstreetcafe5632013-03-23 16:11:31 -07002369 buf->last_scanned = MAX_KEY;
2370 buf->keys = RB_ROOT;
2371
2372 spin_lock_init(&buf->lock);
2373 array_allocator_init(&buf->freelist);
2374}
2375
2376void bch_btree_exit(void)
2377{
2378 if (btree_io_wq)
2379 destroy_workqueue(btree_io_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002380}
2381
2382int __init bch_btree_init(void)
2383{
Kent Overstreet72a44512013-10-24 17:19:26 -07002384 btree_io_wq = create_singlethread_workqueue("bch_btree_io");
2385 if (!btree_io_wq)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002386 return -ENOMEM;
2387
2388 return 0;
2389}