blob: a24c3a95b2c0de85dd4582fadd12f719816c2038 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Kent Overstreetcafe5632013-03-23 16:11:31 -07002/*
3 * Moving/copying garbage collector
4 *
5 * Copyright 2012 Google, Inc.
6 */
7
8#include "bcache.h"
9#include "btree.h"
10#include "debug.h"
11#include "request.h"
12
Kent Overstreetc37511b2013-04-26 15:39:55 -070013#include <trace/events/bcache.h>
14
Kent Overstreetcafe5632013-03-23 16:11:31 -070015struct moving_io {
Kent Overstreet220bb382013-09-10 19:02:45 -070016 struct closure cl;
Kent Overstreetcafe5632013-03-23 16:11:31 -070017 struct keybuf_key *w;
Kent Overstreet220bb382013-09-10 19:02:45 -070018 struct data_insert_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -070019 struct bbio bio;
20};
21
22static bool moving_pred(struct keybuf *buf, struct bkey *k)
23{
24 struct cache_set *c = container_of(buf, struct cache_set,
25 moving_gc_keys);
26 unsigned i;
27
Kent Overstreet10d9dcf2014-02-17 15:48:36 -080028 for (i = 0; i < KEY_PTRS(k); i++)
29 if (ptr_available(c, k, i) &&
30 GC_MOVE(PTR_BUCKET(c, k, i)))
Kent Overstreetcafe5632013-03-23 16:11:31 -070031 return true;
Kent Overstreetcafe5632013-03-23 16:11:31 -070032
33 return false;
34}
35
36/* Moving GC - IO loop */
37
38static void moving_io_destructor(struct closure *cl)
39{
Kent Overstreet220bb382013-09-10 19:02:45 -070040 struct moving_io *io = container_of(cl, struct moving_io, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -070041 kfree(io);
42}
43
44static void write_moving_finish(struct closure *cl)
45{
Kent Overstreet220bb382013-09-10 19:02:45 -070046 struct moving_io *io = container_of(cl, struct moving_io, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -070047 struct bio *bio = &io->bio.bio;
Kent Overstreetcafe5632013-03-23 16:11:31 -070048
Guoqing Jiang491221f2016-09-22 03:10:01 -040049 bio_free_pages(bio);
Kent Overstreetcafe5632013-03-23 16:11:31 -070050
Kent Overstreet220bb382013-09-10 19:02:45 -070051 if (io->op.replace_collision)
Kent Overstreetc37511b2013-04-26 15:39:55 -070052 trace_bcache_gc_copy_collision(&io->w->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -070053
Kent Overstreet220bb382013-09-10 19:02:45 -070054 bch_keybuf_del(&io->op.c->moving_gc_keys, io->w);
Kent Overstreetcafe5632013-03-23 16:11:31 -070055
Kent Overstreet220bb382013-09-10 19:02:45 -070056 up(&io->op.c->moving_in_flight);
Kent Overstreetcafe5632013-03-23 16:11:31 -070057
58 closure_return_with_destructor(cl, moving_io_destructor);
59}
60
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020061static void read_moving_endio(struct bio *bio)
Kent Overstreetcafe5632013-03-23 16:11:31 -070062{
Kent Overstreet6d3d1a92013-12-16 14:12:09 -080063 struct bbio *b = container_of(bio, struct bbio, bio);
Kent Overstreetcafe5632013-03-23 16:11:31 -070064 struct moving_io *io = container_of(bio->bi_private,
Kent Overstreet220bb382013-09-10 19:02:45 -070065 struct moving_io, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -070066
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020067 if (bio->bi_status)
68 io->op.status = bio->bi_status;
Kent Overstreet6d3d1a92013-12-16 14:12:09 -080069 else if (!KEY_DIRTY(&b->key) &&
70 ptr_stale(io->op.c, &b->key, 0)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020071 io->op.status = BLK_STS_IOERR;
Kent Overstreet6d3d1a92013-12-16 14:12:09 -080072 }
Kent Overstreetcafe5632013-03-23 16:11:31 -070073
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020074 bch_bbio_endio(io->op.c, bio, bio->bi_status, "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
Ming Lei3a83f462016-11-22 08:57:21 -070081 bio_init(bio, bio->bi_inline_vecs,
82 DIV_ROUND_UP(KEY_SIZE(&io->w->key), PAGE_SECTORS));
Kent Overstreetcafe5632013-03-23 16:11:31 -070083 bio_get(bio);
84 bio_set_prio(bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
85
Kent Overstreet4f024f32013-10-11 15:44:27 -070086 bio->bi_iter.bi_size = KEY_SIZE(&io->w->key) << 9;
Kent Overstreet220bb382013-09-10 19:02:45 -070087 bio->bi_private = &io->cl;
Kent Overstreet169ef1c2013-03-28 12:50:55 -060088 bch_bio_map(bio, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -070089}
90
91static void write_moving(struct closure *cl)
92{
Kent Overstreet220bb382013-09-10 19:02:45 -070093 struct moving_io *io = container_of(cl, struct moving_io, cl);
94 struct data_insert_op *op = &io->op;
Kent Overstreetcafe5632013-03-23 16:11:31 -070095
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020096 if (!op->status) {
Kent Overstreetcafe5632013-03-23 16:11:31 -070097 moving_init(io);
98
Kent Overstreet4f024f32013-10-11 15:44:27 -070099 io->bio.bio.bi_iter.bi_sector = KEY_START(&io->w->key);
Kent Overstreet220bb382013-09-10 19:02:45 -0700100 op->write_prio = 1;
101 op->bio = &io->bio.bio;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700102
Kent Overstreet220bb382013-09-10 19:02:45 -0700103 op->writeback = KEY_DIRTY(&io->w->key);
104 op->csum = KEY_CSUM(&io->w->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700105
Kent Overstreet220bb382013-09-10 19:02:45 -0700106 bkey_copy(&op->replace_key, &io->w->key);
107 op->replace = true;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700108
Kent Overstreet220bb382013-09-10 19:02:45 -0700109 closure_call(&op->cl, bch_data_insert, NULL, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700110 }
111
Nicholas Swensonda415a02014-01-09 16:03:04 -0800112 continue_at(cl, write_moving_finish, op->wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700113}
114
115static void read_moving_submit(struct closure *cl)
116{
Kent Overstreet220bb382013-09-10 19:02:45 -0700117 struct moving_io *io = container_of(cl, struct moving_io, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700118 struct bio *bio = &io->bio.bio;
119
Kent Overstreet220bb382013-09-10 19:02:45 -0700120 bch_submit_bbio(bio, io->op.c, &io->w->key, 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700121
Nicholas Swensonda415a02014-01-09 16:03:04 -0800122 continue_at(cl, write_moving, io->op.wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700123}
124
Kent Overstreet72a44512013-10-24 17:19:26 -0700125static void read_moving(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700126{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700127 struct keybuf_key *w;
128 struct moving_io *io;
129 struct bio *bio;
Kent Overstreet72a44512013-10-24 17:19:26 -0700130 struct closure cl;
131
132 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700133
134 /* XXX: if we error, background writeback could stall indefinitely */
135
136 while (!test_bit(CACHE_SET_STOPPING, &c->flags)) {
Kent Overstreet72c27062013-06-05 06:24:39 -0700137 w = bch_keybuf_next_rescan(c, &c->moving_gc_keys,
138 &MAX_KEY, moving_pred);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700139 if (!w)
140 break;
141
Kent Overstreet6d3d1a92013-12-16 14:12:09 -0800142 if (ptr_stale(c, &w->key, 0)) {
143 bch_keybuf_del(&c->moving_gc_keys, w);
144 continue;
145 }
146
Kent Overstreetcafe5632013-03-23 16:11:31 -0700147 io = kzalloc(sizeof(struct moving_io) + sizeof(struct bio_vec)
148 * DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS),
149 GFP_KERNEL);
150 if (!io)
151 goto err;
152
153 w->private = io;
154 io->w = w;
Kent Overstreet220bb382013-09-10 19:02:45 -0700155 io->op.inode = KEY_INODE(&w->key);
156 io->op.c = c;
Nicholas Swensonda415a02014-01-09 16:03:04 -0800157 io->op.wq = c->moving_gc_wq;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700158
159 moving_init(io);
160 bio = &io->bio.bio;
161
Mike Christiead0d9e72016-06-05 14:32:05 -0500162 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700163 bio->bi_end_io = read_moving_endio;
164
Ming Lei25d8be72017-12-18 20:22:10 +0800165 if (bch_bio_alloc_pages(bio, GFP_KERNEL))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700166 goto err;
167
Kent Overstreetc37511b2013-04-26 15:39:55 -0700168 trace_bcache_gc_copy(&w->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700169
Kent Overstreet72a44512013-10-24 17:19:26 -0700170 down(&c->moving_in_flight);
Kent Overstreet220bb382013-09-10 19:02:45 -0700171 closure_call(&io->cl, read_moving_submit, NULL, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700172 }
173
174 if (0) {
175err: if (!IS_ERR_OR_NULL(w->private))
176 kfree(w->private);
177
178 bch_keybuf_del(&c->moving_gc_keys, w);
179 }
180
Kent Overstreet72a44512013-10-24 17:19:26 -0700181 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700182}
183
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700184static bool bucket_cmp(struct bucket *l, struct bucket *r)
185{
186 return GC_SECTORS_USED(l) < GC_SECTORS_USED(r);
187}
188
189static unsigned bucket_heap_top(struct cache *ca)
190{
Nicholas Swensonbee63f42013-10-31 19:25:18 -0700191 struct bucket *b;
192 return (b = heap_peek(&ca->heap)) ? GC_SECTORS_USED(b) : 0;
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700193}
194
Kent Overstreet72a44512013-10-24 17:19:26 -0700195void bch_moving_gc(struct cache_set *c)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700196{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700197 struct cache *ca;
198 struct bucket *b;
199 unsigned i;
200
Kent Overstreetcafe5632013-03-23 16:11:31 -0700201 if (!c->copy_gc_enabled)
Kent Overstreet72a44512013-10-24 17:19:26 -0700202 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700203
204 mutex_lock(&c->bucket_lock);
205
206 for_each_cache(ca, c, i) {
207 unsigned sectors_to_move = 0;
208 unsigned reserve_sectors = ca->sb.bucket_size *
Kent Overstreet78365412013-12-17 01:29:34 -0800209 fifo_used(&ca->free[RESERVE_MOVINGGC]);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700210
211 ca->heap.used = 0;
212
213 for_each_bucket(b, ca) {
Nicholas Swenson3f6ef382014-01-23 15:21:02 -0800214 if (GC_MARK(b) == GC_MARK_METADATA ||
215 !GC_SECTORS_USED(b) ||
216 GC_SECTORS_USED(b) == ca->sb.bucket_size ||
217 atomic_read(&b->pin))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700218 continue;
219
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))) {
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700224 sectors_to_move -= bucket_heap_top(ca);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700225 sectors_to_move += GC_SECTORS_USED(b);
226
227 ca->heap.data[0] = b;
228 heap_sift(&ca->heap, 0, bucket_cmp);
229 }
230 }
231
232 while (sectors_to_move > reserve_sectors) {
233 heap_pop(&ca->heap, b, bucket_cmp);
234 sectors_to_move -= GC_SECTORS_USED(b);
235 }
236
Nicholas Swenson981aa8c2013-11-07 17:53:19 -0800237 while (heap_pop(&ca->heap, b, bucket_cmp))
238 SET_GC_MOVE(b, 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700239 }
240
241 mutex_unlock(&c->bucket_lock);
242
243 c->moving_gc_keys.last_scanned = ZERO_KEY;
244
Kent Overstreet72a44512013-10-24 17:19:26 -0700245 read_moving(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700246}
247
248void bch_moving_init_cache_set(struct cache_set *c)
249{
Kent Overstreet72c27062013-06-05 06:24:39 -0700250 bch_keybuf_init(&c->moving_gc_keys);
Kent Overstreet72a44512013-10-24 17:19:26 -0700251 sema_init(&c->moving_in_flight, 64);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700252}