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