]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - block/blk-sysfs.c
block: add scalable completion tracking of requests
[mirror_ubuntu-artful-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
cf43e6be
JA
404static ssize_t print_stat(char *page, struct blk_rq_stat *stat, const char *pre)
405{
406 return sprintf(page, "%s samples=%llu, mean=%lld, min=%lld, max=%lld\n",
407 pre, (long long) stat->nr_samples,
408 (long long) stat->mean, (long long) stat->min,
409 (long long) stat->max);
410}
411
412static ssize_t queue_stats_show(struct request_queue *q, char *page)
413{
414 struct blk_rq_stat stat[2];
415 ssize_t ret;
416
417 blk_queue_stat_get(q, stat);
418
419 ret = print_stat(page, &stat[BLK_STAT_READ], "read :");
420 ret += print_stat(page + ret, &stat[BLK_STAT_WRITE], "write:");
421 return ret;
422}
423
8324aa91
JA
424static struct queue_sysfs_entry queue_requests_entry = {
425 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
426 .show = queue_requests_show,
427 .store = queue_requests_store,
428};
429
430static struct queue_sysfs_entry queue_ra_entry = {
431 .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
432 .show = queue_ra_show,
433 .store = queue_ra_store,
434};
435
436static struct queue_sysfs_entry queue_max_sectors_entry = {
437 .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
438 .show = queue_max_sectors_show,
439 .store = queue_max_sectors_store,
440};
441
442static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
443 .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
444 .show = queue_max_hw_sectors_show,
445};
446
c77a5710
MP
447static struct queue_sysfs_entry queue_max_segments_entry = {
448 .attr = {.name = "max_segments", .mode = S_IRUGO },
449 .show = queue_max_segments_show,
450};
451
13f05c8d
MP
452static struct queue_sysfs_entry queue_max_integrity_segments_entry = {
453 .attr = {.name = "max_integrity_segments", .mode = S_IRUGO },
454 .show = queue_max_integrity_segments_show,
455};
456
c77a5710
MP
457static struct queue_sysfs_entry queue_max_segment_size_entry = {
458 .attr = {.name = "max_segment_size", .mode = S_IRUGO },
459 .show = queue_max_segment_size_show,
460};
461
8324aa91
JA
462static struct queue_sysfs_entry queue_iosched_entry = {
463 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
464 .show = elv_iosched_show,
465 .store = elv_iosched_store,
466};
467
e68b903c
MP
468static struct queue_sysfs_entry queue_hw_sector_size_entry = {
469 .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
e1defc4f
MP
470 .show = queue_logical_block_size_show,
471};
472
473static struct queue_sysfs_entry queue_logical_block_size_entry = {
474 .attr = {.name = "logical_block_size", .mode = S_IRUGO },
475 .show = queue_logical_block_size_show,
e68b903c
MP
476};
477
c72758f3
MP
478static struct queue_sysfs_entry queue_physical_block_size_entry = {
479 .attr = {.name = "physical_block_size", .mode = S_IRUGO },
480 .show = queue_physical_block_size_show,
481};
482
87caf97c
HR
483static struct queue_sysfs_entry queue_chunk_sectors_entry = {
484 .attr = {.name = "chunk_sectors", .mode = S_IRUGO },
485 .show = queue_chunk_sectors_show,
486};
487
c72758f3
MP
488static struct queue_sysfs_entry queue_io_min_entry = {
489 .attr = {.name = "minimum_io_size", .mode = S_IRUGO },
490 .show = queue_io_min_show,
491};
492
493static struct queue_sysfs_entry queue_io_opt_entry = {
494 .attr = {.name = "optimal_io_size", .mode = S_IRUGO },
495 .show = queue_io_opt_show,
e68b903c
MP
496};
497
86b37281
MP
498static struct queue_sysfs_entry queue_discard_granularity_entry = {
499 .attr = {.name = "discard_granularity", .mode = S_IRUGO },
500 .show = queue_discard_granularity_show,
501};
502
0034af03
JA
503static struct queue_sysfs_entry queue_discard_max_hw_entry = {
504 .attr = {.name = "discard_max_hw_bytes", .mode = S_IRUGO },
505 .show = queue_discard_max_hw_show,
506};
507
86b37281 508static struct queue_sysfs_entry queue_discard_max_entry = {
0034af03 509 .attr = {.name = "discard_max_bytes", .mode = S_IRUGO | S_IWUSR },
86b37281 510 .show = queue_discard_max_show,
0034af03 511 .store = queue_discard_max_store,
86b37281
MP
512};
513
98262f27
MP
514static struct queue_sysfs_entry queue_discard_zeroes_data_entry = {
515 .attr = {.name = "discard_zeroes_data", .mode = S_IRUGO },
516 .show = queue_discard_zeroes_data_show,
517};
518
4363ac7c
MP
519static struct queue_sysfs_entry queue_write_same_max_entry = {
520 .attr = {.name = "write_same_max_bytes", .mode = S_IRUGO },
521 .show = queue_write_same_max_show,
522};
523
1308835f
BZ
524static struct queue_sysfs_entry queue_nonrot_entry = {
525 .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
956bcb7c
JA
526 .show = queue_show_nonrot,
527 .store = queue_store_nonrot,
1308835f
BZ
528};
529
797476b8
DLM
530static struct queue_sysfs_entry queue_zoned_entry = {
531 .attr = {.name = "zoned", .mode = S_IRUGO },
532 .show = queue_zoned_show,
533};
534
ac9fafa1
AB
535static struct queue_sysfs_entry queue_nomerges_entry = {
536 .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
537 .show = queue_nomerges_show,
538 .store = queue_nomerges_store,
539};
540
c7c22e4d
JA
541static struct queue_sysfs_entry queue_rq_affinity_entry = {
542 .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
543 .show = queue_rq_affinity_show,
544 .store = queue_rq_affinity_store,
545};
546
bc58ba94
JA
547static struct queue_sysfs_entry queue_iostats_entry = {
548 .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
956bcb7c
JA
549 .show = queue_show_iostats,
550 .store = queue_store_iostats,
bc58ba94
JA
551};
552
e2e1a148
JA
553static struct queue_sysfs_entry queue_random_entry = {
554 .attr = {.name = "add_random", .mode = S_IRUGO | S_IWUSR },
956bcb7c
JA
555 .show = queue_show_random,
556 .store = queue_store_random,
e2e1a148
JA
557};
558
05229bee
JA
559static struct queue_sysfs_entry queue_poll_entry = {
560 .attr = {.name = "io_poll", .mode = S_IRUGO | S_IWUSR },
561 .show = queue_poll_show,
562 .store = queue_poll_store,
563};
564
93e9d8e8
JA
565static struct queue_sysfs_entry queue_wc_entry = {
566 .attr = {.name = "write_cache", .mode = S_IRUGO | S_IWUSR },
567 .show = queue_wc_show,
568 .store = queue_wc_store,
569};
570
ea6ca600
YK
571static struct queue_sysfs_entry queue_dax_entry = {
572 .attr = {.name = "dax", .mode = S_IRUGO },
573 .show = queue_dax_show,
574};
575
cf43e6be
JA
576static struct queue_sysfs_entry queue_stats_entry = {
577 .attr = {.name = "stats", .mode = S_IRUGO },
578 .show = queue_stats_show,
579};
580
8324aa91
JA
581static struct attribute *default_attrs[] = {
582 &queue_requests_entry.attr,
583 &queue_ra_entry.attr,
584 &queue_max_hw_sectors_entry.attr,
585 &queue_max_sectors_entry.attr,
c77a5710 586 &queue_max_segments_entry.attr,
13f05c8d 587 &queue_max_integrity_segments_entry.attr,
c77a5710 588 &queue_max_segment_size_entry.attr,
8324aa91 589 &queue_iosched_entry.attr,
e68b903c 590 &queue_hw_sector_size_entry.attr,
e1defc4f 591 &queue_logical_block_size_entry.attr,
c72758f3 592 &queue_physical_block_size_entry.attr,
87caf97c 593 &queue_chunk_sectors_entry.attr,
c72758f3
MP
594 &queue_io_min_entry.attr,
595 &queue_io_opt_entry.attr,
86b37281
MP
596 &queue_discard_granularity_entry.attr,
597 &queue_discard_max_entry.attr,
0034af03 598 &queue_discard_max_hw_entry.attr,
98262f27 599 &queue_discard_zeroes_data_entry.attr,
4363ac7c 600 &queue_write_same_max_entry.attr,
1308835f 601 &queue_nonrot_entry.attr,
797476b8 602 &queue_zoned_entry.attr,
ac9fafa1 603 &queue_nomerges_entry.attr,
c7c22e4d 604 &queue_rq_affinity_entry.attr,
bc58ba94 605 &queue_iostats_entry.attr,
e2e1a148 606 &queue_random_entry.attr,
05229bee 607 &queue_poll_entry.attr,
93e9d8e8 608 &queue_wc_entry.attr,
ea6ca600 609 &queue_dax_entry.attr,
cf43e6be 610 &queue_stats_entry.attr,
8324aa91
JA
611 NULL,
612};
613
614#define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
615
616static ssize_t
617queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
618{
619 struct queue_sysfs_entry *entry = to_queue(attr);
620 struct request_queue *q =
621 container_of(kobj, struct request_queue, kobj);
622 ssize_t res;
623
624 if (!entry->show)
625 return -EIO;
626 mutex_lock(&q->sysfs_lock);
3f3299d5 627 if (blk_queue_dying(q)) {
8324aa91
JA
628 mutex_unlock(&q->sysfs_lock);
629 return -ENOENT;
630 }
631 res = entry->show(q, page);
632 mutex_unlock(&q->sysfs_lock);
633 return res;
634}
635
636static ssize_t
637queue_attr_store(struct kobject *kobj, struct attribute *attr,
638 const char *page, size_t length)
639{
640 struct queue_sysfs_entry *entry = to_queue(attr);
6728cb0e 641 struct request_queue *q;
8324aa91
JA
642 ssize_t res;
643
644 if (!entry->store)
645 return -EIO;
6728cb0e
JA
646
647 q = container_of(kobj, struct request_queue, kobj);
8324aa91 648 mutex_lock(&q->sysfs_lock);
3f3299d5 649 if (blk_queue_dying(q)) {
8324aa91
JA
650 mutex_unlock(&q->sysfs_lock);
651 return -ENOENT;
652 }
653 res = entry->store(q, page, length);
654 mutex_unlock(&q->sysfs_lock);
655 return res;
656}
657
548bc8e1
TH
658static void blk_free_queue_rcu(struct rcu_head *rcu_head)
659{
660 struct request_queue *q = container_of(rcu_head, struct request_queue,
661 rcu_head);
662 kmem_cache_free(blk_requestq_cachep, q);
663}
664
8324aa91 665/**
499337bb
AM
666 * blk_release_queue: - release a &struct request_queue when it is no longer needed
667 * @kobj: the kobj belonging to the request queue to be released
8324aa91
JA
668 *
669 * Description:
499337bb 670 * blk_release_queue is the pair to blk_init_queue() or
8324aa91
JA
671 * blk_queue_make_request(). It should be called when a request queue is
672 * being released; typically when a block device is being de-registered.
673 * Currently, its primary task it to free all the &struct request
674 * structures that were allocated to the queue and the queue itself.
675 *
45a9c9d9
BVA
676 * Note:
677 * The low level driver must have finished any outstanding requests first
678 * via blk_cleanup_queue().
8324aa91
JA
679 **/
680static void blk_release_queue(struct kobject *kobj)
681{
682 struct request_queue *q =
683 container_of(kobj, struct request_queue, kobj);
8324aa91 684
b02176f3 685 bdi_exit(&q->backing_dev_info);
e8989fae
TH
686 blkcg_exit_queue(q);
687
7e5a8794
TH
688 if (q->elevator) {
689 spin_lock_irq(q->queue_lock);
690 ioc_clear_queue(q);
691 spin_unlock_irq(q->queue_lock);
777eb1bf 692 elevator_exit(q->elevator);
7e5a8794 693 }
777eb1bf 694
a051661c 695 blk_exit_rl(&q->root_rl);
8324aa91
JA
696
697 if (q->queue_tags)
698 __blk_queue_free_tags(q);
699
45a9c9d9 700 if (!q->mq_ops)
f70ced09 701 blk_free_flush_queue(q->fq);
e09aae7e
ML
702 else
703 blk_mq_release(q);
18741986 704
8324aa91
JA
705 blk_trace_shutdown(q);
706
54efd50b
KO
707 if (q->bio_split)
708 bioset_free(q->bio_split);
709
a73f730d 710 ida_simple_remove(&blk_queue_ida, q->id);
548bc8e1 711 call_rcu(&q->rcu_head, blk_free_queue_rcu);
8324aa91
JA
712}
713
52cf25d0 714static const struct sysfs_ops queue_sysfs_ops = {
8324aa91
JA
715 .show = queue_attr_show,
716 .store = queue_attr_store,
717};
718
719struct kobj_type blk_queue_ktype = {
720 .sysfs_ops = &queue_sysfs_ops,
721 .default_attrs = default_attrs,
722 .release = blk_release_queue,
723};
724
725int blk_register_queue(struct gendisk *disk)
726{
727 int ret;
1d54ad6d 728 struct device *dev = disk_to_dev(disk);
8324aa91
JA
729 struct request_queue *q = disk->queue;
730
fb199746 731 if (WARN_ON(!q))
8324aa91
JA
732 return -ENXIO;
733
749fefe6 734 /*
17497acb
TH
735 * SCSI probing may synchronously create and destroy a lot of
736 * request_queues for non-existent devices. Shutting down a fully
737 * functional queue takes measureable wallclock time as RCU grace
738 * periods are involved. To avoid excessive latency in these
739 * cases, a request_queue starts out in a degraded mode which is
740 * faster to shut down and is made fully functional here as
741 * request_queues for non-existent devices never get registered.
749fefe6 742 */
df35c7c9
AS
743 if (!blk_queue_init_done(q)) {
744 queue_flag_set_unlocked(QUEUE_FLAG_INIT_DONE, q);
3ef28e83 745 percpu_ref_switch_to_percpu(&q->q_usage_counter);
df35c7c9
AS
746 blk_queue_bypass_end(q);
747 }
749fefe6 748
1d54ad6d
LZ
749 ret = blk_trace_init_sysfs(dev);
750 if (ret)
751 return ret;
752
c9059598 753 ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
ed5302d3
LY
754 if (ret < 0) {
755 blk_trace_remove_sysfs(dev);
8324aa91 756 return ret;
ed5302d3 757 }
8324aa91
JA
758
759 kobject_uevent(&q->kobj, KOBJ_ADD);
760
320ae51f 761 if (q->mq_ops)
b21d5b30 762 blk_mq_register_dev(dev, q);
320ae51f 763
cd43e26f
MP
764 if (!q->request_fn)
765 return 0;
766
8324aa91
JA
767 ret = elv_register_queue(q);
768 if (ret) {
769 kobject_uevent(&q->kobj, KOBJ_REMOVE);
770 kobject_del(&q->kobj);
80656b67 771 blk_trace_remove_sysfs(dev);
c87ffbb8 772 kobject_put(&dev->kobj);
8324aa91
JA
773 return ret;
774 }
775
776 return 0;
777}
778
779void blk_unregister_queue(struct gendisk *disk)
780{
781 struct request_queue *q = disk->queue;
782
fb199746
AM
783 if (WARN_ON(!q))
784 return;
785
320ae51f 786 if (q->mq_ops)
b21d5b30 787 blk_mq_unregister_dev(disk_to_dev(disk), q);
320ae51f 788
48c0d4d4 789 if (q->request_fn)
8324aa91
JA
790 elv_unregister_queue(q);
791
48c0d4d4
ZK
792 kobject_uevent(&q->kobj, KOBJ_REMOVE);
793 kobject_del(&q->kobj);
794 blk_trace_remove_sysfs(disk_to_dev(disk));
795 kobject_put(&disk_to_dev(disk)->kobj);
8324aa91 796}