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 #ifndef _LARGEFILE_SOURCE
16 #define _LARGEFILE_SOURCE
18 #ifndef _LARGEFILE64_SOURCE
19 #define _LARGEFILE64_SOURCE
33 #include <sys/types.h>
40 #include "resize2fs.h"
42 #include "../version.h"
45 static char *device_name, *io_options;
47 static void usage (char *prog)
49 fprintf (stderr, _("Usage: %s [-d debug_flags] [-f] [-F] [-M] [-P] "
50 "[-p] device [-b|-s|new_size] [-z undo_file]\n\n"),
56 static errcode_t resize_progress_func(ext2_resize_t rfs, int pass,
57 unsigned long cur, unsigned long max)
59 ext2_sim_progmeter progress;
63 progress = (ext2_sim_progmeter) rfs->prog_data;
68 ext2fs_progress_close(progress);
71 case E2_RSZ_EXTEND_ITABLE_PASS:
72 label = _("Extending the inode table");
74 case E2_RSZ_BLOCK_RELOC_PASS:
75 label = _("Relocating blocks");
77 case E2_RSZ_INODE_SCAN_PASS:
78 label = _("Scanning inode table");
80 case E2_RSZ_INODE_REF_UPD_PASS:
81 label = _("Updating inode references");
83 case E2_RSZ_MOVE_ITABLE_PASS:
84 label = _("Moving inode table");
87 label = _("Unknown pass?!?");
90 printf(_("Begin pass %d (max = %lu)\n"), pass, max);
91 retval = ext2fs_progress_init(&progress, label, 30,
95 rfs->prog_data = (void *) progress;
98 ext2fs_progress_update(progress, cur);
101 ext2fs_progress_close(progress);
108 static void determine_fs_stride(ext2_filsys fs)
111 unsigned long long sum;
112 unsigned int has_sb, prev_has_sb = 0, num;
113 int i_stride, b_stride;
114 int flexbg_size = 1 << fs->super->s_log_groups_per_flex;
119 for (group = 0; group < fs->group_desc_count; group++) {
120 has_sb = ext2fs_bg_has_super(fs, group);
121 if (group == 0 || has_sb != prev_has_sb)
123 b_stride = ext2fs_block_bitmap_loc(fs, group) -
124 ext2fs_block_bitmap_loc(fs, group - 1) -
125 fs->super->s_blocks_per_group;
126 i_stride = ext2fs_inode_bitmap_loc(fs, group) -
127 ext2fs_inode_bitmap_loc(fs, group - 1) -
128 fs->super->s_blocks_per_group;
129 if (b_stride != i_stride ||
131 (flexbg_size > 1 && (group % flexbg_size == 0)))
134 /* printf("group %d has stride %d\n", group, b_stride); */
139 prev_has_sb = has_sb;
142 if (fs->group_desc_count > 12 && num < 3)
146 fs->stride = sum / num;
150 fs->super->s_raid_stride = fs->stride;
151 ext2fs_mark_super_dirty(fs);
155 printf("Using RAID stride of %d\n", fs->stride);
159 static void bigalloc_check(ext2_filsys fs, int force)
161 if (!force && ext2fs_has_feature_bigalloc(fs->super)) {
162 fprintf(stderr, "%s", _("\nResizing bigalloc file systems has "
163 "not been fully tested. Proceed at\n"
164 "your own risk! Use the force option "
165 "if you want to go ahead anyway.\n\n"));
170 static int resize2fs_setup_tdb(const char *device, char *undo_file,
173 errcode_t retval = ENOMEM;
174 const char *tdb_dir = NULL;
175 char *tdb_file = NULL;
176 char *dev_name, *tmp_name;
178 /* (re)open a specific undo file */
179 if (undo_file && undo_file[0] != 0) {
180 retval = set_undo_io_backing_manager(*io_ptr);
183 *io_ptr = undo_io_manager;
184 retval = set_undo_io_backup_file(undo_file);
187 printf(_("Overwriting existing filesystem; this can be undone "
188 "using the command:\n"
189 " e2undo %s %s\n\n"),
195 * Configuration via a conf file would be
198 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
200 tdb_dir = "/var/lib/e2fsprogs";
202 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
203 access(tdb_dir, W_OK))
206 tmp_name = strdup(device);
209 dev_name = basename(tmp_name);
210 tdb_file = malloc(strlen(tdb_dir) + 11 + strlen(dev_name) + 7 + 1);
215 sprintf(tdb_file, "%s/resize2fs-%s.e2undo", tdb_dir, dev_name);
218 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
220 com_err(program_name, retval,
221 _("while trying to delete %s"), tdb_file);
225 retval = set_undo_io_backing_manager(*io_ptr);
228 *io_ptr = undo_io_manager;
229 retval = set_undo_io_backup_file(tdb_file);
232 printf(_("Overwriting existing filesystem; this can be undone "
233 "using the command:\n"
234 " e2undo %s %s\n\n"), tdb_file, device);
241 com_err(program_name, retval, "%s",
242 _("while trying to setup undo file\n"));
246 int main (int argc, char ** argv)
255 int force_min_size = 0;
256 int print_min_size = 0;
258 blk64_t new_size = 0;
259 blk64_t max_size = 0;
260 blk64_t min_size = 0;
262 char *new_size_str = 0;
264 ext2fs_struct_stat st_buf;
266 unsigned int sys_page_size = 4096;
267 unsigned int blocksize;
269 int len, mount_flags;
270 char *mtpt, *undo_file = NULL;
273 setlocale(LC_MESSAGES, "");
274 setlocale(LC_CTYPE, "");
275 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
276 textdomain(NLS_CAT_NAME);
277 set_com_err_gettext(gettext);
280 add_error_table(&et_ext2_error_table);
282 fprintf (stderr, "resize2fs %s (%s)\n",
283 E2FSPROGS_VERSION, E2FSPROGS_DATE);
285 program_name = *argv;
287 while ((c = getopt(argc, argv, "d:fFhMPpS:bsz:")) != EOF) {
305 flags |= atoi(optarg);
308 flags |= RESIZE_PERCENT_COMPLETE;
311 use_stride = atoi(optarg);
314 flags |= RESIZE_ENABLE_64BIT;
317 flags |= RESIZE_DISABLE_64BIT;
329 device_name = argv[optind++];
331 new_size_str = argv[optind++];
335 io_options = strchr(device_name, '?');
340 * Figure out whether or not the device is mounted, and if it is
341 * where it is mounted.
349 retval = ext2fs_check_mount_point(device_name, &mount_flags,
352 com_err("ext2fs_check_mount_point", retval,
353 _("while determining whether %s is mounted."),
357 if (!(mount_flags & EXT2_MF_MOUNTED) || (mtpt[len-1] == 0))
363 fd = ext2fs_open_file(device_name, O_RDWR, 0);
365 com_err("open", errno, _("while opening %s"),
370 ret = ext2fs_fstat(fd, &st_buf);
372 com_err("open", errno,
373 _("while getting stat information for %s"),
379 retval = ext2fs_sync_device(fd, 1);
381 com_err(argv[0], retval,
382 _("while trying to flush %s"),
388 if (!S_ISREG(st_buf.st_mode )) {
393 #ifdef CONFIG_TESTIO_DEBUG
394 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
395 io_ptr = test_io_manager;
396 test_io_backing_manager = unix_io_manager;
399 io_ptr = unix_io_manager;
401 if (!(mount_flags & EXT2_MF_MOUNTED))
402 io_flags = EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE;
404 io_flags |= EXT2_FLAG_64BITS;
406 retval = resize2fs_setup_tdb(device_name, undo_file, &io_ptr);
410 retval = ext2fs_open2(device_name, io_options, io_flags,
413 com_err(program_name, retval, _("while trying to open %s"),
415 printf("%s", _("Couldn't find valid filesystem superblock.\n"));
418 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
421 * Before acting on an unmounted filesystem, make sure it's ok,
422 * unless the user is forcing it.
424 * We do ERROR and VALID checks even if we're only printing the
425 * minimimum size, because traversal of a badly damaged filesystem
426 * can cause issues as well. We don't require it to be fscked after
427 * the last mount time in this case, though, as this is a bit less
430 if (!force && !(mount_flags & EXT2_MF_MOUNTED)) {
433 if (fs->super->s_state & EXT2_ERROR_FS)
436 if ((fs->super->s_state & EXT2_VALID_FS) == 0)
439 if ((fs->super->s_lastcheck < fs->super->s_mtime) &&
443 if ((fs->super->s_free_blocks_count > fs->super->s_blocks_count) ||
444 (fs->super->s_free_inodes_count > fs->super->s_inodes_count))
449 _("Please run 'e2fsck -f %s' first.\n\n"),
456 * Check for compatibility with the feature sets. We need to
457 * be more stringent than ext2fs_open().
459 if (fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) {
460 com_err(program_name, EXT2_ET_UNSUPP_FEATURE,
461 "(%s)", device_name);
465 min_size = calculate_minimum_resize_size(fs, flags);
467 if (print_min_size) {
468 printf(_("Estimated minimum size of the filesystem: %llu\n"),
473 /* Determine the system page size if possible */
475 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
476 #define _SC_PAGESIZE _SC_PAGE_SIZE
479 sysval = sysconf(_SC_PAGESIZE);
481 sys_page_size = sysval;
482 #endif /* _SC_PAGESIZE */
483 #endif /* HAVE_SYSCONF */
486 * Get the size of the containing partition, and use this for
487 * defaults and for making sure the new filesystem doesn't
488 * exceed the partition size.
490 blocksize = fs->blocksize;
491 retval = ext2fs_get_device_size2(device_name, blocksize,
494 com_err(program_name, retval, "%s",
495 _("while trying to determine filesystem size"));
500 else if (new_size_str) {
501 new_size = parse_num_blocks2(new_size_str,
502 fs->super->s_log_block_size);
504 com_err(program_name, 0,
505 _("Invalid new size: %s\n"), new_size_str);
510 /* Round down to an even multiple of a pagesize */
511 if (sys_page_size > blocksize)
512 new_size &= ~((blk64_t)((sys_page_size / blocksize)-1));
514 /* If changing 64bit, don't change the filesystem size. */
515 if (flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)) {
516 new_size = ext2fs_blocks_count(fs->super);
518 if (!ext2fs_has_feature_64bit(fs->super)) {
519 /* Take 16T down to 2^32-1 blocks */
520 if (new_size == (1ULL << 32))
522 else if (new_size > (1ULL << 32)) {
523 com_err(program_name, 0, "%s",
524 _("New size too large to be "
525 "expressed in 32 bits\n"));
530 if (!force && new_size < min_size) {
531 com_err(program_name, 0,
532 _("New size smaller than minimum (%llu)\n"), min_size);
535 if (use_stride >= 0) {
536 if (use_stride >= (int) fs->super->s_blocks_per_group) {
537 com_err(program_name, 0, "%s",
538 _("Invalid stride length"));
541 fs->stride = fs->super->s_raid_stride = use_stride;
542 ext2fs_mark_super_dirty(fs);
544 determine_fs_stride(fs);
547 * If we are resizing a plain file, and it's not big enough,
548 * automatically extend it in a sparse fashion by writing the
549 * last requested block.
551 new_file_size = ((__u64) new_size) * blocksize;
552 if ((__u64) new_file_size >
553 (((__u64) 1) << (sizeof(st_buf.st_size)*8 - 1)) - 1)
555 if ((new_file_size > st_buf.st_size) &&
557 if ((ext2fs_llseek(fd, new_file_size-1, SEEK_SET) >= 0) &&
558 (write(fd, "0", 1) == 1))
561 if (!force && (new_size > max_size)) {
562 fprintf(stderr, _("The containing partition (or device)"
563 " is only %llu (%dk) blocks.\nYou requested a new size"
564 " of %llu blocks.\n\n"), max_size,
565 blocksize / 1024, new_size);
568 if ((flags & RESIZE_DISABLE_64BIT) && (flags & RESIZE_ENABLE_64BIT)) {
569 fprintf(stderr, _("Cannot set and unset 64bit feature.\n"));
571 } else if (flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)) {
572 if (new_size >= (1ULL << 32)) {
573 fprintf(stderr, _("Cannot change the 64bit feature "
574 "on a filesystem that is larger than "
578 if (mount_flags & EXT2_MF_MOUNTED) {
579 fprintf(stderr, _("Cannot change the 64bit feature "
580 "while the filesystem is mounted.\n"));
583 if (flags & RESIZE_ENABLE_64BIT &&
584 !ext2fs_has_feature_extents(fs->super)) {
585 fprintf(stderr, _("Please enable the extents feature "
586 "with tune2fs before enabling the 64bit "
590 } else if (new_size == ext2fs_blocks_count(fs->super)) {
591 fprintf(stderr, _("The filesystem is already %llu (%dk) "
592 "blocks long. Nothing to do!\n\n"), new_size,
596 if ((flags & RESIZE_ENABLE_64BIT) &&
597 ext2fs_has_feature_64bit(fs->super)) {
598 fprintf(stderr, _("The filesystem is already 64-bit.\n"));
601 if ((flags & RESIZE_DISABLE_64BIT) &&
602 !ext2fs_has_feature_64bit(fs->super)) {
603 fprintf(stderr, _("The filesystem is already 32-bit.\n"));
606 if (mount_flags & EXT2_MF_MOUNTED) {
607 bigalloc_check(fs, force);
608 retval = online_resize_fs(fs, mtpt, &new_size, flags);
610 bigalloc_check(fs, force);
611 if (flags & RESIZE_ENABLE_64BIT)
612 printf(_("Converting the filesystem to 64-bit.\n"));
613 else if (flags & RESIZE_DISABLE_64BIT)
614 printf(_("Converting the filesystem to 32-bit.\n"));
616 printf(_("Resizing the filesystem on "
617 "%s to %llu (%dk) blocks.\n"),
618 device_name, new_size, blocksize / 1024);
619 retval = resize_fs(fs, &new_size, flags,
620 ((flags & RESIZE_PERCENT_COMPLETE) ?
621 resize_progress_func : 0));
625 com_err(program_name, retval, _("while trying to resize %s"),
628 _("Please run 'e2fsck -fy %s' to fix the filesystem\n"
629 "after the aborted resize operation.\n"),
631 ext2fs_close_free(&fs);
634 printf(_("The filesystem on %s is now %llu (%dk) blocks long.\n\n"),
635 device_name, new_size, blocksize / 1024);
637 if ((st_buf.st_size > new_file_size) &&
639 #ifdef HAVE_FTRUNCATE64
640 retval = ftruncate64(fd, new_file_size);
643 /* Only truncate if new_file_size doesn't overflow off_t */
644 if (((off_t) new_file_size) == new_file_size)
645 retval = ftruncate(fd, (off_t) new_file_size);
648 com_err(program_name, retval,
649 _("while trying to truncate %s"),
654 remove_error_table(&et_ext2_error_table);