drm/i915: Fix typo that broke SVID1 in intel_sdvo_multifunc_encoder()
[linux-2.6/linux-2.6-openrd.git] / kernel / trace / blktrace.c
blob7a34cb563fec4cef90ebbf1d6bcc71dd4af4fef4
1 /*
2 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
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.
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.
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
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/smp_lock.h>
26 #include <linux/time.h>
27 #include <linux/uaccess.h>
29 #include <trace/events/block.h>
31 #include "trace_output.h"
33 #ifdef CONFIG_BLK_DEV_IO_TRACE
35 static unsigned int blktrace_seq __read_mostly = 1;
37 static struct trace_array *blk_tr;
38 static bool blk_tracer_enabled __read_mostly;
40 /* Select an alternative, minimalistic output than the original one */
41 #define TRACE_BLK_OPT_CLASSIC 0x1
43 static struct tracer_opt blk_tracer_opts[] = {
44 /* Default disable the minimalistic output */
45 { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },
46 { }
49 static struct tracer_flags blk_tracer_flags = {
50 .val = 0,
51 .opts = blk_tracer_opts,
54 /* Global reference count of probes */
55 static atomic_t blk_probes_ref = ATOMIC_INIT(0);
57 static void blk_register_tracepoints(void);
58 static void blk_unregister_tracepoints(void);
61 * Send out a notify message.
63 static void trace_note(struct blk_trace *bt, pid_t pid, int action,
64 const void *data, size_t len)
66 struct blk_io_trace *t;
67 struct ring_buffer_event *event = NULL;
68 int pc = 0;
69 int cpu = smp_processor_id();
70 bool blk_tracer = blk_tracer_enabled;
72 if (blk_tracer) {
73 pc = preempt_count();
74 event = trace_buffer_lock_reserve(blk_tr, TRACE_BLK,
75 sizeof(*t) + len,
76 0, pc);
77 if (!event)
78 return;
79 t = ring_buffer_event_data(event);
80 goto record_it;
83 if (!bt->rchan)
84 return;
86 t = relay_reserve(bt->rchan, sizeof(*t) + len);
87 if (t) {
88 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
89 t->time = ktime_to_ns(ktime_get());
90 record_it:
91 t->device = bt->dev;
92 t->action = action;
93 t->pid = pid;
94 t->cpu = cpu;
95 t->pdu_len = len;
96 memcpy((void *) t + sizeof(*t), data, len);
98 if (blk_tracer)
99 trace_buffer_unlock_commit(blk_tr, event, 0, pc);
104 * Send out a notify for this process, if we haven't done so since a trace
105 * started
107 static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk)
109 tsk->btrace_seq = blktrace_seq;
110 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm));
113 static void trace_note_time(struct blk_trace *bt)
115 struct timespec now;
116 unsigned long flags;
117 u32 words[2];
119 getnstimeofday(&now);
120 words[0] = now.tv_sec;
121 words[1] = now.tv_nsec;
123 local_irq_save(flags);
124 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
125 local_irq_restore(flags);
128 void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
130 int n;
131 va_list args;
132 unsigned long flags;
133 char *buf;
135 if (unlikely(bt->trace_state != Blktrace_running &&
136 !blk_tracer_enabled))
137 return;
139 local_irq_save(flags);
140 buf = per_cpu_ptr(bt->msg_data, smp_processor_id());
141 va_start(args, fmt);
142 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
143 va_end(args);
145 trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
146 local_irq_restore(flags);
148 EXPORT_SYMBOL_GPL(__trace_note_message);
150 static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
151 pid_t pid)
153 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
154 return 1;
155 if (sector && (sector < bt->start_lba || sector > bt->end_lba))
156 return 1;
157 if (bt->pid && pid != bt->pid)
158 return 1;
160 return 0;
164 * Data direction bit lookup
166 static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ),
167 BLK_TC_ACT(BLK_TC_WRITE) };
169 /* The ilog2() calls fall out because they're constant */
170 #define MASK_TC_BIT(rw, __name) ((rw & (1 << BIO_RW_ ## __name)) << \
171 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name))
174 * The worker for the various blk_add_trace*() types. Fills out a
175 * blk_io_trace structure and places it in a per-cpu subbuffer.
177 static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
178 int rw, u32 what, int error, int pdu_len, void *pdu_data)
180 struct task_struct *tsk = current;
181 struct ring_buffer_event *event = NULL;
182 struct blk_io_trace *t;
183 unsigned long flags = 0;
184 unsigned long *sequence;
185 pid_t pid;
186 int cpu, pc = 0;
187 bool blk_tracer = blk_tracer_enabled;
189 if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer))
190 return;
192 what |= ddir_act[rw & WRITE];
193 what |= MASK_TC_BIT(rw, BARRIER);
194 what |= MASK_TC_BIT(rw, SYNCIO);
195 what |= MASK_TC_BIT(rw, AHEAD);
196 what |= MASK_TC_BIT(rw, META);
197 what |= MASK_TC_BIT(rw, DISCARD);
199 pid = tsk->pid;
200 if (act_log_check(bt, what, sector, pid))
201 return;
202 cpu = raw_smp_processor_id();
204 if (blk_tracer) {
205 tracing_record_cmdline(current);
207 pc = preempt_count();
208 event = trace_buffer_lock_reserve(blk_tr, TRACE_BLK,
209 sizeof(*t) + pdu_len,
210 0, pc);
211 if (!event)
212 return;
213 t = ring_buffer_event_data(event);
214 goto record_it;
218 * A word about the locking here - we disable interrupts to reserve
219 * some space in the relay per-cpu buffer, to prevent an irq
220 * from coming in and stepping on our toes.
222 local_irq_save(flags);
224 if (unlikely(tsk->btrace_seq != blktrace_seq))
225 trace_note_tsk(bt, tsk);
227 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
228 if (t) {
229 sequence = per_cpu_ptr(bt->sequence, cpu);
231 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
232 t->sequence = ++(*sequence);
233 t->time = ktime_to_ns(ktime_get());
234 record_it:
236 * These two are not needed in ftrace as they are in the
237 * generic trace_entry, filled by tracing_generic_entry_update,
238 * but for the trace_event->bin() synthesizer benefit we do it
239 * here too.
241 t->cpu = cpu;
242 t->pid = pid;
244 t->sector = sector;
245 t->bytes = bytes;
246 t->action = what;
247 t->device = bt->dev;
248 t->error = error;
249 t->pdu_len = pdu_len;
251 if (pdu_len)
252 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
254 if (blk_tracer) {
255 trace_buffer_unlock_commit(blk_tr, event, 0, pc);
256 return;
260 local_irq_restore(flags);
263 static struct dentry *blk_tree_root;
264 static DEFINE_MUTEX(blk_tree_mutex);
266 static void blk_trace_free(struct blk_trace *bt)
268 debugfs_remove(bt->msg_file);
269 debugfs_remove(bt->dropped_file);
270 relay_close(bt->rchan);
271 debugfs_remove(bt->dir);
272 free_percpu(bt->sequence);
273 free_percpu(bt->msg_data);
274 kfree(bt);
277 static void blk_trace_cleanup(struct blk_trace *bt)
279 blk_trace_free(bt);
280 if (atomic_dec_and_test(&blk_probes_ref))
281 blk_unregister_tracepoints();
284 int blk_trace_remove(struct request_queue *q)
286 struct blk_trace *bt;
288 bt = xchg(&q->blk_trace, NULL);
289 if (!bt)
290 return -EINVAL;
292 if (bt->trace_state != Blktrace_running)
293 blk_trace_cleanup(bt);
295 return 0;
297 EXPORT_SYMBOL_GPL(blk_trace_remove);
299 static int blk_dropped_open(struct inode *inode, struct file *filp)
301 filp->private_data = inode->i_private;
303 return 0;
306 static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
307 size_t count, loff_t *ppos)
309 struct blk_trace *bt = filp->private_data;
310 char buf[16];
312 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
314 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
317 static const struct file_operations blk_dropped_fops = {
318 .owner = THIS_MODULE,
319 .open = blk_dropped_open,
320 .read = blk_dropped_read,
323 static int blk_msg_open(struct inode *inode, struct file *filp)
325 filp->private_data = inode->i_private;
327 return 0;
330 static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
331 size_t count, loff_t *ppos)
333 char *msg;
334 struct blk_trace *bt;
336 if (count >= BLK_TN_MAX_MSG)
337 return -EINVAL;
339 msg = kmalloc(count + 1, GFP_KERNEL);
340 if (msg == NULL)
341 return -ENOMEM;
343 if (copy_from_user(msg, buffer, count)) {
344 kfree(msg);
345 return -EFAULT;
348 msg[count] = '\0';
349 bt = filp->private_data;
350 __trace_note_message(bt, "%s", msg);
351 kfree(msg);
353 return count;
356 static const struct file_operations blk_msg_fops = {
357 .owner = THIS_MODULE,
358 .open = blk_msg_open,
359 .write = blk_msg_write,
363 * Keep track of how many times we encountered a full subbuffer, to aid
364 * the user space app in telling how many lost events there were.
366 static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
367 void *prev_subbuf, size_t prev_padding)
369 struct blk_trace *bt;
371 if (!relay_buf_full(buf))
372 return 1;
374 bt = buf->chan->private_data;
375 atomic_inc(&bt->dropped);
376 return 0;
379 static int blk_remove_buf_file_callback(struct dentry *dentry)
381 debugfs_remove(dentry);
383 return 0;
386 static struct dentry *blk_create_buf_file_callback(const char *filename,
387 struct dentry *parent,
388 int mode,
389 struct rchan_buf *buf,
390 int *is_global)
392 return debugfs_create_file(filename, mode, parent, buf,
393 &relay_file_operations);
396 static struct rchan_callbacks blk_relay_callbacks = {
397 .subbuf_start = blk_subbuf_start_callback,
398 .create_buf_file = blk_create_buf_file_callback,
399 .remove_buf_file = blk_remove_buf_file_callback,
402 static void blk_trace_setup_lba(struct blk_trace *bt,
403 struct block_device *bdev)
405 struct hd_struct *part = NULL;
407 if (bdev)
408 part = bdev->bd_part;
410 if (part) {
411 bt->start_lba = part->start_sect;
412 bt->end_lba = part->start_sect + part->nr_sects;
413 } else {
414 bt->start_lba = 0;
415 bt->end_lba = -1ULL;
420 * Setup everything required to start tracing
422 int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
423 struct block_device *bdev,
424 struct blk_user_trace_setup *buts)
426 struct blk_trace *old_bt, *bt = NULL;
427 struct dentry *dir = NULL;
428 int ret, i;
430 if (!buts->buf_size || !buts->buf_nr)
431 return -EINVAL;
433 strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
434 buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
437 * some device names have larger paths - convert the slashes
438 * to underscores for this to work as expected
440 for (i = 0; i < strlen(buts->name); i++)
441 if (buts->name[i] == '/')
442 buts->name[i] = '_';
444 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
445 if (!bt)
446 return -ENOMEM;
448 ret = -ENOMEM;
449 bt->sequence = alloc_percpu(unsigned long);
450 if (!bt->sequence)
451 goto err;
453 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
454 if (!bt->msg_data)
455 goto err;
457 ret = -ENOENT;
459 mutex_lock(&blk_tree_mutex);
460 if (!blk_tree_root) {
461 blk_tree_root = debugfs_create_dir("block", NULL);
462 if (!blk_tree_root) {
463 mutex_unlock(&blk_tree_mutex);
464 goto err;
467 mutex_unlock(&blk_tree_mutex);
469 dir = debugfs_create_dir(buts->name, blk_tree_root);
471 if (!dir)
472 goto err;
474 bt->dir = dir;
475 bt->dev = dev;
476 atomic_set(&bt->dropped, 0);
478 ret = -EIO;
479 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt,
480 &blk_dropped_fops);
481 if (!bt->dropped_file)
482 goto err;
484 bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
485 if (!bt->msg_file)
486 goto err;
488 bt->rchan = relay_open("trace", dir, buts->buf_size,
489 buts->buf_nr, &blk_relay_callbacks, bt);
490 if (!bt->rchan)
491 goto err;
493 bt->act_mask = buts->act_mask;
494 if (!bt->act_mask)
495 bt->act_mask = (u16) -1;
497 blk_trace_setup_lba(bt, bdev);
499 /* overwrite with user settings */
500 if (buts->start_lba)
501 bt->start_lba = buts->start_lba;
502 if (buts->end_lba)
503 bt->end_lba = buts->end_lba;
505 bt->pid = buts->pid;
506 bt->trace_state = Blktrace_setup;
508 ret = -EBUSY;
509 old_bt = xchg(&q->blk_trace, bt);
510 if (old_bt) {
511 (void) xchg(&q->blk_trace, old_bt);
512 goto err;
515 if (atomic_inc_return(&blk_probes_ref) == 1)
516 blk_register_tracepoints();
518 return 0;
519 err:
520 blk_trace_free(bt);
521 return ret;
524 int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
525 struct block_device *bdev,
526 char __user *arg)
528 struct blk_user_trace_setup buts;
529 int ret;
531 ret = copy_from_user(&buts, arg, sizeof(buts));
532 if (ret)
533 return -EFAULT;
535 ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
536 if (ret)
537 return ret;
539 if (copy_to_user(arg, &buts, sizeof(buts)))
540 return -EFAULT;
542 return 0;
544 EXPORT_SYMBOL_GPL(blk_trace_setup);
546 int blk_trace_startstop(struct request_queue *q, int start)
548 int ret;
549 struct blk_trace *bt = q->blk_trace;
551 if (bt == NULL)
552 return -EINVAL;
555 * For starting a trace, we can transition from a setup or stopped
556 * trace. For stopping a trace, the state must be running
558 ret = -EINVAL;
559 if (start) {
560 if (bt->trace_state == Blktrace_setup ||
561 bt->trace_state == Blktrace_stopped) {
562 blktrace_seq++;
563 smp_mb();
564 bt->trace_state = Blktrace_running;
566 trace_note_time(bt);
567 ret = 0;
569 } else {
570 if (bt->trace_state == Blktrace_running) {
571 bt->trace_state = Blktrace_stopped;
572 relay_flush(bt->rchan);
573 ret = 0;
577 return ret;
579 EXPORT_SYMBOL_GPL(blk_trace_startstop);
582 * blk_trace_ioctl: - handle the ioctls associated with tracing
583 * @bdev: the block device
584 * @cmd: the ioctl cmd
585 * @arg: the argument data, if any
588 int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
590 struct request_queue *q;
591 int ret, start = 0;
592 char b[BDEVNAME_SIZE];
594 q = bdev_get_queue(bdev);
595 if (!q)
596 return -ENXIO;
598 mutex_lock(&bdev->bd_mutex);
600 switch (cmd) {
601 case BLKTRACESETUP:
602 bdevname(bdev, b);
603 ret = blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
604 break;
605 case BLKTRACESTART:
606 start = 1;
607 case BLKTRACESTOP:
608 ret = blk_trace_startstop(q, start);
609 break;
610 case BLKTRACETEARDOWN:
611 ret = blk_trace_remove(q);
612 break;
613 default:
614 ret = -ENOTTY;
615 break;
618 mutex_unlock(&bdev->bd_mutex);
619 return ret;
623 * blk_trace_shutdown: - stop and cleanup trace structures
624 * @q: the request queue associated with the device
627 void blk_trace_shutdown(struct request_queue *q)
629 if (q->blk_trace) {
630 blk_trace_startstop(q, 0);
631 blk_trace_remove(q);
636 * blktrace probes
640 * blk_add_trace_rq - Add a trace for a request oriented action
641 * @q: queue the io is for
642 * @rq: the source request
643 * @what: the action
645 * Description:
646 * Records an action against a request. Will log the bio offset + size.
649 static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
650 u32 what)
652 struct blk_trace *bt = q->blk_trace;
653 int rw = rq->cmd_flags & 0x03;
655 if (likely(!bt))
656 return;
658 if (blk_discard_rq(rq))
659 rw |= (1 << BIO_RW_DISCARD);
661 if (blk_pc_request(rq)) {
662 what |= BLK_TC_ACT(BLK_TC_PC);
663 __blk_add_trace(bt, 0, blk_rq_bytes(rq), rw,
664 what, rq->errors, rq->cmd_len, rq->cmd);
665 } else {
666 what |= BLK_TC_ACT(BLK_TC_FS);
667 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), rw,
668 what, rq->errors, 0, NULL);
672 static void blk_add_trace_rq_abort(struct request_queue *q, struct request *rq)
674 blk_add_trace_rq(q, rq, BLK_TA_ABORT);
677 static void blk_add_trace_rq_insert(struct request_queue *q, struct request *rq)
679 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
682 static void blk_add_trace_rq_issue(struct request_queue *q, struct request *rq)
684 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
687 static void blk_add_trace_rq_requeue(struct request_queue *q,
688 struct request *rq)
690 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
693 static void blk_add_trace_rq_complete(struct request_queue *q,
694 struct request *rq)
696 blk_add_trace_rq(q, rq, BLK_TA_COMPLETE);
700 * blk_add_trace_bio - Add a trace for a bio oriented action
701 * @q: queue the io is for
702 * @bio: the source bio
703 * @what: the action
705 * Description:
706 * Records an action against a bio. Will log the bio offset + size.
709 static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
710 u32 what)
712 struct blk_trace *bt = q->blk_trace;
714 if (likely(!bt))
715 return;
717 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what,
718 !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
721 static void blk_add_trace_bio_bounce(struct request_queue *q, struct bio *bio)
723 blk_add_trace_bio(q, bio, BLK_TA_BOUNCE);
726 static void blk_add_trace_bio_complete(struct request_queue *q, struct bio *bio)
728 blk_add_trace_bio(q, bio, BLK_TA_COMPLETE);
731 static void blk_add_trace_bio_backmerge(struct request_queue *q,
732 struct bio *bio)
734 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
737 static void blk_add_trace_bio_frontmerge(struct request_queue *q,
738 struct bio *bio)
740 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
743 static void blk_add_trace_bio_queue(struct request_queue *q, struct bio *bio)
745 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
748 static void blk_add_trace_getrq(struct request_queue *q,
749 struct bio *bio, int rw)
751 if (bio)
752 blk_add_trace_bio(q, bio, BLK_TA_GETRQ);
753 else {
754 struct blk_trace *bt = q->blk_trace;
756 if (bt)
757 __blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL);
762 static void blk_add_trace_sleeprq(struct request_queue *q,
763 struct bio *bio, int rw)
765 if (bio)
766 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ);
767 else {
768 struct blk_trace *bt = q->blk_trace;
770 if (bt)
771 __blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ,
772 0, 0, NULL);
776 static void blk_add_trace_plug(struct request_queue *q)
778 struct blk_trace *bt = q->blk_trace;
780 if (bt)
781 __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
784 static void blk_add_trace_unplug_io(struct request_queue *q)
786 struct blk_trace *bt = q->blk_trace;
788 if (bt) {
789 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
790 __be64 rpdu = cpu_to_be64(pdu);
792 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_IO, 0,
793 sizeof(rpdu), &rpdu);
797 static void blk_add_trace_unplug_timer(struct request_queue *q)
799 struct blk_trace *bt = q->blk_trace;
801 if (bt) {
802 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
803 __be64 rpdu = cpu_to_be64(pdu);
805 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_TIMER, 0,
806 sizeof(rpdu), &rpdu);
810 static void blk_add_trace_split(struct request_queue *q, struct bio *bio,
811 unsigned int pdu)
813 struct blk_trace *bt = q->blk_trace;
815 if (bt) {
816 __be64 rpdu = cpu_to_be64(pdu);
818 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
819 BLK_TA_SPLIT, !bio_flagged(bio, BIO_UPTODATE),
820 sizeof(rpdu), &rpdu);
825 * blk_add_trace_remap - Add a trace for a remap operation
826 * @q: queue the io is for
827 * @bio: the source bio
828 * @dev: target device
829 * @from: source sector
831 * Description:
832 * Device mapper or raid target sometimes need to split a bio because
833 * it spans a stripe (or similar). Add a trace for that action.
836 static void blk_add_trace_remap(struct request_queue *q, struct bio *bio,
837 dev_t dev, sector_t from)
839 struct blk_trace *bt = q->blk_trace;
840 struct blk_io_trace_remap r;
842 if (likely(!bt))
843 return;
845 r.device_from = cpu_to_be32(dev);
846 r.device_to = cpu_to_be32(bio->bi_bdev->bd_dev);
847 r.sector_from = cpu_to_be64(from);
849 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
850 BLK_TA_REMAP, !bio_flagged(bio, BIO_UPTODATE),
851 sizeof(r), &r);
855 * blk_add_driver_data - Add binary message with driver-specific data
856 * @q: queue the io is for
857 * @rq: io request
858 * @data: driver-specific data
859 * @len: length of driver-specific data
861 * Description:
862 * Some drivers might want to write driver-specific data per request.
865 void blk_add_driver_data(struct request_queue *q,
866 struct request *rq,
867 void *data, size_t len)
869 struct blk_trace *bt = q->blk_trace;
871 if (likely(!bt))
872 return;
874 if (blk_pc_request(rq))
875 __blk_add_trace(bt, 0, blk_rq_bytes(rq), 0,
876 BLK_TA_DRV_DATA, rq->errors, len, data);
877 else
878 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), 0,
879 BLK_TA_DRV_DATA, rq->errors, len, data);
881 EXPORT_SYMBOL_GPL(blk_add_driver_data);
883 static void blk_register_tracepoints(void)
885 int ret;
887 ret = register_trace_block_rq_abort(blk_add_trace_rq_abort);
888 WARN_ON(ret);
889 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert);
890 WARN_ON(ret);
891 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue);
892 WARN_ON(ret);
893 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue);
894 WARN_ON(ret);
895 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete);
896 WARN_ON(ret);
897 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce);
898 WARN_ON(ret);
899 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete);
900 WARN_ON(ret);
901 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
902 WARN_ON(ret);
903 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
904 WARN_ON(ret);
905 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue);
906 WARN_ON(ret);
907 ret = register_trace_block_getrq(blk_add_trace_getrq);
908 WARN_ON(ret);
909 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq);
910 WARN_ON(ret);
911 ret = register_trace_block_plug(blk_add_trace_plug);
912 WARN_ON(ret);
913 ret = register_trace_block_unplug_timer(blk_add_trace_unplug_timer);
914 WARN_ON(ret);
915 ret = register_trace_block_unplug_io(blk_add_trace_unplug_io);
916 WARN_ON(ret);
917 ret = register_trace_block_split(blk_add_trace_split);
918 WARN_ON(ret);
919 ret = register_trace_block_remap(blk_add_trace_remap);
920 WARN_ON(ret);
923 static void blk_unregister_tracepoints(void)
925 unregister_trace_block_remap(blk_add_trace_remap);
926 unregister_trace_block_split(blk_add_trace_split);
927 unregister_trace_block_unplug_io(blk_add_trace_unplug_io);
928 unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer);
929 unregister_trace_block_plug(blk_add_trace_plug);
930 unregister_trace_block_sleeprq(blk_add_trace_sleeprq);
931 unregister_trace_block_getrq(blk_add_trace_getrq);
932 unregister_trace_block_bio_queue(blk_add_trace_bio_queue);
933 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
934 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
935 unregister_trace_block_bio_complete(blk_add_trace_bio_complete);
936 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce);
937 unregister_trace_block_rq_complete(blk_add_trace_rq_complete);
938 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue);
939 unregister_trace_block_rq_issue(blk_add_trace_rq_issue);
940 unregister_trace_block_rq_insert(blk_add_trace_rq_insert);
941 unregister_trace_block_rq_abort(blk_add_trace_rq_abort);
943 tracepoint_synchronize_unregister();
947 * struct blk_io_tracer formatting routines
950 static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
952 int i = 0;
953 int tc = t->action >> BLK_TC_SHIFT;
955 if (t->action == BLK_TN_MESSAGE) {
956 rwbs[i++] = 'N';
957 goto out;
960 if (tc & BLK_TC_DISCARD)
961 rwbs[i++] = 'D';
962 else if (tc & BLK_TC_WRITE)
963 rwbs[i++] = 'W';
964 else if (t->bytes)
965 rwbs[i++] = 'R';
966 else
967 rwbs[i++] = 'N';
969 if (tc & BLK_TC_AHEAD)
970 rwbs[i++] = 'A';
971 if (tc & BLK_TC_BARRIER)
972 rwbs[i++] = 'B';
973 if (tc & BLK_TC_SYNC)
974 rwbs[i++] = 'S';
975 if (tc & BLK_TC_META)
976 rwbs[i++] = 'M';
977 out:
978 rwbs[i] = '\0';
981 static inline
982 const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
984 return (const struct blk_io_trace *)ent;
987 static inline const void *pdu_start(const struct trace_entry *ent)
989 return te_blk_io_trace(ent) + 1;
992 static inline u32 t_action(const struct trace_entry *ent)
994 return te_blk_io_trace(ent)->action;
997 static inline u32 t_bytes(const struct trace_entry *ent)
999 return te_blk_io_trace(ent)->bytes;
1002 static inline u32 t_sec(const struct trace_entry *ent)
1004 return te_blk_io_trace(ent)->bytes >> 9;
1007 static inline unsigned long long t_sector(const struct trace_entry *ent)
1009 return te_blk_io_trace(ent)->sector;
1012 static inline __u16 t_error(const struct trace_entry *ent)
1014 return te_blk_io_trace(ent)->error;
1017 static __u64 get_pdu_int(const struct trace_entry *ent)
1019 const __u64 *val = pdu_start(ent);
1020 return be64_to_cpu(*val);
1023 static void get_pdu_remap(const struct trace_entry *ent,
1024 struct blk_io_trace_remap *r)
1026 const struct blk_io_trace_remap *__r = pdu_start(ent);
1027 __u64 sector_from = __r->sector_from;
1029 r->device_from = be32_to_cpu(__r->device_from);
1030 r->device_to = be32_to_cpu(__r->device_to);
1031 r->sector_from = be64_to_cpu(sector_from);
1034 typedef int (blk_log_action_t) (struct trace_iterator *iter, const char *act);
1036 static int blk_log_action_classic(struct trace_iterator *iter, const char *act)
1038 char rwbs[6];
1039 unsigned long long ts = iter->ts;
1040 unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
1041 unsigned secs = (unsigned long)ts;
1042 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1044 fill_rwbs(rwbs, t);
1046 return trace_seq_printf(&iter->seq,
1047 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
1048 MAJOR(t->device), MINOR(t->device), iter->cpu,
1049 secs, nsec_rem, iter->ent->pid, act, rwbs);
1052 static int blk_log_action(struct trace_iterator *iter, const char *act)
1054 char rwbs[6];
1055 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1057 fill_rwbs(rwbs, t);
1058 return trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
1059 MAJOR(t->device), MINOR(t->device), act, rwbs);
1062 static int blk_log_dump_pdu(struct trace_seq *s, const struct trace_entry *ent)
1064 const unsigned char *pdu_buf;
1065 int pdu_len;
1066 int i, end, ret;
1068 pdu_buf = pdu_start(ent);
1069 pdu_len = te_blk_io_trace(ent)->pdu_len;
1071 if (!pdu_len)
1072 return 1;
1074 /* find the last zero that needs to be printed */
1075 for (end = pdu_len - 1; end >= 0; end--)
1076 if (pdu_buf[end])
1077 break;
1078 end++;
1080 if (!trace_seq_putc(s, '('))
1081 return 0;
1083 for (i = 0; i < pdu_len; i++) {
1085 ret = trace_seq_printf(s, "%s%02x",
1086 i == 0 ? "" : " ", pdu_buf[i]);
1087 if (!ret)
1088 return ret;
1091 * stop when the rest is just zeroes and indicate so
1092 * with a ".." appended
1094 if (i == end && end != pdu_len - 1)
1095 return trace_seq_puts(s, " ..) ");
1098 return trace_seq_puts(s, ") ");
1101 static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1103 char cmd[TASK_COMM_LEN];
1105 trace_find_cmdline(ent->pid, cmd);
1107 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1108 int ret;
1110 ret = trace_seq_printf(s, "%u ", t_bytes(ent));
1111 if (!ret)
1112 return 0;
1113 ret = blk_log_dump_pdu(s, ent);
1114 if (!ret)
1115 return 0;
1116 return trace_seq_printf(s, "[%s]\n", cmd);
1117 } else {
1118 if (t_sec(ent))
1119 return trace_seq_printf(s, "%llu + %u [%s]\n",
1120 t_sector(ent), t_sec(ent), cmd);
1121 return trace_seq_printf(s, "[%s]\n", cmd);
1125 static int blk_log_with_error(struct trace_seq *s,
1126 const struct trace_entry *ent)
1128 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1129 int ret;
1131 ret = blk_log_dump_pdu(s, ent);
1132 if (ret)
1133 return trace_seq_printf(s, "[%d]\n", t_error(ent));
1134 return 0;
1135 } else {
1136 if (t_sec(ent))
1137 return trace_seq_printf(s, "%llu + %u [%d]\n",
1138 t_sector(ent),
1139 t_sec(ent), t_error(ent));
1140 return trace_seq_printf(s, "%llu [%d]\n",
1141 t_sector(ent), t_error(ent));
1145 static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1147 struct blk_io_trace_remap r = { .device_from = 0, };
1149 get_pdu_remap(ent, &r);
1150 return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
1151 t_sector(ent), t_sec(ent),
1152 MAJOR(r.device_from), MINOR(r.device_from),
1153 (unsigned long long)r.sector_from);
1156 static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1158 char cmd[TASK_COMM_LEN];
1160 trace_find_cmdline(ent->pid, cmd);
1162 return trace_seq_printf(s, "[%s]\n", cmd);
1165 static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1167 char cmd[TASK_COMM_LEN];
1169 trace_find_cmdline(ent->pid, cmd);
1171 return trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent));
1174 static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1176 char cmd[TASK_COMM_LEN];
1178 trace_find_cmdline(ent->pid, cmd);
1180 return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
1181 get_pdu_int(ent), cmd);
1184 static int blk_log_msg(struct trace_seq *s, const struct trace_entry *ent)
1186 int ret;
1187 const struct blk_io_trace *t = te_blk_io_trace(ent);
1189 ret = trace_seq_putmem(s, t + 1, t->pdu_len);
1190 if (ret)
1191 return trace_seq_putc(s, '\n');
1192 return ret;
1196 * struct tracer operations
1199 static void blk_tracer_print_header(struct seq_file *m)
1201 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1202 return;
1203 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1204 "# | | | | | |\n");
1207 static void blk_tracer_start(struct trace_array *tr)
1209 blk_tracer_enabled = true;
1212 static int blk_tracer_init(struct trace_array *tr)
1214 blk_tr = tr;
1215 blk_tracer_start(tr);
1216 return 0;
1219 static void blk_tracer_stop(struct trace_array *tr)
1221 blk_tracer_enabled = false;
1224 static void blk_tracer_reset(struct trace_array *tr)
1226 blk_tracer_stop(tr);
1229 static const struct {
1230 const char *act[2];
1231 int (*print)(struct trace_seq *s, const struct trace_entry *ent);
1232 } what2act[] = {
1233 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
1234 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1235 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1236 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1237 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1238 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1239 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1240 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1241 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1242 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
1243 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
1244 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1245 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1246 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1247 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1250 static enum print_line_t print_one_line(struct trace_iterator *iter,
1251 bool classic)
1253 struct trace_seq *s = &iter->seq;
1254 const struct blk_io_trace *t;
1255 u16 what;
1256 int ret;
1257 bool long_act;
1258 blk_log_action_t *log_action;
1260 t = te_blk_io_trace(iter->ent);
1261 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1262 long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1263 log_action = classic ? &blk_log_action_classic : &blk_log_action;
1265 if (t->action == BLK_TN_MESSAGE) {
1266 ret = log_action(iter, long_act ? "message" : "m");
1267 if (ret)
1268 ret = blk_log_msg(s, iter->ent);
1269 goto out;
1272 if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
1273 ret = trace_seq_printf(s, "Unknown action %x\n", what);
1274 else {
1275 ret = log_action(iter, what2act[what].act[long_act]);
1276 if (ret)
1277 ret = what2act[what].print(s, iter->ent);
1279 out:
1280 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1283 static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
1284 int flags)
1286 return print_one_line(iter, false);
1289 static int blk_trace_synthesize_old_trace(struct trace_iterator *iter)
1291 struct trace_seq *s = &iter->seq;
1292 struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1293 const int offset = offsetof(struct blk_io_trace, sector);
1294 struct blk_io_trace old = {
1295 .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
1296 .time = iter->ts,
1299 if (!trace_seq_putmem(s, &old, offset))
1300 return 0;
1301 return trace_seq_putmem(s, &t->sector,
1302 sizeof(old) - offset + t->pdu_len);
1305 static enum print_line_t
1306 blk_trace_event_print_binary(struct trace_iterator *iter, int flags)
1308 return blk_trace_synthesize_old_trace(iter) ?
1309 TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1312 static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1314 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1315 return TRACE_TYPE_UNHANDLED;
1317 return print_one_line(iter, true);
1320 static int blk_tracer_set_flag(u32 old_flags, u32 bit, int set)
1322 /* don't output context-info for blk_classic output */
1323 if (bit == TRACE_BLK_OPT_CLASSIC) {
1324 if (set)
1325 trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
1326 else
1327 trace_flags |= TRACE_ITER_CONTEXT_INFO;
1329 return 0;
1332 static struct tracer blk_tracer __read_mostly = {
1333 .name = "blk",
1334 .init = blk_tracer_init,
1335 .reset = blk_tracer_reset,
1336 .start = blk_tracer_start,
1337 .stop = blk_tracer_stop,
1338 .print_header = blk_tracer_print_header,
1339 .print_line = blk_tracer_print_line,
1340 .flags = &blk_tracer_flags,
1341 .set_flag = blk_tracer_set_flag,
1344 static struct trace_event trace_blk_event = {
1345 .type = TRACE_BLK,
1346 .trace = blk_trace_event_print,
1347 .binary = blk_trace_event_print_binary,
1350 static int __init init_blk_tracer(void)
1352 if (!register_ftrace_event(&trace_blk_event)) {
1353 pr_warning("Warning: could not register block events\n");
1354 return 1;
1357 if (register_tracer(&blk_tracer) != 0) {
1358 pr_warning("Warning: could not register the block tracer\n");
1359 unregister_ftrace_event(&trace_blk_event);
1360 return 1;
1363 return 0;
1366 device_initcall(init_blk_tracer);
1368 static int blk_trace_remove_queue(struct request_queue *q)
1370 struct blk_trace *bt;
1372 bt = xchg(&q->blk_trace, NULL);
1373 if (bt == NULL)
1374 return -EINVAL;
1376 if (atomic_dec_and_test(&blk_probes_ref))
1377 blk_unregister_tracepoints();
1379 blk_trace_free(bt);
1380 return 0;
1384 * Setup everything required to start tracing
1386 static int blk_trace_setup_queue(struct request_queue *q,
1387 struct block_device *bdev)
1389 struct blk_trace *old_bt, *bt = NULL;
1390 int ret = -ENOMEM;
1392 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1393 if (!bt)
1394 return -ENOMEM;
1396 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
1397 if (!bt->msg_data)
1398 goto free_bt;
1400 bt->dev = bdev->bd_dev;
1401 bt->act_mask = (u16)-1;
1403 blk_trace_setup_lba(bt, bdev);
1405 old_bt = xchg(&q->blk_trace, bt);
1406 if (old_bt != NULL) {
1407 (void)xchg(&q->blk_trace, old_bt);
1408 ret = -EBUSY;
1409 goto free_bt;
1412 if (atomic_inc_return(&blk_probes_ref) == 1)
1413 blk_register_tracepoints();
1414 return 0;
1416 free_bt:
1417 blk_trace_free(bt);
1418 return ret;
1422 * sysfs interface to enable and configure tracing
1425 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1426 struct device_attribute *attr,
1427 char *buf);
1428 static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1429 struct device_attribute *attr,
1430 const char *buf, size_t count);
1431 #define BLK_TRACE_DEVICE_ATTR(_name) \
1432 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1433 sysfs_blk_trace_attr_show, \
1434 sysfs_blk_trace_attr_store)
1436 static BLK_TRACE_DEVICE_ATTR(enable);
1437 static BLK_TRACE_DEVICE_ATTR(act_mask);
1438 static BLK_TRACE_DEVICE_ATTR(pid);
1439 static BLK_TRACE_DEVICE_ATTR(start_lba);
1440 static BLK_TRACE_DEVICE_ATTR(end_lba);
1442 static struct attribute *blk_trace_attrs[] = {
1443 &dev_attr_enable.attr,
1444 &dev_attr_act_mask.attr,
1445 &dev_attr_pid.attr,
1446 &dev_attr_start_lba.attr,
1447 &dev_attr_end_lba.attr,
1448 NULL
1451 struct attribute_group blk_trace_attr_group = {
1452 .name = "trace",
1453 .attrs = blk_trace_attrs,
1456 static const struct {
1457 int mask;
1458 const char *str;
1459 } mask_maps[] = {
1460 { BLK_TC_READ, "read" },
1461 { BLK_TC_WRITE, "write" },
1462 { BLK_TC_BARRIER, "barrier" },
1463 { BLK_TC_SYNC, "sync" },
1464 { BLK_TC_QUEUE, "queue" },
1465 { BLK_TC_REQUEUE, "requeue" },
1466 { BLK_TC_ISSUE, "issue" },
1467 { BLK_TC_COMPLETE, "complete" },
1468 { BLK_TC_FS, "fs" },
1469 { BLK_TC_PC, "pc" },
1470 { BLK_TC_AHEAD, "ahead" },
1471 { BLK_TC_META, "meta" },
1472 { BLK_TC_DISCARD, "discard" },
1473 { BLK_TC_DRV_DATA, "drv_data" },
1476 static int blk_trace_str2mask(const char *str)
1478 int i;
1479 int mask = 0;
1480 char *buf, *s, *token;
1482 buf = kstrdup(str, GFP_KERNEL);
1483 if (buf == NULL)
1484 return -ENOMEM;
1485 s = strstrip(buf);
1487 while (1) {
1488 token = strsep(&s, ",");
1489 if (token == NULL)
1490 break;
1492 if (*token == '\0')
1493 continue;
1495 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1496 if (strcasecmp(token, mask_maps[i].str) == 0) {
1497 mask |= mask_maps[i].mask;
1498 break;
1501 if (i == ARRAY_SIZE(mask_maps)) {
1502 mask = -EINVAL;
1503 break;
1506 kfree(buf);
1508 return mask;
1511 static ssize_t blk_trace_mask2str(char *buf, int mask)
1513 int i;
1514 char *p = buf;
1516 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1517 if (mask & mask_maps[i].mask) {
1518 p += sprintf(p, "%s%s",
1519 (p == buf) ? "" : ",", mask_maps[i].str);
1522 *p++ = '\n';
1524 return p - buf;
1527 static struct request_queue *blk_trace_get_queue(struct block_device *bdev)
1529 if (bdev->bd_disk == NULL)
1530 return NULL;
1532 return bdev_get_queue(bdev);
1535 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1536 struct device_attribute *attr,
1537 char *buf)
1539 struct hd_struct *p = dev_to_part(dev);
1540 struct request_queue *q;
1541 struct block_device *bdev;
1542 ssize_t ret = -ENXIO;
1544 lock_kernel();
1545 bdev = bdget(part_devt(p));
1546 if (bdev == NULL)
1547 goto out_unlock_kernel;
1549 q = blk_trace_get_queue(bdev);
1550 if (q == NULL)
1551 goto out_bdput;
1553 mutex_lock(&bdev->bd_mutex);
1555 if (attr == &dev_attr_enable) {
1556 ret = sprintf(buf, "%u\n", !!q->blk_trace);
1557 goto out_unlock_bdev;
1560 if (q->blk_trace == NULL)
1561 ret = sprintf(buf, "disabled\n");
1562 else if (attr == &dev_attr_act_mask)
1563 ret = blk_trace_mask2str(buf, q->blk_trace->act_mask);
1564 else if (attr == &dev_attr_pid)
1565 ret = sprintf(buf, "%u\n", q->blk_trace->pid);
1566 else if (attr == &dev_attr_start_lba)
1567 ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
1568 else if (attr == &dev_attr_end_lba)
1569 ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
1571 out_unlock_bdev:
1572 mutex_unlock(&bdev->bd_mutex);
1573 out_bdput:
1574 bdput(bdev);
1575 out_unlock_kernel:
1576 unlock_kernel();
1577 return ret;
1580 static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1581 struct device_attribute *attr,
1582 const char *buf, size_t count)
1584 struct block_device *bdev;
1585 struct request_queue *q;
1586 struct hd_struct *p;
1587 u64 value;
1588 ssize_t ret = -EINVAL;
1590 if (count == 0)
1591 goto out;
1593 if (attr == &dev_attr_act_mask) {
1594 if (sscanf(buf, "%llx", &value) != 1) {
1595 /* Assume it is a list of trace category names */
1596 ret = blk_trace_str2mask(buf);
1597 if (ret < 0)
1598 goto out;
1599 value = ret;
1601 } else if (sscanf(buf, "%llu", &value) != 1)
1602 goto out;
1604 ret = -ENXIO;
1606 lock_kernel();
1607 p = dev_to_part(dev);
1608 bdev = bdget(part_devt(p));
1609 if (bdev == NULL)
1610 goto out_unlock_kernel;
1612 q = blk_trace_get_queue(bdev);
1613 if (q == NULL)
1614 goto out_bdput;
1616 mutex_lock(&bdev->bd_mutex);
1618 if (attr == &dev_attr_enable) {
1619 if (value)
1620 ret = blk_trace_setup_queue(q, bdev);
1621 else
1622 ret = blk_trace_remove_queue(q);
1623 goto out_unlock_bdev;
1626 ret = 0;
1627 if (q->blk_trace == NULL)
1628 ret = blk_trace_setup_queue(q, bdev);
1630 if (ret == 0) {
1631 if (attr == &dev_attr_act_mask)
1632 q->blk_trace->act_mask = value;
1633 else if (attr == &dev_attr_pid)
1634 q->blk_trace->pid = value;
1635 else if (attr == &dev_attr_start_lba)
1636 q->blk_trace->start_lba = value;
1637 else if (attr == &dev_attr_end_lba)
1638 q->blk_trace->end_lba = value;
1641 out_unlock_bdev:
1642 mutex_unlock(&bdev->bd_mutex);
1643 out_bdput:
1644 bdput(bdev);
1645 out_unlock_kernel:
1646 unlock_kernel();
1647 out:
1648 return ret ? ret : count;
1651 int blk_trace_init_sysfs(struct device *dev)
1653 return sysfs_create_group(&dev->kobj, &blk_trace_attr_group);
1656 #endif /* CONFIG_BLK_DEV_IO_TRACE */
1658 #ifdef CONFIG_EVENT_TRACING
1660 void blk_dump_cmd(char *buf, struct request *rq)
1662 int i, end;
1663 int len = rq->cmd_len;
1664 unsigned char *cmd = rq->cmd;
1666 if (!blk_pc_request(rq)) {
1667 buf[0] = '\0';
1668 return;
1671 for (end = len - 1; end >= 0; end--)
1672 if (cmd[end])
1673 break;
1674 end++;
1676 for (i = 0; i < len; i++) {
1677 buf += sprintf(buf, "%s%02x", i == 0 ? "" : " ", cmd[i]);
1678 if (i == end && end != len - 1) {
1679 sprintf(buf, " ..");
1680 break;
1685 void blk_fill_rwbs(char *rwbs, u32 rw, int bytes)
1687 int i = 0;
1689 if (rw & WRITE)
1690 rwbs[i++] = 'W';
1691 else if (rw & 1 << BIO_RW_DISCARD)
1692 rwbs[i++] = 'D';
1693 else if (bytes)
1694 rwbs[i++] = 'R';
1695 else
1696 rwbs[i++] = 'N';
1698 if (rw & 1 << BIO_RW_AHEAD)
1699 rwbs[i++] = 'A';
1700 if (rw & 1 << BIO_RW_BARRIER)
1701 rwbs[i++] = 'B';
1702 if (rw & 1 << BIO_RW_SYNCIO)
1703 rwbs[i++] = 'S';
1704 if (rw & 1 << BIO_RW_META)
1705 rwbs[i++] = 'M';
1707 rwbs[i] = '\0';
1710 void blk_fill_rwbs_rq(char *rwbs, struct request *rq)
1712 int rw = rq->cmd_flags & 0x03;
1713 int bytes;
1715 if (blk_discard_rq(rq))
1716 rw |= (1 << BIO_RW_DISCARD);
1718 bytes = blk_rq_bytes(rq);
1720 blk_fill_rwbs(rwbs, rw, bytes);
1723 #endif /* CONFIG_EVENT_TRACING */