1 diff --git a/block/Kconfig b/block/Kconfig
2 index 377f6dd..27eaed9 100644
5 @@ -11,4 +11,15 @@ config LBD
6 your machine, or if you want to have a raid or loopback device
7 bigger than 2TB. Otherwise say N.
9 +config BLK_DEV_IO_TRACE
10 + bool "Support for tracing block io actions"
13 + Say Y here, if you want to be able to trace the block layer actions
14 + on a given queue. Tracing allows you to see any traffic happening
15 + on a block device queue. For more information (and the user space
16 + support tools needed), fetch the blktrace app from:
18 + git://brick.kernel.dk/data/git/blktrace.git
20 source block/Kconfig.iosched
21 diff --git a/block/Makefile b/block/Makefile
22 index 7e4f93e..c05de0e 100644
25 @@ -8,3 +8,5 @@ obj-$(CONFIG_IOSCHED_NOOP) += noop-iosch
26 obj-$(CONFIG_IOSCHED_AS) += as-iosched.o
27 obj-$(CONFIG_IOSCHED_DEADLINE) += deadline-iosched.o
28 obj-$(CONFIG_IOSCHED_CFQ) += cfq-iosched.o
30 +obj-$(CONFIG_BLK_DEV_IO_TRACE) += blktrace.o
31 diff --git a/block/blktrace.c b/block/blktrace.c
33 index 0000000..6bfcdb8
35 +++ b/block/blktrace.c
37 +#include <linux/config.h>
38 +#include <linux/kernel.h>
39 +#include <linux/blkdev.h>
40 +#include <linux/blktrace_api.h>
41 +#include <linux/percpu.h>
42 +#include <linux/init.h>
43 +#include <linux/mutex.h>
44 +#include <asm/uaccess.h>
46 +static DEFINE_PER_CPU(unsigned long long, blk_trace_cpu_offset) = { 0, };
49 + * The worker for the various blk_add_trace*() types. Fills out a
50 + * blk_io_trace structure and places it in a per-cpu subbuffer.
52 +void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
53 + int rw, u32 what, int error, int pdu_len, void *pdu_data)
55 + struct blk_io_trace *t;
56 + unsigned long flags;
57 + unsigned long *sequence;
61 + if (rw & (1 << BIO_RW_BARRIER))
62 + what |= BLK_TC_ACT(BLK_TC_BARRIER);
63 + if (rw & (1 << BIO_RW_SYNC))
64 + what |= BLK_TC_ACT(BLK_TC_SYNC);
67 + what |= BLK_TC_ACT(BLK_TC_WRITE);
69 + what |= BLK_TC_ACT(BLK_TC_READ);
71 + if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
73 + if (sector < bt->start_lba || sector > bt->end_lba)
77 + if (bt->pid && pid != bt->pid)
81 + * A word about the locking here - we disable interrupts to reserve
82 + * some space in the relayfs per-cpu buffer, to prevent an irq
83 + * from coming in and stepping on our toes. Once reserved, it's
84 + * enough to get preemption disabled to prevent read of this data
85 + * before we are through filling it. get_cpu()/put_cpu() does this
88 + local_irq_save(flags);
90 + t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
92 + local_irq_restore(flags);
98 + sequence = per_cpu_ptr(bt->sequence, cpu);
99 + t->sequence = ++(*sequence);
100 + t->time = sched_clock() - per_cpu(blk_trace_cpu_offset, cpu);
103 + local_irq_restore(flags);
105 + t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
106 + t->device = bt->dev;
107 + t->sector = sector;
111 + t->pdu_len = pdu_len;
114 + strncpy(t->comm, current->comm, sizeof(t->comm));
117 + memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
122 +EXPORT_SYMBOL_GPL(__blk_add_trace);
124 +static struct dentry *blk_tree_root;
125 +static struct mutex blk_tree_mutex;
127 +static inline void blk_remove_root(void)
129 + if (relayfs_remove_dir(blk_tree_root) != -ENOTEMPTY)
130 + blk_tree_root = NULL;
133 +static void blk_remove_tree(struct dentry *dir)
135 + mutex_lock(&blk_tree_mutex);
136 + relayfs_remove_dir(dir);
138 + mutex_unlock(&blk_tree_mutex);
141 +static struct dentry *blk_create_tree(const char *blk_name)
143 + struct dentry *dir = NULL;
145 + mutex_lock(&blk_tree_mutex);
147 + if (!blk_tree_root) {
148 + blk_tree_root = relayfs_create_dir("block", NULL);
149 + if (!blk_tree_root)
153 + dir = relayfs_create_dir(blk_name, blk_tree_root);
158 + mutex_unlock(&blk_tree_mutex);
162 +void blk_cleanup_trace(struct blk_trace *bt)
164 + relay_close(bt->rchan);
165 + relayfs_remove_file(bt->dropped_file);
166 + blk_remove_tree(bt->dir);
170 +int blk_stop_trace(struct block_device *bdev)
172 + request_queue_t *q = bdev_get_queue(bdev);
173 + struct blk_trace *bt = NULL;
179 + down(&bdev->bd_sem);
181 + if (q->blk_trace) {
183 + q->blk_trace = NULL;
190 + blk_cleanup_trace(bt);
195 +static int blk_dropped_open(struct inode *inode, struct file *filp)
197 + filp->private_data = inode->u.generic_ip;
202 +static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
203 + size_t count, loff_t *ppos)
205 + struct blk_trace *bt = filp->private_data;
208 + snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
210 + return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
213 +static struct file_operations blk_dropped_fops = {
214 + .owner = THIS_MODULE,
215 + .open = blk_dropped_open,
216 + .read = blk_dropped_read,
220 + * Keep track of how many times we encountered a full subbuffer, to aid
221 + * the user space app in telling how many lost events there were.
223 +static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
224 + void *prev_subbuf, size_t prev_padding)
226 + struct blk_trace *bt;
228 + if (!relay_buf_full(buf))
231 + bt = buf->chan->private_data;
232 + atomic_inc(&bt->dropped);
236 +static struct rchan_callbacks blk_relay_callbacks = {
237 + .subbuf_start = blk_subbuf_start_callback,
241 + * Setup everything required to start tracing
243 +int blk_start_trace(struct block_device *bdev, char __user *arg)
245 + request_queue_t *q = bdev_get_queue(bdev);
246 + struct blk_user_trace_setup buts;
247 + struct blk_trace *bt = NULL;
248 + struct dentry *dir = NULL;
249 + char b[BDEVNAME_SIZE];
255 + if (copy_from_user(&buts, arg, sizeof(buts)))
258 + if (!buts.buf_size || !buts.buf_nr)
261 + strcpy(buts.name, bdevname(bdev, b));
264 + * some device names have larger paths - convert the slashes
265 + * to underscores for this to work as expected
267 + for (i = 0; i < strlen(buts.name); i++)
268 + if (buts.name[i] == '/')
269 + buts.name[i] = '_';
271 + if (copy_to_user(arg, &buts, sizeof(buts)))
274 + down(&bdev->bd_sem);
280 + bt = kzalloc(sizeof(*bt), GFP_KERNEL);
284 + bt->sequence = alloc_percpu(unsigned long);
289 + dir = blk_create_tree(buts.name);
294 + bt->dev = bdev->bd_dev;
295 + atomic_set(&bt->dropped, 0);
298 + bt->dropped_file = relayfs_create_file("dropped", dir, 0, &blk_dropped_fops, bt);
299 + if (!bt->dropped_file)
302 + bt->rchan = relay_open("trace", dir, buts.buf_size, buts.buf_nr, &blk_relay_callbacks);
305 + bt->rchan->private_data = bt;
307 + bt->act_mask = buts.act_mask;
309 + bt->act_mask = (u16) -1;
311 + bt->start_lba = buts.start_lba;
312 + bt->end_lba = buts.end_lba;
314 + bt->end_lba = -1ULL;
316 + bt->pid = buts.pid;
323 + if (bt && bt->dropped_file)
324 + relayfs_remove_file(bt->dropped_file);
326 + blk_remove_tree(dir);
329 + free_percpu(bt->sequence);
336 + * Average offset over two calls to sched_clock() with a gettimeofday()
339 +static void blk_check_time(unsigned long long *t)
341 + unsigned long long a, b;
345 + do_gettimeofday(&tv);
348 + *t = tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
352 +static void blk_trace_check_cpu_time(void *data)
354 + unsigned long long *t;
355 + int cpu = get_cpu();
357 + t = &per_cpu(blk_trace_cpu_offset, cpu);
360 + * Just call it twice, hopefully the second call will be cache hot
361 + * and a little more precise
370 + * Call blk_trace_check_cpu_time() on each CPU to calibrate our inter-CPU
373 +static void blk_trace_calibrate_offsets(void)
375 + unsigned long flags;
377 + smp_call_function(blk_trace_check_cpu_time, NULL, 1, 1);
378 + local_irq_save(flags);
379 + blk_trace_check_cpu_time(NULL);
380 + local_irq_restore(flags);
383 +static void blk_trace_set_ht_offsets(void)
385 +#if defined(CONFIG_SCHED_SMT)
389 + * now make sure HT siblings have the same time offset
392 + for_each_online_cpu(cpu) {
393 + unsigned long long *cpu_off, *sibling_off;
395 + for_each_cpu_mask(i, cpu_sibling_map[cpu]) {
399 + cpu_off = &per_cpu(blk_trace_cpu_offset, cpu);
400 + sibling_off = &per_cpu(blk_trace_cpu_offset, i);
401 + *sibling_off = *cpu_off;
408 +static __init int blk_trace_init(void)
410 + mutex_init(&blk_tree_mutex);
411 + blk_trace_calibrate_offsets();
412 + blk_trace_set_ht_offsets();
417 +module_init(blk_trace_init);
419 diff --git a/block/elevator.c b/block/elevator.c
420 index 96a61e0..af75304 100644
421 --- a/block/elevator.c
422 +++ b/block/elevator.c
424 #include <linux/init.h>
425 #include <linux/compiler.h>
426 #include <linux/delay.h>
427 +#include <linux/blktrace_api.h>
429 #include <asm/uaccess.h>
431 @@ -316,6 +317,8 @@ void __elv_add_request(request_queue_t *
432 struct list_head *pos;
435 + blk_add_trace_rq(q, rq, BLK_TA_INSERT);
438 rq->flags |= REQ_ORDERED_COLOR;
440 @@ -474,6 +477,7 @@ struct request *elv_next_request(request
441 * not be passed by new incoming requests
443 rq->flags |= REQ_STARTED;
444 + blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
447 if (!q->boundary_rq || q->boundary_rq == rq) {
448 diff --git a/block/ioctl.c b/block/ioctl.c
449 index e110949..63e67a2 100644
453 #include <linux/backing-dev.h>
454 #include <linux/buffer_head.h>
455 #include <linux/smp_lock.h>
456 +#include <linux/blktrace_api.h>
457 #include <asm/uaccess.h>
459 static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user *arg)
460 @@ -189,6 +190,10 @@ static int blkdev_locked_ioctl(struct fi
461 return put_ulong(arg, bdev->bd_inode->i_size >> 9);
463 return put_u64(arg, bdev->bd_inode->i_size);
464 + case BLKSTARTTRACE:
465 + return blk_start_trace(bdev, (char __user *) arg);
467 + return blk_stop_trace(bdev);
471 diff --git a/block/ll_rw_blk.c b/block/ll_rw_blk.c
472 index f9fc07e..4295103 100644
473 --- a/block/ll_rw_blk.c
474 +++ b/block/ll_rw_blk.c
476 #include <linux/writeback.h>
477 #include <linux/interrupt.h>
478 #include <linux/cpu.h>
479 +#include <linux/blktrace_api.h>
483 @@ -1557,8 +1558,10 @@ void blk_plug_device(request_queue_t *q)
484 if (test_bit(QUEUE_FLAG_STOPPED, &q->queue_flags))
487 - if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
488 + if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) {
489 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
490 + blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG);
494 EXPORT_SYMBOL(blk_plug_device);
495 @@ -1622,14 +1625,21 @@ static void blk_backing_dev_unplug(struc
497 * devices don't necessarily have an ->unplug_fn defined
500 + if (q->unplug_fn) {
501 + blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
502 + q->rq.count[READ] + q->rq.count[WRITE]);
508 static void blk_unplug_work(void *data)
510 request_queue_t *q = data;
512 + blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
513 + q->rq.count[READ] + q->rq.count[WRITE]);
518 @@ -1637,6 +1647,9 @@ static void blk_unplug_timeout(unsigned
520 request_queue_t *q = (request_queue_t *)data;
522 + blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL,
523 + q->rq.count[READ] + q->rq.count[WRITE]);
525 kblockd_schedule_work(&q->unplug_work);
528 @@ -1759,6 +1772,11 @@ void blk_cleanup_queue(request_queue_t *
530 __blk_queue_free_tags(q);
532 + if (q->blk_trace) {
533 + blk_cleanup_trace(q->blk_trace);
534 + q->blk_trace = NULL;
537 kmem_cache_free(requestq_cachep, q);
540 @@ -2110,6 +2128,8 @@ rq_starved:
545 + blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ);
549 @@ -2138,6 +2158,8 @@ static struct request *get_request_wait(
551 struct io_context *ioc;
553 + blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
555 __generic_unplug_device(q);
556 spin_unlock_irq(q->queue_lock);
558 @@ -2191,6 +2213,8 @@ EXPORT_SYMBOL(blk_get_request);
560 void blk_requeue_request(request_queue_t *q, struct request *rq)
562 + blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
564 if (blk_rq_tagged(rq))
565 blk_queue_end_tag(q, rq);
567 @@ -2825,6 +2849,8 @@ static int __make_request(request_queue_
568 if (!q->back_merge_fn(q, req, bio))
571 + blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
573 req->biotail->bi_next = bio;
575 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
576 @@ -2840,6 +2866,8 @@ static int __make_request(request_queue_
577 if (!q->front_merge_fn(q, req, bio))
580 + blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
582 bio->bi_next = req->bio;
585 @@ -2957,6 +2985,7 @@ void generic_make_request(struct bio *bi
588 int ret, nr_sectors = bio_sectors(bio);
592 /* Test device or partition size, when known. */
593 @@ -2983,6 +3012,8 @@ void generic_make_request(struct bio *bi
594 * NOTE: we don't repeat the blk_size check for each new device.
595 * Stacking drivers are expected to know what they are doing.
600 char b[BDEVNAME_SIZE];
602 @@ -3015,6 +3046,15 @@ end_io:
604 blk_partition_remap(bio);
606 + if (maxsector != -1)
607 + blk_add_trace_remap(q, bio, old_dev, bio->bi_sector,
610 + blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
612 + maxsector = bio->bi_sector;
613 + old_dev = bio->bi_bdev->bd_dev;
615 ret = q->make_request_fn(q, bio);
618 @@ -3134,6 +3174,8 @@ static int __end_that_request_first(stru
619 int total_bytes, bio_nbytes, error, next_idx = 0;
622 + blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE);
625 * extend uptodate bool to allow < 0 value to be direct io error
627 diff --git a/drivers/block/cciss.c b/drivers/block/cciss.c
628 index 12d7b9b..880892a 100644
629 --- a/drivers/block/cciss.c
630 +++ b/drivers/block/cciss.c
632 #include <linux/hdreg.h>
633 #include <linux/spinlock.h>
634 #include <linux/compat.h>
635 +#include <linux/blktrace_api.h>
636 #include <asm/uaccess.h>
639 @@ -2330,6 +2331,7 @@ static inline void complete_command( ctl
641 cmd->rq->completion_data = cmd;
642 cmd->rq->errors = status;
643 + blk_add_trace_rq(cmd->rq->q, cmd->rq, BLK_TA_COMPLETE);
644 blk_complete_request(cmd->rq);
647 diff --git a/drivers/md/dm.c b/drivers/md/dm.c
648 index e9adeb9..c8f3aa2 100644
649 --- a/drivers/md/dm.c
650 +++ b/drivers/md/dm.c
652 #include <linux/mempool.h>
653 #include <linux/slab.h>
654 #include <linux/idr.h>
655 +#include <linux/blktrace_api.h>
657 static const char *_name = DM_NAME;
659 @@ -334,6 +335,8 @@ static void dec_pending(struct dm_io *io
660 /* nudge anyone waiting on suspend queue */
661 wake_up(&io->md->wait);
663 + blk_add_trace_bio(io->md->queue, io->bio, BLK_TA_COMPLETE);
665 bio_endio(io->bio, io->bio->bi_size, io->error);
668 @@ -392,6 +395,7 @@ static void __map_bio(struct dm_target *
669 struct target_io *tio)
676 @@ -407,10 +411,17 @@ static void __map_bio(struct dm_target *
679 atomic_inc(&tio->io->io_count);
680 + sector = clone->bi_sector;
681 r = ti->type->map(ti, clone, &tio->info);
684 /* the bio has been remapped so dispatch it */
686 + blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
687 + tio->io->bio->bi_bdev->bd_dev, sector,
690 generic_make_request(clone);
694 /* error the io and bail out */
695 diff --git a/fs/bio.c b/fs/bio.c
696 index 1f3bb50..0dd0d81 100644
700 #include <linux/module.h>
701 #include <linux/mempool.h>
702 #include <linux/workqueue.h>
703 +#include <linux/blktrace_api.h>
704 #include <scsi/sg.h> /* for struct sg_iovec */
706 #define BIO_POOL_SIZE 256
707 @@ -1095,6 +1096,9 @@ struct bio_pair *bio_split(struct bio *b
711 + blk_add_trace_pdu_int(bdev_get_queue(bi->bi_bdev), BLK_TA_SPLIT, bi,
712 + bi->bi_sector + first_sectors);
714 BUG_ON(bi->bi_vcnt != 1);
715 BUG_ON(bi->bi_idx != 0);
716 atomic_set(&bp->cnt, 3);
717 diff --git a/fs/compat_ioctl.c b/fs/compat_ioctl.c
718 index 5dd0207..010e02b 100644
719 --- a/fs/compat_ioctl.c
720 +++ b/fs/compat_ioctl.c
722 #include <linux/i2c-dev.h>
723 #include <linux/wireless.h>
724 #include <linux/atalk.h>
725 +#include <linux/blktrace_api.h>
727 #include <net/sock.h> /* siocdevprivate_ioctl */
728 #include <net/bluetooth/bluetooth.h>
729 diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
730 index 860e7a4..266ce9d 100644
731 --- a/include/linux/blkdev.h
732 +++ b/include/linux/blkdev.h
733 @@ -22,6 +22,7 @@ typedef struct request_queue request_que
734 struct elevator_queue;
735 typedef struct elevator_queue elevator_t;
736 struct request_pm_state;
739 #define BLKDEV_MIN_RQ 4
740 #define BLKDEV_MAX_RQ 128 /* Default maximum */
741 @@ -416,6 +417,8 @@ struct request_queue
742 unsigned int sg_reserved_size;
745 + struct blk_trace *blk_trace;
748 * reserved for flush operations
750 diff --git a/include/linux/blktrace_api.h b/include/linux/blktrace_api.h
752 index 0000000..b0fc5cf
754 +++ b/include/linux/blktrace_api.h
759 +#include <linux/config.h>
760 +#include <linux/blkdev.h>
761 +#include <linux/relayfs_fs.h>
767 + BLK_TC_READ = 1 << 0, /* reads */
768 + BLK_TC_WRITE = 1 << 1, /* writes */
769 + BLK_TC_BARRIER = 1 << 2, /* barrier */
770 + BLK_TC_SYNC = 1 << 3, /* barrier */
771 + BLK_TC_QUEUE = 1 << 4, /* queueing/merging */
772 + BLK_TC_REQUEUE = 1 << 5, /* requeueing */
773 + BLK_TC_ISSUE = 1 << 6, /* issue */
774 + BLK_TC_COMPLETE = 1 << 7, /* completions */
775 + BLK_TC_FS = 1 << 8, /* fs requests */
776 + BLK_TC_PC = 1 << 9, /* pc requests */
778 + BLK_TC_END = 1 << 15, /* only 16-bits, reminder */
781 +#define BLK_TC_SHIFT (16)
782 +#define BLK_TC_ACT(act) ((act) << BLK_TC_SHIFT)
785 + * Basic trace actions
788 + __BLK_TA_QUEUE = 1, /* queued */
789 + __BLK_TA_BACKMERGE, /* back merged to existing rq */
790 + __BLK_TA_FRONTMERGE, /* front merge to existing rq */
791 + __BLK_TA_GETRQ, /* allocated new request */
792 + __BLK_TA_SLEEPRQ, /* sleeping on rq allocation */
793 + __BLK_TA_REQUEUE, /* request requeued */
794 + __BLK_TA_ISSUE, /* sent to driver */
795 + __BLK_TA_COMPLETE, /* completed by driver */
796 + __BLK_TA_PLUG, /* queue was plugged */
797 + __BLK_TA_UNPLUG_IO, /* queue was unplugged by io */
798 + __BLK_TA_UNPLUG_TIMER, /* queue was unplugged by timer */
799 + __BLK_TA_INSERT, /* insert request */
800 + __BLK_TA_SPLIT, /* bio was split */
801 + __BLK_TA_BOUNCE, /* bio was bounced */
802 + __BLK_TA_REMAP, /* bio was remapped */
806 + * Trace actions in full. Additionally, read or write is masked
808 +#define BLK_TA_QUEUE (__BLK_TA_QUEUE | BLK_TC_ACT(BLK_TC_QUEUE))
809 +#define BLK_TA_BACKMERGE (__BLK_TA_BACKMERGE | BLK_TC_ACT(BLK_TC_QUEUE))
810 +#define BLK_TA_FRONTMERGE (__BLK_TA_FRONTMERGE | BLK_TC_ACT(BLK_TC_QUEUE))
811 +#define BLK_TA_GETRQ (__BLK_TA_GETRQ | BLK_TC_ACT(BLK_TC_QUEUE))
812 +#define BLK_TA_SLEEPRQ (__BLK_TA_SLEEPRQ | BLK_TC_ACT(BLK_TC_QUEUE))
813 +#define BLK_TA_REQUEUE (__BLK_TA_REQUEUE | BLK_TC_ACT(BLK_TC_REQUEUE))
814 +#define BLK_TA_ISSUE (__BLK_TA_ISSUE | BLK_TC_ACT(BLK_TC_ISSUE))
815 +#define BLK_TA_COMPLETE (__BLK_TA_COMPLETE| BLK_TC_ACT(BLK_TC_COMPLETE))
816 +#define BLK_TA_PLUG (__BLK_TA_PLUG | BLK_TC_ACT(BLK_TC_QUEUE))
817 +#define BLK_TA_UNPLUG_IO (__BLK_TA_UNPLUG_IO | BLK_TC_ACT(BLK_TC_QUEUE))
818 +#define BLK_TA_UNPLUG_TIMER (__BLK_TA_UNPLUG_TIMER | BLK_TC_ACT(BLK_TC_QUEUE))
819 +#define BLK_TA_INSERT (__BLK_TA_INSERT | BLK_TC_ACT(BLK_TC_QUEUE))
820 +#define BLK_TA_SPLIT (__BLK_TA_SPLIT)
821 +#define BLK_TA_BOUNCE (__BLK_TA_BOUNCE)
822 +#define BLK_TA_REMAP (__BLK_TA_REMAP | BLK_TC_ACT(BLK_TC_QUEUE))
824 +#define BLK_IO_TRACE_MAGIC 0x65617400
825 +#define BLK_IO_TRACE_VERSION 0x06
830 +struct blk_io_trace {
831 + u32 magic; /* MAGIC << 8 | version */
832 + u32 sequence; /* event number */
833 + u64 time; /* in microseconds */
834 + u64 sector; /* disk offset */
835 + u32 bytes; /* transfer length */
836 + u32 action; /* what happened */
837 + u32 pid; /* who did it */
838 + u32 cpu; /* on what cpu did it happen */
839 + u16 error; /* completion error */
840 + u16 pdu_len; /* length of data after this trace */
841 + u32 device; /* device number */
842 + char comm[16]; /* task command name (TASK_COMM_LEN) */
848 +struct blk_io_trace_remap {
855 + struct dentry *dir;
856 + struct rchan *rchan;
857 + struct dentry *dropped_file;
859 + unsigned long *sequence;
868 + * User setup structure passed with BLKSTARTTRACE
870 +struct blk_user_trace_setup {
871 + char name[BDEVNAME_SIZE]; /* output */
872 + u16 act_mask; /* input */
873 + u32 buf_size; /* input */
874 + u32 buf_nr; /* input */
880 +#if defined(CONFIG_BLK_DEV_IO_TRACE)
881 +extern int blk_start_trace(struct block_device *, char __user *);
882 +extern int blk_stop_trace(struct block_device *);
883 +extern void blk_cleanup_trace(struct blk_trace *);
884 +extern void __blk_add_trace(struct blk_trace *, sector_t, int, int, u32, int, int, void *);
887 + * blk_add_trace_rq - Add a trace for a request oriented action
888 + * @q: queue the io is for
889 + * @rq: the source request
890 + * @what: the action
893 + * Records an action against a request. Will log the bio offset + size.
896 +static inline void blk_add_trace_rq(struct request_queue *q, struct request *rq,
899 + struct blk_trace *bt = q->blk_trace;
900 + int rw = rq->flags & 0x07;
905 + if (blk_pc_request(rq)) {
906 + what |= BLK_TC_ACT(BLK_TC_PC);
907 + __blk_add_trace(bt, 0, rq->data_len, rw, what, rq->errors, sizeof(rq->cmd), rq->cmd);
909 + what |= BLK_TC_ACT(BLK_TC_FS);
910 + __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9, rw, what, rq->errors, 0, NULL);
915 + * blk_add_trace_bio - Add a trace for a bio oriented action
916 + * @q: queue the io is for
917 + * @bio: the source bio
918 + * @what: the action
921 + * Records an action against a bio. Will log the bio offset + size.
924 +static inline void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
927 + struct blk_trace *bt = q->blk_trace;
932 + __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what, !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
936 + * blk_add_trace_generic - Add a trace for a generic action
937 + * @q: queue the io is for
938 + * @bio: the source bio
939 + * @rw: the data direction
940 + * @what: the action
943 + * Records a simple trace
946 +static inline void blk_add_trace_generic(struct request_queue *q,
947 + struct bio *bio, int rw, u32 what)
949 + struct blk_trace *bt = q->blk_trace;
955 + blk_add_trace_bio(q, bio, what);
957 + __blk_add_trace(bt, 0, 0, rw, what, 0, 0, NULL);
961 + * blk_add_trace_pdu_int - Add a trace for a bio with an integer payload
962 + * @q: queue the io is for
963 + * @what: the action
964 + * @bio: the source bio
965 + * @pdu: the integer payload
968 + * Adds a trace with some integer payload. This might be an unplug
969 + * option given as the action, with the depth at unplug time given
973 +static inline void blk_add_trace_pdu_int(struct request_queue *q, u32 what,
974 + struct bio *bio, unsigned int pdu)
976 + struct blk_trace *bt = q->blk_trace;
977 + u64 rpdu = cpu_to_be64(pdu);
983 + __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what, !bio_flagged(bio, BIO_UPTODATE), sizeof(rpdu), &rpdu);
985 + __blk_add_trace(bt, 0, 0, 0, what, 0, sizeof(rpdu), &rpdu);
989 + * blk_add_trace_remap - Add a trace for a remap operation
990 + * @q: queue the io is for
991 + * @bio: the source bio
992 + * @dev: target device
993 + * @from: source sector
994 + * @to: target sector
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.
1001 +static inline void blk_add_trace_remap(struct request_queue *q, struct bio *bio,
1002 + dev_t dev, sector_t from, sector_t to)
1004 + struct blk_trace *bt = q->blk_trace;
1005 + struct blk_io_trace_remap r;
1010 + r.device = cpu_to_be32(dev);
1011 + r.sector = cpu_to_be64(to);
1013 + __blk_add_trace(bt, from, bio->bi_size, bio->bi_rw, BLK_TA_REMAP, !bio_flagged(bio, BIO_UPTODATE), sizeof(r), &r);
1016 +#else /* !CONFIG_BLK_DEV_IO_TRACE */
1017 +#define blk_start_trace(bdev, arg) (-EINVAL)
1018 +#define blk_stop_trace(bdev) (-EINVAL)
1019 +#define blk_cleanup_trace(bt) do { } while (0)
1020 +#define blk_add_trace_rq(q, rq, what) do { } while (0)
1021 +#define blk_add_trace_bio(q, rq, what) do { } while (0)
1022 +#define blk_add_trace_generic(q, rq, rw, what) do { } while (0)
1023 +#define blk_add_trace_pdu_int(q, what, bio, pdu) do { } while (0)
1024 +#define blk_add_trace_remap(q, bio, dev, f, t) do {} while (0)
1025 +#endif /* CONFIG_BLK_DEV_IO_TRACE */
1028 diff --git a/include/linux/compat_ioctl.h b/include/linux/compat_ioctl.h
1029 index 8fad50f..5bed09a 100644
1030 --- a/include/linux/compat_ioctl.h
1031 +++ b/include/linux/compat_ioctl.h
1032 @@ -97,6 +97,8 @@ COMPATIBLE_IOCTL(BLKRRPART)
1033 COMPATIBLE_IOCTL(BLKFLSBUF)
1034 COMPATIBLE_IOCTL(BLKSECTSET)
1035 COMPATIBLE_IOCTL(BLKSSZGET)
1036 +COMPATIBLE_IOCTL(BLKSTARTTRACE)
1037 +COMPATIBLE_IOCTL(BLKSTOPTRACE)
1038 ULONG_IOCTL(BLKRASET)
1039 ULONG_IOCTL(BLKFRASET)
1041 diff --git a/include/linux/fs.h b/include/linux/fs.h
1042 index e059da9..35c6b45 100644
1043 --- a/include/linux/fs.h
1044 +++ b/include/linux/fs.h
1045 @@ -196,6 +196,8 @@ extern int dir_notify_enable;
1046 #define BLKBSZGET _IOR(0x12,112,size_t)
1047 #define BLKBSZSET _IOW(0x12,113,size_t)
1048 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
1049 +#define BLKSTARTTRACE _IOWR(0x12,115,struct blk_user_trace_setup)
1050 +#define BLKSTOPTRACE _IO(0x12,116)
1052 #define BMAP_IOCTL 1 /* obsolete - kept for compatibility */
1053 #define FIBMAP _IO(0x00,1) /* bmap access */
1054 diff --git a/mm/highmem.c b/mm/highmem.c
1055 index ce2e7e8..d0ea1ee 100644
1059 #include <linux/init.h>
1060 #include <linux/hash.h>
1061 #include <linux/highmem.h>
1062 +#include <linux/blktrace_api.h>
1063 #include <asm/tlbflush.h>
1065 static mempool_t *page_pool, *isa_page_pool;
1066 @@ -483,6 +484,8 @@ void blk_queue_bounce(request_queue_t *q
1067 pool = isa_page_pool;
1070 + blk_add_trace_bio(q, *bio_orig, BLK_TA_BOUNCE);