]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - fs/gfs2/ops_address.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux-2.6-omap-h63xx.git] / fs / gfs2 / ops_address.c
index ce90032c010ec7176d8f31505a5c859833dc3650..9679f8b9870d4b662a9a49023fc8b604ade25a68 100644 (file)
@@ -17,6 +17,7 @@
 #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>
 
@@ -90,7 +91,7 @@ static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
        error = gfs2_block_map(inode, lblock, 0, bh_result);
        if (error)
                return error;
-       if (bh_result->b_blocknr == 0)
+       if (!buffer_mapped(bh_result))
                return -EIO;
        return 0;
 }
@@ -349,45 +350,49 @@ out_unlock:
 }
 
 /**
- * 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);
+       struct gfs2_inode *ip = GFS2_I(mapping->host);
+       struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
        unsigned int data_blocks, ind_blocks, 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;
-
+       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, GL_ATIME|LM_FLAG_TRY_1CB, &ip->i_gh);
+       gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &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();
-               }
+       if (unlikely(error))
                goto out_uninit;
-       }
 
-       gfs2_write_calc_reserv(ip, write_len, &data_blocks, &ind_blocks);
+       error = -ENOMEM;
+       page = __grab_cache_page(mapping, index);
+       *pagep = page;
+       if (!page)
+               goto out_unlock;
+
+       gfs2_write_calc_reserv(ip, 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;
+               goto out_putpage;
 
 
        ip->i_alloc.al_requested = 0;
@@ -414,12 +419,13 @@ 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;
+               goto out_trans_fail;
 
        if (gfs2_is_stuffed(ip)) {
-               if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
+               if (pos + len > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
                        error = gfs2_unstuff_dinode(ip, page);
                        if (error == 0)
                                goto prepare_write;
@@ -434,6 +440,7 @@ prepare_write:
 out:
        if (error) {
                gfs2_trans_end(sdp);
+out_trans_fail:
                if (alloc_required) {
                        gfs2_inplace_release(ip);
 out_qunlock:
@@ -441,6 +448,10 @@ out_qunlock:
 out_alloc_put:
                        gfs2_alloc_put(ip);
                }
+out_putpage:
+               page_cache_release(page);
+               if (pos + len > ip->i_inode.i_size)
+                       vmtruncate(&ip->i_inode, ip->i_inode.i_size);
 out_unlock:
                gfs2_glock_dq_m(1, &ip->i_gh);
 out_uninit:
@@ -476,65 +487,117 @@ 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_di.di_size = 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_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) == 0);
 
-       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;
+       if (gfs2_is_stuffed(ip))
+               return gfs2_stuffed_write_end(inode, dibh, pos, len, copied, page);
 
-               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 (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
+               gfs2_page_add_databufs(ip, page, from, to);
 
-               SetPageUptodate(page);
+       ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
 
-               if (inode->i_size < file_size) {
-                       i_size_write(inode, file_size);
+       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);
                }
-       } 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;
-       }
-
-       if (ip->i_di.di_size < inode->i_size) {
-               ip->i_di.di_size = inode->i_size;
-               di->di_size = cpu_to_be64(inode->i_size);
        }
 
        if (inode == sdp->sd_rindex)
@@ -542,33 +605,15 @@ static int gfs2_commit_write(struct file *file, struct page *page,
 
        brelse(dibh);
        gfs2_trans_end(sdp);
+failed:
        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_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;
 }
 
 /**
@@ -615,58 +660,50 @@ static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
        return dblock;
 }
 
-static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh)
+static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
 {
        struct gfs2_bufdata *bd;
 
+       lock_buffer(bh);
        gfs2_log_lock(sdp);
+       clear_buffer_dirty(bh);
        bd = bh->b_private;
        if (bd) {
-               bd->bd_bh = NULL;
-               bh->b_private = NULL;
-               if (!bd->bd_ail && list_empty(&bd->bd_le.le_list))
-                       kmem_cache_free(gfs2_bufdata_cachep, bd);
+               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);
        }
-       gfs2_log_unlock(sdp);
-
-       lock_buffer(bh);
-       clear_buffer_dirty(bh);
        bh->b_bdev = NULL;
        clear_buffer_mapped(bh);
        clear_buffer_req(bh);
        clear_buffer_new(bh);
-       clear_buffer_delay(bh);
+       gfs2_log_unlock(sdp);
        unlock_buffer(bh);
 }
 
 static void gfs2_invalidatepage(struct page *page, unsigned long offset)
 {
        struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
-       struct buffer_head *head, *bh, *next;
-       unsigned int curr_off = 0;
+       struct buffer_head *bh, *head;
+       unsigned long pos = 0;
 
        BUG_ON(!PageLocked(page));
        if (offset == 0)
                ClearPageChecked(page);
        if (!page_has_buffers(page))
-               return;
+               goto out;
 
        bh = head = page_buffers(page);
        do {
-               unsigned int next_off = curr_off + bh->b_size;
-               next = bh->b_this_page;
-
-               if (offset <= curr_off)
-                       discard_buffer(sdp, bh);
-
-               curr_off = next_off;
-               bh = next;
+               if (offset <= pos)
+                       gfs2_discard(sdp, bh);
+               pos += bh->b_size;
+               bh = bh->b_this_page;
        } while (bh != head);
-
-       if (!offset)
+out:
+       if (offset == 0)
                try_to_release_page(page, 0);
-
-       return;
 }
 
 /**
@@ -734,59 +771,6 @@ out:
        return rv;
 }
 
-/**
- * stuck_releasepage - We're stuck in gfs2_releasepage().  Print stuff out.
- * @bh: the buffer we're stuck on
- *
- */
-
-static void stuck_releasepage(struct buffer_head *bh)
-{
-       struct inode *inode = bh->b_page->mapping->host;
-       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;
-       limit++;
-
-       fs_warn(sdp, "stuck in gfs2_releasepage() %p\n", inode);
-       fs_warn(sdp, "blkno = %llu, bh->b_count = %d\n",
-               (unsigned long long)bh->b_blocknr, atomic_read(&bh->b_count));
-       fs_warn(sdp, "pinned = %u\n", buffer_pinned(bh));
-       fs_warn(sdp, "bh->b_private = %s\n", (bd) ? "!NULL" : "NULL");
-
-       if (!bd)
-               return;
-
-       gl = bd->bd_gl;
-
-       fs_warn(sdp, "gl = (%u, %llu)\n",
-               gl->gl_name.ln_type, (unsigned long long)gl->gl_name.ln_number);
-
-       fs_warn(sdp, "bd_list_tr = %s, bd_le.le_list = %s\n",
-               (list_empty(&bd->bd_list_tr)) ? "no" : "yes",
-               (list_empty(&bd->bd_le.le_list)) ? "no" : "yes");
-
-       if (gl->gl_ops == &gfs2_inode_glops) {
-               struct gfs2_inode *ip = gl->gl_object;
-               unsigned int x;
-
-               if (!ip)
-                       return;
-
-               fs_warn(sdp, "ip = %llu %llu\n",
-                       (unsigned long long)ip->i_no_formal_ino,
-                       (unsigned long long)ip->i_no_addr);
-
-               for (x = 0; x < GFS2_MAX_META_HEIGHT; x++)
-                       fs_warn(sdp, "ip->i_cache[%u] = %s\n",
-                               x, (ip->i_cache[x]) ? "!NULL" : "NULL");
-       }
-}
-
 /**
  * gfs2_releasepage - free the metadata associated with a page
  * @page: the page that's being released
@@ -804,41 +788,39 @@ 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 = jiffies + gfs2_tune_get(sdp, gt_stall_secs) * HZ;
 
        if (!page_has_buffers(page))
-               goto out;
+               return 0;
 
+       gfs2_log_lock(sdp);
        head = bh = page_buffers(page);
        do {
-               while (atomic_read(&bh->b_count)) {
-                       if (!atomic_read(&aspace->i_writecount))
-                               return 0;
-
-                       if (!(gfp_mask & __GFP_WAIT))
-                               return 0;
-
-                       if (time_after_eq(jiffies, t)) {
-                               stuck_releasepage(bh);
-                               /* should we withdraw here? */
-                               return 0;
-                       }
-
-                       yield();
-               }
-
+               if (atomic_read(&bh->b_count))
+                       goto cannot_release;
+               bd = bh->b_private;
+               if (bd && bd->bd_ail)
+                       goto cannot_release;
                gfs2_assert_warn(sdp, !buffer_pinned(bh));
                gfs2_assert_warn(sdp, !buffer_dirty(bh));
+               bh = bh->b_this_page;
+       } while(bh != head);
+       gfs2_log_unlock(sdp);
 
+       head = bh = page_buffers(page);
+       do {
                gfs2_log_lock(sdp);
                bd = bh->b_private;
                if (bd) {
                        gfs2_assert_warn(sdp, bd->bd_bh == bh);
                        gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
-                       gfs2_assert_warn(sdp, !bd->bd_ail);
-                       bd->bd_bh = NULL;
-                       if (!list_empty(&bd->bd_le.le_list))
-                               bd = NULL;
+                       if (!list_empty(&bd->bd_le.le_list)) {
+                               if (!buffer_pinned(bh))
+                                       list_del_init(&bd->bd_le.le_list);
+                               else
+                                       bd = NULL;
+                       }
+                       if (bd)
+                               bd->bd_bh = NULL;
                        bh->b_private = NULL;
                }
                gfs2_log_unlock(sdp);
@@ -848,8 +830,10 @@ 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 = {
@@ -858,8 +842,8 @@ const struct address_space_operations gfs2_file_aops = {
        .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,