]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/linux/backing-dev.h
writeback: let balance_dirty_pages() work on the matching cgroup bdi_writeback
[mirror_ubuntu-artful-kernel.git] / include / linux / backing-dev.h
CommitLineData
1da177e4
LT
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
8#ifndef _LINUX_BACKING_DEV_H
9#define _LINUX_BACKING_DEV_H
10
cf0ca9fe 11#include <linux/kernel.h>
e4ad08fe 12#include <linux/fs.h>
03ba3782 13#include <linux/sched.h>
a212b105 14#include <linux/blkdev.h>
03ba3782 15#include <linux/writeback.h>
52ebea74 16#include <linux/blk-cgroup.h>
66114cad 17#include <linux/backing-dev-defs.h>
1da177e4 18
8077c0d9 19int __must_check bdi_init(struct backing_dev_info *bdi);
b2e8fb6e
PZ
20void bdi_destroy(struct backing_dev_info *bdi);
21
d2cc4dde 22__printf(3, 4)
cf0ca9fe
PZ
23int bdi_register(struct backing_dev_info *bdi, struct device *parent,
24 const char *fmt, ...);
25int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev);
26void bdi_unregister(struct backing_dev_info *bdi);
b4caecd4 27int __must_check bdi_setup_and_register(struct backing_dev_info *, char *);
0e175a18
CW
28void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
29 enum wb_reason reason);
c5444198 30void bdi_start_background_writeback(struct backing_dev_info *bdi);
f0054bb1 31void wb_workfn(struct work_struct *work);
03ba3782 32int bdi_has_dirty_io(struct backing_dev_info *bdi);
f0054bb1 33void wb_wakeup_delayed(struct bdi_writeback *wb);
cf0ca9fe 34
03ba3782 35extern spinlock_t bdi_lock;
66f3b8e2
JA
36extern struct list_head bdi_list;
37
839a8e86
TH
38extern struct workqueue_struct *bdi_wq;
39
03ba3782
JA
40static inline int wb_has_dirty_io(struct bdi_writeback *wb)
41{
42 return !list_empty(&wb->b_dirty) ||
43 !list_empty(&wb->b_io) ||
44 !list_empty(&wb->b_more_io);
45}
46
93f78d88
TH
47static inline void __add_wb_stat(struct bdi_writeback *wb,
48 enum wb_stat_item item, s64 amount)
b2e8fb6e 49{
93f78d88 50 __percpu_counter_add(&wb->stat[item], amount, WB_STAT_BATCH);
b2e8fb6e
PZ
51}
52
93f78d88
TH
53static inline void __inc_wb_stat(struct bdi_writeback *wb,
54 enum wb_stat_item item)
b2e8fb6e 55{
93f78d88 56 __add_wb_stat(wb, item, 1);
b2e8fb6e
PZ
57}
58
93f78d88 59static inline void inc_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
b2e8fb6e
PZ
60{
61 unsigned long flags;
62
63 local_irq_save(flags);
93f78d88 64 __inc_wb_stat(wb, item);
b2e8fb6e
PZ
65 local_irq_restore(flags);
66}
67
93f78d88
TH
68static inline void __dec_wb_stat(struct bdi_writeback *wb,
69 enum wb_stat_item item)
b2e8fb6e 70{
93f78d88 71 __add_wb_stat(wb, item, -1);
b2e8fb6e
PZ
72}
73
93f78d88 74static inline void dec_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
b2e8fb6e
PZ
75{
76 unsigned long flags;
77
78 local_irq_save(flags);
93f78d88 79 __dec_wb_stat(wb, item);
b2e8fb6e
PZ
80 local_irq_restore(flags);
81}
82
93f78d88 83static inline s64 wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
b2e8fb6e 84{
93f78d88 85 return percpu_counter_read_positive(&wb->stat[item]);
b2e8fb6e
PZ
86}
87
93f78d88
TH
88static inline s64 __wb_stat_sum(struct bdi_writeback *wb,
89 enum wb_stat_item item)
b2e8fb6e 90{
93f78d88 91 return percpu_counter_sum_positive(&wb->stat[item]);
b2e8fb6e
PZ
92}
93
93f78d88 94static inline s64 wb_stat_sum(struct bdi_writeback *wb, enum wb_stat_item item)
e0bf68dd 95{
b2e8fb6e
PZ
96 s64 sum;
97 unsigned long flags;
98
99 local_irq_save(flags);
93f78d88 100 sum = __wb_stat_sum(wb, item);
b2e8fb6e
PZ
101 local_irq_restore(flags);
102
103 return sum;
e0bf68dd
PZ
104}
105
93f78d88 106extern void wb_writeout_inc(struct bdi_writeback *wb);
dd5656e5 107
b2e8fb6e
PZ
108/*
109 * maximal error of a stat counter.
110 */
93f78d88 111static inline unsigned long wb_stat_error(struct bdi_writeback *wb)
e0bf68dd 112{
b2e8fb6e 113#ifdef CONFIG_SMP
93f78d88 114 return nr_cpu_ids * WB_STAT_BATCH;
b2e8fb6e
PZ
115#else
116 return 1;
117#endif
e0bf68dd 118}
1da177e4 119
189d3c4a 120int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio);
a42dde04 121int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ratio);
189d3c4a 122
1da177e4
LT
123/*
124 * Flags in backing_dev_info::capability
e4ad08fe
MS
125 *
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)
129 *
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.
133 *
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
5a537485 137 * BDI_CAP_STRICTLIMIT: Keep number of dirty pages below bdi threshold.
89e9b9e0
TH
138 *
139 * BDI_CAP_CGROUP_WRITEBACK: Supports cgroup-aware writeback.
1da177e4 140 */
e4ad08fe
MS
141#define BDI_CAP_NO_ACCT_DIRTY 0x00000001
142#define BDI_CAP_NO_WRITEBACK 0x00000002
b4caecd4
CH
143#define BDI_CAP_NO_ACCT_WB 0x00000004
144#define BDI_CAP_STABLE_WRITES 0x00000008
145#define BDI_CAP_STRICTLIMIT 0x00000010
89e9b9e0 146#define BDI_CAP_CGROUP_WRITEBACK 0x00000020
1da177e4 147
e4ad08fe
MS
148#define BDI_CAP_NO_ACCT_AND_WRITEBACK \
149 (BDI_CAP_NO_WRITEBACK | BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_ACCT_WB)
150
5129a469 151extern struct backing_dev_info noop_backing_dev_info;
1da177e4 152
1da177e4 153int writeback_in_progress(struct backing_dev_info *bdi);
1da177e4 154
a212b105
TH
155static inline struct backing_dev_info *inode_to_bdi(struct inode *inode)
156{
157 struct super_block *sb;
158
159 if (!inode)
160 return &noop_backing_dev_info;
161
162 sb = inode->i_sb;
163#ifdef CONFIG_BLOCK
164 if (sb_is_blkdev_sb(sb))
165 return blk_get_backing_dev_info(I_BDEV(inode));
166#endif
167 return sb->s_bdi;
168}
169
1da177e4
LT
170static inline int bdi_congested(struct backing_dev_info *bdi, int bdi_bits)
171{
172 if (bdi->congested_fn)
173 return bdi->congested_fn(bdi->congested_data, bdi_bits);
4aa9c692 174 return (bdi->wb.congested->state & bdi_bits);
1da177e4
LT
175}
176
177static inline int bdi_read_congested(struct backing_dev_info *bdi)
178{
4452226e 179 return bdi_congested(bdi, 1 << WB_sync_congested);
1da177e4
LT
180}
181
182static inline int bdi_write_congested(struct backing_dev_info *bdi)
183{
4452226e 184 return bdi_congested(bdi, 1 << WB_async_congested);
1da177e4
LT
185}
186
187static inline int bdi_rw_congested(struct backing_dev_info *bdi)
188{
4452226e
TH
189 return bdi_congested(bdi, (1 << WB_sync_congested) |
190 (1 << WB_async_congested));
1da177e4
LT
191}
192
8aa7e847 193long congestion_wait(int sync, long timeout);
0e093d99 194long wait_iff_congested(struct zone *zone, int sync, long timeout);
3965c9ae
WL
195int pdflush_proc_obsolete(struct ctl_table *table, int write,
196 void __user *buffer, size_t *lenp, loff_t *ppos);
1da177e4 197
7d311cda
DW
198static inline bool bdi_cap_stable_pages_required(struct backing_dev_info *bdi)
199{
200 return bdi->capabilities & BDI_CAP_STABLE_WRITES;
201}
202
e4ad08fe
MS
203static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi)
204{
205 return !(bdi->capabilities & BDI_CAP_NO_WRITEBACK);
206}
207
208static inline bool bdi_cap_account_dirty(struct backing_dev_info *bdi)
209{
210 return !(bdi->capabilities & BDI_CAP_NO_ACCT_DIRTY);
211}
1da177e4 212
e4ad08fe
MS
213static inline bool bdi_cap_account_writeback(struct backing_dev_info *bdi)
214{
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));
218}
1da177e4 219
e4ad08fe
MS
220static inline bool mapping_cap_writeback_dirty(struct address_space *mapping)
221{
de1414a6 222 return bdi_cap_writeback_dirty(inode_to_bdi(mapping->host));
e4ad08fe 223}
1da177e4 224
e4ad08fe
MS
225static inline bool mapping_cap_account_dirty(struct address_space *mapping)
226{
de1414a6 227 return bdi_cap_account_dirty(inode_to_bdi(mapping->host));
e4ad08fe 228}
1da177e4 229
03ba3782
JA
230static inline int bdi_sched_wait(void *word)
231{
232 schedule();
233 return 0;
234}
235
89e9b9e0
TH
236#ifdef CONFIG_CGROUP_WRITEBACK
237
52ebea74
TH
238struct bdi_writeback_congested *
239wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp);
240void wb_congested_put(struct bdi_writeback_congested *congested);
241struct bdi_writeback *wb_get_create(struct backing_dev_info *bdi,
242 struct cgroup_subsys_state *memcg_css,
243 gfp_t gfp);
244void __inode_attach_wb(struct inode *inode, struct page *page);
245void wb_memcg_offline(struct mem_cgroup *memcg);
246void wb_blkcg_offline(struct blkcg *blkcg);
247
89e9b9e0
TH
248/**
249 * inode_cgwb_enabled - test whether cgroup writeback is enabled on an inode
250 * @inode: inode of interest
251 *
252 * cgroup writeback requires support from both the bdi and filesystem.
253 * Test whether @inode has both.
254 */
255static inline bool inode_cgwb_enabled(struct inode *inode)
256{
257 struct backing_dev_info *bdi = inode_to_bdi(inode);
258
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);
262}
263
52ebea74
TH
264/**
265 * wb_tryget - try to increment a wb's refcount
266 * @wb: bdi_writeback to get
267 */
268static inline bool wb_tryget(struct bdi_writeback *wb)
269{
270 if (wb != &wb->bdi->wb)
271 return percpu_ref_tryget(&wb->refcnt);
272 return true;
273}
274
275/**
276 * wb_get - increment a wb's refcount
277 * @wb: bdi_writeback to get
278 */
279static inline void wb_get(struct bdi_writeback *wb)
280{
281 if (wb != &wb->bdi->wb)
282 percpu_ref_get(&wb->refcnt);
283}
284
285/**
286 * wb_put - decrement a wb's refcount
287 * @wb: bdi_writeback to put
288 */
289static inline void wb_put(struct bdi_writeback *wb)
290{
291 if (wb != &wb->bdi->wb)
292 percpu_ref_put(&wb->refcnt);
293}
294
295/**
296 * wb_find_current - find wb for %current on a bdi
297 * @bdi: bdi of interest
298 *
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.
301 * NULL if not found.
302 */
303static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
304{
305 struct cgroup_subsys_state *memcg_css;
306 struct bdi_writeback *wb;
307
308 memcg_css = task_css(current, memory_cgrp_id);
309 if (!memcg_css->parent)
310 return &bdi->wb;
311
312 wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
313
314 /*
315 * %current's blkcg equals the effective blkcg of its memcg. No
316 * need to use the relatively expensive cgroup_get_e_css().
317 */
318 if (likely(wb && wb->blkcg_css == task_css(current, blkio_cgrp_id)))
319 return wb;
320 return NULL;
321}
322
323/**
324 * wb_get_create_current - get or create wb for %current on a bdi
325 * @bdi: bdi of interest
326 * @gfp: allocation mask
327 *
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
330 * wb_find_current().
331 */
332static inline struct bdi_writeback *
333wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
334{
335 struct bdi_writeback *wb;
336
337 rcu_read_lock();
338 wb = wb_find_current(bdi);
339 if (wb && unlikely(!wb_tryget(wb)))
340 wb = NULL;
341 rcu_read_unlock();
342
343 if (unlikely(!wb)) {
344 struct cgroup_subsys_state *memcg_css;
345
346 memcg_css = task_get_css(current, memory_cgrp_id);
347 wb = wb_get_create(bdi, memcg_css, gfp);
348 css_put(memcg_css);
349 }
350 return wb;
351}
352
353/**
354 * inode_attach_wb - associate an inode with its wb
355 * @inode: inode of interest
356 * @page: page being dirtied (may be NULL)
357 *
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
360 * @inode->i_lock.
361 */
362static inline void inode_attach_wb(struct inode *inode, struct page *page)
363{
364 if (!inode->i_wb)
365 __inode_attach_wb(inode, page);
366}
367
368/**
369 * inode_detach_wb - disassociate an inode from its wb
370 * @inode: inode of interest
371 *
372 * @inode is being freed. Detach from its wb.
373 */
374static inline void inode_detach_wb(struct inode *inode)
375{
376 if (inode->i_wb) {
377 wb_put(inode->i_wb);
378 inode->i_wb = NULL;
379 }
380}
381
382/**
383 * inode_to_wb - determine the wb of an inode
384 * @inode: inode of interest
385 *
386 * Returns the wb @inode is currently associated with.
387 */
388static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
389{
390 return inode->i_wb;
391}
392
89e9b9e0
TH
393#else /* CONFIG_CGROUP_WRITEBACK */
394
395static inline bool inode_cgwb_enabled(struct inode *inode)
396{
397 return false;
398}
399
52ebea74
TH
400static inline struct bdi_writeback_congested *
401wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp)
402{
403 return bdi->wb.congested;
404}
405
406static inline void wb_congested_put(struct bdi_writeback_congested *congested)
407{
408}
409
410static inline bool wb_tryget(struct bdi_writeback *wb)
411{
412 return true;
413}
414
415static inline void wb_get(struct bdi_writeback *wb)
416{
417}
418
419static inline void wb_put(struct bdi_writeback *wb)
420{
421}
422
423static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
424{
425 return &bdi->wb;
426}
427
428static inline struct bdi_writeback *
429wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
430{
431 return &bdi->wb;
432}
433
434static inline void inode_attach_wb(struct inode *inode, struct page *page)
435{
436}
437
438static inline void inode_detach_wb(struct inode *inode)
439{
440}
441
442static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
443{
444 return &inode_to_bdi(inode)->wb;
445}
446
447static inline void wb_memcg_offline(struct mem_cgroup *memcg)
448{
449}
450
451static inline void wb_blkcg_offline(struct blkcg *blkcg)
452{
453}
454
89e9b9e0
TH
455#endif /* CONFIG_CGROUP_WRITEBACK */
456
457#endif /* _LINUX_BACKING_DEV_H */