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