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"
40 static char *device_name, *io_options;
42 static void usage (char *prog)
44 fprintf (stderr, _("Usage: %s [-d debug_flags] [-f] [-F] [-M] [-P] "
45 "[-p] device [new_size]\n\n"), prog);
50 static errcode_t resize_progress_func(ext2_resize_t rfs, int pass,
51 unsigned long cur, unsigned long max)
53 ext2_sim_progmeter progress;
57 progress = (ext2_sim_progmeter) rfs->prog_data;
62 ext2fs_progress_close(progress);
65 case E2_RSZ_EXTEND_ITABLE_PASS:
66 label = _("Extending the inode table");
68 case E2_RSZ_BLOCK_RELOC_PASS:
69 label = _("Relocating blocks");
71 case E2_RSZ_INODE_SCAN_PASS:
72 label = _("Scanning inode table");
74 case E2_RSZ_INODE_REF_UPD_PASS:
75 label = _("Updating inode references");
77 case E2_RSZ_MOVE_ITABLE_PASS:
78 label = _("Moving inode table");
81 label = _("Unknown pass?!?");
84 printf(_("Begin pass %d (max = %lu)\n"), pass, max);
85 retval = ext2fs_progress_init(&progress, label, 30,
89 rfs->prog_data = (void *) progress;
92 ext2fs_progress_update(progress, cur);
95 ext2fs_progress_close(progress);
102 static void determine_fs_stride(ext2_filsys fs)
105 unsigned long long sum;
106 unsigned int has_sb, prev_has_sb = 0, num;
107 int i_stride, b_stride;
112 for (group = 0; group < fs->group_desc_count; group++) {
113 has_sb = ext2fs_bg_has_super(fs, group);
114 if (group == 0 || has_sb != prev_has_sb)
116 b_stride = ext2fs_block_bitmap_loc(fs, group) -
117 ext2fs_block_bitmap_loc(fs, group - 1) -
118 fs->super->s_blocks_per_group;
119 i_stride = ext2fs_inode_bitmap_loc(fs, group) -
120 ext2fs_inode_bitmap_loc(fs, group - 1) -
121 fs->super->s_blocks_per_group;
122 if (b_stride != i_stride ||
126 /* printf("group %d has stride %d\n", group, b_stride); */
131 prev_has_sb = has_sb;
134 if (fs->group_desc_count > 12 && num < 3)
138 fs->stride = sum / num;
142 fs->super->s_raid_stride = fs->stride;
143 ext2fs_mark_super_dirty(fs);
147 printf("Using RAID stride of %d\n", fs->stride);
151 static void bigalloc_check(ext2_filsys fs, int force)
153 if (!force && EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
154 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
155 fprintf(stderr, "%s", _("\nResizing bigalloc file systems has "
156 "not been fully tested. Proceed at\n"
157 "your own risk! Use the force option "
158 "if you want to go ahead anyway.\n\n"));
163 int main (int argc, char ** argv)
172 int force_min_size = 0;
173 int print_min_size = 0;
175 blk64_t new_size = 0;
176 blk64_t max_size = 0;
177 blk64_t min_size = 0;
179 char *new_size_str = 0;
181 ext2fs_struct_stat st_buf;
183 unsigned int sys_page_size = 4096;
184 unsigned int blocksize;
186 int len, mount_flags;
190 setlocale(LC_MESSAGES, "");
191 setlocale(LC_CTYPE, "");
192 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
193 textdomain(NLS_CAT_NAME);
194 set_com_err_gettext(gettext);
197 add_error_table(&et_ext2_error_table);
199 fprintf (stderr, "resize2fs %s (%s)\n",
200 E2FSPROGS_VERSION, E2FSPROGS_DATE);
202 program_name = *argv;
204 while ((c = getopt (argc, argv, "d:fFhMPpS:")) != EOF) {
222 flags |= atoi(optarg);
225 flags |= RESIZE_PERCENT_COMPLETE;
228 use_stride = atoi(optarg);
237 device_name = argv[optind++];
239 new_size_str = argv[optind++];
243 io_options = strchr(device_name, '?');
248 * Figure out whether or not the device is mounted, and if it is
249 * where it is mounted.
257 retval = ext2fs_check_mount_point(device_name, &mount_flags,
260 com_err("ext2fs_check_mount_point", retval,
261 _("while determining whether %s is mounted."),
265 if (!(mount_flags & EXT2_MF_MOUNTED) || (mtpt[len-1] == 0))
271 fd = ext2fs_open_file(device_name, O_RDWR, 0);
273 com_err("open", errno, _("while opening %s"),
278 ret = ext2fs_fstat(fd, &st_buf);
280 com_err("open", errno,
281 _("while getting stat information for %s"),
287 retval = ext2fs_sync_device(fd, 1);
289 com_err(argv[0], retval,
290 _("while trying to flush %s"),
296 if (!S_ISREG(st_buf.st_mode )) {
301 #ifdef CONFIG_TESTIO_DEBUG
302 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
303 io_ptr = test_io_manager;
304 test_io_backing_manager = unix_io_manager;
307 io_ptr = unix_io_manager;
309 if (!(mount_flags & EXT2_MF_MOUNTED))
310 io_flags = EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE;
312 io_flags |= EXT2_FLAG_64BITS;
314 retval = ext2fs_open2(device_name, io_options, io_flags,
317 com_err(program_name, retval, _("while trying to open %s"),
319 printf("%s", _("Couldn't find valid filesystem superblock.\n"));
322 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
324 if (!(mount_flags & EXT2_MF_MOUNTED)) {
325 if (!force && ((fs->super->s_lastcheck < fs->super->s_mtime) ||
326 (fs->super->s_state & EXT2_ERROR_FS) ||
327 ((fs->super->s_state & EXT2_VALID_FS) == 0))) {
329 _("Please run 'e2fsck -f %s' first.\n\n"),
336 * Check for compatibility with the feature sets. We need to
337 * be more stringent than ext2fs_open().
339 if (fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) {
340 com_err(program_name, EXT2_ET_UNSUPP_FEATURE,
341 "(%s)", device_name);
345 min_size = calculate_minimum_resize_size(fs, flags);
347 if (print_min_size) {
348 printf(_("Estimated minimum size of the filesystem: %llu\n"),
353 /* Determine the system page size if possible */
355 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
356 #define _SC_PAGESIZE _SC_PAGE_SIZE
359 sysval = sysconf(_SC_PAGESIZE);
361 sys_page_size = sysval;
362 #endif /* _SC_PAGESIZE */
363 #endif /* HAVE_SYSCONF */
366 * Get the size of the containing partition, and use this for
367 * defaults and for making sure the new filesystem doesn't
368 * exceed the partition size.
370 blocksize = fs->blocksize;
371 retval = ext2fs_get_device_size2(device_name, blocksize,
374 com_err(program_name, retval, "%s",
375 _("while trying to determine filesystem size"));
380 else if (new_size_str) {
381 new_size = parse_num_blocks2(new_size_str,
382 fs->super->s_log_block_size);
384 com_err(program_name, 0,
385 _("Invalid new size: %s\n"), new_size_str);
390 /* Round down to an even multiple of a pagesize */
391 if (sys_page_size > blocksize)
392 new_size &= ~((sys_page_size / blocksize)-1);
394 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
395 EXT4_FEATURE_INCOMPAT_64BIT)) {
396 /* Take 16T down to 2^32-1 blocks */
397 if (new_size == (1ULL << 32))
399 else if (new_size > (1ULL << 32)) {
400 com_err(program_name, 0, "%s",
401 _("New size too large to be "
402 "expressed in 32 bits\n"));
407 if (!force && new_size < min_size) {
408 com_err(program_name, 0,
409 _("New size smaller than minimum (%llu)\n"), min_size);
412 if (use_stride >= 0) {
413 if (use_stride >= (int) fs->super->s_blocks_per_group) {
414 com_err(program_name, 0, "%s",
415 _("Invalid stride length"));
418 fs->stride = fs->super->s_raid_stride = use_stride;
419 ext2fs_mark_super_dirty(fs);
421 determine_fs_stride(fs);
424 * If we are resizing a plain file, and it's not big enough,
425 * automatically extend it in a sparse fashion by writing the
426 * last requested block.
428 new_file_size = ((__u64) new_size) * blocksize;
429 if ((__u64) new_file_size >
430 (((__u64) 1) << (sizeof(st_buf.st_size)*8 - 1)) - 1)
432 if ((new_file_size > st_buf.st_size) &&
434 if ((ext2fs_llseek(fd, new_file_size-1, SEEK_SET) >= 0) &&
435 (write(fd, "0", 1) == 1))
438 if (!force && (new_size > max_size)) {
439 fprintf(stderr, _("The containing partition (or device)"
440 " is only %llu (%dk) blocks.\nYou requested a new size"
441 " of %llu blocks.\n\n"), max_size,
442 blocksize / 1024, new_size);
445 if (new_size == ext2fs_blocks_count(fs->super)) {
446 fprintf(stderr, _("The filesystem is already %llu (%dk) "
447 "blocks long. Nothing to do!\n\n"), new_size,
451 if (mount_flags & EXT2_MF_MOUNTED) {
452 bigalloc_check(fs, force);
453 retval = online_resize_fs(fs, mtpt, &new_size, flags);
455 bigalloc_check(fs, force);
456 printf(_("Resizing the filesystem on "
457 "%s to %llu (%dk) blocks.\n"),
458 device_name, new_size, blocksize / 1024);
459 retval = resize_fs(fs, &new_size, flags,
460 ((flags & RESIZE_PERCENT_COMPLETE) ?
461 resize_progress_func : 0));
465 com_err(program_name, retval, _("while trying to resize %s"),
468 _("Please run 'e2fsck -fy %s' to fix the filesystem\n"
469 "after the aborted resize operation.\n"),
471 ext2fs_close_free(&fs);
474 printf(_("The filesystem on %s is now %llu (%dk) blocks long.\n\n"),
475 device_name, new_size, blocksize / 1024);
477 if ((st_buf.st_size > new_file_size) &&
479 #ifdef HAVE_FTRUNCATE64
480 retval = ftruncate64(fd, new_file_size);
483 /* Only truncate if new_file_size doesn't overflow off_t */
484 if (((off_t) new_file_size) == new_file_size)
485 retval = ftruncate(fd, (off_t) new_file_size);
488 com_err(program_name, retval,
489 _("while trying to truncate %s"),
494 remove_error_table(&et_ext2_error_table);