bug fix: set lower inodes correctly after branch management succeeds
authorErez Zadok <ezk@bigvaio.(none)>
Wed, 23 May 2007 03:52:12 +0000 (23:52 -0400)
committerErez Zadok <ezk@cs.sunysb.edu>
Sun, 23 Feb 2014 19:02:50 +0000 (14:02 -0500)
Signed-off-by: Erez Zadok <ezk@cs.sunysb.edu>
fs/unionfs/super.c

index 1ca6c54c2fafd57f28f37739f2df455f99656305..4b4233cf81d7e595cc97ffad6322ddeaba1cd51b 100644 (file)
@@ -351,7 +351,13 @@ found_insertion_point:
                       new_branch, err);
                goto out;
        }
-       /* it's probably safe to check_mode the new branch to insert */
+       /*
+        * It's probably safe to check_mode the new branch to insert.  Note:
+        * we don't allow inserting branches which are unionfs's by
+        * themselves (check_branch returns EINVAL in that case).  This is
+        * because this code base doesn't support stacking unionfs: the ODF
+        * code base supports that correctly.
+        */
        if ((err = check_branch(&nd))) {
                printk(KERN_WARNING "unionfs: hidden directory "
                       "\"%s\" is not a valid branch\n", optarg);
@@ -416,14 +422,16 @@ static int unionfs_remount_fs(struct super_block *sb, int *flags,
        int i;
        char *optionstmp, *tmp_to_free; /* kstrdup'ed of "options" */
        char *optname;
-       int cur_branches;       /* no. of current branches */
-       int new_branches;       /* no. of branches actually left in the end */
+       int cur_branches = 0;   /* no. of current branches */
+       int new_branches = 0;   /* no. of branches actually left in the end */
        int add_branches;       /* est. no. of branches to add */
        int del_branches;       /* est. no. of branches to del */
        int max_branches;       /* max possible no. of branches */
        struct unionfs_data *new_data = NULL, *tmp_data = NULL;
        struct path *new_lower_paths = NULL, *tmp_lower_paths = NULL;
+       struct inode **new_lower_inodes = NULL;
        int new_high_branch_id; /* new high branch ID */
+       int old_ibstart, old_ibend;
 
        unionfs_write_lock(sb);
 
@@ -661,10 +669,7 @@ out_no_change:
                        err = -ENOMEM;
                        goto out_release;
                }
-               /*
-                * copy current info into new placeholders, incrementing
-                * refcounts.
-                */
+               /* copy current info into new placeholders */
                memcpy(new_data, tmp_data,
                       new_branches * sizeof(struct unionfs_data));
                memcpy(new_lower_paths, tmp_lower_paths,
@@ -676,12 +681,19 @@ out_no_change:
                 */
                kfree(tmp_data);
                kfree(tmp_lower_paths);
-               /* no need to nullify pointers here */
+               /* no need to nullify pointers here (they get reused below) */
        } else {
                /* number of branches didn't change, no need to re-alloc */
                new_data = tmp_data;
                new_lower_paths = tmp_lower_paths;
        }
+       /* allocate space for new pointers to lower inodes */
+       new_lower_inodes = kcalloc(new_branches,
+                                  sizeof(struct inode *), GFP_KERNEL);
+       if (!new_lower_inodes) {
+               err = -ENOMEM;
+               goto out_release;
+       }
 
        /*
         * OK, just before we actually put the new set of branches in place,
@@ -703,7 +715,7 @@ out_no_change:
         * fsync_super() which would not have returned until all dirty pages
         * were flushed.
         *
-        * But do w have to worry about locked pages?  Is there any chance
+        * But do we have to worry about locked pages?  Is there any chance
         * that in here we'll get locked pages?
         *
         * XXX: what about pages mapped into pagetables?  Are these pages
@@ -730,8 +742,31 @@ out_no_change:
        i = sbmax(sb);          /* save no. of branches to release at end */
        sbend(sb) = new_branches - 1;
        set_dbend(sb->s_root, new_branches - 1);
+       old_ibstart = ibstart(sb->s_root->d_inode);
+       old_ibend = ibend(sb->s_root->d_inode);
+       ibend(sb->s_root->d_inode) = new_branches - 1;
        UNIONFS_D(sb->s_root)->bcount = new_branches;
-       new_branches = i;       /* no. of branches to release below */
+       new_branches = i; /* no. of branches to release below */
+
+       /*
+        * Update lower inodes: 3 steps
+        * 1. grab ref on all new lower inodes
+        */
+       for (i=dbstart(sb->s_root); i<=dbend(sb->s_root); i++) {
+               struct dentry *lower_dentry =
+                       unionfs_lower_dentry_idx(sb->s_root, i);
+               atomic_inc(&lower_dentry->d_inode->i_count);
+               new_lower_inodes[i] = lower_dentry->d_inode;
+       }
+       /* 2. release reference on all older lower inodes */
+       for (i=old_ibstart; i<=old_ibend; i++) {
+               iput(unionfs_lower_inode_idx(sb->s_root->d_inode, i));
+               unionfs_set_lower_inode_idx(sb->s_root->d_inode, i, NULL);
+       }
+       kfree(UNIONFS_I(sb->s_root->d_inode)->lower_inodes);
+       /* 3. update root dentry's inode to new lower_inodes array */
+       UNIONFS_I(sb->s_root->d_inode)->lower_inodes = new_lower_inodes;
+       new_lower_inodes = NULL;
 
        /* maxbytes may have changed */
        sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
@@ -764,8 +799,10 @@ out_free:
        kfree(tmp_data);
        kfree(new_lower_paths);
        kfree(new_data);
+       kfree(new_lower_inodes);
 out_error:
        unionfs_write_unlock(sb);
+       unionfs_check_dentry(sb->s_root);
        return err;
 }