oprofilefs.c 6.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/**
 * @file oprofilefs.c
 *
 * @remark Copyright 2002 OProfile authors
 * @remark Read the file COPYING
 *
 * @author John Levon
 *
 * A simple filesystem for configuration and
 * access of oprofile.
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/oprofile.h>
#include <linux/fs.h>
#include <linux/pagemap.h>
#include <asm/uaccess.h>

#include "oprof.h"

#define OPROFILEFS_MAGIC 0x6f70726f

DEFINE_SPINLOCK(oprofilefs_lock);

R
Robert Richter 已提交
26
static struct inode *oprofilefs_get_inode(struct super_block *sb, int mode)
L
Linus Torvalds 已提交
27
{
R
Robert Richter 已提交
28
	struct inode *inode = new_inode(sb);
L
Linus Torvalds 已提交
29 30 31 32 33 34 35 36 37

	if (inode) {
		inode->i_mode = mode;
		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
	}
	return inode;
}


38
static const struct super_operations s_ops = {
L
Linus Torvalds 已提交
39 40 41 42 43
	.statfs		= simple_statfs,
	.drop_inode 	= generic_delete_inode,
};


R
Robert Richter 已提交
44
ssize_t oprofilefs_str_to_user(char const *str, char __user *buf, size_t count, loff_t *offset)
L
Linus Torvalds 已提交
45 46 47 48 49 50 51
{
	return simple_read_from_buffer(buf, count, offset, str, strlen(str));
}


#define TMPBUFSIZE 50

R
Robert Richter 已提交
52
ssize_t oprofilefs_ulong_to_user(unsigned long val, char __user *buf, size_t count, loff_t *offset)
L
Linus Torvalds 已提交
53 54 55 56 57 58 59 60 61
{
	char tmpbuf[TMPBUFSIZE];
	size_t maxlen = snprintf(tmpbuf, TMPBUFSIZE, "%lu\n", val);
	if (maxlen > TMPBUFSIZE)
		maxlen = TMPBUFSIZE;
	return simple_read_from_buffer(buf, count, offset, tmpbuf, maxlen);
}


R
Robert Richter 已提交
62
int oprofilefs_ulong_from_user(unsigned long *val, char const __user *buf, size_t count)
L
Linus Torvalds 已提交
63 64
{
	char tmpbuf[TMPBUFSIZE];
65
	unsigned long flags;
L
Linus Torvalds 已提交
66 67 68 69 70 71 72 73 74 75 76 77

	if (!count)
		return 0;

	if (count > TMPBUFSIZE - 1)
		return -EINVAL;

	memset(tmpbuf, 0x0, TMPBUFSIZE);

	if (copy_from_user(tmpbuf, buf, count))
		return -EFAULT;

78
	spin_lock_irqsave(&oprofilefs_lock, flags);
L
Linus Torvalds 已提交
79
	*val = simple_strtoul(tmpbuf, NULL, 0);
80
	spin_unlock_irqrestore(&oprofilefs_lock, flags);
L
Linus Torvalds 已提交
81 82 83 84
	return 0;
}


R
Robert Richter 已提交
85
static ssize_t ulong_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
L
Linus Torvalds 已提交
86
{
R
Robert Richter 已提交
87
	unsigned long *val = file->private_data;
L
Linus Torvalds 已提交
88 89 90 91
	return oprofilefs_ulong_to_user(*val, buf, count, offset);
}


R
Robert Richter 已提交
92
static ssize_t ulong_write_file(struct file *file, char const __user *buf, size_t count, loff_t *offset)
L
Linus Torvalds 已提交
93
{
R
Robert Richter 已提交
94
	unsigned long *value = file->private_data;
L
Linus Torvalds 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107
	int retval;

	if (*offset)
		return -EINVAL;

	retval = oprofilefs_ulong_from_user(value, buf, count);

	if (retval)
		return retval;
	return count;
}


R
Robert Richter 已提交
108
static int default_open(struct inode *inode, struct file *filp)
L
Linus Torvalds 已提交
109
{
110 111
	if (inode->i_private)
		filp->private_data = inode->i_private;
L
Linus Torvalds 已提交
112 113 114 115
	return 0;
}


116
static const struct file_operations ulong_fops = {
L
Linus Torvalds 已提交
117 118 119 120 121 122
	.read		= ulong_read_file,
	.write		= ulong_write_file,
	.open		= default_open,
};


123
static const struct file_operations ulong_ro_fops = {
L
Linus Torvalds 已提交
124 125 126 127 128
	.read		= ulong_read_file,
	.open		= default_open,
};


R
Robert Richter 已提交
129 130
static struct dentry *__oprofilefs_create_file(struct super_block *sb,
	struct dentry *root, char const *name, const struct file_operations *fops,
L
Linus Torvalds 已提交
131 132
	int perm)
{
R
Robert Richter 已提交
133 134
	struct dentry *dentry;
	struct inode *inode;
L
Linus Torvalds 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

	dentry = d_alloc_name(root, name);
	if (!dentry)
		return NULL;
	inode = oprofilefs_get_inode(sb, S_IFREG | perm);
	if (!inode) {
		dput(dentry);
		return NULL;
	}
	inode->i_fop = fops;
	d_add(dentry, inode);
	return dentry;
}


R
Robert Richter 已提交
150 151
int oprofilefs_create_ulong(struct super_block *sb, struct dentry *root,
	char const *name, unsigned long *val)
L
Linus Torvalds 已提交
152
{
R
Robert Richter 已提交
153
	struct dentry *d = __oprofilefs_create_file(sb, root, name,
L
Linus Torvalds 已提交
154 155 156 157
						     &ulong_fops, 0644);
	if (!d)
		return -EFAULT;

158
	d->d_inode->i_private = val;
L
Linus Torvalds 已提交
159 160 161 162
	return 0;
}


R
Robert Richter 已提交
163 164
int oprofilefs_create_ro_ulong(struct super_block *sb, struct dentry *root,
	char const *name, unsigned long *val)
L
Linus Torvalds 已提交
165
{
R
Robert Richter 已提交
166
	struct dentry *d = __oprofilefs_create_file(sb, root, name,
L
Linus Torvalds 已提交
167 168 169 170
						     &ulong_ro_fops, 0444);
	if (!d)
		return -EFAULT;

171
	d->d_inode->i_private = val;
L
Linus Torvalds 已提交
172 173 174 175
	return 0;
}


R
Robert Richter 已提交
176
static ssize_t atomic_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
L
Linus Torvalds 已提交
177
{
R
Robert Richter 已提交
178
	atomic_t *val = file->private_data;
L
Linus Torvalds 已提交
179 180
	return oprofilefs_ulong_to_user(atomic_read(val), buf, count, offset);
}
181

L
Linus Torvalds 已提交
182

183
static const struct file_operations atomic_ro_fops = {
L
Linus Torvalds 已提交
184 185 186
	.read		= atomic_read_file,
	.open		= default_open,
};
187

L
Linus Torvalds 已提交
188

R
Robert Richter 已提交
189 190
int oprofilefs_create_ro_atomic(struct super_block *sb, struct dentry *root,
	char const *name, atomic_t *val)
L
Linus Torvalds 已提交
191
{
R
Robert Richter 已提交
192
	struct dentry *d = __oprofilefs_create_file(sb, root, name,
L
Linus Torvalds 已提交
193 194 195 196
						     &atomic_ro_fops, 0444);
	if (!d)
		return -EFAULT;

197
	d->d_inode->i_private = val;
L
Linus Torvalds 已提交
198 199 200
	return 0;
}

201

R
Robert Richter 已提交
202 203
int oprofilefs_create_file(struct super_block *sb, struct dentry *root,
	char const *name, const struct file_operations *fops)
L
Linus Torvalds 已提交
204 205 206 207 208 209 210
{
	if (!__oprofilefs_create_file(sb, root, name, fops, 0644))
		return -EFAULT;
	return 0;
}


R
Robert Richter 已提交
211 212
int oprofilefs_create_file_perm(struct super_block *sb, struct dentry *root,
	char const *name, const struct file_operations *fops, int perm)
L
Linus Torvalds 已提交
213 214 215 216 217 218 219
{
	if (!__oprofilefs_create_file(sb, root, name, fops, perm))
		return -EFAULT;
	return 0;
}


R
Robert Richter 已提交
220 221
struct dentry *oprofilefs_mkdir(struct super_block *sb,
	struct dentry *root, char const *name)
L
Linus Torvalds 已提交
222
{
R
Robert Richter 已提交
223 224
	struct dentry *dentry;
	struct inode *inode;
L
Linus Torvalds 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

	dentry = d_alloc_name(root, name);
	if (!dentry)
		return NULL;
	inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
	if (!inode) {
		dput(dentry);
		return NULL;
	}
	inode->i_op = &simple_dir_inode_operations;
	inode->i_fop = &simple_dir_operations;
	d_add(dentry, inode);
	return dentry;
}


R
Robert Richter 已提交
241
static int oprofilefs_fill_super(struct super_block *sb, void *data, int silent)
L
Linus Torvalds 已提交
242
{
R
Robert Richter 已提交
243 244
	struct inode *root_inode;
	struct dentry *root_dentry;
L
Linus Torvalds 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

	sb->s_blocksize = PAGE_CACHE_SIZE;
	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
	sb->s_magic = OPROFILEFS_MAGIC;
	sb->s_op = &s_ops;
	sb->s_time_gran = 1;

	root_inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
	if (!root_inode)
		return -ENOMEM;
	root_inode->i_op = &simple_dir_inode_operations;
	root_inode->i_fop = &simple_dir_operations;
	root_dentry = d_alloc_root(root_inode);
	if (!root_dentry) {
		iput(root_inode);
		return -ENOMEM;
	}

	sb->s_root = root_dentry;

	oprofile_create_files(sb, root_dentry);

	// FIXME: verify kill_litter_super removes our dentries
	return 0;
}


272 273
static int oprofilefs_get_sb(struct file_system_type *fs_type,
	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
L
Linus Torvalds 已提交
274
{
275
	return get_sb_single(fs_type, flags, data, oprofilefs_fill_super, mnt);
L
Linus Torvalds 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
}


static struct file_system_type oprofilefs_type = {
	.owner		= THIS_MODULE,
	.name		= "oprofilefs",
	.get_sb		= oprofilefs_get_sb,
	.kill_sb	= kill_litter_super,
};


int __init oprofilefs_register(void)
{
	return register_filesystem(&oprofilefs_type);
}


void __exit oprofilefs_unregister(void)
{
	unregister_filesystem(&oprofilefs_type);
}