Merge branch 'for-3.10/drivers' of git://git.kernel.dk/linux-block
[linux-3.10.git] / kernel / trace / blktrace.c
index 223b92e..b8b8560 100644 (file)
 #include <linux/percpu.h>
 #include <linux/init.h>
 #include <linux/mutex.h>
+#include <linux/slab.h>
 #include <linux/debugfs.h>
+#include <linux/export.h>
 #include <linux/time.h>
-#include <trace/block.h>
 #include <linux/uaccess.h>
+
+#include <trace/events/block.h>
+
 #include "trace_output.h"
 
+#ifdef CONFIG_BLK_DEV_IO_TRACE
+
 static unsigned int blktrace_seq __read_mostly = 1;
 
 static struct trace_array *blk_tr;
@@ -59,22 +65,41 @@ static void trace_note(struct blk_trace *bt, pid_t pid, int action,
                       const void *data, size_t len)
 {
        struct blk_io_trace *t;
+       struct ring_buffer_event *event = NULL;
+       struct ring_buffer *buffer = NULL;
+       int pc = 0;
+       int cpu = smp_processor_id();
+       bool blk_tracer = blk_tracer_enabled;
+
+       if (blk_tracer) {
+               buffer = blk_tr->trace_buffer.buffer;
+               pc = preempt_count();
+               event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
+                                                 sizeof(*t) + len,
+                                                 0, pc);
+               if (!event)
+                       return;
+               t = ring_buffer_event_data(event);
+               goto record_it;
+       }
 
        if (!bt->rchan)
                return;
 
        t = relay_reserve(bt->rchan, sizeof(*t) + len);
        if (t) {
-               const int cpu = smp_processor_id();
-
                t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
                t->time = ktime_to_ns(ktime_get());
+record_it:
                t->device = bt->dev;
                t->action = action;
                t->pid = pid;
                t->cpu = cpu;
                t->pdu_len = len;
                memcpy((void *) t + sizeof(*t), data, len);
+
+               if (blk_tracer)
+                       trace_buffer_unlock_commit(buffer, event, 0, pc);
        }
 }
 
@@ -110,18 +135,19 @@ void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
        unsigned long flags;
        char *buf;
 
-       if (blk_tr) {
-               va_start(args, fmt);
-               ftrace_vprintk(fmt, args);
-               va_end(args);
+       if (unlikely(bt->trace_state != Blktrace_running &&
+                    !blk_tracer_enabled))
                return;
-       }
 
-       if (!bt->msg_data)
+       /*
+        * If the BLK_TC_NOTIFY action mask isn't set, don't send any note
+        * message to the trace.
+        */
+       if (!(bt->act_mask & BLK_TC_NOTIFY))
                return;
 
        local_irq_save(flags);
-       buf = per_cpu_ptr(bt->msg_data, smp_processor_id());
+       buf = this_cpu_ptr(bt->msg_data);
        va_start(args, fmt);
        n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
        va_end(args);
@@ -136,7 +162,7 @@ static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
 {
        if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
                return 1;
-       if (sector < bt->start_lba || sector > bt->end_lba)
+       if (sector && (sector < bt->start_lba || sector > bt->end_lba))
                return 1;
        if (bt->pid && pid != bt->pid)
                return 1;
@@ -147,12 +173,14 @@ static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
 /*
  * Data direction bit lookup
  */
-static u32 ddir_act[2] __read_mostly = { BLK_TC_ACT(BLK_TC_READ),
-                                        BLK_TC_ACT(BLK_TC_WRITE) };
+static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ),
+                                BLK_TC_ACT(BLK_TC_WRITE) };
+
+#define BLK_TC_RAHEAD          BLK_TC_AHEAD
 
 /* The ilog2() calls fall out because they're constant */
-#define MASK_TC_BIT(rw, __name) ((rw & (1 << BIO_RW_ ## __name)) << \
-         (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name))
+#define MASK_TC_BIT(rw, __name) ((rw & REQ_ ## __name) << \
+         (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - __REQ_ ## __name))
 
 /*
  * The worker for the various blk_add_trace*() types. Fills out a
@@ -163,33 +191,36 @@ static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
 {
        struct task_struct *tsk = current;
        struct ring_buffer_event *event = NULL;
+       struct ring_buffer *buffer = NULL;
        struct blk_io_trace *t;
        unsigned long flags = 0;
        unsigned long *sequence;
        pid_t pid;
        int cpu, pc = 0;
+       bool blk_tracer = blk_tracer_enabled;
 
-       if (unlikely(bt->trace_state != Blktrace_running ||
-                    !blk_tracer_enabled))
+       if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer))
                return;
 
        what |= ddir_act[rw & WRITE];
-       what |= MASK_TC_BIT(rw, BARRIER);
-       what |= MASK_TC_BIT(rw, SYNCIO);
-       what |= MASK_TC_BIT(rw, AHEAD);
+       what |= MASK_TC_BIT(rw, SYNC);
+       what |= MASK_TC_BIT(rw, RAHEAD);
        what |= MASK_TC_BIT(rw, META);
        what |= MASK_TC_BIT(rw, DISCARD);
+       what |= MASK_TC_BIT(rw, FLUSH);
+       what |= MASK_TC_BIT(rw, FUA);
 
        pid = tsk->pid;
-       if (unlikely(act_log_check(bt, what, sector, pid)))
+       if (act_log_check(bt, what, sector, pid))
                return;
        cpu = raw_smp_processor_id();
 
-       if (blk_tr) {
+       if (blk_tracer) {
                tracing_record_cmdline(current);
 
+               buffer = blk_tr->trace_buffer.buffer;
                pc = preempt_count();
-               event = trace_buffer_lock_reserve(blk_tr, TRACE_BLK,
+               event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
                                                  sizeof(*t) + pdu_len,
                                                  0, pc);
                if (!event)
@@ -235,8 +266,8 @@ record_it:
                if (pdu_len)
                        memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
 
-               if (blk_tr) {
-                       trace_buffer_unlock_commit(blk_tr, event, 0, pc);
+               if (blk_tracer) {
+                       trace_buffer_unlock_commit(buffer, event, 0, pc);
                        return;
                }
        }
@@ -247,14 +278,20 @@ record_it:
 static struct dentry *blk_tree_root;
 static DEFINE_MUTEX(blk_tree_mutex);
 
-static void blk_trace_cleanup(struct blk_trace *bt)
+static void blk_trace_free(struct blk_trace *bt)
 {
        debugfs_remove(bt->msg_file);
        debugfs_remove(bt->dropped_file);
        relay_close(bt->rchan);
+       debugfs_remove(bt->dir);
        free_percpu(bt->sequence);
        free_percpu(bt->msg_data);
        kfree(bt);
+}
+
+static void blk_trace_cleanup(struct blk_trace *bt)
+{
+       blk_trace_free(bt);
        if (atomic_dec_and_test(&blk_probes_ref))
                blk_unregister_tracepoints();
 }
@@ -267,21 +304,13 @@ int blk_trace_remove(struct request_queue *q)
        if (!bt)
                return -EINVAL;
 
-       if (bt->trace_state == Blktrace_setup ||
-           bt->trace_state == Blktrace_stopped)
+       if (bt->trace_state != Blktrace_running)
                blk_trace_cleanup(bt);
 
        return 0;
 }
 EXPORT_SYMBOL_GPL(blk_trace_remove);
 
-static int blk_dropped_open(struct inode *inode, struct file *filp)
-{
-       filp->private_data = inode->i_private;
-
-       return 0;
-}
-
 static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
                                size_t count, loff_t *ppos)
 {
@@ -295,27 +324,21 @@ static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
 
 static const struct file_operations blk_dropped_fops = {
        .owner =        THIS_MODULE,
-       .open =         blk_dropped_open,
+       .open =         simple_open,
        .read =         blk_dropped_read,
+       .llseek =       default_llseek,
 };
 
-static int blk_msg_open(struct inode *inode, struct file *filp)
-{
-       filp->private_data = inode->i_private;
-
-       return 0;
-}
-
 static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
                                size_t count, loff_t *ppos)
 {
        char *msg;
        struct blk_trace *bt;
 
-       if (count > BLK_TN_MAX_MSG)
+       if (count >= BLK_TN_MAX_MSG)
                return -EINVAL;
 
-       msg = kmalloc(count, GFP_KERNEL);
+       msg = kmalloc(count + 1, GFP_KERNEL);
        if (msg == NULL)
                return -ENOMEM;
 
@@ -324,6 +347,7 @@ static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
                return -EFAULT;
        }
 
+       msg[count] = '\0';
        bt = filp->private_data;
        __trace_note_message(bt, "%s", msg);
        kfree(msg);
@@ -333,8 +357,9 @@ static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
 
 static const struct file_operations blk_msg_fops = {
        .owner =        THIS_MODULE,
-       .open =         blk_msg_open,
+       .open =         simple_open,
        .write =        blk_msg_write,
+       .llseek =       noop_llseek,
 };
 
 /*
@@ -356,24 +381,14 @@ static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
 
 static int blk_remove_buf_file_callback(struct dentry *dentry)
 {
-       struct dentry *parent = dentry->d_parent;
        debugfs_remove(dentry);
 
-       /*
-       * this will fail for all but the last file, but that is ok. what we
-       * care about is the top level buts->name directory going away, when
-       * the last trace file is gone. Then we don't have to rmdir() that
-       * manually on trace stop, so it nicely solves the issue with
-       * force killing of running traces.
-       */
-
-       debugfs_remove(parent);
        return 0;
 }
 
 static struct dentry *blk_create_buf_file_callback(const char *filename,
                                                   struct dentry *parent,
-                                                  int mode,
+                                                  umode_t mode,
                                                   struct rchan_buf *buf,
                                                   int *is_global)
 {
@@ -387,11 +402,29 @@ static struct rchan_callbacks blk_relay_callbacks = {
        .remove_buf_file        = blk_remove_buf_file_callback,
 };
 
+static void blk_trace_setup_lba(struct blk_trace *bt,
+                               struct block_device *bdev)
+{
+       struct hd_struct *part = NULL;
+
+       if (bdev)
+               part = bdev->bd_part;
+
+       if (part) {
+               bt->start_lba = part->start_sect;
+               bt->end_lba = part->start_sect + part->nr_sects;
+       } else {
+               bt->start_lba = 0;
+               bt->end_lba = -1ULL;
+       }
+}
+
 /*
  * Setup everything required to start tracing
  */
 int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
-                       struct blk_user_trace_setup *buts)
+                      struct block_device *bdev,
+                      struct blk_user_trace_setup *buts)
 {
        struct blk_trace *old_bt, *bt = NULL;
        struct dentry *dir = NULL;
@@ -411,11 +444,11 @@ int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
                if (buts->name[i] == '/')
                        buts->name[i] = '_';
 
-       ret = -ENOMEM;
        bt = kzalloc(sizeof(*bt), GFP_KERNEL);
        if (!bt)
-               goto err;
+               return -ENOMEM;
 
+       ret = -ENOMEM;
        bt->sequence = alloc_percpu(unsigned long);
        if (!bt->sequence)
                goto err;
@@ -426,11 +459,15 @@ int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
 
        ret = -ENOENT;
 
+       mutex_lock(&blk_tree_mutex);
        if (!blk_tree_root) {
                blk_tree_root = debugfs_create_dir("block", NULL);
-               if (!blk_tree_root)
+               if (!blk_tree_root) {
+                       mutex_unlock(&blk_tree_mutex);
                        goto err;
+               }
        }
+       mutex_unlock(&blk_tree_mutex);
 
        dir = debugfs_create_dir(buts->name, blk_tree_root);
 
@@ -460,17 +497,17 @@ int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
        if (!bt->act_mask)
                bt->act_mask = (u16) -1;
 
-       bt->start_lba = buts->start_lba;
-       bt->end_lba = buts->end_lba;
-       if (!bt->end_lba)
-               bt->end_lba = -1ULL;
+       blk_trace_setup_lba(bt, bdev);
+
+       /* overwrite with user settings */
+       if (buts->start_lba)
+               bt->start_lba = buts->start_lba;
+       if (buts->end_lba)
+               bt->end_lba = buts->end_lba;
 
        bt->pid = buts->pid;
        bt->trace_state = Blktrace_setup;
 
-       if (atomic_add_return(1, &blk_probes_ref) == 1)
-               blk_register_tracepoints();
-
        ret = -EBUSY;
        old_bt = xchg(&q->blk_trace, bt);
        if (old_bt) {
@@ -478,23 +515,17 @@ int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
                goto err;
        }
 
+       if (atomic_inc_return(&blk_probes_ref) == 1)
+               blk_register_tracepoints();
+
        return 0;
 err:
-       if (bt) {
-               if (bt->msg_file)
-                       debugfs_remove(bt->msg_file);
-               if (bt->dropped_file)
-                       debugfs_remove(bt->dropped_file);
-               free_percpu(bt->sequence);
-               free_percpu(bt->msg_data);
-               if (bt->rchan)
-                       relay_close(bt->rchan);
-               kfree(bt);
-       }
+       blk_trace_free(bt);
        return ret;
 }
 
 int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
+                   struct block_device *bdev,
                    char __user *arg)
 {
        struct blk_user_trace_setup buts;
@@ -504,17 +535,53 @@ int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
        if (ret)
                return -EFAULT;
 
-       ret = do_blk_trace_setup(q, name, dev, &buts);
+       ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
        if (ret)
                return ret;
 
-       if (copy_to_user(arg, &buts, sizeof(buts)))
+       if (copy_to_user(arg, &buts, sizeof(buts))) {
+               blk_trace_remove(q);
                return -EFAULT;
-
+       }
        return 0;
 }
 EXPORT_SYMBOL_GPL(blk_trace_setup);
 
+#if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64)
+static int compat_blk_trace_setup(struct request_queue *q, char *name,
+                                 dev_t dev, struct block_device *bdev,
+                                 char __user *arg)
+{
+       struct blk_user_trace_setup buts;
+       struct compat_blk_user_trace_setup cbuts;
+       int ret;
+
+       if (copy_from_user(&cbuts, arg, sizeof(cbuts)))
+               return -EFAULT;
+
+       buts = (struct blk_user_trace_setup) {
+               .act_mask = cbuts.act_mask,
+               .buf_size = cbuts.buf_size,
+               .buf_nr = cbuts.buf_nr,
+               .start_lba = cbuts.start_lba,
+               .end_lba = cbuts.end_lba,
+               .pid = cbuts.pid,
+       };
+       memcpy(&buts.name, &cbuts.name, 32);
+
+       ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
+       if (ret)
+               return ret;
+
+       if (copy_to_user(arg, &buts.name, 32)) {
+               blk_trace_remove(q);
+               return -EFAULT;
+       }
+
+       return 0;
+}
+#endif
+
 int blk_trace_startstop(struct request_queue *q, int start)
 {
        int ret;
@@ -572,8 +639,14 @@ int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
        switch (cmd) {
        case BLKTRACESETUP:
                bdevname(bdev, b);
-               ret = blk_trace_setup(q, b, bdev->bd_dev, arg);
+               ret = blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
+               break;
+#if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64)
+       case BLKTRACESETUP32:
+               bdevname(bdev, b);
+               ret = compat_blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
                break;
+#endif
        case BLKTRACESTART:
                start = 1;
        case BLKTRACESTOP:
@@ -619,50 +692,51 @@ void blk_trace_shutdown(struct request_queue *q)
  *
  **/
 static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
-                                   u32 what)
+                            u32 what)
 {
        struct blk_trace *bt = q->blk_trace;
-       int rw = rq->cmd_flags & 0x03;
 
        if (likely(!bt))
                return;
 
-       if (blk_discard_rq(rq))
-               rw |= (1 << BIO_RW_DISCARD);
-
-       if (blk_pc_request(rq)) {
+       if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
                what |= BLK_TC_ACT(BLK_TC_PC);
-               __blk_add_trace(bt, 0, rq->data_len, rw, what, rq->errors,
-                               sizeof(rq->cmd), rq->cmd);
+               __blk_add_trace(bt, 0, blk_rq_bytes(rq), rq->cmd_flags,
+                               what, rq->errors, rq->cmd_len, rq->cmd);
        } else  {
                what |= BLK_TC_ACT(BLK_TC_FS);
-               __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9,
-                               rw, what, rq->errors, 0, NULL);
+               __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq),
+                               rq->cmd_flags, what, rq->errors, 0, NULL);
        }
 }
 
-static void blk_add_trace_rq_abort(struct request_queue *q, struct request *rq)
+static void blk_add_trace_rq_abort(void *ignore,
+                                  struct request_queue *q, struct request *rq)
 {
        blk_add_trace_rq(q, rq, BLK_TA_ABORT);
 }
 
-static void blk_add_trace_rq_insert(struct request_queue *q, struct request *rq)
+static void blk_add_trace_rq_insert(void *ignore,
+                                   struct request_queue *q, struct request *rq)
 {
        blk_add_trace_rq(q, rq, BLK_TA_INSERT);
 }
 
-static void blk_add_trace_rq_issue(struct request_queue *q, struct request *rq)
+static void blk_add_trace_rq_issue(void *ignore,
+                                  struct request_queue *q, struct request *rq)
 {
        blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
 }
 
-static void blk_add_trace_rq_requeue(struct request_queue *q,
+static void blk_add_trace_rq_requeue(void *ignore,
+                                    struct request_queue *q,
                                     struct request *rq)
 {
        blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
 }
 
-static void blk_add_trace_rq_complete(struct request_queue *q,
+static void blk_add_trace_rq_complete(void *ignore,
+                                     struct request_queue *q,
                                      struct request *rq)
 {
        blk_add_trace_rq(q, rq, BLK_TA_COMPLETE);
@@ -673,55 +747,68 @@ static void blk_add_trace_rq_complete(struct request_queue *q,
  * @q:         queue the io is for
  * @bio:       the source bio
  * @what:      the action
+ * @error:     error, if any
  *
  * Description:
  *     Records an action against a bio. Will log the bio offset + size.
  *
  **/
 static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
-                                    u32 what)
+                             u32 what, int error)
 {
        struct blk_trace *bt = q->blk_trace;
 
        if (likely(!bt))
                return;
 
+       if (!error && !bio_flagged(bio, BIO_UPTODATE))
+               error = EIO;
+
        __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what,
-                       !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
+                       error, 0, NULL);
 }
 
-static void blk_add_trace_bio_bounce(struct request_queue *q, struct bio *bio)
+static void blk_add_trace_bio_bounce(void *ignore,
+                                    struct request_queue *q, struct bio *bio)
 {
-       blk_add_trace_bio(q, bio, BLK_TA_BOUNCE);
+       blk_add_trace_bio(q, bio, BLK_TA_BOUNCE, 0);
 }
 
-static void blk_add_trace_bio_complete(struct request_queue *q, struct bio *bio)
+static void blk_add_trace_bio_complete(void *ignore,
+                                      struct request_queue *q, struct bio *bio,
+                                      int error)
 {
-       blk_add_trace_bio(q, bio, BLK_TA_COMPLETE);
+       blk_add_trace_bio(q, bio, BLK_TA_COMPLETE, error);
 }
 
-static void blk_add_trace_bio_backmerge(struct request_queue *q,
+static void blk_add_trace_bio_backmerge(void *ignore,
+                                       struct request_queue *q,
+                                       struct request *rq,
                                        struct bio *bio)
 {
-       blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
+       blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE, 0);
 }
 
-static void blk_add_trace_bio_frontmerge(struct request_queue *q,
+static void blk_add_trace_bio_frontmerge(void *ignore,
+                                        struct request_queue *q,
+                                        struct request *rq,
                                         struct bio *bio)
 {
-       blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
+       blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE, 0);
 }
 
-static void blk_add_trace_bio_queue(struct request_queue *q, struct bio *bio)
+static void blk_add_trace_bio_queue(void *ignore,
+                                   struct request_queue *q, struct bio *bio)
 {
-       blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
+       blk_add_trace_bio(q, bio, BLK_TA_QUEUE, 0);
 }
 
-static void blk_add_trace_getrq(struct request_queue *q,
+static void blk_add_trace_getrq(void *ignore,
+                               struct request_queue *q,
                                struct bio *bio, int rw)
 {
        if (bio)
-               blk_add_trace_bio(q, bio, BLK_TA_GETRQ);
+               blk_add_trace_bio(q, bio, BLK_TA_GETRQ, 0);
        else {
                struct blk_trace *bt = q->blk_trace;
 
@@ -731,11 +818,12 @@ static void blk_add_trace_getrq(struct request_queue *q,
 }
 
 
-static void blk_add_trace_sleeprq(struct request_queue *q,
+static void blk_add_trace_sleeprq(void *ignore,
+                                 struct request_queue *q,
                                  struct bio *bio, int rw)
 {
        if (bio)
-               blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ);
+               blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ, 0);
        else {
                struct blk_trace *bt = q->blk_trace;
 
@@ -745,7 +833,7 @@ static void blk_add_trace_sleeprq(struct request_queue *q,
        }
 }
 
-static void blk_add_trace_plug(struct request_queue *q)
+static void blk_add_trace_plug(void *ignore, struct request_queue *q)
 {
        struct blk_trace *bt = q->blk_trace;
 
@@ -753,33 +841,26 @@ static void blk_add_trace_plug(struct request_queue *q)
                __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
 }
 
-static void blk_add_trace_unplug_io(struct request_queue *q)
+static void blk_add_trace_unplug(void *ignore, struct request_queue *q,
+                                   unsigned int depth, bool explicit)
 {
        struct blk_trace *bt = q->blk_trace;
 
        if (bt) {
-               unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
-               __be64 rpdu = cpu_to_be64(pdu);
+               __be64 rpdu = cpu_to_be64(depth);
+               u32 what;
 
-               __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_IO, 0,
-                               sizeof(rpdu), &rpdu);
-       }
-}
+               if (explicit)
+                       what = BLK_TA_UNPLUG_IO;
+               else
+                       what = BLK_TA_UNPLUG_TIMER;
 
-static void blk_add_trace_unplug_timer(struct request_queue *q)
-{
-       struct blk_trace *bt = q->blk_trace;
-
-       if (bt) {
-               unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
-               __be64 rpdu = cpu_to_be64(pdu);
-
-               __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_TIMER, 0,
-                               sizeof(rpdu), &rpdu);
+               __blk_add_trace(bt, 0, 0, 0, what, 0, sizeof(rpdu), &rpdu);
        }
 }
 
-static void blk_add_trace_split(struct request_queue *q, struct bio *bio,
+static void blk_add_trace_split(void *ignore,
+                               struct request_queue *q, struct bio *bio,
                                unsigned int pdu)
 {
        struct blk_trace *bt = q->blk_trace;
@@ -794,20 +875,54 @@ static void blk_add_trace_split(struct request_queue *q, struct bio *bio,
 }
 
 /**
- * blk_add_trace_remap - Add a trace for a remap operation
+ * blk_add_trace_bio_remap - Add a trace for a bio-remap operation
+ * @ignore:    trace callback data parameter (not used)
  * @q:         queue the io is for
  * @bio:       the source bio
  * @dev:       target device
  * @from:      source sector
- * @to:                target sector
  *
  * Description:
  *     Device mapper or raid target sometimes need to split a bio because
  *     it spans a stripe (or similar). Add a trace for that action.
  *
  **/
-static void blk_add_trace_remap(struct request_queue *q, struct bio *bio,
-                                      dev_t dev, sector_t from, sector_t to)
+static void blk_add_trace_bio_remap(void *ignore,
+                                   struct request_queue *q, struct bio *bio,
+                                   dev_t dev, sector_t from)
+{
+       struct blk_trace *bt = q->blk_trace;
+       struct blk_io_trace_remap r;
+
+       if (likely(!bt))
+               return;
+
+       r.device_from = cpu_to_be32(dev);
+       r.device_to   = cpu_to_be32(bio->bi_bdev->bd_dev);
+       r.sector_from = cpu_to_be64(from);
+
+       __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
+                       BLK_TA_REMAP, !bio_flagged(bio, BIO_UPTODATE),
+                       sizeof(r), &r);
+}
+
+/**
+ * blk_add_trace_rq_remap - Add a trace for a request-remap operation
+ * @ignore:    trace callback data parameter (not used)
+ * @q:         queue the io is for
+ * @rq:                the source request
+ * @dev:       target device
+ * @from:      source sector
+ *
+ * Description:
+ *     Device mapper remaps request to other devices.
+ *     Add a trace for that action.
+ *
+ **/
+static void blk_add_trace_rq_remap(void *ignore,
+                                  struct request_queue *q,
+                                  struct request *rq, dev_t dev,
+                                  sector_t from)
 {
        struct blk_trace *bt = q->blk_trace;
        struct blk_io_trace_remap r;
@@ -815,12 +930,13 @@ static void blk_add_trace_remap(struct request_queue *q, struct bio *bio,
        if (likely(!bt))
                return;
 
-       r.device = cpu_to_be32(dev);
-       r.device_from = cpu_to_be32(bio->bi_bdev->bd_dev);
-       r.sector = cpu_to_be64(to);
+       r.device_from = cpu_to_be32(dev);
+       r.device_to   = cpu_to_be32(disk_devt(rq->rq_disk));
+       r.sector_from = cpu_to_be64(from);
 
-       __blk_add_trace(bt, from, bio->bi_size, bio->bi_rw, BLK_TA_REMAP,
-                       !bio_flagged(bio, BIO_UPTODATE), sizeof(r), &r);
+       __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq),
+                       rq_data_dir(rq), BLK_TA_REMAP, !!rq->errors,
+                       sizeof(r), &r);
 }
 
 /**
@@ -843,12 +959,12 @@ void blk_add_driver_data(struct request_queue *q,
        if (likely(!bt))
                return;
 
-       if (blk_pc_request(rq))
-               __blk_add_trace(bt, 0, rq->data_len, 0, BLK_TA_DRV_DATA,
-                               rq->errors, len, data);
+       if (rq->cmd_type == REQ_TYPE_BLOCK_PC)
+               __blk_add_trace(bt, 0, blk_rq_bytes(rq), 0,
+                               BLK_TA_DRV_DATA, rq->errors, len, data);
        else
-               __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9,
-                               0, BLK_TA_DRV_DATA, rq->errors, len, data);
+               __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), 0,
+                               BLK_TA_DRV_DATA, rq->errors, len, data);
 }
 EXPORT_SYMBOL_GPL(blk_add_driver_data);
 
@@ -856,61 +972,61 @@ static void blk_register_tracepoints(void)
 {
        int ret;
 
-       ret = register_trace_block_rq_abort(blk_add_trace_rq_abort);
+       ret = register_trace_block_rq_abort(blk_add_trace_rq_abort, NULL);
        WARN_ON(ret);
-       ret = register_trace_block_rq_insert(blk_add_trace_rq_insert);
+       ret = register_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
        WARN_ON(ret);
-       ret = register_trace_block_rq_issue(blk_add_trace_rq_issue);
+       ret = register_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
        WARN_ON(ret);
-       ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue);
+       ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
        WARN_ON(ret);
-       ret = register_trace_block_rq_complete(blk_add_trace_rq_complete);
+       ret = register_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
        WARN_ON(ret);
-       ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce);
+       ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
        WARN_ON(ret);
-       ret = register_trace_block_bio_complete(blk_add_trace_bio_complete);
+       ret = register_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
        WARN_ON(ret);
-       ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
+       ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
        WARN_ON(ret);
-       ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
+       ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
        WARN_ON(ret);
-       ret = register_trace_block_bio_queue(blk_add_trace_bio_queue);
+       ret = register_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
        WARN_ON(ret);
-       ret = register_trace_block_getrq(blk_add_trace_getrq);
+       ret = register_trace_block_getrq(blk_add_trace_getrq, NULL);
        WARN_ON(ret);
-       ret = register_trace_block_sleeprq(blk_add_trace_sleeprq);
+       ret = register_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
        WARN_ON(ret);
-       ret = register_trace_block_plug(blk_add_trace_plug);
+       ret = register_trace_block_plug(blk_add_trace_plug, NULL);
        WARN_ON(ret);
-       ret = register_trace_block_unplug_timer(blk_add_trace_unplug_timer);
+       ret = register_trace_block_unplug(blk_add_trace_unplug, NULL);
        WARN_ON(ret);
-       ret = register_trace_block_unplug_io(blk_add_trace_unplug_io);
+       ret = register_trace_block_split(blk_add_trace_split, NULL);
        WARN_ON(ret);
-       ret = register_trace_block_split(blk_add_trace_split);
+       ret = register_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
        WARN_ON(ret);
-       ret = register_trace_block_remap(blk_add_trace_remap);
+       ret = register_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
        WARN_ON(ret);
 }
 
 static void blk_unregister_tracepoints(void)
 {
-       unregister_trace_block_remap(blk_add_trace_remap);
-       unregister_trace_block_split(blk_add_trace_split);
-       unregister_trace_block_unplug_io(blk_add_trace_unplug_io);
-       unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer);
-       unregister_trace_block_plug(blk_add_trace_plug);
-       unregister_trace_block_sleeprq(blk_add_trace_sleeprq);
-       unregister_trace_block_getrq(blk_add_trace_getrq);
-       unregister_trace_block_bio_queue(blk_add_trace_bio_queue);
-       unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
-       unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
-       unregister_trace_block_bio_complete(blk_add_trace_bio_complete);
-       unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce);
-       unregister_trace_block_rq_complete(blk_add_trace_rq_complete);
-       unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue);
-       unregister_trace_block_rq_issue(blk_add_trace_rq_issue);
-       unregister_trace_block_rq_insert(blk_add_trace_rq_insert);
-       unregister_trace_block_rq_abort(blk_add_trace_rq_abort);
+       unregister_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
+       unregister_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
+       unregister_trace_block_split(blk_add_trace_split, NULL);
+       unregister_trace_block_unplug(blk_add_trace_unplug, NULL);
+       unregister_trace_block_plug(blk_add_trace_plug, NULL);
+       unregister_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
+       unregister_trace_block_getrq(blk_add_trace_getrq, NULL);
+       unregister_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
+       unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
+       unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
+       unregister_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
+       unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
+       unregister_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
+       unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
+       unregister_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
+       unregister_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
+       unregister_trace_block_rq_abort(blk_add_trace_rq_abort, NULL);
 
        tracepoint_synchronize_unregister();
 }
@@ -922,25 +1038,34 @@ static void blk_unregister_tracepoints(void)
 static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
 {
        int i = 0;
+       int tc = t->action >> BLK_TC_SHIFT;
 
-       if (t->action & BLK_TC_DISCARD)
+       if (t->action == BLK_TN_MESSAGE) {
+               rwbs[i++] = 'N';
+               goto out;
+       }
+
+       if (tc & BLK_TC_FLUSH)
+               rwbs[i++] = 'F';
+
+       if (tc & BLK_TC_DISCARD)
                rwbs[i++] = 'D';
-       else if (t->action & BLK_TC_WRITE)
+       else if (tc & BLK_TC_WRITE)
                rwbs[i++] = 'W';
        else if (t->bytes)
                rwbs[i++] = 'R';
        else
                rwbs[i++] = 'N';
 
-       if (t->action & BLK_TC_AHEAD)
+       if (tc & BLK_TC_FUA)
+               rwbs[i++] = 'F';
+       if (tc & BLK_TC_AHEAD)
                rwbs[i++] = 'A';
-       if (t->action & BLK_TC_BARRIER)
-               rwbs[i++] = 'B';
-       if (t->action & BLK_TC_SYNC)
+       if (tc & BLK_TC_SYNC)
                rwbs[i++] = 'S';
-       if (t->action & BLK_TC_META)
+       if (tc & BLK_TC_META)
                rwbs[i++] = 'M';
-
+out:
        rwbs[i] = '\0';
 }
 
@@ -955,6 +1080,16 @@ static inline const void *pdu_start(const struct trace_entry *ent)
        return te_blk_io_trace(ent) + 1;
 }
 
+static inline u32 t_action(const struct trace_entry *ent)
+{
+       return te_blk_io_trace(ent)->action;
+}
+
+static inline u32 t_bytes(const struct trace_entry *ent)
+{
+       return te_blk_io_trace(ent)->bytes;
+}
+
 static inline u32 t_sec(const struct trace_entry *ent)
 {
        return te_blk_io_trace(ent)->bytes >> 9;
@@ -967,7 +1102,7 @@ static inline unsigned long long t_sector(const struct trace_entry *ent)
 
 static inline __u16 t_error(const struct trace_entry *ent)
 {
-       return te_blk_io_trace(ent)->sector;
+       return te_blk_io_trace(ent)->error;
 }
 
 static __u64 get_pdu_int(const struct trace_entry *ent)
@@ -980,69 +1115,133 @@ static void get_pdu_remap(const struct trace_entry *ent,
                          struct blk_io_trace_remap *r)
 {
        const struct blk_io_trace_remap *__r = pdu_start(ent);
-       __u64 sector = __r->sector;
+       __u64 sector_from = __r->sector_from;
 
-       r->device = be32_to_cpu(__r->device);
        r->device_from = be32_to_cpu(__r->device_from);
-       r->sector = be64_to_cpu(sector);
+       r->device_to   = be32_to_cpu(__r->device_to);
+       r->sector_from = be64_to_cpu(sector_from);
 }
 
-static int blk_log_action_iter(struct trace_iterator *iter, const char *act)
+typedef int (blk_log_action_t) (struct trace_iterator *iter, const char *act);
+
+static int blk_log_action_classic(struct trace_iterator *iter, const char *act)
 {
-       char rwbs[6];
-       unsigned long long ts  = ns2usecs(iter->ts);
-       unsigned long usec_rem = do_div(ts, USEC_PER_SEC);
+       char rwbs[RWBS_LEN];
+       unsigned long long ts  = iter->ts;
+       unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
        unsigned secs          = (unsigned long)ts;
-       const struct trace_entry *ent = iter->ent;
-       const struct blk_io_trace *t = (const struct blk_io_trace *)ent;
+       const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
 
        fill_rwbs(rwbs, t);
 
        return trace_seq_printf(&iter->seq,
-                               "%3d,%-3d %2d %5d.%06lu %5u %2s %3s ",
+                               "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
                                MAJOR(t->device), MINOR(t->device), iter->cpu,
-                               secs, usec_rem, ent->pid, act, rwbs);
+                               secs, nsec_rem, iter->ent->pid, act, rwbs);
 }
 
-static int blk_log_action_seq(struct trace_seq *s, const struct blk_io_trace *t,
-                             const char *act)
+static int blk_log_action(struct trace_iterator *iter, const char *act)
 {
-       char rwbs[6];
+       char rwbs[RWBS_LEN];
+       const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
+
        fill_rwbs(rwbs, t);
-       return trace_seq_printf(s, "%3d,%-3d %2s %3s ",
+       return trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
                                MAJOR(t->device), MINOR(t->device), act, rwbs);
 }
 
+static int blk_log_dump_pdu(struct trace_seq *s, const struct trace_entry *ent)
+{
+       const unsigned char *pdu_buf;
+       int pdu_len;
+       int i, end, ret;
+
+       pdu_buf = pdu_start(ent);
+       pdu_len = te_blk_io_trace(ent)->pdu_len;
+
+       if (!pdu_len)
+               return 1;
+
+       /* find the last zero that needs to be printed */
+       for (end = pdu_len - 1; end >= 0; end--)
+               if (pdu_buf[end])
+                       break;
+       end++;
+
+       if (!trace_seq_putc(s, '('))
+               return 0;
+
+       for (i = 0; i < pdu_len; i++) {
+
+               ret = trace_seq_printf(s, "%s%02x",
+                                      i == 0 ? "" : " ", pdu_buf[i]);
+               if (!ret)
+                       return ret;
+
+               /*
+                * stop when the rest is just zeroes and indicate so
+                * with a ".." appended
+                */
+               if (i == end && end != pdu_len - 1)
+                       return trace_seq_puts(s, " ..) ");
+       }
+
+       return trace_seq_puts(s, ") ");
+}
+
 static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
 {
        char cmd[TASK_COMM_LEN];
 
        trace_find_cmdline(ent->pid, cmd);
 
-       if (t_sec(ent))
-               return trace_seq_printf(s, "%llu + %u [%s]\n",
-                                       t_sector(ent), t_sec(ent), cmd);
-       return trace_seq_printf(s, "[%s]\n", cmd);
+       if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
+               int ret;
+
+               ret = trace_seq_printf(s, "%u ", t_bytes(ent));
+               if (!ret)
+                       return 0;
+               ret = blk_log_dump_pdu(s, ent);
+               if (!ret)
+                       return 0;
+               return trace_seq_printf(s, "[%s]\n", cmd);
+       } else {
+               if (t_sec(ent))
+                       return trace_seq_printf(s, "%llu + %u [%s]\n",
+                                               t_sector(ent), t_sec(ent), cmd);
+               return trace_seq_printf(s, "[%s]\n", cmd);
+       }
 }
 
 static int blk_log_with_error(struct trace_seq *s,
                              const struct trace_entry *ent)
 {
-       if (t_sec(ent))
-               return trace_seq_printf(s, "%llu + %u [%d]\n", t_sector(ent),
-                                       t_sec(ent), t_error(ent));
-       return trace_seq_printf(s, "%llu [%d]\n", t_sector(ent), t_error(ent));
+       if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
+               int ret;
+
+               ret = blk_log_dump_pdu(s, ent);
+               if (ret)
+                       return trace_seq_printf(s, "[%d]\n", t_error(ent));
+               return 0;
+       } else {
+               if (t_sec(ent))
+                       return trace_seq_printf(s, "%llu + %u [%d]\n",
+                                               t_sector(ent),
+                                               t_sec(ent), t_error(ent));
+               return trace_seq_printf(s, "%llu [%d]\n",
+                                       t_sector(ent), t_error(ent));
+       }
 }
 
 static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
 {
-       struct blk_io_trace_remap r = { .device = 0, };
+       struct blk_io_trace_remap r = { .device_from = 0, };
 
        get_pdu_remap(ent, &r);
        return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
-                              t_sector(ent),
-                              t_sec(ent), MAJOR(r.device), MINOR(r.device),
-                              (unsigned long long)r.sector);
+                               t_sector(ent), t_sec(ent),
+                               MAJOR(r.device_from), MINOR(r.device_from),
+                               (unsigned long long)r.sector_from);
 }
 
 static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
@@ -1073,6 +1272,17 @@ static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
                                get_pdu_int(ent), cmd);
 }
 
+static int blk_log_msg(struct trace_seq *s, const struct trace_entry *ent)
+{
+       int ret;
+       const struct blk_io_trace *t = te_blk_io_trace(ent);
+
+       ret = trace_seq_putmem(s, t + 1, t->pdu_len);
+       if (ret)
+               return trace_seq_putc(s, '\n');
+       return ret;
+}
+
 /*
  * struct tracer operations
  */
@@ -1087,39 +1297,30 @@ static void blk_tracer_print_header(struct seq_file *m)
 
 static void blk_tracer_start(struct trace_array *tr)
 {
-       if (atomic_add_return(1, &blk_probes_ref) == 1)
-               blk_register_tracepoints();
-       trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
+       blk_tracer_enabled = true;
 }
 
 static int blk_tracer_init(struct trace_array *tr)
 {
        blk_tr = tr;
        blk_tracer_start(tr);
-       blk_tracer_enabled = true;
        return 0;
 }
 
 static void blk_tracer_stop(struct trace_array *tr)
 {
-       trace_flags |= TRACE_ITER_CONTEXT_INFO;
-       if (atomic_dec_and_test(&blk_probes_ref))
-               blk_unregister_tracepoints();
+       blk_tracer_enabled = false;
 }
 
 static void blk_tracer_reset(struct trace_array *tr)
 {
-       if (!atomic_read(&blk_probes_ref))
-               return;
-
-       blk_tracer_enabled = false;
        blk_tracer_stop(tr);
 }
 
-static struct {
+static const struct {
        const char *act[2];
        int        (*print)(struct trace_seq *s, const struct trace_entry *ent);
-} what2act[] __read_mostly = {
+} what2act[] = {
        [__BLK_TA_QUEUE]        = {{  "Q", "queue" },      blk_log_generic },
        [__BLK_TA_BACKMERGE]    = {{  "M", "backmerge" },  blk_log_generic },
        [__BLK_TA_FRONTMERGE]   = {{  "F", "frontmerge" }, blk_log_generic },
@@ -1137,29 +1338,45 @@ static struct {
        [__BLK_TA_REMAP]        = {{  "A", "remap" },      blk_log_remap },
 };
 
-static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
-                                              int flags)
+static enum print_line_t print_one_line(struct trace_iterator *iter,
+                                       bool classic)
 {
        struct trace_seq *s = &iter->seq;
-       const struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
-       const u16 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
+       const struct blk_io_trace *t;
+       u16 what;
        int ret;
+       bool long_act;
+       blk_log_action_t *log_action;
 
-       if (!trace_print_context(iter))
-               return TRACE_TYPE_PARTIAL_LINE;
+       t          = te_blk_io_trace(iter->ent);
+       what       = t->action & ((1 << BLK_TC_SHIFT) - 1);
+       long_act   = !!(trace_flags & TRACE_ITER_VERBOSE);
+       log_action = classic ? &blk_log_action_classic : &blk_log_action;
 
-       if (unlikely(what == 0 || what > ARRAY_SIZE(what2act)))
-               ret = trace_seq_printf(s, "Bad pc action %x\n", what);
+       if (t->action == BLK_TN_MESSAGE) {
+               ret = log_action(iter, long_act ? "message" : "m");
+               if (ret)
+                       ret = blk_log_msg(s, iter->ent);
+               goto out;
+       }
+
+       if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
+               ret = trace_seq_printf(s, "Unknown action %x\n", what);
        else {
-               const bool long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
-               ret = blk_log_action_seq(s, t, what2act[what].act[long_act]);
+               ret = log_action(iter, what2act[what].act[long_act]);
                if (ret)
                        ret = what2act[what].print(s, iter->ent);
        }
-
+out:
        return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
 }
 
+static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
+                                              int flags, struct trace_event *event)
+{
+       return print_one_line(iter, false);
+}
+
 static int blk_trace_synthesize_old_trace(struct trace_iterator *iter)
 {
        struct trace_seq *s = &iter->seq;
@@ -1167,7 +1384,7 @@ static int blk_trace_synthesize_old_trace(struct trace_iterator *iter)
        const int offset = offsetof(struct blk_io_trace, sector);
        struct blk_io_trace old = {
                .magic    = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
-               .time     = ns2usecs(iter->ts),
+               .time     = iter->ts,
        };
 
        if (!trace_seq_putmem(s, &old, offset))
@@ -1177,7 +1394,8 @@ static int blk_trace_synthesize_old_trace(struct trace_iterator *iter)
 }
 
 static enum print_line_t
-blk_trace_event_print_binary(struct trace_iterator *iter, int flags)
+blk_trace_event_print_binary(struct trace_iterator *iter, int flags,
+                            struct trace_event *event)
 {
        return blk_trace_synthesize_old_trace(iter) ?
                        TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
@@ -1185,26 +1403,22 @@ blk_trace_event_print_binary(struct trace_iterator *iter, int flags)
 
 static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
 {
-       const struct blk_io_trace *t;
-       u16 what;
-       int ret;
-
        if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
                return TRACE_TYPE_UNHANDLED;
 
-       t = (const struct blk_io_trace *)iter->ent;
-       what = t->action & ((1 << BLK_TC_SHIFT) - 1);
+       return print_one_line(iter, true);
+}
 
-       if (unlikely(what == 0 || what > ARRAY_SIZE(what2act)))
-               ret = trace_seq_printf(&iter->seq, "Bad pc action %x\n", what);
-       else {
-               const bool long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
-               ret = blk_log_action_iter(iter, what2act[what].act[long_act]);
-               if (ret)
-                       ret = what2act[what].print(&iter->seq, iter->ent);
+static int blk_tracer_set_flag(u32 old_flags, u32 bit, int set)
+{
+       /* don't output context-info for blk_classic output */
+       if (bit == TRACE_BLK_OPT_CLASSIC) {
+               if (set)
+                       trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
+               else
+                       trace_flags |= TRACE_ITER_CONTEXT_INFO;
        }
-
-       return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
+       return 0;
 }
 
 static struct tracer blk_tracer __read_mostly = {
@@ -1216,14 +1430,19 @@ static struct tracer blk_tracer __read_mostly = {
        .print_header   = blk_tracer_print_header,
        .print_line     = blk_tracer_print_line,
        .flags          = &blk_tracer_flags,
+       .set_flag       = blk_tracer_set_flag,
 };
 
-static struct trace_event trace_blk_event = {
-       .type           = TRACE_BLK,
+static struct trace_event_functions trace_blk_event_funcs = {
        .trace          = blk_trace_event_print,
        .binary         = blk_trace_event_print_binary,
 };
 
+static struct trace_event trace_blk_event = {
+       .type           = TRACE_BLK,
+       .funcs          = &trace_blk_event_funcs,
+};
+
 static int __init init_blk_tracer(void)
 {
        if (!register_ftrace_event(&trace_blk_event)) {
@@ -1250,36 +1469,48 @@ static int blk_trace_remove_queue(struct request_queue *q)
        if (bt == NULL)
                return -EINVAL;
 
-       kfree(bt);
+       if (atomic_dec_and_test(&blk_probes_ref))
+               blk_unregister_tracepoints();
+
+       blk_trace_free(bt);
        return 0;
 }
 
 /*
  * Setup everything required to start tracing
  */
-static int blk_trace_setup_queue(struct request_queue *q, dev_t dev)
+static int blk_trace_setup_queue(struct request_queue *q,
+                                struct block_device *bdev)
 {
        struct blk_trace *old_bt, *bt = NULL;
-       int ret;
+       int ret = -ENOMEM;
 
-       ret = -ENOMEM;
        bt = kzalloc(sizeof(*bt), GFP_KERNEL);
        if (!bt)
-               goto err;
+               return -ENOMEM;
 
-       bt->dev = dev;
+       bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
+       if (!bt->msg_data)
+               goto free_bt;
+
+       bt->dev = bdev->bd_dev;
        bt->act_mask = (u16)-1;
-       bt->end_lba = -1ULL;
-       bt->trace_state = Blktrace_running;
+
+       blk_trace_setup_lba(bt, bdev);
 
        old_bt = xchg(&q->blk_trace, bt);
        if (old_bt != NULL) {
                (void)xchg(&q->blk_trace, old_bt);
-               kfree(bt);
                ret = -EBUSY;
+               goto free_bt;
        }
+
+       if (atomic_inc_return(&blk_probes_ref) == 1)
+               blk_register_tracepoints();
        return 0;
-err:
+
+free_bt:
+       blk_trace_free(bt);
        return ret;
 }
 
@@ -1287,72 +1518,6 @@ err:
  * sysfs interface to enable and configure tracing
  */
 
-static ssize_t sysfs_blk_trace_enable_show(struct device *dev,
-                                          struct device_attribute *attr,
-                                          char *buf)
-{
-       struct hd_struct *p = dev_to_part(dev);
-       struct block_device *bdev;
-       ssize_t ret = -ENXIO;
-
-       lock_kernel();
-       bdev = bdget(part_devt(p));
-       if (bdev != NULL) {
-               struct request_queue *q = bdev_get_queue(bdev);
-
-               if (q != NULL) {
-                       mutex_lock(&bdev->bd_mutex);
-                       ret = sprintf(buf, "%u\n", !!q->blk_trace);
-                       mutex_unlock(&bdev->bd_mutex);
-               }
-
-               bdput(bdev);
-       }
-
-       unlock_kernel();
-       return ret;
-}
-
-static ssize_t sysfs_blk_trace_enable_store(struct device *dev,
-                                           struct device_attribute *attr,
-                                           const char *buf, size_t count)
-{
-       struct block_device *bdev;
-       struct request_queue *q;
-       struct hd_struct *p;
-       int value;
-       ssize_t ret = -ENXIO;
-
-       if (count == 0 || sscanf(buf, "%d", &value) != 1)
-               goto out;
-
-       lock_kernel();
-       p = dev_to_part(dev);
-       bdev = bdget(part_devt(p));
-       if (bdev == NULL)
-               goto out_unlock_kernel;
-
-       q = bdev_get_queue(bdev);
-       if (q == NULL)
-               goto out_bdput;
-
-       mutex_lock(&bdev->bd_mutex);
-       if (value)
-               ret = blk_trace_setup_queue(q, bdev->bd_dev);
-       else
-               ret = blk_trace_remove_queue(q);
-       mutex_unlock(&bdev->bd_mutex);
-
-       if (ret == 0)
-               ret = count;
-out_bdput:
-       bdput(bdev);
-out_unlock_kernel:
-       unlock_kernel();
-out:
-       return ret;
-}
-
 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
                                         struct device_attribute *attr,
                                         char *buf);
@@ -1364,8 +1529,7 @@ static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
                    sysfs_blk_trace_attr_show, \
                    sysfs_blk_trace_attr_store)
 
-static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
-                  sysfs_blk_trace_enable_show, sysfs_blk_trace_enable_store);
+static BLK_TRACE_DEVICE_ATTR(enable);
 static BLK_TRACE_DEVICE_ATTR(act_mask);
 static BLK_TRACE_DEVICE_ATTR(pid);
 static BLK_TRACE_DEVICE_ATTR(start_lba);
@@ -1385,53 +1549,86 @@ struct attribute_group blk_trace_attr_group = {
        .attrs = blk_trace_attrs,
 };
 
-static int blk_str2act_mask(const char *str)
+static const struct {
+       int mask;
+       const char *str;
+} mask_maps[] = {
+       { BLK_TC_READ,          "read"          },
+       { BLK_TC_WRITE,         "write"         },
+       { BLK_TC_FLUSH,         "flush"         },
+       { BLK_TC_SYNC,          "sync"          },
+       { BLK_TC_QUEUE,         "queue"         },
+       { BLK_TC_REQUEUE,       "requeue"       },
+       { BLK_TC_ISSUE,         "issue"         },
+       { BLK_TC_COMPLETE,      "complete"      },
+       { BLK_TC_FS,            "fs"            },
+       { BLK_TC_PC,            "pc"            },
+       { BLK_TC_AHEAD,         "ahead"         },
+       { BLK_TC_META,          "meta"          },
+       { BLK_TC_DISCARD,       "discard"       },
+       { BLK_TC_DRV_DATA,      "drv_data"      },
+       { BLK_TC_FUA,           "fua"           },
+};
+
+static int blk_trace_str2mask(const char *str)
 {
+       int i;
        int mask = 0;
-       char *copy = kstrdup(str, GFP_KERNEL), *s;
+       char *buf, *s, *token;
 
-       if (copy == NULL)
+       buf = kstrdup(str, GFP_KERNEL);
+       if (buf == NULL)
                return -ENOMEM;
-
-       s = strstrip(copy);
+       s = strstrip(buf);
 
        while (1) {
-               char *sep = strchr(s, ',');
-
-               if (sep != NULL)
-                       *sep = '\0';
-
-               if (strcasecmp(s, "barrier") == 0)
-                       mask |= BLK_TC_BARRIER;
-               else if (strcasecmp(s, "complete") == 0)
-                       mask |= BLK_TC_COMPLETE;
-               else if (strcasecmp(s, "fs") == 0)
-                       mask |= BLK_TC_FS;
-               else if (strcasecmp(s, "issue") == 0)
-                       mask |= BLK_TC_ISSUE;
-               else if (strcasecmp(s, "pc") == 0)
-                       mask |= BLK_TC_PC;
-               else if (strcasecmp(s, "queue") == 0)
-                       mask |= BLK_TC_QUEUE;
-               else if (strcasecmp(s, "read") == 0)
-                       mask |= BLK_TC_READ;
-               else if (strcasecmp(s, "requeue") == 0)
-                       mask |= BLK_TC_REQUEUE;
-               else if (strcasecmp(s, "sync") == 0)
-                       mask |= BLK_TC_SYNC;
-               else if (strcasecmp(s, "write") == 0)
-                       mask |= BLK_TC_WRITE;
-
-               if (sep == NULL)
+               token = strsep(&s, ",");
+               if (token == NULL)
                        break;
 
-               s = sep + 1;
+               if (*token == '\0')
+                       continue;
+
+               for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
+                       if (strcasecmp(token, mask_maps[i].str) == 0) {
+                               mask |= mask_maps[i].mask;
+                               break;
+                       }
+               }
+               if (i == ARRAY_SIZE(mask_maps)) {
+                       mask = -EINVAL;
+                       break;
+               }
        }
-       kfree(copy);
+       kfree(buf);
 
        return mask;
 }
 
+static ssize_t blk_trace_mask2str(char *buf, int mask)
+{
+       int i;
+       char *p = buf;
+
+       for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
+               if (mask & mask_maps[i].mask) {
+                       p += sprintf(p, "%s%s",
+                                   (p == buf) ? "" : ",", mask_maps[i].str);
+               }
+       }
+       *p++ = '\n';
+
+       return p - buf;
+}
+
+static struct request_queue *blk_trace_get_queue(struct block_device *bdev)
+{
+       if (bdev->bd_disk == NULL)
+               return NULL;
+
+       return bdev_get_queue(bdev);
+}
+
 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
                                         struct device_attribute *attr,
                                         char *buf)
@@ -1441,30 +1638,37 @@ static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
        struct block_device *bdev;
        ssize_t ret = -ENXIO;
 
-       lock_kernel();
        bdev = bdget(part_devt(p));
        if (bdev == NULL)
-               goto out_unlock_kernel;
+               goto out;
 
-       q = bdev_get_queue(bdev);
+       q = blk_trace_get_queue(bdev);
        if (q == NULL)
                goto out_bdput;
+
        mutex_lock(&bdev->bd_mutex);
+
+       if (attr == &dev_attr_enable) {
+               ret = sprintf(buf, "%u\n", !!q->blk_trace);
+               goto out_unlock_bdev;
+       }
+
        if (q->blk_trace == NULL)
                ret = sprintf(buf, "disabled\n");
        else if (attr == &dev_attr_act_mask)
-               ret = sprintf(buf, "%#x\n", q->blk_trace->act_mask);
+               ret = blk_trace_mask2str(buf, q->blk_trace->act_mask);
        else if (attr == &dev_attr_pid)
                ret = sprintf(buf, "%u\n", q->blk_trace->pid);
        else if (attr == &dev_attr_start_lba)
                ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
        else if (attr == &dev_attr_end_lba)
                ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
+
+out_unlock_bdev:
        mutex_unlock(&bdev->bd_mutex);
 out_bdput:
        bdput(bdev);
-out_unlock_kernel:
-       unlock_kernel();
+out:
        return ret;
 }
 
@@ -1476,7 +1680,7 @@ static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
        struct request_queue *q;
        struct hd_struct *p;
        u64 value;
-       ssize_t ret = -ENXIO;
+       ssize_t ret = -EINVAL;
 
        if (count == 0)
                goto out;
@@ -1484,27 +1688,38 @@ static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
        if (attr == &dev_attr_act_mask) {
                if (sscanf(buf, "%llx", &value) != 1) {
                        /* Assume it is a list of trace category names */
-                       value = blk_str2act_mask(buf);
-                       if (value < 0)
+                       ret = blk_trace_str2mask(buf);
+                       if (ret < 0)
                                goto out;
+                       value = ret;
                }
        } else if (sscanf(buf, "%llu", &value) != 1)
                goto out;
 
-       lock_kernel();
+       ret = -ENXIO;
+
        p = dev_to_part(dev);
        bdev = bdget(part_devt(p));
        if (bdev == NULL)
-               goto out_unlock_kernel;
+               goto out;
 
-       q = bdev_get_queue(bdev);
+       q = blk_trace_get_queue(bdev);
        if (q == NULL)
                goto out_bdput;
 
        mutex_lock(&bdev->bd_mutex);
+
+       if (attr == &dev_attr_enable) {
+               if (value)
+                       ret = blk_trace_setup_queue(q, bdev);
+               else
+                       ret = blk_trace_remove_queue(q);
+               goto out_unlock_bdev;
+       }
+
        ret = 0;
        if (q->blk_trace == NULL)
-               ret = blk_trace_setup_queue(q, bdev->bd_dev);
+               ret = blk_trace_setup_queue(q, bdev);
 
        if (ret == 0) {
                if (attr == &dev_attr_act_mask)
@@ -1515,13 +1730,85 @@ static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
                        q->blk_trace->start_lba = value;
                else if (attr == &dev_attr_end_lba)
                        q->blk_trace->end_lba = value;
-               ret = count;
        }
+
+out_unlock_bdev:
        mutex_unlock(&bdev->bd_mutex);
 out_bdput:
        bdput(bdev);
-out_unlock_kernel:
-       unlock_kernel();
 out:
-       return ret;
+       return ret ? ret : count;
+}
+
+int blk_trace_init_sysfs(struct device *dev)
+{
+       return sysfs_create_group(&dev->kobj, &blk_trace_attr_group);
 }
+
+void blk_trace_remove_sysfs(struct device *dev)
+{
+       sysfs_remove_group(&dev->kobj, &blk_trace_attr_group);
+}
+
+#endif /* CONFIG_BLK_DEV_IO_TRACE */
+
+#ifdef CONFIG_EVENT_TRACING
+
+void blk_dump_cmd(char *buf, struct request *rq)
+{
+       int i, end;
+       int len = rq->cmd_len;
+       unsigned char *cmd = rq->cmd;
+
+       if (rq->cmd_type != REQ_TYPE_BLOCK_PC) {
+               buf[0] = '\0';
+               return;
+       }
+
+       for (end = len - 1; end >= 0; end--)
+               if (cmd[end])
+                       break;
+       end++;
+
+       for (i = 0; i < len; i++) {
+               buf += sprintf(buf, "%s%02x", i == 0 ? "" : " ", cmd[i]);
+               if (i == end && end != len - 1) {
+                       sprintf(buf, " ..");
+                       break;
+               }
+       }
+}
+
+void blk_fill_rwbs(char *rwbs, u32 rw, int bytes)
+{
+       int i = 0;
+
+       if (rw & REQ_FLUSH)
+               rwbs[i++] = 'F';
+
+       if (rw & WRITE)
+               rwbs[i++] = 'W';
+       else if (rw & REQ_DISCARD)
+               rwbs[i++] = 'D';
+       else if (bytes)
+               rwbs[i++] = 'R';
+       else
+               rwbs[i++] = 'N';
+
+       if (rw & REQ_FUA)
+               rwbs[i++] = 'F';
+       if (rw & REQ_RAHEAD)
+               rwbs[i++] = 'A';
+       if (rw & REQ_SYNC)
+               rwbs[i++] = 'S';
+       if (rw & REQ_META)
+               rwbs[i++] = 'M';
+       if (rw & REQ_SECURE)
+               rwbs[i++] = 'E';
+
+       rwbs[i] = '\0';
+}
+EXPORT_SYMBOL_GPL(blk_fill_rwbs);
+
+#endif /* CONFIG_EVENT_TRACING */
+