]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - resize/online.c
Clean up and fix Android build files
[thirdparty/e2fsprogs.git] / resize / online.c
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 "config.h"
13 #include "resize2fs.h"
14 #ifdef HAVE_SYS_IOCTL_H
15 #include <sys/ioctl.h>
16 #endif
17 #include <sys/stat.h>
18 #include <fcntl.h>
19
20 extern char *program_name;
21
22 #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
23
24 #ifdef __linux__
25 static int parse_version_number(const char *s)
26 {
27 int major, minor, rev;
28 char *endptr;
29 const char *cp = s;
30
31 if (!s)
32 return 0;
33 major = strtol(cp, &endptr, 10);
34 if (cp == endptr || *endptr != '.')
35 return 0;
36 cp = endptr + 1;
37 minor = strtol(cp, &endptr, 10);
38 if (cp == endptr || *endptr != '.')
39 return 0;
40 cp = endptr + 1;
41 rev = strtol(cp, &endptr, 10);
42 if (cp == endptr)
43 return 0;
44 return ((((major * 256) + minor) * 256) + rev);
45 }
46
47 #define VERSION_CODE(a,b,c) (((a) << 16) + ((b) << 8) + (c))
48
49 #endif
50
51 errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt,
52 blk64_t *new_size, int flags EXT2FS_ATTR((unused)))
53 {
54 #ifdef __linux__
55 struct ext2_new_group_input input;
56 struct ext4_new_group_input input64;
57 struct ext2_super_block *sb = fs->super;
58 unsigned long new_desc_blocks;
59 ext2_filsys new_fs;
60 errcode_t retval;
61 double percent;
62 dgrp_t i;
63 blk_t size;
64 int fd, overhead;
65 int use_old_ioctl = 1;
66 int no_meta_bg_resize = 0;
67 int no_resize_ioctl = 0;
68
69 if (getenv("RESIZE2FS_KERNEL_VERSION")) {
70 char *version_to_emulate = getenv("RESIZE2FS_KERNEL_VERSION");
71 int kvers = parse_version_number(version_to_emulate);
72
73 if (kvers < VERSION_CODE(3, 7, 0))
74 no_meta_bg_resize = 1;
75 if (kvers < VERSION_CODE(3, 3, 0))
76 no_resize_ioctl = 1;
77 }
78
79 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
80 EXT4_FEATURE_COMPAT_SPARSE_SUPER2) &&
81 (access("/sys/fs/ext4/features/sparse_super2", R_OK) != 0)) {
82 com_err(program_name, 0, _("kernel does not support online "
83 "resize with sparse_super2"));
84 exit(1);
85 }
86
87 printf(_("Filesystem at %s is mounted on %s; "
88 "on-line resizing required\n"), fs->device_name, mtpt);
89
90 if (*new_size < ext2fs_blocks_count(sb)) {
91 com_err(program_name, 0, _("On-line shrinking not supported"));
92 exit(1);
93 }
94
95 /*
96 * If the number of descriptor blocks is going to increase,
97 * the on-line resizing inode must be present.
98 */
99 new_desc_blocks = ext2fs_div_ceil(
100 ext2fs_div64_ceil(*new_size -
101 fs->super->s_first_data_block,
102 EXT2_BLOCKS_PER_GROUP(fs->super)),
103 EXT2_DESC_PER_BLOCK(fs->super));
104 printf("old_desc_blocks = %lu, new_desc_blocks = %lu\n",
105 fs->desc_blocks, new_desc_blocks);
106
107 /*
108 * Do error checking to make sure the resize will be successful.
109 */
110 if ((access("/sys/fs/ext4/features/meta_bg_resize", R_OK) != 0) ||
111 no_meta_bg_resize) {
112 if (!EXT2_HAS_COMPAT_FEATURE(fs->super,
113 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
114 (new_desc_blocks != fs->desc_blocks)) {
115 com_err(program_name, 0,
116 _("Filesystem does not support online resizing"));
117 exit(1);
118 }
119
120 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
121 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
122 new_desc_blocks > (fs->desc_blocks +
123 fs->super->s_reserved_gdt_blocks)) {
124 com_err(program_name, 0,
125 _("Not enough reserved gdt blocks for resizing"));
126 exit(1);
127 }
128
129 if ((ext2fs_blocks_count(sb) > MAX_32_NUM) ||
130 (*new_size > MAX_32_NUM)) {
131 com_err(program_name, 0,
132 _("Kernel does not support resizing a file system this large"));
133 exit(1);
134 }
135 }
136
137 fd = open(mtpt, O_RDONLY);
138 if (fd < 0) {
139 com_err(program_name, errno,
140 _("while trying to open mountpoint %s"), mtpt);
141 exit(1);
142 }
143
144 if (no_resize_ioctl) {
145 printf(_("Old resize interface requested.\n"));
146 } else if (ioctl(fd, EXT4_IOC_RESIZE_FS, new_size)) {
147 /*
148 * If kernel does not support EXT4_IOC_RESIZE_FS, use the
149 * old online resize. Note that the old approach does not
150 * handle >32 bit file systems
151 *
152 * Sigh, if we are running a 32-bit binary on a 64-bit
153 * kernel (which happens all the time on the MIPS
154 * architecture in Debian, but can happen on other CPU
155 * architectures as well) we will get EINVAL returned
156 * when an ioctl doesn't exist, at least up to Linux
157 * 3.1. See compat_sys_ioctl() in fs/compat_ioctl.c
158 * in the kernel sources. This is probably a kernel
159 * bug, but work around it here.
160 */
161 if ((errno != ENOTTY) && (errno != EINVAL)) {
162 if (errno == EPERM)
163 com_err(program_name, 0,
164 _("Permission denied to resize filesystem"));
165 else
166 com_err(program_name, errno,
167 _("While checking for on-line resizing "
168 "support"));
169 exit(1);
170 }
171 } else {
172 close(fd);
173 return 0;
174 }
175
176 size = ext2fs_blocks_count(sb);
177
178 if (ioctl(fd, EXT2_IOC_GROUP_EXTEND, &size)) {
179 if (errno == EPERM)
180 com_err(program_name, 0,
181 _("Permission denied to resize filesystem"));
182 else if (errno == ENOTTY)
183 com_err(program_name, 0,
184 _("Kernel does not support online resizing"));
185 else
186 com_err(program_name, errno,
187 _("While checking for on-line resizing support"));
188 exit(1);
189 }
190
191 percent = (ext2fs_r_blocks_count(sb) * 100.0) /
192 ext2fs_blocks_count(sb);
193
194 retval = ext2fs_read_bitmaps(fs);
195 if (retval) {
196 close(fd);
197 return retval;
198 }
199
200 retval = ext2fs_dup_handle(fs, &new_fs);
201 if (retval) {
202 close(fd);
203 return retval;
204 }
205
206 /* The current method of adding one block group at a time to a
207 * mounted filesystem means it is impossible to accomodate the
208 * flex_bg allocation method of placing the metadata together
209 * in a single block group. For now we "fix" this issue by
210 * using the traditional layout for new block groups, where
211 * each block group is self-contained and contains its own
212 * bitmap blocks and inode tables. This means we don't get
213 * the layout advantages of flex_bg in the new block groups,
214 * but at least it allows on-line resizing to function.
215 */
216 new_fs->super->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_FLEX_BG;
217 retval = adjust_fs_info(new_fs, fs, 0, *new_size);
218 if (retval) {
219 close(fd);
220 return retval;
221 }
222
223 printf(_("Performing an on-line resize of %s to %llu (%dk) blocks.\n"),
224 fs->device_name, *new_size, fs->blocksize / 1024);
225
226 size = fs->group_desc_count * sb->s_blocks_per_group +
227 sb->s_first_data_block;
228 if (size > *new_size)
229 size = *new_size;
230
231 if (ioctl(fd, EXT2_IOC_GROUP_EXTEND, &size)) {
232 com_err(program_name, errno,
233 _("While trying to extend the last group"));
234 exit(1);
235 }
236
237 for (i = fs->group_desc_count;
238 i < new_fs->group_desc_count; i++) {
239
240 overhead = (int) (2 + new_fs->inode_blocks_per_group);
241
242 if (ext2fs_bg_has_super(new_fs, new_fs->group_desc_count - 1))
243 overhead += 1 + new_fs->desc_blocks +
244 new_fs->super->s_reserved_gdt_blocks;
245
246 input.group = i;
247 input.block_bitmap = ext2fs_block_bitmap_loc(new_fs, i);
248 input.inode_bitmap = ext2fs_inode_bitmap_loc(new_fs, i);
249 input.inode_table = ext2fs_inode_table_loc(new_fs, i);
250 input.blocks_count = ext2fs_group_blocks_count(new_fs, i);
251 input.reserved_blocks = (blk_t) (percent * input.blocks_count
252 / 100.0);
253
254 #if 0
255 printf("new block bitmap is at 0x%04x\n", input.block_bitmap);
256 printf("new inode bitmap is at 0x%04x\n", input.inode_bitmap);
257 printf("new inode table is at 0x%04x-0x%04x\n",
258 input.inode_table,
259 input.inode_table + new_fs->inode_blocks_per_group-1);
260 printf("new group has %u blocks\n", input.blocks_count);
261 printf("new group will reserve %d blocks\n",
262 input.reserved_blocks);
263 printf("new group has %d free blocks\n",
264 ext2fs_bg_free_blocks_count(new_fs, i),
265 printf("new group has %d free inodes (%d blocks)\n",
266 ext2fs_bg_free_inodes_count(new_fs, i),
267 new_fs->inode_blocks_per_group);
268 printf("Adding group #%d\n", input.group);
269 #endif
270
271 if (use_old_ioctl &&
272 ioctl(fd, EXT2_IOC_GROUP_ADD, &input) == 0)
273 continue;
274 else
275 use_old_ioctl = 0;
276
277 input64.group = input.group;
278 input64.block_bitmap = input.block_bitmap;
279 input64.inode_bitmap = input.inode_bitmap;
280 input64.inode_table = input.inode_table;
281 input64.blocks_count = input.blocks_count;
282 input64.reserved_blocks = input.reserved_blocks;
283 input64.unused = input.unused;
284
285 if (ioctl(fd, EXT4_IOC_GROUP_ADD, &input64) < 0) {
286 com_err(program_name, errno,
287 _("While trying to add group #%d"),
288 input.group);
289 exit(1);
290 }
291 }
292
293 ext2fs_free(new_fs);
294 close(fd);
295
296 return 0;
297 #else
298 printf(_("Filesystem at %s is mounted on %s, and on-line resizing is "
299 "not supported on this system.\n"), fs->device_name, mtpt);
300 exit(1);
301 #endif
302 }