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