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>
16 #include <linux/pagemap.h>
17 #include <linux/uaccess.h>
19 #define gatorfs_MAGIC 0x24051020
21 DEFINE_SPINLOCK(gatorfs_lock);
23 static struct inode *gatorfs_get_inode(struct super_block *sb, int mode)
25 struct inode *inode = new_inode(sb);
28 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)
29 inode->i_ino = get_next_ino();
32 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
37 static const struct super_operations s_ops = {
38 .statfs = simple_statfs,
39 .drop_inode = generic_delete_inode,
42 static ssize_t gatorfs_ulong_to_user(unsigned long val, char __user *buf, size_t count, loff_t *offset)
44 char tmpbuf[TMPBUFSIZE];
45 size_t maxlen = snprintf(tmpbuf, TMPBUFSIZE, "%lu\n", val);
47 if (maxlen > TMPBUFSIZE)
49 return simple_read_from_buffer(buf, count, offset, tmpbuf, maxlen);
52 static ssize_t gatorfs_u64_to_user(u64 val, char __user *buf, size_t count, loff_t *offset)
54 char tmpbuf[TMPBUFSIZE];
55 size_t maxlen = snprintf(tmpbuf, TMPBUFSIZE, "%llu\n", val);
57 if (maxlen > TMPBUFSIZE)
59 return simple_read_from_buffer(buf, count, offset, tmpbuf, maxlen);
62 static int gatorfs_ulong_from_user(unsigned long *val, char const __user *buf, size_t count)
64 char tmpbuf[TMPBUFSIZE];
70 if (count > TMPBUFSIZE - 1)
73 memset(tmpbuf, 0x0, TMPBUFSIZE);
75 if (copy_from_user(tmpbuf, buf, count))
78 spin_lock_irqsave(&gatorfs_lock, flags);
79 *val = simple_strtoul(tmpbuf, NULL, 0);
80 spin_unlock_irqrestore(&gatorfs_lock, flags);
84 static int gatorfs_u64_from_user(u64 *val, char const __user *buf, size_t count)
86 char tmpbuf[TMPBUFSIZE];
92 if (count > TMPBUFSIZE - 1)
95 memset(tmpbuf, 0x0, TMPBUFSIZE);
97 if (copy_from_user(tmpbuf, buf, count))
100 spin_lock_irqsave(&gatorfs_lock, flags);
101 *val = simple_strtoull(tmpbuf, NULL, 0);
102 spin_unlock_irqrestore(&gatorfs_lock, flags);
106 static ssize_t ulong_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
108 unsigned long *val = file->private_data;
110 return gatorfs_ulong_to_user(*val, buf, count, offset);
113 static ssize_t u64_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
115 u64 *val = file->private_data;
117 return gatorfs_u64_to_user(*val, buf, count, offset);
120 static ssize_t ulong_write_file(struct file *file, char const __user *buf, size_t count, loff_t *offset)
122 unsigned long *value = file->private_data;
128 retval = gatorfs_ulong_from_user(value, buf, count);
135 static ssize_t u64_write_file(struct file *file, char const __user *buf, size_t count, loff_t *offset)
137 u64 *value = file->private_data;
143 retval = gatorfs_u64_from_user(value, buf, count);
150 static int default_open(struct inode *inode, struct file *filp)
152 if (inode->i_private)
153 filp->private_data = inode->i_private;
157 static const struct file_operations ulong_fops = {
158 .read = ulong_read_file,
159 .write = ulong_write_file,
160 .open = default_open,
163 static const struct file_operations u64_fops = {
164 .read = u64_read_file,
165 .write = u64_write_file,
166 .open = default_open,
169 static const struct file_operations ulong_ro_fops = {
170 .read = ulong_read_file,
171 .open = default_open,
174 static const struct file_operations u64_ro_fops = {
175 .read = u64_read_file,
176 .open = default_open,
179 static struct dentry *__gatorfs_create_file(struct super_block *sb,
182 const struct file_operations *fops,
185 struct dentry *dentry;
188 dentry = d_alloc_name(root, name);
191 inode = gatorfs_get_inode(sb, S_IFREG | perm);
197 d_add(dentry, inode);
201 int gatorfs_create_ulong(struct super_block *sb, struct dentry *root,
202 char const *name, unsigned long *val)
204 struct dentry *d = __gatorfs_create_file(sb, root, name,
209 d->d_inode->i_private = val;
213 static int gatorfs_create_u64(struct super_block *sb, struct dentry *root,
214 char const *name, u64 *val)
216 struct dentry *d = __gatorfs_create_file(sb, root, name,
221 d->d_inode->i_private = val;
225 int gatorfs_create_ro_ulong(struct super_block *sb, struct dentry *root,
226 char const *name, unsigned long *val)
228 struct dentry *d = __gatorfs_create_file(sb, root, name,
229 &ulong_ro_fops, 0444);
233 d->d_inode->i_private = val;
237 static int gatorfs_create_ro_u64(struct super_block *sb, struct dentry *root,
238 char const *name, u64 *val)
241 __gatorfs_create_file(sb, root, name, &u64_ro_fops, 0444);
245 d->d_inode->i_private = val;
249 static ssize_t atomic_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
251 atomic_t *val = file->private_data;
253 return gatorfs_ulong_to_user(atomic_read(val), buf, count, offset);
256 static const struct file_operations atomic_ro_fops = {
257 .read = atomic_read_file,
258 .open = default_open,
261 static int gatorfs_create_file(struct super_block *sb, struct dentry *root,
262 char const *name, const struct file_operations *fops)
264 if (!__gatorfs_create_file(sb, root, name, fops, 0644))
269 static int gatorfs_create_file_perm(struct super_block *sb, struct dentry *root,
271 const struct file_operations *fops, int perm)
273 if (!__gatorfs_create_file(sb, root, name, fops, perm))
278 struct dentry *gatorfs_mkdir(struct super_block *sb,
279 struct dentry *root, char const *name)
281 struct dentry *dentry;
284 dentry = d_alloc_name(root, name);
287 inode = gatorfs_get_inode(sb, S_IFDIR | 0755);
292 inode->i_op = &simple_dir_inode_operations;
293 inode->i_fop = &simple_dir_operations;
294 d_add(dentry, inode);
298 static int gatorfs_fill_super(struct super_block *sb, void *data, int silent)
300 struct inode *root_inode;
301 struct dentry *root_dentry;
303 sb->s_blocksize = PAGE_CACHE_SIZE;
304 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
305 sb->s_magic = gatorfs_MAGIC;
309 root_inode = gatorfs_get_inode(sb, S_IFDIR | 0755);
312 root_inode->i_op = &simple_dir_inode_operations;
313 root_inode->i_fop = &simple_dir_operations;
315 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
316 root_dentry = d_alloc_root(root_inode);
318 root_dentry = d_make_root(root_inode);
322 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
328 sb->s_root = root_dentry;
330 gator_op_create_files(sb, root_dentry);
335 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39)
336 static int gatorfs_get_sb(struct file_system_type *fs_type,
337 int flags, const char *dev_name, void *data,
338 struct vfsmount *mnt)
340 return get_sb_single(fs_type, flags, data, gatorfs_fill_super, mnt);
343 static struct dentry *gatorfs_mount(struct file_system_type *fs_type,
344 int flags, const char *dev_name, void *data)
346 return mount_nodev(fs_type, flags, data, gatorfs_fill_super);
350 static struct file_system_type gatorfs_type = {
351 .owner = THIS_MODULE,
353 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39)
354 .get_sb = gatorfs_get_sb,
356 .mount = gatorfs_mount,
359 .kill_sb = kill_litter_super,
362 static int __init gatorfs_register(void)
364 return register_filesystem(&gatorfs_type);
367 static void gatorfs_unregister(void)
369 unregister_filesystem(&gatorfs_type);