blob: 38428f46ea745f1dc511cf88e69eddef75c1c8ab [file] [log] [blame]
Kent Overstreetcafe5632013-03-23 16:11:31 -07001/*
2 * Primary bucket allocation code
3 *
4 * Copyright 2012 Google, Inc.
5 *
6 * Allocation in bcache is done in terms of buckets:
7 *
8 * Each bucket has associated an 8 bit gen; this gen corresponds to the gen in
9 * btree pointers - they must match for the pointer to be considered valid.
10 *
11 * Thus (assuming a bucket has no dirty data or metadata in it) we can reuse a
12 * bucket simply by incrementing its gen.
13 *
14 * The gens (along with the priorities; it's really the gens are important but
15 * the code is named as if it's the priorities) are written in an arbitrary list
16 * of buckets on disk, with a pointer to them in the journal header.
17 *
18 * When we invalidate a bucket, we have to write its new gen to disk and wait
19 * for that write to complete before we use it - otherwise after a crash we
20 * could have pointers that appeared to be good but pointed to data that had
21 * been overwritten.
22 *
23 * Since the gens and priorities are all stored contiguously on disk, we can
24 * batch this up: We fill up the free_inc list with freshly invalidated buckets,
25 * call prio_write(), and when prio_write() finishes we pull buckets off the
26 * free_inc list and optionally discard them.
27 *
28 * free_inc isn't the only freelist - if it was, we'd often to sleep while
29 * priorities and gens were being written before we could allocate. c->free is a
30 * smaller freelist, and buckets on that list are always ready to be used.
31 *
32 * If we've got discards enabled, that happens when a bucket moves from the
33 * free_inc list to the free list.
34 *
35 * There is another freelist, because sometimes we have buckets that we know
36 * have nothing pointing into them - these we can reuse without waiting for
37 * priorities to be rewritten. These come from freed btree nodes and buckets
38 * that garbage collection discovered no longer had valid keys pointing into
39 * them (because they were overwritten). That's the unused list - buckets on the
40 * unused list move to the free list, optionally being discarded in the process.
41 *
42 * It's also important to ensure that gens don't wrap around - with respect to
43 * either the oldest gen in the btree or the gen on disk. This is quite
44 * difficult to do in practice, but we explicitly guard against it anyways - if
45 * a bucket is in danger of wrapping around we simply skip invalidating it that
46 * time around, and we garbage collect or rewrite the priorities sooner than we
47 * would have otherwise.
48 *
49 * bch_bucket_alloc() allocates a single bucket from a specific cache.
50 *
51 * bch_bucket_alloc_set() allocates one or more buckets from different caches
52 * out of a cache set.
53 *
54 * free_some_buckets() drives all the processes described above. It's called
55 * from bch_bucket_alloc() and a few other places that need to make sure free
56 * buckets are ready.
57 *
58 * invalidate_buckets_(lru|fifo)() find buckets that are available to be
59 * invalidated, and then invalidate them and stick them on the free_inc list -
60 * in either lru or fifo order.
61 */
62
63#include "bcache.h"
64#include "btree.h"
65
Kent Overstreet119ba0f2013-04-24 19:01:12 -070066#include <linux/kthread.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070067#include <linux/random.h>
68
69#define MAX_IN_FLIGHT_DISCARDS 8U
70
71/* Bucket heap / gen */
72
73uint8_t bch_inc_gen(struct cache *ca, struct bucket *b)
74{
75 uint8_t ret = ++b->gen;
76
77 ca->set->need_gc = max(ca->set->need_gc, bucket_gc_gen(b));
78 WARN_ON_ONCE(ca->set->need_gc > BUCKET_GC_GEN_MAX);
79
80 if (CACHE_SYNC(&ca->set->sb)) {
81 ca->need_save_prio = max(ca->need_save_prio,
82 bucket_disk_gen(b));
83 WARN_ON_ONCE(ca->need_save_prio > BUCKET_DISK_GEN_MAX);
84 }
85
86 return ret;
87}
88
89void bch_rescale_priorities(struct cache_set *c, int sectors)
90{
91 struct cache *ca;
92 struct bucket *b;
93 unsigned next = c->nbuckets * c->sb.bucket_size / 1024;
94 unsigned i;
95 int r;
96
97 atomic_sub(sectors, &c->rescale);
98
99 do {
100 r = atomic_read(&c->rescale);
101
102 if (r >= 0)
103 return;
104 } while (atomic_cmpxchg(&c->rescale, r, r + next) != r);
105
106 mutex_lock(&c->bucket_lock);
107
108 c->min_prio = USHRT_MAX;
109
110 for_each_cache(ca, c, i)
111 for_each_bucket(b, ca)
112 if (b->prio &&
113 b->prio != BTREE_PRIO &&
114 !atomic_read(&b->pin)) {
115 b->prio--;
116 c->min_prio = min(c->min_prio, b->prio);
117 }
118
119 mutex_unlock(&c->bucket_lock);
120}
121
122/* Discard/TRIM */
123
124struct discard {
125 struct list_head list;
126 struct work_struct work;
127 struct cache *ca;
128 long bucket;
129
130 struct bio bio;
131 struct bio_vec bv;
132};
133
134static void discard_finish(struct work_struct *w)
135{
136 struct discard *d = container_of(w, struct discard, work);
137 struct cache *ca = d->ca;
138 char buf[BDEVNAME_SIZE];
139
140 if (!test_bit(BIO_UPTODATE, &d->bio.bi_flags)) {
141 pr_notice("discard error on %s, disabling",
142 bdevname(ca->bdev, buf));
143 d->ca->discard = 0;
144 }
145
146 mutex_lock(&ca->set->bucket_lock);
147
148 fifo_push(&ca->free, d->bucket);
149 list_add(&d->list, &ca->discards);
150 atomic_dec(&ca->discards_in_flight);
151
152 mutex_unlock(&ca->set->bucket_lock);
153
154 closure_wake_up(&ca->set->bucket_wait);
Kent Overstreet119ba0f2013-04-24 19:01:12 -0700155 wake_up_process(ca->alloc_thread);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700156
157 closure_put(&ca->set->cl);
158}
159
160static void discard_endio(struct bio *bio, int error)
161{
162 struct discard *d = container_of(bio, struct discard, bio);
163 schedule_work(&d->work);
164}
165
166static void do_discard(struct cache *ca, long bucket)
167{
168 struct discard *d = list_first_entry(&ca->discards,
169 struct discard, list);
170
171 list_del(&d->list);
172 d->bucket = bucket;
173
174 atomic_inc(&ca->discards_in_flight);
175 closure_get(&ca->set->cl);
176
177 bio_init(&d->bio);
178
179 d->bio.bi_sector = bucket_to_sector(ca->set, d->bucket);
180 d->bio.bi_bdev = ca->bdev;
181 d->bio.bi_rw = REQ_WRITE|REQ_DISCARD;
182 d->bio.bi_max_vecs = 1;
183 d->bio.bi_io_vec = d->bio.bi_inline_vecs;
184 d->bio.bi_size = bucket_bytes(ca);
185 d->bio.bi_end_io = discard_endio;
186 bio_set_prio(&d->bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
187
188 submit_bio(0, &d->bio);
189}
190
191/* Allocation */
192
193static inline bool can_inc_bucket_gen(struct bucket *b)
194{
195 return bucket_gc_gen(b) < BUCKET_GC_GEN_MAX &&
196 bucket_disk_gen(b) < BUCKET_DISK_GEN_MAX;
197}
198
199bool bch_bucket_add_unused(struct cache *ca, struct bucket *b)
200{
201 BUG_ON(GC_MARK(b) || GC_SECTORS_USED(b));
202
203 if (fifo_used(&ca->free) > ca->watermark[WATERMARK_MOVINGGC] &&
204 CACHE_REPLACEMENT(&ca->sb) == CACHE_REPLACEMENT_FIFO)
205 return false;
206
207 b->prio = 0;
208
209 if (can_inc_bucket_gen(b) &&
210 fifo_push(&ca->unused, b - ca->buckets)) {
211 atomic_inc(&b->pin);
212 return true;
213 }
214
215 return false;
216}
217
218static bool can_invalidate_bucket(struct cache *ca, struct bucket *b)
219{
220 return GC_MARK(b) == GC_MARK_RECLAIMABLE &&
221 !atomic_read(&b->pin) &&
222 can_inc_bucket_gen(b);
223}
224
225static void invalidate_one_bucket(struct cache *ca, struct bucket *b)
226{
227 bch_inc_gen(ca, b);
228 b->prio = INITIAL_PRIO;
229 atomic_inc(&b->pin);
230 fifo_push(&ca->free_inc, b - ca->buckets);
231}
232
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700233#define bucket_prio(b) \
234 (((unsigned) (b->prio - ca->set->min_prio)) * GC_SECTORS_USED(b))
235
236#define bucket_max_cmp(l, r) (bucket_prio(l) < bucket_prio(r))
237#define bucket_min_cmp(l, r) (bucket_prio(l) > bucket_prio(r))
238
Kent Overstreetcafe5632013-03-23 16:11:31 -0700239static void invalidate_buckets_lru(struct cache *ca)
240{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700241 struct bucket *b;
242 ssize_t i;
243
244 ca->heap.used = 0;
245
246 for_each_bucket(b, ca) {
Kent Overstreet86b26b82013-04-30 19:14:40 -0700247 /*
248 * If we fill up the unused list, if we then return before
249 * adding anything to the free_inc list we'll skip writing
250 * prios/gens and just go back to allocating from the unused
251 * list:
252 */
253 if (fifo_full(&ca->unused))
254 return;
255
Kent Overstreetcafe5632013-03-23 16:11:31 -0700256 if (!can_invalidate_bucket(ca, b))
257 continue;
258
Kent Overstreet86b26b82013-04-30 19:14:40 -0700259 if (!GC_SECTORS_USED(b) &&
260 bch_bucket_add_unused(ca, b))
261 continue;
262
263 if (!heap_full(&ca->heap))
264 heap_add(&ca->heap, b, bucket_max_cmp);
265 else if (bucket_max_cmp(b, heap_peek(&ca->heap))) {
266 ca->heap.data[0] = b;
267 heap_sift(&ca->heap, 0, bucket_max_cmp);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700268 }
269 }
270
Kent Overstreetcafe5632013-03-23 16:11:31 -0700271 for (i = ca->heap.used / 2 - 1; i >= 0; --i)
272 heap_sift(&ca->heap, i, bucket_min_cmp);
273
274 while (!fifo_full(&ca->free_inc)) {
275 if (!heap_pop(&ca->heap, b, bucket_min_cmp)) {
Kent Overstreet86b26b82013-04-30 19:14:40 -0700276 /*
277 * We don't want to be calling invalidate_buckets()
Kent Overstreetcafe5632013-03-23 16:11:31 -0700278 * multiple times when it can't do anything
279 */
280 ca->invalidate_needs_gc = 1;
281 bch_queue_gc(ca->set);
282 return;
283 }
284
285 invalidate_one_bucket(ca, b);
286 }
287}
288
289static void invalidate_buckets_fifo(struct cache *ca)
290{
291 struct bucket *b;
292 size_t checked = 0;
293
294 while (!fifo_full(&ca->free_inc)) {
295 if (ca->fifo_last_bucket < ca->sb.first_bucket ||
296 ca->fifo_last_bucket >= ca->sb.nbuckets)
297 ca->fifo_last_bucket = ca->sb.first_bucket;
298
299 b = ca->buckets + ca->fifo_last_bucket++;
300
301 if (can_invalidate_bucket(ca, b))
302 invalidate_one_bucket(ca, b);
303
304 if (++checked >= ca->sb.nbuckets) {
305 ca->invalidate_needs_gc = 1;
306 bch_queue_gc(ca->set);
307 return;
308 }
309 }
310}
311
312static void invalidate_buckets_random(struct cache *ca)
313{
314 struct bucket *b;
315 size_t checked = 0;
316
317 while (!fifo_full(&ca->free_inc)) {
318 size_t n;
319 get_random_bytes(&n, sizeof(n));
320
321 n %= (size_t) (ca->sb.nbuckets - ca->sb.first_bucket);
322 n += ca->sb.first_bucket;
323
324 b = ca->buckets + n;
325
326 if (can_invalidate_bucket(ca, b))
327 invalidate_one_bucket(ca, b);
328
329 if (++checked >= ca->sb.nbuckets / 2) {
330 ca->invalidate_needs_gc = 1;
331 bch_queue_gc(ca->set);
332 return;
333 }
334 }
335}
336
337static void invalidate_buckets(struct cache *ca)
338{
339 if (ca->invalidate_needs_gc)
340 return;
341
342 switch (CACHE_REPLACEMENT(&ca->sb)) {
343 case CACHE_REPLACEMENT_LRU:
344 invalidate_buckets_lru(ca);
345 break;
346 case CACHE_REPLACEMENT_FIFO:
347 invalidate_buckets_fifo(ca);
348 break;
349 case CACHE_REPLACEMENT_RANDOM:
350 invalidate_buckets_random(ca);
351 break;
352 }
Kent Overstreet86b26b82013-04-30 19:14:40 -0700353
354 pr_debug("free %zu/%zu free_inc %zu/%zu unused %zu/%zu",
355 fifo_used(&ca->free), ca->free.size,
356 fifo_used(&ca->free_inc), ca->free_inc.size,
357 fifo_used(&ca->unused), ca->unused.size);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700358}
359
360#define allocator_wait(ca, cond) \
361do { \
Kent Overstreet86b26b82013-04-30 19:14:40 -0700362 while (1) { \
Kent Overstreet119ba0f2013-04-24 19:01:12 -0700363 set_current_state(TASK_INTERRUPTIBLE); \
Kent Overstreet86b26b82013-04-30 19:14:40 -0700364 if (cond) \
365 break; \
Kent Overstreetcafe5632013-03-23 16:11:31 -0700366 \
367 mutex_unlock(&(ca)->set->bucket_lock); \
368 if (test_bit(CACHE_SET_STOPPING_2, &ca->set->flags)) { \
Kent Overstreet119ba0f2013-04-24 19:01:12 -0700369 closure_put(&ca->set->cl); \
370 return 0; \
Kent Overstreetcafe5632013-03-23 16:11:31 -0700371 } \
372 \
373 schedule(); \
Kent Overstreetcafe5632013-03-23 16:11:31 -0700374 mutex_lock(&(ca)->set->bucket_lock); \
375 } \
Kent Overstreet119ba0f2013-04-24 19:01:12 -0700376 __set_current_state(TASK_RUNNING); \
Kent Overstreetcafe5632013-03-23 16:11:31 -0700377} while (0)
378
Kent Overstreet119ba0f2013-04-24 19:01:12 -0700379static int bch_allocator_thread(void *arg)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700380{
Kent Overstreet119ba0f2013-04-24 19:01:12 -0700381 struct cache *ca = arg;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700382
383 mutex_lock(&ca->set->bucket_lock);
384
385 while (1) {
Kent Overstreet86b26b82013-04-30 19:14:40 -0700386 /*
387 * First, we pull buckets off of the unused and free_inc lists,
388 * possibly issue discards to them, then we add the bucket to
389 * the free list:
390 */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700391 while (1) {
392 long bucket;
393
394 if ((!atomic_read(&ca->set->prio_blocked) ||
395 !CACHE_SYNC(&ca->set->sb)) &&
396 !fifo_empty(&ca->unused))
397 fifo_pop(&ca->unused, bucket);
398 else if (!fifo_empty(&ca->free_inc))
399 fifo_pop(&ca->free_inc, bucket);
400 else
401 break;
402
403 allocator_wait(ca, (int) fifo_free(&ca->free) >
404 atomic_read(&ca->discards_in_flight));
405
406 if (ca->discard) {
407 allocator_wait(ca, !list_empty(&ca->discards));
408 do_discard(ca, bucket);
409 } else {
410 fifo_push(&ca->free, bucket);
411 closure_wake_up(&ca->set->bucket_wait);
412 }
413 }
414
Kent Overstreet86b26b82013-04-30 19:14:40 -0700415 /*
416 * We've run out of free buckets, we need to find some buckets
417 * we can invalidate. First, invalidate them in memory and add
418 * them to the free_inc list:
419 */
420
421 allocator_wait(ca, ca->set->gc_mark_valid &&
422 (ca->need_save_prio > 64 ||
423 !ca->invalidate_needs_gc));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700424 invalidate_buckets(ca);
425
Kent Overstreet86b26b82013-04-30 19:14:40 -0700426 /*
427 * Now, we write their new gens to disk so we can start writing
428 * new stuff to them:
429 */
430 allocator_wait(ca, !atomic_read(&ca->set->prio_blocked));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700431 if (CACHE_SYNC(&ca->set->sb) &&
432 (!fifo_empty(&ca->free_inc) ||
Kent Overstreet86b26b82013-04-30 19:14:40 -0700433 ca->need_save_prio > 64))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700434 bch_prio_write(ca);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700435 }
436}
437
438long bch_bucket_alloc(struct cache *ca, unsigned watermark, struct closure *cl)
439{
440 long r = -1;
441again:
Kent Overstreet119ba0f2013-04-24 19:01:12 -0700442 wake_up_process(ca->alloc_thread);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700443
444 if (fifo_used(&ca->free) > ca->watermark[watermark] &&
445 fifo_pop(&ca->free, r)) {
446 struct bucket *b = ca->buckets + r;
447#ifdef CONFIG_BCACHE_EDEBUG
448 size_t iter;
449 long i;
450
451 for (iter = 0; iter < prio_buckets(ca) * 2; iter++)
452 BUG_ON(ca->prio_buckets[iter] == (uint64_t) r);
453
454 fifo_for_each(i, &ca->free, iter)
455 BUG_ON(i == r);
456 fifo_for_each(i, &ca->free_inc, iter)
457 BUG_ON(i == r);
458 fifo_for_each(i, &ca->unused, iter)
459 BUG_ON(i == r);
460#endif
461 BUG_ON(atomic_read(&b->pin) != 1);
462
463 SET_GC_SECTORS_USED(b, ca->sb.bucket_size);
464
465 if (watermark <= WATERMARK_METADATA) {
466 SET_GC_MARK(b, GC_MARK_METADATA);
467 b->prio = BTREE_PRIO;
468 } else {
469 SET_GC_MARK(b, GC_MARK_RECLAIMABLE);
470 b->prio = INITIAL_PRIO;
471 }
472
473 return r;
474 }
475
476 pr_debug("alloc failure: blocked %i free %zu free_inc %zu unused %zu",
477 atomic_read(&ca->set->prio_blocked), fifo_used(&ca->free),
478 fifo_used(&ca->free_inc), fifo_used(&ca->unused));
479
480 if (cl) {
481 closure_wait(&ca->set->bucket_wait, cl);
482
483 if (closure_blocking(cl)) {
484 mutex_unlock(&ca->set->bucket_lock);
485 closure_sync(cl);
486 mutex_lock(&ca->set->bucket_lock);
487 goto again;
488 }
489 }
490
491 return -1;
492}
493
494void bch_bucket_free(struct cache_set *c, struct bkey *k)
495{
496 unsigned i;
497
498 for (i = 0; i < KEY_PTRS(k); i++) {
499 struct bucket *b = PTR_BUCKET(c, k, i);
500
Kent Overstreet86b26b82013-04-30 19:14:40 -0700501 SET_GC_MARK(b, GC_MARK_RECLAIMABLE);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700502 SET_GC_SECTORS_USED(b, 0);
503 bch_bucket_add_unused(PTR_CACHE(c, k, i), b);
504 }
505}
506
507int __bch_bucket_alloc_set(struct cache_set *c, unsigned watermark,
508 struct bkey *k, int n, struct closure *cl)
509{
510 int i;
511
512 lockdep_assert_held(&c->bucket_lock);
513 BUG_ON(!n || n > c->caches_loaded || n > 8);
514
515 bkey_init(k);
516
517 /* sort by free space/prio of oldest data in caches */
518
519 for (i = 0; i < n; i++) {
520 struct cache *ca = c->cache_by_alloc[i];
521 long b = bch_bucket_alloc(ca, watermark, cl);
522
523 if (b == -1)
524 goto err;
525
526 k->ptr[i] = PTR(ca->buckets[b].gen,
527 bucket_to_sector(c, b),
528 ca->sb.nr_this_dev);
529
530 SET_KEY_PTRS(k, i + 1);
531 }
532
533 return 0;
534err:
535 bch_bucket_free(c, k);
536 __bkey_put(c, k);
537 return -1;
538}
539
540int bch_bucket_alloc_set(struct cache_set *c, unsigned watermark,
541 struct bkey *k, int n, struct closure *cl)
542{
543 int ret;
544 mutex_lock(&c->bucket_lock);
545 ret = __bch_bucket_alloc_set(c, watermark, k, n, cl);
546 mutex_unlock(&c->bucket_lock);
547 return ret;
548}
549
550/* Init */
551
Kent Overstreet119ba0f2013-04-24 19:01:12 -0700552int bch_cache_allocator_start(struct cache *ca)
553{
554 ca->alloc_thread = kthread_create(bch_allocator_thread,
555 ca, "bcache_allocator");
556 if (IS_ERR(ca->alloc_thread))
557 return PTR_ERR(ca->alloc_thread);
558
559 closure_get(&ca->set->cl);
560 wake_up_process(ca->alloc_thread);
561
562 return 0;
563}
564
Kent Overstreetcafe5632013-03-23 16:11:31 -0700565void bch_cache_allocator_exit(struct cache *ca)
566{
567 struct discard *d;
568
569 while (!list_empty(&ca->discards)) {
570 d = list_first_entry(&ca->discards, struct discard, list);
571 cancel_work_sync(&d->work);
572 list_del(&d->list);
573 kfree(d);
574 }
575}
576
577int bch_cache_allocator_init(struct cache *ca)
578{
579 unsigned i;
580
581 /*
582 * Reserve:
583 * Prio/gen writes first
584 * Then 8 for btree allocations
585 * Then half for the moving garbage collector
586 */
587
588 ca->watermark[WATERMARK_PRIO] = 0;
589
590 ca->watermark[WATERMARK_METADATA] = prio_buckets(ca);
591
592 ca->watermark[WATERMARK_MOVINGGC] = 8 +
593 ca->watermark[WATERMARK_METADATA];
594
595 ca->watermark[WATERMARK_NONE] = ca->free.size / 2 +
596 ca->watermark[WATERMARK_MOVINGGC];
597
598 for (i = 0; i < MAX_IN_FLIGHT_DISCARDS; i++) {
599 struct discard *d = kzalloc(sizeof(*d), GFP_KERNEL);
600 if (!d)
601 return -ENOMEM;
602
603 d->ca = ca;
604 INIT_WORK(&d->work, discard_finish);
605 list_add(&d->list, &ca->discards);
606 }
607
608 return 0;
609}