Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * read_bb --- read the bad blocks inode |
| 3 | * |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame^] | 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% |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 10 | */ |
| 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'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 21 | #include <linux/ext2_fs.h> |
| 22 | |
| 23 | #include "ext2fs.h" |
| 24 | |
| 25 | struct read_bb_record { |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame^] | 26 | ext2_badblocks_list bb_list; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 27 | errcode_t err; |
| 28 | }; |
| 29 | |
| 30 | /* |
| 31 | * Helper function for ext2fs_read_bb_inode() |
| 32 | */ |
| 33 | static 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'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame^] | 41 | rb->err = ext2fs_badblocks_list_add(rb->bb_list, *block_nr); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 42 | 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'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame^] | 50 | errcode_t ext2fs_read_bb_inode(ext2_filsys fs, ext2_badblocks_list *bb_list) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 51 | { |
| 52 | errcode_t retval; |
| 53 | struct read_bb_record rb; |
| 54 | struct ext2_inode inode; |
| 55 | int numblocks; |
| 56 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 57 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 58 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 59 | 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'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame^] | 64 | retval = ext2fs_badblocks_list_create(bb_list, numblocks); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 65 | 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 | |