blob: 8f5c37b0f80f1ee0d2369567a1515d1879fa4463 [file] [log] [blame]
Jens Axboe2056a782006-03-23 20:00:26 +01001/*
Jens Axboe0fe23472006-09-04 15:41:16 +02002 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
Jens Axboe2056a782006-03-23 20:00:26 +01003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 *
17 */
Jens Axboe2056a782006-03-23 20:00:26 +010018#include <linux/kernel.h>
19#include <linux/blkdev.h>
20#include <linux/blktrace_api.h>
21#include <linux/percpu.h>
22#include <linux/init.h>
23#include <linux/mutex.h>
24#include <linux/debugfs.h>
Olaf Kirchbe1c6342006-12-01 10:39:12 +010025#include <linux/time.h>
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010026#include <trace/block.h>
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -020027#include <linux/uaccess.h>
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020028#include <../kernel/trace/trace_output.h>
Jens Axboe2056a782006-03-23 20:00:26 +010029
Jens Axboe2056a782006-03-23 20:00:26 +010030static unsigned int blktrace_seq __read_mostly = 1;
31
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020032static struct trace_array *blk_tr;
33static int __read_mostly blk_tracer_enabled;
34
35/* Select an alternative, minimalistic output than the original one */
36#define TRACE_BLK_OPT_CLASSIC 0x1
37
38static struct tracer_opt blk_tracer_opts[] = {
39 /* Default disable the minimalistic output */
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -020040 { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020041 { }
42};
43
44static struct tracer_flags blk_tracer_flags = {
45 .val = 0,
46 .opts = blk_tracer_opts,
47};
48
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010049/* Global reference count of probes */
50static DEFINE_MUTEX(blk_probe_mutex);
51static atomic_t blk_probes_ref = ATOMIC_INIT(0);
52
53static int blk_register_tracepoints(void);
54static void blk_unregister_tracepoints(void);
55
Jens Axboe2056a782006-03-23 20:00:26 +010056/*
Olaf Kirchbe1c6342006-12-01 10:39:12 +010057 * Send out a notify message.
58 */
Jens Axboea8630552006-12-04 09:30:58 +010059static void trace_note(struct blk_trace *bt, pid_t pid, int action,
60 const void *data, size_t len)
Olaf Kirchbe1c6342006-12-01 10:39:12 +010061{
62 struct blk_io_trace *t;
Olaf Kirchbe1c6342006-12-01 10:39:12 +010063
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020064 if (!bt->rchan)
65 return;
66
Olaf Kirchbe1c6342006-12-01 10:39:12 +010067 t = relay_reserve(bt->rchan, sizeof(*t) + len);
Jens Axboed3d9d2a2006-12-04 09:27:41 +010068 if (t) {
69 const int cpu = smp_processor_id();
Olaf Kirchbe1c6342006-12-01 10:39:12 +010070
Jens Axboed3d9d2a2006-12-04 09:27:41 +010071 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
Ingo Molnar2997c8c2008-01-11 13:35:54 +010072 t->time = ktime_to_ns(ktime_get());
Jens Axboed3d9d2a2006-12-04 09:27:41 +010073 t->device = bt->dev;
74 t->action = action;
75 t->pid = pid;
76 t->cpu = cpu;
77 t->pdu_len = len;
78 memcpy((void *) t + sizeof(*t), data, len);
79 }
Olaf Kirchbe1c6342006-12-01 10:39:12 +010080}
81
82/*
Jens Axboe2056a782006-03-23 20:00:26 +010083 * Send out a notify for this process, if we haven't done so since a trace
84 * started
85 */
86static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk)
87{
Jens Axboea8630552006-12-04 09:30:58 +010088 tsk->btrace_seq = blktrace_seq;
89 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm));
Olaf Kirchbe1c6342006-12-01 10:39:12 +010090}
Jens Axboe2056a782006-03-23 20:00:26 +010091
Olaf Kirchbe1c6342006-12-01 10:39:12 +010092static void trace_note_time(struct blk_trace *bt)
93{
94 struct timespec now;
95 unsigned long flags;
96 u32 words[2];
97
98 getnstimeofday(&now);
99 words[0] = now.tv_sec;
100 words[1] = now.tv_nsec;
101
102 local_irq_save(flags);
103 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
104 local_irq_restore(flags);
Jens Axboe2056a782006-03-23 20:00:26 +0100105}
106
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200107void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
108{
109 int n;
110 va_list args;
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200111 unsigned long flags;
Jens Axboe64565912008-05-28 14:45:33 +0200112 char *buf;
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200113
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200114 if (blk_tr) {
115 va_start(args, fmt);
116 ftrace_vprintk(fmt, args);
117 va_end(args);
118 return;
119 }
120
121 if (!bt->msg_data)
122 return;
123
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200124 local_irq_save(flags);
Jens Axboe64565912008-05-28 14:45:33 +0200125 buf = per_cpu_ptr(bt->msg_data, smp_processor_id());
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200126 va_start(args, fmt);
Jens Axboe64565912008-05-28 14:45:33 +0200127 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200128 va_end(args);
129
Jens Axboe64565912008-05-28 14:45:33 +0200130 trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200131 local_irq_restore(flags);
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200132}
133EXPORT_SYMBOL_GPL(__trace_note_message);
134
Jens Axboe2056a782006-03-23 20:00:26 +0100135static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
136 pid_t pid)
137{
138 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
139 return 1;
140 if (sector < bt->start_lba || sector > bt->end_lba)
141 return 1;
142 if (bt->pid && pid != bt->pid)
143 return 1;
144
145 return 0;
146}
147
148/*
149 * Data direction bit lookup
150 */
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200151static u32 ddir_act[2] __read_mostly = { BLK_TC_ACT(BLK_TC_READ),
152 BLK_TC_ACT(BLK_TC_WRITE) };
Jens Axboe2056a782006-03-23 20:00:26 +0100153
David Woodhouse35ba8f72008-08-10 12:33:00 +0100154/* The ilog2() calls fall out because they're constant */
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200155#define MASK_TC_BIT(rw, __name) ((rw & (1 << BIO_RW_ ## __name)) << \
156 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name))
Jens Axboe2056a782006-03-23 20:00:26 +0100157
158/*
159 * The worker for the various blk_add_trace*() types. Fills out a
160 * blk_io_trace structure and places it in a per-cpu subbuffer.
161 */
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100162static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
Jens Axboe2056a782006-03-23 20:00:26 +0100163 int rw, u32 what, int error, int pdu_len, void *pdu_data)
164{
165 struct task_struct *tsk = current;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200166 struct ring_buffer_event *event = NULL;
Jens Axboe2056a782006-03-23 20:00:26 +0100167 struct blk_io_trace *t;
168 unsigned long flags;
169 unsigned long *sequence;
170 pid_t pid;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200171 int cpu, pc = 0;
Jens Axboe2056a782006-03-23 20:00:26 +0100172
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200173 if (unlikely(bt->trace_state != Blktrace_running ||
174 !blk_tracer_enabled))
Jens Axboe2056a782006-03-23 20:00:26 +0100175 return;
176
177 what |= ddir_act[rw & WRITE];
David Woodhouse35ba8f72008-08-10 12:33:00 +0100178 what |= MASK_TC_BIT(rw, BARRIER);
179 what |= MASK_TC_BIT(rw, SYNC);
180 what |= MASK_TC_BIT(rw, AHEAD);
181 what |= MASK_TC_BIT(rw, META);
182 what |= MASK_TC_BIT(rw, DISCARD);
Jens Axboe2056a782006-03-23 20:00:26 +0100183
184 pid = tsk->pid;
185 if (unlikely(act_log_check(bt, what, sector, pid)))
186 return;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200187 cpu = raw_smp_processor_id();
188
189 if (blk_tr) {
190 struct trace_entry *ent;
191 tracing_record_cmdline(current);
192
193 event = ring_buffer_lock_reserve(blk_tr->buffer,
194 sizeof(*t) + pdu_len, &flags);
195 if (!event)
196 return;
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200197
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200198 ent = ring_buffer_event_data(event);
199 t = (struct blk_io_trace *)ent;
200 pc = preempt_count();
201 tracing_generic_entry_update(ent, 0, pc);
202 ent->type = TRACE_BLK;
203 goto record_it;
204 }
Jens Axboe2056a782006-03-23 20:00:26 +0100205
206 /*
207 * A word about the locking here - we disable interrupts to reserve
208 * some space in the relay per-cpu buffer, to prevent an irq
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200209 * from coming in and stepping on our toes.
Jens Axboe2056a782006-03-23 20:00:26 +0100210 */
211 local_irq_save(flags);
212
213 if (unlikely(tsk->btrace_seq != blktrace_seq))
214 trace_note_tsk(bt, tsk);
215
216 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
217 if (t) {
Jens Axboe2056a782006-03-23 20:00:26 +0100218 sequence = per_cpu_ptr(bt->sequence, cpu);
219
220 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
221 t->sequence = ++(*sequence);
Ingo Molnar2997c8c2008-01-11 13:35:54 +0100222 t->time = ktime_to_ns(ktime_get());
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200223record_it:
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -0200224 /*
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200225 * These two are not needed in ftrace as they are in the
226 * generic trace_entry, filled by tracing_generic_entry_update,
227 * but for the trace_event->bin() synthesizer benefit we do it
228 * here too.
229 */
230 t->cpu = cpu;
231 t->pid = pid;
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -0200232
Jens Axboe2056a782006-03-23 20:00:26 +0100233 t->sector = sector;
234 t->bytes = bytes;
235 t->action = what;
Jens Axboe2056a782006-03-23 20:00:26 +0100236 t->device = bt->dev;
Jens Axboe2056a782006-03-23 20:00:26 +0100237 t->error = error;
238 t->pdu_len = pdu_len;
239
240 if (pdu_len)
241 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200242
243 if (blk_tr) {
244 ring_buffer_unlock_commit(blk_tr->buffer, event, flags);
245 if (pid != 0 &&
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200246 !(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC) &&
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200247 (trace_flags & TRACE_ITER_STACKTRACE) != 0)
248 __trace_stack(blk_tr, NULL, flags, 5, pc);
249 trace_wake_up();
250 return;
251 }
Jens Axboe2056a782006-03-23 20:00:26 +0100252 }
253
254 local_irq_restore(flags);
255}
256
Jens Axboe2056a782006-03-23 20:00:26 +0100257static struct dentry *blk_tree_root;
Jens Axboe11a57152008-01-11 13:37:01 +0100258static DEFINE_MUTEX(blk_tree_mutex);
Jens Axboe2056a782006-03-23 20:00:26 +0100259
260static void blk_trace_cleanup(struct blk_trace *bt)
261{
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200262 debugfs_remove(bt->msg_file);
Jens Axboe2056a782006-03-23 20:00:26 +0100263 debugfs_remove(bt->dropped_file);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100264 relay_close(bt->rchan);
Jens Axboe2056a782006-03-23 20:00:26 +0100265 free_percpu(bt->sequence);
Jens Axboe64565912008-05-28 14:45:33 +0200266 free_percpu(bt->msg_data);
Jens Axboe2056a782006-03-23 20:00:26 +0100267 kfree(bt);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100268 mutex_lock(&blk_probe_mutex);
269 if (atomic_dec_and_test(&blk_probes_ref))
270 blk_unregister_tracepoints();
271 mutex_unlock(&blk_probe_mutex);
Jens Axboe2056a782006-03-23 20:00:26 +0100272}
273
Christof Schmitt6da127a2008-01-11 10:09:43 +0100274int blk_trace_remove(struct request_queue *q)
Jens Axboe2056a782006-03-23 20:00:26 +0100275{
276 struct blk_trace *bt;
277
278 bt = xchg(&q->blk_trace, NULL);
279 if (!bt)
280 return -EINVAL;
281
282 if (bt->trace_state == Blktrace_setup ||
283 bt->trace_state == Blktrace_stopped)
284 blk_trace_cleanup(bt);
285
286 return 0;
287}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100288EXPORT_SYMBOL_GPL(blk_trace_remove);
Jens Axboe2056a782006-03-23 20:00:26 +0100289
290static int blk_dropped_open(struct inode *inode, struct file *filp)
291{
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700292 filp->private_data = inode->i_private;
Jens Axboe2056a782006-03-23 20:00:26 +0100293
294 return 0;
295}
296
297static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
298 size_t count, loff_t *ppos)
299{
300 struct blk_trace *bt = filp->private_data;
301 char buf[16];
302
303 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
304
305 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
306}
307
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800308static const struct file_operations blk_dropped_fops = {
Jens Axboe2056a782006-03-23 20:00:26 +0100309 .owner = THIS_MODULE,
310 .open = blk_dropped_open,
311 .read = blk_dropped_read,
312};
313
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200314static int blk_msg_open(struct inode *inode, struct file *filp)
315{
316 filp->private_data = inode->i_private;
317
318 return 0;
319}
320
321static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
322 size_t count, loff_t *ppos)
323{
324 char *msg;
325 struct blk_trace *bt;
326
327 if (count > BLK_TN_MAX_MSG)
328 return -EINVAL;
329
330 msg = kmalloc(count, GFP_KERNEL);
331 if (msg == NULL)
332 return -ENOMEM;
333
334 if (copy_from_user(msg, buffer, count)) {
335 kfree(msg);
336 return -EFAULT;
337 }
338
339 bt = filp->private_data;
340 __trace_note_message(bt, "%s", msg);
341 kfree(msg);
342
343 return count;
344}
345
346static const struct file_operations blk_msg_fops = {
347 .owner = THIS_MODULE,
348 .open = blk_msg_open,
349 .write = blk_msg_write,
350};
351
Jens Axboe2056a782006-03-23 20:00:26 +0100352/*
353 * Keep track of how many times we encountered a full subbuffer, to aid
354 * the user space app in telling how many lost events there were.
355 */
356static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
357 void *prev_subbuf, size_t prev_padding)
358{
359 struct blk_trace *bt;
360
361 if (!relay_buf_full(buf))
362 return 1;
363
364 bt = buf->chan->private_data;
365 atomic_inc(&bt->dropped);
366 return 0;
367}
368
369static int blk_remove_buf_file_callback(struct dentry *dentry)
370{
Jens Axboef48fc4d2009-01-05 10:17:25 +0100371 struct dentry *parent = dentry->d_parent;
Jens Axboe2056a782006-03-23 20:00:26 +0100372 debugfs_remove(dentry);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100373
374 /*
375 * this will fail for all but the last file, but that is ok. what we
376 * care about is the top level buts->name directory going away, when
377 * the last trace file is gone. Then we don't have to rmdir() that
378 * manually on trace stop, so it nicely solves the issue with
379 * force killing of running traces.
380 */
381
382 debugfs_remove(parent);
Jens Axboe2056a782006-03-23 20:00:26 +0100383 return 0;
384}
385
386static struct dentry *blk_create_buf_file_callback(const char *filename,
387 struct dentry *parent,
388 int mode,
389 struct rchan_buf *buf,
390 int *is_global)
391{
392 return debugfs_create_file(filename, mode, parent, buf,
393 &relay_file_operations);
394}
395
396static struct rchan_callbacks blk_relay_callbacks = {
397 .subbuf_start = blk_subbuf_start_callback,
398 .create_buf_file = blk_create_buf_file_callback,
399 .remove_buf_file = blk_remove_buf_file_callback,
400};
401
402/*
403 * Setup everything required to start tracing
404 */
Christof Schmitt6da127a2008-01-11 10:09:43 +0100405int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
Arnd Bergmann171044d42007-10-09 13:23:53 +0200406 struct blk_user_trace_setup *buts)
Jens Axboe2056a782006-03-23 20:00:26 +0100407{
Jens Axboe2056a782006-03-23 20:00:26 +0100408 struct blk_trace *old_bt, *bt = NULL;
409 struct dentry *dir = NULL;
Jens Axboe2056a782006-03-23 20:00:26 +0100410 int ret, i;
411
Arnd Bergmann171044d42007-10-09 13:23:53 +0200412 if (!buts->buf_size || !buts->buf_nr)
Jens Axboe2056a782006-03-23 20:00:26 +0100413 return -EINVAL;
414
Jens Axboe0497b342008-10-01 16:16:25 +0200415 strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
416 buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
Jens Axboe2056a782006-03-23 20:00:26 +0100417
418 /*
419 * some device names have larger paths - convert the slashes
420 * to underscores for this to work as expected
421 */
Arnd Bergmann171044d42007-10-09 13:23:53 +0200422 for (i = 0; i < strlen(buts->name); i++)
423 if (buts->name[i] == '/')
424 buts->name[i] = '_';
Jens Axboe2056a782006-03-23 20:00:26 +0100425
426 ret = -ENOMEM;
427 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
428 if (!bt)
429 goto err;
430
431 bt->sequence = alloc_percpu(unsigned long);
432 if (!bt->sequence)
433 goto err;
434
Jens Axboe64565912008-05-28 14:45:33 +0200435 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG);
436 if (!bt->msg_data)
437 goto err;
438
Jens Axboe2056a782006-03-23 20:00:26 +0100439 ret = -ENOENT;
Jens Axboef48fc4d2009-01-05 10:17:25 +0100440
441 if (!blk_tree_root) {
442 blk_tree_root = debugfs_create_dir("block", NULL);
443 if (!blk_tree_root)
444 return -ENOMEM;
445 }
446
447 dir = debugfs_create_dir(buts->name, blk_tree_root);
448
Jens Axboe2056a782006-03-23 20:00:26 +0100449 if (!dir)
450 goto err;
451
452 bt->dir = dir;
Christof Schmitt6da127a2008-01-11 10:09:43 +0100453 bt->dev = dev;
Jens Axboe2056a782006-03-23 20:00:26 +0100454 atomic_set(&bt->dropped, 0);
455
456 ret = -EIO;
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200457 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt,
458 &blk_dropped_fops);
Jens Axboe2056a782006-03-23 20:00:26 +0100459 if (!bt->dropped_file)
460 goto err;
461
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200462 bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
463 if (!bt->msg_file)
464 goto err;
465
Arnd Bergmann171044d42007-10-09 13:23:53 +0200466 bt->rchan = relay_open("trace", dir, buts->buf_size,
467 buts->buf_nr, &blk_relay_callbacks, bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100468 if (!bt->rchan)
469 goto err;
Jens Axboe2056a782006-03-23 20:00:26 +0100470
Arnd Bergmann171044d42007-10-09 13:23:53 +0200471 bt->act_mask = buts->act_mask;
Jens Axboe2056a782006-03-23 20:00:26 +0100472 if (!bt->act_mask)
473 bt->act_mask = (u16) -1;
474
Arnd Bergmann171044d42007-10-09 13:23:53 +0200475 bt->start_lba = buts->start_lba;
476 bt->end_lba = buts->end_lba;
Jens Axboe2056a782006-03-23 20:00:26 +0100477 if (!bt->end_lba)
478 bt->end_lba = -1ULL;
479
Arnd Bergmann171044d42007-10-09 13:23:53 +0200480 bt->pid = buts->pid;
Jens Axboe2056a782006-03-23 20:00:26 +0100481 bt->trace_state = Blktrace_setup;
482
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100483 mutex_lock(&blk_probe_mutex);
484 if (atomic_add_return(1, &blk_probes_ref) == 1) {
485 ret = blk_register_tracepoints();
486 if (ret)
487 goto probe_err;
488 }
489 mutex_unlock(&blk_probe_mutex);
490
Jens Axboe2056a782006-03-23 20:00:26 +0100491 ret = -EBUSY;
492 old_bt = xchg(&q->blk_trace, bt);
493 if (old_bt) {
494 (void) xchg(&q->blk_trace, old_bt);
495 goto err;
496 }
497
498 return 0;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100499probe_err:
500 atomic_dec(&blk_probes_ref);
501 mutex_unlock(&blk_probe_mutex);
Jens Axboe2056a782006-03-23 20:00:26 +0100502err:
Jens Axboe2056a782006-03-23 20:00:26 +0100503 if (bt) {
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200504 if (bt->msg_file)
505 debugfs_remove(bt->msg_file);
Jens Axboe2056a782006-03-23 20:00:26 +0100506 if (bt->dropped_file)
507 debugfs_remove(bt->dropped_file);
Alan Sterna1205862006-12-06 20:32:37 -0800508 free_percpu(bt->sequence);
Jens Axboe64565912008-05-28 14:45:33 +0200509 free_percpu(bt->msg_data);
Jens Axboe2056a782006-03-23 20:00:26 +0100510 if (bt->rchan)
511 relay_close(bt->rchan);
512 kfree(bt);
513 }
514 return ret;
515}
516
Christof Schmitt6da127a2008-01-11 10:09:43 +0100517int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
518 char __user *arg)
Arnd Bergmann171044d42007-10-09 13:23:53 +0200519{
520 struct blk_user_trace_setup buts;
521 int ret;
522
523 ret = copy_from_user(&buts, arg, sizeof(buts));
524 if (ret)
525 return -EFAULT;
526
Christof Schmitt6da127a2008-01-11 10:09:43 +0100527 ret = do_blk_trace_setup(q, name, dev, &buts);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200528 if (ret)
529 return ret;
530
531 if (copy_to_user(arg, &buts, sizeof(buts)))
532 return -EFAULT;
533
534 return 0;
535}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100536EXPORT_SYMBOL_GPL(blk_trace_setup);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200537
Christof Schmitt6da127a2008-01-11 10:09:43 +0100538int blk_trace_startstop(struct request_queue *q, int start)
Jens Axboe2056a782006-03-23 20:00:26 +0100539{
Jens Axboe2056a782006-03-23 20:00:26 +0100540 int ret;
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200541 struct blk_trace *bt = q->blk_trace;
Jens Axboe2056a782006-03-23 20:00:26 +0100542
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200543 if (bt == NULL)
Jens Axboe2056a782006-03-23 20:00:26 +0100544 return -EINVAL;
545
546 /*
547 * For starting a trace, we can transition from a setup or stopped
548 * trace. For stopping a trace, the state must be running
549 */
550 ret = -EINVAL;
551 if (start) {
552 if (bt->trace_state == Blktrace_setup ||
553 bt->trace_state == Blktrace_stopped) {
554 blktrace_seq++;
555 smp_mb();
556 bt->trace_state = Blktrace_running;
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100557
558 trace_note_time(bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100559 ret = 0;
560 }
561 } else {
562 if (bt->trace_state == Blktrace_running) {
563 bt->trace_state = Blktrace_stopped;
564 relay_flush(bt->rchan);
565 ret = 0;
566 }
567 }
568
569 return ret;
570}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100571EXPORT_SYMBOL_GPL(blk_trace_startstop);
Jens Axboe2056a782006-03-23 20:00:26 +0100572
573/**
574 * blk_trace_ioctl: - handle the ioctls associated with tracing
575 * @bdev: the block device
576 * @cmd: the ioctl cmd
577 * @arg: the argument data, if any
578 *
579 **/
580int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
581{
Jens Axboe165125e2007-07-24 09:28:11 +0200582 struct request_queue *q;
Jens Axboe2056a782006-03-23 20:00:26 +0100583 int ret, start = 0;
Christof Schmitt6da127a2008-01-11 10:09:43 +0100584 char b[BDEVNAME_SIZE];
Jens Axboe2056a782006-03-23 20:00:26 +0100585
586 q = bdev_get_queue(bdev);
587 if (!q)
588 return -ENXIO;
589
590 mutex_lock(&bdev->bd_mutex);
591
592 switch (cmd) {
593 case BLKTRACESETUP:
Jean Delvaref36f21e2008-05-12 14:02:33 -0700594 bdevname(bdev, b);
Christof Schmitt6da127a2008-01-11 10:09:43 +0100595 ret = blk_trace_setup(q, b, bdev->bd_dev, arg);
Jens Axboe2056a782006-03-23 20:00:26 +0100596 break;
597 case BLKTRACESTART:
598 start = 1;
599 case BLKTRACESTOP:
600 ret = blk_trace_startstop(q, start);
601 break;
602 case BLKTRACETEARDOWN:
603 ret = blk_trace_remove(q);
604 break;
605 default:
606 ret = -ENOTTY;
607 break;
608 }
609
610 mutex_unlock(&bdev->bd_mutex);
611 return ret;
612}
613
614/**
615 * blk_trace_shutdown: - stop and cleanup trace structures
616 * @q: the request queue associated with the device
617 *
618 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200619void blk_trace_shutdown(struct request_queue *q)
Jens Axboe2056a782006-03-23 20:00:26 +0100620{
Alexey Dobriyan6c5c9342006-09-29 01:59:40 -0700621 if (q->blk_trace) {
622 blk_trace_startstop(q, 0);
623 blk_trace_remove(q);
624 }
Jens Axboe2056a782006-03-23 20:00:26 +0100625}
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100626
627/*
628 * blktrace probes
629 */
630
631/**
632 * blk_add_trace_rq - Add a trace for a request oriented action
633 * @q: queue the io is for
634 * @rq: the source request
635 * @what: the action
636 *
637 * Description:
638 * Records an action against a request. Will log the bio offset + size.
639 *
640 **/
641static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
642 u32 what)
643{
644 struct blk_trace *bt = q->blk_trace;
645 int rw = rq->cmd_flags & 0x03;
646
647 if (likely(!bt))
648 return;
649
650 if (blk_discard_rq(rq))
651 rw |= (1 << BIO_RW_DISCARD);
652
653 if (blk_pc_request(rq)) {
654 what |= BLK_TC_ACT(BLK_TC_PC);
655 __blk_add_trace(bt, 0, rq->data_len, rw, what, rq->errors,
656 sizeof(rq->cmd), rq->cmd);
657 } else {
658 what |= BLK_TC_ACT(BLK_TC_FS);
659 __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9,
660 rw, what, rq->errors, 0, NULL);
661 }
662}
663
664static void blk_add_trace_rq_abort(struct request_queue *q, struct request *rq)
665{
666 blk_add_trace_rq(q, rq, BLK_TA_ABORT);
667}
668
669static void blk_add_trace_rq_insert(struct request_queue *q, struct request *rq)
670{
671 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
672}
673
674static void blk_add_trace_rq_issue(struct request_queue *q, struct request *rq)
675{
676 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
677}
678
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200679static void blk_add_trace_rq_requeue(struct request_queue *q,
680 struct request *rq)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100681{
682 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
683}
684
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200685static void blk_add_trace_rq_complete(struct request_queue *q,
686 struct request *rq)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100687{
688 blk_add_trace_rq(q, rq, BLK_TA_COMPLETE);
689}
690
691/**
692 * blk_add_trace_bio - Add a trace for a bio oriented action
693 * @q: queue the io is for
694 * @bio: the source bio
695 * @what: the action
696 *
697 * Description:
698 * Records an action against a bio. Will log the bio offset + size.
699 *
700 **/
701static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
702 u32 what)
703{
704 struct blk_trace *bt = q->blk_trace;
705
706 if (likely(!bt))
707 return;
708
709 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what,
710 !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
711}
712
713static void blk_add_trace_bio_bounce(struct request_queue *q, struct bio *bio)
714{
715 blk_add_trace_bio(q, bio, BLK_TA_BOUNCE);
716}
717
718static void blk_add_trace_bio_complete(struct request_queue *q, struct bio *bio)
719{
720 blk_add_trace_bio(q, bio, BLK_TA_COMPLETE);
721}
722
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200723static void blk_add_trace_bio_backmerge(struct request_queue *q,
724 struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100725{
726 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
727}
728
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200729static void blk_add_trace_bio_frontmerge(struct request_queue *q,
730 struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100731{
732 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
733}
734
735static void blk_add_trace_bio_queue(struct request_queue *q, struct bio *bio)
736{
737 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
738}
739
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200740static void blk_add_trace_getrq(struct request_queue *q,
741 struct bio *bio, int rw)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100742{
743 if (bio)
744 blk_add_trace_bio(q, bio, BLK_TA_GETRQ);
745 else {
746 struct blk_trace *bt = q->blk_trace;
747
748 if (bt)
749 __blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL);
750 }
751}
752
753
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200754static void blk_add_trace_sleeprq(struct request_queue *q,
755 struct bio *bio, int rw)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100756{
757 if (bio)
758 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ);
759 else {
760 struct blk_trace *bt = q->blk_trace;
761
762 if (bt)
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200763 __blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ,
764 0, 0, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100765 }
766}
767
768static void blk_add_trace_plug(struct request_queue *q)
769{
770 struct blk_trace *bt = q->blk_trace;
771
772 if (bt)
773 __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
774}
775
776static void blk_add_trace_unplug_io(struct request_queue *q)
777{
778 struct blk_trace *bt = q->blk_trace;
779
780 if (bt) {
781 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
782 __be64 rpdu = cpu_to_be64(pdu);
783
784 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_IO, 0,
785 sizeof(rpdu), &rpdu);
786 }
787}
788
789static void blk_add_trace_unplug_timer(struct request_queue *q)
790{
791 struct blk_trace *bt = q->blk_trace;
792
793 if (bt) {
794 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
795 __be64 rpdu = cpu_to_be64(pdu);
796
797 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_TIMER, 0,
798 sizeof(rpdu), &rpdu);
799 }
800}
801
802static void blk_add_trace_split(struct request_queue *q, struct bio *bio,
803 unsigned int pdu)
804{
805 struct blk_trace *bt = q->blk_trace;
806
807 if (bt) {
808 __be64 rpdu = cpu_to_be64(pdu);
809
810 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
811 BLK_TA_SPLIT, !bio_flagged(bio, BIO_UPTODATE),
812 sizeof(rpdu), &rpdu);
813 }
814}
815
816/**
817 * blk_add_trace_remap - Add a trace for a remap operation
818 * @q: queue the io is for
819 * @bio: the source bio
820 * @dev: target device
821 * @from: source sector
822 * @to: target sector
823 *
824 * Description:
825 * Device mapper or raid target sometimes need to split a bio because
826 * it spans a stripe (or similar). Add a trace for that action.
827 *
828 **/
829static void blk_add_trace_remap(struct request_queue *q, struct bio *bio,
830 dev_t dev, sector_t from, sector_t to)
831{
832 struct blk_trace *bt = q->blk_trace;
833 struct blk_io_trace_remap r;
834
835 if (likely(!bt))
836 return;
837
838 r.device = cpu_to_be32(dev);
839 r.device_from = cpu_to_be32(bio->bi_bdev->bd_dev);
840 r.sector = cpu_to_be64(to);
841
842 __blk_add_trace(bt, from, bio->bi_size, bio->bi_rw, BLK_TA_REMAP,
843 !bio_flagged(bio, BIO_UPTODATE), sizeof(r), &r);
844}
845
846/**
847 * blk_add_driver_data - Add binary message with driver-specific data
848 * @q: queue the io is for
849 * @rq: io request
850 * @data: driver-specific data
851 * @len: length of driver-specific data
852 *
853 * Description:
854 * Some drivers might want to write driver-specific data per request.
855 *
856 **/
857void blk_add_driver_data(struct request_queue *q,
858 struct request *rq,
859 void *data, size_t len)
860{
861 struct blk_trace *bt = q->blk_trace;
862
863 if (likely(!bt))
864 return;
865
866 if (blk_pc_request(rq))
867 __blk_add_trace(bt, 0, rq->data_len, 0, BLK_TA_DRV_DATA,
868 rq->errors, len, data);
869 else
870 __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9,
871 0, BLK_TA_DRV_DATA, rq->errors, len, data);
872}
873EXPORT_SYMBOL_GPL(blk_add_driver_data);
874
875static int blk_register_tracepoints(void)
876{
877 int ret;
878
879 ret = register_trace_block_rq_abort(blk_add_trace_rq_abort);
880 WARN_ON(ret);
881 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert);
882 WARN_ON(ret);
883 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue);
884 WARN_ON(ret);
885 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue);
886 WARN_ON(ret);
887 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete);
888 WARN_ON(ret);
889 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce);
890 WARN_ON(ret);
891 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete);
892 WARN_ON(ret);
893 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
894 WARN_ON(ret);
895 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
896 WARN_ON(ret);
897 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue);
898 WARN_ON(ret);
899 ret = register_trace_block_getrq(blk_add_trace_getrq);
900 WARN_ON(ret);
901 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq);
902 WARN_ON(ret);
903 ret = register_trace_block_plug(blk_add_trace_plug);
904 WARN_ON(ret);
905 ret = register_trace_block_unplug_timer(blk_add_trace_unplug_timer);
906 WARN_ON(ret);
907 ret = register_trace_block_unplug_io(blk_add_trace_unplug_io);
908 WARN_ON(ret);
909 ret = register_trace_block_split(blk_add_trace_split);
910 WARN_ON(ret);
911 ret = register_trace_block_remap(blk_add_trace_remap);
912 WARN_ON(ret);
913 return 0;
914}
915
916static void blk_unregister_tracepoints(void)
917{
918 unregister_trace_block_remap(blk_add_trace_remap);
919 unregister_trace_block_split(blk_add_trace_split);
920 unregister_trace_block_unplug_io(blk_add_trace_unplug_io);
921 unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer);
922 unregister_trace_block_plug(blk_add_trace_plug);
923 unregister_trace_block_sleeprq(blk_add_trace_sleeprq);
924 unregister_trace_block_getrq(blk_add_trace_getrq);
925 unregister_trace_block_bio_queue(blk_add_trace_bio_queue);
926 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
927 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
928 unregister_trace_block_bio_complete(blk_add_trace_bio_complete);
929 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce);
930 unregister_trace_block_rq_complete(blk_add_trace_rq_complete);
931 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue);
932 unregister_trace_block_rq_issue(blk_add_trace_rq_issue);
933 unregister_trace_block_rq_insert(blk_add_trace_rq_insert);
934 unregister_trace_block_rq_abort(blk_add_trace_rq_abort);
935
936 tracepoint_synchronize_unregister();
937}
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200938
939/*
940 * struct blk_io_tracer formatting routines
941 */
942
943static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
944{
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200945 int i = 0;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200946
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200947 if (t->action & BLK_TC_DISCARD)
948 rwbs[i++] = 'D';
949 else if (t->action & BLK_TC_WRITE)
950 rwbs[i++] = 'W';
951 else if (t->bytes)
952 rwbs[i++] = 'R';
953 else
954 rwbs[i++] = 'N';
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200955
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200956 if (t->action & BLK_TC_AHEAD)
957 rwbs[i++] = 'A';
958 if (t->action & BLK_TC_BARRIER)
959 rwbs[i++] = 'B';
960 if (t->action & BLK_TC_SYNC)
961 rwbs[i++] = 'S';
962 if (t->action & BLK_TC_META)
963 rwbs[i++] = 'M';
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200964
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200965 rwbs[i] = '\0';
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200966}
967
968static inline
969const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
970{
971 return (const struct blk_io_trace *)ent;
972}
973
974static inline const void *pdu_start(const struct trace_entry *ent)
975{
976 return te_blk_io_trace(ent) + 1;
977}
978
979static inline u32 t_sec(const struct trace_entry *ent)
980{
981 return te_blk_io_trace(ent)->bytes >> 9;
982}
983
984static inline unsigned long long t_sector(const struct trace_entry *ent)
985{
986 return te_blk_io_trace(ent)->sector;
987}
988
989static inline __u16 t_error(const struct trace_entry *ent)
990{
991 return te_blk_io_trace(ent)->sector;
992}
993
994static __u64 get_pdu_int(const struct trace_entry *ent)
995{
996 const __u64 *val = pdu_start(ent);
997 return be64_to_cpu(*val);
998}
999
1000static void get_pdu_remap(const struct trace_entry *ent,
1001 struct blk_io_trace_remap *r)
1002{
1003 const struct blk_io_trace_remap *__r = pdu_start(ent);
1004 __u64 sector = __r->sector;
1005
1006 r->device = be32_to_cpu(__r->device);
1007 r->device_from = be32_to_cpu(__r->device_from);
1008 r->sector = be64_to_cpu(sector);
1009}
1010
1011static int blk_log_action_iter(struct trace_iterator *iter, const char *act)
1012{
1013 char rwbs[6];
1014 unsigned long long ts = ns2usecs(iter->ts);
1015 unsigned long usec_rem = do_div(ts, USEC_PER_SEC);
1016 unsigned secs = (unsigned long)ts;
1017 const struct trace_entry *ent = iter->ent;
1018 const struct blk_io_trace *t = (const struct blk_io_trace *)ent;
1019
1020 fill_rwbs(rwbs, t);
1021
1022 return trace_seq_printf(&iter->seq,
1023 "%3d,%-3d %2d %5d.%06lu %5u %2s %3s ",
1024 MAJOR(t->device), MINOR(t->device), iter->cpu,
1025 secs, usec_rem, ent->pid, act, rwbs);
1026}
1027
1028static int blk_log_action_seq(struct trace_seq *s, const struct blk_io_trace *t,
1029 const char *act)
1030{
1031 char rwbs[6];
1032 fill_rwbs(rwbs, t);
1033 return trace_seq_printf(s, "%3d,%-3d %2s %3s ",
1034 MAJOR(t->device), MINOR(t->device), act, rwbs);
1035}
1036
1037static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1038{
1039 const char *cmd = trace_find_cmdline(ent->pid);
1040
1041 if (t_sec(ent))
1042 return trace_seq_printf(s, "%llu + %u [%s]\n",
1043 t_sector(ent), t_sec(ent), cmd);
1044 return trace_seq_printf(s, "[%s]\n", cmd);
1045}
1046
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001047static int blk_log_with_error(struct trace_seq *s,
1048 const struct trace_entry *ent)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001049{
1050 if (t_sec(ent))
1051 return trace_seq_printf(s, "%llu + %u [%d]\n", t_sector(ent),
1052 t_sec(ent), t_error(ent));
1053 return trace_seq_printf(s, "%llu [%d]\n", t_sector(ent), t_error(ent));
1054}
1055
1056static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1057{
1058 struct blk_io_trace_remap r = { .device = 0, };
1059
1060 get_pdu_remap(ent, &r);
1061 return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
1062 t_sector(ent),
1063 t_sec(ent), MAJOR(r.device), MINOR(r.device),
1064 (unsigned long long)r.sector);
1065}
1066
1067static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1068{
1069 return trace_seq_printf(s, "[%s]\n", trace_find_cmdline(ent->pid));
1070}
1071
1072static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1073{
1074 return trace_seq_printf(s, "[%s] %llu\n", trace_find_cmdline(ent->pid),
1075 get_pdu_int(ent));
1076}
1077
1078static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1079{
1080 return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
1081 get_pdu_int(ent), trace_find_cmdline(ent->pid));
1082}
1083
1084/*
1085 * struct tracer operations
1086 */
1087
1088static void blk_tracer_print_header(struct seq_file *m)
1089{
1090 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1091 return;
1092 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1093 "# | | | | | |\n");
1094}
1095
1096static void blk_tracer_start(struct trace_array *tr)
1097{
Arnaldo Carvalho de Melo700a3dc2009-01-28 12:33:56 -02001098 tracing_reset_online_cpus(tr);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001099
1100 mutex_lock(&blk_probe_mutex);
1101 if (atomic_add_return(1, &blk_probes_ref) == 1)
1102 if (blk_register_tracepoints())
1103 atomic_dec(&blk_probes_ref);
1104 mutex_unlock(&blk_probe_mutex);
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001105 trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001106}
1107
1108static int blk_tracer_init(struct trace_array *tr)
1109{
1110 blk_tr = tr;
1111 blk_tracer_start(tr);
1112 mutex_lock(&blk_probe_mutex);
1113 blk_tracer_enabled++;
1114 mutex_unlock(&blk_probe_mutex);
1115 return 0;
1116}
1117
1118static void blk_tracer_stop(struct trace_array *tr)
1119{
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001120 trace_flags |= TRACE_ITER_CONTEXT_INFO;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001121 mutex_lock(&blk_probe_mutex);
1122 if (atomic_dec_and_test(&blk_probes_ref))
1123 blk_unregister_tracepoints();
1124 mutex_unlock(&blk_probe_mutex);
1125}
1126
1127static void blk_tracer_reset(struct trace_array *tr)
1128{
1129 if (!atomic_read(&blk_probes_ref))
1130 return;
1131
1132 mutex_lock(&blk_probe_mutex);
1133 blk_tracer_enabled--;
1134 WARN_ON(blk_tracer_enabled < 0);
1135 mutex_unlock(&blk_probe_mutex);
1136
1137 blk_tracer_stop(tr);
1138}
1139
1140static struct {
1141 const char *act[2];
1142 int (*print)(struct trace_seq *s, const struct trace_entry *ent);
1143} what2act[] __read_mostly = {
1144 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
1145 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1146 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1147 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1148 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1149 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1150 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1151 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1152 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1153 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
1154 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
1155 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1156 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1157 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1158 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1159};
1160
Arnaldo Carvalho de Melo2c9b2382009-02-02 20:30:12 -02001161static int blk_trace_event_print(struct trace_iterator *iter, int flags)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001162{
Arnaldo Carvalho de Melo2c9b2382009-02-02 20:30:12 -02001163 struct trace_seq *s = &iter->seq;
1164 const struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001165 const u16 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1166 int ret;
1167
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001168 if (trace_print_context(iter))
1169 return TRACE_TYPE_PARTIAL_LINE;
1170
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001171 if (unlikely(what == 0 || what > ARRAY_SIZE(what2act)))
1172 ret = trace_seq_printf(s, "Bad pc action %x\n", what);
1173 else {
1174 const bool long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1175 ret = blk_log_action_seq(s, t, what2act[what].act[long_act]);
1176 if (ret)
Arnaldo Carvalho de Melo2c9b2382009-02-02 20:30:12 -02001177 ret = what2act[what].print(s, iter->ent);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001178 }
1179
1180 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1181}
1182
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001183static int blk_trace_synthesize_old_trace(struct trace_iterator *iter)
1184{
1185 struct trace_seq *s = &iter->seq;
1186 struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1187 const int offset = offsetof(struct blk_io_trace, sector);
1188 struct blk_io_trace old = {
1189 .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
1190 .time = ns2usecs(iter->ts),
1191 };
1192
1193 if (!trace_seq_putmem(s, &old, offset))
1194 return 0;
1195 return trace_seq_putmem(s, &t->sector,
1196 sizeof(old) - offset + t->pdu_len);
1197}
1198
1199static int blk_trace_event_print_binary(struct trace_iterator *iter, int flags)
1200{
1201 return blk_trace_synthesize_old_trace(iter) ?
1202 TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1203}
1204
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001205static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1206{
1207 const struct blk_io_trace *t;
1208 u16 what;
1209 int ret;
1210
1211 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1212 return TRACE_TYPE_UNHANDLED;
1213
1214 t = (const struct blk_io_trace *)iter->ent;
1215 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1216
1217 if (unlikely(what == 0 || what > ARRAY_SIZE(what2act)))
1218 ret = trace_seq_printf(&iter->seq, "Bad pc action %x\n", what);
1219 else {
1220 const bool long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1221 ret = blk_log_action_iter(iter, what2act[what].act[long_act]);
1222 if (ret)
1223 ret = what2act[what].print(&iter->seq, iter->ent);
1224 }
1225
1226 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1227}
1228
1229static struct tracer blk_tracer __read_mostly = {
1230 .name = "blk",
1231 .init = blk_tracer_init,
1232 .reset = blk_tracer_reset,
1233 .start = blk_tracer_start,
1234 .stop = blk_tracer_stop,
1235 .print_header = blk_tracer_print_header,
1236 .print_line = blk_tracer_print_line,
1237 .flags = &blk_tracer_flags,
1238};
1239
1240static struct trace_event trace_blk_event = {
1241 .type = TRACE_BLK,
1242 .trace = blk_trace_event_print,
1243 .latency_trace = blk_trace_event_print,
1244 .raw = trace_nop_print,
1245 .hex = trace_nop_print,
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001246 .binary = blk_trace_event_print_binary,
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001247};
1248
1249static int __init init_blk_tracer(void)
1250{
1251 if (!register_ftrace_event(&trace_blk_event)) {
1252 pr_warning("Warning: could not register block events\n");
1253 return 1;
1254 }
1255
1256 if (register_tracer(&blk_tracer) != 0) {
1257 pr_warning("Warning: could not register the block tracer\n");
1258 unregister_ftrace_event(&trace_blk_event);
1259 return 1;
1260 }
1261
1262 return 0;
1263}
1264
1265device_initcall(init_blk_tracer);
1266
1267static int blk_trace_remove_queue(struct request_queue *q)
1268{
1269 struct blk_trace *bt;
1270
1271 bt = xchg(&q->blk_trace, NULL);
1272 if (bt == NULL)
1273 return -EINVAL;
1274
1275 kfree(bt);
1276 return 0;
1277}
1278
1279/*
1280 * Setup everything required to start tracing
1281 */
1282static int blk_trace_setup_queue(struct request_queue *q, dev_t dev)
1283{
1284 struct blk_trace *old_bt, *bt = NULL;
1285 int ret;
1286
1287 ret = -ENOMEM;
1288 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1289 if (!bt)
1290 goto err;
1291
1292 bt->dev = dev;
1293 bt->act_mask = (u16)-1;
1294 bt->end_lba = -1ULL;
1295 bt->trace_state = Blktrace_running;
1296
1297 old_bt = xchg(&q->blk_trace, bt);
1298 if (old_bt != NULL) {
1299 (void)xchg(&q->blk_trace, old_bt);
1300 kfree(bt);
1301 ret = -EBUSY;
1302 }
1303 return 0;
1304err:
1305 return ret;
1306}
1307
1308/*
1309 * sysfs interface to enable and configure tracing
1310 */
1311
1312static ssize_t sysfs_blk_trace_enable_show(struct device *dev,
1313 struct device_attribute *attr,
1314 char *buf)
1315{
1316 struct hd_struct *p = dev_to_part(dev);
1317 struct block_device *bdev;
1318 ssize_t ret = -ENXIO;
1319
1320 lock_kernel();
1321 bdev = bdget(part_devt(p));
1322 if (bdev != NULL) {
1323 struct request_queue *q = bdev_get_queue(bdev);
1324
1325 if (q != NULL) {
1326 mutex_lock(&bdev->bd_mutex);
1327 ret = sprintf(buf, "%u\n", !!q->blk_trace);
1328 mutex_unlock(&bdev->bd_mutex);
1329 }
1330
1331 bdput(bdev);
1332 }
1333
1334 unlock_kernel();
1335 return ret;
1336}
1337
1338static ssize_t sysfs_blk_trace_enable_store(struct device *dev,
1339 struct device_attribute *attr,
1340 const char *buf, size_t count)
1341{
1342 struct block_device *bdev;
1343 struct request_queue *q;
1344 struct hd_struct *p;
1345 int value;
1346 ssize_t ret = -ENXIO;
1347
1348 if (count == 0 || sscanf(buf, "%d", &value) != 1)
1349 goto out;
1350
1351 lock_kernel();
1352 p = dev_to_part(dev);
1353 bdev = bdget(part_devt(p));
1354 if (bdev == NULL)
1355 goto out_unlock_kernel;
1356
1357 q = bdev_get_queue(bdev);
1358 if (q == NULL)
1359 goto out_bdput;
1360
1361 mutex_lock(&bdev->bd_mutex);
1362 if (value)
1363 ret = blk_trace_setup_queue(q, bdev->bd_dev);
1364 else
1365 ret = blk_trace_remove_queue(q);
1366 mutex_unlock(&bdev->bd_mutex);
1367
1368 if (ret == 0)
1369 ret = count;
1370out_bdput:
1371 bdput(bdev);
1372out_unlock_kernel:
1373 unlock_kernel();
1374out:
1375 return ret;
1376}
1377
1378static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1379 struct device_attribute *attr,
1380 char *buf);
1381static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1382 struct device_attribute *attr,
1383 const char *buf, size_t count);
1384#define BLK_TRACE_DEVICE_ATTR(_name) \
1385 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1386 sysfs_blk_trace_attr_show, \
1387 sysfs_blk_trace_attr_store)
1388
1389static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
1390 sysfs_blk_trace_enable_show, sysfs_blk_trace_enable_store);
1391static BLK_TRACE_DEVICE_ATTR(act_mask);
1392static BLK_TRACE_DEVICE_ATTR(pid);
1393static BLK_TRACE_DEVICE_ATTR(start_lba);
1394static BLK_TRACE_DEVICE_ATTR(end_lba);
1395
1396static struct attribute *blk_trace_attrs[] = {
1397 &dev_attr_enable.attr,
1398 &dev_attr_act_mask.attr,
1399 &dev_attr_pid.attr,
1400 &dev_attr_start_lba.attr,
1401 &dev_attr_end_lba.attr,
1402 NULL
1403};
1404
1405struct attribute_group blk_trace_attr_group = {
1406 .name = "trace",
1407 .attrs = blk_trace_attrs,
1408};
1409
1410static int blk_str2act_mask(const char *str)
1411{
1412 int mask = 0;
1413 char *copy = kstrdup(str, GFP_KERNEL), *s;
1414
1415 if (copy == NULL)
1416 return -ENOMEM;
1417
1418 s = strstrip(copy);
1419
1420 while (1) {
1421 char *sep = strchr(s, ',');
1422
1423 if (sep != NULL)
1424 *sep = '\0';
1425
1426 if (strcasecmp(s, "barrier") == 0)
1427 mask |= BLK_TC_BARRIER;
1428 else if (strcasecmp(s, "complete") == 0)
1429 mask |= BLK_TC_COMPLETE;
1430 else if (strcasecmp(s, "fs") == 0)
1431 mask |= BLK_TC_FS;
1432 else if (strcasecmp(s, "issue") == 0)
1433 mask |= BLK_TC_ISSUE;
1434 else if (strcasecmp(s, "pc") == 0)
1435 mask |= BLK_TC_PC;
1436 else if (strcasecmp(s, "queue") == 0)
1437 mask |= BLK_TC_QUEUE;
1438 else if (strcasecmp(s, "read") == 0)
1439 mask |= BLK_TC_READ;
1440 else if (strcasecmp(s, "requeue") == 0)
1441 mask |= BLK_TC_REQUEUE;
1442 else if (strcasecmp(s, "sync") == 0)
1443 mask |= BLK_TC_SYNC;
1444 else if (strcasecmp(s, "write") == 0)
1445 mask |= BLK_TC_WRITE;
1446
1447 if (sep == NULL)
1448 break;
1449
1450 s = sep + 1;
1451 }
1452 kfree(copy);
1453
1454 return mask;
1455}
1456
1457static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1458 struct device_attribute *attr,
1459 char *buf)
1460{
1461 struct hd_struct *p = dev_to_part(dev);
1462 struct request_queue *q;
1463 struct block_device *bdev;
1464 ssize_t ret = -ENXIO;
1465
1466 lock_kernel();
1467 bdev = bdget(part_devt(p));
1468 if (bdev == NULL)
1469 goto out_unlock_kernel;
1470
1471 q = bdev_get_queue(bdev);
1472 if (q == NULL)
1473 goto out_bdput;
1474 mutex_lock(&bdev->bd_mutex);
1475 if (q->blk_trace == NULL)
1476 ret = sprintf(buf, "disabled\n");
1477 else if (attr == &dev_attr_act_mask)
1478 ret = sprintf(buf, "%#x\n", q->blk_trace->act_mask);
1479 else if (attr == &dev_attr_pid)
1480 ret = sprintf(buf, "%u\n", q->blk_trace->pid);
1481 else if (attr == &dev_attr_start_lba)
1482 ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
1483 else if (attr == &dev_attr_end_lba)
1484 ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
1485 mutex_unlock(&bdev->bd_mutex);
1486out_bdput:
1487 bdput(bdev);
1488out_unlock_kernel:
1489 unlock_kernel();
1490 return ret;
1491}
1492
1493static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1494 struct device_attribute *attr,
1495 const char *buf, size_t count)
1496{
1497 struct block_device *bdev;
1498 struct request_queue *q;
1499 struct hd_struct *p;
1500 u64 value;
1501 ssize_t ret = -ENXIO;
1502
1503 if (count == 0)
1504 goto out;
1505
1506 if (attr == &dev_attr_act_mask) {
1507 if (sscanf(buf, "%llx", &value) != 1) {
1508 /* Assume it is a list of trace category names */
1509 value = blk_str2act_mask(buf);
1510 if (value < 0)
1511 goto out;
1512 }
1513 } else if (sscanf(buf, "%llu", &value) != 1)
1514 goto out;
1515
1516 lock_kernel();
1517 p = dev_to_part(dev);
1518 bdev = bdget(part_devt(p));
1519 if (bdev == NULL)
1520 goto out_unlock_kernel;
1521
1522 q = bdev_get_queue(bdev);
1523 if (q == NULL)
1524 goto out_bdput;
1525
1526 mutex_lock(&bdev->bd_mutex);
1527 ret = 0;
1528 if (q->blk_trace == NULL)
1529 ret = blk_trace_setup_queue(q, bdev->bd_dev);
1530
1531 if (ret == 0) {
1532 if (attr == &dev_attr_act_mask)
1533 q->blk_trace->act_mask = value;
1534 else if (attr == &dev_attr_pid)
1535 q->blk_trace->pid = value;
1536 else if (attr == &dev_attr_start_lba)
1537 q->blk_trace->start_lba = value;
1538 else if (attr == &dev_attr_end_lba)
1539 q->blk_trace->end_lba = value;
1540 ret = count;
1541 }
1542 mutex_unlock(&bdev->bd_mutex);
1543out_bdput:
1544 bdput(bdev);
1545out_unlock_kernel:
1546 unlock_kernel();
1547out:
1548 return ret;
1549}