static int get_resize_stats(struct resize_stats *stats, char *key_loc, char *blk_device) { errcode_t retval; ext2_filsys fs; ext2_filsys dup_fs; io_manager io_ptr; long long data_partition_size = 0; io_ptr = unix_io_manager; retval = ext2fs_open2(blk_device, NULL , 0, 0, 0, io_ptr, &fs); if (retval) { ERROR("Couldn't find valid filesystem superblock.\n"); return -1; } retval = ext2fs_dup_handle(fs, &dup_fs); if (retval) { ERROR("Couldn't duplicate filesys.\n"); ext2fs_close(fs); return -1; } if((data_partition_size = get_block_size(blk_device)) < 0) { ERROR("Get %s partition size fail.\n", blk_device); ext2fs_close(fs); ext2fs_free(dup_fs); return -1; } ERROR("Size for partition(%s) is %lluK.\n", blk_device, data_partition_size / 1024); ERROR("Size in superblock is %lluK.\n", (unsigned long long)fs->super->s_blocks_count * fs->blocksize / 1024); if (!strcmp(key_loc, KEY_IN_FOOTER)){ data_partition_size -= RESERVE_SIZE; ERROR("There is key in footer of partition(%s).\n", blk_device); } //adjust partition size to meet resizefs rule. //resizefs will adjust partition size in some case. retval = adjust_fs_size(dup_fs, &data_partition_size); if (retval) { ERROR("Couldn't adjust partition size.\n"); ext2fs_close(fs); ext2fs_free(dup_fs); return -1; } ERROR("Size will (maybe) resize to(after adjust) is %lluK.\n", data_partition_size / 1024); if((unsigned int)(data_partition_size / fs->blocksize) == (unsigned int)(fs->super->s_blocks_count)) { ERROR("The size of data already meet the request(size in superblock = size after adjust).\n"); stats->state = STATS_RESIZED; } stats->size = data_partition_size; ext2fs_close(fs); ext2fs_free(dup_fs); return 0; }
errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt, blk64_t *new_size, int flags EXT2FS_ATTR((unused))) { #ifdef __linux__ struct ext2_new_group_input input; struct ext4_new_group_input input64; struct ext2_super_block *sb = fs->super; unsigned long new_desc_blocks; ext2_filsys new_fs; errcode_t retval; double percent; dgrp_t i; blk_t size; int fd, overhead; int use_old_ioctl = 1; int no_meta_bg_resize = 0; int no_resize_ioctl = 0; if (getenv("RESIZE2FS_KERNEL_VERSION")) { char *version_to_emulate = getenv("RESIZE2FS_KERNEL_VERSION"); int kvers = parse_version_number(version_to_emulate); if (kvers < VERSION_CODE(3, 7, 0)) no_meta_bg_resize = 1; if (kvers < VERSION_CODE(3, 3, 0)) no_resize_ioctl = 1; } if (ext2fs_has_feature_sparse_super2(fs->super) && (access("/sys/fs/ext4/features/sparse_super2", R_OK) != 0)) { com_err(program_name, 0, _("kernel does not support online " "resize with sparse_super2")); exit(1); } printf(_("Filesystem at %s is mounted on %s; " "on-line resizing required\n"), fs->device_name, mtpt); if (*new_size < ext2fs_blocks_count(sb)) { com_err(program_name, 0, _("On-line shrinking not supported")); exit(1); } /* * If the number of descriptor blocks is going to increase, * the on-line resizing inode must be present. */ new_desc_blocks = ext2fs_div_ceil( ext2fs_div64_ceil(*new_size - fs->super->s_first_data_block, EXT2_BLOCKS_PER_GROUP(fs->super)), EXT2_DESC_PER_BLOCK(fs->super)); printf("old_desc_blocks = %lu, new_desc_blocks = %lu\n", fs->desc_blocks, new_desc_blocks); /* * Do error checking to make sure the resize will be successful. */ if ((access("/sys/fs/ext4/features/meta_bg_resize", R_OK) != 0) || no_meta_bg_resize) { if (!ext2fs_has_feature_resize_inode(fs->super) && (new_desc_blocks != fs->desc_blocks)) { com_err(program_name, 0, _("Filesystem does not support online resizing")); exit(1); } if (ext2fs_has_feature_resize_inode(fs->super) && new_desc_blocks > (fs->desc_blocks + fs->super->s_reserved_gdt_blocks)) { com_err(program_name, 0, _("Not enough reserved gdt blocks for resizing")); exit(1); } if ((ext2fs_blocks_count(sb) > MAX_32_NUM) || (*new_size > MAX_32_NUM)) { com_err(program_name, 0, _("Kernel does not support resizing a file system this large")); exit(1); } } fd = open(mtpt, O_RDONLY); if (fd < 0) { com_err(program_name, errno, _("while trying to open mountpoint %s"), mtpt); exit(1); } if (no_resize_ioctl) { printf(_("Old resize interface requested.\n")); } else if (ioctl(fd, EXT4_IOC_RESIZE_FS, new_size)) { /* * If kernel does not support EXT4_IOC_RESIZE_FS, use the * old online resize. Note that the old approach does not * handle >32 bit file systems * * Sigh, if we are running a 32-bit binary on a 64-bit * kernel (which happens all the time on the MIPS * architecture in Debian, but can happen on other CPU * architectures as well) we will get EINVAL returned * when an ioctl doesn't exist, at least up to Linux * 3.1. See compat_sys_ioctl() in fs/compat_ioctl.c * in the kernel sources. This is probably a kernel * bug, but work around it here. */ if ((errno != ENOTTY) && (errno != EINVAL)) { if (errno == EPERM) com_err(program_name, 0, _("Permission denied to resize filesystem")); else com_err(program_name, errno, _("While checking for on-line resizing " "support")); exit(1); } } else { close(fd); return 0; } size = ext2fs_blocks_count(sb); if (ioctl(fd, EXT2_IOC_GROUP_EXTEND, &size)) { if (errno == EPERM) com_err(program_name, 0, _("Permission denied to resize filesystem")); else if (errno == ENOTTY) com_err(program_name, 0, _("Kernel does not support online resizing")); else com_err(program_name, errno, _("While checking for on-line resizing support")); exit(1); } percent = (ext2fs_r_blocks_count(sb) * 100.0) / ext2fs_blocks_count(sb); retval = ext2fs_read_bitmaps(fs); if (retval) { close(fd); return retval; } retval = ext2fs_dup_handle(fs, &new_fs); if (retval) { close(fd); return retval; } /* The current method of adding one block group at a time to a * mounted filesystem means it is impossible to accommodate the * flex_bg allocation method of placing the metadata together * in a single block group. For now we "fix" this issue by * using the traditional layout for new block groups, where * each block group is self-contained and contains its own * bitmap blocks and inode tables. This means we don't get * the layout advantages of flex_bg in the new block groups, * but at least it allows on-line resizing to function. */ ext2fs_clear_feature_flex_bg(new_fs->super); retval = adjust_fs_info(new_fs, fs, 0, *new_size); if (retval) { close(fd); return retval; } printf(_("Performing an on-line resize of %s to %llu (%dk) blocks.\n"), fs->device_name, *new_size, fs->blocksize / 1024); size = fs->group_desc_count * sb->s_blocks_per_group + sb->s_first_data_block; if (size > *new_size) size = *new_size; if (ioctl(fd, EXT2_IOC_GROUP_EXTEND, &size)) { com_err(program_name, errno, _("While trying to extend the last group")); exit(1); } for (i = fs->group_desc_count; i < new_fs->group_desc_count; i++) { overhead = (int) (2 + new_fs->inode_blocks_per_group); if (ext2fs_bg_has_super(new_fs, new_fs->group_desc_count - 1)) overhead += 1 + new_fs->desc_blocks + new_fs->super->s_reserved_gdt_blocks; input.group = i; input.block_bitmap = ext2fs_block_bitmap_loc(new_fs, i); input.inode_bitmap = ext2fs_inode_bitmap_loc(new_fs, i); input.inode_table = ext2fs_inode_table_loc(new_fs, i); input.blocks_count = ext2fs_group_blocks_count(new_fs, i); input.reserved_blocks = (blk_t) (percent * input.blocks_count / 100.0); #if 0 printf("new block bitmap is at 0x%04x\n", input.block_bitmap); printf("new inode bitmap is at 0x%04x\n", input.inode_bitmap); printf("new inode table is at 0x%04x-0x%04x\n", input.inode_table, input.inode_table + new_fs->inode_blocks_per_group-1); printf("new group has %u blocks\n", input.blocks_count); printf("new group will reserve %d blocks\n", input.reserved_blocks); printf("new group has %d free blocks\n", ext2fs_bg_free_blocks_count(new_fs, i), printf("new group has %d free inodes (%d blocks)\n", ext2fs_bg_free_inodes_count(new_fs, i), new_fs->inode_blocks_per_group); printf("Adding group #%d\n", input.group); #endif if (use_old_ioctl && ioctl(fd, EXT2_IOC_GROUP_ADD, &input) == 0) continue; else use_old_ioctl = 0; input64.group = input.group; input64.block_bitmap = input.block_bitmap; input64.inode_bitmap = input.inode_bitmap; input64.inode_table = input.inode_table; input64.blocks_count = input.blocks_count; input64.reserved_blocks = input.reserved_blocks; input64.unused = input.unused; if (ioctl(fd, EXT4_IOC_GROUP_ADD, &input64) < 0) { com_err(program_name, errno, _("While trying to add group #%d"), input.group); exit(1); } } ext2fs_free(new_fs); close(fd); return 0; #else printf(_("Filesystem at %s is mounted on %s, and on-line resizing is " "not supported on this system.\n"), fs->device_name, mtpt); exit(1); #endif }
errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt, blk64_t *new_size, int flags EXT2FS_ATTR((unused))) { #ifdef __linux__ struct ext2_new_group_input input; struct ext4_new_group_input input64; struct ext2_super_block *sb = fs->super; unsigned long new_desc_blocks; ext2_filsys new_fs; errcode_t retval; double percent; dgrp_t i; blk64_t size; int fd, overhead; int use_old_ioctl = 1; printf(_("Filesystem at %s is mounted on %s; " "on-line resizing required\n"), fs->device_name, mtpt); if (*new_size < ext2fs_blocks_count(sb)) { com_err(program_name, 0, _("On-line shrinking not supported")); exit(1); } /* * If the number of descriptor blocks is going to increase, * the on-line resizing inode must be present. */ new_desc_blocks = ext2fs_div_ceil( ext2fs_div64_ceil(*new_size - fs->super->s_first_data_block, EXT2_BLOCKS_PER_GROUP(fs->super)), EXT2_DESC_PER_BLOCK(fs->super)); printf("old desc_blocks = %lu, new_desc_blocks = %lu\n", fs->desc_blocks, new_desc_blocks); if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) && new_desc_blocks != fs->desc_blocks) { com_err(program_name, 0, _("Filesystem does not support online resizing")); exit(1); } fd = open(mtpt, O_RDONLY); if (fd < 0) { com_err(program_name, errno, _("while trying to open mountpoint %s"), mtpt); exit(1); } size=ext2fs_blocks_count(sb); if (ioctl(fd, EXT2_IOC_GROUP_EXTEND, &size)) { if (errno == EPERM) com_err(program_name, 0, _("Permission denied to resize filesystem")); else if (errno == ENOTTY) com_err(program_name, 0, _("Kernel does not support online resizing")); else com_err(program_name, errno, _("While checking for on-line resizing support")); exit(1); } percent = (ext2fs_r_blocks_count(sb) * 100.0) / ext2fs_blocks_count(sb); retval = ext2fs_read_bitmaps(fs); if (retval) return retval; retval = ext2fs_dup_handle(fs, &new_fs); if (retval) return retval; /* The current method of adding one block group at a time to a * mounted filesystem means it is impossible to accomodate the * flex_bg allocation method of placing the metadata together * in a single block group. For now we "fix" this issue by * using the traditional layout for new block groups, where * each block group is self-contained and contains its own * bitmap blocks and inode tables. This means we don't get * the layout advantages of flex_bg in the new block groups, * but at least it allows on-line resizing to function. */ new_fs->super->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_FLEX_BG; retval = adjust_fs_info(new_fs, fs, 0, *new_size); if (retval) return retval; printf(_("Performing an on-line resize of %s to %llu (%dk) blocks.\n"), fs->device_name, *new_size, fs->blocksize / 1024); size = fs->group_desc_count * sb->s_blocks_per_group + sb->s_first_data_block; if (size > *new_size) size = *new_size; if (ioctl(fd, EXT2_IOC_GROUP_EXTEND, &size)) { com_err(program_name, errno, _("While trying to extend the last group")); exit(1); } for (i = fs->group_desc_count; i < new_fs->group_desc_count; i++) { overhead = (int) (2 + new_fs->inode_blocks_per_group); if (ext2fs_bg_has_super(new_fs, new_fs->group_desc_count - 1)) overhead += 1 + new_fs->desc_blocks + new_fs->super->s_reserved_gdt_blocks; input.group = i; input.block_bitmap = ext2fs_block_bitmap_loc(new_fs, i); input.inode_bitmap = ext2fs_inode_bitmap_loc(new_fs, i); input.inode_table = ext2fs_inode_table_loc(new_fs, i); input.blocks_count = sb->s_blocks_per_group; if (i == new_fs->group_desc_count-1) { input.blocks_count = ext2fs_blocks_count(new_fs->super) - sb->s_first_data_block - (i * sb->s_blocks_per_group); } input.reserved_blocks = (blk_t) (percent * input.blocks_count / 100.0); #if 0 printf("new block bitmap is at 0x%04x\n", input.block_bitmap); printf("new inode bitmap is at 0x%04x\n", input.inode_bitmap); printf("new inode table is at 0x%04x-0x%04x\n", input.inode_table, input.inode_table + new_fs->inode_blocks_per_group-1); printf("new group has %u blocks\n", input.blocks_count); printf("new group will reserve %d blocks\n", input.reserved_blocks); printf("new group has %d free blocks\n", ext2fs_bg_free_blocks_count(new_fs, i), printf("new group has %d free inodes (%d blocks)\n", ext2fs_bg_free_inodes_count(new_fs, i), new_fs->inode_blocks_per_group); printf("Adding group #%d\n", input.group); #endif if (use_old_ioctl && ioctl(fd, EXT2_IOC_GROUP_ADD, &input) == 0) continue; else use_old_ioctl = 0; input64.group = input.group; input64.block_bitmap = input.block_bitmap; input64.inode_bitmap = input.inode_bitmap; input64.inode_table = input.inode_table; input64.blocks_count = input.blocks_count; input64.reserved_blocks = input.reserved_blocks; input64.unused = input.unused; if (ioctl(fd, EXT4_IOC_GROUP_ADD, &input64) < 0) { com_err(program_name, errno, _("While trying to add group #%d"), input.group); exit(1); } } ext2fs_free(new_fs); close(fd); return 0; #else printf(_("Filesystem at %s is mounted on %s, and on-line resizing is " "not supported on this system.\n"), fs->device_name, mtpt); exit(1); #endif }
errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt, blk_t *new_size, int flags) { struct ext2_new_group_input input; struct ext2_super_block *sb = fs->super; ext2_filsys new_fs; errcode_t retval; dgrp_t i; blk_t size; int fd, r_frac, overhead; printf(_("Filesystem at %s is mounted on %s; " "on-line resizing required\n"), fs->device_name, mtpt); if (*new_size < sb->s_blocks_count) { printf(_("On-line shrinking from %u to %u not supported.\n"), sb->s_blocks_count, *new_size); exit(1); } fd = open(mtpt, O_RDONLY); if (fd < 0) { com_err(program_name, errno, _("while trying to open mountpoint %s"), mtpt); exit(1); } size=sb->s_blocks_count; if (ioctl(fd, EXT2_IOC_GROUP_EXTEND, &size)) { if (errno == EPERM) com_err(program_name, 0, _("Permission denied to resize filesystem")); else if (errno == ENOTTY) com_err(program_name, 0, _("Filesystem does not support online resizing")); else com_err(program_name, errno, _("While checking for on-line resizing support")); exit(1); } r_frac = ((100 * sb->s_r_blocks_count) + sb->s_blocks_count-1) / sb->s_blocks_count; retval = ext2fs_read_bitmaps(fs); if (retval) return retval; retval = ext2fs_dup_handle(fs, &new_fs); if (retval) return retval; retval = adjust_fs_info(new_fs, fs, *new_size); if (retval) return retval; printf(_("Performing an on-line resize of %s to %d (%dk) blocks.\n"), fs->device_name, *new_size, fs->blocksize / 1024); size = fs->group_desc_count * sb->s_blocks_per_group + sb->s_first_data_block; if (size > *new_size) size = *new_size; if (ioctl(fd, EXT2_IOC_GROUP_EXTEND, &size)) { com_err(program_name, errno, _("While trying to extend the last group")); exit(1); } for (i = fs->group_desc_count; i < new_fs->group_desc_count; i++) { overhead = (int) (2 + new_fs->inode_blocks_per_group); if (ext2fs_bg_has_super(new_fs, new_fs->group_desc_count - 1)) overhead += 1 + new_fs->desc_blocks + new_fs->super->s_reserved_gdt_blocks; input.group = i; input.block_bitmap = new_fs->group_desc[i].bg_block_bitmap; input.inode_bitmap = new_fs->group_desc[i].bg_inode_bitmap; input.inode_table = new_fs->group_desc[i].bg_inode_table; input.blocks_count = sb->s_blocks_per_group; if (i == new_fs->group_desc_count-1) { input.blocks_count = new_fs->super->s_blocks_count - sb->s_first_data_block - (i * sb->s_blocks_per_group); } input.reserved_blocks = input.blocks_count * r_frac / 100; #if 0 printf("new block bitmap is at 0x%04x\n", input.block_bitmap); printf("new inode bitmap is at 0x%04x\n", input.inode_bitmap); printf("new inode table is at 0x%04x-0x%04x\n", input.inode_table, input.inode_table + new_fs->inode_blocks_per_group-1); printf("new group has %d blocks\n", input.blocks_count); printf("new group will reserve %d blocks\n", input.reserved_blocks); printf("new group has %d free blocks\n", new_fs->group_desc[i].bg_free_blocks_count); printf("new group has %d free inodes (%d blocks)\n", new_fs->group_desc[i].bg_free_inodes_count, new_fs->inode_blocks_per_group); printf("Adding group #%d\n", input.group); #endif if (ioctl(fd, EXT2_IOC_GROUP_ADD, &input) < 0) { com_err(program_name, errno, _("While trying to add group #%d"), input.group); exit(1); } } ext2fs_free(new_fs); close(fd); return 0; }