backing-dev.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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/kernel.h>
  10. #include <linux/fs.h>
  11. #include <linux/sched.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/writeback.h>
  14. #include <linux/blk-cgroup.h>
  15. #include <linux/backing-dev-defs.h>
  16. int __must_check bdi_init(struct backing_dev_info *bdi);
  17. void bdi_destroy(struct backing_dev_info *bdi);
  18. __printf(3, 4)
  19. int bdi_register(struct backing_dev_info *bdi, struct device *parent,
  20. const char *fmt, ...);
  21. int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev);
  22. void bdi_unregister(struct backing_dev_info *bdi);
  23. int __must_check bdi_setup_and_register(struct backing_dev_info *, char *);
  24. void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
  25. enum wb_reason reason);
  26. void bdi_start_background_writeback(struct backing_dev_info *bdi);
  27. void wb_workfn(struct work_struct *work);
  28. int bdi_has_dirty_io(struct backing_dev_info *bdi);
  29. void wb_wakeup_delayed(struct bdi_writeback *wb);
  30. extern spinlock_t bdi_lock;
  31. extern struct list_head bdi_list;
  32. extern struct workqueue_struct *bdi_wq;
  33. static inline int wb_has_dirty_io(struct bdi_writeback *wb)
  34. {
  35. return !list_empty(&wb->b_dirty) ||
  36. !list_empty(&wb->b_io) ||
  37. !list_empty(&wb->b_more_io);
  38. }
  39. static inline void __add_wb_stat(struct bdi_writeback *wb,
  40. enum wb_stat_item item, s64 amount)
  41. {
  42. __percpu_counter_add(&wb->stat[item], amount, WB_STAT_BATCH);
  43. }
  44. static inline void __inc_wb_stat(struct bdi_writeback *wb,
  45. enum wb_stat_item item)
  46. {
  47. __add_wb_stat(wb, item, 1);
  48. }
  49. static inline void inc_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
  50. {
  51. unsigned long flags;
  52. local_irq_save(flags);
  53. __inc_wb_stat(wb, item);
  54. local_irq_restore(flags);
  55. }
  56. static inline void __dec_wb_stat(struct bdi_writeback *wb,
  57. enum wb_stat_item item)
  58. {
  59. __add_wb_stat(wb, item, -1);
  60. }
  61. static inline void dec_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
  62. {
  63. unsigned long flags;
  64. local_irq_save(flags);
  65. __dec_wb_stat(wb, item);
  66. local_irq_restore(flags);
  67. }
  68. static inline s64 wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
  69. {
  70. return percpu_counter_read_positive(&wb->stat[item]);
  71. }
  72. static inline s64 __wb_stat_sum(struct bdi_writeback *wb,
  73. enum wb_stat_item item)
  74. {
  75. return percpu_counter_sum_positive(&wb->stat[item]);
  76. }
  77. static inline s64 wb_stat_sum(struct bdi_writeback *wb, enum wb_stat_item item)
  78. {
  79. s64 sum;
  80. unsigned long flags;
  81. local_irq_save(flags);
  82. sum = __wb_stat_sum(wb, item);
  83. local_irq_restore(flags);
  84. return sum;
  85. }
  86. extern void wb_writeout_inc(struct bdi_writeback *wb);
  87. /*
  88. * maximal error of a stat counter.
  89. */
  90. static inline unsigned long wb_stat_error(struct bdi_writeback *wb)
  91. {
  92. #ifdef CONFIG_SMP
  93. return nr_cpu_ids * WB_STAT_BATCH;
  94. #else
  95. return 1;
  96. #endif
  97. }
  98. int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio);
  99. int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ratio);
  100. /*
  101. * Flags in backing_dev_info::capability
  102. *
  103. * The first three flags control whether dirty pages will contribute to the
  104. * VM's accounting and whether writepages() should be called for dirty pages
  105. * (something that would not, for example, be appropriate for ramfs)
  106. *
  107. * WARNING: these flags are closely related and should not normally be
  108. * used separately. The BDI_CAP_NO_ACCT_AND_WRITEBACK combines these
  109. * three flags into a single convenience macro.
  110. *
  111. * BDI_CAP_NO_ACCT_DIRTY: Dirty pages shouldn't contribute to accounting
  112. * BDI_CAP_NO_WRITEBACK: Don't write pages back
  113. * BDI_CAP_NO_ACCT_WB: Don't automatically account writeback pages
  114. * BDI_CAP_STRICTLIMIT: Keep number of dirty pages below bdi threshold.
  115. *
  116. * BDI_CAP_CGROUP_WRITEBACK: Supports cgroup-aware writeback.
  117. */
  118. #define BDI_CAP_NO_ACCT_DIRTY 0x00000001
  119. #define BDI_CAP_NO_WRITEBACK 0x00000002
  120. #define BDI_CAP_NO_ACCT_WB 0x00000004
  121. #define BDI_CAP_STABLE_WRITES 0x00000008
  122. #define BDI_CAP_STRICTLIMIT 0x00000010
  123. #define BDI_CAP_CGROUP_WRITEBACK 0x00000020
  124. #define BDI_CAP_NO_ACCT_AND_WRITEBACK \
  125. (BDI_CAP_NO_WRITEBACK | BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_ACCT_WB)
  126. extern struct backing_dev_info noop_backing_dev_info;
  127. int writeback_in_progress(struct backing_dev_info *bdi);
  128. static inline struct backing_dev_info *inode_to_bdi(struct inode *inode)
  129. {
  130. struct super_block *sb;
  131. if (!inode)
  132. return &noop_backing_dev_info;
  133. sb = inode->i_sb;
  134. #ifdef CONFIG_BLOCK
  135. if (sb_is_blkdev_sb(sb))
  136. return blk_get_backing_dev_info(I_BDEV(inode));
  137. #endif
  138. return sb->s_bdi;
  139. }
  140. static inline int wb_congested(struct bdi_writeback *wb, int cong_bits)
  141. {
  142. struct backing_dev_info *bdi = wb->bdi;
  143. if (bdi->congested_fn)
  144. return bdi->congested_fn(bdi->congested_data, cong_bits);
  145. return wb->congested->state & cong_bits;
  146. }
  147. long congestion_wait(int sync, long timeout);
  148. long wait_iff_congested(struct zone *zone, int sync, long timeout);
  149. int pdflush_proc_obsolete(struct ctl_table *table, int write,
  150. void __user *buffer, size_t *lenp, loff_t *ppos);
  151. static inline bool bdi_cap_stable_pages_required(struct backing_dev_info *bdi)
  152. {
  153. return bdi->capabilities & BDI_CAP_STABLE_WRITES;
  154. }
  155. static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi)
  156. {
  157. return !(bdi->capabilities & BDI_CAP_NO_WRITEBACK);
  158. }
  159. static inline bool bdi_cap_account_dirty(struct backing_dev_info *bdi)
  160. {
  161. return !(bdi->capabilities & BDI_CAP_NO_ACCT_DIRTY);
  162. }
  163. static inline bool bdi_cap_account_writeback(struct backing_dev_info *bdi)
  164. {
  165. /* Paranoia: BDI_CAP_NO_WRITEBACK implies BDI_CAP_NO_ACCT_WB */
  166. return !(bdi->capabilities & (BDI_CAP_NO_ACCT_WB |
  167. BDI_CAP_NO_WRITEBACK));
  168. }
  169. static inline bool mapping_cap_writeback_dirty(struct address_space *mapping)
  170. {
  171. return bdi_cap_writeback_dirty(inode_to_bdi(mapping->host));
  172. }
  173. static inline bool mapping_cap_account_dirty(struct address_space *mapping)
  174. {
  175. return bdi_cap_account_dirty(inode_to_bdi(mapping->host));
  176. }
  177. static inline int bdi_sched_wait(void *word)
  178. {
  179. schedule();
  180. return 0;
  181. }
  182. #ifdef CONFIG_CGROUP_WRITEBACK
  183. struct bdi_writeback_congested *
  184. wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp);
  185. void wb_congested_put(struct bdi_writeback_congested *congested);
  186. struct bdi_writeback *wb_get_create(struct backing_dev_info *bdi,
  187. struct cgroup_subsys_state *memcg_css,
  188. gfp_t gfp);
  189. void __inode_attach_wb(struct inode *inode, struct page *page);
  190. void wb_memcg_offline(struct mem_cgroup *memcg);
  191. void wb_blkcg_offline(struct blkcg *blkcg);
  192. int inode_congested(struct inode *inode, int cong_bits);
  193. /**
  194. * inode_cgwb_enabled - test whether cgroup writeback is enabled on an inode
  195. * @inode: inode of interest
  196. *
  197. * cgroup writeback requires support from both the bdi and filesystem.
  198. * Test whether @inode has both.
  199. */
  200. static inline bool inode_cgwb_enabled(struct inode *inode)
  201. {
  202. struct backing_dev_info *bdi = inode_to_bdi(inode);
  203. return bdi_cap_account_dirty(bdi) &&
  204. (bdi->capabilities & BDI_CAP_CGROUP_WRITEBACK) &&
  205. (inode->i_sb->s_type->fs_flags & FS_CGROUP_WRITEBACK);
  206. }
  207. /**
  208. * wb_tryget - try to increment a wb's refcount
  209. * @wb: bdi_writeback to get
  210. */
  211. static inline bool wb_tryget(struct bdi_writeback *wb)
  212. {
  213. if (wb != &wb->bdi->wb)
  214. return percpu_ref_tryget(&wb->refcnt);
  215. return true;
  216. }
  217. /**
  218. * wb_get - increment a wb's refcount
  219. * @wb: bdi_writeback to get
  220. */
  221. static inline void wb_get(struct bdi_writeback *wb)
  222. {
  223. if (wb != &wb->bdi->wb)
  224. percpu_ref_get(&wb->refcnt);
  225. }
  226. /**
  227. * wb_put - decrement a wb's refcount
  228. * @wb: bdi_writeback to put
  229. */
  230. static inline void wb_put(struct bdi_writeback *wb)
  231. {
  232. if (wb != &wb->bdi->wb)
  233. percpu_ref_put(&wb->refcnt);
  234. }
  235. /**
  236. * wb_find_current - find wb for %current on a bdi
  237. * @bdi: bdi of interest
  238. *
  239. * Find the wb of @bdi which matches both the memcg and blkcg of %current.
  240. * Must be called under rcu_read_lock() which protects the returend wb.
  241. * NULL if not found.
  242. */
  243. static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
  244. {
  245. struct cgroup_subsys_state *memcg_css;
  246. struct bdi_writeback *wb;
  247. memcg_css = task_css(current, memory_cgrp_id);
  248. if (!memcg_css->parent)
  249. return &bdi->wb;
  250. wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
  251. /*
  252. * %current's blkcg equals the effective blkcg of its memcg. No
  253. * need to use the relatively expensive cgroup_get_e_css().
  254. */
  255. if (likely(wb && wb->blkcg_css == task_css(current, blkio_cgrp_id)))
  256. return wb;
  257. return NULL;
  258. }
  259. /**
  260. * wb_get_create_current - get or create wb for %current on a bdi
  261. * @bdi: bdi of interest
  262. * @gfp: allocation mask
  263. *
  264. * Equivalent to wb_get_create() on %current's memcg. This function is
  265. * called from a relatively hot path and optimizes the common cases using
  266. * wb_find_current().
  267. */
  268. static inline struct bdi_writeback *
  269. wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
  270. {
  271. struct bdi_writeback *wb;
  272. rcu_read_lock();
  273. wb = wb_find_current(bdi);
  274. if (wb && unlikely(!wb_tryget(wb)))
  275. wb = NULL;
  276. rcu_read_unlock();
  277. if (unlikely(!wb)) {
  278. struct cgroup_subsys_state *memcg_css;
  279. memcg_css = task_get_css(current, memory_cgrp_id);
  280. wb = wb_get_create(bdi, memcg_css, gfp);
  281. css_put(memcg_css);
  282. }
  283. return wb;
  284. }
  285. /**
  286. * inode_attach_wb - associate an inode with its wb
  287. * @inode: inode of interest
  288. * @page: page being dirtied (may be NULL)
  289. *
  290. * If @inode doesn't have its wb, associate it with the wb matching the
  291. * memcg of @page or, if @page is NULL, %current. May be called w/ or w/o
  292. * @inode->i_lock.
  293. */
  294. static inline void inode_attach_wb(struct inode *inode, struct page *page)
  295. {
  296. if (!inode->i_wb)
  297. __inode_attach_wb(inode, page);
  298. }
  299. /**
  300. * inode_detach_wb - disassociate an inode from its wb
  301. * @inode: inode of interest
  302. *
  303. * @inode is being freed. Detach from its wb.
  304. */
  305. static inline void inode_detach_wb(struct inode *inode)
  306. {
  307. if (inode->i_wb) {
  308. wb_put(inode->i_wb);
  309. inode->i_wb = NULL;
  310. }
  311. }
  312. /**
  313. * inode_to_wb - determine the wb of an inode
  314. * @inode: inode of interest
  315. *
  316. * Returns the wb @inode is currently associated with.
  317. */
  318. static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
  319. {
  320. return inode->i_wb;
  321. }
  322. #else /* CONFIG_CGROUP_WRITEBACK */
  323. static inline bool inode_cgwb_enabled(struct inode *inode)
  324. {
  325. return false;
  326. }
  327. static inline struct bdi_writeback_congested *
  328. wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp)
  329. {
  330. return bdi->wb.congested;
  331. }
  332. static inline void wb_congested_put(struct bdi_writeback_congested *congested)
  333. {
  334. }
  335. static inline bool wb_tryget(struct bdi_writeback *wb)
  336. {
  337. return true;
  338. }
  339. static inline void wb_get(struct bdi_writeback *wb)
  340. {
  341. }
  342. static inline void wb_put(struct bdi_writeback *wb)
  343. {
  344. }
  345. static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
  346. {
  347. return &bdi->wb;
  348. }
  349. static inline struct bdi_writeback *
  350. wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
  351. {
  352. return &bdi->wb;
  353. }
  354. static inline void inode_attach_wb(struct inode *inode, struct page *page)
  355. {
  356. }
  357. static inline void inode_detach_wb(struct inode *inode)
  358. {
  359. }
  360. static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
  361. {
  362. return &inode_to_bdi(inode)->wb;
  363. }
  364. static inline void wb_memcg_offline(struct mem_cgroup *memcg)
  365. {
  366. }
  367. static inline void wb_blkcg_offline(struct blkcg *blkcg)
  368. {
  369. }
  370. static inline int inode_congested(struct inode *inode, int cong_bits)
  371. {
  372. return wb_congested(&inode_to_bdi(inode)->wb, cong_bits);
  373. }
  374. #endif /* CONFIG_CGROUP_WRITEBACK */
  375. static inline int inode_read_congested(struct inode *inode)
  376. {
  377. return inode_congested(inode, 1 << WB_sync_congested);
  378. }
  379. static inline int inode_write_congested(struct inode *inode)
  380. {
  381. return inode_congested(inode, 1 << WB_async_congested);
  382. }
  383. static inline int inode_rw_congested(struct inode *inode)
  384. {
  385. return inode_congested(inode, (1 << WB_sync_congested) |
  386. (1 << WB_async_congested));
  387. }
  388. static inline int bdi_congested(struct backing_dev_info *bdi, int cong_bits)
  389. {
  390. return wb_congested(&bdi->wb, cong_bits);
  391. }
  392. static inline int bdi_read_congested(struct backing_dev_info *bdi)
  393. {
  394. return bdi_congested(bdi, 1 << WB_sync_congested);
  395. }
  396. static inline int bdi_write_congested(struct backing_dev_info *bdi)
  397. {
  398. return bdi_congested(bdi, 1 << WB_async_congested);
  399. }
  400. static inline int bdi_rw_congested(struct backing_dev_info *bdi)
  401. {
  402. return bdi_congested(bdi, (1 << WB_sync_congested) |
  403. (1 << WB_async_congested));
  404. }
  405. #endif /* _LINUX_BACKING_DEV_H */