Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
8324aa91 JA |
2 | /* |
3 | * Functions related to sysfs handling | |
4 | */ | |
5 | #include <linux/kernel.h> | |
5a0e3ad6 | 6 | #include <linux/slab.h> |
8324aa91 JA |
7 | #include <linux/module.h> |
8 | #include <linux/bio.h> | |
9 | #include <linux/blkdev.h> | |
66114cad | 10 | #include <linux/backing-dev.h> |
8324aa91 | 11 | #include <linux/blktrace_api.h> |
320ae51f | 12 | #include <linux/blk-mq.h> |
85e0cbbb | 13 | #include <linux/debugfs.h> |
8324aa91 JA |
14 | |
15 | #include "blk.h" | |
3edcc0ce | 16 | #include "blk-mq.h" |
d173a251 | 17 | #include "blk-mq-debugfs.h" |
2aa7745b | 18 | #include "blk-mq-sched.h" |
0bc65bd4 | 19 | #include "blk-rq-qos.h" |
87760e5e | 20 | #include "blk-wbt.h" |
672fdcf0 | 21 | #include "blk-cgroup.h" |
a7b36ee6 | 22 | #include "blk-throttle.h" |
8324aa91 JA |
23 | |
24 | struct queue_sysfs_entry { | |
25 | struct attribute attr; | |
26 | ssize_t (*show)(struct request_queue *, char *); | |
27 | ssize_t (*store)(struct request_queue *, const char *, size_t); | |
28 | }; | |
29 | ||
30 | static ssize_t | |
9cb308ce | 31 | queue_var_show(unsigned long var, char *page) |
8324aa91 | 32 | { |
9cb308ce | 33 | return sprintf(page, "%lu\n", var); |
8324aa91 JA |
34 | } |
35 | ||
36 | static ssize_t | |
37 | queue_var_store(unsigned long *var, const char *page, size_t count) | |
38 | { | |
b1f3b64d DR |
39 | int err; |
40 | unsigned long v; | |
41 | ||
ed751e68 | 42 | err = kstrtoul(page, 10, &v); |
b1f3b64d DR |
43 | if (err || v > UINT_MAX) |
44 | return -EINVAL; | |
45 | ||
46 | *var = v; | |
8324aa91 | 47 | |
8324aa91 JA |
48 | return count; |
49 | } | |
50 | ||
80e091d1 | 51 | static ssize_t queue_var_store64(s64 *var, const char *page) |
87760e5e JA |
52 | { |
53 | int err; | |
80e091d1 | 54 | s64 v; |
87760e5e | 55 | |
80e091d1 | 56 | err = kstrtos64(page, 10, &v); |
87760e5e JA |
57 | if (err < 0) |
58 | return err; | |
59 | ||
60 | *var = v; | |
61 | return 0; | |
62 | } | |
63 | ||
8324aa91 JA |
64 | static ssize_t queue_requests_show(struct request_queue *q, char *page) |
65 | { | |
28af7428 | 66 | return queue_var_show(q->nr_requests, page); |
8324aa91 JA |
67 | } |
68 | ||
69 | static ssize_t | |
70 | queue_requests_store(struct request_queue *q, const char *page, size_t count) | |
71 | { | |
8324aa91 | 72 | unsigned long nr; |
e3a2b3f9 | 73 | int ret, err; |
b8a9ae77 | 74 | |
344e9ffc | 75 | if (!queue_is_mq(q)) |
b8a9ae77 JA |
76 | return -EINVAL; |
77 | ||
78 | ret = queue_var_store(&nr, page, count); | |
b1f3b64d DR |
79 | if (ret < 0) |
80 | return ret; | |
81 | ||
8324aa91 JA |
82 | if (nr < BLKDEV_MIN_RQ) |
83 | nr = BLKDEV_MIN_RQ; | |
84 | ||
a1ce35fa | 85 | err = blk_mq_update_nr_requests(q, nr); |
e3a2b3f9 JA |
86 | if (err) |
87 | return err; | |
88 | ||
8324aa91 JA |
89 | return ret; |
90 | } | |
91 | ||
92 | static ssize_t queue_ra_show(struct request_queue *q, char *page) | |
93 | { | |
edb0872f | 94 | unsigned long ra_kb; |
8324aa91 | 95 | |
d152c682 | 96 | if (!q->disk) |
edb0872f | 97 | return -EINVAL; |
d152c682 | 98 | ra_kb = q->disk->bdi->ra_pages << (PAGE_SHIFT - 10); |
8c390ff9 | 99 | return queue_var_show(ra_kb, page); |
8324aa91 JA |
100 | } |
101 | ||
102 | static ssize_t | |
103 | queue_ra_store(struct request_queue *q, const char *page, size_t count) | |
104 | { | |
105 | unsigned long ra_kb; | |
edb0872f | 106 | ssize_t ret; |
8324aa91 | 107 | |
d152c682 | 108 | if (!q->disk) |
edb0872f CH |
109 | return -EINVAL; |
110 | ret = queue_var_store(&ra_kb, page, count); | |
b1f3b64d DR |
111 | if (ret < 0) |
112 | return ret; | |
d152c682 | 113 | q->disk->bdi->ra_pages = ra_kb >> (PAGE_SHIFT - 10); |
8324aa91 JA |
114 | return ret; |
115 | } | |
116 | ||
117 | static ssize_t queue_max_sectors_show(struct request_queue *q, char *page) | |
118 | { | |
ae03bf63 | 119 | int max_sectors_kb = queue_max_sectors(q) >> 1; |
8324aa91 | 120 | |
8c390ff9 | 121 | return queue_var_show(max_sectors_kb, page); |
8324aa91 JA |
122 | } |
123 | ||
c77a5710 MP |
124 | static ssize_t queue_max_segments_show(struct request_queue *q, char *page) |
125 | { | |
8c390ff9 | 126 | return queue_var_show(queue_max_segments(q), page); |
c77a5710 MP |
127 | } |
128 | ||
1e739730 CH |
129 | static ssize_t queue_max_discard_segments_show(struct request_queue *q, |
130 | char *page) | |
131 | { | |
8c390ff9 | 132 | return queue_var_show(queue_max_discard_segments(q), page); |
1e739730 CH |
133 | } |
134 | ||
13f05c8d MP |
135 | static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page) |
136 | { | |
8c390ff9 | 137 | return queue_var_show(q->limits.max_integrity_segments, page); |
13f05c8d MP |
138 | } |
139 | ||
c77a5710 MP |
140 | static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page) |
141 | { | |
8c390ff9 | 142 | return queue_var_show(queue_max_segment_size(q), page); |
c77a5710 MP |
143 | } |
144 | ||
e1defc4f | 145 | static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page) |
e68b903c | 146 | { |
e1defc4f | 147 | return queue_var_show(queue_logical_block_size(q), page); |
e68b903c MP |
148 | } |
149 | ||
c72758f3 MP |
150 | static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page) |
151 | { | |
152 | return queue_var_show(queue_physical_block_size(q), page); | |
153 | } | |
154 | ||
87caf97c HR |
155 | static ssize_t queue_chunk_sectors_show(struct request_queue *q, char *page) |
156 | { | |
157 | return queue_var_show(q->limits.chunk_sectors, page); | |
158 | } | |
159 | ||
c72758f3 MP |
160 | static ssize_t queue_io_min_show(struct request_queue *q, char *page) |
161 | { | |
162 | return queue_var_show(queue_io_min(q), page); | |
163 | } | |
164 | ||
165 | static ssize_t queue_io_opt_show(struct request_queue *q, char *page) | |
166 | { | |
167 | return queue_var_show(queue_io_opt(q), page); | |
e68b903c MP |
168 | } |
169 | ||
86b37281 MP |
170 | static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page) |
171 | { | |
172 | return queue_var_show(q->limits.discard_granularity, page); | |
173 | } | |
174 | ||
0034af03 JA |
175 | static ssize_t queue_discard_max_hw_show(struct request_queue *q, char *page) |
176 | { | |
0034af03 | 177 | |
18f922d0 A |
178 | return sprintf(page, "%llu\n", |
179 | (unsigned long long)q->limits.max_hw_discard_sectors << 9); | |
0034af03 JA |
180 | } |
181 | ||
86b37281 MP |
182 | static ssize_t queue_discard_max_show(struct request_queue *q, char *page) |
183 | { | |
a934a00a MP |
184 | return sprintf(page, "%llu\n", |
185 | (unsigned long long)q->limits.max_discard_sectors << 9); | |
86b37281 MP |
186 | } |
187 | ||
0034af03 JA |
188 | static ssize_t queue_discard_max_store(struct request_queue *q, |
189 | const char *page, size_t count) | |
190 | { | |
191 | unsigned long max_discard; | |
192 | ssize_t ret = queue_var_store(&max_discard, page, count); | |
193 | ||
194 | if (ret < 0) | |
195 | return ret; | |
196 | ||
197 | if (max_discard & (q->limits.discard_granularity - 1)) | |
198 | return -EINVAL; | |
199 | ||
200 | max_discard >>= 9; | |
201 | if (max_discard > UINT_MAX) | |
202 | return -EINVAL; | |
203 | ||
204 | if (max_discard > q->limits.max_hw_discard_sectors) | |
205 | max_discard = q->limits.max_hw_discard_sectors; | |
206 | ||
207 | q->limits.max_discard_sectors = max_discard; | |
208 | return ret; | |
209 | } | |
210 | ||
98262f27 MP |
211 | static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page) |
212 | { | |
48920ff2 | 213 | return queue_var_show(0, page); |
98262f27 MP |
214 | } |
215 | ||
4363ac7c MP |
216 | static ssize_t queue_write_same_max_show(struct request_queue *q, char *page) |
217 | { | |
73bd66d9 | 218 | return queue_var_show(0, page); |
4363ac7c MP |
219 | } |
220 | ||
a6f0788e CK |
221 | static ssize_t queue_write_zeroes_max_show(struct request_queue *q, char *page) |
222 | { | |
223 | return sprintf(page, "%llu\n", | |
224 | (unsigned long long)q->limits.max_write_zeroes_sectors << 9); | |
225 | } | |
4363ac7c | 226 | |
a805a4fa DLM |
227 | static ssize_t queue_zone_write_granularity_show(struct request_queue *q, |
228 | char *page) | |
229 | { | |
230 | return queue_var_show(queue_zone_write_granularity(q), page); | |
231 | } | |
232 | ||
0512a75b KB |
233 | static ssize_t queue_zone_append_max_show(struct request_queue *q, char *page) |
234 | { | |
235 | unsigned long long max_sectors = q->limits.max_zone_append_sectors; | |
236 | ||
237 | return sprintf(page, "%llu\n", max_sectors << SECTOR_SHIFT); | |
238 | } | |
239 | ||
8324aa91 JA |
240 | static ssize_t |
241 | queue_max_sectors_store(struct request_queue *q, const char *page, size_t count) | |
242 | { | |
c9c77418 KB |
243 | unsigned long var; |
244 | unsigned int max_sectors_kb, | |
ae03bf63 | 245 | max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1, |
09cbfeaf | 246 | page_kb = 1 << (PAGE_SHIFT - 10); |
c9c77418 | 247 | ssize_t ret = queue_var_store(&var, page, count); |
8324aa91 | 248 | |
b1f3b64d DR |
249 | if (ret < 0) |
250 | return ret; | |
251 | ||
c9c77418 KB |
252 | max_sectors_kb = (unsigned int)var; |
253 | max_hw_sectors_kb = min_not_zero(max_hw_sectors_kb, | |
ca369d51 | 254 | q->limits.max_dev_sectors >> 1); |
c9c77418 KB |
255 | if (max_sectors_kb == 0) { |
256 | q->limits.max_user_sectors = 0; | |
257 | max_sectors_kb = min(max_hw_sectors_kb, | |
258 | BLK_DEF_MAX_SECTORS >> 1); | |
259 | } else { | |
260 | if (max_sectors_kb > max_hw_sectors_kb || | |
261 | max_sectors_kb < page_kb) | |
262 | return -EINVAL; | |
263 | q->limits.max_user_sectors = max_sectors_kb << 1; | |
264 | } | |
7c239517 | 265 | |
0d945c1f | 266 | spin_lock_irq(&q->queue_lock); |
c295fc05 | 267 | q->limits.max_sectors = max_sectors_kb << 1; |
d152c682 CH |
268 | if (q->disk) |
269 | q->disk->bdi->io_pages = max_sectors_kb >> (PAGE_SHIFT - 10); | |
0d945c1f | 270 | spin_unlock_irq(&q->queue_lock); |
8324aa91 JA |
271 | |
272 | return ret; | |
273 | } | |
274 | ||
275 | static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page) | |
276 | { | |
ae03bf63 | 277 | int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1; |
8324aa91 | 278 | |
8c390ff9 | 279 | return queue_var_show(max_hw_sectors_kb, page); |
8324aa91 JA |
280 | } |
281 | ||
28af7428 MG |
282 | static ssize_t queue_virt_boundary_mask_show(struct request_queue *q, char *page) |
283 | { | |
8c390ff9 | 284 | return queue_var_show(q->limits.virt_boundary_mask, page); |
28af7428 MG |
285 | } |
286 | ||
3850e13f KB |
287 | static ssize_t queue_dma_alignment_show(struct request_queue *q, char *page) |
288 | { | |
289 | return queue_var_show(queue_dma_alignment(q), page); | |
290 | } | |
291 | ||
956bcb7c JA |
292 | #define QUEUE_SYSFS_BIT_FNS(name, flag, neg) \ |
293 | static ssize_t \ | |
fc93fe14 | 294 | queue_##name##_show(struct request_queue *q, char *page) \ |
956bcb7c JA |
295 | { \ |
296 | int bit; \ | |
297 | bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags); \ | |
298 | return queue_var_show(neg ? !bit : bit, page); \ | |
299 | } \ | |
300 | static ssize_t \ | |
fc93fe14 | 301 | queue_##name##_store(struct request_queue *q, const char *page, size_t count) \ |
956bcb7c JA |
302 | { \ |
303 | unsigned long val; \ | |
304 | ssize_t ret; \ | |
305 | ret = queue_var_store(&val, page, count); \ | |
c678ef52 AB |
306 | if (ret < 0) \ |
307 | return ret; \ | |
956bcb7c JA |
308 | if (neg) \ |
309 | val = !val; \ | |
310 | \ | |
956bcb7c | 311 | if (val) \ |
8814ce8a | 312 | blk_queue_flag_set(QUEUE_FLAG_##flag, q); \ |
956bcb7c | 313 | else \ |
8814ce8a | 314 | blk_queue_flag_clear(QUEUE_FLAG_##flag, q); \ |
956bcb7c | 315 | return ret; \ |
1308835f BZ |
316 | } |
317 | ||
956bcb7c JA |
318 | QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1); |
319 | QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0); | |
320 | QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0); | |
1cb039f3 | 321 | QUEUE_SYSFS_BIT_FNS(stable_writes, STABLE_WRITES, 0); |
956bcb7c | 322 | #undef QUEUE_SYSFS_BIT_FNS |
1308835f | 323 | |
797476b8 DLM |
324 | static ssize_t queue_zoned_show(struct request_queue *q, char *page) |
325 | { | |
326 | switch (blk_queue_zoned_model(q)) { | |
327 | case BLK_ZONED_HA: | |
328 | return sprintf(page, "host-aware\n"); | |
329 | case BLK_ZONED_HM: | |
330 | return sprintf(page, "host-managed\n"); | |
331 | default: | |
332 | return sprintf(page, "none\n"); | |
333 | } | |
334 | } | |
335 | ||
965b652e DLM |
336 | static ssize_t queue_nr_zones_show(struct request_queue *q, char *page) |
337 | { | |
d86e716a | 338 | return queue_var_show(disk_nr_zones(q->disk), page); |
965b652e DLM |
339 | } |
340 | ||
e15864f8 NC |
341 | static ssize_t queue_max_open_zones_show(struct request_queue *q, char *page) |
342 | { | |
1dc01720 | 343 | return queue_var_show(bdev_max_open_zones(q->disk->part0), page); |
e15864f8 NC |
344 | } |
345 | ||
659bf827 NC |
346 | static ssize_t queue_max_active_zones_show(struct request_queue *q, char *page) |
347 | { | |
1dc01720 | 348 | return queue_var_show(bdev_max_active_zones(q->disk->part0), page); |
659bf827 NC |
349 | } |
350 | ||
ac9fafa1 AB |
351 | static ssize_t queue_nomerges_show(struct request_queue *q, char *page) |
352 | { | |
488991e2 AB |
353 | return queue_var_show((blk_queue_nomerges(q) << 1) | |
354 | blk_queue_noxmerges(q), page); | |
ac9fafa1 AB |
355 | } |
356 | ||
357 | static ssize_t queue_nomerges_store(struct request_queue *q, const char *page, | |
358 | size_t count) | |
359 | { | |
360 | unsigned long nm; | |
361 | ssize_t ret = queue_var_store(&nm, page, count); | |
362 | ||
b1f3b64d DR |
363 | if (ret < 0) |
364 | return ret; | |
365 | ||
57d74df9 CH |
366 | blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, q); |
367 | blk_queue_flag_clear(QUEUE_FLAG_NOXMERGES, q); | |
488991e2 | 368 | if (nm == 2) |
57d74df9 | 369 | blk_queue_flag_set(QUEUE_FLAG_NOMERGES, q); |
488991e2 | 370 | else if (nm) |
57d74df9 | 371 | blk_queue_flag_set(QUEUE_FLAG_NOXMERGES, q); |
1308835f | 372 | |
ac9fafa1 AB |
373 | return ret; |
374 | } | |
375 | ||
c7c22e4d JA |
376 | static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page) |
377 | { | |
9cb308ce | 378 | bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags); |
5757a6d7 | 379 | bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags); |
c7c22e4d | 380 | |
5757a6d7 | 381 | return queue_var_show(set << force, page); |
c7c22e4d JA |
382 | } |
383 | ||
384 | static ssize_t | |
385 | queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count) | |
386 | { | |
387 | ssize_t ret = -EINVAL; | |
0a06ff06 | 388 | #ifdef CONFIG_SMP |
c7c22e4d JA |
389 | unsigned long val; |
390 | ||
391 | ret = queue_var_store(&val, page, count); | |
b1f3b64d DR |
392 | if (ret < 0) |
393 | return ret; | |
394 | ||
e8037d49 | 395 | if (val == 2) { |
57d74df9 CH |
396 | blk_queue_flag_set(QUEUE_FLAG_SAME_COMP, q); |
397 | blk_queue_flag_set(QUEUE_FLAG_SAME_FORCE, q); | |
e8037d49 | 398 | } else if (val == 1) { |
57d74df9 CH |
399 | blk_queue_flag_set(QUEUE_FLAG_SAME_COMP, q); |
400 | blk_queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q); | |
e8037d49 | 401 | } else if (val == 0) { |
57d74df9 CH |
402 | blk_queue_flag_clear(QUEUE_FLAG_SAME_COMP, q); |
403 | blk_queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q); | |
5757a6d7 | 404 | } |
c7c22e4d JA |
405 | #endif |
406 | return ret; | |
407 | } | |
8324aa91 | 408 | |
06426adf JA |
409 | static ssize_t queue_poll_delay_show(struct request_queue *q, char *page) |
410 | { | |
64f1c21e JA |
411 | int val; |
412 | ||
29ece8b4 YY |
413 | if (q->poll_nsec == BLK_MQ_POLL_CLASSIC) |
414 | val = BLK_MQ_POLL_CLASSIC; | |
64f1c21e JA |
415 | else |
416 | val = q->poll_nsec / 1000; | |
417 | ||
418 | return sprintf(page, "%d\n", val); | |
06426adf JA |
419 | } |
420 | ||
421 | static ssize_t queue_poll_delay_store(struct request_queue *q, const char *page, | |
422 | size_t count) | |
423 | { | |
64f1c21e | 424 | int err, val; |
06426adf JA |
425 | |
426 | if (!q->mq_ops || !q->mq_ops->poll) | |
427 | return -EINVAL; | |
428 | ||
64f1c21e JA |
429 | err = kstrtoint(page, 10, &val); |
430 | if (err < 0) | |
431 | return err; | |
06426adf | 432 | |
29ece8b4 YY |
433 | if (val == BLK_MQ_POLL_CLASSIC) |
434 | q->poll_nsec = BLK_MQ_POLL_CLASSIC; | |
435 | else if (val >= 0) | |
64f1c21e | 436 | q->poll_nsec = val * 1000; |
29ece8b4 YY |
437 | else |
438 | return -EINVAL; | |
64f1c21e JA |
439 | |
440 | return count; | |
06426adf JA |
441 | } |
442 | ||
05229bee JA |
443 | static ssize_t queue_poll_show(struct request_queue *q, char *page) |
444 | { | |
445 | return queue_var_show(test_bit(QUEUE_FLAG_POLL, &q->queue_flags), page); | |
446 | } | |
447 | ||
448 | static ssize_t queue_poll_store(struct request_queue *q, const char *page, | |
449 | size_t count) | |
450 | { | |
a614dd22 | 451 | if (!test_bit(QUEUE_FLAG_POLL, &q->queue_flags)) |
05229bee | 452 | return -EINVAL; |
a614dd22 CH |
453 | pr_info_ratelimited("writes to the poll attribute are ignored.\n"); |
454 | pr_info_ratelimited("please use driver specific parameters instead.\n"); | |
455 | return count; | |
05229bee JA |
456 | } |
457 | ||
65cd1d13 WZ |
458 | static ssize_t queue_io_timeout_show(struct request_queue *q, char *page) |
459 | { | |
460 | return sprintf(page, "%u\n", jiffies_to_msecs(q->rq_timeout)); | |
461 | } | |
462 | ||
463 | static ssize_t queue_io_timeout_store(struct request_queue *q, const char *page, | |
464 | size_t count) | |
465 | { | |
466 | unsigned int val; | |
467 | int err; | |
468 | ||
469 | err = kstrtou32(page, 10, &val); | |
470 | if (err || val == 0) | |
471 | return -EINVAL; | |
472 | ||
473 | blk_queue_rq_timeout(q, msecs_to_jiffies(val)); | |
474 | ||
475 | return count; | |
476 | } | |
477 | ||
87760e5e JA |
478 | static ssize_t queue_wb_lat_show(struct request_queue *q, char *page) |
479 | { | |
a7905043 | 480 | if (!wbt_rq_qos(q)) |
87760e5e JA |
481 | return -EINVAL; |
482 | ||
3642ef4d YK |
483 | if (wbt_disabled(q)) |
484 | return sprintf(page, "0\n"); | |
485 | ||
a7905043 | 486 | return sprintf(page, "%llu\n", div_u64(wbt_get_min_lat(q), 1000)); |
87760e5e JA |
487 | } |
488 | ||
489 | static ssize_t queue_wb_lat_store(struct request_queue *q, const char *page, | |
490 | size_t count) | |
491 | { | |
a7905043 | 492 | struct rq_qos *rqos; |
87760e5e | 493 | ssize_t ret; |
80e091d1 | 494 | s64 val; |
87760e5e | 495 | |
87760e5e JA |
496 | ret = queue_var_store64(&val, page); |
497 | if (ret < 0) | |
498 | return ret; | |
d62118b6 JA |
499 | if (val < -1) |
500 | return -EINVAL; | |
501 | ||
a7905043 JB |
502 | rqos = wbt_rq_qos(q); |
503 | if (!rqos) { | |
958f2965 | 504 | ret = wbt_init(q->disk); |
d62118b6 JA |
505 | if (ret) |
506 | return ret; | |
d62118b6 | 507 | } |
87760e5e | 508 | |
80e091d1 | 509 | if (val == -1) |
a7905043 | 510 | val = wbt_default_latency_nsec(q); |
80e091d1 | 511 | else if (val >= 0) |
a7905043 | 512 | val *= 1000ULL; |
d62118b6 | 513 | |
b7143fe6 AZ |
514 | if (wbt_get_min_lat(q) == val) |
515 | return count; | |
516 | ||
c125311d JA |
517 | /* |
518 | * Ensure that the queue is idled, in case the latency update | |
519 | * ends up either enabling or disabling wbt completely. We can't | |
520 | * have IO inflight if that happens. | |
521 | */ | |
a1ce35fa JA |
522 | blk_mq_freeze_queue(q); |
523 | blk_mq_quiesce_queue(q); | |
80e091d1 | 524 | |
c125311d | 525 | wbt_set_min_lat(q, val); |
c125311d | 526 | |
a1ce35fa JA |
527 | blk_mq_unquiesce_queue(q); |
528 | blk_mq_unfreeze_queue(q); | |
c125311d | 529 | |
87760e5e JA |
530 | return count; |
531 | } | |
532 | ||
93e9d8e8 JA |
533 | static ssize_t queue_wc_show(struct request_queue *q, char *page) |
534 | { | |
535 | if (test_bit(QUEUE_FLAG_WC, &q->queue_flags)) | |
536 | return sprintf(page, "write back\n"); | |
537 | ||
538 | return sprintf(page, "write through\n"); | |
539 | } | |
540 | ||
541 | static ssize_t queue_wc_store(struct request_queue *q, const char *page, | |
542 | size_t count) | |
543 | { | |
544 | int set = -1; | |
545 | ||
546 | if (!strncmp(page, "write back", 10)) | |
547 | set = 1; | |
548 | else if (!strncmp(page, "write through", 13) || | |
549 | !strncmp(page, "none", 4)) | |
550 | set = 0; | |
551 | ||
552 | if (set == -1) | |
553 | return -EINVAL; | |
554 | ||
93e9d8e8 | 555 | if (set) |
8814ce8a | 556 | blk_queue_flag_set(QUEUE_FLAG_WC, q); |
93e9d8e8 | 557 | else |
8814ce8a | 558 | blk_queue_flag_clear(QUEUE_FLAG_WC, q); |
93e9d8e8 JA |
559 | |
560 | return count; | |
561 | } | |
562 | ||
6fcefbe5 KO |
563 | static ssize_t queue_fua_show(struct request_queue *q, char *page) |
564 | { | |
565 | return sprintf(page, "%u\n", test_bit(QUEUE_FLAG_FUA, &q->queue_flags)); | |
566 | } | |
567 | ||
ea6ca600 YK |
568 | static ssize_t queue_dax_show(struct request_queue *q, char *page) |
569 | { | |
570 | return queue_var_show(blk_queue_dax(q), page); | |
571 | } | |
572 | ||
35626147 CH |
573 | #define QUEUE_RO_ENTRY(_prefix, _name) \ |
574 | static struct queue_sysfs_entry _prefix##_entry = { \ | |
575 | .attr = { .name = _name, .mode = 0444 }, \ | |
576 | .show = _prefix##_show, \ | |
577 | }; | |
578 | ||
579 | #define QUEUE_RW_ENTRY(_prefix, _name) \ | |
580 | static struct queue_sysfs_entry _prefix##_entry = { \ | |
581 | .attr = { .name = _name, .mode = 0644 }, \ | |
582 | .show = _prefix##_show, \ | |
583 | .store = _prefix##_store, \ | |
584 | }; | |
585 | ||
586 | QUEUE_RW_ENTRY(queue_requests, "nr_requests"); | |
587 | QUEUE_RW_ENTRY(queue_ra, "read_ahead_kb"); | |
588 | QUEUE_RW_ENTRY(queue_max_sectors, "max_sectors_kb"); | |
589 | QUEUE_RO_ENTRY(queue_max_hw_sectors, "max_hw_sectors_kb"); | |
590 | QUEUE_RO_ENTRY(queue_max_segments, "max_segments"); | |
591 | QUEUE_RO_ENTRY(queue_max_integrity_segments, "max_integrity_segments"); | |
592 | QUEUE_RO_ENTRY(queue_max_segment_size, "max_segment_size"); | |
593 | QUEUE_RW_ENTRY(elv_iosched, "scheduler"); | |
594 | ||
595 | QUEUE_RO_ENTRY(queue_logical_block_size, "logical_block_size"); | |
596 | QUEUE_RO_ENTRY(queue_physical_block_size, "physical_block_size"); | |
597 | QUEUE_RO_ENTRY(queue_chunk_sectors, "chunk_sectors"); | |
598 | QUEUE_RO_ENTRY(queue_io_min, "minimum_io_size"); | |
599 | QUEUE_RO_ENTRY(queue_io_opt, "optimal_io_size"); | |
600 | ||
601 | QUEUE_RO_ENTRY(queue_max_discard_segments, "max_discard_segments"); | |
602 | QUEUE_RO_ENTRY(queue_discard_granularity, "discard_granularity"); | |
603 | QUEUE_RO_ENTRY(queue_discard_max_hw, "discard_max_hw_bytes"); | |
604 | QUEUE_RW_ENTRY(queue_discard_max, "discard_max_bytes"); | |
605 | QUEUE_RO_ENTRY(queue_discard_zeroes_data, "discard_zeroes_data"); | |
606 | ||
607 | QUEUE_RO_ENTRY(queue_write_same_max, "write_same_max_bytes"); | |
608 | QUEUE_RO_ENTRY(queue_write_zeroes_max, "write_zeroes_max_bytes"); | |
609 | QUEUE_RO_ENTRY(queue_zone_append_max, "zone_append_max_bytes"); | |
a805a4fa | 610 | QUEUE_RO_ENTRY(queue_zone_write_granularity, "zone_write_granularity"); |
35626147 CH |
611 | |
612 | QUEUE_RO_ENTRY(queue_zoned, "zoned"); | |
613 | QUEUE_RO_ENTRY(queue_nr_zones, "nr_zones"); | |
614 | QUEUE_RO_ENTRY(queue_max_open_zones, "max_open_zones"); | |
615 | QUEUE_RO_ENTRY(queue_max_active_zones, "max_active_zones"); | |
616 | ||
617 | QUEUE_RW_ENTRY(queue_nomerges, "nomerges"); | |
618 | QUEUE_RW_ENTRY(queue_rq_affinity, "rq_affinity"); | |
619 | QUEUE_RW_ENTRY(queue_poll, "io_poll"); | |
620 | QUEUE_RW_ENTRY(queue_poll_delay, "io_poll_delay"); | |
621 | QUEUE_RW_ENTRY(queue_wc, "write_cache"); | |
622 | QUEUE_RO_ENTRY(queue_fua, "fua"); | |
623 | QUEUE_RO_ENTRY(queue_dax, "dax"); | |
624 | QUEUE_RW_ENTRY(queue_io_timeout, "io_timeout"); | |
625 | QUEUE_RW_ENTRY(queue_wb_lat, "wbt_lat_usec"); | |
28af7428 | 626 | QUEUE_RO_ENTRY(queue_virt_boundary_mask, "virt_boundary_mask"); |
3850e13f | 627 | QUEUE_RO_ENTRY(queue_dma_alignment, "dma_alignment"); |
8324aa91 | 628 | |
35626147 CH |
629 | #ifdef CONFIG_BLK_DEV_THROTTLING_LOW |
630 | QUEUE_RW_ENTRY(blk_throtl_sample_time, "throttle_sample_time"); | |
631 | #endif | |
8324aa91 | 632 | |
35626147 | 633 | /* legacy alias for logical_block_size: */ |
e68b903c | 634 | static struct queue_sysfs_entry queue_hw_sector_size_entry = { |
5657a819 | 635 | .attr = {.name = "hw_sector_size", .mode = 0444 }, |
e1defc4f MP |
636 | .show = queue_logical_block_size_show, |
637 | }; | |
638 | ||
fc93fe14 CH |
639 | QUEUE_RW_ENTRY(queue_nonrot, "rotational"); |
640 | QUEUE_RW_ENTRY(queue_iostats, "iostats"); | |
641 | QUEUE_RW_ENTRY(queue_random, "add_random"); | |
1cb039f3 | 642 | QUEUE_RW_ENTRY(queue_stable_writes, "stable_writes"); |
e2e1a148 | 643 | |
4d25339e | 644 | static struct attribute *queue_attrs[] = { |
8324aa91 JA |
645 | &queue_requests_entry.attr, |
646 | &queue_ra_entry.attr, | |
647 | &queue_max_hw_sectors_entry.attr, | |
648 | &queue_max_sectors_entry.attr, | |
c77a5710 | 649 | &queue_max_segments_entry.attr, |
1e739730 | 650 | &queue_max_discard_segments_entry.attr, |
13f05c8d | 651 | &queue_max_integrity_segments_entry.attr, |
c77a5710 | 652 | &queue_max_segment_size_entry.attr, |
35626147 | 653 | &elv_iosched_entry.attr, |
e68b903c | 654 | &queue_hw_sector_size_entry.attr, |
e1defc4f | 655 | &queue_logical_block_size_entry.attr, |
c72758f3 | 656 | &queue_physical_block_size_entry.attr, |
87caf97c | 657 | &queue_chunk_sectors_entry.attr, |
c72758f3 MP |
658 | &queue_io_min_entry.attr, |
659 | &queue_io_opt_entry.attr, | |
86b37281 MP |
660 | &queue_discard_granularity_entry.attr, |
661 | &queue_discard_max_entry.attr, | |
0034af03 | 662 | &queue_discard_max_hw_entry.attr, |
98262f27 | 663 | &queue_discard_zeroes_data_entry.attr, |
4363ac7c | 664 | &queue_write_same_max_entry.attr, |
a6f0788e | 665 | &queue_write_zeroes_max_entry.attr, |
0512a75b | 666 | &queue_zone_append_max_entry.attr, |
a805a4fa | 667 | &queue_zone_write_granularity_entry.attr, |
1308835f | 668 | &queue_nonrot_entry.attr, |
797476b8 | 669 | &queue_zoned_entry.attr, |
965b652e | 670 | &queue_nr_zones_entry.attr, |
e15864f8 | 671 | &queue_max_open_zones_entry.attr, |
659bf827 | 672 | &queue_max_active_zones_entry.attr, |
ac9fafa1 | 673 | &queue_nomerges_entry.attr, |
c7c22e4d | 674 | &queue_rq_affinity_entry.attr, |
bc58ba94 | 675 | &queue_iostats_entry.attr, |
1cb039f3 | 676 | &queue_stable_writes_entry.attr, |
e2e1a148 | 677 | &queue_random_entry.attr, |
05229bee | 678 | &queue_poll_entry.attr, |
93e9d8e8 | 679 | &queue_wc_entry.attr, |
6fcefbe5 | 680 | &queue_fua_entry.attr, |
ea6ca600 | 681 | &queue_dax_entry.attr, |
87760e5e | 682 | &queue_wb_lat_entry.attr, |
06426adf | 683 | &queue_poll_delay_entry.attr, |
65cd1d13 | 684 | &queue_io_timeout_entry.attr, |
297e3d85 | 685 | #ifdef CONFIG_BLK_DEV_THROTTLING_LOW |
35626147 | 686 | &blk_throtl_sample_time_entry.attr, |
297e3d85 | 687 | #endif |
28af7428 | 688 | &queue_virt_boundary_mask_entry.attr, |
3850e13f | 689 | &queue_dma_alignment_entry.attr, |
8324aa91 JA |
690 | NULL, |
691 | }; | |
692 | ||
4d25339e WZ |
693 | static umode_t queue_attr_visible(struct kobject *kobj, struct attribute *attr, |
694 | int n) | |
695 | { | |
2bd85221 CH |
696 | struct gendisk *disk = container_of(kobj, struct gendisk, queue_kobj); |
697 | struct request_queue *q = disk->queue; | |
4d25339e WZ |
698 | |
699 | if (attr == &queue_io_timeout_entry.attr && | |
700 | (!q->mq_ops || !q->mq_ops->timeout)) | |
701 | return 0; | |
702 | ||
659bf827 NC |
703 | if ((attr == &queue_max_open_zones_entry.attr || |
704 | attr == &queue_max_active_zones_entry.attr) && | |
e15864f8 NC |
705 | !blk_queue_is_zoned(q)) |
706 | return 0; | |
707 | ||
4d25339e WZ |
708 | return attr->mode; |
709 | } | |
710 | ||
711 | static struct attribute_group queue_attr_group = { | |
712 | .attrs = queue_attrs, | |
713 | .is_visible = queue_attr_visible, | |
714 | }; | |
715 | ||
716 | ||
8324aa91 JA |
717 | #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr) |
718 | ||
719 | static ssize_t | |
720 | queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page) | |
721 | { | |
722 | struct queue_sysfs_entry *entry = to_queue(attr); | |
2bd85221 CH |
723 | struct gendisk *disk = container_of(kobj, struct gendisk, queue_kobj); |
724 | struct request_queue *q = disk->queue; | |
8324aa91 JA |
725 | ssize_t res; |
726 | ||
727 | if (!entry->show) | |
728 | return -EIO; | |
729 | mutex_lock(&q->sysfs_lock); | |
8324aa91 JA |
730 | res = entry->show(q, page); |
731 | mutex_unlock(&q->sysfs_lock); | |
732 | return res; | |
733 | } | |
734 | ||
735 | static ssize_t | |
736 | queue_attr_store(struct kobject *kobj, struct attribute *attr, | |
737 | const char *page, size_t length) | |
738 | { | |
739 | struct queue_sysfs_entry *entry = to_queue(attr); | |
2bd85221 CH |
740 | struct gendisk *disk = container_of(kobj, struct gendisk, queue_kobj); |
741 | struct request_queue *q = disk->queue; | |
8324aa91 JA |
742 | ssize_t res; |
743 | ||
744 | if (!entry->store) | |
745 | return -EIO; | |
6728cb0e | 746 | |
8324aa91 | 747 | mutex_lock(&q->sysfs_lock); |
8324aa91 JA |
748 | res = entry->store(q, page, length); |
749 | mutex_unlock(&q->sysfs_lock); | |
750 | return res; | |
751 | } | |
752 | ||
52cf25d0 | 753 | static const struct sysfs_ops queue_sysfs_ops = { |
8324aa91 JA |
754 | .show = queue_attr_show, |
755 | .store = queue_attr_store, | |
756 | }; | |
757 | ||
4a8d14bb CH |
758 | static const struct attribute_group *blk_queue_attr_groups[] = { |
759 | &queue_attr_group, | |
760 | NULL | |
761 | }; | |
762 | ||
2bd85221 CH |
763 | static void blk_queue_release(struct kobject *kobj) |
764 | { | |
765 | /* nothing to do here, all data is associated with the parent gendisk */ | |
766 | } | |
767 | ||
5f622417 | 768 | static const struct kobj_type blk_queue_ktype = { |
4a8d14bb | 769 | .default_groups = blk_queue_attr_groups, |
8324aa91 | 770 | .sysfs_ops = &queue_sysfs_ops, |
2bd85221 | 771 | .release = blk_queue_release, |
8324aa91 JA |
772 | }; |
773 | ||
6fc75f30 CH |
774 | static void blk_debugfs_remove(struct gendisk *disk) |
775 | { | |
776 | struct request_queue *q = disk->queue; | |
777 | ||
778 | mutex_lock(&q->debugfs_mutex); | |
779 | blk_trace_shutdown(q); | |
780 | debugfs_remove_recursive(q->debugfs_dir); | |
781 | q->debugfs_dir = NULL; | |
782 | q->sched_debugfs_dir = NULL; | |
783 | q->rqos_debugfs_dir = NULL; | |
784 | mutex_unlock(&q->debugfs_mutex); | |
785 | } | |
786 | ||
2c2086af BVA |
787 | /** |
788 | * blk_register_queue - register a block layer queue with sysfs | |
789 | * @disk: Disk of which the request queue should be registered with sysfs. | |
790 | */ | |
8324aa91 JA |
791 | int blk_register_queue(struct gendisk *disk) |
792 | { | |
8324aa91 | 793 | struct request_queue *q = disk->queue; |
8682b92e | 794 | int ret; |
8324aa91 | 795 | |
cecf5d87 | 796 | mutex_lock(&q->sysfs_dir_lock); |
2bd85221 CH |
797 | kobject_init(&disk->queue_kobj, &blk_queue_ktype); |
798 | ret = kobject_add(&disk->queue_kobj, &disk_to_dev(disk)->kobj, "queue"); | |
cc5c516d | 799 | if (ret < 0) |
2bd85221 | 800 | goto out_put_queue_kobj; |
8324aa91 | 801 | |
40602997 CH |
802 | if (queue_is_mq(q)) { |
803 | ret = blk_mq_sysfs_register(disk); | |
804 | if (ret) | |
2bd85221 | 805 | goto out_put_queue_kobj; |
40602997 | 806 | } |
5cf9c91b CH |
807 | mutex_lock(&q->sysfs_lock); |
808 | ||
85e0cbbb | 809 | mutex_lock(&q->debugfs_mutex); |
2bd85221 | 810 | q->debugfs_dir = debugfs_create_dir(disk->disk_name, blk_debugfs_root); |
5cf9c91b | 811 | if (queue_is_mq(q)) |
a8ecdd71 | 812 | blk_mq_debugfs_register(q); |
5cf9c91b | 813 | mutex_unlock(&q->debugfs_mutex); |
a2247f19 | 814 | |
22d0c408 | 815 | ret = disk_register_independent_access_ranges(disk); |
a2247f19 | 816 | if (ret) |
40602997 | 817 | goto out_debugfs_remove; |
a2247f19 | 818 | |
344e9ffc | 819 | if (q->elevator) { |
cecf5d87 | 820 | ret = elv_register_queue(q, false); |
a2247f19 | 821 | if (ret) |
40602997 | 822 | goto out_unregister_ia_ranges; |
8324aa91 | 823 | } |
cecf5d87 | 824 | |
450deb93 | 825 | ret = blk_crypto_sysfs_register(disk); |
20f01f16 | 826 | if (ret) |
40602997 | 827 | goto out_elv_unregister; |
20f01f16 | 828 | |
cecf5d87 | 829 | blk_queue_flag_set(QUEUE_FLAG_REGISTERED, q); |
04aad37b | 830 | wbt_enable_default(disk); |
5f6dc752 | 831 | blk_throtl_register(disk); |
cecf5d87 ML |
832 | |
833 | /* Now everything is ready and send out KOBJ_ADD uevent */ | |
2bd85221 | 834 | kobject_uevent(&disk->queue_kobj, KOBJ_ADD); |
0546858c | 835 | if (q->elevator) |
cecf5d87 ML |
836 | kobject_uevent(&q->elevator->kobj, KOBJ_ADD); |
837 | mutex_unlock(&q->sysfs_lock); | |
cecf5d87 | 838 | mutex_unlock(&q->sysfs_dir_lock); |
a72c374f ML |
839 | |
840 | /* | |
841 | * SCSI probing may synchronously create and destroy a lot of | |
842 | * request_queues for non-existent devices. Shutting down a fully | |
843 | * functional queue takes measureable wallclock time as RCU grace | |
844 | * periods are involved. To avoid excessive latency in these | |
845 | * cases, a request_queue starts out in a degraded mode which is | |
846 | * faster to shut down and is made fully functional here as | |
847 | * request_queues for non-existent devices never get registered. | |
848 | */ | |
849 | if (!blk_queue_init_done(q)) { | |
850 | blk_queue_flag_set(QUEUE_FLAG_INIT_DONE, q); | |
851 | percpu_ref_switch_to_percpu(&q->q_usage_counter); | |
852 | } | |
853 | ||
a2247f19 DLM |
854 | return ret; |
855 | ||
40602997 | 856 | out_elv_unregister: |
20f01f16 | 857 | elv_unregister_queue(q); |
40602997 | 858 | out_unregister_ia_ranges: |
a2247f19 | 859 | disk_unregister_independent_access_ranges(disk); |
40602997 CH |
860 | out_debugfs_remove: |
861 | blk_debugfs_remove(disk); | |
a2247f19 | 862 | mutex_unlock(&q->sysfs_lock); |
2bd85221 CH |
863 | out_put_queue_kobj: |
864 | kobject_put(&disk->queue_kobj); | |
40602997 | 865 | mutex_unlock(&q->sysfs_dir_lock); |
b410aff2 | 866 | return ret; |
8324aa91 JA |
867 | } |
868 | ||
2c2086af BVA |
869 | /** |
870 | * blk_unregister_queue - counterpart of blk_register_queue() | |
871 | * @disk: Disk of which the request queue should be unregistered from sysfs. | |
872 | * | |
873 | * Note: the caller is responsible for guaranteeing that this function is called | |
874 | * after blk_register_queue() has finished. | |
875 | */ | |
8324aa91 JA |
876 | void blk_unregister_queue(struct gendisk *disk) |
877 | { | |
878 | struct request_queue *q = disk->queue; | |
879 | ||
fb199746 AM |
880 | if (WARN_ON(!q)) |
881 | return; | |
882 | ||
fa70d2e2 | 883 | /* Return early if disk->queue was never registered. */ |
58c898ba | 884 | if (!blk_queue_registered(q)) |
fa70d2e2 MS |
885 | return; |
886 | ||
667257e8 | 887 | /* |
2c2086af BVA |
888 | * Since sysfs_remove_dir() prevents adding new directory entries |
889 | * before removal of existing entries starts, protect against | |
890 | * concurrent elv_iosched_store() calls. | |
667257e8 | 891 | */ |
e9a823fb | 892 | mutex_lock(&q->sysfs_lock); |
8814ce8a | 893 | blk_queue_flag_clear(QUEUE_FLAG_REGISTERED, q); |
cecf5d87 | 894 | mutex_unlock(&q->sysfs_lock); |
02ba8893 | 895 | |
cecf5d87 | 896 | mutex_lock(&q->sysfs_dir_lock); |
2c2086af BVA |
897 | /* |
898 | * Remove the sysfs attributes before unregistering the queue data | |
899 | * structures that can be modified through sysfs. | |
900 | */ | |
344e9ffc | 901 | if (queue_is_mq(q)) |
8682b92e | 902 | blk_mq_sysfs_unregister(disk); |
450deb93 | 903 | blk_crypto_sysfs_unregister(disk); |
667257e8 | 904 | |
b89f625e | 905 | mutex_lock(&q->sysfs_lock); |
f5ec592d | 906 | elv_unregister_queue(q); |
a2247f19 | 907 | disk_unregister_independent_access_ranges(disk); |
b89f625e | 908 | mutex_unlock(&q->sysfs_lock); |
0f692882 EB |
909 | |
910 | /* Now that we've deleted all child objects, we can delete the queue. */ | |
2bd85221 CH |
911 | kobject_uevent(&disk->queue_kobj, KOBJ_REMOVE); |
912 | kobject_del(&disk->queue_kobj); | |
cecf5d87 | 913 | mutex_unlock(&q->sysfs_dir_lock); |
2c2086af | 914 | |
6fc75f30 | 915 | blk_debugfs_remove(disk); |
8324aa91 | 916 | } |