anon-inodes use open coded atomic_inc for the shared inode
[safe/jmp/linux-2.6] / fs / gfs2 / bmap.c
index bece362..93fa427 100644 (file)
@@ -1,29 +1,32 @@
 /*
  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
- * Copyright (C) 2004-2005 Red Hat, Inc.  All rights reserved.
+ * Copyright (C) 2004-2006 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
- * of the GNU General Public License v.2.
+ * of the GNU General Public License version 2.
  */
 
-#include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 #include <linux/completion.h>
 #include <linux/buffer_head.h>
-#include <asm/semaphore.h>
+#include <linux/gfs2_ondisk.h>
+#include <linux/crc32.h>
+#include <linux/lm_interface.h>
 
 #include "gfs2.h"
+#include "incore.h"
 #include "bmap.h"
 #include "glock.h"
 #include "inode.h"
-#include "jdata.h"
 #include "meta_io.h"
-#include "page.h"
 #include "quota.h"
 #include "rgrp.h"
 #include "trans.h"
+#include "dir.h"
+#include "util.h"
+#include "ops_address.h"
 
 /* This doesn't need to be that large as max 64 bit pointers in a 4k
  * block is 512, so __u16 is fine for that. It saves stack space to
@@ -34,8 +37,8 @@ struct metapath {
 };
 
 typedef int (*block_call_t) (struct gfs2_inode *ip, struct buffer_head *dibh,
-                            struct buffer_head *bh, uint64_t *top,
-                            uint64_t *bottom, unsigned int height,
+                            struct buffer_head *bh, __be64 *top,
+                            __be64 *bottom, unsigned int height,
                             void *data);
 
 struct strip_mine {
@@ -44,33 +47,63 @@ struct strip_mine {
 };
 
 /**
- * @gfs2_unstuffer_sync - Synchronously unstuff a dinode
- * @ip:
- * @dibh:
- * @block:
- * @private:
- *
- * Cheat and use a metadata buffer instead of a data page.
+ * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
+ * @ip: the inode
+ * @dibh: the dinode buffer
+ * @block: the block number that was allocated
+ * @private: any locked page held by the caller process
  *
  * Returns: errno
  */
 
-int gfs2_unstuffer_sync(struct gfs2_inode *ip, struct buffer_head *dibh,
-                       uint64_t block, void *private)
+static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
+                              u64 block, struct page *page)
 {
+       struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
+       struct inode *inode = &ip->i_inode;
        struct buffer_head *bh;
-       int error;
+       int release = 0;
 
-       bh = gfs2_meta_new(ip->i_gl, block);
+       if (!page || page->index) {
+               page = grab_cache_page(inode->i_mapping, 0);
+               if (!page)
+                       return -ENOMEM;
+               release = 1;
+       }
 
-       gfs2_buffer_copy_tail(bh, 0, dibh, sizeof(struct gfs2_dinode));
+       if (!PageUptodate(page)) {
+               void *kaddr = kmap(page);
 
-       set_buffer_dirty(bh);
-       error = sync_dirty_buffer(bh);
+               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);
+               kunmap(page);
 
-       brelse(bh);
+               SetPageUptodate(page);
+       }
 
-       return error;
+       if (!page_has_buffers(page))
+               create_empty_buffers(page, 1 << inode->i_blkbits,
+                                    (1 << BH_Uptodate));
+
+       bh = page_buffers(page);
+
+       if (!buffer_mapped(bh))
+               map_bh(bh, inode->i_sb, block);
+
+       set_buffer_uptodate(bh);
+       if (!gfs2_is_jdata(ip))
+               mark_buffer_dirty(bh);
+       if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
+               gfs2_trans_add_bh(ip->i_gl, bh, 0);
+
+       if (release) {
+               unlock_page(page);
+               page_cache_release(page);
+       }
+
+       return 0;
 }
 
 /**
@@ -85,12 +118,12 @@ int gfs2_unstuffer_sync(struct gfs2_inode *ip, struct buffer_head *dibh,
  * Returns: errno
  */
 
-int gfs2_unstuff_dinode(struct gfs2_inode *ip, gfs2_unstuffer_t unstuffer,
-                       void *private)
+int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
 {
        struct buffer_head *bh, *dibh;
-       uint64_t block = 0;
-       int journaled = gfs2_is_jdata(ip);
+       struct gfs2_dinode *di;
+       u64 block = 0;
+       int isdir = gfs2_is_dir(ip);
        int error;
 
        down_write(&ip->i_rw_mutex);
@@ -98,25 +131,24 @@ int gfs2_unstuff_dinode(struct gfs2_inode *ip, gfs2_unstuffer_t unstuffer,
        error = gfs2_meta_inode_buffer(ip, &dibh);
        if (error)
                goto out;
-               
+
        if (ip->i_di.di_size) {
                /* Get a free block, fill it with the stuffed data,
                   and write it out to disk */
 
-               if (journaled) {
+               if (isdir) {
                        block = gfs2_alloc_meta(ip);
 
-                       error = gfs2_jdata_get_buffer(ip, block, 1, &bh);
+                       error = gfs2_dir_get_new_buffer(ip, block, &bh);
                        if (error)
                                goto out_brelse;
-                       gfs2_buffer_copy_tail(bh,
-                                             sizeof(struct gfs2_meta_header),
+                       gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
                                              dibh, sizeof(struct gfs2_dinode));
                        brelse(bh);
                } else {
                        block = gfs2_alloc_data(ip);
 
-                       error = unstuffer(ip, dibh, block, private);
+                       error = gfs2_unstuffer_page(ip, dibh, block, page);
                        if (error)
                                goto out_brelse;
                }
@@ -124,25 +156,24 @@ int gfs2_unstuff_dinode(struct gfs2_inode *ip, gfs2_unstuffer_t unstuffer,
 
        /*  Set up the pointer to the new block  */
 
-       gfs2_trans_add_bh(ip->i_gl, dibh);
-
+       gfs2_trans_add_bh(ip->i_gl, dibh, 1);
+       di = (struct gfs2_dinode *)dibh->b_data;
        gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
 
        if (ip->i_di.di_size) {
-               *(uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode)) = cpu_to_be64(block);
+               *(__be64 *)(di + 1) = cpu_to_be64(block);
                ip->i_di.di_blocks++;
+               gfs2_set_inode_blocks(&ip->i_inode);
+               di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
        }
 
        ip->i_di.di_height = 1;
+       di->di_height = cpu_to_be16(1);
 
-       gfs2_dinode_out(&ip->i_di, dibh->b_data);
-
- out_brelse:
+out_brelse:
        brelse(dibh);
-
- out:
+out:
        up_write(&ip->i_rw_mutex);
-
        return error;
 }
 
@@ -159,16 +190,16 @@ int gfs2_unstuff_dinode(struct gfs2_inode *ip, gfs2_unstuffer_t unstuffer,
  * Returns: the height the tree should be
  */
 
-static unsigned int calc_tree_height(struct gfs2_inode *ip, uint64_t size)
+static unsigned int calc_tree_height(struct gfs2_inode *ip, u64 size)
 {
-       struct gfs2_sbd *sdp = ip->i_sbd;
-       uint64_t *arr;
+       struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
+       u64 *arr;
        unsigned int max, height;
 
        if (ip->i_di.di_size > size)
                size = ip->i_di.di_size;
 
-       if (gfs2_is_jdata(ip)) {
+       if (gfs2_is_dir(ip)) {
                arr = sdp->sd_jheightsize;
                max = sdp->sd_max_jheight;
        } else {
@@ -188,70 +219,65 @@ static unsigned int calc_tree_height(struct gfs2_inode *ip, uint64_t size)
  * @ip: The GFS2 inode
  * @height: The height to build to
  *
- * This routine makes sure that the metadata tree is tall enough to hold
- * "size" bytes of data.
  *
  * Returns: errno
  */
 
-static int build_height(struct gfs2_inode *ip, int height)
+static int build_height(struct inode *inode, unsigned height)
 {
-       struct gfs2_sbd *sdp = ip->i_sbd;
-       struct buffer_head *bh, *dibh;
-       uint64_t block = 0, *bp;
-       unsigned int x;
-       int new_block;
+       struct gfs2_inode *ip = GFS2_I(inode);
+       unsigned new_height = height - ip->i_di.di_height;
+       struct buffer_head *dibh;
+       struct buffer_head *blocks[GFS2_MAX_META_HEIGHT];
+       struct gfs2_dinode *di;
        int error;
+       __be64 *bp;
+       u64 bn;
+       unsigned n;
 
-       while (ip->i_di.di_height < height) {
-               error = gfs2_meta_inode_buffer(ip, &dibh);
-               if (error)
-                       return error;
-
-               new_block = 0;
-               bp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
-               for (x = 0; x < sdp->sd_diptrs; x++, bp++)
-                       if (*bp) {
-                               new_block = 1;
-                               break;
-                       }
+       if (height <= ip->i_di.di_height)
+               return 0;
 
-               if (new_block) {
-                       /* Get a new block, fill it with the old direct
-                          pointers, and write it out */
+       error = gfs2_meta_inode_buffer(ip, &dibh);
+       if (error)
+               return error;
 
-                       block = gfs2_alloc_meta(ip);
+       for(n = 0; n < new_height; n++) {
+               bn = gfs2_alloc_meta(ip);
+               blocks[n] = gfs2_meta_new(ip->i_gl, bn);
+               gfs2_trans_add_bh(ip->i_gl, blocks[n], 1);
+       }
 
-                       bh = gfs2_meta_new(ip->i_gl, block);
-                       gfs2_trans_add_bh(ip->i_gl, bh);
-                       gfs2_metatype_set(bh,
-                                         GFS2_METATYPE_IN,
+       n = 0;
+       bn = blocks[0]->b_blocknr;
+       if (new_height > 1) {
+               for(; n < new_height-1; n++) {
+                       gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN,
                                          GFS2_FORMAT_IN);
-                       gfs2_buffer_copy_tail(bh,
-                                             sizeof(struct gfs2_meta_header),
-                                             dibh, sizeof(struct gfs2_dinode));
-
-                       brelse(bh);
-               }
-
-               /*  Set up the new direct pointer and write it out to disk  */
-
-               gfs2_trans_add_bh(ip->i_gl, dibh);
-
-               gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
-
-               if (new_block) {
-                       *(uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode)) = cpu_to_be64(block);
-                       ip->i_di.di_blocks++;
+                       gfs2_buffer_clear_tail(blocks[n],
+                                              sizeof(struct gfs2_meta_header));
+                       bp = (__be64 *)(blocks[n]->b_data +
+                                    sizeof(struct gfs2_meta_header));
+                       *bp = cpu_to_be64(blocks[n+1]->b_blocknr);
+                       brelse(blocks[n]);
+                       blocks[n] = NULL;
                }
-
-               ip->i_di.di_height++;
-
-               gfs2_dinode_out(&ip->i_di, dibh->b_data);
-               brelse(dibh);
        }
-
-       return 0;
+       gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
+       gfs2_buffer_copy_tail(blocks[n], sizeof(struct gfs2_meta_header),
+                             dibh, sizeof(struct gfs2_dinode));
+       brelse(blocks[n]);
+       gfs2_trans_add_bh(ip->i_gl, dibh, 1);
+       di = (struct gfs2_dinode *)dibh->b_data;
+       gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
+       *(__be64 *)(di + 1) = cpu_to_be64(bn);
+       ip->i_di.di_height += new_height;
+       ip->i_di.di_blocks += new_height;
+       gfs2_set_inode_blocks(&ip->i_inode);
+       di->di_height = cpu_to_be16(ip->i_di.di_height);
+       di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
+       brelse(dibh);
+       return error;
 }
 
 /**
@@ -313,14 +339,15 @@ static int build_height(struct gfs2_inode *ip, int height)
  *
  */
 
-static void find_metapath(struct gfs2_inode *ip, uint64_t block, struct metapath *mp)
+static void find_metapath(struct gfs2_inode *ip, u64 block,
+                         struct metapath *mp)
 {
-       struct gfs2_sbd *sdp = ip->i_sbd;
-       uint64_t b = block;
+       struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
+       u64 b = block;
        unsigned int i;
 
        for (i = ip->i_di.di_height; i--;)
-               mp->mp_list[i] = (__u16)do_div(b, sdp->sd_inptrs);
+               mp->mp_list[i] = do_div(b, sdp->sd_inptrs);
 
 }
 
@@ -335,13 +362,17 @@ static void find_metapath(struct gfs2_inode *ip, uint64_t block, struct metapath
  * metadata tree.
  */
 
-static inline uint64_t *metapointer(struct buffer_head *bh,
-                                   unsigned int height, struct metapath *mp)
+static inline __be64 *metapointer(struct buffer_head *bh, int *boundary,
+                              unsigned int height, const struct metapath *mp)
 {
        unsigned int head_size = (height > 0) ?
                sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
-
-       return ((uint64_t *)(bh->b_data + head_size)) + mp->mp_list[height];
+       __be64 *ptr;
+       *boundary = 0;
+       ptr = ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
+       if (ptr + 1 == (__be64 *)(bh->b_data + bh->b_size))
+               *boundary = 1;
+       return ptr;
 }
 
 /**
@@ -360,43 +391,61 @@ static inline uint64_t *metapointer(struct buffer_head *bh,
  *
  */
 
-static void lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
-                        unsigned int height, struct metapath *mp, int create,
-                        int *new, uint64_t *block)
+static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
+                       unsigned int height, struct metapath *mp, int create,
+                       int *new, u64 *block)
 {
-       uint64_t *ptr = metapointer(bh, height, mp);
+       int boundary;
+       __be64 *ptr = metapointer(bh, &boundary, height, mp);
 
        if (*ptr) {
                *block = be64_to_cpu(*ptr);
-               return;
+               return boundary;
        }
 
        *block = 0;
 
        if (!create)
-               return;
+               return 0;
 
-       if (height == ip->i_di.di_height - 1 &&
-           !gfs2_is_jdata(ip))
+       if (height == ip->i_di.di_height - 1 && !gfs2_is_dir(ip))
                *block = gfs2_alloc_data(ip);
        else
                *block = gfs2_alloc_meta(ip);
 
-       gfs2_trans_add_bh(ip->i_gl, bh);
+       gfs2_trans_add_bh(ip->i_gl, bh, 1);
 
        *ptr = cpu_to_be64(*block);
        ip->i_di.di_blocks++;
+       gfs2_set_inode_blocks(&ip->i_inode);
 
        *new = 1;
+       return 0;
+}
+
+static inline void bmap_lock(struct inode *inode, int create)
+{
+       struct gfs2_inode *ip = GFS2_I(inode);
+       if (create)
+               down_write(&ip->i_rw_mutex);
+       else
+               down_read(&ip->i_rw_mutex);
+}
+
+static inline void bmap_unlock(struct inode *inode, int create)
+{
+       struct gfs2_inode *ip = GFS2_I(inode);
+       if (create)
+               up_write(&ip->i_rw_mutex);
+       else
+               up_read(&ip->i_rw_mutex);
 }
 
 /**
  * gfs2_block_map - Map a block from an inode to a disk block
- * @ip: The GFS2 inode
+ * @inode: The inode
  * @lblock: The logical block number
- * @new: Value/Result argument (1 = may create/did create new blocks)
- * @dblock: the disk block number of the start of an extent
- * @extlen: the size of the extent
+ * @bh_map: The bh to be mapped
  *
  * Find the block number on the current device which corresponds to an
  * inode's block. If the block had to be created, "new" will be set.
@@ -404,105 +453,121 @@ static void lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
  * Returns: errno
  */
 
-int gfs2_block_map(struct gfs2_inode *ip, uint64_t lblock, int *new,
-                  uint64_t *dblock, uint32_t *extlen)
+int gfs2_block_map(struct inode *inode, u64 lblock, int create,
+                  struct buffer_head *bh_map)
 {
-       struct gfs2_sbd *sdp = ip->i_sbd;
+       struct gfs2_inode *ip = GFS2_I(inode);
+       struct gfs2_sbd *sdp = GFS2_SB(inode);
        struct buffer_head *bh;
-       struct metapath mp;
-       int create = *new;
        unsigned int bsize;
        unsigned int height;
        unsigned int end_of_metadata;
        unsigned int x;
        int error = 0;
+       int new = 0;
+       u64 dblock = 0;
+       int boundary;
+       unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
+       struct metapath mp;
+       u64 size;
 
-       *new = 0;
-       *dblock = 0;
-       if (extlen)
-               *extlen = 0;
-
-       if (create)
-               down_write(&ip->i_rw_mutex);
-       else
-               down_read(&ip->i_rw_mutex);
+       BUG_ON(maxlen == 0);
 
        if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
-               goto out;
-
-       bsize = (gfs2_is_jdata(ip)) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
-
-       height = calc_tree_height(ip, (lblock + 1) * bsize);
-       if (ip->i_di.di_height < height) {
-               if (!create)
-                       goto out;
+               return 0;
 
-               error = build_height(ip, height);
-               if (error)
-                       goto out;
+       bmap_lock(inode, create);
+       clear_buffer_mapped(bh_map);
+       clear_buffer_new(bh_map);
+       clear_buffer_boundary(bh_map);
+       bsize = gfs2_is_dir(ip) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
+       size = (lblock + 1) * bsize;
+
+       if (size > ip->i_di.di_size) {
+               height = calc_tree_height(ip, size);
+               if (ip->i_di.di_height < height) {
+                       if (!create)
+                               goto out_ok;
+       
+                       error = build_height(inode, height);
+                       if (error)
+                               goto out_fail;
+               }
        }
 
        find_metapath(ip, lblock, &mp);
        end_of_metadata = ip->i_di.di_height - 1;
-
        error = gfs2_meta_inode_buffer(ip, &bh);
        if (error)
-               goto out;
+               goto out_fail;
 
        for (x = 0; x < end_of_metadata; x++) {
-               lookup_block(ip, bh, x, &mp, create, new, dblock);
+               lookup_block(ip, bh, x, &mp, create, &new, &dblock);
                brelse(bh);
-               if (!*dblock)
-                       goto out;
+               if (!dblock)
+                       goto out_ok;
 
-               error = gfs2_meta_indirect_buffer(ip, x+1, *dblock, *new, &bh);
+               error = gfs2_meta_indirect_buffer(ip, x+1, dblock, new, &bh);
                if (error)
-                       goto out;
+                       goto out_fail;
        }
 
-       lookup_block(ip, bh, end_of_metadata, &mp, create, new, dblock);
-
-       if (extlen && *dblock) {
-               *extlen = 1;
-
-               if (!*new) {
-                       uint64_t tmp_dblock;
-                       int tmp_new;
-                       unsigned int nptrs;
-
-                       nptrs = (end_of_metadata) ? sdp->sd_inptrs :
-                                                   sdp->sd_diptrs;
-
-                       while (++mp.mp_list[end_of_metadata] < nptrs) {
-                               lookup_block(ip, bh, end_of_metadata, &mp,
-                                            0, &tmp_new, &tmp_dblock);
-
-                               if (*dblock + *extlen != tmp_dblock)
-                                       break;
-
-                               (*extlen)++;
+       boundary = lookup_block(ip, bh, end_of_metadata, &mp, create, &new, &dblock);
+       if (dblock) {
+               map_bh(bh_map, inode->i_sb, dblock);
+               if (boundary)
+                       set_buffer_boundary(bh_map);
+               if (new) {
+                       struct buffer_head *dibh;
+                       error = gfs2_meta_inode_buffer(ip, &dibh);
+                       if (!error) {
+                               gfs2_trans_add_bh(ip->i_gl, dibh, 1);
+                               gfs2_dinode_out(ip, dibh->b_data);
+                               brelse(dibh);
                        }
+                       set_buffer_new(bh_map);
+                       goto out_brelse;
                }
-       }
-
-       brelse(bh);
+               while(--maxlen && !buffer_boundary(bh_map)) {
+                       u64 eblock;
 
-       if (*new) {
-               error = gfs2_meta_inode_buffer(ip, &bh);
-               if (!error) {
-                       gfs2_trans_add_bh(ip->i_gl, bh);
-                       gfs2_dinode_out(&ip->i_di, bh->b_data);
-                       brelse(bh);
+                       mp.mp_list[end_of_metadata]++;
+                       boundary = lookup_block(ip, bh, end_of_metadata, &mp, 0, &new, &eblock);
+                       if (eblock != ++dblock)
+                               break;
+                       bh_map->b_size += (1 << inode->i_blkbits);
+                       if (boundary)
+                               set_buffer_boundary(bh_map);
                }
        }
+out_brelse:
+       brelse(bh);
+out_ok:
+       error = 0;
+out_fail:
+       bmap_unlock(inode, create);
+       return error;
+}
 
- out:
-       if (create)
-               up_write(&ip->i_rw_mutex);
-       else
-               up_read(&ip->i_rw_mutex);
+int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
+{
+       struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
+       int ret;
+       int create = *new;
 
-       return error;
+       BUG_ON(!extlen);
+       BUG_ON(!dblock);
+       BUG_ON(!new);
+
+       bh.b_size = 1 << (inode->i_blkbits + 5);
+       ret = gfs2_block_map(inode, lblock, create, &bh);
+       *extlen = bh.b_size >> inode->i_blkbits;
+       *dblock = bh.b_blocknr;
+       if (buffer_new(&bh))
+               *new = 1;
+       else
+               *new = 0;
+       return ret;
 }
 
 /**
@@ -524,13 +589,13 @@ int gfs2_block_map(struct gfs2_inode *ip, uint64_t lblock, int *new,
 
 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
                          struct metapath *mp, unsigned int height,
-                         uint64_t block, int first, block_call_t bc,
+                         u64 block, int first, block_call_t bc,
                          void *data)
 {
-       struct gfs2_sbd *sdp = ip->i_sbd;
+       struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
        struct buffer_head *bh = NULL;
-       uint64_t *top, *bottom;
-       uint64_t bn;
+       __be64 *top, *bottom;
+       u64 bn;
        int error;
        int mh_size = sizeof(struct gfs2_meta_header);
 
@@ -540,19 +605,17 @@ static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
                        return error;
                dibh = bh;
 
-               top = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
-                       mp->mp_list[0];
-               bottom = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
-                       sdp->sd_diptrs;
+               top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
+               bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
        } else {
                error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
                if (error)
                        return error;
 
-               top = (uint64_t *)(bh->b_data + mh_size) +
-                                 ((first) ? mp->mp_list[height] : 0);
+               top = (__be64 *)(bh->b_data + mh_size) +
+                                 (first ? mp->mp_list[height] : 0);
 
-               bottom = (uint64_t *)(bh->b_data + mh_size) + sdp->sd_inptrs;
+               bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
        }
 
        error = bc(ip, dibh, bh, top, bottom, height, data);
@@ -572,9 +635,8 @@ static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
                                break;
                }
 
- out:
+out:
        brelse(bh);
-
        return error;
 }
 
@@ -592,15 +654,15 @@ static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
  */
 
 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
-                   struct buffer_head *bh, uint64_t *top, uint64_t *bottom,
+                   struct buffer_head *bh, __be64 *top, __be64 *bottom,
                    unsigned int height, void *data)
 {
-       struct strip_mine *sm = (struct strip_mine *)data;
-       struct gfs2_sbd *sdp = ip->i_sbd;
+       struct strip_mine *sm = data;
+       struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
        struct gfs2_rgrp_list rlist;
-       uint64_t bn, bstart;
-       uint32_t blen;
-       uint64_t *p;
+       u64 bn, bstart;
+       u32 blen;
+       __be64 *p;
        unsigned int rg_blocks = 0;
        int metadata;
        unsigned int revokes = 0;
@@ -618,7 +680,7 @@ static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
                sm->sm_first = 0;
        }
 
-       metadata = (height != ip->i_di.di_height - 1) || gfs2_is_jdata(ip);
+       metadata = (height != ip->i_di.di_height - 1);
        if (metadata)
                revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
 
@@ -656,8 +718,8 @@ static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
 
        for (x = 0; x < rlist.rl_rgrps; x++) {
                struct gfs2_rgrpd *rgd;
-               rgd = get_gl2rgd(rlist.rl_ghs[x].gh_gl);
-               rg_blocks += rgd->rd_ri.ri_length;
+               rgd = rlist.rl_ghs[x].gh_gl->gl_object;
+               rg_blocks += rgd->rd_length;
        }
 
        error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
@@ -672,8 +734,8 @@ static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
 
        down_write(&ip->i_rw_mutex);
 
-       gfs2_trans_add_bh(ip->i_gl, dibh);
-       gfs2_trans_add_bh(ip->i_gl, bh);
+       gfs2_trans_add_bh(ip->i_gl, dibh, 1);
+       gfs2_trans_add_bh(ip->i_gl, bh, 1);
 
        bstart = 0;
        blen = 0;
@@ -702,6 +764,7 @@ static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
                if (!ip->i_di.di_blocks)
                        gfs2_consist_inode(ip);
                ip->i_di.di_blocks--;
+               gfs2_set_inode_blocks(&ip->i_inode);
        }
        if (bstart) {
                if (metadata)
@@ -710,23 +773,20 @@ static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
                        gfs2_free_data(ip, bstart, blen);
        }
 
-       ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
+       ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
 
-       gfs2_dinode_out(&ip->i_di, dibh->b_data);
+       gfs2_dinode_out(ip, dibh->b_data);
 
        up_write(&ip->i_rw_mutex);
 
        gfs2_trans_end(sdp);
 
- out_rg_gunlock:
+out_rg_gunlock:
        gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
-
- out_rlist:
+out_rlist:
        gfs2_rlist_free(&rlist);
-
- out:
+out:
        gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh);
-
        return error;
 }
 
@@ -740,9 +800,9 @@ static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
  * Returns: errno
  */
 
-static int do_grow(struct gfs2_inode *ip, uint64_t size)
+static int do_grow(struct gfs2_inode *ip, u64 size)
 {
-       struct gfs2_sbd *sdp = ip->i_sbd;
+       struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
        struct gfs2_alloc *al;
        struct buffer_head *dibh;
        unsigned int h;
@@ -754,7 +814,7 @@ static int do_grow(struct gfs2_inode *ip, uint64_t size)
        if (error)
                goto out;
 
-       error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
+       error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
        if (error)
                goto out_gunlock_q;
 
@@ -765,15 +825,14 @@ static int do_grow(struct gfs2_inode *ip, uint64_t size)
                goto out_gunlock_q;
 
        error = gfs2_trans_begin(sdp,
-                       sdp->sd_max_height + al->al_rgd->rd_ri.ri_length +
+                       sdp->sd_max_height + al->al_rgd->rd_length +
                        RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
        if (error)
                goto out_ipres;
 
        if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
                if (gfs2_is_stuffed(ip)) {
-                       error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page,
-                                                   NULL);
+                       error = gfs2_unstuff_dinode(ip, NULL);
                        if (error)
                                goto out_end_trans;
                }
@@ -781,7 +840,7 @@ static int do_grow(struct gfs2_inode *ip, uint64_t size)
                h = calc_tree_height(ip, size);
                if (ip->i_di.di_height < h) {
                        down_write(&ip->i_rw_mutex);
-                       error = build_height(ip, h);
+                       error = build_height(&ip->i_inode, h);
                        up_write(&ip->i_rw_mutex);
                        if (error)
                                goto out_end_trans;
@@ -789,68 +848,109 @@ static int do_grow(struct gfs2_inode *ip, uint64_t size)
        }
 
        ip->i_di.di_size = size;
-       ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
+       ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
 
        error = gfs2_meta_inode_buffer(ip, &dibh);
        if (error)
                goto out_end_trans;
 
-       gfs2_trans_add_bh(ip->i_gl, dibh);
-       gfs2_dinode_out(&ip->i_di, dibh->b_data);
+       gfs2_trans_add_bh(ip->i_gl, dibh, 1);
+       gfs2_dinode_out(ip, dibh->b_data);
        brelse(dibh);
 
- out_end_trans:
+out_end_trans:
        gfs2_trans_end(sdp);
-
- out_ipres:
+out_ipres:
        gfs2_inplace_release(ip);
-
- out_gunlock_q:
+out_gunlock_q:
        gfs2_quota_unlock(ip);
-
- out:
+out:
        gfs2_alloc_put(ip);
-
        return error;
 }
 
-static int truncator_journaled(struct gfs2_inode *ip, uint64_t size)
+
+/**
+ * gfs2_block_truncate_page - Deal with zeroing out data for truncate
+ *
+ * This is partly borrowed from ext3.
+ */
+static int gfs2_block_truncate_page(struct address_space *mapping)
 {
-       uint64_t lbn, dbn;
-       uint32_t off;
+       struct inode *inode = mapping->host;
+       struct gfs2_inode *ip = GFS2_I(inode);
+       struct gfs2_sbd *sdp = GFS2_SB(inode);
+       loff_t from = inode->i_size;
+       unsigned long index = from >> PAGE_CACHE_SHIFT;
+       unsigned offset = from & (PAGE_CACHE_SIZE-1);
+       unsigned blocksize, iblock, length, pos;
        struct buffer_head *bh;
-       int new = 0;
-       int error;
+       struct page *page;
+       int err;
 
-       lbn = size;
-       off = do_div(lbn, ip->i_sbd->sd_jbsize);
+       page = grab_cache_page(mapping, index);
+       if (!page)
+               return 0;
 
-       error = gfs2_block_map(ip, lbn, &new, &dbn, NULL);
-       if (error || !dbn)
-               return error;
+       blocksize = inode->i_sb->s_blocksize;
+       length = blocksize - (offset & (blocksize - 1));
+       iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
 
-       error = gfs2_jdata_get_buffer(ip, dbn, 0, &bh);
-       if (error)
-               return error;
+       if (!page_has_buffers(page))
+               create_empty_buffers(page, blocksize, 0);
 
-       gfs2_trans_add_bh(ip->i_gl, bh);
-       gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header) + off);
+       /* Find the buffer that contains "offset" */
+       bh = page_buffers(page);
+       pos = blocksize;
+       while (offset >= pos) {
+               bh = bh->b_this_page;
+               iblock++;
+               pos += blocksize;
+       }
 
-       brelse(bh);
+       err = 0;
 
-       return 0;
+       if (!buffer_mapped(bh)) {
+               gfs2_get_block(inode, iblock, bh, 0);
+               /* unmapped? It's a hole - nothing to do */
+               if (!buffer_mapped(bh))
+                       goto unlock;
+       }
+
+       /* Ok, it's mapped. Make sure it's up-to-date */
+       if (PageUptodate(page))
+               set_buffer_uptodate(bh);
+
+       if (!buffer_uptodate(bh)) {
+               err = -EIO;
+               ll_rw_block(READ, 1, &bh);
+               wait_on_buffer(bh);
+               /* Uhhuh. Read error. Complain and punt. */
+               if (!buffer_uptodate(bh))
+                       goto unlock;
+               err = 0;
+       }
+
+       if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
+               gfs2_trans_add_bh(ip->i_gl, bh, 0);
+
+       zero_user_page(page, offset, length, KM_USER0);
+
+unlock:
+       unlock_page(page);
+       page_cache_release(page);
+       return err;
 }
 
-static int trunc_start(struct gfs2_inode *ip, uint64_t size,
-                      gfs2_truncator_t truncator)
+static int trunc_start(struct gfs2_inode *ip, u64 size)
 {
-       struct gfs2_sbd *sdp = ip->i_sbd;
+       struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
        struct buffer_head *dibh;
        int journaled = gfs2_is_jdata(ip);
        int error;
 
        error = gfs2_trans_begin(sdp,
-                                RES_DINODE + ((journaled) ? RES_JDATA : 0), 0);
+                                RES_DINODE + (journaled ? RES_JDATA : 0), 0);
        if (error)
                return error;
 
@@ -860,52 +960,43 @@ static int trunc_start(struct gfs2_inode *ip, uint64_t size,
 
        if (gfs2_is_stuffed(ip)) {
                ip->i_di.di_size = size;
-               ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
-               gfs2_trans_add_bh(ip->i_gl, dibh);
-               gfs2_dinode_out(&ip->i_di, dibh->b_data);
+               ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
+               gfs2_trans_add_bh(ip->i_gl, dibh, 1);
+               gfs2_dinode_out(ip, dibh->b_data);
                gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
                error = 1;
 
        } else {
-               if (journaled) {
-                       uint64_t junk = size;
-                       /* we're just interested in the modulus */
-                       if (do_div(junk, sdp->sd_jbsize))
-                               error = truncator_journaled(ip, size);
-               } else if (size & (uint64_t)(sdp->sd_sb.sb_bsize - 1))
-                       error = truncator(ip, size);
+               if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
+                       error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
 
                if (!error) {
                        ip->i_di.di_size = size;
-                       ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
+                       ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
                        ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
-                       gfs2_trans_add_bh(ip->i_gl, dibh);
-                       gfs2_dinode_out(&ip->i_di, dibh->b_data);
+                       gfs2_trans_add_bh(ip->i_gl, dibh, 1);
+                       gfs2_dinode_out(ip, dibh->b_data);
                }
        }
 
        brelse(dibh);
 
- out:
+out:
        gfs2_trans_end(sdp);
-
        return error;
 }
 
-static int trunc_dealloc(struct gfs2_inode *ip, uint64_t size)
+static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
 {
        unsigned int height = ip->i_di.di_height;
-       uint64_t lblock;
+       u64 lblock;
        struct metapath mp;
        int error;
 
        if (!size)
                lblock = 0;
-       else if (gfs2_is_jdata(ip)) {
-               lblock = size - 1;
-               do_div(lblock, ip->i_sbd->sd_jbsize);
-       } else
-               lblock = (size - 1) >> ip->i_sbd->sd_sb.sb_bsize_shift;
+       else
+               lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift;
 
        find_metapath(ip, lblock, &mp);
        gfs2_alloc_get(ip);
@@ -926,14 +1017,14 @@ static int trunc_dealloc(struct gfs2_inode *ip, uint64_t size)
 
        gfs2_quota_unhold(ip);
 
- out:
+out:
        gfs2_alloc_put(ip);
        return error;
 }
 
 static int trunc_end(struct gfs2_inode *ip)
 {
-       struct gfs2_sbd *sdp = ip->i_sbd;
+       struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
        struct buffer_head *dibh;
        int error;
 
@@ -951,21 +1042,19 @@ static int trunc_end(struct gfs2_inode *ip)
                ip->i_di.di_height = 0;
                ip->i_di.di_goal_meta =
                        ip->i_di.di_goal_data =
-                       ip->i_num.no_addr;
+                       ip->i_no_addr;
                gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
        }
-       ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
+       ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
        ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
 
-       gfs2_trans_add_bh(ip->i_gl, dibh);
-       gfs2_dinode_out(&ip->i_di, dibh->b_data);
+       gfs2_trans_add_bh(ip->i_gl, dibh, 1);
+       gfs2_dinode_out(ip, dibh->b_data);
        brelse(dibh);
 
- out:
+out:
        up_write(&ip->i_rw_mutex);
-
        gfs2_trans_end(sdp);
-
        return error;
 }
 
@@ -980,12 +1069,11 @@ static int trunc_end(struct gfs2_inode *ip)
  * Returns: errno
  */
 
-static int do_shrink(struct gfs2_inode *ip, uint64_t size,
-                    gfs2_truncator_t truncator)
+static int do_shrink(struct gfs2_inode *ip, u64 size)
 {
        int error;
 
-       error = trunc_start(ip, size, truncator);
+       error = trunc_start(ip, size);
        if (error < 0)
                return error;
        if (error > 0)
@@ -998,6 +1086,33 @@ static int do_shrink(struct gfs2_inode *ip, uint64_t size,
        return error;
 }
 
+static int do_touch(struct gfs2_inode *ip, u64 size)
+{
+       struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
+       struct buffer_head *dibh;
+       int error;
+
+       error = gfs2_trans_begin(sdp, RES_DINODE, 0);
+       if (error)
+               return error;
+
+       down_write(&ip->i_rw_mutex);
+
+       error = gfs2_meta_inode_buffer(ip, &dibh);
+       if (error)
+               goto do_touch_out;
+
+       ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
+       gfs2_trans_add_bh(ip->i_gl, dibh, 1);
+       gfs2_dinode_out(ip, dibh->b_data);
+       brelse(dibh);
+
+do_touch_out:
+       up_write(&ip->i_rw_mutex);
+       gfs2_trans_end(sdp);
+       return error;
+}
+
 /**
  * gfs2_truncatei - make a file a given size
  * @ip: the inode
@@ -1009,18 +1124,20 @@ static int do_shrink(struct gfs2_inode *ip, uint64_t size,
  * Returns: errno
  */
 
-int gfs2_truncatei(struct gfs2_inode *ip, uint64_t size,
-                  gfs2_truncator_t truncator)
+int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
 {
        int error;
 
-       if (gfs2_assert_warn(ip->i_sbd, S_ISREG(ip->i_di.di_mode)))
+       if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode)))
                return -EINVAL;
 
        if (size > ip->i_di.di_size)
                error = do_grow(ip, size);
+       else if (size < ip->i_di.di_size)
+               error = do_shrink(ip, size);
        else
-               error = do_shrink(ip, size, truncator);
+               /* update time stamps */
+               error = do_touch(ip, size);
 
        return error;
 }
@@ -1051,11 +1168,11 @@ int gfs2_file_dealloc(struct gfs2_inode *ip)
 void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
                            unsigned int *data_blocks, unsigned int *ind_blocks)
 {
-       struct gfs2_sbd *sdp = ip->i_sbd;
+       struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
        unsigned int tmp;
 
-       if (gfs2_is_jdata(ip)) {
-               *data_blocks = DIV_RU(len, sdp->sd_jbsize) + 2;
+       if (gfs2_is_dir(ip)) {
+               *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
                *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
        } else {
                *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
@@ -1063,7 +1180,7 @@ void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
        }
 
        for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
-               tmp = DIV_RU(tmp, sdp->sd_inptrs);
+               tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
                *ind_blocks += tmp;
        }
 }
@@ -1078,12 +1195,12 @@ void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
  * Returns: errno
  */
 
-int gfs2_write_alloc_required(struct gfs2_inode *ip, uint64_t offset,
+int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
                              unsigned int len, int *alloc_required)
 {
-       struct gfs2_sbd *sdp = ip->i_sbd;
-       uint64_t lblock, lblock_stop, dblock;
-       uint32_t extlen;
+       struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
+       u64 lblock, lblock_stop, dblock;
+       u32 extlen;
        int new = 0;
        int error = 0;
 
@@ -1099,7 +1216,7 @@ int gfs2_write_alloc_required(struct gfs2_inode *ip, uint64_t offset,
                return 0;
        }
 
-       if (gfs2_is_jdata(ip)) {
+       if (gfs2_is_dir(ip)) {
                unsigned int bsize = sdp->sd_jbsize;
                lblock = offset;
                do_div(lblock, bsize);
@@ -1112,7 +1229,7 @@ int gfs2_write_alloc_required(struct gfs2_inode *ip, uint64_t offset,
        }
 
        for (; lblock < lblock_stop; lblock += extlen) {
-               error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen);
+               error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen);
                if (error)
                        return error;