blob: 71b1c56e977b885419366bd94c268e4e6aee6620 [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
Theodore Ts'od1154eb2011-09-18 17:34:37 -04008#include "config.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +00009#include <time.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000010#ifdef HAVE_ERRNO_H
11#include <errno.h>
12#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000013
14#include <et/com_err.h>
15#include "e2fsck.h"
16
Theodore Ts'of3db3561997-04-26 13:34:30 +000017static int check_bb_inode_blocks(ext2_filsys fs, blk_t *block_nr, int blockcnt,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +000018 void *priv_data);
Theodore Ts'of3db3561997-04-26 13:34:30 +000019
20
Theodore Ts'o54434922003-12-07 01:28:50 -050021static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +000022{
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000023 printf(_("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +000024 return;
25}
26
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000027void read_bad_blocks_file(e2fsck_t ctx, const char *bad_blocks_file,
Theodore Ts'o3839e651997-04-26 13:21:57 +000028 int replace_bad_blocks)
29{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000030 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +000031 errcode_t retval;
32 badblocks_list bb_list = 0;
33 FILE *f;
Theodore Ts'o74becf31997-04-26 14:37:06 +000034 char buf[1024];
Theodore Ts'o3839e651997-04-26 13:21:57 +000035
Theodore Ts'of8188ff1997-11-14 05:23:04 +000036 e2fsck_read_bitmaps(ctx);
Theodore Ts'of3db3561997-04-26 13:34:30 +000037
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'o0c4a0722000-02-07 03:11:03 +000046 _("while sanity checking the bad blocks inode"));
Theodore Ts'of8188ff1997-11-14 05:23:04 +000047 goto fatal;
Theodore Ts'of3db3561997-04-26 13:34:30 +000048 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -040049
Theodore Ts'o3839e651997-04-26 13:21:57 +000050 /*
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'o0c4a0722000-02-07 03:11:03 +000058 _("while reading the bad blocks inode"));
Theodore Ts'of8188ff1997-11-14 05:23:04 +000059 goto fatal;
Theodore Ts'o3839e651997-04-26 13:21:57 +000060 }
61 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -040062
Theodore Ts'o3839e651997-04-26 13:21:57 +000063 /*
Theodore Ts'o74becf31997-04-26 14:37:06 +000064 * 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'o3839e651997-04-26 13:21:57 +000067 */
Theodore Ts'o74becf31997-04-26 14:37:06 +000068 if (bad_blocks_file) {
69 f = fopen(bad_blocks_file, "r");
70 if (!f) {
71 com_err("read_bad_blocks_file", errno,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000072 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'of8188ff1997-11-14 05:23:04 +000073 goto fatal;
Theodore Ts'o74becf31997-04-26 14:37:06 +000074 }
75 } else {
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -040076 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000077 (ctx->options & E2F_OPT_PREEN) ? "" : "-s ",
Theodore Ts'o3ed57c22001-12-24 15:01:59 -050078 (ctx->options & E2F_OPT_WRITECHECK) ? "-n " : "",
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -040079 fs->device_name, ext2fs_blocks_count(fs->super)-1);
Theodore Ts'o74becf31997-04-26 14:37:06 +000080 f = popen(buf, "r");
81 if (!f) {
82 com_err("read_bad_blocks_file", errno,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000083 _("while trying popen '%s'"), buf);
Theodore Ts'of8188ff1997-11-14 05:23:04 +000084 goto fatal;
Theodore Ts'o74becf31997-04-26 14:37:06 +000085 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000086 }
87 retval = ext2fs_read_bb_FILE(fs, f, &bb_list, invalid_block);
Theodore Ts'oefc6f622008-08-27 23:07:54 -040088 if (bad_blocks_file)
Theodore Ts'o74becf31997-04-26 14:37:06 +000089 fclose(f);
90 else
91 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +000092 if (retval) {
93 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000094 _("while reading in list of bad blocks from file"));
Theodore Ts'of8188ff1997-11-14 05:23:04 +000095 goto fatal;
Theodore Ts'o3839e651997-04-26 13:21:57 +000096 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -040097
Theodore Ts'o3839e651997-04-26 13:21:57 +000098 /*
99 * Finally, update the bad blocks from the bad_block_map
100 */
Theodore Ts'o4f9abdc2008-02-27 15:10:20 -0500101 printf("%s: Updating bad block inode.\n", ctx->device_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000102 retval = ext2fs_update_bb_inode(fs, bb_list);
103 if (retval) {
104 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000105 _("while updating bad block inode"));
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000106 goto fatal;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000107 }
108
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000109 ext2fs_badblocks_list_free(bb_list);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000110 return;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400111
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000112fatal:
113 ctx->flags |= E2F_FLAG_ABORT;
114 return;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400115
Theodore Ts'o3839e651997-04-26 13:21:57 +0000116}
117
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400118static int check_bb_inode_blocks(ext2_filsys fs,
119 blk_t *block_nr,
Theodore Ts'o54434922003-12-07 01:28:50 -0500120 int blockcnt EXT2FS_ATTR((unused)),
121 void *priv_data EXT2FS_ATTR((unused)))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000122{
123 if (!*block_nr)
124 return 0;
125
126 /*
127 * If the block number is outrageous, clear it and ignore it.
128 */
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400129 if (*block_nr >= ext2fs_blocks_count(fs->super) ||
Theodore Ts'of3db3561997-04-26 13:34:30 +0000130 *block_nr < fs->super->s_first_data_block) {
Theodore Ts'of37ab682005-05-05 23:15:55 -0400131 printf(_("Warning: illegal block %u found in bad block inode. "
132 "Cleared.\n"), *block_nr);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000133 *block_nr = 0;
134 return BLOCK_CHANGED;
135 }
136
137 return 0;
138}
139