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"
25 * balloc.c contains the blocks allocation and deallocation routines
29 * Calculate the block group number and offset, given a block number
31 void ext4_get_group_no_and_offset(struct super_block
*sb
, ext4_fsblk_t blocknr
,
32 ext4_group_t
*blockgrpp
, ext4_grpblk_t
*offsetp
)
34 struct ext4_super_block
*es
= EXT4_SB(sb
)->s_es
;
37 blocknr
= blocknr
- le32_to_cpu(es
->s_first_data_block
);
38 offset
= do_div(blocknr
, EXT4_BLOCKS_PER_GROUP(sb
));
46 static int ext4_block_in_group(struct super_block
*sb
, ext4_fsblk_t block
,
47 ext4_group_t block_group
)
49 ext4_group_t actual_group
;
50 ext4_get_group_no_and_offset(sb
, block
, &actual_group
, NULL
);
51 if (actual_group
== block_group
)
56 static int ext4_group_used_meta_blocks(struct super_block
*sb
,
57 ext4_group_t block_group
)
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;
64 if (EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_FLEX_BG
)) {
65 struct ext4_group_desc
*gdp
;
66 struct buffer_head
*bh
;
68 gdp
= ext4_get_group_desc(sb
, block_group
, &bh
);
69 if (!ext4_block_in_group(sb
, ext4_block_bitmap(sb
, gdp
),
73 if (!ext4_block_in_group(sb
, ext4_inode_bitmap(sb
, gdp
),
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
))
87 /* Initializes an uninitialized block bitmap if given, and returns the
88 * number of blocks free in the group. */
89 unsigned ext4_init_block_bitmap(struct super_block
*sb
, struct buffer_head
*bh
,
90 ext4_group_t block_group
, struct ext4_group_desc
*gdp
)
93 unsigned free_blocks
, group_blocks
;
94 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
97 J_ASSERT_BH(bh
, buffer_locked(bh
));
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
)) {
102 ext4_error(sb
, __func__
,
103 "Checksum bad for group %lu\n", block_group
);
104 gdp
->bg_free_blocks_count
= 0;
105 gdp
->bg_free_inodes_count
= 0;
106 gdp
->bg_itable_unused
= 0;
107 memset(bh
->b_data
, 0xff, sb
->s_blocksize
);
110 memset(bh
->b_data
, 0, sb
->s_blocksize
);
113 /* Check for superblock and gdt backups in this group */
114 bit_max
= ext4_bg_has_super(sb
, block_group
);
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
) {
120 bit_max
+= ext4_bg_num_gdb(sb
, block_group
);
122 le16_to_cpu(sbi
->s_es
->s_reserved_gdt_blocks
);
124 } else { /* For META_BG_BLOCK_GROUPS */
125 bit_max
+= ext4_bg_num_gdb(sb
, block_group
);
128 if (block_group
== sbi
->s_groups_count
- 1) {
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
134 group_blocks
= ext4_blocks_count(sbi
->s_es
) -
135 le32_to_cpu(sbi
->s_es
->s_first_data_block
) -
136 (EXT4_BLOCKS_PER_GROUP(sb
) * (sbi
->s_groups_count
- 1));
138 group_blocks
= EXT4_BLOCKS_PER_GROUP(sb
);
141 free_blocks
= group_blocks
- bit_max
;
144 ext4_fsblk_t start
, tmp
;
147 for (bit
= 0; bit
< bit_max
; bit
++)
148 ext4_set_bit(bit
, bh
->b_data
);
150 start
= ext4_group_first_block_no(sb
, block_group
);
152 if (EXT4_HAS_INCOMPAT_FEATURE(sb
,
153 EXT4_FEATURE_INCOMPAT_FLEX_BG
))
156 /* Set bits for block and inode bitmaps, and inode table */
157 tmp
= ext4_block_bitmap(sb
, gdp
);
158 if (!flex_bg
|| ext4_block_in_group(sb
, tmp
, block_group
))
159 ext4_set_bit(tmp
- start
, bh
->b_data
);
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
);
165 tmp
= ext4_inode_table(sb
, gdp
);
166 for (; tmp
< ext4_inode_table(sb
, gdp
) +
167 sbi
->s_itb_per_group
; tmp
++) {
169 ext4_block_in_group(sb
, tmp
, block_group
))
170 ext4_set_bit(tmp
- start
, bh
->b_data
);
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
177 mark_bitmap_end(group_blocks
, sb
->s_blocksize
* 8, bh
->b_data
);
179 return free_blocks
- ext4_group_used_meta_blocks(sb
, block_group
);
184 * The free blocks are managed by bitmaps. A file system contains several
185 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
186 * block for inodes, N blocks for the inode table and data blocks.
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
191 * when a file system is mounted (see ext4_fill_super).
195 #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
198 * ext4_get_group_desc() -- load group descriptor from disk
200 * @block_group: given block group
201 * @bh: pointer to the buffer head to store the block
204 struct ext4_group_desc
* ext4_get_group_desc(struct super_block
*sb
,
205 ext4_group_t block_group
,
206 struct buffer_head
**bh
)
208 unsigned long group_desc
;
209 unsigned long offset
;
210 struct ext4_group_desc
*desc
;
211 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
213 if (block_group
>= sbi
->s_groups_count
) {
214 ext4_error(sb
, "ext4_get_group_desc",
215 "block_group >= groups_count - "
216 "block_group = %lu, groups_count = %lu",
217 block_group
, sbi
->s_groups_count
);
223 group_desc
= block_group
>> EXT4_DESC_PER_BLOCK_BITS(sb
);
224 offset
= block_group
& (EXT4_DESC_PER_BLOCK(sb
) - 1);
225 if (!sbi
->s_group_desc
[group_desc
]) {
226 ext4_error(sb
, "ext4_get_group_desc",
227 "Group descriptor not loaded - "
228 "block_group = %lu, group_desc = %lu, desc = %lu",
229 block_group
, group_desc
, offset
);
233 desc
= (struct ext4_group_desc
*)(
234 (__u8
*)sbi
->s_group_desc
[group_desc
]->b_data
+
235 offset
* EXT4_DESC_SIZE(sb
));
237 *bh
= sbi
->s_group_desc
[group_desc
];
241 static int ext4_valid_block_bitmap(struct super_block
*sb
,
242 struct ext4_group_desc
*desc
,
243 unsigned int block_group
,
244 struct buffer_head
*bh
)
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
;
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.
260 group_first_block
= ext4_group_first_block_no(sb
, block_group
);
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 */
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 */
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
,
282 if (next_zero_bit
>= offset
+ EXT4_SB(sb
)->s_itb_per_group
)
283 /* good bitmap for inode tables */
287 ext4_error(sb
, __func__
,
288 "Invalid block bitmap - "
289 "block_group = %d, block = %llu",
290 block_group
, bitmap_blk
);
294 * ext4_read_block_bitmap()
296 * @block_group: given block group
298 * Read the bitmap for a given block_group,and validate the
299 * bits for block/inode/inode tables are set in the bitmaps
301 * Return buffer_head on success or NULL in case of failure.
304 ext4_read_block_bitmap(struct super_block
*sb
, ext4_group_t block_group
)
306 struct ext4_group_desc
*desc
;
307 struct buffer_head
*bh
= NULL
;
308 ext4_fsblk_t bitmap_blk
;
310 desc
= ext4_get_group_desc(sb
, block_group
, NULL
);
313 bitmap_blk
= ext4_block_bitmap(sb
, desc
);
314 bh
= sb_getblk(sb
, bitmap_blk
);
316 ext4_error(sb
, __func__
,
317 "Cannot read block bitmap - "
318 "block_group = %lu, block_bitmap = %llu",
319 block_group
, bitmap_blk
);
322 if (buffer_uptodate(bh
) &&
323 !(desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)))
327 spin_lock(sb_bgl_lock(EXT4_SB(sb
), block_group
));
328 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
);
332 spin_unlock(sb_bgl_lock(EXT4_SB(sb
), block_group
));
335 spin_unlock(sb_bgl_lock(EXT4_SB(sb
), block_group
));
336 if (bh_submit_read(bh
) < 0) {
338 ext4_error(sb
, __func__
,
339 "Cannot read block bitmap - "
340 "block_group = %lu, block_bitmap = %llu",
341 block_group
, bitmap_blk
);
344 ext4_valid_block_bitmap(sb
, desc
, block_group
, bh
);
346 * file system mounted not to panic on error,
347 * continue with corrupt bitmap
353 * ext4_free_blocks_sb() -- Free given blocks and update quota
354 * @handle: handle to this transaction
356 * @block: start physcial block to free
357 * @count: number of blocks to free
358 * @pdquot_freed_blocks: pointer to quota
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.
367 void ext4_free_blocks_sb(handle_t
*handle
, struct super_block
*sb
,
368 ext4_fsblk_t block
, unsigned long count
,
369 unsigned long *pdquot_freed_blocks
)
371 struct buffer_head
*bitmap_bh
= NULL
;
372 struct buffer_head
*gd_bh
;
373 ext4_group_t block_group
;
376 unsigned long overflow
;
377 struct ext4_group_desc
*desc
;
378 struct ext4_super_block
*es
;
379 struct ext4_sb_info
*sbi
;
381 ext4_grpblk_t group_freed
;
383 *pdquot_freed_blocks
= 0;
386 if (block
< le32_to_cpu(es
->s_first_data_block
) ||
387 block
+ count
< block
||
388 block
+ count
> ext4_blocks_count(es
)) {
389 ext4_error(sb
, "ext4_free_blocks",
390 "Freeing blocks not in datazone - "
391 "block = %llu, count = %lu", block
, count
);
395 ext4_debug("freeing block(s) %llu-%llu\n", block
, block
+ count
- 1);
399 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
401 * Check to see if we are freeing blocks across a group
404 if (bit
+ count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
405 overflow
= bit
+ count
- EXT4_BLOCKS_PER_GROUP(sb
);
409 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
412 desc
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
416 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
),
420 sbi
->s_itb_per_group
)) {
421 ext4_error(sb
, "ext4_free_blocks",
422 "Freeing blocks in system zones - "
423 "Block = %llu, count = %lu",
429 * We are about to start releasing blocks in the bitmap,
430 * so we need undo access.
432 /* @@@ check errors */
433 BUFFER_TRACE(bitmap_bh
, "getting undo access");
434 err
= ext4_journal_get_undo_access(handle
, bitmap_bh
);
439 * We are about to modify some metadata. Call the journal APIs
440 * to unshare ->b_data if a currently-committing transaction is
443 BUFFER_TRACE(gd_bh
, "get_write_access");
444 err
= ext4_journal_get_write_access(handle
, gd_bh
);
448 jbd_lock_bh_state(bitmap_bh
);
450 for (i
= 0, group_freed
= 0; i
< count
; i
++) {
452 * An HJ special. This is expensive...
454 #ifdef CONFIG_JBD2_DEBUG
455 jbd_unlock_bh_state(bitmap_bh
);
457 struct buffer_head
*debug_bh
;
458 debug_bh
= sb_find_get_block(sb
, block
+ i
);
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");
468 jbd_lock_bh_state(bitmap_bh
);
470 if (need_resched()) {
471 jbd_unlock_bh_state(bitmap_bh
);
473 jbd_lock_bh_state(bitmap_bh
);
475 /* @@@ This prevents newly-allocated data from being
476 * freed and then reallocated within the same
479 * Ideally we would want to allow that to happen, but to
480 * do so requires making jbd2_journal_forget() capable of
481 * revoking the queued write of a data block, which
482 * implies blocking on the journal lock. *forget()
483 * cannot block due to truncate races.
485 * Eventually we can fix this by making jbd2_journal_forget()
486 * 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
493 BUFFER_TRACE(bitmap_bh
, "set in b_committed_data");
494 J_ASSERT_BH(bitmap_bh
,
495 bh2jh(bitmap_bh
)->b_committed_data
!= NULL
);
496 ext4_set_bit_atomic(sb_bgl_lock(sbi
, block_group
), bit
+ i
,
497 bh2jh(bitmap_bh
)->b_committed_data
);
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.
504 BUFFER_TRACE(bitmap_bh
, "clear bit");
505 if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi
, block_group
),
506 bit
+ i
, bitmap_bh
->b_data
)) {
507 jbd_unlock_bh_state(bitmap_bh
);
508 ext4_error(sb
, __func__
,
509 "bit already cleared for block %llu",
510 (ext4_fsblk_t
)(block
+ i
));
511 jbd_lock_bh_state(bitmap_bh
);
512 BUFFER_TRACE(bitmap_bh
, "bit already cleared");
517 jbd_unlock_bh_state(bitmap_bh
);
519 spin_lock(sb_bgl_lock(sbi
, block_group
));
520 le16_add_cpu(&desc
->bg_free_blocks_count
, group_freed
);
521 desc
->bg_checksum
= ext4_group_desc_csum(sbi
, block_group
, desc
);
522 spin_unlock(sb_bgl_lock(sbi
, block_group
));
523 percpu_counter_add(&sbi
->s_freeblocks_counter
, count
);
525 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
));
532 /* We dirtied the bitmap block */
533 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
534 err
= ext4_journal_dirty_metadata(handle
, bitmap_bh
);
536 /* And the group descriptor block */
537 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
538 ret
= ext4_journal_dirty_metadata(handle
, gd_bh
);
540 *pdquot_freed_blocks
+= group_freed
;
542 if (overflow
&& !err
) {
550 ext4_std_error(sb
, err
);
555 * ext4_free_blocks() -- Free given blocks and update quota
556 * @handle: handle for this transaction
558 * @block: start physical block to free
559 * @count: number of blocks to count
560 * @metadata: Are these metadata blocks
562 void ext4_free_blocks(handle_t
*handle
, struct inode
*inode
,
563 ext4_fsblk_t block
, unsigned long count
,
566 struct super_block
*sb
;
567 unsigned long dquot_freed_blocks
;
569 /* this isn't the right place to decide whether block is metadata
570 * inode.c/extents.c knows better, but for safety ... */
571 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
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.
580 if (metadata
== 0 && !ext4_should_writeback_data(inode
))
585 ext4_mb_free_blocks(handle
, inode
, block
, count
,
586 metadata
, &dquot_freed_blocks
);
587 if (dquot_freed_blocks
)
588 DQUOT_FREE_BLOCK(inode
, dquot_freed_blocks
);
592 int ext4_claim_free_blocks(struct ext4_sb_info
*sbi
,
595 s64 free_blocks
, dirty_blocks
;
597 struct percpu_counter
*fbc
= &sbi
->s_freeblocks_counter
;
598 struct percpu_counter
*dbc
= &sbi
->s_dirtyblocks_counter
;
600 free_blocks
= percpu_counter_read_positive(fbc
);
601 dirty_blocks
= percpu_counter_read_positive(dbc
);
603 if (!capable(CAP_SYS_RESOURCE
) &&
604 sbi
->s_resuid
!= current
->fsuid
&&
605 (sbi
->s_resgid
== 0 || !in_group_p(sbi
->s_resgid
)))
606 root_blocks
= ext4_r_blocks_count(sbi
->s_es
);
608 if (free_blocks
- (nblocks
+ root_blocks
+ dirty_blocks
) <
609 EXT4_FREEBLOCKS_WATERMARK
) {
610 free_blocks
= percpu_counter_sum(fbc
);
611 dirty_blocks
= percpu_counter_sum(dbc
);
612 if (dirty_blocks
< 0) {
613 printk(KERN_CRIT
"Dirty block accounting "
618 /* Check whether we have space after
619 * accounting for current dirty blocks
621 if (free_blocks
< ((root_blocks
+ nblocks
) + dirty_blocks
))
622 /* we don't have free space */
625 /* Add the blocks to nblocks */
626 percpu_counter_add(dbc
, nblocks
);
631 * ext4_has_free_blocks()
632 * @sbi: in-core super block structure.
633 * @nblocks: number of neeed blocks
635 * Check if filesystem has free blocks available for allocation.
636 * Return the number of blocks avaible for allocation for this request
637 * On success, return nblocks
639 ext4_fsblk_t
ext4_has_free_blocks(struct ext4_sb_info
*sbi
,
642 s64 free_blocks
, dirty_blocks
;
644 struct percpu_counter
*fbc
= &sbi
->s_freeblocks_counter
;
645 struct percpu_counter
*dbc
= &sbi
->s_dirtyblocks_counter
;
647 free_blocks
= percpu_counter_read_positive(fbc
);
648 dirty_blocks
= percpu_counter_read_positive(dbc
);
650 if (!capable(CAP_SYS_RESOURCE
) &&
651 sbi
->s_resuid
!= current
->fsuid
&&
652 (sbi
->s_resgid
== 0 || !in_group_p(sbi
->s_resgid
)))
653 root_blocks
= ext4_r_blocks_count(sbi
->s_es
);
655 if (free_blocks
- (nblocks
+ root_blocks
+ dirty_blocks
) <
656 EXT4_FREEBLOCKS_WATERMARK
) {
657 free_blocks
= percpu_counter_sum(fbc
);
658 dirty_blocks
= percpu_counter_sum(dbc
);
660 if (free_blocks
<= (root_blocks
+ dirty_blocks
))
661 /* we don't have free space */
664 if (free_blocks
- (root_blocks
+ dirty_blocks
) < nblocks
)
665 return free_blocks
- (root_blocks
+ dirty_blocks
);
671 * ext4_should_retry_alloc()
673 * @retries number of attemps has been made
675 * ext4_should_retry_alloc() is called when ENOSPC is returned, and if
676 * it is profitable to retry the operation, this function will wait
677 * for the current or commiting transaction to complete, and then
680 * if the total number of retries exceed three times, return FALSE.
682 int ext4_should_retry_alloc(struct super_block
*sb
, int *retries
)
684 if (!ext4_has_free_blocks(EXT4_SB(sb
), 1) || (*retries
)++ > 3)
687 jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb
->s_id
);
689 return jbd2_journal_force_commit_nested(EXT4_SB(sb
)->s_journal
);
692 #define EXT4_META_BLOCK 0x1
694 static ext4_fsblk_t
do_blk_alloc(handle_t
*handle
, struct inode
*inode
,
695 ext4_lblk_t iblock
, ext4_fsblk_t goal
,
696 unsigned long *count
, int *errp
, int flags
)
698 struct ext4_allocation_request ar
;
701 memset(&ar
, 0, sizeof(ar
));
702 /* Fill with neighbour allocated blocks */
709 if (S_ISREG(inode
->i_mode
) && !(flags
& EXT4_META_BLOCK
))
710 /* enable in-core preallocation for data block allocation */
711 ar
.flags
= EXT4_MB_HINT_DATA
;
713 /* disable in-core preallocation for non-regular files */
716 ret
= ext4_mb_new_blocks(handle
, &ar
, errp
);
722 * ext4_new_meta_blocks() -- allocate block for meta data (indexing) blocks
724 * @handle: handle to this transaction
726 * @goal: given target block(filesystem wide)
727 * @count: total number of blocks need
730 * Return 1st allocated block numberon success, *count stores total account
731 * error stores in errp pointer
733 ext4_fsblk_t
ext4_new_meta_blocks(handle_t
*handle
, struct inode
*inode
,
734 ext4_fsblk_t goal
, unsigned long *count
, int *errp
)
737 ret
= do_blk_alloc(handle
, inode
, 0, goal
,
738 count
, errp
, EXT4_META_BLOCK
);
740 * Account for the allocated meta blocks
742 if (!(*errp
) && EXT4_I(inode
)->i_delalloc_reserved_flag
) {
743 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
744 EXT4_I(inode
)->i_allocated_meta_blocks
+= *count
;
745 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
751 * ext4_new_meta_block() -- allocate block for meta data (indexing) blocks
753 * @handle: handle to this transaction
755 * @goal: given target block(filesystem wide)
758 * Return allocated block number on success
760 ext4_fsblk_t
ext4_new_meta_block(handle_t
*handle
, struct inode
*inode
,
761 ext4_fsblk_t goal
, int *errp
)
763 unsigned long count
= 1;
764 return ext4_new_meta_blocks(handle
, inode
, goal
, &count
, errp
);
768 * ext4_new_blocks() -- allocate data blocks
770 * @handle: handle to this transaction
772 * @goal: given target block(filesystem wide)
773 * @count: total number of blocks need
776 * Return 1st allocated block numberon success, *count stores total account
777 * error stores in errp pointer
780 ext4_fsblk_t
ext4_new_blocks(handle_t
*handle
, struct inode
*inode
,
781 ext4_lblk_t iblock
, ext4_fsblk_t goal
,
782 unsigned long *count
, int *errp
)
784 return do_blk_alloc(handle
, inode
, iblock
, goal
, count
, errp
, 0);
788 * ext4_count_free_blocks() -- count filesystem free blocks
791 * Adds up the number of free blocks from each block group.
793 ext4_fsblk_t
ext4_count_free_blocks(struct super_block
*sb
)
795 ext4_fsblk_t desc_count
;
796 struct ext4_group_desc
*gdp
;
798 ext4_group_t ngroups
= EXT4_SB(sb
)->s_groups_count
;
800 struct ext4_super_block
*es
;
801 ext4_fsblk_t bitmap_count
;
803 struct buffer_head
*bitmap_bh
= NULL
;
805 es
= EXT4_SB(sb
)->s_es
;
811 for (i
= 0; i
< ngroups
; i
++) {
812 gdp
= ext4_get_group_desc(sb
, i
, NULL
);
815 desc_count
+= le16_to_cpu(gdp
->bg_free_blocks_count
);
817 bitmap_bh
= ext4_read_block_bitmap(sb
, i
);
818 if (bitmap_bh
== NULL
)
821 x
= ext4_count_free(bitmap_bh
, sb
->s_blocksize
);
822 printk(KERN_DEBUG
"group %lu: stored = %d, counted = %lu\n",
823 i
, le16_to_cpu(gdp
->bg_free_blocks_count
), x
);
827 printk(KERN_DEBUG
"ext4_count_free_blocks: stored = %llu"
828 ", computed = %llu, %llu\n", ext4_free_blocks_count(es
),
829 desc_count
, bitmap_count
);
834 for (i
= 0; i
< ngroups
; i
++) {
835 gdp
= ext4_get_group_desc(sb
, i
, NULL
);
838 desc_count
+= le16_to_cpu(gdp
->bg_free_blocks_count
);
845 static inline int test_root(ext4_group_t a
, int b
)
854 static int ext4_group_sparse(ext4_group_t group
)
860 return (test_root(group
, 7) || test_root(group
, 5) ||
861 test_root(group
, 3));
865 * ext4_bg_has_super - number of blocks used by the superblock in group
866 * @sb: superblock for filesystem
867 * @group: group number to check
869 * Return the number of blocks used by the superblock (primary or backup)
870 * in this group. Currently this will be only 0 or 1.
872 int ext4_bg_has_super(struct super_block
*sb
, ext4_group_t group
)
874 if (EXT4_HAS_RO_COMPAT_FEATURE(sb
,
875 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER
) &&
876 !ext4_group_sparse(group
))
881 static unsigned long ext4_bg_num_gdb_meta(struct super_block
*sb
,
884 unsigned long metagroup
= group
/ EXT4_DESC_PER_BLOCK(sb
);
885 ext4_group_t first
= metagroup
* EXT4_DESC_PER_BLOCK(sb
);
886 ext4_group_t last
= first
+ EXT4_DESC_PER_BLOCK(sb
) - 1;
888 if (group
== first
|| group
== first
+ 1 || group
== last
)
893 static unsigned long ext4_bg_num_gdb_nometa(struct super_block
*sb
,
896 return ext4_bg_has_super(sb
, group
) ? EXT4_SB(sb
)->s_gdb_count
: 0;
900 * ext4_bg_num_gdb - number of blocks used by the group table in group
901 * @sb: superblock for filesystem
902 * @group: group number to check
904 * Return the number of blocks used by the group descriptor table
905 * (primary or backup) in this group. In the future there may be a
906 * different number of descriptor blocks in each group.
908 unsigned long ext4_bg_num_gdb(struct super_block
*sb
, ext4_group_t group
)
910 unsigned long first_meta_bg
=
911 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_meta_bg
);
912 unsigned long metagroup
= group
/ EXT4_DESC_PER_BLOCK(sb
);
914 if (!EXT4_HAS_INCOMPAT_FEATURE(sb
,EXT4_FEATURE_INCOMPAT_META_BG
) ||
915 metagroup
< first_meta_bg
)
916 return ext4_bg_num_gdb_nometa(sb
, group
);
918 return ext4_bg_num_gdb_meta(sb
,group
);