nfsd: Use write gathering only with NFSv2
[safe/jmp/linux-2.6] / fs / gfs2 / ops_address.c
index ae782d2..a6dde17 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
- * Copyright (C) 2004-2007 Red Hat, Inc.  All rights reserved.
+ * Copyright (C) 2004-2008 Red Hat, Inc.  All rights reserved.
  *
  * This copyrighted material is made available to anyone wishing to use,
  * modify, copy, or redistribute it subject to the terms and conditions
@@ -19,9 +19,7 @@
 #include <linux/writeback.h>
 #include <linux/swap.h>
 #include <linux/gfs2_ondisk.h>
-#include <linux/lm_interface.h>
-#include <linux/swap.h>
-#include <linux/pagevec.h>
+#include <linux/backing-dev.h>
 
 #include "gfs2.h"
 #include "incore.h"
@@ -59,22 +57,6 @@ static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
 }
 
 /**
- * gfs2_get_block - Fills in a buffer head with details about a block
- * @inode: The inode
- * @lblock: The block number to look up
- * @bh_result: The buffer head to return the result in
- * @create: Non-zero if we may add block to the file
- *
- * Returns: errno
- */
-
-int gfs2_get_block(struct inode *inode, sector_t lblock,
-                  struct buffer_head *bh_result, int create)
-{
-       return gfs2_block_map(inode, lblock, create, bh_result);
-}
-
-/**
  * gfs2_get_block_noalloc - Fills in a buffer head with details about a block
  * @inode: The inode
  * @lblock: The block number to look up
@@ -89,7 +71,7 @@ static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
 {
        int error;
 
-       error = gfs2_block_map(inode, lblock, 0, bh_result);
+       error = gfs2_block_map(inode, lblock, bh_result, 0);
        if (error)
                return error;
        if (!buffer_mapped(bh_result))
@@ -100,7 +82,7 @@ static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
 static int gfs2_get_block_direct(struct inode *inode, sector_t lblock,
                                 struct buffer_head *bh_result, int create)
 {
-       return gfs2_block_map(inode, lblock, 0, bh_result);
+       return gfs2_block_map(inode, lblock, bh_result, 0);
 }
 
 /**
@@ -120,11 +102,9 @@ static int gfs2_writepage_common(struct page *page,
        loff_t i_size = i_size_read(inode);
        pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
        unsigned offset;
-       int ret = -EIO;
 
        if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl)))
                goto out;
-       ret = 0;
        if (current->journal_info)
                goto redirty;
        /* Is the page fully outside i_size? (truncate in progress) */
@@ -229,25 +209,23 @@ static int gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc
 {
        struct inode *inode = page->mapping->host;
        struct gfs2_sbd *sdp = GFS2_SB(inode);
-       int error;
+       int ret;
        int done_trans = 0;
 
-       error = gfs2_writepage_common(page, wbc);
-       if (error <= 0)
-               return error;
-
        if (PageChecked(page)) {
                if (wbc->sync_mode != WB_SYNC_ALL)
                        goto out_ignore;
-               error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
-               if (error)
+               ret = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
+               if (ret)
                        goto out_ignore;
                done_trans = 1;
        }
-       error = __gfs2_jdata_writepage(page, wbc);
+       ret = gfs2_writepage_common(page, wbc);
+       if (ret > 0)
+               ret = __gfs2_jdata_writepage(page, wbc);
        if (done_trans)
                gfs2_trans_end(sdp);
-       return error;
+       return ret;
 
 out_ignore:
        redirty_page_for_writepage(wbc, page);
@@ -296,7 +274,7 @@ static int gfs2_write_jdata_pagevec(struct address_space *mapping,
        int i;
        int ret;
 
-       ret = gfs2_trans_begin(sdp, nrblocks, 0);
+       ret = gfs2_trans_begin(sdp, nrblocks, nrblocks);
        if (ret < 0)
                return ret;
 
@@ -457,12 +435,13 @@ static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
        int error;
 
        /*
-        * Due to the order of unstuffing files and ->nopage(), we can be
+        * Due to the order of unstuffing files and ->fault(), we can be
         * asked for a zero page in the case of a stuffed file being extended,
         * so we need to supply one here. It doesn't happen often.
         */
        if (unlikely(page->index)) {
-               zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
+               zero_user(page, 0, PAGE_CACHE_SIZE);
+               SetPageUptodate(page);
                return 0;
        }
 
@@ -472,8 +451,8 @@ static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
 
        kaddr = kmap_atomic(page, KM_USER0);
        memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
-              ip->i_di.di_size);
-       memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size);
+              ip->i_disksize);
+       memset(kaddr + ip->i_disksize, 0, PAGE_CACHE_SIZE - ip->i_disksize);
        kunmap_atomic(kaddr, KM_USER0);
        flush_dcache_page(page);
        brelse(dibh);
@@ -504,7 +483,7 @@ static int __gfs2_readpage(void *file, struct page *page)
                error = stuffed_readpage(ip, page);
                unlock_page(page);
        } else {
-               error = mpage_readpage(page, gfs2_get_block);
+               error = mpage_readpage(page, gfs2_block_map);
        }
 
        if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
@@ -518,31 +497,34 @@ static int __gfs2_readpage(void *file, struct page *page)
  * @file: The file to read
  * @page: The page of the file
  *
- * This deals with the locking required. We use a trylock in order to
- * avoid the page lock / glock ordering problems returning AOP_TRUNCATED_PAGE
- * in the event that we are unable to get the lock.
+ * This deals with the locking required. We have to unlock and
+ * relock the page in order to get the locking in the right
+ * order.
  */
 
 static int gfs2_readpage(struct file *file, struct page *page)
 {
-       struct gfs2_inode *ip = GFS2_I(page->mapping->host);
+       struct address_space *mapping = page->mapping;
+       struct gfs2_inode *ip = GFS2_I(mapping->host);
        struct gfs2_holder gh;
        int error;
 
-       gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|LM_FLAG_TRY_1CB, &gh);
-       error = gfs2_glock_nq_atime(&gh);
-       if (unlikely(error)) {
-               unlock_page(page);
+       unlock_page(page);
+       gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
+       error = gfs2_glock_nq(&gh);
+       if (unlikely(error))
                goto out;
-       }
-       error = __gfs2_readpage(file, page);
+       error = AOP_TRUNCATED_PAGE;
+       lock_page(page);
+       if (page->mapping == mapping && !PageUptodate(page))
+               error = __gfs2_readpage(file, page);
+       else
+               unlock_page(page);
        gfs2_glock_dq(&gh);
 out:
        gfs2_holder_uninit(&gh);
-       if (error == GLR_TRYFAILED) {
-               yield();
-               return AOP_TRUNCATED_PAGE;
-       }
+       if (error && error != AOP_TRUNCATED_PAGE)
+               lock_page(page);
        return error;
 }
 
@@ -598,7 +580,7 @@ int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
  *    Any I/O we ignore at this time will be done via readpage later.
  * 2. We don't handle stuffed files here we let readpage do the honours.
  * 3. mpage_readpages() does most of the heavy lifting in the common case.
- * 4. gfs2_get_block() is relied upon to set BH_Boundary in the right places.
+ * 4. gfs2_block_map() is relied upon to set BH_Boundary in the right places.
  */
 
 static int gfs2_readpages(struct file *file, struct address_space *mapping,
@@ -610,12 +592,12 @@ static int gfs2_readpages(struct file *file, struct address_space *mapping,
        struct gfs2_holder gh;
        int ret;
 
-       gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
-       ret = gfs2_glock_nq_atime(&gh);
+       gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
+       ret = gfs2_glock_nq(&gh);
        if (unlikely(ret))
                goto out_uninit;
        if (!gfs2_is_stuffed(ip))
-               ret = mpage_readpages(mapping, pages, nr_pages, gfs2_get_block);
+               ret = mpage_readpages(mapping, pages, nr_pages, gfs2_block_map);
        gfs2_glock_dq(&gh);
 out_uninit:
        gfs2_holder_uninit(&gh);
@@ -643,7 +625,7 @@ static int gfs2_write_begin(struct file *file, struct address_space *mapping,
 {
        struct gfs2_inode *ip = GFS2_I(mapping->host);
        struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
-       unsigned int data_blocks, ind_blocks, rblocks;
+       unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
        int alloc_required;
        int error = 0;
        struct gfs2_alloc *al;
@@ -652,28 +634,29 @@ static int gfs2_write_begin(struct file *file, struct address_space *mapping,
        unsigned to = from + len;
        struct page *page;
 
-       gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &ip->i_gh);
-       error = gfs2_glock_nq_atime(&ip->i_gh);
+       gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
+       error = gfs2_glock_nq(&ip->i_gh);
        if (unlikely(error))
                goto out_uninit;
 
-       gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks);
        error = gfs2_write_alloc_required(ip, pos, len, &alloc_required);
        if (error)
                goto out_unlock;
 
-       ip->i_alloc.al_requested = 0;
+       if (alloc_required || gfs2_is_jdata(ip))
+               gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks);
+
        if (alloc_required) {
                al = gfs2_alloc_get(ip);
+               if (!al) {
+                       error = -ENOMEM;
+                       goto out_unlock;
+               }
 
-               error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
+               error = gfs2_quota_lock_check(ip);
                if (error)
                        goto out_alloc_put;
 
-               error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
-               if (error)
-                       goto out_qunlock;
-
                al->al_requested = data_blocks + ind_blocks;
                error = gfs2_inplace_reserve(ip);
                if (error)
@@ -692,7 +675,8 @@ static int gfs2_write_begin(struct file *file, struct address_space *mapping,
                goto out_trans_fail;
 
        error = -ENOMEM;
-       page = __grab_cache_page(mapping, index);
+       flags |= AOP_FLAG_NOFS;
+       page = grab_cache_page_write_begin(mapping, index, flags);
        *pagep = page;
        if (unlikely(!page))
                goto out_endtrans;
@@ -710,7 +694,7 @@ static int gfs2_write_begin(struct file *file, struct address_space *mapping,
        }
 
 prepare_write:
-       error = block_prepare_write(page, from, to, gfs2_get_block);
+       error = block_prepare_write(page, from, to, gfs2_block_map);
 out:
        if (error == 0)
                return 0;
@@ -799,7 +783,7 @@ static int gfs2_stuffed_write_end(struct inode *inode, struct buffer_head *dibh,
 
        if (inode->i_size < to) {
                i_size_write(inode, to);
-               ip->i_di.di_size = inode->i_size;
+               ip->i_disksize = inode->i_size;
                di->di_size = cpu_to_be64(inode->i_size);
                mark_inode_dirty(inode);
        }
@@ -839,13 +823,13 @@ static int gfs2_write_end(struct file *file, struct address_space *mapping,
        struct gfs2_inode *ip = GFS2_I(inode);
        struct gfs2_sbd *sdp = GFS2_SB(inode);
        struct buffer_head *dibh;
-       struct gfs2_alloc *al = &ip->i_alloc;
+       struct gfs2_alloc *al = ip->i_alloc;
        struct gfs2_dinode *di;
        unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
        unsigned int to = from + len;
        int ret;
 
-       BUG_ON(gfs2_glock_is_locked_by_me(ip->i_gl) == 0);
+       BUG_ON(gfs2_glock_is_locked_by_me(ip->i_gl) == NULL);
 
        ret = gfs2_meta_inode_buffer(ip, &dibh);
        if (unlikely(ret)) {
@@ -864,14 +848,11 @@ static int gfs2_write_end(struct file *file, struct address_space *mapping,
 
        ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
 
-       if (likely(ret >= 0)) {
-               copied = ret;
-               if  ((pos + copied) > inode->i_size) {
-                       di = (struct gfs2_dinode *)dibh->b_data;
-                       ip->i_di.di_size = inode->i_size;
-                       di->di_size = cpu_to_be64(inode->i_size);
-                       mark_inode_dirty(inode);
-               }
+       if (likely(ret >= 0) && (inode->i_size > ip->i_disksize)) {
+               di = (struct gfs2_dinode *)dibh->b_data;
+               ip->i_disksize = inode->i_size;
+               di->di_size = cpu_to_be64(inode->i_size);
+               mark_inode_dirty(inode);
        }
 
        if (inode == sdp->sd_rindex)
@@ -880,7 +861,7 @@ static int gfs2_write_end(struct file *file, struct address_space *mapping,
        brelse(dibh);
        gfs2_trans_end(sdp);
 failed:
-       if (al->al_requested) {
+       if (al) {
                gfs2_inplace_release(ip);
                gfs2_quota_unlock(ip);
                gfs2_alloc_put(ip);
@@ -923,7 +904,7 @@ static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
                return 0;
 
        if (!gfs2_is_stuffed(ip))
-               dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
+               dblock = generic_block_bmap(mapping, lblock, gfs2_block_map);
 
        gfs2_glock_dq_uninit(&i_gh);
 
@@ -995,7 +976,7 @@ static int gfs2_ok_for_dio(struct gfs2_inode *ip, int rw, loff_t offset)
        if (gfs2_is_stuffed(ip))
                return 0;
 
-       if (offset > i_size_read(&ip->i_inode))
+       if (offset >= i_size_read(&ip->i_inode))
                return 0;
        return 1;
 }
@@ -1020,8 +1001,8 @@ static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
         * unfortunately have the option of only flushing a range like
         * the VFS does.
         */
-       gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, GL_ATIME, &gh);
-       rv = gfs2_glock_nq_atime(&gh);
+       gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, 0, &gh);
+       rv = gfs2_glock_nq(&gh);
        if (rv)
                return rv;
        rv = gfs2_ok_for_dio(ip, rw, offset);
@@ -1114,6 +1095,8 @@ static const struct address_space_operations gfs2_writeback_aops = {
        .invalidatepage = gfs2_invalidatepage,
        .releasepage = gfs2_releasepage,
        .direct_IO = gfs2_direct_IO,
+       .migratepage = buffer_migrate_page,
+       .is_partially_uptodate = block_is_partially_uptodate,
 };
 
 static const struct address_space_operations gfs2_ordered_aops = {
@@ -1128,6 +1111,8 @@ static const struct address_space_operations gfs2_ordered_aops = {
        .invalidatepage = gfs2_invalidatepage,
        .releasepage = gfs2_releasepage,
        .direct_IO = gfs2_direct_IO,
+       .migratepage = buffer_migrate_page,
+       .is_partially_uptodate = block_is_partially_uptodate,
 };
 
 static const struct address_space_operations gfs2_jdata_aops = {
@@ -1142,6 +1127,7 @@ static const struct address_space_operations gfs2_jdata_aops = {
        .bmap = gfs2_bmap,
        .invalidatepage = gfs2_invalidatepage,
        .releasepage = gfs2_releasepage,
+       .is_partially_uptodate = block_is_partially_uptodate,
 };
 
 void gfs2_set_aops(struct inode *inode)