backing-dev.h 10 KB

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