Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * background writeback - scan btree for dirty data and write it to the backing |
| 3 | * device |
| 4 | * |
| 5 | * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com> |
| 6 | * Copyright 2012 Google, Inc. |
| 7 | */ |
| 8 | |
| 9 | #include "bcache.h" |
| 10 | #include "btree.h" |
| 11 | #include "debug.h" |
Kent Overstreet | 279afba | 2013-06-05 06:21:07 -0700 | [diff] [blame] | 12 | #include "writeback.h" |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 13 | |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 14 | #include <linux/delay.h> |
| 15 | #include <linux/freezer.h> |
| 16 | #include <linux/kthread.h> |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 17 | #include <trace/events/bcache.h> |
| 18 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 19 | /* Rate limiting */ |
| 20 | |
| 21 | static void __update_writeback_rate(struct cached_dev *dc) |
| 22 | { |
| 23 | struct cache_set *c = dc->disk.c; |
| 24 | uint64_t cache_sectors = c->nbuckets * c->sb.bucket_size; |
| 25 | uint64_t cache_dirty_target = |
| 26 | div_u64(cache_sectors * dc->writeback_percent, 100); |
| 27 | |
| 28 | int64_t target = div64_u64(cache_dirty_target * bdev_sectors(dc->bdev), |
| 29 | c->cached_dev_sectors); |
| 30 | |
| 31 | /* PD controller */ |
| 32 | |
Kent Overstreet | 279afba | 2013-06-05 06:21:07 -0700 | [diff] [blame] | 33 | int64_t dirty = bcache_dev_sectors_dirty(&dc->disk); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 34 | int64_t derivative = dirty - dc->disk.sectors_dirty_last; |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 35 | int64_t proportional = dirty - target; |
| 36 | int64_t change; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 37 | |
| 38 | dc->disk.sectors_dirty_last = dirty; |
| 39 | |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 40 | /* Scale to sectors per second */ |
| 41 | |
| 42 | proportional *= dc->writeback_rate_update_seconds; |
| 43 | proportional = div_s64(proportional, dc->writeback_rate_p_term_inverse); |
| 44 | |
| 45 | derivative = div_s64(derivative, dc->writeback_rate_update_seconds); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 46 | |
| 47 | derivative = ewma_add(dc->disk.sectors_dirty_derivative, derivative, |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 48 | (dc->writeback_rate_d_term / |
| 49 | dc->writeback_rate_update_seconds) ?: 1, 0); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 50 | |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 51 | derivative *= dc->writeback_rate_d_term; |
| 52 | derivative = div_s64(derivative, dc->writeback_rate_p_term_inverse); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 53 | |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 54 | change = proportional + derivative; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 55 | |
| 56 | /* Don't increase writeback rate if the device isn't keeping up */ |
| 57 | if (change > 0 && |
| 58 | time_after64(local_clock(), |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 59 | dc->writeback_rate.next + NSEC_PER_MSEC)) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 60 | change = 0; |
| 61 | |
| 62 | dc->writeback_rate.rate = |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 63 | clamp_t(int64_t, (int64_t) dc->writeback_rate.rate + change, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 64 | 1, NSEC_PER_MSEC); |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 65 | |
| 66 | dc->writeback_rate_proportional = proportional; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 67 | dc->writeback_rate_derivative = derivative; |
| 68 | dc->writeback_rate_change = change; |
| 69 | dc->writeback_rate_target = target; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static void update_writeback_rate(struct work_struct *work) |
| 73 | { |
| 74 | struct cached_dev *dc = container_of(to_delayed_work(work), |
| 75 | struct cached_dev, |
| 76 | writeback_rate_update); |
| 77 | |
| 78 | down_read(&dc->writeback_lock); |
| 79 | |
| 80 | if (atomic_read(&dc->has_dirty) && |
| 81 | dc->writeback_percent) |
| 82 | __update_writeback_rate(dc); |
| 83 | |
| 84 | up_read(&dc->writeback_lock); |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 85 | |
| 86 | schedule_delayed_work(&dc->writeback_rate_update, |
| 87 | dc->writeback_rate_update_seconds * HZ); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static unsigned writeback_delay(struct cached_dev *dc, unsigned sectors) |
| 91 | { |
Kent Overstreet | c4d951d | 2013-08-21 17:49:09 -0700 | [diff] [blame] | 92 | if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) || |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 93 | !dc->writeback_percent) |
| 94 | return 0; |
| 95 | |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 96 | return bch_next_delay(&dc->writeback_rate, sectors); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 99 | struct dirty_io { |
| 100 | struct closure cl; |
| 101 | struct cached_dev *dc; |
| 102 | struct bio bio; |
| 103 | }; |
Kent Overstreet | 72c2706 | 2013-06-05 06:24:39 -0700 | [diff] [blame] | 104 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 105 | static void dirty_init(struct keybuf_key *w) |
| 106 | { |
| 107 | struct dirty_io *io = w->private; |
| 108 | struct bio *bio = &io->bio; |
| 109 | |
| 110 | bio_init(bio); |
| 111 | if (!io->dc->writeback_percent) |
| 112 | bio_set_prio(bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0)); |
| 113 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 114 | bio->bi_iter.bi_size = KEY_SIZE(&w->key) << 9; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 115 | bio->bi_max_vecs = DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS); |
| 116 | bio->bi_private = w; |
| 117 | bio->bi_io_vec = bio->bi_inline_vecs; |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 118 | bch_bio_map(bio, NULL); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 121 | static void dirty_io_destructor(struct closure *cl) |
| 122 | { |
| 123 | struct dirty_io *io = container_of(cl, struct dirty_io, cl); |
| 124 | kfree(io); |
| 125 | } |
| 126 | |
| 127 | static void write_dirty_finish(struct closure *cl) |
| 128 | { |
| 129 | struct dirty_io *io = container_of(cl, struct dirty_io, cl); |
| 130 | struct keybuf_key *w = io->bio.bi_private; |
| 131 | struct cached_dev *dc = io->dc; |
Kent Overstreet | 8e51e41 | 2013-06-06 18:15:57 -0700 | [diff] [blame] | 132 | struct bio_vec *bv; |
| 133 | int i; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 134 | |
Kent Overstreet | 8e51e41 | 2013-06-06 18:15:57 -0700 | [diff] [blame] | 135 | bio_for_each_segment_all(bv, &io->bio, i) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 136 | __free_page(bv->bv_page); |
| 137 | |
| 138 | /* This is kind of a dumb way of signalling errors. */ |
| 139 | if (KEY_DIRTY(&w->key)) { |
Kent Overstreet | 6054c6d | 2013-07-24 18:06:22 -0700 | [diff] [blame] | 140 | int ret; |
Kent Overstreet | cc7b881 | 2013-07-24 18:07:22 -0700 | [diff] [blame] | 141 | unsigned i; |
| 142 | struct keylist keys; |
Kent Overstreet | 0b93207 | 2013-07-24 17:26:51 -0700 | [diff] [blame] | 143 | |
Kent Overstreet | 0b93207 | 2013-07-24 17:26:51 -0700 | [diff] [blame] | 144 | bch_keylist_init(&keys); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 145 | |
Kent Overstreet | 1b207d8 | 2013-09-10 18:52:54 -0700 | [diff] [blame] | 146 | bkey_copy(keys.top, &w->key); |
| 147 | SET_KEY_DIRTY(keys.top, false); |
| 148 | bch_keylist_push(&keys); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 149 | |
| 150 | for (i = 0; i < KEY_PTRS(&w->key); i++) |
| 151 | atomic_inc(&PTR_BUCKET(dc->disk.c, &w->key, i)->pin); |
| 152 | |
Kent Overstreet | cc7b881 | 2013-07-24 18:07:22 -0700 | [diff] [blame] | 153 | ret = bch_btree_insert(dc->disk.c, &keys, NULL, &w->key); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 154 | |
Kent Overstreet | 6054c6d | 2013-07-24 18:06:22 -0700 | [diff] [blame] | 155 | if (ret) |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 156 | trace_bcache_writeback_collision(&w->key); |
| 157 | |
Kent Overstreet | 6054c6d | 2013-07-24 18:06:22 -0700 | [diff] [blame] | 158 | atomic_long_inc(ret |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 159 | ? &dc->disk.c->writeback_keys_failed |
| 160 | : &dc->disk.c->writeback_keys_done); |
| 161 | } |
| 162 | |
| 163 | bch_keybuf_del(&dc->writeback_keys, w); |
Kent Overstreet | c2a4f31 | 2013-09-23 23:17:31 -0700 | [diff] [blame] | 164 | up(&dc->in_flight); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 165 | |
| 166 | closure_return_with_destructor(cl, dirty_io_destructor); |
| 167 | } |
| 168 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 169 | static void dirty_endio(struct bio *bio) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 170 | { |
| 171 | struct keybuf_key *w = bio->bi_private; |
| 172 | struct dirty_io *io = w->private; |
| 173 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 174 | if (bio->bi_error) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 175 | SET_KEY_DIRTY(&w->key, false); |
| 176 | |
| 177 | closure_put(&io->cl); |
| 178 | } |
| 179 | |
| 180 | static void write_dirty(struct closure *cl) |
| 181 | { |
| 182 | struct dirty_io *io = container_of(cl, struct dirty_io, cl); |
| 183 | struct keybuf_key *w = io->bio.bi_private; |
| 184 | |
| 185 | dirty_init(w); |
| 186 | io->bio.bi_rw = WRITE; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 187 | io->bio.bi_iter.bi_sector = KEY_START(&w->key); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 188 | io->bio.bi_bdev = io->dc->bdev; |
| 189 | io->bio.bi_end_io = dirty_endio; |
| 190 | |
Kent Overstreet | 749b61d | 2013-11-23 23:11:25 -0800 | [diff] [blame] | 191 | closure_bio_submit(&io->bio, cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 192 | |
Kent Overstreet | c2a4f31 | 2013-09-23 23:17:31 -0700 | [diff] [blame] | 193 | continue_at(cl, write_dirty_finish, system_wq); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 196 | static void read_dirty_endio(struct bio *bio) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 197 | { |
| 198 | struct keybuf_key *w = bio->bi_private; |
| 199 | struct dirty_io *io = w->private; |
| 200 | |
| 201 | bch_count_io_errors(PTR_CACHE(io->dc->disk.c, &w->key, 0), |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 202 | bio->bi_error, "reading dirty data from cache"); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 203 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 204 | dirty_endio(bio); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static void read_dirty_submit(struct closure *cl) |
| 208 | { |
| 209 | struct dirty_io *io = container_of(cl, struct dirty_io, cl); |
| 210 | |
Kent Overstreet | 749b61d | 2013-11-23 23:11:25 -0800 | [diff] [blame] | 211 | closure_bio_submit(&io->bio, cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 212 | |
Kent Overstreet | c2a4f31 | 2013-09-23 23:17:31 -0700 | [diff] [blame] | 213 | continue_at(cl, write_dirty, system_wq); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 216 | static void read_dirty(struct cached_dev *dc) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 217 | { |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 218 | unsigned delay = 0; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 219 | struct keybuf_key *w; |
| 220 | struct dirty_io *io; |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 221 | struct closure cl; |
| 222 | |
| 223 | closure_init_stack(&cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 224 | |
| 225 | /* |
| 226 | * XXX: if we error, background writeback just spins. Should use some |
| 227 | * mempools. |
| 228 | */ |
| 229 | |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 230 | while (!kthread_should_stop()) { |
| 231 | try_to_freeze(); |
| 232 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 233 | w = bch_keybuf_next(&dc->writeback_keys); |
| 234 | if (!w) |
| 235 | break; |
| 236 | |
| 237 | BUG_ON(ptr_stale(dc->disk.c, &w->key, 0)); |
| 238 | |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 239 | if (KEY_START(&w->key) != dc->last_read || |
| 240 | jiffies_to_msecs(delay) > 50) |
| 241 | while (!kthread_should_stop() && delay) |
Slava Pestov | 9e5c353 | 2014-05-01 13:48:57 -0700 | [diff] [blame] | 242 | delay = schedule_timeout_interruptible(delay); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 243 | |
| 244 | dc->last_read = KEY_OFFSET(&w->key); |
| 245 | |
| 246 | io = kzalloc(sizeof(struct dirty_io) + sizeof(struct bio_vec) |
| 247 | * DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS), |
| 248 | GFP_KERNEL); |
| 249 | if (!io) |
| 250 | goto err; |
| 251 | |
| 252 | w->private = io; |
| 253 | io->dc = dc; |
| 254 | |
| 255 | dirty_init(w); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 256 | io->bio.bi_iter.bi_sector = PTR_OFFSET(&w->key, 0); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 257 | io->bio.bi_bdev = PTR_CACHE(dc->disk.c, |
| 258 | &w->key, 0)->bdev; |
| 259 | io->bio.bi_rw = READ; |
| 260 | io->bio.bi_end_io = read_dirty_endio; |
| 261 | |
Kent Overstreet | 8e51e41 | 2013-06-06 18:15:57 -0700 | [diff] [blame] | 262 | if (bio_alloc_pages(&io->bio, GFP_KERNEL)) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 263 | goto err_free; |
| 264 | |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 265 | trace_bcache_writeback(&w->key); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 266 | |
Kent Overstreet | c2a4f31 | 2013-09-23 23:17:31 -0700 | [diff] [blame] | 267 | down(&dc->in_flight); |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 268 | closure_call(&io->cl, read_dirty_submit, NULL, &cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 269 | |
| 270 | delay = writeback_delay(dc, KEY_SIZE(&w->key)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | if (0) { |
| 274 | err_free: |
| 275 | kfree(w->private); |
| 276 | err: |
| 277 | bch_keybuf_del(&dc->writeback_keys, w); |
| 278 | } |
| 279 | |
Kent Overstreet | c2a4f31 | 2013-09-23 23:17:31 -0700 | [diff] [blame] | 280 | /* |
| 281 | * Wait for outstanding writeback IOs to finish (and keybuf slots to be |
| 282 | * freed) before refilling again |
| 283 | */ |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 284 | closure_sync(&cl); |
| 285 | } |
| 286 | |
| 287 | /* Scan for dirty data */ |
| 288 | |
| 289 | void bcache_dev_sectors_dirty_add(struct cache_set *c, unsigned inode, |
| 290 | uint64_t offset, int nr_sectors) |
| 291 | { |
| 292 | struct bcache_device *d = c->devices[inode]; |
Kent Overstreet | 48a915a | 2013-10-31 15:43:22 -0700 | [diff] [blame] | 293 | unsigned stripe_offset, stripe, sectors_dirty; |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 294 | |
| 295 | if (!d) |
| 296 | return; |
| 297 | |
Kent Overstreet | 48a915a | 2013-10-31 15:43:22 -0700 | [diff] [blame] | 298 | stripe = offset_to_stripe(d, offset); |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 299 | stripe_offset = offset & (d->stripe_size - 1); |
| 300 | |
| 301 | while (nr_sectors) { |
| 302 | int s = min_t(unsigned, abs(nr_sectors), |
| 303 | d->stripe_size - stripe_offset); |
| 304 | |
| 305 | if (nr_sectors < 0) |
| 306 | s = -s; |
| 307 | |
Kent Overstreet | 48a915a | 2013-10-31 15:43:22 -0700 | [diff] [blame] | 308 | if (stripe >= d->nr_stripes) |
| 309 | return; |
| 310 | |
| 311 | sectors_dirty = atomic_add_return(s, |
| 312 | d->stripe_sectors_dirty + stripe); |
| 313 | if (sectors_dirty == d->stripe_size) |
| 314 | set_bit(stripe, d->full_dirty_stripes); |
| 315 | else |
| 316 | clear_bit(stripe, d->full_dirty_stripes); |
| 317 | |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 318 | nr_sectors -= s; |
| 319 | stripe_offset = 0; |
| 320 | stripe++; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | static bool dirty_pred(struct keybuf *buf, struct bkey *k) |
| 325 | { |
| 326 | return KEY_DIRTY(k); |
| 327 | } |
| 328 | |
Kent Overstreet | 48a915a | 2013-10-31 15:43:22 -0700 | [diff] [blame] | 329 | static void refill_full_stripes(struct cached_dev *dc) |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 330 | { |
Kent Overstreet | 48a915a | 2013-10-31 15:43:22 -0700 | [diff] [blame] | 331 | struct keybuf *buf = &dc->writeback_keys; |
| 332 | unsigned start_stripe, stripe, next_stripe; |
| 333 | bool wrapped = false; |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 334 | |
Kent Overstreet | 48a915a | 2013-10-31 15:43:22 -0700 | [diff] [blame] | 335 | stripe = offset_to_stripe(&dc->disk, KEY_OFFSET(&buf->last_scanned)); |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 336 | |
Kent Overstreet | 48a915a | 2013-10-31 15:43:22 -0700 | [diff] [blame] | 337 | if (stripe >= dc->disk.nr_stripes) |
| 338 | stripe = 0; |
| 339 | |
| 340 | start_stripe = stripe; |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 341 | |
| 342 | while (1) { |
Kent Overstreet | 48a915a | 2013-10-31 15:43:22 -0700 | [diff] [blame] | 343 | stripe = find_next_bit(dc->disk.full_dirty_stripes, |
| 344 | dc->disk.nr_stripes, stripe); |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 345 | |
Kent Overstreet | 48a915a | 2013-10-31 15:43:22 -0700 | [diff] [blame] | 346 | if (stripe == dc->disk.nr_stripes) |
| 347 | goto next; |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 348 | |
Kent Overstreet | 48a915a | 2013-10-31 15:43:22 -0700 | [diff] [blame] | 349 | next_stripe = find_next_zero_bit(dc->disk.full_dirty_stripes, |
| 350 | dc->disk.nr_stripes, stripe); |
| 351 | |
| 352 | buf->last_scanned = KEY(dc->disk.id, |
| 353 | stripe * dc->disk.stripe_size, 0); |
| 354 | |
| 355 | bch_refill_keybuf(dc->disk.c, buf, |
| 356 | &KEY(dc->disk.id, |
| 357 | next_stripe * dc->disk.stripe_size, 0), |
| 358 | dirty_pred); |
| 359 | |
| 360 | if (array_freelist_empty(&buf->freelist)) |
| 361 | return; |
| 362 | |
| 363 | stripe = next_stripe; |
| 364 | next: |
| 365 | if (wrapped && stripe > start_stripe) |
| 366 | return; |
| 367 | |
| 368 | if (stripe == dc->disk.nr_stripes) { |
| 369 | stripe = 0; |
| 370 | wrapped = true; |
| 371 | } |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
| 375 | static bool refill_dirty(struct cached_dev *dc) |
| 376 | { |
| 377 | struct keybuf *buf = &dc->writeback_keys; |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 378 | struct bkey end = KEY(dc->disk.id, MAX_KEY_OFFSET, 0); |
Kent Overstreet | 48a915a | 2013-10-31 15:43:22 -0700 | [diff] [blame] | 379 | bool searched_from_start = false; |
| 380 | |
| 381 | if (dc->partial_stripes_expensive) { |
| 382 | refill_full_stripes(dc); |
| 383 | if (array_freelist_empty(&buf->freelist)) |
| 384 | return false; |
| 385 | } |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 386 | |
| 387 | if (bkey_cmp(&buf->last_scanned, &end) >= 0) { |
| 388 | buf->last_scanned = KEY(dc->disk.id, 0, 0); |
| 389 | searched_from_start = true; |
| 390 | } |
| 391 | |
Kent Overstreet | 48a915a | 2013-10-31 15:43:22 -0700 | [diff] [blame] | 392 | bch_refill_keybuf(dc->disk.c, buf, &end, dirty_pred); |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 393 | |
| 394 | return bkey_cmp(&buf->last_scanned, &end) >= 0 && searched_from_start; |
| 395 | } |
| 396 | |
| 397 | static int bch_writeback_thread(void *arg) |
| 398 | { |
| 399 | struct cached_dev *dc = arg; |
| 400 | bool searched_full_index; |
| 401 | |
| 402 | while (!kthread_should_stop()) { |
| 403 | down_write(&dc->writeback_lock); |
| 404 | if (!atomic_read(&dc->has_dirty) || |
Kent Overstreet | c4d951d | 2013-08-21 17:49:09 -0700 | [diff] [blame] | 405 | (!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) && |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 406 | !dc->writeback_running)) { |
| 407 | up_write(&dc->writeback_lock); |
| 408 | set_current_state(TASK_INTERRUPTIBLE); |
| 409 | |
| 410 | if (kthread_should_stop()) |
| 411 | return 0; |
| 412 | |
| 413 | try_to_freeze(); |
| 414 | schedule(); |
| 415 | continue; |
| 416 | } |
| 417 | |
| 418 | searched_full_index = refill_dirty(dc); |
| 419 | |
| 420 | if (searched_full_index && |
| 421 | RB_EMPTY_ROOT(&dc->writeback_keys.keys)) { |
| 422 | atomic_set(&dc->has_dirty, 0); |
| 423 | cached_dev_put(dc); |
| 424 | SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN); |
| 425 | bch_write_bdev_super(dc, NULL); |
| 426 | } |
| 427 | |
| 428 | up_write(&dc->writeback_lock); |
| 429 | |
| 430 | bch_ratelimit_reset(&dc->writeback_rate); |
| 431 | read_dirty(dc); |
| 432 | |
| 433 | if (searched_full_index) { |
| 434 | unsigned delay = dc->writeback_delay * HZ; |
| 435 | |
| 436 | while (delay && |
| 437 | !kthread_should_stop() && |
Kent Overstreet | c4d951d | 2013-08-21 17:49:09 -0700 | [diff] [blame] | 438 | !test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags)) |
Slava Pestov | 9e5c353 | 2014-05-01 13:48:57 -0700 | [diff] [blame] | 439 | delay = schedule_timeout_interruptible(delay); |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
| 443 | return 0; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Kent Overstreet | 444fc0b | 2013-05-11 17:07:26 -0700 | [diff] [blame] | 446 | /* Init */ |
| 447 | |
Kent Overstreet | c18536a | 2013-07-24 17:44:17 -0700 | [diff] [blame] | 448 | struct sectors_dirty_init { |
| 449 | struct btree_op op; |
| 450 | unsigned inode; |
| 451 | }; |
| 452 | |
| 453 | static int sectors_dirty_init_fn(struct btree_op *_op, struct btree *b, |
Kent Overstreet | 48dad8b | 2013-09-10 18:48:51 -0700 | [diff] [blame] | 454 | struct bkey *k) |
Kent Overstreet | 444fc0b | 2013-05-11 17:07:26 -0700 | [diff] [blame] | 455 | { |
Kent Overstreet | c18536a | 2013-07-24 17:44:17 -0700 | [diff] [blame] | 456 | struct sectors_dirty_init *op = container_of(_op, |
| 457 | struct sectors_dirty_init, op); |
Kent Overstreet | 48dad8b | 2013-09-10 18:48:51 -0700 | [diff] [blame] | 458 | if (KEY_INODE(k) > op->inode) |
| 459 | return MAP_DONE; |
Kent Overstreet | 444fc0b | 2013-05-11 17:07:26 -0700 | [diff] [blame] | 460 | |
Kent Overstreet | 48dad8b | 2013-09-10 18:48:51 -0700 | [diff] [blame] | 461 | if (KEY_DIRTY(k)) |
| 462 | bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k), |
| 463 | KEY_START(k), KEY_SIZE(k)); |
Kent Overstreet | 444fc0b | 2013-05-11 17:07:26 -0700 | [diff] [blame] | 464 | |
Kent Overstreet | 48dad8b | 2013-09-10 18:48:51 -0700 | [diff] [blame] | 465 | return MAP_CONTINUE; |
Kent Overstreet | 444fc0b | 2013-05-11 17:07:26 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | void bch_sectors_dirty_init(struct cached_dev *dc) |
| 469 | { |
Kent Overstreet | c18536a | 2013-07-24 17:44:17 -0700 | [diff] [blame] | 470 | struct sectors_dirty_init op; |
Kent Overstreet | 444fc0b | 2013-05-11 17:07:26 -0700 | [diff] [blame] | 471 | |
Kent Overstreet | b54d693 | 2013-07-24 18:04:18 -0700 | [diff] [blame] | 472 | bch_btree_op_init(&op.op, -1); |
Kent Overstreet | 48dad8b | 2013-09-10 18:48:51 -0700 | [diff] [blame] | 473 | op.inode = dc->disk.id; |
| 474 | |
Kent Overstreet | c18536a | 2013-07-24 17:44:17 -0700 | [diff] [blame] | 475 | bch_btree_map_keys(&op.op, dc->disk.c, &KEY(op.inode, 0, 0), |
Kent Overstreet | 48dad8b | 2013-09-10 18:48:51 -0700 | [diff] [blame] | 476 | sectors_dirty_init_fn, 0); |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 477 | |
| 478 | dc->disk.sectors_dirty_last = bcache_dev_sectors_dirty(&dc->disk); |
Kent Overstreet | 444fc0b | 2013-05-11 17:07:26 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Slava Pestov | 9e5c353 | 2014-05-01 13:48:57 -0700 | [diff] [blame] | 481 | void bch_cached_dev_writeback_init(struct cached_dev *dc) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 482 | { |
Kent Overstreet | c2a4f31 | 2013-09-23 23:17:31 -0700 | [diff] [blame] | 483 | sema_init(&dc->in_flight, 64); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 484 | init_rwsem(&dc->writeback_lock); |
Kent Overstreet | 72c2706 | 2013-06-05 06:24:39 -0700 | [diff] [blame] | 485 | bch_keybuf_init(&dc->writeback_keys); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 486 | |
| 487 | dc->writeback_metadata = true; |
| 488 | dc->writeback_running = true; |
| 489 | dc->writeback_percent = 10; |
| 490 | dc->writeback_delay = 30; |
| 491 | dc->writeback_rate.rate = 1024; |
| 492 | |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 493 | dc->writeback_rate_update_seconds = 5; |
| 494 | dc->writeback_rate_d_term = 30; |
| 495 | dc->writeback_rate_p_term_inverse = 6000; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 496 | |
Slava Pestov | 9e5c353 | 2014-05-01 13:48:57 -0700 | [diff] [blame] | 497 | INIT_DELAYED_WORK(&dc->writeback_rate_update, update_writeback_rate); |
| 498 | } |
| 499 | |
| 500 | int bch_cached_dev_writeback_start(struct cached_dev *dc) |
| 501 | { |
Kent Overstreet | 5e6926d | 2013-07-24 17:50:06 -0700 | [diff] [blame] | 502 | dc->writeback_thread = kthread_create(bch_writeback_thread, dc, |
| 503 | "bcache_writeback"); |
| 504 | if (IS_ERR(dc->writeback_thread)) |
| 505 | return PTR_ERR(dc->writeback_thread); |
| 506 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 507 | schedule_delayed_work(&dc->writeback_rate_update, |
| 508 | dc->writeback_rate_update_seconds * HZ); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 509 | |
Slava Pestov | 9e5c353 | 2014-05-01 13:48:57 -0700 | [diff] [blame] | 510 | bch_writeback_queue(dc); |
| 511 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 512 | return 0; |
| 513 | } |