zero i_uid/i_gid on inode allocation
[safe/jmp/linux-2.6] / arch / s390 / hypfs / inode.c
index 18c0919..5a805df 100644 (file)
@@ -1,11 +1,14 @@
 /*
- *  fs/hypfs/inode.c
+ *  arch/s390/hypfs/inode.c
  *    Hypervisor filesystem for Linux on s390.
  *
- *    Copyright (C) IBM Corp. 2006
+ *    Copyright IBM Corp. 2006, 2008
  *    Author(s): Michael Holzheu <holzheu@de.ibm.com>
  */
 
+#define KMSG_COMPONENT "hypfs"
+#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
+
 #include <linux/types.h>
 #include <linux/errno.h>
 #include <linux/fs.h>
 #include <linux/parser.h>
 #include <linux/sysfs.h>
 #include <linux/module.h>
+#include <linux/seq_file.h>
+#include <linux/mount.h>
 #include <asm/ebcdic.h>
 #include "hypfs.h"
-#include "hypfs_diag.h"
 
 #define HYPFS_MAGIC 0x687970   /* ASCII 'hyp' */
 #define TMP_SIZE 64            /* size of temporary buffers */
@@ -35,7 +39,7 @@ struct hypfs_sb_info {
        struct mutex lock;              /* lock to protect update process */
 };
 
-static struct file_operations hypfs_file_ops;
+static const struct file_operations hypfs_file_ops;
 static struct file_system_type hypfs_type;
 static struct super_operations hypfs_s_ops;
 
@@ -59,17 +63,28 @@ static void hypfs_add_dentry(struct dentry *dentry)
        hypfs_last_dentry = dentry;
 }
 
+static inline int hypfs_positive(struct dentry *dentry)
+{
+       return dentry->d_inode && !d_unhashed(dentry);
+}
+
 static void hypfs_remove(struct dentry *dentry)
 {
        struct dentry *parent;
 
        parent = dentry->d_parent;
-       if (S_ISDIR(dentry->d_inode->i_mode))
-               simple_rmdir(parent->d_inode, dentry);
-       else
-               simple_unlink(parent->d_inode, dentry);
+       if (!parent || !parent->d_inode)
+               return;
+       mutex_lock(&parent->d_inode->i_mutex);
+       if (hypfs_positive(dentry)) {
+               if (S_ISDIR(dentry->d_inode->i_mode))
+                       simple_rmdir(parent->d_inode, dentry);
+               else
+                       simple_unlink(parent->d_inode, dentry);
+       }
        d_delete(dentry);
        dput(dentry);
+       mutex_unlock(&parent->d_inode->i_mutex);
 }
 
 static void hypfs_delete_tree(struct dentry *root)
@@ -91,8 +106,6 @@ static struct inode *hypfs_make_inode(struct super_block *sb, int mode)
                ret->i_mode = mode;
                ret->i_uid = hypfs_info->uid;
                ret->i_gid = hypfs_info->gid;
-               ret->i_blksize = PAGE_CACHE_SIZE;
-               ret->i_blocks = 0;
                ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
                if (mode & S_IFDIR)
                        ret->i_nlink = 2;
@@ -104,13 +117,13 @@ static struct inode *hypfs_make_inode(struct super_block *sb, int mode)
 
 static void hypfs_drop_inode(struct inode *inode)
 {
-       kfree(inode->u.generic_ip);
+       kfree(inode->i_private);
        generic_delete_inode(inode);
 }
 
 static int hypfs_open(struct inode *inode, struct file *filp)
 {
-       char *data = filp->f_dentry->d_inode->u.generic_ip;
+       char *data = filp->f_path.dentry->d_inode->i_private;
        struct hypfs_sb_info *fs_info;
 
        if (filp->f_mode & FMODE_WRITE) {
@@ -135,38 +148,38 @@ static int hypfs_open(struct inode *inode, struct file *filp)
        return 0;
 }
 
-static ssize_t hypfs_aio_read(struct kiocb *iocb, __user char *buf,
-                             size_t count, loff_t offset)
+static ssize_t hypfs_aio_read(struct kiocb *iocb, const struct iovec *iov,
+                             unsigned long nr_segs, loff_t offset)
 {
        char *data;
-       size_t len;
+       ssize_t ret;
        struct file *filp = iocb->ki_filp;
+       /* XXX: temporary */
+       char __user *buf = iov[0].iov_base;
+       size_t count = iov[0].iov_len;
+
+       if (nr_segs != 1)
+               return -EINVAL;
 
        data = filp->private_data;
-       len = strlen(data);
-       if (offset > len) {
-               count = 0;
-               goto out;
-       }
-       if (count > len - offset)
-               count = len - offset;
-       if (copy_to_user(buf, data + offset, count)) {
-               count = -EFAULT;
-               goto out;
-       }
-       iocb->ki_pos += count;
+       ret = simple_read_from_buffer(buf, count, &offset, data, strlen(data));
+       if (ret <= 0)
+               return ret;
+
+       iocb->ki_pos += ret;
        file_accessed(filp);
-out:
-       return count;
+
+       return ret;
 }
-static ssize_t hypfs_aio_write(struct kiocb *iocb, const char __user *buf,
-                              size_t count, loff_t pos)
+static ssize_t hypfs_aio_write(struct kiocb *iocb, const struct iovec *iov,
+                             unsigned long nr_segs, loff_t offset)
 {
        int rc;
        struct super_block *sb;
        struct hypfs_sb_info *fs_info;
+       size_t count = iov_length(iov, nr_segs);
 
-       sb = iocb->ki_filp->f_dentry->d_inode->i_sb;
+       sb = iocb->ki_filp->f_path.dentry->d_inode->i_sb;
        fs_info = sb->s_fs_info;
        /*
         * Currently we only allow one update per second for two reasons:
@@ -184,9 +197,12 @@ static ssize_t hypfs_aio_write(struct kiocb *iocb, const char __user *buf,
                goto out;
        }
        hypfs_delete_tree(sb->s_root);
-       rc = hypfs_diag_create_files(sb, sb->s_root);
+       if (MACHINE_IS_VM)
+               rc = hypfs_vm_create_files(sb, sb->s_root);
+       else
+               rc = hypfs_diag_create_files(sb, sb->s_root);
        if (rc) {
-               printk(KERN_ERR "hypfs: Update failed\n");
+               pr_err("Updating the hypfs tree failed\n");
                hypfs_delete_tree(sb->s_root);
                goto out;
        }
@@ -205,7 +221,7 @@ static int hypfs_release(struct inode *inode, struct file *filp)
 
 enum { opt_uid, opt_gid, opt_err };
 
-static match_table_t hypfs_tokens = {
+static const match_table_t hypfs_tokens = {
        {opt_uid, "uid=%u"},
        {opt_gid, "gid=%u"},
        {opt_err, NULL}
@@ -238,14 +254,22 @@ static int hypfs_parse_options(char *options, struct super_block *sb)
                        break;
                case opt_err:
                default:
-                       printk(KERN_ERR "hypfs: Unrecognized mount option "
-                              "\"%s\" or missing value\n", str);
+                       pr_err("%s is not a valid mount option\n", str);
                        return -EINVAL;
                }
        }
        return 0;
 }
 
+static int hypfs_show_options(struct seq_file *s, struct vfsmount *mnt)
+{
+       struct hypfs_sb_info *hypfs_info = mnt->mnt_sb->s_fs_info;
+
+       seq_printf(s, ",uid=%u", hypfs_info->uid);
+       seq_printf(s, ",gid=%u", hypfs_info->gid);
+       return 0;
+}
+
 static int hypfs_fill_super(struct super_block *sb, void *data, int silent)
 {
        struct inode *root_inode;
@@ -257,8 +281,8 @@ static int hypfs_fill_super(struct super_block *sb, void *data, int silent)
        if (!sbi)
                return -ENOMEM;
        mutex_init(&sbi->lock);
-       sbi->uid = current->uid;
-       sbi->gid = current->gid;
+       sbi->uid = current_uid();
+       sbi->gid = current_gid();
        sb->s_fs_info = sbi;
        sb->s_blocksize = PAGE_CACHE_SIZE;
        sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
@@ -281,7 +305,10 @@ static int hypfs_fill_super(struct super_block *sb, void *data, int silent)
                rc = -ENOMEM;
                goto err_alloc;
        }
-       rc = hypfs_diag_create_files(sb, root_dentry);
+       if (MACHINE_IS_VM)
+               rc = hypfs_vm_create_files(sb, root_dentry);
+       else
+               rc = hypfs_diag_create_files(sb, root_dentry);
        if (rc)
                goto err_tree;
        sbi->update_file = hypfs_create_update_file(sb, root_dentry);
@@ -291,6 +318,7 @@ static int hypfs_fill_super(struct super_block *sb, void *data, int silent)
        }
        hypfs_update_update(sb);
        sb->s_root = root_dentry;
+       pr_info("Hypervisor filesystem mounted\n");
        return 0;
 
 err_tree:
@@ -312,10 +340,12 @@ static void hypfs_kill_super(struct super_block *sb)
 {
        struct hypfs_sb_info *sb_info = sb->s_fs_info;
 
-       hypfs_delete_tree(sb->s_root);
-       hypfs_remove(sb_info->update_file);
-       kfree(sb->s_fs_info);
-       sb->s_fs_info = NULL;
+       if (sb->s_root) {
+               hypfs_delete_tree(sb->s_root);
+               hypfs_remove(sb_info->update_file);
+               kfree(sb->s_fs_info);
+               sb->s_fs_info = NULL;
+       }
        kill_litter_super(sb);
 }
 
@@ -330,13 +360,17 @@ static struct dentry *hypfs_create_file(struct super_block *sb,
        qname.name = name;
        qname.len = strlen(name);
        qname.hash = full_name_hash(name, qname.len);
+       mutex_lock(&parent->d_inode->i_mutex);
        dentry = lookup_one_len(name, parent, strlen(name));
-       if (IS_ERR(dentry))
-               return ERR_PTR(-ENOMEM);
+       if (IS_ERR(dentry)) {
+               dentry = ERR_PTR(-ENOMEM);
+               goto fail;
+       }
        inode = hypfs_make_inode(sb, mode);
        if (!inode) {
                dput(dentry);
-               return ERR_PTR(-ENOMEM);
+               dentry = ERR_PTR(-ENOMEM);
+               goto fail;
        }
        if (mode & S_IFREG) {
                inode->i_fop = &hypfs_file_ops;
@@ -350,9 +384,11 @@ static struct dentry *hypfs_create_file(struct super_block *sb,
                parent->d_inode->i_nlink++;
        } else
                BUG();
-       inode->u.generic_ip = data;
+       inode->i_private = data;
        d_instantiate(dentry, inode);
        dget(dentry);
+fail:
+       mutex_unlock(&parent->d_inode->i_mutex);
        return dentry;
 }
 
@@ -365,7 +401,6 @@ struct dentry *hypfs_mkdir(struct super_block *sb, struct dentry *parent,
        if (IS_ERR(dentry))
                return dentry;
        hypfs_add_dentry(dentry);
-       parent->d_inode->i_nlink++;
        return dentry;
 }
 
@@ -425,7 +460,7 @@ struct dentry *hypfs_create_str(struct super_block *sb, struct dentry *dir,
        return dentry;
 }
 
-static struct file_operations hypfs_file_ops = {
+static const struct file_operations hypfs_file_ops = {
        .open           = hypfs_open,
        .release        = hypfs_release,
        .read           = do_sync_read,
@@ -444,43 +479,51 @@ static struct file_system_type hypfs_type = {
 static struct super_operations hypfs_s_ops = {
        .statfs         = simple_statfs,
        .drop_inode     = hypfs_drop_inode,
+       .show_options   = hypfs_show_options,
 };
 
-static decl_subsys(s390, NULL, NULL);
+static struct kobject *s390_kobj;
 
 static int __init hypfs_init(void)
 {
        int rc;
 
-       if (MACHINE_IS_VM)
-               return -ENODATA;
-       if (hypfs_diag_init()) {
-               rc = -ENODATA;
-               goto fail_diag;
+       if (MACHINE_IS_VM) {
+               if (hypfs_vm_init())
+                       /* no diag 2fc, just exit */
+                       return -ENODATA;
+       } else {
+               if (hypfs_diag_init()) {
+                       rc = -ENODATA;
+                       goto fail_diag;
+               }
        }
-       kset_set_kset_s(&s390_subsys, hypervisor_subsys);
-       rc = subsystem_register(&s390_subsys);
-       if (rc)
+       s390_kobj = kobject_create_and_add("s390", hypervisor_kobj);
+       if (!s390_kobj) {
+               rc = -ENOMEM;;
                goto fail_sysfs;
+       }
        rc = register_filesystem(&hypfs_type);
        if (rc)
                goto fail_filesystem;
        return 0;
 
 fail_filesystem:
-       subsystem_unregister(&s390_subsys);
+       kobject_put(s390_kobj);
 fail_sysfs:
-       hypfs_diag_exit();
+       if (!MACHINE_IS_VM)
+               hypfs_diag_exit();
 fail_diag:
-       printk(KERN_ERR "hypfs: Initialization failed with rc = %i.\n", rc);
+       pr_err("Initialization of hypfs failed with rc=%i\n", rc);
        return rc;
 }
 
 static void __exit hypfs_exit(void)
 {
-       hypfs_diag_exit();
+       if (!MACHINE_IS_VM)
+               hypfs_diag_exit();
        unregister_filesystem(&hypfs_type);
-       subsystem_unregister(&s390_subsys);
+       kobject_put(s390_kobj);
 }
 
 module_init(hypfs_init)