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