writeback.h 2.7 KB

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