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/export.h>
27 #include <linux/time.h>
28 #include <linux/uaccess.h>
29 #include <linux/list.h>
31 #include "../../block/blk.h"
33 #include <trace/events/block.h>
35 #include "trace_output.h"
37 #ifdef CONFIG_BLK_DEV_IO_TRACE
39 static unsigned int blktrace_seq __read_mostly
= 1;
41 static struct trace_array
*blk_tr
;
42 static bool blk_tracer_enabled __read_mostly
;
44 static LIST_HEAD(running_trace_list
);
45 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(running_trace_lock
);
47 /* Select an alternative, minimalistic output than the original one */
48 #define TRACE_BLK_OPT_CLASSIC 0x1
50 static struct tracer_opt blk_tracer_opts
[] = {
51 /* Default disable the minimalistic output */
52 { TRACER_OPT(blk_classic
, TRACE_BLK_OPT_CLASSIC
) },
56 static struct tracer_flags blk_tracer_flags
= {
58 .opts
= blk_tracer_opts
,
61 /* Global reference count of probes */
62 static atomic_t blk_probes_ref
= ATOMIC_INIT(0);
64 static void blk_register_tracepoints(void);
65 static void blk_unregister_tracepoints(void);
68 * Send out a notify message.
70 static void trace_note(struct blk_trace
*bt
, pid_t pid
, int action
,
71 const void *data
, size_t len
)
73 struct blk_io_trace
*t
;
74 struct ring_buffer_event
*event
= NULL
;
75 struct ring_buffer
*buffer
= NULL
;
77 int cpu
= smp_processor_id();
78 bool blk_tracer
= blk_tracer_enabled
;
81 buffer
= blk_tr
->trace_buffer
.buffer
;
83 event
= trace_buffer_lock_reserve(buffer
, TRACE_BLK
,
88 t
= ring_buffer_event_data(event
);
95 t
= relay_reserve(bt
->rchan
, sizeof(*t
) + len
);
97 t
->magic
= BLK_IO_TRACE_MAGIC
| BLK_IO_TRACE_VERSION
;
98 t
->time
= ktime_to_ns(ktime_get());
105 memcpy((void *) t
+ sizeof(*t
), data
, len
);
108 trace_buffer_unlock_commit(blk_tr
, buffer
, event
, 0, pc
);
113 * Send out a notify for this process, if we haven't done so since a trace
116 static void trace_note_tsk(struct task_struct
*tsk
)
119 struct blk_trace
*bt
;
121 tsk
->btrace_seq
= blktrace_seq
;
122 spin_lock_irqsave(&running_trace_lock
, flags
);
123 list_for_each_entry(bt
, &running_trace_list
, running_list
) {
124 trace_note(bt
, tsk
->pid
, BLK_TN_PROCESS
, tsk
->comm
,
127 spin_unlock_irqrestore(&running_trace_lock
, flags
);
130 static void trace_note_time(struct blk_trace
*bt
)
132 struct timespec64 now
;
136 /* need to check user space to see if this breaks in y2038 or y2106 */
137 ktime_get_real_ts64(&now
);
138 words
[0] = (u32
)now
.tv_sec
;
139 words
[1] = now
.tv_nsec
;
141 local_irq_save(flags
);
142 trace_note(bt
, 0, BLK_TN_TIMESTAMP
, words
, sizeof(words
));
143 local_irq_restore(flags
);
146 void __trace_note_message(struct blk_trace
*bt
, const char *fmt
, ...)
153 if (unlikely(bt
->trace_state
!= Blktrace_running
&&
154 !blk_tracer_enabled
))
158 * If the BLK_TC_NOTIFY action mask isn't set, don't send any note
159 * message to the trace.
161 if (!(bt
->act_mask
& BLK_TC_NOTIFY
))
164 local_irq_save(flags
);
165 buf
= this_cpu_ptr(bt
->msg_data
);
167 n
= vscnprintf(buf
, BLK_TN_MAX_MSG
, fmt
, args
);
170 trace_note(bt
, 0, BLK_TN_MESSAGE
, buf
, n
);
171 local_irq_restore(flags
);
173 EXPORT_SYMBOL_GPL(__trace_note_message
);
175 static int act_log_check(struct blk_trace
*bt
, u32 what
, sector_t sector
,
178 if (((bt
->act_mask
<< BLK_TC_SHIFT
) & what
) == 0)
180 if (sector
&& (sector
< bt
->start_lba
|| sector
> bt
->end_lba
))
182 if (bt
->pid
&& pid
!= bt
->pid
)
189 * Data direction bit lookup
191 static const u32 ddir_act
[2] = { BLK_TC_ACT(BLK_TC_READ
),
192 BLK_TC_ACT(BLK_TC_WRITE
) };
194 #define BLK_TC_RAHEAD BLK_TC_AHEAD
195 #define BLK_TC_PREFLUSH BLK_TC_FLUSH
197 /* The ilog2() calls fall out because they're constant */
198 #define MASK_TC_BIT(rw, __name) ((rw & REQ_ ## __name) << \
199 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - __REQ_ ## __name))
202 * The worker for the various blk_add_trace*() types. Fills out a
203 * blk_io_trace structure and places it in a per-cpu subbuffer.
205 static void __blk_add_trace(struct blk_trace
*bt
, sector_t sector
, int bytes
,
206 int op
, int op_flags
, u32 what
, int error
, int pdu_len
,
209 struct task_struct
*tsk
= current
;
210 struct ring_buffer_event
*event
= NULL
;
211 struct ring_buffer
*buffer
= NULL
;
212 struct blk_io_trace
*t
;
213 unsigned long flags
= 0;
214 unsigned long *sequence
;
217 bool blk_tracer
= blk_tracer_enabled
;
219 if (unlikely(bt
->trace_state
!= Blktrace_running
&& !blk_tracer
))
222 what
|= ddir_act
[op_is_write(op
) ? WRITE
: READ
];
223 what
|= MASK_TC_BIT(op_flags
, SYNC
);
224 what
|= MASK_TC_BIT(op_flags
, RAHEAD
);
225 what
|= MASK_TC_BIT(op_flags
, META
);
226 what
|= MASK_TC_BIT(op_flags
, PREFLUSH
);
227 what
|= MASK_TC_BIT(op_flags
, FUA
);
228 if (op
== REQ_OP_DISCARD
|| op
== REQ_OP_SECURE_ERASE
)
229 what
|= BLK_TC_ACT(BLK_TC_DISCARD
);
230 if (op
== REQ_OP_FLUSH
)
231 what
|= BLK_TC_ACT(BLK_TC_FLUSH
);
234 if (act_log_check(bt
, what
, sector
, pid
))
236 cpu
= raw_smp_processor_id();
239 tracing_record_cmdline(current
);
241 buffer
= blk_tr
->trace_buffer
.buffer
;
242 pc
= preempt_count();
243 event
= trace_buffer_lock_reserve(buffer
, TRACE_BLK
,
244 sizeof(*t
) + pdu_len
,
248 t
= ring_buffer_event_data(event
);
252 if (unlikely(tsk
->btrace_seq
!= blktrace_seq
))
256 * A word about the locking here - we disable interrupts to reserve
257 * some space in the relay per-cpu buffer, to prevent an irq
258 * from coming in and stepping on our toes.
260 local_irq_save(flags
);
261 t
= relay_reserve(bt
->rchan
, sizeof(*t
) + pdu_len
);
263 sequence
= per_cpu_ptr(bt
->sequence
, cpu
);
265 t
->magic
= BLK_IO_TRACE_MAGIC
| BLK_IO_TRACE_VERSION
;
266 t
->sequence
= ++(*sequence
);
267 t
->time
= ktime_to_ns(ktime_get());
270 * These two are not needed in ftrace as they are in the
271 * generic trace_entry, filled by tracing_generic_entry_update,
272 * but for the trace_event->bin() synthesizer benefit we do it
283 t
->pdu_len
= pdu_len
;
286 memcpy((void *) t
+ sizeof(*t
), pdu_data
, pdu_len
);
289 trace_buffer_unlock_commit(blk_tr
, buffer
, event
, 0, pc
);
294 local_irq_restore(flags
);
297 static void blk_trace_free(struct blk_trace
*bt
)
299 debugfs_remove(bt
->msg_file
);
300 debugfs_remove(bt
->dropped_file
);
301 relay_close(bt
->rchan
);
302 debugfs_remove(bt
->dir
);
303 free_percpu(bt
->sequence
);
304 free_percpu(bt
->msg_data
);
308 static void blk_trace_cleanup(struct blk_trace
*bt
)
311 if (atomic_dec_and_test(&blk_probes_ref
))
312 blk_unregister_tracepoints();
315 int blk_trace_remove(struct request_queue
*q
)
317 struct blk_trace
*bt
;
319 bt
= xchg(&q
->blk_trace
, NULL
);
323 if (bt
->trace_state
!= Blktrace_running
)
324 blk_trace_cleanup(bt
);
328 EXPORT_SYMBOL_GPL(blk_trace_remove
);
330 static ssize_t
blk_dropped_read(struct file
*filp
, char __user
*buffer
,
331 size_t count
, loff_t
*ppos
)
333 struct blk_trace
*bt
= filp
->private_data
;
336 snprintf(buf
, sizeof(buf
), "%u\n", atomic_read(&bt
->dropped
));
338 return simple_read_from_buffer(buffer
, count
, ppos
, buf
, strlen(buf
));
341 static const struct file_operations blk_dropped_fops
= {
342 .owner
= THIS_MODULE
,
344 .read
= blk_dropped_read
,
345 .llseek
= default_llseek
,
348 static ssize_t
blk_msg_write(struct file
*filp
, const char __user
*buffer
,
349 size_t count
, loff_t
*ppos
)
352 struct blk_trace
*bt
;
354 if (count
>= BLK_TN_MAX_MSG
)
357 msg
= memdup_user_nul(buffer
, count
);
361 bt
= filp
->private_data
;
362 __trace_note_message(bt
, "%s", msg
);
368 static const struct file_operations blk_msg_fops
= {
369 .owner
= THIS_MODULE
,
371 .write
= blk_msg_write
,
372 .llseek
= noop_llseek
,
376 * Keep track of how many times we encountered a full subbuffer, to aid
377 * the user space app in telling how many lost events there were.
379 static int blk_subbuf_start_callback(struct rchan_buf
*buf
, void *subbuf
,
380 void *prev_subbuf
, size_t prev_padding
)
382 struct blk_trace
*bt
;
384 if (!relay_buf_full(buf
))
387 bt
= buf
->chan
->private_data
;
388 atomic_inc(&bt
->dropped
);
392 static int blk_remove_buf_file_callback(struct dentry
*dentry
)
394 debugfs_remove(dentry
);
399 static struct dentry
*blk_create_buf_file_callback(const char *filename
,
400 struct dentry
*parent
,
402 struct rchan_buf
*buf
,
405 return debugfs_create_file(filename
, mode
, parent
, buf
,
406 &relay_file_operations
);
409 static struct rchan_callbacks blk_relay_callbacks
= {
410 .subbuf_start
= blk_subbuf_start_callback
,
411 .create_buf_file
= blk_create_buf_file_callback
,
412 .remove_buf_file
= blk_remove_buf_file_callback
,
415 static void blk_trace_setup_lba(struct blk_trace
*bt
,
416 struct block_device
*bdev
)
418 struct hd_struct
*part
= NULL
;
421 part
= bdev
->bd_part
;
424 bt
->start_lba
= part
->start_sect
;
425 bt
->end_lba
= part
->start_sect
+ part
->nr_sects
;
433 * Setup everything required to start tracing
435 static int do_blk_trace_setup(struct request_queue
*q
, char *name
, dev_t dev
,
436 struct block_device
*bdev
,
437 struct blk_user_trace_setup
*buts
)
439 struct blk_trace
*bt
= NULL
;
440 struct dentry
*dir
= NULL
;
443 if (!buts
->buf_size
|| !buts
->buf_nr
)
446 strncpy(buts
->name
, name
, BLKTRACE_BDEV_SIZE
);
447 buts
->name
[BLKTRACE_BDEV_SIZE
- 1] = '\0';
450 * some device names have larger paths - convert the slashes
451 * to underscores for this to work as expected
453 strreplace(buts
->name
, '/', '_');
455 bt
= kzalloc(sizeof(*bt
), GFP_KERNEL
);
460 bt
->sequence
= alloc_percpu(unsigned long);
464 bt
->msg_data
= __alloc_percpu(BLK_TN_MAX_MSG
, __alignof__(char));
470 if (!blk_debugfs_root
)
473 dir
= debugfs_lookup(buts
->name
, blk_debugfs_root
);
475 bt
->dir
= dir
= debugfs_create_dir(buts
->name
, blk_debugfs_root
);
480 atomic_set(&bt
->dropped
, 0);
481 INIT_LIST_HEAD(&bt
->running_list
);
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 if (cmpxchg(&q
->blk_trace
, NULL
, bt
))
517 if (atomic_inc_return(&blk_probes_ref
) == 1)
518 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 #if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64)
553 static int compat_blk_trace_setup(struct request_queue
*q
, char *name
,
554 dev_t dev
, struct block_device
*bdev
,
557 struct blk_user_trace_setup buts
;
558 struct compat_blk_user_trace_setup cbuts
;
561 if (copy_from_user(&cbuts
, arg
, sizeof(cbuts
)))
564 buts
= (struct blk_user_trace_setup
) {
565 .act_mask
= cbuts
.act_mask
,
566 .buf_size
= cbuts
.buf_size
,
567 .buf_nr
= cbuts
.buf_nr
,
568 .start_lba
= cbuts
.start_lba
,
569 .end_lba
= cbuts
.end_lba
,
573 ret
= do_blk_trace_setup(q
, name
, dev
, bdev
, &buts
);
577 if (copy_to_user(arg
, &buts
.name
, ARRAY_SIZE(buts
.name
))) {
586 int blk_trace_startstop(struct request_queue
*q
, int start
)
589 struct blk_trace
*bt
= q
->blk_trace
;
595 * For starting a trace, we can transition from a setup or stopped
596 * trace. For stopping a trace, the state must be running
600 if (bt
->trace_state
== Blktrace_setup
||
601 bt
->trace_state
== Blktrace_stopped
) {
604 bt
->trace_state
= Blktrace_running
;
605 spin_lock_irq(&running_trace_lock
);
606 list_add(&bt
->running_list
, &running_trace_list
);
607 spin_unlock_irq(&running_trace_lock
);
613 if (bt
->trace_state
== Blktrace_running
) {
614 bt
->trace_state
= Blktrace_stopped
;
615 spin_lock_irq(&running_trace_lock
);
616 list_del_init(&bt
->running_list
);
617 spin_unlock_irq(&running_trace_lock
);
618 relay_flush(bt
->rchan
);
625 EXPORT_SYMBOL_GPL(blk_trace_startstop
);
628 * blk_trace_ioctl: - handle the ioctls associated with tracing
629 * @bdev: the block device
630 * @cmd: the ioctl cmd
631 * @arg: the argument data, if any
634 int blk_trace_ioctl(struct block_device
*bdev
, unsigned cmd
, char __user
*arg
)
636 struct request_queue
*q
;
638 char b
[BDEVNAME_SIZE
];
640 q
= bdev_get_queue(bdev
);
644 mutex_lock(&bdev
->bd_mutex
);
649 ret
= blk_trace_setup(q
, b
, bdev
->bd_dev
, bdev
, arg
);
651 #if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64)
652 case BLKTRACESETUP32
:
654 ret
= compat_blk_trace_setup(q
, b
, bdev
->bd_dev
, bdev
, arg
);
660 ret
= blk_trace_startstop(q
, start
);
662 case BLKTRACETEARDOWN
:
663 ret
= blk_trace_remove(q
);
670 mutex_unlock(&bdev
->bd_mutex
);
675 * blk_trace_shutdown: - stop and cleanup trace structures
676 * @q: the request queue associated with the device
679 void blk_trace_shutdown(struct request_queue
*q
)
682 blk_trace_startstop(q
, 0);
692 * blk_add_trace_rq - Add a trace for a request oriented action
693 * @rq: the source request
694 * @error: return status to log
695 * @nr_bytes: number of completed bytes
699 * Records an action against a request. Will log the bio offset + size.
702 static void blk_add_trace_rq(struct request
*rq
, int error
,
703 unsigned int nr_bytes
, u32 what
)
705 struct blk_trace
*bt
= rq
->q
->blk_trace
;
710 if (blk_rq_is_passthrough(rq
))
711 what
|= BLK_TC_ACT(BLK_TC_PC
);
713 what
|= BLK_TC_ACT(BLK_TC_FS
);
715 __blk_add_trace(bt
, blk_rq_trace_sector(rq
), nr_bytes
, req_op(rq
),
716 rq
->cmd_flags
, what
, error
, 0, NULL
);
719 static void blk_add_trace_rq_insert(void *ignore
,
720 struct request_queue
*q
, struct request
*rq
)
722 blk_add_trace_rq(rq
, 0, blk_rq_bytes(rq
), BLK_TA_INSERT
);
725 static void blk_add_trace_rq_issue(void *ignore
,
726 struct request_queue
*q
, struct request
*rq
)
728 blk_add_trace_rq(rq
, 0, blk_rq_bytes(rq
), BLK_TA_ISSUE
);
731 static void blk_add_trace_rq_requeue(void *ignore
,
732 struct request_queue
*q
,
735 blk_add_trace_rq(rq
, 0, blk_rq_bytes(rq
), BLK_TA_REQUEUE
);
738 static void blk_add_trace_rq_complete(void *ignore
, struct request
*rq
,
739 int error
, unsigned int nr_bytes
)
741 blk_add_trace_rq(rq
, error
, nr_bytes
, BLK_TA_COMPLETE
);
745 * blk_add_trace_bio - Add a trace for a bio oriented action
746 * @q: queue the io is for
747 * @bio: the source bio
749 * @error: error, if any
752 * Records an action against a bio. Will log the bio offset + size.
755 static void blk_add_trace_bio(struct request_queue
*q
, struct bio
*bio
,
758 struct blk_trace
*bt
= q
->blk_trace
;
763 __blk_add_trace(bt
, bio
->bi_iter
.bi_sector
, bio
->bi_iter
.bi_size
,
764 bio_op(bio
), bio
->bi_opf
, what
, error
, 0, NULL
);
767 static void blk_add_trace_bio_bounce(void *ignore
,
768 struct request_queue
*q
, struct bio
*bio
)
770 blk_add_trace_bio(q
, bio
, BLK_TA_BOUNCE
, 0);
773 static void blk_add_trace_bio_complete(void *ignore
,
774 struct request_queue
*q
, struct bio
*bio
,
777 blk_add_trace_bio(q
, bio
, BLK_TA_COMPLETE
, error
);
780 static void blk_add_trace_bio_backmerge(void *ignore
,
781 struct request_queue
*q
,
785 blk_add_trace_bio(q
, bio
, BLK_TA_BACKMERGE
, 0);
788 static void blk_add_trace_bio_frontmerge(void *ignore
,
789 struct request_queue
*q
,
793 blk_add_trace_bio(q
, bio
, BLK_TA_FRONTMERGE
, 0);
796 static void blk_add_trace_bio_queue(void *ignore
,
797 struct request_queue
*q
, struct bio
*bio
)
799 blk_add_trace_bio(q
, bio
, BLK_TA_QUEUE
, 0);
802 static void blk_add_trace_getrq(void *ignore
,
803 struct request_queue
*q
,
804 struct bio
*bio
, int rw
)
807 blk_add_trace_bio(q
, bio
, BLK_TA_GETRQ
, 0);
809 struct blk_trace
*bt
= q
->blk_trace
;
812 __blk_add_trace(bt
, 0, 0, rw
, 0, BLK_TA_GETRQ
, 0, 0,
818 static void blk_add_trace_sleeprq(void *ignore
,
819 struct request_queue
*q
,
820 struct bio
*bio
, int rw
)
823 blk_add_trace_bio(q
, bio
, BLK_TA_SLEEPRQ
, 0);
825 struct blk_trace
*bt
= q
->blk_trace
;
828 __blk_add_trace(bt
, 0, 0, rw
, 0, BLK_TA_SLEEPRQ
,
833 static void blk_add_trace_plug(void *ignore
, struct request_queue
*q
)
835 struct blk_trace
*bt
= q
->blk_trace
;
838 __blk_add_trace(bt
, 0, 0, 0, 0, BLK_TA_PLUG
, 0, 0, NULL
);
841 static void blk_add_trace_unplug(void *ignore
, struct request_queue
*q
,
842 unsigned int depth
, bool explicit)
844 struct blk_trace
*bt
= q
->blk_trace
;
847 __be64 rpdu
= cpu_to_be64(depth
);
851 what
= BLK_TA_UNPLUG_IO
;
853 what
= BLK_TA_UNPLUG_TIMER
;
855 __blk_add_trace(bt
, 0, 0, 0, 0, what
, 0, sizeof(rpdu
), &rpdu
);
859 static void blk_add_trace_split(void *ignore
,
860 struct request_queue
*q
, struct bio
*bio
,
863 struct blk_trace
*bt
= q
->blk_trace
;
866 __be64 rpdu
= cpu_to_be64(pdu
);
868 __blk_add_trace(bt
, bio
->bi_iter
.bi_sector
,
869 bio
->bi_iter
.bi_size
, bio_op(bio
), bio
->bi_opf
,
870 BLK_TA_SPLIT
, bio
->bi_status
, sizeof(rpdu
),
876 * blk_add_trace_bio_remap - Add a trace for a bio-remap operation
877 * @ignore: trace callback data parameter (not used)
878 * @q: queue the io is for
879 * @bio: the source bio
880 * @dev: target device
881 * @from: source sector
884 * Device mapper or raid target sometimes need to split a bio because
885 * it spans a stripe (or similar). Add a trace for that action.
888 static void blk_add_trace_bio_remap(void *ignore
,
889 struct request_queue
*q
, struct bio
*bio
,
890 dev_t dev
, sector_t from
)
892 struct blk_trace
*bt
= q
->blk_trace
;
893 struct blk_io_trace_remap r
;
898 r
.device_from
= cpu_to_be32(dev
);
899 r
.device_to
= cpu_to_be32(bio
->bi_bdev
->bd_dev
);
900 r
.sector_from
= cpu_to_be64(from
);
902 __blk_add_trace(bt
, bio
->bi_iter
.bi_sector
, bio
->bi_iter
.bi_size
,
903 bio_op(bio
), bio
->bi_opf
, BLK_TA_REMAP
, bio
->bi_status
,
908 * blk_add_trace_rq_remap - Add a trace for a request-remap operation
909 * @ignore: trace callback data parameter (not used)
910 * @q: queue the io is for
911 * @rq: the source request
912 * @dev: target device
913 * @from: source sector
916 * Device mapper remaps request to other devices.
917 * Add a trace for that action.
920 static void blk_add_trace_rq_remap(void *ignore
,
921 struct request_queue
*q
,
922 struct request
*rq
, dev_t dev
,
925 struct blk_trace
*bt
= q
->blk_trace
;
926 struct blk_io_trace_remap r
;
931 r
.device_from
= cpu_to_be32(dev
);
932 r
.device_to
= cpu_to_be32(disk_devt(rq
->rq_disk
));
933 r
.sector_from
= cpu_to_be64(from
);
935 __blk_add_trace(bt
, blk_rq_pos(rq
), blk_rq_bytes(rq
),
936 rq_data_dir(rq
), 0, BLK_TA_REMAP
, 0,
941 * blk_add_driver_data - Add binary message with driver-specific data
942 * @q: queue the io is for
944 * @data: driver-specific data
945 * @len: length of driver-specific data
948 * Some drivers might want to write driver-specific data per request.
951 void blk_add_driver_data(struct request_queue
*q
,
953 void *data
, size_t len
)
955 struct blk_trace
*bt
= q
->blk_trace
;
960 __blk_add_trace(bt
, blk_rq_trace_sector(rq
), blk_rq_bytes(rq
), 0, 0,
961 BLK_TA_DRV_DATA
, 0, len
, data
);
963 EXPORT_SYMBOL_GPL(blk_add_driver_data
);
965 static void blk_register_tracepoints(void)
969 ret
= register_trace_block_rq_insert(blk_add_trace_rq_insert
, NULL
);
971 ret
= register_trace_block_rq_issue(blk_add_trace_rq_issue
, NULL
);
973 ret
= register_trace_block_rq_requeue(blk_add_trace_rq_requeue
, NULL
);
975 ret
= register_trace_block_rq_complete(blk_add_trace_rq_complete
, NULL
);
977 ret
= register_trace_block_bio_bounce(blk_add_trace_bio_bounce
, NULL
);
979 ret
= register_trace_block_bio_complete(blk_add_trace_bio_complete
, NULL
);
981 ret
= register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge
, NULL
);
983 ret
= register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge
, NULL
);
985 ret
= register_trace_block_bio_queue(blk_add_trace_bio_queue
, NULL
);
987 ret
= register_trace_block_getrq(blk_add_trace_getrq
, NULL
);
989 ret
= register_trace_block_sleeprq(blk_add_trace_sleeprq
, NULL
);
991 ret
= register_trace_block_plug(blk_add_trace_plug
, NULL
);
993 ret
= register_trace_block_unplug(blk_add_trace_unplug
, NULL
);
995 ret
= register_trace_block_split(blk_add_trace_split
, NULL
);
997 ret
= register_trace_block_bio_remap(blk_add_trace_bio_remap
, NULL
);
999 ret
= register_trace_block_rq_remap(blk_add_trace_rq_remap
, NULL
);
1003 static void blk_unregister_tracepoints(void)
1005 unregister_trace_block_rq_remap(blk_add_trace_rq_remap
, NULL
);
1006 unregister_trace_block_bio_remap(blk_add_trace_bio_remap
, NULL
);
1007 unregister_trace_block_split(blk_add_trace_split
, NULL
);
1008 unregister_trace_block_unplug(blk_add_trace_unplug
, NULL
);
1009 unregister_trace_block_plug(blk_add_trace_plug
, NULL
);
1010 unregister_trace_block_sleeprq(blk_add_trace_sleeprq
, NULL
);
1011 unregister_trace_block_getrq(blk_add_trace_getrq
, NULL
);
1012 unregister_trace_block_bio_queue(blk_add_trace_bio_queue
, NULL
);
1013 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge
, NULL
);
1014 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge
, NULL
);
1015 unregister_trace_block_bio_complete(blk_add_trace_bio_complete
, NULL
);
1016 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce
, NULL
);
1017 unregister_trace_block_rq_complete(blk_add_trace_rq_complete
, NULL
);
1018 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue
, NULL
);
1019 unregister_trace_block_rq_issue(blk_add_trace_rq_issue
, NULL
);
1020 unregister_trace_block_rq_insert(blk_add_trace_rq_insert
, NULL
);
1022 tracepoint_synchronize_unregister();
1026 * struct blk_io_tracer formatting routines
1029 static void fill_rwbs(char *rwbs
, const struct blk_io_trace
*t
)
1032 int tc
= t
->action
>> BLK_TC_SHIFT
;
1034 if (t
->action
== BLK_TN_MESSAGE
) {
1039 if (tc
& BLK_TC_FLUSH
)
1042 if (tc
& BLK_TC_DISCARD
)
1044 else if (tc
& BLK_TC_WRITE
)
1051 if (tc
& BLK_TC_FUA
)
1053 if (tc
& BLK_TC_AHEAD
)
1055 if (tc
& BLK_TC_SYNC
)
1057 if (tc
& BLK_TC_META
)
1064 const struct blk_io_trace
*te_blk_io_trace(const struct trace_entry
*ent
)
1066 return (const struct blk_io_trace
*)ent
;
1069 static inline const void *pdu_start(const struct trace_entry
*ent
)
1071 return te_blk_io_trace(ent
) + 1;
1074 static inline u32
t_action(const struct trace_entry
*ent
)
1076 return te_blk_io_trace(ent
)->action
;
1079 static inline u32
t_bytes(const struct trace_entry
*ent
)
1081 return te_blk_io_trace(ent
)->bytes
;
1084 static inline u32
t_sec(const struct trace_entry
*ent
)
1086 return te_blk_io_trace(ent
)->bytes
>> 9;
1089 static inline unsigned long long t_sector(const struct trace_entry
*ent
)
1091 return te_blk_io_trace(ent
)->sector
;
1094 static inline __u16
t_error(const struct trace_entry
*ent
)
1096 return te_blk_io_trace(ent
)->error
;
1099 static __u64
get_pdu_int(const struct trace_entry
*ent
)
1101 const __u64
*val
= pdu_start(ent
);
1102 return be64_to_cpu(*val
);
1105 static void get_pdu_remap(const struct trace_entry
*ent
,
1106 struct blk_io_trace_remap
*r
)
1108 const struct blk_io_trace_remap
*__r
= pdu_start(ent
);
1109 __u64 sector_from
= __r
->sector_from
;
1111 r
->device_from
= be32_to_cpu(__r
->device_from
);
1112 r
->device_to
= be32_to_cpu(__r
->device_to
);
1113 r
->sector_from
= be64_to_cpu(sector_from
);
1116 typedef void (blk_log_action_t
) (struct trace_iterator
*iter
, const char *act
);
1118 static void blk_log_action_classic(struct trace_iterator
*iter
, const char *act
)
1120 char rwbs
[RWBS_LEN
];
1121 unsigned long long ts
= iter
->ts
;
1122 unsigned long nsec_rem
= do_div(ts
, NSEC_PER_SEC
);
1123 unsigned secs
= (unsigned long)ts
;
1124 const struct blk_io_trace
*t
= te_blk_io_trace(iter
->ent
);
1128 trace_seq_printf(&iter
->seq
,
1129 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
1130 MAJOR(t
->device
), MINOR(t
->device
), iter
->cpu
,
1131 secs
, nsec_rem
, iter
->ent
->pid
, act
, rwbs
);
1134 static void blk_log_action(struct trace_iterator
*iter
, const char *act
)
1136 char rwbs
[RWBS_LEN
];
1137 const struct blk_io_trace
*t
= te_blk_io_trace(iter
->ent
);
1140 trace_seq_printf(&iter
->seq
, "%3d,%-3d %2s %3s ",
1141 MAJOR(t
->device
), MINOR(t
->device
), act
, rwbs
);
1144 static void blk_log_dump_pdu(struct trace_seq
*s
, const struct trace_entry
*ent
)
1146 const unsigned char *pdu_buf
;
1150 pdu_buf
= pdu_start(ent
);
1151 pdu_len
= te_blk_io_trace(ent
)->pdu_len
;
1156 /* find the last zero that needs to be printed */
1157 for (end
= pdu_len
- 1; end
>= 0; end
--)
1162 trace_seq_putc(s
, '(');
1164 for (i
= 0; i
< pdu_len
; i
++) {
1166 trace_seq_printf(s
, "%s%02x",
1167 i
== 0 ? "" : " ", pdu_buf
[i
]);
1170 * stop when the rest is just zeroes and indicate so
1171 * with a ".." appended
1173 if (i
== end
&& end
!= pdu_len
- 1) {
1174 trace_seq_puts(s
, " ..) ");
1179 trace_seq_puts(s
, ") ");
1182 static void blk_log_generic(struct trace_seq
*s
, const struct trace_entry
*ent
)
1184 char cmd
[TASK_COMM_LEN
];
1186 trace_find_cmdline(ent
->pid
, cmd
);
1188 if (t_action(ent
) & BLK_TC_ACT(BLK_TC_PC
)) {
1189 trace_seq_printf(s
, "%u ", t_bytes(ent
));
1190 blk_log_dump_pdu(s
, ent
);
1191 trace_seq_printf(s
, "[%s]\n", cmd
);
1194 trace_seq_printf(s
, "%llu + %u [%s]\n",
1195 t_sector(ent
), t_sec(ent
), cmd
);
1197 trace_seq_printf(s
, "[%s]\n", cmd
);
1201 static void blk_log_with_error(struct trace_seq
*s
,
1202 const struct trace_entry
*ent
)
1204 if (t_action(ent
) & BLK_TC_ACT(BLK_TC_PC
)) {
1205 blk_log_dump_pdu(s
, ent
);
1206 trace_seq_printf(s
, "[%d]\n", t_error(ent
));
1209 trace_seq_printf(s
, "%llu + %u [%d]\n",
1211 t_sec(ent
), t_error(ent
));
1213 trace_seq_printf(s
, "%llu [%d]\n",
1214 t_sector(ent
), t_error(ent
));
1218 static void blk_log_remap(struct trace_seq
*s
, const struct trace_entry
*ent
)
1220 struct blk_io_trace_remap r
= { .device_from
= 0, };
1222 get_pdu_remap(ent
, &r
);
1223 trace_seq_printf(s
, "%llu + %u <- (%d,%d) %llu\n",
1224 t_sector(ent
), t_sec(ent
),
1225 MAJOR(r
.device_from
), MINOR(r
.device_from
),
1226 (unsigned long long)r
.sector_from
);
1229 static void blk_log_plug(struct trace_seq
*s
, const struct trace_entry
*ent
)
1231 char cmd
[TASK_COMM_LEN
];
1233 trace_find_cmdline(ent
->pid
, cmd
);
1235 trace_seq_printf(s
, "[%s]\n", cmd
);
1238 static void blk_log_unplug(struct trace_seq
*s
, const struct trace_entry
*ent
)
1240 char cmd
[TASK_COMM_LEN
];
1242 trace_find_cmdline(ent
->pid
, cmd
);
1244 trace_seq_printf(s
, "[%s] %llu\n", cmd
, get_pdu_int(ent
));
1247 static void blk_log_split(struct trace_seq
*s
, const struct trace_entry
*ent
)
1249 char cmd
[TASK_COMM_LEN
];
1251 trace_find_cmdline(ent
->pid
, cmd
);
1253 trace_seq_printf(s
, "%llu / %llu [%s]\n", t_sector(ent
),
1254 get_pdu_int(ent
), cmd
);
1257 static void blk_log_msg(struct trace_seq
*s
, const struct trace_entry
*ent
)
1259 const struct blk_io_trace
*t
= te_blk_io_trace(ent
);
1261 trace_seq_putmem(s
, t
+ 1, t
->pdu_len
);
1262 trace_seq_putc(s
, '\n');
1266 * struct tracer operations
1269 static void blk_tracer_print_header(struct seq_file
*m
)
1271 if (!(blk_tracer_flags
.val
& TRACE_BLK_OPT_CLASSIC
))
1273 seq_puts(m
, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1277 static void blk_tracer_start(struct trace_array
*tr
)
1279 blk_tracer_enabled
= true;
1282 static int blk_tracer_init(struct trace_array
*tr
)
1285 blk_tracer_start(tr
);
1289 static void blk_tracer_stop(struct trace_array
*tr
)
1291 blk_tracer_enabled
= false;
1294 static void blk_tracer_reset(struct trace_array
*tr
)
1296 blk_tracer_stop(tr
);
1299 static const struct {
1301 void (*print
)(struct trace_seq
*s
, const struct trace_entry
*ent
);
1303 [__BLK_TA_QUEUE
] = {{ "Q", "queue" }, blk_log_generic
},
1304 [__BLK_TA_BACKMERGE
] = {{ "M", "backmerge" }, blk_log_generic
},
1305 [__BLK_TA_FRONTMERGE
] = {{ "F", "frontmerge" }, blk_log_generic
},
1306 [__BLK_TA_GETRQ
] = {{ "G", "getrq" }, blk_log_generic
},
1307 [__BLK_TA_SLEEPRQ
] = {{ "S", "sleeprq" }, blk_log_generic
},
1308 [__BLK_TA_REQUEUE
] = {{ "R", "requeue" }, blk_log_with_error
},
1309 [__BLK_TA_ISSUE
] = {{ "D", "issue" }, blk_log_generic
},
1310 [__BLK_TA_COMPLETE
] = {{ "C", "complete" }, blk_log_with_error
},
1311 [__BLK_TA_PLUG
] = {{ "P", "plug" }, blk_log_plug
},
1312 [__BLK_TA_UNPLUG_IO
] = {{ "U", "unplug_io" }, blk_log_unplug
},
1313 [__BLK_TA_UNPLUG_TIMER
] = {{ "UT", "unplug_timer" }, blk_log_unplug
},
1314 [__BLK_TA_INSERT
] = {{ "I", "insert" }, blk_log_generic
},
1315 [__BLK_TA_SPLIT
] = {{ "X", "split" }, blk_log_split
},
1316 [__BLK_TA_BOUNCE
] = {{ "B", "bounce" }, blk_log_generic
},
1317 [__BLK_TA_REMAP
] = {{ "A", "remap" }, blk_log_remap
},
1320 static enum print_line_t
print_one_line(struct trace_iterator
*iter
,
1323 struct trace_array
*tr
= iter
->tr
;
1324 struct trace_seq
*s
= &iter
->seq
;
1325 const struct blk_io_trace
*t
;
1328 blk_log_action_t
*log_action
;
1330 t
= te_blk_io_trace(iter
->ent
);
1331 what
= t
->action
& ((1 << BLK_TC_SHIFT
) - 1);
1332 long_act
= !!(tr
->trace_flags
& TRACE_ITER_VERBOSE
);
1333 log_action
= classic
? &blk_log_action_classic
: &blk_log_action
;
1335 if (t
->action
== BLK_TN_MESSAGE
) {
1336 log_action(iter
, long_act
? "message" : "m");
1337 blk_log_msg(s
, iter
->ent
);
1338 return trace_handle_return(s
);
1341 if (unlikely(what
== 0 || what
>= ARRAY_SIZE(what2act
)))
1342 trace_seq_printf(s
, "Unknown action %x\n", what
);
1344 log_action(iter
, what2act
[what
].act
[long_act
]);
1345 what2act
[what
].print(s
, iter
->ent
);
1348 return trace_handle_return(s
);
1351 static enum print_line_t
blk_trace_event_print(struct trace_iterator
*iter
,
1352 int flags
, struct trace_event
*event
)
1354 return print_one_line(iter
, false);
1357 static void blk_trace_synthesize_old_trace(struct trace_iterator
*iter
)
1359 struct trace_seq
*s
= &iter
->seq
;
1360 struct blk_io_trace
*t
= (struct blk_io_trace
*)iter
->ent
;
1361 const int offset
= offsetof(struct blk_io_trace
, sector
);
1362 struct blk_io_trace old
= {
1363 .magic
= BLK_IO_TRACE_MAGIC
| BLK_IO_TRACE_VERSION
,
1367 trace_seq_putmem(s
, &old
, offset
);
1368 trace_seq_putmem(s
, &t
->sector
,
1369 sizeof(old
) - offset
+ t
->pdu_len
);
1372 static enum print_line_t
1373 blk_trace_event_print_binary(struct trace_iterator
*iter
, int flags
,
1374 struct trace_event
*event
)
1376 blk_trace_synthesize_old_trace(iter
);
1378 return trace_handle_return(&iter
->seq
);
1381 static enum print_line_t
blk_tracer_print_line(struct trace_iterator
*iter
)
1383 if (!(blk_tracer_flags
.val
& TRACE_BLK_OPT_CLASSIC
))
1384 return TRACE_TYPE_UNHANDLED
;
1386 return print_one_line(iter
, true);
1390 blk_tracer_set_flag(struct trace_array
*tr
, u32 old_flags
, u32 bit
, int set
)
1392 /* don't output context-info for blk_classic output */
1393 if (bit
== TRACE_BLK_OPT_CLASSIC
) {
1395 tr
->trace_flags
&= ~TRACE_ITER_CONTEXT_INFO
;
1397 tr
->trace_flags
|= TRACE_ITER_CONTEXT_INFO
;
1402 static struct tracer blk_tracer __read_mostly
= {
1404 .init
= blk_tracer_init
,
1405 .reset
= blk_tracer_reset
,
1406 .start
= blk_tracer_start
,
1407 .stop
= blk_tracer_stop
,
1408 .print_header
= blk_tracer_print_header
,
1409 .print_line
= blk_tracer_print_line
,
1410 .flags
= &blk_tracer_flags
,
1411 .set_flag
= blk_tracer_set_flag
,
1414 static struct trace_event_functions trace_blk_event_funcs
= {
1415 .trace
= blk_trace_event_print
,
1416 .binary
= blk_trace_event_print_binary
,
1419 static struct trace_event trace_blk_event
= {
1421 .funcs
= &trace_blk_event_funcs
,
1424 static int __init
init_blk_tracer(void)
1426 if (!register_trace_event(&trace_blk_event
)) {
1427 pr_warn("Warning: could not register block events\n");
1431 if (register_tracer(&blk_tracer
) != 0) {
1432 pr_warn("Warning: could not register the block tracer\n");
1433 unregister_trace_event(&trace_blk_event
);
1440 device_initcall(init_blk_tracer
);
1442 static int blk_trace_remove_queue(struct request_queue
*q
)
1444 struct blk_trace
*bt
;
1446 bt
= xchg(&q
->blk_trace
, NULL
);
1450 if (atomic_dec_and_test(&blk_probes_ref
))
1451 blk_unregister_tracepoints();
1458 * Setup everything required to start tracing
1460 static int blk_trace_setup_queue(struct request_queue
*q
,
1461 struct block_device
*bdev
)
1463 struct blk_trace
*bt
= NULL
;
1466 bt
= kzalloc(sizeof(*bt
), GFP_KERNEL
);
1470 bt
->msg_data
= __alloc_percpu(BLK_TN_MAX_MSG
, __alignof__(char));
1474 bt
->dev
= bdev
->bd_dev
;
1475 bt
->act_mask
= (u16
)-1;
1477 blk_trace_setup_lba(bt
, bdev
);
1480 if (cmpxchg(&q
->blk_trace
, NULL
, bt
))
1483 if (atomic_inc_return(&blk_probes_ref
) == 1)
1484 blk_register_tracepoints();
1493 * sysfs interface to enable and configure tracing
1496 static ssize_t
sysfs_blk_trace_attr_show(struct device
*dev
,
1497 struct device_attribute
*attr
,
1499 static ssize_t
sysfs_blk_trace_attr_store(struct device
*dev
,
1500 struct device_attribute
*attr
,
1501 const char *buf
, size_t count
);
1502 #define BLK_TRACE_DEVICE_ATTR(_name) \
1503 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1504 sysfs_blk_trace_attr_show, \
1505 sysfs_blk_trace_attr_store)
1507 static BLK_TRACE_DEVICE_ATTR(enable
);
1508 static BLK_TRACE_DEVICE_ATTR(act_mask
);
1509 static BLK_TRACE_DEVICE_ATTR(pid
);
1510 static BLK_TRACE_DEVICE_ATTR(start_lba
);
1511 static BLK_TRACE_DEVICE_ATTR(end_lba
);
1513 static struct attribute
*blk_trace_attrs
[] = {
1514 &dev_attr_enable
.attr
,
1515 &dev_attr_act_mask
.attr
,
1517 &dev_attr_start_lba
.attr
,
1518 &dev_attr_end_lba
.attr
,
1522 struct attribute_group blk_trace_attr_group
= {
1524 .attrs
= blk_trace_attrs
,
1527 static const struct {
1531 { BLK_TC_READ
, "read" },
1532 { BLK_TC_WRITE
, "write" },
1533 { BLK_TC_FLUSH
, "flush" },
1534 { BLK_TC_SYNC
, "sync" },
1535 { BLK_TC_QUEUE
, "queue" },
1536 { BLK_TC_REQUEUE
, "requeue" },
1537 { BLK_TC_ISSUE
, "issue" },
1538 { BLK_TC_COMPLETE
, "complete" },
1539 { BLK_TC_FS
, "fs" },
1540 { BLK_TC_PC
, "pc" },
1541 { BLK_TC_NOTIFY
, "notify" },
1542 { BLK_TC_AHEAD
, "ahead" },
1543 { BLK_TC_META
, "meta" },
1544 { BLK_TC_DISCARD
, "discard" },
1545 { BLK_TC_DRV_DATA
, "drv_data" },
1546 { BLK_TC_FUA
, "fua" },
1549 static int blk_trace_str2mask(const char *str
)
1553 char *buf
, *s
, *token
;
1555 buf
= kstrdup(str
, GFP_KERNEL
);
1561 token
= strsep(&s
, ",");
1568 for (i
= 0; i
< ARRAY_SIZE(mask_maps
); i
++) {
1569 if (strcasecmp(token
, mask_maps
[i
].str
) == 0) {
1570 mask
|= mask_maps
[i
].mask
;
1574 if (i
== ARRAY_SIZE(mask_maps
)) {
1584 static ssize_t
blk_trace_mask2str(char *buf
, int mask
)
1589 for (i
= 0; i
< ARRAY_SIZE(mask_maps
); i
++) {
1590 if (mask
& mask_maps
[i
].mask
) {
1591 p
+= sprintf(p
, "%s%s",
1592 (p
== buf
) ? "" : ",", mask_maps
[i
].str
);
1600 static struct request_queue
*blk_trace_get_queue(struct block_device
*bdev
)
1602 if (bdev
->bd_disk
== NULL
)
1605 return bdev_get_queue(bdev
);
1608 static ssize_t
sysfs_blk_trace_attr_show(struct device
*dev
,
1609 struct device_attribute
*attr
,
1612 struct hd_struct
*p
= dev_to_part(dev
);
1613 struct request_queue
*q
;
1614 struct block_device
*bdev
;
1615 ssize_t ret
= -ENXIO
;
1617 bdev
= bdget(part_devt(p
));
1621 q
= blk_trace_get_queue(bdev
);
1625 mutex_lock(&bdev
->bd_mutex
);
1627 if (attr
== &dev_attr_enable
) {
1628 ret
= sprintf(buf
, "%u\n", !!q
->blk_trace
);
1629 goto out_unlock_bdev
;
1632 if (q
->blk_trace
== NULL
)
1633 ret
= sprintf(buf
, "disabled\n");
1634 else if (attr
== &dev_attr_act_mask
)
1635 ret
= blk_trace_mask2str(buf
, q
->blk_trace
->act_mask
);
1636 else if (attr
== &dev_attr_pid
)
1637 ret
= sprintf(buf
, "%u\n", q
->blk_trace
->pid
);
1638 else if (attr
== &dev_attr_start_lba
)
1639 ret
= sprintf(buf
, "%llu\n", q
->blk_trace
->start_lba
);
1640 else if (attr
== &dev_attr_end_lba
)
1641 ret
= sprintf(buf
, "%llu\n", q
->blk_trace
->end_lba
);
1644 mutex_unlock(&bdev
->bd_mutex
);
1651 static ssize_t
sysfs_blk_trace_attr_store(struct device
*dev
,
1652 struct device_attribute
*attr
,
1653 const char *buf
, size_t count
)
1655 struct block_device
*bdev
;
1656 struct request_queue
*q
;
1657 struct hd_struct
*p
;
1659 ssize_t ret
= -EINVAL
;
1664 if (attr
== &dev_attr_act_mask
) {
1665 if (kstrtoull(buf
, 0, &value
)) {
1666 /* Assume it is a list of trace category names */
1667 ret
= blk_trace_str2mask(buf
);
1672 } else if (kstrtoull(buf
, 0, &value
))
1677 p
= dev_to_part(dev
);
1678 bdev
= bdget(part_devt(p
));
1682 q
= blk_trace_get_queue(bdev
);
1686 mutex_lock(&bdev
->bd_mutex
);
1688 if (attr
== &dev_attr_enable
) {
1690 ret
= blk_trace_setup_queue(q
, bdev
);
1692 ret
= blk_trace_remove_queue(q
);
1693 goto out_unlock_bdev
;
1697 if (q
->blk_trace
== NULL
)
1698 ret
= blk_trace_setup_queue(q
, bdev
);
1701 if (attr
== &dev_attr_act_mask
)
1702 q
->blk_trace
->act_mask
= value
;
1703 else if (attr
== &dev_attr_pid
)
1704 q
->blk_trace
->pid
= value
;
1705 else if (attr
== &dev_attr_start_lba
)
1706 q
->blk_trace
->start_lba
= value
;
1707 else if (attr
== &dev_attr_end_lba
)
1708 q
->blk_trace
->end_lba
= value
;
1712 mutex_unlock(&bdev
->bd_mutex
);
1716 return ret
? ret
: count
;
1719 int blk_trace_init_sysfs(struct device
*dev
)
1721 return sysfs_create_group(&dev
->kobj
, &blk_trace_attr_group
);
1724 void blk_trace_remove_sysfs(struct device
*dev
)
1726 sysfs_remove_group(&dev
->kobj
, &blk_trace_attr_group
);
1729 #endif /* CONFIG_BLK_DEV_IO_TRACE */
1731 #ifdef CONFIG_EVENT_TRACING
1733 void blk_fill_rwbs(char *rwbs
, unsigned int op
, int bytes
)
1737 if (op
& REQ_PREFLUSH
)
1740 switch (op
& REQ_OP_MASK
) {
1742 case REQ_OP_WRITE_SAME
:
1745 case REQ_OP_DISCARD
:
1748 case REQ_OP_SECURE_ERASE
:
1764 if (op
& REQ_RAHEAD
)
1773 EXPORT_SYMBOL_GPL(blk_fill_rwbs
);
1775 #endif /* CONFIG_EVENT_TRACING */