ocfs2: fix disk file size and memory file size mismatch
[linux-2.6-block.git] / fs / ocfs2 / aops.c
index 843475793504299aea8348bf2312eede339e4846..328ac7f99d52e17b7a820f71077a47b563846ff0 100644 (file)
@@ -2151,6 +2151,7 @@ static int ocfs2_dio_get_block(struct inode *inode, sector_t iblock,
                               struct buffer_head *bh_result, int create)
 {
        struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
+       struct ocfs2_inode_info *oi = OCFS2_I(inode);
        struct ocfs2_write_ctxt *wc;
        struct ocfs2_write_cluster_desc *desc = NULL;
        struct ocfs2_dio_write_ctxt *dwc = NULL;
@@ -2166,16 +2167,26 @@ static int ocfs2_dio_get_block(struct inode *inode, sector_t iblock,
        mlog(0, "get block of %lu at %llu:%u req %u\n",
                        inode->i_ino, pos, len, total_len);
 
-       /* This is the fast path for re-write. */
-       ret = ocfs2_get_block(inode, iblock, bh_result, create);
+       /*
+        * Because we need to change file size in ocfs2_dio_end_io_write(), or
+        * we may need to add it to orphan dir. So can not fall to fast path
+        * while file size will be changed.
+        */
+       if (pos + total_len <= i_size_read(inode)) {
+               down_read(&oi->ip_alloc_sem);
+               /* This is the fast path for re-write. */
+               ret = ocfs2_get_block(inode, iblock, bh_result, create);
 
-       if (buffer_mapped(bh_result) &&
-           !buffer_new(bh_result) &&
-           ret == 0)
-               goto out;
+               up_read(&oi->ip_alloc_sem);
 
-       /* Clear state set by ocfs2_get_block. */
-       bh_result->b_state = 0;
+               if (buffer_mapped(bh_result) &&
+                   !buffer_new(bh_result) &&
+                   ret == 0)
+                       goto out;
+
+               /* Clear state set by ocfs2_get_block. */
+               bh_result->b_state = 0;
+       }
 
        dwc = ocfs2_dio_alloc_write_ctx(bh_result, &first_get_block);
        if (unlikely(dwc == NULL)) {
@@ -2206,6 +2217,8 @@ static int ocfs2_dio_get_block(struct inode *inode, sector_t iblock,
                goto out;
        }
 
+       down_write(&oi->ip_alloc_sem);
+
        if (first_get_block) {
                if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
                        ret = ocfs2_zero_tail(inode, di_bh, pos);
@@ -2259,6 +2272,7 @@ static int ocfs2_dio_get_block(struct inode *inode, sector_t iblock,
        BUG_ON(ret != len);
        ret = 0;
 unlock:
+       up_write(&oi->ip_alloc_sem);
        ocfs2_inode_unlock(inode, 1);
        brelse(di_bh);
 out:
@@ -2275,6 +2289,7 @@ static void ocfs2_dio_end_io_write(struct inode *inode,
        struct ocfs2_cached_dealloc_ctxt dealloc;
        struct ocfs2_extent_tree et;
        struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
+       struct ocfs2_inode_info *oi = OCFS2_I(inode);
        struct ocfs2_unwritten_extent *ue = NULL;
        struct buffer_head *di_bh = NULL;
        struct ocfs2_dinode *di;
@@ -2293,12 +2308,6 @@ static void ocfs2_dio_end_io_write(struct inode *inode,
            !dwc->dw_orphaned)
                goto out;
 
-       ret = ocfs2_inode_lock(inode, &di_bh, 1);
-       if (ret < 0) {
-               mlog_errno(ret);
-               goto out;
-       }
-
        /* ocfs2_file_write_iter will get i_mutex, so we need not lock if we
         * are in that context. */
        if (dwc->dw_writer_pid != task_pid_nr(current)) {
@@ -2306,6 +2315,14 @@ static void ocfs2_dio_end_io_write(struct inode *inode,
                locked = 1;
        }
 
+       ret = ocfs2_inode_lock(inode, &di_bh, 1);
+       if (ret < 0) {
+               mlog_errno(ret);
+               goto out;
+       }
+
+       down_write(&oi->ip_alloc_sem);
+
        /* Delete orphan before acquire i_mutex. */
        if (dwc->dw_orphaned) {
                BUG_ON(dwc->dw_writer_pid != task_pid_nr(current));
@@ -2359,6 +2376,7 @@ static void ocfs2_dio_end_io_write(struct inode *inode,
 commit:
        ocfs2_commit_trans(osb, handle);
 unlock:
+       up_write(&oi->ip_alloc_sem);
        ocfs2_inode_unlock(inode, 1);
        brelse(di_bh);
 out: