Whamcloud - gitweb
ismounted.c (check_mntent_file, is_swap_device): Verify that the
[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 #include "ext2_fs.h"
27 #include "ext2fs.h"
28
29 struct read_bb_record {
30         ext2_badblocks_list     bb_list;
31         errcode_t       err;
32 };
33
34 /*
35  * Helper function for ext2fs_read_bb_inode()
36  */
37 #ifdef __TURBOC__
38 #pragma argsused
39 #endif
40 static int mark_bad_block(ext2_filsys fs, blk_t *block_nr,
41                           e2_blkcnt_t blockcnt, blk_t ref_block,
42                           int ref_offset, void *priv_data)
43 {
44         struct read_bb_record *rb = (struct read_bb_record *) priv_data;
45         
46         if (blockcnt < 0)
47                 return 0;
48         
49         if ((*block_nr < fs->super->s_first_data_block) ||
50             (*block_nr >= fs->super->s_blocks_count))
51                 return 0;       /* Ignore illegal blocks */
52
53         rb->err = ext2fs_badblocks_list_add(rb->bb_list, *block_nr);
54         if (rb->err)
55                 return BLOCK_ABORT;
56         return 0;
57 }
58
59 /*
60  * Reads the current bad blocks from the bad blocks inode.
61  */
62 errcode_t ext2fs_read_bb_inode(ext2_filsys fs, ext2_badblocks_list *bb_list)
63 {
64         errcode_t       retval;
65         struct read_bb_record rb;
66         struct ext2_inode inode;
67         blk_t   numblocks;
68
69         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
70
71         if (!*bb_list) {
72                 retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode);
73                 if (retval)
74                         return retval;
75                 if (inode.i_blocks < 500)
76                         numblocks = (inode.i_blocks /
77                                      (fs->blocksize / 512)) + 20;
78                 else
79                         numblocks = 500;
80                 retval = ext2fs_badblocks_list_create(bb_list, numblocks);
81                 if (retval)
82                         return retval;
83         }
84
85         rb.bb_list = *bb_list;
86         rb.err = 0;
87         retval = ext2fs_block_iterate2(fs, EXT2_BAD_INO, 0, 0,
88                                       mark_bad_block, &rb);
89         if (retval)
90                 return retval;
91
92         return rb.err;
93 }
94
95