blob: 2c498a2a826804e17adbc28221be42e93880de26 [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);
233
Tejun Heo89e9b9e2015-05-22 17:13:36 -0400234/**
235 * inode_cgwb_enabled - test whether cgroup writeback is enabled on an inode
236 * @inode: inode of interest
237 *
238 * cgroup writeback requires support from both the bdi and filesystem.
239 * Test whether @inode has both.
240 */
241static inline bool inode_cgwb_enabled(struct inode *inode)
242{
243 struct backing_dev_info *bdi = inode_to_bdi(inode);
244
245 return bdi_cap_account_dirty(bdi) &&
246 (bdi->capabilities & BDI_CAP_CGROUP_WRITEBACK) &&
247 (inode->i_sb->s_type->fs_flags & FS_CGROUP_WRITEBACK);
248}
249
Tejun Heo52ebea72015-05-22 17:13:37 -0400250/**
251 * wb_tryget - try to increment a wb's refcount
252 * @wb: bdi_writeback to get
253 */
254static inline bool wb_tryget(struct bdi_writeback *wb)
255{
256 if (wb != &wb->bdi->wb)
257 return percpu_ref_tryget(&wb->refcnt);
258 return true;
259}
260
261/**
262 * wb_get - increment a wb's refcount
263 * @wb: bdi_writeback to get
264 */
265static inline void wb_get(struct bdi_writeback *wb)
266{
267 if (wb != &wb->bdi->wb)
268 percpu_ref_get(&wb->refcnt);
269}
270
271/**
272 * wb_put - decrement a wb's refcount
273 * @wb: bdi_writeback to put
274 */
275static inline void wb_put(struct bdi_writeback *wb)
276{
277 if (wb != &wb->bdi->wb)
278 percpu_ref_put(&wb->refcnt);
279}
280
281/**
282 * wb_find_current - find wb for %current on a bdi
283 * @bdi: bdi of interest
284 *
285 * Find the wb of @bdi which matches both the memcg and blkcg of %current.
286 * Must be called under rcu_read_lock() which protects the returend wb.
287 * NULL if not found.
288 */
289static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
290{
291 struct cgroup_subsys_state *memcg_css;
292 struct bdi_writeback *wb;
293
294 memcg_css = task_css(current, memory_cgrp_id);
295 if (!memcg_css->parent)
296 return &bdi->wb;
297
298 wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
299
300 /*
301 * %current's blkcg equals the effective blkcg of its memcg. No
302 * need to use the relatively expensive cgroup_get_e_css().
303 */
304 if (likely(wb && wb->blkcg_css == task_css(current, blkio_cgrp_id)))
305 return wb;
306 return NULL;
307}
308
309/**
310 * wb_get_create_current - get or create wb for %current on a bdi
311 * @bdi: bdi of interest
312 * @gfp: allocation mask
313 *
314 * Equivalent to wb_get_create() on %current's memcg. This function is
315 * called from a relatively hot path and optimizes the common cases using
316 * wb_find_current().
317 */
318static inline struct bdi_writeback *
319wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
320{
321 struct bdi_writeback *wb;
322
323 rcu_read_lock();
324 wb = wb_find_current(bdi);
325 if (wb && unlikely(!wb_tryget(wb)))
326 wb = NULL;
327 rcu_read_unlock();
328
329 if (unlikely(!wb)) {
330 struct cgroup_subsys_state *memcg_css;
331
332 memcg_css = task_get_css(current, memory_cgrp_id);
333 wb = wb_get_create(bdi, memcg_css, gfp);
334 css_put(memcg_css);
335 }
336 return wb;
337}
338
339/**
340 * inode_attach_wb - associate an inode with its wb
341 * @inode: inode of interest
342 * @page: page being dirtied (may be NULL)
343 *
344 * If @inode doesn't have its wb, associate it with the wb matching the
345 * memcg of @page or, if @page is NULL, %current. May be called w/ or w/o
346 * @inode->i_lock.
347 */
348static inline void inode_attach_wb(struct inode *inode, struct page *page)
349{
350 if (!inode->i_wb)
351 __inode_attach_wb(inode, page);
352}
353
354/**
355 * inode_detach_wb - disassociate an inode from its wb
356 * @inode: inode of interest
357 *
358 * @inode is being freed. Detach from its wb.
359 */
360static inline void inode_detach_wb(struct inode *inode)
361{
362 if (inode->i_wb) {
363 wb_put(inode->i_wb);
364 inode->i_wb = NULL;
365 }
366}
367
368/**
369 * inode_to_wb - determine the wb of an inode
370 * @inode: inode of interest
371 *
372 * Returns the wb @inode is currently associated with.
373 */
374static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
375{
376 return inode->i_wb;
377}
378
Tejun Heo89e9b9e2015-05-22 17:13:36 -0400379#else /* CONFIG_CGROUP_WRITEBACK */
380
381static inline bool inode_cgwb_enabled(struct inode *inode)
382{
383 return false;
384}
385
Tejun Heo52ebea72015-05-22 17:13:37 -0400386static inline struct bdi_writeback_congested *
387wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp)
388{
389 return bdi->wb.congested;
390}
391
392static inline void wb_congested_put(struct bdi_writeback_congested *congested)
393{
394}
395
396static inline bool wb_tryget(struct bdi_writeback *wb)
397{
398 return true;
399}
400
401static inline void wb_get(struct bdi_writeback *wb)
402{
403}
404
405static inline void wb_put(struct bdi_writeback *wb)
406{
407}
408
409static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
410{
411 return &bdi->wb;
412}
413
414static inline struct bdi_writeback *
415wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
416{
417 return &bdi->wb;
418}
419
420static inline void inode_attach_wb(struct inode *inode, struct page *page)
421{
422}
423
424static inline void inode_detach_wb(struct inode *inode)
425{
426}
427
428static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
429{
430 return &inode_to_bdi(inode)->wb;
431}
432
433static inline void wb_memcg_offline(struct mem_cgroup *memcg)
434{
435}
436
437static inline void wb_blkcg_offline(struct blkcg *blkcg)
438{
439}
440
Tejun Heo89e9b9e2015-05-22 17:13:36 -0400441#endif /* CONFIG_CGROUP_WRITEBACK */
442
Tejun Heoec8a6f22015-05-22 17:13:41 -0400443static inline int bdi_congested(struct backing_dev_info *bdi, int cong_bits)
444{
445 return wb_congested(&bdi->wb, cong_bits);
446}
447
448static inline int bdi_read_congested(struct backing_dev_info *bdi)
449{
450 return bdi_congested(bdi, 1 << WB_sync_congested);
451}
452
453static inline int bdi_write_congested(struct backing_dev_info *bdi)
454{
455 return bdi_congested(bdi, 1 << WB_async_congested);
456}
457
458static inline int bdi_rw_congested(struct backing_dev_info *bdi)
459{
460 return bdi_congested(bdi, (1 << WB_sync_congested) |
461 (1 << WB_async_congested));
462}
463
Tejun Heo89e9b9e2015-05-22 17:13:36 -0400464#endif /* _LINUX_BACKING_DEV_H */