Nicholas Flintham | 1e3d311 | 2013-04-10 10:48:38 +0100 | [diff] [blame^] | 1 | |
| 2 | #ifndef _LINUX_BACKING_DEV_H |
| 3 | #define _LINUX_BACKING_DEV_H |
| 4 | |
| 5 | #include <linux/percpu_counter.h> |
| 6 | #include <linux/log2.h> |
| 7 | #include <linux/proportions.h> |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/fs.h> |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/timer.h> |
| 12 | #include <linux/writeback.h> |
| 13 | #include <linux/atomic.h> |
| 14 | |
| 15 | struct page; |
| 16 | struct device; |
| 17 | struct dentry; |
| 18 | |
| 19 | enum bdi_state { |
| 20 | BDI_pending, |
| 21 | BDI_wb_alloc, |
| 22 | BDI_async_congested, |
| 23 | BDI_sync_congested, |
| 24 | BDI_registered, |
| 25 | BDI_writeback_running, |
| 26 | BDI_unused, |
| 27 | }; |
| 28 | |
| 29 | typedef int (congested_fn)(void *, int); |
| 30 | |
| 31 | enum bdi_stat_item { |
| 32 | BDI_RECLAIMABLE, |
| 33 | BDI_WRITEBACK, |
| 34 | BDI_DIRTIED, |
| 35 | BDI_WRITTEN, |
| 36 | NR_BDI_STAT_ITEMS |
| 37 | }; |
| 38 | |
| 39 | #define BDI_STAT_BATCH (8*(1+ilog2(nr_cpu_ids))) |
| 40 | |
| 41 | struct bdi_writeback { |
| 42 | struct backing_dev_info *bdi; |
| 43 | unsigned int nr; |
| 44 | |
| 45 | unsigned long last_old_flush; |
| 46 | unsigned long last_active; |
| 47 | |
| 48 | struct task_struct *task; |
| 49 | struct timer_list wakeup_timer; |
| 50 | struct list_head b_dirty; |
| 51 | struct list_head b_io; |
| 52 | struct list_head b_more_io; |
| 53 | spinlock_t list_lock; |
| 54 | }; |
| 55 | |
| 56 | struct backing_dev_info { |
| 57 | struct list_head bdi_list; |
| 58 | unsigned long ra_pages; |
| 59 | unsigned long state; |
| 60 | unsigned int capabilities; |
| 61 | congested_fn *congested_fn; |
| 62 | void *congested_data; |
| 63 | |
| 64 | char *name; |
| 65 | |
| 66 | struct percpu_counter bdi_stat[NR_BDI_STAT_ITEMS]; |
| 67 | |
| 68 | unsigned long bw_time_stamp; |
| 69 | unsigned long dirtied_stamp; |
| 70 | unsigned long written_stamp; /* pages written at bw_time_stamp */ |
| 71 | unsigned long write_bandwidth; |
| 72 | unsigned long avg_write_bandwidth; |
| 73 | |
| 74 | unsigned long dirty_ratelimit; |
| 75 | unsigned long balanced_dirty_ratelimit; |
| 76 | |
| 77 | struct prop_local_percpu completions; |
| 78 | int dirty_exceeded; |
| 79 | |
| 80 | unsigned int min_ratio; |
| 81 | unsigned int max_ratio, max_prop_frac; |
| 82 | |
| 83 | struct bdi_writeback wb; |
| 84 | spinlock_t wb_lock; |
| 85 | |
| 86 | struct list_head work_list; |
| 87 | |
| 88 | struct device *dev; |
| 89 | |
| 90 | struct timer_list laptop_mode_wb_timer; |
| 91 | |
| 92 | #ifdef CONFIG_DEBUG_FS |
| 93 | struct dentry *debug_dir; |
| 94 | struct dentry *debug_stats; |
| 95 | #endif |
| 96 | }; |
| 97 | |
| 98 | int bdi_init(struct backing_dev_info *bdi); |
| 99 | void bdi_destroy(struct backing_dev_info *bdi); |
| 100 | |
| 101 | int bdi_register(struct backing_dev_info *bdi, struct device *parent, |
| 102 | const char *fmt, ...); |
| 103 | int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev); |
| 104 | void bdi_unregister(struct backing_dev_info *bdi); |
| 105 | int bdi_setup_and_register(struct backing_dev_info *, char *, unsigned int); |
| 106 | void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages, |
| 107 | enum wb_reason reason); |
| 108 | void bdi_start_background_writeback(struct backing_dev_info *bdi); |
| 109 | int bdi_writeback_thread(void *data); |
| 110 | int bdi_has_dirty_io(struct backing_dev_info *bdi); |
| 111 | void bdi_arm_supers_timer(void); |
| 112 | void bdi_wakeup_thread_delayed(struct backing_dev_info *bdi); |
| 113 | void bdi_lock_two(struct bdi_writeback *wb1, struct bdi_writeback *wb2); |
| 114 | |
| 115 | extern spinlock_t bdi_lock; |
| 116 | extern struct list_head bdi_list; |
| 117 | extern struct list_head bdi_pending_list; |
| 118 | |
| 119 | static inline int wb_has_dirty_io(struct bdi_writeback *wb) |
| 120 | { |
| 121 | return !list_empty(&wb->b_dirty) || |
| 122 | !list_empty(&wb->b_io) || |
| 123 | !list_empty(&wb->b_more_io); |
| 124 | } |
| 125 | |
| 126 | static inline void __add_bdi_stat(struct backing_dev_info *bdi, |
| 127 | enum bdi_stat_item item, s64 amount) |
| 128 | { |
| 129 | __percpu_counter_add(&bdi->bdi_stat[item], amount, BDI_STAT_BATCH); |
| 130 | } |
| 131 | |
| 132 | static inline void __inc_bdi_stat(struct backing_dev_info *bdi, |
| 133 | enum bdi_stat_item item) |
| 134 | { |
| 135 | __add_bdi_stat(bdi, item, 1); |
| 136 | } |
| 137 | |
| 138 | static inline void inc_bdi_stat(struct backing_dev_info *bdi, |
| 139 | enum bdi_stat_item item) |
| 140 | { |
| 141 | unsigned long flags; |
| 142 | |
| 143 | local_irq_save(flags); |
| 144 | __inc_bdi_stat(bdi, item); |
| 145 | local_irq_restore(flags); |
| 146 | } |
| 147 | |
| 148 | static inline void __dec_bdi_stat(struct backing_dev_info *bdi, |
| 149 | enum bdi_stat_item item) |
| 150 | { |
| 151 | __add_bdi_stat(bdi, item, -1); |
| 152 | } |
| 153 | |
| 154 | static inline void dec_bdi_stat(struct backing_dev_info *bdi, |
| 155 | enum bdi_stat_item item) |
| 156 | { |
| 157 | unsigned long flags; |
| 158 | |
| 159 | local_irq_save(flags); |
| 160 | __dec_bdi_stat(bdi, item); |
| 161 | local_irq_restore(flags); |
| 162 | } |
| 163 | |
| 164 | static inline s64 bdi_stat(struct backing_dev_info *bdi, |
| 165 | enum bdi_stat_item item) |
| 166 | { |
| 167 | return percpu_counter_read_positive(&bdi->bdi_stat[item]); |
| 168 | } |
| 169 | |
| 170 | static inline s64 __bdi_stat_sum(struct backing_dev_info *bdi, |
| 171 | enum bdi_stat_item item) |
| 172 | { |
| 173 | return percpu_counter_sum_positive(&bdi->bdi_stat[item]); |
| 174 | } |
| 175 | |
| 176 | static inline s64 bdi_stat_sum(struct backing_dev_info *bdi, |
| 177 | enum bdi_stat_item item) |
| 178 | { |
| 179 | s64 sum; |
| 180 | unsigned long flags; |
| 181 | |
| 182 | local_irq_save(flags); |
| 183 | sum = __bdi_stat_sum(bdi, item); |
| 184 | local_irq_restore(flags); |
| 185 | |
| 186 | return sum; |
| 187 | } |
| 188 | |
| 189 | extern void bdi_writeout_inc(struct backing_dev_info *bdi); |
| 190 | |
| 191 | static inline unsigned long bdi_stat_error(struct backing_dev_info *bdi) |
| 192 | { |
| 193 | #ifdef CONFIG_SMP |
| 194 | return nr_cpu_ids * BDI_STAT_BATCH; |
| 195 | #else |
| 196 | return 1; |
| 197 | #endif |
| 198 | } |
| 199 | |
| 200 | int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio); |
| 201 | int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ratio); |
| 202 | |
| 203 | #define BDI_CAP_NO_ACCT_DIRTY 0x00000001 |
| 204 | #define BDI_CAP_NO_WRITEBACK 0x00000002 |
| 205 | #define BDI_CAP_MAP_COPY 0x00000004 |
| 206 | #define BDI_CAP_MAP_DIRECT 0x00000008 |
| 207 | #define BDI_CAP_READ_MAP 0x00000010 |
| 208 | #define BDI_CAP_WRITE_MAP 0x00000020 |
| 209 | #define BDI_CAP_EXEC_MAP 0x00000040 |
| 210 | #define BDI_CAP_NO_ACCT_WB 0x00000080 |
| 211 | #define BDI_CAP_SWAP_BACKED 0x00000100 |
| 212 | |
| 213 | #define BDI_CAP_VMFLAGS \ |
| 214 | (BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | BDI_CAP_EXEC_MAP) |
| 215 | |
| 216 | #define BDI_CAP_NO_ACCT_AND_WRITEBACK \ |
| 217 | (BDI_CAP_NO_WRITEBACK | BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_ACCT_WB) |
| 218 | |
| 219 | #if defined(VM_MAYREAD) && \ |
| 220 | (BDI_CAP_READ_MAP != VM_MAYREAD || \ |
| 221 | BDI_CAP_WRITE_MAP != VM_MAYWRITE || \ |
| 222 | BDI_CAP_EXEC_MAP != VM_MAYEXEC) |
| 223 | #error please change backing_dev_info::capabilities flags |
| 224 | #endif |
| 225 | |
| 226 | extern struct backing_dev_info default_backing_dev_info; |
| 227 | extern struct backing_dev_info noop_backing_dev_info; |
| 228 | |
| 229 | int writeback_in_progress(struct backing_dev_info *bdi); |
| 230 | |
| 231 | static inline int bdi_congested(struct backing_dev_info *bdi, int bdi_bits) |
| 232 | { |
| 233 | if (bdi->congested_fn) |
| 234 | return bdi->congested_fn(bdi->congested_data, bdi_bits); |
| 235 | return (bdi->state & bdi_bits); |
| 236 | } |
| 237 | |
| 238 | static inline int bdi_read_congested(struct backing_dev_info *bdi) |
| 239 | { |
| 240 | return bdi_congested(bdi, 1 << BDI_sync_congested); |
| 241 | } |
| 242 | |
| 243 | static inline int bdi_write_congested(struct backing_dev_info *bdi) |
| 244 | { |
| 245 | return bdi_congested(bdi, 1 << BDI_async_congested); |
| 246 | } |
| 247 | |
| 248 | static inline int bdi_rw_congested(struct backing_dev_info *bdi) |
| 249 | { |
| 250 | return bdi_congested(bdi, (1 << BDI_sync_congested) | |
| 251 | (1 << BDI_async_congested)); |
| 252 | } |
| 253 | |
| 254 | enum { |
| 255 | BLK_RW_ASYNC = 0, |
| 256 | BLK_RW_SYNC = 1, |
| 257 | }; |
| 258 | |
| 259 | void clear_bdi_congested(struct backing_dev_info *bdi, int sync); |
| 260 | void set_bdi_congested(struct backing_dev_info *bdi, int sync); |
| 261 | long congestion_wait(int sync, long timeout); |
| 262 | long wait_iff_congested(struct zone *zone, int sync, long timeout); |
| 263 | |
| 264 | static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi) |
| 265 | { |
| 266 | return !(bdi->capabilities & BDI_CAP_NO_WRITEBACK); |
| 267 | } |
| 268 | |
| 269 | static inline bool bdi_cap_account_dirty(struct backing_dev_info *bdi) |
| 270 | { |
| 271 | return !(bdi->capabilities & BDI_CAP_NO_ACCT_DIRTY); |
| 272 | } |
| 273 | |
| 274 | static inline bool bdi_cap_account_writeback(struct backing_dev_info *bdi) |
| 275 | { |
| 276 | |
| 277 | return !(bdi->capabilities & (BDI_CAP_NO_ACCT_WB | |
| 278 | BDI_CAP_NO_WRITEBACK)); |
| 279 | } |
| 280 | |
| 281 | static inline bool bdi_cap_swap_backed(struct backing_dev_info *bdi) |
| 282 | { |
| 283 | return bdi->capabilities & BDI_CAP_SWAP_BACKED; |
| 284 | } |
| 285 | |
| 286 | static inline bool bdi_cap_flush_forker(struct backing_dev_info *bdi) |
| 287 | { |
| 288 | return bdi == &default_backing_dev_info; |
| 289 | } |
| 290 | |
| 291 | static inline bool mapping_cap_writeback_dirty(struct address_space *mapping) |
| 292 | { |
| 293 | return bdi_cap_writeback_dirty(mapping->backing_dev_info); |
| 294 | } |
| 295 | |
| 296 | static inline bool mapping_cap_account_dirty(struct address_space *mapping) |
| 297 | { |
| 298 | return bdi_cap_account_dirty(mapping->backing_dev_info); |
| 299 | } |
| 300 | |
| 301 | static inline bool mapping_cap_swap_backed(struct address_space *mapping) |
| 302 | { |
| 303 | return bdi_cap_swap_backed(mapping->backing_dev_info); |
| 304 | } |
| 305 | |
| 306 | static inline int bdi_sched_wait(void *word) |
| 307 | { |
| 308 | schedule(); |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | #endif |