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
28 #include <sys/types.h>
34 #include "resize2fs.h"
36 #include "../version.h"
38 char *program_name, *device_name, *io_options;
40 static void usage (char *prog)
42 fprintf (stderr, _("Usage: %s [-d debug_flags] [-f] [-F] [-M] [-P] "
43 "[-p] device [new_size]\n\n"), prog);
48 static errcode_t resize_progress_func(ext2_resize_t rfs, int pass,
49 unsigned long cur, unsigned long max)
51 ext2_sim_progmeter progress;
55 progress = (ext2_sim_progmeter) rfs->prog_data;
60 ext2fs_progress_close(progress);
63 case E2_RSZ_EXTEND_ITABLE_PASS:
64 label = _("Extending the inode table");
66 case E2_RSZ_BLOCK_RELOC_PASS:
67 label = _("Relocating blocks");
69 case E2_RSZ_INODE_SCAN_PASS:
70 label = _("Scanning inode table");
72 case E2_RSZ_INODE_REF_UPD_PASS:
73 label = _("Updating inode references");
75 case E2_RSZ_MOVE_ITABLE_PASS:
76 label = _("Moving inode table");
79 label = _("Unknown pass?!?");
82 printf(_("Begin pass %d (max = %lu)\n"), pass, max);
83 retval = ext2fs_progress_init(&progress, label, 30,
87 rfs->prog_data = (void *) progress;
90 ext2fs_progress_update(progress, cur);
93 ext2fs_progress_close(progress);
100 static void determine_fs_stride(ext2_filsys fs)
103 unsigned long long sum;
104 unsigned int has_sb, prev_has_sb, num;
105 int i_stride, b_stride;
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)
114 b_stride = ext2fs_block_bitmap_loc(fs, group) -
115 ext2fs_block_bitmap_loc(fs, group - 1) -
116 fs->super->s_blocks_per_group;
117 i_stride = ext2fs_inode_bitmap_loc(fs, group) -
118 ext2fs_inode_bitmap_loc(fs, group - 1) -
119 fs->super->s_blocks_per_group;
120 if (b_stride != i_stride ||
124 /* printf("group %d has stride %d\n", group, b_stride); */
129 prev_has_sb = has_sb;
132 if (fs->group_desc_count > 12 && num < 3)
136 fs->stride = sum / num;
140 fs->super->s_raid_stride = fs->stride;
141 ext2fs_mark_super_dirty(fs);
145 printf("Using RAID stride of %d\n", fs->stride);
149 int main (int argc, char ** argv)
158 int force_min_size = 0;
159 int print_min_size = 0;
165 char *new_size_str = 0;
168 struct stat64 st_buf;
173 unsigned int sys_page_size = 4096;
175 int len, mount_flags;
179 setlocale(LC_MESSAGES, "");
180 setlocale(LC_CTYPE, "");
181 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
182 textdomain(NLS_CAT_NAME);
185 add_error_table(&et_ext2_error_table);
187 fprintf (stderr, "resize2fs %s (%s)\n",
188 E2FSPROGS_VERSION, E2FSPROGS_DATE);
190 program_name = *argv;
192 while ((c = getopt (argc, argv, "d:fFhMPpS:")) != EOF) {
210 flags |= atoi(optarg);
213 flags |= RESIZE_PERCENT_COMPLETE;
216 use_stride = atoi(optarg);
225 device_name = argv[optind++];
227 new_size_str = argv[optind++];
231 io_options = strchr(device_name, '?');
236 * Figure out whether or not the device is mounted, and if it is
237 * where it is mounted.
245 retval = ext2fs_check_mount_point(device_name, &mount_flags,
248 com_err("ext2fs_check_mount_point", retval,
249 _("while determining whether %s is mounted."),
253 if (!(mount_flags & EXT2_MF_MOUNTED) || (mtpt[len-1] == 0))
260 fd = open64(device_name, O_RDWR);
262 fd = open(device_name, O_RDWR);
265 com_err("open", errno, _("while opening %s"),
271 ret = fstat64(fd, &st_buf);
273 ret = fstat(fd, &st_buf);
276 com_err("open", errno,
277 _("while getting stat information for %s"),
283 retval = ext2fs_sync_device(fd, 1);
285 com_err(argv[0], retval,
286 _("while trying to flush %s"),
292 if (!S_ISREG(st_buf.st_mode )) {
297 #ifdef CONFIG_TESTIO_DEBUG
298 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
299 io_ptr = test_io_manager;
300 test_io_backing_manager = unix_io_manager;
303 io_ptr = unix_io_manager;
305 if (!(mount_flags & EXT2_MF_MOUNTED))
306 io_flags = EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE;
307 retval = ext2fs_open2(device_name, io_options, io_flags,
310 com_err (program_name, retval, _("while trying to open %s"),
312 printf (_("Couldn't find valid filesystem superblock.\n"));
317 * Check for compatibility with the feature sets. We need to
318 * be more stringent than ext2fs_open().
320 if (fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) {
321 com_err(program_name, EXT2_ET_UNSUPP_FEATURE,
322 "(%s)", device_name);
327 * XXXX The combination of flex_bg and !resize_inode causes
328 * major problems for resize2fs, since when the group descriptors
329 * grow in size this can potentially require multiple inode
330 * tables to be moved aside to make room, and resize2fs chokes
331 * rather badly in this scenario. It's a rare combination,
332 * except when a filesystem is expanded more than a certain
333 * size, so for now, we'll just prohibit that combination.
334 * This is something we should fix eventually, though.
336 if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
337 !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
338 com_err(program_name, 0, _("%s: The combination of flex_bg "
339 "and\n\t!resize_inode features "
340 "is not supported by resize2fs.\n"),
345 min_size = calculate_minimum_resize_size(fs);
347 if (print_min_size) {
348 if (!force && ((fs->super->s_lastcheck < fs->super->s_mtime) ||
349 (fs->super->s_state & EXT2_ERROR_FS) ||
350 ((fs->super->s_state & EXT2_VALID_FS) == 0))) {
352 _("Please run 'e2fsck -f %s' first.\n\n"),
356 printf(_("Estimated minimum size of the filesystem: %u\n"),
361 /* Determine the system page size if possible */
363 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
364 #define _SC_PAGESIZE _SC_PAGE_SIZE
367 sysval = sysconf(_SC_PAGESIZE);
369 sys_page_size = sysval;
370 #endif /* _SC_PAGESIZE */
371 #endif /* HAVE_SYSCONF */
374 * Get the size of the containing partition, and use this for
375 * defaults and for making sure the new filesystem doesn't
376 * exceed the partition size.
378 retval = ext2fs_get_device_size(device_name, fs->blocksize,
381 com_err(program_name, retval,
382 _("while trying to determine filesystem size"));
387 else if (new_size_str) {
388 new_size = parse_num_blocks(new_size_str,
389 fs->super->s_log_block_size);
391 com_err(program_name, 0,
392 _("Invalid new size: %s\n"), new_size_str);
397 /* Round down to an even multiple of a pagesize */
398 if (sys_page_size > fs->blocksize)
399 new_size &= ~((sys_page_size / fs->blocksize)-1);
402 if (!force && new_size < min_size) {
403 com_err(program_name, 0,
404 _("New size smaller than minimum (%u)\n"), min_size);
407 if (use_stride >= 0) {
408 if (use_stride >= (int) fs->super->s_blocks_per_group) {
409 com_err(program_name, 0,
410 _("Invalid stride length"));
413 fs->stride = fs->super->s_raid_stride = use_stride;
414 ext2fs_mark_super_dirty(fs);
416 determine_fs_stride(fs);
419 * If we are resizing a plain file, and it's not big enough,
420 * automatically extend it in a sparse fashion by writing the
421 * last requested block.
423 new_file_size = ((__u64) new_size) * fs->blocksize;
424 if ((__u64) new_file_size >
425 (((__u64) 1) << (sizeof(st_buf.st_size)*8 - 1)) - 1)
427 if ((new_file_size > st_buf.st_size) &&
429 if ((ext2fs_llseek(fd, new_file_size-1, SEEK_SET) >= 0) &&
430 (write(fd, "0", 1) == 1))
433 if (!force && (new_size > max_size)) {
434 fprintf(stderr, _("The containing partition (or device)"
435 " is only %u (%dk) blocks.\nYou requested a new size"
436 " of %u blocks.\n\n"), max_size,
437 fs->blocksize / 1024, new_size);
440 if (new_size == ext2fs_blocks_count(fs->super)) {
441 fprintf(stderr, _("The filesystem is already %u blocks "
442 "long. Nothing to do!\n\n"), new_size);
445 if (mount_flags & EXT2_MF_MOUNTED) {
446 retval = online_resize_fs(fs, mtpt, &new_size, flags);
448 if (!force && ((fs->super->s_lastcheck < fs->super->s_mtime) ||
449 (fs->super->s_state & EXT2_ERROR_FS) ||
450 ((fs->super->s_state & EXT2_VALID_FS) == 0))) {
452 _("Please run 'e2fsck -f %s' first.\n\n"),
456 printf(_("Resizing the filesystem on "
457 "%s to %u (%dk) blocks.\n"),
458 device_name, new_size, fs->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"),
474 printf(_("The filesystem on %s is now %u blocks long.\n\n"),
475 device_name, new_size);
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);