Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 Kent Overstreet <kent.overstreet@gmail.com> |
| 3 | * |
| 4 | * Uses a block device as cache for other block devices; optimized for SSDs. |
| 5 | * All allocation is done in buckets, which should match the erase block size |
| 6 | * of the device. |
| 7 | * |
| 8 | * Buckets containing cached data are kept on a heap sorted by priority; |
| 9 | * bucket priority is increased on cache hit, and periodically all the buckets |
| 10 | * on the heap have their priority scaled down. This currently is just used as |
| 11 | * an LRU but in the future should allow for more intelligent heuristics. |
| 12 | * |
| 13 | * Buckets have an 8 bit counter; freeing is accomplished by incrementing the |
| 14 | * counter. Garbage collection is used to remove stale pointers. |
| 15 | * |
| 16 | * Indexing is done via a btree; nodes are not necessarily fully sorted, rather |
| 17 | * as keys are inserted we only sort the pages that have not yet been written. |
| 18 | * When garbage collection is run, we resort the entire node. |
| 19 | * |
| 20 | * All configuration is done via sysfs; see Documentation/bcache.txt. |
| 21 | */ |
| 22 | |
| 23 | #include "bcache.h" |
| 24 | #include "btree.h" |
| 25 | #include "debug.h" |
| 26 | #include "extents.h" |
| 27 | #include "writeback.h" |
| 28 | |
| 29 | static void sort_key_next(struct btree_iter *iter, |
| 30 | struct btree_iter_set *i) |
| 31 | { |
| 32 | i->k = bkey_next(i->k); |
| 33 | |
| 34 | if (i->k == i->end) |
| 35 | *i = iter->data[--iter->used]; |
| 36 | } |
| 37 | |
| 38 | static bool bch_key_sort_cmp(struct btree_iter_set l, |
| 39 | struct btree_iter_set r) |
| 40 | { |
| 41 | int64_t c = bkey_cmp(l.k, r.k); |
| 42 | |
| 43 | return c ? c > 0 : l.k < r.k; |
| 44 | } |
| 45 | |
| 46 | static bool __ptr_invalid(struct cache_set *c, const struct bkey *k) |
| 47 | { |
| 48 | unsigned i; |
| 49 | |
| 50 | for (i = 0; i < KEY_PTRS(k); i++) |
| 51 | if (ptr_available(c, k, i)) { |
| 52 | struct cache *ca = PTR_CACHE(c, k, i); |
| 53 | size_t bucket = PTR_BUCKET_NR(c, k, i); |
| 54 | size_t r = bucket_remainder(c, PTR_OFFSET(k, i)); |
| 55 | |
| 56 | if (KEY_SIZE(k) + r > c->sb.bucket_size || |
| 57 | bucket < ca->sb.first_bucket || |
| 58 | bucket >= ca->sb.nbuckets) |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | return false; |
| 63 | } |
| 64 | |
Kent Overstreet | dc9d98d | 2013-12-17 23:47:33 -0800 | [diff] [blame^] | 65 | /* Common among btree and extent ptrs */ |
| 66 | |
| 67 | static const char *bch_ptr_status(struct cache_set *c, const struct bkey *k) |
| 68 | { |
| 69 | unsigned i; |
| 70 | |
| 71 | for (i = 0; i < KEY_PTRS(k); i++) |
| 72 | if (ptr_available(c, k, i)) { |
| 73 | struct cache *ca = PTR_CACHE(c, k, i); |
| 74 | size_t bucket = PTR_BUCKET_NR(c, k, i); |
| 75 | size_t r = bucket_remainder(c, PTR_OFFSET(k, i)); |
| 76 | |
| 77 | if (KEY_SIZE(k) + r > c->sb.bucket_size) |
| 78 | return "bad, length too big"; |
| 79 | if (bucket < ca->sb.first_bucket) |
| 80 | return "bad, short offset"; |
| 81 | if (bucket >= ca->sb.nbuckets) |
| 82 | return "bad, offset past end of device"; |
| 83 | if (ptr_stale(c, k, i)) |
| 84 | return "stale"; |
| 85 | } |
| 86 | |
| 87 | if (!bkey_cmp(k, &ZERO_KEY)) |
| 88 | return "bad, null key"; |
| 89 | if (!KEY_PTRS(k)) |
| 90 | return "bad, no pointers"; |
| 91 | if (!KEY_SIZE(k)) |
| 92 | return "zeroed key"; |
| 93 | return ""; |
| 94 | } |
| 95 | |
| 96 | void bch_extent_to_text(char *buf, size_t size, const struct bkey *k) |
| 97 | { |
| 98 | unsigned i = 0; |
| 99 | char *out = buf, *end = buf + size; |
| 100 | |
| 101 | #define p(...) (out += scnprintf(out, end - out, __VA_ARGS__)) |
| 102 | |
| 103 | p("%llu:%llu len %llu -> [", KEY_INODE(k), KEY_START(k), KEY_SIZE(k)); |
| 104 | |
| 105 | for (i = 0; i < KEY_PTRS(k); i++) { |
| 106 | if (i) |
| 107 | p(", "); |
| 108 | |
| 109 | if (PTR_DEV(k, i) == PTR_CHECK_DEV) |
| 110 | p("check dev"); |
| 111 | else |
| 112 | p("%llu:%llu gen %llu", PTR_DEV(k, i), |
| 113 | PTR_OFFSET(k, i), PTR_GEN(k, i)); |
| 114 | } |
| 115 | |
| 116 | p("]"); |
| 117 | |
| 118 | if (KEY_DIRTY(k)) |
| 119 | p(" dirty"); |
| 120 | if (KEY_CSUM(k)) |
| 121 | p(" cs%llu %llx", KEY_CSUM(k), k->ptr[1]); |
| 122 | #undef p |
| 123 | } |
| 124 | |
| 125 | static void bch_bkey_dump(struct btree_keys *keys, const struct bkey *k) |
| 126 | { |
| 127 | struct btree *b = container_of(keys, struct btree, keys); |
| 128 | unsigned j; |
| 129 | char buf[80]; |
| 130 | |
| 131 | bch_extent_to_text(buf, sizeof(buf), k); |
| 132 | printk(" %s", buf); |
| 133 | |
| 134 | for (j = 0; j < KEY_PTRS(k); j++) { |
| 135 | size_t n = PTR_BUCKET_NR(b->c, k, j); |
| 136 | printk(" bucket %zu", n); |
| 137 | |
| 138 | if (n >= b->c->sb.first_bucket && n < b->c->sb.nbuckets) |
| 139 | printk(" prio %i", |
| 140 | PTR_BUCKET(b->c, k, j)->prio); |
| 141 | } |
| 142 | |
| 143 | printk(" %s\n", bch_ptr_status(b->c, k)); |
| 144 | } |
| 145 | |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 146 | /* Btree ptrs */ |
| 147 | |
| 148 | bool __bch_btree_ptr_invalid(struct cache_set *c, const struct bkey *k) |
| 149 | { |
| 150 | char buf[80]; |
| 151 | |
| 152 | if (!KEY_PTRS(k) || !KEY_SIZE(k) || KEY_DIRTY(k)) |
| 153 | goto bad; |
| 154 | |
| 155 | if (__ptr_invalid(c, k)) |
| 156 | goto bad; |
| 157 | |
| 158 | return false; |
| 159 | bad: |
Kent Overstreet | dc9d98d | 2013-12-17 23:47:33 -0800 | [diff] [blame^] | 160 | bch_extent_to_text(buf, sizeof(buf), k); |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 161 | cache_bug(c, "spotted btree ptr %s: %s", buf, bch_ptr_status(c, k)); |
| 162 | return true; |
| 163 | } |
| 164 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 165 | static bool bch_btree_ptr_invalid(struct btree_keys *bk, const struct bkey *k) |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 166 | { |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 167 | struct btree *b = container_of(bk, struct btree, keys); |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 168 | return __bch_btree_ptr_invalid(b->c, k); |
| 169 | } |
| 170 | |
| 171 | static bool btree_ptr_bad_expensive(struct btree *b, const struct bkey *k) |
| 172 | { |
| 173 | unsigned i; |
| 174 | char buf[80]; |
| 175 | struct bucket *g; |
| 176 | |
| 177 | if (mutex_trylock(&b->c->bucket_lock)) { |
| 178 | for (i = 0; i < KEY_PTRS(k); i++) |
| 179 | if (ptr_available(b->c, k, i)) { |
| 180 | g = PTR_BUCKET(b->c, k, i); |
| 181 | |
| 182 | if (KEY_DIRTY(k) || |
| 183 | g->prio != BTREE_PRIO || |
| 184 | (b->c->gc_mark_valid && |
| 185 | GC_MARK(g) != GC_MARK_METADATA)) |
| 186 | goto err; |
| 187 | } |
| 188 | |
| 189 | mutex_unlock(&b->c->bucket_lock); |
| 190 | } |
| 191 | |
| 192 | return false; |
| 193 | err: |
| 194 | mutex_unlock(&b->c->bucket_lock); |
Kent Overstreet | dc9d98d | 2013-12-17 23:47:33 -0800 | [diff] [blame^] | 195 | bch_extent_to_text(buf, sizeof(buf), k); |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 196 | btree_bug(b, |
| 197 | "inconsistent btree pointer %s: bucket %li pin %i prio %i gen %i last_gc %i mark %llu gc_gen %i", |
| 198 | buf, PTR_BUCKET_NR(b->c, k, i), atomic_read(&g->pin), |
| 199 | g->prio, g->gen, g->last_gc, GC_MARK(g), g->gc_gen); |
| 200 | return true; |
| 201 | } |
| 202 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 203 | static bool bch_btree_ptr_bad(struct btree_keys *bk, const struct bkey *k) |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 204 | { |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 205 | struct btree *b = container_of(bk, struct btree, keys); |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 206 | unsigned i; |
| 207 | |
| 208 | if (!bkey_cmp(k, &ZERO_KEY) || |
| 209 | !KEY_PTRS(k) || |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 210 | bch_ptr_invalid(bk, k)) |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 211 | return true; |
| 212 | |
| 213 | for (i = 0; i < KEY_PTRS(k); i++) |
| 214 | if (!ptr_available(b->c, k, i) || |
| 215 | ptr_stale(b->c, k, i)) |
| 216 | return true; |
| 217 | |
| 218 | if (expensive_debug_checks(b->c) && |
| 219 | btree_ptr_bad_expensive(b, k)) |
| 220 | return true; |
| 221 | |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | const struct btree_keys_ops bch_btree_keys_ops = { |
| 226 | .sort_cmp = bch_key_sort_cmp, |
| 227 | .key_invalid = bch_btree_ptr_invalid, |
| 228 | .key_bad = bch_btree_ptr_bad, |
Kent Overstreet | dc9d98d | 2013-12-17 23:47:33 -0800 | [diff] [blame^] | 229 | .key_to_text = bch_extent_to_text, |
| 230 | .key_dump = bch_bkey_dump, |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | /* Extents */ |
| 234 | |
| 235 | /* |
| 236 | * Returns true if l > r - unless l == r, in which case returns true if l is |
| 237 | * older than r. |
| 238 | * |
| 239 | * Necessary for btree_sort_fixup() - if there are multiple keys that compare |
| 240 | * equal in different sets, we have to process them newest to oldest. |
| 241 | */ |
| 242 | static bool bch_extent_sort_cmp(struct btree_iter_set l, |
| 243 | struct btree_iter_set r) |
| 244 | { |
| 245 | int64_t c = bkey_cmp(&START_KEY(l.k), &START_KEY(r.k)); |
| 246 | |
| 247 | return c ? c > 0 : l.k < r.k; |
| 248 | } |
| 249 | |
| 250 | static struct bkey *bch_extent_sort_fixup(struct btree_iter *iter, |
| 251 | struct bkey *tmp) |
| 252 | { |
| 253 | while (iter->used > 1) { |
| 254 | struct btree_iter_set *top = iter->data, *i = top + 1; |
| 255 | |
| 256 | if (iter->used > 2 && |
| 257 | bch_extent_sort_cmp(i[0], i[1])) |
| 258 | i++; |
| 259 | |
| 260 | if (bkey_cmp(top->k, &START_KEY(i->k)) <= 0) |
| 261 | break; |
| 262 | |
| 263 | if (!KEY_SIZE(i->k)) { |
| 264 | sort_key_next(iter, i); |
| 265 | heap_sift(iter, i - top, bch_extent_sort_cmp); |
| 266 | continue; |
| 267 | } |
| 268 | |
| 269 | if (top->k > i->k) { |
| 270 | if (bkey_cmp(top->k, i->k) >= 0) |
| 271 | sort_key_next(iter, i); |
| 272 | else |
| 273 | bch_cut_front(top->k, i->k); |
| 274 | |
| 275 | heap_sift(iter, i - top, bch_extent_sort_cmp); |
| 276 | } else { |
| 277 | /* can't happen because of comparison func */ |
| 278 | BUG_ON(!bkey_cmp(&START_KEY(top->k), &START_KEY(i->k))); |
| 279 | |
| 280 | if (bkey_cmp(i->k, top->k) < 0) { |
| 281 | bkey_copy(tmp, top->k); |
| 282 | |
| 283 | bch_cut_back(&START_KEY(i->k), tmp); |
| 284 | bch_cut_front(i->k, top->k); |
| 285 | heap_sift(iter, 0, bch_extent_sort_cmp); |
| 286 | |
| 287 | return tmp; |
| 288 | } else { |
| 289 | bch_cut_back(&START_KEY(i->k), top->k); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return NULL; |
| 295 | } |
| 296 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 297 | static bool bch_extent_invalid(struct btree_keys *bk, const struct bkey *k) |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 298 | { |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 299 | struct btree *b = container_of(bk, struct btree, keys); |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 300 | char buf[80]; |
| 301 | |
| 302 | if (!KEY_SIZE(k)) |
| 303 | return true; |
| 304 | |
| 305 | if (KEY_SIZE(k) > KEY_OFFSET(k)) |
| 306 | goto bad; |
| 307 | |
| 308 | if (__ptr_invalid(b->c, k)) |
| 309 | goto bad; |
| 310 | |
| 311 | return false; |
| 312 | bad: |
Kent Overstreet | dc9d98d | 2013-12-17 23:47:33 -0800 | [diff] [blame^] | 313 | bch_extent_to_text(buf, sizeof(buf), k); |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 314 | cache_bug(b->c, "spotted extent %s: %s", buf, bch_ptr_status(b->c, k)); |
| 315 | return true; |
| 316 | } |
| 317 | |
| 318 | static bool bch_extent_bad_expensive(struct btree *b, const struct bkey *k, |
| 319 | unsigned ptr) |
| 320 | { |
| 321 | struct bucket *g = PTR_BUCKET(b->c, k, ptr); |
| 322 | char buf[80]; |
| 323 | |
| 324 | if (mutex_trylock(&b->c->bucket_lock)) { |
| 325 | if (b->c->gc_mark_valid && |
| 326 | ((GC_MARK(g) != GC_MARK_DIRTY && |
| 327 | KEY_DIRTY(k)) || |
| 328 | GC_MARK(g) == GC_MARK_METADATA)) |
| 329 | goto err; |
| 330 | |
| 331 | if (g->prio == BTREE_PRIO) |
| 332 | goto err; |
| 333 | |
| 334 | mutex_unlock(&b->c->bucket_lock); |
| 335 | } |
| 336 | |
| 337 | return false; |
| 338 | err: |
| 339 | mutex_unlock(&b->c->bucket_lock); |
Kent Overstreet | dc9d98d | 2013-12-17 23:47:33 -0800 | [diff] [blame^] | 340 | bch_extent_to_text(buf, sizeof(buf), k); |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 341 | btree_bug(b, |
| 342 | "inconsistent extent pointer %s:\nbucket %zu pin %i prio %i gen %i last_gc %i mark %llu gc_gen %i", |
| 343 | buf, PTR_BUCKET_NR(b->c, k, ptr), atomic_read(&g->pin), |
| 344 | g->prio, g->gen, g->last_gc, GC_MARK(g), g->gc_gen); |
| 345 | return true; |
| 346 | } |
| 347 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 348 | static bool bch_extent_bad(struct btree_keys *bk, const struct bkey *k) |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 349 | { |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 350 | struct btree *b = container_of(bk, struct btree, keys); |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 351 | struct bucket *g; |
| 352 | unsigned i, stale; |
| 353 | |
| 354 | if (!KEY_PTRS(k) || |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 355 | bch_extent_invalid(bk, k)) |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 356 | return true; |
| 357 | |
| 358 | for (i = 0; i < KEY_PTRS(k); i++) |
| 359 | if (!ptr_available(b->c, k, i)) |
| 360 | return true; |
| 361 | |
| 362 | if (!expensive_debug_checks(b->c) && KEY_DIRTY(k)) |
| 363 | return false; |
| 364 | |
| 365 | for (i = 0; i < KEY_PTRS(k); i++) { |
| 366 | g = PTR_BUCKET(b->c, k, i); |
| 367 | stale = ptr_stale(b->c, k, i); |
| 368 | |
| 369 | btree_bug_on(stale > 96, b, |
| 370 | "key too stale: %i, need_gc %u", |
| 371 | stale, b->c->need_gc); |
| 372 | |
| 373 | btree_bug_on(stale && KEY_DIRTY(k) && KEY_SIZE(k), |
| 374 | b, "stale dirty pointer"); |
| 375 | |
| 376 | if (stale) |
| 377 | return true; |
| 378 | |
| 379 | if (expensive_debug_checks(b->c) && |
| 380 | bch_extent_bad_expensive(b, k, i)) |
| 381 | return true; |
| 382 | } |
| 383 | |
| 384 | return false; |
| 385 | } |
| 386 | |
| 387 | static uint64_t merge_chksums(struct bkey *l, struct bkey *r) |
| 388 | { |
| 389 | return (l->ptr[KEY_PTRS(l)] + r->ptr[KEY_PTRS(r)]) & |
| 390 | ~((uint64_t)1 << 63); |
| 391 | } |
| 392 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 393 | static bool bch_extent_merge(struct btree_keys *bk, struct bkey *l, struct bkey *r) |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 394 | { |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 395 | struct btree *b = container_of(bk, struct btree, keys); |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 396 | unsigned i; |
| 397 | |
| 398 | if (key_merging_disabled(b->c)) |
| 399 | return false; |
| 400 | |
| 401 | if (KEY_PTRS(l) != KEY_PTRS(r) || |
| 402 | KEY_DIRTY(l) != KEY_DIRTY(r) || |
| 403 | bkey_cmp(l, &START_KEY(r))) |
| 404 | return false; |
| 405 | |
| 406 | for (i = 0; i < KEY_PTRS(l); i++) |
| 407 | if (l->ptr[i] + PTR(0, KEY_SIZE(l), 0) != r->ptr[i] || |
| 408 | PTR_BUCKET_NR(b->c, l, i) != PTR_BUCKET_NR(b->c, r, i)) |
| 409 | return false; |
| 410 | |
| 411 | /* Keys with no pointers aren't restricted to one bucket and could |
| 412 | * overflow KEY_SIZE |
| 413 | */ |
| 414 | if (KEY_SIZE(l) + KEY_SIZE(r) > USHRT_MAX) { |
| 415 | SET_KEY_OFFSET(l, KEY_OFFSET(l) + USHRT_MAX - KEY_SIZE(l)); |
| 416 | SET_KEY_SIZE(l, USHRT_MAX); |
| 417 | |
| 418 | bch_cut_front(l, r); |
| 419 | return false; |
| 420 | } |
| 421 | |
| 422 | if (KEY_CSUM(l)) { |
| 423 | if (KEY_CSUM(r)) |
| 424 | l->ptr[KEY_PTRS(l)] = merge_chksums(l, r); |
| 425 | else |
| 426 | SET_KEY_CSUM(l, 0); |
| 427 | } |
| 428 | |
| 429 | SET_KEY_OFFSET(l, KEY_OFFSET(l) + KEY_SIZE(r)); |
| 430 | SET_KEY_SIZE(l, KEY_SIZE(l) + KEY_SIZE(r)); |
| 431 | |
| 432 | return true; |
| 433 | } |
| 434 | |
| 435 | const struct btree_keys_ops bch_extent_keys_ops = { |
| 436 | .sort_cmp = bch_extent_sort_cmp, |
| 437 | .sort_fixup = bch_extent_sort_fixup, |
| 438 | .key_invalid = bch_extent_invalid, |
| 439 | .key_bad = bch_extent_bad, |
| 440 | .key_merge = bch_extent_merge, |
Kent Overstreet | dc9d98d | 2013-12-17 23:47:33 -0800 | [diff] [blame^] | 441 | .key_to_text = bch_extent_to_text, |
| 442 | .key_dump = bch_bkey_dump, |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 443 | .is_extents = true, |
| 444 | }; |