Hopefully get the Kconfig PCI stuff right, finally.
[linux-2.6/linux-mips.git] / drivers / oprofile / oprofilefs.c
blobc82630ec1819d9e3ba72a306d7e566023a294304
1 /**
2 * @file oprofilefs.c
4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
7 * @author John Levon
9 * A simple filesystem for configuration and
10 * access of oprofile.
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 <asm/uaccess.h>
20 #include "oprof.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);
30 if (inode) {
31 inode->i_mode = mode;
32 inode->i_uid = 0;
33 inode->i_gid = 0;
34 inode->i_blksize = PAGE_CACHE_SIZE;
35 inode->i_blocks = 0;
36 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
38 return inode;
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 * buf, size_t count, loff_t * offset)
50 size_t len = strlen(str);
52 if (!count)
53 return 0;
55 if (*offset > len)
56 return 0;
58 if (count > len - *offset)
59 count = len - *offset;
61 if (copy_to_user(buf, str + *offset, count))
62 return -EFAULT;
64 *offset += count;
66 return count;
70 #define TMPBUFSIZE 50
72 ssize_t oprofilefs_ulong_to_user(unsigned long * val, char * buf, size_t count, loff_t * offset)
74 char tmpbuf[TMPBUFSIZE];
75 size_t maxlen;
77 if (!count)
78 return 0;
80 spin_lock(&oprofilefs_lock);
81 maxlen = snprintf(tmpbuf, TMPBUFSIZE, "%lu\n", *val);
82 spin_unlock(&oprofilefs_lock);
83 if (maxlen > TMPBUFSIZE)
84 maxlen = TMPBUFSIZE;
86 if (*offset > maxlen)
87 return 0;
89 if (count > maxlen - *offset)
90 count = maxlen - *offset;
92 if (copy_to_user(buf, tmpbuf + *offset, count))
93 return -EFAULT;
95 *offset += count;
97 return count;
101 int oprofilefs_ulong_from_user(unsigned long * val, char const * buf, size_t count)
103 char tmpbuf[TMPBUFSIZE];
105 if (!count)
106 return 0;
108 if (count > TMPBUFSIZE - 1)
109 return -EINVAL;
111 memset(tmpbuf, 0x0, TMPBUFSIZE);
113 if (copy_from_user(tmpbuf, buf, count))
114 return -EFAULT;
116 spin_lock(&oprofilefs_lock);
117 *val = simple_strtoul(tmpbuf, NULL, 0);
118 spin_unlock(&oprofilefs_lock);
119 return 0;
123 static ssize_t ulong_read_file(struct file * file, char * buf, size_t count, loff_t * offset)
125 return oprofilefs_ulong_to_user(file->private_data, buf, count, offset);
129 static ssize_t ulong_write_file(struct file * file, char const * buf, size_t count, loff_t * offset)
131 unsigned long * value = file->private_data;
132 int retval;
134 if (*offset)
135 return -EINVAL;
137 retval = oprofilefs_ulong_from_user(value, buf, count);
139 if (retval)
140 return retval;
141 return count;
145 static int default_open(struct inode * inode, struct file * filp)
147 if (inode->u.generic_ip)
148 filp->private_data = inode->u.generic_ip;
149 return 0;
153 static struct file_operations ulong_fops = {
154 .read = ulong_read_file,
155 .write = ulong_write_file,
156 .open = default_open,
160 static struct file_operations ulong_ro_fops = {
161 .read = ulong_read_file,
162 .open = default_open,
166 static struct dentry * __oprofilefs_create_file(struct super_block * sb,
167 struct dentry * root, char const * name, struct file_operations * fops)
169 struct dentry * dentry;
170 struct inode * inode;
171 struct qstr qname;
172 qname.name = name;
173 qname.len = strlen(name);
174 qname.hash = full_name_hash(qname.name, qname.len);
175 dentry = d_alloc(root, &qname);
176 if (!dentry)
177 return 0;
178 inode = oprofilefs_get_inode(sb, S_IFREG | 0644);
179 if (!inode) {
180 dput(dentry);
181 return 0;
183 inode->i_fop = fops;
184 d_add(dentry, inode);
185 return dentry;
189 int oprofilefs_create_ulong(struct super_block * sb, struct dentry * root,
190 char const * name, unsigned long * val)
192 struct dentry * d = __oprofilefs_create_file(sb, root, name, &ulong_fops);
193 if (!d)
194 return -EFAULT;
196 d->d_inode->u.generic_ip = val;
197 return 0;
201 int oprofilefs_create_ro_ulong(struct super_block * sb, struct dentry * root,
202 char const * name, unsigned long * val)
204 struct dentry * d = __oprofilefs_create_file(sb, root, name, &ulong_ro_fops);
205 if (!d)
206 return -EFAULT;
208 d->d_inode->u.generic_ip = val;
209 return 0;
213 static ssize_t atomic_read_file(struct file * file, char * buf, size_t count, loff_t * offset)
215 atomic_t * aval = file->private_data;
216 unsigned long val = atomic_read(aval);
217 return oprofilefs_ulong_to_user(&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);
231 if (!d)
232 return -EFAULT;
234 d->d_inode->u.generic_ip = val;
235 return 0;
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))
243 return -EFAULT;
244 return 0;
248 struct dentry * oprofilefs_mkdir(struct super_block * sb,
249 struct dentry * root, char const * name)
251 struct dentry * dentry;
252 struct inode * inode;
253 struct qstr qname;
254 qname.name = name;
255 qname.len = strlen(name);
256 qname.hash = full_name_hash(qname.name, qname.len);
257 dentry = d_alloc(root, &qname);
258 if (!dentry)
259 return 0;
260 inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
261 if (!inode) {
262 dput(dentry);
263 return 0;
265 inode->i_op = &simple_dir_inode_operations;
266 inode->i_fop = &simple_dir_operations;
267 d_add(dentry, inode);
268 return dentry;
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;
280 sb->s_op = &s_ops;
282 root_inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
283 if (!root_inode)
284 return -ENOMEM;
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);
288 if (!root_dentry) {
289 iput(root_inode);
290 return -ENOMEM;
293 sb->s_root = root_dentry;
295 oprofile_create_files(sb, root_dentry);
297 // FIXME: verify kill_litter_super removes our dentries
298 return 0;
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);