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