]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - kernel/trace/blktrace.c
block: unify flags for struct bio and struct request
[mirror_ubuntu-artful-kernel.git] / kernel / trace / 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>
5a0e3ad6 24#include <linux/slab.h>
2056a782 25#include <linux/debugfs.h>
405f5571 26#include <linux/smp_lock.h>
be1c6341 27#include <linux/time.h>
939b3669 28#include <linux/uaccess.h>
55782138
LZ
29
30#include <trace/events/block.h>
31
2db270a8 32#include "trace_output.h"
2056a782 33
55782138
LZ
34#ifdef CONFIG_BLK_DEV_IO_TRACE
35
2056a782
JA
36static unsigned int blktrace_seq __read_mostly = 1;
37
c71a8961 38static struct trace_array *blk_tr;
5006ea73 39static bool blk_tracer_enabled __read_mostly;
c71a8961
ACM
40
41/* Select an alternative, minimalistic output than the original one */
ef18012b 42#define TRACE_BLK_OPT_CLASSIC 0x1
c71a8961
ACM
43
44static struct tracer_opt blk_tracer_opts[] = {
45 /* Default disable the minimalistic output */
157f9c00 46 { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },
c71a8961
ACM
47 { }
48};
49
50static struct tracer_flags blk_tracer_flags = {
51 .val = 0,
52 .opts = blk_tracer_opts,
53};
54
5f3ea37c 55/* Global reference count of probes */
5f3ea37c
ACM
56static atomic_t blk_probes_ref = ATOMIC_INIT(0);
57
3c289ba7 58static void blk_register_tracepoints(void);
5f3ea37c
ACM
59static void blk_unregister_tracepoints(void);
60
be1c6341
OK
61/*
62 * Send out a notify message.
63 */
a863055b
JA
64static void trace_note(struct blk_trace *bt, pid_t pid, int action,
65 const void *data, size_t len)
be1c6341
OK
66{
67 struct blk_io_trace *t;
18cea459 68 struct ring_buffer_event *event = NULL;
e77405ad 69 struct ring_buffer *buffer = NULL;
18cea459
LZ
70 int pc = 0;
71 int cpu = smp_processor_id();
72 bool blk_tracer = blk_tracer_enabled;
73
74 if (blk_tracer) {
e77405ad 75 buffer = blk_tr->buffer;
18cea459 76 pc = preempt_count();
e77405ad 77 event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
18cea459
LZ
78 sizeof(*t) + len,
79 0, pc);
80 if (!event)
81 return;
82 t = ring_buffer_event_data(event);
83 goto record_it;
84 }
be1c6341 85
c71a8961
ACM
86 if (!bt->rchan)
87 return;
88
be1c6341 89 t = relay_reserve(bt->rchan, sizeof(*t) + len);
d3d9d2a5 90 if (t) {
d3d9d2a5 91 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
2997c8c4 92 t->time = ktime_to_ns(ktime_get());
18cea459 93record_it:
d3d9d2a5
JA
94 t->device = bt->dev;
95 t->action = action;
96 t->pid = pid;
97 t->cpu = cpu;
98 t->pdu_len = len;
99 memcpy((void *) t + sizeof(*t), data, len);
18cea459
LZ
100
101 if (blk_tracer)
e77405ad 102 trace_buffer_unlock_commit(buffer, event, 0, pc);
d3d9d2a5 103 }
be1c6341
OK
104}
105
2056a782
JA
106/*
107 * Send out a notify for this process, if we haven't done so since a trace
108 * started
109 */
110static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk)
111{
a863055b
JA
112 tsk->btrace_seq = blktrace_seq;
113 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm));
be1c6341 114}
2056a782 115
be1c6341
OK
116static void trace_note_time(struct blk_trace *bt)
117{
118 struct timespec now;
119 unsigned long flags;
120 u32 words[2];
121
122 getnstimeofday(&now);
123 words[0] = now.tv_sec;
124 words[1] = now.tv_nsec;
125
126 local_irq_save(flags);
127 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
128 local_irq_restore(flags);
2056a782
JA
129}
130
9d5f09a4
AB
131void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
132{
133 int n;
134 va_list args;
14a73f54 135 unsigned long flags;
64565911 136 char *buf;
9d5f09a4 137
18cea459
LZ
138 if (unlikely(bt->trace_state != Blktrace_running &&
139 !blk_tracer_enabled))
c71a8961
ACM
140 return;
141
14a73f54 142 local_irq_save(flags);
64565911 143 buf = per_cpu_ptr(bt->msg_data, smp_processor_id());
9d5f09a4 144 va_start(args, fmt);
64565911 145 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
9d5f09a4
AB
146 va_end(args);
147
64565911 148 trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
14a73f54 149 local_irq_restore(flags);
9d5f09a4
AB
150}
151EXPORT_SYMBOL_GPL(__trace_note_message);
152
2056a782
JA
153static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
154 pid_t pid)
155{
156 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
157 return 1;
d0deef5b 158 if (sector && (sector < bt->start_lba || sector > bt->end_lba))
2056a782
JA
159 return 1;
160 if (bt->pid && pid != bt->pid)
161 return 1;
162
163 return 0;
164}
165
166/*
167 * Data direction bit lookup
168 */
e4955c99
LZ
169static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ),
170 BLK_TC_ACT(BLK_TC_WRITE) };
2056a782 171
7b6d91da
CH
172#define BLK_TC_HARDBARRIER BLK_TC_BARRIER
173#define BLK_TC_RAHEAD BLK_TC_AHEAD
174
35ba8f70 175/* The ilog2() calls fall out because they're constant */
7b6d91da
CH
176#define MASK_TC_BIT(rw, __name) ((rw & REQ_ ## __name) << \
177 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - __REQ_ ## __name))
2056a782
JA
178
179/*
180 * The worker for the various blk_add_trace*() types. Fills out a
181 * blk_io_trace structure and places it in a per-cpu subbuffer.
182 */
5f3ea37c 183static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
2056a782
JA
184 int rw, u32 what, int error, int pdu_len, void *pdu_data)
185{
186 struct task_struct *tsk = current;
c71a8961 187 struct ring_buffer_event *event = NULL;
e77405ad 188 struct ring_buffer *buffer = NULL;
2056a782 189 struct blk_io_trace *t;
0a987751 190 unsigned long flags = 0;
2056a782
JA
191 unsigned long *sequence;
192 pid_t pid;
c71a8961 193 int cpu, pc = 0;
18cea459 194 bool blk_tracer = blk_tracer_enabled;
2056a782 195
18cea459 196 if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer))
2056a782
JA
197 return;
198
199 what |= ddir_act[rw & WRITE];
7b6d91da
CH
200 what |= MASK_TC_BIT(rw, HARDBARRIER);
201 what |= MASK_TC_BIT(rw, SYNC);
202 what |= MASK_TC_BIT(rw, RAHEAD);
35ba8f70
DW
203 what |= MASK_TC_BIT(rw, META);
204 what |= MASK_TC_BIT(rw, DISCARD);
2056a782
JA
205
206 pid = tsk->pid;
d0deef5b 207 if (act_log_check(bt, what, sector, pid))
2056a782 208 return;
c71a8961
ACM
209 cpu = raw_smp_processor_id();
210
18cea459 211 if (blk_tracer) {
c71a8961
ACM
212 tracing_record_cmdline(current);
213
e77405ad 214 buffer = blk_tr->buffer;
51a763dd 215 pc = preempt_count();
e77405ad 216 event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
51a763dd
ACM
217 sizeof(*t) + pdu_len,
218 0, pc);
c71a8961
ACM
219 if (!event)
220 return;
51a763dd 221 t = ring_buffer_event_data(event);
c71a8961
ACM
222 goto record_it;
223 }
2056a782
JA
224
225 /*
226 * A word about the locking here - we disable interrupts to reserve
227 * some space in the relay per-cpu buffer, to prevent an irq
14a73f54 228 * from coming in and stepping on our toes.
2056a782
JA
229 */
230 local_irq_save(flags);
231
232 if (unlikely(tsk->btrace_seq != blktrace_seq))
233 trace_note_tsk(bt, tsk);
234
235 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
236 if (t) {
2056a782
JA
237 sequence = per_cpu_ptr(bt->sequence, cpu);
238
239 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
240 t->sequence = ++(*sequence);
2997c8c4 241 t->time = ktime_to_ns(ktime_get());
c71a8961 242record_it:
08a06b83 243 /*
939b3669
ACM
244 * These two are not needed in ftrace as they are in the
245 * generic trace_entry, filled by tracing_generic_entry_update,
246 * but for the trace_event->bin() synthesizer benefit we do it
247 * here too.
248 */
249 t->cpu = cpu;
250 t->pid = pid;
08a06b83 251
2056a782
JA
252 t->sector = sector;
253 t->bytes = bytes;
254 t->action = what;
2056a782 255 t->device = bt->dev;
2056a782
JA
256 t->error = error;
257 t->pdu_len = pdu_len;
258
259 if (pdu_len)
260 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
c71a8961 261
18cea459 262 if (blk_tracer) {
e77405ad 263 trace_buffer_unlock_commit(buffer, event, 0, pc);
c71a8961
ACM
264 return;
265 }
2056a782
JA
266 }
267
268 local_irq_restore(flags);
269}
270
2056a782 271static struct dentry *blk_tree_root;
11a57153 272static DEFINE_MUTEX(blk_tree_mutex);
2056a782 273
ad5dd549 274static void blk_trace_free(struct blk_trace *bt)
2056a782 275{
02c62304 276 debugfs_remove(bt->msg_file);
2056a782 277 debugfs_remove(bt->dropped_file);
f48fc4d3 278 relay_close(bt->rchan);
39cbb602 279 debugfs_remove(bt->dir);
2056a782 280 free_percpu(bt->sequence);
64565911 281 free_percpu(bt->msg_data);
2056a782 282 kfree(bt);
ad5dd549
LZ
283}
284
285static void blk_trace_cleanup(struct blk_trace *bt)
286{
287 blk_trace_free(bt);
5f3ea37c
ACM
288 if (atomic_dec_and_test(&blk_probes_ref))
289 blk_unregister_tracepoints();
2056a782
JA
290}
291
6da127ad 292int blk_trace_remove(struct request_queue *q)
2056a782
JA
293{
294 struct blk_trace *bt;
295
296 bt = xchg(&q->blk_trace, NULL);
297 if (!bt)
298 return -EINVAL;
299
55547204 300 if (bt->trace_state != Blktrace_running)
2056a782
JA
301 blk_trace_cleanup(bt);
302
303 return 0;
304}
6da127ad 305EXPORT_SYMBOL_GPL(blk_trace_remove);
2056a782
JA
306
307static int blk_dropped_open(struct inode *inode, struct file *filp)
308{
8e18e294 309 filp->private_data = inode->i_private;
2056a782
JA
310
311 return 0;
312}
313
314static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
315 size_t count, loff_t *ppos)
316{
317 struct blk_trace *bt = filp->private_data;
318 char buf[16];
319
320 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
321
322 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
323}
324
2b8693c0 325static const struct file_operations blk_dropped_fops = {
2056a782
JA
326 .owner = THIS_MODULE,
327 .open = blk_dropped_open,
328 .read = blk_dropped_read,
329};
330
02c62304
AB
331static int blk_msg_open(struct inode *inode, struct file *filp)
332{
333 filp->private_data = inode->i_private;
334
335 return 0;
336}
337
338static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
339 size_t count, loff_t *ppos)
340{
341 char *msg;
342 struct blk_trace *bt;
343
7635b03a 344 if (count >= BLK_TN_MAX_MSG)
02c62304
AB
345 return -EINVAL;
346
a4b3ada8 347 msg = kmalloc(count + 1, GFP_KERNEL);
02c62304
AB
348 if (msg == NULL)
349 return -ENOMEM;
350
351 if (copy_from_user(msg, buffer, count)) {
352 kfree(msg);
353 return -EFAULT;
354 }
355
a4b3ada8 356 msg[count] = '\0';
02c62304
AB
357 bt = filp->private_data;
358 __trace_note_message(bt, "%s", msg);
359 kfree(msg);
360
361 return count;
362}
363
364static const struct file_operations blk_msg_fops = {
365 .owner = THIS_MODULE,
366 .open = blk_msg_open,
367 .write = blk_msg_write,
368};
369
2056a782
JA
370/*
371 * Keep track of how many times we encountered a full subbuffer, to aid
372 * the user space app in telling how many lost events there were.
373 */
374static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
375 void *prev_subbuf, size_t prev_padding)
376{
377 struct blk_trace *bt;
378
379 if (!relay_buf_full(buf))
380 return 1;
381
382 bt = buf->chan->private_data;
383 atomic_inc(&bt->dropped);
384 return 0;
385}
386
387static int blk_remove_buf_file_callback(struct dentry *dentry)
388{
389 debugfs_remove(dentry);
f48fc4d3 390
2056a782
JA
391 return 0;
392}
393
394static struct dentry *blk_create_buf_file_callback(const char *filename,
395 struct dentry *parent,
396 int mode,
397 struct rchan_buf *buf,
398 int *is_global)
399{
400 return debugfs_create_file(filename, mode, parent, buf,
401 &relay_file_operations);
402}
403
404static struct rchan_callbacks blk_relay_callbacks = {
405 .subbuf_start = blk_subbuf_start_callback,
406 .create_buf_file = blk_create_buf_file_callback,
407 .remove_buf_file = blk_remove_buf_file_callback,
408};
409
9908c309
LZ
410static void blk_trace_setup_lba(struct blk_trace *bt,
411 struct block_device *bdev)
412{
413 struct hd_struct *part = NULL;
414
415 if (bdev)
416 part = bdev->bd_part;
417
418 if (part) {
419 bt->start_lba = part->start_sect;
420 bt->end_lba = part->start_sect + part->nr_sects;
421 } else {
422 bt->start_lba = 0;
423 bt->end_lba = -1ULL;
424 }
425}
426
2056a782
JA
427/*
428 * Setup everything required to start tracing
429 */
6da127ad 430int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
d0deef5b
SD
431 struct block_device *bdev,
432 struct blk_user_trace_setup *buts)
2056a782 433{
2056a782
JA
434 struct blk_trace *old_bt, *bt = NULL;
435 struct dentry *dir = NULL;
2056a782
JA
436 int ret, i;
437
171044d4 438 if (!buts->buf_size || !buts->buf_nr)
2056a782
JA
439 return -EINVAL;
440
0497b345
JA
441 strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
442 buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
2056a782
JA
443
444 /*
445 * some device names have larger paths - convert the slashes
446 * to underscores for this to work as expected
447 */
171044d4
AB
448 for (i = 0; i < strlen(buts->name); i++)
449 if (buts->name[i] == '/')
450 buts->name[i] = '_';
2056a782 451
2056a782
JA
452 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
453 if (!bt)
ad5dd549 454 return -ENOMEM;
2056a782 455
ad5dd549 456 ret = -ENOMEM;
2056a782
JA
457 bt->sequence = alloc_percpu(unsigned long);
458 if (!bt->sequence)
459 goto err;
460
313e458f 461 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
64565911
JA
462 if (!bt->msg_data)
463 goto err;
464
2056a782 465 ret = -ENOENT;
f48fc4d3 466
b5230b56 467 mutex_lock(&blk_tree_mutex);
f48fc4d3
JA
468 if (!blk_tree_root) {
469 blk_tree_root = debugfs_create_dir("block", NULL);
b5230b56
LZ
470 if (!blk_tree_root) {
471 mutex_unlock(&blk_tree_mutex);
1a17662e 472 goto err;
b5230b56 473 }
f48fc4d3 474 }
b5230b56 475 mutex_unlock(&blk_tree_mutex);
f48fc4d3
JA
476
477 dir = debugfs_create_dir(buts->name, blk_tree_root);
478
2056a782
JA
479 if (!dir)
480 goto err;
481
482 bt->dir = dir;
6da127ad 483 bt->dev = dev;
2056a782
JA
484 atomic_set(&bt->dropped, 0);
485
486 ret = -EIO;
939b3669
ACM
487 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt,
488 &blk_dropped_fops);
2056a782
JA
489 if (!bt->dropped_file)
490 goto err;
491
02c62304
AB
492 bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
493 if (!bt->msg_file)
494 goto err;
495
171044d4
AB
496 bt->rchan = relay_open("trace", dir, buts->buf_size,
497 buts->buf_nr, &blk_relay_callbacks, bt);
2056a782
JA
498 if (!bt->rchan)
499 goto err;
2056a782 500
171044d4 501 bt->act_mask = buts->act_mask;
2056a782
JA
502 if (!bt->act_mask)
503 bt->act_mask = (u16) -1;
504
9908c309 505 blk_trace_setup_lba(bt, bdev);
2056a782 506
d0deef5b
SD
507 /* overwrite with user settings */
508 if (buts->start_lba)
509 bt->start_lba = buts->start_lba;
510 if (buts->end_lba)
511 bt->end_lba = buts->end_lba;
512
171044d4 513 bt->pid = buts->pid;
2056a782
JA
514 bt->trace_state = Blktrace_setup;
515
516 ret = -EBUSY;
517 old_bt = xchg(&q->blk_trace, bt);
518 if (old_bt) {
519 (void) xchg(&q->blk_trace, old_bt);
520 goto err;
521 }
522
17ba97e3 523 if (atomic_inc_return(&blk_probes_ref) == 1)
cbe28296
LZ
524 blk_register_tracepoints();
525
2056a782
JA
526 return 0;
527err:
ad5dd549 528 blk_trace_free(bt);
2056a782
JA
529 return ret;
530}
171044d4 531
6da127ad 532int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
d0deef5b 533 struct block_device *bdev,
6da127ad 534 char __user *arg)
171044d4
AB
535{
536 struct blk_user_trace_setup buts;
537 int ret;
538
539 ret = copy_from_user(&buts, arg, sizeof(buts));
540 if (ret)
541 return -EFAULT;
542
d0deef5b 543 ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
171044d4
AB
544 if (ret)
545 return ret;
546
9a8c28c8
DM
547 if (copy_to_user(arg, &buts, sizeof(buts))) {
548 blk_trace_remove(q);
171044d4 549 return -EFAULT;
9a8c28c8 550 }
171044d4
AB
551 return 0;
552}
6da127ad 553EXPORT_SYMBOL_GPL(blk_trace_setup);
2056a782 554
6da127ad 555int blk_trace_startstop(struct request_queue *q, int start)
2056a782 556{
2056a782 557 int ret;
939b3669 558 struct blk_trace *bt = q->blk_trace;
2056a782 559
939b3669 560 if (bt == NULL)
2056a782
JA
561 return -EINVAL;
562
563 /*
564 * For starting a trace, we can transition from a setup or stopped
565 * trace. For stopping a trace, the state must be running
566 */
567 ret = -EINVAL;
568 if (start) {
569 if (bt->trace_state == Blktrace_setup ||
570 bt->trace_state == Blktrace_stopped) {
571 blktrace_seq++;
572 smp_mb();
573 bt->trace_state = Blktrace_running;
be1c6341
OK
574
575 trace_note_time(bt);
2056a782
JA
576 ret = 0;
577 }
578 } else {
579 if (bt->trace_state == Blktrace_running) {
580 bt->trace_state = Blktrace_stopped;
581 relay_flush(bt->rchan);
582 ret = 0;
583 }
584 }
585
586 return ret;
587}
6da127ad 588EXPORT_SYMBOL_GPL(blk_trace_startstop);
2056a782
JA
589
590/**
591 * blk_trace_ioctl: - handle the ioctls associated with tracing
592 * @bdev: the block device
ef18012b 593 * @cmd: the ioctl cmd
2056a782
JA
594 * @arg: the argument data, if any
595 *
596 **/
597int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
598{
165125e1 599 struct request_queue *q;
2056a782 600 int ret, start = 0;
6da127ad 601 char b[BDEVNAME_SIZE];
2056a782
JA
602
603 q = bdev_get_queue(bdev);
604 if (!q)
605 return -ENXIO;
606
607 mutex_lock(&bdev->bd_mutex);
608
609 switch (cmd) {
610 case BLKTRACESETUP:
f36f21ec 611 bdevname(bdev, b);
d0deef5b 612 ret = blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
2056a782
JA
613 break;
614 case BLKTRACESTART:
615 start = 1;
616 case BLKTRACESTOP:
617 ret = blk_trace_startstop(q, start);
618 break;
619 case BLKTRACETEARDOWN:
620 ret = blk_trace_remove(q);
621 break;
622 default:
623 ret = -ENOTTY;
624 break;
625 }
626
627 mutex_unlock(&bdev->bd_mutex);
628 return ret;
629}
630
631/**
632 * blk_trace_shutdown: - stop and cleanup trace structures
633 * @q: the request queue associated with the device
634 *
635 **/
165125e1 636void blk_trace_shutdown(struct request_queue *q)
2056a782 637{
6c5c9341
AD
638 if (q->blk_trace) {
639 blk_trace_startstop(q, 0);
640 blk_trace_remove(q);
641 }
2056a782 642}
5f3ea37c
ACM
643
644/*
645 * blktrace probes
646 */
647
648/**
649 * blk_add_trace_rq - Add a trace for a request oriented action
650 * @q: queue the io is for
651 * @rq: the source request
652 * @what: the action
653 *
654 * Description:
655 * Records an action against a request. Will log the bio offset + size.
656 *
657 **/
658static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
659 u32 what)
660{
661 struct blk_trace *bt = q->blk_trace;
662 int rw = rq->cmd_flags & 0x03;
663
664 if (likely(!bt))
665 return;
666
33659ebb 667 if (rq->cmd_flags & REQ_DISCARD)
7b6d91da 668 rw |= REQ_DISCARD;
5f3ea37c 669
33659ebb 670 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
5f3ea37c 671 what |= BLK_TC_ACT(BLK_TC_PC);
2e46e8b2
TH
672 __blk_add_trace(bt, 0, blk_rq_bytes(rq), rw,
673 what, rq->errors, rq->cmd_len, rq->cmd);
5f3ea37c
ACM
674 } else {
675 what |= BLK_TC_ACT(BLK_TC_FS);
2e46e8b2
TH
676 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), rw,
677 what, rq->errors, 0, NULL);
5f3ea37c
ACM
678 }
679}
680
38516ab5
SR
681static void blk_add_trace_rq_abort(void *ignore,
682 struct request_queue *q, struct request *rq)
5f3ea37c
ACM
683{
684 blk_add_trace_rq(q, rq, BLK_TA_ABORT);
685}
686
38516ab5
SR
687static void blk_add_trace_rq_insert(void *ignore,
688 struct request_queue *q, struct request *rq)
5f3ea37c
ACM
689{
690 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
691}
692
38516ab5
SR
693static void blk_add_trace_rq_issue(void *ignore,
694 struct request_queue *q, struct request *rq)
5f3ea37c
ACM
695{
696 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
697}
698
38516ab5
SR
699static void blk_add_trace_rq_requeue(void *ignore,
700 struct request_queue *q,
939b3669 701 struct request *rq)
5f3ea37c
ACM
702{
703 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
704}
705
38516ab5
SR
706static void blk_add_trace_rq_complete(void *ignore,
707 struct request_queue *q,
939b3669 708 struct request *rq)
5f3ea37c
ACM
709{
710 blk_add_trace_rq(q, rq, BLK_TA_COMPLETE);
711}
712
713/**
714 * blk_add_trace_bio - Add a trace for a bio oriented action
715 * @q: queue the io is for
716 * @bio: the source bio
717 * @what: the action
718 *
719 * Description:
720 * Records an action against a bio. Will log the bio offset + size.
721 *
722 **/
723static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
724 u32 what)
725{
726 struct blk_trace *bt = q->blk_trace;
727
728 if (likely(!bt))
729 return;
730
731 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what,
732 !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
733}
734
38516ab5
SR
735static void blk_add_trace_bio_bounce(void *ignore,
736 struct request_queue *q, struct bio *bio)
5f3ea37c
ACM
737{
738 blk_add_trace_bio(q, bio, BLK_TA_BOUNCE);
739}
740
38516ab5
SR
741static void blk_add_trace_bio_complete(void *ignore,
742 struct request_queue *q, struct bio *bio)
5f3ea37c
ACM
743{
744 blk_add_trace_bio(q, bio, BLK_TA_COMPLETE);
745}
746
38516ab5
SR
747static void blk_add_trace_bio_backmerge(void *ignore,
748 struct request_queue *q,
939b3669 749 struct bio *bio)
5f3ea37c
ACM
750{
751 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
752}
753
38516ab5
SR
754static void blk_add_trace_bio_frontmerge(void *ignore,
755 struct request_queue *q,
939b3669 756 struct bio *bio)
5f3ea37c
ACM
757{
758 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
759}
760
38516ab5
SR
761static void blk_add_trace_bio_queue(void *ignore,
762 struct request_queue *q, struct bio *bio)
5f3ea37c
ACM
763{
764 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
765}
766
38516ab5
SR
767static void blk_add_trace_getrq(void *ignore,
768 struct request_queue *q,
939b3669 769 struct bio *bio, int rw)
5f3ea37c
ACM
770{
771 if (bio)
772 blk_add_trace_bio(q, bio, BLK_TA_GETRQ);
773 else {
774 struct blk_trace *bt = q->blk_trace;
775
776 if (bt)
777 __blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL);
778 }
779}
780
781
38516ab5
SR
782static void blk_add_trace_sleeprq(void *ignore,
783 struct request_queue *q,
939b3669 784 struct bio *bio, int rw)
5f3ea37c
ACM
785{
786 if (bio)
787 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ);
788 else {
789 struct blk_trace *bt = q->blk_trace;
790
791 if (bt)
939b3669
ACM
792 __blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ,
793 0, 0, NULL);
5f3ea37c
ACM
794 }
795}
796
38516ab5 797static void blk_add_trace_plug(void *ignore, struct request_queue *q)
5f3ea37c
ACM
798{
799 struct blk_trace *bt = q->blk_trace;
800
801 if (bt)
802 __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
803}
804
38516ab5 805static void blk_add_trace_unplug_io(void *ignore, struct request_queue *q)
5f3ea37c
ACM
806{
807 struct blk_trace *bt = q->blk_trace;
808
809 if (bt) {
810 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
811 __be64 rpdu = cpu_to_be64(pdu);
812
813 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_IO, 0,
814 sizeof(rpdu), &rpdu);
815 }
816}
817
38516ab5 818static void blk_add_trace_unplug_timer(void *ignore, struct request_queue *q)
5f3ea37c
ACM
819{
820 struct blk_trace *bt = q->blk_trace;
821
822 if (bt) {
823 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
824 __be64 rpdu = cpu_to_be64(pdu);
825
826 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_TIMER, 0,
827 sizeof(rpdu), &rpdu);
828 }
829}
830
38516ab5
SR
831static void blk_add_trace_split(void *ignore,
832 struct request_queue *q, struct bio *bio,
5f3ea37c
ACM
833 unsigned int pdu)
834{
835 struct blk_trace *bt = q->blk_trace;
836
837 if (bt) {
838 __be64 rpdu = cpu_to_be64(pdu);
839
840 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
841 BLK_TA_SPLIT, !bio_flagged(bio, BIO_UPTODATE),
842 sizeof(rpdu), &rpdu);
843 }
844}
845
846/**
847 * blk_add_trace_remap - Add a trace for a remap operation
546cf44a 848 * @ignore: trace callback data parameter (not used)
5f3ea37c
ACM
849 * @q: queue the io is for
850 * @bio: the source bio
851 * @dev: target device
a42aaa3b 852 * @from: source sector
5f3ea37c
ACM
853 *
854 * Description:
855 * Device mapper or raid target sometimes need to split a bio because
856 * it spans a stripe (or similar). Add a trace for that action.
857 *
858 **/
38516ab5
SR
859static void blk_add_trace_remap(void *ignore,
860 struct request_queue *q, struct bio *bio,
861 dev_t dev, sector_t from)
5f3ea37c
ACM
862{
863 struct blk_trace *bt = q->blk_trace;
864 struct blk_io_trace_remap r;
865
866 if (likely(!bt))
867 return;
868
a42aaa3b
AB
869 r.device_from = cpu_to_be32(dev);
870 r.device_to = cpu_to_be32(bio->bi_bdev->bd_dev);
871 r.sector_from = cpu_to_be64(from);
5f3ea37c 872
22a7c31a
AB
873 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
874 BLK_TA_REMAP, !bio_flagged(bio, BIO_UPTODATE),
875 sizeof(r), &r);
5f3ea37c
ACM
876}
877
b0da3f0d
JN
878/**
879 * blk_add_trace_rq_remap - Add a trace for a request-remap operation
546cf44a 880 * @ignore: trace callback data parameter (not used)
b0da3f0d
JN
881 * @q: queue the io is for
882 * @rq: the source request
883 * @dev: target device
884 * @from: source sector
885 *
886 * Description:
887 * Device mapper remaps request to other devices.
888 * Add a trace for that action.
889 *
890 **/
38516ab5
SR
891static void blk_add_trace_rq_remap(void *ignore,
892 struct request_queue *q,
b0da3f0d
JN
893 struct request *rq, dev_t dev,
894 sector_t from)
895{
896 struct blk_trace *bt = q->blk_trace;
897 struct blk_io_trace_remap r;
898
899 if (likely(!bt))
900 return;
901
902 r.device_from = cpu_to_be32(dev);
903 r.device_to = cpu_to_be32(disk_devt(rq->rq_disk));
904 r.sector_from = cpu_to_be64(from);
905
906 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq),
907 rq_data_dir(rq), BLK_TA_REMAP, !!rq->errors,
908 sizeof(r), &r);
909}
910
5f3ea37c
ACM
911/**
912 * blk_add_driver_data - Add binary message with driver-specific data
913 * @q: queue the io is for
914 * @rq: io request
915 * @data: driver-specific data
916 * @len: length of driver-specific data
917 *
918 * Description:
919 * Some drivers might want to write driver-specific data per request.
920 *
921 **/
922void blk_add_driver_data(struct request_queue *q,
923 struct request *rq,
924 void *data, size_t len)
925{
926 struct blk_trace *bt = q->blk_trace;
927
928 if (likely(!bt))
929 return;
930
33659ebb 931 if (rq->cmd_type == REQ_TYPE_BLOCK_PC)
2e46e8b2
TH
932 __blk_add_trace(bt, 0, blk_rq_bytes(rq), 0,
933 BLK_TA_DRV_DATA, rq->errors, len, data);
5f3ea37c 934 else
2e46e8b2
TH
935 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), 0,
936 BLK_TA_DRV_DATA, rq->errors, len, data);
5f3ea37c
ACM
937}
938EXPORT_SYMBOL_GPL(blk_add_driver_data);
939
3c289ba7 940static void blk_register_tracepoints(void)
5f3ea37c
ACM
941{
942 int ret;
943
38516ab5 944 ret = register_trace_block_rq_abort(blk_add_trace_rq_abort, NULL);
5f3ea37c 945 WARN_ON(ret);
38516ab5 946 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
5f3ea37c 947 WARN_ON(ret);
38516ab5 948 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
5f3ea37c 949 WARN_ON(ret);
38516ab5 950 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
5f3ea37c 951 WARN_ON(ret);
38516ab5 952 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
5f3ea37c 953 WARN_ON(ret);
38516ab5 954 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
5f3ea37c 955 WARN_ON(ret);
38516ab5 956 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
5f3ea37c 957 WARN_ON(ret);
38516ab5 958 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
5f3ea37c 959 WARN_ON(ret);
38516ab5 960 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
5f3ea37c 961 WARN_ON(ret);
38516ab5 962 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
5f3ea37c 963 WARN_ON(ret);
38516ab5 964 ret = register_trace_block_getrq(blk_add_trace_getrq, NULL);
5f3ea37c 965 WARN_ON(ret);
38516ab5 966 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
5f3ea37c 967 WARN_ON(ret);
38516ab5 968 ret = register_trace_block_plug(blk_add_trace_plug, NULL);
5f3ea37c 969 WARN_ON(ret);
38516ab5 970 ret = register_trace_block_unplug_timer(blk_add_trace_unplug_timer, NULL);
5f3ea37c 971 WARN_ON(ret);
38516ab5 972 ret = register_trace_block_unplug_io(blk_add_trace_unplug_io, NULL);
5f3ea37c 973 WARN_ON(ret);
38516ab5 974 ret = register_trace_block_split(blk_add_trace_split, NULL);
5f3ea37c 975 WARN_ON(ret);
38516ab5 976 ret = register_trace_block_remap(blk_add_trace_remap, NULL);
5f3ea37c 977 WARN_ON(ret);
38516ab5 978 ret = register_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
b0da3f0d 979 WARN_ON(ret);
5f3ea37c
ACM
980}
981
982static void blk_unregister_tracepoints(void)
983{
38516ab5
SR
984 unregister_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
985 unregister_trace_block_remap(blk_add_trace_remap, NULL);
986 unregister_trace_block_split(blk_add_trace_split, NULL);
987 unregister_trace_block_unplug_io(blk_add_trace_unplug_io, NULL);
988 unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer, NULL);
989 unregister_trace_block_plug(blk_add_trace_plug, NULL);
990 unregister_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
991 unregister_trace_block_getrq(blk_add_trace_getrq, NULL);
992 unregister_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
993 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
994 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
995 unregister_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
996 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
997 unregister_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
998 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
999 unregister_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
1000 unregister_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
1001 unregister_trace_block_rq_abort(blk_add_trace_rq_abort, NULL);
5f3ea37c
ACM
1002
1003 tracepoint_synchronize_unregister();
1004}
c71a8961
ACM
1005
1006/*
1007 * struct blk_io_tracer formatting routines
1008 */
1009
1010static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
1011{
157f9c00 1012 int i = 0;
65796348 1013 int tc = t->action >> BLK_TC_SHIFT;
157f9c00 1014
18cea459
LZ
1015 if (t->action == BLK_TN_MESSAGE) {
1016 rwbs[i++] = 'N';
1017 goto out;
1018 }
1019
65796348 1020 if (tc & BLK_TC_DISCARD)
157f9c00 1021 rwbs[i++] = 'D';
65796348 1022 else if (tc & BLK_TC_WRITE)
157f9c00
ACM
1023 rwbs[i++] = 'W';
1024 else if (t->bytes)
1025 rwbs[i++] = 'R';
1026 else
1027 rwbs[i++] = 'N';
1028
65796348 1029 if (tc & BLK_TC_AHEAD)
157f9c00 1030 rwbs[i++] = 'A';
65796348 1031 if (tc & BLK_TC_BARRIER)
157f9c00 1032 rwbs[i++] = 'B';
65796348 1033 if (tc & BLK_TC_SYNC)
157f9c00 1034 rwbs[i++] = 'S';
65796348 1035 if (tc & BLK_TC_META)
157f9c00 1036 rwbs[i++] = 'M';
18cea459 1037out:
157f9c00 1038 rwbs[i] = '\0';
c71a8961
ACM
1039}
1040
1041static inline
1042const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
1043{
1044 return (const struct blk_io_trace *)ent;
1045}
1046
1047static inline const void *pdu_start(const struct trace_entry *ent)
1048{
1049 return te_blk_io_trace(ent) + 1;
1050}
1051
66de7792
LZ
1052static inline u32 t_action(const struct trace_entry *ent)
1053{
1054 return te_blk_io_trace(ent)->action;
1055}
1056
1057static inline u32 t_bytes(const struct trace_entry *ent)
1058{
1059 return te_blk_io_trace(ent)->bytes;
1060}
1061
c71a8961
ACM
1062static inline u32 t_sec(const struct trace_entry *ent)
1063{
1064 return te_blk_io_trace(ent)->bytes >> 9;
1065}
1066
1067static inline unsigned long long t_sector(const struct trace_entry *ent)
1068{
1069 return te_blk_io_trace(ent)->sector;
1070}
1071
1072static inline __u16 t_error(const struct trace_entry *ent)
1073{
e0dc81be 1074 return te_blk_io_trace(ent)->error;
c71a8961
ACM
1075}
1076
1077static __u64 get_pdu_int(const struct trace_entry *ent)
1078{
1079 const __u64 *val = pdu_start(ent);
1080 return be64_to_cpu(*val);
1081}
1082
1083static void get_pdu_remap(const struct trace_entry *ent,
1084 struct blk_io_trace_remap *r)
1085{
1086 const struct blk_io_trace_remap *__r = pdu_start(ent);
a42aaa3b 1087 __u64 sector_from = __r->sector_from;
c71a8961 1088
c71a8961 1089 r->device_from = be32_to_cpu(__r->device_from);
a42aaa3b
AB
1090 r->device_to = be32_to_cpu(__r->device_to);
1091 r->sector_from = be64_to_cpu(sector_from);
c71a8961
ACM
1092}
1093
b6a4b0c3
LZ
1094typedef int (blk_log_action_t) (struct trace_iterator *iter, const char *act);
1095
1096static int blk_log_action_classic(struct trace_iterator *iter, const char *act)
c71a8961
ACM
1097{
1098 char rwbs[6];
35ac51bf
LZ
1099 unsigned long long ts = iter->ts;
1100 unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
c71a8961 1101 unsigned secs = (unsigned long)ts;
b6a4b0c3 1102 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
c71a8961
ACM
1103
1104 fill_rwbs(rwbs, t);
1105
1106 return trace_seq_printf(&iter->seq,
35ac51bf 1107 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
c71a8961 1108 MAJOR(t->device), MINOR(t->device), iter->cpu,
b6a4b0c3 1109 secs, nsec_rem, iter->ent->pid, act, rwbs);
c71a8961
ACM
1110}
1111
b6a4b0c3 1112static int blk_log_action(struct trace_iterator *iter, const char *act)
c71a8961
ACM
1113{
1114 char rwbs[6];
b6a4b0c3
LZ
1115 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1116
c71a8961 1117 fill_rwbs(rwbs, t);
b6a4b0c3 1118 return trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
c71a8961
ACM
1119 MAJOR(t->device), MINOR(t->device), act, rwbs);
1120}
1121
66de7792
LZ
1122static int blk_log_dump_pdu(struct trace_seq *s, const struct trace_entry *ent)
1123{
04986257 1124 const unsigned char *pdu_buf;
66de7792
LZ
1125 int pdu_len;
1126 int i, end, ret;
1127
1128 pdu_buf = pdu_start(ent);
1129 pdu_len = te_blk_io_trace(ent)->pdu_len;
1130
1131 if (!pdu_len)
1132 return 1;
1133
1134 /* find the last zero that needs to be printed */
1135 for (end = pdu_len - 1; end >= 0; end--)
1136 if (pdu_buf[end])
1137 break;
1138 end++;
1139
1140 if (!trace_seq_putc(s, '('))
1141 return 0;
1142
1143 for (i = 0; i < pdu_len; i++) {
1144
1145 ret = trace_seq_printf(s, "%s%02x",
1146 i == 0 ? "" : " ", pdu_buf[i]);
1147 if (!ret)
1148 return ret;
1149
1150 /*
1151 * stop when the rest is just zeroes and indicate so
1152 * with a ".." appended
1153 */
1154 if (i == end && end != pdu_len - 1)
1155 return trace_seq_puts(s, " ..) ");
1156 }
1157
1158 return trace_seq_puts(s, ") ");
1159}
1160
c71a8961
ACM
1161static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1162{
4ca53085
SR
1163 char cmd[TASK_COMM_LEN];
1164
1165 trace_find_cmdline(ent->pid, cmd);
c71a8961 1166
66de7792
LZ
1167 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1168 int ret;
1169
1170 ret = trace_seq_printf(s, "%u ", t_bytes(ent));
1171 if (!ret)
1172 return 0;
1173 ret = blk_log_dump_pdu(s, ent);
1174 if (!ret)
1175 return 0;
1176 return trace_seq_printf(s, "[%s]\n", cmd);
1177 } else {
1178 if (t_sec(ent))
1179 return trace_seq_printf(s, "%llu + %u [%s]\n",
1180 t_sector(ent), t_sec(ent), cmd);
1181 return trace_seq_printf(s, "[%s]\n", cmd);
1182 }
c71a8961
ACM
1183}
1184
157f9c00
ACM
1185static int blk_log_with_error(struct trace_seq *s,
1186 const struct trace_entry *ent)
c71a8961 1187{
66de7792
LZ
1188 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1189 int ret;
1190
1191 ret = blk_log_dump_pdu(s, ent);
1192 if (ret)
1193 return trace_seq_printf(s, "[%d]\n", t_error(ent));
1194 return 0;
1195 } else {
1196 if (t_sec(ent))
1197 return trace_seq_printf(s, "%llu + %u [%d]\n",
1198 t_sector(ent),
1199 t_sec(ent), t_error(ent));
1200 return trace_seq_printf(s, "%llu [%d]\n",
1201 t_sector(ent), t_error(ent));
1202 }
c71a8961
ACM
1203}
1204
1205static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1206{
a42aaa3b 1207 struct blk_io_trace_remap r = { .device_from = 0, };
c71a8961
ACM
1208
1209 get_pdu_remap(ent, &r);
1210 return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
a42aaa3b
AB
1211 t_sector(ent), t_sec(ent),
1212 MAJOR(r.device_from), MINOR(r.device_from),
1213 (unsigned long long)r.sector_from);
c71a8961
ACM
1214}
1215
1216static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1217{
4ca53085
SR
1218 char cmd[TASK_COMM_LEN];
1219
1220 trace_find_cmdline(ent->pid, cmd);
1221
1222 return trace_seq_printf(s, "[%s]\n", cmd);
c71a8961
ACM
1223}
1224
1225static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1226{
4ca53085
SR
1227 char cmd[TASK_COMM_LEN];
1228
1229 trace_find_cmdline(ent->pid, cmd);
1230
1231 return trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent));
c71a8961
ACM
1232}
1233
1234static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1235{
4ca53085
SR
1236 char cmd[TASK_COMM_LEN];
1237
1238 trace_find_cmdline(ent->pid, cmd);
1239
c71a8961 1240 return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
4ca53085 1241 get_pdu_int(ent), cmd);
c71a8961
ACM
1242}
1243
18cea459
LZ
1244static int blk_log_msg(struct trace_seq *s, const struct trace_entry *ent)
1245{
1246 int ret;
1247 const struct blk_io_trace *t = te_blk_io_trace(ent);
1248
1249 ret = trace_seq_putmem(s, t + 1, t->pdu_len);
1250 if (ret)
1251 return trace_seq_putc(s, '\n');
1252 return ret;
1253}
1254
c71a8961
ACM
1255/*
1256 * struct tracer operations
1257 */
1258
1259static void blk_tracer_print_header(struct seq_file *m)
1260{
1261 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1262 return;
1263 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1264 "# | | | | | |\n");
1265}
1266
1267static void blk_tracer_start(struct trace_array *tr)
1268{
ad5dd549 1269 blk_tracer_enabled = true;
c71a8961
ACM
1270}
1271
1272static int blk_tracer_init(struct trace_array *tr)
1273{
1274 blk_tr = tr;
1275 blk_tracer_start(tr);
c71a8961
ACM
1276 return 0;
1277}
1278
1279static void blk_tracer_stop(struct trace_array *tr)
1280{
ad5dd549 1281 blk_tracer_enabled = false;
c71a8961
ACM
1282}
1283
1284static void blk_tracer_reset(struct trace_array *tr)
1285{
c71a8961
ACM
1286 blk_tracer_stop(tr);
1287}
1288
e4955c99 1289static const struct {
c71a8961 1290 const char *act[2];
ef18012b 1291 int (*print)(struct trace_seq *s, const struct trace_entry *ent);
e4955c99 1292} what2act[] = {
ef18012b 1293 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
c71a8961
ACM
1294 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1295 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1296 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1297 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1298 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1299 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1300 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1301 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1302 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
1303 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
1304 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1305 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1306 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1307 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1308};
1309
b6a4b0c3
LZ
1310static enum print_line_t print_one_line(struct trace_iterator *iter,
1311 bool classic)
c71a8961 1312{
2c9b238e 1313 struct trace_seq *s = &iter->seq;
b6a4b0c3
LZ
1314 const struct blk_io_trace *t;
1315 u16 what;
c71a8961 1316 int ret;
b6a4b0c3
LZ
1317 bool long_act;
1318 blk_log_action_t *log_action;
c71a8961 1319
b6a4b0c3
LZ
1320 t = te_blk_io_trace(iter->ent);
1321 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1322 long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1323 log_action = classic ? &blk_log_action_classic : &blk_log_action;
08a06b83 1324
18cea459
LZ
1325 if (t->action == BLK_TN_MESSAGE) {
1326 ret = log_action(iter, long_act ? "message" : "m");
1327 if (ret)
1328 ret = blk_log_msg(s, iter->ent);
1329 goto out;
1330 }
1331
eb08f8eb 1332 if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
b78825d6 1333 ret = trace_seq_printf(s, "Unknown action %x\n", what);
c71a8961 1334 else {
b6a4b0c3 1335 ret = log_action(iter, what2act[what].act[long_act]);
c71a8961 1336 if (ret)
2c9b238e 1337 ret = what2act[what].print(s, iter->ent);
c71a8961 1338 }
18cea459 1339out:
c71a8961
ACM
1340 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1341}
1342
b6a4b0c3 1343static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
a9a57763 1344 int flags, struct trace_event *event)
b6a4b0c3 1345{
b6a4b0c3
LZ
1346 return print_one_line(iter, false);
1347}
1348
08a06b83
ACM
1349static int blk_trace_synthesize_old_trace(struct trace_iterator *iter)
1350{
1351 struct trace_seq *s = &iter->seq;
1352 struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1353 const int offset = offsetof(struct blk_io_trace, sector);
1354 struct blk_io_trace old = {
1355 .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
6c051ce0 1356 .time = iter->ts,
08a06b83
ACM
1357 };
1358
1359 if (!trace_seq_putmem(s, &old, offset))
1360 return 0;
1361 return trace_seq_putmem(s, &t->sector,
1362 sizeof(old) - offset + t->pdu_len);
1363}
1364
ae7462b4 1365static enum print_line_t
a9a57763
SR
1366blk_trace_event_print_binary(struct trace_iterator *iter, int flags,
1367 struct trace_event *event)
08a06b83
ACM
1368{
1369 return blk_trace_synthesize_old_trace(iter) ?
1370 TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1371}
1372
c71a8961
ACM
1373static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1374{
c71a8961
ACM
1375 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1376 return TRACE_TYPE_UNHANDLED;
1377
b6a4b0c3 1378 return print_one_line(iter, true);
c71a8961
ACM
1379}
1380
f3948f88
LZ
1381static int blk_tracer_set_flag(u32 old_flags, u32 bit, int set)
1382{
1383 /* don't output context-info for blk_classic output */
1384 if (bit == TRACE_BLK_OPT_CLASSIC) {
1385 if (set)
1386 trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
1387 else
1388 trace_flags |= TRACE_ITER_CONTEXT_INFO;
1389 }
1390 return 0;
1391}
1392
c71a8961
ACM
1393static struct tracer blk_tracer __read_mostly = {
1394 .name = "blk",
1395 .init = blk_tracer_init,
1396 .reset = blk_tracer_reset,
1397 .start = blk_tracer_start,
1398 .stop = blk_tracer_stop,
1399 .print_header = blk_tracer_print_header,
1400 .print_line = blk_tracer_print_line,
1401 .flags = &blk_tracer_flags,
f3948f88 1402 .set_flag = blk_tracer_set_flag,
c71a8961
ACM
1403};
1404
a9a57763 1405static struct trace_event_functions trace_blk_event_funcs = {
c71a8961 1406 .trace = blk_trace_event_print,
08a06b83 1407 .binary = blk_trace_event_print_binary,
c71a8961
ACM
1408};
1409
a9a57763
SR
1410static struct trace_event trace_blk_event = {
1411 .type = TRACE_BLK,
1412 .funcs = &trace_blk_event_funcs,
1413};
1414
c71a8961
ACM
1415static int __init init_blk_tracer(void)
1416{
1417 if (!register_ftrace_event(&trace_blk_event)) {
1418 pr_warning("Warning: could not register block events\n");
1419 return 1;
1420 }
1421
1422 if (register_tracer(&blk_tracer) != 0) {
1423 pr_warning("Warning: could not register the block tracer\n");
1424 unregister_ftrace_event(&trace_blk_event);
1425 return 1;
1426 }
1427
1428 return 0;
1429}
1430
1431device_initcall(init_blk_tracer);
1432
1433static int blk_trace_remove_queue(struct request_queue *q)
1434{
1435 struct blk_trace *bt;
1436
1437 bt = xchg(&q->blk_trace, NULL);
1438 if (bt == NULL)
1439 return -EINVAL;
1440
17ba97e3
LZ
1441 if (atomic_dec_and_test(&blk_probes_ref))
1442 blk_unregister_tracepoints();
1443
ad5dd549 1444 blk_trace_free(bt);
c71a8961
ACM
1445 return 0;
1446}
1447
1448/*
1449 * Setup everything required to start tracing
1450 */
9908c309
LZ
1451static int blk_trace_setup_queue(struct request_queue *q,
1452 struct block_device *bdev)
c71a8961
ACM
1453{
1454 struct blk_trace *old_bt, *bt = NULL;
18cea459 1455 int ret = -ENOMEM;
c71a8961 1456
c71a8961
ACM
1457 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1458 if (!bt)
15152e44 1459 return -ENOMEM;
c71a8961 1460
18cea459
LZ
1461 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
1462 if (!bt->msg_data)
1463 goto free_bt;
1464
9908c309 1465 bt->dev = bdev->bd_dev;
c71a8961 1466 bt->act_mask = (u16)-1;
9908c309
LZ
1467
1468 blk_trace_setup_lba(bt, bdev);
c71a8961
ACM
1469
1470 old_bt = xchg(&q->blk_trace, bt);
1471 if (old_bt != NULL) {
1472 (void)xchg(&q->blk_trace, old_bt);
18cea459
LZ
1473 ret = -EBUSY;
1474 goto free_bt;
c71a8961 1475 }
15152e44 1476
17ba97e3
LZ
1477 if (atomic_inc_return(&blk_probes_ref) == 1)
1478 blk_register_tracepoints();
c71a8961 1479 return 0;
18cea459
LZ
1480
1481free_bt:
1482 blk_trace_free(bt);
1483 return ret;
c71a8961
ACM
1484}
1485
1486/*
1487 * sysfs interface to enable and configure tracing
1488 */
1489
c71a8961
ACM
1490static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1491 struct device_attribute *attr,
1492 char *buf);
1493static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1494 struct device_attribute *attr,
1495 const char *buf, size_t count);
1496#define BLK_TRACE_DEVICE_ATTR(_name) \
1497 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1498 sysfs_blk_trace_attr_show, \
1499 sysfs_blk_trace_attr_store)
1500
cd649b8b 1501static BLK_TRACE_DEVICE_ATTR(enable);
c71a8961
ACM
1502static BLK_TRACE_DEVICE_ATTR(act_mask);
1503static BLK_TRACE_DEVICE_ATTR(pid);
1504static BLK_TRACE_DEVICE_ATTR(start_lba);
1505static BLK_TRACE_DEVICE_ATTR(end_lba);
1506
1507static struct attribute *blk_trace_attrs[] = {
1508 &dev_attr_enable.attr,
1509 &dev_attr_act_mask.attr,
1510 &dev_attr_pid.attr,
1511 &dev_attr_start_lba.attr,
1512 &dev_attr_end_lba.attr,
1513 NULL
1514};
1515
1516struct attribute_group blk_trace_attr_group = {
1517 .name = "trace",
1518 .attrs = blk_trace_attrs,
1519};
1520
09341997
LZ
1521static const struct {
1522 int mask;
1523 const char *str;
1524} mask_maps[] = {
1525 { BLK_TC_READ, "read" },
1526 { BLK_TC_WRITE, "write" },
1527 { BLK_TC_BARRIER, "barrier" },
1528 { BLK_TC_SYNC, "sync" },
1529 { BLK_TC_QUEUE, "queue" },
1530 { BLK_TC_REQUEUE, "requeue" },
1531 { BLK_TC_ISSUE, "issue" },
1532 { BLK_TC_COMPLETE, "complete" },
1533 { BLK_TC_FS, "fs" },
1534 { BLK_TC_PC, "pc" },
1535 { BLK_TC_AHEAD, "ahead" },
1536 { BLK_TC_META, "meta" },
1537 { BLK_TC_DISCARD, "discard" },
1538 { BLK_TC_DRV_DATA, "drv_data" },
1539};
1540
1541static int blk_trace_str2mask(const char *str)
c71a8961 1542{
09341997 1543 int i;
c71a8961 1544 int mask = 0;
9eb85125 1545 char *buf, *s, *token;
c71a8961 1546
9eb85125
LZ
1547 buf = kstrdup(str, GFP_KERNEL);
1548 if (buf == NULL)
c71a8961 1549 return -ENOMEM;
9eb85125 1550 s = strstrip(buf);
c71a8961
ACM
1551
1552 while (1) {
09341997
LZ
1553 token = strsep(&s, ",");
1554 if (token == NULL)
c71a8961
ACM
1555 break;
1556
09341997
LZ
1557 if (*token == '\0')
1558 continue;
1559
1560 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1561 if (strcasecmp(token, mask_maps[i].str) == 0) {
1562 mask |= mask_maps[i].mask;
1563 break;
1564 }
1565 }
1566 if (i == ARRAY_SIZE(mask_maps)) {
1567 mask = -EINVAL;
1568 break;
1569 }
c71a8961 1570 }
9eb85125 1571 kfree(buf);
c71a8961
ACM
1572
1573 return mask;
1574}
1575
09341997
LZ
1576static ssize_t blk_trace_mask2str(char *buf, int mask)
1577{
1578 int i;
1579 char *p = buf;
1580
1581 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1582 if (mask & mask_maps[i].mask) {
1583 p += sprintf(p, "%s%s",
1584 (p == buf) ? "" : ",", mask_maps[i].str);
1585 }
1586 }
1587 *p++ = '\n';
1588
1589 return p - buf;
1590}
1591
b125130b
LZ
1592static struct request_queue *blk_trace_get_queue(struct block_device *bdev)
1593{
1594 if (bdev->bd_disk == NULL)
1595 return NULL;
1596
1597 return bdev_get_queue(bdev);
1598}
1599
c71a8961
ACM
1600static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1601 struct device_attribute *attr,
1602 char *buf)
1603{
1604 struct hd_struct *p = dev_to_part(dev);
1605 struct request_queue *q;
1606 struct block_device *bdev;
1607 ssize_t ret = -ENXIO;
1608
1609 lock_kernel();
1610 bdev = bdget(part_devt(p));
1611 if (bdev == NULL)
1612 goto out_unlock_kernel;
1613
b125130b 1614 q = blk_trace_get_queue(bdev);
c71a8961
ACM
1615 if (q == NULL)
1616 goto out_bdput;
b125130b 1617
c71a8961 1618 mutex_lock(&bdev->bd_mutex);
cd649b8b
LZ
1619
1620 if (attr == &dev_attr_enable) {
1621 ret = sprintf(buf, "%u\n", !!q->blk_trace);
1622 goto out_unlock_bdev;
1623 }
1624
c71a8961
ACM
1625 if (q->blk_trace == NULL)
1626 ret = sprintf(buf, "disabled\n");
1627 else if (attr == &dev_attr_act_mask)
09341997 1628 ret = blk_trace_mask2str(buf, q->blk_trace->act_mask);
c71a8961
ACM
1629 else if (attr == &dev_attr_pid)
1630 ret = sprintf(buf, "%u\n", q->blk_trace->pid);
1631 else if (attr == &dev_attr_start_lba)
1632 ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
1633 else if (attr == &dev_attr_end_lba)
1634 ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
cd649b8b
LZ
1635
1636out_unlock_bdev:
c71a8961
ACM
1637 mutex_unlock(&bdev->bd_mutex);
1638out_bdput:
1639 bdput(bdev);
1640out_unlock_kernel:
1641 unlock_kernel();
1642 return ret;
1643}
1644
1645static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1646 struct device_attribute *attr,
1647 const char *buf, size_t count)
1648{
1649 struct block_device *bdev;
1650 struct request_queue *q;
1651 struct hd_struct *p;
1652 u64 value;
09341997 1653 ssize_t ret = -EINVAL;
c71a8961
ACM
1654
1655 if (count == 0)
1656 goto out;
1657
1658 if (attr == &dev_attr_act_mask) {
1659 if (sscanf(buf, "%llx", &value) != 1) {
1660 /* Assume it is a list of trace category names */
09341997
LZ
1661 ret = blk_trace_str2mask(buf);
1662 if (ret < 0)
c71a8961 1663 goto out;
09341997 1664 value = ret;
c71a8961
ACM
1665 }
1666 } else if (sscanf(buf, "%llu", &value) != 1)
1667 goto out;
1668
09341997
LZ
1669 ret = -ENXIO;
1670
c71a8961
ACM
1671 lock_kernel();
1672 p = dev_to_part(dev);
1673 bdev = bdget(part_devt(p));
1674 if (bdev == NULL)
1675 goto out_unlock_kernel;
1676
b125130b 1677 q = blk_trace_get_queue(bdev);
c71a8961
ACM
1678 if (q == NULL)
1679 goto out_bdput;
1680
1681 mutex_lock(&bdev->bd_mutex);
cd649b8b
LZ
1682
1683 if (attr == &dev_attr_enable) {
1684 if (value)
9908c309 1685 ret = blk_trace_setup_queue(q, bdev);
cd649b8b
LZ
1686 else
1687 ret = blk_trace_remove_queue(q);
1688 goto out_unlock_bdev;
1689 }
1690
c71a8961
ACM
1691 ret = 0;
1692 if (q->blk_trace == NULL)
9908c309 1693 ret = blk_trace_setup_queue(q, bdev);
c71a8961
ACM
1694
1695 if (ret == 0) {
1696 if (attr == &dev_attr_act_mask)
1697 q->blk_trace->act_mask = value;
1698 else if (attr == &dev_attr_pid)
1699 q->blk_trace->pid = value;
1700 else if (attr == &dev_attr_start_lba)
1701 q->blk_trace->start_lba = value;
1702 else if (attr == &dev_attr_end_lba)
1703 q->blk_trace->end_lba = value;
c71a8961 1704 }
cd649b8b
LZ
1705
1706out_unlock_bdev:
c71a8961
ACM
1707 mutex_unlock(&bdev->bd_mutex);
1708out_bdput:
1709 bdput(bdev);
1710out_unlock_kernel:
1711 unlock_kernel();
1712out:
cd649b8b 1713 return ret ? ret : count;
c71a8961 1714}
cd649b8b 1715
1d54ad6d
LZ
1716int blk_trace_init_sysfs(struct device *dev)
1717{
1718 return sysfs_create_group(&dev->kobj, &blk_trace_attr_group);
1719}
1720
48c0d4d4
ZK
1721void blk_trace_remove_sysfs(struct device *dev)
1722{
1723 sysfs_remove_group(&dev->kobj, &blk_trace_attr_group);
1724}
1725
55782138
LZ
1726#endif /* CONFIG_BLK_DEV_IO_TRACE */
1727
1728#ifdef CONFIG_EVENT_TRACING
1729
1730void blk_dump_cmd(char *buf, struct request *rq)
1731{
1732 int i, end;
1733 int len = rq->cmd_len;
1734 unsigned char *cmd = rq->cmd;
1735
33659ebb 1736 if (rq->cmd_type != REQ_TYPE_BLOCK_PC) {
55782138
LZ
1737 buf[0] = '\0';
1738 return;
1739 }
1740
1741 for (end = len - 1; end >= 0; end--)
1742 if (cmd[end])
1743 break;
1744 end++;
1745
1746 for (i = 0; i < len; i++) {
1747 buf += sprintf(buf, "%s%02x", i == 0 ? "" : " ", cmd[i]);
1748 if (i == end && end != len - 1) {
1749 sprintf(buf, " ..");
1750 break;
1751 }
1752 }
1753}
1754
1755void blk_fill_rwbs(char *rwbs, u32 rw, int bytes)
1756{
1757 int i = 0;
1758
1759 if (rw & WRITE)
1760 rwbs[i++] = 'W';
7b6d91da 1761 else if (rw & REQ_DISCARD)
55782138
LZ
1762 rwbs[i++] = 'D';
1763 else if (bytes)
1764 rwbs[i++] = 'R';
1765 else
1766 rwbs[i++] = 'N';
1767
7b6d91da 1768 if (rw & REQ_RAHEAD)
55782138 1769 rwbs[i++] = 'A';
7b6d91da 1770 if (rw & REQ_HARDBARRIER)
55782138 1771 rwbs[i++] = 'B';
7b6d91da 1772 if (rw & REQ_SYNC)
55782138 1773 rwbs[i++] = 'S';
7b6d91da 1774 if (rw & REQ_META)
55782138
LZ
1775 rwbs[i++] = 'M';
1776
1777 rwbs[i] = '\0';
1778}
1779
1780void blk_fill_rwbs_rq(char *rwbs, struct request *rq)
1781{
1782 int rw = rq->cmd_flags & 0x03;
1783 int bytes;
1784
33659ebb 1785 if (rq->cmd_flags & REQ_DISCARD)
7b6d91da 1786 rw |= REQ_DISCARD;
55782138 1787
c9059598 1788 bytes = blk_rq_bytes(rq);
55782138
LZ
1789
1790 blk_fill_rwbs(rwbs, rw, bytes);
1791}
1792
1793#endif /* CONFIG_EVENT_TRACING */
1794