backing-dev.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. #include <linux/slab.h>
  17. int __must_check bdi_init(struct backing_dev_info *bdi);
  18. void bdi_exit(struct backing_dev_info *bdi);
  19. __printf(3, 4)
  20. int bdi_register(struct backing_dev_info *bdi, struct device *parent,
  21. const char *fmt, ...);
  22. int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev);
  23. void bdi_unregister(struct backing_dev_info *bdi);
  24. int __must_check bdi_setup_and_register(struct backing_dev_info *, char *);
  25. void bdi_destroy(struct backing_dev_info *bdi);
  26. void wb_start_writeback(struct bdi_writeback *wb, long nr_pages,
  27. bool range_cyclic, enum wb_reason reason);
  28. void wb_start_background_writeback(struct bdi_writeback *wb);
  29. void wb_workfn(struct work_struct *work);
  30. void wb_wakeup_delayed(struct bdi_writeback *wb);
  31. extern spinlock_t bdi_lock;
  32. extern struct list_head bdi_list;
  33. extern struct workqueue_struct *bdi_wq;
  34. static inline bool wb_has_dirty_io(struct bdi_writeback *wb)
  35. {
  36. return test_bit(WB_has_dirty_io, &wb->state);
  37. }
  38. static inline bool bdi_has_dirty_io(struct backing_dev_info *bdi)
  39. {
  40. /*
  41. * @bdi->tot_write_bandwidth is guaranteed to be > 0 if there are
  42. * any dirty wbs. See wb_update_write_bandwidth().
  43. */
  44. return atomic_long_read(&bdi->tot_write_bandwidth);
  45. }
  46. static inline void __add_wb_stat(struct bdi_writeback *wb,
  47. enum wb_stat_item item, s64 amount)
  48. {
  49. __percpu_counter_add(&wb->stat[item], amount, WB_STAT_BATCH);
  50. }
  51. static inline void __inc_wb_stat(struct bdi_writeback *wb,
  52. enum wb_stat_item item)
  53. {
  54. __add_wb_stat(wb, item, 1);
  55. }
  56. static inline void inc_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
  57. {
  58. unsigned long flags;
  59. local_irq_save(flags);
  60. __inc_wb_stat(wb, item);
  61. local_irq_restore(flags);
  62. }
  63. static inline void __dec_wb_stat(struct bdi_writeback *wb,
  64. enum wb_stat_item item)
  65. {
  66. __add_wb_stat(wb, item, -1);
  67. }
  68. static inline void dec_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
  69. {
  70. unsigned long flags;
  71. local_irq_save(flags);
  72. __dec_wb_stat(wb, item);
  73. local_irq_restore(flags);
  74. }
  75. static inline s64 wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
  76. {
  77. return percpu_counter_read_positive(&wb->stat[item]);
  78. }
  79. static inline s64 __wb_stat_sum(struct bdi_writeback *wb,
  80. enum wb_stat_item item)
  81. {
  82. return percpu_counter_sum_positive(&wb->stat[item]);
  83. }
  84. static inline s64 wb_stat_sum(struct bdi_writeback *wb, enum wb_stat_item item)
  85. {
  86. s64 sum;
  87. unsigned long flags;
  88. local_irq_save(flags);
  89. sum = __wb_stat_sum(wb, item);
  90. local_irq_restore(flags);
  91. return sum;
  92. }
  93. extern void wb_writeout_inc(struct bdi_writeback *wb);
  94. /*
  95. * maximal error of a stat counter.
  96. */
  97. static inline unsigned long wb_stat_error(struct bdi_writeback *wb)
  98. {
  99. #ifdef CONFIG_SMP
  100. return nr_cpu_ids * WB_STAT_BATCH;
  101. #else
  102. return 1;
  103. #endif
  104. }
  105. int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio);
  106. int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ratio);
  107. /*
  108. * Flags in backing_dev_info::capability
  109. *
  110. * The first three flags control whether dirty pages will contribute to the
  111. * VM's accounting and whether writepages() should be called for dirty pages
  112. * (something that would not, for example, be appropriate for ramfs)
  113. *
  114. * WARNING: these flags are closely related and should not normally be
  115. * used separately. The BDI_CAP_NO_ACCT_AND_WRITEBACK combines these
  116. * three flags into a single convenience macro.
  117. *
  118. * BDI_CAP_NO_ACCT_DIRTY: Dirty pages shouldn't contribute to accounting
  119. * BDI_CAP_NO_WRITEBACK: Don't write pages back
  120. * BDI_CAP_NO_ACCT_WB: Don't automatically account writeback pages
  121. * BDI_CAP_STRICTLIMIT: Keep number of dirty pages below bdi threshold.
  122. *
  123. * BDI_CAP_CGROUP_WRITEBACK: Supports cgroup-aware writeback.
  124. */
  125. #define BDI_CAP_NO_ACCT_DIRTY 0x00000001
  126. #define BDI_CAP_NO_WRITEBACK 0x00000002
  127. #define BDI_CAP_NO_ACCT_WB 0x00000004
  128. #define BDI_CAP_STABLE_WRITES 0x00000008
  129. #define BDI_CAP_STRICTLIMIT 0x00000010
  130. #define BDI_CAP_CGROUP_WRITEBACK 0x00000020
  131. #define BDI_CAP_NO_ACCT_AND_WRITEBACK \
  132. (BDI_CAP_NO_WRITEBACK | BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_ACCT_WB)
  133. extern struct backing_dev_info noop_backing_dev_info;
  134. /**
  135. * writeback_in_progress - determine whether there is writeback in progress
  136. * @wb: bdi_writeback of interest
  137. *
  138. * Determine whether there is writeback waiting to be handled against a
  139. * bdi_writeback.
  140. */
  141. static inline bool writeback_in_progress(struct bdi_writeback *wb)
  142. {
  143. return test_bit(WB_writeback_running, &wb->state);
  144. }
  145. static inline struct backing_dev_info *inode_to_bdi(struct inode *inode)
  146. {
  147. struct super_block *sb;
  148. if (!inode)
  149. return &noop_backing_dev_info;
  150. sb = inode->i_sb;
  151. #ifdef CONFIG_BLOCK
  152. if (sb_is_blkdev_sb(sb))
  153. return blk_get_backing_dev_info(I_BDEV(inode));
  154. #endif
  155. return sb->s_bdi;
  156. }
  157. static inline int wb_congested(struct bdi_writeback *wb, int cong_bits)
  158. {
  159. struct backing_dev_info *bdi = wb->bdi;
  160. if (bdi->congested_fn)
  161. return bdi->congested_fn(bdi->congested_data, cong_bits);
  162. return wb->congested->state & cong_bits;
  163. }
  164. long congestion_wait(int sync, long timeout);
  165. long wait_iff_congested(struct pglist_data *pgdat, int sync, long timeout);
  166. int pdflush_proc_obsolete(struct ctl_table *table, int write,
  167. void __user *buffer, size_t *lenp, loff_t *ppos);
  168. static inline bool bdi_cap_stable_pages_required(struct backing_dev_info *bdi)
  169. {
  170. return bdi->capabilities & BDI_CAP_STABLE_WRITES;
  171. }
  172. static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi)
  173. {
  174. return !(bdi->capabilities & BDI_CAP_NO_WRITEBACK);
  175. }
  176. static inline bool bdi_cap_account_dirty(struct backing_dev_info *bdi)
  177. {
  178. return !(bdi->capabilities & BDI_CAP_NO_ACCT_DIRTY);
  179. }
  180. static inline bool bdi_cap_account_writeback(struct backing_dev_info *bdi)
  181. {
  182. /* Paranoia: BDI_CAP_NO_WRITEBACK implies BDI_CAP_NO_ACCT_WB */
  183. return !(bdi->capabilities & (BDI_CAP_NO_ACCT_WB |
  184. BDI_CAP_NO_WRITEBACK));
  185. }
  186. static inline bool mapping_cap_writeback_dirty(struct address_space *mapping)
  187. {
  188. return bdi_cap_writeback_dirty(inode_to_bdi(mapping->host));
  189. }
  190. static inline bool mapping_cap_account_dirty(struct address_space *mapping)
  191. {
  192. return bdi_cap_account_dirty(inode_to_bdi(mapping->host));
  193. }
  194. static inline int bdi_sched_wait(void *word)
  195. {
  196. schedule();
  197. return 0;
  198. }
  199. #ifdef CONFIG_CGROUP_WRITEBACK
  200. struct bdi_writeback_congested *
  201. wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp);
  202. void wb_congested_put(struct bdi_writeback_congested *congested);
  203. struct bdi_writeback *wb_get_create(struct backing_dev_info *bdi,
  204. struct cgroup_subsys_state *memcg_css,
  205. gfp_t gfp);
  206. void wb_memcg_offline(struct mem_cgroup *memcg);
  207. void wb_blkcg_offline(struct blkcg *blkcg);
  208. int inode_congested(struct inode *inode, int cong_bits);
  209. /**
  210. * inode_cgwb_enabled - test whether cgroup writeback is enabled on an inode
  211. * @inode: inode of interest
  212. *
  213. * cgroup writeback requires support from both the bdi and filesystem.
  214. * Also, both memcg and iocg have to be on the default hierarchy. Test
  215. * whether all conditions are met.
  216. *
  217. * Note that the test result may change dynamically on the same inode
  218. * depending on how memcg and iocg are configured.
  219. */
  220. static inline bool inode_cgwb_enabled(struct inode *inode)
  221. {
  222. struct backing_dev_info *bdi = inode_to_bdi(inode);
  223. return cgroup_subsys_on_dfl(memory_cgrp_subsys) &&
  224. cgroup_subsys_on_dfl(io_cgrp_subsys) &&
  225. bdi_cap_account_dirty(bdi) &&
  226. (bdi->capabilities & BDI_CAP_CGROUP_WRITEBACK) &&
  227. (inode->i_sb->s_iflags & SB_I_CGROUPWB);
  228. }
  229. /**
  230. * wb_find_current - find wb for %current on a bdi
  231. * @bdi: bdi of interest
  232. *
  233. * Find the wb of @bdi which matches both the memcg and blkcg of %current.
  234. * Must be called under rcu_read_lock() which protects the returend wb.
  235. * NULL if not found.
  236. */
  237. static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
  238. {
  239. struct cgroup_subsys_state *memcg_css;
  240. struct bdi_writeback *wb;
  241. memcg_css = task_css(current, memory_cgrp_id);
  242. if (!memcg_css->parent)
  243. return &bdi->wb;
  244. wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
  245. /*
  246. * %current's blkcg equals the effective blkcg of its memcg. No
  247. * need to use the relatively expensive cgroup_get_e_css().
  248. */
  249. if (likely(wb && wb->blkcg_css == task_css(current, io_cgrp_id)))
  250. return wb;
  251. return NULL;
  252. }
  253. /**
  254. * wb_get_create_current - get or create wb for %current on a bdi
  255. * @bdi: bdi of interest
  256. * @gfp: allocation mask
  257. *
  258. * Equivalent to wb_get_create() on %current's memcg. This function is
  259. * called from a relatively hot path and optimizes the common cases using
  260. * wb_find_current().
  261. */
  262. static inline struct bdi_writeback *
  263. wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
  264. {
  265. struct bdi_writeback *wb;
  266. rcu_read_lock();
  267. wb = wb_find_current(bdi);
  268. if (wb && unlikely(!wb_tryget(wb)))
  269. wb = NULL;
  270. rcu_read_unlock();
  271. if (unlikely(!wb)) {
  272. struct cgroup_subsys_state *memcg_css;
  273. memcg_css = task_get_css(current, memory_cgrp_id);
  274. wb = wb_get_create(bdi, memcg_css, gfp);
  275. css_put(memcg_css);
  276. }
  277. return wb;
  278. }
  279. /**
  280. * inode_to_wb_is_valid - test whether an inode has a wb associated
  281. * @inode: inode of interest
  282. *
  283. * Returns %true if @inode has a wb associated. May be called without any
  284. * locking.
  285. */
  286. static inline bool inode_to_wb_is_valid(struct inode *inode)
  287. {
  288. return inode->i_wb;
  289. }
  290. /**
  291. * inode_to_wb - determine the wb of an inode
  292. * @inode: inode of interest
  293. *
  294. * Returns the wb @inode is currently associated with. The caller must be
  295. * holding either @inode->i_lock, @inode->i_mapping->tree_lock, or the
  296. * associated wb's list_lock.
  297. */
  298. static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
  299. {
  300. #ifdef CONFIG_LOCKDEP
  301. WARN_ON_ONCE(debug_locks &&
  302. (!lockdep_is_held(&inode->i_lock) &&
  303. !lockdep_is_held(&inode->i_mapping->tree_lock) &&
  304. !lockdep_is_held(&inode->i_wb->list_lock)));
  305. #endif
  306. return inode->i_wb;
  307. }
  308. /**
  309. * unlocked_inode_to_wb_begin - begin unlocked inode wb access transaction
  310. * @inode: target inode
  311. * @lockedp: temp bool output param, to be passed to the end function
  312. *
  313. * The caller wants to access the wb associated with @inode but isn't
  314. * holding inode->i_lock, mapping->tree_lock or wb->list_lock. This
  315. * function determines the wb associated with @inode and ensures that the
  316. * association doesn't change until the transaction is finished with
  317. * unlocked_inode_to_wb_end().
  318. *
  319. * The caller must call unlocked_inode_to_wb_end() with *@lockdep
  320. * afterwards and can't sleep during transaction. IRQ may or may not be
  321. * disabled on return.
  322. */
  323. static inline struct bdi_writeback *
  324. unlocked_inode_to_wb_begin(struct inode *inode, bool *lockedp)
  325. {
  326. rcu_read_lock();
  327. /*
  328. * Paired with store_release in inode_switch_wb_work_fn() and
  329. * ensures that we see the new wb if we see cleared I_WB_SWITCH.
  330. */
  331. *lockedp = smp_load_acquire(&inode->i_state) & I_WB_SWITCH;
  332. if (unlikely(*lockedp))
  333. spin_lock_irq(&inode->i_mapping->tree_lock);
  334. /*
  335. * Protected by either !I_WB_SWITCH + rcu_read_lock() or tree_lock.
  336. * inode_to_wb() will bark. Deref directly.
  337. */
  338. return inode->i_wb;
  339. }
  340. /**
  341. * unlocked_inode_to_wb_end - end inode wb access transaction
  342. * @inode: target inode
  343. * @locked: *@lockedp from unlocked_inode_to_wb_begin()
  344. */
  345. static inline void unlocked_inode_to_wb_end(struct inode *inode, bool locked)
  346. {
  347. if (unlikely(locked))
  348. spin_unlock_irq(&inode->i_mapping->tree_lock);
  349. rcu_read_unlock();
  350. }
  351. #else /* CONFIG_CGROUP_WRITEBACK */
  352. static inline bool inode_cgwb_enabled(struct inode *inode)
  353. {
  354. return false;
  355. }
  356. static inline struct bdi_writeback_congested *
  357. wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp)
  358. {
  359. atomic_inc(&bdi->wb_congested->refcnt);
  360. return bdi->wb_congested;
  361. }
  362. static inline void wb_congested_put(struct bdi_writeback_congested *congested)
  363. {
  364. if (atomic_dec_and_test(&congested->refcnt))
  365. kfree(congested);
  366. }
  367. static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
  368. {
  369. return &bdi->wb;
  370. }
  371. static inline struct bdi_writeback *
  372. wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
  373. {
  374. return &bdi->wb;
  375. }
  376. static inline bool inode_to_wb_is_valid(struct inode *inode)
  377. {
  378. return true;
  379. }
  380. static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
  381. {
  382. return &inode_to_bdi(inode)->wb;
  383. }
  384. static inline struct bdi_writeback *
  385. unlocked_inode_to_wb_begin(struct inode *inode, bool *lockedp)
  386. {
  387. return inode_to_wb(inode);
  388. }
  389. static inline void unlocked_inode_to_wb_end(struct inode *inode, bool locked)
  390. {
  391. }
  392. static inline void wb_memcg_offline(struct mem_cgroup *memcg)
  393. {
  394. }
  395. static inline void wb_blkcg_offline(struct blkcg *blkcg)
  396. {
  397. }
  398. static inline int inode_congested(struct inode *inode, int cong_bits)
  399. {
  400. return wb_congested(&inode_to_bdi(inode)->wb, cong_bits);
  401. }
  402. #endif /* CONFIG_CGROUP_WRITEBACK */
  403. static inline int inode_read_congested(struct inode *inode)
  404. {
  405. return inode_congested(inode, 1 << WB_sync_congested);
  406. }
  407. static inline int inode_write_congested(struct inode *inode)
  408. {
  409. return inode_congested(inode, 1 << WB_async_congested);
  410. }
  411. static inline int inode_rw_congested(struct inode *inode)
  412. {
  413. return inode_congested(inode, (1 << WB_sync_congested) |
  414. (1 << WB_async_congested));
  415. }
  416. static inline int bdi_congested(struct backing_dev_info *bdi, int cong_bits)
  417. {
  418. return wb_congested(&bdi->wb, cong_bits);
  419. }
  420. static inline int bdi_read_congested(struct backing_dev_info *bdi)
  421. {
  422. return bdi_congested(bdi, 1 << WB_sync_congested);
  423. }
  424. static inline int bdi_write_congested(struct backing_dev_info *bdi)
  425. {
  426. return bdi_congested(bdi, 1 << WB_async_congested);
  427. }
  428. static inline int bdi_rw_congested(struct backing_dev_info *bdi)
  429. {
  430. return bdi_congested(bdi, (1 << WB_sync_congested) |
  431. (1 << WB_async_congested));
  432. }
  433. #endif /* _LINUX_BACKING_DEV_H */