2 * Copyright (C) 2010 Kent Overstreet <kent.overstreet@gmail.com>
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
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.
13 * Buckets have an 8 bit counter; freeing is accomplished by incrementing the
14 * counter. Garbage collection is used to remove stale pointers.
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.
20 * All configuration is done via sysfs; see Documentation/bcache.txt.
27 #include "writeback.h"
29 static void sort_key_next(struct btree_iter *iter,
30 struct btree_iter_set *i)
32 i->k = bkey_next(i->k);
35 *i = iter->data[--iter->used];
38 static bool bch_key_sort_cmp(struct btree_iter_set l,
39 struct btree_iter_set r)
41 int64_t c = bkey_cmp(l.k, r.k);
43 return c ? c > 0 : l.k < r.k;
46 static bool __ptr_invalid(struct cache_set *c, const struct bkey *k)
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));
56 if (KEY_SIZE(k) + r > c->sb.bucket_size ||
57 bucket < ca->sb.first_bucket ||
58 bucket >= ca->sb.nbuckets)
67 bool __bch_btree_ptr_invalid(struct cache_set *c, const struct bkey *k)
71 if (!KEY_PTRS(k) || !KEY_SIZE(k) || KEY_DIRTY(k))
74 if (__ptr_invalid(c, k))
79 bch_bkey_to_text(buf, sizeof(buf), k);
80 cache_bug(c, "spotted btree ptr %s: %s", buf, bch_ptr_status(c, k));
84 static bool bch_btree_ptr_invalid(struct btree *b, const struct bkey *k)
86 return __bch_btree_ptr_invalid(b->c, k);
89 static bool btree_ptr_bad_expensive(struct btree *b, const struct bkey *k)
95 if (mutex_trylock(&b->c->bucket_lock)) {
96 for (i = 0; i < KEY_PTRS(k); i++)
97 if (ptr_available(b->c, k, i)) {
98 g = PTR_BUCKET(b->c, k, i);
101 g->prio != BTREE_PRIO ||
102 (b->c->gc_mark_valid &&
103 GC_MARK(g) != GC_MARK_METADATA))
107 mutex_unlock(&b->c->bucket_lock);
112 mutex_unlock(&b->c->bucket_lock);
113 bch_bkey_to_text(buf, sizeof(buf), k);
115 "inconsistent btree pointer %s: bucket %li pin %i prio %i gen %i last_gc %i mark %llu gc_gen %i",
116 buf, PTR_BUCKET_NR(b->c, k, i), atomic_read(&g->pin),
117 g->prio, g->gen, g->last_gc, GC_MARK(g), g->gc_gen);
121 static bool bch_btree_ptr_bad(struct btree *b, const struct bkey *k)
125 if (!bkey_cmp(k, &ZERO_KEY) ||
127 bch_ptr_invalid(b, k))
130 for (i = 0; i < KEY_PTRS(k); i++)
131 if (!ptr_available(b->c, k, i) ||
132 ptr_stale(b->c, k, i))
135 if (expensive_debug_checks(b->c) &&
136 btree_ptr_bad_expensive(b, k))
142 const struct btree_keys_ops bch_btree_keys_ops = {
143 .sort_cmp = bch_key_sort_cmp,
144 .key_invalid = bch_btree_ptr_invalid,
145 .key_bad = bch_btree_ptr_bad,
151 * Returns true if l > r - unless l == r, in which case returns true if l is
154 * Necessary for btree_sort_fixup() - if there are multiple keys that compare
155 * equal in different sets, we have to process them newest to oldest.
157 static bool bch_extent_sort_cmp(struct btree_iter_set l,
158 struct btree_iter_set r)
160 int64_t c = bkey_cmp(&START_KEY(l.k), &START_KEY(r.k));
162 return c ? c > 0 : l.k < r.k;
165 static struct bkey *bch_extent_sort_fixup(struct btree_iter *iter,
168 while (iter->used > 1) {
169 struct btree_iter_set *top = iter->data, *i = top + 1;
171 if (iter->used > 2 &&
172 bch_extent_sort_cmp(i[0], i[1]))
175 if (bkey_cmp(top->k, &START_KEY(i->k)) <= 0)
178 if (!KEY_SIZE(i->k)) {
179 sort_key_next(iter, i);
180 heap_sift(iter, i - top, bch_extent_sort_cmp);
185 if (bkey_cmp(top->k, i->k) >= 0)
186 sort_key_next(iter, i);
188 bch_cut_front(top->k, i->k);
190 heap_sift(iter, i - top, bch_extent_sort_cmp);
192 /* can't happen because of comparison func */
193 BUG_ON(!bkey_cmp(&START_KEY(top->k), &START_KEY(i->k)));
195 if (bkey_cmp(i->k, top->k) < 0) {
196 bkey_copy(tmp, top->k);
198 bch_cut_back(&START_KEY(i->k), tmp);
199 bch_cut_front(i->k, top->k);
200 heap_sift(iter, 0, bch_extent_sort_cmp);
204 bch_cut_back(&START_KEY(i->k), top->k);
212 static bool bch_extent_invalid(struct btree *b, const struct bkey *k)
219 if (KEY_SIZE(k) > KEY_OFFSET(k))
222 if (__ptr_invalid(b->c, k))
227 bch_bkey_to_text(buf, sizeof(buf), k);
228 cache_bug(b->c, "spotted extent %s: %s", buf, bch_ptr_status(b->c, k));
232 static bool bch_extent_bad_expensive(struct btree *b, const struct bkey *k,
235 struct bucket *g = PTR_BUCKET(b->c, k, ptr);
238 if (mutex_trylock(&b->c->bucket_lock)) {
239 if (b->c->gc_mark_valid &&
240 ((GC_MARK(g) != GC_MARK_DIRTY &&
242 GC_MARK(g) == GC_MARK_METADATA))
245 if (g->prio == BTREE_PRIO)
248 mutex_unlock(&b->c->bucket_lock);
253 mutex_unlock(&b->c->bucket_lock);
254 bch_bkey_to_text(buf, sizeof(buf), k);
256 "inconsistent extent pointer %s:\nbucket %zu pin %i prio %i gen %i last_gc %i mark %llu gc_gen %i",
257 buf, PTR_BUCKET_NR(b->c, k, ptr), atomic_read(&g->pin),
258 g->prio, g->gen, g->last_gc, GC_MARK(g), g->gc_gen);
262 static bool bch_extent_bad(struct btree *b, const struct bkey *k)
268 bch_extent_invalid(b, k))
271 for (i = 0; i < KEY_PTRS(k); i++)
272 if (!ptr_available(b->c, k, i))
275 if (!expensive_debug_checks(b->c) && KEY_DIRTY(k))
278 for (i = 0; i < KEY_PTRS(k); i++) {
279 g = PTR_BUCKET(b->c, k, i);
280 stale = ptr_stale(b->c, k, i);
282 btree_bug_on(stale > 96, b,
283 "key too stale: %i, need_gc %u",
284 stale, b->c->need_gc);
286 btree_bug_on(stale && KEY_DIRTY(k) && KEY_SIZE(k),
287 b, "stale dirty pointer");
292 if (expensive_debug_checks(b->c) &&
293 bch_extent_bad_expensive(b, k, i))
300 static uint64_t merge_chksums(struct bkey *l, struct bkey *r)
302 return (l->ptr[KEY_PTRS(l)] + r->ptr[KEY_PTRS(r)]) &
303 ~((uint64_t)1 << 63);
306 static bool bch_extent_merge(struct btree *b, struct bkey *l, struct bkey *r)
310 if (key_merging_disabled(b->c))
313 if (KEY_PTRS(l) != KEY_PTRS(r) ||
314 KEY_DIRTY(l) != KEY_DIRTY(r) ||
315 bkey_cmp(l, &START_KEY(r)))
318 for (i = 0; i < KEY_PTRS(l); i++)
319 if (l->ptr[i] + PTR(0, KEY_SIZE(l), 0) != r->ptr[i] ||
320 PTR_BUCKET_NR(b->c, l, i) != PTR_BUCKET_NR(b->c, r, i))
323 /* Keys with no pointers aren't restricted to one bucket and could
326 if (KEY_SIZE(l) + KEY_SIZE(r) > USHRT_MAX) {
327 SET_KEY_OFFSET(l, KEY_OFFSET(l) + USHRT_MAX - KEY_SIZE(l));
328 SET_KEY_SIZE(l, USHRT_MAX);
336 l->ptr[KEY_PTRS(l)] = merge_chksums(l, r);
341 SET_KEY_OFFSET(l, KEY_OFFSET(l) + KEY_SIZE(r));
342 SET_KEY_SIZE(l, KEY_SIZE(l) + KEY_SIZE(r));
347 const struct btree_keys_ops bch_extent_keys_ops = {
348 .sort_cmp = bch_extent_sort_cmp,
349 .sort_fixup = bch_extent_sort_fixup,
350 .key_invalid = bch_extent_invalid,
351 .key_bad = bch_extent_bad,
352 .key_merge = bch_extent_merge,