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 void bdi_unregister(struct backing_dev_info *bdi);
27 int __must_check bdi_setup_and_register(struct backing_dev_info *, char *);
28 void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
29 enum wb_reason reason);
30 void bdi_start_background_writeback(struct backing_dev_info *bdi);
31 void wb_workfn(struct work_struct *work);
32 int bdi_has_dirty_io(struct backing_dev_info *bdi);
33 void wb_wakeup_delayed(struct bdi_writeback *wb);
35 extern spinlock_t bdi_lock;
36 extern struct list_head bdi_list;
38 extern struct workqueue_struct *bdi_wq;
40 static inline int wb_has_dirty_io(struct bdi_writeback *wb)
42 return !list_empty(&wb->b_dirty) ||
43 !list_empty(&wb->b_io) ||
44 !list_empty(&wb->b_more_io);
47 static inline void __add_wb_stat(struct bdi_writeback *wb,
48 enum wb_stat_item item, s64 amount)
50 __percpu_counter_add(&wb->stat[item], amount, WB_STAT_BATCH);
53 static inline void __inc_wb_stat(struct bdi_writeback *wb,
54 enum wb_stat_item item)
56 __add_wb_stat(wb, item, 1);
59 static inline void inc_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
63 local_irq_save(flags);
64 __inc_wb_stat(wb, item);
65 local_irq_restore(flags);
68 static inline void __dec_wb_stat(struct bdi_writeback *wb,
69 enum wb_stat_item item)
71 __add_wb_stat(wb, item, -1);
74 static inline void dec_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
78 local_irq_save(flags);
79 __dec_wb_stat(wb, item);
80 local_irq_restore(flags);
83 static inline s64 wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
85 return percpu_counter_read_positive(&wb->stat[item]);
88 static inline s64 __wb_stat_sum(struct bdi_writeback *wb,
89 enum wb_stat_item item)
91 return percpu_counter_sum_positive(&wb->stat[item]);
94 static inline s64 wb_stat_sum(struct bdi_writeback *wb, enum wb_stat_item item)
99 local_irq_save(flags);
100 sum = __wb_stat_sum(wb, item);
101 local_irq_restore(flags);
106 extern void wb_writeout_inc(struct bdi_writeback *wb);
109 * maximal error of a stat counter.
111 static inline unsigned long wb_stat_error(struct bdi_writeback *wb)
114 return nr_cpu_ids * WB_STAT_BATCH;
120 int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio);
121 int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ratio);
124 * Flags in backing_dev_info::capability
126 * The first three flags control whether dirty pages will contribute to the
127 * VM's accounting and whether writepages() should be called for dirty pages
128 * (something that would not, for example, be appropriate for ramfs)
130 * WARNING: these flags are closely related and should not normally be
131 * used separately. The BDI_CAP_NO_ACCT_AND_WRITEBACK combines these
132 * three flags into a single convenience macro.
134 * BDI_CAP_NO_ACCT_DIRTY: Dirty pages shouldn't contribute to accounting
135 * BDI_CAP_NO_WRITEBACK: Don't write pages back
136 * BDI_CAP_NO_ACCT_WB: Don't automatically account writeback pages
137 * BDI_CAP_STRICTLIMIT: Keep number of dirty pages below bdi threshold.
139 * BDI_CAP_CGROUP_WRITEBACK: Supports cgroup-aware writeback.
141 #define BDI_CAP_NO_ACCT_DIRTY 0x00000001
142 #define BDI_CAP_NO_WRITEBACK 0x00000002
143 #define BDI_CAP_NO_ACCT_WB 0x00000004
144 #define BDI_CAP_STABLE_WRITES 0x00000008
145 #define BDI_CAP_STRICTLIMIT 0x00000010
146 #define BDI_CAP_CGROUP_WRITEBACK 0x00000020
148 #define BDI_CAP_NO_ACCT_AND_WRITEBACK \
149 (BDI_CAP_NO_WRITEBACK | BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_ACCT_WB)
151 extern struct backing_dev_info noop_backing_dev_info;
153 int writeback_in_progress(struct backing_dev_info *bdi);
155 static inline struct backing_dev_info *inode_to_bdi(struct inode *inode)
157 struct super_block *sb;
160 return &noop_backing_dev_info;
164 if (sb_is_blkdev_sb(sb))
165 return blk_get_backing_dev_info(I_BDEV(inode));
170 static inline int bdi_congested(struct backing_dev_info *bdi, int bdi_bits)
172 if (bdi->congested_fn)
173 return bdi->congested_fn(bdi->congested_data, bdi_bits);
174 return (bdi->wb.congested->state & bdi_bits);
177 static inline int bdi_read_congested(struct backing_dev_info *bdi)
179 return bdi_congested(bdi, 1 << WB_sync_congested);
182 static inline int bdi_write_congested(struct backing_dev_info *bdi)
184 return bdi_congested(bdi, 1 << WB_async_congested);
187 static inline int bdi_rw_congested(struct backing_dev_info *bdi)
189 return bdi_congested(bdi, (1 << WB_sync_congested) |
190 (1 << WB_async_congested));
193 long congestion_wait(int sync, long timeout);
194 long wait_iff_congested(struct zone *zone, int sync, long timeout);
195 int pdflush_proc_obsolete(struct ctl_table *table, int write,
196 void __user *buffer, size_t *lenp, loff_t *ppos);
198 static inline bool bdi_cap_stable_pages_required(struct backing_dev_info *bdi)
200 return bdi->capabilities & BDI_CAP_STABLE_WRITES;
203 static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi)
205 return !(bdi->capabilities & BDI_CAP_NO_WRITEBACK);
208 static inline bool bdi_cap_account_dirty(struct backing_dev_info *bdi)
210 return !(bdi->capabilities & BDI_CAP_NO_ACCT_DIRTY);
213 static inline bool bdi_cap_account_writeback(struct backing_dev_info *bdi)
215 /* Paranoia: BDI_CAP_NO_WRITEBACK implies BDI_CAP_NO_ACCT_WB */
216 return !(bdi->capabilities & (BDI_CAP_NO_ACCT_WB |
217 BDI_CAP_NO_WRITEBACK));
220 static inline bool mapping_cap_writeback_dirty(struct address_space *mapping)
222 return bdi_cap_writeback_dirty(inode_to_bdi(mapping->host));
225 static inline bool mapping_cap_account_dirty(struct address_space *mapping)
227 return bdi_cap_account_dirty(inode_to_bdi(mapping->host));
230 static inline int bdi_sched_wait(void *word)
236 #ifdef CONFIG_CGROUP_WRITEBACK
238 struct bdi_writeback_congested *
239 wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp);
240 void wb_congested_put(struct bdi_writeback_congested *congested);
241 struct bdi_writeback *wb_get_create(struct backing_dev_info *bdi,
242 struct cgroup_subsys_state *memcg_css,
244 void __inode_attach_wb(struct inode *inode, struct page *page);
245 void wb_memcg_offline(struct mem_cgroup *memcg);
246 void wb_blkcg_offline(struct blkcg *blkcg);
249 * inode_cgwb_enabled - test whether cgroup writeback is enabled on an inode
250 * @inode: inode of interest
252 * cgroup writeback requires support from both the bdi and filesystem.
253 * Test whether @inode has both.
255 static inline bool inode_cgwb_enabled(struct inode *inode)
257 struct backing_dev_info *bdi = inode_to_bdi(inode);
259 return bdi_cap_account_dirty(bdi) &&
260 (bdi->capabilities & BDI_CAP_CGROUP_WRITEBACK) &&
261 (inode->i_sb->s_type->fs_flags & FS_CGROUP_WRITEBACK);
265 * wb_tryget - try to increment a wb's refcount
266 * @wb: bdi_writeback to get
268 static inline bool wb_tryget(struct bdi_writeback *wb)
270 if (wb != &wb->bdi->wb)
271 return percpu_ref_tryget(&wb->refcnt);
276 * wb_get - increment a wb's refcount
277 * @wb: bdi_writeback to get
279 static inline void wb_get(struct bdi_writeback *wb)
281 if (wb != &wb->bdi->wb)
282 percpu_ref_get(&wb->refcnt);
286 * wb_put - decrement a wb's refcount
287 * @wb: bdi_writeback to put
289 static inline void wb_put(struct bdi_writeback *wb)
291 if (wb != &wb->bdi->wb)
292 percpu_ref_put(&wb->refcnt);
296 * wb_find_current - find wb for %current on a bdi
297 * @bdi: bdi of interest
299 * Find the wb of @bdi which matches both the memcg and blkcg of %current.
300 * Must be called under rcu_read_lock() which protects the returend wb.
303 static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
305 struct cgroup_subsys_state *memcg_css;
306 struct bdi_writeback *wb;
308 memcg_css = task_css(current, memory_cgrp_id);
309 if (!memcg_css->parent)
312 wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
315 * %current's blkcg equals the effective blkcg of its memcg. No
316 * need to use the relatively expensive cgroup_get_e_css().
318 if (likely(wb && wb->blkcg_css == task_css(current, blkio_cgrp_id)))
324 * wb_get_create_current - get or create wb for %current on a bdi
325 * @bdi: bdi of interest
326 * @gfp: allocation mask
328 * Equivalent to wb_get_create() on %current's memcg. This function is
329 * called from a relatively hot path and optimizes the common cases using
332 static inline struct bdi_writeback *
333 wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
335 struct bdi_writeback *wb;
338 wb = wb_find_current(bdi);
339 if (wb && unlikely(!wb_tryget(wb)))
344 struct cgroup_subsys_state *memcg_css;
346 memcg_css = task_get_css(current, memory_cgrp_id);
347 wb = wb_get_create(bdi, memcg_css, gfp);
354 * inode_attach_wb - associate an inode with its wb
355 * @inode: inode of interest
356 * @page: page being dirtied (may be NULL)
358 * If @inode doesn't have its wb, associate it with the wb matching the
359 * memcg of @page or, if @page is NULL, %current. May be called w/ or w/o
362 static inline void inode_attach_wb(struct inode *inode, struct page *page)
365 __inode_attach_wb(inode, page);
369 * inode_detach_wb - disassociate an inode from its wb
370 * @inode: inode of interest
372 * @inode is being freed. Detach from its wb.
374 static inline void inode_detach_wb(struct inode *inode)
383 * inode_to_wb - determine the wb of an inode
384 * @inode: inode of interest
386 * Returns the wb @inode is currently associated with.
388 static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
393 #else /* CONFIG_CGROUP_WRITEBACK */
395 static inline bool inode_cgwb_enabled(struct inode *inode)
400 static inline struct bdi_writeback_congested *
401 wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp)
403 return bdi->wb.congested;
406 static inline void wb_congested_put(struct bdi_writeback_congested *congested)
410 static inline bool wb_tryget(struct bdi_writeback *wb)
415 static inline void wb_get(struct bdi_writeback *wb)
419 static inline void wb_put(struct bdi_writeback *wb)
423 static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
428 static inline struct bdi_writeback *
429 wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
434 static inline void inode_attach_wb(struct inode *inode, struct page *page)
438 static inline void inode_detach_wb(struct inode *inode)
442 static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
444 return &inode_to_bdi(inode)->wb;
447 static inline void wb_memcg_offline(struct mem_cgroup *memcg)
451 static inline void wb_blkcg_offline(struct blkcg *blkcg)
455 #endif /* CONFIG_CGROUP_WRITEBACK */
457 #endif /* _LINUX_BACKING_DEV_H */