mostly style fixes
[oweals/busybox.git] / e2fsprogs / e2fsck.c
index e72739cba881a029f08f3fb495c9d4be1829fdd9..88eedeaa6e5530b2b6a602b932ca8910dd9f9c19 100644 (file)
@@ -1,10 +1,9 @@
+/* vi: set sw=4 ts=4: */
 /*
  * e2fsck
  *
  * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
- * This file may be
- * redistributed under the terms of the GNU Public License.
- *
+ * Copyright (C) 2006 Garrett Kajmowicz
  *
  * Dictionary Abstract Data Type
  * Copyright (C) 1997 Kaz Kylheku <kaz@ashi.footprints.net>
  *
  * Copyright 1999-2000 Red Hat Software --- All Rights Reserved
  *
- * This file is part of the Linux kernel and is made available under
- * the terms of the GNU General Public License, version 2, or at your
- * option, any later version, incorporated herein by reference.
- *
  * Journal recovery routines for the generic filesystem journaling code;
  * part of the ext2fs journaling system.
+ *
+ * Licensed under GPLv2 or later, see file License in this tarball for details.
  */
 
 #ifndef _GNU_SOURCE
 
 #include "e2fsck.h"    /*Put all of our defines here to clean things up*/
 
-#ifdef __GNUC__
-#define _INLINE_ __inline__
-#define EXT2FS_ATTR(x) __attribute__(x)
-#else
-#define _INLINE_
-#define EXT2FS_ATTR(x)
-#endif
+#define _(x) x
+#define N_(x) x
 
 /*
  * Procedure declarations
@@ -108,7 +100,6 @@ struct problem_context {
 };
 
 
-
 /*
  * Function declarations
  */
@@ -219,7 +210,7 @@ typedef struct {
  * functions.
  */
 
-static _INLINE_ kmem_cache_t * do_cache_create(int len)
+static kmem_cache_t * do_cache_create(int len)
 {
        kmem_cache_t *new_cache;
 
@@ -229,135 +220,11 @@ static _INLINE_ kmem_cache_t * do_cache_create(int len)
        return new_cache;
 }
 
-static _INLINE_ void do_cache_destroy(kmem_cache_t *cache)
+static void do_cache_destroy(kmem_cache_t *cache)
 {
        free(cache);
 }
 
-/*
- * badblocks.c --- replace/append bad blocks to the bad block inode
- */
-
-static int check_bb_inode_blocks(ext2_filsys fs, blk_t *block_nr, int blockcnt,
-                                void *priv_data);
-
-
-static void invalid_block(ext2_filsys fs FSCK_ATTR((unused)), blk_t blk)
-{
-       printf(_("Bad block %u out of range; ignored.\n"), blk);
-       return;
-}
-
-static void read_bad_blocks_file(e2fsck_t ctx, const char *bad_blocks_file,
-                         int replace_bad_blocks)
-{
-       ext2_filsys fs = ctx->fs;
-       errcode_t       retval;
-       badblocks_list  bb_list = 0;
-       FILE            *f;
-       char            buf[1024];
-
-       e2fsck_read_bitmaps(ctx);
-
-       /*
-        * Make sure the bad block inode is sane.  If there are any
-        * illegal blocks, clear them.
-        */
-       retval = ext2fs_block_iterate(fs, EXT2_BAD_INO, 0, 0,
-                                     check_bb_inode_blocks, 0);
-       if (retval) {
-               com_err("ext2fs_block_iterate", retval,
-                       _("while sanity checking the bad blocks inode"));
-               goto fatal;
-       }
-
-       /*
-        * If we're appending to the bad blocks inode, read in the
-        * current bad blocks.
-        */
-       if (!replace_bad_blocks) {
-               retval = ext2fs_read_bb_inode(fs, &bb_list);
-               if (retval) {
-                       com_err("ext2fs_read_bb_inode", retval,
-                               _("while reading the bad blocks inode"));
-                       goto fatal;
-               }
-       }
-
-       /*
-        * Now read in the bad blocks from the file; if
-        * bad_blocks_file is null, then try to run the badblocks
-        * command.
-        */
-       if (bad_blocks_file) {
-               f = fopen(bad_blocks_file, "r");
-               if (!f) {
-                       com_err("read_bad_blocks_file", errno,
-                               _("while trying to open %s"), bad_blocks_file);
-                       goto fatal;
-               }
-       } else {
-               sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize,
-                       (ctx->options & E2F_OPT_PREEN) ? "" : "-s ",
-                       (ctx->options & E2F_OPT_WRITECHECK) ? "-n " : "",
-                       fs->device_name, fs->super->s_blocks_count);
-               f = popen(buf, "r");
-               if (!f) {
-                       com_err("read_bad_blocks_file", errno,
-                               _("while trying popen '%s'"), buf);
-                       goto fatal;
-               }
-       }
-       retval = ext2fs_read_bb_FILE(fs, f, &bb_list, invalid_block);
-       if (bad_blocks_file)
-               fclose(f);
-       else
-               pclose(f);
-       if (retval) {
-               com_err("ext2fs_read_bb_FILE", retval,
-                       _("while reading in list of bad blocks from file"));
-               goto fatal;
-       }
-
-       /*
-        * Finally, update the bad blocks from the bad_block_map
-        */
-       retval = ext2fs_update_bb_inode(fs, bb_list);
-       if (retval) {
-               com_err("ext2fs_update_bb_inode", retval,
-                       _("while updating bad block inode"));
-               goto fatal;
-       }
-
-       ext2fs_badblocks_list_free(bb_list);
-       return;
-
-fatal:
-       ctx->flags |= E2F_FLAG_ABORT;
-       return;
-
-}
-
-static int check_bb_inode_blocks(ext2_filsys fs,
-                                blk_t *block_nr,
-                                int blockcnt FSCK_ATTR((unused)),
-                                void *priv_data FSCK_ATTR((unused)))
-{
-       if (!*block_nr)
-               return 0;
-
-       /*
-        * If the block number is outrageous, clear it and ignore it.
-        */
-       if (*block_nr >= fs->super->s_blocks_count ||
-           *block_nr < fs->super->s_first_data_block) {
-               printf(_("Warning illegal block %u found in bad block inode.  Cleared.\n"), *block_nr);
-               *block_nr = 0;
-               return BLOCK_CHANGED;
-       }
-
-       return 0;
-}
 
 /*
  * Dictionary Abstract Data Type
@@ -390,7 +257,6 @@ static int check_bb_inode_blocks(ext2_filsys fs,
 
 #define dict_root(D) ((D)->nilnode.left)
 #define dict_nil(D) (&(D)->nilnode)
-#define DICT_DEPTH_MAX 64
 
 static void dnode_free(dnode_t *node);
 
@@ -860,7 +726,7 @@ static void e2fsck_free_dir_info(e2fsck_t ctx)
 /*
  * Return the count of number of directories in the dir_info structure
  */
-static inline int e2fsck_get_num_dirinfo(e2fsck_t ctx)
+static int e2fsck_get_num_dirinfo(e2fsck_t ctx)
 {
        return ctx->dir_info_count;
 }
@@ -1095,8 +961,6 @@ static errcode_t e2fsck_reset_context(e2fsck_t ctx)
        ctx->block_dup_map = 0;
        ext2fs_free_block_bitmap(ctx->block_ea_map);
        ctx->block_ea_map = 0;
-       ext2fs_free_inode_bitmap(ctx->inode_bb_map);
-       ctx->inode_bb_map = 0;
        ext2fs_free_inode_bitmap(ctx->inode_bad_map);
        ctx->inode_bad_map = 0;
        ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
@@ -1121,7 +985,6 @@ static errcode_t e2fsck_reset_context(e2fsck_t ctx)
        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;
@@ -1500,7 +1363,7 @@ e2fsck_handle_write_error(io_channel channel, unsigned long block, int count,
        return error;
 }
 
-static inline const char *ehandler_operation(const char *op)
+static const char *ehandler_operation(const char *op)
 {
        const char *ret = operation;
 
@@ -1528,8 +1391,6 @@ static void ehandler_init(io_channel channel)
  * any later version.
  */
 
-#define MNT_FL (MS_MGC_VAL | MS_RDONLY)
-
 /*
  * Define USE_INODE_IO to use the inode_io.c / fileio.c codepaths.
  * This creates a larger static binary, and a smaller binary using
@@ -1608,8 +1469,7 @@ static void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
                                                     bh->b_blocknr,
                                                     1, bh->b_data);
                        if (retval) {
-                               com_err(bh->b_ctx->device_name, retval,
-                                       "while reading block %lu\n",
+                               bb_error_msg("while reading block %lu",
                                        (unsigned long) bh->b_blocknr);
                                bh->b_err = retval;
                                continue;
@@ -1620,8 +1480,7 @@ static void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
                                                      bh->b_blocknr,
                                                      1, bh->b_data);
                        if (retval) {
-                               com_err(bh->b_ctx->device_name, retval,
-                                       "while writing block %lu\n",
+                               bb_error_msg("while writing block %lu",
                                        (unsigned long) bh->b_blocknr);
                                bh->b_err = retval;
                                continue;
@@ -1632,7 +1491,7 @@ static void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
        }
 }
 
-static inline void mark_buffer_dirty(struct buffer_head *bh)
+static void mark_buffer_dirty(struct buffer_head *bh)
 {
        bh->b_dirty = 1;
 }
@@ -1649,7 +1508,7 @@ static void brelse(struct buffer_head *bh)
        ext2fs_free_mem(&bh);
 }
 
-static inline int buffer_uptodate(struct buffer_head *bh)
+static int buffer_uptodate(struct buffer_head *bh)
 {
        return bh->b_uptodate;
 }
@@ -1831,7 +1690,7 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
                memcpy(&jsuper, start ? bh->b_data :  bh->b_data + 1024,
                       sizeof(jsuper));
                brelse(bh);
-#ifdef EXT2FS_ENABLE_SWAPFS
+#if BB_BIG_ENDIAN
                if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
                        ext2fs_swap_super(&jsuper);
 #endif
@@ -1937,8 +1796,7 @@ static errcode_t e2fsck_journal_load(journal_t *journal)
 
        ll_rw_block(READ, 1, &jbh);
        if (jbh->b_err) {
-               com_err(ctx->device_name, jbh->b_err,
-                       _("reading journal superblock\n"));
+               bb_error_msg(_("reading journal superblock"));
                return jbh->b_err;
        }
 
@@ -1994,8 +1852,7 @@ static errcode_t e2fsck_journal_load(journal_t *journal)
         * format to be able to proceed safely, so any other checks that
         * fail we should attempt to recover from. */
        if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
-               com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
-                       _("%s: no valid journal superblock found\n"),
+               bb_error_msg(_("%s: no valid journal superblock found"),
                        ctx->device_name);
                return EXT2_ET_CORRUPT_SUPERBLOCK;
        }
@@ -2003,8 +1860,7 @@ static errcode_t e2fsck_journal_load(journal_t *journal)
        if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
                journal->j_maxlen = ntohl(jsb->s_maxlen);
        else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) {
-               com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
-                       _("%s: journal too short\n"),
+               bb_error_msg(_("%s: journal too short"),
                        ctx->device_name);
                return EXT2_ET_CORRUPT_SUPERBLOCK;
        }
@@ -2297,10 +2153,9 @@ static int e2fsck_run_ext3_journal(e2fsck_t ctx)
                             &ctx->fs);
 
        if (retval) {
-               com_err(ctx->program_name, retval,
-                       _("while trying to re-open %s"),
+               bb_error_msg(_("while trying to re-open %s"),
                        ctx->device_name);
-               fatal_error(ctx, 0);
+               bb_error_msg_and_die(0);
        }
        ctx->fs->priv_data = ctx;
 
@@ -2673,16 +2528,8 @@ static void expand_inode_expression(char ch,
                if (LINUX_S_ISDIR(inode->i_mode))
                        printf("%u", inode->i_size);
                else {
-#ifdef EXT2_NO_64_TYPE
-                       if (inode->i_size_high)
-                               printf("0x%x%08x", inode->i_size_high,
-                                      inode->i_size);
-                       else
-                               printf("%u", inode->i_size);
-#else
-                       printf("%llu", (inode->i_size |
-                                       ((__u64) inode->i_size_high << 32)));
-#endif
+                       printf("%"PRIu64, (inode->i_size |
+                                       ((uint64_t) inode->i_size_high << 32)));
                }
                break;
        case 'S':
@@ -2737,7 +2584,7 @@ static void expand_inode_expression(char ch,
 /*
  * This function expands '%dX' expressions
  */
-static _INLINE_ void expand_dirent_expression(char ch,
+static void expand_dirent_expression(char ch,
                                              struct problem_context *ctx)
 {
        struct ext2_dir_entry   *dirent;
@@ -2776,7 +2623,7 @@ static _INLINE_ void expand_dirent_expression(char ch,
        }
 }
 
-static _INLINE_ void expand_percent_expression(ext2_filsys fs, char ch,
+static void expand_percent_expression(ext2_filsys fs, char ch,
                                               struct problem_context *ctx)
 {
        if (!ctx)
@@ -2790,11 +2637,7 @@ static _INLINE_ void expand_percent_expression(ext2_filsys fs, char ch,
                printf("%u", ctx->blk);
                break;
        case 'B':
-#ifdef EXT2_NO_64_TYPE
-               printf("%d", ctx->blkcount);
-#else
-               printf("%lld", ctx->blkcount);
-#endif
+               printf("%"PRIi64, ctx->blkcount);
                break;
        case 'c':
                printf("%u", ctx->blk2);
@@ -2815,11 +2658,7 @@ static _INLINE_ void expand_percent_expression(ext2_filsys fs, char ch,
                printf("%s", error_message(ctx->errcode));
                break;
        case 'N':
-#ifdef EXT2_NO_64_TYPE
-               printf("%u", ctx->num);
-#else
-               printf("%llu", ctx->num);
-#endif
+               printf("%"PRIi64, ctx->num);
                break;
        case 'p':
                print_pathname(fs, ctx->ino, 0);
@@ -2841,11 +2680,7 @@ static _INLINE_ void expand_percent_expression(ext2_filsys fs, char ch,
                printf("%s", ctx->str ? ctx->str : "NULL");
                break;
        case 'X':
-#ifdef EXT2_NO_64_TYPE
-               printf("0x%x", ctx->num);
-#else
-               printf("0x%llx", ctx->num);
-#endif
+               printf("0x%"PRIi64, ctx->num);
                break;
        default:
        no_context:
@@ -3005,7 +2840,6 @@ static int region_allocate(region_t region, region_addr_t start, int n)
  *      - A bitmap of which inodes are directories.     (inode_dir_map)
  *      - A bitmap of which inodes are regular files.   (inode_reg_map)
  *      - A bitmap of which inodes have bad fields.     (inode_bad_map)
- *      - A bitmap of which inodes are in bad blocks.   (inode_bb_map)
  *      - A bitmap of which inodes are imagic inodes.   (inode_imagic_map)
  *      - A bitmap of which blocks are in use.          (block_found_map)
  *      - A bitmap of which blocks are in use by two inodes     (block_dup_map)
@@ -3031,13 +2865,12 @@ static int process_bad_block(ext2_filsys fs, blk_t *block_nr,
 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                         char *block_buf);
 static void mark_table_blocks(e2fsck_t ctx);
-static void alloc_bb_map(e2fsck_t ctx);
 static void alloc_imagic_map(e2fsck_t ctx);
 static void mark_inode_bad(e2fsck_t ctx, ino_t ino);
 static void handle_fs_bad_blocks(e2fsck_t ctx);
 static void process_inodes(e2fsck_t ctx, char *block_buf);
-static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
-static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
+static int process_inode_cmp(const void *a, const void *b);
+static errcode_t scan_callback(ext2_filsys fs,
                                  dgrp_t group, void * priv_data);
 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
                                    char *block_buf, int adjust_sign);
@@ -3484,10 +3317,6 @@ static void e2fsck_pass1(e2fsck_t ctx)
                if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
                        return;
                if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
-                       if (!ctx->inode_bb_map)
-                               alloc_bb_map(ctx);
-                       ext2fs_mark_inode_bitmap(ctx->inode_bb_map, ino);
-                       ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
                        continue;
                }
                if (pctx.errcode) {
@@ -3832,7 +3661,7 @@ static void e2fsck_pass1(e2fsck_t ctx)
                e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
                                  "recreate inode");
                inode->i_mtime = time(0);
-               e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode, 
+               e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
                                  "recreate inode");
                fs->block_map = save_bmap;
                ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
@@ -3871,7 +3700,6 @@ endit:
  * glock group, call process_inodes.
  */
 static errcode_t scan_callback(ext2_filsys fs,
-                              ext2_inode_scan scan FSCK_ATTR((unused)),
                               dgrp_t group, void * priv_data)
 {
        struct scan_callback_struct *scan_struct;
@@ -3929,7 +3757,7 @@ static void process_inodes(e2fsck_t ctx, char *block_buf)
        ehandler_operation(old_operation);
 }
 
-static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
+static int process_inode_cmp(const void *a, const void *b)
 {
        const struct process_inode_block *ib_a =
                (const struct process_inode_block *) a;
@@ -3968,26 +3796,6 @@ static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
 }
 
 
-/*
- * This procedure will allocate the inode "bb" (badblock) map table
- */
-static void alloc_bb_map(e2fsck_t ctx)
-{
-       struct          problem_context pctx;
-
-       clear_problem_context(&pctx);
-       pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
-                                             _("inode in bad block map"),
-                                             &ctx->inode_bb_map);
-       if (pctx.errcode) {
-               pctx.num = 4;
-               fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
-               /* Should never get here */
-               ctx->flags |= E2F_FLAG_ABORT;
-               return;
-       }
-}
-
 /*
  * This procedure will allocate the inode imagic table
  */
@@ -4015,7 +3823,7 @@ static void alloc_imagic_map(e2fsck_t ctx)
  * WARNING: Assumes checks have already been done to make sure block
  * is valid.  This is true in both process_block and process_bad_block.
  */
-static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block)
+static void mark_block_used(e2fsck_t ctx, blk_t block)
 {
        struct          problem_context pctx;
 
@@ -4592,222 +4400,20 @@ mark_dir:
        return ret_code;
 }
 
-static int process_bad_block(ext2_filsys fs,
+static int process_bad_block(ext2_filsys fs FSCK_ATTR((unused)),
                      blk_t *block_nr,
                      e2_blkcnt_t blockcnt,
                      blk_t ref_block FSCK_ATTR((unused)),
                      int ref_offset FSCK_ATTR((unused)),
-                     void *priv_data)
+                     void *priv_data EXT2FS_ATTR((unused)))
 {
-       struct process_block_struct_1 *p;
-       blk_t           blk = *block_nr;
-       blk_t           first_block;
-       dgrp_t          i;
-       struct problem_context *pctx;
-       e2fsck_t        ctx;
-
        /*
         * Note: This function processes blocks for the bad blocks
         * inode, which is never compressed.  So we don't use HOLE_BLKADDR().
         */
 
-       if (!blk)
-               return 0;
-
-       p = (struct process_block_struct_1 *) priv_data;
-       ctx = p->ctx;
-       pctx = p->pctx;
-
-       pctx->ino = EXT2_BAD_INO;
-       pctx->blk = blk;
-       pctx->blkcount = blockcnt;
-
-       if ((blk < fs->super->s_first_data_block) ||
-           (blk >= fs->super->s_blocks_count)) {
-               if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
-                       *block_nr = 0;
-                       return BLOCK_CHANGED;
-               } else
-                       return 0;
-       }
-
-       if (blockcnt < 0) {
-               if (ext2fs_test_block_bitmap(p->fs_meta_blocks, blk)) {
-                       p->bbcheck = 1;
-                       if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
-                               *block_nr = 0;
-                               return BLOCK_CHANGED;
-                       }
-               } else if (ext2fs_test_block_bitmap(ctx->block_found_map,
-                                                   blk)) {
-                       p->bbcheck = 1;
-                       if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
-                                       pctx)) {
-                               *block_nr = 0;
-                               return BLOCK_CHANGED;
-                       }
-                       if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
-                               return BLOCK_ABORT;
-               } else
-                       mark_block_used(ctx, blk);
-               return 0;
-       }
-
-       ctx->fs_badblocks_count++;
-       /*
-        * If the block is not used, then mark it as used and return.
-        * If it is already marked as found, this must mean that
-        * there's an overlap between the filesystem table blocks
-        * (bitmaps and inode table) and the bad block list.
-        */
-       if (!ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
-               ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
-               return 0;
-       }
-       /*
-        * Try to find the where the filesystem block was used...
-        */
-       first_block = fs->super->s_first_data_block;
-
-       for (i = 0; i < fs->group_desc_count; i++ ) {
-               pctx->group = i;
-               pctx->blk = blk;
-               if (!ext2fs_bg_has_super(fs, i))
-                       goto skip_super;
-               if (blk == first_block) {
-                       if (i == 0) {
-                               if (fix_problem(ctx,
-                                               PR_1_BAD_PRIMARY_SUPERBLOCK,
-                                               pctx)) {
-                                       *block_nr = 0;
-                                       return BLOCK_CHANGED;
-                               }
-                               return 0;
-                       }
-                       fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
-                       return 0;
-               }
-               if ((blk > first_block) &&
-                   (blk <= first_block + fs->desc_blocks)) {
-                       if (i == 0) {
-                               pctx->blk = *block_nr;
-                               if (fix_problem(ctx,
-                       PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
-                                       *block_nr = 0;
-                                       return BLOCK_CHANGED;
-                               }
-                               return 0;
-                       }
-                       fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
-                       return 0;
-               }
-       skip_super:
-               if (blk == fs->group_desc[i].bg_block_bitmap) {
-                       if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
-                               ctx->invalid_block_bitmap_flag[i]++;
-                               ctx->invalid_bitmaps++;
-                       }
-                       return 0;
-               }
-               if (blk == fs->group_desc[i].bg_inode_bitmap) {
-                       if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
-                               ctx->invalid_inode_bitmap_flag[i]++;
-                               ctx->invalid_bitmaps++;
-                       }
-                       return 0;
-               }
-               if ((blk >= fs->group_desc[i].bg_inode_table) &&
-                   (blk < (fs->group_desc[i].bg_inode_table +
-                           fs->inode_blocks_per_group))) {
-                       /*
-                        * If there are bad blocks in the inode table,
-                        * the inode scan code will try to do
-                        * something reasonable automatically.
-                        */
-                       return 0;
-               }
-               first_block += fs->super->s_blocks_per_group;
-       }
-       /*
-        * If we've gotten to this point, then the only
-        * possibility is that the bad block inode meta data
-        * is using a bad block.
-        */
-       if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
-           (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
-           (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
-               p->bbcheck = 1;
-               if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
-                       *block_nr = 0;
-                       return BLOCK_CHANGED;
-               }
-               if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
-                       return BLOCK_ABORT;
-               return 0;
-       }
-
-       pctx->group = -1;
-
-       /* Warn user that the block wasn't claimed */
-       fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
-
-       return 0;
-}
-
-static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
-                           const char *name, int num, blk_t *new_block)
-{
-       ext2_filsys fs = ctx->fs;
-       blk_t           old_block = *new_block;
-       int             i;
-       char            *buf;
-       struct problem_context  pctx;
-
-       clear_problem_context(&pctx);
-
-       pctx.group = group;
-       pctx.blk = old_block;
-       pctx.str = name;
-
-       pctx.errcode = ext2fs_get_free_blocks(fs, first_block,
-                       first_block + fs->super->s_blocks_per_group,
-                                       num, ctx->block_found_map, new_block);
-       if (pctx.errcode) {
-               pctx.num = num;
-               fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
-               ext2fs_unmark_valid(fs);
-               return;
-       }
-       pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
-       if (pctx.errcode) {
-               fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
-               ext2fs_unmark_valid(fs);
-               return;
-       }
-       ext2fs_mark_super_dirty(fs);
-       fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
-       pctx.blk2 = *new_block;
-       fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
-                         PR_1_RELOC_TO), &pctx);
-       pctx.blk2 = 0;
-       for (i = 0; i < num; i++) {
-               pctx.blk = i;
-               ext2fs_mark_block_bitmap(ctx->block_found_map, (*new_block)+i);
-               if (old_block) {
-                       pctx.errcode = io_channel_read_blk(fs->io,
-                                  old_block + i, 1, buf);
-                       if (pctx.errcode)
-                               fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
-               } else
-                       memset(buf, 0, fs->blocksize);
-
-               pctx.blk = (*new_block) + i;
-               pctx.errcode = io_channel_write_blk(fs->io, pctx.blk,
-                                             1, buf);
-               if (pctx.errcode)
-                       fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
-       }
-       ext2fs_free_mem(&buf);
+       printf("Unrecoverable Error: Found %"PRIi64" bad blocks starting at block number: %u\n", blockcnt, *block_nr);
+       return BLOCK_ERROR;
 }
 
 /*
@@ -4819,28 +4425,8 @@ static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
  */
 static void handle_fs_bad_blocks(e2fsck_t ctx)
 {
-       ext2_filsys fs = ctx->fs;
-       dgrp_t          i;
-       int             first_block = fs->super->s_first_data_block;
-
-       for (i = 0; i < fs->group_desc_count; i++) {
-               if (ctx->invalid_block_bitmap_flag[i]) {
-                       new_table_block(ctx, first_block, i, _("block bitmap"),
-                                       1, &fs->group_desc[i].bg_block_bitmap);
-               }
-               if (ctx->invalid_inode_bitmap_flag[i]) {
-                       new_table_block(ctx, first_block, i, _("inode bitmap"),
-                                       1, &fs->group_desc[i].bg_inode_bitmap);
-               }
-               if (ctx->invalid_inode_table_flag[i]) {
-                       new_table_block(ctx, first_block, i, _("inode table"),
-                                       fs->inode_blocks_per_group,
-                                       &fs->group_desc[i].bg_inode_table);
-                       ctx->flags |= E2F_FLAG_RESTART;
-               }
-               first_block += fs->super->s_blocks_per_group;
-       }
-       ctx->invalid_bitmaps = 0;
+       printf("Bad blocks detected on your filesystem\n"
+               "You should get your data off as the device will soon die\n");
 }
 
 /*
@@ -5522,8 +5108,7 @@ static int delete_file_block(ext2_filsys fs,
                        p = (struct dup_block *) dnode_get(n);
                        decrement_badcount(ctx, *block_nr, p);
                } else
-                       com_err("delete_file_block", 0,
-                           _("internal error; can't find dup_blk for %d\n"),
+                       bb_error_msg(_("internal error; can't find dup_blk for %d"),
                                *block_nr);
        } else {
                ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
@@ -5656,8 +5241,7 @@ static int clone_file_block(ext2_filsys fs,
                        ext2fs_mark_block_bitmap(fs->block_map, new_block);
                        return BLOCK_CHANGED;
                } else
-                       com_err("clone_file_block", 0,
-                           _("internal error; can't find dup_blk for %d\n"),
+                       bb_error_msg(_("internal error; can't find dup_blk for %d"),
                                *block_nr);
        }
        return 0;
@@ -5699,8 +5283,7 @@ static int clone_file(e2fsck_t ctx, ext2_ino_t ino,
                goto errout;
        }
        if (cs.errcode) {
-               com_err("clone_file", cs.errcode,
-                       _("returned from clone_file_block"));
+               bb_error_msg(_("returned from clone_file_block"));
                retval = cs.errcode;
                goto errout;
        }
@@ -5827,7 +5410,7 @@ static int update_dir_block(ext2_filsys fs,
 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino);
 static int htree_depth(struct dx_dir_info *dx_dir,
                       struct dx_dirblock_info *dx_db);
-static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b);
+static int special_dir_block_cmp(const void *a, const void *b);
 
 struct check_dir_struct {
        char *buf;
@@ -6057,7 +5640,7 @@ static int dict_de_cmp(const void *a, const void *b)
  * This guarantees that the root node of the htree directories are
  * processed first, so we know what hash version to use.
  */
-static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b)
+static int special_dir_block_cmp(const void *a, const void *b)
 {
        const struct ext2_db_entry *db_a =
                (const struct ext2_db_entry *) a;
@@ -6239,7 +5822,7 @@ static int ext2_file_type(unsigned int mode)
        return 0;
 }
 
-static _INLINE_ int check_filetype(e2fsck_t ctx,
+static int check_filetype(e2fsck_t ctx,
                                   struct ext2_dir_entry *dirent,
                                   struct problem_context *pctx)
 {
@@ -6604,14 +6187,6 @@ static int check_dir_block(ext2_filsys fs,
                         * If the inode is unused, offer to clear it.
                         */
                        problem = PR_2_UNUSED_INODE;
-               } else if (ctx->inode_bb_map &&
-                          (ext2fs_test_inode_bitmap(ctx->inode_bb_map,
-                                                    dirent->inode))) {
-                       /*
-                        * If the inode is in a bad block, offer to
-                        * clear it.
-                        */
-                       problem = PR_2_BB_INODE;
                } else if ((dot_state > 1) &&
                           ((dirent->name_len & 0xFF) == 1) &&
                           (dirent->name[0] == '.')) {
@@ -6909,7 +6484,7 @@ static int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
            !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) &&
            fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
                inode.i_file_acl = 0;
-#ifdef EXT2FS_ENABLE_SWAPFS
+#if BB_BIG_ENDIAN
                /*
                 * This is a special kludge to deal with long symlinks
                 * on big endian systems.  i_blocks had already been
@@ -7146,7 +6721,7 @@ static int update_dir_block(ext2_filsys fs FSCK_ATTR((unused)),
  * Then, pass3 interates over all directory inodes; for each directory
  * it attempts to trace up the filesystem tree, using dirinfo.parent
  * until it reaches a directory which has been marked "done".  If it
- * can not do so, then the directory must be disconnected, and e2fsck
+ * cannot do so, then the directory must be disconnected, and e2fsck
  * will offer to reconnect it to /lost+found.  While it is chasing
  * parent pointers up the filesystem tree, if pass3 sees a directory
  * twice, then it has detected a filesystem loop, and it will again
@@ -7888,7 +7463,6 @@ errcode_t e2fsck_expand_directory(e2fsck_t ctx, ext2_ino_t dir,
  * pass4.c -- pass #4 of e2fsck: Check reference counts
  *
  * Pass 4 frees the following data structures:
- *      - A bitmap of which inodes are in bad blocks.   (inode_bb_map)
  *      - A bitmap of which inodes are imagic inodes.   (inode_imagic_map)
  */
 
@@ -7992,9 +7566,7 @@ static void e2fsck_pass4(e2fsck_t ctx)
                        continue;
                if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, i)) ||
                    (ctx->inode_imagic_map &&
-                    ext2fs_test_inode_bitmap(ctx->inode_imagic_map, i)) ||
-                   (ctx->inode_bb_map &&
-                    ext2fs_test_inode_bitmap(ctx->inode_bb_map, i)))
+                    ext2fs_test_inode_bitmap(ctx->inode_imagic_map, i)))
                        continue;
                ext2fs_icount_fetch(ctx->inode_link_info, i, &link_count);
                ext2fs_icount_fetch(ctx->inode_count, i, &link_counted);
@@ -8029,8 +7601,6 @@ static void e2fsck_pass4(e2fsck_t ctx)
        }
        ext2fs_free_icount(ctx->inode_link_info); ctx->inode_link_info = 0;
        ext2fs_free_icount(ctx->inode_count); ctx->inode_count = 0;
-       ext2fs_free_inode_bitmap(ctx->inode_bb_map);
-       ctx->inode_bb_map = 0;
        ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
        ctx->inode_imagic_map = 0;
        ext2fs_free_mem(&buf);
@@ -9017,7 +8587,7 @@ static const struct e2fsck_problem problem_table[] = {
 
        /* Bad primary block */
        { PR_1_BAD_PRIMARY_BLOCK,
-         N_("\nIf the @b is really bad, the @f can not be fixed.\n"),
+         N_("\nIf the @b is really bad, the @f cannot be fixed.\n"),
          PROMPT_NONE, PR_AFTER_CODE, PR_1_BAD_PRIMARY_BLOCK_PROMPT },
 
        /* Bad primary block prompt */
@@ -9382,7 +8952,7 @@ static const struct e2fsck_problem problem_table[] = {
 
        /* File has duplicate blocks */
        { PR_1D_DUP_FILE,
-         N_("File %Q (@i #%i, mod time %IM) \n"
+         N_("File %Q (@i #%i, mod time %IM)\n"
          "  has %B @m @b(s), shared with %N file(s):\n"),
          PROMPT_NONE, 0 },
 
@@ -9562,7 +9132,7 @@ static const struct e2fsck_problem problem_table[] = {
 
        /* Internal error: couldn't find dir_info */
        { PR_2_NO_DIRINFO,
-         N_("Internal error: couldn't find dir_info for %i.\n"),
+         N_("Internal error: cannot find dir_info for %i.\n"),
          PROMPT_NONE, PR_FATAL },
 
        /* Final rec_len is wrong */
@@ -9838,7 +9408,7 @@ static const struct e2fsck_problem problem_table[] = {
 
        /* Internal error: couldn't find dir_info */
        { PR_3_NO_DIRINFO,
-         N_("Internal error: couldn't find dir_info for %i.\n"),
+         N_("Internal error: cannot find dir_info for %i.\n"),
          PROMPT_NONE, PR_FATAL },
 
        /* Lost+found not a directory */
@@ -10166,7 +9736,7 @@ int fix_problem(e2fsck_t ctx, problem_t code, struct problem_context *pctx)
                preenhalt(ctx);
 
        if (ptr->flags & PR_FATAL)
-               fatal_error(ctx, 0);
+               bb_error_msg_and_die(0);
 
        if (ptr->prompt == PROMPT_NONE) {
                if (ptr->flags & PR_NOCOLLATE)
@@ -10198,7 +9768,7 @@ int fix_problem(e2fsck_t ctx, problem_t code, struct problem_context *pctx)
        }
 
        if ((ptr->prompt == PROMPT_ABORT) && answer)
-               fatal_error(ctx, 0);
+               bb_error_msg_and_die(0);
 
        if (ptr->flags & PR_AFTER_CODE)
                answer = fix_problem(ctx, ptr->second_code, pctx);
@@ -10248,7 +9818,7 @@ static int jread(struct buffer_head **bhp, journal_t *journal,
        err = journal_bmap(journal, offset, &blocknr);
 
        if (err) {
-               printf ("JBD: bad block at offset %u\n", offset);
+               printf("JBD: bad block at offset %u\n", offset);
                return err;
        }
 
@@ -10265,7 +9835,7 @@ static int jread(struct buffer_head **bhp, journal_t *journal,
        }
 
        if (!buffer_uptodate(bh)) {
-               printf ("JBD: Failed to read block at offset %u\n", offset);
+               printf("JBD: Failed to read block at offset %u\n", offset);
                brelse(bh);
                return -EIO;
        }
@@ -10479,7 +10049,7 @@ static int do_one_pass(journal_t *journal,
                                        /* Recover what we can, but
                                         * report failure at the end. */
                                        success = err;
-                                       printf ("JBD: IO error %d recovering "
+                                       printf("JBD: IO error %d recovering "
                                                "block %ld in log\n",
                                                err, io_block);
                                } else {
@@ -10504,7 +10074,7 @@ static int do_one_pass(journal_t *journal,
                                                       blocknr,
                                                     journal->j_blocksize);
                                        if (nbh == NULL) {
-                                               printf ("JBD: Out of memory "
+                                               printf("JBD: Out of memory "
                                                       "during recovery.\n");
                                                err = -ENOMEM;
                                                brelse(bh);
@@ -10583,7 +10153,7 @@ static int do_one_pass(journal_t *journal,
                /* It's really bad news if different passes end up at
                 * different places (but possible due to IO errors). */
                if (info->end_transaction != next_commit_ID) {
-                       printf ("JBD: recovery pass %d ended at "
+                       printf("JBD: recovery pass %d ended at "
                                "transaction %u, expected %u\n",
                                pass, next_commit_ID, info->end_transaction);
                        if (!success)
@@ -10773,7 +10343,7 @@ static int fill_dir_block(ext2_filsys fs,
 }
 
 /* Used for sorting the hash entry */
-static EXT2_QSORT_TYPE name_cmp(const void *a, const void *b)
+static int name_cmp(const void *a, const void *b)
 {
        const struct hash_entry *he_a = (const struct hash_entry *) a;
        const struct hash_entry *he_b = (const struct hash_entry *) b;
@@ -10797,7 +10367,7 @@ static EXT2_QSORT_TYPE name_cmp(const void *a, const void *b)
 }
 
 /* Used for sorting the hash entry */
-static EXT2_QSORT_TYPE hash_cmp(const void *a, const void *b)
+static int hash_cmp(const void *a, const void *b)
 {
        const struct hash_entry *he_a = (const struct hash_entry *) a;
        const struct hash_entry *he_b = (const struct hash_entry *) b;
@@ -11504,7 +11074,7 @@ struct jbd_revoke_table_s
 /* Utility functions to maintain the revoke table */
 
 /* Borrowed from buffer.c: this is a tried and tested block hash function */
-static inline int hash(journal_t *journal, unsigned long block)
+static int hash(journal_t *journal, unsigned long block)
 {
        struct jbd_revoke_table_s *table = journal->j_revoke;
        int hash_shift = table->hash_shift;
@@ -11891,8 +11461,7 @@ static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
        retval = ext2fs_block_iterate2(fs, ino, BLOCK_FLAG_DEPTH_TRAVERSE,
                                      block_buf, release_inode_block, &pb);
        if (retval) {
-               com_err("release_inode_blocks", retval,
-                       _("while calling ext2fs_block_iterate for inode %d"),
+               bb_error_msg(_("while calling ext2fs_block_iterate for inode %d"),
                        ino);
                return 1;
        }
@@ -11914,8 +11483,7 @@ static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
                        count = 1;
                }
                if (retval) {
-                       com_err("release_inode_blocks", retval,
-               _("while calling ext2fs_adjust_ea_refocunt for inode %d"),
+                       bb_error_msg(_("while calling ext2fs_adjust_ea_refocunt for inode %d"),
                                ino);
                        return 1;
                }
@@ -12486,14 +12054,12 @@ static void swap_inode_blocks(e2fsck_t ctx, ext2_ino_t ino, char *block_buf,
        retval = ext2fs_block_iterate(ctx->fs, ino, 0, block_buf,
                                      swap_block, &sb);
        if (retval) {
-               com_err("swap_inode_blocks", retval,
-                       _("while calling ext2fs_block_iterate"));
+               bb_error_msg(_("while calling ext2fs_block_iterate"));
                ctx->flags |= E2F_FLAG_ABORT;
                return;
        }
        if (sb.errcode) {
-               com_err("swap_inode_blocks", sb.errcode,
-                       _("while calling iterator function"));
+               bb_error_msg(_("while calling iterator function"));
                ctx->flags |= E2F_FLAG_ABORT;
                return;
        }
@@ -12514,8 +12080,7 @@ static void swap_inodes(e2fsck_t ctx)
        retval = ext2fs_get_mem(fs->blocksize * fs->inode_blocks_per_group,
                                &buf);
        if (retval) {
-               com_err("swap_inodes", retval,
-                       _("while allocating inode buffer"));
+               bb_error_msg(_("while allocating inode buffer"));
                ctx->flags |= E2F_FLAG_ABORT;
                return;
        }
@@ -12526,8 +12091,7 @@ static void swap_inodes(e2fsck_t ctx)
                      fs->group_desc[group].bg_inode_table,
                      fs->inode_blocks_per_group, buf);
                if (retval) {
-                       com_err("swap_inodes", retval,
-                               _("while reading inode table (group %d)"),
+                       bb_error_msg(_("while reading inode table (group %d)"),
                                group);
                        ctx->flags |= E2F_FLAG_ABORT;
                        return;
@@ -12564,8 +12128,7 @@ static void swap_inodes(e2fsck_t ctx)
                      fs->group_desc[group].bg_inode_table,
                      fs->inode_blocks_per_group, buf);
                if (retval) {
-                       com_err("swap_inodes", retval,
-                               _("while writing inode table (group %d)"),
+                       bb_error_msg(_("while writing inode table (group %d)"),
                                group);
                        ctx->flags |= E2F_FLAG_ABORT;
                        return;
@@ -12577,7 +12140,7 @@ static void swap_inodes(e2fsck_t ctx)
        ext2fs_flush_icache(fs);
 }
 
-#if defined(__powerpc__) && defined(EXT2FS_ENABLE_SWAPFS)
+#if defined(__powerpc__) && BB_BIG_ENDIAN
 /*
  * On the PowerPC, the big-endian variant of the ext2 filesystem
  * has its bitmaps stored as 32-bit words with bit 0 as the LSB
@@ -12663,7 +12226,7 @@ void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
        ret = malloc(size);
        if (!ret) {
                sprintf(buf, "Can't allocate %s\n", description);
-               fatal_error(ctx, buf);
+               bb_error_msg_and_die(buf);
        }
        memset(ret, 0, size);
        return ret;
@@ -12772,15 +12335,15 @@ static int ask_yn(const char * string, int def)
 int ask (e2fsck_t ctx, const char * string, int def)
 {
        if (ctx->options & E2F_OPT_NO) {
-               printf (_("%s? no\n\n"), string);
+               printf(_("%s? no\n\n"), string);
                return 0;
        }
        if (ctx->options & E2F_OPT_YES) {
-               printf (_("%s? yes\n\n"), string);
+               printf(_("%s? yes\n\n"), string);
                return 1;
        }
        if (ctx->options & E2F_OPT_PREEN) {
-               printf ("%s? %s\n\n", string, def ? _("yes") : _("no"));
+               printf("%s? %s\n\n", string, def ? _("yes") : _("no"));
                return def;
        }
        return ask_yn(string, def);
@@ -12792,20 +12355,18 @@ void e2fsck_read_bitmaps(e2fsck_t ctx)
        errcode_t       retval;
 
        if (ctx->invalid_bitmaps) {
-               com_err(ctx->program_name, 0,
-                   _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
+               bb_error_msg(_("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
                        ctx->device_name);
-               fatal_error(ctx, 0);
+               bb_error_msg_and_die(0);
        }
 
        ehandler_operation(_("reading inode and block bitmaps"));
        retval = ext2fs_read_bitmaps(fs);
        ehandler_operation(0);
        if (retval) {
-               com_err(ctx->program_name, retval,
-                       _("while retrying to read bitmaps for %s"),
+               bb_error_msg(_("while retrying to read bitmaps for %s"),
                        ctx->device_name);
-               fatal_error(ctx, 0);
+               bb_error_msg_and_die(0);
        }
 }
 
@@ -12819,10 +12380,9 @@ static void e2fsck_write_bitmaps(e2fsck_t ctx)
                retval = ext2fs_write_block_bitmap(fs);
                ehandler_operation(0);
                if (retval) {
-                       com_err(ctx->program_name, retval,
-                           _("while retrying to write block bitmaps for %s"),
+                       bb_error_msg(_("while retrying to write block bitmaps for %s"),
                                ctx->device_name);
-                       fatal_error(ctx, 0);
+                       bb_error_msg_and_die(0);
                }
        }
 
@@ -12831,10 +12391,9 @@ static void e2fsck_write_bitmaps(e2fsck_t ctx)
                retval = ext2fs_write_inode_bitmap(fs);
                ehandler_operation(0);
                if (retval) {
-                       com_err(ctx->program_name, retval,
-                           _("while retrying to write inode bitmaps for %s"),
+                       bb_error_msg(_("while retrying to write inode bitmaps for %s"),
                                ctx->device_name);
-                       fatal_error(ctx, 0);
+                       bb_error_msg_and_die(0);
                }
        }
 }
@@ -12863,9 +12422,8 @@ void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
 
        retval = ext2fs_read_inode(ctx->fs, ino, inode);
        if (retval) {
-               com_err("ext2fs_read_inode", retval,
-                       _("while reading inode %ld in %s"), ino, proc);
-               fatal_error(ctx, 0);
+               bb_error_msg(_("while reading inode %ld in %s"), ino, proc);
+               bb_error_msg_and_die(0);
        }
 }
 
@@ -12877,9 +12435,8 @@ extern void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
 
        retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
        if (retval) {
-               com_err("ext2fs_write_inode", retval,
-                       _("while writing inode %ld in %s"), ino, proc);
-               fatal_error(ctx, 0);
+               bb_error_msg(_("while writing inode %ld in %s"), ino, proc);
+               bb_error_msg_and_die(0);
        }
 }
 
@@ -12890,9 +12447,8 @@ extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
 
        retval = ext2fs_write_inode(ctx->fs, ino, inode);
        if (retval) {
-               com_err("ext2fs_write_inode", retval,
-                       _("while writing inode %ld in %s"), ino, proc);
-               fatal_error(ctx, 0);
+               bb_error_msg(_("while writing inode %ld in %s"), ino, proc);
+               bb_error_msg_and_die(0);
        }
 }
 
@@ -12946,7 +12502,7 @@ blk_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
                if (io_channel_read_blk(io, superblock,
                                        -SUPERBLOCK_SIZE, buf))
                        continue;
-#ifdef EXT2FS_ENABLE_SWAPFS
+#if BB_BIG_ENDIAN
                if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
                        ext2fs_swap_super(sb);
 #endif
@@ -13020,10 +12576,6 @@ static int cflag;               /* check disk */
 static int show_version_only;
 static int verbose;
 
-static int replace_bad_blocks;
-static int keep_bad_blocks;
-static char *bad_blocks_file;
-
 #define P_E2(singular, plural, n)       n, ((n) == 1 ? singular : plural)
 
 static void show_stats(e2fsck_t ctx)
@@ -13054,27 +12606,26 @@ static void show_stats(e2fsck_t ctx)
                       blocks_used, blocks);
                return;
        }
-       printf ("\n%8d inode%s used (%d%%)\n", P_E2("", "s", inodes_used),
+       printf("\n%8d inode%s used (%d%%)\n", P_E2("", "s", inodes_used),
                100 * inodes_used / inodes);
-       printf ("%8d non-contiguous inode%s (%0d.%d%%)\n",
+       printf("%8d non-contiguous inode%s (%0d.%d%%)\n",
                P_E2("", "s", ctx->fs_fragmented),
                frag_percent / 10, frag_percent % 10);
-       printf (_("         # of inodes with ind/dind/tind blocks: %d/%d/%d\n"),
+       printf(_("         # of inodes with ind/dind/tind blocks: %d/%d/%d\n"),
                ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
-       printf ("%8d block%s used (%d%%)\n", P_E2("", "s", blocks_used),
+       printf("%8d block%s used (%d%%)\n", P_E2("", "s", blocks_used),
                (int) ((long long) 100 * blocks_used / blocks));
-       printf ("%8d bad block%s\n", P_E2("", "s", ctx->fs_badblocks_count));
-       printf ("%8d large file%s\n", P_E2("", "s", ctx->large_files));
-       printf ("\n%8d regular file%s\n", P_E2("", "s", ctx->fs_regular_count));
-       printf ("%8d director%s\n", P_E2("y", "ies", ctx->fs_directory_count));
-       printf ("%8d character device file%s\n", P_E2("", "s", ctx->fs_chardev_count));
-       printf ("%8d block device file%s\n", P_E2("", "s", ctx->fs_blockdev_count));
-       printf ("%8d fifo%s\n", P_E2("", "s", ctx->fs_fifo_count));
-       printf ("%8d link%s\n", P_E2("", "s", ctx->fs_links_count - dir_links));
-       printf ("%8d symbolic link%s", P_E2("", "s", ctx->fs_symlinks_count));
-       printf (" (%d fast symbolic link%s)\n", P_E2("", "s", ctx->fs_fast_symlinks_count));
-       printf ("%8d socket%s--------\n\n", P_E2("", "s", ctx->fs_sockets_count));
-       printf ("%8d file%s\n", P_E2("", "s", ctx->fs_total_count - dir_links));
+       printf("%8d large file%s\n", P_E2("", "s", ctx->large_files));
+       printf("\n%8d regular file%s\n", P_E2("", "s", ctx->fs_regular_count));
+       printf("%8d director%s\n", P_E2("y", "ies", ctx->fs_directory_count));
+       printf("%8d character device file%s\n", P_E2("", "s", ctx->fs_chardev_count));
+       printf("%8d block device file%s\n", P_E2("", "s", ctx->fs_blockdev_count));
+       printf("%8d fifo%s\n", P_E2("", "s", ctx->fs_fifo_count));
+       printf("%8d link%s\n", P_E2("", "s", ctx->fs_links_count - dir_links));
+       printf("%8d symbolic link%s", P_E2("", "s", ctx->fs_symlinks_count));
+       printf(" (%d fast symbolic link%s)\n", P_E2("", "s", ctx->fs_fast_symlinks_count));
+       printf("%8d socket%s--------\n\n", P_E2("", "s", ctx->fs_sockets_count));
+       printf("%8d file%s\n", P_E2("", "s", ctx->fs_total_count - dir_links));
 }
 
 static void check_mount(e2fsck_t ctx)
@@ -13085,8 +12636,7 @@ static void check_mount(e2fsck_t ctx)
        retval = ext2fs_check_if_mounted(ctx->filesystem_name,
                                         &ctx->mount_flags);
        if (retval) {
-               com_err("ext2fs_check_if_mount", retval,
-                       _("while determining whether %s is mounted."),
+               bb_error_msg(_("while determining whether %s is mounted."),
                        ctx->filesystem_name);
                return;
        }
@@ -13107,13 +12657,13 @@ static void check_mount(e2fsck_t ctx)
 
        printf(_("%s is mounted.  "), ctx->filesystem_name);
        if (!ctx->interactive)
-               fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
+               bb_error_msg_and_die(_("Cannot continue, aborting."));
        printf(_("\n\n\007\007\007\007WARNING!!!  "
               "Running e2fsck on a mounted filesystem may cause\n"
               "SEVERE filesystem damage.\007\007\007\n\n"));
        cont = ask_yn(_("Do you really want to continue"), -1);
        if (!cont) {
-               printf (_("check aborted.\n"));
+               printf(_("check aborted.\n"));
                exit (0);
        }
        return;
@@ -13171,8 +12721,7 @@ static void check_if_skip(e2fsck_t ctx)
        int batt = is_on_batt();
        time_t now = time(0);
 
-       if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file ||
-           cflag || swapfs)
+       if ((ctx->options & E2F_OPT_FORCE) || cflag || swapfs)
                return;
 
        if ((fs->super->s_state & EXT2_ERROR_FS) ||
@@ -13366,7 +12915,7 @@ static void reserve_stdio_fds(void)
                if (fd > 2)
                        break;
                if (fd < 0) {
-                       fprintf(stderr, _("ERROR: Couldn't open "
+                       fprintf(stderr, _("ERROR: Cannot open "
                                "/dev/null (%s)\n"),
                                strerror(errno));
                        break;
@@ -13502,8 +13051,7 @@ static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
                                _("Error validating file descriptor %d: %s\n"),
                                        ctx->progress_fd,
                                        error_message(errno));
-                               fatal_error(ctx,
-                       _("Invalid completion information file descriptor"));
+                               bb_error_msg_and_die(_("Invalid completion information file descriptor"));
                        } else
                                close(fd);
                        break;
@@ -13517,8 +13065,7 @@ static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
                case 'a':
                        if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
                        conflict_opt:
-                               fatal_error(ctx,
-       _("Only one the options -p/-a, -n or -y may be specified."));
+                               bb_error_msg_and_die(_("Only one the options -p/-a, -n or -y may be specified."));
                        }
                        ctx->options |= E2F_OPT_PREEN;
                        break;
@@ -13561,11 +13108,6 @@ static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
                case 'P':
                        ctx->process_inode_size = atoi(optarg);
                        break;
-               case 'L':
-                       replace_bad_blocks++;
-               case 'l':
-                       bad_blocks_file = string_copy(optarg, 0);
-                       break;
                case 'd':
                        ctx->options |= E2F_OPT_DEBUG;
                        break;
@@ -13598,17 +13140,14 @@ static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
                                          "of e2fsck\n"));
                        exit(1);
 #endif
-               case 'k':
-                       keep_bad_blocks++;
-                       break;
                default:
-                       usage();
+                       bb_show_usage();
                }
        if (show_version_only)
                return 0;
        if (optind != argc - 1)
-               usage();
-       if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
+               bb_show_usage();
+       if ((ctx->options & E2F_OPT_NO) &&
            !cflag && !swapfs && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
                ctx->options |= E2F_OPT_READONLY;
        ctx->io_options = strchr(argv[optind], '?');
@@ -13616,9 +13155,8 @@ static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
                *ctx->io_options++ = 0;
        ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
        if (!ctx->filesystem_name) {
-               com_err(ctx->program_name, 0, _("Unable to resolve '%s'"),
-                       argv[optind]);
-               fatal_error(ctx, 0);
+               bb_error_msg(_("Unable to resolve '%s'"), argv[optind]);
+               bb_error_msg_and_die(0);
        }
        if (extended_opts)
                parse_extended_opts(ctx, extended_opts);
@@ -13626,33 +13164,24 @@ static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
        if (flush) {
                fd = open(ctx->filesystem_name, O_RDONLY, 0);
                if (fd < 0) {
-                       com_err("open", errno,
-                               _("while opening %s for flushing"),
+                       bb_error_msg(_("while opening %s for flushing"),
                                ctx->filesystem_name);
-                       fatal_error(ctx, 0);
+                       bb_error_msg_and_die(0);
                }
                if ((retval = ext2fs_sync_device(fd, 1))) {
-                       com_err("ext2fs_sync_device", retval,
-                               _("while trying to flush %s"),
+                       bb_error_msg(_("while trying to flush %s"),
                                ctx->filesystem_name);
-                       fatal_error(ctx, 0);
+                       bb_error_msg_and_die(0);
                }
                close(fd);
        }
 #ifdef ENABLE_SWAPFS
-       if (swapfs) {
-               if (cflag || bad_blocks_file) {
+       if (swapfs && cflag) {
                        fprintf(stderr, _("Incompatible options not "
                                          "allowed when byte-swapping.\n"));
                        exit(EXIT_USAGE);
-               }
        }
 #endif
-       if (cflag && bad_blocks_file) {
-               fprintf(stderr, _("The -c and the -l/-L options may "
-                                 "not be both used at the same time.\n"));
-               exit(EXIT_USAGE);
-       }
        /*
         * Set up signal action
         */
@@ -13703,8 +13232,7 @@ int e2fsck_main (int argc, char *argv[])
 
        retval = PRS(argc, argv, &ctx);
        if (retval) {
-               com_err("e2fsck", retval,
-                       _("while trying to initialize program"));
+               bb_error_msg(_("while trying to initialize program"));
                exit(EXIT_ERROR);
        }
        reserve_stdio_fds();
@@ -13725,8 +13253,7 @@ int e2fsck_main (int argc, char *argv[])
            !(ctx->options & E2F_OPT_NO) &&
            !(ctx->options & E2F_OPT_YES)) {
                if (!ctx->interactive)
-                       fatal_error(ctx,
-                                   _("need terminal for interactive repairs"));
+                       bb_error_msg_and_die(_("need terminal for interactive repairs"));
        }
        ctx->superblock = ctx->use_superblock;
 restart:
@@ -13773,7 +13300,7 @@ restart:
                }
        }
        if (retval) {
-               com_err(ctx->program_name, retval, _("while trying to open %s"),
+               bb_error_msg(_("while trying to open %s"),
                        ctx->filesystem_name);
                if (retval == EXT2_ET_REV_TOO_HIGH) {
                        printf(_("The filesystem revision is apparently "
@@ -13798,17 +13325,16 @@ restart:
 #endif
                else
                        fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
-               fatal_error(ctx, 0);
+               bb_error_msg_and_die(0);
        }
        ctx->fs = fs;
        fs->priv_data = ctx;
        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"),
+               bb_error_msg(_("while trying to open %s"),
                        ctx->filesystem_name);
        get_newer:
-               fatal_error(ctx, _("Get a newer version of e2fsck!"));
+               bb_error_msg_and_die(_("Get a newer version of e2fsck!"));
        }
 
        /*
@@ -13828,10 +13354,9 @@ restart:
         */
        retval = e2fsck_check_ext3_journal(ctx);
        if (retval) {
-               com_err(ctx->program_name, retval,
-                       _("while checking ext3 journal for %s"),
+               bb_error_msg(_("while checking ext3 journal for %s"),
                        ctx->device_name);
-               fatal_error(ctx, 0);
+               bb_error_msg_and_die(0);
        }
 
        /*
@@ -13852,16 +13377,14 @@ restart:
                                 * 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);
+                               bb_error_msg(_("unable to set superblock flags on %s"), ctx->device_name);
+                               bb_error_msg_and_die(0);
                        }
                        retval = e2fsck_run_ext3_journal(ctx);
                        if (retval) {
-                               com_err(ctx->program_name, retval,
-                               _("while recovering ext3 journal of %s"),
+                               bb_error_msg(_("while recovering ext3 journal of %s"),
                                        ctx->device_name);
-                               fatal_error(ctx, 0);
+                               bb_error_msg_and_die(0);
                        }
                        ext2fs_close(ctx->fs);
                        ctx->fs = 0;
@@ -13876,26 +13399,22 @@ restart:
         */
        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);
+               bb_error_msg("(%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);
+               bb_error_msg("(%s)", ctx->device_name);
                goto get_newer;
        }
 #ifdef ENABLE_COMPRESSION
        /* FIXME - do we support this at all? */
        if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
-               com_err(ctx->program_name, 0,
-                       _("Warning: compression support is experimental.\n"));
+               bb_error_msg(_("Warning: compression support is experimental."));
 #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"),
+               bb_error_msg(_("E2fsck not compiled with HTREE support,\n\t"
+                         "but filesystem %s has HTREE directories."),
                        ctx->device_name);
                goto get_newer;
        }
@@ -13926,14 +13445,10 @@ restart:
        ext2fs_mark_valid(fs);
        check_super_block(ctx);
        if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
-               fatal_error(ctx, 0);
+               bb_error_msg_and_die(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);
+               bb_error_msg_and_die(0);
 #ifdef ENABLE_SWAPFS
 
 #ifdef WORDS_BIGENDIAN
@@ -13947,13 +13462,13 @@ restart:
                if ((fs->flags & EXT2_FLAG_SWAP_BYTES) == NATIVE_FLAG) {
                        fprintf(stderr, _("%s: Filesystem byte order "
                                "already normalized.\n"), ctx->device_name);
-                       fatal_error(ctx, 0);
+                       bb_error_msg_and_die(0);
                }
        }
        if (swapfs) {
                swap_filesys(ctx);
                if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
-                       fatal_error(ctx, 0);
+                       bb_error_msg_and_die(0);
        }
 #endif
 
@@ -13964,8 +13479,7 @@ restart:
 
        retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
        if (retval) {
-               com_err(ctx->program_name, retval,
-                       _("while reading bad blocks inode"));
+               bb_error_msg(_("while reading bad blocks inode"));
                preenhalt(ctx);
                printf(_("This doesn't bode well,"
                         " but we'll try to go on...\n"));
@@ -13977,9 +13491,8 @@ 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);
+                       bb_error_msg(_("while resetting context"));
+                       bb_error_msg_and_die(0);
                }
                ext2fs_close(fs);
                goto restart;
@@ -13990,7 +13503,7 @@ restart:
                exit_value |= FSCK_CANCELED;
        }
        if (run_result & E2F_FLAG_ABORT)
-               fatal_error(ctx, _("aborted"));
+               bb_error_msg_and_die(_("aborted"));
 
        /* Cleanup */
        if (ext2fs_test_changed(fs)) {