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