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 #define __ATTR_RW(attr) __ATTR(attr, 0644, attr##_show, attr##_store)
78 static struct device_attribute bdi_dev_attrs
[] = {
79 __ATTR_RW(read_ahead_kb
),
80 __ATTR_RO(reclaimable_kb
),
81 __ATTR_RO(writeback_kb
),
83 __ATTR_RO(bdi_dirty_kb
),
88 static __init
int bdi_class_init(void)
90 bdi_class
= class_create(THIS_MODULE
, "bdi");
91 bdi_class
->dev_attrs
= bdi_dev_attrs
;
95 core_initcall(bdi_class_init
);
97 int bdi_register(struct backing_dev_info
*bdi
, struct device
*parent
,
106 name
= kvasprintf(GFP_KERNEL
, fmt
, args
);
112 dev
= device_create(bdi_class
, parent
, MKDEV(0, 0), name
);
119 dev_set_drvdata(bdi
->dev
, bdi
);
125 EXPORT_SYMBOL(bdi_register
);
127 int bdi_register_dev(struct backing_dev_info
*bdi
, dev_t dev
)
129 return bdi_register(bdi
, NULL
, "%u:%u", MAJOR(dev
), MINOR(dev
));
131 EXPORT_SYMBOL(bdi_register_dev
);
133 void bdi_unregister(struct backing_dev_info
*bdi
)
136 device_unregister(bdi
->dev
);
140 EXPORT_SYMBOL(bdi_unregister
);
142 int bdi_init(struct backing_dev_info
*bdi
)
151 for (i
= 0; i
< NR_BDI_STAT_ITEMS
; i
++) {
152 err
= percpu_counter_init_irq(&bdi
->bdi_stat
[i
], 0);
157 bdi
->dirty_exceeded
= 0;
158 err
= prop_local_init_percpu(&bdi
->completions
);
163 percpu_counter_destroy(&bdi
->bdi_stat
[i
]);
168 EXPORT_SYMBOL(bdi_init
);
170 void bdi_destroy(struct backing_dev_info
*bdi
)
176 for (i
= 0; i
< NR_BDI_STAT_ITEMS
; i
++)
177 percpu_counter_destroy(&bdi
->bdi_stat
[i
]);
179 prop_local_destroy_percpu(&bdi
->completions
);
181 EXPORT_SYMBOL(bdi_destroy
);
183 static wait_queue_head_t congestion_wqh
[2] = {
184 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh
[0]),
185 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh
[1])
189 void clear_bdi_congested(struct backing_dev_info
*bdi
, int rw
)
192 wait_queue_head_t
*wqh
= &congestion_wqh
[rw
];
194 bit
= (rw
== WRITE
) ? BDI_write_congested
: BDI_read_congested
;
195 clear_bit(bit
, &bdi
->state
);
196 smp_mb__after_clear_bit();
197 if (waitqueue_active(wqh
))
200 EXPORT_SYMBOL(clear_bdi_congested
);
202 void set_bdi_congested(struct backing_dev_info
*bdi
, int rw
)
206 bit
= (rw
== WRITE
) ? BDI_write_congested
: BDI_read_congested
;
207 set_bit(bit
, &bdi
->state
);
209 EXPORT_SYMBOL(set_bdi_congested
);
212 * congestion_wait - wait for a backing_dev to become uncongested
214 * @timeout: timeout in jiffies
216 * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
217 * write congestion. If no backing_devs are congested then just wait for the
218 * next write to be completed.
220 long congestion_wait(int rw
, long timeout
)
224 wait_queue_head_t
*wqh
= &congestion_wqh
[rw
];
226 prepare_to_wait(wqh
, &wait
, TASK_UNINTERRUPTIBLE
);
227 ret
= io_schedule_timeout(timeout
);
228 finish_wait(wqh
, &wait
);
231 EXPORT_SYMBOL(congestion_wait
);