blob: 20e11f354f117d83e4c83356a99b09c70a87939e [file] [log] [blame]
Jens Axboe2056a782006-03-23 20:00:26 +01001/*
Jens Axboe0fe23472006-09-04 15:41:16 +02002 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
Jens Axboe2056a782006-03-23 20:00:26 +01003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 *
17 */
Jens Axboe2056a782006-03-23 20:00:26 +010018#include <linux/kernel.h>
19#include <linux/blkdev.h>
20#include <linux/blktrace_api.h>
21#include <linux/percpu.h>
22#include <linux/init.h>
23#include <linux/mutex.h>
24#include <linux/debugfs.h>
Olaf Kirchbe1c6342006-12-01 10:39:12 +010025#include <linux/time.h>
Jens Axboe2056a782006-03-23 20:00:26 +010026#include <asm/uaccess.h>
27
Jens Axboe2056a782006-03-23 20:00:26 +010028static unsigned int blktrace_seq __read_mostly = 1;
29
30/*
Olaf Kirchbe1c6342006-12-01 10:39:12 +010031 * Send out a notify message.
32 */
Jens Axboea8630552006-12-04 09:30:58 +010033static void trace_note(struct blk_trace *bt, pid_t pid, int action,
34 const void *data, size_t len)
Olaf Kirchbe1c6342006-12-01 10:39:12 +010035{
36 struct blk_io_trace *t;
Olaf Kirchbe1c6342006-12-01 10:39:12 +010037
38 t = relay_reserve(bt->rchan, sizeof(*t) + len);
Jens Axboed3d9d2a2006-12-04 09:27:41 +010039 if (t) {
40 const int cpu = smp_processor_id();
Olaf Kirchbe1c6342006-12-01 10:39:12 +010041
Jens Axboed3d9d2a2006-12-04 09:27:41 +010042 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
Ingo Molnar2997c8c2008-01-11 13:35:54 +010043 t->time = ktime_to_ns(ktime_get());
Jens Axboed3d9d2a2006-12-04 09:27:41 +010044 t->device = bt->dev;
45 t->action = action;
46 t->pid = pid;
47 t->cpu = cpu;
48 t->pdu_len = len;
49 memcpy((void *) t + sizeof(*t), data, len);
50 }
Olaf Kirchbe1c6342006-12-01 10:39:12 +010051}
52
53/*
Jens Axboe2056a782006-03-23 20:00:26 +010054 * Send out a notify for this process, if we haven't done so since a trace
55 * started
56 */
57static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk)
58{
Jens Axboea8630552006-12-04 09:30:58 +010059 tsk->btrace_seq = blktrace_seq;
60 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm));
Olaf Kirchbe1c6342006-12-01 10:39:12 +010061}
Jens Axboe2056a782006-03-23 20:00:26 +010062
Olaf Kirchbe1c6342006-12-01 10:39:12 +010063static void trace_note_time(struct blk_trace *bt)
64{
65 struct timespec now;
66 unsigned long flags;
67 u32 words[2];
68
69 getnstimeofday(&now);
70 words[0] = now.tv_sec;
71 words[1] = now.tv_nsec;
72
73 local_irq_save(flags);
74 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
75 local_irq_restore(flags);
Jens Axboe2056a782006-03-23 20:00:26 +010076}
77
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +020078void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
79{
80 int n;
81 va_list args;
82 static char bt_msg_buf[BLK_TN_MAX_MSG];
83
84 va_start(args, fmt);
85 n = vscnprintf(bt_msg_buf, BLK_TN_MAX_MSG, fmt, args);
86 va_end(args);
87
88 trace_note(bt, 0, BLK_TN_MESSAGE, bt_msg_buf, n);
89}
90EXPORT_SYMBOL_GPL(__trace_note_message);
91
Jens Axboe2056a782006-03-23 20:00:26 +010092static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
93 pid_t pid)
94{
95 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
96 return 1;
97 if (sector < bt->start_lba || sector > bt->end_lba)
98 return 1;
99 if (bt->pid && pid != bt->pid)
100 return 1;
101
102 return 0;
103}
104
105/*
106 * Data direction bit lookup
107 */
108static u32 ddir_act[2] __read_mostly = { BLK_TC_ACT(BLK_TC_READ), BLK_TC_ACT(BLK_TC_WRITE) };
109
110/*
111 * Bio action bits of interest
112 */
Jens Axboe7457e6e2006-07-23 02:12:01 +0200113static u32 bio_act[9] __read_mostly = { 0, BLK_TC_ACT(BLK_TC_BARRIER), BLK_TC_ACT(BLK_TC_SYNC), 0, BLK_TC_ACT(BLK_TC_AHEAD), 0, 0, 0, BLK_TC_ACT(BLK_TC_META) };
Jens Axboe2056a782006-03-23 20:00:26 +0100114
115/*
116 * More could be added as needed, taking care to increment the decrementer
117 * to get correct indexing
118 */
119#define trace_barrier_bit(rw) \
120 (((rw) & (1 << BIO_RW_BARRIER)) >> (BIO_RW_BARRIER - 0))
121#define trace_sync_bit(rw) \
122 (((rw) & (1 << BIO_RW_SYNC)) >> (BIO_RW_SYNC - 1))
Nathan Scott40359cc2006-07-06 10:03:28 +0200123#define trace_ahead_bit(rw) \
Milton Millerad01b1c2006-07-25 15:04:13 +0200124 (((rw) & (1 << BIO_RW_AHEAD)) << (2 - BIO_RW_AHEAD))
Jens Axboe7457e6e2006-07-23 02:12:01 +0200125#define trace_meta_bit(rw) \
126 (((rw) & (1 << BIO_RW_META)) >> (BIO_RW_META - 3))
Jens Axboe2056a782006-03-23 20:00:26 +0100127
128/*
129 * The worker for the various blk_add_trace*() types. Fills out a
130 * blk_io_trace structure and places it in a per-cpu subbuffer.
131 */
132void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
133 int rw, u32 what, int error, int pdu_len, void *pdu_data)
134{
135 struct task_struct *tsk = current;
136 struct blk_io_trace *t;
137 unsigned long flags;
138 unsigned long *sequence;
139 pid_t pid;
140 int cpu;
141
142 if (unlikely(bt->trace_state != Blktrace_running))
143 return;
144
145 what |= ddir_act[rw & WRITE];
146 what |= bio_act[trace_barrier_bit(rw)];
147 what |= bio_act[trace_sync_bit(rw)];
Nathan Scott40359cc2006-07-06 10:03:28 +0200148 what |= bio_act[trace_ahead_bit(rw)];
Jens Axboe7457e6e2006-07-23 02:12:01 +0200149 what |= bio_act[trace_meta_bit(rw)];
Jens Axboe2056a782006-03-23 20:00:26 +0100150
151 pid = tsk->pid;
152 if (unlikely(act_log_check(bt, what, sector, pid)))
153 return;
154
155 /*
156 * A word about the locking here - we disable interrupts to reserve
157 * some space in the relay per-cpu buffer, to prevent an irq
158 * from coming in and stepping on our toes. Once reserved, it's
159 * enough to get preemption disabled to prevent read of this data
160 * before we are through filling it. get_cpu()/put_cpu() does this
161 * for us
162 */
163 local_irq_save(flags);
164
165 if (unlikely(tsk->btrace_seq != blktrace_seq))
166 trace_note_tsk(bt, tsk);
167
168 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
169 if (t) {
170 cpu = smp_processor_id();
171 sequence = per_cpu_ptr(bt->sequence, cpu);
172
173 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
174 t->sequence = ++(*sequence);
Ingo Molnar2997c8c2008-01-11 13:35:54 +0100175 t->time = ktime_to_ns(ktime_get());
Jens Axboe2056a782006-03-23 20:00:26 +0100176 t->sector = sector;
177 t->bytes = bytes;
178 t->action = what;
179 t->pid = pid;
180 t->device = bt->dev;
181 t->cpu = cpu;
182 t->error = error;
183 t->pdu_len = pdu_len;
184
185 if (pdu_len)
186 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
187 }
188
189 local_irq_restore(flags);
190}
191
192EXPORT_SYMBOL_GPL(__blk_add_trace);
193
194static struct dentry *blk_tree_root;
Jens Axboe11a57152008-01-11 13:37:01 +0100195static DEFINE_MUTEX(blk_tree_mutex);
Jens Axboe2056a782006-03-23 20:00:26 +0100196static unsigned int root_users;
197
198static inline void blk_remove_root(void)
199{
200 if (blk_tree_root) {
201 debugfs_remove(blk_tree_root);
202 blk_tree_root = NULL;
203 }
204}
205
206static void blk_remove_tree(struct dentry *dir)
207{
208 mutex_lock(&blk_tree_mutex);
209 debugfs_remove(dir);
210 if (--root_users == 0)
211 blk_remove_root();
212 mutex_unlock(&blk_tree_mutex);
213}
214
215static struct dentry *blk_create_tree(const char *blk_name)
216{
217 struct dentry *dir = NULL;
Aneesh Kumar K.V35fc51e2007-11-21 12:25:41 +0100218 int created = 0;
Jens Axboe2056a782006-03-23 20:00:26 +0100219
220 mutex_lock(&blk_tree_mutex);
221
222 if (!blk_tree_root) {
223 blk_tree_root = debugfs_create_dir("block", NULL);
224 if (!blk_tree_root)
225 goto err;
Aneesh Kumar K.V35fc51e2007-11-21 12:25:41 +0100226 created = 1;
Jens Axboe2056a782006-03-23 20:00:26 +0100227 }
228
229 dir = debugfs_create_dir(blk_name, blk_tree_root);
230 if (dir)
231 root_users++;
Aneesh Kumar K.V35fc51e2007-11-21 12:25:41 +0100232 else {
233 /* Delete root only if we created it */
234 if (created)
235 blk_remove_root();
236 }
Jens Axboe2056a782006-03-23 20:00:26 +0100237
238err:
239 mutex_unlock(&blk_tree_mutex);
240 return dir;
241}
242
243static void blk_trace_cleanup(struct blk_trace *bt)
244{
245 relay_close(bt->rchan);
246 debugfs_remove(bt->dropped_file);
247 blk_remove_tree(bt->dir);
248 free_percpu(bt->sequence);
249 kfree(bt);
250}
251
Christof Schmitt6da127a2008-01-11 10:09:43 +0100252int blk_trace_remove(struct request_queue *q)
Jens Axboe2056a782006-03-23 20:00:26 +0100253{
254 struct blk_trace *bt;
255
256 bt = xchg(&q->blk_trace, NULL);
257 if (!bt)
258 return -EINVAL;
259
260 if (bt->trace_state == Blktrace_setup ||
261 bt->trace_state == Blktrace_stopped)
262 blk_trace_cleanup(bt);
263
264 return 0;
265}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100266EXPORT_SYMBOL_GPL(blk_trace_remove);
Jens Axboe2056a782006-03-23 20:00:26 +0100267
268static int blk_dropped_open(struct inode *inode, struct file *filp)
269{
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700270 filp->private_data = inode->i_private;
Jens Axboe2056a782006-03-23 20:00:26 +0100271
272 return 0;
273}
274
275static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
276 size_t count, loff_t *ppos)
277{
278 struct blk_trace *bt = filp->private_data;
279 char buf[16];
280
281 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
282
283 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
284}
285
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800286static const struct file_operations blk_dropped_fops = {
Jens Axboe2056a782006-03-23 20:00:26 +0100287 .owner = THIS_MODULE,
288 .open = blk_dropped_open,
289 .read = blk_dropped_read,
290};
291
292/*
293 * Keep track of how many times we encountered a full subbuffer, to aid
294 * the user space app in telling how many lost events there were.
295 */
296static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
297 void *prev_subbuf, size_t prev_padding)
298{
299 struct blk_trace *bt;
300
301 if (!relay_buf_full(buf))
302 return 1;
303
304 bt = buf->chan->private_data;
305 atomic_inc(&bt->dropped);
306 return 0;
307}
308
309static int blk_remove_buf_file_callback(struct dentry *dentry)
310{
311 debugfs_remove(dentry);
312 return 0;
313}
314
315static struct dentry *blk_create_buf_file_callback(const char *filename,
316 struct dentry *parent,
317 int mode,
318 struct rchan_buf *buf,
319 int *is_global)
320{
321 return debugfs_create_file(filename, mode, parent, buf,
322 &relay_file_operations);
323}
324
325static struct rchan_callbacks blk_relay_callbacks = {
326 .subbuf_start = blk_subbuf_start_callback,
327 .create_buf_file = blk_create_buf_file_callback,
328 .remove_buf_file = blk_remove_buf_file_callback,
329};
330
331/*
332 * Setup everything required to start tracing
333 */
Christof Schmitt6da127a2008-01-11 10:09:43 +0100334int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
Arnd Bergmann171044d42007-10-09 13:23:53 +0200335 struct blk_user_trace_setup *buts)
Jens Axboe2056a782006-03-23 20:00:26 +0100336{
Jens Axboe2056a782006-03-23 20:00:26 +0100337 struct blk_trace *old_bt, *bt = NULL;
338 struct dentry *dir = NULL;
Jens Axboe2056a782006-03-23 20:00:26 +0100339 int ret, i;
340
Arnd Bergmann171044d42007-10-09 13:23:53 +0200341 if (!buts->buf_size || !buts->buf_nr)
Jens Axboe2056a782006-03-23 20:00:26 +0100342 return -EINVAL;
343
Christof Schmitt6da127a2008-01-11 10:09:43 +0100344 strcpy(buts->name, name);
Jens Axboe2056a782006-03-23 20:00:26 +0100345
346 /*
347 * some device names have larger paths - convert the slashes
348 * to underscores for this to work as expected
349 */
Arnd Bergmann171044d42007-10-09 13:23:53 +0200350 for (i = 0; i < strlen(buts->name); i++)
351 if (buts->name[i] == '/')
352 buts->name[i] = '_';
Jens Axboe2056a782006-03-23 20:00:26 +0100353
354 ret = -ENOMEM;
355 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
356 if (!bt)
357 goto err;
358
359 bt->sequence = alloc_percpu(unsigned long);
360 if (!bt->sequence)
361 goto err;
362
363 ret = -ENOENT;
Arnd Bergmann171044d42007-10-09 13:23:53 +0200364 dir = blk_create_tree(buts->name);
Jens Axboe2056a782006-03-23 20:00:26 +0100365 if (!dir)
366 goto err;
367
368 bt->dir = dir;
Christof Schmitt6da127a2008-01-11 10:09:43 +0100369 bt->dev = dev;
Jens Axboe2056a782006-03-23 20:00:26 +0100370 atomic_set(&bt->dropped, 0);
371
372 ret = -EIO;
373 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt, &blk_dropped_fops);
374 if (!bt->dropped_file)
375 goto err;
376
Arnd Bergmann171044d42007-10-09 13:23:53 +0200377 bt->rchan = relay_open("trace", dir, buts->buf_size,
378 buts->buf_nr, &blk_relay_callbacks, bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100379 if (!bt->rchan)
380 goto err;
Jens Axboe2056a782006-03-23 20:00:26 +0100381
Arnd Bergmann171044d42007-10-09 13:23:53 +0200382 bt->act_mask = buts->act_mask;
Jens Axboe2056a782006-03-23 20:00:26 +0100383 if (!bt->act_mask)
384 bt->act_mask = (u16) -1;
385
Arnd Bergmann171044d42007-10-09 13:23:53 +0200386 bt->start_lba = buts->start_lba;
387 bt->end_lba = buts->end_lba;
Jens Axboe2056a782006-03-23 20:00:26 +0100388 if (!bt->end_lba)
389 bt->end_lba = -1ULL;
390
Arnd Bergmann171044d42007-10-09 13:23:53 +0200391 bt->pid = buts->pid;
Jens Axboe2056a782006-03-23 20:00:26 +0100392 bt->trace_state = Blktrace_setup;
393
394 ret = -EBUSY;
395 old_bt = xchg(&q->blk_trace, bt);
396 if (old_bt) {
397 (void) xchg(&q->blk_trace, old_bt);
398 goto err;
399 }
400
401 return 0;
402err:
403 if (dir)
404 blk_remove_tree(dir);
405 if (bt) {
406 if (bt->dropped_file)
407 debugfs_remove(bt->dropped_file);
Alan Sterna1205862006-12-06 20:32:37 -0800408 free_percpu(bt->sequence);
Jens Axboe2056a782006-03-23 20:00:26 +0100409 if (bt->rchan)
410 relay_close(bt->rchan);
411 kfree(bt);
412 }
413 return ret;
414}
415
Christof Schmitt6da127a2008-01-11 10:09:43 +0100416int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
417 char __user *arg)
Arnd Bergmann171044d42007-10-09 13:23:53 +0200418{
419 struct blk_user_trace_setup buts;
420 int ret;
421
422 ret = copy_from_user(&buts, arg, sizeof(buts));
423 if (ret)
424 return -EFAULT;
425
Christof Schmitt6da127a2008-01-11 10:09:43 +0100426 ret = do_blk_trace_setup(q, name, dev, &buts);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200427 if (ret)
428 return ret;
429
430 if (copy_to_user(arg, &buts, sizeof(buts)))
431 return -EFAULT;
432
433 return 0;
434}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100435EXPORT_SYMBOL_GPL(blk_trace_setup);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200436
Christof Schmitt6da127a2008-01-11 10:09:43 +0100437int blk_trace_startstop(struct request_queue *q, int start)
Jens Axboe2056a782006-03-23 20:00:26 +0100438{
439 struct blk_trace *bt;
440 int ret;
441
442 if ((bt = q->blk_trace) == NULL)
443 return -EINVAL;
444
445 /*
446 * For starting a trace, we can transition from a setup or stopped
447 * trace. For stopping a trace, the state must be running
448 */
449 ret = -EINVAL;
450 if (start) {
451 if (bt->trace_state == Blktrace_setup ||
452 bt->trace_state == Blktrace_stopped) {
453 blktrace_seq++;
454 smp_mb();
455 bt->trace_state = Blktrace_running;
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100456
457 trace_note_time(bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100458 ret = 0;
459 }
460 } else {
461 if (bt->trace_state == Blktrace_running) {
462 bt->trace_state = Blktrace_stopped;
463 relay_flush(bt->rchan);
464 ret = 0;
465 }
466 }
467
468 return ret;
469}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100470EXPORT_SYMBOL_GPL(blk_trace_startstop);
Jens Axboe2056a782006-03-23 20:00:26 +0100471
472/**
473 * blk_trace_ioctl: - handle the ioctls associated with tracing
474 * @bdev: the block device
475 * @cmd: the ioctl cmd
476 * @arg: the argument data, if any
477 *
478 **/
479int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
480{
Jens Axboe165125e2007-07-24 09:28:11 +0200481 struct request_queue *q;
Jens Axboe2056a782006-03-23 20:00:26 +0100482 int ret, start = 0;
Christof Schmitt6da127a2008-01-11 10:09:43 +0100483 char b[BDEVNAME_SIZE];
Jens Axboe2056a782006-03-23 20:00:26 +0100484
485 q = bdev_get_queue(bdev);
486 if (!q)
487 return -ENXIO;
488
489 mutex_lock(&bdev->bd_mutex);
490
491 switch (cmd) {
492 case BLKTRACESETUP:
Jean Delvaref36f21e2008-05-12 14:02:33 -0700493 bdevname(bdev, b);
Christof Schmitt6da127a2008-01-11 10:09:43 +0100494 ret = blk_trace_setup(q, b, bdev->bd_dev, arg);
Jens Axboe2056a782006-03-23 20:00:26 +0100495 break;
496 case BLKTRACESTART:
497 start = 1;
498 case BLKTRACESTOP:
499 ret = blk_trace_startstop(q, start);
500 break;
501 case BLKTRACETEARDOWN:
502 ret = blk_trace_remove(q);
503 break;
504 default:
505 ret = -ENOTTY;
506 break;
507 }
508
509 mutex_unlock(&bdev->bd_mutex);
510 return ret;
511}
512
513/**
514 * blk_trace_shutdown: - stop and cleanup trace structures
515 * @q: the request queue associated with the device
516 *
517 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200518void blk_trace_shutdown(struct request_queue *q)
Jens Axboe2056a782006-03-23 20:00:26 +0100519{
Alexey Dobriyan6c5c9342006-09-29 01:59:40 -0700520 if (q->blk_trace) {
521 blk_trace_startstop(q, 0);
522 blk_trace_remove(q);
523 }
Jens Axboe2056a782006-03-23 20:00:26 +0100524}