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
;
65 EXT2_CHECK_MAGIC(fs
, EXT2_ET_MAGIC_EXT2FS_FILSYS
);
68 return EXT2_ET_NO_BLOCK_BITMAP
;
70 memset(&rec
, 0, sizeof(rec
));
71 rec
.max_ind_blocks
= 10;
72 retval
= ext2fs_get_array(rec
.max_ind_blocks
, sizeof(blk_t
),
76 memset(rec
.ind_blocks
, 0, rec
.max_ind_blocks
* sizeof(blk_t
));
77 retval
= ext2fs_get_mem(fs
->blocksize
, &rec
.block_buf
);
80 memset(rec
.block_buf
, 0, fs
->blocksize
);
84 * First clear the old bad blocks (while saving the indirect blocks)
86 retval
= ext2fs_block_iterate2(fs
, EXT2_BAD_INO
,
87 BLOCK_FLAG_DEPTH_TRAVERSE
, 0,
88 clear_bad_block_proc
, &rec
);
97 * Now set the bad blocks!
99 * First, mark the bad blocks as used. This prevents a bad
100 * block from being used as an indirect block for the bad
104 retval
= ext2fs_badblocks_list_iterate_begin(bb_list
,
108 retval
= ext2fs_block_iterate2(fs
, EXT2_BAD_INO
,
109 BLOCK_FLAG_APPEND
, 0,
110 set_bad_block_proc
, &rec
);
111 ext2fs_badblocks_list_iterate_end(rec
.bb_iter
);
121 * Update the bad block inode's mod time and block count
124 retval
= ext2fs_read_inode(fs
, EXT2_BAD_INO
, &inode
);
128 now
= fs
->now
? fs
->now
: time(0);
129 ext2fs_inode_xtime_set(&inode
, i_atime
, now
);
130 if (!ext2fs_inode_xtime_get(&inode
, i_ctime
))
131 ext2fs_inode_xtime_set(&inode
, i_ctime
, now
);
132 ext2fs_inode_xtime_set(&inode
, i_mtime
, now
);
133 ext2fs_iblk_set(fs
, &inode
, rec
.bad_block_count
);
134 retval
= ext2fs_inode_size_set(fs
, &inode
,
135 rec
.bad_block_count
* fs
->blocksize
);
139 retval
= ext2fs_write_inode(fs
, EXT2_BAD_INO
, &inode
);
144 ext2fs_free_mem(&rec
.ind_blocks
);
145 ext2fs_free_mem(&rec
.block_buf
);
150 * Helper function for update_bb_inode()
152 * Clear the bad blocks in the bad block inode, while saving the
158 static int clear_bad_block_proc(ext2_filsys fs
, blk_t
*block_nr
,
159 e2_blkcnt_t blockcnt
,
160 blk_t ref_block
EXT2FS_ATTR((unused
)),
161 int ref_offset
EXT2FS_ATTR((unused
)),
164 struct set_badblock_record
*rec
= (struct set_badblock_record
*)
167 unsigned long old_size
;
173 * If the block number is outrageous, clear it and ignore it.
175 if (*block_nr
>= ext2fs_blocks_count(fs
->super
) ||
176 *block_nr
< fs
->super
->s_first_data_block
) {
178 return BLOCK_CHANGED
;
182 if (rec
->ind_blocks_size
>= rec
->max_ind_blocks
) {
183 old_size
= rec
->max_ind_blocks
* sizeof(blk_t
);
184 rec
->max_ind_blocks
+= 10;
185 retval
= ext2fs_resize_mem(old_size
,
186 rec
->max_ind_blocks
* sizeof(blk_t
),
189 rec
->max_ind_blocks
-= 10;
194 rec
->ind_blocks
[rec
->ind_blocks_size
++] = *block_nr
;
198 * Mark the block as unused, and update accounting information
200 ext2fs_block_alloc_stats2(fs
, *block_nr
, -1);
203 return BLOCK_CHANGED
;
208 * Helper function for update_bb_inode()
210 * Set the block list in the bad block inode, using the supplied bitmap.
215 static int set_bad_block_proc(ext2_filsys fs
, blk_t
*block_nr
,
216 e2_blkcnt_t blockcnt
,
217 blk_t ref_block
EXT2FS_ATTR((unused
)),
218 int ref_offset
EXT2FS_ATTR((unused
)),
221 struct set_badblock_record
*rec
= (struct set_badblock_record
*)
228 * Get the next bad block.
230 if (!ext2fs_badblocks_list_iterate(rec
->bb_iter
, &blk
))
232 rec
->bad_block_count
++;
235 * An indirect block; fetch a block from the
236 * previously used indirect block list. The block
237 * most be not marked as used; if so, get another one.
238 * If we run out of reserved indirect blocks, allocate
242 if (rec
->ind_blocks_ptr
< rec
->ind_blocks_size
) {
243 blk
= rec
->ind_blocks
[rec
->ind_blocks_ptr
++];
244 if (ext2fs_test_block_bitmap2(fs
->block_map
, blk
))
247 retval
= ext2fs_new_block(fs
, 0, 0, &blk
);
253 retval
= io_channel_write_blk64(fs
->io
, blk
, 1, rec
->block_buf
);
261 * Update block counts
263 ext2fs_block_alloc_stats2(fs
, blk
, +1);
266 return BLOCK_CHANGED
;