Whamcloud - gitweb
dir_iterate.c (ext2fs_dir_iterate2, ext2fs_process_dir_block):
[tools/e2fsprogs.git] / lib / ext2fs / bb_inode.c
1 /*
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  * 
8  * 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%
14  */
15
16 #include <stdio.h>
17 #include <string.h>
18 #if HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21 #include <fcntl.h>
22 #include <time.h>
23 #if HAVE_SYS_STAT_H
24 #include <sys/stat.h>
25 #endif
26 #if HAVE_SYS_TYPES_H
27 #include <sys/types.h>
28 #endif
29
30 #include "ext2_fs.h"
31 #include "ext2fs.h"
32
33 struct set_badblock_record {
34         ext2_badblocks_iterate  bb_iter;
35         int             bad_block_count;
36         blk_t           *ind_blocks;
37         int             max_ind_blocks;
38         int             ind_blocks_size;
39         int             ind_blocks_ptr;
40         char            *block_buf;
41         errcode_t       err;
42 };
43
44 static int set_bad_block_proc(ext2_filsys fs, blk_t *block_nr,
45                               e2_blkcnt_t blockcnt,
46                               blk_t ref_block, int ref_offset,
47                               void *priv_data);
48 static int clear_bad_block_proc(ext2_filsys fs, blk_t *block_nr,
49                                 e2_blkcnt_t blockcnt,
50                                 blk_t ref_block, int ref_offset,
51                                 void *priv_data);
52         
53 /*
54  * Given a bad blocks bitmap, update the bad blocks inode to reflect
55  * the map.
56  */
57 errcode_t ext2fs_update_bb_inode(ext2_filsys fs, ext2_badblocks_list bb_list)
58 {
59         errcode_t                       retval;
60         struct set_badblock_record      rec;
61         struct ext2_inode               inode;
62         
63         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
64
65         if (!fs->block_map)
66                 return EXT2_ET_NO_BLOCK_BITMAP;
67         
68         rec.bad_block_count = 0;
69         rec.ind_blocks_size = rec.ind_blocks_ptr = 0;
70         rec.max_ind_blocks = 10;
71         retval = ext2fs_get_mem(rec.max_ind_blocks * sizeof(blk_t),
72                                 (void **) &rec.ind_blocks);
73         if (retval)
74                 return retval;
75         memset(rec.ind_blocks, 0, rec.max_ind_blocks * sizeof(blk_t));
76         retval = ext2fs_get_mem(fs->blocksize, (void **) &rec.block_buf);
77         if (retval)
78                 goto cleanup;
79         memset(rec.block_buf, 0, fs->blocksize);
80         rec.err = 0;
81         
82         /*
83          * First clear the old bad blocks (while saving the indirect blocks) 
84          */
85         retval = ext2fs_block_iterate2(fs, EXT2_BAD_INO,
86                                        BLOCK_FLAG_DEPTH_TRAVERSE, 0,
87                                        clear_bad_block_proc, &rec);
88         if (retval)
89                 goto cleanup;
90         if (rec.err) {
91                 retval = rec.err;
92                 goto cleanup;
93         }
94         
95         /*
96          * Now set the bad blocks!
97          *
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 (!).
101          */
102         if (bb_list) {
103                 retval = ext2fs_badblocks_list_iterate_begin(bb_list,
104                                                              &rec.bb_iter);
105                 if (retval)
106                         goto cleanup;
107                 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);
111                 if (retval) 
112                         goto cleanup;
113                 if (rec.err) {
114                         retval = rec.err;
115                         goto cleanup;
116                 }
117         }
118         
119         /*
120          * Update the bad block inode's mod time and block count
121          * field.  
122          */
123         retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode);
124         if (retval)
125                 goto cleanup;
126         
127         inode.i_atime = inode.i_mtime = time(0);
128         if (!inode.i_ctime)
129                 inode.i_ctime = time(0);
130         inode.i_blocks = rec.bad_block_count * (fs->blocksize / 512);
131         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;
136         
137 cleanup:
138         ext2fs_free_mem((void **) &rec.ind_blocks);
139         ext2fs_free_mem((void **) &rec.block_buf);
140         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  */
149 #ifdef __TURBOC__
150  #pragma argsused
151 #endif
152 static int clear_bad_block_proc(ext2_filsys fs, blk_t *block_nr,
153                                 e2_blkcnt_t blockcnt,
154                                 blk_t ref_block, int ref_offset,
155                                 void *priv_data)
156 {
157         struct set_badblock_record *rec = (struct set_badblock_record *)
158                 priv_data;
159         errcode_t       retval;
160         unsigned long   old_size;
161
162         if (!*block_nr)
163                 return 0;
164
165         /*
166          * If the block number is outrageous, clear it and ignore it.
167          */
168         if (*block_nr >= fs->super->s_blocks_count ||
169             *block_nr < fs->super->s_first_data_block) {
170                 *block_nr = 0;
171                 return BLOCK_CHANGED;
172         }
173
174         if (blockcnt < 0) {
175                 if (rec->ind_blocks_size >= rec->max_ind_blocks) {
176                         old_size = rec->max_ind_blocks * sizeof(blk_t);
177                         rec->max_ind_blocks += 10;
178                         retval = ext2fs_resize_mem(old_size, 
179                                    rec->max_ind_blocks * sizeof(blk_t),
180                                    (void **) &rec->ind_blocks);
181                         if (retval) {
182                                 rec->max_ind_blocks -= 10;
183                                 rec->err = retval;
184                                 return BLOCK_ABORT;
185                         }
186                 }
187                 rec->ind_blocks[rec->ind_blocks_size++] = *block_nr;
188         }
189
190         /*
191          * Mark the block as unused, and update accounting information
192          */
193         ext2fs_block_alloc_stats(fs, *block_nr, -1);
194         
195         *block_nr = 0;
196         return BLOCK_CHANGED;
197 }
198
199         
200 /*
201  * Helper function for update_bb_inode()
202  *
203  * Set the block list in the bad block inode, using the supplied bitmap.
204  */
205 #ifdef __TURBOC__
206  #pragma argsused
207 #endif
208 static int set_bad_block_proc(ext2_filsys fs, blk_t *block_nr,
209                               e2_blkcnt_t blockcnt, blk_t ref_block, 
210                               int ref_offset, void *priv_data)
211 {
212         struct set_badblock_record *rec = (struct set_badblock_record *)
213                 priv_data;
214         errcode_t       retval;
215         blk_t           blk;
216
217         if (blockcnt >= 0) {
218                 /*
219                  * Get the next bad block.
220                  */
221                 if (!ext2fs_badblocks_list_iterate(rec->bb_iter, &blk))
222                         return BLOCK_ABORT;
223                 rec->bad_block_count++;
224         } else {
225                 /*
226                  * An indirect block; fetch a block from the
227                  * previously used indirect block list.  The block
228                  * most be not marked as used; if so, get another one.
229                  * If we run out of reserved indirect blocks, allocate
230                  * a new one.
231                  */
232         retry:
233                 if (rec->ind_blocks_ptr < rec->ind_blocks_size) {
234                         blk = rec->ind_blocks[rec->ind_blocks_ptr++];
235                         if (ext2fs_test_block_bitmap(fs->block_map, blk))
236                                 goto retry;
237                 } else {
238                         retval = ext2fs_new_block(fs, 0, 0, &blk);
239                         if (retval) {
240                                 rec->err = retval;
241                                 return BLOCK_ABORT;
242                         }
243                 }
244                 retval = io_channel_write_blk(fs->io, blk, 1, rec->block_buf);
245                 if (retval) {
246                         rec->err = retval;
247                         return BLOCK_ABORT;
248                 }
249         }
250         
251         /*
252          * Update block counts
253          */
254         ext2fs_block_alloc_stats(fs, blk, +1);
255         
256         *block_nr = blk;
257         return BLOCK_CHANGED;
258 }
259
260
261
262
263
264