Pull video into test branch
[linux-drm-fsl-dcu.git] / arch / s390 / hypfs / inode.c
1 /*
2  *  arch/s390/hypfs/inode.c
3  *    Hypervisor filesystem for Linux on s390.
4  *
5  *    Copyright (C) IBM Corp. 2006
6  *    Author(s): Michael Holzheu <holzheu@de.ibm.com>
7  */
8
9 #include <linux/types.h>
10 #include <linux/errno.h>
11 #include <linux/fs.h>
12 #include <linux/namei.h>
13 #include <linux/vfs.h>
14 #include <linux/pagemap.h>
15 #include <linux/gfp.h>
16 #include <linux/time.h>
17 #include <linux/parser.h>
18 #include <linux/sysfs.h>
19 #include <linux/module.h>
20 #include <asm/ebcdic.h>
21 #include "hypfs.h"
22 #include "hypfs_diag.h"
23
24 #define HYPFS_MAGIC 0x687970    /* ASCII 'hyp' */
25 #define TMP_SIZE 64             /* size of temporary buffers */
26
27 static struct dentry *hypfs_create_update_file(struct super_block *sb,
28                                                struct dentry *dir);
29
30 struct hypfs_sb_info {
31         uid_t uid;                      /* uid used for files and dirs */
32         gid_t gid;                      /* gid used for files and dirs */
33         struct dentry *update_file;     /* file to trigger update */
34         time_t last_update;             /* last update time in secs since 1970 */
35         struct mutex lock;              /* lock to protect update process */
36 };
37
38 static struct file_operations hypfs_file_ops;
39 static struct file_system_type hypfs_type;
40 static struct super_operations hypfs_s_ops;
41
42 /* start of list of all dentries, which have to be deleted on update */
43 static struct dentry *hypfs_last_dentry;
44
45 static void hypfs_update_update(struct super_block *sb)
46 {
47         struct hypfs_sb_info *sb_info = sb->s_fs_info;
48         struct inode *inode = sb_info->update_file->d_inode;
49
50         sb_info->last_update = get_seconds();
51         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
52 }
53
54 /* directory tree removal functions */
55
56 static void hypfs_add_dentry(struct dentry *dentry)
57 {
58         dentry->d_fsdata = hypfs_last_dentry;
59         hypfs_last_dentry = dentry;
60 }
61
62 static void hypfs_remove(struct dentry *dentry)
63 {
64         struct dentry *parent;
65
66         parent = dentry->d_parent;
67         if (S_ISDIR(dentry->d_inode->i_mode))
68                 simple_rmdir(parent->d_inode, dentry);
69         else
70                 simple_unlink(parent->d_inode, dentry);
71         d_delete(dentry);
72         dput(dentry);
73 }
74
75 static void hypfs_delete_tree(struct dentry *root)
76 {
77         while (hypfs_last_dentry) {
78                 struct dentry *next_dentry;
79                 next_dentry = hypfs_last_dentry->d_fsdata;
80                 hypfs_remove(hypfs_last_dentry);
81                 hypfs_last_dentry = next_dentry;
82         }
83 }
84
85 static struct inode *hypfs_make_inode(struct super_block *sb, int mode)
86 {
87         struct inode *ret = new_inode(sb);
88
89         if (ret) {
90                 struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
91                 ret->i_mode = mode;
92                 ret->i_uid = hypfs_info->uid;
93                 ret->i_gid = hypfs_info->gid;
94                 ret->i_blocks = 0;
95                 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
96                 if (mode & S_IFDIR)
97                         ret->i_nlink = 2;
98                 else
99                         ret->i_nlink = 1;
100         }
101         return ret;
102 }
103
104 static void hypfs_drop_inode(struct inode *inode)
105 {
106         kfree(inode->i_private);
107         generic_delete_inode(inode);
108 }
109
110 static int hypfs_open(struct inode *inode, struct file *filp)
111 {
112         char *data = filp->f_path.dentry->d_inode->i_private;
113         struct hypfs_sb_info *fs_info;
114
115         if (filp->f_mode & FMODE_WRITE) {
116                 if (!(inode->i_mode & S_IWUGO))
117                         return -EACCES;
118         }
119         if (filp->f_mode & FMODE_READ) {
120                 if (!(inode->i_mode & S_IRUGO))
121                         return -EACCES;
122         }
123
124         fs_info = inode->i_sb->s_fs_info;
125         if(data) {
126                 mutex_lock(&fs_info->lock);
127                 filp->private_data = kstrdup(data, GFP_KERNEL);
128                 if (!filp->private_data) {
129                         mutex_unlock(&fs_info->lock);
130                         return -ENOMEM;
131                 }
132                 mutex_unlock(&fs_info->lock);
133         }
134         return 0;
135 }
136
137 static ssize_t hypfs_aio_read(struct kiocb *iocb, const struct iovec *iov,
138                               unsigned long nr_segs, loff_t offset)
139 {
140         char *data;
141         size_t len;
142         struct file *filp = iocb->ki_filp;
143         /* XXX: temporary */
144         char __user *buf = iov[0].iov_base;
145         size_t count = iov[0].iov_len;
146
147         if (nr_segs != 1) {
148                 count = -EINVAL;
149                 goto out;
150         }
151
152         data = filp->private_data;
153         len = strlen(data);
154         if (offset > len) {
155                 count = 0;
156                 goto out;
157         }
158         if (count > len - offset)
159                 count = len - offset;
160         if (copy_to_user(buf, data + offset, count)) {
161                 count = -EFAULT;
162                 goto out;
163         }
164         iocb->ki_pos += count;
165         file_accessed(filp);
166 out:
167         return count;
168 }
169 static ssize_t hypfs_aio_write(struct kiocb *iocb, const struct iovec *iov,
170                               unsigned long nr_segs, loff_t offset)
171 {
172         int rc;
173         struct super_block *sb;
174         struct hypfs_sb_info *fs_info;
175         size_t count = iov_length(iov, nr_segs);
176
177         sb = iocb->ki_filp->f_path.dentry->d_inode->i_sb;
178         fs_info = sb->s_fs_info;
179         /*
180          * Currently we only allow one update per second for two reasons:
181          * 1. diag 204 is VERY expensive
182          * 2. If several processes do updates in parallel and then read the
183          *    hypfs data, the likelihood of collisions is reduced, if we restrict
184          *    the minimum update interval. A collision occurs, if during the
185          *    data gathering of one process another process triggers an update
186          *    If the first process wants to ensure consistent data, it has
187          *    to restart data collection in this case.
188          */
189         mutex_lock(&fs_info->lock);
190         if (fs_info->last_update == get_seconds()) {
191                 rc = -EBUSY;
192                 goto out;
193         }
194         hypfs_delete_tree(sb->s_root);
195         rc = hypfs_diag_create_files(sb, sb->s_root);
196         if (rc) {
197                 printk(KERN_ERR "hypfs: Update failed\n");
198                 hypfs_delete_tree(sb->s_root);
199                 goto out;
200         }
201         hypfs_update_update(sb);
202         rc = count;
203 out:
204         mutex_unlock(&fs_info->lock);
205         return rc;
206 }
207
208 static int hypfs_release(struct inode *inode, struct file *filp)
209 {
210         kfree(filp->private_data);
211         return 0;
212 }
213
214 enum { opt_uid, opt_gid, opt_err };
215
216 static match_table_t hypfs_tokens = {
217         {opt_uid, "uid=%u"},
218         {opt_gid, "gid=%u"},
219         {opt_err, NULL}
220 };
221
222 static int hypfs_parse_options(char *options, struct super_block *sb)
223 {
224         char *str;
225         substring_t args[MAX_OPT_ARGS];
226
227         if (!options)
228                 return 0;
229         while ((str = strsep(&options, ",")) != NULL) {
230                 int token, option;
231                 struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
232
233                 if (!*str)
234                         continue;
235                 token = match_token(str, hypfs_tokens, args);
236                 switch (token) {
237                 case opt_uid:
238                         if (match_int(&args[0], &option))
239                                 return -EINVAL;
240                         hypfs_info->uid = option;
241                         break;
242                 case opt_gid:
243                         if (match_int(&args[0], &option))
244                                 return -EINVAL;
245                         hypfs_info->gid = option;
246                         break;
247                 case opt_err:
248                 default:
249                         printk(KERN_ERR "hypfs: Unrecognized mount option "
250                                "\"%s\" or missing value\n", str);
251                         return -EINVAL;
252                 }
253         }
254         return 0;
255 }
256
257 static int hypfs_fill_super(struct super_block *sb, void *data, int silent)
258 {
259         struct inode *root_inode;
260         struct dentry *root_dentry;
261         int rc = 0;
262         struct hypfs_sb_info *sbi;
263
264         sbi = kzalloc(sizeof(struct hypfs_sb_info), GFP_KERNEL);
265         if (!sbi)
266                 return -ENOMEM;
267         mutex_init(&sbi->lock);
268         sbi->uid = current->uid;
269         sbi->gid = current->gid;
270         sb->s_fs_info = sbi;
271         sb->s_blocksize = PAGE_CACHE_SIZE;
272         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
273         sb->s_magic = HYPFS_MAGIC;
274         sb->s_op = &hypfs_s_ops;
275         if (hypfs_parse_options(data, sb)) {
276                 rc = -EINVAL;
277                 goto err_alloc;
278         }
279         root_inode = hypfs_make_inode(sb, S_IFDIR | 0755);
280         if (!root_inode) {
281                 rc = -ENOMEM;
282                 goto err_alloc;
283         }
284         root_inode->i_op = &simple_dir_inode_operations;
285         root_inode->i_fop = &simple_dir_operations;
286         root_dentry = d_alloc_root(root_inode);
287         if (!root_dentry) {
288                 iput(root_inode);
289                 rc = -ENOMEM;
290                 goto err_alloc;
291         }
292         rc = hypfs_diag_create_files(sb, root_dentry);
293         if (rc)
294                 goto err_tree;
295         sbi->update_file = hypfs_create_update_file(sb, root_dentry);
296         if (IS_ERR(sbi->update_file)) {
297                 rc = PTR_ERR(sbi->update_file);
298                 goto err_tree;
299         }
300         hypfs_update_update(sb);
301         sb->s_root = root_dentry;
302         return 0;
303
304 err_tree:
305         hypfs_delete_tree(root_dentry);
306         d_genocide(root_dentry);
307         dput(root_dentry);
308 err_alloc:
309         kfree(sbi);
310         return rc;
311 }
312
313 static int hypfs_get_super(struct file_system_type *fst, int flags,
314                         const char *devname, void *data, struct vfsmount *mnt)
315 {
316         return get_sb_single(fst, flags, data, hypfs_fill_super, mnt);
317 }
318
319 static void hypfs_kill_super(struct super_block *sb)
320 {
321         struct hypfs_sb_info *sb_info = sb->s_fs_info;
322
323         if (sb->s_root) {
324                 hypfs_delete_tree(sb->s_root);
325                 hypfs_remove(sb_info->update_file);
326                 kfree(sb->s_fs_info);
327                 sb->s_fs_info = NULL;
328         }
329         kill_litter_super(sb);
330 }
331
332 static struct dentry *hypfs_create_file(struct super_block *sb,
333                                         struct dentry *parent, const char *name,
334                                         char *data, mode_t mode)
335 {
336         struct dentry *dentry;
337         struct inode *inode;
338         struct qstr qname;
339
340         qname.name = name;
341         qname.len = strlen(name);
342         qname.hash = full_name_hash(name, qname.len);
343         dentry = lookup_one_len(name, parent, strlen(name));
344         if (IS_ERR(dentry))
345                 return ERR_PTR(-ENOMEM);
346         inode = hypfs_make_inode(sb, mode);
347         if (!inode) {
348                 dput(dentry);
349                 return ERR_PTR(-ENOMEM);
350         }
351         if (mode & S_IFREG) {
352                 inode->i_fop = &hypfs_file_ops;
353                 if (data)
354                         inode->i_size = strlen(data);
355                 else
356                         inode->i_size = 0;
357         } else if (mode & S_IFDIR) {
358                 inode->i_op = &simple_dir_inode_operations;
359                 inode->i_fop = &simple_dir_operations;
360                 parent->d_inode->i_nlink++;
361         } else
362                 BUG();
363         inode->i_private = data;
364         d_instantiate(dentry, inode);
365         dget(dentry);
366         return dentry;
367 }
368
369 struct dentry *hypfs_mkdir(struct super_block *sb, struct dentry *parent,
370                            const char *name)
371 {
372         struct dentry *dentry;
373
374         dentry = hypfs_create_file(sb, parent, name, NULL, S_IFDIR | DIR_MODE);
375         if (IS_ERR(dentry))
376                 return dentry;
377         hypfs_add_dentry(dentry);
378         parent->d_inode->i_nlink++;
379         return dentry;
380 }
381
382 static struct dentry *hypfs_create_update_file(struct super_block *sb,
383                                                struct dentry *dir)
384 {
385         struct dentry *dentry;
386
387         dentry = hypfs_create_file(sb, dir, "update", NULL,
388                                    S_IFREG | UPDATE_FILE_MODE);
389         /*
390          * We do not put the update file on the 'delete' list with
391          * hypfs_add_dentry(), since it should not be removed when the tree
392          * is updated.
393          */
394         return dentry;
395 }
396
397 struct dentry *hypfs_create_u64(struct super_block *sb, struct dentry *dir,
398                                 const char *name, __u64 value)
399 {
400         char *buffer;
401         char tmp[TMP_SIZE];
402         struct dentry *dentry;
403
404         snprintf(tmp, TMP_SIZE, "%lld\n", (unsigned long long int)value);
405         buffer = kstrdup(tmp, GFP_KERNEL);
406         if (!buffer)
407                 return ERR_PTR(-ENOMEM);
408         dentry =
409             hypfs_create_file(sb, dir, name, buffer, S_IFREG | REG_FILE_MODE);
410         if (IS_ERR(dentry)) {
411                 kfree(buffer);
412                 return ERR_PTR(-ENOMEM);
413         }
414         hypfs_add_dentry(dentry);
415         return dentry;
416 }
417
418 struct dentry *hypfs_create_str(struct super_block *sb, struct dentry *dir,
419                                 const char *name, char *string)
420 {
421         char *buffer;
422         struct dentry *dentry;
423
424         buffer = kmalloc(strlen(string) + 2, GFP_KERNEL);
425         if (!buffer)
426                 return ERR_PTR(-ENOMEM);
427         sprintf(buffer, "%s\n", string);
428         dentry =
429             hypfs_create_file(sb, dir, name, buffer, S_IFREG | REG_FILE_MODE);
430         if (IS_ERR(dentry)) {
431                 kfree(buffer);
432                 return ERR_PTR(-ENOMEM);
433         }
434         hypfs_add_dentry(dentry);
435         return dentry;
436 }
437
438 static struct file_operations hypfs_file_ops = {
439         .open           = hypfs_open,
440         .release        = hypfs_release,
441         .read           = do_sync_read,
442         .write          = do_sync_write,
443         .aio_read       = hypfs_aio_read,
444         .aio_write      = hypfs_aio_write,
445 };
446
447 static struct file_system_type hypfs_type = {
448         .owner          = THIS_MODULE,
449         .name           = "s390_hypfs",
450         .get_sb         = hypfs_get_super,
451         .kill_sb        = hypfs_kill_super
452 };
453
454 static struct super_operations hypfs_s_ops = {
455         .statfs         = simple_statfs,
456         .drop_inode     = hypfs_drop_inode,
457 };
458
459 static decl_subsys(s390, NULL, NULL);
460
461 static int __init hypfs_init(void)
462 {
463         int rc;
464
465         if (MACHINE_IS_VM)
466                 return -ENODATA;
467         if (hypfs_diag_init()) {
468                 rc = -ENODATA;
469                 goto fail_diag;
470         }
471         kset_set_kset_s(&s390_subsys, hypervisor_subsys);
472         rc = subsystem_register(&s390_subsys);
473         if (rc)
474                 goto fail_sysfs;
475         rc = register_filesystem(&hypfs_type);
476         if (rc)
477                 goto fail_filesystem;
478         return 0;
479
480 fail_filesystem:
481         subsystem_unregister(&s390_subsys);
482 fail_sysfs:
483         hypfs_diag_exit();
484 fail_diag:
485         printk(KERN_ERR "hypfs: Initialization failed with rc = %i.\n", rc);
486         return rc;
487 }
488
489 static void __exit hypfs_exit(void)
490 {
491         hypfs_diag_exit();
492         unregister_filesystem(&hypfs_type);
493         subsystem_unregister(&s390_subsys);
494 }
495
496 module_init(hypfs_init)
497 module_exit(hypfs_exit)
498
499 MODULE_LICENSE("GPL");
500 MODULE_AUTHOR("Michael Holzheu <holzheu@de.ibm.com>");
501 MODULE_DESCRIPTION("s390 Hypervisor Filesystem");