2 * include/linux/backing-dev.h
4 * low-level device information and state which is propagated up through
8 #ifndef _LINUX_BACKING_DEV_H
9 #define _LINUX_BACKING_DEV_H
11 #include <linux/percpu_counter.h>
12 #include <linux/log2.h>
13 #include <asm/atomic.h>
18 * Bits in backing_dev_info.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 */
27 typedef int (congested_fn)(void *, int);
33 #define BDI_STAT_BATCH (8*(1+ilog2(nr_cpu_ids)))
35 struct backing_dev_info {
36 unsigned long ra_pages; /* max readahead in PAGE_CACHE_SIZE units */
37 unsigned long state; /* Always use atomic bitops on this */
38 unsigned int capabilities; /* Device capabilities */
39 congested_fn *congested_fn; /* Function pointer if device is md/dm */
40 void *congested_data; /* Pointer to aux data for congested func */
41 void (*unplug_io_fn)(struct backing_dev_info *, struct page *);
44 struct percpu_counter bdi_stat[NR_BDI_STAT_ITEMS];
47 int bdi_init(struct backing_dev_info *bdi);
48 void bdi_destroy(struct backing_dev_info *bdi);
50 static inline void __add_bdi_stat(struct backing_dev_info *bdi,
51 enum bdi_stat_item item, s64 amount)
53 __percpu_counter_add(&bdi->bdi_stat[item], amount, BDI_STAT_BATCH);
56 static inline void __inc_bdi_stat(struct backing_dev_info *bdi,
57 enum bdi_stat_item item)
59 __add_bdi_stat(bdi, item, 1);
62 static inline void inc_bdi_stat(struct backing_dev_info *bdi,
63 enum bdi_stat_item item)
67 local_irq_save(flags);
68 __inc_bdi_stat(bdi, item);
69 local_irq_restore(flags);
72 static inline void __dec_bdi_stat(struct backing_dev_info *bdi,
73 enum bdi_stat_item item)
75 __add_bdi_stat(bdi, item, -1);
78 static inline void dec_bdi_stat(struct backing_dev_info *bdi,
79 enum bdi_stat_item item)
83 local_irq_save(flags);
84 __dec_bdi_stat(bdi, item);
85 local_irq_restore(flags);
88 static inline s64 bdi_stat(struct backing_dev_info *bdi,
89 enum bdi_stat_item item)
91 return percpu_counter_read_positive(&bdi->bdi_stat[item]);
94 static inline s64 __bdi_stat_sum(struct backing_dev_info *bdi,
95 enum bdi_stat_item item)
97 return percpu_counter_sum_positive(&bdi->bdi_stat[item]);
100 static inline s64 bdi_stat_sum(struct backing_dev_info *bdi,
101 enum bdi_stat_item item)
106 local_irq_save(flags);
107 sum = __bdi_stat_sum(bdi, item);
108 local_irq_restore(flags);
114 * maximal error of a stat counter.
116 static inline unsigned long bdi_stat_error(struct backing_dev_info *bdi)
119 return nr_cpu_ids * BDI_STAT_BATCH;
126 * Flags in backing_dev_info::capability
127 * - The first two flags control whether dirty pages will contribute to the
128 * VM's accounting and whether writepages() should be called for dirty pages
129 * (something that would not, for example, be appropriate for ramfs)
130 * - These flags let !MMU mmap() govern direct device mapping vs immediate
131 * copying more easily for MAP_PRIVATE, especially for ROM filesystems
133 #define BDI_CAP_NO_ACCT_DIRTY 0x00000001 /* Dirty pages shouldn't contribute to accounting */
134 #define BDI_CAP_NO_WRITEBACK 0x00000002 /* Don't write pages back */
135 #define BDI_CAP_MAP_COPY 0x00000004 /* Copy can be mapped (MAP_PRIVATE) */
136 #define BDI_CAP_MAP_DIRECT 0x00000008 /* Can be mapped directly (MAP_SHARED) */
137 #define BDI_CAP_READ_MAP 0x00000010 /* Can be mapped for reading */
138 #define BDI_CAP_WRITE_MAP 0x00000020 /* Can be mapped for writing */
139 #define BDI_CAP_EXEC_MAP 0x00000040 /* Can be mapped for execution */
140 #define BDI_CAP_VMFLAGS \
141 (BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | BDI_CAP_EXEC_MAP)
143 #if defined(VM_MAYREAD) && \
144 (BDI_CAP_READ_MAP != VM_MAYREAD || \
145 BDI_CAP_WRITE_MAP != VM_MAYWRITE || \
146 BDI_CAP_EXEC_MAP != VM_MAYEXEC)
147 #error please change backing_dev_info::capabilities flags
150 extern struct backing_dev_info default_backing_dev_info;
151 void default_unplug_io_fn(struct backing_dev_info *bdi, struct page *page);
153 int writeback_acquire(struct backing_dev_info *bdi);
154 int writeback_in_progress(struct backing_dev_info *bdi);
155 void writeback_release(struct backing_dev_info *bdi);
157 static inline int bdi_congested(struct backing_dev_info *bdi, int bdi_bits)
159 if (bdi->congested_fn)
160 return bdi->congested_fn(bdi->congested_data, bdi_bits);
161 return (bdi->state & bdi_bits);
164 static inline int bdi_read_congested(struct backing_dev_info *bdi)
166 return bdi_congested(bdi, 1 << BDI_read_congested);
169 static inline int bdi_write_congested(struct backing_dev_info *bdi)
171 return bdi_congested(bdi, 1 << BDI_write_congested);
174 static inline int bdi_rw_congested(struct backing_dev_info *bdi)
176 return bdi_congested(bdi, (1 << BDI_read_congested)|
177 (1 << BDI_write_congested));
180 void clear_bdi_congested(struct backing_dev_info *bdi, int rw);
181 void set_bdi_congested(struct backing_dev_info *bdi, int rw);
182 long congestion_wait(int rw, long timeout);
184 #define bdi_cap_writeback_dirty(bdi) \
185 (!((bdi)->capabilities & BDI_CAP_NO_WRITEBACK))
187 #define bdi_cap_account_dirty(bdi) \
188 (!((bdi)->capabilities & BDI_CAP_NO_ACCT_DIRTY))
190 #define mapping_cap_writeback_dirty(mapping) \
191 bdi_cap_writeback_dirty((mapping)->backing_dev_info)
193 #define mapping_cap_account_dirty(mapping) \
194 bdi_cap_account_dirty((mapping)->backing_dev_info)
197 #endif /* _LINUX_BACKING_DEV_H */