writeback.h 3.8 KB

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