Whamcloud - gitweb
ChangeLog, .del-ChangeLog~905e7699, version.h:
[tools/e2fsprogs.git] / lib / ext2fs / read_bb.c
1 /*
2  * read_bb --- read the bad blocks inode
3  *
4  * Copyright (C) 1994 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <fcntl.h>
18 #include <time.h>
19 #if HAVE_SYS_STAT_H
20 #include <sys/stat.h>
21 #endif
22 #if HAVE_SYS_TYPES_H
23 #include <sys/types.h>
24 #endif
25
26 #if EXT2_FLAT_INCLUDES
27 #include "ext2_fs.h"
28 #else
29 #include <linux/ext2_fs.h>
30 #endif
31
32 #include "ext2fs.h"
33
34 struct read_bb_record {
35         ext2_badblocks_list     bb_list;
36         errcode_t       err;
37 };
38
39 /*
40  * Helper function for ext2fs_read_bb_inode()
41  */
42 #ifdef __TURBOC__
43 #pragma argsused
44 #endif
45 static int mark_bad_block(ext2_filsys fs, blk_t *block_nr,
46                           e2_blkcnt_t blockcnt, blk_t ref_block,
47                           int ref_offset, void *priv_data)
48 {
49         struct read_bb_record *rb = (struct read_bb_record *) priv_data;
50         
51         if (blockcnt < 0)
52                 return 0;
53         
54         if ((*block_nr < fs->super->s_first_data_block) ||
55             (*block_nr >= fs->super->s_blocks_count))
56                 return 0;       /* Ignore illegal blocks */
57
58         rb->err = ext2fs_badblocks_list_add(rb->bb_list, *block_nr);
59         if (rb->err)
60                 return BLOCK_ABORT;
61         return 0;
62 }
63
64 /*
65  * Reads the current bad blocks from the bad blocks inode.
66  */
67 errcode_t ext2fs_read_bb_inode(ext2_filsys fs, ext2_badblocks_list *bb_list)
68 {
69         errcode_t       retval;
70         struct read_bb_record rb;
71         struct ext2_inode inode;
72         blk_t   numblocks;
73
74         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
75
76         if (!*bb_list) {
77                 retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode);
78                 if (retval)
79                         return retval;
80                 if (inode.i_blocks < 500)
81                         numblocks = (inode.i_blocks /
82                                      (fs->blocksize / 512)) + 20;
83                 else
84                         numblocks = 500;
85                 retval = ext2fs_badblocks_list_create(bb_list, numblocks);
86                 if (retval)
87                         return retval;
88         }
89
90         rb.bb_list = *bb_list;
91         rb.err = 0;
92         retval = ext2fs_block_iterate2(fs, EXT2_BAD_INO, 0, 0,
93                                       mark_bad_block, &rb);
94         if (retval)
95                 return retval;
96
97         return rb.err;
98 }
99
100