blob: 151544740148618ef8b22407618e42af5855ea28 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Kent Overstreet279afba2013-06-05 06:21:07 -07002#ifndef _BCACHE_WRITEBACK_H
3#define _BCACHE_WRITEBACK_H
4
Kent Overstreet72c27062013-06-05 06:24:39 -07005#define CUTOFF_WRITEBACK 40
6#define CUTOFF_WRITEBACK_SYNC 70
7
Kent Overstreet279afba2013-06-05 06:21:07 -07008static inline uint64_t bcache_dev_sectors_dirty(struct bcache_device *d)
9{
10 uint64_t i, ret = 0;
11
12 for (i = 0; i < d->nr_stripes; i++)
13 ret += atomic_read(d->stripe_sectors_dirty + i);
14
15 return ret;
16}
17
Tang Junhuia8394092017-09-06 14:25:56 +080018static inline uint64_t bcache_flash_devs_sectors_dirty(struct cache_set *c)
19{
20 uint64_t i, ret = 0;
21
22 mutex_lock(&bch_register_lock);
23
24 for (i = 0; i < c->nr_uuids; i++) {
25 struct bcache_device *d = c->devices[i];
26
27 if (!d || !UUID_FLASH_ONLY(&c->uuids[i]))
28 continue;
29 ret += bcache_dev_sectors_dirty(d);
30 }
31
32 mutex_unlock(&bch_register_lock);
33
34 return ret;
35}
36
Kent Overstreet48a915a2013-10-31 15:43:22 -070037static inline unsigned offset_to_stripe(struct bcache_device *d,
38 uint64_t offset)
39{
40 do_div(offset, d->stripe_size);
41 return offset;
42}
43
44static inline bool bcache_dev_stripe_dirty(struct cached_dev *dc,
Kent Overstreet72c27062013-06-05 06:24:39 -070045 uint64_t offset,
46 unsigned nr_sectors)
47{
Kent Overstreet48a915a2013-10-31 15:43:22 -070048 unsigned stripe = offset_to_stripe(&dc->disk, offset);
Kent Overstreet72c27062013-06-05 06:24:39 -070049
50 while (1) {
Kent Overstreet48a915a2013-10-31 15:43:22 -070051 if (atomic_read(dc->disk.stripe_sectors_dirty + stripe))
Kent Overstreet72c27062013-06-05 06:24:39 -070052 return true;
53
Kent Overstreet48a915a2013-10-31 15:43:22 -070054 if (nr_sectors <= dc->disk.stripe_size)
Kent Overstreet72c27062013-06-05 06:24:39 -070055 return false;
56
Kent Overstreet48a915a2013-10-31 15:43:22 -070057 nr_sectors -= dc->disk.stripe_size;
Kent Overstreet72c27062013-06-05 06:24:39 -070058 stripe++;
59 }
60}
61
62static inline bool should_writeback(struct cached_dev *dc, struct bio *bio,
63 unsigned cache_mode, bool would_skip)
64{
65 unsigned in_use = dc->disk.c->gc_stats.in_use;
66
67 if (cache_mode != CACHE_MODE_WRITEBACK ||
Kent Overstreetc4d951d2013-08-21 17:49:09 -070068 test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) ||
Kent Overstreet72c27062013-06-05 06:24:39 -070069 in_use > CUTOFF_WRITEBACK_SYNC)
70 return false;
71
72 if (dc->partial_stripes_expensive &&
Kent Overstreet4f024f32013-10-11 15:44:27 -070073 bcache_dev_stripe_dirty(dc, bio->bi_iter.bi_sector,
Kent Overstreet72c27062013-06-05 06:24:39 -070074 bio_sectors(bio)))
75 return true;
76
77 if (would_skip)
78 return false;
79
Christoph Hellwig83b5df62016-11-01 07:40:05 -060080 return op_is_sync(bio->bi_opf) || in_use <= CUTOFF_WRITEBACK;
Kent Overstreet72c27062013-06-05 06:24:39 -070081}
82
Kent Overstreet5e6926da2013-07-24 17:50:06 -070083static inline void bch_writeback_queue(struct cached_dev *dc)
84{
Stefan Bader8d16ce52015-11-29 18:44:49 -080085 if (!IS_ERR_OR_NULL(dc->writeback_thread))
86 wake_up_process(dc->writeback_thread);
Kent Overstreet5e6926da2013-07-24 17:50:06 -070087}
88
89static inline void bch_writeback_add(struct cached_dev *dc)
90{
91 if (!atomic_read(&dc->has_dirty) &&
92 !atomic_xchg(&dc->has_dirty, 1)) {
93 atomic_inc(&dc->count);
94
95 if (BDEV_STATE(&dc->sb) != BDEV_STATE_DIRTY) {
96 SET_BDEV_STATE(&dc->sb, BDEV_STATE_DIRTY);
97 /* XXX: should do this synchronously */
98 bch_write_bdev_super(dc, NULL);
99 }
100
101 bch_writeback_queue(dc);
102 }
103}
104
Kent Overstreet279afba2013-06-05 06:21:07 -0700105void bcache_dev_sectors_dirty_add(struct cache_set *, unsigned, uint64_t, int);
Kent Overstreet279afba2013-06-05 06:21:07 -0700106
Tang Junhui175206c2017-09-07 01:28:53 +0800107void bch_sectors_dirty_init(struct bcache_device *);
Slava Pestov9e5c3532014-05-01 13:48:57 -0700108void bch_cached_dev_writeback_init(struct cached_dev *);
109int bch_cached_dev_writeback_start(struct cached_dev *);
Kent Overstreet279afba2013-06-05 06:21:07 -0700110
111#endif