blob: 5d7dee8bb8507a3de2cb60d48af4cd2f5f00cb35 [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 Overstreet279afba2013-06-05 06:21:07 -070027#include "writeback.h"
Kent Overstreetcafe5632013-03-23 16:11:31 -070028
29#include <linux/slab.h>
30#include <linux/bitops.h>
Kent Overstreet72a44512013-10-24 17:19:26 -070031#include <linux/freezer.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070032#include <linux/hash.h>
Kent Overstreet72a44512013-10-24 17:19:26 -070033#include <linux/kthread.h>
Geert Uytterhoevencd953ed2013-03-27 18:56:28 +010034#include <linux/prefetch.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070035#include <linux/random.h>
36#include <linux/rcupdate.h>
37#include <trace/events/bcache.h>
38
39/*
40 * Todo:
41 * register_bcache: Return errors out to userspace correctly
42 *
43 * Writeback: don't undirty key until after a cache flush
44 *
45 * Create an iterator for key pointers
46 *
47 * On btree write error, mark bucket such that it won't be freed from the cache
48 *
49 * Journalling:
50 * Check for bad keys in replay
51 * Propagate barriers
52 * Refcount journal entries in journal_replay
53 *
54 * Garbage collection:
55 * Finish incremental gc
56 * Gc should free old UUIDs, data for invalid UUIDs
57 *
58 * Provide a way to list backing device UUIDs we have data cached for, and
59 * probably how long it's been since we've seen them, and a way to invalidate
60 * dirty data for devices that will never be attached again
61 *
62 * Keep 1 min/5 min/15 min statistics of how busy a block device has been, so
63 * that based on that and how much dirty data we have we can keep writeback
64 * from being starved
65 *
66 * Add a tracepoint or somesuch to watch for writeback starvation
67 *
68 * When btree depth > 1 and splitting an interior node, we have to make sure
69 * alloc_bucket() cannot fail. This should be true but is not completely
70 * obvious.
71 *
72 * Make sure all allocations get charged to the root cgroup
73 *
74 * Plugging?
75 *
76 * If data write is less than hard sector size of ssd, round up offset in open
77 * bucket to the next whole sector
78 *
79 * Also lookup by cgroup in get_open_bucket()
80 *
81 * Superblock needs to be fleshed out for multiple cache devices
82 *
83 * Add a sysfs tunable for the number of writeback IOs in flight
84 *
85 * Add a sysfs tunable for the number of open data buckets
86 *
87 * IO tracking: Can we track when one process is doing io on behalf of another?
88 * IO tracking: Don't use just an average, weigh more recent stuff higher
89 *
90 * Test module load/unload
91 */
92
Kent Overstreetdf8e8972013-07-24 17:37:59 -070093enum {
94 BTREE_INSERT_STATUS_INSERT,
95 BTREE_INSERT_STATUS_BACK_MERGE,
96 BTREE_INSERT_STATUS_OVERWROTE,
97 BTREE_INSERT_STATUS_FRONT_MERGE,
98};
99
Kent Overstreetcafe5632013-03-23 16:11:31 -0700100#define MAX_NEED_GC 64
101#define MAX_SAVE_PRIO 72
102
103#define PTR_DIRTY_BIT (((uint64_t) 1 << 36))
104
105#define PTR_HASH(c, k) \
106 (((k)->ptr[0] >> c->bucket_bits) | PTR_GEN(k, 0))
107
Kent Overstreetcafe5632013-03-23 16:11:31 -0700108static struct workqueue_struct *btree_io_wq;
109
Kent Overstreetdf8e8972013-07-24 17:37:59 -0700110#define insert_lock(s, b) ((b)->level <= (s)->lock)
111
112/*
113 * These macros are for recursing down the btree - they handle the details of
114 * locking and looking up nodes in the cache for you. They're best treated as
115 * mere syntax when reading code that uses them.
116 *
117 * op->lock determines whether we take a read or a write lock at a given depth.
118 * If you've got a read lock and find that you need a write lock (i.e. you're
119 * going to have to split), set op->lock and return -EINTR; btree_root() will
120 * call you again and you'll have the correct lock.
121 */
122
123/**
124 * btree - recurse down the btree on a specified key
125 * @fn: function to call, which will be passed the child node
126 * @key: key to recurse on
127 * @b: parent btree node
128 * @op: pointer to struct btree_op
129 */
130#define btree(fn, key, b, op, ...) \
131({ \
132 int _r, l = (b)->level - 1; \
133 bool _w = l <= (op)->lock; \
134 struct btree *_child = bch_btree_node_get((b)->c, key, l, _w); \
135 if (!IS_ERR(_child)) { \
136 _child->parent = (b); \
137 _r = bch_btree_ ## fn(_child, op, ##__VA_ARGS__); \
138 rw_unlock(_w, _child); \
139 } else \
140 _r = PTR_ERR(_child); \
141 _r; \
142})
143
144/**
145 * btree_root - call a function on the root of the btree
146 * @fn: function to call, which will be passed the child node
147 * @c: cache set
148 * @op: pointer to struct btree_op
149 */
150#define btree_root(fn, c, op, ...) \
151({ \
152 int _r = -EINTR; \
153 do { \
154 struct btree *_b = (c)->root; \
155 bool _w = insert_lock(op, _b); \
156 rw_lock(_w, _b, _b->level); \
157 if (_b == (c)->root && \
158 _w == insert_lock(op, _b)) { \
159 _b->parent = NULL; \
160 _r = bch_btree_ ## fn(_b, op, ##__VA_ARGS__); \
161 } \
162 rw_unlock(_w, _b); \
Kent Overstreet78365412013-12-17 01:29:34 -0800163 if (_r == -EINTR) \
164 schedule(); \
Kent Overstreetdf8e8972013-07-24 17:37:59 -0700165 bch_cannibalize_unlock(c); \
166 if (_r == -ENOSPC) { \
167 wait_event((c)->try_wait, \
168 !(c)->try_harder); \
169 _r = -EINTR; \
170 } \
171 } while (_r == -EINTR); \
172 \
Kent Overstreet78365412013-12-17 01:29:34 -0800173 finish_wait(&(c)->bucket_wait, &(op)->wait); \
Kent Overstreetdf8e8972013-07-24 17:37:59 -0700174 _r; \
175})
176
Kent Overstreeta85e9682013-12-20 17:28:16 -0800177static inline struct bset *write_block(struct btree *b)
178{
179 return ((void *) btree_bset_first(b)) + b->written * block_bytes(b->c);
180}
181
182static inline bool should_split(struct btree *b)
183{
184 struct bset *i = write_block(b);
185 return b->written >= btree_blocks(b) ||
186 (b->written + __set_blocks(i, i->keys + 15, block_bytes(b->c))
187 > btree_blocks(b));
188}
189
Kent Overstreetcafe5632013-03-23 16:11:31 -0700190/* Btree key manipulation */
191
Kent Overstreet3a3b6a42013-07-24 16:46:42 -0700192void bkey_put(struct cache_set *c, struct bkey *k)
Kent Overstreete7c590e2013-09-10 18:39:16 -0700193{
194 unsigned i;
195
196 for (i = 0; i < KEY_PTRS(k); i++)
197 if (ptr_available(c, k, i))
198 atomic_dec_bug(&PTR_BUCKET(c, k, i)->pin);
199}
200
Kent Overstreetcafe5632013-03-23 16:11:31 -0700201/* Btree IO */
202
203static uint64_t btree_csum_set(struct btree *b, struct bset *i)
204{
205 uint64_t crc = b->key.ptr[0];
Kent Overstreetfafff812013-12-17 21:56:21 -0800206 void *data = (void *) i + 8, *end = bset_bkey_last(i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700207
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600208 crc = bch_crc64_update(crc, data, end - data);
Kent Overstreetc19ed232013-03-26 13:49:02 -0700209 return crc ^ 0xffffffffffffffffULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700210}
211
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800212void bch_btree_node_read_done(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700213{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700214 const char *err = "bad btree header";
Kent Overstreetee811282013-12-17 23:49:49 -0800215 struct bset *i = btree_bset_first(b);
Kent Overstreet57943512013-04-25 13:58:35 -0700216 struct btree_iter *iter;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700217
Kent Overstreet57943512013-04-25 13:58:35 -0700218 iter = mempool_alloc(b->c->fill_iter, GFP_NOWAIT);
219 iter->size = b->c->sb.bucket_size / b->c->sb.block_size;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700220 iter->used = 0;
221
Kent Overstreet280481d2013-10-24 16:36:03 -0700222#ifdef CONFIG_BCACHE_DEBUG
223 iter->b = b;
224#endif
225
Kent Overstreet57943512013-04-25 13:58:35 -0700226 if (!i->seq)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700227 goto err;
228
229 for (;
Kent Overstreeta85e9682013-12-20 17:28:16 -0800230 b->written < btree_blocks(b) && i->seq == b->keys.set[0].data->seq;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700231 i = write_block(b)) {
232 err = "unsupported bset version";
233 if (i->version > BCACHE_BSET_VERSION)
234 goto err;
235
236 err = "bad btree header";
Kent Overstreetee811282013-12-17 23:49:49 -0800237 if (b->written + set_blocks(i, block_bytes(b->c)) >
238 btree_blocks(b))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700239 goto err;
240
241 err = "bad magic";
Kent Overstreet81ab4192013-10-31 15:46:42 -0700242 if (i->magic != bset_magic(&b->c->sb))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700243 goto err;
244
245 err = "bad checksum";
246 switch (i->version) {
247 case 0:
248 if (i->csum != csum_set(i))
249 goto err;
250 break;
251 case BCACHE_BSET_VERSION:
252 if (i->csum != btree_csum_set(b, i))
253 goto err;
254 break;
255 }
256
257 err = "empty set";
Kent Overstreeta85e9682013-12-20 17:28:16 -0800258 if (i != b->keys.set[0].data && !i->keys)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700259 goto err;
260
Kent Overstreetfafff812013-12-17 21:56:21 -0800261 bch_btree_iter_push(iter, i->start, bset_bkey_last(i));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700262
Kent Overstreetee811282013-12-17 23:49:49 -0800263 b->written += set_blocks(i, block_bytes(b->c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700264 }
265
266 err = "corrupted btree";
267 for (i = write_block(b);
Kent Overstreeta85e9682013-12-20 17:28:16 -0800268 bset_sector_offset(&b->keys, i) < KEY_SIZE(&b->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700269 i = ((void *) i) + block_bytes(b->c))
Kent Overstreeta85e9682013-12-20 17:28:16 -0800270 if (i->seq == b->keys.set[0].data->seq)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700271 goto err;
272
Kent Overstreeta85e9682013-12-20 17:28:16 -0800273 bch_btree_sort_and_fix_extents(&b->keys, iter, &b->c->sort);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700274
Kent Overstreeta85e9682013-12-20 17:28:16 -0800275 i = b->keys.set[0].data;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700276 err = "short btree key";
Kent Overstreeta85e9682013-12-20 17:28:16 -0800277 if (b->keys.set[0].size &&
278 bkey_cmp(&b->key, &b->keys.set[0].end) < 0)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700279 goto err;
280
281 if (b->written < btree_blocks(b))
Kent Overstreeta85e9682013-12-20 17:28:16 -0800282 bch_bset_init_next(&b->keys, write_block(b),
283 bset_magic(&b->c->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700284out:
Kent Overstreet57943512013-04-25 13:58:35 -0700285 mempool_free(iter, b->c->fill_iter);
286 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700287err:
288 set_btree_node_io_error(b);
Kent Overstreet88b9f8c2013-12-17 21:46:35 -0800289 bch_cache_set_error(b->c, "%s at bucket %zu, block %u, %u keys",
Kent Overstreetcafe5632013-03-23 16:11:31 -0700290 err, PTR_BUCKET_NR(b->c, &b->key, 0),
Kent Overstreet88b9f8c2013-12-17 21:46:35 -0800291 bset_block_offset(b, i), i->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700292 goto out;
293}
294
Kent Overstreet57943512013-04-25 13:58:35 -0700295static void btree_node_read_endio(struct bio *bio, int error)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700296{
Kent Overstreet57943512013-04-25 13:58:35 -0700297 struct closure *cl = bio->bi_private;
298 closure_put(cl);
299}
Kent Overstreetcafe5632013-03-23 16:11:31 -0700300
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800301static void bch_btree_node_read(struct btree *b)
Kent Overstreet57943512013-04-25 13:58:35 -0700302{
303 uint64_t start_time = local_clock();
304 struct closure cl;
305 struct bio *bio;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700306
Kent Overstreetc37511b2013-04-26 15:39:55 -0700307 trace_bcache_btree_read(b);
308
Kent Overstreet57943512013-04-25 13:58:35 -0700309 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700310
Kent Overstreet57943512013-04-25 13:58:35 -0700311 bio = bch_bbio_alloc(b->c);
312 bio->bi_rw = REQ_META|READ_SYNC;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700313 bio->bi_iter.bi_size = KEY_SIZE(&b->key) << 9;
Kent Overstreet57943512013-04-25 13:58:35 -0700314 bio->bi_end_io = btree_node_read_endio;
315 bio->bi_private = &cl;
316
Kent Overstreeta85e9682013-12-20 17:28:16 -0800317 bch_bio_map(bio, b->keys.set[0].data);
Kent Overstreet57943512013-04-25 13:58:35 -0700318
Kent Overstreet57943512013-04-25 13:58:35 -0700319 bch_submit_bbio(bio, b->c, &b->key, 0);
320 closure_sync(&cl);
321
322 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
323 set_btree_node_io_error(b);
324
325 bch_bbio_free(bio, b->c);
326
327 if (btree_node_io_error(b))
328 goto err;
329
330 bch_btree_node_read_done(b);
Kent Overstreet57943512013-04-25 13:58:35 -0700331 bch_time_stats_update(&b->c->btree_read_time, start_time);
Kent Overstreet57943512013-04-25 13:58:35 -0700332
333 return;
334err:
Geert Uytterhoeven61cbd252013-09-23 23:17:30 -0700335 bch_cache_set_error(b->c, "io error reading bucket %zu",
Kent Overstreet57943512013-04-25 13:58:35 -0700336 PTR_BUCKET_NR(b->c, &b->key, 0));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700337}
338
339static void btree_complete_write(struct btree *b, struct btree_write *w)
340{
341 if (w->prio_blocked &&
342 !atomic_sub_return(w->prio_blocked, &b->c->prio_blocked))
Kent Overstreet119ba0f2013-04-24 19:01:12 -0700343 wake_up_allocators(b->c);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700344
345 if (w->journal) {
346 atomic_dec_bug(w->journal);
347 __closure_wake_up(&b->c->journal.wait);
348 }
349
Kent Overstreetcafe5632013-03-23 16:11:31 -0700350 w->prio_blocked = 0;
351 w->journal = NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700352}
353
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800354static void btree_node_write_unlock(struct closure *cl)
355{
356 struct btree *b = container_of(cl, struct btree, io);
357
358 up(&b->io_mutex);
359}
360
Kent Overstreet57943512013-04-25 13:58:35 -0700361static void __btree_node_write_done(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700362{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800363 struct btree *b = container_of(cl, struct btree, io);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700364 struct btree_write *w = btree_prev_write(b);
365
366 bch_bbio_free(b->bio, b->c);
367 b->bio = NULL;
368 btree_complete_write(b, w);
369
370 if (btree_node_dirty(b))
371 queue_delayed_work(btree_io_wq, &b->work,
372 msecs_to_jiffies(30000));
373
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800374 closure_return_with_destructor(cl, btree_node_write_unlock);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700375}
376
Kent Overstreet57943512013-04-25 13:58:35 -0700377static void btree_node_write_done(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700378{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800379 struct btree *b = container_of(cl, struct btree, io);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700380 struct bio_vec *bv;
381 int n;
382
Kent Overstreet79886132013-11-23 17:19:00 -0800383 bio_for_each_segment_all(bv, b->bio, n)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700384 __free_page(bv->bv_page);
385
Kent Overstreet57943512013-04-25 13:58:35 -0700386 __btree_node_write_done(cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700387}
388
Kent Overstreet57943512013-04-25 13:58:35 -0700389static void btree_node_write_endio(struct bio *bio, int error)
390{
391 struct closure *cl = bio->bi_private;
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800392 struct btree *b = container_of(cl, struct btree, io);
Kent Overstreet57943512013-04-25 13:58:35 -0700393
394 if (error)
395 set_btree_node_io_error(b);
396
397 bch_bbio_count_io_errors(b->c, bio, error, "writing btree");
398 closure_put(cl);
399}
400
401static void do_btree_node_write(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700402{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800403 struct closure *cl = &b->io;
Kent Overstreetee811282013-12-17 23:49:49 -0800404 struct bset *i = btree_bset_last(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700405 BKEY_PADDED(key) k;
406
407 i->version = BCACHE_BSET_VERSION;
408 i->csum = btree_csum_set(b, i);
409
Kent Overstreet57943512013-04-25 13:58:35 -0700410 BUG_ON(b->bio);
411 b->bio = bch_bbio_alloc(b->c);
412
413 b->bio->bi_end_io = btree_node_write_endio;
Kent Overstreetfaadf0c2013-11-01 18:03:08 -0700414 b->bio->bi_private = cl;
Kent Overstreete49c7c32013-06-26 17:25:38 -0700415 b->bio->bi_rw = REQ_META|WRITE_SYNC|REQ_FUA;
Kent Overstreetee811282013-12-17 23:49:49 -0800416 b->bio->bi_iter.bi_size = roundup(set_bytes(i), block_bytes(b->c));
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600417 bch_bio_map(b->bio, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700418
Kent Overstreete49c7c32013-06-26 17:25:38 -0700419 /*
420 * If we're appending to a leaf node, we don't technically need FUA -
421 * this write just needs to be persisted before the next journal write,
422 * which will be marked FLUSH|FUA.
423 *
424 * Similarly if we're writing a new btree root - the pointer is going to
425 * be in the next journal entry.
426 *
427 * But if we're writing a new btree node (that isn't a root) or
428 * appending to a non leaf btree node, we need either FUA or a flush
429 * when we write the parent with the new pointer. FUA is cheaper than a
430 * flush, and writes appending to leaf nodes aren't blocking anything so
431 * just make all btree node writes FUA to keep things sane.
432 */
433
Kent Overstreetcafe5632013-03-23 16:11:31 -0700434 bkey_copy(&k.key, &b->key);
Kent Overstreetee811282013-12-17 23:49:49 -0800435 SET_PTR_OFFSET(&k.key, 0, PTR_OFFSET(&k.key, 0) +
Kent Overstreeta85e9682013-12-20 17:28:16 -0800436 bset_sector_offset(&b->keys, i));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700437
Kent Overstreet8e51e412013-06-06 18:15:57 -0700438 if (!bio_alloc_pages(b->bio, GFP_NOIO)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700439 int j;
440 struct bio_vec *bv;
441 void *base = (void *) ((unsigned long) i & ~(PAGE_SIZE - 1));
442
Kent Overstreet79886132013-11-23 17:19:00 -0800443 bio_for_each_segment_all(bv, b->bio, j)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700444 memcpy(page_address(bv->bv_page),
445 base + j * PAGE_SIZE, PAGE_SIZE);
446
Kent Overstreetcafe5632013-03-23 16:11:31 -0700447 bch_submit_bbio(b->bio, b->c, &k.key, 0);
448
Kent Overstreet57943512013-04-25 13:58:35 -0700449 continue_at(cl, btree_node_write_done, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700450 } else {
451 b->bio->bi_vcnt = 0;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600452 bch_bio_map(b->bio, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700453
Kent Overstreetcafe5632013-03-23 16:11:31 -0700454 bch_submit_bbio(b->bio, b->c, &k.key, 0);
455
456 closure_sync(cl);
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800457 continue_at_nobarrier(cl, __btree_node_write_done, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700458 }
459}
460
Kent Overstreet57943512013-04-25 13:58:35 -0700461void bch_btree_node_write(struct btree *b, struct closure *parent)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700462{
Kent Overstreetee811282013-12-17 23:49:49 -0800463 struct bset *i = btree_bset_last(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700464
Kent Overstreetc37511b2013-04-26 15:39:55 -0700465 trace_bcache_btree_write(b);
466
Kent Overstreetcafe5632013-03-23 16:11:31 -0700467 BUG_ON(current->bio_list);
Kent Overstreet57943512013-04-25 13:58:35 -0700468 BUG_ON(b->written >= btree_blocks(b));
469 BUG_ON(b->written && !i->keys);
Kent Overstreetee811282013-12-17 23:49:49 -0800470 BUG_ON(btree_bset_first(b)->seq != i->seq);
Kent Overstreet280481d2013-10-24 16:36:03 -0700471 bch_check_keys(b, "writing");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700472
Kent Overstreetcafe5632013-03-23 16:11:31 -0700473 cancel_delayed_work(&b->work);
474
Kent Overstreet57943512013-04-25 13:58:35 -0700475 /* If caller isn't waiting for write, parent refcount is cache set */
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800476 down(&b->io_mutex);
477 closure_init(&b->io, parent ?: &b->c->cl);
Kent Overstreet57943512013-04-25 13:58:35 -0700478
Kent Overstreetcafe5632013-03-23 16:11:31 -0700479 clear_bit(BTREE_NODE_dirty, &b->flags);
480 change_bit(BTREE_NODE_write_idx, &b->flags);
481
Kent Overstreet57943512013-04-25 13:58:35 -0700482 do_btree_node_write(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700483
Kent Overstreetee811282013-12-17 23:49:49 -0800484 atomic_long_add(set_blocks(i, block_bytes(b->c)) * b->c->sb.block_size,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700485 &PTR_CACHE(b->c, &b->key, 0)->btree_sectors_written);
486
Kent Overstreeta85e9682013-12-20 17:28:16 -0800487 b->written += set_blocks(i, block_bytes(b->c));
488
Kent Overstreet67539e82013-09-10 22:53:34 -0700489 /* If not a leaf node, always sort */
Kent Overstreeta85e9682013-12-20 17:28:16 -0800490 if (b->level && b->keys.nsets)
Kent Overstreet67539e82013-09-10 22:53:34 -0700491 bch_btree_sort(b, &b->c->sort);
492 else
493 bch_btree_sort_lazy(b, &b->c->sort);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700494
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800495 /*
496 * do verify if there was more than one set initially (i.e. we did a
497 * sort) and we sorted down to a single set:
498 */
Kent Overstreeta85e9682013-12-20 17:28:16 -0800499 if (i != b->keys.set->data && !b->keys.nsets)
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800500 bch_btree_verify(b);
501
Kent Overstreetcafe5632013-03-23 16:11:31 -0700502 if (b->written < btree_blocks(b))
Kent Overstreeta85e9682013-12-20 17:28:16 -0800503 bch_bset_init_next(&b->keys, write_block(b),
504 bset_magic(&b->c->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700505}
506
Kent Overstreetf269af52013-07-23 20:48:29 -0700507static void bch_btree_node_write_sync(struct btree *b)
508{
509 struct closure cl;
510
511 closure_init_stack(&cl);
512 bch_btree_node_write(b, &cl);
513 closure_sync(&cl);
514}
515
Kent Overstreet57943512013-04-25 13:58:35 -0700516static void btree_node_write_work(struct work_struct *w)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700517{
518 struct btree *b = container_of(to_delayed_work(w), struct btree, work);
519
Kent Overstreet57943512013-04-25 13:58:35 -0700520 rw_lock(true, b, b->level);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700521
522 if (btree_node_dirty(b))
Kent Overstreet57943512013-04-25 13:58:35 -0700523 bch_btree_node_write(b, NULL);
524 rw_unlock(true, b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700525}
526
Kent Overstreetc18536a2013-07-24 17:44:17 -0700527static void bch_btree_leaf_dirty(struct btree *b, atomic_t *journal_ref)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700528{
Kent Overstreetee811282013-12-17 23:49:49 -0800529 struct bset *i = btree_bset_last(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700530 struct btree_write *w = btree_current_write(b);
531
Kent Overstreet57943512013-04-25 13:58:35 -0700532 BUG_ON(!b->written);
533 BUG_ON(!i->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700534
Kent Overstreet57943512013-04-25 13:58:35 -0700535 if (!btree_node_dirty(b))
536 queue_delayed_work(btree_io_wq, &b->work, 30 * HZ);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700537
Kent Overstreet57943512013-04-25 13:58:35 -0700538 set_btree_node_dirty(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700539
Kent Overstreetc18536a2013-07-24 17:44:17 -0700540 if (journal_ref) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700541 if (w->journal &&
Kent Overstreetc18536a2013-07-24 17:44:17 -0700542 journal_pin_cmp(b->c, w->journal, journal_ref)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700543 atomic_dec_bug(w->journal);
544 w->journal = NULL;
545 }
546
547 if (!w->journal) {
Kent Overstreetc18536a2013-07-24 17:44:17 -0700548 w->journal = journal_ref;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700549 atomic_inc(w->journal);
550 }
551 }
552
Kent Overstreetcafe5632013-03-23 16:11:31 -0700553 /* Force write if set is too big */
Kent Overstreet57943512013-04-25 13:58:35 -0700554 if (set_bytes(i) > PAGE_SIZE - 48 &&
555 !current->bio_list)
556 bch_btree_node_write(b, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700557}
558
559/*
560 * Btree in memory cache - allocation/freeing
561 * mca -> memory cache
562 */
563
Kent Overstreetcafe5632013-03-23 16:11:31 -0700564#define mca_reserve(c) (((c->root && c->root->level) \
565 ? c->root->level : 1) * 8 + 16)
566#define mca_can_free(c) \
567 max_t(int, 0, c->bucket_cache_used - mca_reserve(c))
568
569static void mca_data_free(struct btree *b)
570{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800571 BUG_ON(b->io_mutex.count != 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700572
Kent Overstreeta85e9682013-12-20 17:28:16 -0800573 bch_btree_keys_free(&b->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700574
Kent Overstreetcafe5632013-03-23 16:11:31 -0700575 b->c->bucket_cache_used--;
Kent Overstreetee811282013-12-17 23:49:49 -0800576 list_move(&b->list, &b->c->btree_cache_freed);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700577}
578
579static void mca_bucket_free(struct btree *b)
580{
581 BUG_ON(btree_node_dirty(b));
582
583 b->key.ptr[0] = 0;
584 hlist_del_init_rcu(&b->hash);
585 list_move(&b->list, &b->c->btree_cache_freeable);
586}
587
588static unsigned btree_order(struct bkey *k)
589{
590 return ilog2(KEY_SIZE(k) / PAGE_SECTORS ?: 1);
591}
592
593static void mca_data_alloc(struct btree *b, struct bkey *k, gfp_t gfp)
594{
Kent Overstreeta85e9682013-12-20 17:28:16 -0800595 if (!bch_btree_keys_alloc(&b->keys,
Kent Overstreetee811282013-12-17 23:49:49 -0800596 max_t(unsigned,
597 ilog2(b->c->btree_pages),
598 btree_order(k)),
599 gfp)) {
600 b->c->bucket_cache_used++;
601 list_move(&b->list, &b->c->btree_cache);
602 } else {
603 list_move(&b->list, &b->c->btree_cache_freed);
604 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700605}
606
607static struct btree *mca_bucket_alloc(struct cache_set *c,
608 struct bkey *k, gfp_t gfp)
609{
610 struct btree *b = kzalloc(sizeof(struct btree), gfp);
611 if (!b)
612 return NULL;
613
614 init_rwsem(&b->lock);
615 lockdep_set_novalidate_class(&b->lock);
616 INIT_LIST_HEAD(&b->list);
Kent Overstreet57943512013-04-25 13:58:35 -0700617 INIT_DELAYED_WORK(&b->work, btree_node_write_work);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700618 b->c = c;
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800619 sema_init(&b->io_mutex, 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700620
621 mca_data_alloc(b, k, gfp);
622 return b;
623}
624
Kent Overstreete8e1d462013-07-24 17:27:07 -0700625static int mca_reap(struct btree *b, unsigned min_order, bool flush)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700626{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700627 struct closure cl;
628
629 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700630 lockdep_assert_held(&b->c->bucket_lock);
631
632 if (!down_write_trylock(&b->lock))
633 return -ENOMEM;
634
Kent Overstreeta85e9682013-12-20 17:28:16 -0800635 BUG_ON(btree_node_dirty(b) && !b->keys.set[0].data);
Kent Overstreete8e1d462013-07-24 17:27:07 -0700636
Kent Overstreeta85e9682013-12-20 17:28:16 -0800637 if (b->keys.page_order < min_order)
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800638 goto out_unlock;
639
640 if (!flush) {
641 if (btree_node_dirty(b))
642 goto out_unlock;
643
644 if (down_trylock(&b->io_mutex))
645 goto out_unlock;
646 up(&b->io_mutex);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700647 }
648
Kent Overstreetf269af52013-07-23 20:48:29 -0700649 if (btree_node_dirty(b))
650 bch_btree_node_write_sync(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700651
Kent Overstreete8e1d462013-07-24 17:27:07 -0700652 /* wait for any in flight btree write */
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800653 down(&b->io_mutex);
654 up(&b->io_mutex);
Kent Overstreete8e1d462013-07-24 17:27:07 -0700655
Kent Overstreetcafe5632013-03-23 16:11:31 -0700656 return 0;
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800657out_unlock:
658 rw_unlock(true, b);
659 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700660}
661
Dave Chinner7dc19d52013-08-28 10:18:11 +1000662static unsigned long bch_mca_scan(struct shrinker *shrink,
663 struct shrink_control *sc)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700664{
665 struct cache_set *c = container_of(shrink, struct cache_set, shrink);
666 struct btree *b, *t;
667 unsigned long i, nr = sc->nr_to_scan;
Dave Chinner7dc19d52013-08-28 10:18:11 +1000668 unsigned long freed = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700669
670 if (c->shrinker_disabled)
Dave Chinner7dc19d52013-08-28 10:18:11 +1000671 return SHRINK_STOP;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700672
673 if (c->try_harder)
Dave Chinner7dc19d52013-08-28 10:18:11 +1000674 return SHRINK_STOP;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700675
676 /* Return -1 if we can't do anything right now */
Kent Overstreeta698e082013-09-23 23:17:34 -0700677 if (sc->gfp_mask & __GFP_IO)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700678 mutex_lock(&c->bucket_lock);
679 else if (!mutex_trylock(&c->bucket_lock))
680 return -1;
681
Kent Overstreet36c9ea92013-06-03 13:04:56 -0700682 /*
683 * It's _really_ critical that we don't free too many btree nodes - we
684 * have to always leave ourselves a reserve. The reserve is how we
685 * guarantee that allocating memory for a new btree node can always
686 * succeed, so that inserting keys into the btree can always succeed and
687 * IO can always make forward progress:
688 */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700689 nr /= c->btree_pages;
690 nr = min_t(unsigned long, nr, mca_can_free(c));
691
692 i = 0;
693 list_for_each_entry_safe(b, t, &c->btree_cache_freeable, list) {
Dave Chinner7dc19d52013-08-28 10:18:11 +1000694 if (freed >= nr)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700695 break;
696
697 if (++i > 3 &&
Kent Overstreete8e1d462013-07-24 17:27:07 -0700698 !mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700699 mca_data_free(b);
700 rw_unlock(true, b);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000701 freed++;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700702 }
703 }
704
Dave Chinner7dc19d52013-08-28 10:18:11 +1000705 for (i = 0; (nr--) && i < c->bucket_cache_used; i++) {
Kent Overstreetb0f32a52013-12-10 13:24:26 -0800706 if (list_empty(&c->btree_cache))
707 goto out;
708
Kent Overstreetcafe5632013-03-23 16:11:31 -0700709 b = list_first_entry(&c->btree_cache, struct btree, list);
710 list_rotate_left(&c->btree_cache);
711
712 if (!b->accessed &&
Kent Overstreete8e1d462013-07-24 17:27:07 -0700713 !mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700714 mca_bucket_free(b);
715 mca_data_free(b);
716 rw_unlock(true, b);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000717 freed++;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700718 } else
719 b->accessed = 0;
720 }
721out:
Kent Overstreetcafe5632013-03-23 16:11:31 -0700722 mutex_unlock(&c->bucket_lock);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000723 return freed;
724}
725
726static unsigned long bch_mca_count(struct shrinker *shrink,
727 struct shrink_control *sc)
728{
729 struct cache_set *c = container_of(shrink, struct cache_set, shrink);
730
731 if (c->shrinker_disabled)
732 return 0;
733
734 if (c->try_harder)
735 return 0;
736
737 return mca_can_free(c) * c->btree_pages;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700738}
739
740void bch_btree_cache_free(struct cache_set *c)
741{
742 struct btree *b;
743 struct closure cl;
744 closure_init_stack(&cl);
745
746 if (c->shrink.list.next)
747 unregister_shrinker(&c->shrink);
748
749 mutex_lock(&c->bucket_lock);
750
751#ifdef CONFIG_BCACHE_DEBUG
752 if (c->verify_data)
753 list_move(&c->verify_data->list, &c->btree_cache);
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800754
755 free_pages((unsigned long) c->verify_ondisk, ilog2(bucket_pages(c)));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700756#endif
757
758 list_splice(&c->btree_cache_freeable,
759 &c->btree_cache);
760
761 while (!list_empty(&c->btree_cache)) {
762 b = list_first_entry(&c->btree_cache, struct btree, list);
763
764 if (btree_node_dirty(b))
765 btree_complete_write(b, btree_current_write(b));
766 clear_bit(BTREE_NODE_dirty, &b->flags);
767
768 mca_data_free(b);
769 }
770
771 while (!list_empty(&c->btree_cache_freed)) {
772 b = list_first_entry(&c->btree_cache_freed,
773 struct btree, list);
774 list_del(&b->list);
775 cancel_delayed_work_sync(&b->work);
776 kfree(b);
777 }
778
779 mutex_unlock(&c->bucket_lock);
780}
781
782int bch_btree_cache_alloc(struct cache_set *c)
783{
784 unsigned i;
785
Kent Overstreetcafe5632013-03-23 16:11:31 -0700786 for (i = 0; i < mca_reserve(c); i++)
Kent Overstreet72a44512013-10-24 17:19:26 -0700787 if (!mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL))
788 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700789
790 list_splice_init(&c->btree_cache,
791 &c->btree_cache_freeable);
792
793#ifdef CONFIG_BCACHE_DEBUG
794 mutex_init(&c->verify_lock);
795
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800796 c->verify_ondisk = (void *)
797 __get_free_pages(GFP_KERNEL, ilog2(bucket_pages(c)));
798
Kent Overstreetcafe5632013-03-23 16:11:31 -0700799 c->verify_data = mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL);
800
801 if (c->verify_data &&
Kent Overstreeta85e9682013-12-20 17:28:16 -0800802 c->verify_data->keys.set->data)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700803 list_del_init(&c->verify_data->list);
804 else
805 c->verify_data = NULL;
806#endif
807
Dave Chinner7dc19d52013-08-28 10:18:11 +1000808 c->shrink.count_objects = bch_mca_count;
809 c->shrink.scan_objects = bch_mca_scan;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700810 c->shrink.seeks = 4;
811 c->shrink.batch = c->btree_pages * 2;
812 register_shrinker(&c->shrink);
813
814 return 0;
815}
816
817/* Btree in memory cache - hash table */
818
819static struct hlist_head *mca_hash(struct cache_set *c, struct bkey *k)
820{
821 return &c->bucket_hash[hash_32(PTR_HASH(c, k), BUCKET_HASH_BITS)];
822}
823
824static struct btree *mca_find(struct cache_set *c, struct bkey *k)
825{
826 struct btree *b;
827
828 rcu_read_lock();
829 hlist_for_each_entry_rcu(b, mca_hash(c, k), hash)
830 if (PTR_HASH(c, &b->key) == PTR_HASH(c, k))
831 goto out;
832 b = NULL;
833out:
834 rcu_read_unlock();
835 return b;
836}
837
Kent Overstreete8e1d462013-07-24 17:27:07 -0700838static struct btree *mca_cannibalize(struct cache_set *c, struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700839{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700840 struct btree *b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700841
Kent Overstreetc37511b2013-04-26 15:39:55 -0700842 trace_bcache_btree_cache_cannibalize(c);
843
Kent Overstreete8e1d462013-07-24 17:27:07 -0700844 if (!c->try_harder) {
845 c->try_harder = current;
846 c->try_harder_start = local_clock();
847 } else if (c->try_harder != current)
848 return ERR_PTR(-ENOSPC);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700849
Kent Overstreete8e1d462013-07-24 17:27:07 -0700850 list_for_each_entry_reverse(b, &c->btree_cache, list)
851 if (!mca_reap(b, btree_order(k), false))
852 return b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700853
Kent Overstreete8e1d462013-07-24 17:27:07 -0700854 list_for_each_entry_reverse(b, &c->btree_cache, list)
855 if (!mca_reap(b, btree_order(k), true))
856 return b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700857
Kent Overstreete8e1d462013-07-24 17:27:07 -0700858 return ERR_PTR(-ENOMEM);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700859}
860
861/*
862 * We can only have one thread cannibalizing other cached btree nodes at a time,
863 * or we'll deadlock. We use an open coded mutex to ensure that, which a
864 * cannibalize_bucket() will take. This means every time we unlock the root of
865 * the btree, we need to release this lock if we have it held.
866 */
Kent Overstreetdf8e8972013-07-24 17:37:59 -0700867static void bch_cannibalize_unlock(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700868{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700869 if (c->try_harder == current) {
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600870 bch_time_stats_update(&c->try_harder_time, c->try_harder_start);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700871 c->try_harder = NULL;
Kent Overstreete8e1d462013-07-24 17:27:07 -0700872 wake_up(&c->try_wait);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700873 }
874}
875
Kent Overstreete8e1d462013-07-24 17:27:07 -0700876static struct btree *mca_alloc(struct cache_set *c, struct bkey *k, int level)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700877{
878 struct btree *b;
879
Kent Overstreete8e1d462013-07-24 17:27:07 -0700880 BUG_ON(current->bio_list);
881
Kent Overstreetcafe5632013-03-23 16:11:31 -0700882 lockdep_assert_held(&c->bucket_lock);
883
884 if (mca_find(c, k))
885 return NULL;
886
887 /* btree_free() doesn't free memory; it sticks the node on the end of
888 * the list. Check if there's any freed nodes there:
889 */
890 list_for_each_entry(b, &c->btree_cache_freeable, list)
Kent Overstreete8e1d462013-07-24 17:27:07 -0700891 if (!mca_reap(b, btree_order(k), false))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700892 goto out;
893
894 /* We never free struct btree itself, just the memory that holds the on
895 * disk node. Check the freed list before allocating a new one:
896 */
897 list_for_each_entry(b, &c->btree_cache_freed, list)
Kent Overstreete8e1d462013-07-24 17:27:07 -0700898 if (!mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700899 mca_data_alloc(b, k, __GFP_NOWARN|GFP_NOIO);
Kent Overstreeta85e9682013-12-20 17:28:16 -0800900 if (!b->keys.set[0].data)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700901 goto err;
902 else
903 goto out;
904 }
905
906 b = mca_bucket_alloc(c, k, __GFP_NOWARN|GFP_NOIO);
907 if (!b)
908 goto err;
909
910 BUG_ON(!down_write_trylock(&b->lock));
Kent Overstreeta85e9682013-12-20 17:28:16 -0800911 if (!b->keys.set->data)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700912 goto err;
913out:
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800914 BUG_ON(b->io_mutex.count != 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700915
916 bkey_copy(&b->key, k);
917 list_move(&b->list, &c->btree_cache);
918 hlist_del_init_rcu(&b->hash);
919 hlist_add_head_rcu(&b->hash, mca_hash(c, k));
920
921 lock_set_subclass(&b->lock.dep_map, level + 1, _THIS_IP_);
Kent Overstreetd6fd3b12013-07-24 17:20:19 -0700922 b->parent = (void *) ~0UL;
Kent Overstreeta85e9682013-12-20 17:28:16 -0800923 b->flags = 0;
924 b->written = 0;
925 b->level = level;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700926
Kent Overstreet65d45232013-12-20 17:22:05 -0800927 if (!b->level)
Kent Overstreeta85e9682013-12-20 17:28:16 -0800928 bch_btree_keys_init(&b->keys, &bch_extent_keys_ops,
929 &b->c->expensive_debug_checks);
Kent Overstreet65d45232013-12-20 17:22:05 -0800930 else
Kent Overstreeta85e9682013-12-20 17:28:16 -0800931 bch_btree_keys_init(&b->keys, &bch_btree_keys_ops,
932 &b->c->expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700933
934 return b;
935err:
936 if (b)
937 rw_unlock(true, b);
938
Kent Overstreete8e1d462013-07-24 17:27:07 -0700939 b = mca_cannibalize(c, k);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700940 if (!IS_ERR(b))
941 goto out;
942
943 return b;
944}
945
946/**
947 * bch_btree_node_get - find a btree node in the cache and lock it, reading it
948 * in from disk if necessary.
949 *
Kent Overstreetb54d6932013-07-24 18:04:18 -0700950 * If IO is necessary and running under generic_make_request, returns -EAGAIN.
Kent Overstreetcafe5632013-03-23 16:11:31 -0700951 *
952 * The btree node will have either a read or a write lock held, depending on
953 * level and op->lock.
954 */
955struct btree *bch_btree_node_get(struct cache_set *c, struct bkey *k,
Kent Overstreete8e1d462013-07-24 17:27:07 -0700956 int level, bool write)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700957{
958 int i = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700959 struct btree *b;
960
961 BUG_ON(level < 0);
962retry:
963 b = mca_find(c, k);
964
965 if (!b) {
Kent Overstreet57943512013-04-25 13:58:35 -0700966 if (current->bio_list)
967 return ERR_PTR(-EAGAIN);
968
Kent Overstreetcafe5632013-03-23 16:11:31 -0700969 mutex_lock(&c->bucket_lock);
Kent Overstreete8e1d462013-07-24 17:27:07 -0700970 b = mca_alloc(c, k, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700971 mutex_unlock(&c->bucket_lock);
972
973 if (!b)
974 goto retry;
975 if (IS_ERR(b))
976 return b;
977
Kent Overstreet57943512013-04-25 13:58:35 -0700978 bch_btree_node_read(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700979
980 if (!write)
981 downgrade_write(&b->lock);
982 } else {
983 rw_lock(write, b, level);
984 if (PTR_HASH(c, &b->key) != PTR_HASH(c, k)) {
985 rw_unlock(write, b);
986 goto retry;
987 }
988 BUG_ON(b->level != level);
989 }
990
991 b->accessed = 1;
992
Kent Overstreeta85e9682013-12-20 17:28:16 -0800993 for (; i <= b->keys.nsets && b->keys.set[i].size; i++) {
994 prefetch(b->keys.set[i].tree);
995 prefetch(b->keys.set[i].data);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700996 }
997
Kent Overstreeta85e9682013-12-20 17:28:16 -0800998 for (; i <= b->keys.nsets; i++)
999 prefetch(b->keys.set[i].data);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001000
Kent Overstreet57943512013-04-25 13:58:35 -07001001 if (btree_node_io_error(b)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001002 rw_unlock(write, b);
Kent Overstreet57943512013-04-25 13:58:35 -07001003 return ERR_PTR(-EIO);
1004 }
1005
1006 BUG_ON(!b->written);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001007
1008 return b;
1009}
1010
1011static void btree_node_prefetch(struct cache_set *c, struct bkey *k, int level)
1012{
1013 struct btree *b;
1014
1015 mutex_lock(&c->bucket_lock);
Kent Overstreete8e1d462013-07-24 17:27:07 -07001016 b = mca_alloc(c, k, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001017 mutex_unlock(&c->bucket_lock);
1018
1019 if (!IS_ERR_OR_NULL(b)) {
Kent Overstreet57943512013-04-25 13:58:35 -07001020 bch_btree_node_read(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001021 rw_unlock(true, b);
1022 }
1023}
1024
1025/* Btree alloc */
1026
Kent Overstreete8e1d462013-07-24 17:27:07 -07001027static void btree_node_free(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001028{
1029 unsigned i;
1030
Kent Overstreetc37511b2013-04-26 15:39:55 -07001031 trace_bcache_btree_node_free(b);
1032
Kent Overstreetcafe5632013-03-23 16:11:31 -07001033 BUG_ON(b == b->c->root);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001034
1035 if (btree_node_dirty(b))
1036 btree_complete_write(b, btree_current_write(b));
1037 clear_bit(BTREE_NODE_dirty, &b->flags);
1038
Kent Overstreetcafe5632013-03-23 16:11:31 -07001039 cancel_delayed_work(&b->work);
1040
1041 mutex_lock(&b->c->bucket_lock);
1042
1043 for (i = 0; i < KEY_PTRS(&b->key); i++) {
1044 BUG_ON(atomic_read(&PTR_BUCKET(b->c, &b->key, i)->pin));
1045
1046 bch_inc_gen(PTR_CACHE(b->c, &b->key, i),
1047 PTR_BUCKET(b->c, &b->key, i));
1048 }
1049
1050 bch_bucket_free(b->c, &b->key);
1051 mca_bucket_free(b);
1052 mutex_unlock(&b->c->bucket_lock);
1053}
1054
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001055struct btree *bch_btree_node_alloc(struct cache_set *c, int level, bool wait)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001056{
1057 BKEY_PADDED(key) k;
1058 struct btree *b = ERR_PTR(-EAGAIN);
1059
1060 mutex_lock(&c->bucket_lock);
1061retry:
Kent Overstreet78365412013-12-17 01:29:34 -08001062 if (__bch_bucket_alloc_set(c, RESERVE_BTREE, &k.key, 1, wait))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001063 goto err;
1064
Kent Overstreet3a3b6a42013-07-24 16:46:42 -07001065 bkey_put(c, &k.key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001066 SET_KEY_SIZE(&k.key, c->btree_pages * PAGE_SECTORS);
1067
Kent Overstreete8e1d462013-07-24 17:27:07 -07001068 b = mca_alloc(c, &k.key, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001069 if (IS_ERR(b))
1070 goto err_free;
1071
1072 if (!b) {
Kent Overstreetb1a67b02013-03-25 11:46:44 -07001073 cache_bug(c,
1074 "Tried to allocate bucket that was in btree cache");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001075 goto retry;
1076 }
1077
Kent Overstreetcafe5632013-03-23 16:11:31 -07001078 b->accessed = 1;
Kent Overstreeta85e9682013-12-20 17:28:16 -08001079 bch_bset_init_next(&b->keys, b->keys.set->data, bset_magic(&b->c->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001080
1081 mutex_unlock(&c->bucket_lock);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001082
1083 trace_bcache_btree_node_alloc(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001084 return b;
1085err_free:
1086 bch_bucket_free(c, &k.key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001087err:
1088 mutex_unlock(&c->bucket_lock);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001089
1090 trace_bcache_btree_node_alloc_fail(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001091 return b;
1092}
1093
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001094static struct btree *btree_node_alloc_replacement(struct btree *b, bool wait)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001095{
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001096 struct btree *n = bch_btree_node_alloc(b->c, b->level, wait);
Kent Overstreet67539e82013-09-10 22:53:34 -07001097 if (!IS_ERR_OR_NULL(n)) {
1098 bch_btree_sort_into(b, n, &b->c->sort);
1099 bkey_copy_key(&n->key, &b->key);
1100 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001101
1102 return n;
1103}
1104
Kent Overstreet8835c122013-07-24 23:18:05 -07001105static void make_btree_freeing_key(struct btree *b, struct bkey *k)
1106{
1107 unsigned i;
1108
1109 bkey_copy(k, &b->key);
1110 bkey_copy_key(k, &ZERO_KEY);
1111
1112 for (i = 0; i < KEY_PTRS(k); i++) {
1113 uint8_t g = PTR_BUCKET(b->c, k, i)->gen + 1;
1114
1115 SET_PTR_GEN(k, i, g);
1116 }
1117
1118 atomic_inc(&b->c->prio_blocked);
1119}
1120
Kent Overstreet78365412013-12-17 01:29:34 -08001121static int btree_check_reserve(struct btree *b, struct btree_op *op)
1122{
1123 struct cache_set *c = b->c;
1124 struct cache *ca;
1125 unsigned i, reserve = c->root->level * 2 + 1;
1126 int ret = 0;
1127
1128 mutex_lock(&c->bucket_lock);
1129
1130 for_each_cache(ca, c, i)
1131 if (fifo_used(&ca->free[RESERVE_BTREE]) < reserve) {
1132 if (op)
1133 prepare_to_wait(&c->bucket_wait, &op->wait,
1134 TASK_UNINTERRUPTIBLE);
1135 ret = -EINTR;
1136 break;
1137 }
1138
1139 mutex_unlock(&c->bucket_lock);
1140 return ret;
1141}
1142
Kent Overstreetcafe5632013-03-23 16:11:31 -07001143/* Garbage collection */
1144
1145uint8_t __bch_btree_mark_key(struct cache_set *c, int level, struct bkey *k)
1146{
1147 uint8_t stale = 0;
1148 unsigned i;
1149 struct bucket *g;
1150
1151 /*
1152 * ptr_invalid() can't return true for the keys that mark btree nodes as
1153 * freed, but since ptr_bad() returns true we'll never actually use them
1154 * for anything and thus we don't want mark their pointers here
1155 */
1156 if (!bkey_cmp(k, &ZERO_KEY))
1157 return stale;
1158
1159 for (i = 0; i < KEY_PTRS(k); i++) {
1160 if (!ptr_available(c, k, i))
1161 continue;
1162
1163 g = PTR_BUCKET(c, k, i);
1164
1165 if (gen_after(g->gc_gen, PTR_GEN(k, i)))
1166 g->gc_gen = PTR_GEN(k, i);
1167
1168 if (ptr_stale(c, k, i)) {
1169 stale = max(stale, ptr_stale(c, k, i));
1170 continue;
1171 }
1172
1173 cache_bug_on(GC_MARK(g) &&
1174 (GC_MARK(g) == GC_MARK_METADATA) != (level != 0),
1175 c, "inconsistent ptrs: mark = %llu, level = %i",
1176 GC_MARK(g), level);
1177
1178 if (level)
1179 SET_GC_MARK(g, GC_MARK_METADATA);
1180 else if (KEY_DIRTY(k))
1181 SET_GC_MARK(g, GC_MARK_DIRTY);
1182
1183 /* guard against overflow */
1184 SET_GC_SECTORS_USED(g, min_t(unsigned,
1185 GC_SECTORS_USED(g) + KEY_SIZE(k),
1186 (1 << 14) - 1));
1187
1188 BUG_ON(!GC_SECTORS_USED(g));
1189 }
1190
1191 return stale;
1192}
1193
1194#define btree_mark_key(b, k) __bch_btree_mark_key(b->c, b->level, k)
1195
Kent Overstreeta1f03582013-09-10 19:07:00 -07001196static bool btree_gc_mark_node(struct btree *b, struct gc_stat *gc)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001197{
1198 uint8_t stale = 0;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001199 unsigned keys = 0, good_keys = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001200 struct bkey *k;
1201 struct btree_iter iter;
1202 struct bset_tree *t;
1203
1204 gc->nodes++;
1205
1206 for_each_key_filter(b, k, &iter, bch_ptr_invalid) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001207 stale = max(stale, btree_mark_key(b, k));
Kent Overstreeta1f03582013-09-10 19:07:00 -07001208 keys++;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001209
Kent Overstreeta85e9682013-12-20 17:28:16 -08001210 if (bch_ptr_bad(&b->keys, k))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001211 continue;
1212
Kent Overstreetcafe5632013-03-23 16:11:31 -07001213 gc->key_bytes += bkey_u64s(k);
1214 gc->nkeys++;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001215 good_keys++;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001216
1217 gc->data += KEY_SIZE(k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001218 }
1219
Kent Overstreeta85e9682013-12-20 17:28:16 -08001220 for (t = b->keys.set; t <= &b->keys.set[b->keys.nsets]; t++)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001221 btree_bug_on(t->size &&
Kent Overstreeta85e9682013-12-20 17:28:16 -08001222 bset_written(&b->keys, t) &&
Kent Overstreetcafe5632013-03-23 16:11:31 -07001223 bkey_cmp(&b->key, &t->end) < 0,
1224 b, "found short btree key in gc");
1225
Kent Overstreeta1f03582013-09-10 19:07:00 -07001226 if (b->c->gc_always_rewrite)
1227 return true;
1228
1229 if (stale > 10)
1230 return true;
1231
1232 if ((keys - good_keys) * 2 > keys)
1233 return true;
1234
1235 return false;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001236}
1237
Kent Overstreeta1f03582013-09-10 19:07:00 -07001238#define GC_MERGE_NODES 4U
Kent Overstreetcafe5632013-03-23 16:11:31 -07001239
1240struct gc_merge_info {
1241 struct btree *b;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001242 unsigned keys;
1243};
1244
Kent Overstreeta1f03582013-09-10 19:07:00 -07001245static int bch_btree_insert_node(struct btree *, struct btree_op *,
1246 struct keylist *, atomic_t *, struct bkey *);
Kent Overstreetb54d6932013-07-24 18:04:18 -07001247
Kent Overstreeta1f03582013-09-10 19:07:00 -07001248static int btree_gc_coalesce(struct btree *b, struct btree_op *op,
1249 struct keylist *keylist, struct gc_stat *gc,
1250 struct gc_merge_info *r)
1251{
1252 unsigned i, nodes = 0, keys = 0, blocks;
1253 struct btree *new_nodes[GC_MERGE_NODES];
1254 struct closure cl;
1255 struct bkey *k;
1256
1257 memset(new_nodes, 0, sizeof(new_nodes));
Kent Overstreetb54d6932013-07-24 18:04:18 -07001258 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001259
Kent Overstreeta1f03582013-09-10 19:07:00 -07001260 while (nodes < GC_MERGE_NODES && !IS_ERR_OR_NULL(r[nodes].b))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001261 keys += r[nodes++].keys;
1262
1263 blocks = btree_default_blocks(b->c) * 2 / 3;
1264
1265 if (nodes < 2 ||
Kent Overstreeta85e9682013-12-20 17:28:16 -08001266 __set_blocks(b->keys.set[0].data, keys,
Kent Overstreetee811282013-12-17 23:49:49 -08001267 block_bytes(b->c)) > blocks * (nodes - 1))
Kent Overstreeta1f03582013-09-10 19:07:00 -07001268 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001269
Kent Overstreeta1f03582013-09-10 19:07:00 -07001270 for (i = 0; i < nodes; i++) {
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001271 new_nodes[i] = btree_node_alloc_replacement(r[i].b, false);
Kent Overstreeta1f03582013-09-10 19:07:00 -07001272 if (IS_ERR_OR_NULL(new_nodes[i]))
1273 goto out_nocoalesce;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001274 }
1275
1276 for (i = nodes - 1; i > 0; --i) {
Kent Overstreetee811282013-12-17 23:49:49 -08001277 struct bset *n1 = btree_bset_first(new_nodes[i]);
1278 struct bset *n2 = btree_bset_first(new_nodes[i - 1]);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001279 struct bkey *k, *last = NULL;
1280
1281 keys = 0;
1282
Kent Overstreeta1f03582013-09-10 19:07:00 -07001283 if (i > 1) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001284 for (k = n2->start;
Kent Overstreetfafff812013-12-17 21:56:21 -08001285 k < bset_bkey_last(n2);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001286 k = bkey_next(k)) {
1287 if (__set_blocks(n1, n1->keys + keys +
Kent Overstreetee811282013-12-17 23:49:49 -08001288 bkey_u64s(k),
1289 block_bytes(b->c)) > blocks)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001290 break;
1291
1292 last = k;
1293 keys += bkey_u64s(k);
1294 }
Kent Overstreeta1f03582013-09-10 19:07:00 -07001295 } else {
1296 /*
1297 * Last node we're not getting rid of - we're getting
1298 * rid of the node at r[0]. Have to try and fit all of
1299 * the remaining keys into this node; we can't ensure
1300 * they will always fit due to rounding and variable
1301 * length keys (shouldn't be possible in practice,
1302 * though)
1303 */
1304 if (__set_blocks(n1, n1->keys + n2->keys,
Kent Overstreetee811282013-12-17 23:49:49 -08001305 block_bytes(b->c)) >
1306 btree_blocks(new_nodes[i]))
Kent Overstreeta1f03582013-09-10 19:07:00 -07001307 goto out_nocoalesce;
1308
1309 keys = n2->keys;
1310 /* Take the key of the node we're getting rid of */
1311 last = &r->b->key;
1312 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001313
Kent Overstreetee811282013-12-17 23:49:49 -08001314 BUG_ON(__set_blocks(n1, n1->keys + keys, block_bytes(b->c)) >
1315 btree_blocks(new_nodes[i]));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001316
Kent Overstreeta1f03582013-09-10 19:07:00 -07001317 if (last)
1318 bkey_copy_key(&new_nodes[i]->key, last);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001319
Kent Overstreetfafff812013-12-17 21:56:21 -08001320 memcpy(bset_bkey_last(n1),
Kent Overstreetcafe5632013-03-23 16:11:31 -07001321 n2->start,
Kent Overstreetfafff812013-12-17 21:56:21 -08001322 (void *) bset_bkey_idx(n2, keys) - (void *) n2->start);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001323
1324 n1->keys += keys;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001325 r[i].keys = n1->keys;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001326
1327 memmove(n2->start,
Kent Overstreetfafff812013-12-17 21:56:21 -08001328 bset_bkey_idx(n2, keys),
1329 (void *) bset_bkey_last(n2) -
1330 (void *) bset_bkey_idx(n2, keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001331
1332 n2->keys -= keys;
1333
Kent Overstreet085d2a32013-11-11 18:20:51 -08001334 if (__bch_keylist_realloc(keylist,
1335 bkey_u64s(&new_nodes[i]->key)))
Kent Overstreeta1f03582013-09-10 19:07:00 -07001336 goto out_nocoalesce;
1337
1338 bch_btree_node_write(new_nodes[i], &cl);
1339 bch_keylist_add(keylist, &new_nodes[i]->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001340 }
1341
Kent Overstreeta1f03582013-09-10 19:07:00 -07001342 for (i = 0; i < nodes; i++) {
Kent Overstreet085d2a32013-11-11 18:20:51 -08001343 if (__bch_keylist_realloc(keylist, bkey_u64s(&r[i].b->key)))
Kent Overstreeta1f03582013-09-10 19:07:00 -07001344 goto out_nocoalesce;
1345
1346 make_btree_freeing_key(r[i].b, keylist->top);
1347 bch_keylist_push(keylist);
1348 }
1349
1350 /* We emptied out this node */
Kent Overstreetee811282013-12-17 23:49:49 -08001351 BUG_ON(btree_bset_first(new_nodes[0])->keys);
Kent Overstreeta1f03582013-09-10 19:07:00 -07001352 btree_node_free(new_nodes[0]);
1353 rw_unlock(true, new_nodes[0]);
1354
1355 closure_sync(&cl);
1356
1357 for (i = 0; i < nodes; i++) {
1358 btree_node_free(r[i].b);
1359 rw_unlock(true, r[i].b);
1360
1361 r[i].b = new_nodes[i];
1362 }
1363
1364 bch_btree_insert_node(b, op, keylist, NULL, NULL);
1365 BUG_ON(!bch_keylist_empty(keylist));
1366
1367 memmove(r, r + 1, sizeof(r[0]) * (nodes - 1));
1368 r[nodes - 1].b = ERR_PTR(-EINTR);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001369
Kent Overstreetc37511b2013-04-26 15:39:55 -07001370 trace_bcache_btree_gc_coalesce(nodes);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001371 gc->nodes--;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001372
Kent Overstreeta1f03582013-09-10 19:07:00 -07001373 /* Invalidated our iterator */
1374 return -EINTR;
1375
1376out_nocoalesce:
1377 closure_sync(&cl);
1378
1379 while ((k = bch_keylist_pop(keylist)))
1380 if (!bkey_cmp(k, &ZERO_KEY))
1381 atomic_dec(&b->c->prio_blocked);
1382
1383 for (i = 0; i < nodes; i++)
1384 if (!IS_ERR_OR_NULL(new_nodes[i])) {
1385 btree_node_free(new_nodes[i]);
1386 rw_unlock(true, new_nodes[i]);
1387 }
1388 return 0;
1389}
1390
1391static unsigned btree_gc_count_keys(struct btree *b)
1392{
1393 struct bkey *k;
1394 struct btree_iter iter;
1395 unsigned ret = 0;
1396
1397 for_each_key_filter(b, k, &iter, bch_ptr_bad)
1398 ret += bkey_u64s(k);
1399
1400 return ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001401}
1402
1403static int btree_gc_recurse(struct btree *b, struct btree_op *op,
1404 struct closure *writes, struct gc_stat *gc)
1405{
Kent Overstreetcafe5632013-03-23 16:11:31 -07001406 unsigned i;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001407 int ret = 0;
1408 bool should_rewrite;
1409 struct btree *n;
1410 struct bkey *k;
1411 struct keylist keys;
1412 struct btree_iter iter;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001413 struct gc_merge_info r[GC_MERGE_NODES];
Kent Overstreeta1f03582013-09-10 19:07:00 -07001414 struct gc_merge_info *last = r + GC_MERGE_NODES - 1;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001415
Kent Overstreeta1f03582013-09-10 19:07:00 -07001416 bch_keylist_init(&keys);
1417 bch_btree_iter_init(b, &iter, &b->c->gc_done);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001418
Kent Overstreeta1f03582013-09-10 19:07:00 -07001419 for (i = 0; i < GC_MERGE_NODES; i++)
1420 r[i].b = ERR_PTR(-EINTR);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001421
Kent Overstreeta1f03582013-09-10 19:07:00 -07001422 while (1) {
Kent Overstreeta85e9682013-12-20 17:28:16 -08001423 k = bch_btree_iter_next_filter(&iter, &b->keys, bch_ptr_bad);
Kent Overstreeta1f03582013-09-10 19:07:00 -07001424 if (k) {
1425 r->b = bch_btree_node_get(b->c, k, b->level - 1, true);
1426 if (IS_ERR(r->b)) {
1427 ret = PTR_ERR(r->b);
1428 break;
1429 }
1430
1431 r->keys = btree_gc_count_keys(r->b);
1432
1433 ret = btree_gc_coalesce(b, op, &keys, gc, r);
1434 if (ret)
1435 break;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001436 }
1437
Kent Overstreeta1f03582013-09-10 19:07:00 -07001438 if (!last->b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001439 break;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001440
1441 if (!IS_ERR(last->b)) {
1442 should_rewrite = btree_gc_mark_node(last->b, gc);
Kent Overstreet78365412013-12-17 01:29:34 -08001443 if (should_rewrite &&
1444 !btree_check_reserve(b, NULL)) {
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001445 n = btree_node_alloc_replacement(last->b,
1446 false);
Kent Overstreeta1f03582013-09-10 19:07:00 -07001447
1448 if (!IS_ERR_OR_NULL(n)) {
1449 bch_btree_node_write_sync(n);
1450 bch_keylist_add(&keys, &n->key);
1451
1452 make_btree_freeing_key(last->b,
1453 keys.top);
1454 bch_keylist_push(&keys);
1455
1456 btree_node_free(last->b);
1457
1458 bch_btree_insert_node(b, op, &keys,
1459 NULL, NULL);
1460 BUG_ON(!bch_keylist_empty(&keys));
1461
1462 rw_unlock(true, last->b);
1463 last->b = n;
1464
1465 /* Invalidated our iterator */
1466 ret = -EINTR;
1467 break;
1468 }
1469 }
1470
1471 if (last->b->level) {
1472 ret = btree_gc_recurse(last->b, op, writes, gc);
1473 if (ret)
1474 break;
1475 }
1476
1477 bkey_copy_key(&b->c->gc_done, &last->b->key);
1478
1479 /*
1480 * Must flush leaf nodes before gc ends, since replace
1481 * operations aren't journalled
1482 */
1483 if (btree_node_dirty(last->b))
1484 bch_btree_node_write(last->b, writes);
1485 rw_unlock(true, last->b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001486 }
1487
Kent Overstreeta1f03582013-09-10 19:07:00 -07001488 memmove(r + 1, r, sizeof(r[0]) * (GC_MERGE_NODES - 1));
1489 r->b = NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001490
Kent Overstreetcafe5632013-03-23 16:11:31 -07001491 if (need_resched()) {
1492 ret = -EAGAIN;
1493 break;
1494 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001495 }
1496
Kent Overstreeta1f03582013-09-10 19:07:00 -07001497 for (i = 0; i < GC_MERGE_NODES; i++)
1498 if (!IS_ERR_OR_NULL(r[i].b)) {
1499 if (btree_node_dirty(r[i].b))
1500 bch_btree_node_write(r[i].b, writes);
1501 rw_unlock(true, r[i].b);
1502 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001503
Kent Overstreeta1f03582013-09-10 19:07:00 -07001504 bch_keylist_free(&keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001505
1506 return ret;
1507}
1508
1509static int bch_btree_gc_root(struct btree *b, struct btree_op *op,
1510 struct closure *writes, struct gc_stat *gc)
1511{
1512 struct btree *n = NULL;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001513 int ret = 0;
1514 bool should_rewrite;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001515
Kent Overstreeta1f03582013-09-10 19:07:00 -07001516 should_rewrite = btree_gc_mark_node(b, gc);
1517 if (should_rewrite) {
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001518 n = btree_node_alloc_replacement(b, false);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001519
Kent Overstreeta1f03582013-09-10 19:07:00 -07001520 if (!IS_ERR_OR_NULL(n)) {
1521 bch_btree_node_write_sync(n);
1522 bch_btree_set_root(n);
1523 btree_node_free(b);
1524 rw_unlock(true, n);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001525
Kent Overstreeta1f03582013-09-10 19:07:00 -07001526 return -EINTR;
1527 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001528 }
1529
Kent Overstreeta1f03582013-09-10 19:07:00 -07001530 if (b->level) {
1531 ret = btree_gc_recurse(b, op, writes, gc);
1532 if (ret)
1533 return ret;
1534 }
1535
1536 bkey_copy_key(&b->c->gc_done, &b->key);
1537
Kent Overstreetcafe5632013-03-23 16:11:31 -07001538 return ret;
1539}
1540
1541static void btree_gc_start(struct cache_set *c)
1542{
1543 struct cache *ca;
1544 struct bucket *b;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001545 unsigned i;
1546
1547 if (!c->gc_mark_valid)
1548 return;
1549
1550 mutex_lock(&c->bucket_lock);
1551
1552 c->gc_mark_valid = 0;
1553 c->gc_done = ZERO_KEY;
1554
1555 for_each_cache(ca, c, i)
1556 for_each_bucket(b, ca) {
1557 b->gc_gen = b->gen;
Kent Overstreet29ebf462013-07-11 19:43:21 -07001558 if (!atomic_read(&b->pin)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001559 SET_GC_MARK(b, GC_MARK_RECLAIMABLE);
Kent Overstreet29ebf462013-07-11 19:43:21 -07001560 SET_GC_SECTORS_USED(b, 0);
1561 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001562 }
1563
Kent Overstreetcafe5632013-03-23 16:11:31 -07001564 mutex_unlock(&c->bucket_lock);
1565}
1566
1567size_t bch_btree_gc_finish(struct cache_set *c)
1568{
1569 size_t available = 0;
1570 struct bucket *b;
1571 struct cache *ca;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001572 unsigned i;
1573
1574 mutex_lock(&c->bucket_lock);
1575
1576 set_gc_sectors(c);
1577 c->gc_mark_valid = 1;
1578 c->need_gc = 0;
1579
1580 if (c->root)
1581 for (i = 0; i < KEY_PTRS(&c->root->key); i++)
1582 SET_GC_MARK(PTR_BUCKET(c, &c->root->key, i),
1583 GC_MARK_METADATA);
1584
1585 for (i = 0; i < KEY_PTRS(&c->uuid_bucket); i++)
1586 SET_GC_MARK(PTR_BUCKET(c, &c->uuid_bucket, i),
1587 GC_MARK_METADATA);
1588
Nicholas Swensonbf0a6282013-11-26 19:14:23 -08001589 /* don't reclaim buckets to which writeback keys point */
1590 rcu_read_lock();
1591 for (i = 0; i < c->nr_uuids; i++) {
1592 struct bcache_device *d = c->devices[i];
1593 struct cached_dev *dc;
1594 struct keybuf_key *w, *n;
1595 unsigned j;
1596
1597 if (!d || UUID_FLASH_ONLY(&c->uuids[i]))
1598 continue;
1599 dc = container_of(d, struct cached_dev, disk);
1600
1601 spin_lock(&dc->writeback_keys.lock);
1602 rbtree_postorder_for_each_entry_safe(w, n,
1603 &dc->writeback_keys.keys, node)
1604 for (j = 0; j < KEY_PTRS(&w->key); j++)
1605 SET_GC_MARK(PTR_BUCKET(c, &w->key, j),
1606 GC_MARK_DIRTY);
1607 spin_unlock(&dc->writeback_keys.lock);
1608 }
1609 rcu_read_unlock();
1610
Kent Overstreetcafe5632013-03-23 16:11:31 -07001611 for_each_cache(ca, c, i) {
1612 uint64_t *i;
1613
1614 ca->invalidate_needs_gc = 0;
1615
1616 for (i = ca->sb.d; i < ca->sb.d + ca->sb.keys; i++)
1617 SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA);
1618
1619 for (i = ca->prio_buckets;
1620 i < ca->prio_buckets + prio_buckets(ca) * 2; i++)
1621 SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA);
1622
1623 for_each_bucket(b, ca) {
1624 b->last_gc = b->gc_gen;
1625 c->need_gc = max(c->need_gc, bucket_gc_gen(b));
1626
1627 if (!atomic_read(&b->pin) &&
1628 GC_MARK(b) == GC_MARK_RECLAIMABLE) {
1629 available++;
1630 if (!GC_SECTORS_USED(b))
1631 bch_bucket_add_unused(ca, b);
1632 }
1633 }
1634 }
1635
Kent Overstreetcafe5632013-03-23 16:11:31 -07001636 mutex_unlock(&c->bucket_lock);
1637 return available;
1638}
1639
Kent Overstreet72a44512013-10-24 17:19:26 -07001640static void bch_btree_gc(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001641{
Kent Overstreetcafe5632013-03-23 16:11:31 -07001642 int ret;
1643 unsigned long available;
1644 struct gc_stat stats;
1645 struct closure writes;
1646 struct btree_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001647 uint64_t start_time = local_clock();
Kent Overstreet57943512013-04-25 13:58:35 -07001648
Kent Overstreetc37511b2013-04-26 15:39:55 -07001649 trace_bcache_gc_start(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001650
1651 memset(&stats, 0, sizeof(struct gc_stat));
1652 closure_init_stack(&writes);
Kent Overstreetb54d6932013-07-24 18:04:18 -07001653 bch_btree_op_init(&op, SHRT_MAX);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001654
1655 btree_gc_start(c);
1656
Kent Overstreeta1f03582013-09-10 19:07:00 -07001657 do {
1658 ret = btree_root(gc_root, c, &op, &writes, &stats);
1659 closure_sync(&writes);
Kent Overstreet57943512013-04-25 13:58:35 -07001660
Kent Overstreeta1f03582013-09-10 19:07:00 -07001661 if (ret && ret != -EAGAIN)
1662 pr_warn("gc failed!");
1663 } while (ret);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001664
1665 available = bch_btree_gc_finish(c);
Kent Overstreet57943512013-04-25 13:58:35 -07001666 wake_up_allocators(c);
1667
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001668 bch_time_stats_update(&c->btree_gc_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001669
1670 stats.key_bytes *= sizeof(uint64_t);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001671 stats.data <<= 9;
1672 stats.in_use = (c->nbuckets - available) * 100 / c->nbuckets;
1673 memcpy(&c->gc_stats, &stats, sizeof(struct gc_stat));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001674
Kent Overstreetc37511b2013-04-26 15:39:55 -07001675 trace_bcache_gc_end(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001676
Kent Overstreet72a44512013-10-24 17:19:26 -07001677 bch_moving_gc(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001678}
1679
Kent Overstreet72a44512013-10-24 17:19:26 -07001680static int bch_gc_thread(void *arg)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001681{
Kent Overstreet72a44512013-10-24 17:19:26 -07001682 struct cache_set *c = arg;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001683 struct cache *ca;
1684 unsigned i;
Kent Overstreet72a44512013-10-24 17:19:26 -07001685
1686 while (1) {
Kent Overstreeta1f03582013-09-10 19:07:00 -07001687again:
Kent Overstreet72a44512013-10-24 17:19:26 -07001688 bch_btree_gc(c);
1689
1690 set_current_state(TASK_INTERRUPTIBLE);
1691 if (kthread_should_stop())
1692 break;
1693
Kent Overstreeta1f03582013-09-10 19:07:00 -07001694 mutex_lock(&c->bucket_lock);
1695
1696 for_each_cache(ca, c, i)
1697 if (ca->invalidate_needs_gc) {
1698 mutex_unlock(&c->bucket_lock);
1699 set_current_state(TASK_RUNNING);
1700 goto again;
1701 }
1702
1703 mutex_unlock(&c->bucket_lock);
1704
Kent Overstreet72a44512013-10-24 17:19:26 -07001705 try_to_freeze();
1706 schedule();
1707 }
1708
1709 return 0;
1710}
1711
1712int bch_gc_thread_start(struct cache_set *c)
1713{
1714 c->gc_thread = kthread_create(bch_gc_thread, c, "bcache_gc");
1715 if (IS_ERR(c->gc_thread))
1716 return PTR_ERR(c->gc_thread);
1717
1718 set_task_state(c->gc_thread, TASK_INTERRUPTIBLE);
1719 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001720}
1721
1722/* Initial partial gc */
1723
1724static int bch_btree_check_recurse(struct btree *b, struct btree_op *op,
1725 unsigned long **seen)
1726{
Kent Overstreet50310162013-09-10 17:18:59 -07001727 int ret = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001728 unsigned i;
Kent Overstreet50310162013-09-10 17:18:59 -07001729 struct bkey *k, *p = NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001730 struct bucket *g;
1731 struct btree_iter iter;
1732
1733 for_each_key_filter(b, k, &iter, bch_ptr_invalid) {
1734 for (i = 0; i < KEY_PTRS(k); i++) {
1735 if (!ptr_available(b->c, k, i))
1736 continue;
1737
1738 g = PTR_BUCKET(b->c, k, i);
1739
1740 if (!__test_and_set_bit(PTR_BUCKET_NR(b->c, k, i),
1741 seen[PTR_DEV(k, i)]) ||
1742 !ptr_stale(b->c, k, i)) {
1743 g->gen = PTR_GEN(k, i);
1744
1745 if (b->level)
1746 g->prio = BTREE_PRIO;
1747 else if (g->prio == BTREE_PRIO)
1748 g->prio = INITIAL_PRIO;
1749 }
1750 }
1751
1752 btree_mark_key(b, k);
1753 }
1754
1755 if (b->level) {
Kent Overstreet50310162013-09-10 17:18:59 -07001756 bch_btree_iter_init(b, &iter, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001757
Kent Overstreet50310162013-09-10 17:18:59 -07001758 do {
Kent Overstreeta85e9682013-12-20 17:28:16 -08001759 k = bch_btree_iter_next_filter(&iter, &b->keys,
1760 bch_ptr_bad);
Kent Overstreet50310162013-09-10 17:18:59 -07001761 if (k)
1762 btree_node_prefetch(b->c, k, b->level - 1);
1763
Kent Overstreetcafe5632013-03-23 16:11:31 -07001764 if (p)
Kent Overstreet50310162013-09-10 17:18:59 -07001765 ret = btree(check_recurse, p, b, op, seen);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001766
Kent Overstreet50310162013-09-10 17:18:59 -07001767 p = k;
1768 } while (p && !ret);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001769 }
1770
1771 return 0;
1772}
1773
Kent Overstreetc18536a2013-07-24 17:44:17 -07001774int bch_btree_check(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001775{
1776 int ret = -ENOMEM;
1777 unsigned i;
1778 unsigned long *seen[MAX_CACHES_PER_SET];
Kent Overstreetc18536a2013-07-24 17:44:17 -07001779 struct btree_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001780
1781 memset(seen, 0, sizeof(seen));
Kent Overstreetb54d6932013-07-24 18:04:18 -07001782 bch_btree_op_init(&op, SHRT_MAX);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001783
1784 for (i = 0; c->cache[i]; i++) {
1785 size_t n = DIV_ROUND_UP(c->cache[i]->sb.nbuckets, 8);
1786 seen[i] = kmalloc(n, GFP_KERNEL);
1787 if (!seen[i])
1788 goto err;
1789
1790 /* Disables the seen array until prio_read() uses it too */
1791 memset(seen[i], 0xFF, n);
1792 }
1793
Kent Overstreetc18536a2013-07-24 17:44:17 -07001794 ret = btree_root(check_recurse, c, &op, seen);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001795err:
1796 for (i = 0; i < MAX_CACHES_PER_SET; i++)
1797 kfree(seen[i]);
1798 return ret;
1799}
1800
1801/* Btree insertion */
1802
Kent Overstreet1b207d82013-09-10 18:52:54 -07001803static bool fix_overlapping_extents(struct btree *b, struct bkey *insert,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001804 struct btree_iter *iter,
Kent Overstreet1b207d82013-09-10 18:52:54 -07001805 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001806{
Kent Overstreet279afba2013-06-05 06:21:07 -07001807 void subtract_dirty(struct bkey *k, uint64_t offset, int sectors)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001808 {
Kent Overstreet279afba2013-06-05 06:21:07 -07001809 if (KEY_DIRTY(k))
1810 bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k),
1811 offset, -sectors);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001812 }
1813
Kent Overstreet279afba2013-06-05 06:21:07 -07001814 uint64_t old_offset;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001815 unsigned old_size, sectors_found = 0;
1816
1817 while (1) {
1818 struct bkey *k = bch_btree_iter_next(iter);
Kent Overstreet911c9612013-07-28 18:35:09 -07001819 if (!k)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001820 break;
1821
Kent Overstreet911c9612013-07-28 18:35:09 -07001822 if (bkey_cmp(&START_KEY(k), insert) >= 0) {
1823 if (KEY_SIZE(k))
1824 break;
1825 else
1826 continue;
1827 }
1828
Kent Overstreetcafe5632013-03-23 16:11:31 -07001829 if (bkey_cmp(k, &START_KEY(insert)) <= 0)
1830 continue;
1831
Kent Overstreet279afba2013-06-05 06:21:07 -07001832 old_offset = KEY_START(k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001833 old_size = KEY_SIZE(k);
1834
1835 /*
1836 * We might overlap with 0 size extents; we can't skip these
1837 * because if they're in the set we're inserting to we have to
1838 * adjust them so they don't overlap with the key we're
Kent Overstreet1b207d82013-09-10 18:52:54 -07001839 * inserting. But we don't want to check them for replace
Kent Overstreetcafe5632013-03-23 16:11:31 -07001840 * operations.
1841 */
1842
Kent Overstreet1b207d82013-09-10 18:52:54 -07001843 if (replace_key && KEY_SIZE(k)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001844 /*
1845 * k might have been split since we inserted/found the
1846 * key we're replacing
1847 */
1848 unsigned i;
1849 uint64_t offset = KEY_START(k) -
Kent Overstreet1b207d82013-09-10 18:52:54 -07001850 KEY_START(replace_key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001851
1852 /* But it must be a subset of the replace key */
Kent Overstreet1b207d82013-09-10 18:52:54 -07001853 if (KEY_START(k) < KEY_START(replace_key) ||
1854 KEY_OFFSET(k) > KEY_OFFSET(replace_key))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001855 goto check_failed;
1856
1857 /* We didn't find a key that we were supposed to */
1858 if (KEY_START(k) > KEY_START(insert) + sectors_found)
1859 goto check_failed;
1860
Kent Overstreetd24a6e12013-11-10 21:55:27 -08001861 if (KEY_PTRS(k) != KEY_PTRS(replace_key) ||
1862 KEY_DIRTY(k) != KEY_DIRTY(replace_key))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001863 goto check_failed;
1864
1865 /* skip past gen */
1866 offset <<= 8;
1867
Kent Overstreet1b207d82013-09-10 18:52:54 -07001868 BUG_ON(!KEY_PTRS(replace_key));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001869
Kent Overstreet1b207d82013-09-10 18:52:54 -07001870 for (i = 0; i < KEY_PTRS(replace_key); i++)
1871 if (k->ptr[i] != replace_key->ptr[i] + offset)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001872 goto check_failed;
1873
1874 sectors_found = KEY_OFFSET(k) - KEY_START(insert);
1875 }
1876
1877 if (bkey_cmp(insert, k) < 0 &&
1878 bkey_cmp(&START_KEY(insert), &START_KEY(k)) > 0) {
1879 /*
1880 * We overlapped in the middle of an existing key: that
1881 * means we have to split the old key. But we have to do
1882 * slightly different things depending on whether the
1883 * old key has been written out yet.
1884 */
1885
1886 struct bkey *top;
1887
Kent Overstreet279afba2013-06-05 06:21:07 -07001888 subtract_dirty(k, KEY_START(insert), KEY_SIZE(insert));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001889
Kent Overstreeta85e9682013-12-20 17:28:16 -08001890 if (bkey_written(&b->keys, k)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001891 /*
1892 * We insert a new key to cover the top of the
1893 * old key, and the old key is modified in place
1894 * to represent the bottom split.
1895 *
1896 * It's completely arbitrary whether the new key
1897 * is the top or the bottom, but it has to match
1898 * up with what btree_sort_fixup() does - it
1899 * doesn't check for this kind of overlap, it
1900 * depends on us inserting a new key for the top
1901 * here.
1902 */
Kent Overstreeta85e9682013-12-20 17:28:16 -08001903 top = bch_bset_search(b,
1904 bset_tree_last(&b->keys),
Kent Overstreetcafe5632013-03-23 16:11:31 -07001905 insert);
Kent Overstreeta85e9682013-12-20 17:28:16 -08001906 bch_bset_insert(&b->keys, top, k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001907 } else {
1908 BKEY_PADDED(key) temp;
1909 bkey_copy(&temp.key, k);
Kent Overstreeta85e9682013-12-20 17:28:16 -08001910 bch_bset_insert(&b->keys, k, &temp.key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001911 top = bkey_next(k);
1912 }
1913
1914 bch_cut_front(insert, top);
1915 bch_cut_back(&START_KEY(insert), k);
Kent Overstreeta85e9682013-12-20 17:28:16 -08001916 bch_bset_fix_invalidated_key(&b->keys, k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001917 return false;
1918 }
1919
1920 if (bkey_cmp(insert, k) < 0) {
1921 bch_cut_front(insert, k);
1922 } else {
Kent Overstreet1fa84552013-11-10 21:55:27 -08001923 if (bkey_cmp(&START_KEY(insert), &START_KEY(k)) > 0)
1924 old_offset = KEY_START(insert);
1925
Kent Overstreeta85e9682013-12-20 17:28:16 -08001926 if (bkey_written(&b->keys, k) &&
Kent Overstreetcafe5632013-03-23 16:11:31 -07001927 bkey_cmp(&START_KEY(insert), &START_KEY(k)) <= 0) {
1928 /*
1929 * Completely overwrote, so we don't have to
1930 * invalidate the binary search tree
1931 */
1932 bch_cut_front(k, k);
1933 } else {
1934 __bch_cut_back(&START_KEY(insert), k);
Kent Overstreeta85e9682013-12-20 17:28:16 -08001935 bch_bset_fix_invalidated_key(&b->keys, k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001936 }
1937 }
1938
Kent Overstreet279afba2013-06-05 06:21:07 -07001939 subtract_dirty(k, old_offset, old_size - KEY_SIZE(k));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001940 }
1941
1942check_failed:
Kent Overstreet1b207d82013-09-10 18:52:54 -07001943 if (replace_key) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001944 if (!sectors_found) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001945 return true;
1946 } else if (sectors_found < KEY_SIZE(insert)) {
1947 SET_KEY_OFFSET(insert, KEY_OFFSET(insert) -
1948 (KEY_SIZE(insert) - sectors_found));
1949 SET_KEY_SIZE(insert, sectors_found);
1950 }
1951 }
1952
1953 return false;
1954}
1955
1956static bool btree_insert_key(struct btree *b, struct btree_op *op,
Kent Overstreet1b207d82013-09-10 18:52:54 -07001957 struct bkey *k, struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001958{
Kent Overstreetee811282013-12-17 23:49:49 -08001959 struct bset *i = btree_bset_last(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001960 struct bkey *m, *prev;
Kent Overstreet85b14922013-05-14 20:33:16 -07001961 unsigned status = BTREE_INSERT_STATUS_INSERT;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001962
1963 BUG_ON(bkey_cmp(k, &b->key) > 0);
1964 BUG_ON(b->level && !KEY_PTRS(k));
1965 BUG_ON(!b->level && !KEY_OFFSET(k));
1966
1967 if (!b->level) {
1968 struct btree_iter iter;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001969
1970 /*
1971 * bset_search() returns the first key that is strictly greater
1972 * than the search key - but for back merging, we want to find
Kent Overstreet0eacac22013-07-01 19:29:05 -07001973 * the previous key.
Kent Overstreetcafe5632013-03-23 16:11:31 -07001974 */
Kent Overstreetcafe5632013-03-23 16:11:31 -07001975 prev = NULL;
Kent Overstreeta85e9682013-12-20 17:28:16 -08001976 m = bch_btree_iter_init(b, &iter,
1977 PRECEDING_KEY(&START_KEY(k)));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001978
Kent Overstreet1b207d82013-09-10 18:52:54 -07001979 if (fix_overlapping_extents(b, k, &iter, replace_key)) {
1980 op->insert_collision = true;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001981 return false;
Kent Overstreet1b207d82013-09-10 18:52:54 -07001982 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001983
Kent Overstreet1fa84552013-11-10 21:55:27 -08001984 if (KEY_DIRTY(k))
1985 bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k),
1986 KEY_START(k), KEY_SIZE(k));
1987
Kent Overstreetfafff812013-12-17 21:56:21 -08001988 while (m != bset_bkey_last(i) &&
Kent Overstreetcafe5632013-03-23 16:11:31 -07001989 bkey_cmp(k, &START_KEY(m)) > 0)
1990 prev = m, m = bkey_next(m);
1991
1992 if (key_merging_disabled(b->c))
1993 goto insert;
1994
1995 /* prev is in the tree, if we merge we're done */
Kent Overstreet85b14922013-05-14 20:33:16 -07001996 status = BTREE_INSERT_STATUS_BACK_MERGE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001997 if (prev &&
Kent Overstreeta85e9682013-12-20 17:28:16 -08001998 bch_bkey_try_merge(&b->keys, prev, k))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001999 goto merged;
2000
Kent Overstreet85b14922013-05-14 20:33:16 -07002001 status = BTREE_INSERT_STATUS_OVERWROTE;
Kent Overstreetfafff812013-12-17 21:56:21 -08002002 if (m != bset_bkey_last(i) &&
Kent Overstreetcafe5632013-03-23 16:11:31 -07002003 KEY_PTRS(m) == KEY_PTRS(k) && !KEY_SIZE(m))
2004 goto copy;
2005
Kent Overstreet85b14922013-05-14 20:33:16 -07002006 status = BTREE_INSERT_STATUS_FRONT_MERGE;
Kent Overstreetfafff812013-12-17 21:56:21 -08002007 if (m != bset_bkey_last(i) &&
Kent Overstreeta85e9682013-12-20 17:28:16 -08002008 bch_bkey_try_merge(&b->keys, k, m))
Kent Overstreetcafe5632013-03-23 16:11:31 -07002009 goto copy;
Kent Overstreet1b207d82013-09-10 18:52:54 -07002010 } else {
2011 BUG_ON(replace_key);
Kent Overstreeta85e9682013-12-20 17:28:16 -08002012 m = bch_bset_search(b, bset_tree_last(&b->keys), k);
Kent Overstreet1b207d82013-09-10 18:52:54 -07002013 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07002014
Kent Overstreeta85e9682013-12-20 17:28:16 -08002015insert: bch_bset_insert(&b->keys, m, k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002016copy: bkey_copy(m, k);
2017merged:
Kent Overstreet1b207d82013-09-10 18:52:54 -07002018 bch_check_keys(b, "%u for %s", status,
2019 replace_key ? "replace" : "insert");
Kent Overstreetcafe5632013-03-23 16:11:31 -07002020
2021 if (b->level && !KEY_OFFSET(k))
Kent Overstreet57943512013-04-25 13:58:35 -07002022 btree_current_write(b)->prio_blocked++;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002023
Kent Overstreet1b207d82013-09-10 18:52:54 -07002024 trace_bcache_btree_insert_key(b, k, replace_key != NULL, status);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002025
2026 return true;
2027}
2028
Kent Overstreet26c949f2013-09-10 18:41:15 -07002029static bool bch_btree_insert_keys(struct btree *b, struct btree_op *op,
Kent Overstreet1b207d82013-09-10 18:52:54 -07002030 struct keylist *insert_keys,
2031 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002032{
2033 bool ret = false;
Kent Overstreet280481d2013-10-24 16:36:03 -07002034 int oldsize = bch_count_data(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002035
Kent Overstreet26c949f2013-09-10 18:41:15 -07002036 while (!bch_keylist_empty(insert_keys)) {
Kent Overstreet403b6cd2013-07-24 17:22:44 -07002037 struct bset *i = write_block(b);
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07002038 struct bkey *k = insert_keys->keys;
Kent Overstreet26c949f2013-09-10 18:41:15 -07002039
Kent Overstreetee811282013-12-17 23:49:49 -08002040 if (b->written +
2041 __set_blocks(i, i->keys + bkey_u64s(k),
2042 block_bytes(b->c)) > btree_blocks(b))
Kent Overstreet403b6cd2013-07-24 17:22:44 -07002043 break;
2044
2045 if (bkey_cmp(k, &b->key) <= 0) {
Kent Overstreet3a3b6a42013-07-24 16:46:42 -07002046 if (!b->level)
2047 bkey_put(b->c, k);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002048
Kent Overstreet1b207d82013-09-10 18:52:54 -07002049 ret |= btree_insert_key(b, op, k, replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002050 bch_keylist_pop_front(insert_keys);
2051 } else if (bkey_cmp(&START_KEY(k), &b->key) < 0) {
Kent Overstreet26c949f2013-09-10 18:41:15 -07002052 BKEY_PADDED(key) temp;
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07002053 bkey_copy(&temp.key, insert_keys->keys);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002054
2055 bch_cut_back(&b->key, &temp.key);
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07002056 bch_cut_front(&b->key, insert_keys->keys);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002057
Kent Overstreet1b207d82013-09-10 18:52:54 -07002058 ret |= btree_insert_key(b, op, &temp.key, replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002059 break;
2060 } else {
2061 break;
2062 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07002063 }
2064
Kent Overstreet403b6cd2013-07-24 17:22:44 -07002065 BUG_ON(!bch_keylist_empty(insert_keys) && b->level);
2066
Kent Overstreetcafe5632013-03-23 16:11:31 -07002067 BUG_ON(bch_count_data(b) < oldsize);
2068 return ret;
2069}
2070
Kent Overstreet26c949f2013-09-10 18:41:15 -07002071static int btree_split(struct btree *b, struct btree_op *op,
2072 struct keylist *insert_keys,
Kent Overstreet1b207d82013-09-10 18:52:54 -07002073 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002074{
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002075 bool split;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002076 struct btree *n1, *n2 = NULL, *n3 = NULL;
2077 uint64_t start_time = local_clock();
Kent Overstreetb54d6932013-07-24 18:04:18 -07002078 struct closure cl;
Kent Overstreet17e21a92013-07-26 12:32:38 -07002079 struct keylist parent_keys;
Kent Overstreetb54d6932013-07-24 18:04:18 -07002080
2081 closure_init_stack(&cl);
Kent Overstreet17e21a92013-07-26 12:32:38 -07002082 bch_keylist_init(&parent_keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002083
Kent Overstreet78365412013-12-17 01:29:34 -08002084 if (!b->level &&
2085 btree_check_reserve(b, op))
2086 return -EINTR;
2087
Kent Overstreetbc9389e2013-09-10 19:07:35 -07002088 n1 = btree_node_alloc_replacement(b, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002089 if (IS_ERR(n1))
2090 goto err;
2091
Kent Overstreetee811282013-12-17 23:49:49 -08002092 split = set_blocks(btree_bset_first(n1),
2093 block_bytes(n1->c)) > (btree_blocks(b) * 4) / 5;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002094
Kent Overstreetcafe5632013-03-23 16:11:31 -07002095 if (split) {
2096 unsigned keys = 0;
2097
Kent Overstreetee811282013-12-17 23:49:49 -08002098 trace_bcache_btree_node_split(b, btree_bset_first(n1)->keys);
Kent Overstreetc37511b2013-04-26 15:39:55 -07002099
Kent Overstreetbc9389e2013-09-10 19:07:35 -07002100 n2 = bch_btree_node_alloc(b->c, b->level, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002101 if (IS_ERR(n2))
2102 goto err_free1;
2103
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002104 if (!b->parent) {
Kent Overstreetbc9389e2013-09-10 19:07:35 -07002105 n3 = bch_btree_node_alloc(b->c, b->level + 1, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002106 if (IS_ERR(n3))
2107 goto err_free2;
2108 }
2109
Kent Overstreet1b207d82013-09-10 18:52:54 -07002110 bch_btree_insert_keys(n1, op, insert_keys, replace_key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002111
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002112 /*
2113 * Has to be a linear search because we don't have an auxiliary
Kent Overstreetcafe5632013-03-23 16:11:31 -07002114 * search tree yet
2115 */
2116
Kent Overstreetee811282013-12-17 23:49:49 -08002117 while (keys < (btree_bset_first(n1)->keys * 3) / 5)
2118 keys += bkey_u64s(bset_bkey_idx(btree_bset_first(n1),
Kent Overstreetfafff812013-12-17 21:56:21 -08002119 keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002120
Kent Overstreetfafff812013-12-17 21:56:21 -08002121 bkey_copy_key(&n1->key,
Kent Overstreetee811282013-12-17 23:49:49 -08002122 bset_bkey_idx(btree_bset_first(n1), keys));
2123 keys += bkey_u64s(bset_bkey_idx(btree_bset_first(n1), keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002124
Kent Overstreetee811282013-12-17 23:49:49 -08002125 btree_bset_first(n2)->keys = btree_bset_first(n1)->keys - keys;
2126 btree_bset_first(n1)->keys = keys;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002127
Kent Overstreetee811282013-12-17 23:49:49 -08002128 memcpy(btree_bset_first(n2)->start,
2129 bset_bkey_last(btree_bset_first(n1)),
2130 btree_bset_first(n2)->keys * sizeof(uint64_t));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002131
2132 bkey_copy_key(&n2->key, &b->key);
2133
Kent Overstreet17e21a92013-07-26 12:32:38 -07002134 bch_keylist_add(&parent_keys, &n2->key);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002135 bch_btree_node_write(n2, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002136 rw_unlock(true, n2);
Kent Overstreetc37511b2013-04-26 15:39:55 -07002137 } else {
Kent Overstreetee811282013-12-17 23:49:49 -08002138 trace_bcache_btree_node_compact(b, btree_bset_first(n1)->keys);
Kent Overstreetc37511b2013-04-26 15:39:55 -07002139
Kent Overstreet1b207d82013-09-10 18:52:54 -07002140 bch_btree_insert_keys(n1, op, insert_keys, replace_key);
Kent Overstreetc37511b2013-04-26 15:39:55 -07002141 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07002142
Kent Overstreet17e21a92013-07-26 12:32:38 -07002143 bch_keylist_add(&parent_keys, &n1->key);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002144 bch_btree_node_write(n1, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002145
2146 if (n3) {
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002147 /* Depth increases, make a new root */
Kent Overstreetcafe5632013-03-23 16:11:31 -07002148 bkey_copy_key(&n3->key, &MAX_KEY);
Kent Overstreet17e21a92013-07-26 12:32:38 -07002149 bch_btree_insert_keys(n3, op, &parent_keys, NULL);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002150 bch_btree_node_write(n3, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002151
Kent Overstreetb54d6932013-07-24 18:04:18 -07002152 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002153 bch_btree_set_root(n3);
2154 rw_unlock(true, n3);
Kent Overstreet17e21a92013-07-26 12:32:38 -07002155
2156 btree_node_free(b);
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002157 } else if (!b->parent) {
2158 /* Root filled up but didn't need to be split */
Kent Overstreetb54d6932013-07-24 18:04:18 -07002159 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002160 bch_btree_set_root(n1);
Kent Overstreet17e21a92013-07-26 12:32:38 -07002161
2162 btree_node_free(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002163 } else {
Kent Overstreet17e21a92013-07-26 12:32:38 -07002164 /* Split a non root node */
Kent Overstreetb54d6932013-07-24 18:04:18 -07002165 closure_sync(&cl);
Kent Overstreet17e21a92013-07-26 12:32:38 -07002166 make_btree_freeing_key(b, parent_keys.top);
2167 bch_keylist_push(&parent_keys);
2168
2169 btree_node_free(b);
2170
2171 bch_btree_insert_node(b->parent, op, &parent_keys, NULL, NULL);
2172 BUG_ON(!bch_keylist_empty(&parent_keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002173 }
2174
2175 rw_unlock(true, n1);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002176
Kent Overstreet169ef1c2013-03-28 12:50:55 -06002177 bch_time_stats_update(&b->c->btree_split_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002178
2179 return 0;
2180err_free2:
Kent Overstreet5f5837d2013-12-16 16:38:49 -08002181 bkey_put(b->c, &n2->key);
Kent Overstreete8e1d462013-07-24 17:27:07 -07002182 btree_node_free(n2);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002183 rw_unlock(true, n2);
2184err_free1:
Kent Overstreet5f5837d2013-12-16 16:38:49 -08002185 bkey_put(b->c, &n1->key);
Kent Overstreete8e1d462013-07-24 17:27:07 -07002186 btree_node_free(n1);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002187 rw_unlock(true, n1);
2188err:
Kent Overstreet5f5837d2013-12-16 16:38:49 -08002189 WARN(1, "bcache: btree split failed");
2190
Kent Overstreetcafe5632013-03-23 16:11:31 -07002191 if (n3 == ERR_PTR(-EAGAIN) ||
2192 n2 == ERR_PTR(-EAGAIN) ||
2193 n1 == ERR_PTR(-EAGAIN))
2194 return -EAGAIN;
2195
Kent Overstreetcafe5632013-03-23 16:11:31 -07002196 return -ENOMEM;
2197}
2198
Kent Overstreet26c949f2013-09-10 18:41:15 -07002199static int bch_btree_insert_node(struct btree *b, struct btree_op *op,
Kent Overstreetc18536a2013-07-24 17:44:17 -07002200 struct keylist *insert_keys,
Kent Overstreet1b207d82013-09-10 18:52:54 -07002201 atomic_t *journal_ref,
2202 struct bkey *replace_key)
Kent Overstreet26c949f2013-09-10 18:41:15 -07002203{
Kent Overstreet17e21a92013-07-26 12:32:38 -07002204 BUG_ON(b->level && replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002205
Kent Overstreet17e21a92013-07-26 12:32:38 -07002206 if (should_split(b)) {
2207 if (current->bio_list) {
2208 op->lock = b->c->root->level + 1;
2209 return -EAGAIN;
2210 } else if (op->lock <= b->c->root->level) {
2211 op->lock = b->c->root->level + 1;
2212 return -EINTR;
Kent Overstreet26c949f2013-09-10 18:41:15 -07002213 } else {
Kent Overstreet17e21a92013-07-26 12:32:38 -07002214 /* Invalidated all iterators */
2215 return btree_split(b, op, insert_keys, replace_key) ?:
2216 -EINTR;
Kent Overstreet26c949f2013-09-10 18:41:15 -07002217 }
Kent Overstreet17e21a92013-07-26 12:32:38 -07002218 } else {
Kent Overstreetee811282013-12-17 23:49:49 -08002219 BUG_ON(write_block(b) != btree_bset_last(b));
Kent Overstreet26c949f2013-09-10 18:41:15 -07002220
Kent Overstreet17e21a92013-07-26 12:32:38 -07002221 if (bch_btree_insert_keys(b, op, insert_keys, replace_key)) {
2222 if (!b->level)
2223 bch_btree_leaf_dirty(b, journal_ref);
2224 else
2225 bch_btree_node_write_sync(b);
2226 }
2227
2228 return 0;
2229 }
Kent Overstreet26c949f2013-09-10 18:41:15 -07002230}
2231
Kent Overstreete7c590e2013-09-10 18:39:16 -07002232int bch_btree_insert_check_key(struct btree *b, struct btree_op *op,
2233 struct bkey *check_key)
2234{
2235 int ret = -EINTR;
2236 uint64_t btree_ptr = b->key.ptr[0];
2237 unsigned long seq = b->seq;
2238 struct keylist insert;
2239 bool upgrade = op->lock == -1;
2240
2241 bch_keylist_init(&insert);
2242
2243 if (upgrade) {
2244 rw_unlock(false, b);
2245 rw_lock(true, b, b->level);
2246
2247 if (b->key.ptr[0] != btree_ptr ||
2248 b->seq != seq + 1)
2249 goto out;
2250 }
2251
2252 SET_KEY_PTRS(check_key, 1);
2253 get_random_bytes(&check_key->ptr[0], sizeof(uint64_t));
2254
2255 SET_PTR_DEV(check_key, 0, PTR_CHECK_DEV);
2256
2257 bch_keylist_add(&insert, check_key);
2258
Kent Overstreet1b207d82013-09-10 18:52:54 -07002259 ret = bch_btree_insert_node(b, op, &insert, NULL, NULL);
Kent Overstreete7c590e2013-09-10 18:39:16 -07002260
2261 BUG_ON(!ret && !bch_keylist_empty(&insert));
2262out:
2263 if (upgrade)
2264 downgrade_write(&b->lock);
2265 return ret;
2266}
2267
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002268struct btree_insert_op {
2269 struct btree_op op;
2270 struct keylist *keys;
2271 atomic_t *journal_ref;
2272 struct bkey *replace_key;
2273};
2274
Wei Yongjun08239ca2013-11-28 10:31:35 +08002275static int btree_insert_fn(struct btree_op *b_op, struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002276{
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002277 struct btree_insert_op *op = container_of(b_op,
2278 struct btree_insert_op, op);
Kent Overstreet403b6cd2013-07-24 17:22:44 -07002279
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002280 int ret = bch_btree_insert_node(b, &op->op, op->keys,
2281 op->journal_ref, op->replace_key);
2282 if (ret && !bch_keylist_empty(op->keys))
2283 return ret;
2284 else
2285 return MAP_DONE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002286}
2287
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002288int bch_btree_insert(struct cache_set *c, struct keylist *keys,
2289 atomic_t *journal_ref, struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002290{
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002291 struct btree_insert_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002292 int ret = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002293
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002294 BUG_ON(current->bio_list);
Kent Overstreet4f3d4012013-09-10 18:46:36 -07002295 BUG_ON(bch_keylist_empty(keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002296
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002297 bch_btree_op_init(&op.op, 0);
2298 op.keys = keys;
2299 op.journal_ref = journal_ref;
2300 op.replace_key = replace_key;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002301
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002302 while (!ret && !bch_keylist_empty(keys)) {
2303 op.op.lock = 0;
2304 ret = bch_btree_map_leaf_nodes(&op.op, c,
2305 &START_KEY(keys->keys),
2306 btree_insert_fn);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002307 }
2308
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002309 if (ret) {
2310 struct bkey *k;
2311
2312 pr_err("error %i", ret);
2313
2314 while ((k = bch_keylist_pop(keys)))
Kent Overstreet3a3b6a42013-07-24 16:46:42 -07002315 bkey_put(c, k);
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002316 } else if (op.op.insert_collision)
2317 ret = -ESRCH;
Kent Overstreet6054c6d2013-07-24 18:06:22 -07002318
Kent Overstreetcafe5632013-03-23 16:11:31 -07002319 return ret;
2320}
2321
2322void bch_btree_set_root(struct btree *b)
2323{
2324 unsigned i;
Kent Overstreete49c7c32013-06-26 17:25:38 -07002325 struct closure cl;
2326
2327 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002328
Kent Overstreetc37511b2013-04-26 15:39:55 -07002329 trace_bcache_btree_set_root(b);
2330
Kent Overstreetcafe5632013-03-23 16:11:31 -07002331 BUG_ON(!b->written);
2332
2333 for (i = 0; i < KEY_PTRS(&b->key); i++)
2334 BUG_ON(PTR_BUCKET(b->c, &b->key, i)->prio != BTREE_PRIO);
2335
2336 mutex_lock(&b->c->bucket_lock);
2337 list_del_init(&b->list);
2338 mutex_unlock(&b->c->bucket_lock);
2339
2340 b->c->root = b;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002341
Kent Overstreete49c7c32013-06-26 17:25:38 -07002342 bch_journal_meta(b->c, &cl);
2343 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002344}
2345
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002346/* Map across nodes or keys */
2347
2348static int bch_btree_map_nodes_recurse(struct btree *b, struct btree_op *op,
2349 struct bkey *from,
2350 btree_map_nodes_fn *fn, int flags)
2351{
2352 int ret = MAP_CONTINUE;
2353
2354 if (b->level) {
2355 struct bkey *k;
2356 struct btree_iter iter;
2357
2358 bch_btree_iter_init(b, &iter, from);
2359
Kent Overstreeta85e9682013-12-20 17:28:16 -08002360 while ((k = bch_btree_iter_next_filter(&iter, &b->keys,
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002361 bch_ptr_bad))) {
2362 ret = btree(map_nodes_recurse, k, b,
2363 op, from, fn, flags);
2364 from = NULL;
2365
2366 if (ret != MAP_CONTINUE)
2367 return ret;
2368 }
2369 }
2370
2371 if (!b->level || flags == MAP_ALL_NODES)
2372 ret = fn(op, b);
2373
2374 return ret;
2375}
2376
2377int __bch_btree_map_nodes(struct btree_op *op, struct cache_set *c,
2378 struct bkey *from, btree_map_nodes_fn *fn, int flags)
2379{
Kent Overstreetb54d6932013-07-24 18:04:18 -07002380 return btree_root(map_nodes_recurse, c, op, from, fn, flags);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002381}
2382
2383static int bch_btree_map_keys_recurse(struct btree *b, struct btree_op *op,
2384 struct bkey *from, btree_map_keys_fn *fn,
2385 int flags)
2386{
2387 int ret = MAP_CONTINUE;
2388 struct bkey *k;
2389 struct btree_iter iter;
2390
2391 bch_btree_iter_init(b, &iter, from);
2392
Kent Overstreeta85e9682013-12-20 17:28:16 -08002393 while ((k = bch_btree_iter_next_filter(&iter, &b->keys, bch_ptr_bad))) {
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002394 ret = !b->level
2395 ? fn(op, b, k)
2396 : btree(map_keys_recurse, k, b, op, from, fn, flags);
2397 from = NULL;
2398
2399 if (ret != MAP_CONTINUE)
2400 return ret;
2401 }
2402
2403 if (!b->level && (flags & MAP_END_KEY))
2404 ret = fn(op, b, &KEY(KEY_INODE(&b->key),
2405 KEY_OFFSET(&b->key), 0));
2406
2407 return ret;
2408}
2409
2410int bch_btree_map_keys(struct btree_op *op, struct cache_set *c,
2411 struct bkey *from, btree_map_keys_fn *fn, int flags)
2412{
Kent Overstreetb54d6932013-07-24 18:04:18 -07002413 return btree_root(map_keys_recurse, c, op, from, fn, flags);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002414}
2415
Kent Overstreetcafe5632013-03-23 16:11:31 -07002416/* Keybuf code */
2417
2418static inline int keybuf_cmp(struct keybuf_key *l, struct keybuf_key *r)
2419{
2420 /* Overlapping keys compare equal */
2421 if (bkey_cmp(&l->key, &START_KEY(&r->key)) <= 0)
2422 return -1;
2423 if (bkey_cmp(&START_KEY(&l->key), &r->key) >= 0)
2424 return 1;
2425 return 0;
2426}
2427
2428static inline int keybuf_nonoverlapping_cmp(struct keybuf_key *l,
2429 struct keybuf_key *r)
2430{
2431 return clamp_t(int64_t, bkey_cmp(&l->key, &r->key), -1, 1);
2432}
2433
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002434struct refill {
2435 struct btree_op op;
Kent Overstreet48a915a2013-10-31 15:43:22 -07002436 unsigned nr_found;
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002437 struct keybuf *buf;
2438 struct bkey *end;
2439 keybuf_pred_fn *pred;
2440};
2441
2442static int refill_keybuf_fn(struct btree_op *op, struct btree *b,
2443 struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002444{
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002445 struct refill *refill = container_of(op, struct refill, op);
2446 struct keybuf *buf = refill->buf;
2447 int ret = MAP_CONTINUE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002448
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002449 if (bkey_cmp(k, refill->end) >= 0) {
2450 ret = MAP_DONE;
2451 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002452 }
2453
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002454 if (!KEY_SIZE(k)) /* end key */
2455 goto out;
2456
2457 if (refill->pred(buf, k)) {
2458 struct keybuf_key *w;
2459
2460 spin_lock(&buf->lock);
2461
2462 w = array_alloc(&buf->freelist);
2463 if (!w) {
2464 spin_unlock(&buf->lock);
2465 return MAP_DONE;
2466 }
2467
2468 w->private = NULL;
2469 bkey_copy(&w->key, k);
2470
2471 if (RB_INSERT(&buf->keys, w, node, keybuf_cmp))
2472 array_free(&buf->freelist, w);
Kent Overstreet48a915a2013-10-31 15:43:22 -07002473 else
2474 refill->nr_found++;
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002475
2476 if (array_freelist_empty(&buf->freelist))
2477 ret = MAP_DONE;
2478
2479 spin_unlock(&buf->lock);
2480 }
2481out:
2482 buf->last_scanned = *k;
2483 return ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002484}
2485
2486void bch_refill_keybuf(struct cache_set *c, struct keybuf *buf,
Kent Overstreet72c27062013-06-05 06:24:39 -07002487 struct bkey *end, keybuf_pred_fn *pred)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002488{
2489 struct bkey start = buf->last_scanned;
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002490 struct refill refill;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002491
2492 cond_resched();
2493
Kent Overstreetb54d6932013-07-24 18:04:18 -07002494 bch_btree_op_init(&refill.op, -1);
Kent Overstreet48a915a2013-10-31 15:43:22 -07002495 refill.nr_found = 0;
2496 refill.buf = buf;
2497 refill.end = end;
2498 refill.pred = pred;
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002499
2500 bch_btree_map_keys(&refill.op, c, &buf->last_scanned,
2501 refill_keybuf_fn, MAP_END_KEY);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002502
Kent Overstreet48a915a2013-10-31 15:43:22 -07002503 trace_bcache_keyscan(refill.nr_found,
2504 KEY_INODE(&start), KEY_OFFSET(&start),
2505 KEY_INODE(&buf->last_scanned),
2506 KEY_OFFSET(&buf->last_scanned));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002507
2508 spin_lock(&buf->lock);
2509
2510 if (!RB_EMPTY_ROOT(&buf->keys)) {
2511 struct keybuf_key *w;
2512 w = RB_FIRST(&buf->keys, struct keybuf_key, node);
2513 buf->start = START_KEY(&w->key);
2514
2515 w = RB_LAST(&buf->keys, struct keybuf_key, node);
2516 buf->end = w->key;
2517 } else {
2518 buf->start = MAX_KEY;
2519 buf->end = MAX_KEY;
2520 }
2521
2522 spin_unlock(&buf->lock);
2523}
2524
2525static void __bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w)
2526{
2527 rb_erase(&w->node, &buf->keys);
2528 array_free(&buf->freelist, w);
2529}
2530
2531void bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w)
2532{
2533 spin_lock(&buf->lock);
2534 __bch_keybuf_del(buf, w);
2535 spin_unlock(&buf->lock);
2536}
2537
2538bool bch_keybuf_check_overlapping(struct keybuf *buf, struct bkey *start,
2539 struct bkey *end)
2540{
2541 bool ret = false;
2542 struct keybuf_key *p, *w, s;
2543 s.key = *start;
2544
2545 if (bkey_cmp(end, &buf->start) <= 0 ||
2546 bkey_cmp(start, &buf->end) >= 0)
2547 return false;
2548
2549 spin_lock(&buf->lock);
2550 w = RB_GREATER(&buf->keys, s, node, keybuf_nonoverlapping_cmp);
2551
2552 while (w && bkey_cmp(&START_KEY(&w->key), end) < 0) {
2553 p = w;
2554 w = RB_NEXT(w, node);
2555
2556 if (p->private)
2557 ret = true;
2558 else
2559 __bch_keybuf_del(buf, p);
2560 }
2561
2562 spin_unlock(&buf->lock);
2563 return ret;
2564}
2565
2566struct keybuf_key *bch_keybuf_next(struct keybuf *buf)
2567{
2568 struct keybuf_key *w;
2569 spin_lock(&buf->lock);
2570
2571 w = RB_FIRST(&buf->keys, struct keybuf_key, node);
2572
2573 while (w && w->private)
2574 w = RB_NEXT(w, node);
2575
2576 if (w)
2577 w->private = ERR_PTR(-EINTR);
2578
2579 spin_unlock(&buf->lock);
2580 return w;
2581}
2582
2583struct keybuf_key *bch_keybuf_next_rescan(struct cache_set *c,
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002584 struct keybuf *buf,
2585 struct bkey *end,
2586 keybuf_pred_fn *pred)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002587{
2588 struct keybuf_key *ret;
2589
2590 while (1) {
2591 ret = bch_keybuf_next(buf);
2592 if (ret)
2593 break;
2594
2595 if (bkey_cmp(&buf->last_scanned, end) >= 0) {
2596 pr_debug("scan finished");
2597 break;
2598 }
2599
Kent Overstreet72c27062013-06-05 06:24:39 -07002600 bch_refill_keybuf(c, buf, end, pred);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002601 }
2602
2603 return ret;
2604}
2605
Kent Overstreet72c27062013-06-05 06:24:39 -07002606void bch_keybuf_init(struct keybuf *buf)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002607{
Kent Overstreetcafe5632013-03-23 16:11:31 -07002608 buf->last_scanned = MAX_KEY;
2609 buf->keys = RB_ROOT;
2610
2611 spin_lock_init(&buf->lock);
2612 array_allocator_init(&buf->freelist);
2613}
2614
2615void bch_btree_exit(void)
2616{
2617 if (btree_io_wq)
2618 destroy_workqueue(btree_io_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002619}
2620
2621int __init bch_btree_init(void)
2622{
Kent Overstreet72a44512013-10-24 17:19:26 -07002623 btree_io_wq = create_singlethread_workqueue("bch_btree_io");
2624 if (!btree_io_wq)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002625 return -ENOMEM;
2626
2627 return 0;
2628}