ext4: split out ext4_free_blocks_after_init()
[linux-block.git] / fs / ext4 / balloc.c
CommitLineData
ac27a0ec 1/*
617ba13b 2 * linux/fs/ext4/balloc.c
ac27a0ec
DK
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>
dab291af 17#include <linux/jbd2.h>
ac27a0ec
DK
18#include <linux/quotaops.h>
19#include <linux/buffer_head.h>
3dcf5451
CH
20#include "ext4.h"
21#include "ext4_jbd2.h"
e21675d4 22#include "mballoc.h"
3dcf5451 23
0562e0ba
JZ
24#include <trace/events/ext4.h>
25
49f7f9af
TT
26static unsigned int num_base_meta_blocks(struct super_block *sb,
27 ext4_group_t block_group);
28
ac27a0ec
DK
29/*
30 * balloc.c contains the blocks allocation and deallocation routines
31 */
32
72b64b59
AM
33/*
34 * Calculate the block group number and offset, given a block number
35 */
36void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr,
fd2d4291 37 ext4_group_t *blockgrpp, ext4_grpblk_t *offsetp)
72b64b59 38{
8c55e204 39 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
72b64b59
AM
40 ext4_grpblk_t offset;
41
8c55e204 42 blocknr = blocknr - le32_to_cpu(es->s_first_data_block);
f4e5bc24 43 offset = do_div(blocknr, EXT4_BLOCKS_PER_GROUP(sb));
72b64b59
AM
44 if (offsetp)
45 *offsetp = offset;
46 if (blockgrpp)
8c55e204 47 *blockgrpp = blocknr;
72b64b59
AM
48
49}
50
0bf7e837
JS
51static int ext4_block_in_group(struct super_block *sb, ext4_fsblk_t block,
52 ext4_group_t block_group)
53{
54 ext4_group_t actual_group;
7477827f 55 ext4_get_group_no_and_offset(sb, block, &actual_group, NULL);
0bf7e837
JS
56 if (actual_group == block_group)
57 return 1;
58 return 0;
59}
60
61static int ext4_group_used_meta_blocks(struct super_block *sb,
e187c658
TT
62 ext4_group_t block_group,
63 struct ext4_group_desc *gdp)
0bf7e837
JS
64{
65 ext4_fsblk_t tmp;
66 struct ext4_sb_info *sbi = EXT4_SB(sb);
67 /* block bitmap, inode bitmap, and inode table blocks */
68 int used_blocks = sbi->s_itb_per_group + 2;
69
70 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
0bf7e837
JS
71 if (!ext4_block_in_group(sb, ext4_block_bitmap(sb, gdp),
72 block_group))
73 used_blocks--;
74
75 if (!ext4_block_in_group(sb, ext4_inode_bitmap(sb, gdp),
76 block_group))
77 used_blocks--;
78
79 tmp = ext4_inode_table(sb, gdp);
80 for (; tmp < ext4_inode_table(sb, gdp) +
81 sbi->s_itb_per_group; tmp++) {
82 if (!ext4_block_in_group(sb, tmp, block_group))
83 used_blocks -= 1;
84 }
85 }
86 return used_blocks;
87}
c2ea3fde 88
49f7f9af
TT
89static unsigned int num_blocks_in_group(struct super_block *sb,
90 ext4_group_t block_group)
91{
92 if (block_group == ext4_get_groups_count(sb) - 1) {
93 /*
94 * Even though mke2fs always initializes the first and
95 * last group, just in case some other tool was used,
96 * we need to make sure we calculate the right free
97 * blocks.
98 */
99 return ext4_blocks_count(EXT4_SB(sb)->s_es) -
100 ext4_group_first_block_no(sb, block_group);
101 } else
102 return EXT4_BLOCKS_PER_GROUP(sb);
103}
104
fd034a84
TT
105/* Initializes an uninitialized block bitmap */
106void ext4_init_block_bitmap(struct super_block *sb, struct buffer_head *bh,
107 ext4_group_t block_group,
108 struct ext4_group_desc *gdp)
717d50e4 109{
49f7f9af 110 unsigned int bit, bit_max = num_base_meta_blocks(sb, block_group);
717d50e4 111 struct ext4_sb_info *sbi = EXT4_SB(sb);
fd034a84
TT
112 ext4_fsblk_t start, tmp;
113 int flex_bg = 0;
114
115 J_ASSERT_BH(bh, buffer_locked(bh));
116
117 /* If checksum is bad mark all blocks used to prevent allocation
118 * essentially implementing a per-group read-only flag. */
119 if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
120 ext4_error(sb, "Checksum bad for group %u", block_group);
121 ext4_free_blks_set(sb, gdp, 0);
122 ext4_free_inodes_set(sb, gdp, 0);
123 ext4_itable_unused_set(sb, gdp, 0);
124 memset(bh->b_data, 0xff, sb->s_blocksize);
125 return;
717d50e4 126 }
fd034a84 127 memset(bh->b_data, 0, sb->s_blocksize);
717d50e4 128
fd034a84
TT
129 for (bit = 0; bit < bit_max; bit++)
130 ext4_set_bit(bit, bh->b_data);
d00a6d7b 131
fd034a84 132 start = ext4_group_first_block_no(sb, block_group);
717d50e4 133
fd034a84
TT
134 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
135 flex_bg = 1;
717d50e4 136
fd034a84
TT
137 /* Set bits for block and inode bitmaps, and inode table */
138 tmp = ext4_block_bitmap(sb, gdp);
139 if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
140 ext4_set_bit(tmp - start, bh->b_data);
717d50e4 141
fd034a84
TT
142 tmp = ext4_inode_bitmap(sb, gdp);
143 if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
144 ext4_set_bit(tmp - start, bh->b_data);
0bf7e837 145
fd034a84
TT
146 tmp = ext4_inode_table(sb, gdp);
147 for (; tmp < ext4_inode_table(sb, gdp) +
148 sbi->s_itb_per_group; tmp++) {
0bf7e837
JS
149 if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
150 ext4_set_bit(tmp - start, bh->b_data);
717d50e4 151 }
fd034a84
TT
152 /*
153 * Also if the number of blocks within the group is less than
154 * the blocksize * 8 ( which is the size of bitmap ), set rest
155 * of the block bitmap to 1
156 */
157 ext4_mark_bitmap_end(num_blocks_in_group(sb, block_group),
158 sb->s_blocksize * 8, bh->b_data);
717d50e4
AD
159}
160
fd034a84
TT
161/* Return the number of free blocks in a block group. It is used when
162 * the block bitmap is uninitialized, so we can't just count the bits
163 * in the bitmap. */
164unsigned ext4_free_blocks_after_init(struct super_block *sb,
165 ext4_group_t block_group,
166 struct ext4_group_desc *gdp)
167{
168 return num_blocks_in_group(sb, block_group) -
169 num_base_meta_blocks(sb, block_group) -
170 ext4_group_used_meta_blocks(sb, block_group, gdp);
171}
717d50e4 172
ac27a0ec
DK
173/*
174 * The free blocks are managed by bitmaps. A file system contains several
175 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
176 * block for inodes, N blocks for the inode table and data blocks.
177 *
178 * The file system contains group descriptors which are located after the
179 * super block. Each descriptor contains the number of the bitmap block and
180 * the free blocks count in the block. The descriptors are loaded in memory
e627432c 181 * when a file system is mounted (see ext4_fill_super).
ac27a0ec
DK
182 */
183
ac27a0ec 184/**
617ba13b 185 * ext4_get_group_desc() -- load group descriptor from disk
ac27a0ec
DK
186 * @sb: super block
187 * @block_group: given block group
188 * @bh: pointer to the buffer head to store the block
189 * group descriptor
190 */
af5bc92d 191struct ext4_group_desc * ext4_get_group_desc(struct super_block *sb,
fd2d4291 192 ext4_group_t block_group,
af5bc92d 193 struct buffer_head **bh)
ac27a0ec 194{
498e5f24
TT
195 unsigned int group_desc;
196 unsigned int offset;
8df9675f 197 ext4_group_t ngroups = ext4_get_groups_count(sb);
af5bc92d 198 struct ext4_group_desc *desc;
617ba13b 199 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec 200
8df9675f 201 if (block_group >= ngroups) {
12062ddd
ES
202 ext4_error(sb, "block_group >= groups_count - block_group = %u,"
203 " groups_count = %u", block_group, ngroups);
ac27a0ec
DK
204
205 return NULL;
206 }
ac27a0ec 207
617ba13b
MC
208 group_desc = block_group >> EXT4_DESC_PER_BLOCK_BITS(sb);
209 offset = block_group & (EXT4_DESC_PER_BLOCK(sb) - 1);
ac27a0ec 210 if (!sbi->s_group_desc[group_desc]) {
12062ddd 211 ext4_error(sb, "Group descriptor not loaded - "
498e5f24 212 "block_group = %u, group_desc = %u, desc = %u",
af5bc92d 213 block_group, group_desc, offset);
ac27a0ec
DK
214 return NULL;
215 }
216
0d1ee42f
AR
217 desc = (struct ext4_group_desc *)(
218 (__u8 *)sbi->s_group_desc[group_desc]->b_data +
219 offset * EXT4_DESC_SIZE(sb));
ac27a0ec
DK
220 if (bh)
221 *bh = sbi->s_group_desc[group_desc];
0d1ee42f 222 return desc;
ac27a0ec
DK
223}
224
abcb2947
AK
225static int ext4_valid_block_bitmap(struct super_block *sb,
226 struct ext4_group_desc *desc,
227 unsigned int block_group,
228 struct buffer_head *bh)
229{
230 ext4_grpblk_t offset;
231 ext4_grpblk_t next_zero_bit;
232 ext4_fsblk_t bitmap_blk;
233 ext4_fsblk_t group_first_block;
234
235 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
236 /* with FLEX_BG, the inode/block bitmaps and itable
237 * blocks may not be in the group at all
238 * so the bitmap validation will be skipped for those groups
239 * or it has to also read the block group where the bitmaps
240 * are located to verify they are set.
241 */
242 return 1;
243 }
244 group_first_block = ext4_group_first_block_no(sb, block_group);
245
246 /* check whether block bitmap block number is set */
247 bitmap_blk = ext4_block_bitmap(sb, desc);
248 offset = bitmap_blk - group_first_block;
249 if (!ext4_test_bit(offset, bh->b_data))
250 /* bad block bitmap */
251 goto err_out;
252
253 /* check whether the inode bitmap block number is set */
254 bitmap_blk = ext4_inode_bitmap(sb, desc);
255 offset = bitmap_blk - group_first_block;
256 if (!ext4_test_bit(offset, bh->b_data))
257 /* bad block bitmap */
258 goto err_out;
259
260 /* check whether the inode table block number is set */
261 bitmap_blk = ext4_inode_table(sb, desc);
262 offset = bitmap_blk - group_first_block;
263 next_zero_bit = ext4_find_next_zero_bit(bh->b_data,
264 offset + EXT4_SB(sb)->s_itb_per_group,
265 offset);
266 if (next_zero_bit >= offset + EXT4_SB(sb)->s_itb_per_group)
267 /* good bitmap for inode tables */
268 return 1;
269
270err_out:
12062ddd 271 ext4_error(sb, "Invalid block bitmap - block_group = %d, block = %llu",
abcb2947
AK
272 block_group, bitmap_blk);
273 return 0;
274}
ac27a0ec 275/**
574ca174 276 * ext4_read_block_bitmap()
ac27a0ec
DK
277 * @sb: super block
278 * @block_group: given block group
279 *
abcb2947
AK
280 * Read the bitmap for a given block_group,and validate the
281 * bits for block/inode/inode tables are set in the bitmaps
ac27a0ec
DK
282 *
283 * Return buffer_head on success or NULL in case of failure.
284 */
717d50e4 285struct buffer_head *
574ca174 286ext4_read_block_bitmap(struct super_block *sb, ext4_group_t block_group)
ac27a0ec 287{
af5bc92d
TT
288 struct ext4_group_desc *desc;
289 struct buffer_head *bh = NULL;
7c9e69fa 290 ext4_fsblk_t bitmap_blk;
ac27a0ec 291
717d50e4 292 desc = ext4_get_group_desc(sb, block_group, NULL);
ac27a0ec 293 if (!desc)
7c9e69fa
AK
294 return NULL;
295 bitmap_blk = ext4_block_bitmap(sb, desc);
abcb2947
AK
296 bh = sb_getblk(sb, bitmap_blk);
297 if (unlikely(!bh)) {
12062ddd 298 ext4_error(sb, "Cannot read block bitmap - "
a9df9a49 299 "block_group = %u, block_bitmap = %llu",
e29d1cde 300 block_group, bitmap_blk);
abcb2947
AK
301 return NULL;
302 }
2ccb5fb9
AK
303
304 if (bitmap_uptodate(bh))
abcb2947
AK
305 return bh;
306
c806e68f 307 lock_buffer(bh);
2ccb5fb9
AK
308 if (bitmap_uptodate(bh)) {
309 unlock_buffer(bh);
310 return bh;
311 }
955ce5f5 312 ext4_lock_group(sb, block_group);
717d50e4 313 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
abcb2947 314 ext4_init_block_bitmap(sb, bh, block_group, desc);
2ccb5fb9 315 set_bitmap_uptodate(bh);
abcb2947 316 set_buffer_uptodate(bh);
955ce5f5 317 ext4_unlock_group(sb, block_group);
3300beda 318 unlock_buffer(bh);
abcb2947 319 return bh;
717d50e4 320 }
955ce5f5 321 ext4_unlock_group(sb, block_group);
2ccb5fb9
AK
322 if (buffer_uptodate(bh)) {
323 /*
324 * if not uninit if bh is uptodate,
325 * bitmap is also uptodate
326 */
327 set_bitmap_uptodate(bh);
328 unlock_buffer(bh);
329 return bh;
330 }
331 /*
332 * submit the buffer_head for read. We can
333 * safely mark the bitmap as uptodate now.
334 * We do it here so the bitmap uptodate bit
335 * get set with buffer lock held.
336 */
0562e0ba 337 trace_ext4_read_block_bitmap_load(sb, block_group);
2ccb5fb9 338 set_bitmap_uptodate(bh);
abcb2947
AK
339 if (bh_submit_read(bh) < 0) {
340 put_bh(bh);
12062ddd 341 ext4_error(sb, "Cannot read block bitmap - "
a9df9a49 342 "block_group = %u, block_bitmap = %llu",
e29d1cde 343 block_group, bitmap_blk);
abcb2947
AK
344 return NULL;
345 }
519deca0
AK
346 ext4_valid_block_bitmap(sb, desc, block_group, bh);
347 /*
348 * file system mounted not to panic on error,
349 * continue with corrupt bitmap
350 */
ac27a0ec
DK
351 return bh;
352}
ac27a0ec 353
8c3bf8a0
ES
354/**
355 * ext4_has_free_blocks()
356 * @sbi: in-core super block structure.
357 * @nblocks: number of needed blocks
358 *
359 * Check if filesystem has nblocks free & available for allocation.
360 * On success return 1, return 0 on failure.
361 */
55f020db
AH
362static int ext4_has_free_blocks(struct ext4_sb_info *sbi,
363 s64 nblocks, unsigned int flags)
a30d542a 364{
a996031c 365 s64 free_blocks, dirty_blocks, root_blocks;
a30d542a 366 struct percpu_counter *fbc = &sbi->s_freeblocks_counter;
6bc6e63f 367 struct percpu_counter *dbc = &sbi->s_dirtyblocks_counter;
a30d542a 368
6bc6e63f
AK
369 free_blocks = percpu_counter_read_positive(fbc);
370 dirty_blocks = percpu_counter_read_positive(dbc);
a996031c 371 root_blocks = ext4_r_blocks_count(sbi->s_es);
a30d542a 372
6bc6e63f
AK
373 if (free_blocks - (nblocks + root_blocks + dirty_blocks) <
374 EXT4_FREEBLOCKS_WATERMARK) {
02d21168
AM
375 free_blocks = percpu_counter_sum_positive(fbc);
376 dirty_blocks = percpu_counter_sum_positive(dbc);
6bc6e63f
AK
377 }
378 /* Check whether we have space after
a996031c 379 * accounting for current dirty blocks & root reserved blocks.
6bc6e63f 380 */
a996031c
ES
381 if (free_blocks >= ((root_blocks + nblocks) + dirty_blocks))
382 return 1;
a30d542a 383
a996031c 384 /* Hm, nope. Are (enough) root reserved blocks available? */
4c9c544e 385 if (sbi->s_resuid == current_fsuid() ||
a996031c 386 ((sbi->s_resgid != 0) && in_group_p(sbi->s_resgid)) ||
55f020db
AH
387 capable(CAP_SYS_RESOURCE) ||
388 (flags & EXT4_MB_USE_ROOT_BLOCKS)) {
389
a996031c
ES
390 if (free_blocks >= (nblocks + dirty_blocks))
391 return 1;
392 }
393
394 return 0;
a30d542a
AK
395}
396
8c3bf8a0 397int ext4_claim_free_blocks(struct ext4_sb_info *sbi,
55f020db 398 s64 nblocks, unsigned int flags)
ac27a0ec 399{
55f020db 400 if (ext4_has_free_blocks(sbi, nblocks, flags)) {
8c3bf8a0 401 percpu_counter_add(&sbi->s_dirtyblocks_counter, nblocks);
16eb7295 402 return 0;
8c3bf8a0
ES
403 } else
404 return -ENOSPC;
a30d542a 405}
07031431 406
ac27a0ec 407/**
617ba13b 408 * ext4_should_retry_alloc()
ac27a0ec
DK
409 * @sb: super block
410 * @retries number of attemps has been made
411 *
617ba13b 412 * ext4_should_retry_alloc() is called when ENOSPC is returned, and if
ac27a0ec 413 * it is profitable to retry the operation, this function will wait
25985edc 414 * for the current or committing transaction to complete, and then
ac27a0ec
DK
415 * return TRUE.
416 *
417 * if the total number of retries exceed three times, return FALSE.
418 */
617ba13b 419int ext4_should_retry_alloc(struct super_block *sb, int *retries)
ac27a0ec 420{
55f020db 421 if (!ext4_has_free_blocks(EXT4_SB(sb), 1, 0) ||
8f64b32e
ES
422 (*retries)++ > 3 ||
423 !EXT4_SB(sb)->s_journal)
ac27a0ec
DK
424 return 0;
425
426 jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
427
dab291af 428 return jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal);
ac27a0ec
DK
429}
430
654b4908 431/*
d2a17637 432 * ext4_new_meta_blocks() -- allocate block for meta data (indexing) blocks
654b4908
AK
433 *
434 * @handle: handle to this transaction
435 * @inode: file inode
436 * @goal: given target block(filesystem wide)
97df5d15 437 * @count: pointer to total number of blocks needed
654b4908
AK
438 * @errp: error code
439 *
97df5d15 440 * Return 1st allocated block number on success, *count stores total account
d2a17637 441 * error stores in errp pointer
654b4908 442 */
d2a17637 443ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
55f020db
AH
444 ext4_fsblk_t goal, unsigned int flags,
445 unsigned long *count, int *errp)
654b4908 446{
97df5d15 447 struct ext4_allocation_request ar;
d2a17637 448 ext4_fsblk_t ret;
97df5d15
TT
449
450 memset(&ar, 0, sizeof(ar));
451 /* Fill with neighbour allocated blocks */
452 ar.inode = inode;
453 ar.goal = goal;
454 ar.len = count ? *count : 1;
55f020db 455 ar.flags = flags;
97df5d15
TT
456
457 ret = ext4_mb_new_blocks(handle, &ar, errp);
458 if (count)
459 *count = ar.len;
d2a17637 460 /*
72b8ab9d
ES
461 * Account for the allocated meta blocks. We will never
462 * fail EDQUOT for metdata, but we do account for it.
d2a17637 463 */
f2321097
TT
464 if (!(*errp) &&
465 ext4_test_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED)) {
d2a17637 466 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
97df5d15 467 EXT4_I(inode)->i_allocated_meta_blocks += ar.len;
d2a17637 468 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
72b8ab9d 469 dquot_alloc_block_nofail(inode, ar.len);
d2a17637
MC
470 }
471 return ret;
654b4908
AK
472}
473
ac27a0ec 474/**
617ba13b 475 * ext4_count_free_blocks() -- count filesystem free blocks
ac27a0ec
DK
476 * @sb: superblock
477 *
478 * Adds up the number of free blocks from each block group.
479 */
617ba13b 480ext4_fsblk_t ext4_count_free_blocks(struct super_block *sb)
ac27a0ec 481{
617ba13b
MC
482 ext4_fsblk_t desc_count;
483 struct ext4_group_desc *gdp;
fd2d4291 484 ext4_group_t i;
8df9675f 485 ext4_group_t ngroups = ext4_get_groups_count(sb);
617ba13b
MC
486#ifdef EXT4FS_DEBUG
487 struct ext4_super_block *es;
488 ext4_fsblk_t bitmap_count;
498e5f24 489 unsigned int x;
ac27a0ec
DK
490 struct buffer_head *bitmap_bh = NULL;
491
617ba13b 492 es = EXT4_SB(sb)->s_es;
ac27a0ec
DK
493 desc_count = 0;
494 bitmap_count = 0;
495 gdp = NULL;
496
ac27a0ec 497 for (i = 0; i < ngroups; i++) {
617ba13b 498 gdp = ext4_get_group_desc(sb, i, NULL);
ac27a0ec
DK
499 if (!gdp)
500 continue;
9fd9784c 501 desc_count += ext4_free_blks_count(sb, gdp);
ac27a0ec 502 brelse(bitmap_bh);
574ca174 503 bitmap_bh = ext4_read_block_bitmap(sb, i);
ac27a0ec
DK
504 if (bitmap_bh == NULL)
505 continue;
506
617ba13b 507 x = ext4_count_free(bitmap_bh, sb->s_blocksize);
9fd9784c
TLSC
508 printk(KERN_DEBUG "group %u: stored = %d, counted = %u\n",
509 i, ext4_free_blks_count(sb, gdp), x);
ac27a0ec
DK
510 bitmap_count += x;
511 }
512 brelse(bitmap_bh);
4776004f
TT
513 printk(KERN_DEBUG "ext4_count_free_blocks: stored = %llu"
514 ", computed = %llu, %llu\n", ext4_free_blocks_count(es),
515 desc_count, bitmap_count);
ac27a0ec
DK
516 return bitmap_count;
517#else
518 desc_count = 0;
ac27a0ec 519 for (i = 0; i < ngroups; i++) {
617ba13b 520 gdp = ext4_get_group_desc(sb, i, NULL);
ac27a0ec
DK
521 if (!gdp)
522 continue;
560671a0 523 desc_count += ext4_free_blks_count(sb, gdp);
ac27a0ec
DK
524 }
525
526 return desc_count;
527#endif
528}
529
fd2d4291 530static inline int test_root(ext4_group_t a, int b)
ac27a0ec
DK
531{
532 int num = b;
533
534 while (a > num)
535 num *= b;
536 return num == a;
537}
538
fd2d4291 539static int ext4_group_sparse(ext4_group_t group)
ac27a0ec
DK
540{
541 if (group <= 1)
542 return 1;
543 if (!(group & 1))
544 return 0;
545 return (test_root(group, 7) || test_root(group, 5) ||
546 test_root(group, 3));
547}
548
549/**
617ba13b 550 * ext4_bg_has_super - number of blocks used by the superblock in group
ac27a0ec
DK
551 * @sb: superblock for filesystem
552 * @group: group number to check
553 *
554 * Return the number of blocks used by the superblock (primary or backup)
555 * in this group. Currently this will be only 0 or 1.
556 */
fd2d4291 557int ext4_bg_has_super(struct super_block *sb, ext4_group_t group)
ac27a0ec 558{
617ba13b
MC
559 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
560 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) &&
561 !ext4_group_sparse(group))
ac27a0ec
DK
562 return 0;
563 return 1;
564}
565
fd2d4291
AM
566static unsigned long ext4_bg_num_gdb_meta(struct super_block *sb,
567 ext4_group_t group)
ac27a0ec 568{
617ba13b 569 unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
fd2d4291
AM
570 ext4_group_t first = metagroup * EXT4_DESC_PER_BLOCK(sb);
571 ext4_group_t last = first + EXT4_DESC_PER_BLOCK(sb) - 1;
ac27a0ec
DK
572
573 if (group == first || group == first + 1 || group == last)
574 return 1;
575 return 0;
576}
577
fd2d4291
AM
578static unsigned long ext4_bg_num_gdb_nometa(struct super_block *sb,
579 ext4_group_t group)
ac27a0ec 580{
8dadb198
TT
581 if (!ext4_bg_has_super(sb, group))
582 return 0;
583
584 if (EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG))
585 return le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
586 else
587 return EXT4_SB(sb)->s_gdb_count;
ac27a0ec
DK
588}
589
590/**
617ba13b 591 * ext4_bg_num_gdb - number of blocks used by the group table in group
ac27a0ec
DK
592 * @sb: superblock for filesystem
593 * @group: group number to check
594 *
595 * Return the number of blocks used by the group descriptor table
596 * (primary or backup) in this group. In the future there may be a
597 * different number of descriptor blocks in each group.
598 */
fd2d4291 599unsigned long ext4_bg_num_gdb(struct super_block *sb, ext4_group_t group)
ac27a0ec
DK
600{
601 unsigned long first_meta_bg =
617ba13b
MC
602 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
603 unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
ac27a0ec 604
617ba13b 605 if (!EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG) ||
ac27a0ec 606 metagroup < first_meta_bg)
af5bc92d 607 return ext4_bg_num_gdb_nometa(sb, group);
ac27a0ec 608
617ba13b 609 return ext4_bg_num_gdb_meta(sb,group);
ac27a0ec
DK
610
611}
c2ea3fde 612
49f7f9af
TT
613/*
614 * This function returns the number of file system metadata blocks at
615 * the beginning of a block group, including the reserved gdt blocks.
616 */
617static unsigned int num_base_meta_blocks(struct super_block *sb,
618 ext4_group_t block_group)
619{
620 struct ext4_sb_info *sbi = EXT4_SB(sb);
621 int num;
622
623 /* Check for superblock and gdt backups in this group */
624 num = ext4_bg_has_super(sb, block_group);
625
626 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
627 block_group < le32_to_cpu(sbi->s_es->s_first_meta_bg) *
628 sbi->s_desc_per_block) {
629 if (num) {
630 num += ext4_bg_num_gdb(sb, block_group);
631 num += le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks);
632 }
633 } else { /* For META_BG_BLOCK_GROUPS */
634 num += ext4_bg_num_gdb(sb, block_group);
635 }
636 return num;
637}
f86186b4
ES
638/**
639 * ext4_inode_to_goal_block - return a hint for block allocation
640 * @inode: inode for block allocation
641 *
642 * Return the ideal location to start allocating blocks for a
643 * newly created inode.
644 */
645ext4_fsblk_t ext4_inode_to_goal_block(struct inode *inode)
646{
647 struct ext4_inode_info *ei = EXT4_I(inode);
648 ext4_group_t block_group;
649 ext4_grpblk_t colour;
650 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
651 ext4_fsblk_t bg_start;
652 ext4_fsblk_t last_block;
653
654 block_group = ei->i_block_group;
655 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
656 /*
657 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
658 * block groups per flexgroup, reserve the first block
659 * group for directories and special files. Regular
660 * files will start at the second block group. This
661 * tends to speed up directory access and improves
662 * fsck times.
663 */
664 block_group &= ~(flex_size-1);
665 if (S_ISREG(inode->i_mode))
666 block_group++;
667 }
668 bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
669 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
670
671 /*
672 * If we are doing delayed allocation, we don't need take
673 * colour into account.
674 */
675 if (test_opt(inode->i_sb, DELALLOC))
676 return bg_start;
677
678 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
679 colour = (current->pid % 16) *
680 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
681 else
682 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
683 return bg_start + colour;
684}
685