Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * read_bb --- read the bad blocks inode |
| 4 | * |
| 5 | * Copyright (C) 1994 Theodore Ts'o. |
| 6 | * |
| 7 | * %Begin-Header% |
| 8 | * This file may be redistributed under the terms of the GNU Public |
| 9 | * License. |
| 10 | * %End-Header% |
| 11 | */ |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <string.h> |
| 15 | #if HAVE_UNISTD_H |
| 16 | #include <unistd.h> |
| 17 | #endif |
| 18 | #include <fcntl.h> |
| 19 | #include <time.h> |
| 20 | #if HAVE_SYS_STAT_H |
| 21 | #include <sys/stat.h> |
| 22 | #endif |
| 23 | #if HAVE_SYS_TYPES_H |
| 24 | #include <sys/types.h> |
| 25 | #endif |
| 26 | |
| 27 | #include "ext2_fs.h" |
| 28 | #include "ext2fs.h" |
| 29 | |
| 30 | struct read_bb_record { |
| 31 | ext2_badblocks_list bb_list; |
| 32 | errcode_t err; |
| 33 | }; |
| 34 | |
| 35 | /* |
| 36 | * Helper function for ext2fs_read_bb_inode() |
| 37 | */ |
| 38 | #ifdef __TURBOC__ |
| 39 | # pragma argsused |
| 40 | #endif |
| 41 | static int mark_bad_block(ext2_filsys fs, blk_t *block_nr, |
| 42 | e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), |
| 43 | blk_t ref_block EXT2FS_ATTR((unused)), |
| 44 | int ref_offset EXT2FS_ATTR((unused)), |
| 45 | void *priv_data) |
| 46 | { |
| 47 | struct read_bb_record *rb = (struct read_bb_record *) priv_data; |
| 48 | |
| 49 | if (blockcnt < 0) |
| 50 | return 0; |
| 51 | |
| 52 | if ((*block_nr < fs->super->s_first_data_block) || |
| 53 | (*block_nr >= fs->super->s_blocks_count)) |
| 54 | return 0; /* Ignore illegal blocks */ |
| 55 | |
| 56 | rb->err = ext2fs_badblocks_list_add(rb->bb_list, *block_nr); |
| 57 | if (rb->err) |
| 58 | return BLOCK_ABORT; |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * Reads the current bad blocks from the bad blocks inode. |
| 64 | */ |
| 65 | errcode_t ext2fs_read_bb_inode(ext2_filsys fs, ext2_badblocks_list *bb_list) |
| 66 | { |
| 67 | errcode_t retval; |
| 68 | struct read_bb_record rb; |
| 69 | struct ext2_inode inode; |
| 70 | blk_t numblocks; |
| 71 | |
| 72 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 73 | |
| 74 | if (!*bb_list) { |
| 75 | retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode); |
| 76 | if (retval) |
| 77 | return retval; |
| 78 | if (inode.i_blocks < 500) |
| 79 | numblocks = (inode.i_blocks / |
| 80 | (fs->blocksize / 512)) + 20; |
| 81 | else |
| 82 | numblocks = 500; |
| 83 | retval = ext2fs_badblocks_list_create(bb_list, numblocks); |
| 84 | if (retval) |
| 85 | return retval; |
| 86 | } |
| 87 | |
| 88 | rb.bb_list = *bb_list; |
| 89 | rb.err = 0; |
| 90 | retval = ext2fs_block_iterate2(fs, EXT2_BAD_INO, 0, 0, |
| 91 | mark_bad_block, &rb); |
| 92 | if (retval) |
| 93 | return retval; |
| 94 | |
| 95 | return rb.err; |
| 96 | } |