blob: 36ea2b65dcdc65a281bef8e7fa1acf0a44434003 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Jens Axboe2056a782006-03-23 20:00:26 +010025#include <linux/debugfs.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040026#include <linux/smp_lock.h>
Olaf Kirchbe1c6342006-12-01 10:39:12 +010027#include <linux/time.h>
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -020028#include <linux/uaccess.h>
Li Zefan55782132009-06-09 13:43:05 +080029
30#include <trace/events/block.h>
31
Frederic Weisbecker2db270a2009-02-07 20:46:45 +010032#include "trace_output.h"
Jens Axboe2056a782006-03-23 20:00:26 +010033
Li Zefan55782132009-06-09 13:43:05 +080034#ifdef CONFIG_BLK_DEV_IO_TRACE
35
Jens Axboe2056a782006-03-23 20:00:26 +010036static unsigned int blktrace_seq __read_mostly = 1;
37
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020038static struct trace_array *blk_tr;
Li Zefan5006ea72009-03-20 09:48:03 +080039static bool blk_tracer_enabled __read_mostly;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020040
41/* Select an alternative, minimalistic output than the original one */
Steven Rostedtef180122009-03-10 14:10:56 -040042#define TRACE_BLK_OPT_CLASSIC 0x1
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020043
44static struct tracer_opt blk_tracer_opts[] = {
45 /* Default disable the minimalistic output */
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -020046 { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020047 { }
48};
49
50static struct tracer_flags blk_tracer_flags = {
51 .val = 0,
52 .opts = blk_tracer_opts,
53};
54
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010055/* Global reference count of probes */
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010056static atomic_t blk_probes_ref = ATOMIC_INIT(0);
57
Li Zefan3c289ba2009-03-20 09:48:26 +080058static void blk_register_tracepoints(void);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010059static void blk_unregister_tracepoints(void);
60
Jens Axboe2056a782006-03-23 20:00:26 +010061/*
Olaf Kirchbe1c6342006-12-01 10:39:12 +010062 * Send out a notify message.
63 */
Jens Axboea8630552006-12-04 09:30:58 +010064static void trace_note(struct blk_trace *bt, pid_t pid, int action,
65 const void *data, size_t len)
Olaf Kirchbe1c6342006-12-01 10:39:12 +010066{
67 struct blk_io_trace *t;
Li Zefan18cea452009-03-27 10:21:54 +080068 struct ring_buffer_event *event = NULL;
Steven Rostedte77405a2009-09-02 14:17:06 -040069 struct ring_buffer *buffer = NULL;
Li Zefan18cea452009-03-27 10:21:54 +080070 int pc = 0;
71 int cpu = smp_processor_id();
72 bool blk_tracer = blk_tracer_enabled;
73
74 if (blk_tracer) {
Steven Rostedte77405a2009-09-02 14:17:06 -040075 buffer = blk_tr->buffer;
Li Zefan18cea452009-03-27 10:21:54 +080076 pc = preempt_count();
Steven Rostedte77405a2009-09-02 14:17:06 -040077 event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
Li Zefan18cea452009-03-27 10:21:54 +080078 sizeof(*t) + len,
79 0, pc);
80 if (!event)
81 return;
82 t = ring_buffer_event_data(event);
83 goto record_it;
84 }
Olaf Kirchbe1c6342006-12-01 10:39:12 +010085
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020086 if (!bt->rchan)
87 return;
88
Olaf Kirchbe1c6342006-12-01 10:39:12 +010089 t = relay_reserve(bt->rchan, sizeof(*t) + len);
Jens Axboed3d9d2a2006-12-04 09:27:41 +010090 if (t) {
Jens Axboed3d9d2a2006-12-04 09:27:41 +010091 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
Ingo Molnar2997c8c2008-01-11 13:35:54 +010092 t->time = ktime_to_ns(ktime_get());
Li Zefan18cea452009-03-27 10:21:54 +080093record_it:
Jens Axboed3d9d2a2006-12-04 09:27:41 +010094 t->device = bt->dev;
95 t->action = action;
96 t->pid = pid;
97 t->cpu = cpu;
98 t->pdu_len = len;
99 memcpy((void *) t + sizeof(*t), data, len);
Li Zefan18cea452009-03-27 10:21:54 +0800100
101 if (blk_tracer)
Steven Rostedte77405a2009-09-02 14:17:06 -0400102 trace_buffer_unlock_commit(buffer, event, 0, pc);
Jens Axboed3d9d2a2006-12-04 09:27:41 +0100103 }
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100104}
105
106/*
Jens Axboe2056a782006-03-23 20:00:26 +0100107 * Send out a notify for this process, if we haven't done so since a trace
108 * started
109 */
110static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk)
111{
Jens Axboea8630552006-12-04 09:30:58 +0100112 tsk->btrace_seq = blktrace_seq;
113 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm));
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100114}
Jens Axboe2056a782006-03-23 20:00:26 +0100115
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100116static void trace_note_time(struct blk_trace *bt)
117{
118 struct timespec now;
119 unsigned long flags;
120 u32 words[2];
121
122 getnstimeofday(&now);
123 words[0] = now.tv_sec;
124 words[1] = now.tv_nsec;
125
126 local_irq_save(flags);
127 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
128 local_irq_restore(flags);
Jens Axboe2056a782006-03-23 20:00:26 +0100129}
130
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200131void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
132{
133 int n;
134 va_list args;
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200135 unsigned long flags;
Jens Axboe64565912008-05-28 14:45:33 +0200136 char *buf;
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200137
Li Zefan18cea452009-03-27 10:21:54 +0800138 if (unlikely(bt->trace_state != Blktrace_running &&
139 !blk_tracer_enabled))
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200140 return;
141
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200142 local_irq_save(flags);
Jens Axboe64565912008-05-28 14:45:33 +0200143 buf = per_cpu_ptr(bt->msg_data, smp_processor_id());
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200144 va_start(args, fmt);
Jens Axboe64565912008-05-28 14:45:33 +0200145 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200146 va_end(args);
147
Jens Axboe64565912008-05-28 14:45:33 +0200148 trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200149 local_irq_restore(flags);
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200150}
151EXPORT_SYMBOL_GPL(__trace_note_message);
152
Jens Axboe2056a782006-03-23 20:00:26 +0100153static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
154 pid_t pid)
155{
156 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
157 return 1;
Shawn Dud0deef52009-04-14 13:58:56 +0800158 if (sector && (sector < bt->start_lba || sector > bt->end_lba))
Jens Axboe2056a782006-03-23 20:00:26 +0100159 return 1;
160 if (bt->pid && pid != bt->pid)
161 return 1;
162
163 return 0;
164}
165
166/*
167 * Data direction bit lookup
168 */
Li Zefane4955c92009-03-24 16:04:37 +0800169static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ),
170 BLK_TC_ACT(BLK_TC_WRITE) };
Jens Axboe2056a782006-03-23 20:00:26 +0100171
David Woodhouse35ba8f72008-08-10 12:33:00 +0100172/* The ilog2() calls fall out because they're constant */
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200173#define MASK_TC_BIT(rw, __name) ((rw & (1 << BIO_RW_ ## __name)) << \
174 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name))
Jens Axboe2056a782006-03-23 20:00:26 +0100175
176/*
177 * The worker for the various blk_add_trace*() types. Fills out a
178 * blk_io_trace structure and places it in a per-cpu subbuffer.
179 */
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100180static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
Jens Axboe2056a782006-03-23 20:00:26 +0100181 int rw, u32 what, int error, int pdu_len, void *pdu_data)
182{
183 struct task_struct *tsk = current;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200184 struct ring_buffer_event *event = NULL;
Steven Rostedte77405a2009-09-02 14:17:06 -0400185 struct ring_buffer *buffer = NULL;
Jens Axboe2056a782006-03-23 20:00:26 +0100186 struct blk_io_trace *t;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200187 unsigned long flags = 0;
Jens Axboe2056a782006-03-23 20:00:26 +0100188 unsigned long *sequence;
189 pid_t pid;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200190 int cpu, pc = 0;
Li Zefan18cea452009-03-27 10:21:54 +0800191 bool blk_tracer = blk_tracer_enabled;
Jens Axboe2056a782006-03-23 20:00:26 +0100192
Li Zefan18cea452009-03-27 10:21:54 +0800193 if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer))
Jens Axboe2056a782006-03-23 20:00:26 +0100194 return;
195
196 what |= ddir_act[rw & WRITE];
David Woodhouse35ba8f72008-08-10 12:33:00 +0100197 what |= MASK_TC_BIT(rw, BARRIER);
Ingo Molnar72c26c92009-02-19 09:00:35 +0100198 what |= MASK_TC_BIT(rw, SYNCIO);
David Woodhouse35ba8f72008-08-10 12:33:00 +0100199 what |= MASK_TC_BIT(rw, AHEAD);
200 what |= MASK_TC_BIT(rw, META);
201 what |= MASK_TC_BIT(rw, DISCARD);
Jens Axboe2056a782006-03-23 20:00:26 +0100202
203 pid = tsk->pid;
Shawn Dud0deef52009-04-14 13:58:56 +0800204 if (act_log_check(bt, what, sector, pid))
Jens Axboe2056a782006-03-23 20:00:26 +0100205 return;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200206 cpu = raw_smp_processor_id();
207
Li Zefan18cea452009-03-27 10:21:54 +0800208 if (blk_tracer) {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200209 tracing_record_cmdline(current);
210
Steven Rostedte77405a2009-09-02 14:17:06 -0400211 buffer = blk_tr->buffer;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200212 pc = preempt_count();
Steven Rostedte77405a2009-09-02 14:17:06 -0400213 event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200214 sizeof(*t) + pdu_len,
215 0, pc);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200216 if (!event)
217 return;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200218 t = ring_buffer_event_data(event);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200219 goto record_it;
220 }
Jens Axboe2056a782006-03-23 20:00:26 +0100221
222 /*
223 * A word about the locking here - we disable interrupts to reserve
224 * some space in the relay per-cpu buffer, to prevent an irq
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200225 * from coming in and stepping on our toes.
Jens Axboe2056a782006-03-23 20:00:26 +0100226 */
227 local_irq_save(flags);
228
229 if (unlikely(tsk->btrace_seq != blktrace_seq))
230 trace_note_tsk(bt, tsk);
231
232 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
233 if (t) {
Jens Axboe2056a782006-03-23 20:00:26 +0100234 sequence = per_cpu_ptr(bt->sequence, cpu);
235
236 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
237 t->sequence = ++(*sequence);
Ingo Molnar2997c8c2008-01-11 13:35:54 +0100238 t->time = ktime_to_ns(ktime_get());
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200239record_it:
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -0200240 /*
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200241 * These two are not needed in ftrace as they are in the
242 * generic trace_entry, filled by tracing_generic_entry_update,
243 * but for the trace_event->bin() synthesizer benefit we do it
244 * here too.
245 */
246 t->cpu = cpu;
247 t->pid = pid;
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -0200248
Jens Axboe2056a782006-03-23 20:00:26 +0100249 t->sector = sector;
250 t->bytes = bytes;
251 t->action = what;
Jens Axboe2056a782006-03-23 20:00:26 +0100252 t->device = bt->dev;
Jens Axboe2056a782006-03-23 20:00:26 +0100253 t->error = error;
254 t->pdu_len = pdu_len;
255
256 if (pdu_len)
257 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200258
Li Zefan18cea452009-03-27 10:21:54 +0800259 if (blk_tracer) {
Steven Rostedte77405a2009-09-02 14:17:06 -0400260 trace_buffer_unlock_commit(buffer, event, 0, pc);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200261 return;
262 }
Jens Axboe2056a782006-03-23 20:00:26 +0100263 }
264
265 local_irq_restore(flags);
266}
267
Jens Axboe2056a782006-03-23 20:00:26 +0100268static struct dentry *blk_tree_root;
Jens Axboe11a57152008-01-11 13:37:01 +0100269static DEFINE_MUTEX(blk_tree_mutex);
Jens Axboe2056a782006-03-23 20:00:26 +0100270
Li Zefanad5dd542009-03-27 10:20:24 +0800271static void blk_trace_free(struct blk_trace *bt)
Jens Axboe2056a782006-03-23 20:00:26 +0100272{
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200273 debugfs_remove(bt->msg_file);
Jens Axboe2056a782006-03-23 20:00:26 +0100274 debugfs_remove(bt->dropped_file);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100275 relay_close(bt->rchan);
Alan D. Brunelle39cbb602009-08-07 12:01:08 -0400276 debugfs_remove(bt->dir);
Jens Axboe2056a782006-03-23 20:00:26 +0100277 free_percpu(bt->sequence);
Jens Axboe64565912008-05-28 14:45:33 +0200278 free_percpu(bt->msg_data);
Jens Axboe2056a782006-03-23 20:00:26 +0100279 kfree(bt);
Li Zefanad5dd542009-03-27 10:20:24 +0800280}
281
282static void blk_trace_cleanup(struct blk_trace *bt)
283{
284 blk_trace_free(bt);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100285 if (atomic_dec_and_test(&blk_probes_ref))
286 blk_unregister_tracepoints();
Jens Axboe2056a782006-03-23 20:00:26 +0100287}
288
Christof Schmitt6da127a2008-01-11 10:09:43 +0100289int blk_trace_remove(struct request_queue *q)
Jens Axboe2056a782006-03-23 20:00:26 +0100290{
291 struct blk_trace *bt;
292
293 bt = xchg(&q->blk_trace, NULL);
294 if (!bt)
295 return -EINVAL;
296
Li Zefan55547202009-03-25 17:21:26 +0800297 if (bt->trace_state != Blktrace_running)
Jens Axboe2056a782006-03-23 20:00:26 +0100298 blk_trace_cleanup(bt);
299
300 return 0;
301}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100302EXPORT_SYMBOL_GPL(blk_trace_remove);
Jens Axboe2056a782006-03-23 20:00:26 +0100303
304static int blk_dropped_open(struct inode *inode, struct file *filp)
305{
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700306 filp->private_data = inode->i_private;
Jens Axboe2056a782006-03-23 20:00:26 +0100307
308 return 0;
309}
310
311static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
312 size_t count, loff_t *ppos)
313{
314 struct blk_trace *bt = filp->private_data;
315 char buf[16];
316
317 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
318
319 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
320}
321
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800322static const struct file_operations blk_dropped_fops = {
Jens Axboe2056a782006-03-23 20:00:26 +0100323 .owner = THIS_MODULE,
324 .open = blk_dropped_open,
325 .read = blk_dropped_read,
326};
327
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200328static int blk_msg_open(struct inode *inode, struct file *filp)
329{
330 filp->private_data = inode->i_private;
331
332 return 0;
333}
334
335static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
336 size_t count, loff_t *ppos)
337{
338 char *msg;
339 struct blk_trace *bt;
340
Li Zefan7635b032009-04-03 15:31:34 +0800341 if (count >= BLK_TN_MAX_MSG)
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200342 return -EINVAL;
343
Carl Henrik Lundea4b3ada2009-04-03 14:27:15 +0200344 msg = kmalloc(count + 1, GFP_KERNEL);
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200345 if (msg == NULL)
346 return -ENOMEM;
347
348 if (copy_from_user(msg, buffer, count)) {
349 kfree(msg);
350 return -EFAULT;
351 }
352
Carl Henrik Lundea4b3ada2009-04-03 14:27:15 +0200353 msg[count] = '\0';
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200354 bt = filp->private_data;
355 __trace_note_message(bt, "%s", msg);
356 kfree(msg);
357
358 return count;
359}
360
361static const struct file_operations blk_msg_fops = {
362 .owner = THIS_MODULE,
363 .open = blk_msg_open,
364 .write = blk_msg_write,
365};
366
Jens Axboe2056a782006-03-23 20:00:26 +0100367/*
368 * Keep track of how many times we encountered a full subbuffer, to aid
369 * the user space app in telling how many lost events there were.
370 */
371static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
372 void *prev_subbuf, size_t prev_padding)
373{
374 struct blk_trace *bt;
375
376 if (!relay_buf_full(buf))
377 return 1;
378
379 bt = buf->chan->private_data;
380 atomic_inc(&bt->dropped);
381 return 0;
382}
383
384static int blk_remove_buf_file_callback(struct dentry *dentry)
385{
386 debugfs_remove(dentry);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100387
Jens Axboe2056a782006-03-23 20:00:26 +0100388 return 0;
389}
390
391static struct dentry *blk_create_buf_file_callback(const char *filename,
392 struct dentry *parent,
393 int mode,
394 struct rchan_buf *buf,
395 int *is_global)
396{
397 return debugfs_create_file(filename, mode, parent, buf,
398 &relay_file_operations);
399}
400
401static struct rchan_callbacks blk_relay_callbacks = {
402 .subbuf_start = blk_subbuf_start_callback,
403 .create_buf_file = blk_create_buf_file_callback,
404 .remove_buf_file = blk_remove_buf_file_callback,
405};
406
Li Zefan9908c302009-04-14 13:59:34 +0800407static void blk_trace_setup_lba(struct blk_trace *bt,
408 struct block_device *bdev)
409{
410 struct hd_struct *part = NULL;
411
412 if (bdev)
413 part = bdev->bd_part;
414
415 if (part) {
416 bt->start_lba = part->start_sect;
417 bt->end_lba = part->start_sect + part->nr_sects;
418 } else {
419 bt->start_lba = 0;
420 bt->end_lba = -1ULL;
421 }
422}
423
Jens Axboe2056a782006-03-23 20:00:26 +0100424/*
425 * Setup everything required to start tracing
426 */
Christof Schmitt6da127a2008-01-11 10:09:43 +0100427int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
Shawn Dud0deef52009-04-14 13:58:56 +0800428 struct block_device *bdev,
429 struct blk_user_trace_setup *buts)
Jens Axboe2056a782006-03-23 20:00:26 +0100430{
Jens Axboe2056a782006-03-23 20:00:26 +0100431 struct blk_trace *old_bt, *bt = NULL;
432 struct dentry *dir = NULL;
Jens Axboe2056a782006-03-23 20:00:26 +0100433 int ret, i;
434
Arnd Bergmann171044d42007-10-09 13:23:53 +0200435 if (!buts->buf_size || !buts->buf_nr)
Jens Axboe2056a782006-03-23 20:00:26 +0100436 return -EINVAL;
437
Jens Axboe0497b342008-10-01 16:16:25 +0200438 strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
439 buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
Jens Axboe2056a782006-03-23 20:00:26 +0100440
441 /*
442 * some device names have larger paths - convert the slashes
443 * to underscores for this to work as expected
444 */
Arnd Bergmann171044d42007-10-09 13:23:53 +0200445 for (i = 0; i < strlen(buts->name); i++)
446 if (buts->name[i] == '/')
447 buts->name[i] = '_';
Jens Axboe2056a782006-03-23 20:00:26 +0100448
Jens Axboe2056a782006-03-23 20:00:26 +0100449 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
450 if (!bt)
Li Zefanad5dd542009-03-27 10:20:24 +0800451 return -ENOMEM;
Jens Axboe2056a782006-03-23 20:00:26 +0100452
Li Zefanad5dd542009-03-27 10:20:24 +0800453 ret = -ENOMEM;
Jens Axboe2056a782006-03-23 20:00:26 +0100454 bt->sequence = alloc_percpu(unsigned long);
455 if (!bt->sequence)
456 goto err;
457
Ingo Molnarf0ef0392009-03-06 16:44:14 +0100458 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
Jens Axboe64565912008-05-28 14:45:33 +0200459 if (!bt->msg_data)
460 goto err;
461
Jens Axboe2056a782006-03-23 20:00:26 +0100462 ret = -ENOENT;
Jens Axboef48fc4d2009-01-05 10:17:25 +0100463
Li Zefanb5230b52009-03-25 17:19:33 +0800464 mutex_lock(&blk_tree_mutex);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100465 if (!blk_tree_root) {
466 blk_tree_root = debugfs_create_dir("block", NULL);
Li Zefanb5230b52009-03-25 17:19:33 +0800467 if (!blk_tree_root) {
468 mutex_unlock(&blk_tree_mutex);
Li Zefan1a176622009-03-20 09:47:30 +0800469 goto err;
Li Zefanb5230b52009-03-25 17:19:33 +0800470 }
Jens Axboef48fc4d2009-01-05 10:17:25 +0100471 }
Li Zefanb5230b52009-03-25 17:19:33 +0800472 mutex_unlock(&blk_tree_mutex);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100473
474 dir = debugfs_create_dir(buts->name, blk_tree_root);
475
Jens Axboe2056a782006-03-23 20:00:26 +0100476 if (!dir)
477 goto err;
478
479 bt->dir = dir;
Christof Schmitt6da127a2008-01-11 10:09:43 +0100480 bt->dev = dev;
Jens Axboe2056a782006-03-23 20:00:26 +0100481 atomic_set(&bt->dropped, 0);
482
483 ret = -EIO;
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200484 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt,
485 &blk_dropped_fops);
Jens Axboe2056a782006-03-23 20:00:26 +0100486 if (!bt->dropped_file)
487 goto err;
488
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200489 bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
490 if (!bt->msg_file)
491 goto err;
492
Arnd Bergmann171044d42007-10-09 13:23:53 +0200493 bt->rchan = relay_open("trace", dir, buts->buf_size,
494 buts->buf_nr, &blk_relay_callbacks, bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100495 if (!bt->rchan)
496 goto err;
Jens Axboe2056a782006-03-23 20:00:26 +0100497
Arnd Bergmann171044d42007-10-09 13:23:53 +0200498 bt->act_mask = buts->act_mask;
Jens Axboe2056a782006-03-23 20:00:26 +0100499 if (!bt->act_mask)
500 bt->act_mask = (u16) -1;
501
Li Zefan9908c302009-04-14 13:59:34 +0800502 blk_trace_setup_lba(bt, bdev);
Jens Axboe2056a782006-03-23 20:00:26 +0100503
Shawn Dud0deef52009-04-14 13:58:56 +0800504 /* overwrite with user settings */
505 if (buts->start_lba)
506 bt->start_lba = buts->start_lba;
507 if (buts->end_lba)
508 bt->end_lba = buts->end_lba;
509
Arnd Bergmann171044d42007-10-09 13:23:53 +0200510 bt->pid = buts->pid;
Jens Axboe2056a782006-03-23 20:00:26 +0100511 bt->trace_state = Blktrace_setup;
512
513 ret = -EBUSY;
514 old_bt = xchg(&q->blk_trace, bt);
515 if (old_bt) {
516 (void) xchg(&q->blk_trace, old_bt);
517 goto err;
518 }
519
Li Zefan17ba97e2009-03-27 10:20:09 +0800520 if (atomic_inc_return(&blk_probes_ref) == 1)
Li Zefancbe28292009-03-20 09:48:47 +0800521 blk_register_tracepoints();
522
Jens Axboe2056a782006-03-23 20:00:26 +0100523 return 0;
524err:
Li Zefanad5dd542009-03-27 10:20:24 +0800525 blk_trace_free(bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100526 return ret;
527}
528
Christof Schmitt6da127a2008-01-11 10:09:43 +0100529int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
Shawn Dud0deef52009-04-14 13:58:56 +0800530 struct block_device *bdev,
Christof Schmitt6da127a2008-01-11 10:09:43 +0100531 char __user *arg)
Arnd Bergmann171044d42007-10-09 13:23:53 +0200532{
533 struct blk_user_trace_setup buts;
534 int ret;
535
536 ret = copy_from_user(&buts, arg, sizeof(buts));
537 if (ret)
538 return -EFAULT;
539
Shawn Dud0deef52009-04-14 13:58:56 +0800540 ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200541 if (ret)
542 return ret;
543
Dmitry Monakhov9a8c28c2010-02-27 20:35:13 +0300544 if (copy_to_user(arg, &buts, sizeof(buts))) {
545 blk_trace_remove(q);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200546 return -EFAULT;
Dmitry Monakhov9a8c28c2010-02-27 20:35:13 +0300547 }
Arnd Bergmann171044d42007-10-09 13:23:53 +0200548 return 0;
549}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100550EXPORT_SYMBOL_GPL(blk_trace_setup);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200551
Christof Schmitt6da127a2008-01-11 10:09:43 +0100552int blk_trace_startstop(struct request_queue *q, int start)
Jens Axboe2056a782006-03-23 20:00:26 +0100553{
Jens Axboe2056a782006-03-23 20:00:26 +0100554 int ret;
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200555 struct blk_trace *bt = q->blk_trace;
Jens Axboe2056a782006-03-23 20:00:26 +0100556
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200557 if (bt == NULL)
Jens Axboe2056a782006-03-23 20:00:26 +0100558 return -EINVAL;
559
560 /*
561 * For starting a trace, we can transition from a setup or stopped
562 * trace. For stopping a trace, the state must be running
563 */
564 ret = -EINVAL;
565 if (start) {
566 if (bt->trace_state == Blktrace_setup ||
567 bt->trace_state == Blktrace_stopped) {
568 blktrace_seq++;
569 smp_mb();
570 bt->trace_state = Blktrace_running;
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100571
572 trace_note_time(bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100573 ret = 0;
574 }
575 } else {
576 if (bt->trace_state == Blktrace_running) {
577 bt->trace_state = Blktrace_stopped;
578 relay_flush(bt->rchan);
579 ret = 0;
580 }
581 }
582
583 return ret;
584}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100585EXPORT_SYMBOL_GPL(blk_trace_startstop);
Jens Axboe2056a782006-03-23 20:00:26 +0100586
587/**
588 * blk_trace_ioctl: - handle the ioctls associated with tracing
589 * @bdev: the block device
Steven Rostedtef180122009-03-10 14:10:56 -0400590 * @cmd: the ioctl cmd
Jens Axboe2056a782006-03-23 20:00:26 +0100591 * @arg: the argument data, if any
592 *
593 **/
594int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
595{
Jens Axboe165125e2007-07-24 09:28:11 +0200596 struct request_queue *q;
Jens Axboe2056a782006-03-23 20:00:26 +0100597 int ret, start = 0;
Christof Schmitt6da127a2008-01-11 10:09:43 +0100598 char b[BDEVNAME_SIZE];
Jens Axboe2056a782006-03-23 20:00:26 +0100599
600 q = bdev_get_queue(bdev);
601 if (!q)
602 return -ENXIO;
603
604 mutex_lock(&bdev->bd_mutex);
605
606 switch (cmd) {
607 case BLKTRACESETUP:
Jean Delvaref36f21e2008-05-12 14:02:33 -0700608 bdevname(bdev, b);
Shawn Dud0deef52009-04-14 13:58:56 +0800609 ret = blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
Jens Axboe2056a782006-03-23 20:00:26 +0100610 break;
611 case BLKTRACESTART:
612 start = 1;
613 case BLKTRACESTOP:
614 ret = blk_trace_startstop(q, start);
615 break;
616 case BLKTRACETEARDOWN:
617 ret = blk_trace_remove(q);
618 break;
619 default:
620 ret = -ENOTTY;
621 break;
622 }
623
624 mutex_unlock(&bdev->bd_mutex);
625 return ret;
626}
627
628/**
629 * blk_trace_shutdown: - stop and cleanup trace structures
630 * @q: the request queue associated with the device
631 *
632 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200633void blk_trace_shutdown(struct request_queue *q)
Jens Axboe2056a782006-03-23 20:00:26 +0100634{
Alexey Dobriyan6c5c9342006-09-29 01:59:40 -0700635 if (q->blk_trace) {
636 blk_trace_startstop(q, 0);
637 blk_trace_remove(q);
638 }
Jens Axboe2056a782006-03-23 20:00:26 +0100639}
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100640
641/*
642 * blktrace probes
643 */
644
645/**
646 * blk_add_trace_rq - Add a trace for a request oriented action
647 * @q: queue the io is for
648 * @rq: the source request
649 * @what: the action
650 *
651 * Description:
652 * Records an action against a request. Will log the bio offset + size.
653 *
654 **/
655static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
656 u32 what)
657{
658 struct blk_trace *bt = q->blk_trace;
659 int rw = rq->cmd_flags & 0x03;
660
661 if (likely(!bt))
662 return;
663
664 if (blk_discard_rq(rq))
665 rw |= (1 << BIO_RW_DISCARD);
666
667 if (blk_pc_request(rq)) {
668 what |= BLK_TC_ACT(BLK_TC_PC);
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900669 __blk_add_trace(bt, 0, blk_rq_bytes(rq), rw,
670 what, rq->errors, rq->cmd_len, rq->cmd);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100671 } else {
672 what |= BLK_TC_ACT(BLK_TC_FS);
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900673 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), rw,
674 what, rq->errors, 0, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100675 }
676}
677
Steven Rostedt38516ab2010-04-20 17:04:50 -0400678static void blk_add_trace_rq_abort(void *ignore,
679 struct request_queue *q, struct request *rq)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100680{
681 blk_add_trace_rq(q, rq, BLK_TA_ABORT);
682}
683
Steven Rostedt38516ab2010-04-20 17:04:50 -0400684static void blk_add_trace_rq_insert(void *ignore,
685 struct request_queue *q, struct request *rq)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100686{
687 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
688}
689
Steven Rostedt38516ab2010-04-20 17:04:50 -0400690static void blk_add_trace_rq_issue(void *ignore,
691 struct request_queue *q, struct request *rq)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100692{
693 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
694}
695
Steven Rostedt38516ab2010-04-20 17:04:50 -0400696static void blk_add_trace_rq_requeue(void *ignore,
697 struct request_queue *q,
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200698 struct request *rq)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100699{
700 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
701}
702
Steven Rostedt38516ab2010-04-20 17:04:50 -0400703static void blk_add_trace_rq_complete(void *ignore,
704 struct request_queue *q,
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200705 struct request *rq)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100706{
707 blk_add_trace_rq(q, rq, BLK_TA_COMPLETE);
708}
709
710/**
711 * blk_add_trace_bio - Add a trace for a bio oriented action
712 * @q: queue the io is for
713 * @bio: the source bio
714 * @what: the action
715 *
716 * Description:
717 * Records an action against a bio. Will log the bio offset + size.
718 *
719 **/
720static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
721 u32 what)
722{
723 struct blk_trace *bt = q->blk_trace;
724
725 if (likely(!bt))
726 return;
727
728 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what,
729 !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
730}
731
Steven Rostedt38516ab2010-04-20 17:04:50 -0400732static void blk_add_trace_bio_bounce(void *ignore,
733 struct request_queue *q, struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100734{
735 blk_add_trace_bio(q, bio, BLK_TA_BOUNCE);
736}
737
Steven Rostedt38516ab2010-04-20 17:04:50 -0400738static void blk_add_trace_bio_complete(void *ignore,
739 struct request_queue *q, struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100740{
741 blk_add_trace_bio(q, bio, BLK_TA_COMPLETE);
742}
743
Steven Rostedt38516ab2010-04-20 17:04:50 -0400744static void blk_add_trace_bio_backmerge(void *ignore,
745 struct request_queue *q,
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200746 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
Steven Rostedt38516ab2010-04-20 17:04:50 -0400751static void blk_add_trace_bio_frontmerge(void *ignore,
752 struct request_queue *q,
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200753 struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100754{
755 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
756}
757
Steven Rostedt38516ab2010-04-20 17:04:50 -0400758static void blk_add_trace_bio_queue(void *ignore,
759 struct request_queue *q, struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100760{
761 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
762}
763
Steven Rostedt38516ab2010-04-20 17:04:50 -0400764static void blk_add_trace_getrq(void *ignore,
765 struct request_queue *q,
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200766 struct bio *bio, int rw)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100767{
768 if (bio)
769 blk_add_trace_bio(q, bio, BLK_TA_GETRQ);
770 else {
771 struct blk_trace *bt = q->blk_trace;
772
773 if (bt)
774 __blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL);
775 }
776}
777
778
Steven Rostedt38516ab2010-04-20 17:04:50 -0400779static void blk_add_trace_sleeprq(void *ignore,
780 struct request_queue *q,
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200781 struct bio *bio, int rw)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100782{
783 if (bio)
784 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ);
785 else {
786 struct blk_trace *bt = q->blk_trace;
787
788 if (bt)
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200789 __blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ,
790 0, 0, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100791 }
792}
793
Steven Rostedt38516ab2010-04-20 17:04:50 -0400794static void blk_add_trace_plug(void *ignore, struct request_queue *q)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100795{
796 struct blk_trace *bt = q->blk_trace;
797
798 if (bt)
799 __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
800}
801
Steven Rostedt38516ab2010-04-20 17:04:50 -0400802static void blk_add_trace_unplug_io(void *ignore, struct request_queue *q)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100803{
804 struct blk_trace *bt = q->blk_trace;
805
806 if (bt) {
807 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
808 __be64 rpdu = cpu_to_be64(pdu);
809
810 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_IO, 0,
811 sizeof(rpdu), &rpdu);
812 }
813}
814
Steven Rostedt38516ab2010-04-20 17:04:50 -0400815static void blk_add_trace_unplug_timer(void *ignore, struct request_queue *q)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100816{
817 struct blk_trace *bt = q->blk_trace;
818
819 if (bt) {
820 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
821 __be64 rpdu = cpu_to_be64(pdu);
822
823 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_TIMER, 0,
824 sizeof(rpdu), &rpdu);
825 }
826}
827
Steven Rostedt38516ab2010-04-20 17:04:50 -0400828static void blk_add_trace_split(void *ignore,
829 struct request_queue *q, struct bio *bio,
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100830 unsigned int pdu)
831{
832 struct blk_trace *bt = q->blk_trace;
833
834 if (bt) {
835 __be64 rpdu = cpu_to_be64(pdu);
836
837 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
838 BLK_TA_SPLIT, !bio_flagged(bio, BIO_UPTODATE),
839 sizeof(rpdu), &rpdu);
840 }
841}
842
843/**
844 * blk_add_trace_remap - Add a trace for a remap operation
845 * @q: queue the io is for
846 * @bio: the source bio
847 * @dev: target device
Alan D. Brunellea42aaa32009-05-04 16:27:26 -0400848 * @from: source sector
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100849 *
850 * Description:
851 * Device mapper or raid target sometimes need to split a bio because
852 * it spans a stripe (or similar). Add a trace for that action.
853 *
854 **/
Steven Rostedt38516ab2010-04-20 17:04:50 -0400855static void blk_add_trace_remap(void *ignore,
856 struct request_queue *q, struct bio *bio,
857 dev_t dev, sector_t from)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100858{
859 struct blk_trace *bt = q->blk_trace;
860 struct blk_io_trace_remap r;
861
862 if (likely(!bt))
863 return;
864
Alan D. Brunellea42aaa32009-05-04 16:27:26 -0400865 r.device_from = cpu_to_be32(dev);
866 r.device_to = cpu_to_be32(bio->bi_bdev->bd_dev);
867 r.sector_from = cpu_to_be64(from);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100868
Alan D. Brunelle22a7c312009-05-04 16:35:08 -0400869 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
870 BLK_TA_REMAP, !bio_flagged(bio, BIO_UPTODATE),
871 sizeof(r), &r);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100872}
873
874/**
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +0200875 * blk_add_trace_rq_remap - Add a trace for a request-remap operation
876 * @q: queue the io is for
877 * @rq: the source request
878 * @dev: target device
879 * @from: source sector
880 *
881 * Description:
882 * Device mapper remaps request to other devices.
883 * Add a trace for that action.
884 *
885 **/
Steven Rostedt38516ab2010-04-20 17:04:50 -0400886static void blk_add_trace_rq_remap(void *ignore,
887 struct request_queue *q,
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +0200888 struct request *rq, dev_t dev,
889 sector_t from)
890{
891 struct blk_trace *bt = q->blk_trace;
892 struct blk_io_trace_remap r;
893
894 if (likely(!bt))
895 return;
896
897 r.device_from = cpu_to_be32(dev);
898 r.device_to = cpu_to_be32(disk_devt(rq->rq_disk));
899 r.sector_from = cpu_to_be64(from);
900
901 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq),
902 rq_data_dir(rq), BLK_TA_REMAP, !!rq->errors,
903 sizeof(r), &r);
904}
905
906/**
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100907 * blk_add_driver_data - Add binary message with driver-specific data
908 * @q: queue the io is for
909 * @rq: io request
910 * @data: driver-specific data
911 * @len: length of driver-specific data
912 *
913 * Description:
914 * Some drivers might want to write driver-specific data per request.
915 *
916 **/
917void blk_add_driver_data(struct request_queue *q,
918 struct request *rq,
919 void *data, size_t len)
920{
921 struct blk_trace *bt = q->blk_trace;
922
923 if (likely(!bt))
924 return;
925
926 if (blk_pc_request(rq))
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900927 __blk_add_trace(bt, 0, blk_rq_bytes(rq), 0,
928 BLK_TA_DRV_DATA, rq->errors, len, data);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100929 else
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900930 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), 0,
931 BLK_TA_DRV_DATA, rq->errors, len, data);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100932}
933EXPORT_SYMBOL_GPL(blk_add_driver_data);
934
Li Zefan3c289ba2009-03-20 09:48:26 +0800935static void blk_register_tracepoints(void)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100936{
937 int ret;
938
Steven Rostedt38516ab2010-04-20 17:04:50 -0400939 ret = register_trace_block_rq_abort(blk_add_trace_rq_abort, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100940 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -0400941 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100942 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -0400943 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100944 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -0400945 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100946 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -0400947 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100948 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -0400949 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100950 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -0400951 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100952 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -0400953 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100954 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -0400955 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100956 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -0400957 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100958 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -0400959 ret = register_trace_block_getrq(blk_add_trace_getrq, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100960 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -0400961 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100962 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -0400963 ret = register_trace_block_plug(blk_add_trace_plug, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100964 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -0400965 ret = register_trace_block_unplug_timer(blk_add_trace_unplug_timer, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100966 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -0400967 ret = register_trace_block_unplug_io(blk_add_trace_unplug_io, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100968 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -0400969 ret = register_trace_block_split(blk_add_trace_split, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100970 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -0400971 ret = register_trace_block_remap(blk_add_trace_remap, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100972 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -0400973 ret = register_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +0200974 WARN_ON(ret);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100975}
976
977static void blk_unregister_tracepoints(void)
978{
Steven Rostedt38516ab2010-04-20 17:04:50 -0400979 unregister_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
980 unregister_trace_block_remap(blk_add_trace_remap, NULL);
981 unregister_trace_block_split(blk_add_trace_split, NULL);
982 unregister_trace_block_unplug_io(blk_add_trace_unplug_io, NULL);
983 unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer, NULL);
984 unregister_trace_block_plug(blk_add_trace_plug, NULL);
985 unregister_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
986 unregister_trace_block_getrq(blk_add_trace_getrq, NULL);
987 unregister_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
988 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
989 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
990 unregister_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
991 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
992 unregister_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
993 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
994 unregister_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
995 unregister_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
996 unregister_trace_block_rq_abort(blk_add_trace_rq_abort, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100997
998 tracepoint_synchronize_unregister();
999}
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001000
1001/*
1002 * struct blk_io_tracer formatting routines
1003 */
1004
1005static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
1006{
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001007 int i = 0;
Li Zefan65796342009-03-24 16:05:06 +08001008 int tc = t->action >> BLK_TC_SHIFT;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001009
Li Zefan18cea452009-03-27 10:21:54 +08001010 if (t->action == BLK_TN_MESSAGE) {
1011 rwbs[i++] = 'N';
1012 goto out;
1013 }
1014
Li Zefan65796342009-03-24 16:05:06 +08001015 if (tc & BLK_TC_DISCARD)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001016 rwbs[i++] = 'D';
Li Zefan65796342009-03-24 16:05:06 +08001017 else if (tc & BLK_TC_WRITE)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001018 rwbs[i++] = 'W';
1019 else if (t->bytes)
1020 rwbs[i++] = 'R';
1021 else
1022 rwbs[i++] = 'N';
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001023
Li Zefan65796342009-03-24 16:05:06 +08001024 if (tc & BLK_TC_AHEAD)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001025 rwbs[i++] = 'A';
Li Zefan65796342009-03-24 16:05:06 +08001026 if (tc & BLK_TC_BARRIER)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001027 rwbs[i++] = 'B';
Li Zefan65796342009-03-24 16:05:06 +08001028 if (tc & BLK_TC_SYNC)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001029 rwbs[i++] = 'S';
Li Zefan65796342009-03-24 16:05:06 +08001030 if (tc & BLK_TC_META)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001031 rwbs[i++] = 'M';
Li Zefan18cea452009-03-27 10:21:54 +08001032out:
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001033 rwbs[i] = '\0';
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001034}
1035
1036static inline
1037const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
1038{
1039 return (const struct blk_io_trace *)ent;
1040}
1041
1042static inline const void *pdu_start(const struct trace_entry *ent)
1043{
1044 return te_blk_io_trace(ent) + 1;
1045}
1046
Li Zefan66de7792009-04-01 16:19:19 +08001047static inline u32 t_action(const struct trace_entry *ent)
1048{
1049 return te_blk_io_trace(ent)->action;
1050}
1051
1052static inline u32 t_bytes(const struct trace_entry *ent)
1053{
1054 return te_blk_io_trace(ent)->bytes;
1055}
1056
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001057static inline u32 t_sec(const struct trace_entry *ent)
1058{
1059 return te_blk_io_trace(ent)->bytes >> 9;
1060}
1061
1062static inline unsigned long long t_sector(const struct trace_entry *ent)
1063{
1064 return te_blk_io_trace(ent)->sector;
1065}
1066
1067static inline __u16 t_error(const struct trace_entry *ent)
1068{
Li Zefane0dc81b2009-03-24 16:05:51 +08001069 return te_blk_io_trace(ent)->error;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001070}
1071
1072static __u64 get_pdu_int(const struct trace_entry *ent)
1073{
1074 const __u64 *val = pdu_start(ent);
1075 return be64_to_cpu(*val);
1076}
1077
1078static void get_pdu_remap(const struct trace_entry *ent,
1079 struct blk_io_trace_remap *r)
1080{
1081 const struct blk_io_trace_remap *__r = pdu_start(ent);
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001082 __u64 sector_from = __r->sector_from;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001083
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001084 r->device_from = be32_to_cpu(__r->device_from);
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001085 r->device_to = be32_to_cpu(__r->device_to);
1086 r->sector_from = be64_to_cpu(sector_from);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001087}
1088
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001089typedef int (blk_log_action_t) (struct trace_iterator *iter, const char *act);
1090
1091static int blk_log_action_classic(struct trace_iterator *iter, const char *act)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001092{
1093 char rwbs[6];
Li Zefan35ac51b2009-03-27 10:19:46 +08001094 unsigned long long ts = iter->ts;
1095 unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001096 unsigned secs = (unsigned long)ts;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001097 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001098
1099 fill_rwbs(rwbs, t);
1100
1101 return trace_seq_printf(&iter->seq,
Li Zefan35ac51b2009-03-27 10:19:46 +08001102 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001103 MAJOR(t->device), MINOR(t->device), iter->cpu,
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001104 secs, nsec_rem, iter->ent->pid, act, rwbs);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001105}
1106
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001107static int blk_log_action(struct trace_iterator *iter, const char *act)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001108{
1109 char rwbs[6];
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001110 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1111
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001112 fill_rwbs(rwbs, t);
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001113 return trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001114 MAJOR(t->device), MINOR(t->device), act, rwbs);
1115}
1116
Li Zefan66de7792009-04-01 16:19:19 +08001117static int blk_log_dump_pdu(struct trace_seq *s, const struct trace_entry *ent)
1118{
Li Zefan049862572009-05-11 14:33:23 +08001119 const unsigned char *pdu_buf;
Li Zefan66de7792009-04-01 16:19:19 +08001120 int pdu_len;
1121 int i, end, ret;
1122
1123 pdu_buf = pdu_start(ent);
1124 pdu_len = te_blk_io_trace(ent)->pdu_len;
1125
1126 if (!pdu_len)
1127 return 1;
1128
1129 /* find the last zero that needs to be printed */
1130 for (end = pdu_len - 1; end >= 0; end--)
1131 if (pdu_buf[end])
1132 break;
1133 end++;
1134
1135 if (!trace_seq_putc(s, '('))
1136 return 0;
1137
1138 for (i = 0; i < pdu_len; i++) {
1139
1140 ret = trace_seq_printf(s, "%s%02x",
1141 i == 0 ? "" : " ", pdu_buf[i]);
1142 if (!ret)
1143 return ret;
1144
1145 /*
1146 * stop when the rest is just zeroes and indicate so
1147 * with a ".." appended
1148 */
1149 if (i == end && end != pdu_len - 1)
1150 return trace_seq_puts(s, " ..) ");
1151 }
1152
1153 return trace_seq_puts(s, ") ");
1154}
1155
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001156static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1157{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001158 char cmd[TASK_COMM_LEN];
1159
1160 trace_find_cmdline(ent->pid, cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001161
Li Zefan66de7792009-04-01 16:19:19 +08001162 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1163 int ret;
1164
1165 ret = trace_seq_printf(s, "%u ", t_bytes(ent));
1166 if (!ret)
1167 return 0;
1168 ret = blk_log_dump_pdu(s, ent);
1169 if (!ret)
1170 return 0;
1171 return trace_seq_printf(s, "[%s]\n", cmd);
1172 } else {
1173 if (t_sec(ent))
1174 return trace_seq_printf(s, "%llu + %u [%s]\n",
1175 t_sector(ent), t_sec(ent), cmd);
1176 return trace_seq_printf(s, "[%s]\n", cmd);
1177 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001178}
1179
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001180static int blk_log_with_error(struct trace_seq *s,
1181 const struct trace_entry *ent)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001182{
Li Zefan66de7792009-04-01 16:19:19 +08001183 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1184 int ret;
1185
1186 ret = blk_log_dump_pdu(s, ent);
1187 if (ret)
1188 return trace_seq_printf(s, "[%d]\n", t_error(ent));
1189 return 0;
1190 } else {
1191 if (t_sec(ent))
1192 return trace_seq_printf(s, "%llu + %u [%d]\n",
1193 t_sector(ent),
1194 t_sec(ent), t_error(ent));
1195 return trace_seq_printf(s, "%llu [%d]\n",
1196 t_sector(ent), t_error(ent));
1197 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001198}
1199
1200static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1201{
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001202 struct blk_io_trace_remap r = { .device_from = 0, };
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001203
1204 get_pdu_remap(ent, &r);
1205 return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001206 t_sector(ent), t_sec(ent),
1207 MAJOR(r.device_from), MINOR(r.device_from),
1208 (unsigned long long)r.sector_from);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001209}
1210
1211static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1212{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001213 char cmd[TASK_COMM_LEN];
1214
1215 trace_find_cmdline(ent->pid, cmd);
1216
1217 return trace_seq_printf(s, "[%s]\n", cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001218}
1219
1220static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1221{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001222 char cmd[TASK_COMM_LEN];
1223
1224 trace_find_cmdline(ent->pid, cmd);
1225
1226 return trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent));
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001227}
1228
1229static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1230{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001231 char cmd[TASK_COMM_LEN];
1232
1233 trace_find_cmdline(ent->pid, cmd);
1234
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001235 return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
Steven Rostedt4ca530852009-03-16 19:20:15 -04001236 get_pdu_int(ent), cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001237}
1238
Li Zefan18cea452009-03-27 10:21:54 +08001239static int blk_log_msg(struct trace_seq *s, const struct trace_entry *ent)
1240{
1241 int ret;
1242 const struct blk_io_trace *t = te_blk_io_trace(ent);
1243
1244 ret = trace_seq_putmem(s, t + 1, t->pdu_len);
1245 if (ret)
1246 return trace_seq_putc(s, '\n');
1247 return ret;
1248}
1249
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001250/*
1251 * struct tracer operations
1252 */
1253
1254static void blk_tracer_print_header(struct seq_file *m)
1255{
1256 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1257 return;
1258 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1259 "# | | | | | |\n");
1260}
1261
1262static void blk_tracer_start(struct trace_array *tr)
1263{
Li Zefanad5dd542009-03-27 10:20:24 +08001264 blk_tracer_enabled = true;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001265}
1266
1267static int blk_tracer_init(struct trace_array *tr)
1268{
1269 blk_tr = tr;
1270 blk_tracer_start(tr);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001271 return 0;
1272}
1273
1274static void blk_tracer_stop(struct trace_array *tr)
1275{
Li Zefanad5dd542009-03-27 10:20:24 +08001276 blk_tracer_enabled = false;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001277}
1278
1279static void blk_tracer_reset(struct trace_array *tr)
1280{
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001281 blk_tracer_stop(tr);
1282}
1283
Li Zefane4955c92009-03-24 16:04:37 +08001284static const struct {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001285 const char *act[2];
Steven Rostedtef180122009-03-10 14:10:56 -04001286 int (*print)(struct trace_seq *s, const struct trace_entry *ent);
Li Zefane4955c92009-03-24 16:04:37 +08001287} what2act[] = {
Steven Rostedtef180122009-03-10 14:10:56 -04001288 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001289 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1290 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1291 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1292 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1293 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1294 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1295 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1296 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1297 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
1298 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
1299 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1300 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1301 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1302 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1303};
1304
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001305static enum print_line_t print_one_line(struct trace_iterator *iter,
1306 bool classic)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001307{
Arnaldo Carvalho de Melo2c9b2382009-02-02 20:30:12 -02001308 struct trace_seq *s = &iter->seq;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001309 const struct blk_io_trace *t;
1310 u16 what;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001311 int ret;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001312 bool long_act;
1313 blk_log_action_t *log_action;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001314
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001315 t = te_blk_io_trace(iter->ent);
1316 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1317 long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1318 log_action = classic ? &blk_log_action_classic : &blk_log_action;
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001319
Li Zefan18cea452009-03-27 10:21:54 +08001320 if (t->action == BLK_TN_MESSAGE) {
1321 ret = log_action(iter, long_act ? "message" : "m");
1322 if (ret)
1323 ret = blk_log_msg(s, iter->ent);
1324 goto out;
1325 }
1326
Li Zefaneb08f8e2009-03-24 16:05:27 +08001327 if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
Li Zefanb78825d2009-04-01 16:18:53 +08001328 ret = trace_seq_printf(s, "Unknown action %x\n", what);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001329 else {
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001330 ret = log_action(iter, what2act[what].act[long_act]);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001331 if (ret)
Arnaldo Carvalho de Melo2c9b2382009-02-02 20:30:12 -02001332 ret = what2act[what].print(s, iter->ent);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001333 }
Li Zefan18cea452009-03-27 10:21:54 +08001334out:
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001335 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1336}
1337
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001338static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
Steven Rostedta9a57762010-04-22 18:46:14 -04001339 int flags, struct trace_event *event)
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001340{
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001341 return print_one_line(iter, false);
1342}
1343
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001344static int blk_trace_synthesize_old_trace(struct trace_iterator *iter)
1345{
1346 struct trace_seq *s = &iter->seq;
1347 struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1348 const int offset = offsetof(struct blk_io_trace, sector);
1349 struct blk_io_trace old = {
1350 .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
Li Zefan6c051ce2009-03-25 17:18:56 +08001351 .time = iter->ts,
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001352 };
1353
1354 if (!trace_seq_putmem(s, &old, offset))
1355 return 0;
1356 return trace_seq_putmem(s, &t->sector,
1357 sizeof(old) - offset + t->pdu_len);
1358}
1359
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -02001360static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001361blk_trace_event_print_binary(struct trace_iterator *iter, int flags,
1362 struct trace_event *event)
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001363{
1364 return blk_trace_synthesize_old_trace(iter) ?
1365 TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1366}
1367
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001368static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1369{
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001370 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1371 return TRACE_TYPE_UNHANDLED;
1372
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001373 return print_one_line(iter, true);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001374}
1375
Li Zefanf3948f82009-04-15 11:02:56 +08001376static int blk_tracer_set_flag(u32 old_flags, u32 bit, int set)
1377{
1378 /* don't output context-info for blk_classic output */
1379 if (bit == TRACE_BLK_OPT_CLASSIC) {
1380 if (set)
1381 trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
1382 else
1383 trace_flags |= TRACE_ITER_CONTEXT_INFO;
1384 }
1385 return 0;
1386}
1387
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001388static struct tracer blk_tracer __read_mostly = {
1389 .name = "blk",
1390 .init = blk_tracer_init,
1391 .reset = blk_tracer_reset,
1392 .start = blk_tracer_start,
1393 .stop = blk_tracer_stop,
1394 .print_header = blk_tracer_print_header,
1395 .print_line = blk_tracer_print_line,
1396 .flags = &blk_tracer_flags,
Li Zefanf3948f82009-04-15 11:02:56 +08001397 .set_flag = blk_tracer_set_flag,
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001398};
1399
Steven Rostedta9a57762010-04-22 18:46:14 -04001400static struct trace_event_functions trace_blk_event_funcs = {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001401 .trace = blk_trace_event_print,
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001402 .binary = blk_trace_event_print_binary,
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001403};
1404
Steven Rostedta9a57762010-04-22 18:46:14 -04001405static struct trace_event trace_blk_event = {
1406 .type = TRACE_BLK,
1407 .funcs = &trace_blk_event_funcs,
1408};
1409
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001410static int __init init_blk_tracer(void)
1411{
1412 if (!register_ftrace_event(&trace_blk_event)) {
1413 pr_warning("Warning: could not register block events\n");
1414 return 1;
1415 }
1416
1417 if (register_tracer(&blk_tracer) != 0) {
1418 pr_warning("Warning: could not register the block tracer\n");
1419 unregister_ftrace_event(&trace_blk_event);
1420 return 1;
1421 }
1422
1423 return 0;
1424}
1425
1426device_initcall(init_blk_tracer);
1427
1428static int blk_trace_remove_queue(struct request_queue *q)
1429{
1430 struct blk_trace *bt;
1431
1432 bt = xchg(&q->blk_trace, NULL);
1433 if (bt == NULL)
1434 return -EINVAL;
1435
Li Zefan17ba97e2009-03-27 10:20:09 +08001436 if (atomic_dec_and_test(&blk_probes_ref))
1437 blk_unregister_tracepoints();
1438
Li Zefanad5dd542009-03-27 10:20:24 +08001439 blk_trace_free(bt);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001440 return 0;
1441}
1442
1443/*
1444 * Setup everything required to start tracing
1445 */
Li Zefan9908c302009-04-14 13:59:34 +08001446static int blk_trace_setup_queue(struct request_queue *q,
1447 struct block_device *bdev)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001448{
1449 struct blk_trace *old_bt, *bt = NULL;
Li Zefan18cea452009-03-27 10:21:54 +08001450 int ret = -ENOMEM;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001451
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001452 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1453 if (!bt)
Li Zefan15152e42009-03-20 09:49:08 +08001454 return -ENOMEM;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001455
Li Zefan18cea452009-03-27 10:21:54 +08001456 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
1457 if (!bt->msg_data)
1458 goto free_bt;
1459
Li Zefan9908c302009-04-14 13:59:34 +08001460 bt->dev = bdev->bd_dev;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001461 bt->act_mask = (u16)-1;
Li Zefan9908c302009-04-14 13:59:34 +08001462
1463 blk_trace_setup_lba(bt, bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001464
1465 old_bt = xchg(&q->blk_trace, bt);
1466 if (old_bt != NULL) {
1467 (void)xchg(&q->blk_trace, old_bt);
Li Zefan18cea452009-03-27 10:21:54 +08001468 ret = -EBUSY;
1469 goto free_bt;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001470 }
Li Zefan15152e42009-03-20 09:49:08 +08001471
Li Zefan17ba97e2009-03-27 10:20:09 +08001472 if (atomic_inc_return(&blk_probes_ref) == 1)
1473 blk_register_tracepoints();
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001474 return 0;
Li Zefan18cea452009-03-27 10:21:54 +08001475
1476free_bt:
1477 blk_trace_free(bt);
1478 return ret;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001479}
1480
1481/*
1482 * sysfs interface to enable and configure tracing
1483 */
1484
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001485static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1486 struct device_attribute *attr,
1487 char *buf);
1488static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1489 struct device_attribute *attr,
1490 const char *buf, size_t count);
1491#define BLK_TRACE_DEVICE_ATTR(_name) \
1492 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1493 sysfs_blk_trace_attr_show, \
1494 sysfs_blk_trace_attr_store)
1495
Li Zefancd649b82009-03-20 11:33:55 +08001496static BLK_TRACE_DEVICE_ATTR(enable);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001497static BLK_TRACE_DEVICE_ATTR(act_mask);
1498static BLK_TRACE_DEVICE_ATTR(pid);
1499static BLK_TRACE_DEVICE_ATTR(start_lba);
1500static BLK_TRACE_DEVICE_ATTR(end_lba);
1501
1502static struct attribute *blk_trace_attrs[] = {
1503 &dev_attr_enable.attr,
1504 &dev_attr_act_mask.attr,
1505 &dev_attr_pid.attr,
1506 &dev_attr_start_lba.attr,
1507 &dev_attr_end_lba.attr,
1508 NULL
1509};
1510
1511struct attribute_group blk_trace_attr_group = {
1512 .name = "trace",
1513 .attrs = blk_trace_attrs,
1514};
1515
Li Zefan09341992009-03-24 17:43:30 +08001516static const struct {
1517 int mask;
1518 const char *str;
1519} mask_maps[] = {
1520 { BLK_TC_READ, "read" },
1521 { BLK_TC_WRITE, "write" },
1522 { BLK_TC_BARRIER, "barrier" },
1523 { BLK_TC_SYNC, "sync" },
1524 { BLK_TC_QUEUE, "queue" },
1525 { BLK_TC_REQUEUE, "requeue" },
1526 { BLK_TC_ISSUE, "issue" },
1527 { BLK_TC_COMPLETE, "complete" },
1528 { BLK_TC_FS, "fs" },
1529 { BLK_TC_PC, "pc" },
1530 { BLK_TC_AHEAD, "ahead" },
1531 { BLK_TC_META, "meta" },
1532 { BLK_TC_DISCARD, "discard" },
1533 { BLK_TC_DRV_DATA, "drv_data" },
1534};
1535
1536static int blk_trace_str2mask(const char *str)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001537{
Li Zefan09341992009-03-24 17:43:30 +08001538 int i;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001539 int mask = 0;
Li Zefan9eb85122009-04-09 11:19:40 +08001540 char *buf, *s, *token;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001541
Li Zefan9eb85122009-04-09 11:19:40 +08001542 buf = kstrdup(str, GFP_KERNEL);
1543 if (buf == NULL)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001544 return -ENOMEM;
Li Zefan9eb85122009-04-09 11:19:40 +08001545 s = strstrip(buf);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001546
1547 while (1) {
Li Zefan09341992009-03-24 17:43:30 +08001548 token = strsep(&s, ",");
1549 if (token == NULL)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001550 break;
1551
Li Zefan09341992009-03-24 17:43:30 +08001552 if (*token == '\0')
1553 continue;
1554
1555 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1556 if (strcasecmp(token, mask_maps[i].str) == 0) {
1557 mask |= mask_maps[i].mask;
1558 break;
1559 }
1560 }
1561 if (i == ARRAY_SIZE(mask_maps)) {
1562 mask = -EINVAL;
1563 break;
1564 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001565 }
Li Zefan9eb85122009-04-09 11:19:40 +08001566 kfree(buf);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001567
1568 return mask;
1569}
1570
Li Zefan09341992009-03-24 17:43:30 +08001571static ssize_t blk_trace_mask2str(char *buf, int mask)
1572{
1573 int i;
1574 char *p = buf;
1575
1576 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1577 if (mask & mask_maps[i].mask) {
1578 p += sprintf(p, "%s%s",
1579 (p == buf) ? "" : ",", mask_maps[i].str);
1580 }
1581 }
1582 *p++ = '\n';
1583
1584 return p - buf;
1585}
1586
Li Zefanb1251302009-03-20 10:34:00 +08001587static struct request_queue *blk_trace_get_queue(struct block_device *bdev)
1588{
1589 if (bdev->bd_disk == NULL)
1590 return NULL;
1591
1592 return bdev_get_queue(bdev);
1593}
1594
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001595static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1596 struct device_attribute *attr,
1597 char *buf)
1598{
1599 struct hd_struct *p = dev_to_part(dev);
1600 struct request_queue *q;
1601 struct block_device *bdev;
1602 ssize_t ret = -ENXIO;
1603
1604 lock_kernel();
1605 bdev = bdget(part_devt(p));
1606 if (bdev == NULL)
1607 goto out_unlock_kernel;
1608
Li Zefanb1251302009-03-20 10:34:00 +08001609 q = blk_trace_get_queue(bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001610 if (q == NULL)
1611 goto out_bdput;
Li Zefanb1251302009-03-20 10:34:00 +08001612
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001613 mutex_lock(&bdev->bd_mutex);
Li Zefancd649b82009-03-20 11:33:55 +08001614
1615 if (attr == &dev_attr_enable) {
1616 ret = sprintf(buf, "%u\n", !!q->blk_trace);
1617 goto out_unlock_bdev;
1618 }
1619
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001620 if (q->blk_trace == NULL)
1621 ret = sprintf(buf, "disabled\n");
1622 else if (attr == &dev_attr_act_mask)
Li Zefan09341992009-03-24 17:43:30 +08001623 ret = blk_trace_mask2str(buf, q->blk_trace->act_mask);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001624 else if (attr == &dev_attr_pid)
1625 ret = sprintf(buf, "%u\n", q->blk_trace->pid);
1626 else if (attr == &dev_attr_start_lba)
1627 ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
1628 else if (attr == &dev_attr_end_lba)
1629 ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
Li Zefancd649b82009-03-20 11:33:55 +08001630
1631out_unlock_bdev:
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001632 mutex_unlock(&bdev->bd_mutex);
1633out_bdput:
1634 bdput(bdev);
1635out_unlock_kernel:
1636 unlock_kernel();
1637 return ret;
1638}
1639
1640static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1641 struct device_attribute *attr,
1642 const char *buf, size_t count)
1643{
1644 struct block_device *bdev;
1645 struct request_queue *q;
1646 struct hd_struct *p;
1647 u64 value;
Li Zefan09341992009-03-24 17:43:30 +08001648 ssize_t ret = -EINVAL;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001649
1650 if (count == 0)
1651 goto out;
1652
1653 if (attr == &dev_attr_act_mask) {
1654 if (sscanf(buf, "%llx", &value) != 1) {
1655 /* Assume it is a list of trace category names */
Li Zefan09341992009-03-24 17:43:30 +08001656 ret = blk_trace_str2mask(buf);
1657 if (ret < 0)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001658 goto out;
Li Zefan09341992009-03-24 17:43:30 +08001659 value = ret;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001660 }
1661 } else if (sscanf(buf, "%llu", &value) != 1)
1662 goto out;
1663
Li Zefan09341992009-03-24 17:43:30 +08001664 ret = -ENXIO;
1665
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001666 lock_kernel();
1667 p = dev_to_part(dev);
1668 bdev = bdget(part_devt(p));
1669 if (bdev == NULL)
1670 goto out_unlock_kernel;
1671
Li Zefanb1251302009-03-20 10:34:00 +08001672 q = blk_trace_get_queue(bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001673 if (q == NULL)
1674 goto out_bdput;
1675
1676 mutex_lock(&bdev->bd_mutex);
Li Zefancd649b82009-03-20 11:33:55 +08001677
1678 if (attr == &dev_attr_enable) {
1679 if (value)
Li Zefan9908c302009-04-14 13:59:34 +08001680 ret = blk_trace_setup_queue(q, bdev);
Li Zefancd649b82009-03-20 11:33:55 +08001681 else
1682 ret = blk_trace_remove_queue(q);
1683 goto out_unlock_bdev;
1684 }
1685
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001686 ret = 0;
1687 if (q->blk_trace == NULL)
Li Zefan9908c302009-04-14 13:59:34 +08001688 ret = blk_trace_setup_queue(q, bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001689
1690 if (ret == 0) {
1691 if (attr == &dev_attr_act_mask)
1692 q->blk_trace->act_mask = value;
1693 else if (attr == &dev_attr_pid)
1694 q->blk_trace->pid = value;
1695 else if (attr == &dev_attr_start_lba)
1696 q->blk_trace->start_lba = value;
1697 else if (attr == &dev_attr_end_lba)
1698 q->blk_trace->end_lba = value;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001699 }
Li Zefancd649b82009-03-20 11:33:55 +08001700
1701out_unlock_bdev:
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001702 mutex_unlock(&bdev->bd_mutex);
1703out_bdput:
1704 bdput(bdev);
1705out_unlock_kernel:
1706 unlock_kernel();
1707out:
Li Zefancd649b82009-03-20 11:33:55 +08001708 return ret ? ret : count;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001709}
Li Zefancd649b82009-03-20 11:33:55 +08001710
Li Zefan1d54ad62009-04-14 14:00:05 +08001711int blk_trace_init_sysfs(struct device *dev)
1712{
1713 return sysfs_create_group(&dev->kobj, &blk_trace_attr_group);
1714}
1715
Zdenek Kabelac48c0d4d2009-09-25 06:19:26 +02001716void blk_trace_remove_sysfs(struct device *dev)
1717{
1718 sysfs_remove_group(&dev->kobj, &blk_trace_attr_group);
1719}
1720
Li Zefan55782132009-06-09 13:43:05 +08001721#endif /* CONFIG_BLK_DEV_IO_TRACE */
1722
1723#ifdef CONFIG_EVENT_TRACING
1724
1725void blk_dump_cmd(char *buf, struct request *rq)
1726{
1727 int i, end;
1728 int len = rq->cmd_len;
1729 unsigned char *cmd = rq->cmd;
1730
1731 if (!blk_pc_request(rq)) {
1732 buf[0] = '\0';
1733 return;
1734 }
1735
1736 for (end = len - 1; end >= 0; end--)
1737 if (cmd[end])
1738 break;
1739 end++;
1740
1741 for (i = 0; i < len; i++) {
1742 buf += sprintf(buf, "%s%02x", i == 0 ? "" : " ", cmd[i]);
1743 if (i == end && end != len - 1) {
1744 sprintf(buf, " ..");
1745 break;
1746 }
1747 }
1748}
1749
1750void blk_fill_rwbs(char *rwbs, u32 rw, int bytes)
1751{
1752 int i = 0;
1753
1754 if (rw & WRITE)
1755 rwbs[i++] = 'W';
1756 else if (rw & 1 << BIO_RW_DISCARD)
1757 rwbs[i++] = 'D';
1758 else if (bytes)
1759 rwbs[i++] = 'R';
1760 else
1761 rwbs[i++] = 'N';
1762
1763 if (rw & 1 << BIO_RW_AHEAD)
1764 rwbs[i++] = 'A';
1765 if (rw & 1 << BIO_RW_BARRIER)
1766 rwbs[i++] = 'B';
1767 if (rw & 1 << BIO_RW_SYNCIO)
1768 rwbs[i++] = 'S';
1769 if (rw & 1 << BIO_RW_META)
1770 rwbs[i++] = 'M';
1771
1772 rwbs[i] = '\0';
1773}
1774
1775void blk_fill_rwbs_rq(char *rwbs, struct request *rq)
1776{
1777 int rw = rq->cmd_flags & 0x03;
1778 int bytes;
1779
1780 if (blk_discard_rq(rq))
1781 rw |= (1 << BIO_RW_DISCARD);
1782
Linus Torvaldsc9059592009-06-11 10:52:27 -07001783 bytes = blk_rq_bytes(rq);
Li Zefan55782132009-06-09 13:43:05 +08001784
1785 blk_fill_rwbs(rwbs, rw, bytes);
1786}
1787
1788#endif /* CONFIG_EVENT_TRACING */
1789