blk-mq: move debugfs declarations to a separate header file
[sfrench/cifs-2.6.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-debugfs.h"
25 #include "blk-mq-tag.h"
26
27 struct blk_mq_debugfs_attr {
28         const char *name;
29         umode_t mode;
30         int (*show)(void *, struct seq_file *);
31         ssize_t (*write)(void *, const char __user *, size_t, loff_t *);
32         /* Set either .show or .seq_ops. */
33         const struct seq_operations *seq_ops;
34 };
35
36 static int blk_flags_show(struct seq_file *m, const unsigned long flags,
37                           const char *const *flag_name, int flag_name_count)
38 {
39         bool sep = false;
40         int i;
41
42         for (i = 0; i < sizeof(flags) * BITS_PER_BYTE; i++) {
43                 if (!(flags & BIT(i)))
44                         continue;
45                 if (sep)
46                         seq_puts(m, "|");
47                 sep = true;
48                 if (i < flag_name_count && flag_name[i])
49                         seq_puts(m, flag_name[i]);
50                 else
51                         seq_printf(m, "%d", i);
52         }
53         return 0;
54 }
55
56 #define QUEUE_FLAG_NAME(name) [QUEUE_FLAG_##name] = #name
57 static const char *const blk_queue_flag_name[] = {
58         QUEUE_FLAG_NAME(QUEUED),
59         QUEUE_FLAG_NAME(STOPPED),
60         QUEUE_FLAG_NAME(SYNCFULL),
61         QUEUE_FLAG_NAME(ASYNCFULL),
62         QUEUE_FLAG_NAME(DYING),
63         QUEUE_FLAG_NAME(BYPASS),
64         QUEUE_FLAG_NAME(BIDI),
65         QUEUE_FLAG_NAME(NOMERGES),
66         QUEUE_FLAG_NAME(SAME_COMP),
67         QUEUE_FLAG_NAME(FAIL_IO),
68         QUEUE_FLAG_NAME(STACKABLE),
69         QUEUE_FLAG_NAME(NONROT),
70         QUEUE_FLAG_NAME(IO_STAT),
71         QUEUE_FLAG_NAME(DISCARD),
72         QUEUE_FLAG_NAME(NOXMERGES),
73         QUEUE_FLAG_NAME(ADD_RANDOM),
74         QUEUE_FLAG_NAME(SECERASE),
75         QUEUE_FLAG_NAME(SAME_FORCE),
76         QUEUE_FLAG_NAME(DEAD),
77         QUEUE_FLAG_NAME(INIT_DONE),
78         QUEUE_FLAG_NAME(NO_SG_MERGE),
79         QUEUE_FLAG_NAME(POLL),
80         QUEUE_FLAG_NAME(WC),
81         QUEUE_FLAG_NAME(FUA),
82         QUEUE_FLAG_NAME(FLUSH_NQ),
83         QUEUE_FLAG_NAME(DAX),
84         QUEUE_FLAG_NAME(STATS),
85         QUEUE_FLAG_NAME(POLL_STATS),
86         QUEUE_FLAG_NAME(REGISTERED),
87 };
88 #undef QUEUE_FLAG_NAME
89
90 static int queue_state_show(void *data, struct seq_file *m)
91 {
92         struct request_queue *q = data;
93
94         blk_flags_show(m, q->queue_flags, blk_queue_flag_name,
95                        ARRAY_SIZE(blk_queue_flag_name));
96         seq_puts(m, "\n");
97         return 0;
98 }
99
100 static ssize_t queue_state_write(void *data, const char __user *buf,
101                                  size_t count, loff_t *ppos)
102 {
103         struct request_queue *q = data;
104         char opbuf[16] = { }, *op;
105
106         /*
107          * The "state" attribute is removed after blk_cleanup_queue() has called
108          * blk_mq_free_queue(). Return if QUEUE_FLAG_DEAD has been set to avoid
109          * triggering a use-after-free.
110          */
111         if (blk_queue_dead(q))
112                 return -ENOENT;
113
114         if (count >= sizeof(opbuf)) {
115                 pr_err("%s: operation too long\n", __func__);
116                 goto inval;
117         }
118
119         if (copy_from_user(opbuf, buf, count))
120                 return -EFAULT;
121         op = strstrip(opbuf);
122         if (strcmp(op, "run") == 0) {
123                 blk_mq_run_hw_queues(q, true);
124         } else if (strcmp(op, "start") == 0) {
125                 blk_mq_start_stopped_hw_queues(q, true);
126         } else {
127                 pr_err("%s: unsupported operation '%s'\n", __func__, op);
128 inval:
129                 pr_err("%s: use either 'run' or 'start'\n", __func__);
130                 return -EINVAL;
131         }
132         return count;
133 }
134
135 static void print_stat(struct seq_file *m, struct blk_rq_stat *stat)
136 {
137         if (stat->nr_samples) {
138                 seq_printf(m, "samples=%d, mean=%lld, min=%llu, max=%llu",
139                            stat->nr_samples, stat->mean, stat->min, stat->max);
140         } else {
141                 seq_puts(m, "samples=0");
142         }
143 }
144
145 static int queue_poll_stat_show(void *data, struct seq_file *m)
146 {
147         struct request_queue *q = data;
148         int bucket;
149
150         for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS/2; bucket++) {
151                 seq_printf(m, "read  (%d Bytes): ", 1 << (9+bucket));
152                 print_stat(m, &q->poll_stat[2*bucket]);
153                 seq_puts(m, "\n");
154
155                 seq_printf(m, "write (%d Bytes): ",  1 << (9+bucket));
156                 print_stat(m, &q->poll_stat[2*bucket+1]);
157                 seq_puts(m, "\n");
158         }
159         return 0;
160 }
161
162 #define HCTX_STATE_NAME(name) [BLK_MQ_S_##name] = #name
163 static const char *const hctx_state_name[] = {
164         HCTX_STATE_NAME(STOPPED),
165         HCTX_STATE_NAME(TAG_ACTIVE),
166         HCTX_STATE_NAME(SCHED_RESTART),
167         HCTX_STATE_NAME(TAG_WAITING),
168         HCTX_STATE_NAME(START_ON_RUN),
169 };
170 #undef HCTX_STATE_NAME
171
172 static int hctx_state_show(void *data, struct seq_file *m)
173 {
174         struct blk_mq_hw_ctx *hctx = data;
175
176         blk_flags_show(m, hctx->state, hctx_state_name,
177                        ARRAY_SIZE(hctx_state_name));
178         seq_puts(m, "\n");
179         return 0;
180 }
181
182 #define BLK_TAG_ALLOC_NAME(name) [BLK_TAG_ALLOC_##name] = #name
183 static const char *const alloc_policy_name[] = {
184         BLK_TAG_ALLOC_NAME(FIFO),
185         BLK_TAG_ALLOC_NAME(RR),
186 };
187 #undef BLK_TAG_ALLOC_NAME
188
189 #define HCTX_FLAG_NAME(name) [ilog2(BLK_MQ_F_##name)] = #name
190 static const char *const hctx_flag_name[] = {
191         HCTX_FLAG_NAME(SHOULD_MERGE),
192         HCTX_FLAG_NAME(TAG_SHARED),
193         HCTX_FLAG_NAME(SG_MERGE),
194         HCTX_FLAG_NAME(BLOCKING),
195         HCTX_FLAG_NAME(NO_SCHED),
196 };
197 #undef HCTX_FLAG_NAME
198
199 static int hctx_flags_show(void *data, struct seq_file *m)
200 {
201         struct blk_mq_hw_ctx *hctx = data;
202         const int alloc_policy = BLK_MQ_FLAG_TO_ALLOC_POLICY(hctx->flags);
203
204         seq_puts(m, "alloc_policy=");
205         if (alloc_policy < ARRAY_SIZE(alloc_policy_name) &&
206             alloc_policy_name[alloc_policy])
207                 seq_puts(m, alloc_policy_name[alloc_policy]);
208         else
209                 seq_printf(m, "%d", alloc_policy);
210         seq_puts(m, " ");
211         blk_flags_show(m,
212                        hctx->flags ^ BLK_ALLOC_POLICY_TO_MQ_FLAG(alloc_policy),
213                        hctx_flag_name, ARRAY_SIZE(hctx_flag_name));
214         seq_puts(m, "\n");
215         return 0;
216 }
217
218 #define REQ_OP_NAME(name) [REQ_OP_##name] = #name
219 static const char *const op_name[] = {
220         REQ_OP_NAME(READ),
221         REQ_OP_NAME(WRITE),
222         REQ_OP_NAME(FLUSH),
223         REQ_OP_NAME(DISCARD),
224         REQ_OP_NAME(ZONE_REPORT),
225         REQ_OP_NAME(SECURE_ERASE),
226         REQ_OP_NAME(ZONE_RESET),
227         REQ_OP_NAME(WRITE_SAME),
228         REQ_OP_NAME(WRITE_ZEROES),
229         REQ_OP_NAME(SCSI_IN),
230         REQ_OP_NAME(SCSI_OUT),
231         REQ_OP_NAME(DRV_IN),
232         REQ_OP_NAME(DRV_OUT),
233 };
234 #undef REQ_OP_NAME
235
236 #define CMD_FLAG_NAME(name) [__REQ_##name] = #name
237 static const char *const cmd_flag_name[] = {
238         CMD_FLAG_NAME(FAILFAST_DEV),
239         CMD_FLAG_NAME(FAILFAST_TRANSPORT),
240         CMD_FLAG_NAME(FAILFAST_DRIVER),
241         CMD_FLAG_NAME(SYNC),
242         CMD_FLAG_NAME(META),
243         CMD_FLAG_NAME(PRIO),
244         CMD_FLAG_NAME(NOMERGE),
245         CMD_FLAG_NAME(IDLE),
246         CMD_FLAG_NAME(INTEGRITY),
247         CMD_FLAG_NAME(FUA),
248         CMD_FLAG_NAME(PREFLUSH),
249         CMD_FLAG_NAME(RAHEAD),
250         CMD_FLAG_NAME(BACKGROUND),
251         CMD_FLAG_NAME(NOUNMAP),
252 };
253 #undef CMD_FLAG_NAME
254
255 #define RQF_NAME(name) [ilog2((__force u32)RQF_##name)] = #name
256 static const char *const rqf_name[] = {
257         RQF_NAME(SORTED),
258         RQF_NAME(STARTED),
259         RQF_NAME(QUEUED),
260         RQF_NAME(SOFTBARRIER),
261         RQF_NAME(FLUSH_SEQ),
262         RQF_NAME(MIXED_MERGE),
263         RQF_NAME(MQ_INFLIGHT),
264         RQF_NAME(DONTPREP),
265         RQF_NAME(PREEMPT),
266         RQF_NAME(COPY_USER),
267         RQF_NAME(FAILED),
268         RQF_NAME(QUIET),
269         RQF_NAME(ELVPRIV),
270         RQF_NAME(IO_STAT),
271         RQF_NAME(ALLOCED),
272         RQF_NAME(PM),
273         RQF_NAME(HASHED),
274         RQF_NAME(STATS),
275         RQF_NAME(SPECIAL_PAYLOAD),
276 };
277 #undef RQF_NAME
278
279 static int blk_mq_debugfs_rq_show(struct seq_file *m, void *v)
280 {
281         struct request *rq = list_entry_rq(v);
282         const struct blk_mq_ops *const mq_ops = rq->q->mq_ops;
283         const unsigned int op = rq->cmd_flags & REQ_OP_MASK;
284
285         seq_printf(m, "%p {.op=", rq);
286         if (op < ARRAY_SIZE(op_name) && op_name[op])
287                 seq_printf(m, "%s", op_name[op]);
288         else
289                 seq_printf(m, "%d", op);
290         seq_puts(m, ", .cmd_flags=");
291         blk_flags_show(m, rq->cmd_flags & ~REQ_OP_MASK, cmd_flag_name,
292                        ARRAY_SIZE(cmd_flag_name));
293         seq_puts(m, ", .rq_flags=");
294         blk_flags_show(m, (__force unsigned int)rq->rq_flags, rqf_name,
295                        ARRAY_SIZE(rqf_name));
296         seq_printf(m, ", .tag=%d, .internal_tag=%d", rq->tag,
297                    rq->internal_tag);
298         if (mq_ops->show_rq)
299                 mq_ops->show_rq(m, rq);
300         seq_puts(m, "}\n");
301         return 0;
302 }
303
304 static void *hctx_dispatch_start(struct seq_file *m, loff_t *pos)
305         __acquires(&hctx->lock)
306 {
307         struct blk_mq_hw_ctx *hctx = m->private;
308
309         spin_lock(&hctx->lock);
310         return seq_list_start(&hctx->dispatch, *pos);
311 }
312
313 static void *hctx_dispatch_next(struct seq_file *m, void *v, loff_t *pos)
314 {
315         struct blk_mq_hw_ctx *hctx = m->private;
316
317         return seq_list_next(v, &hctx->dispatch, pos);
318 }
319
320 static void hctx_dispatch_stop(struct seq_file *m, void *v)
321         __releases(&hctx->lock)
322 {
323         struct blk_mq_hw_ctx *hctx = m->private;
324
325         spin_unlock(&hctx->lock);
326 }
327
328 static const struct seq_operations hctx_dispatch_seq_ops = {
329         .start  = hctx_dispatch_start,
330         .next   = hctx_dispatch_next,
331         .stop   = hctx_dispatch_stop,
332         .show   = blk_mq_debugfs_rq_show,
333 };
334
335 static int hctx_ctx_map_show(void *data, struct seq_file *m)
336 {
337         struct blk_mq_hw_ctx *hctx = data;
338
339         sbitmap_bitmap_show(&hctx->ctx_map, m);
340         return 0;
341 }
342
343 static void blk_mq_debugfs_tags_show(struct seq_file *m,
344                                      struct blk_mq_tags *tags)
345 {
346         seq_printf(m, "nr_tags=%u\n", tags->nr_tags);
347         seq_printf(m, "nr_reserved_tags=%u\n", tags->nr_reserved_tags);
348         seq_printf(m, "active_queues=%d\n",
349                    atomic_read(&tags->active_queues));
350
351         seq_puts(m, "\nbitmap_tags:\n");
352         sbitmap_queue_show(&tags->bitmap_tags, m);
353
354         if (tags->nr_reserved_tags) {
355                 seq_puts(m, "\nbreserved_tags:\n");
356                 sbitmap_queue_show(&tags->breserved_tags, m);
357         }
358 }
359
360 static int hctx_tags_show(void *data, struct seq_file *m)
361 {
362         struct blk_mq_hw_ctx *hctx = data;
363         struct request_queue *q = hctx->queue;
364         int res;
365
366         res = mutex_lock_interruptible(&q->sysfs_lock);
367         if (res)
368                 goto out;
369         if (hctx->tags)
370                 blk_mq_debugfs_tags_show(m, hctx->tags);
371         mutex_unlock(&q->sysfs_lock);
372
373 out:
374         return res;
375 }
376
377 static int hctx_tags_bitmap_show(void *data, struct seq_file *m)
378 {
379         struct blk_mq_hw_ctx *hctx = data;
380         struct request_queue *q = hctx->queue;
381         int res;
382
383         res = mutex_lock_interruptible(&q->sysfs_lock);
384         if (res)
385                 goto out;
386         if (hctx->tags)
387                 sbitmap_bitmap_show(&hctx->tags->bitmap_tags.sb, m);
388         mutex_unlock(&q->sysfs_lock);
389
390 out:
391         return res;
392 }
393
394 static int hctx_sched_tags_show(void *data, struct seq_file *m)
395 {
396         struct blk_mq_hw_ctx *hctx = data;
397         struct request_queue *q = hctx->queue;
398         int res;
399
400         res = mutex_lock_interruptible(&q->sysfs_lock);
401         if (res)
402                 goto out;
403         if (hctx->sched_tags)
404                 blk_mq_debugfs_tags_show(m, hctx->sched_tags);
405         mutex_unlock(&q->sysfs_lock);
406
407 out:
408         return res;
409 }
410
411 static int hctx_sched_tags_bitmap_show(void *data, struct seq_file *m)
412 {
413         struct blk_mq_hw_ctx *hctx = data;
414         struct request_queue *q = hctx->queue;
415         int res;
416
417         res = mutex_lock_interruptible(&q->sysfs_lock);
418         if (res)
419                 goto out;
420         if (hctx->sched_tags)
421                 sbitmap_bitmap_show(&hctx->sched_tags->bitmap_tags.sb, m);
422         mutex_unlock(&q->sysfs_lock);
423
424 out:
425         return res;
426 }
427
428 static int hctx_io_poll_show(void *data, struct seq_file *m)
429 {
430         struct blk_mq_hw_ctx *hctx = data;
431
432         seq_printf(m, "considered=%lu\n", hctx->poll_considered);
433         seq_printf(m, "invoked=%lu\n", hctx->poll_invoked);
434         seq_printf(m, "success=%lu\n", hctx->poll_success);
435         return 0;
436 }
437
438 static ssize_t hctx_io_poll_write(void *data, const char __user *buf,
439                                   size_t count, loff_t *ppos)
440 {
441         struct blk_mq_hw_ctx *hctx = data;
442
443         hctx->poll_considered = hctx->poll_invoked = hctx->poll_success = 0;
444         return count;
445 }
446
447 static int hctx_dispatched_show(void *data, struct seq_file *m)
448 {
449         struct blk_mq_hw_ctx *hctx = data;
450         int i;
451
452         seq_printf(m, "%8u\t%lu\n", 0U, hctx->dispatched[0]);
453
454         for (i = 1; i < BLK_MQ_MAX_DISPATCH_ORDER - 1; i++) {
455                 unsigned int d = 1U << (i - 1);
456
457                 seq_printf(m, "%8u\t%lu\n", d, hctx->dispatched[i]);
458         }
459
460         seq_printf(m, "%8u+\t%lu\n", 1U << (i - 1), hctx->dispatched[i]);
461         return 0;
462 }
463
464 static ssize_t hctx_dispatched_write(void *data, const char __user *buf,
465                                      size_t count, loff_t *ppos)
466 {
467         struct blk_mq_hw_ctx *hctx = data;
468         int i;
469
470         for (i = 0; i < BLK_MQ_MAX_DISPATCH_ORDER; i++)
471                 hctx->dispatched[i] = 0;
472         return count;
473 }
474
475 static int hctx_queued_show(void *data, struct seq_file *m)
476 {
477         struct blk_mq_hw_ctx *hctx = data;
478
479         seq_printf(m, "%lu\n", hctx->queued);
480         return 0;
481 }
482
483 static ssize_t hctx_queued_write(void *data, const char __user *buf,
484                                  size_t count, loff_t *ppos)
485 {
486         struct blk_mq_hw_ctx *hctx = data;
487
488         hctx->queued = 0;
489         return count;
490 }
491
492 static int hctx_run_show(void *data, struct seq_file *m)
493 {
494         struct blk_mq_hw_ctx *hctx = data;
495
496         seq_printf(m, "%lu\n", hctx->run);
497         return 0;
498 }
499
500 static ssize_t hctx_run_write(void *data, const char __user *buf, size_t count,
501                               loff_t *ppos)
502 {
503         struct blk_mq_hw_ctx *hctx = data;
504
505         hctx->run = 0;
506         return count;
507 }
508
509 static int hctx_active_show(void *data, struct seq_file *m)
510 {
511         struct blk_mq_hw_ctx *hctx = data;
512
513         seq_printf(m, "%d\n", atomic_read(&hctx->nr_active));
514         return 0;
515 }
516
517 static void *ctx_rq_list_start(struct seq_file *m, loff_t *pos)
518         __acquires(&ctx->lock)
519 {
520         struct blk_mq_ctx *ctx = m->private;
521
522         spin_lock(&ctx->lock);
523         return seq_list_start(&ctx->rq_list, *pos);
524 }
525
526 static void *ctx_rq_list_next(struct seq_file *m, void *v, loff_t *pos)
527 {
528         struct blk_mq_ctx *ctx = m->private;
529
530         return seq_list_next(v, &ctx->rq_list, pos);
531 }
532
533 static void ctx_rq_list_stop(struct seq_file *m, void *v)
534         __releases(&ctx->lock)
535 {
536         struct blk_mq_ctx *ctx = m->private;
537
538         spin_unlock(&ctx->lock);
539 }
540
541 static const struct seq_operations ctx_rq_list_seq_ops = {
542         .start  = ctx_rq_list_start,
543         .next   = ctx_rq_list_next,
544         .stop   = ctx_rq_list_stop,
545         .show   = blk_mq_debugfs_rq_show,
546 };
547 static int ctx_dispatched_show(void *data, struct seq_file *m)
548 {
549         struct blk_mq_ctx *ctx = data;
550
551         seq_printf(m, "%lu %lu\n", ctx->rq_dispatched[1], ctx->rq_dispatched[0]);
552         return 0;
553 }
554
555 static ssize_t ctx_dispatched_write(void *data, const char __user *buf,
556                                     size_t count, loff_t *ppos)
557 {
558         struct blk_mq_ctx *ctx = data;
559
560         ctx->rq_dispatched[0] = ctx->rq_dispatched[1] = 0;
561         return count;
562 }
563
564 static int ctx_merged_show(void *data, struct seq_file *m)
565 {
566         struct blk_mq_ctx *ctx = data;
567
568         seq_printf(m, "%lu\n", ctx->rq_merged);
569         return 0;
570 }
571
572 static ssize_t ctx_merged_write(void *data, const char __user *buf,
573                                 size_t count, loff_t *ppos)
574 {
575         struct blk_mq_ctx *ctx = data;
576
577         ctx->rq_merged = 0;
578         return count;
579 }
580
581 static int ctx_completed_show(void *data, struct seq_file *m)
582 {
583         struct blk_mq_ctx *ctx = data;
584
585         seq_printf(m, "%lu %lu\n", ctx->rq_completed[1], ctx->rq_completed[0]);
586         return 0;
587 }
588
589 static ssize_t ctx_completed_write(void *data, const char __user *buf,
590                                    size_t count, loff_t *ppos)
591 {
592         struct blk_mq_ctx *ctx = data;
593
594         ctx->rq_completed[0] = ctx->rq_completed[1] = 0;
595         return count;
596 }
597
598 static int blk_mq_debugfs_show(struct seq_file *m, void *v)
599 {
600         const struct blk_mq_debugfs_attr *attr = m->private;
601         void *data = d_inode(m->file->f_path.dentry->d_parent)->i_private;
602
603         return attr->show(data, m);
604 }
605
606 static ssize_t blk_mq_debugfs_write(struct file *file, const char __user *buf,
607                                     size_t count, loff_t *ppos)
608 {
609         struct seq_file *m = file->private_data;
610         const struct blk_mq_debugfs_attr *attr = m->private;
611         void *data = d_inode(file->f_path.dentry->d_parent)->i_private;
612
613         if (!attr->write)
614                 return -EPERM;
615
616         return attr->write(data, buf, count, ppos);
617 }
618
619 static int blk_mq_debugfs_open(struct inode *inode, struct file *file)
620 {
621         const struct blk_mq_debugfs_attr *attr = inode->i_private;
622         void *data = d_inode(file->f_path.dentry->d_parent)->i_private;
623         struct seq_file *m;
624         int ret;
625
626         if (attr->seq_ops) {
627                 ret = seq_open(file, attr->seq_ops);
628                 if (!ret) {
629                         m = file->private_data;
630                         m->private = data;
631                 }
632                 return ret;
633         }
634
635         if (WARN_ON_ONCE(!attr->show))
636                 return -EPERM;
637
638         return single_open(file, blk_mq_debugfs_show, inode->i_private);
639 }
640
641 static int blk_mq_debugfs_release(struct inode *inode, struct file *file)
642 {
643         const struct blk_mq_debugfs_attr *attr = inode->i_private;
644
645         if (attr->show)
646                 return single_release(inode, file);
647         else
648                 return seq_release(inode, file);
649 }
650
651 const struct file_operations blk_mq_debugfs_fops = {
652         .open           = blk_mq_debugfs_open,
653         .read           = seq_read,
654         .write          = blk_mq_debugfs_write,
655         .llseek         = seq_lseek,
656         .release        = blk_mq_debugfs_release,
657 };
658
659 static const struct blk_mq_debugfs_attr blk_mq_debugfs_queue_attrs[] = {
660         {"poll_stat", 0400, queue_poll_stat_show},
661         {"state", 0600, queue_state_show, queue_state_write},
662         {},
663 };
664
665 static const struct blk_mq_debugfs_attr blk_mq_debugfs_hctx_attrs[] = {
666         {"state", 0400, hctx_state_show},
667         {"flags", 0400, hctx_flags_show},
668         {"dispatch", 0400, .seq_ops = &hctx_dispatch_seq_ops},
669         {"ctx_map", 0400, hctx_ctx_map_show},
670         {"tags", 0400, hctx_tags_show},
671         {"tags_bitmap", 0400, hctx_tags_bitmap_show},
672         {"sched_tags", 0400, hctx_sched_tags_show},
673         {"sched_tags_bitmap", 0400, hctx_sched_tags_bitmap_show},
674         {"io_poll", 0600, hctx_io_poll_show, hctx_io_poll_write},
675         {"dispatched", 0600, hctx_dispatched_show, hctx_dispatched_write},
676         {"queued", 0600, hctx_queued_show, hctx_queued_write},
677         {"run", 0600, hctx_run_show, hctx_run_write},
678         {"active", 0400, hctx_active_show},
679         {},
680 };
681
682 static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = {
683         {"rq_list", 0400, .seq_ops = &ctx_rq_list_seq_ops},
684         {"dispatched", 0600, ctx_dispatched_show, ctx_dispatched_write},
685         {"merged", 0600, ctx_merged_show, ctx_merged_write},
686         {"completed", 0600, ctx_completed_show, ctx_completed_write},
687         {},
688 };
689
690 int blk_mq_debugfs_register(struct request_queue *q)
691 {
692         if (!blk_debugfs_root)
693                 return -ENOENT;
694
695         q->debugfs_dir = debugfs_create_dir(kobject_name(q->kobj.parent),
696                                             blk_debugfs_root);
697         if (!q->debugfs_dir)
698                 goto err;
699
700         if (blk_mq_debugfs_register_mq(q))
701                 goto err;
702
703         return 0;
704
705 err:
706         blk_mq_debugfs_unregister(q);
707         return -ENOMEM;
708 }
709
710 void blk_mq_debugfs_unregister(struct request_queue *q)
711 {
712         debugfs_remove_recursive(q->debugfs_dir);
713         q->mq_debugfs_dir = NULL;
714         q->debugfs_dir = NULL;
715 }
716
717 static bool debugfs_create_files(struct dentry *parent, void *data,
718                                  const struct blk_mq_debugfs_attr *attr)
719 {
720         d_inode(parent)->i_private = data;
721
722         for (; attr->name; attr++) {
723                 if (!debugfs_create_file(attr->name, attr->mode, parent,
724                                          (void *)attr, &blk_mq_debugfs_fops))
725                         return false;
726         }
727         return true;
728 }
729
730 static int blk_mq_debugfs_register_ctx(struct request_queue *q,
731                                        struct blk_mq_ctx *ctx,
732                                        struct dentry *hctx_dir)
733 {
734         struct dentry *ctx_dir;
735         char name[20];
736
737         snprintf(name, sizeof(name), "cpu%u", ctx->cpu);
738         ctx_dir = debugfs_create_dir(name, hctx_dir);
739         if (!ctx_dir)
740                 return -ENOMEM;
741
742         if (!debugfs_create_files(ctx_dir, ctx, blk_mq_debugfs_ctx_attrs))
743                 return -ENOMEM;
744
745         return 0;
746 }
747
748 static int blk_mq_debugfs_register_hctx(struct request_queue *q,
749                                         struct blk_mq_hw_ctx *hctx)
750 {
751         struct blk_mq_ctx *ctx;
752         struct dentry *hctx_dir;
753         char name[20];
754         int i;
755
756         snprintf(name, sizeof(name), "hctx%u", hctx->queue_num);
757         hctx_dir = debugfs_create_dir(name, q->mq_debugfs_dir);
758         if (!hctx_dir)
759                 return -ENOMEM;
760
761         if (!debugfs_create_files(hctx_dir, hctx, blk_mq_debugfs_hctx_attrs))
762                 return -ENOMEM;
763
764         hctx_for_each_ctx(hctx, ctx, i) {
765                 if (blk_mq_debugfs_register_ctx(q, ctx, hctx_dir))
766                         return -ENOMEM;
767         }
768
769         return 0;
770 }
771
772 int blk_mq_debugfs_register_mq(struct request_queue *q)
773 {
774         struct blk_mq_hw_ctx *hctx;
775         int i;
776
777         if (!q->debugfs_dir)
778                 return -ENOENT;
779
780         q->mq_debugfs_dir = debugfs_create_dir("mq", q->debugfs_dir);
781         if (!q->mq_debugfs_dir)
782                 goto err;
783
784         if (!debugfs_create_files(q->mq_debugfs_dir, q, blk_mq_debugfs_queue_attrs))
785                 goto err;
786
787         queue_for_each_hw_ctx(q, hctx, i) {
788                 if (blk_mq_debugfs_register_hctx(q, hctx))
789                         goto err;
790         }
791
792         return 0;
793
794 err:
795         blk_mq_debugfs_unregister_mq(q);
796         return -ENOMEM;
797 }
798
799 void blk_mq_debugfs_unregister_mq(struct request_queue *q)
800 {
801         debugfs_remove_recursive(q->mq_debugfs_dir);
802         q->mq_debugfs_dir = NULL;
803 }