writeback.h 3.6 KB

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