Merge tag 'nfs-for-4.2-1' of git://git.linux-nfs.org/projects/trondmy/linux-nfs
[deliverable/linux.git] / include / linux / backing-dev.h
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
11 #include <linux/kernel.h>
12 #include <linux/fs.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>
18
19 int __must_check bdi_init(struct backing_dev_info *bdi);
20 void bdi_destroy(struct backing_dev_info *bdi);
21
22 __printf(3, 4)
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 int __must_check bdi_setup_and_register(struct backing_dev_info *, char *);
27 void wb_start_writeback(struct bdi_writeback *wb, long nr_pages,
28 bool range_cyclic, enum wb_reason reason);
29 void wb_start_background_writeback(struct bdi_writeback *wb);
30 void wb_workfn(struct work_struct *work);
31 void wb_wakeup_delayed(struct bdi_writeback *wb);
32
33 extern spinlock_t bdi_lock;
34 extern struct list_head bdi_list;
35
36 extern struct workqueue_struct *bdi_wq;
37
38 static inline bool wb_has_dirty_io(struct bdi_writeback *wb)
39 {
40 return test_bit(WB_has_dirty_io, &wb->state);
41 }
42
43 static inline bool bdi_has_dirty_io(struct backing_dev_info *bdi)
44 {
45 /*
46 * @bdi->tot_write_bandwidth is guaranteed to be > 0 if there are
47 * any dirty wbs. See wb_update_write_bandwidth().
48 */
49 return atomic_long_read(&bdi->tot_write_bandwidth);
50 }
51
52 static inline void __add_wb_stat(struct bdi_writeback *wb,
53 enum wb_stat_item item, s64 amount)
54 {
55 __percpu_counter_add(&wb->stat[item], amount, WB_STAT_BATCH);
56 }
57
58 static inline void __inc_wb_stat(struct bdi_writeback *wb,
59 enum wb_stat_item item)
60 {
61 __add_wb_stat(wb, item, 1);
62 }
63
64 static inline void inc_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
65 {
66 unsigned long flags;
67
68 local_irq_save(flags);
69 __inc_wb_stat(wb, item);
70 local_irq_restore(flags);
71 }
72
73 static inline void __dec_wb_stat(struct bdi_writeback *wb,
74 enum wb_stat_item item)
75 {
76 __add_wb_stat(wb, item, -1);
77 }
78
79 static inline void dec_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
80 {
81 unsigned long flags;
82
83 local_irq_save(flags);
84 __dec_wb_stat(wb, item);
85 local_irq_restore(flags);
86 }
87
88 static inline s64 wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
89 {
90 return percpu_counter_read_positive(&wb->stat[item]);
91 }
92
93 static inline s64 __wb_stat_sum(struct bdi_writeback *wb,
94 enum wb_stat_item item)
95 {
96 return percpu_counter_sum_positive(&wb->stat[item]);
97 }
98
99 static inline s64 wb_stat_sum(struct bdi_writeback *wb, enum wb_stat_item item)
100 {
101 s64 sum;
102 unsigned long flags;
103
104 local_irq_save(flags);
105 sum = __wb_stat_sum(wb, item);
106 local_irq_restore(flags);
107
108 return sum;
109 }
110
111 extern void wb_writeout_inc(struct bdi_writeback *wb);
112
113 /*
114 * maximal error of a stat counter.
115 */
116 static inline unsigned long wb_stat_error(struct bdi_writeback *wb)
117 {
118 #ifdef CONFIG_SMP
119 return nr_cpu_ids * WB_STAT_BATCH;
120 #else
121 return 1;
122 #endif
123 }
124
125 int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio);
126 int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ratio);
127
128 /*
129 * Flags in backing_dev_info::capability
130 *
131 * The first three flags control whether dirty pages will contribute to the
132 * VM's accounting and whether writepages() should be called for dirty pages
133 * (something that would not, for example, be appropriate for ramfs)
134 *
135 * WARNING: these flags are closely related and should not normally be
136 * used separately. The BDI_CAP_NO_ACCT_AND_WRITEBACK combines these
137 * three flags into a single convenience macro.
138 *
139 * BDI_CAP_NO_ACCT_DIRTY: Dirty pages shouldn't contribute to accounting
140 * BDI_CAP_NO_WRITEBACK: Don't write pages back
141 * BDI_CAP_NO_ACCT_WB: Don't automatically account writeback pages
142 * BDI_CAP_STRICTLIMIT: Keep number of dirty pages below bdi threshold.
143 *
144 * BDI_CAP_CGROUP_WRITEBACK: Supports cgroup-aware writeback.
145 */
146 #define BDI_CAP_NO_ACCT_DIRTY 0x00000001
147 #define BDI_CAP_NO_WRITEBACK 0x00000002
148 #define BDI_CAP_NO_ACCT_WB 0x00000004
149 #define BDI_CAP_STABLE_WRITES 0x00000008
150 #define BDI_CAP_STRICTLIMIT 0x00000010
151 #define BDI_CAP_CGROUP_WRITEBACK 0x00000020
152
153 #define BDI_CAP_NO_ACCT_AND_WRITEBACK \
154 (BDI_CAP_NO_WRITEBACK | BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_ACCT_WB)
155
156 extern struct backing_dev_info noop_backing_dev_info;
157
158 /**
159 * writeback_in_progress - determine whether there is writeback in progress
160 * @wb: bdi_writeback of interest
161 *
162 * Determine whether there is writeback waiting to be handled against a
163 * bdi_writeback.
164 */
165 static inline bool writeback_in_progress(struct bdi_writeback *wb)
166 {
167 return test_bit(WB_writeback_running, &wb->state);
168 }
169
170 static inline struct backing_dev_info *inode_to_bdi(struct inode *inode)
171 {
172 struct super_block *sb;
173
174 if (!inode)
175 return &noop_backing_dev_info;
176
177 sb = inode->i_sb;
178 #ifdef CONFIG_BLOCK
179 if (sb_is_blkdev_sb(sb))
180 return blk_get_backing_dev_info(I_BDEV(inode));
181 #endif
182 return sb->s_bdi;
183 }
184
185 static inline int wb_congested(struct bdi_writeback *wb, int cong_bits)
186 {
187 struct backing_dev_info *bdi = wb->bdi;
188
189 if (bdi->congested_fn)
190 return bdi->congested_fn(bdi->congested_data, cong_bits);
191 return wb->congested->state & cong_bits;
192 }
193
194 long congestion_wait(int sync, long timeout);
195 long wait_iff_congested(struct zone *zone, int sync, long timeout);
196 int pdflush_proc_obsolete(struct ctl_table *table, int write,
197 void __user *buffer, size_t *lenp, loff_t *ppos);
198
199 static inline bool bdi_cap_stable_pages_required(struct backing_dev_info *bdi)
200 {
201 return bdi->capabilities & BDI_CAP_STABLE_WRITES;
202 }
203
204 static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi)
205 {
206 return !(bdi->capabilities & BDI_CAP_NO_WRITEBACK);
207 }
208
209 static inline bool bdi_cap_account_dirty(struct backing_dev_info *bdi)
210 {
211 return !(bdi->capabilities & BDI_CAP_NO_ACCT_DIRTY);
212 }
213
214 static inline bool bdi_cap_account_writeback(struct backing_dev_info *bdi)
215 {
216 /* Paranoia: BDI_CAP_NO_WRITEBACK implies BDI_CAP_NO_ACCT_WB */
217 return !(bdi->capabilities & (BDI_CAP_NO_ACCT_WB |
218 BDI_CAP_NO_WRITEBACK));
219 }
220
221 static inline bool mapping_cap_writeback_dirty(struct address_space *mapping)
222 {
223 return bdi_cap_writeback_dirty(inode_to_bdi(mapping->host));
224 }
225
226 static inline bool mapping_cap_account_dirty(struct address_space *mapping)
227 {
228 return bdi_cap_account_dirty(inode_to_bdi(mapping->host));
229 }
230
231 static inline int bdi_sched_wait(void *word)
232 {
233 schedule();
234 return 0;
235 }
236
237 #ifdef CONFIG_CGROUP_WRITEBACK
238
239 struct bdi_writeback_congested *
240 wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp);
241 void wb_congested_put(struct bdi_writeback_congested *congested);
242 struct bdi_writeback *wb_get_create(struct backing_dev_info *bdi,
243 struct cgroup_subsys_state *memcg_css,
244 gfp_t gfp);
245 void wb_memcg_offline(struct mem_cgroup *memcg);
246 void wb_blkcg_offline(struct blkcg *blkcg);
247 int inode_congested(struct inode *inode, int cong_bits);
248
249 /**
250 * inode_cgwb_enabled - test whether cgroup writeback is enabled on an inode
251 * @inode: inode of interest
252 *
253 * cgroup writeback requires support from both the bdi and filesystem.
254 * Test whether @inode has both.
255 */
256 static inline bool inode_cgwb_enabled(struct inode *inode)
257 {
258 struct backing_dev_info *bdi = inode_to_bdi(inode);
259
260 return bdi_cap_account_dirty(bdi) &&
261 (bdi->capabilities & BDI_CAP_CGROUP_WRITEBACK) &&
262 (inode->i_sb->s_iflags & SB_I_CGROUPWB);
263 }
264
265 /**
266 * wb_find_current - find wb for %current on a bdi
267 * @bdi: bdi of interest
268 *
269 * Find the wb of @bdi which matches both the memcg and blkcg of %current.
270 * Must be called under rcu_read_lock() which protects the returend wb.
271 * NULL if not found.
272 */
273 static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
274 {
275 struct cgroup_subsys_state *memcg_css;
276 struct bdi_writeback *wb;
277
278 memcg_css = task_css(current, memory_cgrp_id);
279 if (!memcg_css->parent)
280 return &bdi->wb;
281
282 wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
283
284 /*
285 * %current's blkcg equals the effective blkcg of its memcg. No
286 * need to use the relatively expensive cgroup_get_e_css().
287 */
288 if (likely(wb && wb->blkcg_css == task_css(current, blkio_cgrp_id)))
289 return wb;
290 return NULL;
291 }
292
293 /**
294 * wb_get_create_current - get or create wb for %current on a bdi
295 * @bdi: bdi of interest
296 * @gfp: allocation mask
297 *
298 * Equivalent to wb_get_create() on %current's memcg. This function is
299 * called from a relatively hot path and optimizes the common cases using
300 * wb_find_current().
301 */
302 static inline struct bdi_writeback *
303 wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
304 {
305 struct bdi_writeback *wb;
306
307 rcu_read_lock();
308 wb = wb_find_current(bdi);
309 if (wb && unlikely(!wb_tryget(wb)))
310 wb = NULL;
311 rcu_read_unlock();
312
313 if (unlikely(!wb)) {
314 struct cgroup_subsys_state *memcg_css;
315
316 memcg_css = task_get_css(current, memory_cgrp_id);
317 wb = wb_get_create(bdi, memcg_css, gfp);
318 css_put(memcg_css);
319 }
320 return wb;
321 }
322
323 /**
324 * inode_to_wb_is_valid - test whether an inode has a wb associated
325 * @inode: inode of interest
326 *
327 * Returns %true if @inode has a wb associated. May be called without any
328 * locking.
329 */
330 static inline bool inode_to_wb_is_valid(struct inode *inode)
331 {
332 return inode->i_wb;
333 }
334
335 /**
336 * inode_to_wb - determine the wb of an inode
337 * @inode: inode of interest
338 *
339 * Returns the wb @inode is currently associated with. The caller must be
340 * holding either @inode->i_lock, @inode->i_mapping->tree_lock, or the
341 * associated wb's list_lock.
342 */
343 static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
344 {
345 #ifdef CONFIG_LOCKDEP
346 WARN_ON_ONCE(debug_locks &&
347 (!lockdep_is_held(&inode->i_lock) &&
348 !lockdep_is_held(&inode->i_mapping->tree_lock) &&
349 !lockdep_is_held(&inode->i_wb->list_lock)));
350 #endif
351 return inode->i_wb;
352 }
353
354 /**
355 * unlocked_inode_to_wb_begin - begin unlocked inode wb access transaction
356 * @inode: target inode
357 * @lockedp: temp bool output param, to be passed to the end function
358 *
359 * The caller wants to access the wb associated with @inode but isn't
360 * holding inode->i_lock, mapping->tree_lock or wb->list_lock. This
361 * function determines the wb associated with @inode and ensures that the
362 * association doesn't change until the transaction is finished with
363 * unlocked_inode_to_wb_end().
364 *
365 * The caller must call unlocked_inode_to_wb_end() with *@lockdep
366 * afterwards and can't sleep during transaction. IRQ may or may not be
367 * disabled on return.
368 */
369 static inline struct bdi_writeback *
370 unlocked_inode_to_wb_begin(struct inode *inode, bool *lockedp)
371 {
372 rcu_read_lock();
373
374 /*
375 * Paired with store_release in inode_switch_wb_work_fn() and
376 * ensures that we see the new wb if we see cleared I_WB_SWITCH.
377 */
378 *lockedp = smp_load_acquire(&inode->i_state) & I_WB_SWITCH;
379
380 if (unlikely(*lockedp))
381 spin_lock_irq(&inode->i_mapping->tree_lock);
382
383 /*
384 * Protected by either !I_WB_SWITCH + rcu_read_lock() or tree_lock.
385 * inode_to_wb() will bark. Deref directly.
386 */
387 return inode->i_wb;
388 }
389
390 /**
391 * unlocked_inode_to_wb_end - end inode wb access transaction
392 * @inode: target inode
393 * @locked: *@lockedp from unlocked_inode_to_wb_begin()
394 */
395 static inline void unlocked_inode_to_wb_end(struct inode *inode, bool locked)
396 {
397 if (unlikely(locked))
398 spin_unlock_irq(&inode->i_mapping->tree_lock);
399
400 rcu_read_unlock();
401 }
402
403 struct wb_iter {
404 int start_blkcg_id;
405 struct radix_tree_iter tree_iter;
406 void **slot;
407 };
408
409 static inline struct bdi_writeback *__wb_iter_next(struct wb_iter *iter,
410 struct backing_dev_info *bdi)
411 {
412 struct radix_tree_iter *titer = &iter->tree_iter;
413
414 WARN_ON_ONCE(!rcu_read_lock_held());
415
416 if (iter->start_blkcg_id >= 0) {
417 iter->slot = radix_tree_iter_init(titer, iter->start_blkcg_id);
418 iter->start_blkcg_id = -1;
419 } else {
420 iter->slot = radix_tree_next_slot(iter->slot, titer, 0);
421 }
422
423 if (!iter->slot)
424 iter->slot = radix_tree_next_chunk(&bdi->cgwb_tree, titer, 0);
425 if (iter->slot)
426 return *iter->slot;
427 return NULL;
428 }
429
430 static inline struct bdi_writeback *__wb_iter_init(struct wb_iter *iter,
431 struct backing_dev_info *bdi,
432 int start_blkcg_id)
433 {
434 iter->start_blkcg_id = start_blkcg_id;
435
436 if (start_blkcg_id)
437 return __wb_iter_next(iter, bdi);
438 else
439 return &bdi->wb;
440 }
441
442 /**
443 * bdi_for_each_wb - walk all wb's of a bdi in ascending blkcg ID order
444 * @wb_cur: cursor struct bdi_writeback pointer
445 * @bdi: bdi to walk wb's of
446 * @iter: pointer to struct wb_iter to be used as iteration buffer
447 * @start_blkcg_id: blkcg ID to start iteration from
448 *
449 * Iterate @wb_cur through the wb's (bdi_writeback's) of @bdi in ascending
450 * blkcg ID order starting from @start_blkcg_id. @iter is struct wb_iter
451 * to be used as temp storage during iteration. rcu_read_lock() must be
452 * held throughout iteration.
453 */
454 #define bdi_for_each_wb(wb_cur, bdi, iter, start_blkcg_id) \
455 for ((wb_cur) = __wb_iter_init(iter, bdi, start_blkcg_id); \
456 (wb_cur); (wb_cur) = __wb_iter_next(iter, bdi))
457
458 #else /* CONFIG_CGROUP_WRITEBACK */
459
460 static inline bool inode_cgwb_enabled(struct inode *inode)
461 {
462 return false;
463 }
464
465 static inline struct bdi_writeback_congested *
466 wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp)
467 {
468 return bdi->wb.congested;
469 }
470
471 static inline void wb_congested_put(struct bdi_writeback_congested *congested)
472 {
473 }
474
475 static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
476 {
477 return &bdi->wb;
478 }
479
480 static inline struct bdi_writeback *
481 wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
482 {
483 return &bdi->wb;
484 }
485
486 static inline bool inode_to_wb_is_valid(struct inode *inode)
487 {
488 return true;
489 }
490
491 static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
492 {
493 return &inode_to_bdi(inode)->wb;
494 }
495
496 static inline struct bdi_writeback *
497 unlocked_inode_to_wb_begin(struct inode *inode, bool *lockedp)
498 {
499 return inode_to_wb(inode);
500 }
501
502 static inline void unlocked_inode_to_wb_end(struct inode *inode, bool locked)
503 {
504 }
505
506 static inline void wb_memcg_offline(struct mem_cgroup *memcg)
507 {
508 }
509
510 static inline void wb_blkcg_offline(struct blkcg *blkcg)
511 {
512 }
513
514 struct wb_iter {
515 int next_id;
516 };
517
518 #define bdi_for_each_wb(wb_cur, bdi, iter, start_blkcg_id) \
519 for ((iter)->next_id = (start_blkcg_id); \
520 ({ (wb_cur) = !(iter)->next_id++ ? &(bdi)->wb : NULL; }); )
521
522 static inline int inode_congested(struct inode *inode, int cong_bits)
523 {
524 return wb_congested(&inode_to_bdi(inode)->wb, cong_bits);
525 }
526
527 #endif /* CONFIG_CGROUP_WRITEBACK */
528
529 static inline int inode_read_congested(struct inode *inode)
530 {
531 return inode_congested(inode, 1 << WB_sync_congested);
532 }
533
534 static inline int inode_write_congested(struct inode *inode)
535 {
536 return inode_congested(inode, 1 << WB_async_congested);
537 }
538
539 static inline int inode_rw_congested(struct inode *inode)
540 {
541 return inode_congested(inode, (1 << WB_sync_congested) |
542 (1 << WB_async_congested));
543 }
544
545 static inline int bdi_congested(struct backing_dev_info *bdi, int cong_bits)
546 {
547 return wb_congested(&bdi->wb, cong_bits);
548 }
549
550 static inline int bdi_read_congested(struct backing_dev_info *bdi)
551 {
552 return bdi_congested(bdi, 1 << WB_sync_congested);
553 }
554
555 static inline int bdi_write_congested(struct backing_dev_info *bdi)
556 {
557 return bdi_congested(bdi, 1 << WB_async_congested);
558 }
559
560 static inline int bdi_rw_congested(struct backing_dev_info *bdi)
561 {
562 return bdi_congested(bdi, (1 << WB_sync_congested) |
563 (1 << WB_async_congested));
564 }
565
566 #endif /* _LINUX_BACKING_DEV_H */
This page took 0.0678 seconds and 6 git commands to generate.