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 | { | |
54bdd67d | 411 | return sprintf(page, "%d\n", -1); |
06426adf JA |
412 | } |
413 | ||
414 | static ssize_t queue_poll_delay_store(struct request_queue *q, const char *page, | |
415 | size_t count) | |
416 | { | |
64f1c21e | 417 | return count; |
06426adf JA |
418 | } |
419 | ||
05229bee JA |
420 | static ssize_t queue_poll_show(struct request_queue *q, char *page) |
421 | { | |
422 | return queue_var_show(test_bit(QUEUE_FLAG_POLL, &q->queue_flags), page); | |
423 | } | |
424 | ||
425 | static ssize_t queue_poll_store(struct request_queue *q, const char *page, | |
426 | size_t count) | |
427 | { | |
a614dd22 | 428 | if (!test_bit(QUEUE_FLAG_POLL, &q->queue_flags)) |
05229bee | 429 | return -EINVAL; |
a614dd22 CH |
430 | pr_info_ratelimited("writes to the poll attribute are ignored.\n"); |
431 | pr_info_ratelimited("please use driver specific parameters instead.\n"); | |
432 | return count; | |
05229bee JA |
433 | } |
434 | ||
65cd1d13 WZ |
435 | static ssize_t queue_io_timeout_show(struct request_queue *q, char *page) |
436 | { | |
437 | return sprintf(page, "%u\n", jiffies_to_msecs(q->rq_timeout)); | |
438 | } | |
439 | ||
440 | static ssize_t queue_io_timeout_store(struct request_queue *q, const char *page, | |
441 | size_t count) | |
442 | { | |
443 | unsigned int val; | |
444 | int err; | |
445 | ||
446 | err = kstrtou32(page, 10, &val); | |
447 | if (err || val == 0) | |
448 | return -EINVAL; | |
449 | ||
450 | blk_queue_rq_timeout(q, msecs_to_jiffies(val)); | |
451 | ||
452 | return count; | |
453 | } | |
454 | ||
87760e5e JA |
455 | static ssize_t queue_wb_lat_show(struct request_queue *q, char *page) |
456 | { | |
a7905043 | 457 | if (!wbt_rq_qos(q)) |
87760e5e JA |
458 | return -EINVAL; |
459 | ||
3642ef4d YK |
460 | if (wbt_disabled(q)) |
461 | return sprintf(page, "0\n"); | |
462 | ||
a7905043 | 463 | return sprintf(page, "%llu\n", div_u64(wbt_get_min_lat(q), 1000)); |
87760e5e JA |
464 | } |
465 | ||
466 | static ssize_t queue_wb_lat_store(struct request_queue *q, const char *page, | |
467 | size_t count) | |
468 | { | |
a7905043 | 469 | struct rq_qos *rqos; |
87760e5e | 470 | ssize_t ret; |
80e091d1 | 471 | s64 val; |
87760e5e | 472 | |
87760e5e JA |
473 | ret = queue_var_store64(&val, page); |
474 | if (ret < 0) | |
475 | return ret; | |
d62118b6 JA |
476 | if (val < -1) |
477 | return -EINVAL; | |
478 | ||
a7905043 JB |
479 | rqos = wbt_rq_qos(q); |
480 | if (!rqos) { | |
958f2965 | 481 | ret = wbt_init(q->disk); |
d62118b6 JA |
482 | if (ret) |
483 | return ret; | |
d62118b6 | 484 | } |
87760e5e | 485 | |
80e091d1 | 486 | if (val == -1) |
a7905043 | 487 | val = wbt_default_latency_nsec(q); |
80e091d1 | 488 | else if (val >= 0) |
a7905043 | 489 | val *= 1000ULL; |
d62118b6 | 490 | |
b7143fe6 AZ |
491 | if (wbt_get_min_lat(q) == val) |
492 | return count; | |
493 | ||
c125311d JA |
494 | /* |
495 | * Ensure that the queue is idled, in case the latency update | |
496 | * ends up either enabling or disabling wbt completely. We can't | |
497 | * have IO inflight if that happens. | |
498 | */ | |
a1ce35fa JA |
499 | blk_mq_freeze_queue(q); |
500 | blk_mq_quiesce_queue(q); | |
80e091d1 | 501 | |
c125311d | 502 | wbt_set_min_lat(q, val); |
c125311d | 503 | |
a1ce35fa JA |
504 | blk_mq_unquiesce_queue(q); |
505 | blk_mq_unfreeze_queue(q); | |
c125311d | 506 | |
87760e5e JA |
507 | return count; |
508 | } | |
509 | ||
93e9d8e8 JA |
510 | static ssize_t queue_wc_show(struct request_queue *q, char *page) |
511 | { | |
512 | if (test_bit(QUEUE_FLAG_WC, &q->queue_flags)) | |
513 | return sprintf(page, "write back\n"); | |
514 | ||
515 | return sprintf(page, "write through\n"); | |
516 | } | |
517 | ||
518 | static ssize_t queue_wc_store(struct request_queue *q, const char *page, | |
519 | size_t count) | |
520 | { | |
521 | int set = -1; | |
522 | ||
523 | if (!strncmp(page, "write back", 10)) | |
524 | set = 1; | |
525 | else if (!strncmp(page, "write through", 13) || | |
526 | !strncmp(page, "none", 4)) | |
527 | set = 0; | |
528 | ||
529 | if (set == -1) | |
530 | return -EINVAL; | |
531 | ||
93e9d8e8 | 532 | if (set) |
8814ce8a | 533 | blk_queue_flag_set(QUEUE_FLAG_WC, q); |
93e9d8e8 | 534 | else |
8814ce8a | 535 | blk_queue_flag_clear(QUEUE_FLAG_WC, q); |
93e9d8e8 JA |
536 | |
537 | return count; | |
538 | } | |
539 | ||
6fcefbe5 KO |
540 | static ssize_t queue_fua_show(struct request_queue *q, char *page) |
541 | { | |
542 | return sprintf(page, "%u\n", test_bit(QUEUE_FLAG_FUA, &q->queue_flags)); | |
543 | } | |
544 | ||
ea6ca600 YK |
545 | static ssize_t queue_dax_show(struct request_queue *q, char *page) |
546 | { | |
547 | return queue_var_show(blk_queue_dax(q), page); | |
548 | } | |
549 | ||
35626147 CH |
550 | #define QUEUE_RO_ENTRY(_prefix, _name) \ |
551 | static struct queue_sysfs_entry _prefix##_entry = { \ | |
552 | .attr = { .name = _name, .mode = 0444 }, \ | |
553 | .show = _prefix##_show, \ | |
554 | }; | |
555 | ||
556 | #define QUEUE_RW_ENTRY(_prefix, _name) \ | |
557 | static struct queue_sysfs_entry _prefix##_entry = { \ | |
558 | .attr = { .name = _name, .mode = 0644 }, \ | |
559 | .show = _prefix##_show, \ | |
560 | .store = _prefix##_store, \ | |
561 | }; | |
562 | ||
563 | QUEUE_RW_ENTRY(queue_requests, "nr_requests"); | |
564 | QUEUE_RW_ENTRY(queue_ra, "read_ahead_kb"); | |
565 | QUEUE_RW_ENTRY(queue_max_sectors, "max_sectors_kb"); | |
566 | QUEUE_RO_ENTRY(queue_max_hw_sectors, "max_hw_sectors_kb"); | |
567 | QUEUE_RO_ENTRY(queue_max_segments, "max_segments"); | |
568 | QUEUE_RO_ENTRY(queue_max_integrity_segments, "max_integrity_segments"); | |
569 | QUEUE_RO_ENTRY(queue_max_segment_size, "max_segment_size"); | |
570 | QUEUE_RW_ENTRY(elv_iosched, "scheduler"); | |
571 | ||
572 | QUEUE_RO_ENTRY(queue_logical_block_size, "logical_block_size"); | |
573 | QUEUE_RO_ENTRY(queue_physical_block_size, "physical_block_size"); | |
574 | QUEUE_RO_ENTRY(queue_chunk_sectors, "chunk_sectors"); | |
575 | QUEUE_RO_ENTRY(queue_io_min, "minimum_io_size"); | |
576 | QUEUE_RO_ENTRY(queue_io_opt, "optimal_io_size"); | |
577 | ||
578 | QUEUE_RO_ENTRY(queue_max_discard_segments, "max_discard_segments"); | |
579 | QUEUE_RO_ENTRY(queue_discard_granularity, "discard_granularity"); | |
580 | QUEUE_RO_ENTRY(queue_discard_max_hw, "discard_max_hw_bytes"); | |
581 | QUEUE_RW_ENTRY(queue_discard_max, "discard_max_bytes"); | |
582 | QUEUE_RO_ENTRY(queue_discard_zeroes_data, "discard_zeroes_data"); | |
583 | ||
584 | QUEUE_RO_ENTRY(queue_write_same_max, "write_same_max_bytes"); | |
585 | QUEUE_RO_ENTRY(queue_write_zeroes_max, "write_zeroes_max_bytes"); | |
586 | QUEUE_RO_ENTRY(queue_zone_append_max, "zone_append_max_bytes"); | |
a805a4fa | 587 | QUEUE_RO_ENTRY(queue_zone_write_granularity, "zone_write_granularity"); |
35626147 CH |
588 | |
589 | QUEUE_RO_ENTRY(queue_zoned, "zoned"); | |
590 | QUEUE_RO_ENTRY(queue_nr_zones, "nr_zones"); | |
591 | QUEUE_RO_ENTRY(queue_max_open_zones, "max_open_zones"); | |
592 | QUEUE_RO_ENTRY(queue_max_active_zones, "max_active_zones"); | |
593 | ||
594 | QUEUE_RW_ENTRY(queue_nomerges, "nomerges"); | |
595 | QUEUE_RW_ENTRY(queue_rq_affinity, "rq_affinity"); | |
596 | QUEUE_RW_ENTRY(queue_poll, "io_poll"); | |
597 | QUEUE_RW_ENTRY(queue_poll_delay, "io_poll_delay"); | |
598 | QUEUE_RW_ENTRY(queue_wc, "write_cache"); | |
599 | QUEUE_RO_ENTRY(queue_fua, "fua"); | |
600 | QUEUE_RO_ENTRY(queue_dax, "dax"); | |
601 | QUEUE_RW_ENTRY(queue_io_timeout, "io_timeout"); | |
602 | QUEUE_RW_ENTRY(queue_wb_lat, "wbt_lat_usec"); | |
28af7428 | 603 | QUEUE_RO_ENTRY(queue_virt_boundary_mask, "virt_boundary_mask"); |
3850e13f | 604 | QUEUE_RO_ENTRY(queue_dma_alignment, "dma_alignment"); |
8324aa91 | 605 | |
35626147 CH |
606 | #ifdef CONFIG_BLK_DEV_THROTTLING_LOW |
607 | QUEUE_RW_ENTRY(blk_throtl_sample_time, "throttle_sample_time"); | |
608 | #endif | |
8324aa91 | 609 | |
35626147 | 610 | /* legacy alias for logical_block_size: */ |
e68b903c | 611 | static struct queue_sysfs_entry queue_hw_sector_size_entry = { |
5657a819 | 612 | .attr = {.name = "hw_sector_size", .mode = 0444 }, |
e1defc4f MP |
613 | .show = queue_logical_block_size_show, |
614 | }; | |
615 | ||
fc93fe14 CH |
616 | QUEUE_RW_ENTRY(queue_nonrot, "rotational"); |
617 | QUEUE_RW_ENTRY(queue_iostats, "iostats"); | |
618 | QUEUE_RW_ENTRY(queue_random, "add_random"); | |
1cb039f3 | 619 | QUEUE_RW_ENTRY(queue_stable_writes, "stable_writes"); |
e2e1a148 | 620 | |
4d25339e | 621 | static struct attribute *queue_attrs[] = { |
8324aa91 JA |
622 | &queue_requests_entry.attr, |
623 | &queue_ra_entry.attr, | |
624 | &queue_max_hw_sectors_entry.attr, | |
625 | &queue_max_sectors_entry.attr, | |
c77a5710 | 626 | &queue_max_segments_entry.attr, |
1e739730 | 627 | &queue_max_discard_segments_entry.attr, |
13f05c8d | 628 | &queue_max_integrity_segments_entry.attr, |
c77a5710 | 629 | &queue_max_segment_size_entry.attr, |
35626147 | 630 | &elv_iosched_entry.attr, |
e68b903c | 631 | &queue_hw_sector_size_entry.attr, |
e1defc4f | 632 | &queue_logical_block_size_entry.attr, |
c72758f3 | 633 | &queue_physical_block_size_entry.attr, |
87caf97c | 634 | &queue_chunk_sectors_entry.attr, |
c72758f3 MP |
635 | &queue_io_min_entry.attr, |
636 | &queue_io_opt_entry.attr, | |
86b37281 MP |
637 | &queue_discard_granularity_entry.attr, |
638 | &queue_discard_max_entry.attr, | |
0034af03 | 639 | &queue_discard_max_hw_entry.attr, |
98262f27 | 640 | &queue_discard_zeroes_data_entry.attr, |
4363ac7c | 641 | &queue_write_same_max_entry.attr, |
a6f0788e | 642 | &queue_write_zeroes_max_entry.attr, |
0512a75b | 643 | &queue_zone_append_max_entry.attr, |
a805a4fa | 644 | &queue_zone_write_granularity_entry.attr, |
1308835f | 645 | &queue_nonrot_entry.attr, |
797476b8 | 646 | &queue_zoned_entry.attr, |
965b652e | 647 | &queue_nr_zones_entry.attr, |
e15864f8 | 648 | &queue_max_open_zones_entry.attr, |
659bf827 | 649 | &queue_max_active_zones_entry.attr, |
ac9fafa1 | 650 | &queue_nomerges_entry.attr, |
c7c22e4d | 651 | &queue_rq_affinity_entry.attr, |
bc58ba94 | 652 | &queue_iostats_entry.attr, |
1cb039f3 | 653 | &queue_stable_writes_entry.attr, |
e2e1a148 | 654 | &queue_random_entry.attr, |
05229bee | 655 | &queue_poll_entry.attr, |
93e9d8e8 | 656 | &queue_wc_entry.attr, |
6fcefbe5 | 657 | &queue_fua_entry.attr, |
ea6ca600 | 658 | &queue_dax_entry.attr, |
87760e5e | 659 | &queue_wb_lat_entry.attr, |
06426adf | 660 | &queue_poll_delay_entry.attr, |
65cd1d13 | 661 | &queue_io_timeout_entry.attr, |
297e3d85 | 662 | #ifdef CONFIG_BLK_DEV_THROTTLING_LOW |
35626147 | 663 | &blk_throtl_sample_time_entry.attr, |
297e3d85 | 664 | #endif |
28af7428 | 665 | &queue_virt_boundary_mask_entry.attr, |
3850e13f | 666 | &queue_dma_alignment_entry.attr, |
8324aa91 JA |
667 | NULL, |
668 | }; | |
669 | ||
4d25339e WZ |
670 | static umode_t queue_attr_visible(struct kobject *kobj, struct attribute *attr, |
671 | int n) | |
672 | { | |
2bd85221 CH |
673 | struct gendisk *disk = container_of(kobj, struct gendisk, queue_kobj); |
674 | struct request_queue *q = disk->queue; | |
4d25339e WZ |
675 | |
676 | if (attr == &queue_io_timeout_entry.attr && | |
677 | (!q->mq_ops || !q->mq_ops->timeout)) | |
678 | return 0; | |
679 | ||
659bf827 NC |
680 | if ((attr == &queue_max_open_zones_entry.attr || |
681 | attr == &queue_max_active_zones_entry.attr) && | |
e15864f8 NC |
682 | !blk_queue_is_zoned(q)) |
683 | return 0; | |
684 | ||
4d25339e WZ |
685 | return attr->mode; |
686 | } | |
687 | ||
688 | static struct attribute_group queue_attr_group = { | |
689 | .attrs = queue_attrs, | |
690 | .is_visible = queue_attr_visible, | |
691 | }; | |
692 | ||
693 | ||
8324aa91 JA |
694 | #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr) |
695 | ||
696 | static ssize_t | |
697 | queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page) | |
698 | { | |
699 | struct queue_sysfs_entry *entry = to_queue(attr); | |
2bd85221 CH |
700 | struct gendisk *disk = container_of(kobj, struct gendisk, queue_kobj); |
701 | struct request_queue *q = disk->queue; | |
8324aa91 JA |
702 | ssize_t res; |
703 | ||
704 | if (!entry->show) | |
705 | return -EIO; | |
706 | mutex_lock(&q->sysfs_lock); | |
8324aa91 JA |
707 | res = entry->show(q, page); |
708 | mutex_unlock(&q->sysfs_lock); | |
709 | return res; | |
710 | } | |
711 | ||
712 | static ssize_t | |
713 | queue_attr_store(struct kobject *kobj, struct attribute *attr, | |
714 | const char *page, size_t length) | |
715 | { | |
716 | struct queue_sysfs_entry *entry = to_queue(attr); | |
2bd85221 CH |
717 | struct gendisk *disk = container_of(kobj, struct gendisk, queue_kobj); |
718 | struct request_queue *q = disk->queue; | |
8324aa91 JA |
719 | ssize_t res; |
720 | ||
721 | if (!entry->store) | |
722 | return -EIO; | |
6728cb0e | 723 | |
8324aa91 | 724 | mutex_lock(&q->sysfs_lock); |
8324aa91 JA |
725 | res = entry->store(q, page, length); |
726 | mutex_unlock(&q->sysfs_lock); | |
727 | return res; | |
728 | } | |
729 | ||
52cf25d0 | 730 | static const struct sysfs_ops queue_sysfs_ops = { |
8324aa91 JA |
731 | .show = queue_attr_show, |
732 | .store = queue_attr_store, | |
733 | }; | |
734 | ||
4a8d14bb CH |
735 | static const struct attribute_group *blk_queue_attr_groups[] = { |
736 | &queue_attr_group, | |
737 | NULL | |
738 | }; | |
739 | ||
2bd85221 CH |
740 | static void blk_queue_release(struct kobject *kobj) |
741 | { | |
742 | /* nothing to do here, all data is associated with the parent gendisk */ | |
743 | } | |
744 | ||
5f622417 | 745 | static const struct kobj_type blk_queue_ktype = { |
4a8d14bb | 746 | .default_groups = blk_queue_attr_groups, |
8324aa91 | 747 | .sysfs_ops = &queue_sysfs_ops, |
2bd85221 | 748 | .release = blk_queue_release, |
8324aa91 JA |
749 | }; |
750 | ||
6fc75f30 CH |
751 | static void blk_debugfs_remove(struct gendisk *disk) |
752 | { | |
753 | struct request_queue *q = disk->queue; | |
754 | ||
755 | mutex_lock(&q->debugfs_mutex); | |
756 | blk_trace_shutdown(q); | |
757 | debugfs_remove_recursive(q->debugfs_dir); | |
758 | q->debugfs_dir = NULL; | |
759 | q->sched_debugfs_dir = NULL; | |
760 | q->rqos_debugfs_dir = NULL; | |
761 | mutex_unlock(&q->debugfs_mutex); | |
762 | } | |
763 | ||
2c2086af BVA |
764 | /** |
765 | * blk_register_queue - register a block layer queue with sysfs | |
766 | * @disk: Disk of which the request queue should be registered with sysfs. | |
767 | */ | |
8324aa91 JA |
768 | int blk_register_queue(struct gendisk *disk) |
769 | { | |
8324aa91 | 770 | struct request_queue *q = disk->queue; |
8682b92e | 771 | int ret; |
8324aa91 | 772 | |
cecf5d87 | 773 | mutex_lock(&q->sysfs_dir_lock); |
2bd85221 CH |
774 | kobject_init(&disk->queue_kobj, &blk_queue_ktype); |
775 | ret = kobject_add(&disk->queue_kobj, &disk_to_dev(disk)->kobj, "queue"); | |
cc5c516d | 776 | if (ret < 0) |
2bd85221 | 777 | goto out_put_queue_kobj; |
8324aa91 | 778 | |
40602997 CH |
779 | if (queue_is_mq(q)) { |
780 | ret = blk_mq_sysfs_register(disk); | |
781 | if (ret) | |
2bd85221 | 782 | goto out_put_queue_kobj; |
40602997 | 783 | } |
5cf9c91b CH |
784 | mutex_lock(&q->sysfs_lock); |
785 | ||
85e0cbbb | 786 | mutex_lock(&q->debugfs_mutex); |
2bd85221 | 787 | q->debugfs_dir = debugfs_create_dir(disk->disk_name, blk_debugfs_root); |
5cf9c91b | 788 | if (queue_is_mq(q)) |
a8ecdd71 | 789 | blk_mq_debugfs_register(q); |
5cf9c91b | 790 | mutex_unlock(&q->debugfs_mutex); |
a2247f19 | 791 | |
22d0c408 | 792 | ret = disk_register_independent_access_ranges(disk); |
a2247f19 | 793 | if (ret) |
40602997 | 794 | goto out_debugfs_remove; |
a2247f19 | 795 | |
344e9ffc | 796 | if (q->elevator) { |
cecf5d87 | 797 | ret = elv_register_queue(q, false); |
a2247f19 | 798 | if (ret) |
40602997 | 799 | goto out_unregister_ia_ranges; |
8324aa91 | 800 | } |
cecf5d87 | 801 | |
450deb93 | 802 | ret = blk_crypto_sysfs_register(disk); |
20f01f16 | 803 | if (ret) |
40602997 | 804 | goto out_elv_unregister; |
20f01f16 | 805 | |
cecf5d87 | 806 | blk_queue_flag_set(QUEUE_FLAG_REGISTERED, q); |
04aad37b | 807 | wbt_enable_default(disk); |
5f6dc752 | 808 | blk_throtl_register(disk); |
cecf5d87 ML |
809 | |
810 | /* Now everything is ready and send out KOBJ_ADD uevent */ | |
2bd85221 | 811 | kobject_uevent(&disk->queue_kobj, KOBJ_ADD); |
0546858c | 812 | if (q->elevator) |
cecf5d87 ML |
813 | kobject_uevent(&q->elevator->kobj, KOBJ_ADD); |
814 | mutex_unlock(&q->sysfs_lock); | |
cecf5d87 | 815 | mutex_unlock(&q->sysfs_dir_lock); |
a72c374f ML |
816 | |
817 | /* | |
818 | * SCSI probing may synchronously create and destroy a lot of | |
819 | * request_queues for non-existent devices. Shutting down a fully | |
820 | * functional queue takes measureable wallclock time as RCU grace | |
821 | * periods are involved. To avoid excessive latency in these | |
822 | * cases, a request_queue starts out in a degraded mode which is | |
823 | * faster to shut down and is made fully functional here as | |
824 | * request_queues for non-existent devices never get registered. | |
825 | */ | |
826 | if (!blk_queue_init_done(q)) { | |
827 | blk_queue_flag_set(QUEUE_FLAG_INIT_DONE, q); | |
828 | percpu_ref_switch_to_percpu(&q->q_usage_counter); | |
829 | } | |
830 | ||
a2247f19 DLM |
831 | return ret; |
832 | ||
40602997 | 833 | out_elv_unregister: |
20f01f16 | 834 | elv_unregister_queue(q); |
40602997 | 835 | out_unregister_ia_ranges: |
a2247f19 | 836 | disk_unregister_independent_access_ranges(disk); |
40602997 CH |
837 | out_debugfs_remove: |
838 | blk_debugfs_remove(disk); | |
a2247f19 | 839 | mutex_unlock(&q->sysfs_lock); |
2bd85221 CH |
840 | out_put_queue_kobj: |
841 | kobject_put(&disk->queue_kobj); | |
40602997 | 842 | mutex_unlock(&q->sysfs_dir_lock); |
b410aff2 | 843 | return ret; |
8324aa91 JA |
844 | } |
845 | ||
2c2086af BVA |
846 | /** |
847 | * blk_unregister_queue - counterpart of blk_register_queue() | |
848 | * @disk: Disk of which the request queue should be unregistered from sysfs. | |
849 | * | |
850 | * Note: the caller is responsible for guaranteeing that this function is called | |
851 | * after blk_register_queue() has finished. | |
852 | */ | |
8324aa91 JA |
853 | void blk_unregister_queue(struct gendisk *disk) |
854 | { | |
855 | struct request_queue *q = disk->queue; | |
856 | ||
fb199746 AM |
857 | if (WARN_ON(!q)) |
858 | return; | |
859 | ||
fa70d2e2 | 860 | /* Return early if disk->queue was never registered. */ |
58c898ba | 861 | if (!blk_queue_registered(q)) |
fa70d2e2 MS |
862 | return; |
863 | ||
667257e8 | 864 | /* |
2c2086af BVA |
865 | * Since sysfs_remove_dir() prevents adding new directory entries |
866 | * before removal of existing entries starts, protect against | |
867 | * concurrent elv_iosched_store() calls. | |
667257e8 | 868 | */ |
e9a823fb | 869 | mutex_lock(&q->sysfs_lock); |
8814ce8a | 870 | blk_queue_flag_clear(QUEUE_FLAG_REGISTERED, q); |
cecf5d87 | 871 | mutex_unlock(&q->sysfs_lock); |
02ba8893 | 872 | |
cecf5d87 | 873 | mutex_lock(&q->sysfs_dir_lock); |
2c2086af BVA |
874 | /* |
875 | * Remove the sysfs attributes before unregistering the queue data | |
876 | * structures that can be modified through sysfs. | |
877 | */ | |
344e9ffc | 878 | if (queue_is_mq(q)) |
8682b92e | 879 | blk_mq_sysfs_unregister(disk); |
450deb93 | 880 | blk_crypto_sysfs_unregister(disk); |
667257e8 | 881 | |
b89f625e | 882 | mutex_lock(&q->sysfs_lock); |
f5ec592d | 883 | elv_unregister_queue(q); |
a2247f19 | 884 | disk_unregister_independent_access_ranges(disk); |
b89f625e | 885 | mutex_unlock(&q->sysfs_lock); |
0f692882 EB |
886 | |
887 | /* Now that we've deleted all child objects, we can delete the queue. */ | |
2bd85221 CH |
888 | kobject_uevent(&disk->queue_kobj, KOBJ_REMOVE); |
889 | kobject_del(&disk->queue_kobj); | |
cecf5d87 | 890 | mutex_unlock(&q->sysfs_dir_lock); |
2c2086af | 891 | |
6fc75f30 | 892 | blk_debugfs_remove(disk); |
8324aa91 | 893 | } |