blob: e6ce7766a0324fb50e8ada1300f1301e0ddd6182 [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
Theodore Ts'o36a43d61998-03-24 16:17:51 +000049static int set_bad_block_proc(ext2_filsys fs, blk_t *block_nr,
Theodore Ts'o03673db1998-06-10 20:39:43 +000050 e2_blkcnt_t blockcnt,
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000051 blk_t ref_block, int ref_offset,
52 void *priv_data);
Theodore Ts'o36a43d61998-03-24 16:17:51 +000053static int clear_bad_block_proc(ext2_filsys fs, blk_t *block_nr,
Theodore Ts'o03673db1998-06-10 20:39:43 +000054 e2_blkcnt_t blockcnt,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000055 blk_t ref_block, int ref_offset,
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000056 void *priv_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +000057
58/*
59 * Given a bad blocks bitmap, update the bad blocks inode to reflect
60 * the map.
61 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +000062errcode_t ext2fs_update_bb_inode(ext2_filsys fs, ext2_badblocks_list bb_list)
Theodore Ts'o3839e651997-04-26 13:21:57 +000063{
64 errcode_t retval;
65 struct set_badblock_record rec;
66 struct ext2_inode inode;
Theodore Ts'of3db3561997-04-26 13:34:30 +000067 blk_t blk;
Theodore Ts'o3839e651997-04-26 13:21:57 +000068
Theodore Ts'of3db3561997-04-26 13:34:30 +000069 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
70
Theodore Ts'o3839e651997-04-26 13:21:57 +000071 if (!fs->block_map)
72 return EXT2_ET_NO_BLOCK_BITMAP;
73
74 rec.bad_block_count = 0;
75 rec.ind_blocks_size = rec.ind_blocks_ptr = 0;
76 rec.max_ind_blocks = 10;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000077 retval = ext2fs_get_mem(rec.max_ind_blocks * sizeof(blk_t),
78 (void **) &rec.ind_blocks);
79 if (retval)
80 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +000081 memset(rec.ind_blocks, 0, rec.max_ind_blocks * sizeof(blk_t));
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000082 retval = ext2fs_get_mem(fs->blocksize, (void **) &rec.block_buf);
83 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +000084 goto cleanup;
Theodore Ts'o3839e651997-04-26 13:21:57 +000085 memset(rec.block_buf, 0, fs->blocksize);
86 rec.err = 0;
87
88 /*
89 * First clear the old bad blocks (while saving the indirect blocks)
90 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +000091 retval = ext2fs_block_iterate2(fs, EXT2_BAD_INO,
92 BLOCK_FLAG_DEPTH_TRAVERSE, 0,
93 clear_bad_block_proc, &rec);
Theodore Ts'o3839e651997-04-26 13:21:57 +000094 if (retval)
95 goto cleanup;
96 if (rec.err) {
97 retval = rec.err;
98 goto cleanup;
99 }
100
101 /*
102 * Now set the bad blocks!
Theodore Ts'of3db3561997-04-26 13:34:30 +0000103 *
104 * First, mark the bad blocks as used. This prevents a bad
105 * block from being used as an indirecto block for the bad
106 * block inode (!).
Theodore Ts'o3839e651997-04-26 13:21:57 +0000107 */
108 if (bb_list) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000109 retval = ext2fs_badblocks_list_iterate_begin(bb_list,
110 &rec.bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000111 if (retval)
112 goto cleanup;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000113 while (ext2fs_badblocks_list_iterate(rec.bb_iter, &blk)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000114 ext2fs_mark_block_bitmap(fs->block_map, blk);
115 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000116 ext2fs_badblocks_list_iterate_end(rec.bb_iter);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000117 ext2fs_mark_bb_dirty(fs);
118
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000119 retval = ext2fs_badblocks_list_iterate_begin(bb_list,
120 &rec.bb_iter);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000121 if (retval)
122 goto cleanup;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000123 retval = ext2fs_block_iterate2(fs, EXT2_BAD_INO,
124 BLOCK_FLAG_APPEND, 0,
125 set_bad_block_proc, &rec);
126 ext2fs_badblocks_list_iterate_end(rec.bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000127 if (retval)
128 goto cleanup;
129 if (rec.err) {
130 retval = rec.err;
131 goto cleanup;
132 }
133 }
134
135 /*
136 * Update the bad block inode's mod time and block count
137 * field.
138 */
139 retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode);
140 if (retval)
141 goto cleanup;
142
143 inode.i_atime = inode.i_mtime = time(0);
144 if (!inode.i_ctime)
145 inode.i_ctime = time(0);
146 inode.i_blocks = rec.bad_block_count * (fs->blocksize / 512);
147 inode.i_size = rec.bad_block_count * fs->blocksize;
148
149 retval = ext2fs_write_inode(fs, EXT2_BAD_INO, &inode);
150 if (retval)
151 goto cleanup;
152
153cleanup:
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000154 ext2fs_free_mem((void **) &rec.ind_blocks);
155 ext2fs_free_mem((void **) &rec.block_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000156 return retval;
157}
158
159/*
160 * Helper function for update_bb_inode()
161 *
162 * Clear the bad blocks in the bad block inode, while saving the
163 * indirect blocks.
164 */
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +0000165#ifdef __TURBOC__
166#pragma argsused
167#endif
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000168static int clear_bad_block_proc(ext2_filsys fs, blk_t *block_nr,
Theodore Ts'o03673db1998-06-10 20:39:43 +0000169 e2_blkcnt_t blockcnt,
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +0000170 blk_t ref_block, int ref_offset,
171 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000172{
173 struct set_badblock_record *rec = (struct set_badblock_record *)
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +0000174 priv_data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000175 errcode_t retval;
176 int group;
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000177 unsigned long old_size;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000178
179 if (!*block_nr)
180 return 0;
181
Theodore Ts'of3db3561997-04-26 13:34:30 +0000182 /*
183 * If the block number is outrageous, clear it and ignore it.
184 */
185 if (*block_nr >= fs->super->s_blocks_count ||
186 *block_nr < fs->super->s_first_data_block) {
187 *block_nr = 0;
188 return BLOCK_CHANGED;
189 }
190
Theodore Ts'o3839e651997-04-26 13:21:57 +0000191 if (blockcnt < 0) {
192 if (rec->ind_blocks_size >= rec->max_ind_blocks) {
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000193 old_size = rec->max_ind_blocks * sizeof(blk_t);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000194 rec->max_ind_blocks += 10;
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000195 retval = ext2fs_resize_mem(old_size,
196 rec->max_ind_blocks * sizeof(blk_t),
197 (void **) &rec->ind_blocks);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000198 if (retval) {
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000199 rec->max_ind_blocks -= 10;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000200 rec->err = retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000201 return BLOCK_ABORT;
202 }
203 }
204 rec->ind_blocks[rec->ind_blocks_size++] = *block_nr;
205 }
206
207 /*
208 * Mark the block as unused, and update accounting information
209 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000210 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000211 ext2fs_mark_bb_dirty(fs);
212 group = ext2fs_group_of_blk(fs, *block_nr);
213 fs->group_desc[group].bg_free_blocks_count++;
214 fs->super->s_free_blocks_count++;
215 ext2fs_mark_super_dirty(fs);
216
217 *block_nr = 0;
218 return BLOCK_CHANGED;
219}
220
221
222/*
223 * Helper function for update_bb_inode()
224 *
225 * Set the block list in the bad block inode, using the supplied bitmap.
226 */
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +0000227#ifdef __TURBOC__
228#pragma argsused
229#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000230static int set_bad_block_proc(ext2_filsys fs, blk_t *block_nr,
Theodore Ts'o03673db1998-06-10 20:39:43 +0000231 e2_blkcnt_t blockcnt, blk_t ref_block,
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +0000232 int ref_offset, void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000233{
234 struct set_badblock_record *rec = (struct set_badblock_record *)
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +0000235 priv_data;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000236 errcode_t retval;
237 blk_t blk;
238 int group;
239
240 if (blockcnt >= 0) {
241 /*
242 * Get the next bad block.
243 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000244 if (!ext2fs_badblocks_list_iterate(rec->bb_iter, &blk))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000245 return BLOCK_ABORT;
246 rec->bad_block_count++;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000247 } else {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000248 /*
249 * An indirect block; fetch a block from the
Theodore Ts'of3db3561997-04-26 13:34:30 +0000250 * previously used indirect block list. The block
251 * most be not marked as used; if so, get another one.
252 * If we run out of reserved indirect blocks, allocate
253 * a new one.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000254 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000255 retry:
256 if (rec->ind_blocks_ptr < rec->ind_blocks_size) {
257 blk = rec->ind_blocks[rec->ind_blocks_ptr++];
258 if (ext2fs_test_block_bitmap(fs->block_map, blk))
259 goto retry;
260 } else {
261 retval = ext2fs_new_block(fs, 0, 0, &blk);
262 if (retval) {
263 rec->err = retval;
264 return BLOCK_ABORT;
265 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000266 }
267 retval = io_channel_write_blk(fs->io, blk, 1, rec->block_buf);
268 if (retval) {
269 rec->err = retval;
270 return BLOCK_ABORT;
271 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000272 ext2fs_mark_block_bitmap(fs->block_map, blk);
273 ext2fs_mark_bb_dirty(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000274 }
275
276 /*
Theodore Ts'of3db3561997-04-26 13:34:30 +0000277 * Update block counts
Theodore Ts'o3839e651997-04-26 13:21:57 +0000278 */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000279 group = ext2fs_group_of_blk(fs, blk);
280 fs->group_desc[group].bg_free_blocks_count--;
281 fs->super->s_free_blocks_count--;
282 ext2fs_mark_super_dirty(fs);
283
284 *block_nr = blk;
285 return BLOCK_CHANGED;
286}
287
288
289
290
291
292