read_bb.c revision 3cb6c5021d722e17b7105d1bc090880671f6fc6d
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 <stdlib.h>
18#include <fcntl.h>
19#include <time.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <linux/ext2_fs.h>
24
25#include "ext2fs.h"
26
27struct read_bb_record {
28	ext2_badblocks_list	bb_list;
29	errcode_t	err;
30};
31
32/*
33 * Helper function for ext2fs_read_bb_inode()
34 */
35#ifdef __TURBOC__
36#pragma argsused
37#endif
38static int mark_bad_block(ext2_filsys fs, blk_t *block_nr,
39			     int blockcnt, void *private)
40{
41	struct read_bb_record *rb = (struct read_bb_record *) private;
42
43	if (blockcnt < 0)
44		return 0;
45
46	rb->err = ext2fs_badblocks_list_add(rb->bb_list, *block_nr);
47	if (rb->err)
48		return BLOCK_ABORT;
49	return 0;
50}
51
52/*
53 * Reads the current bad blocks from the bad blocks inode.
54 */
55errcode_t ext2fs_read_bb_inode(ext2_filsys fs, ext2_badblocks_list *bb_list)
56{
57	errcode_t	retval;
58	struct read_bb_record rb;
59	struct ext2_inode inode;
60	blk_t	numblocks;
61
62	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
63
64	if (!*bb_list) {
65		retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode);
66		if (retval)
67			return retval;
68		numblocks = (inode.i_blocks / (fs->blocksize / 512)) + 20;
69		retval = ext2fs_badblocks_list_create(bb_list, numblocks);
70		if (retval)
71			return retval;
72	}
73
74	rb.bb_list = *bb_list;
75	rb.err = 0;
76	retval = ext2fs_block_iterate(fs, EXT2_BAD_INO, 0, 0,
77				      mark_bad_block, &rb);
78	if (retval)
79		return retval;
80
81	return rb.err;
82}
83
84
85