blob: fc41d83be22bb02bab61f09b4b356dbba4697a05 [file] [log] [blame]
Jens Axboe8324aa92008-01-29 14:51:59 +01001/*
2 * Functions related to sysfs handling
3 */
4#include <linux/kernel.h>
5#include <linux/module.h>
6#include <linux/bio.h>
7#include <linux/blkdev.h>
8#include <linux/blktrace_api.h>
9
10#include "blk.h"
11
12struct queue_sysfs_entry {
13 struct attribute attr;
14 ssize_t (*show)(struct request_queue *, char *);
15 ssize_t (*store)(struct request_queue *, const char *, size_t);
16};
17
18static ssize_t
19queue_var_show(unsigned int var, char *page)
20{
21 return sprintf(page, "%d\n", var);
22}
23
24static ssize_t
25queue_var_store(unsigned long *var, const char *page, size_t count)
26{
27 char *p = (char *) page;
28
29 *var = simple_strtoul(p, &p, 10);
30 return count;
31}
32
33static ssize_t queue_requests_show(struct request_queue *q, char *page)
34{
35 return queue_var_show(q->nr_requests, (page));
36}
37
38static ssize_t
39queue_requests_store(struct request_queue *q, const char *page, size_t count)
40{
41 struct request_list *rl = &q->rq;
42 unsigned long nr;
43 int ret = queue_var_store(&nr, page, count);
44 if (nr < BLKDEV_MIN_RQ)
45 nr = BLKDEV_MIN_RQ;
46
47 spin_lock_irq(q->queue_lock);
48 q->nr_requests = nr;
49 blk_queue_congestion_threshold(q);
50
51 if (rl->count[READ] >= queue_congestion_on_threshold(q))
52 blk_set_queue_congested(q, READ);
53 else if (rl->count[READ] < queue_congestion_off_threshold(q))
54 blk_clear_queue_congested(q, READ);
55
56 if (rl->count[WRITE] >= queue_congestion_on_threshold(q))
57 blk_set_queue_congested(q, WRITE);
58 else if (rl->count[WRITE] < queue_congestion_off_threshold(q))
59 blk_clear_queue_congested(q, WRITE);
60
61 if (rl->count[READ] >= q->nr_requests) {
62 blk_set_queue_full(q, READ);
63 } else if (rl->count[READ]+1 <= q->nr_requests) {
64 blk_clear_queue_full(q, READ);
65 wake_up(&rl->wait[READ]);
66 }
67
68 if (rl->count[WRITE] >= q->nr_requests) {
69 blk_set_queue_full(q, WRITE);
70 } else if (rl->count[WRITE]+1 <= q->nr_requests) {
71 blk_clear_queue_full(q, WRITE);
72 wake_up(&rl->wait[WRITE]);
73 }
74 spin_unlock_irq(q->queue_lock);
75 return ret;
76}
77
78static ssize_t queue_ra_show(struct request_queue *q, char *page)
79{
80 int ra_kb = q->backing_dev_info.ra_pages << (PAGE_CACHE_SHIFT - 10);
81
82 return queue_var_show(ra_kb, (page));
83}
84
85static ssize_t
86queue_ra_store(struct request_queue *q, const char *page, size_t count)
87{
88 unsigned long ra_kb;
89 ssize_t ret = queue_var_store(&ra_kb, page, count);
90
91 spin_lock_irq(q->queue_lock);
92 q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
93 spin_unlock_irq(q->queue_lock);
94
95 return ret;
96}
97
98static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
99{
100 int max_sectors_kb = q->max_sectors >> 1;
101
102 return queue_var_show(max_sectors_kb, (page));
103}
104
Martin K. Petersene68b9032008-01-29 19:14:08 +0100105static ssize_t queue_hw_sector_size_show(struct request_queue *q, char *page)
106{
107 return queue_var_show(q->hardsect_size, page);
108}
109
Jens Axboe8324aa92008-01-29 14:51:59 +0100110static ssize_t
111queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
112{
113 unsigned long max_sectors_kb,
114 max_hw_sectors_kb = q->max_hw_sectors >> 1,
115 page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
116 ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
117
118 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
119 return -EINVAL;
120 /*
121 * Take the queue lock to update the readahead and max_sectors
122 * values synchronously:
123 */
124 spin_lock_irq(q->queue_lock);
125 q->max_sectors = max_sectors_kb << 1;
126 spin_unlock_irq(q->queue_lock);
127
128 return ret;
129}
130
131static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
132{
133 int max_hw_sectors_kb = q->max_hw_sectors >> 1;
134
135 return queue_var_show(max_hw_sectors_kb, (page));
136}
137
138
139static struct queue_sysfs_entry queue_requests_entry = {
140 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
141 .show = queue_requests_show,
142 .store = queue_requests_store,
143};
144
145static struct queue_sysfs_entry queue_ra_entry = {
146 .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
147 .show = queue_ra_show,
148 .store = queue_ra_store,
149};
150
151static struct queue_sysfs_entry queue_max_sectors_entry = {
152 .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
153 .show = queue_max_sectors_show,
154 .store = queue_max_sectors_store,
155};
156
157static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
158 .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
159 .show = queue_max_hw_sectors_show,
160};
161
162static struct queue_sysfs_entry queue_iosched_entry = {
163 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
164 .show = elv_iosched_show,
165 .store = elv_iosched_store,
166};
167
Martin K. Petersene68b9032008-01-29 19:14:08 +0100168static struct queue_sysfs_entry queue_hw_sector_size_entry = {
169 .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
170 .show = queue_hw_sector_size_show,
171};
172
Jens Axboe8324aa92008-01-29 14:51:59 +0100173static struct attribute *default_attrs[] = {
174 &queue_requests_entry.attr,
175 &queue_ra_entry.attr,
176 &queue_max_hw_sectors_entry.attr,
177 &queue_max_sectors_entry.attr,
178 &queue_iosched_entry.attr,
Martin K. Petersene68b9032008-01-29 19:14:08 +0100179 &queue_hw_sector_size_entry.attr,
Jens Axboe8324aa92008-01-29 14:51:59 +0100180 NULL,
181};
182
183#define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
184
185static ssize_t
186queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
187{
188 struct queue_sysfs_entry *entry = to_queue(attr);
189 struct request_queue *q =
190 container_of(kobj, struct request_queue, kobj);
191 ssize_t res;
192
193 if (!entry->show)
194 return -EIO;
195 mutex_lock(&q->sysfs_lock);
196 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
197 mutex_unlock(&q->sysfs_lock);
198 return -ENOENT;
199 }
200 res = entry->show(q, page);
201 mutex_unlock(&q->sysfs_lock);
202 return res;
203}
204
205static ssize_t
206queue_attr_store(struct kobject *kobj, struct attribute *attr,
207 const char *page, size_t length)
208{
209 struct queue_sysfs_entry *entry = to_queue(attr);
Jens Axboe6728cb02008-01-31 13:03:55 +0100210 struct request_queue *q;
Jens Axboe8324aa92008-01-29 14:51:59 +0100211 ssize_t res;
212
213 if (!entry->store)
214 return -EIO;
Jens Axboe6728cb02008-01-31 13:03:55 +0100215
216 q = container_of(kobj, struct request_queue, kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100217 mutex_lock(&q->sysfs_lock);
218 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
219 mutex_unlock(&q->sysfs_lock);
220 return -ENOENT;
221 }
222 res = entry->store(q, page, length);
223 mutex_unlock(&q->sysfs_lock);
224 return res;
225}
226
227/**
228 * blk_cleanup_queue: - release a &struct request_queue when it is no longer needed
229 * @kobj: the kobj belonging of the request queue to be released
230 *
231 * Description:
232 * blk_cleanup_queue is the pair to blk_init_queue() or
233 * blk_queue_make_request(). It should be called when a request queue is
234 * being released; typically when a block device is being de-registered.
235 * Currently, its primary task it to free all the &struct request
236 * structures that were allocated to the queue and the queue itself.
237 *
238 * Caveat:
239 * Hopefully the low level driver will have finished any
240 * outstanding requests first...
241 **/
242static void blk_release_queue(struct kobject *kobj)
243{
244 struct request_queue *q =
245 container_of(kobj, struct request_queue, kobj);
246 struct request_list *rl = &q->rq;
247
248 blk_sync_queue(q);
249
250 if (rl->rq_pool)
251 mempool_destroy(rl->rq_pool);
252
253 if (q->queue_tags)
254 __blk_queue_free_tags(q);
255
256 blk_trace_shutdown(q);
257
258 bdi_destroy(&q->backing_dev_info);
259 kmem_cache_free(blk_requestq_cachep, q);
260}
261
262static struct sysfs_ops queue_sysfs_ops = {
263 .show = queue_attr_show,
264 .store = queue_attr_store,
265};
266
267struct kobj_type blk_queue_ktype = {
268 .sysfs_ops = &queue_sysfs_ops,
269 .default_attrs = default_attrs,
270 .release = blk_release_queue,
271};
272
273int blk_register_queue(struct gendisk *disk)
274{
275 int ret;
276
277 struct request_queue *q = disk->queue;
278
Akinobu Mitafb199742008-04-21 09:51:06 +0200279 if (WARN_ON(!q))
Jens Axboe8324aa92008-01-29 14:51:59 +0100280 return -ENXIO;
281
Akinobu Mitafb199742008-04-21 09:51:06 +0200282 if (!q->request_fn)
283 return 0;
284
Jens Axboe8324aa92008-01-29 14:51:59 +0100285 ret = kobject_add(&q->kobj, kobject_get(&disk->dev.kobj),
286 "%s", "queue");
287 if (ret < 0)
288 return ret;
289
290 kobject_uevent(&q->kobj, KOBJ_ADD);
291
292 ret = elv_register_queue(q);
293 if (ret) {
294 kobject_uevent(&q->kobj, KOBJ_REMOVE);
295 kobject_del(&q->kobj);
296 return ret;
297 }
298
299 return 0;
300}
301
302void blk_unregister_queue(struct gendisk *disk)
303{
304 struct request_queue *q = disk->queue;
305
Akinobu Mitafb199742008-04-21 09:51:06 +0200306 if (WARN_ON(!q))
307 return;
308
309 if (q->request_fn) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100310 elv_unregister_queue(q);
311
312 kobject_uevent(&q->kobj, KOBJ_REMOVE);
313 kobject_del(&q->kobj);
314 kobject_put(&disk->dev.kobj);
315 }
316}