1 #ifndef _BCACHE_WRITEBACK_H
2 #define _BCACHE_WRITEBACK_H
7 #define CUTOFF_WRITEBACK 60
8 #define CUTOFF_WRITEBACK_SYNC 30
10 static inline uint64_t bcache_dev_sectors_dirty(struct bcache_device *d)
14 for (i = 0; i < d->nr_stripes; i++)
15 ret += atomic_read(d->stripe_sectors_dirty + i);
20 static inline unsigned offset_to_stripe(struct bcache_device *d,
23 do_div(offset, d->stripe_size);
27 static inline bool bcache_dev_stripe_dirty(struct cached_dev *dc,
31 unsigned stripe = offset_to_stripe(&dc->disk, offset);
34 if (atomic_read(dc->disk.stripe_sectors_dirty + stripe))
37 if (nr_sectors <= dc->disk.stripe_size)
40 nr_sectors -= dc->disk.stripe_size;
45 static inline bool should_writeback(struct cached_dev *dc, struct bio *bio,
46 unsigned cache_mode, bool would_skip)
48 struct cache_set *c = dc->disk.c;
49 u64 available = sectors_available(c);
51 if (cache_mode != CACHE_MODE_WRITEBACK ||
52 test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) ||
53 available * 100 < c->capacity * CUTOFF_WRITEBACK_SYNC)
56 if (dc->partial_stripes_expensive &&
57 bcache_dev_stripe_dirty(dc, bio->bi_iter.bi_sector,
64 return bio->bi_opf & REQ_SYNC ||
65 available * 100 < c->capacity * CUTOFF_WRITEBACK;
68 static inline void bch_writeback_queue(struct cached_dev *dc)
70 if (!IS_ERR_OR_NULL(dc->writeback_thread))
71 wake_up_process(dc->writeback_thread);
74 static inline void bch_writeback_add(struct cached_dev *dc)
76 if (!atomic_read(&dc->has_dirty) &&
77 !atomic_xchg(&dc->has_dirty, 1)) {
78 atomic_inc(&dc->count);
80 if (BDEV_STATE(dc->disk_sb.sb) != BDEV_STATE_DIRTY) {
81 SET_BDEV_STATE(dc->disk_sb.sb, BDEV_STATE_DIRTY);
82 /* XXX: should do this synchronously */
83 bch_write_bdev_super(dc, NULL);
86 bch_writeback_queue(dc);
90 #ifndef NO_BCACHE_WRITEBACK
92 void bcache_dev_sectors_dirty_add(struct cache_set *, unsigned, u64, int);
94 void bch_writeback_recalc_oldest_gens(struct cache_set *);
95 void bch_sectors_dirty_init(struct cached_dev *, struct cache_set *c);
97 void bch_cached_dev_writeback_stop(struct cached_dev *);
98 void bch_cached_dev_writeback_free(struct cached_dev *);
99 int bch_cached_dev_writeback_init(struct cached_dev *);
100 int bch_cached_dev_writeback_start(struct cached_dev *);
104 static inline void bcache_dev_sectors_dirty_add(struct cache_set *c,
105 unsigned i, u64 o, int n) {}
106 static inline void bch_writeback_recalc_oldest_gens(struct cache_set *c) {}
107 static inline void bch_sectors_dirty_init(struct cached_dev *dc,
108 struct cache_set *c) {}
109 static inline void bch_cached_dev_writeback_stop(struct cached_dev *dc) {}
110 static inline void bch_cached_dev_writeback_free(struct cached_dev *dc) {}
111 static inline int bch_cached_dev_writeback_init(struct cached_dev *dc)
115 static inline int bch_cached_dev_writeback_start(struct cached_dev *dc)