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