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)
170 struct dentry * dentry;
171 struct inode * inode;
174 qname.len = strlen(name);
175 qname.hash = full_name_hash(qname.name, qname.len);
176 dentry = d_alloc(root, &qname);
179 inode = oprofilefs_get_inode(sb, S_IFREG | 0644);
185 d_add(dentry, inode);
190 int oprofilefs_create_ulong(struct super_block * sb, struct dentry * root,
191 char const * name, unsigned long * val)
193 struct dentry * d = __oprofilefs_create_file(sb, root, name, &ulong_fops);
197 d->d_inode->u.generic_ip = val;
202 int oprofilefs_create_ro_ulong(struct super_block * sb, struct dentry * root,
203 char const * name, unsigned long * val)
205 struct dentry * d = __oprofilefs_create_file(sb, root, name, &ulong_ro_fops);
209 d->d_inode->u.generic_ip = val;
214 static ssize_t atomic_read_file(struct file * file, char __user * buf, size_t count, loff_t * offset)
216 atomic_t * val = file->private_data;
217 return oprofilefs_ulong_to_user(atomic_read(val), buf, count, offset);
221 static struct file_operations atomic_ro_fops = {
222 .read = atomic_read_file,
223 .open = default_open,
227 int oprofilefs_create_ro_atomic(struct super_block * sb, struct dentry * root,
228 char const * name, atomic_t * val)
230 struct dentry * d = __oprofilefs_create_file(sb, root, name, &atomic_ro_fops);
234 d->d_inode->u.generic_ip = val;
239 int oprofilefs_create_file(struct super_block * sb, struct dentry * root,
240 char const * name, struct file_operations * fops)
242 if (!__oprofilefs_create_file(sb, root, name, fops))
248 struct dentry * oprofilefs_mkdir(struct super_block * sb,
249 struct dentry * root, char const * name)
251 struct dentry * dentry;
252 struct inode * inode;
255 qname.len = strlen(name);
256 qname.hash = full_name_hash(qname.name, qname.len);
257 dentry = d_alloc(root, &qname);
260 inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
265 inode->i_op = &simple_dir_inode_operations;
266 inode->i_fop = &simple_dir_operations;
267 d_add(dentry, inode);
272 static int oprofilefs_fill_super(struct super_block * sb, void * data, int silent)
274 struct inode * root_inode;
275 struct dentry * root_dentry;
277 sb->s_blocksize = PAGE_CACHE_SIZE;
278 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
279 sb->s_magic = OPROFILEFS_MAGIC;
282 root_inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
285 root_inode->i_op = &simple_dir_inode_operations;
286 root_inode->i_fop = &simple_dir_operations;
287 root_dentry = d_alloc_root(root_inode);
293 sb->s_root = root_dentry;
295 oprofile_create_files(sb, root_dentry);
297 // FIXME: verify kill_litter_super removes our dentries
302 static struct super_block *oprofilefs_get_sb(struct file_system_type *fs_type,
303 int flags, const char *dev_name, void *data)
305 return get_sb_single(fs_type, flags, data, oprofilefs_fill_super);
309 static struct file_system_type oprofilefs_type = {
310 .owner = THIS_MODULE,
311 .name = "oprofilefs",
312 .get_sb = oprofilefs_get_sb,
313 .kill_sb = kill_litter_super,
317 int __init oprofilefs_register(void)
319 return register_filesystem(&oprofilefs_type);
323 void __exit oprofilefs_unregister(void)
325 unregister_filesystem(&oprofilefs_type);