Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1 | /* |
| 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" |
| 26 | #include "request.h" |
Kent Overstreet | 279afba | 2013-06-05 06:21:07 -0700 | [diff] [blame] | 27 | #include "writeback.h" |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 28 | |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/bitops.h> |
Kent Overstreet | 72a4451 | 2013-10-24 17:19:26 -0700 | [diff] [blame] | 31 | #include <linux/freezer.h> |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 32 | #include <linux/hash.h> |
Kent Overstreet | 72a4451 | 2013-10-24 17:19:26 -0700 | [diff] [blame] | 33 | #include <linux/kthread.h> |
Geert Uytterhoeven | cd953ed | 2013-03-27 18:56:28 +0100 | [diff] [blame] | 34 | #include <linux/prefetch.h> |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 35 | #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 | |
| 93 | static const char * const op_types[] = { |
| 94 | "insert", "replace" |
| 95 | }; |
| 96 | |
| 97 | static const char *op_type(struct btree_op *op) |
| 98 | { |
| 99 | return op_types[op->type]; |
| 100 | } |
| 101 | |
Kent Overstreet | df8e897 | 2013-07-24 17:37:59 -0700 | [diff] [blame^] | 102 | enum { |
| 103 | BTREE_INSERT_STATUS_INSERT, |
| 104 | BTREE_INSERT_STATUS_BACK_MERGE, |
| 105 | BTREE_INSERT_STATUS_OVERWROTE, |
| 106 | BTREE_INSERT_STATUS_FRONT_MERGE, |
| 107 | }; |
| 108 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 109 | #define MAX_NEED_GC 64 |
| 110 | #define MAX_SAVE_PRIO 72 |
| 111 | |
| 112 | #define PTR_DIRTY_BIT (((uint64_t) 1 << 36)) |
| 113 | |
| 114 | #define PTR_HASH(c, k) \ |
| 115 | (((k)->ptr[0] >> c->bucket_bits) | PTR_GEN(k, 0)) |
| 116 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 117 | static struct workqueue_struct *btree_io_wq; |
| 118 | |
| 119 | void bch_btree_op_init_stack(struct btree_op *op) |
| 120 | { |
| 121 | memset(op, 0, sizeof(struct btree_op)); |
| 122 | closure_init_stack(&op->cl); |
| 123 | op->lock = -1; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Kent Overstreet | df8e897 | 2013-07-24 17:37:59 -0700 | [diff] [blame^] | 126 | static inline bool should_split(struct btree *b) |
| 127 | { |
| 128 | struct bset *i = write_block(b); |
| 129 | return b->written >= btree_blocks(b) || |
| 130 | (b->written + __set_blocks(i, i->keys + 15, b->c) |
| 131 | > btree_blocks(b)); |
| 132 | } |
| 133 | |
| 134 | #define insert_lock(s, b) ((b)->level <= (s)->lock) |
| 135 | |
| 136 | /* |
| 137 | * These macros are for recursing down the btree - they handle the details of |
| 138 | * locking and looking up nodes in the cache for you. They're best treated as |
| 139 | * mere syntax when reading code that uses them. |
| 140 | * |
| 141 | * op->lock determines whether we take a read or a write lock at a given depth. |
| 142 | * If you've got a read lock and find that you need a write lock (i.e. you're |
| 143 | * going to have to split), set op->lock and return -EINTR; btree_root() will |
| 144 | * call you again and you'll have the correct lock. |
| 145 | */ |
| 146 | |
| 147 | /** |
| 148 | * btree - recurse down the btree on a specified key |
| 149 | * @fn: function to call, which will be passed the child node |
| 150 | * @key: key to recurse on |
| 151 | * @b: parent btree node |
| 152 | * @op: pointer to struct btree_op |
| 153 | */ |
| 154 | #define btree(fn, key, b, op, ...) \ |
| 155 | ({ \ |
| 156 | int _r, l = (b)->level - 1; \ |
| 157 | bool _w = l <= (op)->lock; \ |
| 158 | struct btree *_child = bch_btree_node_get((b)->c, key, l, _w); \ |
| 159 | if (!IS_ERR(_child)) { \ |
| 160 | _child->parent = (b); \ |
| 161 | _r = bch_btree_ ## fn(_child, op, ##__VA_ARGS__); \ |
| 162 | rw_unlock(_w, _child); \ |
| 163 | } else \ |
| 164 | _r = PTR_ERR(_child); \ |
| 165 | _r; \ |
| 166 | }) |
| 167 | |
| 168 | /** |
| 169 | * btree_root - call a function on the root of the btree |
| 170 | * @fn: function to call, which will be passed the child node |
| 171 | * @c: cache set |
| 172 | * @op: pointer to struct btree_op |
| 173 | */ |
| 174 | #define btree_root(fn, c, op, ...) \ |
| 175 | ({ \ |
| 176 | int _r = -EINTR; \ |
| 177 | do { \ |
| 178 | struct btree *_b = (c)->root; \ |
| 179 | bool _w = insert_lock(op, _b); \ |
| 180 | rw_lock(_w, _b, _b->level); \ |
| 181 | if (_b == (c)->root && \ |
| 182 | _w == insert_lock(op, _b)) { \ |
| 183 | _b->parent = NULL; \ |
| 184 | _r = bch_btree_ ## fn(_b, op, ##__VA_ARGS__); \ |
| 185 | } \ |
| 186 | rw_unlock(_w, _b); \ |
| 187 | bch_cannibalize_unlock(c); \ |
| 188 | if (_r == -ENOSPC) { \ |
| 189 | wait_event((c)->try_wait, \ |
| 190 | !(c)->try_harder); \ |
| 191 | _r = -EINTR; \ |
| 192 | } \ |
| 193 | } while (_r == -EINTR); \ |
| 194 | \ |
| 195 | _r; \ |
| 196 | }) |
| 197 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 198 | /* Btree key manipulation */ |
| 199 | |
Kent Overstreet | e7c590e | 2013-09-10 18:39:16 -0700 | [diff] [blame] | 200 | void __bkey_put(struct cache_set *c, struct bkey *k) |
| 201 | { |
| 202 | unsigned i; |
| 203 | |
| 204 | for (i = 0; i < KEY_PTRS(k); i++) |
| 205 | if (ptr_available(c, k, i)) |
| 206 | atomic_dec_bug(&PTR_BUCKET(c, k, i)->pin); |
| 207 | } |
| 208 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 209 | static void bkey_put(struct cache_set *c, struct bkey *k, int level) |
| 210 | { |
| 211 | if ((level && KEY_OFFSET(k)) || !level) |
| 212 | __bkey_put(c, k); |
| 213 | } |
| 214 | |
| 215 | /* Btree IO */ |
| 216 | |
| 217 | static uint64_t btree_csum_set(struct btree *b, struct bset *i) |
| 218 | { |
| 219 | uint64_t crc = b->key.ptr[0]; |
| 220 | void *data = (void *) i + 8, *end = end(i); |
| 221 | |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 222 | crc = bch_crc64_update(crc, data, end - data); |
Kent Overstreet | c19ed23 | 2013-03-26 13:49:02 -0700 | [diff] [blame] | 223 | return crc ^ 0xffffffffffffffffULL; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Kent Overstreet | f3059a5 | 2013-05-15 17:13:45 -0700 | [diff] [blame] | 226 | static void bch_btree_node_read_done(struct btree *b) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 227 | { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 228 | const char *err = "bad btree header"; |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 229 | struct bset *i = b->sets[0].data; |
| 230 | struct btree_iter *iter; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 231 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 232 | iter = mempool_alloc(b->c->fill_iter, GFP_NOWAIT); |
| 233 | iter->size = b->c->sb.bucket_size / b->c->sb.block_size; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 234 | iter->used = 0; |
| 235 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 236 | if (!i->seq) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 237 | goto err; |
| 238 | |
| 239 | for (; |
| 240 | b->written < btree_blocks(b) && i->seq == b->sets[0].data->seq; |
| 241 | i = write_block(b)) { |
| 242 | err = "unsupported bset version"; |
| 243 | if (i->version > BCACHE_BSET_VERSION) |
| 244 | goto err; |
| 245 | |
| 246 | err = "bad btree header"; |
| 247 | if (b->written + set_blocks(i, b->c) > btree_blocks(b)) |
| 248 | goto err; |
| 249 | |
| 250 | err = "bad magic"; |
| 251 | if (i->magic != bset_magic(b->c)) |
| 252 | goto err; |
| 253 | |
| 254 | err = "bad checksum"; |
| 255 | switch (i->version) { |
| 256 | case 0: |
| 257 | if (i->csum != csum_set(i)) |
| 258 | goto err; |
| 259 | break; |
| 260 | case BCACHE_BSET_VERSION: |
| 261 | if (i->csum != btree_csum_set(b, i)) |
| 262 | goto err; |
| 263 | break; |
| 264 | } |
| 265 | |
| 266 | err = "empty set"; |
| 267 | if (i != b->sets[0].data && !i->keys) |
| 268 | goto err; |
| 269 | |
| 270 | bch_btree_iter_push(iter, i->start, end(i)); |
| 271 | |
| 272 | b->written += set_blocks(i, b->c); |
| 273 | } |
| 274 | |
| 275 | err = "corrupted btree"; |
| 276 | for (i = write_block(b); |
| 277 | index(i, b) < btree_blocks(b); |
| 278 | i = ((void *) i) + block_bytes(b->c)) |
| 279 | if (i->seq == b->sets[0].data->seq) |
| 280 | goto err; |
| 281 | |
| 282 | bch_btree_sort_and_fix_extents(b, iter); |
| 283 | |
| 284 | i = b->sets[0].data; |
| 285 | err = "short btree key"; |
| 286 | if (b->sets[0].size && |
| 287 | bkey_cmp(&b->key, &b->sets[0].end) < 0) |
| 288 | goto err; |
| 289 | |
| 290 | if (b->written < btree_blocks(b)) |
| 291 | bch_bset_init_next(b); |
| 292 | out: |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 293 | mempool_free(iter, b->c->fill_iter); |
| 294 | return; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 295 | err: |
| 296 | set_btree_node_io_error(b); |
Kent Overstreet | 07e86cc | 2013-03-25 11:46:43 -0700 | [diff] [blame] | 297 | bch_cache_set_error(b->c, "%s at bucket %zu, block %zu, %u keys", |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 298 | err, PTR_BUCKET_NR(b->c, &b->key, 0), |
| 299 | index(i, b), i->keys); |
| 300 | goto out; |
| 301 | } |
| 302 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 303 | static void btree_node_read_endio(struct bio *bio, int error) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 304 | { |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 305 | struct closure *cl = bio->bi_private; |
| 306 | closure_put(cl); |
| 307 | } |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 308 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 309 | void bch_btree_node_read(struct btree *b) |
| 310 | { |
| 311 | uint64_t start_time = local_clock(); |
| 312 | struct closure cl; |
| 313 | struct bio *bio; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 314 | |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 315 | trace_bcache_btree_read(b); |
| 316 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 317 | closure_init_stack(&cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 318 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 319 | bio = bch_bbio_alloc(b->c); |
| 320 | bio->bi_rw = REQ_META|READ_SYNC; |
| 321 | bio->bi_size = KEY_SIZE(&b->key) << 9; |
| 322 | bio->bi_end_io = btree_node_read_endio; |
| 323 | bio->bi_private = &cl; |
| 324 | |
| 325 | bch_bio_map(bio, b->sets[0].data); |
| 326 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 327 | bch_submit_bbio(bio, b->c, &b->key, 0); |
| 328 | closure_sync(&cl); |
| 329 | |
| 330 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) |
| 331 | set_btree_node_io_error(b); |
| 332 | |
| 333 | bch_bbio_free(bio, b->c); |
| 334 | |
| 335 | if (btree_node_io_error(b)) |
| 336 | goto err; |
| 337 | |
| 338 | bch_btree_node_read_done(b); |
| 339 | |
| 340 | spin_lock(&b->c->btree_read_time_lock); |
| 341 | bch_time_stats_update(&b->c->btree_read_time, start_time); |
| 342 | spin_unlock(&b->c->btree_read_time_lock); |
| 343 | |
| 344 | return; |
| 345 | err: |
Geert Uytterhoeven | 61cbd25 | 2013-09-23 23:17:30 -0700 | [diff] [blame] | 346 | bch_cache_set_error(b->c, "io error reading bucket %zu", |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 347 | PTR_BUCKET_NR(b->c, &b->key, 0)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static void btree_complete_write(struct btree *b, struct btree_write *w) |
| 351 | { |
| 352 | if (w->prio_blocked && |
| 353 | !atomic_sub_return(w->prio_blocked, &b->c->prio_blocked)) |
Kent Overstreet | 119ba0f | 2013-04-24 19:01:12 -0700 | [diff] [blame] | 354 | wake_up_allocators(b->c); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 355 | |
| 356 | if (w->journal) { |
| 357 | atomic_dec_bug(w->journal); |
| 358 | __closure_wake_up(&b->c->journal.wait); |
| 359 | } |
| 360 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 361 | w->prio_blocked = 0; |
| 362 | w->journal = NULL; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 365 | static void __btree_node_write_done(struct closure *cl) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 366 | { |
| 367 | struct btree *b = container_of(cl, struct btree, io.cl); |
| 368 | struct btree_write *w = btree_prev_write(b); |
| 369 | |
| 370 | bch_bbio_free(b->bio, b->c); |
| 371 | b->bio = NULL; |
| 372 | btree_complete_write(b, w); |
| 373 | |
| 374 | if (btree_node_dirty(b)) |
| 375 | queue_delayed_work(btree_io_wq, &b->work, |
| 376 | msecs_to_jiffies(30000)); |
| 377 | |
| 378 | closure_return(cl); |
| 379 | } |
| 380 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 381 | static void btree_node_write_done(struct closure *cl) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 382 | { |
| 383 | struct btree *b = container_of(cl, struct btree, io.cl); |
| 384 | struct bio_vec *bv; |
| 385 | int n; |
| 386 | |
| 387 | __bio_for_each_segment(bv, b->bio, n, 0) |
| 388 | __free_page(bv->bv_page); |
| 389 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 390 | __btree_node_write_done(cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 393 | static void btree_node_write_endio(struct bio *bio, int error) |
| 394 | { |
| 395 | struct closure *cl = bio->bi_private; |
| 396 | struct btree *b = container_of(cl, struct btree, io.cl); |
| 397 | |
| 398 | if (error) |
| 399 | set_btree_node_io_error(b); |
| 400 | |
| 401 | bch_bbio_count_io_errors(b->c, bio, error, "writing btree"); |
| 402 | closure_put(cl); |
| 403 | } |
| 404 | |
| 405 | static void do_btree_node_write(struct btree *b) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 406 | { |
| 407 | struct closure *cl = &b->io.cl; |
| 408 | struct bset *i = b->sets[b->nsets].data; |
| 409 | BKEY_PADDED(key) k; |
| 410 | |
| 411 | i->version = BCACHE_BSET_VERSION; |
| 412 | i->csum = btree_csum_set(b, i); |
| 413 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 414 | BUG_ON(b->bio); |
| 415 | b->bio = bch_bbio_alloc(b->c); |
| 416 | |
| 417 | b->bio->bi_end_io = btree_node_write_endio; |
| 418 | b->bio->bi_private = &b->io.cl; |
Kent Overstreet | e49c7c3 | 2013-06-26 17:25:38 -0700 | [diff] [blame] | 419 | b->bio->bi_rw = REQ_META|WRITE_SYNC|REQ_FUA; |
| 420 | b->bio->bi_size = set_blocks(i, b->c) * block_bytes(b->c); |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 421 | bch_bio_map(b->bio, i); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 422 | |
Kent Overstreet | e49c7c3 | 2013-06-26 17:25:38 -0700 | [diff] [blame] | 423 | /* |
| 424 | * If we're appending to a leaf node, we don't technically need FUA - |
| 425 | * this write just needs to be persisted before the next journal write, |
| 426 | * which will be marked FLUSH|FUA. |
| 427 | * |
| 428 | * Similarly if we're writing a new btree root - the pointer is going to |
| 429 | * be in the next journal entry. |
| 430 | * |
| 431 | * But if we're writing a new btree node (that isn't a root) or |
| 432 | * appending to a non leaf btree node, we need either FUA or a flush |
| 433 | * when we write the parent with the new pointer. FUA is cheaper than a |
| 434 | * flush, and writes appending to leaf nodes aren't blocking anything so |
| 435 | * just make all btree node writes FUA to keep things sane. |
| 436 | */ |
| 437 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 438 | bkey_copy(&k.key, &b->key); |
| 439 | SET_PTR_OFFSET(&k.key, 0, PTR_OFFSET(&k.key, 0) + bset_offset(b, i)); |
| 440 | |
Kent Overstreet | 8e51e41 | 2013-06-06 18:15:57 -0700 | [diff] [blame] | 441 | if (!bio_alloc_pages(b->bio, GFP_NOIO)) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 442 | int j; |
| 443 | struct bio_vec *bv; |
| 444 | void *base = (void *) ((unsigned long) i & ~(PAGE_SIZE - 1)); |
| 445 | |
| 446 | bio_for_each_segment(bv, b->bio, j) |
| 447 | memcpy(page_address(bv->bv_page), |
| 448 | base + j * PAGE_SIZE, PAGE_SIZE); |
| 449 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 450 | bch_submit_bbio(b->bio, b->c, &k.key, 0); |
| 451 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 452 | continue_at(cl, btree_node_write_done, NULL); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 453 | } else { |
| 454 | b->bio->bi_vcnt = 0; |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 455 | bch_bio_map(b->bio, i); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 456 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 457 | bch_submit_bbio(b->bio, b->c, &k.key, 0); |
| 458 | |
| 459 | closure_sync(cl); |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 460 | __btree_node_write_done(cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 464 | void bch_btree_node_write(struct btree *b, struct closure *parent) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 465 | { |
| 466 | struct bset *i = b->sets[b->nsets].data; |
| 467 | |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 468 | trace_bcache_btree_write(b); |
| 469 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 470 | BUG_ON(current->bio_list); |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 471 | BUG_ON(b->written >= btree_blocks(b)); |
| 472 | BUG_ON(b->written && !i->keys); |
| 473 | BUG_ON(b->sets->data->seq != i->seq); |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 474 | bch_check_key_order(b, i); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 475 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 476 | cancel_delayed_work(&b->work); |
| 477 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 478 | /* If caller isn't waiting for write, parent refcount is cache set */ |
| 479 | closure_lock(&b->io, parent ?: &b->c->cl); |
| 480 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 481 | clear_bit(BTREE_NODE_dirty, &b->flags); |
| 482 | change_bit(BTREE_NODE_write_idx, &b->flags); |
| 483 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 484 | do_btree_node_write(b); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 485 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 486 | b->written += set_blocks(i, b->c); |
| 487 | atomic_long_add(set_blocks(i, b->c) * b->c->sb.block_size, |
| 488 | &PTR_CACHE(b->c, &b->key, 0)->btree_sectors_written); |
| 489 | |
| 490 | bch_btree_sort_lazy(b); |
| 491 | |
| 492 | if (b->written < btree_blocks(b)) |
| 493 | bch_bset_init_next(b); |
| 494 | } |
| 495 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 496 | static void btree_node_write_work(struct work_struct *w) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 497 | { |
| 498 | struct btree *b = container_of(to_delayed_work(w), struct btree, work); |
| 499 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 500 | rw_lock(true, b, b->level); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 501 | |
| 502 | if (btree_node_dirty(b)) |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 503 | bch_btree_node_write(b, NULL); |
| 504 | rw_unlock(true, b); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 507 | static void bch_btree_leaf_dirty(struct btree *b, struct btree_op *op) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 508 | { |
| 509 | struct bset *i = b->sets[b->nsets].data; |
| 510 | struct btree_write *w = btree_current_write(b); |
| 511 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 512 | BUG_ON(!b->written); |
| 513 | BUG_ON(!i->keys); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 514 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 515 | if (!btree_node_dirty(b)) |
| 516 | queue_delayed_work(btree_io_wq, &b->work, 30 * HZ); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 517 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 518 | set_btree_node_dirty(b); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 519 | |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 520 | if (op->journal) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 521 | if (w->journal && |
| 522 | journal_pin_cmp(b->c, w, op)) { |
| 523 | atomic_dec_bug(w->journal); |
| 524 | w->journal = NULL; |
| 525 | } |
| 526 | |
| 527 | if (!w->journal) { |
| 528 | w->journal = op->journal; |
| 529 | atomic_inc(w->journal); |
| 530 | } |
| 531 | } |
| 532 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 533 | /* Force write if set is too big */ |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 534 | if (set_bytes(i) > PAGE_SIZE - 48 && |
| 535 | !current->bio_list) |
| 536 | bch_btree_node_write(b, NULL); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /* |
| 540 | * Btree in memory cache - allocation/freeing |
| 541 | * mca -> memory cache |
| 542 | */ |
| 543 | |
| 544 | static void mca_reinit(struct btree *b) |
| 545 | { |
| 546 | unsigned i; |
| 547 | |
| 548 | b->flags = 0; |
| 549 | b->written = 0; |
| 550 | b->nsets = 0; |
| 551 | |
| 552 | for (i = 0; i < MAX_BSETS; i++) |
| 553 | b->sets[i].size = 0; |
| 554 | /* |
| 555 | * Second loop starts at 1 because b->sets[0]->data is the memory we |
| 556 | * allocated |
| 557 | */ |
| 558 | for (i = 1; i < MAX_BSETS; i++) |
| 559 | b->sets[i].data = NULL; |
| 560 | } |
| 561 | |
| 562 | #define mca_reserve(c) (((c->root && c->root->level) \ |
| 563 | ? c->root->level : 1) * 8 + 16) |
| 564 | #define mca_can_free(c) \ |
| 565 | max_t(int, 0, c->bucket_cache_used - mca_reserve(c)) |
| 566 | |
| 567 | static void mca_data_free(struct btree *b) |
| 568 | { |
| 569 | struct bset_tree *t = b->sets; |
| 570 | BUG_ON(!closure_is_unlocked(&b->io.cl)); |
| 571 | |
| 572 | if (bset_prev_bytes(b) < PAGE_SIZE) |
| 573 | kfree(t->prev); |
| 574 | else |
| 575 | free_pages((unsigned long) t->prev, |
| 576 | get_order(bset_prev_bytes(b))); |
| 577 | |
| 578 | if (bset_tree_bytes(b) < PAGE_SIZE) |
| 579 | kfree(t->tree); |
| 580 | else |
| 581 | free_pages((unsigned long) t->tree, |
| 582 | get_order(bset_tree_bytes(b))); |
| 583 | |
| 584 | free_pages((unsigned long) t->data, b->page_order); |
| 585 | |
| 586 | t->prev = NULL; |
| 587 | t->tree = NULL; |
| 588 | t->data = NULL; |
| 589 | list_move(&b->list, &b->c->btree_cache_freed); |
| 590 | b->c->bucket_cache_used--; |
| 591 | } |
| 592 | |
| 593 | static void mca_bucket_free(struct btree *b) |
| 594 | { |
| 595 | BUG_ON(btree_node_dirty(b)); |
| 596 | |
| 597 | b->key.ptr[0] = 0; |
| 598 | hlist_del_init_rcu(&b->hash); |
| 599 | list_move(&b->list, &b->c->btree_cache_freeable); |
| 600 | } |
| 601 | |
| 602 | static unsigned btree_order(struct bkey *k) |
| 603 | { |
| 604 | return ilog2(KEY_SIZE(k) / PAGE_SECTORS ?: 1); |
| 605 | } |
| 606 | |
| 607 | static void mca_data_alloc(struct btree *b, struct bkey *k, gfp_t gfp) |
| 608 | { |
| 609 | struct bset_tree *t = b->sets; |
| 610 | BUG_ON(t->data); |
| 611 | |
| 612 | b->page_order = max_t(unsigned, |
| 613 | ilog2(b->c->btree_pages), |
| 614 | btree_order(k)); |
| 615 | |
| 616 | t->data = (void *) __get_free_pages(gfp, b->page_order); |
| 617 | if (!t->data) |
| 618 | goto err; |
| 619 | |
| 620 | t->tree = bset_tree_bytes(b) < PAGE_SIZE |
| 621 | ? kmalloc(bset_tree_bytes(b), gfp) |
| 622 | : (void *) __get_free_pages(gfp, get_order(bset_tree_bytes(b))); |
| 623 | if (!t->tree) |
| 624 | goto err; |
| 625 | |
| 626 | t->prev = bset_prev_bytes(b) < PAGE_SIZE |
| 627 | ? kmalloc(bset_prev_bytes(b), gfp) |
| 628 | : (void *) __get_free_pages(gfp, get_order(bset_prev_bytes(b))); |
| 629 | if (!t->prev) |
| 630 | goto err; |
| 631 | |
| 632 | list_move(&b->list, &b->c->btree_cache); |
| 633 | b->c->bucket_cache_used++; |
| 634 | return; |
| 635 | err: |
| 636 | mca_data_free(b); |
| 637 | } |
| 638 | |
| 639 | static struct btree *mca_bucket_alloc(struct cache_set *c, |
| 640 | struct bkey *k, gfp_t gfp) |
| 641 | { |
| 642 | struct btree *b = kzalloc(sizeof(struct btree), gfp); |
| 643 | if (!b) |
| 644 | return NULL; |
| 645 | |
| 646 | init_rwsem(&b->lock); |
| 647 | lockdep_set_novalidate_class(&b->lock); |
| 648 | INIT_LIST_HEAD(&b->list); |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 649 | INIT_DELAYED_WORK(&b->work, btree_node_write_work); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 650 | b->c = c; |
| 651 | closure_init_unlocked(&b->io); |
| 652 | |
| 653 | mca_data_alloc(b, k, gfp); |
| 654 | return b; |
| 655 | } |
| 656 | |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 657 | static int mca_reap(struct btree *b, unsigned min_order, bool flush) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 658 | { |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 659 | struct closure cl; |
| 660 | |
| 661 | closure_init_stack(&cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 662 | lockdep_assert_held(&b->c->bucket_lock); |
| 663 | |
| 664 | if (!down_write_trylock(&b->lock)) |
| 665 | return -ENOMEM; |
| 666 | |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 667 | BUG_ON(btree_node_dirty(b) && !b->sets[0].data); |
| 668 | |
| 669 | if (b->page_order < min_order || |
| 670 | (!flush && |
| 671 | (btree_node_dirty(b) || |
| 672 | atomic_read(&b->io.cl.remaining) != -1))) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 673 | rw_unlock(true, b); |
| 674 | return -ENOMEM; |
| 675 | } |
| 676 | |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 677 | if (btree_node_dirty(b)) { |
| 678 | bch_btree_node_write(b, &cl); |
| 679 | closure_sync(&cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 682 | /* wait for any in flight btree write */ |
| 683 | closure_wait_event_sync(&b->io.wait, &cl, |
| 684 | atomic_read(&b->io.cl.remaining) == -1); |
| 685 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 686 | return 0; |
| 687 | } |
| 688 | |
Dave Chinner | 7dc19d5 | 2013-08-28 10:18:11 +1000 | [diff] [blame] | 689 | static unsigned long bch_mca_scan(struct shrinker *shrink, |
| 690 | struct shrink_control *sc) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 691 | { |
| 692 | struct cache_set *c = container_of(shrink, struct cache_set, shrink); |
| 693 | struct btree *b, *t; |
| 694 | unsigned long i, nr = sc->nr_to_scan; |
Dave Chinner | 7dc19d5 | 2013-08-28 10:18:11 +1000 | [diff] [blame] | 695 | unsigned long freed = 0; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 696 | |
| 697 | if (c->shrinker_disabled) |
Dave Chinner | 7dc19d5 | 2013-08-28 10:18:11 +1000 | [diff] [blame] | 698 | return SHRINK_STOP; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 699 | |
| 700 | if (c->try_harder) |
Dave Chinner | 7dc19d5 | 2013-08-28 10:18:11 +1000 | [diff] [blame] | 701 | return SHRINK_STOP; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 702 | |
| 703 | /* Return -1 if we can't do anything right now */ |
Kent Overstreet | a698e08 | 2013-09-23 23:17:34 -0700 | [diff] [blame] | 704 | if (sc->gfp_mask & __GFP_IO) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 705 | mutex_lock(&c->bucket_lock); |
| 706 | else if (!mutex_trylock(&c->bucket_lock)) |
| 707 | return -1; |
| 708 | |
Kent Overstreet | 36c9ea9 | 2013-06-03 13:04:56 -0700 | [diff] [blame] | 709 | /* |
| 710 | * It's _really_ critical that we don't free too many btree nodes - we |
| 711 | * have to always leave ourselves a reserve. The reserve is how we |
| 712 | * guarantee that allocating memory for a new btree node can always |
| 713 | * succeed, so that inserting keys into the btree can always succeed and |
| 714 | * IO can always make forward progress: |
| 715 | */ |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 716 | nr /= c->btree_pages; |
| 717 | nr = min_t(unsigned long, nr, mca_can_free(c)); |
| 718 | |
| 719 | i = 0; |
| 720 | list_for_each_entry_safe(b, t, &c->btree_cache_freeable, list) { |
Dave Chinner | 7dc19d5 | 2013-08-28 10:18:11 +1000 | [diff] [blame] | 721 | if (freed >= nr) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 722 | break; |
| 723 | |
| 724 | if (++i > 3 && |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 725 | !mca_reap(b, 0, false)) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 726 | mca_data_free(b); |
| 727 | rw_unlock(true, b); |
Dave Chinner | 7dc19d5 | 2013-08-28 10:18:11 +1000 | [diff] [blame] | 728 | freed++; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
| 732 | /* |
| 733 | * Can happen right when we first start up, before we've read in any |
| 734 | * btree nodes |
| 735 | */ |
| 736 | if (list_empty(&c->btree_cache)) |
| 737 | goto out; |
| 738 | |
Dave Chinner | 7dc19d5 | 2013-08-28 10:18:11 +1000 | [diff] [blame] | 739 | for (i = 0; (nr--) && i < c->bucket_cache_used; i++) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 740 | b = list_first_entry(&c->btree_cache, struct btree, list); |
| 741 | list_rotate_left(&c->btree_cache); |
| 742 | |
| 743 | if (!b->accessed && |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 744 | !mca_reap(b, 0, false)) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 745 | mca_bucket_free(b); |
| 746 | mca_data_free(b); |
| 747 | rw_unlock(true, b); |
Dave Chinner | 7dc19d5 | 2013-08-28 10:18:11 +1000 | [diff] [blame] | 748 | freed++; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 749 | } else |
| 750 | b->accessed = 0; |
| 751 | } |
| 752 | out: |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 753 | mutex_unlock(&c->bucket_lock); |
Dave Chinner | 7dc19d5 | 2013-08-28 10:18:11 +1000 | [diff] [blame] | 754 | return freed; |
| 755 | } |
| 756 | |
| 757 | static unsigned long bch_mca_count(struct shrinker *shrink, |
| 758 | struct shrink_control *sc) |
| 759 | { |
| 760 | struct cache_set *c = container_of(shrink, struct cache_set, shrink); |
| 761 | |
| 762 | if (c->shrinker_disabled) |
| 763 | return 0; |
| 764 | |
| 765 | if (c->try_harder) |
| 766 | return 0; |
| 767 | |
| 768 | return mca_can_free(c) * c->btree_pages; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | void bch_btree_cache_free(struct cache_set *c) |
| 772 | { |
| 773 | struct btree *b; |
| 774 | struct closure cl; |
| 775 | closure_init_stack(&cl); |
| 776 | |
| 777 | if (c->shrink.list.next) |
| 778 | unregister_shrinker(&c->shrink); |
| 779 | |
| 780 | mutex_lock(&c->bucket_lock); |
| 781 | |
| 782 | #ifdef CONFIG_BCACHE_DEBUG |
| 783 | if (c->verify_data) |
| 784 | list_move(&c->verify_data->list, &c->btree_cache); |
| 785 | #endif |
| 786 | |
| 787 | list_splice(&c->btree_cache_freeable, |
| 788 | &c->btree_cache); |
| 789 | |
| 790 | while (!list_empty(&c->btree_cache)) { |
| 791 | b = list_first_entry(&c->btree_cache, struct btree, list); |
| 792 | |
| 793 | if (btree_node_dirty(b)) |
| 794 | btree_complete_write(b, btree_current_write(b)); |
| 795 | clear_bit(BTREE_NODE_dirty, &b->flags); |
| 796 | |
| 797 | mca_data_free(b); |
| 798 | } |
| 799 | |
| 800 | while (!list_empty(&c->btree_cache_freed)) { |
| 801 | b = list_first_entry(&c->btree_cache_freed, |
| 802 | struct btree, list); |
| 803 | list_del(&b->list); |
| 804 | cancel_delayed_work_sync(&b->work); |
| 805 | kfree(b); |
| 806 | } |
| 807 | |
| 808 | mutex_unlock(&c->bucket_lock); |
| 809 | } |
| 810 | |
| 811 | int bch_btree_cache_alloc(struct cache_set *c) |
| 812 | { |
| 813 | unsigned i; |
| 814 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 815 | for (i = 0; i < mca_reserve(c); i++) |
Kent Overstreet | 72a4451 | 2013-10-24 17:19:26 -0700 | [diff] [blame] | 816 | if (!mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL)) |
| 817 | return -ENOMEM; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 818 | |
| 819 | list_splice_init(&c->btree_cache, |
| 820 | &c->btree_cache_freeable); |
| 821 | |
| 822 | #ifdef CONFIG_BCACHE_DEBUG |
| 823 | mutex_init(&c->verify_lock); |
| 824 | |
| 825 | c->verify_data = mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL); |
| 826 | |
| 827 | if (c->verify_data && |
| 828 | c->verify_data->sets[0].data) |
| 829 | list_del_init(&c->verify_data->list); |
| 830 | else |
| 831 | c->verify_data = NULL; |
| 832 | #endif |
| 833 | |
Dave Chinner | 7dc19d5 | 2013-08-28 10:18:11 +1000 | [diff] [blame] | 834 | c->shrink.count_objects = bch_mca_count; |
| 835 | c->shrink.scan_objects = bch_mca_scan; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 836 | c->shrink.seeks = 4; |
| 837 | c->shrink.batch = c->btree_pages * 2; |
| 838 | register_shrinker(&c->shrink); |
| 839 | |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | /* Btree in memory cache - hash table */ |
| 844 | |
| 845 | static struct hlist_head *mca_hash(struct cache_set *c, struct bkey *k) |
| 846 | { |
| 847 | return &c->bucket_hash[hash_32(PTR_HASH(c, k), BUCKET_HASH_BITS)]; |
| 848 | } |
| 849 | |
| 850 | static struct btree *mca_find(struct cache_set *c, struct bkey *k) |
| 851 | { |
| 852 | struct btree *b; |
| 853 | |
| 854 | rcu_read_lock(); |
| 855 | hlist_for_each_entry_rcu(b, mca_hash(c, k), hash) |
| 856 | if (PTR_HASH(c, &b->key) == PTR_HASH(c, k)) |
| 857 | goto out; |
| 858 | b = NULL; |
| 859 | out: |
| 860 | rcu_read_unlock(); |
| 861 | return b; |
| 862 | } |
| 863 | |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 864 | static struct btree *mca_cannibalize(struct cache_set *c, struct bkey *k) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 865 | { |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 866 | struct btree *b; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 867 | |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 868 | trace_bcache_btree_cache_cannibalize(c); |
| 869 | |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 870 | if (!c->try_harder) { |
| 871 | c->try_harder = current; |
| 872 | c->try_harder_start = local_clock(); |
| 873 | } else if (c->try_harder != current) |
| 874 | return ERR_PTR(-ENOSPC); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 875 | |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 876 | list_for_each_entry_reverse(b, &c->btree_cache, list) |
| 877 | if (!mca_reap(b, btree_order(k), false)) |
| 878 | return b; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 879 | |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 880 | list_for_each_entry_reverse(b, &c->btree_cache, list) |
| 881 | if (!mca_reap(b, btree_order(k), true)) |
| 882 | return b; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 883 | |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 884 | return ERR_PTR(-ENOMEM); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | /* |
| 888 | * We can only have one thread cannibalizing other cached btree nodes at a time, |
| 889 | * or we'll deadlock. We use an open coded mutex to ensure that, which a |
| 890 | * cannibalize_bucket() will take. This means every time we unlock the root of |
| 891 | * the btree, we need to release this lock if we have it held. |
| 892 | */ |
Kent Overstreet | df8e897 | 2013-07-24 17:37:59 -0700 | [diff] [blame^] | 893 | static void bch_cannibalize_unlock(struct cache_set *c) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 894 | { |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 895 | if (c->try_harder == current) { |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 896 | bch_time_stats_update(&c->try_harder_time, c->try_harder_start); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 897 | c->try_harder = NULL; |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 898 | wake_up(&c->try_wait); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 902 | static struct btree *mca_alloc(struct cache_set *c, struct bkey *k, int level) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 903 | { |
| 904 | struct btree *b; |
| 905 | |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 906 | BUG_ON(current->bio_list); |
| 907 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 908 | lockdep_assert_held(&c->bucket_lock); |
| 909 | |
| 910 | if (mca_find(c, k)) |
| 911 | return NULL; |
| 912 | |
| 913 | /* btree_free() doesn't free memory; it sticks the node on the end of |
| 914 | * the list. Check if there's any freed nodes there: |
| 915 | */ |
| 916 | list_for_each_entry(b, &c->btree_cache_freeable, list) |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 917 | if (!mca_reap(b, btree_order(k), false)) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 918 | goto out; |
| 919 | |
| 920 | /* We never free struct btree itself, just the memory that holds the on |
| 921 | * disk node. Check the freed list before allocating a new one: |
| 922 | */ |
| 923 | list_for_each_entry(b, &c->btree_cache_freed, list) |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 924 | if (!mca_reap(b, 0, false)) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 925 | mca_data_alloc(b, k, __GFP_NOWARN|GFP_NOIO); |
| 926 | if (!b->sets[0].data) |
| 927 | goto err; |
| 928 | else |
| 929 | goto out; |
| 930 | } |
| 931 | |
| 932 | b = mca_bucket_alloc(c, k, __GFP_NOWARN|GFP_NOIO); |
| 933 | if (!b) |
| 934 | goto err; |
| 935 | |
| 936 | BUG_ON(!down_write_trylock(&b->lock)); |
| 937 | if (!b->sets->data) |
| 938 | goto err; |
| 939 | out: |
| 940 | BUG_ON(!closure_is_unlocked(&b->io.cl)); |
| 941 | |
| 942 | bkey_copy(&b->key, k); |
| 943 | list_move(&b->list, &c->btree_cache); |
| 944 | hlist_del_init_rcu(&b->hash); |
| 945 | hlist_add_head_rcu(&b->hash, mca_hash(c, k)); |
| 946 | |
| 947 | lock_set_subclass(&b->lock.dep_map, level + 1, _THIS_IP_); |
| 948 | b->level = level; |
Kent Overstreet | d6fd3b1 | 2013-07-24 17:20:19 -0700 | [diff] [blame] | 949 | b->parent = (void *) ~0UL; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 950 | |
| 951 | mca_reinit(b); |
| 952 | |
| 953 | return b; |
| 954 | err: |
| 955 | if (b) |
| 956 | rw_unlock(true, b); |
| 957 | |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 958 | b = mca_cannibalize(c, k); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 959 | if (!IS_ERR(b)) |
| 960 | goto out; |
| 961 | |
| 962 | return b; |
| 963 | } |
| 964 | |
| 965 | /** |
| 966 | * bch_btree_node_get - find a btree node in the cache and lock it, reading it |
| 967 | * in from disk if necessary. |
| 968 | * |
| 969 | * If IO is necessary, it uses the closure embedded in struct btree_op to wait; |
| 970 | * if that closure is in non blocking mode, will return -EAGAIN. |
| 971 | * |
| 972 | * The btree node will have either a read or a write lock held, depending on |
| 973 | * level and op->lock. |
| 974 | */ |
| 975 | struct btree *bch_btree_node_get(struct cache_set *c, struct bkey *k, |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 976 | int level, bool write) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 977 | { |
| 978 | int i = 0; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 979 | struct btree *b; |
| 980 | |
| 981 | BUG_ON(level < 0); |
| 982 | retry: |
| 983 | b = mca_find(c, k); |
| 984 | |
| 985 | if (!b) { |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 986 | if (current->bio_list) |
| 987 | return ERR_PTR(-EAGAIN); |
| 988 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 989 | mutex_lock(&c->bucket_lock); |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 990 | b = mca_alloc(c, k, level); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 991 | mutex_unlock(&c->bucket_lock); |
| 992 | |
| 993 | if (!b) |
| 994 | goto retry; |
| 995 | if (IS_ERR(b)) |
| 996 | return b; |
| 997 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 998 | bch_btree_node_read(b); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 999 | |
| 1000 | if (!write) |
| 1001 | downgrade_write(&b->lock); |
| 1002 | } else { |
| 1003 | rw_lock(write, b, level); |
| 1004 | if (PTR_HASH(c, &b->key) != PTR_HASH(c, k)) { |
| 1005 | rw_unlock(write, b); |
| 1006 | goto retry; |
| 1007 | } |
| 1008 | BUG_ON(b->level != level); |
| 1009 | } |
| 1010 | |
| 1011 | b->accessed = 1; |
| 1012 | |
| 1013 | for (; i <= b->nsets && b->sets[i].size; i++) { |
| 1014 | prefetch(b->sets[i].tree); |
| 1015 | prefetch(b->sets[i].data); |
| 1016 | } |
| 1017 | |
| 1018 | for (; i <= b->nsets; i++) |
| 1019 | prefetch(b->sets[i].data); |
| 1020 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 1021 | if (btree_node_io_error(b)) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1022 | rw_unlock(write, b); |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 1023 | return ERR_PTR(-EIO); |
| 1024 | } |
| 1025 | |
| 1026 | BUG_ON(!b->written); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1027 | |
| 1028 | return b; |
| 1029 | } |
| 1030 | |
| 1031 | static void btree_node_prefetch(struct cache_set *c, struct bkey *k, int level) |
| 1032 | { |
| 1033 | struct btree *b; |
| 1034 | |
| 1035 | mutex_lock(&c->bucket_lock); |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 1036 | b = mca_alloc(c, k, level); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1037 | mutex_unlock(&c->bucket_lock); |
| 1038 | |
| 1039 | if (!IS_ERR_OR_NULL(b)) { |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 1040 | bch_btree_node_read(b); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1041 | rw_unlock(true, b); |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | /* Btree alloc */ |
| 1046 | |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 1047 | static void btree_node_free(struct btree *b) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1048 | { |
| 1049 | unsigned i; |
| 1050 | |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 1051 | trace_bcache_btree_node_free(b); |
| 1052 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1053 | BUG_ON(b == b->c->root); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1054 | |
| 1055 | if (btree_node_dirty(b)) |
| 1056 | btree_complete_write(b, btree_current_write(b)); |
| 1057 | clear_bit(BTREE_NODE_dirty, &b->flags); |
| 1058 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1059 | cancel_delayed_work(&b->work); |
| 1060 | |
| 1061 | mutex_lock(&b->c->bucket_lock); |
| 1062 | |
| 1063 | for (i = 0; i < KEY_PTRS(&b->key); i++) { |
| 1064 | BUG_ON(atomic_read(&PTR_BUCKET(b->c, &b->key, i)->pin)); |
| 1065 | |
| 1066 | bch_inc_gen(PTR_CACHE(b->c, &b->key, i), |
| 1067 | PTR_BUCKET(b->c, &b->key, i)); |
| 1068 | } |
| 1069 | |
| 1070 | bch_bucket_free(b->c, &b->key); |
| 1071 | mca_bucket_free(b); |
| 1072 | mutex_unlock(&b->c->bucket_lock); |
| 1073 | } |
| 1074 | |
Kent Overstreet | 35fcd84 | 2013-07-24 17:29:09 -0700 | [diff] [blame] | 1075 | struct btree *bch_btree_node_alloc(struct cache_set *c, int level) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1076 | { |
| 1077 | BKEY_PADDED(key) k; |
| 1078 | struct btree *b = ERR_PTR(-EAGAIN); |
| 1079 | |
| 1080 | mutex_lock(&c->bucket_lock); |
| 1081 | retry: |
Kent Overstreet | 35fcd84 | 2013-07-24 17:29:09 -0700 | [diff] [blame] | 1082 | if (__bch_bucket_alloc_set(c, WATERMARK_METADATA, &k.key, 1, true)) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1083 | goto err; |
| 1084 | |
| 1085 | SET_KEY_SIZE(&k.key, c->btree_pages * PAGE_SECTORS); |
| 1086 | |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 1087 | b = mca_alloc(c, &k.key, level); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1088 | if (IS_ERR(b)) |
| 1089 | goto err_free; |
| 1090 | |
| 1091 | if (!b) { |
Kent Overstreet | b1a67b0 | 2013-03-25 11:46:44 -0700 | [diff] [blame] | 1092 | cache_bug(c, |
| 1093 | "Tried to allocate bucket that was in btree cache"); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1094 | __bkey_put(c, &k.key); |
| 1095 | goto retry; |
| 1096 | } |
| 1097 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1098 | b->accessed = 1; |
| 1099 | bch_bset_init_next(b); |
| 1100 | |
| 1101 | mutex_unlock(&c->bucket_lock); |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 1102 | |
| 1103 | trace_bcache_btree_node_alloc(b); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1104 | return b; |
| 1105 | err_free: |
| 1106 | bch_bucket_free(c, &k.key); |
| 1107 | __bkey_put(c, &k.key); |
| 1108 | err: |
| 1109 | mutex_unlock(&c->bucket_lock); |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 1110 | |
| 1111 | trace_bcache_btree_node_alloc_fail(b); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1112 | return b; |
| 1113 | } |
| 1114 | |
Kent Overstreet | 35fcd84 | 2013-07-24 17:29:09 -0700 | [diff] [blame] | 1115 | static struct btree *btree_node_alloc_replacement(struct btree *b) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1116 | { |
Kent Overstreet | 35fcd84 | 2013-07-24 17:29:09 -0700 | [diff] [blame] | 1117 | struct btree *n = bch_btree_node_alloc(b->c, b->level); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1118 | if (!IS_ERR_OR_NULL(n)) |
| 1119 | bch_btree_sort_into(b, n); |
| 1120 | |
| 1121 | return n; |
| 1122 | } |
| 1123 | |
| 1124 | /* Garbage collection */ |
| 1125 | |
| 1126 | uint8_t __bch_btree_mark_key(struct cache_set *c, int level, struct bkey *k) |
| 1127 | { |
| 1128 | uint8_t stale = 0; |
| 1129 | unsigned i; |
| 1130 | struct bucket *g; |
| 1131 | |
| 1132 | /* |
| 1133 | * ptr_invalid() can't return true for the keys that mark btree nodes as |
| 1134 | * freed, but since ptr_bad() returns true we'll never actually use them |
| 1135 | * for anything and thus we don't want mark their pointers here |
| 1136 | */ |
| 1137 | if (!bkey_cmp(k, &ZERO_KEY)) |
| 1138 | return stale; |
| 1139 | |
| 1140 | for (i = 0; i < KEY_PTRS(k); i++) { |
| 1141 | if (!ptr_available(c, k, i)) |
| 1142 | continue; |
| 1143 | |
| 1144 | g = PTR_BUCKET(c, k, i); |
| 1145 | |
| 1146 | if (gen_after(g->gc_gen, PTR_GEN(k, i))) |
| 1147 | g->gc_gen = PTR_GEN(k, i); |
| 1148 | |
| 1149 | if (ptr_stale(c, k, i)) { |
| 1150 | stale = max(stale, ptr_stale(c, k, i)); |
| 1151 | continue; |
| 1152 | } |
| 1153 | |
| 1154 | cache_bug_on(GC_MARK(g) && |
| 1155 | (GC_MARK(g) == GC_MARK_METADATA) != (level != 0), |
| 1156 | c, "inconsistent ptrs: mark = %llu, level = %i", |
| 1157 | GC_MARK(g), level); |
| 1158 | |
| 1159 | if (level) |
| 1160 | SET_GC_MARK(g, GC_MARK_METADATA); |
| 1161 | else if (KEY_DIRTY(k)) |
| 1162 | SET_GC_MARK(g, GC_MARK_DIRTY); |
| 1163 | |
| 1164 | /* guard against overflow */ |
| 1165 | SET_GC_SECTORS_USED(g, min_t(unsigned, |
| 1166 | GC_SECTORS_USED(g) + KEY_SIZE(k), |
| 1167 | (1 << 14) - 1)); |
| 1168 | |
| 1169 | BUG_ON(!GC_SECTORS_USED(g)); |
| 1170 | } |
| 1171 | |
| 1172 | return stale; |
| 1173 | } |
| 1174 | |
| 1175 | #define btree_mark_key(b, k) __bch_btree_mark_key(b->c, b->level, k) |
| 1176 | |
| 1177 | static int btree_gc_mark_node(struct btree *b, unsigned *keys, |
| 1178 | struct gc_stat *gc) |
| 1179 | { |
| 1180 | uint8_t stale = 0; |
| 1181 | unsigned last_dev = -1; |
| 1182 | struct bcache_device *d = NULL; |
| 1183 | struct bkey *k; |
| 1184 | struct btree_iter iter; |
| 1185 | struct bset_tree *t; |
| 1186 | |
| 1187 | gc->nodes++; |
| 1188 | |
| 1189 | for_each_key_filter(b, k, &iter, bch_ptr_invalid) { |
| 1190 | if (last_dev != KEY_INODE(k)) { |
| 1191 | last_dev = KEY_INODE(k); |
| 1192 | |
| 1193 | d = KEY_INODE(k) < b->c->nr_uuids |
| 1194 | ? b->c->devices[last_dev] |
| 1195 | : NULL; |
| 1196 | } |
| 1197 | |
| 1198 | stale = max(stale, btree_mark_key(b, k)); |
| 1199 | |
| 1200 | if (bch_ptr_bad(b, k)) |
| 1201 | continue; |
| 1202 | |
| 1203 | *keys += bkey_u64s(k); |
| 1204 | |
| 1205 | gc->key_bytes += bkey_u64s(k); |
| 1206 | gc->nkeys++; |
| 1207 | |
| 1208 | gc->data += KEY_SIZE(k); |
Kent Overstreet | 444fc0b | 2013-05-11 17:07:26 -0700 | [diff] [blame] | 1209 | if (KEY_DIRTY(k)) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1210 | gc->dirty += KEY_SIZE(k); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | for (t = b->sets; t <= &b->sets[b->nsets]; t++) |
| 1214 | btree_bug_on(t->size && |
| 1215 | bset_written(b, t) && |
| 1216 | bkey_cmp(&b->key, &t->end) < 0, |
| 1217 | b, "found short btree key in gc"); |
| 1218 | |
| 1219 | return stale; |
| 1220 | } |
| 1221 | |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 1222 | static struct btree *btree_gc_alloc(struct btree *b, struct bkey *k) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1223 | { |
| 1224 | /* |
| 1225 | * We block priorities from being written for the duration of garbage |
| 1226 | * collection, so we can't sleep in btree_alloc() -> |
| 1227 | * bch_bucket_alloc_set(), or we'd risk deadlock - so we don't pass it |
| 1228 | * our closure. |
| 1229 | */ |
Kent Overstreet | 35fcd84 | 2013-07-24 17:29:09 -0700 | [diff] [blame] | 1230 | struct btree *n = btree_node_alloc_replacement(b); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1231 | |
| 1232 | if (!IS_ERR_OR_NULL(n)) { |
| 1233 | swap(b, n); |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 1234 | __bkey_put(b->c, &b->key); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1235 | |
| 1236 | memcpy(k->ptr, b->key.ptr, |
| 1237 | sizeof(uint64_t) * KEY_PTRS(&b->key)); |
| 1238 | |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 1239 | btree_node_free(n); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1240 | up_write(&n->lock); |
| 1241 | } |
| 1242 | |
| 1243 | return b; |
| 1244 | } |
| 1245 | |
| 1246 | /* |
| 1247 | * Leaving this at 2 until we've got incremental garbage collection done; it |
| 1248 | * could be higher (and has been tested with 4) except that garbage collection |
| 1249 | * could take much longer, adversely affecting latency. |
| 1250 | */ |
| 1251 | #define GC_MERGE_NODES 2U |
| 1252 | |
| 1253 | struct gc_merge_info { |
| 1254 | struct btree *b; |
| 1255 | struct bkey *k; |
| 1256 | unsigned keys; |
| 1257 | }; |
| 1258 | |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 1259 | static void btree_gc_coalesce(struct btree *b, struct gc_stat *gc, |
| 1260 | struct gc_merge_info *r) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1261 | { |
| 1262 | unsigned nodes = 0, keys = 0, blocks; |
| 1263 | int i; |
| 1264 | |
| 1265 | while (nodes < GC_MERGE_NODES && r[nodes].b) |
| 1266 | keys += r[nodes++].keys; |
| 1267 | |
| 1268 | blocks = btree_default_blocks(b->c) * 2 / 3; |
| 1269 | |
| 1270 | if (nodes < 2 || |
| 1271 | __set_blocks(b->sets[0].data, keys, b->c) > blocks * (nodes - 1)) |
| 1272 | return; |
| 1273 | |
| 1274 | for (i = nodes - 1; i >= 0; --i) { |
| 1275 | if (r[i].b->written) |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 1276 | r[i].b = btree_gc_alloc(r[i].b, r[i].k); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1277 | |
| 1278 | if (r[i].b->written) |
| 1279 | return; |
| 1280 | } |
| 1281 | |
| 1282 | for (i = nodes - 1; i > 0; --i) { |
| 1283 | struct bset *n1 = r[i].b->sets->data; |
| 1284 | struct bset *n2 = r[i - 1].b->sets->data; |
| 1285 | struct bkey *k, *last = NULL; |
| 1286 | |
| 1287 | keys = 0; |
| 1288 | |
| 1289 | if (i == 1) { |
| 1290 | /* |
| 1291 | * Last node we're not getting rid of - we're getting |
| 1292 | * rid of the node at r[0]. Have to try and fit all of |
| 1293 | * the remaining keys into this node; we can't ensure |
| 1294 | * they will always fit due to rounding and variable |
| 1295 | * length keys (shouldn't be possible in practice, |
| 1296 | * though) |
| 1297 | */ |
| 1298 | if (__set_blocks(n1, n1->keys + r->keys, |
| 1299 | b->c) > btree_blocks(r[i].b)) |
| 1300 | return; |
| 1301 | |
| 1302 | keys = n2->keys; |
| 1303 | last = &r->b->key; |
| 1304 | } else |
| 1305 | for (k = n2->start; |
| 1306 | k < end(n2); |
| 1307 | k = bkey_next(k)) { |
| 1308 | if (__set_blocks(n1, n1->keys + keys + |
| 1309 | bkey_u64s(k), b->c) > blocks) |
| 1310 | break; |
| 1311 | |
| 1312 | last = k; |
| 1313 | keys += bkey_u64s(k); |
| 1314 | } |
| 1315 | |
| 1316 | BUG_ON(__set_blocks(n1, n1->keys + keys, |
| 1317 | b->c) > btree_blocks(r[i].b)); |
| 1318 | |
| 1319 | if (last) { |
| 1320 | bkey_copy_key(&r[i].b->key, last); |
| 1321 | bkey_copy_key(r[i].k, last); |
| 1322 | } |
| 1323 | |
| 1324 | memcpy(end(n1), |
| 1325 | n2->start, |
| 1326 | (void *) node(n2, keys) - (void *) n2->start); |
| 1327 | |
| 1328 | n1->keys += keys; |
| 1329 | |
| 1330 | memmove(n2->start, |
| 1331 | node(n2, keys), |
| 1332 | (void *) end(n2) - (void *) node(n2, keys)); |
| 1333 | |
| 1334 | n2->keys -= keys; |
| 1335 | |
| 1336 | r[i].keys = n1->keys; |
| 1337 | r[i - 1].keys = n2->keys; |
| 1338 | } |
| 1339 | |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 1340 | btree_node_free(r->b); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1341 | up_write(&r->b->lock); |
| 1342 | |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 1343 | trace_bcache_btree_gc_coalesce(nodes); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1344 | |
| 1345 | gc->nodes--; |
| 1346 | nodes--; |
| 1347 | |
| 1348 | memmove(&r[0], &r[1], sizeof(struct gc_merge_info) * nodes); |
| 1349 | memset(&r[nodes], 0, sizeof(struct gc_merge_info)); |
| 1350 | } |
| 1351 | |
| 1352 | static int btree_gc_recurse(struct btree *b, struct btree_op *op, |
| 1353 | struct closure *writes, struct gc_stat *gc) |
| 1354 | { |
| 1355 | void write(struct btree *r) |
| 1356 | { |
| 1357 | if (!r->written) |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 1358 | bch_btree_node_write(r, &op->cl); |
| 1359 | else if (btree_node_dirty(r)) |
| 1360 | bch_btree_node_write(r, writes); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1361 | |
| 1362 | up_write(&r->lock); |
| 1363 | } |
| 1364 | |
| 1365 | int ret = 0, stale; |
| 1366 | unsigned i; |
| 1367 | struct gc_merge_info r[GC_MERGE_NODES]; |
| 1368 | |
| 1369 | memset(r, 0, sizeof(r)); |
| 1370 | |
| 1371 | while ((r->k = bch_next_recurse_key(b, &b->c->gc_done))) { |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 1372 | r->b = bch_btree_node_get(b->c, r->k, b->level - 1, true); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1373 | |
| 1374 | if (IS_ERR(r->b)) { |
| 1375 | ret = PTR_ERR(r->b); |
| 1376 | break; |
| 1377 | } |
| 1378 | |
| 1379 | r->keys = 0; |
| 1380 | stale = btree_gc_mark_node(r->b, &r->keys, gc); |
| 1381 | |
| 1382 | if (!b->written && |
| 1383 | (r->b->level || stale > 10 || |
| 1384 | b->c->gc_always_rewrite)) |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 1385 | r->b = btree_gc_alloc(r->b, r->k); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1386 | |
| 1387 | if (r->b->level) |
| 1388 | ret = btree_gc_recurse(r->b, op, writes, gc); |
| 1389 | |
| 1390 | if (ret) { |
| 1391 | write(r->b); |
| 1392 | break; |
| 1393 | } |
| 1394 | |
| 1395 | bkey_copy_key(&b->c->gc_done, r->k); |
| 1396 | |
| 1397 | if (!b->written) |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 1398 | btree_gc_coalesce(b, gc, r); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1399 | |
| 1400 | if (r[GC_MERGE_NODES - 1].b) |
| 1401 | write(r[GC_MERGE_NODES - 1].b); |
| 1402 | |
| 1403 | memmove(&r[1], &r[0], |
| 1404 | sizeof(struct gc_merge_info) * (GC_MERGE_NODES - 1)); |
| 1405 | |
| 1406 | /* When we've got incremental GC working, we'll want to do |
| 1407 | * if (should_resched()) |
| 1408 | * return -EAGAIN; |
| 1409 | */ |
| 1410 | cond_resched(); |
| 1411 | #if 0 |
| 1412 | if (need_resched()) { |
| 1413 | ret = -EAGAIN; |
| 1414 | break; |
| 1415 | } |
| 1416 | #endif |
| 1417 | } |
| 1418 | |
| 1419 | for (i = 1; i < GC_MERGE_NODES && r[i].b; i++) |
| 1420 | write(r[i].b); |
| 1421 | |
| 1422 | /* Might have freed some children, must remove their keys */ |
| 1423 | if (!b->written) |
| 1424 | bch_btree_sort(b); |
| 1425 | |
| 1426 | return ret; |
| 1427 | } |
| 1428 | |
| 1429 | static int bch_btree_gc_root(struct btree *b, struct btree_op *op, |
| 1430 | struct closure *writes, struct gc_stat *gc) |
| 1431 | { |
| 1432 | struct btree *n = NULL; |
| 1433 | unsigned keys = 0; |
| 1434 | int ret = 0, stale = btree_gc_mark_node(b, &keys, gc); |
| 1435 | |
| 1436 | if (b->level || stale > 10) |
Kent Overstreet | 35fcd84 | 2013-07-24 17:29:09 -0700 | [diff] [blame] | 1437 | n = btree_node_alloc_replacement(b); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1438 | |
| 1439 | if (!IS_ERR_OR_NULL(n)) |
| 1440 | swap(b, n); |
| 1441 | |
| 1442 | if (b->level) |
| 1443 | ret = btree_gc_recurse(b, op, writes, gc); |
| 1444 | |
| 1445 | if (!b->written || btree_node_dirty(b)) { |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 1446 | bch_btree_node_write(b, n ? &op->cl : NULL); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | if (!IS_ERR_OR_NULL(n)) { |
| 1450 | closure_sync(&op->cl); |
| 1451 | bch_btree_set_root(b); |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 1452 | btree_node_free(n); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1453 | rw_unlock(true, b); |
| 1454 | } |
| 1455 | |
| 1456 | return ret; |
| 1457 | } |
| 1458 | |
| 1459 | static void btree_gc_start(struct cache_set *c) |
| 1460 | { |
| 1461 | struct cache *ca; |
| 1462 | struct bucket *b; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1463 | unsigned i; |
| 1464 | |
| 1465 | if (!c->gc_mark_valid) |
| 1466 | return; |
| 1467 | |
| 1468 | mutex_lock(&c->bucket_lock); |
| 1469 | |
| 1470 | c->gc_mark_valid = 0; |
| 1471 | c->gc_done = ZERO_KEY; |
| 1472 | |
| 1473 | for_each_cache(ca, c, i) |
| 1474 | for_each_bucket(b, ca) { |
| 1475 | b->gc_gen = b->gen; |
Kent Overstreet | 29ebf46 | 2013-07-11 19:43:21 -0700 | [diff] [blame] | 1476 | if (!atomic_read(&b->pin)) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1477 | SET_GC_MARK(b, GC_MARK_RECLAIMABLE); |
Kent Overstreet | 29ebf46 | 2013-07-11 19:43:21 -0700 | [diff] [blame] | 1478 | SET_GC_SECTORS_USED(b, 0); |
| 1479 | } |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1482 | mutex_unlock(&c->bucket_lock); |
| 1483 | } |
| 1484 | |
| 1485 | size_t bch_btree_gc_finish(struct cache_set *c) |
| 1486 | { |
| 1487 | size_t available = 0; |
| 1488 | struct bucket *b; |
| 1489 | struct cache *ca; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1490 | unsigned i; |
| 1491 | |
| 1492 | mutex_lock(&c->bucket_lock); |
| 1493 | |
| 1494 | set_gc_sectors(c); |
| 1495 | c->gc_mark_valid = 1; |
| 1496 | c->need_gc = 0; |
| 1497 | |
| 1498 | if (c->root) |
| 1499 | for (i = 0; i < KEY_PTRS(&c->root->key); i++) |
| 1500 | SET_GC_MARK(PTR_BUCKET(c, &c->root->key, i), |
| 1501 | GC_MARK_METADATA); |
| 1502 | |
| 1503 | for (i = 0; i < KEY_PTRS(&c->uuid_bucket); i++) |
| 1504 | SET_GC_MARK(PTR_BUCKET(c, &c->uuid_bucket, i), |
| 1505 | GC_MARK_METADATA); |
| 1506 | |
| 1507 | for_each_cache(ca, c, i) { |
| 1508 | uint64_t *i; |
| 1509 | |
| 1510 | ca->invalidate_needs_gc = 0; |
| 1511 | |
| 1512 | for (i = ca->sb.d; i < ca->sb.d + ca->sb.keys; i++) |
| 1513 | SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA); |
| 1514 | |
| 1515 | for (i = ca->prio_buckets; |
| 1516 | i < ca->prio_buckets + prio_buckets(ca) * 2; i++) |
| 1517 | SET_GC_MARK(ca->buckets + *i, GC_MARK_METADATA); |
| 1518 | |
| 1519 | for_each_bucket(b, ca) { |
| 1520 | b->last_gc = b->gc_gen; |
| 1521 | c->need_gc = max(c->need_gc, bucket_gc_gen(b)); |
| 1522 | |
| 1523 | if (!atomic_read(&b->pin) && |
| 1524 | GC_MARK(b) == GC_MARK_RECLAIMABLE) { |
| 1525 | available++; |
| 1526 | if (!GC_SECTORS_USED(b)) |
| 1527 | bch_bucket_add_unused(ca, b); |
| 1528 | } |
| 1529 | } |
| 1530 | } |
| 1531 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1532 | mutex_unlock(&c->bucket_lock); |
| 1533 | return available; |
| 1534 | } |
| 1535 | |
Kent Overstreet | 72a4451 | 2013-10-24 17:19:26 -0700 | [diff] [blame] | 1536 | static void bch_btree_gc(struct cache_set *c) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1537 | { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1538 | int ret; |
| 1539 | unsigned long available; |
| 1540 | struct gc_stat stats; |
| 1541 | struct closure writes; |
| 1542 | struct btree_op op; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1543 | uint64_t start_time = local_clock(); |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 1544 | |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 1545 | trace_bcache_gc_start(c); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1546 | |
| 1547 | memset(&stats, 0, sizeof(struct gc_stat)); |
| 1548 | closure_init_stack(&writes); |
| 1549 | bch_btree_op_init_stack(&op); |
| 1550 | op.lock = SHRT_MAX; |
| 1551 | |
| 1552 | btree_gc_start(c); |
| 1553 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 1554 | atomic_inc(&c->prio_blocked); |
| 1555 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1556 | ret = btree_root(gc_root, c, &op, &writes, &stats); |
| 1557 | closure_sync(&op.cl); |
| 1558 | closure_sync(&writes); |
| 1559 | |
| 1560 | if (ret) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1561 | pr_warn("gc failed!"); |
Kent Overstreet | 72a4451 | 2013-10-24 17:19:26 -0700 | [diff] [blame] | 1562 | return; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | /* Possibly wait for new UUIDs or whatever to hit disk */ |
| 1566 | bch_journal_meta(c, &op.cl); |
| 1567 | closure_sync(&op.cl); |
| 1568 | |
| 1569 | available = bch_btree_gc_finish(c); |
| 1570 | |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 1571 | atomic_dec(&c->prio_blocked); |
| 1572 | wake_up_allocators(c); |
| 1573 | |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 1574 | bch_time_stats_update(&c->btree_gc_time, start_time); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1575 | |
| 1576 | stats.key_bytes *= sizeof(uint64_t); |
| 1577 | stats.dirty <<= 9; |
| 1578 | stats.data <<= 9; |
| 1579 | stats.in_use = (c->nbuckets - available) * 100 / c->nbuckets; |
| 1580 | memcpy(&c->gc_stats, &stats, sizeof(struct gc_stat)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1581 | |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 1582 | trace_bcache_gc_end(c); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1583 | |
Kent Overstreet | 72a4451 | 2013-10-24 17:19:26 -0700 | [diff] [blame] | 1584 | bch_moving_gc(c); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1585 | } |
| 1586 | |
Kent Overstreet | 72a4451 | 2013-10-24 17:19:26 -0700 | [diff] [blame] | 1587 | static int bch_gc_thread(void *arg) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1588 | { |
Kent Overstreet | 72a4451 | 2013-10-24 17:19:26 -0700 | [diff] [blame] | 1589 | struct cache_set *c = arg; |
| 1590 | |
| 1591 | while (1) { |
| 1592 | bch_btree_gc(c); |
| 1593 | |
| 1594 | set_current_state(TASK_INTERRUPTIBLE); |
| 1595 | if (kthread_should_stop()) |
| 1596 | break; |
| 1597 | |
| 1598 | try_to_freeze(); |
| 1599 | schedule(); |
| 1600 | } |
| 1601 | |
| 1602 | return 0; |
| 1603 | } |
| 1604 | |
| 1605 | int bch_gc_thread_start(struct cache_set *c) |
| 1606 | { |
| 1607 | c->gc_thread = kthread_create(bch_gc_thread, c, "bcache_gc"); |
| 1608 | if (IS_ERR(c->gc_thread)) |
| 1609 | return PTR_ERR(c->gc_thread); |
| 1610 | |
| 1611 | set_task_state(c->gc_thread, TASK_INTERRUPTIBLE); |
| 1612 | return 0; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | /* Initial partial gc */ |
| 1616 | |
| 1617 | static int bch_btree_check_recurse(struct btree *b, struct btree_op *op, |
| 1618 | unsigned long **seen) |
| 1619 | { |
| 1620 | int ret; |
| 1621 | unsigned i; |
| 1622 | struct bkey *k; |
| 1623 | struct bucket *g; |
| 1624 | struct btree_iter iter; |
| 1625 | |
| 1626 | for_each_key_filter(b, k, &iter, bch_ptr_invalid) { |
| 1627 | for (i = 0; i < KEY_PTRS(k); i++) { |
| 1628 | if (!ptr_available(b->c, k, i)) |
| 1629 | continue; |
| 1630 | |
| 1631 | g = PTR_BUCKET(b->c, k, i); |
| 1632 | |
| 1633 | if (!__test_and_set_bit(PTR_BUCKET_NR(b->c, k, i), |
| 1634 | seen[PTR_DEV(k, i)]) || |
| 1635 | !ptr_stale(b->c, k, i)) { |
| 1636 | g->gen = PTR_GEN(k, i); |
| 1637 | |
| 1638 | if (b->level) |
| 1639 | g->prio = BTREE_PRIO; |
| 1640 | else if (g->prio == BTREE_PRIO) |
| 1641 | g->prio = INITIAL_PRIO; |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | btree_mark_key(b, k); |
| 1646 | } |
| 1647 | |
| 1648 | if (b->level) { |
| 1649 | k = bch_next_recurse_key(b, &ZERO_KEY); |
| 1650 | |
| 1651 | while (k) { |
| 1652 | struct bkey *p = bch_next_recurse_key(b, k); |
| 1653 | if (p) |
| 1654 | btree_node_prefetch(b->c, p, b->level - 1); |
| 1655 | |
| 1656 | ret = btree(check_recurse, k, b, op, seen); |
| 1657 | if (ret) |
| 1658 | return ret; |
| 1659 | |
| 1660 | k = p; |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | return 0; |
| 1665 | } |
| 1666 | |
| 1667 | int bch_btree_check(struct cache_set *c, struct btree_op *op) |
| 1668 | { |
| 1669 | int ret = -ENOMEM; |
| 1670 | unsigned i; |
| 1671 | unsigned long *seen[MAX_CACHES_PER_SET]; |
| 1672 | |
| 1673 | memset(seen, 0, sizeof(seen)); |
| 1674 | |
| 1675 | for (i = 0; c->cache[i]; i++) { |
| 1676 | size_t n = DIV_ROUND_UP(c->cache[i]->sb.nbuckets, 8); |
| 1677 | seen[i] = kmalloc(n, GFP_KERNEL); |
| 1678 | if (!seen[i]) |
| 1679 | goto err; |
| 1680 | |
| 1681 | /* Disables the seen array until prio_read() uses it too */ |
| 1682 | memset(seen[i], 0xFF, n); |
| 1683 | } |
| 1684 | |
| 1685 | ret = btree_root(check_recurse, c, op, seen); |
| 1686 | err: |
| 1687 | for (i = 0; i < MAX_CACHES_PER_SET; i++) |
| 1688 | kfree(seen[i]); |
| 1689 | return ret; |
| 1690 | } |
| 1691 | |
| 1692 | /* Btree insertion */ |
| 1693 | |
| 1694 | static void shift_keys(struct btree *b, struct bkey *where, struct bkey *insert) |
| 1695 | { |
| 1696 | struct bset *i = b->sets[b->nsets].data; |
| 1697 | |
| 1698 | memmove((uint64_t *) where + bkey_u64s(insert), |
| 1699 | where, |
| 1700 | (void *) end(i) - (void *) where); |
| 1701 | |
| 1702 | i->keys += bkey_u64s(insert); |
| 1703 | bkey_copy(where, insert); |
| 1704 | bch_bset_fix_lookup_table(b, where); |
| 1705 | } |
| 1706 | |
| 1707 | static bool fix_overlapping_extents(struct btree *b, |
| 1708 | struct bkey *insert, |
| 1709 | struct btree_iter *iter, |
| 1710 | struct btree_op *op) |
| 1711 | { |
Kent Overstreet | 279afba | 2013-06-05 06:21:07 -0700 | [diff] [blame] | 1712 | void subtract_dirty(struct bkey *k, uint64_t offset, int sectors) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1713 | { |
Kent Overstreet | 279afba | 2013-06-05 06:21:07 -0700 | [diff] [blame] | 1714 | if (KEY_DIRTY(k)) |
| 1715 | bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k), |
| 1716 | offset, -sectors); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
Kent Overstreet | 279afba | 2013-06-05 06:21:07 -0700 | [diff] [blame] | 1719 | uint64_t old_offset; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1720 | unsigned old_size, sectors_found = 0; |
| 1721 | |
| 1722 | while (1) { |
| 1723 | struct bkey *k = bch_btree_iter_next(iter); |
| 1724 | if (!k || |
| 1725 | bkey_cmp(&START_KEY(k), insert) >= 0) |
| 1726 | break; |
| 1727 | |
| 1728 | if (bkey_cmp(k, &START_KEY(insert)) <= 0) |
| 1729 | continue; |
| 1730 | |
Kent Overstreet | 279afba | 2013-06-05 06:21:07 -0700 | [diff] [blame] | 1731 | old_offset = KEY_START(k); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1732 | old_size = KEY_SIZE(k); |
| 1733 | |
| 1734 | /* |
| 1735 | * We might overlap with 0 size extents; we can't skip these |
| 1736 | * because if they're in the set we're inserting to we have to |
| 1737 | * adjust them so they don't overlap with the key we're |
| 1738 | * inserting. But we don't want to check them for BTREE_REPLACE |
| 1739 | * operations. |
| 1740 | */ |
| 1741 | |
| 1742 | if (op->type == BTREE_REPLACE && |
| 1743 | KEY_SIZE(k)) { |
| 1744 | /* |
| 1745 | * k might have been split since we inserted/found the |
| 1746 | * key we're replacing |
| 1747 | */ |
| 1748 | unsigned i; |
| 1749 | uint64_t offset = KEY_START(k) - |
| 1750 | KEY_START(&op->replace); |
| 1751 | |
| 1752 | /* But it must be a subset of the replace key */ |
| 1753 | if (KEY_START(k) < KEY_START(&op->replace) || |
| 1754 | KEY_OFFSET(k) > KEY_OFFSET(&op->replace)) |
| 1755 | goto check_failed; |
| 1756 | |
| 1757 | /* We didn't find a key that we were supposed to */ |
| 1758 | if (KEY_START(k) > KEY_START(insert) + sectors_found) |
| 1759 | goto check_failed; |
| 1760 | |
| 1761 | if (KEY_PTRS(&op->replace) != KEY_PTRS(k)) |
| 1762 | goto check_failed; |
| 1763 | |
| 1764 | /* skip past gen */ |
| 1765 | offset <<= 8; |
| 1766 | |
| 1767 | BUG_ON(!KEY_PTRS(&op->replace)); |
| 1768 | |
| 1769 | for (i = 0; i < KEY_PTRS(&op->replace); i++) |
| 1770 | if (k->ptr[i] != op->replace.ptr[i] + offset) |
| 1771 | goto check_failed; |
| 1772 | |
| 1773 | sectors_found = KEY_OFFSET(k) - KEY_START(insert); |
| 1774 | } |
| 1775 | |
| 1776 | if (bkey_cmp(insert, k) < 0 && |
| 1777 | bkey_cmp(&START_KEY(insert), &START_KEY(k)) > 0) { |
| 1778 | /* |
| 1779 | * We overlapped in the middle of an existing key: that |
| 1780 | * means we have to split the old key. But we have to do |
| 1781 | * slightly different things depending on whether the |
| 1782 | * old key has been written out yet. |
| 1783 | */ |
| 1784 | |
| 1785 | struct bkey *top; |
| 1786 | |
Kent Overstreet | 279afba | 2013-06-05 06:21:07 -0700 | [diff] [blame] | 1787 | subtract_dirty(k, KEY_START(insert), KEY_SIZE(insert)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1788 | |
| 1789 | if (bkey_written(b, k)) { |
| 1790 | /* |
| 1791 | * We insert a new key to cover the top of the |
| 1792 | * old key, and the old key is modified in place |
| 1793 | * to represent the bottom split. |
| 1794 | * |
| 1795 | * It's completely arbitrary whether the new key |
| 1796 | * is the top or the bottom, but it has to match |
| 1797 | * up with what btree_sort_fixup() does - it |
| 1798 | * doesn't check for this kind of overlap, it |
| 1799 | * depends on us inserting a new key for the top |
| 1800 | * here. |
| 1801 | */ |
| 1802 | top = bch_bset_search(b, &b->sets[b->nsets], |
| 1803 | insert); |
| 1804 | shift_keys(b, top, k); |
| 1805 | } else { |
| 1806 | BKEY_PADDED(key) temp; |
| 1807 | bkey_copy(&temp.key, k); |
| 1808 | shift_keys(b, k, &temp.key); |
| 1809 | top = bkey_next(k); |
| 1810 | } |
| 1811 | |
| 1812 | bch_cut_front(insert, top); |
| 1813 | bch_cut_back(&START_KEY(insert), k); |
| 1814 | bch_bset_fix_invalidated_key(b, k); |
| 1815 | return false; |
| 1816 | } |
| 1817 | |
| 1818 | if (bkey_cmp(insert, k) < 0) { |
| 1819 | bch_cut_front(insert, k); |
| 1820 | } else { |
Kent Overstreet | 1fa8455 | 2013-11-10 21:55:27 -0800 | [diff] [blame] | 1821 | if (bkey_cmp(&START_KEY(insert), &START_KEY(k)) > 0) |
| 1822 | old_offset = KEY_START(insert); |
| 1823 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1824 | if (bkey_written(b, k) && |
| 1825 | bkey_cmp(&START_KEY(insert), &START_KEY(k)) <= 0) { |
| 1826 | /* |
| 1827 | * Completely overwrote, so we don't have to |
| 1828 | * invalidate the binary search tree |
| 1829 | */ |
| 1830 | bch_cut_front(k, k); |
| 1831 | } else { |
| 1832 | __bch_cut_back(&START_KEY(insert), k); |
| 1833 | bch_bset_fix_invalidated_key(b, k); |
| 1834 | } |
| 1835 | } |
| 1836 | |
Kent Overstreet | 279afba | 2013-06-05 06:21:07 -0700 | [diff] [blame] | 1837 | subtract_dirty(k, old_offset, old_size - KEY_SIZE(k)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | check_failed: |
| 1841 | if (op->type == BTREE_REPLACE) { |
| 1842 | if (!sectors_found) { |
| 1843 | op->insert_collision = true; |
| 1844 | return true; |
| 1845 | } else if (sectors_found < KEY_SIZE(insert)) { |
| 1846 | SET_KEY_OFFSET(insert, KEY_OFFSET(insert) - |
| 1847 | (KEY_SIZE(insert) - sectors_found)); |
| 1848 | SET_KEY_SIZE(insert, sectors_found); |
| 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | return false; |
| 1853 | } |
| 1854 | |
| 1855 | static bool btree_insert_key(struct btree *b, struct btree_op *op, |
| 1856 | struct bkey *k) |
| 1857 | { |
| 1858 | struct bset *i = b->sets[b->nsets].data; |
| 1859 | struct bkey *m, *prev; |
Kent Overstreet | 85b1492 | 2013-05-14 20:33:16 -0700 | [diff] [blame] | 1860 | unsigned status = BTREE_INSERT_STATUS_INSERT; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1861 | |
| 1862 | BUG_ON(bkey_cmp(k, &b->key) > 0); |
| 1863 | BUG_ON(b->level && !KEY_PTRS(k)); |
| 1864 | BUG_ON(!b->level && !KEY_OFFSET(k)); |
| 1865 | |
| 1866 | if (!b->level) { |
| 1867 | struct btree_iter iter; |
| 1868 | struct bkey search = KEY(KEY_INODE(k), KEY_START(k), 0); |
| 1869 | |
| 1870 | /* |
| 1871 | * bset_search() returns the first key that is strictly greater |
| 1872 | * than the search key - but for back merging, we want to find |
| 1873 | * the first key that is greater than or equal to KEY_START(k) - |
| 1874 | * unless KEY_START(k) is 0. |
| 1875 | */ |
| 1876 | if (KEY_OFFSET(&search)) |
| 1877 | SET_KEY_OFFSET(&search, KEY_OFFSET(&search) - 1); |
| 1878 | |
| 1879 | prev = NULL; |
| 1880 | m = bch_btree_iter_init(b, &iter, &search); |
| 1881 | |
| 1882 | if (fix_overlapping_extents(b, k, &iter, op)) |
| 1883 | return false; |
| 1884 | |
Kent Overstreet | 1fa8455 | 2013-11-10 21:55:27 -0800 | [diff] [blame] | 1885 | if (KEY_DIRTY(k)) |
| 1886 | bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k), |
| 1887 | KEY_START(k), KEY_SIZE(k)); |
| 1888 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1889 | while (m != end(i) && |
| 1890 | bkey_cmp(k, &START_KEY(m)) > 0) |
| 1891 | prev = m, m = bkey_next(m); |
| 1892 | |
| 1893 | if (key_merging_disabled(b->c)) |
| 1894 | goto insert; |
| 1895 | |
| 1896 | /* prev is in the tree, if we merge we're done */ |
Kent Overstreet | 85b1492 | 2013-05-14 20:33:16 -0700 | [diff] [blame] | 1897 | status = BTREE_INSERT_STATUS_BACK_MERGE; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1898 | if (prev && |
| 1899 | bch_bkey_try_merge(b, prev, k)) |
| 1900 | goto merged; |
| 1901 | |
Kent Overstreet | 85b1492 | 2013-05-14 20:33:16 -0700 | [diff] [blame] | 1902 | status = BTREE_INSERT_STATUS_OVERWROTE; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1903 | if (m != end(i) && |
| 1904 | KEY_PTRS(m) == KEY_PTRS(k) && !KEY_SIZE(m)) |
| 1905 | goto copy; |
| 1906 | |
Kent Overstreet | 85b1492 | 2013-05-14 20:33:16 -0700 | [diff] [blame] | 1907 | status = BTREE_INSERT_STATUS_FRONT_MERGE; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1908 | if (m != end(i) && |
| 1909 | bch_bkey_try_merge(b, k, m)) |
| 1910 | goto copy; |
| 1911 | } else |
| 1912 | m = bch_bset_search(b, &b->sets[b->nsets], k); |
| 1913 | |
| 1914 | insert: shift_keys(b, m, k); |
| 1915 | copy: bkey_copy(m, k); |
| 1916 | merged: |
Kent Overstreet | 85b1492 | 2013-05-14 20:33:16 -0700 | [diff] [blame] | 1917 | bch_check_keys(b, "%u for %s", status, op_type(op)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1918 | |
| 1919 | if (b->level && !KEY_OFFSET(k)) |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 1920 | btree_current_write(b)->prio_blocked++; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1921 | |
Kent Overstreet | 85b1492 | 2013-05-14 20:33:16 -0700 | [diff] [blame] | 1922 | trace_bcache_btree_insert_key(b, k, op->type, status); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1923 | |
| 1924 | return true; |
| 1925 | } |
| 1926 | |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 1927 | static bool bch_btree_insert_keys(struct btree *b, struct btree_op *op, |
| 1928 | struct keylist *insert_keys) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1929 | { |
| 1930 | bool ret = false; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1931 | unsigned oldsize = bch_count_data(b); |
| 1932 | |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 1933 | while (!bch_keylist_empty(insert_keys)) { |
Kent Overstreet | 403b6cd | 2013-07-24 17:22:44 -0700 | [diff] [blame] | 1934 | struct bset *i = write_block(b); |
Kent Overstreet | c2f95ae | 2013-07-24 17:24:25 -0700 | [diff] [blame] | 1935 | struct bkey *k = insert_keys->keys; |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 1936 | |
Kent Overstreet | 403b6cd | 2013-07-24 17:22:44 -0700 | [diff] [blame] | 1937 | if (b->written + __set_blocks(i, i->keys + bkey_u64s(k), b->c) |
| 1938 | > btree_blocks(b)) |
| 1939 | break; |
| 1940 | |
| 1941 | if (bkey_cmp(k, &b->key) <= 0) { |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 1942 | bkey_put(b->c, k, b->level); |
| 1943 | |
| 1944 | ret |= btree_insert_key(b, op, k); |
| 1945 | bch_keylist_pop_front(insert_keys); |
| 1946 | } else if (bkey_cmp(&START_KEY(k), &b->key) < 0) { |
| 1947 | #if 0 |
| 1948 | if (op->type == BTREE_REPLACE) { |
| 1949 | bkey_put(b->c, k, b->level); |
| 1950 | bch_keylist_pop_front(insert_keys); |
| 1951 | op->insert_collision = true; |
| 1952 | break; |
| 1953 | } |
| 1954 | #endif |
| 1955 | BKEY_PADDED(key) temp; |
Kent Overstreet | c2f95ae | 2013-07-24 17:24:25 -0700 | [diff] [blame] | 1956 | bkey_copy(&temp.key, insert_keys->keys); |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 1957 | |
| 1958 | bch_cut_back(&b->key, &temp.key); |
Kent Overstreet | c2f95ae | 2013-07-24 17:24:25 -0700 | [diff] [blame] | 1959 | bch_cut_front(&b->key, insert_keys->keys); |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 1960 | |
| 1961 | ret |= btree_insert_key(b, op, &temp.key); |
| 1962 | break; |
| 1963 | } else { |
| 1964 | break; |
| 1965 | } |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1966 | } |
| 1967 | |
Kent Overstreet | 403b6cd | 2013-07-24 17:22:44 -0700 | [diff] [blame] | 1968 | BUG_ON(!bch_keylist_empty(insert_keys) && b->level); |
| 1969 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1970 | BUG_ON(bch_count_data(b) < oldsize); |
| 1971 | return ret; |
| 1972 | } |
| 1973 | |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 1974 | static int btree_split(struct btree *b, struct btree_op *op, |
| 1975 | struct keylist *insert_keys, |
| 1976 | struct keylist *parent_keys) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1977 | { |
Kent Overstreet | d6fd3b1 | 2013-07-24 17:20:19 -0700 | [diff] [blame] | 1978 | bool split; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1979 | struct btree *n1, *n2 = NULL, *n3 = NULL; |
| 1980 | uint64_t start_time = local_clock(); |
| 1981 | |
Kent Overstreet | 35fcd84 | 2013-07-24 17:29:09 -0700 | [diff] [blame] | 1982 | n1 = btree_node_alloc_replacement(b); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1983 | if (IS_ERR(n1)) |
| 1984 | goto err; |
| 1985 | |
| 1986 | split = set_blocks(n1->sets[0].data, n1->c) > (btree_blocks(b) * 4) / 5; |
| 1987 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1988 | if (split) { |
| 1989 | unsigned keys = 0; |
| 1990 | |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 1991 | trace_bcache_btree_node_split(b, n1->sets[0].data->keys); |
| 1992 | |
Kent Overstreet | 35fcd84 | 2013-07-24 17:29:09 -0700 | [diff] [blame] | 1993 | n2 = bch_btree_node_alloc(b->c, b->level); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1994 | if (IS_ERR(n2)) |
| 1995 | goto err_free1; |
| 1996 | |
Kent Overstreet | d6fd3b1 | 2013-07-24 17:20:19 -0700 | [diff] [blame] | 1997 | if (!b->parent) { |
Kent Overstreet | 35fcd84 | 2013-07-24 17:29:09 -0700 | [diff] [blame] | 1998 | n3 = bch_btree_node_alloc(b->c, b->level + 1); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1999 | if (IS_ERR(n3)) |
| 2000 | goto err_free2; |
| 2001 | } |
| 2002 | |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 2003 | bch_btree_insert_keys(n1, op, insert_keys); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2004 | |
Kent Overstreet | d6fd3b1 | 2013-07-24 17:20:19 -0700 | [diff] [blame] | 2005 | /* |
| 2006 | * Has to be a linear search because we don't have an auxiliary |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2007 | * search tree yet |
| 2008 | */ |
| 2009 | |
| 2010 | while (keys < (n1->sets[0].data->keys * 3) / 5) |
| 2011 | keys += bkey_u64s(node(n1->sets[0].data, keys)); |
| 2012 | |
| 2013 | bkey_copy_key(&n1->key, node(n1->sets[0].data, keys)); |
| 2014 | keys += bkey_u64s(node(n1->sets[0].data, keys)); |
| 2015 | |
| 2016 | n2->sets[0].data->keys = n1->sets[0].data->keys - keys; |
| 2017 | n1->sets[0].data->keys = keys; |
| 2018 | |
| 2019 | memcpy(n2->sets[0].data->start, |
| 2020 | end(n1->sets[0].data), |
| 2021 | n2->sets[0].data->keys * sizeof(uint64_t)); |
| 2022 | |
| 2023 | bkey_copy_key(&n2->key, &b->key); |
| 2024 | |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 2025 | bch_keylist_add(parent_keys, &n2->key); |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 2026 | bch_btree_node_write(n2, &op->cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2027 | rw_unlock(true, n2); |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 2028 | } else { |
| 2029 | trace_bcache_btree_node_compact(b, n1->sets[0].data->keys); |
| 2030 | |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 2031 | bch_btree_insert_keys(n1, op, insert_keys); |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 2032 | } |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2033 | |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 2034 | bch_keylist_add(parent_keys, &n1->key); |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 2035 | bch_btree_node_write(n1, &op->cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2036 | |
| 2037 | if (n3) { |
Kent Overstreet | d6fd3b1 | 2013-07-24 17:20:19 -0700 | [diff] [blame] | 2038 | /* Depth increases, make a new root */ |
| 2039 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2040 | bkey_copy_key(&n3->key, &MAX_KEY); |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 2041 | bch_btree_insert_keys(n3, op, parent_keys); |
Kent Overstreet | 5794351 | 2013-04-25 13:58:35 -0700 | [diff] [blame] | 2042 | bch_btree_node_write(n3, &op->cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2043 | |
| 2044 | closure_sync(&op->cl); |
| 2045 | bch_btree_set_root(n3); |
| 2046 | rw_unlock(true, n3); |
Kent Overstreet | d6fd3b1 | 2013-07-24 17:20:19 -0700 | [diff] [blame] | 2047 | } else if (!b->parent) { |
| 2048 | /* Root filled up but didn't need to be split */ |
| 2049 | |
Kent Overstreet | c2f95ae | 2013-07-24 17:24:25 -0700 | [diff] [blame] | 2050 | bch_keylist_reset(parent_keys); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2051 | closure_sync(&op->cl); |
| 2052 | bch_btree_set_root(n1); |
| 2053 | } else { |
| 2054 | unsigned i; |
| 2055 | |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 2056 | bkey_copy(parent_keys->top, &b->key); |
| 2057 | bkey_copy_key(parent_keys->top, &ZERO_KEY); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2058 | |
| 2059 | for (i = 0; i < KEY_PTRS(&b->key); i++) { |
| 2060 | uint8_t g = PTR_BUCKET(b->c, &b->key, i)->gen + 1; |
| 2061 | |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 2062 | SET_PTR_GEN(parent_keys->top, i, g); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2063 | } |
| 2064 | |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 2065 | bch_keylist_push(parent_keys); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2066 | closure_sync(&op->cl); |
| 2067 | atomic_inc(&b->c->prio_blocked); |
| 2068 | } |
| 2069 | |
| 2070 | rw_unlock(true, n1); |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 2071 | btree_node_free(b); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2072 | |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 2073 | bch_time_stats_update(&b->c->btree_split_time, start_time); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2074 | |
| 2075 | return 0; |
| 2076 | err_free2: |
| 2077 | __bkey_put(n2->c, &n2->key); |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 2078 | btree_node_free(n2); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2079 | rw_unlock(true, n2); |
| 2080 | err_free1: |
| 2081 | __bkey_put(n1->c, &n1->key); |
Kent Overstreet | e8e1d46 | 2013-07-24 17:27:07 -0700 | [diff] [blame] | 2082 | btree_node_free(n1); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2083 | rw_unlock(true, n1); |
| 2084 | err: |
| 2085 | if (n3 == ERR_PTR(-EAGAIN) || |
| 2086 | n2 == ERR_PTR(-EAGAIN) || |
| 2087 | n1 == ERR_PTR(-EAGAIN)) |
| 2088 | return -EAGAIN; |
| 2089 | |
| 2090 | pr_warn("couldn't split"); |
| 2091 | return -ENOMEM; |
| 2092 | } |
| 2093 | |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 2094 | static int bch_btree_insert_node(struct btree *b, struct btree_op *op, |
| 2095 | struct keylist *insert_keys) |
| 2096 | { |
| 2097 | int ret = 0; |
| 2098 | struct keylist split_keys; |
| 2099 | |
| 2100 | bch_keylist_init(&split_keys); |
| 2101 | |
| 2102 | BUG_ON(b->level); |
| 2103 | |
| 2104 | do { |
| 2105 | if (should_split(b)) { |
| 2106 | if (current->bio_list) { |
| 2107 | op->lock = b->c->root->level + 1; |
| 2108 | ret = -EAGAIN; |
| 2109 | } else if (op->lock <= b->c->root->level) { |
| 2110 | op->lock = b->c->root->level + 1; |
| 2111 | ret = -EINTR; |
| 2112 | } else { |
| 2113 | struct btree *parent = b->parent; |
| 2114 | |
| 2115 | ret = btree_split(b, op, insert_keys, |
| 2116 | &split_keys); |
| 2117 | insert_keys = &split_keys; |
| 2118 | b = parent; |
Kent Overstreet | 403b6cd | 2013-07-24 17:22:44 -0700 | [diff] [blame] | 2119 | if (!ret) |
| 2120 | ret = -EINTR; |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 2121 | } |
| 2122 | } else { |
| 2123 | BUG_ON(write_block(b) != b->sets[b->nsets].data); |
| 2124 | |
| 2125 | if (bch_btree_insert_keys(b, op, insert_keys)) { |
| 2126 | if (!b->level) |
| 2127 | bch_btree_leaf_dirty(b, op); |
| 2128 | else |
| 2129 | bch_btree_node_write(b, &op->cl); |
| 2130 | } |
| 2131 | } |
| 2132 | } while (!bch_keylist_empty(&split_keys)); |
| 2133 | |
| 2134 | return ret; |
| 2135 | } |
| 2136 | |
Kent Overstreet | e7c590e | 2013-09-10 18:39:16 -0700 | [diff] [blame] | 2137 | int bch_btree_insert_check_key(struct btree *b, struct btree_op *op, |
| 2138 | struct bkey *check_key) |
| 2139 | { |
| 2140 | int ret = -EINTR; |
| 2141 | uint64_t btree_ptr = b->key.ptr[0]; |
| 2142 | unsigned long seq = b->seq; |
| 2143 | struct keylist insert; |
| 2144 | bool upgrade = op->lock == -1; |
| 2145 | |
| 2146 | bch_keylist_init(&insert); |
| 2147 | |
| 2148 | if (upgrade) { |
| 2149 | rw_unlock(false, b); |
| 2150 | rw_lock(true, b, b->level); |
| 2151 | |
| 2152 | if (b->key.ptr[0] != btree_ptr || |
| 2153 | b->seq != seq + 1) |
| 2154 | goto out; |
| 2155 | } |
| 2156 | |
| 2157 | SET_KEY_PTRS(check_key, 1); |
| 2158 | get_random_bytes(&check_key->ptr[0], sizeof(uint64_t)); |
| 2159 | |
| 2160 | SET_PTR_DEV(check_key, 0, PTR_CHECK_DEV); |
| 2161 | |
| 2162 | bch_keylist_add(&insert, check_key); |
| 2163 | |
| 2164 | BUG_ON(op->type != BTREE_INSERT); |
| 2165 | |
| 2166 | ret = bch_btree_insert_node(b, op, &insert); |
| 2167 | |
| 2168 | BUG_ON(!ret && !bch_keylist_empty(&insert)); |
| 2169 | out: |
| 2170 | if (upgrade) |
| 2171 | downgrade_write(&b->lock); |
| 2172 | return ret; |
| 2173 | } |
| 2174 | |
Kent Overstreet | 4f3d401 | 2013-09-10 18:46:36 -0700 | [diff] [blame] | 2175 | static int bch_btree_insert_recurse(struct btree *b, struct btree_op *op, |
| 2176 | struct keylist *keys) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2177 | { |
Kent Overstreet | 4f3d401 | 2013-09-10 18:46:36 -0700 | [diff] [blame] | 2178 | if (bch_keylist_empty(keys)) |
Kent Overstreet | 403b6cd | 2013-07-24 17:22:44 -0700 | [diff] [blame] | 2179 | return 0; |
| 2180 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2181 | if (b->level) { |
Kent Overstreet | 4f3d401 | 2013-09-10 18:46:36 -0700 | [diff] [blame] | 2182 | struct bkey *k; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2183 | |
Kent Overstreet | c2f95ae | 2013-07-24 17:24:25 -0700 | [diff] [blame] | 2184 | k = bch_next_recurse_key(b, &START_KEY(keys->keys)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2185 | if (!k) { |
| 2186 | btree_bug(b, "no key to recurse on at level %i/%i", |
| 2187 | b->level, b->c->root->level); |
| 2188 | |
Kent Overstreet | c2f95ae | 2013-07-24 17:24:25 -0700 | [diff] [blame] | 2189 | bch_keylist_reset(keys); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2190 | return -EIO; |
| 2191 | } |
| 2192 | |
Kent Overstreet | 4f3d401 | 2013-09-10 18:46:36 -0700 | [diff] [blame] | 2193 | return btree(insert_recurse, k, b, op, keys); |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 2194 | } else { |
Kent Overstreet | 4f3d401 | 2013-09-10 18:46:36 -0700 | [diff] [blame] | 2195 | return bch_btree_insert_node(b, op, keys); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2196 | } |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2197 | } |
| 2198 | |
Kent Overstreet | 4f3d401 | 2013-09-10 18:46:36 -0700 | [diff] [blame] | 2199 | int bch_btree_insert(struct btree_op *op, struct cache_set *c, |
| 2200 | struct keylist *keys) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2201 | { |
| 2202 | int ret = 0; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2203 | |
| 2204 | /* |
| 2205 | * Don't want to block with the btree locked unless we have to, |
| 2206 | * otherwise we get deadlocks with try_harder and between split/gc |
| 2207 | */ |
| 2208 | clear_closure_blocking(&op->cl); |
| 2209 | |
Kent Overstreet | 4f3d401 | 2013-09-10 18:46:36 -0700 | [diff] [blame] | 2210 | BUG_ON(bch_keylist_empty(keys)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2211 | |
Kent Overstreet | 4f3d401 | 2013-09-10 18:46:36 -0700 | [diff] [blame] | 2212 | while (!bch_keylist_empty(keys)) { |
Kent Overstreet | 403b6cd | 2013-07-24 17:22:44 -0700 | [diff] [blame] | 2213 | op->lock = 0; |
Kent Overstreet | 4f3d401 | 2013-09-10 18:46:36 -0700 | [diff] [blame] | 2214 | ret = btree_root(insert_recurse, c, op, keys); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2215 | |
| 2216 | if (ret == -EAGAIN) { |
| 2217 | ret = 0; |
| 2218 | closure_sync(&op->cl); |
| 2219 | } else if (ret) { |
| 2220 | struct bkey *k; |
| 2221 | |
| 2222 | pr_err("error %i trying to insert key for %s", |
| 2223 | ret, op_type(op)); |
| 2224 | |
Kent Overstreet | 4f3d401 | 2013-09-10 18:46:36 -0700 | [diff] [blame] | 2225 | while ((k = bch_keylist_pop(keys))) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2226 | bkey_put(c, k, 0); |
| 2227 | } |
| 2228 | } |
| 2229 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2230 | return ret; |
| 2231 | } |
| 2232 | |
| 2233 | void bch_btree_set_root(struct btree *b) |
| 2234 | { |
| 2235 | unsigned i; |
Kent Overstreet | e49c7c3 | 2013-06-26 17:25:38 -0700 | [diff] [blame] | 2236 | struct closure cl; |
| 2237 | |
| 2238 | closure_init_stack(&cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2239 | |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 2240 | trace_bcache_btree_set_root(b); |
| 2241 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2242 | BUG_ON(!b->written); |
| 2243 | |
| 2244 | for (i = 0; i < KEY_PTRS(&b->key); i++) |
| 2245 | BUG_ON(PTR_BUCKET(b->c, &b->key, i)->prio != BTREE_PRIO); |
| 2246 | |
| 2247 | mutex_lock(&b->c->bucket_lock); |
| 2248 | list_del_init(&b->list); |
| 2249 | mutex_unlock(&b->c->bucket_lock); |
| 2250 | |
| 2251 | b->c->root = b; |
| 2252 | __bkey_put(b->c, &b->key); |
| 2253 | |
Kent Overstreet | e49c7c3 | 2013-06-26 17:25:38 -0700 | [diff] [blame] | 2254 | bch_journal_meta(b->c, &cl); |
| 2255 | closure_sync(&cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2256 | } |
| 2257 | |
| 2258 | /* Cache lookup */ |
| 2259 | |
| 2260 | static int submit_partial_cache_miss(struct btree *b, struct btree_op *op, |
| 2261 | struct bkey *k) |
| 2262 | { |
| 2263 | struct search *s = container_of(op, struct search, op); |
| 2264 | struct bio *bio = &s->bio.bio; |
| 2265 | int ret = 0; |
| 2266 | |
| 2267 | while (!ret && |
| 2268 | !op->lookup_done) { |
| 2269 | unsigned sectors = INT_MAX; |
| 2270 | |
| 2271 | if (KEY_INODE(k) == op->inode) { |
| 2272 | if (KEY_START(k) <= bio->bi_sector) |
| 2273 | break; |
| 2274 | |
| 2275 | sectors = min_t(uint64_t, sectors, |
| 2276 | KEY_START(k) - bio->bi_sector); |
| 2277 | } |
| 2278 | |
| 2279 | ret = s->d->cache_miss(b, s, bio, sectors); |
| 2280 | } |
| 2281 | |
| 2282 | return ret; |
| 2283 | } |
| 2284 | |
| 2285 | /* |
| 2286 | * Read from a single key, handling the initial cache miss if the key starts in |
| 2287 | * the middle of the bio |
| 2288 | */ |
| 2289 | static int submit_partial_cache_hit(struct btree *b, struct btree_op *op, |
| 2290 | struct bkey *k) |
| 2291 | { |
| 2292 | struct search *s = container_of(op, struct search, op); |
| 2293 | struct bio *bio = &s->bio.bio; |
| 2294 | unsigned ptr; |
| 2295 | struct bio *n; |
| 2296 | |
| 2297 | int ret = submit_partial_cache_miss(b, op, k); |
| 2298 | if (ret || op->lookup_done) |
| 2299 | return ret; |
| 2300 | |
| 2301 | /* XXX: figure out best pointer - for multiple cache devices */ |
| 2302 | ptr = 0; |
| 2303 | |
| 2304 | PTR_BUCKET(b->c, k, ptr)->prio = INITIAL_PRIO; |
| 2305 | |
| 2306 | while (!op->lookup_done && |
| 2307 | KEY_INODE(k) == op->inode && |
| 2308 | bio->bi_sector < KEY_OFFSET(k)) { |
| 2309 | struct bkey *bio_key; |
| 2310 | sector_t sector = PTR_OFFSET(k, ptr) + |
| 2311 | (bio->bi_sector - KEY_START(k)); |
| 2312 | unsigned sectors = min_t(uint64_t, INT_MAX, |
| 2313 | KEY_OFFSET(k) - bio->bi_sector); |
| 2314 | |
| 2315 | n = bch_bio_split(bio, sectors, GFP_NOIO, s->d->bio_split); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2316 | if (n == bio) |
| 2317 | op->lookup_done = true; |
| 2318 | |
| 2319 | bio_key = &container_of(n, struct bbio, bio)->key; |
| 2320 | |
| 2321 | /* |
| 2322 | * The bucket we're reading from might be reused while our bio |
| 2323 | * is in flight, and we could then end up reading the wrong |
| 2324 | * data. |
| 2325 | * |
| 2326 | * We guard against this by checking (in cache_read_endio()) if |
| 2327 | * the pointer is stale again; if so, we treat it as an error |
| 2328 | * and reread from the backing device (but we don't pass that |
| 2329 | * error up anywhere). |
| 2330 | */ |
| 2331 | |
| 2332 | bch_bkey_copy_single_ptr(bio_key, k, ptr); |
| 2333 | SET_PTR_OFFSET(bio_key, 0, sector); |
| 2334 | |
| 2335 | n->bi_end_io = bch_cache_read_endio; |
| 2336 | n->bi_private = &s->cl; |
| 2337 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2338 | __bch_submit_bbio(n, b->c); |
| 2339 | } |
| 2340 | |
| 2341 | return 0; |
| 2342 | } |
| 2343 | |
Kent Overstreet | df8e897 | 2013-07-24 17:37:59 -0700 | [diff] [blame^] | 2344 | static int bch_btree_search_recurse(struct btree *b, struct btree_op *op) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2345 | { |
| 2346 | struct search *s = container_of(op, struct search, op); |
| 2347 | struct bio *bio = &s->bio.bio; |
| 2348 | |
| 2349 | int ret = 0; |
| 2350 | struct bkey *k; |
| 2351 | struct btree_iter iter; |
| 2352 | bch_btree_iter_init(b, &iter, &KEY(op->inode, bio->bi_sector, 0)); |
| 2353 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2354 | do { |
| 2355 | k = bch_btree_iter_next_filter(&iter, b, bch_ptr_bad); |
| 2356 | if (!k) { |
| 2357 | /* |
| 2358 | * b->key would be exactly what we want, except that |
| 2359 | * pointers to btree nodes have nonzero size - we |
| 2360 | * wouldn't go far enough |
| 2361 | */ |
| 2362 | |
| 2363 | ret = submit_partial_cache_miss(b, op, |
| 2364 | &KEY(KEY_INODE(&b->key), |
| 2365 | KEY_OFFSET(&b->key), 0)); |
| 2366 | break; |
| 2367 | } |
| 2368 | |
| 2369 | ret = b->level |
| 2370 | ? btree(search_recurse, k, b, op) |
| 2371 | : submit_partial_cache_hit(b, op, k); |
| 2372 | } while (!ret && |
| 2373 | !op->lookup_done); |
| 2374 | |
| 2375 | return ret; |
| 2376 | } |
| 2377 | |
Kent Overstreet | df8e897 | 2013-07-24 17:37:59 -0700 | [diff] [blame^] | 2378 | void bch_btree_search_async(struct closure *cl) |
| 2379 | { |
| 2380 | struct btree_op *op = container_of(cl, struct btree_op, cl); |
| 2381 | |
| 2382 | int ret = btree_root(search_recurse, op->c, op); |
| 2383 | |
| 2384 | if (ret == -EAGAIN) |
| 2385 | continue_at(cl, bch_btree_search_async, bcache_wq); |
| 2386 | |
| 2387 | closure_return(cl); |
| 2388 | } |
| 2389 | |
Kent Overstreet | 48dad8b | 2013-09-10 18:48:51 -0700 | [diff] [blame] | 2390 | /* Map across nodes or keys */ |
| 2391 | |
| 2392 | static int bch_btree_map_nodes_recurse(struct btree *b, struct btree_op *op, |
| 2393 | struct bkey *from, |
| 2394 | btree_map_nodes_fn *fn, int flags) |
| 2395 | { |
| 2396 | int ret = MAP_CONTINUE; |
| 2397 | |
| 2398 | if (b->level) { |
| 2399 | struct bkey *k; |
| 2400 | struct btree_iter iter; |
| 2401 | |
| 2402 | bch_btree_iter_init(b, &iter, from); |
| 2403 | |
| 2404 | while ((k = bch_btree_iter_next_filter(&iter, b, |
| 2405 | bch_ptr_bad))) { |
| 2406 | ret = btree(map_nodes_recurse, k, b, |
| 2407 | op, from, fn, flags); |
| 2408 | from = NULL; |
| 2409 | |
| 2410 | if (ret != MAP_CONTINUE) |
| 2411 | return ret; |
| 2412 | } |
| 2413 | } |
| 2414 | |
| 2415 | if (!b->level || flags == MAP_ALL_NODES) |
| 2416 | ret = fn(op, b); |
| 2417 | |
| 2418 | return ret; |
| 2419 | } |
| 2420 | |
| 2421 | int __bch_btree_map_nodes(struct btree_op *op, struct cache_set *c, |
| 2422 | struct bkey *from, btree_map_nodes_fn *fn, int flags) |
| 2423 | { |
| 2424 | int ret = btree_root(map_nodes_recurse, c, op, from, fn, flags); |
| 2425 | if (closure_blocking(&op->cl)) |
| 2426 | closure_sync(&op->cl); |
| 2427 | return ret; |
| 2428 | } |
| 2429 | |
| 2430 | static int bch_btree_map_keys_recurse(struct btree *b, struct btree_op *op, |
| 2431 | struct bkey *from, btree_map_keys_fn *fn, |
| 2432 | int flags) |
| 2433 | { |
| 2434 | int ret = MAP_CONTINUE; |
| 2435 | struct bkey *k; |
| 2436 | struct btree_iter iter; |
| 2437 | |
| 2438 | bch_btree_iter_init(b, &iter, from); |
| 2439 | |
| 2440 | while ((k = bch_btree_iter_next_filter(&iter, b, bch_ptr_bad))) { |
| 2441 | ret = !b->level |
| 2442 | ? fn(op, b, k) |
| 2443 | : btree(map_keys_recurse, k, b, op, from, fn, flags); |
| 2444 | from = NULL; |
| 2445 | |
| 2446 | if (ret != MAP_CONTINUE) |
| 2447 | return ret; |
| 2448 | } |
| 2449 | |
| 2450 | if (!b->level && (flags & MAP_END_KEY)) |
| 2451 | ret = fn(op, b, &KEY(KEY_INODE(&b->key), |
| 2452 | KEY_OFFSET(&b->key), 0)); |
| 2453 | |
| 2454 | return ret; |
| 2455 | } |
| 2456 | |
| 2457 | int bch_btree_map_keys(struct btree_op *op, struct cache_set *c, |
| 2458 | struct bkey *from, btree_map_keys_fn *fn, int flags) |
| 2459 | { |
| 2460 | int ret = btree_root(map_keys_recurse, c, op, from, fn, flags); |
| 2461 | if (closure_blocking(&op->cl)) |
| 2462 | closure_sync(&op->cl); |
| 2463 | return ret; |
| 2464 | } |
| 2465 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2466 | /* Keybuf code */ |
| 2467 | |
| 2468 | static inline int keybuf_cmp(struct keybuf_key *l, struct keybuf_key *r) |
| 2469 | { |
| 2470 | /* Overlapping keys compare equal */ |
| 2471 | if (bkey_cmp(&l->key, &START_KEY(&r->key)) <= 0) |
| 2472 | return -1; |
| 2473 | if (bkey_cmp(&START_KEY(&l->key), &r->key) >= 0) |
| 2474 | return 1; |
| 2475 | return 0; |
| 2476 | } |
| 2477 | |
| 2478 | static inline int keybuf_nonoverlapping_cmp(struct keybuf_key *l, |
| 2479 | struct keybuf_key *r) |
| 2480 | { |
| 2481 | return clamp_t(int64_t, bkey_cmp(&l->key, &r->key), -1, 1); |
| 2482 | } |
| 2483 | |
Kent Overstreet | 48dad8b | 2013-09-10 18:48:51 -0700 | [diff] [blame] | 2484 | struct refill { |
| 2485 | struct btree_op op; |
| 2486 | struct keybuf *buf; |
| 2487 | struct bkey *end; |
| 2488 | keybuf_pred_fn *pred; |
| 2489 | }; |
| 2490 | |
| 2491 | static int refill_keybuf_fn(struct btree_op *op, struct btree *b, |
| 2492 | struct bkey *k) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2493 | { |
Kent Overstreet | 48dad8b | 2013-09-10 18:48:51 -0700 | [diff] [blame] | 2494 | struct refill *refill = container_of(op, struct refill, op); |
| 2495 | struct keybuf *buf = refill->buf; |
| 2496 | int ret = MAP_CONTINUE; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2497 | |
Kent Overstreet | 48dad8b | 2013-09-10 18:48:51 -0700 | [diff] [blame] | 2498 | if (bkey_cmp(k, refill->end) >= 0) { |
| 2499 | ret = MAP_DONE; |
| 2500 | goto out; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2501 | } |
| 2502 | |
Kent Overstreet | 48dad8b | 2013-09-10 18:48:51 -0700 | [diff] [blame] | 2503 | if (!KEY_SIZE(k)) /* end key */ |
| 2504 | goto out; |
| 2505 | |
| 2506 | if (refill->pred(buf, k)) { |
| 2507 | struct keybuf_key *w; |
| 2508 | |
| 2509 | spin_lock(&buf->lock); |
| 2510 | |
| 2511 | w = array_alloc(&buf->freelist); |
| 2512 | if (!w) { |
| 2513 | spin_unlock(&buf->lock); |
| 2514 | return MAP_DONE; |
| 2515 | } |
| 2516 | |
| 2517 | w->private = NULL; |
| 2518 | bkey_copy(&w->key, k); |
| 2519 | |
| 2520 | if (RB_INSERT(&buf->keys, w, node, keybuf_cmp)) |
| 2521 | array_free(&buf->freelist, w); |
| 2522 | |
| 2523 | if (array_freelist_empty(&buf->freelist)) |
| 2524 | ret = MAP_DONE; |
| 2525 | |
| 2526 | spin_unlock(&buf->lock); |
| 2527 | } |
| 2528 | out: |
| 2529 | buf->last_scanned = *k; |
| 2530 | return ret; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2531 | } |
| 2532 | |
| 2533 | void bch_refill_keybuf(struct cache_set *c, struct keybuf *buf, |
Kent Overstreet | 72c2706 | 2013-06-05 06:24:39 -0700 | [diff] [blame] | 2534 | struct bkey *end, keybuf_pred_fn *pred) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2535 | { |
| 2536 | struct bkey start = buf->last_scanned; |
Kent Overstreet | 48dad8b | 2013-09-10 18:48:51 -0700 | [diff] [blame] | 2537 | struct refill refill; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2538 | |
| 2539 | cond_resched(); |
| 2540 | |
Kent Overstreet | 48dad8b | 2013-09-10 18:48:51 -0700 | [diff] [blame] | 2541 | bch_btree_op_init_stack(&refill.op); |
| 2542 | refill.buf = buf; |
| 2543 | refill.end = end; |
| 2544 | refill.pred = pred; |
| 2545 | |
| 2546 | bch_btree_map_keys(&refill.op, c, &buf->last_scanned, |
| 2547 | refill_keybuf_fn, MAP_END_KEY); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2548 | |
| 2549 | pr_debug("found %s keys from %llu:%llu to %llu:%llu", |
| 2550 | RB_EMPTY_ROOT(&buf->keys) ? "no" : |
| 2551 | array_freelist_empty(&buf->freelist) ? "some" : "a few", |
| 2552 | KEY_INODE(&start), KEY_OFFSET(&start), |
| 2553 | KEY_INODE(&buf->last_scanned), KEY_OFFSET(&buf->last_scanned)); |
| 2554 | |
| 2555 | spin_lock(&buf->lock); |
| 2556 | |
| 2557 | if (!RB_EMPTY_ROOT(&buf->keys)) { |
| 2558 | struct keybuf_key *w; |
| 2559 | w = RB_FIRST(&buf->keys, struct keybuf_key, node); |
| 2560 | buf->start = START_KEY(&w->key); |
| 2561 | |
| 2562 | w = RB_LAST(&buf->keys, struct keybuf_key, node); |
| 2563 | buf->end = w->key; |
| 2564 | } else { |
| 2565 | buf->start = MAX_KEY; |
| 2566 | buf->end = MAX_KEY; |
| 2567 | } |
| 2568 | |
| 2569 | spin_unlock(&buf->lock); |
| 2570 | } |
| 2571 | |
| 2572 | static void __bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w) |
| 2573 | { |
| 2574 | rb_erase(&w->node, &buf->keys); |
| 2575 | array_free(&buf->freelist, w); |
| 2576 | } |
| 2577 | |
| 2578 | void bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w) |
| 2579 | { |
| 2580 | spin_lock(&buf->lock); |
| 2581 | __bch_keybuf_del(buf, w); |
| 2582 | spin_unlock(&buf->lock); |
| 2583 | } |
| 2584 | |
| 2585 | bool bch_keybuf_check_overlapping(struct keybuf *buf, struct bkey *start, |
| 2586 | struct bkey *end) |
| 2587 | { |
| 2588 | bool ret = false; |
| 2589 | struct keybuf_key *p, *w, s; |
| 2590 | s.key = *start; |
| 2591 | |
| 2592 | if (bkey_cmp(end, &buf->start) <= 0 || |
| 2593 | bkey_cmp(start, &buf->end) >= 0) |
| 2594 | return false; |
| 2595 | |
| 2596 | spin_lock(&buf->lock); |
| 2597 | w = RB_GREATER(&buf->keys, s, node, keybuf_nonoverlapping_cmp); |
| 2598 | |
| 2599 | while (w && bkey_cmp(&START_KEY(&w->key), end) < 0) { |
| 2600 | p = w; |
| 2601 | w = RB_NEXT(w, node); |
| 2602 | |
| 2603 | if (p->private) |
| 2604 | ret = true; |
| 2605 | else |
| 2606 | __bch_keybuf_del(buf, p); |
| 2607 | } |
| 2608 | |
| 2609 | spin_unlock(&buf->lock); |
| 2610 | return ret; |
| 2611 | } |
| 2612 | |
| 2613 | struct keybuf_key *bch_keybuf_next(struct keybuf *buf) |
| 2614 | { |
| 2615 | struct keybuf_key *w; |
| 2616 | spin_lock(&buf->lock); |
| 2617 | |
| 2618 | w = RB_FIRST(&buf->keys, struct keybuf_key, node); |
| 2619 | |
| 2620 | while (w && w->private) |
| 2621 | w = RB_NEXT(w, node); |
| 2622 | |
| 2623 | if (w) |
| 2624 | w->private = ERR_PTR(-EINTR); |
| 2625 | |
| 2626 | spin_unlock(&buf->lock); |
| 2627 | return w; |
| 2628 | } |
| 2629 | |
| 2630 | struct keybuf_key *bch_keybuf_next_rescan(struct cache_set *c, |
Kent Overstreet | 48dad8b | 2013-09-10 18:48:51 -0700 | [diff] [blame] | 2631 | struct keybuf *buf, |
| 2632 | struct bkey *end, |
| 2633 | keybuf_pred_fn *pred) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2634 | { |
| 2635 | struct keybuf_key *ret; |
| 2636 | |
| 2637 | while (1) { |
| 2638 | ret = bch_keybuf_next(buf); |
| 2639 | if (ret) |
| 2640 | break; |
| 2641 | |
| 2642 | if (bkey_cmp(&buf->last_scanned, end) >= 0) { |
| 2643 | pr_debug("scan finished"); |
| 2644 | break; |
| 2645 | } |
| 2646 | |
Kent Overstreet | 72c2706 | 2013-06-05 06:24:39 -0700 | [diff] [blame] | 2647 | bch_refill_keybuf(c, buf, end, pred); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | return ret; |
| 2651 | } |
| 2652 | |
Kent Overstreet | 72c2706 | 2013-06-05 06:24:39 -0700 | [diff] [blame] | 2653 | void bch_keybuf_init(struct keybuf *buf) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2654 | { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2655 | buf->last_scanned = MAX_KEY; |
| 2656 | buf->keys = RB_ROOT; |
| 2657 | |
| 2658 | spin_lock_init(&buf->lock); |
| 2659 | array_allocator_init(&buf->freelist); |
| 2660 | } |
| 2661 | |
| 2662 | void bch_btree_exit(void) |
| 2663 | { |
| 2664 | if (btree_io_wq) |
| 2665 | destroy_workqueue(btree_io_wq); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2666 | } |
| 2667 | |
| 2668 | int __init bch_btree_init(void) |
| 2669 | { |
Kent Overstreet | 72a4451 | 2013-10-24 17:19:26 -0700 | [diff] [blame] | 2670 | btree_io_wq = create_singlethread_workqueue("bch_btree_io"); |
| 2671 | if (!btree_io_wq) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 2672 | return -ENOMEM; |
| 2673 | |
| 2674 | return 0; |
| 2675 | } |