Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux...
[firefly-linux-kernel-4.4.55.git] / fs / ext4 / balloc.c
index 357e4e50374a3e7ce2a87bdafc73b7a9c0773baf..8a23483ca8d0c3668e171bbc53c7a629a2403876 100644 (file)
@@ -14,9 +14,9 @@
 #include <linux/time.h>
 #include <linux/capability.h>
 #include <linux/fs.h>
-#include <linux/jbd.h>
+#include <linux/jbd2.h>
 #include <linux/ext4_fs.h>
-#include <linux/ext4_jbd.h>
+#include <linux/ext4_jbd2.h>
 #include <linux/quotaops.h>
 #include <linux/buffer_head.h>
 
  * balloc.c contains the blocks allocation and deallocation routines
  */
 
+/*
+ * Calculate the block group number and offset, given a block number
+ */
+void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr,
+               unsigned long *blockgrpp, ext4_grpblk_t *offsetp)
+{
+        struct ext4_super_block *es = EXT4_SB(sb)->s_es;
+       ext4_grpblk_t offset;
+
+        blocknr = blocknr - le32_to_cpu(es->s_first_data_block);
+       offset = do_div(blocknr, EXT4_BLOCKS_PER_GROUP(sb));
+       if (offsetp)
+               *offsetp = offset;
+       if (blockgrpp)
+               *blockgrpp = blocknr;
+
+}
+
 /*
  * The free blocks are managed by bitmaps.  A file system contains several
  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
@@ -32,7 +50,7 @@
  * The file system contains group descriptors which are located after the
  * super block.  Each descriptor contains the number of the bitmap block and
  * the free blocks count in the block.  The descriptors are loaded in memory
- * when a file system is mounted (see ext4_read_super).
+ * when a file system is mounted (see ext4_fill_super).
  */
 
 
@@ -74,10 +92,12 @@ struct ext4_group_desc * ext4_get_group_desc(struct super_block * sb,
                return NULL;
        }
 
-       desc = (struct ext4_group_desc *) sbi->s_group_desc[group_desc]->b_data;
+       desc = (struct ext4_group_desc *)(
+               (__u8 *)sbi->s_group_desc[group_desc]->b_data +
+               offset * EXT4_DESC_SIZE(sb));
        if (bh)
                *bh = sbi->s_group_desc[group_desc];
-       return desc + offset;
+       return desc;
 }
 
 /**
@@ -99,12 +119,13 @@ read_block_bitmap(struct super_block *sb, unsigned int block_group)
        desc = ext4_get_group_desc (sb, block_group, NULL);
        if (!desc)
                goto error_out;
-       bh = sb_bread(sb, le32_to_cpu(desc->bg_block_bitmap));
+       bh = sb_bread(sb, ext4_block_bitmap(sb, desc));
        if (!bh)
                ext4_error (sb, "read_block_bitmap",
                            "Cannot read block bitmap - "
-                           "block_group = %d, block_bitmap = %u",
-                           block_group, le32_to_cpu(desc->bg_block_bitmap));
+                           "block_group = %d, block_bitmap = %llu",
+                           block_group,
+                           ext4_block_bitmap(sb, desc));
 error_out:
        return bh;
 }
@@ -144,10 +165,10 @@ restart:
 
        printk("Block Allocation Reservation Windows Map (%s):\n", fn);
        while (n) {
-               rsv = list_entry(n, struct ext4_reserve_window_node, rsv_node);
+               rsv = rb_entry(n, struct ext4_reserve_window_node, rsv_node);
                if (verbose)
                        printk("reservation window 0x%p "
-                              "start:  %lu, end:  %lu\n",
+                              "start:  %llu, end:  %llu\n",
                               rsv, rsv->rsv_start, rsv->rsv_end);
                if (rsv->rsv_start && rsv->rsv_start >= rsv->rsv_end) {
                        printk("Bad reservation %p (start >= end)\n",
@@ -432,21 +453,18 @@ void ext4_free_blocks_sb(handle_t *handle, struct super_block *sb,
        es = sbi->s_es;
        if (block < le32_to_cpu(es->s_first_data_block) ||
            block + count < block ||
-           block + count > le32_to_cpu(es->s_blocks_count)) {
+           block + count > ext4_blocks_count(es)) {
                ext4_error (sb, "ext4_free_blocks",
                            "Freeing blocks not in datazone - "
-                           "block = "E3FSBLK", count = %lu", block, count);
+                           "block = %llu, count = %lu", block, count);
                goto error_return;
        }
 
-       ext4_debug ("freeing block(s) %lu-%lu\n", block, block + count - 1);
+       ext4_debug ("freeing block(s) %llu-%llu\n", block, block + count - 1);
 
 do_more:
        overflow = 0;
-       block_group = (block - le32_to_cpu(es->s_first_data_block)) /
-                     EXT4_BLOCKS_PER_GROUP(sb);
-       bit = (block - le32_to_cpu(es->s_first_data_block)) %
-                     EXT4_BLOCKS_PER_GROUP(sb);
+       ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
        /*
         * Check to see if we are freeing blocks across a group
         * boundary.
@@ -463,15 +481,14 @@ do_more:
        if (!desc)
                goto error_return;
 
-       if (in_range (le32_to_cpu(desc->bg_block_bitmap), block, count) ||
-           in_range (le32_to_cpu(desc->bg_inode_bitmap), block, count) ||
-           in_range (block, le32_to_cpu(desc->bg_inode_table),
-                     sbi->s_itb_per_group) ||
-           in_range (block + count - 1, le32_to_cpu(desc->bg_inode_table),
-                     sbi->s_itb_per_group))
+       if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
+           in_range(ext4_inode_bitmap(sb, desc), block, count) ||
+           in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
+           in_range(block + count - 1, ext4_inode_table(sb, desc),
+                    sbi->s_itb_per_group))
                ext4_error (sb, "ext4_free_blocks",
                            "Freeing blocks in system zones - "
-                           "Block = "E3FSBLK", count = %lu",
+                           "Block = %llu, count = %lu",
                            block, count);
 
        /*
@@ -526,12 +543,12 @@ do_more:
                 * transaction.
                 *
                 * Ideally we would want to allow that to happen, but to
-                * do so requires making journal_forget() capable of
+                * do so requires making jbd2_journal_forget() capable of
                 * revoking the queued write of a data block, which
                 * implies blocking on the journal lock.  *forget()
                 * cannot block due to truncate races.
                 *
-                * Eventually we can fix this by making journal_forget()
+                * Eventually we can fix this by making jbd2_journal_forget()
                 * return a status indicating whether or not it was able
                 * to revoke the buffer.  On successful revoke, it is
                 * safe not to set the allocation bit in the committed
@@ -555,8 +572,8 @@ do_more:
                                                bit + i, bitmap_bh->b_data)) {
                        jbd_unlock_bh_state(bitmap_bh);
                        ext4_error(sb, __FUNCTION__,
-                               "bit already cleared for block "E3FSBLK,
-                                block + i);
+                                  "bit already cleared for block %llu",
+                                  (ext4_fsblk_t)(block + i));
                        jbd_lock_bh_state(bitmap_bh);
                        BUFFER_TRACE(bitmap_bh, "bit already cleared");
                } else {
@@ -730,7 +747,7 @@ find_next_usable_block(ext4_grpblk_t start, struct buffer_head *bh,
                here = 0;
 
        p = ((char *)bh->b_data) + (here >> 3);
-       r = memscan(p, 0, (maxblocks - here + 7) >> 3);
+       r = memscan(p, 0, ((maxblocks + 7) >> 3) - (here >> 3));
        next = (r - ((char *)bh->b_data)) << 3;
 
        if (next < maxblocks && next >= start && ext4_test_allocatable(next, bh))
@@ -949,7 +966,7 @@ static int find_next_reservable_window(
 
                prev = rsv;
                next = rb_next(&rsv->rsv_node);
-               rsv = list_entry(next,struct ext4_reserve_window_node,rsv_node);
+               rsv = rb_entry(next,struct ext4_reserve_window_node,rsv_node);
 
                /*
                 * Reached the last reservation, we can just append to the
@@ -1148,7 +1165,7 @@ retry:
         * check if the first free block is within the
         * free space we just reserved
         */
-       if (start_block >= my_rsv->rsv_start && start_block < my_rsv->rsv_end)
+       if (start_block >= my_rsv->rsv_start && start_block <= my_rsv->rsv_end)
                return 0;               /* success */
        /*
         * if the first free bit we found is out of the reservable space
@@ -1193,7 +1210,7 @@ static void try_to_extend_reservation(struct ext4_reserve_window_node *my_rsv,
        if (!next)
                my_rsv->rsv_end += size;
        else {
-               next_rsv = list_entry(next, struct ext4_reserve_window_node, rsv_node);
+               next_rsv = rb_entry(next, struct ext4_reserve_window_node, rsv_node);
 
                if ((next_rsv->rsv_start - my_rsv->rsv_end - 1) >= size)
                        my_rsv->rsv_end += size;
@@ -1271,7 +1288,7 @@ ext4_try_to_allocate_with_rsv(struct super_block *sb, handle_t *handle,
        }
        /*
         * grp_goal is a group relative block number (if there is a goal)
-        * 0 < grp_goal < EXT4_BLOCKS_PER_GROUP(sb)
+        * 0 <= grp_goal < EXT4_BLOCKS_PER_GROUP(sb)
         * first block is a filesystem wide block number
         * first block is the block number of the first block in this group
         */
@@ -1307,10 +1324,14 @@ ext4_try_to_allocate_with_rsv(struct super_block *sb, handle_t *handle,
                        if (!goal_in_my_reservation(&my_rsv->rsv_window,
                                                        grp_goal, group, sb))
                                grp_goal = -1;
-               } else if (grp_goal > 0 &&
-                         (my_rsv->rsv_end-grp_goal+1) < *count)
-                       try_to_extend_reservation(my_rsv, sb,
-                                       *count-my_rsv->rsv_end + grp_goal - 1);
+               } else if (grp_goal >= 0) {
+                       int curr = my_rsv->rsv_end -
+                                       (grp_goal + group_first_block) + 1;
+
+                       if (curr < *count)
+                               try_to_extend_reservation(my_rsv, sb,
+                                                       *count - curr);
+               }
 
                if ((my_rsv->rsv_start > group_last_block) ||
                                (my_rsv->rsv_end < group_first_block)) {
@@ -1354,7 +1375,7 @@ static int ext4_has_free_blocks(struct ext4_sb_info *sbi)
        ext4_fsblk_t free_blocks, root_blocks;
 
        free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
-       root_blocks = le32_to_cpu(sbi->s_es->s_r_blocks_count);
+       root_blocks = ext4_r_blocks_count(sbi->s_es);
        if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) &&
                sbi->s_resuid != current->fsuid &&
                (sbi->s_resgid == 0 || !in_group_p (sbi->s_resgid))) {
@@ -1382,7 +1403,7 @@ int ext4_should_retry_alloc(struct super_block *sb, int *retries)
 
        jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
 
-       return journal_force_commit_nested(EXT4_SB(sb)->s_journal);
+       return jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal);
 }
 
 /**
@@ -1404,7 +1425,7 @@ ext4_fsblk_t ext4_new_blocks(handle_t *handle, struct inode *inode,
 {
        struct buffer_head *bitmap_bh = NULL;
        struct buffer_head *gdp_bh;
-       int group_no;
+       unsigned long group_no;
        int goal_group;
        ext4_grpblk_t grp_target_blk;   /* blockgroup relative goal block */
        ext4_grpblk_t grp_alloc_blk;    /* blockgroup-relative allocated block*/
@@ -1465,10 +1486,9 @@ ext4_fsblk_t ext4_new_blocks(handle_t *handle, struct inode *inode,
         * First, test whether the goal block is free.
         */
        if (goal < le32_to_cpu(es->s_first_data_block) ||
-           goal >= le32_to_cpu(es->s_blocks_count))
+           goal >= ext4_blocks_count(es))
                goal = le32_to_cpu(es->s_first_data_block);
-       group_no = (goal - le32_to_cpu(es->s_first_data_block)) /
-                       EXT4_BLOCKS_PER_GROUP(sb);
+       ext4_get_group_no_and_offset(sb, goal, &group_no, &grp_target_blk);
        goal_group = group_no;
 retry_alloc:
        gdp = ext4_get_group_desc(sb, group_no, &gdp_bh);
@@ -1485,8 +1505,6 @@ retry_alloc:
                my_rsv = NULL;
 
        if (free_blocks > 0) {
-               grp_target_blk = ((goal - le32_to_cpu(es->s_first_data_block)) %
-                               EXT4_BLOCKS_PER_GROUP(sb));
                bitmap_bh = read_block_bitmap(sb, group_no);
                if (!bitmap_bh)
                        goto io_error;
@@ -1511,10 +1529,8 @@ retry_alloc:
                if (group_no >= ngroups)
                        group_no = 0;
                gdp = ext4_get_group_desc(sb, group_no, &gdp_bh);
-               if (!gdp) {
-                       *errp = -EIO;
-                       goto out;
-               }
+               if (!gdp)
+                       goto io_error;
                free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
                /*
                 * skip this group if the number of
@@ -1548,6 +1564,7 @@ retry_alloc:
         */
        if (my_rsv) {
                my_rsv = NULL;
+               windowsz = 0;
                group_no = goal_group;
                goto retry_alloc;
        }
@@ -1567,15 +1584,15 @@ allocated:
 
        ret_block = grp_alloc_blk + ext4_group_first_block_no(sb, group_no);
 
-       if (in_range(le32_to_cpu(gdp->bg_block_bitmap), ret_block, num) ||
-           in_range(le32_to_cpu(gdp->bg_inode_bitmap), ret_block, num) ||
-           in_range(ret_block, le32_to_cpu(gdp->bg_inode_table),
-                     EXT4_SB(sb)->s_itb_per_group) ||
-           in_range(ret_block + num - 1, le32_to_cpu(gdp->bg_inode_table),
-                     EXT4_SB(sb)->s_itb_per_group))
+       if (in_range(ext4_block_bitmap(sb, gdp), ret_block, num) ||
+           in_range(ext4_block_bitmap(sb, gdp), ret_block, num) ||
+           in_range(ret_block, ext4_inode_table(sb, gdp),
+                    EXT4_SB(sb)->s_itb_per_group) ||
+           in_range(ret_block + num - 1, ext4_inode_table(sb, gdp),
+                    EXT4_SB(sb)->s_itb_per_group))
                ext4_error(sb, "ext4_new_block",
                            "Allocating block in system zone - "
-                           "blocks from "E3FSBLK", length %lu",
+                           "blocks from %llu, length %lu",
                             ret_block, num);
 
        performed_allocation = 1;
@@ -1610,11 +1627,11 @@ allocated:
        jbd_unlock_bh_state(bitmap_bh);
 #endif
 
-       if (ret_block + num - 1 >= le32_to_cpu(es->s_blocks_count)) {
+       if (ret_block + num - 1 >= ext4_blocks_count(es)) {
                ext4_error(sb, "ext4_new_block",
-                           "block("E3FSBLK") >= blocks count(%d) - "
-                           "block_group = %d, es == %p ", ret_block,
-                       le32_to_cpu(es->s_blocks_count), group_no, es);
+                           "block(%llu) >= blocks count(%llu) - "
+                           "block_group = %lu, es == %p ", ret_block,
+                       ext4_blocks_count(es), group_no, es);
                goto out;
        }
 
@@ -1711,9 +1728,9 @@ ext4_fsblk_t ext4_count_free_blocks(struct super_block *sb)
                bitmap_count += x;
        }
        brelse(bitmap_bh);
-       printk("ext4_count_free_blocks: stored = "E3FSBLK
-               ", computed = "E3FSBLK", "E3FSBLK"\n",
-              le32_to_cpu(es->s_free_blocks_count),
+       printk("ext4_count_free_blocks: stored = %llu"
+               ", computed = %llu, %llu\n",
+              EXT4_FREE_BLOCKS_COUNT(es),
                desc_count, bitmap_count);
        return bitmap_count;
 #else
@@ -1733,9 +1750,10 @@ ext4_fsblk_t ext4_count_free_blocks(struct super_block *sb)
 static inline int
 block_in_use(ext4_fsblk_t block, struct super_block *sb, unsigned char *map)
 {
-       return ext4_test_bit ((block -
-               le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) %
-                        EXT4_BLOCKS_PER_GROUP(sb), map);
+       ext4_grpblk_t offset;
+
+       ext4_get_group_no_and_offset(sb, block, NULL, &offset);
+       return ext4_test_bit (offset, map);
 }
 
 static inline int test_root(int a, int b)