Merge tag 'x86-build-2020-08-03' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / block / blk-sysfs.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Functions related to sysfs handling
4  */
5 #include <linux/kernel.h>
6 #include <linux/slab.h>
7 #include <linux/module.h>
8 #include <linux/bio.h>
9 #include <linux/blkdev.h>
10 #include <linux/backing-dev.h>
11 #include <linux/blktrace_api.h>
12 #include <linux/blk-mq.h>
13 #include <linux/blk-cgroup.h>
14 #include <linux/debugfs.h>
15
16 #include "blk.h"
17 #include "blk-mq.h"
18 #include "blk-mq-debugfs.h"
19 #include "blk-wbt.h"
20
21 struct queue_sysfs_entry {
22         struct attribute attr;
23         ssize_t (*show)(struct request_queue *, char *);
24         ssize_t (*store)(struct request_queue *, const char *, size_t);
25 };
26
27 static ssize_t
28 queue_var_show(unsigned long var, char *page)
29 {
30         return sprintf(page, "%lu\n", var);
31 }
32
33 static ssize_t
34 queue_var_store(unsigned long *var, const char *page, size_t count)
35 {
36         int err;
37         unsigned long v;
38
39         err = kstrtoul(page, 10, &v);
40         if (err || v > UINT_MAX)
41                 return -EINVAL;
42
43         *var = v;
44
45         return count;
46 }
47
48 static ssize_t queue_var_store64(s64 *var, const char *page)
49 {
50         int err;
51         s64 v;
52
53         err = kstrtos64(page, 10, &v);
54         if (err < 0)
55                 return err;
56
57         *var = v;
58         return 0;
59 }
60
61 static ssize_t queue_requests_show(struct request_queue *q, char *page)
62 {
63         return queue_var_show(q->nr_requests, (page));
64 }
65
66 static ssize_t
67 queue_requests_store(struct request_queue *q, const char *page, size_t count)
68 {
69         unsigned long nr;
70         int ret, err;
71
72         if (!queue_is_mq(q))
73                 return -EINVAL;
74
75         ret = queue_var_store(&nr, page, count);
76         if (ret < 0)
77                 return ret;
78
79         if (nr < BLKDEV_MIN_RQ)
80                 nr = BLKDEV_MIN_RQ;
81
82         err = blk_mq_update_nr_requests(q, nr);
83         if (err)
84                 return err;
85
86         return ret;
87 }
88
89 static ssize_t queue_ra_show(struct request_queue *q, char *page)
90 {
91         unsigned long ra_kb = q->backing_dev_info->ra_pages <<
92                                         (PAGE_SHIFT - 10);
93
94         return queue_var_show(ra_kb, (page));
95 }
96
97 static ssize_t
98 queue_ra_store(struct request_queue *q, const char *page, size_t count)
99 {
100         unsigned long ra_kb;
101         ssize_t ret = queue_var_store(&ra_kb, page, count);
102
103         if (ret < 0)
104                 return ret;
105
106         q->backing_dev_info->ra_pages = ra_kb >> (PAGE_SHIFT - 10);
107
108         return ret;
109 }
110
111 static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
112 {
113         int max_sectors_kb = queue_max_sectors(q) >> 1;
114
115         return queue_var_show(max_sectors_kb, (page));
116 }
117
118 static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
119 {
120         return queue_var_show(queue_max_segments(q), (page));
121 }
122
123 static ssize_t queue_max_discard_segments_show(struct request_queue *q,
124                 char *page)
125 {
126         return queue_var_show(queue_max_discard_segments(q), (page));
127 }
128
129 static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
130 {
131         return queue_var_show(q->limits.max_integrity_segments, (page));
132 }
133
134 static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
135 {
136         return queue_var_show(queue_max_segment_size(q), (page));
137 }
138
139 static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
140 {
141         return queue_var_show(queue_logical_block_size(q), page);
142 }
143
144 static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
145 {
146         return queue_var_show(queue_physical_block_size(q), page);
147 }
148
149 static ssize_t queue_chunk_sectors_show(struct request_queue *q, char *page)
150 {
151         return queue_var_show(q->limits.chunk_sectors, page);
152 }
153
154 static ssize_t queue_io_min_show(struct request_queue *q, char *page)
155 {
156         return queue_var_show(queue_io_min(q), page);
157 }
158
159 static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
160 {
161         return queue_var_show(queue_io_opt(q), page);
162 }
163
164 static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
165 {
166         return queue_var_show(q->limits.discard_granularity, page);
167 }
168
169 static ssize_t queue_discard_max_hw_show(struct request_queue *q, char *page)
170 {
171
172         return sprintf(page, "%llu\n",
173                 (unsigned long long)q->limits.max_hw_discard_sectors << 9);
174 }
175
176 static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
177 {
178         return sprintf(page, "%llu\n",
179                        (unsigned long long)q->limits.max_discard_sectors << 9);
180 }
181
182 static ssize_t queue_discard_max_store(struct request_queue *q,
183                                        const char *page, size_t count)
184 {
185         unsigned long max_discard;
186         ssize_t ret = queue_var_store(&max_discard, page, count);
187
188         if (ret < 0)
189                 return ret;
190
191         if (max_discard & (q->limits.discard_granularity - 1))
192                 return -EINVAL;
193
194         max_discard >>= 9;
195         if (max_discard > UINT_MAX)
196                 return -EINVAL;
197
198         if (max_discard > q->limits.max_hw_discard_sectors)
199                 max_discard = q->limits.max_hw_discard_sectors;
200
201         q->limits.max_discard_sectors = max_discard;
202         return ret;
203 }
204
205 static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
206 {
207         return queue_var_show(0, page);
208 }
209
210 static ssize_t queue_write_same_max_show(struct request_queue *q, char *page)
211 {
212         return sprintf(page, "%llu\n",
213                 (unsigned long long)q->limits.max_write_same_sectors << 9);
214 }
215
216 static ssize_t queue_write_zeroes_max_show(struct request_queue *q, char *page)
217 {
218         return sprintf(page, "%llu\n",
219                 (unsigned long long)q->limits.max_write_zeroes_sectors << 9);
220 }
221
222 static ssize_t queue_zone_append_max_show(struct request_queue *q, char *page)
223 {
224         unsigned long long max_sectors = q->limits.max_zone_append_sectors;
225
226         return sprintf(page, "%llu\n", max_sectors << SECTOR_SHIFT);
227 }
228
229 static ssize_t
230 queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
231 {
232         unsigned long max_sectors_kb,
233                 max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
234                         page_kb = 1 << (PAGE_SHIFT - 10);
235         ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
236
237         if (ret < 0)
238                 return ret;
239
240         max_hw_sectors_kb = min_not_zero(max_hw_sectors_kb, (unsigned long)
241                                          q->limits.max_dev_sectors >> 1);
242
243         if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
244                 return -EINVAL;
245
246         spin_lock_irq(&q->queue_lock);
247         q->limits.max_sectors = max_sectors_kb << 1;
248         q->backing_dev_info->io_pages = max_sectors_kb >> (PAGE_SHIFT - 10);
249         spin_unlock_irq(&q->queue_lock);
250
251         return ret;
252 }
253
254 static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
255 {
256         int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
257
258         return queue_var_show(max_hw_sectors_kb, (page));
259 }
260
261 #define QUEUE_SYSFS_BIT_FNS(name, flag, neg)                            \
262 static ssize_t                                                          \
263 queue_show_##name(struct request_queue *q, char *page)                  \
264 {                                                                       \
265         int bit;                                                        \
266         bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags);             \
267         return queue_var_show(neg ? !bit : bit, page);                  \
268 }                                                                       \
269 static ssize_t                                                          \
270 queue_store_##name(struct request_queue *q, const char *page, size_t count) \
271 {                                                                       \
272         unsigned long val;                                              \
273         ssize_t ret;                                                    \
274         ret = queue_var_store(&val, page, count);                       \
275         if (ret < 0)                                                    \
276                  return ret;                                            \
277         if (neg)                                                        \
278                 val = !val;                                             \
279                                                                         \
280         if (val)                                                        \
281                 blk_queue_flag_set(QUEUE_FLAG_##flag, q);               \
282         else                                                            \
283                 blk_queue_flag_clear(QUEUE_FLAG_##flag, q);             \
284         return ret;                                                     \
285 }
286
287 QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
288 QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
289 QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
290 #undef QUEUE_SYSFS_BIT_FNS
291
292 static ssize_t queue_zoned_show(struct request_queue *q, char *page)
293 {
294         switch (blk_queue_zoned_model(q)) {
295         case BLK_ZONED_HA:
296                 return sprintf(page, "host-aware\n");
297         case BLK_ZONED_HM:
298                 return sprintf(page, "host-managed\n");
299         default:
300                 return sprintf(page, "none\n");
301         }
302 }
303
304 static ssize_t queue_nr_zones_show(struct request_queue *q, char *page)
305 {
306         return queue_var_show(blk_queue_nr_zones(q), page);
307 }
308
309 static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
310 {
311         return queue_var_show((blk_queue_nomerges(q) << 1) |
312                                blk_queue_noxmerges(q), page);
313 }
314
315 static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
316                                     size_t count)
317 {
318         unsigned long nm;
319         ssize_t ret = queue_var_store(&nm, page, count);
320
321         if (ret < 0)
322                 return ret;
323
324         blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
325         blk_queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
326         if (nm == 2)
327                 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, q);
328         else if (nm)
329                 blk_queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
330
331         return ret;
332 }
333
334 static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
335 {
336         bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
337         bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
338
339         return queue_var_show(set << force, page);
340 }
341
342 static ssize_t
343 queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
344 {
345         ssize_t ret = -EINVAL;
346 #ifdef CONFIG_SMP
347         unsigned long val;
348
349         ret = queue_var_store(&val, page, count);
350         if (ret < 0)
351                 return ret;
352
353         if (val == 2) {
354                 blk_queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
355                 blk_queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
356         } else if (val == 1) {
357                 blk_queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
358                 blk_queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
359         } else if (val == 0) {
360                 blk_queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
361                 blk_queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
362         }
363 #endif
364         return ret;
365 }
366
367 static ssize_t queue_poll_delay_show(struct request_queue *q, char *page)
368 {
369         int val;
370
371         if (q->poll_nsec == BLK_MQ_POLL_CLASSIC)
372                 val = BLK_MQ_POLL_CLASSIC;
373         else
374                 val = q->poll_nsec / 1000;
375
376         return sprintf(page, "%d\n", val);
377 }
378
379 static ssize_t queue_poll_delay_store(struct request_queue *q, const char *page,
380                                 size_t count)
381 {
382         int err, val;
383
384         if (!q->mq_ops || !q->mq_ops->poll)
385                 return -EINVAL;
386
387         err = kstrtoint(page, 10, &val);
388         if (err < 0)
389                 return err;
390
391         if (val == BLK_MQ_POLL_CLASSIC)
392                 q->poll_nsec = BLK_MQ_POLL_CLASSIC;
393         else if (val >= 0)
394                 q->poll_nsec = val * 1000;
395         else
396                 return -EINVAL;
397
398         return count;
399 }
400
401 static ssize_t queue_poll_show(struct request_queue *q, char *page)
402 {
403         return queue_var_show(test_bit(QUEUE_FLAG_POLL, &q->queue_flags), page);
404 }
405
406 static ssize_t queue_poll_store(struct request_queue *q, const char *page,
407                                 size_t count)
408 {
409         unsigned long poll_on;
410         ssize_t ret;
411
412         if (!q->tag_set || q->tag_set->nr_maps <= HCTX_TYPE_POLL ||
413             !q->tag_set->map[HCTX_TYPE_POLL].nr_queues)
414                 return -EINVAL;
415
416         ret = queue_var_store(&poll_on, page, count);
417         if (ret < 0)
418                 return ret;
419
420         if (poll_on)
421                 blk_queue_flag_set(QUEUE_FLAG_POLL, q);
422         else
423                 blk_queue_flag_clear(QUEUE_FLAG_POLL, q);
424
425         return ret;
426 }
427
428 static ssize_t queue_io_timeout_show(struct request_queue *q, char *page)
429 {
430         return sprintf(page, "%u\n", jiffies_to_msecs(q->rq_timeout));
431 }
432
433 static ssize_t queue_io_timeout_store(struct request_queue *q, const char *page,
434                                   size_t count)
435 {
436         unsigned int val;
437         int err;
438
439         err = kstrtou32(page, 10, &val);
440         if (err || val == 0)
441                 return -EINVAL;
442
443         blk_queue_rq_timeout(q, msecs_to_jiffies(val));
444
445         return count;
446 }
447
448 static ssize_t queue_wb_lat_show(struct request_queue *q, char *page)
449 {
450         if (!wbt_rq_qos(q))
451                 return -EINVAL;
452
453         return sprintf(page, "%llu\n", div_u64(wbt_get_min_lat(q), 1000));
454 }
455
456 static ssize_t queue_wb_lat_store(struct request_queue *q, const char *page,
457                                   size_t count)
458 {
459         struct rq_qos *rqos;
460         ssize_t ret;
461         s64 val;
462
463         ret = queue_var_store64(&val, page);
464         if (ret < 0)
465                 return ret;
466         if (val < -1)
467                 return -EINVAL;
468
469         rqos = wbt_rq_qos(q);
470         if (!rqos) {
471                 ret = wbt_init(q);
472                 if (ret)
473                         return ret;
474         }
475
476         if (val == -1)
477                 val = wbt_default_latency_nsec(q);
478         else if (val >= 0)
479                 val *= 1000ULL;
480
481         if (wbt_get_min_lat(q) == val)
482                 return count;
483
484         /*
485          * Ensure that the queue is idled, in case the latency update
486          * ends up either enabling or disabling wbt completely. We can't
487          * have IO inflight if that happens.
488          */
489         blk_mq_freeze_queue(q);
490         blk_mq_quiesce_queue(q);
491
492         wbt_set_min_lat(q, val);
493
494         blk_mq_unquiesce_queue(q);
495         blk_mq_unfreeze_queue(q);
496
497         return count;
498 }
499
500 static ssize_t queue_wc_show(struct request_queue *q, char *page)
501 {
502         if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
503                 return sprintf(page, "write back\n");
504
505         return sprintf(page, "write through\n");
506 }
507
508 static ssize_t queue_wc_store(struct request_queue *q, const char *page,
509                               size_t count)
510 {
511         int set = -1;
512
513         if (!strncmp(page, "write back", 10))
514                 set = 1;
515         else if (!strncmp(page, "write through", 13) ||
516                  !strncmp(page, "none", 4))
517                 set = 0;
518
519         if (set == -1)
520                 return -EINVAL;
521
522         if (set)
523                 blk_queue_flag_set(QUEUE_FLAG_WC, q);
524         else
525                 blk_queue_flag_clear(QUEUE_FLAG_WC, q);
526
527         return count;
528 }
529
530 static ssize_t queue_fua_show(struct request_queue *q, char *page)
531 {
532         return sprintf(page, "%u\n", test_bit(QUEUE_FLAG_FUA, &q->queue_flags));
533 }
534
535 static ssize_t queue_dax_show(struct request_queue *q, char *page)
536 {
537         return queue_var_show(blk_queue_dax(q), page);
538 }
539
540 static struct queue_sysfs_entry queue_requests_entry = {
541         .attr = {.name = "nr_requests", .mode = 0644 },
542         .show = queue_requests_show,
543         .store = queue_requests_store,
544 };
545
546 static struct queue_sysfs_entry queue_ra_entry = {
547         .attr = {.name = "read_ahead_kb", .mode = 0644 },
548         .show = queue_ra_show,
549         .store = queue_ra_store,
550 };
551
552 static struct queue_sysfs_entry queue_max_sectors_entry = {
553         .attr = {.name = "max_sectors_kb", .mode = 0644 },
554         .show = queue_max_sectors_show,
555         .store = queue_max_sectors_store,
556 };
557
558 static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
559         .attr = {.name = "max_hw_sectors_kb", .mode = 0444 },
560         .show = queue_max_hw_sectors_show,
561 };
562
563 static struct queue_sysfs_entry queue_max_segments_entry = {
564         .attr = {.name = "max_segments", .mode = 0444 },
565         .show = queue_max_segments_show,
566 };
567
568 static struct queue_sysfs_entry queue_max_discard_segments_entry = {
569         .attr = {.name = "max_discard_segments", .mode = 0444 },
570         .show = queue_max_discard_segments_show,
571 };
572
573 static struct queue_sysfs_entry queue_max_integrity_segments_entry = {
574         .attr = {.name = "max_integrity_segments", .mode = 0444 },
575         .show = queue_max_integrity_segments_show,
576 };
577
578 static struct queue_sysfs_entry queue_max_segment_size_entry = {
579         .attr = {.name = "max_segment_size", .mode = 0444 },
580         .show = queue_max_segment_size_show,
581 };
582
583 static struct queue_sysfs_entry queue_iosched_entry = {
584         .attr = {.name = "scheduler", .mode = 0644 },
585         .show = elv_iosched_show,
586         .store = elv_iosched_store,
587 };
588
589 static struct queue_sysfs_entry queue_hw_sector_size_entry = {
590         .attr = {.name = "hw_sector_size", .mode = 0444 },
591         .show = queue_logical_block_size_show,
592 };
593
594 static struct queue_sysfs_entry queue_logical_block_size_entry = {
595         .attr = {.name = "logical_block_size", .mode = 0444 },
596         .show = queue_logical_block_size_show,
597 };
598
599 static struct queue_sysfs_entry queue_physical_block_size_entry = {
600         .attr = {.name = "physical_block_size", .mode = 0444 },
601         .show = queue_physical_block_size_show,
602 };
603
604 static struct queue_sysfs_entry queue_chunk_sectors_entry = {
605         .attr = {.name = "chunk_sectors", .mode = 0444 },
606         .show = queue_chunk_sectors_show,
607 };
608
609 static struct queue_sysfs_entry queue_io_min_entry = {
610         .attr = {.name = "minimum_io_size", .mode = 0444 },
611         .show = queue_io_min_show,
612 };
613
614 static struct queue_sysfs_entry queue_io_opt_entry = {
615         .attr = {.name = "optimal_io_size", .mode = 0444 },
616         .show = queue_io_opt_show,
617 };
618
619 static struct queue_sysfs_entry queue_discard_granularity_entry = {
620         .attr = {.name = "discard_granularity", .mode = 0444 },
621         .show = queue_discard_granularity_show,
622 };
623
624 static struct queue_sysfs_entry queue_discard_max_hw_entry = {
625         .attr = {.name = "discard_max_hw_bytes", .mode = 0444 },
626         .show = queue_discard_max_hw_show,
627 };
628
629 static struct queue_sysfs_entry queue_discard_max_entry = {
630         .attr = {.name = "discard_max_bytes", .mode = 0644 },
631         .show = queue_discard_max_show,
632         .store = queue_discard_max_store,
633 };
634
635 static struct queue_sysfs_entry queue_discard_zeroes_data_entry = {
636         .attr = {.name = "discard_zeroes_data", .mode = 0444 },
637         .show = queue_discard_zeroes_data_show,
638 };
639
640 static struct queue_sysfs_entry queue_write_same_max_entry = {
641         .attr = {.name = "write_same_max_bytes", .mode = 0444 },
642         .show = queue_write_same_max_show,
643 };
644
645 static struct queue_sysfs_entry queue_write_zeroes_max_entry = {
646         .attr = {.name = "write_zeroes_max_bytes", .mode = 0444 },
647         .show = queue_write_zeroes_max_show,
648 };
649
650 static struct queue_sysfs_entry queue_zone_append_max_entry = {
651         .attr = {.name = "zone_append_max_bytes", .mode = 0444 },
652         .show = queue_zone_append_max_show,
653 };
654
655 static struct queue_sysfs_entry queue_nonrot_entry = {
656         .attr = {.name = "rotational", .mode = 0644 },
657         .show = queue_show_nonrot,
658         .store = queue_store_nonrot,
659 };
660
661 static struct queue_sysfs_entry queue_zoned_entry = {
662         .attr = {.name = "zoned", .mode = 0444 },
663         .show = queue_zoned_show,
664 };
665
666 static struct queue_sysfs_entry queue_nr_zones_entry = {
667         .attr = {.name = "nr_zones", .mode = 0444 },
668         .show = queue_nr_zones_show,
669 };
670
671 static struct queue_sysfs_entry queue_nomerges_entry = {
672         .attr = {.name = "nomerges", .mode = 0644 },
673         .show = queue_nomerges_show,
674         .store = queue_nomerges_store,
675 };
676
677 static struct queue_sysfs_entry queue_rq_affinity_entry = {
678         .attr = {.name = "rq_affinity", .mode = 0644 },
679         .show = queue_rq_affinity_show,
680         .store = queue_rq_affinity_store,
681 };
682
683 static struct queue_sysfs_entry queue_iostats_entry = {
684         .attr = {.name = "iostats", .mode = 0644 },
685         .show = queue_show_iostats,
686         .store = queue_store_iostats,
687 };
688
689 static struct queue_sysfs_entry queue_random_entry = {
690         .attr = {.name = "add_random", .mode = 0644 },
691         .show = queue_show_random,
692         .store = queue_store_random,
693 };
694
695 static struct queue_sysfs_entry queue_poll_entry = {
696         .attr = {.name = "io_poll", .mode = 0644 },
697         .show = queue_poll_show,
698         .store = queue_poll_store,
699 };
700
701 static struct queue_sysfs_entry queue_poll_delay_entry = {
702         .attr = {.name = "io_poll_delay", .mode = 0644 },
703         .show = queue_poll_delay_show,
704         .store = queue_poll_delay_store,
705 };
706
707 static struct queue_sysfs_entry queue_wc_entry = {
708         .attr = {.name = "write_cache", .mode = 0644 },
709         .show = queue_wc_show,
710         .store = queue_wc_store,
711 };
712
713 static struct queue_sysfs_entry queue_fua_entry = {
714         .attr = {.name = "fua", .mode = 0444 },
715         .show = queue_fua_show,
716 };
717
718 static struct queue_sysfs_entry queue_dax_entry = {
719         .attr = {.name = "dax", .mode = 0444 },
720         .show = queue_dax_show,
721 };
722
723 static struct queue_sysfs_entry queue_io_timeout_entry = {
724         .attr = {.name = "io_timeout", .mode = 0644 },
725         .show = queue_io_timeout_show,
726         .store = queue_io_timeout_store,
727 };
728
729 static struct queue_sysfs_entry queue_wb_lat_entry = {
730         .attr = {.name = "wbt_lat_usec", .mode = 0644 },
731         .show = queue_wb_lat_show,
732         .store = queue_wb_lat_store,
733 };
734
735 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
736 static struct queue_sysfs_entry throtl_sample_time_entry = {
737         .attr = {.name = "throttle_sample_time", .mode = 0644 },
738         .show = blk_throtl_sample_time_show,
739         .store = blk_throtl_sample_time_store,
740 };
741 #endif
742
743 static struct attribute *queue_attrs[] = {
744         &queue_requests_entry.attr,
745         &queue_ra_entry.attr,
746         &queue_max_hw_sectors_entry.attr,
747         &queue_max_sectors_entry.attr,
748         &queue_max_segments_entry.attr,
749         &queue_max_discard_segments_entry.attr,
750         &queue_max_integrity_segments_entry.attr,
751         &queue_max_segment_size_entry.attr,
752         &queue_iosched_entry.attr,
753         &queue_hw_sector_size_entry.attr,
754         &queue_logical_block_size_entry.attr,
755         &queue_physical_block_size_entry.attr,
756         &queue_chunk_sectors_entry.attr,
757         &queue_io_min_entry.attr,
758         &queue_io_opt_entry.attr,
759         &queue_discard_granularity_entry.attr,
760         &queue_discard_max_entry.attr,
761         &queue_discard_max_hw_entry.attr,
762         &queue_discard_zeroes_data_entry.attr,
763         &queue_write_same_max_entry.attr,
764         &queue_write_zeroes_max_entry.attr,
765         &queue_zone_append_max_entry.attr,
766         &queue_nonrot_entry.attr,
767         &queue_zoned_entry.attr,
768         &queue_nr_zones_entry.attr,
769         &queue_nomerges_entry.attr,
770         &queue_rq_affinity_entry.attr,
771         &queue_iostats_entry.attr,
772         &queue_random_entry.attr,
773         &queue_poll_entry.attr,
774         &queue_wc_entry.attr,
775         &queue_fua_entry.attr,
776         &queue_dax_entry.attr,
777         &queue_wb_lat_entry.attr,
778         &queue_poll_delay_entry.attr,
779         &queue_io_timeout_entry.attr,
780 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
781         &throtl_sample_time_entry.attr,
782 #endif
783         NULL,
784 };
785
786 static umode_t queue_attr_visible(struct kobject *kobj, struct attribute *attr,
787                                 int n)
788 {
789         struct request_queue *q =
790                 container_of(kobj, struct request_queue, kobj);
791
792         if (attr == &queue_io_timeout_entry.attr &&
793                 (!q->mq_ops || !q->mq_ops->timeout))
794                         return 0;
795
796         return attr->mode;
797 }
798
799 static struct attribute_group queue_attr_group = {
800         .attrs = queue_attrs,
801         .is_visible = queue_attr_visible,
802 };
803
804
805 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
806
807 static ssize_t
808 queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
809 {
810         struct queue_sysfs_entry *entry = to_queue(attr);
811         struct request_queue *q =
812                 container_of(kobj, struct request_queue, kobj);
813         ssize_t res;
814
815         if (!entry->show)
816                 return -EIO;
817         mutex_lock(&q->sysfs_lock);
818         res = entry->show(q, page);
819         mutex_unlock(&q->sysfs_lock);
820         return res;
821 }
822
823 static ssize_t
824 queue_attr_store(struct kobject *kobj, struct attribute *attr,
825                     const char *page, size_t length)
826 {
827         struct queue_sysfs_entry *entry = to_queue(attr);
828         struct request_queue *q;
829         ssize_t res;
830
831         if (!entry->store)
832                 return -EIO;
833
834         q = container_of(kobj, struct request_queue, kobj);
835         mutex_lock(&q->sysfs_lock);
836         res = entry->store(q, page, length);
837         mutex_unlock(&q->sysfs_lock);
838         return res;
839 }
840
841 static void blk_free_queue_rcu(struct rcu_head *rcu_head)
842 {
843         struct request_queue *q = container_of(rcu_head, struct request_queue,
844                                                rcu_head);
845         kmem_cache_free(blk_requestq_cachep, q);
846 }
847
848 /* Unconfigure the I/O scheduler and dissociate from the cgroup controller. */
849 static void blk_exit_queue(struct request_queue *q)
850 {
851         /*
852          * Since the I/O scheduler exit code may access cgroup information,
853          * perform I/O scheduler exit before disassociating from the block
854          * cgroup controller.
855          */
856         if (q->elevator) {
857                 ioc_clear_queue(q);
858                 __elevator_exit(q, q->elevator);
859                 q->elevator = NULL;
860         }
861
862         /*
863          * Remove all references to @q from the block cgroup controller before
864          * restoring @q->queue_lock to avoid that restoring this pointer causes
865          * e.g. blkcg_print_blkgs() to crash.
866          */
867         blkcg_exit_queue(q);
868
869         /*
870          * Since the cgroup code may dereference the @q->backing_dev_info
871          * pointer, only decrease its reference count after having removed the
872          * association with the block cgroup controller.
873          */
874         bdi_put(q->backing_dev_info);
875 }
876
877 /**
878  * blk_release_queue - releases all allocated resources of the request_queue
879  * @kobj: pointer to a kobject, whose container is a request_queue
880  *
881  * This function releases all allocated resources of the request queue.
882  *
883  * The struct request_queue refcount is incremented with blk_get_queue() and
884  * decremented with blk_put_queue(). Once the refcount reaches 0 this function
885  * is called.
886  *
887  * For drivers that have a request_queue on a gendisk and added with
888  * __device_add_disk() the refcount to request_queue will reach 0 with
889  * the last put_disk() called by the driver. For drivers which don't use
890  * __device_add_disk() this happens with blk_cleanup_queue().
891  *
892  * Drivers exist which depend on the release of the request_queue to be
893  * synchronous, it should not be deferred.
894  *
895  * Context: can sleep
896  */
897 static void blk_release_queue(struct kobject *kobj)
898 {
899         struct request_queue *q =
900                 container_of(kobj, struct request_queue, kobj);
901
902         might_sleep();
903
904         if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags))
905                 blk_stat_remove_callback(q, q->poll_cb);
906         blk_stat_free_callback(q->poll_cb);
907
908         blk_free_queue_stats(q->stats);
909
910         if (queue_is_mq(q))
911                 cancel_delayed_work_sync(&q->requeue_work);
912
913         blk_exit_queue(q);
914
915         blk_queue_free_zone_bitmaps(q);
916
917         if (queue_is_mq(q))
918                 blk_mq_release(q);
919
920         blk_trace_shutdown(q);
921         mutex_lock(&q->debugfs_mutex);
922         debugfs_remove_recursive(q->debugfs_dir);
923         mutex_unlock(&q->debugfs_mutex);
924
925         if (queue_is_mq(q))
926                 blk_mq_debugfs_unregister(q);
927
928         bioset_exit(&q->bio_split);
929
930         ida_simple_remove(&blk_queue_ida, q->id);
931         call_rcu(&q->rcu_head, blk_free_queue_rcu);
932 }
933
934 static const struct sysfs_ops queue_sysfs_ops = {
935         .show   = queue_attr_show,
936         .store  = queue_attr_store,
937 };
938
939 struct kobj_type blk_queue_ktype = {
940         .sysfs_ops      = &queue_sysfs_ops,
941         .release        = blk_release_queue,
942 };
943
944 /**
945  * blk_register_queue - register a block layer queue with sysfs
946  * @disk: Disk of which the request queue should be registered with sysfs.
947  */
948 int blk_register_queue(struct gendisk *disk)
949 {
950         int ret;
951         struct device *dev = disk_to_dev(disk);
952         struct request_queue *q = disk->queue;
953         bool has_elevator = false;
954
955         if (WARN_ON(!q))
956                 return -ENXIO;
957
958         WARN_ONCE(blk_queue_registered(q),
959                   "%s is registering an already registered queue\n",
960                   kobject_name(&dev->kobj));
961
962         /*
963          * SCSI probing may synchronously create and destroy a lot of
964          * request_queues for non-existent devices.  Shutting down a fully
965          * functional queue takes measureable wallclock time as RCU grace
966          * periods are involved.  To avoid excessive latency in these
967          * cases, a request_queue starts out in a degraded mode which is
968          * faster to shut down and is made fully functional here as
969          * request_queues for non-existent devices never get registered.
970          */
971         if (!blk_queue_init_done(q)) {
972                 blk_queue_flag_set(QUEUE_FLAG_INIT_DONE, q);
973                 percpu_ref_switch_to_percpu(&q->q_usage_counter);
974         }
975
976         ret = blk_trace_init_sysfs(dev);
977         if (ret)
978                 return ret;
979
980         mutex_lock(&q->sysfs_dir_lock);
981
982         ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
983         if (ret < 0) {
984                 blk_trace_remove_sysfs(dev);
985                 goto unlock;
986         }
987
988         ret = sysfs_create_group(&q->kobj, &queue_attr_group);
989         if (ret) {
990                 blk_trace_remove_sysfs(dev);
991                 kobject_del(&q->kobj);
992                 kobject_put(&dev->kobj);
993                 goto unlock;
994         }
995
996         mutex_lock(&q->debugfs_mutex);
997         q->debugfs_dir = debugfs_create_dir(kobject_name(q->kobj.parent),
998                                             blk_debugfs_root);
999         mutex_unlock(&q->debugfs_mutex);
1000
1001         if (queue_is_mq(q)) {
1002                 __blk_mq_register_dev(dev, q);
1003                 blk_mq_debugfs_register(q);
1004         }
1005
1006         mutex_lock(&q->sysfs_lock);
1007         if (q->elevator) {
1008                 ret = elv_register_queue(q, false);
1009                 if (ret) {
1010                         mutex_unlock(&q->sysfs_lock);
1011                         mutex_unlock(&q->sysfs_dir_lock);
1012                         kobject_del(&q->kobj);
1013                         blk_trace_remove_sysfs(dev);
1014                         kobject_put(&dev->kobj);
1015                         return ret;
1016                 }
1017                 has_elevator = true;
1018         }
1019
1020         blk_queue_flag_set(QUEUE_FLAG_REGISTERED, q);
1021         wbt_enable_default(q);
1022         blk_throtl_register_queue(q);
1023
1024         /* Now everything is ready and send out KOBJ_ADD uevent */
1025         kobject_uevent(&q->kobj, KOBJ_ADD);
1026         if (has_elevator)
1027                 kobject_uevent(&q->elevator->kobj, KOBJ_ADD);
1028         mutex_unlock(&q->sysfs_lock);
1029
1030         ret = 0;
1031 unlock:
1032         mutex_unlock(&q->sysfs_dir_lock);
1033         return ret;
1034 }
1035 EXPORT_SYMBOL_GPL(blk_register_queue);
1036
1037 /**
1038  * blk_unregister_queue - counterpart of blk_register_queue()
1039  * @disk: Disk of which the request queue should be unregistered from sysfs.
1040  *
1041  * Note: the caller is responsible for guaranteeing that this function is called
1042  * after blk_register_queue() has finished.
1043  */
1044 void blk_unregister_queue(struct gendisk *disk)
1045 {
1046         struct request_queue *q = disk->queue;
1047
1048         if (WARN_ON(!q))
1049                 return;
1050
1051         /* Return early if disk->queue was never registered. */
1052         if (!blk_queue_registered(q))
1053                 return;
1054
1055         /*
1056          * Since sysfs_remove_dir() prevents adding new directory entries
1057          * before removal of existing entries starts, protect against
1058          * concurrent elv_iosched_store() calls.
1059          */
1060         mutex_lock(&q->sysfs_lock);
1061         blk_queue_flag_clear(QUEUE_FLAG_REGISTERED, q);
1062         mutex_unlock(&q->sysfs_lock);
1063
1064         mutex_lock(&q->sysfs_dir_lock);
1065         /*
1066          * Remove the sysfs attributes before unregistering the queue data
1067          * structures that can be modified through sysfs.
1068          */
1069         if (queue_is_mq(q))
1070                 blk_mq_unregister_dev(disk_to_dev(disk), q);
1071
1072         kobject_uevent(&q->kobj, KOBJ_REMOVE);
1073         kobject_del(&q->kobj);
1074         blk_trace_remove_sysfs(disk_to_dev(disk));
1075
1076         mutex_lock(&q->sysfs_lock);
1077         if (q->elevator)
1078                 elv_unregister_queue(q);
1079         mutex_unlock(&q->sysfs_lock);
1080         mutex_unlock(&q->sysfs_dir_lock);
1081
1082         kobject_put(&disk_to_dev(disk)->kobj);
1083 }