blob: 166cfe7d681de292396163897aa4023d623b81d0 [file] [log] [blame]
Jon Medhurstaaf37a32013-06-11 12:10:56 +01001/**
2 * @file gatorfs.c
3 *
4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
6 *
7 * @author John Levon
8 *
9 * A simple filesystem for configuration and
10 * access of oprofile.
11 */
12
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/fs.h>
16#include <linux/pagemap.h>
17#include <asm/uaccess.h>
18
19#define gatorfs_MAGIC 0x24051020
20#define TMPBUFSIZE 50
21DEFINE_SPINLOCK(gatorfs_lock);
22
23static struct inode *gatorfs_get_inode(struct super_block *sb, int mode)
24{
25 struct inode *inode = new_inode(sb);
26
27 if (inode) {
28#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)
29 inode->i_ino = get_next_ino();
30#endif
31 inode->i_mode = mode;
32 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
33 }
34 return inode;
35}
36
37static const struct super_operations s_ops = {
38 .statfs = simple_statfs,
39 .drop_inode = generic_delete_inode,
40};
41
Jon Medhurst15ce78d2014-04-10 09:02:02 +010042static ssize_t gatorfs_ulong_to_user(unsigned long val, char __user *buf, size_t count, loff_t *offset)
Jon Medhurstaaf37a32013-06-11 12:10:56 +010043{
44 char tmpbuf[TMPBUFSIZE];
45 size_t maxlen = snprintf(tmpbuf, TMPBUFSIZE, "%lu\n", val);
46 if (maxlen > TMPBUFSIZE)
47 maxlen = TMPBUFSIZE;
48 return simple_read_from_buffer(buf, count, offset, tmpbuf, maxlen);
49}
50
Jon Medhurst15ce78d2014-04-10 09:02:02 +010051static ssize_t gatorfs_u64_to_user(u64 val, char __user *buf, size_t count, loff_t *offset)
Jon Medhurstaaf37a32013-06-11 12:10:56 +010052{
53 char tmpbuf[TMPBUFSIZE];
54 size_t maxlen = snprintf(tmpbuf, TMPBUFSIZE, "%llu\n", val);
55 if (maxlen > TMPBUFSIZE)
56 maxlen = TMPBUFSIZE;
57 return simple_read_from_buffer(buf, count, offset, tmpbuf, maxlen);
58}
59
Jon Medhurst15ce78d2014-04-10 09:02:02 +010060static int gatorfs_ulong_from_user(unsigned long *val, char const __user *buf, size_t count)
Jon Medhurstaaf37a32013-06-11 12:10:56 +010061{
62 char tmpbuf[TMPBUFSIZE];
63 unsigned long flags;
64
65 if (!count)
66 return 0;
67
68 if (count > TMPBUFSIZE - 1)
69 return -EINVAL;
70
71 memset(tmpbuf, 0x0, TMPBUFSIZE);
72
73 if (copy_from_user(tmpbuf, buf, count))
74 return -EFAULT;
75
76 spin_lock_irqsave(&gatorfs_lock, flags);
77 *val = simple_strtoul(tmpbuf, NULL, 0);
78 spin_unlock_irqrestore(&gatorfs_lock, flags);
79 return 0;
80}
81
Jon Medhurst15ce78d2014-04-10 09:02:02 +010082static int gatorfs_u64_from_user(u64 *val, char const __user *buf, size_t count)
Jon Medhurstaaf37a32013-06-11 12:10:56 +010083{
84 char tmpbuf[TMPBUFSIZE];
85 unsigned long flags;
86
87 if (!count)
88 return 0;
89
90 if (count > TMPBUFSIZE - 1)
91 return -EINVAL;
92
93 memset(tmpbuf, 0x0, TMPBUFSIZE);
94
95 if (copy_from_user(tmpbuf, buf, count))
96 return -EFAULT;
97
98 spin_lock_irqsave(&gatorfs_lock, flags);
99 *val = simple_strtoull(tmpbuf, NULL, 0);
100 spin_unlock_irqrestore(&gatorfs_lock, flags);
101 return 0;
102}
103
104static ssize_t ulong_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
105{
106 unsigned long *val = file->private_data;
107 return gatorfs_ulong_to_user(*val, buf, count, offset);
108}
109
110static ssize_t u64_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
111{
112 u64 *val = file->private_data;
113 return gatorfs_u64_to_user(*val, buf, count, offset);
114}
115
116static ssize_t ulong_write_file(struct file *file, char const __user *buf, size_t count, loff_t *offset)
117{
118 unsigned long *value = file->private_data;
119 int retval;
120
121 if (*offset)
122 return -EINVAL;
123
124 retval = gatorfs_ulong_from_user(value, buf, count);
125
126 if (retval)
127 return retval;
128 return count;
129}
130
131static ssize_t u64_write_file(struct file *file, char const __user *buf, size_t count, loff_t *offset)
132{
133 u64 *value = file->private_data;
134 int retval;
135
136 if (*offset)
137 return -EINVAL;
138
139 retval = gatorfs_u64_from_user(value, buf, count);
140
141 if (retval)
142 return retval;
143 return count;
144}
145
146static int default_open(struct inode *inode, struct file *filp)
147{
148 if (inode->i_private)
149 filp->private_data = inode->i_private;
150 return 0;
151}
152
153static const struct file_operations ulong_fops = {
154 .read = ulong_read_file,
155 .write = ulong_write_file,
156 .open = default_open,
157};
158
159static const struct file_operations u64_fops = {
160 .read = u64_read_file,
161 .write = u64_write_file,
162 .open = default_open,
163};
164
165static const struct file_operations ulong_ro_fops = {
166 .read = ulong_read_file,
167 .open = default_open,
168};
169
170static const struct file_operations u64_ro_fops = {
171 .read = u64_read_file,
172 .open = default_open,
173};
174
175static struct dentry *__gatorfs_create_file(struct super_block *sb,
176 struct dentry *root,
177 char const *name,
178 const struct file_operations *fops,
179 int perm)
180{
181 struct dentry *dentry;
182 struct inode *inode;
183
184 dentry = d_alloc_name(root, name);
185 if (!dentry)
186 return NULL;
187 inode = gatorfs_get_inode(sb, S_IFREG | perm);
188 if (!inode) {
189 dput(dentry);
190 return NULL;
191 }
192 inode->i_fop = fops;
193 d_add(dentry, inode);
194 return dentry;
195}
196
197int gatorfs_create_ulong(struct super_block *sb, struct dentry *root,
198 char const *name, unsigned long *val)
199{
200 struct dentry *d = __gatorfs_create_file(sb, root, name,
201 &ulong_fops, 0644);
202 if (!d)
203 return -EFAULT;
204
205 d->d_inode->i_private = val;
206 return 0;
207}
208
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100209static int gatorfs_create_u64(struct super_block *sb, struct dentry *root,
210 char const *name, u64 *val)
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100211{
212 struct dentry *d = __gatorfs_create_file(sb, root, name,
213 &u64_fops, 0644);
214 if (!d)
215 return -EFAULT;
216
217 d->d_inode->i_private = val;
218 return 0;
219}
220
221int gatorfs_create_ro_ulong(struct super_block *sb, struct dentry *root,
222 char const *name, unsigned long *val)
223{
224 struct dentry *d = __gatorfs_create_file(sb, root, name,
225 &ulong_ro_fops, 0444);
226 if (!d)
227 return -EFAULT;
228
229 d->d_inode->i_private = val;
230 return 0;
231}
232
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100233static int gatorfs_create_ro_u64(struct super_block *sb, struct dentry *root,
234 char const *name, u64 * val)
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100235{
236 struct dentry *d =
237 __gatorfs_create_file(sb, root, name, &u64_ro_fops, 0444);
238 if (!d)
239 return -EFAULT;
240
241 d->d_inode->i_private = val;
242 return 0;
243}
244
245static ssize_t atomic_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
246{
247 atomic_t *val = file->private_data;
248 return gatorfs_ulong_to_user(atomic_read(val), buf, count, offset);
249}
250
251static const struct file_operations atomic_ro_fops = {
252 .read = atomic_read_file,
253 .open = default_open,
254};
255
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100256static int gatorfs_create_file(struct super_block *sb, struct dentry *root,
257 char const *name, const struct file_operations *fops)
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100258{
259 if (!__gatorfs_create_file(sb, root, name, fops, 0644))
260 return -EFAULT;
261 return 0;
262}
263
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100264static int gatorfs_create_file_perm(struct super_block *sb, struct dentry *root,
265 char const *name,
266 const struct file_operations *fops, int perm)
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100267{
268 if (!__gatorfs_create_file(sb, root, name, fops, perm))
269 return -EFAULT;
270 return 0;
271}
272
273struct dentry *gatorfs_mkdir(struct super_block *sb,
274 struct dentry *root, char const *name)
275{
276 struct dentry *dentry;
277 struct inode *inode;
278
279 dentry = d_alloc_name(root, name);
280 if (!dentry)
281 return NULL;
282 inode = gatorfs_get_inode(sb, S_IFDIR | 0755);
283 if (!inode) {
284 dput(dentry);
285 return NULL;
286 }
287 inode->i_op = &simple_dir_inode_operations;
288 inode->i_fop = &simple_dir_operations;
289 d_add(dentry, inode);
290 return dentry;
291}
292
293static int gatorfs_fill_super(struct super_block *sb, void *data, int silent)
294{
295 struct inode *root_inode;
296 struct dentry *root_dentry;
297
298 sb->s_blocksize = PAGE_CACHE_SIZE;
299 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
300 sb->s_magic = gatorfs_MAGIC;
301 sb->s_op = &s_ops;
302 sb->s_time_gran = 1;
303
304 root_inode = gatorfs_get_inode(sb, S_IFDIR | 0755);
305 if (!root_inode)
306 return -ENOMEM;
307 root_inode->i_op = &simple_dir_inode_operations;
308 root_inode->i_fop = &simple_dir_operations;
309
310#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
311 root_dentry = d_alloc_root(root_inode);
312#else
313 root_dentry = d_make_root(root_inode);
314#endif
315
316 if (!root_dentry) {
317#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
318 iput(root_inode);
319#endif
320 return -ENOMEM;
321 }
322
323 sb->s_root = root_dentry;
324
325 gator_op_create_files(sb, root_dentry);
326
327 return 0;
328}
329
330#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39)
331static int gatorfs_get_sb(struct file_system_type *fs_type,
332 int flags, const char *dev_name, void *data,
333 struct vfsmount *mnt)
334{
335 return get_sb_single(fs_type, flags, data, gatorfs_fill_super, mnt);
336}
337#else
338static struct dentry *gatorfs_mount(struct file_system_type *fs_type,
339 int flags, const char *dev_name, void *data)
340{
341 return mount_nodev(fs_type, flags, data, gatorfs_fill_super);
342}
343#endif
344
345static struct file_system_type gatorfs_type = {
346 .owner = THIS_MODULE,
347 .name = "gatorfs",
348#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39)
349 .get_sb = gatorfs_get_sb,
350#else
351 .mount = gatorfs_mount,
352#endif
353
354 .kill_sb = kill_litter_super,
355};
356
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100357static int __init gatorfs_register(void)
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100358{
359 return register_filesystem(&gatorfs_type);
360}
361
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100362static void gatorfs_unregister(void)
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100363{
364 unregister_filesystem(&gatorfs_type);
365}