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