2 * online.c --- Do on-line resizing of the ext3 filesystem
4 * Copyright (C) 2006 by Theodore Ts'o
7 * This file may be redistributed under the terms of the GNU Public
12 #include "resize2fs.h"
13 #ifdef HAVE_SYS_IOCTL_H
14 #include <sys/ioctl.h>
19 extern char *program_name
;
21 errcode_t
online_resize_fs(ext2_filsys fs
, const char *mtpt
,
22 blk64_t
*new_size
, int flags
EXT2FS_ATTR((unused
)))
25 struct ext2_new_group_input input
;
26 struct ext4_new_group_input input64
;
27 struct ext2_super_block
*sb
= fs
->super
;
28 unsigned long new_desc_blocks
;
35 int use_old_ioctl
= 1;
37 printf(_("Filesystem at %s is mounted on %s; "
38 "on-line resizing required\n"), fs
->device_name
, mtpt
);
40 if (*new_size
< ext2fs_blocks_count(sb
)) {
41 printf(_("On-line shrinking from %llu to %llu not supported.\n"),
42 ext2fs_blocks_count(sb
), *new_size
);
47 * If the number of descriptor blocks is going to increase,
48 * the on-line resizing inode must be present.
50 new_desc_blocks
= ext2fs_div_ceil(
51 ext2fs_div64_ceil(*new_size
-
52 fs
->super
->s_first_data_block
,
53 EXT2_BLOCKS_PER_GROUP(fs
->super
)),
54 EXT2_DESC_PER_BLOCK(fs
->super
));
55 printf("old desc_blocks = %lu, new_desc_blocks = %lu\n",
56 fs
->desc_blocks
, new_desc_blocks
);
57 if (!(fs
->super
->s_feature_compat
&
58 EXT2_FEATURE_COMPAT_RESIZE_INODE
) &&
59 new_desc_blocks
!= fs
->desc_blocks
) {
60 com_err(program_name
, 0,
61 _("Filesystem does not support online resizing"));
65 fd
= open(mtpt
, O_RDONLY
);
67 com_err(program_name
, errno
,
68 _("while trying to open mountpoint %s"), mtpt
);
72 size
=ext2fs_blocks_count(sb
);
73 if (ioctl(fd
, EXT2_IOC_GROUP_EXTEND
, &size
)) {
75 com_err(program_name
, 0,
76 _("Permission denied to resize filesystem"));
77 else if (errno
== ENOTTY
)
78 com_err(program_name
, 0,
79 _("Kernel does not support online resizing"));
81 com_err(program_name
, errno
,
82 _("While checking for on-line resizing support"));
86 percent
= (ext2fs_r_blocks_count(sb
) * 100.0) /
87 ext2fs_blocks_count(sb
);
89 retval
= ext2fs_read_bitmaps(fs
);
93 retval
= ext2fs_dup_handle(fs
, &new_fs
);
97 /* The current method of adding one block group at a time to a
98 * mounted filesystem means it is impossible to accomodate the
99 * flex_bg allocation method of placing the metadata together
100 * in a single block group. For now we "fix" this issue by
101 * using the traditional layout for new block groups, where
102 * each block group is self-contained and contains its own
103 * bitmap blocks and inode tables. This means we don't get
104 * the layout advantages of flex_bg in the new block groups,
105 * but at least it allows on-line resizing to function.
107 new_fs
->super
->s_feature_incompat
&= ~EXT4_FEATURE_INCOMPAT_FLEX_BG
;
108 retval
= adjust_fs_info(new_fs
, fs
, 0, *new_size
);
112 printf(_("Performing an on-line resize of %s to %llu (%dk) blocks.\n"),
113 fs
->device_name
, *new_size
, fs
->blocksize
/ 1024);
115 size
= fs
->group_desc_count
* sb
->s_blocks_per_group
+
116 sb
->s_first_data_block
;
117 if (size
> *new_size
)
120 if (ioctl(fd
, EXT2_IOC_GROUP_EXTEND
, &size
)) {
121 com_err(program_name
, errno
,
122 _("While trying to extend the last group"));
126 for (i
= fs
->group_desc_count
;
127 i
< new_fs
->group_desc_count
; i
++) {
129 overhead
= (int) (2 + new_fs
->inode_blocks_per_group
);
131 if (ext2fs_bg_has_super(new_fs
, new_fs
->group_desc_count
- 1))
132 overhead
+= 1 + new_fs
->desc_blocks
+
133 new_fs
->super
->s_reserved_gdt_blocks
;
136 input
.block_bitmap
= ext2fs_block_bitmap_loc(new_fs
, i
);
137 input
.inode_bitmap
= ext2fs_inode_bitmap_loc(new_fs
, i
);
138 input
.inode_table
= ext2fs_inode_table_loc(new_fs
, i
);
139 input
.blocks_count
= sb
->s_blocks_per_group
;
140 if (i
== new_fs
->group_desc_count
-1) {
141 input
.blocks_count
= ext2fs_blocks_count(new_fs
->super
) -
142 sb
->s_first_data_block
-
143 (i
* sb
->s_blocks_per_group
);
145 input
.reserved_blocks
= (blk_t
) (percent
* input
.blocks_count
149 printf("new block bitmap is at 0x%04x\n", input
.block_bitmap
);
150 printf("new inode bitmap is at 0x%04x\n", input
.inode_bitmap
);
151 printf("new inode table is at 0x%04x-0x%04x\n",
153 input
.inode_table
+ new_fs
->inode_blocks_per_group
-1);
154 printf("new group has %u blocks\n", input
.blocks_count
);
155 printf("new group will reserve %d blocks\n",
156 input
.reserved_blocks
);
157 printf("new group has %d free blocks\n",
158 ext2fs_bg_free_blocks_count(new_fs
, i
),
159 printf("new group has %d free inodes (%d blocks)\n",
160 ext2fs_bg_free_inodes_count(new_fs
, i
),
161 new_fs
->inode_blocks_per_group
);
162 printf("Adding group #%d\n", input
.group
);
166 ioctl(fd
, EXT2_IOC_GROUP_ADD
, &input
) == 0)
171 input64
.group
= input
.group
;
172 input64
.block_bitmap
= input
.block_bitmap
;
173 input64
.inode_bitmap
= input
.inode_bitmap
;
174 input64
.inode_table
= input
.inode_table
;
175 input64
.blocks_count
= input
.blocks_count
;
176 input64
.reserved_blocks
= input
.reserved_blocks
;
177 input64
.unused
= input
.unused
;
179 if (ioctl(fd
, EXT4_IOC_GROUP_ADD
, &input64
) < 0) {
180 com_err(program_name
, errno
,
181 _("While trying to add group #%d"),
192 printf(_("Filesystem at %s is mounted on %s, and on-line resizing is "
193 "not supported on this system.\n"), fs
->device_name
, mtpt
);