Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * badblocks.c --- replace/append bad blocks to the bad block inode |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 3 | * |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 4 | * Copyright (C) 1993, 1994 Theodore Ts'o. This file may be |
| 5 | * redistributed under the terms of the GNU Public License. |
| 6 | */ |
| 7 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 8 | #include "config.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 9 | #include <time.h> |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 10 | #ifdef HAVE_ERRNO_H |
| 11 | #include <errno.h> |
| 12 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 13 | |
| 14 | #include <et/com_err.h> |
| 15 | #include "e2fsck.h" |
| 16 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 17 | static int check_bb_inode_blocks(ext2_filsys fs, blk_t *block_nr, int blockcnt, |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 18 | void *priv_data); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 19 | |
| 20 | |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 21 | static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 22 | { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 23 | printf(_("Bad block %u out of range; ignored.\n"), blk); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 24 | return; |
| 25 | } |
| 26 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 27 | void read_bad_blocks_file(e2fsck_t ctx, const char *bad_blocks_file, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 28 | int replace_bad_blocks) |
| 29 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 30 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 31 | errcode_t retval; |
| 32 | badblocks_list bb_list = 0; |
| 33 | FILE *f; |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 34 | char buf[1024]; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 35 | |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 36 | e2fsck_read_bitmaps(ctx); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * Make sure the bad block inode is sane. If there are any |
| 40 | * illegal blocks, clear them. |
| 41 | */ |
| 42 | retval = ext2fs_block_iterate(fs, EXT2_BAD_INO, 0, 0, |
| 43 | check_bb_inode_blocks, 0); |
| 44 | if (retval) { |
| 45 | com_err("ext2fs_block_iterate", retval, |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 46 | _("while sanity checking the bad blocks inode")); |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 47 | goto fatal; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 48 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 49 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 50 | /* |
| 51 | * If we're appending to the bad blocks inode, read in the |
| 52 | * current bad blocks. |
| 53 | */ |
| 54 | if (!replace_bad_blocks) { |
| 55 | retval = ext2fs_read_bb_inode(fs, &bb_list); |
| 56 | if (retval) { |
| 57 | com_err("ext2fs_read_bb_inode", retval, |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 58 | _("while reading the bad blocks inode")); |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 59 | goto fatal; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 62 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 63 | /* |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 64 | * Now read in the bad blocks from the file; if |
| 65 | * bad_blocks_file is null, then try to run the badblocks |
| 66 | * command. |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 67 | */ |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 68 | if (bad_blocks_file) { |
| 69 | f = fopen(bad_blocks_file, "r"); |
| 70 | if (!f) { |
| 71 | com_err("read_bad_blocks_file", errno, |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 72 | _("while trying to open %s"), bad_blocks_file); |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 73 | goto fatal; |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 74 | } |
| 75 | } else { |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 76 | sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize, |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 77 | (ctx->options & E2F_OPT_PREEN) ? "" : "-s ", |
Theodore Ts'o | 3ed57c2 | 2001-12-24 15:01:59 -0500 | [diff] [blame] | 78 | (ctx->options & E2F_OPT_WRITECHECK) ? "-n " : "", |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 79 | fs->device_name, ext2fs_blocks_count(fs->super)-1); |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 80 | f = popen(buf, "r"); |
| 81 | if (!f) { |
| 82 | com_err("read_bad_blocks_file", errno, |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 83 | _("while trying popen '%s'"), buf); |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 84 | goto fatal; |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 85 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 86 | } |
| 87 | retval = ext2fs_read_bb_FILE(fs, f, &bb_list, invalid_block); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 88 | if (bad_blocks_file) |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 89 | fclose(f); |
| 90 | else |
| 91 | pclose(f); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 92 | if (retval) { |
| 93 | com_err("ext2fs_read_bb_FILE", retval, |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 94 | _("while reading in list of bad blocks from file")); |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 95 | goto fatal; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 96 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 97 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 98 | /* |
| 99 | * Finally, update the bad blocks from the bad_block_map |
| 100 | */ |
Theodore Ts'o | 4f9abdc | 2008-02-27 15:10:20 -0500 | [diff] [blame] | 101 | printf("%s: Updating bad block inode.\n", ctx->device_name); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 102 | retval = ext2fs_update_bb_inode(fs, bb_list); |
| 103 | if (retval) { |
| 104 | com_err("ext2fs_update_bb_inode", retval, |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 105 | _("while updating bad block inode")); |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 106 | goto fatal; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Theodore Ts'o | cbbf031 | 2001-06-13 00:12:04 +0000 | [diff] [blame] | 109 | ext2fs_badblocks_list_free(bb_list); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 110 | return; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 111 | |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 112 | fatal: |
| 113 | ctx->flags |= E2F_FLAG_ABORT; |
| 114 | return; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 115 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 118 | static int check_bb_inode_blocks(ext2_filsys fs, |
| 119 | blk_t *block_nr, |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 120 | int blockcnt EXT2FS_ATTR((unused)), |
| 121 | void *priv_data EXT2FS_ATTR((unused))) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 122 | { |
| 123 | if (!*block_nr) |
| 124 | return 0; |
| 125 | |
| 126 | /* |
| 127 | * If the block number is outrageous, clear it and ignore it. |
| 128 | */ |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 129 | if (*block_nr >= ext2fs_blocks_count(fs->super) || |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 130 | *block_nr < fs->super->s_first_data_block) { |
Theodore Ts'o | f37ab68 | 2005-05-05 23:15:55 -0400 | [diff] [blame] | 131 | printf(_("Warning: illegal block %u found in bad block inode. " |
| 132 | "Cleared.\n"), *block_nr); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 133 | *block_nr = 0; |
| 134 | return BLOCK_CHANGED; |
| 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |