blob: 7c1275e66025b691ec8ee4896448d46e28a2c5d9 [file] [log] [blame]
Kent Overstreetcafe5632013-03-23 16:11:31 -07001/*
2 * Moving/copying garbage collector
3 *
4 * Copyright 2012 Google, Inc.
5 */
6
7#include "bcache.h"
8#include "btree.h"
9#include "debug.h"
10#include "request.h"
11
Kent Overstreetc37511b2013-04-26 15:39:55 -070012#include <trace/events/bcache.h>
13
Kent Overstreetcafe5632013-03-23 16:11:31 -070014struct moving_io {
Kent Overstreet220bb382013-09-10 19:02:45 -070015 struct closure cl;
Kent Overstreetcafe5632013-03-23 16:11:31 -070016 struct keybuf_key *w;
Kent Overstreet220bb382013-09-10 19:02:45 -070017 struct data_insert_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -070018 struct bbio bio;
19};
20
21static bool moving_pred(struct keybuf *buf, struct bkey *k)
22{
23 struct cache_set *c = container_of(buf, struct cache_set,
24 moving_gc_keys);
25 unsigned i;
26
27 for (i = 0; i < KEY_PTRS(k); i++) {
28 struct cache *ca = PTR_CACHE(c, k, i);
29 struct bucket *g = PTR_BUCKET(c, k, i);
30
31 if (GC_SECTORS_USED(g) < ca->gc_move_threshold)
32 return true;
33 }
34
35 return false;
36}
37
38/* Moving GC - IO loop */
39
40static void moving_io_destructor(struct closure *cl)
41{
Kent Overstreet220bb382013-09-10 19:02:45 -070042 struct moving_io *io = container_of(cl, struct moving_io, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -070043 kfree(io);
44}
45
46static void write_moving_finish(struct closure *cl)
47{
Kent Overstreet220bb382013-09-10 19:02:45 -070048 struct moving_io *io = container_of(cl, struct moving_io, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -070049 struct bio *bio = &io->bio.bio;
Kent Overstreet8e51e412013-06-06 18:15:57 -070050 struct bio_vec *bv;
51 int i;
Kent Overstreetcafe5632013-03-23 16:11:31 -070052
Kent Overstreet8e51e412013-06-06 18:15:57 -070053 bio_for_each_segment_all(bv, bio, i)
Kent Overstreetcafe5632013-03-23 16:11:31 -070054 __free_page(bv->bv_page);
55
Kent Overstreet220bb382013-09-10 19:02:45 -070056 if (io->op.replace_collision)
Kent Overstreetc37511b2013-04-26 15:39:55 -070057 trace_bcache_gc_copy_collision(&io->w->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -070058
Kent Overstreet220bb382013-09-10 19:02:45 -070059 bch_keybuf_del(&io->op.c->moving_gc_keys, io->w);
Kent Overstreetcafe5632013-03-23 16:11:31 -070060
Kent Overstreet220bb382013-09-10 19:02:45 -070061 up(&io->op.c->moving_in_flight);
Kent Overstreetcafe5632013-03-23 16:11:31 -070062
63 closure_return_with_destructor(cl, moving_io_destructor);
64}
65
66static void read_moving_endio(struct bio *bio, int error)
67{
68 struct moving_io *io = container_of(bio->bi_private,
Kent Overstreet220bb382013-09-10 19:02:45 -070069 struct moving_io, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -070070
71 if (error)
Kent Overstreet220bb382013-09-10 19:02:45 -070072 io->op.error = error;
Kent Overstreetcafe5632013-03-23 16:11:31 -070073
Kent Overstreet220bb382013-09-10 19:02:45 -070074 bch_bbio_endio(io->op.c, bio, error, "reading data to move");
Kent Overstreetcafe5632013-03-23 16:11:31 -070075}
76
77static void moving_init(struct moving_io *io)
78{
79 struct bio *bio = &io->bio.bio;
80
81 bio_init(bio);
82 bio_get(bio);
83 bio_set_prio(bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
84
85 bio->bi_size = KEY_SIZE(&io->w->key) << 9;
86 bio->bi_max_vecs = DIV_ROUND_UP(KEY_SIZE(&io->w->key),
87 PAGE_SECTORS);
Kent Overstreet220bb382013-09-10 19:02:45 -070088 bio->bi_private = &io->cl;
Kent Overstreetcafe5632013-03-23 16:11:31 -070089 bio->bi_io_vec = bio->bi_inline_vecs;
Kent Overstreet169ef1c2013-03-28 12:50:55 -060090 bch_bio_map(bio, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -070091}
92
93static void write_moving(struct closure *cl)
94{
Kent Overstreet220bb382013-09-10 19:02:45 -070095 struct moving_io *io = container_of(cl, struct moving_io, cl);
96 struct data_insert_op *op = &io->op;
Kent Overstreetcafe5632013-03-23 16:11:31 -070097
Kent Overstreet220bb382013-09-10 19:02:45 -070098 if (!op->error) {
Kent Overstreetcafe5632013-03-23 16:11:31 -070099 moving_init(io);
100
Kent Overstreet220bb382013-09-10 19:02:45 -0700101 io->bio.bio.bi_sector = KEY_START(&io->w->key);
102 op->write_prio = 1;
103 op->bio = &io->bio.bio;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700104
Kent Overstreet220bb382013-09-10 19:02:45 -0700105 op->writeback = KEY_DIRTY(&io->w->key);
106 op->csum = KEY_CSUM(&io->w->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700107
Kent Overstreet220bb382013-09-10 19:02:45 -0700108 bkey_copy(&op->replace_key, &io->w->key);
109 op->replace = true;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700110
Kent Overstreet220bb382013-09-10 19:02:45 -0700111 closure_call(&op->cl, bch_data_insert, NULL, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700112 }
113
Kent Overstreet72a44512013-10-24 17:19:26 -0700114 continue_at(cl, write_moving_finish, system_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700115}
116
117static void read_moving_submit(struct closure *cl)
118{
Kent Overstreet220bb382013-09-10 19:02:45 -0700119 struct moving_io *io = container_of(cl, struct moving_io, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700120 struct bio *bio = &io->bio.bio;
121
Kent Overstreet220bb382013-09-10 19:02:45 -0700122 bch_submit_bbio(bio, io->op.c, &io->w->key, 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700123
Kent Overstreet72a44512013-10-24 17:19:26 -0700124 continue_at(cl, write_moving, system_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700125}
126
Kent Overstreet72a44512013-10-24 17:19:26 -0700127static void read_moving(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700128{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700129 struct keybuf_key *w;
130 struct moving_io *io;
131 struct bio *bio;
Kent Overstreet72a44512013-10-24 17:19:26 -0700132 struct closure cl;
133
134 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700135
136 /* XXX: if we error, background writeback could stall indefinitely */
137
138 while (!test_bit(CACHE_SET_STOPPING, &c->flags)) {
Kent Overstreet72c27062013-06-05 06:24:39 -0700139 w = bch_keybuf_next_rescan(c, &c->moving_gc_keys,
140 &MAX_KEY, moving_pred);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700141 if (!w)
142 break;
143
144 io = kzalloc(sizeof(struct moving_io) + sizeof(struct bio_vec)
145 * DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS),
146 GFP_KERNEL);
147 if (!io)
148 goto err;
149
150 w->private = io;
151 io->w = w;
Kent Overstreet220bb382013-09-10 19:02:45 -0700152 io->op.inode = KEY_INODE(&w->key);
153 io->op.c = c;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700154
155 moving_init(io);
156 bio = &io->bio.bio;
157
158 bio->bi_rw = READ;
159 bio->bi_end_io = read_moving_endio;
160
Kent Overstreet8e51e412013-06-06 18:15:57 -0700161 if (bio_alloc_pages(bio, GFP_KERNEL))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700162 goto err;
163
Kent Overstreetc37511b2013-04-26 15:39:55 -0700164 trace_bcache_gc_copy(&w->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700165
Kent Overstreet72a44512013-10-24 17:19:26 -0700166 down(&c->moving_in_flight);
Kent Overstreet220bb382013-09-10 19:02:45 -0700167 closure_call(&io->cl, read_moving_submit, NULL, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700168 }
169
170 if (0) {
171err: if (!IS_ERR_OR_NULL(w->private))
172 kfree(w->private);
173
174 bch_keybuf_del(&c->moving_gc_keys, w);
175 }
176
Kent Overstreet72a44512013-10-24 17:19:26 -0700177 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700178}
179
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700180static bool bucket_cmp(struct bucket *l, struct bucket *r)
181{
182 return GC_SECTORS_USED(l) < GC_SECTORS_USED(r);
183}
184
185static unsigned bucket_heap_top(struct cache *ca)
186{
187 return GC_SECTORS_USED(heap_peek(&ca->heap));
188}
189
Kent Overstreet72a44512013-10-24 17:19:26 -0700190void bch_moving_gc(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700191{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700192 struct cache *ca;
193 struct bucket *b;
194 unsigned i;
195
Kent Overstreetcafe5632013-03-23 16:11:31 -0700196 if (!c->copy_gc_enabled)
Kent Overstreet72a44512013-10-24 17:19:26 -0700197 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700198
199 mutex_lock(&c->bucket_lock);
200
201 for_each_cache(ca, c, i) {
202 unsigned sectors_to_move = 0;
203 unsigned reserve_sectors = ca->sb.bucket_size *
204 min(fifo_used(&ca->free), ca->free.size / 2);
205
206 ca->heap.used = 0;
207
208 for_each_bucket(b, ca) {
209 if (!GC_SECTORS_USED(b))
210 continue;
211
212 if (!heap_full(&ca->heap)) {
213 sectors_to_move += GC_SECTORS_USED(b);
214 heap_add(&ca->heap, b, bucket_cmp);
215 } else if (bucket_cmp(b, heap_peek(&ca->heap))) {
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700216 sectors_to_move -= bucket_heap_top(ca);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700217 sectors_to_move += GC_SECTORS_USED(b);
218
219 ca->heap.data[0] = b;
220 heap_sift(&ca->heap, 0, bucket_cmp);
221 }
222 }
223
224 while (sectors_to_move > reserve_sectors) {
225 heap_pop(&ca->heap, b, bucket_cmp);
226 sectors_to_move -= GC_SECTORS_USED(b);
227 }
228
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700229 ca->gc_move_threshold = bucket_heap_top(ca);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700230
231 pr_debug("threshold %u", ca->gc_move_threshold);
232 }
233
234 mutex_unlock(&c->bucket_lock);
235
236 c->moving_gc_keys.last_scanned = ZERO_KEY;
237
Kent Overstreet72a44512013-10-24 17:19:26 -0700238 read_moving(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700239}
240
241void bch_moving_init_cache_set(struct cache_set *c)
242{
Kent Overstreet72c27062013-06-05 06:24:39 -0700243 bch_keybuf_init(&c->moving_gc_keys);
Kent Overstreet72a44512013-10-24 17:19:26 -0700244 sema_init(&c->moving_in_flight, 64);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700245}