1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _BCACHE_WRITEBACK_H 3 #define _BCACHE_WRITEBACK_H 4 5 #define CUTOFF_WRITEBACK 40 6 #define CUTOFF_WRITEBACK_SYNC 70 7 8 #define CUTOFF_WRITEBACK_MAX 70 9 #define CUTOFF_WRITEBACK_SYNC_MAX 90 10 11 #define MAX_WRITEBACKS_IN_PASS 5 12 #define MAX_WRITESIZE_IN_PASS 5000 /* *512b */ 13 14 #define WRITEBACK_RATE_UPDATE_SECS_MAX 60 15 #define WRITEBACK_RATE_UPDATE_SECS_DEFAULT 5 16 17 #define BCH_AUTO_GC_DIRTY_THRESHOLD 50 18 19 #define BCH_DIRTY_INIT_THRD_MAX 64 20 /* 21 * 14 (16384ths) is chosen here as something that each backing device 22 * should be a reasonable fraction of the share, and not to blow up 23 * until individual backing devices are a petabyte. 24 */ 25 #define WRITEBACK_SHARE_SHIFT 14 26 27 struct bch_dirty_init_state; 28 struct dirty_init_thrd_info { 29 struct bch_dirty_init_state *state; 30 struct task_struct *thread; 31 }; 32 33 struct bch_dirty_init_state { 34 struct cache_set *c; 35 struct bcache_device *d; 36 int total_threads; 37 int key_idx; 38 spinlock_t idx_lock; 39 atomic_t started; 40 atomic_t enough; 41 wait_queue_head_t wait; 42 struct dirty_init_thrd_info infos[BCH_DIRTY_INIT_THRD_MAX]; 43 }; 44 45 static inline uint64_t bcache_dev_sectors_dirty(struct bcache_device *d) 46 { 47 uint64_t i, ret = 0; 48 49 for (i = 0; i < d->nr_stripes; i++) 50 ret += atomic_read(d->stripe_sectors_dirty + i); 51 52 return ret; 53 } 54 55 static inline unsigned int offset_to_stripe(struct bcache_device *d, 56 uint64_t offset) 57 { 58 do_div(offset, d->stripe_size); 59 return offset; 60 } 61 62 static inline bool bcache_dev_stripe_dirty(struct cached_dev *dc, 63 uint64_t offset, 64 unsigned int nr_sectors) 65 { 66 unsigned int stripe = offset_to_stripe(&dc->disk, offset); 67 68 while (1) { 69 if (atomic_read(dc->disk.stripe_sectors_dirty + stripe)) 70 return true; 71 72 if (nr_sectors <= dc->disk.stripe_size) 73 return false; 74 75 nr_sectors -= dc->disk.stripe_size; 76 stripe++; 77 } 78 } 79 80 extern unsigned int bch_cutoff_writeback; 81 extern unsigned int bch_cutoff_writeback_sync; 82 83 static inline bool should_writeback(struct cached_dev *dc, struct bio *bio, 84 unsigned int cache_mode, bool would_skip) 85 { 86 unsigned int in_use = dc->disk.c->gc_stats.in_use; 87 88 if (cache_mode != CACHE_MODE_WRITEBACK || 89 test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) || 90 in_use > bch_cutoff_writeback_sync) 91 return false; 92 93 if (bio_op(bio) == REQ_OP_DISCARD) 94 return false; 95 96 if (dc->partial_stripes_expensive && 97 bcache_dev_stripe_dirty(dc, bio->bi_iter.bi_sector, 98 bio_sectors(bio))) 99 return true; 100 101 if (would_skip) 102 return false; 103 104 return (op_is_sync(bio->bi_opf) || 105 bio->bi_opf & (REQ_META|REQ_PRIO) || 106 in_use <= bch_cutoff_writeback); 107 } 108 109 static inline void bch_writeback_queue(struct cached_dev *dc) 110 { 111 if (!IS_ERR_OR_NULL(dc->writeback_thread)) 112 wake_up_process(dc->writeback_thread); 113 } 114 115 static inline void bch_writeback_add(struct cached_dev *dc) 116 { 117 if (!atomic_read(&dc->has_dirty) && 118 !atomic_xchg(&dc->has_dirty, 1)) { 119 if (BDEV_STATE(&dc->sb) != BDEV_STATE_DIRTY) { 120 SET_BDEV_STATE(&dc->sb, BDEV_STATE_DIRTY); 121 /* XXX: should do this synchronously */ 122 bch_write_bdev_super(dc, NULL); 123 } 124 125 bch_writeback_queue(dc); 126 } 127 } 128 129 void bcache_dev_sectors_dirty_add(struct cache_set *c, unsigned int inode, 130 uint64_t offset, int nr_sectors); 131 132 void bch_sectors_dirty_init(struct bcache_device *d); 133 void bch_cached_dev_writeback_init(struct cached_dev *dc); 134 int bch_cached_dev_writeback_start(struct cached_dev *dc); 135 136 #endif 137