backing-dev.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 <linux/fs.h>
  14. #include <linux/sched.h>
  15. #include <linux/timer.h>
  16. #include <linux/writeback.h>
  17. #include <linux/atomic.h>
  18. struct page;
  19. struct device;
  20. struct dentry;
  21. /*
  22. * Bits in backing_dev_info.state
  23. */
  24. enum bdi_state {
  25. BDI_pending, /* On its way to being activated */
  26. BDI_wb_alloc, /* Default embedded wb allocated */
  27. BDI_async_congested, /* The async (write) queue is getting full */
  28. BDI_sync_congested, /* The sync queue is getting full */
  29. BDI_registered, /* bdi_register() was done */
  30. BDI_writeback_running, /* Writeback is in progress */
  31. BDI_unused, /* Available bits start here */
  32. };
  33. typedef int (congested_fn)(void *, int);
  34. enum bdi_stat_item {
  35. BDI_RECLAIMABLE,
  36. BDI_WRITEBACK,
  37. BDI_DIRTIED,
  38. BDI_WRITTEN,
  39. NR_BDI_STAT_ITEMS
  40. };
  41. #define BDI_STAT_BATCH (8*(1+ilog2(nr_cpu_ids)))
  42. struct bdi_writeback {
  43. struct backing_dev_info *bdi; /* our parent bdi */
  44. unsigned int nr;
  45. unsigned long last_old_flush; /* last old data flush */
  46. unsigned long last_active; /* last time bdi thread was active */
  47. struct task_struct *task; /* writeback thread */
  48. struct timer_list wakeup_timer; /* used for delayed bdi thread wakeup */
  49. struct list_head b_dirty; /* dirty inodes */
  50. struct list_head b_io; /* parked for writeback */
  51. struct list_head b_more_io; /* parked for more writeback */
  52. spinlock_t list_lock; /* protects the b_* lists */
  53. };
  54. struct backing_dev_info {
  55. struct list_head bdi_list;
  56. unsigned long ra_pages; /* max readahead in PAGE_CACHE_SIZE units */
  57. unsigned long state; /* Always use atomic bitops on this */
  58. unsigned int capabilities; /* Device capabilities */
  59. congested_fn *congested_fn; /* Function pointer if device is md/dm */
  60. void *congested_data; /* Pointer to aux data for congested func */
  61. char *name;
  62. struct percpu_counter bdi_stat[NR_BDI_STAT_ITEMS];
  63. unsigned long bw_time_stamp; /* last time write bw is updated */
  64. unsigned long dirtied_stamp;
  65. unsigned long written_stamp; /* pages written at bw_time_stamp */
  66. unsigned long write_bandwidth; /* the estimated write bandwidth */
  67. unsigned long avg_write_bandwidth; /* further smoothed write bw */
  68. /*
  69. * The base dirty throttle rate, re-calculated on every 200ms.
  70. * All the bdi tasks' dirty rate will be curbed under it.
  71. * @dirty_ratelimit tracks the estimated @balanced_dirty_ratelimit
  72. * in small steps and is much more smooth/stable than the latter.
  73. */
  74. unsigned long dirty_ratelimit;
  75. unsigned long balanced_dirty_ratelimit;
  76. struct prop_local_percpu completions;
  77. int dirty_exceeded;
  78. unsigned int min_ratio;
  79. unsigned int max_ratio, max_prop_frac;
  80. struct bdi_writeback wb; /* default writeback info for this bdi */
  81. spinlock_t wb_lock; /* protects work_list */
  82. struct list_head work_list;
  83. struct device *dev;
  84. struct timer_list laptop_mode_wb_timer;
  85. #ifdef CONFIG_DEBUG_FS
  86. struct dentry *debug_dir;
  87. struct dentry *debug_stats;
  88. #endif
  89. };
  90. int bdi_init(struct backing_dev_info *bdi);
  91. void bdi_destroy(struct backing_dev_info *bdi);
  92. int bdi_register(struct backing_dev_info *bdi, struct device *parent,
  93. const char *fmt, ...);
  94. int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev);
  95. void bdi_unregister(struct backing_dev_info *bdi);
  96. int bdi_setup_and_register(struct backing_dev_info *, char *, unsigned int);
  97. void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
  98. enum wb_reason reason);
  99. void bdi_start_background_writeback(struct backing_dev_info *bdi);
  100. int bdi_writeback_thread(void *data);
  101. int bdi_has_dirty_io(struct backing_dev_info *bdi);
  102. void bdi_arm_supers_timer(void);
  103. void bdi_wakeup_thread_delayed(struct backing_dev_info *bdi);
  104. void bdi_lock_two(struct bdi_writeback *wb1, struct bdi_writeback *wb2);
  105. extern spinlock_t bdi_lock;
  106. extern struct list_head bdi_list;
  107. extern struct list_head bdi_pending_list;
  108. static inline int wb_has_dirty_io(struct bdi_writeback *wb)
  109. {
  110. return !list_empty(&wb->b_dirty) ||
  111. !list_empty(&wb->b_io) ||
  112. !list_empty(&wb->b_more_io);
  113. }
  114. static inline void __add_bdi_stat(struct backing_dev_info *bdi,
  115. enum bdi_stat_item item, s64 amount)
  116. {
  117. __percpu_counter_add(&bdi->bdi_stat[item], amount, BDI_STAT_BATCH);
  118. }
  119. static inline void __inc_bdi_stat(struct backing_dev_info *bdi,
  120. enum bdi_stat_item item)
  121. {
  122. __add_bdi_stat(bdi, item, 1);
  123. }
  124. static inline void inc_bdi_stat(struct backing_dev_info *bdi,
  125. enum bdi_stat_item item)
  126. {
  127. unsigned long flags;
  128. local_irq_save(flags);
  129. __inc_bdi_stat(bdi, item);
  130. local_irq_restore(flags);
  131. }
  132. static inline void __dec_bdi_stat(struct backing_dev_info *bdi,
  133. enum bdi_stat_item item)
  134. {
  135. __add_bdi_stat(bdi, item, -1);
  136. }
  137. static inline void dec_bdi_stat(struct backing_dev_info *bdi,
  138. enum bdi_stat_item item)
  139. {
  140. unsigned long flags;
  141. local_irq_save(flags);
  142. __dec_bdi_stat(bdi, item);
  143. local_irq_restore(flags);
  144. }
  145. static inline s64 bdi_stat(struct backing_dev_info *bdi,
  146. enum bdi_stat_item item)
  147. {
  148. return percpu_counter_read_positive(&bdi->bdi_stat[item]);
  149. }
  150. static inline s64 __bdi_stat_sum(struct backing_dev_info *bdi,
  151. enum bdi_stat_item item)
  152. {
  153. return percpu_counter_sum_positive(&bdi->bdi_stat[item]);
  154. }
  155. static inline s64 bdi_stat_sum(struct backing_dev_info *bdi,
  156. enum bdi_stat_item item)
  157. {
  158. s64 sum;
  159. unsigned long flags;
  160. local_irq_save(flags);
  161. sum = __bdi_stat_sum(bdi, item);
  162. local_irq_restore(flags);
  163. return sum;
  164. }
  165. extern void bdi_writeout_inc(struct backing_dev_info *bdi);
  166. /*
  167. * maximal error of a stat counter.
  168. */
  169. static inline unsigned long bdi_stat_error(struct backing_dev_info *bdi)
  170. {
  171. #ifdef CONFIG_SMP
  172. return nr_cpu_ids * BDI_STAT_BATCH;
  173. #else
  174. return 1;
  175. #endif
  176. }
  177. int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio);
  178. int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ratio);
  179. /*
  180. * Flags in backing_dev_info::capability
  181. *
  182. * The first three flags control whether dirty pages will contribute to the
  183. * VM's accounting and whether writepages() should be called for dirty pages
  184. * (something that would not, for example, be appropriate for ramfs)
  185. *
  186. * WARNING: these flags are closely related and should not normally be
  187. * used separately. The BDI_CAP_NO_ACCT_AND_WRITEBACK combines these
  188. * three flags into a single convenience macro.
  189. *
  190. * BDI_CAP_NO_ACCT_DIRTY: Dirty pages shouldn't contribute to accounting
  191. * BDI_CAP_NO_WRITEBACK: Don't write pages back
  192. * BDI_CAP_NO_ACCT_WB: Don't automatically account writeback pages
  193. *
  194. * These flags let !MMU mmap() govern direct device mapping vs immediate
  195. * copying more easily for MAP_PRIVATE, especially for ROM filesystems.
  196. *
  197. * BDI_CAP_MAP_COPY: Copy can be mapped (MAP_PRIVATE)
  198. * BDI_CAP_MAP_DIRECT: Can be mapped directly (MAP_SHARED)
  199. * BDI_CAP_READ_MAP: Can be mapped for reading
  200. * BDI_CAP_WRITE_MAP: Can be mapped for writing
  201. * BDI_CAP_EXEC_MAP: Can be mapped for execution
  202. *
  203. * BDI_CAP_SWAP_BACKED: Count shmem/tmpfs objects as swap-backed.
  204. */
  205. #define BDI_CAP_NO_ACCT_DIRTY 0x00000001
  206. #define BDI_CAP_NO_WRITEBACK 0x00000002
  207. #define BDI_CAP_MAP_COPY 0x00000004
  208. #define BDI_CAP_MAP_DIRECT 0x00000008
  209. #define BDI_CAP_READ_MAP 0x00000010
  210. #define BDI_CAP_WRITE_MAP 0x00000020
  211. #define BDI_CAP_EXEC_MAP 0x00000040
  212. #define BDI_CAP_NO_ACCT_WB 0x00000080
  213. #define BDI_CAP_SWAP_BACKED 0x00000100
  214. #define BDI_CAP_VMFLAGS \
  215. (BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | BDI_CAP_EXEC_MAP)
  216. #define BDI_CAP_NO_ACCT_AND_WRITEBACK \
  217. (BDI_CAP_NO_WRITEBACK | BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_ACCT_WB)
  218. #if defined(VM_MAYREAD) && \
  219. (BDI_CAP_READ_MAP != VM_MAYREAD || \
  220. BDI_CAP_WRITE_MAP != VM_MAYWRITE || \
  221. BDI_CAP_EXEC_MAP != VM_MAYEXEC)
  222. #error please change backing_dev_info::capabilities flags
  223. #endif
  224. extern struct backing_dev_info default_backing_dev_info;
  225. extern struct backing_dev_info noop_backing_dev_info;
  226. int writeback_in_progress(struct backing_dev_info *bdi);
  227. static inline int bdi_congested(struct backing_dev_info *bdi, int bdi_bits)
  228. {
  229. if (bdi->congested_fn)
  230. return bdi->congested_fn(bdi->congested_data, bdi_bits);
  231. return (bdi->state & bdi_bits);
  232. }
  233. static inline int bdi_read_congested(struct backing_dev_info *bdi)
  234. {
  235. return bdi_congested(bdi, 1 << BDI_sync_congested);
  236. }
  237. static inline int bdi_write_congested(struct backing_dev_info *bdi)
  238. {
  239. return bdi_congested(bdi, 1 << BDI_async_congested);
  240. }
  241. static inline int bdi_rw_congested(struct backing_dev_info *bdi)
  242. {
  243. return bdi_congested(bdi, (1 << BDI_sync_congested) |
  244. (1 << BDI_async_congested));
  245. }
  246. enum {
  247. BLK_RW_ASYNC = 0,
  248. BLK_RW_SYNC = 1,
  249. };
  250. void clear_bdi_congested(struct backing_dev_info *bdi, int sync);
  251. void set_bdi_congested(struct backing_dev_info *bdi, int sync);
  252. long congestion_wait(int sync, long timeout);
  253. long wait_iff_congested(struct zone *zone, int sync, long timeout);
  254. static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi)
  255. {
  256. return !(bdi->capabilities & BDI_CAP_NO_WRITEBACK);
  257. }
  258. static inline bool bdi_cap_account_dirty(struct backing_dev_info *bdi)
  259. {
  260. return !(bdi->capabilities & BDI_CAP_NO_ACCT_DIRTY);
  261. }
  262. static inline bool bdi_cap_account_writeback(struct backing_dev_info *bdi)
  263. {
  264. /* Paranoia: BDI_CAP_NO_WRITEBACK implies BDI_CAP_NO_ACCT_WB */
  265. return !(bdi->capabilities & (BDI_CAP_NO_ACCT_WB |
  266. BDI_CAP_NO_WRITEBACK));
  267. }
  268. static inline bool bdi_cap_swap_backed(struct backing_dev_info *bdi)
  269. {
  270. return bdi->capabilities & BDI_CAP_SWAP_BACKED;
  271. }
  272. static inline bool bdi_cap_flush_forker(struct backing_dev_info *bdi)
  273. {
  274. return bdi == &default_backing_dev_info;
  275. }
  276. static inline bool mapping_cap_writeback_dirty(struct address_space *mapping)
  277. {
  278. return bdi_cap_writeback_dirty(mapping->backing_dev_info);
  279. }
  280. static inline bool mapping_cap_account_dirty(struct address_space *mapping)
  281. {
  282. return bdi_cap_account_dirty(mapping->backing_dev_info);
  283. }
  284. static inline bool mapping_cap_swap_backed(struct address_space *mapping)
  285. {
  286. return bdi_cap_swap_backed(mapping->backing_dev_info);
  287. }
  288. static inline int bdi_sched_wait(void *word)
  289. {
  290. schedule();
  291. return 0;
  292. }
  293. #endif /* _LINUX_BACKING_DEV_H */