GFS2: Fix error code
[safe/jmp/linux-2.6] / fs / gfs2 / rgrp.c
index 3d0193a..503b842 100644 (file)
@@ -179,7 +179,7 @@ static inline u64 gfs2_bit_search(const __le64 *ptr, u64 mask, u8 state)
  * always aligned to a 64 bit boundary.
  *
  * The size of the buffer is in bytes, but is it assumed that it is
- * always ok to to read a complete multiple of 64 bits at the end
+ * always ok to read a complete multiple of 64 bits at the end
  * of the block in case the end is no aligned to a natural boundary.
  *
  * Return: the block number (bitmap buffer scope) that was found
@@ -591,11 +591,7 @@ static int gfs2_ri_update(struct gfs2_inode *ip)
        u64 rgrp_count = ip->i_disksize;
        int error;
 
-       if (do_div(rgrp_count, sizeof(struct gfs2_rindex))) {
-               gfs2_consist_inode(ip);
-               return -EIO;
-       }
-
+       do_div(rgrp_count, sizeof(struct gfs2_rindex));
        clear_rgrpdi(sdp);
 
        file_ra_state_init(&ra_state, inode->i_mapping);
@@ -857,7 +853,8 @@ static void gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
                                        goto start_new_extent;
                                if ((start + nr_sects) != blk) {
                                        rv = blkdev_issue_discard(bdev, start,
-                                                           nr_sects, GFP_NOFS);
+                                                           nr_sects, GFP_NOFS,
+                                                           DISCARD_FL_BARRIER);
                                        if (rv)
                                                goto fail;
                                        nr_sects = 0;
@@ -871,7 +868,8 @@ start_new_extent:
                }
        }
        if (nr_sects) {
-               rv = blkdev_issue_discard(bdev, start, nr_sects, GFP_NOFS);
+               rv = blkdev_issue_discard(bdev, start, nr_sects, GFP_NOFS,
+                                        DISCARD_FL_BARRIER);
                if (rv)
                        goto fail;
        }
@@ -913,7 +911,7 @@ void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
 struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
 {
        BUG_ON(ip->i_alloc != NULL);
-       ip->i_alloc = kzalloc(sizeof(struct gfs2_alloc), GFP_KERNEL);
+       ip->i_alloc = kzalloc(sizeof(struct gfs2_alloc), GFP_NOFS);
        return ip->i_alloc;
 }
 
@@ -1256,7 +1254,7 @@ void gfs2_inplace_release(struct gfs2_inode *ip)
  * Returns: The block type (GFS2_BLKST_*)
  */
 
-unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
+static unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
 {
        struct gfs2_bitmap *bi = NULL;
        u32 length, rgrp_block, buf_block;
@@ -1463,7 +1461,7 @@ static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd)
 {
        struct gfs2_sbd *sdp = rgd->rd_sbd;
        fs_warn(sdp, "rgrp %llu has an error, marking it readonly until umount\n",
-               (unsigned long long)rgd->rd_addr);
+               (unsigned long long)rgd->rd_addr);
        fs_warn(sdp, "umount on all nodes and run fsck.gfs2 to fix the error\n");
        gfs2_rgrp_dump(NULL, rgd->rd_gl);
        rgd->rd_flags |= GFS2_RDF_ERROR;
@@ -1561,10 +1559,14 @@ int gfs2_alloc_di(struct gfs2_inode *dip, u64 *bn, u64 *generation)
 
        rgd->rd_last_alloc = blk;
        block = rgd->rd_data0 + blk;
-       gfs2_assert_withdraw(sdp, rgd->rd_free);
+       if (rgd->rd_free == 0)
+               goto rgrp_error;
+
        rgd->rd_free--;
        rgd->rd_dinodes++;
        *generation = rgd->rd_igeneration++;
+       if (*generation == 0)
+               *generation = rgd->rd_igeneration++;
        gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
        gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
 
@@ -1690,6 +1692,52 @@ void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
 }
 
 /**
+ * gfs2_check_blk_type - Check the type of a block
+ * @sdp: The superblock
+ * @no_addr: The block number to check
+ * @type: The block type we are looking for
+ *
+ * Returns: 0 if the block type matches the expected type
+ *          -ESTALE if it doesn't match
+ *          or -ve errno if something went wrong while checking
+ */
+
+int gfs2_check_blk_type(struct gfs2_sbd *sdp, u64 no_addr, unsigned int type)
+{
+       struct gfs2_rgrpd *rgd;
+       struct gfs2_holder ri_gh, rgd_gh;
+       struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
+       int ri_locked = 0;
+       int error;
+
+       if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
+               error = gfs2_rindex_hold(sdp, &ri_gh);
+               if (error)
+                       goto fail;
+               ri_locked = 1;
+       }
+
+       error = -EINVAL;
+       rgd = gfs2_blk2rgrpd(sdp, no_addr);
+       if (!rgd)
+               goto fail_rindex;
+
+       error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
+       if (error)
+               goto fail_rindex;
+
+       if (gfs2_get_block_type(rgd, no_addr) != type)
+               error = -ESTALE;
+
+       gfs2_glock_dq_uninit(&rgd_gh);
+fail_rindex:
+       if (ri_locked)
+               gfs2_glock_dq_uninit(&ri_gh);
+fail:
+       return error;
+}
+
+/**
  * gfs2_rlist_add - add a RG to a list of RGs
  * @sdp: the filesystem
  * @rlist: the list of resource groups