Unionfs: lock our dentry in file operations
authorRachita Kothiyal <rachita@dewey.fsl.cs.sunysb.edu>
Mon, 31 Mar 2008 19:49:55 +0000 (15:49 -0400)
committerRachita Kothiyal <rachita@dewey.fsl.cs.sunysb.edu>
Mon, 31 Mar 2008 19:49:55 +0000 (15:49 -0400)
Signed-off-by: Erez Zadok <ezk@cs.sunysb.edu>
fs/unionfs/commonfops.c
fs/unionfs/dirfops.c
fs/unionfs/file.c
fs/unionfs/union.h

index f19c02a0ef099c329da6639a207a119e1d71b418..4ad8035c2d86bb5b630c1f875b7e533c5676c071 100644 (file)
@@ -369,7 +369,7 @@ out:
  * @willwrite: true if caller may cause changes to the file; false otherwise.
  * Caller must lock/unlock dentry's branch configuration.
  */
-int unionfs_file_revalidate_locked(struct file *file, bool willwrite)
+int unionfs_file_revalidate(struct file *file, bool willwrite)
 {
        struct super_block *sb;
        struct dentry *dentry;
@@ -379,6 +379,7 @@ int unionfs_file_revalidate_locked(struct file *file, bool willwrite)
        int err = 0;
 
        dentry = file->f_path.dentry;
+       verify_locked(dentry);
        sb = dentry->d_sb;
 
        /*
@@ -486,17 +487,6 @@ out_nofree:
        return err;
 }
 
-int unionfs_file_revalidate(struct file *file, bool willwrite)
-{
-       int err;
-
-       unionfs_lock_dentry(file->f_path.dentry, UNIONFS_DMUTEX_CHILD);
-       err = unionfs_file_revalidate_locked(file, willwrite);
-       unionfs_unlock_dentry(file->f_path.dentry);
-
-       return err;
-}
-
 /* unionfs_open helper function: open a directory */
 static int __open_dir(struct inode *inode, struct file *file)
 {
@@ -724,6 +714,8 @@ int unionfs_file_release(struct inode *inode, struct file *file)
        int fgen, err = 0;
 
        unionfs_read_lock(sb, UNIONFS_SMUTEX_PARENT);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
+
        /*
         * Yes, we have to revalidate this file even if it's being released.
         * This is important for open-but-unlinked files, as well as mmap
@@ -741,7 +733,6 @@ int unionfs_file_release(struct inode *inode, struct file *file)
        bstart = fbstart(file);
        bend = fbend(file);
 
-       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
        for (bindex = bstart; bindex <= bend; bindex++) {
                lower_file = unionfs_lower_file_idx(file, bindex);
 
@@ -756,7 +747,6 @@ int unionfs_file_release(struct inode *inode, struct file *file)
                        unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
                }
        }
-       unionfs_unlock_dentry(dentry);
 
        kfree(fileinfo->lower_files);
        kfree(fileinfo->saved_branch_ids);
@@ -764,6 +754,7 @@ int unionfs_file_release(struct inode *inode, struct file *file)
        kfree(fileinfo);
 
 out:
+       unionfs_unlock_dentry(dentry);
        unionfs_read_unlock(sb);
        return err;
 }
@@ -810,7 +801,6 @@ static int unionfs_ioctl_queryfile(struct file *file, unsigned int cmd,
        struct vfsmount *mnt;
 
        dentry = file->f_path.dentry;
-       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
        orig_bstart = dbstart(dentry);
        orig_bend = dbend(dentry);
        err = unionfs_partial_lookup(dentry);
@@ -852,15 +842,16 @@ static int unionfs_ioctl_queryfile(struct file *file, unsigned int cmd,
                err = -EFAULT;
 
 out:
-       unionfs_unlock_dentry(dentry);
        return err < 0 ? err : bend;
 }
 
 long unionfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
        long err;
+       struct dentry *dentry = file->f_path.dentry;
 
-       unionfs_read_lock(file->f_path.dentry->d_sb, UNIONFS_SMUTEX_PARENT);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
 
        err = unionfs_file_revalidate(file, true);
        if (unlikely(err))
@@ -888,7 +879,8 @@ long unionfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 
 out:
        unionfs_check_file(file);
-       unionfs_read_unlock(file->f_path.dentry->d_sb);
+       unionfs_unlock_dentry(dentry);
+       unionfs_read_unlock(dentry->d_sb);
        return err;
 }
 
@@ -902,7 +894,7 @@ int unionfs_flush(struct file *file, fl_owner_t id)
        unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
        unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
 
-       err = unionfs_file_revalidate_locked(file, true);
+       err = unionfs_file_revalidate(file, true);
        if (unlikely(err))
                goto out;
        unionfs_check_file(file);
@@ -924,7 +916,7 @@ int unionfs_flush(struct file *file, fl_owner_t id)
 out:
        if (!err)
                unionfs_check_file(file);
-       unionfs_unlock_dentry(file->f_path.dentry);
+       unionfs_unlock_dentry(dentry);
        unionfs_read_unlock(dentry->d_sb);
        return err;
 }
index 7f6ff5fec57bc0da7e14d9a279a9a82113a622e8..47bf4fd1f131d348066b50e5c80a225c9d09731e 100644 (file)
@@ -22,6 +22,7 @@ static int unionfs_readdir(struct file *file, void *dirent, filldir_t filldir)
 {
        int err = 0;
        struct file *odf_file = NULL;
+       struct dentry *dentry = file->f_path.dentry;
        struct inode *inode = NULL;
        struct dentry *odf_cache = NULL;
        loff_t size, pos;
@@ -32,13 +33,14 @@ static int unionfs_readdir(struct file *file, void *dirent, filldir_t filldir)
        int namelen;
        unsigned int d_type;
 
-       unionfs_read_lock(file->f_path.dentry->d_sb, UNIONFS_SMUTEX_PARENT);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
 
        err = unionfs_file_revalidate(file, false);
        if (unlikely(err))
                goto out;
 
-       inode = file->f_path.dentry->d_inode;
+       inode = dentry->d_inode;
 
        /* Get the odf/ic file */
        /* FIXME: this should be saved somewhere, also check mtime */
@@ -128,15 +130,18 @@ out:
        if (odf_file)
                filp_close(odf_file, NULL);
        dput(odf_cache);
-       unionfs_read_unlock(file->f_path.dentry->d_sb);
+       unionfs_unlock_dentry(dentry);
+       unionfs_read_unlock(dentry->d_sb);
        return err;
 }
 
 static loff_t unionfs_dir_llseek(struct file *file, loff_t offset, int origin)
 {
+       struct dentry *dentry = file->f_path.dentry;
        loff_t err;
 
-       unionfs_read_lock(file->f_path.dentry->d_sb, UNIONFS_SMUTEX_PARENT);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
 
        err = unionfs_file_revalidate(file, false);
        if (unlikely(err))
@@ -144,7 +149,8 @@ static loff_t unionfs_dir_llseek(struct file *file, loff_t offset, int origin)
 
        err = generic_file_llseek(file, offset, origin);
 out:
-       unionfs_read_unlock(file->f_path.dentry->d_sb);
+       unionfs_unlock_dentry(dentry);
+       unionfs_read_unlock(dentry->d_sb);
        return err;
 }
 
index 9397ff32ab20cc16446f55cda4f79a7ab5740efc..0a393876d5c31286da08f07007b22c42b1f8b2da 100644 (file)
@@ -27,7 +27,7 @@ static ssize_t unionfs_read(struct file *file, char __user *buf,
 
        unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
        unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
-       err = unionfs_file_revalidate_locked(file, false);
+       err = unionfs_file_revalidate(file, false);
        if (unlikely(err))
                goto out;
 
@@ -55,7 +55,7 @@ static ssize_t unionfs_write(struct file *file, const char __user *buf,
 
        unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
        unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
-       err = unionfs_file_revalidate_locked(file, true);
+       err = unionfs_file_revalidate(file, true);
        if (unlikely(err))
                goto out;
 
@@ -94,9 +94,11 @@ static int unionfs_mmap(struct file *file, struct vm_area_struct *vma)
        int err = 0;
        bool willwrite;
        struct file *lower_file;
+       struct dentry *dentry = file->f_path.dentry;
        struct vm_operations_struct *saved_vm_ops = NULL;
 
-       unionfs_read_lock(file->f_path.dentry->d_sb, UNIONFS_SMUTEX_PARENT);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
 
        /* This might be deferred to mmap's writepage */
        willwrite = ((vma->vm_flags | VM_SHARED | VM_WRITE) == vma->vm_flags);
@@ -157,10 +159,11 @@ static int unionfs_mmap(struct file *file, struct vm_area_struct *vma)
 out:
        if (!err) {
                /* copyup could cause parent dir times to change */
-               unionfs_copy_attr_times(file->f_path.dentry->d_parent->d_inode);
+               unionfs_copy_attr_times(dentry->d_parent->d_inode);
                unionfs_check_file(file);
        }
-       unionfs_read_unlock(file->f_path.dentry->d_sb);
+       unionfs_unlock_dentry(dentry);
+       unionfs_read_unlock(dentry->d_sb);
        return err;
 }
 
@@ -172,7 +175,8 @@ int unionfs_fsync(struct file *file, struct dentry *dentry, int datasync)
        struct inode *lower_inode, *inode;
        int err = -EINVAL;
 
-       unionfs_read_lock(file->f_path.dentry->d_sb, UNIONFS_SMUTEX_PARENT);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
        err = unionfs_file_revalidate(file, true);
        if (unlikely(err))
                goto out;
@@ -207,8 +211,10 @@ int unionfs_fsync(struct file *file, struct dentry *dentry, int datasync)
        unionfs_copy_attr_times(inode);
 
 out:
-       unionfs_check_file(file);
-       unionfs_read_unlock(file->f_path.dentry->d_sb);
+       if (!err)
+               unionfs_check_file(file);
+       unionfs_unlock_dentry(dentry);
+       unionfs_read_unlock(dentry->d_sb);
        return err;
 }
 
@@ -216,11 +222,12 @@ int unionfs_fasync(int fd, struct file *file, int flag)
 {
        int bindex, bstart, bend;
        struct file *lower_file;
-       struct dentry *dentry;
+       struct dentry *dentry = file->f_path.dentry;
        struct inode *lower_inode, *inode;
        int err = 0;
 
-       unionfs_read_lock(file->f_path.dentry->d_sb, UNIONFS_SMUTEX_PARENT);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
        err = unionfs_file_revalidate(file, true);
        if (unlikely(err))
                goto out;
@@ -231,7 +238,6 @@ int unionfs_fasync(int fd, struct file *file, int flag)
        if (bstart < 0 || bend < 0)
                goto out;
 
-       dentry = file->f_path.dentry;
        inode = dentry->d_inode;
        if (unlikely(!inode)) {
                printk(KERN_ERR
@@ -253,8 +259,10 @@ int unionfs_fasync(int fd, struct file *file, int flag)
        unionfs_copy_attr_times(inode);
 
 out:
-       unionfs_check_file(file);
-       unionfs_read_unlock(file->f_path.dentry->d_sb);
+       if (!err)
+               unionfs_check_file(file);
+       unionfs_unlock_dentry(dentry);
+       unionfs_read_unlock(dentry->d_sb);
        return err;
 }
 
@@ -264,8 +272,10 @@ static ssize_t unionfs_splice_read(struct file *file, loff_t *ppos,
 {
        ssize_t err;
        struct file *lower_file;
+       struct dentry *dentry = file->f_path.dentry;
 
-       unionfs_read_lock(file->f_path.dentry->d_sb, UNIONFS_SMUTEX_PARENT);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
        err = unionfs_file_revalidate(file, false);
        if (unlikely(err))
                goto out;
@@ -274,14 +284,14 @@ static ssize_t unionfs_splice_read(struct file *file, loff_t *ppos,
        err = vfs_splice_to(lower_file, ppos, pipe, len, flags);
        /* update our inode atime upon a successful lower splice-read */
        if (err >= 0) {
-               fsstack_copy_attr_atime(file->f_path.dentry->d_inode,
+               fsstack_copy_attr_atime(dentry->d_inode,
                                        lower_file->f_path.dentry->d_inode);
                unionfs_check_file(file);
        }
 
 out:
-       unionfs_check_file(file);
-       unionfs_read_unlock(file->f_path.dentry->d_sb);
+       unionfs_unlock_dentry(dentry);
+       unionfs_read_unlock(dentry->d_sb);
        return err;
 }
 
@@ -291,8 +301,10 @@ static ssize_t unionfs_splice_write(struct pipe_inode_info *pipe,
 {
        ssize_t err = 0;
        struct file *lower_file;
+       struct dentry *dentry = file->f_path.dentry;
 
-       unionfs_read_lock(file->f_path.dentry->d_sb, UNIONFS_SMUTEX_PARENT);
+       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
+       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
        err = unionfs_file_revalidate(file, true);
        if (unlikely(err))
                goto out;
@@ -301,15 +313,16 @@ static ssize_t unionfs_splice_write(struct pipe_inode_info *pipe,
        err = vfs_splice_from(pipe, lower_file, ppos, len, flags);
        /* update our inode times+sizes upon a successful lower write */
        if (err >= 0) {
-               fsstack_copy_inode_size(file->f_path.dentry->d_inode,
+               fsstack_copy_inode_size(dentry->d_inode,
                                        lower_file->f_path.dentry->d_inode);
-               fsstack_copy_attr_times(file->f_path.dentry->d_inode,
+               fsstack_copy_attr_times(dentry->d_inode,
                                        lower_file->f_path.dentry->d_inode);
                unionfs_check_file(file);
        }
 
 out:
-       unionfs_read_unlock(file->f_path.dentry->d_sb);
+       unionfs_unlock_dentry(dentry);
+       unionfs_read_unlock(dentry->d_sb);
        return err;
 }
 
index 3278ceb82b79cfca30d3cca6d3c65aeeedf36eb0..4f256c0186687ad59a9eec7dee29153af3586336 100644 (file)
@@ -364,7 +364,6 @@ extern int unionfs_setlk(struct file *file, int cmd, struct file_lock *fl);
 extern int unionfs_getlk(struct file *file, struct file_lock *fl);
 
 /* Common file operations. */
-extern int unionfs_file_revalidate_locked(struct file *file, bool willwrite);
 extern int unionfs_file_revalidate(struct file *file, bool willwrite);
 extern int unionfs_open(struct inode *inode, struct file *file);
 extern int unionfs_file_release(struct inode *inode, struct file *file);