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