Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1 | /* |
| 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 Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 12 | #include <trace/events/bcache.h> |
| 13 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 14 | struct moving_io { |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 15 | struct closure cl; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 16 | struct keybuf_key *w; |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 17 | struct data_insert_op op; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 18 | struct bbio bio; |
| 19 | }; |
| 20 | |
| 21 | static 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 Overstreet | 10d9dcf | 2014-02-17 15:48:36 -0800 | [diff] [blame] | 27 | for (i = 0; i < KEY_PTRS(k); i++) |
| 28 | if (ptr_available(c, k, i) && |
| 29 | GC_MOVE(PTR_BUCKET(c, k, i))) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 30 | return true; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 31 | |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | /* Moving GC - IO loop */ |
| 36 | |
| 37 | static void moving_io_destructor(struct closure *cl) |
| 38 | { |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 39 | struct moving_io *io = container_of(cl, struct moving_io, cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 40 | kfree(io); |
| 41 | } |
| 42 | |
| 43 | static void write_moving_finish(struct closure *cl) |
| 44 | { |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 45 | struct moving_io *io = container_of(cl, struct moving_io, cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 46 | struct bio *bio = &io->bio.bio; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 47 | |
Guoqing Jiang | 491221f | 2016-09-22 03:10:01 -0400 | [diff] [blame] | 48 | bio_free_pages(bio); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 49 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 50 | if (io->op.replace_collision) |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 51 | trace_bcache_gc_copy_collision(&io->w->key); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 52 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 53 | bch_keybuf_del(&io->op.c->moving_gc_keys, io->w); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 54 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 55 | up(&io->op.c->moving_in_flight); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 56 | |
| 57 | closure_return_with_destructor(cl, moving_io_destructor); |
| 58 | } |
| 59 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 60 | static void read_moving_endio(struct bio *bio) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 61 | { |
Kent Overstreet | 6d3d1a9 | 2013-12-16 14:12:09 -0800 | [diff] [blame] | 62 | struct bbio *b = container_of(bio, struct bbio, bio); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 63 | struct moving_io *io = container_of(bio->bi_private, |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 64 | struct moving_io, cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 65 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 66 | if (bio->bi_error) |
| 67 | io->op.error = bio->bi_error; |
Kent Overstreet | 6d3d1a9 | 2013-12-16 14:12:09 -0800 | [diff] [blame] | 68 | else if (!KEY_DIRTY(&b->key) && |
| 69 | ptr_stale(io->op.c, &b->key, 0)) { |
| 70 | io->op.error = -EINTR; |
| 71 | } |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 72 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 73 | bch_bbio_endio(io->op.c, bio, bio->bi_error, "reading data to move"); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static void moving_init(struct moving_io *io) |
| 77 | { |
| 78 | struct bio *bio = &io->bio.bio; |
| 79 | |
Ming Lei | 3a83f46 | 2016-11-22 08:57:21 -0700 | [diff] [blame^] | 80 | bio_init(bio, bio->bi_inline_vecs, |
| 81 | DIV_ROUND_UP(KEY_SIZE(&io->w->key), PAGE_SECTORS)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 82 | bio_get(bio); |
| 83 | bio_set_prio(bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0)); |
| 84 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 85 | bio->bi_iter.bi_size = KEY_SIZE(&io->w->key) << 9; |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 86 | bio->bi_private = &io->cl; |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 87 | bch_bio_map(bio, NULL); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static void write_moving(struct closure *cl) |
| 91 | { |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 92 | struct moving_io *io = container_of(cl, struct moving_io, cl); |
| 93 | struct data_insert_op *op = &io->op; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 94 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 95 | if (!op->error) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 96 | moving_init(io); |
| 97 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 98 | io->bio.bio.bi_iter.bi_sector = KEY_START(&io->w->key); |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 99 | op->write_prio = 1; |
| 100 | op->bio = &io->bio.bio; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 101 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 102 | op->writeback = KEY_DIRTY(&io->w->key); |
| 103 | op->csum = KEY_CSUM(&io->w->key); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 104 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 105 | bkey_copy(&op->replace_key, &io->w->key); |
| 106 | op->replace = true; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 107 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 108 | closure_call(&op->cl, bch_data_insert, NULL, cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Nicholas Swenson | da415a0 | 2014-01-09 16:03:04 -0800 | [diff] [blame] | 111 | continue_at(cl, write_moving_finish, op->wq); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static void read_moving_submit(struct closure *cl) |
| 115 | { |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 116 | struct moving_io *io = container_of(cl, struct moving_io, cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 117 | struct bio *bio = &io->bio.bio; |
| 118 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 119 | bch_submit_bbio(bio, io->op.c, &io->w->key, 0); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 120 | |
Nicholas Swenson | da415a0 | 2014-01-09 16:03:04 -0800 | [diff] [blame] | 121 | continue_at(cl, write_moving, io->op.wq); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Kent Overstreet | 72a4451 | 2013-10-24 17:19:26 -0700 | [diff] [blame] | 124 | static void read_moving(struct cache_set *c) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 125 | { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 126 | struct keybuf_key *w; |
| 127 | struct moving_io *io; |
| 128 | struct bio *bio; |
Kent Overstreet | 72a4451 | 2013-10-24 17:19:26 -0700 | [diff] [blame] | 129 | struct closure cl; |
| 130 | |
| 131 | closure_init_stack(&cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 132 | |
| 133 | /* XXX: if we error, background writeback could stall indefinitely */ |
| 134 | |
| 135 | while (!test_bit(CACHE_SET_STOPPING, &c->flags)) { |
Kent Overstreet | 72c2706 | 2013-06-05 06:24:39 -0700 | [diff] [blame] | 136 | w = bch_keybuf_next_rescan(c, &c->moving_gc_keys, |
| 137 | &MAX_KEY, moving_pred); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 138 | if (!w) |
| 139 | break; |
| 140 | |
Kent Overstreet | 6d3d1a9 | 2013-12-16 14:12:09 -0800 | [diff] [blame] | 141 | if (ptr_stale(c, &w->key, 0)) { |
| 142 | bch_keybuf_del(&c->moving_gc_keys, w); |
| 143 | continue; |
| 144 | } |
| 145 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 146 | io = kzalloc(sizeof(struct moving_io) + sizeof(struct bio_vec) |
| 147 | * DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS), |
| 148 | GFP_KERNEL); |
| 149 | if (!io) |
| 150 | goto err; |
| 151 | |
| 152 | w->private = io; |
| 153 | io->w = w; |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 154 | io->op.inode = KEY_INODE(&w->key); |
| 155 | io->op.c = c; |
Nicholas Swenson | da415a0 | 2014-01-09 16:03:04 -0800 | [diff] [blame] | 156 | io->op.wq = c->moving_gc_wq; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 157 | |
| 158 | moving_init(io); |
| 159 | bio = &io->bio.bio; |
| 160 | |
Mike Christie | ad0d9e7 | 2016-06-05 14:32:05 -0500 | [diff] [blame] | 161 | bio_set_op_attrs(bio, REQ_OP_READ, 0); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 162 | bio->bi_end_io = read_moving_endio; |
| 163 | |
Kent Overstreet | 8e51e41 | 2013-06-06 18:15:57 -0700 | [diff] [blame] | 164 | if (bio_alloc_pages(bio, GFP_KERNEL)) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 165 | goto err; |
| 166 | |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 167 | trace_bcache_gc_copy(&w->key); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 168 | |
Kent Overstreet | 72a4451 | 2013-10-24 17:19:26 -0700 | [diff] [blame] | 169 | down(&c->moving_in_flight); |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 170 | closure_call(&io->cl, read_moving_submit, NULL, &cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | if (0) { |
| 174 | err: if (!IS_ERR_OR_NULL(w->private)) |
| 175 | kfree(w->private); |
| 176 | |
| 177 | bch_keybuf_del(&c->moving_gc_keys, w); |
| 178 | } |
| 179 | |
Kent Overstreet | 72a4451 | 2013-10-24 17:19:26 -0700 | [diff] [blame] | 180 | closure_sync(&cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Kent Overstreet | b1a67b0 | 2013-03-25 11:46:44 -0700 | [diff] [blame] | 183 | static bool bucket_cmp(struct bucket *l, struct bucket *r) |
| 184 | { |
| 185 | return GC_SECTORS_USED(l) < GC_SECTORS_USED(r); |
| 186 | } |
| 187 | |
| 188 | static unsigned bucket_heap_top(struct cache *ca) |
| 189 | { |
Nicholas Swenson | bee63f4 | 2013-10-31 19:25:18 -0700 | [diff] [blame] | 190 | struct bucket *b; |
| 191 | return (b = heap_peek(&ca->heap)) ? GC_SECTORS_USED(b) : 0; |
Kent Overstreet | b1a67b0 | 2013-03-25 11:46:44 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Kent Overstreet | 72a4451 | 2013-10-24 17:19:26 -0700 | [diff] [blame] | 194 | void bch_moving_gc(struct cache_set *c) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 195 | { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 196 | struct cache *ca; |
| 197 | struct bucket *b; |
| 198 | unsigned i; |
| 199 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 200 | if (!c->copy_gc_enabled) |
Kent Overstreet | 72a4451 | 2013-10-24 17:19:26 -0700 | [diff] [blame] | 201 | return; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 202 | |
| 203 | mutex_lock(&c->bucket_lock); |
| 204 | |
| 205 | for_each_cache(ca, c, i) { |
| 206 | unsigned sectors_to_move = 0; |
| 207 | unsigned reserve_sectors = ca->sb.bucket_size * |
Kent Overstreet | 7836541 | 2013-12-17 01:29:34 -0800 | [diff] [blame] | 208 | fifo_used(&ca->free[RESERVE_MOVINGGC]); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 209 | |
| 210 | ca->heap.used = 0; |
| 211 | |
| 212 | for_each_bucket(b, ca) { |
Nicholas Swenson | 3f6ef38 | 2014-01-23 15:21:02 -0800 | [diff] [blame] | 213 | if (GC_MARK(b) == GC_MARK_METADATA || |
| 214 | !GC_SECTORS_USED(b) || |
| 215 | GC_SECTORS_USED(b) == ca->sb.bucket_size || |
| 216 | atomic_read(&b->pin)) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 217 | continue; |
| 218 | |
| 219 | if (!heap_full(&ca->heap)) { |
| 220 | sectors_to_move += GC_SECTORS_USED(b); |
| 221 | heap_add(&ca->heap, b, bucket_cmp); |
| 222 | } else if (bucket_cmp(b, heap_peek(&ca->heap))) { |
Kent Overstreet | b1a67b0 | 2013-03-25 11:46:44 -0700 | [diff] [blame] | 223 | sectors_to_move -= bucket_heap_top(ca); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 224 | sectors_to_move += GC_SECTORS_USED(b); |
| 225 | |
| 226 | ca->heap.data[0] = b; |
| 227 | heap_sift(&ca->heap, 0, bucket_cmp); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | while (sectors_to_move > reserve_sectors) { |
| 232 | heap_pop(&ca->heap, b, bucket_cmp); |
| 233 | sectors_to_move -= GC_SECTORS_USED(b); |
| 234 | } |
| 235 | |
Nicholas Swenson | 981aa8c | 2013-11-07 17:53:19 -0800 | [diff] [blame] | 236 | while (heap_pop(&ca->heap, b, bucket_cmp)) |
| 237 | SET_GC_MOVE(b, 1); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | mutex_unlock(&c->bucket_lock); |
| 241 | |
| 242 | c->moving_gc_keys.last_scanned = ZERO_KEY; |
| 243 | |
Kent Overstreet | 72a4451 | 2013-10-24 17:19:26 -0700 | [diff] [blame] | 244 | read_moving(c); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void bch_moving_init_cache_set(struct cache_set *c) |
| 248 | { |
Kent Overstreet | 72c2706 | 2013-06-05 06:24:39 -0700 | [diff] [blame] | 249 | bch_keybuf_init(&c->moving_gc_keys); |
Kent Overstreet | 72a4451 | 2013-10-24 17:19:26 -0700 | [diff] [blame] | 250 | sema_init(&c->moving_in_flight, 64); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 251 | } |