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 | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 11 | #include <linux/percpu_counter.h> |
| 12 | #include <linux/log2.h> |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 13 | #include <linux/proportions.h> |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 14 | #include <linux/kernel.h> |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 15 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/atomic.h> |
| 17 | |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 18 | struct page; |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 19 | struct device; |
Miklos Szeredi | 76f1418 | 2008-04-30 00:54:36 -0700 | [diff] [blame] | 20 | struct dentry; |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | /* |
| 23 | * Bits in backing_dev_info.state |
| 24 | */ |
| 25 | enum bdi_state { |
| 26 | BDI_pdflush, /* A pdflush thread is working this device */ |
Jens Axboe | 1faa16d | 2009-04-06 14:48:01 +0200 | [diff] [blame] | 27 | BDI_async_congested, /* The async (write) queue is getting full */ |
| 28 | BDI_sync_congested, /* The sync queue is getting full */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | BDI_unused, /* Available bits start here */ |
| 30 | }; |
| 31 | |
| 32 | typedef int (congested_fn)(void *, int); |
| 33 | |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 34 | enum bdi_stat_item { |
Peter Zijlstra | c9e51e4 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 35 | BDI_RECLAIMABLE, |
Peter Zijlstra | 69cb51d | 2007-10-16 23:25:48 -0700 | [diff] [blame] | 36 | BDI_WRITEBACK, |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 37 | NR_BDI_STAT_ITEMS |
| 38 | }; |
| 39 | |
| 40 | #define BDI_STAT_BATCH (8*(1+ilog2(nr_cpu_ids))) |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | struct backing_dev_info { |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame^] | 43 | struct list_head bdi_list; |
| 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | unsigned long ra_pages; /* max readahead in PAGE_CACHE_SIZE units */ |
| 46 | unsigned long state; /* Always use atomic bitops on this */ |
| 47 | unsigned int capabilities; /* Device capabilities */ |
| 48 | congested_fn *congested_fn; /* Function pointer if device is md/dm */ |
| 49 | void *congested_data; /* Pointer to aux data for congested func */ |
| 50 | void (*unplug_io_fn)(struct backing_dev_info *, struct page *); |
| 51 | void *unplug_io_data; |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 52 | |
| 53 | struct percpu_counter bdi_stat[NR_BDI_STAT_ITEMS]; |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 54 | |
| 55 | struct prop_local_percpu completions; |
| 56 | int dirty_exceeded; |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 57 | |
Peter Zijlstra | 189d3c4 | 2008-04-30 00:54:35 -0700 | [diff] [blame] | 58 | unsigned int min_ratio; |
Peter Zijlstra | a42dde0 | 2008-04-30 00:54:36 -0700 | [diff] [blame] | 59 | unsigned int max_ratio, max_prop_frac; |
Peter Zijlstra | 189d3c4 | 2008-04-30 00:54:35 -0700 | [diff] [blame] | 60 | |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 61 | struct device *dev; |
Miklos Szeredi | 76f1418 | 2008-04-30 00:54:36 -0700 | [diff] [blame] | 62 | |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame^] | 63 | struct list_head b_dirty; /* dirty inodes */ |
| 64 | struct list_head b_io; /* parked for writeback */ |
| 65 | struct list_head b_more_io; /* parked for more writeback */ |
| 66 | |
Miklos Szeredi | 76f1418 | 2008-04-30 00:54:36 -0700 | [diff] [blame] | 67 | #ifdef CONFIG_DEBUG_FS |
| 68 | struct dentry *debug_dir; |
| 69 | struct dentry *debug_stats; |
| 70 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 73 | int bdi_init(struct backing_dev_info *bdi); |
| 74 | void bdi_destroy(struct backing_dev_info *bdi); |
| 75 | |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 76 | int bdi_register(struct backing_dev_info *bdi, struct device *parent, |
| 77 | const char *fmt, ...); |
| 78 | int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev); |
| 79 | void bdi_unregister(struct backing_dev_info *bdi); |
| 80 | |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame^] | 81 | extern struct mutex bdi_lock; |
| 82 | extern struct list_head bdi_list; |
| 83 | |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 84 | static inline void __add_bdi_stat(struct backing_dev_info *bdi, |
| 85 | enum bdi_stat_item item, s64 amount) |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 86 | { |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 87 | __percpu_counter_add(&bdi->bdi_stat[item], amount, BDI_STAT_BATCH); |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 90 | static inline void __inc_bdi_stat(struct backing_dev_info *bdi, |
| 91 | enum bdi_stat_item item) |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 92 | { |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 93 | __add_bdi_stat(bdi, item, 1); |
| 94 | } |
| 95 | |
| 96 | static inline void inc_bdi_stat(struct backing_dev_info *bdi, |
| 97 | enum bdi_stat_item item) |
| 98 | { |
| 99 | unsigned long flags; |
| 100 | |
| 101 | local_irq_save(flags); |
| 102 | __inc_bdi_stat(bdi, item); |
| 103 | local_irq_restore(flags); |
| 104 | } |
| 105 | |
| 106 | static inline void __dec_bdi_stat(struct backing_dev_info *bdi, |
| 107 | enum bdi_stat_item item) |
| 108 | { |
| 109 | __add_bdi_stat(bdi, item, -1); |
| 110 | } |
| 111 | |
| 112 | static inline void dec_bdi_stat(struct backing_dev_info *bdi, |
| 113 | enum bdi_stat_item item) |
| 114 | { |
| 115 | unsigned long flags; |
| 116 | |
| 117 | local_irq_save(flags); |
| 118 | __dec_bdi_stat(bdi, item); |
| 119 | local_irq_restore(flags); |
| 120 | } |
| 121 | |
| 122 | static inline s64 bdi_stat(struct backing_dev_info *bdi, |
| 123 | enum bdi_stat_item item) |
| 124 | { |
| 125 | return percpu_counter_read_positive(&bdi->bdi_stat[item]); |
| 126 | } |
| 127 | |
| 128 | static inline s64 __bdi_stat_sum(struct backing_dev_info *bdi, |
| 129 | enum bdi_stat_item item) |
| 130 | { |
| 131 | return percpu_counter_sum_positive(&bdi->bdi_stat[item]); |
| 132 | } |
| 133 | |
| 134 | static inline s64 bdi_stat_sum(struct backing_dev_info *bdi, |
| 135 | enum bdi_stat_item item) |
| 136 | { |
| 137 | s64 sum; |
| 138 | unsigned long flags; |
| 139 | |
| 140 | local_irq_save(flags); |
| 141 | sum = __bdi_stat_sum(bdi, item); |
| 142 | local_irq_restore(flags); |
| 143 | |
| 144 | return sum; |
| 145 | } |
| 146 | |
Miklos Szeredi | dd5656e | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 147 | extern void bdi_writeout_inc(struct backing_dev_info *bdi); |
| 148 | |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 149 | /* |
| 150 | * maximal error of a stat counter. |
| 151 | */ |
| 152 | static inline unsigned long bdi_stat_error(struct backing_dev_info *bdi) |
| 153 | { |
| 154 | #ifdef CONFIG_SMP |
| 155 | return nr_cpu_ids * BDI_STAT_BATCH; |
| 156 | #else |
| 157 | return 1; |
| 158 | #endif |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 159 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Peter Zijlstra | 189d3c4 | 2008-04-30 00:54:35 -0700 | [diff] [blame] | 161 | 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] | 162 | 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] | 163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | /* |
| 165 | * Flags in backing_dev_info::capability |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 166 | * |
| 167 | * The first three flags control whether dirty pages will contribute to the |
| 168 | * VM's accounting and whether writepages() should be called for dirty pages |
| 169 | * (something that would not, for example, be appropriate for ramfs) |
| 170 | * |
| 171 | * WARNING: these flags are closely related and should not normally be |
| 172 | * used separately. The BDI_CAP_NO_ACCT_AND_WRITEBACK combines these |
| 173 | * three flags into a single convenience macro. |
| 174 | * |
| 175 | * BDI_CAP_NO_ACCT_DIRTY: Dirty pages shouldn't contribute to accounting |
| 176 | * BDI_CAP_NO_WRITEBACK: Don't write pages back |
| 177 | * BDI_CAP_NO_ACCT_WB: Don't automatically account writeback pages |
| 178 | * |
| 179 | * These flags let !MMU mmap() govern direct device mapping vs immediate |
| 180 | * copying more easily for MAP_PRIVATE, especially for ROM filesystems. |
| 181 | * |
| 182 | * BDI_CAP_MAP_COPY: Copy can be mapped (MAP_PRIVATE) |
| 183 | * BDI_CAP_MAP_DIRECT: Can be mapped directly (MAP_SHARED) |
| 184 | * BDI_CAP_READ_MAP: Can be mapped for reading |
| 185 | * BDI_CAP_WRITE_MAP: Can be mapped for writing |
| 186 | * BDI_CAP_EXEC_MAP: Can be mapped for execution |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 187 | * |
| 188 | * BDI_CAP_SWAP_BACKED: Count shmem/tmpfs objects as swap-backed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | */ |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 190 | #define BDI_CAP_NO_ACCT_DIRTY 0x00000001 |
| 191 | #define BDI_CAP_NO_WRITEBACK 0x00000002 |
| 192 | #define BDI_CAP_MAP_COPY 0x00000004 |
| 193 | #define BDI_CAP_MAP_DIRECT 0x00000008 |
| 194 | #define BDI_CAP_READ_MAP 0x00000010 |
| 195 | #define BDI_CAP_WRITE_MAP 0x00000020 |
| 196 | #define BDI_CAP_EXEC_MAP 0x00000040 |
| 197 | #define BDI_CAP_NO_ACCT_WB 0x00000080 |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 198 | #define BDI_CAP_SWAP_BACKED 0x00000100 |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | #define BDI_CAP_VMFLAGS \ |
| 201 | (BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | BDI_CAP_EXEC_MAP) |
| 202 | |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 203 | #define BDI_CAP_NO_ACCT_AND_WRITEBACK \ |
| 204 | (BDI_CAP_NO_WRITEBACK | BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_ACCT_WB) |
| 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | #if defined(VM_MAYREAD) && \ |
| 207 | (BDI_CAP_READ_MAP != VM_MAYREAD || \ |
| 208 | BDI_CAP_WRITE_MAP != VM_MAYWRITE || \ |
| 209 | BDI_CAP_EXEC_MAP != VM_MAYEXEC) |
| 210 | #error please change backing_dev_info::capabilities flags |
| 211 | #endif |
| 212 | |
| 213 | extern struct backing_dev_info default_backing_dev_info; |
| 214 | void default_unplug_io_fn(struct backing_dev_info *bdi, struct page *page); |
| 215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | int writeback_in_progress(struct backing_dev_info *bdi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
| 218 | static inline int bdi_congested(struct backing_dev_info *bdi, int bdi_bits) |
| 219 | { |
| 220 | if (bdi->congested_fn) |
| 221 | return bdi->congested_fn(bdi->congested_data, bdi_bits); |
| 222 | return (bdi->state & bdi_bits); |
| 223 | } |
| 224 | |
| 225 | static inline int bdi_read_congested(struct backing_dev_info *bdi) |
| 226 | { |
Jens Axboe | 1faa16d | 2009-04-06 14:48:01 +0200 | [diff] [blame] | 227 | return bdi_congested(bdi, 1 << BDI_sync_congested); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static inline int bdi_write_congested(struct backing_dev_info *bdi) |
| 231 | { |
Jens Axboe | 1faa16d | 2009-04-06 14:48:01 +0200 | [diff] [blame] | 232 | return bdi_congested(bdi, 1 << BDI_async_congested); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | static inline int bdi_rw_congested(struct backing_dev_info *bdi) |
| 236 | { |
Jens Axboe | 1faa16d | 2009-04-06 14:48:01 +0200 | [diff] [blame] | 237 | return bdi_congested(bdi, (1 << BDI_sync_congested) | |
| 238 | (1 << BDI_async_congested)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Trond Myklebust | 373c0a7 | 2009-07-11 10:06:54 -0400 | [diff] [blame] | 241 | enum { |
| 242 | BLK_RW_ASYNC = 0, |
| 243 | BLK_RW_SYNC = 1, |
| 244 | }; |
| 245 | |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 246 | void clear_bdi_congested(struct backing_dev_info *bdi, int sync); |
| 247 | void set_bdi_congested(struct backing_dev_info *bdi, int sync); |
| 248 | long congestion_wait(int sync, long timeout); |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 251 | static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi) |
| 252 | { |
| 253 | return !(bdi->capabilities & BDI_CAP_NO_WRITEBACK); |
| 254 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 256 | static inline bool bdi_cap_account_dirty(struct backing_dev_info *bdi) |
| 257 | { |
| 258 | return !(bdi->capabilities & BDI_CAP_NO_ACCT_DIRTY); |
| 259 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 261 | static inline bool bdi_cap_account_writeback(struct backing_dev_info *bdi) |
| 262 | { |
| 263 | /* Paranoia: BDI_CAP_NO_WRITEBACK implies BDI_CAP_NO_ACCT_WB */ |
| 264 | return !(bdi->capabilities & (BDI_CAP_NO_ACCT_WB | |
| 265 | BDI_CAP_NO_WRITEBACK)); |
| 266 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 268 | static inline bool bdi_cap_swap_backed(struct backing_dev_info *bdi) |
| 269 | { |
| 270 | return bdi->capabilities & BDI_CAP_SWAP_BACKED; |
| 271 | } |
| 272 | |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 273 | static inline bool mapping_cap_writeback_dirty(struct address_space *mapping) |
| 274 | { |
| 275 | return bdi_cap_writeback_dirty(mapping->backing_dev_info); |
| 276 | } |
| 277 | |
| 278 | static inline bool mapping_cap_account_dirty(struct address_space *mapping) |
| 279 | { |
| 280 | return bdi_cap_account_dirty(mapping->backing_dev_info); |
| 281 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 283 | static inline bool mapping_cap_swap_backed(struct address_space *mapping) |
| 284 | { |
| 285 | return bdi_cap_swap_backed(mapping->backing_dev_info); |
| 286 | } |
| 287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | #endif /* _LINUX_BACKING_DEV_H */ |