ring_buffer: remove unused flags parameter
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / block / blktrace.c
CommitLineData
2056a782 1/*
0fe23479 2 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
2056a782
JA
3 *
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 */
2056a782
JA
18#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>
be1c6341 25#include <linux/time.h>
5f3ea37c 26#include <trace/block.h>
939b3669 27#include <linux/uaccess.h>
c71a8961 28#include <../kernel/trace/trace_output.h>
2056a782 29
2056a782
JA
30static unsigned int blktrace_seq __read_mostly = 1;
31
c71a8961
ACM
32static 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 */
157f9c00 40 { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },
c71a8961
ACM
41 { }
42};
43
44static struct tracer_flags blk_tracer_flags = {
45 .val = 0,
46 .opts = blk_tracer_opts,
47};
48
5f3ea37c
ACM
49/* 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
be1c6341
OK
56/*
57 * Send out a notify message.
58 */
a863055b
JA
59static void trace_note(struct blk_trace *bt, pid_t pid, int action,
60 const void *data, size_t len)
be1c6341
OK
61{
62 struct blk_io_trace *t;
be1c6341 63
c71a8961
ACM
64 if (!bt->rchan)
65 return;
66
be1c6341 67 t = relay_reserve(bt->rchan, sizeof(*t) + len);
d3d9d2a5
JA
68 if (t) {
69 const int cpu = smp_processor_id();
70
71 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
2997c8c4 72 t->time = ktime_to_ns(ktime_get());
d3d9d2a5
JA
73 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 }
be1c6341
OK
80}
81
2056a782
JA
82/*
83 * 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{
a863055b
JA
88 tsk->btrace_seq = blktrace_seq;
89 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm));
be1c6341 90}
2056a782 91
be1c6341
OK
92static 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);
2056a782
JA
105}
106
9d5f09a4
AB
107void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
108{
109 int n;
110 va_list args;
14a73f54 111 unsigned long flags;
64565911 112 char *buf;
9d5f09a4 113
c71a8961
ACM
114 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
14a73f54 124 local_irq_save(flags);
64565911 125 buf = per_cpu_ptr(bt->msg_data, smp_processor_id());
9d5f09a4 126 va_start(args, fmt);
64565911 127 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
9d5f09a4
AB
128 va_end(args);
129
64565911 130 trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
14a73f54 131 local_irq_restore(flags);
9d5f09a4
AB
132}
133EXPORT_SYMBOL_GPL(__trace_note_message);
134
2056a782
JA
135static 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 */
939b3669
ACM
151static u32 ddir_act[2] __read_mostly = { BLK_TC_ACT(BLK_TC_READ),
152 BLK_TC_ACT(BLK_TC_WRITE) };
2056a782 153
35ba8f70 154/* The ilog2() calls fall out because they're constant */
939b3669
ACM
155#define MASK_TC_BIT(rw, __name) ((rw & (1 << BIO_RW_ ## __name)) << \
156 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name))
2056a782
JA
157
158/*
159 * The worker for the various blk_add_trace*() types. Fills out a
160 * blk_io_trace structure and places it in a per-cpu subbuffer.
161 */
5f3ea37c 162static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
2056a782
JA
163 int rw, u32 what, int error, int pdu_len, void *pdu_data)
164{
165 struct task_struct *tsk = current;
c71a8961 166 struct ring_buffer_event *event = NULL;
2056a782 167 struct blk_io_trace *t;
0a987751 168 unsigned long flags = 0;
2056a782
JA
169 unsigned long *sequence;
170 pid_t pid;
c71a8961 171 int cpu, pc = 0;
2056a782 172
157f9c00
ACM
173 if (unlikely(bt->trace_state != Blktrace_running ||
174 !blk_tracer_enabled))
2056a782
JA
175 return;
176
177 what |= ddir_act[rw & WRITE];
35ba8f70
DW
178 what |= MASK_TC_BIT(rw, BARRIER);
179 what |= MASK_TC_BIT(rw, SYNC);
180 what |= MASK_TC_BIT(rw, AHEAD);
181 what |= MASK_TC_BIT(rw, META);
182 what |= MASK_TC_BIT(rw, DISCARD);
2056a782
JA
183
184 pid = tsk->pid;
185 if (unlikely(act_log_check(bt, what, sector, pid)))
186 return;
c71a8961
ACM
187 cpu = raw_smp_processor_id();
188
189 if (blk_tr) {
190 struct trace_entry *ent;
191 tracing_record_cmdline(current);
192
193 event = ring_buffer_lock_reserve(blk_tr->buffer,
0a987751 194 sizeof(*t) + pdu_len);
c71a8961
ACM
195 if (!event)
196 return;
157f9c00 197
c71a8961
ACM
198 ent = ring_buffer_event_data(event);
199 t = (struct blk_io_trace *)ent;
200 pc = preempt_count();
201 tracing_generic_entry_update(ent, 0, pc);
202 ent->type = TRACE_BLK;
203 goto record_it;
204 }
2056a782
JA
205
206 /*
207 * A word about the locking here - we disable interrupts to reserve
208 * some space in the relay per-cpu buffer, to prevent an irq
14a73f54 209 * from coming in and stepping on our toes.
2056a782
JA
210 */
211 local_irq_save(flags);
212
213 if (unlikely(tsk->btrace_seq != blktrace_seq))
214 trace_note_tsk(bt, tsk);
215
216 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
217 if (t) {
2056a782
JA
218 sequence = per_cpu_ptr(bt->sequence, cpu);
219
220 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
221 t->sequence = ++(*sequence);
2997c8c4 222 t->time = ktime_to_ns(ktime_get());
c71a8961 223record_it:
08a06b83 224 /*
939b3669
ACM
225 * These two are not needed in ftrace as they are in the
226 * generic trace_entry, filled by tracing_generic_entry_update,
227 * but for the trace_event->bin() synthesizer benefit we do it
228 * here too.
229 */
230 t->cpu = cpu;
231 t->pid = pid;
08a06b83 232
2056a782
JA
233 t->sector = sector;
234 t->bytes = bytes;
235 t->action = what;
2056a782 236 t->device = bt->dev;
2056a782
JA
237 t->error = error;
238 t->pdu_len = pdu_len;
239
240 if (pdu_len)
241 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
c71a8961
ACM
242
243 if (blk_tr) {
0a987751 244 ring_buffer_unlock_commit(blk_tr->buffer, event);
c71a8961 245 if (pid != 0 &&
157f9c00 246 !(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC) &&
c71a8961 247 (trace_flags & TRACE_ITER_STACKTRACE) != 0)
0a987751 248 __trace_stack(blk_tr, 0, 5, pc);
c71a8961
ACM
249 trace_wake_up();
250 return;
251 }
2056a782
JA
252 }
253
254 local_irq_restore(flags);
255}
256
2056a782 257static struct dentry *blk_tree_root;
11a57153 258static DEFINE_MUTEX(blk_tree_mutex);
2056a782
JA
259
260static void blk_trace_cleanup(struct blk_trace *bt)
261{
02c62304 262 debugfs_remove(bt->msg_file);
2056a782 263 debugfs_remove(bt->dropped_file);
f48fc4d3 264 relay_close(bt->rchan);
2056a782 265 free_percpu(bt->sequence);
64565911 266 free_percpu(bt->msg_data);
2056a782 267 kfree(bt);
5f3ea37c
ACM
268 mutex_lock(&blk_probe_mutex);
269 if (atomic_dec_and_test(&blk_probes_ref))
270 blk_unregister_tracepoints();
271 mutex_unlock(&blk_probe_mutex);
2056a782
JA
272}
273
6da127ad 274int blk_trace_remove(struct request_queue *q)
2056a782
JA
275{
276 struct blk_trace *bt;
277
278 bt = xchg(&q->blk_trace, NULL);
279 if (!bt)
280 return -EINVAL;
281
282 if (bt->trace_state == Blktrace_setup ||
283 bt->trace_state == Blktrace_stopped)
284 blk_trace_cleanup(bt);
285
286 return 0;
287}
6da127ad 288EXPORT_SYMBOL_GPL(blk_trace_remove);
2056a782
JA
289
290static int blk_dropped_open(struct inode *inode, struct file *filp)
291{
8e18e294 292 filp->private_data = inode->i_private;
2056a782
JA
293
294 return 0;
295}
296
297static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
298 size_t count, loff_t *ppos)
299{
300 struct blk_trace *bt = filp->private_data;
301 char buf[16];
302
303 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
304
305 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
306}
307
2b8693c0 308static const struct file_operations blk_dropped_fops = {
2056a782
JA
309 .owner = THIS_MODULE,
310 .open = blk_dropped_open,
311 .read = blk_dropped_read,
312};
313
02c62304
AB
314static int blk_msg_open(struct inode *inode, struct file *filp)
315{
316 filp->private_data = inode->i_private;
317
318 return 0;
319}
320
321static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
322 size_t count, loff_t *ppos)
323{
324 char *msg;
325 struct blk_trace *bt;
326
327 if (count > BLK_TN_MAX_MSG)
328 return -EINVAL;
329
330 msg = kmalloc(count, GFP_KERNEL);
331 if (msg == NULL)
332 return -ENOMEM;
333
334 if (copy_from_user(msg, buffer, count)) {
335 kfree(msg);
336 return -EFAULT;
337 }
338
339 bt = filp->private_data;
340 __trace_note_message(bt, "%s", msg);
341 kfree(msg);
342
343 return count;
344}
345
346static const struct file_operations blk_msg_fops = {
347 .owner = THIS_MODULE,
348 .open = blk_msg_open,
349 .write = blk_msg_write,
350};
351
2056a782
JA
352/*
353 * Keep track of how many times we encountered a full subbuffer, to aid
354 * the user space app in telling how many lost events there were.
355 */
356static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
357 void *prev_subbuf, size_t prev_padding)
358{
359 struct blk_trace *bt;
360
361 if (!relay_buf_full(buf))
362 return 1;
363
364 bt = buf->chan->private_data;
365 atomic_inc(&bt->dropped);
366 return 0;
367}
368
369static int blk_remove_buf_file_callback(struct dentry *dentry)
370{
f48fc4d3 371 struct dentry *parent = dentry->d_parent;
2056a782 372 debugfs_remove(dentry);
f48fc4d3
JA
373
374 /*
375 * this will fail for all but the last file, but that is ok. what we
376 * care about is the top level buts->name directory going away, when
377 * the last trace file is gone. Then we don't have to rmdir() that
378 * manually on trace stop, so it nicely solves the issue with
379 * force killing of running traces.
380 */
381
382 debugfs_remove(parent);
2056a782
JA
383 return 0;
384}
385
386static struct dentry *blk_create_buf_file_callback(const char *filename,
387 struct dentry *parent,
388 int mode,
389 struct rchan_buf *buf,
390 int *is_global)
391{
392 return debugfs_create_file(filename, mode, parent, buf,
393 &relay_file_operations);
394}
395
396static struct rchan_callbacks blk_relay_callbacks = {
397 .subbuf_start = blk_subbuf_start_callback,
398 .create_buf_file = blk_create_buf_file_callback,
399 .remove_buf_file = blk_remove_buf_file_callback,
400};
401
402/*
403 * Setup everything required to start tracing
404 */
6da127ad 405int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
171044d4 406 struct blk_user_trace_setup *buts)
2056a782 407{
2056a782
JA
408 struct blk_trace *old_bt, *bt = NULL;
409 struct dentry *dir = NULL;
2056a782
JA
410 int ret, i;
411
171044d4 412 if (!buts->buf_size || !buts->buf_nr)
2056a782
JA
413 return -EINVAL;
414
0497b345
JA
415 strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
416 buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
2056a782
JA
417
418 /*
419 * some device names have larger paths - convert the slashes
420 * to underscores for this to work as expected
421 */
171044d4
AB
422 for (i = 0; i < strlen(buts->name); i++)
423 if (buts->name[i] == '/')
424 buts->name[i] = '_';
2056a782
JA
425
426 ret = -ENOMEM;
427 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
428 if (!bt)
429 goto err;
430
431 bt->sequence = alloc_percpu(unsigned long);
432 if (!bt->sequence)
433 goto err;
434
64565911
JA
435 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG);
436 if (!bt->msg_data)
437 goto err;
438
2056a782 439 ret = -ENOENT;
f48fc4d3
JA
440
441 if (!blk_tree_root) {
442 blk_tree_root = debugfs_create_dir("block", NULL);
443 if (!blk_tree_root)
444 return -ENOMEM;
445 }
446
447 dir = debugfs_create_dir(buts->name, blk_tree_root);
448
2056a782
JA
449 if (!dir)
450 goto err;
451
452 bt->dir = dir;
6da127ad 453 bt->dev = dev;
2056a782
JA
454 atomic_set(&bt->dropped, 0);
455
456 ret = -EIO;
939b3669
ACM
457 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt,
458 &blk_dropped_fops);
2056a782
JA
459 if (!bt->dropped_file)
460 goto err;
461
02c62304
AB
462 bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
463 if (!bt->msg_file)
464 goto err;
465
171044d4
AB
466 bt->rchan = relay_open("trace", dir, buts->buf_size,
467 buts->buf_nr, &blk_relay_callbacks, bt);
2056a782
JA
468 if (!bt->rchan)
469 goto err;
2056a782 470
171044d4 471 bt->act_mask = buts->act_mask;
2056a782
JA
472 if (!bt->act_mask)
473 bt->act_mask = (u16) -1;
474
171044d4
AB
475 bt->start_lba = buts->start_lba;
476 bt->end_lba = buts->end_lba;
2056a782
JA
477 if (!bt->end_lba)
478 bt->end_lba = -1ULL;
479
171044d4 480 bt->pid = buts->pid;
2056a782
JA
481 bt->trace_state = Blktrace_setup;
482
5f3ea37c
ACM
483 mutex_lock(&blk_probe_mutex);
484 if (atomic_add_return(1, &blk_probes_ref) == 1) {
485 ret = blk_register_tracepoints();
486 if (ret)
487 goto probe_err;
488 }
489 mutex_unlock(&blk_probe_mutex);
490
2056a782
JA
491 ret = -EBUSY;
492 old_bt = xchg(&q->blk_trace, bt);
493 if (old_bt) {
494 (void) xchg(&q->blk_trace, old_bt);
495 goto err;
496 }
497
498 return 0;
5f3ea37c
ACM
499probe_err:
500 atomic_dec(&blk_probes_ref);
501 mutex_unlock(&blk_probe_mutex);
2056a782 502err:
2056a782 503 if (bt) {
02c62304
AB
504 if (bt->msg_file)
505 debugfs_remove(bt->msg_file);
2056a782
JA
506 if (bt->dropped_file)
507 debugfs_remove(bt->dropped_file);
a1205868 508 free_percpu(bt->sequence);
64565911 509 free_percpu(bt->msg_data);
2056a782
JA
510 if (bt->rchan)
511 relay_close(bt->rchan);
512 kfree(bt);
513 }
514 return ret;
515}
171044d4 516
6da127ad
CS
517int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
518 char __user *arg)
171044d4
AB
519{
520 struct blk_user_trace_setup buts;
521 int ret;
522
523 ret = copy_from_user(&buts, arg, sizeof(buts));
524 if (ret)
525 return -EFAULT;
526
6da127ad 527 ret = do_blk_trace_setup(q, name, dev, &buts);
171044d4
AB
528 if (ret)
529 return ret;
530
531 if (copy_to_user(arg, &buts, sizeof(buts)))
532 return -EFAULT;
533
534 return 0;
535}
6da127ad 536EXPORT_SYMBOL_GPL(blk_trace_setup);
2056a782 537
6da127ad 538int blk_trace_startstop(struct request_queue *q, int start)
2056a782 539{
2056a782 540 int ret;
939b3669 541 struct blk_trace *bt = q->blk_trace;
2056a782 542
939b3669 543 if (bt == NULL)
2056a782
JA
544 return -EINVAL;
545
546 /*
547 * For starting a trace, we can transition from a setup or stopped
548 * trace. For stopping a trace, the state must be running
549 */
550 ret = -EINVAL;
551 if (start) {
552 if (bt->trace_state == Blktrace_setup ||
553 bt->trace_state == Blktrace_stopped) {
554 blktrace_seq++;
555 smp_mb();
556 bt->trace_state = Blktrace_running;
be1c6341
OK
557
558 trace_note_time(bt);
2056a782
JA
559 ret = 0;
560 }
561 } else {
562 if (bt->trace_state == Blktrace_running) {
563 bt->trace_state = Blktrace_stopped;
564 relay_flush(bt->rchan);
565 ret = 0;
566 }
567 }
568
569 return ret;
570}
6da127ad 571EXPORT_SYMBOL_GPL(blk_trace_startstop);
2056a782
JA
572
573/**
574 * blk_trace_ioctl: - handle the ioctls associated with tracing
575 * @bdev: the block device
576 * @cmd: the ioctl cmd
577 * @arg: the argument data, if any
578 *
579 **/
580int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
581{
165125e1 582 struct request_queue *q;
2056a782 583 int ret, start = 0;
6da127ad 584 char b[BDEVNAME_SIZE];
2056a782
JA
585
586 q = bdev_get_queue(bdev);
587 if (!q)
588 return -ENXIO;
589
590 mutex_lock(&bdev->bd_mutex);
591
592 switch (cmd) {
593 case BLKTRACESETUP:
f36f21ec 594 bdevname(bdev, b);
6da127ad 595 ret = blk_trace_setup(q, b, bdev->bd_dev, arg);
2056a782
JA
596 break;
597 case BLKTRACESTART:
598 start = 1;
599 case BLKTRACESTOP:
600 ret = blk_trace_startstop(q, start);
601 break;
602 case BLKTRACETEARDOWN:
603 ret = blk_trace_remove(q);
604 break;
605 default:
606 ret = -ENOTTY;
607 break;
608 }
609
610 mutex_unlock(&bdev->bd_mutex);
611 return ret;
612}
613
614/**
615 * blk_trace_shutdown: - stop and cleanup trace structures
616 * @q: the request queue associated with the device
617 *
618 **/
165125e1 619void blk_trace_shutdown(struct request_queue *q)
2056a782 620{
6c5c9341
AD
621 if (q->blk_trace) {
622 blk_trace_startstop(q, 0);
623 blk_trace_remove(q);
624 }
2056a782 625}
5f3ea37c
ACM
626
627/*
628 * blktrace probes
629 */
630
631/**
632 * blk_add_trace_rq - Add a trace for a request oriented action
633 * @q: queue the io is for
634 * @rq: the source request
635 * @what: the action
636 *
637 * Description:
638 * Records an action against a request. Will log the bio offset + size.
639 *
640 **/
641static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
642 u32 what)
643{
644 struct blk_trace *bt = q->blk_trace;
645 int rw = rq->cmd_flags & 0x03;
646
647 if (likely(!bt))
648 return;
649
650 if (blk_discard_rq(rq))
651 rw |= (1 << BIO_RW_DISCARD);
652
653 if (blk_pc_request(rq)) {
654 what |= BLK_TC_ACT(BLK_TC_PC);
655 __blk_add_trace(bt, 0, rq->data_len, rw, what, rq->errors,
656 sizeof(rq->cmd), rq->cmd);
657 } else {
658 what |= BLK_TC_ACT(BLK_TC_FS);
659 __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9,
660 rw, what, rq->errors, 0, NULL);
661 }
662}
663
664static void blk_add_trace_rq_abort(struct request_queue *q, struct request *rq)
665{
666 blk_add_trace_rq(q, rq, BLK_TA_ABORT);
667}
668
669static void blk_add_trace_rq_insert(struct request_queue *q, struct request *rq)
670{
671 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
672}
673
674static void blk_add_trace_rq_issue(struct request_queue *q, struct request *rq)
675{
676 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
677}
678
939b3669
ACM
679static void blk_add_trace_rq_requeue(struct request_queue *q,
680 struct request *rq)
5f3ea37c
ACM
681{
682 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
683}
684
939b3669
ACM
685static void blk_add_trace_rq_complete(struct request_queue *q,
686 struct request *rq)
5f3ea37c
ACM
687{
688 blk_add_trace_rq(q, rq, BLK_TA_COMPLETE);
689}
690
691/**
692 * blk_add_trace_bio - Add a trace for a bio oriented action
693 * @q: queue the io is for
694 * @bio: the source bio
695 * @what: the action
696 *
697 * Description:
698 * Records an action against a bio. Will log the bio offset + size.
699 *
700 **/
701static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
702 u32 what)
703{
704 struct blk_trace *bt = q->blk_trace;
705
706 if (likely(!bt))
707 return;
708
709 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what,
710 !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
711}
712
713static void blk_add_trace_bio_bounce(struct request_queue *q, struct bio *bio)
714{
715 blk_add_trace_bio(q, bio, BLK_TA_BOUNCE);
716}
717
718static void blk_add_trace_bio_complete(struct request_queue *q, struct bio *bio)
719{
720 blk_add_trace_bio(q, bio, BLK_TA_COMPLETE);
721}
722
939b3669
ACM
723static void blk_add_trace_bio_backmerge(struct request_queue *q,
724 struct bio *bio)
5f3ea37c
ACM
725{
726 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
727}
728
939b3669
ACM
729static void blk_add_trace_bio_frontmerge(struct request_queue *q,
730 struct bio *bio)
5f3ea37c
ACM
731{
732 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
733}
734
735static void blk_add_trace_bio_queue(struct request_queue *q, struct bio *bio)
736{
737 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
738}
739
939b3669
ACM
740static void blk_add_trace_getrq(struct request_queue *q,
741 struct bio *bio, int rw)
5f3ea37c
ACM
742{
743 if (bio)
744 blk_add_trace_bio(q, bio, BLK_TA_GETRQ);
745 else {
746 struct blk_trace *bt = q->blk_trace;
747
748 if (bt)
749 __blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL);
750 }
751}
752
753
939b3669
ACM
754static void blk_add_trace_sleeprq(struct request_queue *q,
755 struct bio *bio, int rw)
5f3ea37c
ACM
756{
757 if (bio)
758 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ);
759 else {
760 struct blk_trace *bt = q->blk_trace;
761
762 if (bt)
939b3669
ACM
763 __blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ,
764 0, 0, NULL);
5f3ea37c
ACM
765 }
766}
767
768static void blk_add_trace_plug(struct request_queue *q)
769{
770 struct blk_trace *bt = q->blk_trace;
771
772 if (bt)
773 __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
774}
775
776static void blk_add_trace_unplug_io(struct request_queue *q)
777{
778 struct blk_trace *bt = q->blk_trace;
779
780 if (bt) {
781 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
782 __be64 rpdu = cpu_to_be64(pdu);
783
784 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_IO, 0,
785 sizeof(rpdu), &rpdu);
786 }
787}
788
789static void blk_add_trace_unplug_timer(struct request_queue *q)
790{
791 struct blk_trace *bt = q->blk_trace;
792
793 if (bt) {
794 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
795 __be64 rpdu = cpu_to_be64(pdu);
796
797 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_TIMER, 0,
798 sizeof(rpdu), &rpdu);
799 }
800}
801
802static void blk_add_trace_split(struct request_queue *q, struct bio *bio,
803 unsigned int pdu)
804{
805 struct blk_trace *bt = q->blk_trace;
806
807 if (bt) {
808 __be64 rpdu = cpu_to_be64(pdu);
809
810 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
811 BLK_TA_SPLIT, !bio_flagged(bio, BIO_UPTODATE),
812 sizeof(rpdu), &rpdu);
813 }
814}
815
816/**
817 * blk_add_trace_remap - Add a trace for a remap operation
818 * @q: queue the io is for
819 * @bio: the source bio
820 * @dev: target device
821 * @from: source sector
822 * @to: target sector
823 *
824 * Description:
825 * Device mapper or raid target sometimes need to split a bio because
826 * it spans a stripe (or similar). Add a trace for that action.
827 *
828 **/
829static void blk_add_trace_remap(struct request_queue *q, struct bio *bio,
830 dev_t dev, sector_t from, sector_t to)
831{
832 struct blk_trace *bt = q->blk_trace;
833 struct blk_io_trace_remap r;
834
835 if (likely(!bt))
836 return;
837
838 r.device = cpu_to_be32(dev);
839 r.device_from = cpu_to_be32(bio->bi_bdev->bd_dev);
840 r.sector = cpu_to_be64(to);
841
842 __blk_add_trace(bt, from, bio->bi_size, bio->bi_rw, BLK_TA_REMAP,
843 !bio_flagged(bio, BIO_UPTODATE), sizeof(r), &r);
844}
845
846/**
847 * blk_add_driver_data - Add binary message with driver-specific data
848 * @q: queue the io is for
849 * @rq: io request
850 * @data: driver-specific data
851 * @len: length of driver-specific data
852 *
853 * Description:
854 * Some drivers might want to write driver-specific data per request.
855 *
856 **/
857void blk_add_driver_data(struct request_queue *q,
858 struct request *rq,
859 void *data, size_t len)
860{
861 struct blk_trace *bt = q->blk_trace;
862
863 if (likely(!bt))
864 return;
865
866 if (blk_pc_request(rq))
867 __blk_add_trace(bt, 0, rq->data_len, 0, BLK_TA_DRV_DATA,
868 rq->errors, len, data);
869 else
870 __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9,
871 0, BLK_TA_DRV_DATA, rq->errors, len, data);
872}
873EXPORT_SYMBOL_GPL(blk_add_driver_data);
874
875static int blk_register_tracepoints(void)
876{
877 int ret;
878
879 ret = register_trace_block_rq_abort(blk_add_trace_rq_abort);
880 WARN_ON(ret);
881 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert);
882 WARN_ON(ret);
883 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue);
884 WARN_ON(ret);
885 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue);
886 WARN_ON(ret);
887 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete);
888 WARN_ON(ret);
889 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce);
890 WARN_ON(ret);
891 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete);
892 WARN_ON(ret);
893 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
894 WARN_ON(ret);
895 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
896 WARN_ON(ret);
897 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue);
898 WARN_ON(ret);
899 ret = register_trace_block_getrq(blk_add_trace_getrq);
900 WARN_ON(ret);
901 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq);
902 WARN_ON(ret);
903 ret = register_trace_block_plug(blk_add_trace_plug);
904 WARN_ON(ret);
905 ret = register_trace_block_unplug_timer(blk_add_trace_unplug_timer);
906 WARN_ON(ret);
907 ret = register_trace_block_unplug_io(blk_add_trace_unplug_io);
908 WARN_ON(ret);
909 ret = register_trace_block_split(blk_add_trace_split);
910 WARN_ON(ret);
911 ret = register_trace_block_remap(blk_add_trace_remap);
912 WARN_ON(ret);
913 return 0;
914}
915
916static void blk_unregister_tracepoints(void)
917{
918 unregister_trace_block_remap(blk_add_trace_remap);
919 unregister_trace_block_split(blk_add_trace_split);
920 unregister_trace_block_unplug_io(blk_add_trace_unplug_io);
921 unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer);
922 unregister_trace_block_plug(blk_add_trace_plug);
923 unregister_trace_block_sleeprq(blk_add_trace_sleeprq);
924 unregister_trace_block_getrq(blk_add_trace_getrq);
925 unregister_trace_block_bio_queue(blk_add_trace_bio_queue);
926 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
927 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
928 unregister_trace_block_bio_complete(blk_add_trace_bio_complete);
929 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce);
930 unregister_trace_block_rq_complete(blk_add_trace_rq_complete);
931 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue);
932 unregister_trace_block_rq_issue(blk_add_trace_rq_issue);
933 unregister_trace_block_rq_insert(blk_add_trace_rq_insert);
934 unregister_trace_block_rq_abort(blk_add_trace_rq_abort);
935
936 tracepoint_synchronize_unregister();
937}
c71a8961
ACM
938
939/*
940 * struct blk_io_tracer formatting routines
941 */
942
943static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
944{
157f9c00
ACM
945 int i = 0;
946
947 if (t->action & BLK_TC_DISCARD)
948 rwbs[i++] = 'D';
949 else if (t->action & BLK_TC_WRITE)
950 rwbs[i++] = 'W';
951 else if (t->bytes)
952 rwbs[i++] = 'R';
953 else
954 rwbs[i++] = 'N';
955
956 if (t->action & BLK_TC_AHEAD)
957 rwbs[i++] = 'A';
958 if (t->action & BLK_TC_BARRIER)
959 rwbs[i++] = 'B';
960 if (t->action & BLK_TC_SYNC)
961 rwbs[i++] = 'S';
962 if (t->action & BLK_TC_META)
963 rwbs[i++] = 'M';
964
965 rwbs[i] = '\0';
c71a8961
ACM
966}
967
968static inline
969const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
970{
971 return (const struct blk_io_trace *)ent;
972}
973
974static inline const void *pdu_start(const struct trace_entry *ent)
975{
976 return te_blk_io_trace(ent) + 1;
977}
978
979static inline u32 t_sec(const struct trace_entry *ent)
980{
981 return te_blk_io_trace(ent)->bytes >> 9;
982}
983
984static inline unsigned long long t_sector(const struct trace_entry *ent)
985{
986 return te_blk_io_trace(ent)->sector;
987}
988
989static inline __u16 t_error(const struct trace_entry *ent)
990{
991 return te_blk_io_trace(ent)->sector;
992}
993
994static __u64 get_pdu_int(const struct trace_entry *ent)
995{
996 const __u64 *val = pdu_start(ent);
997 return be64_to_cpu(*val);
998}
999
1000static void get_pdu_remap(const struct trace_entry *ent,
1001 struct blk_io_trace_remap *r)
1002{
1003 const struct blk_io_trace_remap *__r = pdu_start(ent);
1004 __u64 sector = __r->sector;
1005
1006 r->device = be32_to_cpu(__r->device);
1007 r->device_from = be32_to_cpu(__r->device_from);
1008 r->sector = be64_to_cpu(sector);
1009}
1010
1011static int blk_log_action_iter(struct trace_iterator *iter, const char *act)
1012{
1013 char rwbs[6];
1014 unsigned long long ts = ns2usecs(iter->ts);
1015 unsigned long usec_rem = do_div(ts, USEC_PER_SEC);
1016 unsigned secs = (unsigned long)ts;
1017 const struct trace_entry *ent = iter->ent;
1018 const struct blk_io_trace *t = (const struct blk_io_trace *)ent;
1019
1020 fill_rwbs(rwbs, t);
1021
1022 return trace_seq_printf(&iter->seq,
1023 "%3d,%-3d %2d %5d.%06lu %5u %2s %3s ",
1024 MAJOR(t->device), MINOR(t->device), iter->cpu,
1025 secs, usec_rem, ent->pid, act, rwbs);
1026}
1027
1028static int blk_log_action_seq(struct trace_seq *s, const struct blk_io_trace *t,
1029 const char *act)
1030{
1031 char rwbs[6];
1032 fill_rwbs(rwbs, t);
1033 return trace_seq_printf(s, "%3d,%-3d %2s %3s ",
1034 MAJOR(t->device), MINOR(t->device), act, rwbs);
1035}
1036
1037static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1038{
1039 const char *cmd = trace_find_cmdline(ent->pid);
1040
1041 if (t_sec(ent))
1042 return trace_seq_printf(s, "%llu + %u [%s]\n",
1043 t_sector(ent), t_sec(ent), cmd);
1044 return trace_seq_printf(s, "[%s]\n", cmd);
1045}
1046
157f9c00
ACM
1047static int blk_log_with_error(struct trace_seq *s,
1048 const struct trace_entry *ent)
c71a8961
ACM
1049{
1050 if (t_sec(ent))
1051 return trace_seq_printf(s, "%llu + %u [%d]\n", t_sector(ent),
1052 t_sec(ent), t_error(ent));
1053 return trace_seq_printf(s, "%llu [%d]\n", t_sector(ent), t_error(ent));
1054}
1055
1056static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1057{
1058 struct blk_io_trace_remap r = { .device = 0, };
1059
1060 get_pdu_remap(ent, &r);
1061 return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
1062 t_sector(ent),
1063 t_sec(ent), MAJOR(r.device), MINOR(r.device),
1064 (unsigned long long)r.sector);
1065}
1066
1067static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1068{
1069 return trace_seq_printf(s, "[%s]\n", trace_find_cmdline(ent->pid));
1070}
1071
1072static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1073{
1074 return trace_seq_printf(s, "[%s] %llu\n", trace_find_cmdline(ent->pid),
1075 get_pdu_int(ent));
1076}
1077
1078static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1079{
1080 return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
1081 get_pdu_int(ent), trace_find_cmdline(ent->pid));
1082}
1083
1084/*
1085 * struct tracer operations
1086 */
1087
1088static void blk_tracer_print_header(struct seq_file *m)
1089{
1090 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1091 return;
1092 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1093 "# | | | | | |\n");
1094}
1095
1096static void blk_tracer_start(struct trace_array *tr)
1097{
700a3dcb 1098 tracing_reset_online_cpus(tr);
c71a8961
ACM
1099
1100 mutex_lock(&blk_probe_mutex);
1101 if (atomic_add_return(1, &blk_probes_ref) == 1)
1102 if (blk_register_tracepoints())
1103 atomic_dec(&blk_probes_ref);
1104 mutex_unlock(&blk_probe_mutex);
08a06b83 1105 trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
c71a8961
ACM
1106}
1107
1108static int blk_tracer_init(struct trace_array *tr)
1109{
1110 blk_tr = tr;
1111 blk_tracer_start(tr);
1112 mutex_lock(&blk_probe_mutex);
1113 blk_tracer_enabled++;
1114 mutex_unlock(&blk_probe_mutex);
1115 return 0;
1116}
1117
1118static void blk_tracer_stop(struct trace_array *tr)
1119{
08a06b83 1120 trace_flags |= TRACE_ITER_CONTEXT_INFO;
c71a8961
ACM
1121 mutex_lock(&blk_probe_mutex);
1122 if (atomic_dec_and_test(&blk_probes_ref))
1123 blk_unregister_tracepoints();
1124 mutex_unlock(&blk_probe_mutex);
1125}
1126
1127static void blk_tracer_reset(struct trace_array *tr)
1128{
1129 if (!atomic_read(&blk_probes_ref))
1130 return;
1131
1132 mutex_lock(&blk_probe_mutex);
1133 blk_tracer_enabled--;
1134 WARN_ON(blk_tracer_enabled < 0);
1135 mutex_unlock(&blk_probe_mutex);
1136
1137 blk_tracer_stop(tr);
1138}
1139
1140static struct {
1141 const char *act[2];
1142 int (*print)(struct trace_seq *s, const struct trace_entry *ent);
1143} what2act[] __read_mostly = {
1144 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
1145 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1146 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1147 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1148 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1149 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1150 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1151 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1152 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1153 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
1154 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
1155 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1156 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1157 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1158 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1159};
1160
ae7462b4
ACM
1161static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
1162 int flags)
c71a8961 1163{
2c9b238e
ACM
1164 struct trace_seq *s = &iter->seq;
1165 const struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
c71a8961
ACM
1166 const u16 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1167 int ret;
1168
d9793bd8 1169 if (!trace_print_context(iter))
08a06b83
ACM
1170 return TRACE_TYPE_PARTIAL_LINE;
1171
c71a8961
ACM
1172 if (unlikely(what == 0 || what > ARRAY_SIZE(what2act)))
1173 ret = trace_seq_printf(s, "Bad pc action %x\n", what);
1174 else {
1175 const bool long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1176 ret = blk_log_action_seq(s, t, what2act[what].act[long_act]);
1177 if (ret)
2c9b238e 1178 ret = what2act[what].print(s, iter->ent);
c71a8961
ACM
1179 }
1180
1181 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1182}
1183
08a06b83
ACM
1184static int blk_trace_synthesize_old_trace(struct trace_iterator *iter)
1185{
1186 struct trace_seq *s = &iter->seq;
1187 struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1188 const int offset = offsetof(struct blk_io_trace, sector);
1189 struct blk_io_trace old = {
1190 .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
1191 .time = ns2usecs(iter->ts),
1192 };
1193
1194 if (!trace_seq_putmem(s, &old, offset))
1195 return 0;
1196 return trace_seq_putmem(s, &t->sector,
1197 sizeof(old) - offset + t->pdu_len);
1198}
1199
ae7462b4
ACM
1200static enum print_line_t
1201blk_trace_event_print_binary(struct trace_iterator *iter, int flags)
08a06b83
ACM
1202{
1203 return blk_trace_synthesize_old_trace(iter) ?
1204 TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1205}
1206
c71a8961
ACM
1207static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1208{
1209 const struct blk_io_trace *t;
1210 u16 what;
1211 int ret;
1212
1213 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1214 return TRACE_TYPE_UNHANDLED;
1215
1216 t = (const struct blk_io_trace *)iter->ent;
1217 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1218
1219 if (unlikely(what == 0 || what > ARRAY_SIZE(what2act)))
1220 ret = trace_seq_printf(&iter->seq, "Bad pc action %x\n", what);
1221 else {
1222 const bool long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1223 ret = blk_log_action_iter(iter, what2act[what].act[long_act]);
1224 if (ret)
1225 ret = what2act[what].print(&iter->seq, iter->ent);
1226 }
1227
1228 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1229}
1230
1231static struct tracer blk_tracer __read_mostly = {
1232 .name = "blk",
1233 .init = blk_tracer_init,
1234 .reset = blk_tracer_reset,
1235 .start = blk_tracer_start,
1236 .stop = blk_tracer_stop,
1237 .print_header = blk_tracer_print_header,
1238 .print_line = blk_tracer_print_line,
1239 .flags = &blk_tracer_flags,
1240};
1241
1242static struct trace_event trace_blk_event = {
1243 .type = TRACE_BLK,
1244 .trace = blk_trace_event_print,
1245 .latency_trace = blk_trace_event_print,
08a06b83 1246 .binary = blk_trace_event_print_binary,
c71a8961
ACM
1247};
1248
1249static int __init init_blk_tracer(void)
1250{
1251 if (!register_ftrace_event(&trace_blk_event)) {
1252 pr_warning("Warning: could not register block events\n");
1253 return 1;
1254 }
1255
1256 if (register_tracer(&blk_tracer) != 0) {
1257 pr_warning("Warning: could not register the block tracer\n");
1258 unregister_ftrace_event(&trace_blk_event);
1259 return 1;
1260 }
1261
1262 return 0;
1263}
1264
1265device_initcall(init_blk_tracer);
1266
1267static int blk_trace_remove_queue(struct request_queue *q)
1268{
1269 struct blk_trace *bt;
1270
1271 bt = xchg(&q->blk_trace, NULL);
1272 if (bt == NULL)
1273 return -EINVAL;
1274
1275 kfree(bt);
1276 return 0;
1277}
1278
1279/*
1280 * Setup everything required to start tracing
1281 */
1282static int blk_trace_setup_queue(struct request_queue *q, dev_t dev)
1283{
1284 struct blk_trace *old_bt, *bt = NULL;
1285 int ret;
1286
1287 ret = -ENOMEM;
1288 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1289 if (!bt)
1290 goto err;
1291
1292 bt->dev = dev;
1293 bt->act_mask = (u16)-1;
1294 bt->end_lba = -1ULL;
1295 bt->trace_state = Blktrace_running;
1296
1297 old_bt = xchg(&q->blk_trace, bt);
1298 if (old_bt != NULL) {
1299 (void)xchg(&q->blk_trace, old_bt);
1300 kfree(bt);
1301 ret = -EBUSY;
1302 }
1303 return 0;
1304err:
1305 return ret;
1306}
1307
1308/*
1309 * sysfs interface to enable and configure tracing
1310 */
1311
1312static ssize_t sysfs_blk_trace_enable_show(struct device *dev,
1313 struct device_attribute *attr,
1314 char *buf)
1315{
1316 struct hd_struct *p = dev_to_part(dev);
1317 struct block_device *bdev;
1318 ssize_t ret = -ENXIO;
1319
1320 lock_kernel();
1321 bdev = bdget(part_devt(p));
1322 if (bdev != NULL) {
1323 struct request_queue *q = bdev_get_queue(bdev);
1324
1325 if (q != NULL) {
1326 mutex_lock(&bdev->bd_mutex);
1327 ret = sprintf(buf, "%u\n", !!q->blk_trace);
1328 mutex_unlock(&bdev->bd_mutex);
1329 }
1330
1331 bdput(bdev);
1332 }
1333
1334 unlock_kernel();
1335 return ret;
1336}
1337
1338static ssize_t sysfs_blk_trace_enable_store(struct device *dev,
1339 struct device_attribute *attr,
1340 const char *buf, size_t count)
1341{
1342 struct block_device *bdev;
1343 struct request_queue *q;
1344 struct hd_struct *p;
1345 int value;
1346 ssize_t ret = -ENXIO;
1347
1348 if (count == 0 || sscanf(buf, "%d", &value) != 1)
1349 goto out;
1350
1351 lock_kernel();
1352 p = dev_to_part(dev);
1353 bdev = bdget(part_devt(p));
1354 if (bdev == NULL)
1355 goto out_unlock_kernel;
1356
1357 q = bdev_get_queue(bdev);
1358 if (q == NULL)
1359 goto out_bdput;
1360
1361 mutex_lock(&bdev->bd_mutex);
1362 if (value)
1363 ret = blk_trace_setup_queue(q, bdev->bd_dev);
1364 else
1365 ret = blk_trace_remove_queue(q);
1366 mutex_unlock(&bdev->bd_mutex);
1367
1368 if (ret == 0)
1369 ret = count;
1370out_bdput:
1371 bdput(bdev);
1372out_unlock_kernel:
1373 unlock_kernel();
1374out:
1375 return ret;
1376}
1377
1378static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1379 struct device_attribute *attr,
1380 char *buf);
1381static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1382 struct device_attribute *attr,
1383 const char *buf, size_t count);
1384#define BLK_TRACE_DEVICE_ATTR(_name) \
1385 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1386 sysfs_blk_trace_attr_show, \
1387 sysfs_blk_trace_attr_store)
1388
1389static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
1390 sysfs_blk_trace_enable_show, sysfs_blk_trace_enable_store);
1391static BLK_TRACE_DEVICE_ATTR(act_mask);
1392static BLK_TRACE_DEVICE_ATTR(pid);
1393static BLK_TRACE_DEVICE_ATTR(start_lba);
1394static BLK_TRACE_DEVICE_ATTR(end_lba);
1395
1396static struct attribute *blk_trace_attrs[] = {
1397 &dev_attr_enable.attr,
1398 &dev_attr_act_mask.attr,
1399 &dev_attr_pid.attr,
1400 &dev_attr_start_lba.attr,
1401 &dev_attr_end_lba.attr,
1402 NULL
1403};
1404
1405struct attribute_group blk_trace_attr_group = {
1406 .name = "trace",
1407 .attrs = blk_trace_attrs,
1408};
1409
1410static int blk_str2act_mask(const char *str)
1411{
1412 int mask = 0;
1413 char *copy = kstrdup(str, GFP_KERNEL), *s;
1414
1415 if (copy == NULL)
1416 return -ENOMEM;
1417
1418 s = strstrip(copy);
1419
1420 while (1) {
1421 char *sep = strchr(s, ',');
1422
1423 if (sep != NULL)
1424 *sep = '\0';
1425
1426 if (strcasecmp(s, "barrier") == 0)
1427 mask |= BLK_TC_BARRIER;
1428 else if (strcasecmp(s, "complete") == 0)
1429 mask |= BLK_TC_COMPLETE;
1430 else if (strcasecmp(s, "fs") == 0)
1431 mask |= BLK_TC_FS;
1432 else if (strcasecmp(s, "issue") == 0)
1433 mask |= BLK_TC_ISSUE;
1434 else if (strcasecmp(s, "pc") == 0)
1435 mask |= BLK_TC_PC;
1436 else if (strcasecmp(s, "queue") == 0)
1437 mask |= BLK_TC_QUEUE;
1438 else if (strcasecmp(s, "read") == 0)
1439 mask |= BLK_TC_READ;
1440 else if (strcasecmp(s, "requeue") == 0)
1441 mask |= BLK_TC_REQUEUE;
1442 else if (strcasecmp(s, "sync") == 0)
1443 mask |= BLK_TC_SYNC;
1444 else if (strcasecmp(s, "write") == 0)
1445 mask |= BLK_TC_WRITE;
1446
1447 if (sep == NULL)
1448 break;
1449
1450 s = sep + 1;
1451 }
1452 kfree(copy);
1453
1454 return mask;
1455}
1456
1457static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1458 struct device_attribute *attr,
1459 char *buf)
1460{
1461 struct hd_struct *p = dev_to_part(dev);
1462 struct request_queue *q;
1463 struct block_device *bdev;
1464 ssize_t ret = -ENXIO;
1465
1466 lock_kernel();
1467 bdev = bdget(part_devt(p));
1468 if (bdev == NULL)
1469 goto out_unlock_kernel;
1470
1471 q = bdev_get_queue(bdev);
1472 if (q == NULL)
1473 goto out_bdput;
1474 mutex_lock(&bdev->bd_mutex);
1475 if (q->blk_trace == NULL)
1476 ret = sprintf(buf, "disabled\n");
1477 else if (attr == &dev_attr_act_mask)
1478 ret = sprintf(buf, "%#x\n", q->blk_trace->act_mask);
1479 else if (attr == &dev_attr_pid)
1480 ret = sprintf(buf, "%u\n", q->blk_trace->pid);
1481 else if (attr == &dev_attr_start_lba)
1482 ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
1483 else if (attr == &dev_attr_end_lba)
1484 ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
1485 mutex_unlock(&bdev->bd_mutex);
1486out_bdput:
1487 bdput(bdev);
1488out_unlock_kernel:
1489 unlock_kernel();
1490 return ret;
1491}
1492
1493static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1494 struct device_attribute *attr,
1495 const char *buf, size_t count)
1496{
1497 struct block_device *bdev;
1498 struct request_queue *q;
1499 struct hd_struct *p;
1500 u64 value;
1501 ssize_t ret = -ENXIO;
1502
1503 if (count == 0)
1504 goto out;
1505
1506 if (attr == &dev_attr_act_mask) {
1507 if (sscanf(buf, "%llx", &value) != 1) {
1508 /* Assume it is a list of trace category names */
1509 value = blk_str2act_mask(buf);
1510 if (value < 0)
1511 goto out;
1512 }
1513 } else if (sscanf(buf, "%llu", &value) != 1)
1514 goto out;
1515
1516 lock_kernel();
1517 p = dev_to_part(dev);
1518 bdev = bdget(part_devt(p));
1519 if (bdev == NULL)
1520 goto out_unlock_kernel;
1521
1522 q = bdev_get_queue(bdev);
1523 if (q == NULL)
1524 goto out_bdput;
1525
1526 mutex_lock(&bdev->bd_mutex);
1527 ret = 0;
1528 if (q->blk_trace == NULL)
1529 ret = blk_trace_setup_queue(q, bdev->bd_dev);
1530
1531 if (ret == 0) {
1532 if (attr == &dev_attr_act_mask)
1533 q->blk_trace->act_mask = value;
1534 else if (attr == &dev_attr_pid)
1535 q->blk_trace->pid = value;
1536 else if (attr == &dev_attr_start_lba)
1537 q->blk_trace->start_lba = value;
1538 else if (attr == &dev_attr_end_lba)
1539 q->blk_trace->end_lba = value;
1540 ret = count;
1541 }
1542 mutex_unlock(&bdev->bd_mutex);
1543out_bdput:
1544 bdput(bdev);
1545out_unlock_kernel:
1546 unlock_kernel();
1547out:
1548 return ret;
1549}