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 #ifdef CONFIG_DEBUG_FS
14 #include <linux/debugfs.h>
15 #include <linux/seq_file.h>
17 static struct dentry
*bdi_debug_root
;
19 static void bdi_debug_init(void)
21 bdi_debug_root
= debugfs_create_dir("bdi", NULL
);
24 static int bdi_debug_stats_show(struct seq_file
*m
, void *v
)
26 struct backing_dev_info
*bdi
= m
->private;
27 unsigned long background_thresh
;
28 unsigned long dirty_thresh
;
29 unsigned long bdi_thresh
;
31 get_dirty_limits(&background_thresh
, &dirty_thresh
, &bdi_thresh
, bdi
);
33 #define K(x) ((x) << (PAGE_SHIFT - 10))
35 "BdiWriteback: %8lu kB\n"
36 "BdiReclaimable: %8lu kB\n"
37 "BdiDirtyThresh: %8lu kB\n"
38 "DirtyThresh: %8lu kB\n"
39 "BackgroundThresh: %8lu kB\n",
40 (unsigned long) K(bdi_stat(bdi
, BDI_WRITEBACK
)),
41 (unsigned long) K(bdi_stat(bdi
, BDI_RECLAIMABLE
)),
44 K(background_thresh
));
50 static int bdi_debug_stats_open(struct inode
*inode
, struct file
*file
)
52 return single_open(file
, bdi_debug_stats_show
, inode
->i_private
);
55 static const struct file_operations bdi_debug_stats_fops
= {
56 .open
= bdi_debug_stats_open
,
59 .release
= single_release
,
62 static void bdi_debug_register(struct backing_dev_info
*bdi
, const char *name
)
64 bdi
->debug_dir
= debugfs_create_dir(name
, bdi_debug_root
);
65 bdi
->debug_stats
= debugfs_create_file("stats", 0444, bdi
->debug_dir
,
66 bdi
, &bdi_debug_stats_fops
);
69 static void bdi_debug_unregister(struct backing_dev_info
*bdi
)
71 debugfs_remove(bdi
->debug_stats
);
72 debugfs_remove(bdi
->debug_dir
);
75 static inline void bdi_debug_init(void)
78 static inline void bdi_debug_register(struct backing_dev_info
*bdi
,
82 static inline void bdi_debug_unregister(struct backing_dev_info
*bdi
)
87 static ssize_t
read_ahead_kb_store(struct device
*dev
,
88 struct device_attribute
*attr
,
89 const char *buf
, size_t count
)
91 struct backing_dev_info
*bdi
= dev_get_drvdata(dev
);
93 unsigned long read_ahead_kb
;
94 ssize_t ret
= -EINVAL
;
96 read_ahead_kb
= simple_strtoul(buf
, &end
, 10);
97 if (*buf
&& (end
[0] == '\0' || (end
[0] == '\n' && end
[1] == '\0'))) {
98 bdi
->ra_pages
= read_ahead_kb
>> (PAGE_SHIFT
- 10);
104 #define K(pages) ((pages) << (PAGE_SHIFT - 10))
106 #define BDI_SHOW(name, expr) \
107 static ssize_t name##_show(struct device *dev, \
108 struct device_attribute *attr, char *page) \
110 struct backing_dev_info *bdi = dev_get_drvdata(dev); \
112 return snprintf(page, PAGE_SIZE-1, "%lld\n", (long long)expr); \
115 BDI_SHOW(read_ahead_kb
, K(bdi
->ra_pages
))
117 static ssize_t
min_ratio_store(struct device
*dev
,
118 struct device_attribute
*attr
, const char *buf
, size_t count
)
120 struct backing_dev_info
*bdi
= dev_get_drvdata(dev
);
123 ssize_t ret
= -EINVAL
;
125 ratio
= simple_strtoul(buf
, &end
, 10);
126 if (*buf
&& (end
[0] == '\0' || (end
[0] == '\n' && end
[1] == '\0'))) {
127 ret
= bdi_set_min_ratio(bdi
, ratio
);
133 BDI_SHOW(min_ratio
, bdi
->min_ratio
)
135 static ssize_t
max_ratio_store(struct device
*dev
,
136 struct device_attribute
*attr
, const char *buf
, size_t count
)
138 struct backing_dev_info
*bdi
= dev_get_drvdata(dev
);
141 ssize_t ret
= -EINVAL
;
143 ratio
= simple_strtoul(buf
, &end
, 10);
144 if (*buf
&& (end
[0] == '\0' || (end
[0] == '\n' && end
[1] == '\0'))) {
145 ret
= bdi_set_max_ratio(bdi
, ratio
);
151 BDI_SHOW(max_ratio
, bdi
->max_ratio
)
153 #define __ATTR_RW(attr) __ATTR(attr, 0644, attr##_show, attr##_store)
155 static struct device_attribute bdi_dev_attrs
[] = {
156 __ATTR_RW(read_ahead_kb
),
157 __ATTR_RW(min_ratio
),
158 __ATTR_RW(max_ratio
),
162 static __init
int bdi_class_init(void)
164 bdi_class
= class_create(THIS_MODULE
, "bdi");
165 bdi_class
->dev_attrs
= bdi_dev_attrs
;
170 postcore_initcall(bdi_class_init
);
172 int bdi_register(struct backing_dev_info
*bdi
, struct device
*parent
,
173 const char *fmt
, ...)
179 if (bdi
->dev
) /* The driver needs to use separate queues per device */
183 dev
= device_create_vargs(bdi_class
, parent
, MKDEV(0, 0), bdi
, fmt
, args
);
191 bdi_debug_register(bdi
, dev_name(dev
));
196 EXPORT_SYMBOL(bdi_register
);
198 int bdi_register_dev(struct backing_dev_info
*bdi
, dev_t dev
)
200 return bdi_register(bdi
, NULL
, "%u:%u", MAJOR(dev
), MINOR(dev
));
202 EXPORT_SYMBOL(bdi_register_dev
);
204 void bdi_unregister(struct backing_dev_info
*bdi
)
207 bdi_debug_unregister(bdi
);
208 device_unregister(bdi
->dev
);
212 EXPORT_SYMBOL(bdi_unregister
);
214 int bdi_init(struct backing_dev_info
*bdi
)
222 bdi
->max_ratio
= 100;
223 bdi
->max_prop_frac
= PROP_FRAC_BASE
;
225 for (i
= 0; i
< NR_BDI_STAT_ITEMS
; i
++) {
226 err
= percpu_counter_init_irq(&bdi
->bdi_stat
[i
], 0);
231 bdi
->dirty_exceeded
= 0;
232 err
= prop_local_init_percpu(&bdi
->completions
);
237 percpu_counter_destroy(&bdi
->bdi_stat
[i
]);
242 EXPORT_SYMBOL(bdi_init
);
244 void bdi_destroy(struct backing_dev_info
*bdi
)
250 for (i
= 0; i
< NR_BDI_STAT_ITEMS
; i
++)
251 percpu_counter_destroy(&bdi
->bdi_stat
[i
]);
253 prop_local_destroy_percpu(&bdi
->completions
);
255 EXPORT_SYMBOL(bdi_destroy
);
257 static wait_queue_head_t congestion_wqh
[2] = {
258 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh
[0]),
259 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh
[1])
263 void clear_bdi_congested(struct backing_dev_info
*bdi
, int rw
)
266 wait_queue_head_t
*wqh
= &congestion_wqh
[rw
];
268 bit
= (rw
== WRITE
) ? BDI_write_congested
: BDI_read_congested
;
269 clear_bit(bit
, &bdi
->state
);
270 smp_mb__after_clear_bit();
271 if (waitqueue_active(wqh
))
274 EXPORT_SYMBOL(clear_bdi_congested
);
276 void set_bdi_congested(struct backing_dev_info
*bdi
, int rw
)
280 bit
= (rw
== WRITE
) ? BDI_write_congested
: BDI_read_congested
;
281 set_bit(bit
, &bdi
->state
);
283 EXPORT_SYMBOL(set_bdi_congested
);
286 * congestion_wait - wait for a backing_dev to become uncongested
288 * @timeout: timeout in jiffies
290 * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
291 * write congestion. If no backing_devs are congested then just wait for the
292 * next write to be completed.
294 long congestion_wait(int rw
, long timeout
)
298 wait_queue_head_t
*wqh
= &congestion_wqh
[rw
];
300 prepare_to_wait(wqh
, &wait
, TASK_UNINTERRUPTIBLE
);
301 ret
= io_schedule_timeout(timeout
);
302 finish_wait(wqh
, &wait
);
305 EXPORT_SYMBOL(congestion_wait
);