1 /* vi: set sw=4 ts=4: */
3 * bb_inode.c --- routines to update the bad block inode.
5 * WARNING: This routine modifies a lot of state in the filesystem; if
6 * this routine returns an error, the bad block inode may be in an
9 * Copyright (C) 1994, 1995 Theodore Ts'o.
12 * This file may be redistributed under the terms of the GNU Public
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 rec
.bad_block_count
= 0;
70 rec
.ind_blocks_size
= rec
.ind_blocks_ptr
= 0;
71 rec
.max_ind_blocks
= 10;
72 retval
= ext2fs_get_mem(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 indirecto 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 inode
.i_atime
= inode
.i_mtime
= time(NULL
);
130 inode
.i_ctime
= time(NULL
);
131 inode
.i_blocks
= rec
.bad_block_count
* (fs
->blocksize
/ 512);
132 inode
.i_size
= rec
.bad_block_count
* fs
->blocksize
;
134 retval
= ext2fs_write_inode(fs
, EXT2_BAD_INO
, &inode
);
139 ext2fs_free_mem(&rec
.ind_blocks
);
140 ext2fs_free_mem(&rec
.block_buf
);
145 * Helper function for update_bb_inode()
147 * Clear the bad blocks in the bad block inode, while saving the
153 static int clear_bad_block_proc(ext2_filsys fs
, blk_t
*block_nr
,
154 e2_blkcnt_t blockcnt
,
155 blk_t ref_block
EXT2FS_ATTR((unused
)),
156 int ref_offset
EXT2FS_ATTR((unused
)),
159 struct set_badblock_record
*rec
= (struct set_badblock_record
*)
162 unsigned long old_size
;
168 * If the block number is outrageous, clear it and ignore it.
170 if (*block_nr
>= fs
->super
->s_blocks_count
||
171 *block_nr
< fs
->super
->s_first_data_block
) {
173 return BLOCK_CHANGED
;
177 if (rec
->ind_blocks_size
>= rec
->max_ind_blocks
) {
178 old_size
= rec
->max_ind_blocks
* sizeof(blk_t
);
179 rec
->max_ind_blocks
+= 10;
180 retval
= ext2fs_resize_mem(old_size
,
181 rec
->max_ind_blocks
* sizeof(blk_t
),
184 rec
->max_ind_blocks
-= 10;
189 rec
->ind_blocks
[rec
->ind_blocks_size
++] = *block_nr
;
193 * Mark the block as unused, and update accounting information
195 ext2fs_block_alloc_stats(fs
, *block_nr
, -1);
198 return BLOCK_CHANGED
;
203 * Helper function for update_bb_inode()
205 * Set the block list in the bad block inode, using the supplied bitmap.
210 static int set_bad_block_proc(ext2_filsys fs
, blk_t
*block_nr
,
211 e2_blkcnt_t blockcnt
,
212 blk_t ref_block
EXT2FS_ATTR((unused
)),
213 int ref_offset
EXT2FS_ATTR((unused
)),
216 struct set_badblock_record
*rec
= (struct set_badblock_record
*)
223 * Get the next bad block.
225 if (!ext2fs_badblocks_list_iterate(rec
->bb_iter
, &blk
))
227 rec
->bad_block_count
++;
230 * An indirect block; fetch a block from the
231 * previously used indirect block list. The block
232 * most be not marked as used; if so, get another one.
233 * If we run out of reserved indirect blocks, allocate
237 if (rec
->ind_blocks_ptr
< rec
->ind_blocks_size
) {
238 blk
= rec
->ind_blocks
[rec
->ind_blocks_ptr
++];
239 if (ext2fs_test_block_bitmap(fs
->block_map
, blk
))
242 retval
= ext2fs_new_block(fs
, 0, 0, &blk
);
248 retval
= io_channel_write_blk(fs
->io
, blk
, 1, rec
->block_buf
);
256 * Update block counts
258 ext2fs_block_alloc_stats(fs
, blk
, +1);
261 return BLOCK_CHANGED
;