Linux 2.6.31-rc1
[linux-2.6/next.git] / mm / backing-dev.c
blob493b468a503541fd65b64872eece6fb7228e36fa
2 #include <linux/wait.h>
3 #include <linux/backing-dev.h>
4 #include <linux/fs.h>
5 #include <linux/pagemap.h>
6 #include <linux/sched.h>
7 #include <linux/module.h>
8 #include <linux/writeback.h>
9 #include <linux/device.h>
11 void default_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
14 EXPORT_SYMBOL(default_unplug_io_fn);
16 struct backing_dev_info default_backing_dev_info = {
17 .ra_pages = VM_MAX_READAHEAD * 1024 / PAGE_CACHE_SIZE,
18 .state = 0,
19 .capabilities = BDI_CAP_MAP_COPY,
20 .unplug_io_fn = default_unplug_io_fn,
22 EXPORT_SYMBOL_GPL(default_backing_dev_info);
24 static struct class *bdi_class;
26 #ifdef CONFIG_DEBUG_FS
27 #include <linux/debugfs.h>
28 #include <linux/seq_file.h>
30 static struct dentry *bdi_debug_root;
32 static void bdi_debug_init(void)
34 bdi_debug_root = debugfs_create_dir("bdi", NULL);
37 static int bdi_debug_stats_show(struct seq_file *m, void *v)
39 struct backing_dev_info *bdi = m->private;
40 unsigned long background_thresh;
41 unsigned long dirty_thresh;
42 unsigned long bdi_thresh;
44 get_dirty_limits(&background_thresh, &dirty_thresh, &bdi_thresh, bdi);
46 #define K(x) ((x) << (PAGE_SHIFT - 10))
47 seq_printf(m,
48 "BdiWriteback: %8lu kB\n"
49 "BdiReclaimable: %8lu kB\n"
50 "BdiDirtyThresh: %8lu kB\n"
51 "DirtyThresh: %8lu kB\n"
52 "BackgroundThresh: %8lu kB\n",
53 (unsigned long) K(bdi_stat(bdi, BDI_WRITEBACK)),
54 (unsigned long) K(bdi_stat(bdi, BDI_RECLAIMABLE)),
55 K(bdi_thresh),
56 K(dirty_thresh),
57 K(background_thresh));
58 #undef K
60 return 0;
63 static int bdi_debug_stats_open(struct inode *inode, struct file *file)
65 return single_open(file, bdi_debug_stats_show, inode->i_private);
68 static const struct file_operations bdi_debug_stats_fops = {
69 .open = bdi_debug_stats_open,
70 .read = seq_read,
71 .llseek = seq_lseek,
72 .release = single_release,
75 static void bdi_debug_register(struct backing_dev_info *bdi, const char *name)
77 bdi->debug_dir = debugfs_create_dir(name, bdi_debug_root);
78 bdi->debug_stats = debugfs_create_file("stats", 0444, bdi->debug_dir,
79 bdi, &bdi_debug_stats_fops);
82 static void bdi_debug_unregister(struct backing_dev_info *bdi)
84 debugfs_remove(bdi->debug_stats);
85 debugfs_remove(bdi->debug_dir);
87 #else
88 static inline void bdi_debug_init(void)
91 static inline void bdi_debug_register(struct backing_dev_info *bdi,
92 const char *name)
95 static inline void bdi_debug_unregister(struct backing_dev_info *bdi)
98 #endif
100 static ssize_t read_ahead_kb_store(struct device *dev,
101 struct device_attribute *attr,
102 const char *buf, size_t count)
104 struct backing_dev_info *bdi = dev_get_drvdata(dev);
105 char *end;
106 unsigned long read_ahead_kb;
107 ssize_t ret = -EINVAL;
109 read_ahead_kb = simple_strtoul(buf, &end, 10);
110 if (*buf && (end[0] == '\0' || (end[0] == '\n' && end[1] == '\0'))) {
111 bdi->ra_pages = read_ahead_kb >> (PAGE_SHIFT - 10);
112 ret = count;
114 return ret;
117 #define K(pages) ((pages) << (PAGE_SHIFT - 10))
119 #define BDI_SHOW(name, expr) \
120 static ssize_t name##_show(struct device *dev, \
121 struct device_attribute *attr, char *page) \
123 struct backing_dev_info *bdi = dev_get_drvdata(dev); \
125 return snprintf(page, PAGE_SIZE-1, "%lld\n", (long long)expr); \
128 BDI_SHOW(read_ahead_kb, K(bdi->ra_pages))
130 static ssize_t min_ratio_store(struct device *dev,
131 struct device_attribute *attr, const char *buf, size_t count)
133 struct backing_dev_info *bdi = dev_get_drvdata(dev);
134 char *end;
135 unsigned int ratio;
136 ssize_t ret = -EINVAL;
138 ratio = simple_strtoul(buf, &end, 10);
139 if (*buf && (end[0] == '\0' || (end[0] == '\n' && end[1] == '\0'))) {
140 ret = bdi_set_min_ratio(bdi, ratio);
141 if (!ret)
142 ret = count;
144 return ret;
146 BDI_SHOW(min_ratio, bdi->min_ratio)
148 static ssize_t max_ratio_store(struct device *dev,
149 struct device_attribute *attr, const char *buf, size_t count)
151 struct backing_dev_info *bdi = dev_get_drvdata(dev);
152 char *end;
153 unsigned int ratio;
154 ssize_t ret = -EINVAL;
156 ratio = simple_strtoul(buf, &end, 10);
157 if (*buf && (end[0] == '\0' || (end[0] == '\n' && end[1] == '\0'))) {
158 ret = bdi_set_max_ratio(bdi, ratio);
159 if (!ret)
160 ret = count;
162 return ret;
164 BDI_SHOW(max_ratio, bdi->max_ratio)
166 #define __ATTR_RW(attr) __ATTR(attr, 0644, attr##_show, attr##_store)
168 static struct device_attribute bdi_dev_attrs[] = {
169 __ATTR_RW(read_ahead_kb),
170 __ATTR_RW(min_ratio),
171 __ATTR_RW(max_ratio),
172 __ATTR_NULL,
175 static __init int bdi_class_init(void)
177 bdi_class = class_create(THIS_MODULE, "bdi");
178 bdi_class->dev_attrs = bdi_dev_attrs;
179 bdi_debug_init();
180 return 0;
182 postcore_initcall(bdi_class_init);
184 static int __init default_bdi_init(void)
186 int err;
188 err = bdi_init(&default_backing_dev_info);
189 if (!err)
190 bdi_register(&default_backing_dev_info, NULL, "default");
192 return err;
194 subsys_initcall(default_bdi_init);
196 int bdi_register(struct backing_dev_info *bdi, struct device *parent,
197 const char *fmt, ...)
199 va_list args;
200 int ret = 0;
201 struct device *dev;
203 if (bdi->dev) /* The driver needs to use separate queues per device */
204 goto exit;
206 va_start(args, fmt);
207 dev = device_create_vargs(bdi_class, parent, MKDEV(0, 0), bdi, fmt, args);
208 va_end(args);
209 if (IS_ERR(dev)) {
210 ret = PTR_ERR(dev);
211 goto exit;
214 bdi->dev = dev;
215 bdi_debug_register(bdi, dev_name(dev));
217 exit:
218 return ret;
220 EXPORT_SYMBOL(bdi_register);
222 int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev)
224 return bdi_register(bdi, NULL, "%u:%u", MAJOR(dev), MINOR(dev));
226 EXPORT_SYMBOL(bdi_register_dev);
228 void bdi_unregister(struct backing_dev_info *bdi)
230 if (bdi->dev) {
231 bdi_debug_unregister(bdi);
232 device_unregister(bdi->dev);
233 bdi->dev = NULL;
236 EXPORT_SYMBOL(bdi_unregister);
238 int bdi_init(struct backing_dev_info *bdi)
240 int i;
241 int err;
243 bdi->dev = NULL;
245 bdi->min_ratio = 0;
246 bdi->max_ratio = 100;
247 bdi->max_prop_frac = PROP_FRAC_BASE;
249 for (i = 0; i < NR_BDI_STAT_ITEMS; i++) {
250 err = percpu_counter_init(&bdi->bdi_stat[i], 0);
251 if (err)
252 goto err;
255 bdi->dirty_exceeded = 0;
256 err = prop_local_init_percpu(&bdi->completions);
258 if (err) {
259 err:
260 while (i--)
261 percpu_counter_destroy(&bdi->bdi_stat[i]);
264 return err;
266 EXPORT_SYMBOL(bdi_init);
268 void bdi_destroy(struct backing_dev_info *bdi)
270 int i;
272 bdi_unregister(bdi);
274 for (i = 0; i < NR_BDI_STAT_ITEMS; i++)
275 percpu_counter_destroy(&bdi->bdi_stat[i]);
277 prop_local_destroy_percpu(&bdi->completions);
279 EXPORT_SYMBOL(bdi_destroy);
281 static wait_queue_head_t congestion_wqh[2] = {
282 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
283 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
287 void clear_bdi_congested(struct backing_dev_info *bdi, int sync)
289 enum bdi_state bit;
290 wait_queue_head_t *wqh = &congestion_wqh[sync];
292 bit = sync ? BDI_sync_congested : BDI_async_congested;
293 clear_bit(bit, &bdi->state);
294 smp_mb__after_clear_bit();
295 if (waitqueue_active(wqh))
296 wake_up(wqh);
298 EXPORT_SYMBOL(clear_bdi_congested);
300 void set_bdi_congested(struct backing_dev_info *bdi, int sync)
302 enum bdi_state bit;
304 bit = sync ? BDI_sync_congested : BDI_async_congested;
305 set_bit(bit, &bdi->state);
307 EXPORT_SYMBOL(set_bdi_congested);
310 * congestion_wait - wait for a backing_dev to become uncongested
311 * @rw: READ or WRITE
312 * @timeout: timeout in jiffies
314 * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
315 * write congestion. If no backing_devs are congested then just wait for the
316 * next write to be completed.
318 long congestion_wait(int rw, long timeout)
320 long ret;
321 DEFINE_WAIT(wait);
322 wait_queue_head_t *wqh = &congestion_wqh[rw];
324 prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
325 ret = io_schedule_timeout(timeout);
326 finish_wait(wqh, &wait);
327 return ret;
329 EXPORT_SYMBOL(congestion_wait);