Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2 | * linux/fs/ext4/balloc.c |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 5 | * Remy Card (card@masi.ibp.fr) |
| 6 | * Laboratoire MASI - Institut Blaise Pascal |
| 7 | * Universite Pierre et Marie Curie (Paris VI) |
| 8 | * |
| 9 | * Enhanced block allocation by Stephen Tweedie (sct@redhat.com), 1993 |
| 10 | * Big-endian to little-endian byte-swapping/bitmaps by |
| 11 | * David S. Miller (davem@caip.rutgers.edu), 1995 |
| 12 | */ |
| 13 | |
| 14 | #include <linux/time.h> |
| 15 | #include <linux/capability.h> |
| 16 | #include <linux/fs.h> |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 17 | #include <linux/jbd2.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 18 | #include <linux/quotaops.h> |
| 19 | #include <linux/buffer_head.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 20 | #include "ext4.h" |
| 21 | #include "ext4_jbd2.h" |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 22 | #include "group.h" |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 23 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 24 | /* |
| 25 | * balloc.c contains the blocks allocation and deallocation routines |
| 26 | */ |
| 27 | |
| 28 | /* |
Andrew Morton | 72b64b5 | 2006-10-11 01:21:18 -0700 | [diff] [blame] | 29 | * Calculate the block group number and offset, given a block number |
| 30 | */ |
| 31 | void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr, |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 32 | ext4_group_t *blockgrpp, ext4_grpblk_t *offsetp) |
Andrew Morton | 72b64b5 | 2006-10-11 01:21:18 -0700 | [diff] [blame] | 33 | { |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 34 | struct ext4_super_block *es = EXT4_SB(sb)->s_es; |
Andrew Morton | 72b64b5 | 2006-10-11 01:21:18 -0700 | [diff] [blame] | 35 | ext4_grpblk_t offset; |
| 36 | |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 37 | blocknr = blocknr - le32_to_cpu(es->s_first_data_block); |
Andrew Morton | f4e5bc2 | 2006-10-11 01:21:19 -0700 | [diff] [blame] | 38 | offset = do_div(blocknr, EXT4_BLOCKS_PER_GROUP(sb)); |
Andrew Morton | 72b64b5 | 2006-10-11 01:21:18 -0700 | [diff] [blame] | 39 | if (offsetp) |
| 40 | *offsetp = offset; |
| 41 | if (blockgrpp) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 42 | *blockgrpp = blocknr; |
Andrew Morton | 72b64b5 | 2006-10-11 01:21:18 -0700 | [diff] [blame] | 43 | |
| 44 | } |
| 45 | |
Jose R. Santos | 0bf7e83 | 2008-06-03 14:07:29 -0400 | [diff] [blame] | 46 | static int ext4_block_in_group(struct super_block *sb, ext4_fsblk_t block, |
| 47 | ext4_group_t block_group) |
| 48 | { |
| 49 | ext4_group_t actual_group; |
Aneesh Kumar K.V | 7477827 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 50 | ext4_get_group_no_and_offset(sb, block, &actual_group, NULL); |
Jose R. Santos | 0bf7e83 | 2008-06-03 14:07:29 -0400 | [diff] [blame] | 51 | if (actual_group == block_group) |
| 52 | return 1; |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int ext4_group_used_meta_blocks(struct super_block *sb, |
| 57 | ext4_group_t block_group) |
| 58 | { |
| 59 | ext4_fsblk_t tmp; |
| 60 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 61 | /* block bitmap, inode bitmap, and inode table blocks */ |
| 62 | int used_blocks = sbi->s_itb_per_group + 2; |
| 63 | |
| 64 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) { |
| 65 | struct ext4_group_desc *gdp; |
| 66 | struct buffer_head *bh; |
| 67 | |
| 68 | gdp = ext4_get_group_desc(sb, block_group, &bh); |
| 69 | if (!ext4_block_in_group(sb, ext4_block_bitmap(sb, gdp), |
| 70 | block_group)) |
| 71 | used_blocks--; |
| 72 | |
| 73 | if (!ext4_block_in_group(sb, ext4_inode_bitmap(sb, gdp), |
| 74 | block_group)) |
| 75 | used_blocks--; |
| 76 | |
| 77 | tmp = ext4_inode_table(sb, gdp); |
| 78 | for (; tmp < ext4_inode_table(sb, gdp) + |
| 79 | sbi->s_itb_per_group; tmp++) { |
| 80 | if (!ext4_block_in_group(sb, tmp, block_group)) |
| 81 | used_blocks -= 1; |
| 82 | } |
| 83 | } |
| 84 | return used_blocks; |
| 85 | } |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame^] | 86 | |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 87 | /* Initializes an uninitialized block bitmap if given, and returns the |
| 88 | * number of blocks free in the group. */ |
| 89 | unsigned ext4_init_block_bitmap(struct super_block *sb, struct buffer_head *bh, |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 90 | ext4_group_t block_group, struct ext4_group_desc *gdp) |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 91 | { |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 92 | int bit, bit_max; |
| 93 | unsigned free_blocks, group_blocks; |
| 94 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 95 | |
| 96 | if (bh) { |
| 97 | J_ASSERT_BH(bh, buffer_locked(bh)); |
| 98 | |
| 99 | /* If checksum is bad mark all blocks used to prevent allocation |
| 100 | * essentially implementing a per-group read-only flag. */ |
| 101 | if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) { |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 102 | ext4_error(sb, __func__, |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 103 | "Checksum bad for group %lu\n", block_group); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 104 | gdp->bg_free_blocks_count = 0; |
| 105 | gdp->bg_free_inodes_count = 0; |
| 106 | gdp->bg_itable_unused = 0; |
| 107 | memset(bh->b_data, 0xff, sb->s_blocksize); |
| 108 | return 0; |
| 109 | } |
| 110 | memset(bh->b_data, 0, sb->s_blocksize); |
| 111 | } |
| 112 | |
| 113 | /* Check for superblock and gdt backups in this group */ |
| 114 | bit_max = ext4_bg_has_super(sb, block_group); |
| 115 | |
| 116 | if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) || |
| 117 | block_group < le32_to_cpu(sbi->s_es->s_first_meta_bg) * |
| 118 | sbi->s_desc_per_block) { |
| 119 | if (bit_max) { |
| 120 | bit_max += ext4_bg_num_gdb(sb, block_group); |
| 121 | bit_max += |
| 122 | le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks); |
| 123 | } |
| 124 | } else { /* For META_BG_BLOCK_GROUPS */ |
Akinobu Mita | 6afd670 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 125 | bit_max += ext4_bg_num_gdb(sb, block_group); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | if (block_group == sbi->s_groups_count - 1) { |
| 129 | /* |
| 130 | * Even though mke2fs always initialize first and last group |
| 131 | * if some other tool enabled the EXT4_BG_BLOCK_UNINIT we need |
| 132 | * to make sure we calculate the right free blocks |
| 133 | */ |
| 134 | group_blocks = ext4_blocks_count(sbi->s_es) - |
| 135 | le32_to_cpu(sbi->s_es->s_first_data_block) - |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 136 | (EXT4_BLOCKS_PER_GROUP(sb) * (sbi->s_groups_count - 1)); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 137 | } else { |
| 138 | group_blocks = EXT4_BLOCKS_PER_GROUP(sb); |
| 139 | } |
| 140 | |
| 141 | free_blocks = group_blocks - bit_max; |
| 142 | |
| 143 | if (bh) { |
Jose R. Santos | 0bf7e83 | 2008-06-03 14:07:29 -0400 | [diff] [blame] | 144 | ext4_fsblk_t start, tmp; |
| 145 | int flex_bg = 0; |
Akinobu Mita | d00a6d7 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 146 | |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 147 | for (bit = 0; bit < bit_max; bit++) |
| 148 | ext4_set_bit(bit, bh->b_data); |
| 149 | |
Akinobu Mita | d00a6d7 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 150 | start = ext4_group_first_block_no(sb, block_group); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 151 | |
Jose R. Santos | 0bf7e83 | 2008-06-03 14:07:29 -0400 | [diff] [blame] | 152 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, |
| 153 | EXT4_FEATURE_INCOMPAT_FLEX_BG)) |
| 154 | flex_bg = 1; |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 155 | |
Jose R. Santos | 0bf7e83 | 2008-06-03 14:07:29 -0400 | [diff] [blame] | 156 | /* Set bits for block and inode bitmaps, and inode table */ |
| 157 | tmp = ext4_block_bitmap(sb, gdp); |
| 158 | if (!flex_bg || ext4_block_in_group(sb, tmp, block_group)) |
| 159 | ext4_set_bit(tmp - start, bh->b_data); |
| 160 | |
| 161 | tmp = ext4_inode_bitmap(sb, gdp); |
| 162 | if (!flex_bg || ext4_block_in_group(sb, tmp, block_group)) |
| 163 | ext4_set_bit(tmp - start, bh->b_data); |
| 164 | |
| 165 | tmp = ext4_inode_table(sb, gdp); |
| 166 | for (; tmp < ext4_inode_table(sb, gdp) + |
| 167 | sbi->s_itb_per_group; tmp++) { |
| 168 | if (!flex_bg || |
| 169 | ext4_block_in_group(sb, tmp, block_group)) |
| 170 | ext4_set_bit(tmp - start, bh->b_data); |
| 171 | } |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 172 | /* |
| 173 | * Also if the number of blocks within the group is |
| 174 | * less than the blocksize * 8 ( which is the size |
| 175 | * of bitmap ), set rest of the block bitmap to 1 |
| 176 | */ |
| 177 | mark_bitmap_end(group_blocks, sb->s_blocksize * 8, bh->b_data); |
| 178 | } |
Jose R. Santos | 0bf7e83 | 2008-06-03 14:07:29 -0400 | [diff] [blame] | 179 | return free_blocks - ext4_group_used_meta_blocks(sb, block_group); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | |
Andrew Morton | 72b64b5 | 2006-10-11 01:21:18 -0700 | [diff] [blame] | 183 | /* |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 184 | * The free blocks are managed by bitmaps. A file system contains several |
| 185 | * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap |
| 186 | * block for inodes, N blocks for the inode table and data blocks. |
| 187 | * |
| 188 | * The file system contains group descriptors which are located after the |
| 189 | * super block. Each descriptor contains the number of the bitmap block and |
| 190 | * the free blocks count in the block. The descriptors are loaded in memory |
Aneesh Kumar K.V | e627432 | 2007-02-20 13:57:58 -0800 | [diff] [blame] | 191 | * when a file system is mounted (see ext4_fill_super). |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 192 | */ |
| 193 | |
| 194 | |
| 195 | #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1) |
| 196 | |
| 197 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 198 | * ext4_get_group_desc() -- load group descriptor from disk |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 199 | * @sb: super block |
| 200 | * @block_group: given block group |
| 201 | * @bh: pointer to the buffer head to store the block |
| 202 | * group descriptor |
| 203 | */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 204 | struct ext4_group_desc * ext4_get_group_desc(struct super_block *sb, |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 205 | ext4_group_t block_group, |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 206 | struct buffer_head **bh) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 207 | { |
| 208 | unsigned long group_desc; |
| 209 | unsigned long offset; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 210 | struct ext4_group_desc *desc; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 211 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 212 | |
| 213 | if (block_group >= sbi->s_groups_count) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 214 | ext4_error(sb, "ext4_get_group_desc", |
| 215 | "block_group >= groups_count - " |
| 216 | "block_group = %lu, groups_count = %lu", |
| 217 | block_group, sbi->s_groups_count); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 218 | |
| 219 | return NULL; |
| 220 | } |
| 221 | smp_rmb(); |
| 222 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 223 | group_desc = block_group >> EXT4_DESC_PER_BLOCK_BITS(sb); |
| 224 | offset = block_group & (EXT4_DESC_PER_BLOCK(sb) - 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 225 | if (!sbi->s_group_desc[group_desc]) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 226 | ext4_error(sb, "ext4_get_group_desc", |
| 227 | "Group descriptor not loaded - " |
| 228 | "block_group = %lu, group_desc = %lu, desc = %lu", |
| 229 | block_group, group_desc, offset); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 230 | return NULL; |
| 231 | } |
| 232 | |
Alexandre Ratchov | 0d1ee42 | 2006-10-11 01:21:14 -0700 | [diff] [blame] | 233 | desc = (struct ext4_group_desc *)( |
| 234 | (__u8 *)sbi->s_group_desc[group_desc]->b_data + |
| 235 | offset * EXT4_DESC_SIZE(sb)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 236 | if (bh) |
| 237 | *bh = sbi->s_group_desc[group_desc]; |
Alexandre Ratchov | 0d1ee42 | 2006-10-11 01:21:14 -0700 | [diff] [blame] | 238 | return desc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Aneesh Kumar K.V | abcb294 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 241 | static int ext4_valid_block_bitmap(struct super_block *sb, |
| 242 | struct ext4_group_desc *desc, |
| 243 | unsigned int block_group, |
| 244 | struct buffer_head *bh) |
| 245 | { |
| 246 | ext4_grpblk_t offset; |
| 247 | ext4_grpblk_t next_zero_bit; |
| 248 | ext4_fsblk_t bitmap_blk; |
| 249 | ext4_fsblk_t group_first_block; |
| 250 | |
| 251 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) { |
| 252 | /* with FLEX_BG, the inode/block bitmaps and itable |
| 253 | * blocks may not be in the group at all |
| 254 | * so the bitmap validation will be skipped for those groups |
| 255 | * or it has to also read the block group where the bitmaps |
| 256 | * are located to verify they are set. |
| 257 | */ |
| 258 | return 1; |
| 259 | } |
| 260 | group_first_block = ext4_group_first_block_no(sb, block_group); |
| 261 | |
| 262 | /* check whether block bitmap block number is set */ |
| 263 | bitmap_blk = ext4_block_bitmap(sb, desc); |
| 264 | offset = bitmap_blk - group_first_block; |
| 265 | if (!ext4_test_bit(offset, bh->b_data)) |
| 266 | /* bad block bitmap */ |
| 267 | goto err_out; |
| 268 | |
| 269 | /* check whether the inode bitmap block number is set */ |
| 270 | bitmap_blk = ext4_inode_bitmap(sb, desc); |
| 271 | offset = bitmap_blk - group_first_block; |
| 272 | if (!ext4_test_bit(offset, bh->b_data)) |
| 273 | /* bad block bitmap */ |
| 274 | goto err_out; |
| 275 | |
| 276 | /* check whether the inode table block number is set */ |
| 277 | bitmap_blk = ext4_inode_table(sb, desc); |
| 278 | offset = bitmap_blk - group_first_block; |
| 279 | next_zero_bit = ext4_find_next_zero_bit(bh->b_data, |
| 280 | offset + EXT4_SB(sb)->s_itb_per_group, |
| 281 | offset); |
| 282 | if (next_zero_bit >= offset + EXT4_SB(sb)->s_itb_per_group) |
| 283 | /* good bitmap for inode tables */ |
| 284 | return 1; |
| 285 | |
| 286 | err_out: |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 287 | ext4_error(sb, __func__, |
Aneesh Kumar K.V | abcb294 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 288 | "Invalid block bitmap - " |
| 289 | "block_group = %d, block = %llu", |
| 290 | block_group, bitmap_blk); |
| 291 | return 0; |
| 292 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 293 | /** |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 294 | * ext4_read_block_bitmap() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 295 | * @sb: super block |
| 296 | * @block_group: given block group |
| 297 | * |
Aneesh Kumar K.V | abcb294 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 298 | * Read the bitmap for a given block_group,and validate the |
| 299 | * bits for block/inode/inode tables are set in the bitmaps |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 300 | * |
| 301 | * Return buffer_head on success or NULL in case of failure. |
| 302 | */ |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 303 | struct buffer_head * |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 304 | ext4_read_block_bitmap(struct super_block *sb, ext4_group_t block_group) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 305 | { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 306 | struct ext4_group_desc *desc; |
| 307 | struct buffer_head *bh = NULL; |
Aneesh Kumar K.V | 7c9e69f | 2007-10-16 23:27:02 -0700 | [diff] [blame] | 308 | ext4_fsblk_t bitmap_blk; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 309 | |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 310 | desc = ext4_get_group_desc(sb, block_group, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 311 | if (!desc) |
Aneesh Kumar K.V | 7c9e69f | 2007-10-16 23:27:02 -0700 | [diff] [blame] | 312 | return NULL; |
| 313 | bitmap_blk = ext4_block_bitmap(sb, desc); |
Aneesh Kumar K.V | abcb294 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 314 | bh = sb_getblk(sb, bitmap_blk); |
| 315 | if (unlikely(!bh)) { |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 316 | ext4_error(sb, __func__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 317 | "Cannot read block bitmap - " |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 318 | "block_group = %lu, block_bitmap = %llu", |
| 319 | block_group, bitmap_blk); |
Aneesh Kumar K.V | abcb294 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 320 | return NULL; |
| 321 | } |
| 322 | if (bh_uptodate_or_lock(bh)) |
| 323 | return bh; |
| 324 | |
Eric Sandeen | b5f10ee | 2008-08-02 21:21:08 -0400 | [diff] [blame] | 325 | spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
Aneesh Kumar K.V | abcb294 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 326 | if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
| 327 | ext4_init_block_bitmap(sb, bh, block_group, desc); |
| 328 | set_buffer_uptodate(bh); |
| 329 | unlock_buffer(bh); |
Eric Sandeen | b5f10ee | 2008-08-02 21:21:08 -0400 | [diff] [blame] | 330 | spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
Aneesh Kumar K.V | abcb294 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 331 | return bh; |
| 332 | } |
Eric Sandeen | b5f10ee | 2008-08-02 21:21:08 -0400 | [diff] [blame] | 333 | spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
Aneesh Kumar K.V | abcb294 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 334 | if (bh_submit_read(bh) < 0) { |
| 335 | put_bh(bh); |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 336 | ext4_error(sb, __func__, |
Aneesh Kumar K.V | abcb294 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 337 | "Cannot read block bitmap - " |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 338 | "block_group = %lu, block_bitmap = %llu", |
| 339 | block_group, bitmap_blk); |
Aneesh Kumar K.V | abcb294 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 340 | return NULL; |
| 341 | } |
Aneesh Kumar K.V | 519deca | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 342 | ext4_valid_block_bitmap(sb, desc, block_group, bh); |
| 343 | /* |
| 344 | * file system mounted not to panic on error, |
| 345 | * continue with corrupt bitmap |
| 346 | */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 347 | return bh; |
| 348 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 349 | |
| 350 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 351 | * ext4_free_blocks_sb() -- Free given blocks and update quota |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 352 | * @handle: handle to this transaction |
| 353 | * @sb: super block |
| 354 | * @block: start physcial block to free |
| 355 | * @count: number of blocks to free |
| 356 | * @pdquot_freed_blocks: pointer to quota |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame^] | 357 | * |
| 358 | * XXX This function is only used by the on-line resizing code, which |
| 359 | * should probably be fixed up to call the mballoc variant. There |
| 360 | * this needs to be cleaned up later; in fact, I'm not convinced this |
| 361 | * is 100% correct in the face of the mballoc code. The online resizing |
| 362 | * code needs to be fixed up to more tightly (and correctly) interlock |
| 363 | * with the mballoc code. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 364 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 365 | void ext4_free_blocks_sb(handle_t *handle, struct super_block *sb, |
| 366 | ext4_fsblk_t block, unsigned long count, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 367 | unsigned long *pdquot_freed_blocks) |
| 368 | { |
| 369 | struct buffer_head *bitmap_bh = NULL; |
| 370 | struct buffer_head *gd_bh; |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 371 | ext4_group_t block_group; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 372 | ext4_grpblk_t bit; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 373 | unsigned long i; |
| 374 | unsigned long overflow; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 375 | struct ext4_group_desc *desc; |
| 376 | struct ext4_super_block *es; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 377 | struct ext4_sb_info *sbi; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 378 | int err = 0, ret; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 379 | ext4_grpblk_t group_freed; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 380 | |
| 381 | *pdquot_freed_blocks = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 382 | sbi = EXT4_SB(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 383 | es = sbi->s_es; |
| 384 | if (block < le32_to_cpu(es->s_first_data_block) || |
| 385 | block + count < block || |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 386 | block + count > ext4_blocks_count(es)) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 387 | ext4_error(sb, "ext4_free_blocks", |
| 388 | "Freeing blocks not in datazone - " |
| 389 | "block = %llu, count = %lu", block, count); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 390 | goto error_return; |
| 391 | } |
| 392 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 393 | ext4_debug("freeing block(s) %llu-%llu\n", block, block + count - 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 394 | |
| 395 | do_more: |
| 396 | overflow = 0; |
Mingming Cao | 3a5b2ec | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 397 | ext4_get_group_no_and_offset(sb, block, &block_group, &bit); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 398 | /* |
| 399 | * Check to see if we are freeing blocks across a group |
| 400 | * boundary. |
| 401 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 402 | if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) { |
| 403 | overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 404 | count -= overflow; |
| 405 | } |
| 406 | brelse(bitmap_bh); |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 407 | bitmap_bh = ext4_read_block_bitmap(sb, block_group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 408 | if (!bitmap_bh) |
| 409 | goto error_return; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 410 | desc = ext4_get_group_desc(sb, block_group, &gd_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 411 | if (!desc) |
| 412 | goto error_return; |
| 413 | |
Alexandre Ratchov | 8fadc14 | 2006-10-11 01:21:15 -0700 | [diff] [blame] | 414 | if (in_range(ext4_block_bitmap(sb, desc), block, count) || |
| 415 | in_range(ext4_inode_bitmap(sb, desc), block, count) || |
| 416 | in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) || |
| 417 | in_range(block + count - 1, ext4_inode_table(sb, desc), |
Aneesh Kumar K.V | cb47dce | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 418 | sbi->s_itb_per_group)) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 419 | ext4_error(sb, "ext4_free_blocks", |
| 420 | "Freeing blocks in system zones - " |
| 421 | "Block = %llu, count = %lu", |
| 422 | block, count); |
Aneesh Kumar K.V | cb47dce | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 423 | goto error_return; |
| 424 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 425 | |
| 426 | /* |
| 427 | * We are about to start releasing blocks in the bitmap, |
| 428 | * so we need undo access. |
| 429 | */ |
| 430 | /* @@@ check errors */ |
| 431 | BUFFER_TRACE(bitmap_bh, "getting undo access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 432 | err = ext4_journal_get_undo_access(handle, bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 433 | if (err) |
| 434 | goto error_return; |
| 435 | |
| 436 | /* |
| 437 | * We are about to modify some metadata. Call the journal APIs |
| 438 | * to unshare ->b_data if a currently-committing transaction is |
| 439 | * using it |
| 440 | */ |
| 441 | BUFFER_TRACE(gd_bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 442 | err = ext4_journal_get_write_access(handle, gd_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 443 | if (err) |
| 444 | goto error_return; |
| 445 | |
| 446 | jbd_lock_bh_state(bitmap_bh); |
| 447 | |
| 448 | for (i = 0, group_freed = 0; i < count; i++) { |
| 449 | /* |
| 450 | * An HJ special. This is expensive... |
| 451 | */ |
Jose R. Santos | e23291b | 2007-07-18 08:57:06 -0400 | [diff] [blame] | 452 | #ifdef CONFIG_JBD2_DEBUG |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 453 | jbd_unlock_bh_state(bitmap_bh); |
| 454 | { |
| 455 | struct buffer_head *debug_bh; |
| 456 | debug_bh = sb_find_get_block(sb, block + i); |
| 457 | if (debug_bh) { |
| 458 | BUFFER_TRACE(debug_bh, "Deleted!"); |
| 459 | if (!bh2jh(bitmap_bh)->b_committed_data) |
| 460 | BUFFER_TRACE(debug_bh, |
| 461 | "No commited data in bitmap"); |
| 462 | BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap"); |
| 463 | __brelse(debug_bh); |
| 464 | } |
| 465 | } |
| 466 | jbd_lock_bh_state(bitmap_bh); |
| 467 | #endif |
| 468 | if (need_resched()) { |
| 469 | jbd_unlock_bh_state(bitmap_bh); |
| 470 | cond_resched(); |
| 471 | jbd_lock_bh_state(bitmap_bh); |
| 472 | } |
| 473 | /* @@@ This prevents newly-allocated data from being |
| 474 | * freed and then reallocated within the same |
| 475 | * transaction. |
| 476 | * |
| 477 | * Ideally we would want to allow that to happen, but to |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 478 | * do so requires making jbd2_journal_forget() capable of |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 479 | * revoking the queued write of a data block, which |
| 480 | * implies blocking on the journal lock. *forget() |
| 481 | * cannot block due to truncate races. |
| 482 | * |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 483 | * Eventually we can fix this by making jbd2_journal_forget() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 484 | * return a status indicating whether or not it was able |
| 485 | * to revoke the buffer. On successful revoke, it is |
| 486 | * safe not to set the allocation bit in the committed |
| 487 | * bitmap, because we know that there is no outstanding |
| 488 | * activity on the buffer any more and so it is safe to |
| 489 | * reallocate it. |
| 490 | */ |
| 491 | BUFFER_TRACE(bitmap_bh, "set in b_committed_data"); |
| 492 | J_ASSERT_BH(bitmap_bh, |
| 493 | bh2jh(bitmap_bh)->b_committed_data != NULL); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 494 | ext4_set_bit_atomic(sb_bgl_lock(sbi, block_group), bit + i, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 495 | bh2jh(bitmap_bh)->b_committed_data); |
| 496 | |
| 497 | /* |
| 498 | * We clear the bit in the bitmap after setting the committed |
| 499 | * data bit, because this is the reverse order to that which |
| 500 | * the allocator uses. |
| 501 | */ |
| 502 | BUFFER_TRACE(bitmap_bh, "clear bit"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 503 | if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 504 | bit + i, bitmap_bh->b_data)) { |
| 505 | jbd_unlock_bh_state(bitmap_bh); |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 506 | ext4_error(sb, __func__, |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 507 | "bit already cleared for block %llu", |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 508 | (ext4_fsblk_t)(block + i)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 509 | jbd_lock_bh_state(bitmap_bh); |
| 510 | BUFFER_TRACE(bitmap_bh, "bit already cleared"); |
| 511 | } else { |
| 512 | group_freed++; |
| 513 | } |
| 514 | } |
| 515 | jbd_unlock_bh_state(bitmap_bh); |
| 516 | |
| 517 | spin_lock(sb_bgl_lock(sbi, block_group)); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 518 | le16_add_cpu(&desc->bg_free_blocks_count, group_freed); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 519 | desc->bg_checksum = ext4_group_desc_csum(sbi, block_group, desc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 520 | spin_unlock(sb_bgl_lock(sbi, block_group)); |
Peter Zijlstra | aa0dff2 | 2007-10-16 23:25:42 -0700 | [diff] [blame] | 521 | percpu_counter_add(&sbi->s_freeblocks_counter, count); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 522 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 523 | if (sbi->s_log_groups_per_flex) { |
| 524 | ext4_group_t flex_group = ext4_flex_group(sbi, block_group); |
| 525 | spin_lock(sb_bgl_lock(sbi, flex_group)); |
| 526 | sbi->s_flex_groups[flex_group].free_blocks += count; |
| 527 | spin_unlock(sb_bgl_lock(sbi, flex_group)); |
| 528 | } |
| 529 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 530 | /* We dirtied the bitmap block */ |
| 531 | BUFFER_TRACE(bitmap_bh, "dirtied bitmap block"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 532 | err = ext4_journal_dirty_metadata(handle, bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 533 | |
| 534 | /* And the group descriptor block */ |
| 535 | BUFFER_TRACE(gd_bh, "dirtied group descriptor block"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 536 | ret = ext4_journal_dirty_metadata(handle, gd_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 537 | if (!err) err = ret; |
| 538 | *pdquot_freed_blocks += group_freed; |
| 539 | |
| 540 | if (overflow && !err) { |
| 541 | block += count; |
| 542 | count = overflow; |
| 543 | goto do_more; |
| 544 | } |
| 545 | sb->s_dirt = 1; |
| 546 | error_return: |
| 547 | brelse(bitmap_bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 548 | ext4_std_error(sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 549 | return; |
| 550 | } |
| 551 | |
| 552 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 553 | * ext4_free_blocks() -- Free given blocks and update quota |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 554 | * @handle: handle for this transaction |
| 555 | * @inode: inode |
| 556 | * @block: start physical block to free |
| 557 | * @count: number of blocks to count |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 558 | * @metadata: Are these metadata blocks |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 559 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 560 | void ext4_free_blocks(handle_t *handle, struct inode *inode, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 561 | ext4_fsblk_t block, unsigned long count, |
| 562 | int metadata) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 563 | { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 564 | struct super_block *sb; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 565 | unsigned long dquot_freed_blocks; |
| 566 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 567 | /* this isn't the right place to decide whether block is metadata |
| 568 | * inode.c/extents.c knows better, but for safety ... */ |
| 569 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) || |
| 570 | ext4_should_journal_data(inode)) |
| 571 | metadata = 1; |
| 572 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 573 | sb = inode->i_sb; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 574 | |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame^] | 575 | ext4_mb_free_blocks(handle, inode, block, count, |
| 576 | metadata, &dquot_freed_blocks); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 577 | if (dquot_freed_blocks) |
| 578 | DQUOT_FREE_BLOCK(inode, dquot_freed_blocks); |
| 579 | return; |
| 580 | } |
| 581 | |
Aneesh Kumar K.V | a30d542 | 2008-10-09 10:56:23 -0400 | [diff] [blame] | 582 | int ext4_claim_free_blocks(struct ext4_sb_info *sbi, |
Aneesh Kumar K.V | 5c79161 | 2008-10-08 23:12:24 -0400 | [diff] [blame] | 583 | s64 nblocks) |
Aneesh Kumar K.V | a30d542 | 2008-10-09 10:56:23 -0400 | [diff] [blame] | 584 | { |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 585 | s64 free_blocks, dirty_blocks; |
Aneesh Kumar K.V | 5c79161 | 2008-10-08 23:12:24 -0400 | [diff] [blame] | 586 | s64 root_blocks = 0; |
Aneesh Kumar K.V | a30d542 | 2008-10-09 10:56:23 -0400 | [diff] [blame] | 587 | struct percpu_counter *fbc = &sbi->s_freeblocks_counter; |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 588 | struct percpu_counter *dbc = &sbi->s_dirtyblocks_counter; |
Aneesh Kumar K.V | a30d542 | 2008-10-09 10:56:23 -0400 | [diff] [blame] | 589 | |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 590 | free_blocks = percpu_counter_read_positive(fbc); |
| 591 | dirty_blocks = percpu_counter_read_positive(dbc); |
Aneesh Kumar K.V | a30d542 | 2008-10-09 10:56:23 -0400 | [diff] [blame] | 592 | |
| 593 | if (!capable(CAP_SYS_RESOURCE) && |
| 594 | sbi->s_resuid != current->fsuid && |
| 595 | (sbi->s_resgid == 0 || !in_group_p(sbi->s_resgid))) |
| 596 | root_blocks = ext4_r_blocks_count(sbi->s_es); |
| 597 | |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 598 | if (free_blocks - (nblocks + root_blocks + dirty_blocks) < |
| 599 | EXT4_FREEBLOCKS_WATERMARK) { |
| 600 | free_blocks = percpu_counter_sum(fbc); |
| 601 | dirty_blocks = percpu_counter_sum(dbc); |
| 602 | if (dirty_blocks < 0) { |
| 603 | printk(KERN_CRIT "Dirty block accounting " |
| 604 | "went wrong %lld\n", |
| 605 | dirty_blocks); |
| 606 | } |
| 607 | } |
| 608 | /* Check whether we have space after |
| 609 | * accounting for current dirty blocks |
| 610 | */ |
Aneesh Kumar K.V | 5c79161 | 2008-10-08 23:12:24 -0400 | [diff] [blame] | 611 | if (free_blocks < ((root_blocks + nblocks) + dirty_blocks)) |
Aneesh Kumar K.V | a30d542 | 2008-10-09 10:56:23 -0400 | [diff] [blame] | 612 | /* we don't have free space */ |
| 613 | return -ENOSPC; |
| 614 | |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 615 | /* Add the blocks to nblocks */ |
| 616 | percpu_counter_add(dbc, nblocks); |
Aneesh Kumar K.V | a30d542 | 2008-10-09 10:56:23 -0400 | [diff] [blame] | 617 | return 0; |
| 618 | } |
| 619 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 620 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 621 | * ext4_has_free_blocks() |
Mingming Cao | 0703143 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 622 | * @sbi: in-core super block structure. |
| 623 | * @nblocks: number of neeed blocks |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 624 | * |
Mingming Cao | 0703143 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 625 | * Check if filesystem has free blocks available for allocation. |
| 626 | * Return the number of blocks avaible for allocation for this request |
| 627 | * On success, return nblocks |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 628 | */ |
Mingming Cao | 0703143 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 629 | ext4_fsblk_t ext4_has_free_blocks(struct ext4_sb_info *sbi, |
Aneesh Kumar K.V | 5c79161 | 2008-10-08 23:12:24 -0400 | [diff] [blame] | 630 | s64 nblocks) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 631 | { |
Aneesh Kumar K.V | 5c79161 | 2008-10-08 23:12:24 -0400 | [diff] [blame] | 632 | s64 free_blocks, dirty_blocks; |
| 633 | s64 root_blocks = 0; |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 634 | struct percpu_counter *fbc = &sbi->s_freeblocks_counter; |
| 635 | struct percpu_counter *dbc = &sbi->s_dirtyblocks_counter; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 636 | |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 637 | free_blocks = percpu_counter_read_positive(fbc); |
| 638 | dirty_blocks = percpu_counter_read_positive(dbc); |
Mingming Cao | 0703143 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 639 | |
| 640 | if (!capable(CAP_SYS_RESOURCE) && |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 641 | sbi->s_resuid != current->fsuid && |
Mingming Cao | 0703143 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 642 | (sbi->s_resgid == 0 || !in_group_p(sbi->s_resgid))) |
| 643 | root_blocks = ext4_r_blocks_count(sbi->s_es); |
Aneesh Kumar K.V | a30d542 | 2008-10-09 10:56:23 -0400 | [diff] [blame] | 644 | |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 645 | if (free_blocks - (nblocks + root_blocks + dirty_blocks) < |
| 646 | EXT4_FREEBLOCKS_WATERMARK) { |
Aneesh Kumar K.V | 5c79161 | 2008-10-08 23:12:24 -0400 | [diff] [blame] | 647 | free_blocks = percpu_counter_sum(fbc); |
| 648 | dirty_blocks = percpu_counter_sum(dbc); |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 649 | } |
| 650 | if (free_blocks <= (root_blocks + dirty_blocks)) |
Aneesh Kumar K.V | 16eb729 | 2008-08-19 21:16:54 -0400 | [diff] [blame] | 651 | /* we don't have free space */ |
| 652 | return 0; |
Aneesh Kumar K.V | 5c79161 | 2008-10-08 23:12:24 -0400 | [diff] [blame] | 653 | |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 654 | if (free_blocks - (root_blocks + dirty_blocks) < nblocks) |
Aneesh Kumar K.V | 5c79161 | 2008-10-08 23:12:24 -0400 | [diff] [blame] | 655 | return free_blocks - (root_blocks + dirty_blocks); |
Mingming Cao | 0703143 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 656 | return nblocks; |
Aneesh Kumar K.V | a30d542 | 2008-10-09 10:56:23 -0400 | [diff] [blame] | 657 | } |
Mingming Cao | 0703143 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 658 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 659 | |
| 660 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 661 | * ext4_should_retry_alloc() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 662 | * @sb: super block |
| 663 | * @retries number of attemps has been made |
| 664 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 665 | * ext4_should_retry_alloc() is called when ENOSPC is returned, and if |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 666 | * it is profitable to retry the operation, this function will wait |
| 667 | * for the current or commiting transaction to complete, and then |
| 668 | * return TRUE. |
| 669 | * |
| 670 | * if the total number of retries exceed three times, return FALSE. |
| 671 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 672 | int ext4_should_retry_alloc(struct super_block *sb, int *retries) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 673 | { |
Mingming Cao | 0703143 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 674 | if (!ext4_has_free_blocks(EXT4_SB(sb), 1) || (*retries)++ > 3) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 675 | return 0; |
| 676 | |
| 677 | jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id); |
| 678 | |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 679 | return jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 682 | #define EXT4_META_BLOCK 0x1 |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 683 | |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 684 | static ext4_fsblk_t do_blk_alloc(handle_t *handle, struct inode *inode, |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 685 | ext4_lblk_t iblock, ext4_fsblk_t goal, |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 686 | unsigned long *count, int *errp, int flags) |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 687 | { |
| 688 | struct ext4_allocation_request ar; |
| 689 | ext4_fsblk_t ret; |
| 690 | |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 691 | memset(&ar, 0, sizeof(ar)); |
| 692 | /* Fill with neighbour allocated blocks */ |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 693 | |
| 694 | ar.inode = inode; |
| 695 | ar.goal = goal; |
| 696 | ar.len = *count; |
| 697 | ar.logical = iblock; |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 698 | |
| 699 | if (S_ISREG(inode->i_mode) && !(flags & EXT4_META_BLOCK)) |
| 700 | /* enable in-core preallocation for data block allocation */ |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 701 | ar.flags = EXT4_MB_HINT_DATA; |
| 702 | else |
| 703 | /* disable in-core preallocation for non-regular files */ |
| 704 | ar.flags = 0; |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 705 | |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 706 | ret = ext4_mb_new_blocks(handle, &ar, errp); |
| 707 | *count = ar.len; |
| 708 | return ret; |
| 709 | } |
| 710 | |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 711 | /* |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 712 | * ext4_new_meta_blocks() -- allocate block for meta data (indexing) blocks |
| 713 | * |
| 714 | * @handle: handle to this transaction |
| 715 | * @inode: file inode |
| 716 | * @goal: given target block(filesystem wide) |
| 717 | * @count: total number of blocks need |
| 718 | * @errp: error code |
| 719 | * |
| 720 | * Return 1st allocated block numberon success, *count stores total account |
| 721 | * error stores in errp pointer |
| 722 | */ |
| 723 | ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode, |
| 724 | ext4_fsblk_t goal, unsigned long *count, int *errp) |
| 725 | { |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 726 | ext4_fsblk_t ret; |
| 727 | ret = do_blk_alloc(handle, inode, 0, goal, |
| 728 | count, errp, EXT4_META_BLOCK); |
| 729 | /* |
| 730 | * Account for the allocated meta blocks |
| 731 | */ |
Aneesh Kumar K.V | 166348d | 2008-09-08 23:08:40 -0400 | [diff] [blame] | 732 | if (!(*errp) && EXT4_I(inode)->i_delalloc_reserved_flag) { |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 733 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
| 734 | EXT4_I(inode)->i_allocated_meta_blocks += *count; |
| 735 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
| 736 | } |
| 737 | return ret; |
| 738 | } |
| 739 | |
| 740 | /* |
| 741 | * ext4_new_meta_block() -- allocate block for meta data (indexing) blocks |
| 742 | * |
| 743 | * @handle: handle to this transaction |
| 744 | * @inode: file inode |
| 745 | * @goal: given target block(filesystem wide) |
| 746 | * @errp: error code |
| 747 | * |
| 748 | * Return allocated block number on success |
| 749 | */ |
| 750 | ext4_fsblk_t ext4_new_meta_block(handle_t *handle, struct inode *inode, |
| 751 | ext4_fsblk_t goal, int *errp) |
| 752 | { |
| 753 | unsigned long count = 1; |
| 754 | return ext4_new_meta_blocks(handle, inode, goal, &count, errp); |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | /* |
| 758 | * ext4_new_blocks() -- allocate data blocks |
| 759 | * |
| 760 | * @handle: handle to this transaction |
| 761 | * @inode: file inode |
| 762 | * @goal: given target block(filesystem wide) |
| 763 | * @count: total number of blocks need |
| 764 | * @errp: error code |
| 765 | * |
| 766 | * Return 1st allocated block numberon success, *count stores total account |
| 767 | * error stores in errp pointer |
| 768 | */ |
| 769 | |
| 770 | ext4_fsblk_t ext4_new_blocks(handle_t *handle, struct inode *inode, |
| 771 | ext4_lblk_t iblock, ext4_fsblk_t goal, |
| 772 | unsigned long *count, int *errp) |
| 773 | { |
| 774 | return do_blk_alloc(handle, inode, iblock, goal, count, errp, 0); |
| 775 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 776 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 777 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 778 | * ext4_count_free_blocks() -- count filesystem free blocks |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 779 | * @sb: superblock |
| 780 | * |
| 781 | * Adds up the number of free blocks from each block group. |
| 782 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 783 | ext4_fsblk_t ext4_count_free_blocks(struct super_block *sb) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 784 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 785 | ext4_fsblk_t desc_count; |
| 786 | struct ext4_group_desc *gdp; |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 787 | ext4_group_t i; |
| 788 | ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 789 | #ifdef EXT4FS_DEBUG |
| 790 | struct ext4_super_block *es; |
| 791 | ext4_fsblk_t bitmap_count; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 792 | unsigned long x; |
| 793 | struct buffer_head *bitmap_bh = NULL; |
| 794 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 795 | es = EXT4_SB(sb)->s_es; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 796 | desc_count = 0; |
| 797 | bitmap_count = 0; |
| 798 | gdp = NULL; |
| 799 | |
| 800 | smp_rmb(); |
| 801 | for (i = 0; i < ngroups; i++) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 802 | gdp = ext4_get_group_desc(sb, i, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 803 | if (!gdp) |
| 804 | continue; |
| 805 | desc_count += le16_to_cpu(gdp->bg_free_blocks_count); |
| 806 | brelse(bitmap_bh); |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 807 | bitmap_bh = ext4_read_block_bitmap(sb, i); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 808 | if (bitmap_bh == NULL) |
| 809 | continue; |
| 810 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 811 | x = ext4_count_free(bitmap_bh, sb->s_blocksize); |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 812 | printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 813 | i, le16_to_cpu(gdp->bg_free_blocks_count), x); |
| 814 | bitmap_count += x; |
| 815 | } |
| 816 | brelse(bitmap_bh); |
Theodore Ts'o | 4776004 | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 817 | printk(KERN_DEBUG "ext4_count_free_blocks: stored = %llu" |
| 818 | ", computed = %llu, %llu\n", ext4_free_blocks_count(es), |
| 819 | desc_count, bitmap_count); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 820 | return bitmap_count; |
| 821 | #else |
| 822 | desc_count = 0; |
| 823 | smp_rmb(); |
| 824 | for (i = 0; i < ngroups; i++) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 825 | gdp = ext4_get_group_desc(sb, i, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 826 | if (!gdp) |
| 827 | continue; |
| 828 | desc_count += le16_to_cpu(gdp->bg_free_blocks_count); |
| 829 | } |
| 830 | |
| 831 | return desc_count; |
| 832 | #endif |
| 833 | } |
| 834 | |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 835 | static inline int test_root(ext4_group_t a, int b) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 836 | { |
| 837 | int num = b; |
| 838 | |
| 839 | while (a > num) |
| 840 | num *= b; |
| 841 | return num == a; |
| 842 | } |
| 843 | |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 844 | static int ext4_group_sparse(ext4_group_t group) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 845 | { |
| 846 | if (group <= 1) |
| 847 | return 1; |
| 848 | if (!(group & 1)) |
| 849 | return 0; |
| 850 | return (test_root(group, 7) || test_root(group, 5) || |
| 851 | test_root(group, 3)); |
| 852 | } |
| 853 | |
| 854 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 855 | * ext4_bg_has_super - number of blocks used by the superblock in group |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 856 | * @sb: superblock for filesystem |
| 857 | * @group: group number to check |
| 858 | * |
| 859 | * Return the number of blocks used by the superblock (primary or backup) |
| 860 | * in this group. Currently this will be only 0 or 1. |
| 861 | */ |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 862 | int ext4_bg_has_super(struct super_block *sb, ext4_group_t group) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 863 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 864 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 865 | EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) && |
| 866 | !ext4_group_sparse(group)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 867 | return 0; |
| 868 | return 1; |
| 869 | } |
| 870 | |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 871 | static unsigned long ext4_bg_num_gdb_meta(struct super_block *sb, |
| 872 | ext4_group_t group) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 873 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 874 | unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb); |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 875 | ext4_group_t first = metagroup * EXT4_DESC_PER_BLOCK(sb); |
| 876 | ext4_group_t last = first + EXT4_DESC_PER_BLOCK(sb) - 1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 877 | |
| 878 | if (group == first || group == first + 1 || group == last) |
| 879 | return 1; |
| 880 | return 0; |
| 881 | } |
| 882 | |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 883 | static unsigned long ext4_bg_num_gdb_nometa(struct super_block *sb, |
| 884 | ext4_group_t group) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 885 | { |
Akinobu Mita | 859cb93 | 2008-02-06 01:40:17 -0800 | [diff] [blame] | 886 | return ext4_bg_has_super(sb, group) ? EXT4_SB(sb)->s_gdb_count : 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | /** |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 890 | * ext4_bg_num_gdb - number of blocks used by the group table in group |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 891 | * @sb: superblock for filesystem |
| 892 | * @group: group number to check |
| 893 | * |
| 894 | * Return the number of blocks used by the group descriptor table |
| 895 | * (primary or backup) in this group. In the future there may be a |
| 896 | * different number of descriptor blocks in each group. |
| 897 | */ |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 898 | unsigned long ext4_bg_num_gdb(struct super_block *sb, ext4_group_t group) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 899 | { |
| 900 | unsigned long first_meta_bg = |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 901 | le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg); |
| 902 | unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 903 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 904 | if (!EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG) || |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 905 | metagroup < first_meta_bg) |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 906 | return ext4_bg_num_gdb_nometa(sb, group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 907 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 908 | return ext4_bg_num_gdb_meta(sb,group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 909 | |
| 910 | } |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame^] | 911 | |