blob: 3fabddd2dd0c4a461eea9e27fc6b1bb730fbfc8c [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * read_bb --- read the bad blocks inode
3 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * 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%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
12#include <stdio.h>
13#include <string.h>
14#include <unistd.h>
15#include <stdlib.h>
16#include <fcntl.h>
17#include <time.h>
18#include <sys/stat.h>
19#include <sys/types.h>
20
Theodore Ts'o3839e651997-04-26 13:21:57 +000021#include <linux/ext2_fs.h>
22
23#include "ext2fs.h"
24
25struct read_bb_record {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000026 ext2_badblocks_list bb_list;
Theodore Ts'o3839e651997-04-26 13:21:57 +000027 errcode_t err;
28};
29
30/*
31 * Helper function for ext2fs_read_bb_inode()
32 */
33static int mark_bad_block(ext2_filsys fs, blk_t *block_nr,
34 int blockcnt, void *private)
35{
36 struct read_bb_record *rb = (struct read_bb_record *) private;
37
38 if (blockcnt < 0)
39 return 0;
40
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000041 rb->err = ext2fs_badblocks_list_add(rb->bb_list, *block_nr);
Theodore Ts'o3839e651997-04-26 13:21:57 +000042 if (rb->err)
43 return BLOCK_ABORT;
44 return 0;
45}
46
47/*
48 * Reads the current bad blocks from the bad blocks inode.
49 */
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000050errcode_t ext2fs_read_bb_inode(ext2_filsys fs, ext2_badblocks_list *bb_list)
Theodore Ts'o3839e651997-04-26 13:21:57 +000051{
52 errcode_t retval;
53 struct read_bb_record rb;
54 struct ext2_inode inode;
55 int numblocks;
56
Theodore Ts'of3db3561997-04-26 13:34:30 +000057 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
58
Theodore Ts'o3839e651997-04-26 13:21:57 +000059 if (!*bb_list) {
60 retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode);
61 if (retval)
62 return retval;
63 numblocks = (inode.i_blocks / (fs->blocksize / 512)) + 20;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000064 retval = ext2fs_badblocks_list_create(bb_list, numblocks);
Theodore Ts'o3839e651997-04-26 13:21:57 +000065 if (retval)
66 return retval;
67 }
68
69 rb.bb_list = *bb_list;
70 rb.err = 0;
71 retval = ext2fs_block_iterate(fs, EXT2_BAD_INO, 0, 0,
72 mark_bad_block, &rb);
73 if (retval)
74 return retval;
75
76 return rb.err;
77}
78
79