movinggc.c 5.5 KB

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