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
28 #include <sys/types.h>
34 #include <linux/ext2_fs.h>
38 struct set_badblock_record {
39 ext2_badblocks_iterate bb_iter;
49 static int set_bad_block_proc(ext2_filsys fs, blk_t *block_nr, int blockcnt,
50 blk_t ref_block, int ref_offset, void *private);
51 static int clear_bad_block_proc(ext2_filsys fs, blk_t *block_nr, int blockcnt,
52 blk_t ref_block, int ref_offset,
56 * Given a bad blocks bitmap, update the bad blocks inode to reflect
59 errcode_t ext2fs_update_bb_inode(ext2_filsys fs, ext2_badblocks_list bb_list)
62 struct set_badblock_record rec;
63 struct ext2_inode inode;
66 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
69 return EXT2_ET_NO_BLOCK_BITMAP;
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));
77 memset(rec.ind_blocks, 0, rec.max_ind_blocks * sizeof(blk_t));
78 rec.block_buf = malloc(fs->blocksize);
83 memset(rec.block_buf, 0, fs->blocksize);
87 * First clear the old bad blocks (while saving the indirect blocks)
89 retval = ext2fs_block_iterate2(fs, EXT2_BAD_INO,
90 BLOCK_FLAG_DEPTH_TRAVERSE, 0,
91 clear_bad_block_proc, &rec);
100 * Now set the bad blocks!
102 * First, mark the bad blocks as used. This prevents a bad
103 * block from being used as an indirecto block for the bad
107 retval = ext2fs_badblocks_list_iterate_begin(bb_list,
111 while (ext2fs_badblocks_list_iterate(rec.bb_iter, &blk)) {
112 ext2fs_mark_block_bitmap(fs->block_map, blk);
114 ext2fs_badblocks_list_iterate_end(rec.bb_iter);
115 ext2fs_mark_bb_dirty(fs);
117 retval = ext2fs_badblocks_list_iterate_begin(bb_list,
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);
134 * Update the bad block inode's mod time and block count
137 retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode);
141 inode.i_atime = inode.i_mtime = time(0);
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;
147 retval = ext2fs_write_inode(fs, EXT2_BAD_INO, &inode);
152 free(rec.ind_blocks);
158 * Helper function for update_bb_inode()
160 * Clear the bad blocks in the bad block inode, while saving the
166 static int clear_bad_block_proc(ext2_filsys fs, blk_t *block_nr, int blockcnt,
167 blk_t ref_block, int ref_offset, void *private)
169 struct set_badblock_record *rec = (struct set_badblock_record *)
177 * If the block number is outrageous, clear it and ignore it.
179 if (*block_nr >= fs->super->s_blocks_count ||
180 *block_nr < fs->super->s_first_data_block) {
182 return BLOCK_CHANGED;
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 *
191 if (!rec->ind_blocks) {
196 rec->ind_blocks[rec->ind_blocks_size++] = *block_nr;
200 * Mark the block as unused, and update accounting information
202 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
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);
210 return BLOCK_CHANGED;
215 * Helper function for update_bb_inode()
217 * Set the block list in the bad block inode, using the supplied bitmap.
222 static int set_bad_block_proc(ext2_filsys fs, blk_t *block_nr,
223 int blockcnt, blk_t ref_block,
224 int ref_offset, void *private)
226 struct set_badblock_record *rec = (struct set_badblock_record *)
234 * Get the next bad block.
236 if (!ext2fs_badblocks_list_iterate(rec->bb_iter, &blk))
238 rec->bad_block_count++;
241 * An indirect block; fetch a block from the
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
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))
253 retval = ext2fs_new_block(fs, 0, 0, &blk);
259 retval = io_channel_write_blk(fs->io, blk, 1, rec->block_buf);
264 ext2fs_mark_block_bitmap(fs->block_map, blk);
265 ext2fs_mark_bb_dirty(fs);
269 * Update block counts
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);
277 return BLOCK_CHANGED;