2 * linux/fs/ext4/balloc.c
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)
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
14 #include <linux/time.h>
15 #include <linux/capability.h>
17 #include <linux/jbd2.h>
18 #include <linux/quotaops.h>
19 #include <linux/buffer_head.h>
21 #include "ext4_jbd2.h"
24 #include <trace/events/ext4.h>
26 static unsigned int num_base_meta_blocks(struct super_block *sb,
27 ext4_group_t block_group);
30 * balloc.c contains the blocks allocation and deallocation routines
34 * Calculate the block group number and offset, given a block number
36 void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr,
37 ext4_group_t *blockgrpp, ext4_grpblk_t *offsetp)
39 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
42 blocknr = blocknr - le32_to_cpu(es->s_first_data_block);
43 offset = do_div(blocknr, EXT4_BLOCKS_PER_GROUP(sb));
51 static int ext4_block_in_group(struct super_block *sb, ext4_fsblk_t block,
52 ext4_group_t block_group)
54 ext4_group_t actual_group;
55 ext4_get_group_no_and_offset(sb, block, &actual_group, NULL);
56 if (actual_group == block_group)
61 static int ext4_group_used_meta_blocks(struct super_block *sb,
62 ext4_group_t block_group,
63 struct ext4_group_desc *gdp)
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;
70 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
71 if (!ext4_block_in_group(sb, ext4_block_bitmap(sb, gdp),
75 if (!ext4_block_in_group(sb, ext4_inode_bitmap(sb, gdp),
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))
89 static unsigned int num_blocks_in_group(struct super_block *sb,
90 ext4_group_t block_group)
92 if (block_group == ext4_get_groups_count(sb) - 1) {
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
99 return ext4_blocks_count(EXT4_SB(sb)->s_es) -
100 ext4_group_first_block_no(sb, block_group);
102 return EXT4_BLOCKS_PER_GROUP(sb);
105 /* Initializes an uninitialized block bitmap */
106 void ext4_init_block_bitmap(struct super_block *sb, struct buffer_head *bh,
107 ext4_group_t block_group,
108 struct ext4_group_desc *gdp)
110 unsigned int bit, bit_max = num_base_meta_blocks(sb, block_group);
111 struct ext4_sb_info *sbi = EXT4_SB(sb);
112 ext4_fsblk_t start, tmp;
115 J_ASSERT_BH(bh, buffer_locked(bh));
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);
127 memset(bh->b_data, 0, sb->s_blocksize);
129 for (bit = 0; bit < bit_max; bit++)
130 ext4_set_bit(bit, bh->b_data);
132 start = ext4_group_first_block_no(sb, block_group);
134 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
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);
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);
146 tmp = ext4_inode_table(sb, gdp);
147 for (; tmp < ext4_inode_table(sb, gdp) +
148 sbi->s_itb_per_group; tmp++) {
149 if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
150 ext4_set_bit(tmp - start, bh->b_data);
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
157 ext4_mark_bitmap_end(num_blocks_in_group(sb, block_group),
158 sb->s_blocksize * 8, bh->b_data);
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
164 unsigned ext4_free_blocks_after_init(struct super_block *sb,
165 ext4_group_t block_group,
166 struct ext4_group_desc *gdp)
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);
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.
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
181 * when a file system is mounted (see ext4_fill_super).
185 * ext4_get_group_desc() -- load group descriptor from disk
187 * @block_group: given block group
188 * @bh: pointer to the buffer head to store the block
191 struct ext4_group_desc * ext4_get_group_desc(struct super_block *sb,
192 ext4_group_t block_group,
193 struct buffer_head **bh)
195 unsigned int group_desc;
197 ext4_group_t ngroups = ext4_get_groups_count(sb);
198 struct ext4_group_desc *desc;
199 struct ext4_sb_info *sbi = EXT4_SB(sb);
201 if (block_group >= ngroups) {
202 ext4_error(sb, "block_group >= groups_count - block_group = %u,"
203 " groups_count = %u", block_group, ngroups);
208 group_desc = block_group >> EXT4_DESC_PER_BLOCK_BITS(sb);
209 offset = block_group & (EXT4_DESC_PER_BLOCK(sb) - 1);
210 if (!sbi->s_group_desc[group_desc]) {
211 ext4_error(sb, "Group descriptor not loaded - "
212 "block_group = %u, group_desc = %u, desc = %u",
213 block_group, group_desc, offset);
217 desc = (struct ext4_group_desc *)(
218 (__u8 *)sbi->s_group_desc[group_desc]->b_data +
219 offset * EXT4_DESC_SIZE(sb));
221 *bh = sbi->s_group_desc[group_desc];
225 static 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)
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;
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.
244 group_first_block = ext4_group_first_block_no(sb, block_group);
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 */
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 */
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,
266 if (next_zero_bit >= offset + EXT4_SB(sb)->s_itb_per_group)
267 /* good bitmap for inode tables */
271 ext4_error(sb, "Invalid block bitmap - block_group = %d, block = %llu",
272 block_group, bitmap_blk);
276 * ext4_read_block_bitmap()
278 * @block_group: given block group
280 * Read the bitmap for a given block_group,and validate the
281 * bits for block/inode/inode tables are set in the bitmaps
283 * Return buffer_head on success or NULL in case of failure.
286 ext4_read_block_bitmap(struct super_block *sb, ext4_group_t block_group)
288 struct ext4_group_desc *desc;
289 struct buffer_head *bh = NULL;
290 ext4_fsblk_t bitmap_blk;
292 desc = ext4_get_group_desc(sb, block_group, NULL);
295 bitmap_blk = ext4_block_bitmap(sb, desc);
296 bh = sb_getblk(sb, bitmap_blk);
298 ext4_error(sb, "Cannot read block bitmap - "
299 "block_group = %u, block_bitmap = %llu",
300 block_group, bitmap_blk);
304 if (bitmap_uptodate(bh))
308 if (bitmap_uptodate(bh)) {
312 ext4_lock_group(sb, block_group);
313 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
314 ext4_init_block_bitmap(sb, bh, block_group, desc);
315 set_bitmap_uptodate(bh);
316 set_buffer_uptodate(bh);
317 ext4_unlock_group(sb, block_group);
321 ext4_unlock_group(sb, block_group);
322 if (buffer_uptodate(bh)) {
324 * if not uninit if bh is uptodate,
325 * bitmap is also uptodate
327 set_bitmap_uptodate(bh);
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.
337 trace_ext4_read_block_bitmap_load(sb, block_group);
338 set_bitmap_uptodate(bh);
339 if (bh_submit_read(bh) < 0) {
341 ext4_error(sb, "Cannot read block bitmap - "
342 "block_group = %u, block_bitmap = %llu",
343 block_group, bitmap_blk);
346 ext4_valid_block_bitmap(sb, desc, block_group, bh);
348 * file system mounted not to panic on error,
349 * continue with corrupt bitmap
355 * ext4_has_free_blocks()
356 * @sbi: in-core super block structure.
357 * @nblocks: number of needed blocks
359 * Check if filesystem has nblocks free & available for allocation.
360 * On success return 1, return 0 on failure.
362 static int ext4_has_free_blocks(struct ext4_sb_info *sbi,
363 s64 nblocks, unsigned int flags)
365 s64 free_blocks, dirty_blocks, root_blocks;
366 struct percpu_counter *fbc = &sbi->s_freeblocks_counter;
367 struct percpu_counter *dbc = &sbi->s_dirtyblocks_counter;
369 free_blocks = percpu_counter_read_positive(fbc);
370 dirty_blocks = percpu_counter_read_positive(dbc);
371 root_blocks = ext4_r_blocks_count(sbi->s_es);
373 if (free_blocks - (nblocks + root_blocks + dirty_blocks) <
374 EXT4_FREEBLOCKS_WATERMARK) {
375 free_blocks = percpu_counter_sum_positive(fbc);
376 dirty_blocks = percpu_counter_sum_positive(dbc);
378 /* Check whether we have space after
379 * accounting for current dirty blocks & root reserved blocks.
381 if (free_blocks >= ((root_blocks + nblocks) + dirty_blocks))
384 /* Hm, nope. Are (enough) root reserved blocks available? */
385 if (sbi->s_resuid == current_fsuid() ||
386 ((sbi->s_resgid != 0) && in_group_p(sbi->s_resgid)) ||
387 capable(CAP_SYS_RESOURCE) ||
388 (flags & EXT4_MB_USE_ROOT_BLOCKS)) {
390 if (free_blocks >= (nblocks + dirty_blocks))
397 int ext4_claim_free_blocks(struct ext4_sb_info *sbi,
398 s64 nblocks, unsigned int flags)
400 if (ext4_has_free_blocks(sbi, nblocks, flags)) {
401 percpu_counter_add(&sbi->s_dirtyblocks_counter, nblocks);
408 * ext4_should_retry_alloc()
410 * @retries number of attemps has been made
412 * ext4_should_retry_alloc() is called when ENOSPC is returned, and if
413 * it is profitable to retry the operation, this function will wait
414 * for the current or committing transaction to complete, and then
417 * if the total number of retries exceed three times, return FALSE.
419 int ext4_should_retry_alloc(struct super_block *sb, int *retries)
421 if (!ext4_has_free_blocks(EXT4_SB(sb), 1, 0) ||
423 !EXT4_SB(sb)->s_journal)
426 jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
428 return jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal);
432 * ext4_new_meta_blocks() -- allocate block for meta data (indexing) blocks
434 * @handle: handle to this transaction
436 * @goal: given target block(filesystem wide)
437 * @count: pointer to total number of blocks needed
440 * Return 1st allocated block number on success, *count stores total account
441 * error stores in errp pointer
443 ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
444 ext4_fsblk_t goal, unsigned int flags,
445 unsigned long *count, int *errp)
447 struct ext4_allocation_request ar;
450 memset(&ar, 0, sizeof(ar));
451 /* Fill with neighbour allocated blocks */
454 ar.len = count ? *count : 1;
457 ret = ext4_mb_new_blocks(handle, &ar, errp);
461 * Account for the allocated meta blocks. We will never
462 * fail EDQUOT for metdata, but we do account for it.
465 ext4_test_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED)) {
466 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
467 EXT4_I(inode)->i_allocated_meta_blocks += ar.len;
468 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
469 dquot_alloc_block_nofail(inode, ar.len);
475 * ext4_count_free_blocks() -- count filesystem free blocks
478 * Adds up the number of free blocks from each block group.
480 ext4_fsblk_t ext4_count_free_blocks(struct super_block *sb)
482 ext4_fsblk_t desc_count;
483 struct ext4_group_desc *gdp;
485 ext4_group_t ngroups = ext4_get_groups_count(sb);
487 struct ext4_super_block *es;
488 ext4_fsblk_t bitmap_count;
490 struct buffer_head *bitmap_bh = NULL;
492 es = EXT4_SB(sb)->s_es;
497 for (i = 0; i < ngroups; i++) {
498 gdp = ext4_get_group_desc(sb, i, NULL);
501 desc_count += ext4_free_blks_count(sb, gdp);
503 bitmap_bh = ext4_read_block_bitmap(sb, i);
504 if (bitmap_bh == NULL)
507 x = ext4_count_free(bitmap_bh, sb->s_blocksize);
508 printk(KERN_DEBUG "group %u: stored = %d, counted = %u\n",
509 i, ext4_free_blks_count(sb, gdp), x);
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);
519 for (i = 0; i < ngroups; i++) {
520 gdp = ext4_get_group_desc(sb, i, NULL);
523 desc_count += ext4_free_blks_count(sb, gdp);
530 static inline int test_root(ext4_group_t a, int b)
539 static int ext4_group_sparse(ext4_group_t group)
545 return (test_root(group, 7) || test_root(group, 5) ||
546 test_root(group, 3));
550 * ext4_bg_has_super - number of blocks used by the superblock in group
551 * @sb: superblock for filesystem
552 * @group: group number to check
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.
557 int ext4_bg_has_super(struct super_block *sb, ext4_group_t group)
559 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
560 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) &&
561 !ext4_group_sparse(group))
566 static unsigned long ext4_bg_num_gdb_meta(struct super_block *sb,
569 unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
570 ext4_group_t first = metagroup * EXT4_DESC_PER_BLOCK(sb);
571 ext4_group_t last = first + EXT4_DESC_PER_BLOCK(sb) - 1;
573 if (group == first || group == first + 1 || group == last)
578 static unsigned long ext4_bg_num_gdb_nometa(struct super_block *sb,
581 if (!ext4_bg_has_super(sb, group))
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);
587 return EXT4_SB(sb)->s_gdb_count;
591 * ext4_bg_num_gdb - number of blocks used by the group table in group
592 * @sb: superblock for filesystem
593 * @group: group number to check
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.
599 unsigned long ext4_bg_num_gdb(struct super_block *sb, ext4_group_t group)
601 unsigned long first_meta_bg =
602 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
603 unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
605 if (!EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG) ||
606 metagroup < first_meta_bg)
607 return ext4_bg_num_gdb_nometa(sb, group);
609 return ext4_bg_num_gdb_meta(sb,group);
614 * This function returns the number of file system metadata blocks at
615 * the beginning of a block group, including the reserved gdt blocks.
617 static unsigned int num_base_meta_blocks(struct super_block *sb,
618 ext4_group_t block_group)
620 struct ext4_sb_info *sbi = EXT4_SB(sb);
623 /* Check for superblock and gdt backups in this group */
624 num = ext4_bg_has_super(sb, block_group);
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) {
630 num += ext4_bg_num_gdb(sb, block_group);
631 num += le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks);
633 } else { /* For META_BG_BLOCK_GROUPS */
634 num += ext4_bg_num_gdb(sb, block_group);
639 * ext4_inode_to_goal_block - return a hint for block allocation
640 * @inode: inode for block allocation
642 * Return the ideal location to start allocating blocks for a
643 * newly created inode.
645 ext4_fsblk_t ext4_inode_to_goal_block(struct inode *inode)
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;
654 block_group = ei->i_block_group;
655 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
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
664 block_group &= ~(flex_size-1);
665 if (S_ISREG(inode->i_mode))
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;
672 * If we are doing delayed allocation, we don't need take
673 * colour into account.
675 if (test_opt(inode->i_sb, DELALLOC))
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);
682 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
683 return bg_start + colour;