2 * include/linux/backing-dev.h
4 * low-level device information and state which is propagated up through
8 #ifndef _LINUX_BACKING_DEV_H
9 #define _LINUX_BACKING_DEV_H
11 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/blkdev.h>
15 #include <linux/writeback.h>
16 #include <linux/blk-cgroup.h>
17 #include <linux/backing-dev-defs.h>
19 int __must_check bdi_init(struct backing_dev_info *bdi);
20 void bdi_destroy(struct backing_dev_info *bdi);
23 int bdi_register(struct backing_dev_info *bdi, struct device *parent,
24 const char *fmt, ...);
25 int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev);
26 int __must_check bdi_setup_and_register(struct backing_dev_info *, char *);
27 void wb_start_writeback(struct bdi_writeback *wb, long nr_pages,
28 bool range_cyclic, enum wb_reason reason);
29 void wb_start_background_writeback(struct bdi_writeback *wb);
30 void wb_workfn(struct work_struct *work);
31 void wb_wakeup_delayed(struct bdi_writeback *wb);
33 extern spinlock_t bdi_lock;
34 extern struct list_head bdi_list;
36 extern struct workqueue_struct *bdi_wq;
38 static inline bool wb_has_dirty_io(struct bdi_writeback *wb)
40 return test_bit(WB_has_dirty_io, &wb->state);
43 static inline bool bdi_has_dirty_io(struct backing_dev_info *bdi)
46 * @bdi->tot_write_bandwidth is guaranteed to be > 0 if there are
47 * any dirty wbs. See wb_update_write_bandwidth().
49 return atomic_long_read(&bdi->tot_write_bandwidth);
52 static inline void __add_wb_stat(struct bdi_writeback *wb,
53 enum wb_stat_item item, s64 amount)
55 __percpu_counter_add(&wb->stat[item], amount, WB_STAT_BATCH);
58 static inline void __inc_wb_stat(struct bdi_writeback *wb,
59 enum wb_stat_item item)
61 __add_wb_stat(wb, item, 1);
64 static inline void inc_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
68 local_irq_save(flags);
69 __inc_wb_stat(wb, item);
70 local_irq_restore(flags);
73 static inline void __dec_wb_stat(struct bdi_writeback *wb,
74 enum wb_stat_item item)
76 __add_wb_stat(wb, item, -1);
79 static inline void dec_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
83 local_irq_save(flags);
84 __dec_wb_stat(wb, item);
85 local_irq_restore(flags);
88 static inline s64 wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
90 return percpu_counter_read_positive(&wb->stat[item]);
93 static inline s64 __wb_stat_sum(struct bdi_writeback *wb,
94 enum wb_stat_item item)
96 return percpu_counter_sum_positive(&wb->stat[item]);
99 static inline s64 wb_stat_sum(struct bdi_writeback *wb, enum wb_stat_item item)
104 local_irq_save(flags);
105 sum = __wb_stat_sum(wb, item);
106 local_irq_restore(flags);
111 extern void wb_writeout_inc(struct bdi_writeback *wb);
114 * maximal error of a stat counter.
116 static inline unsigned long wb_stat_error(struct bdi_writeback *wb)
119 return nr_cpu_ids * WB_STAT_BATCH;
125 int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio);
126 int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ratio);
129 * Flags in backing_dev_info::capability
131 * The first three flags control whether dirty pages will contribute to the
132 * VM's accounting and whether writepages() should be called for dirty pages
133 * (something that would not, for example, be appropriate for ramfs)
135 * WARNING: these flags are closely related and should not normally be
136 * used separately. The BDI_CAP_NO_ACCT_AND_WRITEBACK combines these
137 * three flags into a single convenience macro.
139 * BDI_CAP_NO_ACCT_DIRTY: Dirty pages shouldn't contribute to accounting
140 * BDI_CAP_NO_WRITEBACK: Don't write pages back
141 * BDI_CAP_NO_ACCT_WB: Don't automatically account writeback pages
142 * BDI_CAP_STRICTLIMIT: Keep number of dirty pages below bdi threshold.
144 * BDI_CAP_CGROUP_WRITEBACK: Supports cgroup-aware writeback.
146 #define BDI_CAP_NO_ACCT_DIRTY 0x00000001
147 #define BDI_CAP_NO_WRITEBACK 0x00000002
148 #define BDI_CAP_NO_ACCT_WB 0x00000004
149 #define BDI_CAP_STABLE_WRITES 0x00000008
150 #define BDI_CAP_STRICTLIMIT 0x00000010
151 #define BDI_CAP_CGROUP_WRITEBACK 0x00000020
153 #define BDI_CAP_NO_ACCT_AND_WRITEBACK \
154 (BDI_CAP_NO_WRITEBACK | BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_ACCT_WB)
156 extern struct backing_dev_info noop_backing_dev_info;
159 * writeback_in_progress - determine whether there is writeback in progress
160 * @wb: bdi_writeback of interest
162 * Determine whether there is writeback waiting to be handled against a
165 static inline bool writeback_in_progress(struct bdi_writeback *wb)
167 return test_bit(WB_writeback_running, &wb->state);
170 static inline struct backing_dev_info *inode_to_bdi(struct inode *inode)
172 struct super_block *sb;
175 return &noop_backing_dev_info;
179 if (sb_is_blkdev_sb(sb))
180 return blk_get_backing_dev_info(I_BDEV(inode));
185 static inline int wb_congested(struct bdi_writeback *wb, int cong_bits)
187 struct backing_dev_info *bdi = wb->bdi;
189 if (bdi->congested_fn)
190 return bdi->congested_fn(bdi->congested_data, cong_bits);
191 return wb->congested->state & cong_bits;
194 long congestion_wait(int sync, long timeout);
195 long wait_iff_congested(struct zone *zone, int sync, long timeout);
196 int pdflush_proc_obsolete(struct ctl_table *table, int write,
197 void __user *buffer, size_t *lenp, loff_t *ppos);
199 static inline bool bdi_cap_stable_pages_required(struct backing_dev_info *bdi)
201 return bdi->capabilities & BDI_CAP_STABLE_WRITES;
204 static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi)
206 return !(bdi->capabilities & BDI_CAP_NO_WRITEBACK);
209 static inline bool bdi_cap_account_dirty(struct backing_dev_info *bdi)
211 return !(bdi->capabilities & BDI_CAP_NO_ACCT_DIRTY);
214 static inline bool bdi_cap_account_writeback(struct backing_dev_info *bdi)
216 /* Paranoia: BDI_CAP_NO_WRITEBACK implies BDI_CAP_NO_ACCT_WB */
217 return !(bdi->capabilities & (BDI_CAP_NO_ACCT_WB |
218 BDI_CAP_NO_WRITEBACK));
221 static inline bool mapping_cap_writeback_dirty(struct address_space *mapping)
223 return bdi_cap_writeback_dirty(inode_to_bdi(mapping->host));
226 static inline bool mapping_cap_account_dirty(struct address_space *mapping)
228 return bdi_cap_account_dirty(inode_to_bdi(mapping->host));
231 static inline int bdi_sched_wait(void *word)
237 #ifdef CONFIG_CGROUP_WRITEBACK
239 struct bdi_writeback_congested *
240 wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp);
241 void wb_congested_put(struct bdi_writeback_congested *congested);
242 struct bdi_writeback *wb_get_create(struct backing_dev_info *bdi,
243 struct cgroup_subsys_state *memcg_css,
245 void wb_memcg_offline(struct mem_cgroup *memcg);
246 void wb_blkcg_offline(struct blkcg *blkcg);
247 int inode_congested(struct inode *inode, int cong_bits);
250 * inode_cgwb_enabled - test whether cgroup writeback is enabled on an inode
251 * @inode: inode of interest
253 * cgroup writeback requires support from both the bdi and filesystem.
254 * Test whether @inode has both.
256 static inline bool inode_cgwb_enabled(struct inode *inode)
258 struct backing_dev_info *bdi = inode_to_bdi(inode);
260 return bdi_cap_account_dirty(bdi) &&
261 (bdi->capabilities & BDI_CAP_CGROUP_WRITEBACK) &&
262 (inode->i_sb->s_iflags & SB_I_CGROUPWB);
266 * wb_find_current - find wb for %current on a bdi
267 * @bdi: bdi of interest
269 * Find the wb of @bdi which matches both the memcg and blkcg of %current.
270 * Must be called under rcu_read_lock() which protects the returend wb.
273 static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
275 struct cgroup_subsys_state *memcg_css;
276 struct bdi_writeback *wb;
278 memcg_css = task_css(current, memory_cgrp_id);
279 if (!memcg_css->parent)
282 wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
285 * %current's blkcg equals the effective blkcg of its memcg. No
286 * need to use the relatively expensive cgroup_get_e_css().
288 if (likely(wb && wb->blkcg_css == task_css(current, blkio_cgrp_id)))
294 * wb_get_create_current - get or create wb for %current on a bdi
295 * @bdi: bdi of interest
296 * @gfp: allocation mask
298 * Equivalent to wb_get_create() on %current's memcg. This function is
299 * called from a relatively hot path and optimizes the common cases using
302 static inline struct bdi_writeback *
303 wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
305 struct bdi_writeback *wb;
308 wb = wb_find_current(bdi);
309 if (wb && unlikely(!wb_tryget(wb)))
314 struct cgroup_subsys_state *memcg_css;
316 memcg_css = task_get_css(current, memory_cgrp_id);
317 wb = wb_get_create(bdi, memcg_css, gfp);
324 * inode_to_wb_is_valid - test whether an inode has a wb associated
325 * @inode: inode of interest
327 * Returns %true if @inode has a wb associated. May be called without any
330 static inline bool inode_to_wb_is_valid(struct inode *inode)
336 * inode_to_wb - determine the wb of an inode
337 * @inode: inode of interest
339 * Returns the wb @inode is currently associated with. The caller must be
340 * holding either @inode->i_lock, @inode->i_mapping->tree_lock, or the
341 * associated wb's list_lock.
343 static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
345 #ifdef CONFIG_LOCKDEP
346 WARN_ON_ONCE(debug_locks &&
347 (!lockdep_is_held(&inode->i_lock) &&
348 !lockdep_is_held(&inode->i_mapping->tree_lock) &&
349 !lockdep_is_held(&inode->i_wb->list_lock)));
355 * unlocked_inode_to_wb_begin - begin unlocked inode wb access transaction
356 * @inode: target inode
357 * @lockedp: temp bool output param, to be passed to the end function
359 * The caller wants to access the wb associated with @inode but isn't
360 * holding inode->i_lock, mapping->tree_lock or wb->list_lock. This
361 * function determines the wb associated with @inode and ensures that the
362 * association doesn't change until the transaction is finished with
363 * unlocked_inode_to_wb_end().
365 * The caller must call unlocked_inode_to_wb_end() with *@lockdep
366 * afterwards and can't sleep during transaction. IRQ may or may not be
367 * disabled on return.
369 static inline struct bdi_writeback *
370 unlocked_inode_to_wb_begin(struct inode *inode, bool *lockedp)
375 * Paired with store_release in inode_switch_wb_work_fn() and
376 * ensures that we see the new wb if we see cleared I_WB_SWITCH.
378 *lockedp = smp_load_acquire(&inode->i_state) & I_WB_SWITCH;
380 if (unlikely(*lockedp))
381 spin_lock_irq(&inode->i_mapping->tree_lock);
384 * Protected by either !I_WB_SWITCH + rcu_read_lock() or tree_lock.
385 * inode_to_wb() will bark. Deref directly.
391 * unlocked_inode_to_wb_end - end inode wb access transaction
392 * @inode: target inode
393 * @locked: *@lockedp from unlocked_inode_to_wb_begin()
395 static inline void unlocked_inode_to_wb_end(struct inode *inode, bool locked)
397 if (unlikely(locked))
398 spin_unlock_irq(&inode->i_mapping->tree_lock);
405 struct radix_tree_iter tree_iter;
409 static inline struct bdi_writeback *__wb_iter_next(struct wb_iter *iter,
410 struct backing_dev_info *bdi)
412 struct radix_tree_iter *titer = &iter->tree_iter;
414 WARN_ON_ONCE(!rcu_read_lock_held());
416 if (iter->start_blkcg_id >= 0) {
417 iter->slot = radix_tree_iter_init(titer, iter->start_blkcg_id);
418 iter->start_blkcg_id = -1;
420 iter->slot = radix_tree_next_slot(iter->slot, titer, 0);
424 iter->slot = radix_tree_next_chunk(&bdi->cgwb_tree, titer, 0);
430 static inline struct bdi_writeback *__wb_iter_init(struct wb_iter *iter,
431 struct backing_dev_info *bdi,
434 iter->start_blkcg_id = start_blkcg_id;
437 return __wb_iter_next(iter, bdi);
443 * bdi_for_each_wb - walk all wb's of a bdi in ascending blkcg ID order
444 * @wb_cur: cursor struct bdi_writeback pointer
445 * @bdi: bdi to walk wb's of
446 * @iter: pointer to struct wb_iter to be used as iteration buffer
447 * @start_blkcg_id: blkcg ID to start iteration from
449 * Iterate @wb_cur through the wb's (bdi_writeback's) of @bdi in ascending
450 * blkcg ID order starting from @start_blkcg_id. @iter is struct wb_iter
451 * to be used as temp storage during iteration. rcu_read_lock() must be
452 * held throughout iteration.
454 #define bdi_for_each_wb(wb_cur, bdi, iter, start_blkcg_id) \
455 for ((wb_cur) = __wb_iter_init(iter, bdi, start_blkcg_id); \
456 (wb_cur); (wb_cur) = __wb_iter_next(iter, bdi))
458 #else /* CONFIG_CGROUP_WRITEBACK */
460 static inline bool inode_cgwb_enabled(struct inode *inode)
465 static inline struct bdi_writeback_congested *
466 wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp)
468 return bdi->wb.congested;
471 static inline void wb_congested_put(struct bdi_writeback_congested *congested)
475 static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
480 static inline struct bdi_writeback *
481 wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
486 static inline bool inode_to_wb_is_valid(struct inode *inode)
491 static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
493 return &inode_to_bdi(inode)->wb;
496 static inline struct bdi_writeback *
497 unlocked_inode_to_wb_begin(struct inode *inode, bool *lockedp)
499 return inode_to_wb(inode);
502 static inline void unlocked_inode_to_wb_end(struct inode *inode, bool locked)
506 static inline void wb_memcg_offline(struct mem_cgroup *memcg)
510 static inline void wb_blkcg_offline(struct blkcg *blkcg)
518 #define bdi_for_each_wb(wb_cur, bdi, iter, start_blkcg_id) \
519 for ((iter)->next_id = (start_blkcg_id); \
520 ({ (wb_cur) = !(iter)->next_id++ ? &(bdi)->wb : NULL; }); )
522 static inline int inode_congested(struct inode *inode, int cong_bits)
524 return wb_congested(&inode_to_bdi(inode)->wb, cong_bits);
527 #endif /* CONFIG_CGROUP_WRITEBACK */
529 static inline int inode_read_congested(struct inode *inode)
531 return inode_congested(inode, 1 << WB_sync_congested);
534 static inline int inode_write_congested(struct inode *inode)
536 return inode_congested(inode, 1 << WB_async_congested);
539 static inline int inode_rw_congested(struct inode *inode)
541 return inode_congested(inode, (1 << WB_sync_congested) |
542 (1 << WB_async_congested));
545 static inline int bdi_congested(struct backing_dev_info *bdi, int cong_bits)
547 return wb_congested(&bdi->wb, cong_bits);
550 static inline int bdi_read_congested(struct backing_dev_info *bdi)
552 return bdi_congested(bdi, 1 << WB_sync_congested);
555 static inline int bdi_write_congested(struct backing_dev_info *bdi)
557 return bdi_congested(bdi, 1 << WB_async_congested);
560 static inline int bdi_rw_congested(struct backing_dev_info *bdi)
562 return bdi_congested(bdi, (1 << WB_sync_congested) |
563 (1 << WB_async_congested));
566 #endif /* _LINUX_BACKING_DEV_H */