writeback.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 MAX_WRITEBACKS_IN_PASS 5
  7. #define MAX_WRITESIZE_IN_PASS 5000 /* *512b */
  8. #define WRITEBACK_RATE_UPDATE_SECS_MAX 60
  9. #define WRITEBACK_RATE_UPDATE_SECS_DEFAULT 5
  10. /*
  11. * 14 (16384ths) is chosen here as something that each backing device
  12. * should be a reasonable fraction of the share, and not to blow up
  13. * until individual backing devices are a petabyte.
  14. */
  15. #define WRITEBACK_SHARE_SHIFT 14
  16. static inline uint64_t bcache_dev_sectors_dirty(struct bcache_device *d)
  17. {
  18. uint64_t i, ret = 0;
  19. for (i = 0; i < d->nr_stripes; i++)
  20. ret += atomic_read(d->stripe_sectors_dirty + i);
  21. return ret;
  22. }
  23. static inline int offset_to_stripe(struct bcache_device *d,
  24. uint64_t offset)
  25. {
  26. do_div(offset, d->stripe_size);
  27. /* d->nr_stripes is in range [1, INT_MAX] */
  28. if (unlikely(offset >= d->nr_stripes)) {
  29. pr_err("Invalid stripe %llu (>= nr_stripes %d).\n",
  30. offset, d->nr_stripes);
  31. return -EINVAL;
  32. }
  33. /*
  34. * Here offset is definitly smaller than INT_MAX,
  35. * return it as int will never overflow.
  36. */
  37. return offset;
  38. }
  39. static inline bool bcache_dev_stripe_dirty(struct cached_dev *dc,
  40. uint64_t offset,
  41. unsigned int nr_sectors)
  42. {
  43. int stripe = offset_to_stripe(&dc->disk, offset);
  44. if (stripe < 0)
  45. return false;
  46. while (1) {
  47. if (atomic_read(dc->disk.stripe_sectors_dirty + stripe))
  48. return true;
  49. if (nr_sectors <= dc->disk.stripe_size)
  50. return false;
  51. nr_sectors -= dc->disk.stripe_size;
  52. stripe++;
  53. }
  54. }
  55. static inline bool should_writeback(struct cached_dev *dc, struct bio *bio,
  56. unsigned int cache_mode, bool would_skip)
  57. {
  58. unsigned int in_use = dc->disk.c->gc_stats.in_use;
  59. if (cache_mode != CACHE_MODE_WRITEBACK ||
  60. test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) ||
  61. in_use > CUTOFF_WRITEBACK_SYNC)
  62. return false;
  63. if (bio_op(bio) == REQ_OP_DISCARD)
  64. return false;
  65. if (dc->partial_stripes_expensive &&
  66. bcache_dev_stripe_dirty(dc, bio->bi_iter.bi_sector,
  67. bio_sectors(bio)))
  68. return true;
  69. if (would_skip)
  70. return false;
  71. return (op_is_sync(bio->bi_opf) ||
  72. bio->bi_opf & (REQ_META|REQ_PRIO) ||
  73. in_use <= CUTOFF_WRITEBACK);
  74. }
  75. static inline void bch_writeback_queue(struct cached_dev *dc)
  76. {
  77. if (!IS_ERR_OR_NULL(dc->writeback_thread))
  78. wake_up_process(dc->writeback_thread);
  79. }
  80. static inline void bch_writeback_add(struct cached_dev *dc)
  81. {
  82. if (!atomic_read(&dc->has_dirty) &&
  83. !atomic_xchg(&dc->has_dirty, 1)) {
  84. if (BDEV_STATE(&dc->sb) != BDEV_STATE_DIRTY) {
  85. SET_BDEV_STATE(&dc->sb, BDEV_STATE_DIRTY);
  86. /* XXX: should do this synchronously */
  87. bch_write_bdev_super(dc, NULL);
  88. }
  89. bch_writeback_queue(dc);
  90. }
  91. }
  92. void bcache_dev_sectors_dirty_add(struct cache_set *c, unsigned int inode,
  93. uint64_t offset, int nr_sectors);
  94. void bch_sectors_dirty_init(struct bcache_device *d);
  95. void bch_cached_dev_writeback_init(struct cached_dev *dc);
  96. int bch_cached_dev_writeback_start(struct cached_dev *dc);
  97. #endif