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 uint64_t bcache_flash_devs_sectors_dirty(struct cache_set *c)
  24. {
  25. uint64_t i, ret = 0;
  26. mutex_lock(&bch_register_lock);
  27. for (i = 0; i < c->devices_max_used; i++) {
  28. struct bcache_device *d = c->devices[i];
  29. if (!d || !UUID_FLASH_ONLY(&c->uuids[i]))
  30. continue;
  31. ret += bcache_dev_sectors_dirty(d);
  32. }
  33. mutex_unlock(&bch_register_lock);
  34. return ret;
  35. }
  36. static inline unsigned offset_to_stripe(struct bcache_device *d,
  37. uint64_t offset)
  38. {
  39. do_div(offset, d->stripe_size);
  40. return offset;
  41. }
  42. static inline bool bcache_dev_stripe_dirty(struct cached_dev *dc,
  43. uint64_t offset,
  44. unsigned nr_sectors)
  45. {
  46. unsigned stripe = offset_to_stripe(&dc->disk, offset);
  47. while (1) {
  48. if (atomic_read(dc->disk.stripe_sectors_dirty + stripe))
  49. return true;
  50. if (nr_sectors <= dc->disk.stripe_size)
  51. return false;
  52. nr_sectors -= dc->disk.stripe_size;
  53. stripe++;
  54. }
  55. }
  56. static inline bool should_writeback(struct cached_dev *dc, struct bio *bio,
  57. unsigned cache_mode, bool would_skip)
  58. {
  59. unsigned in_use = dc->disk.c->gc_stats.in_use;
  60. if (cache_mode != CACHE_MODE_WRITEBACK ||
  61. test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) ||
  62. in_use > CUTOFF_WRITEBACK_SYNC)
  63. return false;
  64. if (dc->partial_stripes_expensive &&
  65. bcache_dev_stripe_dirty(dc, bio->bi_iter.bi_sector,
  66. bio_sectors(bio)))
  67. return true;
  68. if (would_skip)
  69. return false;
  70. return (op_is_sync(bio->bi_opf) ||
  71. bio->bi_opf & (REQ_META|REQ_PRIO) ||
  72. in_use <= CUTOFF_WRITEBACK);
  73. }
  74. static inline void bch_writeback_queue(struct cached_dev *dc)
  75. {
  76. if (!IS_ERR_OR_NULL(dc->writeback_thread))
  77. wake_up_process(dc->writeback_thread);
  78. }
  79. static inline void bch_writeback_add(struct cached_dev *dc)
  80. {
  81. if (!atomic_read(&dc->has_dirty) &&
  82. !atomic_xchg(&dc->has_dirty, 1)) {
  83. if (BDEV_STATE(&dc->sb) != BDEV_STATE_DIRTY) {
  84. SET_BDEV_STATE(&dc->sb, BDEV_STATE_DIRTY);
  85. /* XXX: should do this synchronously */
  86. bch_write_bdev_super(dc, NULL);
  87. }
  88. bch_writeback_queue(dc);
  89. }
  90. }
  91. void bcache_dev_sectors_dirty_add(struct cache_set *, unsigned, uint64_t, int);
  92. void bch_sectors_dirty_init(struct bcache_device *);
  93. void bch_cached_dev_writeback_init(struct cached_dev *);
  94. int bch_cached_dev_writeback_start(struct cached_dev *);
  95. #endif