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