e2fsprogs/e2fsck/e2fsck.c

240 lines
5.5 KiB
C
Raw Permalink Normal View History

1997-04-26 17:21:57 +04:00
/*
* e2fsck.c - a consistency checker for the new extended file system.
*
1997-04-29 20:15:03 +04:00
* Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Public
* License.
* %End-Header%
1997-04-26 17:21:57 +04:00
*/
#include "config.h"
1997-04-26 17:58:21 +04:00
#include <errno.h>
1997-04-26 17:21:57 +04:00
#include "e2fsck.h"
1997-04-29 20:15:03 +04:00
#include "problem.h"
1997-04-26 17:21:57 +04:00
/*
* This function allocates an e2fsck context
1997-04-26 17:21:57 +04:00
*/
errcode_t e2fsck_allocate_context(e2fsck_t *ret)
1997-04-26 17:21:57 +04:00
{
e2fsck_t context;
errcode_t retval;
char *time_env;
1997-04-26 17:21:57 +04:00
retval = ext2fs_get_mem(sizeof(struct e2fsck_struct), &context);
if (retval)
return retval;
memset(context, 0, sizeof(struct e2fsck_struct));
1997-04-26 17:21:57 +04:00
context->process_inode_size = 256;
context->ext_attr_ver = 2;
context->blocks_per_page = 1;
context->htree_slack_percentage = 255;
time_env = getenv("E2FSCK_TIME");
if (time_env)
context->now = strtoul(time_env, NULL, 0);
else {
context->now = time(0);
if (context->now < 1262322000) /* January 1 2010 */
context->flags |= E2F_FLAG_TIME_INSANE;
}
1997-04-26 17:21:57 +04:00
*ret = context;
return 0;
1997-04-26 17:21:57 +04:00
}
1997-04-29 21:48:10 +04:00
/*
* This function resets an e2fsck context; it is called when e2fsck
* needs to be restarted.
*/
errcode_t e2fsck_reset_context(e2fsck_t ctx)
1997-04-26 17:21:57 +04:00
{
int i;
ctx->flags &= E2F_RESET_FLAGS;
ctx->lost_and_found = 0;
ctx->bad_lost_and_found = 0;
if (ctx->inode_used_map) {
ext2fs_free_inode_bitmap(ctx->inode_used_map);
ctx->inode_used_map = 0;
1997-04-29 19:29:49 +04:00
}
if (ctx->inode_dir_map) {
ext2fs_free_inode_bitmap(ctx->inode_dir_map);
ctx->inode_dir_map = 0;
1997-04-26 17:21:57 +04:00
}
Many files: pass4.c (e2fsck_pass4): If an inode is set in the inode_imagic_map bitmap, don't check to see if it is disconnected from the inode tree (because it almost certainly will be). Free inode_imagic_map at the end of pass 4. pass2.c (check_dir_block, check_filetype): If the FILETYPE feature is set, check the directory entry's filetype information field, and fix/set it if necessary. (e2fsck_pass2): Free the inode_reg_map bitmap at the end of pass 2. pass1.c (e2fsck_pass1, alloc_imagic_map): Allocate and fill in information for inode_reg_map and inode_imagic_map, which indicates which inodes are regular files and AFS inodes, respectively. Since only the master superblock is written during a restart, force that superblock to be used after a restart; otherwise changes to the block group descriptors end up getting ignored. problem.c, problemP.h: If e2fsck is run -n, make def_yn variable be 0 for "no". Add support for a new flag, PR_NO_NOMSG, which supresses the problem message if e2fsck is run with the -n option. problem.c, problem.h (PR_2_SET_FILETYPE, PR_2_BAD_FILETYPE): Add new problem codes. message.c (expand_dirent_expression): Add support for %dt which prints the dirent type information. e2fsck.c (e2fsck_reset_context): Free new bitmaps (inode_reg_map and inode_imagic_map). e2fsck.h (e2fsck_t): Add new inode_reg_map and inode_magic_map to the context structure. ChangeLog, nt_io.c: nt_io.c: New file which supports I/O under Windows NT. ChangeLog, gen_uuid_nt.c: gen_uuid_nt.c: New file which creates a UUID under Windows NT. Many files: Add support for non-Unix compiles
1999-10-21 23:33:18 +04:00
if (ctx->inode_reg_map) {
ext2fs_free_inode_bitmap(ctx->inode_reg_map);
ctx->inode_reg_map = 0;
Many files: pass4.c (e2fsck_pass4): If an inode is set in the inode_imagic_map bitmap, don't check to see if it is disconnected from the inode tree (because it almost certainly will be). Free inode_imagic_map at the end of pass 4. pass2.c (check_dir_block, check_filetype): If the FILETYPE feature is set, check the directory entry's filetype information field, and fix/set it if necessary. (e2fsck_pass2): Free the inode_reg_map bitmap at the end of pass 2. pass1.c (e2fsck_pass1, alloc_imagic_map): Allocate and fill in information for inode_reg_map and inode_imagic_map, which indicates which inodes are regular files and AFS inodes, respectively. Since only the master superblock is written during a restart, force that superblock to be used after a restart; otherwise changes to the block group descriptors end up getting ignored. problem.c, problemP.h: If e2fsck is run -n, make def_yn variable be 0 for "no". Add support for a new flag, PR_NO_NOMSG, which supresses the problem message if e2fsck is run with the -n option. problem.c, problem.h (PR_2_SET_FILETYPE, PR_2_BAD_FILETYPE): Add new problem codes. message.c (expand_dirent_expression): Add support for %dt which prints the dirent type information. e2fsck.c (e2fsck_reset_context): Free new bitmaps (inode_reg_map and inode_imagic_map). e2fsck.h (e2fsck_t): Add new inode_reg_map and inode_magic_map to the context structure. ChangeLog, nt_io.c: nt_io.c: New file which supports I/O under Windows NT. ChangeLog, gen_uuid_nt.c: gen_uuid_nt.c: New file which creates a UUID under Windows NT. Many files: Add support for non-Unix compiles
1999-10-21 23:33:18 +04:00
}
if (ctx->block_found_map) {
ext2fs_free_block_bitmap(ctx->block_found_map);
ctx->block_found_map = 0;
1997-04-29 20:15:03 +04:00
}
if (ctx->inode_link_info) {
ext2fs_free_icount(ctx->inode_link_info);
ctx->inode_link_info = 0;
1997-04-26 17:21:57 +04:00
}
if (ctx->journal_io) {
if (ctx->fs && ctx->fs->io != ctx->journal_io)
io_channel_close(ctx->journal_io);
ctx->journal_io = 0;
}
if (ctx->fs && ctx->fs->dblist) {
ext2fs_free_dblist(ctx->fs->dblist);
ctx->fs->dblist = 0;
1997-04-29 20:15:03 +04:00
}
e2fsck_free_dir_info(ctx);
e2fsck_free_dx_dir_info(ctx);
if (ctx->refcount) {
ea_refcount_free(ctx->refcount);
ctx->refcount = 0;
}
if (ctx->refcount_extra) {
ea_refcount_free(ctx->refcount_extra);
ctx->refcount_extra = 0;
}
if (ctx->block_dup_map) {
ext2fs_free_block_bitmap(ctx->block_dup_map);
ctx->block_dup_map = 0;
1997-04-26 17:34:30 +04:00
}
if (ctx->block_ea_map) {
ext2fs_free_block_bitmap(ctx->block_ea_map);
ctx->block_ea_map = 0;
}
if (ctx->block_metadata_map) {
ext2fs_free_block_bitmap(ctx->block_metadata_map);
ctx->block_metadata_map = 0;
}
if (ctx->inode_bb_map) {
ext2fs_free_inode_bitmap(ctx->inode_bb_map);
ctx->inode_bb_map = 0;
1997-04-29 21:48:10 +04:00
}
if (ctx->inode_bad_map) {
ext2fs_free_inode_bitmap(ctx->inode_bad_map);
ctx->inode_bad_map = 0;
1997-04-29 18:53:37 +04:00
}
Many files: pass4.c (e2fsck_pass4): If an inode is set in the inode_imagic_map bitmap, don't check to see if it is disconnected from the inode tree (because it almost certainly will be). Free inode_imagic_map at the end of pass 4. pass2.c (check_dir_block, check_filetype): If the FILETYPE feature is set, check the directory entry's filetype information field, and fix/set it if necessary. (e2fsck_pass2): Free the inode_reg_map bitmap at the end of pass 2. pass1.c (e2fsck_pass1, alloc_imagic_map): Allocate and fill in information for inode_reg_map and inode_imagic_map, which indicates which inodes are regular files and AFS inodes, respectively. Since only the master superblock is written during a restart, force that superblock to be used after a restart; otherwise changes to the block group descriptors end up getting ignored. problem.c, problemP.h: If e2fsck is run -n, make def_yn variable be 0 for "no". Add support for a new flag, PR_NO_NOMSG, which supresses the problem message if e2fsck is run with the -n option. problem.c, problem.h (PR_2_SET_FILETYPE, PR_2_BAD_FILETYPE): Add new problem codes. message.c (expand_dirent_expression): Add support for %dt which prints the dirent type information. e2fsck.c (e2fsck_reset_context): Free new bitmaps (inode_reg_map and inode_imagic_map). e2fsck.h (e2fsck_t): Add new inode_reg_map and inode_magic_map to the context structure. ChangeLog, nt_io.c: nt_io.c: New file which supports I/O under Windows NT. ChangeLog, gen_uuid_nt.c: gen_uuid_nt.c: New file which creates a UUID under Windows NT. Many files: Add support for non-Unix compiles
1999-10-21 23:33:18 +04:00
if (ctx->inode_imagic_map) {
ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
ctx->inode_imagic_map = 0;
}
if (ctx->dirs_to_hash) {
ext2fs_u32_list_free(ctx->dirs_to_hash);
ctx->dirs_to_hash = 0;
}
1997-04-29 18:53:37 +04:00
1997-04-26 17:21:57 +04:00
/*
* Clear the array of invalid meta-data flags
1997-04-26 17:21:57 +04:00
*/
if (ctx->invalid_inode_bitmap_flag) {
ext2fs_free_mem(&ctx->invalid_inode_bitmap_flag);
ctx->invalid_inode_bitmap_flag = 0;
}
if (ctx->invalid_block_bitmap_flag) {
ext2fs_free_mem(&ctx->invalid_block_bitmap_flag);
ctx->invalid_block_bitmap_flag = 0;
}
if (ctx->invalid_inode_table_flag) {
ext2fs_free_mem(&ctx->invalid_inode_table_flag);
ctx->invalid_inode_table_flag = 0;
}
if (ctx->encrypted_dirs) {
ext2fs_u32_list_free(ctx->encrypted_dirs);
ctx->encrypted_dirs = 0;
}
/* Clear statistic counters */
ctx->fs_directory_count = 0;
ctx->fs_regular_count = 0;
ctx->fs_blockdev_count = 0;
ctx->fs_chardev_count = 0;
ctx->fs_links_count = 0;
ctx->fs_symlinks_count = 0;
ctx->fs_fast_symlinks_count = 0;
ctx->fs_fifo_count = 0;
ctx->fs_total_count = 0;
ctx->fs_badblocks_count = 0;
ctx->fs_sockets_count = 0;
ctx->fs_ind_count = 0;
ctx->fs_dind_count = 0;
ctx->fs_tind_count = 0;
ctx->fs_fragmented = 0;
ctx->fs_fragmented_dir = 0;
Many files: unix.c: Fix bug in check of feature set, to make sure we can really fix this filesystem. problem.h: Make blkcount type to be of type blkcnt_t. Make the num field be a 64 bit type. Add the problem code PR_1_FEATURE_LARGE_FILES problem.c: Add table entry for the problem code PR_1_FEATURE_LARGE_FILES. pass1.c (e2fsck_pass1): A non-zero i_dir_acl field is only a problem for directory inodes. (Since it is also i_size_high now.) If there are no large_files, then clear the LARGE_FLAG feature flag. If there are large_files, but the LARGE_FLAG feature flag is not set, complain and offer to fix it. (check_blocks): Add support to deal with non-directory inodes that have i_size_high set (i.e., large_files). Don't give an error if a directory has preallocated blocks, to support the DIR_PREALLOC feature. (process_block, process_bad_block): The blockcnt variable is a type of blkcnt_t, for conversion to the new block_iterate2. pass2.c (process_bad_inode): A non-zero i_dir_acl field is only a problem for directory inodes. (Since it is also i_size_high now.) message.c (expand_inode_expression): Print a 64-bits of the inode size for non-directory inodes. (Directory inodes can only use a 32-bit directory acl size, since i_size_high is shared with i_dir_acl.) Add sanity check so that trying to print out the directory acl on a non-directory inode will print zero. (expand_percent_expression): %B and %N, which print pctx->blkcount and pctx->num, can now be 64 bit variables. Print them using the "%lld" format if EXT2_NO_64_TYPE is not defined. e2fsck.h: Add the large_flagsfield to the e2fsck context. e2fsck.c (e2fsck_reset_context): Clear the large_flags field. ChangeLog, expect.1: f_messy_inode: Modify test to deal with changes to support 64-bit size files. (/MAKEDEV had i_dir_acl, now i_size_high, set.)
1998-03-24 19:22:38 +03:00
ctx->large_files = 0;
for (i=0; i < MAX_EXTENT_DEPTH_COUNT; i++)
ctx->extent_depth_count[i] = 0;
/* Reset the superblock to the user's requested value */
ctx->superblock = ctx->use_superblock;
return 0;
}
1997-04-26 17:21:57 +04:00
void e2fsck_free_context(e2fsck_t ctx)
{
if (!ctx)
return;
e2fsck_reset_context(ctx);
if (ctx->blkid)
blkid_put_cache(ctx->blkid);
if (ctx->profile)
profile_release(ctx->profile);
if (ctx->filesystem_name)
ext2fs_free_mem(&ctx->filesystem_name);
if (ctx->device_name)
ext2fs_free_mem(&ctx->device_name);
if (ctx->log_fn)
free(ctx->log_fn);
ext2fs_free_mem(&ctx);
1997-04-26 17:21:57 +04:00
}
/*
* This function runs through the e2fsck passes and calls them all,
* returning restart, abort, or cancel as necessary...
*/
typedef void (*pass_t)(e2fsck_t ctx);
static pass_t e2fsck_passes[] = {
e2fsck_pass1, e2fsck_pass1e, e2fsck_pass2, e2fsck_pass3,
e2fsck_pass4, e2fsck_pass5, 0 };
int e2fsck_run(e2fsck_t ctx)
{
int i;
pass_t e2fsck_pass;
#ifdef HAVE_SETJMP_H
if (setjmp(ctx->abort_loc)) {
ctx->flags &= ~E2F_FLAG_SETJMP_OK;
return (ctx->flags & E2F_FLAG_RUN_RETURN);
}
ctx->flags |= E2F_FLAG_SETJMP_OK;
#endif
for (i=0; (e2fsck_pass = e2fsck_passes[i]); i++) {
if (ctx->flags & E2F_FLAG_RUN_RETURN)
break;
if (e2fsck_mmp_update(ctx->fs))
fatal_error(ctx, 0);
e2fsck_pass(ctx);
if (ctx->progress)
(void) (ctx->progress)(ctx, 0, 0, 0);
}
ctx->flags &= ~E2F_FLAG_SETJMP_OK;
if (ctx->flags & E2F_FLAG_RUN_RETURN)
return (ctx->flags & E2F_FLAG_RUN_RETURN);
return 0;
}