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