writeback.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #ifndef _BCACHE_WRITEBACK_H
  2. #define _BCACHE_WRITEBACK_H
  3. #define CUTOFF_WRITEBACK 40
  4. #define CUTOFF_WRITEBACK_SYNC 70
  5. static inline uint64_t bcache_dev_sectors_dirty(struct bcache_device *d)
  6. {
  7. uint64_t i, ret = 0;
  8. for (i = 0; i < d->nr_stripes; i++)
  9. ret += atomic_read(d->stripe_sectors_dirty + i);
  10. return ret;
  11. }
  12. static inline uint64_t bcache_flash_devs_sectors_dirty(struct cache_set *c)
  13. {
  14. uint64_t i, ret = 0;
  15. mutex_lock(&bch_register_lock);
  16. for (i = 0; i < c->nr_uuids; i++) {
  17. struct bcache_device *d = c->devices[i];
  18. if (!d || !UUID_FLASH_ONLY(&c->uuids[i]))
  19. continue;
  20. ret += bcache_dev_sectors_dirty(d);
  21. }
  22. mutex_unlock(&bch_register_lock);
  23. return ret;
  24. }
  25. static inline unsigned offset_to_stripe(struct bcache_device *d,
  26. uint64_t offset)
  27. {
  28. do_div(offset, d->stripe_size);
  29. return offset;
  30. }
  31. static inline bool bcache_dev_stripe_dirty(struct cached_dev *dc,
  32. uint64_t offset,
  33. unsigned nr_sectors)
  34. {
  35. unsigned stripe = offset_to_stripe(&dc->disk, offset);
  36. while (1) {
  37. if (atomic_read(dc->disk.stripe_sectors_dirty + stripe))
  38. return true;
  39. if (nr_sectors <= dc->disk.stripe_size)
  40. return false;
  41. nr_sectors -= dc->disk.stripe_size;
  42. stripe++;
  43. }
  44. }
  45. static inline bool should_writeback(struct cached_dev *dc, struct bio *bio,
  46. unsigned cache_mode, bool would_skip)
  47. {
  48. unsigned in_use = dc->disk.c->gc_stats.in_use;
  49. if (cache_mode != CACHE_MODE_WRITEBACK ||
  50. test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) ||
  51. in_use > CUTOFF_WRITEBACK_SYNC)
  52. return false;
  53. if (dc->partial_stripes_expensive &&
  54. bcache_dev_stripe_dirty(dc, bio->bi_iter.bi_sector,
  55. bio_sectors(bio)))
  56. return true;
  57. if (would_skip)
  58. return false;
  59. return bio->bi_opf & REQ_SYNC ||
  60. in_use <= CUTOFF_WRITEBACK;
  61. }
  62. static inline void bch_writeback_queue(struct cached_dev *dc)
  63. {
  64. if (!IS_ERR_OR_NULL(dc->writeback_thread))
  65. wake_up_process(dc->writeback_thread);
  66. }
  67. static inline void bch_writeback_add(struct cached_dev *dc)
  68. {
  69. if (!atomic_read(&dc->has_dirty) &&
  70. !atomic_xchg(&dc->has_dirty, 1)) {
  71. atomic_inc(&dc->count);
  72. if (BDEV_STATE(&dc->sb) != BDEV_STATE_DIRTY) {
  73. SET_BDEV_STATE(&dc->sb, BDEV_STATE_DIRTY);
  74. /* XXX: should do this synchronously */
  75. bch_write_bdev_super(dc, NULL);
  76. }
  77. bch_writeback_queue(dc);
  78. }
  79. }
  80. void bcache_dev_sectors_dirty_add(struct cache_set *, unsigned, uint64_t, int);
  81. void bch_sectors_dirty_init(struct bcache_device *);
  82. void bch_cached_dev_writeback_init(struct cached_dev *);
  83. int bch_cached_dev_writeback_start(struct cached_dev *);
  84. #endif