movinggc.c 5.4 KB

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