]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/linux/backing-dev.h
Merge tag 'devicetree-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-jammy-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>
de1414a6 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);
b4caecd4 26int __must_check bdi_setup_and_register(struct backing_dev_info *, char *);
c00ddad3
TH
27void wb_start_writeback(struct bdi_writeback *wb, long nr_pages,
28 bool range_cyclic, enum wb_reason reason);
9ecf4866 29void wb_start_background_writeback(struct bdi_writeback *wb);
f0054bb1 30void wb_workfn(struct work_struct *work);
f0054bb1 31void wb_wakeup_delayed(struct bdi_writeback *wb);
cf0ca9fe 32
03ba3782 33extern spinlock_t bdi_lock;
66f3b8e2
JA
34extern struct list_head bdi_list;
35
839a8e86
TH
36extern struct workqueue_struct *bdi_wq;
37
d6c10f1f 38static inline bool wb_has_dirty_io(struct bdi_writeback *wb)
03ba3782 39{
d6c10f1f 40 return test_bit(WB_has_dirty_io, &wb->state);
03ba3782
JA
41}
42
95a46c65
TH
43static 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);
03ba3782
JA
50}
51
93f78d88
TH
52static inline void __add_wb_stat(struct bdi_writeback *wb,
53 enum wb_stat_item item, s64 amount)
b2e8fb6e 54{
93f78d88 55 __percpu_counter_add(&wb->stat[item], amount, WB_STAT_BATCH);
b2e8fb6e
PZ
56}
57
93f78d88
TH
58static inline void __inc_wb_stat(struct bdi_writeback *wb,
59 enum wb_stat_item item)
b2e8fb6e 60{
93f78d88 61 __add_wb_stat(wb, item, 1);
b2e8fb6e
PZ
62}
63
93f78d88 64static inline void inc_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
b2e8fb6e
PZ
65{
66 unsigned long flags;
67
68 local_irq_save(flags);
93f78d88 69 __inc_wb_stat(wb, item);
b2e8fb6e
PZ
70 local_irq_restore(flags);
71}
72
93f78d88
TH
73static inline void __dec_wb_stat(struct bdi_writeback *wb,
74 enum wb_stat_item item)
b2e8fb6e 75{
93f78d88 76 __add_wb_stat(wb, item, -1);
b2e8fb6e
PZ
77}
78
93f78d88 79static inline void dec_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
b2e8fb6e
PZ
80{
81 unsigned long flags;
82
83 local_irq_save(flags);
93f78d88 84 __dec_wb_stat(wb, item);
b2e8fb6e
PZ
85 local_irq_restore(flags);
86}
87
93f78d88 88static inline s64 wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
b2e8fb6e 89{
93f78d88 90 return percpu_counter_read_positive(&wb->stat[item]);
b2e8fb6e
PZ
91}
92
93f78d88
TH
93static inline s64 __wb_stat_sum(struct bdi_writeback *wb,
94 enum wb_stat_item item)
b2e8fb6e 95{
93f78d88 96 return percpu_counter_sum_positive(&wb->stat[item]);
b2e8fb6e
PZ
97}
98
93f78d88 99static inline s64 wb_stat_sum(struct bdi_writeback *wb, enum wb_stat_item item)
e0bf68dd 100{
b2e8fb6e
PZ
101 s64 sum;
102 unsigned long flags;
103
104 local_irq_save(flags);
93f78d88 105 sum = __wb_stat_sum(wb, item);
b2e8fb6e
PZ
106 local_irq_restore(flags);
107
108 return sum;
e0bf68dd
PZ
109}
110
93f78d88 111extern void wb_writeout_inc(struct bdi_writeback *wb);
dd5656e5 112
b2e8fb6e
PZ
113/*
114 * maximal error of a stat counter.
115 */
93f78d88 116static inline unsigned long wb_stat_error(struct bdi_writeback *wb)
e0bf68dd 117{
b2e8fb6e 118#ifdef CONFIG_SMP
93f78d88 119 return nr_cpu_ids * WB_STAT_BATCH;
b2e8fb6e
PZ
120#else
121 return 1;
122#endif
e0bf68dd 123}
1da177e4 124
189d3c4a 125int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio);
a42dde04 126int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ratio);
189d3c4a 127
1da177e4
LT
128/*
129 * Flags in backing_dev_info::capability
e4ad08fe
MS
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
5a537485 142 * BDI_CAP_STRICTLIMIT: Keep number of dirty pages below bdi threshold.
89e9b9e0
TH
143 *
144 * BDI_CAP_CGROUP_WRITEBACK: Supports cgroup-aware writeback.
1da177e4 145 */
e4ad08fe
MS
146#define BDI_CAP_NO_ACCT_DIRTY 0x00000001
147#define BDI_CAP_NO_WRITEBACK 0x00000002
b4caecd4
CH
148#define BDI_CAP_NO_ACCT_WB 0x00000004
149#define BDI_CAP_STABLE_WRITES 0x00000008
150#define BDI_CAP_STRICTLIMIT 0x00000010
89e9b9e0 151#define BDI_CAP_CGROUP_WRITEBACK 0x00000020
1da177e4 152
e4ad08fe
MS
153#define BDI_CAP_NO_ACCT_AND_WRITEBACK \
154 (BDI_CAP_NO_WRITEBACK | BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_ACCT_WB)
155
5129a469 156extern struct backing_dev_info noop_backing_dev_info;
1da177e4 157
bc05873d
TH
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 */
165static inline bool writeback_in_progress(struct bdi_writeback *wb)
1da177e4 166{
bc05873d 167 return test_bit(WB_writeback_running, &wb->state);
1da177e4
LT
168}
169
a212b105 170static inline struct backing_dev_info *inode_to_bdi(struct inode *inode)
1da177e4 171{
a212b105 172 struct super_block *sb;
1da177e4 173
a212b105
TH
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;
1da177e4
LT
183}
184
ec8a6f26 185static inline int wb_congested(struct bdi_writeback *wb, int cong_bits)
1da177e4 186{
ec8a6f26 187 struct backing_dev_info *bdi = wb->bdi;
1da177e4 188
ec8a6f26
TH
189 if (bdi->congested_fn)
190 return bdi->congested_fn(bdi->congested_data, cong_bits);
191 return wb->congested->state & cong_bits;
1da177e4 192}
373c0a7e 193
8aa7e847 194long congestion_wait(int sync, long timeout);
0e093d99 195long wait_iff_congested(struct zone *zone, int sync, long timeout);
3965c9ae
WL
196int pdflush_proc_obsolete(struct ctl_table *table, int write,
197 void __user *buffer, size_t *lenp, loff_t *ppos);
1da177e4 198
7d311cda
DW
199static inline bool bdi_cap_stable_pages_required(struct backing_dev_info *bdi)
200{
201 return bdi->capabilities & BDI_CAP_STABLE_WRITES;
202}
203
e4ad08fe
MS
204static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi)
205{
206 return !(bdi->capabilities & BDI_CAP_NO_WRITEBACK);
207}
208
209static inline bool bdi_cap_account_dirty(struct backing_dev_info *bdi)
210{
211 return !(bdi->capabilities & BDI_CAP_NO_ACCT_DIRTY);
212}
1da177e4 213
e4ad08fe
MS
214static 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}
1da177e4 220
e4ad08fe
MS
221static inline bool mapping_cap_writeback_dirty(struct address_space *mapping)
222{
de1414a6 223 return bdi_cap_writeback_dirty(inode_to_bdi(mapping->host));
e4ad08fe 224}
1da177e4 225
e4ad08fe
MS
226static inline bool mapping_cap_account_dirty(struct address_space *mapping)
227{
de1414a6 228 return bdi_cap_account_dirty(inode_to_bdi(mapping->host));
e4ad08fe 229}
1da177e4 230
03ba3782
JA
231static inline int bdi_sched_wait(void *word)
232{
233 schedule();
234 return 0;
235}
236
89e9b9e0
TH
237#ifdef CONFIG_CGROUP_WRITEBACK
238
52ebea74
TH
239struct bdi_writeback_congested *
240wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp);
241void wb_congested_put(struct bdi_writeback_congested *congested);
242struct bdi_writeback *wb_get_create(struct backing_dev_info *bdi,
243 struct cgroup_subsys_state *memcg_css,
244 gfp_t gfp);
52ebea74
TH
245void wb_memcg_offline(struct mem_cgroup *memcg);
246void wb_blkcg_offline(struct blkcg *blkcg);
703c2708 247int inode_congested(struct inode *inode, int cong_bits);
52ebea74 248
89e9b9e0
TH
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 */
256static 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) &&
46b15caa 262 (inode->i_sb->s_iflags & SB_I_CGROUPWB);
89e9b9e0
TH
263}
264
52ebea74
TH
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 */
273static 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 */
302static inline struct bdi_writeback *
303wb_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
aaa2cacf
TH
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 */
330static inline bool inode_to_wb_is_valid(struct inode *inode)
331{
332 return inode->i_wb;
333}
334
52ebea74
TH
335/**
336 * inode_to_wb - determine the wb of an inode
337 * @inode: inode of interest
338 *
aaa2cacf
TH
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.
52ebea74
TH
342 */
343static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
344{
aaa2cacf
TH
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
52ebea74
TH
351 return inode->i_wb;
352}
353
682aa8e1
TH
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 */
369static inline struct bdi_writeback *
370unlocked_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);
aaa2cacf
TH
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;
682aa8e1
TH
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 */
395static 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
ebe41ab0
TH
403struct wb_iter {
404 int start_blkcg_id;
405 struct radix_tree_iter tree_iter;
406 void **slot;
407};
408
409static 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
430static 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
89e9b9e0
TH
458#else /* CONFIG_CGROUP_WRITEBACK */
459
460static inline bool inode_cgwb_enabled(struct inode *inode)
461{
462 return false;
463}
464
52ebea74
TH
465static inline struct bdi_writeback_congested *
466wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp)
467{
468 return bdi->wb.congested;
469}
470
471static inline void wb_congested_put(struct bdi_writeback_congested *congested)
472{
473}
474
52ebea74
TH
475static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
476{
477 return &bdi->wb;
478}
479
480static inline struct bdi_writeback *
481wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
482{
483 return &bdi->wb;
484}
485
aaa2cacf
TH
486static inline bool inode_to_wb_is_valid(struct inode *inode)
487{
488 return true;
489}
490
52ebea74
TH
491static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
492{
493 return &inode_to_bdi(inode)->wb;
494}
495
682aa8e1
TH
496static inline struct bdi_writeback *
497unlocked_inode_to_wb_begin(struct inode *inode, bool *lockedp)
498{
499 return inode_to_wb(inode);
500}
501
502static inline void unlocked_inode_to_wb_end(struct inode *inode, bool locked)
503{
504}
505
52ebea74
TH
506static inline void wb_memcg_offline(struct mem_cgroup *memcg)
507{
508}
509
510static inline void wb_blkcg_offline(struct blkcg *blkcg)
511{
512}
513
ebe41ab0
TH
514struct 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
703c2708
TH
522static inline int inode_congested(struct inode *inode, int cong_bits)
523{
524 return wb_congested(&inode_to_bdi(inode)->wb, cong_bits);
525}
526
89e9b9e0
TH
527#endif /* CONFIG_CGROUP_WRITEBACK */
528
703c2708
TH
529static inline int inode_read_congested(struct inode *inode)
530{
531 return inode_congested(inode, 1 << WB_sync_congested);
532}
533
534static inline int inode_write_congested(struct inode *inode)
535{
536 return inode_congested(inode, 1 << WB_async_congested);
537}
538
539static 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
ec8a6f26
TH
545static inline int bdi_congested(struct backing_dev_info *bdi, int cong_bits)
546{
547 return wb_congested(&bdi->wb, cong_bits);
548}
549
550static inline int bdi_read_congested(struct backing_dev_info *bdi)
551{
552 return bdi_congested(bdi, 1 << WB_sync_congested);
553}
554
555static inline int bdi_write_congested(struct backing_dev_info *bdi)
556{
557 return bdi_congested(bdi, 1 << WB_async_congested);
558}
559
560static 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
89e9b9e0 566#endif /* _LINUX_BACKING_DEV_H */