Merge branch 'for-linus' of git://gitorious.org/linux-omap-dss2/linux
[safe/jmp/linux-2.6] / fs / ocfs2 / file.c
index 62442e4..06ccf6a 100644 (file)
@@ -59,6 +59,7 @@
 #include "xattr.h"
 #include "acl.h"
 #include "quota.h"
+#include "refcounttree.h"
 
 #include "buffer_head_io.h"
 
@@ -259,7 +260,7 @@ int ocfs2_update_inode_atime(struct inode *inode,
                goto out;
        }
 
-       ret = ocfs2_journal_access_di(handle, inode, bh,
+       ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
                                      OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret) {
                mlog_errno(ret);
@@ -334,6 +335,39 @@ out:
        return ret;
 }
 
+static int ocfs2_cow_file_pos(struct inode *inode,
+                             struct buffer_head *fe_bh,
+                             u64 offset)
+{
+       int status;
+       u32 phys, cpos = offset >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
+       unsigned int num_clusters = 0;
+       unsigned int ext_flags = 0;
+
+       /*
+        * If the new offset is aligned to the range of the cluster, there is
+        * no space for ocfs2_zero_range_for_truncate to fill, so no need to
+        * CoW either.
+        */
+       if ((offset & (OCFS2_SB(inode->i_sb)->s_clustersize - 1)) == 0)
+               return 0;
+
+       status = ocfs2_get_clusters(inode, cpos, &phys,
+                                   &num_clusters, &ext_flags);
+       if (status) {
+               mlog_errno(status);
+               goto out;
+       }
+
+       if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
+               goto out;
+
+       return ocfs2_refcount_cow(inode, fe_bh, cpos, 1, cpos+1);
+
+out:
+       return status;
+}
+
 static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
                                     struct inode *inode,
                                     struct buffer_head *fe_bh,
@@ -346,6 +380,17 @@ static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
 
        mlog_entry_void();
 
+       /*
+        * We need to CoW the cluster contains the offset if it is reflinked
+        * since we will call ocfs2_zero_range_for_truncate later which will
+        * write "0" from offset to the end of the cluster.
+        */
+       status = ocfs2_cow_file_pos(inode, fe_bh, new_i_size);
+       if (status) {
+               mlog_errno(status);
+               return status;
+       }
+
        /* TODO: This needs to actually orphan the inode in this
         * transaction. */
 
@@ -356,7 +401,7 @@ static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
                goto out;
        }
 
-       status = ocfs2_journal_access_di(handle, inode, fe_bh,
+       status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
                                         OCFS2_JOURNAL_ACCESS_WRITE);
        if (status < 0) {
                mlog_errno(status);
@@ -486,6 +531,8 @@ bail_unlock_sem:
        up_write(&OCFS2_I(inode)->ip_alloc_sem);
 
 bail:
+       if (!status && OCFS2_I(inode)->ip_clusters == 0)
+               status = ocfs2_try_remove_refcount_tree(inode, di_bh);
 
        mlog_exit(status);
        return status;
@@ -515,11 +562,10 @@ int ocfs2_add_inode_data(struct ocfs2_super *osb,
        int ret;
        struct ocfs2_extent_tree et;
 
-       ocfs2_init_dinode_extent_tree(&et, inode, fe_bh);
-       ret = ocfs2_add_clusters_in_btree(osb, inode, logical_offset,
-                                          clusters_to_add, mark_unwritten,
-                                          &et, handle,
-                                          data_ac, meta_ac, reason_ret);
+       ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), fe_bh);
+       ret = ocfs2_add_clusters_in_btree(handle, &et, logical_offset,
+                                         clusters_to_add, mark_unwritten,
+                                         data_ac, meta_ac, reason_ret);
 
        return ret;
 }
@@ -564,7 +610,7 @@ restart_all:
             (unsigned long long)OCFS2_I(inode)->ip_blkno,
             (long long)i_size_read(inode), le32_to_cpu(fe->i_clusters),
             clusters_to_add);
-       ocfs2_init_dinode_extent_tree(&et, inode, bh);
+       ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), bh);
        status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
                                       &data_ac, &meta_ac);
        if (status) {
@@ -593,7 +639,7 @@ restarted_transaction:
        /* reserve a write to the file entry early on - that we if we
         * run out of credits in the allocation path, we can still
         * update i_size. */
-       status = ocfs2_journal_access_di(handle, inode, bh,
+       status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
                                         OCFS2_JOURNAL_ACCESS_WRITE);
        if (status < 0) {
                mlog_errno(status);
@@ -1131,7 +1177,7 @@ static int __ocfs2_write_remove_suid(struct inode *inode,
                goto out;
        }
 
-       ret = ocfs2_journal_access_di(handle, inode, bh,
+       ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
                                      OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret < 0) {
                mlog_errno(ret);
@@ -1395,7 +1441,7 @@ static int ocfs2_remove_inode_range(struct inode *inode,
        struct address_space *mapping = inode->i_mapping;
        struct ocfs2_extent_tree et;
 
-       ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
+       ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
        ocfs2_init_dealloc_ctxt(&dealloc);
 
        if (byte_len == 0)
@@ -1657,11 +1703,77 @@ static long ocfs2_fallocate(struct inode *inode, int mode, loff_t offset,
                                         OCFS2_IOC_RESVSP64, &sr, change_size);
 }
 
+int ocfs2_check_range_for_refcount(struct inode *inode, loff_t pos,
+                                  size_t count)
+{
+       int ret = 0;
+       unsigned int extent_flags;
+       u32 cpos, clusters, extent_len, phys_cpos;
+       struct super_block *sb = inode->i_sb;
+
+       if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)) ||
+           !(OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) ||
+           OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
+               return 0;
+
+       cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
+       clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
+
+       while (clusters) {
+               ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
+                                        &extent_flags);
+               if (ret < 0) {
+                       mlog_errno(ret);
+                       goto out;
+               }
+
+               if (phys_cpos && (extent_flags & OCFS2_EXT_REFCOUNTED)) {
+                       ret = 1;
+                       break;
+               }
+
+               if (extent_len > clusters)
+                       extent_len = clusters;
+
+               clusters -= extent_len;
+               cpos += extent_len;
+       }
+out:
+       return ret;
+}
+
+static int ocfs2_prepare_inode_for_refcount(struct inode *inode,
+                                           loff_t pos, size_t count,
+                                           int *meta_level)
+{
+       int ret;
+       struct buffer_head *di_bh = NULL;
+       u32 cpos = pos >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
+       u32 clusters =
+               ocfs2_clusters_for_bytes(inode->i_sb, pos + count) - cpos;
+
+       ret = ocfs2_inode_lock(inode, &di_bh, 1);
+       if (ret) {
+               mlog_errno(ret);
+               goto out;
+       }
+
+       *meta_level = 1;
+
+       ret = ocfs2_refcount_cow(inode, di_bh, cpos, clusters, UINT_MAX);
+       if (ret)
+               mlog_errno(ret);
+out:
+       brelse(di_bh);
+       return ret;
+}
+
 static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
                                         loff_t *ppos,
                                         size_t count,
                                         int appending,
-                                        int *direct_io)
+                                        int *direct_io,
+                                        int *has_refcount)
 {
        int ret = 0, meta_level = 0;
        struct inode *inode = dentry->d_inode;
@@ -1713,6 +1825,24 @@ static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
 
                end = saved_pos + count;
 
+               ret = ocfs2_check_range_for_refcount(inode, saved_pos, count);
+               if (ret == 1) {
+                       ocfs2_inode_unlock(inode, meta_level);
+                       meta_level = -1;
+
+                       ret = ocfs2_prepare_inode_for_refcount(inode,
+                                                              saved_pos,
+                                                              count,
+                                                              &meta_level);
+                       if (has_refcount)
+                               *has_refcount = 1;
+               }
+
+               if (ret < 0) {
+                       mlog_errno(ret);
+                       goto out_unlock;
+               }
+
                /*
                 * Skip the O_DIRECT checks if we don't need
                 * them.
@@ -1729,6 +1859,10 @@ static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
                        break;
                }
 
+               if (has_refcount && *has_refcount == 1) {
+                       *direct_io = 0;
+                       break;
+               }
                /*
                 * Allowing concurrent direct writes means
                 * i_size changes wouldn't be synchronized, so
@@ -1759,7 +1893,8 @@ static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
                *ppos = saved_pos;
 
 out_unlock:
-       ocfs2_inode_unlock(inode, meta_level);
+       if (meta_level >= 0)
+               ocfs2_inode_unlock(inode, meta_level);
 
 out:
        return ret;
@@ -1771,7 +1906,7 @@ static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
                                    loff_t pos)
 {
        int ret, direct_io, appending, rw_level, have_alloc_sem  = 0;
-       int can_do_direct;
+       int can_do_direct, has_refcount = 0;
        ssize_t written = 0;
        size_t ocount;          /* original count */
        size_t count;           /* after file limit checks */
@@ -1814,7 +1949,7 @@ relock:
        can_do_direct = direct_io;
        ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
                                            iocb->ki_left, appending,
-                                           &can_do_direct);
+                                           &can_do_direct, &has_refcount);
        if (ret < 0) {
                mlog_errno(ret);
                goto out;
@@ -1851,6 +1986,7 @@ relock:
                if (ret)
                        goto out_dio;
 
+               count = ocount;
                ret = generic_write_checks(file, ppos, &count,
                                           S_ISBLK(inode->i_mode));
                if (ret)
@@ -1870,27 +2006,31 @@ relock:
                        goto out_dio;
                }
        } else {
-               written = generic_file_aio_write_nolock(iocb, iov, nr_segs,
-                                                       *ppos);
+               written = __generic_file_aio_write(iocb, iov, nr_segs, ppos);
        }
 
 out_dio:
        /* buffered aio wouldn't have proper lock coverage today */
        BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
 
-       if ((file->f_flags & O_SYNC && !direct_io) || IS_SYNC(inode)) {
-               /*
-                * The generic write paths have handled getting data
-                * to disk, but since we don't make use of the dirty
-                * inode list, a manual journal commit is necessary
-                * here.
-                */
-               if (old_size != i_size_read(inode) ||
-                   old_clusters != OCFS2_I(inode)->ip_clusters) {
+       if ((file->f_flags & O_DSYNC && !direct_io) || IS_SYNC(inode) ||
+           (file->f_flags & O_DIRECT && has_refcount)) {
+               ret = filemap_fdatawrite_range(file->f_mapping, pos,
+                                              pos + count - 1);
+               if (ret < 0)
+                       written = ret;
+
+               if (!ret && (old_size != i_size_read(inode) ||
+                   old_clusters != OCFS2_I(inode)->ip_clusters ||
+                   has_refcount)) {
                        ret = jbd2_journal_force_commit(osb->journal->j_journal);
                        if (ret < 0)
                                written = ret;
                }
+
+               if (!ret)
+                       ret = filemap_fdatawait_range(file->f_mapping, pos,
+                                                     pos + count - 1);
        }
 
        /* 
@@ -1918,8 +2058,10 @@ out_sems:
 
        mutex_unlock(&inode->i_mutex);
 
+       if (written)
+               ret = written;
        mlog_exit(ret);
-       return written ? written : ret;
+       return ret;
 }
 
 static int ocfs2_splice_to_file(struct pipe_inode_info *pipe,
@@ -1929,7 +2071,7 @@ static int ocfs2_splice_to_file(struct pipe_inode_info *pipe,
        int ret;
 
        ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, &sd->pos,
-                                           sd->total_len, 0, NULL);
+                                           sd->total_len, 0, NULL, NULL);
        if (ret < 0) {
                mlog_errno(ret);
                return ret;
@@ -1988,31 +2130,16 @@ static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
 
        if (ret > 0) {
                unsigned long nr_pages;
+               int err;
 
-               *ppos += ret;
                nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
 
-               /*
-                * If file or inode is SYNC and we actually wrote some data,
-                * sync it.
-                */
-               if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
-                       int err;
-
-                       mutex_lock(&inode->i_mutex);
-                       err = ocfs2_rw_lock(inode, 1);
-                       if (err < 0) {
-                               mlog_errno(err);
-                       } else {
-                               err = generic_osync_inode(inode, mapping,
-                                                 OSYNC_METADATA|OSYNC_DATA);
-                               ocfs2_rw_unlock(inode, 1);
-                       }
-                       mutex_unlock(&inode->i_mutex);
+               err = generic_write_sync(out, *ppos, ret);
+               if (err)
+                       ret = err;
+               else
+                       *ppos += ret;
 
-                       if (err)
-                               ret = err;
-               }
                balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
        }