movinggc.c 5.5 KB
Newer Older
K
Kent Overstreet 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * Moving/copying garbage collector
 *
 * Copyright 2012 Google, Inc.
 */

#include "bcache.h"
#include "btree.h"
#include "debug.h"
#include "request.h"

K
Kent Overstreet 已提交
12 13
#include <trace/events/bcache.h>

K
Kent Overstreet 已提交
14
struct moving_io {
K
Kent Overstreet 已提交
15
	struct closure		cl;
K
Kent Overstreet 已提交
16
	struct keybuf_key	*w;
K
Kent Overstreet 已提交
17
	struct data_insert_op	op;
K
Kent Overstreet 已提交
18 19 20 21 22 23 24 25 26
	struct bbio		bio;
};

static bool moving_pred(struct keybuf *buf, struct bkey *k)
{
	struct cache_set *c = container_of(buf, struct cache_set,
					   moving_gc_keys);
	unsigned i;

K
Kent Overstreet 已提交
27 28 29
	for (i = 0; i < KEY_PTRS(k); i++)
		if (ptr_available(c, k, i) &&
		    GC_MOVE(PTR_BUCKET(c, k, i)))
K
Kent Overstreet 已提交
30 31 32 33 34 35 36 37 38
			return true;

	return false;
}

/* Moving GC - IO loop */

static void moving_io_destructor(struct closure *cl)
{
K
Kent Overstreet 已提交
39
	struct moving_io *io = container_of(cl, struct moving_io, cl);
K
Kent Overstreet 已提交
40 41 42 43 44
	kfree(io);
}

static void write_moving_finish(struct closure *cl)
{
K
Kent Overstreet 已提交
45
	struct moving_io *io = container_of(cl, struct moving_io, cl);
K
Kent Overstreet 已提交
46
	struct bio *bio = &io->bio.bio;
47 48
	struct bio_vec *bv;
	int i;
K
Kent Overstreet 已提交
49

50
	bio_for_each_segment_all(bv, bio, i)
K
Kent Overstreet 已提交
51 52
		__free_page(bv->bv_page);

K
Kent Overstreet 已提交
53
	if (io->op.replace_collision)
K
Kent Overstreet 已提交
54
		trace_bcache_gc_copy_collision(&io->w->key);
K
Kent Overstreet 已提交
55

K
Kent Overstreet 已提交
56
	bch_keybuf_del(&io->op.c->moving_gc_keys, io->w);
K
Kent Overstreet 已提交
57

K
Kent Overstreet 已提交
58
	up(&io->op.c->moving_in_flight);
K
Kent Overstreet 已提交
59 60 61 62

	closure_return_with_destructor(cl, moving_io_destructor);
}

63
static void read_moving_endio(struct bio *bio)
K
Kent Overstreet 已提交
64
{
65
	struct bbio *b = container_of(bio, struct bbio, bio);
K
Kent Overstreet 已提交
66
	struct moving_io *io = container_of(bio->bi_private,
K
Kent Overstreet 已提交
67
					    struct moving_io, cl);
K
Kent Overstreet 已提交
68

69 70
	if (bio->bi_error)
		io->op.error = bio->bi_error;
71 72 73 74
	else if (!KEY_DIRTY(&b->key) &&
		 ptr_stale(io->op.c, &b->key, 0)) {
		io->op.error = -EINTR;
	}
K
Kent Overstreet 已提交
75

76
	bch_bbio_endio(io->op.c, bio, bio->bi_error, "reading data to move");
K
Kent Overstreet 已提交
77 78 79 80 81 82 83 84 85 86
}

static void moving_init(struct moving_io *io)
{
	struct bio *bio = &io->bio.bio;

	bio_init(bio);
	bio_get(bio);
	bio_set_prio(bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));

87
	bio->bi_iter.bi_size	= KEY_SIZE(&io->w->key) << 9;
K
Kent Overstreet 已提交
88 89
	bio->bi_max_vecs	= DIV_ROUND_UP(KEY_SIZE(&io->w->key),
					       PAGE_SECTORS);
K
Kent Overstreet 已提交
90
	bio->bi_private		= &io->cl;
K
Kent Overstreet 已提交
91
	bio->bi_io_vec		= bio->bi_inline_vecs;
92
	bch_bio_map(bio, NULL);
K
Kent Overstreet 已提交
93 94 95 96
}

static void write_moving(struct closure *cl)
{
K
Kent Overstreet 已提交
97 98
	struct moving_io *io = container_of(cl, struct moving_io, cl);
	struct data_insert_op *op = &io->op;
K
Kent Overstreet 已提交
99

K
Kent Overstreet 已提交
100
	if (!op->error) {
K
Kent Overstreet 已提交
101 102
		moving_init(io);

103
		io->bio.bio.bi_iter.bi_sector = KEY_START(&io->w->key);
K
Kent Overstreet 已提交
104 105
		op->write_prio		= 1;
		op->bio			= &io->bio.bio;
K
Kent Overstreet 已提交
106

K
Kent Overstreet 已提交
107 108
		op->writeback		= KEY_DIRTY(&io->w->key);
		op->csum		= KEY_CSUM(&io->w->key);
K
Kent Overstreet 已提交
109

K
Kent Overstreet 已提交
110 111
		bkey_copy(&op->replace_key, &io->w->key);
		op->replace		= true;
K
Kent Overstreet 已提交
112

K
Kent Overstreet 已提交
113
		closure_call(&op->cl, bch_data_insert, NULL, cl);
K
Kent Overstreet 已提交
114 115
	}

116
	continue_at(cl, write_moving_finish, op->wq);
K
Kent Overstreet 已提交
117 118 119 120
}

static void read_moving_submit(struct closure *cl)
{
K
Kent Overstreet 已提交
121
	struct moving_io *io = container_of(cl, struct moving_io, cl);
K
Kent Overstreet 已提交
122 123
	struct bio *bio = &io->bio.bio;

K
Kent Overstreet 已提交
124
	bch_submit_bbio(bio, io->op.c, &io->w->key, 0);
K
Kent Overstreet 已提交
125

126
	continue_at(cl, write_moving, io->op.wq);
K
Kent Overstreet 已提交
127 128
}

K
Kent Overstreet 已提交
129
static void read_moving(struct cache_set *c)
K
Kent Overstreet 已提交
130 131 132 133
{
	struct keybuf_key *w;
	struct moving_io *io;
	struct bio *bio;
K
Kent Overstreet 已提交
134 135 136
	struct closure cl;

	closure_init_stack(&cl);
K
Kent Overstreet 已提交
137 138 139 140

	/* XXX: if we error, background writeback could stall indefinitely */

	while (!test_bit(CACHE_SET_STOPPING, &c->flags)) {
K
Kent Overstreet 已提交
141 142
		w = bch_keybuf_next_rescan(c, &c->moving_gc_keys,
					   &MAX_KEY, moving_pred);
K
Kent Overstreet 已提交
143 144 145
		if (!w)
			break;

146 147 148 149 150
		if (ptr_stale(c, &w->key, 0)) {
			bch_keybuf_del(&c->moving_gc_keys, w);
			continue;
		}

K
Kent Overstreet 已提交
151 152 153 154 155 156 157 158
		io = kzalloc(sizeof(struct moving_io) + sizeof(struct bio_vec)
			     * DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS),
			     GFP_KERNEL);
		if (!io)
			goto err;

		w->private	= io;
		io->w		= w;
K
Kent Overstreet 已提交
159 160
		io->op.inode	= KEY_INODE(&w->key);
		io->op.c	= c;
161
		io->op.wq	= c->moving_gc_wq;
K
Kent Overstreet 已提交
162 163 164 165

		moving_init(io);
		bio = &io->bio.bio;

M
Mike Christie 已提交
166
		bio_set_op_attrs(bio, REQ_OP_READ, 0);
K
Kent Overstreet 已提交
167 168
		bio->bi_end_io	= read_moving_endio;

169
		if (bio_alloc_pages(bio, GFP_KERNEL))
K
Kent Overstreet 已提交
170 171
			goto err;

K
Kent Overstreet 已提交
172
		trace_bcache_gc_copy(&w->key);
K
Kent Overstreet 已提交
173

K
Kent Overstreet 已提交
174
		down(&c->moving_in_flight);
K
Kent Overstreet 已提交
175
		closure_call(&io->cl, read_moving_submit, NULL, &cl);
K
Kent Overstreet 已提交
176 177 178 179 180 181 182 183 184
	}

	if (0) {
err:		if (!IS_ERR_OR_NULL(w->private))
			kfree(w->private);

		bch_keybuf_del(&c->moving_gc_keys, w);
	}

K
Kent Overstreet 已提交
185
	closure_sync(&cl);
K
Kent Overstreet 已提交
186 187
}

K
Kent Overstreet 已提交
188 189 190 191 192 193 194
static bool bucket_cmp(struct bucket *l, struct bucket *r)
{
	return GC_SECTORS_USED(l) < GC_SECTORS_USED(r);
}

static unsigned bucket_heap_top(struct cache *ca)
{
195 196
	struct bucket *b;
	return (b = heap_peek(&ca->heap)) ? GC_SECTORS_USED(b) : 0;
K
Kent Overstreet 已提交
197 198
}

K
Kent Overstreet 已提交
199
void bch_moving_gc(struct cache_set *c)
K
Kent Overstreet 已提交
200 201 202 203 204 205
{
	struct cache *ca;
	struct bucket *b;
	unsigned i;

	if (!c->copy_gc_enabled)
K
Kent Overstreet 已提交
206
		return;
K
Kent Overstreet 已提交
207 208 209 210 211 212

	mutex_lock(&c->bucket_lock);

	for_each_cache(ca, c, i) {
		unsigned sectors_to_move = 0;
		unsigned reserve_sectors = ca->sb.bucket_size *
213
			fifo_used(&ca->free[RESERVE_MOVINGGC]);
K
Kent Overstreet 已提交
214 215 216 217

		ca->heap.used = 0;

		for_each_bucket(b, ca) {
218 219 220 221
			if (GC_MARK(b) == GC_MARK_METADATA ||
			    !GC_SECTORS_USED(b) ||
			    GC_SECTORS_USED(b) == ca->sb.bucket_size ||
			    atomic_read(&b->pin))
K
Kent Overstreet 已提交
222 223 224 225 226 227
				continue;

			if (!heap_full(&ca->heap)) {
				sectors_to_move += GC_SECTORS_USED(b);
				heap_add(&ca->heap, b, bucket_cmp);
			} else if (bucket_cmp(b, heap_peek(&ca->heap))) {
K
Kent Overstreet 已提交
228
				sectors_to_move -= bucket_heap_top(ca);
K
Kent Overstreet 已提交
229 230 231 232 233 234 235 236 237 238 239 240
				sectors_to_move += GC_SECTORS_USED(b);

				ca->heap.data[0] = b;
				heap_sift(&ca->heap, 0, bucket_cmp);
			}
		}

		while (sectors_to_move > reserve_sectors) {
			heap_pop(&ca->heap, b, bucket_cmp);
			sectors_to_move -= GC_SECTORS_USED(b);
		}

241 242
		while (heap_pop(&ca->heap, b, bucket_cmp))
			SET_GC_MOVE(b, 1);
K
Kent Overstreet 已提交
243 244 245 246 247 248
	}

	mutex_unlock(&c->bucket_lock);

	c->moving_gc_keys.last_scanned = ZERO_KEY;

K
Kent Overstreet 已提交
249
	read_moving(c);
K
Kent Overstreet 已提交
250 251 252 253
}

void bch_moving_init_cache_set(struct cache_set *c)
{
K
Kent Overstreet 已提交
254
	bch_keybuf_init(&c->moving_gc_keys);
K
Kent Overstreet 已提交
255
	sema_init(&c->moving_in_flight, 64);
K
Kent Overstreet 已提交
256
}