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