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