[PATCH] kernel: update to include Alan's relay_reserve() method
[blktrace.git] / kernel / blk-trace-2.6.16-rc1-git-T0
1 diff --git a/block/Kconfig b/block/Kconfig
2 index 377f6dd..27eaed9 100644
3 --- a/block/Kconfig
4 +++ b/block/Kconfig
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.
8  
9 +config BLK_DEV_IO_TRACE
10 +       bool "Support for tracing block io actions"
11 +       select RELAYFS_FS
12 +       help
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:
17 +
18 +         git://brick.kernel.dk/data/git/blktrace.git
19 +
20  source block/Kconfig.iosched
21 diff --git a/block/Makefile b/block/Makefile
22 index 7e4f93e..c05de0e 100644
23 --- a/block/Makefile
24 +++ b/block/Makefile
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
29 +
30 +obj-$(CONFIG_BLK_DEV_IO_TRACE) += blktrace.o
31 diff --git a/block/blktrace.c b/block/blktrace.c
32 new file mode 100644
33 index 0000000..d12d166
34 --- /dev/null
35 +++ b/block/blktrace.c
36 @@ -0,0 +1,360 @@
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>
45 +
46 +static DEFINE_PER_CPU(unsigned long long, blk_trace_cpu_offset) = { 0, };
47 +
48 +void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
49 +                    int rw, u32 what, int error, int pdu_len, void *pdu_data)
50 +{
51 +       struct blk_io_trace t;
52 +       unsigned long flags;
53 +       void *rbuf;
54 +       pid_t pid;
55 +       int cpu;
56 +
57 +       if (rw & (1 << BIO_RW_BARRIER))
58 +               what |= BLK_TC_ACT(BLK_TC_BARRIER);
59 +       if (rw & (1 << BIO_RW_SYNC))
60 +               what |= BLK_TC_ACT(BLK_TC_SYNC);
61 +
62 +       if (rw & WRITE)
63 +               what |= BLK_TC_ACT(BLK_TC_WRITE);
64 +       else
65 +               what |= BLK_TC_ACT(BLK_TC_READ);
66 +
67 +       if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
68 +               return;
69 +       if (sector < bt->start_lba || sector > bt->end_lba)
70 +               return;
71 +
72 +       pid = current->pid;
73 +       if (bt->pid && pid != bt->pid)
74 +               return;
75 +
76 +       t.magic         = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
77 +
78 +       t.device        = bt->dev;
79 +       t.sector        = sector;
80 +       t.bytes         = bytes;
81 +       t.action        = what;
82 +       t.error         = error;
83 +       t.pdu_len       = pdu_len;
84 +
85 +       t.pid           = pid;
86 +       memcpy(t.comm, current->comm, sizeof(t.comm));
87 +
88 +       /*
89 +        * need to serialize this part completely to prevent multiple CPUs
90 +        * from misordering events
91 +        */
92 +       spin_lock_irqsave(&bt->lock, flags);
93 +
94 +       t.sequence      = ++bt->sequence;
95 +
96 +       cpu = smp_processor_id();
97 +       t.cpu           = cpu;
98 +       t.time          = sched_clock() - per_cpu(blk_trace_cpu_offset, cpu);
99 +
100 +       rbuf = relay_reserve(bt->rchan, sizeof(t) + pdu_len);
101 +       if (rbuf) {
102 +               memcpy(rbuf, &t, sizeof(t));
103 +               if (pdu_len)
104 +                       memcpy(rbuf + sizeof(t), pdu_data, pdu_len);
105 +       }
106 +
107 +       spin_unlock_irqrestore(&bt->lock, flags);
108 +}
109 +
110 +EXPORT_SYMBOL_GPL(__blk_add_trace);
111 +
112 +static struct dentry *blk_tree_root;
113 +static struct mutex blk_tree_mutex;
114 +
115 +static inline void blk_remove_root(void)
116 +{
117 +       if (relayfs_remove_dir(blk_tree_root) != -ENOTEMPTY)
118 +               blk_tree_root = NULL;
119 +}
120 +
121 +static void blk_remove_tree(struct dentry *dir)
122 +{
123 +       mutex_lock(&blk_tree_mutex);
124 +       relayfs_remove_dir(dir);
125 +       blk_remove_root();
126 +       mutex_unlock(&blk_tree_mutex);
127 +}
128 +
129 +static struct dentry *blk_create_tree(const char *blk_name)
130 +{
131 +       struct dentry *dir = NULL;
132 +
133 +       mutex_lock(&blk_tree_mutex);
134 +
135 +       if (!blk_tree_root) {
136 +               blk_tree_root = relayfs_create_dir("block", NULL);
137 +               if (!blk_tree_root)
138 +                       goto err;
139 +       }
140 +
141 +       dir = relayfs_create_dir(blk_name, blk_tree_root);
142 +       if (!dir)
143 +               blk_remove_root();
144 +
145 +err:
146 +       mutex_unlock(&blk_tree_mutex);
147 +       return dir;
148 +}
149 +
150 +void blk_cleanup_trace(struct blk_trace *bt)
151 +{
152 +       relay_close(bt->rchan);
153 +       relayfs_remove_file(bt->dropped_file);
154 +       blk_remove_tree(bt->dir);
155 +       kfree(bt);
156 +}
157 +
158 +int blk_stop_trace(struct block_device *bdev)
159 +{
160 +       request_queue_t *q = bdev_get_queue(bdev);
161 +       struct blk_trace *bt = NULL;
162 +       int ret = -EINVAL;
163 +
164 +       if (!q)
165 +               return -ENXIO;
166 +
167 +       down(&bdev->bd_sem);
168 +
169 +       if (q->blk_trace) {
170 +               bt = q->blk_trace;
171 +               q->blk_trace = NULL;
172 +               ret = 0;
173 +       }
174 +
175 +       up(&bdev->bd_sem);
176 +
177 +       if (bt)
178 +               blk_cleanup_trace(bt);
179 +
180 +       return ret;
181 +}
182 +
183 +static int blk_dropped_open(struct inode *inode, struct file *filp)
184 +{
185 +       filp->private_data = inode->u.generic_ip;
186 +       
187 +       return 0;
188 +}
189 +
190 +static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
191 +                               size_t count, loff_t *ppos)
192 +{
193 +       struct blk_trace *bt = filp->private_data;
194 +       char buf[16];
195 +
196 +       snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
197 +       
198 +       return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
199 +}
200 +
201 +static struct file_operations blk_dropped_fops = {
202 +       .owner =        THIS_MODULE,
203 +       .open =         blk_dropped_open,
204 +       .read =         blk_dropped_read,
205 +};
206 +
207 +static int blk_subbuf_start_callback (struct rchan_buf *buf,
208 +                                     void *subbuf,
209 +                                     void *prev_subbuf,
210 +                                     size_t prev_padding)
211 +{
212 +       if (relay_buf_full(buf)) {
213 +               struct blk_trace *bt = buf->chan->private_data;
214 +
215 +               atomic_inc(&bt->dropped);
216 +               return 0;
217 +       }
218 +
219 +       return 1;
220 +}
221 +
222 +static struct rchan_callbacks blk_relay_callbacks = {
223 +       .subbuf_start = blk_subbuf_start_callback,
224 +};
225 +
226 +int blk_start_trace(struct block_device *bdev, char __user *arg)
227 +{
228 +       request_queue_t *q = bdev_get_queue(bdev);
229 +       struct blk_user_trace_setup buts;
230 +       struct blk_trace *bt = NULL;
231 +       struct dentry *dir = NULL;
232 +       char b[BDEVNAME_SIZE];
233 +       int ret, i;
234 +
235 +       if (!q)
236 +               return -ENXIO;
237 +
238 +       if (copy_from_user(&buts, arg, sizeof(buts)))
239 +               return -EFAULT;
240 +
241 +       if (!buts.buf_size || !buts.buf_nr)
242 +               return -EINVAL;
243 +
244 +       strcpy(buts.name, bdevname(bdev, b));
245 +
246 +       /*
247 +        * some device names have larger paths - convert the slashes
248 +        * to underscores for this to work as expected
249 +        */
250 +       for (i = 0; i < strlen(buts.name); i++)
251 +               if (buts.name[i] == '/')
252 +                       buts.name[i] = '_';
253 +
254 +       if (copy_to_user(arg, &buts, sizeof(buts)))
255 +               return -EFAULT;
256 +
257 +       down(&bdev->bd_sem);
258 +       ret = -EBUSY;
259 +       if (q->blk_trace)
260 +               goto err;
261 +
262 +       ret = -ENOMEM;
263 +       bt = kmalloc(sizeof(*bt), GFP_KERNEL);
264 +       if (!bt)
265 +               goto err;
266 +
267 +       ret = -ENOENT;
268 +       dir = blk_create_tree(buts.name);
269 +       if (!dir)
270 +               goto err;
271 +
272 +       bt->dir = dir;
273 +       bt->dev = bdev->bd_dev;
274 +       bt->sequence = 0;
275 +       spin_lock_init(&bt->lock);
276 +       atomic_set(&bt->dropped, 0);
277 +
278 +       ret = -EIO;
279 +       bt->dropped_file = relayfs_create_file("dropped", dir, 0, &blk_dropped_fops, bt);
280 +       if (!bt->dropped_file)
281 +               goto err;
282 +
283 +       bt->rchan = relay_open("trace", dir, buts.buf_size, buts.buf_nr, &blk_relay_callbacks);
284 +       if (!bt->rchan)
285 +               goto err;
286 +       bt->rchan->private_data = bt;
287 +
288 +       bt->act_mask = buts.act_mask;
289 +       if (!bt->act_mask)
290 +               bt->act_mask = (u16) -1;
291 +
292 +       bt->start_lba = buts.start_lba;
293 +       bt->end_lba = buts.end_lba;
294 +       if (!bt->end_lba)
295 +               bt->end_lba = -1ULL;
296 +
297 +       bt->pid = buts.pid;
298 +
299 +       q->blk_trace = bt;
300 +       up(&bdev->bd_sem);
301 +       return 0;
302 +err:
303 +       up(&bdev->bd_sem);
304 +       if (bt && bt->dropped_file)
305 +               relayfs_remove_file(bt->dropped_file);
306 +       if (dir)
307 +               blk_remove_tree(dir);
308 +       if (bt)
309 +               kfree(bt);
310 +       return ret;
311 +}
312 +
313 +/*
314 + * Average offset over two calls to sched_clock() with a gettimeofday()
315 + * in the middle
316 + */
317 +static void blk_check_time(unsigned long long *t)
318 +{
319 +       unsigned long long a, b;
320 +       struct timeval tv;
321 +
322 +       a = sched_clock();
323 +       do_gettimeofday(&tv);
324 +       b = sched_clock();
325 +
326 +       *t = tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
327 +       *t -= (a + b) / 2;
328 +}
329 +
330 +static void blk_trace_check_cpu_time(void *data)
331 +{
332 +       unsigned long long *t;
333 +       int cpu = get_cpu();
334 +
335 +       t = &per_cpu(blk_trace_cpu_offset, cpu);
336 +
337 +       /*
338 +        * Just call it twice, hopefully the second call will be cache hot
339 +        * and a little more precise
340 +        */
341 +       blk_check_time(t);
342 +       blk_check_time(t);
343 +
344 +       put_cpu();
345 +}
346 +
347 +/*
348 + * Call blk_trace_check_cpu_time() on each CPU to calibrate our inter-CPU
349 + * timings
350 + */
351 +static void blk_trace_calibrate_offsets(void)
352 +{
353 +       unsigned long flags;
354 +
355 +       smp_call_function(blk_trace_check_cpu_time, NULL, 1, 1);
356 +       local_irq_save(flags);
357 +       blk_trace_check_cpu_time(NULL);
358 +       local_irq_restore(flags);
359 +}
360 +
361 +static void blk_trace_set_ht_offsets(void)
362 +{
363 +#if defined(CONFIG_SCHED_SMT)
364 +       int cpu, i;
365 +
366 +       /*
367 +        * now make sure HT siblings have the same time offset
368 +        */
369 +       preempt_disable();
370 +       for_each_online_cpu(cpu) {
371 +               unsigned long long *cpu_off, *sibling_off;
372 +
373 +               for_each_cpu_mask(i, cpu_sibling_map[cpu]) {
374 +                       if (i == cpu)
375 +                               continue;
376 +
377 +                       cpu_off = &per_cpu(blk_trace_cpu_offset, cpu);
378 +                       sibling_off = &per_cpu(blk_trace_cpu_offset, i);
379 +                       *sibling_off = *cpu_off;
380 +               }
381 +       }
382 +       preempt_enable();
383 +#endif
384 +}
385 +
386 +static __init int blk_trace_init(void)
387 +{
388 +       mutex_init(&blk_tree_mutex);
389 +       blk_trace_calibrate_offsets();
390 +       blk_trace_set_ht_offsets();
391 +
392 +       return 0;
393 +}
394 +
395 +module_init(blk_trace_init);
396 +
397 diff --git a/block/elevator.c b/block/elevator.c
398 index c9f424d..793c686 100644
399 --- a/block/elevator.c
400 +++ b/block/elevator.c
401 @@ -33,6 +33,7 @@
402  #include <linux/init.h>
403  #include <linux/compiler.h>
404  #include <linux/delay.h>
405 +#include <linux/blktrace_api.h>
406  
407  #include <asm/uaccess.h>
408  
409 @@ -333,6 +334,8 @@ void __elv_add_request(request_queue_t *
410         struct list_head *pos;
411         unsigned ordseq;
412  
413 +       blk_add_trace_rq(q, rq, BLK_TA_INSERT);
414 +
415         if (q->ordcolor)
416                 rq->flags |= REQ_ORDERED_COLOR;
417  
418 @@ -491,6 +494,7 @@ struct request *elv_next_request(request
419                          * not be passed by new incoming requests
420                          */
421                         rq->flags |= REQ_STARTED;
422 +                       blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
423                 }
424  
425                 if (!q->boundary_rq || q->boundary_rq == rq) {
426 diff --git a/block/ioctl.c b/block/ioctl.c
427 index e110949..63e67a2 100644
428 --- a/block/ioctl.c
429 +++ b/block/ioctl.c
430 @@ -5,6 +5,7 @@
431  #include <linux/backing-dev.h>
432  #include <linux/buffer_head.h>
433  #include <linux/smp_lock.h>
434 +#include <linux/blktrace_api.h>
435  #include <asm/uaccess.h>
436  
437  static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user *arg)
438 @@ -189,6 +190,10 @@ static int blkdev_locked_ioctl(struct fi
439                 return put_ulong(arg, bdev->bd_inode->i_size >> 9);
440         case BLKGETSIZE64:
441                 return put_u64(arg, bdev->bd_inode->i_size);
442 +       case BLKSTARTTRACE:
443 +               return blk_start_trace(bdev, (char __user *) arg);
444 +       case BLKSTOPTRACE:
445 +               return blk_stop_trace(bdev);
446         }
447         return -ENOIOCTLCMD;
448  }
449 diff --git a/block/ll_rw_blk.c b/block/ll_rw_blk.c
450 index 8e27d0a..bfcde0f 100644
451 --- a/block/ll_rw_blk.c
452 +++ b/block/ll_rw_blk.c
453 @@ -28,6 +28,7 @@
454  #include <linux/writeback.h>
455  #include <linux/interrupt.h>
456  #include <linux/cpu.h>
457 +#include <linux/blktrace_api.h>
458  
459  /*
460   * for max sense size
461 @@ -1555,8 +1556,10 @@ void blk_plug_device(request_queue_t *q)
462         if (test_bit(QUEUE_FLAG_STOPPED, &q->queue_flags))
463                 return;
464  
465 -       if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
466 +       if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) {
467                 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
468 +               blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG);
469 +       }
470  }
471  
472  EXPORT_SYMBOL(blk_plug_device);
473 @@ -1620,14 +1623,21 @@ static void blk_backing_dev_unplug(struc
474         /*
475          * devices don't necessarily have an ->unplug_fn defined
476          */
477 -       if (q->unplug_fn)
478 +       if (q->unplug_fn) {
479 +               blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
480 +                                       q->rq.count[READ] + q->rq.count[WRITE]);
481 +
482                 q->unplug_fn(q);
483 +       }
484  }
485  
486  static void blk_unplug_work(void *data)
487  {
488         request_queue_t *q = data;
489  
490 +       blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
491 +                               q->rq.count[READ] + q->rq.count[WRITE]);
492 +
493         q->unplug_fn(q);
494  }
495  
496 @@ -1635,6 +1645,9 @@ static void blk_unplug_timeout(unsigned 
497  {
498         request_queue_t *q = (request_queue_t *)data;
499  
500 +       blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL,
501 +                               q->rq.count[READ] + q->rq.count[WRITE]);
502 +
503         kblockd_schedule_work(&q->unplug_work);
504  }
505  
506 @@ -1757,6 +1770,11 @@ void blk_cleanup_queue(request_queue_t *
507         if (q->queue_tags)
508                 __blk_queue_free_tags(q);
509  
510 +       if (q->blk_trace) {
511 +               blk_cleanup_trace(q->blk_trace);
512 +               q->blk_trace = NULL;
513 +       }
514 +
515         kmem_cache_free(requestq_cachep, q);
516  }
517  
518 @@ -2108,6 +2126,8 @@ rq_starved:
519         
520         rq_init(q, rq);
521         rq->rl = rl;
522 +
523 +       blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ);
524  out:
525         return rq;
526  }
527 @@ -2136,6 +2156,8 @@ static struct request *get_request_wait(
528                 if (!rq) {
529                         struct io_context *ioc;
530  
531 +                       blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
532 +
533                         __generic_unplug_device(q);
534                         spin_unlock_irq(q->queue_lock);
535                         io_schedule();
536 @@ -2189,6 +2211,8 @@ EXPORT_SYMBOL(blk_get_request);
537   */
538  void blk_requeue_request(request_queue_t *q, struct request *rq)
539  {
540 +       blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
541 +
542         if (blk_rq_tagged(rq))
543                 blk_queue_end_tag(q, rq);
544  
545 @@ -2820,6 +2844,8 @@ static int __make_request(request_queue_
546                         if (!q->back_merge_fn(q, req, bio))
547                                 break;
548  
549 +                       blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
550 +
551                         req->biotail->bi_next = bio;
552                         req->biotail = bio;
553                         req->nr_sectors = req->hard_nr_sectors += nr_sectors;
554 @@ -2835,6 +2861,8 @@ static int __make_request(request_queue_
555                         if (!q->front_merge_fn(q, req, bio))
556                                 break;
557  
558 +                       blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
559 +
560                         bio->bi_next = req->bio;
561                         req->bio = bio;
562  
563 @@ -2952,6 +2980,7 @@ void generic_make_request(struct bio *bi
564         request_queue_t *q;
565         sector_t maxsector;
566         int ret, nr_sectors = bio_sectors(bio);
567 +       dev_t old_dev;
568  
569         might_sleep();
570         /* Test device or partition size, when known. */
571 @@ -2978,6 +3007,8 @@ void generic_make_request(struct bio *bi
572          * NOTE: we don't repeat the blk_size check for each new device.
573          * Stacking drivers are expected to know what they are doing.
574          */
575 +       maxsector = -1;
576 +       old_dev = 0;
577         do {
578                 char b[BDEVNAME_SIZE];
579  
580 @@ -3010,6 +3041,15 @@ end_io:
581                  */
582                 blk_partition_remap(bio);
583  
584 +               if (maxsector != -1)
585 +                       blk_add_trace_remap(q, bio, old_dev, bio->bi_sector, 
586 +                                           maxsector);
587 +
588 +               blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
589 +
590 +               maxsector = bio->bi_sector;
591 +               old_dev = bio->bi_bdev->bd_dev;
592 +
593                 ret = q->make_request_fn(q, bio);
594         } while (ret);
595  }
596 @@ -3129,6 +3169,8 @@ static int __end_that_request_first(stru
597         int total_bytes, bio_nbytes, error, next_idx = 0;
598         struct bio *bio;
599  
600 +       blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE);
601 +
602         /*
603          * extend uptodate bool to allow < 0 value to be direct io error
604          */
605 diff --git a/drivers/block/cciss.c b/drivers/block/cciss.c
606 index 12d7b9b..880892a 100644
607 --- a/drivers/block/cciss.c
608 +++ b/drivers/block/cciss.c
609 @@ -38,6 +38,7 @@
610  #include <linux/hdreg.h>
611  #include <linux/spinlock.h>
612  #include <linux/compat.h>
613 +#include <linux/blktrace_api.h>
614  #include <asm/uaccess.h>
615  #include <asm/io.h>
616  
617 @@ -2330,6 +2331,7 @@ static inline void complete_command( ctl
618  
619         cmd->rq->completion_data = cmd;
620         cmd->rq->errors = status;
621 +       blk_add_trace_rq(cmd->rq->q, cmd->rq, BLK_TA_COMPLETE);
622         blk_complete_request(cmd->rq);
623  }
624  
625 diff --git a/drivers/md/dm.c b/drivers/md/dm.c
626 index 8c16359..8c979c2 100644
627 --- a/drivers/md/dm.c
628 +++ b/drivers/md/dm.c
629 @@ -17,6 +17,7 @@
630  #include <linux/mempool.h>
631  #include <linux/slab.h>
632  #include <linux/idr.h>
633 +#include <linux/blktrace_api.h>
634  
635  static const char *_name = DM_NAME;
636  
637 @@ -303,6 +304,8 @@ static void dec_pending(struct dm_io *io
638                         /* nudge anyone waiting on suspend queue */
639                         wake_up(&io->md->wait);
640  
641 +               blk_add_trace_bio(io->md->queue, io->bio, BLK_TA_COMPLETE);
642 +
643                 bio_endio(io->bio, io->bio->bi_size, io->error);
644                 free_io(io->md, io);
645         }
646 @@ -361,6 +364,7 @@ static void __map_bio(struct dm_target *
647                       struct target_io *tio)
648  {
649         int r;
650 +       sector_t sector;
651  
652         /*
653          * Sanity checks.
654 @@ -376,10 +380,17 @@ static void __map_bio(struct dm_target *
655          * this io.
656          */
657         atomic_inc(&tio->io->io_count);
658 +       sector = clone->bi_sector;
659         r = ti->type->map(ti, clone, &tio->info);
660 -       if (r > 0)
661 +       if (r > 0) {
662                 /* the bio has been remapped so dispatch it */
663 +
664 +               blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone, 
665 +                                   tio->io->bio->bi_bdev->bd_dev, sector, 
666 +                                   clone->bi_sector);
667 +
668                 generic_make_request(clone);
669 +       }
670  
671         else if (r < 0) {
672                 /* error the io and bail out */
673 diff --git a/fs/bio.c b/fs/bio.c
674 index bbc442b..8a1b0b6 100644
675 --- a/fs/bio.c
676 +++ b/fs/bio.c
677 @@ -25,6 +25,7 @@
678  #include <linux/module.h>
679  #include <linux/mempool.h>
680  #include <linux/workqueue.h>
681 +#include <linux/blktrace_api.h>
682  #include <scsi/sg.h>           /* for struct sg_iovec */
683  
684  #define BIO_POOL_SIZE 256
685 @@ -1094,6 +1095,9 @@ struct bio_pair *bio_split(struct bio *b
686         if (!bp)
687                 return bp;
688  
689 +       blk_add_trace_pdu_int(bdev_get_queue(bi->bi_bdev), BLK_TA_SPLIT, bi,
690 +                               bi->bi_sector + first_sectors);
691 +
692         BUG_ON(bi->bi_vcnt != 1);
693         BUG_ON(bi->bi_idx != 0);
694         atomic_set(&bp->cnt, 3);
695 diff --git a/fs/compat_ioctl.c b/fs/compat_ioctl.c
696 index 5dd0207..010e02b 100644
697 --- a/fs/compat_ioctl.c
698 +++ b/fs/compat_ioctl.c
699 @@ -72,6 +72,7 @@
700  #include <linux/i2c-dev.h>
701  #include <linux/wireless.h>
702  #include <linux/atalk.h>
703 +#include <linux/blktrace_api.h>
704  
705  #include <net/sock.h>          /* siocdevprivate_ioctl */
706  #include <net/bluetooth/bluetooth.h>
707 diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
708 index 02a585f..195c3b9 100644
709 --- a/include/linux/blkdev.h
710 +++ b/include/linux/blkdev.h
711 @@ -22,6 +22,7 @@ typedef struct request_queue request_que
712  struct elevator_queue;
713  typedef struct elevator_queue elevator_t;
714  struct request_pm_state;
715 +struct blk_trace;
716  
717  #define BLKDEV_MIN_RQ  4
718  #define BLKDEV_MAX_RQ  128     /* Default maximum */
719 @@ -416,6 +417,8 @@ struct request_queue
720         unsigned int            sg_reserved_size;
721         int                     node;
722  
723 +       struct blk_trace        *blk_trace;
724 +
725         /*
726          * reserved for flush operations
727          */
728 diff --git a/include/linux/blktrace_api.h b/include/linux/blktrace_api.h
729 new file mode 100644
730 index 0000000..d6b4317
731 --- /dev/null
732 +++ b/include/linux/blktrace_api.h
733 @@ -0,0 +1,216 @@
734 +#ifndef BLKTRACE_H
735 +#define BLKTRACE_H
736 +
737 +#include <linux/config.h>
738 +#include <linux/blkdev.h>
739 +#include <linux/relayfs_fs.h>
740 +
741 +/*
742 + * Trace categories
743 + */
744 +enum blktrace_cat {
745 +       BLK_TC_READ     = 1 << 0,       /* reads */
746 +       BLK_TC_WRITE    = 1 << 1,       /* writes */
747 +       BLK_TC_BARRIER  = 1 << 2,       /* barrier */
748 +       BLK_TC_SYNC     = 1 << 3,       /* barrier */
749 +       BLK_TC_QUEUE    = 1 << 4,       /* queueing/merging */
750 +       BLK_TC_REQUEUE  = 1 << 5,       /* requeueing */
751 +       BLK_TC_ISSUE    = 1 << 6,       /* issue */
752 +       BLK_TC_COMPLETE = 1 << 7,       /* completions */
753 +       BLK_TC_FS       = 1 << 8,       /* fs requests */
754 +       BLK_TC_PC       = 1 << 9,       /* pc requests */
755 +
756 +       BLK_TC_END      = 1 << 15,      /* only 16-bits, reminder */
757 +};
758 +
759 +#define BLK_TC_SHIFT           (16)
760 +#define BLK_TC_ACT(act)                ((act) << BLK_TC_SHIFT)
761 +
762 +/*
763 + * Basic trace actions
764 + */
765 +enum blktrace_act {
766 +       __BLK_TA_QUEUE = 1,             /* queued */
767 +       __BLK_TA_BACKMERGE,             /* back merged to existing rq */
768 +       __BLK_TA_FRONTMERGE,            /* front merge to existing rq */
769 +       __BLK_TA_GETRQ,                 /* allocated new request */
770 +       __BLK_TA_SLEEPRQ,               /* sleeping on rq allocation */
771 +       __BLK_TA_REQUEUE,               /* request requeued */
772 +       __BLK_TA_ISSUE,                 /* sent to driver */
773 +       __BLK_TA_COMPLETE,              /* completed by driver */
774 +       __BLK_TA_PLUG,                  /* queue was plugged */
775 +       __BLK_TA_UNPLUG_IO,             /* queue was unplugged by io */
776 +       __BLK_TA_UNPLUG_TIMER,          /* queue was unplugged by timer */
777 +       __BLK_TA_INSERT,                /* insert request */
778 +       __BLK_TA_SPLIT,                 /* bio was split */
779 +       __BLK_TA_BOUNCE,                /* bio was bounced */
780 +       __BLK_TA_REMAP,                 /* bio was remapped */
781 +};
782 +
783 +/*
784 + * Trace actions in full. Additionally, read or write is masked
785 + */
786 +#define BLK_TA_QUEUE           (__BLK_TA_QUEUE | BLK_TC_ACT(BLK_TC_QUEUE))
787 +#define BLK_TA_BACKMERGE       (__BLK_TA_BACKMERGE | BLK_TC_ACT(BLK_TC_QUEUE))
788 +#define BLK_TA_FRONTMERGE      (__BLK_TA_FRONTMERGE | BLK_TC_ACT(BLK_TC_QUEUE))
789 +#define        BLK_TA_GETRQ            (__BLK_TA_GETRQ | BLK_TC_ACT(BLK_TC_QUEUE))
790 +#define        BLK_TA_SLEEPRQ          (__BLK_TA_SLEEPRQ | BLK_TC_ACT(BLK_TC_QUEUE))
791 +#define        BLK_TA_REQUEUE          (__BLK_TA_REQUEUE | BLK_TC_ACT(BLK_TC_REQUEUE))
792 +#define BLK_TA_ISSUE           (__BLK_TA_ISSUE | BLK_TC_ACT(BLK_TC_ISSUE))
793 +#define BLK_TA_COMPLETE                (__BLK_TA_COMPLETE| BLK_TC_ACT(BLK_TC_COMPLETE))
794 +#define BLK_TA_PLUG            (__BLK_TA_PLUG | BLK_TC_ACT(BLK_TC_QUEUE))
795 +#define BLK_TA_UNPLUG_IO       (__BLK_TA_UNPLUG_IO | BLK_TC_ACT(BLK_TC_QUEUE))
796 +#define BLK_TA_UNPLUG_TIMER    (__BLK_TA_UNPLUG_TIMER | BLK_TC_ACT(BLK_TC_QUEUE))
797 +#define BLK_TA_INSERT          (__BLK_TA_INSERT | BLK_TC_ACT(BLK_TC_QUEUE))
798 +#define BLK_TA_SPLIT           (__BLK_TA_SPLIT)
799 +#define BLK_TA_BOUNCE          (__BLK_TA_BOUNCE)
800 +#define BLK_TA_REMAP           (__BLK_TA_REMAP | BLK_TC_ACT(BLK_TC_QUEUE))
801 +
802 +#define BLK_IO_TRACE_MAGIC     0x65617400
803 +#define BLK_IO_TRACE_VERSION   0x05
804 +
805 +/*
806 + * The trace itself
807 + */
808 +struct blk_io_trace {
809 +       u32 magic;              /* MAGIC << 8 | version */
810 +       u32 sequence;           /* event number */
811 +       u64 time;               /* in microseconds */
812 +       u64 sector;             /* disk offset */
813 +       u32 bytes;              /* transfer length */
814 +       u32 action;             /* what happened */
815 +       u32 pid;                /* who did it */
816 +       u32 cpu;                /* on what cpu did it happen */
817 +       u16 error;              /* completion error */
818 +       u16 pdu_len;            /* length of data after this trace */
819 +       u32 device;             /* device number */
820 +       char comm[16];          /* task command name (TASK_COMM_LEN) */
821 +};
822 +
823 +/*
824 + * The remap event
825 + */
826 +struct blk_io_trace_remap {
827 +       u32 device;
828 +       u32 __pad;
829 +       u64 sector;
830 +};
831 +
832 +struct blk_trace {
833 +       struct dentry *dir;
834 +       struct rchan *rchan;
835 +       struct dentry *dropped_file;
836 +       atomic_t dropped;
837 +       spinlock_t lock;
838 +       unsigned long sequence;
839 +       u32 dev;
840 +       u16 act_mask;
841 +       u64 start_lba;
842 +       u64 end_lba;
843 +       u32 pid;
844 +};
845 +
846 +/*
847 + * User setup structure passed with BLKSTARTTRACE
848 + */
849 +struct blk_user_trace_setup {
850 +       char name[BDEVNAME_SIZE];       /* output */
851 +       u16 act_mask;                   /* input */
852 +       u32 buf_size;                   /* input */
853 +       u32 buf_nr;                     /* input */
854 +       u64 start_lba;
855 +       u64 end_lba;
856 +       u32 pid;
857 +};
858 +
859 +#if defined(CONFIG_BLK_DEV_IO_TRACE)
860 +extern int blk_start_trace(struct block_device *, char __user *);
861 +extern int blk_stop_trace(struct block_device *);
862 +extern void blk_cleanup_trace(struct blk_trace *);
863 +extern void __blk_add_trace(struct blk_trace *, sector_t, int, int, u32, int, int, void *);
864 +
865 +static inline void blk_add_trace_rq(struct request_queue *q, struct request *rq,
866 +                                   u32 what)
867 +{
868 +       struct blk_trace *bt = q->blk_trace;
869 +       int rw = rq->flags & 0x07;
870 +
871 +       if (likely(!bt))
872 +               return;
873 +
874 +       if (blk_pc_request(rq)) {
875 +               what |= BLK_TC_ACT(BLK_TC_PC);
876 +               __blk_add_trace(bt, 0, rq->data_len, rw, what, rq->errors, sizeof(rq->cmd), rq->cmd);
877 +       } else  {
878 +               what |= BLK_TC_ACT(BLK_TC_FS);
879 +               __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9, rw, what, rq->errors, 0, NULL);
880 +       }
881 +}
882 +
883 +static inline void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
884 +                                    u32 what)
885 +{
886 +       struct blk_trace *bt = q->blk_trace;
887 +
888 +       if (likely(!bt))
889 +               return;
890 +
891 +       __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what, !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
892 +}
893 +
894 +static inline void blk_add_trace_generic(struct request_queue *q,
895 +                                        struct bio *bio, int rw, u32 what)
896 +{
897 +       struct blk_trace *bt = q->blk_trace;
898 +
899 +       if (likely(!bt))
900 +               return;
901 +
902 +       if (bio)
903 +               blk_add_trace_bio(q, bio, what);
904 +       else
905 +               __blk_add_trace(bt, 0, 0, rw, what, 0, 0, NULL);
906 +}
907 +
908 +static inline void blk_add_trace_pdu_int(struct request_queue *q, u32 what,
909 +                                        struct bio *bio, unsigned int pdu)
910 +{
911 +       struct blk_trace *bt = q->blk_trace;
912 +       u64 rpdu = cpu_to_be64(pdu);
913 +
914 +       if (likely(!bt))
915 +               return;
916 +
917 +       if (bio)
918 +               __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what, !bio_flagged(bio, BIO_UPTODATE), sizeof(rpdu), &rpdu);
919 +       else
920 +               __blk_add_trace(bt, 0, 0, 0, what, 0, sizeof(rpdu), &rpdu);
921 +}
922 +
923 +static inline void blk_add_trace_remap(struct request_queue *q, struct bio *bio,
924 +                                      dev_t dev, sector_t from, sector_t to)
925 +{
926 +       struct blk_trace *bt = q->blk_trace;
927 +       struct blk_io_trace_remap r;
928 +
929 +       if (likely(!bt))
930 +               return;
931 +
932 +       r.device = cpu_to_be32(dev);
933 +       r.sector = cpu_to_be64(to);
934 +
935 +       __blk_add_trace(bt, from, bio->bi_size, bio->bi_rw, BLK_TA_REMAP, !bio_flagged(bio, BIO_UPTODATE), sizeof(r), &r);
936 +}
937 +
938 +#else /* !CONFIG_BLK_DEV_IO_TRACE */
939 +#define blk_start_trace(bdev, arg)             (-EINVAL)
940 +#define blk_stop_trace(bdev)                   (-EINVAL)
941 +#define blk_cleanup_trace(bt)                  do { } while (0)
942 +#define blk_add_trace_rq(q, rq, what)          do { } while (0)
943 +#define blk_add_trace_bio(q, rq, what)         do { } while (0)
944 +#define blk_add_trace_generic(q, rq, rw, what) do { } while (0)
945 +#define blk_add_trace_pdu_int(q, what, bio, pdu)       do { } while (0)
946 +#define blk_add_trace_remap(q, bio, dev, f, t) do {} while (0)
947 +#endif /* CONFIG_BLK_DEV_IO_TRACE */
948 +
949 +#endif
950 diff --git a/include/linux/compat_ioctl.h b/include/linux/compat_ioctl.h
951 index 8fad50f..5bed09a 100644
952 --- a/include/linux/compat_ioctl.h
953 +++ b/include/linux/compat_ioctl.h
954 @@ -97,6 +97,8 @@ COMPATIBLE_IOCTL(BLKRRPART)
955  COMPATIBLE_IOCTL(BLKFLSBUF)
956  COMPATIBLE_IOCTL(BLKSECTSET)
957  COMPATIBLE_IOCTL(BLKSSZGET)
958 +COMPATIBLE_IOCTL(BLKSTARTTRACE)
959 +COMPATIBLE_IOCTL(BLKSTOPTRACE)
960  ULONG_IOCTL(BLKRASET)
961  ULONG_IOCTL(BLKFRASET)
962  /* RAID */
963 diff --git a/include/linux/fs.h b/include/linux/fs.h
964 index b77f260..5575284 100644
965 --- a/include/linux/fs.h
966 +++ b/include/linux/fs.h
967 @@ -196,6 +196,8 @@ extern int dir_notify_enable;
968  #define BLKBSZGET  _IOR(0x12,112,size_t)
969  #define BLKBSZSET  _IOW(0x12,113,size_t)
970  #define BLKGETSIZE64 _IOR(0x12,114,size_t)     /* return device size in bytes (u64 *arg) */
971 +#define BLKSTARTTRACE _IOWR(0x12,115,struct blk_user_trace_setup)
972 +#define BLKSTOPTRACE _IO(0x12,116)
973  
974  #define BMAP_IOCTL 1           /* obsolete - kept for compatibility */
975  #define FIBMAP    _IO(0x00,1)  /* bmap access */
976 diff --git a/mm/highmem.c b/mm/highmem.c
977 index ce2e7e8..d0ea1ee 100644
978 --- a/mm/highmem.c
979 +++ b/mm/highmem.c
980 @@ -26,6 +26,7 @@
981  #include <linux/init.h>
982  #include <linux/hash.h>
983  #include <linux/highmem.h>
984 +#include <linux/blktrace_api.h>
985  #include <asm/tlbflush.h>
986  
987  static mempool_t *page_pool, *isa_page_pool;
988 @@ -483,6 +484,8 @@ void blk_queue_bounce(request_queue_t *q
989                 pool = isa_page_pool;
990         }
991  
992 +       blk_add_trace_bio(q, *bio_orig, BLK_TA_BOUNCE);
993 +
994         /*
995          * slow path
996          */