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 ssize_t gatorfs_str_to_user(char const *str, char __user *buf, size_t count, loff_t *offset)
44 return simple_read_from_buffer(buf, count, offset, str, strlen(str));
47 ssize_t gatorfs_ulong_to_user(unsigned long val, char __user *buf, size_t count, loff_t *offset)
49 char tmpbuf[TMPBUFSIZE];
50 size_t maxlen = snprintf(tmpbuf, TMPBUFSIZE, "%lu\n", val);
51 if (maxlen > TMPBUFSIZE)
53 return simple_read_from_buffer(buf, count, offset, tmpbuf, maxlen);
56 ssize_t gatorfs_u64_to_user(u64 val, char __user *buf, size_t count, loff_t *offset)
58 char tmpbuf[TMPBUFSIZE];
59 size_t maxlen = snprintf(tmpbuf, TMPBUFSIZE, "%llu\n", val);
60 if (maxlen > TMPBUFSIZE)
62 return simple_read_from_buffer(buf, count, offset, tmpbuf, maxlen);
65 int gatorfs_ulong_from_user(unsigned long *val, char const __user *buf, size_t count)
67 char tmpbuf[TMPBUFSIZE];
73 if (count > TMPBUFSIZE - 1)
76 memset(tmpbuf, 0x0, TMPBUFSIZE);
78 if (copy_from_user(tmpbuf, buf, count))
81 spin_lock_irqsave(&gatorfs_lock, flags);
82 *val = simple_strtoul(tmpbuf, NULL, 0);
83 spin_unlock_irqrestore(&gatorfs_lock, flags);
87 int gatorfs_u64_from_user(u64 *val, char const __user *buf, size_t count)
89 char tmpbuf[TMPBUFSIZE];
95 if (count > TMPBUFSIZE - 1)
98 memset(tmpbuf, 0x0, TMPBUFSIZE);
100 if (copy_from_user(tmpbuf, buf, count))
103 spin_lock_irqsave(&gatorfs_lock, flags);
104 *val = simple_strtoull(tmpbuf, NULL, 0);
105 spin_unlock_irqrestore(&gatorfs_lock, flags);
109 static ssize_t ulong_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
111 unsigned long *val = file->private_data;
112 return gatorfs_ulong_to_user(*val, buf, count, offset);
115 static ssize_t u64_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
117 u64 *val = file->private_data;
118 return gatorfs_u64_to_user(*val, buf, count, offset);
121 static ssize_t ulong_write_file(struct file *file, char const __user *buf, size_t count, loff_t *offset)
123 unsigned long *value = file->private_data;
129 retval = gatorfs_ulong_from_user(value, buf, count);
136 static ssize_t u64_write_file(struct file *file, char const __user *buf, size_t count, loff_t *offset)
138 u64 *value = file->private_data;
144 retval = gatorfs_u64_from_user(value, buf, count);
151 static int default_open(struct inode *inode, struct file *filp)
153 if (inode->i_private)
154 filp->private_data = inode->i_private;
158 static const struct file_operations ulong_fops = {
159 .read = ulong_read_file,
160 .write = ulong_write_file,
161 .open = default_open,
164 static const struct file_operations u64_fops = {
165 .read = u64_read_file,
166 .write = u64_write_file,
167 .open = default_open,
170 static const struct file_operations ulong_ro_fops = {
171 .read = ulong_read_file,
172 .open = default_open,
175 static const struct file_operations u64_ro_fops = {
176 .read = u64_read_file,
177 .open = default_open,
180 static struct dentry *__gatorfs_create_file(struct super_block *sb,
183 const struct file_operations *fops,
186 struct dentry *dentry;
189 dentry = d_alloc_name(root, name);
192 inode = gatorfs_get_inode(sb, S_IFREG | perm);
198 d_add(dentry, inode);
202 int gatorfs_create_ulong(struct super_block *sb, struct dentry *root,
203 char const *name, unsigned long *val)
205 struct dentry *d = __gatorfs_create_file(sb, root, name,
210 d->d_inode->i_private = val;
214 int gatorfs_create_u64(struct super_block *sb, struct dentry *root,
215 char const *name, u64 *val)
217 struct dentry *d = __gatorfs_create_file(sb, root, name,
222 d->d_inode->i_private = val;
226 int gatorfs_create_ro_ulong(struct super_block *sb, struct dentry *root,
227 char const *name, unsigned long *val)
229 struct dentry *d = __gatorfs_create_file(sb, root, name,
230 &ulong_ro_fops, 0444);
234 d->d_inode->i_private = val;
238 int gatorfs_create_ro_u64(struct super_block *sb, struct dentry *root,
239 char const *name, u64 * val)
242 __gatorfs_create_file(sb, root, name, &u64_ro_fops, 0444);
246 d->d_inode->i_private = val;
250 static ssize_t atomic_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
252 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 int gatorfs_create_ro_atomic(struct super_block *sb, struct dentry *root,
262 char const *name, atomic_t *val)
264 struct dentry *d = __gatorfs_create_file(sb, root, name,
265 &atomic_ro_fops, 0444);
269 d->d_inode->i_private = val;
273 int gatorfs_create_file(struct super_block *sb, struct dentry *root,
274 char const *name, const struct file_operations *fops)
276 if (!__gatorfs_create_file(sb, root, name, fops, 0644))
281 int gatorfs_create_file_perm(struct super_block *sb, struct dentry *root,
283 const struct file_operations *fops, int perm)
285 if (!__gatorfs_create_file(sb, root, name, fops, perm))
290 struct dentry *gatorfs_mkdir(struct super_block *sb,
291 struct dentry *root, char const *name)
293 struct dentry *dentry;
296 dentry = d_alloc_name(root, name);
299 inode = gatorfs_get_inode(sb, S_IFDIR | 0755);
304 inode->i_op = &simple_dir_inode_operations;
305 inode->i_fop = &simple_dir_operations;
306 d_add(dentry, inode);
310 static int gatorfs_fill_super(struct super_block *sb, void *data, int silent)
312 struct inode *root_inode;
313 struct dentry *root_dentry;
315 sb->s_blocksize = PAGE_CACHE_SIZE;
316 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
317 sb->s_magic = gatorfs_MAGIC;
321 root_inode = gatorfs_get_inode(sb, S_IFDIR | 0755);
324 root_inode->i_op = &simple_dir_inode_operations;
325 root_inode->i_fop = &simple_dir_operations;
327 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
328 root_dentry = d_alloc_root(root_inode);
330 root_dentry = d_make_root(root_inode);
334 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
340 sb->s_root = root_dentry;
342 gator_op_create_files(sb, root_dentry);
347 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39)
348 static int gatorfs_get_sb(struct file_system_type *fs_type,
349 int flags, const char *dev_name, void *data,
350 struct vfsmount *mnt)
352 return get_sb_single(fs_type, flags, data, gatorfs_fill_super, mnt);
355 static struct dentry *gatorfs_mount(struct file_system_type *fs_type,
356 int flags, const char *dev_name, void *data)
358 return mount_nodev(fs_type, flags, data, gatorfs_fill_super);
362 static struct file_system_type gatorfs_type = {
363 .owner = THIS_MODULE,
365 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39)
366 .get_sb = gatorfs_get_sb,
368 .mount = gatorfs_mount,
371 .kill_sb = kill_litter_super,
374 int __init gatorfs_register(void)
376 return register_filesystem(&gatorfs_type);
379 void gatorfs_unregister(void)
381 unregister_filesystem(&gatorfs_type);