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 Library
12 * General Public License, version 2.
28 #include <sys/types.h>
34 struct set_badblock_record {
35 ext2_badblocks_iterate bb_iter;
45 static int set_bad_block_proc(ext2_filsys fs, blk_t *block_nr,
47 blk_t ref_block, int ref_offset,
49 static int clear_bad_block_proc(ext2_filsys fs, blk_t *block_nr,
51 blk_t ref_block, int ref_offset,
55 * Given a bad blocks bitmap, update the bad blocks inode to reflect
58 errcode_t ext2fs_update_bb_inode(ext2_filsys fs, ext2_badblocks_list bb_list)
61 struct set_badblock_record rec;
62 struct ext2_inode inode;
64 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
67 return EXT2_ET_NO_BLOCK_BITMAP;
69 memset(&rec, 0, sizeof(rec));
70 rec.max_ind_blocks = 10;
71 retval = ext2fs_get_array(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 indirect 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 = fs->now ? fs->now : time(0);
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);
136 retval = ext2fs_write_inode(fs, EXT2_BAD_INO, &inode);
141 ext2fs_free_mem(&rec.ind_blocks);
142 ext2fs_free_mem(&rec.block_buf);
147 * Helper function for update_bb_inode()
149 * Clear the bad blocks in the bad block inode, while saving the
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)),
161 struct set_badblock_record *rec = (struct set_badblock_record *)
164 unsigned long old_size;
170 * If the block number is outrageous, clear it and ignore it.
172 if (*block_nr >= ext2fs_blocks_count(fs->super) ||
173 *block_nr < fs->super->s_first_data_block) {
175 return BLOCK_CHANGED;
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),
186 rec->max_ind_blocks -= 10;
191 rec->ind_blocks[rec->ind_blocks_size++] = *block_nr;
195 * Mark the block as unused, and update accounting information
197 ext2fs_block_alloc_stats2(fs, *block_nr, -1);
200 return BLOCK_CHANGED;
205 * Helper function for update_bb_inode()
207 * Set the block list in the bad block inode, using the supplied bitmap.
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)),
218 struct set_badblock_record *rec = (struct set_badblock_record *)
225 * Get the next bad block.
227 if (!ext2fs_badblocks_list_iterate(rec->bb_iter, &blk))
229 rec->bad_block_count++;
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
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))
244 retval = ext2fs_new_block(fs, 0, 0, &blk);
250 retval = io_channel_write_blk64(fs->io, blk, 1, rec->block_buf);
258 * Update block counts
260 ext2fs_block_alloc_stats2(fs, blk, +1);
263 return BLOCK_CHANGED;