blob: 6f0882105f95c6e928f3bea1dc51fc33247bd632 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -070011#include <linux/kernel.h>
Miklos Szeredie4ad08f2008-04-30 00:54:37 -070012#include <linux/fs.h>
Jens Axboe03ba3782009-09-09 09:08:54 +020013#include <linux/sched.h>
Tejun Heoa212b102015-05-22 17:13:33 -040014#include <linux/blkdev.h>
Jens Axboe03ba3782009-09-09 09:08:54 +020015#include <linux/writeback.h>
Tejun Heo52ebea72015-05-22 17:13:37 -040016#include <linux/blk-cgroup.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040017#include <linux/backing-dev-defs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Mikulas Patocka8077c0d2013-10-14 12:14:13 -040019int __must_check bdi_init(struct backing_dev_info *bdi);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070020void bdi_destroy(struct backing_dev_info *bdi);
21
Joe Perchesd2cc4dd2012-11-29 08:37:03 -060022__printf(3, 4)
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -070023int 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);
Christoph Hellwigb4caecd2015-01-14 10:42:32 +010027int __must_check bdi_setup_and_register(struct backing_dev_info *, char *);
Curt Wohlgemuth0e175a12011-10-07 21:54:10 -060028void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
29 enum wb_reason reason);
Christoph Hellwigc5444192010-06-08 18:15:15 +020030void bdi_start_background_writeback(struct backing_dev_info *bdi);
Tejun Heof0054bb2015-05-22 17:13:30 -040031void wb_workfn(struct work_struct *work);
Jens Axboe03ba3782009-09-09 09:08:54 +020032int bdi_has_dirty_io(struct backing_dev_info *bdi);
Tejun Heof0054bb2015-05-22 17:13:30 -040033void wb_wakeup_delayed(struct bdi_writeback *wb);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -070034
Jens Axboe03ba3782009-09-09 09:08:54 +020035extern spinlock_t bdi_lock;
Jens Axboe66f3b8e2009-09-02 09:19:46 +020036extern struct list_head bdi_list;
37
Tejun Heo839a8e82013-04-01 19:08:06 -070038extern struct workqueue_struct *bdi_wq;
39
Jens Axboe03ba3782009-09-09 09:08:54 +020040static 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
Tejun Heo93f78d82015-05-22 17:13:27 -040047static inline void __add_wb_stat(struct bdi_writeback *wb,
48 enum wb_stat_item item, s64 amount)
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -070049{
Tejun Heo93f78d82015-05-22 17:13:27 -040050 __percpu_counter_add(&wb->stat[item], amount, WB_STAT_BATCH);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -070051}
52
Tejun Heo93f78d82015-05-22 17:13:27 -040053static inline void __inc_wb_stat(struct bdi_writeback *wb,
54 enum wb_stat_item item)
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -070055{
Tejun Heo93f78d82015-05-22 17:13:27 -040056 __add_wb_stat(wb, item, 1);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070057}
58
Tejun Heo93f78d82015-05-22 17:13:27 -040059static inline void inc_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070060{
61 unsigned long flags;
62
63 local_irq_save(flags);
Tejun Heo93f78d82015-05-22 17:13:27 -040064 __inc_wb_stat(wb, item);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070065 local_irq_restore(flags);
66}
67
Tejun Heo93f78d82015-05-22 17:13:27 -040068static inline void __dec_wb_stat(struct bdi_writeback *wb,
69 enum wb_stat_item item)
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070070{
Tejun Heo93f78d82015-05-22 17:13:27 -040071 __add_wb_stat(wb, item, -1);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070072}
73
Tejun Heo93f78d82015-05-22 17:13:27 -040074static inline void dec_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070075{
76 unsigned long flags;
77
78 local_irq_save(flags);
Tejun Heo93f78d82015-05-22 17:13:27 -040079 __dec_wb_stat(wb, item);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070080 local_irq_restore(flags);
81}
82
Tejun Heo93f78d82015-05-22 17:13:27 -040083static inline s64 wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070084{
Tejun Heo93f78d82015-05-22 17:13:27 -040085 return percpu_counter_read_positive(&wb->stat[item]);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070086}
87
Tejun Heo93f78d82015-05-22 17:13:27 -040088static inline s64 __wb_stat_sum(struct bdi_writeback *wb,
89 enum wb_stat_item item)
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070090{
Tejun Heo93f78d82015-05-22 17:13:27 -040091 return percpu_counter_sum_positive(&wb->stat[item]);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070092}
93
Tejun Heo93f78d82015-05-22 17:13:27 -040094static inline s64 wb_stat_sum(struct bdi_writeback *wb, enum wb_stat_item item)
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070095{
96 s64 sum;
97 unsigned long flags;
98
99 local_irq_save(flags);
Tejun Heo93f78d82015-05-22 17:13:27 -0400100 sum = __wb_stat_sum(wb, item);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700101 local_irq_restore(flags);
102
103 return sum;
104}
105
Tejun Heo93f78d82015-05-22 17:13:27 -0400106extern void wb_writeout_inc(struct bdi_writeback *wb);
Miklos Szeredidd5656e2008-04-30 00:54:37 -0700107
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700108/*
109 * maximal error of a stat counter.
110 */
Tejun Heo93f78d82015-05-22 17:13:27 -0400111static inline unsigned long wb_stat_error(struct bdi_writeback *wb)
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700112{
113#ifdef CONFIG_SMP
Tejun Heo93f78d82015-05-22 17:13:27 -0400114 return nr_cpu_ids * WB_STAT_BATCH;
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700115#else
116 return 1;
117#endif
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700118}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700120int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio);
Peter Zijlstraa42dde02008-04-30 00:54:36 -0700121int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ratio);
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/*
124 * Flags in backing_dev_info::capability
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700125 *
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
Maxim Patlasov5a537482013-09-11 14:22:46 -0700137 * BDI_CAP_STRICTLIMIT: Keep number of dirty pages below bdi threshold.
Tejun Heo89e9b9e2015-05-22 17:13:36 -0400138 *
139 * BDI_CAP_CGROUP_WRITEBACK: Supports cgroup-aware writeback.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700141#define BDI_CAP_NO_ACCT_DIRTY 0x00000001
142#define BDI_CAP_NO_WRITEBACK 0x00000002
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100143#define BDI_CAP_NO_ACCT_WB 0x00000004
144#define BDI_CAP_STABLE_WRITES 0x00000008
145#define BDI_CAP_STRICTLIMIT 0x00000010
Tejun Heo89e9b9e2015-05-22 17:13:36 -0400146#define BDI_CAP_CGROUP_WRITEBACK 0x00000020
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700148#define BDI_CAP_NO_ACCT_AND_WRITEBACK \
149 (BDI_CAP_NO_WRITEBACK | BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_ACCT_WB)
150
Jörn Engel5129a462010-04-25 08:54:42 +0200151extern struct backing_dev_info noop_backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153int writeback_in_progress(struct backing_dev_info *bdi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Tejun Heoa212b102015-05-22 17:13:33 -0400155static 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
Tejun Heoec8a6f22015-05-22 17:13:41 -0400170static inline int wb_congested(struct bdi_writeback *wb, int cong_bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Tejun Heoec8a6f22015-05-22 17:13:41 -0400172 struct backing_dev_info *bdi = wb->bdi;
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (bdi->congested_fn)
Tejun Heoec8a6f22015-05-22 17:13:41 -0400175 return bdi->congested_fn(bdi->congested_data, cong_bits);
176 return wb->congested->state & cong_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Jens Axboe8aa7e842009-07-09 14:52:32 +0200179long congestion_wait(int sync, long timeout);
Mel Gorman0e093d992010-10-26 14:21:45 -0700180long wait_iff_congested(struct zone *zone, int sync, long timeout);
Wanpeng Li3965c9a2012-07-31 16:41:52 -0700181int pdflush_proc_obsolete(struct ctl_table *table, int write,
182 void __user *buffer, size_t *lenp, loff_t *ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Darrick J. Wong7d311cd2013-02-21 16:42:48 -0800184static inline bool bdi_cap_stable_pages_required(struct backing_dev_info *bdi)
185{
186 return bdi->capabilities & BDI_CAP_STABLE_WRITES;
187}
188
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700189static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi)
190{
191 return !(bdi->capabilities & BDI_CAP_NO_WRITEBACK);
192}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700194static inline bool bdi_cap_account_dirty(struct backing_dev_info *bdi)
195{
196 return !(bdi->capabilities & BDI_CAP_NO_ACCT_DIRTY);
197}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700199static inline bool bdi_cap_account_writeback(struct backing_dev_info *bdi)
200{
201 /* Paranoia: BDI_CAP_NO_WRITEBACK implies BDI_CAP_NO_ACCT_WB */
202 return !(bdi->capabilities & (BDI_CAP_NO_ACCT_WB |
203 BDI_CAP_NO_WRITEBACK));
204}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700206static inline bool mapping_cap_writeback_dirty(struct address_space *mapping)
207{
Christoph Hellwigde1414a2015-01-14 10:42:36 +0100208 return bdi_cap_writeback_dirty(inode_to_bdi(mapping->host));
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700209}
210
211static inline bool mapping_cap_account_dirty(struct address_space *mapping)
212{
Christoph Hellwigde1414a2015-01-14 10:42:36 +0100213 return bdi_cap_account_dirty(inode_to_bdi(mapping->host));
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700214}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Jens Axboe03ba3782009-09-09 09:08:54 +0200216static inline int bdi_sched_wait(void *word)
217{
218 schedule();
219 return 0;
220}
221
Tejun Heo89e9b9e2015-05-22 17:13:36 -0400222#ifdef CONFIG_CGROUP_WRITEBACK
223
Tejun Heo52ebea72015-05-22 17:13:37 -0400224struct bdi_writeback_congested *
225wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp);
226void wb_congested_put(struct bdi_writeback_congested *congested);
227struct bdi_writeback *wb_get_create(struct backing_dev_info *bdi,
228 struct cgroup_subsys_state *memcg_css,
229 gfp_t gfp);
230void __inode_attach_wb(struct inode *inode, struct page *page);
231void wb_memcg_offline(struct mem_cgroup *memcg);
232void wb_blkcg_offline(struct blkcg *blkcg);
Tejun Heo703c2702015-05-22 17:13:44 -0400233int inode_congested(struct inode *inode, int cong_bits);
Tejun Heo52ebea72015-05-22 17:13:37 -0400234
Tejun Heo89e9b9e2015-05-22 17:13:36 -0400235/**
236 * inode_cgwb_enabled - test whether cgroup writeback is enabled on an inode
237 * @inode: inode of interest
238 *
239 * cgroup writeback requires support from both the bdi and filesystem.
240 * Test whether @inode has both.
241 */
242static inline bool inode_cgwb_enabled(struct inode *inode)
243{
244 struct backing_dev_info *bdi = inode_to_bdi(inode);
245
246 return bdi_cap_account_dirty(bdi) &&
247 (bdi->capabilities & BDI_CAP_CGROUP_WRITEBACK) &&
248 (inode->i_sb->s_type->fs_flags & FS_CGROUP_WRITEBACK);
249}
250
Tejun Heo52ebea72015-05-22 17:13:37 -0400251/**
252 * wb_tryget - try to increment a wb's refcount
253 * @wb: bdi_writeback to get
254 */
255static inline bool wb_tryget(struct bdi_writeback *wb)
256{
257 if (wb != &wb->bdi->wb)
258 return percpu_ref_tryget(&wb->refcnt);
259 return true;
260}
261
262/**
263 * wb_get - increment a wb's refcount
264 * @wb: bdi_writeback to get
265 */
266static inline void wb_get(struct bdi_writeback *wb)
267{
268 if (wb != &wb->bdi->wb)
269 percpu_ref_get(&wb->refcnt);
270}
271
272/**
273 * wb_put - decrement a wb's refcount
274 * @wb: bdi_writeback to put
275 */
276static inline void wb_put(struct bdi_writeback *wb)
277{
278 if (wb != &wb->bdi->wb)
279 percpu_ref_put(&wb->refcnt);
280}
281
282/**
283 * wb_find_current - find wb for %current on a bdi
284 * @bdi: bdi of interest
285 *
286 * Find the wb of @bdi which matches both the memcg and blkcg of %current.
287 * Must be called under rcu_read_lock() which protects the returend wb.
288 * NULL if not found.
289 */
290static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
291{
292 struct cgroup_subsys_state *memcg_css;
293 struct bdi_writeback *wb;
294
295 memcg_css = task_css(current, memory_cgrp_id);
296 if (!memcg_css->parent)
297 return &bdi->wb;
298
299 wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
300
301 /*
302 * %current's blkcg equals the effective blkcg of its memcg. No
303 * need to use the relatively expensive cgroup_get_e_css().
304 */
305 if (likely(wb && wb->blkcg_css == task_css(current, blkio_cgrp_id)))
306 return wb;
307 return NULL;
308}
309
310/**
311 * wb_get_create_current - get or create wb for %current on a bdi
312 * @bdi: bdi of interest
313 * @gfp: allocation mask
314 *
315 * Equivalent to wb_get_create() on %current's memcg. This function is
316 * called from a relatively hot path and optimizes the common cases using
317 * wb_find_current().
318 */
319static inline struct bdi_writeback *
320wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
321{
322 struct bdi_writeback *wb;
323
324 rcu_read_lock();
325 wb = wb_find_current(bdi);
326 if (wb && unlikely(!wb_tryget(wb)))
327 wb = NULL;
328 rcu_read_unlock();
329
330 if (unlikely(!wb)) {
331 struct cgroup_subsys_state *memcg_css;
332
333 memcg_css = task_get_css(current, memory_cgrp_id);
334 wb = wb_get_create(bdi, memcg_css, gfp);
335 css_put(memcg_css);
336 }
337 return wb;
338}
339
340/**
341 * inode_attach_wb - associate an inode with its wb
342 * @inode: inode of interest
343 * @page: page being dirtied (may be NULL)
344 *
345 * If @inode doesn't have its wb, associate it with the wb matching the
346 * memcg of @page or, if @page is NULL, %current. May be called w/ or w/o
347 * @inode->i_lock.
348 */
349static inline void inode_attach_wb(struct inode *inode, struct page *page)
350{
351 if (!inode->i_wb)
352 __inode_attach_wb(inode, page);
353}
354
355/**
356 * inode_detach_wb - disassociate an inode from its wb
357 * @inode: inode of interest
358 *
359 * @inode is being freed. Detach from its wb.
360 */
361static inline void inode_detach_wb(struct inode *inode)
362{
363 if (inode->i_wb) {
364 wb_put(inode->i_wb);
365 inode->i_wb = NULL;
366 }
367}
368
369/**
370 * inode_to_wb - determine the wb of an inode
371 * @inode: inode of interest
372 *
373 * Returns the wb @inode is currently associated with.
374 */
375static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
376{
377 return inode->i_wb;
378}
379
Tejun Heo89e9b9e2015-05-22 17:13:36 -0400380#else /* CONFIG_CGROUP_WRITEBACK */
381
382static inline bool inode_cgwb_enabled(struct inode *inode)
383{
384 return false;
385}
386
Tejun Heo52ebea72015-05-22 17:13:37 -0400387static inline struct bdi_writeback_congested *
388wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp)
389{
390 return bdi->wb.congested;
391}
392
393static inline void wb_congested_put(struct bdi_writeback_congested *congested)
394{
395}
396
397static inline bool wb_tryget(struct bdi_writeback *wb)
398{
399 return true;
400}
401
402static inline void wb_get(struct bdi_writeback *wb)
403{
404}
405
406static inline void wb_put(struct bdi_writeback *wb)
407{
408}
409
410static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
411{
412 return &bdi->wb;
413}
414
415static inline struct bdi_writeback *
416wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
417{
418 return &bdi->wb;
419}
420
421static inline void inode_attach_wb(struct inode *inode, struct page *page)
422{
423}
424
425static inline void inode_detach_wb(struct inode *inode)
426{
427}
428
429static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
430{
431 return &inode_to_bdi(inode)->wb;
432}
433
434static inline void wb_memcg_offline(struct mem_cgroup *memcg)
435{
436}
437
438static inline void wb_blkcg_offline(struct blkcg *blkcg)
439{
440}
441
Tejun Heo703c2702015-05-22 17:13:44 -0400442static inline int inode_congested(struct inode *inode, int cong_bits)
443{
444 return wb_congested(&inode_to_bdi(inode)->wb, cong_bits);
445}
446
Tejun Heo89e9b9e2015-05-22 17:13:36 -0400447#endif /* CONFIG_CGROUP_WRITEBACK */
448
Tejun Heo703c2702015-05-22 17:13:44 -0400449static inline int inode_read_congested(struct inode *inode)
450{
451 return inode_congested(inode, 1 << WB_sync_congested);
452}
453
454static inline int inode_write_congested(struct inode *inode)
455{
456 return inode_congested(inode, 1 << WB_async_congested);
457}
458
459static inline int inode_rw_congested(struct inode *inode)
460{
461 return inode_congested(inode, (1 << WB_sync_congested) |
462 (1 << WB_async_congested));
463}
464
Tejun Heoec8a6f22015-05-22 17:13:41 -0400465static inline int bdi_congested(struct backing_dev_info *bdi, int cong_bits)
466{
467 return wb_congested(&bdi->wb, cong_bits);
468}
469
470static inline int bdi_read_congested(struct backing_dev_info *bdi)
471{
472 return bdi_congested(bdi, 1 << WB_sync_congested);
473}
474
475static inline int bdi_write_congested(struct backing_dev_info *bdi)
476{
477 return bdi_congested(bdi, 1 << WB_async_congested);
478}
479
480static inline int bdi_rw_congested(struct backing_dev_info *bdi)
481{
482 return bdi_congested(bdi, (1 << WB_sync_congested) |
483 (1 << WB_async_congested));
484}
485
Tejun Heo89e9b9e2015-05-22 17:13:36 -0400486#endif /* _LINUX_BACKING_DEV_H */