blob: fe6f83d547e907fbbf5b374d054187b96d260b0e [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
42ssize_t gatorfs_str_to_user(char const *str, char __user *buf, size_t count, loff_t *offset)
43{
44 return simple_read_from_buffer(buf, count, offset, str, strlen(str));
45}
46
47ssize_t gatorfs_ulong_to_user(unsigned long val, char __user *buf, size_t count, loff_t *offset)
48{
49 char tmpbuf[TMPBUFSIZE];
50 size_t maxlen = snprintf(tmpbuf, TMPBUFSIZE, "%lu\n", val);
51 if (maxlen > TMPBUFSIZE)
52 maxlen = TMPBUFSIZE;
53 return simple_read_from_buffer(buf, count, offset, tmpbuf, maxlen);
54}
55
56ssize_t gatorfs_u64_to_user(u64 val, char __user *buf, size_t count, loff_t *offset)
57{
58 char tmpbuf[TMPBUFSIZE];
59 size_t maxlen = snprintf(tmpbuf, TMPBUFSIZE, "%llu\n", val);
60 if (maxlen > TMPBUFSIZE)
61 maxlen = TMPBUFSIZE;
62 return simple_read_from_buffer(buf, count, offset, tmpbuf, maxlen);
63}
64
65int gatorfs_ulong_from_user(unsigned long *val, char const __user *buf, size_t count)
66{
67 char tmpbuf[TMPBUFSIZE];
68 unsigned long flags;
69
70 if (!count)
71 return 0;
72
73 if (count > TMPBUFSIZE - 1)
74 return -EINVAL;
75
76 memset(tmpbuf, 0x0, TMPBUFSIZE);
77
78 if (copy_from_user(tmpbuf, buf, count))
79 return -EFAULT;
80
81 spin_lock_irqsave(&gatorfs_lock, flags);
82 *val = simple_strtoul(tmpbuf, NULL, 0);
83 spin_unlock_irqrestore(&gatorfs_lock, flags);
84 return 0;
85}
86
87int gatorfs_u64_from_user(u64 *val, char const __user *buf, size_t count)
88{
89 char tmpbuf[TMPBUFSIZE];
90 unsigned long flags;
91
92 if (!count)
93 return 0;
94
95 if (count > TMPBUFSIZE - 1)
96 return -EINVAL;
97
98 memset(tmpbuf, 0x0, TMPBUFSIZE);
99
100 if (copy_from_user(tmpbuf, buf, count))
101 return -EFAULT;
102
103 spin_lock_irqsave(&gatorfs_lock, flags);
104 *val = simple_strtoull(tmpbuf, NULL, 0);
105 spin_unlock_irqrestore(&gatorfs_lock, flags);
106 return 0;
107}
108
109static ssize_t ulong_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
110{
111 unsigned long *val = file->private_data;
112 return gatorfs_ulong_to_user(*val, buf, count, offset);
113}
114
115static ssize_t u64_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
116{
117 u64 *val = file->private_data;
118 return gatorfs_u64_to_user(*val, buf, count, offset);
119}
120
121static ssize_t ulong_write_file(struct file *file, char const __user *buf, size_t count, loff_t *offset)
122{
123 unsigned long *value = file->private_data;
124 int retval;
125
126 if (*offset)
127 return -EINVAL;
128
129 retval = gatorfs_ulong_from_user(value, buf, count);
130
131 if (retval)
132 return retval;
133 return count;
134}
135
136static ssize_t u64_write_file(struct file *file, char const __user *buf, size_t count, loff_t *offset)
137{
138 u64 *value = file->private_data;
139 int retval;
140
141 if (*offset)
142 return -EINVAL;
143
144 retval = gatorfs_u64_from_user(value, buf, count);
145
146 if (retval)
147 return retval;
148 return count;
149}
150
151static int default_open(struct inode *inode, struct file *filp)
152{
153 if (inode->i_private)
154 filp->private_data = inode->i_private;
155 return 0;
156}
157
158static const struct file_operations ulong_fops = {
159 .read = ulong_read_file,
160 .write = ulong_write_file,
161 .open = default_open,
162};
163
164static const struct file_operations u64_fops = {
165 .read = u64_read_file,
166 .write = u64_write_file,
167 .open = default_open,
168};
169
170static const struct file_operations ulong_ro_fops = {
171 .read = ulong_read_file,
172 .open = default_open,
173};
174
175static const struct file_operations u64_ro_fops = {
176 .read = u64_read_file,
177 .open = default_open,
178};
179
180static struct dentry *__gatorfs_create_file(struct super_block *sb,
181 struct dentry *root,
182 char const *name,
183 const struct file_operations *fops,
184 int perm)
185{
186 struct dentry *dentry;
187 struct inode *inode;
188
189 dentry = d_alloc_name(root, name);
190 if (!dentry)
191 return NULL;
192 inode = gatorfs_get_inode(sb, S_IFREG | perm);
193 if (!inode) {
194 dput(dentry);
195 return NULL;
196 }
197 inode->i_fop = fops;
198 d_add(dentry, inode);
199 return dentry;
200}
201
202int gatorfs_create_ulong(struct super_block *sb, struct dentry *root,
203 char const *name, unsigned long *val)
204{
205 struct dentry *d = __gatorfs_create_file(sb, root, name,
206 &ulong_fops, 0644);
207 if (!d)
208 return -EFAULT;
209
210 d->d_inode->i_private = val;
211 return 0;
212}
213
214int gatorfs_create_u64(struct super_block *sb, struct dentry *root,
215 char const *name, u64 *val)
216{
217 struct dentry *d = __gatorfs_create_file(sb, root, name,
218 &u64_fops, 0644);
219 if (!d)
220 return -EFAULT;
221
222 d->d_inode->i_private = val;
223 return 0;
224}
225
226int gatorfs_create_ro_ulong(struct super_block *sb, struct dentry *root,
227 char const *name, unsigned long *val)
228{
229 struct dentry *d = __gatorfs_create_file(sb, root, name,
230 &ulong_ro_fops, 0444);
231 if (!d)
232 return -EFAULT;
233
234 d->d_inode->i_private = val;
235 return 0;
236}
237
238int gatorfs_create_ro_u64(struct super_block *sb, struct dentry *root,
239 char const *name, u64 * val)
240{
241 struct dentry *d =
242 __gatorfs_create_file(sb, root, name, &u64_ro_fops, 0444);
243 if (!d)
244 return -EFAULT;
245
246 d->d_inode->i_private = val;
247 return 0;
248}
249
250static ssize_t atomic_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
251{
252 atomic_t *val = file->private_data;
253 return gatorfs_ulong_to_user(atomic_read(val), buf, count, offset);
254}
255
256static const struct file_operations atomic_ro_fops = {
257 .read = atomic_read_file,
258 .open = default_open,
259};
260
261int gatorfs_create_ro_atomic(struct super_block *sb, struct dentry *root,
262 char const *name, atomic_t *val)
263{
264 struct dentry *d = __gatorfs_create_file(sb, root, name,
265 &atomic_ro_fops, 0444);
266 if (!d)
267 return -EFAULT;
268
269 d->d_inode->i_private = val;
270 return 0;
271}
272
273int gatorfs_create_file(struct super_block *sb, struct dentry *root,
274 char const *name, const struct file_operations *fops)
275{
276 if (!__gatorfs_create_file(sb, root, name, fops, 0644))
277 return -EFAULT;
278 return 0;
279}
280
281int gatorfs_create_file_perm(struct super_block *sb, struct dentry *root,
282 char const *name,
283 const struct file_operations *fops, int perm)
284{
285 if (!__gatorfs_create_file(sb, root, name, fops, perm))
286 return -EFAULT;
287 return 0;
288}
289
290struct dentry *gatorfs_mkdir(struct super_block *sb,
291 struct dentry *root, char const *name)
292{
293 struct dentry *dentry;
294 struct inode *inode;
295
296 dentry = d_alloc_name(root, name);
297 if (!dentry)
298 return NULL;
299 inode = gatorfs_get_inode(sb, S_IFDIR | 0755);
300 if (!inode) {
301 dput(dentry);
302 return NULL;
303 }
304 inode->i_op = &simple_dir_inode_operations;
305 inode->i_fop = &simple_dir_operations;
306 d_add(dentry, inode);
307 return dentry;
308}
309
310static int gatorfs_fill_super(struct super_block *sb, void *data, int silent)
311{
312 struct inode *root_inode;
313 struct dentry *root_dentry;
314
315 sb->s_blocksize = PAGE_CACHE_SIZE;
316 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
317 sb->s_magic = gatorfs_MAGIC;
318 sb->s_op = &s_ops;
319 sb->s_time_gran = 1;
320
321 root_inode = gatorfs_get_inode(sb, S_IFDIR | 0755);
322 if (!root_inode)
323 return -ENOMEM;
324 root_inode->i_op = &simple_dir_inode_operations;
325 root_inode->i_fop = &simple_dir_operations;
326
327#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
328 root_dentry = d_alloc_root(root_inode);
329#else
330 root_dentry = d_make_root(root_inode);
331#endif
332
333 if (!root_dentry) {
334#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
335 iput(root_inode);
336#endif
337 return -ENOMEM;
338 }
339
340 sb->s_root = root_dentry;
341
342 gator_op_create_files(sb, root_dentry);
343
344 return 0;
345}
346
347#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39)
348static int gatorfs_get_sb(struct file_system_type *fs_type,
349 int flags, const char *dev_name, void *data,
350 struct vfsmount *mnt)
351{
352 return get_sb_single(fs_type, flags, data, gatorfs_fill_super, mnt);
353}
354#else
355static struct dentry *gatorfs_mount(struct file_system_type *fs_type,
356 int flags, const char *dev_name, void *data)
357{
358 return mount_nodev(fs_type, flags, data, gatorfs_fill_super);
359}
360#endif
361
362static struct file_system_type gatorfs_type = {
363 .owner = THIS_MODULE,
364 .name = "gatorfs",
365#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39)
366 .get_sb = gatorfs_get_sb,
367#else
368 .mount = gatorfs_mount,
369#endif
370
371 .kill_sb = kill_litter_super,
372};
373
374int __init gatorfs_register(void)
375{
376 return register_filesystem(&gatorfs_type);
377}
378
379void gatorfs_unregister(void)
380{
381 unregister_filesystem(&gatorfs_type);
382}