blob: 243bafc2ec90e61a1dd59e70dd7bc1259dde8db3 [file] [log] [blame]
Jens Axboe2056a782006-03-23 20:00:26 +01001/*
Jens Axboe0fe23472006-09-04 15:41:16 +02002 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
Jens Axboe2056a782006-03-23 20:00:26 +01003 *
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 Axboe2056a782006-03-23 20:00:26 +010018#include <linux/kernel.h>
19#include <linux/blkdev.h>
20#include <linux/blktrace_api.h>
21#include <linux/percpu.h>
22#include <linux/init.h>
23#include <linux/mutex.h>
24#include <linux/debugfs.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040025#include <linux/smp_lock.h>
Olaf Kirchbe1c6342006-12-01 10:39:12 +010026#include <linux/time.h>
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -020027#include <linux/uaccess.h>
Li Zefan55782132009-06-09 13:43:05 +080028
29#include <trace/events/block.h>
30
Frederic Weisbecker2db270a2009-02-07 20:46:45 +010031#include "trace_output.h"
Jens Axboe2056a782006-03-23 20:00:26 +010032
Li Zefan55782132009-06-09 13:43:05 +080033#ifdef CONFIG_BLK_DEV_IO_TRACE
34
Jens Axboe2056a782006-03-23 20:00:26 +010035static unsigned int blktrace_seq __read_mostly = 1;
36
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020037static struct trace_array *blk_tr;
Li Zefan5006ea72009-03-20 09:48:03 +080038static bool blk_tracer_enabled __read_mostly;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020039
40/* Select an alternative, minimalistic output than the original one */
Steven Rostedtef180122009-03-10 14:10:56 -040041#define TRACE_BLK_OPT_CLASSIC 0x1
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020042
43static struct tracer_opt blk_tracer_opts[] = {
44 /* Default disable the minimalistic output */
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -020045 { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020046 { }
47};
48
49static struct tracer_flags blk_tracer_flags = {
50 .val = 0,
51 .opts = blk_tracer_opts,
52};
53
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010054/* Global reference count of probes */
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010055static atomic_t blk_probes_ref = ATOMIC_INIT(0);
56
Li Zefan3c289ba2009-03-20 09:48:26 +080057static void blk_register_tracepoints(void);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010058static void blk_unregister_tracepoints(void);
59
Jens Axboe2056a782006-03-23 20:00:26 +010060/*
Olaf Kirchbe1c6342006-12-01 10:39:12 +010061 * Send out a notify message.
62 */
Jens Axboea8630552006-12-04 09:30:58 +010063static void trace_note(struct blk_trace *bt, pid_t pid, int action,
64 const void *data, size_t len)
Olaf Kirchbe1c6342006-12-01 10:39:12 +010065{
66 struct blk_io_trace *t;
Li Zefan18cea452009-03-27 10:21:54 +080067 struct ring_buffer_event *event = NULL;
Steven Rostedte77405a2009-09-02 14:17:06 -040068 struct ring_buffer *buffer = NULL;
Li Zefan18cea452009-03-27 10:21:54 +080069 int pc = 0;
70 int cpu = smp_processor_id();
71 bool blk_tracer = blk_tracer_enabled;
72
73 if (blk_tracer) {
Steven Rostedte77405a2009-09-02 14:17:06 -040074 buffer = blk_tr->buffer;
Li Zefan18cea452009-03-27 10:21:54 +080075 pc = preempt_count();
Steven Rostedte77405a2009-09-02 14:17:06 -040076 event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
Li Zefan18cea452009-03-27 10:21:54 +080077 sizeof(*t) + len,
78 0, pc);
79 if (!event)
80 return;
81 t = ring_buffer_event_data(event);
82 goto record_it;
83 }
Olaf Kirchbe1c6342006-12-01 10:39:12 +010084
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020085 if (!bt->rchan)
86 return;
87
Olaf Kirchbe1c6342006-12-01 10:39:12 +010088 t = relay_reserve(bt->rchan, sizeof(*t) + len);
Jens Axboed3d9d2a2006-12-04 09:27:41 +010089 if (t) {
Jens Axboed3d9d2a2006-12-04 09:27:41 +010090 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
Ingo Molnar2997c8c2008-01-11 13:35:54 +010091 t->time = ktime_to_ns(ktime_get());
Li Zefan18cea452009-03-27 10:21:54 +080092record_it:
Jens Axboed3d9d2a2006-12-04 09:27:41 +010093 t->device = bt->dev;
94 t->action = action;
95 t->pid = pid;
96 t->cpu = cpu;
97 t->pdu_len = len;
98 memcpy((void *) t + sizeof(*t), data, len);
Li Zefan18cea452009-03-27 10:21:54 +080099
100 if (blk_tracer)
Steven Rostedte77405a2009-09-02 14:17:06 -0400101 trace_buffer_unlock_commit(buffer, event, 0, pc);
Jens Axboed3d9d2a2006-12-04 09:27:41 +0100102 }
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100103}
104
105/*
Jens Axboe2056a782006-03-23 20:00:26 +0100106 * Send out a notify for this process, if we haven't done so since a trace
107 * started
108 */
109static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk)
110{
Jens Axboea8630552006-12-04 09:30:58 +0100111 tsk->btrace_seq = blktrace_seq;
112 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm));
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100113}
Jens Axboe2056a782006-03-23 20:00:26 +0100114
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100115static void trace_note_time(struct blk_trace *bt)
116{
117 struct timespec now;
118 unsigned long flags;
119 u32 words[2];
120
121 getnstimeofday(&now);
122 words[0] = now.tv_sec;
123 words[1] = now.tv_nsec;
124
125 local_irq_save(flags);
126 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
127 local_irq_restore(flags);
Jens Axboe2056a782006-03-23 20:00:26 +0100128}
129
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200130void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
131{
132 int n;
133 va_list args;
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200134 unsigned long flags;
Jens Axboe64565912008-05-28 14:45:33 +0200135 char *buf;
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200136
Li Zefan18cea452009-03-27 10:21:54 +0800137 if (unlikely(bt->trace_state != Blktrace_running &&
138 !blk_tracer_enabled))
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200139 return;
140
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200141 local_irq_save(flags);
Jens Axboe64565912008-05-28 14:45:33 +0200142 buf = per_cpu_ptr(bt->msg_data, smp_processor_id());
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200143 va_start(args, fmt);
Jens Axboe64565912008-05-28 14:45:33 +0200144 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200145 va_end(args);
146
Jens Axboe64565912008-05-28 14:45:33 +0200147 trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200148 local_irq_restore(flags);
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200149}
150EXPORT_SYMBOL_GPL(__trace_note_message);
151
Jens Axboe2056a782006-03-23 20:00:26 +0100152static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
153 pid_t pid)
154{
155 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
156 return 1;
Shawn Dud0deef52009-04-14 13:58:56 +0800157 if (sector && (sector < bt->start_lba || sector > bt->end_lba))
Jens Axboe2056a782006-03-23 20:00:26 +0100158 return 1;
159 if (bt->pid && pid != bt->pid)
160 return 1;
161
162 return 0;
163}
164
165/*
166 * Data direction bit lookup
167 */
Li Zefane4955c92009-03-24 16:04:37 +0800168static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ),
169 BLK_TC_ACT(BLK_TC_WRITE) };
Jens Axboe2056a782006-03-23 20:00:26 +0100170
David Woodhouse35ba8f72008-08-10 12:33:00 +0100171/* The ilog2() calls fall out because they're constant */
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200172#define MASK_TC_BIT(rw, __name) ((rw & (1 << BIO_RW_ ## __name)) << \
173 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name))
Jens Axboe2056a782006-03-23 20:00:26 +0100174
175/*
176 * The worker for the various blk_add_trace*() types. Fills out a
177 * blk_io_trace structure and places it in a per-cpu subbuffer.
178 */
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100179static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
Jens Axboe2056a782006-03-23 20:00:26 +0100180 int rw, u32 what, int error, int pdu_len, void *pdu_data)
181{
182 struct task_struct *tsk = current;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200183 struct ring_buffer_event *event = NULL;
Steven Rostedte77405a2009-09-02 14:17:06 -0400184 struct ring_buffer *buffer = NULL;
Jens Axboe2056a782006-03-23 20:00:26 +0100185 struct blk_io_trace *t;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200186 unsigned long flags = 0;
Jens Axboe2056a782006-03-23 20:00:26 +0100187 unsigned long *sequence;
188 pid_t pid;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200189 int cpu, pc = 0;
Li Zefan18cea452009-03-27 10:21:54 +0800190 bool blk_tracer = blk_tracer_enabled;
Jens Axboe2056a782006-03-23 20:00:26 +0100191
Li Zefan18cea452009-03-27 10:21:54 +0800192 if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer))
Jens Axboe2056a782006-03-23 20:00:26 +0100193 return;
194
195 what |= ddir_act[rw & WRITE];
David Woodhouse35ba8f72008-08-10 12:33:00 +0100196 what |= MASK_TC_BIT(rw, BARRIER);
Ingo Molnar72c26c92009-02-19 09:00:35 +0100197 what |= MASK_TC_BIT(rw, SYNCIO);
David Woodhouse35ba8f72008-08-10 12:33:00 +0100198 what |= MASK_TC_BIT(rw, AHEAD);
199 what |= MASK_TC_BIT(rw, META);
200 what |= MASK_TC_BIT(rw, DISCARD);
Jens Axboe2056a782006-03-23 20:00:26 +0100201
202 pid = tsk->pid;
Shawn Dud0deef52009-04-14 13:58:56 +0800203 if (act_log_check(bt, what, sector, pid))
Jens Axboe2056a782006-03-23 20:00:26 +0100204 return;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200205 cpu = raw_smp_processor_id();
206
Li Zefan18cea452009-03-27 10:21:54 +0800207 if (blk_tracer) {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200208 tracing_record_cmdline(current);
209
Steven Rostedte77405a2009-09-02 14:17:06 -0400210 buffer = blk_tr->buffer;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200211 pc = preempt_count();
Steven Rostedte77405a2009-09-02 14:17:06 -0400212 event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200213 sizeof(*t) + pdu_len,
214 0, pc);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200215 if (!event)
216 return;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200217 t = ring_buffer_event_data(event);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200218 goto record_it;
219 }
Jens Axboe2056a782006-03-23 20:00:26 +0100220
221 /*
222 * A word about the locking here - we disable interrupts to reserve
223 * some space in the relay per-cpu buffer, to prevent an irq
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200224 * from coming in and stepping on our toes.
Jens Axboe2056a782006-03-23 20:00:26 +0100225 */
226 local_irq_save(flags);
227
228 if (unlikely(tsk->btrace_seq != blktrace_seq))
229 trace_note_tsk(bt, tsk);
230
231 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
232 if (t) {
Jens Axboe2056a782006-03-23 20:00:26 +0100233 sequence = per_cpu_ptr(bt->sequence, cpu);
234
235 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
236 t->sequence = ++(*sequence);
Ingo Molnar2997c8c2008-01-11 13:35:54 +0100237 t->time = ktime_to_ns(ktime_get());
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200238record_it:
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -0200239 /*
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200240 * These two are not needed in ftrace as they are in the
241 * generic trace_entry, filled by tracing_generic_entry_update,
242 * but for the trace_event->bin() synthesizer benefit we do it
243 * here too.
244 */
245 t->cpu = cpu;
246 t->pid = pid;
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -0200247
Jens Axboe2056a782006-03-23 20:00:26 +0100248 t->sector = sector;
249 t->bytes = bytes;
250 t->action = what;
Jens Axboe2056a782006-03-23 20:00:26 +0100251 t->device = bt->dev;
Jens Axboe2056a782006-03-23 20:00:26 +0100252 t->error = error;
253 t->pdu_len = pdu_len;
254
255 if (pdu_len)
256 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200257
Li Zefan18cea452009-03-27 10:21:54 +0800258 if (blk_tracer) {
Steven Rostedte77405a2009-09-02 14:17:06 -0400259 trace_buffer_unlock_commit(buffer, event, 0, pc);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200260 return;
261 }
Jens Axboe2056a782006-03-23 20:00:26 +0100262 }
263
264 local_irq_restore(flags);
265}
266
Jens Axboe2056a782006-03-23 20:00:26 +0100267static struct dentry *blk_tree_root;
Jens Axboe11a57152008-01-11 13:37:01 +0100268static DEFINE_MUTEX(blk_tree_mutex);
Jens Axboe2056a782006-03-23 20:00:26 +0100269
Li Zefanad5dd542009-03-27 10:20:24 +0800270static void blk_trace_free(struct blk_trace *bt)
Jens Axboe2056a782006-03-23 20:00:26 +0100271{
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200272 debugfs_remove(bt->msg_file);
Jens Axboe2056a782006-03-23 20:00:26 +0100273 debugfs_remove(bt->dropped_file);
Stefan Rasplfd51d252009-05-19 09:59:08 +0200274 debugfs_remove(bt->dir);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100275 relay_close(bt->rchan);
Jens Axboe2056a782006-03-23 20:00:26 +0100276 free_percpu(bt->sequence);
Jens Axboe64565912008-05-28 14:45:33 +0200277 free_percpu(bt->msg_data);
Jens Axboe2056a782006-03-23 20:00:26 +0100278 kfree(bt);
Li Zefanad5dd542009-03-27 10:20:24 +0800279}
280
281static void blk_trace_cleanup(struct blk_trace *bt)
282{
283 blk_trace_free(bt);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100284 if (atomic_dec_and_test(&blk_probes_ref))
285 blk_unregister_tracepoints();
Jens Axboe2056a782006-03-23 20:00:26 +0100286}
287
Christof Schmitt6da127a2008-01-11 10:09:43 +0100288int blk_trace_remove(struct request_queue *q)
Jens Axboe2056a782006-03-23 20:00:26 +0100289{
290 struct blk_trace *bt;
291
292 bt = xchg(&q->blk_trace, NULL);
293 if (!bt)
294 return -EINVAL;
295
Li Zefan55547202009-03-25 17:21:26 +0800296 if (bt->trace_state != Blktrace_running)
Jens Axboe2056a782006-03-23 20:00:26 +0100297 blk_trace_cleanup(bt);
298
299 return 0;
300}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100301EXPORT_SYMBOL_GPL(blk_trace_remove);
Jens Axboe2056a782006-03-23 20:00:26 +0100302
303static int blk_dropped_open(struct inode *inode, struct file *filp)
304{
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700305 filp->private_data = inode->i_private;
Jens Axboe2056a782006-03-23 20:00:26 +0100306
307 return 0;
308}
309
310static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
311 size_t count, loff_t *ppos)
312{
313 struct blk_trace *bt = filp->private_data;
314 char buf[16];
315
316 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
317
318 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
319}
320
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800321static const struct file_operations blk_dropped_fops = {
Jens Axboe2056a782006-03-23 20:00:26 +0100322 .owner = THIS_MODULE,
323 .open = blk_dropped_open,
324 .read = blk_dropped_read,
325};
326
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200327static int blk_msg_open(struct inode *inode, struct file *filp)
328{
329 filp->private_data = inode->i_private;
330
331 return 0;
332}
333
334static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
335 size_t count, loff_t *ppos)
336{
337 char *msg;
338 struct blk_trace *bt;
339
Li Zefan7635b032009-04-03 15:31:34 +0800340 if (count >= BLK_TN_MAX_MSG)
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200341 return -EINVAL;
342
Carl Henrik Lundea4b3ada2009-04-03 14:27:15 +0200343 msg = kmalloc(count + 1, GFP_KERNEL);
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200344 if (msg == NULL)
345 return -ENOMEM;
346
347 if (copy_from_user(msg, buffer, count)) {
348 kfree(msg);
349 return -EFAULT;
350 }
351
Carl Henrik Lundea4b3ada2009-04-03 14:27:15 +0200352 msg[count] = '\0';
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200353 bt = filp->private_data;
354 __trace_note_message(bt, "%s", msg);
355 kfree(msg);
356
357 return count;
358}
359
360static const struct file_operations blk_msg_fops = {
361 .owner = THIS_MODULE,
362 .open = blk_msg_open,
363 .write = blk_msg_write,
364};
365
Jens Axboe2056a782006-03-23 20:00:26 +0100366/*
367 * Keep track of how many times we encountered a full subbuffer, to aid
368 * the user space app in telling how many lost events there were.
369 */
370static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
371 void *prev_subbuf, size_t prev_padding)
372{
373 struct blk_trace *bt;
374
375 if (!relay_buf_full(buf))
376 return 1;
377
378 bt = buf->chan->private_data;
379 atomic_inc(&bt->dropped);
380 return 0;
381}
382
383static int blk_remove_buf_file_callback(struct dentry *dentry)
384{
Jens Axboef48fc4d2009-01-05 10:17:25 +0100385 struct dentry *parent = dentry->d_parent;
Jens Axboe2056a782006-03-23 20:00:26 +0100386 debugfs_remove(dentry);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100387
388 /*
389 * this will fail for all but the last file, but that is ok. what we
390 * care about is the top level buts->name directory going away, when
391 * the last trace file is gone. Then we don't have to rmdir() that
392 * manually on trace stop, so it nicely solves the issue with
393 * force killing of running traces.
394 */
395
396 debugfs_remove(parent);
Jens Axboe2056a782006-03-23 20:00:26 +0100397 return 0;
398}
399
400static struct dentry *blk_create_buf_file_callback(const char *filename,
401 struct dentry *parent,
402 int mode,
403 struct rchan_buf *buf,
404 int *is_global)
405{
406 return debugfs_create_file(filename, mode, parent, buf,
407 &relay_file_operations);
408}
409
410static struct rchan_callbacks blk_relay_callbacks = {
411 .subbuf_start = blk_subbuf_start_callback,
412 .create_buf_file = blk_create_buf_file_callback,
413 .remove_buf_file = blk_remove_buf_file_callback,
414};
415
Li Zefan9908c302009-04-14 13:59:34 +0800416static void blk_trace_setup_lba(struct blk_trace *bt,
417 struct block_device *bdev)
418{
419 struct hd_struct *part = NULL;
420
421 if (bdev)
422 part = bdev->bd_part;
423
424 if (part) {
425 bt->start_lba = part->start_sect;
426 bt->end_lba = part->start_sect + part->nr_sects;
427 } else {
428 bt->start_lba = 0;
429 bt->end_lba = -1ULL;
430 }
431}
432
Jens Axboe2056a782006-03-23 20:00:26 +0100433/*
434 * Setup everything required to start tracing
435 */
Christof Schmitt6da127a2008-01-11 10:09:43 +0100436int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
Shawn Dud0deef52009-04-14 13:58:56 +0800437 struct block_device *bdev,
438 struct blk_user_trace_setup *buts)
Jens Axboe2056a782006-03-23 20:00:26 +0100439{
Jens Axboe2056a782006-03-23 20:00:26 +0100440 struct blk_trace *old_bt, *bt = NULL;
441 struct dentry *dir = NULL;
Jens Axboe2056a782006-03-23 20:00:26 +0100442 int ret, i;
443
Arnd Bergmann171044d42007-10-09 13:23:53 +0200444 if (!buts->buf_size || !buts->buf_nr)
Jens Axboe2056a782006-03-23 20:00:26 +0100445 return -EINVAL;
446
Jens Axboe0497b342008-10-01 16:16:25 +0200447 strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
448 buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
Jens Axboe2056a782006-03-23 20:00:26 +0100449
450 /*
451 * some device names have larger paths - convert the slashes
452 * to underscores for this to work as expected
453 */
Arnd Bergmann171044d42007-10-09 13:23:53 +0200454 for (i = 0; i < strlen(buts->name); i++)
455 if (buts->name[i] == '/')
456 buts->name[i] = '_';
Jens Axboe2056a782006-03-23 20:00:26 +0100457
Jens Axboe2056a782006-03-23 20:00:26 +0100458 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
459 if (!bt)
Li Zefanad5dd542009-03-27 10:20:24 +0800460 return -ENOMEM;
Jens Axboe2056a782006-03-23 20:00:26 +0100461
Li Zefanad5dd542009-03-27 10:20:24 +0800462 ret = -ENOMEM;
Jens Axboe2056a782006-03-23 20:00:26 +0100463 bt->sequence = alloc_percpu(unsigned long);
464 if (!bt->sequence)
465 goto err;
466
Ingo Molnarf0ef0392009-03-06 16:44:14 +0100467 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
Jens Axboe64565912008-05-28 14:45:33 +0200468 if (!bt->msg_data)
469 goto err;
470
Jens Axboe2056a782006-03-23 20:00:26 +0100471 ret = -ENOENT;
Jens Axboef48fc4d2009-01-05 10:17:25 +0100472
Li Zefanb5230b52009-03-25 17:19:33 +0800473 mutex_lock(&blk_tree_mutex);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100474 if (!blk_tree_root) {
475 blk_tree_root = debugfs_create_dir("block", NULL);
Li Zefanb5230b52009-03-25 17:19:33 +0800476 if (!blk_tree_root) {
477 mutex_unlock(&blk_tree_mutex);
Li Zefan1a176622009-03-20 09:47:30 +0800478 goto err;
Li Zefanb5230b52009-03-25 17:19:33 +0800479 }
Jens Axboef48fc4d2009-01-05 10:17:25 +0100480 }
Li Zefanb5230b52009-03-25 17:19:33 +0800481 mutex_unlock(&blk_tree_mutex);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100482
483 dir = debugfs_create_dir(buts->name, blk_tree_root);
484
Jens Axboe2056a782006-03-23 20:00:26 +0100485 if (!dir)
486 goto err;
487
488 bt->dir = dir;
Christof Schmitt6da127a2008-01-11 10:09:43 +0100489 bt->dev = dev;
Jens Axboe2056a782006-03-23 20:00:26 +0100490 atomic_set(&bt->dropped, 0);
491
492 ret = -EIO;
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200493 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt,
494 &blk_dropped_fops);
Jens Axboe2056a782006-03-23 20:00:26 +0100495 if (!bt->dropped_file)
496 goto err;
497
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200498 bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
499 if (!bt->msg_file)
500 goto err;
501
Arnd Bergmann171044d42007-10-09 13:23:53 +0200502 bt->rchan = relay_open("trace", dir, buts->buf_size,
503 buts->buf_nr, &blk_relay_callbacks, bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100504 if (!bt->rchan)
505 goto err;
Jens Axboe2056a782006-03-23 20:00:26 +0100506
Arnd Bergmann171044d42007-10-09 13:23:53 +0200507 bt->act_mask = buts->act_mask;
Jens Axboe2056a782006-03-23 20:00:26 +0100508 if (!bt->act_mask)
509 bt->act_mask = (u16) -1;
510
Li Zefan9908c302009-04-14 13:59:34 +0800511 blk_trace_setup_lba(bt, bdev);
Jens Axboe2056a782006-03-23 20:00:26 +0100512
Shawn Dud0deef52009-04-14 13:58:56 +0800513 /* overwrite with user settings */
514 if (buts->start_lba)
515 bt->start_lba = buts->start_lba;
516 if (buts->end_lba)
517 bt->end_lba = buts->end_lba;
518
Arnd Bergmann171044d42007-10-09 13:23:53 +0200519 bt->pid = buts->pid;
Jens Axboe2056a782006-03-23 20:00:26 +0100520 bt->trace_state = Blktrace_setup;
521
522 ret = -EBUSY;
523 old_bt = xchg(&q->blk_trace, bt);
524 if (old_bt) {
525 (void) xchg(&q->blk_trace, old_bt);
526 goto err;
527 }
528
Li Zefan17ba97e2009-03-27 10:20:09 +0800529 if (atomic_inc_return(&blk_probes_ref) == 1)
Li Zefancbe28292009-03-20 09:48:47 +0800530 blk_register_tracepoints();
531
Jens Axboe2056a782006-03-23 20:00:26 +0100532 return 0;
533err:
Li Zefanad5dd542009-03-27 10:20:24 +0800534 blk_trace_free(bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100535 return ret;
536}
537
Christof Schmitt6da127a2008-01-11 10:09:43 +0100538int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
Shawn Dud0deef52009-04-14 13:58:56 +0800539 struct block_device *bdev,
Christof Schmitt6da127a2008-01-11 10:09:43 +0100540 char __user *arg)
Arnd Bergmann171044d42007-10-09 13:23:53 +0200541{
542 struct blk_user_trace_setup buts;
543 int ret;
544
545 ret = copy_from_user(&buts, arg, sizeof(buts));
546 if (ret)
547 return -EFAULT;
548
Shawn Dud0deef52009-04-14 13:58:56 +0800549 ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200550 if (ret)
551 return ret;
552
553 if (copy_to_user(arg, &buts, sizeof(buts)))
554 return -EFAULT;
555
556 return 0;
557}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100558EXPORT_SYMBOL_GPL(blk_trace_setup);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200559
Christof Schmitt6da127a2008-01-11 10:09:43 +0100560int blk_trace_startstop(struct request_queue *q, int start)
Jens Axboe2056a782006-03-23 20:00:26 +0100561{
Jens Axboe2056a782006-03-23 20:00:26 +0100562 int ret;
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200563 struct blk_trace *bt = q->blk_trace;
Jens Axboe2056a782006-03-23 20:00:26 +0100564
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200565 if (bt == NULL)
Jens Axboe2056a782006-03-23 20:00:26 +0100566 return -EINVAL;
567
568 /*
569 * For starting a trace, we can transition from a setup or stopped
570 * trace. For stopping a trace, the state must be running
571 */
572 ret = -EINVAL;
573 if (start) {
574 if (bt->trace_state == Blktrace_setup ||
575 bt->trace_state == Blktrace_stopped) {
576 blktrace_seq++;
577 smp_mb();
578 bt->trace_state = Blktrace_running;
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100579
580 trace_note_time(bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100581 ret = 0;
582 }
583 } else {
584 if (bt->trace_state == Blktrace_running) {
585 bt->trace_state = Blktrace_stopped;
586 relay_flush(bt->rchan);
587 ret = 0;
588 }
589 }
590
591 return ret;
592}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100593EXPORT_SYMBOL_GPL(blk_trace_startstop);
Jens Axboe2056a782006-03-23 20:00:26 +0100594
595/**
596 * blk_trace_ioctl: - handle the ioctls associated with tracing
597 * @bdev: the block device
Steven Rostedtef180122009-03-10 14:10:56 -0400598 * @cmd: the ioctl cmd
Jens Axboe2056a782006-03-23 20:00:26 +0100599 * @arg: the argument data, if any
600 *
601 **/
602int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
603{
Jens Axboe165125e2007-07-24 09:28:11 +0200604 struct request_queue *q;
Jens Axboe2056a782006-03-23 20:00:26 +0100605 int ret, start = 0;
Christof Schmitt6da127a2008-01-11 10:09:43 +0100606 char b[BDEVNAME_SIZE];
Jens Axboe2056a782006-03-23 20:00:26 +0100607
608 q = bdev_get_queue(bdev);
609 if (!q)
610 return -ENXIO;
611
612 mutex_lock(&bdev->bd_mutex);
613
614 switch (cmd) {
615 case BLKTRACESETUP:
Jean Delvaref36f21e2008-05-12 14:02:33 -0700616 bdevname(bdev, b);
Shawn Dud0deef52009-04-14 13:58:56 +0800617 ret = blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
Jens Axboe2056a782006-03-23 20:00:26 +0100618 break;
619 case BLKTRACESTART:
620 start = 1;
621 case BLKTRACESTOP:
622 ret = blk_trace_startstop(q, start);
623 break;
624 case BLKTRACETEARDOWN:
625 ret = blk_trace_remove(q);
626 break;
627 default:
628 ret = -ENOTTY;
629 break;
630 }
631
632 mutex_unlock(&bdev->bd_mutex);
633 return ret;
634}
635
636/**
637 * blk_trace_shutdown: - stop and cleanup trace structures
638 * @q: the request queue associated with the device
639 *
640 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200641void blk_trace_shutdown(struct request_queue *q)
Jens Axboe2056a782006-03-23 20:00:26 +0100642{
Alexey Dobriyan6c5c9342006-09-29 01:59:40 -0700643 if (q->blk_trace) {
644 blk_trace_startstop(q, 0);
645 blk_trace_remove(q);
646 }
Jens Axboe2056a782006-03-23 20:00:26 +0100647}
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100648
649/*
650 * blktrace probes
651 */
652
653/**
654 * blk_add_trace_rq - Add a trace for a request oriented action
655 * @q: queue the io is for
656 * @rq: the source request
657 * @what: the action
658 *
659 * Description:
660 * Records an action against a request. Will log the bio offset + size.
661 *
662 **/
663static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
664 u32 what)
665{
666 struct blk_trace *bt = q->blk_trace;
667 int rw = rq->cmd_flags & 0x03;
668
669 if (likely(!bt))
670 return;
671
672 if (blk_discard_rq(rq))
673 rw |= (1 << BIO_RW_DISCARD);
674
675 if (blk_pc_request(rq)) {
676 what |= BLK_TC_ACT(BLK_TC_PC);
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900677 __blk_add_trace(bt, 0, blk_rq_bytes(rq), rw,
678 what, rq->errors, rq->cmd_len, rq->cmd);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100679 } else {
680 what |= BLK_TC_ACT(BLK_TC_FS);
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900681 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), rw,
682 what, rq->errors, 0, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100683 }
684}
685
686static void blk_add_trace_rq_abort(struct request_queue *q, struct request *rq)
687{
688 blk_add_trace_rq(q, rq, BLK_TA_ABORT);
689}
690
691static void blk_add_trace_rq_insert(struct request_queue *q, struct request *rq)
692{
693 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
694}
695
696static void blk_add_trace_rq_issue(struct request_queue *q, struct request *rq)
697{
698 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
699}
700
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200701static void blk_add_trace_rq_requeue(struct request_queue *q,
702 struct request *rq)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100703{
704 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
705}
706
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200707static void blk_add_trace_rq_complete(struct request_queue *q,
708 struct request *rq)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100709{
710 blk_add_trace_rq(q, rq, BLK_TA_COMPLETE);
711}
712
713/**
714 * blk_add_trace_bio - Add a trace for a bio oriented action
715 * @q: queue the io is for
716 * @bio: the source bio
717 * @what: the action
718 *
719 * Description:
720 * Records an action against a bio. Will log the bio offset + size.
721 *
722 **/
723static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
724 u32 what)
725{
726 struct blk_trace *bt = q->blk_trace;
727
728 if (likely(!bt))
729 return;
730
731 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what,
732 !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
733}
734
735static void blk_add_trace_bio_bounce(struct request_queue *q, struct bio *bio)
736{
737 blk_add_trace_bio(q, bio, BLK_TA_BOUNCE);
738}
739
740static void blk_add_trace_bio_complete(struct request_queue *q, struct bio *bio)
741{
742 blk_add_trace_bio(q, bio, BLK_TA_COMPLETE);
743}
744
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200745static void blk_add_trace_bio_backmerge(struct request_queue *q,
746 struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100747{
748 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
749}
750
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200751static void blk_add_trace_bio_frontmerge(struct request_queue *q,
752 struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100753{
754 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
755}
756
757static void blk_add_trace_bio_queue(struct request_queue *q, struct bio *bio)
758{
759 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
760}
761
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200762static void blk_add_trace_getrq(struct request_queue *q,
763 struct bio *bio, int rw)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100764{
765 if (bio)
766 blk_add_trace_bio(q, bio, BLK_TA_GETRQ);
767 else {
768 struct blk_trace *bt = q->blk_trace;
769
770 if (bt)
771 __blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL);
772 }
773}
774
775
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200776static void blk_add_trace_sleeprq(struct request_queue *q,
777 struct bio *bio, int rw)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100778{
779 if (bio)
780 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ);
781 else {
782 struct blk_trace *bt = q->blk_trace;
783
784 if (bt)
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200785 __blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ,
786 0, 0, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100787 }
788}
789
790static void blk_add_trace_plug(struct request_queue *q)
791{
792 struct blk_trace *bt = q->blk_trace;
793
794 if (bt)
795 __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
796}
797
798static void blk_add_trace_unplug_io(struct request_queue *q)
799{
800 struct blk_trace *bt = q->blk_trace;
801
802 if (bt) {
803 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
804 __be64 rpdu = cpu_to_be64(pdu);
805
806 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_IO, 0,
807 sizeof(rpdu), &rpdu);
808 }
809}
810
811static void blk_add_trace_unplug_timer(struct request_queue *q)
812{
813 struct blk_trace *bt = q->blk_trace;
814
815 if (bt) {
816 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
817 __be64 rpdu = cpu_to_be64(pdu);
818
819 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_TIMER, 0,
820 sizeof(rpdu), &rpdu);
821 }
822}
823
824static void blk_add_trace_split(struct request_queue *q, struct bio *bio,
825 unsigned int pdu)
826{
827 struct blk_trace *bt = q->blk_trace;
828
829 if (bt) {
830 __be64 rpdu = cpu_to_be64(pdu);
831
832 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
833 BLK_TA_SPLIT, !bio_flagged(bio, BIO_UPTODATE),
834 sizeof(rpdu), &rpdu);
835 }
836}
837
838/**
839 * blk_add_trace_remap - Add a trace for a remap operation
840 * @q: queue the io is for
841 * @bio: the source bio
842 * @dev: target device
Alan D. Brunellea42aaa32009-05-04 16:27:26 -0400843 * @from: source sector
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100844 *
845 * Description:
846 * Device mapper or raid target sometimes need to split a bio because
847 * it spans a stripe (or similar). Add a trace for that action.
848 *
849 **/
850static void blk_add_trace_remap(struct request_queue *q, struct bio *bio,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -0400851 dev_t dev, sector_t from)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100852{
853 struct blk_trace *bt = q->blk_trace;
854 struct blk_io_trace_remap r;
855
856 if (likely(!bt))
857 return;
858
Alan D. Brunellea42aaa32009-05-04 16:27:26 -0400859 r.device_from = cpu_to_be32(dev);
860 r.device_to = cpu_to_be32(bio->bi_bdev->bd_dev);
861 r.sector_from = cpu_to_be64(from);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100862
Alan D. Brunelle22a7c312009-05-04 16:35:08 -0400863 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
864 BLK_TA_REMAP, !bio_flagged(bio, BIO_UPTODATE),
865 sizeof(r), &r);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100866}
867
868/**
869 * blk_add_driver_data - Add binary message with driver-specific data
870 * @q: queue the io is for
871 * @rq: io request
872 * @data: driver-specific data
873 * @len: length of driver-specific data
874 *
875 * Description:
876 * Some drivers might want to write driver-specific data per request.
877 *
878 **/
879void blk_add_driver_data(struct request_queue *q,
880 struct request *rq,
881 void *data, size_t len)
882{
883 struct blk_trace *bt = q->blk_trace;
884
885 if (likely(!bt))
886 return;
887
888 if (blk_pc_request(rq))
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900889 __blk_add_trace(bt, 0, blk_rq_bytes(rq), 0,
890 BLK_TA_DRV_DATA, rq->errors, len, data);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100891 else
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900892 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), 0,
893 BLK_TA_DRV_DATA, rq->errors, len, data);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100894}
895EXPORT_SYMBOL_GPL(blk_add_driver_data);
896
Li Zefan3c289ba2009-03-20 09:48:26 +0800897static void blk_register_tracepoints(void)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100898{
899 int ret;
900
901 ret = register_trace_block_rq_abort(blk_add_trace_rq_abort);
902 WARN_ON(ret);
903 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert);
904 WARN_ON(ret);
905 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue);
906 WARN_ON(ret);
907 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue);
908 WARN_ON(ret);
909 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete);
910 WARN_ON(ret);
911 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce);
912 WARN_ON(ret);
913 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete);
914 WARN_ON(ret);
915 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
916 WARN_ON(ret);
917 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
918 WARN_ON(ret);
919 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue);
920 WARN_ON(ret);
921 ret = register_trace_block_getrq(blk_add_trace_getrq);
922 WARN_ON(ret);
923 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq);
924 WARN_ON(ret);
925 ret = register_trace_block_plug(blk_add_trace_plug);
926 WARN_ON(ret);
927 ret = register_trace_block_unplug_timer(blk_add_trace_unplug_timer);
928 WARN_ON(ret);
929 ret = register_trace_block_unplug_io(blk_add_trace_unplug_io);
930 WARN_ON(ret);
931 ret = register_trace_block_split(blk_add_trace_split);
932 WARN_ON(ret);
933 ret = register_trace_block_remap(blk_add_trace_remap);
934 WARN_ON(ret);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100935}
936
937static void blk_unregister_tracepoints(void)
938{
939 unregister_trace_block_remap(blk_add_trace_remap);
940 unregister_trace_block_split(blk_add_trace_split);
941 unregister_trace_block_unplug_io(blk_add_trace_unplug_io);
942 unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer);
943 unregister_trace_block_plug(blk_add_trace_plug);
944 unregister_trace_block_sleeprq(blk_add_trace_sleeprq);
945 unregister_trace_block_getrq(blk_add_trace_getrq);
946 unregister_trace_block_bio_queue(blk_add_trace_bio_queue);
947 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
948 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
949 unregister_trace_block_bio_complete(blk_add_trace_bio_complete);
950 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce);
951 unregister_trace_block_rq_complete(blk_add_trace_rq_complete);
952 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue);
953 unregister_trace_block_rq_issue(blk_add_trace_rq_issue);
954 unregister_trace_block_rq_insert(blk_add_trace_rq_insert);
955 unregister_trace_block_rq_abort(blk_add_trace_rq_abort);
956
957 tracepoint_synchronize_unregister();
958}
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200959
960/*
961 * struct blk_io_tracer formatting routines
962 */
963
964static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
965{
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200966 int i = 0;
Li Zefan65796342009-03-24 16:05:06 +0800967 int tc = t->action >> BLK_TC_SHIFT;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200968
Li Zefan18cea452009-03-27 10:21:54 +0800969 if (t->action == BLK_TN_MESSAGE) {
970 rwbs[i++] = 'N';
971 goto out;
972 }
973
Li Zefan65796342009-03-24 16:05:06 +0800974 if (tc & BLK_TC_DISCARD)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200975 rwbs[i++] = 'D';
Li Zefan65796342009-03-24 16:05:06 +0800976 else if (tc & BLK_TC_WRITE)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200977 rwbs[i++] = 'W';
978 else if (t->bytes)
979 rwbs[i++] = 'R';
980 else
981 rwbs[i++] = 'N';
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200982
Li Zefan65796342009-03-24 16:05:06 +0800983 if (tc & BLK_TC_AHEAD)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200984 rwbs[i++] = 'A';
Li Zefan65796342009-03-24 16:05:06 +0800985 if (tc & BLK_TC_BARRIER)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200986 rwbs[i++] = 'B';
Li Zefan65796342009-03-24 16:05:06 +0800987 if (tc & BLK_TC_SYNC)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200988 rwbs[i++] = 'S';
Li Zefan65796342009-03-24 16:05:06 +0800989 if (tc & BLK_TC_META)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200990 rwbs[i++] = 'M';
Li Zefan18cea452009-03-27 10:21:54 +0800991out:
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200992 rwbs[i] = '\0';
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200993}
994
995static inline
996const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
997{
998 return (const struct blk_io_trace *)ent;
999}
1000
1001static inline const void *pdu_start(const struct trace_entry *ent)
1002{
1003 return te_blk_io_trace(ent) + 1;
1004}
1005
Li Zefan66de7792009-04-01 16:19:19 +08001006static inline u32 t_action(const struct trace_entry *ent)
1007{
1008 return te_blk_io_trace(ent)->action;
1009}
1010
1011static inline u32 t_bytes(const struct trace_entry *ent)
1012{
1013 return te_blk_io_trace(ent)->bytes;
1014}
1015
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001016static inline u32 t_sec(const struct trace_entry *ent)
1017{
1018 return te_blk_io_trace(ent)->bytes >> 9;
1019}
1020
1021static inline unsigned long long t_sector(const struct trace_entry *ent)
1022{
1023 return te_blk_io_trace(ent)->sector;
1024}
1025
1026static inline __u16 t_error(const struct trace_entry *ent)
1027{
Li Zefane0dc81b2009-03-24 16:05:51 +08001028 return te_blk_io_trace(ent)->error;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001029}
1030
1031static __u64 get_pdu_int(const struct trace_entry *ent)
1032{
1033 const __u64 *val = pdu_start(ent);
1034 return be64_to_cpu(*val);
1035}
1036
1037static void get_pdu_remap(const struct trace_entry *ent,
1038 struct blk_io_trace_remap *r)
1039{
1040 const struct blk_io_trace_remap *__r = pdu_start(ent);
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001041 __u64 sector_from = __r->sector_from;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001042
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001043 r->device_from = be32_to_cpu(__r->device_from);
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001044 r->device_to = be32_to_cpu(__r->device_to);
1045 r->sector_from = be64_to_cpu(sector_from);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001046}
1047
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001048typedef int (blk_log_action_t) (struct trace_iterator *iter, const char *act);
1049
1050static int blk_log_action_classic(struct trace_iterator *iter, const char *act)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001051{
1052 char rwbs[6];
Li Zefan35ac51b2009-03-27 10:19:46 +08001053 unsigned long long ts = iter->ts;
1054 unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001055 unsigned secs = (unsigned long)ts;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001056 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001057
1058 fill_rwbs(rwbs, t);
1059
1060 return trace_seq_printf(&iter->seq,
Li Zefan35ac51b2009-03-27 10:19:46 +08001061 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001062 MAJOR(t->device), MINOR(t->device), iter->cpu,
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001063 secs, nsec_rem, iter->ent->pid, act, rwbs);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001064}
1065
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001066static int blk_log_action(struct trace_iterator *iter, const char *act)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001067{
1068 char rwbs[6];
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001069 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1070
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001071 fill_rwbs(rwbs, t);
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001072 return trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001073 MAJOR(t->device), MINOR(t->device), act, rwbs);
1074}
1075
Li Zefan66de7792009-04-01 16:19:19 +08001076static int blk_log_dump_pdu(struct trace_seq *s, const struct trace_entry *ent)
1077{
Li Zefan049862572009-05-11 14:33:23 +08001078 const unsigned char *pdu_buf;
Li Zefan66de7792009-04-01 16:19:19 +08001079 int pdu_len;
1080 int i, end, ret;
1081
1082 pdu_buf = pdu_start(ent);
1083 pdu_len = te_blk_io_trace(ent)->pdu_len;
1084
1085 if (!pdu_len)
1086 return 1;
1087
1088 /* find the last zero that needs to be printed */
1089 for (end = pdu_len - 1; end >= 0; end--)
1090 if (pdu_buf[end])
1091 break;
1092 end++;
1093
1094 if (!trace_seq_putc(s, '('))
1095 return 0;
1096
1097 for (i = 0; i < pdu_len; i++) {
1098
1099 ret = trace_seq_printf(s, "%s%02x",
1100 i == 0 ? "" : " ", pdu_buf[i]);
1101 if (!ret)
1102 return ret;
1103
1104 /*
1105 * stop when the rest is just zeroes and indicate so
1106 * with a ".." appended
1107 */
1108 if (i == end && end != pdu_len - 1)
1109 return trace_seq_puts(s, " ..) ");
1110 }
1111
1112 return trace_seq_puts(s, ") ");
1113}
1114
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001115static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1116{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001117 char cmd[TASK_COMM_LEN];
1118
1119 trace_find_cmdline(ent->pid, cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001120
Li Zefan66de7792009-04-01 16:19:19 +08001121 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1122 int ret;
1123
1124 ret = trace_seq_printf(s, "%u ", t_bytes(ent));
1125 if (!ret)
1126 return 0;
1127 ret = blk_log_dump_pdu(s, ent);
1128 if (!ret)
1129 return 0;
1130 return trace_seq_printf(s, "[%s]\n", cmd);
1131 } else {
1132 if (t_sec(ent))
1133 return trace_seq_printf(s, "%llu + %u [%s]\n",
1134 t_sector(ent), t_sec(ent), cmd);
1135 return trace_seq_printf(s, "[%s]\n", cmd);
1136 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001137}
1138
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001139static int blk_log_with_error(struct trace_seq *s,
1140 const struct trace_entry *ent)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001141{
Li Zefan66de7792009-04-01 16:19:19 +08001142 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1143 int ret;
1144
1145 ret = blk_log_dump_pdu(s, ent);
1146 if (ret)
1147 return trace_seq_printf(s, "[%d]\n", t_error(ent));
1148 return 0;
1149 } else {
1150 if (t_sec(ent))
1151 return trace_seq_printf(s, "%llu + %u [%d]\n",
1152 t_sector(ent),
1153 t_sec(ent), t_error(ent));
1154 return trace_seq_printf(s, "%llu [%d]\n",
1155 t_sector(ent), t_error(ent));
1156 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001157}
1158
1159static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1160{
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001161 struct blk_io_trace_remap r = { .device_from = 0, };
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001162
1163 get_pdu_remap(ent, &r);
1164 return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001165 t_sector(ent), t_sec(ent),
1166 MAJOR(r.device_from), MINOR(r.device_from),
1167 (unsigned long long)r.sector_from);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001168}
1169
1170static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1171{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001172 char cmd[TASK_COMM_LEN];
1173
1174 trace_find_cmdline(ent->pid, cmd);
1175
1176 return trace_seq_printf(s, "[%s]\n", cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001177}
1178
1179static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1180{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001181 char cmd[TASK_COMM_LEN];
1182
1183 trace_find_cmdline(ent->pid, cmd);
1184
1185 return trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent));
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001186}
1187
1188static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1189{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001190 char cmd[TASK_COMM_LEN];
1191
1192 trace_find_cmdline(ent->pid, cmd);
1193
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001194 return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
Steven Rostedt4ca530852009-03-16 19:20:15 -04001195 get_pdu_int(ent), cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001196}
1197
Li Zefan18cea452009-03-27 10:21:54 +08001198static int blk_log_msg(struct trace_seq *s, const struct trace_entry *ent)
1199{
1200 int ret;
1201 const struct blk_io_trace *t = te_blk_io_trace(ent);
1202
1203 ret = trace_seq_putmem(s, t + 1, t->pdu_len);
1204 if (ret)
1205 return trace_seq_putc(s, '\n');
1206 return ret;
1207}
1208
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001209/*
1210 * struct tracer operations
1211 */
1212
1213static void blk_tracer_print_header(struct seq_file *m)
1214{
1215 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1216 return;
1217 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1218 "# | | | | | |\n");
1219}
1220
1221static void blk_tracer_start(struct trace_array *tr)
1222{
Li Zefanad5dd542009-03-27 10:20:24 +08001223 blk_tracer_enabled = true;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001224}
1225
1226static int blk_tracer_init(struct trace_array *tr)
1227{
1228 blk_tr = tr;
1229 blk_tracer_start(tr);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001230 return 0;
1231}
1232
1233static void blk_tracer_stop(struct trace_array *tr)
1234{
Li Zefanad5dd542009-03-27 10:20:24 +08001235 blk_tracer_enabled = false;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001236}
1237
1238static void blk_tracer_reset(struct trace_array *tr)
1239{
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001240 blk_tracer_stop(tr);
1241}
1242
Li Zefane4955c92009-03-24 16:04:37 +08001243static const struct {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001244 const char *act[2];
Steven Rostedtef180122009-03-10 14:10:56 -04001245 int (*print)(struct trace_seq *s, const struct trace_entry *ent);
Li Zefane4955c92009-03-24 16:04:37 +08001246} what2act[] = {
Steven Rostedtef180122009-03-10 14:10:56 -04001247 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001248 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1249 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1250 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1251 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1252 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1253 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1254 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1255 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1256 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
1257 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
1258 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1259 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1260 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1261 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1262};
1263
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001264static enum print_line_t print_one_line(struct trace_iterator *iter,
1265 bool classic)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001266{
Arnaldo Carvalho de Melo2c9b2382009-02-02 20:30:12 -02001267 struct trace_seq *s = &iter->seq;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001268 const struct blk_io_trace *t;
1269 u16 what;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001270 int ret;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001271 bool long_act;
1272 blk_log_action_t *log_action;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001273
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001274 t = te_blk_io_trace(iter->ent);
1275 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1276 long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1277 log_action = classic ? &blk_log_action_classic : &blk_log_action;
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001278
Li Zefan18cea452009-03-27 10:21:54 +08001279 if (t->action == BLK_TN_MESSAGE) {
1280 ret = log_action(iter, long_act ? "message" : "m");
1281 if (ret)
1282 ret = blk_log_msg(s, iter->ent);
1283 goto out;
1284 }
1285
Li Zefaneb08f8e2009-03-24 16:05:27 +08001286 if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
Li Zefanb78825d2009-04-01 16:18:53 +08001287 ret = trace_seq_printf(s, "Unknown action %x\n", what);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001288 else {
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001289 ret = log_action(iter, what2act[what].act[long_act]);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001290 if (ret)
Arnaldo Carvalho de Melo2c9b2382009-02-02 20:30:12 -02001291 ret = what2act[what].print(s, iter->ent);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001292 }
Li Zefan18cea452009-03-27 10:21:54 +08001293out:
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001294 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1295}
1296
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001297static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
1298 int flags)
1299{
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001300 return print_one_line(iter, false);
1301}
1302
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001303static int blk_trace_synthesize_old_trace(struct trace_iterator *iter)
1304{
1305 struct trace_seq *s = &iter->seq;
1306 struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1307 const int offset = offsetof(struct blk_io_trace, sector);
1308 struct blk_io_trace old = {
1309 .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
Li Zefan6c051ce2009-03-25 17:18:56 +08001310 .time = iter->ts,
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001311 };
1312
1313 if (!trace_seq_putmem(s, &old, offset))
1314 return 0;
1315 return trace_seq_putmem(s, &t->sector,
1316 sizeof(old) - offset + t->pdu_len);
1317}
1318
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -02001319static enum print_line_t
1320blk_trace_event_print_binary(struct trace_iterator *iter, int flags)
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001321{
1322 return blk_trace_synthesize_old_trace(iter) ?
1323 TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1324}
1325
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001326static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1327{
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001328 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1329 return TRACE_TYPE_UNHANDLED;
1330
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001331 return print_one_line(iter, true);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001332}
1333
Li Zefanf3948f82009-04-15 11:02:56 +08001334static int blk_tracer_set_flag(u32 old_flags, u32 bit, int set)
1335{
1336 /* don't output context-info for blk_classic output */
1337 if (bit == TRACE_BLK_OPT_CLASSIC) {
1338 if (set)
1339 trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
1340 else
1341 trace_flags |= TRACE_ITER_CONTEXT_INFO;
1342 }
1343 return 0;
1344}
1345
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001346static struct tracer blk_tracer __read_mostly = {
1347 .name = "blk",
1348 .init = blk_tracer_init,
1349 .reset = blk_tracer_reset,
1350 .start = blk_tracer_start,
1351 .stop = blk_tracer_stop,
1352 .print_header = blk_tracer_print_header,
1353 .print_line = blk_tracer_print_line,
1354 .flags = &blk_tracer_flags,
Li Zefanf3948f82009-04-15 11:02:56 +08001355 .set_flag = blk_tracer_set_flag,
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001356};
1357
1358static struct trace_event trace_blk_event = {
Steven Rostedtef180122009-03-10 14:10:56 -04001359 .type = TRACE_BLK,
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001360 .trace = blk_trace_event_print,
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001361 .binary = blk_trace_event_print_binary,
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001362};
1363
1364static int __init init_blk_tracer(void)
1365{
1366 if (!register_ftrace_event(&trace_blk_event)) {
1367 pr_warning("Warning: could not register block events\n");
1368 return 1;
1369 }
1370
1371 if (register_tracer(&blk_tracer) != 0) {
1372 pr_warning("Warning: could not register the block tracer\n");
1373 unregister_ftrace_event(&trace_blk_event);
1374 return 1;
1375 }
1376
1377 return 0;
1378}
1379
1380device_initcall(init_blk_tracer);
1381
1382static int blk_trace_remove_queue(struct request_queue *q)
1383{
1384 struct blk_trace *bt;
1385
1386 bt = xchg(&q->blk_trace, NULL);
1387 if (bt == NULL)
1388 return -EINVAL;
1389
Li Zefan17ba97e2009-03-27 10:20:09 +08001390 if (atomic_dec_and_test(&blk_probes_ref))
1391 blk_unregister_tracepoints();
1392
Li Zefanad5dd542009-03-27 10:20:24 +08001393 blk_trace_free(bt);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001394 return 0;
1395}
1396
1397/*
1398 * Setup everything required to start tracing
1399 */
Li Zefan9908c302009-04-14 13:59:34 +08001400static int blk_trace_setup_queue(struct request_queue *q,
1401 struct block_device *bdev)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001402{
1403 struct blk_trace *old_bt, *bt = NULL;
Li Zefan18cea452009-03-27 10:21:54 +08001404 int ret = -ENOMEM;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001405
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001406 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1407 if (!bt)
Li Zefan15152e42009-03-20 09:49:08 +08001408 return -ENOMEM;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001409
Li Zefan18cea452009-03-27 10:21:54 +08001410 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
1411 if (!bt->msg_data)
1412 goto free_bt;
1413
Li Zefan9908c302009-04-14 13:59:34 +08001414 bt->dev = bdev->bd_dev;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001415 bt->act_mask = (u16)-1;
Li Zefan9908c302009-04-14 13:59:34 +08001416
1417 blk_trace_setup_lba(bt, bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001418
1419 old_bt = xchg(&q->blk_trace, bt);
1420 if (old_bt != NULL) {
1421 (void)xchg(&q->blk_trace, old_bt);
Li Zefan18cea452009-03-27 10:21:54 +08001422 ret = -EBUSY;
1423 goto free_bt;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001424 }
Li Zefan15152e42009-03-20 09:49:08 +08001425
Li Zefan17ba97e2009-03-27 10:20:09 +08001426 if (atomic_inc_return(&blk_probes_ref) == 1)
1427 blk_register_tracepoints();
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001428 return 0;
Li Zefan18cea452009-03-27 10:21:54 +08001429
1430free_bt:
1431 blk_trace_free(bt);
1432 return ret;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001433}
1434
1435/*
1436 * sysfs interface to enable and configure tracing
1437 */
1438
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001439static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1440 struct device_attribute *attr,
1441 char *buf);
1442static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1443 struct device_attribute *attr,
1444 const char *buf, size_t count);
1445#define BLK_TRACE_DEVICE_ATTR(_name) \
1446 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1447 sysfs_blk_trace_attr_show, \
1448 sysfs_blk_trace_attr_store)
1449
Li Zefancd649b82009-03-20 11:33:55 +08001450static BLK_TRACE_DEVICE_ATTR(enable);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001451static BLK_TRACE_DEVICE_ATTR(act_mask);
1452static BLK_TRACE_DEVICE_ATTR(pid);
1453static BLK_TRACE_DEVICE_ATTR(start_lba);
1454static BLK_TRACE_DEVICE_ATTR(end_lba);
1455
1456static struct attribute *blk_trace_attrs[] = {
1457 &dev_attr_enable.attr,
1458 &dev_attr_act_mask.attr,
1459 &dev_attr_pid.attr,
1460 &dev_attr_start_lba.attr,
1461 &dev_attr_end_lba.attr,
1462 NULL
1463};
1464
1465struct attribute_group blk_trace_attr_group = {
1466 .name = "trace",
1467 .attrs = blk_trace_attrs,
1468};
1469
Li Zefan09341992009-03-24 17:43:30 +08001470static const struct {
1471 int mask;
1472 const char *str;
1473} mask_maps[] = {
1474 { BLK_TC_READ, "read" },
1475 { BLK_TC_WRITE, "write" },
1476 { BLK_TC_BARRIER, "barrier" },
1477 { BLK_TC_SYNC, "sync" },
1478 { BLK_TC_QUEUE, "queue" },
1479 { BLK_TC_REQUEUE, "requeue" },
1480 { BLK_TC_ISSUE, "issue" },
1481 { BLK_TC_COMPLETE, "complete" },
1482 { BLK_TC_FS, "fs" },
1483 { BLK_TC_PC, "pc" },
1484 { BLK_TC_AHEAD, "ahead" },
1485 { BLK_TC_META, "meta" },
1486 { BLK_TC_DISCARD, "discard" },
1487 { BLK_TC_DRV_DATA, "drv_data" },
1488};
1489
1490static int blk_trace_str2mask(const char *str)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001491{
Li Zefan09341992009-03-24 17:43:30 +08001492 int i;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001493 int mask = 0;
Li Zefan9eb85122009-04-09 11:19:40 +08001494 char *buf, *s, *token;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001495
Li Zefan9eb85122009-04-09 11:19:40 +08001496 buf = kstrdup(str, GFP_KERNEL);
1497 if (buf == NULL)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001498 return -ENOMEM;
Li Zefan9eb85122009-04-09 11:19:40 +08001499 s = strstrip(buf);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001500
1501 while (1) {
Li Zefan09341992009-03-24 17:43:30 +08001502 token = strsep(&s, ",");
1503 if (token == NULL)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001504 break;
1505
Li Zefan09341992009-03-24 17:43:30 +08001506 if (*token == '\0')
1507 continue;
1508
1509 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1510 if (strcasecmp(token, mask_maps[i].str) == 0) {
1511 mask |= mask_maps[i].mask;
1512 break;
1513 }
1514 }
1515 if (i == ARRAY_SIZE(mask_maps)) {
1516 mask = -EINVAL;
1517 break;
1518 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001519 }
Li Zefan9eb85122009-04-09 11:19:40 +08001520 kfree(buf);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001521
1522 return mask;
1523}
1524
Li Zefan09341992009-03-24 17:43:30 +08001525static ssize_t blk_trace_mask2str(char *buf, int mask)
1526{
1527 int i;
1528 char *p = buf;
1529
1530 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1531 if (mask & mask_maps[i].mask) {
1532 p += sprintf(p, "%s%s",
1533 (p == buf) ? "" : ",", mask_maps[i].str);
1534 }
1535 }
1536 *p++ = '\n';
1537
1538 return p - buf;
1539}
1540
Li Zefanb1251302009-03-20 10:34:00 +08001541static struct request_queue *blk_trace_get_queue(struct block_device *bdev)
1542{
1543 if (bdev->bd_disk == NULL)
1544 return NULL;
1545
1546 return bdev_get_queue(bdev);
1547}
1548
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001549static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1550 struct device_attribute *attr,
1551 char *buf)
1552{
1553 struct hd_struct *p = dev_to_part(dev);
1554 struct request_queue *q;
1555 struct block_device *bdev;
1556 ssize_t ret = -ENXIO;
1557
1558 lock_kernel();
1559 bdev = bdget(part_devt(p));
1560 if (bdev == NULL)
1561 goto out_unlock_kernel;
1562
Li Zefanb1251302009-03-20 10:34:00 +08001563 q = blk_trace_get_queue(bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001564 if (q == NULL)
1565 goto out_bdput;
Li Zefanb1251302009-03-20 10:34:00 +08001566
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001567 mutex_lock(&bdev->bd_mutex);
Li Zefancd649b82009-03-20 11:33:55 +08001568
1569 if (attr == &dev_attr_enable) {
1570 ret = sprintf(buf, "%u\n", !!q->blk_trace);
1571 goto out_unlock_bdev;
1572 }
1573
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001574 if (q->blk_trace == NULL)
1575 ret = sprintf(buf, "disabled\n");
1576 else if (attr == &dev_attr_act_mask)
Li Zefan09341992009-03-24 17:43:30 +08001577 ret = blk_trace_mask2str(buf, q->blk_trace->act_mask);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001578 else if (attr == &dev_attr_pid)
1579 ret = sprintf(buf, "%u\n", q->blk_trace->pid);
1580 else if (attr == &dev_attr_start_lba)
1581 ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
1582 else if (attr == &dev_attr_end_lba)
1583 ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
Li Zefancd649b82009-03-20 11:33:55 +08001584
1585out_unlock_bdev:
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001586 mutex_unlock(&bdev->bd_mutex);
1587out_bdput:
1588 bdput(bdev);
1589out_unlock_kernel:
1590 unlock_kernel();
1591 return ret;
1592}
1593
1594static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1595 struct device_attribute *attr,
1596 const char *buf, size_t count)
1597{
1598 struct block_device *bdev;
1599 struct request_queue *q;
1600 struct hd_struct *p;
1601 u64 value;
Li Zefan09341992009-03-24 17:43:30 +08001602 ssize_t ret = -EINVAL;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001603
1604 if (count == 0)
1605 goto out;
1606
1607 if (attr == &dev_attr_act_mask) {
1608 if (sscanf(buf, "%llx", &value) != 1) {
1609 /* Assume it is a list of trace category names */
Li Zefan09341992009-03-24 17:43:30 +08001610 ret = blk_trace_str2mask(buf);
1611 if (ret < 0)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001612 goto out;
Li Zefan09341992009-03-24 17:43:30 +08001613 value = ret;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001614 }
1615 } else if (sscanf(buf, "%llu", &value) != 1)
1616 goto out;
1617
Li Zefan09341992009-03-24 17:43:30 +08001618 ret = -ENXIO;
1619
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001620 lock_kernel();
1621 p = dev_to_part(dev);
1622 bdev = bdget(part_devt(p));
1623 if (bdev == NULL)
1624 goto out_unlock_kernel;
1625
Li Zefanb1251302009-03-20 10:34:00 +08001626 q = blk_trace_get_queue(bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001627 if (q == NULL)
1628 goto out_bdput;
1629
1630 mutex_lock(&bdev->bd_mutex);
Li Zefancd649b82009-03-20 11:33:55 +08001631
1632 if (attr == &dev_attr_enable) {
1633 if (value)
Li Zefan9908c302009-04-14 13:59:34 +08001634 ret = blk_trace_setup_queue(q, bdev);
Li Zefancd649b82009-03-20 11:33:55 +08001635 else
1636 ret = blk_trace_remove_queue(q);
1637 goto out_unlock_bdev;
1638 }
1639
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001640 ret = 0;
1641 if (q->blk_trace == NULL)
Li Zefan9908c302009-04-14 13:59:34 +08001642 ret = blk_trace_setup_queue(q, bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001643
1644 if (ret == 0) {
1645 if (attr == &dev_attr_act_mask)
1646 q->blk_trace->act_mask = value;
1647 else if (attr == &dev_attr_pid)
1648 q->blk_trace->pid = value;
1649 else if (attr == &dev_attr_start_lba)
1650 q->blk_trace->start_lba = value;
1651 else if (attr == &dev_attr_end_lba)
1652 q->blk_trace->end_lba = value;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001653 }
Li Zefancd649b82009-03-20 11:33:55 +08001654
1655out_unlock_bdev:
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001656 mutex_unlock(&bdev->bd_mutex);
1657out_bdput:
1658 bdput(bdev);
1659out_unlock_kernel:
1660 unlock_kernel();
1661out:
Li Zefancd649b82009-03-20 11:33:55 +08001662 return ret ? ret : count;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001663}
Li Zefancd649b82009-03-20 11:33:55 +08001664
Li Zefan1d54ad62009-04-14 14:00:05 +08001665int blk_trace_init_sysfs(struct device *dev)
1666{
1667 return sysfs_create_group(&dev->kobj, &blk_trace_attr_group);
1668}
1669
Li Zefan55782132009-06-09 13:43:05 +08001670#endif /* CONFIG_BLK_DEV_IO_TRACE */
1671
1672#ifdef CONFIG_EVENT_TRACING
1673
1674void blk_dump_cmd(char *buf, struct request *rq)
1675{
1676 int i, end;
1677 int len = rq->cmd_len;
1678 unsigned char *cmd = rq->cmd;
1679
1680 if (!blk_pc_request(rq)) {
1681 buf[0] = '\0';
1682 return;
1683 }
1684
1685 for (end = len - 1; end >= 0; end--)
1686 if (cmd[end])
1687 break;
1688 end++;
1689
1690 for (i = 0; i < len; i++) {
1691 buf += sprintf(buf, "%s%02x", i == 0 ? "" : " ", cmd[i]);
1692 if (i == end && end != len - 1) {
1693 sprintf(buf, " ..");
1694 break;
1695 }
1696 }
1697}
1698
1699void blk_fill_rwbs(char *rwbs, u32 rw, int bytes)
1700{
1701 int i = 0;
1702
1703 if (rw & WRITE)
1704 rwbs[i++] = 'W';
1705 else if (rw & 1 << BIO_RW_DISCARD)
1706 rwbs[i++] = 'D';
1707 else if (bytes)
1708 rwbs[i++] = 'R';
1709 else
1710 rwbs[i++] = 'N';
1711
1712 if (rw & 1 << BIO_RW_AHEAD)
1713 rwbs[i++] = 'A';
1714 if (rw & 1 << BIO_RW_BARRIER)
1715 rwbs[i++] = 'B';
1716 if (rw & 1 << BIO_RW_SYNCIO)
1717 rwbs[i++] = 'S';
1718 if (rw & 1 << BIO_RW_META)
1719 rwbs[i++] = 'M';
1720
1721 rwbs[i] = '\0';
1722}
1723
1724void blk_fill_rwbs_rq(char *rwbs, struct request *rq)
1725{
1726 int rw = rq->cmd_flags & 0x03;
1727 int bytes;
1728
1729 if (blk_discard_rq(rq))
1730 rw |= (1 << BIO_RW_DISCARD);
1731
Linus Torvaldsc9059592009-06-11 10:52:27 -07001732 bytes = blk_rq_bytes(rq);
Li Zefan55782132009-06-09 13:43:05 +08001733
1734 blk_fill_rwbs(rwbs, rw, bytes);
1735}
1736
1737#endif /* CONFIG_EVENT_TRACING */
1738