blob: c86edd2442944ec2532bdde3e2087b56519a224d [file] [log] [blame]
Andrew Morton3fcfab12006-10-19 23:28:16 -07001
2#include <linux/wait.h>
3#include <linux/backing-dev.h>
4#include <linux/fs.h>
Jens Axboe26160152009-03-17 09:35:06 +01005#include <linux/pagemap.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -07006#include <linux/sched.h>
7#include <linux/module.h>
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -07008#include <linux/writeback.h>
9#include <linux/device.h>
10
Jens Axboe26160152009-03-17 09:35:06 +010011void default_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
12{
13}
14EXPORT_SYMBOL(default_unplug_io_fn);
15
16struct 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,
21};
22EXPORT_SYMBOL_GPL(default_backing_dev_info);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -070023
24static struct class *bdi_class;
25
Miklos Szeredi76f14182008-04-30 00:54:36 -070026#ifdef CONFIG_DEBUG_FS
27#include <linux/debugfs.h>
28#include <linux/seq_file.h>
29
30static struct dentry *bdi_debug_root;
31
32static void bdi_debug_init(void)
33{
34 bdi_debug_root = debugfs_create_dir("bdi", NULL);
35}
36
37static int bdi_debug_stats_show(struct seq_file *m, void *v)
38{
39 struct backing_dev_info *bdi = m->private;
David Rientjes364aeb22009-01-06 14:39:29 -080040 unsigned long background_thresh;
41 unsigned long dirty_thresh;
42 unsigned long bdi_thresh;
Miklos Szeredi76f14182008-04-30 00:54:36 -070043
44 get_dirty_limits(&background_thresh, &dirty_thresh, &bdi_thresh, bdi);
45
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
59
60 return 0;
61}
62
63static int bdi_debug_stats_open(struct inode *inode, struct file *file)
64{
65 return single_open(file, bdi_debug_stats_show, inode->i_private);
66}
67
68static 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,
73};
74
75static void bdi_debug_register(struct backing_dev_info *bdi, const char *name)
76{
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);
80}
81
82static void bdi_debug_unregister(struct backing_dev_info *bdi)
83{
84 debugfs_remove(bdi->debug_stats);
85 debugfs_remove(bdi->debug_dir);
86}
87#else
88static inline void bdi_debug_init(void)
89{
90}
91static inline void bdi_debug_register(struct backing_dev_info *bdi,
92 const char *name)
93{
94}
95static inline void bdi_debug_unregister(struct backing_dev_info *bdi)
96{
97}
98#endif
99
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700100static ssize_t read_ahead_kb_store(struct device *dev,
101 struct device_attribute *attr,
102 const char *buf, size_t count)
103{
104 struct backing_dev_info *bdi = dev_get_drvdata(dev);
105 char *end;
106 unsigned long read_ahead_kb;
107 ssize_t ret = -EINVAL;
108
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;
113 }
114 return ret;
115}
116
117#define K(pages) ((pages) << (PAGE_SHIFT - 10))
118
119#define BDI_SHOW(name, expr) \
120static ssize_t name##_show(struct device *dev, \
121 struct device_attribute *attr, char *page) \
122{ \
123 struct backing_dev_info *bdi = dev_get_drvdata(dev); \
124 \
125 return snprintf(page, PAGE_SIZE-1, "%lld\n", (long long)expr); \
126}
127
128BDI_SHOW(read_ahead_kb, K(bdi->ra_pages))
129
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700130static ssize_t min_ratio_store(struct device *dev,
131 struct device_attribute *attr, const char *buf, size_t count)
132{
133 struct backing_dev_info *bdi = dev_get_drvdata(dev);
134 char *end;
135 unsigned int ratio;
136 ssize_t ret = -EINVAL;
137
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;
143 }
144 return ret;
145}
146BDI_SHOW(min_ratio, bdi->min_ratio)
147
Peter Zijlstraa42dde02008-04-30 00:54:36 -0700148static ssize_t max_ratio_store(struct device *dev,
149 struct device_attribute *attr, const char *buf, size_t count)
150{
151 struct backing_dev_info *bdi = dev_get_drvdata(dev);
152 char *end;
153 unsigned int ratio;
154 ssize_t ret = -EINVAL;
155
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;
161 }
162 return ret;
163}
164BDI_SHOW(max_ratio, bdi->max_ratio)
165
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700166#define __ATTR_RW(attr) __ATTR(attr, 0644, attr##_show, attr##_store)
167
168static struct device_attribute bdi_dev_attrs[] = {
169 __ATTR_RW(read_ahead_kb),
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700170 __ATTR_RW(min_ratio),
Peter Zijlstraa42dde02008-04-30 00:54:36 -0700171 __ATTR_RW(max_ratio),
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700172 __ATTR_NULL,
173};
174
175static __init int bdi_class_init(void)
176{
177 bdi_class = class_create(THIS_MODULE, "bdi");
178 bdi_class->dev_attrs = bdi_dev_attrs;
Miklos Szeredi76f14182008-04-30 00:54:36 -0700179 bdi_debug_init();
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700180 return 0;
181}
Miklos Szeredi76f14182008-04-30 00:54:36 -0700182postcore_initcall(bdi_class_init);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700183
Jens Axboe26160152009-03-17 09:35:06 +0100184static int __init default_bdi_init(void)
185{
186 int err;
187
188 err = bdi_init(&default_backing_dev_info);
189 if (!err)
190 bdi_register(&default_backing_dev_info, NULL, "default");
191
192 return err;
193}
194subsys_initcall(default_bdi_init);
195
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700196int bdi_register(struct backing_dev_info *bdi, struct device *parent,
197 const char *fmt, ...)
198{
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700199 va_list args;
200 int ret = 0;
201 struct device *dev;
202
Andrew Morton69fc2082008-12-09 13:14:06 -0800203 if (bdi->dev) /* The driver needs to use separate queues per device */
Kay Sieversf1d0b062008-12-02 10:31:50 -0800204 goto exit;
205
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700206 va_start(args, fmt);
Greg Kroah-Hartman19051c52008-05-15 13:44:08 -0700207 dev = device_create_vargs(bdi_class, parent, MKDEV(0, 0), bdi, fmt, args);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700208 va_end(args);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700209 if (IS_ERR(dev)) {
210 ret = PTR_ERR(dev);
211 goto exit;
212 }
213
214 bdi->dev = dev;
Greg Kroah-Hartman19051c52008-05-15 13:44:08 -0700215 bdi_debug_register(bdi, dev_name(dev));
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700216
217exit:
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700218 return ret;
219}
220EXPORT_SYMBOL(bdi_register);
221
222int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev)
223{
224 return bdi_register(bdi, NULL, "%u:%u", MAJOR(dev), MINOR(dev));
225}
226EXPORT_SYMBOL(bdi_register_dev);
227
228void bdi_unregister(struct backing_dev_info *bdi)
229{
230 if (bdi->dev) {
Miklos Szeredi76f14182008-04-30 00:54:36 -0700231 bdi_debug_unregister(bdi);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700232 device_unregister(bdi->dev);
233 bdi->dev = NULL;
234 }
235}
236EXPORT_SYMBOL(bdi_unregister);
Andrew Morton3fcfab12006-10-19 23:28:16 -0700237
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700238int bdi_init(struct backing_dev_info *bdi)
239{
Denis Cheng4b01a0b2007-12-04 23:45:07 -0800240 int i;
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700241 int err;
242
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700243 bdi->dev = NULL;
244
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700245 bdi->min_ratio = 0;
Peter Zijlstraa42dde02008-04-30 00:54:36 -0700246 bdi->max_ratio = 100;
247 bdi->max_prop_frac = PROP_FRAC_BASE;
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700248
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700249 for (i = 0; i < NR_BDI_STAT_ITEMS; i++) {
Peter Zijlstraea319512008-12-26 15:08:55 +0100250 err = percpu_counter_init(&bdi->bdi_stat[i], 0);
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700251 if (err)
252 goto err;
253 }
254
255 bdi->dirty_exceeded = 0;
256 err = prop_local_init_percpu(&bdi->completions);
257
258 if (err) {
259err:
Denis Cheng4b01a0b2007-12-04 23:45:07 -0800260 while (i--)
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700261 percpu_counter_destroy(&bdi->bdi_stat[i]);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700262 }
263
264 return err;
265}
266EXPORT_SYMBOL(bdi_init);
267
268void bdi_destroy(struct backing_dev_info *bdi)
269{
270 int i;
271
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700272 bdi_unregister(bdi);
273
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700274 for (i = 0; i < NR_BDI_STAT_ITEMS; i++)
275 percpu_counter_destroy(&bdi->bdi_stat[i]);
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700276
277 prop_local_destroy_percpu(&bdi->completions);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700278}
279EXPORT_SYMBOL(bdi_destroy);
280
Andrew Morton3fcfab12006-10-19 23:28:16 -0700281static wait_queue_head_t congestion_wqh[2] = {
282 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
283 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
284 };
285
Jens Axboe1faa16d2009-04-06 14:48:01 +0200286void clear_bdi_congested(struct backing_dev_info *bdi, int sync)
Andrew Morton3fcfab12006-10-19 23:28:16 -0700287{
288 enum bdi_state bit;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200289 wait_queue_head_t *wqh = &congestion_wqh[sync];
Andrew Morton3fcfab12006-10-19 23:28:16 -0700290
Jens Axboe1faa16d2009-04-06 14:48:01 +0200291 bit = sync ? BDI_sync_congested : BDI_async_congested;
Andrew Morton3fcfab12006-10-19 23:28:16 -0700292 clear_bit(bit, &bdi->state);
293 smp_mb__after_clear_bit();
294 if (waitqueue_active(wqh))
295 wake_up(wqh);
296}
297EXPORT_SYMBOL(clear_bdi_congested);
298
Jens Axboe1faa16d2009-04-06 14:48:01 +0200299void set_bdi_congested(struct backing_dev_info *bdi, int sync)
Andrew Morton3fcfab12006-10-19 23:28:16 -0700300{
301 enum bdi_state bit;
302
Jens Axboe1faa16d2009-04-06 14:48:01 +0200303 bit = sync ? BDI_sync_congested : BDI_async_congested;
Andrew Morton3fcfab12006-10-19 23:28:16 -0700304 set_bit(bit, &bdi->state);
305}
306EXPORT_SYMBOL(set_bdi_congested);
307
308/**
309 * congestion_wait - wait for a backing_dev to become uncongested
Jens Axboe8aa7e842009-07-09 14:52:32 +0200310 * @sync: SYNC or ASYNC IO
Andrew Morton3fcfab12006-10-19 23:28:16 -0700311 * @timeout: timeout in jiffies
312 *
313 * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
314 * write congestion. If no backing_devs are congested then just wait for the
315 * next write to be completed.
316 */
Jens Axboe8aa7e842009-07-09 14:52:32 +0200317long congestion_wait(int sync, long timeout)
Andrew Morton3fcfab12006-10-19 23:28:16 -0700318{
319 long ret;
320 DEFINE_WAIT(wait);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200321 wait_queue_head_t *wqh = &congestion_wqh[sync];
Andrew Morton3fcfab12006-10-19 23:28:16 -0700322
323 prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
324 ret = io_schedule_timeout(timeout);
325 finish_wait(wqh, &wait);
326 return ret;
327}
328EXPORT_SYMBOL(congestion_wait);
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700329