backing-dev.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * include/linux/backing-dev.h
  3. *
  4. * low-level device information and state which is propagated up through
  5. * to high-level code.
  6. */
  7. #ifndef _LINUX_BACKING_DEV_H
  8. #define _LINUX_BACKING_DEV_H
  9. #include <linux/percpu_counter.h>
  10. #include <linux/log2.h>
  11. #include <linux/proportions.h>
  12. #include <linux/kernel.h>
  13. #include <asm/atomic.h>
  14. struct page;
  15. struct device;
  16. /*
  17. * Bits in backing_dev_info.state
  18. */
  19. enum bdi_state {
  20. BDI_pdflush, /* A pdflush thread is working this device */
  21. BDI_write_congested, /* The write queue is getting full */
  22. BDI_read_congested, /* The read queue is getting full */
  23. BDI_unused, /* Available bits start here */
  24. };
  25. typedef int (congested_fn)(void *, int);
  26. enum bdi_stat_item {
  27. BDI_RECLAIMABLE,
  28. BDI_WRITEBACK,
  29. NR_BDI_STAT_ITEMS
  30. };
  31. #define BDI_STAT_BATCH (8*(1+ilog2(nr_cpu_ids)))
  32. struct backing_dev_info {
  33. unsigned long ra_pages; /* max readahead in PAGE_CACHE_SIZE units */
  34. unsigned long state; /* Always use atomic bitops on this */
  35. unsigned int capabilities; /* Device capabilities */
  36. congested_fn *congested_fn; /* Function pointer if device is md/dm */
  37. void *congested_data; /* Pointer to aux data for congested func */
  38. void (*unplug_io_fn)(struct backing_dev_info *, struct page *);
  39. void *unplug_io_data;
  40. struct percpu_counter bdi_stat[NR_BDI_STAT_ITEMS];
  41. struct prop_local_percpu completions;
  42. int dirty_exceeded;
  43. unsigned int min_ratio;
  44. unsigned int max_ratio, max_prop_frac;
  45. struct device *dev;
  46. };
  47. int bdi_init(struct backing_dev_info *bdi);
  48. void bdi_destroy(struct backing_dev_info *bdi);
  49. int bdi_register(struct backing_dev_info *bdi, struct device *parent,
  50. const char *fmt, ...);
  51. int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev);
  52. void bdi_unregister(struct backing_dev_info *bdi);
  53. static inline void __add_bdi_stat(struct backing_dev_info *bdi,
  54. enum bdi_stat_item item, s64 amount)
  55. {
  56. __percpu_counter_add(&bdi->bdi_stat[item], amount, BDI_STAT_BATCH);
  57. }
  58. static inline void __inc_bdi_stat(struct backing_dev_info *bdi,
  59. enum bdi_stat_item item)
  60. {
  61. __add_bdi_stat(bdi, item, 1);
  62. }
  63. static inline void inc_bdi_stat(struct backing_dev_info *bdi,
  64. enum bdi_stat_item item)
  65. {
  66. unsigned long flags;
  67. local_irq_save(flags);
  68. __inc_bdi_stat(bdi, item);
  69. local_irq_restore(flags);
  70. }
  71. static inline void __dec_bdi_stat(struct backing_dev_info *bdi,
  72. enum bdi_stat_item item)
  73. {
  74. __add_bdi_stat(bdi, item, -1);
  75. }
  76. static inline void dec_bdi_stat(struct backing_dev_info *bdi,
  77. enum bdi_stat_item item)
  78. {
  79. unsigned long flags;
  80. local_irq_save(flags);
  81. __dec_bdi_stat(bdi, item);
  82. local_irq_restore(flags);
  83. }
  84. static inline s64 bdi_stat(struct backing_dev_info *bdi,
  85. enum bdi_stat_item item)
  86. {
  87. return percpu_counter_read_positive(&bdi->bdi_stat[item]);
  88. }
  89. static inline s64 __bdi_stat_sum(struct backing_dev_info *bdi,
  90. enum bdi_stat_item item)
  91. {
  92. return percpu_counter_sum_positive(&bdi->bdi_stat[item]);
  93. }
  94. static inline s64 bdi_stat_sum(struct backing_dev_info *bdi,
  95. enum bdi_stat_item item)
  96. {
  97. s64 sum;
  98. unsigned long flags;
  99. local_irq_save(flags);
  100. sum = __bdi_stat_sum(bdi, item);
  101. local_irq_restore(flags);
  102. return sum;
  103. }
  104. /*
  105. * maximal error of a stat counter.
  106. */
  107. static inline unsigned long bdi_stat_error(struct backing_dev_info *bdi)
  108. {
  109. #ifdef CONFIG_SMP
  110. return nr_cpu_ids * BDI_STAT_BATCH;
  111. #else
  112. return 1;
  113. #endif
  114. }
  115. int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio);
  116. int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ratio);
  117. /*
  118. * Flags in backing_dev_info::capability
  119. * - The first two flags control whether dirty pages will contribute to the
  120. * VM's accounting and whether writepages() should be called for dirty pages
  121. * (something that would not, for example, be appropriate for ramfs)
  122. * - These flags let !MMU mmap() govern direct device mapping vs immediate
  123. * copying more easily for MAP_PRIVATE, especially for ROM filesystems
  124. */
  125. #define BDI_CAP_NO_ACCT_DIRTY 0x00000001 /* Dirty pages shouldn't contribute to accounting */
  126. #define BDI_CAP_NO_WRITEBACK 0x00000002 /* Don't write pages back */
  127. #define BDI_CAP_MAP_COPY 0x00000004 /* Copy can be mapped (MAP_PRIVATE) */
  128. #define BDI_CAP_MAP_DIRECT 0x00000008 /* Can be mapped directly (MAP_SHARED) */
  129. #define BDI_CAP_READ_MAP 0x00000010 /* Can be mapped for reading */
  130. #define BDI_CAP_WRITE_MAP 0x00000020 /* Can be mapped for writing */
  131. #define BDI_CAP_EXEC_MAP 0x00000040 /* Can be mapped for execution */
  132. #define BDI_CAP_VMFLAGS \
  133. (BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | BDI_CAP_EXEC_MAP)
  134. #if defined(VM_MAYREAD) && \
  135. (BDI_CAP_READ_MAP != VM_MAYREAD || \
  136. BDI_CAP_WRITE_MAP != VM_MAYWRITE || \
  137. BDI_CAP_EXEC_MAP != VM_MAYEXEC)
  138. #error please change backing_dev_info::capabilities flags
  139. #endif
  140. extern struct backing_dev_info default_backing_dev_info;
  141. void default_unplug_io_fn(struct backing_dev_info *bdi, struct page *page);
  142. int writeback_in_progress(struct backing_dev_info *bdi);
  143. static inline int bdi_congested(struct backing_dev_info *bdi, int bdi_bits)
  144. {
  145. if (bdi->congested_fn)
  146. return bdi->congested_fn(bdi->congested_data, bdi_bits);
  147. return (bdi->state & bdi_bits);
  148. }
  149. static inline int bdi_read_congested(struct backing_dev_info *bdi)
  150. {
  151. return bdi_congested(bdi, 1 << BDI_read_congested);
  152. }
  153. static inline int bdi_write_congested(struct backing_dev_info *bdi)
  154. {
  155. return bdi_congested(bdi, 1 << BDI_write_congested);
  156. }
  157. static inline int bdi_rw_congested(struct backing_dev_info *bdi)
  158. {
  159. return bdi_congested(bdi, (1 << BDI_read_congested)|
  160. (1 << BDI_write_congested));
  161. }
  162. void clear_bdi_congested(struct backing_dev_info *bdi, int rw);
  163. void set_bdi_congested(struct backing_dev_info *bdi, int rw);
  164. long congestion_wait(int rw, long timeout);
  165. #define bdi_cap_writeback_dirty(bdi) \
  166. (!((bdi)->capabilities & BDI_CAP_NO_WRITEBACK))
  167. #define bdi_cap_account_dirty(bdi) \
  168. (!((bdi)->capabilities & BDI_CAP_NO_ACCT_DIRTY))
  169. #define mapping_cap_writeback_dirty(mapping) \
  170. bdi_cap_writeback_dirty((mapping)->backing_dev_info)
  171. #define mapping_cap_account_dirty(mapping) \
  172. bdi_cap_account_dirty((mapping)->backing_dev_info)
  173. #endif /* _LINUX_BACKING_DEV_H */