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