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