blob: 6206a934eb8c85a2f21636759f1b64afe8e3fb70 [file] [log] [blame]
Jens Axboe8324aa92008-01-29 14:51:59 +01001/*
2 * Functions related to sysfs handling
3 */
4#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/slab.h>
Jens Axboe8324aa92008-01-29 14:51:59 +01006#include <linux/module.h>
7#include <linux/bio.h>
8#include <linux/blkdev.h>
9#include <linux/blktrace_api.h>
10
11#include "blk.h"
Tejun Heo5efd6112012-03-05 13:15:12 -080012#include "blk-cgroup.h"
Jens Axboe8324aa92008-01-29 14:51:59 +010013
14struct queue_sysfs_entry {
15 struct attribute attr;
16 ssize_t (*show)(struct request_queue *, char *);
17 ssize_t (*store)(struct request_queue *, const char *, size_t);
18};
19
20static ssize_t
Xiaotian Feng9cb308c2009-07-17 15:26:26 +080021queue_var_show(unsigned long var, char *page)
Jens Axboe8324aa92008-01-29 14:51:59 +010022{
Xiaotian Feng9cb308c2009-07-17 15:26:26 +080023 return sprintf(page, "%lu\n", var);
Jens Axboe8324aa92008-01-29 14:51:59 +010024}
25
26static ssize_t
27queue_var_store(unsigned long *var, const char *page, size_t count)
28{
Dave Reisnerb1f3b642012-09-08 11:55:45 -040029 int err;
30 unsigned long v;
Jens Axboe8324aa92008-01-29 14:51:59 +010031
Dave Reisnerb1f3b642012-09-08 11:55:45 -040032 err = strict_strtoul(page, 10, &v);
33 if (err || v > UINT_MAX)
34 return -EINVAL;
35
36 *var = v;
37
Jens Axboe8324aa92008-01-29 14:51:59 +010038 return count;
39}
40
41static ssize_t queue_requests_show(struct request_queue *q, char *page)
42{
43 return queue_var_show(q->nr_requests, (page));
44}
45
46static ssize_t
47queue_requests_store(struct request_queue *q, const char *page, size_t count)
48{
Tejun Heoa0516612012-06-26 15:05:44 -070049 struct request_list *rl;
Jens Axboe8324aa92008-01-29 14:51:59 +010050 unsigned long nr;
Jens Axboeb8a9ae72009-09-11 22:44:29 +020051 int ret;
52
53 if (!q->request_fn)
54 return -EINVAL;
55
56 ret = queue_var_store(&nr, page, count);
Dave Reisnerb1f3b642012-09-08 11:55:45 -040057 if (ret < 0)
58 return ret;
59
Jens Axboe8324aa92008-01-29 14:51:59 +010060 if (nr < BLKDEV_MIN_RQ)
61 nr = BLKDEV_MIN_RQ;
62
63 spin_lock_irq(q->queue_lock);
64 q->nr_requests = nr;
65 blk_queue_congestion_threshold(q);
66
Tejun Heoa0516612012-06-26 15:05:44 -070067 /* congestion isn't cgroup aware and follows root blkcg for now */
68 rl = &q->root_rl;
69
Jens Axboe1faa16d2009-04-06 14:48:01 +020070 if (rl->count[BLK_RW_SYNC] >= queue_congestion_on_threshold(q))
71 blk_set_queue_congested(q, BLK_RW_SYNC);
72 else if (rl->count[BLK_RW_SYNC] < queue_congestion_off_threshold(q))
73 blk_clear_queue_congested(q, BLK_RW_SYNC);
Jens Axboe8324aa92008-01-29 14:51:59 +010074
Jens Axboe1faa16d2009-04-06 14:48:01 +020075 if (rl->count[BLK_RW_ASYNC] >= queue_congestion_on_threshold(q))
76 blk_set_queue_congested(q, BLK_RW_ASYNC);
77 else if (rl->count[BLK_RW_ASYNC] < queue_congestion_off_threshold(q))
78 blk_clear_queue_congested(q, BLK_RW_ASYNC);
Jens Axboe8324aa92008-01-29 14:51:59 +010079
Tejun Heoa0516612012-06-26 15:05:44 -070080 blk_queue_for_each_rl(rl, q) {
81 if (rl->count[BLK_RW_SYNC] >= q->nr_requests) {
82 blk_set_rl_full(rl, BLK_RW_SYNC);
83 } else {
84 blk_clear_rl_full(rl, BLK_RW_SYNC);
85 wake_up(&rl->wait[BLK_RW_SYNC]);
86 }
87
88 if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) {
89 blk_set_rl_full(rl, BLK_RW_ASYNC);
90 } else {
91 blk_clear_rl_full(rl, BLK_RW_ASYNC);
92 wake_up(&rl->wait[BLK_RW_ASYNC]);
93 }
Jens Axboe8324aa92008-01-29 14:51:59 +010094 }
95
Jens Axboe8324aa92008-01-29 14:51:59 +010096 spin_unlock_irq(q->queue_lock);
97 return ret;
98}
99
100static ssize_t queue_ra_show(struct request_queue *q, char *page)
101{
Xiaotian Feng9cb308c2009-07-17 15:26:26 +0800102 unsigned long ra_kb = q->backing_dev_info.ra_pages <<
103 (PAGE_CACHE_SHIFT - 10);
Jens Axboe8324aa92008-01-29 14:51:59 +0100104
105 return queue_var_show(ra_kb, (page));
106}
107
108static ssize_t
109queue_ra_store(struct request_queue *q, const char *page, size_t count)
110{
111 unsigned long ra_kb;
112 ssize_t ret = queue_var_store(&ra_kb, page, count);
113
Dave Reisnerb1f3b642012-09-08 11:55:45 -0400114 if (ret < 0)
115 return ret;
116
Jens Axboe8324aa92008-01-29 14:51:59 +0100117 q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
Jens Axboe8324aa92008-01-29 14:51:59 +0100118
119 return ret;
120}
121
122static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
123{
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400124 int max_sectors_kb = queue_max_sectors(q) >> 1;
Jens Axboe8324aa92008-01-29 14:51:59 +0100125
126 return queue_var_show(max_sectors_kb, (page));
127}
128
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500129static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
130{
131 return queue_var_show(queue_max_segments(q), (page));
132}
133
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200134static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
135{
136 return queue_var_show(q->limits.max_integrity_segments, (page));
137}
138
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500139static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
140{
Martin K. Petersene692cb62010-12-01 19:41:49 +0100141 if (blk_queue_cluster(q))
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500142 return queue_var_show(queue_max_segment_size(q), (page));
143
144 return queue_var_show(PAGE_CACHE_SIZE, (page));
145}
146
Martin K. Petersene1defc42009-05-22 17:17:49 -0400147static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
Martin K. Petersene68b9032008-01-29 19:14:08 +0100148{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400149 return queue_var_show(queue_logical_block_size(q), page);
Martin K. Petersene68b9032008-01-29 19:14:08 +0100150}
151
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400152static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
153{
154 return queue_var_show(queue_physical_block_size(q), page);
155}
156
157static ssize_t queue_io_min_show(struct request_queue *q, char *page)
158{
159 return queue_var_show(queue_io_min(q), page);
160}
161
162static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
163{
164 return queue_var_show(queue_io_opt(q), page);
Jens Axboe8324aa92008-01-29 14:51:59 +0100165}
166
Martin K. Petersen86b37282009-11-10 11:50:21 +0100167static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
168{
169 return queue_var_show(q->limits.discard_granularity, page);
170}
171
172static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
173{
Martin K. Petersena934a002011-05-18 10:37:35 +0200174 return sprintf(page, "%llu\n",
175 (unsigned long long)q->limits.max_discard_sectors << 9);
Martin K. Petersen86b37282009-11-10 11:50:21 +0100176}
177
Martin K. Petersen98262f22009-12-03 09:24:48 +0100178static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
179{
180 return queue_var_show(queue_discard_zeroes_data(q), page);
181}
182
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400183static ssize_t queue_write_same_max_show(struct request_queue *q, char *page)
184{
185 return sprintf(page, "%llu\n",
186 (unsigned long long)q->limits.max_write_same_sectors << 9);
187}
188
189
Jens Axboe8324aa92008-01-29 14:51:59 +0100190static ssize_t
191queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
192{
193 unsigned long max_sectors_kb,
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400194 max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
Jens Axboe8324aa92008-01-29 14:51:59 +0100195 page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
196 ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
197
Dave Reisnerb1f3b642012-09-08 11:55:45 -0400198 if (ret < 0)
199 return ret;
200
Jens Axboe8324aa92008-01-29 14:51:59 +0100201 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
202 return -EINVAL;
Wu Fengguang7c239512008-11-25 09:08:39 +0100203
Jens Axboe8324aa92008-01-29 14:51:59 +0100204 spin_lock_irq(q->queue_lock);
Nikanth Karthikesanc295fc02009-09-01 22:40:15 +0200205 q->limits.max_sectors = max_sectors_kb << 1;
Jens Axboe8324aa92008-01-29 14:51:59 +0100206 spin_unlock_irq(q->queue_lock);
207
208 return ret;
209}
210
211static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
212{
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400213 int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
Jens Axboe8324aa92008-01-29 14:51:59 +0100214
215 return queue_var_show(max_hw_sectors_kb, (page));
216}
217
Jens Axboe956bcb72010-08-07 18:13:50 +0200218#define QUEUE_SYSFS_BIT_FNS(name, flag, neg) \
219static ssize_t \
220queue_show_##name(struct request_queue *q, char *page) \
221{ \
222 int bit; \
223 bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags); \
224 return queue_var_show(neg ? !bit : bit, page); \
225} \
226static ssize_t \
227queue_store_##name(struct request_queue *q, const char *page, size_t count) \
228{ \
229 unsigned long val; \
230 ssize_t ret; \
231 ret = queue_var_store(&val, page, count); \
232 if (neg) \
233 val = !val; \
234 \
235 spin_lock_irq(q->queue_lock); \
236 if (val) \
237 queue_flag_set(QUEUE_FLAG_##flag, q); \
238 else \
239 queue_flag_clear(QUEUE_FLAG_##flag, q); \
240 spin_unlock_irq(q->queue_lock); \
241 return ret; \
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100242}
243
Jens Axboe956bcb72010-08-07 18:13:50 +0200244QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
245QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
246QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
247#undef QUEUE_SYSFS_BIT_FNS
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100248
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200249static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
250{
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100251 return queue_var_show((blk_queue_nomerges(q) << 1) |
252 blk_queue_noxmerges(q), page);
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200253}
254
255static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
256 size_t count)
257{
258 unsigned long nm;
259 ssize_t ret = queue_var_store(&nm, page, count);
260
Dave Reisnerb1f3b642012-09-08 11:55:45 -0400261 if (ret < 0)
262 return ret;
263
Jens Axboebf0f9702008-05-07 09:09:39 +0200264 spin_lock_irq(q->queue_lock);
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100265 queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
266 queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
267 if (nm == 2)
Jens Axboebf0f9702008-05-07 09:09:39 +0200268 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100269 else if (nm)
270 queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
Jens Axboebf0f9702008-05-07 09:09:39 +0200271 spin_unlock_irq(q->queue_lock);
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100272
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200273 return ret;
274}
275
Jens Axboec7c22e42008-09-13 20:26:01 +0200276static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
277{
Xiaotian Feng9cb308c2009-07-17 15:26:26 +0800278 bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
Dan Williams5757a6d2011-07-23 20:44:25 +0200279 bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
Jens Axboec7c22e42008-09-13 20:26:01 +0200280
Dan Williams5757a6d2011-07-23 20:44:25 +0200281 return queue_var_show(set << force, page);
Jens Axboec7c22e42008-09-13 20:26:01 +0200282}
283
284static ssize_t
285queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
286{
287 ssize_t ret = -EINVAL;
288#if defined(CONFIG_USE_GENERIC_SMP_HELPERS)
289 unsigned long val;
290
291 ret = queue_var_store(&val, page, count);
Dave Reisnerb1f3b642012-09-08 11:55:45 -0400292 if (ret < 0)
293 return ret;
294
Jens Axboec7c22e42008-09-13 20:26:01 +0200295 spin_lock_irq(q->queue_lock);
Eric Seppanene8037d42011-08-23 21:25:12 +0200296 if (val == 2) {
Jens Axboec7c22e42008-09-13 20:26:01 +0200297 queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
Eric Seppanene8037d42011-08-23 21:25:12 +0200298 queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
299 } else if (val == 1) {
300 queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
301 queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
302 } else if (val == 0) {
Dan Williams5757a6d2011-07-23 20:44:25 +0200303 queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
304 queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
305 }
Jens Axboec7c22e42008-09-13 20:26:01 +0200306 spin_unlock_irq(q->queue_lock);
307#endif
308 return ret;
309}
Jens Axboe8324aa92008-01-29 14:51:59 +0100310
311static struct queue_sysfs_entry queue_requests_entry = {
312 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
313 .show = queue_requests_show,
314 .store = queue_requests_store,
315};
316
317static struct queue_sysfs_entry queue_ra_entry = {
318 .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
319 .show = queue_ra_show,
320 .store = queue_ra_store,
321};
322
323static struct queue_sysfs_entry queue_max_sectors_entry = {
324 .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
325 .show = queue_max_sectors_show,
326 .store = queue_max_sectors_store,
327};
328
329static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
330 .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
331 .show = queue_max_hw_sectors_show,
332};
333
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500334static struct queue_sysfs_entry queue_max_segments_entry = {
335 .attr = {.name = "max_segments", .mode = S_IRUGO },
336 .show = queue_max_segments_show,
337};
338
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200339static struct queue_sysfs_entry queue_max_integrity_segments_entry = {
340 .attr = {.name = "max_integrity_segments", .mode = S_IRUGO },
341 .show = queue_max_integrity_segments_show,
342};
343
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500344static struct queue_sysfs_entry queue_max_segment_size_entry = {
345 .attr = {.name = "max_segment_size", .mode = S_IRUGO },
346 .show = queue_max_segment_size_show,
347};
348
Jens Axboe8324aa92008-01-29 14:51:59 +0100349static struct queue_sysfs_entry queue_iosched_entry = {
350 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
351 .show = elv_iosched_show,
352 .store = elv_iosched_store,
353};
354
Martin K. Petersene68b9032008-01-29 19:14:08 +0100355static struct queue_sysfs_entry queue_hw_sector_size_entry = {
356 .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
Martin K. Petersene1defc42009-05-22 17:17:49 -0400357 .show = queue_logical_block_size_show,
358};
359
360static struct queue_sysfs_entry queue_logical_block_size_entry = {
361 .attr = {.name = "logical_block_size", .mode = S_IRUGO },
362 .show = queue_logical_block_size_show,
Martin K. Petersene68b9032008-01-29 19:14:08 +0100363};
364
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400365static struct queue_sysfs_entry queue_physical_block_size_entry = {
366 .attr = {.name = "physical_block_size", .mode = S_IRUGO },
367 .show = queue_physical_block_size_show,
368};
369
370static struct queue_sysfs_entry queue_io_min_entry = {
371 .attr = {.name = "minimum_io_size", .mode = S_IRUGO },
372 .show = queue_io_min_show,
373};
374
375static struct queue_sysfs_entry queue_io_opt_entry = {
376 .attr = {.name = "optimal_io_size", .mode = S_IRUGO },
377 .show = queue_io_opt_show,
Jens Axboe8324aa92008-01-29 14:51:59 +0100378};
379
Martin K. Petersen86b37282009-11-10 11:50:21 +0100380static struct queue_sysfs_entry queue_discard_granularity_entry = {
381 .attr = {.name = "discard_granularity", .mode = S_IRUGO },
382 .show = queue_discard_granularity_show,
383};
384
385static struct queue_sysfs_entry queue_discard_max_entry = {
386 .attr = {.name = "discard_max_bytes", .mode = S_IRUGO },
387 .show = queue_discard_max_show,
388};
389
Martin K. Petersen98262f22009-12-03 09:24:48 +0100390static struct queue_sysfs_entry queue_discard_zeroes_data_entry = {
391 .attr = {.name = "discard_zeroes_data", .mode = S_IRUGO },
392 .show = queue_discard_zeroes_data_show,
393};
394
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400395static struct queue_sysfs_entry queue_write_same_max_entry = {
396 .attr = {.name = "write_same_max_bytes", .mode = S_IRUGO },
397 .show = queue_write_same_max_show,
398};
399
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100400static struct queue_sysfs_entry queue_nonrot_entry = {
401 .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
Jens Axboe956bcb72010-08-07 18:13:50 +0200402 .show = queue_show_nonrot,
403 .store = queue_store_nonrot,
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100404};
405
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200406static struct queue_sysfs_entry queue_nomerges_entry = {
407 .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
408 .show = queue_nomerges_show,
409 .store = queue_nomerges_store,
410};
411
Jens Axboec7c22e42008-09-13 20:26:01 +0200412static struct queue_sysfs_entry queue_rq_affinity_entry = {
413 .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
414 .show = queue_rq_affinity_show,
415 .store = queue_rq_affinity_store,
416};
417
Jens Axboebc58ba92009-01-23 10:54:44 +0100418static struct queue_sysfs_entry queue_iostats_entry = {
419 .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
Jens Axboe956bcb72010-08-07 18:13:50 +0200420 .show = queue_show_iostats,
421 .store = queue_store_iostats,
Jens Axboebc58ba92009-01-23 10:54:44 +0100422};
423
Jens Axboee2e1a142010-06-09 10:42:09 +0200424static struct queue_sysfs_entry queue_random_entry = {
425 .attr = {.name = "add_random", .mode = S_IRUGO | S_IWUSR },
Jens Axboe956bcb72010-08-07 18:13:50 +0200426 .show = queue_show_random,
427 .store = queue_store_random,
Jens Axboee2e1a142010-06-09 10:42:09 +0200428};
429
Jens Axboe8324aa92008-01-29 14:51:59 +0100430static struct attribute *default_attrs[] = {
431 &queue_requests_entry.attr,
432 &queue_ra_entry.attr,
433 &queue_max_hw_sectors_entry.attr,
434 &queue_max_sectors_entry.attr,
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500435 &queue_max_segments_entry.attr,
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200436 &queue_max_integrity_segments_entry.attr,
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500437 &queue_max_segment_size_entry.attr,
Jens Axboe8324aa92008-01-29 14:51:59 +0100438 &queue_iosched_entry.attr,
Martin K. Petersene68b9032008-01-29 19:14:08 +0100439 &queue_hw_sector_size_entry.attr,
Martin K. Petersene1defc42009-05-22 17:17:49 -0400440 &queue_logical_block_size_entry.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400441 &queue_physical_block_size_entry.attr,
442 &queue_io_min_entry.attr,
443 &queue_io_opt_entry.attr,
Martin K. Petersen86b37282009-11-10 11:50:21 +0100444 &queue_discard_granularity_entry.attr,
445 &queue_discard_max_entry.attr,
Martin K. Petersen98262f22009-12-03 09:24:48 +0100446 &queue_discard_zeroes_data_entry.attr,
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400447 &queue_write_same_max_entry.attr,
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100448 &queue_nonrot_entry.attr,
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200449 &queue_nomerges_entry.attr,
Jens Axboec7c22e42008-09-13 20:26:01 +0200450 &queue_rq_affinity_entry.attr,
Jens Axboebc58ba92009-01-23 10:54:44 +0100451 &queue_iostats_entry.attr,
Jens Axboee2e1a142010-06-09 10:42:09 +0200452 &queue_random_entry.attr,
Jens Axboe8324aa92008-01-29 14:51:59 +0100453 NULL,
454};
455
456#define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
457
458static ssize_t
459queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
460{
461 struct queue_sysfs_entry *entry = to_queue(attr);
462 struct request_queue *q =
463 container_of(kobj, struct request_queue, kobj);
464 ssize_t res;
465
466 if (!entry->show)
467 return -EIO;
468 mutex_lock(&q->sysfs_lock);
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100469 if (blk_queue_dying(q)) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100470 mutex_unlock(&q->sysfs_lock);
471 return -ENOENT;
472 }
473 res = entry->show(q, page);
474 mutex_unlock(&q->sysfs_lock);
475 return res;
476}
477
478static ssize_t
479queue_attr_store(struct kobject *kobj, struct attribute *attr,
480 const char *page, size_t length)
481{
482 struct queue_sysfs_entry *entry = to_queue(attr);
Jens Axboe6728cb02008-01-31 13:03:55 +0100483 struct request_queue *q;
Jens Axboe8324aa92008-01-29 14:51:59 +0100484 ssize_t res;
485
486 if (!entry->store)
487 return -EIO;
Jens Axboe6728cb02008-01-31 13:03:55 +0100488
489 q = container_of(kobj, struct request_queue, kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100490 mutex_lock(&q->sysfs_lock);
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100491 if (blk_queue_dying(q)) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100492 mutex_unlock(&q->sysfs_lock);
493 return -ENOENT;
494 }
495 res = entry->store(q, page, length);
496 mutex_unlock(&q->sysfs_lock);
497 return res;
498}
499
Tejun Heo548bc8e2013-01-09 08:05:13 -0800500static void blk_free_queue_rcu(struct rcu_head *rcu_head)
501{
502 struct request_queue *q = container_of(rcu_head, struct request_queue,
503 rcu_head);
504 kmem_cache_free(blk_requestq_cachep, q);
505}
506
Jens Axboe8324aa92008-01-29 14:51:59 +0100507/**
Andrew Morton499337b2011-09-21 10:01:22 +0200508 * blk_release_queue: - release a &struct request_queue when it is no longer needed
509 * @kobj: the kobj belonging to the request queue to be released
Jens Axboe8324aa92008-01-29 14:51:59 +0100510 *
511 * Description:
Andrew Morton499337b2011-09-21 10:01:22 +0200512 * blk_release_queue is the pair to blk_init_queue() or
Jens Axboe8324aa92008-01-29 14:51:59 +0100513 * blk_queue_make_request(). It should be called when a request queue is
514 * being released; typically when a block device is being de-registered.
515 * Currently, its primary task it to free all the &struct request
516 * structures that were allocated to the queue and the queue itself.
517 *
518 * Caveat:
519 * Hopefully the low level driver will have finished any
520 * outstanding requests first...
521 **/
522static void blk_release_queue(struct kobject *kobj)
523{
524 struct request_queue *q =
525 container_of(kobj, struct request_queue, kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100526
527 blk_sync_queue(q);
528
Tejun Heoe8989fa2012-03-05 13:15:20 -0800529 blkcg_exit_queue(q);
530
Tejun Heo7e5a8792011-12-14 00:33:42 +0100531 if (q->elevator) {
532 spin_lock_irq(q->queue_lock);
533 ioc_clear_queue(q);
534 spin_unlock_irq(q->queue_lock);
Hannes Reinecke777eb1b2011-09-28 08:07:01 -0600535 elevator_exit(q->elevator);
Tejun Heo7e5a8792011-12-14 00:33:42 +0100536 }
Hannes Reinecke777eb1b2011-09-28 08:07:01 -0600537
Tejun Heoa0516612012-06-26 15:05:44 -0700538 blk_exit_rl(&q->root_rl);
Jens Axboe8324aa92008-01-29 14:51:59 +0100539
540 if (q->queue_tags)
541 __blk_queue_free_tags(q);
542
543 blk_trace_shutdown(q);
544
545 bdi_destroy(&q->backing_dev_info);
Tejun Heoa73f7302011-12-14 00:33:37 +0100546
547 ida_simple_remove(&blk_queue_ida, q->id);
Tejun Heo548bc8e2013-01-09 08:05:13 -0800548 call_rcu(&q->rcu_head, blk_free_queue_rcu);
Jens Axboe8324aa92008-01-29 14:51:59 +0100549}
550
Emese Revfy52cf25d2010-01-19 02:58:23 +0100551static const struct sysfs_ops queue_sysfs_ops = {
Jens Axboe8324aa92008-01-29 14:51:59 +0100552 .show = queue_attr_show,
553 .store = queue_attr_store,
554};
555
556struct kobj_type blk_queue_ktype = {
557 .sysfs_ops = &queue_sysfs_ops,
558 .default_attrs = default_attrs,
559 .release = blk_release_queue,
560};
561
562int blk_register_queue(struct gendisk *disk)
563{
564 int ret;
Li Zefan1d54ad62009-04-14 14:00:05 +0800565 struct device *dev = disk_to_dev(disk);
Jens Axboe8324aa92008-01-29 14:51:59 +0100566 struct request_queue *q = disk->queue;
567
Akinobu Mitafb199742008-04-21 09:51:06 +0200568 if (WARN_ON(!q))
Jens Axboe8324aa92008-01-29 14:51:59 +0100569 return -ENXIO;
570
Tejun Heo749fefe2012-09-20 14:08:52 -0700571 /*
572 * Initialization must be complete by now. Finish the initial
573 * bypass from queue allocation.
574 */
575 blk_queue_bypass_end(q);
576
Li Zefan1d54ad62009-04-14 14:00:05 +0800577 ret = blk_trace_init_sysfs(dev);
578 if (ret)
579 return ret;
580
Linus Torvaldsc9059592009-06-11 10:52:27 -0700581 ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
Liu Yuaned5302d2011-04-19 13:47:58 +0200582 if (ret < 0) {
583 blk_trace_remove_sysfs(dev);
Jens Axboe8324aa92008-01-29 14:51:59 +0100584 return ret;
Liu Yuaned5302d2011-04-19 13:47:58 +0200585 }
Jens Axboe8324aa92008-01-29 14:51:59 +0100586
587 kobject_uevent(&q->kobj, KOBJ_ADD);
588
Martin K. Petersencd43e262009-05-22 17:17:52 -0400589 if (!q->request_fn)
590 return 0;
591
Jens Axboe8324aa92008-01-29 14:51:59 +0100592 ret = elv_register_queue(q);
593 if (ret) {
594 kobject_uevent(&q->kobj, KOBJ_REMOVE);
595 kobject_del(&q->kobj);
Liu Yuan80656b62011-04-13 22:14:54 +0200596 blk_trace_remove_sysfs(dev);
Xiaotian Fengc87ffbb2010-08-23 12:30:29 +0200597 kobject_put(&dev->kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100598 return ret;
599 }
600
601 return 0;
602}
603
604void blk_unregister_queue(struct gendisk *disk)
605{
606 struct request_queue *q = disk->queue;
607
Akinobu Mitafb199742008-04-21 09:51:06 +0200608 if (WARN_ON(!q))
609 return;
610
Zdenek Kabelac48c0d4d2009-09-25 06:19:26 +0200611 if (q->request_fn)
Jens Axboe8324aa92008-01-29 14:51:59 +0100612 elv_unregister_queue(q);
613
Zdenek Kabelac48c0d4d2009-09-25 06:19:26 +0200614 kobject_uevent(&q->kobj, KOBJ_REMOVE);
615 kobject_del(&q->kobj);
616 blk_trace_remove_sysfs(disk_to_dev(disk));
617 kobject_put(&disk_to_dev(disk)->kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100618}