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