read_bb.c 1.88 KB
Newer Older
Theodore Ts'o's avatar
Theodore Ts'o committed
1 2 3
/*
 * read_bb --- read the bad blocks inode
 *
Theodore Ts'o's avatar
Theodore Ts'o committed
4 5 6 7 8 9
 * Copyright (C) 1994 Theodore Ts'o.
 *
 * %Begin-Header%
 * This file may be redistributed under the terms of the GNU Public
 * License.
 * %End-Header%
Theodore Ts'o's avatar
Theodore Ts'o committed
10 11 12 13
 */

#include <stdio.h>
#include <string.h>
Theodore Ts'o's avatar
Theodore Ts'o committed
14
#if HAVE_UNISTD_H
Theodore Ts'o's avatar
Theodore Ts'o committed
15
#include <unistd.h>
Theodore Ts'o's avatar
Theodore Ts'o committed
16
#endif
Theodore Ts'o's avatar
Theodore Ts'o committed
17 18
#include <fcntl.h>
#include <time.h>
Theodore Ts'o's avatar
Theodore Ts'o committed
19
#if HAVE_SYS_STAT_H
Theodore Ts'o's avatar
Theodore Ts'o committed
20
#include <sys/stat.h>
Theodore Ts'o's avatar
Theodore Ts'o committed
21 22
#endif
#if HAVE_SYS_TYPES_H
Theodore Ts'o's avatar
Theodore Ts'o committed
23
#include <sys/types.h>
Theodore Ts'o's avatar
Theodore Ts'o committed
24
#endif
Theodore Ts'o's avatar
Theodore Ts'o committed
25

Theodore Ts'o's avatar
Theodore Ts'o committed
26
#include "ext2_fs.h"
Theodore Ts'o's avatar
Theodore Ts'o committed
27 28 29
#include "ext2fs.h"

struct read_bb_record {
Theodore Ts'o's avatar
Theodore Ts'o committed
30
	ext2_badblocks_list	bb_list;
Theodore Ts'o's avatar
Theodore Ts'o committed
31 32 33 34 35 36
	errcode_t	err;
};

/*
 * Helper function for ext2fs_read_bb_inode()
 */
Theodore Ts'o's avatar
Theodore Ts'o committed
37 38 39
#ifdef __TURBOC__
#pragma argsused
#endif
Theodore Ts'o's avatar
Theodore Ts'o committed
40
static int mark_bad_block(ext2_filsys fs, blk_t *block_nr,
Theodore Ts'o's avatar
Theodore Ts'o committed
41
			  e2_blkcnt_t blockcnt, blk_t ref_block,
Theodore Ts'o's avatar
Theodore Ts'o committed
42
			  int ref_offset, void *priv_data)
Theodore Ts'o's avatar
Theodore Ts'o committed
43
{
Theodore Ts'o's avatar
Theodore Ts'o committed
44
	struct read_bb_record *rb = (struct read_bb_record *) priv_data;
Theodore Ts'o's avatar
Theodore Ts'o committed
45 46 47 48
	
	if (blockcnt < 0)
		return 0;
	
Theodore Ts'o's avatar
Theodore Ts'o committed
49 50 51 52
	if ((*block_nr < fs->super->s_first_data_block) ||
	    (*block_nr >= fs->super->s_blocks_count))
		return 0;	/* Ignore illegal blocks */

Theodore Ts'o's avatar
Theodore Ts'o committed
53
	rb->err = ext2fs_badblocks_list_add(rb->bb_list, *block_nr);
Theodore Ts'o's avatar
Theodore Ts'o committed
54 55 56 57 58 59 60 61
	if (rb->err)
		return BLOCK_ABORT;
	return 0;
}

/*
 * Reads the current bad blocks from the bad blocks inode.
 */
Theodore Ts'o's avatar
Theodore Ts'o committed
62
errcode_t ext2fs_read_bb_inode(ext2_filsys fs, ext2_badblocks_list *bb_list)
Theodore Ts'o's avatar
Theodore Ts'o committed
63 64 65 66
{
	errcode_t	retval;
	struct read_bb_record rb;
	struct ext2_inode inode;
Theodore Ts'o's avatar
Theodore Ts'o committed
67
	blk_t	numblocks;
Theodore Ts'o's avatar
Theodore Ts'o committed
68

Theodore Ts'o's avatar
Theodore Ts'o committed
69 70
	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);

Theodore Ts'o's avatar
Theodore Ts'o committed
71 72 73 74
	if (!*bb_list) {
		retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode);
		if (retval)
			return retval;
Theodore Ts'o's avatar
Theodore Ts'o committed
75 76 77 78 79
		if (inode.i_blocks < 500)
			numblocks = (inode.i_blocks /
				     (fs->blocksize / 512)) + 20;
		else
			numblocks = 500;
Theodore Ts'o's avatar
Theodore Ts'o committed
80
		retval = ext2fs_badblocks_list_create(bb_list, numblocks);
Theodore Ts'o's avatar
Theodore Ts'o committed
81 82 83 84 85 86
		if (retval)
			return retval;
	}

	rb.bb_list = *bb_list;
	rb.err = 0;
Theodore Ts'o's avatar
Theodore Ts'o committed
87
	retval = ext2fs_block_iterate2(fs, EXT2_BAD_INO, 0, 0,
Theodore Ts'o's avatar
Theodore Ts'o committed
88 89 90 91 92 93 94 95
				      mark_bad_block, &rb);
	if (retval)
		return retval;

	return rb.err;
}