blob: 39af8af6fc302c981226ab8c8f277026b319ca4a [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>
Olaf Kirchbe1c6342006-12-01 10:39:12 +010025#include <linux/time.h>
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -020026#include <linux/uaccess.h>
Li Zefan55782132009-06-09 13:43:05 +080027
28#include <trace/events/block.h>
29
Frederic Weisbecker2db270a2009-02-07 20:46:45 +010030#include "trace_output.h"
Jens Axboe2056a782006-03-23 20:00:26 +010031
Li Zefan55782132009-06-09 13:43:05 +080032#ifdef CONFIG_BLK_DEV_IO_TRACE
33
Jens Axboe2056a782006-03-23 20:00:26 +010034static unsigned int blktrace_seq __read_mostly = 1;
35
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020036static struct trace_array *blk_tr;
Li Zefan5006ea72009-03-20 09:48:03 +080037static bool blk_tracer_enabled __read_mostly;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020038
39/* Select an alternative, minimalistic output than the original one */
Steven Rostedtef180122009-03-10 14:10:56 -040040#define TRACE_BLK_OPT_CLASSIC 0x1
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020041
42static struct tracer_opt blk_tracer_opts[] = {
43 /* Default disable the minimalistic output */
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -020044 { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020045 { }
46};
47
48static struct tracer_flags blk_tracer_flags = {
49 .val = 0,
50 .opts = blk_tracer_opts,
51};
52
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010053/* Global reference count of probes */
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010054static atomic_t blk_probes_ref = ATOMIC_INIT(0);
55
Li Zefan3c289ba2009-03-20 09:48:26 +080056static void blk_register_tracepoints(void);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010057static void blk_unregister_tracepoints(void);
58
Jens Axboe2056a782006-03-23 20:00:26 +010059/*
Olaf Kirchbe1c6342006-12-01 10:39:12 +010060 * Send out a notify message.
61 */
Jens Axboea8630552006-12-04 09:30:58 +010062static void trace_note(struct blk_trace *bt, pid_t pid, int action,
63 const void *data, size_t len)
Olaf Kirchbe1c6342006-12-01 10:39:12 +010064{
65 struct blk_io_trace *t;
Li Zefan18cea452009-03-27 10:21:54 +080066 struct ring_buffer_event *event = NULL;
67 int pc = 0;
68 int cpu = smp_processor_id();
69 bool blk_tracer = blk_tracer_enabled;
70
71 if (blk_tracer) {
72 pc = preempt_count();
73 event = trace_buffer_lock_reserve(blk_tr, TRACE_BLK,
74 sizeof(*t) + len,
75 0, pc);
76 if (!event)
77 return;
78 t = ring_buffer_event_data(event);
79 goto record_it;
80 }
Olaf Kirchbe1c6342006-12-01 10:39:12 +010081
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020082 if (!bt->rchan)
83 return;
84
Olaf Kirchbe1c6342006-12-01 10:39:12 +010085 t = relay_reserve(bt->rchan, sizeof(*t) + len);
Jens Axboed3d9d2a2006-12-04 09:27:41 +010086 if (t) {
Jens Axboed3d9d2a2006-12-04 09:27:41 +010087 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
Ingo Molnar2997c8c2008-01-11 13:35:54 +010088 t->time = ktime_to_ns(ktime_get());
Li Zefan18cea452009-03-27 10:21:54 +080089record_it:
Jens Axboed3d9d2a2006-12-04 09:27:41 +010090 t->device = bt->dev;
91 t->action = action;
92 t->pid = pid;
93 t->cpu = cpu;
94 t->pdu_len = len;
95 memcpy((void *) t + sizeof(*t), data, len);
Li Zefan18cea452009-03-27 10:21:54 +080096
97 if (blk_tracer)
98 trace_buffer_unlock_commit(blk_tr, event, 0, pc);
Jens Axboed3d9d2a2006-12-04 09:27:41 +010099 }
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100100}
101
102/*
Jens Axboe2056a782006-03-23 20:00:26 +0100103 * Send out a notify for this process, if we haven't done so since a trace
104 * started
105 */
106static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk)
107{
Jens Axboea8630552006-12-04 09:30:58 +0100108 tsk->btrace_seq = blktrace_seq;
109 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm));
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100110}
Jens Axboe2056a782006-03-23 20:00:26 +0100111
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100112static void trace_note_time(struct blk_trace *bt)
113{
114 struct timespec now;
115 unsigned long flags;
116 u32 words[2];
117
118 getnstimeofday(&now);
119 words[0] = now.tv_sec;
120 words[1] = now.tv_nsec;
121
122 local_irq_save(flags);
123 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
124 local_irq_restore(flags);
Jens Axboe2056a782006-03-23 20:00:26 +0100125}
126
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200127void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
128{
129 int n;
130 va_list args;
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200131 unsigned long flags;
Jens Axboe64565912008-05-28 14:45:33 +0200132 char *buf;
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200133
Li Zefan18cea452009-03-27 10:21:54 +0800134 if (unlikely(bt->trace_state != Blktrace_running &&
135 !blk_tracer_enabled))
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200136 return;
137
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200138 local_irq_save(flags);
Jens Axboe64565912008-05-28 14:45:33 +0200139 buf = per_cpu_ptr(bt->msg_data, smp_processor_id());
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200140 va_start(args, fmt);
Jens Axboe64565912008-05-28 14:45:33 +0200141 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200142 va_end(args);
143
Jens Axboe64565912008-05-28 14:45:33 +0200144 trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200145 local_irq_restore(flags);
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200146}
147EXPORT_SYMBOL_GPL(__trace_note_message);
148
Jens Axboe2056a782006-03-23 20:00:26 +0100149static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
150 pid_t pid)
151{
152 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
153 return 1;
Shawn Dud0deef52009-04-14 13:58:56 +0800154 if (sector && (sector < bt->start_lba || sector > bt->end_lba))
Jens Axboe2056a782006-03-23 20:00:26 +0100155 return 1;
156 if (bt->pid && pid != bt->pid)
157 return 1;
158
159 return 0;
160}
161
162/*
163 * Data direction bit lookup
164 */
Li Zefane4955c92009-03-24 16:04:37 +0800165static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ),
166 BLK_TC_ACT(BLK_TC_WRITE) };
Jens Axboe2056a782006-03-23 20:00:26 +0100167
David Woodhouse35ba8f72008-08-10 12:33:00 +0100168/* The ilog2() calls fall out because they're constant */
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200169#define MASK_TC_BIT(rw, __name) ((rw & (1 << BIO_RW_ ## __name)) << \
170 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name))
Jens Axboe2056a782006-03-23 20:00:26 +0100171
172/*
173 * The worker for the various blk_add_trace*() types. Fills out a
174 * blk_io_trace structure and places it in a per-cpu subbuffer.
175 */
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100176static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
Jens Axboe2056a782006-03-23 20:00:26 +0100177 int rw, u32 what, int error, int pdu_len, void *pdu_data)
178{
179 struct task_struct *tsk = current;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200180 struct ring_buffer_event *event = NULL;
Jens Axboe2056a782006-03-23 20:00:26 +0100181 struct blk_io_trace *t;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200182 unsigned long flags = 0;
Jens Axboe2056a782006-03-23 20:00:26 +0100183 unsigned long *sequence;
184 pid_t pid;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200185 int cpu, pc = 0;
Li Zefan18cea452009-03-27 10:21:54 +0800186 bool blk_tracer = blk_tracer_enabled;
Jens Axboe2056a782006-03-23 20:00:26 +0100187
Li Zefan18cea452009-03-27 10:21:54 +0800188 if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer))
Jens Axboe2056a782006-03-23 20:00:26 +0100189 return;
190
191 what |= ddir_act[rw & WRITE];
David Woodhouse35ba8f72008-08-10 12:33:00 +0100192 what |= MASK_TC_BIT(rw, BARRIER);
Ingo Molnar72c26c92009-02-19 09:00:35 +0100193 what |= MASK_TC_BIT(rw, SYNCIO);
David Woodhouse35ba8f72008-08-10 12:33:00 +0100194 what |= MASK_TC_BIT(rw, AHEAD);
195 what |= MASK_TC_BIT(rw, META);
196 what |= MASK_TC_BIT(rw, DISCARD);
Jens Axboe2056a782006-03-23 20:00:26 +0100197
198 pid = tsk->pid;
Shawn Dud0deef52009-04-14 13:58:56 +0800199 if (act_log_check(bt, what, sector, pid))
Jens Axboe2056a782006-03-23 20:00:26 +0100200 return;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200201 cpu = raw_smp_processor_id();
202
Li Zefan18cea452009-03-27 10:21:54 +0800203 if (blk_tracer) {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200204 tracing_record_cmdline(current);
205
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200206 pc = preempt_count();
207 event = trace_buffer_lock_reserve(blk_tr, TRACE_BLK,
208 sizeof(*t) + pdu_len,
209 0, pc);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200210 if (!event)
211 return;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200212 t = ring_buffer_event_data(event);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200213 goto record_it;
214 }
Jens Axboe2056a782006-03-23 20:00:26 +0100215
216 /*
217 * A word about the locking here - we disable interrupts to reserve
218 * some space in the relay per-cpu buffer, to prevent an irq
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200219 * from coming in and stepping on our toes.
Jens Axboe2056a782006-03-23 20:00:26 +0100220 */
221 local_irq_save(flags);
222
223 if (unlikely(tsk->btrace_seq != blktrace_seq))
224 trace_note_tsk(bt, tsk);
225
226 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
227 if (t) {
Jens Axboe2056a782006-03-23 20:00:26 +0100228 sequence = per_cpu_ptr(bt->sequence, cpu);
229
230 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
231 t->sequence = ++(*sequence);
Ingo Molnar2997c8c2008-01-11 13:35:54 +0100232 t->time = ktime_to_ns(ktime_get());
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200233record_it:
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -0200234 /*
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200235 * These two are not needed in ftrace as they are in the
236 * generic trace_entry, filled by tracing_generic_entry_update,
237 * but for the trace_event->bin() synthesizer benefit we do it
238 * here too.
239 */
240 t->cpu = cpu;
241 t->pid = pid;
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -0200242
Jens Axboe2056a782006-03-23 20:00:26 +0100243 t->sector = sector;
244 t->bytes = bytes;
245 t->action = what;
Jens Axboe2056a782006-03-23 20:00:26 +0100246 t->device = bt->dev;
Jens Axboe2056a782006-03-23 20:00:26 +0100247 t->error = error;
248 t->pdu_len = pdu_len;
249
250 if (pdu_len)
251 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200252
Li Zefan18cea452009-03-27 10:21:54 +0800253 if (blk_tracer) {
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200254 trace_buffer_unlock_commit(blk_tr, event, 0, pc);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200255 return;
256 }
Jens Axboe2056a782006-03-23 20:00:26 +0100257 }
258
259 local_irq_restore(flags);
260}
261
Jens Axboe2056a782006-03-23 20:00:26 +0100262static struct dentry *blk_tree_root;
Jens Axboe11a57152008-01-11 13:37:01 +0100263static DEFINE_MUTEX(blk_tree_mutex);
Jens Axboe2056a782006-03-23 20:00:26 +0100264
Li Zefanad5dd542009-03-27 10:20:24 +0800265static void blk_trace_free(struct blk_trace *bt)
Jens Axboe2056a782006-03-23 20:00:26 +0100266{
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200267 debugfs_remove(bt->msg_file);
Jens Axboe2056a782006-03-23 20:00:26 +0100268 debugfs_remove(bt->dropped_file);
Stefan Rasplfd51d252009-05-19 09:59:08 +0200269 debugfs_remove(bt->dir);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100270 relay_close(bt->rchan);
Jens Axboe2056a782006-03-23 20:00:26 +0100271 free_percpu(bt->sequence);
Jens Axboe64565912008-05-28 14:45:33 +0200272 free_percpu(bt->msg_data);
Jens Axboe2056a782006-03-23 20:00:26 +0100273 kfree(bt);
Li Zefanad5dd542009-03-27 10:20:24 +0800274}
275
276static void blk_trace_cleanup(struct blk_trace *bt)
277{
278 blk_trace_free(bt);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100279 if (atomic_dec_and_test(&blk_probes_ref))
280 blk_unregister_tracepoints();
Jens Axboe2056a782006-03-23 20:00:26 +0100281}
282
Christof Schmitt6da127a2008-01-11 10:09:43 +0100283int blk_trace_remove(struct request_queue *q)
Jens Axboe2056a782006-03-23 20:00:26 +0100284{
285 struct blk_trace *bt;
286
287 bt = xchg(&q->blk_trace, NULL);
288 if (!bt)
289 return -EINVAL;
290
Li Zefan55547202009-03-25 17:21:26 +0800291 if (bt->trace_state != Blktrace_running)
Jens Axboe2056a782006-03-23 20:00:26 +0100292 blk_trace_cleanup(bt);
293
294 return 0;
295}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100296EXPORT_SYMBOL_GPL(blk_trace_remove);
Jens Axboe2056a782006-03-23 20:00:26 +0100297
298static int blk_dropped_open(struct inode *inode, struct file *filp)
299{
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700300 filp->private_data = inode->i_private;
Jens Axboe2056a782006-03-23 20:00:26 +0100301
302 return 0;
303}
304
305static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
306 size_t count, loff_t *ppos)
307{
308 struct blk_trace *bt = filp->private_data;
309 char buf[16];
310
311 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
312
313 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
314}
315
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800316static const struct file_operations blk_dropped_fops = {
Jens Axboe2056a782006-03-23 20:00:26 +0100317 .owner = THIS_MODULE,
318 .open = blk_dropped_open,
319 .read = blk_dropped_read,
320};
321
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200322static int blk_msg_open(struct inode *inode, struct file *filp)
323{
324 filp->private_data = inode->i_private;
325
326 return 0;
327}
328
329static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
330 size_t count, loff_t *ppos)
331{
332 char *msg;
333 struct blk_trace *bt;
334
Li Zefan7635b032009-04-03 15:31:34 +0800335 if (count >= BLK_TN_MAX_MSG)
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200336 return -EINVAL;
337
Carl Henrik Lundea4b3ada2009-04-03 14:27:15 +0200338 msg = kmalloc(count + 1, GFP_KERNEL);
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200339 if (msg == NULL)
340 return -ENOMEM;
341
342 if (copy_from_user(msg, buffer, count)) {
343 kfree(msg);
344 return -EFAULT;
345 }
346
Carl Henrik Lundea4b3ada2009-04-03 14:27:15 +0200347 msg[count] = '\0';
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200348 bt = filp->private_data;
349 __trace_note_message(bt, "%s", msg);
350 kfree(msg);
351
352 return count;
353}
354
355static const struct file_operations blk_msg_fops = {
356 .owner = THIS_MODULE,
357 .open = blk_msg_open,
358 .write = blk_msg_write,
359};
360
Jens Axboe2056a782006-03-23 20:00:26 +0100361/*
362 * Keep track of how many times we encountered a full subbuffer, to aid
363 * the user space app in telling how many lost events there were.
364 */
365static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
366 void *prev_subbuf, size_t prev_padding)
367{
368 struct blk_trace *bt;
369
370 if (!relay_buf_full(buf))
371 return 1;
372
373 bt = buf->chan->private_data;
374 atomic_inc(&bt->dropped);
375 return 0;
376}
377
378static int blk_remove_buf_file_callback(struct dentry *dentry)
379{
Jens Axboef48fc4d2009-01-05 10:17:25 +0100380 struct dentry *parent = dentry->d_parent;
Jens Axboe2056a782006-03-23 20:00:26 +0100381 debugfs_remove(dentry);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100382
383 /*
384 * this will fail for all but the last file, but that is ok. what we
385 * care about is the top level buts->name directory going away, when
386 * the last trace file is gone. Then we don't have to rmdir() that
387 * manually on trace stop, so it nicely solves the issue with
388 * force killing of running traces.
389 */
390
391 debugfs_remove(parent);
Jens Axboe2056a782006-03-23 20:00:26 +0100392 return 0;
393}
394
395static struct dentry *blk_create_buf_file_callback(const char *filename,
396 struct dentry *parent,
397 int mode,
398 struct rchan_buf *buf,
399 int *is_global)
400{
401 return debugfs_create_file(filename, mode, parent, buf,
402 &relay_file_operations);
403}
404
405static struct rchan_callbacks blk_relay_callbacks = {
406 .subbuf_start = blk_subbuf_start_callback,
407 .create_buf_file = blk_create_buf_file_callback,
408 .remove_buf_file = blk_remove_buf_file_callback,
409};
410
Li Zefan9908c302009-04-14 13:59:34 +0800411static void blk_trace_setup_lba(struct blk_trace *bt,
412 struct block_device *bdev)
413{
414 struct hd_struct *part = NULL;
415
416 if (bdev)
417 part = bdev->bd_part;
418
419 if (part) {
420 bt->start_lba = part->start_sect;
421 bt->end_lba = part->start_sect + part->nr_sects;
422 } else {
423 bt->start_lba = 0;
424 bt->end_lba = -1ULL;
425 }
426}
427
Jens Axboe2056a782006-03-23 20:00:26 +0100428/*
429 * Setup everything required to start tracing
430 */
Christof Schmitt6da127a2008-01-11 10:09:43 +0100431int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
Shawn Dud0deef52009-04-14 13:58:56 +0800432 struct block_device *bdev,
433 struct blk_user_trace_setup *buts)
Jens Axboe2056a782006-03-23 20:00:26 +0100434{
Jens Axboe2056a782006-03-23 20:00:26 +0100435 struct blk_trace *old_bt, *bt = NULL;
436 struct dentry *dir = NULL;
Jens Axboe2056a782006-03-23 20:00:26 +0100437 int ret, i;
438
Arnd Bergmann171044d42007-10-09 13:23:53 +0200439 if (!buts->buf_size || !buts->buf_nr)
Jens Axboe2056a782006-03-23 20:00:26 +0100440 return -EINVAL;
441
Jens Axboe0497b342008-10-01 16:16:25 +0200442 strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
443 buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
Jens Axboe2056a782006-03-23 20:00:26 +0100444
445 /*
446 * some device names have larger paths - convert the slashes
447 * to underscores for this to work as expected
448 */
Arnd Bergmann171044d42007-10-09 13:23:53 +0200449 for (i = 0; i < strlen(buts->name); i++)
450 if (buts->name[i] == '/')
451 buts->name[i] = '_';
Jens Axboe2056a782006-03-23 20:00:26 +0100452
Jens Axboe2056a782006-03-23 20:00:26 +0100453 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
454 if (!bt)
Li Zefanad5dd542009-03-27 10:20:24 +0800455 return -ENOMEM;
Jens Axboe2056a782006-03-23 20:00:26 +0100456
Li Zefanad5dd542009-03-27 10:20:24 +0800457 ret = -ENOMEM;
Jens Axboe2056a782006-03-23 20:00:26 +0100458 bt->sequence = alloc_percpu(unsigned long);
459 if (!bt->sequence)
460 goto err;
461
Ingo Molnarf0ef0392009-03-06 16:44:14 +0100462 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
Jens Axboe64565912008-05-28 14:45:33 +0200463 if (!bt->msg_data)
464 goto err;
465
Jens Axboe2056a782006-03-23 20:00:26 +0100466 ret = -ENOENT;
Jens Axboef48fc4d2009-01-05 10:17:25 +0100467
Li Zefanb5230b52009-03-25 17:19:33 +0800468 mutex_lock(&blk_tree_mutex);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100469 if (!blk_tree_root) {
470 blk_tree_root = debugfs_create_dir("block", NULL);
Li Zefanb5230b52009-03-25 17:19:33 +0800471 if (!blk_tree_root) {
472 mutex_unlock(&blk_tree_mutex);
Li Zefan1a176622009-03-20 09:47:30 +0800473 goto err;
Li Zefanb5230b52009-03-25 17:19:33 +0800474 }
Jens Axboef48fc4d2009-01-05 10:17:25 +0100475 }
Li Zefanb5230b52009-03-25 17:19:33 +0800476 mutex_unlock(&blk_tree_mutex);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100477
478 dir = debugfs_create_dir(buts->name, blk_tree_root);
479
Jens Axboe2056a782006-03-23 20:00:26 +0100480 if (!dir)
481 goto err;
482
483 bt->dir = dir;
Christof Schmitt6da127a2008-01-11 10:09:43 +0100484 bt->dev = dev;
Jens Axboe2056a782006-03-23 20:00:26 +0100485 atomic_set(&bt->dropped, 0);
486
487 ret = -EIO;
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200488 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt,
489 &blk_dropped_fops);
Jens Axboe2056a782006-03-23 20:00:26 +0100490 if (!bt->dropped_file)
491 goto err;
492
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200493 bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
494 if (!bt->msg_file)
495 goto err;
496
Arnd Bergmann171044d42007-10-09 13:23:53 +0200497 bt->rchan = relay_open("trace", dir, buts->buf_size,
498 buts->buf_nr, &blk_relay_callbacks, bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100499 if (!bt->rchan)
500 goto err;
Jens Axboe2056a782006-03-23 20:00:26 +0100501
Arnd Bergmann171044d42007-10-09 13:23:53 +0200502 bt->act_mask = buts->act_mask;
Jens Axboe2056a782006-03-23 20:00:26 +0100503 if (!bt->act_mask)
504 bt->act_mask = (u16) -1;
505
Li Zefan9908c302009-04-14 13:59:34 +0800506 blk_trace_setup_lba(bt, bdev);
Jens Axboe2056a782006-03-23 20:00:26 +0100507
Shawn Dud0deef52009-04-14 13:58:56 +0800508 /* overwrite with user settings */
509 if (buts->start_lba)
510 bt->start_lba = buts->start_lba;
511 if (buts->end_lba)
512 bt->end_lba = buts->end_lba;
513
Arnd Bergmann171044d42007-10-09 13:23:53 +0200514 bt->pid = buts->pid;
Jens Axboe2056a782006-03-23 20:00:26 +0100515 bt->trace_state = Blktrace_setup;
516
517 ret = -EBUSY;
518 old_bt = xchg(&q->blk_trace, bt);
519 if (old_bt) {
520 (void) xchg(&q->blk_trace, old_bt);
521 goto err;
522 }
523
Li Zefan17ba97e2009-03-27 10:20:09 +0800524 if (atomic_inc_return(&blk_probes_ref) == 1)
Li Zefancbe28292009-03-20 09:48:47 +0800525 blk_register_tracepoints();
526
Jens Axboe2056a782006-03-23 20:00:26 +0100527 return 0;
528err:
Li Zefanad5dd542009-03-27 10:20:24 +0800529 blk_trace_free(bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100530 return ret;
531}
532
Christof Schmitt6da127a2008-01-11 10:09:43 +0100533int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
Shawn Dud0deef52009-04-14 13:58:56 +0800534 struct block_device *bdev,
Christof Schmitt6da127a2008-01-11 10:09:43 +0100535 char __user *arg)
Arnd Bergmann171044d42007-10-09 13:23:53 +0200536{
537 struct blk_user_trace_setup buts;
538 int ret;
539
540 ret = copy_from_user(&buts, arg, sizeof(buts));
541 if (ret)
542 return -EFAULT;
543
Shawn Dud0deef52009-04-14 13:58:56 +0800544 ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200545 if (ret)
546 return ret;
547
548 if (copy_to_user(arg, &buts, sizeof(buts)))
549 return -EFAULT;
550
551 return 0;
552}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100553EXPORT_SYMBOL_GPL(blk_trace_setup);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200554
Christof Schmitt6da127a2008-01-11 10:09:43 +0100555int blk_trace_startstop(struct request_queue *q, int start)
Jens Axboe2056a782006-03-23 20:00:26 +0100556{
Jens Axboe2056a782006-03-23 20:00:26 +0100557 int ret;
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200558 struct blk_trace *bt = q->blk_trace;
Jens Axboe2056a782006-03-23 20:00:26 +0100559
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200560 if (bt == NULL)
Jens Axboe2056a782006-03-23 20:00:26 +0100561 return -EINVAL;
562
563 /*
564 * For starting a trace, we can transition from a setup or stopped
565 * trace. For stopping a trace, the state must be running
566 */
567 ret = -EINVAL;
568 if (start) {
569 if (bt->trace_state == Blktrace_setup ||
570 bt->trace_state == Blktrace_stopped) {
571 blktrace_seq++;
572 smp_mb();
573 bt->trace_state = Blktrace_running;
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100574
575 trace_note_time(bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100576 ret = 0;
577 }
578 } else {
579 if (bt->trace_state == Blktrace_running) {
580 bt->trace_state = Blktrace_stopped;
581 relay_flush(bt->rchan);
582 ret = 0;
583 }
584 }
585
586 return ret;
587}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100588EXPORT_SYMBOL_GPL(blk_trace_startstop);
Jens Axboe2056a782006-03-23 20:00:26 +0100589
590/**
591 * blk_trace_ioctl: - handle the ioctls associated with tracing
592 * @bdev: the block device
Steven Rostedtef180122009-03-10 14:10:56 -0400593 * @cmd: the ioctl cmd
Jens Axboe2056a782006-03-23 20:00:26 +0100594 * @arg: the argument data, if any
595 *
596 **/
597int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
598{
Jens Axboe165125e2007-07-24 09:28:11 +0200599 struct request_queue *q;
Jens Axboe2056a782006-03-23 20:00:26 +0100600 int ret, start = 0;
Christof Schmitt6da127a2008-01-11 10:09:43 +0100601 char b[BDEVNAME_SIZE];
Jens Axboe2056a782006-03-23 20:00:26 +0100602
603 q = bdev_get_queue(bdev);
604 if (!q)
605 return -ENXIO;
606
607 mutex_lock(&bdev->bd_mutex);
608
609 switch (cmd) {
610 case BLKTRACESETUP:
Jean Delvaref36f21e2008-05-12 14:02:33 -0700611 bdevname(bdev, b);
Shawn Dud0deef52009-04-14 13:58:56 +0800612 ret = blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
Jens Axboe2056a782006-03-23 20:00:26 +0100613 break;
614 case BLKTRACESTART:
615 start = 1;
616 case BLKTRACESTOP:
617 ret = blk_trace_startstop(q, start);
618 break;
619 case BLKTRACETEARDOWN:
620 ret = blk_trace_remove(q);
621 break;
622 default:
623 ret = -ENOTTY;
624 break;
625 }
626
627 mutex_unlock(&bdev->bd_mutex);
628 return ret;
629}
630
631/**
632 * blk_trace_shutdown: - stop and cleanup trace structures
633 * @q: the request queue associated with the device
634 *
635 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200636void blk_trace_shutdown(struct request_queue *q)
Jens Axboe2056a782006-03-23 20:00:26 +0100637{
Alexey Dobriyan6c5c9342006-09-29 01:59:40 -0700638 if (q->blk_trace) {
639 blk_trace_startstop(q, 0);
640 blk_trace_remove(q);
641 }
Jens Axboe2056a782006-03-23 20:00:26 +0100642}
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100643
644/*
645 * blktrace probes
646 */
647
648/**
649 * blk_add_trace_rq - Add a trace for a request oriented action
650 * @q: queue the io is for
651 * @rq: the source request
652 * @what: the action
653 *
654 * Description:
655 * Records an action against a request. Will log the bio offset + size.
656 *
657 **/
658static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
659 u32 what)
660{
661 struct blk_trace *bt = q->blk_trace;
662 int rw = rq->cmd_flags & 0x03;
663
664 if (likely(!bt))
665 return;
666
667 if (blk_discard_rq(rq))
668 rw |= (1 << BIO_RW_DISCARD);
669
670 if (blk_pc_request(rq)) {
671 what |= BLK_TC_ACT(BLK_TC_PC);
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900672 __blk_add_trace(bt, 0, blk_rq_bytes(rq), rw,
673 what, rq->errors, rq->cmd_len, rq->cmd);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100674 } else {
675 what |= BLK_TC_ACT(BLK_TC_FS);
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900676 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), rw,
677 what, rq->errors, 0, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100678 }
679}
680
681static void blk_add_trace_rq_abort(struct request_queue *q, struct request *rq)
682{
683 blk_add_trace_rq(q, rq, BLK_TA_ABORT);
684}
685
686static void blk_add_trace_rq_insert(struct request_queue *q, struct request *rq)
687{
688 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
689}
690
691static void blk_add_trace_rq_issue(struct request_queue *q, struct request *rq)
692{
693 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
694}
695
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200696static void blk_add_trace_rq_requeue(struct request_queue *q,
697 struct request *rq)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100698{
699 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
700}
701
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200702static void blk_add_trace_rq_complete(struct request_queue *q,
703 struct request *rq)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100704{
705 blk_add_trace_rq(q, rq, BLK_TA_COMPLETE);
706}
707
708/**
709 * blk_add_trace_bio - Add a trace for a bio oriented action
710 * @q: queue the io is for
711 * @bio: the source bio
712 * @what: the action
713 *
714 * Description:
715 * Records an action against a bio. Will log the bio offset + size.
716 *
717 **/
718static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
719 u32 what)
720{
721 struct blk_trace *bt = q->blk_trace;
722
723 if (likely(!bt))
724 return;
725
726 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what,
727 !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
728}
729
730static void blk_add_trace_bio_bounce(struct request_queue *q, struct bio *bio)
731{
732 blk_add_trace_bio(q, bio, BLK_TA_BOUNCE);
733}
734
735static void blk_add_trace_bio_complete(struct request_queue *q, struct bio *bio)
736{
737 blk_add_trace_bio(q, bio, BLK_TA_COMPLETE);
738}
739
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200740static void blk_add_trace_bio_backmerge(struct request_queue *q,
741 struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100742{
743 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
744}
745
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200746static void blk_add_trace_bio_frontmerge(struct request_queue *q,
747 struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100748{
749 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
750}
751
752static void blk_add_trace_bio_queue(struct request_queue *q, struct bio *bio)
753{
754 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
755}
756
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200757static void blk_add_trace_getrq(struct request_queue *q,
758 struct bio *bio, int rw)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100759{
760 if (bio)
761 blk_add_trace_bio(q, bio, BLK_TA_GETRQ);
762 else {
763 struct blk_trace *bt = q->blk_trace;
764
765 if (bt)
766 __blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL);
767 }
768}
769
770
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200771static void blk_add_trace_sleeprq(struct request_queue *q,
772 struct bio *bio, int rw)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100773{
774 if (bio)
775 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ);
776 else {
777 struct blk_trace *bt = q->blk_trace;
778
779 if (bt)
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200780 __blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ,
781 0, 0, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100782 }
783}
784
785static void blk_add_trace_plug(struct request_queue *q)
786{
787 struct blk_trace *bt = q->blk_trace;
788
789 if (bt)
790 __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
791}
792
793static void blk_add_trace_unplug_io(struct request_queue *q)
794{
795 struct blk_trace *bt = q->blk_trace;
796
797 if (bt) {
798 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
799 __be64 rpdu = cpu_to_be64(pdu);
800
801 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_IO, 0,
802 sizeof(rpdu), &rpdu);
803 }
804}
805
806static void blk_add_trace_unplug_timer(struct request_queue *q)
807{
808 struct blk_trace *bt = q->blk_trace;
809
810 if (bt) {
811 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
812 __be64 rpdu = cpu_to_be64(pdu);
813
814 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_TIMER, 0,
815 sizeof(rpdu), &rpdu);
816 }
817}
818
819static void blk_add_trace_split(struct request_queue *q, struct bio *bio,
820 unsigned int pdu)
821{
822 struct blk_trace *bt = q->blk_trace;
823
824 if (bt) {
825 __be64 rpdu = cpu_to_be64(pdu);
826
827 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
828 BLK_TA_SPLIT, !bio_flagged(bio, BIO_UPTODATE),
829 sizeof(rpdu), &rpdu);
830 }
831}
832
833/**
834 * blk_add_trace_remap - Add a trace for a remap operation
835 * @q: queue the io is for
836 * @bio: the source bio
837 * @dev: target device
Alan D. Brunellea42aaa32009-05-04 16:27:26 -0400838 * @from: source sector
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100839 *
840 * Description:
841 * Device mapper or raid target sometimes need to split a bio because
842 * it spans a stripe (or similar). Add a trace for that action.
843 *
844 **/
845static void blk_add_trace_remap(struct request_queue *q, struct bio *bio,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -0400846 dev_t dev, sector_t from)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100847{
848 struct blk_trace *bt = q->blk_trace;
849 struct blk_io_trace_remap r;
850
851 if (likely(!bt))
852 return;
853
Alan D. Brunellea42aaa32009-05-04 16:27:26 -0400854 r.device_from = cpu_to_be32(dev);
855 r.device_to = cpu_to_be32(bio->bi_bdev->bd_dev);
856 r.sector_from = cpu_to_be64(from);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100857
Alan D. Brunelle22a7c312009-05-04 16:35:08 -0400858 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
859 BLK_TA_REMAP, !bio_flagged(bio, BIO_UPTODATE),
860 sizeof(r), &r);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100861}
862
863/**
864 * blk_add_driver_data - Add binary message with driver-specific data
865 * @q: queue the io is for
866 * @rq: io request
867 * @data: driver-specific data
868 * @len: length of driver-specific data
869 *
870 * Description:
871 * Some drivers might want to write driver-specific data per request.
872 *
873 **/
874void blk_add_driver_data(struct request_queue *q,
875 struct request *rq,
876 void *data, size_t len)
877{
878 struct blk_trace *bt = q->blk_trace;
879
880 if (likely(!bt))
881 return;
882
883 if (blk_pc_request(rq))
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900884 __blk_add_trace(bt, 0, blk_rq_bytes(rq), 0,
885 BLK_TA_DRV_DATA, rq->errors, len, data);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100886 else
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900887 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), 0,
888 BLK_TA_DRV_DATA, rq->errors, len, data);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100889}
890EXPORT_SYMBOL_GPL(blk_add_driver_data);
891
Li Zefan3c289ba2009-03-20 09:48:26 +0800892static void blk_register_tracepoints(void)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100893{
894 int ret;
895
896 ret = register_trace_block_rq_abort(blk_add_trace_rq_abort);
897 WARN_ON(ret);
898 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert);
899 WARN_ON(ret);
900 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue);
901 WARN_ON(ret);
902 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue);
903 WARN_ON(ret);
904 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete);
905 WARN_ON(ret);
906 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce);
907 WARN_ON(ret);
908 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete);
909 WARN_ON(ret);
910 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
911 WARN_ON(ret);
912 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
913 WARN_ON(ret);
914 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue);
915 WARN_ON(ret);
916 ret = register_trace_block_getrq(blk_add_trace_getrq);
917 WARN_ON(ret);
918 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq);
919 WARN_ON(ret);
920 ret = register_trace_block_plug(blk_add_trace_plug);
921 WARN_ON(ret);
922 ret = register_trace_block_unplug_timer(blk_add_trace_unplug_timer);
923 WARN_ON(ret);
924 ret = register_trace_block_unplug_io(blk_add_trace_unplug_io);
925 WARN_ON(ret);
926 ret = register_trace_block_split(blk_add_trace_split);
927 WARN_ON(ret);
928 ret = register_trace_block_remap(blk_add_trace_remap);
929 WARN_ON(ret);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100930}
931
932static void blk_unregister_tracepoints(void)
933{
934 unregister_trace_block_remap(blk_add_trace_remap);
935 unregister_trace_block_split(blk_add_trace_split);
936 unregister_trace_block_unplug_io(blk_add_trace_unplug_io);
937 unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer);
938 unregister_trace_block_plug(blk_add_trace_plug);
939 unregister_trace_block_sleeprq(blk_add_trace_sleeprq);
940 unregister_trace_block_getrq(blk_add_trace_getrq);
941 unregister_trace_block_bio_queue(blk_add_trace_bio_queue);
942 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
943 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
944 unregister_trace_block_bio_complete(blk_add_trace_bio_complete);
945 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce);
946 unregister_trace_block_rq_complete(blk_add_trace_rq_complete);
947 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue);
948 unregister_trace_block_rq_issue(blk_add_trace_rq_issue);
949 unregister_trace_block_rq_insert(blk_add_trace_rq_insert);
950 unregister_trace_block_rq_abort(blk_add_trace_rq_abort);
951
952 tracepoint_synchronize_unregister();
953}
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200954
955/*
956 * struct blk_io_tracer formatting routines
957 */
958
959static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
960{
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200961 int i = 0;
Li Zefan65796342009-03-24 16:05:06 +0800962 int tc = t->action >> BLK_TC_SHIFT;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200963
Li Zefan18cea452009-03-27 10:21:54 +0800964 if (t->action == BLK_TN_MESSAGE) {
965 rwbs[i++] = 'N';
966 goto out;
967 }
968
Li Zefan65796342009-03-24 16:05:06 +0800969 if (tc & BLK_TC_DISCARD)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200970 rwbs[i++] = 'D';
Li Zefan65796342009-03-24 16:05:06 +0800971 else if (tc & BLK_TC_WRITE)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200972 rwbs[i++] = 'W';
973 else if (t->bytes)
974 rwbs[i++] = 'R';
975 else
976 rwbs[i++] = 'N';
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200977
Li Zefan65796342009-03-24 16:05:06 +0800978 if (tc & BLK_TC_AHEAD)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200979 rwbs[i++] = 'A';
Li Zefan65796342009-03-24 16:05:06 +0800980 if (tc & BLK_TC_BARRIER)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200981 rwbs[i++] = 'B';
Li Zefan65796342009-03-24 16:05:06 +0800982 if (tc & BLK_TC_SYNC)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200983 rwbs[i++] = 'S';
Li Zefan65796342009-03-24 16:05:06 +0800984 if (tc & BLK_TC_META)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200985 rwbs[i++] = 'M';
Li Zefan18cea452009-03-27 10:21:54 +0800986out:
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200987 rwbs[i] = '\0';
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200988}
989
990static inline
991const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
992{
993 return (const struct blk_io_trace *)ent;
994}
995
996static inline const void *pdu_start(const struct trace_entry *ent)
997{
998 return te_blk_io_trace(ent) + 1;
999}
1000
Li Zefan66de7792009-04-01 16:19:19 +08001001static inline u32 t_action(const struct trace_entry *ent)
1002{
1003 return te_blk_io_trace(ent)->action;
1004}
1005
1006static inline u32 t_bytes(const struct trace_entry *ent)
1007{
1008 return te_blk_io_trace(ent)->bytes;
1009}
1010
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001011static inline u32 t_sec(const struct trace_entry *ent)
1012{
1013 return te_blk_io_trace(ent)->bytes >> 9;
1014}
1015
1016static inline unsigned long long t_sector(const struct trace_entry *ent)
1017{
1018 return te_blk_io_trace(ent)->sector;
1019}
1020
1021static inline __u16 t_error(const struct trace_entry *ent)
1022{
Li Zefane0dc81b2009-03-24 16:05:51 +08001023 return te_blk_io_trace(ent)->error;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001024}
1025
1026static __u64 get_pdu_int(const struct trace_entry *ent)
1027{
1028 const __u64 *val = pdu_start(ent);
1029 return be64_to_cpu(*val);
1030}
1031
1032static void get_pdu_remap(const struct trace_entry *ent,
1033 struct blk_io_trace_remap *r)
1034{
1035 const struct blk_io_trace_remap *__r = pdu_start(ent);
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001036 __u64 sector_from = __r->sector_from;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001037
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001038 r->device_from = be32_to_cpu(__r->device_from);
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001039 r->device_to = be32_to_cpu(__r->device_to);
1040 r->sector_from = be64_to_cpu(sector_from);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001041}
1042
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001043typedef int (blk_log_action_t) (struct trace_iterator *iter, const char *act);
1044
1045static int blk_log_action_classic(struct trace_iterator *iter, const char *act)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001046{
1047 char rwbs[6];
Li Zefan35ac51b2009-03-27 10:19:46 +08001048 unsigned long long ts = iter->ts;
1049 unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001050 unsigned secs = (unsigned long)ts;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001051 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001052
1053 fill_rwbs(rwbs, t);
1054
1055 return trace_seq_printf(&iter->seq,
Li Zefan35ac51b2009-03-27 10:19:46 +08001056 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001057 MAJOR(t->device), MINOR(t->device), iter->cpu,
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001058 secs, nsec_rem, iter->ent->pid, act, rwbs);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001059}
1060
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001061static int blk_log_action(struct trace_iterator *iter, const char *act)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001062{
1063 char rwbs[6];
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001064 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1065
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001066 fill_rwbs(rwbs, t);
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001067 return trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001068 MAJOR(t->device), MINOR(t->device), act, rwbs);
1069}
1070
Li Zefan66de7792009-04-01 16:19:19 +08001071static int blk_log_dump_pdu(struct trace_seq *s, const struct trace_entry *ent)
1072{
Li Zefan04986252009-05-11 14:33:23 +08001073 const unsigned char *pdu_buf;
Li Zefan66de7792009-04-01 16:19:19 +08001074 int pdu_len;
1075 int i, end, ret;
1076
1077 pdu_buf = pdu_start(ent);
1078 pdu_len = te_blk_io_trace(ent)->pdu_len;
1079
1080 if (!pdu_len)
1081 return 1;
1082
1083 /* find the last zero that needs to be printed */
1084 for (end = pdu_len - 1; end >= 0; end--)
1085 if (pdu_buf[end])
1086 break;
1087 end++;
1088
1089 if (!trace_seq_putc(s, '('))
1090 return 0;
1091
1092 for (i = 0; i < pdu_len; i++) {
1093
1094 ret = trace_seq_printf(s, "%s%02x",
1095 i == 0 ? "" : " ", pdu_buf[i]);
1096 if (!ret)
1097 return ret;
1098
1099 /*
1100 * stop when the rest is just zeroes and indicate so
1101 * with a ".." appended
1102 */
1103 if (i == end && end != pdu_len - 1)
1104 return trace_seq_puts(s, " ..) ");
1105 }
1106
1107 return trace_seq_puts(s, ") ");
1108}
1109
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001110static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1111{
Steven Rostedt4ca53082009-03-16 19:20:15 -04001112 char cmd[TASK_COMM_LEN];
1113
1114 trace_find_cmdline(ent->pid, cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001115
Li Zefan66de7792009-04-01 16:19:19 +08001116 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1117 int ret;
1118
1119 ret = trace_seq_printf(s, "%u ", t_bytes(ent));
1120 if (!ret)
1121 return 0;
1122 ret = blk_log_dump_pdu(s, ent);
1123 if (!ret)
1124 return 0;
1125 return trace_seq_printf(s, "[%s]\n", cmd);
1126 } else {
1127 if (t_sec(ent))
1128 return trace_seq_printf(s, "%llu + %u [%s]\n",
1129 t_sector(ent), t_sec(ent), cmd);
1130 return trace_seq_printf(s, "[%s]\n", cmd);
1131 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001132}
1133
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001134static int blk_log_with_error(struct trace_seq *s,
1135 const struct trace_entry *ent)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001136{
Li Zefan66de7792009-04-01 16:19:19 +08001137 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1138 int ret;
1139
1140 ret = blk_log_dump_pdu(s, ent);
1141 if (ret)
1142 return trace_seq_printf(s, "[%d]\n", t_error(ent));
1143 return 0;
1144 } else {
1145 if (t_sec(ent))
1146 return trace_seq_printf(s, "%llu + %u [%d]\n",
1147 t_sector(ent),
1148 t_sec(ent), t_error(ent));
1149 return trace_seq_printf(s, "%llu [%d]\n",
1150 t_sector(ent), t_error(ent));
1151 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001152}
1153
1154static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1155{
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001156 struct blk_io_trace_remap r = { .device_from = 0, };
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001157
1158 get_pdu_remap(ent, &r);
1159 return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001160 t_sector(ent), t_sec(ent),
1161 MAJOR(r.device_from), MINOR(r.device_from),
1162 (unsigned long long)r.sector_from);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001163}
1164
1165static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1166{
Steven Rostedt4ca53082009-03-16 19:20:15 -04001167 char cmd[TASK_COMM_LEN];
1168
1169 trace_find_cmdline(ent->pid, cmd);
1170
1171 return trace_seq_printf(s, "[%s]\n", cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001172}
1173
1174static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1175{
Steven Rostedt4ca53082009-03-16 19:20:15 -04001176 char cmd[TASK_COMM_LEN];
1177
1178 trace_find_cmdline(ent->pid, cmd);
1179
1180 return trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent));
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001181}
1182
1183static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1184{
Steven Rostedt4ca53082009-03-16 19:20:15 -04001185 char cmd[TASK_COMM_LEN];
1186
1187 trace_find_cmdline(ent->pid, cmd);
1188
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001189 return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
Steven Rostedt4ca53082009-03-16 19:20:15 -04001190 get_pdu_int(ent), cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001191}
1192
Li Zefan18cea452009-03-27 10:21:54 +08001193static int blk_log_msg(struct trace_seq *s, const struct trace_entry *ent)
1194{
1195 int ret;
1196 const struct blk_io_trace *t = te_blk_io_trace(ent);
1197
1198 ret = trace_seq_putmem(s, t + 1, t->pdu_len);
1199 if (ret)
1200 return trace_seq_putc(s, '\n');
1201 return ret;
1202}
1203
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001204/*
1205 * struct tracer operations
1206 */
1207
1208static void blk_tracer_print_header(struct seq_file *m)
1209{
1210 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1211 return;
1212 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1213 "# | | | | | |\n");
1214}
1215
1216static void blk_tracer_start(struct trace_array *tr)
1217{
Li Zefanad5dd542009-03-27 10:20:24 +08001218 blk_tracer_enabled = true;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001219}
1220
1221static int blk_tracer_init(struct trace_array *tr)
1222{
1223 blk_tr = tr;
1224 blk_tracer_start(tr);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001225 return 0;
1226}
1227
1228static void blk_tracer_stop(struct trace_array *tr)
1229{
Li Zefanad5dd542009-03-27 10:20:24 +08001230 blk_tracer_enabled = false;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001231}
1232
1233static void blk_tracer_reset(struct trace_array *tr)
1234{
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001235 blk_tracer_stop(tr);
1236}
1237
Li Zefane4955c92009-03-24 16:04:37 +08001238static const struct {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001239 const char *act[2];
Steven Rostedtef180122009-03-10 14:10:56 -04001240 int (*print)(struct trace_seq *s, const struct trace_entry *ent);
Li Zefane4955c92009-03-24 16:04:37 +08001241} what2act[] = {
Steven Rostedtef180122009-03-10 14:10:56 -04001242 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001243 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1244 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1245 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1246 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1247 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1248 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1249 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1250 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1251 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
1252 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
1253 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1254 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1255 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1256 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1257};
1258
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001259static enum print_line_t print_one_line(struct trace_iterator *iter,
1260 bool classic)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001261{
Arnaldo Carvalho de Melo2c9b2382009-02-02 20:30:12 -02001262 struct trace_seq *s = &iter->seq;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001263 const struct blk_io_trace *t;
1264 u16 what;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001265 int ret;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001266 bool long_act;
1267 blk_log_action_t *log_action;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001268
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001269 t = te_blk_io_trace(iter->ent);
1270 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1271 long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1272 log_action = classic ? &blk_log_action_classic : &blk_log_action;
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001273
Li Zefan18cea452009-03-27 10:21:54 +08001274 if (t->action == BLK_TN_MESSAGE) {
1275 ret = log_action(iter, long_act ? "message" : "m");
1276 if (ret)
1277 ret = blk_log_msg(s, iter->ent);
1278 goto out;
1279 }
1280
Li Zefaneb08f8e2009-03-24 16:05:27 +08001281 if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
Li Zefanb78825d2009-04-01 16:18:53 +08001282 ret = trace_seq_printf(s, "Unknown action %x\n", what);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001283 else {
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001284 ret = log_action(iter, what2act[what].act[long_act]);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001285 if (ret)
Arnaldo Carvalho de Melo2c9b2382009-02-02 20:30:12 -02001286 ret = what2act[what].print(s, iter->ent);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001287 }
Li Zefan18cea452009-03-27 10:21:54 +08001288out:
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001289 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1290}
1291
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001292static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
1293 int flags)
1294{
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001295 return print_one_line(iter, false);
1296}
1297
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001298static int blk_trace_synthesize_old_trace(struct trace_iterator *iter)
1299{
1300 struct trace_seq *s = &iter->seq;
1301 struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1302 const int offset = offsetof(struct blk_io_trace, sector);
1303 struct blk_io_trace old = {
1304 .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
Li Zefan6c051ce2009-03-25 17:18:56 +08001305 .time = iter->ts,
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001306 };
1307
1308 if (!trace_seq_putmem(s, &old, offset))
1309 return 0;
1310 return trace_seq_putmem(s, &t->sector,
1311 sizeof(old) - offset + t->pdu_len);
1312}
1313
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -02001314static enum print_line_t
1315blk_trace_event_print_binary(struct trace_iterator *iter, int flags)
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001316{
1317 return blk_trace_synthesize_old_trace(iter) ?
1318 TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1319}
1320
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001321static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1322{
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001323 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1324 return TRACE_TYPE_UNHANDLED;
1325
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001326 return print_one_line(iter, true);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001327}
1328
Li Zefanf3948f82009-04-15 11:02:56 +08001329static int blk_tracer_set_flag(u32 old_flags, u32 bit, int set)
1330{
1331 /* don't output context-info for blk_classic output */
1332 if (bit == TRACE_BLK_OPT_CLASSIC) {
1333 if (set)
1334 trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
1335 else
1336 trace_flags |= TRACE_ITER_CONTEXT_INFO;
1337 }
1338 return 0;
1339}
1340
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001341static struct tracer blk_tracer __read_mostly = {
1342 .name = "blk",
1343 .init = blk_tracer_init,
1344 .reset = blk_tracer_reset,
1345 .start = blk_tracer_start,
1346 .stop = blk_tracer_stop,
1347 .print_header = blk_tracer_print_header,
1348 .print_line = blk_tracer_print_line,
1349 .flags = &blk_tracer_flags,
Li Zefanf3948f82009-04-15 11:02:56 +08001350 .set_flag = blk_tracer_set_flag,
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001351};
1352
1353static struct trace_event trace_blk_event = {
Steven Rostedtef180122009-03-10 14:10:56 -04001354 .type = TRACE_BLK,
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001355 .trace = blk_trace_event_print,
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001356 .binary = blk_trace_event_print_binary,
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001357};
1358
1359static int __init init_blk_tracer(void)
1360{
1361 if (!register_ftrace_event(&trace_blk_event)) {
1362 pr_warning("Warning: could not register block events\n");
1363 return 1;
1364 }
1365
1366 if (register_tracer(&blk_tracer) != 0) {
1367 pr_warning("Warning: could not register the block tracer\n");
1368 unregister_ftrace_event(&trace_blk_event);
1369 return 1;
1370 }
1371
1372 return 0;
1373}
1374
1375device_initcall(init_blk_tracer);
1376
1377static int blk_trace_remove_queue(struct request_queue *q)
1378{
1379 struct blk_trace *bt;
1380
1381 bt = xchg(&q->blk_trace, NULL);
1382 if (bt == NULL)
1383 return -EINVAL;
1384
Li Zefan17ba97e2009-03-27 10:20:09 +08001385 if (atomic_dec_and_test(&blk_probes_ref))
1386 blk_unregister_tracepoints();
1387
Li Zefanad5dd542009-03-27 10:20:24 +08001388 blk_trace_free(bt);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001389 return 0;
1390}
1391
1392/*
1393 * Setup everything required to start tracing
1394 */
Li Zefan9908c302009-04-14 13:59:34 +08001395static int blk_trace_setup_queue(struct request_queue *q,
1396 struct block_device *bdev)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001397{
1398 struct blk_trace *old_bt, *bt = NULL;
Li Zefan18cea452009-03-27 10:21:54 +08001399 int ret = -ENOMEM;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001400
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001401 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1402 if (!bt)
Li Zefan15152e42009-03-20 09:49:08 +08001403 return -ENOMEM;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001404
Li Zefan18cea452009-03-27 10:21:54 +08001405 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
1406 if (!bt->msg_data)
1407 goto free_bt;
1408
Li Zefan9908c302009-04-14 13:59:34 +08001409 bt->dev = bdev->bd_dev;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001410 bt->act_mask = (u16)-1;
Li Zefan9908c302009-04-14 13:59:34 +08001411
1412 blk_trace_setup_lba(bt, bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001413
1414 old_bt = xchg(&q->blk_trace, bt);
1415 if (old_bt != NULL) {
1416 (void)xchg(&q->blk_trace, old_bt);
Li Zefan18cea452009-03-27 10:21:54 +08001417 ret = -EBUSY;
1418 goto free_bt;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001419 }
Li Zefan15152e42009-03-20 09:49:08 +08001420
Li Zefan17ba97e2009-03-27 10:20:09 +08001421 if (atomic_inc_return(&blk_probes_ref) == 1)
1422 blk_register_tracepoints();
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001423 return 0;
Li Zefan18cea452009-03-27 10:21:54 +08001424
1425free_bt:
1426 blk_trace_free(bt);
1427 return ret;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001428}
1429
1430/*
1431 * sysfs interface to enable and configure tracing
1432 */
1433
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001434static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1435 struct device_attribute *attr,
1436 char *buf);
1437static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1438 struct device_attribute *attr,
1439 const char *buf, size_t count);
1440#define BLK_TRACE_DEVICE_ATTR(_name) \
1441 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1442 sysfs_blk_trace_attr_show, \
1443 sysfs_blk_trace_attr_store)
1444
Li Zefancd649b82009-03-20 11:33:55 +08001445static BLK_TRACE_DEVICE_ATTR(enable);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001446static BLK_TRACE_DEVICE_ATTR(act_mask);
1447static BLK_TRACE_DEVICE_ATTR(pid);
1448static BLK_TRACE_DEVICE_ATTR(start_lba);
1449static BLK_TRACE_DEVICE_ATTR(end_lba);
1450
1451static struct attribute *blk_trace_attrs[] = {
1452 &dev_attr_enable.attr,
1453 &dev_attr_act_mask.attr,
1454 &dev_attr_pid.attr,
1455 &dev_attr_start_lba.attr,
1456 &dev_attr_end_lba.attr,
1457 NULL
1458};
1459
1460struct attribute_group blk_trace_attr_group = {
1461 .name = "trace",
1462 .attrs = blk_trace_attrs,
1463};
1464
Li Zefan09341992009-03-24 17:43:30 +08001465static const struct {
1466 int mask;
1467 const char *str;
1468} mask_maps[] = {
1469 { BLK_TC_READ, "read" },
1470 { BLK_TC_WRITE, "write" },
1471 { BLK_TC_BARRIER, "barrier" },
1472 { BLK_TC_SYNC, "sync" },
1473 { BLK_TC_QUEUE, "queue" },
1474 { BLK_TC_REQUEUE, "requeue" },
1475 { BLK_TC_ISSUE, "issue" },
1476 { BLK_TC_COMPLETE, "complete" },
1477 { BLK_TC_FS, "fs" },
1478 { BLK_TC_PC, "pc" },
1479 { BLK_TC_AHEAD, "ahead" },
1480 { BLK_TC_META, "meta" },
1481 { BLK_TC_DISCARD, "discard" },
1482 { BLK_TC_DRV_DATA, "drv_data" },
1483};
1484
1485static int blk_trace_str2mask(const char *str)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001486{
Li Zefan09341992009-03-24 17:43:30 +08001487 int i;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001488 int mask = 0;
Li Zefan9eb85122009-04-09 11:19:40 +08001489 char *buf, *s, *token;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001490
Li Zefan9eb85122009-04-09 11:19:40 +08001491 buf = kstrdup(str, GFP_KERNEL);
1492 if (buf == NULL)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001493 return -ENOMEM;
Li Zefan9eb85122009-04-09 11:19:40 +08001494 s = strstrip(buf);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001495
1496 while (1) {
Li Zefan09341992009-03-24 17:43:30 +08001497 token = strsep(&s, ",");
1498 if (token == NULL)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001499 break;
1500
Li Zefan09341992009-03-24 17:43:30 +08001501 if (*token == '\0')
1502 continue;
1503
1504 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1505 if (strcasecmp(token, mask_maps[i].str) == 0) {
1506 mask |= mask_maps[i].mask;
1507 break;
1508 }
1509 }
1510 if (i == ARRAY_SIZE(mask_maps)) {
1511 mask = -EINVAL;
1512 break;
1513 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001514 }
Li Zefan9eb85122009-04-09 11:19:40 +08001515 kfree(buf);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001516
1517 return mask;
1518}
1519
Li Zefan09341992009-03-24 17:43:30 +08001520static ssize_t blk_trace_mask2str(char *buf, int mask)
1521{
1522 int i;
1523 char *p = buf;
1524
1525 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1526 if (mask & mask_maps[i].mask) {
1527 p += sprintf(p, "%s%s",
1528 (p == buf) ? "" : ",", mask_maps[i].str);
1529 }
1530 }
1531 *p++ = '\n';
1532
1533 return p - buf;
1534}
1535
Li Zefanb1251302009-03-20 10:34:00 +08001536static struct request_queue *blk_trace_get_queue(struct block_device *bdev)
1537{
1538 if (bdev->bd_disk == NULL)
1539 return NULL;
1540
1541 return bdev_get_queue(bdev);
1542}
1543
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001544static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1545 struct device_attribute *attr,
1546 char *buf)
1547{
1548 struct hd_struct *p = dev_to_part(dev);
1549 struct request_queue *q;
1550 struct block_device *bdev;
1551 ssize_t ret = -ENXIO;
1552
1553 lock_kernel();
1554 bdev = bdget(part_devt(p));
1555 if (bdev == NULL)
1556 goto out_unlock_kernel;
1557
Li Zefanb1251302009-03-20 10:34:00 +08001558 q = blk_trace_get_queue(bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001559 if (q == NULL)
1560 goto out_bdput;
Li Zefanb1251302009-03-20 10:34:00 +08001561
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001562 mutex_lock(&bdev->bd_mutex);
Li Zefancd649b82009-03-20 11:33:55 +08001563
1564 if (attr == &dev_attr_enable) {
1565 ret = sprintf(buf, "%u\n", !!q->blk_trace);
1566 goto out_unlock_bdev;
1567 }
1568
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001569 if (q->blk_trace == NULL)
1570 ret = sprintf(buf, "disabled\n");
1571 else if (attr == &dev_attr_act_mask)
Li Zefan09341992009-03-24 17:43:30 +08001572 ret = blk_trace_mask2str(buf, q->blk_trace->act_mask);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001573 else if (attr == &dev_attr_pid)
1574 ret = sprintf(buf, "%u\n", q->blk_trace->pid);
1575 else if (attr == &dev_attr_start_lba)
1576 ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
1577 else if (attr == &dev_attr_end_lba)
1578 ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
Li Zefancd649b82009-03-20 11:33:55 +08001579
1580out_unlock_bdev:
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001581 mutex_unlock(&bdev->bd_mutex);
1582out_bdput:
1583 bdput(bdev);
1584out_unlock_kernel:
1585 unlock_kernel();
1586 return ret;
1587}
1588
1589static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1590 struct device_attribute *attr,
1591 const char *buf, size_t count)
1592{
1593 struct block_device *bdev;
1594 struct request_queue *q;
1595 struct hd_struct *p;
1596 u64 value;
Li Zefan09341992009-03-24 17:43:30 +08001597 ssize_t ret = -EINVAL;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001598
1599 if (count == 0)
1600 goto out;
1601
1602 if (attr == &dev_attr_act_mask) {
1603 if (sscanf(buf, "%llx", &value) != 1) {
1604 /* Assume it is a list of trace category names */
Li Zefan09341992009-03-24 17:43:30 +08001605 ret = blk_trace_str2mask(buf);
1606 if (ret < 0)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001607 goto out;
Li Zefan09341992009-03-24 17:43:30 +08001608 value = ret;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001609 }
1610 } else if (sscanf(buf, "%llu", &value) != 1)
1611 goto out;
1612
Li Zefan09341992009-03-24 17:43:30 +08001613 ret = -ENXIO;
1614
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001615 lock_kernel();
1616 p = dev_to_part(dev);
1617 bdev = bdget(part_devt(p));
1618 if (bdev == NULL)
1619 goto out_unlock_kernel;
1620
Li Zefanb1251302009-03-20 10:34:00 +08001621 q = blk_trace_get_queue(bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001622 if (q == NULL)
1623 goto out_bdput;
1624
1625 mutex_lock(&bdev->bd_mutex);
Li Zefancd649b82009-03-20 11:33:55 +08001626
1627 if (attr == &dev_attr_enable) {
1628 if (value)
Li Zefan9908c302009-04-14 13:59:34 +08001629 ret = blk_trace_setup_queue(q, bdev);
Li Zefancd649b82009-03-20 11:33:55 +08001630 else
1631 ret = blk_trace_remove_queue(q);
1632 goto out_unlock_bdev;
1633 }
1634
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001635 ret = 0;
1636 if (q->blk_trace == NULL)
Li Zefan9908c302009-04-14 13:59:34 +08001637 ret = blk_trace_setup_queue(q, bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001638
1639 if (ret == 0) {
1640 if (attr == &dev_attr_act_mask)
1641 q->blk_trace->act_mask = value;
1642 else if (attr == &dev_attr_pid)
1643 q->blk_trace->pid = value;
1644 else if (attr == &dev_attr_start_lba)
1645 q->blk_trace->start_lba = value;
1646 else if (attr == &dev_attr_end_lba)
1647 q->blk_trace->end_lba = value;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001648 }
Li Zefancd649b82009-03-20 11:33:55 +08001649
1650out_unlock_bdev:
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001651 mutex_unlock(&bdev->bd_mutex);
1652out_bdput:
1653 bdput(bdev);
1654out_unlock_kernel:
1655 unlock_kernel();
1656out:
Li Zefancd649b82009-03-20 11:33:55 +08001657 return ret ? ret : count;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001658}
Li Zefancd649b82009-03-20 11:33:55 +08001659
Li Zefan1d54ad62009-04-14 14:00:05 +08001660int blk_trace_init_sysfs(struct device *dev)
1661{
1662 return sysfs_create_group(&dev->kobj, &blk_trace_attr_group);
1663}
1664
Li Zefan55782132009-06-09 13:43:05 +08001665#endif /* CONFIG_BLK_DEV_IO_TRACE */
1666
1667#ifdef CONFIG_EVENT_TRACING
1668
1669void blk_dump_cmd(char *buf, struct request *rq)
1670{
1671 int i, end;
1672 int len = rq->cmd_len;
1673 unsigned char *cmd = rq->cmd;
1674
1675 if (!blk_pc_request(rq)) {
1676 buf[0] = '\0';
1677 return;
1678 }
1679
1680 for (end = len - 1; end >= 0; end--)
1681 if (cmd[end])
1682 break;
1683 end++;
1684
1685 for (i = 0; i < len; i++) {
1686 buf += sprintf(buf, "%s%02x", i == 0 ? "" : " ", cmd[i]);
1687 if (i == end && end != len - 1) {
1688 sprintf(buf, " ..");
1689 break;
1690 }
1691 }
1692}
1693
1694void blk_fill_rwbs(char *rwbs, u32 rw, int bytes)
1695{
1696 int i = 0;
1697
1698 if (rw & WRITE)
1699 rwbs[i++] = 'W';
1700 else if (rw & 1 << BIO_RW_DISCARD)
1701 rwbs[i++] = 'D';
1702 else if (bytes)
1703 rwbs[i++] = 'R';
1704 else
1705 rwbs[i++] = 'N';
1706
1707 if (rw & 1 << BIO_RW_AHEAD)
1708 rwbs[i++] = 'A';
1709 if (rw & 1 << BIO_RW_BARRIER)
1710 rwbs[i++] = 'B';
1711 if (rw & 1 << BIO_RW_SYNCIO)
1712 rwbs[i++] = 'S';
1713 if (rw & 1 << BIO_RW_META)
1714 rwbs[i++] = 'M';
1715
1716 rwbs[i] = '\0';
1717}
1718
1719void blk_fill_rwbs_rq(char *rwbs, struct request *rq)
1720{
1721 int rw = rq->cmd_flags & 0x03;
1722 int bytes;
1723
1724 if (blk_discard_rq(rq))
1725 rw |= (1 << BIO_RW_DISCARD);
1726
Linus Torvaldsc9059592009-06-11 10:52:27 -07001727 bytes = blk_rq_bytes(rq);
Li Zefan55782132009-06-09 13:43:05 +08001728
1729 blk_fill_rwbs(rwbs, rw, bytes);
1730}
1731
1732#endif /* CONFIG_EVENT_TRACING */
1733