anon-inodes use open coded atomic_inc for the shared inode
[safe/jmp/linux-2.6] / fs / gfs2 / bmap.c
index cc57f2e..93fa427 100644 (file)
@@ -7,7 +7,6 @@
  * of the GNU General Public License version 2.
  */
 
-#include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 #include <linux/completion.h>
@@ -38,8 +37,8 @@ struct metapath {
 };
 
 typedef int (*block_call_t) (struct gfs2_inode *ip, struct buffer_head *dibh,
-                            struct buffer_head *bh, u64 *top,
-                            u64 *bottom, unsigned int height,
+                            struct buffer_head *bh, __be64 *top,
+                            __be64 *bottom, unsigned int height,
                             void *data);
 
 struct strip_mine {
@@ -94,9 +93,10 @@ static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
                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);
-       mark_buffer_dirty(bh);
 
        if (release) {
                unlock_page(page);
@@ -163,6 +163,7 @@ int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
        if (ip->i_di.di_size) {
                *(__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);
        }
 
@@ -230,7 +231,7 @@ static int build_height(struct inode *inode, unsigned height)
        struct buffer_head *blocks[GFS2_MAX_META_HEIGHT];
        struct gfs2_dinode *di;
        int error;
-       u64 *bp;
+       __be64 *bp;
        u64 bn;
        unsigned n;
 
@@ -255,7 +256,7 @@ static int build_height(struct inode *inode, unsigned height)
                                          GFS2_FORMAT_IN);
                        gfs2_buffer_clear_tail(blocks[n],
                                               sizeof(struct gfs2_meta_header));
-                       bp = (u64 *)(blocks[n]->b_data +
+                       bp = (__be64 *)(blocks[n]->b_data +
                                     sizeof(struct gfs2_meta_header));
                        *bp = cpu_to_be64(blocks[n+1]->b_blocknr);
                        brelse(blocks[n]);
@@ -272,6 +273,7 @@ static int build_height(struct inode *inode, unsigned height)
        *(__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);
@@ -360,15 +362,15 @@ static void find_metapath(struct gfs2_inode *ip, u64 block,
  * metadata tree.
  */
 
-static inline u64 *metapointer(struct buffer_head *bh, int *boundary,
+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);
-       u64 *ptr;
+       __be64 *ptr;
        *boundary = 0;
-       ptr = ((u64 *)(bh->b_data + head_size)) + mp->mp_list[height];
-       if (ptr + 1 == (u64 *)(bh->b_data + bh->b_size))
+       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;
 }
@@ -394,7 +396,7 @@ static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
                        int *new, u64 *block)
 {
        int boundary;
-       u64 *ptr = metapointer(bh, &boundary, height, mp);
+       __be64 *ptr = metapointer(bh, &boundary, height, mp);
 
        if (*ptr) {
                *block = be64_to_cpu(*ptr);
@@ -415,17 +417,35 @@ static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
 
        *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_pointers - Map a block from an inode to a disk block
+ * gfs2_block_map - Map a block from an inode to a disk block
  * @inode: The inode
  * @lblock: The logical block number
- * @map_bh: The bh to be mapped
- * @mp: metapath to use
+ * @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.
@@ -433,9 +453,8 @@ static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
  * Returns: errno
  */
 
-static int gfs2_block_pointers(struct inode *inode, u64 lblock, int create,
-                              struct buffer_head *bh_map, struct metapath *mp,
-                              unsigned int maxlen)
+int gfs2_block_map(struct inode *inode, u64 lblock, int create,
+                  struct buffer_head *bh_map)
 {
        struct gfs2_inode *ip = GFS2_I(inode);
        struct gfs2_sbd *sdp = GFS2_SB(inode);
@@ -448,57 +467,62 @@ static int gfs2_block_pointers(struct inode *inode, u64 lblock, int create,
        int new = 0;
        u64 dblock = 0;
        int boundary;
+       unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
+       struct metapath mp;
+       u64 size;
 
        BUG_ON(maxlen == 0);
 
        if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
                return 0;
 
+       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;
-
-       height = calc_tree_height(ip, (lblock + 1) * bsize);
-       if (ip->i_di.di_height < height) {
-               if (!create)
-                       return 0;
-
-               error = build_height(inode, height);
-               if (error)
-                       return error;
+       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);
+       find_metapath(ip, lblock, &mp);
        end_of_metadata = ip->i_di.di_height - 1;
-
        error = gfs2_meta_inode_buffer(ip, &bh);
        if (error)
-               return error;
+               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)
-                       return 0;
+                       goto out_ok;
 
                error = gfs2_meta_indirect_buffer(ip, x+1, dblock, new, &bh);
                if (error)
-                       return error;
+                       goto out_fail;
        }
 
-       boundary = lookup_block(ip, bh, end_of_metadata, mp, create, &new, &dblock);
-       clear_buffer_mapped(bh_map);
-       clear_buffer_new(bh_map);
-       clear_buffer_boundary(bh_map);
-
+       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);
+                       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->i_di, dibh->b_data);
+                               gfs2_dinode_out(ip, dibh->b_data);
                                brelse(dibh);
                        }
                        set_buffer_new(bh_map);
@@ -507,8 +531,8 @@ static int gfs2_block_pointers(struct inode *inode, u64 lblock, int create,
                while(--maxlen && !buffer_boundary(bh_map)) {
                        u64 eblock;
 
-                       mp->mp_list[end_of_metadata]++;
-                       boundary = lookup_block(ip, bh, end_of_metadata, mp, 0, &new, &eblock);
+                       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);
@@ -518,44 +542,16 @@ static int gfs2_block_pointers(struct inode *inode, u64 lblock, int create,
        }
 out_brelse:
        brelse(bh);
-       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);
-}
-
-int gfs2_block_map(struct inode *inode, u64 lblock, int create,
-                  struct buffer_head *bh, unsigned int maxlen)
-{
-       struct metapath mp;
-       int ret;
-
-       bmap_lock(inode, create);
-       ret = gfs2_block_pointers(inode, lblock, create, bh, &mp, maxlen);
+out_ok:
+       error = 0;
+out_fail:
        bmap_unlock(inode, create);
-       return ret;
+       return error;
 }
 
 int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
 {
-       struct metapath mp;
-       struct buffer_head bh = { .b_state = 0, .b_blocknr = 0, .b_size = 0 };
+       struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
        int ret;
        int create = *new;
 
@@ -563,9 +559,8 @@ int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsi
        BUG_ON(!dblock);
        BUG_ON(!new);
 
-       bmap_lock(inode, create);
-       ret = gfs2_block_pointers(inode, lblock, create, &bh, &mp, 32);
-       bmap_unlock(inode, create);
+       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))
@@ -599,7 +594,7 @@ static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
 {
        struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
        struct buffer_head *bh = NULL;
-       u64 *top, *bottom;
+       __be64 *top, *bottom;
        u64 bn;
        int error;
        int mh_size = sizeof(struct gfs2_meta_header);
@@ -610,17 +605,17 @@ static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
                        return error;
                dibh = bh;
 
-               top = (u64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
-               bottom = (u64 *)(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 = (u64 *)(bh->b_data + mh_size) +
+               top = (__be64 *)(bh->b_data + mh_size) +
                                  (first ? mp->mp_list[height] : 0);
 
-               bottom = (u64 *)(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);
@@ -659,7 +654,7 @@ out:
  */
 
 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
-                   struct buffer_head *bh, u64 *top, u64 *bottom,
+                   struct buffer_head *bh, __be64 *top, __be64 *bottom,
                    unsigned int height, void *data)
 {
        struct strip_mine *sm = data;
@@ -667,7 +662,7 @@ static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
        struct gfs2_rgrp_list rlist;
        u64 bn, bstart;
        u32 blen;
-       u64 *p;
+       __be64 *p;
        unsigned int rg_blocks = 0;
        int metadata;
        unsigned int revokes = 0;
@@ -724,7 +719,7 @@ 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 = rlist.rl_ghs[x].gh_gl->gl_object;
-               rg_blocks += rgd->rd_ri.ri_length;
+               rg_blocks += rgd->rd_length;
        }
 
        error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
@@ -769,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)
@@ -777,9 +773,9 @@ 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);
 
@@ -818,7 +814,7 @@ static int do_grow(struct gfs2_inode *ip, u64 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;
 
@@ -829,7 +825,7 @@ static int do_grow(struct gfs2_inode *ip, u64 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;
@@ -852,14 +848,14 @@ static int do_grow(struct gfs2_inode *ip, u64 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, 1);
-       gfs2_dinode_out(&ip->i_di, dibh->b_data);
+       gfs2_dinode_out(ip, dibh->b_data);
        brelse(dibh);
 
 out_end_trans:
@@ -890,7 +886,6 @@ static int gfs2_block_truncate_page(struct address_space *mapping)
        unsigned blocksize, iblock, length, pos;
        struct buffer_head *bh;
        struct page *page;
-       void *kaddr;
        int err;
 
        page = grab_cache_page(mapping, index);
@@ -933,15 +928,13 @@ static int gfs2_block_truncate_page(struct address_space *mapping)
                /* 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);
 
-       kaddr = kmap_atomic(page, KM_USER0);
-       memset(kaddr + offset, 0, length);
-       flush_dcache_page(page);
-       kunmap_atomic(kaddr, KM_USER0);
+       zero_user_page(page, offset, length, KM_USER0);
 
 unlock:
        unlock_page(page);
@@ -967,9 +960,9 @@ static int trunc_start(struct gfs2_inode *ip, u64 size)
 
        if (gfs2_is_stuffed(ip)) {
                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;
                gfs2_trans_add_bh(ip->i_gl, dibh, 1);
-               gfs2_dinode_out(&ip->i_di, dibh->b_data);
+               gfs2_dinode_out(ip, dibh->b_data);
                gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
                error = 1;
 
@@ -979,10 +972,10 @@ static int trunc_start(struct gfs2_inode *ip, u64 size)
 
                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, 1);
-                       gfs2_dinode_out(&ip->i_di, dibh->b_data);
+                       gfs2_dinode_out(ip, dibh->b_data);
                }
        }
 
@@ -1049,14 +1042,14 @@ 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, 1);
-       gfs2_dinode_out(&ip->i_di, dibh->b_data);
+       gfs2_dinode_out(ip, dibh->b_data);
        brelse(dibh);
 
 out:
@@ -1093,6 +1086,33 @@ static int do_shrink(struct gfs2_inode *ip, u64 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
@@ -1108,13 +1128,16 @@ int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
 {
        int error;
 
-       if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), 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
+       else if (size < ip->i_di.di_size)
                error = do_shrink(ip, size);
+       else
+               /* update time stamps */
+               error = do_touch(ip, size);
 
        return error;
 }