These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / fs / ocfs2 / namei.c
index 176fe6a..3123408 100644 (file)
@@ -106,8 +106,6 @@ static int ocfs2_double_lock(struct ocfs2_super *osb,
 static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2);
 /* An orphan dir name is an 8 byte value, printed as a hex string */
 #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
-#define OCFS2_DIO_ORPHAN_PREFIX "dio-"
-#define OCFS2_DIO_ORPHAN_PREFIX_LEN 4
 
 static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
                                   unsigned int flags)
@@ -200,11 +198,12 @@ bail:
 static struct inode *ocfs2_get_init_inode(struct inode *dir, umode_t mode)
 {
        struct inode *inode;
+       int status;
 
        inode = new_inode(dir->i_sb);
        if (!inode) {
                mlog(ML_ERROR, "new_inode failed!\n");
-               return NULL;
+               return ERR_PTR(-ENOMEM);
        }
 
        /* populate as many fields early on as possible - many of
@@ -213,7 +212,10 @@ static struct inode *ocfs2_get_init_inode(struct inode *dir, umode_t mode)
        if (S_ISDIR(mode))
                set_nlink(inode, 2);
        inode_init_owner(inode, dir, mode);
-       dquot_initialize(inode);
+       status = dquot_initialize(inode);
+       if (status)
+               return ERR_PTR(status);
+
        return inode;
 }
 
@@ -264,7 +266,11 @@ static int ocfs2_mknod(struct inode *dir,
                          (unsigned long long)OCFS2_I(dir)->ip_blkno,
                          (unsigned long)dev, mode);
 
-       dquot_initialize(dir);
+       status = dquot_initialize(dir);
+       if (status) {
+               mlog_errno(status);
+               return status;
+       }
 
        /* get our super block */
        osb = OCFS2_SB(dir->i_sb);
@@ -311,8 +317,9 @@ static int ocfs2_mknod(struct inode *dir,
        }
 
        inode = ocfs2_get_init_inode(dir, mode);
-       if (!inode) {
-               status = -ENOMEM;
+       if (IS_ERR(inode)) {
+               status = PTR_ERR(inode);
+               inode = NULL;
                mlog_errno(status);
                goto leave;
        }
@@ -360,7 +367,7 @@ static int ocfs2_mknod(struct inode *dir,
                goto leave;
        }
 
-       status = posix_acl_create(dir, &mode, &default_acl, &acl);
+       status = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
        if (status) {
                mlog_errno(status);
                goto leave;
@@ -648,9 +655,18 @@ static int ocfs2_mknod_locked(struct ocfs2_super *osb,
                return status;
        }
 
-       return __ocfs2_mknod_locked(dir, inode, dev, new_fe_bh,
+       status = __ocfs2_mknod_locked(dir, inode, dev, new_fe_bh,
                                    parent_fe_bh, handle, inode_ac,
                                    fe_blkno, suballoc_loc, suballoc_bit);
+       if (status < 0) {
+               u64 bg_blkno = ocfs2_which_suballoc_group(fe_blkno, suballoc_bit);
+               int tmp = ocfs2_free_suballoc_bits(handle, inode_ac->ac_inode,
+                               inode_ac->ac_bh, suballoc_bit, bg_blkno, 1);
+               if (tmp)
+                       mlog_errno(tmp);
+       }
+
+       return status;
 }
 
 static int ocfs2_mkdir(struct inode *dir,
@@ -708,7 +724,11 @@ static int ocfs2_link(struct dentry *old_dentry,
        if (S_ISDIR(inode->i_mode))
                return -EPERM;
 
-       dquot_initialize(dir);
+       err = dquot_initialize(dir);
+       if (err) {
+               mlog_errno(err);
+               return err;
+       }
 
        err = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
                        &parent_fe_bh, dir, 0);
@@ -896,7 +916,11 @@ static int ocfs2_unlink(struct inode *dir,
                           (unsigned long long)OCFS2_I(dir)->ip_blkno,
                           (unsigned long long)OCFS2_I(inode)->ip_blkno);
 
-       dquot_initialize(dir);
+       status = dquot_initialize(dir);
+       if (status) {
+               mlog_errno(status);
+               return status;
+       }
 
        BUG_ON(d_inode(dentry->d_parent) != dir);
 
@@ -1018,11 +1042,6 @@ leave:
        if (handle)
                ocfs2_commit_trans(osb, handle);
 
-       if (child_locked)
-               ocfs2_inode_unlock(inode, 1);
-
-       ocfs2_inode_unlock(dir, 1);
-
        if (orphan_dir) {
                /* This was locked for us in ocfs2_prepare_orphan_dir() */
                ocfs2_inode_unlock(orphan_dir, 1);
@@ -1030,6 +1049,11 @@ leave:
                iput(orphan_dir);
        }
 
+       if (child_locked)
+               ocfs2_inode_unlock(inode, 1);
+
+       ocfs2_inode_unlock(dir, 1);
+
        brelse(fe_bh);
        brelse(parent_node_bh);
 
@@ -1116,8 +1140,6 @@ static int ocfs2_double_lock(struct ocfs2_super *osb,
        int inode1_is_ancestor, inode2_is_ancestor;
        struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
        struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
-       struct buffer_head **tmpbh;
-       struct inode *tmpinode;
 
        trace_ocfs2_double_lock((unsigned long long)oi1->ip_blkno,
                                (unsigned long long)oi2->ip_blkno);
@@ -1148,13 +1170,8 @@ static int ocfs2_double_lock(struct ocfs2_super *osb,
                                (oi1->ip_blkno < oi2->ip_blkno &&
                                inode2_is_ancestor == 0)) {
                        /* switch id1 and id2 around */
-                       tmpbh = bh2;
-                       bh2 = bh1;
-                       bh1 = tmpbh;
-
-                       tmpinode = inode2;
-                       inode2 = inode1;
-                       inode1 = tmpinode;
+                       swap(bh2, bh1);
+                       swap(inode2, inode1);
                }
                /* lock id2 */
                status = ocfs2_inode_lock_nested(inode2, bh2, 1,
@@ -1237,8 +1254,16 @@ static int ocfs2_rename(struct inode *old_dir,
                           old_dentry->d_name.len, old_dentry->d_name.name,
                           new_dentry->d_name.len, new_dentry->d_name.name);
 
-       dquot_initialize(old_dir);
-       dquot_initialize(new_dir);
+       status = dquot_initialize(old_dir);
+       if (status) {
+               mlog_errno(status);
+               goto bail;
+       }
+       status = dquot_initialize(new_dir);
+       if (status) {
+               mlog_errno(status);
+               goto bail;
+       }
 
        osb = OCFS2_SB(old_dir->i_sb);
 
@@ -1291,6 +1316,11 @@ static int ocfs2_rename(struct inode *old_dir,
        }
        parents_locked = 1;
 
+       if (!new_dir->i_nlink) {
+               status = -EACCES;
+               goto bail;
+       }
+
        /* make sure both dirs have bhs
         * get an extra ref on old_dir_bh if old==new */
        if (!new_dir_bh) {
@@ -1551,12 +1581,25 @@ static int ocfs2_rename(struct inode *old_dir,
        status = ocfs2_find_entry(old_dentry->d_name.name,
                                  old_dentry->d_name.len, old_dir,
                                  &old_entry_lookup);
-       if (status)
+       if (status) {
+               if (!is_journal_aborted(osb->journal->j_journal)) {
+                       ocfs2_error(osb->sb, "new entry %.*s is added, but old entry %.*s "
+                                       "is not deleted.",
+                                       new_dentry->d_name.len, new_dentry->d_name.name,
+                                       old_dentry->d_name.len, old_dentry->d_name.name);
+               }
                goto bail;
+       }
 
        status = ocfs2_delete_entry(handle, old_dir, &old_entry_lookup);
        if (status < 0) {
                mlog_errno(status);
+               if (!is_journal_aborted(osb->journal->j_journal)) {
+                       ocfs2_error(osb->sb, "new entry %.*s is added, but old entry %.*s "
+                                       "is not deleted.",
+                                       new_dentry->d_name.len, new_dentry->d_name.name,
+                                       old_dentry->d_name.len, old_dentry->d_name.name);
+               }
                goto bail;
        }
 
@@ -1615,21 +1658,9 @@ static int ocfs2_rename(struct inode *old_dir,
        ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
        status = 0;
 bail:
-       if (rename_lock)
-               ocfs2_rename_unlock(osb);
-
        if (handle)
                ocfs2_commit_trans(osb, handle);
 
-       if (parents_locked)
-               ocfs2_double_unlock(old_dir, new_dir);
-
-       if (old_child_locked)
-               ocfs2_inode_unlock(old_inode, 1);
-
-       if (new_child_locked)
-               ocfs2_inode_unlock(new_inode, 1);
-
        if (orphan_dir) {
                /* This was locked for us in ocfs2_prepare_orphan_dir() */
                ocfs2_inode_unlock(orphan_dir, 1);
@@ -1637,6 +1668,18 @@ bail:
                iput(orphan_dir);
        }
 
+       if (new_child_locked)
+               ocfs2_inode_unlock(new_inode, 1);
+
+       if (old_child_locked)
+               ocfs2_inode_unlock(old_inode, 1);
+
+       if (parents_locked)
+               ocfs2_double_unlock(old_dir, new_dir);
+
+       if (rename_lock)
+               ocfs2_rename_unlock(osb);
+
        if (new_inode)
                sync_mapping_buffers(old_inode->i_mapping);
 
@@ -1793,7 +1836,11 @@ static int ocfs2_symlink(struct inode *dir,
        trace_ocfs2_symlink_begin(dir, dentry, symname,
                                  dentry->d_name.len, dentry->d_name.name);
 
-       dquot_initialize(dir);
+       status = dquot_initialize(dir);
+       if (status) {
+               mlog_errno(status);
+               goto bail;
+       }
 
        sb = dir->i_sb;
        osb = OCFS2_SB(sb);
@@ -1838,8 +1885,9 @@ static int ocfs2_symlink(struct inode *dir,
        }
 
        inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO);
-       if (!inode) {
-               status = -ENOMEM;
+       if (IS_ERR(inode)) {
+               status = PTR_ERR(inode);
+               inode = NULL;
                mlog_errno(status);
                goto bail;
        }
@@ -2492,8 +2540,9 @@ int ocfs2_create_inode_in_orphan(struct inode *dir,
        }
 
        inode = ocfs2_get_init_inode(dir, mode);
-       if (!inode) {
-               status = -ENOMEM;
+       if (IS_ERR(inode)) {
+               status = PTR_ERR(inode);
+               inode = NULL;
                mlog_errno(status);
                goto leave;
        }
@@ -2577,27 +2626,6 @@ leave:
        return status;
 }
 
-static int ocfs2_dio_orphan_recovered(struct inode *inode)
-{
-       int ret;
-       struct buffer_head *di_bh = NULL;
-       struct ocfs2_dinode *di = NULL;
-
-       ret = ocfs2_inode_lock(inode, &di_bh, 1);
-       if (ret < 0) {
-               mlog_errno(ret);
-               return 0;
-       }
-
-       di = (struct ocfs2_dinode *) di_bh->b_data;
-       ret = !(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL));
-       ocfs2_inode_unlock(inode, 1);
-       brelse(di_bh);
-
-       return ret;
-}
-
-#define OCFS2_DIO_ORPHANED_FL_CHECK_INTERVAL 10000
 int ocfs2_add_inode_to_orphan(struct ocfs2_super *osb,
        struct inode *inode)
 {
@@ -2609,7 +2637,6 @@ int ocfs2_add_inode_to_orphan(struct ocfs2_super *osb,
        handle_t *handle = NULL;
        struct ocfs2_dinode *di = NULL;
 
-restart:
        status = ocfs2_inode_lock(inode, &di_bh, 1);
        if (status < 0) {
                mlog_errno(status);
@@ -2619,15 +2646,21 @@ restart:
        di = (struct ocfs2_dinode *) di_bh->b_data;
        /*
         * Another append dio crashed?
-        * If so, wait for recovery first.
+        * If so, manually recover it first.
         */
        if (unlikely(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL))) {
-               ocfs2_inode_unlock(inode, 1);
-               brelse(di_bh);
-               wait_event_interruptible_timeout(OCFS2_I(inode)->append_dio_wq,
-                               ocfs2_dio_orphan_recovered(inode),
-                               msecs_to_jiffies(OCFS2_DIO_ORPHANED_FL_CHECK_INTERVAL));
-               goto restart;
+               status = ocfs2_truncate_file(inode, di_bh, i_size_read(inode));
+               if (status < 0) {
+                       if (status != -ENOSPC)
+                               mlog_errno(status);
+                       goto bail_unlock_inode;
+               }
+
+               status = ocfs2_del_inode_from_orphan(osb, inode, di_bh, 0, 0);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail_unlock_inode;
+               }
        }
 
        status = ocfs2_prepare_orphan_dir(osb, &orphan_dir_inode,
@@ -2670,30 +2703,22 @@ bail:
 }
 
 int ocfs2_del_inode_from_orphan(struct ocfs2_super *osb,
-               struct inode *inode, int update_isize,
-               loff_t end)
+               struct inode *inode, struct buffer_head *di_bh,
+               int update_isize, loff_t end)
 {
        struct inode *orphan_dir_inode = NULL;
        struct buffer_head *orphan_dir_bh = NULL;
-       struct buffer_head *di_bh = NULL;
-       struct ocfs2_dinode *di = NULL;
+       struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
        handle_t *handle = NULL;
        int status = 0;
 
-       status = ocfs2_inode_lock(inode, &di_bh, 1);
-       if (status < 0) {
-               mlog_errno(status);
-               goto bail;
-       }
-       di = (struct ocfs2_dinode *) di_bh->b_data;
-
        orphan_dir_inode = ocfs2_get_system_file_inode(osb,
                        ORPHAN_DIR_SYSTEM_INODE,
                        le16_to_cpu(di->i_dio_orphaned_slot));
        if (!orphan_dir_inode) {
                status = -ENOENT;
                mlog_errno(status);
-               goto bail_unlock_inode;
+               goto bail;
        }
 
        mutex_lock(&orphan_dir_inode->i_mutex);
@@ -2702,7 +2727,7 @@ int ocfs2_del_inode_from_orphan(struct ocfs2_super *osb,
                mutex_unlock(&orphan_dir_inode->i_mutex);
                iput(orphan_dir_inode);
                mlog_errno(status);
-               goto bail_unlock_inode;
+               goto bail;
        }
 
        handle = ocfs2_start_trans(osb,
@@ -2749,10 +2774,6 @@ bail_unlock_orphan:
        brelse(orphan_dir_bh);
        iput(orphan_dir_inode);
 
-bail_unlock_inode:
-       ocfs2_inode_unlock(inode, 1);
-       brelse(di_bh);
-
 bail:
        return status;
 }