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/slab.h>
25 #include <linux/debugfs.h>
26 #include <linux/smp_lock.h>
27 #include <linux/time.h>
28 #include <linux/uaccess.h>
30 #include <trace/events/block.h>
32 #include "trace_output.h"
34 #ifdef CONFIG_BLK_DEV_IO_TRACE
36 static unsigned int blktrace_seq __read_mostly
= 1;
38 static struct trace_array
*blk_tr
;
39 static bool blk_tracer_enabled __read_mostly
;
41 /* Select an alternative, minimalistic output than the original one */
42 #define TRACE_BLK_OPT_CLASSIC 0x1
44 static struct tracer_opt blk_tracer_opts
[] = {
45 /* Default disable the minimalistic output */
46 { TRACER_OPT(blk_classic
, TRACE_BLK_OPT_CLASSIC
) },
50 static struct tracer_flags blk_tracer_flags
= {
52 .opts
= blk_tracer_opts
,
55 /* Global reference count of probes */
56 static atomic_t blk_probes_ref
= ATOMIC_INIT(0);
58 static void blk_register_tracepoints(void);
59 static void blk_unregister_tracepoints(void);
62 * Send out a notify message.
64 static void trace_note(struct blk_trace
*bt
, pid_t pid
, int action
,
65 const void *data
, size_t len
)
67 struct blk_io_trace
*t
;
68 struct ring_buffer_event
*event
= NULL
;
69 struct ring_buffer
*buffer
= NULL
;
71 int cpu
= smp_processor_id();
72 bool blk_tracer
= blk_tracer_enabled
;
75 buffer
= blk_tr
->buffer
;
77 event
= trace_buffer_lock_reserve(buffer
, TRACE_BLK
,
82 t
= ring_buffer_event_data(event
);
89 t
= relay_reserve(bt
->rchan
, sizeof(*t
) + len
);
91 t
->magic
= BLK_IO_TRACE_MAGIC
| BLK_IO_TRACE_VERSION
;
92 t
->time
= ktime_to_ns(ktime_get());
99 memcpy((void *) t
+ sizeof(*t
), data
, len
);
102 trace_buffer_unlock_commit(buffer
, event
, 0, pc
);
107 * Send out a notify for this process, if we haven't done so since a trace
110 static void trace_note_tsk(struct blk_trace
*bt
, struct task_struct
*tsk
)
112 tsk
->btrace_seq
= blktrace_seq
;
113 trace_note(bt
, tsk
->pid
, BLK_TN_PROCESS
, tsk
->comm
, sizeof(tsk
->comm
));
116 static void trace_note_time(struct blk_trace
*bt
)
122 getnstimeofday(&now
);
123 words
[0] = now
.tv_sec
;
124 words
[1] = now
.tv_nsec
;
126 local_irq_save(flags
);
127 trace_note(bt
, 0, BLK_TN_TIMESTAMP
, words
, sizeof(words
));
128 local_irq_restore(flags
);
131 void __trace_note_message(struct blk_trace
*bt
, const char *fmt
, ...)
138 if (unlikely(bt
->trace_state
!= Blktrace_running
&&
139 !blk_tracer_enabled
))
142 local_irq_save(flags
);
143 buf
= per_cpu_ptr(bt
->msg_data
, smp_processor_id());
145 n
= vscnprintf(buf
, BLK_TN_MAX_MSG
, fmt
, args
);
148 trace_note(bt
, 0, BLK_TN_MESSAGE
, buf
, n
);
149 local_irq_restore(flags
);
151 EXPORT_SYMBOL_GPL(__trace_note_message
);
153 static int act_log_check(struct blk_trace
*bt
, u32 what
, sector_t sector
,
156 if (((bt
->act_mask
<< BLK_TC_SHIFT
) & what
) == 0)
158 if (sector
&& (sector
< bt
->start_lba
|| sector
> bt
->end_lba
))
160 if (bt
->pid
&& pid
!= bt
->pid
)
167 * Data direction bit lookup
169 static const u32 ddir_act
[2] = { BLK_TC_ACT(BLK_TC_READ
),
170 BLK_TC_ACT(BLK_TC_WRITE
) };
172 /* The ilog2() calls fall out because they're constant */
173 #define MASK_TC_BIT(rw, __name) ((rw & (1 << BIO_RW_ ## __name)) << \
174 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name))
177 * The worker for the various blk_add_trace*() types. Fills out a
178 * blk_io_trace structure and places it in a per-cpu subbuffer.
180 static void __blk_add_trace(struct blk_trace
*bt
, sector_t sector
, int bytes
,
181 int rw
, u32 what
, int error
, int pdu_len
, void *pdu_data
)
183 struct task_struct
*tsk
= current
;
184 struct ring_buffer_event
*event
= NULL
;
185 struct ring_buffer
*buffer
= NULL
;
186 struct blk_io_trace
*t
;
187 unsigned long flags
= 0;
188 unsigned long *sequence
;
191 bool blk_tracer
= blk_tracer_enabled
;
193 if (unlikely(bt
->trace_state
!= Blktrace_running
&& !blk_tracer
))
196 what
|= ddir_act
[rw
& WRITE
];
197 what
|= MASK_TC_BIT(rw
, BARRIER
);
198 what
|= MASK_TC_BIT(rw
, SYNCIO
);
199 what
|= MASK_TC_BIT(rw
, AHEAD
);
200 what
|= MASK_TC_BIT(rw
, META
);
201 what
|= MASK_TC_BIT(rw
, DISCARD
);
204 if (act_log_check(bt
, what
, sector
, pid
))
206 cpu
= raw_smp_processor_id();
209 tracing_record_cmdline(current
);
211 buffer
= blk_tr
->buffer
;
212 pc
= preempt_count();
213 event
= trace_buffer_lock_reserve(buffer
, TRACE_BLK
,
214 sizeof(*t
) + pdu_len
,
218 t
= ring_buffer_event_data(event
);
223 * A word about the locking here - we disable interrupts to reserve
224 * some space in the relay per-cpu buffer, to prevent an irq
225 * from coming in and stepping on our toes.
227 local_irq_save(flags
);
229 if (unlikely(tsk
->btrace_seq
!= blktrace_seq
))
230 trace_note_tsk(bt
, tsk
);
232 t
= relay_reserve(bt
->rchan
, sizeof(*t
) + pdu_len
);
234 sequence
= per_cpu_ptr(bt
->sequence
, cpu
);
236 t
->magic
= BLK_IO_TRACE_MAGIC
| BLK_IO_TRACE_VERSION
;
237 t
->sequence
= ++(*sequence
);
238 t
->time
= ktime_to_ns(ktime_get());
241 * These two are not needed in ftrace as they are in the
242 * generic trace_entry, filled by tracing_generic_entry_update,
243 * but for the trace_event->bin() synthesizer benefit we do it
254 t
->pdu_len
= pdu_len
;
257 memcpy((void *) t
+ sizeof(*t
), pdu_data
, pdu_len
);
260 trace_buffer_unlock_commit(buffer
, event
, 0, pc
);
265 local_irq_restore(flags
);
268 static struct dentry
*blk_tree_root
;
269 static DEFINE_MUTEX(blk_tree_mutex
);
271 static void blk_trace_free(struct blk_trace
*bt
)
273 debugfs_remove(bt
->msg_file
);
274 debugfs_remove(bt
->dropped_file
);
275 relay_close(bt
->rchan
);
276 debugfs_remove(bt
->dir
);
277 free_percpu(bt
->sequence
);
278 free_percpu(bt
->msg_data
);
282 static void blk_trace_cleanup(struct blk_trace
*bt
)
285 if (atomic_dec_and_test(&blk_probes_ref
))
286 blk_unregister_tracepoints();
289 int blk_trace_remove(struct request_queue
*q
)
291 struct blk_trace
*bt
;
293 bt
= xchg(&q
->blk_trace
, NULL
);
297 if (bt
->trace_state
!= Blktrace_running
)
298 blk_trace_cleanup(bt
);
302 EXPORT_SYMBOL_GPL(blk_trace_remove
);
304 static int blk_dropped_open(struct inode
*inode
, struct file
*filp
)
306 filp
->private_data
= inode
->i_private
;
311 static ssize_t
blk_dropped_read(struct file
*filp
, char __user
*buffer
,
312 size_t count
, loff_t
*ppos
)
314 struct blk_trace
*bt
= filp
->private_data
;
317 snprintf(buf
, sizeof(buf
), "%u\n", atomic_read(&bt
->dropped
));
319 return simple_read_from_buffer(buffer
, count
, ppos
, buf
, strlen(buf
));
322 static const struct file_operations blk_dropped_fops
= {
323 .owner
= THIS_MODULE
,
324 .open
= blk_dropped_open
,
325 .read
= blk_dropped_read
,
328 static int blk_msg_open(struct inode
*inode
, struct file
*filp
)
330 filp
->private_data
= inode
->i_private
;
335 static ssize_t
blk_msg_write(struct file
*filp
, const char __user
*buffer
,
336 size_t count
, loff_t
*ppos
)
339 struct blk_trace
*bt
;
341 if (count
>= BLK_TN_MAX_MSG
)
344 msg
= kmalloc(count
+ 1, GFP_KERNEL
);
348 if (copy_from_user(msg
, buffer
, count
)) {
354 bt
= filp
->private_data
;
355 __trace_note_message(bt
, "%s", msg
);
361 static const struct file_operations blk_msg_fops
= {
362 .owner
= THIS_MODULE
,
363 .open
= blk_msg_open
,
364 .write
= blk_msg_write
,
368 * Keep track of how many times we encountered a full subbuffer, to aid
369 * the user space app in telling how many lost events there were.
371 static int blk_subbuf_start_callback(struct rchan_buf
*buf
, void *subbuf
,
372 void *prev_subbuf
, size_t prev_padding
)
374 struct blk_trace
*bt
;
376 if (!relay_buf_full(buf
))
379 bt
= buf
->chan
->private_data
;
380 atomic_inc(&bt
->dropped
);
384 static int blk_remove_buf_file_callback(struct dentry
*dentry
)
386 debugfs_remove(dentry
);
391 static struct dentry
*blk_create_buf_file_callback(const char *filename
,
392 struct dentry
*parent
,
394 struct rchan_buf
*buf
,
397 return debugfs_create_file(filename
, mode
, parent
, buf
,
398 &relay_file_operations
);
401 static struct rchan_callbacks blk_relay_callbacks
= {
402 .subbuf_start
= blk_subbuf_start_callback
,
403 .create_buf_file
= blk_create_buf_file_callback
,
404 .remove_buf_file
= blk_remove_buf_file_callback
,
407 static void blk_trace_setup_lba(struct blk_trace
*bt
,
408 struct block_device
*bdev
)
410 struct hd_struct
*part
= NULL
;
413 part
= bdev
->bd_part
;
416 bt
->start_lba
= part
->start_sect
;
417 bt
->end_lba
= part
->start_sect
+ part
->nr_sects
;
425 * Setup everything required to start tracing
427 int do_blk_trace_setup(struct request_queue
*q
, char *name
, dev_t dev
,
428 struct block_device
*bdev
,
429 struct blk_user_trace_setup
*buts
)
431 struct blk_trace
*old_bt
, *bt
= NULL
;
432 struct dentry
*dir
= NULL
;
435 if (!buts
->buf_size
|| !buts
->buf_nr
)
438 strncpy(buts
->name
, name
, BLKTRACE_BDEV_SIZE
);
439 buts
->name
[BLKTRACE_BDEV_SIZE
- 1] = '\0';
442 * some device names have larger paths - convert the slashes
443 * to underscores for this to work as expected
445 for (i
= 0; i
< strlen(buts
->name
); i
++)
446 if (buts
->name
[i
] == '/')
449 bt
= kzalloc(sizeof(*bt
), GFP_KERNEL
);
454 bt
->sequence
= alloc_percpu(unsigned long);
458 bt
->msg_data
= __alloc_percpu(BLK_TN_MAX_MSG
, __alignof__(char));
464 mutex_lock(&blk_tree_mutex
);
465 if (!blk_tree_root
) {
466 blk_tree_root
= debugfs_create_dir("block", NULL
);
467 if (!blk_tree_root
) {
468 mutex_unlock(&blk_tree_mutex
);
472 mutex_unlock(&blk_tree_mutex
);
474 dir
= debugfs_create_dir(buts
->name
, blk_tree_root
);
481 atomic_set(&bt
->dropped
, 0);
484 bt
->dropped_file
= debugfs_create_file("dropped", 0444, dir
, bt
,
486 if (!bt
->dropped_file
)
489 bt
->msg_file
= debugfs_create_file("msg", 0222, dir
, bt
, &blk_msg_fops
);
493 bt
->rchan
= relay_open("trace", dir
, buts
->buf_size
,
494 buts
->buf_nr
, &blk_relay_callbacks
, bt
);
498 bt
->act_mask
= buts
->act_mask
;
500 bt
->act_mask
= (u16
) -1;
502 blk_trace_setup_lba(bt
, bdev
);
504 /* overwrite with user settings */
506 bt
->start_lba
= buts
->start_lba
;
508 bt
->end_lba
= buts
->end_lba
;
511 bt
->trace_state
= Blktrace_setup
;
514 old_bt
= xchg(&q
->blk_trace
, bt
);
516 (void) xchg(&q
->blk_trace
, old_bt
);
520 if (atomic_inc_return(&blk_probes_ref
) == 1)
521 blk_register_tracepoints();
529 int blk_trace_setup(struct request_queue
*q
, char *name
, dev_t dev
,
530 struct block_device
*bdev
,
533 struct blk_user_trace_setup buts
;
536 ret
= copy_from_user(&buts
, arg
, sizeof(buts
));
540 ret
= do_blk_trace_setup(q
, name
, dev
, bdev
, &buts
);
544 if (copy_to_user(arg
, &buts
, sizeof(buts
))) {
550 EXPORT_SYMBOL_GPL(blk_trace_setup
);
552 int blk_trace_startstop(struct request_queue
*q
, int start
)
555 struct blk_trace
*bt
= q
->blk_trace
;
561 * For starting a trace, we can transition from a setup or stopped
562 * trace. For stopping a trace, the state must be running
566 if (bt
->trace_state
== Blktrace_setup
||
567 bt
->trace_state
== Blktrace_stopped
) {
570 bt
->trace_state
= Blktrace_running
;
576 if (bt
->trace_state
== Blktrace_running
) {
577 bt
->trace_state
= Blktrace_stopped
;
578 relay_flush(bt
->rchan
);
585 EXPORT_SYMBOL_GPL(blk_trace_startstop
);
588 * blk_trace_ioctl: - handle the ioctls associated with tracing
589 * @bdev: the block device
590 * @cmd: the ioctl cmd
591 * @arg: the argument data, if any
594 int blk_trace_ioctl(struct block_device
*bdev
, unsigned cmd
, char __user
*arg
)
596 struct request_queue
*q
;
598 char b
[BDEVNAME_SIZE
];
600 q
= bdev_get_queue(bdev
);
604 mutex_lock(&bdev
->bd_mutex
);
609 ret
= blk_trace_setup(q
, b
, bdev
->bd_dev
, bdev
, arg
);
614 ret
= blk_trace_startstop(q
, start
);
616 case BLKTRACETEARDOWN
:
617 ret
= blk_trace_remove(q
);
624 mutex_unlock(&bdev
->bd_mutex
);
629 * blk_trace_shutdown: - stop and cleanup trace structures
630 * @q: the request queue associated with the device
633 void blk_trace_shutdown(struct request_queue
*q
)
636 blk_trace_startstop(q
, 0);
646 * blk_add_trace_rq - Add a trace for a request oriented action
647 * @q: queue the io is for
648 * @rq: the source request
652 * Records an action against a request. Will log the bio offset + size.
655 static void blk_add_trace_rq(struct request_queue
*q
, struct request
*rq
,
658 struct blk_trace
*bt
= q
->blk_trace
;
659 int rw
= rq
->cmd_flags
& 0x03;
664 if (blk_discard_rq(rq
))
665 rw
|= (1 << BIO_RW_DISCARD
);
667 if (blk_pc_request(rq
)) {
668 what
|= BLK_TC_ACT(BLK_TC_PC
);
669 __blk_add_trace(bt
, 0, blk_rq_bytes(rq
), rw
,
670 what
, rq
->errors
, rq
->cmd_len
, rq
->cmd
);
672 what
|= BLK_TC_ACT(BLK_TC_FS
);
673 __blk_add_trace(bt
, blk_rq_pos(rq
), blk_rq_bytes(rq
), rw
,
674 what
, rq
->errors
, 0, NULL
);
678 static void blk_add_trace_rq_abort(void *ignore
,
679 struct request_queue
*q
, struct request
*rq
)
681 blk_add_trace_rq(q
, rq
, BLK_TA_ABORT
);
684 static void blk_add_trace_rq_insert(void *ignore
,
685 struct request_queue
*q
, struct request
*rq
)
687 blk_add_trace_rq(q
, rq
, BLK_TA_INSERT
);
690 static void blk_add_trace_rq_issue(void *ignore
,
691 struct request_queue
*q
, struct request
*rq
)
693 blk_add_trace_rq(q
, rq
, BLK_TA_ISSUE
);
696 static void blk_add_trace_rq_requeue(void *ignore
,
697 struct request_queue
*q
,
700 blk_add_trace_rq(q
, rq
, BLK_TA_REQUEUE
);
703 static void blk_add_trace_rq_complete(void *ignore
,
704 struct request_queue
*q
,
707 blk_add_trace_rq(q
, rq
, BLK_TA_COMPLETE
);
711 * blk_add_trace_bio - Add a trace for a bio oriented action
712 * @q: queue the io is for
713 * @bio: the source bio
717 * Records an action against a bio. Will log the bio offset + size.
720 static void blk_add_trace_bio(struct request_queue
*q
, struct bio
*bio
,
723 struct blk_trace
*bt
= q
->blk_trace
;
728 __blk_add_trace(bt
, bio
->bi_sector
, bio
->bi_size
, bio
->bi_rw
, what
,
729 !bio_flagged(bio
, BIO_UPTODATE
), 0, NULL
);
732 static void blk_add_trace_bio_bounce(void *ignore
,
733 struct request_queue
*q
, struct bio
*bio
)
735 blk_add_trace_bio(q
, bio
, BLK_TA_BOUNCE
);
738 static void blk_add_trace_bio_complete(void *ignore
,
739 struct request_queue
*q
, struct bio
*bio
)
741 blk_add_trace_bio(q
, bio
, BLK_TA_COMPLETE
);
744 static void blk_add_trace_bio_backmerge(void *ignore
,
745 struct request_queue
*q
,
748 blk_add_trace_bio(q
, bio
, BLK_TA_BACKMERGE
);
751 static void blk_add_trace_bio_frontmerge(void *ignore
,
752 struct request_queue
*q
,
755 blk_add_trace_bio(q
, bio
, BLK_TA_FRONTMERGE
);
758 static void blk_add_trace_bio_queue(void *ignore
,
759 struct request_queue
*q
, struct bio
*bio
)
761 blk_add_trace_bio(q
, bio
, BLK_TA_QUEUE
);
764 static void blk_add_trace_getrq(void *ignore
,
765 struct request_queue
*q
,
766 struct bio
*bio
, int rw
)
769 blk_add_trace_bio(q
, bio
, BLK_TA_GETRQ
);
771 struct blk_trace
*bt
= q
->blk_trace
;
774 __blk_add_trace(bt
, 0, 0, rw
, BLK_TA_GETRQ
, 0, 0, NULL
);
779 static void blk_add_trace_sleeprq(void *ignore
,
780 struct request_queue
*q
,
781 struct bio
*bio
, int rw
)
784 blk_add_trace_bio(q
, bio
, BLK_TA_SLEEPRQ
);
786 struct blk_trace
*bt
= q
->blk_trace
;
789 __blk_add_trace(bt
, 0, 0, rw
, BLK_TA_SLEEPRQ
,
794 static void blk_add_trace_plug(void *ignore
, struct request_queue
*q
)
796 struct blk_trace
*bt
= q
->blk_trace
;
799 __blk_add_trace(bt
, 0, 0, 0, BLK_TA_PLUG
, 0, 0, NULL
);
802 static void blk_add_trace_unplug_io(void *ignore
, struct request_queue
*q
)
804 struct blk_trace
*bt
= q
->blk_trace
;
807 unsigned int pdu
= q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
];
808 __be64 rpdu
= cpu_to_be64(pdu
);
810 __blk_add_trace(bt
, 0, 0, 0, BLK_TA_UNPLUG_IO
, 0,
811 sizeof(rpdu
), &rpdu
);
815 static void blk_add_trace_unplug_timer(void *ignore
, struct request_queue
*q
)
817 struct blk_trace
*bt
= q
->blk_trace
;
820 unsigned int pdu
= q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
];
821 __be64 rpdu
= cpu_to_be64(pdu
);
823 __blk_add_trace(bt
, 0, 0, 0, BLK_TA_UNPLUG_TIMER
, 0,
824 sizeof(rpdu
), &rpdu
);
828 static void blk_add_trace_split(void *ignore
,
829 struct request_queue
*q
, struct bio
*bio
,
832 struct blk_trace
*bt
= q
->blk_trace
;
835 __be64 rpdu
= cpu_to_be64(pdu
);
837 __blk_add_trace(bt
, bio
->bi_sector
, bio
->bi_size
, bio
->bi_rw
,
838 BLK_TA_SPLIT
, !bio_flagged(bio
, BIO_UPTODATE
),
839 sizeof(rpdu
), &rpdu
);
844 * blk_add_trace_remap - Add a trace for a remap operation
845 * @ignore: trace callback data parameter (not used)
846 * @q: queue the io is for
847 * @bio: the source bio
848 * @dev: target device
849 * @from: source sector
852 * Device mapper or raid target sometimes need to split a bio because
853 * it spans a stripe (or similar). Add a trace for that action.
856 static void blk_add_trace_remap(void *ignore
,
857 struct request_queue
*q
, struct bio
*bio
,
858 dev_t dev
, sector_t from
)
860 struct blk_trace
*bt
= q
->blk_trace
;
861 struct blk_io_trace_remap r
;
866 r
.device_from
= cpu_to_be32(dev
);
867 r
.device_to
= cpu_to_be32(bio
->bi_bdev
->bd_dev
);
868 r
.sector_from
= cpu_to_be64(from
);
870 __blk_add_trace(bt
, bio
->bi_sector
, bio
->bi_size
, bio
->bi_rw
,
871 BLK_TA_REMAP
, !bio_flagged(bio
, BIO_UPTODATE
),
876 * blk_add_trace_rq_remap - Add a trace for a request-remap operation
877 * @ignore: trace callback data parameter (not used)
878 * @q: queue the io is for
879 * @rq: the source request
880 * @dev: target device
881 * @from: source sector
884 * Device mapper remaps request to other devices.
885 * Add a trace for that action.
888 static void blk_add_trace_rq_remap(void *ignore
,
889 struct request_queue
*q
,
890 struct request
*rq
, dev_t dev
,
893 struct blk_trace
*bt
= q
->blk_trace
;
894 struct blk_io_trace_remap r
;
899 r
.device_from
= cpu_to_be32(dev
);
900 r
.device_to
= cpu_to_be32(disk_devt(rq
->rq_disk
));
901 r
.sector_from
= cpu_to_be64(from
);
903 __blk_add_trace(bt
, blk_rq_pos(rq
), blk_rq_bytes(rq
),
904 rq_data_dir(rq
), BLK_TA_REMAP
, !!rq
->errors
,
909 * blk_add_driver_data - Add binary message with driver-specific data
910 * @q: queue the io is for
912 * @data: driver-specific data
913 * @len: length of driver-specific data
916 * Some drivers might want to write driver-specific data per request.
919 void blk_add_driver_data(struct request_queue
*q
,
921 void *data
, size_t len
)
923 struct blk_trace
*bt
= q
->blk_trace
;
928 if (blk_pc_request(rq
))
929 __blk_add_trace(bt
, 0, blk_rq_bytes(rq
), 0,
930 BLK_TA_DRV_DATA
, rq
->errors
, len
, data
);
932 __blk_add_trace(bt
, blk_rq_pos(rq
), blk_rq_bytes(rq
), 0,
933 BLK_TA_DRV_DATA
, rq
->errors
, len
, data
);
935 EXPORT_SYMBOL_GPL(blk_add_driver_data
);
937 static void blk_register_tracepoints(void)
941 ret
= register_trace_block_rq_abort(blk_add_trace_rq_abort
, NULL
);
943 ret
= register_trace_block_rq_insert(blk_add_trace_rq_insert
, NULL
);
945 ret
= register_trace_block_rq_issue(blk_add_trace_rq_issue
, NULL
);
947 ret
= register_trace_block_rq_requeue(blk_add_trace_rq_requeue
, NULL
);
949 ret
= register_trace_block_rq_complete(blk_add_trace_rq_complete
, NULL
);
951 ret
= register_trace_block_bio_bounce(blk_add_trace_bio_bounce
, NULL
);
953 ret
= register_trace_block_bio_complete(blk_add_trace_bio_complete
, NULL
);
955 ret
= register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge
, NULL
);
957 ret
= register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge
, NULL
);
959 ret
= register_trace_block_bio_queue(blk_add_trace_bio_queue
, NULL
);
961 ret
= register_trace_block_getrq(blk_add_trace_getrq
, NULL
);
963 ret
= register_trace_block_sleeprq(blk_add_trace_sleeprq
, NULL
);
965 ret
= register_trace_block_plug(blk_add_trace_plug
, NULL
);
967 ret
= register_trace_block_unplug_timer(blk_add_trace_unplug_timer
, NULL
);
969 ret
= register_trace_block_unplug_io(blk_add_trace_unplug_io
, NULL
);
971 ret
= register_trace_block_split(blk_add_trace_split
, NULL
);
973 ret
= register_trace_block_remap(blk_add_trace_remap
, NULL
);
975 ret
= register_trace_block_rq_remap(blk_add_trace_rq_remap
, NULL
);
979 static void blk_unregister_tracepoints(void)
981 unregister_trace_block_rq_remap(blk_add_trace_rq_remap
, NULL
);
982 unregister_trace_block_remap(blk_add_trace_remap
, NULL
);
983 unregister_trace_block_split(blk_add_trace_split
, NULL
);
984 unregister_trace_block_unplug_io(blk_add_trace_unplug_io
, NULL
);
985 unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer
, NULL
);
986 unregister_trace_block_plug(blk_add_trace_plug
, NULL
);
987 unregister_trace_block_sleeprq(blk_add_trace_sleeprq
, NULL
);
988 unregister_trace_block_getrq(blk_add_trace_getrq
, NULL
);
989 unregister_trace_block_bio_queue(blk_add_trace_bio_queue
, NULL
);
990 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge
, NULL
);
991 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge
, NULL
);
992 unregister_trace_block_bio_complete(blk_add_trace_bio_complete
, NULL
);
993 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce
, NULL
);
994 unregister_trace_block_rq_complete(blk_add_trace_rq_complete
, NULL
);
995 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue
, NULL
);
996 unregister_trace_block_rq_issue(blk_add_trace_rq_issue
, NULL
);
997 unregister_trace_block_rq_insert(blk_add_trace_rq_insert
, NULL
);
998 unregister_trace_block_rq_abort(blk_add_trace_rq_abort
, NULL
);
1000 tracepoint_synchronize_unregister();
1004 * struct blk_io_tracer formatting routines
1007 static void fill_rwbs(char *rwbs
, const struct blk_io_trace
*t
)
1010 int tc
= t
->action
>> BLK_TC_SHIFT
;
1012 if (t
->action
== BLK_TN_MESSAGE
) {
1017 if (tc
& BLK_TC_DISCARD
)
1019 else if (tc
& BLK_TC_WRITE
)
1026 if (tc
& BLK_TC_AHEAD
)
1028 if (tc
& BLK_TC_BARRIER
)
1030 if (tc
& BLK_TC_SYNC
)
1032 if (tc
& BLK_TC_META
)
1039 const struct blk_io_trace
*te_blk_io_trace(const struct trace_entry
*ent
)
1041 return (const struct blk_io_trace
*)ent
;
1044 static inline const void *pdu_start(const struct trace_entry
*ent
)
1046 return te_blk_io_trace(ent
) + 1;
1049 static inline u32
t_action(const struct trace_entry
*ent
)
1051 return te_blk_io_trace(ent
)->action
;
1054 static inline u32
t_bytes(const struct trace_entry
*ent
)
1056 return te_blk_io_trace(ent
)->bytes
;
1059 static inline u32
t_sec(const struct trace_entry
*ent
)
1061 return te_blk_io_trace(ent
)->bytes
>> 9;
1064 static inline unsigned long long t_sector(const struct trace_entry
*ent
)
1066 return te_blk_io_trace(ent
)->sector
;
1069 static inline __u16
t_error(const struct trace_entry
*ent
)
1071 return te_blk_io_trace(ent
)->error
;
1074 static __u64
get_pdu_int(const struct trace_entry
*ent
)
1076 const __u64
*val
= pdu_start(ent
);
1077 return be64_to_cpu(*val
);
1080 static void get_pdu_remap(const struct trace_entry
*ent
,
1081 struct blk_io_trace_remap
*r
)
1083 const struct blk_io_trace_remap
*__r
= pdu_start(ent
);
1084 __u64 sector_from
= __r
->sector_from
;
1086 r
->device_from
= be32_to_cpu(__r
->device_from
);
1087 r
->device_to
= be32_to_cpu(__r
->device_to
);
1088 r
->sector_from
= be64_to_cpu(sector_from
);
1091 typedef int (blk_log_action_t
) (struct trace_iterator
*iter
, const char *act
);
1093 static int blk_log_action_classic(struct trace_iterator
*iter
, const char *act
)
1096 unsigned long long ts
= iter
->ts
;
1097 unsigned long nsec_rem
= do_div(ts
, NSEC_PER_SEC
);
1098 unsigned secs
= (unsigned long)ts
;
1099 const struct blk_io_trace
*t
= te_blk_io_trace(iter
->ent
);
1103 return trace_seq_printf(&iter
->seq
,
1104 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
1105 MAJOR(t
->device
), MINOR(t
->device
), iter
->cpu
,
1106 secs
, nsec_rem
, iter
->ent
->pid
, act
, rwbs
);
1109 static int blk_log_action(struct trace_iterator
*iter
, const char *act
)
1112 const struct blk_io_trace
*t
= te_blk_io_trace(iter
->ent
);
1115 return trace_seq_printf(&iter
->seq
, "%3d,%-3d %2s %3s ",
1116 MAJOR(t
->device
), MINOR(t
->device
), act
, rwbs
);
1119 static int blk_log_dump_pdu(struct trace_seq
*s
, const struct trace_entry
*ent
)
1121 const unsigned char *pdu_buf
;
1125 pdu_buf
= pdu_start(ent
);
1126 pdu_len
= te_blk_io_trace(ent
)->pdu_len
;
1131 /* find the last zero that needs to be printed */
1132 for (end
= pdu_len
- 1; end
>= 0; end
--)
1137 if (!trace_seq_putc(s
, '('))
1140 for (i
= 0; i
< pdu_len
; i
++) {
1142 ret
= trace_seq_printf(s
, "%s%02x",
1143 i
== 0 ? "" : " ", pdu_buf
[i
]);
1148 * stop when the rest is just zeroes and indicate so
1149 * with a ".." appended
1151 if (i
== end
&& end
!= pdu_len
- 1)
1152 return trace_seq_puts(s
, " ..) ");
1155 return trace_seq_puts(s
, ") ");
1158 static int blk_log_generic(struct trace_seq
*s
, const struct trace_entry
*ent
)
1160 char cmd
[TASK_COMM_LEN
];
1162 trace_find_cmdline(ent
->pid
, cmd
);
1164 if (t_action(ent
) & BLK_TC_ACT(BLK_TC_PC
)) {
1167 ret
= trace_seq_printf(s
, "%u ", t_bytes(ent
));
1170 ret
= blk_log_dump_pdu(s
, ent
);
1173 return trace_seq_printf(s
, "[%s]\n", cmd
);
1176 return trace_seq_printf(s
, "%llu + %u [%s]\n",
1177 t_sector(ent
), t_sec(ent
), cmd
);
1178 return trace_seq_printf(s
, "[%s]\n", cmd
);
1182 static int blk_log_with_error(struct trace_seq
*s
,
1183 const struct trace_entry
*ent
)
1185 if (t_action(ent
) & BLK_TC_ACT(BLK_TC_PC
)) {
1188 ret
= blk_log_dump_pdu(s
, ent
);
1190 return trace_seq_printf(s
, "[%d]\n", t_error(ent
));
1194 return trace_seq_printf(s
, "%llu + %u [%d]\n",
1196 t_sec(ent
), t_error(ent
));
1197 return trace_seq_printf(s
, "%llu [%d]\n",
1198 t_sector(ent
), t_error(ent
));
1202 static int blk_log_remap(struct trace_seq
*s
, const struct trace_entry
*ent
)
1204 struct blk_io_trace_remap r
= { .device_from
= 0, };
1206 get_pdu_remap(ent
, &r
);
1207 return trace_seq_printf(s
, "%llu + %u <- (%d,%d) %llu\n",
1208 t_sector(ent
), t_sec(ent
),
1209 MAJOR(r
.device_from
), MINOR(r
.device_from
),
1210 (unsigned long long)r
.sector_from
);
1213 static int blk_log_plug(struct trace_seq
*s
, const struct trace_entry
*ent
)
1215 char cmd
[TASK_COMM_LEN
];
1217 trace_find_cmdline(ent
->pid
, cmd
);
1219 return trace_seq_printf(s
, "[%s]\n", cmd
);
1222 static int blk_log_unplug(struct trace_seq
*s
, const struct trace_entry
*ent
)
1224 char cmd
[TASK_COMM_LEN
];
1226 trace_find_cmdline(ent
->pid
, cmd
);
1228 return trace_seq_printf(s
, "[%s] %llu\n", cmd
, get_pdu_int(ent
));
1231 static int blk_log_split(struct trace_seq
*s
, const struct trace_entry
*ent
)
1233 char cmd
[TASK_COMM_LEN
];
1235 trace_find_cmdline(ent
->pid
, cmd
);
1237 return trace_seq_printf(s
, "%llu / %llu [%s]\n", t_sector(ent
),
1238 get_pdu_int(ent
), cmd
);
1241 static int blk_log_msg(struct trace_seq
*s
, const struct trace_entry
*ent
)
1244 const struct blk_io_trace
*t
= te_blk_io_trace(ent
);
1246 ret
= trace_seq_putmem(s
, t
+ 1, t
->pdu_len
);
1248 return trace_seq_putc(s
, '\n');
1253 * struct tracer operations
1256 static void blk_tracer_print_header(struct seq_file
*m
)
1258 if (!(blk_tracer_flags
.val
& TRACE_BLK_OPT_CLASSIC
))
1260 seq_puts(m
, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1264 static void blk_tracer_start(struct trace_array
*tr
)
1266 blk_tracer_enabled
= true;
1269 static int blk_tracer_init(struct trace_array
*tr
)
1272 blk_tracer_start(tr
);
1276 static void blk_tracer_stop(struct trace_array
*tr
)
1278 blk_tracer_enabled
= false;
1281 static void blk_tracer_reset(struct trace_array
*tr
)
1283 blk_tracer_stop(tr
);
1286 static const struct {
1288 int (*print
)(struct trace_seq
*s
, const struct trace_entry
*ent
);
1290 [__BLK_TA_QUEUE
] = {{ "Q", "queue" }, blk_log_generic
},
1291 [__BLK_TA_BACKMERGE
] = {{ "M", "backmerge" }, blk_log_generic
},
1292 [__BLK_TA_FRONTMERGE
] = {{ "F", "frontmerge" }, blk_log_generic
},
1293 [__BLK_TA_GETRQ
] = {{ "G", "getrq" }, blk_log_generic
},
1294 [__BLK_TA_SLEEPRQ
] = {{ "S", "sleeprq" }, blk_log_generic
},
1295 [__BLK_TA_REQUEUE
] = {{ "R", "requeue" }, blk_log_with_error
},
1296 [__BLK_TA_ISSUE
] = {{ "D", "issue" }, blk_log_generic
},
1297 [__BLK_TA_COMPLETE
] = {{ "C", "complete" }, blk_log_with_error
},
1298 [__BLK_TA_PLUG
] = {{ "P", "plug" }, blk_log_plug
},
1299 [__BLK_TA_UNPLUG_IO
] = {{ "U", "unplug_io" }, blk_log_unplug
},
1300 [__BLK_TA_UNPLUG_TIMER
] = {{ "UT", "unplug_timer" }, blk_log_unplug
},
1301 [__BLK_TA_INSERT
] = {{ "I", "insert" }, blk_log_generic
},
1302 [__BLK_TA_SPLIT
] = {{ "X", "split" }, blk_log_split
},
1303 [__BLK_TA_BOUNCE
] = {{ "B", "bounce" }, blk_log_generic
},
1304 [__BLK_TA_REMAP
] = {{ "A", "remap" }, blk_log_remap
},
1307 static enum print_line_t
print_one_line(struct trace_iterator
*iter
,
1310 struct trace_seq
*s
= &iter
->seq
;
1311 const struct blk_io_trace
*t
;
1315 blk_log_action_t
*log_action
;
1317 t
= te_blk_io_trace(iter
->ent
);
1318 what
= t
->action
& ((1 << BLK_TC_SHIFT
) - 1);
1319 long_act
= !!(trace_flags
& TRACE_ITER_VERBOSE
);
1320 log_action
= classic
? &blk_log_action_classic
: &blk_log_action
;
1322 if (t
->action
== BLK_TN_MESSAGE
) {
1323 ret
= log_action(iter
, long_act
? "message" : "m");
1325 ret
= blk_log_msg(s
, iter
->ent
);
1329 if (unlikely(what
== 0 || what
>= ARRAY_SIZE(what2act
)))
1330 ret
= trace_seq_printf(s
, "Unknown action %x\n", what
);
1332 ret
= log_action(iter
, what2act
[what
].act
[long_act
]);
1334 ret
= what2act
[what
].print(s
, iter
->ent
);
1337 return ret
? TRACE_TYPE_HANDLED
: TRACE_TYPE_PARTIAL_LINE
;
1340 static enum print_line_t
blk_trace_event_print(struct trace_iterator
*iter
,
1341 int flags
, struct trace_event
*event
)
1343 return print_one_line(iter
, false);
1346 static int blk_trace_synthesize_old_trace(struct trace_iterator
*iter
)
1348 struct trace_seq
*s
= &iter
->seq
;
1349 struct blk_io_trace
*t
= (struct blk_io_trace
*)iter
->ent
;
1350 const int offset
= offsetof(struct blk_io_trace
, sector
);
1351 struct blk_io_trace old
= {
1352 .magic
= BLK_IO_TRACE_MAGIC
| BLK_IO_TRACE_VERSION
,
1356 if (!trace_seq_putmem(s
, &old
, offset
))
1358 return trace_seq_putmem(s
, &t
->sector
,
1359 sizeof(old
) - offset
+ t
->pdu_len
);
1362 static enum print_line_t
1363 blk_trace_event_print_binary(struct trace_iterator
*iter
, int flags
,
1364 struct trace_event
*event
)
1366 return blk_trace_synthesize_old_trace(iter
) ?
1367 TRACE_TYPE_HANDLED
: TRACE_TYPE_PARTIAL_LINE
;
1370 static enum print_line_t
blk_tracer_print_line(struct trace_iterator
*iter
)
1372 if (!(blk_tracer_flags
.val
& TRACE_BLK_OPT_CLASSIC
))
1373 return TRACE_TYPE_UNHANDLED
;
1375 return print_one_line(iter
, true);
1378 static int blk_tracer_set_flag(u32 old_flags
, u32 bit
, int set
)
1380 /* don't output context-info for blk_classic output */
1381 if (bit
== TRACE_BLK_OPT_CLASSIC
) {
1383 trace_flags
&= ~TRACE_ITER_CONTEXT_INFO
;
1385 trace_flags
|= TRACE_ITER_CONTEXT_INFO
;
1390 static struct tracer blk_tracer __read_mostly
= {
1392 .init
= blk_tracer_init
,
1393 .reset
= blk_tracer_reset
,
1394 .start
= blk_tracer_start
,
1395 .stop
= blk_tracer_stop
,
1396 .print_header
= blk_tracer_print_header
,
1397 .print_line
= blk_tracer_print_line
,
1398 .flags
= &blk_tracer_flags
,
1399 .set_flag
= blk_tracer_set_flag
,
1402 static struct trace_event_functions trace_blk_event_funcs
= {
1403 .trace
= blk_trace_event_print
,
1404 .binary
= blk_trace_event_print_binary
,
1407 static struct trace_event trace_blk_event
= {
1409 .funcs
= &trace_blk_event_funcs
,
1412 static int __init
init_blk_tracer(void)
1414 if (!register_ftrace_event(&trace_blk_event
)) {
1415 pr_warning("Warning: could not register block events\n");
1419 if (register_tracer(&blk_tracer
) != 0) {
1420 pr_warning("Warning: could not register the block tracer\n");
1421 unregister_ftrace_event(&trace_blk_event
);
1428 device_initcall(init_blk_tracer
);
1430 static int blk_trace_remove_queue(struct request_queue
*q
)
1432 struct blk_trace
*bt
;
1434 bt
= xchg(&q
->blk_trace
, NULL
);
1438 if (atomic_dec_and_test(&blk_probes_ref
))
1439 blk_unregister_tracepoints();
1446 * Setup everything required to start tracing
1448 static int blk_trace_setup_queue(struct request_queue
*q
,
1449 struct block_device
*bdev
)
1451 struct blk_trace
*old_bt
, *bt
= NULL
;
1454 bt
= kzalloc(sizeof(*bt
), GFP_KERNEL
);
1458 bt
->msg_data
= __alloc_percpu(BLK_TN_MAX_MSG
, __alignof__(char));
1462 bt
->dev
= bdev
->bd_dev
;
1463 bt
->act_mask
= (u16
)-1;
1465 blk_trace_setup_lba(bt
, bdev
);
1467 old_bt
= xchg(&q
->blk_trace
, bt
);
1468 if (old_bt
!= NULL
) {
1469 (void)xchg(&q
->blk_trace
, old_bt
);
1474 if (atomic_inc_return(&blk_probes_ref
) == 1)
1475 blk_register_tracepoints();
1484 * sysfs interface to enable and configure tracing
1487 static ssize_t
sysfs_blk_trace_attr_show(struct device
*dev
,
1488 struct device_attribute
*attr
,
1490 static ssize_t
sysfs_blk_trace_attr_store(struct device
*dev
,
1491 struct device_attribute
*attr
,
1492 const char *buf
, size_t count
);
1493 #define BLK_TRACE_DEVICE_ATTR(_name) \
1494 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1495 sysfs_blk_trace_attr_show, \
1496 sysfs_blk_trace_attr_store)
1498 static BLK_TRACE_DEVICE_ATTR(enable
);
1499 static BLK_TRACE_DEVICE_ATTR(act_mask
);
1500 static BLK_TRACE_DEVICE_ATTR(pid
);
1501 static BLK_TRACE_DEVICE_ATTR(start_lba
);
1502 static BLK_TRACE_DEVICE_ATTR(end_lba
);
1504 static struct attribute
*blk_trace_attrs
[] = {
1505 &dev_attr_enable
.attr
,
1506 &dev_attr_act_mask
.attr
,
1508 &dev_attr_start_lba
.attr
,
1509 &dev_attr_end_lba
.attr
,
1513 struct attribute_group blk_trace_attr_group
= {
1515 .attrs
= blk_trace_attrs
,
1518 static const struct {
1522 { BLK_TC_READ
, "read" },
1523 { BLK_TC_WRITE
, "write" },
1524 { BLK_TC_BARRIER
, "barrier" },
1525 { BLK_TC_SYNC
, "sync" },
1526 { BLK_TC_QUEUE
, "queue" },
1527 { BLK_TC_REQUEUE
, "requeue" },
1528 { BLK_TC_ISSUE
, "issue" },
1529 { BLK_TC_COMPLETE
, "complete" },
1530 { BLK_TC_FS
, "fs" },
1531 { BLK_TC_PC
, "pc" },
1532 { BLK_TC_AHEAD
, "ahead" },
1533 { BLK_TC_META
, "meta" },
1534 { BLK_TC_DISCARD
, "discard" },
1535 { BLK_TC_DRV_DATA
, "drv_data" },
1538 static int blk_trace_str2mask(const char *str
)
1542 char *buf
, *s
, *token
;
1544 buf
= kstrdup(str
, GFP_KERNEL
);
1550 token
= strsep(&s
, ",");
1557 for (i
= 0; i
< ARRAY_SIZE(mask_maps
); i
++) {
1558 if (strcasecmp(token
, mask_maps
[i
].str
) == 0) {
1559 mask
|= mask_maps
[i
].mask
;
1563 if (i
== ARRAY_SIZE(mask_maps
)) {
1573 static ssize_t
blk_trace_mask2str(char *buf
, int mask
)
1578 for (i
= 0; i
< ARRAY_SIZE(mask_maps
); i
++) {
1579 if (mask
& mask_maps
[i
].mask
) {
1580 p
+= sprintf(p
, "%s%s",
1581 (p
== buf
) ? "" : ",", mask_maps
[i
].str
);
1589 static struct request_queue
*blk_trace_get_queue(struct block_device
*bdev
)
1591 if (bdev
->bd_disk
== NULL
)
1594 return bdev_get_queue(bdev
);
1597 static ssize_t
sysfs_blk_trace_attr_show(struct device
*dev
,
1598 struct device_attribute
*attr
,
1601 struct hd_struct
*p
= dev_to_part(dev
);
1602 struct request_queue
*q
;
1603 struct block_device
*bdev
;
1604 ssize_t ret
= -ENXIO
;
1607 bdev
= bdget(part_devt(p
));
1609 goto out_unlock_kernel
;
1611 q
= blk_trace_get_queue(bdev
);
1615 mutex_lock(&bdev
->bd_mutex
);
1617 if (attr
== &dev_attr_enable
) {
1618 ret
= sprintf(buf
, "%u\n", !!q
->blk_trace
);
1619 goto out_unlock_bdev
;
1622 if (q
->blk_trace
== NULL
)
1623 ret
= sprintf(buf
, "disabled\n");
1624 else if (attr
== &dev_attr_act_mask
)
1625 ret
= blk_trace_mask2str(buf
, q
->blk_trace
->act_mask
);
1626 else if (attr
== &dev_attr_pid
)
1627 ret
= sprintf(buf
, "%u\n", q
->blk_trace
->pid
);
1628 else if (attr
== &dev_attr_start_lba
)
1629 ret
= sprintf(buf
, "%llu\n", q
->blk_trace
->start_lba
);
1630 else if (attr
== &dev_attr_end_lba
)
1631 ret
= sprintf(buf
, "%llu\n", q
->blk_trace
->end_lba
);
1634 mutex_unlock(&bdev
->bd_mutex
);
1642 static ssize_t
sysfs_blk_trace_attr_store(struct device
*dev
,
1643 struct device_attribute
*attr
,
1644 const char *buf
, size_t count
)
1646 struct block_device
*bdev
;
1647 struct request_queue
*q
;
1648 struct hd_struct
*p
;
1650 ssize_t ret
= -EINVAL
;
1655 if (attr
== &dev_attr_act_mask
) {
1656 if (sscanf(buf
, "%llx", &value
) != 1) {
1657 /* Assume it is a list of trace category names */
1658 ret
= blk_trace_str2mask(buf
);
1663 } else if (sscanf(buf
, "%llu", &value
) != 1)
1669 p
= dev_to_part(dev
);
1670 bdev
= bdget(part_devt(p
));
1672 goto out_unlock_kernel
;
1674 q
= blk_trace_get_queue(bdev
);
1678 mutex_lock(&bdev
->bd_mutex
);
1680 if (attr
== &dev_attr_enable
) {
1682 ret
= blk_trace_setup_queue(q
, bdev
);
1684 ret
= blk_trace_remove_queue(q
);
1685 goto out_unlock_bdev
;
1689 if (q
->blk_trace
== NULL
)
1690 ret
= blk_trace_setup_queue(q
, bdev
);
1693 if (attr
== &dev_attr_act_mask
)
1694 q
->blk_trace
->act_mask
= value
;
1695 else if (attr
== &dev_attr_pid
)
1696 q
->blk_trace
->pid
= value
;
1697 else if (attr
== &dev_attr_start_lba
)
1698 q
->blk_trace
->start_lba
= value
;
1699 else if (attr
== &dev_attr_end_lba
)
1700 q
->blk_trace
->end_lba
= value
;
1704 mutex_unlock(&bdev
->bd_mutex
);
1710 return ret
? ret
: count
;
1713 int blk_trace_init_sysfs(struct device
*dev
)
1715 return sysfs_create_group(&dev
->kobj
, &blk_trace_attr_group
);
1718 void blk_trace_remove_sysfs(struct device
*dev
)
1720 sysfs_remove_group(&dev
->kobj
, &blk_trace_attr_group
);
1723 #endif /* CONFIG_BLK_DEV_IO_TRACE */
1725 #ifdef CONFIG_EVENT_TRACING
1727 void blk_dump_cmd(char *buf
, struct request
*rq
)
1730 int len
= rq
->cmd_len
;
1731 unsigned char *cmd
= rq
->cmd
;
1733 if (!blk_pc_request(rq
)) {
1738 for (end
= len
- 1; end
>= 0; end
--)
1743 for (i
= 0; i
< len
; i
++) {
1744 buf
+= sprintf(buf
, "%s%02x", i
== 0 ? "" : " ", cmd
[i
]);
1745 if (i
== end
&& end
!= len
- 1) {
1746 sprintf(buf
, " ..");
1752 void blk_fill_rwbs(char *rwbs
, u32 rw
, int bytes
)
1758 else if (rw
& 1 << BIO_RW_DISCARD
)
1765 if (rw
& 1 << BIO_RW_AHEAD
)
1767 if (rw
& 1 << BIO_RW_BARRIER
)
1769 if (rw
& 1 << BIO_RW_SYNCIO
)
1771 if (rw
& 1 << BIO_RW_META
)
1777 void blk_fill_rwbs_rq(char *rwbs
, struct request
*rq
)
1779 int rw
= rq
->cmd_flags
& 0x03;
1782 if (blk_discard_rq(rq
))
1783 rw
|= (1 << BIO_RW_DISCARD
);
1785 bytes
= blk_rq_bytes(rq
);
1787 blk_fill_rwbs(rwbs
, rw
, bytes
);
1790 #endif /* CONFIG_EVENT_TRACING */