2 * Moving/copying garbage collector
4 * Copyright 2012 Google, Inc.
18 static bool moving_pred(struct keybuf *buf, struct bkey *k)
20 struct cache_set *c = container_of(buf, struct cache_set,
24 for (i = 0; i < KEY_PTRS(k); i++) {
25 struct cache *ca = PTR_CACHE(c, k, i);
26 struct bucket *g = PTR_BUCKET(c, k, i);
28 if (GC_SECTORS_USED(g) < ca->gc_move_threshold)
35 /* Moving GC - IO loop */
37 static void moving_io_destructor(struct closure *cl)
39 struct moving_io *io = container_of(cl, struct moving_io, s.cl);
43 static void write_moving_finish(struct closure *cl)
45 struct moving_io *io = container_of(cl, struct moving_io, s.cl);
46 struct bio *bio = &io->bio.bio;
47 struct bio_vec *bv = bio_iovec_idx(bio, bio->bi_vcnt);
49 while (bv-- != bio->bi_io_vec)
50 __free_page(bv->bv_page);
52 pr_debug("%s %s", io->s.op.insert_collision
53 ? "collision moving" : "moved",
56 bch_keybuf_del(&io->s.op.c->moving_gc_keys, io->w);
58 atomic_dec_bug(&io->s.op.c->in_flight);
59 closure_wake_up(&io->s.op.c->moving_gc_wait);
61 closure_return_with_destructor(cl, moving_io_destructor);
64 static void read_moving_endio(struct bio *bio, int error,
65 struct batch_complete *batch)
67 struct moving_io *io = container_of(bio->bi_private,
68 struct moving_io, s.cl);
73 bch_bbio_endio(io->s.op.c, bio, error, "reading data to move");
76 static void moving_init(struct moving_io *io)
78 struct bio *bio = &io->bio.bio;
82 bio_set_prio(bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
84 bio->bi_size = KEY_SIZE(&io->w->key) << 9;
85 bio->bi_max_vecs = DIV_ROUND_UP(KEY_SIZE(&io->w->key),
87 bio->bi_private = &io->s.cl;
88 bio->bi_io_vec = bio->bi_inline_vecs;
89 bch_bio_map(bio, NULL);
92 static void write_moving(struct closure *cl)
94 struct search *s = container_of(cl, struct search, cl);
95 struct moving_io *io = container_of(s, struct moving_io, s);
98 trace_bcache_write_moving(&io->bio.bio);
102 io->bio.bio.bi_sector = KEY_START(&io->w->key);
104 s->op.write_prio = 1;
105 s->op.cache_bio = &io->bio.bio;
107 s->writeback = KEY_DIRTY(&io->w->key);
108 s->op.csum = KEY_CSUM(&io->w->key);
110 s->op.type = BTREE_REPLACE;
111 bkey_copy(&s->op.replace, &io->w->key);
113 closure_init(&s->op.cl, cl);
114 bch_insert_data(&s->op.cl);
117 continue_at(cl, write_moving_finish, NULL);
120 static void read_moving_submit(struct closure *cl)
122 struct search *s = container_of(cl, struct search, cl);
123 struct moving_io *io = container_of(s, struct moving_io, s);
124 struct bio *bio = &io->bio.bio;
126 trace_bcache_read_moving(bio);
127 bch_submit_bbio(bio, s->op.c, &io->w->key, 0);
129 continue_at(cl, write_moving, bch_gc_wq);
132 static void read_moving(struct closure *cl)
134 struct cache_set *c = container_of(cl, struct cache_set, moving_gc);
135 struct keybuf_key *w;
136 struct moving_io *io;
139 /* XXX: if we error, background writeback could stall indefinitely */
141 while (!test_bit(CACHE_SET_STOPPING, &c->flags)) {
142 w = bch_keybuf_next_rescan(c, &c->moving_gc_keys, &MAX_KEY);
146 io = kzalloc(sizeof(struct moving_io) + sizeof(struct bio_vec)
147 * DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS),
154 io->s.op.inode = KEY_INODE(&w->key);
161 bio->bi_end_io = read_moving_endio;
163 if (bch_bio_alloc_pages(bio, GFP_KERNEL))
166 pr_debug("%s", pkey(&w->key));
168 closure_call(&io->s.cl, read_moving_submit, NULL, &c->gc.cl);
170 if (atomic_inc_return(&c->in_flight) >= 64) {
171 closure_wait_event(&c->moving_gc_wait, cl,
172 atomic_read(&c->in_flight) < 64);
173 continue_at(cl, read_moving, bch_gc_wq);
178 err: if (!IS_ERR_OR_NULL(w->private))
181 bch_keybuf_del(&c->moving_gc_keys, w);
187 static bool bucket_cmp(struct bucket *l, struct bucket *r)
189 return GC_SECTORS_USED(l) < GC_SECTORS_USED(r);
192 static unsigned bucket_heap_top(struct cache *ca)
194 return GC_SECTORS_USED(heap_peek(&ca->heap));
197 void bch_moving_gc(struct closure *cl)
199 struct cache_set *c = container_of(cl, struct cache_set, gc.cl);
204 if (!c->copy_gc_enabled)
207 mutex_lock(&c->bucket_lock);
209 for_each_cache(ca, c, i) {
210 unsigned sectors_to_move = 0;
211 unsigned reserve_sectors = ca->sb.bucket_size *
212 min(fifo_used(&ca->free), ca->free.size / 2);
216 for_each_bucket(b, ca) {
217 if (!GC_SECTORS_USED(b))
220 if (!heap_full(&ca->heap)) {
221 sectors_to_move += GC_SECTORS_USED(b);
222 heap_add(&ca->heap, b, bucket_cmp);
223 } else if (bucket_cmp(b, heap_peek(&ca->heap))) {
224 sectors_to_move -= bucket_heap_top(ca);
225 sectors_to_move += GC_SECTORS_USED(b);
227 ca->heap.data[0] = b;
228 heap_sift(&ca->heap, 0, bucket_cmp);
232 while (sectors_to_move > reserve_sectors) {
233 heap_pop(&ca->heap, b, bucket_cmp);
234 sectors_to_move -= GC_SECTORS_USED(b);
237 ca->gc_move_threshold = bucket_heap_top(ca);
239 pr_debug("threshold %u", ca->gc_move_threshold);
242 mutex_unlock(&c->bucket_lock);
244 c->moving_gc_keys.last_scanned = ZERO_KEY;
246 closure_init(&c->moving_gc, cl);
247 read_moving(&c->moving_gc);
252 void bch_moving_init_cache_set(struct cache_set *c)
254 bch_keybuf_init(&c->moving_gc_keys, moving_pred);