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
25 #include <sys/types.h>
31 #include "resize2fs.h"
33 #include "../version.h"
35 char *program_name, *device_name, *io_options;
37 static void usage (char *prog)
39 fprintf (stderr, _("Usage: %s [-d debug_flags] [-f] [-F] [-p] "
40 "device [new_size]\n\n"), prog);
45 static errcode_t resize_progress_func(ext2_resize_t rfs, int pass,
46 unsigned long cur, unsigned long max)
48 ext2_sim_progmeter progress;
52 progress = (ext2_sim_progmeter) rfs->prog_data;
57 ext2fs_progress_close(progress);
60 case E2_RSZ_EXTEND_ITABLE_PASS:
61 label = _("Extending the inode table");
63 case E2_RSZ_BLOCK_RELOC_PASS:
64 label = _("Relocating blocks");
66 case E2_RSZ_INODE_SCAN_PASS:
67 label = _("Scanning inode table");
69 case E2_RSZ_INODE_REF_UPD_PASS:
70 label = _("Updating inode references");
72 case E2_RSZ_MOVE_ITABLE_PASS:
73 label = _("Moving inode table");
76 label = _("Unknown pass?!?");
79 printf(_("Begin pass %d (max = %lu)\n"), pass, max);
80 retval = ext2fs_progress_init(&progress, label, 30,
84 rfs->prog_data = (void *) progress;
87 ext2fs_progress_update(progress, cur);
90 ext2fs_progress_close(progress);
97 static void determine_fs_stride(ext2_filsys fs)
100 unsigned long long sum;
101 unsigned int has_sb, prev_has_sb, num;
102 int i_stride, b_stride;
107 for (group = 0; group < fs->group_desc_count; group++) {
108 has_sb = ext2fs_bg_has_super(fs, group);
109 if (group == 0 || has_sb != prev_has_sb)
111 b_stride = fs->group_desc[group].bg_block_bitmap -
112 fs->group_desc[group-1].bg_block_bitmap -
113 fs->super->s_blocks_per_group;
114 i_stride = fs->group_desc[group].bg_inode_bitmap -
115 fs->group_desc[group-1].bg_inode_bitmap -
116 fs->super->s_blocks_per_group;
117 if (b_stride != i_stride ||
121 /* printf("group %d has stride %d\n", group, b_stride); */
126 prev_has_sb = has_sb;
129 if (fs->group_desc_count > 12 && num < 3)
133 fs->stride = sum / num;
137 fs->super->s_raid_stride = fs->stride;
138 ext2fs_mark_super_dirty(fs);
142 printf("Using RAID stride of %d\n", fs->stride);
146 int main (int argc, char ** argv)
159 char *new_size_str = 0;
162 struct stat64 st_buf;
167 unsigned int sys_page_size = 4096;
169 int len, mount_flags;
173 setlocale(LC_MESSAGES, "");
174 setlocale(LC_CTYPE, "");
175 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
176 textdomain(NLS_CAT_NAME);
179 add_error_table(&et_ext2_error_table);
181 fprintf (stderr, "resize2fs %s (%s)\n",
182 E2FSPROGS_VERSION, E2FSPROGS_DATE);
184 program_name = *argv;
186 while ((c = getopt (argc, argv, "d:fFhpS:")) != EOF) {
198 flags |= atoi(optarg);
201 flags |= RESIZE_PERCENT_COMPLETE;
204 use_stride = atoi(optarg);
213 device_name = argv[optind++];
215 new_size_str = argv[optind++];
219 io_options = strchr(device_name, '?');
224 * Figure out whether or not the device is mounted, and if it is
225 * where it is mounted.
233 retval = ext2fs_check_mount_point(device_name, &mount_flags,
236 com_err("ext2fs_check_mount_point", retval,
237 _("while determining whether %s is mounted."),
241 if (!(mount_flags & EXT2_MF_MOUNTED) || (mtpt[len-1] == 0))
248 fd = open64(device_name, O_RDWR);
250 fd = open(device_name, O_RDWR);
253 com_err("open", errno, _("while opening %s"),
259 ret = fstat64(fd, &st_buf);
261 ret = fstat(fd, &st_buf);
264 com_err("open", errno,
265 _("while getting stat information for %s"),
271 retval = ext2fs_sync_device(fd, 1);
273 com_err(argv[0], retval,
274 _("while trying to flush %s"),
280 if (!S_ISREG(st_buf.st_mode )) {
285 if (flags & RESIZE_DEBUG_IO) {
286 io_ptr = test_io_manager;
287 test_io_backing_manager = unix_io_manager;
289 io_ptr = unix_io_manager;
291 if (!(mount_flags & EXT2_MF_MOUNTED))
292 io_flags = EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE;
293 retval = ext2fs_open2(device_name, io_options, io_flags,
296 com_err (program_name, retval, _("while trying to open %s"),
298 printf (_("Couldn't find valid filesystem superblock.\n"));
302 * Check for compatibility with the feature sets. We need to
303 * be more stringent than ext2fs_open().
305 if (fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) {
306 com_err(program_name, EXT2_ET_UNSUPP_FEATURE,
307 "(%s)", device_name);
311 /* Determine the system page size if possible */
313 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
314 #define _SC_PAGESIZE _SC_PAGE_SIZE
317 sysval = sysconf(_SC_PAGESIZE);
319 sys_page_size = sysval;
320 #endif /* _SC_PAGESIZE */
321 #endif /* HAVE_SYSCONF */
324 * Get the size of the containing partition, and use this for
325 * defaults and for making sure the new filesystem doesn't
326 * exceed the partition size.
328 retval = ext2fs_get_device_size(device_name, fs->blocksize,
331 com_err(program_name, retval,
332 _("while trying to determine filesystem size"));
336 new_size = parse_num_blocks(new_size_str,
337 fs->super->s_log_block_size);
339 com_err(program_name, 0, _("bad filesystem size - %s"),
345 /* Round down to an even multiple of a pagesize */
346 if (sys_page_size > fs->blocksize)
347 new_size &= ~((sys_page_size / fs->blocksize)-1);
350 if (use_stride >= 0) {
351 if (use_stride >= (int) fs->super->s_blocks_per_group) {
352 com_err(program_name, 0,
353 _("Invalid stride length"));
356 fs->stride = fs->super->s_raid_stride = use_stride;
357 ext2fs_mark_super_dirty(fs);
359 determine_fs_stride(fs);
362 * If we are resizing a plain file, and it's not big enough,
363 * automatically extend it in a sparse fashion by writing the
364 * last requested block.
366 new_file_size = ((__u64) new_size) * fs->blocksize;
367 if ((__u64) new_file_size >
368 (((__u64) 1) << (sizeof(st_buf.st_size)*8 - 1)) - 1)
370 if ((new_file_size > st_buf.st_size) &&
372 if ((ext2fs_llseek(fd, new_file_size-1, SEEK_SET) >= 0) &&
373 (write(fd, "0", 1) == 1))
376 if (!force && (new_size > max_size)) {
377 fprintf(stderr, _("The containing partition (or device)"
378 " is only %u (%dk) blocks.\nYou requested a new size"
379 " of %u blocks.\n\n"), max_size,
380 fs->blocksize / 1024, new_size);
383 if (new_size == fs->super->s_blocks_count) {
384 fprintf(stderr, _("The filesystem is already %u blocks "
385 "long. Nothing to do!\n\n"), new_size);
388 if (mount_flags & EXT2_MF_MOUNTED) {
389 retval = online_resize_fs(fs, mtpt, &new_size, flags);
391 if (!force && ((fs->super->s_lastcheck < fs->super->s_mtime) ||
392 (fs->super->s_state & EXT2_ERROR_FS) ||
393 ((fs->super->s_state & EXT2_VALID_FS) == 0))) {
395 _("Please run 'e2fsck -f %s' first.\n\n"),
399 printf("Resizing the filesystem on %s to %u (%dk) blocks.\n",
400 device_name, new_size, fs->blocksize / 1024);
401 retval = resize_fs(fs, &new_size, flags,
402 ((flags & RESIZE_PERCENT_COMPLETE) ?
403 resize_progress_func : 0));
406 com_err(program_name, retval, _("while trying to resize %s"),
411 printf(_("The filesystem on %s is now %u blocks long.\n\n"),
412 device_name, new_size);
414 if ((st_buf.st_size > new_file_size) &&
416 #ifdef HAVE_FTRUNCATE64
417 ftruncate64(fd, new_file_size);
419 /* Only truncate if new_file_size doesn't overflow off_t */
420 if (((off_t) new_file_size) == new_file_size)
421 ftruncate(fd, (off_t) new_file_size);
426 remove_error_table(&et_ext2_error_table);