backing-dev.h 9.5 KB

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