blob: 5c4bddecfaf092c6ec1e3a6a16da605c44dd9574 [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
Kent Overstreet10d9dcf2014-02-17 15:48:36 -080027 for (i = 0; i < KEY_PTRS(k); i++)
28 if (ptr_available(c, k, i) &&
29 GC_MOVE(PTR_BUCKET(c, k, i)))
Kent Overstreetcafe5632013-03-23 16:11:31 -070030 return true;
Kent Overstreetcafe5632013-03-23 16:11:31 -070031
32 return false;
33}
34
35/* Moving GC - IO loop */
36
37static void moving_io_destructor(struct closure *cl)
38{
Kent Overstreet220bb382013-09-10 19:02:45 -070039 struct moving_io *io = container_of(cl, struct moving_io, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -070040 kfree(io);
41}
42
43static void write_moving_finish(struct closure *cl)
44{
Kent Overstreet220bb382013-09-10 19:02:45 -070045 struct moving_io *io = container_of(cl, struct moving_io, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -070046 struct bio *bio = &io->bio.bio;
Kent Overstreetcafe5632013-03-23 16:11:31 -070047
Guoqing Jiang491221f2016-09-22 03:10:01 -040048 bio_free_pages(bio);
Kent Overstreetcafe5632013-03-23 16:11:31 -070049
Kent Overstreet220bb382013-09-10 19:02:45 -070050 if (io->op.replace_collision)
Kent Overstreetc37511b2013-04-26 15:39:55 -070051 trace_bcache_gc_copy_collision(&io->w->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -070052
Kent Overstreet220bb382013-09-10 19:02:45 -070053 bch_keybuf_del(&io->op.c->moving_gc_keys, io->w);
Kent Overstreetcafe5632013-03-23 16:11:31 -070054
Kent Overstreet220bb382013-09-10 19:02:45 -070055 up(&io->op.c->moving_in_flight);
Kent Overstreetcafe5632013-03-23 16:11:31 -070056
57 closure_return_with_destructor(cl, moving_io_destructor);
58}
59
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020060static void read_moving_endio(struct bio *bio)
Kent Overstreetcafe5632013-03-23 16:11:31 -070061{
Kent Overstreet6d3d1a92013-12-16 14:12:09 -080062 struct bbio *b = container_of(bio, struct bbio, bio);
Kent Overstreetcafe5632013-03-23 16:11:31 -070063 struct moving_io *io = container_of(bio->bi_private,
Kent Overstreet220bb382013-09-10 19:02:45 -070064 struct moving_io, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -070065
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020066 if (bio->bi_error)
67 io->op.error = bio->bi_error;
Kent Overstreet6d3d1a92013-12-16 14:12:09 -080068 else if (!KEY_DIRTY(&b->key) &&
69 ptr_stale(io->op.c, &b->key, 0)) {
70 io->op.error = -EINTR;
71 }
Kent Overstreetcafe5632013-03-23 16:11:31 -070072
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020073 bch_bbio_endio(io->op.c, bio, bio->bi_error, "reading data to move");
Kent Overstreetcafe5632013-03-23 16:11:31 -070074}
75
76static void moving_init(struct moving_io *io)
77{
78 struct bio *bio = &io->bio.bio;
79
80 bio_init(bio);
81 bio_get(bio);
82 bio_set_prio(bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
83
Kent Overstreet4f024f32013-10-11 15:44:27 -070084 bio->bi_iter.bi_size = KEY_SIZE(&io->w->key) << 9;
Kent Overstreetcafe5632013-03-23 16:11:31 -070085 bio->bi_max_vecs = DIV_ROUND_UP(KEY_SIZE(&io->w->key),
86 PAGE_SECTORS);
Kent Overstreet220bb382013-09-10 19:02:45 -070087 bio->bi_private = &io->cl;
Kent Overstreetcafe5632013-03-23 16:11:31 -070088 bio->bi_io_vec = bio->bi_inline_vecs;
Kent Overstreet169ef1c2013-03-28 12:50:55 -060089 bch_bio_map(bio, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -070090}
91
92static void write_moving(struct closure *cl)
93{
Kent Overstreet220bb382013-09-10 19:02:45 -070094 struct moving_io *io = container_of(cl, struct moving_io, cl);
95 struct data_insert_op *op = &io->op;
Kent Overstreetcafe5632013-03-23 16:11:31 -070096
Kent Overstreet220bb382013-09-10 19:02:45 -070097 if (!op->error) {
Kent Overstreetcafe5632013-03-23 16:11:31 -070098 moving_init(io);
99
Kent Overstreet4f024f32013-10-11 15:44:27 -0700100 io->bio.bio.bi_iter.bi_sector = KEY_START(&io->w->key);
Kent Overstreet220bb382013-09-10 19:02:45 -0700101 op->write_prio = 1;
102 op->bio = &io->bio.bio;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700103
Kent Overstreet220bb382013-09-10 19:02:45 -0700104 op->writeback = KEY_DIRTY(&io->w->key);
105 op->csum = KEY_CSUM(&io->w->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700106
Kent Overstreet220bb382013-09-10 19:02:45 -0700107 bkey_copy(&op->replace_key, &io->w->key);
108 op->replace = true;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700109
Kent Overstreet220bb382013-09-10 19:02:45 -0700110 closure_call(&op->cl, bch_data_insert, NULL, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700111 }
112
Nicholas Swensonda415a02014-01-09 16:03:04 -0800113 continue_at(cl, write_moving_finish, op->wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700114}
115
116static void read_moving_submit(struct closure *cl)
117{
Kent Overstreet220bb382013-09-10 19:02:45 -0700118 struct moving_io *io = container_of(cl, struct moving_io, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700119 struct bio *bio = &io->bio.bio;
120
Kent Overstreet220bb382013-09-10 19:02:45 -0700121 bch_submit_bbio(bio, io->op.c, &io->w->key, 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700122
Nicholas Swensonda415a02014-01-09 16:03:04 -0800123 continue_at(cl, write_moving, io->op.wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700124}
125
Kent Overstreet72a44512013-10-24 17:19:26 -0700126static void read_moving(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700127{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700128 struct keybuf_key *w;
129 struct moving_io *io;
130 struct bio *bio;
Kent Overstreet72a44512013-10-24 17:19:26 -0700131 struct closure cl;
132
133 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700134
135 /* XXX: if we error, background writeback could stall indefinitely */
136
137 while (!test_bit(CACHE_SET_STOPPING, &c->flags)) {
Kent Overstreet72c27062013-06-05 06:24:39 -0700138 w = bch_keybuf_next_rescan(c, &c->moving_gc_keys,
139 &MAX_KEY, moving_pred);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700140 if (!w)
141 break;
142
Kent Overstreet6d3d1a92013-12-16 14:12:09 -0800143 if (ptr_stale(c, &w->key, 0)) {
144 bch_keybuf_del(&c->moving_gc_keys, w);
145 continue;
146 }
147
Kent Overstreetcafe5632013-03-23 16:11:31 -0700148 io = kzalloc(sizeof(struct moving_io) + sizeof(struct bio_vec)
149 * DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS),
150 GFP_KERNEL);
151 if (!io)
152 goto err;
153
154 w->private = io;
155 io->w = w;
Kent Overstreet220bb382013-09-10 19:02:45 -0700156 io->op.inode = KEY_INODE(&w->key);
157 io->op.c = c;
Nicholas Swensonda415a02014-01-09 16:03:04 -0800158 io->op.wq = c->moving_gc_wq;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700159
160 moving_init(io);
161 bio = &io->bio.bio;
162
Mike Christiead0d9e72016-06-05 14:32:05 -0500163 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700164 bio->bi_end_io = read_moving_endio;
165
Kent Overstreet8e51e412013-06-06 18:15:57 -0700166 if (bio_alloc_pages(bio, GFP_KERNEL))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700167 goto err;
168
Kent Overstreetc37511b2013-04-26 15:39:55 -0700169 trace_bcache_gc_copy(&w->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700170
Kent Overstreet72a44512013-10-24 17:19:26 -0700171 down(&c->moving_in_flight);
Kent Overstreet220bb382013-09-10 19:02:45 -0700172 closure_call(&io->cl, read_moving_submit, NULL, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700173 }
174
175 if (0) {
176err: if (!IS_ERR_OR_NULL(w->private))
177 kfree(w->private);
178
179 bch_keybuf_del(&c->moving_gc_keys, w);
180 }
181
Kent Overstreet72a44512013-10-24 17:19:26 -0700182 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700183}
184
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700185static bool bucket_cmp(struct bucket *l, struct bucket *r)
186{
187 return GC_SECTORS_USED(l) < GC_SECTORS_USED(r);
188}
189
190static unsigned bucket_heap_top(struct cache *ca)
191{
Nicholas Swensonbee63f42013-10-31 19:25:18 -0700192 struct bucket *b;
193 return (b = heap_peek(&ca->heap)) ? GC_SECTORS_USED(b) : 0;
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700194}
195
Kent Overstreet72a44512013-10-24 17:19:26 -0700196void bch_moving_gc(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700197{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700198 struct cache *ca;
199 struct bucket *b;
200 unsigned i;
201
Kent Overstreetcafe5632013-03-23 16:11:31 -0700202 if (!c->copy_gc_enabled)
Kent Overstreet72a44512013-10-24 17:19:26 -0700203 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700204
205 mutex_lock(&c->bucket_lock);
206
207 for_each_cache(ca, c, i) {
208 unsigned sectors_to_move = 0;
209 unsigned reserve_sectors = ca->sb.bucket_size *
Kent Overstreet78365412013-12-17 01:29:34 -0800210 fifo_used(&ca->free[RESERVE_MOVINGGC]);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700211
212 ca->heap.used = 0;
213
214 for_each_bucket(b, ca) {
Nicholas Swenson3f6ef382014-01-23 15:21:02 -0800215 if (GC_MARK(b) == GC_MARK_METADATA ||
216 !GC_SECTORS_USED(b) ||
217 GC_SECTORS_USED(b) == ca->sb.bucket_size ||
218 atomic_read(&b->pin))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700219 continue;
220
221 if (!heap_full(&ca->heap)) {
222 sectors_to_move += GC_SECTORS_USED(b);
223 heap_add(&ca->heap, b, bucket_cmp);
224 } else if (bucket_cmp(b, heap_peek(&ca->heap))) {
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700225 sectors_to_move -= bucket_heap_top(ca);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700226 sectors_to_move += GC_SECTORS_USED(b);
227
228 ca->heap.data[0] = b;
229 heap_sift(&ca->heap, 0, bucket_cmp);
230 }
231 }
232
233 while (sectors_to_move > reserve_sectors) {
234 heap_pop(&ca->heap, b, bucket_cmp);
235 sectors_to_move -= GC_SECTORS_USED(b);
236 }
237
Nicholas Swenson981aa8c2013-11-07 17:53:19 -0800238 while (heap_pop(&ca->heap, b, bucket_cmp))
239 SET_GC_MOVE(b, 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700240 }
241
242 mutex_unlock(&c->bucket_lock);
243
244 c->moving_gc_keys.last_scanned = ZERO_KEY;
245
Kent Overstreet72a44512013-10-24 17:19:26 -0700246 read_moving(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700247}
248
249void bch_moving_init_cache_set(struct cache_set *c)
250{
Kent Overstreet72c27062013-06-05 06:24:39 -0700251 bch_keybuf_init(&c->moving_gc_keys);
Kent Overstreet72a44512013-10-24 17:19:26 -0700252 sema_init(&c->moving_in_flight, 64);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700253}