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