blob: b78c9c3e26705f45fb1e90ac116dc8bb96c8122b [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
Xiaotian Feng9cb308c2009-07-17 15:26:26 +080019queue_var_show(unsigned long var, char *page)
Jens Axboe8324aa92008-01-29 14:51:59 +010020{
Xiaotian Feng9cb308c2009-07-17 15:26:26 +080021 return sprintf(page, "%lu\n", var);
Jens Axboe8324aa92008-01-29 14:51:59 +010022}
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;
Jens Axboeb8a9ae72009-09-11 22:44:29 +020043 int ret;
44
45 if (!q->request_fn)
46 return -EINVAL;
47
48 ret = queue_var_store(&nr, page, count);
Jens Axboe8324aa92008-01-29 14:51:59 +010049 if (nr < BLKDEV_MIN_RQ)
50 nr = BLKDEV_MIN_RQ;
51
52 spin_lock_irq(q->queue_lock);
53 q->nr_requests = nr;
54 blk_queue_congestion_threshold(q);
55
Jens Axboe1faa16d2009-04-06 14:48:01 +020056 if (rl->count[BLK_RW_SYNC] >= queue_congestion_on_threshold(q))
57 blk_set_queue_congested(q, BLK_RW_SYNC);
58 else if (rl->count[BLK_RW_SYNC] < queue_congestion_off_threshold(q))
59 blk_clear_queue_congested(q, BLK_RW_SYNC);
Jens Axboe8324aa92008-01-29 14:51:59 +010060
Jens Axboe1faa16d2009-04-06 14:48:01 +020061 if (rl->count[BLK_RW_ASYNC] >= queue_congestion_on_threshold(q))
62 blk_set_queue_congested(q, BLK_RW_ASYNC);
63 else if (rl->count[BLK_RW_ASYNC] < queue_congestion_off_threshold(q))
64 blk_clear_queue_congested(q, BLK_RW_ASYNC);
Jens Axboe8324aa92008-01-29 14:51:59 +010065
Jens Axboe1faa16d2009-04-06 14:48:01 +020066 if (rl->count[BLK_RW_SYNC] >= q->nr_requests) {
67 blk_set_queue_full(q, BLK_RW_SYNC);
68 } else if (rl->count[BLK_RW_SYNC]+1 <= q->nr_requests) {
69 blk_clear_queue_full(q, BLK_RW_SYNC);
70 wake_up(&rl->wait[BLK_RW_SYNC]);
Jens Axboe8324aa92008-01-29 14:51:59 +010071 }
72
Jens Axboe1faa16d2009-04-06 14:48:01 +020073 if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) {
74 blk_set_queue_full(q, BLK_RW_ASYNC);
75 } else if (rl->count[BLK_RW_ASYNC]+1 <= q->nr_requests) {
76 blk_clear_queue_full(q, BLK_RW_ASYNC);
77 wake_up(&rl->wait[BLK_RW_ASYNC]);
Jens Axboe8324aa92008-01-29 14:51:59 +010078 }
79 spin_unlock_irq(q->queue_lock);
80 return ret;
81}
82
83static ssize_t queue_ra_show(struct request_queue *q, char *page)
84{
Xiaotian Feng9cb308c2009-07-17 15:26:26 +080085 unsigned long ra_kb = q->backing_dev_info.ra_pages <<
86 (PAGE_CACHE_SHIFT - 10);
Jens Axboe8324aa92008-01-29 14:51:59 +010087
88 return queue_var_show(ra_kb, (page));
89}
90
91static ssize_t
92queue_ra_store(struct request_queue *q, const char *page, size_t count)
93{
94 unsigned long ra_kb;
95 ssize_t ret = queue_var_store(&ra_kb, page, count);
96
Jens Axboe8324aa92008-01-29 14:51:59 +010097 q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
Jens Axboe8324aa92008-01-29 14:51:59 +010098
99 return ret;
100}
101
102static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
103{
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400104 int max_sectors_kb = queue_max_sectors(q) >> 1;
Jens Axboe8324aa92008-01-29 14:51:59 +0100105
106 return queue_var_show(max_sectors_kb, (page));
107}
108
Martin K. Petersene1defc42009-05-22 17:17:49 -0400109static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
Martin K. Petersene68b9032008-01-29 19:14:08 +0100110{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400111 return queue_var_show(queue_logical_block_size(q), page);
Martin K. Petersene68b9032008-01-29 19:14:08 +0100112}
113
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400114static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
115{
116 return queue_var_show(queue_physical_block_size(q), page);
117}
118
119static ssize_t queue_io_min_show(struct request_queue *q, char *page)
120{
121 return queue_var_show(queue_io_min(q), page);
122}
123
124static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
125{
126 return queue_var_show(queue_io_opt(q), page);
Jens Axboe8324aa92008-01-29 14:51:59 +0100127}
128
129static ssize_t
130queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
131{
132 unsigned long max_sectors_kb,
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400133 max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
Jens Axboe8324aa92008-01-29 14:51:59 +0100134 page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
135 ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
136
137 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
138 return -EINVAL;
Wu Fengguang7c239512008-11-25 09:08:39 +0100139
Jens Axboe8324aa92008-01-29 14:51:59 +0100140 spin_lock_irq(q->queue_lock);
Nikanth Karthikesanc295fc02009-09-01 22:40:15 +0200141 q->limits.max_sectors = max_sectors_kb << 1;
Jens Axboe8324aa92008-01-29 14:51:59 +0100142 spin_unlock_irq(q->queue_lock);
143
144 return ret;
145}
146
147static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
148{
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400149 int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
Jens Axboe8324aa92008-01-29 14:51:59 +0100150
151 return queue_var_show(max_hw_sectors_kb, (page));
152}
153
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100154static ssize_t queue_nonrot_show(struct request_queue *q, char *page)
155{
156 return queue_var_show(!blk_queue_nonrot(q), page);
157}
158
159static ssize_t queue_nonrot_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
165 spin_lock_irq(q->queue_lock);
166 if (nm)
167 queue_flag_clear(QUEUE_FLAG_NONROT, q);
168 else
169 queue_flag_set(QUEUE_FLAG_NONROT, q);
170 spin_unlock_irq(q->queue_lock);
171
172 return ret;
173}
174
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200175static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
176{
177 return queue_var_show(blk_queue_nomerges(q), page);
178}
179
180static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
181 size_t count)
182{
183 unsigned long nm;
184 ssize_t ret = queue_var_store(&nm, page, count);
185
Jens Axboebf0f9702008-05-07 09:09:39 +0200186 spin_lock_irq(q->queue_lock);
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200187 if (nm)
Jens Axboebf0f9702008-05-07 09:09:39 +0200188 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200189 else
Jens Axboebf0f9702008-05-07 09:09:39 +0200190 queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
Jens Axboebf0f9702008-05-07 09:09:39 +0200191 spin_unlock_irq(q->queue_lock);
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100192
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200193 return ret;
194}
195
Jens Axboec7c22e42008-09-13 20:26:01 +0200196static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
197{
Xiaotian Feng9cb308c2009-07-17 15:26:26 +0800198 bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
Jens Axboec7c22e42008-09-13 20:26:01 +0200199
Xiaotian Feng9cb308c2009-07-17 15:26:26 +0800200 return queue_var_show(set, page);
Jens Axboec7c22e42008-09-13 20:26:01 +0200201}
202
203static ssize_t
204queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
205{
206 ssize_t ret = -EINVAL;
207#if defined(CONFIG_USE_GENERIC_SMP_HELPERS)
208 unsigned long val;
209
210 ret = queue_var_store(&val, page, count);
211 spin_lock_irq(q->queue_lock);
212 if (val)
213 queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
214 else
215 queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
216 spin_unlock_irq(q->queue_lock);
217#endif
218 return ret;
219}
Jens Axboe8324aa92008-01-29 14:51:59 +0100220
Jens Axboebc58ba92009-01-23 10:54:44 +0100221static ssize_t queue_iostats_show(struct request_queue *q, char *page)
222{
223 return queue_var_show(blk_queue_io_stat(q), page);
224}
225
226static ssize_t queue_iostats_store(struct request_queue *q, const char *page,
227 size_t count)
228{
229 unsigned long stats;
230 ssize_t ret = queue_var_store(&stats, page, count);
231
232 spin_lock_irq(q->queue_lock);
233 if (stats)
234 queue_flag_set(QUEUE_FLAG_IO_STAT, q);
235 else
236 queue_flag_clear(QUEUE_FLAG_IO_STAT, q);
237 spin_unlock_irq(q->queue_lock);
238
239 return ret;
240}
241
Jens Axboe8324aa92008-01-29 14:51:59 +0100242static struct queue_sysfs_entry queue_requests_entry = {
243 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
244 .show = queue_requests_show,
245 .store = queue_requests_store,
246};
247
248static struct queue_sysfs_entry queue_ra_entry = {
249 .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
250 .show = queue_ra_show,
251 .store = queue_ra_store,
252};
253
254static struct queue_sysfs_entry queue_max_sectors_entry = {
255 .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
256 .show = queue_max_sectors_show,
257 .store = queue_max_sectors_store,
258};
259
260static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
261 .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
262 .show = queue_max_hw_sectors_show,
263};
264
265static struct queue_sysfs_entry queue_iosched_entry = {
266 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
267 .show = elv_iosched_show,
268 .store = elv_iosched_store,
269};
270
Martin K. Petersene68b9032008-01-29 19:14:08 +0100271static struct queue_sysfs_entry queue_hw_sector_size_entry = {
272 .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
Martin K. Petersene1defc42009-05-22 17:17:49 -0400273 .show = queue_logical_block_size_show,
274};
275
276static struct queue_sysfs_entry queue_logical_block_size_entry = {
277 .attr = {.name = "logical_block_size", .mode = S_IRUGO },
278 .show = queue_logical_block_size_show,
Martin K. Petersene68b9032008-01-29 19:14:08 +0100279};
280
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400281static struct queue_sysfs_entry queue_physical_block_size_entry = {
282 .attr = {.name = "physical_block_size", .mode = S_IRUGO },
283 .show = queue_physical_block_size_show,
284};
285
286static struct queue_sysfs_entry queue_io_min_entry = {
287 .attr = {.name = "minimum_io_size", .mode = S_IRUGO },
288 .show = queue_io_min_show,
289};
290
291static struct queue_sysfs_entry queue_io_opt_entry = {
292 .attr = {.name = "optimal_io_size", .mode = S_IRUGO },
293 .show = queue_io_opt_show,
Jens Axboe8324aa92008-01-29 14:51:59 +0100294};
295
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100296static struct queue_sysfs_entry queue_nonrot_entry = {
297 .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
298 .show = queue_nonrot_show,
299 .store = queue_nonrot_store,
300};
301
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200302static struct queue_sysfs_entry queue_nomerges_entry = {
303 .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
304 .show = queue_nomerges_show,
305 .store = queue_nomerges_store,
306};
307
Jens Axboec7c22e42008-09-13 20:26:01 +0200308static struct queue_sysfs_entry queue_rq_affinity_entry = {
309 .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
310 .show = queue_rq_affinity_show,
311 .store = queue_rq_affinity_store,
312};
313
Jens Axboebc58ba92009-01-23 10:54:44 +0100314static struct queue_sysfs_entry queue_iostats_entry = {
315 .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
316 .show = queue_iostats_show,
317 .store = queue_iostats_store,
318};
319
Jens Axboe8324aa92008-01-29 14:51:59 +0100320static struct attribute *default_attrs[] = {
321 &queue_requests_entry.attr,
322 &queue_ra_entry.attr,
323 &queue_max_hw_sectors_entry.attr,
324 &queue_max_sectors_entry.attr,
325 &queue_iosched_entry.attr,
Martin K. Petersene68b9032008-01-29 19:14:08 +0100326 &queue_hw_sector_size_entry.attr,
Martin K. Petersene1defc42009-05-22 17:17:49 -0400327 &queue_logical_block_size_entry.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400328 &queue_physical_block_size_entry.attr,
329 &queue_io_min_entry.attr,
330 &queue_io_opt_entry.attr,
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100331 &queue_nonrot_entry.attr,
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200332 &queue_nomerges_entry.attr,
Jens Axboec7c22e42008-09-13 20:26:01 +0200333 &queue_rq_affinity_entry.attr,
Jens Axboebc58ba92009-01-23 10:54:44 +0100334 &queue_iostats_entry.attr,
Jens Axboe8324aa92008-01-29 14:51:59 +0100335 NULL,
336};
337
338#define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
339
340static ssize_t
341queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
342{
343 struct queue_sysfs_entry *entry = to_queue(attr);
344 struct request_queue *q =
345 container_of(kobj, struct request_queue, kobj);
346 ssize_t res;
347
348 if (!entry->show)
349 return -EIO;
350 mutex_lock(&q->sysfs_lock);
351 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
352 mutex_unlock(&q->sysfs_lock);
353 return -ENOENT;
354 }
355 res = entry->show(q, page);
356 mutex_unlock(&q->sysfs_lock);
357 return res;
358}
359
360static ssize_t
361queue_attr_store(struct kobject *kobj, struct attribute *attr,
362 const char *page, size_t length)
363{
364 struct queue_sysfs_entry *entry = to_queue(attr);
Jens Axboe6728cb02008-01-31 13:03:55 +0100365 struct request_queue *q;
Jens Axboe8324aa92008-01-29 14:51:59 +0100366 ssize_t res;
367
368 if (!entry->store)
369 return -EIO;
Jens Axboe6728cb02008-01-31 13:03:55 +0100370
371 q = container_of(kobj, struct request_queue, kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100372 mutex_lock(&q->sysfs_lock);
373 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
374 mutex_unlock(&q->sysfs_lock);
375 return -ENOENT;
376 }
377 res = entry->store(q, page, length);
378 mutex_unlock(&q->sysfs_lock);
379 return res;
380}
381
382/**
383 * blk_cleanup_queue: - release a &struct request_queue when it is no longer needed
384 * @kobj: the kobj belonging of the request queue to be released
385 *
386 * Description:
387 * blk_cleanup_queue is the pair to blk_init_queue() or
388 * blk_queue_make_request(). It should be called when a request queue is
389 * being released; typically when a block device is being de-registered.
390 * Currently, its primary task it to free all the &struct request
391 * structures that were allocated to the queue and the queue itself.
392 *
393 * Caveat:
394 * Hopefully the low level driver will have finished any
395 * outstanding requests first...
396 **/
397static void blk_release_queue(struct kobject *kobj)
398{
399 struct request_queue *q =
400 container_of(kobj, struct request_queue, kobj);
401 struct request_list *rl = &q->rq;
402
403 blk_sync_queue(q);
404
405 if (rl->rq_pool)
406 mempool_destroy(rl->rq_pool);
407
408 if (q->queue_tags)
409 __blk_queue_free_tags(q);
410
411 blk_trace_shutdown(q);
412
413 bdi_destroy(&q->backing_dev_info);
414 kmem_cache_free(blk_requestq_cachep, q);
415}
416
417static struct sysfs_ops queue_sysfs_ops = {
418 .show = queue_attr_show,
419 .store = queue_attr_store,
420};
421
422struct kobj_type blk_queue_ktype = {
423 .sysfs_ops = &queue_sysfs_ops,
424 .default_attrs = default_attrs,
425 .release = blk_release_queue,
426};
427
428int blk_register_queue(struct gendisk *disk)
429{
430 int ret;
Li Zefan1d54ad62009-04-14 14:00:05 +0800431 struct device *dev = disk_to_dev(disk);
Jens Axboe8324aa92008-01-29 14:51:59 +0100432
433 struct request_queue *q = disk->queue;
434
Akinobu Mitafb199742008-04-21 09:51:06 +0200435 if (WARN_ON(!q))
Jens Axboe8324aa92008-01-29 14:51:59 +0100436 return -ENXIO;
437
Li Zefan1d54ad62009-04-14 14:00:05 +0800438 ret = blk_trace_init_sysfs(dev);
439 if (ret)
440 return ret;
441
Linus Torvaldsc9059592009-06-11 10:52:27 -0700442 ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
Jens Axboe8324aa92008-01-29 14:51:59 +0100443 if (ret < 0)
444 return ret;
445
446 kobject_uevent(&q->kobj, KOBJ_ADD);
447
Martin K. Petersencd43e262009-05-22 17:17:52 -0400448 if (!q->request_fn)
449 return 0;
450
Jens Axboe8324aa92008-01-29 14:51:59 +0100451 ret = elv_register_queue(q);
452 if (ret) {
453 kobject_uevent(&q->kobj, KOBJ_REMOVE);
454 kobject_del(&q->kobj);
455 return ret;
456 }
457
458 return 0;
459}
460
461void blk_unregister_queue(struct gendisk *disk)
462{
463 struct request_queue *q = disk->queue;
464
Akinobu Mitafb199742008-04-21 09:51:06 +0200465 if (WARN_ON(!q))
466 return;
467
468 if (q->request_fn) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100469 elv_unregister_queue(q);
470
471 kobject_uevent(&q->kobj, KOBJ_REMOVE);
472 kobject_del(&q->kobj);
Tejun Heoed9e1982008-08-25 19:56:05 +0900473 kobject_put(&disk_to_dev(disk)->kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100474 }
475}