blob: 3ff9bba3379a84891ddbc97450fcdbddf6e42a1a [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
Jens Axboe1faa16d2009-04-06 14:48:01 +020051 if (rl->count[BLK_RW_SYNC] >= queue_congestion_on_threshold(q))
52 blk_set_queue_congested(q, BLK_RW_SYNC);
53 else if (rl->count[BLK_RW_SYNC] < queue_congestion_off_threshold(q))
54 blk_clear_queue_congested(q, BLK_RW_SYNC);
Jens Axboe8324aa92008-01-29 14:51:59 +010055
Jens Axboe1faa16d2009-04-06 14:48:01 +020056 if (rl->count[BLK_RW_ASYNC] >= queue_congestion_on_threshold(q))
57 blk_set_queue_congested(q, BLK_RW_ASYNC);
58 else if (rl->count[BLK_RW_ASYNC] < queue_congestion_off_threshold(q))
59 blk_clear_queue_congested(q, BLK_RW_ASYNC);
Jens Axboe8324aa92008-01-29 14:51:59 +010060
Jens Axboe1faa16d2009-04-06 14:48:01 +020061 if (rl->count[BLK_RW_SYNC] >= q->nr_requests) {
62 blk_set_queue_full(q, BLK_RW_SYNC);
63 } else if (rl->count[BLK_RW_SYNC]+1 <= q->nr_requests) {
64 blk_clear_queue_full(q, BLK_RW_SYNC);
65 wake_up(&rl->wait[BLK_RW_SYNC]);
Jens Axboe8324aa92008-01-29 14:51:59 +010066 }
67
Jens Axboe1faa16d2009-04-06 14:48:01 +020068 if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) {
69 blk_set_queue_full(q, BLK_RW_ASYNC);
70 } else if (rl->count[BLK_RW_ASYNC]+1 <= q->nr_requests) {
71 blk_clear_queue_full(q, BLK_RW_ASYNC);
72 wake_up(&rl->wait[BLK_RW_ASYNC]);
Jens Axboe8324aa92008-01-29 14:51:59 +010073 }
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
Jens Axboe8324aa92008-01-29 14:51:59 +010091 q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
Jens Axboe8324aa92008-01-29 14:51:59 +010092
93 return ret;
94}
95
96static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
97{
98 int max_sectors_kb = q->max_sectors >> 1;
99
100 return queue_var_show(max_sectors_kb, (page));
101}
102
Martin K. Petersene68b9032008-01-29 19:14:08 +0100103static ssize_t queue_hw_sector_size_show(struct request_queue *q, char *page)
104{
105 return queue_var_show(q->hardsect_size, page);
106}
107
Jens Axboe8324aa92008-01-29 14:51:59 +0100108static ssize_t
109queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
110{
111 unsigned long max_sectors_kb,
112 max_hw_sectors_kb = q->max_hw_sectors >> 1,
113 page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
114 ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
115
116 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
117 return -EINVAL;
Wu Fengguang7c239512008-11-25 09:08:39 +0100118
Jens Axboe8324aa92008-01-29 14:51:59 +0100119 spin_lock_irq(q->queue_lock);
120 q->max_sectors = max_sectors_kb << 1;
121 spin_unlock_irq(q->queue_lock);
122
123 return ret;
124}
125
126static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
127{
128 int max_hw_sectors_kb = q->max_hw_sectors >> 1;
129
130 return queue_var_show(max_hw_sectors_kb, (page));
131}
132
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100133static ssize_t queue_nonrot_show(struct request_queue *q, char *page)
134{
135 return queue_var_show(!blk_queue_nonrot(q), page);
136}
137
138static ssize_t queue_nonrot_store(struct request_queue *q, const char *page,
139 size_t count)
140{
141 unsigned long nm;
142 ssize_t ret = queue_var_store(&nm, page, count);
143
144 spin_lock_irq(q->queue_lock);
145 if (nm)
146 queue_flag_clear(QUEUE_FLAG_NONROT, q);
147 else
148 queue_flag_set(QUEUE_FLAG_NONROT, q);
149 spin_unlock_irq(q->queue_lock);
150
151 return ret;
152}
153
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200154static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
155{
156 return queue_var_show(blk_queue_nomerges(q), page);
157}
158
159static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
160 size_t count)
161{
162 unsigned long nm;
163 ssize_t ret = queue_var_store(&nm, page, count);
164
Jens Axboebf0f9702008-05-07 09:09:39 +0200165 spin_lock_irq(q->queue_lock);
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200166 if (nm)
Jens Axboebf0f9702008-05-07 09:09:39 +0200167 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200168 else
Jens Axboebf0f9702008-05-07 09:09:39 +0200169 queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
Jens Axboebf0f9702008-05-07 09:09:39 +0200170 spin_unlock_irq(q->queue_lock);
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100171
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200172 return ret;
173}
174
Jens Axboec7c22e42008-09-13 20:26:01 +0200175static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
176{
177 unsigned int set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
178
179 return queue_var_show(set != 0, page);
180}
181
182static ssize_t
183queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
184{
185 ssize_t ret = -EINVAL;
186#if defined(CONFIG_USE_GENERIC_SMP_HELPERS)
187 unsigned long val;
188
189 ret = queue_var_store(&val, page, count);
190 spin_lock_irq(q->queue_lock);
191 if (val)
192 queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
193 else
194 queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
195 spin_unlock_irq(q->queue_lock);
196#endif
197 return ret;
198}
Jens Axboe8324aa92008-01-29 14:51:59 +0100199
Jens Axboebc58ba92009-01-23 10:54:44 +0100200static ssize_t queue_iostats_show(struct request_queue *q, char *page)
201{
202 return queue_var_show(blk_queue_io_stat(q), page);
203}
204
205static ssize_t queue_iostats_store(struct request_queue *q, const char *page,
206 size_t count)
207{
208 unsigned long stats;
209 ssize_t ret = queue_var_store(&stats, page, count);
210
211 spin_lock_irq(q->queue_lock);
212 if (stats)
213 queue_flag_set(QUEUE_FLAG_IO_STAT, q);
214 else
215 queue_flag_clear(QUEUE_FLAG_IO_STAT, q);
216 spin_unlock_irq(q->queue_lock);
217
218 return ret;
219}
220
Jens Axboe8324aa92008-01-29 14:51:59 +0100221static struct queue_sysfs_entry queue_requests_entry = {
222 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
223 .show = queue_requests_show,
224 .store = queue_requests_store,
225};
226
227static struct queue_sysfs_entry queue_ra_entry = {
228 .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
229 .show = queue_ra_show,
230 .store = queue_ra_store,
231};
232
233static struct queue_sysfs_entry queue_max_sectors_entry = {
234 .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
235 .show = queue_max_sectors_show,
236 .store = queue_max_sectors_store,
237};
238
239static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
240 .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
241 .show = queue_max_hw_sectors_show,
242};
243
244static struct queue_sysfs_entry queue_iosched_entry = {
245 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
246 .show = elv_iosched_show,
247 .store = elv_iosched_store,
248};
249
Martin K. Petersene68b9032008-01-29 19:14:08 +0100250static struct queue_sysfs_entry queue_hw_sector_size_entry = {
251 .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
252 .show = queue_hw_sector_size_show,
253};
254
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100255static struct queue_sysfs_entry queue_nonrot_entry = {
256 .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
257 .show = queue_nonrot_show,
258 .store = queue_nonrot_store,
259};
260
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200261static struct queue_sysfs_entry queue_nomerges_entry = {
262 .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
263 .show = queue_nomerges_show,
264 .store = queue_nomerges_store,
265};
266
Jens Axboec7c22e42008-09-13 20:26:01 +0200267static struct queue_sysfs_entry queue_rq_affinity_entry = {
268 .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
269 .show = queue_rq_affinity_show,
270 .store = queue_rq_affinity_store,
271};
272
Jens Axboebc58ba92009-01-23 10:54:44 +0100273static struct queue_sysfs_entry queue_iostats_entry = {
274 .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
275 .show = queue_iostats_show,
276 .store = queue_iostats_store,
277};
278
Jens Axboe8324aa92008-01-29 14:51:59 +0100279static struct attribute *default_attrs[] = {
280 &queue_requests_entry.attr,
281 &queue_ra_entry.attr,
282 &queue_max_hw_sectors_entry.attr,
283 &queue_max_sectors_entry.attr,
284 &queue_iosched_entry.attr,
Martin K. Petersene68b9032008-01-29 19:14:08 +0100285 &queue_hw_sector_size_entry.attr,
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100286 &queue_nonrot_entry.attr,
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200287 &queue_nomerges_entry.attr,
Jens Axboec7c22e42008-09-13 20:26:01 +0200288 &queue_rq_affinity_entry.attr,
Jens Axboebc58ba92009-01-23 10:54:44 +0100289 &queue_iostats_entry.attr,
Jens Axboe8324aa92008-01-29 14:51:59 +0100290 NULL,
291};
292
293#define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
294
295static ssize_t
296queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
297{
298 struct queue_sysfs_entry *entry = to_queue(attr);
299 struct request_queue *q =
300 container_of(kobj, struct request_queue, kobj);
301 ssize_t res;
302
303 if (!entry->show)
304 return -EIO;
305 mutex_lock(&q->sysfs_lock);
306 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
307 mutex_unlock(&q->sysfs_lock);
308 return -ENOENT;
309 }
310 res = entry->show(q, page);
311 mutex_unlock(&q->sysfs_lock);
312 return res;
313}
314
315static ssize_t
316queue_attr_store(struct kobject *kobj, struct attribute *attr,
317 const char *page, size_t length)
318{
319 struct queue_sysfs_entry *entry = to_queue(attr);
Jens Axboe6728cb02008-01-31 13:03:55 +0100320 struct request_queue *q;
Jens Axboe8324aa92008-01-29 14:51:59 +0100321 ssize_t res;
322
323 if (!entry->store)
324 return -EIO;
Jens Axboe6728cb02008-01-31 13:03:55 +0100325
326 q = container_of(kobj, struct request_queue, kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100327 mutex_lock(&q->sysfs_lock);
328 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
329 mutex_unlock(&q->sysfs_lock);
330 return -ENOENT;
331 }
332 res = entry->store(q, page, length);
333 mutex_unlock(&q->sysfs_lock);
334 return res;
335}
336
337/**
338 * blk_cleanup_queue: - release a &struct request_queue when it is no longer needed
339 * @kobj: the kobj belonging of the request queue to be released
340 *
341 * Description:
342 * blk_cleanup_queue is the pair to blk_init_queue() or
343 * blk_queue_make_request(). It should be called when a request queue is
344 * being released; typically when a block device is being de-registered.
345 * Currently, its primary task it to free all the &struct request
346 * structures that were allocated to the queue and the queue itself.
347 *
348 * Caveat:
349 * Hopefully the low level driver will have finished any
350 * outstanding requests first...
351 **/
352static void blk_release_queue(struct kobject *kobj)
353{
354 struct request_queue *q =
355 container_of(kobj, struct request_queue, kobj);
356 struct request_list *rl = &q->rq;
357
358 blk_sync_queue(q);
359
360 if (rl->rq_pool)
361 mempool_destroy(rl->rq_pool);
362
363 if (q->queue_tags)
364 __blk_queue_free_tags(q);
365
366 blk_trace_shutdown(q);
367
368 bdi_destroy(&q->backing_dev_info);
369 kmem_cache_free(blk_requestq_cachep, q);
370}
371
372static struct sysfs_ops queue_sysfs_ops = {
373 .show = queue_attr_show,
374 .store = queue_attr_store,
375};
376
377struct kobj_type blk_queue_ktype = {
378 .sysfs_ops = &queue_sysfs_ops,
379 .default_attrs = default_attrs,
380 .release = blk_release_queue,
381};
382
383int blk_register_queue(struct gendisk *disk)
384{
385 int ret;
386
387 struct request_queue *q = disk->queue;
388
Akinobu Mitafb199742008-04-21 09:51:06 +0200389 if (WARN_ON(!q))
Jens Axboe8324aa92008-01-29 14:51:59 +0100390 return -ENXIO;
391
Akinobu Mitafb199742008-04-21 09:51:06 +0200392 if (!q->request_fn)
393 return 0;
394
Tejun Heoed9e1982008-08-25 19:56:05 +0900395 ret = kobject_add(&q->kobj, kobject_get(&disk_to_dev(disk)->kobj),
Jens Axboe8324aa92008-01-29 14:51:59 +0100396 "%s", "queue");
397 if (ret < 0)
398 return ret;
399
400 kobject_uevent(&q->kobj, KOBJ_ADD);
401
402 ret = elv_register_queue(q);
403 if (ret) {
404 kobject_uevent(&q->kobj, KOBJ_REMOVE);
405 kobject_del(&q->kobj);
406 return ret;
407 }
408
409 return 0;
410}
411
412void blk_unregister_queue(struct gendisk *disk)
413{
414 struct request_queue *q = disk->queue;
415
Akinobu Mitafb199742008-04-21 09:51:06 +0200416 if (WARN_ON(!q))
417 return;
418
419 if (q->request_fn) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100420 elv_unregister_queue(q);
421
422 kobject_uevent(&q->kobj, KOBJ_REMOVE);
423 kobject_del(&q->kobj);
Tejun Heoed9e1982008-08-25 19:56:05 +0900424 kobject_put(&disk_to_dev(disk)->kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100425 }
426}