Adrian Hunterblock: add secure discard8d57a9816 years agoHistory
1845 lines · 42.6 KB · C
RawDownload
/* * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA * */#include <linux/kernel.h>#include <linux/blkdev.h>#include <linux/blktrace_api.h>#include <linux/percpu.h>#include <linux/init.h>#include <linux/mutex.h>#include <linux/slab.h>#include <linux/debugfs.h>#include <linux/smp_lock.h>#include <linux/time.h>#include <linux/uaccess.h> #include <trace/events/block.h> #include "trace_output.h" #ifdef CONFIG_BLK_DEV_IO_TRACE static unsigned int blktrace_seq __read_mostly = 1; static struct trace_array *blk_tr;static bool blk_tracer_enabled __read_mostly; /* Select an alternative, minimalistic output than the original one */#define TRACE_BLK_OPT_CLASSIC	0x1 static struct tracer_opt blk_tracer_opts[] = {	/* Default disable the minimalistic output */	{ TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },	{ }}; static struct tracer_flags blk_tracer_flags = {	.val  = 0,	.opts = blk_tracer_opts,}; /* Global reference count of probes */static atomic_t blk_probes_ref = ATOMIC_INIT(0); static void blk_register_tracepoints(void);static void blk_unregister_tracepoints(void); /* * Send out a notify message. */static void trace_note(struct blk_trace *bt, pid_t pid, int action,		       const void *data, size_t len){	struct blk_io_trace *t;	struct ring_buffer_event *event = NULL;	struct ring_buffer *buffer = NULL;	int pc = 0;	int cpu = smp_processor_id();	bool blk_tracer = blk_tracer_enabled; 	if (blk_tracer) {		buffer = blk_tr->buffer;		pc = preempt_count();		event = trace_buffer_lock_reserve(buffer, TRACE_BLK,						  sizeof(*t) + len,						  0, pc);		if (!event)			return;		t = ring_buffer_event_data(event);		goto record_it;	} 	if (!bt->rchan)		return; 	t = relay_reserve(bt->rchan, sizeof(*t) + len);	if (t) {		t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;		t->time = ktime_to_ns(ktime_get());record_it:		t->device = bt->dev;		t->action = action;		t->pid = pid;		t->cpu = cpu;		t->pdu_len = len;		memcpy((void *) t + sizeof(*t), data, len); 		if (blk_tracer)			trace_buffer_unlock_commit(buffer, event, 0, pc);	}} /* * Send out a notify for this process, if we haven't done so since a trace * started */static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk){	tsk->btrace_seq = blktrace_seq;	trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm));} static void trace_note_time(struct blk_trace *bt){	struct timespec now;	unsigned long flags;	u32 words[2]; 	getnstimeofday(&now);	words[0] = now.tv_sec;	words[1] = now.tv_nsec; 	local_irq_save(flags);	trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));	local_irq_restore(flags);} void __trace_note_message(struct blk_trace *bt, const char *fmt, ...){	int n;	va_list args;	unsigned long flags;	char *buf; 	if (unlikely(bt->trace_state != Blktrace_running &&		     !blk_tracer_enabled))		return; 	local_irq_save(flags);	buf = per_cpu_ptr(bt->msg_data, smp_processor_id());	va_start(args, fmt);	n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);	va_end(args); 	trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);	local_irq_restore(flags);}EXPORT_SYMBOL_GPL(__trace_note_message); static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,			 pid_t pid){	if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)		return 1;	if (sector && (sector < bt->start_lba || sector > bt->end_lba))		return 1;	if (bt->pid && pid != bt->pid)		return 1; 	return 0;} /* * Data direction bit lookup */static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ),				 BLK_TC_ACT(BLK_TC_WRITE) }; #define BLK_TC_HARDBARRIER	BLK_TC_BARRIER#define BLK_TC_RAHEAD		BLK_TC_AHEAD /* The ilog2() calls fall out because they're constant */#define MASK_TC_BIT(rw, __name) ((rw & REQ_ ## __name) << \	  (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - __REQ_ ## __name)) /* * The worker for the various blk_add_trace*() types. Fills out a * blk_io_trace structure and places it in a per-cpu subbuffer. */static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,		     int rw, u32 what, int error, int pdu_len, void *pdu_data){	struct task_struct *tsk = current;	struct ring_buffer_event *event = NULL;	struct ring_buffer *buffer = NULL;	struct blk_io_trace *t;	unsigned long flags = 0;	unsigned long *sequence;	pid_t pid;	int cpu, pc = 0;	bool blk_tracer = blk_tracer_enabled; 	if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer))		return; 	what |= ddir_act[rw & WRITE];	what |= MASK_TC_BIT(rw, HARDBARRIER);	what |= MASK_TC_BIT(rw, SYNC);	what |= MASK_TC_BIT(rw, RAHEAD);	what |= MASK_TC_BIT(rw, META);	what |= MASK_TC_BIT(rw, DISCARD); 	pid = tsk->pid;	if (act_log_check(bt, what, sector, pid))		return;	cpu = raw_smp_processor_id(); 	if (blk_tracer) {		tracing_record_cmdline(current); 		buffer = blk_tr->buffer;		pc = preempt_count();		event = trace_buffer_lock_reserve(buffer, TRACE_BLK,						  sizeof(*t) + pdu_len,						  0, pc);		if (!event)			return;		t = ring_buffer_event_data(event);		goto record_it;	} 	/*	 * A word about the locking here - we disable interrupts to reserve	 * some space in the relay per-cpu buffer, to prevent an irq	 * from coming in and stepping on our toes.	 */	local_irq_save(flags); 	if (unlikely(tsk->btrace_seq != blktrace_seq))		trace_note_tsk(bt, tsk); 	t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);	if (t) {		sequence = per_cpu_ptr(bt->sequence, cpu); 		t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;		t->sequence = ++(*sequence);		t->time = ktime_to_ns(ktime_get());record_it:		/*		 * These two are not needed in ftrace as they are in the		 * generic trace_entry, filled by tracing_generic_entry_update,		 * but for the trace_event->bin() synthesizer benefit we do it		 * here too.		 */		t->cpu = cpu;		t->pid = pid; 		t->sector = sector;		t->bytes = bytes;		t->action = what;		t->device = bt->dev;		t->error = error;		t->pdu_len = pdu_len; 		if (pdu_len)			memcpy((void *) t + sizeof(*t), pdu_data, pdu_len); 		if (blk_tracer) {			trace_buffer_unlock_commit(buffer, event, 0, pc);			return;		}	} 	local_irq_restore(flags);} static struct dentry *blk_tree_root;static DEFINE_MUTEX(blk_tree_mutex); static void blk_trace_free(struct blk_trace *bt){	debugfs_remove(bt->msg_file);	debugfs_remove(bt->dropped_file);	relay_close(bt->rchan);	debugfs_remove(bt->dir);	free_percpu(bt->sequence);	free_percpu(bt->msg_data);	kfree(bt);} static void blk_trace_cleanup(struct blk_trace *bt){	blk_trace_free(bt);	if (atomic_dec_and_test(&blk_probes_ref))		blk_unregister_tracepoints();} int blk_trace_remove(struct request_queue *q){	struct blk_trace *bt; 	bt = xchg(&q->blk_trace, NULL);	if (!bt)		return -EINVAL; 	if (bt->trace_state != Blktrace_running)		blk_trace_cleanup(bt); 	return 0;}EXPORT_SYMBOL_GPL(blk_trace_remove); static int blk_dropped_open(struct inode *inode, struct file *filp){	filp->private_data = inode->i_private; 	return 0;} static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,				size_t count, loff_t *ppos){	struct blk_trace *bt = filp->private_data;	char buf[16]; 	snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped)); 	return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));} static const struct file_operations blk_dropped_fops = {	.owner =	THIS_MODULE,	.open =		blk_dropped_open,	.read =		blk_dropped_read,}; static int blk_msg_open(struct inode *inode, struct file *filp){	filp->private_data = inode->i_private; 	return 0;} static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,				size_t count, loff_t *ppos){	char *msg;	struct blk_trace *bt; 	if (count >= BLK_TN_MAX_MSG)		return -EINVAL; 	msg = kmalloc(count + 1, GFP_KERNEL);	if (msg == NULL)		return -ENOMEM; 	if (copy_from_user(msg, buffer, count)) {		kfree(msg);		return -EFAULT;	} 	msg[count] = '\0';	bt = filp->private_data;	__trace_note_message(bt, "%s", msg);	kfree(msg); 	return count;} static const struct file_operations blk_msg_fops = {	.owner =	THIS_MODULE,	.open =		blk_msg_open,	.write =	blk_msg_write,}; /* * Keep track of how many times we encountered a full subbuffer, to aid * the user space app in telling how many lost events there were. */static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,				     void *prev_subbuf, size_t prev_padding){	struct blk_trace *bt; 	if (!relay_buf_full(buf))		return 1; 	bt = buf->chan->private_data;	atomic_inc(&bt->dropped);	return 0;} static int blk_remove_buf_file_callback(struct dentry *dentry){	debugfs_remove(dentry); 	return 0;} static struct dentry *blk_create_buf_file_callback(const char *filename,						   struct dentry *parent,						   int mode,						   struct rchan_buf *buf,						   int *is_global){	return debugfs_create_file(filename, mode, parent, buf,					&relay_file_operations);} static struct rchan_callbacks blk_relay_callbacks = {	.subbuf_start		= blk_subbuf_start_callback,	.create_buf_file	= blk_create_buf_file_callback,	.remove_buf_file	= blk_remove_buf_file_callback,}; static void blk_trace_setup_lba(struct blk_trace *bt,				struct block_device *bdev){	struct hd_struct *part = NULL; 	if (bdev)		part = bdev->bd_part; 	if (part) {		bt->start_lba = part->start_sect;		bt->end_lba = part->start_sect + part->nr_sects;	} else {		bt->start_lba = 0;		bt->end_lba = -1ULL;	}} /* * Setup everything required to start tracing */int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,		       struct block_device *bdev,		       struct blk_user_trace_setup *buts){	struct blk_trace *old_bt, *bt = NULL;	struct dentry *dir = NULL;	int ret, i; 	if (!buts->buf_size || !buts->buf_nr)		return -EINVAL; 	strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);	buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0'; 	/*	 * some device names have larger paths - convert the slashes	 * to underscores for this to work as expected	 */	for (i = 0; i < strlen(buts->name); i++)		if (buts->name[i] == '/')			buts->name[i] = '_'; 	bt = kzalloc(sizeof(*bt), GFP_KERNEL);	if (!bt)		return -ENOMEM; 	ret = -ENOMEM;	bt->sequence = alloc_percpu(unsigned long);	if (!bt->sequence)		goto err; 	bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));	if (!bt->msg_data)		goto err; 	ret = -ENOENT; 	mutex_lock(&blk_tree_mutex);	if (!blk_tree_root) {		blk_tree_root = debugfs_create_dir("block", NULL);		if (!blk_tree_root) {			mutex_unlock(&blk_tree_mutex);			goto err;		}	}	mutex_unlock(&blk_tree_mutex); 	dir = debugfs_create_dir(buts->name, blk_tree_root); 	if (!dir)		goto err; 	bt->dir = dir;	bt->dev = dev;	atomic_set(&bt->dropped, 0); 	ret = -EIO;	bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt,					       &blk_dropped_fops);	if (!bt->dropped_file)		goto err; 	bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);	if (!bt->msg_file)		goto err; 	bt->rchan = relay_open("trace", dir, buts->buf_size,				buts->buf_nr, &blk_relay_callbacks, bt);	if (!bt->rchan)		goto err; 

Lines 1–500 of 1845.