blk-mq-debugfs: clean up flag definitions
[linux-2.6-block.git] / block / blk-mq-debugfs.c
CommitLineData
07e4fead
OS
1/*
2 * Copyright (C) 2017 Facebook
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <https://www.gnu.org/licenses/>.
15 */
16
17#include <linux/kernel.h>
18#include <linux/blkdev.h>
19#include <linux/debugfs.h>
20
21#include <linux/blk-mq.h>
18fbda91 22#include "blk.h"
07e4fead 23#include "blk-mq.h"
d96b37c0 24#include "blk-mq-tag.h"
07e4fead
OS
25
26struct blk_mq_debugfs_attr {
27 const char *name;
28 umode_t mode;
29 const struct file_operations *fops;
30};
31
950cd7e9
OS
32static int blk_mq_debugfs_seq_open(struct inode *inode, struct file *file,
33 const struct seq_operations *ops)
34{
35 struct seq_file *m;
36 int ret;
37
38 ret = seq_open(file, ops);
39 if (!ret) {
40 m = file->private_data;
41 m->private = inode->i_private;
42 }
43 return ret;
44}
45
91d68905
BVA
46static int blk_flags_show(struct seq_file *m, const unsigned long flags,
47 const char *const *flag_name, int flag_name_count)
48{
49 bool sep = false;
50 int i;
51
52 for (i = 0; i < sizeof(flags) * BITS_PER_BYTE; i++) {
53 if (!(flags & BIT(i)))
54 continue;
55 if (sep)
bec03d6b 56 seq_puts(m, "|");
91d68905
BVA
57 sep = true;
58 if (i < flag_name_count && flag_name[i])
59 seq_puts(m, flag_name[i]);
60 else
61 seq_printf(m, "%d", i);
62 }
91d68905
BVA
63 return 0;
64}
65
1a435111 66#define QUEUE_FLAG_NAME(name) [QUEUE_FLAG_##name] = #name
91d68905 67static const char *const blk_queue_flag_name[] = {
1a435111
OS
68 QUEUE_FLAG_NAME(QUEUED),
69 QUEUE_FLAG_NAME(STOPPED),
70 QUEUE_FLAG_NAME(SYNCFULL),
71 QUEUE_FLAG_NAME(ASYNCFULL),
72 QUEUE_FLAG_NAME(DYING),
73 QUEUE_FLAG_NAME(BYPASS),
74 QUEUE_FLAG_NAME(BIDI),
75 QUEUE_FLAG_NAME(NOMERGES),
76 QUEUE_FLAG_NAME(SAME_COMP),
77 QUEUE_FLAG_NAME(FAIL_IO),
78 QUEUE_FLAG_NAME(STACKABLE),
79 QUEUE_FLAG_NAME(NONROT),
80 QUEUE_FLAG_NAME(IO_STAT),
81 QUEUE_FLAG_NAME(DISCARD),
82 QUEUE_FLAG_NAME(NOXMERGES),
83 QUEUE_FLAG_NAME(ADD_RANDOM),
84 QUEUE_FLAG_NAME(SECERASE),
85 QUEUE_FLAG_NAME(SAME_FORCE),
86 QUEUE_FLAG_NAME(DEAD),
87 QUEUE_FLAG_NAME(INIT_DONE),
88 QUEUE_FLAG_NAME(NO_SG_MERGE),
89 QUEUE_FLAG_NAME(POLL),
90 QUEUE_FLAG_NAME(WC),
91 QUEUE_FLAG_NAME(FUA),
92 QUEUE_FLAG_NAME(FLUSH_NQ),
93 QUEUE_FLAG_NAME(DAX),
94 QUEUE_FLAG_NAME(STATS),
95 QUEUE_FLAG_NAME(POLL_STATS),
96 QUEUE_FLAG_NAME(REGISTERED),
91d68905 97};
1a435111 98#undef QUEUE_FLAG_NAME
91d68905
BVA
99
100static int blk_queue_flags_show(struct seq_file *m, void *v)
101{
102 struct request_queue *q = m->private;
103
104 blk_flags_show(m, q->queue_flags, blk_queue_flag_name,
105 ARRAY_SIZE(blk_queue_flag_name));
fd07dc81 106 seq_puts(m, "\n");
91d68905
BVA
107 return 0;
108}
109
110static ssize_t blk_queue_flags_store(struct file *file, const char __user *ubuf,
111 size_t len, loff_t *offp)
112{
113 struct request_queue *q = file_inode(file)->i_private;
114 char op[16] = { }, *s;
115
116 len = min(len, sizeof(op) - 1);
117 if (copy_from_user(op, ubuf, len))
118 return -EFAULT;
119 s = op;
120 strsep(&s, " \t\n"); /* strip trailing whitespace */
121 if (strcmp(op, "run") == 0) {
122 blk_mq_run_hw_queues(q, true);
123 } else if (strcmp(op, "start") == 0) {
124 blk_mq_start_stopped_hw_queues(q, true);
125 } else {
126 pr_err("%s: unsupported operation %s. Use either 'run' or 'start'\n",
127 __func__, op);
128 return -EINVAL;
129 }
130 return len;
131}
132
133static int blk_queue_flags_open(struct inode *inode, struct file *file)
134{
135 return single_open(file, blk_queue_flags_show, inode->i_private);
136}
137
138static const struct file_operations blk_queue_flags_fops = {
139 .open = blk_queue_flags_open,
140 .read = seq_read,
141 .llseek = seq_lseek,
142 .release = single_release,
143 .write = blk_queue_flags_store,
144};
145
34dbad5d
OS
146static void print_stat(struct seq_file *m, struct blk_rq_stat *stat)
147{
148 if (stat->nr_samples) {
149 seq_printf(m, "samples=%d, mean=%lld, min=%llu, max=%llu",
150 stat->nr_samples, stat->mean, stat->min, stat->max);
151 } else {
152 seq_puts(m, "samples=0");
153 }
154}
155
156static int queue_poll_stat_show(struct seq_file *m, void *v)
157{
158 struct request_queue *q = m->private;
0206319f 159 int bucket;
34dbad5d 160
0206319f
SB
161 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS/2; bucket++) {
162 seq_printf(m, "read (%d Bytes): ", 1 << (9+bucket));
163 print_stat(m, &q->poll_stat[2*bucket]);
164 seq_puts(m, "\n");
34dbad5d 165
0206319f
SB
166 seq_printf(m, "write (%d Bytes): ", 1 << (9+bucket));
167 print_stat(m, &q->poll_stat[2*bucket+1]);
168 seq_puts(m, "\n");
169 }
34dbad5d
OS
170 return 0;
171}
172
173static int queue_poll_stat_open(struct inode *inode, struct file *file)
174{
175 return single_open(file, queue_poll_stat_show, inode->i_private);
176}
177
178static const struct file_operations queue_poll_stat_fops = {
179 .open = queue_poll_stat_open,
180 .read = seq_read,
181 .llseek = seq_lseek,
182 .release = single_release,
183};
184
1a435111 185#define HCTX_STATE_NAME(name) [BLK_MQ_S_##name] = #name
f5c0b091 186static const char *const hctx_state_name[] = {
1a435111
OS
187 HCTX_STATE_NAME(STOPPED),
188 HCTX_STATE_NAME(TAG_ACTIVE),
189 HCTX_STATE_NAME(SCHED_RESTART),
190 HCTX_STATE_NAME(TAG_WAITING),
191 HCTX_STATE_NAME(START_ON_RUN),
f5c0b091 192};
1a435111
OS
193#undef HCTX_STATE_NAME
194
9abb2ad2
OS
195static int hctx_state_show(struct seq_file *m, void *v)
196{
197 struct blk_mq_hw_ctx *hctx = m->private;
198
f5c0b091
BVA
199 blk_flags_show(m, hctx->state, hctx_state_name,
200 ARRAY_SIZE(hctx_state_name));
fd07dc81 201 seq_puts(m, "\n");
9abb2ad2
OS
202 return 0;
203}
204
205static int hctx_state_open(struct inode *inode, struct file *file)
206{
207 return single_open(file, hctx_state_show, inode->i_private);
208}
209
210static const struct file_operations hctx_state_fops = {
211 .open = hctx_state_open,
212 .read = seq_read,
213 .llseek = seq_lseek,
214 .release = single_release,
215};
216
1a435111 217#define BLK_TAG_ALLOC_NAME(name) [BLK_TAG_ALLOC_##name] = #name
f5c0b091 218static const char *const alloc_policy_name[] = {
1a435111
OS
219 BLK_TAG_ALLOC_NAME(FIFO),
220 BLK_TAG_ALLOC_NAME(RR),
f5c0b091 221};
1a435111 222#undef BLK_TAG_ALLOC_NAME
f5c0b091 223
1a435111 224#define HCTX_FLAG_NAME(name) [ilog2(BLK_MQ_F_##name)] = #name
f5c0b091 225static const char *const hctx_flag_name[] = {
1a435111
OS
226 HCTX_FLAG_NAME(SHOULD_MERGE),
227 HCTX_FLAG_NAME(TAG_SHARED),
228 HCTX_FLAG_NAME(SG_MERGE),
229 HCTX_FLAG_NAME(BLOCKING),
230 HCTX_FLAG_NAME(NO_SCHED),
f5c0b091 231};
1a435111 232#undef HCTX_FLAG_NAME
f5c0b091 233
9abb2ad2
OS
234static int hctx_flags_show(struct seq_file *m, void *v)
235{
236 struct blk_mq_hw_ctx *hctx = m->private;
f5c0b091 237 const int alloc_policy = BLK_MQ_FLAG_TO_ALLOC_POLICY(hctx->flags);
9abb2ad2 238
f5c0b091
BVA
239 seq_puts(m, "alloc_policy=");
240 if (alloc_policy < ARRAY_SIZE(alloc_policy_name) &&
241 alloc_policy_name[alloc_policy])
242 seq_puts(m, alloc_policy_name[alloc_policy]);
243 else
244 seq_printf(m, "%d", alloc_policy);
245 seq_puts(m, " ");
246 blk_flags_show(m,
247 hctx->flags ^ BLK_ALLOC_POLICY_TO_MQ_FLAG(alloc_policy),
248 hctx_flag_name, ARRAY_SIZE(hctx_flag_name));
fd07dc81 249 seq_puts(m, "\n");
9abb2ad2
OS
250 return 0;
251}
252
253static int hctx_flags_open(struct inode *inode, struct file *file)
254{
255 return single_open(file, hctx_flags_show, inode->i_private);
256}
257
258static const struct file_operations hctx_flags_fops = {
259 .open = hctx_flags_open,
260 .read = seq_read,
261 .llseek = seq_lseek,
262 .release = single_release,
263};
264
1a435111 265#define REQ_OP_NAME(name) [REQ_OP_##name] = #name
8658dca8 266static const char *const op_name[] = {
1a435111
OS
267 REQ_OP_NAME(READ),
268 REQ_OP_NAME(WRITE),
269 REQ_OP_NAME(FLUSH),
270 REQ_OP_NAME(DISCARD),
271 REQ_OP_NAME(ZONE_REPORT),
272 REQ_OP_NAME(SECURE_ERASE),
273 REQ_OP_NAME(ZONE_RESET),
274 REQ_OP_NAME(WRITE_SAME),
275 REQ_OP_NAME(WRITE_ZEROES),
276 REQ_OP_NAME(SCSI_IN),
277 REQ_OP_NAME(SCSI_OUT),
278 REQ_OP_NAME(DRV_IN),
279 REQ_OP_NAME(DRV_OUT),
8658dca8 280};
1a435111 281#undef REQ_OP_NAME
8658dca8 282
1a435111 283#define CMD_FLAG_NAME(name) [__REQ_##name] = #name
8658dca8 284static const char *const cmd_flag_name[] = {
1a435111
OS
285 CMD_FLAG_NAME(FAILFAST_DEV),
286 CMD_FLAG_NAME(FAILFAST_TRANSPORT),
287 CMD_FLAG_NAME(FAILFAST_DRIVER),
288 CMD_FLAG_NAME(SYNC),
289 CMD_FLAG_NAME(META),
290 CMD_FLAG_NAME(PRIO),
291 CMD_FLAG_NAME(NOMERGE),
292 CMD_FLAG_NAME(IDLE),
293 CMD_FLAG_NAME(INTEGRITY),
294 CMD_FLAG_NAME(FUA),
295 CMD_FLAG_NAME(PREFLUSH),
296 CMD_FLAG_NAME(RAHEAD),
297 CMD_FLAG_NAME(BACKGROUND),
298 CMD_FLAG_NAME(NOUNMAP),
8658dca8 299};
1a435111 300#undef CMD_FLAG_NAME
8658dca8 301
1a435111 302#define RQF_NAME(name) [ilog2((__force u32)RQF_##name)] = #name
8658dca8 303static const char *const rqf_name[] = {
1a435111
OS
304 RQF_NAME(SORTED),
305 RQF_NAME(STARTED),
306 RQF_NAME(QUEUED),
307 RQF_NAME(SOFTBARRIER),
308 RQF_NAME(FLUSH_SEQ),
309 RQF_NAME(MIXED_MERGE),
310 RQF_NAME(MQ_INFLIGHT),
311 RQF_NAME(DONTPREP),
312 RQF_NAME(PREEMPT),
313 RQF_NAME(COPY_USER),
314 RQF_NAME(FAILED),
315 RQF_NAME(QUIET),
316 RQF_NAME(ELVPRIV),
317 RQF_NAME(IO_STAT),
318 RQF_NAME(ALLOCED),
319 RQF_NAME(PM),
320 RQF_NAME(HASHED),
321 RQF_NAME(STATS),
322 RQF_NAME(SPECIAL_PAYLOAD),
8658dca8 323};
1a435111 324#undef RQF_NAME
8658dca8 325
950cd7e9
OS
326static int blk_mq_debugfs_rq_show(struct seq_file *m, void *v)
327{
328 struct request *rq = list_entry_rq(v);
2836ee4b 329 const struct blk_mq_ops *const mq_ops = rq->q->mq_ops;
8658dca8 330 const unsigned int op = rq->cmd_flags & REQ_OP_MASK;
950cd7e9 331
8658dca8
BVA
332 seq_printf(m, "%p {.op=", rq);
333 if (op < ARRAY_SIZE(op_name) && op_name[op])
334 seq_printf(m, "%s", op_name[op]);
335 else
336 seq_printf(m, "%d", op);
337 seq_puts(m, ", .cmd_flags=");
338 blk_flags_show(m, rq->cmd_flags & ~REQ_OP_MASK, cmd_flag_name,
339 ARRAY_SIZE(cmd_flag_name));
340 seq_puts(m, ", .rq_flags=");
341 blk_flags_show(m, (__force unsigned int)rq->rq_flags, rqf_name,
342 ARRAY_SIZE(rqf_name));
2836ee4b 343 seq_printf(m, ", .tag=%d, .internal_tag=%d", rq->tag,
8658dca8 344 rq->internal_tag);
2836ee4b
BVA
345 if (mq_ops->show_rq)
346 mq_ops->show_rq(m, rq);
347 seq_puts(m, "}\n");
950cd7e9
OS
348 return 0;
349}
350
351static void *hctx_dispatch_start(struct seq_file *m, loff_t *pos)
f3bcb0e6 352 __acquires(&hctx->lock)
950cd7e9
OS
353{
354 struct blk_mq_hw_ctx *hctx = m->private;
355
356 spin_lock(&hctx->lock);
357 return seq_list_start(&hctx->dispatch, *pos);
358}
359
360static void *hctx_dispatch_next(struct seq_file *m, void *v, loff_t *pos)
361{
362 struct blk_mq_hw_ctx *hctx = m->private;
363
364 return seq_list_next(v, &hctx->dispatch, pos);
365}
366
367static void hctx_dispatch_stop(struct seq_file *m, void *v)
f3bcb0e6 368 __releases(&hctx->lock)
950cd7e9
OS
369{
370 struct blk_mq_hw_ctx *hctx = m->private;
371
372 spin_unlock(&hctx->lock);
373}
374
375static const struct seq_operations hctx_dispatch_seq_ops = {
376 .start = hctx_dispatch_start,
377 .next = hctx_dispatch_next,
378 .stop = hctx_dispatch_stop,
379 .show = blk_mq_debugfs_rq_show,
380};
381
382static int hctx_dispatch_open(struct inode *inode, struct file *file)
383{
384 return blk_mq_debugfs_seq_open(inode, file, &hctx_dispatch_seq_ops);
385}
386
387static const struct file_operations hctx_dispatch_fops = {
388 .open = hctx_dispatch_open,
389 .read = seq_read,
390 .llseek = seq_lseek,
391 .release = seq_release,
392};
393
0bfa5288
OS
394static int hctx_ctx_map_show(struct seq_file *m, void *v)
395{
396 struct blk_mq_hw_ctx *hctx = m->private;
397
398 sbitmap_bitmap_show(&hctx->ctx_map, m);
399 return 0;
400}
401
402static int hctx_ctx_map_open(struct inode *inode, struct file *file)
403{
404 return single_open(file, hctx_ctx_map_show, inode->i_private);
405}
406
407static const struct file_operations hctx_ctx_map_fops = {
408 .open = hctx_ctx_map_open,
409 .read = seq_read,
410 .llseek = seq_lseek,
411 .release = single_release,
412};
413
d96b37c0
OS
414static void blk_mq_debugfs_tags_show(struct seq_file *m,
415 struct blk_mq_tags *tags)
416{
417 seq_printf(m, "nr_tags=%u\n", tags->nr_tags);
418 seq_printf(m, "nr_reserved_tags=%u\n", tags->nr_reserved_tags);
419 seq_printf(m, "active_queues=%d\n",
420 atomic_read(&tags->active_queues));
421
422 seq_puts(m, "\nbitmap_tags:\n");
423 sbitmap_queue_show(&tags->bitmap_tags, m);
424
425 if (tags->nr_reserved_tags) {
426 seq_puts(m, "\nbreserved_tags:\n");
427 sbitmap_queue_show(&tags->breserved_tags, m);
428 }
429}
430
431static int hctx_tags_show(struct seq_file *m, void *v)
432{
433 struct blk_mq_hw_ctx *hctx = m->private;
434 struct request_queue *q = hctx->queue;
8c0f14ea 435 int res;
d96b37c0 436
8c0f14ea
BVA
437 res = mutex_lock_interruptible(&q->sysfs_lock);
438 if (res)
439 goto out;
d96b37c0
OS
440 if (hctx->tags)
441 blk_mq_debugfs_tags_show(m, hctx->tags);
442 mutex_unlock(&q->sysfs_lock);
443
8c0f14ea
BVA
444out:
445 return res;
d96b37c0
OS
446}
447
448static int hctx_tags_open(struct inode *inode, struct file *file)
449{
450 return single_open(file, hctx_tags_show, inode->i_private);
451}
452
453static const struct file_operations hctx_tags_fops = {
454 .open = hctx_tags_open,
455 .read = seq_read,
456 .llseek = seq_lseek,
457 .release = single_release,
458};
459
d7e3621a
OS
460static int hctx_tags_bitmap_show(struct seq_file *m, void *v)
461{
462 struct blk_mq_hw_ctx *hctx = m->private;
463 struct request_queue *q = hctx->queue;
8c0f14ea 464 int res;
d7e3621a 465
8c0f14ea
BVA
466 res = mutex_lock_interruptible(&q->sysfs_lock);
467 if (res)
468 goto out;
d7e3621a
OS
469 if (hctx->tags)
470 sbitmap_bitmap_show(&hctx->tags->bitmap_tags.sb, m);
471 mutex_unlock(&q->sysfs_lock);
8c0f14ea
BVA
472
473out:
474 return res;
d7e3621a
OS
475}
476
477static int hctx_tags_bitmap_open(struct inode *inode, struct file *file)
478{
479 return single_open(file, hctx_tags_bitmap_show, inode->i_private);
480}
481
482static const struct file_operations hctx_tags_bitmap_fops = {
483 .open = hctx_tags_bitmap_open,
484 .read = seq_read,
485 .llseek = seq_lseek,
486 .release = single_release,
487};
488
d96b37c0
OS
489static int hctx_sched_tags_show(struct seq_file *m, void *v)
490{
491 struct blk_mq_hw_ctx *hctx = m->private;
492 struct request_queue *q = hctx->queue;
8c0f14ea 493 int res;
d96b37c0 494
8c0f14ea
BVA
495 res = mutex_lock_interruptible(&q->sysfs_lock);
496 if (res)
497 goto out;
d96b37c0
OS
498 if (hctx->sched_tags)
499 blk_mq_debugfs_tags_show(m, hctx->sched_tags);
500 mutex_unlock(&q->sysfs_lock);
501
8c0f14ea
BVA
502out:
503 return res;
d96b37c0
OS
504}
505
506static int hctx_sched_tags_open(struct inode *inode, struct file *file)
507{
508 return single_open(file, hctx_sched_tags_show, inode->i_private);
509}
510
511static const struct file_operations hctx_sched_tags_fops = {
512 .open = hctx_sched_tags_open,
513 .read = seq_read,
514 .llseek = seq_lseek,
515 .release = single_release,
516};
517
d7e3621a
OS
518static int hctx_sched_tags_bitmap_show(struct seq_file *m, void *v)
519{
520 struct blk_mq_hw_ctx *hctx = m->private;
521 struct request_queue *q = hctx->queue;
8c0f14ea 522 int res;
d7e3621a 523
8c0f14ea
BVA
524 res = mutex_lock_interruptible(&q->sysfs_lock);
525 if (res)
526 goto out;
d7e3621a
OS
527 if (hctx->sched_tags)
528 sbitmap_bitmap_show(&hctx->sched_tags->bitmap_tags.sb, m);
529 mutex_unlock(&q->sysfs_lock);
8c0f14ea
BVA
530
531out:
532 return res;
d7e3621a
OS
533}
534
535static int hctx_sched_tags_bitmap_open(struct inode *inode, struct file *file)
536{
537 return single_open(file, hctx_sched_tags_bitmap_show, inode->i_private);
538}
539
540static const struct file_operations hctx_sched_tags_bitmap_fops = {
541 .open = hctx_sched_tags_bitmap_open,
542 .read = seq_read,
543 .llseek = seq_lseek,
544 .release = single_release,
545};
546
be215473
OS
547static int hctx_io_poll_show(struct seq_file *m, void *v)
548{
549 struct blk_mq_hw_ctx *hctx = m->private;
550
551 seq_printf(m, "considered=%lu\n", hctx->poll_considered);
552 seq_printf(m, "invoked=%lu\n", hctx->poll_invoked);
553 seq_printf(m, "success=%lu\n", hctx->poll_success);
554 return 0;
555}
556
557static int hctx_io_poll_open(struct inode *inode, struct file *file)
558{
559 return single_open(file, hctx_io_poll_show, inode->i_private);
560}
561
562static ssize_t hctx_io_poll_write(struct file *file, const char __user *buf,
563 size_t count, loff_t *ppos)
564{
565 struct seq_file *m = file->private_data;
566 struct blk_mq_hw_ctx *hctx = m->private;
567
568 hctx->poll_considered = hctx->poll_invoked = hctx->poll_success = 0;
569 return count;
570}
571
572static const struct file_operations hctx_io_poll_fops = {
573 .open = hctx_io_poll_open,
574 .read = seq_read,
575 .write = hctx_io_poll_write,
576 .llseek = seq_lseek,
577 .release = single_release,
578};
579
be215473
OS
580static int hctx_dispatched_show(struct seq_file *m, void *v)
581{
582 struct blk_mq_hw_ctx *hctx = m->private;
583 int i;
584
585 seq_printf(m, "%8u\t%lu\n", 0U, hctx->dispatched[0]);
586
587 for (i = 1; i < BLK_MQ_MAX_DISPATCH_ORDER - 1; i++) {
588 unsigned int d = 1U << (i - 1);
589
590 seq_printf(m, "%8u\t%lu\n", d, hctx->dispatched[i]);
591 }
592
593 seq_printf(m, "%8u+\t%lu\n", 1U << (i - 1), hctx->dispatched[i]);
594 return 0;
595}
596
597static int hctx_dispatched_open(struct inode *inode, struct file *file)
598{
599 return single_open(file, hctx_dispatched_show, inode->i_private);
600}
601
602static ssize_t hctx_dispatched_write(struct file *file, const char __user *buf,
603 size_t count, loff_t *ppos)
604{
605 struct seq_file *m = file->private_data;
606 struct blk_mq_hw_ctx *hctx = m->private;
607 int i;
608
609 for (i = 0; i < BLK_MQ_MAX_DISPATCH_ORDER; i++)
610 hctx->dispatched[i] = 0;
611 return count;
612}
613
614static const struct file_operations hctx_dispatched_fops = {
615 .open = hctx_dispatched_open,
616 .read = seq_read,
617 .write = hctx_dispatched_write,
618 .llseek = seq_lseek,
619 .release = single_release,
620};
621
4a46f05e
OS
622static int hctx_queued_show(struct seq_file *m, void *v)
623{
624 struct blk_mq_hw_ctx *hctx = m->private;
625
626 seq_printf(m, "%lu\n", hctx->queued);
627 return 0;
628}
629
630static int hctx_queued_open(struct inode *inode, struct file *file)
631{
632 return single_open(file, hctx_queued_show, inode->i_private);
633}
634
635static ssize_t hctx_queued_write(struct file *file, const char __user *buf,
636 size_t count, loff_t *ppos)
637{
638 struct seq_file *m = file->private_data;
639 struct blk_mq_hw_ctx *hctx = m->private;
640
641 hctx->queued = 0;
642 return count;
643}
644
645static const struct file_operations hctx_queued_fops = {
646 .open = hctx_queued_open,
647 .read = seq_read,
648 .write = hctx_queued_write,
649 .llseek = seq_lseek,
650 .release = single_release,
651};
652
653static int hctx_run_show(struct seq_file *m, void *v)
654{
655 struct blk_mq_hw_ctx *hctx = m->private;
656
657 seq_printf(m, "%lu\n", hctx->run);
658 return 0;
659}
660
661static int hctx_run_open(struct inode *inode, struct file *file)
662{
663 return single_open(file, hctx_run_show, inode->i_private);
664}
665
666static ssize_t hctx_run_write(struct file *file, const char __user *buf,
667 size_t count, loff_t *ppos)
668{
669 struct seq_file *m = file->private_data;
670 struct blk_mq_hw_ctx *hctx = m->private;
671
672 hctx->run = 0;
673 return count;
674}
675
676static const struct file_operations hctx_run_fops = {
677 .open = hctx_run_open,
678 .read = seq_read,
679 .write = hctx_run_write,
680 .llseek = seq_lseek,
681 .release = single_release,
682};
683
684static int hctx_active_show(struct seq_file *m, void *v)
685{
686 struct blk_mq_hw_ctx *hctx = m->private;
687
688 seq_printf(m, "%d\n", atomic_read(&hctx->nr_active));
689 return 0;
690}
691
692static int hctx_active_open(struct inode *inode, struct file *file)
693{
694 return single_open(file, hctx_active_show, inode->i_private);
695}
696
697static const struct file_operations hctx_active_fops = {
698 .open = hctx_active_open,
699 .read = seq_read,
700 .llseek = seq_lseek,
701 .release = single_release,
702};
703
950cd7e9 704static void *ctx_rq_list_start(struct seq_file *m, loff_t *pos)
f3bcb0e6 705 __acquires(&ctx->lock)
950cd7e9
OS
706{
707 struct blk_mq_ctx *ctx = m->private;
708
709 spin_lock(&ctx->lock);
710 return seq_list_start(&ctx->rq_list, *pos);
711}
712
713static void *ctx_rq_list_next(struct seq_file *m, void *v, loff_t *pos)
714{
715 struct blk_mq_ctx *ctx = m->private;
716
717 return seq_list_next(v, &ctx->rq_list, pos);
718}
719
720static void ctx_rq_list_stop(struct seq_file *m, void *v)
f3bcb0e6 721 __releases(&ctx->lock)
950cd7e9
OS
722{
723 struct blk_mq_ctx *ctx = m->private;
724
725 spin_unlock(&ctx->lock);
726}
727
728static const struct seq_operations ctx_rq_list_seq_ops = {
729 .start = ctx_rq_list_start,
730 .next = ctx_rq_list_next,
731 .stop = ctx_rq_list_stop,
732 .show = blk_mq_debugfs_rq_show,
733};
734
735static int ctx_rq_list_open(struct inode *inode, struct file *file)
736{
737 return blk_mq_debugfs_seq_open(inode, file, &ctx_rq_list_seq_ops);
738}
739
740static const struct file_operations ctx_rq_list_fops = {
741 .open = ctx_rq_list_open,
742 .read = seq_read,
743 .llseek = seq_lseek,
744 .release = seq_release,
745};
746
4a46f05e
OS
747static int ctx_dispatched_show(struct seq_file *m, void *v)
748{
749 struct blk_mq_ctx *ctx = m->private;
750
751 seq_printf(m, "%lu %lu\n", ctx->rq_dispatched[1], ctx->rq_dispatched[0]);
752 return 0;
753}
754
755static int ctx_dispatched_open(struct inode *inode, struct file *file)
756{
757 return single_open(file, ctx_dispatched_show, inode->i_private);
758}
759
760static ssize_t ctx_dispatched_write(struct file *file, const char __user *buf,
761 size_t count, loff_t *ppos)
762{
763 struct seq_file *m = file->private_data;
764 struct blk_mq_ctx *ctx = m->private;
765
766 ctx->rq_dispatched[0] = ctx->rq_dispatched[1] = 0;
767 return count;
768}
769
770static const struct file_operations ctx_dispatched_fops = {
771 .open = ctx_dispatched_open,
772 .read = seq_read,
773 .write = ctx_dispatched_write,
774 .llseek = seq_lseek,
775 .release = single_release,
776};
777
778static int ctx_merged_show(struct seq_file *m, void *v)
779{
780 struct blk_mq_ctx *ctx = m->private;
781
782 seq_printf(m, "%lu\n", ctx->rq_merged);
783 return 0;
784}
785
786static int ctx_merged_open(struct inode *inode, struct file *file)
787{
788 return single_open(file, ctx_merged_show, inode->i_private);
789}
790
791static ssize_t ctx_merged_write(struct file *file, const char __user *buf,
792 size_t count, loff_t *ppos)
793{
794 struct seq_file *m = file->private_data;
795 struct blk_mq_ctx *ctx = m->private;
796
797 ctx->rq_merged = 0;
798 return count;
799}
800
801static const struct file_operations ctx_merged_fops = {
802 .open = ctx_merged_open,
803 .read = seq_read,
804 .write = ctx_merged_write,
805 .llseek = seq_lseek,
806 .release = single_release,
807};
808
809static int ctx_completed_show(struct seq_file *m, void *v)
810{
811 struct blk_mq_ctx *ctx = m->private;
812
813 seq_printf(m, "%lu %lu\n", ctx->rq_completed[1], ctx->rq_completed[0]);
814 return 0;
815}
816
817static int ctx_completed_open(struct inode *inode, struct file *file)
818{
819 return single_open(file, ctx_completed_show, inode->i_private);
820}
821
822static ssize_t ctx_completed_write(struct file *file, const char __user *buf,
823 size_t count, loff_t *ppos)
824{
825 struct seq_file *m = file->private_data;
826 struct blk_mq_ctx *ctx = m->private;
827
828 ctx->rq_completed[0] = ctx->rq_completed[1] = 0;
829 return count;
830}
831
832static const struct file_operations ctx_completed_fops = {
833 .open = ctx_completed_open,
834 .read = seq_read,
835 .write = ctx_completed_write,
836 .llseek = seq_lseek,
837 .release = single_release,
838};
839
34dbad5d
OS
840static const struct blk_mq_debugfs_attr blk_mq_debugfs_queue_attrs[] = {
841 {"poll_stat", 0400, &queue_poll_stat_fops},
65ca1ca3 842 {"state", 0600, &blk_queue_flags_fops},
34dbad5d
OS
843 {},
844};
845
07e4fead 846static const struct blk_mq_debugfs_attr blk_mq_debugfs_hctx_attrs[] = {
9abb2ad2
OS
847 {"state", 0400, &hctx_state_fops},
848 {"flags", 0400, &hctx_flags_fops},
950cd7e9 849 {"dispatch", 0400, &hctx_dispatch_fops},
0bfa5288 850 {"ctx_map", 0400, &hctx_ctx_map_fops},
d96b37c0 851 {"tags", 0400, &hctx_tags_fops},
d7e3621a 852 {"tags_bitmap", 0400, &hctx_tags_bitmap_fops},
d96b37c0 853 {"sched_tags", 0400, &hctx_sched_tags_fops},
d7e3621a 854 {"sched_tags_bitmap", 0400, &hctx_sched_tags_bitmap_fops},
be215473 855 {"io_poll", 0600, &hctx_io_poll_fops},
be215473 856 {"dispatched", 0600, &hctx_dispatched_fops},
4a46f05e
OS
857 {"queued", 0600, &hctx_queued_fops},
858 {"run", 0600, &hctx_run_fops},
859 {"active", 0400, &hctx_active_fops},
72f2f8f6 860 {},
07e4fead
OS
861};
862
863static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = {
950cd7e9 864 {"rq_list", 0400, &ctx_rq_list_fops},
4a46f05e
OS
865 {"dispatched", 0600, &ctx_dispatched_fops},
866 {"merged", 0600, &ctx_merged_fops},
867 {"completed", 0600, &ctx_completed_fops},
72f2f8f6 868 {},
07e4fead
OS
869};
870
4c9e4019 871int blk_mq_debugfs_register(struct request_queue *q)
07e4fead 872{
18fbda91 873 if (!blk_debugfs_root)
07e4fead
OS
874 return -ENOENT;
875
4c9e4019
BVA
876 q->debugfs_dir = debugfs_create_dir(kobject_name(q->kobj.parent),
877 blk_debugfs_root);
07e4fead
OS
878 if (!q->debugfs_dir)
879 goto err;
880
62d6c949 881 if (blk_mq_debugfs_register_mq(q))
07e4fead
OS
882 goto err;
883
884 return 0;
885
886err:
887 blk_mq_debugfs_unregister(q);
888 return -ENOMEM;
889}
890
891void blk_mq_debugfs_unregister(struct request_queue *q)
892{
893 debugfs_remove_recursive(q->debugfs_dir);
894 q->mq_debugfs_dir = NULL;
895 q->debugfs_dir = NULL;
896}
897
72f2f8f6
BVA
898static bool debugfs_create_files(struct dentry *parent, void *data,
899 const struct blk_mq_debugfs_attr *attr)
900{
901 for (; attr->name; attr++) {
902 if (!debugfs_create_file(attr->name, attr->mode, parent,
903 data, attr->fops))
904 return false;
905 }
906 return true;
907}
908
07e4fead
OS
909static int blk_mq_debugfs_register_ctx(struct request_queue *q,
910 struct blk_mq_ctx *ctx,
911 struct dentry *hctx_dir)
912{
913 struct dentry *ctx_dir;
914 char name[20];
07e4fead
OS
915
916 snprintf(name, sizeof(name), "cpu%u", ctx->cpu);
917 ctx_dir = debugfs_create_dir(name, hctx_dir);
918 if (!ctx_dir)
919 return -ENOMEM;
920
72f2f8f6
BVA
921 if (!debugfs_create_files(ctx_dir, ctx, blk_mq_debugfs_ctx_attrs))
922 return -ENOMEM;
07e4fead
OS
923
924 return 0;
925}
926
927static int blk_mq_debugfs_register_hctx(struct request_queue *q,
928 struct blk_mq_hw_ctx *hctx)
929{
930 struct blk_mq_ctx *ctx;
931 struct dentry *hctx_dir;
932 char name[20];
933 int i;
934
935 snprintf(name, sizeof(name), "%u", hctx->queue_num);
936 hctx_dir = debugfs_create_dir(name, q->mq_debugfs_dir);
937 if (!hctx_dir)
938 return -ENOMEM;
939
72f2f8f6
BVA
940 if (!debugfs_create_files(hctx_dir, hctx, blk_mq_debugfs_hctx_attrs))
941 return -ENOMEM;
07e4fead
OS
942
943 hctx_for_each_ctx(hctx, ctx, i) {
944 if (blk_mq_debugfs_register_ctx(q, ctx, hctx_dir))
945 return -ENOMEM;
946 }
947
948 return 0;
949}
950
62d6c949 951int blk_mq_debugfs_register_mq(struct request_queue *q)
07e4fead
OS
952{
953 struct blk_mq_hw_ctx *hctx;
954 int i;
955
956 if (!q->debugfs_dir)
957 return -ENOENT;
958
959 q->mq_debugfs_dir = debugfs_create_dir("mq", q->debugfs_dir);
960 if (!q->mq_debugfs_dir)
961 goto err;
962
34dbad5d
OS
963 if (!debugfs_create_files(q->mq_debugfs_dir, q, blk_mq_debugfs_queue_attrs))
964 goto err;
965
07e4fead
OS
966 queue_for_each_hw_ctx(q, hctx, i) {
967 if (blk_mq_debugfs_register_hctx(q, hctx))
968 goto err;
969 }
970
971 return 0;
972
973err:
62d6c949 974 blk_mq_debugfs_unregister_mq(q);
07e4fead
OS
975 return -ENOMEM;
976}
977
62d6c949 978void blk_mq_debugfs_unregister_mq(struct request_queue *q)
07e4fead
OS
979{
980 debugfs_remove_recursive(q->mq_debugfs_dir);
981 q->mq_debugfs_dir = NULL;
982}