]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - block/blk-sysfs.c
block: Add 'zoned' queue limit
[mirror_ubuntu-zesty-kernel.git] / block / blk-sysfs.c
CommitLineData
8324aa91
JA
1/*
2 * Functions related to sysfs handling
3 */
4#include <linux/kernel.h>
5a0e3ad6 5#include <linux/slab.h>
8324aa91
JA
6#include <linux/module.h>
7#include <linux/bio.h>
8#include <linux/blkdev.h>
66114cad 9#include <linux/backing-dev.h>
8324aa91 10#include <linux/blktrace_api.h>
320ae51f 11#include <linux/blk-mq.h>
eea8f41c 12#include <linux/blk-cgroup.h>
8324aa91
JA
13
14#include "blk.h"
3edcc0ce 15#include "blk-mq.h"
8324aa91
JA
16
17struct queue_sysfs_entry {
18 struct attribute attr;
19 ssize_t (*show)(struct request_queue *, char *);
20 ssize_t (*store)(struct request_queue *, const char *, size_t);
21};
22
23static ssize_t
9cb308ce 24queue_var_show(unsigned long var, char *page)
8324aa91 25{
9cb308ce 26 return sprintf(page, "%lu\n", var);
8324aa91
JA
27}
28
29static ssize_t
30queue_var_store(unsigned long *var, const char *page, size_t count)
31{
b1f3b64d
DR
32 int err;
33 unsigned long v;
34
ed751e68 35 err = kstrtoul(page, 10, &v);
b1f3b64d
DR
36 if (err || v > UINT_MAX)
37 return -EINVAL;
38
39 *var = v;
8324aa91 40
8324aa91
JA
41 return count;
42}
43
44static ssize_t queue_requests_show(struct request_queue *q, char *page)
45{
46 return queue_var_show(q->nr_requests, (page));
47}
48
49static ssize_t
50queue_requests_store(struct request_queue *q, const char *page, size_t count)
51{
8324aa91 52 unsigned long nr;
e3a2b3f9 53 int ret, err;
b8a9ae77 54
e3a2b3f9 55 if (!q->request_fn && !q->mq_ops)
b8a9ae77
JA
56 return -EINVAL;
57
58 ret = queue_var_store(&nr, page, count);
b1f3b64d
DR
59 if (ret < 0)
60 return ret;
61
8324aa91
JA
62 if (nr < BLKDEV_MIN_RQ)
63 nr = BLKDEV_MIN_RQ;
64
e3a2b3f9
JA
65 if (q->request_fn)
66 err = blk_update_nr_requests(q, nr);
67 else
68 err = blk_mq_update_nr_requests(q, nr);
69
70 if (err)
71 return err;
72
8324aa91
JA
73 return ret;
74}
75
76static ssize_t queue_ra_show(struct request_queue *q, char *page)
77{
9cb308ce 78 unsigned long ra_kb = q->backing_dev_info.ra_pages <<
09cbfeaf 79 (PAGE_SHIFT - 10);
8324aa91
JA
80
81 return queue_var_show(ra_kb, (page));
82}
83
84static ssize_t
85queue_ra_store(struct request_queue *q, const char *page, size_t count)
86{
87 unsigned long ra_kb;
88 ssize_t ret = queue_var_store(&ra_kb, page, count);
89
b1f3b64d
DR
90 if (ret < 0)
91 return ret;
92
09cbfeaf 93 q->backing_dev_info.ra_pages = ra_kb >> (PAGE_SHIFT - 10);
8324aa91
JA
94
95 return ret;
96}
97
98static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
99{
ae03bf63 100 int max_sectors_kb = queue_max_sectors(q) >> 1;
8324aa91
JA
101
102 return queue_var_show(max_sectors_kb, (page));
103}
104
c77a5710
MP
105static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
106{
107 return queue_var_show(queue_max_segments(q), (page));
108}
109
13f05c8d
MP
110static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
111{
112 return queue_var_show(q->limits.max_integrity_segments, (page));
113}
114
c77a5710
MP
115static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
116{
e692cb66 117 if (blk_queue_cluster(q))
c77a5710
MP
118 return queue_var_show(queue_max_segment_size(q), (page));
119
09cbfeaf 120 return queue_var_show(PAGE_SIZE, (page));
c77a5710
MP
121}
122
e1defc4f 123static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
e68b903c 124{
e1defc4f 125 return queue_var_show(queue_logical_block_size(q), page);
e68b903c
MP
126}
127
c72758f3
MP
128static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
129{
130 return queue_var_show(queue_physical_block_size(q), page);
131}
132
133static ssize_t queue_io_min_show(struct request_queue *q, char *page)
134{
135 return queue_var_show(queue_io_min(q), page);
136}
137
138static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
139{
140 return queue_var_show(queue_io_opt(q), page);
e68b903c
MP
141}
142
86b37281
MP
143static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
144{
145 return queue_var_show(q->limits.discard_granularity, page);
146}
147
0034af03
JA
148static ssize_t queue_discard_max_hw_show(struct request_queue *q, char *page)
149{
0034af03 150
18f922d0
A
151 return sprintf(page, "%llu\n",
152 (unsigned long long)q->limits.max_hw_discard_sectors << 9);
0034af03
JA
153}
154
86b37281
MP
155static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
156{
a934a00a
MP
157 return sprintf(page, "%llu\n",
158 (unsigned long long)q->limits.max_discard_sectors << 9);
86b37281
MP
159}
160
0034af03
JA
161static ssize_t queue_discard_max_store(struct request_queue *q,
162 const char *page, size_t count)
163{
164 unsigned long max_discard;
165 ssize_t ret = queue_var_store(&max_discard, page, count);
166
167 if (ret < 0)
168 return ret;
169
170 if (max_discard & (q->limits.discard_granularity - 1))
171 return -EINVAL;
172
173 max_discard >>= 9;
174 if (max_discard > UINT_MAX)
175 return -EINVAL;
176
177 if (max_discard > q->limits.max_hw_discard_sectors)
178 max_discard = q->limits.max_hw_discard_sectors;
179
180 q->limits.max_discard_sectors = max_discard;
181 return ret;
182}
183
98262f27
MP
184static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
185{
186 return queue_var_show(queue_discard_zeroes_data(q), page);
187}
188
4363ac7c
MP
189static ssize_t queue_write_same_max_show(struct request_queue *q, char *page)
190{
191 return sprintf(page, "%llu\n",
192 (unsigned long long)q->limits.max_write_same_sectors << 9);
193}
194
195
8324aa91
JA
196static ssize_t
197queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
198{
199 unsigned long max_sectors_kb,
ae03bf63 200 max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
09cbfeaf 201 page_kb = 1 << (PAGE_SHIFT - 10);
8324aa91
JA
202 ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
203
b1f3b64d
DR
204 if (ret < 0)
205 return ret;
206
ca369d51
MP
207 max_hw_sectors_kb = min_not_zero(max_hw_sectors_kb, (unsigned long)
208 q->limits.max_dev_sectors >> 1);
209
8324aa91
JA
210 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
211 return -EINVAL;
7c239517 212
8324aa91 213 spin_lock_irq(q->queue_lock);
c295fc05 214 q->limits.max_sectors = max_sectors_kb << 1;
8324aa91
JA
215 spin_unlock_irq(q->queue_lock);
216
217 return ret;
218}
219
220static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
221{
ae03bf63 222 int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
8324aa91
JA
223
224 return queue_var_show(max_hw_sectors_kb, (page));
225}
226
956bcb7c
JA
227#define QUEUE_SYSFS_BIT_FNS(name, flag, neg) \
228static ssize_t \
229queue_show_##name(struct request_queue *q, char *page) \
230{ \
231 int bit; \
232 bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags); \
233 return queue_var_show(neg ? !bit : bit, page); \
234} \
235static ssize_t \
236queue_store_##name(struct request_queue *q, const char *page, size_t count) \
237{ \
238 unsigned long val; \
239 ssize_t ret; \
240 ret = queue_var_store(&val, page, count); \
c678ef52
AB
241 if (ret < 0) \
242 return ret; \
956bcb7c
JA
243 if (neg) \
244 val = !val; \
245 \
246 spin_lock_irq(q->queue_lock); \
247 if (val) \
248 queue_flag_set(QUEUE_FLAG_##flag, q); \
249 else \
250 queue_flag_clear(QUEUE_FLAG_##flag, q); \
251 spin_unlock_irq(q->queue_lock); \
252 return ret; \
1308835f
BZ
253}
254
956bcb7c
JA
255QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
256QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
257QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
258#undef QUEUE_SYSFS_BIT_FNS
1308835f 259
797476b8
DLM
260static ssize_t queue_zoned_show(struct request_queue *q, char *page)
261{
262 switch (blk_queue_zoned_model(q)) {
263 case BLK_ZONED_HA:
264 return sprintf(page, "host-aware\n");
265 case BLK_ZONED_HM:
266 return sprintf(page, "host-managed\n");
267 default:
268 return sprintf(page, "none\n");
269 }
270}
271
ac9fafa1
AB
272static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
273{
488991e2
AB
274 return queue_var_show((blk_queue_nomerges(q) << 1) |
275 blk_queue_noxmerges(q), page);
ac9fafa1
AB
276}
277
278static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
279 size_t count)
280{
281 unsigned long nm;
282 ssize_t ret = queue_var_store(&nm, page, count);
283
b1f3b64d
DR
284 if (ret < 0)
285 return ret;
286
bf0f9702 287 spin_lock_irq(q->queue_lock);
488991e2
AB
288 queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
289 queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
290 if (nm == 2)
bf0f9702 291 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
488991e2
AB
292 else if (nm)
293 queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
bf0f9702 294 spin_unlock_irq(q->queue_lock);
1308835f 295
ac9fafa1
AB
296 return ret;
297}
298
c7c22e4d
JA
299static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
300{
9cb308ce 301 bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
5757a6d7 302 bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
c7c22e4d 303
5757a6d7 304 return queue_var_show(set << force, page);
c7c22e4d
JA
305}
306
307static ssize_t
308queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
309{
310 ssize_t ret = -EINVAL;
0a06ff06 311#ifdef CONFIG_SMP
c7c22e4d
JA
312 unsigned long val;
313
314 ret = queue_var_store(&val, page, count);
b1f3b64d
DR
315 if (ret < 0)
316 return ret;
317
c7c22e4d 318 spin_lock_irq(q->queue_lock);
e8037d49 319 if (val == 2) {
c7c22e4d 320 queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
e8037d49
ES
321 queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
322 } else if (val == 1) {
323 queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
324 queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
325 } else if (val == 0) {
5757a6d7
DW
326 queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
327 queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
328 }
c7c22e4d
JA
329 spin_unlock_irq(q->queue_lock);
330#endif
331 return ret;
332}
8324aa91 333
05229bee
JA
334static ssize_t queue_poll_show(struct request_queue *q, char *page)
335{
336 return queue_var_show(test_bit(QUEUE_FLAG_POLL, &q->queue_flags), page);
337}
338
339static ssize_t queue_poll_store(struct request_queue *q, const char *page,
340 size_t count)
341{
342 unsigned long poll_on;
343 ssize_t ret;
344
345 if (!q->mq_ops || !q->mq_ops->poll)
346 return -EINVAL;
347
348 ret = queue_var_store(&poll_on, page, count);
349 if (ret < 0)
350 return ret;
351
352 spin_lock_irq(q->queue_lock);
353 if (poll_on)
354 queue_flag_set(QUEUE_FLAG_POLL, q);
355 else
356 queue_flag_clear(QUEUE_FLAG_POLL, q);
357 spin_unlock_irq(q->queue_lock);
358
359 return ret;
360}
361
93e9d8e8
JA
362static ssize_t queue_wc_show(struct request_queue *q, char *page)
363{
364 if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
365 return sprintf(page, "write back\n");
366
367 return sprintf(page, "write through\n");
368}
369
370static ssize_t queue_wc_store(struct request_queue *q, const char *page,
371 size_t count)
372{
373 int set = -1;
374
375 if (!strncmp(page, "write back", 10))
376 set = 1;
377 else if (!strncmp(page, "write through", 13) ||
378 !strncmp(page, "none", 4))
379 set = 0;
380
381 if (set == -1)
382 return -EINVAL;
383
384 spin_lock_irq(q->queue_lock);
385 if (set)
386 queue_flag_set(QUEUE_FLAG_WC, q);
387 else
388 queue_flag_clear(QUEUE_FLAG_WC, q);
389 spin_unlock_irq(q->queue_lock);
390
391 return count;
392}
393
ea6ca600
YK
394static ssize_t queue_dax_show(struct request_queue *q, char *page)
395{
396 return queue_var_show(blk_queue_dax(q), page);
397}
398
8324aa91
JA
399static struct queue_sysfs_entry queue_requests_entry = {
400 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
401 .show = queue_requests_show,
402 .store = queue_requests_store,
403};
404
405static struct queue_sysfs_entry queue_ra_entry = {
406 .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
407 .show = queue_ra_show,
408 .store = queue_ra_store,
409};
410
411static struct queue_sysfs_entry queue_max_sectors_entry = {
412 .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
413 .show = queue_max_sectors_show,
414 .store = queue_max_sectors_store,
415};
416
417static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
418 .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
419 .show = queue_max_hw_sectors_show,
420};
421
c77a5710
MP
422static struct queue_sysfs_entry queue_max_segments_entry = {
423 .attr = {.name = "max_segments", .mode = S_IRUGO },
424 .show = queue_max_segments_show,
425};
426
13f05c8d
MP
427static struct queue_sysfs_entry queue_max_integrity_segments_entry = {
428 .attr = {.name = "max_integrity_segments", .mode = S_IRUGO },
429 .show = queue_max_integrity_segments_show,
430};
431
c77a5710
MP
432static struct queue_sysfs_entry queue_max_segment_size_entry = {
433 .attr = {.name = "max_segment_size", .mode = S_IRUGO },
434 .show = queue_max_segment_size_show,
435};
436
8324aa91
JA
437static struct queue_sysfs_entry queue_iosched_entry = {
438 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
439 .show = elv_iosched_show,
440 .store = elv_iosched_store,
441};
442
e68b903c
MP
443static struct queue_sysfs_entry queue_hw_sector_size_entry = {
444 .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
e1defc4f
MP
445 .show = queue_logical_block_size_show,
446};
447
448static struct queue_sysfs_entry queue_logical_block_size_entry = {
449 .attr = {.name = "logical_block_size", .mode = S_IRUGO },
450 .show = queue_logical_block_size_show,
e68b903c
MP
451};
452
c72758f3
MP
453static struct queue_sysfs_entry queue_physical_block_size_entry = {
454 .attr = {.name = "physical_block_size", .mode = S_IRUGO },
455 .show = queue_physical_block_size_show,
456};
457
458static struct queue_sysfs_entry queue_io_min_entry = {
459 .attr = {.name = "minimum_io_size", .mode = S_IRUGO },
460 .show = queue_io_min_show,
461};
462
463static struct queue_sysfs_entry queue_io_opt_entry = {
464 .attr = {.name = "optimal_io_size", .mode = S_IRUGO },
465 .show = queue_io_opt_show,
e68b903c
MP
466};
467
86b37281
MP
468static struct queue_sysfs_entry queue_discard_granularity_entry = {
469 .attr = {.name = "discard_granularity", .mode = S_IRUGO },
470 .show = queue_discard_granularity_show,
471};
472
0034af03
JA
473static struct queue_sysfs_entry queue_discard_max_hw_entry = {
474 .attr = {.name = "discard_max_hw_bytes", .mode = S_IRUGO },
475 .show = queue_discard_max_hw_show,
476};
477
86b37281 478static struct queue_sysfs_entry queue_discard_max_entry = {
0034af03 479 .attr = {.name = "discard_max_bytes", .mode = S_IRUGO | S_IWUSR },
86b37281 480 .show = queue_discard_max_show,
0034af03 481 .store = queue_discard_max_store,
86b37281
MP
482};
483
98262f27
MP
484static struct queue_sysfs_entry queue_discard_zeroes_data_entry = {
485 .attr = {.name = "discard_zeroes_data", .mode = S_IRUGO },
486 .show = queue_discard_zeroes_data_show,
487};
488
4363ac7c
MP
489static struct queue_sysfs_entry queue_write_same_max_entry = {
490 .attr = {.name = "write_same_max_bytes", .mode = S_IRUGO },
491 .show = queue_write_same_max_show,
492};
493
1308835f
BZ
494static struct queue_sysfs_entry queue_nonrot_entry = {
495 .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
956bcb7c
JA
496 .show = queue_show_nonrot,
497 .store = queue_store_nonrot,
1308835f
BZ
498};
499
797476b8
DLM
500static struct queue_sysfs_entry queue_zoned_entry = {
501 .attr = {.name = "zoned", .mode = S_IRUGO },
502 .show = queue_zoned_show,
503};
504
ac9fafa1
AB
505static struct queue_sysfs_entry queue_nomerges_entry = {
506 .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
507 .show = queue_nomerges_show,
508 .store = queue_nomerges_store,
509};
510
c7c22e4d
JA
511static struct queue_sysfs_entry queue_rq_affinity_entry = {
512 .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
513 .show = queue_rq_affinity_show,
514 .store = queue_rq_affinity_store,
515};
516
bc58ba94
JA
517static struct queue_sysfs_entry queue_iostats_entry = {
518 .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
956bcb7c
JA
519 .show = queue_show_iostats,
520 .store = queue_store_iostats,
bc58ba94
JA
521};
522
e2e1a148
JA
523static struct queue_sysfs_entry queue_random_entry = {
524 .attr = {.name = "add_random", .mode = S_IRUGO | S_IWUSR },
956bcb7c
JA
525 .show = queue_show_random,
526 .store = queue_store_random,
e2e1a148
JA
527};
528
05229bee
JA
529static struct queue_sysfs_entry queue_poll_entry = {
530 .attr = {.name = "io_poll", .mode = S_IRUGO | S_IWUSR },
531 .show = queue_poll_show,
532 .store = queue_poll_store,
533};
534
93e9d8e8
JA
535static struct queue_sysfs_entry queue_wc_entry = {
536 .attr = {.name = "write_cache", .mode = S_IRUGO | S_IWUSR },
537 .show = queue_wc_show,
538 .store = queue_wc_store,
539};
540
ea6ca600
YK
541static struct queue_sysfs_entry queue_dax_entry = {
542 .attr = {.name = "dax", .mode = S_IRUGO },
543 .show = queue_dax_show,
544};
545
8324aa91
JA
546static struct attribute *default_attrs[] = {
547 &queue_requests_entry.attr,
548 &queue_ra_entry.attr,
549 &queue_max_hw_sectors_entry.attr,
550 &queue_max_sectors_entry.attr,
c77a5710 551 &queue_max_segments_entry.attr,
13f05c8d 552 &queue_max_integrity_segments_entry.attr,
c77a5710 553 &queue_max_segment_size_entry.attr,
8324aa91 554 &queue_iosched_entry.attr,
e68b903c 555 &queue_hw_sector_size_entry.attr,
e1defc4f 556 &queue_logical_block_size_entry.attr,
c72758f3
MP
557 &queue_physical_block_size_entry.attr,
558 &queue_io_min_entry.attr,
559 &queue_io_opt_entry.attr,
86b37281
MP
560 &queue_discard_granularity_entry.attr,
561 &queue_discard_max_entry.attr,
0034af03 562 &queue_discard_max_hw_entry.attr,
98262f27 563 &queue_discard_zeroes_data_entry.attr,
4363ac7c 564 &queue_write_same_max_entry.attr,
1308835f 565 &queue_nonrot_entry.attr,
797476b8 566 &queue_zoned_entry.attr,
ac9fafa1 567 &queue_nomerges_entry.attr,
c7c22e4d 568 &queue_rq_affinity_entry.attr,
bc58ba94 569 &queue_iostats_entry.attr,
e2e1a148 570 &queue_random_entry.attr,
05229bee 571 &queue_poll_entry.attr,
93e9d8e8 572 &queue_wc_entry.attr,
ea6ca600 573 &queue_dax_entry.attr,
8324aa91
JA
574 NULL,
575};
576
577#define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
578
579static ssize_t
580queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
581{
582 struct queue_sysfs_entry *entry = to_queue(attr);
583 struct request_queue *q =
584 container_of(kobj, struct request_queue, kobj);
585 ssize_t res;
586
587 if (!entry->show)
588 return -EIO;
589 mutex_lock(&q->sysfs_lock);
3f3299d5 590 if (blk_queue_dying(q)) {
8324aa91
JA
591 mutex_unlock(&q->sysfs_lock);
592 return -ENOENT;
593 }
594 res = entry->show(q, page);
595 mutex_unlock(&q->sysfs_lock);
596 return res;
597}
598
599static ssize_t
600queue_attr_store(struct kobject *kobj, struct attribute *attr,
601 const char *page, size_t length)
602{
603 struct queue_sysfs_entry *entry = to_queue(attr);
6728cb0e 604 struct request_queue *q;
8324aa91
JA
605 ssize_t res;
606
607 if (!entry->store)
608 return -EIO;
6728cb0e
JA
609
610 q = container_of(kobj, struct request_queue, kobj);
8324aa91 611 mutex_lock(&q->sysfs_lock);
3f3299d5 612 if (blk_queue_dying(q)) {
8324aa91
JA
613 mutex_unlock(&q->sysfs_lock);
614 return -ENOENT;
615 }
616 res = entry->store(q, page, length);
617 mutex_unlock(&q->sysfs_lock);
618 return res;
619}
620
548bc8e1
TH
621static void blk_free_queue_rcu(struct rcu_head *rcu_head)
622{
623 struct request_queue *q = container_of(rcu_head, struct request_queue,
624 rcu_head);
625 kmem_cache_free(blk_requestq_cachep, q);
626}
627
8324aa91 628/**
499337bb
AM
629 * blk_release_queue: - release a &struct request_queue when it is no longer needed
630 * @kobj: the kobj belonging to the request queue to be released
8324aa91
JA
631 *
632 * Description:
499337bb 633 * blk_release_queue is the pair to blk_init_queue() or
8324aa91
JA
634 * blk_queue_make_request(). It should be called when a request queue is
635 * being released; typically when a block device is being de-registered.
636 * Currently, its primary task it to free all the &struct request
637 * structures that were allocated to the queue and the queue itself.
638 *
45a9c9d9
BVA
639 * Note:
640 * The low level driver must have finished any outstanding requests first
641 * via blk_cleanup_queue().
8324aa91
JA
642 **/
643static void blk_release_queue(struct kobject *kobj)
644{
645 struct request_queue *q =
646 container_of(kobj, struct request_queue, kobj);
8324aa91 647
b02176f3 648 bdi_exit(&q->backing_dev_info);
e8989fae
TH
649 blkcg_exit_queue(q);
650
7e5a8794
TH
651 if (q->elevator) {
652 spin_lock_irq(q->queue_lock);
653 ioc_clear_queue(q);
654 spin_unlock_irq(q->queue_lock);
777eb1bf 655 elevator_exit(q->elevator);
7e5a8794 656 }
777eb1bf 657
a051661c 658 blk_exit_rl(&q->root_rl);
8324aa91
JA
659
660 if (q->queue_tags)
661 __blk_queue_free_tags(q);
662
45a9c9d9 663 if (!q->mq_ops)
f70ced09 664 blk_free_flush_queue(q->fq);
e09aae7e
ML
665 else
666 blk_mq_release(q);
18741986 667
8324aa91
JA
668 blk_trace_shutdown(q);
669
54efd50b
KO
670 if (q->bio_split)
671 bioset_free(q->bio_split);
672
a73f730d 673 ida_simple_remove(&blk_queue_ida, q->id);
548bc8e1 674 call_rcu(&q->rcu_head, blk_free_queue_rcu);
8324aa91
JA
675}
676
52cf25d0 677static const struct sysfs_ops queue_sysfs_ops = {
8324aa91
JA
678 .show = queue_attr_show,
679 .store = queue_attr_store,
680};
681
682struct kobj_type blk_queue_ktype = {
683 .sysfs_ops = &queue_sysfs_ops,
684 .default_attrs = default_attrs,
685 .release = blk_release_queue,
686};
687
688int blk_register_queue(struct gendisk *disk)
689{
690 int ret;
1d54ad6d 691 struct device *dev = disk_to_dev(disk);
8324aa91
JA
692 struct request_queue *q = disk->queue;
693
fb199746 694 if (WARN_ON(!q))
8324aa91
JA
695 return -ENXIO;
696
749fefe6 697 /*
17497acb
TH
698 * SCSI probing may synchronously create and destroy a lot of
699 * request_queues for non-existent devices. Shutting down a fully
700 * functional queue takes measureable wallclock time as RCU grace
701 * periods are involved. To avoid excessive latency in these
702 * cases, a request_queue starts out in a degraded mode which is
703 * faster to shut down and is made fully functional here as
704 * request_queues for non-existent devices never get registered.
749fefe6 705 */
df35c7c9
AS
706 if (!blk_queue_init_done(q)) {
707 queue_flag_set_unlocked(QUEUE_FLAG_INIT_DONE, q);
3ef28e83 708 percpu_ref_switch_to_percpu(&q->q_usage_counter);
df35c7c9
AS
709 blk_queue_bypass_end(q);
710 }
749fefe6 711
1d54ad6d
LZ
712 ret = blk_trace_init_sysfs(dev);
713 if (ret)
714 return ret;
715
c9059598 716 ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
ed5302d3
LY
717 if (ret < 0) {
718 blk_trace_remove_sysfs(dev);
8324aa91 719 return ret;
ed5302d3 720 }
8324aa91
JA
721
722 kobject_uevent(&q->kobj, KOBJ_ADD);
723
320ae51f 724 if (q->mq_ops)
b21d5b30 725 blk_mq_register_dev(dev, q);
320ae51f 726
cd43e26f
MP
727 if (!q->request_fn)
728 return 0;
729
8324aa91
JA
730 ret = elv_register_queue(q);
731 if (ret) {
732 kobject_uevent(&q->kobj, KOBJ_REMOVE);
733 kobject_del(&q->kobj);
80656b67 734 blk_trace_remove_sysfs(dev);
c87ffbb8 735 kobject_put(&dev->kobj);
8324aa91
JA
736 return ret;
737 }
738
739 return 0;
740}
741
742void blk_unregister_queue(struct gendisk *disk)
743{
744 struct request_queue *q = disk->queue;
745
fb199746
AM
746 if (WARN_ON(!q))
747 return;
748
320ae51f 749 if (q->mq_ops)
b21d5b30 750 blk_mq_unregister_dev(disk_to_dev(disk), q);
320ae51f 751
48c0d4d4 752 if (q->request_fn)
8324aa91
JA
753 elv_unregister_queue(q);
754
48c0d4d4
ZK
755 kobject_uevent(&q->kobj, KOBJ_REMOVE);
756 kobject_del(&q->kobj);
757 blk_trace_remove_sysfs(disk_to_dev(disk));
758 kobject_put(&disk_to_dev(disk)->kobj);
8324aa91 759}