blob: 9424c8a15e377a264f30016fcfce645a204cdb8d [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
Kent Overstreetcafe5632013-03-23 16:11:31 -0700182/* Btree key manipulation */
183
Kent Overstreet3a3b6a42013-07-24 16:46:42 -0700184void bkey_put(struct cache_set *c, struct bkey *k)
Kent Overstreete7c590e2013-09-10 18:39:16 -0700185{
186 unsigned i;
187
188 for (i = 0; i < KEY_PTRS(k); i++)
189 if (ptr_available(c, k, i))
190 atomic_dec_bug(&PTR_BUCKET(c, k, i)->pin);
191}
192
Kent Overstreetcafe5632013-03-23 16:11:31 -0700193/* Btree IO */
194
195static uint64_t btree_csum_set(struct btree *b, struct bset *i)
196{
197 uint64_t crc = b->key.ptr[0];
Kent Overstreetfafff812013-12-17 21:56:21 -0800198 void *data = (void *) i + 8, *end = bset_bkey_last(i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700199
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600200 crc = bch_crc64_update(crc, data, end - data);
Kent Overstreetc19ed232013-03-26 13:49:02 -0700201 return crc ^ 0xffffffffffffffffULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700202}
203
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800204void bch_btree_node_read_done(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700205{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700206 const char *err = "bad btree header";
Kent Overstreetee811282013-12-17 23:49:49 -0800207 struct bset *i = btree_bset_first(b);
Kent Overstreet57943512013-04-25 13:58:35 -0700208 struct btree_iter *iter;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700209
Kent Overstreet57943512013-04-25 13:58:35 -0700210 iter = mempool_alloc(b->c->fill_iter, GFP_NOWAIT);
211 iter->size = b->c->sb.bucket_size / b->c->sb.block_size;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700212 iter->used = 0;
213
Kent Overstreet280481d2013-10-24 16:36:03 -0700214#ifdef CONFIG_BCACHE_DEBUG
Kent Overstreetc052dd92013-11-11 17:35:24 -0800215 iter->b = &b->keys;
Kent Overstreet280481d2013-10-24 16:36:03 -0700216#endif
217
Kent Overstreet57943512013-04-25 13:58:35 -0700218 if (!i->seq)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700219 goto err;
220
221 for (;
Kent Overstreeta85e9682013-12-20 17:28:16 -0800222 b->written < btree_blocks(b) && i->seq == b->keys.set[0].data->seq;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700223 i = write_block(b)) {
224 err = "unsupported bset version";
225 if (i->version > BCACHE_BSET_VERSION)
226 goto err;
227
228 err = "bad btree header";
Kent Overstreetee811282013-12-17 23:49:49 -0800229 if (b->written + set_blocks(i, block_bytes(b->c)) >
230 btree_blocks(b))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700231 goto err;
232
233 err = "bad magic";
Kent Overstreet81ab4192013-10-31 15:46:42 -0700234 if (i->magic != bset_magic(&b->c->sb))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700235 goto err;
236
237 err = "bad checksum";
238 switch (i->version) {
239 case 0:
240 if (i->csum != csum_set(i))
241 goto err;
242 break;
243 case BCACHE_BSET_VERSION:
244 if (i->csum != btree_csum_set(b, i))
245 goto err;
246 break;
247 }
248
249 err = "empty set";
Kent Overstreeta85e9682013-12-20 17:28:16 -0800250 if (i != b->keys.set[0].data && !i->keys)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700251 goto err;
252
Kent Overstreetfafff812013-12-17 21:56:21 -0800253 bch_btree_iter_push(iter, i->start, bset_bkey_last(i));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700254
Kent Overstreetee811282013-12-17 23:49:49 -0800255 b->written += set_blocks(i, block_bytes(b->c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700256 }
257
258 err = "corrupted btree";
259 for (i = write_block(b);
Kent Overstreeta85e9682013-12-20 17:28:16 -0800260 bset_sector_offset(&b->keys, i) < KEY_SIZE(&b->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700261 i = ((void *) i) + block_bytes(b->c))
Kent Overstreeta85e9682013-12-20 17:28:16 -0800262 if (i->seq == b->keys.set[0].data->seq)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700263 goto err;
264
Kent Overstreeta85e9682013-12-20 17:28:16 -0800265 bch_btree_sort_and_fix_extents(&b->keys, iter, &b->c->sort);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700266
Kent Overstreeta85e9682013-12-20 17:28:16 -0800267 i = b->keys.set[0].data;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700268 err = "short btree key";
Kent Overstreeta85e9682013-12-20 17:28:16 -0800269 if (b->keys.set[0].size &&
270 bkey_cmp(&b->key, &b->keys.set[0].end) < 0)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700271 goto err;
272
273 if (b->written < btree_blocks(b))
Kent Overstreeta85e9682013-12-20 17:28:16 -0800274 bch_bset_init_next(&b->keys, write_block(b),
275 bset_magic(&b->c->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700276out:
Kent Overstreet57943512013-04-25 13:58:35 -0700277 mempool_free(iter, b->c->fill_iter);
278 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700279err:
280 set_btree_node_io_error(b);
Kent Overstreet88b9f8c2013-12-17 21:46:35 -0800281 bch_cache_set_error(b->c, "%s at bucket %zu, block %u, %u keys",
Kent Overstreetcafe5632013-03-23 16:11:31 -0700282 err, PTR_BUCKET_NR(b->c, &b->key, 0),
Kent Overstreet88b9f8c2013-12-17 21:46:35 -0800283 bset_block_offset(b, i), i->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700284 goto out;
285}
286
Kent Overstreet57943512013-04-25 13:58:35 -0700287static void btree_node_read_endio(struct bio *bio, int error)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700288{
Kent Overstreet57943512013-04-25 13:58:35 -0700289 struct closure *cl = bio->bi_private;
290 closure_put(cl);
291}
Kent Overstreetcafe5632013-03-23 16:11:31 -0700292
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800293static void bch_btree_node_read(struct btree *b)
Kent Overstreet57943512013-04-25 13:58:35 -0700294{
295 uint64_t start_time = local_clock();
296 struct closure cl;
297 struct bio *bio;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700298
Kent Overstreetc37511b2013-04-26 15:39:55 -0700299 trace_bcache_btree_read(b);
300
Kent Overstreet57943512013-04-25 13:58:35 -0700301 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700302
Kent Overstreet57943512013-04-25 13:58:35 -0700303 bio = bch_bbio_alloc(b->c);
304 bio->bi_rw = REQ_META|READ_SYNC;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700305 bio->bi_iter.bi_size = KEY_SIZE(&b->key) << 9;
Kent Overstreet57943512013-04-25 13:58:35 -0700306 bio->bi_end_io = btree_node_read_endio;
307 bio->bi_private = &cl;
308
Kent Overstreeta85e9682013-12-20 17:28:16 -0800309 bch_bio_map(bio, b->keys.set[0].data);
Kent Overstreet57943512013-04-25 13:58:35 -0700310
Kent Overstreet57943512013-04-25 13:58:35 -0700311 bch_submit_bbio(bio, b->c, &b->key, 0);
312 closure_sync(&cl);
313
314 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
315 set_btree_node_io_error(b);
316
317 bch_bbio_free(bio, b->c);
318
319 if (btree_node_io_error(b))
320 goto err;
321
322 bch_btree_node_read_done(b);
Kent Overstreet57943512013-04-25 13:58:35 -0700323 bch_time_stats_update(&b->c->btree_read_time, start_time);
Kent Overstreet57943512013-04-25 13:58:35 -0700324
325 return;
326err:
Geert Uytterhoeven61cbd252013-09-23 23:17:30 -0700327 bch_cache_set_error(b->c, "io error reading bucket %zu",
Kent Overstreet57943512013-04-25 13:58:35 -0700328 PTR_BUCKET_NR(b->c, &b->key, 0));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700329}
330
331static void btree_complete_write(struct btree *b, struct btree_write *w)
332{
333 if (w->prio_blocked &&
334 !atomic_sub_return(w->prio_blocked, &b->c->prio_blocked))
Kent Overstreet119ba0f2013-04-24 19:01:12 -0700335 wake_up_allocators(b->c);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700336
337 if (w->journal) {
338 atomic_dec_bug(w->journal);
339 __closure_wake_up(&b->c->journal.wait);
340 }
341
Kent Overstreetcafe5632013-03-23 16:11:31 -0700342 w->prio_blocked = 0;
343 w->journal = NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700344}
345
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800346static void btree_node_write_unlock(struct closure *cl)
347{
348 struct btree *b = container_of(cl, struct btree, io);
349
350 up(&b->io_mutex);
351}
352
Kent Overstreet57943512013-04-25 13:58:35 -0700353static void __btree_node_write_done(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700354{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800355 struct btree *b = container_of(cl, struct btree, io);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700356 struct btree_write *w = btree_prev_write(b);
357
358 bch_bbio_free(b->bio, b->c);
359 b->bio = NULL;
360 btree_complete_write(b, w);
361
362 if (btree_node_dirty(b))
363 queue_delayed_work(btree_io_wq, &b->work,
364 msecs_to_jiffies(30000));
365
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800366 closure_return_with_destructor(cl, btree_node_write_unlock);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700367}
368
Kent Overstreet57943512013-04-25 13:58:35 -0700369static void btree_node_write_done(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700370{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800371 struct btree *b = container_of(cl, struct btree, io);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700372 struct bio_vec *bv;
373 int n;
374
Kent Overstreet79886132013-11-23 17:19:00 -0800375 bio_for_each_segment_all(bv, b->bio, n)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700376 __free_page(bv->bv_page);
377
Kent Overstreet57943512013-04-25 13:58:35 -0700378 __btree_node_write_done(cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700379}
380
Kent Overstreet57943512013-04-25 13:58:35 -0700381static void btree_node_write_endio(struct bio *bio, int error)
382{
383 struct closure *cl = bio->bi_private;
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800384 struct btree *b = container_of(cl, struct btree, io);
Kent Overstreet57943512013-04-25 13:58:35 -0700385
386 if (error)
387 set_btree_node_io_error(b);
388
389 bch_bbio_count_io_errors(b->c, bio, error, "writing btree");
390 closure_put(cl);
391}
392
393static void do_btree_node_write(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700394{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800395 struct closure *cl = &b->io;
Kent Overstreetee811282013-12-17 23:49:49 -0800396 struct bset *i = btree_bset_last(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700397 BKEY_PADDED(key) k;
398
399 i->version = BCACHE_BSET_VERSION;
400 i->csum = btree_csum_set(b, i);
401
Kent Overstreet57943512013-04-25 13:58:35 -0700402 BUG_ON(b->bio);
403 b->bio = bch_bbio_alloc(b->c);
404
405 b->bio->bi_end_io = btree_node_write_endio;
Kent Overstreetfaadf0c2013-11-01 18:03:08 -0700406 b->bio->bi_private = cl;
Kent Overstreete49c7c32013-06-26 17:25:38 -0700407 b->bio->bi_rw = REQ_META|WRITE_SYNC|REQ_FUA;
Kent Overstreetee811282013-12-17 23:49:49 -0800408 b->bio->bi_iter.bi_size = roundup(set_bytes(i), block_bytes(b->c));
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600409 bch_bio_map(b->bio, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700410
Kent Overstreete49c7c32013-06-26 17:25:38 -0700411 /*
412 * If we're appending to a leaf node, we don't technically need FUA -
413 * this write just needs to be persisted before the next journal write,
414 * which will be marked FLUSH|FUA.
415 *
416 * Similarly if we're writing a new btree root - the pointer is going to
417 * be in the next journal entry.
418 *
419 * But if we're writing a new btree node (that isn't a root) or
420 * appending to a non leaf btree node, we need either FUA or a flush
421 * when we write the parent with the new pointer. FUA is cheaper than a
422 * flush, and writes appending to leaf nodes aren't blocking anything so
423 * just make all btree node writes FUA to keep things sane.
424 */
425
Kent Overstreetcafe5632013-03-23 16:11:31 -0700426 bkey_copy(&k.key, &b->key);
Kent Overstreetee811282013-12-17 23:49:49 -0800427 SET_PTR_OFFSET(&k.key, 0, PTR_OFFSET(&k.key, 0) +
Kent Overstreeta85e9682013-12-20 17:28:16 -0800428 bset_sector_offset(&b->keys, i));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700429
Kent Overstreet8e51e412013-06-06 18:15:57 -0700430 if (!bio_alloc_pages(b->bio, GFP_NOIO)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700431 int j;
432 struct bio_vec *bv;
433 void *base = (void *) ((unsigned long) i & ~(PAGE_SIZE - 1));
434
Kent Overstreet79886132013-11-23 17:19:00 -0800435 bio_for_each_segment_all(bv, b->bio, j)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700436 memcpy(page_address(bv->bv_page),
437 base + j * PAGE_SIZE, PAGE_SIZE);
438
Kent Overstreetcafe5632013-03-23 16:11:31 -0700439 bch_submit_bbio(b->bio, b->c, &k.key, 0);
440
Kent Overstreet57943512013-04-25 13:58:35 -0700441 continue_at(cl, btree_node_write_done, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700442 } else {
443 b->bio->bi_vcnt = 0;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600444 bch_bio_map(b->bio, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700445
Kent Overstreetcafe5632013-03-23 16:11:31 -0700446 bch_submit_bbio(b->bio, b->c, &k.key, 0);
447
448 closure_sync(cl);
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800449 continue_at_nobarrier(cl, __btree_node_write_done, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700450 }
451}
452
Kent Overstreet57943512013-04-25 13:58:35 -0700453void bch_btree_node_write(struct btree *b, struct closure *parent)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700454{
Kent Overstreetee811282013-12-17 23:49:49 -0800455 struct bset *i = btree_bset_last(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700456
Kent Overstreetc37511b2013-04-26 15:39:55 -0700457 trace_bcache_btree_write(b);
458
Kent Overstreetcafe5632013-03-23 16:11:31 -0700459 BUG_ON(current->bio_list);
Kent Overstreet57943512013-04-25 13:58:35 -0700460 BUG_ON(b->written >= btree_blocks(b));
461 BUG_ON(b->written && !i->keys);
Kent Overstreetee811282013-12-17 23:49:49 -0800462 BUG_ON(btree_bset_first(b)->seq != i->seq);
Kent Overstreet280481d2013-10-24 16:36:03 -0700463 bch_check_keys(b, "writing");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700464
Kent Overstreetcafe5632013-03-23 16:11:31 -0700465 cancel_delayed_work(&b->work);
466
Kent Overstreet57943512013-04-25 13:58:35 -0700467 /* If caller isn't waiting for write, parent refcount is cache set */
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800468 down(&b->io_mutex);
469 closure_init(&b->io, parent ?: &b->c->cl);
Kent Overstreet57943512013-04-25 13:58:35 -0700470
Kent Overstreetcafe5632013-03-23 16:11:31 -0700471 clear_bit(BTREE_NODE_dirty, &b->flags);
472 change_bit(BTREE_NODE_write_idx, &b->flags);
473
Kent Overstreet57943512013-04-25 13:58:35 -0700474 do_btree_node_write(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700475
Kent Overstreetee811282013-12-17 23:49:49 -0800476 atomic_long_add(set_blocks(i, block_bytes(b->c)) * b->c->sb.block_size,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700477 &PTR_CACHE(b->c, &b->key, 0)->btree_sectors_written);
478
Kent Overstreeta85e9682013-12-20 17:28:16 -0800479 b->written += set_blocks(i, block_bytes(b->c));
480
Kent Overstreet67539e82013-09-10 22:53:34 -0700481 /* If not a leaf node, always sort */
Kent Overstreeta85e9682013-12-20 17:28:16 -0800482 if (b->level && b->keys.nsets)
Kent Overstreet67539e82013-09-10 22:53:34 -0700483 bch_btree_sort(b, &b->c->sort);
484 else
485 bch_btree_sort_lazy(b, &b->c->sort);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700486
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800487 /*
488 * do verify if there was more than one set initially (i.e. we did a
489 * sort) and we sorted down to a single set:
490 */
Kent Overstreeta85e9682013-12-20 17:28:16 -0800491 if (i != b->keys.set->data && !b->keys.nsets)
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800492 bch_btree_verify(b);
493
Kent Overstreetcafe5632013-03-23 16:11:31 -0700494 if (b->written < btree_blocks(b))
Kent Overstreeta85e9682013-12-20 17:28:16 -0800495 bch_bset_init_next(&b->keys, write_block(b),
496 bset_magic(&b->c->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700497}
498
Kent Overstreetf269af52013-07-23 20:48:29 -0700499static void bch_btree_node_write_sync(struct btree *b)
500{
501 struct closure cl;
502
503 closure_init_stack(&cl);
504 bch_btree_node_write(b, &cl);
505 closure_sync(&cl);
506}
507
Kent Overstreet57943512013-04-25 13:58:35 -0700508static void btree_node_write_work(struct work_struct *w)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700509{
510 struct btree *b = container_of(to_delayed_work(w), struct btree, work);
511
Kent Overstreet57943512013-04-25 13:58:35 -0700512 rw_lock(true, b, b->level);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700513
514 if (btree_node_dirty(b))
Kent Overstreet57943512013-04-25 13:58:35 -0700515 bch_btree_node_write(b, NULL);
516 rw_unlock(true, b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700517}
518
Kent Overstreetc18536a2013-07-24 17:44:17 -0700519static void bch_btree_leaf_dirty(struct btree *b, atomic_t *journal_ref)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700520{
Kent Overstreetee811282013-12-17 23:49:49 -0800521 struct bset *i = btree_bset_last(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700522 struct btree_write *w = btree_current_write(b);
523
Kent Overstreet57943512013-04-25 13:58:35 -0700524 BUG_ON(!b->written);
525 BUG_ON(!i->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700526
Kent Overstreet57943512013-04-25 13:58:35 -0700527 if (!btree_node_dirty(b))
528 queue_delayed_work(btree_io_wq, &b->work, 30 * HZ);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700529
Kent Overstreet57943512013-04-25 13:58:35 -0700530 set_btree_node_dirty(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700531
Kent Overstreetc18536a2013-07-24 17:44:17 -0700532 if (journal_ref) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700533 if (w->journal &&
Kent Overstreetc18536a2013-07-24 17:44:17 -0700534 journal_pin_cmp(b->c, w->journal, journal_ref)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700535 atomic_dec_bug(w->journal);
536 w->journal = NULL;
537 }
538
539 if (!w->journal) {
Kent Overstreetc18536a2013-07-24 17:44:17 -0700540 w->journal = journal_ref;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700541 atomic_inc(w->journal);
542 }
543 }
544
Kent Overstreetcafe5632013-03-23 16:11:31 -0700545 /* Force write if set is too big */
Kent Overstreet57943512013-04-25 13:58:35 -0700546 if (set_bytes(i) > PAGE_SIZE - 48 &&
547 !current->bio_list)
548 bch_btree_node_write(b, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700549}
550
551/*
552 * Btree in memory cache - allocation/freeing
553 * mca -> memory cache
554 */
555
Kent Overstreetcafe5632013-03-23 16:11:31 -0700556#define mca_reserve(c) (((c->root && c->root->level) \
557 ? c->root->level : 1) * 8 + 16)
558#define mca_can_free(c) \
559 max_t(int, 0, c->bucket_cache_used - mca_reserve(c))
560
561static void mca_data_free(struct btree *b)
562{
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800563 BUG_ON(b->io_mutex.count != 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700564
Kent Overstreeta85e9682013-12-20 17:28:16 -0800565 bch_btree_keys_free(&b->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700566
Kent Overstreetcafe5632013-03-23 16:11:31 -0700567 b->c->bucket_cache_used--;
Kent Overstreetee811282013-12-17 23:49:49 -0800568 list_move(&b->list, &b->c->btree_cache_freed);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700569}
570
571static void mca_bucket_free(struct btree *b)
572{
573 BUG_ON(btree_node_dirty(b));
574
575 b->key.ptr[0] = 0;
576 hlist_del_init_rcu(&b->hash);
577 list_move(&b->list, &b->c->btree_cache_freeable);
578}
579
580static unsigned btree_order(struct bkey *k)
581{
582 return ilog2(KEY_SIZE(k) / PAGE_SECTORS ?: 1);
583}
584
585static void mca_data_alloc(struct btree *b, struct bkey *k, gfp_t gfp)
586{
Kent Overstreeta85e9682013-12-20 17:28:16 -0800587 if (!bch_btree_keys_alloc(&b->keys,
Kent Overstreetee811282013-12-17 23:49:49 -0800588 max_t(unsigned,
589 ilog2(b->c->btree_pages),
590 btree_order(k)),
591 gfp)) {
592 b->c->bucket_cache_used++;
593 list_move(&b->list, &b->c->btree_cache);
594 } else {
595 list_move(&b->list, &b->c->btree_cache_freed);
596 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700597}
598
599static struct btree *mca_bucket_alloc(struct cache_set *c,
600 struct bkey *k, gfp_t gfp)
601{
602 struct btree *b = kzalloc(sizeof(struct btree), gfp);
603 if (!b)
604 return NULL;
605
606 init_rwsem(&b->lock);
607 lockdep_set_novalidate_class(&b->lock);
608 INIT_LIST_HEAD(&b->list);
Kent Overstreet57943512013-04-25 13:58:35 -0700609 INIT_DELAYED_WORK(&b->work, btree_node_write_work);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700610 b->c = c;
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800611 sema_init(&b->io_mutex, 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700612
613 mca_data_alloc(b, k, gfp);
614 return b;
615}
616
Kent Overstreete8e1d462013-07-24 17:27:07 -0700617static int mca_reap(struct btree *b, unsigned min_order, bool flush)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700618{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700619 struct closure cl;
620
621 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700622 lockdep_assert_held(&b->c->bucket_lock);
623
624 if (!down_write_trylock(&b->lock))
625 return -ENOMEM;
626
Kent Overstreeta85e9682013-12-20 17:28:16 -0800627 BUG_ON(btree_node_dirty(b) && !b->keys.set[0].data);
Kent Overstreete8e1d462013-07-24 17:27:07 -0700628
Kent Overstreeta85e9682013-12-20 17:28:16 -0800629 if (b->keys.page_order < min_order)
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800630 goto out_unlock;
631
632 if (!flush) {
633 if (btree_node_dirty(b))
634 goto out_unlock;
635
636 if (down_trylock(&b->io_mutex))
637 goto out_unlock;
638 up(&b->io_mutex);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700639 }
640
Kent Overstreetf269af52013-07-23 20:48:29 -0700641 if (btree_node_dirty(b))
642 bch_btree_node_write_sync(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700643
Kent Overstreete8e1d462013-07-24 17:27:07 -0700644 /* wait for any in flight btree write */
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800645 down(&b->io_mutex);
646 up(&b->io_mutex);
Kent Overstreete8e1d462013-07-24 17:27:07 -0700647
Kent Overstreetcafe5632013-03-23 16:11:31 -0700648 return 0;
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800649out_unlock:
650 rw_unlock(true, b);
651 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700652}
653
Dave Chinner7dc19d52013-08-28 10:18:11 +1000654static unsigned long bch_mca_scan(struct shrinker *shrink,
655 struct shrink_control *sc)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700656{
657 struct cache_set *c = container_of(shrink, struct cache_set, shrink);
658 struct btree *b, *t;
659 unsigned long i, nr = sc->nr_to_scan;
Dave Chinner7dc19d52013-08-28 10:18:11 +1000660 unsigned long freed = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700661
662 if (c->shrinker_disabled)
Dave Chinner7dc19d52013-08-28 10:18:11 +1000663 return SHRINK_STOP;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700664
665 if (c->try_harder)
Dave Chinner7dc19d52013-08-28 10:18:11 +1000666 return SHRINK_STOP;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700667
668 /* Return -1 if we can't do anything right now */
Kent Overstreeta698e082013-09-23 23:17:34 -0700669 if (sc->gfp_mask & __GFP_IO)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700670 mutex_lock(&c->bucket_lock);
671 else if (!mutex_trylock(&c->bucket_lock))
672 return -1;
673
Kent Overstreet36c9ea92013-06-03 13:04:56 -0700674 /*
675 * It's _really_ critical that we don't free too many btree nodes - we
676 * have to always leave ourselves a reserve. The reserve is how we
677 * guarantee that allocating memory for a new btree node can always
678 * succeed, so that inserting keys into the btree can always succeed and
679 * IO can always make forward progress:
680 */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700681 nr /= c->btree_pages;
682 nr = min_t(unsigned long, nr, mca_can_free(c));
683
684 i = 0;
685 list_for_each_entry_safe(b, t, &c->btree_cache_freeable, list) {
Dave Chinner7dc19d52013-08-28 10:18:11 +1000686 if (freed >= nr)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700687 break;
688
689 if (++i > 3 &&
Kent Overstreete8e1d462013-07-24 17:27:07 -0700690 !mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700691 mca_data_free(b);
692 rw_unlock(true, b);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000693 freed++;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700694 }
695 }
696
Dave Chinner7dc19d52013-08-28 10:18:11 +1000697 for (i = 0; (nr--) && i < c->bucket_cache_used; i++) {
Kent Overstreetb0f32a52013-12-10 13:24:26 -0800698 if (list_empty(&c->btree_cache))
699 goto out;
700
Kent Overstreetcafe5632013-03-23 16:11:31 -0700701 b = list_first_entry(&c->btree_cache, struct btree, list);
702 list_rotate_left(&c->btree_cache);
703
704 if (!b->accessed &&
Kent Overstreete8e1d462013-07-24 17:27:07 -0700705 !mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700706 mca_bucket_free(b);
707 mca_data_free(b);
708 rw_unlock(true, b);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000709 freed++;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700710 } else
711 b->accessed = 0;
712 }
713out:
Kent Overstreetcafe5632013-03-23 16:11:31 -0700714 mutex_unlock(&c->bucket_lock);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000715 return freed;
716}
717
718static unsigned long bch_mca_count(struct shrinker *shrink,
719 struct shrink_control *sc)
720{
721 struct cache_set *c = container_of(shrink, struct cache_set, shrink);
722
723 if (c->shrinker_disabled)
724 return 0;
725
726 if (c->try_harder)
727 return 0;
728
729 return mca_can_free(c) * c->btree_pages;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700730}
731
732void bch_btree_cache_free(struct cache_set *c)
733{
734 struct btree *b;
735 struct closure cl;
736 closure_init_stack(&cl);
737
738 if (c->shrink.list.next)
739 unregister_shrinker(&c->shrink);
740
741 mutex_lock(&c->bucket_lock);
742
743#ifdef CONFIG_BCACHE_DEBUG
744 if (c->verify_data)
745 list_move(&c->verify_data->list, &c->btree_cache);
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800746
747 free_pages((unsigned long) c->verify_ondisk, ilog2(bucket_pages(c)));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700748#endif
749
750 list_splice(&c->btree_cache_freeable,
751 &c->btree_cache);
752
753 while (!list_empty(&c->btree_cache)) {
754 b = list_first_entry(&c->btree_cache, struct btree, list);
755
756 if (btree_node_dirty(b))
757 btree_complete_write(b, btree_current_write(b));
758 clear_bit(BTREE_NODE_dirty, &b->flags);
759
760 mca_data_free(b);
761 }
762
763 while (!list_empty(&c->btree_cache_freed)) {
764 b = list_first_entry(&c->btree_cache_freed,
765 struct btree, list);
766 list_del(&b->list);
767 cancel_delayed_work_sync(&b->work);
768 kfree(b);
769 }
770
771 mutex_unlock(&c->bucket_lock);
772}
773
774int bch_btree_cache_alloc(struct cache_set *c)
775{
776 unsigned i;
777
Kent Overstreetcafe5632013-03-23 16:11:31 -0700778 for (i = 0; i < mca_reserve(c); i++)
Kent Overstreet72a44512013-10-24 17:19:26 -0700779 if (!mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL))
780 return -ENOMEM;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700781
782 list_splice_init(&c->btree_cache,
783 &c->btree_cache_freeable);
784
785#ifdef CONFIG_BCACHE_DEBUG
786 mutex_init(&c->verify_lock);
787
Kent Overstreet78b77bf2013-12-17 22:49:08 -0800788 c->verify_ondisk = (void *)
789 __get_free_pages(GFP_KERNEL, ilog2(bucket_pages(c)));
790
Kent Overstreetcafe5632013-03-23 16:11:31 -0700791 c->verify_data = mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL);
792
793 if (c->verify_data &&
Kent Overstreeta85e9682013-12-20 17:28:16 -0800794 c->verify_data->keys.set->data)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700795 list_del_init(&c->verify_data->list);
796 else
797 c->verify_data = NULL;
798#endif
799
Dave Chinner7dc19d52013-08-28 10:18:11 +1000800 c->shrink.count_objects = bch_mca_count;
801 c->shrink.scan_objects = bch_mca_scan;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700802 c->shrink.seeks = 4;
803 c->shrink.batch = c->btree_pages * 2;
804 register_shrinker(&c->shrink);
805
806 return 0;
807}
808
809/* Btree in memory cache - hash table */
810
811static struct hlist_head *mca_hash(struct cache_set *c, struct bkey *k)
812{
813 return &c->bucket_hash[hash_32(PTR_HASH(c, k), BUCKET_HASH_BITS)];
814}
815
816static struct btree *mca_find(struct cache_set *c, struct bkey *k)
817{
818 struct btree *b;
819
820 rcu_read_lock();
821 hlist_for_each_entry_rcu(b, mca_hash(c, k), hash)
822 if (PTR_HASH(c, &b->key) == PTR_HASH(c, k))
823 goto out;
824 b = NULL;
825out:
826 rcu_read_unlock();
827 return b;
828}
829
Kent Overstreete8e1d462013-07-24 17:27:07 -0700830static struct btree *mca_cannibalize(struct cache_set *c, struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700831{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700832 struct btree *b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700833
Kent Overstreetc37511b2013-04-26 15:39:55 -0700834 trace_bcache_btree_cache_cannibalize(c);
835
Kent Overstreete8e1d462013-07-24 17:27:07 -0700836 if (!c->try_harder) {
837 c->try_harder = current;
838 c->try_harder_start = local_clock();
839 } else if (c->try_harder != current)
840 return ERR_PTR(-ENOSPC);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700841
Kent Overstreete8e1d462013-07-24 17:27:07 -0700842 list_for_each_entry_reverse(b, &c->btree_cache, list)
843 if (!mca_reap(b, btree_order(k), false))
844 return b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700845
Kent Overstreete8e1d462013-07-24 17:27:07 -0700846 list_for_each_entry_reverse(b, &c->btree_cache, list)
847 if (!mca_reap(b, btree_order(k), true))
848 return b;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700849
Kent Overstreete8e1d462013-07-24 17:27:07 -0700850 return ERR_PTR(-ENOMEM);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700851}
852
853/*
854 * We can only have one thread cannibalizing other cached btree nodes at a time,
855 * or we'll deadlock. We use an open coded mutex to ensure that, which a
856 * cannibalize_bucket() will take. This means every time we unlock the root of
857 * the btree, we need to release this lock if we have it held.
858 */
Kent Overstreetdf8e8972013-07-24 17:37:59 -0700859static void bch_cannibalize_unlock(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700860{
Kent Overstreete8e1d462013-07-24 17:27:07 -0700861 if (c->try_harder == current) {
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600862 bch_time_stats_update(&c->try_harder_time, c->try_harder_start);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700863 c->try_harder = NULL;
Kent Overstreete8e1d462013-07-24 17:27:07 -0700864 wake_up(&c->try_wait);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700865 }
866}
867
Kent Overstreete8e1d462013-07-24 17:27:07 -0700868static struct btree *mca_alloc(struct cache_set *c, struct bkey *k, int level)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700869{
870 struct btree *b;
871
Kent Overstreete8e1d462013-07-24 17:27:07 -0700872 BUG_ON(current->bio_list);
873
Kent Overstreetcafe5632013-03-23 16:11:31 -0700874 lockdep_assert_held(&c->bucket_lock);
875
876 if (mca_find(c, k))
877 return NULL;
878
879 /* btree_free() doesn't free memory; it sticks the node on the end of
880 * the list. Check if there's any freed nodes there:
881 */
882 list_for_each_entry(b, &c->btree_cache_freeable, list)
Kent Overstreete8e1d462013-07-24 17:27:07 -0700883 if (!mca_reap(b, btree_order(k), false))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700884 goto out;
885
886 /* We never free struct btree itself, just the memory that holds the on
887 * disk node. Check the freed list before allocating a new one:
888 */
889 list_for_each_entry(b, &c->btree_cache_freed, list)
Kent Overstreete8e1d462013-07-24 17:27:07 -0700890 if (!mca_reap(b, 0, false)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700891 mca_data_alloc(b, k, __GFP_NOWARN|GFP_NOIO);
Kent Overstreeta85e9682013-12-20 17:28:16 -0800892 if (!b->keys.set[0].data)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700893 goto err;
894 else
895 goto out;
896 }
897
898 b = mca_bucket_alloc(c, k, __GFP_NOWARN|GFP_NOIO);
899 if (!b)
900 goto err;
901
902 BUG_ON(!down_write_trylock(&b->lock));
Kent Overstreeta85e9682013-12-20 17:28:16 -0800903 if (!b->keys.set->data)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700904 goto err;
905out:
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800906 BUG_ON(b->io_mutex.count != 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700907
908 bkey_copy(&b->key, k);
909 list_move(&b->list, &c->btree_cache);
910 hlist_del_init_rcu(&b->hash);
911 hlist_add_head_rcu(&b->hash, mca_hash(c, k));
912
913 lock_set_subclass(&b->lock.dep_map, level + 1, _THIS_IP_);
Kent Overstreetd6fd3b12013-07-24 17:20:19 -0700914 b->parent = (void *) ~0UL;
Kent Overstreeta85e9682013-12-20 17:28:16 -0800915 b->flags = 0;
916 b->written = 0;
917 b->level = level;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700918
Kent Overstreet65d45232013-12-20 17:22:05 -0800919 if (!b->level)
Kent Overstreeta85e9682013-12-20 17:28:16 -0800920 bch_btree_keys_init(&b->keys, &bch_extent_keys_ops,
921 &b->c->expensive_debug_checks);
Kent Overstreet65d45232013-12-20 17:22:05 -0800922 else
Kent Overstreeta85e9682013-12-20 17:28:16 -0800923 bch_btree_keys_init(&b->keys, &bch_btree_keys_ops,
924 &b->c->expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700925
926 return b;
927err:
928 if (b)
929 rw_unlock(true, b);
930
Kent Overstreete8e1d462013-07-24 17:27:07 -0700931 b = mca_cannibalize(c, k);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700932 if (!IS_ERR(b))
933 goto out;
934
935 return b;
936}
937
938/**
939 * bch_btree_node_get - find a btree node in the cache and lock it, reading it
940 * in from disk if necessary.
941 *
Kent Overstreetb54d6932013-07-24 18:04:18 -0700942 * If IO is necessary and running under generic_make_request, returns -EAGAIN.
Kent Overstreetcafe5632013-03-23 16:11:31 -0700943 *
944 * The btree node will have either a read or a write lock held, depending on
945 * level and op->lock.
946 */
947struct btree *bch_btree_node_get(struct cache_set *c, struct bkey *k,
Kent Overstreete8e1d462013-07-24 17:27:07 -0700948 int level, bool write)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700949{
950 int i = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700951 struct btree *b;
952
953 BUG_ON(level < 0);
954retry:
955 b = mca_find(c, k);
956
957 if (!b) {
Kent Overstreet57943512013-04-25 13:58:35 -0700958 if (current->bio_list)
959 return ERR_PTR(-EAGAIN);
960
Kent Overstreetcafe5632013-03-23 16:11:31 -0700961 mutex_lock(&c->bucket_lock);
Kent Overstreete8e1d462013-07-24 17:27:07 -0700962 b = mca_alloc(c, k, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700963 mutex_unlock(&c->bucket_lock);
964
965 if (!b)
966 goto retry;
967 if (IS_ERR(b))
968 return b;
969
Kent Overstreet57943512013-04-25 13:58:35 -0700970 bch_btree_node_read(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700971
972 if (!write)
973 downgrade_write(&b->lock);
974 } else {
975 rw_lock(write, b, level);
976 if (PTR_HASH(c, &b->key) != PTR_HASH(c, k)) {
977 rw_unlock(write, b);
978 goto retry;
979 }
980 BUG_ON(b->level != level);
981 }
982
983 b->accessed = 1;
984
Kent Overstreeta85e9682013-12-20 17:28:16 -0800985 for (; i <= b->keys.nsets && b->keys.set[i].size; i++) {
986 prefetch(b->keys.set[i].tree);
987 prefetch(b->keys.set[i].data);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700988 }
989
Kent Overstreeta85e9682013-12-20 17:28:16 -0800990 for (; i <= b->keys.nsets; i++)
991 prefetch(b->keys.set[i].data);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700992
Kent Overstreet57943512013-04-25 13:58:35 -0700993 if (btree_node_io_error(b)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700994 rw_unlock(write, b);
Kent Overstreet57943512013-04-25 13:58:35 -0700995 return ERR_PTR(-EIO);
996 }
997
998 BUG_ON(!b->written);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700999
1000 return b;
1001}
1002
1003static void btree_node_prefetch(struct cache_set *c, struct bkey *k, int level)
1004{
1005 struct btree *b;
1006
1007 mutex_lock(&c->bucket_lock);
Kent Overstreete8e1d462013-07-24 17:27:07 -07001008 b = mca_alloc(c, k, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001009 mutex_unlock(&c->bucket_lock);
1010
1011 if (!IS_ERR_OR_NULL(b)) {
Kent Overstreet57943512013-04-25 13:58:35 -07001012 bch_btree_node_read(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001013 rw_unlock(true, b);
1014 }
1015}
1016
1017/* Btree alloc */
1018
Kent Overstreete8e1d462013-07-24 17:27:07 -07001019static void btree_node_free(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001020{
1021 unsigned i;
1022
Kent Overstreetc37511b2013-04-26 15:39:55 -07001023 trace_bcache_btree_node_free(b);
1024
Kent Overstreetcafe5632013-03-23 16:11:31 -07001025 BUG_ON(b == b->c->root);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001026
1027 if (btree_node_dirty(b))
1028 btree_complete_write(b, btree_current_write(b));
1029 clear_bit(BTREE_NODE_dirty, &b->flags);
1030
Kent Overstreetcafe5632013-03-23 16:11:31 -07001031 cancel_delayed_work(&b->work);
1032
1033 mutex_lock(&b->c->bucket_lock);
1034
1035 for (i = 0; i < KEY_PTRS(&b->key); i++) {
1036 BUG_ON(atomic_read(&PTR_BUCKET(b->c, &b->key, i)->pin));
1037
1038 bch_inc_gen(PTR_CACHE(b->c, &b->key, i),
1039 PTR_BUCKET(b->c, &b->key, i));
1040 }
1041
1042 bch_bucket_free(b->c, &b->key);
1043 mca_bucket_free(b);
1044 mutex_unlock(&b->c->bucket_lock);
1045}
1046
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001047struct btree *bch_btree_node_alloc(struct cache_set *c, int level, bool wait)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001048{
1049 BKEY_PADDED(key) k;
1050 struct btree *b = ERR_PTR(-EAGAIN);
1051
1052 mutex_lock(&c->bucket_lock);
1053retry:
Kent Overstreet78365412013-12-17 01:29:34 -08001054 if (__bch_bucket_alloc_set(c, RESERVE_BTREE, &k.key, 1, wait))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001055 goto err;
1056
Kent Overstreet3a3b6a42013-07-24 16:46:42 -07001057 bkey_put(c, &k.key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001058 SET_KEY_SIZE(&k.key, c->btree_pages * PAGE_SECTORS);
1059
Kent Overstreete8e1d462013-07-24 17:27:07 -07001060 b = mca_alloc(c, &k.key, level);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001061 if (IS_ERR(b))
1062 goto err_free;
1063
1064 if (!b) {
Kent Overstreetb1a67b02013-03-25 11:46:44 -07001065 cache_bug(c,
1066 "Tried to allocate bucket that was in btree cache");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001067 goto retry;
1068 }
1069
Kent Overstreetcafe5632013-03-23 16:11:31 -07001070 b->accessed = 1;
Kent Overstreeta85e9682013-12-20 17:28:16 -08001071 bch_bset_init_next(&b->keys, b->keys.set->data, bset_magic(&b->c->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001072
1073 mutex_unlock(&c->bucket_lock);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001074
1075 trace_bcache_btree_node_alloc(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001076 return b;
1077err_free:
1078 bch_bucket_free(c, &k.key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001079err:
1080 mutex_unlock(&c->bucket_lock);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001081
1082 trace_bcache_btree_node_alloc_fail(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001083 return b;
1084}
1085
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001086static struct btree *btree_node_alloc_replacement(struct btree *b, bool wait)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001087{
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001088 struct btree *n = bch_btree_node_alloc(b->c, b->level, wait);
Kent Overstreet67539e82013-09-10 22:53:34 -07001089 if (!IS_ERR_OR_NULL(n)) {
1090 bch_btree_sort_into(b, n, &b->c->sort);
1091 bkey_copy_key(&n->key, &b->key);
1092 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001093
1094 return n;
1095}
1096
Kent Overstreet8835c122013-07-24 23:18:05 -07001097static void make_btree_freeing_key(struct btree *b, struct bkey *k)
1098{
1099 unsigned i;
1100
1101 bkey_copy(k, &b->key);
1102 bkey_copy_key(k, &ZERO_KEY);
1103
1104 for (i = 0; i < KEY_PTRS(k); i++) {
1105 uint8_t g = PTR_BUCKET(b->c, k, i)->gen + 1;
1106
1107 SET_PTR_GEN(k, i, g);
1108 }
1109
1110 atomic_inc(&b->c->prio_blocked);
1111}
1112
Kent Overstreet78365412013-12-17 01:29:34 -08001113static int btree_check_reserve(struct btree *b, struct btree_op *op)
1114{
1115 struct cache_set *c = b->c;
1116 struct cache *ca;
1117 unsigned i, reserve = c->root->level * 2 + 1;
1118 int ret = 0;
1119
1120 mutex_lock(&c->bucket_lock);
1121
1122 for_each_cache(ca, c, i)
1123 if (fifo_used(&ca->free[RESERVE_BTREE]) < reserve) {
1124 if (op)
1125 prepare_to_wait(&c->bucket_wait, &op->wait,
1126 TASK_UNINTERRUPTIBLE);
1127 ret = -EINTR;
1128 break;
1129 }
1130
1131 mutex_unlock(&c->bucket_lock);
1132 return ret;
1133}
1134
Kent Overstreetcafe5632013-03-23 16:11:31 -07001135/* Garbage collection */
1136
1137uint8_t __bch_btree_mark_key(struct cache_set *c, int level, struct bkey *k)
1138{
1139 uint8_t stale = 0;
1140 unsigned i;
1141 struct bucket *g;
1142
1143 /*
1144 * ptr_invalid() can't return true for the keys that mark btree nodes as
1145 * freed, but since ptr_bad() returns true we'll never actually use them
1146 * for anything and thus we don't want mark their pointers here
1147 */
1148 if (!bkey_cmp(k, &ZERO_KEY))
1149 return stale;
1150
1151 for (i = 0; i < KEY_PTRS(k); i++) {
1152 if (!ptr_available(c, k, i))
1153 continue;
1154
1155 g = PTR_BUCKET(c, k, i);
1156
1157 if (gen_after(g->gc_gen, PTR_GEN(k, i)))
1158 g->gc_gen = PTR_GEN(k, i);
1159
1160 if (ptr_stale(c, k, i)) {
1161 stale = max(stale, ptr_stale(c, k, i));
1162 continue;
1163 }
1164
1165 cache_bug_on(GC_MARK(g) &&
1166 (GC_MARK(g) == GC_MARK_METADATA) != (level != 0),
1167 c, "inconsistent ptrs: mark = %llu, level = %i",
1168 GC_MARK(g), level);
1169
1170 if (level)
1171 SET_GC_MARK(g, GC_MARK_METADATA);
1172 else if (KEY_DIRTY(k))
1173 SET_GC_MARK(g, GC_MARK_DIRTY);
1174
1175 /* guard against overflow */
1176 SET_GC_SECTORS_USED(g, min_t(unsigned,
1177 GC_SECTORS_USED(g) + KEY_SIZE(k),
1178 (1 << 14) - 1));
1179
1180 BUG_ON(!GC_SECTORS_USED(g));
1181 }
1182
1183 return stale;
1184}
1185
1186#define btree_mark_key(b, k) __bch_btree_mark_key(b->c, b->level, k)
1187
Kent Overstreeta1f03582013-09-10 19:07:00 -07001188static bool btree_gc_mark_node(struct btree *b, struct gc_stat *gc)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001189{
1190 uint8_t stale = 0;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001191 unsigned keys = 0, good_keys = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001192 struct bkey *k;
1193 struct btree_iter iter;
1194 struct bset_tree *t;
1195
1196 gc->nodes++;
1197
Kent Overstreetc052dd92013-11-11 17:35:24 -08001198 for_each_key_filter(&b->keys, k, &iter, bch_ptr_invalid) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001199 stale = max(stale, btree_mark_key(b, k));
Kent Overstreeta1f03582013-09-10 19:07:00 -07001200 keys++;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001201
Kent Overstreeta85e9682013-12-20 17:28:16 -08001202 if (bch_ptr_bad(&b->keys, k))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001203 continue;
1204
Kent Overstreetcafe5632013-03-23 16:11:31 -07001205 gc->key_bytes += bkey_u64s(k);
1206 gc->nkeys++;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001207 good_keys++;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001208
1209 gc->data += KEY_SIZE(k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001210 }
1211
Kent Overstreeta85e9682013-12-20 17:28:16 -08001212 for (t = b->keys.set; t <= &b->keys.set[b->keys.nsets]; t++)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001213 btree_bug_on(t->size &&
Kent Overstreeta85e9682013-12-20 17:28:16 -08001214 bset_written(&b->keys, t) &&
Kent Overstreetcafe5632013-03-23 16:11:31 -07001215 bkey_cmp(&b->key, &t->end) < 0,
1216 b, "found short btree key in gc");
1217
Kent Overstreeta1f03582013-09-10 19:07:00 -07001218 if (b->c->gc_always_rewrite)
1219 return true;
1220
1221 if (stale > 10)
1222 return true;
1223
1224 if ((keys - good_keys) * 2 > keys)
1225 return true;
1226
1227 return false;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001228}
1229
Kent Overstreeta1f03582013-09-10 19:07:00 -07001230#define GC_MERGE_NODES 4U
Kent Overstreetcafe5632013-03-23 16:11:31 -07001231
1232struct gc_merge_info {
1233 struct btree *b;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001234 unsigned keys;
1235};
1236
Kent Overstreeta1f03582013-09-10 19:07:00 -07001237static int bch_btree_insert_node(struct btree *, struct btree_op *,
1238 struct keylist *, atomic_t *, struct bkey *);
Kent Overstreetb54d6932013-07-24 18:04:18 -07001239
Kent Overstreeta1f03582013-09-10 19:07:00 -07001240static int btree_gc_coalesce(struct btree *b, struct btree_op *op,
1241 struct keylist *keylist, struct gc_stat *gc,
1242 struct gc_merge_info *r)
1243{
1244 unsigned i, nodes = 0, keys = 0, blocks;
1245 struct btree *new_nodes[GC_MERGE_NODES];
1246 struct closure cl;
1247 struct bkey *k;
1248
1249 memset(new_nodes, 0, sizeof(new_nodes));
Kent Overstreetb54d6932013-07-24 18:04:18 -07001250 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001251
Kent Overstreeta1f03582013-09-10 19:07:00 -07001252 while (nodes < GC_MERGE_NODES && !IS_ERR_OR_NULL(r[nodes].b))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001253 keys += r[nodes++].keys;
1254
1255 blocks = btree_default_blocks(b->c) * 2 / 3;
1256
1257 if (nodes < 2 ||
Kent Overstreeta85e9682013-12-20 17:28:16 -08001258 __set_blocks(b->keys.set[0].data, keys,
Kent Overstreetee811282013-12-17 23:49:49 -08001259 block_bytes(b->c)) > blocks * (nodes - 1))
Kent Overstreeta1f03582013-09-10 19:07:00 -07001260 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001261
Kent Overstreeta1f03582013-09-10 19:07:00 -07001262 for (i = 0; i < nodes; i++) {
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001263 new_nodes[i] = btree_node_alloc_replacement(r[i].b, false);
Kent Overstreeta1f03582013-09-10 19:07:00 -07001264 if (IS_ERR_OR_NULL(new_nodes[i]))
1265 goto out_nocoalesce;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001266 }
1267
1268 for (i = nodes - 1; i > 0; --i) {
Kent Overstreetee811282013-12-17 23:49:49 -08001269 struct bset *n1 = btree_bset_first(new_nodes[i]);
1270 struct bset *n2 = btree_bset_first(new_nodes[i - 1]);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001271 struct bkey *k, *last = NULL;
1272
1273 keys = 0;
1274
Kent Overstreeta1f03582013-09-10 19:07:00 -07001275 if (i > 1) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001276 for (k = n2->start;
Kent Overstreetfafff812013-12-17 21:56:21 -08001277 k < bset_bkey_last(n2);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001278 k = bkey_next(k)) {
1279 if (__set_blocks(n1, n1->keys + keys +
Kent Overstreetee811282013-12-17 23:49:49 -08001280 bkey_u64s(k),
1281 block_bytes(b->c)) > blocks)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001282 break;
1283
1284 last = k;
1285 keys += bkey_u64s(k);
1286 }
Kent Overstreeta1f03582013-09-10 19:07:00 -07001287 } else {
1288 /*
1289 * Last node we're not getting rid of - we're getting
1290 * rid of the node at r[0]. Have to try and fit all of
1291 * the remaining keys into this node; we can't ensure
1292 * they will always fit due to rounding and variable
1293 * length keys (shouldn't be possible in practice,
1294 * though)
1295 */
1296 if (__set_blocks(n1, n1->keys + n2->keys,
Kent Overstreetee811282013-12-17 23:49:49 -08001297 block_bytes(b->c)) >
1298 btree_blocks(new_nodes[i]))
Kent Overstreeta1f03582013-09-10 19:07:00 -07001299 goto out_nocoalesce;
1300
1301 keys = n2->keys;
1302 /* Take the key of the node we're getting rid of */
1303 last = &r->b->key;
1304 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001305
Kent Overstreetee811282013-12-17 23:49:49 -08001306 BUG_ON(__set_blocks(n1, n1->keys + keys, block_bytes(b->c)) >
1307 btree_blocks(new_nodes[i]));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001308
Kent Overstreeta1f03582013-09-10 19:07:00 -07001309 if (last)
1310 bkey_copy_key(&new_nodes[i]->key, last);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001311
Kent Overstreetfafff812013-12-17 21:56:21 -08001312 memcpy(bset_bkey_last(n1),
Kent Overstreetcafe5632013-03-23 16:11:31 -07001313 n2->start,
Kent Overstreetfafff812013-12-17 21:56:21 -08001314 (void *) bset_bkey_idx(n2, keys) - (void *) n2->start);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001315
1316 n1->keys += keys;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001317 r[i].keys = n1->keys;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001318
1319 memmove(n2->start,
Kent Overstreetfafff812013-12-17 21:56:21 -08001320 bset_bkey_idx(n2, keys),
1321 (void *) bset_bkey_last(n2) -
1322 (void *) bset_bkey_idx(n2, keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001323
1324 n2->keys -= keys;
1325
Kent Overstreet085d2a32013-11-11 18:20:51 -08001326 if (__bch_keylist_realloc(keylist,
1327 bkey_u64s(&new_nodes[i]->key)))
Kent Overstreeta1f03582013-09-10 19:07:00 -07001328 goto out_nocoalesce;
1329
1330 bch_btree_node_write(new_nodes[i], &cl);
1331 bch_keylist_add(keylist, &new_nodes[i]->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001332 }
1333
Kent Overstreeta1f03582013-09-10 19:07:00 -07001334 for (i = 0; i < nodes; i++) {
Kent Overstreet085d2a32013-11-11 18:20:51 -08001335 if (__bch_keylist_realloc(keylist, bkey_u64s(&r[i].b->key)))
Kent Overstreeta1f03582013-09-10 19:07:00 -07001336 goto out_nocoalesce;
1337
1338 make_btree_freeing_key(r[i].b, keylist->top);
1339 bch_keylist_push(keylist);
1340 }
1341
1342 /* We emptied out this node */
Kent Overstreetee811282013-12-17 23:49:49 -08001343 BUG_ON(btree_bset_first(new_nodes[0])->keys);
Kent Overstreeta1f03582013-09-10 19:07:00 -07001344 btree_node_free(new_nodes[0]);
1345 rw_unlock(true, new_nodes[0]);
1346
1347 closure_sync(&cl);
1348
1349 for (i = 0; i < nodes; i++) {
1350 btree_node_free(r[i].b);
1351 rw_unlock(true, r[i].b);
1352
1353 r[i].b = new_nodes[i];
1354 }
1355
1356 bch_btree_insert_node(b, op, keylist, NULL, NULL);
1357 BUG_ON(!bch_keylist_empty(keylist));
1358
1359 memmove(r, r + 1, sizeof(r[0]) * (nodes - 1));
1360 r[nodes - 1].b = ERR_PTR(-EINTR);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001361
Kent Overstreetc37511b2013-04-26 15:39:55 -07001362 trace_bcache_btree_gc_coalesce(nodes);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001363 gc->nodes--;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001364
Kent Overstreeta1f03582013-09-10 19:07:00 -07001365 /* Invalidated our iterator */
1366 return -EINTR;
1367
1368out_nocoalesce:
1369 closure_sync(&cl);
1370
1371 while ((k = bch_keylist_pop(keylist)))
1372 if (!bkey_cmp(k, &ZERO_KEY))
1373 atomic_dec(&b->c->prio_blocked);
1374
1375 for (i = 0; i < nodes; i++)
1376 if (!IS_ERR_OR_NULL(new_nodes[i])) {
1377 btree_node_free(new_nodes[i]);
1378 rw_unlock(true, new_nodes[i]);
1379 }
1380 return 0;
1381}
1382
1383static unsigned btree_gc_count_keys(struct btree *b)
1384{
1385 struct bkey *k;
1386 struct btree_iter iter;
1387 unsigned ret = 0;
1388
Kent Overstreetc052dd92013-11-11 17:35:24 -08001389 for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad)
Kent Overstreeta1f03582013-09-10 19:07:00 -07001390 ret += bkey_u64s(k);
1391
1392 return ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001393}
1394
1395static int btree_gc_recurse(struct btree *b, struct btree_op *op,
1396 struct closure *writes, struct gc_stat *gc)
1397{
Kent Overstreetcafe5632013-03-23 16:11:31 -07001398 unsigned i;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001399 int ret = 0;
1400 bool should_rewrite;
1401 struct btree *n;
1402 struct bkey *k;
1403 struct keylist keys;
1404 struct btree_iter iter;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001405 struct gc_merge_info r[GC_MERGE_NODES];
Kent Overstreeta1f03582013-09-10 19:07:00 -07001406 struct gc_merge_info *last = r + GC_MERGE_NODES - 1;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001407
Kent Overstreeta1f03582013-09-10 19:07:00 -07001408 bch_keylist_init(&keys);
Kent Overstreetc052dd92013-11-11 17:35:24 -08001409 bch_btree_iter_init(&b->keys, &iter, &b->c->gc_done);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001410
Kent Overstreeta1f03582013-09-10 19:07:00 -07001411 for (i = 0; i < GC_MERGE_NODES; i++)
1412 r[i].b = ERR_PTR(-EINTR);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001413
Kent Overstreeta1f03582013-09-10 19:07:00 -07001414 while (1) {
Kent Overstreeta85e9682013-12-20 17:28:16 -08001415 k = bch_btree_iter_next_filter(&iter, &b->keys, bch_ptr_bad);
Kent Overstreeta1f03582013-09-10 19:07:00 -07001416 if (k) {
1417 r->b = bch_btree_node_get(b->c, k, b->level - 1, true);
1418 if (IS_ERR(r->b)) {
1419 ret = PTR_ERR(r->b);
1420 break;
1421 }
1422
1423 r->keys = btree_gc_count_keys(r->b);
1424
1425 ret = btree_gc_coalesce(b, op, &keys, gc, r);
1426 if (ret)
1427 break;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001428 }
1429
Kent Overstreeta1f03582013-09-10 19:07:00 -07001430 if (!last->b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001431 break;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001432
1433 if (!IS_ERR(last->b)) {
1434 should_rewrite = btree_gc_mark_node(last->b, gc);
Kent Overstreet78365412013-12-17 01:29:34 -08001435 if (should_rewrite &&
1436 !btree_check_reserve(b, NULL)) {
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001437 n = btree_node_alloc_replacement(last->b,
1438 false);
Kent Overstreeta1f03582013-09-10 19:07:00 -07001439
1440 if (!IS_ERR_OR_NULL(n)) {
1441 bch_btree_node_write_sync(n);
1442 bch_keylist_add(&keys, &n->key);
1443
1444 make_btree_freeing_key(last->b,
1445 keys.top);
1446 bch_keylist_push(&keys);
1447
1448 btree_node_free(last->b);
1449
1450 bch_btree_insert_node(b, op, &keys,
1451 NULL, NULL);
1452 BUG_ON(!bch_keylist_empty(&keys));
1453
1454 rw_unlock(true, last->b);
1455 last->b = n;
1456
1457 /* Invalidated our iterator */
1458 ret = -EINTR;
1459 break;
1460 }
1461 }
1462
1463 if (last->b->level) {
1464 ret = btree_gc_recurse(last->b, op, writes, gc);
1465 if (ret)
1466 break;
1467 }
1468
1469 bkey_copy_key(&b->c->gc_done, &last->b->key);
1470
1471 /*
1472 * Must flush leaf nodes before gc ends, since replace
1473 * operations aren't journalled
1474 */
1475 if (btree_node_dirty(last->b))
1476 bch_btree_node_write(last->b, writes);
1477 rw_unlock(true, last->b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001478 }
1479
Kent Overstreeta1f03582013-09-10 19:07:00 -07001480 memmove(r + 1, r, sizeof(r[0]) * (GC_MERGE_NODES - 1));
1481 r->b = NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001482
Kent Overstreetcafe5632013-03-23 16:11:31 -07001483 if (need_resched()) {
1484 ret = -EAGAIN;
1485 break;
1486 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001487 }
1488
Kent Overstreeta1f03582013-09-10 19:07:00 -07001489 for (i = 0; i < GC_MERGE_NODES; i++)
1490 if (!IS_ERR_OR_NULL(r[i].b)) {
1491 if (btree_node_dirty(r[i].b))
1492 bch_btree_node_write(r[i].b, writes);
1493 rw_unlock(true, r[i].b);
1494 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001495
Kent Overstreeta1f03582013-09-10 19:07:00 -07001496 bch_keylist_free(&keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001497
1498 return ret;
1499}
1500
1501static int bch_btree_gc_root(struct btree *b, struct btree_op *op,
1502 struct closure *writes, struct gc_stat *gc)
1503{
1504 struct btree *n = NULL;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001505 int ret = 0;
1506 bool should_rewrite;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001507
Kent Overstreeta1f03582013-09-10 19:07:00 -07001508 should_rewrite = btree_gc_mark_node(b, gc);
1509 if (should_rewrite) {
Kent Overstreetbc9389e2013-09-10 19:07:35 -07001510 n = btree_node_alloc_replacement(b, false);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001511
Kent Overstreeta1f03582013-09-10 19:07:00 -07001512 if (!IS_ERR_OR_NULL(n)) {
1513 bch_btree_node_write_sync(n);
1514 bch_btree_set_root(n);
1515 btree_node_free(b);
1516 rw_unlock(true, n);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001517
Kent Overstreeta1f03582013-09-10 19:07:00 -07001518 return -EINTR;
1519 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001520 }
1521
Kent Overstreeta1f03582013-09-10 19:07:00 -07001522 if (b->level) {
1523 ret = btree_gc_recurse(b, op, writes, gc);
1524 if (ret)
1525 return ret;
1526 }
1527
1528 bkey_copy_key(&b->c->gc_done, &b->key);
1529
Kent Overstreetcafe5632013-03-23 16:11:31 -07001530 return ret;
1531}
1532
1533static void btree_gc_start(struct cache_set *c)
1534{
1535 struct cache *ca;
1536 struct bucket *b;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001537 unsigned i;
1538
1539 if (!c->gc_mark_valid)
1540 return;
1541
1542 mutex_lock(&c->bucket_lock);
1543
1544 c->gc_mark_valid = 0;
1545 c->gc_done = ZERO_KEY;
1546
1547 for_each_cache(ca, c, i)
1548 for_each_bucket(b, ca) {
1549 b->gc_gen = b->gen;
Kent Overstreet29ebf462013-07-11 19:43:21 -07001550 if (!atomic_read(&b->pin)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001551 SET_GC_MARK(b, GC_MARK_RECLAIMABLE);
Kent Overstreet29ebf462013-07-11 19:43:21 -07001552 SET_GC_SECTORS_USED(b, 0);
1553 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001554 }
1555
Kent Overstreetcafe5632013-03-23 16:11:31 -07001556 mutex_unlock(&c->bucket_lock);
1557}
1558
1559size_t bch_btree_gc_finish(struct cache_set *c)
1560{
1561 size_t available = 0;
1562 struct bucket *b;
1563 struct cache *ca;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001564 unsigned i;
1565
1566 mutex_lock(&c->bucket_lock);
1567
1568 set_gc_sectors(c);
1569 c->gc_mark_valid = 1;
1570 c->need_gc = 0;
1571
1572 if (c->root)
1573 for (i = 0; i < KEY_PTRS(&c->root->key); i++)
1574 SET_GC_MARK(PTR_BUCKET(c, &c->root->key, i),
1575 GC_MARK_METADATA);
1576
1577 for (i = 0; i < KEY_PTRS(&c->uuid_bucket); i++)
1578 SET_GC_MARK(PTR_BUCKET(c, &c->uuid_bucket, i),
1579 GC_MARK_METADATA);
1580
Nicholas Swensonbf0a6282013-11-26 19:14:23 -08001581 /* don't reclaim buckets to which writeback keys point */
1582 rcu_read_lock();
1583 for (i = 0; i < c->nr_uuids; i++) {
1584 struct bcache_device *d = c->devices[i];
1585 struct cached_dev *dc;
1586 struct keybuf_key *w, *n;
1587 unsigned j;
1588
1589 if (!d || UUID_FLASH_ONLY(&c->uuids[i]))
1590 continue;
1591 dc = container_of(d, struct cached_dev, disk);
1592
1593 spin_lock(&dc->writeback_keys.lock);
1594 rbtree_postorder_for_each_entry_safe(w, n,
1595 &dc->writeback_keys.keys, node)
1596 for (j = 0; j < KEY_PTRS(&w->key); j++)
1597 SET_GC_MARK(PTR_BUCKET(c, &w->key, j),
1598 GC_MARK_DIRTY);
1599 spin_unlock(&dc->writeback_keys.lock);
1600 }
1601 rcu_read_unlock();
1602
Kent Overstreetcafe5632013-03-23 16:11:31 -07001603 for_each_cache(ca, c, i) {
1604 uint64_t *i;
1605
1606 ca->invalidate_needs_gc = 0;
1607
1608 for (i = ca->sb.d; i < ca->sb.d + ca->sb.keys; i++)
1609 SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA);
1610
1611 for (i = ca->prio_buckets;
1612 i < ca->prio_buckets + prio_buckets(ca) * 2; i++)
1613 SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA);
1614
1615 for_each_bucket(b, ca) {
1616 b->last_gc = b->gc_gen;
1617 c->need_gc = max(c->need_gc, bucket_gc_gen(b));
1618
1619 if (!atomic_read(&b->pin) &&
1620 GC_MARK(b) == GC_MARK_RECLAIMABLE) {
1621 available++;
1622 if (!GC_SECTORS_USED(b))
1623 bch_bucket_add_unused(ca, b);
1624 }
1625 }
1626 }
1627
Kent Overstreetcafe5632013-03-23 16:11:31 -07001628 mutex_unlock(&c->bucket_lock);
1629 return available;
1630}
1631
Kent Overstreet72a44512013-10-24 17:19:26 -07001632static void bch_btree_gc(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001633{
Kent Overstreetcafe5632013-03-23 16:11:31 -07001634 int ret;
1635 unsigned long available;
1636 struct gc_stat stats;
1637 struct closure writes;
1638 struct btree_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001639 uint64_t start_time = local_clock();
Kent Overstreet57943512013-04-25 13:58:35 -07001640
Kent Overstreetc37511b2013-04-26 15:39:55 -07001641 trace_bcache_gc_start(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001642
1643 memset(&stats, 0, sizeof(struct gc_stat));
1644 closure_init_stack(&writes);
Kent Overstreetb54d6932013-07-24 18:04:18 -07001645 bch_btree_op_init(&op, SHRT_MAX);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001646
1647 btree_gc_start(c);
1648
Kent Overstreeta1f03582013-09-10 19:07:00 -07001649 do {
1650 ret = btree_root(gc_root, c, &op, &writes, &stats);
1651 closure_sync(&writes);
Kent Overstreet57943512013-04-25 13:58:35 -07001652
Kent Overstreeta1f03582013-09-10 19:07:00 -07001653 if (ret && ret != -EAGAIN)
1654 pr_warn("gc failed!");
1655 } while (ret);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001656
1657 available = bch_btree_gc_finish(c);
Kent Overstreet57943512013-04-25 13:58:35 -07001658 wake_up_allocators(c);
1659
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001660 bch_time_stats_update(&c->btree_gc_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001661
1662 stats.key_bytes *= sizeof(uint64_t);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001663 stats.data <<= 9;
1664 stats.in_use = (c->nbuckets - available) * 100 / c->nbuckets;
1665 memcpy(&c->gc_stats, &stats, sizeof(struct gc_stat));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001666
Kent Overstreetc37511b2013-04-26 15:39:55 -07001667 trace_bcache_gc_end(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001668
Kent Overstreet72a44512013-10-24 17:19:26 -07001669 bch_moving_gc(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001670}
1671
Kent Overstreet72a44512013-10-24 17:19:26 -07001672static int bch_gc_thread(void *arg)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001673{
Kent Overstreet72a44512013-10-24 17:19:26 -07001674 struct cache_set *c = arg;
Kent Overstreeta1f03582013-09-10 19:07:00 -07001675 struct cache *ca;
1676 unsigned i;
Kent Overstreet72a44512013-10-24 17:19:26 -07001677
1678 while (1) {
Kent Overstreeta1f03582013-09-10 19:07:00 -07001679again:
Kent Overstreet72a44512013-10-24 17:19:26 -07001680 bch_btree_gc(c);
1681
1682 set_current_state(TASK_INTERRUPTIBLE);
1683 if (kthread_should_stop())
1684 break;
1685
Kent Overstreeta1f03582013-09-10 19:07:00 -07001686 mutex_lock(&c->bucket_lock);
1687
1688 for_each_cache(ca, c, i)
1689 if (ca->invalidate_needs_gc) {
1690 mutex_unlock(&c->bucket_lock);
1691 set_current_state(TASK_RUNNING);
1692 goto again;
1693 }
1694
1695 mutex_unlock(&c->bucket_lock);
1696
Kent Overstreet72a44512013-10-24 17:19:26 -07001697 try_to_freeze();
1698 schedule();
1699 }
1700
1701 return 0;
1702}
1703
1704int bch_gc_thread_start(struct cache_set *c)
1705{
1706 c->gc_thread = kthread_create(bch_gc_thread, c, "bcache_gc");
1707 if (IS_ERR(c->gc_thread))
1708 return PTR_ERR(c->gc_thread);
1709
1710 set_task_state(c->gc_thread, TASK_INTERRUPTIBLE);
1711 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001712}
1713
1714/* Initial partial gc */
1715
1716static int bch_btree_check_recurse(struct btree *b, struct btree_op *op,
1717 unsigned long **seen)
1718{
Kent Overstreet50310162013-09-10 17:18:59 -07001719 int ret = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001720 unsigned i;
Kent Overstreet50310162013-09-10 17:18:59 -07001721 struct bkey *k, *p = NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001722 struct bucket *g;
1723 struct btree_iter iter;
1724
Kent Overstreetc052dd92013-11-11 17:35:24 -08001725 for_each_key_filter(&b->keys, k, &iter, bch_ptr_invalid) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001726 for (i = 0; i < KEY_PTRS(k); i++) {
1727 if (!ptr_available(b->c, k, i))
1728 continue;
1729
1730 g = PTR_BUCKET(b->c, k, i);
1731
1732 if (!__test_and_set_bit(PTR_BUCKET_NR(b->c, k, i),
1733 seen[PTR_DEV(k, i)]) ||
1734 !ptr_stale(b->c, k, i)) {
1735 g->gen = PTR_GEN(k, i);
1736
1737 if (b->level)
1738 g->prio = BTREE_PRIO;
1739 else if (g->prio == BTREE_PRIO)
1740 g->prio = INITIAL_PRIO;
1741 }
1742 }
1743
1744 btree_mark_key(b, k);
1745 }
1746
1747 if (b->level) {
Kent Overstreetc052dd92013-11-11 17:35:24 -08001748 bch_btree_iter_init(&b->keys, &iter, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001749
Kent Overstreet50310162013-09-10 17:18:59 -07001750 do {
Kent Overstreeta85e9682013-12-20 17:28:16 -08001751 k = bch_btree_iter_next_filter(&iter, &b->keys,
1752 bch_ptr_bad);
Kent Overstreet50310162013-09-10 17:18:59 -07001753 if (k)
1754 btree_node_prefetch(b->c, k, b->level - 1);
1755
Kent Overstreetcafe5632013-03-23 16:11:31 -07001756 if (p)
Kent Overstreet50310162013-09-10 17:18:59 -07001757 ret = btree(check_recurse, p, b, op, seen);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001758
Kent Overstreet50310162013-09-10 17:18:59 -07001759 p = k;
1760 } while (p && !ret);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001761 }
1762
1763 return 0;
1764}
1765
Kent Overstreetc18536a2013-07-24 17:44:17 -07001766int bch_btree_check(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001767{
1768 int ret = -ENOMEM;
1769 unsigned i;
1770 unsigned long *seen[MAX_CACHES_PER_SET];
Kent Overstreetc18536a2013-07-24 17:44:17 -07001771 struct btree_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001772
1773 memset(seen, 0, sizeof(seen));
Kent Overstreetb54d6932013-07-24 18:04:18 -07001774 bch_btree_op_init(&op, SHRT_MAX);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001775
1776 for (i = 0; c->cache[i]; i++) {
1777 size_t n = DIV_ROUND_UP(c->cache[i]->sb.nbuckets, 8);
1778 seen[i] = kmalloc(n, GFP_KERNEL);
1779 if (!seen[i])
1780 goto err;
1781
1782 /* Disables the seen array until prio_read() uses it too */
1783 memset(seen[i], 0xFF, n);
1784 }
1785
Kent Overstreetc18536a2013-07-24 17:44:17 -07001786 ret = btree_root(check_recurse, c, &op, seen);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001787err:
1788 for (i = 0; i < MAX_CACHES_PER_SET; i++)
1789 kfree(seen[i]);
1790 return ret;
1791}
1792
1793/* Btree insertion */
1794
Kent Overstreet1b207d82013-09-10 18:52:54 -07001795static bool fix_overlapping_extents(struct btree *b, struct bkey *insert,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001796 struct btree_iter *iter,
Kent Overstreet1b207d82013-09-10 18:52:54 -07001797 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001798{
Kent Overstreet279afba2013-06-05 06:21:07 -07001799 void subtract_dirty(struct bkey *k, uint64_t offset, int sectors)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001800 {
Kent Overstreet279afba2013-06-05 06:21:07 -07001801 if (KEY_DIRTY(k))
1802 bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k),
1803 offset, -sectors);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001804 }
1805
Kent Overstreet279afba2013-06-05 06:21:07 -07001806 uint64_t old_offset;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001807 unsigned old_size, sectors_found = 0;
1808
1809 while (1) {
1810 struct bkey *k = bch_btree_iter_next(iter);
Kent Overstreet911c9612013-07-28 18:35:09 -07001811 if (!k)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001812 break;
1813
Kent Overstreet911c9612013-07-28 18:35:09 -07001814 if (bkey_cmp(&START_KEY(k), insert) >= 0) {
1815 if (KEY_SIZE(k))
1816 break;
1817 else
1818 continue;
1819 }
1820
Kent Overstreetcafe5632013-03-23 16:11:31 -07001821 if (bkey_cmp(k, &START_KEY(insert)) <= 0)
1822 continue;
1823
Kent Overstreet279afba2013-06-05 06:21:07 -07001824 old_offset = KEY_START(k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001825 old_size = KEY_SIZE(k);
1826
1827 /*
1828 * We might overlap with 0 size extents; we can't skip these
1829 * because if they're in the set we're inserting to we have to
1830 * adjust them so they don't overlap with the key we're
Kent Overstreet1b207d82013-09-10 18:52:54 -07001831 * inserting. But we don't want to check them for replace
Kent Overstreetcafe5632013-03-23 16:11:31 -07001832 * operations.
1833 */
1834
Kent Overstreet1b207d82013-09-10 18:52:54 -07001835 if (replace_key && KEY_SIZE(k)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001836 /*
1837 * k might have been split since we inserted/found the
1838 * key we're replacing
1839 */
1840 unsigned i;
1841 uint64_t offset = KEY_START(k) -
Kent Overstreet1b207d82013-09-10 18:52:54 -07001842 KEY_START(replace_key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001843
1844 /* But it must be a subset of the replace key */
Kent Overstreet1b207d82013-09-10 18:52:54 -07001845 if (KEY_START(k) < KEY_START(replace_key) ||
1846 KEY_OFFSET(k) > KEY_OFFSET(replace_key))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001847 goto check_failed;
1848
1849 /* We didn't find a key that we were supposed to */
1850 if (KEY_START(k) > KEY_START(insert) + sectors_found)
1851 goto check_failed;
1852
Kent Overstreetd24a6e12013-11-10 21:55:27 -08001853 if (KEY_PTRS(k) != KEY_PTRS(replace_key) ||
1854 KEY_DIRTY(k) != KEY_DIRTY(replace_key))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001855 goto check_failed;
1856
1857 /* skip past gen */
1858 offset <<= 8;
1859
Kent Overstreet1b207d82013-09-10 18:52:54 -07001860 BUG_ON(!KEY_PTRS(replace_key));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001861
Kent Overstreet1b207d82013-09-10 18:52:54 -07001862 for (i = 0; i < KEY_PTRS(replace_key); i++)
1863 if (k->ptr[i] != replace_key->ptr[i] + offset)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001864 goto check_failed;
1865
1866 sectors_found = KEY_OFFSET(k) - KEY_START(insert);
1867 }
1868
1869 if (bkey_cmp(insert, k) < 0 &&
1870 bkey_cmp(&START_KEY(insert), &START_KEY(k)) > 0) {
1871 /*
1872 * We overlapped in the middle of an existing key: that
1873 * means we have to split the old key. But we have to do
1874 * slightly different things depending on whether the
1875 * old key has been written out yet.
1876 */
1877
1878 struct bkey *top;
1879
Kent Overstreet279afba2013-06-05 06:21:07 -07001880 subtract_dirty(k, KEY_START(insert), KEY_SIZE(insert));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001881
Kent Overstreeta85e9682013-12-20 17:28:16 -08001882 if (bkey_written(&b->keys, k)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001883 /*
1884 * We insert a new key to cover the top of the
1885 * old key, and the old key is modified in place
1886 * to represent the bottom split.
1887 *
1888 * It's completely arbitrary whether the new key
1889 * is the top or the bottom, but it has to match
1890 * up with what btree_sort_fixup() does - it
1891 * doesn't check for this kind of overlap, it
1892 * depends on us inserting a new key for the top
1893 * here.
1894 */
Kent Overstreetc052dd92013-11-11 17:35:24 -08001895 top = bch_bset_search(&b->keys,
Kent Overstreeta85e9682013-12-20 17:28:16 -08001896 bset_tree_last(&b->keys),
Kent Overstreetcafe5632013-03-23 16:11:31 -07001897 insert);
Kent Overstreeta85e9682013-12-20 17:28:16 -08001898 bch_bset_insert(&b->keys, top, k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001899 } else {
1900 BKEY_PADDED(key) temp;
1901 bkey_copy(&temp.key, k);
Kent Overstreeta85e9682013-12-20 17:28:16 -08001902 bch_bset_insert(&b->keys, k, &temp.key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001903 top = bkey_next(k);
1904 }
1905
1906 bch_cut_front(insert, top);
1907 bch_cut_back(&START_KEY(insert), k);
Kent Overstreeta85e9682013-12-20 17:28:16 -08001908 bch_bset_fix_invalidated_key(&b->keys, k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001909 return false;
1910 }
1911
1912 if (bkey_cmp(insert, k) < 0) {
1913 bch_cut_front(insert, k);
1914 } else {
Kent Overstreet1fa84552013-11-10 21:55:27 -08001915 if (bkey_cmp(&START_KEY(insert), &START_KEY(k)) > 0)
1916 old_offset = KEY_START(insert);
1917
Kent Overstreeta85e9682013-12-20 17:28:16 -08001918 if (bkey_written(&b->keys, k) &&
Kent Overstreetcafe5632013-03-23 16:11:31 -07001919 bkey_cmp(&START_KEY(insert), &START_KEY(k)) <= 0) {
1920 /*
1921 * Completely overwrote, so we don't have to
1922 * invalidate the binary search tree
1923 */
1924 bch_cut_front(k, k);
1925 } else {
1926 __bch_cut_back(&START_KEY(insert), k);
Kent Overstreeta85e9682013-12-20 17:28:16 -08001927 bch_bset_fix_invalidated_key(&b->keys, k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001928 }
1929 }
1930
Kent Overstreet279afba2013-06-05 06:21:07 -07001931 subtract_dirty(k, old_offset, old_size - KEY_SIZE(k));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001932 }
1933
1934check_failed:
Kent Overstreet1b207d82013-09-10 18:52:54 -07001935 if (replace_key) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001936 if (!sectors_found) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001937 return true;
1938 } else if (sectors_found < KEY_SIZE(insert)) {
1939 SET_KEY_OFFSET(insert, KEY_OFFSET(insert) -
1940 (KEY_SIZE(insert) - sectors_found));
1941 SET_KEY_SIZE(insert, sectors_found);
1942 }
1943 }
1944
1945 return false;
1946}
1947
1948static bool btree_insert_key(struct btree *b, struct btree_op *op,
Kent Overstreet1b207d82013-09-10 18:52:54 -07001949 struct bkey *k, struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001950{
Kent Overstreetee811282013-12-17 23:49:49 -08001951 struct bset *i = btree_bset_last(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001952 struct bkey *m, *prev;
Kent Overstreet85b14922013-05-14 20:33:16 -07001953 unsigned status = BTREE_INSERT_STATUS_INSERT;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001954
1955 BUG_ON(bkey_cmp(k, &b->key) > 0);
1956 BUG_ON(b->level && !KEY_PTRS(k));
1957 BUG_ON(!b->level && !KEY_OFFSET(k));
1958
1959 if (!b->level) {
1960 struct btree_iter iter;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001961
1962 /*
1963 * bset_search() returns the first key that is strictly greater
1964 * than the search key - but for back merging, we want to find
Kent Overstreet0eacac22013-07-01 19:29:05 -07001965 * the previous key.
Kent Overstreetcafe5632013-03-23 16:11:31 -07001966 */
Kent Overstreetcafe5632013-03-23 16:11:31 -07001967 prev = NULL;
Kent Overstreetc052dd92013-11-11 17:35:24 -08001968 m = bch_btree_iter_init(&b->keys, &iter,
Kent Overstreeta85e9682013-12-20 17:28:16 -08001969 PRECEDING_KEY(&START_KEY(k)));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001970
Kent Overstreet1b207d82013-09-10 18:52:54 -07001971 if (fix_overlapping_extents(b, k, &iter, replace_key)) {
1972 op->insert_collision = true;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001973 return false;
Kent Overstreet1b207d82013-09-10 18:52:54 -07001974 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001975
Kent Overstreet1fa84552013-11-10 21:55:27 -08001976 if (KEY_DIRTY(k))
1977 bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k),
1978 KEY_START(k), KEY_SIZE(k));
1979
Kent Overstreetfafff812013-12-17 21:56:21 -08001980 while (m != bset_bkey_last(i) &&
Kent Overstreetcafe5632013-03-23 16:11:31 -07001981 bkey_cmp(k, &START_KEY(m)) > 0)
1982 prev = m, m = bkey_next(m);
1983
1984 if (key_merging_disabled(b->c))
1985 goto insert;
1986
1987 /* prev is in the tree, if we merge we're done */
Kent Overstreet85b14922013-05-14 20:33:16 -07001988 status = BTREE_INSERT_STATUS_BACK_MERGE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001989 if (prev &&
Kent Overstreeta85e9682013-12-20 17:28:16 -08001990 bch_bkey_try_merge(&b->keys, prev, k))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001991 goto merged;
1992
Kent Overstreet85b14922013-05-14 20:33:16 -07001993 status = BTREE_INSERT_STATUS_OVERWROTE;
Kent Overstreetfafff812013-12-17 21:56:21 -08001994 if (m != bset_bkey_last(i) &&
Kent Overstreetcafe5632013-03-23 16:11:31 -07001995 KEY_PTRS(m) == KEY_PTRS(k) && !KEY_SIZE(m))
1996 goto copy;
1997
Kent Overstreet85b14922013-05-14 20:33:16 -07001998 status = BTREE_INSERT_STATUS_FRONT_MERGE;
Kent Overstreetfafff812013-12-17 21:56:21 -08001999 if (m != bset_bkey_last(i) &&
Kent Overstreeta85e9682013-12-20 17:28:16 -08002000 bch_bkey_try_merge(&b->keys, k, m))
Kent Overstreetcafe5632013-03-23 16:11:31 -07002001 goto copy;
Kent Overstreet1b207d82013-09-10 18:52:54 -07002002 } else {
2003 BUG_ON(replace_key);
Kent Overstreetc052dd92013-11-11 17:35:24 -08002004 m = bch_bset_search(&b->keys, bset_tree_last(&b->keys), k);
Kent Overstreet1b207d82013-09-10 18:52:54 -07002005 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07002006
Kent Overstreeta85e9682013-12-20 17:28:16 -08002007insert: bch_bset_insert(&b->keys, m, k);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002008copy: bkey_copy(m, k);
2009merged:
Kent Overstreet1b207d82013-09-10 18:52:54 -07002010 bch_check_keys(b, "%u for %s", status,
2011 replace_key ? "replace" : "insert");
Kent Overstreetcafe5632013-03-23 16:11:31 -07002012
2013 if (b->level && !KEY_OFFSET(k))
Kent Overstreet57943512013-04-25 13:58:35 -07002014 btree_current_write(b)->prio_blocked++;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002015
Kent Overstreet1b207d82013-09-10 18:52:54 -07002016 trace_bcache_btree_insert_key(b, k, replace_key != NULL, status);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002017
2018 return true;
2019}
2020
Kent Overstreet59158fd2013-11-11 19:03:54 -08002021static size_t insert_u64s_remaining(struct btree *b)
2022{
2023 ssize_t ret = bch_btree_keys_u64s_remaining(&b->keys);
2024
2025 /*
2026 * Might land in the middle of an existing extent and have to split it
2027 */
2028 if (b->keys.ops->is_extents)
2029 ret -= KEY_MAX_U64S;
2030
2031 return max(ret, 0L);
2032}
2033
Kent Overstreet26c949f2013-09-10 18:41:15 -07002034static bool bch_btree_insert_keys(struct btree *b, struct btree_op *op,
Kent Overstreet1b207d82013-09-10 18:52:54 -07002035 struct keylist *insert_keys,
2036 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002037{
2038 bool ret = false;
Kent Overstreet280481d2013-10-24 16:36:03 -07002039 int oldsize = bch_count_data(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002040
Kent Overstreet26c949f2013-09-10 18:41:15 -07002041 while (!bch_keylist_empty(insert_keys)) {
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07002042 struct bkey *k = insert_keys->keys;
Kent Overstreet26c949f2013-09-10 18:41:15 -07002043
Kent Overstreet59158fd2013-11-11 19:03:54 -08002044 if (bkey_u64s(k) > insert_u64s_remaining(b))
Kent Overstreet403b6cd2013-07-24 17:22:44 -07002045 break;
2046
2047 if (bkey_cmp(k, &b->key) <= 0) {
Kent Overstreet3a3b6a42013-07-24 16:46:42 -07002048 if (!b->level)
2049 bkey_put(b->c, k);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002050
Kent Overstreet1b207d82013-09-10 18:52:54 -07002051 ret |= btree_insert_key(b, op, k, replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002052 bch_keylist_pop_front(insert_keys);
2053 } else if (bkey_cmp(&START_KEY(k), &b->key) < 0) {
Kent Overstreet26c949f2013-09-10 18:41:15 -07002054 BKEY_PADDED(key) temp;
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07002055 bkey_copy(&temp.key, insert_keys->keys);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002056
2057 bch_cut_back(&b->key, &temp.key);
Kent Overstreetc2f95ae2013-07-24 17:24:25 -07002058 bch_cut_front(&b->key, insert_keys->keys);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002059
Kent Overstreet1b207d82013-09-10 18:52:54 -07002060 ret |= btree_insert_key(b, op, &temp.key, replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002061 break;
2062 } else {
2063 break;
2064 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07002065 }
2066
Kent Overstreet403b6cd2013-07-24 17:22:44 -07002067 BUG_ON(!bch_keylist_empty(insert_keys) && b->level);
2068
Kent Overstreetcafe5632013-03-23 16:11:31 -07002069 BUG_ON(bch_count_data(b) < oldsize);
2070 return ret;
2071}
2072
Kent Overstreet26c949f2013-09-10 18:41:15 -07002073static int btree_split(struct btree *b, struct btree_op *op,
2074 struct keylist *insert_keys,
Kent Overstreet1b207d82013-09-10 18:52:54 -07002075 struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002076{
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002077 bool split;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002078 struct btree *n1, *n2 = NULL, *n3 = NULL;
2079 uint64_t start_time = local_clock();
Kent Overstreetb54d6932013-07-24 18:04:18 -07002080 struct closure cl;
Kent Overstreet17e21a92013-07-26 12:32:38 -07002081 struct keylist parent_keys;
Kent Overstreetb54d6932013-07-24 18:04:18 -07002082
2083 closure_init_stack(&cl);
Kent Overstreet17e21a92013-07-26 12:32:38 -07002084 bch_keylist_init(&parent_keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002085
Kent Overstreet78365412013-12-17 01:29:34 -08002086 if (!b->level &&
2087 btree_check_reserve(b, op))
2088 return -EINTR;
2089
Kent Overstreetbc9389e2013-09-10 19:07:35 -07002090 n1 = btree_node_alloc_replacement(b, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002091 if (IS_ERR(n1))
2092 goto err;
2093
Kent Overstreetee811282013-12-17 23:49:49 -08002094 split = set_blocks(btree_bset_first(n1),
2095 block_bytes(n1->c)) > (btree_blocks(b) * 4) / 5;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002096
Kent Overstreetcafe5632013-03-23 16:11:31 -07002097 if (split) {
2098 unsigned keys = 0;
2099
Kent Overstreetee811282013-12-17 23:49:49 -08002100 trace_bcache_btree_node_split(b, btree_bset_first(n1)->keys);
Kent Overstreetc37511b2013-04-26 15:39:55 -07002101
Kent Overstreetbc9389e2013-09-10 19:07:35 -07002102 n2 = bch_btree_node_alloc(b->c, b->level, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002103 if (IS_ERR(n2))
2104 goto err_free1;
2105
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002106 if (!b->parent) {
Kent Overstreetbc9389e2013-09-10 19:07:35 -07002107 n3 = bch_btree_node_alloc(b->c, b->level + 1, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002108 if (IS_ERR(n3))
2109 goto err_free2;
2110 }
2111
Kent Overstreet1b207d82013-09-10 18:52:54 -07002112 bch_btree_insert_keys(n1, op, insert_keys, replace_key);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002113
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002114 /*
2115 * Has to be a linear search because we don't have an auxiliary
Kent Overstreetcafe5632013-03-23 16:11:31 -07002116 * search tree yet
2117 */
2118
Kent Overstreetee811282013-12-17 23:49:49 -08002119 while (keys < (btree_bset_first(n1)->keys * 3) / 5)
2120 keys += bkey_u64s(bset_bkey_idx(btree_bset_first(n1),
Kent Overstreetfafff812013-12-17 21:56:21 -08002121 keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002122
Kent Overstreetfafff812013-12-17 21:56:21 -08002123 bkey_copy_key(&n1->key,
Kent Overstreetee811282013-12-17 23:49:49 -08002124 bset_bkey_idx(btree_bset_first(n1), keys));
2125 keys += bkey_u64s(bset_bkey_idx(btree_bset_first(n1), keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002126
Kent Overstreetee811282013-12-17 23:49:49 -08002127 btree_bset_first(n2)->keys = btree_bset_first(n1)->keys - keys;
2128 btree_bset_first(n1)->keys = keys;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002129
Kent Overstreetee811282013-12-17 23:49:49 -08002130 memcpy(btree_bset_first(n2)->start,
2131 bset_bkey_last(btree_bset_first(n1)),
2132 btree_bset_first(n2)->keys * sizeof(uint64_t));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002133
2134 bkey_copy_key(&n2->key, &b->key);
2135
Kent Overstreet17e21a92013-07-26 12:32:38 -07002136 bch_keylist_add(&parent_keys, &n2->key);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002137 bch_btree_node_write(n2, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002138 rw_unlock(true, n2);
Kent Overstreetc37511b2013-04-26 15:39:55 -07002139 } else {
Kent Overstreetee811282013-12-17 23:49:49 -08002140 trace_bcache_btree_node_compact(b, btree_bset_first(n1)->keys);
Kent Overstreetc37511b2013-04-26 15:39:55 -07002141
Kent Overstreet1b207d82013-09-10 18:52:54 -07002142 bch_btree_insert_keys(n1, op, insert_keys, replace_key);
Kent Overstreetc37511b2013-04-26 15:39:55 -07002143 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07002144
Kent Overstreet17e21a92013-07-26 12:32:38 -07002145 bch_keylist_add(&parent_keys, &n1->key);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002146 bch_btree_node_write(n1, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002147
2148 if (n3) {
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002149 /* Depth increases, make a new root */
Kent Overstreetcafe5632013-03-23 16:11:31 -07002150 bkey_copy_key(&n3->key, &MAX_KEY);
Kent Overstreet17e21a92013-07-26 12:32:38 -07002151 bch_btree_insert_keys(n3, op, &parent_keys, NULL);
Kent Overstreetb54d6932013-07-24 18:04:18 -07002152 bch_btree_node_write(n3, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002153
Kent Overstreetb54d6932013-07-24 18:04:18 -07002154 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002155 bch_btree_set_root(n3);
2156 rw_unlock(true, n3);
Kent Overstreet17e21a92013-07-26 12:32:38 -07002157
2158 btree_node_free(b);
Kent Overstreetd6fd3b12013-07-24 17:20:19 -07002159 } else if (!b->parent) {
2160 /* Root filled up but didn't need to be split */
Kent Overstreetb54d6932013-07-24 18:04:18 -07002161 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002162 bch_btree_set_root(n1);
Kent Overstreet17e21a92013-07-26 12:32:38 -07002163
2164 btree_node_free(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002165 } else {
Kent Overstreet17e21a92013-07-26 12:32:38 -07002166 /* Split a non root node */
Kent Overstreetb54d6932013-07-24 18:04:18 -07002167 closure_sync(&cl);
Kent Overstreet17e21a92013-07-26 12:32:38 -07002168 make_btree_freeing_key(b, parent_keys.top);
2169 bch_keylist_push(&parent_keys);
2170
2171 btree_node_free(b);
2172
2173 bch_btree_insert_node(b->parent, op, &parent_keys, NULL, NULL);
2174 BUG_ON(!bch_keylist_empty(&parent_keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002175 }
2176
2177 rw_unlock(true, n1);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002178
Kent Overstreet169ef1c2013-03-28 12:50:55 -06002179 bch_time_stats_update(&b->c->btree_split_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002180
2181 return 0;
2182err_free2:
Kent Overstreet5f5837d2013-12-16 16:38:49 -08002183 bkey_put(b->c, &n2->key);
Kent Overstreete8e1d462013-07-24 17:27:07 -07002184 btree_node_free(n2);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002185 rw_unlock(true, n2);
2186err_free1:
Kent Overstreet5f5837d2013-12-16 16:38:49 -08002187 bkey_put(b->c, &n1->key);
Kent Overstreete8e1d462013-07-24 17:27:07 -07002188 btree_node_free(n1);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002189 rw_unlock(true, n1);
2190err:
Kent Overstreet5f5837d2013-12-16 16:38:49 -08002191 WARN(1, "bcache: btree split failed");
2192
Kent Overstreetcafe5632013-03-23 16:11:31 -07002193 if (n3 == ERR_PTR(-EAGAIN) ||
2194 n2 == ERR_PTR(-EAGAIN) ||
2195 n1 == ERR_PTR(-EAGAIN))
2196 return -EAGAIN;
2197
Kent Overstreetcafe5632013-03-23 16:11:31 -07002198 return -ENOMEM;
2199}
2200
Kent Overstreet26c949f2013-09-10 18:41:15 -07002201static int bch_btree_insert_node(struct btree *b, struct btree_op *op,
Kent Overstreetc18536a2013-07-24 17:44:17 -07002202 struct keylist *insert_keys,
Kent Overstreet1b207d82013-09-10 18:52:54 -07002203 atomic_t *journal_ref,
2204 struct bkey *replace_key)
Kent Overstreet26c949f2013-09-10 18:41:15 -07002205{
Kent Overstreet17e21a92013-07-26 12:32:38 -07002206 BUG_ON(b->level && replace_key);
Kent Overstreet26c949f2013-09-10 18:41:15 -07002207
Kent Overstreet59158fd2013-11-11 19:03:54 -08002208 if (bch_keylist_nkeys(insert_keys) > insert_u64s_remaining(b)) {
Kent Overstreet17e21a92013-07-26 12:32:38 -07002209 if (current->bio_list) {
2210 op->lock = b->c->root->level + 1;
2211 return -EAGAIN;
2212 } else if (op->lock <= b->c->root->level) {
2213 op->lock = b->c->root->level + 1;
2214 return -EINTR;
Kent Overstreet26c949f2013-09-10 18:41:15 -07002215 } else {
Kent Overstreet17e21a92013-07-26 12:32:38 -07002216 /* Invalidated all iterators */
2217 return btree_split(b, op, insert_keys, replace_key) ?:
2218 -EINTR;
Kent Overstreet26c949f2013-09-10 18:41:15 -07002219 }
Kent Overstreet17e21a92013-07-26 12:32:38 -07002220 } else {
Kent Overstreetee811282013-12-17 23:49:49 -08002221 BUG_ON(write_block(b) != btree_bset_last(b));
Kent Overstreet26c949f2013-09-10 18:41:15 -07002222
Kent Overstreet17e21a92013-07-26 12:32:38 -07002223 if (bch_btree_insert_keys(b, op, insert_keys, replace_key)) {
2224 if (!b->level)
2225 bch_btree_leaf_dirty(b, journal_ref);
2226 else
2227 bch_btree_node_write_sync(b);
2228 }
2229
2230 return 0;
2231 }
Kent Overstreet26c949f2013-09-10 18:41:15 -07002232}
2233
Kent Overstreete7c590e2013-09-10 18:39:16 -07002234int bch_btree_insert_check_key(struct btree *b, struct btree_op *op,
2235 struct bkey *check_key)
2236{
2237 int ret = -EINTR;
2238 uint64_t btree_ptr = b->key.ptr[0];
2239 unsigned long seq = b->seq;
2240 struct keylist insert;
2241 bool upgrade = op->lock == -1;
2242
2243 bch_keylist_init(&insert);
2244
2245 if (upgrade) {
2246 rw_unlock(false, b);
2247 rw_lock(true, b, b->level);
2248
2249 if (b->key.ptr[0] != btree_ptr ||
2250 b->seq != seq + 1)
2251 goto out;
2252 }
2253
2254 SET_KEY_PTRS(check_key, 1);
2255 get_random_bytes(&check_key->ptr[0], sizeof(uint64_t));
2256
2257 SET_PTR_DEV(check_key, 0, PTR_CHECK_DEV);
2258
2259 bch_keylist_add(&insert, check_key);
2260
Kent Overstreet1b207d82013-09-10 18:52:54 -07002261 ret = bch_btree_insert_node(b, op, &insert, NULL, NULL);
Kent Overstreete7c590e2013-09-10 18:39:16 -07002262
2263 BUG_ON(!ret && !bch_keylist_empty(&insert));
2264out:
2265 if (upgrade)
2266 downgrade_write(&b->lock);
2267 return ret;
2268}
2269
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002270struct btree_insert_op {
2271 struct btree_op op;
2272 struct keylist *keys;
2273 atomic_t *journal_ref;
2274 struct bkey *replace_key;
2275};
2276
Wei Yongjun08239ca2013-11-28 10:31:35 +08002277static int btree_insert_fn(struct btree_op *b_op, struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002278{
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002279 struct btree_insert_op *op = container_of(b_op,
2280 struct btree_insert_op, op);
Kent Overstreet403b6cd2013-07-24 17:22:44 -07002281
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002282 int ret = bch_btree_insert_node(b, &op->op, op->keys,
2283 op->journal_ref, op->replace_key);
2284 if (ret && !bch_keylist_empty(op->keys))
2285 return ret;
2286 else
2287 return MAP_DONE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002288}
2289
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002290int bch_btree_insert(struct cache_set *c, struct keylist *keys,
2291 atomic_t *journal_ref, struct bkey *replace_key)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002292{
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002293 struct btree_insert_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002294 int ret = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002295
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002296 BUG_ON(current->bio_list);
Kent Overstreet4f3d4012013-09-10 18:46:36 -07002297 BUG_ON(bch_keylist_empty(keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002298
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002299 bch_btree_op_init(&op.op, 0);
2300 op.keys = keys;
2301 op.journal_ref = journal_ref;
2302 op.replace_key = replace_key;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002303
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002304 while (!ret && !bch_keylist_empty(keys)) {
2305 op.op.lock = 0;
2306 ret = bch_btree_map_leaf_nodes(&op.op, c,
2307 &START_KEY(keys->keys),
2308 btree_insert_fn);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002309 }
2310
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002311 if (ret) {
2312 struct bkey *k;
2313
2314 pr_err("error %i", ret);
2315
2316 while ((k = bch_keylist_pop(keys)))
Kent Overstreet3a3b6a42013-07-24 16:46:42 -07002317 bkey_put(c, k);
Kent Overstreetcc7b8812013-07-24 18:07:22 -07002318 } else if (op.op.insert_collision)
2319 ret = -ESRCH;
Kent Overstreet6054c6d2013-07-24 18:06:22 -07002320
Kent Overstreetcafe5632013-03-23 16:11:31 -07002321 return ret;
2322}
2323
2324void bch_btree_set_root(struct btree *b)
2325{
2326 unsigned i;
Kent Overstreete49c7c32013-06-26 17:25:38 -07002327 struct closure cl;
2328
2329 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002330
Kent Overstreetc37511b2013-04-26 15:39:55 -07002331 trace_bcache_btree_set_root(b);
2332
Kent Overstreetcafe5632013-03-23 16:11:31 -07002333 BUG_ON(!b->written);
2334
2335 for (i = 0; i < KEY_PTRS(&b->key); i++)
2336 BUG_ON(PTR_BUCKET(b->c, &b->key, i)->prio != BTREE_PRIO);
2337
2338 mutex_lock(&b->c->bucket_lock);
2339 list_del_init(&b->list);
2340 mutex_unlock(&b->c->bucket_lock);
2341
2342 b->c->root = b;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002343
Kent Overstreete49c7c32013-06-26 17:25:38 -07002344 bch_journal_meta(b->c, &cl);
2345 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002346}
2347
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002348/* Map across nodes or keys */
2349
2350static int bch_btree_map_nodes_recurse(struct btree *b, struct btree_op *op,
2351 struct bkey *from,
2352 btree_map_nodes_fn *fn, int flags)
2353{
2354 int ret = MAP_CONTINUE;
2355
2356 if (b->level) {
2357 struct bkey *k;
2358 struct btree_iter iter;
2359
Kent Overstreetc052dd92013-11-11 17:35:24 -08002360 bch_btree_iter_init(&b->keys, &iter, from);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002361
Kent Overstreeta85e9682013-12-20 17:28:16 -08002362 while ((k = bch_btree_iter_next_filter(&iter, &b->keys,
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002363 bch_ptr_bad))) {
2364 ret = btree(map_nodes_recurse, k, b,
2365 op, from, fn, flags);
2366 from = NULL;
2367
2368 if (ret != MAP_CONTINUE)
2369 return ret;
2370 }
2371 }
2372
2373 if (!b->level || flags == MAP_ALL_NODES)
2374 ret = fn(op, b);
2375
2376 return ret;
2377}
2378
2379int __bch_btree_map_nodes(struct btree_op *op, struct cache_set *c,
2380 struct bkey *from, btree_map_nodes_fn *fn, int flags)
2381{
Kent Overstreetb54d6932013-07-24 18:04:18 -07002382 return btree_root(map_nodes_recurse, c, op, from, fn, flags);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002383}
2384
2385static int bch_btree_map_keys_recurse(struct btree *b, struct btree_op *op,
2386 struct bkey *from, btree_map_keys_fn *fn,
2387 int flags)
2388{
2389 int ret = MAP_CONTINUE;
2390 struct bkey *k;
2391 struct btree_iter iter;
2392
Kent Overstreetc052dd92013-11-11 17:35:24 -08002393 bch_btree_iter_init(&b->keys, &iter, from);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002394
Kent Overstreeta85e9682013-12-20 17:28:16 -08002395 while ((k = bch_btree_iter_next_filter(&iter, &b->keys, bch_ptr_bad))) {
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002396 ret = !b->level
2397 ? fn(op, b, k)
2398 : btree(map_keys_recurse, k, b, op, from, fn, flags);
2399 from = NULL;
2400
2401 if (ret != MAP_CONTINUE)
2402 return ret;
2403 }
2404
2405 if (!b->level && (flags & MAP_END_KEY))
2406 ret = fn(op, b, &KEY(KEY_INODE(&b->key),
2407 KEY_OFFSET(&b->key), 0));
2408
2409 return ret;
2410}
2411
2412int bch_btree_map_keys(struct btree_op *op, struct cache_set *c,
2413 struct bkey *from, btree_map_keys_fn *fn, int flags)
2414{
Kent Overstreetb54d6932013-07-24 18:04:18 -07002415 return btree_root(map_keys_recurse, c, op, from, fn, flags);
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002416}
2417
Kent Overstreetcafe5632013-03-23 16:11:31 -07002418/* Keybuf code */
2419
2420static inline int keybuf_cmp(struct keybuf_key *l, struct keybuf_key *r)
2421{
2422 /* Overlapping keys compare equal */
2423 if (bkey_cmp(&l->key, &START_KEY(&r->key)) <= 0)
2424 return -1;
2425 if (bkey_cmp(&START_KEY(&l->key), &r->key) >= 0)
2426 return 1;
2427 return 0;
2428}
2429
2430static inline int keybuf_nonoverlapping_cmp(struct keybuf_key *l,
2431 struct keybuf_key *r)
2432{
2433 return clamp_t(int64_t, bkey_cmp(&l->key, &r->key), -1, 1);
2434}
2435
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002436struct refill {
2437 struct btree_op op;
Kent Overstreet48a915a2013-10-31 15:43:22 -07002438 unsigned nr_found;
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002439 struct keybuf *buf;
2440 struct bkey *end;
2441 keybuf_pred_fn *pred;
2442};
2443
2444static int refill_keybuf_fn(struct btree_op *op, struct btree *b,
2445 struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002446{
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002447 struct refill *refill = container_of(op, struct refill, op);
2448 struct keybuf *buf = refill->buf;
2449 int ret = MAP_CONTINUE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002450
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002451 if (bkey_cmp(k, refill->end) >= 0) {
2452 ret = MAP_DONE;
2453 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002454 }
2455
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002456 if (!KEY_SIZE(k)) /* end key */
2457 goto out;
2458
2459 if (refill->pred(buf, k)) {
2460 struct keybuf_key *w;
2461
2462 spin_lock(&buf->lock);
2463
2464 w = array_alloc(&buf->freelist);
2465 if (!w) {
2466 spin_unlock(&buf->lock);
2467 return MAP_DONE;
2468 }
2469
2470 w->private = NULL;
2471 bkey_copy(&w->key, k);
2472
2473 if (RB_INSERT(&buf->keys, w, node, keybuf_cmp))
2474 array_free(&buf->freelist, w);
Kent Overstreet48a915a2013-10-31 15:43:22 -07002475 else
2476 refill->nr_found++;
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002477
2478 if (array_freelist_empty(&buf->freelist))
2479 ret = MAP_DONE;
2480
2481 spin_unlock(&buf->lock);
2482 }
2483out:
2484 buf->last_scanned = *k;
2485 return ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002486}
2487
2488void bch_refill_keybuf(struct cache_set *c, struct keybuf *buf,
Kent Overstreet72c27062013-06-05 06:24:39 -07002489 struct bkey *end, keybuf_pred_fn *pred)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002490{
2491 struct bkey start = buf->last_scanned;
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002492 struct refill refill;
Kent Overstreetcafe5632013-03-23 16:11:31 -07002493
2494 cond_resched();
2495
Kent Overstreetb54d6932013-07-24 18:04:18 -07002496 bch_btree_op_init(&refill.op, -1);
Kent Overstreet48a915a2013-10-31 15:43:22 -07002497 refill.nr_found = 0;
2498 refill.buf = buf;
2499 refill.end = end;
2500 refill.pred = pred;
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002501
2502 bch_btree_map_keys(&refill.op, c, &buf->last_scanned,
2503 refill_keybuf_fn, MAP_END_KEY);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002504
Kent Overstreet48a915a2013-10-31 15:43:22 -07002505 trace_bcache_keyscan(refill.nr_found,
2506 KEY_INODE(&start), KEY_OFFSET(&start),
2507 KEY_INODE(&buf->last_scanned),
2508 KEY_OFFSET(&buf->last_scanned));
Kent Overstreetcafe5632013-03-23 16:11:31 -07002509
2510 spin_lock(&buf->lock);
2511
2512 if (!RB_EMPTY_ROOT(&buf->keys)) {
2513 struct keybuf_key *w;
2514 w = RB_FIRST(&buf->keys, struct keybuf_key, node);
2515 buf->start = START_KEY(&w->key);
2516
2517 w = RB_LAST(&buf->keys, struct keybuf_key, node);
2518 buf->end = w->key;
2519 } else {
2520 buf->start = MAX_KEY;
2521 buf->end = MAX_KEY;
2522 }
2523
2524 spin_unlock(&buf->lock);
2525}
2526
2527static void __bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w)
2528{
2529 rb_erase(&w->node, &buf->keys);
2530 array_free(&buf->freelist, w);
2531}
2532
2533void bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w)
2534{
2535 spin_lock(&buf->lock);
2536 __bch_keybuf_del(buf, w);
2537 spin_unlock(&buf->lock);
2538}
2539
2540bool bch_keybuf_check_overlapping(struct keybuf *buf, struct bkey *start,
2541 struct bkey *end)
2542{
2543 bool ret = false;
2544 struct keybuf_key *p, *w, s;
2545 s.key = *start;
2546
2547 if (bkey_cmp(end, &buf->start) <= 0 ||
2548 bkey_cmp(start, &buf->end) >= 0)
2549 return false;
2550
2551 spin_lock(&buf->lock);
2552 w = RB_GREATER(&buf->keys, s, node, keybuf_nonoverlapping_cmp);
2553
2554 while (w && bkey_cmp(&START_KEY(&w->key), end) < 0) {
2555 p = w;
2556 w = RB_NEXT(w, node);
2557
2558 if (p->private)
2559 ret = true;
2560 else
2561 __bch_keybuf_del(buf, p);
2562 }
2563
2564 spin_unlock(&buf->lock);
2565 return ret;
2566}
2567
2568struct keybuf_key *bch_keybuf_next(struct keybuf *buf)
2569{
2570 struct keybuf_key *w;
2571 spin_lock(&buf->lock);
2572
2573 w = RB_FIRST(&buf->keys, struct keybuf_key, node);
2574
2575 while (w && w->private)
2576 w = RB_NEXT(w, node);
2577
2578 if (w)
2579 w->private = ERR_PTR(-EINTR);
2580
2581 spin_unlock(&buf->lock);
2582 return w;
2583}
2584
2585struct keybuf_key *bch_keybuf_next_rescan(struct cache_set *c,
Kent Overstreet48dad8b2013-09-10 18:48:51 -07002586 struct keybuf *buf,
2587 struct bkey *end,
2588 keybuf_pred_fn *pred)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002589{
2590 struct keybuf_key *ret;
2591
2592 while (1) {
2593 ret = bch_keybuf_next(buf);
2594 if (ret)
2595 break;
2596
2597 if (bkey_cmp(&buf->last_scanned, end) >= 0) {
2598 pr_debug("scan finished");
2599 break;
2600 }
2601
Kent Overstreet72c27062013-06-05 06:24:39 -07002602 bch_refill_keybuf(c, buf, end, pred);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002603 }
2604
2605 return ret;
2606}
2607
Kent Overstreet72c27062013-06-05 06:24:39 -07002608void bch_keybuf_init(struct keybuf *buf)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002609{
Kent Overstreetcafe5632013-03-23 16:11:31 -07002610 buf->last_scanned = MAX_KEY;
2611 buf->keys = RB_ROOT;
2612
2613 spin_lock_init(&buf->lock);
2614 array_allocator_init(&buf->freelist);
2615}
2616
2617void bch_btree_exit(void)
2618{
2619 if (btree_io_wq)
2620 destroy_workqueue(btree_io_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -07002621}
2622
2623int __init bch_btree_init(void)
2624{
Kent Overstreet72a44512013-10-24 17:19:26 -07002625 btree_io_wq = create_singlethread_workqueue("bch_btree_io");
2626 if (!btree_io_wq)
Kent Overstreetcafe5632013-03-23 16:11:31 -07002627 return -ENOMEM;
2628
2629 return 0;
2630}