blob: 1090b0aed9bac6135d359e0cb07317085e6145d6 [file] [log] [blame]
Jens Axboe2056a782006-03-23 20:00:26 +01001/*
Jens Axboe0fe23472006-09-04 15:41:16 +02002 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
Jens Axboe2056a782006-03-23 20:00:26 +01003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 *
17 */
Jens Axboe2056a782006-03-23 20:00:26 +010018#include <linux/kernel.h>
19#include <linux/blkdev.h>
20#include <linux/blktrace_api.h>
21#include <linux/percpu.h>
22#include <linux/init.h>
23#include <linux/mutex.h>
24#include <linux/debugfs.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040025#include <linux/smp_lock.h>
Olaf Kirchbe1c6342006-12-01 10:39:12 +010026#include <linux/time.h>
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -020027#include <linux/uaccess.h>
Li Zefan55782132009-06-09 13:43:05 +080028
29#include <trace/events/block.h>
30
Frederic Weisbecker2db270a2009-02-07 20:46:45 +010031#include "trace_output.h"
Jens Axboe2056a782006-03-23 20:00:26 +010032
Li Zefan55782132009-06-09 13:43:05 +080033#ifdef CONFIG_BLK_DEV_IO_TRACE
34
Jens Axboe2056a782006-03-23 20:00:26 +010035static unsigned int blktrace_seq __read_mostly = 1;
36
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020037static struct trace_array *blk_tr;
Li Zefan5006ea72009-03-20 09:48:03 +080038static bool blk_tracer_enabled __read_mostly;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020039
40/* Select an alternative, minimalistic output than the original one */
Steven Rostedtef180122009-03-10 14:10:56 -040041#define TRACE_BLK_OPT_CLASSIC 0x1
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020042
43static struct tracer_opt blk_tracer_opts[] = {
44 /* Default disable the minimalistic output */
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -020045 { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020046 { }
47};
48
49static struct tracer_flags blk_tracer_flags = {
50 .val = 0,
51 .opts = blk_tracer_opts,
52};
53
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010054/* Global reference count of probes */
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010055static atomic_t blk_probes_ref = ATOMIC_INIT(0);
56
Li Zefan3c289ba2009-03-20 09:48:26 +080057static void blk_register_tracepoints(void);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010058static void blk_unregister_tracepoints(void);
59
Jens Axboe2056a782006-03-23 20:00:26 +010060/*
Olaf Kirchbe1c6342006-12-01 10:39:12 +010061 * Send out a notify message.
62 */
Jens Axboea8630552006-12-04 09:30:58 +010063static void trace_note(struct blk_trace *bt, pid_t pid, int action,
64 const void *data, size_t len)
Olaf Kirchbe1c6342006-12-01 10:39:12 +010065{
66 struct blk_io_trace *t;
Li Zefan18cea452009-03-27 10:21:54 +080067 struct ring_buffer_event *event = NULL;
68 int pc = 0;
69 int cpu = smp_processor_id();
70 bool blk_tracer = blk_tracer_enabled;
71
72 if (blk_tracer) {
73 pc = preempt_count();
74 event = trace_buffer_lock_reserve(blk_tr, TRACE_BLK,
75 sizeof(*t) + len,
76 0, pc);
77 if (!event)
78 return;
79 t = ring_buffer_event_data(event);
80 goto record_it;
81 }
Olaf Kirchbe1c6342006-12-01 10:39:12 +010082
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020083 if (!bt->rchan)
84 return;
85
Olaf Kirchbe1c6342006-12-01 10:39:12 +010086 t = relay_reserve(bt->rchan, sizeof(*t) + len);
Jens Axboed3d9d2a2006-12-04 09:27:41 +010087 if (t) {
Jens Axboed3d9d2a2006-12-04 09:27:41 +010088 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
Ingo Molnar2997c8c2008-01-11 13:35:54 +010089 t->time = ktime_to_ns(ktime_get());
Li Zefan18cea452009-03-27 10:21:54 +080090record_it:
Jens Axboed3d9d2a2006-12-04 09:27:41 +010091 t->device = bt->dev;
92 t->action = action;
93 t->pid = pid;
94 t->cpu = cpu;
95 t->pdu_len = len;
96 memcpy((void *) t + sizeof(*t), data, len);
Li Zefan18cea452009-03-27 10:21:54 +080097
98 if (blk_tracer)
99 trace_buffer_unlock_commit(blk_tr, event, 0, pc);
Jens Axboed3d9d2a2006-12-04 09:27:41 +0100100 }
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100101}
102
103/*
Jens Axboe2056a782006-03-23 20:00:26 +0100104 * Send out a notify for this process, if we haven't done so since a trace
105 * started
106 */
107static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk)
108{
Jens Axboea8630552006-12-04 09:30:58 +0100109 tsk->btrace_seq = blktrace_seq;
110 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm));
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100111}
Jens Axboe2056a782006-03-23 20:00:26 +0100112
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100113static void trace_note_time(struct blk_trace *bt)
114{
115 struct timespec now;
116 unsigned long flags;
117 u32 words[2];
118
119 getnstimeofday(&now);
120 words[0] = now.tv_sec;
121 words[1] = now.tv_nsec;
122
123 local_irq_save(flags);
124 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
125 local_irq_restore(flags);
Jens Axboe2056a782006-03-23 20:00:26 +0100126}
127
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200128void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
129{
130 int n;
131 va_list args;
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200132 unsigned long flags;
Jens Axboe64565912008-05-28 14:45:33 +0200133 char *buf;
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200134
Li Zefan18cea452009-03-27 10:21:54 +0800135 if (unlikely(bt->trace_state != Blktrace_running &&
136 !blk_tracer_enabled))
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200137 return;
138
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200139 local_irq_save(flags);
Jens Axboe64565912008-05-28 14:45:33 +0200140 buf = per_cpu_ptr(bt->msg_data, smp_processor_id());
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200141 va_start(args, fmt);
Jens Axboe64565912008-05-28 14:45:33 +0200142 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200143 va_end(args);
144
Jens Axboe64565912008-05-28 14:45:33 +0200145 trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200146 local_irq_restore(flags);
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200147}
148EXPORT_SYMBOL_GPL(__trace_note_message);
149
Jens Axboe2056a782006-03-23 20:00:26 +0100150static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
151 pid_t pid)
152{
153 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
154 return 1;
Shawn Dud0deef52009-04-14 13:58:56 +0800155 if (sector && (sector < bt->start_lba || sector > bt->end_lba))
Jens Axboe2056a782006-03-23 20:00:26 +0100156 return 1;
157 if (bt->pid && pid != bt->pid)
158 return 1;
159
160 return 0;
161}
162
163/*
164 * Data direction bit lookup
165 */
Li Zefane4955c92009-03-24 16:04:37 +0800166static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ),
167 BLK_TC_ACT(BLK_TC_WRITE) };
Jens Axboe2056a782006-03-23 20:00:26 +0100168
David Woodhouse35ba8f72008-08-10 12:33:00 +0100169/* The ilog2() calls fall out because they're constant */
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200170#define MASK_TC_BIT(rw, __name) ((rw & (1 << BIO_RW_ ## __name)) << \
171 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name))
Jens Axboe2056a782006-03-23 20:00:26 +0100172
173/*
174 * The worker for the various blk_add_trace*() types. Fills out a
175 * blk_io_trace structure and places it in a per-cpu subbuffer.
176 */
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100177static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
Jens Axboe2056a782006-03-23 20:00:26 +0100178 int rw, u32 what, int error, int pdu_len, void *pdu_data)
179{
180 struct task_struct *tsk = current;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200181 struct ring_buffer_event *event = NULL;
Jens Axboe2056a782006-03-23 20:00:26 +0100182 struct blk_io_trace *t;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200183 unsigned long flags = 0;
Jens Axboe2056a782006-03-23 20:00:26 +0100184 unsigned long *sequence;
185 pid_t pid;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200186 int cpu, pc = 0;
Li Zefan18cea452009-03-27 10:21:54 +0800187 bool blk_tracer = blk_tracer_enabled;
Jens Axboe2056a782006-03-23 20:00:26 +0100188
Li Zefan18cea452009-03-27 10:21:54 +0800189 if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer))
Jens Axboe2056a782006-03-23 20:00:26 +0100190 return;
191
192 what |= ddir_act[rw & WRITE];
David Woodhouse35ba8f72008-08-10 12:33:00 +0100193 what |= MASK_TC_BIT(rw, BARRIER);
Ingo Molnar72c26c92009-02-19 09:00:35 +0100194 what |= MASK_TC_BIT(rw, SYNCIO);
David Woodhouse35ba8f72008-08-10 12:33:00 +0100195 what |= MASK_TC_BIT(rw, AHEAD);
196 what |= MASK_TC_BIT(rw, META);
197 what |= MASK_TC_BIT(rw, DISCARD);
Jens Axboe2056a782006-03-23 20:00:26 +0100198
199 pid = tsk->pid;
Shawn Dud0deef52009-04-14 13:58:56 +0800200 if (act_log_check(bt, what, sector, pid))
Jens Axboe2056a782006-03-23 20:00:26 +0100201 return;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200202 cpu = raw_smp_processor_id();
203
Li Zefan18cea452009-03-27 10:21:54 +0800204 if (blk_tracer) {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200205 tracing_record_cmdline(current);
206
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200207 pc = preempt_count();
208 event = trace_buffer_lock_reserve(blk_tr, TRACE_BLK,
209 sizeof(*t) + pdu_len,
210 0, pc);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200211 if (!event)
212 return;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200213 t = ring_buffer_event_data(event);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200214 goto record_it;
215 }
Jens Axboe2056a782006-03-23 20:00:26 +0100216
217 /*
218 * A word about the locking here - we disable interrupts to reserve
219 * some space in the relay per-cpu buffer, to prevent an irq
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200220 * from coming in and stepping on our toes.
Jens Axboe2056a782006-03-23 20:00:26 +0100221 */
222 local_irq_save(flags);
223
224 if (unlikely(tsk->btrace_seq != blktrace_seq))
225 trace_note_tsk(bt, tsk);
226
227 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
228 if (t) {
Jens Axboe2056a782006-03-23 20:00:26 +0100229 sequence = per_cpu_ptr(bt->sequence, cpu);
230
231 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
232 t->sequence = ++(*sequence);
Ingo Molnar2997c8c2008-01-11 13:35:54 +0100233 t->time = ktime_to_ns(ktime_get());
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200234record_it:
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -0200235 /*
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200236 * These two are not needed in ftrace as they are in the
237 * generic trace_entry, filled by tracing_generic_entry_update,
238 * but for the trace_event->bin() synthesizer benefit we do it
239 * here too.
240 */
241 t->cpu = cpu;
242 t->pid = pid;
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -0200243
Jens Axboe2056a782006-03-23 20:00:26 +0100244 t->sector = sector;
245 t->bytes = bytes;
246 t->action = what;
Jens Axboe2056a782006-03-23 20:00:26 +0100247 t->device = bt->dev;
Jens Axboe2056a782006-03-23 20:00:26 +0100248 t->error = error;
249 t->pdu_len = pdu_len;
250
251 if (pdu_len)
252 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200253
Li Zefan18cea452009-03-27 10:21:54 +0800254 if (blk_tracer) {
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200255 trace_buffer_unlock_commit(blk_tr, event, 0, pc);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200256 return;
257 }
Jens Axboe2056a782006-03-23 20:00:26 +0100258 }
259
260 local_irq_restore(flags);
261}
262
Jens Axboe2056a782006-03-23 20:00:26 +0100263static struct dentry *blk_tree_root;
Jens Axboe11a57152008-01-11 13:37:01 +0100264static DEFINE_MUTEX(blk_tree_mutex);
Jens Axboe2056a782006-03-23 20:00:26 +0100265
Li Zefanad5dd542009-03-27 10:20:24 +0800266static void blk_trace_free(struct blk_trace *bt)
Jens Axboe2056a782006-03-23 20:00:26 +0100267{
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200268 debugfs_remove(bt->msg_file);
Jens Axboe2056a782006-03-23 20:00:26 +0100269 debugfs_remove(bt->dropped_file);
Stefan Rasplfd51d252009-05-19 09:59:08 +0200270 debugfs_remove(bt->dir);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100271 relay_close(bt->rchan);
Jens Axboe2056a782006-03-23 20:00:26 +0100272 free_percpu(bt->sequence);
Jens Axboe64565912008-05-28 14:45:33 +0200273 free_percpu(bt->msg_data);
Jens Axboe2056a782006-03-23 20:00:26 +0100274 kfree(bt);
Li Zefanad5dd542009-03-27 10:20:24 +0800275}
276
277static void blk_trace_cleanup(struct blk_trace *bt)
278{
279 blk_trace_free(bt);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100280 if (atomic_dec_and_test(&blk_probes_ref))
281 blk_unregister_tracepoints();
Jens Axboe2056a782006-03-23 20:00:26 +0100282}
283
Christof Schmitt6da127a2008-01-11 10:09:43 +0100284int blk_trace_remove(struct request_queue *q)
Jens Axboe2056a782006-03-23 20:00:26 +0100285{
286 struct blk_trace *bt;
287
288 bt = xchg(&q->blk_trace, NULL);
289 if (!bt)
290 return -EINVAL;
291
Li Zefan55547202009-03-25 17:21:26 +0800292 if (bt->trace_state != Blktrace_running)
Jens Axboe2056a782006-03-23 20:00:26 +0100293 blk_trace_cleanup(bt);
294
295 return 0;
296}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100297EXPORT_SYMBOL_GPL(blk_trace_remove);
Jens Axboe2056a782006-03-23 20:00:26 +0100298
299static int blk_dropped_open(struct inode *inode, struct file *filp)
300{
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700301 filp->private_data = inode->i_private;
Jens Axboe2056a782006-03-23 20:00:26 +0100302
303 return 0;
304}
305
306static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
307 size_t count, loff_t *ppos)
308{
309 struct blk_trace *bt = filp->private_data;
310 char buf[16];
311
312 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
313
314 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
315}
316
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800317static const struct file_operations blk_dropped_fops = {
Jens Axboe2056a782006-03-23 20:00:26 +0100318 .owner = THIS_MODULE,
319 .open = blk_dropped_open,
320 .read = blk_dropped_read,
321};
322
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200323static int blk_msg_open(struct inode *inode, struct file *filp)
324{
325 filp->private_data = inode->i_private;
326
327 return 0;
328}
329
330static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
331 size_t count, loff_t *ppos)
332{
333 char *msg;
334 struct blk_trace *bt;
335
Li Zefan7635b032009-04-03 15:31:34 +0800336 if (count >= BLK_TN_MAX_MSG)
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200337 return -EINVAL;
338
Carl Henrik Lundea4b3ada2009-04-03 14:27:15 +0200339 msg = kmalloc(count + 1, GFP_KERNEL);
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200340 if (msg == NULL)
341 return -ENOMEM;
342
343 if (copy_from_user(msg, buffer, count)) {
344 kfree(msg);
345 return -EFAULT;
346 }
347
Carl Henrik Lundea4b3ada2009-04-03 14:27:15 +0200348 msg[count] = '\0';
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200349 bt = filp->private_data;
350 __trace_note_message(bt, "%s", msg);
351 kfree(msg);
352
353 return count;
354}
355
356static const struct file_operations blk_msg_fops = {
357 .owner = THIS_MODULE,
358 .open = blk_msg_open,
359 .write = blk_msg_write,
360};
361
Jens Axboe2056a782006-03-23 20:00:26 +0100362/*
363 * Keep track of how many times we encountered a full subbuffer, to aid
364 * the user space app in telling how many lost events there were.
365 */
366static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
367 void *prev_subbuf, size_t prev_padding)
368{
369 struct blk_trace *bt;
370
371 if (!relay_buf_full(buf))
372 return 1;
373
374 bt = buf->chan->private_data;
375 atomic_inc(&bt->dropped);
376 return 0;
377}
378
379static int blk_remove_buf_file_callback(struct dentry *dentry)
380{
Jens Axboef48fc4d2009-01-05 10:17:25 +0100381 struct dentry *parent = dentry->d_parent;
Jens Axboe2056a782006-03-23 20:00:26 +0100382 debugfs_remove(dentry);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100383
384 /*
385 * this will fail for all but the last file, but that is ok. what we
386 * care about is the top level buts->name directory going away, when
387 * the last trace file is gone. Then we don't have to rmdir() that
388 * manually on trace stop, so it nicely solves the issue with
389 * force killing of running traces.
390 */
391
392 debugfs_remove(parent);
Jens Axboe2056a782006-03-23 20:00:26 +0100393 return 0;
394}
395
396static struct dentry *blk_create_buf_file_callback(const char *filename,
397 struct dentry *parent,
398 int mode,
399 struct rchan_buf *buf,
400 int *is_global)
401{
402 return debugfs_create_file(filename, mode, parent, buf,
403 &relay_file_operations);
404}
405
406static struct rchan_callbacks blk_relay_callbacks = {
407 .subbuf_start = blk_subbuf_start_callback,
408 .create_buf_file = blk_create_buf_file_callback,
409 .remove_buf_file = blk_remove_buf_file_callback,
410};
411
Li Zefan9908c302009-04-14 13:59:34 +0800412static void blk_trace_setup_lba(struct blk_trace *bt,
413 struct block_device *bdev)
414{
415 struct hd_struct *part = NULL;
416
417 if (bdev)
418 part = bdev->bd_part;
419
420 if (part) {
421 bt->start_lba = part->start_sect;
422 bt->end_lba = part->start_sect + part->nr_sects;
423 } else {
424 bt->start_lba = 0;
425 bt->end_lba = -1ULL;
426 }
427}
428
Jens Axboe2056a782006-03-23 20:00:26 +0100429/*
430 * Setup everything required to start tracing
431 */
Christof Schmitt6da127a2008-01-11 10:09:43 +0100432int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
Shawn Dud0deef52009-04-14 13:58:56 +0800433 struct block_device *bdev,
434 struct blk_user_trace_setup *buts)
Jens Axboe2056a782006-03-23 20:00:26 +0100435{
Jens Axboe2056a782006-03-23 20:00:26 +0100436 struct blk_trace *old_bt, *bt = NULL;
437 struct dentry *dir = NULL;
Jens Axboe2056a782006-03-23 20:00:26 +0100438 int ret, i;
439
Arnd Bergmann171044d42007-10-09 13:23:53 +0200440 if (!buts->buf_size || !buts->buf_nr)
Jens Axboe2056a782006-03-23 20:00:26 +0100441 return -EINVAL;
442
Jens Axboe0497b342008-10-01 16:16:25 +0200443 strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
444 buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
Jens Axboe2056a782006-03-23 20:00:26 +0100445
446 /*
447 * some device names have larger paths - convert the slashes
448 * to underscores for this to work as expected
449 */
Arnd Bergmann171044d42007-10-09 13:23:53 +0200450 for (i = 0; i < strlen(buts->name); i++)
451 if (buts->name[i] == '/')
452 buts->name[i] = '_';
Jens Axboe2056a782006-03-23 20:00:26 +0100453
Jens Axboe2056a782006-03-23 20:00:26 +0100454 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
455 if (!bt)
Li Zefanad5dd542009-03-27 10:20:24 +0800456 return -ENOMEM;
Jens Axboe2056a782006-03-23 20:00:26 +0100457
Li Zefanad5dd542009-03-27 10:20:24 +0800458 ret = -ENOMEM;
Jens Axboe2056a782006-03-23 20:00:26 +0100459 bt->sequence = alloc_percpu(unsigned long);
460 if (!bt->sequence)
461 goto err;
462
Ingo Molnarf0ef0392009-03-06 16:44:14 +0100463 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
Jens Axboe64565912008-05-28 14:45:33 +0200464 if (!bt->msg_data)
465 goto err;
466
Jens Axboe2056a782006-03-23 20:00:26 +0100467 ret = -ENOENT;
Jens Axboef48fc4d2009-01-05 10:17:25 +0100468
Li Zefanb5230b52009-03-25 17:19:33 +0800469 mutex_lock(&blk_tree_mutex);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100470 if (!blk_tree_root) {
471 blk_tree_root = debugfs_create_dir("block", NULL);
Li Zefanb5230b52009-03-25 17:19:33 +0800472 if (!blk_tree_root) {
473 mutex_unlock(&blk_tree_mutex);
Li Zefan1a176622009-03-20 09:47:30 +0800474 goto err;
Li Zefanb5230b52009-03-25 17:19:33 +0800475 }
Jens Axboef48fc4d2009-01-05 10:17:25 +0100476 }
Li Zefanb5230b52009-03-25 17:19:33 +0800477 mutex_unlock(&blk_tree_mutex);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100478
479 dir = debugfs_create_dir(buts->name, blk_tree_root);
480
Jens Axboe2056a782006-03-23 20:00:26 +0100481 if (!dir)
482 goto err;
483
484 bt->dir = dir;
Christof Schmitt6da127a2008-01-11 10:09:43 +0100485 bt->dev = dev;
Jens Axboe2056a782006-03-23 20:00:26 +0100486 atomic_set(&bt->dropped, 0);
487
488 ret = -EIO;
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200489 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt,
490 &blk_dropped_fops);
Jens Axboe2056a782006-03-23 20:00:26 +0100491 if (!bt->dropped_file)
492 goto err;
493
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200494 bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
495 if (!bt->msg_file)
496 goto err;
497
Arnd Bergmann171044d42007-10-09 13:23:53 +0200498 bt->rchan = relay_open("trace", dir, buts->buf_size,
499 buts->buf_nr, &blk_relay_callbacks, bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100500 if (!bt->rchan)
501 goto err;
Jens Axboe2056a782006-03-23 20:00:26 +0100502
Arnd Bergmann171044d42007-10-09 13:23:53 +0200503 bt->act_mask = buts->act_mask;
Jens Axboe2056a782006-03-23 20:00:26 +0100504 if (!bt->act_mask)
505 bt->act_mask = (u16) -1;
506
Li Zefan9908c302009-04-14 13:59:34 +0800507 blk_trace_setup_lba(bt, bdev);
Jens Axboe2056a782006-03-23 20:00:26 +0100508
Shawn Dud0deef52009-04-14 13:58:56 +0800509 /* overwrite with user settings */
510 if (buts->start_lba)
511 bt->start_lba = buts->start_lba;
512 if (buts->end_lba)
513 bt->end_lba = buts->end_lba;
514
Arnd Bergmann171044d42007-10-09 13:23:53 +0200515 bt->pid = buts->pid;
Jens Axboe2056a782006-03-23 20:00:26 +0100516 bt->trace_state = Blktrace_setup;
517
518 ret = -EBUSY;
519 old_bt = xchg(&q->blk_trace, bt);
520 if (old_bt) {
521 (void) xchg(&q->blk_trace, old_bt);
522 goto err;
523 }
524
Li Zefan17ba97e2009-03-27 10:20:09 +0800525 if (atomic_inc_return(&blk_probes_ref) == 1)
Li Zefancbe28292009-03-20 09:48:47 +0800526 blk_register_tracepoints();
527
Jens Axboe2056a782006-03-23 20:00:26 +0100528 return 0;
529err:
Li Zefanad5dd542009-03-27 10:20:24 +0800530 blk_trace_free(bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100531 return ret;
532}
533
Christof Schmitt6da127a2008-01-11 10:09:43 +0100534int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
Shawn Dud0deef52009-04-14 13:58:56 +0800535 struct block_device *bdev,
Christof Schmitt6da127a2008-01-11 10:09:43 +0100536 char __user *arg)
Arnd Bergmann171044d42007-10-09 13:23:53 +0200537{
538 struct blk_user_trace_setup buts;
539 int ret;
540
541 ret = copy_from_user(&buts, arg, sizeof(buts));
542 if (ret)
543 return -EFAULT;
544
Shawn Dud0deef52009-04-14 13:58:56 +0800545 ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200546 if (ret)
547 return ret;
548
549 if (copy_to_user(arg, &buts, sizeof(buts)))
550 return -EFAULT;
551
552 return 0;
553}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100554EXPORT_SYMBOL_GPL(blk_trace_setup);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200555
Christof Schmitt6da127a2008-01-11 10:09:43 +0100556int blk_trace_startstop(struct request_queue *q, int start)
Jens Axboe2056a782006-03-23 20:00:26 +0100557{
Jens Axboe2056a782006-03-23 20:00:26 +0100558 int ret;
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200559 struct blk_trace *bt = q->blk_trace;
Jens Axboe2056a782006-03-23 20:00:26 +0100560
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200561 if (bt == NULL)
Jens Axboe2056a782006-03-23 20:00:26 +0100562 return -EINVAL;
563
564 /*
565 * For starting a trace, we can transition from a setup or stopped
566 * trace. For stopping a trace, the state must be running
567 */
568 ret = -EINVAL;
569 if (start) {
570 if (bt->trace_state == Blktrace_setup ||
571 bt->trace_state == Blktrace_stopped) {
572 blktrace_seq++;
573 smp_mb();
574 bt->trace_state = Blktrace_running;
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100575
576 trace_note_time(bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100577 ret = 0;
578 }
579 } else {
580 if (bt->trace_state == Blktrace_running) {
581 bt->trace_state = Blktrace_stopped;
582 relay_flush(bt->rchan);
583 ret = 0;
584 }
585 }
586
587 return ret;
588}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100589EXPORT_SYMBOL_GPL(blk_trace_startstop);
Jens Axboe2056a782006-03-23 20:00:26 +0100590
591/**
592 * blk_trace_ioctl: - handle the ioctls associated with tracing
593 * @bdev: the block device
Steven Rostedtef180122009-03-10 14:10:56 -0400594 * @cmd: the ioctl cmd
Jens Axboe2056a782006-03-23 20:00:26 +0100595 * @arg: the argument data, if any
596 *
597 **/
598int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
599{
Jens Axboe165125e2007-07-24 09:28:11 +0200600 struct request_queue *q;
Jens Axboe2056a782006-03-23 20:00:26 +0100601 int ret, start = 0;
Christof Schmitt6da127a2008-01-11 10:09:43 +0100602 char b[BDEVNAME_SIZE];
Jens Axboe2056a782006-03-23 20:00:26 +0100603
604 q = bdev_get_queue(bdev);
605 if (!q)
606 return -ENXIO;
607
608 mutex_lock(&bdev->bd_mutex);
609
610 switch (cmd) {
611 case BLKTRACESETUP:
Jean Delvaref36f21e2008-05-12 14:02:33 -0700612 bdevname(bdev, b);
Shawn Dud0deef52009-04-14 13:58:56 +0800613 ret = blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
Jens Axboe2056a782006-03-23 20:00:26 +0100614 break;
615 case BLKTRACESTART:
616 start = 1;
617 case BLKTRACESTOP:
618 ret = blk_trace_startstop(q, start);
619 break;
620 case BLKTRACETEARDOWN:
621 ret = blk_trace_remove(q);
622 break;
623 default:
624 ret = -ENOTTY;
625 break;
626 }
627
628 mutex_unlock(&bdev->bd_mutex);
629 return ret;
630}
631
632/**
633 * blk_trace_shutdown: - stop and cleanup trace structures
634 * @q: the request queue associated with the device
635 *
636 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200637void blk_trace_shutdown(struct request_queue *q)
Jens Axboe2056a782006-03-23 20:00:26 +0100638{
Alexey Dobriyan6c5c9342006-09-29 01:59:40 -0700639 if (q->blk_trace) {
640 blk_trace_startstop(q, 0);
641 blk_trace_remove(q);
642 }
Jens Axboe2056a782006-03-23 20:00:26 +0100643}
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100644
645/*
646 * blktrace probes
647 */
648
649/**
650 * blk_add_trace_rq - Add a trace for a request oriented action
651 * @q: queue the io is for
652 * @rq: the source request
653 * @what: the action
654 *
655 * Description:
656 * Records an action against a request. Will log the bio offset + size.
657 *
658 **/
659static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
660 u32 what)
661{
662 struct blk_trace *bt = q->blk_trace;
663 int rw = rq->cmd_flags & 0x03;
664
665 if (likely(!bt))
666 return;
667
668 if (blk_discard_rq(rq))
669 rw |= (1 << BIO_RW_DISCARD);
670
671 if (blk_pc_request(rq)) {
672 what |= BLK_TC_ACT(BLK_TC_PC);
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900673 __blk_add_trace(bt, 0, blk_rq_bytes(rq), rw,
674 what, rq->errors, rq->cmd_len, rq->cmd);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100675 } else {
676 what |= BLK_TC_ACT(BLK_TC_FS);
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900677 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), rw,
678 what, rq->errors, 0, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100679 }
680}
681
682static void blk_add_trace_rq_abort(struct request_queue *q, struct request *rq)
683{
684 blk_add_trace_rq(q, rq, BLK_TA_ABORT);
685}
686
687static void blk_add_trace_rq_insert(struct request_queue *q, struct request *rq)
688{
689 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
690}
691
692static void blk_add_trace_rq_issue(struct request_queue *q, struct request *rq)
693{
694 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
695}
696
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200697static void blk_add_trace_rq_requeue(struct request_queue *q,
698 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
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200703static void blk_add_trace_rq_complete(struct request_queue *q,
704 struct request *rq)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100705{
706 blk_add_trace_rq(q, rq, BLK_TA_COMPLETE);
707}
708
709/**
710 * blk_add_trace_bio - Add a trace for a bio oriented action
711 * @q: queue the io is for
712 * @bio: the source bio
713 * @what: the action
714 *
715 * Description:
716 * Records an action against a bio. Will log the bio offset + size.
717 *
718 **/
719static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
720 u32 what)
721{
722 struct blk_trace *bt = q->blk_trace;
723
724 if (likely(!bt))
725 return;
726
727 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what,
728 !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
729}
730
731static void blk_add_trace_bio_bounce(struct request_queue *q, struct bio *bio)
732{
733 blk_add_trace_bio(q, bio, BLK_TA_BOUNCE);
734}
735
736static void blk_add_trace_bio_complete(struct request_queue *q, struct bio *bio)
737{
738 blk_add_trace_bio(q, bio, BLK_TA_COMPLETE);
739}
740
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200741static void blk_add_trace_bio_backmerge(struct request_queue *q,
742 struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100743{
744 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
745}
746
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200747static void blk_add_trace_bio_frontmerge(struct request_queue *q,
748 struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100749{
750 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
751}
752
753static void blk_add_trace_bio_queue(struct request_queue *q, struct bio *bio)
754{
755 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
756}
757
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200758static void blk_add_trace_getrq(struct request_queue *q,
759 struct bio *bio, int rw)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100760{
761 if (bio)
762 blk_add_trace_bio(q, bio, BLK_TA_GETRQ);
763 else {
764 struct blk_trace *bt = q->blk_trace;
765
766 if (bt)
767 __blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL);
768 }
769}
770
771
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200772static void blk_add_trace_sleeprq(struct request_queue *q,
773 struct bio *bio, int rw)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100774{
775 if (bio)
776 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ);
777 else {
778 struct blk_trace *bt = q->blk_trace;
779
780 if (bt)
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200781 __blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ,
782 0, 0, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100783 }
784}
785
786static void blk_add_trace_plug(struct request_queue *q)
787{
788 struct blk_trace *bt = q->blk_trace;
789
790 if (bt)
791 __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
792}
793
794static void blk_add_trace_unplug_io(struct request_queue *q)
795{
796 struct blk_trace *bt = q->blk_trace;
797
798 if (bt) {
799 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
800 __be64 rpdu = cpu_to_be64(pdu);
801
802 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_IO, 0,
803 sizeof(rpdu), &rpdu);
804 }
805}
806
807static void blk_add_trace_unplug_timer(struct request_queue *q)
808{
809 struct blk_trace *bt = q->blk_trace;
810
811 if (bt) {
812 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
813 __be64 rpdu = cpu_to_be64(pdu);
814
815 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_TIMER, 0,
816 sizeof(rpdu), &rpdu);
817 }
818}
819
820static void blk_add_trace_split(struct request_queue *q, struct bio *bio,
821 unsigned int pdu)
822{
823 struct blk_trace *bt = q->blk_trace;
824
825 if (bt) {
826 __be64 rpdu = cpu_to_be64(pdu);
827
828 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
829 BLK_TA_SPLIT, !bio_flagged(bio, BIO_UPTODATE),
830 sizeof(rpdu), &rpdu);
831 }
832}
833
834/**
835 * blk_add_trace_remap - Add a trace for a remap operation
836 * @q: queue the io is for
837 * @bio: the source bio
838 * @dev: target device
Alan D. Brunellea42aaa32009-05-04 16:27:26 -0400839 * @from: source sector
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100840 *
841 * Description:
842 * Device mapper or raid target sometimes need to split a bio because
843 * it spans a stripe (or similar). Add a trace for that action.
844 *
845 **/
846static void blk_add_trace_remap(struct request_queue *q, struct bio *bio,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -0400847 dev_t dev, sector_t from)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100848{
849 struct blk_trace *bt = q->blk_trace;
850 struct blk_io_trace_remap r;
851
852 if (likely(!bt))
853 return;
854
Alan D. Brunellea42aaa32009-05-04 16:27:26 -0400855 r.device_from = cpu_to_be32(dev);
856 r.device_to = cpu_to_be32(bio->bi_bdev->bd_dev);
857 r.sector_from = cpu_to_be64(from);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100858
Alan D. Brunelle22a7c312009-05-04 16:35:08 -0400859 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
860 BLK_TA_REMAP, !bio_flagged(bio, BIO_UPTODATE),
861 sizeof(r), &r);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100862}
863
864/**
865 * blk_add_driver_data - Add binary message with driver-specific data
866 * @q: queue the io is for
867 * @rq: io request
868 * @data: driver-specific data
869 * @len: length of driver-specific data
870 *
871 * Description:
872 * Some drivers might want to write driver-specific data per request.
873 *
874 **/
875void blk_add_driver_data(struct request_queue *q,
876 struct request *rq,
877 void *data, size_t len)
878{
879 struct blk_trace *bt = q->blk_trace;
880
881 if (likely(!bt))
882 return;
883
884 if (blk_pc_request(rq))
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900885 __blk_add_trace(bt, 0, blk_rq_bytes(rq), 0,
886 BLK_TA_DRV_DATA, rq->errors, len, data);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100887 else
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900888 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), 0,
889 BLK_TA_DRV_DATA, rq->errors, len, data);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100890}
891EXPORT_SYMBOL_GPL(blk_add_driver_data);
892
Li Zefan3c289ba2009-03-20 09:48:26 +0800893static void blk_register_tracepoints(void)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100894{
895 int ret;
896
897 ret = register_trace_block_rq_abort(blk_add_trace_rq_abort);
898 WARN_ON(ret);
899 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert);
900 WARN_ON(ret);
901 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue);
902 WARN_ON(ret);
903 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue);
904 WARN_ON(ret);
905 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete);
906 WARN_ON(ret);
907 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce);
908 WARN_ON(ret);
909 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete);
910 WARN_ON(ret);
911 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
912 WARN_ON(ret);
913 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
914 WARN_ON(ret);
915 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue);
916 WARN_ON(ret);
917 ret = register_trace_block_getrq(blk_add_trace_getrq);
918 WARN_ON(ret);
919 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq);
920 WARN_ON(ret);
921 ret = register_trace_block_plug(blk_add_trace_plug);
922 WARN_ON(ret);
923 ret = register_trace_block_unplug_timer(blk_add_trace_unplug_timer);
924 WARN_ON(ret);
925 ret = register_trace_block_unplug_io(blk_add_trace_unplug_io);
926 WARN_ON(ret);
927 ret = register_trace_block_split(blk_add_trace_split);
928 WARN_ON(ret);
929 ret = register_trace_block_remap(blk_add_trace_remap);
930 WARN_ON(ret);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100931}
932
933static void blk_unregister_tracepoints(void)
934{
935 unregister_trace_block_remap(blk_add_trace_remap);
936 unregister_trace_block_split(blk_add_trace_split);
937 unregister_trace_block_unplug_io(blk_add_trace_unplug_io);
938 unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer);
939 unregister_trace_block_plug(blk_add_trace_plug);
940 unregister_trace_block_sleeprq(blk_add_trace_sleeprq);
941 unregister_trace_block_getrq(blk_add_trace_getrq);
942 unregister_trace_block_bio_queue(blk_add_trace_bio_queue);
943 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
944 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
945 unregister_trace_block_bio_complete(blk_add_trace_bio_complete);
946 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce);
947 unregister_trace_block_rq_complete(blk_add_trace_rq_complete);
948 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue);
949 unregister_trace_block_rq_issue(blk_add_trace_rq_issue);
950 unregister_trace_block_rq_insert(blk_add_trace_rq_insert);
951 unregister_trace_block_rq_abort(blk_add_trace_rq_abort);
952
953 tracepoint_synchronize_unregister();
954}
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200955
956/*
957 * struct blk_io_tracer formatting routines
958 */
959
960static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
961{
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200962 int i = 0;
Li Zefan65796342009-03-24 16:05:06 +0800963 int tc = t->action >> BLK_TC_SHIFT;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200964
Li Zefan18cea452009-03-27 10:21:54 +0800965 if (t->action == BLK_TN_MESSAGE) {
966 rwbs[i++] = 'N';
967 goto out;
968 }
969
Li Zefan65796342009-03-24 16:05:06 +0800970 if (tc & BLK_TC_DISCARD)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200971 rwbs[i++] = 'D';
Li Zefan65796342009-03-24 16:05:06 +0800972 else if (tc & BLK_TC_WRITE)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200973 rwbs[i++] = 'W';
974 else if (t->bytes)
975 rwbs[i++] = 'R';
976 else
977 rwbs[i++] = 'N';
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200978
Li Zefan65796342009-03-24 16:05:06 +0800979 if (tc & BLK_TC_AHEAD)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200980 rwbs[i++] = 'A';
Li Zefan65796342009-03-24 16:05:06 +0800981 if (tc & BLK_TC_BARRIER)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200982 rwbs[i++] = 'B';
Li Zefan65796342009-03-24 16:05:06 +0800983 if (tc & BLK_TC_SYNC)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200984 rwbs[i++] = 'S';
Li Zefan65796342009-03-24 16:05:06 +0800985 if (tc & BLK_TC_META)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200986 rwbs[i++] = 'M';
Li Zefan18cea452009-03-27 10:21:54 +0800987out:
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200988 rwbs[i] = '\0';
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200989}
990
991static inline
992const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
993{
994 return (const struct blk_io_trace *)ent;
995}
996
997static inline const void *pdu_start(const struct trace_entry *ent)
998{
999 return te_blk_io_trace(ent) + 1;
1000}
1001
Li Zefan66de7792009-04-01 16:19:19 +08001002static inline u32 t_action(const struct trace_entry *ent)
1003{
1004 return te_blk_io_trace(ent)->action;
1005}
1006
1007static inline u32 t_bytes(const struct trace_entry *ent)
1008{
1009 return te_blk_io_trace(ent)->bytes;
1010}
1011
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001012static inline u32 t_sec(const struct trace_entry *ent)
1013{
1014 return te_blk_io_trace(ent)->bytes >> 9;
1015}
1016
1017static inline unsigned long long t_sector(const struct trace_entry *ent)
1018{
1019 return te_blk_io_trace(ent)->sector;
1020}
1021
1022static inline __u16 t_error(const struct trace_entry *ent)
1023{
Li Zefane0dc81b2009-03-24 16:05:51 +08001024 return te_blk_io_trace(ent)->error;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001025}
1026
1027static __u64 get_pdu_int(const struct trace_entry *ent)
1028{
1029 const __u64 *val = pdu_start(ent);
1030 return be64_to_cpu(*val);
1031}
1032
1033static void get_pdu_remap(const struct trace_entry *ent,
1034 struct blk_io_trace_remap *r)
1035{
1036 const struct blk_io_trace_remap *__r = pdu_start(ent);
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001037 __u64 sector_from = __r->sector_from;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001038
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001039 r->device_from = be32_to_cpu(__r->device_from);
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001040 r->device_to = be32_to_cpu(__r->device_to);
1041 r->sector_from = be64_to_cpu(sector_from);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001042}
1043
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001044typedef int (blk_log_action_t) (struct trace_iterator *iter, const char *act);
1045
1046static int blk_log_action_classic(struct trace_iterator *iter, const char *act)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001047{
1048 char rwbs[6];
Li Zefan35ac51b2009-03-27 10:19:46 +08001049 unsigned long long ts = iter->ts;
1050 unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001051 unsigned secs = (unsigned long)ts;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001052 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001053
1054 fill_rwbs(rwbs, t);
1055
1056 return trace_seq_printf(&iter->seq,
Li Zefan35ac51b2009-03-27 10:19:46 +08001057 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001058 MAJOR(t->device), MINOR(t->device), iter->cpu,
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001059 secs, nsec_rem, iter->ent->pid, act, rwbs);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001060}
1061
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001062static int blk_log_action(struct trace_iterator *iter, const char *act)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001063{
1064 char rwbs[6];
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001065 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1066
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001067 fill_rwbs(rwbs, t);
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001068 return trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001069 MAJOR(t->device), MINOR(t->device), act, rwbs);
1070}
1071
Li Zefan66de7792009-04-01 16:19:19 +08001072static int blk_log_dump_pdu(struct trace_seq *s, const struct trace_entry *ent)
1073{
Li Zefan049862572009-05-11 14:33:23 +08001074 const unsigned char *pdu_buf;
Li Zefan66de7792009-04-01 16:19:19 +08001075 int pdu_len;
1076 int i, end, ret;
1077
1078 pdu_buf = pdu_start(ent);
1079 pdu_len = te_blk_io_trace(ent)->pdu_len;
1080
1081 if (!pdu_len)
1082 return 1;
1083
1084 /* find the last zero that needs to be printed */
1085 for (end = pdu_len - 1; end >= 0; end--)
1086 if (pdu_buf[end])
1087 break;
1088 end++;
1089
1090 if (!trace_seq_putc(s, '('))
1091 return 0;
1092
1093 for (i = 0; i < pdu_len; i++) {
1094
1095 ret = trace_seq_printf(s, "%s%02x",
1096 i == 0 ? "" : " ", pdu_buf[i]);
1097 if (!ret)
1098 return ret;
1099
1100 /*
1101 * stop when the rest is just zeroes and indicate so
1102 * with a ".." appended
1103 */
1104 if (i == end && end != pdu_len - 1)
1105 return trace_seq_puts(s, " ..) ");
1106 }
1107
1108 return trace_seq_puts(s, ") ");
1109}
1110
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001111static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1112{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001113 char cmd[TASK_COMM_LEN];
1114
1115 trace_find_cmdline(ent->pid, cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001116
Li Zefan66de7792009-04-01 16:19:19 +08001117 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1118 int ret;
1119
1120 ret = trace_seq_printf(s, "%u ", t_bytes(ent));
1121 if (!ret)
1122 return 0;
1123 ret = blk_log_dump_pdu(s, ent);
1124 if (!ret)
1125 return 0;
1126 return trace_seq_printf(s, "[%s]\n", cmd);
1127 } else {
1128 if (t_sec(ent))
1129 return trace_seq_printf(s, "%llu + %u [%s]\n",
1130 t_sector(ent), t_sec(ent), cmd);
1131 return trace_seq_printf(s, "[%s]\n", cmd);
1132 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001133}
1134
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001135static int blk_log_with_error(struct trace_seq *s,
1136 const struct trace_entry *ent)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001137{
Li Zefan66de7792009-04-01 16:19:19 +08001138 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1139 int ret;
1140
1141 ret = blk_log_dump_pdu(s, ent);
1142 if (ret)
1143 return trace_seq_printf(s, "[%d]\n", t_error(ent));
1144 return 0;
1145 } else {
1146 if (t_sec(ent))
1147 return trace_seq_printf(s, "%llu + %u [%d]\n",
1148 t_sector(ent),
1149 t_sec(ent), t_error(ent));
1150 return trace_seq_printf(s, "%llu [%d]\n",
1151 t_sector(ent), t_error(ent));
1152 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001153}
1154
1155static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1156{
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001157 struct blk_io_trace_remap r = { .device_from = 0, };
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001158
1159 get_pdu_remap(ent, &r);
1160 return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001161 t_sector(ent), t_sec(ent),
1162 MAJOR(r.device_from), MINOR(r.device_from),
1163 (unsigned long long)r.sector_from);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001164}
1165
1166static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1167{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001168 char cmd[TASK_COMM_LEN];
1169
1170 trace_find_cmdline(ent->pid, cmd);
1171
1172 return trace_seq_printf(s, "[%s]\n", cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001173}
1174
1175static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1176{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001177 char cmd[TASK_COMM_LEN];
1178
1179 trace_find_cmdline(ent->pid, cmd);
1180
1181 return trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent));
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001182}
1183
1184static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1185{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001186 char cmd[TASK_COMM_LEN];
1187
1188 trace_find_cmdline(ent->pid, cmd);
1189
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001190 return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
Steven Rostedt4ca530852009-03-16 19:20:15 -04001191 get_pdu_int(ent), cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001192}
1193
Li Zefan18cea452009-03-27 10:21:54 +08001194static int blk_log_msg(struct trace_seq *s, const struct trace_entry *ent)
1195{
1196 int ret;
1197 const struct blk_io_trace *t = te_blk_io_trace(ent);
1198
1199 ret = trace_seq_putmem(s, t + 1, t->pdu_len);
1200 if (ret)
1201 return trace_seq_putc(s, '\n');
1202 return ret;
1203}
1204
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001205/*
1206 * struct tracer operations
1207 */
1208
1209static void blk_tracer_print_header(struct seq_file *m)
1210{
1211 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1212 return;
1213 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1214 "# | | | | | |\n");
1215}
1216
1217static void blk_tracer_start(struct trace_array *tr)
1218{
Li Zefanad5dd542009-03-27 10:20:24 +08001219 blk_tracer_enabled = true;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001220}
1221
1222static int blk_tracer_init(struct trace_array *tr)
1223{
1224 blk_tr = tr;
1225 blk_tracer_start(tr);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001226 return 0;
1227}
1228
1229static void blk_tracer_stop(struct trace_array *tr)
1230{
Li Zefanad5dd542009-03-27 10:20:24 +08001231 blk_tracer_enabled = false;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001232}
1233
1234static void blk_tracer_reset(struct trace_array *tr)
1235{
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001236 blk_tracer_stop(tr);
1237}
1238
Li Zefane4955c92009-03-24 16:04:37 +08001239static const struct {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001240 const char *act[2];
Steven Rostedtef180122009-03-10 14:10:56 -04001241 int (*print)(struct trace_seq *s, const struct trace_entry *ent);
Li Zefane4955c92009-03-24 16:04:37 +08001242} what2act[] = {
Steven Rostedtef180122009-03-10 14:10:56 -04001243 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001244 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1245 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1246 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1247 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1248 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1249 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1250 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1251 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1252 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
1253 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
1254 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1255 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1256 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1257 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1258};
1259
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001260static enum print_line_t print_one_line(struct trace_iterator *iter,
1261 bool classic)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001262{
Arnaldo Carvalho de Melo2c9b2382009-02-02 20:30:12 -02001263 struct trace_seq *s = &iter->seq;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001264 const struct blk_io_trace *t;
1265 u16 what;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001266 int ret;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001267 bool long_act;
1268 blk_log_action_t *log_action;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001269
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001270 t = te_blk_io_trace(iter->ent);
1271 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1272 long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1273 log_action = classic ? &blk_log_action_classic : &blk_log_action;
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001274
Li Zefan18cea452009-03-27 10:21:54 +08001275 if (t->action == BLK_TN_MESSAGE) {
1276 ret = log_action(iter, long_act ? "message" : "m");
1277 if (ret)
1278 ret = blk_log_msg(s, iter->ent);
1279 goto out;
1280 }
1281
Li Zefaneb08f8e2009-03-24 16:05:27 +08001282 if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
Li Zefanb78825d2009-04-01 16:18:53 +08001283 ret = trace_seq_printf(s, "Unknown action %x\n", what);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001284 else {
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001285 ret = log_action(iter, what2act[what].act[long_act]);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001286 if (ret)
Arnaldo Carvalho de Melo2c9b2382009-02-02 20:30:12 -02001287 ret = what2act[what].print(s, iter->ent);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001288 }
Li Zefan18cea452009-03-27 10:21:54 +08001289out:
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001290 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1291}
1292
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001293static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
1294 int flags)
1295{
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001296 return print_one_line(iter, false);
1297}
1298
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001299static int blk_trace_synthesize_old_trace(struct trace_iterator *iter)
1300{
1301 struct trace_seq *s = &iter->seq;
1302 struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1303 const int offset = offsetof(struct blk_io_trace, sector);
1304 struct blk_io_trace old = {
1305 .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
Li Zefan6c051ce2009-03-25 17:18:56 +08001306 .time = iter->ts,
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001307 };
1308
1309 if (!trace_seq_putmem(s, &old, offset))
1310 return 0;
1311 return trace_seq_putmem(s, &t->sector,
1312 sizeof(old) - offset + t->pdu_len);
1313}
1314
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -02001315static enum print_line_t
1316blk_trace_event_print_binary(struct trace_iterator *iter, int flags)
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001317{
1318 return blk_trace_synthesize_old_trace(iter) ?
1319 TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1320}
1321
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001322static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1323{
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001324 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1325 return TRACE_TYPE_UNHANDLED;
1326
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001327 return print_one_line(iter, true);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001328}
1329
Li Zefanf3948f82009-04-15 11:02:56 +08001330static int blk_tracer_set_flag(u32 old_flags, u32 bit, int set)
1331{
1332 /* don't output context-info for blk_classic output */
1333 if (bit == TRACE_BLK_OPT_CLASSIC) {
1334 if (set)
1335 trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
1336 else
1337 trace_flags |= TRACE_ITER_CONTEXT_INFO;
1338 }
1339 return 0;
1340}
1341
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001342static struct tracer blk_tracer __read_mostly = {
1343 .name = "blk",
1344 .init = blk_tracer_init,
1345 .reset = blk_tracer_reset,
1346 .start = blk_tracer_start,
1347 .stop = blk_tracer_stop,
1348 .print_header = blk_tracer_print_header,
1349 .print_line = blk_tracer_print_line,
1350 .flags = &blk_tracer_flags,
Li Zefanf3948f82009-04-15 11:02:56 +08001351 .set_flag = blk_tracer_set_flag,
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001352};
1353
1354static struct trace_event trace_blk_event = {
Steven Rostedtef180122009-03-10 14:10:56 -04001355 .type = TRACE_BLK,
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001356 .trace = blk_trace_event_print,
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001357 .binary = blk_trace_event_print_binary,
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001358};
1359
1360static int __init init_blk_tracer(void)
1361{
1362 if (!register_ftrace_event(&trace_blk_event)) {
1363 pr_warning("Warning: could not register block events\n");
1364 return 1;
1365 }
1366
1367 if (register_tracer(&blk_tracer) != 0) {
1368 pr_warning("Warning: could not register the block tracer\n");
1369 unregister_ftrace_event(&trace_blk_event);
1370 return 1;
1371 }
1372
1373 return 0;
1374}
1375
1376device_initcall(init_blk_tracer);
1377
1378static int blk_trace_remove_queue(struct request_queue *q)
1379{
1380 struct blk_trace *bt;
1381
1382 bt = xchg(&q->blk_trace, NULL);
1383 if (bt == NULL)
1384 return -EINVAL;
1385
Li Zefan17ba97e2009-03-27 10:20:09 +08001386 if (atomic_dec_and_test(&blk_probes_ref))
1387 blk_unregister_tracepoints();
1388
Li Zefanad5dd542009-03-27 10:20:24 +08001389 blk_trace_free(bt);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001390 return 0;
1391}
1392
1393/*
1394 * Setup everything required to start tracing
1395 */
Li Zefan9908c302009-04-14 13:59:34 +08001396static int blk_trace_setup_queue(struct request_queue *q,
1397 struct block_device *bdev)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001398{
1399 struct blk_trace *old_bt, *bt = NULL;
Li Zefan18cea452009-03-27 10:21:54 +08001400 int ret = -ENOMEM;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001401
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001402 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1403 if (!bt)
Li Zefan15152e42009-03-20 09:49:08 +08001404 return -ENOMEM;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001405
Li Zefan18cea452009-03-27 10:21:54 +08001406 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
1407 if (!bt->msg_data)
1408 goto free_bt;
1409
Li Zefan9908c302009-04-14 13:59:34 +08001410 bt->dev = bdev->bd_dev;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001411 bt->act_mask = (u16)-1;
Li Zefan9908c302009-04-14 13:59:34 +08001412
1413 blk_trace_setup_lba(bt, bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001414
1415 old_bt = xchg(&q->blk_trace, bt);
1416 if (old_bt != NULL) {
1417 (void)xchg(&q->blk_trace, old_bt);
Li Zefan18cea452009-03-27 10:21:54 +08001418 ret = -EBUSY;
1419 goto free_bt;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001420 }
Li Zefan15152e42009-03-20 09:49:08 +08001421
Li Zefan17ba97e2009-03-27 10:20:09 +08001422 if (atomic_inc_return(&blk_probes_ref) == 1)
1423 blk_register_tracepoints();
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001424 return 0;
Li Zefan18cea452009-03-27 10:21:54 +08001425
1426free_bt:
1427 blk_trace_free(bt);
1428 return ret;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001429}
1430
1431/*
1432 * sysfs interface to enable and configure tracing
1433 */
1434
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001435static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1436 struct device_attribute *attr,
1437 char *buf);
1438static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1439 struct device_attribute *attr,
1440 const char *buf, size_t count);
1441#define BLK_TRACE_DEVICE_ATTR(_name) \
1442 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1443 sysfs_blk_trace_attr_show, \
1444 sysfs_blk_trace_attr_store)
1445
Li Zefancd649b82009-03-20 11:33:55 +08001446static BLK_TRACE_DEVICE_ATTR(enable);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001447static BLK_TRACE_DEVICE_ATTR(act_mask);
1448static BLK_TRACE_DEVICE_ATTR(pid);
1449static BLK_TRACE_DEVICE_ATTR(start_lba);
1450static BLK_TRACE_DEVICE_ATTR(end_lba);
1451
1452static struct attribute *blk_trace_attrs[] = {
1453 &dev_attr_enable.attr,
1454 &dev_attr_act_mask.attr,
1455 &dev_attr_pid.attr,
1456 &dev_attr_start_lba.attr,
1457 &dev_attr_end_lba.attr,
1458 NULL
1459};
1460
1461struct attribute_group blk_trace_attr_group = {
1462 .name = "trace",
1463 .attrs = blk_trace_attrs,
1464};
1465
Li Zefan09341992009-03-24 17:43:30 +08001466static const struct {
1467 int mask;
1468 const char *str;
1469} mask_maps[] = {
1470 { BLK_TC_READ, "read" },
1471 { BLK_TC_WRITE, "write" },
1472 { BLK_TC_BARRIER, "barrier" },
1473 { BLK_TC_SYNC, "sync" },
1474 { BLK_TC_QUEUE, "queue" },
1475 { BLK_TC_REQUEUE, "requeue" },
1476 { BLK_TC_ISSUE, "issue" },
1477 { BLK_TC_COMPLETE, "complete" },
1478 { BLK_TC_FS, "fs" },
1479 { BLK_TC_PC, "pc" },
1480 { BLK_TC_AHEAD, "ahead" },
1481 { BLK_TC_META, "meta" },
1482 { BLK_TC_DISCARD, "discard" },
1483 { BLK_TC_DRV_DATA, "drv_data" },
1484};
1485
1486static int blk_trace_str2mask(const char *str)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001487{
Li Zefan09341992009-03-24 17:43:30 +08001488 int i;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001489 int mask = 0;
Li Zefan9eb85122009-04-09 11:19:40 +08001490 char *buf, *s, *token;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001491
Li Zefan9eb85122009-04-09 11:19:40 +08001492 buf = kstrdup(str, GFP_KERNEL);
1493 if (buf == NULL)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001494 return -ENOMEM;
Li Zefan9eb85122009-04-09 11:19:40 +08001495 s = strstrip(buf);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001496
1497 while (1) {
Li Zefan09341992009-03-24 17:43:30 +08001498 token = strsep(&s, ",");
1499 if (token == NULL)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001500 break;
1501
Li Zefan09341992009-03-24 17:43:30 +08001502 if (*token == '\0')
1503 continue;
1504
1505 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1506 if (strcasecmp(token, mask_maps[i].str) == 0) {
1507 mask |= mask_maps[i].mask;
1508 break;
1509 }
1510 }
1511 if (i == ARRAY_SIZE(mask_maps)) {
1512 mask = -EINVAL;
1513 break;
1514 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001515 }
Li Zefan9eb85122009-04-09 11:19:40 +08001516 kfree(buf);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001517
1518 return mask;
1519}
1520
Li Zefan09341992009-03-24 17:43:30 +08001521static ssize_t blk_trace_mask2str(char *buf, int mask)
1522{
1523 int i;
1524 char *p = buf;
1525
1526 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1527 if (mask & mask_maps[i].mask) {
1528 p += sprintf(p, "%s%s",
1529 (p == buf) ? "" : ",", mask_maps[i].str);
1530 }
1531 }
1532 *p++ = '\n';
1533
1534 return p - buf;
1535}
1536
Li Zefanb1251302009-03-20 10:34:00 +08001537static struct request_queue *blk_trace_get_queue(struct block_device *bdev)
1538{
1539 if (bdev->bd_disk == NULL)
1540 return NULL;
1541
1542 return bdev_get_queue(bdev);
1543}
1544
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001545static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1546 struct device_attribute *attr,
1547 char *buf)
1548{
1549 struct hd_struct *p = dev_to_part(dev);
1550 struct request_queue *q;
1551 struct block_device *bdev;
1552 ssize_t ret = -ENXIO;
1553
1554 lock_kernel();
1555 bdev = bdget(part_devt(p));
1556 if (bdev == NULL)
1557 goto out_unlock_kernel;
1558
Li Zefanb1251302009-03-20 10:34:00 +08001559 q = blk_trace_get_queue(bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001560 if (q == NULL)
1561 goto out_bdput;
Li Zefanb1251302009-03-20 10:34:00 +08001562
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001563 mutex_lock(&bdev->bd_mutex);
Li Zefancd649b82009-03-20 11:33:55 +08001564
1565 if (attr == &dev_attr_enable) {
1566 ret = sprintf(buf, "%u\n", !!q->blk_trace);
1567 goto out_unlock_bdev;
1568 }
1569
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001570 if (q->blk_trace == NULL)
1571 ret = sprintf(buf, "disabled\n");
1572 else if (attr == &dev_attr_act_mask)
Li Zefan09341992009-03-24 17:43:30 +08001573 ret = blk_trace_mask2str(buf, q->blk_trace->act_mask);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001574 else if (attr == &dev_attr_pid)
1575 ret = sprintf(buf, "%u\n", q->blk_trace->pid);
1576 else if (attr == &dev_attr_start_lba)
1577 ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
1578 else if (attr == &dev_attr_end_lba)
1579 ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
Li Zefancd649b82009-03-20 11:33:55 +08001580
1581out_unlock_bdev:
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001582 mutex_unlock(&bdev->bd_mutex);
1583out_bdput:
1584 bdput(bdev);
1585out_unlock_kernel:
1586 unlock_kernel();
1587 return ret;
1588}
1589
1590static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1591 struct device_attribute *attr,
1592 const char *buf, size_t count)
1593{
1594 struct block_device *bdev;
1595 struct request_queue *q;
1596 struct hd_struct *p;
1597 u64 value;
Li Zefan09341992009-03-24 17:43:30 +08001598 ssize_t ret = -EINVAL;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001599
1600 if (count == 0)
1601 goto out;
1602
1603 if (attr == &dev_attr_act_mask) {
1604 if (sscanf(buf, "%llx", &value) != 1) {
1605 /* Assume it is a list of trace category names */
Li Zefan09341992009-03-24 17:43:30 +08001606 ret = blk_trace_str2mask(buf);
1607 if (ret < 0)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001608 goto out;
Li Zefan09341992009-03-24 17:43:30 +08001609 value = ret;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001610 }
1611 } else if (sscanf(buf, "%llu", &value) != 1)
1612 goto out;
1613
Li Zefan09341992009-03-24 17:43:30 +08001614 ret = -ENXIO;
1615
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001616 lock_kernel();
1617 p = dev_to_part(dev);
1618 bdev = bdget(part_devt(p));
1619 if (bdev == NULL)
1620 goto out_unlock_kernel;
1621
Li Zefanb1251302009-03-20 10:34:00 +08001622 q = blk_trace_get_queue(bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001623 if (q == NULL)
1624 goto out_bdput;
1625
1626 mutex_lock(&bdev->bd_mutex);
Li Zefancd649b82009-03-20 11:33:55 +08001627
1628 if (attr == &dev_attr_enable) {
1629 if (value)
Li Zefan9908c302009-04-14 13:59:34 +08001630 ret = blk_trace_setup_queue(q, bdev);
Li Zefancd649b82009-03-20 11:33:55 +08001631 else
1632 ret = blk_trace_remove_queue(q);
1633 goto out_unlock_bdev;
1634 }
1635
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001636 ret = 0;
1637 if (q->blk_trace == NULL)
Li Zefan9908c302009-04-14 13:59:34 +08001638 ret = blk_trace_setup_queue(q, bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001639
1640 if (ret == 0) {
1641 if (attr == &dev_attr_act_mask)
1642 q->blk_trace->act_mask = value;
1643 else if (attr == &dev_attr_pid)
1644 q->blk_trace->pid = value;
1645 else if (attr == &dev_attr_start_lba)
1646 q->blk_trace->start_lba = value;
1647 else if (attr == &dev_attr_end_lba)
1648 q->blk_trace->end_lba = value;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001649 }
Li Zefancd649b82009-03-20 11:33:55 +08001650
1651out_unlock_bdev:
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001652 mutex_unlock(&bdev->bd_mutex);
1653out_bdput:
1654 bdput(bdev);
1655out_unlock_kernel:
1656 unlock_kernel();
1657out:
Li Zefancd649b82009-03-20 11:33:55 +08001658 return ret ? ret : count;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001659}
Li Zefancd649b82009-03-20 11:33:55 +08001660
Li Zefan1d54ad62009-04-14 14:00:05 +08001661int blk_trace_init_sysfs(struct device *dev)
1662{
1663 return sysfs_create_group(&dev->kobj, &blk_trace_attr_group);
1664}
1665
Li Zefan55782132009-06-09 13:43:05 +08001666#endif /* CONFIG_BLK_DEV_IO_TRACE */
1667
1668#ifdef CONFIG_EVENT_TRACING
1669
1670void blk_dump_cmd(char *buf, struct request *rq)
1671{
1672 int i, end;
1673 int len = rq->cmd_len;
1674 unsigned char *cmd = rq->cmd;
1675
1676 if (!blk_pc_request(rq)) {
1677 buf[0] = '\0';
1678 return;
1679 }
1680
1681 for (end = len - 1; end >= 0; end--)
1682 if (cmd[end])
1683 break;
1684 end++;
1685
1686 for (i = 0; i < len; i++) {
1687 buf += sprintf(buf, "%s%02x", i == 0 ? "" : " ", cmd[i]);
1688 if (i == end && end != len - 1) {
1689 sprintf(buf, " ..");
1690 break;
1691 }
1692 }
1693}
1694
1695void blk_fill_rwbs(char *rwbs, u32 rw, int bytes)
1696{
1697 int i = 0;
1698
1699 if (rw & WRITE)
1700 rwbs[i++] = 'W';
1701 else if (rw & 1 << BIO_RW_DISCARD)
1702 rwbs[i++] = 'D';
1703 else if (bytes)
1704 rwbs[i++] = 'R';
1705 else
1706 rwbs[i++] = 'N';
1707
1708 if (rw & 1 << BIO_RW_AHEAD)
1709 rwbs[i++] = 'A';
1710 if (rw & 1 << BIO_RW_BARRIER)
1711 rwbs[i++] = 'B';
1712 if (rw & 1 << BIO_RW_SYNCIO)
1713 rwbs[i++] = 'S';
1714 if (rw & 1 << BIO_RW_META)
1715 rwbs[i++] = 'M';
1716
1717 rwbs[i] = '\0';
1718}
1719
1720void blk_fill_rwbs_rq(char *rwbs, struct request *rq)
1721{
1722 int rw = rq->cmd_flags & 0x03;
1723 int bytes;
1724
1725 if (blk_discard_rq(rq))
1726 rw |= (1 << BIO_RW_DISCARD);
1727
Linus Torvaldsc9059592009-06-11 10:52:27 -07001728 bytes = blk_rq_bytes(rq);
Li Zefan55782132009-06-09 13:43:05 +08001729
1730 blk_fill_rwbs(rwbs, rw, bytes);
1731}
1732
1733#endif /* CONFIG_EVENT_TRACING */
1734