Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 11 | #include <linux/kernel.h> |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 12 | #include <linux/fs.h> |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 13 | #include <linux/sched.h> |
Tejun Heo | a212b10 | 2015-05-22 17:13:33 -0400 | [diff] [blame] | 14 | #include <linux/blkdev.h> |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 15 | #include <linux/writeback.h> |
Tejun Heo | 66114ca | 2015-05-22 17:13:32 -0400 | [diff] [blame] | 16 | #include <linux/backing-dev-defs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Mikulas Patocka | 8077c0d | 2013-10-14 12:14:13 -0400 | [diff] [blame] | 18 | int __must_check bdi_init(struct backing_dev_info *bdi); |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 19 | void bdi_destroy(struct backing_dev_info *bdi); |
| 20 | |
Joe Perches | d2cc4dd | 2012-11-29 08:37:03 -0600 | [diff] [blame] | 21 | __printf(3, 4) |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 22 | int bdi_register(struct backing_dev_info *bdi, struct device *parent, |
| 23 | const char *fmt, ...); |
| 24 | int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev); |
| 25 | void bdi_unregister(struct backing_dev_info *bdi); |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 26 | int __must_check bdi_setup_and_register(struct backing_dev_info *, char *); |
Curt Wohlgemuth | 0e175a1 | 2011-10-07 21:54:10 -0600 | [diff] [blame] | 27 | void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages, |
| 28 | enum wb_reason reason); |
Christoph Hellwig | c544419 | 2010-06-08 18:15:15 +0200 | [diff] [blame] | 29 | void bdi_start_background_writeback(struct backing_dev_info *bdi); |
Tejun Heo | f0054bb | 2015-05-22 17:13:30 -0400 | [diff] [blame] | 30 | void wb_workfn(struct work_struct *work); |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 31 | int bdi_has_dirty_io(struct backing_dev_info *bdi); |
Tejun Heo | f0054bb | 2015-05-22 17:13:30 -0400 | [diff] [blame] | 32 | void wb_wakeup_delayed(struct bdi_writeback *wb); |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 33 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 34 | extern spinlock_t bdi_lock; |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 35 | extern struct list_head bdi_list; |
| 36 | |
Tejun Heo | 839a8e8 | 2013-04-01 19:08:06 -0700 | [diff] [blame] | 37 | extern struct workqueue_struct *bdi_wq; |
| 38 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 39 | static inline int wb_has_dirty_io(struct bdi_writeback *wb) |
| 40 | { |
| 41 | return !list_empty(&wb->b_dirty) || |
| 42 | !list_empty(&wb->b_io) || |
| 43 | !list_empty(&wb->b_more_io); |
| 44 | } |
| 45 | |
Tejun Heo | 93f78d8 | 2015-05-22 17:13:27 -0400 | [diff] [blame] | 46 | static inline void __add_wb_stat(struct bdi_writeback *wb, |
| 47 | enum wb_stat_item item, s64 amount) |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 48 | { |
Tejun Heo | 93f78d8 | 2015-05-22 17:13:27 -0400 | [diff] [blame] | 49 | __percpu_counter_add(&wb->stat[item], amount, WB_STAT_BATCH); |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Tejun Heo | 93f78d8 | 2015-05-22 17:13:27 -0400 | [diff] [blame] | 52 | static inline void __inc_wb_stat(struct bdi_writeback *wb, |
| 53 | enum wb_stat_item item) |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 54 | { |
Tejun Heo | 93f78d8 | 2015-05-22 17:13:27 -0400 | [diff] [blame] | 55 | __add_wb_stat(wb, item, 1); |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Tejun Heo | 93f78d8 | 2015-05-22 17:13:27 -0400 | [diff] [blame] | 58 | static inline void inc_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item) |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 59 | { |
| 60 | unsigned long flags; |
| 61 | |
| 62 | local_irq_save(flags); |
Tejun Heo | 93f78d8 | 2015-05-22 17:13:27 -0400 | [diff] [blame] | 63 | __inc_wb_stat(wb, item); |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 64 | local_irq_restore(flags); |
| 65 | } |
| 66 | |
Tejun Heo | 93f78d8 | 2015-05-22 17:13:27 -0400 | [diff] [blame] | 67 | static inline void __dec_wb_stat(struct bdi_writeback *wb, |
| 68 | enum wb_stat_item item) |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 69 | { |
Tejun Heo | 93f78d8 | 2015-05-22 17:13:27 -0400 | [diff] [blame] | 70 | __add_wb_stat(wb, item, -1); |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Tejun Heo | 93f78d8 | 2015-05-22 17:13:27 -0400 | [diff] [blame] | 73 | static inline void dec_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item) |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 74 | { |
| 75 | unsigned long flags; |
| 76 | |
| 77 | local_irq_save(flags); |
Tejun Heo | 93f78d8 | 2015-05-22 17:13:27 -0400 | [diff] [blame] | 78 | __dec_wb_stat(wb, item); |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 79 | local_irq_restore(flags); |
| 80 | } |
| 81 | |
Tejun Heo | 93f78d8 | 2015-05-22 17:13:27 -0400 | [diff] [blame] | 82 | static inline s64 wb_stat(struct bdi_writeback *wb, enum wb_stat_item item) |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 83 | { |
Tejun Heo | 93f78d8 | 2015-05-22 17:13:27 -0400 | [diff] [blame] | 84 | return percpu_counter_read_positive(&wb->stat[item]); |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Tejun Heo | 93f78d8 | 2015-05-22 17:13:27 -0400 | [diff] [blame] | 87 | static inline s64 __wb_stat_sum(struct bdi_writeback *wb, |
| 88 | enum wb_stat_item item) |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 89 | { |
Tejun Heo | 93f78d8 | 2015-05-22 17:13:27 -0400 | [diff] [blame] | 90 | return percpu_counter_sum_positive(&wb->stat[item]); |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Tejun Heo | 93f78d8 | 2015-05-22 17:13:27 -0400 | [diff] [blame] | 93 | static inline s64 wb_stat_sum(struct bdi_writeback *wb, enum wb_stat_item item) |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 94 | { |
| 95 | s64 sum; |
| 96 | unsigned long flags; |
| 97 | |
| 98 | local_irq_save(flags); |
Tejun Heo | 93f78d8 | 2015-05-22 17:13:27 -0400 | [diff] [blame] | 99 | sum = __wb_stat_sum(wb, item); |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 100 | local_irq_restore(flags); |
| 101 | |
| 102 | return sum; |
| 103 | } |
| 104 | |
Tejun Heo | 93f78d8 | 2015-05-22 17:13:27 -0400 | [diff] [blame] | 105 | extern void wb_writeout_inc(struct bdi_writeback *wb); |
Miklos Szeredi | dd5656e | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 106 | |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 107 | /* |
| 108 | * maximal error of a stat counter. |
| 109 | */ |
Tejun Heo | 93f78d8 | 2015-05-22 17:13:27 -0400 | [diff] [blame] | 110 | static inline unsigned long wb_stat_error(struct bdi_writeback *wb) |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 111 | { |
| 112 | #ifdef CONFIG_SMP |
Tejun Heo | 93f78d8 | 2015-05-22 17:13:27 -0400 | [diff] [blame] | 113 | return nr_cpu_ids * WB_STAT_BATCH; |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 114 | #else |
| 115 | return 1; |
| 116 | #endif |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 117 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Peter Zijlstra | 189d3c4 | 2008-04-30 00:54:35 -0700 | [diff] [blame] | 119 | int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio); |
Peter Zijlstra | a42dde0 | 2008-04-30 00:54:36 -0700 | [diff] [blame] | 120 | int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ratio); |
Peter Zijlstra | 189d3c4 | 2008-04-30 00:54:35 -0700 | [diff] [blame] | 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | /* |
| 123 | * Flags in backing_dev_info::capability |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 124 | * |
| 125 | * The first three flags control whether dirty pages will contribute to the |
| 126 | * VM's accounting and whether writepages() should be called for dirty pages |
| 127 | * (something that would not, for example, be appropriate for ramfs) |
| 128 | * |
| 129 | * WARNING: these flags are closely related and should not normally be |
| 130 | * used separately. The BDI_CAP_NO_ACCT_AND_WRITEBACK combines these |
| 131 | * three flags into a single convenience macro. |
| 132 | * |
| 133 | * BDI_CAP_NO_ACCT_DIRTY: Dirty pages shouldn't contribute to accounting |
| 134 | * BDI_CAP_NO_WRITEBACK: Don't write pages back |
| 135 | * BDI_CAP_NO_ACCT_WB: Don't automatically account writeback pages |
Maxim Patlasov | 5a53748 | 2013-09-11 14:22:46 -0700 | [diff] [blame] | 136 | * BDI_CAP_STRICTLIMIT: Keep number of dirty pages below bdi threshold. |
Tejun Heo | 89e9b9e | 2015-05-22 17:13:36 -0400 | [diff] [blame^] | 137 | * |
| 138 | * BDI_CAP_CGROUP_WRITEBACK: Supports cgroup-aware writeback. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | */ |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 140 | #define BDI_CAP_NO_ACCT_DIRTY 0x00000001 |
| 141 | #define BDI_CAP_NO_WRITEBACK 0x00000002 |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 142 | #define BDI_CAP_NO_ACCT_WB 0x00000004 |
| 143 | #define BDI_CAP_STABLE_WRITES 0x00000008 |
| 144 | #define BDI_CAP_STRICTLIMIT 0x00000010 |
Tejun Heo | 89e9b9e | 2015-05-22 17:13:36 -0400 | [diff] [blame^] | 145 | #define BDI_CAP_CGROUP_WRITEBACK 0x00000020 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 147 | #define BDI_CAP_NO_ACCT_AND_WRITEBACK \ |
| 148 | (BDI_CAP_NO_WRITEBACK | BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_ACCT_WB) |
| 149 | |
Jörn Engel | 5129a46 | 2010-04-25 08:54:42 +0200 | [diff] [blame] | 150 | extern struct backing_dev_info noop_backing_dev_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | int writeback_in_progress(struct backing_dev_info *bdi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Tejun Heo | a212b10 | 2015-05-22 17:13:33 -0400 | [diff] [blame] | 154 | static inline struct backing_dev_info *inode_to_bdi(struct inode *inode) |
| 155 | { |
| 156 | struct super_block *sb; |
| 157 | |
| 158 | if (!inode) |
| 159 | return &noop_backing_dev_info; |
| 160 | |
| 161 | sb = inode->i_sb; |
| 162 | #ifdef CONFIG_BLOCK |
| 163 | if (sb_is_blkdev_sb(sb)) |
| 164 | return blk_get_backing_dev_info(I_BDEV(inode)); |
| 165 | #endif |
| 166 | return sb->s_bdi; |
| 167 | } |
| 168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | static inline int bdi_congested(struct backing_dev_info *bdi, int bdi_bits) |
| 170 | { |
| 171 | if (bdi->congested_fn) |
| 172 | return bdi->congested_fn(bdi->congested_data, bdi_bits); |
Tejun Heo | 4aa9c69 | 2015-05-22 17:13:35 -0400 | [diff] [blame] | 173 | return (bdi->wb.congested->state & bdi_bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static inline int bdi_read_congested(struct backing_dev_info *bdi) |
| 177 | { |
Tejun Heo | 4452226 | 2015-05-22 17:13:26 -0400 | [diff] [blame] | 178 | return bdi_congested(bdi, 1 << WB_sync_congested); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static inline int bdi_write_congested(struct backing_dev_info *bdi) |
| 182 | { |
Tejun Heo | 4452226 | 2015-05-22 17:13:26 -0400 | [diff] [blame] | 183 | return bdi_congested(bdi, 1 << WB_async_congested); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static inline int bdi_rw_congested(struct backing_dev_info *bdi) |
| 187 | { |
Tejun Heo | 4452226 | 2015-05-22 17:13:26 -0400 | [diff] [blame] | 188 | return bdi_congested(bdi, (1 << WB_sync_congested) | |
| 189 | (1 << WB_async_congested)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 192 | long congestion_wait(int sync, long timeout); |
Mel Gorman | 0e093d99 | 2010-10-26 14:21:45 -0700 | [diff] [blame] | 193 | long wait_iff_congested(struct zone *zone, int sync, long timeout); |
Wanpeng Li | 3965c9a | 2012-07-31 16:41:52 -0700 | [diff] [blame] | 194 | int pdflush_proc_obsolete(struct ctl_table *table, int write, |
| 195 | void __user *buffer, size_t *lenp, loff_t *ppos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
Darrick J. Wong | 7d311cd | 2013-02-21 16:42:48 -0800 | [diff] [blame] | 197 | static inline bool bdi_cap_stable_pages_required(struct backing_dev_info *bdi) |
| 198 | { |
| 199 | return bdi->capabilities & BDI_CAP_STABLE_WRITES; |
| 200 | } |
| 201 | |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 202 | static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi) |
| 203 | { |
| 204 | return !(bdi->capabilities & BDI_CAP_NO_WRITEBACK); |
| 205 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 207 | static inline bool bdi_cap_account_dirty(struct backing_dev_info *bdi) |
| 208 | { |
| 209 | return !(bdi->capabilities & BDI_CAP_NO_ACCT_DIRTY); |
| 210 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 212 | static inline bool bdi_cap_account_writeback(struct backing_dev_info *bdi) |
| 213 | { |
| 214 | /* Paranoia: BDI_CAP_NO_WRITEBACK implies BDI_CAP_NO_ACCT_WB */ |
| 215 | return !(bdi->capabilities & (BDI_CAP_NO_ACCT_WB | |
| 216 | BDI_CAP_NO_WRITEBACK)); |
| 217 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 219 | static inline bool mapping_cap_writeback_dirty(struct address_space *mapping) |
| 220 | { |
Christoph Hellwig | de1414a | 2015-01-14 10:42:36 +0100 | [diff] [blame] | 221 | return bdi_cap_writeback_dirty(inode_to_bdi(mapping->host)); |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static inline bool mapping_cap_account_dirty(struct address_space *mapping) |
| 225 | { |
Christoph Hellwig | de1414a | 2015-01-14 10:42:36 +0100 | [diff] [blame] | 226 | return bdi_cap_account_dirty(inode_to_bdi(mapping->host)); |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 227 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 229 | static inline int bdi_sched_wait(void *word) |
| 230 | { |
| 231 | schedule(); |
| 232 | return 0; |
| 233 | } |
| 234 | |
Tejun Heo | 89e9b9e | 2015-05-22 17:13:36 -0400 | [diff] [blame^] | 235 | #ifdef CONFIG_CGROUP_WRITEBACK |
| 236 | |
| 237 | /** |
| 238 | * inode_cgwb_enabled - test whether cgroup writeback is enabled on an inode |
| 239 | * @inode: inode of interest |
| 240 | * |
| 241 | * cgroup writeback requires support from both the bdi and filesystem. |
| 242 | * Test whether @inode has both. |
| 243 | */ |
| 244 | static inline bool inode_cgwb_enabled(struct inode *inode) |
| 245 | { |
| 246 | struct backing_dev_info *bdi = inode_to_bdi(inode); |
| 247 | |
| 248 | return bdi_cap_account_dirty(bdi) && |
| 249 | (bdi->capabilities & BDI_CAP_CGROUP_WRITEBACK) && |
| 250 | (inode->i_sb->s_type->fs_flags & FS_CGROUP_WRITEBACK); |
| 251 | } |
| 252 | |
| 253 | #else /* CONFIG_CGROUP_WRITEBACK */ |
| 254 | |
| 255 | static inline bool inode_cgwb_enabled(struct inode *inode) |
| 256 | { |
| 257 | return false; |
| 258 | } |
| 259 | |
| 260 | #endif /* CONFIG_CGROUP_WRITEBACK */ |
| 261 | |
| 262 | #endif /* _LINUX_BACKING_DEV_H */ |