2 * main.c --- ext2 resizer main program
4 * Copyright (C) 1997, 1998 by Theodore Ts'o and
7 * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 by Theodore Ts'o
10 * This file may be redistributed under the terms of the GNU Public
15 #define _LARGEFILE_SOURCE
16 #define _LARGEFILE64_SOURCE
29 #include <sys/types.h>
35 #include "resize2fs.h"
37 #include "../version.h"
39 char *program_name, *device_name, *io_options;
41 static void usage (char *prog)
43 fprintf (stderr, _("Usage: %s [-d debug_flags] [-f] [-F] [-M] [-P] "
44 "[-p] device [new_size]\n\n"), prog);
49 static errcode_t resize_progress_func(ext2_resize_t rfs, int pass,
50 unsigned long cur, unsigned long max)
52 ext2_sim_progmeter progress;
56 progress = (ext2_sim_progmeter) rfs->prog_data;
61 ext2fs_progress_close(progress);
64 case E2_RSZ_EXTEND_ITABLE_PASS:
65 label = _("Extending the inode table");
67 case E2_RSZ_BLOCK_RELOC_PASS:
68 label = _("Relocating blocks");
70 case E2_RSZ_INODE_SCAN_PASS:
71 label = _("Scanning inode table");
73 case E2_RSZ_INODE_REF_UPD_PASS:
74 label = _("Updating inode references");
76 case E2_RSZ_MOVE_ITABLE_PASS:
77 label = _("Moving inode table");
80 label = _("Unknown pass?!?");
83 printf(_("Begin pass %d (max = %lu)\n"), pass, max);
84 retval = ext2fs_progress_init(&progress, label, 30,
88 rfs->prog_data = (void *) progress;
91 ext2fs_progress_update(progress, cur);
94 ext2fs_progress_close(progress);
101 static void determine_fs_stride(ext2_filsys fs)
104 unsigned long long sum;
105 unsigned int has_sb, prev_has_sb, num;
106 int i_stride, b_stride;
111 for (group = 0; group < fs->group_desc_count; group++) {
112 has_sb = ext2fs_bg_has_super(fs, group);
113 if (group == 0 || has_sb != prev_has_sb)
115 b_stride = ext2fs_block_bitmap_loc(fs, group) -
116 ext2fs_block_bitmap_loc(fs, group - 1) -
117 fs->super->s_blocks_per_group;
118 i_stride = ext2fs_inode_bitmap_loc(fs, group) -
119 ext2fs_inode_bitmap_loc(fs, group - 1) -
120 fs->super->s_blocks_per_group;
121 if (b_stride != i_stride ||
125 /* printf("group %d has stride %d\n", group, b_stride); */
130 prev_has_sb = has_sb;
133 if (fs->group_desc_count > 12 && num < 3)
137 fs->stride = sum / num;
141 fs->super->s_raid_stride = fs->stride;
142 ext2fs_mark_super_dirty(fs);
146 printf("Using RAID stride of %d\n", fs->stride);
150 int main (int argc, char ** argv)
159 int force_min_size = 0;
160 int print_min_size = 0;
162 blk64_t new_size = 0;
163 blk64_t max_size = 0;
164 blk64_t min_size = 0;
166 char *new_size_str = 0;
168 ext2fs_struct_stat st_buf;
170 unsigned int sys_page_size = 4096;
172 int len, mount_flags;
176 setlocale(LC_MESSAGES, "");
177 setlocale(LC_CTYPE, "");
178 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
179 textdomain(NLS_CAT_NAME);
180 set_com_err_gettext(gettext);
183 add_error_table(&et_ext2_error_table);
185 fprintf (stderr, "resize2fs %s (%s)\n",
186 E2FSPROGS_VERSION, E2FSPROGS_DATE);
188 program_name = *argv;
190 while ((c = getopt (argc, argv, "d:fFhMPpS:")) != EOF) {
208 flags |= atoi(optarg);
211 flags |= RESIZE_PERCENT_COMPLETE;
214 use_stride = atoi(optarg);
223 device_name = argv[optind++];
225 new_size_str = argv[optind++];
229 io_options = strchr(device_name, '?');
234 * Figure out whether or not the device is mounted, and if it is
235 * where it is mounted.
243 retval = ext2fs_check_mount_point(device_name, &mount_flags,
246 com_err("ext2fs_check_mount_point", retval,
247 _("while determining whether %s is mounted."),
251 if (!(mount_flags & EXT2_MF_MOUNTED) || (mtpt[len-1] == 0))
257 fd = ext2fs_open_file(device_name, O_RDWR, 0);
259 com_err("open", errno, _("while opening %s"),
264 ret = ext2fs_fstat(fd, &st_buf);
266 com_err("open", errno,
267 _("while getting stat information for %s"),
273 retval = ext2fs_sync_device(fd, 1);
275 com_err(argv[0], retval,
276 _("while trying to flush %s"),
282 if (!S_ISREG(st_buf.st_mode )) {
287 #ifdef CONFIG_TESTIO_DEBUG
288 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
289 io_ptr = test_io_manager;
290 test_io_backing_manager = unix_io_manager;
293 io_ptr = unix_io_manager;
295 if (!(mount_flags & EXT2_MF_MOUNTED))
296 io_flags = EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE;
298 io_flags |= EXT2_FLAG_64BITS;
300 retval = ext2fs_open2(device_name, io_options, io_flags,
303 com_err (program_name, retval, _("while trying to open %s"),
305 printf (_("Couldn't find valid filesystem superblock.\n"));
310 * Check for compatibility with the feature sets. We need to
311 * be more stringent than ext2fs_open().
313 if (fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) {
314 com_err(program_name, EXT2_ET_UNSUPP_FEATURE,
315 "(%s)", device_name);
319 min_size = calculate_minimum_resize_size(fs);
321 if (print_min_size) {
322 if (!force && ((fs->super->s_state & EXT2_ERROR_FS) ||
323 ((fs->super->s_state & EXT2_VALID_FS) == 0))) {
325 _("Please run 'e2fsck -f %s' first.\n\n"),
329 printf(_("Estimated minimum size of the filesystem: %llu\n"),
334 /* Determine the system page size if possible */
336 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
337 #define _SC_PAGESIZE _SC_PAGE_SIZE
340 sysval = sysconf(_SC_PAGESIZE);
342 sys_page_size = sysval;
343 #endif /* _SC_PAGESIZE */
344 #endif /* HAVE_SYSCONF */
347 * Get the size of the containing partition, and use this for
348 * defaults and for making sure the new filesystem doesn't
349 * exceed the partition size.
351 retval = ext2fs_get_device_size2(device_name, fs->blocksize,
354 com_err(program_name, retval,
355 _("while trying to determine filesystem size"));
360 else if (new_size_str) {
361 new_size = parse_num_blocks2(new_size_str,
362 fs->super->s_log_block_size);
364 com_err(program_name, 0,
365 _("Invalid new size: %s\n"), new_size_str);
370 /* Round down to an even multiple of a pagesize */
371 if (sys_page_size > fs->blocksize)
372 new_size &= ~((sys_page_size / fs->blocksize)-1);
374 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
375 EXT4_FEATURE_INCOMPAT_64BIT)) {
376 /* Take 16T down to 2^32-1 blocks */
377 if (new_size == (1ULL << 32))
379 else if (new_size > (1ULL << 32)) {
380 com_err(program_name, 0,
381 _("New size too large to be "
382 "expressed in 32 bits\n"));
387 if (!force && new_size < min_size) {
388 com_err(program_name, 0,
389 _("New size smaller than minimum (%llu)\n"), min_size);
392 if (use_stride >= 0) {
393 if (use_stride >= (int) fs->super->s_blocks_per_group) {
394 com_err(program_name, 0,
395 _("Invalid stride length"));
398 fs->stride = fs->super->s_raid_stride = use_stride;
399 ext2fs_mark_super_dirty(fs);
401 determine_fs_stride(fs);
404 * If we are resizing a plain file, and it's not big enough,
405 * automatically extend it in a sparse fashion by writing the
406 * last requested block.
408 new_file_size = ((__u64) new_size) * fs->blocksize;
409 if ((__u64) new_file_size >
410 (((__u64) 1) << (sizeof(st_buf.st_size)*8 - 1)) - 1)
412 if ((new_file_size > st_buf.st_size) &&
414 if ((ext2fs_llseek(fd, new_file_size-1, SEEK_SET) >= 0) &&
415 (write(fd, "0", 1) == 1))
418 if (!force && (new_size > max_size)) {
419 fprintf(stderr, _("The containing partition (or device)"
420 " is only %llu (%dk) blocks.\nYou requested a new size"
421 " of %llu blocks.\n\n"), max_size,
422 fs->blocksize / 1024, new_size);
425 if (new_size == ext2fs_blocks_count(fs->super)) {
426 fprintf(stderr, _("The filesystem is already %llu blocks "
427 "long. Nothing to do!\n\n"), new_size);
430 if (mount_flags & EXT2_MF_MOUNTED) {
431 retval = online_resize_fs(fs, mtpt, &new_size, flags);
433 if (!force && ((fs->super->s_lastcheck < fs->super->s_mtime) ||
434 (fs->super->s_state & EXT2_ERROR_FS) ||
435 ((fs->super->s_state & EXT2_VALID_FS) == 0))) {
437 _("Please run 'e2fsck -f %s' first.\n\n"),
442 * XXXX The combination of flex_bg and !resize_inode
443 * causes major problems for resize2fs, since when the
444 * group descriptors grow in size this can potentially
445 * require multiple inode tables to be moved aside to
446 * make room, and resize2fs chokes rather badly in
447 * this scenario. It's a rare combination, except
448 * when a filesystem is expanded more than a certain
449 * size, so for now, we'll just prohibit that
450 * combination. This is something we should fix
451 * eventually, though.
453 if ((fs->super->s_feature_incompat &
454 EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
455 !(fs->super->s_feature_compat &
456 EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
457 com_err(program_name, 0, _("%s: The combination of "
458 "flex_bg and\n\t!resize_inode features "
459 "is not supported by resize2fs.\n"),
463 printf(_("Resizing the filesystem on "
464 "%s to %llu (%dk) blocks.\n"),
465 device_name, new_size, fs->blocksize / 1024);
466 retval = resize_fs(fs, &new_size, flags,
467 ((flags & RESIZE_PERCENT_COMPLETE) ?
468 resize_progress_func : 0));
472 com_err(program_name, retval, _("while trying to resize %s"),
475 _("Please run 'e2fsck -fy %s' to fix the filesystem\n"
476 "after the aborted resize operation.\n"),
481 printf(_("The filesystem on %s is now %llu blocks long.\n\n"),
482 device_name, new_size);
484 if ((st_buf.st_size > new_file_size) &&
486 #ifdef HAVE_FTRUNCATE64
487 retval = ftruncate64(fd, new_file_size);
490 /* Only truncate if new_file_size doesn't overflow off_t */
491 if (((off_t) new_file_size) == new_file_size)
492 retval = ftruncate(fd, (off_t) new_file_size);
495 com_err(program_name, retval,
496 _("while trying to truncate %s"),
501 remove_error_table(&et_ext2_error_table);