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>
27 * balloc.c contains the blocks allocation and deallocation routines
31 * Calculate the block group number and offset into the block/cluster
32 * allocation bitmap, given a block number
34 void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr,
35 ext4_group_t *blockgrpp, ext4_grpblk_t *offsetp)
37 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
40 blocknr = blocknr - le32_to_cpu(es->s_first_data_block);
41 offset = do_div(blocknr, EXT4_BLOCKS_PER_GROUP(sb)) >>
42 EXT4_SB(sb)->s_cluster_bits;
50 static int ext4_block_in_group(struct super_block *sb, ext4_fsblk_t block,
51 ext4_group_t block_group)
53 ext4_group_t actual_group;
54 ext4_get_group_no_and_offset(sb, block, &actual_group, NULL);
55 if (actual_group == block_group)
60 /* Return the number of clusters used for file system metadata; this
61 * represents the overhead needed by the file system.
63 unsigned ext4_num_overhead_clusters(struct super_block *sb,
64 ext4_group_t block_group,
65 struct ext4_group_desc *gdp)
67 unsigned num_clusters;
68 int block_cluster = -1, inode_cluster = -1, itbl_cluster = -1, i, c;
69 ext4_fsblk_t start = ext4_group_first_block_no(sb, block_group);
70 ext4_fsblk_t itbl_blk;
71 struct ext4_sb_info *sbi = EXT4_SB(sb);
73 /* This is the number of clusters used by the superblock,
74 * block group descriptors, and reserved block group
75 * descriptor blocks */
76 num_clusters = ext4_num_base_meta_clusters(sb, block_group);
79 * For the allocation bitmaps and inode table, we first need
80 * to check to see if the block is in the block group. If it
81 * is, then check to see if the cluster is already accounted
82 * for in the clusters used for the base metadata cluster, or
83 * if we can increment the base metadata cluster to include
84 * that block. Otherwise, we will have to track the cluster
85 * used for the allocation bitmap or inode table explicitly.
86 * Normally all of these blocks are contiguous, so the special
87 * case handling shouldn't be necessary except for *very*
88 * unusual file system layouts.
90 if (ext4_block_in_group(sb, ext4_block_bitmap(sb, gdp), block_group)) {
91 block_cluster = EXT4_B2C(sbi,
92 ext4_block_bitmap(sb, gdp) - start);
93 if (block_cluster < num_clusters)
95 else if (block_cluster == num_clusters) {
101 if (ext4_block_in_group(sb, ext4_inode_bitmap(sb, gdp), block_group)) {
102 inode_cluster = EXT4_B2C(sbi,
103 ext4_inode_bitmap(sb, gdp) - start);
104 if (inode_cluster < num_clusters)
106 else if (inode_cluster == num_clusters) {
112 itbl_blk = ext4_inode_table(sb, gdp);
113 for (i = 0; i < sbi->s_itb_per_group; i++) {
114 if (ext4_block_in_group(sb, itbl_blk + i, block_group)) {
115 c = EXT4_B2C(sbi, itbl_blk + i - start);
116 if ((c < num_clusters) || (c == inode_cluster) ||
117 (c == block_cluster) || (c == itbl_cluster))
119 if (c == num_clusters) {
128 if (block_cluster != -1)
130 if (inode_cluster != -1)
136 static unsigned int num_clusters_in_group(struct super_block *sb,
137 ext4_group_t block_group)
141 if (block_group == ext4_get_groups_count(sb) - 1) {
143 * Even though mke2fs always initializes the first and
144 * last group, just in case some other tool was used,
145 * we need to make sure we calculate the right free
148 blocks = ext4_blocks_count(EXT4_SB(sb)->s_es) -
149 ext4_group_first_block_no(sb, block_group);
151 blocks = EXT4_BLOCKS_PER_GROUP(sb);
152 return EXT4_NUM_B2C(EXT4_SB(sb), blocks);
155 /* Initializes an uninitialized block bitmap */
156 void ext4_init_block_bitmap(struct super_block *sb, struct buffer_head *bh,
157 ext4_group_t block_group,
158 struct ext4_group_desc *gdp)
160 unsigned int bit, bit_max;
161 struct ext4_sb_info *sbi = EXT4_SB(sb);
162 ext4_fsblk_t start, tmp;
165 J_ASSERT_BH(bh, buffer_locked(bh));
167 /* If checksum is bad mark all blocks used to prevent allocation
168 * essentially implementing a per-group read-only flag. */
169 if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
170 ext4_error(sb, "Checksum bad for group %u", block_group);
171 ext4_free_group_clusters_set(sb, gdp, 0);
172 ext4_free_inodes_set(sb, gdp, 0);
173 ext4_itable_unused_set(sb, gdp, 0);
174 memset(bh->b_data, 0xff, sb->s_blocksize);
177 memset(bh->b_data, 0, sb->s_blocksize);
179 bit_max = ext4_num_base_meta_clusters(sb, block_group);
180 for (bit = 0; bit < bit_max; bit++)
181 ext4_set_bit(bit, bh->b_data);
183 start = ext4_group_first_block_no(sb, block_group);
185 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
188 /* Set bits for block and inode bitmaps, and inode table */
189 tmp = ext4_block_bitmap(sb, gdp);
190 if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
191 ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
193 tmp = ext4_inode_bitmap(sb, gdp);
194 if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
195 ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
197 tmp = ext4_inode_table(sb, gdp);
198 for (; tmp < ext4_inode_table(sb, gdp) +
199 sbi->s_itb_per_group; tmp++) {
200 if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
201 ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
205 * Also if the number of blocks within the group is less than
206 * the blocksize * 8 ( which is the size of bitmap ), set rest
207 * of the block bitmap to 1
209 ext4_mark_bitmap_end(num_clusters_in_group(sb, block_group),
210 sb->s_blocksize * 8, bh->b_data);
213 /* Return the number of free blocks in a block group. It is used when
214 * the block bitmap is uninitialized, so we can't just count the bits
216 unsigned ext4_free_clusters_after_init(struct super_block *sb,
217 ext4_group_t block_group,
218 struct ext4_group_desc *gdp)
220 return num_clusters_in_group(sb, block_group) -
221 ext4_num_overhead_clusters(sb, block_group, gdp);
225 * The free blocks are managed by bitmaps. A file system contains several
226 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
227 * block for inodes, N blocks for the inode table and data blocks.
229 * The file system contains group descriptors which are located after the
230 * super block. Each descriptor contains the number of the bitmap block and
231 * the free blocks count in the block. The descriptors are loaded in memory
232 * when a file system is mounted (see ext4_fill_super).
236 * ext4_get_group_desc() -- load group descriptor from disk
238 * @block_group: given block group
239 * @bh: pointer to the buffer head to store the block
242 struct ext4_group_desc * ext4_get_group_desc(struct super_block *sb,
243 ext4_group_t block_group,
244 struct buffer_head **bh)
246 unsigned int group_desc;
248 ext4_group_t ngroups = ext4_get_groups_count(sb);
249 struct ext4_group_desc *desc;
250 struct ext4_sb_info *sbi = EXT4_SB(sb);
252 if (block_group >= ngroups) {
253 ext4_error(sb, "block_group >= groups_count - block_group = %u,"
254 " groups_count = %u", block_group, ngroups);
259 group_desc = block_group >> EXT4_DESC_PER_BLOCK_BITS(sb);
260 offset = block_group & (EXT4_DESC_PER_BLOCK(sb) - 1);
261 if (!sbi->s_group_desc[group_desc]) {
262 ext4_error(sb, "Group descriptor not loaded - "
263 "block_group = %u, group_desc = %u, desc = %u",
264 block_group, group_desc, offset);
268 desc = (struct ext4_group_desc *)(
269 (__u8 *)sbi->s_group_desc[group_desc]->b_data +
270 offset * EXT4_DESC_SIZE(sb));
272 *bh = sbi->s_group_desc[group_desc];
276 static int ext4_valid_block_bitmap(struct super_block *sb,
277 struct ext4_group_desc *desc,
278 unsigned int block_group,
279 struct buffer_head *bh)
281 struct ext4_sb_info *sbi = EXT4_SB(sb);
282 ext4_grpblk_t offset;
283 ext4_grpblk_t next_zero_bit;
284 ext4_fsblk_t bitmap_blk;
285 ext4_fsblk_t group_first_block;
287 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
288 /* with FLEX_BG, the inode/block bitmaps and itable
289 * blocks may not be in the group at all
290 * so the bitmap validation will be skipped for those groups
291 * or it has to also read the block group where the bitmaps
292 * are located to verify they are set.
296 group_first_block = ext4_group_first_block_no(sb, block_group);
298 /* check whether block bitmap block number is set */
299 bitmap_blk = ext4_block_bitmap(sb, desc);
300 offset = bitmap_blk - group_first_block;
301 if (!ext4_test_bit(EXT4_B2C(sbi, offset), bh->b_data))
302 /* bad block bitmap */
305 /* check whether the inode bitmap block number is set */
306 bitmap_blk = ext4_inode_bitmap(sb, desc);
307 offset = bitmap_blk - group_first_block;
308 if (!ext4_test_bit(EXT4_B2C(sbi, offset), bh->b_data))
309 /* bad block bitmap */
312 /* check whether the inode table block number is set */
313 bitmap_blk = ext4_inode_table(sb, desc);
314 offset = bitmap_blk - group_first_block;
315 next_zero_bit = ext4_find_next_zero_bit(bh->b_data,
316 EXT4_B2C(sbi, offset + EXT4_SB(sb)->s_itb_per_group),
317 EXT4_B2C(sbi, offset));
319 EXT4_B2C(sbi, offset + EXT4_SB(sb)->s_itb_per_group))
320 /* good bitmap for inode tables */
324 ext4_error(sb, "Invalid block bitmap - block_group = %d, block = %llu",
325 block_group, bitmap_blk);
329 * ext4_read_block_bitmap()
331 * @block_group: given block group
333 * Read the bitmap for a given block_group,and validate the
334 * bits for block/inode/inode tables are set in the bitmaps
336 * Return buffer_head on success or NULL in case of failure.
339 ext4_read_block_bitmap(struct super_block *sb, ext4_group_t block_group)
341 struct ext4_group_desc *desc;
342 struct buffer_head *bh = NULL;
343 ext4_fsblk_t bitmap_blk;
345 desc = ext4_get_group_desc(sb, block_group, NULL);
348 bitmap_blk = ext4_block_bitmap(sb, desc);
349 bh = sb_getblk(sb, bitmap_blk);
351 ext4_error(sb, "Cannot read block bitmap - "
352 "block_group = %u, block_bitmap = %llu",
353 block_group, bitmap_blk);
357 if (bitmap_uptodate(bh))
361 if (bitmap_uptodate(bh)) {
365 ext4_lock_group(sb, block_group);
366 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
367 ext4_init_block_bitmap(sb, bh, block_group, desc);
368 set_bitmap_uptodate(bh);
369 set_buffer_uptodate(bh);
370 ext4_unlock_group(sb, block_group);
374 ext4_unlock_group(sb, block_group);
375 if (buffer_uptodate(bh)) {
377 * if not uninit if bh is uptodate,
378 * bitmap is also uptodate
380 set_bitmap_uptodate(bh);
385 * submit the buffer_head for read. We can
386 * safely mark the bitmap as uptodate now.
387 * We do it here so the bitmap uptodate bit
388 * get set with buffer lock held.
390 trace_ext4_read_block_bitmap_load(sb, block_group);
391 set_bitmap_uptodate(bh);
392 if (bh_submit_read(bh) < 0) {
394 ext4_error(sb, "Cannot read block bitmap - "
395 "block_group = %u, block_bitmap = %llu",
396 block_group, bitmap_blk);
399 ext4_valid_block_bitmap(sb, desc, block_group, bh);
401 * file system mounted not to panic on error,
402 * continue with corrupt bitmap
408 * ext4_has_free_clusters()
409 * @sbi: in-core super block structure.
410 * @nclusters: number of needed blocks
411 * @flags: flags from ext4_mb_new_blocks()
413 * Check if filesystem has nclusters free & available for allocation.
414 * On success return 1, return 0 on failure.
416 static int ext4_has_free_clusters(struct ext4_sb_info *sbi,
417 s64 nclusters, unsigned int flags)
419 s64 free_clusters, dirty_clusters, root_clusters;
420 struct percpu_counter *fcc = &sbi->s_freeclusters_counter;
421 struct percpu_counter *dcc = &sbi->s_dirtyclusters_counter;
423 free_clusters = percpu_counter_read_positive(fcc);
424 dirty_clusters = percpu_counter_read_positive(dcc);
427 * r_blocks_count should always be multiple of the cluster ratio so
428 * we are safe to do a plane bit shift only.
430 root_clusters = ext4_r_blocks_count(sbi->s_es) >> sbi->s_cluster_bits;
432 if (free_clusters - (nclusters + root_clusters + dirty_clusters) <
433 EXT4_FREECLUSTERS_WATERMARK) {
434 free_clusters = percpu_counter_sum_positive(fcc);
435 dirty_clusters = percpu_counter_sum_positive(dcc);
437 /* Check whether we have space after accounting for current
438 * dirty clusters & root reserved clusters.
440 if (free_clusters >= ((root_clusters + nclusters) + dirty_clusters))
443 /* Hm, nope. Are (enough) root reserved clusters available? */
444 if (sbi->s_resuid == current_fsuid() ||
445 ((sbi->s_resgid != 0) && in_group_p(sbi->s_resgid)) ||
446 capable(CAP_SYS_RESOURCE) ||
447 (flags & EXT4_MB_USE_ROOT_BLOCKS)) {
449 if (free_clusters >= (nclusters + dirty_clusters))
456 int ext4_claim_free_clusters(struct ext4_sb_info *sbi,
457 s64 nclusters, unsigned int flags)
459 if (ext4_has_free_clusters(sbi, nclusters, flags)) {
460 percpu_counter_add(&sbi->s_dirtyclusters_counter, nclusters);
467 * ext4_should_retry_alloc()
469 * @retries number of attemps has been made
471 * ext4_should_retry_alloc() is called when ENOSPC is returned, and if
472 * it is profitable to retry the operation, this function will wait
473 * for the current or committing transaction to complete, and then
476 * if the total number of retries exceed three times, return FALSE.
478 int ext4_should_retry_alloc(struct super_block *sb, int *retries)
480 if (!ext4_has_free_clusters(EXT4_SB(sb), 1, 0) ||
482 !EXT4_SB(sb)->s_journal)
485 jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
487 return jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal);
491 * ext4_new_meta_blocks() -- allocate block for meta data (indexing) blocks
493 * @handle: handle to this transaction
495 * @goal: given target block(filesystem wide)
496 * @count: pointer to total number of clusters needed
499 * Return 1st allocated block number on success, *count stores total account
500 * error stores in errp pointer
502 ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
503 ext4_fsblk_t goal, unsigned int flags,
504 unsigned long *count, int *errp)
506 struct ext4_allocation_request ar;
509 memset(&ar, 0, sizeof(ar));
510 /* Fill with neighbour allocated blocks */
513 ar.len = count ? *count : 1;
516 ret = ext4_mb_new_blocks(handle, &ar, errp);
520 * Account for the allocated meta blocks. We will never
521 * fail EDQUOT for metdata, but we do account for it.
524 ext4_test_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED)) {
525 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
526 EXT4_I(inode)->i_allocated_meta_blocks += ar.len;
527 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
528 dquot_alloc_block_nofail(inode,
529 EXT4_C2B(EXT4_SB(inode->i_sb), ar.len));
535 * ext4_count_free_clusters() -- count filesystem free clusters
538 * Adds up the number of free clusters from each block group.
540 ext4_fsblk_t ext4_count_free_clusters(struct super_block *sb)
542 ext4_fsblk_t desc_count;
543 struct ext4_group_desc *gdp;
545 ext4_group_t ngroups = ext4_get_groups_count(sb);
547 struct ext4_super_block *es;
548 ext4_fsblk_t bitmap_count;
550 struct buffer_head *bitmap_bh = NULL;
552 es = EXT4_SB(sb)->s_es;
557 for (i = 0; i < ngroups; i++) {
558 gdp = ext4_get_group_desc(sb, i, NULL);
561 desc_count += ext4_free_group_clusters(sb, gdp);
563 bitmap_bh = ext4_read_block_bitmap(sb, i);
564 if (bitmap_bh == NULL)
567 x = ext4_count_free(bitmap_bh->b_data,
568 EXT4_BLOCKS_PER_GROUP(sb) / 8);
569 printk(KERN_DEBUG "group %u: stored = %d, counted = %u\n",
570 i, ext4_free_group_clusters(sb, gdp), x);
574 printk(KERN_DEBUG "ext4_count_free_clusters: stored = %llu"
575 ", computed = %llu, %llu\n",
576 EXT4_NUM_B2C(EXT4_SB(sb), ext4_free_blocks_count(es)),
577 desc_count, bitmap_count);
581 for (i = 0; i < ngroups; i++) {
582 gdp = ext4_get_group_desc(sb, i, NULL);
585 desc_count += ext4_free_group_clusters(sb, gdp);
592 static inline int test_root(ext4_group_t a, int b)
601 static int ext4_group_sparse(ext4_group_t group)
607 return (test_root(group, 7) || test_root(group, 5) ||
608 test_root(group, 3));
612 * ext4_bg_has_super - number of blocks used by the superblock in group
613 * @sb: superblock for filesystem
614 * @group: group number to check
616 * Return the number of blocks used by the superblock (primary or backup)
617 * in this group. Currently this will be only 0 or 1.
619 int ext4_bg_has_super(struct super_block *sb, ext4_group_t group)
621 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
622 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) &&
623 !ext4_group_sparse(group))
628 static unsigned long ext4_bg_num_gdb_meta(struct super_block *sb,
631 unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
632 ext4_group_t first = metagroup * EXT4_DESC_PER_BLOCK(sb);
633 ext4_group_t last = first + EXT4_DESC_PER_BLOCK(sb) - 1;
635 if (group == first || group == first + 1 || group == last)
640 static unsigned long ext4_bg_num_gdb_nometa(struct super_block *sb,
643 if (!ext4_bg_has_super(sb, group))
646 if (EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG))
647 return le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
649 return EXT4_SB(sb)->s_gdb_count;
653 * ext4_bg_num_gdb - number of blocks used by the group table in group
654 * @sb: superblock for filesystem
655 * @group: group number to check
657 * Return the number of blocks used by the group descriptor table
658 * (primary or backup) in this group. In the future there may be a
659 * different number of descriptor blocks in each group.
661 unsigned long ext4_bg_num_gdb(struct super_block *sb, ext4_group_t group)
663 unsigned long first_meta_bg =
664 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
665 unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
667 if (!EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG) ||
668 metagroup < first_meta_bg)
669 return ext4_bg_num_gdb_nometa(sb, group);
671 return ext4_bg_num_gdb_meta(sb,group);
676 * This function returns the number of file system metadata clusters at
677 * the beginning of a block group, including the reserved gdt blocks.
679 unsigned ext4_num_base_meta_clusters(struct super_block *sb,
680 ext4_group_t block_group)
682 struct ext4_sb_info *sbi = EXT4_SB(sb);
685 /* Check for superblock and gdt backups in this group */
686 num = ext4_bg_has_super(sb, block_group);
688 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
689 block_group < le32_to_cpu(sbi->s_es->s_first_meta_bg) *
690 sbi->s_desc_per_block) {
692 num += ext4_bg_num_gdb(sb, block_group);
693 num += le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks);
695 } else { /* For META_BG_BLOCK_GROUPS */
696 num += ext4_bg_num_gdb(sb, block_group);
698 return EXT4_NUM_B2C(sbi, num);
701 * ext4_inode_to_goal_block - return a hint for block allocation
702 * @inode: inode for block allocation
704 * Return the ideal location to start allocating blocks for a
705 * newly created inode.
707 ext4_fsblk_t ext4_inode_to_goal_block(struct inode *inode)
709 struct ext4_inode_info *ei = EXT4_I(inode);
710 ext4_group_t block_group;
711 ext4_grpblk_t colour;
712 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
713 ext4_fsblk_t bg_start;
714 ext4_fsblk_t last_block;
716 block_group = ei->i_block_group;
717 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
719 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
720 * block groups per flexgroup, reserve the first block
721 * group for directories and special files. Regular
722 * files will start at the second block group. This
723 * tends to speed up directory access and improves
726 block_group &= ~(flex_size-1);
727 if (S_ISREG(inode->i_mode))
730 bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
731 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
734 * If we are doing delayed allocation, we don't need take
735 * colour into account.
737 if (test_opt(inode->i_sb, DELALLOC))
740 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
741 colour = (current->pid % 16) *
742 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
744 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
745 return bg_start + colour;