]>
Commit | Line | Data |
---|---|---|
24b2c7a7 TT |
1 | /* |
2 | * main.c --- ext2 resizer main program | |
3 | * | |
0cee8a5c TT |
4 | * Copyright (C) 1997, 1998 by Theodore Ts'o and |
5 | * PowerQuest, Inc. | |
6 | * | |
55f4cbd9 | 7 | * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 by Theodore Ts'o |
efc6f628 | 8 | * |
24b2c7a7 | 9 | * %Begin-Header% |
0cee8a5c TT |
10 | * This file may be redistributed under the terms of the GNU Public |
11 | * License. | |
24b2c7a7 TT |
12 | * %End-Header% |
13 | */ | |
14 | ||
fe07357f TT |
15 | #define _LARGEFILE_SOURCE |
16 | #define _LARGEFILE64_SOURCE | |
17 | ||
c762c8e6 TT |
18 | #ifdef HAVE_GETOPT_H |
19 | #include <getopt.h> | |
373b8337 TT |
20 | #else |
21 | extern char *optarg; | |
22 | extern int optind; | |
c762c8e6 | 23 | #endif |
fe07357f | 24 | #include <unistd.h> |
f38cf3cb TT |
25 | #ifdef HAVE_STDLIB_H |
26 | #include <stdlib.h> | |
27 | #endif | |
fe07357f | 28 | #include <sys/types.h> |
116db1b5 | 29 | #include <sys/stat.h> |
fe07357f | 30 | #include <fcntl.h> |
c762c8e6 | 31 | |
55f4cbd9 TT |
32 | #include "e2p/e2p.h" |
33 | ||
24b2c7a7 TT |
34 | #include "resize2fs.h" |
35 | ||
0cee8a5c | 36 | #include "../version.h" |
2e561e02 | 37 | |
2e8ca9a2 | 38 | char *program_name, *device_name, *io_options; |
24b2c7a7 | 39 | |
dfcdc32f | 40 | static void usage (char *prog) |
24b2c7a7 | 41 | { |
199ddaaa JB |
42 | fprintf (stderr, _("Usage: %s [-d debug_flags] [-f] [-F] [-M] [-P] " |
43 | "[-p] device [new_size]\n\n"), prog); | |
2e561e02 | 44 | |
24b2c7a7 TT |
45 | exit (1); |
46 | } | |
47 | ||
3b627e8d TT |
48 | static errcode_t resize_progress_func(ext2_resize_t rfs, int pass, |
49 | unsigned long cur, unsigned long max) | |
63b44fbe TT |
50 | { |
51 | ext2_sim_progmeter progress; | |
52 | const char *label; | |
53 | errcode_t retval; | |
54 | ||
55 | progress = (ext2_sim_progmeter) rfs->prog_data; | |
f4b2a6db | 56 | if (max == 0) |
3b627e8d | 57 | return 0; |
63b44fbe TT |
58 | if (cur == 0) { |
59 | if (progress) | |
60 | ext2fs_progress_close(progress); | |
61 | progress = 0; | |
62 | switch (pass) { | |
a8519a2d | 63 | case E2_RSZ_EXTEND_ITABLE_PASS: |
a13575f4 | 64 | label = _("Extending the inode table"); |
63b44fbe TT |
65 | break; |
66 | case E2_RSZ_BLOCK_RELOC_PASS: | |
a13575f4 | 67 | label = _("Relocating blocks"); |
63b44fbe | 68 | break; |
a8519a2d | 69 | case E2_RSZ_INODE_SCAN_PASS: |
a13575f4 | 70 | label = _("Scanning inode table"); |
63b44fbe TT |
71 | break; |
72 | case E2_RSZ_INODE_REF_UPD_PASS: | |
a13575f4 | 73 | label = _("Updating inode references"); |
63b44fbe TT |
74 | break; |
75 | case E2_RSZ_MOVE_ITABLE_PASS: | |
a13575f4 | 76 | label = _("Moving inode table"); |
63b44fbe | 77 | break; |
a8519a2d | 78 | default: |
a13575f4 | 79 | label = _("Unknown pass?!?"); |
a8519a2d | 80 | break; |
63b44fbe | 81 | } |
a13575f4 | 82 | printf(_("Begin pass %d (max = %lu)\n"), pass, max); |
63b44fbe TT |
83 | retval = ext2fs_progress_init(&progress, label, 30, |
84 | 40, max, 0); | |
85 | if (retval) | |
86 | progress = 0; | |
87 | rfs->prog_data = (void *) progress; | |
88 | } | |
89 | if (progress) | |
90 | ext2fs_progress_update(progress, cur); | |
91 | if (cur >= max) { | |
92 | if (progress) | |
93 | ext2fs_progress_close(progress); | |
94 | progress = 0; | |
95 | rfs->prog_data = 0; | |
96 | } | |
3b627e8d | 97 | return 0; |
63b44fbe TT |
98 | } |
99 | ||
46c5490d TT |
100 | static void determine_fs_stride(ext2_filsys fs) |
101 | { | |
102 | unsigned int group; | |
103 | unsigned long long sum; | |
104 | unsigned int has_sb, prev_has_sb, num; | |
105 | int i_stride, b_stride; | |
106 | ||
96c6a3ac TT |
107 | if (fs->stride) |
108 | return; | |
46c5490d TT |
109 | num = 0; sum = 0; |
110 | for (group = 0; group < fs->group_desc_count; group++) { | |
111 | has_sb = ext2fs_bg_has_super(fs, group); | |
112 | if (group == 0 || has_sb != prev_has_sb) | |
113 | goto next; | |
efc6f628 TT |
114 | b_stride = fs->group_desc[group].bg_block_bitmap - |
115 | fs->group_desc[group-1].bg_block_bitmap - | |
46c5490d | 116 | fs->super->s_blocks_per_group; |
efc6f628 TT |
117 | i_stride = fs->group_desc[group].bg_inode_bitmap - |
118 | fs->group_desc[group-1].bg_inode_bitmap - | |
46c5490d TT |
119 | fs->super->s_blocks_per_group; |
120 | if (b_stride != i_stride || | |
121 | b_stride < 0) | |
122 | goto next; | |
123 | ||
124 | /* printf("group %d has stride %d\n", group, b_stride); */ | |
125 | sum += b_stride; | |
126 | num++; | |
efc6f628 | 127 | |
46c5490d TT |
128 | next: |
129 | prev_has_sb = has_sb; | |
130 | } | |
131 | ||
132 | if (fs->group_desc_count > 12 && num < 3) | |
133 | sum = 0; | |
134 | ||
135 | if (num) | |
136 | fs->stride = sum / num; | |
137 | else | |
138 | fs->stride = 0; | |
139 | ||
96c6a3ac TT |
140 | fs->super->s_raid_stride = fs->stride; |
141 | ext2fs_mark_super_dirty(fs); | |
142 | ||
46c5490d TT |
143 | #if 0 |
144 | if (fs->stride) | |
145 | printf("Using RAID stride of %d\n", fs->stride); | |
146 | #endif | |
147 | } | |
148 | ||
0a617317 | 149 | int main (int argc, char ** argv) |
24b2c7a7 TT |
150 | { |
151 | errcode_t retval; | |
152 | ext2_filsys fs; | |
153 | int c; | |
05e112a1 | 154 | int flags = 0; |
c762c8e6 | 155 | int flush = 0; |
f4b2a6db | 156 | int force = 0; |
5d2ef12f | 157 | int io_flags = 0; |
199ddaaa JB |
158 | int force_min_size = 0; |
159 | int print_min_size = 0; | |
fe07357f | 160 | int fd, ret; |
f4b2a6db TT |
161 | blk_t new_size = 0; |
162 | blk_t max_size = 0; | |
24b2c7a7 | 163 | io_manager io_ptr; |
55f4cbd9 | 164 | char *new_size_str = 0; |
46c5490d | 165 | int use_stride = -1; |
fe07357f TT |
166 | #ifdef HAVE_FSTAT64 |
167 | struct stat64 st_buf; | |
168 | #else | |
116db1b5 | 169 | struct stat st_buf; |
fe07357f TT |
170 | #endif |
171 | __s64 new_file_size; | |
54434927 | 172 | unsigned int sys_page_size = 4096; |
a7ccdff8 | 173 | long sysval; |
bf69235a TT |
174 | int len, mount_flags; |
175 | char *mtpt; | |
ddc32a04 TT |
176 | |
177 | #ifdef ENABLE_NLS | |
178 | setlocale(LC_MESSAGES, ""); | |
179 | setlocale(LC_CTYPE, ""); | |
180 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); | |
181 | textdomain(NLS_CAT_NAME); | |
182 | #endif | |
183 | ||
a6d8302b | 184 | add_error_table(&et_ext2_error_table); |
24b2c7a7 | 185 | |
f35fd3d5 | 186 | fprintf (stderr, "resize2fs %s (%s)\n", |
ba0af756 | 187 | E2FSPROGS_VERSION, E2FSPROGS_DATE); |
24b2c7a7 TT |
188 | if (argc && *argv) |
189 | program_name = *argv; | |
2e561e02 | 190 | |
199ddaaa | 191 | while ((c = getopt (argc, argv, "d:fFhMPpS:")) != EOF) { |
24b2c7a7 TT |
192 | switch (c) { |
193 | case 'h': | |
194 | usage(program_name); | |
195 | break; | |
f4b2a6db TT |
196 | case 'f': |
197 | force = 1; | |
198 | break; | |
c762c8e6 TT |
199 | case 'F': |
200 | flush = 1; | |
201 | break; | |
199ddaaa JB |
202 | case 'M': |
203 | force_min_size = 1; | |
204 | break; | |
205 | case 'P': | |
206 | print_min_size = 1; | |
207 | break; | |
05e112a1 TT |
208 | case 'd': |
209 | flags |= atoi(optarg); | |
210 | break; | |
211 | case 'p': | |
212 | flags |= RESIZE_PERCENT_COMPLETE; | |
213 | break; | |
46c5490d TT |
214 | case 'S': |
215 | use_stride = atoi(optarg); | |
216 | break; | |
24b2c7a7 | 217 | default: |
f4b2a6db | 218 | usage(program_name); |
24b2c7a7 TT |
219 | } |
220 | } | |
f4b2a6db TT |
221 | if (optind == argc) |
222 | usage(program_name); | |
2e561e02 | 223 | |
24b2c7a7 | 224 | device_name = argv[optind++]; |
55f4cbd9 TT |
225 | if (optind < argc) |
226 | new_size_str = argv[optind++]; | |
f4b2a6db TT |
227 | if (optind < argc) |
228 | usage(program_name); | |
efc6f628 | 229 | |
2e8ca9a2 TT |
230 | io_options = strchr(device_name, '?'); |
231 | if (io_options) | |
232 | *io_options++ = 0; | |
233 | ||
bf69235a TT |
234 | /* |
235 | * Figure out whether or not the device is mounted, and if it is | |
236 | * where it is mounted. | |
237 | */ | |
238 | len=80; | |
239 | while (1) { | |
240 | mtpt = malloc(len); | |
241 | if (!mtpt) | |
242 | return ENOMEM; | |
243 | mtpt[len-1] = 0; | |
efc6f628 | 244 | retval = ext2fs_check_mount_point(device_name, &mount_flags, |
bf69235a TT |
245 | mtpt, len); |
246 | if (retval) { | |
247 | com_err("ext2fs_check_mount_point", retval, | |
248 | _("while determining whether %s is mounted."), | |
249 | device_name); | |
250 | exit(1); | |
251 | } | |
252 | if (!(mount_flags & EXT2_MF_MOUNTED) || (mtpt[len-1] == 0)) | |
253 | break; | |
254 | free(mtpt); | |
255 | len = 2 * len; | |
256 | } | |
fe07357f TT |
257 | |
258 | #ifdef HAVE_OPEN64 | |
259 | fd = open64(device_name, O_RDWR); | |
260 | #else | |
261 | fd = open(device_name, O_RDWR); | |
262 | #endif | |
263 | if (fd < 0) { | |
264 | com_err("open", errno, _("while opening %s"), | |
265 | device_name); | |
266 | exit(1); | |
267 | } | |
268 | ||
269 | #ifdef HAVE_FSTAT64 | |
270 | ret = fstat64(fd, &st_buf); | |
271 | #else | |
272 | ret = fstat(fd, &st_buf); | |
273 | #endif | |
274 | if (ret < 0) { | |
efc6f628 | 275 | com_err("open", errno, |
fe07357f TT |
276 | _("while getting stat information for %s"), |
277 | device_name); | |
278 | exit(1); | |
279 | } | |
efc6f628 | 280 | |
c762c8e6 | 281 | if (flush) { |
48e08e03 TT |
282 | retval = ext2fs_sync_device(fd, 1); |
283 | if (retval) { | |
efc6f628 | 284 | com_err(argv[0], retval, |
a13575f4 | 285 | _("while trying to flush %s"), |
c762c8e6 TT |
286 | device_name); |
287 | exit(1); | |
288 | } | |
fe07357f TT |
289 | } |
290 | ||
291 | if (!S_ISREG(st_buf.st_mode )) { | |
c762c8e6 | 292 | close(fd); |
fe07357f | 293 | fd = -1; |
c762c8e6 TT |
294 | } |
295 | ||
f38cf3cb TT |
296 | #ifdef CONFIG_TESTIO_DEBUG |
297 | if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) { | |
05e112a1 TT |
298 | io_ptr = test_io_manager; |
299 | test_io_backing_manager = unix_io_manager; | |
efc6f628 | 300 | } else |
f38cf3cb | 301 | #endif |
05e112a1 TT |
302 | io_ptr = unix_io_manager; |
303 | ||
5d2ef12f TT |
304 | if (!(mount_flags & EXT2_MF_MOUNTED)) |
305 | io_flags = EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE; | |
efc6f628 | 306 | retval = ext2fs_open2(device_name, io_options, io_flags, |
2e8ca9a2 | 307 | 0, 0, io_ptr, &fs); |
24b2c7a7 | 308 | if (retval) { |
a13575f4 | 309 | com_err (program_name, retval, _("while trying to open %s"), |
24b2c7a7 | 310 | device_name); |
a13575f4 | 311 | printf (_("Couldn't find valid filesystem superblock.\n")); |
24b2c7a7 TT |
312 | exit (1); |
313 | } | |
236efede | 314 | |
101c84f2 TT |
315 | /* |
316 | * Check for compatibility with the feature sets. We need to | |
317 | * be more stringent than ext2fs_open(). | |
318 | */ | |
76dd5e5c | 319 | if (fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) { |
101c84f2 TT |
320 | com_err(program_name, EXT2_ET_UNSUPP_FEATURE, |
321 | "(%s)", device_name); | |
322 | exit(1); | |
323 | } | |
a8d632a4 TT |
324 | |
325 | /* | |
326 | * XXXX The combination of flex_bg and !resize_inode causes | |
327 | * major problems for resize2fs, since when the group descriptors | |
328 | * grow in size this can potentially require multiple inode | |
329 | * tables to be moved aside to make room, and resize2fs chokes | |
330 | * rather badly in this scenario. It's a rare combination, | |
331 | * except when a filesystem is expanded more than a certain | |
332 | * size, so for now, we'll just prohibit that combination. | |
333 | * This is something we should fix eventually, though. | |
334 | */ | |
335 | if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) && | |
336 | !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) { | |
337 | com_err(program_name, 0, _("%s: The combination of flex_bg " | |
338 | "and\n\t!resize_inode features " | |
339 | "is not supported by resize2fs.\n"), | |
340 | device_name); | |
341 | exit(1); | |
342 | } | |
efc6f628 | 343 | |
199ddaaa | 344 | if (print_min_size) { |
a8d632a4 | 345 | printf(_("Estimated minimum size of the filesystem: %u\n"), |
199ddaaa JB |
346 | calculate_minimum_resize_size(fs)); |
347 | exit(0); | |
348 | } | |
349 | ||
a7ccdff8 TT |
350 | /* Determine the system page size if possible */ |
351 | #ifdef HAVE_SYSCONF | |
352 | #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE)) | |
353 | #define _SC_PAGESIZE _SC_PAGE_SIZE | |
354 | #endif | |
355 | #ifdef _SC_PAGESIZE | |
356 | sysval = sysconf(_SC_PAGESIZE); | |
357 | if (sysval > 0) | |
358 | sys_page_size = sysval; | |
359 | #endif /* _SC_PAGESIZE */ | |
360 | #endif /* HAVE_SYSCONF */ | |
361 | ||
f4b2a6db TT |
362 | /* |
363 | * Get the size of the containing partition, and use this for | |
fe07357f | 364 | * defaults and for making sure the new filesystem doesn't |
f4b2a6db TT |
365 | * exceed the partition size. |
366 | */ | |
367 | retval = ext2fs_get_device_size(device_name, fs->blocksize, | |
368 | &max_size); | |
369 | if (retval) { | |
370 | com_err(program_name, retval, | |
a13575f4 | 371 | _("while trying to determine filesystem size")); |
f4b2a6db TT |
372 | exit(1); |
373 | } | |
199ddaaa JB |
374 | if (force_min_size) |
375 | new_size = calculate_minimum_resize_size(fs); | |
376 | else if (new_size_str) { | |
efc6f628 | 377 | new_size = parse_num_blocks(new_size_str, |
55f4cbd9 | 378 | fs->super->s_log_block_size); |
55f4cbd9 | 379 | } else { |
f4b2a6db | 380 | new_size = max_size; |
a7ccdff8 TT |
381 | /* Round down to an even multiple of a pagesize */ |
382 | if (sys_page_size > fs->blocksize) | |
383 | new_size &= ~((sys_page_size / fs->blocksize)-1); | |
384 | } | |
46c5490d TT |
385 | |
386 | if (use_stride >= 0) { | |
de8f3a76 | 387 | if (use_stride >= (int) fs->super->s_blocks_per_group) { |
efc6f628 | 388 | com_err(program_name, 0, |
46c5490d TT |
389 | _("Invalid stride length")); |
390 | exit(1); | |
391 | } | |
96c6a3ac TT |
392 | fs->stride = fs->super->s_raid_stride = use_stride; |
393 | ext2fs_mark_super_dirty(fs); | |
46c5490d TT |
394 | } else |
395 | determine_fs_stride(fs); | |
efc6f628 | 396 | |
116db1b5 TT |
397 | /* |
398 | * If we are resizing a plain file, and it's not big enough, | |
399 | * automatically extend it in a sparse fashion by writing the | |
400 | * last requested block. | |
401 | */ | |
fe07357f | 402 | new_file_size = ((__u64) new_size) * fs->blocksize; |
efc6f628 | 403 | if ((__u64) new_file_size > |
fe07357f TT |
404 | (((__u64) 1) << (sizeof(st_buf.st_size)*8 - 1)) - 1) |
405 | fd = -1; | |
406 | if ((new_file_size > st_buf.st_size) && | |
407 | (fd > 0)) { | |
408 | if ((ext2fs_llseek(fd, new_file_size-1, SEEK_SET) >= 0) && | |
409 | (write(fd, "0", 1) == 1)) | |
116db1b5 | 410 | max_size = new_size; |
116db1b5 | 411 | } |
f4b2a6db | 412 | if (!force && (new_size > max_size)) { |
a13575f4 | 413 | fprintf(stderr, _("The containing partition (or device)" |
8deb80a5 TS |
414 | " is only %u (%dk) blocks.\nYou requested a new size" |
415 | " of %u blocks.\n\n"), max_size, | |
792a0881 | 416 | fs->blocksize / 1024, new_size); |
f4b2a6db TT |
417 | exit(1); |
418 | } | |
419 | if (new_size == fs->super->s_blocks_count) { | |
8deb80a5 | 420 | fprintf(stderr, _("The filesystem is already %u blocks " |
a13575f4 | 421 | "long. Nothing to do!\n\n"), new_size); |
f4b2a6db TT |
422 | exit(0); |
423 | } | |
bf69235a TT |
424 | if (mount_flags & EXT2_MF_MOUNTED) { |
425 | retval = online_resize_fs(fs, mtpt, &new_size, flags); | |
426 | } else { | |
427 | if (!force && ((fs->super->s_lastcheck < fs->super->s_mtime) || | |
428 | (fs->super->s_state & EXT2_ERROR_FS) || | |
429 | ((fs->super->s_state & EXT2_VALID_FS) == 0))) { | |
efc6f628 | 430 | fprintf(stderr, |
bf69235a TT |
431 | _("Please run 'e2fsck -f %s' first.\n\n"), |
432 | device_name); | |
433 | exit(1); | |
434 | } | |
8deb80a5 | 435 | printf("Resizing the filesystem on %s to %u (%dk) blocks.\n", |
bf69235a TT |
436 | device_name, new_size, fs->blocksize / 1024); |
437 | retval = resize_fs(fs, &new_size, flags, | |
438 | ((flags & RESIZE_PERCENT_COMPLETE) ? | |
439 | resize_progress_func : 0)); | |
f4b2a6db | 440 | } |
1e1da29f | 441 | if (retval) { |
a13575f4 | 442 | com_err(program_name, retval, _("while trying to resize %s"), |
1e1da29f TT |
443 | device_name); |
444 | ext2fs_close (fs); | |
16082112 | 445 | exit(1); |
1e1da29f | 446 | } |
8deb80a5 | 447 | printf(_("The filesystem on %s is now %u blocks long.\n\n"), |
7e71e4c5 | 448 | device_name, new_size); |
fe07357f TT |
449 | |
450 | if ((st_buf.st_size > new_file_size) && | |
451 | (fd > 0)) { | |
261cd396 | 452 | #ifdef HAVE_FTRUNCATE64 |
fe07357f TT |
453 | ftruncate64(fd, new_file_size); |
454 | #else | |
98dca6c5 TT |
455 | /* Only truncate if new_file_size doesn't overflow off_t */ |
456 | if (((off_t) new_file_size) == new_file_size) | |
457 | ftruncate(fd, (off_t) new_file_size); | |
fe07357f TT |
458 | #endif |
459 | } | |
460 | if (fd > 0) | |
461 | close(fd); | |
a6d8302b | 462 | remove_error_table(&et_ext2_error_table); |
0a617317 | 463 | return (0); |
24b2c7a7 | 464 | } |