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 #define __ATTR_RW(attr) __ATTR(attr, 0644, attr##_show, attr##_store)
60 static struct device_attribute bdi_dev_attrs
[] = {
61 __ATTR_RW(read_ahead_kb
),
62 __ATTR_RO(reclaimable_kb
),
63 __ATTR_RO(writeback_kb
),
65 __ATTR_RO(bdi_dirty_kb
),
69 static __init
int bdi_class_init(void)
71 bdi_class
= class_create(THIS_MODULE
, "bdi");
72 bdi_class
->dev_attrs
= bdi_dev_attrs
;
76 core_initcall(bdi_class_init
);
78 int bdi_register(struct backing_dev_info
*bdi
, struct device
*parent
,
87 name
= kvasprintf(GFP_KERNEL
, fmt
, args
);
93 dev
= device_create(bdi_class
, parent
, MKDEV(0, 0), name
);
100 dev_set_drvdata(bdi
->dev
, bdi
);
106 EXPORT_SYMBOL(bdi_register
);
108 int bdi_register_dev(struct backing_dev_info
*bdi
, dev_t dev
)
110 return bdi_register(bdi
, NULL
, "%u:%u", MAJOR(dev
), MINOR(dev
));
112 EXPORT_SYMBOL(bdi_register_dev
);
114 void bdi_unregister(struct backing_dev_info
*bdi
)
117 device_unregister(bdi
->dev
);
121 EXPORT_SYMBOL(bdi_unregister
);
123 int bdi_init(struct backing_dev_info
*bdi
)
130 for (i
= 0; i
< NR_BDI_STAT_ITEMS
; i
++) {
131 err
= percpu_counter_init_irq(&bdi
->bdi_stat
[i
], 0);
136 bdi
->dirty_exceeded
= 0;
137 err
= prop_local_init_percpu(&bdi
->completions
);
142 percpu_counter_destroy(&bdi
->bdi_stat
[i
]);
147 EXPORT_SYMBOL(bdi_init
);
149 void bdi_destroy(struct backing_dev_info
*bdi
)
155 for (i
= 0; i
< NR_BDI_STAT_ITEMS
; i
++)
156 percpu_counter_destroy(&bdi
->bdi_stat
[i
]);
158 prop_local_destroy_percpu(&bdi
->completions
);
160 EXPORT_SYMBOL(bdi_destroy
);
162 static wait_queue_head_t congestion_wqh
[2] = {
163 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh
[0]),
164 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh
[1])
168 void clear_bdi_congested(struct backing_dev_info
*bdi
, int rw
)
171 wait_queue_head_t
*wqh
= &congestion_wqh
[rw
];
173 bit
= (rw
== WRITE
) ? BDI_write_congested
: BDI_read_congested
;
174 clear_bit(bit
, &bdi
->state
);
175 smp_mb__after_clear_bit();
176 if (waitqueue_active(wqh
))
179 EXPORT_SYMBOL(clear_bdi_congested
);
181 void set_bdi_congested(struct backing_dev_info
*bdi
, int rw
)
185 bit
= (rw
== WRITE
) ? BDI_write_congested
: BDI_read_congested
;
186 set_bit(bit
, &bdi
->state
);
188 EXPORT_SYMBOL(set_bdi_congested
);
191 * congestion_wait - wait for a backing_dev to become uncongested
193 * @timeout: timeout in jiffies
195 * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
196 * write congestion. If no backing_devs are congested then just wait for the
197 * next write to be completed.
199 long congestion_wait(int rw
, long timeout
)
203 wait_queue_head_t
*wqh
= &congestion_wqh
[rw
];
205 prepare_to_wait(wqh
, &wait
, TASK_UNINTERRUPTIBLE
);
206 ret
= io_schedule_timeout(timeout
);
207 finish_wait(wqh
, &wait
);
210 EXPORT_SYMBOL(congestion_wait
);