blob: d81ee5ccc72668887ca1f70cabaa2a10c2f2de3d [file] [log] [blame]
Kent Overstreetcafe5632013-03-23 16:11:31 -07001/*
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 Overstreet279afba2013-06-05 06:21:07 -070012#include "writeback.h"
Kent Overstreetcafe5632013-03-23 16:11:31 -070013
Kent Overstreetc37511b2013-04-26 15:39:55 -070014#include <trace/events/bcache.h>
15
Kent Overstreetcafe5632013-03-23 16:11:31 -070016static struct workqueue_struct *dirty_wq;
17
18static void read_dirty(struct closure *);
19
20struct dirty_io {
21 struct closure cl;
22 struct cached_dev *dc;
23 struct bio bio;
24};
25
26/* Rate limiting */
27
28static void __update_writeback_rate(struct cached_dev *dc)
29{
30 struct cache_set *c = dc->disk.c;
31 uint64_t cache_sectors = c->nbuckets * c->sb.bucket_size;
32 uint64_t cache_dirty_target =
33 div_u64(cache_sectors * dc->writeback_percent, 100);
34
35 int64_t target = div64_u64(cache_dirty_target * bdev_sectors(dc->bdev),
36 c->cached_dev_sectors);
37
38 /* PD controller */
39
40 int change = 0;
41 int64_t error;
Kent Overstreet279afba2013-06-05 06:21:07 -070042 int64_t dirty = bcache_dev_sectors_dirty(&dc->disk);
Kent Overstreetcafe5632013-03-23 16:11:31 -070043 int64_t derivative = dirty - dc->disk.sectors_dirty_last;
44
45 dc->disk.sectors_dirty_last = dirty;
46
47 derivative *= dc->writeback_rate_d_term;
48 derivative = clamp(derivative, -dirty, dirty);
49
50 derivative = ewma_add(dc->disk.sectors_dirty_derivative, derivative,
51 dc->writeback_rate_d_smooth, 0);
52
53 /* Avoid divide by zero */
54 if (!target)
55 goto out;
56
57 error = div64_s64((dirty + derivative - target) << 8, target);
58
59 change = div_s64((dc->writeback_rate.rate * error) >> 8,
60 dc->writeback_rate_p_term_inverse);
61
62 /* Don't increase writeback rate if the device isn't keeping up */
63 if (change > 0 &&
64 time_after64(local_clock(),
65 dc->writeback_rate.next + 10 * NSEC_PER_MSEC))
66 change = 0;
67
68 dc->writeback_rate.rate =
69 clamp_t(int64_t, dc->writeback_rate.rate + change,
70 1, NSEC_PER_MSEC);
71out:
72 dc->writeback_rate_derivative = derivative;
73 dc->writeback_rate_change = change;
74 dc->writeback_rate_target = target;
75
76 schedule_delayed_work(&dc->writeback_rate_update,
77 dc->writeback_rate_update_seconds * HZ);
78}
79
80static void update_writeback_rate(struct work_struct *work)
81{
82 struct cached_dev *dc = container_of(to_delayed_work(work),
83 struct cached_dev,
84 writeback_rate_update);
85
86 down_read(&dc->writeback_lock);
87
88 if (atomic_read(&dc->has_dirty) &&
89 dc->writeback_percent)
90 __update_writeback_rate(dc);
91
92 up_read(&dc->writeback_lock);
93}
94
95static unsigned writeback_delay(struct cached_dev *dc, unsigned sectors)
96{
97 if (atomic_read(&dc->disk.detaching) ||
98 !dc->writeback_percent)
99 return 0;
100
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600101 return bch_next_delay(&dc->writeback_rate, sectors * 10000000ULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700102}
103
104/* Background writeback */
105
106static bool dirty_pred(struct keybuf *buf, struct bkey *k)
107{
108 return KEY_DIRTY(k);
109}
110
Kent Overstreet72c27062013-06-05 06:24:39 -0700111static bool dirty_full_stripe_pred(struct keybuf *buf, struct bkey *k)
112{
113 uint64_t stripe;
114 unsigned nr_sectors = KEY_SIZE(k);
115 struct cached_dev *dc = container_of(buf, struct cached_dev,
116 writeback_keys);
117 unsigned stripe_size = 1 << dc->disk.stripe_size_bits;
118
119 if (!KEY_DIRTY(k))
120 return false;
121
122 stripe = KEY_START(k) >> dc->disk.stripe_size_bits;
123 while (1) {
124 if (atomic_read(dc->disk.stripe_sectors_dirty + stripe) !=
125 stripe_size)
126 return false;
127
128 if (nr_sectors <= stripe_size)
129 return true;
130
131 nr_sectors -= stripe_size;
132 stripe++;
133 }
134}
135
Kent Overstreetcafe5632013-03-23 16:11:31 -0700136static void dirty_init(struct keybuf_key *w)
137{
138 struct dirty_io *io = w->private;
139 struct bio *bio = &io->bio;
140
141 bio_init(bio);
142 if (!io->dc->writeback_percent)
143 bio_set_prio(bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
144
145 bio->bi_size = KEY_SIZE(&w->key) << 9;
146 bio->bi_max_vecs = DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS);
147 bio->bi_private = w;
148 bio->bi_io_vec = bio->bi_inline_vecs;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600149 bch_bio_map(bio, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700150}
151
152static void refill_dirty(struct closure *cl)
153{
154 struct cached_dev *dc = container_of(cl, struct cached_dev,
155 writeback.cl);
156 struct keybuf *buf = &dc->writeback_keys;
157 bool searched_from_start = false;
158 struct bkey end = MAX_KEY;
159 SET_KEY_INODE(&end, dc->disk.id);
160
161 if (!atomic_read(&dc->disk.detaching) &&
162 !dc->writeback_running)
163 closure_return(cl);
164
165 down_write(&dc->writeback_lock);
166
167 if (!atomic_read(&dc->has_dirty)) {
168 SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
169 bch_write_bdev_super(dc, NULL);
170
171 up_write(&dc->writeback_lock);
172 closure_return(cl);
173 }
174
175 if (bkey_cmp(&buf->last_scanned, &end) >= 0) {
176 buf->last_scanned = KEY(dc->disk.id, 0, 0);
177 searched_from_start = true;
178 }
179
Kent Overstreet72c27062013-06-05 06:24:39 -0700180 if (dc->partial_stripes_expensive) {
181 uint64_t i;
182
183 for (i = 0; i < dc->disk.nr_stripes; i++)
184 if (atomic_read(dc->disk.stripe_sectors_dirty + i) ==
185 1 << dc->disk.stripe_size_bits)
186 goto full_stripes;
187
188 goto normal_refill;
189full_stripes:
190 bch_refill_keybuf(dc->disk.c, buf, &end,
191 dirty_full_stripe_pred);
192 } else {
193normal_refill:
194 bch_refill_keybuf(dc->disk.c, buf, &end, dirty_pred);
195 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700196
197 if (bkey_cmp(&buf->last_scanned, &end) >= 0 && searched_from_start) {
198 /* Searched the entire btree - delay awhile */
199
200 if (RB_EMPTY_ROOT(&buf->keys)) {
201 atomic_set(&dc->has_dirty, 0);
202 cached_dev_put(dc);
203 }
204
205 if (!atomic_read(&dc->disk.detaching))
206 closure_delay(&dc->writeback, dc->writeback_delay * HZ);
207 }
208
209 up_write(&dc->writeback_lock);
210
211 ratelimit_reset(&dc->writeback_rate);
212
213 /* Punt to workqueue only so we don't recurse and blow the stack */
214 continue_at(cl, read_dirty, dirty_wq);
215}
216
217void bch_writeback_queue(struct cached_dev *dc)
218{
219 if (closure_trylock(&dc->writeback.cl, &dc->disk.cl)) {
220 if (!atomic_read(&dc->disk.detaching))
221 closure_delay(&dc->writeback, dc->writeback_delay * HZ);
222
223 continue_at(&dc->writeback.cl, refill_dirty, dirty_wq);
224 }
225}
226
Kent Overstreet279afba2013-06-05 06:21:07 -0700227void bch_writeback_add(struct cached_dev *dc)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700228{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700229 if (!atomic_read(&dc->has_dirty) &&
230 !atomic_xchg(&dc->has_dirty, 1)) {
231 atomic_inc(&dc->count);
232
233 if (BDEV_STATE(&dc->sb) != BDEV_STATE_DIRTY) {
234 SET_BDEV_STATE(&dc->sb, BDEV_STATE_DIRTY);
235 /* XXX: should do this synchronously */
236 bch_write_bdev_super(dc, NULL);
237 }
238
239 bch_writeback_queue(dc);
240
241 if (dc->writeback_percent)
242 schedule_delayed_work(&dc->writeback_rate_update,
243 dc->writeback_rate_update_seconds * HZ);
244 }
245}
246
Kent Overstreet279afba2013-06-05 06:21:07 -0700247void bcache_dev_sectors_dirty_add(struct cache_set *c, unsigned inode,
248 uint64_t offset, int nr_sectors)
249{
250 struct bcache_device *d = c->devices[inode];
251 unsigned stripe_size, stripe_offset;
252 uint64_t stripe;
253
254 if (!d)
255 return;
256
257 stripe_size = 1 << d->stripe_size_bits;
258 stripe = offset >> d->stripe_size_bits;
259 stripe_offset = offset & (stripe_size - 1);
260
261 while (nr_sectors) {
262 int s = min_t(unsigned, abs(nr_sectors),
263 stripe_size - stripe_offset);
264
265 if (nr_sectors < 0)
266 s = -s;
267
268 atomic_add(s, d->stripe_sectors_dirty + stripe);
269 nr_sectors -= s;
270 stripe_offset = 0;
271 stripe++;
272 }
273}
274
Kent Overstreetcafe5632013-03-23 16:11:31 -0700275/* Background writeback - IO loop */
276
277static void dirty_io_destructor(struct closure *cl)
278{
279 struct dirty_io *io = container_of(cl, struct dirty_io, cl);
280 kfree(io);
281}
282
283static void write_dirty_finish(struct closure *cl)
284{
285 struct dirty_io *io = container_of(cl, struct dirty_io, cl);
286 struct keybuf_key *w = io->bio.bi_private;
287 struct cached_dev *dc = io->dc;
288 struct bio_vec *bv = bio_iovec_idx(&io->bio, io->bio.bi_vcnt);
289
290 while (bv-- != io->bio.bi_io_vec)
291 __free_page(bv->bv_page);
292
293 /* This is kind of a dumb way of signalling errors. */
294 if (KEY_DIRTY(&w->key)) {
295 unsigned i;
296 struct btree_op op;
297 bch_btree_op_init_stack(&op);
298
299 op.type = BTREE_REPLACE;
300 bkey_copy(&op.replace, &w->key);
301
302 SET_KEY_DIRTY(&w->key, false);
303 bch_keylist_add(&op.keys, &w->key);
304
305 for (i = 0; i < KEY_PTRS(&w->key); i++)
306 atomic_inc(&PTR_BUCKET(dc->disk.c, &w->key, i)->pin);
307
Kent Overstreetcafe5632013-03-23 16:11:31 -0700308 bch_btree_insert(&op, dc->disk.c);
309 closure_sync(&op.cl);
310
Kent Overstreetc37511b2013-04-26 15:39:55 -0700311 if (op.insert_collision)
312 trace_bcache_writeback_collision(&w->key);
313
Kent Overstreetcafe5632013-03-23 16:11:31 -0700314 atomic_long_inc(op.insert_collision
315 ? &dc->disk.c->writeback_keys_failed
316 : &dc->disk.c->writeback_keys_done);
317 }
318
319 bch_keybuf_del(&dc->writeback_keys, w);
320 atomic_dec_bug(&dc->in_flight);
321
322 closure_wake_up(&dc->writeback_wait);
323
324 closure_return_with_destructor(cl, dirty_io_destructor);
325}
326
327static void dirty_endio(struct bio *bio, int error)
328{
329 struct keybuf_key *w = bio->bi_private;
330 struct dirty_io *io = w->private;
331
332 if (error)
333 SET_KEY_DIRTY(&w->key, false);
334
335 closure_put(&io->cl);
336}
337
338static void write_dirty(struct closure *cl)
339{
340 struct dirty_io *io = container_of(cl, struct dirty_io, cl);
341 struct keybuf_key *w = io->bio.bi_private;
342
343 dirty_init(w);
344 io->bio.bi_rw = WRITE;
345 io->bio.bi_sector = KEY_START(&w->key);
346 io->bio.bi_bdev = io->dc->bdev;
347 io->bio.bi_end_io = dirty_endio;
348
Kent Overstreetcafe5632013-03-23 16:11:31 -0700349 closure_bio_submit(&io->bio, cl, &io->dc->disk);
350
351 continue_at(cl, write_dirty_finish, dirty_wq);
352}
353
354static void read_dirty_endio(struct bio *bio, int error)
355{
356 struct keybuf_key *w = bio->bi_private;
357 struct dirty_io *io = w->private;
358
359 bch_count_io_errors(PTR_CACHE(io->dc->disk.c, &w->key, 0),
360 error, "reading dirty data from cache");
361
362 dirty_endio(bio, error);
363}
364
365static void read_dirty_submit(struct closure *cl)
366{
367 struct dirty_io *io = container_of(cl, struct dirty_io, cl);
368
Kent Overstreetcafe5632013-03-23 16:11:31 -0700369 closure_bio_submit(&io->bio, cl, &io->dc->disk);
370
371 continue_at(cl, write_dirty, dirty_wq);
372}
373
374static void read_dirty(struct closure *cl)
375{
376 struct cached_dev *dc = container_of(cl, struct cached_dev,
377 writeback.cl);
378 unsigned delay = writeback_delay(dc, 0);
379 struct keybuf_key *w;
380 struct dirty_io *io;
381
382 /*
383 * XXX: if we error, background writeback just spins. Should use some
384 * mempools.
385 */
386
387 while (1) {
388 w = bch_keybuf_next(&dc->writeback_keys);
389 if (!w)
390 break;
391
392 BUG_ON(ptr_stale(dc->disk.c, &w->key, 0));
393
394 if (delay > 0 &&
395 (KEY_START(&w->key) != dc->last_read ||
396 jiffies_to_msecs(delay) > 50)) {
397 w->private = NULL;
398
399 closure_delay(&dc->writeback, delay);
400 continue_at(cl, read_dirty, dirty_wq);
401 }
402
403 dc->last_read = KEY_OFFSET(&w->key);
404
405 io = kzalloc(sizeof(struct dirty_io) + sizeof(struct bio_vec)
406 * DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS),
407 GFP_KERNEL);
408 if (!io)
409 goto err;
410
411 w->private = io;
412 io->dc = dc;
413
414 dirty_init(w);
415 io->bio.bi_sector = PTR_OFFSET(&w->key, 0);
416 io->bio.bi_bdev = PTR_CACHE(dc->disk.c,
417 &w->key, 0)->bdev;
418 io->bio.bi_rw = READ;
419 io->bio.bi_end_io = read_dirty_endio;
420
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600421 if (bch_bio_alloc_pages(&io->bio, GFP_KERNEL))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700422 goto err_free;
423
Kent Overstreetc37511b2013-04-26 15:39:55 -0700424 trace_bcache_writeback(&w->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700425
426 closure_call(&io->cl, read_dirty_submit, NULL, &dc->disk.cl);
427
428 delay = writeback_delay(dc, KEY_SIZE(&w->key));
429
430 atomic_inc(&dc->in_flight);
431
432 if (!closure_wait_event(&dc->writeback_wait, cl,
433 atomic_read(&dc->in_flight) < 64))
434 continue_at(cl, read_dirty, dirty_wq);
435 }
436
437 if (0) {
438err_free:
439 kfree(w->private);
440err:
441 bch_keybuf_del(&dc->writeback_keys, w);
442 }
443
444 refill_dirty(cl);
445}
446
Kent Overstreet444fc0b2013-05-11 17:07:26 -0700447/* Init */
448
449static int bch_btree_sectors_dirty_init(struct btree *b, struct btree_op *op,
450 struct cached_dev *dc)
451{
452 struct bkey *k;
453 struct btree_iter iter;
454
455 bch_btree_iter_init(b, &iter, &KEY(dc->disk.id, 0, 0));
456 while ((k = bch_btree_iter_next_filter(&iter, b, bch_ptr_bad)))
457 if (!b->level) {
458 if (KEY_INODE(k) > dc->disk.id)
459 break;
460
461 if (KEY_DIRTY(k))
Kent Overstreet279afba2013-06-05 06:21:07 -0700462 bcache_dev_sectors_dirty_add(b->c, dc->disk.id,
463 KEY_START(k),
464 KEY_SIZE(k));
Kent Overstreet444fc0b2013-05-11 17:07:26 -0700465 } else {
466 btree(sectors_dirty_init, k, b, op, dc);
467 if (KEY_INODE(k) > dc->disk.id)
468 break;
469
470 cond_resched();
471 }
472
473 return 0;
474}
475
476void bch_sectors_dirty_init(struct cached_dev *dc)
477{
478 struct btree_op op;
479
480 bch_btree_op_init_stack(&op);
481 btree_root(sectors_dirty_init, dc->disk.c, &op, dc);
482}
483
Kent Overstreetf59fce82013-05-15 00:11:26 -0700484void bch_cached_dev_writeback_init(struct cached_dev *dc)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700485{
486 closure_init_unlocked(&dc->writeback);
487 init_rwsem(&dc->writeback_lock);
488
Kent Overstreet72c27062013-06-05 06:24:39 -0700489 bch_keybuf_init(&dc->writeback_keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700490
491 dc->writeback_metadata = true;
492 dc->writeback_running = true;
493 dc->writeback_percent = 10;
494 dc->writeback_delay = 30;
495 dc->writeback_rate.rate = 1024;
496
497 dc->writeback_rate_update_seconds = 30;
498 dc->writeback_rate_d_term = 16;
499 dc->writeback_rate_p_term_inverse = 64;
500 dc->writeback_rate_d_smooth = 8;
501
502 INIT_DELAYED_WORK(&dc->writeback_rate_update, update_writeback_rate);
503 schedule_delayed_work(&dc->writeback_rate_update,
504 dc->writeback_rate_update_seconds * HZ);
505}
506
507void bch_writeback_exit(void)
508{
509 if (dirty_wq)
510 destroy_workqueue(dirty_wq);
511}
512
513int __init bch_writeback_init(void)
514{
515 dirty_wq = create_singlethread_workqueue("bcache_writeback");
516 if (!dirty_wq)
517 return -ENOMEM;
518
519 return 0;
520}