1 /* 2 * main.c --- ext2 resizer main program 3 * 4 * Copyright (C) 1997, 1998 by Theodore Ts'o and 5 * PowerQuest, Inc. 6 * 7 * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 by Theodore Ts'o 8 * 9 * %Begin-Header% 10 * This file may be redistributed under the terms of the GNU Public 11 * License. 12 * %End-Header% 13 */ 14 15 #ifndef _LARGEFILE_SOURCE 16 #define _LARGEFILE_SOURCE 17 #endif 18 #ifndef _LARGEFILE64_SOURCE 19 #define _LARGEFILE64_SOURCE 20 #endif 21 22 #include "config.h" 23 #ifdef HAVE_GETOPT_H 24 #include <getopt.h> 25 #else 26 extern char *optarg; 27 extern int optind; 28 #endif 29 #include <unistd.h> 30 #ifdef HAVE_STDLIB_H 31 #include <stdlib.h> 32 #endif 33 #include <sys/types.h> 34 #include <sys/stat.h> 35 #include <fcntl.h> 36 #include <libgen.h> 37 38 #include "e2p/e2p.h" 39 40 #include "resize2fs.h" 41 42 #include "../version.h" 43 44 char *program_name; 45 static char *device_name, *io_options; 46 47 static void usage (char *prog) 48 { 49 fprintf (stderr, _("Usage: %s [-d debug_flags] [-f] [-F] [-M] [-P] " 50 "[-p] device [-b|-s|new_size] [-z undo_file]\n\n"), 51 prog); 52 53 exit (1); 54 } 55 56 static errcode_t resize_progress_func(ext2_resize_t rfs, int pass, 57 unsigned long cur, unsigned long max) 58 { 59 ext2_sim_progmeter progress; 60 const char *label; 61 errcode_t retval; 62 63 progress = (ext2_sim_progmeter) rfs->prog_data; 64 if (max == 0) 65 return 0; 66 if (cur == 0) { 67 if (progress) 68 ext2fs_progress_close(progress); 69 progress = 0; 70 switch (pass) { 71 case E2_RSZ_EXTEND_ITABLE_PASS: 72 label = _("Extending the inode table"); 73 break; 74 case E2_RSZ_BLOCK_RELOC_PASS: 75 label = _("Relocating blocks"); 76 break; 77 case E2_RSZ_INODE_SCAN_PASS: 78 label = _("Scanning inode table"); 79 break; 80 case E2_RSZ_INODE_REF_UPD_PASS: 81 label = _("Updating inode references"); 82 break; 83 case E2_RSZ_MOVE_ITABLE_PASS: 84 label = _("Moving inode table"); 85 break; 86 default: 87 label = _("Unknown pass?!?"); 88 break; 89 } 90 printf(_("Begin pass %d (max = %lu)\n"), pass, max); 91 retval = ext2fs_progress_init(&progress, label, 30, 92 40, max, 0); 93 if (retval) 94 progress = 0; 95 rfs->prog_data = (void *) progress; 96 } 97 if (progress) 98 ext2fs_progress_update(progress, cur); 99 if (cur >= max) { 100 if (progress) 101 ext2fs_progress_close(progress); 102 progress = 0; 103 rfs->prog_data = 0; 104 } 105 return 0; 106 } 107 108 static void determine_fs_stride(ext2_filsys fs) 109 { 110 unsigned int group; 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; 115 116 if (fs->stride) 117 return; 118 num = 0; sum = 0; 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) 122 goto next; 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 || 130 b_stride < 0 || 131 (flexbg_size > 1 && (group % flexbg_size == 0))) 132 goto next; 133 134 /* printf("group %d has stride %d\n", group, b_stride); */ 135 sum += b_stride; 136 num++; 137 138 next: 139 prev_has_sb = has_sb; 140 } 141 142 if (fs->group_desc_count > 12 && num < 3) 143 sum = 0; 144 145 if (num) 146 fs->stride = sum / num; 147 else 148 fs->stride = 0; 149 150 fs->super->s_raid_stride = fs->stride; 151 ext2fs_mark_super_dirty(fs); 152 153 #if 0 154 if (fs->stride) 155 printf("Using RAID stride of %d\n", fs->stride); 156 #endif 157 } 158 159 static void bigalloc_check(ext2_filsys fs, int force) 160 { 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")); 166 exit(1); 167 } 168 } 169 170 static int resize2fs_setup_tdb(const char *device, char *undo_file, 171 io_manager *io_ptr) 172 { 173 errcode_t retval = ENOMEM; 174 const char *tdb_dir = NULL; 175 char *tdb_file = NULL; 176 char *dev_name, *tmp_name; 177 178 /* (re)open a specific undo file */ 179 if (undo_file && undo_file[0] != 0) { 180 retval = set_undo_io_backing_manager(*io_ptr); 181 if (retval) 182 goto err; 183 *io_ptr = undo_io_manager; 184 retval = set_undo_io_backup_file(undo_file); 185 if (retval) 186 goto err; 187 printf(_("Overwriting existing filesystem; this can be undone " 188 "using the command:\n" 189 " e2undo %s %s\n\n"), 190 undo_file, device); 191 return retval; 192 } 193 194 /* 195 * Configuration via a conf file would be 196 * nice 197 */ 198 tdb_dir = getenv("E2FSPROGS_UNDO_DIR"); 199 if (!tdb_dir) 200 tdb_dir = "/var/lib/e2fsprogs"; 201 202 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) || 203 access(tdb_dir, W_OK)) 204 return 0; 205 206 tmp_name = strdup(device); 207 if (!tmp_name) 208 goto errout; 209 dev_name = basename(tmp_name); 210 tdb_file = malloc(strlen(tdb_dir) + 11 + strlen(dev_name) + 7 + 1); 211 if (!tdb_file) { 212 free(tmp_name); 213 goto errout; 214 } 215 sprintf(tdb_file, "%s/resize2fs-%s.e2undo", tdb_dir, dev_name); 216 free(tmp_name); 217 218 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) { 219 retval = errno; 220 com_err(program_name, retval, 221 _("while trying to delete %s"), tdb_file); 222 goto errout; 223 } 224 225 retval = set_undo_io_backing_manager(*io_ptr); 226 if (retval) 227 goto errout; 228 *io_ptr = undo_io_manager; 229 retval = set_undo_io_backup_file(tdb_file); 230 if (retval) 231 goto errout; 232 printf(_("Overwriting existing filesystem; this can be undone " 233 "using the command:\n" 234 " e2undo %s %s\n\n"), tdb_file, device); 235 236 free(tdb_file); 237 return 0; 238 errout: 239 free(tdb_file); 240 err: 241 com_err(program_name, retval, "%s", 242 _("while trying to setup undo file\n")); 243 return retval; 244 } 245 246 int main (int argc, char ** argv) 247 { 248 errcode_t retval; 249 ext2_filsys fs; 250 int c; 251 int flags = 0; 252 int flush = 0; 253 int force = 0; 254 int io_flags = 0; 255 int force_min_size = 0; 256 int print_min_size = 0; 257 int fd, ret; 258 blk64_t new_size = 0; 259 blk64_t max_size = 0; 260 blk64_t min_size = 0; 261 io_manager io_ptr; 262 char *new_size_str = 0; 263 int use_stride = -1; 264 ext2fs_struct_stat st_buf; 265 __s64 new_file_size; 266 unsigned int sys_page_size = 4096; 267 unsigned int blocksize; 268 long sysval; 269 int len, mount_flags; 270 char *mtpt, *undo_file = NULL; 271 272 #ifdef ENABLE_NLS 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); 278 #endif 279 280 add_error_table(&et_ext2_error_table); 281 282 fprintf (stderr, "resize2fs %s (%s)\n", 283 E2FSPROGS_VERSION, E2FSPROGS_DATE); 284 if (argc && *argv) 285 program_name = *argv; 286 287 while ((c = getopt(argc, argv, "d:fFhMPpS:bsz:")) != EOF) { 288 switch (c) { 289 case 'h': 290 usage(program_name); 291 break; 292 case 'f': 293 force = 1; 294 break; 295 case 'F': 296 flush = 1; 297 break; 298 case 'M': 299 force_min_size = 1; 300 break; 301 case 'P': 302 print_min_size = 1; 303 break; 304 case 'd': 305 flags |= atoi(optarg); 306 break; 307 case 'p': 308 flags |= RESIZE_PERCENT_COMPLETE; 309 break; 310 case 'S': 311 use_stride = atoi(optarg); 312 break; 313 case 'b': 314 flags |= RESIZE_ENABLE_64BIT; 315 break; 316 case 's': 317 flags |= RESIZE_DISABLE_64BIT; 318 break; 319 case 'z': 320 undo_file = optarg; 321 break; 322 default: 323 usage(program_name); 324 } 325 } 326 if (optind == argc) 327 usage(program_name); 328 329 device_name = argv[optind++]; 330 if (optind < argc) 331 new_size_str = argv[optind++]; 332 if (optind < argc) 333 usage(program_name); 334 335 io_options = strchr(device_name, '?'); 336 if (io_options) 337 *io_options++ = 0; 338 339 /* 340 * Figure out whether or not the device is mounted, and if it is 341 * where it is mounted. 342 */ 343 len=80; 344 while (1) { 345 mtpt = malloc(len); 346 if (!mtpt) 347 return ENOMEM; 348 mtpt[len-1] = 0; 349 retval = ext2fs_check_mount_point(device_name, &mount_flags, 350 mtpt, len); 351 if (retval) { 352 com_err("ext2fs_check_mount_point", retval, 353 _("while determining whether %s is mounted."), 354 device_name); 355 exit(1); 356 } 357 if (!(mount_flags & EXT2_MF_MOUNTED) || (mtpt[len-1] == 0)) 358 break; 359 free(mtpt); 360 len = 2 * len; 361 } 362 363 fd = ext2fs_open_file(device_name, O_RDWR, 0); 364 if (fd < 0) { 365 com_err("open", errno, _("while opening %s"), 366 device_name); 367 exit(1); 368 } 369 370 ret = ext2fs_fstat(fd, &st_buf); 371 if (ret < 0) { 372 com_err("open", errno, 373 _("while getting stat information for %s"), 374 device_name); 375 exit(1); 376 } 377 378 if (flush) { 379 retval = ext2fs_sync_device(fd, 1); 380 if (retval) { 381 com_err(argv[0], retval, 382 _("while trying to flush %s"), 383 device_name); 384 exit(1); 385 } 386 } 387 388 if (!S_ISREG(st_buf.st_mode )) { 389 close(fd); 390 fd = -1; 391 } 392 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; 397 } else 398 #endif 399 io_ptr = unix_io_manager; 400 401 if (!(mount_flags & EXT2_MF_MOUNTED)) 402 io_flags = EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE; 403 404 io_flags |= EXT2_FLAG_64BITS; 405 if (undo_file) { 406 retval = resize2fs_setup_tdb(device_name, undo_file, &io_ptr); 407 if (retval) 408 exit(1); 409 } 410 retval = ext2fs_open2(device_name, io_options, io_flags, 411 0, 0, io_ptr, &fs); 412 if (retval) { 413 com_err(program_name, retval, _("while trying to open %s"), 414 device_name); 415 printf("%s", _("Couldn't find valid filesystem superblock.\n")); 416 exit (1); 417 } 418 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE; 419 420 /* 421 * Before acting on an unmounted filesystem, make sure it's ok, 422 * unless the user is forcing it. 423 * 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 428 * risky. 429 */ 430 if (!force && !(mount_flags & EXT2_MF_MOUNTED)) { 431 int checkit = 0; 432 433 if (fs->super->s_state & EXT2_ERROR_FS) 434 checkit = 1; 435 436 if ((fs->super->s_state & EXT2_VALID_FS) == 0) 437 checkit = 1; 438 439 if ((fs->super->s_lastcheck < fs->super->s_mtime) && 440 !print_min_size) 441 checkit = 1; 442 443 if (checkit) { 444 fprintf(stderr, 445 _("Please run 'e2fsck -f %s' first.\n\n"), 446 device_name); 447 exit(1); 448 } 449 } 450 451 /* 452 * Check for compatibility with the feature sets. We need to 453 * be more stringent than ext2fs_open(). 454 */ 455 if (fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) { 456 com_err(program_name, EXT2_ET_UNSUPP_FEATURE, 457 "(%s)", device_name); 458 exit(1); 459 } 460 461 min_size = calculate_minimum_resize_size(fs, flags); 462 463 if (print_min_size) { 464 printf(_("Estimated minimum size of the filesystem: %llu\n"), 465 min_size); 466 exit(0); 467 } 468 469 /* Determine the system page size if possible */ 470 #ifdef HAVE_SYSCONF 471 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE)) 472 #define _SC_PAGESIZE _SC_PAGE_SIZE 473 #endif 474 #ifdef _SC_PAGESIZE 475 sysval = sysconf(_SC_PAGESIZE); 476 if (sysval > 0) 477 sys_page_size = sysval; 478 #endif /* _SC_PAGESIZE */ 479 #endif /* HAVE_SYSCONF */ 480 481 /* 482 * Get the size of the containing partition, and use this for 483 * defaults and for making sure the new filesystem doesn't 484 * exceed the partition size. 485 */ 486 blocksize = fs->blocksize; 487 retval = ext2fs_get_device_size2(device_name, blocksize, 488 &max_size); 489 if (retval) { 490 com_err(program_name, retval, "%s", 491 _("while trying to determine filesystem size")); 492 exit(1); 493 } 494 if (force_min_size) 495 new_size = min_size; 496 else if (new_size_str) { 497 new_size = parse_num_blocks2(new_size_str, 498 fs->super->s_log_block_size); 499 if (new_size == 0) { 500 com_err(program_name, 0, 501 _("Invalid new size: %s\n"), new_size_str); 502 exit(1); 503 } 504 } else { 505 new_size = max_size; 506 /* Round down to an even multiple of a pagesize */ 507 if (sys_page_size > blocksize) 508 new_size &= ~((sys_page_size / blocksize)-1); 509 } 510 /* If changing 64bit, don't change the filesystem size. */ 511 if (flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)) { 512 new_size = ext2fs_blocks_count(fs->super); 513 } 514 if (!ext2fs_has_feature_64bit(fs->super)) { 515 /* Take 16T down to 2^32-1 blocks */ 516 if (new_size == (1ULL << 32)) 517 new_size--; 518 else if (new_size > (1ULL << 32)) { 519 com_err(program_name, 0, "%s", 520 _("New size too large to be " 521 "expressed in 32 bits\n")); 522 exit(1); 523 } 524 } 525 526 if (!force && new_size < min_size) { 527 com_err(program_name, 0, 528 _("New size smaller than minimum (%llu)\n"), min_size); 529 exit(1); 530 } 531 if (use_stride >= 0) { 532 if (use_stride >= (int) fs->super->s_blocks_per_group) { 533 com_err(program_name, 0, "%s", 534 _("Invalid stride length")); 535 exit(1); 536 } 537 fs->stride = fs->super->s_raid_stride = use_stride; 538 ext2fs_mark_super_dirty(fs); 539 } else 540 determine_fs_stride(fs); 541 542 /* 543 * If we are resizing a plain file, and it's not big enough, 544 * automatically extend it in a sparse fashion by writing the 545 * last requested block. 546 */ 547 new_file_size = ((__u64) new_size) * blocksize; 548 if ((__u64) new_file_size > 549 (((__u64) 1) << (sizeof(st_buf.st_size)*8 - 1)) - 1) 550 fd = -1; 551 if ((new_file_size > st_buf.st_size) && 552 (fd > 0)) { 553 if ((ext2fs_llseek(fd, new_file_size-1, SEEK_SET) >= 0) && 554 (write(fd, "0", 1) == 1)) 555 max_size = new_size; 556 } 557 if (!force && (new_size > max_size)) { 558 fprintf(stderr, _("The containing partition (or device)" 559 " is only %llu (%dk) blocks.\nYou requested a new size" 560 " of %llu blocks.\n\n"), max_size, 561 blocksize / 1024, new_size); 562 exit(1); 563 } 564 if ((flags & RESIZE_DISABLE_64BIT) && (flags & RESIZE_ENABLE_64BIT)) { 565 fprintf(stderr, _("Cannot set and unset 64bit feature.\n")); 566 exit(1); 567 } else if (flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)) { 568 if (new_size >= (1ULL << 32)) { 569 fprintf(stderr, _("Cannot change the 64bit feature " 570 "on a filesystem that is larger than " 571 "2^32 blocks.\n")); 572 exit(1); 573 } 574 if (mount_flags & EXT2_MF_MOUNTED) { 575 fprintf(stderr, _("Cannot change the 64bit feature " 576 "while the filesystem is mounted.\n")); 577 exit(1); 578 } 579 if (flags & RESIZE_ENABLE_64BIT && 580 !ext2fs_has_feature_extents(fs->super)) { 581 fprintf(stderr, _("Please enable the extents feature " 582 "with tune2fs before enabling the 64bit " 583 "feature.\n")); 584 exit(1); 585 } 586 } else if (new_size == ext2fs_blocks_count(fs->super)) { 587 fprintf(stderr, _("The filesystem is already %llu (%dk) " 588 "blocks long. Nothing to do!\n\n"), new_size, 589 blocksize / 1024); 590 exit(0); 591 } 592 if ((flags & RESIZE_ENABLE_64BIT) && 593 ext2fs_has_feature_64bit(fs->super)) { 594 fprintf(stderr, _("The filesystem is already 64-bit.\n")); 595 exit(0); 596 } 597 if ((flags & RESIZE_DISABLE_64BIT) && 598 !ext2fs_has_feature_64bit(fs->super)) { 599 fprintf(stderr, _("The filesystem is already 32-bit.\n")); 600 exit(0); 601 } 602 if (mount_flags & EXT2_MF_MOUNTED) { 603 bigalloc_check(fs, force); 604 retval = online_resize_fs(fs, mtpt, &new_size, flags); 605 } else { 606 bigalloc_check(fs, force); 607 if (flags & RESIZE_ENABLE_64BIT) 608 printf(_("Converting the filesystem to 64-bit.\n")); 609 else if (flags & RESIZE_DISABLE_64BIT) 610 printf(_("Converting the filesystem to 32-bit.\n")); 611 else 612 printf(_("Resizing the filesystem on " 613 "%s to %llu (%dk) blocks.\n"), 614 device_name, new_size, blocksize / 1024); 615 retval = resize_fs(fs, &new_size, flags, 616 ((flags & RESIZE_PERCENT_COMPLETE) ? 617 resize_progress_func : 0)); 618 } 619 free(mtpt); 620 if (retval) { 621 com_err(program_name, retval, _("while trying to resize %s"), 622 device_name); 623 fprintf(stderr, 624 _("Please run 'e2fsck -fy %s' to fix the filesystem\n" 625 "after the aborted resize operation.\n"), 626 device_name); 627 ext2fs_close_free(&fs); 628 exit(1); 629 } 630 printf(_("The filesystem on %s is now %llu (%dk) blocks long.\n\n"), 631 device_name, new_size, blocksize / 1024); 632 633 if ((st_buf.st_size > new_file_size) && 634 (fd > 0)) { 635 #ifdef HAVE_FTRUNCATE64 636 retval = ftruncate64(fd, new_file_size); 637 #else 638 retval = 0; 639 /* Only truncate if new_file_size doesn't overflow off_t */ 640 if (((off_t) new_file_size) == new_file_size) 641 retval = ftruncate(fd, (off_t) new_file_size); 642 #endif 643 if (retval) 644 com_err(program_name, retval, 645 _("while trying to truncate %s"), 646 device_name); 647 } 648 if (fd > 0) 649 close(fd); 650 remove_error_table(&et_ext2_error_table); 651 return (0); 652 } 653