blob: bb7aa31c2a081c94ab0317698c4899ba51e06e30 [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 Overstreet5e6926d2013-07-24 17:50:06 -070014#include <linux/delay.h>
Kent Overstreet5e6926d2013-07-24 17:50:06 -070015#include <linux/kthread.h>
Kent Overstreetc37511b2013-04-26 15:39:55 -070016#include <trace/events/bcache.h>
17
Kent Overstreetcafe5632013-03-23 16:11:31 -070018/* Rate limiting */
19
20static void __update_writeback_rate(struct cached_dev *dc)
21{
22 struct cache_set *c = dc->disk.c;
Tang Junhuie40cb302017-09-06 14:25:56 +080023 uint64_t cache_sectors = c->nbuckets * c->sb.bucket_size -
24 bcache_flash_devs_sectors_dirty(c);
Kent Overstreetcafe5632013-03-23 16:11:31 -070025 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 Overstreet279afba2013-06-05 06:21:07 -070033 int64_t dirty = bcache_dev_sectors_dirty(&dc->disk);
Kent Overstreetcafe5632013-03-23 16:11:31 -070034 int64_t derivative = dirty - dc->disk.sectors_dirty_last;
Kent Overstreet16749c22013-11-11 13:58:34 -080035 int64_t proportional = dirty - target;
36 int64_t change;
Kent Overstreetcafe5632013-03-23 16:11:31 -070037
38 dc->disk.sectors_dirty_last = dirty;
39
Kent Overstreet16749c22013-11-11 13:58:34 -080040 /* 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 Overstreetcafe5632013-03-23 16:11:31 -070046
47 derivative = ewma_add(dc->disk.sectors_dirty_derivative, derivative,
Kent Overstreet16749c22013-11-11 13:58:34 -080048 (dc->writeback_rate_d_term /
49 dc->writeback_rate_update_seconds) ?: 1, 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -070050
Kent Overstreet16749c22013-11-11 13:58:34 -080051 derivative *= dc->writeback_rate_d_term;
52 derivative = div_s64(derivative, dc->writeback_rate_p_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -070053
Kent Overstreet16749c22013-11-11 13:58:34 -080054 change = proportional + derivative;
Kent Overstreetcafe5632013-03-23 16:11:31 -070055
56 /* Don't increase writeback rate if the device isn't keeping up */
57 if (change > 0 &&
58 time_after64(local_clock(),
Kent Overstreet16749c22013-11-11 13:58:34 -080059 dc->writeback_rate.next + NSEC_PER_MSEC))
Kent Overstreetcafe5632013-03-23 16:11:31 -070060 change = 0;
61
62 dc->writeback_rate.rate =
Kent Overstreet16749c22013-11-11 13:58:34 -080063 clamp_t(int64_t, (int64_t) dc->writeback_rate.rate + change,
Kent Overstreetcafe5632013-03-23 16:11:31 -070064 1, NSEC_PER_MSEC);
Kent Overstreet16749c22013-11-11 13:58:34 -080065
66 dc->writeback_rate_proportional = proportional;
Kent Overstreetcafe5632013-03-23 16:11:31 -070067 dc->writeback_rate_derivative = derivative;
68 dc->writeback_rate_change = change;
69 dc->writeback_rate_target = target;
Kent Overstreetcafe5632013-03-23 16:11:31 -070070}
71
72static 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 Overstreet5e6926d2013-07-24 17:50:06 -070085
86 schedule_delayed_work(&dc->writeback_rate_update,
87 dc->writeback_rate_update_seconds * HZ);
Kent Overstreetcafe5632013-03-23 16:11:31 -070088}
89
90static unsigned writeback_delay(struct cached_dev *dc, unsigned sectors)
91{
Kent Overstreetc4d951d2013-08-21 17:49:09 -070092 if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) ||
Kent Overstreetcafe5632013-03-23 16:11:31 -070093 !dc->writeback_percent)
94 return 0;
95
Kent Overstreet16749c22013-11-11 13:58:34 -080096 return bch_next_delay(&dc->writeback_rate, sectors);
Kent Overstreetcafe5632013-03-23 16:11:31 -070097}
98
Kent Overstreet5e6926d2013-07-24 17:50:06 -070099struct dirty_io {
100 struct closure cl;
101 struct cached_dev *dc;
102 struct bio bio;
103};
Kent Overstreet72c27062013-06-05 06:24:39 -0700104
Kent Overstreetcafe5632013-03-23 16:11:31 -0700105static 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 Overstreet4f024f32013-10-11 15:44:27 -0700114 bio->bi_iter.bi_size = KEY_SIZE(&w->key) << 9;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700115 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 Overstreet169ef1c2013-03-28 12:50:55 -0600118 bch_bio_map(bio, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700119}
120
Kent Overstreetcafe5632013-03-23 16:11:31 -0700121static 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
127static 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 Overstreetcafe5632013-03-23 16:11:31 -0700132
Guoqing Jiang491221f2016-09-22 03:10:01 -0400133 bio_free_pages(&io->bio);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700134
135 /* This is kind of a dumb way of signalling errors. */
136 if (KEY_DIRTY(&w->key)) {
Kent Overstreet6054c6d2013-07-24 18:06:22 -0700137 int ret;
Kent Overstreetcc7b8812013-07-24 18:07:22 -0700138 unsigned i;
139 struct keylist keys;
Kent Overstreet0b932072013-07-24 17:26:51 -0700140
Kent Overstreet0b932072013-07-24 17:26:51 -0700141 bch_keylist_init(&keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700142
Kent Overstreet1b207d82013-09-10 18:52:54 -0700143 bkey_copy(keys.top, &w->key);
144 SET_KEY_DIRTY(keys.top, false);
145 bch_keylist_push(&keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700146
147 for (i = 0; i < KEY_PTRS(&w->key); i++)
148 atomic_inc(&PTR_BUCKET(dc->disk.c, &w->key, i)->pin);
149
Kent Overstreetcc7b8812013-07-24 18:07:22 -0700150 ret = bch_btree_insert(dc->disk.c, &keys, NULL, &w->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700151
Kent Overstreet6054c6d2013-07-24 18:06:22 -0700152 if (ret)
Kent Overstreetc37511b2013-04-26 15:39:55 -0700153 trace_bcache_writeback_collision(&w->key);
154
Kent Overstreet6054c6d2013-07-24 18:06:22 -0700155 atomic_long_inc(ret
Kent Overstreetcafe5632013-03-23 16:11:31 -0700156 ? &dc->disk.c->writeback_keys_failed
157 : &dc->disk.c->writeback_keys_done);
158 }
159
160 bch_keybuf_del(&dc->writeback_keys, w);
Kent Overstreetc2a4f312013-09-23 23:17:31 -0700161 up(&dc->in_flight);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700162
163 closure_return_with_destructor(cl, dirty_io_destructor);
164}
165
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200166static void dirty_endio(struct bio *bio)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700167{
168 struct keybuf_key *w = bio->bi_private;
169 struct dirty_io *io = w->private;
170
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200171 if (bio->bi_error)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700172 SET_KEY_DIRTY(&w->key, false);
173
174 closure_put(&io->cl);
175}
176
177static void write_dirty(struct closure *cl)
178{
179 struct dirty_io *io = container_of(cl, struct dirty_io, cl);
180 struct keybuf_key *w = io->bio.bi_private;
181
182 dirty_init(w);
Mike Christiead0d9e72016-06-05 14:32:05 -0500183 bio_set_op_attrs(&io->bio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700184 io->bio.bi_iter.bi_sector = KEY_START(&w->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700185 io->bio.bi_bdev = io->dc->bdev;
186 io->bio.bi_end_io = dirty_endio;
187
Kent Overstreet749b61d2013-11-23 23:11:25 -0800188 closure_bio_submit(&io->bio, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700189
Tang Junhui57aa1a692017-09-06 14:25:59 +0800190 continue_at(cl, write_dirty_finish, io->dc->writeback_write_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700191}
192
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200193static void read_dirty_endio(struct bio *bio)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700194{
195 struct keybuf_key *w = bio->bi_private;
196 struct dirty_io *io = w->private;
197
198 bch_count_io_errors(PTR_CACHE(io->dc->disk.c, &w->key, 0),
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200199 bio->bi_error, "reading dirty data from cache");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700200
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200201 dirty_endio(bio);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700202}
203
204static void read_dirty_submit(struct closure *cl)
205{
206 struct dirty_io *io = container_of(cl, struct dirty_io, cl);
207
Kent Overstreet749b61d2013-11-23 23:11:25 -0800208 closure_bio_submit(&io->bio, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700209
Tang Junhui57aa1a692017-09-06 14:25:59 +0800210 continue_at(cl, write_dirty, io->dc->writeback_write_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700211}
212
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700213static void read_dirty(struct cached_dev *dc)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700214{
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700215 unsigned delay = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700216 struct keybuf_key *w;
217 struct dirty_io *io;
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700218 struct closure cl;
219
220 closure_init_stack(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700221
222 /*
223 * XXX: if we error, background writeback just spins. Should use some
224 * mempools.
225 */
226
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700227 while (!kthread_should_stop()) {
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700228
Kent Overstreetcafe5632013-03-23 16:11:31 -0700229 w = bch_keybuf_next(&dc->writeback_keys);
230 if (!w)
231 break;
232
233 BUG_ON(ptr_stale(dc->disk.c, &w->key, 0));
234
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700235 if (KEY_START(&w->key) != dc->last_read ||
236 jiffies_to_msecs(delay) > 50)
237 while (!kthread_should_stop() && delay)
Slava Pestov9e5c3532014-05-01 13:48:57 -0700238 delay = schedule_timeout_interruptible(delay);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700239
240 dc->last_read = KEY_OFFSET(&w->key);
241
242 io = kzalloc(sizeof(struct dirty_io) + sizeof(struct bio_vec)
243 * DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS),
244 GFP_KERNEL);
245 if (!io)
246 goto err;
247
248 w->private = io;
249 io->dc = dc;
250
251 dirty_init(w);
Mike Christiead0d9e72016-06-05 14:32:05 -0500252 bio_set_op_attrs(&io->bio, REQ_OP_READ, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700253 io->bio.bi_iter.bi_sector = PTR_OFFSET(&w->key, 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700254 io->bio.bi_bdev = PTR_CACHE(dc->disk.c,
255 &w->key, 0)->bdev;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700256 io->bio.bi_end_io = read_dirty_endio;
257
Kent Overstreet8e51e412013-06-06 18:15:57 -0700258 if (bio_alloc_pages(&io->bio, GFP_KERNEL))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700259 goto err_free;
260
Kent Overstreetc37511b2013-04-26 15:39:55 -0700261 trace_bcache_writeback(&w->key);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700262
Kent Overstreetc2a4f312013-09-23 23:17:31 -0700263 down(&dc->in_flight);
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700264 closure_call(&io->cl, read_dirty_submit, NULL, &cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700265
266 delay = writeback_delay(dc, KEY_SIZE(&w->key));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700267 }
268
269 if (0) {
270err_free:
271 kfree(w->private);
272err:
273 bch_keybuf_del(&dc->writeback_keys, w);
274 }
275
Kent Overstreetc2a4f312013-09-23 23:17:31 -0700276 /*
277 * Wait for outstanding writeback IOs to finish (and keybuf slots to be
278 * freed) before refilling again
279 */
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700280 closure_sync(&cl);
281}
282
283/* Scan for dirty data */
284
285void bcache_dev_sectors_dirty_add(struct cache_set *c, unsigned inode,
286 uint64_t offset, int nr_sectors)
287{
288 struct bcache_device *d = c->devices[inode];
Kent Overstreet48a915a2013-10-31 15:43:22 -0700289 unsigned stripe_offset, stripe, sectors_dirty;
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700290
291 if (!d)
292 return;
293
Kent Overstreet48a915a2013-10-31 15:43:22 -0700294 stripe = offset_to_stripe(d, offset);
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700295 stripe_offset = offset & (d->stripe_size - 1);
296
297 while (nr_sectors) {
298 int s = min_t(unsigned, abs(nr_sectors),
299 d->stripe_size - stripe_offset);
300
301 if (nr_sectors < 0)
302 s = -s;
303
Kent Overstreet48a915a2013-10-31 15:43:22 -0700304 if (stripe >= d->nr_stripes)
305 return;
306
307 sectors_dirty = atomic_add_return(s,
308 d->stripe_sectors_dirty + stripe);
309 if (sectors_dirty == d->stripe_size)
310 set_bit(stripe, d->full_dirty_stripes);
311 else
312 clear_bit(stripe, d->full_dirty_stripes);
313
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700314 nr_sectors -= s;
315 stripe_offset = 0;
316 stripe++;
317 }
318}
319
320static bool dirty_pred(struct keybuf *buf, struct bkey *k)
321{
Kent Overstreet627ccd22015-11-29 18:47:01 -0800322 struct cached_dev *dc = container_of(buf, struct cached_dev, writeback_keys);
323
324 BUG_ON(KEY_INODE(k) != dc->disk.id);
325
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700326 return KEY_DIRTY(k);
327}
328
Kent Overstreet48a915a2013-10-31 15:43:22 -0700329static void refill_full_stripes(struct cached_dev *dc)
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700330{
Kent Overstreet48a915a2013-10-31 15:43:22 -0700331 struct keybuf *buf = &dc->writeback_keys;
332 unsigned start_stripe, stripe, next_stripe;
333 bool wrapped = false;
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700334
Kent Overstreet48a915a2013-10-31 15:43:22 -0700335 stripe = offset_to_stripe(&dc->disk, KEY_OFFSET(&buf->last_scanned));
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700336
Kent Overstreet48a915a2013-10-31 15:43:22 -0700337 if (stripe >= dc->disk.nr_stripes)
338 stripe = 0;
339
340 start_stripe = stripe;
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700341
342 while (1) {
Kent Overstreet48a915a2013-10-31 15:43:22 -0700343 stripe = find_next_bit(dc->disk.full_dirty_stripes,
344 dc->disk.nr_stripes, stripe);
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700345
Kent Overstreet48a915a2013-10-31 15:43:22 -0700346 if (stripe == dc->disk.nr_stripes)
347 goto next;
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700348
Kent Overstreet48a915a2013-10-31 15:43:22 -0700349 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;
364next:
365 if (wrapped && stripe > start_stripe)
366 return;
367
368 if (stripe == dc->disk.nr_stripes) {
369 stripe = 0;
370 wrapped = true;
371 }
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700372 }
373}
374
Kent Overstreet627ccd22015-11-29 18:47:01 -0800375/*
376 * Returns true if we scanned the entire disk
377 */
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700378static bool refill_dirty(struct cached_dev *dc)
379{
380 struct keybuf *buf = &dc->writeback_keys;
Kent Overstreet627ccd22015-11-29 18:47:01 -0800381 struct bkey start = KEY(dc->disk.id, 0, 0);
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700382 struct bkey end = KEY(dc->disk.id, MAX_KEY_OFFSET, 0);
Kent Overstreet627ccd22015-11-29 18:47:01 -0800383 struct bkey start_pos;
384
385 /*
386 * make sure keybuf pos is inside the range for this disk - at bringup
387 * we might not be attached yet so this disk's inode nr isn't
388 * initialized then
389 */
390 if (bkey_cmp(&buf->last_scanned, &start) < 0 ||
391 bkey_cmp(&buf->last_scanned, &end) > 0)
392 buf->last_scanned = start;
Kent Overstreet48a915a2013-10-31 15:43:22 -0700393
394 if (dc->partial_stripes_expensive) {
395 refill_full_stripes(dc);
396 if (array_freelist_empty(&buf->freelist))
397 return false;
398 }
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700399
Kent Overstreet627ccd22015-11-29 18:47:01 -0800400 start_pos = buf->last_scanned;
Kent Overstreet48a915a2013-10-31 15:43:22 -0700401 bch_refill_keybuf(dc->disk.c, buf, &end, dirty_pred);
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700402
Kent Overstreet627ccd22015-11-29 18:47:01 -0800403 if (bkey_cmp(&buf->last_scanned, &end) < 0)
404 return false;
405
406 /*
407 * If we get to the end start scanning again from the beginning, and
408 * only scan up to where we initially started scanning from:
409 */
410 buf->last_scanned = start;
411 bch_refill_keybuf(dc->disk.c, buf, &start_pos, dirty_pred);
412
413 return bkey_cmp(&buf->last_scanned, &start_pos) >= 0;
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700414}
415
416static int bch_writeback_thread(void *arg)
417{
418 struct cached_dev *dc = arg;
419 bool searched_full_index;
420
421 while (!kthread_should_stop()) {
422 down_write(&dc->writeback_lock);
Coly Liee6fcd82018-02-07 11:41:41 -0800423 set_current_state(TASK_INTERRUPTIBLE);
Coly Life451382018-03-18 17:36:15 -0700424 /*
425 * If the bache device is detaching, skip here and continue
426 * to perform writeback. Otherwise, if no dirty data on cache,
427 * or there is dirty data on cache but writeback is disabled,
428 * the writeback thread should sleep here and wait for others
429 * to wake up it.
430 */
431 if (!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) &&
432 (!atomic_read(&dc->has_dirty) || !dc->writeback_running)) {
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700433 up_write(&dc->writeback_lock);
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700434
Coly Liee6fcd82018-02-07 11:41:41 -0800435 if (kthread_should_stop()) {
436 set_current_state(TASK_RUNNING);
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700437 return 0;
Coly Liee6fcd82018-02-07 11:41:41 -0800438 }
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700439
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700440 schedule();
441 continue;
442 }
Coly Liee6fcd82018-02-07 11:41:41 -0800443 set_current_state(TASK_RUNNING);
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700444
445 searched_full_index = refill_dirty(dc);
446
447 if (searched_full_index &&
448 RB_EMPTY_ROOT(&dc->writeback_keys.keys)) {
449 atomic_set(&dc->has_dirty, 0);
450 cached_dev_put(dc);
451 SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
452 bch_write_bdev_super(dc, NULL);
Coly Life451382018-03-18 17:36:15 -0700453 /*
454 * If bcache device is detaching via sysfs interface,
455 * writeback thread should stop after there is no dirty
456 * data on cache. BCACHE_DEV_DETACHING flag is set in
457 * bch_cached_dev_detach().
458 */
459 if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags))
460 break;
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700461 }
462
463 up_write(&dc->writeback_lock);
464
465 bch_ratelimit_reset(&dc->writeback_rate);
466 read_dirty(dc);
467
468 if (searched_full_index) {
469 unsigned delay = dc->writeback_delay * HZ;
470
471 while (delay &&
472 !kthread_should_stop() &&
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700473 !test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags))
Slava Pestov9e5c3532014-05-01 13:48:57 -0700474 delay = schedule_timeout_interruptible(delay);
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700475 }
476 }
477
478 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700479}
480
Kent Overstreet444fc0b2013-05-11 17:07:26 -0700481/* Init */
482
Kent Overstreetc18536a2013-07-24 17:44:17 -0700483struct sectors_dirty_init {
484 struct btree_op op;
485 unsigned inode;
486};
487
488static int sectors_dirty_init_fn(struct btree_op *_op, struct btree *b,
Kent Overstreet48dad8b2013-09-10 18:48:51 -0700489 struct bkey *k)
Kent Overstreet444fc0b2013-05-11 17:07:26 -0700490{
Kent Overstreetc18536a2013-07-24 17:44:17 -0700491 struct sectors_dirty_init *op = container_of(_op,
492 struct sectors_dirty_init, op);
Kent Overstreet48dad8b2013-09-10 18:48:51 -0700493 if (KEY_INODE(k) > op->inode)
494 return MAP_DONE;
Kent Overstreet444fc0b2013-05-11 17:07:26 -0700495
Kent Overstreet48dad8b2013-09-10 18:48:51 -0700496 if (KEY_DIRTY(k))
497 bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k),
498 KEY_START(k), KEY_SIZE(k));
Kent Overstreet444fc0b2013-05-11 17:07:26 -0700499
Kent Overstreet48dad8b2013-09-10 18:48:51 -0700500 return MAP_CONTINUE;
Kent Overstreet444fc0b2013-05-11 17:07:26 -0700501}
502
Tang Junhui2a9b5572017-09-07 01:28:53 +0800503void bch_sectors_dirty_init(struct bcache_device *d)
Kent Overstreet444fc0b2013-05-11 17:07:26 -0700504{
Kent Overstreetc18536a2013-07-24 17:44:17 -0700505 struct sectors_dirty_init op;
Kent Overstreet444fc0b2013-05-11 17:07:26 -0700506
Kent Overstreetb54d6932013-07-24 18:04:18 -0700507 bch_btree_op_init(&op.op, -1);
Tang Junhui2a9b5572017-09-07 01:28:53 +0800508 op.inode = d->id;
Kent Overstreet48dad8b2013-09-10 18:48:51 -0700509
Tang Junhui2a9b5572017-09-07 01:28:53 +0800510 bch_btree_map_keys(&op.op, d->c, &KEY(op.inode, 0, 0),
Kent Overstreet48dad8b2013-09-10 18:48:51 -0700511 sectors_dirty_init_fn, 0);
Kent Overstreet16749c22013-11-11 13:58:34 -0800512
Tang Junhui2a9b5572017-09-07 01:28:53 +0800513 d->sectors_dirty_last = bcache_dev_sectors_dirty(d);
Kent Overstreet444fc0b2013-05-11 17:07:26 -0700514}
515
Slava Pestov9e5c3532014-05-01 13:48:57 -0700516void bch_cached_dev_writeback_init(struct cached_dev *dc)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700517{
Kent Overstreetc2a4f312013-09-23 23:17:31 -0700518 sema_init(&dc->in_flight, 64);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700519 init_rwsem(&dc->writeback_lock);
Kent Overstreet72c27062013-06-05 06:24:39 -0700520 bch_keybuf_init(&dc->writeback_keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700521
522 dc->writeback_metadata = true;
523 dc->writeback_running = true;
524 dc->writeback_percent = 10;
525 dc->writeback_delay = 30;
526 dc->writeback_rate.rate = 1024;
527
Kent Overstreet16749c22013-11-11 13:58:34 -0800528 dc->writeback_rate_update_seconds = 5;
529 dc->writeback_rate_d_term = 30;
530 dc->writeback_rate_p_term_inverse = 6000;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700531
Slava Pestov9e5c3532014-05-01 13:48:57 -0700532 INIT_DELAYED_WORK(&dc->writeback_rate_update, update_writeback_rate);
533}
534
535int bch_cached_dev_writeback_start(struct cached_dev *dc)
536{
Tang Junhui57aa1a692017-09-06 14:25:59 +0800537 dc->writeback_write_wq = alloc_workqueue("bcache_writeback_wq",
538 WQ_MEM_RECLAIM, 0);
539 if (!dc->writeback_write_wq)
540 return -ENOMEM;
541
Kent Overstreet5e6926d2013-07-24 17:50:06 -0700542 dc->writeback_thread = kthread_create(bch_writeback_thread, dc,
543 "bcache_writeback");
544 if (IS_ERR(dc->writeback_thread))
545 return PTR_ERR(dc->writeback_thread);
546
Kent Overstreetcafe5632013-03-23 16:11:31 -0700547 schedule_delayed_work(&dc->writeback_rate_update,
548 dc->writeback_rate_update_seconds * HZ);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700549
Slava Pestov9e5c3532014-05-01 13:48:57 -0700550 bch_writeback_queue(dc);
551
Kent Overstreetcafe5632013-03-23 16:11:31 -0700552 return 0;
553}