blob: b636bf0a0e35be99dd0e4a854a4e7e4758f54e0e [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 Szeredie4ad08fe2008-04-30 00:54:37 -070012#include <linux/fs.h>
Jens Axboe03ba3782009-09-09 09:08:54 +020013#include <linux/sched.h>
Tejun Heo0b6d7572015-05-22 17:13:33 -040014#include <linux/blkdev.h>
Jens Axboe03ba3782009-09-09 09:08:54 +020015#include <linux/writeback.h>
Tejun Heo40d7d422015-09-23 17:07:29 -040016#include <linux/memcontrol.h>
Tejun Heo6b385782015-05-22 17:13:37 -040017#include <linux/blk-cgroup.h>
Tejun Heo9d6e9852015-05-22 17:13:32 -040018#include <linux/backing-dev-defs.h>
Tejun Heod443edc2015-07-02 08:44:34 -060019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Mikulas Patocka8077c0d2013-10-14 12:14:13 -040021int __must_check bdi_init(struct backing_dev_info *bdi);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070022void bdi_destroy(struct backing_dev_info *bdi);
23
Joe Perchesd2cc4dd2012-11-29 08:37:03 -060024__printf(3, 4)
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -070025int bdi_register(struct backing_dev_info *bdi, struct device *parent,
26 const char *fmt, ...);
27int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev);
Christoph Hellwigb4caecd2015-01-14 10:42:32 +010028int __must_check bdi_setup_and_register(struct backing_dev_info *, char *);
Tejun Heo283c54c2015-05-22 17:13:51 -040029void wb_start_writeback(struct bdi_writeback *wb, long nr_pages,
30 bool range_cyclic, enum wb_reason reason);
Tejun Heo5f618482015-05-22 17:13:54 -040031void wb_start_background_writeback(struct bdi_writeback *wb);
Tejun Heoc9b0f222015-05-22 17:13:30 -040032void wb_workfn(struct work_struct *work);
Tejun Heoc9b0f222015-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
Tejun Heo00913372015-05-22 17:13:45 -040040static inline bool wb_has_dirty_io(struct bdi_writeback *wb)
Jens Axboe03ba3782009-09-09 09:08:54 +020041{
Tejun Heo00913372015-05-22 17:13:45 -040042 return test_bit(WB_has_dirty_io, &wb->state);
Jens Axboe03ba3782009-09-09 09:08:54 +020043}
44
Tejun Heo42969232015-05-22 17:13:47 -040045static inline bool bdi_has_dirty_io(struct backing_dev_info *bdi)
46{
47 /*
48 * @bdi->tot_write_bandwidth is guaranteed to be > 0 if there are
49 * any dirty wbs. See wb_update_write_bandwidth().
50 */
51 return atomic_long_read(&bdi->tot_write_bandwidth);
52}
53
Tejun Heo9ce34202015-05-22 17:13:27 -040054static inline void __add_wb_stat(struct bdi_writeback *wb,
55 enum wb_stat_item item, s64 amount)
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -070056{
Tejun Heo9ce34202015-05-22 17:13:27 -040057 __percpu_counter_add(&wb->stat[item], amount, WB_STAT_BATCH);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -070058}
59
Tejun Heo9ce34202015-05-22 17:13:27 -040060static inline void __inc_wb_stat(struct bdi_writeback *wb,
61 enum wb_stat_item item)
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -070062{
Tejun Heo9ce34202015-05-22 17:13:27 -040063 __add_wb_stat(wb, item, 1);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070064}
65
Tejun Heo9ce34202015-05-22 17:13:27 -040066static inline void inc_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070067{
68 unsigned long flags;
69
70 local_irq_save(flags);
Tejun Heo9ce34202015-05-22 17:13:27 -040071 __inc_wb_stat(wb, item);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070072 local_irq_restore(flags);
73}
74
Tejun Heo9ce34202015-05-22 17:13:27 -040075static inline void __dec_wb_stat(struct bdi_writeback *wb,
76 enum wb_stat_item item)
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070077{
Tejun Heo9ce34202015-05-22 17:13:27 -040078 __add_wb_stat(wb, item, -1);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070079}
80
Tejun Heo9ce34202015-05-22 17:13:27 -040081static inline void dec_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070082{
83 unsigned long flags;
84
85 local_irq_save(flags);
Tejun Heo9ce34202015-05-22 17:13:27 -040086 __dec_wb_stat(wb, item);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070087 local_irq_restore(flags);
88}
89
Tejun Heo9ce34202015-05-22 17:13:27 -040090static inline s64 wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070091{
Tejun Heo9ce34202015-05-22 17:13:27 -040092 return percpu_counter_read_positive(&wb->stat[item]);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070093}
94
Tejun Heo9ce34202015-05-22 17:13:27 -040095static inline s64 __wb_stat_sum(struct bdi_writeback *wb,
96 enum wb_stat_item item)
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070097{
Tejun Heo9ce34202015-05-22 17:13:27 -040098 return percpu_counter_sum_positive(&wb->stat[item]);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070099}
100
Tejun Heo9ce34202015-05-22 17:13:27 -0400101static inline s64 wb_stat_sum(struct bdi_writeback *wb, enum wb_stat_item item)
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700102{
103 s64 sum;
104 unsigned long flags;
105
106 local_irq_save(flags);
Tejun Heo9ce34202015-05-22 17:13:27 -0400107 sum = __wb_stat_sum(wb, item);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700108 local_irq_restore(flags);
109
110 return sum;
111}
112
Tejun Heo9ce34202015-05-22 17:13:27 -0400113extern void wb_writeout_inc(struct bdi_writeback *wb);
Miklos Szeredidd5656e2008-04-30 00:54:37 -0700114
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700115/*
116 * maximal error of a stat counter.
117 */
Tejun Heo9ce34202015-05-22 17:13:27 -0400118static inline unsigned long wb_stat_error(struct bdi_writeback *wb)
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700119{
120#ifdef CONFIG_SMP
Tejun Heo9ce34202015-05-22 17:13:27 -0400121 return nr_cpu_ids * WB_STAT_BATCH;
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700122#else
123 return 1;
124#endif
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700125}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700127int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio);
Peter Zijlstraa42dde02008-04-30 00:54:36 -0700128int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ratio);
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/*
131 * Flags in backing_dev_info::capability
Miklos Szeredie4ad08fe2008-04-30 00:54:37 -0700132 *
133 * The first three flags control whether dirty pages will contribute to the
134 * VM's accounting and whether writepages() should be called for dirty pages
135 * (something that would not, for example, be appropriate for ramfs)
136 *
137 * WARNING: these flags are closely related and should not normally be
138 * used separately. The BDI_CAP_NO_ACCT_AND_WRITEBACK combines these
139 * three flags into a single convenience macro.
140 *
141 * BDI_CAP_NO_ACCT_DIRTY: Dirty pages shouldn't contribute to accounting
142 * BDI_CAP_NO_WRITEBACK: Don't write pages back
143 * BDI_CAP_NO_ACCT_WB: Don't automatically account writeback pages
Maxim Patlasov5a537482013-09-11 14:22:46 -0700144 * BDI_CAP_STRICTLIMIT: Keep number of dirty pages below bdi threshold.
Tejun Heo0632c4c2015-05-22 17:13:36 -0400145 *
146 * BDI_CAP_CGROUP_WRITEBACK: Supports cgroup-aware writeback.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 */
Miklos Szeredie4ad08fe2008-04-30 00:54:37 -0700148#define BDI_CAP_NO_ACCT_DIRTY 0x00000001
149#define BDI_CAP_NO_WRITEBACK 0x00000002
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100150#define BDI_CAP_NO_ACCT_WB 0x00000004
151#define BDI_CAP_STABLE_WRITES 0x00000008
152#define BDI_CAP_STRICTLIMIT 0x00000010
Tejun Heo0632c4c2015-05-22 17:13:36 -0400153#define BDI_CAP_CGROUP_WRITEBACK 0x00000020
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Miklos Szeredie4ad08fe2008-04-30 00:54:37 -0700155#define BDI_CAP_NO_ACCT_AND_WRITEBACK \
156 (BDI_CAP_NO_WRITEBACK | BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_ACCT_WB)
157
Jörn Engel5129a462010-04-25 08:54:42 +0200158extern struct backing_dev_info noop_backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Tejun Heo876a21b2015-05-22 17:13:53 -0400160/**
161 * writeback_in_progress - determine whether there is writeback in progress
162 * @wb: bdi_writeback of interest
163 *
164 * Determine whether there is writeback waiting to be handled against a
165 * bdi_writeback.
166 */
167static inline bool writeback_in_progress(struct bdi_writeback *wb)
168{
169 return test_bit(WB_writeback_running, &wb->state);
170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Tejun Heo0b6d7572015-05-22 17:13:33 -0400172static inline struct backing_dev_info *inode_to_bdi(struct inode *inode)
173{
174 struct super_block *sb;
175
176 if (!inode)
177 return &noop_backing_dev_info;
178
179 sb = inode->i_sb;
180#ifdef CONFIG_BLOCK
181 if (sb_is_blkdev_sb(sb))
182 return blk_get_backing_dev_info(I_BDEV(inode));
183#endif
184 return sb->s_bdi;
185}
186
Tejun Heo5de85152015-05-22 17:13:41 -0400187static inline int wb_congested(struct bdi_writeback *wb, int cong_bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Tejun Heo5de85152015-05-22 17:13:41 -0400189 struct backing_dev_info *bdi = wb->bdi;
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (bdi->congested_fn)
Tejun Heo5de85152015-05-22 17:13:41 -0400192 return bdi->congested_fn(bdi->congested_data, cong_bits);
193 return wb->congested->state & cong_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Jens Axboe8aa7e842009-07-09 14:52:32 +0200196long congestion_wait(int sync, long timeout);
Mel Gorman0e093d992010-10-26 14:21:45 -0700197long wait_iff_congested(struct zone *zone, int sync, long timeout);
Wanpeng Li3965c9a2012-07-31 16:41:52 -0700198int pdflush_proc_obsolete(struct ctl_table *table, int write,
199 void __user *buffer, size_t *lenp, loff_t *ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Darrick J. Wong7d311cd2013-02-21 16:42:48 -0800201static inline bool bdi_cap_stable_pages_required(struct backing_dev_info *bdi)
202{
203 return bdi->capabilities & BDI_CAP_STABLE_WRITES;
204}
205
Miklos Szeredie4ad08fe2008-04-30 00:54:37 -0700206static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi)
207{
208 return !(bdi->capabilities & BDI_CAP_NO_WRITEBACK);
209}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Miklos Szeredie4ad08fe2008-04-30 00:54:37 -0700211static inline bool bdi_cap_account_dirty(struct backing_dev_info *bdi)
212{
213 return !(bdi->capabilities & BDI_CAP_NO_ACCT_DIRTY);
214}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Miklos Szeredie4ad08fe2008-04-30 00:54:37 -0700216static inline bool bdi_cap_account_writeback(struct backing_dev_info *bdi)
217{
218 /* Paranoia: BDI_CAP_NO_WRITEBACK implies BDI_CAP_NO_ACCT_WB */
219 return !(bdi->capabilities & (BDI_CAP_NO_ACCT_WB |
220 BDI_CAP_NO_WRITEBACK));
221}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Miklos Szeredie4ad08fe2008-04-30 00:54:37 -0700223static inline bool mapping_cap_writeback_dirty(struct address_space *mapping)
224{
Christoph Hellwigde1414a2015-01-14 10:42:36 +0100225 return bdi_cap_writeback_dirty(inode_to_bdi(mapping->host));
Miklos Szeredie4ad08fe2008-04-30 00:54:37 -0700226}
227
228static inline bool mapping_cap_account_dirty(struct address_space *mapping)
229{
Christoph Hellwigde1414a2015-01-14 10:42:36 +0100230 return bdi_cap_account_dirty(inode_to_bdi(mapping->host));
Miklos Szeredie4ad08fe2008-04-30 00:54:37 -0700231}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Jens Axboe03ba3782009-09-09 09:08:54 +0200233static inline int bdi_sched_wait(void *word)
234{
235 schedule();
236 return 0;
237}
238
Tejun Heo0632c4c2015-05-22 17:13:36 -0400239#ifdef CONFIG_CGROUP_WRITEBACK
240
Tejun Heo6b385782015-05-22 17:13:37 -0400241struct bdi_writeback_congested *
242wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp);
243void wb_congested_put(struct bdi_writeback_congested *congested);
244struct bdi_writeback *wb_get_create(struct backing_dev_info *bdi,
245 struct cgroup_subsys_state *memcg_css,
246 gfp_t gfp);
Tejun Heo6b385782015-05-22 17:13:37 -0400247void wb_memcg_offline(struct mem_cgroup *memcg);
248void wb_blkcg_offline(struct blkcg *blkcg);
Tejun Heo28928452015-05-22 17:13:44 -0400249int inode_congested(struct inode *inode, int cong_bits);
Tejun Heo6b385782015-05-22 17:13:37 -0400250
Tejun Heo0632c4c2015-05-22 17:13:36 -0400251/**
252 * inode_cgwb_enabled - test whether cgroup writeback is enabled on an inode
253 * @inode: inode of interest
254 *
255 * cgroup writeback requires support from both the bdi and filesystem.
Tejun Heo40d7d422015-09-23 17:07:29 -0400256 * Also, both memcg and iocg have to be on the default hierarchy. Test
257 * whether all conditions are met.
258 *
259 * Note that the test result may change dynamically on the same inode
260 * depending on how memcg and iocg are configured.
Tejun Heo0632c4c2015-05-22 17:13:36 -0400261 */
262static inline bool inode_cgwb_enabled(struct inode *inode)
263{
264 struct backing_dev_info *bdi = inode_to_bdi(inode);
265
Tejun Heoded8e212015-09-18 11:56:28 -0400266
267 return cgroup_subsys_on_dfl(memory_cgrp_subsys) &&
268 cgroup_subsys_on_dfl(io_cgrp_subsys) &&
Tejun Heo40d7d422015-09-23 17:07:29 -0400269 bdi_cap_account_dirty(bdi) &&
Tejun Heo0632c4c2015-05-22 17:13:36 -0400270 (bdi->capabilities & BDI_CAP_CGROUP_WRITEBACK) &&
Tejun Heo1a4adc82015-06-16 18:48:31 -0400271 (inode->i_sb->s_iflags & SB_I_CGROUPWB);
Tejun Heo0632c4c2015-05-22 17:13:36 -0400272}
273
Tejun Heo6b385782015-05-22 17:13:37 -0400274/**
Tejun Heo6b385782015-05-22 17:13:37 -0400275 * wb_find_current - find wb for %current on a bdi
276 * @bdi: bdi of interest
277 *
278 * Find the wb of @bdi which matches both the memcg and blkcg of %current.
279 * Must be called under rcu_read_lock() which protects the returend wb.
280 * NULL if not found.
281 */
282static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
283{
284 struct cgroup_subsys_state *memcg_css;
285 struct bdi_writeback *wb;
286
287 memcg_css = task_css(current, memory_cgrp_id);
288 if (!memcg_css->parent)
289 return &bdi->wb;
290
291 wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
292
293 /*
294 * %current's blkcg equals the effective blkcg of its memcg. No
295 * need to use the relatively expensive cgroup_get_e_css().
296 */
Tejun Heoa3648d92015-08-18 14:55:29 -0700297 if (likely(wb && wb->blkcg_css == task_css(current, io_cgrp_id)))
Tejun Heo6b385782015-05-22 17:13:37 -0400298 return wb;
299 return NULL;
300}
301
302/**
303 * wb_get_create_current - get or create wb for %current on a bdi
304 * @bdi: bdi of interest
305 * @gfp: allocation mask
306 *
307 * Equivalent to wb_get_create() on %current's memcg. This function is
308 * called from a relatively hot path and optimizes the common cases using
309 * wb_find_current().
310 */
311static inline struct bdi_writeback *
312wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
313{
314 struct bdi_writeback *wb;
315
316 rcu_read_lock();
317 wb = wb_find_current(bdi);
318 if (wb && unlikely(!wb_tryget(wb)))
319 wb = NULL;
320 rcu_read_unlock();
321
322 if (unlikely(!wb)) {
323 struct cgroup_subsys_state *memcg_css;
324
325 memcg_css = task_get_css(current, memory_cgrp_id);
326 wb = wb_get_create(bdi, memcg_css, gfp);
327 css_put(memcg_css);
328 }
329 return wb;
330}
331
332/**
Tejun Heo67332a62015-05-28 14:50:55 -0400333 * inode_to_wb_is_valid - test whether an inode has a wb associated
334 * @inode: inode of interest
335 *
336 * Returns %true if @inode has a wb associated. May be called without any
337 * locking.
338 */
339static inline bool inode_to_wb_is_valid(struct inode *inode)
340{
341 return inode->i_wb;
342}
343
344/**
Tejun Heo6b385782015-05-22 17:13:37 -0400345 * inode_to_wb - determine the wb of an inode
346 * @inode: inode of interest
347 *
Tejun Heo67332a62015-05-28 14:50:55 -0400348 * Returns the wb @inode is currently associated with. The caller must be
349 * holding either @inode->i_lock, @inode->i_mapping->tree_lock, or the
350 * associated wb's list_lock.
Tejun Heo6b385782015-05-22 17:13:37 -0400351 */
352static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
353{
Tejun Heo67332a62015-05-28 14:50:55 -0400354#ifdef CONFIG_LOCKDEP
355 WARN_ON_ONCE(debug_locks &&
356 (!lockdep_is_held(&inode->i_lock) &&
357 !lockdep_is_held(&inode->i_mapping->tree_lock) &&
358 !lockdep_is_held(&inode->i_wb->list_lock)));
359#endif
Tejun Heo6b385782015-05-22 17:13:37 -0400360 return inode->i_wb;
361}
362
Tejun Heob034f6f2015-05-28 14:50:53 -0400363/**
364 * unlocked_inode_to_wb_begin - begin unlocked inode wb access transaction
365 * @inode: target inode
366 * @lockedp: temp bool output param, to be passed to the end function
367 *
368 * The caller wants to access the wb associated with @inode but isn't
369 * holding inode->i_lock, mapping->tree_lock or wb->list_lock. This
370 * function determines the wb associated with @inode and ensures that the
371 * association doesn't change until the transaction is finished with
372 * unlocked_inode_to_wb_end().
373 *
374 * The caller must call unlocked_inode_to_wb_end() with *@lockdep
375 * afterwards and can't sleep during transaction. IRQ may or may not be
376 * disabled on return.
377 */
378static inline struct bdi_writeback *
379unlocked_inode_to_wb_begin(struct inode *inode, bool *lockedp)
380{
381 rcu_read_lock();
382
383 /*
384 * Paired with store_release in inode_switch_wb_work_fn() and
385 * ensures that we see the new wb if we see cleared I_WB_SWITCH.
386 */
387 *lockedp = smp_load_acquire(&inode->i_state) & I_WB_SWITCH;
388
389 if (unlikely(*lockedp))
390 spin_lock_irq(&inode->i_mapping->tree_lock);
Tejun Heo67332a62015-05-28 14:50:55 -0400391
392 /*
393 * Protected by either !I_WB_SWITCH + rcu_read_lock() or tree_lock.
394 * inode_to_wb() will bark. Deref directly.
395 */
396 return inode->i_wb;
Tejun Heob034f6f2015-05-28 14:50:53 -0400397}
398
399/**
400 * unlocked_inode_to_wb_end - end inode wb access transaction
401 * @inode: target inode
402 * @locked: *@lockedp from unlocked_inode_to_wb_begin()
403 */
404static inline void unlocked_inode_to_wb_end(struct inode *inode, bool locked)
405{
406 if (unlikely(locked))
407 spin_unlock_irq(&inode->i_mapping->tree_lock);
408
409 rcu_read_unlock();
410}
411
Tejun Heob3997c82015-05-22 17:13:50 -0400412struct wb_iter {
Tejun Heob2eaa012015-08-18 14:54:52 -0700413 int start_memcg_id;
Tejun Heob3997c82015-05-22 17:13:50 -0400414 struct radix_tree_iter tree_iter;
415 void **slot;
416};
417
418static inline struct bdi_writeback *__wb_iter_next(struct wb_iter *iter,
419 struct backing_dev_info *bdi)
420{
421 struct radix_tree_iter *titer = &iter->tree_iter;
422
423 WARN_ON_ONCE(!rcu_read_lock_held());
424
Tejun Heob2eaa012015-08-18 14:54:52 -0700425 if (iter->start_memcg_id >= 0) {
426 iter->slot = radix_tree_iter_init(titer, iter->start_memcg_id);
427 iter->start_memcg_id = -1;
Tejun Heob3997c82015-05-22 17:13:50 -0400428 } else {
429 iter->slot = radix_tree_next_slot(iter->slot, titer, 0);
430 }
431
432 if (!iter->slot)
433 iter->slot = radix_tree_next_chunk(&bdi->cgwb_tree, titer, 0);
434 if (iter->slot)
435 return *iter->slot;
436 return NULL;
437}
438
439static inline struct bdi_writeback *__wb_iter_init(struct wb_iter *iter,
440 struct backing_dev_info *bdi,
Tejun Heob2eaa012015-08-18 14:54:52 -0700441 int start_memcg_id)
Tejun Heob3997c82015-05-22 17:13:50 -0400442{
Tejun Heob2eaa012015-08-18 14:54:52 -0700443 iter->start_memcg_id = start_memcg_id;
Tejun Heob3997c82015-05-22 17:13:50 -0400444
Tejun Heob2eaa012015-08-18 14:54:52 -0700445 if (start_memcg_id)
Tejun Heob3997c82015-05-22 17:13:50 -0400446 return __wb_iter_next(iter, bdi);
447 else
448 return &bdi->wb;
449}
450
451/**
Tejun Heob2eaa012015-08-18 14:54:52 -0700452 * bdi_for_each_wb - walk all wb's of a bdi in ascending memcg ID order
Tejun Heob3997c82015-05-22 17:13:50 -0400453 * @wb_cur: cursor struct bdi_writeback pointer
454 * @bdi: bdi to walk wb's of
455 * @iter: pointer to struct wb_iter to be used as iteration buffer
Tejun Heob2eaa012015-08-18 14:54:52 -0700456 * @start_memcg_id: memcg ID to start iteration from
Tejun Heob3997c82015-05-22 17:13:50 -0400457 *
458 * Iterate @wb_cur through the wb's (bdi_writeback's) of @bdi in ascending
Tejun Heob2eaa012015-08-18 14:54:52 -0700459 * memcg ID order starting from @start_memcg_id. @iter is struct wb_iter
Tejun Heob3997c82015-05-22 17:13:50 -0400460 * to be used as temp storage during iteration. rcu_read_lock() must be
461 * held throughout iteration.
462 */
Tejun Heob2eaa012015-08-18 14:54:52 -0700463#define bdi_for_each_wb(wb_cur, bdi, iter, start_memcg_id) \
464 for ((wb_cur) = __wb_iter_init(iter, bdi, start_memcg_id); \
Tejun Heob3997c82015-05-22 17:13:50 -0400465 (wb_cur); (wb_cur) = __wb_iter_next(iter, bdi))
466
Tejun Heo0632c4c2015-05-22 17:13:36 -0400467#else /* CONFIG_CGROUP_WRITEBACK */
468
469static inline bool inode_cgwb_enabled(struct inode *inode)
470{
471 return false;
472}
473
Tejun Heo6b385782015-05-22 17:13:37 -0400474static inline struct bdi_writeback_congested *
475wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp)
476{
Tejun Heod443edc2015-07-02 08:44:34 -0600477 atomic_inc(&bdi->wb_congested->refcnt);
478 return bdi->wb_congested;
Tejun Heo6b385782015-05-22 17:13:37 -0400479}
480
481static inline void wb_congested_put(struct bdi_writeback_congested *congested)
482{
Tejun Heod443edc2015-07-02 08:44:34 -0600483 if (atomic_dec_and_test(&congested->refcnt))
484 kfree(congested);
Tejun Heo6b385782015-05-22 17:13:37 -0400485}
486
Tejun Heo6b385782015-05-22 17:13:37 -0400487static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
488{
489 return &bdi->wb;
490}
491
492static inline struct bdi_writeback *
493wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
494{
495 return &bdi->wb;
496}
497
Tejun Heo67332a62015-05-28 14:50:55 -0400498static inline bool inode_to_wb_is_valid(struct inode *inode)
499{
500 return true;
501}
502
Tejun Heo6b385782015-05-22 17:13:37 -0400503static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
504{
505 return &inode_to_bdi(inode)->wb;
506}
507
Tejun Heob034f6f2015-05-28 14:50:53 -0400508static inline struct bdi_writeback *
509unlocked_inode_to_wb_begin(struct inode *inode, bool *lockedp)
510{
511 return inode_to_wb(inode);
512}
513
514static inline void unlocked_inode_to_wb_end(struct inode *inode, bool locked)
515{
516}
517
Tejun Heo6b385782015-05-22 17:13:37 -0400518static inline void wb_memcg_offline(struct mem_cgroup *memcg)
519{
520}
521
522static inline void wb_blkcg_offline(struct blkcg *blkcg)
523{
524}
525
Tejun Heob3997c82015-05-22 17:13:50 -0400526struct wb_iter {
527 int next_id;
528};
529
530#define bdi_for_each_wb(wb_cur, bdi, iter, start_blkcg_id) \
531 for ((iter)->next_id = (start_blkcg_id); \
532 ({ (wb_cur) = !(iter)->next_id++ ? &(bdi)->wb : NULL; }); )
533
Tejun Heo28928452015-05-22 17:13:44 -0400534static inline int inode_congested(struct inode *inode, int cong_bits)
535{
536 return wb_congested(&inode_to_bdi(inode)->wb, cong_bits);
537}
538
Tejun Heo0632c4c2015-05-22 17:13:36 -0400539#endif /* CONFIG_CGROUP_WRITEBACK */
540
Tejun Heo28928452015-05-22 17:13:44 -0400541static inline int inode_read_congested(struct inode *inode)
542{
543 return inode_congested(inode, 1 << WB_sync_congested);
544}
545
546static inline int inode_write_congested(struct inode *inode)
547{
548 return inode_congested(inode, 1 << WB_async_congested);
549}
550
551static inline int inode_rw_congested(struct inode *inode)
552{
553 return inode_congested(inode, (1 << WB_sync_congested) |
554 (1 << WB_async_congested));
555}
556
Tejun Heo5de85152015-05-22 17:13:41 -0400557static inline int bdi_congested(struct backing_dev_info *bdi, int cong_bits)
558{
559 return wb_congested(&bdi->wb, cong_bits);
560}
561
562static inline int bdi_read_congested(struct backing_dev_info *bdi)
563{
564 return bdi_congested(bdi, 1 << WB_sync_congested);
565}
566
567static inline int bdi_write_congested(struct backing_dev_info *bdi)
568{
569 return bdi_congested(bdi, 1 << WB_async_congested);
570}
571
572static inline int bdi_rw_congested(struct backing_dev_info *bdi)
573{
574 return bdi_congested(bdi, (1 << WB_sync_congested) |
575 (1 << WB_async_congested));
576}
577
Tejun Heo0632c4c2015-05-22 17:13:36 -0400578#endif /* _LINUX_BACKING_DEV_H */