blob: 7612e674f0cb740f9ab89274da45205c15bba153 [file] [log] [blame]
Mike Frysinger1fd98e02005-05-09 22:10:42 +00001/*
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
29struct 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__
Mike Frysingerf8855132006-03-28 02:35:56 +000038# pragma argsused
Mike Frysinger1fd98e02005-05-09 22:10:42 +000039#endif
40static int mark_bad_block(ext2_filsys fs, blk_t *block_nr,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000041 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
Mike Frysinger1fd98e02005-05-09 22:10:42 +000042 blk_t ref_block EXT2FS_ATTR((unused)),
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000043 int ref_offset EXT2FS_ATTR((unused)),
Mike Frysinger1fd98e02005-05-09 22:10:42 +000044 void *priv_data)
45{
46 struct read_bb_record *rb = (struct read_bb_record *) priv_data;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000047
Mike Frysinger1fd98e02005-05-09 22:10:42 +000048 if (blockcnt < 0)
49 return 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000050
Mike Frysinger1fd98e02005-05-09 22:10:42 +000051 if ((*block_nr < fs->super->s_first_data_block) ||
52 (*block_nr >= fs->super->s_blocks_count))
53 return 0; /* Ignore illegal blocks */
54
55 rb->err = ext2fs_badblocks_list_add(rb->bb_list, *block_nr);
56 if (rb->err)
57 return BLOCK_ABORT;
58 return 0;
59}
60
61/*
62 * Reads the current bad blocks from the bad blocks inode.
63 */
64errcode_t ext2fs_read_bb_inode(ext2_filsys fs, ext2_badblocks_list *bb_list)
65{
66 errcode_t retval;
67 struct read_bb_record rb;
68 struct ext2_inode inode;
69 blk_t numblocks;
70
71 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
72
73 if (!*bb_list) {
74 retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode);
75 if (retval)
76 return retval;
77 if (inode.i_blocks < 500)
78 numblocks = (inode.i_blocks /
79 (fs->blocksize / 512)) + 20;
80 else
81 numblocks = 500;
82 retval = ext2fs_badblocks_list_create(bb_list, numblocks);
83 if (retval)
84 return retval;
85 }
86
87 rb.bb_list = *bb_list;
88 rb.err = 0;
89 retval = ext2fs_block_iterate2(fs, EXT2_BAD_INO, 0, 0,
90 mark_bad_block, &rb);
91 if (retval)
92 return retval;
93
94 return rb.err;
95}
96
97