[XFS] Don't do I/O beyond eof when unreserving space
[safe/jmp/linux-2.6] / fs / gfs2 / ops_address.c
index e16ad81..e64a1b0 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
@@ -21,7 +21,6 @@
 #include <linux/gfs2_ondisk.h>
 #include <linux/lm_interface.h>
 #include <linux/backing-dev.h>
-#include <linux/pagevec.h>
 
 #include "gfs2.h"
 #include "incore.h"
@@ -104,11 +103,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) */
@@ -280,7 +277,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;
 
@@ -441,12 +438,12 @@ 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);
                return 0;
        }
 
@@ -502,31 +499,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);
+       unlock_page(page);
+       gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
        error = gfs2_glock_nq_atime(&gh);
-       if (unlikely(error)) {
-               unlock_page(page);
+       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;
 }
 
@@ -646,18 +646,17 @@ static int gfs2_write_begin(struct file *file, struct address_space *mapping,
        if (error)
                goto out_unlock;
 
-       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)
@@ -823,13 +822,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)) {
@@ -848,14 +847,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_di.di_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 (inode == sdp->sd_rindex)
@@ -864,7 +860,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);