blob: bee52abb8a4dbfd46e53f650d7d7dbca881a9169 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Zijlstrab2e8fb62007-10-16 23:25:47 -070011#include <linux/percpu_counter.h>
12#include <linux/log2.h>
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -070013#include <linux/proportions.h>
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -070014#include <linux/kernel.h>
Miklos Szeredie4ad08f2008-04-30 00:54:37 -070015#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/atomic.h>
17
Andrew Morton3fcfab12006-10-19 23:28:16 -070018struct page;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -070019struct device;
Miklos Szeredi76f14182008-04-30 00:54:36 -070020struct dentry;
Andrew Morton3fcfab12006-10-19 23:28:16 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022/*
23 * Bits in backing_dev_info.state
24 */
25enum bdi_state {
26 BDI_pdflush, /* A pdflush thread is working this device */
27 BDI_write_congested, /* The write queue is getting full */
28 BDI_read_congested, /* The read queue is getting full */
29 BDI_unused, /* Available bits start here */
30};
31
32typedef int (congested_fn)(void *, int);
33
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070034enum bdi_stat_item {
Peter Zijlstrac9e51e42007-10-16 23:25:47 -070035 BDI_RECLAIMABLE,
Peter Zijlstra69cb51d2007-10-16 23:25:48 -070036 BDI_WRITEBACK,
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070037 NR_BDI_STAT_ITEMS
38};
39
40#define BDI_STAT_BATCH (8*(1+ilog2(nr_cpu_ids)))
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042struct backing_dev_info {
43 unsigned long ra_pages; /* max readahead in PAGE_CACHE_SIZE units */
44 unsigned long state; /* Always use atomic bitops on this */
45 unsigned int capabilities; /* Device capabilities */
46 congested_fn *congested_fn; /* Function pointer if device is md/dm */
47 void *congested_data; /* Pointer to aux data for congested func */
48 void (*unplug_io_fn)(struct backing_dev_info *, struct page *);
49 void *unplug_io_data;
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070050
51 struct percpu_counter bdi_stat[NR_BDI_STAT_ITEMS];
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -070052
53 struct prop_local_percpu completions;
54 int dirty_exceeded;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -070055
Peter Zijlstra189d3c42008-04-30 00:54:35 -070056 unsigned int min_ratio;
Peter Zijlstraa42dde02008-04-30 00:54:36 -070057 unsigned int max_ratio, max_prop_frac;
Peter Zijlstra189d3c42008-04-30 00:54:35 -070058
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -070059 struct device *dev;
Miklos Szeredi76f14182008-04-30 00:54:36 -070060
61#ifdef CONFIG_DEBUG_FS
62 struct dentry *debug_dir;
63 struct dentry *debug_stats;
64#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070067int bdi_init(struct backing_dev_info *bdi);
68void bdi_destroy(struct backing_dev_info *bdi);
69
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -070070int bdi_register(struct backing_dev_info *bdi, struct device *parent,
71 const char *fmt, ...);
72int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev);
73void bdi_unregister(struct backing_dev_info *bdi);
74
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070075static inline void __add_bdi_stat(struct backing_dev_info *bdi,
76 enum bdi_stat_item item, s64 amount)
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -070077{
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070078 __percpu_counter_add(&bdi->bdi_stat[item], amount, BDI_STAT_BATCH);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -070079}
80
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070081static inline void __inc_bdi_stat(struct backing_dev_info *bdi,
82 enum bdi_stat_item item)
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -070083{
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -070084 __add_bdi_stat(bdi, item, 1);
85}
86
87static inline void inc_bdi_stat(struct backing_dev_info *bdi,
88 enum bdi_stat_item item)
89{
90 unsigned long flags;
91
92 local_irq_save(flags);
93 __inc_bdi_stat(bdi, item);
94 local_irq_restore(flags);
95}
96
97static inline void __dec_bdi_stat(struct backing_dev_info *bdi,
98 enum bdi_stat_item item)
99{
100 __add_bdi_stat(bdi, item, -1);
101}
102
103static inline void dec_bdi_stat(struct backing_dev_info *bdi,
104 enum bdi_stat_item item)
105{
106 unsigned long flags;
107
108 local_irq_save(flags);
109 __dec_bdi_stat(bdi, item);
110 local_irq_restore(flags);
111}
112
113static inline s64 bdi_stat(struct backing_dev_info *bdi,
114 enum bdi_stat_item item)
115{
116 return percpu_counter_read_positive(&bdi->bdi_stat[item]);
117}
118
119static inline s64 __bdi_stat_sum(struct backing_dev_info *bdi,
120 enum bdi_stat_item item)
121{
122 return percpu_counter_sum_positive(&bdi->bdi_stat[item]);
123}
124
125static inline s64 bdi_stat_sum(struct backing_dev_info *bdi,
126 enum bdi_stat_item item)
127{
128 s64 sum;
129 unsigned long flags;
130
131 local_irq_save(flags);
132 sum = __bdi_stat_sum(bdi, item);
133 local_irq_restore(flags);
134
135 return sum;
136}
137
Miklos Szeredidd5656e2008-04-30 00:54:37 -0700138extern void bdi_writeout_inc(struct backing_dev_info *bdi);
139
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700140/*
141 * maximal error of a stat counter.
142 */
143static inline unsigned long bdi_stat_error(struct backing_dev_info *bdi)
144{
145#ifdef CONFIG_SMP
146 return nr_cpu_ids * BDI_STAT_BATCH;
147#else
148 return 1;
149#endif
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700150}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700152int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio);
Peter Zijlstraa42dde02008-04-30 00:54:36 -0700153int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ratio);
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/*
156 * Flags in backing_dev_info::capability
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700157 *
158 * The first three flags control whether dirty pages will contribute to the
159 * VM's accounting and whether writepages() should be called for dirty pages
160 * (something that would not, for example, be appropriate for ramfs)
161 *
162 * WARNING: these flags are closely related and should not normally be
163 * used separately. The BDI_CAP_NO_ACCT_AND_WRITEBACK combines these
164 * three flags into a single convenience macro.
165 *
166 * BDI_CAP_NO_ACCT_DIRTY: Dirty pages shouldn't contribute to accounting
167 * BDI_CAP_NO_WRITEBACK: Don't write pages back
168 * BDI_CAP_NO_ACCT_WB: Don't automatically account writeback pages
169 *
170 * These flags let !MMU mmap() govern direct device mapping vs immediate
171 * copying more easily for MAP_PRIVATE, especially for ROM filesystems.
172 *
173 * BDI_CAP_MAP_COPY: Copy can be mapped (MAP_PRIVATE)
174 * BDI_CAP_MAP_DIRECT: Can be mapped directly (MAP_SHARED)
175 * BDI_CAP_READ_MAP: Can be mapped for reading
176 * BDI_CAP_WRITE_MAP: Can be mapped for writing
177 * BDI_CAP_EXEC_MAP: Can be mapped for execution
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700178 *
179 * BDI_CAP_SWAP_BACKED: Count shmem/tmpfs objects as swap-backed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700181#define BDI_CAP_NO_ACCT_DIRTY 0x00000001
182#define BDI_CAP_NO_WRITEBACK 0x00000002
183#define BDI_CAP_MAP_COPY 0x00000004
184#define BDI_CAP_MAP_DIRECT 0x00000008
185#define BDI_CAP_READ_MAP 0x00000010
186#define BDI_CAP_WRITE_MAP 0x00000020
187#define BDI_CAP_EXEC_MAP 0x00000040
188#define BDI_CAP_NO_ACCT_WB 0x00000080
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700189#define BDI_CAP_SWAP_BACKED 0x00000100
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#define BDI_CAP_VMFLAGS \
192 (BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | BDI_CAP_EXEC_MAP)
193
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700194#define BDI_CAP_NO_ACCT_AND_WRITEBACK \
195 (BDI_CAP_NO_WRITEBACK | BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_ACCT_WB)
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197#if defined(VM_MAYREAD) && \
198 (BDI_CAP_READ_MAP != VM_MAYREAD || \
199 BDI_CAP_WRITE_MAP != VM_MAYWRITE || \
200 BDI_CAP_EXEC_MAP != VM_MAYEXEC)
201#error please change backing_dev_info::capabilities flags
202#endif
203
204extern struct backing_dev_info default_backing_dev_info;
205void default_unplug_io_fn(struct backing_dev_info *bdi, struct page *page);
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207int writeback_in_progress(struct backing_dev_info *bdi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209static inline int bdi_congested(struct backing_dev_info *bdi, int bdi_bits)
210{
211 if (bdi->congested_fn)
212 return bdi->congested_fn(bdi->congested_data, bdi_bits);
213 return (bdi->state & bdi_bits);
214}
215
216static inline int bdi_read_congested(struct backing_dev_info *bdi)
217{
218 return bdi_congested(bdi, 1 << BDI_read_congested);
219}
220
221static inline int bdi_write_congested(struct backing_dev_info *bdi)
222{
223 return bdi_congested(bdi, 1 << BDI_write_congested);
224}
225
226static inline int bdi_rw_congested(struct backing_dev_info *bdi)
227{
228 return bdi_congested(bdi, (1 << BDI_read_congested)|
229 (1 << BDI_write_congested));
230}
231
Andrew Morton3fcfab12006-10-19 23:28:16 -0700232void clear_bdi_congested(struct backing_dev_info *bdi, int rw);
233void set_bdi_congested(struct backing_dev_info *bdi, int rw);
234long congestion_wait(int rw, long timeout);
Andrew Morton3fcfab12006-10-19 23:28:16 -0700235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700237static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi)
238{
239 return !(bdi->capabilities & BDI_CAP_NO_WRITEBACK);
240}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700242static inline bool bdi_cap_account_dirty(struct backing_dev_info *bdi)
243{
244 return !(bdi->capabilities & BDI_CAP_NO_ACCT_DIRTY);
245}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700247static inline bool bdi_cap_account_writeback(struct backing_dev_info *bdi)
248{
249 /* Paranoia: BDI_CAP_NO_WRITEBACK implies BDI_CAP_NO_ACCT_WB */
250 return !(bdi->capabilities & (BDI_CAP_NO_ACCT_WB |
251 BDI_CAP_NO_WRITEBACK));
252}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700254static inline bool bdi_cap_swap_backed(struct backing_dev_info *bdi)
255{
256 return bdi->capabilities & BDI_CAP_SWAP_BACKED;
257}
258
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700259static inline bool mapping_cap_writeback_dirty(struct address_space *mapping)
260{
261 return bdi_cap_writeback_dirty(mapping->backing_dev_info);
262}
263
264static inline bool mapping_cap_account_dirty(struct address_space *mapping)
265{
266 return bdi_cap_account_dirty(mapping->backing_dev_info);
267}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700269static inline bool mapping_cap_swap_backed(struct address_space *mapping)
270{
271 return bdi_cap_swap_backed(mapping->backing_dev_info);
272}
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274#endif /* _LINUX_BACKING_DEV_H */