2 * bb_inode.c --- routines to update the bad block inode.
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
8 * Copyright (C) 1994, 1995 Theodore Ts'o.
11 * This file may be redistributed under the terms of the GNU Public
27 #include <sys/types.h>
33 struct set_badblock_record {
34 ext2_badblocks_iterate bb_iter;
44 static int set_bad_block_proc(ext2_filsys fs, blk_t *block_nr,
46 blk_t ref_block, int ref_offset,
48 static int clear_bad_block_proc(ext2_filsys fs, blk_t *block_nr,
50 blk_t ref_block, int ref_offset,
54 * Given a bad blocks bitmap, update the bad blocks inode to reflect
57 errcode_t ext2fs_update_bb_inode(ext2_filsys fs, ext2_badblocks_list bb_list)
60 struct set_badblock_record rec;
61 struct ext2_inode inode;
63 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
66 return EXT2_ET_NO_BLOCK_BITMAP;
68 rec.bad_block_count = 0;
69 rec.ind_blocks_size = rec.ind_blocks_ptr = 0;
70 rec.max_ind_blocks = 10;
71 retval = ext2fs_get_mem(rec.max_ind_blocks * sizeof(blk_t),
75 memset(rec.ind_blocks, 0, rec.max_ind_blocks * sizeof(blk_t));
76 retval = ext2fs_get_mem(fs->blocksize, &rec.block_buf);
79 memset(rec.block_buf, 0, fs->blocksize);
83 * First clear the old bad blocks (while saving the indirect blocks)
85 retval = ext2fs_block_iterate2(fs, EXT2_BAD_INO,
86 BLOCK_FLAG_DEPTH_TRAVERSE, 0,
87 clear_bad_block_proc, &rec);
96 * Now set the bad blocks!
98 * First, mark the bad blocks as used. This prevents a bad
99 * block from being used as an indirecto block for the bad
103 retval = ext2fs_badblocks_list_iterate_begin(bb_list,
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);
120 * Update the bad block inode's mod time and block count
123 retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode);
127 inode.i_atime = inode.i_mtime = time(0);
129 inode.i_ctime = time(0);
130 inode.i_blocks = rec.bad_block_count * (fs->blocksize / 512);
131 inode.i_size = rec.bad_block_count * fs->blocksize;
133 retval = ext2fs_write_inode(fs, EXT2_BAD_INO, &inode);
138 ext2fs_free_mem(&rec.ind_blocks);
139 ext2fs_free_mem(&rec.block_buf);
144 * Helper function for update_bb_inode()
146 * Clear the bad blocks in the bad block inode, while saving the
152 static int clear_bad_block_proc(ext2_filsys fs, blk_t *block_nr,
153 e2_blkcnt_t blockcnt,
154 blk_t ref_block, int ref_offset,
157 struct set_badblock_record *rec = (struct set_badblock_record *)
160 unsigned long old_size;
166 * If the block number is outrageous, clear it and ignore it.
168 if (*block_nr >= fs->super->s_blocks_count ||
169 *block_nr < fs->super->s_first_data_block) {
171 return BLOCK_CHANGED;
175 if (rec->ind_blocks_size >= rec->max_ind_blocks) {
176 old_size = rec->max_ind_blocks * sizeof(blk_t);
177 rec->max_ind_blocks += 10;
178 retval = ext2fs_resize_mem(old_size,
179 rec->max_ind_blocks * sizeof(blk_t),
182 rec->max_ind_blocks -= 10;
187 rec->ind_blocks[rec->ind_blocks_size++] = *block_nr;
191 * Mark the block as unused, and update accounting information
193 ext2fs_block_alloc_stats(fs, *block_nr, -1);
196 return BLOCK_CHANGED;
201 * Helper function for update_bb_inode()
203 * Set the block list in the bad block inode, using the supplied bitmap.
208 static int set_bad_block_proc(ext2_filsys fs, blk_t *block_nr,
209 e2_blkcnt_t blockcnt, blk_t ref_block,
210 int ref_offset, void *priv_data)
212 struct set_badblock_record *rec = (struct set_badblock_record *)
219 * Get the next bad block.
221 if (!ext2fs_badblocks_list_iterate(rec->bb_iter, &blk))
223 rec->bad_block_count++;
226 * An indirect block; fetch a block from the
227 * previously used indirect block list. The block
228 * most be not marked as used; if so, get another one.
229 * If we run out of reserved indirect blocks, allocate
233 if (rec->ind_blocks_ptr < rec->ind_blocks_size) {
234 blk = rec->ind_blocks[rec->ind_blocks_ptr++];
235 if (ext2fs_test_block_bitmap(fs->block_map, blk))
238 retval = ext2fs_new_block(fs, 0, 0, &blk);
244 retval = io_channel_write_blk(fs->io, blk, 1, rec->block_buf);
252 * Update block counts
254 ext2fs_block_alloc_stats(fs, blk, +1);
257 return BLOCK_CHANGED;