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