| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * bb_inode.c --- routines to update the bad block inode. |
| 3 | * |
| 4 | * WARNING: This routine modifies a lot of state in the filesystem; if |
| 5 | * this routine returns an error, the bad block inode may be in an |
| 6 | * inconsistent state. |
| 7 | * |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 8 | * Copyright (C) 1994, 1995 Theodore Ts'o. |
| 9 | * |
| 10 | * %Begin-Header% |
| 11 | * This file may be redistributed under the terms of the GNU Public |
| 12 | * License. |
| 13 | * %End-Header% |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <string.h> |
| Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 18 | #if HAVE_UNISTD_H |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 19 | #include <unistd.h> |
| Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 20 | #endif |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <time.h> |
| Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame^] | 24 | #if HAVE_SYS_STAT_H |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 25 | #include <sys/stat.h> |
| Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame^] | 26 | #endif |
| 27 | #if HAVE_SYS_TYPES_H |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 28 | #include <sys/types.h> |
| Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame^] | 29 | #endif |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 30 | #if HAVE_ERRNO_H |
| 31 | #include <errno.h> |
| 32 | #endif |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 33 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 34 | #include <linux/ext2_fs.h> |
| 35 | |
| 36 | #include "ext2fs.h" |
| 37 | |
| 38 | struct set_badblock_record { |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 39 | ext2_badblocks_iterate bb_iter; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 40 | int bad_block_count; |
| 41 | blk_t *ind_blocks; |
| 42 | int max_ind_blocks; |
| 43 | int ind_blocks_size; |
| 44 | int ind_blocks_ptr; |
| 45 | char *block_buf; |
| 46 | errcode_t err; |
| 47 | }; |
| 48 | |
| 49 | static int set_bad_block_proc(ext2_filsys fs, blk_t *block_nr, int blockcnt, |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 50 | blk_t ref_block, int ref_offset, void *private); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 51 | static int clear_bad_block_proc(ext2_filsys fs, blk_t *block_nr, int blockcnt, |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 52 | blk_t ref_block, int ref_offset, |
| 53 | void *private); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * Given a bad blocks bitmap, update the bad blocks inode to reflect |
| 57 | * the map. |
| 58 | */ |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 59 | errcode_t ext2fs_update_bb_inode(ext2_filsys fs, ext2_badblocks_list bb_list) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 60 | { |
| 61 | errcode_t retval; |
| 62 | struct set_badblock_record rec; |
| 63 | struct ext2_inode inode; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 64 | blk_t blk; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 65 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 66 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 67 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 68 | if (!fs->block_map) |
| 69 | return EXT2_ET_NO_BLOCK_BITMAP; |
| 70 | |
| 71 | rec.bad_block_count = 0; |
| 72 | rec.ind_blocks_size = rec.ind_blocks_ptr = 0; |
| 73 | rec.max_ind_blocks = 10; |
| 74 | rec.ind_blocks = malloc(rec.max_ind_blocks * sizeof(blk_t)); |
| 75 | if (!rec.ind_blocks) |
| 76 | return ENOMEM; |
| 77 | memset(rec.ind_blocks, 0, rec.max_ind_blocks * sizeof(blk_t)); |
| 78 | rec.block_buf = malloc(fs->blocksize); |
| 79 | if (!rec.block_buf) { |
| 80 | retval = ENOMEM; |
| 81 | goto cleanup; |
| 82 | } |
| 83 | memset(rec.block_buf, 0, fs->blocksize); |
| 84 | rec.err = 0; |
| 85 | |
| 86 | /* |
| 87 | * First clear the old bad blocks (while saving the indirect blocks) |
| 88 | */ |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 89 | retval = ext2fs_block_iterate2(fs, EXT2_BAD_INO, |
| 90 | BLOCK_FLAG_DEPTH_TRAVERSE, 0, |
| 91 | clear_bad_block_proc, &rec); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 92 | if (retval) |
| 93 | goto cleanup; |
| 94 | if (rec.err) { |
| 95 | retval = rec.err; |
| 96 | goto cleanup; |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Now set the bad blocks! |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 101 | * |
| 102 | * First, mark the bad blocks as used. This prevents a bad |
| 103 | * block from being used as an indirecto block for the bad |
| 104 | * block inode (!). |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 105 | */ |
| 106 | if (bb_list) { |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 107 | retval = ext2fs_badblocks_list_iterate_begin(bb_list, |
| 108 | &rec.bb_iter); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 109 | if (retval) |
| 110 | goto cleanup; |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 111 | while (ext2fs_badblocks_list_iterate(rec.bb_iter, &blk)) { |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 112 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
| 113 | } |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 114 | ext2fs_badblocks_list_iterate_end(rec.bb_iter); |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 115 | ext2fs_mark_bb_dirty(fs); |
| 116 | |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 117 | retval = ext2fs_badblocks_list_iterate_begin(bb_list, |
| 118 | &rec.bb_iter); |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 119 | if (retval) |
| 120 | goto cleanup; |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 121 | retval = ext2fs_block_iterate2(fs, EXT2_BAD_INO, |
| 122 | BLOCK_FLAG_APPEND, 0, |
| 123 | set_bad_block_proc, &rec); |
| 124 | ext2fs_badblocks_list_iterate_end(rec.bb_iter); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 125 | if (retval) |
| 126 | goto cleanup; |
| 127 | if (rec.err) { |
| 128 | retval = rec.err; |
| 129 | goto cleanup; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Update the bad block inode's mod time and block count |
| 135 | * field. |
| 136 | */ |
| 137 | retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode); |
| 138 | if (retval) |
| 139 | goto cleanup; |
| 140 | |
| 141 | inode.i_atime = inode.i_mtime = time(0); |
| 142 | if (!inode.i_ctime) |
| 143 | inode.i_ctime = time(0); |
| 144 | inode.i_blocks = rec.bad_block_count * (fs->blocksize / 512); |
| 145 | inode.i_size = rec.bad_block_count * fs->blocksize; |
| 146 | |
| 147 | retval = ext2fs_write_inode(fs, EXT2_BAD_INO, &inode); |
| 148 | if (retval) |
| 149 | goto cleanup; |
| 150 | |
| 151 | cleanup: |
| 152 | free(rec.ind_blocks); |
| 153 | free(rec.block_buf); |
| 154 | return retval; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Helper function for update_bb_inode() |
| 159 | * |
| 160 | * Clear the bad blocks in the bad block inode, while saving the |
| 161 | * indirect blocks. |
| 162 | */ |
| Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 163 | #ifdef __TURBOC__ |
| 164 | #pragma argsused |
| 165 | #endif |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 166 | static int clear_bad_block_proc(ext2_filsys fs, blk_t *block_nr, int blockcnt, |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 167 | blk_t ref_block, int ref_offset, void *private) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 168 | { |
| 169 | struct set_badblock_record *rec = (struct set_badblock_record *) |
| 170 | private; |
| 171 | int group; |
| 172 | |
| 173 | if (!*block_nr) |
| 174 | return 0; |
| 175 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 176 | /* |
| 177 | * If the block number is outrageous, clear it and ignore it. |
| 178 | */ |
| 179 | if (*block_nr >= fs->super->s_blocks_count || |
| 180 | *block_nr < fs->super->s_first_data_block) { |
| 181 | *block_nr = 0; |
| 182 | return BLOCK_CHANGED; |
| 183 | } |
| 184 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 185 | if (blockcnt < 0) { |
| 186 | if (rec->ind_blocks_size >= rec->max_ind_blocks) { |
| 187 | rec->max_ind_blocks += 10; |
| 188 | rec->ind_blocks = realloc(rec->ind_blocks, |
| 189 | rec->max_ind_blocks * |
| 190 | sizeof(blk_t)); |
| 191 | if (!rec->ind_blocks) { |
| 192 | rec->err = ENOMEM; |
| 193 | return BLOCK_ABORT; |
| 194 | } |
| 195 | } |
| 196 | rec->ind_blocks[rec->ind_blocks_size++] = *block_nr; |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Mark the block as unused, and update accounting information |
| 201 | */ |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 202 | ext2fs_unmark_block_bitmap(fs->block_map, *block_nr); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 203 | ext2fs_mark_bb_dirty(fs); |
| 204 | group = ext2fs_group_of_blk(fs, *block_nr); |
| 205 | fs->group_desc[group].bg_free_blocks_count++; |
| 206 | fs->super->s_free_blocks_count++; |
| 207 | ext2fs_mark_super_dirty(fs); |
| 208 | |
| 209 | *block_nr = 0; |
| 210 | return BLOCK_CHANGED; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | /* |
| 215 | * Helper function for update_bb_inode() |
| 216 | * |
| 217 | * Set the block list in the bad block inode, using the supplied bitmap. |
| 218 | */ |
| Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 219 | #ifdef __TURBOC__ |
| 220 | #pragma argsused |
| 221 | #endif |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 222 | static int set_bad_block_proc(ext2_filsys fs, blk_t *block_nr, |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 223 | int blockcnt, blk_t ref_block, |
| 224 | int ref_offset, void *private) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 225 | { |
| 226 | struct set_badblock_record *rec = (struct set_badblock_record *) |
| 227 | private; |
| 228 | errcode_t retval; |
| 229 | blk_t blk; |
| 230 | int group; |
| 231 | |
| 232 | if (blockcnt >= 0) { |
| 233 | /* |
| 234 | * Get the next bad block. |
| 235 | */ |
| Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 236 | if (!ext2fs_badblocks_list_iterate(rec->bb_iter, &blk)) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 237 | return BLOCK_ABORT; |
| 238 | rec->bad_block_count++; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 239 | } else { |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 240 | /* |
| 241 | * An indirect block; fetch a block from the |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 242 | * previously used indirect block list. The block |
| 243 | * most be not marked as used; if so, get another one. |
| 244 | * If we run out of reserved indirect blocks, allocate |
| 245 | * a new one. |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 246 | */ |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 247 | retry: |
| 248 | if (rec->ind_blocks_ptr < rec->ind_blocks_size) { |
| 249 | blk = rec->ind_blocks[rec->ind_blocks_ptr++]; |
| 250 | if (ext2fs_test_block_bitmap(fs->block_map, blk)) |
| 251 | goto retry; |
| 252 | } else { |
| 253 | retval = ext2fs_new_block(fs, 0, 0, &blk); |
| 254 | if (retval) { |
| 255 | rec->err = retval; |
| 256 | return BLOCK_ABORT; |
| 257 | } |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 258 | } |
| 259 | retval = io_channel_write_blk(fs->io, blk, 1, rec->block_buf); |
| 260 | if (retval) { |
| 261 | rec->err = retval; |
| 262 | return BLOCK_ABORT; |
| 263 | } |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 264 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
| 265 | ext2fs_mark_bb_dirty(fs); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 269 | * Update block counts |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 270 | */ |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 271 | group = ext2fs_group_of_blk(fs, blk); |
| 272 | fs->group_desc[group].bg_free_blocks_count--; |
| 273 | fs->super->s_free_blocks_count--; |
| 274 | ext2fs_mark_super_dirty(fs); |
| 275 | |
| 276 | *block_nr = blk; |
| 277 | return BLOCK_CHANGED; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |