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