• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
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  */
18 
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 
21 #include <linux/kernel.h>
22 #include <linux/blkdev.h>
23 #include <linux/blktrace_api.h>
24 #include <linux/percpu.h>
25 #include <linux/init.h>
26 #include <linux/mutex.h>
27 #include <linux/slab.h>
28 #include <linux/debugfs.h>
29 #include <linux/export.h>
30 #include <linux/time.h>
31 #include <linux/uaccess.h>
32 #include <linux/list.h>
33 
34 #include <trace/events/block.h>
35 
36 #include "trace_output.h"
37 
38 #ifdef CONFIG_BLK_DEV_IO_TRACE
39 
40 static unsigned int blktrace_seq __read_mostly = 1;
41 
42 static struct trace_array *blk_tr;
43 static bool blk_tracer_enabled __read_mostly;
44 
45 static LIST_HEAD(running_trace_list);
46 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(running_trace_lock);
47 
48 /* Select an alternative, minimalistic output than the original one */
49 #define TRACE_BLK_OPT_CLASSIC	0x1
50 
51 static struct tracer_opt blk_tracer_opts[] = {
52 	/* Default disable the minimalistic output */
53 	{ TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },
54 	{ }
55 };
56 
57 static struct tracer_flags blk_tracer_flags = {
58 	.val  = 0,
59 	.opts = blk_tracer_opts,
60 };
61 
62 /* Global reference count of probes */
63 static DEFINE_MUTEX(blk_probe_mutex);
64 static int blk_probes_ref;
65 
66 static void blk_register_tracepoints(void);
67 static void blk_unregister_tracepoints(void);
68 
69 /*
70  * Send out a notify message.
71  */
trace_note(struct blk_trace * bt,pid_t pid,int action,const void * data,size_t len)72 static void trace_note(struct blk_trace *bt, pid_t pid, int action,
73 		       const void *data, size_t len)
74 {
75 	struct blk_io_trace *t;
76 	struct ring_buffer_event *event = NULL;
77 	struct ring_buffer *buffer = NULL;
78 	int pc = 0;
79 	int cpu = smp_processor_id();
80 	bool blk_tracer = blk_tracer_enabled;
81 
82 	if (blk_tracer) {
83 		buffer = blk_tr->trace_buffer.buffer;
84 		pc = preempt_count();
85 		event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
86 						  sizeof(*t) + len,
87 						  0, pc);
88 		if (!event)
89 			return;
90 		t = ring_buffer_event_data(event);
91 		goto record_it;
92 	}
93 
94 	if (!bt->rchan)
95 		return;
96 
97 	t = relay_reserve(bt->rchan, sizeof(*t) + len);
98 	if (t) {
99 		t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
100 		t->time = ktime_to_ns(ktime_get());
101 record_it:
102 		t->device = bt->dev;
103 		t->action = action;
104 		t->pid = pid;
105 		t->cpu = cpu;
106 		t->pdu_len = len;
107 		memcpy((void *) t + sizeof(*t), data, len);
108 
109 		if (blk_tracer)
110 			trace_buffer_unlock_commit(blk_tr, buffer, event, 0, pc);
111 	}
112 }
113 
114 /*
115  * Send out a notify for this process, if we haven't done so since a trace
116  * started
117  */
trace_note_tsk(struct task_struct * tsk)118 static void trace_note_tsk(struct task_struct *tsk)
119 {
120 	unsigned long flags;
121 	struct blk_trace *bt;
122 
123 	tsk->btrace_seq = blktrace_seq;
124 	spin_lock_irqsave(&running_trace_lock, flags);
125 	list_for_each_entry(bt, &running_trace_list, running_list) {
126 		trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm,
127 			   sizeof(tsk->comm));
128 	}
129 	spin_unlock_irqrestore(&running_trace_lock, flags);
130 }
131 
trace_note_time(struct blk_trace * bt)132 static void trace_note_time(struct blk_trace *bt)
133 {
134 	struct timespec now;
135 	unsigned long flags;
136 	u32 words[2];
137 
138 	getnstimeofday(&now);
139 	words[0] = now.tv_sec;
140 	words[1] = now.tv_nsec;
141 
142 	local_irq_save(flags);
143 	trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
144 	local_irq_restore(flags);
145 }
146 
__trace_note_message(struct blk_trace * bt,const char * fmt,...)147 void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
148 {
149 	int n;
150 	va_list args;
151 	unsigned long flags;
152 	char *buf;
153 
154 	if (unlikely(bt->trace_state != Blktrace_running &&
155 		     !blk_tracer_enabled))
156 		return;
157 
158 	/*
159 	 * If the BLK_TC_NOTIFY action mask isn't set, don't send any note
160 	 * message to the trace.
161 	 */
162 	if (!(bt->act_mask & BLK_TC_NOTIFY))
163 		return;
164 
165 	local_irq_save(flags);
166 	buf = this_cpu_ptr(bt->msg_data);
167 	va_start(args, fmt);
168 	n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
169 	va_end(args);
170 
171 	trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
172 	local_irq_restore(flags);
173 }
174 EXPORT_SYMBOL_GPL(__trace_note_message);
175 
act_log_check(struct blk_trace * bt,u32 what,sector_t sector,pid_t pid)176 static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
177 			 pid_t pid)
178 {
179 	if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
180 		return 1;
181 	if (sector && (sector < bt->start_lba || sector > bt->end_lba))
182 		return 1;
183 	if (bt->pid && pid != bt->pid)
184 		return 1;
185 
186 	return 0;
187 }
188 
189 /*
190  * Data direction bit lookup
191  */
192 static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ),
193 				 BLK_TC_ACT(BLK_TC_WRITE) };
194 
195 #define BLK_TC_RAHEAD		BLK_TC_AHEAD
196 
197 /* The ilog2() calls fall out because they're constant */
198 #define MASK_TC_BIT(rw, __name) ((rw & REQ_ ## __name) << \
199 	  (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - __REQ_ ## __name))
200 
201 /*
202  * The worker for the various blk_add_trace*() types. Fills out a
203  * blk_io_trace structure and places it in a per-cpu subbuffer.
204  */
__blk_add_trace(struct blk_trace * bt,sector_t sector,int bytes,int rw,u32 what,int error,int pdu_len,void * pdu_data)205 static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
206 		     int rw, u32 what, int error, int pdu_len, void *pdu_data)
207 {
208 	struct task_struct *tsk = current;
209 	struct ring_buffer_event *event = NULL;
210 	struct ring_buffer *buffer = NULL;
211 	struct blk_io_trace *t;
212 	unsigned long flags = 0;
213 	unsigned long *sequence;
214 	pid_t pid;
215 	int cpu, pc = 0;
216 	bool blk_tracer = blk_tracer_enabled;
217 
218 	if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer))
219 		return;
220 
221 	what |= ddir_act[rw & WRITE];
222 	what |= MASK_TC_BIT(rw, SYNC);
223 	what |= MASK_TC_BIT(rw, RAHEAD);
224 	what |= MASK_TC_BIT(rw, META);
225 	what |= MASK_TC_BIT(rw, DISCARD);
226 	what |= MASK_TC_BIT(rw, FLUSH);
227 	what |= MASK_TC_BIT(rw, FUA);
228 
229 	pid = tsk->pid;
230 	if (act_log_check(bt, what, sector, pid))
231 		return;
232 	cpu = raw_smp_processor_id();
233 
234 	if (blk_tracer) {
235 		tracing_record_cmdline(current);
236 
237 		buffer = blk_tr->trace_buffer.buffer;
238 		pc = preempt_count();
239 		event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
240 						  sizeof(*t) + pdu_len,
241 						  0, pc);
242 		if (!event)
243 			return;
244 		t = ring_buffer_event_data(event);
245 		goto record_it;
246 	}
247 
248 	if (unlikely(tsk->btrace_seq != blktrace_seq))
249 		trace_note_tsk(tsk);
250 
251 	/*
252 	 * A word about the locking here - we disable interrupts to reserve
253 	 * some space in the relay per-cpu buffer, to prevent an irq
254 	 * from coming in and stepping on our toes.
255 	 */
256 	local_irq_save(flags);
257 	t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
258 	if (t) {
259 		sequence = per_cpu_ptr(bt->sequence, cpu);
260 
261 		t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
262 		t->sequence = ++(*sequence);
263 		t->time = ktime_to_ns(ktime_get());
264 record_it:
265 		/*
266 		 * These two are not needed in ftrace as they are in the
267 		 * generic trace_entry, filled by tracing_generic_entry_update,
268 		 * but for the trace_event->bin() synthesizer benefit we do it
269 		 * here too.
270 		 */
271 		t->cpu = cpu;
272 		t->pid = pid;
273 
274 		t->sector = sector;
275 		t->bytes = bytes;
276 		t->action = what;
277 		t->device = bt->dev;
278 		t->error = error;
279 		t->pdu_len = pdu_len;
280 
281 		if (pdu_len)
282 			memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
283 
284 		if (blk_tracer) {
285 			trace_buffer_unlock_commit(blk_tr, buffer, event, 0, pc);
286 			return;
287 		}
288 	}
289 
290 	local_irq_restore(flags);
291 }
292 
293 static struct dentry *blk_tree_root;
294 static DEFINE_MUTEX(blk_tree_mutex);
295 
blk_trace_free(struct blk_trace * bt)296 static void blk_trace_free(struct blk_trace *bt)
297 {
298 	debugfs_remove(bt->msg_file);
299 	debugfs_remove(bt->dropped_file);
300 	relay_close(bt->rchan);
301 	debugfs_remove(bt->dir);
302 	free_percpu(bt->sequence);
303 	free_percpu(bt->msg_data);
304 	kfree(bt);
305 }
306 
get_probe_ref(void)307 static void get_probe_ref(void)
308 {
309 	mutex_lock(&blk_probe_mutex);
310 	if (++blk_probes_ref == 1)
311 		blk_register_tracepoints();
312 	mutex_unlock(&blk_probe_mutex);
313 }
314 
put_probe_ref(void)315 static void put_probe_ref(void)
316 {
317 	mutex_lock(&blk_probe_mutex);
318 	if (!--blk_probes_ref)
319 		blk_unregister_tracepoints();
320 	mutex_unlock(&blk_probe_mutex);
321 }
322 
blk_trace_cleanup(struct blk_trace * bt)323 static void blk_trace_cleanup(struct blk_trace *bt)
324 {
325 	synchronize_rcu();
326 	blk_trace_free(bt);
327 	put_probe_ref();
328 }
329 
__blk_trace_remove(struct request_queue * q)330 static int __blk_trace_remove(struct request_queue *q)
331 {
332 	struct blk_trace *bt;
333 
334 	bt = xchg(&q->blk_trace, NULL);
335 	if (!bt)
336 		return -EINVAL;
337 
338 	if (bt->trace_state != Blktrace_running)
339 		blk_trace_cleanup(bt);
340 
341 	return 0;
342 }
343 
blk_trace_remove(struct request_queue * q)344 int blk_trace_remove(struct request_queue *q)
345 {
346 	int ret;
347 
348 	mutex_lock(&q->blk_trace_mutex);
349 	ret = __blk_trace_remove(q);
350 	mutex_unlock(&q->blk_trace_mutex);
351 
352 	return ret;
353 }
354 EXPORT_SYMBOL_GPL(blk_trace_remove);
355 
blk_dropped_read(struct file * filp,char __user * buffer,size_t count,loff_t * ppos)356 static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
357 				size_t count, loff_t *ppos)
358 {
359 	struct blk_trace *bt = filp->private_data;
360 	char buf[16];
361 
362 	snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
363 
364 	return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
365 }
366 
367 static const struct file_operations blk_dropped_fops = {
368 	.owner =	THIS_MODULE,
369 	.open =		simple_open,
370 	.read =		blk_dropped_read,
371 	.llseek =	default_llseek,
372 };
373 
blk_msg_write(struct file * filp,const char __user * buffer,size_t count,loff_t * ppos)374 static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
375 				size_t count, loff_t *ppos)
376 {
377 	char *msg;
378 	struct blk_trace *bt;
379 
380 	if (count >= BLK_TN_MAX_MSG)
381 		return -EINVAL;
382 
383 	msg = kmalloc(count + 1, GFP_KERNEL);
384 	if (msg == NULL)
385 		return -ENOMEM;
386 
387 	if (copy_from_user(msg, buffer, count)) {
388 		kfree(msg);
389 		return -EFAULT;
390 	}
391 
392 	msg[count] = '\0';
393 	bt = filp->private_data;
394 	__trace_note_message(bt, "%s", msg);
395 	kfree(msg);
396 
397 	return count;
398 }
399 
400 static const struct file_operations blk_msg_fops = {
401 	.owner =	THIS_MODULE,
402 	.open =		simple_open,
403 	.write =	blk_msg_write,
404 	.llseek =	noop_llseek,
405 };
406 
407 /*
408  * Keep track of how many times we encountered a full subbuffer, to aid
409  * the user space app in telling how many lost events there were.
410  */
blk_subbuf_start_callback(struct rchan_buf * buf,void * subbuf,void * prev_subbuf,size_t prev_padding)411 static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
412 				     void *prev_subbuf, size_t prev_padding)
413 {
414 	struct blk_trace *bt;
415 
416 	if (!relay_buf_full(buf))
417 		return 1;
418 
419 	bt = buf->chan->private_data;
420 	atomic_inc(&bt->dropped);
421 	return 0;
422 }
423 
blk_remove_buf_file_callback(struct dentry * dentry)424 static int blk_remove_buf_file_callback(struct dentry *dentry)
425 {
426 	debugfs_remove(dentry);
427 
428 	return 0;
429 }
430 
blk_create_buf_file_callback(const char * filename,struct dentry * parent,umode_t mode,struct rchan_buf * buf,int * is_global)431 static struct dentry *blk_create_buf_file_callback(const char *filename,
432 						   struct dentry *parent,
433 						   umode_t mode,
434 						   struct rchan_buf *buf,
435 						   int *is_global)
436 {
437 	return debugfs_create_file(filename, mode, parent, buf,
438 					&relay_file_operations);
439 }
440 
441 static struct rchan_callbacks blk_relay_callbacks = {
442 	.subbuf_start		= blk_subbuf_start_callback,
443 	.create_buf_file	= blk_create_buf_file_callback,
444 	.remove_buf_file	= blk_remove_buf_file_callback,
445 };
446 
blk_trace_setup_lba(struct blk_trace * bt,struct block_device * bdev)447 static void blk_trace_setup_lba(struct blk_trace *bt,
448 				struct block_device *bdev)
449 {
450 	struct hd_struct *part = NULL;
451 
452 	if (bdev)
453 		part = bdev->bd_part;
454 
455 	if (part) {
456 		bt->start_lba = part->start_sect;
457 		bt->end_lba = part->start_sect + part->nr_sects;
458 	} else {
459 		bt->start_lba = 0;
460 		bt->end_lba = -1ULL;
461 	}
462 }
463 
464 /*
465  * Setup everything required to start tracing
466  */
do_blk_trace_setup(struct request_queue * q,char * name,dev_t dev,struct block_device * bdev,struct blk_user_trace_setup * buts)467 int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
468 		       struct block_device *bdev,
469 		       struct blk_user_trace_setup *buts)
470 {
471 	struct blk_trace *bt = NULL;
472 	struct dentry *dir = NULL;
473 	int ret;
474 
475 	if (!buts->buf_size || !buts->buf_nr)
476 		return -EINVAL;
477 
478 	strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
479 	buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
480 
481 	/*
482 	 * some device names have larger paths - convert the slashes
483 	 * to underscores for this to work as expected
484 	 */
485 	strreplace(buts->name, '/', '_');
486 
487 	/*
488 	 * bdev can be NULL, as with scsi-generic, this is a helpful as
489 	 * we can be.
490 	 */
491 	if (q->blk_trace) {
492 		pr_warn("Concurrent blktraces are not allowed on %s\n",
493 			buts->name);
494 		return -EBUSY;
495 	}
496 
497 	bt = kzalloc(sizeof(*bt), GFP_KERNEL);
498 	if (!bt)
499 		return -ENOMEM;
500 
501 	ret = -ENOMEM;
502 	bt->sequence = alloc_percpu(unsigned long);
503 	if (!bt->sequence)
504 		goto err;
505 
506 	bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
507 	if (!bt->msg_data)
508 		goto err;
509 
510 	ret = -ENOENT;
511 
512 	mutex_lock(&blk_tree_mutex);
513 	if (!blk_tree_root) {
514 		blk_tree_root = debugfs_create_dir("block", NULL);
515 		if (!blk_tree_root) {
516 			mutex_unlock(&blk_tree_mutex);
517 			goto err;
518 		}
519 	}
520 	mutex_unlock(&blk_tree_mutex);
521 
522 	dir = debugfs_create_dir(buts->name, blk_tree_root);
523 
524 	if (!dir)
525 		goto err;
526 
527 	bt->dir = dir;
528 	bt->dev = dev;
529 	atomic_set(&bt->dropped, 0);
530 	INIT_LIST_HEAD(&bt->running_list);
531 
532 	ret = -EIO;
533 	bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt,
534 					       &blk_dropped_fops);
535 	if (!bt->dropped_file)
536 		goto err;
537 
538 	bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
539 	if (!bt->msg_file)
540 		goto err;
541 
542 	bt->rchan = relay_open("trace", dir, buts->buf_size,
543 				buts->buf_nr, &blk_relay_callbacks, bt);
544 	if (!bt->rchan)
545 		goto err;
546 
547 	bt->act_mask = buts->act_mask;
548 	if (!bt->act_mask)
549 		bt->act_mask = (u16) -1;
550 
551 	blk_trace_setup_lba(bt, bdev);
552 
553 	/* overwrite with user settings */
554 	if (buts->start_lba)
555 		bt->start_lba = buts->start_lba;
556 	if (buts->end_lba)
557 		bt->end_lba = buts->end_lba;
558 
559 	bt->pid = buts->pid;
560 	bt->trace_state = Blktrace_setup;
561 
562 	ret = -EBUSY;
563 	if (cmpxchg(&q->blk_trace, NULL, bt))
564 		goto err;
565 
566 	get_probe_ref();
567 
568 	return 0;
569 err:
570 	blk_trace_free(bt);
571 	return ret;
572 }
573 
__blk_trace_setup(struct request_queue * q,char * name,dev_t dev,struct block_device * bdev,char __user * arg)574 static int __blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
575 			     struct block_device *bdev, char __user *arg)
576 {
577 	struct blk_user_trace_setup buts;
578 	int ret;
579 
580 	ret = copy_from_user(&buts, arg, sizeof(buts));
581 	if (ret)
582 		return -EFAULT;
583 
584 	ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
585 	if (ret)
586 		return ret;
587 
588 	if (copy_to_user(arg, &buts, sizeof(buts))) {
589 		__blk_trace_remove(q);
590 		return -EFAULT;
591 	}
592 	return 0;
593 }
594 
blk_trace_setup(struct request_queue * q,char * name,dev_t dev,struct block_device * bdev,char __user * arg)595 int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
596 		    struct block_device *bdev,
597 		    char __user *arg)
598 {
599 	int ret;
600 
601 	mutex_lock(&q->blk_trace_mutex);
602 	ret = __blk_trace_setup(q, name, dev, bdev, arg);
603 	mutex_unlock(&q->blk_trace_mutex);
604 
605 	return ret;
606 }
607 EXPORT_SYMBOL_GPL(blk_trace_setup);
608 
609 #if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64)
compat_blk_trace_setup(struct request_queue * q,char * name,dev_t dev,struct block_device * bdev,char __user * arg)610 static int compat_blk_trace_setup(struct request_queue *q, char *name,
611 				  dev_t dev, struct block_device *bdev,
612 				  char __user *arg)
613 {
614 	struct blk_user_trace_setup buts;
615 	struct compat_blk_user_trace_setup cbuts;
616 	int ret;
617 
618 	if (copy_from_user(&cbuts, arg, sizeof(cbuts)))
619 		return -EFAULT;
620 
621 	buts = (struct blk_user_trace_setup) {
622 		.act_mask = cbuts.act_mask,
623 		.buf_size = cbuts.buf_size,
624 		.buf_nr = cbuts.buf_nr,
625 		.start_lba = cbuts.start_lba,
626 		.end_lba = cbuts.end_lba,
627 		.pid = cbuts.pid,
628 	};
629 
630 	ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
631 	if (ret)
632 		return ret;
633 
634 	if (copy_to_user(arg, &buts.name, ARRAY_SIZE(buts.name))) {
635 		__blk_trace_remove(q);
636 		return -EFAULT;
637 	}
638 
639 	return 0;
640 }
641 #endif
642 
__blk_trace_startstop(struct request_queue * q,int start)643 static int __blk_trace_startstop(struct request_queue *q, int start)
644 {
645 	int ret;
646 	struct blk_trace *bt;
647 
648 	bt = rcu_dereference_protected(q->blk_trace,
649 				       lockdep_is_held(&q->blk_trace_mutex));
650 	if (bt == NULL)
651 		return -EINVAL;
652 
653 	/*
654 	 * For starting a trace, we can transition from a setup or stopped
655 	 * trace. For stopping a trace, the state must be running
656 	 */
657 	ret = -EINVAL;
658 	if (start) {
659 		if (bt->trace_state == Blktrace_setup ||
660 		    bt->trace_state == Blktrace_stopped) {
661 			blktrace_seq++;
662 			smp_mb();
663 			bt->trace_state = Blktrace_running;
664 			spin_lock_irq(&running_trace_lock);
665 			list_add(&bt->running_list, &running_trace_list);
666 			spin_unlock_irq(&running_trace_lock);
667 
668 			trace_note_time(bt);
669 			ret = 0;
670 		}
671 	} else {
672 		if (bt->trace_state == Blktrace_running) {
673 			bt->trace_state = Blktrace_stopped;
674 			spin_lock_irq(&running_trace_lock);
675 			list_del_init(&bt->running_list);
676 			spin_unlock_irq(&running_trace_lock);
677 			relay_flush(bt->rchan);
678 			ret = 0;
679 		}
680 	}
681 
682 	return ret;
683 }
684 
blk_trace_startstop(struct request_queue * q,int start)685 int blk_trace_startstop(struct request_queue *q, int start)
686 {
687 	int ret;
688 
689 	mutex_lock(&q->blk_trace_mutex);
690 	ret = __blk_trace_startstop(q, start);
691 	mutex_unlock(&q->blk_trace_mutex);
692 
693 	return ret;
694 }
695 EXPORT_SYMBOL_GPL(blk_trace_startstop);
696 
697 /*
698  * When reading or writing the blktrace sysfs files, the references to the
699  * opened sysfs or device files should prevent the underlying block device
700  * from being removed. So no further delete protection is really needed.
701  */
702 
703 /**
704  * blk_trace_ioctl: - handle the ioctls associated with tracing
705  * @bdev:	the block device
706  * @cmd:	the ioctl cmd
707  * @arg:	the argument data, if any
708  *
709  **/
blk_trace_ioctl(struct block_device * bdev,unsigned cmd,char __user * arg)710 int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
711 {
712 	struct request_queue *q;
713 	int ret, start = 0;
714 	char b[BDEVNAME_SIZE];
715 
716 	q = bdev_get_queue(bdev);
717 	if (!q)
718 		return -ENXIO;
719 
720 	mutex_lock(&q->blk_trace_mutex);
721 
722 	switch (cmd) {
723 	case BLKTRACESETUP:
724 		bdevname(bdev, b);
725 		ret = __blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
726 		break;
727 #if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64)
728 	case BLKTRACESETUP32:
729 		bdevname(bdev, b);
730 		ret = compat_blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
731 		break;
732 #endif
733 	case BLKTRACESTART:
734 		start = 1;
735 	case BLKTRACESTOP:
736 		ret = __blk_trace_startstop(q, start);
737 		break;
738 	case BLKTRACETEARDOWN:
739 		ret = __blk_trace_remove(q);
740 		break;
741 	default:
742 		ret = -ENOTTY;
743 		break;
744 	}
745 
746 	mutex_unlock(&q->blk_trace_mutex);
747 	return ret;
748 }
749 
750 /**
751  * blk_trace_shutdown: - stop and cleanup trace structures
752  * @q:    the request queue associated with the device
753  *
754  **/
blk_trace_shutdown(struct request_queue * q)755 void blk_trace_shutdown(struct request_queue *q)
756 {
757 	mutex_lock(&q->blk_trace_mutex);
758 	if (rcu_dereference_protected(q->blk_trace,
759 				      lockdep_is_held(&q->blk_trace_mutex))) {
760 		__blk_trace_startstop(q, 0);
761 		__blk_trace_remove(q);
762 	}
763 
764 	mutex_unlock(&q->blk_trace_mutex);
765 }
766 
767 /*
768  * blktrace probes
769  */
770 
771 /**
772  * blk_add_trace_rq - Add a trace for a request oriented action
773  * @q:		queue the io is for
774  * @rq:		the source request
775  * @nr_bytes:	number of completed bytes
776  * @what:	the action
777  *
778  * Description:
779  *     Records an action against a request. Will log the bio offset + size.
780  *
781  **/
blk_add_trace_rq(struct request_queue * q,struct request * rq,unsigned int nr_bytes,u32 what)782 static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
783 			     unsigned int nr_bytes, u32 what)
784 {
785 	struct blk_trace *bt;
786 
787 	rcu_read_lock();
788 	bt = rcu_dereference(q->blk_trace);
789 	if (likely(!bt)) {
790 		rcu_read_unlock();
791 		return;
792 	}
793 
794 	if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
795 		what |= BLK_TC_ACT(BLK_TC_PC);
796 		__blk_add_trace(bt, 0, nr_bytes, rq->cmd_flags,
797 				what, rq->errors, rq->cmd_len, rq->cmd);
798 	} else  {
799 		what |= BLK_TC_ACT(BLK_TC_FS);
800 		__blk_add_trace(bt, blk_rq_pos(rq), nr_bytes,
801 				rq->cmd_flags, what, rq->errors, 0, NULL);
802 	}
803 	rcu_read_unlock();
804 }
805 
blk_add_trace_rq_abort(void * ignore,struct request_queue * q,struct request * rq)806 static void blk_add_trace_rq_abort(void *ignore,
807 				   struct request_queue *q, struct request *rq)
808 {
809 	blk_add_trace_rq(q, rq, blk_rq_bytes(rq), BLK_TA_ABORT);
810 }
811 
blk_add_trace_rq_insert(void * ignore,struct request_queue * q,struct request * rq)812 static void blk_add_trace_rq_insert(void *ignore,
813 				    struct request_queue *q, struct request *rq)
814 {
815 	blk_add_trace_rq(q, rq, blk_rq_bytes(rq), BLK_TA_INSERT);
816 }
817 
blk_add_trace_rq_issue(void * ignore,struct request_queue * q,struct request * rq)818 static void blk_add_trace_rq_issue(void *ignore,
819 				   struct request_queue *q, struct request *rq)
820 {
821 	blk_add_trace_rq(q, rq, blk_rq_bytes(rq), BLK_TA_ISSUE);
822 }
823 
blk_add_trace_rq_requeue(void * ignore,struct request_queue * q,struct request * rq)824 static void blk_add_trace_rq_requeue(void *ignore,
825 				     struct request_queue *q,
826 				     struct request *rq)
827 {
828 	blk_add_trace_rq(q, rq, blk_rq_bytes(rq), BLK_TA_REQUEUE);
829 }
830 
blk_add_trace_rq_complete(void * ignore,struct request_queue * q,struct request * rq,unsigned int nr_bytes)831 static void blk_add_trace_rq_complete(void *ignore,
832 				      struct request_queue *q,
833 				      struct request *rq,
834 				      unsigned int nr_bytes)
835 {
836 	blk_add_trace_rq(q, rq, nr_bytes, BLK_TA_COMPLETE);
837 }
838 
839 /**
840  * blk_add_trace_bio - Add a trace for a bio oriented action
841  * @q:		queue the io is for
842  * @bio:	the source bio
843  * @what:	the action
844  * @error:	error, if any
845  *
846  * Description:
847  *     Records an action against a bio. Will log the bio offset + size.
848  *
849  **/
blk_add_trace_bio(struct request_queue * q,struct bio * bio,u32 what,int error)850 static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
851 			      u32 what, int error)
852 {
853 	struct blk_trace *bt;
854 
855 	rcu_read_lock();
856 	bt = rcu_dereference(q->blk_trace);
857 	if (likely(!bt)) {
858 		rcu_read_unlock();
859 		return;
860 	}
861 
862 	__blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size,
863 			bio->bi_rw, what, error, 0, NULL);
864 	rcu_read_unlock();
865 }
866 
blk_add_trace_bio_bounce(void * ignore,struct request_queue * q,struct bio * bio)867 static void blk_add_trace_bio_bounce(void *ignore,
868 				     struct request_queue *q, struct bio *bio)
869 {
870 	blk_add_trace_bio(q, bio, BLK_TA_BOUNCE, 0);
871 }
872 
blk_add_trace_bio_complete(void * ignore,struct request_queue * q,struct bio * bio,int error)873 static void blk_add_trace_bio_complete(void *ignore,
874 				       struct request_queue *q, struct bio *bio,
875 				       int error)
876 {
877 	blk_add_trace_bio(q, bio, BLK_TA_COMPLETE, error);
878 }
879 
blk_add_trace_bio_backmerge(void * ignore,struct request_queue * q,struct request * rq,struct bio * bio)880 static void blk_add_trace_bio_backmerge(void *ignore,
881 					struct request_queue *q,
882 					struct request *rq,
883 					struct bio *bio)
884 {
885 	blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE, 0);
886 }
887 
blk_add_trace_bio_frontmerge(void * ignore,struct request_queue * q,struct request * rq,struct bio * bio)888 static void blk_add_trace_bio_frontmerge(void *ignore,
889 					 struct request_queue *q,
890 					 struct request *rq,
891 					 struct bio *bio)
892 {
893 	blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE, 0);
894 }
895 
blk_add_trace_bio_queue(void * ignore,struct request_queue * q,struct bio * bio)896 static void blk_add_trace_bio_queue(void *ignore,
897 				    struct request_queue *q, struct bio *bio)
898 {
899 	blk_add_trace_bio(q, bio, BLK_TA_QUEUE, 0);
900 }
901 
blk_add_trace_getrq(void * ignore,struct request_queue * q,struct bio * bio,int rw)902 static void blk_add_trace_getrq(void *ignore,
903 				struct request_queue *q,
904 				struct bio *bio, int rw)
905 {
906 	if (bio)
907 		blk_add_trace_bio(q, bio, BLK_TA_GETRQ, 0);
908 	else {
909 		struct blk_trace *bt;
910 
911 		rcu_read_lock();
912 		bt = rcu_dereference(q->blk_trace);
913 		if (bt)
914 			__blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL);
915 		rcu_read_unlock();
916 	}
917 }
918 
919 
blk_add_trace_sleeprq(void * ignore,struct request_queue * q,struct bio * bio,int rw)920 static void blk_add_trace_sleeprq(void *ignore,
921 				  struct request_queue *q,
922 				  struct bio *bio, int rw)
923 {
924 	if (bio)
925 		blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ, 0);
926 	else {
927 		struct blk_trace *bt;
928 
929 		rcu_read_lock();
930 		bt = rcu_dereference(q->blk_trace);
931 		if (bt)
932 			__blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ,
933 					0, 0, NULL);
934 		rcu_read_unlock();
935 	}
936 }
937 
blk_add_trace_plug(void * ignore,struct request_queue * q)938 static void blk_add_trace_plug(void *ignore, struct request_queue *q)
939 {
940 	struct blk_trace *bt;
941 
942 	rcu_read_lock();
943 	bt = rcu_dereference(q->blk_trace);
944 	if (bt)
945 		__blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
946 	rcu_read_unlock();
947 }
948 
blk_add_trace_unplug(void * ignore,struct request_queue * q,unsigned int depth,bool explicit)949 static void blk_add_trace_unplug(void *ignore, struct request_queue *q,
950 				    unsigned int depth, bool explicit)
951 {
952 	struct blk_trace *bt;
953 
954 	rcu_read_lock();
955 	bt = rcu_dereference(q->blk_trace);
956 	if (bt) {
957 		__be64 rpdu = cpu_to_be64(depth);
958 		u32 what;
959 
960 		if (explicit)
961 			what = BLK_TA_UNPLUG_IO;
962 		else
963 			what = BLK_TA_UNPLUG_TIMER;
964 
965 		__blk_add_trace(bt, 0, 0, 0, what, 0, sizeof(rpdu), &rpdu);
966 	}
967 	rcu_read_unlock();
968 }
969 
blk_add_trace_split(void * ignore,struct request_queue * q,struct bio * bio,unsigned int pdu)970 static void blk_add_trace_split(void *ignore,
971 				struct request_queue *q, struct bio *bio,
972 				unsigned int pdu)
973 {
974 	struct blk_trace *bt;
975 
976 	rcu_read_lock();
977 	bt = rcu_dereference(q->blk_trace);
978 	if (bt) {
979 		__be64 rpdu = cpu_to_be64(pdu);
980 
981 		__blk_add_trace(bt, bio->bi_iter.bi_sector,
982 				bio->bi_iter.bi_size, bio->bi_rw, BLK_TA_SPLIT,
983 				bio->bi_error, sizeof(rpdu), &rpdu);
984 	}
985 	rcu_read_unlock();
986 }
987 
988 /**
989  * blk_add_trace_bio_remap - Add a trace for a bio-remap operation
990  * @ignore:	trace callback data parameter (not used)
991  * @q:		queue the io is for
992  * @bio:	the source bio
993  * @dev:	target device
994  * @from:	source sector
995  *
996  * Description:
997  *     Device mapper or raid target sometimes need to split a bio because
998  *     it spans a stripe (or similar). Add a trace for that action.
999  *
1000  **/
blk_add_trace_bio_remap(void * ignore,struct request_queue * q,struct bio * bio,dev_t dev,sector_t from)1001 static void blk_add_trace_bio_remap(void *ignore,
1002 				    struct request_queue *q, struct bio *bio,
1003 				    dev_t dev, sector_t from)
1004 {
1005 	struct blk_trace *bt;
1006 	struct blk_io_trace_remap r;
1007 
1008 	rcu_read_lock();
1009 	bt = rcu_dereference(q->blk_trace);
1010 	if (likely(!bt)) {
1011 		rcu_read_unlock();
1012 		return;
1013 	}
1014 
1015 	r.device_from = cpu_to_be32(dev);
1016 	r.device_to   = cpu_to_be32(bio->bi_bdev->bd_dev);
1017 	r.sector_from = cpu_to_be64(from);
1018 
1019 	__blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size,
1020 			bio->bi_rw, BLK_TA_REMAP, bio->bi_error,
1021 			sizeof(r), &r);
1022 	rcu_read_unlock();
1023 }
1024 
1025 /**
1026  * blk_add_trace_rq_remap - Add a trace for a request-remap operation
1027  * @ignore:	trace callback data parameter (not used)
1028  * @q:		queue the io is for
1029  * @rq:		the source request
1030  * @dev:	target device
1031  * @from:	source sector
1032  *
1033  * Description:
1034  *     Device mapper remaps request to other devices.
1035  *     Add a trace for that action.
1036  *
1037  **/
blk_add_trace_rq_remap(void * ignore,struct request_queue * q,struct request * rq,dev_t dev,sector_t from)1038 static void blk_add_trace_rq_remap(void *ignore,
1039 				   struct request_queue *q,
1040 				   struct request *rq, dev_t dev,
1041 				   sector_t from)
1042 {
1043 	struct blk_trace *bt;
1044 	struct blk_io_trace_remap r;
1045 
1046 	rcu_read_lock();
1047 	bt = rcu_dereference(q->blk_trace);
1048 	if (likely(!bt)) {
1049 		rcu_read_unlock();
1050 		return;
1051 	}
1052 
1053 	r.device_from = cpu_to_be32(dev);
1054 	r.device_to   = cpu_to_be32(disk_devt(rq->rq_disk));
1055 	r.sector_from = cpu_to_be64(from);
1056 
1057 	__blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq),
1058 			rq_data_dir(rq), BLK_TA_REMAP, !!rq->errors,
1059 			sizeof(r), &r);
1060 	rcu_read_unlock();
1061 }
1062 
1063 /**
1064  * blk_add_driver_data - Add binary message with driver-specific data
1065  * @q:		queue the io is for
1066  * @rq:		io request
1067  * @data:	driver-specific data
1068  * @len:	length of driver-specific data
1069  *
1070  * Description:
1071  *     Some drivers might want to write driver-specific data per request.
1072  *
1073  **/
blk_add_driver_data(struct request_queue * q,struct request * rq,void * data,size_t len)1074 void blk_add_driver_data(struct request_queue *q,
1075 			 struct request *rq,
1076 			 void *data, size_t len)
1077 {
1078 	struct blk_trace *bt;
1079 
1080 	rcu_read_lock();
1081 	bt = rcu_dereference(q->blk_trace);
1082 	if (likely(!bt)) {
1083 		rcu_read_unlock();
1084 		return;
1085 	}
1086 
1087 	if (rq->cmd_type == REQ_TYPE_BLOCK_PC)
1088 		__blk_add_trace(bt, 0, blk_rq_bytes(rq), 0,
1089 				BLK_TA_DRV_DATA, rq->errors, len, data);
1090 	else
1091 		__blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), 0,
1092 				BLK_TA_DRV_DATA, rq->errors, len, data);
1093 	rcu_read_unlock();
1094 }
1095 EXPORT_SYMBOL_GPL(blk_add_driver_data);
1096 
blk_register_tracepoints(void)1097 static void blk_register_tracepoints(void)
1098 {
1099 	int ret;
1100 
1101 	ret = register_trace_block_rq_abort(blk_add_trace_rq_abort, NULL);
1102 	WARN_ON(ret);
1103 	ret = register_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
1104 	WARN_ON(ret);
1105 	ret = register_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
1106 	WARN_ON(ret);
1107 	ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
1108 	WARN_ON(ret);
1109 	ret = register_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
1110 	WARN_ON(ret);
1111 	ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
1112 	WARN_ON(ret);
1113 	ret = register_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
1114 	WARN_ON(ret);
1115 	ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
1116 	WARN_ON(ret);
1117 	ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
1118 	WARN_ON(ret);
1119 	ret = register_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
1120 	WARN_ON(ret);
1121 	ret = register_trace_block_getrq(blk_add_trace_getrq, NULL);
1122 	WARN_ON(ret);
1123 	ret = register_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
1124 	WARN_ON(ret);
1125 	ret = register_trace_block_plug(blk_add_trace_plug, NULL);
1126 	WARN_ON(ret);
1127 	ret = register_trace_block_unplug(blk_add_trace_unplug, NULL);
1128 	WARN_ON(ret);
1129 	ret = register_trace_block_split(blk_add_trace_split, NULL);
1130 	WARN_ON(ret);
1131 	ret = register_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
1132 	WARN_ON(ret);
1133 	ret = register_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
1134 	WARN_ON(ret);
1135 }
1136 
blk_unregister_tracepoints(void)1137 static void blk_unregister_tracepoints(void)
1138 {
1139 	unregister_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
1140 	unregister_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
1141 	unregister_trace_block_split(blk_add_trace_split, NULL);
1142 	unregister_trace_block_unplug(blk_add_trace_unplug, NULL);
1143 	unregister_trace_block_plug(blk_add_trace_plug, NULL);
1144 	unregister_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
1145 	unregister_trace_block_getrq(blk_add_trace_getrq, NULL);
1146 	unregister_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
1147 	unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
1148 	unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
1149 	unregister_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
1150 	unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
1151 	unregister_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
1152 	unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
1153 	unregister_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
1154 	unregister_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
1155 	unregister_trace_block_rq_abort(blk_add_trace_rq_abort, NULL);
1156 
1157 	tracepoint_synchronize_unregister();
1158 }
1159 
1160 /*
1161  * struct blk_io_tracer formatting routines
1162  */
1163 
fill_rwbs(char * rwbs,const struct blk_io_trace * t)1164 static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
1165 {
1166 	int i = 0;
1167 	int tc = t->action >> BLK_TC_SHIFT;
1168 
1169 	if (t->action == BLK_TN_MESSAGE) {
1170 		rwbs[i++] = 'N';
1171 		goto out;
1172 	}
1173 
1174 	if (tc & BLK_TC_FLUSH)
1175 		rwbs[i++] = 'F';
1176 
1177 	if (tc & BLK_TC_DISCARD)
1178 		rwbs[i++] = 'D';
1179 	else if (tc & BLK_TC_WRITE)
1180 		rwbs[i++] = 'W';
1181 	else if (t->bytes)
1182 		rwbs[i++] = 'R';
1183 	else
1184 		rwbs[i++] = 'N';
1185 
1186 	if (tc & BLK_TC_FUA)
1187 		rwbs[i++] = 'F';
1188 	if (tc & BLK_TC_AHEAD)
1189 		rwbs[i++] = 'A';
1190 	if (tc & BLK_TC_SYNC)
1191 		rwbs[i++] = 'S';
1192 	if (tc & BLK_TC_META)
1193 		rwbs[i++] = 'M';
1194 out:
1195 	rwbs[i] = '\0';
1196 }
1197 
1198 static inline
te_blk_io_trace(const struct trace_entry * ent)1199 const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
1200 {
1201 	return (const struct blk_io_trace *)ent;
1202 }
1203 
pdu_start(const struct trace_entry * ent)1204 static inline const void *pdu_start(const struct trace_entry *ent)
1205 {
1206 	return te_blk_io_trace(ent) + 1;
1207 }
1208 
t_action(const struct trace_entry * ent)1209 static inline u32 t_action(const struct trace_entry *ent)
1210 {
1211 	return te_blk_io_trace(ent)->action;
1212 }
1213 
t_bytes(const struct trace_entry * ent)1214 static inline u32 t_bytes(const struct trace_entry *ent)
1215 {
1216 	return te_blk_io_trace(ent)->bytes;
1217 }
1218 
t_sec(const struct trace_entry * ent)1219 static inline u32 t_sec(const struct trace_entry *ent)
1220 {
1221 	return te_blk_io_trace(ent)->bytes >> 9;
1222 }
1223 
t_sector(const struct trace_entry * ent)1224 static inline unsigned long long t_sector(const struct trace_entry *ent)
1225 {
1226 	return te_blk_io_trace(ent)->sector;
1227 }
1228 
t_error(const struct trace_entry * ent)1229 static inline __u16 t_error(const struct trace_entry *ent)
1230 {
1231 	return te_blk_io_trace(ent)->error;
1232 }
1233 
get_pdu_int(const struct trace_entry * ent)1234 static __u64 get_pdu_int(const struct trace_entry *ent)
1235 {
1236 	const __u64 *val = pdu_start(ent);
1237 	return be64_to_cpu(*val);
1238 }
1239 
get_pdu_remap(const struct trace_entry * ent,struct blk_io_trace_remap * r)1240 static void get_pdu_remap(const struct trace_entry *ent,
1241 			  struct blk_io_trace_remap *r)
1242 {
1243 	const struct blk_io_trace_remap *__r = pdu_start(ent);
1244 	__u64 sector_from = __r->sector_from;
1245 
1246 	r->device_from = be32_to_cpu(__r->device_from);
1247 	r->device_to   = be32_to_cpu(__r->device_to);
1248 	r->sector_from = be64_to_cpu(sector_from);
1249 }
1250 
1251 typedef void (blk_log_action_t) (struct trace_iterator *iter, const char *act);
1252 
blk_log_action_classic(struct trace_iterator * iter,const char * act)1253 static void blk_log_action_classic(struct trace_iterator *iter, const char *act)
1254 {
1255 	char rwbs[RWBS_LEN];
1256 	unsigned long long ts  = iter->ts;
1257 	unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
1258 	unsigned secs	       = (unsigned long)ts;
1259 	const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1260 
1261 	fill_rwbs(rwbs, t);
1262 
1263 	trace_seq_printf(&iter->seq,
1264 			 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
1265 			 MAJOR(t->device), MINOR(t->device), iter->cpu,
1266 			 secs, nsec_rem, iter->ent->pid, act, rwbs);
1267 }
1268 
blk_log_action(struct trace_iterator * iter,const char * act)1269 static void blk_log_action(struct trace_iterator *iter, const char *act)
1270 {
1271 	char rwbs[RWBS_LEN];
1272 	const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1273 
1274 	fill_rwbs(rwbs, t);
1275 	trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
1276 			 MAJOR(t->device), MINOR(t->device), act, rwbs);
1277 }
1278 
blk_log_dump_pdu(struct trace_seq * s,const struct trace_entry * ent)1279 static void blk_log_dump_pdu(struct trace_seq *s, const struct trace_entry *ent)
1280 {
1281 	const unsigned char *pdu_buf;
1282 	int pdu_len;
1283 	int i, end;
1284 
1285 	pdu_buf = pdu_start(ent);
1286 	pdu_len = te_blk_io_trace(ent)->pdu_len;
1287 
1288 	if (!pdu_len)
1289 		return;
1290 
1291 	/* find the last zero that needs to be printed */
1292 	for (end = pdu_len - 1; end >= 0; end--)
1293 		if (pdu_buf[end])
1294 			break;
1295 	end++;
1296 
1297 	trace_seq_putc(s, '(');
1298 
1299 	for (i = 0; i < pdu_len; i++) {
1300 
1301 		trace_seq_printf(s, "%s%02x",
1302 				 i == 0 ? "" : " ", pdu_buf[i]);
1303 
1304 		/*
1305 		 * stop when the rest is just zeroes and indicate so
1306 		 * with a ".." appended
1307 		 */
1308 		if (i == end && end != pdu_len - 1) {
1309 			trace_seq_puts(s, " ..) ");
1310 			return;
1311 		}
1312 	}
1313 
1314 	trace_seq_puts(s, ") ");
1315 }
1316 
blk_log_generic(struct trace_seq * s,const struct trace_entry * ent)1317 static void blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1318 {
1319 	char cmd[TASK_COMM_LEN];
1320 
1321 	trace_find_cmdline(ent->pid, cmd);
1322 
1323 	if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1324 		trace_seq_printf(s, "%u ", t_bytes(ent));
1325 		blk_log_dump_pdu(s, ent);
1326 		trace_seq_printf(s, "[%s]\n", cmd);
1327 	} else {
1328 		if (t_sec(ent))
1329 			trace_seq_printf(s, "%llu + %u [%s]\n",
1330 						t_sector(ent), t_sec(ent), cmd);
1331 		else
1332 			trace_seq_printf(s, "[%s]\n", cmd);
1333 	}
1334 }
1335 
blk_log_with_error(struct trace_seq * s,const struct trace_entry * ent)1336 static void blk_log_with_error(struct trace_seq *s,
1337 			      const struct trace_entry *ent)
1338 {
1339 	if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1340 		blk_log_dump_pdu(s, ent);
1341 		trace_seq_printf(s, "[%d]\n", t_error(ent));
1342 	} else {
1343 		if (t_sec(ent))
1344 			trace_seq_printf(s, "%llu + %u [%d]\n",
1345 					 t_sector(ent),
1346 					 t_sec(ent), t_error(ent));
1347 		else
1348 			trace_seq_printf(s, "%llu [%d]\n",
1349 					 t_sector(ent), t_error(ent));
1350 	}
1351 }
1352 
blk_log_remap(struct trace_seq * s,const struct trace_entry * ent)1353 static void blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1354 {
1355 	struct blk_io_trace_remap r = { .device_from = 0, };
1356 
1357 	get_pdu_remap(ent, &r);
1358 	trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
1359 			 t_sector(ent), t_sec(ent),
1360 			 MAJOR(r.device_from), MINOR(r.device_from),
1361 			 (unsigned long long)r.sector_from);
1362 }
1363 
blk_log_plug(struct trace_seq * s,const struct trace_entry * ent)1364 static void blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1365 {
1366 	char cmd[TASK_COMM_LEN];
1367 
1368 	trace_find_cmdline(ent->pid, cmd);
1369 
1370 	trace_seq_printf(s, "[%s]\n", cmd);
1371 }
1372 
blk_log_unplug(struct trace_seq * s,const struct trace_entry * ent)1373 static void blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1374 {
1375 	char cmd[TASK_COMM_LEN];
1376 
1377 	trace_find_cmdline(ent->pid, cmd);
1378 
1379 	trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent));
1380 }
1381 
blk_log_split(struct trace_seq * s,const struct trace_entry * ent)1382 static void blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1383 {
1384 	char cmd[TASK_COMM_LEN];
1385 
1386 	trace_find_cmdline(ent->pid, cmd);
1387 
1388 	trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
1389 			 get_pdu_int(ent), cmd);
1390 }
1391 
blk_log_msg(struct trace_seq * s,const struct trace_entry * ent)1392 static void blk_log_msg(struct trace_seq *s, const struct trace_entry *ent)
1393 {
1394 	const struct blk_io_trace *t = te_blk_io_trace(ent);
1395 
1396 	trace_seq_putmem(s, t + 1, t->pdu_len);
1397 	trace_seq_putc(s, '\n');
1398 }
1399 
1400 /*
1401  * struct tracer operations
1402  */
1403 
blk_tracer_print_header(struct seq_file * m)1404 static void blk_tracer_print_header(struct seq_file *m)
1405 {
1406 	if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1407 		return;
1408 	seq_puts(m, "# DEV   CPU TIMESTAMP     PID ACT FLG\n"
1409 		    "#  |     |     |           |   |   |\n");
1410 }
1411 
blk_tracer_start(struct trace_array * tr)1412 static void blk_tracer_start(struct trace_array *tr)
1413 {
1414 	blk_tracer_enabled = true;
1415 }
1416 
blk_tracer_init(struct trace_array * tr)1417 static int blk_tracer_init(struct trace_array *tr)
1418 {
1419 	blk_tr = tr;
1420 	blk_tracer_start(tr);
1421 	return 0;
1422 }
1423 
blk_tracer_stop(struct trace_array * tr)1424 static void blk_tracer_stop(struct trace_array *tr)
1425 {
1426 	blk_tracer_enabled = false;
1427 }
1428 
blk_tracer_reset(struct trace_array * tr)1429 static void blk_tracer_reset(struct trace_array *tr)
1430 {
1431 	blk_tracer_stop(tr);
1432 }
1433 
1434 static const struct {
1435 	const char *act[2];
1436 	void	   (*print)(struct trace_seq *s, const struct trace_entry *ent);
1437 } what2act[] = {
1438 	[__BLK_TA_QUEUE]	= {{  "Q", "queue" },	   blk_log_generic },
1439 	[__BLK_TA_BACKMERGE]	= {{  "M", "backmerge" },  blk_log_generic },
1440 	[__BLK_TA_FRONTMERGE]	= {{  "F", "frontmerge" }, blk_log_generic },
1441 	[__BLK_TA_GETRQ]	= {{  "G", "getrq" },	   blk_log_generic },
1442 	[__BLK_TA_SLEEPRQ]	= {{  "S", "sleeprq" },	   blk_log_generic },
1443 	[__BLK_TA_REQUEUE]	= {{  "R", "requeue" },	   blk_log_with_error },
1444 	[__BLK_TA_ISSUE]	= {{  "D", "issue" },	   blk_log_generic },
1445 	[__BLK_TA_COMPLETE]	= {{  "C", "complete" },   blk_log_with_error },
1446 	[__BLK_TA_PLUG]		= {{  "P", "plug" },	   blk_log_plug },
1447 	[__BLK_TA_UNPLUG_IO]	= {{  "U", "unplug_io" },  blk_log_unplug },
1448 	[__BLK_TA_UNPLUG_TIMER]	= {{ "UT", "unplug_timer" }, blk_log_unplug },
1449 	[__BLK_TA_INSERT]	= {{  "I", "insert" },	   blk_log_generic },
1450 	[__BLK_TA_SPLIT]	= {{  "X", "split" },	   blk_log_split },
1451 	[__BLK_TA_BOUNCE]	= {{  "B", "bounce" },	   blk_log_generic },
1452 	[__BLK_TA_REMAP]	= {{  "A", "remap" },	   blk_log_remap },
1453 };
1454 
print_one_line(struct trace_iterator * iter,bool classic)1455 static enum print_line_t print_one_line(struct trace_iterator *iter,
1456 					bool classic)
1457 {
1458 	struct trace_array *tr = iter->tr;
1459 	struct trace_seq *s = &iter->seq;
1460 	const struct blk_io_trace *t;
1461 	u16 what;
1462 	bool long_act;
1463 	blk_log_action_t *log_action;
1464 
1465 	t	   = te_blk_io_trace(iter->ent);
1466 	what	   = t->action & ((1 << BLK_TC_SHIFT) - 1);
1467 	long_act   = !!(tr->trace_flags & TRACE_ITER_VERBOSE);
1468 	log_action = classic ? &blk_log_action_classic : &blk_log_action;
1469 
1470 	if (t->action == BLK_TN_MESSAGE) {
1471 		log_action(iter, long_act ? "message" : "m");
1472 		blk_log_msg(s, iter->ent);
1473 	}
1474 
1475 	if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
1476 		trace_seq_printf(s, "Unknown action %x\n", what);
1477 	else {
1478 		log_action(iter, what2act[what].act[long_act]);
1479 		what2act[what].print(s, iter->ent);
1480 	}
1481 
1482 	return trace_handle_return(s);
1483 }
1484 
blk_trace_event_print(struct trace_iterator * iter,int flags,struct trace_event * event)1485 static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
1486 					       int flags, struct trace_event *event)
1487 {
1488 	return print_one_line(iter, false);
1489 }
1490 
blk_trace_synthesize_old_trace(struct trace_iterator * iter)1491 static void blk_trace_synthesize_old_trace(struct trace_iterator *iter)
1492 {
1493 	struct trace_seq *s = &iter->seq;
1494 	struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1495 	const int offset = offsetof(struct blk_io_trace, sector);
1496 	struct blk_io_trace old = {
1497 		.magic	  = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
1498 		.time     = iter->ts,
1499 	};
1500 
1501 	trace_seq_putmem(s, &old, offset);
1502 	trace_seq_putmem(s, &t->sector,
1503 			 sizeof(old) - offset + t->pdu_len);
1504 }
1505 
1506 static enum print_line_t
blk_trace_event_print_binary(struct trace_iterator * iter,int flags,struct trace_event * event)1507 blk_trace_event_print_binary(struct trace_iterator *iter, int flags,
1508 			     struct trace_event *event)
1509 {
1510 	blk_trace_synthesize_old_trace(iter);
1511 
1512 	return trace_handle_return(&iter->seq);
1513 }
1514 
blk_tracer_print_line(struct trace_iterator * iter)1515 static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1516 {
1517 	if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1518 		return TRACE_TYPE_UNHANDLED;
1519 
1520 	return print_one_line(iter, true);
1521 }
1522 
1523 static int
blk_tracer_set_flag(struct trace_array * tr,u32 old_flags,u32 bit,int set)1524 blk_tracer_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
1525 {
1526 	/* don't output context-info for blk_classic output */
1527 	if (bit == TRACE_BLK_OPT_CLASSIC) {
1528 		if (set)
1529 			tr->trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
1530 		else
1531 			tr->trace_flags |= TRACE_ITER_CONTEXT_INFO;
1532 	}
1533 	return 0;
1534 }
1535 
1536 static struct tracer blk_tracer __read_mostly = {
1537 	.name		= "blk",
1538 	.init		= blk_tracer_init,
1539 	.reset		= blk_tracer_reset,
1540 	.start		= blk_tracer_start,
1541 	.stop		= blk_tracer_stop,
1542 	.print_header	= blk_tracer_print_header,
1543 	.print_line	= blk_tracer_print_line,
1544 	.flags		= &blk_tracer_flags,
1545 	.set_flag	= blk_tracer_set_flag,
1546 };
1547 
1548 static struct trace_event_functions trace_blk_event_funcs = {
1549 	.trace		= blk_trace_event_print,
1550 	.binary		= blk_trace_event_print_binary,
1551 };
1552 
1553 static struct trace_event trace_blk_event = {
1554 	.type		= TRACE_BLK,
1555 	.funcs		= &trace_blk_event_funcs,
1556 };
1557 
init_blk_tracer(void)1558 static int __init init_blk_tracer(void)
1559 {
1560 	if (!register_trace_event(&trace_blk_event)) {
1561 		pr_warning("Warning: could not register block events\n");
1562 		return 1;
1563 	}
1564 
1565 	if (register_tracer(&blk_tracer) != 0) {
1566 		pr_warning("Warning: could not register the block tracer\n");
1567 		unregister_trace_event(&trace_blk_event);
1568 		return 1;
1569 	}
1570 
1571 	return 0;
1572 }
1573 
1574 device_initcall(init_blk_tracer);
1575 
blk_trace_remove_queue(struct request_queue * q)1576 static int blk_trace_remove_queue(struct request_queue *q)
1577 {
1578 	struct blk_trace *bt;
1579 
1580 	bt = xchg(&q->blk_trace, NULL);
1581 	if (bt == NULL)
1582 		return -EINVAL;
1583 
1584 	if (bt->trace_state == Blktrace_running) {
1585 		bt->trace_state = Blktrace_stopped;
1586 		spin_lock_irq(&running_trace_lock);
1587 		list_del_init(&bt->running_list);
1588 		spin_unlock_irq(&running_trace_lock);
1589 		relay_flush(bt->rchan);
1590 	}
1591 
1592 	put_probe_ref();
1593 	synchronize_rcu();
1594 	blk_trace_free(bt);
1595 	return 0;
1596 }
1597 
1598 /*
1599  * Setup everything required to start tracing
1600  */
blk_trace_setup_queue(struct request_queue * q,struct block_device * bdev)1601 static int blk_trace_setup_queue(struct request_queue *q,
1602 				 struct block_device *bdev)
1603 {
1604 	struct blk_trace *bt = NULL;
1605 	int ret = -ENOMEM;
1606 
1607 	bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1608 	if (!bt)
1609 		return -ENOMEM;
1610 
1611 	bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
1612 	if (!bt->msg_data)
1613 		goto free_bt;
1614 
1615 	bt->dev = bdev->bd_dev;
1616 	bt->act_mask = (u16)-1;
1617 
1618 	blk_trace_setup_lba(bt, bdev);
1619 
1620 	ret = -EBUSY;
1621 	if (cmpxchg(&q->blk_trace, NULL, bt))
1622 		goto free_bt;
1623 
1624 	get_probe_ref();
1625 	return 0;
1626 
1627 free_bt:
1628 	blk_trace_free(bt);
1629 	return ret;
1630 }
1631 
1632 /*
1633  * sysfs interface to enable and configure tracing
1634  */
1635 
1636 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1637 					 struct device_attribute *attr,
1638 					 char *buf);
1639 static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1640 					  struct device_attribute *attr,
1641 					  const char *buf, size_t count);
1642 #define BLK_TRACE_DEVICE_ATTR(_name) \
1643 	DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1644 		    sysfs_blk_trace_attr_show, \
1645 		    sysfs_blk_trace_attr_store)
1646 
1647 static BLK_TRACE_DEVICE_ATTR(enable);
1648 static BLK_TRACE_DEVICE_ATTR(act_mask);
1649 static BLK_TRACE_DEVICE_ATTR(pid);
1650 static BLK_TRACE_DEVICE_ATTR(start_lba);
1651 static BLK_TRACE_DEVICE_ATTR(end_lba);
1652 
1653 static struct attribute *blk_trace_attrs[] = {
1654 	&dev_attr_enable.attr,
1655 	&dev_attr_act_mask.attr,
1656 	&dev_attr_pid.attr,
1657 	&dev_attr_start_lba.attr,
1658 	&dev_attr_end_lba.attr,
1659 	NULL
1660 };
1661 
1662 struct attribute_group blk_trace_attr_group = {
1663 	.name  = "trace",
1664 	.attrs = blk_trace_attrs,
1665 };
1666 
1667 static const struct {
1668 	int mask;
1669 	const char *str;
1670 } mask_maps[] = {
1671 	{ BLK_TC_READ,		"read"		},
1672 	{ BLK_TC_WRITE,		"write"		},
1673 	{ BLK_TC_FLUSH,		"flush"		},
1674 	{ BLK_TC_SYNC,		"sync"		},
1675 	{ BLK_TC_QUEUE,		"queue"		},
1676 	{ BLK_TC_REQUEUE,	"requeue"	},
1677 	{ BLK_TC_ISSUE,		"issue"		},
1678 	{ BLK_TC_COMPLETE,	"complete"	},
1679 	{ BLK_TC_FS,		"fs"		},
1680 	{ BLK_TC_PC,		"pc"		},
1681 	{ BLK_TC_AHEAD,		"ahead"		},
1682 	{ BLK_TC_META,		"meta"		},
1683 	{ BLK_TC_DISCARD,	"discard"	},
1684 	{ BLK_TC_DRV_DATA,	"drv_data"	},
1685 	{ BLK_TC_FUA,		"fua"		},
1686 };
1687 
blk_trace_str2mask(const char * str)1688 static int blk_trace_str2mask(const char *str)
1689 {
1690 	int i;
1691 	int mask = 0;
1692 	char *buf, *s, *token;
1693 
1694 	buf = kstrdup(str, GFP_KERNEL);
1695 	if (buf == NULL)
1696 		return -ENOMEM;
1697 	s = strstrip(buf);
1698 
1699 	while (1) {
1700 		token = strsep(&s, ",");
1701 		if (token == NULL)
1702 			break;
1703 
1704 		if (*token == '\0')
1705 			continue;
1706 
1707 		for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1708 			if (strcasecmp(token, mask_maps[i].str) == 0) {
1709 				mask |= mask_maps[i].mask;
1710 				break;
1711 			}
1712 		}
1713 		if (i == ARRAY_SIZE(mask_maps)) {
1714 			mask = -EINVAL;
1715 			break;
1716 		}
1717 	}
1718 	kfree(buf);
1719 
1720 	return mask;
1721 }
1722 
blk_trace_mask2str(char * buf,int mask)1723 static ssize_t blk_trace_mask2str(char *buf, int mask)
1724 {
1725 	int i;
1726 	char *p = buf;
1727 
1728 	for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1729 		if (mask & mask_maps[i].mask) {
1730 			p += sprintf(p, "%s%s",
1731 				    (p == buf) ? "" : ",", mask_maps[i].str);
1732 		}
1733 	}
1734 	*p++ = '\n';
1735 
1736 	return p - buf;
1737 }
1738 
blk_trace_get_queue(struct block_device * bdev)1739 static struct request_queue *blk_trace_get_queue(struct block_device *bdev)
1740 {
1741 	if (bdev->bd_disk == NULL)
1742 		return NULL;
1743 
1744 	return bdev_get_queue(bdev);
1745 }
1746 
sysfs_blk_trace_attr_show(struct device * dev,struct device_attribute * attr,char * buf)1747 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1748 					 struct device_attribute *attr,
1749 					 char *buf)
1750 {
1751 	struct hd_struct *p = dev_to_part(dev);
1752 	struct request_queue *q;
1753 	struct block_device *bdev;
1754 	struct blk_trace *bt;
1755 	ssize_t ret = -ENXIO;
1756 
1757 	bdev = bdget(part_devt(p));
1758 	if (bdev == NULL)
1759 		goto out;
1760 
1761 	q = blk_trace_get_queue(bdev);
1762 	if (q == NULL)
1763 		goto out_bdput;
1764 
1765 	mutex_lock(&q->blk_trace_mutex);
1766 
1767 	bt = rcu_dereference_protected(q->blk_trace,
1768 				       lockdep_is_held(&q->blk_trace_mutex));
1769 	if (attr == &dev_attr_enable) {
1770 		ret = sprintf(buf, "%u\n", !!bt);
1771 		goto out_unlock_bdev;
1772 	}
1773 
1774 	if (bt == NULL)
1775 		ret = sprintf(buf, "disabled\n");
1776 	else if (attr == &dev_attr_act_mask)
1777 		ret = blk_trace_mask2str(buf, bt->act_mask);
1778 	else if (attr == &dev_attr_pid)
1779 		ret = sprintf(buf, "%u\n", bt->pid);
1780 	else if (attr == &dev_attr_start_lba)
1781 		ret = sprintf(buf, "%llu\n", bt->start_lba);
1782 	else if (attr == &dev_attr_end_lba)
1783 		ret = sprintf(buf, "%llu\n", bt->end_lba);
1784 
1785 out_unlock_bdev:
1786 	mutex_unlock(&q->blk_trace_mutex);
1787 out_bdput:
1788 	bdput(bdev);
1789 out:
1790 	return ret;
1791 }
1792 
sysfs_blk_trace_attr_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1793 static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1794 					  struct device_attribute *attr,
1795 					  const char *buf, size_t count)
1796 {
1797 	struct block_device *bdev;
1798 	struct request_queue *q;
1799 	struct hd_struct *p;
1800 	struct blk_trace *bt;
1801 	u64 value;
1802 	ssize_t ret = -EINVAL;
1803 
1804 	if (count == 0)
1805 		goto out;
1806 
1807 	if (attr == &dev_attr_act_mask) {
1808 		if (sscanf(buf, "%llx", &value) != 1) {
1809 			/* Assume it is a list of trace category names */
1810 			ret = blk_trace_str2mask(buf);
1811 			if (ret < 0)
1812 				goto out;
1813 			value = ret;
1814 		}
1815 	} else if (sscanf(buf, "%llu", &value) != 1)
1816 		goto out;
1817 
1818 	ret = -ENXIO;
1819 
1820 	p = dev_to_part(dev);
1821 	bdev = bdget(part_devt(p));
1822 	if (bdev == NULL)
1823 		goto out;
1824 
1825 	q = blk_trace_get_queue(bdev);
1826 	if (q == NULL)
1827 		goto out_bdput;
1828 
1829 	mutex_lock(&q->blk_trace_mutex);
1830 
1831 	bt = rcu_dereference_protected(q->blk_trace,
1832 				       lockdep_is_held(&q->blk_trace_mutex));
1833 	if (attr == &dev_attr_enable) {
1834 		if (!!value == !!bt) {
1835 			ret = 0;
1836 			goto out_unlock_bdev;
1837 		}
1838 		if (value)
1839 			ret = blk_trace_setup_queue(q, bdev);
1840 		else
1841 			ret = blk_trace_remove_queue(q);
1842 		goto out_unlock_bdev;
1843 	}
1844 
1845 	ret = 0;
1846 	if (bt == NULL) {
1847 		ret = blk_trace_setup_queue(q, bdev);
1848 		bt = rcu_dereference_protected(q->blk_trace,
1849 				lockdep_is_held(&q->blk_trace_mutex));
1850 	}
1851 
1852 	if (ret == 0) {
1853 		if (attr == &dev_attr_act_mask)
1854 			bt->act_mask = value;
1855 		else if (attr == &dev_attr_pid)
1856 			bt->pid = value;
1857 		else if (attr == &dev_attr_start_lba)
1858 			bt->start_lba = value;
1859 		else if (attr == &dev_attr_end_lba)
1860 			bt->end_lba = value;
1861 	}
1862 
1863 out_unlock_bdev:
1864 	mutex_unlock(&q->blk_trace_mutex);
1865 out_bdput:
1866 	bdput(bdev);
1867 out:
1868 	return ret ? ret : count;
1869 }
1870 
blk_trace_init_sysfs(struct device * dev)1871 int blk_trace_init_sysfs(struct device *dev)
1872 {
1873 	return sysfs_create_group(&dev->kobj, &blk_trace_attr_group);
1874 }
1875 
blk_trace_remove_sysfs(struct device * dev)1876 void blk_trace_remove_sysfs(struct device *dev)
1877 {
1878 	sysfs_remove_group(&dev->kobj, &blk_trace_attr_group);
1879 }
1880 
1881 #endif /* CONFIG_BLK_DEV_IO_TRACE */
1882 
1883 #ifdef CONFIG_EVENT_TRACING
1884 
blk_dump_cmd(char * buf,struct request * rq)1885 void blk_dump_cmd(char *buf, struct request *rq)
1886 {
1887 	int i, end;
1888 	int len = rq->cmd_len;
1889 	unsigned char *cmd = rq->cmd;
1890 
1891 	if (rq->cmd_type != REQ_TYPE_BLOCK_PC) {
1892 		buf[0] = '\0';
1893 		return;
1894 	}
1895 
1896 	for (end = len - 1; end >= 0; end--)
1897 		if (cmd[end])
1898 			break;
1899 	end++;
1900 
1901 	for (i = 0; i < len; i++) {
1902 		buf += sprintf(buf, "%s%02x", i == 0 ? "" : " ", cmd[i]);
1903 		if (i == end && end != len - 1) {
1904 			sprintf(buf, " ..");
1905 			break;
1906 		}
1907 	}
1908 }
1909 
blk_fill_rwbs(char * rwbs,u32 rw,int bytes)1910 void blk_fill_rwbs(char *rwbs, u32 rw, int bytes)
1911 {
1912 	int i = 0;
1913 
1914 	if (rw & REQ_FLUSH)
1915 		rwbs[i++] = 'F';
1916 
1917 	if (rw & WRITE)
1918 		rwbs[i++] = 'W';
1919 	else if (rw & REQ_DISCARD)
1920 		rwbs[i++] = 'D';
1921 	else if (bytes)
1922 		rwbs[i++] = 'R';
1923 	else
1924 		rwbs[i++] = 'N';
1925 
1926 	if (rw & REQ_FUA)
1927 		rwbs[i++] = 'F';
1928 	if (rw & REQ_RAHEAD)
1929 		rwbs[i++] = 'A';
1930 	if (rw & REQ_SYNC)
1931 		rwbs[i++] = 'S';
1932 	if (rw & REQ_META)
1933 		rwbs[i++] = 'M';
1934 	if (rw & REQ_SECURE)
1935 		rwbs[i++] = 'E';
1936 
1937 	rwbs[i] = '\0';
1938 }
1939 EXPORT_SYMBOL_GPL(blk_fill_rwbs);
1940 
1941 #endif /* CONFIG_EVENT_TRACING */
1942 
1943