backing-dev.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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/flex_proportions.h>
  12. #include <linux/kernel.h>
  13. #include <linux/fs.h>
  14. #include <linux/sched.h>
  15. #include <linux/timer.h>
  16. #include <linux/writeback.h>
  17. #include <linux/atomic.h>
  18. #include <linux/sysctl.h>
  19. #include <linux/workqueue.h>
  20. struct page;
  21. struct device;
  22. struct dentry;
  23. /*
  24. * Bits in bdi_writeback.state
  25. */
  26. enum wb_state {
  27. WB_async_congested, /* The async (write) queue is getting full */
  28. WB_sync_congested, /* The sync queue is getting full */
  29. WB_registered, /* bdi_register() was done */
  30. WB_writeback_running, /* Writeback is in progress */
  31. };
  32. typedef int (congested_fn)(void *, int);
  33. enum wb_stat_item {
  34. WB_RECLAIMABLE,
  35. WB_WRITEBACK,
  36. WB_DIRTIED,
  37. WB_WRITTEN,
  38. NR_WB_STAT_ITEMS
  39. };
  40. #define WB_STAT_BATCH (8*(1+ilog2(nr_cpu_ids)))
  41. struct bdi_writeback {
  42. struct backing_dev_info *bdi; /* our parent bdi */
  43. unsigned long state; /* Always use atomic bitops on this */
  44. unsigned long last_old_flush; /* last old data flush */
  45. struct delayed_work dwork; /* work item used for writeback */
  46. struct list_head b_dirty; /* dirty inodes */
  47. struct list_head b_io; /* parked for writeback */
  48. struct list_head b_more_io; /* parked for more writeback */
  49. struct list_head b_dirty_time; /* time stamps are dirty */
  50. spinlock_t list_lock; /* protects the b_* lists */
  51. struct percpu_counter stat[NR_WB_STAT_ITEMS];
  52. unsigned long bw_time_stamp; /* last time write bw is updated */
  53. unsigned long dirtied_stamp;
  54. unsigned long written_stamp; /* pages written at bw_time_stamp */
  55. unsigned long write_bandwidth; /* the estimated write bandwidth */
  56. unsigned long avg_write_bandwidth; /* further smoothed write bw */
  57. /*
  58. * The base dirty throttle rate, re-calculated on every 200ms.
  59. * All the bdi tasks' dirty rate will be curbed under it.
  60. * @dirty_ratelimit tracks the estimated @balanced_dirty_ratelimit
  61. * in small steps and is much more smooth/stable than the latter.
  62. */
  63. unsigned long dirty_ratelimit;
  64. unsigned long balanced_dirty_ratelimit;
  65. struct fprop_local_percpu completions;
  66. int dirty_exceeded;
  67. };
  68. struct backing_dev_info {
  69. struct list_head bdi_list;
  70. unsigned long ra_pages; /* max readahead in PAGE_CACHE_SIZE units */
  71. unsigned int capabilities; /* Device capabilities */
  72. congested_fn *congested_fn; /* Function pointer if device is md/dm */
  73. void *congested_data; /* Pointer to aux data for congested func */
  74. char *name;
  75. unsigned int min_ratio;
  76. unsigned int max_ratio, max_prop_frac;
  77. struct bdi_writeback wb; /* default writeback info for this bdi */
  78. spinlock_t wb_lock; /* protects work_list & wb.dwork scheduling */
  79. struct list_head work_list;
  80. struct device *dev;
  81. struct timer_list laptop_mode_wb_timer;
  82. #ifdef CONFIG_DEBUG_FS
  83. struct dentry *debug_dir;
  84. struct dentry *debug_stats;
  85. #endif
  86. };
  87. struct backing_dev_info *inode_to_bdi(struct inode *inode);
  88. int __must_check bdi_init(struct backing_dev_info *bdi);
  89. void bdi_destroy(struct backing_dev_info *bdi);
  90. __printf(3, 4)
  91. int bdi_register(struct backing_dev_info *bdi, struct device *parent,
  92. const char *fmt, ...);
  93. int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev);
  94. void bdi_unregister(struct backing_dev_info *bdi);
  95. int __must_check bdi_setup_and_register(struct backing_dev_info *, char *);
  96. void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
  97. enum wb_reason reason);
  98. void bdi_start_background_writeback(struct backing_dev_info *bdi);
  99. void bdi_writeback_workfn(struct work_struct *work);
  100. int bdi_has_dirty_io(struct backing_dev_info *bdi);
  101. void bdi_wakeup_thread_delayed(struct backing_dev_info *bdi);
  102. extern spinlock_t bdi_lock;
  103. extern struct list_head bdi_list;
  104. extern struct workqueue_struct *bdi_wq;
  105. static inline int wb_has_dirty_io(struct bdi_writeback *wb)
  106. {
  107. return !list_empty(&wb->b_dirty) ||
  108. !list_empty(&wb->b_io) ||
  109. !list_empty(&wb->b_more_io);
  110. }
  111. static inline void __add_wb_stat(struct bdi_writeback *wb,
  112. enum wb_stat_item item, s64 amount)
  113. {
  114. __percpu_counter_add(&wb->stat[item], amount, WB_STAT_BATCH);
  115. }
  116. static inline void __inc_wb_stat(struct bdi_writeback *wb,
  117. enum wb_stat_item item)
  118. {
  119. __add_wb_stat(wb, item, 1);
  120. }
  121. static inline void inc_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
  122. {
  123. unsigned long flags;
  124. local_irq_save(flags);
  125. __inc_wb_stat(wb, item);
  126. local_irq_restore(flags);
  127. }
  128. static inline void __dec_wb_stat(struct bdi_writeback *wb,
  129. enum wb_stat_item item)
  130. {
  131. __add_wb_stat(wb, item, -1);
  132. }
  133. static inline void dec_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
  134. {
  135. unsigned long flags;
  136. local_irq_save(flags);
  137. __dec_wb_stat(wb, item);
  138. local_irq_restore(flags);
  139. }
  140. static inline s64 wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
  141. {
  142. return percpu_counter_read_positive(&wb->stat[item]);
  143. }
  144. static inline s64 __wb_stat_sum(struct bdi_writeback *wb,
  145. enum wb_stat_item item)
  146. {
  147. return percpu_counter_sum_positive(&wb->stat[item]);
  148. }
  149. static inline s64 wb_stat_sum(struct bdi_writeback *wb, enum wb_stat_item item)
  150. {
  151. s64 sum;
  152. unsigned long flags;
  153. local_irq_save(flags);
  154. sum = __wb_stat_sum(wb, item);
  155. local_irq_restore(flags);
  156. return sum;
  157. }
  158. extern void wb_writeout_inc(struct bdi_writeback *wb);
  159. /*
  160. * maximal error of a stat counter.
  161. */
  162. static inline unsigned long wb_stat_error(struct bdi_writeback *wb)
  163. {
  164. #ifdef CONFIG_SMP
  165. return nr_cpu_ids * WB_STAT_BATCH;
  166. #else
  167. return 1;
  168. #endif
  169. }
  170. int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio);
  171. int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ratio);
  172. /*
  173. * Flags in backing_dev_info::capability
  174. *
  175. * The first three flags control whether dirty pages will contribute to the
  176. * VM's accounting and whether writepages() should be called for dirty pages
  177. * (something that would not, for example, be appropriate for ramfs)
  178. *
  179. * WARNING: these flags are closely related and should not normally be
  180. * used separately. The BDI_CAP_NO_ACCT_AND_WRITEBACK combines these
  181. * three flags into a single convenience macro.
  182. *
  183. * BDI_CAP_NO_ACCT_DIRTY: Dirty pages shouldn't contribute to accounting
  184. * BDI_CAP_NO_WRITEBACK: Don't write pages back
  185. * BDI_CAP_NO_ACCT_WB: Don't automatically account writeback pages
  186. * BDI_CAP_STRICTLIMIT: Keep number of dirty pages below bdi threshold.
  187. */
  188. #define BDI_CAP_NO_ACCT_DIRTY 0x00000001
  189. #define BDI_CAP_NO_WRITEBACK 0x00000002
  190. #define BDI_CAP_NO_ACCT_WB 0x00000004
  191. #define BDI_CAP_STABLE_WRITES 0x00000008
  192. #define BDI_CAP_STRICTLIMIT 0x00000010
  193. #define BDI_CAP_NO_ACCT_AND_WRITEBACK \
  194. (BDI_CAP_NO_WRITEBACK | BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_ACCT_WB)
  195. extern struct backing_dev_info noop_backing_dev_info;
  196. int writeback_in_progress(struct backing_dev_info *bdi);
  197. static inline int bdi_congested(struct backing_dev_info *bdi, int bdi_bits)
  198. {
  199. if (bdi->congested_fn)
  200. return bdi->congested_fn(bdi->congested_data, bdi_bits);
  201. return (bdi->wb.state & bdi_bits);
  202. }
  203. static inline int bdi_read_congested(struct backing_dev_info *bdi)
  204. {
  205. return bdi_congested(bdi, 1 << WB_sync_congested);
  206. }
  207. static inline int bdi_write_congested(struct backing_dev_info *bdi)
  208. {
  209. return bdi_congested(bdi, 1 << WB_async_congested);
  210. }
  211. static inline int bdi_rw_congested(struct backing_dev_info *bdi)
  212. {
  213. return bdi_congested(bdi, (1 << WB_sync_congested) |
  214. (1 << WB_async_congested));
  215. }
  216. enum {
  217. BLK_RW_ASYNC = 0,
  218. BLK_RW_SYNC = 1,
  219. };
  220. void clear_bdi_congested(struct backing_dev_info *bdi, int sync);
  221. void set_bdi_congested(struct backing_dev_info *bdi, int sync);
  222. long congestion_wait(int sync, long timeout);
  223. long wait_iff_congested(struct zone *zone, int sync, long timeout);
  224. int pdflush_proc_obsolete(struct ctl_table *table, int write,
  225. void __user *buffer, size_t *lenp, loff_t *ppos);
  226. static inline bool bdi_cap_stable_pages_required(struct backing_dev_info *bdi)
  227. {
  228. return bdi->capabilities & BDI_CAP_STABLE_WRITES;
  229. }
  230. static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi)
  231. {
  232. return !(bdi->capabilities & BDI_CAP_NO_WRITEBACK);
  233. }
  234. static inline bool bdi_cap_account_dirty(struct backing_dev_info *bdi)
  235. {
  236. return !(bdi->capabilities & BDI_CAP_NO_ACCT_DIRTY);
  237. }
  238. static inline bool bdi_cap_account_writeback(struct backing_dev_info *bdi)
  239. {
  240. /* Paranoia: BDI_CAP_NO_WRITEBACK implies BDI_CAP_NO_ACCT_WB */
  241. return !(bdi->capabilities & (BDI_CAP_NO_ACCT_WB |
  242. BDI_CAP_NO_WRITEBACK));
  243. }
  244. static inline bool mapping_cap_writeback_dirty(struct address_space *mapping)
  245. {
  246. return bdi_cap_writeback_dirty(inode_to_bdi(mapping->host));
  247. }
  248. static inline bool mapping_cap_account_dirty(struct address_space *mapping)
  249. {
  250. return bdi_cap_account_dirty(inode_to_bdi(mapping->host));
  251. }
  252. static inline int bdi_sched_wait(void *word)
  253. {
  254. schedule();
  255. return 0;
  256. }
  257. #endif /* _LINUX_BACKING_DEV_H */