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