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