mm: bdi: allow setting a maximum for the bdi dirty limit
[linux-2.6.git] / mm / backing-dev.c
1
2 #include <linux/wait.h>
3 #include <linux/backing-dev.h>
4 #include <linux/fs.h>
5 #include <linux/sched.h>
6 #include <linux/module.h>
7 #include <linux/writeback.h>
8 #include <linux/device.h>
9
10
11 static struct class *bdi_class;
12
13 static ssize_t read_ahead_kb_store(struct device *dev,
14                                   struct device_attribute *attr,
15                                   const char *buf, size_t count)
16 {
17         struct backing_dev_info *bdi = dev_get_drvdata(dev);
18         char *end;
19         unsigned long read_ahead_kb;
20         ssize_t ret = -EINVAL;
21
22         read_ahead_kb = simple_strtoul(buf, &end, 10);
23         if (*buf && (end[0] == '\0' || (end[0] == '\n' && end[1] == '\0'))) {
24                 bdi->ra_pages = read_ahead_kb >> (PAGE_SHIFT - 10);
25                 ret = count;
26         }
27         return ret;
28 }
29
30 #define K(pages) ((pages) << (PAGE_SHIFT - 10))
31
32 #define BDI_SHOW(name, expr)                                            \
33 static ssize_t name##_show(struct device *dev,                          \
34                            struct device_attribute *attr, char *page)   \
35 {                                                                       \
36         struct backing_dev_info *bdi = dev_get_drvdata(dev);            \
37                                                                         \
38         return snprintf(page, PAGE_SIZE-1, "%lld\n", (long long)expr);  \
39 }
40
41 BDI_SHOW(read_ahead_kb, K(bdi->ra_pages))
42
43 BDI_SHOW(reclaimable_kb, K(bdi_stat(bdi, BDI_RECLAIMABLE)))
44 BDI_SHOW(writeback_kb, K(bdi_stat(bdi, BDI_WRITEBACK)))
45
46 static inline unsigned long get_dirty(struct backing_dev_info *bdi, int i)
47 {
48         unsigned long thresh[3];
49
50         get_dirty_limits(&thresh[0], &thresh[1], &thresh[2], bdi);
51
52         return thresh[i];
53 }
54
55 BDI_SHOW(dirty_kb, K(get_dirty(bdi, 1)))
56 BDI_SHOW(bdi_dirty_kb, K(get_dirty(bdi, 2)))
57
58 static ssize_t min_ratio_store(struct device *dev,
59                 struct device_attribute *attr, const char *buf, size_t count)
60 {
61         struct backing_dev_info *bdi = dev_get_drvdata(dev);
62         char *end;
63         unsigned int ratio;
64         ssize_t ret = -EINVAL;
65
66         ratio = simple_strtoul(buf, &end, 10);
67         if (*buf && (end[0] == '\0' || (end[0] == '\n' && end[1] == '\0'))) {
68                 ret = bdi_set_min_ratio(bdi, ratio);
69                 if (!ret)
70                         ret = count;
71         }
72         return ret;
73 }
74 BDI_SHOW(min_ratio, bdi->min_ratio)
75
76 static ssize_t max_ratio_store(struct device *dev,
77                 struct device_attribute *attr, const char *buf, size_t count)
78 {
79         struct backing_dev_info *bdi = dev_get_drvdata(dev);
80         char *end;
81         unsigned int ratio;
82         ssize_t ret = -EINVAL;
83
84         ratio = simple_strtoul(buf, &end, 10);
85         if (*buf && (end[0] == '\0' || (end[0] == '\n' && end[1] == '\0'))) {
86                 ret = bdi_set_max_ratio(bdi, ratio);
87                 if (!ret)
88                         ret = count;
89         }
90         return ret;
91 }
92 BDI_SHOW(max_ratio, bdi->max_ratio)
93
94 #define __ATTR_RW(attr) __ATTR(attr, 0644, attr##_show, attr##_store)
95
96 static struct device_attribute bdi_dev_attrs[] = {
97         __ATTR_RW(read_ahead_kb),
98         __ATTR_RO(reclaimable_kb),
99         __ATTR_RO(writeback_kb),
100         __ATTR_RO(dirty_kb),
101         __ATTR_RO(bdi_dirty_kb),
102         __ATTR_RW(min_ratio),
103         __ATTR_RW(max_ratio),
104         __ATTR_NULL,
105 };
106
107 static __init int bdi_class_init(void)
108 {
109         bdi_class = class_create(THIS_MODULE, "bdi");
110         bdi_class->dev_attrs = bdi_dev_attrs;
111         return 0;
112 }
113
114 core_initcall(bdi_class_init);
115
116 int bdi_register(struct backing_dev_info *bdi, struct device *parent,
117                 const char *fmt, ...)
118 {
119         char *name;
120         va_list args;
121         int ret = 0;
122         struct device *dev;
123
124         va_start(args, fmt);
125         name = kvasprintf(GFP_KERNEL, fmt, args);
126         va_end(args);
127
128         if (!name)
129                 return -ENOMEM;
130
131         dev = device_create(bdi_class, parent, MKDEV(0, 0), name);
132         if (IS_ERR(dev)) {
133                 ret = PTR_ERR(dev);
134                 goto exit;
135         }
136
137         bdi->dev = dev;
138         dev_set_drvdata(bdi->dev, bdi);
139
140 exit:
141         kfree(name);
142         return ret;
143 }
144 EXPORT_SYMBOL(bdi_register);
145
146 int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev)
147 {
148         return bdi_register(bdi, NULL, "%u:%u", MAJOR(dev), MINOR(dev));
149 }
150 EXPORT_SYMBOL(bdi_register_dev);
151
152 void bdi_unregister(struct backing_dev_info *bdi)
153 {
154         if (bdi->dev) {
155                 device_unregister(bdi->dev);
156                 bdi->dev = NULL;
157         }
158 }
159 EXPORT_SYMBOL(bdi_unregister);
160
161 int bdi_init(struct backing_dev_info *bdi)
162 {
163         int i;
164         int err;
165
166         bdi->dev = NULL;
167
168         bdi->min_ratio = 0;
169         bdi->max_ratio = 100;
170         bdi->max_prop_frac = PROP_FRAC_BASE;
171
172         for (i = 0; i < NR_BDI_STAT_ITEMS; i++) {
173                 err = percpu_counter_init_irq(&bdi->bdi_stat[i], 0);
174                 if (err)
175                         goto err;
176         }
177
178         bdi->dirty_exceeded = 0;
179         err = prop_local_init_percpu(&bdi->completions);
180
181         if (err) {
182 err:
183                 while (i--)
184                         percpu_counter_destroy(&bdi->bdi_stat[i]);
185         }
186
187         return err;
188 }
189 EXPORT_SYMBOL(bdi_init);
190
191 void bdi_destroy(struct backing_dev_info *bdi)
192 {
193         int i;
194
195         bdi_unregister(bdi);
196
197         for (i = 0; i < NR_BDI_STAT_ITEMS; i++)
198                 percpu_counter_destroy(&bdi->bdi_stat[i]);
199
200         prop_local_destroy_percpu(&bdi->completions);
201 }
202 EXPORT_SYMBOL(bdi_destroy);
203
204 static wait_queue_head_t congestion_wqh[2] = {
205                 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
206                 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
207         };
208
209
210 void clear_bdi_congested(struct backing_dev_info *bdi, int rw)
211 {
212         enum bdi_state bit;
213         wait_queue_head_t *wqh = &congestion_wqh[rw];
214
215         bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
216         clear_bit(bit, &bdi->state);
217         smp_mb__after_clear_bit();
218         if (waitqueue_active(wqh))
219                 wake_up(wqh);
220 }
221 EXPORT_SYMBOL(clear_bdi_congested);
222
223 void set_bdi_congested(struct backing_dev_info *bdi, int rw)
224 {
225         enum bdi_state bit;
226
227         bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
228         set_bit(bit, &bdi->state);
229 }
230 EXPORT_SYMBOL(set_bdi_congested);
231
232 /**
233  * congestion_wait - wait for a backing_dev to become uncongested
234  * @rw: READ or WRITE
235  * @timeout: timeout in jiffies
236  *
237  * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
238  * write congestion.  If no backing_devs are congested then just wait for the
239  * next write to be completed.
240  */
241 long congestion_wait(int rw, long timeout)
242 {
243         long ret;
244         DEFINE_WAIT(wait);
245         wait_queue_head_t *wqh = &congestion_wqh[rw];
246
247         prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
248         ret = io_schedule_timeout(timeout);
249         finish_wait(wqh, &wait);
250         return ret;
251 }
252 EXPORT_SYMBOL(congestion_wait);
253