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