4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
9 * A simple filesystem for configuration and
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/oprofile.h>
17 #include <linux/pagemap.h>
18 #include <asm/uaccess.h>
22 #define OPROFILEFS_MAGIC 0x6f70726f
24 spinlock_t oprofilefs_lock = SPIN_LOCK_UNLOCKED;
26 static struct inode * oprofilefs_get_inode(struct super_block * sb, int mode)
28 struct inode * inode = new_inode(sb);
34 inode->i_blksize = PAGE_CACHE_SIZE;
36 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
42 static struct super_operations s_ops = {
43 .statfs = simple_statfs,
44 .drop_inode = generic_delete_inode,
48 ssize_t oprofilefs_str_to_user(char const * str, char __user * buf, size_t count, loff_t * offset)
50 size_t len = strlen(str);
58 if (count > len - *offset)
59 count = len - *offset;
61 if (copy_to_user(buf, str + *offset, count))
72 ssize_t oprofilefs_ulong_to_user(unsigned long val, char __user * buf, size_t count, loff_t * offset)
74 char tmpbuf[TMPBUFSIZE];
80 spin_lock(&oprofilefs_lock);
81 maxlen = snprintf(tmpbuf, TMPBUFSIZE, "%lu\n", val);
82 spin_unlock(&oprofilefs_lock);
83 if (maxlen > TMPBUFSIZE)
89 if (count > maxlen - *offset)
90 count = maxlen - *offset;
92 if (copy_to_user(buf, tmpbuf + *offset, count))
101 int oprofilefs_ulong_from_user(unsigned long * val, char const __user * buf, size_t count)
103 char tmpbuf[TMPBUFSIZE];
108 if (count > TMPBUFSIZE - 1)
111 memset(tmpbuf, 0x0, TMPBUFSIZE);
113 if (copy_from_user(tmpbuf, buf, count))
116 spin_lock(&oprofilefs_lock);
117 *val = simple_strtoul(tmpbuf, NULL, 0);
118 spin_unlock(&oprofilefs_lock);
123 static ssize_t ulong_read_file(struct file * file, char __user * buf, size_t count, loff_t * offset)
125 unsigned long * val = file->private_data;
126 return oprofilefs_ulong_to_user(*val, buf, count, offset);
130 static ssize_t ulong_write_file(struct file * file, char const __user * buf, size_t count, loff_t * offset)
132 unsigned long * value = file->private_data;
138 retval = oprofilefs_ulong_from_user(value, buf, count);
146 static int default_open(struct inode * inode, struct file * filp)
148 if (inode->u.generic_ip)
149 filp->private_data = inode->u.generic_ip;
154 static struct file_operations ulong_fops = {
155 .read = ulong_read_file,
156 .write = ulong_write_file,
157 .open = default_open,
161 static struct file_operations ulong_ro_fops = {
162 .read = ulong_read_file,
163 .open = default_open,
167 static struct dentry * __oprofilefs_create_file(struct super_block * sb,
168 struct dentry * root, char const * name, struct file_operations * fops,
171 struct dentry * dentry;
172 struct inode * inode;
175 qname.len = strlen(name);
176 qname.hash = full_name_hash(qname.name, qname.len);
177 dentry = d_alloc(root, &qname);
180 inode = oprofilefs_get_inode(sb, S_IFREG | perm);
186 d_add(dentry, inode);
191 int oprofilefs_create_ulong(struct super_block * sb, struct dentry * root,
192 char const * name, unsigned long * val)
194 struct dentry * d = __oprofilefs_create_file(sb, root, name,
199 d->d_inode->u.generic_ip = val;
204 int oprofilefs_create_ro_ulong(struct super_block * sb, struct dentry * root,
205 char const * name, unsigned long * val)
207 struct dentry * d = __oprofilefs_create_file(sb, root, name,
208 &ulong_ro_fops, 0444);
212 d->d_inode->u.generic_ip = val;
217 static ssize_t atomic_read_file(struct file * file, char __user * buf, size_t count, loff_t * offset)
219 atomic_t * val = file->private_data;
220 return oprofilefs_ulong_to_user(atomic_read(val), buf, count, offset);
224 static struct file_operations atomic_ro_fops = {
225 .read = atomic_read_file,
226 .open = default_open,
230 int oprofilefs_create_ro_atomic(struct super_block * sb, struct dentry * root,
231 char const * name, atomic_t * val)
233 struct dentry * d = __oprofilefs_create_file(sb, root, name,
234 &atomic_ro_fops, 0444);
238 d->d_inode->u.generic_ip = val;
243 int oprofilefs_create_file(struct super_block * sb, struct dentry * root,
244 char const * name, struct file_operations * fops)
246 if (!__oprofilefs_create_file(sb, root, name, fops, 0644))
252 int oprofilefs_create_file_perm(struct super_block * sb, struct dentry * root,
253 char const * name, struct file_operations * fops, int perm)
255 if (!__oprofilefs_create_file(sb, root, name, fops, perm))
261 struct dentry * oprofilefs_mkdir(struct super_block * sb,
262 struct dentry * root, char const * name)
264 struct dentry * dentry;
265 struct inode * inode;
268 qname.len = strlen(name);
269 qname.hash = full_name_hash(qname.name, qname.len);
270 dentry = d_alloc(root, &qname);
273 inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
278 inode->i_op = &simple_dir_inode_operations;
279 inode->i_fop = &simple_dir_operations;
280 d_add(dentry, inode);
285 static int oprofilefs_fill_super(struct super_block * sb, void * data, int silent)
287 struct inode * root_inode;
288 struct dentry * root_dentry;
290 sb->s_blocksize = PAGE_CACHE_SIZE;
291 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
292 sb->s_magic = OPROFILEFS_MAGIC;
295 root_inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
298 root_inode->i_op = &simple_dir_inode_operations;
299 root_inode->i_fop = &simple_dir_operations;
300 root_dentry = d_alloc_root(root_inode);
306 sb->s_root = root_dentry;
308 oprofile_create_files(sb, root_dentry);
310 // FIXME: verify kill_litter_super removes our dentries
315 static struct super_block *oprofilefs_get_sb(struct file_system_type *fs_type,
316 int flags, const char *dev_name, void *data)
318 return get_sb_single(fs_type, flags, data, oprofilefs_fill_super);
322 static struct file_system_type oprofilefs_type = {
323 .owner = THIS_MODULE,
324 .name = "oprofilefs",
325 .get_sb = oprofilefs_get_sb,
326 .kill_sb = kill_litter_super,
330 int __init oprofilefs_register(void)
332 return register_filesystem(&oprofilefs_type);
336 void __exit oprofilefs_unregister(void)
338 unregister_filesystem(&oprofilefs_type);