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