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