Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / e2fsck / badblocks.c
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>
9
10 #include <et/com_err.h>
11 #include "e2fsck.h"
12
13 static void invalid_block(ext2_filsys fs, blk_t blk)
14 {
15         printf("Bad block %lu out of range; ignored.\n", blk);
16         return;
17 }
18
19 void read_bad_blocks_file(ext2_filsys fs, const char *bad_blocks_file,
20                           int replace_bad_blocks)
21 {
22         errcode_t       retval;
23         badblocks_list  bb_list = 0;
24         FILE            *f;
25
26         read_bitmaps(fs);
27         
28         /*
29          * If we're appending to the bad blocks inode, read in the
30          * current bad blocks.
31          */
32         if (!replace_bad_blocks) {
33                 retval = ext2fs_read_bb_inode(fs, &bb_list);
34                 if (retval) {
35                         com_err("ext2fs_read_bb_inode", retval,
36                                 "while reading the bad blocks inode");
37                         fatal_error(0);
38                 }
39         }
40         
41         /*
42          * Now read in the bad blocks from the file.
43          */
44         f = fopen(bad_blocks_file, "r");
45         if (!f) {
46                 com_err("read_bad_blocks_file", errno,
47                         "while trying to open %s", bad_blocks_file);
48                 fatal_error(0);
49         }
50         retval = ext2fs_read_bb_FILE(fs, f, &bb_list, invalid_block);
51         fclose (f);
52         if (retval) {
53                 com_err("ext2fs_read_bb_FILE", retval,
54                         "while reading in list of bad blocks from file");
55                 fatal_error(0);
56         }
57         
58         /*
59          * Finally, update the bad blocks from the bad_block_map
60          */
61         retval = ext2fs_update_bb_inode(fs, bb_list);
62         if (retval) {
63                 com_err("ext2fs_update_bb_inode", retval,
64                         "while updating bad block inode");
65                 fatal_error(0);
66         }
67
68         badblocks_list_free(bb_list);
69         return;
70 }
71
72 void test_disk(ext2_filsys fs)
73 {
74         errcode_t       retval;
75         badblocks_list  bb_list = 0;
76         FILE            *f;
77         char            buf[1024];
78
79         read_bitmaps(fs);
80         
81         /*
82          * Always read in the current list of bad blocks.
83          */
84         retval = ext2fs_read_bb_inode(fs, &bb_list);
85         if (retval) {
86                 com_err("ext2fs_read_bb_inode", retval,
87                         "while reading the bad blocks inode");
88                 fatal_error(0);
89         }
90         
91         /*
92          * Now run the bad blocks program
93          */
94         sprintf(buf, "badblocks %s%s %ld", preen ? "" : "-s ",
95                 fs->device_name,
96                 fs->super->s_blocks_count);
97         if (verbose)
98                 printf("Running command: %s\n", buf);
99         f = popen(buf, "r");
100         if (!f) {
101                 com_err("popen", errno,
102                         "while trying to run %s", buf);
103                 fatal_error(0);
104         }
105         retval = ext2fs_read_bb_FILE(fs, f, &bb_list, invalid_block);
106         fclose (f);
107         if (retval) {
108                 com_err("ext2fs_read_bb_FILE", retval,
109                         "while processing list of bad blocks from program");
110                 fatal_error(0);
111         }
112         
113         /*
114          * Finally, update the bad blocks from the bad_block_map
115          */
116         retval = ext2fs_update_bb_inode(fs, bb_list);
117         if (retval) {
118                 com_err("ext2fs_update_bb_inode", retval,
119                         "while updating bad block inode");
120                 fatal_error(0);
121         }
122
123         badblocks_list_free(bb_list);
124         return;
125 }
126