movinggc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 bucket *g = PTR_BUCKET(c, k, i);
  24. if (GC_MOVE(g))
  25. return true;
  26. }
  27. return false;
  28. }
  29. /* Moving GC - IO loop */
  30. static void moving_io_destructor(struct closure *cl)
  31. {
  32. struct moving_io *io = container_of(cl, struct moving_io, cl);
  33. kfree(io);
  34. }
  35. static void write_moving_finish(struct closure *cl)
  36. {
  37. struct moving_io *io = container_of(cl, struct moving_io, cl);
  38. struct bio *bio = &io->bio.bio;
  39. struct bio_vec *bv;
  40. int i;
  41. bio_for_each_segment_all(bv, bio, i)
  42. __free_page(bv->bv_page);
  43. if (io->op.replace_collision)
  44. trace_bcache_gc_copy_collision(&io->w->key);
  45. bch_keybuf_del(&io->op.c->moving_gc_keys, io->w);
  46. up(&io->op.c->moving_in_flight);
  47. closure_return_with_destructor(cl, moving_io_destructor);
  48. }
  49. static void read_moving_endio(struct bio *bio, int error)
  50. {
  51. struct bbio *b = container_of(bio, struct bbio, bio);
  52. struct moving_io *io = container_of(bio->bi_private,
  53. struct moving_io, cl);
  54. if (error)
  55. io->op.error = error;
  56. else if (!KEY_DIRTY(&b->key) &&
  57. ptr_stale(io->op.c, &b->key, 0)) {
  58. io->op.error = -EINTR;
  59. }
  60. bch_bbio_endio(io->op.c, bio, error, "reading data to move");
  61. }
  62. static void moving_init(struct moving_io *io)
  63. {
  64. struct bio *bio = &io->bio.bio;
  65. bio_init(bio);
  66. bio_get(bio);
  67. bio_set_prio(bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
  68. bio->bi_iter.bi_size = KEY_SIZE(&io->w->key) << 9;
  69. bio->bi_max_vecs = DIV_ROUND_UP(KEY_SIZE(&io->w->key),
  70. PAGE_SECTORS);
  71. bio->bi_private = &io->cl;
  72. bio->bi_io_vec = bio->bi_inline_vecs;
  73. bch_bio_map(bio, NULL);
  74. }
  75. static void write_moving(struct closure *cl)
  76. {
  77. struct moving_io *io = container_of(cl, struct moving_io, cl);
  78. struct data_insert_op *op = &io->op;
  79. if (!op->error) {
  80. moving_init(io);
  81. io->bio.bio.bi_iter.bi_sector = KEY_START(&io->w->key);
  82. op->write_prio = 1;
  83. op->bio = &io->bio.bio;
  84. op->writeback = KEY_DIRTY(&io->w->key);
  85. op->csum = KEY_CSUM(&io->w->key);
  86. bkey_copy(&op->replace_key, &io->w->key);
  87. op->replace = true;
  88. closure_call(&op->cl, bch_data_insert, NULL, cl);
  89. }
  90. continue_at(cl, write_moving_finish, system_wq);
  91. }
  92. static void read_moving_submit(struct closure *cl)
  93. {
  94. struct moving_io *io = container_of(cl, struct moving_io, cl);
  95. struct bio *bio = &io->bio.bio;
  96. bch_submit_bbio(bio, io->op.c, &io->w->key, 0);
  97. continue_at(cl, write_moving, system_wq);
  98. }
  99. static void read_moving(struct cache_set *c)
  100. {
  101. struct keybuf_key *w;
  102. struct moving_io *io;
  103. struct bio *bio;
  104. struct closure cl;
  105. closure_init_stack(&cl);
  106. /* XXX: if we error, background writeback could stall indefinitely */
  107. while (!test_bit(CACHE_SET_STOPPING, &c->flags)) {
  108. w = bch_keybuf_next_rescan(c, &c->moving_gc_keys,
  109. &MAX_KEY, moving_pred);
  110. if (!w)
  111. break;
  112. if (ptr_stale(c, &w->key, 0)) {
  113. bch_keybuf_del(&c->moving_gc_keys, w);
  114. continue;
  115. }
  116. io = kzalloc(sizeof(struct moving_io) + sizeof(struct bio_vec)
  117. * DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS),
  118. GFP_KERNEL);
  119. if (!io)
  120. goto err;
  121. w->private = io;
  122. io->w = w;
  123. io->op.inode = KEY_INODE(&w->key);
  124. io->op.c = c;
  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_SECTORS_USED(b))
  166. continue;
  167. if (!heap_full(&ca->heap)) {
  168. sectors_to_move += GC_SECTORS_USED(b);
  169. heap_add(&ca->heap, b, bucket_cmp);
  170. } else if (bucket_cmp(b, heap_peek(&ca->heap))) {
  171. sectors_to_move -= bucket_heap_top(ca);
  172. sectors_to_move += GC_SECTORS_USED(b);
  173. ca->heap.data[0] = b;
  174. heap_sift(&ca->heap, 0, bucket_cmp);
  175. }
  176. }
  177. while (sectors_to_move > reserve_sectors) {
  178. heap_pop(&ca->heap, b, bucket_cmp);
  179. sectors_to_move -= GC_SECTORS_USED(b);
  180. }
  181. while (heap_pop(&ca->heap, b, bucket_cmp))
  182. SET_GC_MOVE(b, 1);
  183. }
  184. mutex_unlock(&c->bucket_lock);
  185. c->moving_gc_keys.last_scanned = ZERO_KEY;
  186. read_moving(c);
  187. }
  188. void bch_moving_init_cache_set(struct cache_set *c)
  189. {
  190. bch_keybuf_init(&c->moving_gc_keys);
  191. sema_init(&c->moving_in_flight, 64);
  192. }