trace: better manage the context info for events
[deliverable/linux.git] / block / blktrace.c
CommitLineData
2056a782 1/*
0fe23479 2 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
2056a782
JA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * 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
11 * GNU 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, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 *
17 */
2056a782
JA
18#include <linux/kernel.h>
19#include <linux/blkdev.h>
20#include <linux/blktrace_api.h>
21#include <linux/percpu.h>
22#include <linux/init.h>
23#include <linux/mutex.h>
24#include <linux/debugfs.h>
be1c6341 25#include <linux/time.h>
5f3ea37c 26#include <trace/block.h>
2056a782 27#include <asm/uaccess.h>
c71a8961 28#include <../kernel/trace/trace_output.h>
2056a782 29
2056a782
JA
30static unsigned int blktrace_seq __read_mostly = 1;
31
c71a8961
ACM
32static struct trace_array *blk_tr;
33static int __read_mostly blk_tracer_enabled;
34
35/* Select an alternative, minimalistic output than the original one */
36#define TRACE_BLK_OPT_CLASSIC 0x1
37
38static struct tracer_opt blk_tracer_opts[] = {
39 /* Default disable the minimalistic output */
157f9c00 40 { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },
c71a8961
ACM
41 { }
42};
43
44static struct tracer_flags blk_tracer_flags = {
45 .val = 0,
46 .opts = blk_tracer_opts,
47};
48
5f3ea37c
ACM
49/* Global reference count of probes */
50static DEFINE_MUTEX(blk_probe_mutex);
51static atomic_t blk_probes_ref = ATOMIC_INIT(0);
52
53static int blk_register_tracepoints(void);
54static void blk_unregister_tracepoints(void);
55
be1c6341
OK
56/*
57 * Send out a notify message.
58 */
a863055b
JA
59static void trace_note(struct blk_trace *bt, pid_t pid, int action,
60 const void *data, size_t len)
be1c6341
OK
61{
62 struct blk_io_trace *t;
be1c6341 63
c71a8961
ACM
64 if (!bt->rchan)
65 return;
66
be1c6341 67 t = relay_reserve(bt->rchan, sizeof(*t) + len);
d3d9d2a5
JA
68 if (t) {
69 const int cpu = smp_processor_id();
70
71 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
2997c8c4 72 t->time = ktime_to_ns(ktime_get());
d3d9d2a5
JA
73 t->device = bt->dev;
74 t->action = action;
75 t->pid = pid;
76 t->cpu = cpu;
77 t->pdu_len = len;
78 memcpy((void *) t + sizeof(*t), data, len);
79 }
be1c6341
OK
80}
81
2056a782
JA
82/*
83 * Send out a notify for this process, if we haven't done so since a trace
84 * started
85 */
86static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk)
87{
a863055b
JA
88 tsk->btrace_seq = blktrace_seq;
89 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm));
be1c6341 90}
2056a782 91
be1c6341
OK
92static void trace_note_time(struct blk_trace *bt)
93{
94 struct timespec now;
95 unsigned long flags;
96 u32 words[2];
97
98 getnstimeofday(&now);
99 words[0] = now.tv_sec;
100 words[1] = now.tv_nsec;
101
102 local_irq_save(flags);
103 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
104 local_irq_restore(flags);
2056a782
JA
105}
106
9d5f09a4
AB
107void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
108{
109 int n;
110 va_list args;
14a73f54 111 unsigned long flags;
64565911 112 char *buf;
9d5f09a4 113
c71a8961
ACM
114 if (blk_tr) {
115 va_start(args, fmt);
116 ftrace_vprintk(fmt, args);
117 va_end(args);
118 return;
119 }
120
121 if (!bt->msg_data)
122 return;
123
14a73f54 124 local_irq_save(flags);
64565911 125 buf = per_cpu_ptr(bt->msg_data, smp_processor_id());
9d5f09a4 126 va_start(args, fmt);
64565911 127 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
9d5f09a4
AB
128 va_end(args);
129
64565911 130 trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
14a73f54 131 local_irq_restore(flags);
9d5f09a4
AB
132}
133EXPORT_SYMBOL_GPL(__trace_note_message);
134
2056a782
JA
135static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
136 pid_t pid)
137{
138 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
139 return 1;
140 if (sector < bt->start_lba || sector > bt->end_lba)
141 return 1;
142 if (bt->pid && pid != bt->pid)
143 return 1;
144
145 return 0;
146}
147
148/*
149 * Data direction bit lookup
150 */
151static u32 ddir_act[2] __read_mostly = { BLK_TC_ACT(BLK_TC_READ), BLK_TC_ACT(BLK_TC_WRITE) };
152
35ba8f70
DW
153/* The ilog2() calls fall out because they're constant */
154#define MASK_TC_BIT(rw, __name) ( (rw & (1 << BIO_RW_ ## __name)) << \
155 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name) )
2056a782
JA
156
157/*
158 * The worker for the various blk_add_trace*() types. Fills out a
159 * blk_io_trace structure and places it in a per-cpu subbuffer.
160 */
5f3ea37c 161static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
2056a782
JA
162 int rw, u32 what, int error, int pdu_len, void *pdu_data)
163{
164 struct task_struct *tsk = current;
c71a8961 165 struct ring_buffer_event *event = NULL;
2056a782
JA
166 struct blk_io_trace *t;
167 unsigned long flags;
168 unsigned long *sequence;
169 pid_t pid;
c71a8961 170 int cpu, pc = 0;
2056a782 171
157f9c00
ACM
172 if (unlikely(bt->trace_state != Blktrace_running ||
173 !blk_tracer_enabled))
2056a782
JA
174 return;
175
176 what |= ddir_act[rw & WRITE];
35ba8f70
DW
177 what |= MASK_TC_BIT(rw, BARRIER);
178 what |= MASK_TC_BIT(rw, SYNC);
179 what |= MASK_TC_BIT(rw, AHEAD);
180 what |= MASK_TC_BIT(rw, META);
181 what |= MASK_TC_BIT(rw, DISCARD);
2056a782
JA
182
183 pid = tsk->pid;
184 if (unlikely(act_log_check(bt, what, sector, pid)))
185 return;
c71a8961
ACM
186 cpu = raw_smp_processor_id();
187
188 if (blk_tr) {
189 struct trace_entry *ent;
190 tracing_record_cmdline(current);
191
192 event = ring_buffer_lock_reserve(blk_tr->buffer,
193 sizeof(*t) + pdu_len, &flags);
194 if (!event)
195 return;
157f9c00 196
c71a8961
ACM
197 ent = ring_buffer_event_data(event);
198 t = (struct blk_io_trace *)ent;
199 pc = preempt_count();
200 tracing_generic_entry_update(ent, 0, pc);
201 ent->type = TRACE_BLK;
202 goto record_it;
203 }
2056a782
JA
204
205 /*
206 * A word about the locking here - we disable interrupts to reserve
207 * some space in the relay per-cpu buffer, to prevent an irq
14a73f54 208 * from coming in and stepping on our toes.
2056a782
JA
209 */
210 local_irq_save(flags);
211
212 if (unlikely(tsk->btrace_seq != blktrace_seq))
213 trace_note_tsk(bt, tsk);
214
215 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
216 if (t) {
2056a782
JA
217 sequence = per_cpu_ptr(bt->sequence, cpu);
218
219 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
220 t->sequence = ++(*sequence);
2997c8c4 221 t->time = ktime_to_ns(ktime_get());
c71a8961
ACM
222 t->cpu = cpu;
223 t->pid = pid;
224record_it:
2056a782
JA
225 t->sector = sector;
226 t->bytes = bytes;
227 t->action = what;
2056a782 228 t->device = bt->dev;
2056a782
JA
229 t->error = error;
230 t->pdu_len = pdu_len;
231
232 if (pdu_len)
233 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
c71a8961
ACM
234
235 if (blk_tr) {
236 ring_buffer_unlock_commit(blk_tr->buffer, event, flags);
237 if (pid != 0 &&
157f9c00 238 !(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC) &&
c71a8961
ACM
239 (trace_flags & TRACE_ITER_STACKTRACE) != 0)
240 __trace_stack(blk_tr, NULL, flags, 5, pc);
241 trace_wake_up();
242 return;
243 }
2056a782
JA
244 }
245
246 local_irq_restore(flags);
247}
248
2056a782 249static struct dentry *blk_tree_root;
11a57153 250static DEFINE_MUTEX(blk_tree_mutex);
2056a782
JA
251
252static void blk_trace_cleanup(struct blk_trace *bt)
253{
02c62304 254 debugfs_remove(bt->msg_file);
2056a782 255 debugfs_remove(bt->dropped_file);
f48fc4d3 256 relay_close(bt->rchan);
2056a782 257 free_percpu(bt->sequence);
64565911 258 free_percpu(bt->msg_data);
2056a782 259 kfree(bt);
5f3ea37c
ACM
260 mutex_lock(&blk_probe_mutex);
261 if (atomic_dec_and_test(&blk_probes_ref))
262 blk_unregister_tracepoints();
263 mutex_unlock(&blk_probe_mutex);
2056a782
JA
264}
265
6da127ad 266int blk_trace_remove(struct request_queue *q)
2056a782
JA
267{
268 struct blk_trace *bt;
269
270 bt = xchg(&q->blk_trace, NULL);
271 if (!bt)
272 return -EINVAL;
273
274 if (bt->trace_state == Blktrace_setup ||
275 bt->trace_state == Blktrace_stopped)
276 blk_trace_cleanup(bt);
277
278 return 0;
279}
6da127ad 280EXPORT_SYMBOL_GPL(blk_trace_remove);
2056a782
JA
281
282static int blk_dropped_open(struct inode *inode, struct file *filp)
283{
8e18e294 284 filp->private_data = inode->i_private;
2056a782
JA
285
286 return 0;
287}
288
289static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
290 size_t count, loff_t *ppos)
291{
292 struct blk_trace *bt = filp->private_data;
293 char buf[16];
294
295 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
296
297 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
298}
299
2b8693c0 300static const struct file_operations blk_dropped_fops = {
2056a782
JA
301 .owner = THIS_MODULE,
302 .open = blk_dropped_open,
303 .read = blk_dropped_read,
304};
305
02c62304
AB
306static int blk_msg_open(struct inode *inode, struct file *filp)
307{
308 filp->private_data = inode->i_private;
309
310 return 0;
311}
312
313static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
314 size_t count, loff_t *ppos)
315{
316 char *msg;
317 struct blk_trace *bt;
318
319 if (count > BLK_TN_MAX_MSG)
320 return -EINVAL;
321
322 msg = kmalloc(count, GFP_KERNEL);
323 if (msg == NULL)
324 return -ENOMEM;
325
326 if (copy_from_user(msg, buffer, count)) {
327 kfree(msg);
328 return -EFAULT;
329 }
330
331 bt = filp->private_data;
332 __trace_note_message(bt, "%s", msg);
333 kfree(msg);
334
335 return count;
336}
337
338static const struct file_operations blk_msg_fops = {
339 .owner = THIS_MODULE,
340 .open = blk_msg_open,
341 .write = blk_msg_write,
342};
343
2056a782
JA
344/*
345 * Keep track of how many times we encountered a full subbuffer, to aid
346 * the user space app in telling how many lost events there were.
347 */
348static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
349 void *prev_subbuf, size_t prev_padding)
350{
351 struct blk_trace *bt;
352
353 if (!relay_buf_full(buf))
354 return 1;
355
356 bt = buf->chan->private_data;
357 atomic_inc(&bt->dropped);
358 return 0;
359}
360
361static int blk_remove_buf_file_callback(struct dentry *dentry)
362{
f48fc4d3 363 struct dentry *parent = dentry->d_parent;
2056a782 364 debugfs_remove(dentry);
f48fc4d3
JA
365
366 /*
367 * this will fail for all but the last file, but that is ok. what we
368 * care about is the top level buts->name directory going away, when
369 * the last trace file is gone. Then we don't have to rmdir() that
370 * manually on trace stop, so it nicely solves the issue with
371 * force killing of running traces.
372 */
373
374 debugfs_remove(parent);
2056a782
JA
375 return 0;
376}
377
378static struct dentry *blk_create_buf_file_callback(const char *filename,
379 struct dentry *parent,
380 int mode,
381 struct rchan_buf *buf,
382 int *is_global)
383{
384 return debugfs_create_file(filename, mode, parent, buf,
385 &relay_file_operations);
386}
387
388static struct rchan_callbacks blk_relay_callbacks = {
389 .subbuf_start = blk_subbuf_start_callback,
390 .create_buf_file = blk_create_buf_file_callback,
391 .remove_buf_file = blk_remove_buf_file_callback,
392};
393
394/*
395 * Setup everything required to start tracing
396 */
6da127ad 397int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
171044d4 398 struct blk_user_trace_setup *buts)
2056a782 399{
2056a782
JA
400 struct blk_trace *old_bt, *bt = NULL;
401 struct dentry *dir = NULL;
2056a782
JA
402 int ret, i;
403
171044d4 404 if (!buts->buf_size || !buts->buf_nr)
2056a782
JA
405 return -EINVAL;
406
0497b345
JA
407 strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
408 buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
2056a782
JA
409
410 /*
411 * some device names have larger paths - convert the slashes
412 * to underscores for this to work as expected
413 */
171044d4
AB
414 for (i = 0; i < strlen(buts->name); i++)
415 if (buts->name[i] == '/')
416 buts->name[i] = '_';
2056a782
JA
417
418 ret = -ENOMEM;
419 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
420 if (!bt)
421 goto err;
422
423 bt->sequence = alloc_percpu(unsigned long);
424 if (!bt->sequence)
425 goto err;
426
64565911
JA
427 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG);
428 if (!bt->msg_data)
429 goto err;
430
2056a782 431 ret = -ENOENT;
f48fc4d3
JA
432
433 if (!blk_tree_root) {
434 blk_tree_root = debugfs_create_dir("block", NULL);
435 if (!blk_tree_root)
436 return -ENOMEM;
437 }
438
439 dir = debugfs_create_dir(buts->name, blk_tree_root);
440
2056a782
JA
441 if (!dir)
442 goto err;
443
444 bt->dir = dir;
6da127ad 445 bt->dev = dev;
2056a782
JA
446 atomic_set(&bt->dropped, 0);
447
448 ret = -EIO;
449 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt, &blk_dropped_fops);
450 if (!bt->dropped_file)
451 goto err;
452
02c62304
AB
453 bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
454 if (!bt->msg_file)
455 goto err;
456
171044d4
AB
457 bt->rchan = relay_open("trace", dir, buts->buf_size,
458 buts->buf_nr, &blk_relay_callbacks, bt);
2056a782
JA
459 if (!bt->rchan)
460 goto err;
2056a782 461
171044d4 462 bt->act_mask = buts->act_mask;
2056a782
JA
463 if (!bt->act_mask)
464 bt->act_mask = (u16) -1;
465
171044d4
AB
466 bt->start_lba = buts->start_lba;
467 bt->end_lba = buts->end_lba;
2056a782
JA
468 if (!bt->end_lba)
469 bt->end_lba = -1ULL;
470
171044d4 471 bt->pid = buts->pid;
2056a782
JA
472 bt->trace_state = Blktrace_setup;
473
5f3ea37c
ACM
474 mutex_lock(&blk_probe_mutex);
475 if (atomic_add_return(1, &blk_probes_ref) == 1) {
476 ret = blk_register_tracepoints();
477 if (ret)
478 goto probe_err;
479 }
480 mutex_unlock(&blk_probe_mutex);
481
2056a782
JA
482 ret = -EBUSY;
483 old_bt = xchg(&q->blk_trace, bt);
484 if (old_bt) {
485 (void) xchg(&q->blk_trace, old_bt);
486 goto err;
487 }
488
489 return 0;
5f3ea37c
ACM
490probe_err:
491 atomic_dec(&blk_probes_ref);
492 mutex_unlock(&blk_probe_mutex);
2056a782 493err:
2056a782 494 if (bt) {
02c62304
AB
495 if (bt->msg_file)
496 debugfs_remove(bt->msg_file);
2056a782
JA
497 if (bt->dropped_file)
498 debugfs_remove(bt->dropped_file);
a1205868 499 free_percpu(bt->sequence);
64565911 500 free_percpu(bt->msg_data);
2056a782
JA
501 if (bt->rchan)
502 relay_close(bt->rchan);
503 kfree(bt);
504 }
505 return ret;
506}
171044d4 507
6da127ad
CS
508int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
509 char __user *arg)
171044d4
AB
510{
511 struct blk_user_trace_setup buts;
512 int ret;
513
514 ret = copy_from_user(&buts, arg, sizeof(buts));
515 if (ret)
516 return -EFAULT;
517
6da127ad 518 ret = do_blk_trace_setup(q, name, dev, &buts);
171044d4
AB
519 if (ret)
520 return ret;
521
522 if (copy_to_user(arg, &buts, sizeof(buts)))
523 return -EFAULT;
524
525 return 0;
526}
6da127ad 527EXPORT_SYMBOL_GPL(blk_trace_setup);
2056a782 528
6da127ad 529int blk_trace_startstop(struct request_queue *q, int start)
2056a782
JA
530{
531 struct blk_trace *bt;
532 int ret;
533
534 if ((bt = q->blk_trace) == NULL)
535 return -EINVAL;
536
537 /*
538 * For starting a trace, we can transition from a setup or stopped
539 * trace. For stopping a trace, the state must be running
540 */
541 ret = -EINVAL;
542 if (start) {
543 if (bt->trace_state == Blktrace_setup ||
544 bt->trace_state == Blktrace_stopped) {
545 blktrace_seq++;
546 smp_mb();
547 bt->trace_state = Blktrace_running;
be1c6341
OK
548
549 trace_note_time(bt);
2056a782
JA
550 ret = 0;
551 }
552 } else {
553 if (bt->trace_state == Blktrace_running) {
554 bt->trace_state = Blktrace_stopped;
555 relay_flush(bt->rchan);
556 ret = 0;
557 }
558 }
559
560 return ret;
561}
6da127ad 562EXPORT_SYMBOL_GPL(blk_trace_startstop);
2056a782
JA
563
564/**
565 * blk_trace_ioctl: - handle the ioctls associated with tracing
566 * @bdev: the block device
567 * @cmd: the ioctl cmd
568 * @arg: the argument data, if any
569 *
570 **/
571int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
572{
165125e1 573 struct request_queue *q;
2056a782 574 int ret, start = 0;
6da127ad 575 char b[BDEVNAME_SIZE];
2056a782
JA
576
577 q = bdev_get_queue(bdev);
578 if (!q)
579 return -ENXIO;
580
581 mutex_lock(&bdev->bd_mutex);
582
583 switch (cmd) {
584 case BLKTRACESETUP:
f36f21ec 585 bdevname(bdev, b);
6da127ad 586 ret = blk_trace_setup(q, b, bdev->bd_dev, arg);
2056a782
JA
587 break;
588 case BLKTRACESTART:
589 start = 1;
590 case BLKTRACESTOP:
591 ret = blk_trace_startstop(q, start);
592 break;
593 case BLKTRACETEARDOWN:
594 ret = blk_trace_remove(q);
595 break;
596 default:
597 ret = -ENOTTY;
598 break;
599 }
600
601 mutex_unlock(&bdev->bd_mutex);
602 return ret;
603}
604
605/**
606 * blk_trace_shutdown: - stop and cleanup trace structures
607 * @q: the request queue associated with the device
608 *
609 **/
165125e1 610void blk_trace_shutdown(struct request_queue *q)
2056a782 611{
6c5c9341
AD
612 if (q->blk_trace) {
613 blk_trace_startstop(q, 0);
614 blk_trace_remove(q);
615 }
2056a782 616}
5f3ea37c
ACM
617
618/*
619 * blktrace probes
620 */
621
622/**
623 * blk_add_trace_rq - Add a trace for a request oriented action
624 * @q: queue the io is for
625 * @rq: the source request
626 * @what: the action
627 *
628 * Description:
629 * Records an action against a request. Will log the bio offset + size.
630 *
631 **/
632static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
633 u32 what)
634{
635 struct blk_trace *bt = q->blk_trace;
636 int rw = rq->cmd_flags & 0x03;
637
638 if (likely(!bt))
639 return;
640
641 if (blk_discard_rq(rq))
642 rw |= (1 << BIO_RW_DISCARD);
643
644 if (blk_pc_request(rq)) {
645 what |= BLK_TC_ACT(BLK_TC_PC);
646 __blk_add_trace(bt, 0, rq->data_len, rw, what, rq->errors,
647 sizeof(rq->cmd), rq->cmd);
648 } else {
649 what |= BLK_TC_ACT(BLK_TC_FS);
650 __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9,
651 rw, what, rq->errors, 0, NULL);
652 }
653}
654
655static void blk_add_trace_rq_abort(struct request_queue *q, struct request *rq)
656{
657 blk_add_trace_rq(q, rq, BLK_TA_ABORT);
658}
659
660static void blk_add_trace_rq_insert(struct request_queue *q, struct request *rq)
661{
662 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
663}
664
665static void blk_add_trace_rq_issue(struct request_queue *q, struct request *rq)
666{
667 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
668}
669
670static void blk_add_trace_rq_requeue(struct request_queue *q, struct request *rq)
671{
672 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
673}
674
675static void blk_add_trace_rq_complete(struct request_queue *q, struct request *rq)
676{
677 blk_add_trace_rq(q, rq, BLK_TA_COMPLETE);
678}
679
680/**
681 * blk_add_trace_bio - Add a trace for a bio oriented action
682 * @q: queue the io is for
683 * @bio: the source bio
684 * @what: the action
685 *
686 * Description:
687 * Records an action against a bio. Will log the bio offset + size.
688 *
689 **/
690static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
691 u32 what)
692{
693 struct blk_trace *bt = q->blk_trace;
694
695 if (likely(!bt))
696 return;
697
698 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what,
699 !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
700}
701
702static void blk_add_trace_bio_bounce(struct request_queue *q, struct bio *bio)
703{
704 blk_add_trace_bio(q, bio, BLK_TA_BOUNCE);
705}
706
707static void blk_add_trace_bio_complete(struct request_queue *q, struct bio *bio)
708{
709 blk_add_trace_bio(q, bio, BLK_TA_COMPLETE);
710}
711
712static void blk_add_trace_bio_backmerge(struct request_queue *q, struct bio *bio)
713{
714 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
715}
716
717static void blk_add_trace_bio_frontmerge(struct request_queue *q, struct bio *bio)
718{
719 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
720}
721
722static void blk_add_trace_bio_queue(struct request_queue *q, struct bio *bio)
723{
724 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
725}
726
727static void blk_add_trace_getrq(struct request_queue *q, struct bio *bio, int rw)
728{
729 if (bio)
730 blk_add_trace_bio(q, bio, BLK_TA_GETRQ);
731 else {
732 struct blk_trace *bt = q->blk_trace;
733
734 if (bt)
735 __blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL);
736 }
737}
738
739
740static void blk_add_trace_sleeprq(struct request_queue *q, struct bio *bio, int rw)
741{
742 if (bio)
743 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ);
744 else {
745 struct blk_trace *bt = q->blk_trace;
746
747 if (bt)
748 __blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ, 0, 0, NULL);
749 }
750}
751
752static void blk_add_trace_plug(struct request_queue *q)
753{
754 struct blk_trace *bt = q->blk_trace;
755
756 if (bt)
757 __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
758}
759
760static void blk_add_trace_unplug_io(struct request_queue *q)
761{
762 struct blk_trace *bt = q->blk_trace;
763
764 if (bt) {
765 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
766 __be64 rpdu = cpu_to_be64(pdu);
767
768 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_IO, 0,
769 sizeof(rpdu), &rpdu);
770 }
771}
772
773static void blk_add_trace_unplug_timer(struct request_queue *q)
774{
775 struct blk_trace *bt = q->blk_trace;
776
777 if (bt) {
778 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
779 __be64 rpdu = cpu_to_be64(pdu);
780
781 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_TIMER, 0,
782 sizeof(rpdu), &rpdu);
783 }
784}
785
786static void blk_add_trace_split(struct request_queue *q, struct bio *bio,
787 unsigned int pdu)
788{
789 struct blk_trace *bt = q->blk_trace;
790
791 if (bt) {
792 __be64 rpdu = cpu_to_be64(pdu);
793
794 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
795 BLK_TA_SPLIT, !bio_flagged(bio, BIO_UPTODATE),
796 sizeof(rpdu), &rpdu);
797 }
798}
799
800/**
801 * blk_add_trace_remap - Add a trace for a remap operation
802 * @q: queue the io is for
803 * @bio: the source bio
804 * @dev: target device
805 * @from: source sector
806 * @to: target sector
807 *
808 * Description:
809 * Device mapper or raid target sometimes need to split a bio because
810 * it spans a stripe (or similar). Add a trace for that action.
811 *
812 **/
813static void blk_add_trace_remap(struct request_queue *q, struct bio *bio,
814 dev_t dev, sector_t from, sector_t to)
815{
816 struct blk_trace *bt = q->blk_trace;
817 struct blk_io_trace_remap r;
818
819 if (likely(!bt))
820 return;
821
822 r.device = cpu_to_be32(dev);
823 r.device_from = cpu_to_be32(bio->bi_bdev->bd_dev);
824 r.sector = cpu_to_be64(to);
825
826 __blk_add_trace(bt, from, bio->bi_size, bio->bi_rw, BLK_TA_REMAP,
827 !bio_flagged(bio, BIO_UPTODATE), sizeof(r), &r);
828}
829
830/**
831 * blk_add_driver_data - Add binary message with driver-specific data
832 * @q: queue the io is for
833 * @rq: io request
834 * @data: driver-specific data
835 * @len: length of driver-specific data
836 *
837 * Description:
838 * Some drivers might want to write driver-specific data per request.
839 *
840 **/
841void blk_add_driver_data(struct request_queue *q,
842 struct request *rq,
843 void *data, size_t len)
844{
845 struct blk_trace *bt = q->blk_trace;
846
847 if (likely(!bt))
848 return;
849
850 if (blk_pc_request(rq))
851 __blk_add_trace(bt, 0, rq->data_len, 0, BLK_TA_DRV_DATA,
852 rq->errors, len, data);
853 else
854 __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9,
855 0, BLK_TA_DRV_DATA, rq->errors, len, data);
856}
857EXPORT_SYMBOL_GPL(blk_add_driver_data);
858
859static int blk_register_tracepoints(void)
860{
861 int ret;
862
863 ret = register_trace_block_rq_abort(blk_add_trace_rq_abort);
864 WARN_ON(ret);
865 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert);
866 WARN_ON(ret);
867 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue);
868 WARN_ON(ret);
869 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue);
870 WARN_ON(ret);
871 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete);
872 WARN_ON(ret);
873 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce);
874 WARN_ON(ret);
875 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete);
876 WARN_ON(ret);
877 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
878 WARN_ON(ret);
879 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
880 WARN_ON(ret);
881 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue);
882 WARN_ON(ret);
883 ret = register_trace_block_getrq(blk_add_trace_getrq);
884 WARN_ON(ret);
885 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq);
886 WARN_ON(ret);
887 ret = register_trace_block_plug(blk_add_trace_plug);
888 WARN_ON(ret);
889 ret = register_trace_block_unplug_timer(blk_add_trace_unplug_timer);
890 WARN_ON(ret);
891 ret = register_trace_block_unplug_io(blk_add_trace_unplug_io);
892 WARN_ON(ret);
893 ret = register_trace_block_split(blk_add_trace_split);
894 WARN_ON(ret);
895 ret = register_trace_block_remap(blk_add_trace_remap);
896 WARN_ON(ret);
897 return 0;
898}
899
900static void blk_unregister_tracepoints(void)
901{
902 unregister_trace_block_remap(blk_add_trace_remap);
903 unregister_trace_block_split(blk_add_trace_split);
904 unregister_trace_block_unplug_io(blk_add_trace_unplug_io);
905 unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer);
906 unregister_trace_block_plug(blk_add_trace_plug);
907 unregister_trace_block_sleeprq(blk_add_trace_sleeprq);
908 unregister_trace_block_getrq(blk_add_trace_getrq);
909 unregister_trace_block_bio_queue(blk_add_trace_bio_queue);
910 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
911 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
912 unregister_trace_block_bio_complete(blk_add_trace_bio_complete);
913 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce);
914 unregister_trace_block_rq_complete(blk_add_trace_rq_complete);
915 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue);
916 unregister_trace_block_rq_issue(blk_add_trace_rq_issue);
917 unregister_trace_block_rq_insert(blk_add_trace_rq_insert);
918 unregister_trace_block_rq_abort(blk_add_trace_rq_abort);
919
920 tracepoint_synchronize_unregister();
921}
c71a8961
ACM
922
923/*
924 * struct blk_io_tracer formatting routines
925 */
926
927static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
928{
157f9c00
ACM
929 int i = 0;
930
931 if (t->action & BLK_TC_DISCARD)
932 rwbs[i++] = 'D';
933 else if (t->action & BLK_TC_WRITE)
934 rwbs[i++] = 'W';
935 else if (t->bytes)
936 rwbs[i++] = 'R';
937 else
938 rwbs[i++] = 'N';
939
940 if (t->action & BLK_TC_AHEAD)
941 rwbs[i++] = 'A';
942 if (t->action & BLK_TC_BARRIER)
943 rwbs[i++] = 'B';
944 if (t->action & BLK_TC_SYNC)
945 rwbs[i++] = 'S';
946 if (t->action & BLK_TC_META)
947 rwbs[i++] = 'M';
948
949 rwbs[i] = '\0';
c71a8961
ACM
950}
951
952static inline
953const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
954{
955 return (const struct blk_io_trace *)ent;
956}
957
958static inline const void *pdu_start(const struct trace_entry *ent)
959{
960 return te_blk_io_trace(ent) + 1;
961}
962
963static inline u32 t_sec(const struct trace_entry *ent)
964{
965 return te_blk_io_trace(ent)->bytes >> 9;
966}
967
968static inline unsigned long long t_sector(const struct trace_entry *ent)
969{
970 return te_blk_io_trace(ent)->sector;
971}
972
973static inline __u16 t_error(const struct trace_entry *ent)
974{
975 return te_blk_io_trace(ent)->sector;
976}
977
978static __u64 get_pdu_int(const struct trace_entry *ent)
979{
980 const __u64 *val = pdu_start(ent);
981 return be64_to_cpu(*val);
982}
983
984static void get_pdu_remap(const struct trace_entry *ent,
985 struct blk_io_trace_remap *r)
986{
987 const struct blk_io_trace_remap *__r = pdu_start(ent);
988 __u64 sector = __r->sector;
989
990 r->device = be32_to_cpu(__r->device);
991 r->device_from = be32_to_cpu(__r->device_from);
992 r->sector = be64_to_cpu(sector);
993}
994
995static int blk_log_action_iter(struct trace_iterator *iter, const char *act)
996{
997 char rwbs[6];
998 unsigned long long ts = ns2usecs(iter->ts);
999 unsigned long usec_rem = do_div(ts, USEC_PER_SEC);
1000 unsigned secs = (unsigned long)ts;
1001 const struct trace_entry *ent = iter->ent;
1002 const struct blk_io_trace *t = (const struct blk_io_trace *)ent;
1003
1004 fill_rwbs(rwbs, t);
1005
1006 return trace_seq_printf(&iter->seq,
1007 "%3d,%-3d %2d %5d.%06lu %5u %2s %3s ",
1008 MAJOR(t->device), MINOR(t->device), iter->cpu,
1009 secs, usec_rem, ent->pid, act, rwbs);
1010}
1011
1012static int blk_log_action_seq(struct trace_seq *s, const struct blk_io_trace *t,
1013 const char *act)
1014{
1015 char rwbs[6];
1016 fill_rwbs(rwbs, t);
1017 return trace_seq_printf(s, "%3d,%-3d %2s %3s ",
1018 MAJOR(t->device), MINOR(t->device), act, rwbs);
1019}
1020
1021static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1022{
1023 const char *cmd = trace_find_cmdline(ent->pid);
1024
1025 if (t_sec(ent))
1026 return trace_seq_printf(s, "%llu + %u [%s]\n",
1027 t_sector(ent), t_sec(ent), cmd);
1028 return trace_seq_printf(s, "[%s]\n", cmd);
1029}
1030
157f9c00
ACM
1031static int blk_log_with_error(struct trace_seq *s,
1032 const struct trace_entry *ent)
c71a8961
ACM
1033{
1034 if (t_sec(ent))
1035 return trace_seq_printf(s, "%llu + %u [%d]\n", t_sector(ent),
1036 t_sec(ent), t_error(ent));
1037 return trace_seq_printf(s, "%llu [%d]\n", t_sector(ent), t_error(ent));
1038}
1039
1040static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1041{
1042 struct blk_io_trace_remap r = { .device = 0, };
1043
1044 get_pdu_remap(ent, &r);
1045 return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
1046 t_sector(ent),
1047 t_sec(ent), MAJOR(r.device), MINOR(r.device),
1048 (unsigned long long)r.sector);
1049}
1050
1051static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1052{
1053 return trace_seq_printf(s, "[%s]\n", trace_find_cmdline(ent->pid));
1054}
1055
1056static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1057{
1058 return trace_seq_printf(s, "[%s] %llu\n", trace_find_cmdline(ent->pid),
1059 get_pdu_int(ent));
1060}
1061
1062static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1063{
1064 return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
1065 get_pdu_int(ent), trace_find_cmdline(ent->pid));
1066}
1067
1068/*
1069 * struct tracer operations
1070 */
1071
1072static void blk_tracer_print_header(struct seq_file *m)
1073{
1074 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1075 return;
1076 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1077 "# | | | | | |\n");
1078}
1079
1080static void blk_tracer_start(struct trace_array *tr)
1081{
700a3dcb 1082 tracing_reset_online_cpus(tr);
c71a8961
ACM
1083
1084 mutex_lock(&blk_probe_mutex);
1085 if (atomic_add_return(1, &blk_probes_ref) == 1)
1086 if (blk_register_tracepoints())
1087 atomic_dec(&blk_probes_ref);
1088 mutex_unlock(&blk_probe_mutex);
1089}
1090
1091static int blk_tracer_init(struct trace_array *tr)
1092{
1093 blk_tr = tr;
1094 blk_tracer_start(tr);
1095 mutex_lock(&blk_probe_mutex);
1096 blk_tracer_enabled++;
1097 mutex_unlock(&blk_probe_mutex);
1098 return 0;
1099}
1100
1101static void blk_tracer_stop(struct trace_array *tr)
1102{
1103 mutex_lock(&blk_probe_mutex);
1104 if (atomic_dec_and_test(&blk_probes_ref))
1105 blk_unregister_tracepoints();
1106 mutex_unlock(&blk_probe_mutex);
1107}
1108
1109static void blk_tracer_reset(struct trace_array *tr)
1110{
1111 if (!atomic_read(&blk_probes_ref))
1112 return;
1113
1114 mutex_lock(&blk_probe_mutex);
1115 blk_tracer_enabled--;
1116 WARN_ON(blk_tracer_enabled < 0);
1117 mutex_unlock(&blk_probe_mutex);
1118
1119 blk_tracer_stop(tr);
1120}
1121
1122static struct {
1123 const char *act[2];
1124 int (*print)(struct trace_seq *s, const struct trace_entry *ent);
1125} what2act[] __read_mostly = {
1126 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
1127 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1128 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1129 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1130 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1131 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1132 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1133 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1134 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1135 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
1136 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
1137 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1138 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1139 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1140 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1141};
1142
1143static int blk_trace_event_print(struct trace_seq *s, struct trace_entry *ent,
1144 int flags)
1145{
1146 const struct blk_io_trace *t = (struct blk_io_trace *)ent;
1147 const u16 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1148 int ret;
1149
1150 if (unlikely(what == 0 || what > ARRAY_SIZE(what2act)))
1151 ret = trace_seq_printf(s, "Bad pc action %x\n", what);
1152 else {
1153 const bool long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1154 ret = blk_log_action_seq(s, t, what2act[what].act[long_act]);
1155 if (ret)
1156 ret = what2act[what].print(s, ent);
1157 }
1158
1159 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1160}
1161
1162static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1163{
1164 const struct blk_io_trace *t;
1165 u16 what;
1166 int ret;
1167
1168 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1169 return TRACE_TYPE_UNHANDLED;
1170
1171 t = (const struct blk_io_trace *)iter->ent;
1172 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1173
1174 if (unlikely(what == 0 || what > ARRAY_SIZE(what2act)))
1175 ret = trace_seq_printf(&iter->seq, "Bad pc action %x\n", what);
1176 else {
1177 const bool long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1178 ret = blk_log_action_iter(iter, what2act[what].act[long_act]);
1179 if (ret)
1180 ret = what2act[what].print(&iter->seq, iter->ent);
1181 }
1182
1183 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1184}
1185
1186static struct tracer blk_tracer __read_mostly = {
1187 .name = "blk",
1188 .init = blk_tracer_init,
1189 .reset = blk_tracer_reset,
1190 .start = blk_tracer_start,
1191 .stop = blk_tracer_stop,
1192 .print_header = blk_tracer_print_header,
1193 .print_line = blk_tracer_print_line,
1194 .flags = &blk_tracer_flags,
1195};
1196
1197static struct trace_event trace_blk_event = {
1198 .type = TRACE_BLK,
1199 .trace = blk_trace_event_print,
1200 .latency_trace = blk_trace_event_print,
1201 .raw = trace_nop_print,
1202 .hex = trace_nop_print,
1203 .binary = trace_nop_print,
1204};
1205
1206static int __init init_blk_tracer(void)
1207{
1208 if (!register_ftrace_event(&trace_blk_event)) {
1209 pr_warning("Warning: could not register block events\n");
1210 return 1;
1211 }
1212
1213 if (register_tracer(&blk_tracer) != 0) {
1214 pr_warning("Warning: could not register the block tracer\n");
1215 unregister_ftrace_event(&trace_blk_event);
1216 return 1;
1217 }
1218
1219 return 0;
1220}
1221
1222device_initcall(init_blk_tracer);
1223
1224static int blk_trace_remove_queue(struct request_queue *q)
1225{
1226 struct blk_trace *bt;
1227
1228 bt = xchg(&q->blk_trace, NULL);
1229 if (bt == NULL)
1230 return -EINVAL;
1231
1232 kfree(bt);
1233 return 0;
1234}
1235
1236/*
1237 * Setup everything required to start tracing
1238 */
1239static int blk_trace_setup_queue(struct request_queue *q, dev_t dev)
1240{
1241 struct blk_trace *old_bt, *bt = NULL;
1242 int ret;
1243
1244 ret = -ENOMEM;
1245 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1246 if (!bt)
1247 goto err;
1248
1249 bt->dev = dev;
1250 bt->act_mask = (u16)-1;
1251 bt->end_lba = -1ULL;
1252 bt->trace_state = Blktrace_running;
1253
1254 old_bt = xchg(&q->blk_trace, bt);
1255 if (old_bt != NULL) {
1256 (void)xchg(&q->blk_trace, old_bt);
1257 kfree(bt);
1258 ret = -EBUSY;
1259 }
1260 return 0;
1261err:
1262 return ret;
1263}
1264
1265/*
1266 * sysfs interface to enable and configure tracing
1267 */
1268
1269static ssize_t sysfs_blk_trace_enable_show(struct device *dev,
1270 struct device_attribute *attr,
1271 char *buf)
1272{
1273 struct hd_struct *p = dev_to_part(dev);
1274 struct block_device *bdev;
1275 ssize_t ret = -ENXIO;
1276
1277 lock_kernel();
1278 bdev = bdget(part_devt(p));
1279 if (bdev != NULL) {
1280 struct request_queue *q = bdev_get_queue(bdev);
1281
1282 if (q != NULL) {
1283 mutex_lock(&bdev->bd_mutex);
1284 ret = sprintf(buf, "%u\n", !!q->blk_trace);
1285 mutex_unlock(&bdev->bd_mutex);
1286 }
1287
1288 bdput(bdev);
1289 }
1290
1291 unlock_kernel();
1292 return ret;
1293}
1294
1295static ssize_t sysfs_blk_trace_enable_store(struct device *dev,
1296 struct device_attribute *attr,
1297 const char *buf, size_t count)
1298{
1299 struct block_device *bdev;
1300 struct request_queue *q;
1301 struct hd_struct *p;
1302 int value;
1303 ssize_t ret = -ENXIO;
1304
1305 if (count == 0 || sscanf(buf, "%d", &value) != 1)
1306 goto out;
1307
1308 lock_kernel();
1309 p = dev_to_part(dev);
1310 bdev = bdget(part_devt(p));
1311 if (bdev == NULL)
1312 goto out_unlock_kernel;
1313
1314 q = bdev_get_queue(bdev);
1315 if (q == NULL)
1316 goto out_bdput;
1317
1318 mutex_lock(&bdev->bd_mutex);
1319 if (value)
1320 ret = blk_trace_setup_queue(q, bdev->bd_dev);
1321 else
1322 ret = blk_trace_remove_queue(q);
1323 mutex_unlock(&bdev->bd_mutex);
1324
1325 if (ret == 0)
1326 ret = count;
1327out_bdput:
1328 bdput(bdev);
1329out_unlock_kernel:
1330 unlock_kernel();
1331out:
1332 return ret;
1333}
1334
1335static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1336 struct device_attribute *attr,
1337 char *buf);
1338static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1339 struct device_attribute *attr,
1340 const char *buf, size_t count);
1341#define BLK_TRACE_DEVICE_ATTR(_name) \
1342 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1343 sysfs_blk_trace_attr_show, \
1344 sysfs_blk_trace_attr_store)
1345
1346static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
1347 sysfs_blk_trace_enable_show, sysfs_blk_trace_enable_store);
1348static BLK_TRACE_DEVICE_ATTR(act_mask);
1349static BLK_TRACE_DEVICE_ATTR(pid);
1350static BLK_TRACE_DEVICE_ATTR(start_lba);
1351static BLK_TRACE_DEVICE_ATTR(end_lba);
1352
1353static struct attribute *blk_trace_attrs[] = {
1354 &dev_attr_enable.attr,
1355 &dev_attr_act_mask.attr,
1356 &dev_attr_pid.attr,
1357 &dev_attr_start_lba.attr,
1358 &dev_attr_end_lba.attr,
1359 NULL
1360};
1361
1362struct attribute_group blk_trace_attr_group = {
1363 .name = "trace",
1364 .attrs = blk_trace_attrs,
1365};
1366
1367static int blk_str2act_mask(const char *str)
1368{
1369 int mask = 0;
1370 char *copy = kstrdup(str, GFP_KERNEL), *s;
1371
1372 if (copy == NULL)
1373 return -ENOMEM;
1374
1375 s = strstrip(copy);
1376
1377 while (1) {
1378 char *sep = strchr(s, ',');
1379
1380 if (sep != NULL)
1381 *sep = '\0';
1382
1383 if (strcasecmp(s, "barrier") == 0)
1384 mask |= BLK_TC_BARRIER;
1385 else if (strcasecmp(s, "complete") == 0)
1386 mask |= BLK_TC_COMPLETE;
1387 else if (strcasecmp(s, "fs") == 0)
1388 mask |= BLK_TC_FS;
1389 else if (strcasecmp(s, "issue") == 0)
1390 mask |= BLK_TC_ISSUE;
1391 else if (strcasecmp(s, "pc") == 0)
1392 mask |= BLK_TC_PC;
1393 else if (strcasecmp(s, "queue") == 0)
1394 mask |= BLK_TC_QUEUE;
1395 else if (strcasecmp(s, "read") == 0)
1396 mask |= BLK_TC_READ;
1397 else if (strcasecmp(s, "requeue") == 0)
1398 mask |= BLK_TC_REQUEUE;
1399 else if (strcasecmp(s, "sync") == 0)
1400 mask |= BLK_TC_SYNC;
1401 else if (strcasecmp(s, "write") == 0)
1402 mask |= BLK_TC_WRITE;
1403
1404 if (sep == NULL)
1405 break;
1406
1407 s = sep + 1;
1408 }
1409 kfree(copy);
1410
1411 return mask;
1412}
1413
1414static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1415 struct device_attribute *attr,
1416 char *buf)
1417{
1418 struct hd_struct *p = dev_to_part(dev);
1419 struct request_queue *q;
1420 struct block_device *bdev;
1421 ssize_t ret = -ENXIO;
1422
1423 lock_kernel();
1424 bdev = bdget(part_devt(p));
1425 if (bdev == NULL)
1426 goto out_unlock_kernel;
1427
1428 q = bdev_get_queue(bdev);
1429 if (q == NULL)
1430 goto out_bdput;
1431 mutex_lock(&bdev->bd_mutex);
1432 if (q->blk_trace == NULL)
1433 ret = sprintf(buf, "disabled\n");
1434 else if (attr == &dev_attr_act_mask)
1435 ret = sprintf(buf, "%#x\n", q->blk_trace->act_mask);
1436 else if (attr == &dev_attr_pid)
1437 ret = sprintf(buf, "%u\n", q->blk_trace->pid);
1438 else if (attr == &dev_attr_start_lba)
1439 ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
1440 else if (attr == &dev_attr_end_lba)
1441 ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
1442 mutex_unlock(&bdev->bd_mutex);
1443out_bdput:
1444 bdput(bdev);
1445out_unlock_kernel:
1446 unlock_kernel();
1447 return ret;
1448}
1449
1450static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1451 struct device_attribute *attr,
1452 const char *buf, size_t count)
1453{
1454 struct block_device *bdev;
1455 struct request_queue *q;
1456 struct hd_struct *p;
1457 u64 value;
1458 ssize_t ret = -ENXIO;
1459
1460 if (count == 0)
1461 goto out;
1462
1463 if (attr == &dev_attr_act_mask) {
1464 if (sscanf(buf, "%llx", &value) != 1) {
1465 /* Assume it is a list of trace category names */
1466 value = blk_str2act_mask(buf);
1467 if (value < 0)
1468 goto out;
1469 }
1470 } else if (sscanf(buf, "%llu", &value) != 1)
1471 goto out;
1472
1473 lock_kernel();
1474 p = dev_to_part(dev);
1475 bdev = bdget(part_devt(p));
1476 if (bdev == NULL)
1477 goto out_unlock_kernel;
1478
1479 q = bdev_get_queue(bdev);
1480 if (q == NULL)
1481 goto out_bdput;
1482
1483 mutex_lock(&bdev->bd_mutex);
1484 ret = 0;
1485 if (q->blk_trace == NULL)
1486 ret = blk_trace_setup_queue(q, bdev->bd_dev);
1487
1488 if (ret == 0) {
1489 if (attr == &dev_attr_act_mask)
1490 q->blk_trace->act_mask = value;
1491 else if (attr == &dev_attr_pid)
1492 q->blk_trace->pid = value;
1493 else if (attr == &dev_attr_start_lba)
1494 q->blk_trace->start_lba = value;
1495 else if (attr == &dev_attr_end_lba)
1496 q->blk_trace->end_lba = value;
1497 ret = count;
1498 }
1499 mutex_unlock(&bdev->bd_mutex);
1500out_bdput:
1501 bdput(bdev);
1502out_unlock_kernel:
1503 unlock_kernel();
1504out:
1505 return ret;
1506}
This page took 0.35426 seconds and 5 git commands to generate.