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] [-S RAID-stride] "
51 "[-z undo_file]\n\n"),
57 static errcode_t
resize_progress_func(ext2_resize_t rfs
, int pass
,
58 unsigned long cur
, unsigned long max
)
60 ext2_sim_progmeter progress
;
64 progress
= (ext2_sim_progmeter
) rfs
->prog_data
;
69 ext2fs_progress_close(progress
);
72 case E2_RSZ_EXTEND_ITABLE_PASS
:
73 label
= _("Extending the inode table");
75 case E2_RSZ_BLOCK_RELOC_PASS
:
76 label
= _("Relocating blocks");
78 case E2_RSZ_INODE_SCAN_PASS
:
79 label
= _("Scanning inode table");
81 case E2_RSZ_INODE_REF_UPD_PASS
:
82 label
= _("Updating inode references");
84 case E2_RSZ_MOVE_ITABLE_PASS
:
85 label
= _("Moving inode table");
88 label
= _("Unknown pass?!?");
91 printf(_("Begin pass %d (max = %lu)\n"), pass
, max
);
92 retval
= ext2fs_progress_init(&progress
, label
, 30,
96 rfs
->prog_data
= (void *) progress
;
99 ext2fs_progress_update(progress
, cur
);
102 ext2fs_progress_close(progress
);
109 static void determine_fs_stride(ext2_filsys fs
)
112 unsigned long long sum
;
113 unsigned int has_sb
, prev_has_sb
= 0, num
;
114 int i_stride
, b_stride
;
115 int flexbg_size
= 1 << fs
->super
->s_log_groups_per_flex
;
120 for (group
= 0; group
< fs
->group_desc_count
; group
++) {
121 has_sb
= ext2fs_bg_has_super(fs
, group
);
122 if (group
== 0 || has_sb
!= prev_has_sb
)
124 b_stride
= ext2fs_block_bitmap_loc(fs
, group
) -
125 ext2fs_block_bitmap_loc(fs
, group
- 1) -
126 fs
->super
->s_blocks_per_group
;
127 i_stride
= ext2fs_inode_bitmap_loc(fs
, group
) -
128 ext2fs_inode_bitmap_loc(fs
, group
- 1) -
129 fs
->super
->s_blocks_per_group
;
130 if (b_stride
!= i_stride
||
132 (flexbg_size
> 1 && (group
% flexbg_size
== 0)))
135 /* printf("group %d has stride %d\n", group, b_stride); */
140 prev_has_sb
= has_sb
;
143 if (fs
->group_desc_count
> 12 && num
< 3)
147 fs
->stride
= sum
/ num
;
151 fs
->super
->s_raid_stride
= fs
->stride
;
152 ext2fs_mark_super_dirty(fs
);
156 printf("Using RAID stride of %d\n", fs
->stride
);
160 static void bigalloc_check(ext2_filsys fs
, int force
)
162 if (!force
&& ext2fs_has_feature_bigalloc(fs
->super
)) {
163 fprintf(stderr
, "%s", _("\nResizing bigalloc file systems has "
164 "not been fully tested. Proceed at\n"
165 "your own risk! Use the force option "
166 "if you want to go ahead anyway.\n\n"));
171 static int resize2fs_setup_tdb(const char *device
, char *undo_file
,
174 errcode_t retval
= ENOMEM
;
175 const char *tdb_dir
= NULL
;
176 char *tdb_file
= NULL
;
177 char *dev_name
, *tmp_name
;
179 /* (re)open a specific undo file */
180 if (undo_file
&& undo_file
[0] != 0) {
181 retval
= set_undo_io_backing_manager(*io_ptr
);
184 *io_ptr
= undo_io_manager
;
185 retval
= set_undo_io_backup_file(undo_file
);
188 printf(_("Overwriting existing filesystem; this can be undone "
189 "using the command:\n"
190 " e2undo %s %s\n\n"),
196 * Configuration via a conf file would be
199 tdb_dir
= getenv("E2FSPROGS_UNDO_DIR");
201 tdb_dir
= "/var/lib/e2fsprogs";
203 if (!strcmp(tdb_dir
, "none") || (tdb_dir
[0] == 0) ||
204 access(tdb_dir
, W_OK
))
207 tmp_name
= strdup(device
);
210 dev_name
= basename(tmp_name
);
211 tdb_file
= malloc(strlen(tdb_dir
) + 11 + strlen(dev_name
) + 7 + 1);
216 sprintf(tdb_file
, "%s/resize2fs-%s.e2undo", tdb_dir
, dev_name
);
219 if ((unlink(tdb_file
) < 0) && (errno
!= ENOENT
)) {
221 com_err(program_name
, retval
,
222 _("while trying to delete %s"), tdb_file
);
226 retval
= set_undo_io_backing_manager(*io_ptr
);
229 *io_ptr
= undo_io_manager
;
230 retval
= set_undo_io_backup_file(tdb_file
);
233 printf(_("Overwriting existing filesystem; this can be undone "
234 "using the command:\n"
235 " e2undo %s %s\n\n"), tdb_file
, device
);
242 com_err(program_name
, retval
, "%s",
243 _("while trying to setup undo file\n"));
247 int main (int argc
, char ** argv
)
256 int force_min_size
= 0;
257 int print_min_size
= 0;
259 blk64_t new_size
= 0;
260 blk64_t max_size
= 0;
261 blk64_t min_size
= 0;
263 char *new_size_str
= 0;
265 ext2fs_struct_stat st_buf
;
267 unsigned int sys_page_size
= 4096;
268 unsigned int blocksize
;
270 int len
, mount_flags
;
271 char *mtpt
, *undo_file
= NULL
;
274 setlocale(LC_MESSAGES
, "");
275 setlocale(LC_CTYPE
, "");
276 bindtextdomain(NLS_CAT_NAME
, LOCALEDIR
);
277 textdomain(NLS_CAT_NAME
);
278 set_com_err_gettext(gettext
);
281 add_error_table(&et_ext2_error_table
);
283 fprintf (stderr
, "resize2fs %s (%s)\n",
284 E2FSPROGS_VERSION
, E2FSPROGS_DATE
);
286 program_name
= *argv
;
288 while ((c
= getopt(argc
, argv
, "d:fFhMPpS:bsz:")) != EOF
) {
306 flags
|= atoi(optarg
);
309 flags
|= RESIZE_PERCENT_COMPLETE
;
312 use_stride
= atoi(optarg
);
315 flags
|= RESIZE_ENABLE_64BIT
;
318 flags
|= RESIZE_DISABLE_64BIT
;
330 device_name
= argv
[optind
++];
332 new_size_str
= argv
[optind
++];
336 io_options
= strchr(device_name
, '?');
341 * Figure out whether or not the device is mounted, and if it is
342 * where it is mounted.
350 retval
= ext2fs_check_mount_point(device_name
, &mount_flags
,
353 com_err("ext2fs_check_mount_point", retval
,
354 _("while determining whether %s is mounted."),
358 if (!(mount_flags
& EXT2_MF_MOUNTED
) || (mtpt
[len
-1] == 0))
364 fd
= ext2fs_open_file(device_name
, O_RDWR
, 0);
366 com_err("open", errno
, _("while opening %s"),
371 ret
= ext2fs_fstat(fd
, &st_buf
);
373 com_err("open", errno
,
374 _("while getting stat information for %s"),
380 retval
= ext2fs_sync_device(fd
, 1);
382 com_err(argv
[0], retval
,
383 _("while trying to flush %s"),
389 if (!S_ISREG(st_buf
.st_mode
)) {
394 #ifdef CONFIG_TESTIO_DEBUG
395 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
396 io_ptr
= test_io_manager
;
397 test_io_backing_manager
= unix_io_manager
;
400 io_ptr
= unix_io_manager
;
402 if (!(mount_flags
& EXT2_MF_MOUNTED
))
403 io_flags
= EXT2_FLAG_RW
| EXT2_FLAG_EXCLUSIVE
;
405 io_flags
|= EXT2_FLAG_64BITS
;
407 retval
= resize2fs_setup_tdb(device_name
, undo_file
, &io_ptr
);
411 retval
= ext2fs_open2(device_name
, io_options
, io_flags
,
414 com_err(program_name
, retval
, _("while trying to open %s"),
416 printf("%s", _("Couldn't find valid filesystem superblock.\n"));
419 fs
->default_bitmap_type
= EXT2FS_BMAP64_RBTREE
;
422 * Before acting on an unmounted filesystem, make sure it's ok,
423 * unless the user is forcing it.
425 * We do ERROR and VALID checks even if we're only printing the
426 * minimum size, because traversal of a badly damaged filesystem
427 * can cause issues as well. We don't require it to be fscked after
428 * the last mount time in this case, though, as this is a bit less
431 if (!force
&& !(mount_flags
& EXT2_MF_MOUNTED
)) {
434 if (fs
->super
->s_state
& EXT2_ERROR_FS
)
437 if ((fs
->super
->s_state
& EXT2_VALID_FS
) == 0)
440 if ((fs
->super
->s_lastcheck
< fs
->super
->s_mtime
) &&
444 if ((ext2fs_free_blocks_count(fs
->super
) >
445 ext2fs_blocks_count(fs
->super
)) ||
446 (fs
->super
->s_free_inodes_count
> fs
->super
->s_inodes_count
))
451 _("Please run 'e2fsck -f %s' first.\n\n"),
458 * Check for compatibility with the feature sets. We need to
459 * be more stringent than ext2fs_open().
461 if (fs
->super
->s_feature_compat
& ~EXT2_LIB_FEATURE_COMPAT_SUPP
) {
462 com_err(program_name
, EXT2_ET_UNSUPP_FEATURE
,
463 "(%s)", device_name
);
467 min_size
= calculate_minimum_resize_size(fs
, flags
);
469 if (print_min_size
) {
470 printf(_("Estimated minimum size of the filesystem: %llu\n"),
475 /* Determine the system page size if possible */
477 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
478 #define _SC_PAGESIZE _SC_PAGE_SIZE
481 sysval
= sysconf(_SC_PAGESIZE
);
483 sys_page_size
= sysval
;
484 #endif /* _SC_PAGESIZE */
485 #endif /* HAVE_SYSCONF */
488 * Get the size of the containing partition, and use this for
489 * defaults and for making sure the new filesystem doesn't
490 * exceed the partition size.
492 blocksize
= fs
->blocksize
;
493 retval
= ext2fs_get_device_size2(device_name
, blocksize
,
496 com_err(program_name
, retval
, "%s",
497 _("while trying to determine filesystem size"));
502 else if (new_size_str
) {
503 new_size
= parse_num_blocks2(new_size_str
,
504 fs
->super
->s_log_block_size
);
506 com_err(program_name
, 0,
507 _("Invalid new size: %s\n"), new_size_str
);
512 /* Round down to an even multiple of a pagesize */
513 if (sys_page_size
> blocksize
)
514 new_size
&= ~((blk64_t
)((sys_page_size
/ blocksize
)-1));
516 /* If changing 64bit, don't change the filesystem size. */
517 if (flags
& (RESIZE_DISABLE_64BIT
| RESIZE_ENABLE_64BIT
)) {
518 new_size
= ext2fs_blocks_count(fs
->super
);
520 if (!ext2fs_has_feature_64bit(fs
->super
)) {
521 /* Take 16T down to 2^32-1 blocks */
522 if (new_size
== (1ULL << 32))
524 else if (new_size
> (1ULL << 32)) {
525 com_err(program_name
, 0, "%s",
526 _("New size too large to be "
527 "expressed in 32 bits\n"));
532 if (!force
&& new_size
< min_size
) {
533 com_err(program_name
, 0,
534 _("New size smaller than minimum (%llu)\n"), min_size
);
537 if (use_stride
>= 0) {
538 if (use_stride
>= (int) fs
->super
->s_blocks_per_group
) {
539 com_err(program_name
, 0, "%s",
540 _("Invalid stride length"));
543 fs
->stride
= fs
->super
->s_raid_stride
= use_stride
;
544 ext2fs_mark_super_dirty(fs
);
546 determine_fs_stride(fs
);
549 * If we are resizing a plain file, and it's not big enough,
550 * automatically extend it in a sparse fashion by writing the
551 * last requested block.
553 new_file_size
= ((__u64
) new_size
) * blocksize
;
554 if ((__u64
) new_file_size
>
555 (((__u64
) 1) << (sizeof(st_buf
.st_size
)*8 - 1)) - 1)
557 if ((new_file_size
> st_buf
.st_size
) &&
559 if ((ext2fs_llseek(fd
, new_file_size
-1, SEEK_SET
) >= 0) &&
560 (write(fd
, "0", 1) == 1))
563 if (!force
&& (new_size
> max_size
)) {
564 fprintf(stderr
, _("The containing partition (or device)"
565 " is only %llu (%dk) blocks.\nYou requested a new size"
566 " of %llu blocks.\n\n"), max_size
,
567 blocksize
/ 1024, new_size
);
570 if ((flags
& RESIZE_DISABLE_64BIT
) && (flags
& RESIZE_ENABLE_64BIT
)) {
571 fprintf(stderr
, _("Cannot set and unset 64bit feature.\n"));
573 } else if (flags
& (RESIZE_DISABLE_64BIT
| RESIZE_ENABLE_64BIT
)) {
574 if (new_size
>= (1ULL << 32)) {
575 fprintf(stderr
, _("Cannot change the 64bit feature "
576 "on a filesystem that is larger than "
580 if (mount_flags
& EXT2_MF_MOUNTED
) {
581 fprintf(stderr
, _("Cannot change the 64bit feature "
582 "while the filesystem is mounted.\n"));
585 if (flags
& RESIZE_ENABLE_64BIT
&&
586 !ext2fs_has_feature_extents(fs
->super
)) {
587 fprintf(stderr
, _("Please enable the extents feature "
588 "with tune2fs before enabling the 64bit "
592 } else if (new_size
== ext2fs_blocks_count(fs
->super
)) {
593 fprintf(stderr
, _("The filesystem is already %llu (%dk) "
594 "blocks long. Nothing to do!\n\n"), new_size
,
598 if ((flags
& RESIZE_ENABLE_64BIT
) &&
599 ext2fs_has_feature_64bit(fs
->super
)) {
600 fprintf(stderr
, _("The filesystem is already 64-bit.\n"));
603 if ((flags
& RESIZE_DISABLE_64BIT
) &&
604 !ext2fs_has_feature_64bit(fs
->super
)) {
605 fprintf(stderr
, _("The filesystem is already 32-bit.\n"));
608 if (mount_flags
& EXT2_MF_MOUNTED
) {
609 retval
= online_resize_fs(fs
, mtpt
, &new_size
, flags
);
611 bigalloc_check(fs
, force
);
612 if (flags
& RESIZE_ENABLE_64BIT
)
613 printf(_("Converting the filesystem to 64-bit.\n"));
614 else if (flags
& RESIZE_DISABLE_64BIT
)
615 printf(_("Converting the filesystem to 32-bit.\n"));
617 printf(_("Resizing the filesystem on "
618 "%s to %llu (%dk) blocks.\n"),
619 device_name
, new_size
, blocksize
/ 1024);
620 retval
= resize_fs(fs
, &new_size
, flags
,
621 ((flags
& RESIZE_PERCENT_COMPLETE
) ?
622 resize_progress_func
: 0));
626 com_err(program_name
, retval
, _("while trying to resize %s"),
629 _("Please run 'e2fsck -fy %s' to fix the filesystem\n"
630 "after the aborted resize operation.\n"),
632 ext2fs_close_free(&fs
);
635 printf(_("The filesystem on %s is now %llu (%dk) blocks long.\n\n"),
636 device_name
, new_size
, blocksize
/ 1024);
638 if ((st_buf
.st_size
> new_file_size
) &&
640 #ifdef HAVE_FTRUNCATE64
641 retval
= ftruncate64(fd
, new_file_size
);
644 /* Only truncate if new_file_size doesn't overflow off_t */
645 if (((off_t
) new_file_size
) == new_file_size
)
646 retval
= ftruncate(fd
, (off_t
) new_file_size
);
649 com_err(program_name
, retval
,
650 _("while trying to truncate %s"),
655 remove_error_table(&et_ext2_error_table
);