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> |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 25 | #include <linux/time.h> |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 26 | #include <trace/block.h> |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
Frederic Weisbecker | 2db270a | 2009-02-07 20:46:45 +0100 | [diff] [blame] | 28 | #include "trace_output.h" |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 29 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 30 | static unsigned int blktrace_seq __read_mostly = 1; |
| 31 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 32 | static struct trace_array *blk_tr; |
Li Zefan | 5006ea7 | 2009-03-20 09:48:03 +0800 | [diff] [blame] | 33 | static bool blk_tracer_enabled __read_mostly; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 34 | |
| 35 | /* Select an alternative, minimalistic output than the original one */ |
Steven Rostedt | ef18012 | 2009-03-10 14:10:56 -0400 | [diff] [blame] | 36 | #define TRACE_BLK_OPT_CLASSIC 0x1 |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 37 | |
| 38 | static struct tracer_opt blk_tracer_opts[] = { |
| 39 | /* Default disable the minimalistic output */ |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 40 | { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) }, |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 41 | { } |
| 42 | }; |
| 43 | |
| 44 | static struct tracer_flags blk_tracer_flags = { |
| 45 | .val = 0, |
| 46 | .opts = blk_tracer_opts, |
| 47 | }; |
| 48 | |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 49 | /* Global reference count of probes */ |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 50 | static atomic_t blk_probes_ref = ATOMIC_INIT(0); |
| 51 | |
Li Zefan | 3c289ba | 2009-03-20 09:48:26 +0800 | [diff] [blame] | 52 | static void blk_register_tracepoints(void); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 53 | static void blk_unregister_tracepoints(void); |
| 54 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 55 | /* |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 56 | * Send out a notify message. |
| 57 | */ |
Jens Axboe | a863055 | 2006-12-04 09:30:58 +0100 | [diff] [blame] | 58 | static void trace_note(struct blk_trace *bt, pid_t pid, int action, |
| 59 | const void *data, size_t len) |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 60 | { |
| 61 | struct blk_io_trace *t; |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 62 | struct ring_buffer_event *event = NULL; |
| 63 | int pc = 0; |
| 64 | int cpu = smp_processor_id(); |
| 65 | bool blk_tracer = blk_tracer_enabled; |
| 66 | |
| 67 | if (blk_tracer) { |
| 68 | pc = preempt_count(); |
| 69 | event = trace_buffer_lock_reserve(blk_tr, TRACE_BLK, |
| 70 | sizeof(*t) + len, |
| 71 | 0, pc); |
| 72 | if (!event) |
| 73 | return; |
| 74 | t = ring_buffer_event_data(event); |
| 75 | goto record_it; |
| 76 | } |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 77 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 78 | if (!bt->rchan) |
| 79 | return; |
| 80 | |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 81 | t = relay_reserve(bt->rchan, sizeof(*t) + len); |
Jens Axboe | d3d9d2a | 2006-12-04 09:27:41 +0100 | [diff] [blame] | 82 | if (t) { |
Jens Axboe | d3d9d2a | 2006-12-04 09:27:41 +0100 | [diff] [blame] | 83 | t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION; |
Ingo Molnar | 2997c8c | 2008-01-11 13:35:54 +0100 | [diff] [blame] | 84 | t->time = ktime_to_ns(ktime_get()); |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 85 | record_it: |
Jens Axboe | d3d9d2a | 2006-12-04 09:27:41 +0100 | [diff] [blame] | 86 | t->device = bt->dev; |
| 87 | t->action = action; |
| 88 | t->pid = pid; |
| 89 | t->cpu = cpu; |
| 90 | t->pdu_len = len; |
| 91 | memcpy((void *) t + sizeof(*t), data, len); |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 92 | |
| 93 | if (blk_tracer) |
| 94 | trace_buffer_unlock_commit(blk_tr, event, 0, pc); |
Jens Axboe | d3d9d2a | 2006-12-04 09:27:41 +0100 | [diff] [blame] | 95 | } |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /* |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 99 | * Send out a notify for this process, if we haven't done so since a trace |
| 100 | * started |
| 101 | */ |
| 102 | static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk) |
| 103 | { |
Jens Axboe | a863055 | 2006-12-04 09:30:58 +0100 | [diff] [blame] | 104 | tsk->btrace_seq = blktrace_seq; |
| 105 | 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] | 106 | } |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 107 | |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 108 | static void trace_note_time(struct blk_trace *bt) |
| 109 | { |
| 110 | struct timespec now; |
| 111 | unsigned long flags; |
| 112 | u32 words[2]; |
| 113 | |
| 114 | getnstimeofday(&now); |
| 115 | words[0] = now.tv_sec; |
| 116 | words[1] = now.tv_nsec; |
| 117 | |
| 118 | local_irq_save(flags); |
| 119 | trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words)); |
| 120 | local_irq_restore(flags); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Alan D. Brunelle | 9d5f09a | 2008-05-27 14:54:41 +0200 | [diff] [blame] | 123 | void __trace_note_message(struct blk_trace *bt, const char *fmt, ...) |
| 124 | { |
| 125 | int n; |
| 126 | va_list args; |
Carl Henrik Lunde | 14a73f5 | 2008-06-12 20:13:58 +0200 | [diff] [blame] | 127 | unsigned long flags; |
Jens Axboe | 6456591 | 2008-05-28 14:45:33 +0200 | [diff] [blame] | 128 | char *buf; |
Alan D. Brunelle | 9d5f09a | 2008-05-27 14:54:41 +0200 | [diff] [blame] | 129 | |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 130 | if (unlikely(bt->trace_state != Blktrace_running && |
| 131 | !blk_tracer_enabled)) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 132 | return; |
| 133 | |
Carl Henrik Lunde | 14a73f5 | 2008-06-12 20:13:58 +0200 | [diff] [blame] | 134 | local_irq_save(flags); |
Jens Axboe | 6456591 | 2008-05-28 14:45:33 +0200 | [diff] [blame] | 135 | buf = per_cpu_ptr(bt->msg_data, smp_processor_id()); |
Alan D. Brunelle | 9d5f09a | 2008-05-27 14:54:41 +0200 | [diff] [blame] | 136 | va_start(args, fmt); |
Jens Axboe | 6456591 | 2008-05-28 14:45:33 +0200 | [diff] [blame] | 137 | n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args); |
Alan D. Brunelle | 9d5f09a | 2008-05-27 14:54:41 +0200 | [diff] [blame] | 138 | va_end(args); |
| 139 | |
Jens Axboe | 6456591 | 2008-05-28 14:45:33 +0200 | [diff] [blame] | 140 | trace_note(bt, 0, BLK_TN_MESSAGE, buf, n); |
Carl Henrik Lunde | 14a73f5 | 2008-06-12 20:13:58 +0200 | [diff] [blame] | 141 | local_irq_restore(flags); |
Alan D. Brunelle | 9d5f09a | 2008-05-27 14:54:41 +0200 | [diff] [blame] | 142 | } |
| 143 | EXPORT_SYMBOL_GPL(__trace_note_message); |
| 144 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 145 | static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector, |
| 146 | pid_t pid) |
| 147 | { |
| 148 | if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0) |
| 149 | return 1; |
Shawn Du | d0deef5 | 2009-04-14 13:58:56 +0800 | [diff] [blame] | 150 | if (sector && (sector < bt->start_lba || sector > bt->end_lba)) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 151 | return 1; |
| 152 | if (bt->pid && pid != bt->pid) |
| 153 | return 1; |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Data direction bit lookup |
| 160 | */ |
Li Zefan | e4955c9 | 2009-03-24 16:04:37 +0800 | [diff] [blame] | 161 | static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ), |
| 162 | BLK_TC_ACT(BLK_TC_WRITE) }; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 163 | |
David Woodhouse | 35ba8f7 | 2008-08-10 12:33:00 +0100 | [diff] [blame] | 164 | /* The ilog2() calls fall out because they're constant */ |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 165 | #define MASK_TC_BIT(rw, __name) ((rw & (1 << BIO_RW_ ## __name)) << \ |
| 166 | (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name)) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 167 | |
| 168 | /* |
| 169 | * The worker for the various blk_add_trace*() types. Fills out a |
| 170 | * blk_io_trace structure and places it in a per-cpu subbuffer. |
| 171 | */ |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 172 | 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] | 173 | int rw, u32 what, int error, int pdu_len, void *pdu_data) |
| 174 | { |
| 175 | struct task_struct *tsk = current; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 176 | struct ring_buffer_event *event = NULL; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 177 | struct blk_io_trace *t; |
Arnaldo Carvalho de Melo | 0a98775 | 2009-02-05 16:12:56 -0200 | [diff] [blame] | 178 | unsigned long flags = 0; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 179 | unsigned long *sequence; |
| 180 | pid_t pid; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 181 | int cpu, pc = 0; |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 182 | bool blk_tracer = blk_tracer_enabled; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 183 | |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 184 | if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer)) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 185 | return; |
| 186 | |
| 187 | what |= ddir_act[rw & WRITE]; |
David Woodhouse | 35ba8f7 | 2008-08-10 12:33:00 +0100 | [diff] [blame] | 188 | what |= MASK_TC_BIT(rw, BARRIER); |
Ingo Molnar | 72c26c9 | 2009-02-19 09:00:35 +0100 | [diff] [blame] | 189 | what |= MASK_TC_BIT(rw, SYNCIO); |
David Woodhouse | 35ba8f7 | 2008-08-10 12:33:00 +0100 | [diff] [blame] | 190 | what |= MASK_TC_BIT(rw, AHEAD); |
| 191 | what |= MASK_TC_BIT(rw, META); |
| 192 | what |= MASK_TC_BIT(rw, DISCARD); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 193 | |
| 194 | pid = tsk->pid; |
Shawn Du | d0deef5 | 2009-04-14 13:58:56 +0800 | [diff] [blame] | 195 | if (act_log_check(bt, what, sector, pid)) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 196 | return; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 197 | cpu = raw_smp_processor_id(); |
| 198 | |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 199 | if (blk_tracer) { |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 200 | tracing_record_cmdline(current); |
| 201 | |
Arnaldo Carvalho de Melo | 51a763d | 2009-02-05 16:14:13 -0200 | [diff] [blame] | 202 | pc = preempt_count(); |
| 203 | event = trace_buffer_lock_reserve(blk_tr, TRACE_BLK, |
| 204 | sizeof(*t) + pdu_len, |
| 205 | 0, pc); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 206 | if (!event) |
| 207 | return; |
Arnaldo Carvalho de Melo | 51a763d | 2009-02-05 16:14:13 -0200 | [diff] [blame] | 208 | t = ring_buffer_event_data(event); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 209 | goto record_it; |
| 210 | } |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 211 | |
| 212 | /* |
| 213 | * A word about the locking here - we disable interrupts to reserve |
| 214 | * 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] | 215 | * from coming in and stepping on our toes. |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 216 | */ |
| 217 | local_irq_save(flags); |
| 218 | |
| 219 | if (unlikely(tsk->btrace_seq != blktrace_seq)) |
| 220 | trace_note_tsk(bt, tsk); |
| 221 | |
| 222 | t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len); |
| 223 | if (t) { |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 224 | sequence = per_cpu_ptr(bt->sequence, cpu); |
| 225 | |
| 226 | t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION; |
| 227 | t->sequence = ++(*sequence); |
Ingo Molnar | 2997c8c | 2008-01-11 13:35:54 +0100 | [diff] [blame] | 228 | t->time = ktime_to_ns(ktime_get()); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 229 | record_it: |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 230 | /* |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 231 | * These two are not needed in ftrace as they are in the |
| 232 | * generic trace_entry, filled by tracing_generic_entry_update, |
| 233 | * but for the trace_event->bin() synthesizer benefit we do it |
| 234 | * here too. |
| 235 | */ |
| 236 | t->cpu = cpu; |
| 237 | t->pid = pid; |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 238 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 239 | t->sector = sector; |
| 240 | t->bytes = bytes; |
| 241 | t->action = what; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 242 | t->device = bt->dev; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 243 | t->error = error; |
| 244 | t->pdu_len = pdu_len; |
| 245 | |
| 246 | if (pdu_len) |
| 247 | memcpy((void *) t + sizeof(*t), pdu_data, pdu_len); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 248 | |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 249 | if (blk_tracer) { |
Arnaldo Carvalho de Melo | 51a763d | 2009-02-05 16:14:13 -0200 | [diff] [blame] | 250 | trace_buffer_unlock_commit(blk_tr, event, 0, pc); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 251 | return; |
| 252 | } |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | local_irq_restore(flags); |
| 256 | } |
| 257 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 258 | static struct dentry *blk_tree_root; |
Jens Axboe | 11a5715 | 2008-01-11 13:37:01 +0100 | [diff] [blame] | 259 | static DEFINE_MUTEX(blk_tree_mutex); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 260 | |
Li Zefan | ad5dd54 | 2009-03-27 10:20:24 +0800 | [diff] [blame] | 261 | static void blk_trace_free(struct blk_trace *bt) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 262 | { |
Alan D. Brunelle | 02c6230 | 2008-06-11 09:12:52 +0200 | [diff] [blame] | 263 | debugfs_remove(bt->msg_file); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 264 | debugfs_remove(bt->dropped_file); |
Jens Axboe | f48fc4d | 2009-01-05 10:17:25 +0100 | [diff] [blame] | 265 | relay_close(bt->rchan); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 266 | free_percpu(bt->sequence); |
Jens Axboe | 6456591 | 2008-05-28 14:45:33 +0200 | [diff] [blame] | 267 | free_percpu(bt->msg_data); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 268 | kfree(bt); |
Li Zefan | ad5dd54 | 2009-03-27 10:20:24 +0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static void blk_trace_cleanup(struct blk_trace *bt) |
| 272 | { |
| 273 | blk_trace_free(bt); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 274 | if (atomic_dec_and_test(&blk_probes_ref)) |
| 275 | blk_unregister_tracepoints(); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 278 | int blk_trace_remove(struct request_queue *q) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 279 | { |
| 280 | struct blk_trace *bt; |
| 281 | |
| 282 | bt = xchg(&q->blk_trace, NULL); |
| 283 | if (!bt) |
| 284 | return -EINVAL; |
| 285 | |
Li Zefan | 5554720 | 2009-03-25 17:21:26 +0800 | [diff] [blame] | 286 | if (bt->trace_state != Blktrace_running) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 287 | blk_trace_cleanup(bt); |
| 288 | |
| 289 | return 0; |
| 290 | } |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 291 | EXPORT_SYMBOL_GPL(blk_trace_remove); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 292 | |
| 293 | static int blk_dropped_open(struct inode *inode, struct file *filp) |
| 294 | { |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 295 | filp->private_data = inode->i_private; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static ssize_t blk_dropped_read(struct file *filp, char __user *buffer, |
| 301 | size_t count, loff_t *ppos) |
| 302 | { |
| 303 | struct blk_trace *bt = filp->private_data; |
| 304 | char buf[16]; |
| 305 | |
| 306 | snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped)); |
| 307 | |
| 308 | return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf)); |
| 309 | } |
| 310 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 311 | static const struct file_operations blk_dropped_fops = { |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 312 | .owner = THIS_MODULE, |
| 313 | .open = blk_dropped_open, |
| 314 | .read = blk_dropped_read, |
| 315 | }; |
| 316 | |
Alan D. Brunelle | 02c6230 | 2008-06-11 09:12:52 +0200 | [diff] [blame] | 317 | static int blk_msg_open(struct inode *inode, struct file *filp) |
| 318 | { |
| 319 | filp->private_data = inode->i_private; |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | static ssize_t blk_msg_write(struct file *filp, const char __user *buffer, |
| 325 | size_t count, loff_t *ppos) |
| 326 | { |
| 327 | char *msg; |
| 328 | struct blk_trace *bt; |
| 329 | |
Li Zefan | 7635b03 | 2009-04-03 15:31:34 +0800 | [diff] [blame] | 330 | if (count >= BLK_TN_MAX_MSG) |
Alan D. Brunelle | 02c6230 | 2008-06-11 09:12:52 +0200 | [diff] [blame] | 331 | return -EINVAL; |
| 332 | |
Carl Henrik Lunde | a4b3ada | 2009-04-03 14:27:15 +0200 | [diff] [blame] | 333 | msg = kmalloc(count + 1, GFP_KERNEL); |
Alan D. Brunelle | 02c6230 | 2008-06-11 09:12:52 +0200 | [diff] [blame] | 334 | if (msg == NULL) |
| 335 | return -ENOMEM; |
| 336 | |
| 337 | if (copy_from_user(msg, buffer, count)) { |
| 338 | kfree(msg); |
| 339 | return -EFAULT; |
| 340 | } |
| 341 | |
Carl Henrik Lunde | a4b3ada | 2009-04-03 14:27:15 +0200 | [diff] [blame] | 342 | msg[count] = '\0'; |
Alan D. Brunelle | 02c6230 | 2008-06-11 09:12:52 +0200 | [diff] [blame] | 343 | bt = filp->private_data; |
| 344 | __trace_note_message(bt, "%s", msg); |
| 345 | kfree(msg); |
| 346 | |
| 347 | return count; |
| 348 | } |
| 349 | |
| 350 | static const struct file_operations blk_msg_fops = { |
| 351 | .owner = THIS_MODULE, |
| 352 | .open = blk_msg_open, |
| 353 | .write = blk_msg_write, |
| 354 | }; |
| 355 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 356 | /* |
| 357 | * Keep track of how many times we encountered a full subbuffer, to aid |
| 358 | * the user space app in telling how many lost events there were. |
| 359 | */ |
| 360 | static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf, |
| 361 | void *prev_subbuf, size_t prev_padding) |
| 362 | { |
| 363 | struct blk_trace *bt; |
| 364 | |
| 365 | if (!relay_buf_full(buf)) |
| 366 | return 1; |
| 367 | |
| 368 | bt = buf->chan->private_data; |
| 369 | atomic_inc(&bt->dropped); |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | static int blk_remove_buf_file_callback(struct dentry *dentry) |
| 374 | { |
Jens Axboe | f48fc4d | 2009-01-05 10:17:25 +0100 | [diff] [blame] | 375 | struct dentry *parent = dentry->d_parent; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 376 | debugfs_remove(dentry); |
Jens Axboe | f48fc4d | 2009-01-05 10:17:25 +0100 | [diff] [blame] | 377 | |
| 378 | /* |
| 379 | * this will fail for all but the last file, but that is ok. what we |
| 380 | * care about is the top level buts->name directory going away, when |
| 381 | * the last trace file is gone. Then we don't have to rmdir() that |
| 382 | * manually on trace stop, so it nicely solves the issue with |
| 383 | * force killing of running traces. |
| 384 | */ |
| 385 | |
| 386 | debugfs_remove(parent); |
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); |
| 667 | __blk_add_trace(bt, 0, rq->data_len, rw, what, rq->errors, |
Li Zefan | e2494e1 | 2009-04-02 13:43:26 +0800 | [diff] [blame] | 668 | 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); |
| 671 | __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9, |
| 672 | rw, what, rq->errors, 0, NULL); |
| 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 |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 833 | * @to: target sector |
Alan D. Brunelle | a42aaa3 | 2009-05-04 16:27:26 -0400 | [diff] [blame^] | 834 | * @from: source sector |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 835 | * |
| 836 | * Description: |
| 837 | * Device mapper or raid target sometimes need to split a bio because |
| 838 | * it spans a stripe (or similar). Add a trace for that action. |
| 839 | * |
| 840 | **/ |
| 841 | static void blk_add_trace_remap(struct request_queue *q, struct bio *bio, |
Alan D. Brunelle | a42aaa3 | 2009-05-04 16:27:26 -0400 | [diff] [blame^] | 842 | dev_t dev, sector_t to, sector_t from) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 843 | { |
| 844 | struct blk_trace *bt = q->blk_trace; |
| 845 | struct blk_io_trace_remap r; |
| 846 | |
| 847 | if (likely(!bt)) |
| 848 | return; |
| 849 | |
Alan D. Brunelle | a42aaa3 | 2009-05-04 16:27:26 -0400 | [diff] [blame^] | 850 | r.device_from = cpu_to_be32(dev); |
| 851 | r.device_to = cpu_to_be32(bio->bi_bdev->bd_dev); |
| 852 | r.sector_from = cpu_to_be64(from); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 853 | |
| 854 | __blk_add_trace(bt, from, bio->bi_size, bio->bi_rw, BLK_TA_REMAP, |
| 855 | !bio_flagged(bio, BIO_UPTODATE), sizeof(r), &r); |
| 856 | } |
| 857 | |
| 858 | /** |
| 859 | * blk_add_driver_data - Add binary message with driver-specific data |
| 860 | * @q: queue the io is for |
| 861 | * @rq: io request |
| 862 | * @data: driver-specific data |
| 863 | * @len: length of driver-specific data |
| 864 | * |
| 865 | * Description: |
| 866 | * Some drivers might want to write driver-specific data per request. |
| 867 | * |
| 868 | **/ |
| 869 | void blk_add_driver_data(struct request_queue *q, |
| 870 | struct request *rq, |
| 871 | void *data, size_t len) |
| 872 | { |
| 873 | struct blk_trace *bt = q->blk_trace; |
| 874 | |
| 875 | if (likely(!bt)) |
| 876 | return; |
| 877 | |
| 878 | if (blk_pc_request(rq)) |
| 879 | __blk_add_trace(bt, 0, rq->data_len, 0, BLK_TA_DRV_DATA, |
| 880 | rq->errors, len, data); |
| 881 | else |
| 882 | __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9, |
| 883 | 0, BLK_TA_DRV_DATA, rq->errors, len, data); |
| 884 | } |
| 885 | EXPORT_SYMBOL_GPL(blk_add_driver_data); |
| 886 | |
Li Zefan | 3c289ba | 2009-03-20 09:48:26 +0800 | [diff] [blame] | 887 | static void blk_register_tracepoints(void) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 888 | { |
| 889 | int ret; |
| 890 | |
| 891 | ret = register_trace_block_rq_abort(blk_add_trace_rq_abort); |
| 892 | WARN_ON(ret); |
| 893 | ret = register_trace_block_rq_insert(blk_add_trace_rq_insert); |
| 894 | WARN_ON(ret); |
| 895 | ret = register_trace_block_rq_issue(blk_add_trace_rq_issue); |
| 896 | WARN_ON(ret); |
| 897 | ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue); |
| 898 | WARN_ON(ret); |
| 899 | ret = register_trace_block_rq_complete(blk_add_trace_rq_complete); |
| 900 | WARN_ON(ret); |
| 901 | ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce); |
| 902 | WARN_ON(ret); |
| 903 | ret = register_trace_block_bio_complete(blk_add_trace_bio_complete); |
| 904 | WARN_ON(ret); |
| 905 | ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge); |
| 906 | WARN_ON(ret); |
| 907 | ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge); |
| 908 | WARN_ON(ret); |
| 909 | ret = register_trace_block_bio_queue(blk_add_trace_bio_queue); |
| 910 | WARN_ON(ret); |
| 911 | ret = register_trace_block_getrq(blk_add_trace_getrq); |
| 912 | WARN_ON(ret); |
| 913 | ret = register_trace_block_sleeprq(blk_add_trace_sleeprq); |
| 914 | WARN_ON(ret); |
| 915 | ret = register_trace_block_plug(blk_add_trace_plug); |
| 916 | WARN_ON(ret); |
| 917 | ret = register_trace_block_unplug_timer(blk_add_trace_unplug_timer); |
| 918 | WARN_ON(ret); |
| 919 | ret = register_trace_block_unplug_io(blk_add_trace_unplug_io); |
| 920 | WARN_ON(ret); |
| 921 | ret = register_trace_block_split(blk_add_trace_split); |
| 922 | WARN_ON(ret); |
| 923 | ret = register_trace_block_remap(blk_add_trace_remap); |
| 924 | WARN_ON(ret); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | static void blk_unregister_tracepoints(void) |
| 928 | { |
| 929 | unregister_trace_block_remap(blk_add_trace_remap); |
| 930 | unregister_trace_block_split(blk_add_trace_split); |
| 931 | unregister_trace_block_unplug_io(blk_add_trace_unplug_io); |
| 932 | unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer); |
| 933 | unregister_trace_block_plug(blk_add_trace_plug); |
| 934 | unregister_trace_block_sleeprq(blk_add_trace_sleeprq); |
| 935 | unregister_trace_block_getrq(blk_add_trace_getrq); |
| 936 | unregister_trace_block_bio_queue(blk_add_trace_bio_queue); |
| 937 | unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge); |
| 938 | unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge); |
| 939 | unregister_trace_block_bio_complete(blk_add_trace_bio_complete); |
| 940 | unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce); |
| 941 | unregister_trace_block_rq_complete(blk_add_trace_rq_complete); |
| 942 | unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue); |
| 943 | unregister_trace_block_rq_issue(blk_add_trace_rq_issue); |
| 944 | unregister_trace_block_rq_insert(blk_add_trace_rq_insert); |
| 945 | unregister_trace_block_rq_abort(blk_add_trace_rq_abort); |
| 946 | |
| 947 | tracepoint_synchronize_unregister(); |
| 948 | } |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 949 | |
| 950 | /* |
| 951 | * struct blk_io_tracer formatting routines |
| 952 | */ |
| 953 | |
| 954 | static void fill_rwbs(char *rwbs, const struct blk_io_trace *t) |
| 955 | { |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 956 | int i = 0; |
Li Zefan | 6579634 | 2009-03-24 16:05:06 +0800 | [diff] [blame] | 957 | int tc = t->action >> BLK_TC_SHIFT; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 958 | |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 959 | if (t->action == BLK_TN_MESSAGE) { |
| 960 | rwbs[i++] = 'N'; |
| 961 | goto out; |
| 962 | } |
| 963 | |
Li Zefan | 6579634 | 2009-03-24 16:05:06 +0800 | [diff] [blame] | 964 | if (tc & BLK_TC_DISCARD) |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 965 | rwbs[i++] = 'D'; |
Li Zefan | 6579634 | 2009-03-24 16:05:06 +0800 | [diff] [blame] | 966 | else if (tc & BLK_TC_WRITE) |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 967 | rwbs[i++] = 'W'; |
| 968 | else if (t->bytes) |
| 969 | rwbs[i++] = 'R'; |
| 970 | else |
| 971 | rwbs[i++] = 'N'; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 972 | |
Li Zefan | 6579634 | 2009-03-24 16:05:06 +0800 | [diff] [blame] | 973 | if (tc & BLK_TC_AHEAD) |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 974 | rwbs[i++] = 'A'; |
Li Zefan | 6579634 | 2009-03-24 16:05:06 +0800 | [diff] [blame] | 975 | if (tc & BLK_TC_BARRIER) |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 976 | rwbs[i++] = 'B'; |
Li Zefan | 6579634 | 2009-03-24 16:05:06 +0800 | [diff] [blame] | 977 | if (tc & BLK_TC_SYNC) |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 978 | rwbs[i++] = 'S'; |
Li Zefan | 6579634 | 2009-03-24 16:05:06 +0800 | [diff] [blame] | 979 | if (tc & BLK_TC_META) |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 980 | rwbs[i++] = 'M'; |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 981 | out: |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 982 | rwbs[i] = '\0'; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | static inline |
| 986 | const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent) |
| 987 | { |
| 988 | return (const struct blk_io_trace *)ent; |
| 989 | } |
| 990 | |
| 991 | static inline const void *pdu_start(const struct trace_entry *ent) |
| 992 | { |
| 993 | return te_blk_io_trace(ent) + 1; |
| 994 | } |
| 995 | |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 996 | static inline u32 t_action(const struct trace_entry *ent) |
| 997 | { |
| 998 | return te_blk_io_trace(ent)->action; |
| 999 | } |
| 1000 | |
| 1001 | static inline u32 t_bytes(const struct trace_entry *ent) |
| 1002 | { |
| 1003 | return te_blk_io_trace(ent)->bytes; |
| 1004 | } |
| 1005 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1006 | static inline u32 t_sec(const struct trace_entry *ent) |
| 1007 | { |
| 1008 | return te_blk_io_trace(ent)->bytes >> 9; |
| 1009 | } |
| 1010 | |
| 1011 | static inline unsigned long long t_sector(const struct trace_entry *ent) |
| 1012 | { |
| 1013 | return te_blk_io_trace(ent)->sector; |
| 1014 | } |
| 1015 | |
| 1016 | static inline __u16 t_error(const struct trace_entry *ent) |
| 1017 | { |
Li Zefan | e0dc81b | 2009-03-24 16:05:51 +0800 | [diff] [blame] | 1018 | return te_blk_io_trace(ent)->error; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | static __u64 get_pdu_int(const struct trace_entry *ent) |
| 1022 | { |
| 1023 | const __u64 *val = pdu_start(ent); |
| 1024 | return be64_to_cpu(*val); |
| 1025 | } |
| 1026 | |
| 1027 | static void get_pdu_remap(const struct trace_entry *ent, |
| 1028 | struct blk_io_trace_remap *r) |
| 1029 | { |
| 1030 | const struct blk_io_trace_remap *__r = pdu_start(ent); |
Alan D. Brunelle | a42aaa3 | 2009-05-04 16:27:26 -0400 | [diff] [blame^] | 1031 | __u64 sector_from = __r->sector_from; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1032 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1033 | r->device_from = be32_to_cpu(__r->device_from); |
Alan D. Brunelle | a42aaa3 | 2009-05-04 16:27:26 -0400 | [diff] [blame^] | 1034 | r->device_to = be32_to_cpu(__r->device_to); |
| 1035 | r->sector_from = be64_to_cpu(sector_from); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1036 | } |
| 1037 | |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1038 | typedef int (blk_log_action_t) (struct trace_iterator *iter, const char *act); |
| 1039 | |
| 1040 | 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] | 1041 | { |
| 1042 | char rwbs[6]; |
Li Zefan | 35ac51b | 2009-03-27 10:19:46 +0800 | [diff] [blame] | 1043 | unsigned long long ts = iter->ts; |
| 1044 | unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1045 | unsigned secs = (unsigned long)ts; |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1046 | 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] | 1047 | |
| 1048 | fill_rwbs(rwbs, t); |
| 1049 | |
| 1050 | return trace_seq_printf(&iter->seq, |
Li Zefan | 35ac51b | 2009-03-27 10:19:46 +0800 | [diff] [blame] | 1051 | "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ", |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1052 | MAJOR(t->device), MINOR(t->device), iter->cpu, |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1053 | secs, nsec_rem, iter->ent->pid, act, rwbs); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1054 | } |
| 1055 | |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1056 | 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] | 1057 | { |
| 1058 | char rwbs[6]; |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1059 | const struct blk_io_trace *t = te_blk_io_trace(iter->ent); |
| 1060 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1061 | fill_rwbs(rwbs, t); |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1062 | return trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ", |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1063 | MAJOR(t->device), MINOR(t->device), act, rwbs); |
| 1064 | } |
| 1065 | |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 1066 | static int blk_log_dump_pdu(struct trace_seq *s, const struct trace_entry *ent) |
| 1067 | { |
| 1068 | const char *pdu_buf; |
| 1069 | int pdu_len; |
| 1070 | int i, end, ret; |
| 1071 | |
| 1072 | pdu_buf = pdu_start(ent); |
| 1073 | pdu_len = te_blk_io_trace(ent)->pdu_len; |
| 1074 | |
| 1075 | if (!pdu_len) |
| 1076 | return 1; |
| 1077 | |
| 1078 | /* find the last zero that needs to be printed */ |
| 1079 | for (end = pdu_len - 1; end >= 0; end--) |
| 1080 | if (pdu_buf[end]) |
| 1081 | break; |
| 1082 | end++; |
| 1083 | |
| 1084 | if (!trace_seq_putc(s, '(')) |
| 1085 | return 0; |
| 1086 | |
| 1087 | for (i = 0; i < pdu_len; i++) { |
| 1088 | |
| 1089 | ret = trace_seq_printf(s, "%s%02x", |
| 1090 | i == 0 ? "" : " ", pdu_buf[i]); |
| 1091 | if (!ret) |
| 1092 | return ret; |
| 1093 | |
| 1094 | /* |
| 1095 | * stop when the rest is just zeroes and indicate so |
| 1096 | * with a ".." appended |
| 1097 | */ |
| 1098 | if (i == end && end != pdu_len - 1) |
| 1099 | return trace_seq_puts(s, " ..) "); |
| 1100 | } |
| 1101 | |
| 1102 | return trace_seq_puts(s, ") "); |
| 1103 | } |
| 1104 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1105 | static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent) |
| 1106 | { |
Steven Rostedt | 4ca53085 | 2009-03-16 19:20:15 -0400 | [diff] [blame] | 1107 | char cmd[TASK_COMM_LEN]; |
| 1108 | |
| 1109 | trace_find_cmdline(ent->pid, cmd); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1110 | |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 1111 | if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) { |
| 1112 | int ret; |
| 1113 | |
| 1114 | ret = trace_seq_printf(s, "%u ", t_bytes(ent)); |
| 1115 | if (!ret) |
| 1116 | return 0; |
| 1117 | ret = blk_log_dump_pdu(s, ent); |
| 1118 | if (!ret) |
| 1119 | return 0; |
| 1120 | return trace_seq_printf(s, "[%s]\n", cmd); |
| 1121 | } else { |
| 1122 | if (t_sec(ent)) |
| 1123 | return trace_seq_printf(s, "%llu + %u [%s]\n", |
| 1124 | t_sector(ent), t_sec(ent), cmd); |
| 1125 | return trace_seq_printf(s, "[%s]\n", cmd); |
| 1126 | } |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1127 | } |
| 1128 | |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 1129 | static int blk_log_with_error(struct trace_seq *s, |
| 1130 | const struct trace_entry *ent) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1131 | { |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 1132 | if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) { |
| 1133 | int ret; |
| 1134 | |
| 1135 | ret = blk_log_dump_pdu(s, ent); |
| 1136 | if (ret) |
| 1137 | return trace_seq_printf(s, "[%d]\n", t_error(ent)); |
| 1138 | return 0; |
| 1139 | } else { |
| 1140 | if (t_sec(ent)) |
| 1141 | return trace_seq_printf(s, "%llu + %u [%d]\n", |
| 1142 | t_sector(ent), |
| 1143 | t_sec(ent), t_error(ent)); |
| 1144 | return trace_seq_printf(s, "%llu [%d]\n", |
| 1145 | t_sector(ent), t_error(ent)); |
| 1146 | } |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent) |
| 1150 | { |
Alan D. Brunelle | a42aaa3 | 2009-05-04 16:27:26 -0400 | [diff] [blame^] | 1151 | struct blk_io_trace_remap r = { .device_from = 0, }; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1152 | |
| 1153 | get_pdu_remap(ent, &r); |
| 1154 | return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n", |
Alan D. Brunelle | a42aaa3 | 2009-05-04 16:27:26 -0400 | [diff] [blame^] | 1155 | t_sector(ent), t_sec(ent), |
| 1156 | MAJOR(r.device_from), MINOR(r.device_from), |
| 1157 | (unsigned long long)r.sector_from); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent) |
| 1161 | { |
Steven Rostedt | 4ca53085 | 2009-03-16 19:20:15 -0400 | [diff] [blame] | 1162 | char cmd[TASK_COMM_LEN]; |
| 1163 | |
| 1164 | trace_find_cmdline(ent->pid, cmd); |
| 1165 | |
| 1166 | return trace_seq_printf(s, "[%s]\n", cmd); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent) |
| 1170 | { |
Steven Rostedt | 4ca53085 | 2009-03-16 19:20:15 -0400 | [diff] [blame] | 1171 | char cmd[TASK_COMM_LEN]; |
| 1172 | |
| 1173 | trace_find_cmdline(ent->pid, cmd); |
| 1174 | |
| 1175 | 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] | 1176 | } |
| 1177 | |
| 1178 | static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent) |
| 1179 | { |
Steven Rostedt | 4ca53085 | 2009-03-16 19:20:15 -0400 | [diff] [blame] | 1180 | char cmd[TASK_COMM_LEN]; |
| 1181 | |
| 1182 | trace_find_cmdline(ent->pid, cmd); |
| 1183 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1184 | return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent), |
Steven Rostedt | 4ca53085 | 2009-03-16 19:20:15 -0400 | [diff] [blame] | 1185 | get_pdu_int(ent), cmd); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1186 | } |
| 1187 | |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 1188 | static int blk_log_msg(struct trace_seq *s, const struct trace_entry *ent) |
| 1189 | { |
| 1190 | int ret; |
| 1191 | const struct blk_io_trace *t = te_blk_io_trace(ent); |
| 1192 | |
| 1193 | ret = trace_seq_putmem(s, t + 1, t->pdu_len); |
| 1194 | if (ret) |
| 1195 | return trace_seq_putc(s, '\n'); |
| 1196 | return ret; |
| 1197 | } |
| 1198 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1199 | /* |
| 1200 | * struct tracer operations |
| 1201 | */ |
| 1202 | |
| 1203 | static void blk_tracer_print_header(struct seq_file *m) |
| 1204 | { |
| 1205 | if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC)) |
| 1206 | return; |
| 1207 | seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n" |
| 1208 | "# | | | | | |\n"); |
| 1209 | } |
| 1210 | |
| 1211 | static void blk_tracer_start(struct trace_array *tr) |
| 1212 | { |
Li Zefan | ad5dd54 | 2009-03-27 10:20:24 +0800 | [diff] [blame] | 1213 | blk_tracer_enabled = true; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | static int blk_tracer_init(struct trace_array *tr) |
| 1217 | { |
| 1218 | blk_tr = tr; |
| 1219 | blk_tracer_start(tr); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1220 | return 0; |
| 1221 | } |
| 1222 | |
| 1223 | static void blk_tracer_stop(struct trace_array *tr) |
| 1224 | { |
Li Zefan | ad5dd54 | 2009-03-27 10:20:24 +0800 | [diff] [blame] | 1225 | blk_tracer_enabled = false; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | static void blk_tracer_reset(struct trace_array *tr) |
| 1229 | { |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1230 | blk_tracer_stop(tr); |
| 1231 | } |
| 1232 | |
Li Zefan | e4955c9 | 2009-03-24 16:04:37 +0800 | [diff] [blame] | 1233 | static const struct { |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1234 | const char *act[2]; |
Steven Rostedt | ef18012 | 2009-03-10 14:10:56 -0400 | [diff] [blame] | 1235 | int (*print)(struct trace_seq *s, const struct trace_entry *ent); |
Li Zefan | e4955c9 | 2009-03-24 16:04:37 +0800 | [diff] [blame] | 1236 | } what2act[] = { |
Steven Rostedt | ef18012 | 2009-03-10 14:10:56 -0400 | [diff] [blame] | 1237 | [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic }, |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1238 | [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic }, |
| 1239 | [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic }, |
| 1240 | [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic }, |
| 1241 | [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic }, |
| 1242 | [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error }, |
| 1243 | [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic }, |
| 1244 | [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error }, |
| 1245 | [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug }, |
| 1246 | [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug }, |
| 1247 | [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug }, |
| 1248 | [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic }, |
| 1249 | [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split }, |
| 1250 | [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic }, |
| 1251 | [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap }, |
| 1252 | }; |
| 1253 | |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1254 | static enum print_line_t print_one_line(struct trace_iterator *iter, |
| 1255 | bool classic) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1256 | { |
Arnaldo Carvalho de Melo | 2c9b238 | 2009-02-02 20:30:12 -0200 | [diff] [blame] | 1257 | struct trace_seq *s = &iter->seq; |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1258 | const struct blk_io_trace *t; |
| 1259 | u16 what; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1260 | int ret; |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1261 | bool long_act; |
| 1262 | blk_log_action_t *log_action; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1263 | |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1264 | t = te_blk_io_trace(iter->ent); |
| 1265 | what = t->action & ((1 << BLK_TC_SHIFT) - 1); |
| 1266 | long_act = !!(trace_flags & TRACE_ITER_VERBOSE); |
| 1267 | log_action = classic ? &blk_log_action_classic : &blk_log_action; |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 1268 | |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 1269 | if (t->action == BLK_TN_MESSAGE) { |
| 1270 | ret = log_action(iter, long_act ? "message" : "m"); |
| 1271 | if (ret) |
| 1272 | ret = blk_log_msg(s, iter->ent); |
| 1273 | goto out; |
| 1274 | } |
| 1275 | |
Li Zefan | eb08f8e | 2009-03-24 16:05:27 +0800 | [diff] [blame] | 1276 | if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act))) |
Li Zefan | b78825d | 2009-04-01 16:18:53 +0800 | [diff] [blame] | 1277 | ret = trace_seq_printf(s, "Unknown action %x\n", what); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1278 | else { |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1279 | ret = log_action(iter, what2act[what].act[long_act]); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1280 | if (ret) |
Arnaldo Carvalho de Melo | 2c9b238 | 2009-02-02 20:30:12 -0200 | [diff] [blame] | 1281 | ret = what2act[what].print(s, iter->ent); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1282 | } |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 1283 | out: |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1284 | return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE; |
| 1285 | } |
| 1286 | |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1287 | static enum print_line_t blk_trace_event_print(struct trace_iterator *iter, |
| 1288 | int flags) |
| 1289 | { |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1290 | return print_one_line(iter, false); |
| 1291 | } |
| 1292 | |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 1293 | static int blk_trace_synthesize_old_trace(struct trace_iterator *iter) |
| 1294 | { |
| 1295 | struct trace_seq *s = &iter->seq; |
| 1296 | struct blk_io_trace *t = (struct blk_io_trace *)iter->ent; |
| 1297 | const int offset = offsetof(struct blk_io_trace, sector); |
| 1298 | struct blk_io_trace old = { |
| 1299 | .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION, |
Li Zefan | 6c051ce | 2009-03-25 17:18:56 +0800 | [diff] [blame] | 1300 | .time = iter->ts, |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 1301 | }; |
| 1302 | |
| 1303 | if (!trace_seq_putmem(s, &old, offset)) |
| 1304 | return 0; |
| 1305 | return trace_seq_putmem(s, &t->sector, |
| 1306 | sizeof(old) - offset + t->pdu_len); |
| 1307 | } |
| 1308 | |
Arnaldo Carvalho de Melo | ae7462b | 2009-02-03 22:05:50 -0200 | [diff] [blame] | 1309 | static enum print_line_t |
| 1310 | 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] | 1311 | { |
| 1312 | return blk_trace_synthesize_old_trace(iter) ? |
| 1313 | TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE; |
| 1314 | } |
| 1315 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1316 | static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter) |
| 1317 | { |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1318 | if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC)) |
| 1319 | return TRACE_TYPE_UNHANDLED; |
| 1320 | |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1321 | return print_one_line(iter, true); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1322 | } |
| 1323 | |
Li Zefan | f3948f8 | 2009-04-15 11:02:56 +0800 | [diff] [blame] | 1324 | static int blk_tracer_set_flag(u32 old_flags, u32 bit, int set) |
| 1325 | { |
| 1326 | /* don't output context-info for blk_classic output */ |
| 1327 | if (bit == TRACE_BLK_OPT_CLASSIC) { |
| 1328 | if (set) |
| 1329 | trace_flags &= ~TRACE_ITER_CONTEXT_INFO; |
| 1330 | else |
| 1331 | trace_flags |= TRACE_ITER_CONTEXT_INFO; |
| 1332 | } |
| 1333 | return 0; |
| 1334 | } |
| 1335 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1336 | static struct tracer blk_tracer __read_mostly = { |
| 1337 | .name = "blk", |
| 1338 | .init = blk_tracer_init, |
| 1339 | .reset = blk_tracer_reset, |
| 1340 | .start = blk_tracer_start, |
| 1341 | .stop = blk_tracer_stop, |
| 1342 | .print_header = blk_tracer_print_header, |
| 1343 | .print_line = blk_tracer_print_line, |
| 1344 | .flags = &blk_tracer_flags, |
Li Zefan | f3948f8 | 2009-04-15 11:02:56 +0800 | [diff] [blame] | 1345 | .set_flag = blk_tracer_set_flag, |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1346 | }; |
| 1347 | |
| 1348 | static struct trace_event trace_blk_event = { |
Steven Rostedt | ef18012 | 2009-03-10 14:10:56 -0400 | [diff] [blame] | 1349 | .type = TRACE_BLK, |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1350 | .trace = blk_trace_event_print, |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 1351 | .binary = blk_trace_event_print_binary, |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1352 | }; |
| 1353 | |
| 1354 | static int __init init_blk_tracer(void) |
| 1355 | { |
| 1356 | if (!register_ftrace_event(&trace_blk_event)) { |
| 1357 | pr_warning("Warning: could not register block events\n"); |
| 1358 | return 1; |
| 1359 | } |
| 1360 | |
| 1361 | if (register_tracer(&blk_tracer) != 0) { |
| 1362 | pr_warning("Warning: could not register the block tracer\n"); |
| 1363 | unregister_ftrace_event(&trace_blk_event); |
| 1364 | return 1; |
| 1365 | } |
| 1366 | |
| 1367 | return 0; |
| 1368 | } |
| 1369 | |
| 1370 | device_initcall(init_blk_tracer); |
| 1371 | |
| 1372 | static int blk_trace_remove_queue(struct request_queue *q) |
| 1373 | { |
| 1374 | struct blk_trace *bt; |
| 1375 | |
| 1376 | bt = xchg(&q->blk_trace, NULL); |
| 1377 | if (bt == NULL) |
| 1378 | return -EINVAL; |
| 1379 | |
Li Zefan | 17ba97e | 2009-03-27 10:20:09 +0800 | [diff] [blame] | 1380 | if (atomic_dec_and_test(&blk_probes_ref)) |
| 1381 | blk_unregister_tracepoints(); |
| 1382 | |
Li Zefan | ad5dd54 | 2009-03-27 10:20:24 +0800 | [diff] [blame] | 1383 | blk_trace_free(bt); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1384 | return 0; |
| 1385 | } |
| 1386 | |
| 1387 | /* |
| 1388 | * Setup everything required to start tracing |
| 1389 | */ |
Li Zefan | 9908c30 | 2009-04-14 13:59:34 +0800 | [diff] [blame] | 1390 | static int blk_trace_setup_queue(struct request_queue *q, |
| 1391 | struct block_device *bdev) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1392 | { |
| 1393 | struct blk_trace *old_bt, *bt = NULL; |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 1394 | int ret = -ENOMEM; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1395 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1396 | bt = kzalloc(sizeof(*bt), GFP_KERNEL); |
| 1397 | if (!bt) |
Li Zefan | 15152e4 | 2009-03-20 09:49:08 +0800 | [diff] [blame] | 1398 | return -ENOMEM; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1399 | |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 1400 | bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char)); |
| 1401 | if (!bt->msg_data) |
| 1402 | goto free_bt; |
| 1403 | |
Li Zefan | 9908c30 | 2009-04-14 13:59:34 +0800 | [diff] [blame] | 1404 | bt->dev = bdev->bd_dev; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1405 | bt->act_mask = (u16)-1; |
Li Zefan | 9908c30 | 2009-04-14 13:59:34 +0800 | [diff] [blame] | 1406 | |
| 1407 | blk_trace_setup_lba(bt, bdev); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1408 | |
| 1409 | old_bt = xchg(&q->blk_trace, bt); |
| 1410 | if (old_bt != NULL) { |
| 1411 | (void)xchg(&q->blk_trace, old_bt); |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 1412 | ret = -EBUSY; |
| 1413 | goto free_bt; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1414 | } |
Li Zefan | 15152e4 | 2009-03-20 09:49:08 +0800 | [diff] [blame] | 1415 | |
Li Zefan | 17ba97e | 2009-03-27 10:20:09 +0800 | [diff] [blame] | 1416 | if (atomic_inc_return(&blk_probes_ref) == 1) |
| 1417 | blk_register_tracepoints(); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1418 | return 0; |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 1419 | |
| 1420 | free_bt: |
| 1421 | blk_trace_free(bt); |
| 1422 | return ret; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | /* |
| 1426 | * sysfs interface to enable and configure tracing |
| 1427 | */ |
| 1428 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1429 | static ssize_t sysfs_blk_trace_attr_show(struct device *dev, |
| 1430 | struct device_attribute *attr, |
| 1431 | char *buf); |
| 1432 | static ssize_t sysfs_blk_trace_attr_store(struct device *dev, |
| 1433 | struct device_attribute *attr, |
| 1434 | const char *buf, size_t count); |
| 1435 | #define BLK_TRACE_DEVICE_ATTR(_name) \ |
| 1436 | DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \ |
| 1437 | sysfs_blk_trace_attr_show, \ |
| 1438 | sysfs_blk_trace_attr_store) |
| 1439 | |
Li Zefan | cd649b8 | 2009-03-20 11:33:55 +0800 | [diff] [blame] | 1440 | static BLK_TRACE_DEVICE_ATTR(enable); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1441 | static BLK_TRACE_DEVICE_ATTR(act_mask); |
| 1442 | static BLK_TRACE_DEVICE_ATTR(pid); |
| 1443 | static BLK_TRACE_DEVICE_ATTR(start_lba); |
| 1444 | static BLK_TRACE_DEVICE_ATTR(end_lba); |
| 1445 | |
| 1446 | static struct attribute *blk_trace_attrs[] = { |
| 1447 | &dev_attr_enable.attr, |
| 1448 | &dev_attr_act_mask.attr, |
| 1449 | &dev_attr_pid.attr, |
| 1450 | &dev_attr_start_lba.attr, |
| 1451 | &dev_attr_end_lba.attr, |
| 1452 | NULL |
| 1453 | }; |
| 1454 | |
| 1455 | struct attribute_group blk_trace_attr_group = { |
| 1456 | .name = "trace", |
| 1457 | .attrs = blk_trace_attrs, |
| 1458 | }; |
| 1459 | |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1460 | static const struct { |
| 1461 | int mask; |
| 1462 | const char *str; |
| 1463 | } mask_maps[] = { |
| 1464 | { BLK_TC_READ, "read" }, |
| 1465 | { BLK_TC_WRITE, "write" }, |
| 1466 | { BLK_TC_BARRIER, "barrier" }, |
| 1467 | { BLK_TC_SYNC, "sync" }, |
| 1468 | { BLK_TC_QUEUE, "queue" }, |
| 1469 | { BLK_TC_REQUEUE, "requeue" }, |
| 1470 | { BLK_TC_ISSUE, "issue" }, |
| 1471 | { BLK_TC_COMPLETE, "complete" }, |
| 1472 | { BLK_TC_FS, "fs" }, |
| 1473 | { BLK_TC_PC, "pc" }, |
| 1474 | { BLK_TC_AHEAD, "ahead" }, |
| 1475 | { BLK_TC_META, "meta" }, |
| 1476 | { BLK_TC_DISCARD, "discard" }, |
| 1477 | { BLK_TC_DRV_DATA, "drv_data" }, |
| 1478 | }; |
| 1479 | |
| 1480 | static int blk_trace_str2mask(const char *str) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1481 | { |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1482 | int i; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1483 | int mask = 0; |
Li Zefan | 9eb8512 | 2009-04-09 11:19:40 +0800 | [diff] [blame] | 1484 | char *buf, *s, *token; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1485 | |
Li Zefan | 9eb8512 | 2009-04-09 11:19:40 +0800 | [diff] [blame] | 1486 | buf = kstrdup(str, GFP_KERNEL); |
| 1487 | if (buf == NULL) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1488 | return -ENOMEM; |
Li Zefan | 9eb8512 | 2009-04-09 11:19:40 +0800 | [diff] [blame] | 1489 | s = strstrip(buf); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1490 | |
| 1491 | while (1) { |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1492 | token = strsep(&s, ","); |
| 1493 | if (token == NULL) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1494 | break; |
| 1495 | |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1496 | if (*token == '\0') |
| 1497 | continue; |
| 1498 | |
| 1499 | for (i = 0; i < ARRAY_SIZE(mask_maps); i++) { |
| 1500 | if (strcasecmp(token, mask_maps[i].str) == 0) { |
| 1501 | mask |= mask_maps[i].mask; |
| 1502 | break; |
| 1503 | } |
| 1504 | } |
| 1505 | if (i == ARRAY_SIZE(mask_maps)) { |
| 1506 | mask = -EINVAL; |
| 1507 | break; |
| 1508 | } |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1509 | } |
Li Zefan | 9eb8512 | 2009-04-09 11:19:40 +0800 | [diff] [blame] | 1510 | kfree(buf); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1511 | |
| 1512 | return mask; |
| 1513 | } |
| 1514 | |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1515 | static ssize_t blk_trace_mask2str(char *buf, int mask) |
| 1516 | { |
| 1517 | int i; |
| 1518 | char *p = buf; |
| 1519 | |
| 1520 | for (i = 0; i < ARRAY_SIZE(mask_maps); i++) { |
| 1521 | if (mask & mask_maps[i].mask) { |
| 1522 | p += sprintf(p, "%s%s", |
| 1523 | (p == buf) ? "" : ",", mask_maps[i].str); |
| 1524 | } |
| 1525 | } |
| 1526 | *p++ = '\n'; |
| 1527 | |
| 1528 | return p - buf; |
| 1529 | } |
| 1530 | |
Li Zefan | b125130 | 2009-03-20 10:34:00 +0800 | [diff] [blame] | 1531 | static struct request_queue *blk_trace_get_queue(struct block_device *bdev) |
| 1532 | { |
| 1533 | if (bdev->bd_disk == NULL) |
| 1534 | return NULL; |
| 1535 | |
| 1536 | return bdev_get_queue(bdev); |
| 1537 | } |
| 1538 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1539 | static ssize_t sysfs_blk_trace_attr_show(struct device *dev, |
| 1540 | struct device_attribute *attr, |
| 1541 | char *buf) |
| 1542 | { |
| 1543 | struct hd_struct *p = dev_to_part(dev); |
| 1544 | struct request_queue *q; |
| 1545 | struct block_device *bdev; |
| 1546 | ssize_t ret = -ENXIO; |
| 1547 | |
| 1548 | lock_kernel(); |
| 1549 | bdev = bdget(part_devt(p)); |
| 1550 | if (bdev == NULL) |
| 1551 | goto out_unlock_kernel; |
| 1552 | |
Li Zefan | b125130 | 2009-03-20 10:34:00 +0800 | [diff] [blame] | 1553 | q = blk_trace_get_queue(bdev); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1554 | if (q == NULL) |
| 1555 | goto out_bdput; |
Li Zefan | b125130 | 2009-03-20 10:34:00 +0800 | [diff] [blame] | 1556 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1557 | mutex_lock(&bdev->bd_mutex); |
Li Zefan | cd649b8 | 2009-03-20 11:33:55 +0800 | [diff] [blame] | 1558 | |
| 1559 | if (attr == &dev_attr_enable) { |
| 1560 | ret = sprintf(buf, "%u\n", !!q->blk_trace); |
| 1561 | goto out_unlock_bdev; |
| 1562 | } |
| 1563 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1564 | if (q->blk_trace == NULL) |
| 1565 | ret = sprintf(buf, "disabled\n"); |
| 1566 | else if (attr == &dev_attr_act_mask) |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1567 | ret = blk_trace_mask2str(buf, q->blk_trace->act_mask); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1568 | else if (attr == &dev_attr_pid) |
| 1569 | ret = sprintf(buf, "%u\n", q->blk_trace->pid); |
| 1570 | else if (attr == &dev_attr_start_lba) |
| 1571 | ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba); |
| 1572 | else if (attr == &dev_attr_end_lba) |
| 1573 | ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba); |
Li Zefan | cd649b8 | 2009-03-20 11:33:55 +0800 | [diff] [blame] | 1574 | |
| 1575 | out_unlock_bdev: |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1576 | mutex_unlock(&bdev->bd_mutex); |
| 1577 | out_bdput: |
| 1578 | bdput(bdev); |
| 1579 | out_unlock_kernel: |
| 1580 | unlock_kernel(); |
| 1581 | return ret; |
| 1582 | } |
| 1583 | |
| 1584 | static ssize_t sysfs_blk_trace_attr_store(struct device *dev, |
| 1585 | struct device_attribute *attr, |
| 1586 | const char *buf, size_t count) |
| 1587 | { |
| 1588 | struct block_device *bdev; |
| 1589 | struct request_queue *q; |
| 1590 | struct hd_struct *p; |
| 1591 | u64 value; |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1592 | ssize_t ret = -EINVAL; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1593 | |
| 1594 | if (count == 0) |
| 1595 | goto out; |
| 1596 | |
| 1597 | if (attr == &dev_attr_act_mask) { |
| 1598 | if (sscanf(buf, "%llx", &value) != 1) { |
| 1599 | /* Assume it is a list of trace category names */ |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1600 | ret = blk_trace_str2mask(buf); |
| 1601 | if (ret < 0) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1602 | goto out; |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1603 | value = ret; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1604 | } |
| 1605 | } else if (sscanf(buf, "%llu", &value) != 1) |
| 1606 | goto out; |
| 1607 | |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1608 | ret = -ENXIO; |
| 1609 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1610 | lock_kernel(); |
| 1611 | p = dev_to_part(dev); |
| 1612 | bdev = bdget(part_devt(p)); |
| 1613 | if (bdev == NULL) |
| 1614 | goto out_unlock_kernel; |
| 1615 | |
Li Zefan | b125130 | 2009-03-20 10:34:00 +0800 | [diff] [blame] | 1616 | q = blk_trace_get_queue(bdev); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1617 | if (q == NULL) |
| 1618 | goto out_bdput; |
| 1619 | |
| 1620 | mutex_lock(&bdev->bd_mutex); |
Li Zefan | cd649b8 | 2009-03-20 11:33:55 +0800 | [diff] [blame] | 1621 | |
| 1622 | if (attr == &dev_attr_enable) { |
| 1623 | if (value) |
Li Zefan | 9908c30 | 2009-04-14 13:59:34 +0800 | [diff] [blame] | 1624 | ret = blk_trace_setup_queue(q, bdev); |
Li Zefan | cd649b8 | 2009-03-20 11:33:55 +0800 | [diff] [blame] | 1625 | else |
| 1626 | ret = blk_trace_remove_queue(q); |
| 1627 | goto out_unlock_bdev; |
| 1628 | } |
| 1629 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1630 | ret = 0; |
| 1631 | if (q->blk_trace == NULL) |
Li Zefan | 9908c30 | 2009-04-14 13:59:34 +0800 | [diff] [blame] | 1632 | ret = blk_trace_setup_queue(q, bdev); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1633 | |
| 1634 | if (ret == 0) { |
| 1635 | if (attr == &dev_attr_act_mask) |
| 1636 | q->blk_trace->act_mask = value; |
| 1637 | else if (attr == &dev_attr_pid) |
| 1638 | q->blk_trace->pid = value; |
| 1639 | else if (attr == &dev_attr_start_lba) |
| 1640 | q->blk_trace->start_lba = value; |
| 1641 | else if (attr == &dev_attr_end_lba) |
| 1642 | q->blk_trace->end_lba = value; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1643 | } |
Li Zefan | cd649b8 | 2009-03-20 11:33:55 +0800 | [diff] [blame] | 1644 | |
| 1645 | out_unlock_bdev: |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1646 | mutex_unlock(&bdev->bd_mutex); |
| 1647 | out_bdput: |
| 1648 | bdput(bdev); |
| 1649 | out_unlock_kernel: |
| 1650 | unlock_kernel(); |
| 1651 | out: |
Li Zefan | cd649b8 | 2009-03-20 11:33:55 +0800 | [diff] [blame] | 1652 | return ret ? ret : count; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1653 | } |
Li Zefan | cd649b8 | 2009-03-20 11:33:55 +0800 | [diff] [blame] | 1654 | |
Li Zefan | 1d54ad6 | 2009-04-14 14:00:05 +0800 | [diff] [blame] | 1655 | int blk_trace_init_sysfs(struct device *dev) |
| 1656 | { |
| 1657 | return sysfs_create_group(&dev->kobj, &blk_trace_attr_group); |
| 1658 | } |
| 1659 | |