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 Theodore Ts'o. This file may be redistributed
9 * under the terms of the GNU Public License.
19 #include <sys/types.h>
22 #include <linux/ext2_fs.h>
26 struct set_badblock_record {
27 badblocks_iterate bb_iter;
37 static int set_bad_block_proc(ext2_filsys fs, blk_t *block_nr, int blockcnt,
39 static int clear_bad_block_proc(ext2_filsys fs, blk_t *block_nr, int blockcnt,
43 * Given a bad blocks bitmap, update the bad blocks inode to reflect
46 errcode_t ext2fs_update_bb_inode(ext2_filsys fs, badblocks_list bb_list)
49 struct set_badblock_record rec;
50 struct ext2_inode inode;
53 return EXT2_ET_NO_BLOCK_BITMAP;
55 rec.bad_block_count = 0;
56 rec.ind_blocks_size = rec.ind_blocks_ptr = 0;
57 rec.max_ind_blocks = 10;
58 rec.ind_blocks = malloc(rec.max_ind_blocks * sizeof(blk_t));
61 memset(rec.ind_blocks, 0, rec.max_ind_blocks * sizeof(blk_t));
62 rec.block_buf = malloc(fs->blocksize);
67 memset(rec.block_buf, 0, fs->blocksize);
71 * First clear the old bad blocks (while saving the indirect blocks)
73 retval = ext2fs_block_iterate(fs, EXT2_BAD_INO, 0, 0,
74 clear_bad_block_proc, &rec);
83 * Now set the bad blocks!
86 retval = badblocks_list_iterate_begin(bb_list, &rec.bb_iter);
89 retval = ext2fs_block_iterate(fs, EXT2_BAD_INO,
91 set_bad_block_proc, &rec);
92 badblocks_list_iterate_end(rec.bb_iter);
102 * Update the bad block inode's mod time and block count
105 retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode);
109 inode.i_atime = inode.i_mtime = time(0);
111 inode.i_ctime = time(0);
112 inode.i_blocks = rec.bad_block_count * (fs->blocksize / 512);
113 inode.i_size = rec.bad_block_count * fs->blocksize;
115 retval = ext2fs_write_inode(fs, EXT2_BAD_INO, &inode);
120 free(rec.ind_blocks);
126 * Helper function for update_bb_inode()
128 * Clear the bad blocks in the bad block inode, while saving the
131 static int clear_bad_block_proc(ext2_filsys fs, blk_t *block_nr, int blockcnt,
134 struct set_badblock_record *rec = (struct set_badblock_record *)
142 if (rec->ind_blocks_size >= rec->max_ind_blocks) {
143 rec->max_ind_blocks += 10;
144 rec->ind_blocks = realloc(rec->ind_blocks,
145 rec->max_ind_blocks *
147 if (!rec->ind_blocks) {
152 rec->ind_blocks[rec->ind_blocks_size++] = *block_nr;
156 * Mark the block as unused, and update accounting information
158 ext2fs_unmark_block_bitmap(fs, fs->block_map, *block_nr);
159 ext2fs_mark_bb_dirty(fs);
160 group = ext2fs_group_of_blk(fs, *block_nr);
161 fs->group_desc[group].bg_free_blocks_count++;
162 fs->super->s_free_blocks_count++;
163 ext2fs_mark_super_dirty(fs);
166 return BLOCK_CHANGED;
171 * Helper function for update_bb_inode()
173 * Set the block list in the bad block inode, using the supplied bitmap.
175 static int set_bad_block_proc(ext2_filsys fs, blk_t *block_nr,
176 int blockcnt, void *private)
178 struct set_badblock_record *rec = (struct set_badblock_record *)
186 * Get the next bad block.
188 if (!badblocks_list_iterate(rec->bb_iter, &blk))
190 rec->bad_block_count++;
191 } else if (rec->ind_blocks_ptr < rec->ind_blocks_size)
193 * An indirect block; fetch a block from the
194 * previously used indirect block list.
196 blk = rec->ind_blocks[rec->ind_blocks_ptr++];
199 * An indirect block, and we're out of reserved
200 * indirect blocks. Allocate a new one.
202 retval = ext2fs_new_block(fs, 0, 0, &blk);
207 retval = io_channel_write_blk(fs->io, blk, 1, rec->block_buf);
215 * Mark the block as used, and update block counts
217 ext2fs_mark_block_bitmap(fs, fs->block_map, blk);
218 ext2fs_mark_bb_dirty(fs);
219 group = ext2fs_group_of_blk(fs, blk);
220 fs->group_desc[group].bg_free_blocks_count--;
221 fs->super->s_free_blocks_count--;
222 ext2fs_mark_super_dirty(fs);
225 return BLOCK_CHANGED;