blob: cc867cd5471c994ebe159f0bae08c4120063462f [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * bb_inode.c --- routines to update the bad block inode.
3 *
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
6 * inconsistent state.
7 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00008 * Copyright (C) 1994, 1995 Theodore Ts'o.
9 *
10 * %Begin-Header%
11 * This file may be redistributed under the terms of the GNU Public
12 * License.
13 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000014 */
15
16#include <stdio.h>
17#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000018#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000019#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000020#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000021#include <fcntl.h>
22#include <time.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000023#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000024#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000025#endif
26#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000028#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000029
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000030#if EXT2_FLAT_INCLUDES
31#include "ext2_fs.h"
32#else
Theodore Ts'o3839e651997-04-26 13:21:57 +000033#include <linux/ext2_fs.h>
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000034#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000035
36#include "ext2fs.h"
37
38struct set_badblock_record {
Theodore Ts'o21c84b71997-04-29 16:15:03 +000039 ext2_badblocks_iterate bb_iter;
Theodore Ts'o3839e651997-04-26 13:21:57 +000040 int bad_block_count;
41 blk_t *ind_blocks;
42 int max_ind_blocks;
43 int ind_blocks_size;
44 int ind_blocks_ptr;
45 char *block_buf;
46 errcode_t err;
47};
48
49static int set_bad_block_proc(ext2_filsys fs, blk_t *block_nr, int blockcnt,
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000050 blk_t ref_block, int ref_offset,
51 void *priv_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +000052static int clear_bad_block_proc(ext2_filsys fs, blk_t *block_nr, int blockcnt,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000053 blk_t ref_block, int ref_offset,
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000054 void *priv_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +000055
56/*
57 * Given a bad blocks bitmap, update the bad blocks inode to reflect
58 * the map.
59 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +000060errcode_t ext2fs_update_bb_inode(ext2_filsys fs, ext2_badblocks_list bb_list)
Theodore Ts'o3839e651997-04-26 13:21:57 +000061{
62 errcode_t retval;
63 struct set_badblock_record rec;
64 struct ext2_inode inode;
Theodore Ts'of3db3561997-04-26 13:34:30 +000065 blk_t blk;
Theodore Ts'o3839e651997-04-26 13:21:57 +000066
Theodore Ts'of3db3561997-04-26 13:34:30 +000067 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
68
Theodore Ts'o3839e651997-04-26 13:21:57 +000069 if (!fs->block_map)
70 return EXT2_ET_NO_BLOCK_BITMAP;
71
72 rec.bad_block_count = 0;
73 rec.ind_blocks_size = rec.ind_blocks_ptr = 0;
74 rec.max_ind_blocks = 10;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000075 retval = ext2fs_get_mem(rec.max_ind_blocks * sizeof(blk_t),
76 (void **) &rec.ind_blocks);
77 if (retval)
78 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +000079 memset(rec.ind_blocks, 0, rec.max_ind_blocks * sizeof(blk_t));
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000080 retval = ext2fs_get_mem(fs->blocksize, (void **) &rec.block_buf);
81 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +000082 goto cleanup;
Theodore Ts'o3839e651997-04-26 13:21:57 +000083 memset(rec.block_buf, 0, fs->blocksize);
84 rec.err = 0;
85
86 /*
87 * First clear the old bad blocks (while saving the indirect blocks)
88 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +000089 retval = ext2fs_block_iterate2(fs, EXT2_BAD_INO,
90 BLOCK_FLAG_DEPTH_TRAVERSE, 0,
91 clear_bad_block_proc, &rec);
Theodore Ts'o3839e651997-04-26 13:21:57 +000092 if (retval)
93 goto cleanup;
94 if (rec.err) {
95 retval = rec.err;
96 goto cleanup;
97 }
98
99 /*
100 * Now set the bad blocks!
Theodore Ts'of3db3561997-04-26 13:34:30 +0000101 *
102 * First, mark the bad blocks as used. This prevents a bad
103 * block from being used as an indirecto block for the bad
104 * block inode (!).
Theodore Ts'o3839e651997-04-26 13:21:57 +0000105 */
106 if (bb_list) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000107 retval = ext2fs_badblocks_list_iterate_begin(bb_list,
108 &rec.bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000109 if (retval)
110 goto cleanup;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000111 while (ext2fs_badblocks_list_iterate(rec.bb_iter, &blk)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000112 ext2fs_mark_block_bitmap(fs->block_map, blk);
113 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000114 ext2fs_badblocks_list_iterate_end(rec.bb_iter);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000115 ext2fs_mark_bb_dirty(fs);
116
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000117 retval = ext2fs_badblocks_list_iterate_begin(bb_list,
118 &rec.bb_iter);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000119 if (retval)
120 goto cleanup;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000121 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);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000125 if (retval)
126 goto cleanup;
127 if (rec.err) {
128 retval = rec.err;
129 goto cleanup;
130 }
131 }
132
133 /*
134 * Update the bad block inode's mod time and block count
135 * field.
136 */
137 retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode);
138 if (retval)
139 goto cleanup;
140
141 inode.i_atime = inode.i_mtime = time(0);
142 if (!inode.i_ctime)
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;
146
147 retval = ext2fs_write_inode(fs, EXT2_BAD_INO, &inode);
148 if (retval)
149 goto cleanup;
150
151cleanup:
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000152 ext2fs_free_mem((void **) &rec.ind_blocks);
153 ext2fs_free_mem((void **) &rec.block_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000154 return retval;
155}
156
157/*
158 * Helper function for update_bb_inode()
159 *
160 * Clear the bad blocks in the bad block inode, while saving the
161 * indirect blocks.
162 */
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +0000163#ifdef __TURBOC__
164#pragma argsused
165#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000166static int clear_bad_block_proc(ext2_filsys fs, blk_t *block_nr, int blockcnt,
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +0000167 blk_t ref_block, int ref_offset,
168 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000169{
170 struct set_badblock_record *rec = (struct set_badblock_record *)
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +0000171 priv_data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000172 errcode_t retval;
173 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000174
175 if (!*block_nr)
176 return 0;
177
Theodore Ts'of3db3561997-04-26 13:34:30 +0000178 /*
179 * If the block number is outrageous, clear it and ignore it.
180 */
181 if (*block_nr >= fs->super->s_blocks_count ||
182 *block_nr < fs->super->s_first_data_block) {
183 *block_nr = 0;
184 return BLOCK_CHANGED;
185 }
186
Theodore Ts'o3839e651997-04-26 13:21:57 +0000187 if (blockcnt < 0) {
188 if (rec->ind_blocks_size >= rec->max_ind_blocks) {
189 rec->max_ind_blocks += 10;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000190 retval = ext2fs_resize_mem(rec->max_ind_blocks
191 * sizeof(blk_t),
192 (void **) &rec->ind_blocks);
193 if (retval) {
194 rec->err = retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000195 return BLOCK_ABORT;
196 }
197 }
198 rec->ind_blocks[rec->ind_blocks_size++] = *block_nr;
199 }
200
201 /*
202 * Mark the block as unused, and update accounting information
203 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000204 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000205 ext2fs_mark_bb_dirty(fs);
206 group = ext2fs_group_of_blk(fs, *block_nr);
207 fs->group_desc[group].bg_free_blocks_count++;
208 fs->super->s_free_blocks_count++;
209 ext2fs_mark_super_dirty(fs);
210
211 *block_nr = 0;
212 return BLOCK_CHANGED;
213}
214
215
216/*
217 * Helper function for update_bb_inode()
218 *
219 * Set the block list in the bad block inode, using the supplied bitmap.
220 */
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +0000221#ifdef __TURBOC__
222#pragma argsused
223#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000224static int set_bad_block_proc(ext2_filsys fs, blk_t *block_nr,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000225 int blockcnt, blk_t ref_block,
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +0000226 int ref_offset, void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000227{
228 struct set_badblock_record *rec = (struct set_badblock_record *)
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +0000229 priv_data;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000230 errcode_t retval;
231 blk_t blk;
232 int group;
233
234 if (blockcnt >= 0) {
235 /*
236 * Get the next bad block.
237 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000238 if (!ext2fs_badblocks_list_iterate(rec->bb_iter, &blk))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000239 return BLOCK_ABORT;
240 rec->bad_block_count++;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000241 } else {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000242 /*
243 * An indirect block; fetch a block from the
Theodore Ts'of3db3561997-04-26 13:34:30 +0000244 * previously used indirect block list. The block
245 * most be not marked as used; if so, get another one.
246 * If we run out of reserved indirect blocks, allocate
247 * a new one.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000248 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000249 retry:
250 if (rec->ind_blocks_ptr < rec->ind_blocks_size) {
251 blk = rec->ind_blocks[rec->ind_blocks_ptr++];
252 if (ext2fs_test_block_bitmap(fs->block_map, blk))
253 goto retry;
254 } else {
255 retval = ext2fs_new_block(fs, 0, 0, &blk);
256 if (retval) {
257 rec->err = retval;
258 return BLOCK_ABORT;
259 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000260 }
261 retval = io_channel_write_blk(fs->io, blk, 1, rec->block_buf);
262 if (retval) {
263 rec->err = retval;
264 return BLOCK_ABORT;
265 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000266 ext2fs_mark_block_bitmap(fs->block_map, blk);
267 ext2fs_mark_bb_dirty(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000268 }
269
270 /*
Theodore Ts'of3db3561997-04-26 13:34:30 +0000271 * Update block counts
Theodore Ts'o3839e651997-04-26 13:21:57 +0000272 */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000273 group = ext2fs_group_of_blk(fs, blk);
274 fs->group_desc[group].bg_free_blocks_count--;
275 fs->super->s_free_blocks_count--;
276 ext2fs_mark_super_dirty(fs);
277
278 *block_nr = blk;
279 return BLOCK_CHANGED;
280}
281
282
283
284
285
286