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