]> asedeno.scripts.mit.edu Git - linux.git/blob - block/blk-mq-sysfs.c
blk-mq: move debugfs declarations to a separate header file
[linux.git] / block / blk-mq-sysfs.c
1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/backing-dev.h>
4 #include <linux/bio.h>
5 #include <linux/blkdev.h>
6 #include <linux/mm.h>
7 #include <linux/init.h>
8 #include <linux/slab.h>
9 #include <linux/workqueue.h>
10 #include <linux/smp.h>
11
12 #include <linux/blk-mq.h>
13 #include "blk-mq.h"
14 #include "blk-mq-debugfs.h"
15 #include "blk-mq-tag.h"
16
17 static void blk_mq_sysfs_release(struct kobject *kobj)
18 {
19 }
20
21 static void blk_mq_hw_sysfs_release(struct kobject *kobj)
22 {
23         struct blk_mq_hw_ctx *hctx = container_of(kobj, struct blk_mq_hw_ctx,
24                                                   kobj);
25         free_cpumask_var(hctx->cpumask);
26         kfree(hctx->ctxs);
27         kfree(hctx);
28 }
29
30 struct blk_mq_ctx_sysfs_entry {
31         struct attribute attr;
32         ssize_t (*show)(struct blk_mq_ctx *, char *);
33         ssize_t (*store)(struct blk_mq_ctx *, const char *, size_t);
34 };
35
36 struct blk_mq_hw_ctx_sysfs_entry {
37         struct attribute attr;
38         ssize_t (*show)(struct blk_mq_hw_ctx *, char *);
39         ssize_t (*store)(struct blk_mq_hw_ctx *, const char *, size_t);
40 };
41
42 static ssize_t blk_mq_sysfs_show(struct kobject *kobj, struct attribute *attr,
43                                  char *page)
44 {
45         struct blk_mq_ctx_sysfs_entry *entry;
46         struct blk_mq_ctx *ctx;
47         struct request_queue *q;
48         ssize_t res;
49
50         entry = container_of(attr, struct blk_mq_ctx_sysfs_entry, attr);
51         ctx = container_of(kobj, struct blk_mq_ctx, kobj);
52         q = ctx->queue;
53
54         if (!entry->show)
55                 return -EIO;
56
57         res = -ENOENT;
58         mutex_lock(&q->sysfs_lock);
59         if (!blk_queue_dying(q))
60                 res = entry->show(ctx, page);
61         mutex_unlock(&q->sysfs_lock);
62         return res;
63 }
64
65 static ssize_t blk_mq_sysfs_store(struct kobject *kobj, struct attribute *attr,
66                                   const char *page, size_t length)
67 {
68         struct blk_mq_ctx_sysfs_entry *entry;
69         struct blk_mq_ctx *ctx;
70         struct request_queue *q;
71         ssize_t res;
72
73         entry = container_of(attr, struct blk_mq_ctx_sysfs_entry, attr);
74         ctx = container_of(kobj, struct blk_mq_ctx, kobj);
75         q = ctx->queue;
76
77         if (!entry->store)
78                 return -EIO;
79
80         res = -ENOENT;
81         mutex_lock(&q->sysfs_lock);
82         if (!blk_queue_dying(q))
83                 res = entry->store(ctx, page, length);
84         mutex_unlock(&q->sysfs_lock);
85         return res;
86 }
87
88 static ssize_t blk_mq_hw_sysfs_show(struct kobject *kobj,
89                                     struct attribute *attr, char *page)
90 {
91         struct blk_mq_hw_ctx_sysfs_entry *entry;
92         struct blk_mq_hw_ctx *hctx;
93         struct request_queue *q;
94         ssize_t res;
95
96         entry = container_of(attr, struct blk_mq_hw_ctx_sysfs_entry, attr);
97         hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
98         q = hctx->queue;
99
100         if (!entry->show)
101                 return -EIO;
102
103         res = -ENOENT;
104         mutex_lock(&q->sysfs_lock);
105         if (!blk_queue_dying(q))
106                 res = entry->show(hctx, page);
107         mutex_unlock(&q->sysfs_lock);
108         return res;
109 }
110
111 static ssize_t blk_mq_hw_sysfs_store(struct kobject *kobj,
112                                      struct attribute *attr, const char *page,
113                                      size_t length)
114 {
115         struct blk_mq_hw_ctx_sysfs_entry *entry;
116         struct blk_mq_hw_ctx *hctx;
117         struct request_queue *q;
118         ssize_t res;
119
120         entry = container_of(attr, struct blk_mq_hw_ctx_sysfs_entry, attr);
121         hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
122         q = hctx->queue;
123
124         if (!entry->store)
125                 return -EIO;
126
127         res = -ENOENT;
128         mutex_lock(&q->sysfs_lock);
129         if (!blk_queue_dying(q))
130                 res = entry->store(hctx, page, length);
131         mutex_unlock(&q->sysfs_lock);
132         return res;
133 }
134
135 static ssize_t blk_mq_hw_sysfs_nr_tags_show(struct blk_mq_hw_ctx *hctx,
136                                             char *page)
137 {
138         return sprintf(page, "%u\n", hctx->tags->nr_tags);
139 }
140
141 static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,
142                                                      char *page)
143 {
144         return sprintf(page, "%u\n", hctx->tags->nr_reserved_tags);
145 }
146
147 static ssize_t blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx *hctx, char *page)
148 {
149         unsigned int i, first = 1;
150         ssize_t ret = 0;
151
152         for_each_cpu(i, hctx->cpumask) {
153                 if (first)
154                         ret += sprintf(ret + page, "%u", i);
155                 else
156                         ret += sprintf(ret + page, ", %u", i);
157
158                 first = 0;
159         }
160
161         ret += sprintf(ret + page, "\n");
162         return ret;
163 }
164
165 static struct attribute *default_ctx_attrs[] = {
166         NULL,
167 };
168
169 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags = {
170         .attr = {.name = "nr_tags", .mode = S_IRUGO },
171         .show = blk_mq_hw_sysfs_nr_tags_show,
172 };
173 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_reserved_tags = {
174         .attr = {.name = "nr_reserved_tags", .mode = S_IRUGO },
175         .show = blk_mq_hw_sysfs_nr_reserved_tags_show,
176 };
177 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_cpus = {
178         .attr = {.name = "cpu_list", .mode = S_IRUGO },
179         .show = blk_mq_hw_sysfs_cpus_show,
180 };
181
182 static struct attribute *default_hw_ctx_attrs[] = {
183         &blk_mq_hw_sysfs_nr_tags.attr,
184         &blk_mq_hw_sysfs_nr_reserved_tags.attr,
185         &blk_mq_hw_sysfs_cpus.attr,
186         NULL,
187 };
188
189 static const struct sysfs_ops blk_mq_sysfs_ops = {
190         .show   = blk_mq_sysfs_show,
191         .store  = blk_mq_sysfs_store,
192 };
193
194 static const struct sysfs_ops blk_mq_hw_sysfs_ops = {
195         .show   = blk_mq_hw_sysfs_show,
196         .store  = blk_mq_hw_sysfs_store,
197 };
198
199 static struct kobj_type blk_mq_ktype = {
200         .sysfs_ops      = &blk_mq_sysfs_ops,
201         .release        = blk_mq_sysfs_release,
202 };
203
204 static struct kobj_type blk_mq_ctx_ktype = {
205         .sysfs_ops      = &blk_mq_sysfs_ops,
206         .default_attrs  = default_ctx_attrs,
207         .release        = blk_mq_sysfs_release,
208 };
209
210 static struct kobj_type blk_mq_hw_ktype = {
211         .sysfs_ops      = &blk_mq_hw_sysfs_ops,
212         .default_attrs  = default_hw_ctx_attrs,
213         .release        = blk_mq_hw_sysfs_release,
214 };
215
216 static void blk_mq_unregister_hctx(struct blk_mq_hw_ctx *hctx)
217 {
218         struct blk_mq_ctx *ctx;
219         int i;
220
221         if (!hctx->nr_ctx)
222                 return;
223
224         hctx_for_each_ctx(hctx, ctx, i)
225                 kobject_del(&ctx->kobj);
226
227         kobject_del(&hctx->kobj);
228 }
229
230 static int blk_mq_register_hctx(struct blk_mq_hw_ctx *hctx)
231 {
232         struct request_queue *q = hctx->queue;
233         struct blk_mq_ctx *ctx;
234         int i, ret;
235
236         if (!hctx->nr_ctx)
237                 return 0;
238
239         ret = kobject_add(&hctx->kobj, &q->mq_kobj, "%u", hctx->queue_num);
240         if (ret)
241                 return ret;
242
243         hctx_for_each_ctx(hctx, ctx, i) {
244                 ret = kobject_add(&ctx->kobj, &hctx->kobj, "cpu%u", ctx->cpu);
245                 if (ret)
246                         break;
247         }
248
249         return ret;
250 }
251
252 static void __blk_mq_unregister_dev(struct device *dev, struct request_queue *q)
253 {
254         struct blk_mq_hw_ctx *hctx;
255         int i;
256
257         lockdep_assert_held(&q->sysfs_lock);
258
259         queue_for_each_hw_ctx(q, hctx, i)
260                 blk_mq_unregister_hctx(hctx);
261
262         blk_mq_debugfs_unregister_mq(q);
263
264         kobject_uevent(&q->mq_kobj, KOBJ_REMOVE);
265         kobject_del(&q->mq_kobj);
266         kobject_put(&dev->kobj);
267
268         q->mq_sysfs_init_done = false;
269 }
270
271 void blk_mq_unregister_dev(struct device *dev, struct request_queue *q)
272 {
273         mutex_lock(&q->sysfs_lock);
274         __blk_mq_unregister_dev(dev, q);
275         mutex_unlock(&q->sysfs_lock);
276 }
277
278 void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx)
279 {
280         kobject_init(&hctx->kobj, &blk_mq_hw_ktype);
281 }
282
283 void blk_mq_sysfs_deinit(struct request_queue *q)
284 {
285         struct blk_mq_ctx *ctx;
286         int cpu;
287
288         for_each_possible_cpu(cpu) {
289                 ctx = per_cpu_ptr(q->queue_ctx, cpu);
290                 kobject_put(&ctx->kobj);
291         }
292         kobject_put(&q->mq_kobj);
293 }
294
295 void blk_mq_sysfs_init(struct request_queue *q)
296 {
297         struct blk_mq_ctx *ctx;
298         int cpu;
299
300         kobject_init(&q->mq_kobj, &blk_mq_ktype);
301
302         for_each_possible_cpu(cpu) {
303                 ctx = per_cpu_ptr(q->queue_ctx, cpu);
304                 kobject_init(&ctx->kobj, &blk_mq_ctx_ktype);
305         }
306 }
307
308 int __blk_mq_register_dev(struct device *dev, struct request_queue *q)
309 {
310         struct blk_mq_hw_ctx *hctx;
311         int ret, i;
312
313         WARN_ON_ONCE(!q->kobj.parent);
314         lockdep_assert_held(&q->sysfs_lock);
315
316         ret = kobject_add(&q->mq_kobj, kobject_get(&dev->kobj), "%s", "mq");
317         if (ret < 0)
318                 goto out;
319
320         kobject_uevent(&q->mq_kobj, KOBJ_ADD);
321
322         blk_mq_debugfs_register(q);
323
324         queue_for_each_hw_ctx(q, hctx, i) {
325                 ret = blk_mq_register_hctx(hctx);
326                 if (ret)
327                         goto unreg;
328         }
329
330         q->mq_sysfs_init_done = true;
331
332 out:
333         return ret;
334
335 unreg:
336         while (--i >= 0)
337                 blk_mq_unregister_hctx(q->queue_hw_ctx[i]);
338
339         blk_mq_debugfs_unregister_mq(q);
340
341         kobject_uevent(&q->mq_kobj, KOBJ_REMOVE);
342         kobject_del(&q->mq_kobj);
343         kobject_put(&dev->kobj);
344         return ret;
345 }
346
347 int blk_mq_register_dev(struct device *dev, struct request_queue *q)
348 {
349         int ret;
350
351         mutex_lock(&q->sysfs_lock);
352         ret = __blk_mq_register_dev(dev, q);
353         mutex_unlock(&q->sysfs_lock);
354
355         return ret;
356 }
357 EXPORT_SYMBOL_GPL(blk_mq_register_dev);
358
359 void blk_mq_sysfs_unregister(struct request_queue *q)
360 {
361         struct blk_mq_hw_ctx *hctx;
362         int i;
363
364         mutex_lock(&q->sysfs_lock);
365         if (!q->mq_sysfs_init_done)
366                 goto unlock;
367
368         blk_mq_debugfs_unregister_mq(q);
369
370         queue_for_each_hw_ctx(q, hctx, i)
371                 blk_mq_unregister_hctx(hctx);
372
373 unlock:
374         mutex_unlock(&q->sysfs_lock);
375 }
376
377 int blk_mq_sysfs_register(struct request_queue *q)
378 {
379         struct blk_mq_hw_ctx *hctx;
380         int i, ret = 0;
381
382         mutex_lock(&q->sysfs_lock);
383         if (!q->mq_sysfs_init_done)
384                 goto unlock;
385
386         blk_mq_debugfs_register_mq(q);
387
388         queue_for_each_hw_ctx(q, hctx, i) {
389                 ret = blk_mq_register_hctx(hctx);
390                 if (ret)
391                         break;
392         }
393
394 unlock:
395         mutex_unlock(&q->sysfs_lock);
396
397         return ret;
398 }