blob: 630f167f82405bc5d4a5c38c2e2c6e9e2158dc8f [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>
Jens Axboe2056a782006-03-23 20:00:26 +010027#include <asm/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 */
40 { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC ) },
41 { }
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 */
151static u32 ddir_act[2] __read_mostly = { BLK_TC_ACT(BLK_TC_READ), BLK_TC_ACT(BLK_TC_WRITE) };
152
David Woodhouse35ba8f72008-08-10 12:33:00 +0100153/* The ilog2() calls fall out because they're constant */
154#define MASK_TC_BIT(rw, __name) ( (rw & (1 << BIO_RW_ ## __name)) << \
155 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name) )
Jens Axboe2056a782006-03-23 20:00:26 +0100156
157/*
158 * The worker for the various blk_add_trace*() types. Fills out a
159 * blk_io_trace structure and places it in a per-cpu subbuffer.
160 */
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100161static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
Jens Axboe2056a782006-03-23 20:00:26 +0100162 int rw, u32 what, int error, int pdu_len, void *pdu_data)
163{
164 struct task_struct *tsk = current;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200165 struct ring_buffer_event *event = NULL;
Jens Axboe2056a782006-03-23 20:00:26 +0100166 struct blk_io_trace *t;
167 unsigned long flags;
168 unsigned long *sequence;
169 pid_t pid;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200170 int cpu, pc = 0;
Jens Axboe2056a782006-03-23 20:00:26 +0100171
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200172 if (unlikely(bt->trace_state != Blktrace_running || !blk_tracer_enabled))
Jens Axboe2056a782006-03-23 20:00:26 +0100173 return;
174
175 what |= ddir_act[rw & WRITE];
David Woodhouse35ba8f72008-08-10 12:33:00 +0100176 what |= MASK_TC_BIT(rw, BARRIER);
177 what |= MASK_TC_BIT(rw, SYNC);
178 what |= MASK_TC_BIT(rw, AHEAD);
179 what |= MASK_TC_BIT(rw, META);
180 what |= MASK_TC_BIT(rw, DISCARD);
Jens Axboe2056a782006-03-23 20:00:26 +0100181
182 pid = tsk->pid;
183 if (unlikely(act_log_check(bt, what, sector, pid)))
184 return;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200185 cpu = raw_smp_processor_id();
186
187 if (blk_tr) {
188 struct trace_entry *ent;
189 tracing_record_cmdline(current);
190
191 event = ring_buffer_lock_reserve(blk_tr->buffer,
192 sizeof(*t) + pdu_len, &flags);
193 if (!event)
194 return;
195
196 ent = ring_buffer_event_data(event);
197 t = (struct blk_io_trace *)ent;
198 pc = preempt_count();
199 tracing_generic_entry_update(ent, 0, pc);
200 ent->type = TRACE_BLK;
201 goto record_it;
202 }
Jens Axboe2056a782006-03-23 20:00:26 +0100203
204 /*
205 * A word about the locking here - we disable interrupts to reserve
206 * some space in the relay per-cpu buffer, to prevent an irq
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200207 * from coming in and stepping on our toes.
Jens Axboe2056a782006-03-23 20:00:26 +0100208 */
209 local_irq_save(flags);
210
211 if (unlikely(tsk->btrace_seq != blktrace_seq))
212 trace_note_tsk(bt, tsk);
213
214 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
215 if (t) {
Jens Axboe2056a782006-03-23 20:00:26 +0100216 sequence = per_cpu_ptr(bt->sequence, cpu);
217
218 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
219 t->sequence = ++(*sequence);
Ingo Molnar2997c8c2008-01-11 13:35:54 +0100220 t->time = ktime_to_ns(ktime_get());
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200221 t->cpu = cpu;
222 t->pid = pid;
223record_it:
Jens Axboe2056a782006-03-23 20:00:26 +0100224 t->sector = sector;
225 t->bytes = bytes;
226 t->action = what;
Jens Axboe2056a782006-03-23 20:00:26 +0100227 t->device = bt->dev;
Jens Axboe2056a782006-03-23 20:00:26 +0100228 t->error = error;
229 t->pdu_len = pdu_len;
230
231 if (pdu_len)
232 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200233
234 if (blk_tr) {
235 ring_buffer_unlock_commit(blk_tr->buffer, event, flags);
236 if (pid != 0 &&
237 (blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC) == 0 &&
238 (trace_flags & TRACE_ITER_STACKTRACE) != 0)
239 __trace_stack(blk_tr, NULL, flags, 5, pc);
240 trace_wake_up();
241 return;
242 }
Jens Axboe2056a782006-03-23 20:00:26 +0100243 }
244
245 local_irq_restore(flags);
246}
247
Jens Axboe2056a782006-03-23 20:00:26 +0100248static struct dentry *blk_tree_root;
Jens Axboe11a57152008-01-11 13:37:01 +0100249static DEFINE_MUTEX(blk_tree_mutex);
Jens Axboe2056a782006-03-23 20:00:26 +0100250static unsigned int root_users;
251
252static inline void blk_remove_root(void)
253{
254 if (blk_tree_root) {
255 debugfs_remove(blk_tree_root);
256 blk_tree_root = NULL;
257 }
258}
259
260static void blk_remove_tree(struct dentry *dir)
261{
262 mutex_lock(&blk_tree_mutex);
263 debugfs_remove(dir);
264 if (--root_users == 0)
265 blk_remove_root();
266 mutex_unlock(&blk_tree_mutex);
267}
268
269static struct dentry *blk_create_tree(const char *blk_name)
270{
271 struct dentry *dir = NULL;
Aneesh Kumar K.V35fc51e2007-11-21 12:25:41 +0100272 int created = 0;
Jens Axboe2056a782006-03-23 20:00:26 +0100273
274 mutex_lock(&blk_tree_mutex);
275
276 if (!blk_tree_root) {
277 blk_tree_root = debugfs_create_dir("block", NULL);
278 if (!blk_tree_root)
279 goto err;
Aneesh Kumar K.V35fc51e2007-11-21 12:25:41 +0100280 created = 1;
Jens Axboe2056a782006-03-23 20:00:26 +0100281 }
282
283 dir = debugfs_create_dir(blk_name, blk_tree_root);
284 if (dir)
285 root_users++;
Aneesh Kumar K.V35fc51e2007-11-21 12:25:41 +0100286 else {
287 /* Delete root only if we created it */
288 if (created)
289 blk_remove_root();
290 }
Jens Axboe2056a782006-03-23 20:00:26 +0100291
292err:
293 mutex_unlock(&blk_tree_mutex);
294 return dir;
295}
296
297static void blk_trace_cleanup(struct blk_trace *bt)
298{
299 relay_close(bt->rchan);
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200300 debugfs_remove(bt->msg_file);
Jens Axboe2056a782006-03-23 20:00:26 +0100301 debugfs_remove(bt->dropped_file);
302 blk_remove_tree(bt->dir);
303 free_percpu(bt->sequence);
Jens Axboe64565912008-05-28 14:45:33 +0200304 free_percpu(bt->msg_data);
Jens Axboe2056a782006-03-23 20:00:26 +0100305 kfree(bt);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100306 mutex_lock(&blk_probe_mutex);
307 if (atomic_dec_and_test(&blk_probes_ref))
308 blk_unregister_tracepoints();
309 mutex_unlock(&blk_probe_mutex);
Jens Axboe2056a782006-03-23 20:00:26 +0100310}
311
Christof Schmitt6da127a2008-01-11 10:09:43 +0100312int blk_trace_remove(struct request_queue *q)
Jens Axboe2056a782006-03-23 20:00:26 +0100313{
314 struct blk_trace *bt;
315
316 bt = xchg(&q->blk_trace, NULL);
317 if (!bt)
318 return -EINVAL;
319
320 if (bt->trace_state == Blktrace_setup ||
321 bt->trace_state == Blktrace_stopped)
322 blk_trace_cleanup(bt);
323
324 return 0;
325}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100326EXPORT_SYMBOL_GPL(blk_trace_remove);
Jens Axboe2056a782006-03-23 20:00:26 +0100327
328static int blk_dropped_open(struct inode *inode, struct file *filp)
329{
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700330 filp->private_data = inode->i_private;
Jens Axboe2056a782006-03-23 20:00:26 +0100331
332 return 0;
333}
334
335static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
336 size_t count, loff_t *ppos)
337{
338 struct blk_trace *bt = filp->private_data;
339 char buf[16];
340
341 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
342
343 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
344}
345
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800346static const struct file_operations blk_dropped_fops = {
Jens Axboe2056a782006-03-23 20:00:26 +0100347 .owner = THIS_MODULE,
348 .open = blk_dropped_open,
349 .read = blk_dropped_read,
350};
351
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200352static int blk_msg_open(struct inode *inode, struct file *filp)
353{
354 filp->private_data = inode->i_private;
355
356 return 0;
357}
358
359static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
360 size_t count, loff_t *ppos)
361{
362 char *msg;
363 struct blk_trace *bt;
364
365 if (count > BLK_TN_MAX_MSG)
366 return -EINVAL;
367
368 msg = kmalloc(count, GFP_KERNEL);
369 if (msg == NULL)
370 return -ENOMEM;
371
372 if (copy_from_user(msg, buffer, count)) {
373 kfree(msg);
374 return -EFAULT;
375 }
376
377 bt = filp->private_data;
378 __trace_note_message(bt, "%s", msg);
379 kfree(msg);
380
381 return count;
382}
383
384static const struct file_operations blk_msg_fops = {
385 .owner = THIS_MODULE,
386 .open = blk_msg_open,
387 .write = blk_msg_write,
388};
389
Jens Axboe2056a782006-03-23 20:00:26 +0100390/*
391 * Keep track of how many times we encountered a full subbuffer, to aid
392 * the user space app in telling how many lost events there were.
393 */
394static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
395 void *prev_subbuf, size_t prev_padding)
396{
397 struct blk_trace *bt;
398
399 if (!relay_buf_full(buf))
400 return 1;
401
402 bt = buf->chan->private_data;
403 atomic_inc(&bt->dropped);
404 return 0;
405}
406
407static int blk_remove_buf_file_callback(struct dentry *dentry)
408{
409 debugfs_remove(dentry);
410 return 0;
411}
412
413static struct dentry *blk_create_buf_file_callback(const char *filename,
414 struct dentry *parent,
415 int mode,
416 struct rchan_buf *buf,
417 int *is_global)
418{
419 return debugfs_create_file(filename, mode, parent, buf,
420 &relay_file_operations);
421}
422
423static struct rchan_callbacks blk_relay_callbacks = {
424 .subbuf_start = blk_subbuf_start_callback,
425 .create_buf_file = blk_create_buf_file_callback,
426 .remove_buf_file = blk_remove_buf_file_callback,
427};
428
429/*
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,
Arnd Bergmann171044d42007-10-09 13:23:53 +0200433 struct blk_user_trace_setup *buts)
Jens Axboe2056a782006-03-23 20:00:26 +0100434{
Jens Axboe2056a782006-03-23 20:00:26 +0100435 struct blk_trace *old_bt, *bt = NULL;
436 struct dentry *dir = NULL;
Jens Axboe2056a782006-03-23 20:00:26 +0100437 int ret, i;
438
Arnd Bergmann171044d42007-10-09 13:23:53 +0200439 if (!buts->buf_size || !buts->buf_nr)
Jens Axboe2056a782006-03-23 20:00:26 +0100440 return -EINVAL;
441
Jens Axboe0497b342008-10-01 16:16:25 +0200442 strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
443 buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
Jens Axboe2056a782006-03-23 20:00:26 +0100444
445 /*
446 * some device names have larger paths - convert the slashes
447 * to underscores for this to work as expected
448 */
Arnd Bergmann171044d42007-10-09 13:23:53 +0200449 for (i = 0; i < strlen(buts->name); i++)
450 if (buts->name[i] == '/')
451 buts->name[i] = '_';
Jens Axboe2056a782006-03-23 20:00:26 +0100452
453 ret = -ENOMEM;
454 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
455 if (!bt)
456 goto err;
457
458 bt->sequence = alloc_percpu(unsigned long);
459 if (!bt->sequence)
460 goto err;
461
Jens Axboe64565912008-05-28 14:45:33 +0200462 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG);
463 if (!bt->msg_data)
464 goto err;
465
Jens Axboe2056a782006-03-23 20:00:26 +0100466 ret = -ENOENT;
Arnd Bergmann171044d42007-10-09 13:23:53 +0200467 dir = blk_create_tree(buts->name);
Jens Axboe2056a782006-03-23 20:00:26 +0100468 if (!dir)
469 goto err;
470
471 bt->dir = dir;
Christof Schmitt6da127a2008-01-11 10:09:43 +0100472 bt->dev = dev;
Jens Axboe2056a782006-03-23 20:00:26 +0100473 atomic_set(&bt->dropped, 0);
474
475 ret = -EIO;
476 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt, &blk_dropped_fops);
477 if (!bt->dropped_file)
478 goto err;
479
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200480 bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
481 if (!bt->msg_file)
482 goto err;
483
Arnd Bergmann171044d42007-10-09 13:23:53 +0200484 bt->rchan = relay_open("trace", dir, buts->buf_size,
485 buts->buf_nr, &blk_relay_callbacks, bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100486 if (!bt->rchan)
487 goto err;
Jens Axboe2056a782006-03-23 20:00:26 +0100488
Arnd Bergmann171044d42007-10-09 13:23:53 +0200489 bt->act_mask = buts->act_mask;
Jens Axboe2056a782006-03-23 20:00:26 +0100490 if (!bt->act_mask)
491 bt->act_mask = (u16) -1;
492
Arnd Bergmann171044d42007-10-09 13:23:53 +0200493 bt->start_lba = buts->start_lba;
494 bt->end_lba = buts->end_lba;
Jens Axboe2056a782006-03-23 20:00:26 +0100495 if (!bt->end_lba)
496 bt->end_lba = -1ULL;
497
Arnd Bergmann171044d42007-10-09 13:23:53 +0200498 bt->pid = buts->pid;
Jens Axboe2056a782006-03-23 20:00:26 +0100499 bt->trace_state = Blktrace_setup;
500
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100501 mutex_lock(&blk_probe_mutex);
502 if (atomic_add_return(1, &blk_probes_ref) == 1) {
503 ret = blk_register_tracepoints();
504 if (ret)
505 goto probe_err;
506 }
507 mutex_unlock(&blk_probe_mutex);
508
Jens Axboe2056a782006-03-23 20:00:26 +0100509 ret = -EBUSY;
510 old_bt = xchg(&q->blk_trace, bt);
511 if (old_bt) {
512 (void) xchg(&q->blk_trace, old_bt);
513 goto err;
514 }
515
516 return 0;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100517probe_err:
518 atomic_dec(&blk_probes_ref);
519 mutex_unlock(&blk_probe_mutex);
Jens Axboe2056a782006-03-23 20:00:26 +0100520err:
521 if (dir)
522 blk_remove_tree(dir);
523 if (bt) {
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200524 if (bt->msg_file)
525 debugfs_remove(bt->msg_file);
Jens Axboe2056a782006-03-23 20:00:26 +0100526 if (bt->dropped_file)
527 debugfs_remove(bt->dropped_file);
Alan Sterna1205862006-12-06 20:32:37 -0800528 free_percpu(bt->sequence);
Jens Axboe64565912008-05-28 14:45:33 +0200529 free_percpu(bt->msg_data);
Jens Axboe2056a782006-03-23 20:00:26 +0100530 if (bt->rchan)
531 relay_close(bt->rchan);
532 kfree(bt);
533 }
534 return ret;
535}
536
Christof Schmitt6da127a2008-01-11 10:09:43 +0100537int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
538 char __user *arg)
Arnd Bergmann171044d42007-10-09 13:23:53 +0200539{
540 struct blk_user_trace_setup buts;
541 int ret;
542
543 ret = copy_from_user(&buts, arg, sizeof(buts));
544 if (ret)
545 return -EFAULT;
546
Christof Schmitt6da127a2008-01-11 10:09:43 +0100547 ret = do_blk_trace_setup(q, name, dev, &buts);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200548 if (ret)
549 return ret;
550
551 if (copy_to_user(arg, &buts, sizeof(buts)))
552 return -EFAULT;
553
554 return 0;
555}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100556EXPORT_SYMBOL_GPL(blk_trace_setup);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200557
Christof Schmitt6da127a2008-01-11 10:09:43 +0100558int blk_trace_startstop(struct request_queue *q, int start)
Jens Axboe2056a782006-03-23 20:00:26 +0100559{
560 struct blk_trace *bt;
561 int ret;
562
563 if ((bt = q->blk_trace) == NULL)
564 return -EINVAL;
565
566 /*
567 * For starting a trace, we can transition from a setup or stopped
568 * trace. For stopping a trace, the state must be running
569 */
570 ret = -EINVAL;
571 if (start) {
572 if (bt->trace_state == Blktrace_setup ||
573 bt->trace_state == Blktrace_stopped) {
574 blktrace_seq++;
575 smp_mb();
576 bt->trace_state = Blktrace_running;
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100577
578 trace_note_time(bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100579 ret = 0;
580 }
581 } else {
582 if (bt->trace_state == Blktrace_running) {
583 bt->trace_state = Blktrace_stopped;
584 relay_flush(bt->rchan);
585 ret = 0;
586 }
587 }
588
589 return ret;
590}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100591EXPORT_SYMBOL_GPL(blk_trace_startstop);
Jens Axboe2056a782006-03-23 20:00:26 +0100592
593/**
594 * blk_trace_ioctl: - handle the ioctls associated with tracing
595 * @bdev: the block device
596 * @cmd: the ioctl cmd
597 * @arg: the argument data, if any
598 *
599 **/
600int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
601{
Jens Axboe165125e2007-07-24 09:28:11 +0200602 struct request_queue *q;
Jens Axboe2056a782006-03-23 20:00:26 +0100603 int ret, start = 0;
Christof Schmitt6da127a2008-01-11 10:09:43 +0100604 char b[BDEVNAME_SIZE];
Jens Axboe2056a782006-03-23 20:00:26 +0100605
606 q = bdev_get_queue(bdev);
607 if (!q)
608 return -ENXIO;
609
610 mutex_lock(&bdev->bd_mutex);
611
612 switch (cmd) {
613 case BLKTRACESETUP:
Jean Delvaref36f21e2008-05-12 14:02:33 -0700614 bdevname(bdev, b);
Christof Schmitt6da127a2008-01-11 10:09:43 +0100615 ret = blk_trace_setup(q, b, bdev->bd_dev, arg);
Jens Axboe2056a782006-03-23 20:00:26 +0100616 break;
617 case BLKTRACESTART:
618 start = 1;
619 case BLKTRACESTOP:
620 ret = blk_trace_startstop(q, start);
621 break;
622 case BLKTRACETEARDOWN:
623 ret = blk_trace_remove(q);
624 break;
625 default:
626 ret = -ENOTTY;
627 break;
628 }
629
630 mutex_unlock(&bdev->bd_mutex);
631 return ret;
632}
633
634/**
635 * blk_trace_shutdown: - stop and cleanup trace structures
636 * @q: the request queue associated with the device
637 *
638 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200639void blk_trace_shutdown(struct request_queue *q)
Jens Axboe2056a782006-03-23 20:00:26 +0100640{
Alexey Dobriyan6c5c9342006-09-29 01:59:40 -0700641 if (q->blk_trace) {
642 blk_trace_startstop(q, 0);
643 blk_trace_remove(q);
644 }
Jens Axboe2056a782006-03-23 20:00:26 +0100645}
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100646
647/*
648 * blktrace probes
649 */
650
651/**
652 * blk_add_trace_rq - Add a trace for a request oriented action
653 * @q: queue the io is for
654 * @rq: the source request
655 * @what: the action
656 *
657 * Description:
658 * Records an action against a request. Will log the bio offset + size.
659 *
660 **/
661static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
662 u32 what)
663{
664 struct blk_trace *bt = q->blk_trace;
665 int rw = rq->cmd_flags & 0x03;
666
667 if (likely(!bt))
668 return;
669
670 if (blk_discard_rq(rq))
671 rw |= (1 << BIO_RW_DISCARD);
672
673 if (blk_pc_request(rq)) {
674 what |= BLK_TC_ACT(BLK_TC_PC);
675 __blk_add_trace(bt, 0, rq->data_len, rw, what, rq->errors,
676 sizeof(rq->cmd), rq->cmd);
677 } else {
678 what |= BLK_TC_ACT(BLK_TC_FS);
679 __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9,
680 rw, what, rq->errors, 0, NULL);
681 }
682}
683
684static void blk_add_trace_rq_abort(struct request_queue *q, struct request *rq)
685{
686 blk_add_trace_rq(q, rq, BLK_TA_ABORT);
687}
688
689static void blk_add_trace_rq_insert(struct request_queue *q, struct request *rq)
690{
691 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
692}
693
694static void blk_add_trace_rq_issue(struct request_queue *q, struct request *rq)
695{
696 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
697}
698
699static void blk_add_trace_rq_requeue(struct request_queue *q, struct request *rq)
700{
701 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
702}
703
704static void blk_add_trace_rq_complete(struct request_queue *q, struct request *rq)
705{
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
741static void blk_add_trace_bio_backmerge(struct request_queue *q, struct bio *bio)
742{
743 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
744}
745
746static void blk_add_trace_bio_frontmerge(struct request_queue *q, struct bio *bio)
747{
748 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
749}
750
751static void blk_add_trace_bio_queue(struct request_queue *q, struct bio *bio)
752{
753 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
754}
755
756static void blk_add_trace_getrq(struct request_queue *q, struct bio *bio, int rw)
757{
758 if (bio)
759 blk_add_trace_bio(q, bio, BLK_TA_GETRQ);
760 else {
761 struct blk_trace *bt = q->blk_trace;
762
763 if (bt)
764 __blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL);
765 }
766}
767
768
769static void blk_add_trace_sleeprq(struct request_queue *q, struct bio *bio, int rw)
770{
771 if (bio)
772 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ);
773 else {
774 struct blk_trace *bt = q->blk_trace;
775
776 if (bt)
777 __blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ, 0, 0, NULL);
778 }
779}
780
781static void blk_add_trace_plug(struct request_queue *q)
782{
783 struct blk_trace *bt = q->blk_trace;
784
785 if (bt)
786 __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
787}
788
789static void blk_add_trace_unplug_io(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_IO, 0,
798 sizeof(rpdu), &rpdu);
799 }
800}
801
802static void blk_add_trace_unplug_timer(struct request_queue *q)
803{
804 struct blk_trace *bt = q->blk_trace;
805
806 if (bt) {
807 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
808 __be64 rpdu = cpu_to_be64(pdu);
809
810 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_TIMER, 0,
811 sizeof(rpdu), &rpdu);
812 }
813}
814
815static void blk_add_trace_split(struct request_queue *q, struct bio *bio,
816 unsigned int pdu)
817{
818 struct blk_trace *bt = q->blk_trace;
819
820 if (bt) {
821 __be64 rpdu = cpu_to_be64(pdu);
822
823 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
824 BLK_TA_SPLIT, !bio_flagged(bio, BIO_UPTODATE),
825 sizeof(rpdu), &rpdu);
826 }
827}
828
829/**
830 * blk_add_trace_remap - Add a trace for a remap operation
831 * @q: queue the io is for
832 * @bio: the source bio
833 * @dev: target device
834 * @from: source sector
835 * @to: target sector
836 *
837 * Description:
838 * Device mapper or raid target sometimes need to split a bio because
839 * it spans a stripe (or similar). Add a trace for that action.
840 *
841 **/
842static void blk_add_trace_remap(struct request_queue *q, struct bio *bio,
843 dev_t dev, sector_t from, sector_t to)
844{
845 struct blk_trace *bt = q->blk_trace;
846 struct blk_io_trace_remap r;
847
848 if (likely(!bt))
849 return;
850
851 r.device = cpu_to_be32(dev);
852 r.device_from = cpu_to_be32(bio->bi_bdev->bd_dev);
853 r.sector = cpu_to_be64(to);
854
855 __blk_add_trace(bt, from, bio->bi_size, bio->bi_rw, BLK_TA_REMAP,
856 !bio_flagged(bio, BIO_UPTODATE), sizeof(r), &r);
857}
858
859/**
860 * blk_add_driver_data - Add binary message with driver-specific data
861 * @q: queue the io is for
862 * @rq: io request
863 * @data: driver-specific data
864 * @len: length of driver-specific data
865 *
866 * Description:
867 * Some drivers might want to write driver-specific data per request.
868 *
869 **/
870void blk_add_driver_data(struct request_queue *q,
871 struct request *rq,
872 void *data, size_t len)
873{
874 struct blk_trace *bt = q->blk_trace;
875
876 if (likely(!bt))
877 return;
878
879 if (blk_pc_request(rq))
880 __blk_add_trace(bt, 0, rq->data_len, 0, BLK_TA_DRV_DATA,
881 rq->errors, len, data);
882 else
883 __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9,
884 0, BLK_TA_DRV_DATA, rq->errors, len, data);
885}
886EXPORT_SYMBOL_GPL(blk_add_driver_data);
887
888static int blk_register_tracepoints(void)
889{
890 int ret;
891
892 ret = register_trace_block_rq_abort(blk_add_trace_rq_abort);
893 WARN_ON(ret);
894 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert);
895 WARN_ON(ret);
896 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue);
897 WARN_ON(ret);
898 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue);
899 WARN_ON(ret);
900 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete);
901 WARN_ON(ret);
902 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce);
903 WARN_ON(ret);
904 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete);
905 WARN_ON(ret);
906 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
907 WARN_ON(ret);
908 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
909 WARN_ON(ret);
910 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue);
911 WARN_ON(ret);
912 ret = register_trace_block_getrq(blk_add_trace_getrq);
913 WARN_ON(ret);
914 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq);
915 WARN_ON(ret);
916 ret = register_trace_block_plug(blk_add_trace_plug);
917 WARN_ON(ret);
918 ret = register_trace_block_unplug_timer(blk_add_trace_unplug_timer);
919 WARN_ON(ret);
920 ret = register_trace_block_unplug_io(blk_add_trace_unplug_io);
921 WARN_ON(ret);
922 ret = register_trace_block_split(blk_add_trace_split);
923 WARN_ON(ret);
924 ret = register_trace_block_remap(blk_add_trace_remap);
925 WARN_ON(ret);
926 return 0;
927}
928
929static void blk_unregister_tracepoints(void)
930{
931 unregister_trace_block_remap(blk_add_trace_remap);
932 unregister_trace_block_split(blk_add_trace_split);
933 unregister_trace_block_unplug_io(blk_add_trace_unplug_io);
934 unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer);
935 unregister_trace_block_plug(blk_add_trace_plug);
936 unregister_trace_block_sleeprq(blk_add_trace_sleeprq);
937 unregister_trace_block_getrq(blk_add_trace_getrq);
938 unregister_trace_block_bio_queue(blk_add_trace_bio_queue);
939 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
940 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
941 unregister_trace_block_bio_complete(blk_add_trace_bio_complete);
942 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce);
943 unregister_trace_block_rq_complete(blk_add_trace_rq_complete);
944 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue);
945 unregister_trace_block_rq_issue(blk_add_trace_rq_issue);
946 unregister_trace_block_rq_insert(blk_add_trace_rq_insert);
947 unregister_trace_block_rq_abort(blk_add_trace_rq_abort);
948
949 tracepoint_synchronize_unregister();
950}
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200951
952/*
953 * struct blk_io_tracer formatting routines
954 */
955
956static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
957{
958 int i = 0;
959
960 if (t->action & BLK_TC_DISCARD) rwbs[i++] = 'D';
961 else if (t->action & BLK_TC_WRITE) rwbs[i++] = 'W';
962 else if (t->bytes) rwbs[i++] = 'R';
963 else rwbs[i++] = 'N';
964
965 if (t->action & BLK_TC_AHEAD) rwbs[i++] = 'A';
966 if (t->action & BLK_TC_BARRIER) rwbs[i++] = 'B';
967 if (t->action & BLK_TC_SYNC) rwbs[i++] = 'S';
968 if (t->action & BLK_TC_META) rwbs[i++] = 'M';
969
970 rwbs[i] = '\0';
971}
972
973static inline
974const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
975{
976 return (const struct blk_io_trace *)ent;
977}
978
979static inline const void *pdu_start(const struct trace_entry *ent)
980{
981 return te_blk_io_trace(ent) + 1;
982}
983
984static inline u32 t_sec(const struct trace_entry *ent)
985{
986 return te_blk_io_trace(ent)->bytes >> 9;
987}
988
989static inline unsigned long long t_sector(const struct trace_entry *ent)
990{
991 return te_blk_io_trace(ent)->sector;
992}
993
994static inline __u16 t_error(const struct trace_entry *ent)
995{
996 return te_blk_io_trace(ent)->sector;
997}
998
999static __u64 get_pdu_int(const struct trace_entry *ent)
1000{
1001 const __u64 *val = pdu_start(ent);
1002 return be64_to_cpu(*val);
1003}
1004
1005static void get_pdu_remap(const struct trace_entry *ent,
1006 struct blk_io_trace_remap *r)
1007{
1008 const struct blk_io_trace_remap *__r = pdu_start(ent);
1009 __u64 sector = __r->sector;
1010
1011 r->device = be32_to_cpu(__r->device);
1012 r->device_from = be32_to_cpu(__r->device_from);
1013 r->sector = be64_to_cpu(sector);
1014}
1015
1016static int blk_log_action_iter(struct trace_iterator *iter, const char *act)
1017{
1018 char rwbs[6];
1019 unsigned long long ts = ns2usecs(iter->ts);
1020 unsigned long usec_rem = do_div(ts, USEC_PER_SEC);
1021 unsigned secs = (unsigned long)ts;
1022 const struct trace_entry *ent = iter->ent;
1023 const struct blk_io_trace *t = (const struct blk_io_trace *)ent;
1024
1025 fill_rwbs(rwbs, t);
1026
1027 return trace_seq_printf(&iter->seq,
1028 "%3d,%-3d %2d %5d.%06lu %5u %2s %3s ",
1029 MAJOR(t->device), MINOR(t->device), iter->cpu,
1030 secs, usec_rem, ent->pid, act, rwbs);
1031}
1032
1033static int blk_log_action_seq(struct trace_seq *s, const struct blk_io_trace *t,
1034 const char *act)
1035{
1036 char rwbs[6];
1037 fill_rwbs(rwbs, t);
1038 return trace_seq_printf(s, "%3d,%-3d %2s %3s ",
1039 MAJOR(t->device), MINOR(t->device), act, rwbs);
1040}
1041
1042static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1043{
1044 const char *cmd = trace_find_cmdline(ent->pid);
1045
1046 if (t_sec(ent))
1047 return trace_seq_printf(s, "%llu + %u [%s]\n",
1048 t_sector(ent), t_sec(ent), cmd);
1049 return trace_seq_printf(s, "[%s]\n", cmd);
1050}
1051
1052static int blk_log_with_error(struct trace_seq *s, const struct trace_entry *ent)
1053{
1054 if (t_sec(ent))
1055 return trace_seq_printf(s, "%llu + %u [%d]\n", t_sector(ent),
1056 t_sec(ent), t_error(ent));
1057 return trace_seq_printf(s, "%llu [%d]\n", t_sector(ent), t_error(ent));
1058}
1059
1060static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1061{
1062 struct blk_io_trace_remap r = { .device = 0, };
1063
1064 get_pdu_remap(ent, &r);
1065 return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
1066 t_sector(ent),
1067 t_sec(ent), MAJOR(r.device), MINOR(r.device),
1068 (unsigned long long)r.sector);
1069}
1070
1071static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1072{
1073 return trace_seq_printf(s, "[%s]\n", trace_find_cmdline(ent->pid));
1074}
1075
1076static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1077{
1078 return trace_seq_printf(s, "[%s] %llu\n", trace_find_cmdline(ent->pid),
1079 get_pdu_int(ent));
1080}
1081
1082static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1083{
1084 return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
1085 get_pdu_int(ent), trace_find_cmdline(ent->pid));
1086}
1087
1088/*
1089 * struct tracer operations
1090 */
1091
1092static void blk_tracer_print_header(struct seq_file *m)
1093{
1094 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1095 return;
1096 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1097 "# | | | | | |\n");
1098}
1099
1100static void blk_tracer_start(struct trace_array *tr)
1101{
1102 int cpu;
1103
1104 tr->time_start = ftrace_now(tr->cpu);
1105
1106 for_each_online_cpu(cpu)
1107 tracing_reset(tr, cpu);
1108
1109 mutex_lock(&blk_probe_mutex);
1110 if (atomic_add_return(1, &blk_probes_ref) == 1)
1111 if (blk_register_tracepoints())
1112 atomic_dec(&blk_probes_ref);
1113 mutex_unlock(&blk_probe_mutex);
1114}
1115
1116static int blk_tracer_init(struct trace_array *tr)
1117{
1118 blk_tr = tr;
1119 blk_tracer_start(tr);
1120 mutex_lock(&blk_probe_mutex);
1121 blk_tracer_enabled++;
1122 mutex_unlock(&blk_probe_mutex);
1123 return 0;
1124}
1125
1126static void blk_tracer_stop(struct trace_array *tr)
1127{
1128 mutex_lock(&blk_probe_mutex);
1129 if (atomic_dec_and_test(&blk_probes_ref))
1130 blk_unregister_tracepoints();
1131 mutex_unlock(&blk_probe_mutex);
1132}
1133
1134static void blk_tracer_reset(struct trace_array *tr)
1135{
1136 if (!atomic_read(&blk_probes_ref))
1137 return;
1138
1139 mutex_lock(&blk_probe_mutex);
1140 blk_tracer_enabled--;
1141 WARN_ON(blk_tracer_enabled < 0);
1142 mutex_unlock(&blk_probe_mutex);
1143
1144 blk_tracer_stop(tr);
1145}
1146
1147static struct {
1148 const char *act[2];
1149 int (*print)(struct trace_seq *s, const struct trace_entry *ent);
1150} what2act[] __read_mostly = {
1151 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
1152 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1153 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1154 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1155 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1156 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1157 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1158 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1159 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1160 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
1161 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
1162 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1163 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1164 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1165 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1166};
1167
1168static int blk_trace_event_print(struct trace_seq *s, struct trace_entry *ent,
1169 int flags)
1170{
1171 const struct blk_io_trace *t = (struct blk_io_trace *)ent;
1172 const u16 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1173 int ret;
1174
1175 if (unlikely(what == 0 || what > ARRAY_SIZE(what2act)))
1176 ret = trace_seq_printf(s, "Bad pc action %x\n", what);
1177 else {
1178 const bool long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1179 ret = blk_log_action_seq(s, t, what2act[what].act[long_act]);
1180 if (ret)
1181 ret = what2act[what].print(s, ent);
1182 }
1183
1184 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1185}
1186
1187static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1188{
1189 const struct blk_io_trace *t;
1190 u16 what;
1191 int ret;
1192
1193 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1194 return TRACE_TYPE_UNHANDLED;
1195
1196 t = (const struct blk_io_trace *)iter->ent;
1197 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1198
1199 if (unlikely(what == 0 || what > ARRAY_SIZE(what2act)))
1200 ret = trace_seq_printf(&iter->seq, "Bad pc action %x\n", what);
1201 else {
1202 const bool long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1203 ret = blk_log_action_iter(iter, what2act[what].act[long_act]);
1204 if (ret)
1205 ret = what2act[what].print(&iter->seq, iter->ent);
1206 }
1207
1208 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1209}
1210
1211static struct tracer blk_tracer __read_mostly = {
1212 .name = "blk",
1213 .init = blk_tracer_init,
1214 .reset = blk_tracer_reset,
1215 .start = blk_tracer_start,
1216 .stop = blk_tracer_stop,
1217 .print_header = blk_tracer_print_header,
1218 .print_line = blk_tracer_print_line,
1219 .flags = &blk_tracer_flags,
1220};
1221
1222static struct trace_event trace_blk_event = {
1223 .type = TRACE_BLK,
1224 .trace = blk_trace_event_print,
1225 .latency_trace = blk_trace_event_print,
1226 .raw = trace_nop_print,
1227 .hex = trace_nop_print,
1228 .binary = trace_nop_print,
1229};
1230
1231static int __init init_blk_tracer(void)
1232{
1233 if (!register_ftrace_event(&trace_blk_event)) {
1234 pr_warning("Warning: could not register block events\n");
1235 return 1;
1236 }
1237
1238 if (register_tracer(&blk_tracer) != 0) {
1239 pr_warning("Warning: could not register the block tracer\n");
1240 unregister_ftrace_event(&trace_blk_event);
1241 return 1;
1242 }
1243
1244 return 0;
1245}
1246
1247device_initcall(init_blk_tracer);
1248
1249static int blk_trace_remove_queue(struct request_queue *q)
1250{
1251 struct blk_trace *bt;
1252
1253 bt = xchg(&q->blk_trace, NULL);
1254 if (bt == NULL)
1255 return -EINVAL;
1256
1257 kfree(bt);
1258 return 0;
1259}
1260
1261/*
1262 * Setup everything required to start tracing
1263 */
1264static int blk_trace_setup_queue(struct request_queue *q, dev_t dev)
1265{
1266 struct blk_trace *old_bt, *bt = NULL;
1267 int ret;
1268
1269 ret = -ENOMEM;
1270 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1271 if (!bt)
1272 goto err;
1273
1274 bt->dev = dev;
1275 bt->act_mask = (u16)-1;
1276 bt->end_lba = -1ULL;
1277 bt->trace_state = Blktrace_running;
1278
1279 old_bt = xchg(&q->blk_trace, bt);
1280 if (old_bt != NULL) {
1281 (void)xchg(&q->blk_trace, old_bt);
1282 kfree(bt);
1283 ret = -EBUSY;
1284 }
1285 return 0;
1286err:
1287 return ret;
1288}
1289
1290/*
1291 * sysfs interface to enable and configure tracing
1292 */
1293
1294static ssize_t sysfs_blk_trace_enable_show(struct device *dev,
1295 struct device_attribute *attr,
1296 char *buf)
1297{
1298 struct hd_struct *p = dev_to_part(dev);
1299 struct block_device *bdev;
1300 ssize_t ret = -ENXIO;
1301
1302 lock_kernel();
1303 bdev = bdget(part_devt(p));
1304 if (bdev != NULL) {
1305 struct request_queue *q = bdev_get_queue(bdev);
1306
1307 if (q != NULL) {
1308 mutex_lock(&bdev->bd_mutex);
1309 ret = sprintf(buf, "%u\n", !!q->blk_trace);
1310 mutex_unlock(&bdev->bd_mutex);
1311 }
1312
1313 bdput(bdev);
1314 }
1315
1316 unlock_kernel();
1317 return ret;
1318}
1319
1320static ssize_t sysfs_blk_trace_enable_store(struct device *dev,
1321 struct device_attribute *attr,
1322 const char *buf, size_t count)
1323{
1324 struct block_device *bdev;
1325 struct request_queue *q;
1326 struct hd_struct *p;
1327 int value;
1328 ssize_t ret = -ENXIO;
1329
1330 if (count == 0 || sscanf(buf, "%d", &value) != 1)
1331 goto out;
1332
1333 lock_kernel();
1334 p = dev_to_part(dev);
1335 bdev = bdget(part_devt(p));
1336 if (bdev == NULL)
1337 goto out_unlock_kernel;
1338
1339 q = bdev_get_queue(bdev);
1340 if (q == NULL)
1341 goto out_bdput;
1342
1343 mutex_lock(&bdev->bd_mutex);
1344 if (value)
1345 ret = blk_trace_setup_queue(q, bdev->bd_dev);
1346 else
1347 ret = blk_trace_remove_queue(q);
1348 mutex_unlock(&bdev->bd_mutex);
1349
1350 if (ret == 0)
1351 ret = count;
1352out_bdput:
1353 bdput(bdev);
1354out_unlock_kernel:
1355 unlock_kernel();
1356out:
1357 return ret;
1358}
1359
1360static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1361 struct device_attribute *attr,
1362 char *buf);
1363static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1364 struct device_attribute *attr,
1365 const char *buf, size_t count);
1366#define BLK_TRACE_DEVICE_ATTR(_name) \
1367 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1368 sysfs_blk_trace_attr_show, \
1369 sysfs_blk_trace_attr_store)
1370
1371static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
1372 sysfs_blk_trace_enable_show, sysfs_blk_trace_enable_store);
1373static BLK_TRACE_DEVICE_ATTR(act_mask);
1374static BLK_TRACE_DEVICE_ATTR(pid);
1375static BLK_TRACE_DEVICE_ATTR(start_lba);
1376static BLK_TRACE_DEVICE_ATTR(end_lba);
1377
1378static struct attribute *blk_trace_attrs[] = {
1379 &dev_attr_enable.attr,
1380 &dev_attr_act_mask.attr,
1381 &dev_attr_pid.attr,
1382 &dev_attr_start_lba.attr,
1383 &dev_attr_end_lba.attr,
1384 NULL
1385};
1386
1387struct attribute_group blk_trace_attr_group = {
1388 .name = "trace",
1389 .attrs = blk_trace_attrs,
1390};
1391
1392static int blk_str2act_mask(const char *str)
1393{
1394 int mask = 0;
1395 char *copy = kstrdup(str, GFP_KERNEL), *s;
1396
1397 if (copy == NULL)
1398 return -ENOMEM;
1399
1400 s = strstrip(copy);
1401
1402 while (1) {
1403 char *sep = strchr(s, ',');
1404
1405 if (sep != NULL)
1406 *sep = '\0';
1407
1408 if (strcasecmp(s, "barrier") == 0)
1409 mask |= BLK_TC_BARRIER;
1410 else if (strcasecmp(s, "complete") == 0)
1411 mask |= BLK_TC_COMPLETE;
1412 else if (strcasecmp(s, "fs") == 0)
1413 mask |= BLK_TC_FS;
1414 else if (strcasecmp(s, "issue") == 0)
1415 mask |= BLK_TC_ISSUE;
1416 else if (strcasecmp(s, "pc") == 0)
1417 mask |= BLK_TC_PC;
1418 else if (strcasecmp(s, "queue") == 0)
1419 mask |= BLK_TC_QUEUE;
1420 else if (strcasecmp(s, "read") == 0)
1421 mask |= BLK_TC_READ;
1422 else if (strcasecmp(s, "requeue") == 0)
1423 mask |= BLK_TC_REQUEUE;
1424 else if (strcasecmp(s, "sync") == 0)
1425 mask |= BLK_TC_SYNC;
1426 else if (strcasecmp(s, "write") == 0)
1427 mask |= BLK_TC_WRITE;
1428
1429 if (sep == NULL)
1430 break;
1431
1432 s = sep + 1;
1433 }
1434 kfree(copy);
1435
1436 return mask;
1437}
1438
1439static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1440 struct device_attribute *attr,
1441 char *buf)
1442{
1443 struct hd_struct *p = dev_to_part(dev);
1444 struct request_queue *q;
1445 struct block_device *bdev;
1446 ssize_t ret = -ENXIO;
1447
1448 lock_kernel();
1449 bdev = bdget(part_devt(p));
1450 if (bdev == NULL)
1451 goto out_unlock_kernel;
1452
1453 q = bdev_get_queue(bdev);
1454 if (q == NULL)
1455 goto out_bdput;
1456 mutex_lock(&bdev->bd_mutex);
1457 if (q->blk_trace == NULL)
1458 ret = sprintf(buf, "disabled\n");
1459 else if (attr == &dev_attr_act_mask)
1460 ret = sprintf(buf, "%#x\n", q->blk_trace->act_mask);
1461 else if (attr == &dev_attr_pid)
1462 ret = sprintf(buf, "%u\n", q->blk_trace->pid);
1463 else if (attr == &dev_attr_start_lba)
1464 ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
1465 else if (attr == &dev_attr_end_lba)
1466 ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
1467 mutex_unlock(&bdev->bd_mutex);
1468out_bdput:
1469 bdput(bdev);
1470out_unlock_kernel:
1471 unlock_kernel();
1472 return ret;
1473}
1474
1475static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1476 struct device_attribute *attr,
1477 const char *buf, size_t count)
1478{
1479 struct block_device *bdev;
1480 struct request_queue *q;
1481 struct hd_struct *p;
1482 u64 value;
1483 ssize_t ret = -ENXIO;
1484
1485 if (count == 0)
1486 goto out;
1487
1488 if (attr == &dev_attr_act_mask) {
1489 if (sscanf(buf, "%llx", &value) != 1) {
1490 /* Assume it is a list of trace category names */
1491 value = blk_str2act_mask(buf);
1492 if (value < 0)
1493 goto out;
1494 }
1495 } else if (sscanf(buf, "%llu", &value) != 1)
1496 goto out;
1497
1498 lock_kernel();
1499 p = dev_to_part(dev);
1500 bdev = bdget(part_devt(p));
1501 if (bdev == NULL)
1502 goto out_unlock_kernel;
1503
1504 q = bdev_get_queue(bdev);
1505 if (q == NULL)
1506 goto out_bdput;
1507
1508 mutex_lock(&bdev->bd_mutex);
1509 ret = 0;
1510 if (q->blk_trace == NULL)
1511 ret = blk_trace_setup_queue(q, bdev->bd_dev);
1512
1513 if (ret == 0) {
1514 if (attr == &dev_attr_act_mask)
1515 q->blk_trace->act_mask = value;
1516 else if (attr == &dev_attr_pid)
1517 q->blk_trace->pid = value;
1518 else if (attr == &dev_attr_start_lba)
1519 q->blk_trace->start_lba = value;
1520 else if (attr == &dev_attr_end_lba)
1521 q->blk_trace->end_lba = value;
1522 ret = count;
1523 }
1524 mutex_unlock(&bdev->bd_mutex);
1525out_bdput:
1526 bdput(bdev);
1527out_unlock_kernel:
1528 unlock_kernel();
1529out:
1530 return ret;
1531}