[GFS2] Update gfs2_trans_add_unrevoke to accept extents
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / gfs2 / bmap.c
index e4effc47abfc865f82f71b6855f06521253c25b5..651e5320bb4f9112f87dcc2681df86131f85471f 100644 (file)
@@ -33,6 +33,7 @@
  * keep it small.
  */
 struct metapath {
+       struct buffer_head *mp_bh[GFS2_MAX_META_HEIGHT];
        __u16 mp_list[GFS2_MAX_META_HEIGHT];
 };
 
@@ -137,7 +138,7 @@ int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
 
                if (isdir) {
                        block = gfs2_alloc_meta(ip);
-
+                       gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), block, 1);
                        error = gfs2_dir_get_new_buffer(ip, block, &bh);
                        if (error)
                                goto out_brelse;
@@ -166,7 +167,7 @@ int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
                di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
        }
 
-       ip->i_di.di_height = 1;
+       ip->i_height = 1;
        di->di_height = cpu_to_be16(1);
 
 out_brelse:
@@ -176,43 +177,6 @@ out:
        return error;
 }
 
-/**
- * calc_tree_height - Calculate the height of a metadata tree
- * @ip: The GFS2 inode
- * @size: The proposed size of the file
- *
- * Work out how tall a metadata tree needs to be in order to accommodate a
- * file of a particular size. If size is less than the current size of
- * the inode, then the current size of the inode is used instead of the
- * supplied one.
- *
- * Returns: the height the tree should be
- */
-
-static unsigned int calc_tree_height(struct gfs2_inode *ip, u64 size)
-{
-       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_dir(ip)) {
-               arr = sdp->sd_jheightsize;
-               max = sdp->sd_max_jheight;
-       } else {
-               arr = sdp->sd_heightsize;
-               max = sdp->sd_max_height;
-       }
-
-       for (height = 0; height < max; height++)
-               if (arr[height] >= size)
-                       break;
-
-       return height;
-}
-
 /**
  * build_height - Build a metadata tree of the requested height
  * @ip: The GFS2 inode
@@ -222,19 +186,18 @@ static unsigned int calc_tree_height(struct gfs2_inode *ip, u64 size)
  * Returns: errno
  */
 
-static int build_height(struct inode *inode, unsigned height)
+static int build_height(struct inode *inode, struct metapath *mp, unsigned height)
 {
        struct gfs2_inode *ip = GFS2_I(inode);
-       unsigned new_height = height - ip->i_di.di_height;
+       unsigned new_height = height - ip->i_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;
 
-       if (height <= ip->i_di.di_height)
+       if (height <= ip->i_height)
                return 0;
 
        error = gfs2_meta_inode_buffer(ip, &dibh);
@@ -243,37 +206,38 @@ static int build_height(struct inode *inode, unsigned height)
 
        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);
+               gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, 1);
+               mp->mp_bh[n] = gfs2_meta_new(ip->i_gl, bn);
+               gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[n], 1);
        }
 
        n = 0;
-       bn = blocks[0]->b_blocknr;
+       bn = mp->mp_bh[0]->b_blocknr;
        if (new_height > 1) {
                for(; n < new_height-1; n++) {
-                       gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN,
+                       gfs2_metatype_set(mp->mp_bh[n], GFS2_METATYPE_IN,
                                          GFS2_FORMAT_IN);
-                       gfs2_buffer_clear_tail(blocks[n],
+                       gfs2_buffer_clear_tail(mp->mp_bh[n],
                                               sizeof(struct gfs2_meta_header));
-                       bp = (__be64 *)(blocks[n]->b_data +
+                       bp = (__be64 *)(mp->mp_bh[n]->b_data +
                                     sizeof(struct gfs2_meta_header));
-                       *bp = cpu_to_be64(blocks[n+1]->b_blocknr);
-                       brelse(blocks[n]);
-                       blocks[n] = NULL;
+                       *bp = cpu_to_be64(mp->mp_bh[n+1]->b_blocknr);
+                       brelse(mp->mp_bh[n]);
+                       mp->mp_bh[n] = NULL;
                }
        }
-       gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
-       gfs2_buffer_copy_tail(blocks[n], sizeof(struct gfs2_meta_header),
+       gfs2_metatype_set(mp->mp_bh[n], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
+       gfs2_buffer_copy_tail(mp->mp_bh[n], sizeof(struct gfs2_meta_header),
                              dibh, sizeof(struct gfs2_dinode));
-       brelse(blocks[n]);
+       brelse(mp->mp_bh[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_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_height = cpu_to_be16(ip->i_height);
        di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
        brelse(dibh);
        return error;
@@ -342,17 +306,15 @@ static void find_metapath(struct gfs2_inode *ip, u64 block,
                          struct metapath *mp)
 {
        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] = do_div(b, sdp->sd_inptrs);
+       for (i = ip->i_height; i--;)
+               mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
 
 }
 
 /**
  * metapointer - Return pointer to start of metadata in a buffer
- * @bh: The buffer
  * @height: The metadata height (0 = dinode)
  * @mp: The metapath
  *
@@ -361,9 +323,10 @@ static void find_metapath(struct gfs2_inode *ip, u64 block,
  * metadata tree.
  */
 
-static inline __be64 *metapointer(struct buffer_head *bh, int *boundary,
-                              unsigned int height, const struct metapath *mp)
+static inline __be64 *metapointer(int *boundary, unsigned int height,
+                                 const struct metapath *mp)
 {
+       struct buffer_head *bh = mp->mp_bh[height];
        unsigned int head_size = (height > 0) ?
                sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
        __be64 *ptr;
@@ -377,7 +340,6 @@ static inline __be64 *metapointer(struct buffer_head *bh, int *boundary,
 /**
  * lookup_block - Get the next metadata block in metadata tree
  * @ip: The GFS2 inode
- * @bh: Buffer containing the pointers to metadata blocks
  * @height: The height of the tree (0 = dinode)
  * @mp: The metapath
  * @create: Non-zero if we may create a new meatdata block
@@ -390,12 +352,12 @@ static inline __be64 *metapointer(struct buffer_head *bh, int *boundary,
  *
  */
 
-static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
-                       unsigned int height, struct metapath *mp, int create,
+static int lookup_block(struct gfs2_inode *ip, unsigned int height,
+                       struct metapath *mp, int create,
                        int *new, u64 *block)
 {
        int boundary;
-       __be64 *ptr = metapointer(bh, &boundary, height, mp);
+       __be64 *ptr = metapointer(&boundary, height, mp);
 
        if (*ptr) {
                *block = be64_to_cpu(*ptr);
@@ -407,12 +369,14 @@ static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
        if (!create)
                return 0;
 
-       if (height == ip->i_di.di_height - 1 && !gfs2_is_dir(ip))
+       if (height == ip->i_height - 1 && !gfs2_is_dir(ip))
                *block = gfs2_alloc_data(ip);
-       else
+       else {
                *block = gfs2_alloc_meta(ip);
+               gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), *block, 1);
+       }
 
-       gfs2_trans_add_bh(ip->i_gl, bh, 1);
+       gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[height], 1);
 
        *ptr = cpu_to_be64(*block);
        ip->i_di.di_blocks++;
@@ -422,6 +386,41 @@ static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
        return 0;
 }
 
+static int lookup_metapath(struct inode *inode, struct metapath *mp,
+                          int create, int *new, u64 *dblock)
+{
+       struct buffer_head *bh;
+       struct gfs2_inode *ip = GFS2_I(inode);
+       unsigned int end_of_metadata = ip->i_height - 1;
+       unsigned int x;
+       int ret = gfs2_meta_inode_buffer(ip, &bh);
+       if (ret)
+               return ret;
+
+       mp->mp_bh[0] = bh;
+
+       for (x = 0; x < end_of_metadata; x++) {
+               lookup_block(ip, x, mp, create, new, dblock);
+               if (!dblock)
+                       return 0;
+
+               ret = gfs2_meta_indirect_buffer(ip, x+1, *dblock, *new, &mp->mp_bh[x+1]);
+               if (ret)
+                       return ret;
+       }
+
+       return lookup_block(ip, end_of_metadata, mp, create, new, dblock);
+}
+
+static void release_metapath(struct metapath *mp)
+{
+       int i;
+
+       for (i = 0; i < GFS2_MAX_META_HEIGHT; i++)
+               if (mp->mp_bh[i])
+                       brelse(mp->mp_bh[i]);
+}
+
 static inline void bmap_lock(struct inode *inode, int create)
 {
        struct gfs2_inode *ip = GFS2_I(inode);
@@ -457,11 +456,7 @@ int gfs2_block_map(struct inode *inode, sector_t lblock,
 {
        struct gfs2_inode *ip = GFS2_I(inode);
        struct gfs2_sbd *sdp = GFS2_SB(inode);
-       struct buffer_head *bh;
-       unsigned int bsize;
-       unsigned int height;
-       unsigned int end_of_metadata;
-       unsigned int x;
+       unsigned int bsize = sdp->sd_sb.sb_bsize;
        int error = 0;
        int new = 0;
        u64 dblock = 0;
@@ -469,67 +464,54 @@ int gfs2_block_map(struct inode *inode, sector_t lblock,
        unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
        struct metapath mp;
        u64 size;
-       struct buffer_head *dibh = NULL;
-
+       const u64 *arr = sdp->sd_heightsize;
        BUG_ON(maxlen == 0);
 
        if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
                return 0;
 
+       memset(mp.mp_bh, 0, sizeof(mp.mp_bh));
        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;
-               }
+       if (gfs2_is_dir(ip)) {
+               bsize = sdp->sd_jbsize;
+               arr = sdp->sd_jheightsize;
        }
+       size = (lblock + 1) * bsize;
 
-       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_fail;
-       dibh = bh;
-       get_bh(dibh);
-
-       for (x = 0; x < end_of_metadata; x++) {
-               lookup_block(ip, bh, x, &mp, create, &new, &dblock);
-               brelse(bh);
-               if (!dblock)
+       if (size > arr[ip->i_height]) {
+               u8 height = ip->i_height;
+               if (!create)
                        goto out_ok;
-
-               error = gfs2_meta_indirect_buffer(ip, x+1, dblock, new, &bh);
+               while (size > arr[height])
+                       height++;
+               error = build_height(inode, &mp, height);
                if (error)
                        goto out_fail;
        }
 
-       boundary = lookup_block(ip, bh, end_of_metadata, &mp, create, &new, &dblock);
+       find_metapath(ip, lblock, &mp);
+       error = lookup_metapath(inode, &mp, create, &new, &dblock);
+       if (error < 0)
+               goto out_fail;
+       boundary = error;
+
        if (dblock) {
                map_bh(bh_map, inode->i_sb, dblock);
                if (boundary)
                        set_buffer_boundary(bh_map);
                if (new) {
-                       gfs2_trans_add_bh(ip->i_gl, dibh, 1);
-                       gfs2_dinode_out(ip, dibh->b_data);
+                       gfs2_trans_add_bh(ip->i_gl, mp.mp_bh[0], 1);
+                       gfs2_dinode_out(ip, mp.mp_bh[0]->b_data);
                        set_buffer_new(bh_map);
-                       goto out_brelse;
+                       goto out_ok;
                }
                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[ip->i_height - 1]++;
+                       boundary = lookup_block(ip, ip->i_height - 1, &mp, 0, &new, &eblock);
                        if (eblock != ++dblock)
                                break;
                        bh_map->b_size += (1 << inode->i_blkbits);
@@ -537,17 +519,17 @@ int gfs2_block_map(struct inode *inode, sector_t lblock,
                                set_buffer_boundary(bh_map);
                }
        }
-out_brelse:
-       brelse(bh);
 out_ok:
        error = 0;
 out_fail:
-       if (dibh)
-               brelse(dibh);
+       release_metapath(&mp);
        bmap_unlock(inode, create);
        return error;
 }
 
+/*
+ * Deprecated: do not use in new code
+ */
 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 };
@@ -621,7 +603,7 @@ static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
        if (error)
                goto out;
 
-       if (height < ip->i_di.di_height - 1)
+       if (height < ip->i_height - 1)
                for (; top < bottom; top++, first = 0) {
                        if (!*top)
                                continue;
@@ -679,7 +661,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);
+       metadata = (height != ip->i_height - 1);
        if (metadata)
                revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
 
@@ -713,7 +695,7 @@ static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
        else
                goto out; /* Nothing to do */
 
-       gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
+       gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
 
        for (x = 0; x < rlist.rl_rgrps; x++) {
                struct gfs2_rgrpd *rgd;
@@ -804,7 +786,6 @@ static int do_grow(struct gfs2_inode *ip, u64 size)
        struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
        struct gfs2_alloc *al;
        struct buffer_head *dibh;
-       unsigned int h;
        int error;
 
        al = gfs2_alloc_get(ip);
@@ -830,20 +811,24 @@ static int do_grow(struct gfs2_inode *ip, u64 size)
                goto out_ipres;
 
        if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
+               const u64 *arr = sdp->sd_heightsize;
                if (gfs2_is_stuffed(ip)) {
                        error = gfs2_unstuff_dinode(ip, NULL);
                        if (error)
                                goto out_end_trans;
                }
 
-               h = calc_tree_height(ip, size);
-               if (ip->i_di.di_height < h) {
-                       down_write(&ip->i_rw_mutex);
-                       error = build_height(&ip->i_inode, h);
-                       up_write(&ip->i_rw_mutex);
-                       if (error)
-                               goto out_end_trans;
+               down_write(&ip->i_rw_mutex);
+               if (size > arr[ip->i_height]) {
+                       struct metapath mp;
+                       u8 height = ip->i_height;
+                       while(size > arr[height])
+                               height++;
+                       error = build_height(&ip->i_inode, &mp, height);
                }
+               up_write(&ip->i_rw_mutex);
+               if (error)
+                       goto out_end_trans;
        }
 
        ip->i_di.di_size = size;
@@ -932,7 +917,7 @@ static int gfs2_block_truncate_page(struct address_space *mapping)
        if (!gfs2_is_writeback(ip))
                gfs2_trans_add_bh(ip->i_gl, bh, 0);
 
-       zero_user_page(page, offset, length, KM_USER0);
+       zero_user(page, offset, length);
 
 unlock:
        unlock_page(page);
@@ -986,7 +971,7 @@ out:
 
 static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
 {
-       unsigned int height = ip->i_di.di_height;
+       unsigned int height = ip->i_height;
        u64 lblock;
        struct metapath mp;
        int error;
@@ -1037,10 +1022,8 @@ static int trunc_end(struct gfs2_inode *ip)
                goto out;
 
        if (!ip->i_di.di_size) {
-               ip->i_di.di_height = 0;
-               ip->i_di.di_goal_meta =
-                       ip->i_di.di_goal_data =
-                       ip->i_no_addr;
+               ip->i_height = 0;
+               ip->i_goal = ip->i_no_addr;
                gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
        }
        ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
@@ -1197,10 +1180,9 @@ int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
                              unsigned int len, int *alloc_required)
 {
        struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
-       u64 lblock, lblock_stop, dblock;
-       u32 extlen;
-       int new = 0;
-       int error = 0;
+       struct buffer_head bh;
+       unsigned int shift;
+       u64 lblock, lblock_stop, size;
 
        *alloc_required = 0;
 
@@ -1214,6 +1196,8 @@ int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
                return 0;
        }
 
+       *alloc_required = 1;
+       shift = sdp->sd_sb.sb_bsize_shift;
        if (gfs2_is_dir(ip)) {
                unsigned int bsize = sdp->sd_jbsize;
                lblock = offset;
@@ -1221,27 +1205,25 @@ int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
                lblock_stop = offset + len + bsize - 1;
                do_div(lblock_stop, bsize);
        } else {
-               unsigned int shift = sdp->sd_sb.sb_bsize_shift;
                u64 end_of_file = (ip->i_di.di_size + sdp->sd_sb.sb_bsize - 1) >> shift;
                lblock = offset >> shift;
                lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
-               if (lblock_stop > end_of_file) {
-                       *alloc_required = 1;
+               if (lblock_stop > end_of_file)
                        return 0;
-               }
        }
 
-       for (; lblock < lblock_stop; lblock += extlen) {
-               error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen);
-               if (error)
-                       return error;
-
-               if (!dblock) {
-                       *alloc_required = 1;
+       size = (lblock_stop - lblock) << shift;
+       do {
+               bh.b_state = 0;
+               bh.b_size = size;
+               gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
+               if (!buffer_mapped(&bh))
                        return 0;
-               }
-       }
+               size -= bh.b_size;
+               lblock += (bh.b_size >> ip->i_inode.i_blkbits);
+       } while(size > 0);
 
+       *alloc_required = 0;
        return 0;
 }