static errcode_t try_open_fs(e2fsck_t ctx, int flags, io_manager io_ptr, ext2_filsys *ret_fs) { errcode_t retval; *ret_fs = NULL; if (ctx->superblock && ctx->blocksize) { retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options, flags, ctx->superblock, ctx->blocksize, io_ptr, ret_fs); } else if (ctx->superblock) { int blocksize; for (blocksize = EXT2_MIN_BLOCK_SIZE; blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) { if (*ret_fs) { ext2fs_free(*ret_fs); *ret_fs = NULL; } retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options, flags, ctx->superblock, blocksize, io_ptr, ret_fs); if (!retval) break; } } else retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options, flags, 0, 0, io_ptr, ret_fs); return retval; }
static void print_ext2_info(const char *device) { struct ext2_super_block *sb; ext2_filsys fs; errcode_t retval; time_t tm; char buf[80]; retval = ext2fs_open2(device, 0, EXT2_FLAG_64BITS, 0, 0, unix_io_manager, &fs); if (retval) return; sb = fs->super; if (sb->s_mtime) { tm = sb->s_mtime; if (sb->s_last_mounted[0]) { memset(buf, 0, sizeof(buf)); strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted)); printf(_("\tlast mounted on %s on %s"), buf, ctime(&tm)); } else printf(_("\tlast mounted on %s"), ctime(&tm)); } else if (sb->s_mkfs_time) { tm = sb->s_mkfs_time; printf(_("\tcreated on %s"), ctime(&tm)); } else if (sb->s_wtime) { tm = sb->s_wtime; printf(_("\tlast modified on %s"), ctime(&tm)); } ext2fs_close_free(&fs); }
static int ext2lib_mount(fsi_t *fsi, const char *name, const char *options) { int err; char opts[30] = ""; ext2_filsys *fs; uint64_t offset = fsip_fs_offset(fsi); if (offset) snprintf(opts, 29, "offset=%" PRId64, offset); fs = malloc(sizeof (*fs)); if (fs == NULL) return (-1); err = ext2fs_open2(name, opts, 0, 0, 0, unix_io_manager, fs); if (err != 0) { free(fs); errno = EINVAL; return (-1); } fsip_fs_set_data(fsi, fs); return (0); }
errcode_t ext2fs_open(const char *name, int flags, int superblock, unsigned int block_size, io_manager manager, ext2_filsys *ret_fs) { return ext2fs_open2(name, 0, flags, superblock, block_size, manager, ret_fs); }
static int get_resize_stats(struct resize_stats *stats, char *key_loc, char *blk_device) { errcode_t retval; ext2_filsys fs; ext2_filsys dup_fs; io_manager io_ptr; long long data_partition_size = 0; io_ptr = unix_io_manager; retval = ext2fs_open2(blk_device, NULL , 0, 0, 0, io_ptr, &fs); if (retval) { ERROR("Couldn't find valid filesystem superblock.\n"); return -1; } retval = ext2fs_dup_handle(fs, &dup_fs); if (retval) { ERROR("Couldn't duplicate filesys.\n"); ext2fs_close(fs); return -1; } if((data_partition_size = get_block_size(blk_device)) < 0) { ERROR("Get %s partition size fail.\n", blk_device); ext2fs_close(fs); ext2fs_free(dup_fs); return -1; } ERROR("Size for partition(%s) is %lluK.\n", blk_device, data_partition_size / 1024); ERROR("Size in superblock is %lluK.\n", (unsigned long long)fs->super->s_blocks_count * fs->blocksize / 1024); if (!strcmp(key_loc, KEY_IN_FOOTER)){ data_partition_size -= RESERVE_SIZE; ERROR("There is key in footer of partition(%s).\n", blk_device); } //adjust partition size to meet resizefs rule. //resizefs will adjust partition size in some case. retval = adjust_fs_size(dup_fs, &data_partition_size); if (retval) { ERROR("Couldn't adjust partition size.\n"); ext2fs_close(fs); ext2fs_free(dup_fs); return -1; } ERROR("Size will (maybe) resize to(after adjust) is %lluK.\n", data_partition_size / 1024); if((unsigned int)(data_partition_size / fs->blocksize) == (unsigned int)(fs->super->s_blocks_count)) { ERROR("The size of data already meet the request(size in superblock = size after adjust).\n"); stats->state = STATS_RESIZED; } stats->size = data_partition_size; ext2fs_close(fs); ext2fs_free(dup_fs); return 0; }
int main (int argc, char ** argv) { errcode_t retval; ext2_filsys fs; int c; int flags = 0; int flush = 0; int force = 0; int io_flags = 0; int force_min_size = 0; int print_min_size = 0; int fd, ret; blk64_t new_size = 0; blk64_t max_size = 0; blk64_t min_size = 0; io_manager io_ptr; char *new_size_str = 0; int use_stride = -1; ext2fs_struct_stat st_buf; __s64 new_file_size; unsigned int sys_page_size = 4096; long sysval; int len, mount_flags; char *mtpt; #ifdef ENABLE_NLS setlocale(LC_MESSAGES, ""); setlocale(LC_CTYPE, ""); bindtextdomain(NLS_CAT_NAME, LOCALEDIR); textdomain(NLS_CAT_NAME); set_com_err_gettext(gettext); #endif add_error_table(&et_ext2_error_table); fprintf (stderr, "resize2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE); if (argc && *argv) program_name = *argv; while ((c = getopt (argc, argv, "d:fFhMPpS:")) != EOF) { switch (c) { case 'h': usage(program_name); break; case 'f': force = 1; break; case 'F': flush = 1; break; case 'M': force_min_size = 1; break; case 'P': print_min_size = 1; break; case 'd': flags |= atoi(optarg); break; case 'p': flags |= RESIZE_PERCENT_COMPLETE; break; case 'S': use_stride = atoi(optarg); break; default: usage(program_name); } } if (optind == argc) usage(program_name); device_name = argv[optind++]; if (optind < argc) new_size_str = argv[optind++]; if (optind < argc) usage(program_name); io_options = strchr(device_name, '?'); if (io_options) *io_options++ = 0; /* * Figure out whether or not the device is mounted, and if it is * where it is mounted. */ len=80; while (1) { mtpt = malloc(len); if (!mtpt) return ENOMEM; mtpt[len-1] = 0; retval = ext2fs_check_mount_point(device_name, &mount_flags, mtpt, len); if (retval) { com_err("ext2fs_check_mount_point", retval, _("while determining whether %s is mounted."), device_name); exit(1); } if (!(mount_flags & EXT2_MF_MOUNTED) || (mtpt[len-1] == 0)) break; free(mtpt); len = 2 * len; } fd = ext2fs_open_file(device_name, O_RDWR, 0); if (fd < 0) { com_err("open", errno, _("while opening %s"), device_name); exit(1); } ret = ext2fs_fstat(fd, &st_buf); if (ret < 0) { com_err("open", errno, _("while getting stat information for %s"), device_name); exit(1); } if (flush) { retval = ext2fs_sync_device(fd, 1); if (retval) { com_err(argv[0], retval, _("while trying to flush %s"), device_name); exit(1); } } if (!S_ISREG(st_buf.st_mode )) { close(fd); fd = -1; } #ifdef CONFIG_TESTIO_DEBUG if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) { io_ptr = test_io_manager; test_io_backing_manager = unix_io_manager; } else #endif io_ptr = unix_io_manager; if (!(mount_flags & EXT2_MF_MOUNTED)) io_flags = EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE; io_flags |= EXT2_FLAG_64BITS; retval = ext2fs_open2(device_name, io_options, io_flags, 0, 0, io_ptr, &fs); if (retval) { com_err (program_name, retval, _("while trying to open %s"), device_name); printf (_("Couldn't find valid filesystem superblock.\n")); exit (1); } /* * Check for compatibility with the feature sets. We need to * be more stringent than ext2fs_open(). */ if (fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) { com_err(program_name, EXT2_ET_UNSUPP_FEATURE, "(%s)", device_name); exit(1); } min_size = calculate_minimum_resize_size(fs); if (print_min_size) { if (!force && ((fs->super->s_state & EXT2_ERROR_FS) || ((fs->super->s_state & EXT2_VALID_FS) == 0))) { fprintf(stderr, _("Please run 'e2fsck -f %s' first.\n\n"), device_name); exit(1); } printf(_("Estimated minimum size of the filesystem: %llu\n"), min_size); exit(0); } /* Determine the system page size if possible */ #ifdef HAVE_SYSCONF #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE)) #define _SC_PAGESIZE _SC_PAGE_SIZE #endif #ifdef _SC_PAGESIZE sysval = sysconf(_SC_PAGESIZE); if (sysval > 0) sys_page_size = sysval; #endif /* _SC_PAGESIZE */ #endif /* HAVE_SYSCONF */ /* * Get the size of the containing partition, and use this for * defaults and for making sure the new filesystem doesn't * exceed the partition size. */ retval = ext2fs_get_device_size2(device_name, fs->blocksize, &max_size); if (retval) { com_err(program_name, retval, _("while trying to determine filesystem size")); exit(1); } if (force_min_size) new_size = min_size; else if (new_size_str) { new_size = parse_num_blocks2(new_size_str, fs->super->s_log_block_size); if (new_size == 0) { com_err(program_name, 0, _("Invalid new size: %s\n"), new_size_str); exit(1); } } else { new_size = max_size; /* Round down to an even multiple of a pagesize */ if (sys_page_size > fs->blocksize) new_size &= ~((sys_page_size / fs->blocksize)-1); } if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super, EXT4_FEATURE_INCOMPAT_64BIT)) { /* Take 16T down to 2^32-1 blocks */ if (new_size == (1ULL << 32)) new_size--; else if (new_size > (1ULL << 32)) { com_err(program_name, 0, _("New size too large to be " "expressed in 32 bits\n")); exit(1); } } if (!force && new_size < min_size) { com_err(program_name, 0, _("New size smaller than minimum (%llu)\n"), min_size); exit(1); } if (use_stride >= 0) { if (use_stride >= (int) fs->super->s_blocks_per_group) { com_err(program_name, 0, _("Invalid stride length")); exit(1); } fs->stride = fs->super->s_raid_stride = use_stride; ext2fs_mark_super_dirty(fs); } else determine_fs_stride(fs); /* * If we are resizing a plain file, and it's not big enough, * automatically extend it in a sparse fashion by writing the * last requested block. */ new_file_size = ((__u64) new_size) * fs->blocksize; if ((__u64) new_file_size > (((__u64) 1) << (sizeof(st_buf.st_size)*8 - 1)) - 1) fd = -1; if ((new_file_size > st_buf.st_size) && (fd > 0)) { if ((ext2fs_llseek(fd, new_file_size-1, SEEK_SET) >= 0) && (write(fd, "0", 1) == 1)) max_size = new_size; } if (!force && (new_size > max_size)) { fprintf(stderr, _("The containing partition (or device)" " is only %llu (%dk) blocks.\nYou requested a new size" " of %llu blocks.\n\n"), max_size, fs->blocksize / 1024, new_size); exit(1); } if (new_size == ext2fs_blocks_count(fs->super)) { fprintf(stderr, _("The filesystem is already %llu blocks " "long. Nothing to do!\n\n"), new_size); exit(0); } if (mount_flags & EXT2_MF_MOUNTED) { retval = online_resize_fs(fs, mtpt, &new_size, flags); } else { if (!force && ((fs->super->s_lastcheck < fs->super->s_mtime) || (fs->super->s_state & EXT2_ERROR_FS) || ((fs->super->s_state & EXT2_VALID_FS) == 0))) { fprintf(stderr, _("Please run 'e2fsck -f %s' first.\n\n"), device_name); exit(1); } /* * XXXX The combination of flex_bg and !resize_inode * causes major problems for resize2fs, since when the * group descriptors grow in size this can potentially * require multiple inode tables to be moved aside to * make room, and resize2fs chokes rather badly in * this scenario. It's a rare combination, except * when a filesystem is expanded more than a certain * size, so for now, we'll just prohibit that * combination. This is something we should fix * eventually, though. */ if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) && !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) { com_err(program_name, 0, _("%s: The combination of " "flex_bg and\n\t!resize_inode features " "is not supported by resize2fs.\n"), device_name); exit(1); } printf(_("Resizing the filesystem on " "%s to %llu (%dk) blocks.\n"), device_name, new_size, fs->blocksize / 1024); retval = resize_fs(fs, &new_size, flags, ((flags & RESIZE_PERCENT_COMPLETE) ? resize_progress_func : 0)); } free(mtpt); if (retval) { com_err(program_name, retval, _("while trying to resize %s"), device_name); fprintf(stderr, _("Please run 'e2fsck -fy %s' to fix the filesystem\n" "after the aborted resize operation.\n"), device_name); ext2fs_close(fs); exit(1); } printf(_("The filesystem on %s is now %llu blocks long.\n\n"), device_name, new_size); if ((st_buf.st_size > new_file_size) && (fd > 0)) { #ifdef HAVE_FTRUNCATE64 retval = ftruncate64(fd, new_file_size); #else retval = 0; /* Only truncate if new_file_size doesn't overflow off_t */ if (((off_t) new_file_size) == new_file_size) retval = ftruncate(fd, (off_t) new_file_size); #endif if (retval) com_err(program_name, retval, _("while trying to truncate %s"), device_name); } if (fd > 0) close(fd); remove_error_table(&et_ext2_error_table); return (0); }
int main (int argc, char ** argv) { errcode_t retval; ext2_filsys fs; int c; int flags = 0; int flush = 0; int force = 0; int io_flags = 0; int force_min_size = 0; int print_min_size = 0; int fd, ret; blk64_t new_size = 0; blk64_t max_size = 0; blk64_t min_size = 0; io_manager io_ptr; char *new_size_str = 0; int use_stride = -1; ext2fs_struct_stat st_buf; __s64 new_file_size; unsigned int sys_page_size = 4096; unsigned int blocksize; long sysval; int len, mount_flags; char *mtpt, *undo_file = NULL; #ifdef ENABLE_NLS setlocale(LC_MESSAGES, ""); setlocale(LC_CTYPE, ""); bindtextdomain(NLS_CAT_NAME, LOCALEDIR); textdomain(NLS_CAT_NAME); set_com_err_gettext(gettext); #endif add_error_table(&et_ext2_error_table); fprintf (stderr, "resize2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE); if (argc && *argv) program_name = *argv; while ((c = getopt(argc, argv, "d:fFhMPpS:bsz:")) != EOF) { switch (c) { case 'h': usage(program_name); break; case 'f': force = 1; break; case 'F': flush = 1; break; case 'M': force_min_size = 1; break; case 'P': print_min_size = 1; break; case 'd': flags |= atoi(optarg); break; case 'p': flags |= RESIZE_PERCENT_COMPLETE; break; case 'S': use_stride = atoi(optarg); break; case 'b': flags |= RESIZE_ENABLE_64BIT; break; case 's': flags |= RESIZE_DISABLE_64BIT; break; case 'z': undo_file = optarg; break; default: usage(program_name); } } if (optind == argc) usage(program_name); device_name = argv[optind++]; if (optind < argc) new_size_str = argv[optind++]; if (optind < argc) usage(program_name); io_options = strchr(device_name, '?'); if (io_options) *io_options++ = 0; /* * Figure out whether or not the device is mounted, and if it is * where it is mounted. */ len=80; while (1) { mtpt = malloc(len); if (!mtpt) return ENOMEM; mtpt[len-1] = 0; retval = ext2fs_check_mount_point(device_name, &mount_flags, mtpt, len); if (retval) { com_err("ext2fs_check_mount_point", retval, _("while determining whether %s is mounted."), device_name); exit(1); } if (!(mount_flags & EXT2_MF_MOUNTED) || (mtpt[len-1] == 0)) break; free(mtpt); len = 2 * len; } fd = ext2fs_open_file(device_name, O_RDWR, 0); if (fd < 0) { com_err("open", errno, _("while opening %s"), device_name); exit(1); } ret = ext2fs_fstat(fd, &st_buf); if (ret < 0) { com_err("open", errno, _("while getting stat information for %s"), device_name); exit(1); } if (flush) { retval = ext2fs_sync_device(fd, 1); if (retval) { com_err(argv[0], retval, _("while trying to flush %s"), device_name); exit(1); } } if (!S_ISREG(st_buf.st_mode )) { close(fd); fd = -1; } #ifdef CONFIG_TESTIO_DEBUG if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) { io_ptr = test_io_manager; test_io_backing_manager = unix_io_manager; } else #endif io_ptr = unix_io_manager; if (!(mount_flags & EXT2_MF_MOUNTED)) io_flags = EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE; io_flags |= EXT2_FLAG_64BITS; if (undo_file) { retval = resize2fs_setup_tdb(device_name, undo_file, &io_ptr); if (retval) exit(1); } retval = ext2fs_open2(device_name, io_options, io_flags, 0, 0, io_ptr, &fs); if (retval) { com_err(program_name, retval, _("while trying to open %s"), device_name); printf("%s", _("Couldn't find valid filesystem superblock.\n")); exit (1); } fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE; /* * Before acting on an unmounted filesystem, make sure it's ok, * unless the user is forcing it. * * We do ERROR and VALID checks even if we're only printing the * minimimum size, because traversal of a badly damaged filesystem * can cause issues as well. We don't require it to be fscked after * the last mount time in this case, though, as this is a bit less * risky. */ if (!force && !(mount_flags & EXT2_MF_MOUNTED)) { int checkit = 0; if (fs->super->s_state & EXT2_ERROR_FS) checkit = 1; if ((fs->super->s_state & EXT2_VALID_FS) == 0) checkit = 1; if ((fs->super->s_lastcheck < fs->super->s_mtime) && !print_min_size) checkit = 1; if (checkit) { fprintf(stderr, _("Please run 'e2fsck -f %s' first.\n\n"), device_name); exit(1); } } /* * Check for compatibility with the feature sets. We need to * be more stringent than ext2fs_open(). */ if (fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) { com_err(program_name, EXT2_ET_UNSUPP_FEATURE, "(%s)", device_name); exit(1); } min_size = calculate_minimum_resize_size(fs, flags); if (print_min_size) { printf(_("Estimated minimum size of the filesystem: %llu\n"), min_size); exit(0); } /* Determine the system page size if possible */ #ifdef HAVE_SYSCONF #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE)) #define _SC_PAGESIZE _SC_PAGE_SIZE #endif #ifdef _SC_PAGESIZE sysval = sysconf(_SC_PAGESIZE); if (sysval > 0) sys_page_size = sysval; #endif /* _SC_PAGESIZE */ #endif /* HAVE_SYSCONF */ /* * Get the size of the containing partition, and use this for * defaults and for making sure the new filesystem doesn't * exceed the partition size. */ blocksize = fs->blocksize; retval = ext2fs_get_device_size2(device_name, blocksize, &max_size); if (retval) { com_err(program_name, retval, "%s", _("while trying to determine filesystem size")); exit(1); } if (force_min_size) new_size = min_size; else if (new_size_str) { new_size = parse_num_blocks2(new_size_str, fs->super->s_log_block_size); if (new_size == 0) { com_err(program_name, 0, _("Invalid new size: %s\n"), new_size_str); exit(1); } } else { new_size = max_size; /* Round down to an even multiple of a pagesize */ if (sys_page_size > blocksize) new_size &= ~((sys_page_size / blocksize)-1); } /* If changing 64bit, don't change the filesystem size. */ if (flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)) { new_size = ext2fs_blocks_count(fs->super); } if (!ext2fs_has_feature_64bit(fs->super)) { /* Take 16T down to 2^32-1 blocks */ if (new_size == (1ULL << 32)) new_size--; else if (new_size > (1ULL << 32)) { com_err(program_name, 0, "%s", _("New size too large to be " "expressed in 32 bits\n")); exit(1); } } if (!force && new_size < min_size) { com_err(program_name, 0, _("New size smaller than minimum (%llu)\n"), min_size); exit(1); } if (use_stride >= 0) { if (use_stride >= (int) fs->super->s_blocks_per_group) { com_err(program_name, 0, "%s", _("Invalid stride length")); exit(1); } fs->stride = fs->super->s_raid_stride = use_stride; ext2fs_mark_super_dirty(fs); } else determine_fs_stride(fs); /* * If we are resizing a plain file, and it's not big enough, * automatically extend it in a sparse fashion by writing the * last requested block. */ new_file_size = ((__u64) new_size) * blocksize; if ((__u64) new_file_size > (((__u64) 1) << (sizeof(st_buf.st_size)*8 - 1)) - 1) fd = -1; if ((new_file_size > st_buf.st_size) && (fd > 0)) { if ((ext2fs_llseek(fd, new_file_size-1, SEEK_SET) >= 0) && (write(fd, "0", 1) == 1)) max_size = new_size; } if (!force && (new_size > max_size)) { fprintf(stderr, _("The containing partition (or device)" " is only %llu (%dk) blocks.\nYou requested a new size" " of %llu blocks.\n\n"), max_size, blocksize / 1024, new_size); exit(1); } if ((flags & RESIZE_DISABLE_64BIT) && (flags & RESIZE_ENABLE_64BIT)) { fprintf(stderr, _("Cannot set and unset 64bit feature.\n")); exit(1); } else if (flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)) { if (new_size >= (1ULL << 32)) { fprintf(stderr, _("Cannot change the 64bit feature " "on a filesystem that is larger than " "2^32 blocks.\n")); exit(1); } if (mount_flags & EXT2_MF_MOUNTED) { fprintf(stderr, _("Cannot change the 64bit feature " "while the filesystem is mounted.\n")); exit(1); } if (flags & RESIZE_ENABLE_64BIT && !ext2fs_has_feature_extents(fs->super)) { fprintf(stderr, _("Please enable the extents feature " "with tune2fs before enabling the 64bit " "feature.\n")); exit(1); } } else if (new_size == ext2fs_blocks_count(fs->super)) { fprintf(stderr, _("The filesystem is already %llu (%dk) " "blocks long. Nothing to do!\n\n"), new_size, blocksize / 1024); exit(0); } if ((flags & RESIZE_ENABLE_64BIT) && ext2fs_has_feature_64bit(fs->super)) { fprintf(stderr, _("The filesystem is already 64-bit.\n")); exit(0); } if ((flags & RESIZE_DISABLE_64BIT) && !ext2fs_has_feature_64bit(fs->super)) { fprintf(stderr, _("The filesystem is already 32-bit.\n")); exit(0); } if (mount_flags & EXT2_MF_MOUNTED) { bigalloc_check(fs, force); retval = online_resize_fs(fs, mtpt, &new_size, flags); } else { bigalloc_check(fs, force); if (flags & RESIZE_ENABLE_64BIT) printf(_("Converting the filesystem to 64-bit.\n")); else if (flags & RESIZE_DISABLE_64BIT) printf(_("Converting the filesystem to 32-bit.\n")); else printf(_("Resizing the filesystem on " "%s to %llu (%dk) blocks.\n"), device_name, new_size, blocksize / 1024); retval = resize_fs(fs, &new_size, flags, ((flags & RESIZE_PERCENT_COMPLETE) ? resize_progress_func : 0)); } free(mtpt); if (retval) { com_err(program_name, retval, _("while trying to resize %s"), device_name); fprintf(stderr, _("Please run 'e2fsck -fy %s' to fix the filesystem\n" "after the aborted resize operation.\n"), device_name); ext2fs_close_free(&fs); exit(1); } printf(_("The filesystem on %s is now %llu (%dk) blocks long.\n\n"), device_name, new_size, blocksize / 1024); if ((st_buf.st_size > new_file_size) && (fd > 0)) { #ifdef HAVE_FTRUNCATE64 retval = ftruncate64(fd, new_file_size); #else retval = 0; /* Only truncate if new_file_size doesn't overflow off_t */ if (((off_t) new_file_size) == new_file_size) retval = ftruncate(fd, (off_t) new_file_size); #endif if (retval) com_err(program_name, retval, _("while trying to truncate %s"), device_name); } if (fd > 0) close(fd); remove_error_table(&et_ext2_error_table); return (0); }
int main(int argc, char *argv[]) { int c, force = 0, dry_run = 0, verbose = 0, dump = 0; io_channel channel; errcode_t retval; int mount_flags, csum_error = 0, io_error = 0; size_t i, keys_per_block; char *device_name, *tdb_file; io_manager manager = unix_io_manager; struct undo_context undo_ctx; char *buf; struct undo_key_block *keyb; struct undo_key *dkey; struct undo_key_info *ikey; __u32 key_crc, blk_crc, hdr_crc; blk64_t lblk; ext2_filsys fs; __u64 offset = 0; char opt_offset_string[40] = { 0 }; #ifdef ENABLE_NLS setlocale(LC_MESSAGES, ""); setlocale(LC_CTYPE, ""); bindtextdomain(NLS_CAT_NAME, LOCALEDIR); textdomain(NLS_CAT_NAME); set_com_err_gettext(gettext); #endif add_error_table(&et_ext2_error_table); prg_name = argv[0]; while ((c = getopt(argc, argv, "fhno:vz:")) != EOF) { switch (c) { case 'f': force = 1; break; case 'h': dump = 1; break; case 'n': dry_run = 1; break; case 'o': offset = strtoull(optarg, &buf, 0); if (*buf) { com_err(prg_name, 0, _("illegal offset - %s"), optarg); exit(1); } /* used to indicate that an offset was specified */ opt_offset_string[0] = 1; break; case 'v': verbose = 1; break; case 'z': undo_file = optarg; break; default: usage(); } } if (argc != optind + 2) usage(); tdb_file = argv[optind]; device_name = argv[optind+1]; if (undo_file && strcmp(tdb_file, undo_file) == 0) { printf(_("Will not write to an undo file while replaying it.\n")); exit(1); } /* Interpret the undo file */ retval = manager->open(tdb_file, IO_FLAG_EXCLUSIVE, &undo_ctx.undo_file); if (retval) { com_err(prg_name, errno, _("while opening undo file `%s'\n"), tdb_file); exit(1); } retval = io_channel_read_blk64(undo_ctx.undo_file, 0, -(int)sizeof(undo_ctx.hdr), &undo_ctx.hdr); if (retval) { com_err(prg_name, retval, _("while reading undo file")); exit(1); } if (memcmp(undo_ctx.hdr.magic, E2UNDO_MAGIC, sizeof(undo_ctx.hdr.magic))) { fprintf(stderr, _("%s: Not an undo file.\n"), tdb_file); exit(1); } if (dump) { dump_header(&undo_ctx.hdr); exit(1); } hdr_crc = ext2fs_crc32c_le(~0, (unsigned char *)&undo_ctx.hdr, sizeof(struct undo_header) - sizeof(__u32)); if (!force && ext2fs_le32_to_cpu(undo_ctx.hdr.header_crc) != hdr_crc) { fprintf(stderr, _("%s: Header checksum doesn't match.\n"), tdb_file); exit(1); } undo_ctx.blocksize = ext2fs_le32_to_cpu(undo_ctx.hdr.block_size); undo_ctx.fs_blocksize = ext2fs_le32_to_cpu(undo_ctx.hdr.fs_block_size); if (undo_ctx.blocksize == 0 || undo_ctx.fs_blocksize == 0) { fprintf(stderr, _("%s: Corrupt undo file header.\n"), tdb_file); exit(1); } if (!force && undo_ctx.blocksize > E2UNDO_MAX_BLOCK_SIZE) { fprintf(stderr, _("%s: Undo block size too large.\n"), tdb_file); exit(1); } if (!force && undo_ctx.blocksize < E2UNDO_MIN_BLOCK_SIZE) { fprintf(stderr, _("%s: Undo block size too small.\n"), tdb_file); exit(1); } undo_ctx.super_block = ext2fs_le64_to_cpu(undo_ctx.hdr.super_offset); undo_ctx.num_keys = ext2fs_le64_to_cpu(undo_ctx.hdr.num_keys); io_channel_set_blksize(undo_ctx.undo_file, undo_ctx.blocksize); /* * Do not compare undo_ctx.hdr.f_compat with the available compatible * features set, because a "missing" compatible feature should * not cause any problems. */ if (!force && (undo_ctx.hdr.f_incompat || undo_ctx.hdr.f_rocompat)) { fprintf(stderr, _("%s: Unknown undo file feature set.\n"), tdb_file); exit(1); } /* open the fs */ retval = ext2fs_check_if_mounted(device_name, &mount_flags); if (retval) { com_err(prg_name, retval, _("Error while determining whether " "%s is mounted."), device_name); exit(1); } if (mount_flags & EXT2_MF_MOUNTED) { com_err(prg_name, retval, "%s", _("e2undo should only be run " "on unmounted filesystems")); exit(1); } if (undo_file) { retval = e2undo_setup_tdb(device_name, &manager); if (retval) exit(1); } retval = manager->open(device_name, IO_FLAG_EXCLUSIVE | (dry_run ? 0 : IO_FLAG_RW), &channel); if (retval) { com_err(prg_name, retval, _("while opening `%s'"), device_name); exit(1); } if (*opt_offset_string || e2undo_has_feature_fs_offset(&undo_ctx.hdr)) { if (!*opt_offset_string) offset = ext2fs_le64_to_cpu(undo_ctx.hdr.fs_offset); retval = snprintf(opt_offset_string, sizeof(opt_offset_string), "offset=%llu", offset); if ((size_t) retval >= sizeof(opt_offset_string)) { /* should not happen... */ com_err(prg_name, 0, _("specified offset is too large")); exit(1); } io_channel_set_options(channel, opt_offset_string); } if (!force && check_filesystem(&undo_ctx, channel)) exit(1); /* prepare to read keys */ retval = ext2fs_get_mem(sizeof(struct undo_key_info) * undo_ctx.num_keys, &undo_ctx.keys); if (retval) { com_err(prg_name, retval, "%s", _("while allocating memory")); exit(1); } ikey = undo_ctx.keys; retval = ext2fs_get_mem(undo_ctx.blocksize, &keyb); if (retval) { com_err(prg_name, retval, "%s", _("while allocating memory")); exit(1); } retval = ext2fs_get_mem(E2UNDO_MAX_EXTENT_BLOCKS * undo_ctx.blocksize, &buf); if (retval) { com_err(prg_name, retval, "%s", _("while allocating memory")); exit(1); } /* load keys */ keys_per_block = KEYS_PER_BLOCK(&undo_ctx); lblk = ext2fs_le64_to_cpu(undo_ctx.hdr.key_offset); dbg_printf("nr_keys=%lu, kpb=%zu, blksz=%u\n", undo_ctx.num_keys, keys_per_block, undo_ctx.blocksize); for (i = 0; i < undo_ctx.num_keys; i += keys_per_block) { size_t j, max_j; __le32 crc; retval = io_channel_read_blk64(undo_ctx.undo_file, lblk, 1, keyb); if (retval) { com_err(prg_name, retval, "%s", _("while reading keys")); if (force) { io_error = 1; undo_ctx.num_keys = i - 1; break; } exit(1); } /* check keys */ if (!force && ext2fs_le32_to_cpu(keyb->magic) != KEYBLOCK_MAGIC) { fprintf(stderr, _("%s: wrong key magic at %llu\n"), tdb_file, lblk); exit(1); } crc = keyb->crc; keyb->crc = 0; key_crc = ext2fs_crc32c_le(~0, (unsigned char *)keyb, undo_ctx.blocksize); if (!force && ext2fs_le32_to_cpu(crc) != key_crc) { fprintf(stderr, _("%s: key block checksum error at %llu.\n"), tdb_file, lblk); exit(1); } /* load keys from key block */ lblk++; max_j = undo_ctx.num_keys - i; if (max_j > keys_per_block) max_j = keys_per_block; for (j = 0, dkey = keyb->keys; j < max_j; j++, ikey++, dkey++) { ikey->fsblk = ext2fs_le64_to_cpu(dkey->fsblk); ikey->fileblk = lblk; ikey->blk_crc = ext2fs_le32_to_cpu(dkey->blk_crc); ikey->size = ext2fs_le32_to_cpu(dkey->size); lblk += (ikey->size + undo_ctx.blocksize - 1) / undo_ctx.blocksize; if (E2UNDO_MAX_EXTENT_BLOCKS * undo_ctx.blocksize < ikey->size) { com_err(prg_name, retval, _("%s: block %llu is too long."), tdb_file, ikey->fsblk); exit(1); } /* check each block's crc */ retval = io_channel_read_blk64(undo_ctx.undo_file, ikey->fileblk, -(int)ikey->size, buf); if (retval) { com_err(prg_name, retval, _("while fetching block %llu."), ikey->fileblk); if (!force) exit(1); io_error = 1; continue; } blk_crc = ext2fs_crc32c_le(~0, (unsigned char *)buf, ikey->size); if (blk_crc != ikey->blk_crc) { fprintf(stderr, _("checksum error in filesystem block " "%llu (undo blk %llu)\n"), ikey->fsblk, ikey->fileblk); if (!force) exit(1); csum_error = 1; } } } ext2fs_free_mem(&keyb); /* sort keys in fs block order */ qsort(undo_ctx.keys, undo_ctx.num_keys, sizeof(struct undo_key_info), key_compare); /* replay */ io_channel_set_blksize(channel, undo_ctx.fs_blocksize); for (i = 0, ikey = undo_ctx.keys; i < undo_ctx.num_keys; i++, ikey++) { retval = io_channel_read_blk64(undo_ctx.undo_file, ikey->fileblk, -(int)ikey->size, buf); if (retval) { com_err(prg_name, retval, _("while fetching block %llu."), ikey->fileblk); io_error = 1; continue; } if (verbose) printf("Replayed block of size %u from %llu to %llu\n", ikey->size, ikey->fileblk, ikey->fsblk); if (dry_run) continue; retval = io_channel_write_blk64(channel, ikey->fsblk, -(int)ikey->size, buf); if (retval) { com_err(prg_name, retval, _("while writing block %llu."), ikey->fsblk); io_error = 1; } } if (csum_error) fprintf(stderr, _("Undo file corruption; run e2fsck NOW!\n")); if (io_error) fprintf(stderr, _("IO error during replay; run e2fsck NOW!\n")); if (!(ext2fs_le32_to_cpu(undo_ctx.hdr.state) & E2UNDO_STATE_FINISHED)) { force = 1; fprintf(stderr, _("Incomplete undo record; run e2fsck.\n")); } ext2fs_free_mem(&buf); ext2fs_free_mem(&undo_ctx.keys); io_channel_close(channel); /* If there were problems, try to force a fsck */ if (!dry_run && (force || csum_error || io_error)) { retval = ext2fs_open2(device_name, NULL, EXT2_FLAG_RW | EXT2_FLAG_64BITS, 0, 0, manager, &fs); if (retval) goto out; fs->super->s_state &= ~EXT2_VALID_FS; if (csum_error || io_error) fs->super->s_state |= EXT2_ERROR_FS; ext2fs_mark_super_dirty(fs); ext2fs_close_free(&fs); } out: io_channel_close(undo_ctx.undo_file); return csum_error; }
int main (int argc, char *argv[]) { errcode_t retval = 0; int exit_value = FSCK_OK; ext2_filsys fs = 0; io_manager io_ptr; struct ext2_super_block *sb; const char *lib_ver_date; int my_ver, lib_ver; e2fsck_t ctx; struct problem_context pctx; int flags, run_result; clear_problem_context(&pctx); #ifdef MTRACE mtrace(); #endif #ifdef MCHECK mcheck(0); #endif #ifdef ENABLE_NLS setlocale(LC_MESSAGES, ""); setlocale(LC_CTYPE, ""); bindtextdomain(NLS_CAT_NAME, LOCALEDIR); textdomain(NLS_CAT_NAME); #endif my_ver = ext2fs_parse_version_string(my_ver_string); lib_ver = ext2fs_get_library_version(0, &lib_ver_date); if (my_ver > lib_ver) { fprintf( stderr, _("Error: ext2fs library version " "out of date!\n")); show_version_only++; } retval = PRS(argc, argv, &ctx); if (retval) { com_err("e2fsck", retval, _("while trying to initialize program")); exit(FSCK_ERROR); } reserve_stdio_fds(); #ifdef RESOURCE_TRACK init_resource_track(&ctx->global_rtrack); #endif if (!(ctx->options & E2F_OPT_PREEN) || show_version_only) fprintf(stderr, "e2fsck %s (%s)\n", my_ver_string, my_ver_date); if (show_version_only) { fprintf(stderr, _("\tUsing %s, %s\n"), error_message(EXT2_ET_BASE), lib_ver_date); exit(FSCK_OK); } check_mount(ctx); if (!(ctx->options & E2F_OPT_PREEN) && !(ctx->options & E2F_OPT_NO) && !(ctx->options & E2F_OPT_YES)) { if (!ctx->interactive) fatal_error(ctx, _("need terminal for interactive repairs")); } ctx->superblock = ctx->use_superblock; restart: #ifdef CONFIG_TESTIO_DEBUG io_ptr = test_io_manager; test_io_backing_manager = unix_io_manager; #else io_ptr = unix_io_manager; #endif flags = 0; if ((ctx->options & E2F_OPT_READONLY) == 0) flags |= EXT2_FLAG_RW; if ((ctx->mount_flags & EXT2_MF_MOUNTED) == 0) flags |= EXT2_FLAG_EXCLUSIVE; if (ctx->superblock && ctx->blocksize) { retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options, flags, ctx->superblock, ctx->blocksize, io_ptr, &fs); } else if (ctx->superblock) { int blocksize; for (blocksize = EXT2_MIN_BLOCK_SIZE; blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) { retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options, flags, ctx->superblock, blocksize, io_ptr, &fs); if (!retval) break; } } else retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options, flags, 0, 0, io_ptr, &fs); if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) && !(ctx->flags & E2F_FLAG_SB_SPECIFIED) && ((retval == EXT2_ET_BAD_MAGIC) || ((retval == 0) && ext2fs_check_desc(fs)))) { if (!fs || (fs->group_desc_count > 1)) { printf(_("%s trying backup blocks...\n"), retval ? _("Couldn't find ext2 superblock,") : _("Group descriptors look bad...")); get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr); if (fs) ext2fs_close(fs); goto restart; } } if (retval) { com_err(ctx->program_name, retval, _("while trying to open %s"), ctx->filesystem_name); if (retval == EXT2_ET_REV_TOO_HIGH) { printf(_("The filesystem revision is apparently " "too high for this version of e2fsck.\n" "(Or the filesystem superblock " "is corrupt)\n\n")); fix_problem(ctx, PR_0_SB_CORRUPT, &pctx); } else if (retval == EXT2_ET_SHORT_READ) printf(_("Could this be a zero-length partition?\n")); else if ((retval == EPERM) || (retval == EACCES)) printf(_("You must have %s access to the " "filesystem or be root\n"), (ctx->options & E2F_OPT_READONLY) ? "r/o" : "r/w"); else if (retval == ENXIO) printf(_("Possibly non-existent or swap device?\n")); else if (retval == EBUSY) printf(_("Filesystem mounted or opened exclusively " "by another program?\n")); #ifdef EROFS else if (retval == EROFS) printf(_("Disk write-protected; use the -n option " "to do a read-only\n" "check of the device.\n")); #endif else fix_problem(ctx, PR_0_SB_CORRUPT, &pctx); fatal_error(ctx, 0); } ctx->fs = fs; fs->priv_data = ctx; fs->now = ctx->now; sb = fs->super; if (sb->s_rev_level > E2FSCK_CURRENT_REV) { com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH, _("while trying to open %s"), ctx->filesystem_name); get_newer: fatal_error(ctx, _("Get a newer version of e2fsck!")); } /* * Set the device name, which is used whenever we print error * or informational messages to the user. */ if (ctx->device_name == 0 && (sb->s_volume_name[0] != 0)) { ctx->device_name = string_copy(ctx, sb->s_volume_name, sizeof(sb->s_volume_name)); } if (ctx->device_name == 0) ctx->device_name = ctx->filesystem_name; /* * Make sure the ext3 superblock fields are consistent. */ retval = e2fsck_check_ext3_journal(ctx); if (retval) { com_err(ctx->program_name, retval, _("while checking ext3 journal for %s"), ctx->device_name); fatal_error(ctx, 0); } /* * Check to see if we need to do ext3-style recovery. If so, * do it, and then restart the fsck. */ if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) { if (ctx->options & E2F_OPT_READONLY) { printf(_("Warning: skipping journal recovery " "because doing a read-only filesystem " "check.\n")); io_channel_flush(ctx->fs->io); } else { if (ctx->flags & E2F_FLAG_RESTARTED) { /* * Whoops, we attempted to run the * journal twice. This should never * happen, unless the hardware or * device driver is being bogus. */ com_err(ctx->program_name, 0, _("unable to set superblock flags on %s\n"), ctx->device_name); fatal_error(ctx, 0); } retval = e2fsck_run_ext3_journal(ctx); if (retval) { com_err(ctx->program_name, retval, _("while recovering ext3 journal of %s"), ctx->device_name); fatal_error(ctx, 0); } ext2fs_close(ctx->fs); ctx->fs = 0; ctx->flags |= E2F_FLAG_RESTARTED; goto restart; } } /* * Check for compatibility with the feature sets. We need to * be more stringent than ext2fs_open(). */ if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) || (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) { com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE, "(%s)", ctx->device_name); goto get_newer; } if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) { com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE, "(%s)", ctx->device_name); goto get_newer; } #ifdef ENABLE_COMPRESSION if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION) com_err(ctx->program_name, 0, _("Warning: compression support is experimental.\n")); #endif #ifndef ENABLE_HTREE if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) { com_err(ctx->program_name, 0, _("E2fsck not compiled with HTREE support,\n\t" "but filesystem %s has HTREE directories.\n"), ctx->device_name); goto get_newer; } #endif /* * If the user specified a specific superblock, presumably the * master superblock has been trashed. So we mark the * superblock as dirty, so it can be written out. */ if (ctx->superblock && !(ctx->options & E2F_OPT_READONLY)) ext2fs_mark_super_dirty(fs); /* * We only update the master superblock because (a) paranoia; * we don't want to corrupt the backup superblocks, and (b) we * don't need to update the mount count and last checked * fields in the backup superblock (the kernel doesn't * update the backup superblocks anyway). */ fs->flags |= EXT2_FLAG_MASTER_SB_ONLY; ehandler_init(fs->io); if (ctx->superblock) set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0); ext2fs_mark_valid(fs); check_super_block(ctx); if (ctx->flags & E2F_FLAG_SIGNAL_MASK) fatal_error(ctx, 0); check_if_skip(ctx); if (bad_blocks_file) read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks); else if (cflag) read_bad_blocks_file(ctx, 0, !keep_bad_blocks); /* Test disk */ if (ctx->flags & E2F_FLAG_SIGNAL_MASK) fatal_error(ctx, 0); #ifdef ENABLE_SWAPFS if (normalize_swapfs) { if ((fs->flags & EXT2_FLAG_SWAP_BYTES) == ext2fs_native_flag()) { fprintf(stderr, _("%s: Filesystem byte order " "already normalized.\n"), ctx->device_name); fatal_error(ctx, 0); } } if (swapfs) { swap_filesys(ctx); if (ctx->flags & E2F_FLAG_SIGNAL_MASK) fatal_error(ctx, 0); } #endif /* * Mark the system as valid, 'til proven otherwise */ ext2fs_mark_valid(fs); retval = ext2fs_read_bb_inode(fs, &fs->badblocks); if (retval) { com_err(ctx->program_name, retval, _("while reading bad blocks inode")); preenhalt(ctx); printf(_("This doesn't bode well," " but we'll try to go on...\n")); } run_result = e2fsck_run(ctx); e2fsck_clear_progbar(ctx); if (run_result == E2F_FLAG_RESTART) { printf(_("Restarting e2fsck from the beginning...\n")); retval = e2fsck_reset_context(ctx); if (retval) { com_err(ctx->program_name, retval, _("while resetting context")); fatal_error(ctx, 0); } ext2fs_close(fs); goto restart; } if (run_result & E2F_FLAG_CANCEL) { printf(_("%s: e2fsck canceled.\n"), ctx->device_name ? ctx->device_name : ctx->filesystem_name); exit_value |= FSCK_CANCELED; } if (run_result & E2F_FLAG_ABORT) fatal_error(ctx, _("aborted")); #ifdef MTRACE mtrace_print("Cleanup"); #endif if (ext2fs_test_changed(fs)) { exit_value |= FSCK_NONDESTRUCT; if (!(ctx->options & E2F_OPT_PREEN)) printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"), ctx->device_name); if (ctx->mount_flags & EXT2_MF_ISROOT) { printf(_("%s: ***** REBOOT LINUX *****\n"), ctx->device_name); exit_value |= FSCK_REBOOT; } } if (!ext2fs_test_valid(fs) || ((exit_value & FSCK_CANCELED) && (sb->s_state & EXT2_ERROR_FS))) { printf(_("\n%s: ********** WARNING: Filesystem still has " "errors **********\n\n"), ctx->device_name); exit_value |= FSCK_UNCORRECTED; exit_value &= ~FSCK_NONDESTRUCT; } if (exit_value & FSCK_CANCELED) { int allow_cancellation; profile_get_boolean(ctx->profile, "options", "allow_cancellation", 0, 0, &allow_cancellation); exit_value &= ~FSCK_NONDESTRUCT; if (allow_cancellation && ext2fs_test_valid(fs) && (sb->s_state & EXT2_VALID_FS) && !(sb->s_state & EXT2_ERROR_FS)) exit_value = 0; } else { show_stats(ctx); if (!(ctx->options & E2F_OPT_READONLY)) { if (ext2fs_test_valid(fs)) { if (!(sb->s_state & EXT2_VALID_FS)) exit_value |= FSCK_NONDESTRUCT; sb->s_state = EXT2_VALID_FS; } else sb->s_state &= ~EXT2_VALID_FS; sb->s_mnt_count = 0; sb->s_lastcheck = ctx->now; ext2fs_mark_super_dirty(fs); } } e2fsck_write_bitmaps(ctx); ext2fs_close(fs); ctx->fs = NULL; free(ctx->filesystem_name); free(ctx->journal_name); #ifdef RESOURCE_TRACK if (ctx->options & E2F_OPT_TIME) print_resource_track(NULL, &ctx->global_rtrack); #endif e2fsck_free_context(ctx); return exit_value; }
int main (int argc, char *argv[]) { errcode_t retval = 0, orig_retval = 0; int exit_value = FSCK_OK; ext2_filsys fs = 0; io_manager io_ptr; struct ext2_super_block *sb; const char *lib_ver_date; int my_ver, lib_ver; e2fsck_t ctx; struct problem_context pctx; int flags, run_result; int journal_size; int sysval, sys_page_size = 4096; __u32 features[3]; clear_problem_context(&pctx); #ifdef MTRACE mtrace(); #endif #ifdef MCHECK mcheck(0); #endif #ifdef ENABLE_NLS setlocale(LC_MESSAGES, ""); setlocale(LC_CTYPE, ""); bindtextdomain(NLS_CAT_NAME, LOCALEDIR); textdomain(NLS_CAT_NAME); #endif my_ver = ext2fs_parse_version_string(my_ver_string); lib_ver = ext2fs_get_library_version(0, &lib_ver_date); if (my_ver > lib_ver) { fprintf( stderr, _("Error: ext2fs library version " "out of date!\n")); show_version_only++; } retval = PRS(argc, argv, &ctx); if (retval) { com_err("e2fsck", retval, _("while trying to initialize program")); exit(FSCK_ERROR); } reserve_stdio_fds(); #ifdef RESOURCE_TRACK init_resource_track(&ctx->global_rtrack); #endif if (!(ctx->options & E2F_OPT_PREEN) || show_version_only) fprintf(stderr, "e2fsck %s (%s)\n", my_ver_string, my_ver_date); if (show_version_only) { fprintf(stderr, _("\tUsing %s, %s\n"), error_message(EXT2_ET_BASE), lib_ver_date); exit(FSCK_OK); } check_mount(ctx); if (!(ctx->options & E2F_OPT_PREEN) && !(ctx->options & E2F_OPT_NO) && !(ctx->options & E2F_OPT_YES)) { if (!ctx->interactive) fatal_error(ctx, _("need terminal for interactive repairs")); } ctx->superblock = ctx->use_superblock; restart: #ifdef CONFIG_TESTIO_DEBUG io_ptr = test_io_manager; test_io_backing_manager = unix_io_manager; #else io_ptr = unix_io_manager; #endif flags = EXT2_FLAG_NOFREE_ON_ERROR; if ((ctx->options & E2F_OPT_READONLY) == 0) flags |= EXT2_FLAG_RW; if ((ctx->mount_flags & EXT2_MF_MOUNTED) == 0) flags |= EXT2_FLAG_EXCLUSIVE; if (ctx->superblock && ctx->blocksize) { retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options, flags, ctx->superblock, ctx->blocksize, io_ptr, &fs); } else if (ctx->superblock) { int blocksize; for (blocksize = EXT2_MIN_BLOCK_SIZE; blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) { retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options, flags, ctx->superblock, blocksize, io_ptr, &fs); if (!retval) break; } } else retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options, flags, 0, 0, io_ptr, &fs); if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) && !(ctx->flags & E2F_FLAG_SB_SPECIFIED) && ((retval == EXT2_ET_BAD_MAGIC) || (retval == EXT2_ET_CORRUPT_SUPERBLOCK) || ((retval == 0) && ext2fs_check_desc(fs)))) { if (fs->flags & EXT2_FLAG_NOFREE_ON_ERROR) { ext2fs_free(fs); fs = NULL; } if (!fs || (fs->group_desc_count > 1)) { printf(_("%s: %s trying backup blocks...\n"), ctx->program_name, retval ? _("Superblock invalid,") : _("Group descriptors look bad...")); get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr); if (fs) ext2fs_close(fs); orig_retval = retval; goto restart; } } if (((retval == EXT2_ET_UNSUPP_FEATURE) || (retval == EXT2_ET_RO_UNSUPP_FEATURE)) && fs && fs->super) { sb = fs->super; features[0] = (sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP); features[1] = (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP); features[2] = (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP); if (features[0] || features[1] || features[2]) goto print_unsupp_features; } if (retval) { if (orig_retval) retval = orig_retval; com_err(ctx->program_name, retval, _("while trying to open %s"), ctx->filesystem_name); if (retval == EXT2_ET_REV_TOO_HIGH) { printf(_("The filesystem revision is apparently " "too high for this version of e2fsck.\n" "(Or the filesystem superblock " "is corrupt)\n\n")); fix_problem(ctx, PR_0_SB_CORRUPT, &pctx); } else if (retval == EXT2_ET_SHORT_READ) printf(_("Could this be a zero-length partition?\n")); else if ((retval == EPERM) || (retval == EACCES)) printf(_("You must have %s access to the " "filesystem or be root\n"), (ctx->options & E2F_OPT_READONLY) ? "r/o" : "r/w"); else if (retval == ENXIO) printf(_("Possibly non-existent or swap device?\n")); else if (retval == EBUSY) printf(_("Filesystem mounted or opened exclusively " "by another program?\n")); #ifdef EROFS else if (retval == EROFS) printf(_("Disk write-protected; use the -n option " "to do a read-only\n" "check of the device.\n")); #endif else fix_problem(ctx, PR_0_SB_CORRUPT, &pctx); fatal_error(ctx, 0); } /* * We only update the master superblock because (a) paranoia; * we don't want to corrupt the backup superblocks, and (b) we * don't need to update the mount count and last checked * fields in the backup superblock (the kernel doesn't update * the backup superblocks anyway). With newer versions of the * library this flag is set by ext2fs_open2(), but we set this * here just to be sure. (No, we don't support e2fsck running * with some other libext2fs than the one that it was shipped * with, but just in case....) */ fs->flags |= EXT2_FLAG_MASTER_SB_ONLY; if (!(ctx->flags & E2F_FLAG_GOT_DEVSIZE)) { __u32 blocksize = EXT2_BLOCK_SIZE(fs->super); int need_restart = 0; pctx.errcode = ext2fs_get_device_size(ctx->filesystem_name, blocksize, &ctx->num_blocks); /* * The floppy driver refuses to allow anyone else to * open the device if has been opened with O_EXCL; * this is unlike other block device drivers in Linux. * To handle this, we close the filesystem and then * reopen the filesystem after we get the device size. */ if (pctx.errcode == EBUSY) { ext2fs_close(fs); need_restart++; pctx.errcode = ext2fs_get_device_size(ctx->filesystem_name, blocksize, &ctx->num_blocks); } if (pctx.errcode == EXT2_ET_UNIMPLEMENTED) ctx->num_blocks = 0; else if (pctx.errcode) { fix_problem(ctx, PR_0_GETSIZE_ERROR, &pctx); ctx->flags |= E2F_FLAG_ABORT; fatal_error(ctx, 0); } ctx->flags |= E2F_FLAG_GOT_DEVSIZE; if (need_restart) goto restart; } ctx->fs = fs; fs->priv_data = ctx; fs->now = ctx->now; sb = fs->super; if (sb->s_rev_level > E2FSCK_CURRENT_REV) { com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH, _("while trying to open %s"), ctx->filesystem_name); get_newer: fatal_error(ctx, _("Get a newer version of e2fsck!")); } /* * Set the device name, which is used whenever we print error * or informational messages to the user. */ if (ctx->device_name == 0 && (sb->s_volume_name[0] != 0)) { ctx->device_name = string_copy(ctx, sb->s_volume_name, sizeof(sb->s_volume_name)); } if (ctx->device_name == 0) ctx->device_name = ctx->filesystem_name; /* * Make sure the ext3 superblock fields are consistent. */ retval = e2fsck_check_ext3_journal(ctx); if (retval) { com_err(ctx->program_name, retval, _("while checking ext3 journal for %s"), ctx->device_name); fatal_error(ctx, 0); } /* * Check to see if we need to do ext3-style recovery. If so, * do it, and then restart the fsck. */ if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) { if (ctx->options & E2F_OPT_READONLY) { printf(_("Warning: skipping journal recovery " "because doing a read-only filesystem " "check.\n")); io_channel_flush(ctx->fs->io); } else { if (ctx->flags & E2F_FLAG_RESTARTED) { /* * Whoops, we attempted to run the * journal twice. This should never * happen, unless the hardware or * device driver is being bogus. */ com_err(ctx->program_name, 0, _("unable to set superblock flags on %s\n"), ctx->device_name); fatal_error(ctx, 0); } retval = e2fsck_run_ext3_journal(ctx); if (retval) { com_err(ctx->program_name, retval, _("while recovering ext3 journal of %s"), ctx->device_name); fatal_error(ctx, 0); } ext2fs_close(ctx->fs); ctx->fs = 0; ctx->flags |= E2F_FLAG_RESTARTED; goto restart; } } /* * Check for compatibility with the feature sets. We need to * be more stringent than ext2fs_open(). */ features[0] = sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP; features[1] = sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP; features[2] = (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP); print_unsupp_features: if (features[0] || features[1] || features[2]) { int i, j; __u32 *mask = features, m; fprintf(stderr, _("%s has unsupported feature(s):"), ctx->filesystem_name); for (i=0; i <3; i++,mask++) { for (j=0,m=1; j < 32; j++, m<<=1) { if (*mask & m) fprintf(stderr, " %s", e2p_feature2string(i, m)); } } putc('\n', stderr); goto get_newer; } #ifdef ENABLE_COMPRESSION if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION) com_err(ctx->program_name, 0, _("Warning: compression support is experimental.\n")); #endif #ifndef ENABLE_HTREE if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) { com_err(ctx->program_name, 0, _("E2fsck not compiled with HTREE support,\n\t" "but filesystem %s has HTREE directories.\n"), ctx->device_name); goto get_newer; } #endif /* * If the user specified a specific superblock, presumably the * master superblock has been trashed. So we mark the * superblock as dirty, so it can be written out. */ if (ctx->superblock && !(ctx->options & E2F_OPT_READONLY)) ext2fs_mark_super_dirty(fs); /* * Calculate the number of filesystem blocks per pagesize. If * fs->blocksize > page_size, set the number of blocks per * pagesize to 1 to avoid division by zero errors. */ #ifdef _SC_PAGESIZE sysval = sysconf(_SC_PAGESIZE); if (sysval > 0) sys_page_size = sysval; #endif /* _SC_PAGESIZE */ ctx->blocks_per_page = sys_page_size / fs->blocksize; if (ctx->blocks_per_page == 0) ctx->blocks_per_page = 1; ehandler_init(fs->io); if (ctx->superblock) set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0); ext2fs_mark_valid(fs); check_super_block(ctx); if (ctx->flags & E2F_FLAG_SIGNAL_MASK) fatal_error(ctx, 0); check_if_skip(ctx); if (bad_blocks_file) read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks); else if (cflag) read_bad_blocks_file(ctx, 0, !keep_bad_blocks); /* Test disk */ if (ctx->flags & E2F_FLAG_SIGNAL_MASK) fatal_error(ctx, 0); #ifdef ENABLE_SWAPFS if (normalize_swapfs) { if ((fs->flags & EXT2_FLAG_SWAP_BYTES) == ext2fs_native_flag()) { fprintf(stderr, _("%s: Filesystem byte order " "already normalized.\n"), ctx->device_name); fatal_error(ctx, 0); } } if (swapfs) { swap_filesys(ctx); if (ctx->flags & E2F_FLAG_SIGNAL_MASK) fatal_error(ctx, 0); } #endif /* * Mark the system as valid, 'til proven otherwise */ ext2fs_mark_valid(fs); retval = ext2fs_read_bb_inode(fs, &fs->badblocks); if (retval) { com_err(ctx->program_name, retval, _("while reading bad blocks inode")); preenhalt(ctx); printf(_("This doesn't bode well," " but we'll try to go on...\n")); } /* * Save the journal size in megabytes. * Try and use the journal size from the backup else let e2fsck * find the default journal size. */ if (sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS) journal_size = sb->s_jnl_blocks[16] >> 20; else