blob: 74e414bffcaec87e2aa422236ca5f9963f3a1725 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * badblocks.c --- replace/append bad blocks to the bad block inode
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o3839e651997-04-26 13:21:57 +00004 * 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,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +000017 void *priv_data);
Theodore Ts'of3db3561997-04-26 13:34:30 +000018
19
Theodore Ts'o54434922003-12-07 01:28:50 -050020static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +000021{
Theodore Ts'o0c4a0722000-02-07 03:11:03 +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'of8188ff1997-11-14 05:23:04 +000035 e2fsck_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) {
JP Abgralle0ed7402014-03-19 19:08:39 -070044 com_err("ext2fs_block_iterate", retval, "%s",
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000045 _("while sanity checking the bad blocks inode"));
Theodore Ts'of8188ff1997-11-14 05:23:04 +000046 goto fatal;
Theodore Ts'of3db3561997-04-26 13:34:30 +000047 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -040048
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) {
JP Abgralle0ed7402014-03-19 19:08:39 -070056 com_err("ext2fs_read_bb_inode", retval, "%s",
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000057 _("while reading the bad blocks inode"));
Theodore Ts'of8188ff1997-11-14 05:23:04 +000058 goto fatal;
Theodore Ts'o3839e651997-04-26 13:21:57 +000059 }
60 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -040061
Theodore Ts'o3839e651997-04-26 13:21:57 +000062 /*
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,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000071 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'of8188ff1997-11-14 05:23:04 +000072 goto fatal;
Theodore Ts'o74becf31997-04-26 14:37:06 +000073 }
74 } else {
JP Abgralle0ed7402014-03-19 19:08:39 -070075 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000076 (ctx->options & E2F_OPT_PREEN) ? "" : "-s ",
Theodore Ts'o3ed57c22001-12-24 15:01:59 -050077 (ctx->options & E2F_OPT_WRITECHECK) ? "-n " : "",
JP Abgralle0ed7402014-03-19 19:08:39 -070078 fs->device_name, ext2fs_blocks_count(fs->super)-1);
Theodore Ts'o74becf31997-04-26 14:37:06 +000079 f = popen(buf, "r");
80 if (!f) {
81 com_err("read_bad_blocks_file", errno,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000082 _("while trying popen '%s'"), buf);
Theodore Ts'of8188ff1997-11-14 05:23:04 +000083 goto fatal;
Theodore Ts'o74becf31997-04-26 14:37:06 +000084 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000085 }
86 retval = ext2fs_read_bb_FILE(fs, f, &bb_list, invalid_block);
Theodore Ts'oefc6f622008-08-27 23:07:54 -040087 if (bad_blocks_file)
Theodore Ts'o74becf31997-04-26 14:37:06 +000088 fclose(f);
89 else
90 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +000091 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -070092 com_err("ext2fs_read_bb_FILE", retval, "%s",
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000093 _("while reading in list of bad blocks from file"));
Theodore Ts'of8188ff1997-11-14 05:23:04 +000094 goto fatal;
Theodore Ts'o3839e651997-04-26 13:21:57 +000095 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -040096
Theodore Ts'o3839e651997-04-26 13:21:57 +000097 /*
98 * Finally, update the bad blocks from the bad_block_map
99 */
Theodore Ts'o4f9abdc2008-02-27 15:10:20 -0500100 printf("%s: Updating bad block inode.\n", ctx->device_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000101 retval = ext2fs_update_bb_inode(fs, bb_list);
102 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700103 com_err("ext2fs_update_bb_inode", retval, "%s",
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000104 _("while updating bad block inode"));
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000105 goto fatal;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000106 }
107
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000108 ext2fs_badblocks_list_free(bb_list);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000109 return;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400110
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000111fatal:
112 ctx->flags |= E2F_FLAG_ABORT;
113 return;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400114
Theodore Ts'o3839e651997-04-26 13:21:57 +0000115}
116
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400117static int check_bb_inode_blocks(ext2_filsys fs,
118 blk_t *block_nr,
Theodore Ts'o54434922003-12-07 01:28:50 -0500119 int blockcnt EXT2FS_ATTR((unused)),
120 void *priv_data EXT2FS_ATTR((unused)))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000121{
122 if (!*block_nr)
123 return 0;
124
125 /*
126 * If the block number is outrageous, clear it and ignore it.
127 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700128 if (*block_nr >= ext2fs_blocks_count(fs->super) ||
Theodore Ts'of3db3561997-04-26 13:34:30 +0000129 *block_nr < fs->super->s_first_data_block) {
Theodore Ts'of37ab682005-05-05 23:15:55 -0400130 printf(_("Warning: illegal block %u found in bad block inode. "
131 "Cleared.\n"), *block_nr);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000132 *block_nr = 0;
133 return BLOCK_CHANGED;
134 }
135
136 return 0;
137}
138