video: rockchip: vcodec: add reg_rlc inside dec_pp
[firefly-linux-kernel-4.4.55.git] / fs / ocfs2 / aops.c
index 20dfec72e90330a9b613e2a78ee5b59874870925..e6795c7c76a854b39441381bdce5fed147bb1a53 100644 (file)
@@ -28,6 +28,8 @@
 #include <linux/pipe_fs_i.h>
 #include <linux/mpage.h>
 #include <linux/quotaops.h>
+#include <linux/blkdev.h>
+#include <linux/uio.h>
 
 #include <cluster/masklog.h>
 
@@ -47,6 +49,9 @@
 #include "ocfs2_trace.h"
 
 #include "buffer_head_io.h"
+#include "dir.h"
+#include "namei.h"
+#include "sysfile.h"
 
 static int ocfs2_symlink_get_block(struct inode *inode, sector_t iblock,
                                   struct buffer_head *bh_result, int create)
@@ -80,6 +85,7 @@ static int ocfs2_symlink_get_block(struct inode *inode, sector_t iblock,
 
        if ((u64)iblock >= ocfs2_clusters_to_blocks(inode->i_sb,
                                                    le32_to_cpu(fe->i_clusters))) {
+               err = -ENOMEM;
                mlog(ML_ERROR, "block offset is outside the allocated size: "
                     "%llu\n", (unsigned long long)iblock);
                goto bail;
@@ -92,6 +98,7 @@ static int ocfs2_symlink_get_block(struct inode *inode, sector_t iblock,
                            iblock;
                buffer_cache_bh = sb_getblk(osb->sb, blkno);
                if (!buffer_cache_bh) {
+                       err = -ENOMEM;
                        mlog(ML_ERROR, "couldn't getblock for symlink!\n");
                        goto bail;
                }
@@ -220,7 +227,7 @@ int ocfs2_read_inline_data(struct inode *inode, struct page *page,
        struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
 
        if (!(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL)) {
-               ocfs2_error(inode->i_sb, "Inode %llu lost inline data flag",
+               ocfs2_error(inode->i_sb, "Inode %llu lost inline data flag\n",
                            (unsigned long long)OCFS2_I(inode)->ip_blkno);
                return -EROFS;
        }
@@ -230,7 +237,7 @@ int ocfs2_read_inline_data(struct inode *inode, struct page *page,
        if (size > PAGE_CACHE_SIZE ||
            size > ocfs2_max_inline_data_with_xattr(inode->i_sb, di)) {
                ocfs2_error(inode->i_sb,
-                           "Inode %llu has with inline data has bad size: %Lu",
+                           "Inode %llu has with inline data has bad size: %Lu\n",
                            (unsigned long long)OCFS2_I(inode)->ip_blkno,
                            (unsigned long long)size);
                return -EROFS;
@@ -504,18 +511,21 @@ bail:
  *
  * called like this: dio->get_blocks(dio->inode, fs_startblk,
  *                                     fs_count, map_bh, dio->rw == WRITE);
- *
- * Note that we never bother to allocate blocks here, and thus ignore the
- * create argument.
  */
 static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
                                     struct buffer_head *bh_result, int create)
 {
        int ret;
+       u32 cpos = 0;
+       int alloc_locked = 0;
        u64 p_blkno, inode_blocks, contig_blocks;
        unsigned int ext_flags;
        unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
        unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
+       unsigned long len = bh_result->b_size;
+       unsigned int clusters_to_alloc = 0, contig_clusters = 0;
+
+       cpos = ocfs2_blocks_to_clusters(inode->i_sb, iblock);
 
        /* This function won't even be called if the request isn't all
         * nicely aligned and of the right size, so there's no need
@@ -523,10 +533,14 @@ static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
 
        inode_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
 
+       down_read(&OCFS2_I(inode)->ip_alloc_sem);
+
        /* This figures out the size of the next contiguous block, and
         * our logical offset */
        ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno,
                                          &contig_blocks, &ext_flags);
+       up_read(&OCFS2_I(inode)->ip_alloc_sem);
+
        if (ret) {
                mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n",
                     (unsigned long long)iblock);
@@ -537,6 +551,48 @@ static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
        /* We should already CoW the refcounted extent in case of create. */
        BUG_ON(create && (ext_flags & OCFS2_EXT_REFCOUNTED));
 
+       /* allocate blocks if no p_blkno is found, and create == 1 */
+       if (!p_blkno && create) {
+               ret = ocfs2_inode_lock(inode, NULL, 1);
+               if (ret < 0) {
+                       mlog_errno(ret);
+                       goto bail;
+               }
+
+               alloc_locked = 1;
+
+               down_write(&OCFS2_I(inode)->ip_alloc_sem);
+
+               /* fill hole, allocate blocks can't be larger than the size
+                * of the hole */
+               clusters_to_alloc = ocfs2_clusters_for_bytes(inode->i_sb, len);
+               contig_clusters = ocfs2_clusters_for_blocks(inode->i_sb,
+                               contig_blocks);
+               if (clusters_to_alloc > contig_clusters)
+                       clusters_to_alloc = contig_clusters;
+
+               /* allocate extent and insert them into the extent tree */
+               ret = ocfs2_extend_allocation(inode, cpos,
+                               clusters_to_alloc, 0);
+               if (ret < 0) {
+                       up_write(&OCFS2_I(inode)->ip_alloc_sem);
+                       mlog_errno(ret);
+                       goto bail;
+               }
+
+               ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno,
+                               &contig_blocks, &ext_flags);
+               if (ret < 0) {
+                       up_write(&OCFS2_I(inode)->ip_alloc_sem);
+                       mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n",
+                                       (unsigned long long)iblock);
+                       ret = -EIO;
+                       goto bail;
+               }
+               set_buffer_new(bh_result);
+               up_write(&OCFS2_I(inode)->ip_alloc_sem);
+       }
+
        /*
         * get_more_blocks() expects us to describe a hole by clearing
         * the mapped bit on bh_result().
@@ -554,6 +610,8 @@ static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
                contig_blocks = max_blocks;
        bh_result->b_size = contig_blocks << blocksize_bits;
 bail:
+       if (alloc_locked)
+               ocfs2_inode_unlock(inode, 1);
        return ret;
 }
 
@@ -565,68 +623,370 @@ bail:
 static void ocfs2_dio_end_io(struct kiocb *iocb,
                             loff_t offset,
                             ssize_t bytes,
-                            void *private,
-                            int ret,
-                            bool is_async)
+                            void *private)
 {
        struct inode *inode = file_inode(iocb->ki_filp);
        int level;
-       wait_queue_head_t *wq = ocfs2_ioend_wq(inode);
 
        /* this io's submitter should not have unlocked this before we could */
        BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
 
-       if (ocfs2_iocb_is_sem_locked(iocb))
-               ocfs2_iocb_clear_sem_locked(iocb);
-
        if (ocfs2_iocb_is_unaligned_aio(iocb)) {
                ocfs2_iocb_clear_unaligned_aio(iocb);
 
-               if (atomic_dec_and_test(&OCFS2_I(inode)->ip_unaligned_aio) &&
-                   waitqueue_active(wq)) {
-                       wake_up_all(wq);
-               }
+               mutex_unlock(&OCFS2_I(inode)->ip_unaligned_aio);
        }
 
-       ocfs2_iocb_clear_rw_locked(iocb);
+       /* Let rw unlock to be done later to protect append direct io write */
+       if (offset + bytes <= i_size_read(inode)) {
+               ocfs2_iocb_clear_rw_locked(iocb);
 
-       level = ocfs2_iocb_rw_locked_level(iocb);
-       ocfs2_rw_unlock(inode, level);
+               level = ocfs2_iocb_rw_locked_level(iocb);
+               ocfs2_rw_unlock(inode, level);
+       }
+}
 
-       inode_dio_done(inode);
-       if (is_async)
-               aio_complete(iocb, ret, 0);
+static int ocfs2_releasepage(struct page *page, gfp_t wait)
+{
+       if (!page_has_buffers(page))
+               return 0;
+       return try_to_free_buffers(page);
 }
 
-/*
- * ocfs2_invalidatepage() and ocfs2_releasepage() are shamelessly stolen
- * from ext3.  PageChecked() bits have been removed as OCFS2 does not
- * do journalled data.
- */
-static void ocfs2_invalidatepage(struct page *page, unsigned long offset)
+static int ocfs2_is_overwrite(struct ocfs2_super *osb,
+               struct inode *inode, loff_t offset)
 {
-       journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
+       int ret = 0;
+       u32 v_cpos = 0;
+       u32 p_cpos = 0;
+       unsigned int num_clusters = 0;
+       unsigned int ext_flags = 0;
 
-       jbd2_journal_invalidatepage(journal, page, offset);
+       v_cpos = ocfs2_bytes_to_clusters(osb->sb, offset);
+       ret = ocfs2_get_clusters(inode, v_cpos, &p_cpos,
+                       &num_clusters, &ext_flags);
+       if (ret < 0) {
+               mlog_errno(ret);
+               return ret;
+       }
+
+       if (p_cpos && !(ext_flags & OCFS2_EXT_UNWRITTEN))
+               return 1;
+
+       return 0;
 }
 
-static int ocfs2_releasepage(struct page *page, gfp_t wait)
+static int ocfs2_direct_IO_zero_extend(struct ocfs2_super *osb,
+               struct inode *inode, loff_t offset,
+               u64 zero_len, int cluster_align)
 {
-       journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
+       u32 p_cpos = 0;
+       u32 v_cpos = ocfs2_bytes_to_clusters(osb->sb, i_size_read(inode));
+       unsigned int num_clusters = 0;
+       unsigned int ext_flags = 0;
+       int ret = 0;
 
-       if (!page_has_buffers(page))
+       if (offset <= i_size_read(inode) || cluster_align)
+               return 0;
+
+       ret = ocfs2_get_clusters(inode, v_cpos, &p_cpos, &num_clusters,
+                       &ext_flags);
+       if (ret < 0) {
+               mlog_errno(ret);
+               return ret;
+       }
+
+       if (p_cpos && !(ext_flags & OCFS2_EXT_UNWRITTEN)) {
+               u64 s = i_size_read(inode);
+               sector_t sector = ((u64)p_cpos << (osb->s_clustersize_bits - 9)) +
+                       (do_div(s, osb->s_clustersize) >> 9);
+
+               ret = blkdev_issue_zeroout(osb->sb->s_bdev, sector,
+                               zero_len >> 9, GFP_NOFS, false);
+               if (ret < 0)
+                       mlog_errno(ret);
+       }
+
+       return ret;
+}
+
+static int ocfs2_direct_IO_extend_no_holes(struct ocfs2_super *osb,
+               struct inode *inode, loff_t offset)
+{
+       u64 zero_start, zero_len, total_zero_len;
+       u32 p_cpos = 0, clusters_to_add;
+       u32 v_cpos = ocfs2_bytes_to_clusters(osb->sb, i_size_read(inode));
+       unsigned int num_clusters = 0;
+       unsigned int ext_flags = 0;
+       u32 size_div, offset_div;
+       int ret = 0;
+
+       {
+               u64 o = offset;
+               u64 s = i_size_read(inode);
+
+               offset_div = do_div(o, osb->s_clustersize);
+               size_div = do_div(s, osb->s_clustersize);
+       }
+
+       if (offset <= i_size_read(inode))
                return 0;
-       return jbd2_journal_try_to_free_buffers(journal, page, wait);
+
+       clusters_to_add = ocfs2_bytes_to_clusters(inode->i_sb, offset) -
+               ocfs2_bytes_to_clusters(inode->i_sb, i_size_read(inode));
+       total_zero_len = offset - i_size_read(inode);
+       if (clusters_to_add)
+               total_zero_len -= offset_div;
+
+       /* Allocate clusters to fill out holes, and this is only needed
+        * when we add more than one clusters. Otherwise the cluster will
+        * be allocated during direct IO */
+       if (clusters_to_add > 1) {
+               ret = ocfs2_extend_allocation(inode,
+                               OCFS2_I(inode)->ip_clusters,
+                               clusters_to_add - 1, 0);
+               if (ret) {
+                       mlog_errno(ret);
+                       goto out;
+               }
+       }
+
+       while (total_zero_len) {
+               ret = ocfs2_get_clusters(inode, v_cpos, &p_cpos, &num_clusters,
+                               &ext_flags);
+               if (ret < 0) {
+                       mlog_errno(ret);
+                       goto out;
+               }
+
+               zero_start = ocfs2_clusters_to_bytes(osb->sb, p_cpos) +
+                       size_div;
+               zero_len = ocfs2_clusters_to_bytes(osb->sb, num_clusters) -
+                       size_div;
+               zero_len = min(total_zero_len, zero_len);
+
+               if (p_cpos && !(ext_flags & OCFS2_EXT_UNWRITTEN)) {
+                       ret = blkdev_issue_zeroout(osb->sb->s_bdev,
+                                       zero_start >> 9, zero_len >> 9,
+                                       GFP_NOFS, false);
+                       if (ret < 0) {
+                               mlog_errno(ret);
+                               goto out;
+                       }
+               }
+
+               total_zero_len -= zero_len;
+               v_cpos += ocfs2_bytes_to_clusters(osb->sb, zero_len + size_div);
+
+               /* Only at first iteration can be cluster not aligned.
+                * So set size_div to 0 for the rest */
+               size_div = 0;
+       }
+
+out:
+       return ret;
 }
 
-static ssize_t ocfs2_direct_IO(int rw,
-                              struct kiocb *iocb,
-                              const struct iovec *iov,
-                              loff_t offset,
-                              unsigned long nr_segs)
+static ssize_t ocfs2_direct_IO_write(struct kiocb *iocb,
+               struct iov_iter *iter,
+               loff_t offset)
 {
+       ssize_t ret = 0;
+       ssize_t written = 0;
+       bool orphaned = false;
+       int is_overwrite = 0;
        struct file *file = iocb->ki_filp;
        struct inode *inode = file_inode(file)->i_mapping->host;
+       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
+       struct buffer_head *di_bh = NULL;
+       size_t count = iter->count;
+       journal_t *journal = osb->journal->j_journal;
+       u64 zero_len_head, zero_len_tail;
+       int cluster_align_head, cluster_align_tail;
+       loff_t final_size = offset + count;
+       int append_write = offset >= i_size_read(inode) ? 1 : 0;
+       unsigned int num_clusters = 0;
+       unsigned int ext_flags = 0;
+
+       {
+               u64 o = offset;
+               u64 s = i_size_read(inode);
+
+               zero_len_head = do_div(o, 1 << osb->s_clustersize_bits);
+               cluster_align_head = !zero_len_head;
+
+               zero_len_tail = osb->s_clustersize -
+                       do_div(s, osb->s_clustersize);
+               if ((offset - i_size_read(inode)) < zero_len_tail)
+                       zero_len_tail = offset - i_size_read(inode);
+               cluster_align_tail = !zero_len_tail;
+       }
+
+       /*
+        * when final_size > inode->i_size, inode->i_size will be
+        * updated after direct write, so add the inode to orphan
+        * dir first.
+        */
+       if (final_size > i_size_read(inode)) {
+               ret = ocfs2_add_inode_to_orphan(osb, inode);
+               if (ret < 0) {
+                       mlog_errno(ret);
+                       goto out;
+               }
+               orphaned = true;
+       }
+
+       if (append_write) {
+               ret = ocfs2_inode_lock(inode, NULL, 1);
+               if (ret < 0) {
+                       mlog_errno(ret);
+                       goto clean_orphan;
+               }
+
+               /* zeroing out the previously allocated cluster tail
+                * that but not zeroed */
+               if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
+                       down_read(&OCFS2_I(inode)->ip_alloc_sem);
+                       ret = ocfs2_direct_IO_zero_extend(osb, inode, offset,
+                                       zero_len_tail, cluster_align_tail);
+                       up_read(&OCFS2_I(inode)->ip_alloc_sem);
+               } else {
+                       down_write(&OCFS2_I(inode)->ip_alloc_sem);
+                       ret = ocfs2_direct_IO_extend_no_holes(osb, inode,
+                                       offset);
+                       up_write(&OCFS2_I(inode)->ip_alloc_sem);
+               }
+               if (ret < 0) {
+                       mlog_errno(ret);
+                       ocfs2_inode_unlock(inode, 1);
+                       goto clean_orphan;
+               }
+
+               is_overwrite = ocfs2_is_overwrite(osb, inode, offset);
+               if (is_overwrite < 0) {
+                       mlog_errno(is_overwrite);
+                       ret = is_overwrite;
+                       ocfs2_inode_unlock(inode, 1);
+                       goto clean_orphan;
+               }
+
+               ocfs2_inode_unlock(inode, 1);
+       }
+
+       written = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev, iter,
+                                      offset, ocfs2_direct_IO_get_blocks,
+                                      ocfs2_dio_end_io, NULL, 0);
+       /* overwrite aio may return -EIOCBQUEUED, and it is not an error */
+       if ((written < 0) && (written != -EIOCBQUEUED)) {
+               loff_t i_size = i_size_read(inode);
+
+               if (offset + count > i_size) {
+                       ret = ocfs2_inode_lock(inode, &di_bh, 1);
+                       if (ret < 0) {
+                               mlog_errno(ret);
+                               goto clean_orphan;
+                       }
+
+                       if (i_size == i_size_read(inode)) {
+                               ret = ocfs2_truncate_file(inode, di_bh,
+                                               i_size);
+                               if (ret < 0) {
+                                       if (ret != -ENOSPC)
+                                               mlog_errno(ret);
+
+                                       ocfs2_inode_unlock(inode, 1);
+                                       brelse(di_bh);
+                                       di_bh = NULL;
+                                       goto clean_orphan;
+                               }
+                       }
+
+                       ocfs2_inode_unlock(inode, 1);
+                       brelse(di_bh);
+                       di_bh = NULL;
+
+                       ret = jbd2_journal_force_commit(journal);
+                       if (ret < 0)
+                               mlog_errno(ret);
+               }
+       } else if (written > 0 && append_write && !is_overwrite &&
+                       !cluster_align_head) {
+               /* zeroing out the allocated cluster head */
+               u32 p_cpos = 0;
+               u32 v_cpos = ocfs2_bytes_to_clusters(osb->sb, offset);
+
+               ret = ocfs2_inode_lock(inode, NULL, 0);
+               if (ret < 0) {
+                       mlog_errno(ret);
+                       goto clean_orphan;
+               }
+
+               ret = ocfs2_get_clusters(inode, v_cpos, &p_cpos,
+                               &num_clusters, &ext_flags);
+               if (ret < 0) {
+                       mlog_errno(ret);
+                       ocfs2_inode_unlock(inode, 0);
+                       goto clean_orphan;
+               }
+
+               BUG_ON(!p_cpos || (ext_flags & OCFS2_EXT_UNWRITTEN));
+
+               ret = blkdev_issue_zeroout(osb->sb->s_bdev,
+                               (u64)p_cpos << (osb->s_clustersize_bits - 9),
+                               zero_len_head >> 9, GFP_NOFS, false);
+               if (ret < 0)
+                       mlog_errno(ret);
+
+               ocfs2_inode_unlock(inode, 0);
+       }
+
+clean_orphan:
+       if (orphaned) {
+               int tmp_ret;
+               int update_isize = written > 0 ? 1 : 0;
+               loff_t end = update_isize ? offset + written : 0;
+
+               tmp_ret = ocfs2_inode_lock(inode, &di_bh, 1);
+               if (tmp_ret < 0) {
+                       ret = tmp_ret;
+                       mlog_errno(ret);
+                       goto out;
+               }
+
+               tmp_ret = ocfs2_del_inode_from_orphan(osb, inode, di_bh,
+                               update_isize, end);
+               if (tmp_ret < 0) {
+                       ocfs2_inode_unlock(inode, 1);
+                       ret = tmp_ret;
+                       mlog_errno(ret);
+                       brelse(di_bh);
+                       goto out;
+               }
+
+               ocfs2_inode_unlock(inode, 1);
+               brelse(di_bh);
+
+               tmp_ret = jbd2_journal_force_commit(journal);
+               if (tmp_ret < 0) {
+                       ret = tmp_ret;
+                       mlog_errno(tmp_ret);
+               }
+       }
+
+out:
+       if (ret >= 0)
+               ret = written;
+       return ret;
+}
+
+static ssize_t ocfs2_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
+                              loff_t offset)
+{
+       struct file *file = iocb->ki_filp;
+       struct inode *inode = file_inode(file)->i_mapping->host;
+       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
+       int full_coherency = !(osb->s_mount_opt &
+                       OCFS2_MOUNT_COHERENCY_BUFFERED);
 
        /*
         * Fallback to buffered I/O if we see an inode without
@@ -635,14 +995,19 @@ static ssize_t ocfs2_direct_IO(int rw,
        if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
                return 0;
 
-       /* Fallback to buffered I/O if we are appending. */
-       if (i_size_read(inode) <= offset)
+       /* Fallback to buffered I/O if we are appending and
+        * concurrent O_DIRECT writes are allowed.
+        */
+       if (i_size_read(inode) <= offset && !full_coherency)
                return 0;
 
-       return __blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev,
-                                   iov, offset, nr_segs,
-                                   ocfs2_direct_IO_get_blocks,
-                                   ocfs2_dio_end_io, NULL, 0);
+       if (iov_iter_rw(iter) == READ)
+               return __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
+                                           iter, offset,
+                                           ocfs2_direct_IO_get_blocks,
+                                           ocfs2_dio_end_io, NULL, 0);
+       else
+               return ocfs2_direct_IO_write(iocb, iter, offset);
 }
 
 static void ocfs2_figure_cluster_boundaries(struct ocfs2_super *osb,
@@ -917,7 +1282,7 @@ void ocfs2_unlock_and_free_pages(struct page **pages, int num_pages)
        }
 }
 
-static void ocfs2_free_write_ctxt(struct ocfs2_write_ctxt *wc)
+static void ocfs2_unlock_pages(struct ocfs2_write_ctxt *wc)
 {
        int i;
 
@@ -938,7 +1303,11 @@ static void ocfs2_free_write_ctxt(struct ocfs2_write_ctxt *wc)
                page_cache_release(wc->w_target_page);
        }
        ocfs2_unlock_and_free_pages(wc->w_pages, wc->w_num_pages);
+}
 
+static void ocfs2_free_write_ctxt(struct ocfs2_write_ctxt *wc)
+{
+       ocfs2_unlock_pages(wc);
        brelse(wc->w_di_bh);
        kfree(wc);
 }
@@ -1274,7 +1643,7 @@ static int ocfs2_write_cluster(struct address_space *mapping,
        ret = ocfs2_extent_map_get_blocks(inode, v_blkno, &p_blkno, NULL,
                                          NULL);
        if (ret < 0) {
-               ocfs2_error(inode->i_sb, "Corrupting extend for inode %llu, "
+               mlog(ML_ERROR, "Get physical blkno failed for inode %llu, "
                            "at logical block %llu",
                            (unsigned long long)OCFS2_I(inode)->ip_blkno,
                            (unsigned long long)v_blkno);
@@ -1504,8 +1873,16 @@ static int ocfs2_write_begin_inline(struct address_space *mapping,
        handle_t *handle;
        struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
 
+       handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
+       if (IS_ERR(handle)) {
+               ret = PTR_ERR(handle);
+               mlog_errno(ret);
+               goto out;
+       }
+
        page = find_or_create_page(mapping, 0, GFP_NOFS);
        if (!page) {
+               ocfs2_commit_trans(osb, handle);
                ret = -ENOMEM;
                mlog_errno(ret);
                goto out;
@@ -1517,13 +1894,6 @@ static int ocfs2_write_begin_inline(struct address_space *mapping,
        wc->w_pages[0] = wc->w_target_page = page;
        wc->w_num_pages = 1;
 
-       handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
-       if (IS_ERR(handle)) {
-               ret = PTR_ERR(handle);
-               mlog_errno(ret);
-               goto out;
-       }
-
        ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), wc->w_di_bh,
                                      OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret) {
@@ -1756,7 +2126,7 @@ try_again:
                goto out;
        } else if (ret == 1) {
                clusters_need = wc->w_clen;
-               ret = ocfs2_refcount_cow(inode, filp, di_bh,
+               ret = ocfs2_refcount_cow(inode, di_bh,
                                         wc->w_cpos, wc->w_clen, UINT_MAX);
                if (ret) {
                        mlog_errno(ret);
@@ -1807,8 +2177,7 @@ try_again:
                        data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
 
                credits = ocfs2_calc_extend_credits(inode->i_sb,
-                                                   &di->id2.i_list,
-                                                   clusters_to_alloc);
+                                                   &di->id2.i_list);
 
        }
 
@@ -1841,10 +2210,7 @@ try_again:
                if (ret)
                        goto out_commit;
        }
-       /*
-        * We don't want this to fail in ocfs2_write_end(), so do it
-        * here.
-        */
+
        ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), wc->w_di_bh,
                                      OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret) {
@@ -1902,10 +2268,14 @@ out_commit:
 out:
        ocfs2_free_write_ctxt(wc);
 
-       if (data_ac)
+       if (data_ac) {
                ocfs2_free_alloc_context(data_ac);
-       if (meta_ac)
+               data_ac = NULL;
+       }
+       if (meta_ac) {
                ocfs2_free_alloc_context(meta_ac);
+               meta_ac = NULL;
+       }
 
        if (ret == -ENOSPC && try_free) {
                /*
@@ -1997,7 +2367,7 @@ int ocfs2_write_end_nolock(struct address_space *mapping,
                           loff_t pos, unsigned len, unsigned copied,
                           struct page *page, void *fsdata)
 {
-       int i;
+       int i, ret;
        unsigned from, to, start = pos & (PAGE_CACHE_SIZE - 1);
        struct inode *inode = mapping->host;
        struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
@@ -2006,6 +2376,14 @@ int ocfs2_write_end_nolock(struct address_space *mapping,
        handle_t *handle = wc->w_handle;
        struct page *tmppage;
 
+       ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), wc->w_di_bh,
+                       OCFS2_JOURNAL_ACCESS_WRITE);
+       if (ret) {
+               copied = ret;
+               mlog_errno(ret);
+               goto out;
+       }
+
        if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
                ocfs2_write_end_inline(inode, pos, len, &copied, di, wc);
                goto out_write_size;
@@ -2049,7 +2427,7 @@ int ocfs2_write_end_nolock(struct address_space *mapping,
 
 out_write_size:
        pos += copied;
-       if (pos > inode->i_size) {
+       if (pos > i_size_read(inode)) {
                i_size_write(inode, pos);
                mark_inode_dirty(inode);
        }
@@ -2058,13 +2436,23 @@ out_write_size:
        inode->i_mtime = inode->i_ctime = CURRENT_TIME;
        di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
        di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
+       ocfs2_update_inode_fsync_trans(handle, inode, 1);
        ocfs2_journal_dirty(handle, wc->w_di_bh);
 
+out:
+       /* unlock pages before dealloc since it needs acquiring j_trans_barrier
+        * lock, or it will cause a deadlock since journal commit threads holds
+        * this lock and will ask for the page lock when flushing the data.
+        * put it here to preserve the unlock order.
+        */
+       ocfs2_unlock_pages(wc);
+
        ocfs2_commit_trans(osb, handle);
 
        ocfs2_run_deallocs(osb, &wc->w_dealloc);
 
-       ocfs2_free_write_ctxt(wc);
+       brelse(wc->w_di_bh);
+       kfree(wc);
 
        return copied;
 }
@@ -2092,7 +2480,7 @@ const struct address_space_operations ocfs2_aops = {
        .write_end              = ocfs2_write_end,
        .bmap                   = ocfs2_bmap,
        .direct_IO              = ocfs2_direct_IO,
-       .invalidatepage         = ocfs2_invalidatepage,
+       .invalidatepage         = block_invalidatepage,
        .releasepage            = ocfs2_releasepage,
        .migratepage            = buffer_migrate_page,
        .is_partially_uptodate  = block_is_partially_uptodate,