blob: 268eecf8b7803d247394a58a14ad24d20e5ebc2b [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * bb_inode.c --- routines to update the bad block inode.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o3839e651997-04-26 13:21:57 +00004 * 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.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04007 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00008 * Copyright (C) 1994, 1995 Theodore Ts'o.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04009 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +000010 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -040011 * This file may be redistributed under the terms of the GNU Library
12 * General Public License, version 2.
Theodore Ts'o21c84b71997-04-29 16:15:03 +000013 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000014 */
15
Theodore Ts'od1154eb2011-09-18 17:34:37 -040016#include "config.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000017#include <stdio.h>
18#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000019#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000020#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000021#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000022#include <fcntl.h>
23#include <time.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000024#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000025#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000026#endif
27#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000028#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000029#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000030
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000031#include "ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000032#include "ext2fs.h"
33
34struct set_badblock_record {
Theodore Ts'o21c84b71997-04-29 16:15:03 +000035 ext2_badblocks_iterate bb_iter;
Theodore Ts'o3839e651997-04-26 13:21:57 +000036 int bad_block_count;
37 blk_t *ind_blocks;
38 int max_ind_blocks;
39 int ind_blocks_size;
40 int ind_blocks_ptr;
41 char *block_buf;
42 errcode_t err;
43};
44
Theodore Ts'o36a43d61998-03-24 16:17:51 +000045static int set_bad_block_proc(ext2_filsys fs, blk_t *block_nr,
Theodore Ts'o03673db1998-06-10 20:39:43 +000046 e2_blkcnt_t blockcnt,
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000047 blk_t ref_block, int ref_offset,
48 void *priv_data);
Theodore Ts'o36a43d61998-03-24 16:17:51 +000049static int clear_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'o21c84b71997-04-29 16:15:03 +000051 blk_t ref_block, int ref_offset,
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000052 void *priv_data);
Theodore Ts'oefc6f622008-08-27 23:07:54 -040053
Theodore Ts'o3839e651997-04-26 13:21:57 +000054/*
55 * Given a bad blocks bitmap, update the bad blocks inode to reflect
56 * the map.
57 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +000058errcode_t ext2fs_update_bb_inode(ext2_filsys fs, ext2_badblocks_list bb_list)
Theodore Ts'o3839e651997-04-26 13:21:57 +000059{
60 errcode_t retval;
61 struct set_badblock_record rec;
62 struct ext2_inode inode;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040063
Theodore Ts'of3db3561997-04-26 13:34:30 +000064 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
65
Theodore Ts'o3839e651997-04-26 13:21:57 +000066 if (!fs->block_map)
67 return EXT2_ET_NO_BLOCK_BITMAP;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040068
Theodore Ts'o21502782010-12-20 10:57:29 -050069 memset(&rec, 0, sizeof(rec));
Theodore Ts'o3839e651997-04-26 13:21:57 +000070 rec.max_ind_blocks = 10;
Theodore Ts'oee010792007-11-09 19:01:06 -050071 retval = ext2fs_get_array(rec.max_ind_blocks, sizeof(blk_t),
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040072 &rec.ind_blocks);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000073 if (retval)
74 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +000075 memset(rec.ind_blocks, 0, rec.max_ind_blocks * sizeof(blk_t));
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040076 retval = ext2fs_get_mem(fs->blocksize, &rec.block_buf);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000077 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +000078 goto cleanup;
Theodore Ts'o3839e651997-04-26 13:21:57 +000079 memset(rec.block_buf, 0, fs->blocksize);
80 rec.err = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040081
Theodore Ts'o3839e651997-04-26 13:21:57 +000082 /*
Theodore Ts'oefc6f622008-08-27 23:07:54 -040083 * First clear the old bad blocks (while saving the indirect blocks)
Theodore Ts'o3839e651997-04-26 13:21:57 +000084 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +000085 retval = ext2fs_block_iterate2(fs, EXT2_BAD_INO,
86 BLOCK_FLAG_DEPTH_TRAVERSE, 0,
87 clear_bad_block_proc, &rec);
Theodore Ts'o3839e651997-04-26 13:21:57 +000088 if (retval)
89 goto cleanup;
90 if (rec.err) {
91 retval = rec.err;
92 goto cleanup;
93 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -040094
Theodore Ts'o3839e651997-04-26 13:21:57 +000095 /*
96 * Now set the bad blocks!
Theodore Ts'of3db3561997-04-26 13:34:30 +000097 *
98 * First, mark the bad blocks as used. This prevents a bad
99 * block from being used as an indirecto block for the bad
100 * block inode (!).
Theodore Ts'o3839e651997-04-26 13:21:57 +0000101 */
102 if (bb_list) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000103 retval = ext2fs_badblocks_list_iterate_begin(bb_list,
104 &rec.bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000105 if (retval)
106 goto cleanup;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000107 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);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400111 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000112 goto cleanup;
113 if (rec.err) {
114 retval = rec.err;
115 goto cleanup;
116 }
117 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400118
Theodore Ts'o3839e651997-04-26 13:21:57 +0000119 /*
120 * Update the bad block inode's mod time and block count
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400121 * field.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000122 */
123 retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode);
124 if (retval)
125 goto cleanup;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400126
Theodore Ts'o32138182005-09-24 20:14:51 -0400127 inode.i_atime = inode.i_mtime = fs->now ? fs->now : time(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000128 if (!inode.i_ctime)
Theodore Ts'o32138182005-09-24 20:14:51 -0400129 inode.i_ctime = fs->now ? fs->now : time(0);
Theodore Ts'o1ca10592008-04-09 11:39:11 -0400130 ext2fs_iblk_set(fs, &inode, rec.bad_block_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000131 inode.i_size = rec.bad_block_count * fs->blocksize;
132
133 retval = ext2fs_write_inode(fs, EXT2_BAD_INO, &inode);
134 if (retval)
135 goto cleanup;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400136
Theodore Ts'o3839e651997-04-26 13:21:57 +0000137cleanup:
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400138 ext2fs_free_mem(&rec.ind_blocks);
139 ext2fs_free_mem(&rec.block_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000140 return retval;
141}
142
143/*
144 * Helper function for update_bb_inode()
145 *
146 * Clear the bad blocks in the bad block inode, while saving the
147 * indirect blocks.
148 */
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +0000149#ifdef __TURBOC__
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000150 #pragma argsused
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +0000151#endif
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000152static int clear_bad_block_proc(ext2_filsys fs, blk_t *block_nr,
Theodore Ts'o03673db1998-06-10 20:39:43 +0000153 e2_blkcnt_t blockcnt,
Theodore Ts'o54434922003-12-07 01:28:50 -0500154 blk_t ref_block EXT2FS_ATTR((unused)),
155 int ref_offset EXT2FS_ATTR((unused)),
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +0000156 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157{
158 struct set_badblock_record *rec = (struct set_badblock_record *)
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +0000159 priv_data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000160 errcode_t retval;
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000161 unsigned long old_size;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000162
163 if (!*block_nr)
164 return 0;
165
Theodore Ts'of3db3561997-04-26 13:34:30 +0000166 /*
167 * If the block number is outrageous, clear it and ignore it.
168 */
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400169 if (*block_nr >= ext2fs_blocks_count(fs->super) ||
Theodore Ts'of3db3561997-04-26 13:34:30 +0000170 *block_nr < fs->super->s_first_data_block) {
171 *block_nr = 0;
172 return BLOCK_CHANGED;
173 }
174
Theodore Ts'o3839e651997-04-26 13:21:57 +0000175 if (blockcnt < 0) {
176 if (rec->ind_blocks_size >= rec->max_ind_blocks) {
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000177 old_size = rec->max_ind_blocks * sizeof(blk_t);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000178 rec->max_ind_blocks += 10;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400179 retval = ext2fs_resize_mem(old_size,
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000180 rec->max_ind_blocks * sizeof(blk_t),
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400181 &rec->ind_blocks);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000182 if (retval) {
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000183 rec->max_ind_blocks -= 10;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000184 rec->err = retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000185 return BLOCK_ABORT;
186 }
187 }
188 rec->ind_blocks[rec->ind_blocks_size++] = *block_nr;
189 }
190
191 /*
192 * Mark the block as unused, and update accounting information
193 */
Valerie Aurora Henson48f23052009-10-25 21:46:58 -0400194 ext2fs_block_alloc_stats2(fs, *block_nr, -1);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400195
Theodore Ts'o3839e651997-04-26 13:21:57 +0000196 *block_nr = 0;
197 return BLOCK_CHANGED;
198}
199
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400200
Theodore Ts'o3839e651997-04-26 13:21:57 +0000201/*
202 * Helper function for update_bb_inode()
203 *
204 * Set the block list in the bad block inode, using the supplied bitmap.
205 */
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +0000206#ifdef __TURBOC__
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000207 #pragma argsused
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +0000208#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000209static int set_bad_block_proc(ext2_filsys fs, blk_t *block_nr,
Theodore Ts'o54434922003-12-07 01:28:50 -0500210 e2_blkcnt_t blockcnt,
211 blk_t ref_block EXT2FS_ATTR((unused)),
212 int ref_offset EXT2FS_ATTR((unused)),
213 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000214{
215 struct set_badblock_record *rec = (struct set_badblock_record *)
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +0000216 priv_data;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000217 errcode_t retval;
218 blk_t blk;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000219
220 if (blockcnt >= 0) {
221 /*
222 * Get the next bad block.
223 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000224 if (!ext2fs_badblocks_list_iterate(rec->bb_iter, &blk))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000225 return BLOCK_ABORT;
226 rec->bad_block_count++;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000227 } else {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000228 /*
229 * An indirect block; fetch a block from the
Theodore Ts'of3db3561997-04-26 13:34:30 +0000230 * previously used indirect block list. The block
231 * most be not marked as used; if so, get another one.
232 * If we run out of reserved indirect blocks, allocate
233 * a new one.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000234 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000235 retry:
236 if (rec->ind_blocks_ptr < rec->ind_blocks_size) {
237 blk = rec->ind_blocks[rec->ind_blocks_ptr++];
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -0400238 if (ext2fs_test_block_bitmap2(fs->block_map, blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000239 goto retry;
240 } else {
241 retval = ext2fs_new_block(fs, 0, 0, &blk);
242 if (retval) {
243 rec->err = retval;
244 return BLOCK_ABORT;
245 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000246 }
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400247 retval = io_channel_write_blk64(fs->io, blk, 1, rec->block_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000248 if (retval) {
249 rec->err = retval;
250 return BLOCK_ABORT;
251 }
252 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400253
Theodore Ts'o3839e651997-04-26 13:21:57 +0000254 /*
Theodore Ts'of3db3561997-04-26 13:34:30 +0000255 * Update block counts
Theodore Ts'o3839e651997-04-26 13:21:57 +0000256 */
Valerie Aurora Henson48f23052009-10-25 21:46:58 -0400257 ext2fs_block_alloc_stats2(fs, blk, +1);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400258
Theodore Ts'o3839e651997-04-26 13:21:57 +0000259 *block_nr = blk;
260 return BLOCK_CHANGED;
261}
262
263
264
265
266
267