nfsd: Use write gathering only with NFSv2
[safe/jmp/linux-2.6] / fs / gfs2 / ops_address.c
index dd1ea49..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
@@ -17,8 +17,9 @@
 #include <linux/mpage.h>
 #include <linux/fs.h>
 #include <linux/writeback.h>
+#include <linux/swap.h>
 #include <linux/gfs2_ondisk.h>
-#include <linux/lm_interface.h>
+#include <linux/backing-dev.h>
 
 #include "gfs2.h"
 #include "incore.h"
@@ -31,7 +32,6 @@
 #include "quota.h"
 #include "trans.h"
 #include "rgrp.h"
-#include "ops_file.h"
 #include "super.h"
 #include "util.h"
 #include "glops.h"
@@ -57,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
@@ -87,10 +71,10 @@ 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 (bh_result->b_blocknr == 0)
+       if (!buffer_mapped(bh_result))
                return -EIO;
        return 0;
 }
@@ -98,20 +82,19 @@ 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);
 }
 
 /**
- * gfs2_writepage - Write complete page
- * @page: Page to write
+ * gfs2_writepage_common - Common bits of writepage
+ * @page: The page to be written
+ * @wbc: The writeback control
  *
- * Returns: errno
- *
- * Some of this is copied from block_write_full_page() although we still
- * call it to do most of the work.
+ * Returns: 1 if writepage is ok, otherwise an error code or zero if no error.
  */
 
-static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
+static int gfs2_writepage_common(struct page *page,
+                                struct writeback_control *wbc)
 {
        struct inode *inode = page->mapping->host;
        struct gfs2_inode *ip = GFS2_I(inode);
@@ -119,42 +102,130 @@ static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
        loff_t i_size = i_size_read(inode);
        pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
        unsigned offset;
-       int error;
-       int done_trans = 0;
 
-       if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) {
-               unlock_page(page);
-               return -EIO;
-       }
+       if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl)))
+               goto out;
        if (current->journal_info)
-               goto out_ignore;
-
+               goto redirty;
        /* Is the page fully outside i_size? (truncate in progress) */
-        offset = i_size & (PAGE_CACHE_SIZE-1);
+       offset = i_size & (PAGE_CACHE_SIZE-1);
        if (page->index > end_index || (page->index == end_index && !offset)) {
                page->mapping->a_ops->invalidatepage(page, 0);
-               unlock_page(page);
-               return 0; /* don't care */
+               goto out;
+       }
+       return 1;
+redirty:
+       redirty_page_for_writepage(wbc, page);
+out:
+       unlock_page(page);
+       return 0;
+}
+
+/**
+ * gfs2_writeback_writepage - Write page for writeback mappings
+ * @page: The page
+ * @wbc: The writeback control
+ *
+ */
+
+static int gfs2_writeback_writepage(struct page *page,
+                                   struct writeback_control *wbc)
+{
+       int ret;
+
+       ret = gfs2_writepage_common(page, wbc);
+       if (ret <= 0)
+               return ret;
+
+       ret = mpage_writepage(page, gfs2_get_block_noalloc, wbc);
+       if (ret == -EAGAIN)
+               ret = block_write_full_page(page, gfs2_get_block_noalloc, wbc);
+       return ret;
+}
+
+/**
+ * gfs2_ordered_writepage - Write page for ordered data files
+ * @page: The page to write
+ * @wbc: The writeback control
+ *
+ */
+
+static int gfs2_ordered_writepage(struct page *page,
+                                 struct writeback_control *wbc)
+{
+       struct inode *inode = page->mapping->host;
+       struct gfs2_inode *ip = GFS2_I(inode);
+       int ret;
+
+       ret = gfs2_writepage_common(page, wbc);
+       if (ret <= 0)
+               return ret;
+
+       if (!page_has_buffers(page)) {
+               create_empty_buffers(page, inode->i_sb->s_blocksize,
+                                    (1 << BH_Dirty)|(1 << BH_Uptodate));
        }
+       gfs2_page_add_databufs(ip, page, 0, inode->i_sb->s_blocksize-1);
+       return block_write_full_page(page, gfs2_get_block_noalloc, wbc);
+}
 
-       if ((sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) &&
-           PageChecked(page)) {
+/**
+ * __gfs2_jdata_writepage - The core of jdata writepage
+ * @page: The page to write
+ * @wbc: The writeback control
+ *
+ * This is shared between writepage and writepages and implements the
+ * core of the writepage operation. If a transaction is required then
+ * PageChecked will have been set and the transaction will have
+ * already been started before this is called.
+ */
+
+static int __gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
+{
+       struct inode *inode = page->mapping->host;
+       struct gfs2_inode *ip = GFS2_I(inode);
+       struct gfs2_sbd *sdp = GFS2_SB(inode);
+
+       if (PageChecked(page)) {
                ClearPageChecked(page);
-               error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
-               if (error)
-                       goto out_ignore;
                if (!page_has_buffers(page)) {
                        create_empty_buffers(page, inode->i_sb->s_blocksize,
                                             (1 << BH_Dirty)|(1 << BH_Uptodate));
                }
                gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
+       }
+       return block_write_full_page(page, gfs2_get_block_noalloc, wbc);
+}
+
+/**
+ * gfs2_jdata_writepage - Write complete page
+ * @page: Page to write
+ *
+ * Returns: errno
+ *
+ */
+
+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 ret;
+       int done_trans = 0;
+
+       if (PageChecked(page)) {
+               if (wbc->sync_mode != WB_SYNC_ALL)
+                       goto out_ignore;
+               ret = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
+               if (ret)
+                       goto out_ignore;
                done_trans = 1;
        }
-       error = block_write_full_page(page, gfs2_get_block_noalloc, wbc);
+       ret = gfs2_writepage_common(page, wbc);
+       if (ret > 0)
+               ret = __gfs2_jdata_writepage(page, wbc);
        if (done_trans)
                gfs2_trans_end(sdp);
-       gfs2_meta_cache_flush(ip);
-       return error;
+       return ret;
 
 out_ignore:
        redirty_page_for_writepage(wbc, page);
@@ -163,29 +234,190 @@ out_ignore:
 }
 
 /**
- * gfs2_writepages - Write a bunch of dirty pages back to disk
+ * gfs2_writeback_writepages - Write a bunch of dirty pages back to disk
  * @mapping: The mapping to write
  * @wbc: Write-back control
  *
- * For journaled files and/or ordered writes this just falls back to the
- * kernel's default writepages path for now. We will probably want to change
- * that eventually (i.e. when we look at allocate on flush).
- *
- * For the data=writeback case though we can already ignore buffer heads
+ * For the data=writeback case we can already ignore buffer heads
  * and write whole extents at once. This is a big reduction in the
  * number of I/O requests we send and the bmap calls we make in this case.
  */
-static int gfs2_writepages(struct address_space *mapping,
-                          struct writeback_control *wbc)
+static int gfs2_writeback_writepages(struct address_space *mapping,
+                                    struct writeback_control *wbc)
+{
+       return mpage_writepages(mapping, wbc, gfs2_get_block_noalloc);
+}
+
+/**
+ * gfs2_write_jdata_pagevec - Write back a pagevec's worth of pages
+ * @mapping: The mapping
+ * @wbc: The writeback control
+ * @writepage: The writepage function to call for each page
+ * @pvec: The vector of pages
+ * @nr_pages: The number of pages to write
+ *
+ * Returns: non-zero if loop should terminate, zero otherwise
+ */
+
+static int gfs2_write_jdata_pagevec(struct address_space *mapping,
+                                   struct writeback_control *wbc,
+                                   struct pagevec *pvec,
+                                   int nr_pages, pgoff_t end)
 {
        struct inode *inode = mapping->host;
-       struct gfs2_inode *ip = GFS2_I(inode);
        struct gfs2_sbd *sdp = GFS2_SB(inode);
+       loff_t i_size = i_size_read(inode);
+       pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
+       unsigned offset = i_size & (PAGE_CACHE_SIZE-1);
+       unsigned nrblocks = nr_pages * (PAGE_CACHE_SIZE/inode->i_sb->s_blocksize);
+       struct backing_dev_info *bdi = mapping->backing_dev_info;
+       int i;
+       int ret;
+
+       ret = gfs2_trans_begin(sdp, nrblocks, nrblocks);
+       if (ret < 0)
+               return ret;
+
+       for(i = 0; i < nr_pages; i++) {
+               struct page *page = pvec->pages[i];
+
+               lock_page(page);
+
+               if (unlikely(page->mapping != mapping)) {
+                       unlock_page(page);
+                       continue;
+               }
+
+               if (!wbc->range_cyclic && page->index > end) {
+                       ret = 1;
+                       unlock_page(page);
+                       continue;
+               }
+
+               if (wbc->sync_mode != WB_SYNC_NONE)
+                       wait_on_page_writeback(page);
+
+               if (PageWriteback(page) ||
+                   !clear_page_dirty_for_io(page)) {
+                       unlock_page(page);
+                       continue;
+               }
+
+               /* Is the page fully outside i_size? (truncate in progress) */
+               if (page->index > end_index || (page->index == end_index && !offset)) {
+                       page->mapping->a_ops->invalidatepage(page, 0);
+                       unlock_page(page);
+                       continue;
+               }
+
+               ret = __gfs2_jdata_writepage(page, wbc);
+
+               if (ret || (--(wbc->nr_to_write) <= 0))
+                       ret = 1;
+               if (wbc->nonblocking && bdi_write_congested(bdi)) {
+                       wbc->encountered_congestion = 1;
+                       ret = 1;
+               }
+
+       }
+       gfs2_trans_end(sdp);
+       return ret;
+}
+
+/**
+ * gfs2_write_cache_jdata - Like write_cache_pages but different
+ * @mapping: The mapping to write
+ * @wbc: The writeback control
+ * @writepage: The writepage function to call
+ * @data: The data to pass to writepage
+ *
+ * The reason that we use our own function here is that we need to
+ * start transactions before we grab page locks. This allows us
+ * to get the ordering right.
+ */
 
-       if (sdp->sd_args.ar_data == GFS2_DATA_WRITEBACK && !gfs2_is_jdata(ip))
-               return mpage_writepages(mapping, wbc, gfs2_get_block_noalloc);
+static int gfs2_write_cache_jdata(struct address_space *mapping,
+                                 struct writeback_control *wbc)
+{
+       struct backing_dev_info *bdi = mapping->backing_dev_info;
+       int ret = 0;
+       int done = 0;
+       struct pagevec pvec;
+       int nr_pages;
+       pgoff_t index;
+       pgoff_t end;
+       int scanned = 0;
+       int range_whole = 0;
+
+       if (wbc->nonblocking && bdi_write_congested(bdi)) {
+               wbc->encountered_congestion = 1;
+               return 0;
+       }
 
-       return generic_writepages(mapping, wbc);
+       pagevec_init(&pvec, 0);
+       if (wbc->range_cyclic) {
+               index = mapping->writeback_index; /* Start from prev offset */
+               end = -1;
+       } else {
+               index = wbc->range_start >> PAGE_CACHE_SHIFT;
+               end = wbc->range_end >> PAGE_CACHE_SHIFT;
+               if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
+                       range_whole = 1;
+               scanned = 1;
+       }
+
+retry:
+        while (!done && (index <= end) &&
+               (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
+                                              PAGECACHE_TAG_DIRTY,
+                                              min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
+               scanned = 1;
+               ret = gfs2_write_jdata_pagevec(mapping, wbc, &pvec, nr_pages, end);
+               if (ret)
+                       done = 1;
+               if (ret > 0)
+                       ret = 0;
+
+               pagevec_release(&pvec);
+               cond_resched();
+       }
+
+       if (!scanned && !done) {
+               /*
+                * We hit the last page and there is more work to be done: wrap
+                * back to the start of the file
+                */
+               scanned = 1;
+               index = 0;
+               goto retry;
+       }
+
+       if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
+               mapping->writeback_index = index;
+       return ret;
+}
+
+
+/**
+ * gfs2_jdata_writepages - Write a bunch of dirty pages back to disk
+ * @mapping: The mapping to write
+ * @wbc: The writeback control
+ * 
+ */
+
+static int gfs2_jdata_writepages(struct address_space *mapping,
+                                struct writeback_control *wbc)
+{
+       struct gfs2_inode *ip = GFS2_I(mapping->host);
+       struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
+       int ret;
+
+       ret = gfs2_write_cache_jdata(mapping, wbc);
+       if (ret == 0 && wbc->sync_mode == WB_SYNC_ALL) {
+               gfs2_log_flush(sdp, ip->i_gl);
+               ret = gfs2_write_cache_jdata(mapping, wbc);
+       }
+       return ret;
 }
 
 /**
@@ -203,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;
        }
 
@@ -218,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);
@@ -230,62 +463,110 @@ static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
 
 
 /**
- * gfs2_readpage - readpage with locking
- * @file: The file to read a page for. N.B. This may be NULL if we are
- * reading an internal file.
+ * __gfs2_readpage - readpage
+ * @file: The file to read a page for
  * @page: The page to read
  *
- * Returns: errno
+ * This is the core of gfs2's readpage. Its used by the internal file
+ * reading code as in that case we already hold the glock. Also its
+ * called by gfs2_readpage() once the required lock has been granted.
+ *
  */
 
-static int gfs2_readpage(struct file *file, struct page *page)
+static int __gfs2_readpage(void *file, struct page *page)
 {
        struct gfs2_inode *ip = GFS2_I(page->mapping->host);
        struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
-       struct gfs2_file *gf = NULL;
-       struct gfs2_holder gh;
        int error;
-       int do_unlock = 0;
-
-       if (likely(file != &gfs2_internal_file_sentinel)) {
-               if (file) {
-                       gf = file->private_data;
-                       if (test_bit(GFF_EXLOCK, &gf->f_flags))
-                               /* gfs2_sharewrite_fault has grabbed the ip->i_gl already */
-                               goto skip_lock;
-               }
-               gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|LM_FLAG_TRY_1CB, &gh);
-               do_unlock = 1;
-               error = gfs2_glock_nq_atime(&gh);
-               if (unlikely(error))
-                       goto out_unlock;
-       }
 
-skip_lock:
        if (gfs2_is_stuffed(ip)) {
                error = stuffed_readpage(ip, page);
                unlock_page(page);
-       } else
-               error = mpage_readpage(page, gfs2_get_block);
+       } else {
+               error = mpage_readpage(page, gfs2_block_map);
+       }
 
        if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
-               error = -EIO;
+               return -EIO;
 
-       if (do_unlock) {
-               gfs2_glock_dq_m(1, &gh);
-               gfs2_holder_uninit(&gh);
-       }
-out:
        return error;
-out_unlock:
+}
+
+/**
+ * gfs2_readpage - read a page of a file
+ * @file: The file to read
+ * @page: The page of the file
+ *
+ * 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 address_space *mapping = page->mapping;
+       struct gfs2_inode *ip = GFS2_I(mapping->host);
+       struct gfs2_holder gh;
+       int error;
+
        unlock_page(page);
-       if (error == GLR_TRYFAILED) {
-               error = AOP_TRUNCATED_PAGE;
-               yield();
-       }
-       if (do_unlock)
-               gfs2_holder_uninit(&gh);
-       goto out;
+       gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
+       error = gfs2_glock_nq(&gh);
+       if (unlikely(error))
+               goto out;
+       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 && error != AOP_TRUNCATED_PAGE)
+               lock_page(page);
+       return error;
+}
+
+/**
+ * gfs2_internal_read - read an internal file
+ * @ip: The gfs2 inode
+ * @ra_state: The readahead state (or NULL for no readahead)
+ * @buf: The buffer to fill
+ * @pos: The file position
+ * @size: The amount to read
+ *
+ */
+
+int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
+                       char *buf, loff_t *pos, unsigned size)
+{
+       struct address_space *mapping = ip->i_inode.i_mapping;
+       unsigned long index = *pos / PAGE_CACHE_SIZE;
+       unsigned offset = *pos & (PAGE_CACHE_SIZE - 1);
+       unsigned copied = 0;
+       unsigned amt;
+       struct page *page;
+       void *p;
+
+       do {
+               amt = size - copied;
+               if (offset + size > PAGE_CACHE_SIZE)
+                       amt = PAGE_CACHE_SIZE - offset;
+               page = read_cache_page(mapping, index, __gfs2_readpage, NULL);
+               if (IS_ERR(page))
+                       return PTR_ERR(page);
+               p = kmap_atomic(page, KM_USER0);
+               memcpy(buf + copied, p + offset, amt);
+               kunmap_atomic(p, KM_USER0);
+               mark_page_accessed(page);
+               page_cache_release(page);
+               copied += amt;
+               index++;
+               offset = 0;
+       } while(copied < size);
+       (*pos) += size;
+       return size;
 }
 
 /**
@@ -299,10 +580,9 @@ out_unlock:
  *    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.
- * 5. We use LM_FLAG_TRY_1CB here, effectively we then have lock-ahead as
- *    well as read-ahead.
+ * 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,
                          struct list_head *pages, unsigned nr_pages)
 {
@@ -310,98 +590,73 @@ static int gfs2_readpages(struct file *file, struct address_space *mapping,
        struct gfs2_inode *ip = GFS2_I(inode);
        struct gfs2_sbd *sdp = GFS2_SB(inode);
        struct gfs2_holder gh;
-       int ret = 0;
-       int do_unlock = 0;
+       int ret;
 
-       if (likely(file != &gfs2_internal_file_sentinel)) {
-               if (file) {
-                       struct gfs2_file *gf = file->private_data;
-                       if (test_bit(GFF_EXLOCK, &gf->f_flags))
-                               goto skip_lock;
-               }
-               gfs2_holder_init(ip->i_gl, LM_ST_SHARED,
-                                LM_FLAG_TRY_1CB|GL_ATIME, &gh);
-               do_unlock = 1;
-               ret = gfs2_glock_nq_atime(&gh);
-               if (ret == GLR_TRYFAILED)
-                       goto out_noerror;
-               if (unlikely(ret))
-                       goto out_unlock;
-       }
-skip_lock:
+       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);
-
-       if (do_unlock) {
-               gfs2_glock_dq_m(1, &gh);
-               gfs2_holder_uninit(&gh);
-       }
-out:
+               ret = mpage_readpages(mapping, pages, nr_pages, gfs2_block_map);
+       gfs2_glock_dq(&gh);
+out_uninit:
+       gfs2_holder_uninit(&gh);
        if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
                ret = -EIO;
        return ret;
-out_noerror:
-       ret = 0;
-out_unlock:
-       if (do_unlock)
-               gfs2_holder_uninit(&gh);
-       goto out;
 }
 
 /**
- * gfs2_prepare_write - Prepare to write a page to a file
+ * gfs2_write_begin - Begin to write to a file
  * @file: The file to write to
- * @page: The page which is to be prepared for writing
- * @from: From (byte range within page)
- * @to: To (byte range within page)
+ * @mapping: The mapping in which to write
+ * @pos: The file offset at which to start writing
+ * @len: Length of the write
+ * @flags: Various flags
+ * @pagep: Pointer to return the page
+ * @fsdata: Pointer to return fs data (unused by GFS2)
  *
  * Returns: errno
  */
 
-static int gfs2_prepare_write(struct file *file, struct page *page,
-                             unsigned from, unsigned to)
+static int gfs2_write_begin(struct file *file, struct address_space *mapping,
+                           loff_t pos, unsigned len, unsigned flags,
+                           struct page **pagep, void **fsdata)
 {
-       struct gfs2_inode *ip = GFS2_I(page->mapping->host);
-       struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
-       unsigned int data_blocks, ind_blocks, rblocks;
+       struct gfs2_inode *ip = GFS2_I(mapping->host);
+       struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
+       unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
        int alloc_required;
        int error = 0;
-       loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + from;
-       loff_t end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
        struct gfs2_alloc *al;
-       unsigned int write_len = to - from;
-
-
-       gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME|LM_FLAG_TRY_1CB, &ip->i_gh);
-       error = gfs2_glock_nq_atime(&ip->i_gh);
-       if (unlikely(error)) {
-               if (error == GLR_TRYFAILED) {
-                       unlock_page(page);
-                       error = AOP_TRUNCATED_PAGE;
-                       yield();
-               }
+       pgoff_t index = pos >> PAGE_CACHE_SHIFT;
+       unsigned from = pos & (PAGE_CACHE_SIZE - 1);
+       unsigned to = from + len;
+       struct page *page;
+
+       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, write_len, &data_blocks, &ind_blocks);
 
-       error = gfs2_write_alloc_required(ip, pos, write_len, &alloc_required);
+       error = gfs2_write_alloc_required(ip, pos, len, &alloc_required);
        if (error)
                goto out_unlock;
 
+       if (alloc_required || gfs2_is_jdata(ip))
+               gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks);
 
-       ip->i_alloc.al_requested = 0;
        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)
@@ -414,40 +669,53 @@ static int gfs2_prepare_write(struct file *file, struct page *page,
        if (ind_blocks || data_blocks)
                rblocks += RES_STATFS + RES_QUOTA;
 
-       error = gfs2_trans_begin(sdp, rblocks, 0);
+       error = gfs2_trans_begin(sdp, rblocks,
+                                PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
        if (error)
                goto out_trans_fail;
 
+       error = -ENOMEM;
+       flags |= AOP_FLAG_NOFS;
+       page = grab_cache_page_write_begin(mapping, index, flags);
+       *pagep = page;
+       if (unlikely(!page))
+               goto out_endtrans;
+
        if (gfs2_is_stuffed(ip)) {
-               if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
+               error = 0;
+               if (pos + len > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
                        error = gfs2_unstuff_dinode(ip, page);
                        if (error == 0)
                                goto prepare_write;
-               } else if (!PageUptodate(page))
+               } else if (!PageUptodate(page)) {
                        error = stuffed_readpage(ip, page);
+               }
                goto out;
        }
 
 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) {
-               gfs2_trans_end(sdp);
+       if (error == 0)
+               return 0;
+
+       page_cache_release(page);
+       if (pos + len > ip->i_inode.i_size)
+               vmtruncate(&ip->i_inode, ip->i_inode.i_size);
+out_endtrans:
+       gfs2_trans_end(sdp);
 out_trans_fail:
-               if (alloc_required) {
-                       gfs2_inplace_release(ip);
+       if (alloc_required) {
+               gfs2_inplace_release(ip);
 out_qunlock:
-                       gfs2_quota_unlock(ip);
+               gfs2_quota_unlock(ip);
 out_alloc_put:
-                       gfs2_alloc_put(ip);
-               }
+               gfs2_alloc_put(ip);
+       }
 out_unlock:
-               gfs2_glock_dq_m(1, &ip->i_gh);
+       gfs2_glock_dq(&ip->i_gh);
 out_uninit:
-               gfs2_holder_uninit(&ip->i_gh);
-       }
-
+       gfs2_holder_uninit(&ip->i_gh);
        return error;
 }
 
@@ -477,65 +745,114 @@ static void adjust_fs_space(struct inode *inode)
 }
 
 /**
- * gfs2_commit_write - Commit write to a file
+ * gfs2_stuffed_write_end - Write end for stuffed files
+ * @inode: The inode
+ * @dibh: The buffer_head containing the on-disk inode
+ * @pos: The file position
+ * @len: The length of the write
+ * @copied: How much was actually copied by the VFS
+ * @page: The page
+ *
+ * This copies the data from the page into the inode block after
+ * the inode data structure itself.
+ *
+ * Returns: errno
+ */
+static int gfs2_stuffed_write_end(struct inode *inode, struct buffer_head *dibh,
+                                 loff_t pos, unsigned len, unsigned copied,
+                                 struct page *page)
+{
+       struct gfs2_inode *ip = GFS2_I(inode);
+       struct gfs2_sbd *sdp = GFS2_SB(inode);
+       u64 to = pos + copied;
+       void *kaddr;
+       unsigned char *buf = dibh->b_data + sizeof(struct gfs2_dinode);
+       struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
+
+       BUG_ON((pos + len) > (dibh->b_size - sizeof(struct gfs2_dinode)));
+       kaddr = kmap_atomic(page, KM_USER0);
+       memcpy(buf + pos, kaddr + pos, copied);
+       memset(kaddr + pos + copied, 0, len - copied);
+       flush_dcache_page(page);
+       kunmap_atomic(kaddr, KM_USER0);
+
+       if (!PageUptodate(page))
+               SetPageUptodate(page);
+       unlock_page(page);
+       page_cache_release(page);
+
+       if (inode->i_size < to) {
+               i_size_write(inode, to);
+               ip->i_disksize = inode->i_size;
+               di->di_size = cpu_to_be64(inode->i_size);
+               mark_inode_dirty(inode);
+       }
+
+       if (inode == sdp->sd_rindex)
+               adjust_fs_space(inode);
+
+       brelse(dibh);
+       gfs2_trans_end(sdp);
+       gfs2_glock_dq(&ip->i_gh);
+       gfs2_holder_uninit(&ip->i_gh);
+       return copied;
+}
+
+/**
+ * gfs2_write_end
  * @file: The file to write to
- * @page: The page containing the data
- * @from: From (byte range within page)
- * @to: To (byte range within page)
+ * @mapping: The address space to write to
+ * @pos: The file position
+ * @len: The length of the data
+ * @copied:
+ * @page: The page that has been written
+ * @fsdata: The fsdata (unused in GFS2)
+ *
+ * The main write_end function for GFS2. We have a separate one for
+ * stuffed files as they are slightly different, otherwise we just
+ * put our locking around the VFS provided functions.
  *
  * Returns: errno
  */
 
-static int gfs2_commit_write(struct file *file, struct page *page,
-                            unsigned from, unsigned to)
+static int gfs2_write_end(struct file *file, struct address_space *mapping,
+                         loff_t pos, unsigned len, unsigned copied,
+                         struct page *page, void *fsdata)
 {
        struct inode *inode = page->mapping->host;
        struct gfs2_inode *ip = GFS2_I(inode);
        struct gfs2_sbd *sdp = GFS2_SB(inode);
-       int error = -EOPNOTSUPP;
        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;
 
-       if (gfs2_assert_withdraw(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)))
-                goto fail_nounlock;
+       BUG_ON(gfs2_glock_is_locked_by_me(ip->i_gl) == NULL);
 
-       error = gfs2_meta_inode_buffer(ip, &dibh);
-       if (error)
-               goto fail_endtrans;
+       ret = gfs2_meta_inode_buffer(ip, &dibh);
+       if (unlikely(ret)) {
+               unlock_page(page);
+               page_cache_release(page);
+               goto failed;
+       }
 
        gfs2_trans_add_bh(ip->i_gl, dibh, 1);
-       di = (struct gfs2_dinode *)dibh->b_data;
-
-       if (gfs2_is_stuffed(ip)) {
-               u64 file_size;
-               void *kaddr;
-
-               file_size = ((u64)page->index << PAGE_CACHE_SHIFT) + to;
 
-               kaddr = kmap_atomic(page, KM_USER0);
-               memcpy(dibh->b_data + sizeof(struct gfs2_dinode) + from,
-                      kaddr + from, to - from);
-               kunmap_atomic(kaddr, KM_USER0);
+       if (gfs2_is_stuffed(ip))
+               return gfs2_stuffed_write_end(inode, dibh, pos, len, copied, page);
 
-               SetPageUptodate(page);
+       if (!gfs2_is_writeback(ip))
+               gfs2_page_add_databufs(ip, page, from, to);
 
-               if (inode->i_size < file_size) {
-                       i_size_write(inode, file_size);
-                       mark_inode_dirty(inode);
-               }
-       } else {
-               if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED ||
-                   gfs2_is_jdata(ip))
-                       gfs2_page_add_databufs(ip, page, from, to);
-               error = generic_commit_write(file, page, from, to);
-               if (error)
-                       goto fail;
-       }
+       ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
 
-       if (ip->i_di.di_size < inode->i_size) {
-               ip->i_di.di_size = inode->i_size;
+       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)
@@ -543,33 +860,15 @@ static int gfs2_commit_write(struct file *file, struct page *page,
 
        brelse(dibh);
        gfs2_trans_end(sdp);
-       if (al->al_requested) {
+failed:
+       if (al) {
                gfs2_inplace_release(ip);
                gfs2_quota_unlock(ip);
                gfs2_alloc_put(ip);
        }
-       unlock_page(page);
-       gfs2_glock_dq_m(1, &ip->i_gh);
-       lock_page(page);
+       gfs2_glock_dq(&ip->i_gh);
        gfs2_holder_uninit(&ip->i_gh);
-       return 0;
-
-fail:
-       brelse(dibh);
-fail_endtrans:
-       gfs2_trans_end(sdp);
-       if (al->al_requested) {
-               gfs2_inplace_release(ip);
-               gfs2_quota_unlock(ip);
-               gfs2_alloc_put(ip);
-       }
-       unlock_page(page);
-       gfs2_glock_dq_m(1, &ip->i_gh);
-       lock_page(page);
-       gfs2_holder_uninit(&ip->i_gh);
-fail_nounlock:
-       ClearPageUptodate(page);
-       return error;
+       return ret;
 }
 
 /**
@@ -581,11 +880,7 @@ fail_nounlock:
  
 static int gfs2_set_page_dirty(struct page *page)
 {
-       struct gfs2_inode *ip = GFS2_I(page->mapping->host);
-       struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
-
-       if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
-               SetPageChecked(page);
+       SetPageChecked(page);
        return __set_page_dirty_buffers(page);
 }
 
@@ -609,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);
 
@@ -625,10 +920,10 @@ static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
        clear_buffer_dirty(bh);
        bd = bh->b_private;
        if (bd) {
-               if (!list_empty(&bd->bd_le.le_list)) {
-                       if (!buffer_pinned(bh))
-                               list_del_init(&bd->bd_le.le_list);
-               }
+               if (!list_empty(&bd->bd_le.le_list) && !buffer_pinned(bh))
+                       list_del_init(&bd->bd_le.le_list);
+               else
+                       gfs2_remove_from_journal(bh, current->journal_info, 0);
        }
        bh->b_bdev = NULL;
        clear_buffer_mapped(bh);
@@ -675,17 +970,13 @@ static int gfs2_ok_for_dio(struct gfs2_inode *ip, int rw, loff_t offset)
 {
        /*
         * Should we return an error here? I can't see that O_DIRECT for
-        * a journaled file makes any sense. For now we'll silently fall
-        * back to buffered I/O, likewise we do the same for stuffed
-        * files since they are (a) small and (b) unaligned.
+        * a stuffed file makes any sense. For now we'll silently fall
+        * back to buffered I/O
         */
-       if (gfs2_is_jdata(ip))
-               return 0;
-
        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;
 }
@@ -710,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);
@@ -746,7 +1037,7 @@ int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
        struct gfs2_bufdata *bd;
 
        if (!page_has_buffers(page))
-               goto out;
+               return 0;
 
        gfs2_log_lock(sdp);
        head = bh = page_buffers(page);
@@ -786,25 +1077,70 @@ int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
                bh = bh->b_this_page;
        } while (bh != head);
 
-out:
        return try_to_free_buffers(page);
 cannot_release:
        gfs2_log_unlock(sdp);
        return 0;
 }
 
-const struct address_space_operations gfs2_file_aops = {
-       .writepage = gfs2_writepage,
-       .writepages = gfs2_writepages,
+static const struct address_space_operations gfs2_writeback_aops = {
+       .writepage = gfs2_writeback_writepage,
+       .writepages = gfs2_writeback_writepages,
+       .readpage = gfs2_readpage,
+       .readpages = gfs2_readpages,
+       .sync_page = block_sync_page,
+       .write_begin = gfs2_write_begin,
+       .write_end = gfs2_write_end,
+       .bmap = gfs2_bmap,
+       .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 = {
+       .writepage = gfs2_ordered_writepage,
        .readpage = gfs2_readpage,
        .readpages = gfs2_readpages,
        .sync_page = block_sync_page,
-       .prepare_write = gfs2_prepare_write,
-       .commit_write = gfs2_commit_write,
+       .write_begin = gfs2_write_begin,
+       .write_end = gfs2_write_end,
        .set_page_dirty = gfs2_set_page_dirty,
        .bmap = gfs2_bmap,
        .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 = {
+       .writepage = gfs2_jdata_writepage,
+       .writepages = gfs2_jdata_writepages,
+       .readpage = gfs2_readpage,
+       .readpages = gfs2_readpages,
+       .sync_page = block_sync_page,
+       .write_begin = gfs2_write_begin,
+       .write_end = gfs2_write_end,
+       .set_page_dirty = gfs2_set_page_dirty,
+       .bmap = gfs2_bmap,
+       .invalidatepage = gfs2_invalidatepage,
+       .releasepage = gfs2_releasepage,
+       .is_partially_uptodate = block_is_partially_uptodate,
 };
 
+void gfs2_set_aops(struct inode *inode)
+{
+       struct gfs2_inode *ip = GFS2_I(inode);
+
+       if (gfs2_is_writeback(ip))
+               inode->i_mapping->a_ops = &gfs2_writeback_aops;
+       else if (gfs2_is_ordered(ip))
+               inode->i_mapping->a_ops = &gfs2_ordered_aops;
+       else if (gfs2_is_jdata(ip))
+               inode->i_mapping->a_ops = &gfs2_jdata_aops;
+       else
+               BUG();
+}
+