Whamcloud - gitweb
Many files:
[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.  This file may be redistributed
5  * under the terms of the GNU Public License.
6  */
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <fcntl.h>
13 #include <time.h>
14 #include <sys/stat.h>
15 #include <sys/types.h>
16
17 #include <linux/fs.h>
18 #include <linux/ext2_fs.h>
19
20 #include "ext2fs.h"
21
22 struct read_bb_record {
23         badblocks_list  bb_list;
24         errcode_t       err;
25 };
26
27 /*
28  * Helper function for ext2fs_read_bb_inode()
29  */
30 static int mark_bad_block(ext2_filsys fs, blk_t *block_nr,
31                              int blockcnt, void *private)
32 {
33         struct read_bb_record *rb = (struct read_bb_record *) private;
34         
35         if (blockcnt < 0)
36                 return 0;
37         
38         rb->err = badblocks_list_add(rb->bb_list, *block_nr);
39         if (rb->err)
40                 return BLOCK_ABORT;
41         return 0;
42 }
43
44 /*
45  * Reads the current bad blocks from the bad blocks inode.
46  */
47 errcode_t ext2fs_read_bb_inode(ext2_filsys fs, badblocks_list *bb_list)
48 {
49         errcode_t       retval;
50         struct read_bb_record rb;
51         struct ext2_inode inode;
52         int     numblocks;
53
54         if (!*bb_list) {
55                 retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode);
56                 if (retval)
57                         return retval;
58                 numblocks = (inode.i_blocks / (fs->blocksize / 512)) + 20;
59                 retval = badblocks_list_create(bb_list, numblocks);
60                 if (retval)
61                         return retval;
62         }
63
64         rb.bb_list = *bb_list;
65         rb.err = 0;
66         retval = ext2fs_block_iterate(fs, EXT2_BAD_INO, 0, 0,
67                                       mark_bad_block, &rb);
68         if (retval)
69                 return retval;
70
71         return rb.err;
72 }
73
74