1 /**
2  * @file oprofilefs.c
3  *
4  * @remark Copyright 2002 OProfile authors
5  * @remark Read the file COPYING
6  *
7  * @author John Levon
8  *
9  * A simple filesystem for configuration and
10  * access of oprofile.
11  */
12 
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/oprofile.h>
16 #include <linux/fs.h>
17 #include <linux/pagemap.h>
18 #include <linux/uaccess.h>
19 
20 #include "oprof.h"
21 
22 #define OPROFILEFS_MAGIC 0x6f70726f
23 
24 DEFINE_RAW_SPINLOCK(oprofilefs_lock);
25 
oprofilefs_get_inode(struct super_block * sb,int mode)26 static struct inode *oprofilefs_get_inode(struct super_block *sb, int mode)
27 {
28 	struct inode *inode = new_inode(sb);
29 
30 	if (inode) {
31 		inode->i_ino = get_next_ino();
32 		inode->i_mode = mode;
33 		inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
34 	}
35 	return inode;
36 }
37 
38 
39 static const struct super_operations s_ops = {
40 	.statfs		= simple_statfs,
41 	.drop_inode 	= generic_delete_inode,
42 };
43 
44 
oprofilefs_str_to_user(char const * str,char __user * buf,size_t count,loff_t * offset)45 ssize_t oprofilefs_str_to_user(char const *str, char __user *buf, size_t count, loff_t *offset)
46 {
47 	return simple_read_from_buffer(buf, count, offset, str, strlen(str));
48 }
49 
50 
51 #define TMPBUFSIZE 50
52 
oprofilefs_ulong_to_user(unsigned long val,char __user * buf,size_t count,loff_t * offset)53 ssize_t oprofilefs_ulong_to_user(unsigned long val, char __user *buf, size_t count, loff_t *offset)
54 {
55 	char tmpbuf[TMPBUFSIZE];
56 	size_t maxlen = snprintf(tmpbuf, TMPBUFSIZE, "%lu\n", val);
57 	if (maxlen > TMPBUFSIZE)
58 		maxlen = TMPBUFSIZE;
59 	return simple_read_from_buffer(buf, count, offset, tmpbuf, maxlen);
60 }
61 
62 
63 /*
64  * Note: If oprofilefs_ulong_from_user() returns 0, then *val remains
65  * unchanged and might be uninitialized. This follows write syscall
66  * implementation when count is zero: "If count is zero ... [and if]
67  * no errors are detected, 0 will be returned without causing any
68  * other effect." (man 2 write)
69  */
oprofilefs_ulong_from_user(unsigned long * val,char const __user * buf,size_t count)70 int oprofilefs_ulong_from_user(unsigned long *val, char const __user *buf, size_t count)
71 {
72 	char tmpbuf[TMPBUFSIZE];
73 	unsigned long flags;
74 
75 	if (!count)
76 		return 0;
77 
78 	if (count > TMPBUFSIZE - 1)
79 		return -EINVAL;
80 
81 	memset(tmpbuf, 0x0, TMPBUFSIZE);
82 
83 	if (copy_from_user(tmpbuf, buf, count))
84 		return -EFAULT;
85 
86 	raw_spin_lock_irqsave(&oprofilefs_lock, flags);
87 	*val = simple_strtoul(tmpbuf, NULL, 0);
88 	raw_spin_unlock_irqrestore(&oprofilefs_lock, flags);
89 	return count;
90 }
91 
92 
ulong_read_file(struct file * file,char __user * buf,size_t count,loff_t * offset)93 static ssize_t ulong_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
94 {
95 	unsigned long *val = file->private_data;
96 	return oprofilefs_ulong_to_user(*val, buf, count, offset);
97 }
98 
99 
ulong_write_file(struct file * file,char const __user * buf,size_t count,loff_t * offset)100 static ssize_t ulong_write_file(struct file *file, char const __user *buf, size_t count, loff_t *offset)
101 {
102 	unsigned long value;
103 	int retval;
104 
105 	if (*offset)
106 		return -EINVAL;
107 
108 	retval = oprofilefs_ulong_from_user(&value, buf, count);
109 	if (retval <= 0)
110 		return retval;
111 
112 	retval = oprofile_set_ulong(file->private_data, value);
113 	if (retval)
114 		return retval;
115 
116 	return count;
117 }
118 
119 
120 static const struct file_operations ulong_fops = {
121 	.read		= ulong_read_file,
122 	.write		= ulong_write_file,
123 	.open		= simple_open,
124 	.llseek		= default_llseek,
125 };
126 
127 
128 static const struct file_operations ulong_ro_fops = {
129 	.read		= ulong_read_file,
130 	.open		= simple_open,
131 	.llseek		= default_llseek,
132 };
133 
134 
__oprofilefs_create_file(struct dentry * root,char const * name,const struct file_operations * fops,int perm,void * priv)135 static int __oprofilefs_create_file(struct dentry *root, char const *name,
136 	const struct file_operations *fops, int perm, void *priv)
137 {
138 	struct dentry *dentry;
139 	struct inode *inode;
140 
141 	if (!root)
142 		return -ENOMEM;
143 
144 	inode_lock(d_inode(root));
145 	dentry = d_alloc_name(root, name);
146 	if (!dentry) {
147 		inode_unlock(d_inode(root));
148 		return -ENOMEM;
149 	}
150 	inode = oprofilefs_get_inode(root->d_sb, S_IFREG | perm);
151 	if (!inode) {
152 		dput(dentry);
153 		inode_unlock(d_inode(root));
154 		return -ENOMEM;
155 	}
156 	inode->i_fop = fops;
157 	inode->i_private = priv;
158 	d_add(dentry, inode);
159 	inode_unlock(d_inode(root));
160 	return 0;
161 }
162 
163 
oprofilefs_create_ulong(struct dentry * root,char const * name,unsigned long * val)164 int oprofilefs_create_ulong(struct dentry *root,
165 	char const *name, unsigned long *val)
166 {
167 	return __oprofilefs_create_file(root, name,
168 					&ulong_fops, 0644, val);
169 }
170 
171 
oprofilefs_create_ro_ulong(struct dentry * root,char const * name,unsigned long * val)172 int oprofilefs_create_ro_ulong(struct dentry *root,
173 	char const *name, unsigned long *val)
174 {
175 	return __oprofilefs_create_file(root, name,
176 					&ulong_ro_fops, 0444, val);
177 }
178 
179 
atomic_read_file(struct file * file,char __user * buf,size_t count,loff_t * offset)180 static ssize_t atomic_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
181 {
182 	atomic_t *val = file->private_data;
183 	return oprofilefs_ulong_to_user(atomic_read(val), buf, count, offset);
184 }
185 
186 
187 static const struct file_operations atomic_ro_fops = {
188 	.read		= atomic_read_file,
189 	.open		= simple_open,
190 	.llseek		= default_llseek,
191 };
192 
193 
oprofilefs_create_ro_atomic(struct dentry * root,char const * name,atomic_t * val)194 int oprofilefs_create_ro_atomic(struct dentry *root,
195 	char const *name, atomic_t *val)
196 {
197 	return __oprofilefs_create_file(root, name,
198 					&atomic_ro_fops, 0444, val);
199 }
200 
201 
oprofilefs_create_file(struct dentry * root,char const * name,const struct file_operations * fops)202 int oprofilefs_create_file(struct dentry *root,
203 	char const *name, const struct file_operations *fops)
204 {
205 	return __oprofilefs_create_file(root, name, fops, 0644, NULL);
206 }
207 
208 
oprofilefs_create_file_perm(struct dentry * root,char const * name,const struct file_operations * fops,int perm)209 int oprofilefs_create_file_perm(struct dentry *root,
210 	char const *name, const struct file_operations *fops, int perm)
211 {
212 	return __oprofilefs_create_file(root, name, fops, perm, NULL);
213 }
214 
215 
oprofilefs_mkdir(struct dentry * parent,char const * name)216 struct dentry *oprofilefs_mkdir(struct dentry *parent, char const *name)
217 {
218 	struct dentry *dentry;
219 	struct inode *inode;
220 
221 	inode_lock(d_inode(parent));
222 	dentry = d_alloc_name(parent, name);
223 	if (!dentry) {
224 		inode_unlock(d_inode(parent));
225 		return NULL;
226 	}
227 	inode = oprofilefs_get_inode(parent->d_sb, S_IFDIR | 0755);
228 	if (!inode) {
229 		dput(dentry);
230 		inode_unlock(d_inode(parent));
231 		return NULL;
232 	}
233 	inode->i_op = &simple_dir_inode_operations;
234 	inode->i_fop = &simple_dir_operations;
235 	d_add(dentry, inode);
236 	inode_unlock(d_inode(parent));
237 	return dentry;
238 }
239 
240 
oprofilefs_fill_super(struct super_block * sb,void * data,int silent)241 static int oprofilefs_fill_super(struct super_block *sb, void *data, int silent)
242 {
243 	struct inode *root_inode;
244 
245 	sb->s_blocksize = PAGE_SIZE;
246 	sb->s_blocksize_bits = PAGE_SHIFT;
247 	sb->s_magic = OPROFILEFS_MAGIC;
248 	sb->s_op = &s_ops;
249 	sb->s_time_gran = 1;
250 
251 	root_inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
252 	if (!root_inode)
253 		return -ENOMEM;
254 	root_inode->i_op = &simple_dir_inode_operations;
255 	root_inode->i_fop = &simple_dir_operations;
256 	sb->s_root = d_make_root(root_inode);
257 	if (!sb->s_root)
258 		return -ENOMEM;
259 
260 	oprofile_create_files(sb->s_root);
261 
262 	// FIXME: verify kill_litter_super removes our dentries
263 	return 0;
264 }
265 
266 
oprofilefs_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)267 static struct dentry *oprofilefs_mount(struct file_system_type *fs_type,
268 	int flags, const char *dev_name, void *data)
269 {
270 	return mount_single(fs_type, flags, data, oprofilefs_fill_super);
271 }
272 
273 
274 static struct file_system_type oprofilefs_type = {
275 	.owner		= THIS_MODULE,
276 	.name		= "oprofilefs",
277 	.mount		= oprofilefs_mount,
278 	.kill_sb	= kill_litter_super,
279 };
280 MODULE_ALIAS_FS("oprofilefs");
281 
282 
oprofilefs_register(void)283 int __init oprofilefs_register(void)
284 {
285 	return register_filesystem(&oprofilefs_type);
286 }
287 
288 
oprofilefs_unregister(void)289 void __exit oprofilefs_unregister(void)
290 {
291 	unregister_filesystem(&oprofilefs_type);
292 }
293