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 <asm/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);
46 if (maxlen > TMPBUFSIZE)
48 return simple_read_from_buffer(buf, count, offset, tmpbuf, maxlen);
51 static ssize_t gatorfs_u64_to_user(u64 val, char __user *buf, size_t count, loff_t *offset)
53 char tmpbuf[TMPBUFSIZE];
54 size_t maxlen = snprintf(tmpbuf, TMPBUFSIZE, "%llu\n", val);
55 if (maxlen > TMPBUFSIZE)
57 return simple_read_from_buffer(buf, count, offset, tmpbuf, maxlen);
60 static int gatorfs_ulong_from_user(unsigned long *val, char const __user *buf, size_t count)
62 char tmpbuf[TMPBUFSIZE];
68 if (count > TMPBUFSIZE - 1)
71 memset(tmpbuf, 0x0, TMPBUFSIZE);
73 if (copy_from_user(tmpbuf, buf, count))
76 spin_lock_irqsave(&gatorfs_lock, flags);
77 *val = simple_strtoul(tmpbuf, NULL, 0);
78 spin_unlock_irqrestore(&gatorfs_lock, flags);
82 static int gatorfs_u64_from_user(u64 *val, char const __user *buf, size_t count)
84 char tmpbuf[TMPBUFSIZE];
90 if (count > TMPBUFSIZE - 1)
93 memset(tmpbuf, 0x0, TMPBUFSIZE);
95 if (copy_from_user(tmpbuf, buf, count))
98 spin_lock_irqsave(&gatorfs_lock, flags);
99 *val = simple_strtoull(tmpbuf, NULL, 0);
100 spin_unlock_irqrestore(&gatorfs_lock, flags);
104 static ssize_t ulong_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
106 unsigned long *val = file->private_data;
107 return gatorfs_ulong_to_user(*val, buf, count, offset);
110 static ssize_t u64_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
112 u64 *val = file->private_data;
113 return gatorfs_u64_to_user(*val, buf, count, offset);
116 static ssize_t ulong_write_file(struct file *file, char const __user *buf, size_t count, loff_t *offset)
118 unsigned long *value = file->private_data;
124 retval = gatorfs_ulong_from_user(value, buf, count);
131 static ssize_t u64_write_file(struct file *file, char const __user *buf, size_t count, loff_t *offset)
133 u64 *value = file->private_data;
139 retval = gatorfs_u64_from_user(value, buf, count);
146 static int default_open(struct inode *inode, struct file *filp)
148 if (inode->i_private)
149 filp->private_data = inode->i_private;
153 static const struct file_operations ulong_fops = {
154 .read = ulong_read_file,
155 .write = ulong_write_file,
156 .open = default_open,
159 static const struct file_operations u64_fops = {
160 .read = u64_read_file,
161 .write = u64_write_file,
162 .open = default_open,
165 static const struct file_operations ulong_ro_fops = {
166 .read = ulong_read_file,
167 .open = default_open,
170 static const struct file_operations u64_ro_fops = {
171 .read = u64_read_file,
172 .open = default_open,
175 static struct dentry *__gatorfs_create_file(struct super_block *sb,
178 const struct file_operations *fops,
181 struct dentry *dentry;
184 dentry = d_alloc_name(root, name);
187 inode = gatorfs_get_inode(sb, S_IFREG | perm);
193 d_add(dentry, inode);
197 int gatorfs_create_ulong(struct super_block *sb, struct dentry *root,
198 char const *name, unsigned long *val)
200 struct dentry *d = __gatorfs_create_file(sb, root, name,
205 d->d_inode->i_private = val;
209 static int gatorfs_create_u64(struct super_block *sb, struct dentry *root,
210 char const *name, u64 *val)
212 struct dentry *d = __gatorfs_create_file(sb, root, name,
217 d->d_inode->i_private = val;
221 int gatorfs_create_ro_ulong(struct super_block *sb, struct dentry *root,
222 char const *name, unsigned long *val)
224 struct dentry *d = __gatorfs_create_file(sb, root, name,
225 &ulong_ro_fops, 0444);
229 d->d_inode->i_private = val;
233 static int gatorfs_create_ro_u64(struct super_block *sb, struct dentry *root,
234 char const *name, u64 * val)
237 __gatorfs_create_file(sb, root, name, &u64_ro_fops, 0444);
241 d->d_inode->i_private = val;
245 static ssize_t atomic_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
247 atomic_t *val = file->private_data;
248 return gatorfs_ulong_to_user(atomic_read(val), buf, count, offset);
251 static const struct file_operations atomic_ro_fops = {
252 .read = atomic_read_file,
253 .open = default_open,
256 static int gatorfs_create_file(struct super_block *sb, struct dentry *root,
257 char const *name, const struct file_operations *fops)
259 if (!__gatorfs_create_file(sb, root, name, fops, 0644))
264 static int gatorfs_create_file_perm(struct super_block *sb, struct dentry *root,
266 const struct file_operations *fops, int perm)
268 if (!__gatorfs_create_file(sb, root, name, fops, perm))
273 struct dentry *gatorfs_mkdir(struct super_block *sb,
274 struct dentry *root, char const *name)
276 struct dentry *dentry;
279 dentry = d_alloc_name(root, name);
282 inode = gatorfs_get_inode(sb, S_IFDIR | 0755);
287 inode->i_op = &simple_dir_inode_operations;
288 inode->i_fop = &simple_dir_operations;
289 d_add(dentry, inode);
293 static int gatorfs_fill_super(struct super_block *sb, void *data, int silent)
295 struct inode *root_inode;
296 struct dentry *root_dentry;
298 sb->s_blocksize = PAGE_CACHE_SIZE;
299 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
300 sb->s_magic = gatorfs_MAGIC;
304 root_inode = gatorfs_get_inode(sb, S_IFDIR | 0755);
307 root_inode->i_op = &simple_dir_inode_operations;
308 root_inode->i_fop = &simple_dir_operations;
310 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
311 root_dentry = d_alloc_root(root_inode);
313 root_dentry = d_make_root(root_inode);
317 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
323 sb->s_root = root_dentry;
325 gator_op_create_files(sb, root_dentry);
330 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39)
331 static int gatorfs_get_sb(struct file_system_type *fs_type,
332 int flags, const char *dev_name, void *data,
333 struct vfsmount *mnt)
335 return get_sb_single(fs_type, flags, data, gatorfs_fill_super, mnt);
338 static struct dentry *gatorfs_mount(struct file_system_type *fs_type,
339 int flags, const char *dev_name, void *data)
341 return mount_nodev(fs_type, flags, data, gatorfs_fill_super);
345 static struct file_system_type gatorfs_type = {
346 .owner = THIS_MODULE,
348 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39)
349 .get_sb = gatorfs_get_sb,
351 .mount = gatorfs_mount,
354 .kill_sb = kill_litter_super,
357 static int __init gatorfs_register(void)
359 return register_filesystem(&gatorfs_type);
362 static void gatorfs_unregister(void)
364 unregister_filesystem(&gatorfs_type);