movinggc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Moving/copying garbage collector
  3. *
  4. * Copyright 2012 Google, Inc.
  5. */
  6. #include "bcache.h"
  7. #include "btree.h"
  8. #include "debug.h"
  9. #include "request.h"
  10. #include <trace/events/bcache.h>
  11. struct moving_io {
  12. struct closure cl;
  13. struct keybuf_key *w;
  14. struct data_insert_op op;
  15. struct bbio bio;
  16. };
  17. static bool moving_pred(struct keybuf *buf, struct bkey *k)
  18. {
  19. struct cache_set *c = container_of(buf, struct cache_set,
  20. moving_gc_keys);
  21. unsigned i;
  22. for (i = 0; i < KEY_PTRS(k); i++) {
  23. struct cache *ca = PTR_CACHE(c, k, i);
  24. struct bucket *g = PTR_BUCKET(c, k, i);
  25. if (GC_SECTORS_USED(g) < ca->gc_move_threshold)
  26. return true;
  27. }
  28. return false;
  29. }
  30. /* Moving GC - IO loop */
  31. static void moving_io_destructor(struct closure *cl)
  32. {
  33. struct moving_io *io = container_of(cl, struct moving_io, cl);
  34. kfree(io);
  35. }
  36. static void write_moving_finish(struct closure *cl)
  37. {
  38. struct moving_io *io = container_of(cl, struct moving_io, cl);
  39. struct bio *bio = &io->bio.bio;
  40. struct bio_vec *bv;
  41. int i;
  42. bio_for_each_segment_all(bv, bio, i)
  43. __free_page(bv->bv_page);
  44. if (io->op.replace_collision)
  45. trace_bcache_gc_copy_collision(&io->w->key);
  46. bch_keybuf_del(&io->op.c->moving_gc_keys, io->w);
  47. up(&io->op.c->moving_in_flight);
  48. closure_return_with_destructor(cl, moving_io_destructor);
  49. }
  50. static void read_moving_endio(struct bio *bio, int error)
  51. {
  52. struct moving_io *io = container_of(bio->bi_private,
  53. struct moving_io, cl);
  54. if (error)
  55. io->op.error = error;
  56. bch_bbio_endio(io->op.c, bio, error, "reading data to move");
  57. }
  58. static void moving_init(struct moving_io *io)
  59. {
  60. struct bio *bio = &io->bio.bio;
  61. bio_init(bio);
  62. bio_get(bio);
  63. bio_set_prio(bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
  64. bio->bi_size = KEY_SIZE(&io->w->key) << 9;
  65. bio->bi_max_vecs = DIV_ROUND_UP(KEY_SIZE(&io->w->key),
  66. PAGE_SECTORS);
  67. bio->bi_private = &io->cl;
  68. bio->bi_io_vec = bio->bi_inline_vecs;
  69. bch_bio_map(bio, NULL);
  70. }
  71. static void write_moving(struct closure *cl)
  72. {
  73. struct moving_io *io = container_of(cl, struct moving_io, cl);
  74. struct data_insert_op *op = &io->op;
  75. if (!op->error) {
  76. moving_init(io);
  77. io->bio.bio.bi_sector = KEY_START(&io->w->key);
  78. op->write_prio = 1;
  79. op->bio = &io->bio.bio;
  80. op->writeback = KEY_DIRTY(&io->w->key);
  81. op->csum = KEY_CSUM(&io->w->key);
  82. bkey_copy(&op->replace_key, &io->w->key);
  83. op->replace = true;
  84. closure_call(&op->cl, bch_data_insert, NULL, cl);
  85. }
  86. continue_at(cl, write_moving_finish, system_wq);
  87. }
  88. static void read_moving_submit(struct closure *cl)
  89. {
  90. struct moving_io *io = container_of(cl, struct moving_io, cl);
  91. struct bio *bio = &io->bio.bio;
  92. bch_submit_bbio(bio, io->op.c, &io->w->key, 0);
  93. continue_at(cl, write_moving, system_wq);
  94. }
  95. static void read_moving(struct cache_set *c)
  96. {
  97. struct keybuf_key *w;
  98. struct moving_io *io;
  99. struct bio *bio;
  100. struct closure cl;
  101. closure_init_stack(&cl);
  102. /* XXX: if we error, background writeback could stall indefinitely */
  103. while (!test_bit(CACHE_SET_STOPPING, &c->flags)) {
  104. w = bch_keybuf_next_rescan(c, &c->moving_gc_keys,
  105. &MAX_KEY, moving_pred);
  106. if (!w)
  107. break;
  108. io = kzalloc(sizeof(struct moving_io) + sizeof(struct bio_vec)
  109. * DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS),
  110. GFP_KERNEL);
  111. if (!io)
  112. goto err;
  113. w->private = io;
  114. io->w = w;
  115. io->op.inode = KEY_INODE(&w->key);
  116. io->op.c = c;
  117. moving_init(io);
  118. bio = &io->bio.bio;
  119. bio->bi_rw = READ;
  120. bio->bi_end_io = read_moving_endio;
  121. if (bio_alloc_pages(bio, GFP_KERNEL))
  122. goto err;
  123. trace_bcache_gc_copy(&w->key);
  124. down(&c->moving_in_flight);
  125. closure_call(&io->cl, read_moving_submit, NULL, &cl);
  126. }
  127. if (0) {
  128. err: if (!IS_ERR_OR_NULL(w->private))
  129. kfree(w->private);
  130. bch_keybuf_del(&c->moving_gc_keys, w);
  131. }
  132. closure_sync(&cl);
  133. }
  134. static bool bucket_cmp(struct bucket *l, struct bucket *r)
  135. {
  136. return GC_SECTORS_USED(l) < GC_SECTORS_USED(r);
  137. }
  138. static unsigned bucket_heap_top(struct cache *ca)
  139. {
  140. return GC_SECTORS_USED(heap_peek(&ca->heap));
  141. }
  142. void bch_moving_gc(struct cache_set *c)
  143. {
  144. struct cache *ca;
  145. struct bucket *b;
  146. unsigned i;
  147. if (!c->copy_gc_enabled)
  148. return;
  149. mutex_lock(&c->bucket_lock);
  150. for_each_cache(ca, c, i) {
  151. unsigned sectors_to_move = 0;
  152. unsigned reserve_sectors = ca->sb.bucket_size *
  153. min(fifo_used(&ca->free), ca->free.size / 2);
  154. ca->heap.used = 0;
  155. for_each_bucket(b, ca) {
  156. if (!GC_SECTORS_USED(b))
  157. continue;
  158. if (!heap_full(&ca->heap)) {
  159. sectors_to_move += GC_SECTORS_USED(b);
  160. heap_add(&ca->heap, b, bucket_cmp);
  161. } else if (bucket_cmp(b, heap_peek(&ca->heap))) {
  162. sectors_to_move -= bucket_heap_top(ca);
  163. sectors_to_move += GC_SECTORS_USED(b);
  164. ca->heap.data[0] = b;
  165. heap_sift(&ca->heap, 0, bucket_cmp);
  166. }
  167. }
  168. while (sectors_to_move > reserve_sectors) {
  169. heap_pop(&ca->heap, b, bucket_cmp);
  170. sectors_to_move -= GC_SECTORS_USED(b);
  171. }
  172. ca->gc_move_threshold = bucket_heap_top(ca);
  173. pr_debug("threshold %u", ca->gc_move_threshold);
  174. }
  175. mutex_unlock(&c->bucket_lock);
  176. c->moving_gc_keys.last_scanned = ZERO_KEY;
  177. read_moving(c);
  178. }
  179. void bch_moving_init_cache_set(struct cache_set *c)
  180. {
  181. bch_keybuf_init(&c->moving_gc_keys);
  182. sema_init(&c->moving_in_flight, 64);
  183. }