blob: a9addf750e6a5203440666aa6bc3da2d94f4a916 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
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'o50e1e101997-04-26 13:58:21 +00009#ifdef HAVE_ERRNO_H
10#include <errno.h>
11#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000012
13#include <et/com_err.h>
14#include "e2fsck.h"
15
Theodore Ts'of3db3561997-04-26 13:34:30 +000016static int check_bb_inode_blocks(ext2_filsys fs, blk_t *block_nr, int blockcnt,
17 void *private);
18
19
Theodore Ts'o3839e651997-04-26 13:21:57 +000020static void invalid_block(ext2_filsys fs, blk_t blk)
21{
Theodore Ts'o50e1e101997-04-26 13:58:21 +000022 printf("Bad block %u out of range; ignored.\n", blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +000023 return;
24}
25
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000026void read_bad_blocks_file(e2fsck_t ctx, const char *bad_blocks_file,
Theodore Ts'o3839e651997-04-26 13:21:57 +000027 int replace_bad_blocks)
28{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000029 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +000030 errcode_t retval;
31 badblocks_list bb_list = 0;
32 FILE *f;
Theodore Ts'o74becf31997-04-26 14:37:06 +000033 char buf[1024];
Theodore Ts'o3839e651997-04-26 13:21:57 +000034
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000035 read_bitmaps(ctx);
Theodore Ts'of3db3561997-04-26 13:34:30 +000036
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,
45 "while sanity checking the bad blocks inode");
46 fatal_error(0);
47 }
48
Theodore Ts'o3839e651997-04-26 13:21:57 +000049 /*
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,
57 "while reading the bad blocks inode");
58 fatal_error(0);
59 }
60 }
61
62 /*
Theodore Ts'o74becf31997-04-26 14:37:06 +000063 * 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'o3839e651997-04-26 13:21:57 +000066 */
Theodore Ts'o74becf31997-04-26 14:37:06 +000067 if (bad_blocks_file) {
68 f = fopen(bad_blocks_file, "r");
69 if (!f) {
70 com_err("read_bad_blocks_file", errno,
71 "while trying to open %s", bad_blocks_file);
72 fatal_error(0);
73 }
74 } else {
Theodore Ts'of635d7f1997-05-09 02:50:16 +000075 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000076 (ctx->options & E2F_OPT_PREEN) ? "" : "-s ",
77 fs->device_name, fs->super->s_blocks_count);
Theodore Ts'o74becf31997-04-26 14:37:06 +000078 f = popen(buf, "r");
79 if (!f) {
80 com_err("read_bad_blocks_file", errno,
81 "while trying popen '%s'", buf);
82 fatal_error(0);
83 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000084 }
85 retval = ext2fs_read_bb_FILE(fs, f, &bb_list, invalid_block);
Theodore Ts'o74becf31997-04-26 14:37:06 +000086 if (bad_blocks_file)
87 fclose(f);
88 else
89 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +000090 if (retval) {
91 com_err("ext2fs_read_bb_FILE", retval,
92 "while reading in list of bad blocks from file");
93 fatal_error(0);
94 }
95
96 /*
97 * Finally, update the bad blocks from the bad_block_map
98 */
99 retval = ext2fs_update_bb_inode(fs, bb_list);
100 if (retval) {
101 com_err("ext2fs_update_bb_inode", retval,
102 "while updating bad block inode");
103 fatal_error(0);
104 }
105
106 badblocks_list_free(bb_list);
107 return;
108}
109
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000110void test_disk(e2fsck_t ctx)
Theodore Ts'o74becf31997-04-26 14:37:06 +0000111{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000112 read_bad_blocks_file(ctx, 0, 1);
Theodore Ts'o74becf31997-04-26 14:37:06 +0000113}
114
Theodore Ts'of3db3561997-04-26 13:34:30 +0000115static int check_bb_inode_blocks(ext2_filsys fs, blk_t *block_nr, int blockcnt,
116 void *private)
117{
118 if (!*block_nr)
119 return 0;
120
121 /*
122 * If the block number is outrageous, clear it and ignore it.
123 */
124 if (*block_nr >= fs->super->s_blocks_count ||
125 *block_nr < fs->super->s_first_data_block) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000126 printf("Warning illegal block %u found in bad block inode. Cleared.\n", *block_nr);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000127 *block_nr = 0;
128 return BLOCK_CHANGED;
129 }
130
131 return 0;
132}
133