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> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 14 | #if HAVE_UNISTD_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 16 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 17 | #include <fcntl.h> |
| 18 | #include <time.h> |
Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 19 | #if HAVE_SYS_STAT_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 20 | #include <sys/stat.h> |
Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 21 | #endif |
| 22 | #if HAVE_SYS_TYPES_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 23 | #include <sys/types.h> |
Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 24 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 25 | |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 26 | #include "ext2_fs.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 27 | #include "ext2fs.h" |
| 28 | |
| 29 | struct read_bb_record { |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 30 | ext2_badblocks_list bb_list; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 31 | errcode_t err; |
| 32 | }; |
| 33 | |
| 34 | /* |
| 35 | * Helper function for ext2fs_read_bb_inode() |
| 36 | */ |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 37 | #ifdef __TURBOC__ |
Theodore Ts'o | 48e6e81 | 2003-07-06 00:36:48 -0400 | [diff] [blame] | 38 | #pragma argsused |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 39 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 40 | static int mark_bad_block(ext2_filsys fs, blk_t *block_nr, |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 41 | e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), |
| 42 | blk_t ref_block EXT2FS_ATTR((unused)), |
| 43 | int ref_offset EXT2FS_ATTR((unused)), |
| 44 | void *priv_data) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 45 | { |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 46 | struct read_bb_record *rb = (struct read_bb_record *) priv_data; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 47 | |
| 48 | if (blockcnt < 0) |
| 49 | return 0; |
| 50 | |
Theodore Ts'o | 4faba5b | 1998-06-16 05:23:41 +0000 | [diff] [blame] | 51 | if ((*block_nr < fs->super->s_first_data_block) || |
| 52 | (*block_nr >= fs->super->s_blocks_count)) |
| 53 | return 0; /* Ignore illegal blocks */ |
| 54 | |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 55 | rb->err = ext2fs_badblocks_list_add(rb->bb_list, *block_nr); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 56 | if (rb->err) |
| 57 | return BLOCK_ABORT; |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Reads the current bad blocks from the bad blocks inode. |
| 63 | */ |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 64 | 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] | 65 | { |
| 66 | errcode_t retval; |
| 67 | struct read_bb_record rb; |
| 68 | struct ext2_inode inode; |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 69 | blk_t numblocks; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 70 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 71 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 72 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 73 | if (!*bb_list) { |
| 74 | retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode); |
| 75 | if (retval) |
| 76 | return retval; |
Theodore Ts'o | 1ca1059 | 2008-04-09 11:39:11 -0400 | [diff] [blame^] | 77 | numblocks = inode.i_blocks; |
| 78 | if (!((fs->super->s_feature_ro_compat & |
| 79 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE) && |
| 80 | (inode.i_flags & EXT4_HUGE_FILE_FL))) |
| 81 | numblocks = numblocks / (fs->blocksize / 512); |
| 82 | numblocks += 20; |
| 83 | if (numblocks < 50) |
| 84 | numblocks = 50; |
| 85 | if (numblocks > 50000) |
Theodore Ts'o | 4faba5b | 1998-06-16 05:23:41 +0000 | [diff] [blame] | 86 | numblocks = 500; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 87 | retval = ext2fs_badblocks_list_create(bb_list, numblocks); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 88 | if (retval) |
| 89 | return retval; |
| 90 | } |
| 91 | |
| 92 | rb.bb_list = *bb_list; |
| 93 | rb.err = 0; |
Theodore Ts'o | 357d186 | 2008-02-02 21:26:54 -0500 | [diff] [blame] | 94 | retval = ext2fs_block_iterate2(fs, EXT2_BAD_INO, BLOCK_FLAG_READ_ONLY, |
| 95 | 0, mark_bad_block, &rb); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 96 | if (retval) |
| 97 | return retval; |
| 98 | |
| 99 | return rb.err; |
| 100 | } |
| 101 | |
| 102 | |