blob: 9337e17f9110d2e6706b61c2ad4f07d7fe0596fb [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{
Martin K. Petersenae03bf62009-05-22 17:17:50 -040098 int max_sectors_kb = queue_max_sectors(q) >> 1;
Jens Axboe8324aa92008-01-29 14:51:59 +010099
100 return queue_var_show(max_sectors_kb, (page));
101}
102
Martin K. Petersene1defc42009-05-22 17:17:49 -0400103static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
Martin K. Petersene68b9032008-01-29 19:14:08 +0100104{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400105 return queue_var_show(queue_logical_block_size(q), page);
Martin K. Petersene68b9032008-01-29 19:14:08 +0100106}
107
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400108static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
109{
110 return queue_var_show(queue_physical_block_size(q), page);
111}
112
113static ssize_t queue_io_min_show(struct request_queue *q, char *page)
114{
115 return queue_var_show(queue_io_min(q), page);
116}
117
118static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
119{
120 return queue_var_show(queue_io_opt(q), page);
121}
122
Jens Axboe8324aa92008-01-29 14:51:59 +0100123static ssize_t
124queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
125{
126 unsigned long max_sectors_kb,
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400127 max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
Jens Axboe8324aa92008-01-29 14:51:59 +0100128 page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
129 ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
130
131 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
132 return -EINVAL;
Wu Fengguang7c239512008-11-25 09:08:39 +0100133
Jens Axboe8324aa92008-01-29 14:51:59 +0100134 spin_lock_irq(q->queue_lock);
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400135 blk_queue_max_sectors(q, max_sectors_kb << 1);
Jens Axboe8324aa92008-01-29 14:51:59 +0100136 spin_unlock_irq(q->queue_lock);
137
138 return ret;
139}
140
141static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
142{
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400143 int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
Jens Axboe8324aa92008-01-29 14:51:59 +0100144
145 return queue_var_show(max_hw_sectors_kb, (page));
146}
147
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100148static ssize_t queue_nonrot_show(struct request_queue *q, char *page)
149{
150 return queue_var_show(!blk_queue_nonrot(q), page);
151}
152
153static ssize_t queue_nonrot_store(struct request_queue *q, const char *page,
154 size_t count)
155{
156 unsigned long nm;
157 ssize_t ret = queue_var_store(&nm, page, count);
158
159 spin_lock_irq(q->queue_lock);
160 if (nm)
161 queue_flag_clear(QUEUE_FLAG_NONROT, q);
162 else
163 queue_flag_set(QUEUE_FLAG_NONROT, q);
164 spin_unlock_irq(q->queue_lock);
165
166 return ret;
167}
168
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200169static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
170{
171 return queue_var_show(blk_queue_nomerges(q), page);
172}
173
174static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
175 size_t count)
176{
177 unsigned long nm;
178 ssize_t ret = queue_var_store(&nm, page, count);
179
Jens Axboebf0f9702008-05-07 09:09:39 +0200180 spin_lock_irq(q->queue_lock);
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200181 if (nm)
Jens Axboebf0f9702008-05-07 09:09:39 +0200182 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200183 else
Jens Axboebf0f9702008-05-07 09:09:39 +0200184 queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
Jens Axboebf0f9702008-05-07 09:09:39 +0200185 spin_unlock_irq(q->queue_lock);
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100186
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200187 return ret;
188}
189
Jens Axboec7c22e42008-09-13 20:26:01 +0200190static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
191{
192 unsigned int set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
193
194 return queue_var_show(set != 0, page);
195}
196
197static ssize_t
198queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
199{
200 ssize_t ret = -EINVAL;
201#if defined(CONFIG_USE_GENERIC_SMP_HELPERS)
202 unsigned long val;
203
204 ret = queue_var_store(&val, page, count);
205 spin_lock_irq(q->queue_lock);
206 if (val)
207 queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
208 else
209 queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
210 spin_unlock_irq(q->queue_lock);
211#endif
212 return ret;
213}
Jens Axboe8324aa92008-01-29 14:51:59 +0100214
Jens Axboebc58ba92009-01-23 10:54:44 +0100215static ssize_t queue_iostats_show(struct request_queue *q, char *page)
216{
217 return queue_var_show(blk_queue_io_stat(q), page);
218}
219
220static ssize_t queue_iostats_store(struct request_queue *q, const char *page,
221 size_t count)
222{
223 unsigned long stats;
224 ssize_t ret = queue_var_store(&stats, page, count);
225
226 spin_lock_irq(q->queue_lock);
227 if (stats)
228 queue_flag_set(QUEUE_FLAG_IO_STAT, q);
229 else
230 queue_flag_clear(QUEUE_FLAG_IO_STAT, q);
231 spin_unlock_irq(q->queue_lock);
232
233 return ret;
234}
235
Jens Axboe8324aa92008-01-29 14:51:59 +0100236static struct queue_sysfs_entry queue_requests_entry = {
237 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
238 .show = queue_requests_show,
239 .store = queue_requests_store,
240};
241
242static struct queue_sysfs_entry queue_ra_entry = {
243 .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
244 .show = queue_ra_show,
245 .store = queue_ra_store,
246};
247
248static struct queue_sysfs_entry queue_max_sectors_entry = {
249 .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
250 .show = queue_max_sectors_show,
251 .store = queue_max_sectors_store,
252};
253
254static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
255 .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
256 .show = queue_max_hw_sectors_show,
257};
258
259static struct queue_sysfs_entry queue_iosched_entry = {
260 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
261 .show = elv_iosched_show,
262 .store = elv_iosched_store,
263};
264
Martin K. Petersene68b9032008-01-29 19:14:08 +0100265static struct queue_sysfs_entry queue_hw_sector_size_entry = {
266 .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
Martin K. Petersene1defc42009-05-22 17:17:49 -0400267 .show = queue_logical_block_size_show,
268};
269
270static struct queue_sysfs_entry queue_logical_block_size_entry = {
271 .attr = {.name = "logical_block_size", .mode = S_IRUGO },
272 .show = queue_logical_block_size_show,
Martin K. Petersene68b9032008-01-29 19:14:08 +0100273};
274
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400275static struct queue_sysfs_entry queue_physical_block_size_entry = {
276 .attr = {.name = "physical_block_size", .mode = S_IRUGO },
277 .show = queue_physical_block_size_show,
278};
279
280static struct queue_sysfs_entry queue_io_min_entry = {
281 .attr = {.name = "minimum_io_size", .mode = S_IRUGO },
282 .show = queue_io_min_show,
283};
284
285static struct queue_sysfs_entry queue_io_opt_entry = {
286 .attr = {.name = "optimal_io_size", .mode = S_IRUGO },
287 .show = queue_io_opt_show,
288};
289
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100290static struct queue_sysfs_entry queue_nonrot_entry = {
291 .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
292 .show = queue_nonrot_show,
293 .store = queue_nonrot_store,
294};
295
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200296static struct queue_sysfs_entry queue_nomerges_entry = {
297 .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
298 .show = queue_nomerges_show,
299 .store = queue_nomerges_store,
300};
301
Jens Axboec7c22e42008-09-13 20:26:01 +0200302static struct queue_sysfs_entry queue_rq_affinity_entry = {
303 .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
304 .show = queue_rq_affinity_show,
305 .store = queue_rq_affinity_store,
306};
307
Jens Axboebc58ba92009-01-23 10:54:44 +0100308static struct queue_sysfs_entry queue_iostats_entry = {
309 .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
310 .show = queue_iostats_show,
311 .store = queue_iostats_store,
312};
313
Jens Axboe8324aa92008-01-29 14:51:59 +0100314static struct attribute *default_attrs[] = {
315 &queue_requests_entry.attr,
316 &queue_ra_entry.attr,
317 &queue_max_hw_sectors_entry.attr,
318 &queue_max_sectors_entry.attr,
319 &queue_iosched_entry.attr,
Martin K. Petersene68b9032008-01-29 19:14:08 +0100320 &queue_hw_sector_size_entry.attr,
Martin K. Petersene1defc42009-05-22 17:17:49 -0400321 &queue_logical_block_size_entry.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400322 &queue_physical_block_size_entry.attr,
323 &queue_io_min_entry.attr,
324 &queue_io_opt_entry.attr,
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100325 &queue_nonrot_entry.attr,
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200326 &queue_nomerges_entry.attr,
Jens Axboec7c22e42008-09-13 20:26:01 +0200327 &queue_rq_affinity_entry.attr,
Jens Axboebc58ba92009-01-23 10:54:44 +0100328 &queue_iostats_entry.attr,
Jens Axboe8324aa92008-01-29 14:51:59 +0100329 NULL,
330};
331
332#define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
333
334static ssize_t
335queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
336{
337 struct queue_sysfs_entry *entry = to_queue(attr);
338 struct request_queue *q =
339 container_of(kobj, struct request_queue, kobj);
340 ssize_t res;
341
342 if (!entry->show)
343 return -EIO;
344 mutex_lock(&q->sysfs_lock);
345 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
346 mutex_unlock(&q->sysfs_lock);
347 return -ENOENT;
348 }
349 res = entry->show(q, page);
350 mutex_unlock(&q->sysfs_lock);
351 return res;
352}
353
354static ssize_t
355queue_attr_store(struct kobject *kobj, struct attribute *attr,
356 const char *page, size_t length)
357{
358 struct queue_sysfs_entry *entry = to_queue(attr);
Jens Axboe6728cb02008-01-31 13:03:55 +0100359 struct request_queue *q;
Jens Axboe8324aa92008-01-29 14:51:59 +0100360 ssize_t res;
361
362 if (!entry->store)
363 return -EIO;
Jens Axboe6728cb02008-01-31 13:03:55 +0100364
365 q = container_of(kobj, struct request_queue, kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100366 mutex_lock(&q->sysfs_lock);
367 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
368 mutex_unlock(&q->sysfs_lock);
369 return -ENOENT;
370 }
371 res = entry->store(q, page, length);
372 mutex_unlock(&q->sysfs_lock);
373 return res;
374}
375
376/**
377 * blk_cleanup_queue: - release a &struct request_queue when it is no longer needed
378 * @kobj: the kobj belonging of the request queue to be released
379 *
380 * Description:
381 * blk_cleanup_queue is the pair to blk_init_queue() or
382 * blk_queue_make_request(). It should be called when a request queue is
383 * being released; typically when a block device is being de-registered.
384 * Currently, its primary task it to free all the &struct request
385 * structures that were allocated to the queue and the queue itself.
386 *
387 * Caveat:
388 * Hopefully the low level driver will have finished any
389 * outstanding requests first...
390 **/
391static void blk_release_queue(struct kobject *kobj)
392{
393 struct request_queue *q =
394 container_of(kobj, struct request_queue, kobj);
395 struct request_list *rl = &q->rq;
396
397 blk_sync_queue(q);
398
399 if (rl->rq_pool)
400 mempool_destroy(rl->rq_pool);
401
402 if (q->queue_tags)
403 __blk_queue_free_tags(q);
404
405 blk_trace_shutdown(q);
406
407 bdi_destroy(&q->backing_dev_info);
408 kmem_cache_free(blk_requestq_cachep, q);
409}
410
411static struct sysfs_ops queue_sysfs_ops = {
412 .show = queue_attr_show,
413 .store = queue_attr_store,
414};
415
416struct kobj_type blk_queue_ktype = {
417 .sysfs_ops = &queue_sysfs_ops,
418 .default_attrs = default_attrs,
419 .release = blk_release_queue,
420};
421
422int blk_register_queue(struct gendisk *disk)
423{
424 int ret;
425
426 struct request_queue *q = disk->queue;
427
Akinobu Mitafb199742008-04-21 09:51:06 +0200428 if (WARN_ON(!q))
Jens Axboe8324aa92008-01-29 14:51:59 +0100429 return -ENXIO;
430
Tejun Heoed9e1982008-08-25 19:56:05 +0900431 ret = kobject_add(&q->kobj, kobject_get(&disk_to_dev(disk)->kobj),
Jens Axboe8324aa92008-01-29 14:51:59 +0100432 "%s", "queue");
433 if (ret < 0)
434 return ret;
435
436 kobject_uevent(&q->kobj, KOBJ_ADD);
437
Martin K. Petersencd43e262009-05-22 17:17:52 -0400438 if (!q->request_fn)
439 return 0;
440
Jens Axboe8324aa92008-01-29 14:51:59 +0100441 ret = elv_register_queue(q);
442 if (ret) {
443 kobject_uevent(&q->kobj, KOBJ_REMOVE);
444 kobject_del(&q->kobj);
445 return ret;
446 }
447
448 return 0;
449}
450
451void blk_unregister_queue(struct gendisk *disk)
452{
453 struct request_queue *q = disk->queue;
454
Akinobu Mitafb199742008-04-21 09:51:06 +0200455 if (WARN_ON(!q))
456 return;
457
458 if (q->request_fn) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100459 elv_unregister_queue(q);
460
461 kobject_uevent(&q->kobj, KOBJ_REMOVE);
462 kobject_del(&q->kobj);
Tejun Heoed9e1982008-08-25 19:56:05 +0900463 kobject_put(&disk_to_dev(disk)->kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100464 }
465}