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