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