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