blob: 152c390f3c3f816e8d42dd79638f48c3681fb234 [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/balloc.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
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 Caodab291a2006-10-11 01:21:01 -070017#include <linux/jbd2.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070018#include <linux/quotaops.h>
19#include <linux/buffer_head.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040020#include "ext4.h"
21#include "ext4_jbd2.h"
Andreas Dilger717d50e2007-10-16 18:38:25 -040022#include "group.h"
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040023
Dave Kleikampac27a0e2006-10-11 01:20:50 -070024/*
25 * balloc.c contains the blocks allocation and deallocation routines
26 */
27
28/*
Andrew Morton72b64b52006-10-11 01:21:18 -070029 * Calculate the block group number and offset, given a block number
30 */
31void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr,
Avantika Mathurfd2d4292008-01-28 23:58:27 -050032 ext4_group_t *blockgrpp, ext4_grpblk_t *offsetp)
Andrew Morton72b64b52006-10-11 01:21:18 -070033{
Dave Kleikamp8c55e202007-05-24 13:04:54 -040034 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Andrew Morton72b64b52006-10-11 01:21:18 -070035 ext4_grpblk_t offset;
36
Dave Kleikamp8c55e202007-05-24 13:04:54 -040037 blocknr = blocknr - le32_to_cpu(es->s_first_data_block);
Andrew Mortonf4e5bc22006-10-11 01:21:19 -070038 offset = do_div(blocknr, EXT4_BLOCKS_PER_GROUP(sb));
Andrew Morton72b64b52006-10-11 01:21:18 -070039 if (offsetp)
40 *offsetp = offset;
41 if (blockgrpp)
Dave Kleikamp8c55e202007-05-24 13:04:54 -040042 *blockgrpp = blocknr;
Andrew Morton72b64b52006-10-11 01:21:18 -070043
44}
45
Jose R. Santos0bf7e832008-06-03 14:07:29 -040046static 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.V74778272008-07-11 19:27:31 -040050 ext4_get_group_no_and_offset(sb, block, &actual_group, NULL);
Jose R. Santos0bf7e832008-06-03 14:07:29 -040051 if (actual_group == block_group)
52 return 1;
53 return 0;
54}
55
56static 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'oc2ea3fd2008-10-10 09:40:52 -040086
Andreas Dilger717d50e2007-10-16 18:38:25 -040087/* Initializes an uninitialized block bitmap if given, and returns the
88 * number of blocks free in the group. */
89unsigned ext4_init_block_bitmap(struct super_block *sb, struct buffer_head *bh,
Avantika Mathurfd2d4292008-01-28 23:58:27 -050090 ext4_group_t block_group, struct ext4_group_desc *gdp)
Andreas Dilger717d50e2007-10-16 18:38:25 -040091{
Andreas Dilger717d50e2007-10-16 18:38:25 -040092 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 Harrison46e665e2008-04-17 10:38:59 -0400102 ext4_error(sb, __func__,
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500103 "Checksum bad for group %lu\n", block_group);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400104 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 Mita6afd6702008-07-11 19:27:31 -0400125 bit_max += ext4_bg_num_gdb(sb, block_group);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400126 }
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'oaf5bc922008-09-08 22:25:24 -0400136 (EXT4_BLOCKS_PER_GROUP(sb) * (sbi->s_groups_count - 1));
Andreas Dilger717d50e2007-10-16 18:38:25 -0400137 } else {
138 group_blocks = EXT4_BLOCKS_PER_GROUP(sb);
139 }
140
141 free_blocks = group_blocks - bit_max;
142
143 if (bh) {
Jose R. Santos0bf7e832008-06-03 14:07:29 -0400144 ext4_fsblk_t start, tmp;
145 int flex_bg = 0;
Akinobu Mitad00a6d72008-04-17 10:38:59 -0400146
Andreas Dilger717d50e2007-10-16 18:38:25 -0400147 for (bit = 0; bit < bit_max; bit++)
148 ext4_set_bit(bit, bh->b_data);
149
Akinobu Mitad00a6d72008-04-17 10:38:59 -0400150 start = ext4_group_first_block_no(sb, block_group);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400151
Jose R. Santos0bf7e832008-06-03 14:07:29 -0400152 if (EXT4_HAS_INCOMPAT_FEATURE(sb,
153 EXT4_FEATURE_INCOMPAT_FLEX_BG))
154 flex_bg = 1;
Andreas Dilger717d50e2007-10-16 18:38:25 -0400155
Jose R. Santos0bf7e832008-06-03 14:07:29 -0400156 /* 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 Dilger717d50e2007-10-16 18:38:25 -0400172 /*
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. Santos0bf7e832008-06-03 14:07:29 -0400179 return free_blocks - ext4_group_used_meta_blocks(sb, block_group);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400180}
181
182
Andrew Morton72b64b52006-10-11 01:21:18 -0700183/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700184 * 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.Ve6274322007-02-20 13:57:58 -0800191 * when a file system is mounted (see ext4_fill_super).
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700192 */
193
194
195#define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
196
197/**
Mingming Cao617ba132006-10-11 01:20:53 -0700198 * ext4_get_group_desc() -- load group descriptor from disk
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700199 * @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'oaf5bc922008-09-08 22:25:24 -0400204struct ext4_group_desc * ext4_get_group_desc(struct super_block *sb,
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500205 ext4_group_t block_group,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400206 struct buffer_head **bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700207{
208 unsigned long group_desc;
209 unsigned long offset;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400210 struct ext4_group_desc *desc;
Mingming Cao617ba132006-10-11 01:20:53 -0700211 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700212
213 if (block_group >= sbi->s_groups_count) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400214 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 Kleikampac27a0e2006-10-11 01:20:50 -0700218
219 return NULL;
220 }
221 smp_rmb();
222
Mingming Cao617ba132006-10-11 01:20:53 -0700223 group_desc = block_group >> EXT4_DESC_PER_BLOCK_BITS(sb);
224 offset = block_group & (EXT4_DESC_PER_BLOCK(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700225 if (!sbi->s_group_desc[group_desc]) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400226 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 Kleikampac27a0e2006-10-11 01:20:50 -0700230 return NULL;
231 }
232
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -0700233 desc = (struct ext4_group_desc *)(
234 (__u8 *)sbi->s_group_desc[group_desc]->b_data +
235 offset * EXT4_DESC_SIZE(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700236 if (bh)
237 *bh = sbi->s_group_desc[group_desc];
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -0700238 return desc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700239}
240
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500241static 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
286err_out:
Harvey Harrison46e665e2008-04-17 10:38:59 -0400287 ext4_error(sb, __func__,
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500288 "Invalid block bitmap - "
289 "block_group = %d, block = %llu",
290 block_group, bitmap_blk);
291 return 0;
292}
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700293/**
Theodore Ts'o574ca172008-07-11 19:27:31 -0400294 * ext4_read_block_bitmap()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700295 * @sb: super block
296 * @block_group: given block group
297 *
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500298 * Read the bitmap for a given block_group,and validate the
299 * bits for block/inode/inode tables are set in the bitmaps
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700300 *
301 * Return buffer_head on success or NULL in case of failure.
302 */
Andreas Dilger717d50e2007-10-16 18:38:25 -0400303struct buffer_head *
Theodore Ts'o574ca172008-07-11 19:27:31 -0400304ext4_read_block_bitmap(struct super_block *sb, ext4_group_t block_group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700305{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400306 struct ext4_group_desc *desc;
307 struct buffer_head *bh = NULL;
Aneesh Kumar K.V7c9e69f2007-10-16 23:27:02 -0700308 ext4_fsblk_t bitmap_blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700309
Andreas Dilger717d50e2007-10-16 18:38:25 -0400310 desc = ext4_get_group_desc(sb, block_group, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700311 if (!desc)
Aneesh Kumar K.V7c9e69f2007-10-16 23:27:02 -0700312 return NULL;
313 bitmap_blk = ext4_block_bitmap(sb, desc);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500314 bh = sb_getblk(sb, bitmap_blk);
315 if (unlikely(!bh)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400316 ext4_error(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700317 "Cannot read block bitmap - "
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400318 "block_group = %lu, block_bitmap = %llu",
319 block_group, bitmap_blk);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500320 return NULL;
321 }
Frederic Bohec806e682008-10-10 08:09:18 -0400322 if (buffer_uptodate(bh) &&
323 !(desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)))
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500324 return bh;
325
Frederic Bohec806e682008-10-10 08:09:18 -0400326 lock_buffer(bh);
Eric Sandeenb5f10ee2008-08-02 21:21:08 -0400327 spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group));
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500328 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
329 ext4_init_block_bitmap(sb, bh, block_group, desc);
330 set_buffer_uptodate(bh);
331 unlock_buffer(bh);
Eric Sandeenb5f10ee2008-08-02 21:21:08 -0400332 spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500333 return bh;
334 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -0400335 spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500336 if (bh_submit_read(bh) < 0) {
337 put_bh(bh);
Harvey Harrison46e665e2008-04-17 10:38:59 -0400338 ext4_error(sb, __func__,
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500339 "Cannot read block bitmap - "
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400340 "block_group = %lu, block_bitmap = %llu",
341 block_group, bitmap_blk);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500342 return NULL;
343 }
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -0400344 ext4_valid_block_bitmap(sb, desc, block_group, bh);
345 /*
346 * file system mounted not to panic on error,
347 * continue with corrupt bitmap
348 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700349 return bh;
350}
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700351
352/**
Mingming Cao617ba132006-10-11 01:20:53 -0700353 * ext4_free_blocks_sb() -- Free given blocks and update quota
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700354 * @handle: handle to this transaction
355 * @sb: super block
356 * @block: start physcial block to free
357 * @count: number of blocks to free
358 * @pdquot_freed_blocks: pointer to quota
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400359 *
360 * XXX This function is only used by the on-line resizing code, which
361 * should probably be fixed up to call the mballoc variant. There
362 * this needs to be cleaned up later; in fact, I'm not convinced this
363 * is 100% correct in the face of the mballoc code. The online resizing
364 * code needs to be fixed up to more tightly (and correctly) interlock
365 * with the mballoc code.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700366 */
Mingming Cao617ba132006-10-11 01:20:53 -0700367void ext4_free_blocks_sb(handle_t *handle, struct super_block *sb,
368 ext4_fsblk_t block, unsigned long count,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700369 unsigned long *pdquot_freed_blocks)
370{
371 struct buffer_head *bitmap_bh = NULL;
372 struct buffer_head *gd_bh;
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500373 ext4_group_t block_group;
Mingming Cao617ba132006-10-11 01:20:53 -0700374 ext4_grpblk_t bit;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700375 unsigned long i;
376 unsigned long overflow;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400377 struct ext4_group_desc *desc;
378 struct ext4_super_block *es;
Mingming Cao617ba132006-10-11 01:20:53 -0700379 struct ext4_sb_info *sbi;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700380 int err = 0, ret;
Mingming Cao617ba132006-10-11 01:20:53 -0700381 ext4_grpblk_t group_freed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700382
383 *pdquot_freed_blocks = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700384 sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700385 es = sbi->s_es;
386 if (block < le32_to_cpu(es->s_first_data_block) ||
387 block + count < block ||
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700388 block + count > ext4_blocks_count(es)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400389 ext4_error(sb, "ext4_free_blocks",
390 "Freeing blocks not in datazone - "
391 "block = %llu, count = %lu", block, count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700392 goto error_return;
393 }
394
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400395 ext4_debug("freeing block(s) %llu-%llu\n", block, block + count - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700396
397do_more:
398 overflow = 0;
Mingming Cao3a5b2ec2006-10-11 01:21:05 -0700399 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700400 /*
401 * Check to see if we are freeing blocks across a group
402 * boundary.
403 */
Mingming Cao617ba132006-10-11 01:20:53 -0700404 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
405 overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700406 count -= overflow;
407 }
408 brelse(bitmap_bh);
Theodore Ts'o574ca172008-07-11 19:27:31 -0400409 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700410 if (!bitmap_bh)
411 goto error_return;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400412 desc = ext4_get_group_desc(sb, block_group, &gd_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700413 if (!desc)
414 goto error_return;
415
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700416 if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
417 in_range(ext4_inode_bitmap(sb, desc), block, count) ||
418 in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
419 in_range(block + count - 1, ext4_inode_table(sb, desc),
Aneesh Kumar K.Vcb47dce2008-01-28 23:58:27 -0500420 sbi->s_itb_per_group)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400421 ext4_error(sb, "ext4_free_blocks",
422 "Freeing blocks in system zones - "
423 "Block = %llu, count = %lu",
424 block, count);
Aneesh Kumar K.Vcb47dce2008-01-28 23:58:27 -0500425 goto error_return;
426 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700427
428 /*
429 * We are about to start releasing blocks in the bitmap,
430 * so we need undo access.
431 */
432 /* @@@ check errors */
433 BUFFER_TRACE(bitmap_bh, "getting undo access");
Mingming Cao617ba132006-10-11 01:20:53 -0700434 err = ext4_journal_get_undo_access(handle, bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700435 if (err)
436 goto error_return;
437
438 /*
439 * We are about to modify some metadata. Call the journal APIs
440 * to unshare ->b_data if a currently-committing transaction is
441 * using it
442 */
443 BUFFER_TRACE(gd_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700444 err = ext4_journal_get_write_access(handle, gd_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700445 if (err)
446 goto error_return;
447
448 jbd_lock_bh_state(bitmap_bh);
449
450 for (i = 0, group_freed = 0; i < count; i++) {
451 /*
452 * An HJ special. This is expensive...
453 */
Jose R. Santose23291b2007-07-18 08:57:06 -0400454#ifdef CONFIG_JBD2_DEBUG
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700455 jbd_unlock_bh_state(bitmap_bh);
456 {
457 struct buffer_head *debug_bh;
458 debug_bh = sb_find_get_block(sb, block + i);
459 if (debug_bh) {
460 BUFFER_TRACE(debug_bh, "Deleted!");
461 if (!bh2jh(bitmap_bh)->b_committed_data)
462 BUFFER_TRACE(debug_bh,
463 "No commited data in bitmap");
464 BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap");
465 __brelse(debug_bh);
466 }
467 }
468 jbd_lock_bh_state(bitmap_bh);
469#endif
470 if (need_resched()) {
471 jbd_unlock_bh_state(bitmap_bh);
472 cond_resched();
473 jbd_lock_bh_state(bitmap_bh);
474 }
475 /* @@@ This prevents newly-allocated data from being
476 * freed and then reallocated within the same
477 * transaction.
478 *
479 * Ideally we would want to allow that to happen, but to
Mingming Caodab291a2006-10-11 01:21:01 -0700480 * do so requires making jbd2_journal_forget() capable of
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700481 * revoking the queued write of a data block, which
482 * implies blocking on the journal lock. *forget()
483 * cannot block due to truncate races.
484 *
Mingming Caodab291a2006-10-11 01:21:01 -0700485 * Eventually we can fix this by making jbd2_journal_forget()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700486 * return a status indicating whether or not it was able
487 * to revoke the buffer. On successful revoke, it is
488 * safe not to set the allocation bit in the committed
489 * bitmap, because we know that there is no outstanding
490 * activity on the buffer any more and so it is safe to
491 * reallocate it.
492 */
493 BUFFER_TRACE(bitmap_bh, "set in b_committed_data");
494 J_ASSERT_BH(bitmap_bh,
495 bh2jh(bitmap_bh)->b_committed_data != NULL);
Mingming Cao617ba132006-10-11 01:20:53 -0700496 ext4_set_bit_atomic(sb_bgl_lock(sbi, block_group), bit + i,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700497 bh2jh(bitmap_bh)->b_committed_data);
498
499 /*
500 * We clear the bit in the bitmap after setting the committed
501 * data bit, because this is the reverse order to that which
502 * the allocator uses.
503 */
504 BUFFER_TRACE(bitmap_bh, "clear bit");
Mingming Cao617ba132006-10-11 01:20:53 -0700505 if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700506 bit + i, bitmap_bh->b_data)) {
507 jbd_unlock_bh_state(bitmap_bh);
Harvey Harrison46e665e2008-04-17 10:38:59 -0400508 ext4_error(sb, __func__,
Mingming Cao2ae02102006-10-11 01:21:11 -0700509 "bit already cleared for block %llu",
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700510 (ext4_fsblk_t)(block + i));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700511 jbd_lock_bh_state(bitmap_bh);
512 BUFFER_TRACE(bitmap_bh, "bit already cleared");
513 } else {
514 group_freed++;
515 }
516 }
517 jbd_unlock_bh_state(bitmap_bh);
518
519 spin_lock(sb_bgl_lock(sbi, block_group));
Marcin Slusarze8546d02008-04-17 10:38:59 -0400520 le16_add_cpu(&desc->bg_free_blocks_count, group_freed);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400521 desc->bg_checksum = ext4_group_desc_csum(sbi, block_group, desc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700522 spin_unlock(sb_bgl_lock(sbi, block_group));
Peter Zijlstraaa0dff22007-10-16 23:25:42 -0700523 percpu_counter_add(&sbi->s_freeblocks_counter, count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700524
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400525 if (sbi->s_log_groups_per_flex) {
526 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
527 spin_lock(sb_bgl_lock(sbi, flex_group));
528 sbi->s_flex_groups[flex_group].free_blocks += count;
529 spin_unlock(sb_bgl_lock(sbi, flex_group));
530 }
531
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700532 /* We dirtied the bitmap block */
533 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
Mingming Cao617ba132006-10-11 01:20:53 -0700534 err = ext4_journal_dirty_metadata(handle, bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700535
536 /* And the group descriptor block */
537 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Mingming Cao617ba132006-10-11 01:20:53 -0700538 ret = ext4_journal_dirty_metadata(handle, gd_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700539 if (!err) err = ret;
540 *pdquot_freed_blocks += group_freed;
541
542 if (overflow && !err) {
543 block += count;
544 count = overflow;
545 goto do_more;
546 }
547 sb->s_dirt = 1;
548error_return:
549 brelse(bitmap_bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700550 ext4_std_error(sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700551 return;
552}
553
554/**
Mingming Cao617ba132006-10-11 01:20:53 -0700555 * ext4_free_blocks() -- Free given blocks and update quota
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700556 * @handle: handle for this transaction
557 * @inode: inode
558 * @block: start physical block to free
559 * @count: number of blocks to count
Alex Tomasc9de5602008-01-29 00:19:52 -0500560 * @metadata: Are these metadata blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700561 */
Mingming Cao617ba132006-10-11 01:20:53 -0700562void ext4_free_blocks(handle_t *handle, struct inode *inode,
Alex Tomasc9de5602008-01-29 00:19:52 -0500563 ext4_fsblk_t block, unsigned long count,
564 int metadata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700565{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400566 struct super_block *sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700567 unsigned long dquot_freed_blocks;
568
Alex Tomasc9de5602008-01-29 00:19:52 -0500569 /* this isn't the right place to decide whether block is metadata
570 * inode.c/extents.c knows better, but for safety ... */
Aneesh Kumar K.Va1aebc12008-10-10 20:13:31 -0400571 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
572 metadata = 1;
573
574 /* We need to make sure we don't reuse
575 * block released untill the transaction commit.
576 * writeback mode have weak data consistency so
577 * don't force data as metadata when freeing block
578 * for writeback mode.
579 */
580 if (metadata == 0 && !ext4_should_writeback_data(inode))
Alex Tomasc9de5602008-01-29 00:19:52 -0500581 metadata = 1;
582
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700583 sb = inode->i_sb;
Alex Tomasc9de5602008-01-29 00:19:52 -0500584
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400585 ext4_mb_free_blocks(handle, inode, block, count,
586 metadata, &dquot_freed_blocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700587 if (dquot_freed_blocks)
588 DQUOT_FREE_BLOCK(inode, dquot_freed_blocks);
589 return;
590}
591
Eric Sandeen8c3bf8a02008-10-28 00:08:12 -0400592/**
593 * ext4_has_free_blocks()
594 * @sbi: in-core super block structure.
595 * @nblocks: number of needed blocks
596 *
597 * Check if filesystem has nblocks free & available for allocation.
598 * On success return 1, return 0 on failure.
599 */
600int ext4_has_free_blocks(struct ext4_sb_info *sbi, s64 nblocks)
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -0400601{
Eric Sandeena9960312008-10-28 00:08:17 -0400602 s64 free_blocks, dirty_blocks, root_blocks;
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -0400603 struct percpu_counter *fbc = &sbi->s_freeblocks_counter;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400604 struct percpu_counter *dbc = &sbi->s_dirtyblocks_counter;
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -0400605
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400606 free_blocks = percpu_counter_read_positive(fbc);
607 dirty_blocks = percpu_counter_read_positive(dbc);
Eric Sandeena9960312008-10-28 00:08:17 -0400608 root_blocks = ext4_r_blocks_count(sbi->s_es);
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -0400609
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400610 if (free_blocks - (nblocks + root_blocks + dirty_blocks) <
611 EXT4_FREEBLOCKS_WATERMARK) {
Andrew Morton02d21162008-12-09 13:14:14 -0800612 free_blocks = percpu_counter_sum_positive(fbc);
613 dirty_blocks = percpu_counter_sum_positive(dbc);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400614 if (dirty_blocks < 0) {
615 printk(KERN_CRIT "Dirty block accounting "
616 "went wrong %lld\n",
Alexander Beregalov8f72fbd2008-10-29 17:13:08 -0400617 (long long)dirty_blocks);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400618 }
619 }
620 /* Check whether we have space after
Eric Sandeena9960312008-10-28 00:08:17 -0400621 * accounting for current dirty blocks & root reserved blocks.
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400622 */
Eric Sandeena9960312008-10-28 00:08:17 -0400623 if (free_blocks >= ((root_blocks + nblocks) + dirty_blocks))
624 return 1;
Aneesh Kumar K.V5c791612008-10-08 23:12:24 -0400625
Eric Sandeena9960312008-10-28 00:08:17 -0400626 /* Hm, nope. Are (enough) root reserved blocks available? */
David Howells4c9c5442008-11-14 10:38:51 +1100627 if (sbi->s_resuid == current_fsuid() ||
Eric Sandeena9960312008-10-28 00:08:17 -0400628 ((sbi->s_resgid != 0) && in_group_p(sbi->s_resgid)) ||
629 capable(CAP_SYS_RESOURCE)) {
630 if (free_blocks >= (nblocks + dirty_blocks))
631 return 1;
632 }
633
634 return 0;
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -0400635}
Mingming Cao07031432008-07-11 19:27:31 -0400636
Eric Sandeen8c3bf8a02008-10-28 00:08:12 -0400637int ext4_claim_free_blocks(struct ext4_sb_info *sbi,
638 s64 nblocks)
639{
640 if (ext4_has_free_blocks(sbi, nblocks)) {
641 percpu_counter_add(&sbi->s_dirtyblocks_counter, nblocks);
642 return 0;
643 } else
644 return -ENOSPC;
645}
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700646
647/**
Mingming Cao617ba132006-10-11 01:20:53 -0700648 * ext4_should_retry_alloc()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700649 * @sb: super block
650 * @retries number of attemps has been made
651 *
Mingming Cao617ba132006-10-11 01:20:53 -0700652 * ext4_should_retry_alloc() is called when ENOSPC is returned, and if
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700653 * it is profitable to retry the operation, this function will wait
654 * for the current or commiting transaction to complete, and then
655 * return TRUE.
656 *
657 * if the total number of retries exceed three times, return FALSE.
658 */
Mingming Cao617ba132006-10-11 01:20:53 -0700659int ext4_should_retry_alloc(struct super_block *sb, int *retries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700660{
Mingming Cao07031432008-07-11 19:27:31 -0400661 if (!ext4_has_free_blocks(EXT4_SB(sb), 1) || (*retries)++ > 3)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700662 return 0;
663
664 jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
665
Mingming Caodab291a2006-10-11 01:21:01 -0700666 return jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700667}
668
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400669#define EXT4_META_BLOCK 0x1
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700670
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400671static ext4_fsblk_t do_blk_alloc(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400672 ext4_lblk_t iblock, ext4_fsblk_t goal,
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400673 unsigned long *count, int *errp, int flags)
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400674{
675 struct ext4_allocation_request ar;
676 ext4_fsblk_t ret;
677
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400678 memset(&ar, 0, sizeof(ar));
679 /* Fill with neighbour allocated blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400680
681 ar.inode = inode;
682 ar.goal = goal;
683 ar.len = *count;
684 ar.logical = iblock;
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400685
686 if (S_ISREG(inode->i_mode) && !(flags & EXT4_META_BLOCK))
687 /* enable in-core preallocation for data block allocation */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400688 ar.flags = EXT4_MB_HINT_DATA;
689 else
690 /* disable in-core preallocation for non-regular files */
691 ar.flags = 0;
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400692
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400693 ret = ext4_mb_new_blocks(handle, &ar, errp);
694 *count = ar.len;
695 return ret;
696}
697
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400698/*
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400699 * ext4_new_meta_blocks() -- allocate block for meta data (indexing) blocks
700 *
701 * @handle: handle to this transaction
702 * @inode: file inode
703 * @goal: given target block(filesystem wide)
704 * @count: total number of blocks need
705 * @errp: error code
706 *
707 * Return 1st allocated block numberon success, *count stores total account
708 * error stores in errp pointer
709 */
710ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
711 ext4_fsblk_t goal, unsigned long *count, int *errp)
712{
Mingming Caod2a17632008-07-14 17:52:37 -0400713 ext4_fsblk_t ret;
714 ret = do_blk_alloc(handle, inode, 0, goal,
715 count, errp, EXT4_META_BLOCK);
716 /*
717 * Account for the allocated meta blocks
718 */
Aneesh Kumar K.V166348d2008-09-08 23:08:40 -0400719 if (!(*errp) && EXT4_I(inode)->i_delalloc_reserved_flag) {
Mingming Caod2a17632008-07-14 17:52:37 -0400720 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
721 EXT4_I(inode)->i_allocated_meta_blocks += *count;
722 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
723 }
724 return ret;
725}
726
727/*
728 * ext4_new_meta_block() -- allocate block for meta data (indexing) blocks
729 *
730 * @handle: handle to this transaction
731 * @inode: file inode
732 * @goal: given target block(filesystem wide)
733 * @errp: error code
734 *
735 * Return allocated block number on success
736 */
737ext4_fsblk_t ext4_new_meta_block(handle_t *handle, struct inode *inode,
738 ext4_fsblk_t goal, int *errp)
739{
740 unsigned long count = 1;
741 return ext4_new_meta_blocks(handle, inode, goal, &count, errp);
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400742}
743
744/*
745 * ext4_new_blocks() -- allocate data blocks
746 *
747 * @handle: handle to this transaction
748 * @inode: file inode
749 * @goal: given target block(filesystem wide)
750 * @count: total number of blocks need
751 * @errp: error code
752 *
753 * Return 1st allocated block numberon success, *count stores total account
754 * error stores in errp pointer
755 */
756
757ext4_fsblk_t ext4_new_blocks(handle_t *handle, struct inode *inode,
758 ext4_lblk_t iblock, ext4_fsblk_t goal,
759 unsigned long *count, int *errp)
760{
761 return do_blk_alloc(handle, inode, iblock, goal, count, errp, 0);
762}
Alex Tomasc9de5602008-01-29 00:19:52 -0500763
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700764/**
Mingming Cao617ba132006-10-11 01:20:53 -0700765 * ext4_count_free_blocks() -- count filesystem free blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700766 * @sb: superblock
767 *
768 * Adds up the number of free blocks from each block group.
769 */
Mingming Cao617ba132006-10-11 01:20:53 -0700770ext4_fsblk_t ext4_count_free_blocks(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700771{
Mingming Cao617ba132006-10-11 01:20:53 -0700772 ext4_fsblk_t desc_count;
773 struct ext4_group_desc *gdp;
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500774 ext4_group_t i;
775 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
Mingming Cao617ba132006-10-11 01:20:53 -0700776#ifdef EXT4FS_DEBUG
777 struct ext4_super_block *es;
778 ext4_fsblk_t bitmap_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700779 unsigned long x;
780 struct buffer_head *bitmap_bh = NULL;
781
Mingming Cao617ba132006-10-11 01:20:53 -0700782 es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700783 desc_count = 0;
784 bitmap_count = 0;
785 gdp = NULL;
786
787 smp_rmb();
788 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -0700789 gdp = ext4_get_group_desc(sb, i, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700790 if (!gdp)
791 continue;
792 desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
793 brelse(bitmap_bh);
Theodore Ts'o574ca172008-07-11 19:27:31 -0400794 bitmap_bh = ext4_read_block_bitmap(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700795 if (bitmap_bh == NULL)
796 continue;
797
Mingming Cao617ba132006-10-11 01:20:53 -0700798 x = ext4_count_free(bitmap_bh, sb->s_blocksize);
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500799 printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700800 i, le16_to_cpu(gdp->bg_free_blocks_count), x);
801 bitmap_count += x;
802 }
803 brelse(bitmap_bh);
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400804 printk(KERN_DEBUG "ext4_count_free_blocks: stored = %llu"
805 ", computed = %llu, %llu\n", ext4_free_blocks_count(es),
806 desc_count, bitmap_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700807 return bitmap_count;
808#else
809 desc_count = 0;
810 smp_rmb();
811 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -0700812 gdp = ext4_get_group_desc(sb, i, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700813 if (!gdp)
814 continue;
815 desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
816 }
817
818 return desc_count;
819#endif
820}
821
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500822static inline int test_root(ext4_group_t a, int b)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700823{
824 int num = b;
825
826 while (a > num)
827 num *= b;
828 return num == a;
829}
830
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500831static int ext4_group_sparse(ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700832{
833 if (group <= 1)
834 return 1;
835 if (!(group & 1))
836 return 0;
837 return (test_root(group, 7) || test_root(group, 5) ||
838 test_root(group, 3));
839}
840
841/**
Mingming Cao617ba132006-10-11 01:20:53 -0700842 * ext4_bg_has_super - number of blocks used by the superblock in group
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700843 * @sb: superblock for filesystem
844 * @group: group number to check
845 *
846 * Return the number of blocks used by the superblock (primary or backup)
847 * in this group. Currently this will be only 0 or 1.
848 */
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500849int ext4_bg_has_super(struct super_block *sb, ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700850{
Mingming Cao617ba132006-10-11 01:20:53 -0700851 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
852 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) &&
853 !ext4_group_sparse(group))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700854 return 0;
855 return 1;
856}
857
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500858static unsigned long ext4_bg_num_gdb_meta(struct super_block *sb,
859 ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700860{
Mingming Cao617ba132006-10-11 01:20:53 -0700861 unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500862 ext4_group_t first = metagroup * EXT4_DESC_PER_BLOCK(sb);
863 ext4_group_t last = first + EXT4_DESC_PER_BLOCK(sb) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700864
865 if (group == first || group == first + 1 || group == last)
866 return 1;
867 return 0;
868}
869
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500870static unsigned long ext4_bg_num_gdb_nometa(struct super_block *sb,
871 ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700872{
Akinobu Mita859cb932008-02-06 01:40:17 -0800873 return ext4_bg_has_super(sb, group) ? EXT4_SB(sb)->s_gdb_count : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700874}
875
876/**
Mingming Cao617ba132006-10-11 01:20:53 -0700877 * ext4_bg_num_gdb - number of blocks used by the group table in group
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700878 * @sb: superblock for filesystem
879 * @group: group number to check
880 *
881 * Return the number of blocks used by the group descriptor table
882 * (primary or backup) in this group. In the future there may be a
883 * different number of descriptor blocks in each group.
884 */
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500885unsigned long ext4_bg_num_gdb(struct super_block *sb, ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700886{
887 unsigned long first_meta_bg =
Mingming Cao617ba132006-10-11 01:20:53 -0700888 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
889 unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700890
Mingming Cao617ba132006-10-11 01:20:53 -0700891 if (!EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700892 metagroup < first_meta_bg)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400893 return ext4_bg_num_gdb_nometa(sb, group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700894
Mingming Cao617ba132006-10-11 01:20:53 -0700895 return ext4_bg_num_gdb_meta(sb,group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700896
897}
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400898