movinggc.c 5.4 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. 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. bio_free_pages(bio);
  39. if (io->op.replace_collision)
  40. trace_bcache_gc_copy_collision(&io->w->key);
  41. bch_keybuf_del(&io->op.c->moving_gc_keys, io->w);
  42. up(&io->op.c->moving_in_flight);
  43. closure_return_with_destructor(cl, moving_io_destructor);
  44. }
  45. static void read_moving_endio(struct bio *bio)
  46. {
  47. struct bbio *b = container_of(bio, struct bbio, bio);
  48. struct moving_io *io = container_of(bio->bi_private,
  49. struct moving_io, cl);
  50. if (bio->bi_error)
  51. io->op.error = bio->bi_error;
  52. else if (!KEY_DIRTY(&b->key) &&
  53. ptr_stale(io->op.c, &b->key, 0)) {
  54. io->op.error = -EINTR;
  55. }
  56. bch_bbio_endio(io->op.c, bio, bio->bi_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_iter.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_iter.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, op->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, io->op.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. if (ptr_stale(c, &w->key, 0)) {
  109. bch_keybuf_del(&c->moving_gc_keys, w);
  110. continue;
  111. }
  112. io = kzalloc(sizeof(struct moving_io) + sizeof(struct bio_vec)
  113. * DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS),
  114. GFP_KERNEL);
  115. if (!io)
  116. goto err;
  117. w->private = io;
  118. io->w = w;
  119. io->op.inode = KEY_INODE(&w->key);
  120. io->op.c = c;
  121. io->op.wq = c->moving_gc_wq;
  122. moving_init(io);
  123. bio = &io->bio.bio;
  124. bio_set_op_attrs(bio, REQ_OP_READ, 0);
  125. bio->bi_end_io = read_moving_endio;
  126. if (bio_alloc_pages(bio, GFP_KERNEL))
  127. goto err;
  128. trace_bcache_gc_copy(&w->key);
  129. down(&c->moving_in_flight);
  130. closure_call(&io->cl, read_moving_submit, NULL, &cl);
  131. }
  132. if (0) {
  133. err: if (!IS_ERR_OR_NULL(w->private))
  134. kfree(w->private);
  135. bch_keybuf_del(&c->moving_gc_keys, w);
  136. }
  137. closure_sync(&cl);
  138. }
  139. static bool bucket_cmp(struct bucket *l, struct bucket *r)
  140. {
  141. return GC_SECTORS_USED(l) < GC_SECTORS_USED(r);
  142. }
  143. static unsigned bucket_heap_top(struct cache *ca)
  144. {
  145. struct bucket *b;
  146. return (b = heap_peek(&ca->heap)) ? GC_SECTORS_USED(b) : 0;
  147. }
  148. void bch_moving_gc(struct cache_set *c)
  149. {
  150. struct cache *ca;
  151. struct bucket *b;
  152. unsigned i;
  153. if (!c->copy_gc_enabled)
  154. return;
  155. mutex_lock(&c->bucket_lock);
  156. for_each_cache(ca, c, i) {
  157. unsigned sectors_to_move = 0;
  158. unsigned reserve_sectors = ca->sb.bucket_size *
  159. fifo_used(&ca->free[RESERVE_MOVINGGC]);
  160. ca->heap.used = 0;
  161. for_each_bucket(b, ca) {
  162. if (GC_MARK(b) == GC_MARK_METADATA ||
  163. !GC_SECTORS_USED(b) ||
  164. GC_SECTORS_USED(b) == ca->sb.bucket_size ||
  165. atomic_read(&b->pin))
  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. }