2 #include <linux/wait.h>
3 #include <linux/backing-dev.h>
5 #include <linux/sched.h>
6 #include <linux/module.h>
7 #include <linux/writeback.h>
8 #include <linux/device.h>
11 static struct class *bdi_class;
13 static ssize_t read_ahead_kb_store(struct device *dev,
14 struct device_attribute *attr,
15 const char *buf, size_t count)
17 struct backing_dev_info *bdi = dev_get_drvdata(dev);
19 unsigned long read_ahead_kb;
20 ssize_t ret = -EINVAL;
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);
30 #define K(pages) ((pages) << (PAGE_SHIFT - 10))
32 #define BDI_SHOW(name, expr) \
33 static ssize_t name##_show(struct device *dev, \
34 struct device_attribute *attr, char *page) \
36 struct backing_dev_info *bdi = dev_get_drvdata(dev); \
38 return snprintf(page, PAGE_SIZE-1, "%lld\n", (long long)expr); \
41 BDI_SHOW(read_ahead_kb, K(bdi->ra_pages))
43 BDI_SHOW(reclaimable_kb, K(bdi_stat(bdi, BDI_RECLAIMABLE)))
44 BDI_SHOW(writeback_kb, K(bdi_stat(bdi, BDI_WRITEBACK)))
46 static inline unsigned long get_dirty(struct backing_dev_info *bdi, int i)
48 unsigned long thresh[3];
50 get_dirty_limits(&thresh[0], &thresh[1], &thresh[2], bdi);
55 BDI_SHOW(dirty_kb, K(get_dirty(bdi, 1)))
56 BDI_SHOW(bdi_dirty_kb, K(get_dirty(bdi, 2)))
58 static ssize_t min_ratio_store(struct device *dev,
59 struct device_attribute *attr, const char *buf, size_t count)
61 struct backing_dev_info *bdi = dev_get_drvdata(dev);
64 ssize_t ret = -EINVAL;
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);
74 BDI_SHOW(min_ratio, bdi->min_ratio)
76 static ssize_t max_ratio_store(struct device *dev,
77 struct device_attribute *attr, const char *buf, size_t count)
79 struct backing_dev_info *bdi = dev_get_drvdata(dev);
82 ssize_t ret = -EINVAL;
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);
92 BDI_SHOW(max_ratio, bdi->max_ratio)
94 #define __ATTR_RW(attr) __ATTR(attr, 0644, attr##_show, attr##_store)
96 static struct device_attribute bdi_dev_attrs[] = {
97 __ATTR_RW(read_ahead_kb),
98 __ATTR_RO(reclaimable_kb),
99 __ATTR_RO(writeback_kb),
101 __ATTR_RO(bdi_dirty_kb),
102 __ATTR_RW(min_ratio),
103 __ATTR_RW(max_ratio),
107 static __init int bdi_class_init(void)
109 bdi_class = class_create(THIS_MODULE, "bdi");
110 bdi_class->dev_attrs = bdi_dev_attrs;
114 core_initcall(bdi_class_init);
116 int bdi_register(struct backing_dev_info *bdi, struct device *parent,
117 const char *fmt, ...)
125 name = kvasprintf(GFP_KERNEL, fmt, args);
131 dev = device_create(bdi_class, parent, MKDEV(0, 0), name);
138 dev_set_drvdata(bdi->dev, bdi);
144 EXPORT_SYMBOL(bdi_register);
146 int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev)
148 return bdi_register(bdi, NULL, "%u:%u", MAJOR(dev), MINOR(dev));
150 EXPORT_SYMBOL(bdi_register_dev);
152 void bdi_unregister(struct backing_dev_info *bdi)
155 device_unregister(bdi->dev);
159 EXPORT_SYMBOL(bdi_unregister);
161 int bdi_init(struct backing_dev_info *bdi)
169 bdi->max_ratio = 100;
170 bdi->max_prop_frac = PROP_FRAC_BASE;
172 for (i = 0; i < NR_BDI_STAT_ITEMS; i++) {
173 err = percpu_counter_init_irq(&bdi->bdi_stat[i], 0);
178 bdi->dirty_exceeded = 0;
179 err = prop_local_init_percpu(&bdi->completions);
184 percpu_counter_destroy(&bdi->bdi_stat[i]);
189 EXPORT_SYMBOL(bdi_init);
191 void bdi_destroy(struct backing_dev_info *bdi)
197 for (i = 0; i < NR_BDI_STAT_ITEMS; i++)
198 percpu_counter_destroy(&bdi->bdi_stat[i]);
200 prop_local_destroy_percpu(&bdi->completions);
202 EXPORT_SYMBOL(bdi_destroy);
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])
210 void clear_bdi_congested(struct backing_dev_info *bdi, int rw)
213 wait_queue_head_t *wqh = &congestion_wqh[rw];
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))
221 EXPORT_SYMBOL(clear_bdi_congested);
223 void set_bdi_congested(struct backing_dev_info *bdi, int rw)
227 bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
228 set_bit(bit, &bdi->state);
230 EXPORT_SYMBOL(set_bdi_congested);
233 * congestion_wait - wait for a backing_dev to become uncongested
235 * @timeout: timeout in jiffies
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.
241 long congestion_wait(int rw, long timeout)
245 wait_queue_head_t *wqh = &congestion_wqh[rw];
247 prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
248 ret = io_schedule_timeout(timeout);
249 finish_wait(wqh, &wait);
252 EXPORT_SYMBOL(congestion_wait);