blob: 947c5b3f90c4ce42dd511b04bd9b28ded2842ac3 [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>
Frederic Weisbecker2db270a2009-02-07 20:46:45 +010028#include "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;
Li Zefan5006ea72009-03-20 09:48:03 +080033static bool blk_tracer_enabled __read_mostly;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020034
35/* Select an alternative, minimalistic output than the original one */
Steven Rostedtef180122009-03-10 14:10:56 -040036#define TRACE_BLK_OPT_CLASSIC 0x1
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020037
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 */
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010050static atomic_t blk_probes_ref = ATOMIC_INIT(0);
51
Li Zefan3c289ba2009-03-20 09:48:26 +080052static void blk_register_tracepoints(void);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010053static void blk_unregister_tracepoints(void);
54
Jens Axboe2056a782006-03-23 20:00:26 +010055/*
Olaf Kirchbe1c6342006-12-01 10:39:12 +010056 * Send out a notify message.
57 */
Jens Axboea8630552006-12-04 09:30:58 +010058static void trace_note(struct blk_trace *bt, pid_t pid, int action,
59 const void *data, size_t len)
Olaf Kirchbe1c6342006-12-01 10:39:12 +010060{
61 struct blk_io_trace *t;
Li Zefan18cea452009-03-27 10:21:54 +080062 struct ring_buffer_event *event = NULL;
63 int pc = 0;
64 int cpu = smp_processor_id();
65 bool blk_tracer = blk_tracer_enabled;
66
67 if (blk_tracer) {
68 pc = preempt_count();
69 event = trace_buffer_lock_reserve(blk_tr, TRACE_BLK,
70 sizeof(*t) + len,
71 0, pc);
72 if (!event)
73 return;
74 t = ring_buffer_event_data(event);
75 goto record_it;
76 }
Olaf Kirchbe1c6342006-12-01 10:39:12 +010077
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020078 if (!bt->rchan)
79 return;
80
Olaf Kirchbe1c6342006-12-01 10:39:12 +010081 t = relay_reserve(bt->rchan, sizeof(*t) + len);
Jens Axboed3d9d2a2006-12-04 09:27:41 +010082 if (t) {
Jens Axboed3d9d2a2006-12-04 09:27:41 +010083 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
Ingo Molnar2997c8c2008-01-11 13:35:54 +010084 t->time = ktime_to_ns(ktime_get());
Li Zefan18cea452009-03-27 10:21:54 +080085record_it:
Jens Axboed3d9d2a2006-12-04 09:27:41 +010086 t->device = bt->dev;
87 t->action = action;
88 t->pid = pid;
89 t->cpu = cpu;
90 t->pdu_len = len;
91 memcpy((void *) t + sizeof(*t), data, len);
Li Zefan18cea452009-03-27 10:21:54 +080092
93 if (blk_tracer)
94 trace_buffer_unlock_commit(blk_tr, event, 0, pc);
Jens Axboed3d9d2a2006-12-04 09:27:41 +010095 }
Olaf Kirchbe1c6342006-12-01 10:39:12 +010096}
97
98/*
Jens Axboe2056a782006-03-23 20:00:26 +010099 * Send out a notify for this process, if we haven't done so since a trace
100 * started
101 */
102static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk)
103{
Jens Axboea8630552006-12-04 09:30:58 +0100104 tsk->btrace_seq = blktrace_seq;
105 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm));
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100106}
Jens Axboe2056a782006-03-23 20:00:26 +0100107
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100108static void trace_note_time(struct blk_trace *bt)
109{
110 struct timespec now;
111 unsigned long flags;
112 u32 words[2];
113
114 getnstimeofday(&now);
115 words[0] = now.tv_sec;
116 words[1] = now.tv_nsec;
117
118 local_irq_save(flags);
119 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
120 local_irq_restore(flags);
Jens Axboe2056a782006-03-23 20:00:26 +0100121}
122
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200123void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
124{
125 int n;
126 va_list args;
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200127 unsigned long flags;
Jens Axboe64565912008-05-28 14:45:33 +0200128 char *buf;
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200129
Li Zefan18cea452009-03-27 10:21:54 +0800130 if (unlikely(bt->trace_state != Blktrace_running &&
131 !blk_tracer_enabled))
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200132 return;
133
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200134 local_irq_save(flags);
Jens Axboe64565912008-05-28 14:45:33 +0200135 buf = per_cpu_ptr(bt->msg_data, smp_processor_id());
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200136 va_start(args, fmt);
Jens Axboe64565912008-05-28 14:45:33 +0200137 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200138 va_end(args);
139
Jens Axboe64565912008-05-28 14:45:33 +0200140 trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200141 local_irq_restore(flags);
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200142}
143EXPORT_SYMBOL_GPL(__trace_note_message);
144
Jens Axboe2056a782006-03-23 20:00:26 +0100145static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
146 pid_t pid)
147{
148 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
149 return 1;
150 if (sector < bt->start_lba || sector > bt->end_lba)
151 return 1;
152 if (bt->pid && pid != bt->pid)
153 return 1;
154
155 return 0;
156}
157
158/*
159 * Data direction bit lookup
160 */
Li Zefane4955c92009-03-24 16:04:37 +0800161static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ),
162 BLK_TC_ACT(BLK_TC_WRITE) };
Jens Axboe2056a782006-03-23 20:00:26 +0100163
David Woodhouse35ba8f72008-08-10 12:33:00 +0100164/* The ilog2() calls fall out because they're constant */
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200165#define MASK_TC_BIT(rw, __name) ((rw & (1 << BIO_RW_ ## __name)) << \
166 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name))
Jens Axboe2056a782006-03-23 20:00:26 +0100167
168/*
169 * The worker for the various blk_add_trace*() types. Fills out a
170 * blk_io_trace structure and places it in a per-cpu subbuffer.
171 */
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100172static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
Jens Axboe2056a782006-03-23 20:00:26 +0100173 int rw, u32 what, int error, int pdu_len, void *pdu_data)
174{
175 struct task_struct *tsk = current;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200176 struct ring_buffer_event *event = NULL;
Jens Axboe2056a782006-03-23 20:00:26 +0100177 struct blk_io_trace *t;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200178 unsigned long flags = 0;
Jens Axboe2056a782006-03-23 20:00:26 +0100179 unsigned long *sequence;
180 pid_t pid;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200181 int cpu, pc = 0;
Li Zefan18cea452009-03-27 10:21:54 +0800182 bool blk_tracer = blk_tracer_enabled;
Jens Axboe2056a782006-03-23 20:00:26 +0100183
Li Zefan18cea452009-03-27 10:21:54 +0800184 if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer))
Jens Axboe2056a782006-03-23 20:00:26 +0100185 return;
186
187 what |= ddir_act[rw & WRITE];
David Woodhouse35ba8f72008-08-10 12:33:00 +0100188 what |= MASK_TC_BIT(rw, BARRIER);
Ingo Molnar72c26c92009-02-19 09:00:35 +0100189 what |= MASK_TC_BIT(rw, SYNCIO);
David Woodhouse35ba8f72008-08-10 12:33:00 +0100190 what |= MASK_TC_BIT(rw, AHEAD);
191 what |= MASK_TC_BIT(rw, META);
192 what |= MASK_TC_BIT(rw, DISCARD);
Jens Axboe2056a782006-03-23 20:00:26 +0100193
194 pid = tsk->pid;
195 if (unlikely(act_log_check(bt, what, sector, pid)))
196 return;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200197 cpu = raw_smp_processor_id();
198
Li Zefan18cea452009-03-27 10:21:54 +0800199 if (blk_tracer) {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200200 tracing_record_cmdline(current);
201
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200202 pc = preempt_count();
203 event = trace_buffer_lock_reserve(blk_tr, TRACE_BLK,
204 sizeof(*t) + pdu_len,
205 0, pc);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200206 if (!event)
207 return;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200208 t = ring_buffer_event_data(event);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200209 goto record_it;
210 }
Jens Axboe2056a782006-03-23 20:00:26 +0100211
212 /*
213 * A word about the locking here - we disable interrupts to reserve
214 * some space in the relay per-cpu buffer, to prevent an irq
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200215 * from coming in and stepping on our toes.
Jens Axboe2056a782006-03-23 20:00:26 +0100216 */
217 local_irq_save(flags);
218
219 if (unlikely(tsk->btrace_seq != blktrace_seq))
220 trace_note_tsk(bt, tsk);
221
222 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
223 if (t) {
Jens Axboe2056a782006-03-23 20:00:26 +0100224 sequence = per_cpu_ptr(bt->sequence, cpu);
225
226 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
227 t->sequence = ++(*sequence);
Ingo Molnar2997c8c2008-01-11 13:35:54 +0100228 t->time = ktime_to_ns(ktime_get());
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200229record_it:
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -0200230 /*
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200231 * These two are not needed in ftrace as they are in the
232 * generic trace_entry, filled by tracing_generic_entry_update,
233 * but for the trace_event->bin() synthesizer benefit we do it
234 * here too.
235 */
236 t->cpu = cpu;
237 t->pid = pid;
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -0200238
Jens Axboe2056a782006-03-23 20:00:26 +0100239 t->sector = sector;
240 t->bytes = bytes;
241 t->action = what;
Jens Axboe2056a782006-03-23 20:00:26 +0100242 t->device = bt->dev;
Jens Axboe2056a782006-03-23 20:00:26 +0100243 t->error = error;
244 t->pdu_len = pdu_len;
245
246 if (pdu_len)
247 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200248
Li Zefan18cea452009-03-27 10:21:54 +0800249 if (blk_tracer) {
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200250 trace_buffer_unlock_commit(blk_tr, event, 0, pc);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200251 return;
252 }
Jens Axboe2056a782006-03-23 20:00:26 +0100253 }
254
255 local_irq_restore(flags);
256}
257
Jens Axboe2056a782006-03-23 20:00:26 +0100258static struct dentry *blk_tree_root;
Jens Axboe11a57152008-01-11 13:37:01 +0100259static DEFINE_MUTEX(blk_tree_mutex);
Jens Axboe2056a782006-03-23 20:00:26 +0100260
Li Zefanad5dd542009-03-27 10:20:24 +0800261static void blk_trace_free(struct blk_trace *bt)
Jens Axboe2056a782006-03-23 20:00:26 +0100262{
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200263 debugfs_remove(bt->msg_file);
Jens Axboe2056a782006-03-23 20:00:26 +0100264 debugfs_remove(bt->dropped_file);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100265 relay_close(bt->rchan);
Jens Axboe2056a782006-03-23 20:00:26 +0100266 free_percpu(bt->sequence);
Jens Axboe64565912008-05-28 14:45:33 +0200267 free_percpu(bt->msg_data);
Jens Axboe2056a782006-03-23 20:00:26 +0100268 kfree(bt);
Li Zefanad5dd542009-03-27 10:20:24 +0800269}
270
271static void blk_trace_cleanup(struct blk_trace *bt)
272{
273 blk_trace_free(bt);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100274 if (atomic_dec_and_test(&blk_probes_ref))
275 blk_unregister_tracepoints();
Jens Axboe2056a782006-03-23 20:00:26 +0100276}
277
Christof Schmitt6da127a2008-01-11 10:09:43 +0100278int blk_trace_remove(struct request_queue *q)
Jens Axboe2056a782006-03-23 20:00:26 +0100279{
280 struct blk_trace *bt;
281
282 bt = xchg(&q->blk_trace, NULL);
283 if (!bt)
284 return -EINVAL;
285
Li Zefan55547202009-03-25 17:21:26 +0800286 if (bt->trace_state != Blktrace_running)
Jens Axboe2056a782006-03-23 20:00:26 +0100287 blk_trace_cleanup(bt);
288
289 return 0;
290}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100291EXPORT_SYMBOL_GPL(blk_trace_remove);
Jens Axboe2056a782006-03-23 20:00:26 +0100292
293static int blk_dropped_open(struct inode *inode, struct file *filp)
294{
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700295 filp->private_data = inode->i_private;
Jens Axboe2056a782006-03-23 20:00:26 +0100296
297 return 0;
298}
299
300static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
301 size_t count, loff_t *ppos)
302{
303 struct blk_trace *bt = filp->private_data;
304 char buf[16];
305
306 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
307
308 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
309}
310
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800311static const struct file_operations blk_dropped_fops = {
Jens Axboe2056a782006-03-23 20:00:26 +0100312 .owner = THIS_MODULE,
313 .open = blk_dropped_open,
314 .read = blk_dropped_read,
315};
316
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200317static int blk_msg_open(struct inode *inode, struct file *filp)
318{
319 filp->private_data = inode->i_private;
320
321 return 0;
322}
323
324static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
325 size_t count, loff_t *ppos)
326{
327 char *msg;
328 struct blk_trace *bt;
329
330 if (count > BLK_TN_MAX_MSG)
331 return -EINVAL;
332
333 msg = kmalloc(count, GFP_KERNEL);
334 if (msg == NULL)
335 return -ENOMEM;
336
337 if (copy_from_user(msg, buffer, count)) {
338 kfree(msg);
339 return -EFAULT;
340 }
341
342 bt = filp->private_data;
343 __trace_note_message(bt, "%s", msg);
344 kfree(msg);
345
346 return count;
347}
348
349static const struct file_operations blk_msg_fops = {
350 .owner = THIS_MODULE,
351 .open = blk_msg_open,
352 .write = blk_msg_write,
353};
354
Jens Axboe2056a782006-03-23 20:00:26 +0100355/*
356 * Keep track of how many times we encountered a full subbuffer, to aid
357 * the user space app in telling how many lost events there were.
358 */
359static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
360 void *prev_subbuf, size_t prev_padding)
361{
362 struct blk_trace *bt;
363
364 if (!relay_buf_full(buf))
365 return 1;
366
367 bt = buf->chan->private_data;
368 atomic_inc(&bt->dropped);
369 return 0;
370}
371
372static int blk_remove_buf_file_callback(struct dentry *dentry)
373{
Jens Axboef48fc4d2009-01-05 10:17:25 +0100374 struct dentry *parent = dentry->d_parent;
Jens Axboe2056a782006-03-23 20:00:26 +0100375 debugfs_remove(dentry);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100376
377 /*
378 * this will fail for all but the last file, but that is ok. what we
379 * care about is the top level buts->name directory going away, when
380 * the last trace file is gone. Then we don't have to rmdir() that
381 * manually on trace stop, so it nicely solves the issue with
382 * force killing of running traces.
383 */
384
385 debugfs_remove(parent);
Jens Axboe2056a782006-03-23 20:00:26 +0100386 return 0;
387}
388
389static struct dentry *blk_create_buf_file_callback(const char *filename,
390 struct dentry *parent,
391 int mode,
392 struct rchan_buf *buf,
393 int *is_global)
394{
395 return debugfs_create_file(filename, mode, parent, buf,
396 &relay_file_operations);
397}
398
399static struct rchan_callbacks blk_relay_callbacks = {
400 .subbuf_start = blk_subbuf_start_callback,
401 .create_buf_file = blk_create_buf_file_callback,
402 .remove_buf_file = blk_remove_buf_file_callback,
403};
404
405/*
406 * Setup everything required to start tracing
407 */
Christof Schmitt6da127a2008-01-11 10:09:43 +0100408int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
Arnd Bergmann171044d42007-10-09 13:23:53 +0200409 struct blk_user_trace_setup *buts)
Jens Axboe2056a782006-03-23 20:00:26 +0100410{
Jens Axboe2056a782006-03-23 20:00:26 +0100411 struct blk_trace *old_bt, *bt = NULL;
412 struct dentry *dir = NULL;
Jens Axboe2056a782006-03-23 20:00:26 +0100413 int ret, i;
414
Arnd Bergmann171044d42007-10-09 13:23:53 +0200415 if (!buts->buf_size || !buts->buf_nr)
Jens Axboe2056a782006-03-23 20:00:26 +0100416 return -EINVAL;
417
Jens Axboe0497b342008-10-01 16:16:25 +0200418 strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
419 buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
Jens Axboe2056a782006-03-23 20:00:26 +0100420
421 /*
422 * some device names have larger paths - convert the slashes
423 * to underscores for this to work as expected
424 */
Arnd Bergmann171044d42007-10-09 13:23:53 +0200425 for (i = 0; i < strlen(buts->name); i++)
426 if (buts->name[i] == '/')
427 buts->name[i] = '_';
Jens Axboe2056a782006-03-23 20:00:26 +0100428
Jens Axboe2056a782006-03-23 20:00:26 +0100429 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
430 if (!bt)
Li Zefanad5dd542009-03-27 10:20:24 +0800431 return -ENOMEM;
Jens Axboe2056a782006-03-23 20:00:26 +0100432
Li Zefanad5dd542009-03-27 10:20:24 +0800433 ret = -ENOMEM;
Jens Axboe2056a782006-03-23 20:00:26 +0100434 bt->sequence = alloc_percpu(unsigned long);
435 if (!bt->sequence)
436 goto err;
437
Ingo Molnarf0ef0392009-03-06 16:44:14 +0100438 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
Jens Axboe64565912008-05-28 14:45:33 +0200439 if (!bt->msg_data)
440 goto err;
441
Jens Axboe2056a782006-03-23 20:00:26 +0100442 ret = -ENOENT;
Jens Axboef48fc4d2009-01-05 10:17:25 +0100443
Li Zefanb5230b52009-03-25 17:19:33 +0800444 mutex_lock(&blk_tree_mutex);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100445 if (!blk_tree_root) {
446 blk_tree_root = debugfs_create_dir("block", NULL);
Li Zefanb5230b52009-03-25 17:19:33 +0800447 if (!blk_tree_root) {
448 mutex_unlock(&blk_tree_mutex);
Li Zefan1a176622009-03-20 09:47:30 +0800449 goto err;
Li Zefanb5230b52009-03-25 17:19:33 +0800450 }
Jens Axboef48fc4d2009-01-05 10:17:25 +0100451 }
Li Zefanb5230b52009-03-25 17:19:33 +0800452 mutex_unlock(&blk_tree_mutex);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100453
454 dir = debugfs_create_dir(buts->name, blk_tree_root);
455
Jens Axboe2056a782006-03-23 20:00:26 +0100456 if (!dir)
457 goto err;
458
459 bt->dir = dir;
Christof Schmitt6da127a2008-01-11 10:09:43 +0100460 bt->dev = dev;
Jens Axboe2056a782006-03-23 20:00:26 +0100461 atomic_set(&bt->dropped, 0);
462
463 ret = -EIO;
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200464 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt,
465 &blk_dropped_fops);
Jens Axboe2056a782006-03-23 20:00:26 +0100466 if (!bt->dropped_file)
467 goto err;
468
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200469 bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
470 if (!bt->msg_file)
471 goto err;
472
Arnd Bergmann171044d42007-10-09 13:23:53 +0200473 bt->rchan = relay_open("trace", dir, buts->buf_size,
474 buts->buf_nr, &blk_relay_callbacks, bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100475 if (!bt->rchan)
476 goto err;
Jens Axboe2056a782006-03-23 20:00:26 +0100477
Arnd Bergmann171044d42007-10-09 13:23:53 +0200478 bt->act_mask = buts->act_mask;
Jens Axboe2056a782006-03-23 20:00:26 +0100479 if (!bt->act_mask)
480 bt->act_mask = (u16) -1;
481
Arnd Bergmann171044d42007-10-09 13:23:53 +0200482 bt->start_lba = buts->start_lba;
483 bt->end_lba = buts->end_lba;
Jens Axboe2056a782006-03-23 20:00:26 +0100484 if (!bt->end_lba)
485 bt->end_lba = -1ULL;
486
Arnd Bergmann171044d42007-10-09 13:23:53 +0200487 bt->pid = buts->pid;
Jens Axboe2056a782006-03-23 20:00:26 +0100488 bt->trace_state = Blktrace_setup;
489
490 ret = -EBUSY;
491 old_bt = xchg(&q->blk_trace, bt);
492 if (old_bt) {
493 (void) xchg(&q->blk_trace, old_bt);
494 goto err;
495 }
496
Li Zefan17ba97e2009-03-27 10:20:09 +0800497 if (atomic_inc_return(&blk_probes_ref) == 1)
Li Zefancbe28292009-03-20 09:48:47 +0800498 blk_register_tracepoints();
499
Jens Axboe2056a782006-03-23 20:00:26 +0100500 return 0;
501err:
Li Zefanad5dd542009-03-27 10:20:24 +0800502 blk_trace_free(bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100503 return ret;
504}
505
Christof Schmitt6da127a2008-01-11 10:09:43 +0100506int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
507 char __user *arg)
Arnd Bergmann171044d42007-10-09 13:23:53 +0200508{
509 struct blk_user_trace_setup buts;
510 int ret;
511
512 ret = copy_from_user(&buts, arg, sizeof(buts));
513 if (ret)
514 return -EFAULT;
515
Christof Schmitt6da127a2008-01-11 10:09:43 +0100516 ret = do_blk_trace_setup(q, name, dev, &buts);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200517 if (ret)
518 return ret;
519
520 if (copy_to_user(arg, &buts, sizeof(buts)))
521 return -EFAULT;
522
523 return 0;
524}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100525EXPORT_SYMBOL_GPL(blk_trace_setup);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200526
Christof Schmitt6da127a2008-01-11 10:09:43 +0100527int blk_trace_startstop(struct request_queue *q, int start)
Jens Axboe2056a782006-03-23 20:00:26 +0100528{
Jens Axboe2056a782006-03-23 20:00:26 +0100529 int ret;
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200530 struct blk_trace *bt = q->blk_trace;
Jens Axboe2056a782006-03-23 20:00:26 +0100531
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200532 if (bt == NULL)
Jens Axboe2056a782006-03-23 20:00:26 +0100533 return -EINVAL;
534
535 /*
536 * For starting a trace, we can transition from a setup or stopped
537 * trace. For stopping a trace, the state must be running
538 */
539 ret = -EINVAL;
540 if (start) {
541 if (bt->trace_state == Blktrace_setup ||
542 bt->trace_state == Blktrace_stopped) {
543 blktrace_seq++;
544 smp_mb();
545 bt->trace_state = Blktrace_running;
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100546
547 trace_note_time(bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100548 ret = 0;
549 }
550 } else {
551 if (bt->trace_state == Blktrace_running) {
552 bt->trace_state = Blktrace_stopped;
553 relay_flush(bt->rchan);
554 ret = 0;
555 }
556 }
557
558 return ret;
559}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100560EXPORT_SYMBOL_GPL(blk_trace_startstop);
Jens Axboe2056a782006-03-23 20:00:26 +0100561
562/**
563 * blk_trace_ioctl: - handle the ioctls associated with tracing
564 * @bdev: the block device
Steven Rostedtef180122009-03-10 14:10:56 -0400565 * @cmd: the ioctl cmd
Jens Axboe2056a782006-03-23 20:00:26 +0100566 * @arg: the argument data, if any
567 *
568 **/
569int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
570{
Jens Axboe165125e2007-07-24 09:28:11 +0200571 struct request_queue *q;
Jens Axboe2056a782006-03-23 20:00:26 +0100572 int ret, start = 0;
Christof Schmitt6da127a2008-01-11 10:09:43 +0100573 char b[BDEVNAME_SIZE];
Jens Axboe2056a782006-03-23 20:00:26 +0100574
575 q = bdev_get_queue(bdev);
576 if (!q)
577 return -ENXIO;
578
579 mutex_lock(&bdev->bd_mutex);
580
581 switch (cmd) {
582 case BLKTRACESETUP:
Jean Delvaref36f21e2008-05-12 14:02:33 -0700583 bdevname(bdev, b);
Christof Schmitt6da127a2008-01-11 10:09:43 +0100584 ret = blk_trace_setup(q, b, bdev->bd_dev, arg);
Jens Axboe2056a782006-03-23 20:00:26 +0100585 break;
586 case BLKTRACESTART:
587 start = 1;
588 case BLKTRACESTOP:
589 ret = blk_trace_startstop(q, start);
590 break;
591 case BLKTRACETEARDOWN:
592 ret = blk_trace_remove(q);
593 break;
594 default:
595 ret = -ENOTTY;
596 break;
597 }
598
599 mutex_unlock(&bdev->bd_mutex);
600 return ret;
601}
602
603/**
604 * blk_trace_shutdown: - stop and cleanup trace structures
605 * @q: the request queue associated with the device
606 *
607 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200608void blk_trace_shutdown(struct request_queue *q)
Jens Axboe2056a782006-03-23 20:00:26 +0100609{
Alexey Dobriyan6c5c9342006-09-29 01:59:40 -0700610 if (q->blk_trace) {
611 blk_trace_startstop(q, 0);
612 blk_trace_remove(q);
613 }
Jens Axboe2056a782006-03-23 20:00:26 +0100614}
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100615
616/*
617 * blktrace probes
618 */
619
620/**
621 * blk_add_trace_rq - Add a trace for a request oriented action
622 * @q: queue the io is for
623 * @rq: the source request
624 * @what: the action
625 *
626 * Description:
627 * Records an action against a request. Will log the bio offset + size.
628 *
629 **/
630static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
631 u32 what)
632{
633 struct blk_trace *bt = q->blk_trace;
634 int rw = rq->cmd_flags & 0x03;
635
636 if (likely(!bt))
637 return;
638
639 if (blk_discard_rq(rq))
640 rw |= (1 << BIO_RW_DISCARD);
641
642 if (blk_pc_request(rq)) {
643 what |= BLK_TC_ACT(BLK_TC_PC);
644 __blk_add_trace(bt, 0, rq->data_len, rw, what, rq->errors,
645 sizeof(rq->cmd), rq->cmd);
646 } else {
647 what |= BLK_TC_ACT(BLK_TC_FS);
648 __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9,
649 rw, what, rq->errors, 0, NULL);
650 }
651}
652
653static void blk_add_trace_rq_abort(struct request_queue *q, struct request *rq)
654{
655 blk_add_trace_rq(q, rq, BLK_TA_ABORT);
656}
657
658static void blk_add_trace_rq_insert(struct request_queue *q, struct request *rq)
659{
660 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
661}
662
663static void blk_add_trace_rq_issue(struct request_queue *q, struct request *rq)
664{
665 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
666}
667
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200668static void blk_add_trace_rq_requeue(struct request_queue *q,
669 struct request *rq)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100670{
671 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
672}
673
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200674static void blk_add_trace_rq_complete(struct request_queue *q,
675 struct request *rq)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100676{
677 blk_add_trace_rq(q, rq, BLK_TA_COMPLETE);
678}
679
680/**
681 * blk_add_trace_bio - Add a trace for a bio oriented action
682 * @q: queue the io is for
683 * @bio: the source bio
684 * @what: the action
685 *
686 * Description:
687 * Records an action against a bio. Will log the bio offset + size.
688 *
689 **/
690static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
691 u32 what)
692{
693 struct blk_trace *bt = q->blk_trace;
694
695 if (likely(!bt))
696 return;
697
698 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what,
699 !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
700}
701
702static void blk_add_trace_bio_bounce(struct request_queue *q, struct bio *bio)
703{
704 blk_add_trace_bio(q, bio, BLK_TA_BOUNCE);
705}
706
707static void blk_add_trace_bio_complete(struct request_queue *q, struct bio *bio)
708{
709 blk_add_trace_bio(q, bio, BLK_TA_COMPLETE);
710}
711
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200712static void blk_add_trace_bio_backmerge(struct request_queue *q,
713 struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100714{
715 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
716}
717
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200718static void blk_add_trace_bio_frontmerge(struct request_queue *q,
719 struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100720{
721 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
722}
723
724static void blk_add_trace_bio_queue(struct request_queue *q, struct bio *bio)
725{
726 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
727}
728
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200729static void blk_add_trace_getrq(struct request_queue *q,
730 struct bio *bio, int rw)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100731{
732 if (bio)
733 blk_add_trace_bio(q, bio, BLK_TA_GETRQ);
734 else {
735 struct blk_trace *bt = q->blk_trace;
736
737 if (bt)
738 __blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL);
739 }
740}
741
742
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200743static void blk_add_trace_sleeprq(struct request_queue *q,
744 struct bio *bio, int rw)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100745{
746 if (bio)
747 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ);
748 else {
749 struct blk_trace *bt = q->blk_trace;
750
751 if (bt)
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200752 __blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ,
753 0, 0, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100754 }
755}
756
757static void blk_add_trace_plug(struct request_queue *q)
758{
759 struct blk_trace *bt = q->blk_trace;
760
761 if (bt)
762 __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
763}
764
765static void blk_add_trace_unplug_io(struct request_queue *q)
766{
767 struct blk_trace *bt = q->blk_trace;
768
769 if (bt) {
770 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
771 __be64 rpdu = cpu_to_be64(pdu);
772
773 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_IO, 0,
774 sizeof(rpdu), &rpdu);
775 }
776}
777
778static void blk_add_trace_unplug_timer(struct request_queue *q)
779{
780 struct blk_trace *bt = q->blk_trace;
781
782 if (bt) {
783 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
784 __be64 rpdu = cpu_to_be64(pdu);
785
786 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_TIMER, 0,
787 sizeof(rpdu), &rpdu);
788 }
789}
790
791static void blk_add_trace_split(struct request_queue *q, struct bio *bio,
792 unsigned int pdu)
793{
794 struct blk_trace *bt = q->blk_trace;
795
796 if (bt) {
797 __be64 rpdu = cpu_to_be64(pdu);
798
799 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
800 BLK_TA_SPLIT, !bio_flagged(bio, BIO_UPTODATE),
801 sizeof(rpdu), &rpdu);
802 }
803}
804
805/**
806 * blk_add_trace_remap - Add a trace for a remap operation
807 * @q: queue the io is for
808 * @bio: the source bio
809 * @dev: target device
810 * @from: source sector
811 * @to: target sector
812 *
813 * Description:
814 * Device mapper or raid target sometimes need to split a bio because
815 * it spans a stripe (or similar). Add a trace for that action.
816 *
817 **/
818static void blk_add_trace_remap(struct request_queue *q, struct bio *bio,
819 dev_t dev, sector_t from, sector_t to)
820{
821 struct blk_trace *bt = q->blk_trace;
822 struct blk_io_trace_remap r;
823
824 if (likely(!bt))
825 return;
826
827 r.device = cpu_to_be32(dev);
828 r.device_from = cpu_to_be32(bio->bi_bdev->bd_dev);
829 r.sector = cpu_to_be64(to);
830
831 __blk_add_trace(bt, from, bio->bi_size, bio->bi_rw, BLK_TA_REMAP,
832 !bio_flagged(bio, BIO_UPTODATE), sizeof(r), &r);
833}
834
835/**
836 * blk_add_driver_data - Add binary message with driver-specific data
837 * @q: queue the io is for
838 * @rq: io request
839 * @data: driver-specific data
840 * @len: length of driver-specific data
841 *
842 * Description:
843 * Some drivers might want to write driver-specific data per request.
844 *
845 **/
846void blk_add_driver_data(struct request_queue *q,
847 struct request *rq,
848 void *data, size_t len)
849{
850 struct blk_trace *bt = q->blk_trace;
851
852 if (likely(!bt))
853 return;
854
855 if (blk_pc_request(rq))
856 __blk_add_trace(bt, 0, rq->data_len, 0, BLK_TA_DRV_DATA,
857 rq->errors, len, data);
858 else
859 __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9,
860 0, BLK_TA_DRV_DATA, rq->errors, len, data);
861}
862EXPORT_SYMBOL_GPL(blk_add_driver_data);
863
Li Zefan3c289ba2009-03-20 09:48:26 +0800864static void blk_register_tracepoints(void)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100865{
866 int ret;
867
868 ret = register_trace_block_rq_abort(blk_add_trace_rq_abort);
869 WARN_ON(ret);
870 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert);
871 WARN_ON(ret);
872 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue);
873 WARN_ON(ret);
874 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue);
875 WARN_ON(ret);
876 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete);
877 WARN_ON(ret);
878 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce);
879 WARN_ON(ret);
880 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete);
881 WARN_ON(ret);
882 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
883 WARN_ON(ret);
884 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
885 WARN_ON(ret);
886 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue);
887 WARN_ON(ret);
888 ret = register_trace_block_getrq(blk_add_trace_getrq);
889 WARN_ON(ret);
890 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq);
891 WARN_ON(ret);
892 ret = register_trace_block_plug(blk_add_trace_plug);
893 WARN_ON(ret);
894 ret = register_trace_block_unplug_timer(blk_add_trace_unplug_timer);
895 WARN_ON(ret);
896 ret = register_trace_block_unplug_io(blk_add_trace_unplug_io);
897 WARN_ON(ret);
898 ret = register_trace_block_split(blk_add_trace_split);
899 WARN_ON(ret);
900 ret = register_trace_block_remap(blk_add_trace_remap);
901 WARN_ON(ret);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100902}
903
904static void blk_unregister_tracepoints(void)
905{
906 unregister_trace_block_remap(blk_add_trace_remap);
907 unregister_trace_block_split(blk_add_trace_split);
908 unregister_trace_block_unplug_io(blk_add_trace_unplug_io);
909 unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer);
910 unregister_trace_block_plug(blk_add_trace_plug);
911 unregister_trace_block_sleeprq(blk_add_trace_sleeprq);
912 unregister_trace_block_getrq(blk_add_trace_getrq);
913 unregister_trace_block_bio_queue(blk_add_trace_bio_queue);
914 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
915 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
916 unregister_trace_block_bio_complete(blk_add_trace_bio_complete);
917 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce);
918 unregister_trace_block_rq_complete(blk_add_trace_rq_complete);
919 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue);
920 unregister_trace_block_rq_issue(blk_add_trace_rq_issue);
921 unregister_trace_block_rq_insert(blk_add_trace_rq_insert);
922 unregister_trace_block_rq_abort(blk_add_trace_rq_abort);
923
924 tracepoint_synchronize_unregister();
925}
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200926
927/*
928 * struct blk_io_tracer formatting routines
929 */
930
931static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
932{
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200933 int i = 0;
Li Zefan65796342009-03-24 16:05:06 +0800934 int tc = t->action >> BLK_TC_SHIFT;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200935
Li Zefan18cea452009-03-27 10:21:54 +0800936 if (t->action == BLK_TN_MESSAGE) {
937 rwbs[i++] = 'N';
938 goto out;
939 }
940
Li Zefan65796342009-03-24 16:05:06 +0800941 if (tc & BLK_TC_DISCARD)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200942 rwbs[i++] = 'D';
Li Zefan65796342009-03-24 16:05:06 +0800943 else if (tc & BLK_TC_WRITE)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200944 rwbs[i++] = 'W';
945 else if (t->bytes)
946 rwbs[i++] = 'R';
947 else
948 rwbs[i++] = 'N';
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200949
Li Zefan65796342009-03-24 16:05:06 +0800950 if (tc & BLK_TC_AHEAD)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200951 rwbs[i++] = 'A';
Li Zefan65796342009-03-24 16:05:06 +0800952 if (tc & BLK_TC_BARRIER)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200953 rwbs[i++] = 'B';
Li Zefan65796342009-03-24 16:05:06 +0800954 if (tc & BLK_TC_SYNC)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200955 rwbs[i++] = 'S';
Li Zefan65796342009-03-24 16:05:06 +0800956 if (tc & BLK_TC_META)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200957 rwbs[i++] = 'M';
Li Zefan18cea452009-03-27 10:21:54 +0800958out:
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -0200959 rwbs[i] = '\0';
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200960}
961
962static inline
963const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
964{
965 return (const struct blk_io_trace *)ent;
966}
967
968static inline const void *pdu_start(const struct trace_entry *ent)
969{
970 return te_blk_io_trace(ent) + 1;
971}
972
973static inline u32 t_sec(const struct trace_entry *ent)
974{
975 return te_blk_io_trace(ent)->bytes >> 9;
976}
977
978static inline unsigned long long t_sector(const struct trace_entry *ent)
979{
980 return te_blk_io_trace(ent)->sector;
981}
982
983static inline __u16 t_error(const struct trace_entry *ent)
984{
Li Zefane0dc81b2009-03-24 16:05:51 +0800985 return te_blk_io_trace(ent)->error;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200986}
987
988static __u64 get_pdu_int(const struct trace_entry *ent)
989{
990 const __u64 *val = pdu_start(ent);
991 return be64_to_cpu(*val);
992}
993
994static void get_pdu_remap(const struct trace_entry *ent,
995 struct blk_io_trace_remap *r)
996{
997 const struct blk_io_trace_remap *__r = pdu_start(ent);
998 __u64 sector = __r->sector;
999
1000 r->device = be32_to_cpu(__r->device);
1001 r->device_from = be32_to_cpu(__r->device_from);
1002 r->sector = be64_to_cpu(sector);
1003}
1004
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001005typedef int (blk_log_action_t) (struct trace_iterator *iter, const char *act);
1006
1007static int blk_log_action_classic(struct trace_iterator *iter, const char *act)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001008{
1009 char rwbs[6];
Li Zefan35ac51b2009-03-27 10:19:46 +08001010 unsigned long long ts = iter->ts;
1011 unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001012 unsigned secs = (unsigned long)ts;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001013 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001014
1015 fill_rwbs(rwbs, t);
1016
1017 return trace_seq_printf(&iter->seq,
Li Zefan35ac51b2009-03-27 10:19:46 +08001018 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001019 MAJOR(t->device), MINOR(t->device), iter->cpu,
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001020 secs, nsec_rem, iter->ent->pid, act, rwbs);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001021}
1022
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001023static int blk_log_action(struct trace_iterator *iter, const char *act)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001024{
1025 char rwbs[6];
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001026 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1027
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001028 fill_rwbs(rwbs, t);
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001029 return trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001030 MAJOR(t->device), MINOR(t->device), act, rwbs);
1031}
1032
1033static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1034{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001035 char cmd[TASK_COMM_LEN];
1036
1037 trace_find_cmdline(ent->pid, cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001038
1039 if (t_sec(ent))
1040 return trace_seq_printf(s, "%llu + %u [%s]\n",
1041 t_sector(ent), t_sec(ent), cmd);
1042 return trace_seq_printf(s, "[%s]\n", cmd);
1043}
1044
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001045static int blk_log_with_error(struct trace_seq *s,
1046 const struct trace_entry *ent)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001047{
1048 if (t_sec(ent))
1049 return trace_seq_printf(s, "%llu + %u [%d]\n", t_sector(ent),
1050 t_sec(ent), t_error(ent));
1051 return trace_seq_printf(s, "%llu [%d]\n", t_sector(ent), t_error(ent));
1052}
1053
1054static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1055{
1056 struct blk_io_trace_remap r = { .device = 0, };
1057
1058 get_pdu_remap(ent, &r);
1059 return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
1060 t_sector(ent),
1061 t_sec(ent), MAJOR(r.device), MINOR(r.device),
1062 (unsigned long long)r.sector);
1063}
1064
1065static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1066{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001067 char cmd[TASK_COMM_LEN];
1068
1069 trace_find_cmdline(ent->pid, cmd);
1070
1071 return trace_seq_printf(s, "[%s]\n", cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001072}
1073
1074static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1075{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001076 char cmd[TASK_COMM_LEN];
1077
1078 trace_find_cmdline(ent->pid, cmd);
1079
1080 return trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent));
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001081}
1082
1083static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1084{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001085 char cmd[TASK_COMM_LEN];
1086
1087 trace_find_cmdline(ent->pid, cmd);
1088
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001089 return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
Steven Rostedt4ca530852009-03-16 19:20:15 -04001090 get_pdu_int(ent), cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001091}
1092
Li Zefan18cea452009-03-27 10:21:54 +08001093static int blk_log_msg(struct trace_seq *s, const struct trace_entry *ent)
1094{
1095 int ret;
1096 const struct blk_io_trace *t = te_blk_io_trace(ent);
1097
1098 ret = trace_seq_putmem(s, t + 1, t->pdu_len);
1099 if (ret)
1100 return trace_seq_putc(s, '\n');
1101 return ret;
1102}
1103
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001104/*
1105 * struct tracer operations
1106 */
1107
1108static void blk_tracer_print_header(struct seq_file *m)
1109{
1110 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1111 return;
1112 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1113 "# | | | | | |\n");
1114}
1115
1116static void blk_tracer_start(struct trace_array *tr)
1117{
Li Zefanad5dd542009-03-27 10:20:24 +08001118 blk_tracer_enabled = true;
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001119 trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001120}
1121
1122static int blk_tracer_init(struct trace_array *tr)
1123{
1124 blk_tr = tr;
1125 blk_tracer_start(tr);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001126 return 0;
1127}
1128
1129static void blk_tracer_stop(struct trace_array *tr)
1130{
Li Zefanad5dd542009-03-27 10:20:24 +08001131 blk_tracer_enabled = false;
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001132 trace_flags |= TRACE_ITER_CONTEXT_INFO;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001133}
1134
1135static void blk_tracer_reset(struct trace_array *tr)
1136{
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001137 blk_tracer_stop(tr);
1138}
1139
Li Zefane4955c92009-03-24 16:04:37 +08001140static const struct {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001141 const char *act[2];
Steven Rostedtef180122009-03-10 14:10:56 -04001142 int (*print)(struct trace_seq *s, const struct trace_entry *ent);
Li Zefane4955c92009-03-24 16:04:37 +08001143} what2act[] = {
Steven Rostedtef180122009-03-10 14:10:56 -04001144 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001145 [__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
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001161static enum print_line_t print_one_line(struct trace_iterator *iter,
1162 bool classic)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001163{
Arnaldo Carvalho de Melo2c9b2382009-02-02 20:30:12 -02001164 struct trace_seq *s = &iter->seq;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001165 const struct blk_io_trace *t;
1166 u16 what;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001167 int ret;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001168 bool long_act;
1169 blk_log_action_t *log_action;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001170
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001171 t = te_blk_io_trace(iter->ent);
1172 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1173 long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1174 log_action = classic ? &blk_log_action_classic : &blk_log_action;
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001175
Li Zefan18cea452009-03-27 10:21:54 +08001176 if (t->action == BLK_TN_MESSAGE) {
1177 ret = log_action(iter, long_act ? "message" : "m");
1178 if (ret)
1179 ret = blk_log_msg(s, iter->ent);
1180 goto out;
1181 }
1182
Li Zefaneb08f8e2009-03-24 16:05:27 +08001183 if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001184 ret = trace_seq_printf(s, "Bad pc action %x\n", what);
1185 else {
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001186 ret = log_action(iter, what2act[what].act[long_act]);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001187 if (ret)
Arnaldo Carvalho de Melo2c9b2382009-02-02 20:30:12 -02001188 ret = what2act[what].print(s, iter->ent);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001189 }
Li Zefan18cea452009-03-27 10:21:54 +08001190out:
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001191 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1192}
1193
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001194static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
1195 int flags)
1196{
1197 if (!trace_print_context(iter))
1198 return TRACE_TYPE_PARTIAL_LINE;
1199
1200 return print_one_line(iter, false);
1201}
1202
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001203static int blk_trace_synthesize_old_trace(struct trace_iterator *iter)
1204{
1205 struct trace_seq *s = &iter->seq;
1206 struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1207 const int offset = offsetof(struct blk_io_trace, sector);
1208 struct blk_io_trace old = {
1209 .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
Li Zefan6c051ce2009-03-25 17:18:56 +08001210 .time = iter->ts,
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001211 };
1212
1213 if (!trace_seq_putmem(s, &old, offset))
1214 return 0;
1215 return trace_seq_putmem(s, &t->sector,
1216 sizeof(old) - offset + t->pdu_len);
1217}
1218
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -02001219static enum print_line_t
1220blk_trace_event_print_binary(struct trace_iterator *iter, int flags)
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001221{
1222 return blk_trace_synthesize_old_trace(iter) ?
1223 TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1224}
1225
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001226static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1227{
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001228 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1229 return TRACE_TYPE_UNHANDLED;
1230
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001231 return print_one_line(iter, true);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001232}
1233
1234static struct tracer blk_tracer __read_mostly = {
1235 .name = "blk",
1236 .init = blk_tracer_init,
1237 .reset = blk_tracer_reset,
1238 .start = blk_tracer_start,
1239 .stop = blk_tracer_stop,
1240 .print_header = blk_tracer_print_header,
1241 .print_line = blk_tracer_print_line,
1242 .flags = &blk_tracer_flags,
1243};
1244
1245static struct trace_event trace_blk_event = {
Steven Rostedtef180122009-03-10 14:10:56 -04001246 .type = TRACE_BLK,
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001247 .trace = blk_trace_event_print,
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001248 .binary = blk_trace_event_print_binary,
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001249};
1250
1251static int __init init_blk_tracer(void)
1252{
1253 if (!register_ftrace_event(&trace_blk_event)) {
1254 pr_warning("Warning: could not register block events\n");
1255 return 1;
1256 }
1257
1258 if (register_tracer(&blk_tracer) != 0) {
1259 pr_warning("Warning: could not register the block tracer\n");
1260 unregister_ftrace_event(&trace_blk_event);
1261 return 1;
1262 }
1263
1264 return 0;
1265}
1266
1267device_initcall(init_blk_tracer);
1268
1269static int blk_trace_remove_queue(struct request_queue *q)
1270{
1271 struct blk_trace *bt;
1272
1273 bt = xchg(&q->blk_trace, NULL);
1274 if (bt == NULL)
1275 return -EINVAL;
1276
Li Zefan17ba97e2009-03-27 10:20:09 +08001277 if (atomic_dec_and_test(&blk_probes_ref))
1278 blk_unregister_tracepoints();
1279
Li Zefanad5dd542009-03-27 10:20:24 +08001280 blk_trace_free(bt);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001281 return 0;
1282}
1283
1284/*
1285 * Setup everything required to start tracing
1286 */
1287static int blk_trace_setup_queue(struct request_queue *q, dev_t dev)
1288{
1289 struct blk_trace *old_bt, *bt = NULL;
Li Zefan18cea452009-03-27 10:21:54 +08001290 int ret = -ENOMEM;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001291
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001292 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1293 if (!bt)
Li Zefan15152e42009-03-20 09:49:08 +08001294 return -ENOMEM;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001295
Li Zefan18cea452009-03-27 10:21:54 +08001296 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
1297 if (!bt->msg_data)
1298 goto free_bt;
1299
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001300 bt->dev = dev;
1301 bt->act_mask = (u16)-1;
1302 bt->end_lba = -1ULL;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001303
1304 old_bt = xchg(&q->blk_trace, bt);
1305 if (old_bt != NULL) {
1306 (void)xchg(&q->blk_trace, old_bt);
Li Zefan18cea452009-03-27 10:21:54 +08001307 ret = -EBUSY;
1308 goto free_bt;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001309 }
Li Zefan15152e42009-03-20 09:49:08 +08001310
Li Zefan17ba97e2009-03-27 10:20:09 +08001311 if (atomic_inc_return(&blk_probes_ref) == 1)
1312 blk_register_tracepoints();
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001313 return 0;
Li Zefan18cea452009-03-27 10:21:54 +08001314
1315free_bt:
1316 blk_trace_free(bt);
1317 return ret;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001318}
1319
1320/*
1321 * sysfs interface to enable and configure tracing
1322 */
1323
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001324static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1325 struct device_attribute *attr,
1326 char *buf);
1327static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1328 struct device_attribute *attr,
1329 const char *buf, size_t count);
1330#define BLK_TRACE_DEVICE_ATTR(_name) \
1331 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1332 sysfs_blk_trace_attr_show, \
1333 sysfs_blk_trace_attr_store)
1334
Li Zefancd649b82009-03-20 11:33:55 +08001335static BLK_TRACE_DEVICE_ATTR(enable);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001336static BLK_TRACE_DEVICE_ATTR(act_mask);
1337static BLK_TRACE_DEVICE_ATTR(pid);
1338static BLK_TRACE_DEVICE_ATTR(start_lba);
1339static BLK_TRACE_DEVICE_ATTR(end_lba);
1340
1341static struct attribute *blk_trace_attrs[] = {
1342 &dev_attr_enable.attr,
1343 &dev_attr_act_mask.attr,
1344 &dev_attr_pid.attr,
1345 &dev_attr_start_lba.attr,
1346 &dev_attr_end_lba.attr,
1347 NULL
1348};
1349
1350struct attribute_group blk_trace_attr_group = {
1351 .name = "trace",
1352 .attrs = blk_trace_attrs,
1353};
1354
Li Zefan09341992009-03-24 17:43:30 +08001355static const struct {
1356 int mask;
1357 const char *str;
1358} mask_maps[] = {
1359 { BLK_TC_READ, "read" },
1360 { BLK_TC_WRITE, "write" },
1361 { BLK_TC_BARRIER, "barrier" },
1362 { BLK_TC_SYNC, "sync" },
1363 { BLK_TC_QUEUE, "queue" },
1364 { BLK_TC_REQUEUE, "requeue" },
1365 { BLK_TC_ISSUE, "issue" },
1366 { BLK_TC_COMPLETE, "complete" },
1367 { BLK_TC_FS, "fs" },
1368 { BLK_TC_PC, "pc" },
1369 { BLK_TC_AHEAD, "ahead" },
1370 { BLK_TC_META, "meta" },
1371 { BLK_TC_DISCARD, "discard" },
1372 { BLK_TC_DRV_DATA, "drv_data" },
1373};
1374
1375static int blk_trace_str2mask(const char *str)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001376{
Li Zefan09341992009-03-24 17:43:30 +08001377 int i;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001378 int mask = 0;
Li Zefan09341992009-03-24 17:43:30 +08001379 char *s, *token;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001380
Li Zefan09341992009-03-24 17:43:30 +08001381 s = kstrdup(str, GFP_KERNEL);
1382 if (s == NULL)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001383 return -ENOMEM;
Li Zefan09341992009-03-24 17:43:30 +08001384 s = strstrip(s);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001385
1386 while (1) {
Li Zefan09341992009-03-24 17:43:30 +08001387 token = strsep(&s, ",");
1388 if (token == NULL)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001389 break;
1390
Li Zefan09341992009-03-24 17:43:30 +08001391 if (*token == '\0')
1392 continue;
1393
1394 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1395 if (strcasecmp(token, mask_maps[i].str) == 0) {
1396 mask |= mask_maps[i].mask;
1397 break;
1398 }
1399 }
1400 if (i == ARRAY_SIZE(mask_maps)) {
1401 mask = -EINVAL;
1402 break;
1403 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001404 }
Li Zefan09341992009-03-24 17:43:30 +08001405 kfree(s);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001406
1407 return mask;
1408}
1409
Li Zefan09341992009-03-24 17:43:30 +08001410static ssize_t blk_trace_mask2str(char *buf, int mask)
1411{
1412 int i;
1413 char *p = buf;
1414
1415 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1416 if (mask & mask_maps[i].mask) {
1417 p += sprintf(p, "%s%s",
1418 (p == buf) ? "" : ",", mask_maps[i].str);
1419 }
1420 }
1421 *p++ = '\n';
1422
1423 return p - buf;
1424}
1425
Li Zefanb1251302009-03-20 10:34:00 +08001426static struct request_queue *blk_trace_get_queue(struct block_device *bdev)
1427{
1428 if (bdev->bd_disk == NULL)
1429 return NULL;
1430
1431 return bdev_get_queue(bdev);
1432}
1433
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001434static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1435 struct device_attribute *attr,
1436 char *buf)
1437{
1438 struct hd_struct *p = dev_to_part(dev);
1439 struct request_queue *q;
1440 struct block_device *bdev;
1441 ssize_t ret = -ENXIO;
1442
1443 lock_kernel();
1444 bdev = bdget(part_devt(p));
1445 if (bdev == NULL)
1446 goto out_unlock_kernel;
1447
Li Zefanb1251302009-03-20 10:34:00 +08001448 q = blk_trace_get_queue(bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001449 if (q == NULL)
1450 goto out_bdput;
Li Zefanb1251302009-03-20 10:34:00 +08001451
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001452 mutex_lock(&bdev->bd_mutex);
Li Zefancd649b82009-03-20 11:33:55 +08001453
1454 if (attr == &dev_attr_enable) {
1455 ret = sprintf(buf, "%u\n", !!q->blk_trace);
1456 goto out_unlock_bdev;
1457 }
1458
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001459 if (q->blk_trace == NULL)
1460 ret = sprintf(buf, "disabled\n");
1461 else if (attr == &dev_attr_act_mask)
Li Zefan09341992009-03-24 17:43:30 +08001462 ret = blk_trace_mask2str(buf, q->blk_trace->act_mask);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001463 else if (attr == &dev_attr_pid)
1464 ret = sprintf(buf, "%u\n", q->blk_trace->pid);
1465 else if (attr == &dev_attr_start_lba)
1466 ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
1467 else if (attr == &dev_attr_end_lba)
1468 ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
Li Zefancd649b82009-03-20 11:33:55 +08001469
1470out_unlock_bdev:
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001471 mutex_unlock(&bdev->bd_mutex);
1472out_bdput:
1473 bdput(bdev);
1474out_unlock_kernel:
1475 unlock_kernel();
1476 return ret;
1477}
1478
1479static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1480 struct device_attribute *attr,
1481 const char *buf, size_t count)
1482{
1483 struct block_device *bdev;
1484 struct request_queue *q;
1485 struct hd_struct *p;
1486 u64 value;
Li Zefan09341992009-03-24 17:43:30 +08001487 ssize_t ret = -EINVAL;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001488
1489 if (count == 0)
1490 goto out;
1491
1492 if (attr == &dev_attr_act_mask) {
1493 if (sscanf(buf, "%llx", &value) != 1) {
1494 /* Assume it is a list of trace category names */
Li Zefan09341992009-03-24 17:43:30 +08001495 ret = blk_trace_str2mask(buf);
1496 if (ret < 0)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001497 goto out;
Li Zefan09341992009-03-24 17:43:30 +08001498 value = ret;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001499 }
1500 } else if (sscanf(buf, "%llu", &value) != 1)
1501 goto out;
1502
Li Zefan09341992009-03-24 17:43:30 +08001503 ret = -ENXIO;
1504
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001505 lock_kernel();
1506 p = dev_to_part(dev);
1507 bdev = bdget(part_devt(p));
1508 if (bdev == NULL)
1509 goto out_unlock_kernel;
1510
Li Zefanb1251302009-03-20 10:34:00 +08001511 q = blk_trace_get_queue(bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001512 if (q == NULL)
1513 goto out_bdput;
1514
1515 mutex_lock(&bdev->bd_mutex);
Li Zefancd649b82009-03-20 11:33:55 +08001516
1517 if (attr == &dev_attr_enable) {
1518 if (value)
1519 ret = blk_trace_setup_queue(q, bdev->bd_dev);
1520 else
1521 ret = blk_trace_remove_queue(q);
1522 goto out_unlock_bdev;
1523 }
1524
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001525 ret = 0;
1526 if (q->blk_trace == NULL)
1527 ret = blk_trace_setup_queue(q, bdev->bd_dev);
1528
1529 if (ret == 0) {
1530 if (attr == &dev_attr_act_mask)
1531 q->blk_trace->act_mask = value;
1532 else if (attr == &dev_attr_pid)
1533 q->blk_trace->pid = value;
1534 else if (attr == &dev_attr_start_lba)
1535 q->blk_trace->start_lba = value;
1536 else if (attr == &dev_attr_end_lba)
1537 q->blk_trace->end_lba = value;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001538 }
Li Zefancd649b82009-03-20 11:33:55 +08001539
1540out_unlock_bdev:
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001541 mutex_unlock(&bdev->bd_mutex);
1542out_bdput:
1543 bdput(bdev);
1544out_unlock_kernel:
1545 unlock_kernel();
1546out:
Li Zefancd649b82009-03-20 11:33:55 +08001547 return ret ? ret : count;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001548}
Li Zefancd649b82009-03-20 11:33:55 +08001549