]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - e2fsck/badblocks.c
mke2fs: free tdb_dir string if it came from the profile
[thirdparty/e2fsprogs.git] / e2fsck / badblocks.c
CommitLineData
3839e657
TT
1/*
2 * badblocks.c --- replace/append bad blocks to the bad block inode
efc6f628 3 *
3839e657
TT
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>
50e1e10f
TT
9#ifdef HAVE_ERRNO_H
10#include <errno.h>
11#endif
3839e657
TT
12
13#include <et/com_err.h>
14#include "e2fsck.h"
15
f3db3566 16static int check_bb_inode_blocks(ext2_filsys fs, blk_t *block_nr, int blockcnt,
54dc7ca2 17 void *priv_data);
f3db3566
TT
18
19
54434927 20static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
3839e657 21{
0c4a0726 22 printf(_("Bad block %u out of range; ignored.\n"), blk);
3839e657
TT
23 return;
24}
25
1b6bf175 26void read_bad_blocks_file(e2fsck_t ctx, const char *bad_blocks_file,
3839e657
TT
27 int replace_bad_blocks)
28{
1b6bf175 29 ext2_filsys fs = ctx->fs;
3839e657
TT
30 errcode_t retval;
31 badblocks_list bb_list = 0;
32 FILE *f;
74becf3c 33 char buf[1024];
3839e657 34
f8188fff 35 e2fsck_read_bitmaps(ctx);
f3db3566
TT
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,
0c4a0726 45 _("while sanity checking the bad blocks inode"));
f8188fff 46 goto fatal;
f3db3566 47 }
efc6f628 48
3839e657
TT
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,
0c4a0726 57 _("while reading the bad blocks inode"));
f8188fff 58 goto fatal;
3839e657
TT
59 }
60 }
efc6f628 61
3839e657 62 /*
74becf3c
TT
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.
3839e657 66 */
74becf3c
TT
67 if (bad_blocks_file) {
68 f = fopen(bad_blocks_file, "r");
69 if (!f) {
70 com_err("read_bad_blocks_file", errno,
0c4a0726 71 _("while trying to open %s"), bad_blocks_file);
f8188fff 72 goto fatal;
74becf3c
TT
73 }
74 } else {
4efbac6f 75 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
1b6bf175 76 (ctx->options & E2F_OPT_PREEN) ? "" : "-s ",
3ed57c27 77 (ctx->options & E2F_OPT_WRITECHECK) ? "-n " : "",
4efbac6f 78 fs->device_name, ext2fs_blocks_count(fs->super)-1);
74becf3c
TT
79 f = popen(buf, "r");
80 if (!f) {
81 com_err("read_bad_blocks_file", errno,
0c4a0726 82 _("while trying popen '%s'"), buf);
f8188fff 83 goto fatal;
74becf3c 84 }
3839e657
TT
85 }
86 retval = ext2fs_read_bb_FILE(fs, f, &bb_list, invalid_block);
efc6f628 87 if (bad_blocks_file)
74becf3c
TT
88 fclose(f);
89 else
90 pclose(f);
3839e657
TT
91 if (retval) {
92 com_err("ext2fs_read_bb_FILE", retval,
0c4a0726 93 _("while reading in list of bad blocks from file"));
f8188fff 94 goto fatal;
3839e657 95 }
efc6f628 96
3839e657
TT
97 /*
98 * Finally, update the bad blocks from the bad_block_map
99 */
4f9abdcb 100 printf("%s: Updating bad block inode.\n", ctx->device_name);
3839e657
TT
101 retval = ext2fs_update_bb_inode(fs, bb_list);
102 if (retval) {
103 com_err("ext2fs_update_bb_inode", retval,
0c4a0726 104 _("while updating bad block inode"));
f8188fff 105 goto fatal;
3839e657
TT
106 }
107
cbbf031b 108 ext2fs_badblocks_list_free(bb_list);
3839e657 109 return;
efc6f628 110
f8188fff
TT
111fatal:
112 ctx->flags |= E2F_FLAG_ABORT;
113 return;
efc6f628 114
3839e657
TT
115}
116
efc6f628
TT
117static int check_bb_inode_blocks(ext2_filsys fs,
118 blk_t *block_nr,
54434927
TT
119 int blockcnt EXT2FS_ATTR((unused)),
120 void *priv_data EXT2FS_ATTR((unused)))
f3db3566
TT
121{
122 if (!*block_nr)
123 return 0;
124
125 /*
126 * If the block number is outrageous, clear it and ignore it.
127 */
4efbac6f 128 if (*block_nr >= ext2fs_blocks_count(fs->super) ||
f3db3566 129 *block_nr < fs->super->s_first_data_block) {
f37ab68a
TT
130 printf(_("Warning: illegal block %u found in bad block inode. "
131 "Cleared.\n"), *block_nr);
f3db3566
TT
132 *block_nr = 0;
133 return BLOCK_CHANGED;
134 }
135
136 return 0;
137}
138