365
* ext4_add_groupblocks() -- Add given blocks to an existing group
366
* @handle: handle to this transaction
368
* @block: start physcial block to add to the block group
369
* @count: number of blocks to free
371
* This marks the blocks as free in the bitmap. We ask the
372
* mballoc to reload the buddy after this by setting group
373
* EXT4_GROUP_INFO_NEED_INIT_BIT flag
375
void ext4_add_groupblocks(handle_t *handle, struct super_block *sb,
376
ext4_fsblk_t block, unsigned long count)
378
struct buffer_head *bitmap_bh = NULL;
379
struct buffer_head *gd_bh;
380
ext4_group_t block_group;
383
struct ext4_group_desc *desc;
384
struct ext4_sb_info *sbi = EXT4_SB(sb);
385
int err = 0, ret, blk_free_count;
386
ext4_grpblk_t blocks_freed;
387
struct ext4_group_info *grp;
389
ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1);
391
ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
392
grp = ext4_get_group_info(sb, block_group);
394
* Check to see if we are freeing blocks across a group
397
if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
400
bitmap_bh = ext4_read_block_bitmap(sb, block_group);
403
desc = ext4_get_group_desc(sb, block_group, &gd_bh);
407
if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
408
in_range(ext4_inode_bitmap(sb, desc), block, count) ||
409
in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
410
in_range(block + count - 1, ext4_inode_table(sb, desc),
411
sbi->s_itb_per_group)) {
412
ext4_error(sb, "Adding blocks in system zones - "
413
"Block = %llu, count = %lu",
419
* We are about to add blocks to the bitmap,
420
* so we need undo access.
422
BUFFER_TRACE(bitmap_bh, "getting undo access");
423
err = ext4_journal_get_undo_access(handle, bitmap_bh);
428
* We are about to modify some metadata. Call the journal APIs
429
* to unshare ->b_data if a currently-committing transaction is
432
BUFFER_TRACE(gd_bh, "get_write_access");
433
err = ext4_journal_get_write_access(handle, gd_bh);
437
* make sure we don't allow a parallel init on other groups in the
440
down_write(&grp->alloc_sem);
441
for (i = 0, blocks_freed = 0; i < count; i++) {
442
BUFFER_TRACE(bitmap_bh, "clear bit");
443
if (!ext4_clear_bit_atomic(ext4_group_lock_ptr(sb, block_group),
444
bit + i, bitmap_bh->b_data)) {
445
ext4_error(sb, "bit already cleared for block %llu",
446
(ext4_fsblk_t)(block + i));
447
BUFFER_TRACE(bitmap_bh, "bit already cleared");
452
ext4_lock_group(sb, block_group);
453
blk_free_count = blocks_freed + ext4_free_blks_count(sb, desc);
454
ext4_free_blks_set(sb, desc, blk_free_count);
455
desc->bg_checksum = ext4_group_desc_csum(sbi, block_group, desc);
456
ext4_unlock_group(sb, block_group);
457
percpu_counter_add(&sbi->s_freeblocks_counter, blocks_freed);
459
if (sbi->s_log_groups_per_flex) {
460
ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
461
atomic_add(blocks_freed,
462
&sbi->s_flex_groups[flex_group].free_blocks);
465
* request to reload the buddy with the
466
* new bitmap information
468
set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
469
grp->bb_free += blocks_freed;
470
up_write(&grp->alloc_sem);
472
/* We dirtied the bitmap block */
473
BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
474
err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
476
/* And the group descriptor block */
477
BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
478
ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
484
ext4_std_error(sb, err);
489
365
* ext4_has_free_blocks()
490
366
* @sbi: in-core super block structure.
491
367
* @nblocks: number of needed blocks