[PATCH] kernel: previous patch was reversed by mistake
[blktrace.git] / kernel / blk-trace-2.6.16-rc1-git-U2
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..6bfcdb8
34 --- /dev/null
35 +++ b/block/blktrace.c
36 @@ -0,0 +1,382 @@
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 +/*
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.
51 + */
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)
54 +{
55 +       struct blk_io_trace *t;
56 +       unsigned long flags;
57 +       unsigned long *sequence;
58 +       pid_t pid;
59 +       int cpu;
60 +
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);
65 +
66 +       if (rw & WRITE)
67 +               what |= BLK_TC_ACT(BLK_TC_WRITE);
68 +       else
69 +               what |= BLK_TC_ACT(BLK_TC_READ);
70 +
71 +       if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
72 +               return;
73 +       if (sector < bt->start_lba || sector > bt->end_lba)
74 +               return;
75 +
76 +       pid = current->pid;
77 +       if (bt->pid && pid != bt->pid)
78 +               return;
79 +
80 +       /*
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
86 +        * for us
87 +        */
88 +       local_irq_save(flags);
89 +
90 +       t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
91 +       if (unlikely(!t)) {
92 +               local_irq_restore(flags);
93 +               return;
94 +       }
95 +
96 +       cpu = get_cpu();
97 +
98 +       sequence = per_cpu_ptr(bt->sequence, cpu);
99 +       t->sequence = ++(*sequence);
100 +       t->time = sched_clock() - per_cpu(blk_trace_cpu_offset, cpu);
101 +       t->cpu = cpu;
102 +
103 +       local_irq_restore(flags);
104 +
105 +       t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
106 +       t->device = bt->dev;
107 +       t->sector = sector;
108 +       t->bytes = bytes;
109 +       t->action = what;
110 +       t->error = error;
111 +       t->pdu_len = pdu_len;
112 +
113 +       t->pid = pid;
114 +       strncpy(t->comm, current->comm, sizeof(t->comm));
115 +
116 +       if (pdu_len)
117 +               memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
118 +
119 +       put_cpu();
120 +}
121 +
122 +EXPORT_SYMBOL_GPL(__blk_add_trace);
123 +
124 +static struct dentry *blk_tree_root;
125 +static struct mutex blk_tree_mutex;
126 +
127 +static inline void blk_remove_root(void)
128 +{
129 +       if (relayfs_remove_dir(blk_tree_root) != -ENOTEMPTY)
130 +               blk_tree_root = NULL;
131 +}
132 +
133 +static void blk_remove_tree(struct dentry *dir)
134 +{
135 +       mutex_lock(&blk_tree_mutex);
136 +       relayfs_remove_dir(dir);
137 +       blk_remove_root();
138 +       mutex_unlock(&blk_tree_mutex);
139 +}
140 +
141 +static struct dentry *blk_create_tree(const char *blk_name)
142 +{
143 +       struct dentry *dir = NULL;
144 +
145 +       mutex_lock(&blk_tree_mutex);
146 +
147 +       if (!blk_tree_root) {
148 +               blk_tree_root = relayfs_create_dir("block", NULL);
149 +               if (!blk_tree_root)
150 +                       goto err;
151 +       }
152 +
153 +       dir = relayfs_create_dir(blk_name, blk_tree_root);
154 +       if (!dir)
155 +               blk_remove_root();
156 +
157 +err:
158 +       mutex_unlock(&blk_tree_mutex);
159 +       return dir;
160 +}
161 +
162 +void blk_cleanup_trace(struct blk_trace *bt)
163 +{
164 +       relay_close(bt->rchan);
165 +       relayfs_remove_file(bt->dropped_file);
166 +       blk_remove_tree(bt->dir);
167 +       kfree(bt);
168 +}
169 +
170 +int blk_stop_trace(struct block_device *bdev)
171 +{
172 +       request_queue_t *q = bdev_get_queue(bdev);
173 +       struct blk_trace *bt = NULL;
174 +       int ret = -EINVAL;
175 +
176 +       if (!q)
177 +               return -ENXIO;
178 +
179 +       down(&bdev->bd_sem);
180 +
181 +       if (q->blk_trace) {
182 +               bt = q->blk_trace;
183 +               q->blk_trace = NULL;
184 +               ret = 0;
185 +       }
186 +
187 +       up(&bdev->bd_sem);
188 +
189 +       if (bt)
190 +               blk_cleanup_trace(bt);
191 +
192 +       return ret;
193 +}
194 +
195 +static int blk_dropped_open(struct inode *inode, struct file *filp)
196 +{
197 +       filp->private_data = inode->u.generic_ip;
198 +       
199 +       return 0;
200 +}
201 +
202 +static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
203 +                               size_t count, loff_t *ppos)
204 +{
205 +       struct blk_trace *bt = filp->private_data;
206 +       char buf[16];
207 +
208 +       snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
209 +       
210 +       return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
211 +}
212 +
213 +static struct file_operations blk_dropped_fops = {
214 +       .owner =        THIS_MODULE,
215 +       .open =         blk_dropped_open,
216 +       .read =         blk_dropped_read,
217 +};
218 +
219 +/*
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.
222 + */
223 +static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
224 +                                    void *prev_subbuf, size_t prev_padding)
225 +{
226 +       struct blk_trace *bt;
227 +
228 +       if (!relay_buf_full(buf))
229 +               return 1;
230 +
231 +       bt = buf->chan->private_data;
232 +       atomic_inc(&bt->dropped);
233 +       return 0;
234 +}
235 +
236 +static struct rchan_callbacks blk_relay_callbacks = {
237 +       .subbuf_start = blk_subbuf_start_callback,
238 +};
239 +
240 +/*
241 + * Setup everything required to start tracing
242 + */
243 +int blk_start_trace(struct block_device *bdev, char __user *arg)
244 +{
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];
250 +       int ret, i;
251 +
252 +       if (!q)
253 +               return -ENXIO;
254 +
255 +       if (copy_from_user(&buts, arg, sizeof(buts)))
256 +               return -EFAULT;
257 +
258 +       if (!buts.buf_size || !buts.buf_nr)
259 +               return -EINVAL;
260 +
261 +       strcpy(buts.name, bdevname(bdev, b));
262 +
263 +       /*
264 +        * some device names have larger paths - convert the slashes
265 +        * to underscores for this to work as expected
266 +        */
267 +       for (i = 0; i < strlen(buts.name); i++)
268 +               if (buts.name[i] == '/')
269 +                       buts.name[i] = '_';
270 +
271 +       if (copy_to_user(arg, &buts, sizeof(buts)))
272 +               return -EFAULT;
273 +
274 +       down(&bdev->bd_sem);
275 +       ret = -EBUSY;
276 +       if (q->blk_trace)
277 +               goto err;
278 +
279 +       ret = -ENOMEM;
280 +       bt = kzalloc(sizeof(*bt), GFP_KERNEL);
281 +       if (!bt)
282 +               goto err;
283 +
284 +       bt->sequence = alloc_percpu(unsigned long);
285 +       if (!bt->sequence)
286 +               goto err;
287 +
288 +       ret = -ENOENT;
289 +       dir = blk_create_tree(buts.name);
290 +       if (!dir)
291 +               goto err;
292 +
293 +       bt->dir = dir;
294 +       bt->dev = bdev->bd_dev;
295 +       atomic_set(&bt->dropped, 0);
296 +
297 +       ret = -EIO;
298 +       bt->dropped_file = relayfs_create_file("dropped", dir, 0, &blk_dropped_fops, bt);
299 +       if (!bt->dropped_file)
300 +               goto err;
301 +
302 +       bt->rchan = relay_open("trace", dir, buts.buf_size, buts.buf_nr, &blk_relay_callbacks);
303 +       if (!bt->rchan)
304 +               goto err;
305 +       bt->rchan->private_data = bt;
306 +
307 +       bt->act_mask = buts.act_mask;
308 +       if (!bt->act_mask)
309 +               bt->act_mask = (u16) -1;
310 +
311 +       bt->start_lba = buts.start_lba;
312 +       bt->end_lba = buts.end_lba;
313 +       if (!bt->end_lba)
314 +               bt->end_lba = -1ULL;
315 +
316 +       bt->pid = buts.pid;
317 +
318 +       q->blk_trace = bt;
319 +       up(&bdev->bd_sem);
320 +       return 0;
321 +err:
322 +       up(&bdev->bd_sem);
323 +       if (bt && bt->dropped_file)
324 +               relayfs_remove_file(bt->dropped_file);
325 +       if (dir)
326 +               blk_remove_tree(dir);
327 +       if (bt) {
328 +               if (bt->sequence)
329 +                       free_percpu(bt->sequence);
330 +               kfree(bt);
331 +       }
332 +       return ret;
333 +}
334 +
335 +/*
336 + * Average offset over two calls to sched_clock() with a gettimeofday()
337 + * in the middle
338 + */
339 +static void blk_check_time(unsigned long long *t)
340 +{
341 +       unsigned long long a, b;
342 +       struct timeval tv;
343 +
344 +       a = sched_clock();
345 +       do_gettimeofday(&tv);
346 +       b = sched_clock();
347 +
348 +       *t = tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
349 +       *t -= (a + b) / 2;
350 +}
351 +
352 +static void blk_trace_check_cpu_time(void *data)
353 +{
354 +       unsigned long long *t;
355 +       int cpu = get_cpu();
356 +
357 +       t = &per_cpu(blk_trace_cpu_offset, cpu);
358 +
359 +       /*
360 +        * Just call it twice, hopefully the second call will be cache hot
361 +        * and a little more precise
362 +        */
363 +       blk_check_time(t);
364 +       blk_check_time(t);
365 +
366 +       put_cpu();
367 +}
368 +
369 +/*
370 + * Call blk_trace_check_cpu_time() on each CPU to calibrate our inter-CPU
371 + * timings
372 + */
373 +static void blk_trace_calibrate_offsets(void)
374 +{
375 +       unsigned long flags;
376 +
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);
381 +}
382 +
383 +static void blk_trace_set_ht_offsets(void)
384 +{
385 +#if defined(CONFIG_SCHED_SMT)
386 +       int cpu, i;
387 +
388 +       /*
389 +        * now make sure HT siblings have the same time offset
390 +        */
391 +       preempt_disable();
392 +       for_each_online_cpu(cpu) {
393 +               unsigned long long *cpu_off, *sibling_off;
394 +
395 +               for_each_cpu_mask(i, cpu_sibling_map[cpu]) {
396 +                       if (i == cpu)
397 +                               continue;
398 +
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;
402 +               }
403 +       }
404 +       preempt_enable();
405 +#endif
406 +}
407 +
408 +static __init int blk_trace_init(void)
409 +{
410 +       mutex_init(&blk_tree_mutex);
411 +       blk_trace_calibrate_offsets();
412 +       blk_trace_set_ht_offsets();
413 +
414 +       return 0;
415 +}
416 +
417 +module_init(blk_trace_init);
418 +
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
423 @@ -33,6 +33,7 @@
424  #include <linux/init.h>
425  #include <linux/compiler.h>
426  #include <linux/delay.h>
427 +#include <linux/blktrace_api.h>
428  
429  #include <asm/uaccess.h>
430  
431 @@ -316,6 +317,8 @@ void __elv_add_request(request_queue_t *
432         struct list_head *pos;
433         unsigned ordseq;
434  
435 +       blk_add_trace_rq(q, rq, BLK_TA_INSERT);
436 +
437         if (q->ordcolor)
438                 rq->flags |= REQ_ORDERED_COLOR;
439  
440 @@ -474,6 +477,7 @@ struct request *elv_next_request(request
441                          * not be passed by new incoming requests
442                          */
443                         rq->flags |= REQ_STARTED;
444 +                       blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
445                 }
446  
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
450 --- a/block/ioctl.c
451 +++ b/block/ioctl.c
452 @@ -5,6 +5,7 @@
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>
458  
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);
462         case BLKGETSIZE64:
463                 return put_u64(arg, bdev->bd_inode->i_size);
464 +       case BLKSTARTTRACE:
465 +               return blk_start_trace(bdev, (char __user *) arg);
466 +       case BLKSTOPTRACE:
467 +               return blk_stop_trace(bdev);
468         }
469         return -ENOIOCTLCMD;
470  }
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
475 @@ -28,6 +28,7 @@
476  #include <linux/writeback.h>
477  #include <linux/interrupt.h>
478  #include <linux/cpu.h>
479 +#include <linux/blktrace_api.h>
480  
481  /*
482   * for max sense size
483 @@ -1557,8 +1558,10 @@ void blk_plug_device(request_queue_t *q)
484         if (test_bit(QUEUE_FLAG_STOPPED, &q->queue_flags))
485                 return;
486  
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);
491 +       }
492  }
493  
494  EXPORT_SYMBOL(blk_plug_device);
495 @@ -1622,14 +1625,21 @@ static void blk_backing_dev_unplug(struc
496         /*
497          * devices don't necessarily have an ->unplug_fn defined
498          */
499 -       if (q->unplug_fn)
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]);
503 +
504                 q->unplug_fn(q);
505 +       }
506  }
507  
508  static void blk_unplug_work(void *data)
509  {
510         request_queue_t *q = data;
511  
512 +       blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
513 +                               q->rq.count[READ] + q->rq.count[WRITE]);
514 +
515         q->unplug_fn(q);
516  }
517  
518 @@ -1637,6 +1647,9 @@ static void blk_unplug_timeout(unsigned 
519  {
520         request_queue_t *q = (request_queue_t *)data;
521  
522 +       blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL,
523 +                               q->rq.count[READ] + q->rq.count[WRITE]);
524 +
525         kblockd_schedule_work(&q->unplug_work);
526  }
527  
528 @@ -1759,6 +1772,11 @@ void blk_cleanup_queue(request_queue_t *
529         if (q->queue_tags)
530                 __blk_queue_free_tags(q);
531  
532 +       if (q->blk_trace) {
533 +               blk_cleanup_trace(q->blk_trace);
534 +               q->blk_trace = NULL;
535 +       }
536 +
537         kmem_cache_free(requestq_cachep, q);
538  }
539  
540 @@ -2110,6 +2128,8 @@ rq_starved:
541         
542         rq_init(q, rq);
543         rq->rl = rl;
544 +
545 +       blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ);
546  out:
547         return rq;
548  }
549 @@ -2138,6 +2158,8 @@ static struct request *get_request_wait(
550                 if (!rq) {
551                         struct io_context *ioc;
552  
553 +                       blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
554 +
555                         __generic_unplug_device(q);
556                         spin_unlock_irq(q->queue_lock);
557                         io_schedule();
558 @@ -2191,6 +2213,8 @@ EXPORT_SYMBOL(blk_get_request);
559   */
560  void blk_requeue_request(request_queue_t *q, struct request *rq)
561  {
562 +       blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
563 +
564         if (blk_rq_tagged(rq))
565                 blk_queue_end_tag(q, rq);
566  
567 @@ -2825,6 +2849,8 @@ static int __make_request(request_queue_
568                         if (!q->back_merge_fn(q, req, bio))
569                                 break;
570  
571 +                       blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
572 +
573                         req->biotail->bi_next = bio;
574                         req->biotail = 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))
578                                 break;
579  
580 +                       blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
581 +
582                         bio->bi_next = req->bio;
583                         req->bio = bio;
584  
585 @@ -2957,6 +2985,7 @@ void generic_make_request(struct bio *bi
586         request_queue_t *q;
587         sector_t maxsector;
588         int ret, nr_sectors = bio_sectors(bio);
589 +       dev_t old_dev;
590  
591         might_sleep();
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.
596          */
597 +       maxsector = -1;
598 +       old_dev = 0;
599         do {
600                 char b[BDEVNAME_SIZE];
601  
602 @@ -3015,6 +3046,15 @@ end_io:
603                  */
604                 blk_partition_remap(bio);
605  
606 +               if (maxsector != -1)
607 +                       blk_add_trace_remap(q, bio, old_dev, bio->bi_sector, 
608 +                                           maxsector);
609 +
610 +               blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
611 +
612 +               maxsector = bio->bi_sector;
613 +               old_dev = bio->bi_bdev->bd_dev;
614 +
615                 ret = q->make_request_fn(q, bio);
616         } while (ret);
617  }
618 @@ -3134,6 +3174,8 @@ static int __end_that_request_first(stru
619         int total_bytes, bio_nbytes, error, next_idx = 0;
620         struct bio *bio;
621  
622 +       blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE);
623 +
624         /*
625          * extend uptodate bool to allow < 0 value to be direct io error
626          */
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
631 @@ -38,6 +38,7 @@
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>
637  #include <asm/io.h>
638  
639 @@ -2330,6 +2331,7 @@ static inline void complete_command( ctl
640  
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);
645  }
646  
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
651 @@ -17,6 +17,7 @@
652  #include <linux/mempool.h>
653  #include <linux/slab.h>
654  #include <linux/idr.h>
655 +#include <linux/blktrace_api.h>
656  
657  static const char *_name = DM_NAME;
658  
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);
662  
663 +               blk_add_trace_bio(io->md->queue, io->bio, BLK_TA_COMPLETE);
664 +
665                 bio_endio(io->bio, io->bio->bi_size, io->error);
666                 free_io(io->md, io);
667         }
668 @@ -392,6 +395,7 @@ static void __map_bio(struct dm_target *
669                       struct target_io *tio)
670  {
671         int r;
672 +       sector_t sector;
673  
674         /*
675          * Sanity checks.
676 @@ -407,10 +411,17 @@ static void __map_bio(struct dm_target *
677          * this io.
678          */
679         atomic_inc(&tio->io->io_count);
680 +       sector = clone->bi_sector;
681         r = ti->type->map(ti, clone, &tio->info);
682 -       if (r > 0)
683 +       if (r > 0) {
684                 /* the bio has been remapped so dispatch it */
685 +
686 +               blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone, 
687 +                                   tio->io->bio->bi_bdev->bd_dev, sector, 
688 +                                   clone->bi_sector);
689 +
690                 generic_make_request(clone);
691 +       }
692  
693         else if (r < 0) {
694                 /* error the io and bail out */
695 diff --git a/fs/bio.c b/fs/bio.c
696 index 1f3bb50..0dd0d81 100644
697 --- a/fs/bio.c
698 +++ b/fs/bio.c
699 @@ -25,6 +25,7 @@
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 */
705  
706  #define BIO_POOL_SIZE 256
707 @@ -1095,6 +1096,9 @@ struct bio_pair *bio_split(struct bio *b
708         if (!bp)
709                 return bp;
710  
711 +       blk_add_trace_pdu_int(bdev_get_queue(bi->bi_bdev), BLK_TA_SPLIT, bi,
712 +                               bi->bi_sector + first_sectors);
713 +
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
721 @@ -72,6 +72,7 @@
722  #include <linux/i2c-dev.h>
723  #include <linux/wireless.h>
724  #include <linux/atalk.h>
725 +#include <linux/blktrace_api.h>
726  
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;
737 +struct blk_trace;
738  
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;
743         int                     node;
744  
745 +       struct blk_trace        *blk_trace;
746 +
747         /*
748          * reserved for flush operations
749          */
750 diff --git a/include/linux/blktrace_api.h b/include/linux/blktrace_api.h
751 new file mode 100644
752 index 0000000..b0fc5cf
753 --- /dev/null
754 +++ b/include/linux/blktrace_api.h
755 @@ -0,0 +1,272 @@
756 +#ifndef BLKTRACE_H
757 +#define BLKTRACE_H
758 +
759 +#include <linux/config.h>
760 +#include <linux/blkdev.h>
761 +#include <linux/relayfs_fs.h>
762 +
763 +/*
764 + * Trace categories
765 + */
766 +enum blktrace_cat {
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 */
777 +
778 +       BLK_TC_END      = 1 << 15,      /* only 16-bits, reminder */
779 +};
780 +
781 +#define BLK_TC_SHIFT           (16)
782 +#define BLK_TC_ACT(act)                ((act) << BLK_TC_SHIFT)
783 +
784 +/*
785 + * Basic trace actions
786 + */
787 +enum blktrace_act {
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 */
803 +};
804 +
805 +/*
806 + * Trace actions in full. Additionally, read or write is masked
807 + */
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))
823 +
824 +#define BLK_IO_TRACE_MAGIC     0x65617400
825 +#define BLK_IO_TRACE_VERSION   0x06
826 +
827 +/*
828 + * The trace itself
829 + */
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) */
843 +};
844 +
845 +/*
846 + * The remap event
847 + */
848 +struct blk_io_trace_remap {
849 +       u32 device;
850 +       u32 __pad;
851 +       u64 sector;
852 +};
853 +
854 +struct blk_trace {
855 +       struct dentry *dir;
856 +       struct rchan *rchan;
857 +       struct dentry *dropped_file;
858 +       atomic_t dropped;
859 +       unsigned long *sequence;
860 +       u32 dev;
861 +       u16 act_mask;
862 +       u64 start_lba;
863 +       u64 end_lba;
864 +       u32 pid;
865 +};
866 +
867 +/*
868 + * User setup structure passed with BLKSTARTTRACE
869 + */
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 */
875 +       u64 start_lba;
876 +       u64 end_lba;
877 +       u32 pid;
878 +};
879 +
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 *);
885 +
886 +/**
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
891 + *
892 + * Description:
893 + *     Records an action against a request. Will log the bio offset + size.
894 + *
895 + **/
896 +static inline void blk_add_trace_rq(struct request_queue *q, struct request *rq,
897 +                                   u32 what)
898 +{
899 +       struct blk_trace *bt = q->blk_trace;
900 +       int rw = rq->flags & 0x07;
901 +
902 +       if (likely(!bt))
903 +               return;
904 +
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);
908 +       } else  {
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);
911 +       }
912 +}
913 +
914 +/**
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
919 + *
920 + * Description:
921 + *     Records an action against a bio. Will log the bio offset + size.
922 + *
923 + **/
924 +static inline void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
925 +                                    u32 what)
926 +{
927 +       struct blk_trace *bt = q->blk_trace;
928 +
929 +       if (likely(!bt))
930 +               return;
931 +
932 +       __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what, !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
933 +}
934 +
935 +/**
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
941 + *
942 + * Description:
943 + *     Records a simple trace
944 + *
945 + **/
946 +static inline void blk_add_trace_generic(struct request_queue *q,
947 +                                        struct bio *bio, int rw, u32 what)
948 +{
949 +       struct blk_trace *bt = q->blk_trace;
950 +
951 +       if (likely(!bt))
952 +               return;
953 +
954 +       if (bio)
955 +               blk_add_trace_bio(q, bio, what);
956 +       else
957 +               __blk_add_trace(bt, 0, 0, rw, what, 0, 0, NULL);
958 +}
959 +
960 +/**
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
966 + *
967 + * Description:
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
970 + *     as the payload
971 + *
972 + **/
973 +static inline void blk_add_trace_pdu_int(struct request_queue *q, u32 what,
974 +                                        struct bio *bio, unsigned int pdu)
975 +{
976 +       struct blk_trace *bt = q->blk_trace;
977 +       u64 rpdu = cpu_to_be64(pdu);
978 +
979 +       if (likely(!bt))
980 +               return;
981 +
982 +       if (bio)
983 +               __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what, !bio_flagged(bio, BIO_UPTODATE), sizeof(rpdu), &rpdu);
984 +       else
985 +               __blk_add_trace(bt, 0, 0, 0, what, 0, sizeof(rpdu), &rpdu);
986 +}
987 +
988 +/**
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
995 + *
996 + * Description:
997 + *     Device mapper or raid target sometimes need to split a bio because
998 + *     it spans a stripe (or similar). Add a trace for that action.
999 + *
1000 + **/
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)
1003 +{
1004 +       struct blk_trace *bt = q->blk_trace;
1005 +       struct blk_io_trace_remap r;
1006 +
1007 +       if (likely(!bt))
1008 +               return;
1009 +
1010 +       r.device = cpu_to_be32(dev);
1011 +       r.sector = cpu_to_be64(to);
1012 +
1013 +       __blk_add_trace(bt, from, bio->bi_size, bio->bi_rw, BLK_TA_REMAP, !bio_flagged(bio, BIO_UPTODATE), sizeof(r), &r);
1014 +}
1015 +
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 */
1026 +
1027 +#endif
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)
1040  /* RAID */
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)
1051  
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
1056 --- a/mm/highmem.c
1057 +++ b/mm/highmem.c
1058 @@ -26,6 +26,7 @@
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>
1064  
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;
1068         }
1069  
1070 +       blk_add_trace_bio(q, *bio_orig, BLK_TA_BOUNCE);
1071 +
1072         /*
1073          * slow path
1074          */