Home | History | Annotate | Download | only in resize
      1 /*
      2  * online.c --- Do on-line resizing of the ext3 filesystem
      3  *
      4  * Copyright (C) 2006 by Theodore Ts'o
      5  *
      6  * %Begin-Header%
      7  * This file may be redistributed under the terms of the GNU Public
      8  * License.
      9  * %End-Header%
     10  */
     11 
     12 #include "resize2fs.h"
     13 #ifdef HAVE_SYS_IOCTL_H
     14 #include <sys/ioctl.h>
     15 #endif
     16 #include <sys/stat.h>
     17 #include <fcntl.h>
     18 
     19 extern char *program_name;
     20 
     21 errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt,
     22 			   blk_t *new_size, int flags EXT2FS_ATTR((unused)))
     23 {
     24 #ifdef __linux__
     25 	struct ext2_new_group_input input;
     26 	struct ext2_super_block *sb = fs->super;
     27 	unsigned long		new_desc_blocks;
     28 	ext2_filsys 		new_fs;
     29 	errcode_t 		retval;
     30 	dgrp_t			i;
     31 	blk_t			size;
     32 	int			fd, r_frac, overhead;
     33 
     34 	printf(_("Filesystem at %s is mounted on %s; "
     35 		 "on-line resizing required\n"), fs->device_name, mtpt);
     36 
     37 	if (*new_size < sb->s_blocks_count) {
     38 		printf(_("On-line shrinking from %u to %u not supported.\n"),
     39 		       sb->s_blocks_count, *new_size);
     40 		exit(1);
     41 	}
     42 
     43 	/*
     44 	 * If the number of descriptor blocks is going to increase,
     45 	 * the on-line resizing inode must be present.
     46 	 */
     47 	new_desc_blocks = ext2fs_div_ceil(
     48 		ext2fs_div_ceil(*new_size -
     49 				fs->super->s_first_data_block,
     50 				EXT2_BLOCKS_PER_GROUP(fs->super)),
     51 		EXT2_DESC_PER_BLOCK(fs->super));
     52 	printf("old desc_blocks = %lu, new_desc_blocks = %lu\n",
     53 	       fs->desc_blocks, new_desc_blocks);
     54 	if (!(fs->super->s_feature_compat &
     55 	      EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
     56 	    new_desc_blocks != fs->desc_blocks) {
     57 		com_err(program_name, 0,
     58 			_("Filesystem does not support online resizing"));
     59 		exit(1);
     60 	}
     61 
     62 	fd = open(mtpt, O_RDONLY);
     63 	if (fd < 0) {
     64 		com_err(program_name, errno,
     65 			_("while trying to open mountpoint %s"), mtpt);
     66 		exit(1);
     67 	}
     68 
     69 	size=sb->s_blocks_count;
     70 	if (ioctl(fd, EXT2_IOC_GROUP_EXTEND, &size)) {
     71 		if (errno == EPERM)
     72 			com_err(program_name, 0,
     73 				_("Permission denied to resize filesystem"));
     74 		else if (errno == ENOTTY)
     75 			com_err(program_name, 0,
     76 			_("Kernel does not support online resizing"));
     77 		else
     78 			com_err(program_name, errno,
     79 			_("While checking for on-line resizing support"));
     80 		exit(1);
     81 	}
     82 
     83 	r_frac = ext2fs_div_ceil(100 * sb->s_r_blocks_count, sb->s_blocks_count);
     84 
     85 	retval = ext2fs_read_bitmaps(fs);
     86 	if (retval)
     87 		return retval;
     88 
     89 	retval = ext2fs_dup_handle(fs, &new_fs);
     90 	if (retval)
     91 		return retval;
     92 
     93 	retval = adjust_fs_info(new_fs, fs, *new_size);
     94 	if (retval)
     95 		return retval;
     96 
     97 	printf(_("Performing an on-line resize of %s to %u (%dk) blocks.\n"),
     98 	       fs->device_name, *new_size, fs->blocksize / 1024);
     99 
    100 	size = fs->group_desc_count * sb->s_blocks_per_group +
    101 		sb->s_first_data_block;
    102 	if (size > *new_size)
    103 		size = *new_size;
    104 
    105 	if (ioctl(fd, EXT2_IOC_GROUP_EXTEND, &size)) {
    106 		com_err(program_name, errno,
    107 			_("While trying to extend the last group"));
    108 		exit(1);
    109 	}
    110 
    111 	for (i = fs->group_desc_count;
    112 	     i < new_fs->group_desc_count; i++) {
    113 
    114 		overhead = (int) (2 + new_fs->inode_blocks_per_group);
    115 
    116 		if (ext2fs_bg_has_super(new_fs, new_fs->group_desc_count - 1))
    117 			overhead += 1 + new_fs->desc_blocks +
    118 				new_fs->super->s_reserved_gdt_blocks;
    119 
    120 		input.group = i;
    121 		input.block_bitmap = new_fs->group_desc[i].bg_block_bitmap;
    122 		input.inode_bitmap = new_fs->group_desc[i].bg_inode_bitmap;
    123 		input.inode_table = new_fs->group_desc[i].bg_inode_table;
    124 		input.blocks_count = sb->s_blocks_per_group;
    125 		if (i == new_fs->group_desc_count-1) {
    126 			input.blocks_count = new_fs->super->s_blocks_count -
    127 				sb->s_first_data_block -
    128 				(i * sb->s_blocks_per_group);
    129 		}
    130 		input.reserved_blocks = e2p_percent(r_frac,
    131 						    input.blocks_count);
    132 
    133 #if 0
    134 		printf("new block bitmap is at 0x%04x\n", input.block_bitmap);
    135 		printf("new inode bitmap is at 0x%04x\n", input.inode_bitmap);
    136 		printf("new inode table is at 0x%04x-0x%04x\n",
    137 		       input.inode_table,
    138 		       input.inode_table + new_fs->inode_blocks_per_group-1);
    139 		printf("new group has %u blocks\n", input.blocks_count);
    140 		printf("new group will reserve %d blocks\n",
    141 		       input.reserved_blocks);
    142 		printf("new group has %d free blocks\n",
    143 		       new_fs->group_desc[i].bg_free_blocks_count);
    144 		printf("new group has %d free inodes (%d blocks)\n",
    145 		       new_fs->group_desc[i].bg_free_inodes_count,
    146 		       new_fs->inode_blocks_per_group);
    147 		printf("Adding group #%d\n", input.group);
    148 #endif
    149 
    150 		if (ioctl(fd, EXT2_IOC_GROUP_ADD, &input) < 0) {
    151 			com_err(program_name, errno,
    152 				_("While trying to add group #%d"),
    153 				input.group);
    154 			exit(1);
    155 		}
    156 	}
    157 
    158 	ext2fs_free(new_fs);
    159 	close(fd);
    160 
    161 	return 0;
    162 #else
    163 	printf(_("Filesystem at %s is mounted on %s, and on-line resizing is"
    164 		 "not supported on this system.\n"), fs->device_name, mtpt);
    165 	exit(1);
    166 #endif
    167 }
    168