[GFS2] Tidy up error handling in gfs2_releasepage()
[pandora-kernel.git] / fs / gfs2 / ops_address.c
index 031270a..4872042 100644 (file)
@@ -27,7 +27,6 @@
 #include "log.h"
 #include "meta_io.h"
 #include "ops_address.h"
-#include "page.h"
 #include "quota.h"
 #include "trans.h"
 #include "rgrp.h"
 #include "util.h"
 #include "glops.h"
 
+
+static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
+                                  unsigned int from, unsigned int to)
+{
+       struct buffer_head *head = page_buffers(page);
+       unsigned int bsize = head->b_size;
+       struct buffer_head *bh;
+       unsigned int start, end;
+
+       for (bh = head, start = 0; bh != head || !start;
+            bh = bh->b_this_page, start = end) {
+               end = start + bsize;
+               if (end <= from || start >= to)
+                       continue;
+               gfs2_trans_add_bh(ip->i_gl, bh, 0);
+       }
+}
+
 /**
  * gfs2_get_block - Fills in a buffer head with details about a block
  * @inode: The inode
@@ -114,8 +131,8 @@ static int get_block_noalloc(struct inode *inode, sector_t lblock,
 static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
 {
        struct inode *inode = page->mapping->host;
-       struct gfs2_inode *ip = GFS2_I(page->mapping->host);
-       struct gfs2_sbd *sdp = GFS2_SB(page->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;
@@ -141,6 +158,10 @@ static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
                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);
                done_trans = 1;
        }
@@ -220,14 +241,22 @@ static int gfs2_readpage(struct file *file, struct page *page)
        struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
        struct gfs2_holder gh;
        int error;
+       int do_unlock = 0;
 
        if (likely(file != &gfs2_internal_file_sentinal)) {
+               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, GL_ATIME|GL_AOP, &gh);
+               do_unlock = 1;
                error = gfs2_glock_nq_m_atime(1, &gh);
                if (unlikely(error))
                        goto out_unlock;
        }
 
+skip_lock:
        if (gfs2_is_stuffed(ip)) {
                error = stuffed_readpage(ip, page);
                unlock_page(page);
@@ -245,7 +274,7 @@ out:
        return error;
 out_unlock:
        unlock_page(page);
-       if (file != &gfs2_internal_file_sentinal)
+       if (do_unlock)
                gfs2_holder_uninit(&gh);
        goto out;
 }
@@ -274,17 +303,24 @@ static int gfs2_readpages(struct file *file, struct address_space *mapping,
        struct gfs2_holder gh;
        unsigned page_idx;
        int ret;
+       int do_unlock = 0;
 
        if (likely(file != &gfs2_internal_file_sentinal)) {
+               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|GL_AOP, &gh);
+               do_unlock = 1;
                ret = gfs2_glock_nq_m_atime(1, &gh);
                if (ret == GLR_TRYFAILED) 
                        goto out_noerror;
                if (unlikely(ret))
                        goto out_unlock;
        }
-
+skip_lock:
        if (gfs2_is_stuffed(ip)) {
                struct pagevec lru_pvec;
                pagevec_init(&lru_pvec, 0);
@@ -309,7 +345,7 @@ static int gfs2_readpages(struct file *file, struct address_space *mapping,
                ret = mpage_readpages(mapping, pages, nr_pages, gfs2_get_block);
        }
 
-       if (likely(file != &gfs2_internal_file_sentinal)) {
+       if (do_unlock) {
                gfs2_glock_dq_m(1, &gh);
                gfs2_holder_uninit(&gh);
        }
@@ -327,7 +363,7 @@ out_unlock:
                unlock_page(page);
                page_cache_release(page);
        }
-       if (likely(file != &gfs2_internal_file_sentinal))
+       if (do_unlock)
                gfs2_holder_uninit(&gh);
        goto out;
 }
@@ -395,8 +431,7 @@ static int gfs2_prepare_write(struct file *file, struct page *page,
 
        if (gfs2_is_stuffed(ip)) {
                if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
-                       error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page,
-                                                   page);
+                       error = gfs2_unstuff_dinode(ip, page);
                        if (error == 0)
                                goto prepare_write;
                } else if (!PageUptodate(page))
@@ -546,10 +581,8 @@ static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh)
        if (bd) {
                bd->bd_bh = NULL;
                bh->b_private = NULL;
-               gfs2_log_unlock(sdp);
-               brelse(bh);
-       } else
-               gfs2_log_unlock(sdp);
+       }
+       gfs2_log_unlock(sdp);
 
        lock_buffer(bh);
        clear_buffer_dirty(bh);
@@ -563,7 +596,7 @@ static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh)
 
 static void gfs2_invalidatepage(struct page *page, unsigned long offset)
 {
-       struct gfs2_sbd *sdp = page->mapping->host->i_sb->s_fs_info;
+       struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
        struct buffer_head *head, *bh, *next;
        unsigned int curr_off = 0;
 
@@ -589,8 +622,9 @@ static void gfs2_invalidatepage(struct page *page, unsigned long offset)
        return;
 }
 
-static ssize_t gfs2_direct_IO_write(struct kiocb *iocb, const struct iovec *iov,
-                                   loff_t offset, unsigned long nr_segs)
+static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
+                             const struct iovec *iov, loff_t offset,
+                             unsigned long nr_segs)
 {
        struct file *file = iocb->ki_filp;
        struct inode *inode = file->f_mapping->host;
@@ -598,8 +632,10 @@ static ssize_t gfs2_direct_IO_write(struct kiocb *iocb, const struct iovec *iov,
        struct gfs2_holder gh;
        int rv;
 
+       if (rw == READ)
+               mutex_lock(&inode->i_mutex);
        /*
-        * Shared lock, even though its write, since we do no allocation
+        * Shared lock, even if its a write, since we do no allocation
         * on this path. All we need change is atime.
         */
        gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
@@ -607,6 +643,9 @@ static ssize_t gfs2_direct_IO_write(struct kiocb *iocb, const struct iovec *iov,
        if (rv)
                goto out;
 
+       if (offset > i_size_read(inode))
+               goto out;
+
        /*
         * 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
@@ -619,47 +658,19 @@ static ssize_t gfs2_direct_IO_write(struct kiocb *iocb, const struct iovec *iov,
        if (gfs2_is_stuffed(ip))
                goto out;
 
-       rv = __blockdev_direct_IO(WRITE, iocb, inode, inode->i_sb->s_bdev,
-                                 iov, offset, nr_segs, gfs2_get_block,
-                                 NULL, DIO_OWN_LOCKING);
+       rv = blockdev_direct_IO_own_locking(rw, iocb, inode,
+                                           inode->i_sb->s_bdev,
+                                           iov, offset, nr_segs,
+                                           gfs2_get_block, NULL);
 out:
        gfs2_glock_dq_m(1, &gh);
        gfs2_holder_uninit(&gh);
+       if (rw == READ)
+               mutex_unlock(&inode->i_mutex);
 
        return rv;
 }
 
-/**
- * gfs2_direct_IO
- *
- * This is called with a shared lock already held for the read path.
- * Currently, no locks are held when the write path is called.
- */
-static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
-                             const struct iovec *iov, loff_t offset,
-                             unsigned long nr_segs)
-{
-       struct file *file = iocb->ki_filp;
-       struct inode *inode = file->f_mapping->host;
-       struct gfs2_inode *ip = GFS2_I(inode);
-       struct gfs2_sbd *sdp = GFS2_SB(inode);
-       int ret;
-
-       if (rw == WRITE)
-               return gfs2_direct_IO_write(iocb, iov, offset, nr_segs);
-
-       if (gfs2_assert_warn(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)) ||
-           gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
-               return -EINVAL;
-
-       mutex_lock(&inode->i_mutex);
-       ret = __blockdev_direct_IO(READ, iocb, inode, inode->i_sb->s_bdev, iov,
-                                  offset, nr_segs, gfs2_get_block, NULL,
-                                  DIO_OWN_LOCKING);
-       mutex_unlock(&inode->i_mutex);
-       return ret;
-}
-
 /**
  * stuck_releasepage - We're stuck in gfs2_releasepage().  Print stuff out.
  * @bh: the buffer we're stuck on
@@ -672,6 +683,10 @@ static void stuck_releasepage(struct buffer_head *bh)
        struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
        struct gfs2_bufdata *bd = bh->b_private;
        struct gfs2_glock *gl;
+static unsigned limit = 0;
+
+       if (limit++ > 3)
+               return;
 
        fs_warn(sdp, "stuck in gfs2_releasepage() %p\n", inode);
        fs_warn(sdp, "blkno = %llu, bh->b_count = %d\n",
@@ -725,28 +740,24 @@ int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
        struct gfs2_sbd *sdp = aspace->i_sb->s_fs_info;
        struct buffer_head *bh, *head;
        struct gfs2_bufdata *bd;
-       unsigned long t;
+       unsigned long t = jiffies + gfs2_tune_get(sdp, gt_stall_secs) * HZ;
 
        if (!page_has_buffers(page))
                goto out;
 
        head = bh = page_buffers(page);
        do {
-               t = jiffies;
-
                while (atomic_read(&bh->b_count)) {
-                       if (atomic_read(&aspace->i_writecount)) {
-                               if (time_after_eq(jiffies, t +
-                                   gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
-                                       stuck_releasepage(bh);
-                                       t = jiffies;
-                               }
-
-                               yield();
-                               continue;
+                       if (!atomic_read(&aspace->i_writecount))
+                               return 0;
+
+                       if (time_after_eq(jiffies, t)) {
+                               stuck_releasepage(bh);
+                               /* should we withdraw here? */
+                               return 0;
                        }
 
-                       return 0;
+                       yield();
                }
 
                gfs2_assert_warn(sdp, !buffer_pinned(bh));
@@ -762,10 +773,9 @@ int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
                }
 
                bh = bh->b_this_page;
-       }
-       while (bh != head);
+       } while (bh != head);
 
- out:
+out:
        return try_to_free_buffers(page);
 }