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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <asm/atomic.h> |
| 16 | |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 17 | struct page; |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 18 | struct device; |
Miklos Szeredi | 76f1418 | 2008-04-30 00:54:36 -0700 | [diff] [blame^] | 19 | struct dentry; |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | /* |
| 22 | * Bits in backing_dev_info.state |
| 23 | */ |
| 24 | enum bdi_state { |
| 25 | BDI_pdflush, /* A pdflush thread is working this device */ |
| 26 | BDI_write_congested, /* The write queue is getting full */ |
| 27 | BDI_read_congested, /* The read queue is getting full */ |
| 28 | BDI_unused, /* Available bits start here */ |
| 29 | }; |
| 30 | |
| 31 | typedef int (congested_fn)(void *, int); |
| 32 | |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 33 | enum bdi_stat_item { |
Peter Zijlstra | c9e51e4 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 34 | BDI_RECLAIMABLE, |
Peter Zijlstra | 69cb51d | 2007-10-16 23:25:48 -0700 | [diff] [blame] | 35 | BDI_WRITEBACK, |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 36 | NR_BDI_STAT_ITEMS |
| 37 | }; |
| 38 | |
| 39 | #define BDI_STAT_BATCH (8*(1+ilog2(nr_cpu_ids))) |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | struct backing_dev_info { |
| 42 | unsigned long ra_pages; /* max readahead in PAGE_CACHE_SIZE units */ |
| 43 | unsigned long state; /* Always use atomic bitops on this */ |
| 44 | unsigned int capabilities; /* Device capabilities */ |
| 45 | congested_fn *congested_fn; /* Function pointer if device is md/dm */ |
| 46 | void *congested_data; /* Pointer to aux data for congested func */ |
| 47 | void (*unplug_io_fn)(struct backing_dev_info *, struct page *); |
| 48 | void *unplug_io_data; |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 49 | |
| 50 | struct percpu_counter bdi_stat[NR_BDI_STAT_ITEMS]; |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 51 | |
| 52 | struct prop_local_percpu completions; |
| 53 | int dirty_exceeded; |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 54 | |
Peter Zijlstra | 189d3c4 | 2008-04-30 00:54:35 -0700 | [diff] [blame] | 55 | unsigned int min_ratio; |
Peter Zijlstra | a42dde0 | 2008-04-30 00:54:36 -0700 | [diff] [blame] | 56 | unsigned int max_ratio, max_prop_frac; |
Peter Zijlstra | 189d3c4 | 2008-04-30 00:54:35 -0700 | [diff] [blame] | 57 | |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 58 | struct device *dev; |
Miklos Szeredi | 76f1418 | 2008-04-30 00:54:36 -0700 | [diff] [blame^] | 59 | |
| 60 | #ifdef CONFIG_DEBUG_FS |
| 61 | struct dentry *debug_dir; |
| 62 | struct dentry *debug_stats; |
| 63 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 66 | int bdi_init(struct backing_dev_info *bdi); |
| 67 | void bdi_destroy(struct backing_dev_info *bdi); |
| 68 | |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 69 | int bdi_register(struct backing_dev_info *bdi, struct device *parent, |
| 70 | const char *fmt, ...); |
| 71 | int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev); |
| 72 | void bdi_unregister(struct backing_dev_info *bdi); |
| 73 | |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 74 | static inline void __add_bdi_stat(struct backing_dev_info *bdi, |
| 75 | enum bdi_stat_item item, s64 amount) |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 76 | { |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 77 | __percpu_counter_add(&bdi->bdi_stat[item], amount, BDI_STAT_BATCH); |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 80 | static inline void __inc_bdi_stat(struct backing_dev_info *bdi, |
| 81 | enum bdi_stat_item item) |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 82 | { |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 83 | __add_bdi_stat(bdi, item, 1); |
| 84 | } |
| 85 | |
| 86 | static inline void inc_bdi_stat(struct backing_dev_info *bdi, |
| 87 | enum bdi_stat_item item) |
| 88 | { |
| 89 | unsigned long flags; |
| 90 | |
| 91 | local_irq_save(flags); |
| 92 | __inc_bdi_stat(bdi, item); |
| 93 | local_irq_restore(flags); |
| 94 | } |
| 95 | |
| 96 | static inline void __dec_bdi_stat(struct backing_dev_info *bdi, |
| 97 | enum bdi_stat_item item) |
| 98 | { |
| 99 | __add_bdi_stat(bdi, item, -1); |
| 100 | } |
| 101 | |
| 102 | static inline void dec_bdi_stat(struct backing_dev_info *bdi, |
| 103 | enum bdi_stat_item item) |
| 104 | { |
| 105 | unsigned long flags; |
| 106 | |
| 107 | local_irq_save(flags); |
| 108 | __dec_bdi_stat(bdi, item); |
| 109 | local_irq_restore(flags); |
| 110 | } |
| 111 | |
| 112 | static inline s64 bdi_stat(struct backing_dev_info *bdi, |
| 113 | enum bdi_stat_item item) |
| 114 | { |
| 115 | return percpu_counter_read_positive(&bdi->bdi_stat[item]); |
| 116 | } |
| 117 | |
| 118 | static inline s64 __bdi_stat_sum(struct backing_dev_info *bdi, |
| 119 | enum bdi_stat_item item) |
| 120 | { |
| 121 | return percpu_counter_sum_positive(&bdi->bdi_stat[item]); |
| 122 | } |
| 123 | |
| 124 | static inline s64 bdi_stat_sum(struct backing_dev_info *bdi, |
| 125 | enum bdi_stat_item item) |
| 126 | { |
| 127 | s64 sum; |
| 128 | unsigned long flags; |
| 129 | |
| 130 | local_irq_save(flags); |
| 131 | sum = __bdi_stat_sum(bdi, item); |
| 132 | local_irq_restore(flags); |
| 133 | |
| 134 | return sum; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * maximal error of a stat counter. |
| 139 | */ |
| 140 | static inline unsigned long bdi_stat_error(struct backing_dev_info *bdi) |
| 141 | { |
| 142 | #ifdef CONFIG_SMP |
| 143 | return nr_cpu_ids * BDI_STAT_BATCH; |
| 144 | #else |
| 145 | return 1; |
| 146 | #endif |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 147 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Peter Zijlstra | 189d3c4 | 2008-04-30 00:54:35 -0700 | [diff] [blame] | 149 | 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] | 150 | 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] | 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | /* |
| 153 | * Flags in backing_dev_info::capability |
| 154 | * - The first two flags control whether dirty pages will contribute to the |
| 155 | * VM's accounting and whether writepages() should be called for dirty pages |
| 156 | * (something that would not, for example, be appropriate for ramfs) |
| 157 | * - These flags let !MMU mmap() govern direct device mapping vs immediate |
| 158 | * copying more easily for MAP_PRIVATE, especially for ROM filesystems |
| 159 | */ |
| 160 | #define BDI_CAP_NO_ACCT_DIRTY 0x00000001 /* Dirty pages shouldn't contribute to accounting */ |
| 161 | #define BDI_CAP_NO_WRITEBACK 0x00000002 /* Don't write pages back */ |
| 162 | #define BDI_CAP_MAP_COPY 0x00000004 /* Copy can be mapped (MAP_PRIVATE) */ |
| 163 | #define BDI_CAP_MAP_DIRECT 0x00000008 /* Can be mapped directly (MAP_SHARED) */ |
| 164 | #define BDI_CAP_READ_MAP 0x00000010 /* Can be mapped for reading */ |
| 165 | #define BDI_CAP_WRITE_MAP 0x00000020 /* Can be mapped for writing */ |
| 166 | #define BDI_CAP_EXEC_MAP 0x00000040 /* Can be mapped for execution */ |
| 167 | #define BDI_CAP_VMFLAGS \ |
| 168 | (BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | BDI_CAP_EXEC_MAP) |
| 169 | |
| 170 | #if defined(VM_MAYREAD) && \ |
| 171 | (BDI_CAP_READ_MAP != VM_MAYREAD || \ |
| 172 | BDI_CAP_WRITE_MAP != VM_MAYWRITE || \ |
| 173 | BDI_CAP_EXEC_MAP != VM_MAYEXEC) |
| 174 | #error please change backing_dev_info::capabilities flags |
| 175 | #endif |
| 176 | |
| 177 | extern struct backing_dev_info default_backing_dev_info; |
| 178 | void default_unplug_io_fn(struct backing_dev_info *bdi, struct page *page); |
| 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | int writeback_in_progress(struct backing_dev_info *bdi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
| 182 | static inline int bdi_congested(struct backing_dev_info *bdi, int bdi_bits) |
| 183 | { |
| 184 | if (bdi->congested_fn) |
| 185 | return bdi->congested_fn(bdi->congested_data, bdi_bits); |
| 186 | return (bdi->state & bdi_bits); |
| 187 | } |
| 188 | |
| 189 | static inline int bdi_read_congested(struct backing_dev_info *bdi) |
| 190 | { |
| 191 | return bdi_congested(bdi, 1 << BDI_read_congested); |
| 192 | } |
| 193 | |
| 194 | static inline int bdi_write_congested(struct backing_dev_info *bdi) |
| 195 | { |
| 196 | return bdi_congested(bdi, 1 << BDI_write_congested); |
| 197 | } |
| 198 | |
| 199 | static inline int bdi_rw_congested(struct backing_dev_info *bdi) |
| 200 | { |
| 201 | return bdi_congested(bdi, (1 << BDI_read_congested)| |
| 202 | (1 << BDI_write_congested)); |
| 203 | } |
| 204 | |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 205 | void clear_bdi_congested(struct backing_dev_info *bdi, int rw); |
| 206 | void set_bdi_congested(struct backing_dev_info *bdi, int rw); |
| 207 | long congestion_wait(int rw, long timeout); |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | #define bdi_cap_writeback_dirty(bdi) \ |
| 210 | (!((bdi)->capabilities & BDI_CAP_NO_WRITEBACK)) |
| 211 | |
| 212 | #define bdi_cap_account_dirty(bdi) \ |
| 213 | (!((bdi)->capabilities & BDI_CAP_NO_ACCT_DIRTY)) |
| 214 | |
| 215 | #define mapping_cap_writeback_dirty(mapping) \ |
| 216 | bdi_cap_writeback_dirty((mapping)->backing_dev_info) |
| 217 | |
| 218 | #define mapping_cap_account_dirty(mapping) \ |
| 219 | bdi_cap_account_dirty((mapping)->backing_dev_info) |
| 220 | |
| 221 | |
| 222 | #endif /* _LINUX_BACKING_DEV_H */ |