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