blob: 2c52944a78ba5ac6a04ad419fdb67963f7ff6c8e [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * pass2.c --- check directory structure
3 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00004 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 *
11 * Pass 2 of e2fsck iterates through all active directory inodes, and
12 * applies to following tests to each directory entry in the directory
13 * blocks in the inodes:
14 *
15 * - The length of the directory entry (rec_len) should be at
16 * least 8 bytes, and no more than the remaining space
17 * left in the directory block.
18 * - The length of the name in the directory entry (name_len)
19 * should be less than (rec_len - 8).
20 * - The inode number in the directory entry should be within
21 * legal bounds.
22 * - The inode number should refer to a in-use inode.
23 * - The first entry should be '.', and its inode should be
24 * the inode of the directory.
25 * - The second entry should be '..'.
26 *
27 * To minimize disk seek time, the directory blocks are processed in
28 * sorted order of block numbers.
29 *
30 * Pass 2 also collects the following information:
31 * - The inode numbers of the subdirectories for each directory.
32 *
33 * Pass 2 relies on the following information from previous passes:
34 * - The directory information collected in pass 1.
35 * - The inode_used_map bitmap
36 * - The inode_bad_map bitmap
37 * - The inode_dir_map bitmap
Theodore Ts'o3839e651997-04-26 13:21:57 +000038 *
39 * Pass 2 frees the following data structures
40 * - The inode_bad_map bitmap
41 */
42
43#include "et/com_err.h"
44
45#include "e2fsck.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000046#include "problem.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000047
48/*
49 * Keeps track of how many times an inode is referenced.
50 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +000051ext2_icount_t inode_count = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000052
53static void deallocate_inode(ext2_filsys fs, ino_t ino,
54 char* block_buf);
55static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino);
56static int check_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000057 struct ext2_db_entry *dir_blocks_info,
58 void *private);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000059static int allocate_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000060 struct ext2_db_entry *dir_blocks_info,
61 char *buf, struct problem_context *pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000062static int update_dir_block(ext2_filsys fs,
63 blk_t *block_nr,
64 int blockcnt,
65 void *private);
Theodore Ts'o3839e651997-04-26 13:21:57 +000066
Theodore Ts'o21c84b71997-04-29 16:15:03 +000067struct check_dir_struct {
68 char *buf;
69 struct problem_context pctx;
70};
71
Theodore Ts'o3839e651997-04-26 13:21:57 +000072void pass2(ext2_filsys fs)
73{
Theodore Ts'o3839e651997-04-26 13:21:57 +000074 char *buf;
75 struct resource_track rtrack;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000076 struct dir_info *dir;
77 errcode_t retval;
78 ino_t size;
79 struct check_dir_struct cd;
80
Theodore Ts'o3839e651997-04-26 13:21:57 +000081 init_resource_track(&rtrack);
82
83#ifdef MTRACE
84 mtrace_print("Pass 2");
85#endif
86
87 if (!preen)
88 printf("Pass 2: Checking directory structure\n");
Theodore Ts'o521e3681997-04-29 17:48:10 +000089 retval = ext2fs_create_icount2(fs, EXT2_ICOUNT_OPT_INCREMENT,
90 0, inode_link_info, &inode_count);
Theodore Ts'o21c84b71997-04-29 16:15:03 +000091 if (retval) {
92 com_err("ext2fs_create_icount", retval,
93 "while creating inode_count");
94 fatal_error(0);
95 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000096 buf = allocate_memory(fs->blocksize, "directory scan buffer");
97
Theodore Ts'o21c84b71997-04-29 16:15:03 +000098 /*
99 * Set up the parent pointer for the root directory, if
100 * present. (If the root directory is not present, we will
101 * create it in pass 3.)
102 */
103 dir = get_dir_info(EXT2_ROOT_INO);
104 if (dir)
105 dir->parent = EXT2_ROOT_INO;
106
107 cd.buf = buf;
108 clear_problem_context(&cd.pctx);
109
110 retval = ext2fs_dblist_iterate(fs->dblist, check_dir_block, &cd);
Theodore Ts'o7ac02a51997-06-11 18:32:35 +0000111 if (retval) {
112 com_err("ext2fs_dblist_iterate", retval,
113 "while iterating through dblist");
114 fatal_error(0);
115 }
116
Theodore Ts'o3839e651997-04-26 13:21:57 +0000117 free(buf);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000118 ext2fs_free_dblist(fs->dblist);
119
Theodore Ts'o3839e651997-04-26 13:21:57 +0000120 if (inode_bad_map) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000121 ext2fs_free_inode_bitmap(inode_bad_map);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000122 inode_bad_map = 0;
123 }
124 if (tflag > 1) {
125 printf("Pass 2: ");
126 print_resource_track(&rtrack);
127 }
128}
129
130/*
131 * Make sure the first entry in the directory is '.', and that the
132 * directory entry is sane.
133 */
134static int check_dot(ext2_filsys fs,
135 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000136 ino_t ino, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000137{
138 struct ext2_dir_entry *nextdir;
139 int status = 0;
140 int created = 0;
141 int new_len;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000142 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000143
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000144 if (!dirent->inode)
145 problem = PR_2_MISSING_DOT;
146 else if ((dirent->name_len != 1) ||
147 (dirent->name[0] != '.'))
148 problem = PR_2_1ST_NOT_DOT;
149 else if (dirent->name[1] != '\0')
150 problem = PR_2_DOT_NULL_TERM;
151
152 if (problem) {
153 if (fix_problem(fs, problem, pctx)) {
154 if (dirent->rec_len < 12)
155 dirent->rec_len = 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000156 dirent->inode = ino;
157 dirent->name_len = 1;
158 dirent->name[0] = '.';
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000159 dirent->name[1] = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000160 status = 1;
161 created = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000162 }
163 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000164 if (dirent->inode != ino) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000165 if (fix_problem(fs, PR_2_BAD_INODE_DOT, pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000166 dirent->inode = ino;
167 status = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000168 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000169 }
170 if (dirent->rec_len > 12) {
171 new_len = dirent->rec_len - 12;
172 if (new_len > 12) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000173 preenhalt(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000174 if (created ||
175 ask("Directory entry for '.' is big. Split", 1)) {
176 nextdir = (struct ext2_dir_entry *)
177 ((char *) dirent + 12);
178 dirent->rec_len = 12;
179 nextdir->rec_len = new_len;
180 nextdir->inode = 0;
181 nextdir->name_len = 0;
182 status = 1;
183 }
184 }
185 }
186 return status;
187}
188
189/*
190 * Make sure the second entry in the directory is '..', and that the
191 * directory entry is sane. We do not check the inode number of '..'
192 * here; this gets done in pass 3.
193 */
194static int check_dotdot(ext2_filsys fs,
195 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000196 struct dir_info *dir, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000197{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000198 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000199
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000200 if (!dirent->inode)
201 problem = PR_2_MISSING_DOT_DOT;
202 else if ((dirent->name_len != 2) ||
203 (dirent->name[0] != '.') ||
204 (dirent->name[1] != '.'))
205 problem = PR_2_2ND_NOT_DOT_DOT;
206 else if (dirent->name[2] != '\0')
207 problem = PR_2_DOT_DOT_NULL_TERM;
208
209 if (problem) {
210 if (fix_problem(fs, problem, pctx)) {
211 if (dirent->rec_len < 12)
212 dirent->rec_len = 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000213 /*
214 * Note: we don't have the parent inode just
215 * yet, so we will fill it in with the root
216 * inode. This will get fixed in pass 3.
217 */
218 dirent->inode = EXT2_ROOT_INO;
219 dirent->name_len = 2;
220 dirent->name[0] = '.';
221 dirent->name[1] = '.';
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000222 dirent->name[2] = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000223 return 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000224 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000225 return 0;
226 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000227 dir->dotdot = dirent->inode;
228 return 0;
229}
230
231/*
232 * Check to make sure a directory entry doesn't contain any illegal
233 * characters.
234 */
235static int check_name(ext2_filsys fs,
236 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000237 ino_t dir_ino, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000238{
239 int i;
240 int fixup = -1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000241 int ret = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000242
243 for ( i = 0; i < dirent->name_len; i++) {
244 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
245 if (fixup < 0) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000246 fixup = fix_problem(fs, PR_2_BAD_NAME, pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000247 }
248 if (fixup) {
249 dirent->name[i] = '.';
250 ret = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000251 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000252 }
253 }
254 return ret;
255}
256
257static int check_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000258 struct ext2_db_entry *db,
259 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000260{
261 struct dir_info *subdir, *dir;
262 struct ext2_dir_entry *dirent;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000263 int offset = 0;
264 int dir_modified = 0;
265 errcode_t retval;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000266 int dot_state;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000267 blk_t block_nr = db->blk;
268 ino_t ino = db->ino;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000269 __u16 links;
270 struct check_dir_struct *cd = private;
271 char *buf = cd->buf;
272
Theodore Ts'o3839e651997-04-26 13:21:57 +0000273 /*
274 * Make sure the inode is still in use (could have been
275 * deleted in the duplicate/bad blocks pass.
276 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000277 if (!(ext2fs_test_inode_bitmap(inode_used_map, ino)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000278 return 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000279
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000280 cd->pctx.ino = ino;
281 cd->pctx.blk = block_nr;
282 cd->pctx.blkcount = db->blockcnt;
283 cd->pctx.ino2 = 0;
284 cd->pctx.dirent = 0;
285 cd->pctx.num = 0;
286
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000287 if (db->blk == 0) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000288 if (allocate_dir_block(fs, db, buf, &cd->pctx))
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000289 return 0;
290 block_nr = db->blk;
291 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000292
293 if (db->blockcnt)
294 dot_state = 2;
295 else
296 dot_state = 0;
297
298#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000299 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000300 db->blockcnt, ino);
301#endif
302
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000303 retval = ext2fs_read_dir_block(fs, block_nr, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000304 if (retval) {
305 com_err(program_name, retval,
306 "while reading directory block %d", block_nr);
307 }
308
309 do {
310 dot_state++;
311 dirent = (struct ext2_dir_entry *) (buf + offset);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000312 cd->pctx.dirent = dirent;
313 cd->pctx.num = offset;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000314 if (((offset + dirent->rec_len) > fs->blocksize) ||
315 (dirent->rec_len < 8) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000316 ((dirent->rec_len % 4) != 0) ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000317 ((dirent->name_len+8) > dirent->rec_len)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000318 if (fix_problem(fs, PR_2_DIR_CORRUPTED, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000319 dirent->rec_len = fs->blocksize - offset;
320 dirent->name_len = 0;
321 dirent->inode = 0;
322 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000323 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000324 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000325 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000326
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000327 if (dirent->name_len > EXT2_NAME_LEN) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000328 if (fix_problem(fs, PR_2_FILENAME_LONG, &cd->pctx)) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000329 dirent->name_len = EXT2_NAME_LEN;
330 dir_modified++;
331 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000332 }
333
Theodore Ts'o3839e651997-04-26 13:21:57 +0000334 if (dot_state == 1) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000335 if (check_dot(fs, dirent, ino, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000336 dir_modified++;
337 } else if (dot_state == 2) {
338 dir = get_dir_info(ino);
339 if (!dir) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000340 printf("Internal error: couldn't find dir_info for %lu\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000341 ino);
342 fatal_error(0);
343 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000344 if (check_dotdot(fs, dirent, dir, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000345 dir_modified++;
346 } else if (dirent->inode == ino) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000347 if (fix_problem(fs, PR_2_LINK_DOT, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000348 dirent->inode = 0;
349 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000350 goto next;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000351 }
352 }
353 if (!dirent->inode)
354 goto next;
355
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000356 if (check_name(fs, dirent, ino, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000357 dir_modified++;
358
359 /*
360 * Make sure the inode listed is a legal one.
361 */
362 if (((dirent->inode != EXT2_ROOT_INO) &&
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000363 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000364 (dirent->inode > fs->super->s_inodes_count)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000365 if (fix_problem(fs, PR_2_BAD_INO, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000366 dirent->inode = 0;
367 dir_modified++;
368 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000369 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000370 }
371
372 /*
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000373 * If the inode is unused, offer to clear it.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000374 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000375 if (!(ext2fs_test_inode_bitmap(inode_used_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000376 dirent->inode))) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000377 if (fix_problem(fs, PR_2_UNUSED_INODE, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000378 dirent->inode = 0;
379 dir_modified++;
380 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000381 }
382 }
383
384 /*
385 * If the inode is in a bad block, offer to clear it.
386 */
387 if (inode_bb_map &&
388 (ext2fs_test_inode_bitmap(inode_bb_map,
389 dirent->inode))) {
390 if (fix_problem(fs, PR_2_BB_INODE, &cd->pctx)) {
391 dirent->inode = 0;
392 dir_modified++;
393 goto next;
394 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000395 }
396
397 /*
398 * If the inode was marked as having bad fields in
399 * pass1, process it and offer to fix/clear it.
400 * (We wait until now so that we can display the
401 * pathname to the user.)
402 */
403 if (inode_bad_map &&
Theodore Ts'of3db3561997-04-26 13:34:30 +0000404 ext2fs_test_inode_bitmap(inode_bad_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000405 dirent->inode)) {
406 if (process_bad_inode(fs, ino, dirent->inode)) {
407 dirent->inode = 0;
408 dir_modified++;
409 goto next;
410 }
411 }
412
413 /*
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000414 * Don't allow links to the root directory. We check
415 * this specially to make sure we catch this error
416 * case even if the root directory hasn't been created
417 * yet.
418 */
419 if ((dot_state > 2) && (dirent->inode == EXT2_ROOT_INO)) {
420 if (fix_problem(fs, PR_2_LINK_ROOT, &cd->pctx)) {
421 dirent->inode = 0;
422 dir_modified++;
423 goto next;
424 }
425 }
426
427 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000428 * If this is a directory, then mark its parent in its
429 * dir_info structure. If the parent field is already
430 * filled in, then this directory has more than one
431 * hard link. We assume the first link is correct,
432 * and ask the user if he/she wants to clear this one.
433 */
434 if ((dot_state > 2) &&
Theodore Ts'of3db3561997-04-26 13:34:30 +0000435 (ext2fs_test_inode_bitmap(inode_dir_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000436 dirent->inode))) {
437 subdir = get_dir_info(dirent->inode);
438 if (!subdir) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000439 printf("INTERNAL ERROR: missing dir %u\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000440 dirent->inode);
441 fatal_error(0);
442 }
443 if (subdir->parent) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000444 cd->pctx.ino2 = subdir->parent;
445 if (fix_problem(fs, PR_2_LINK_DIR,
446 &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000447 dirent->inode = 0;
448 dir_modified++;
449 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000450 }
451 cd->pctx.ino2 = 0;
452 } else
453 subdir->parent = ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000454 }
455
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000456 ext2fs_icount_increment(inode_count, dirent->inode, &links);
457 if (links > 1)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000458 fs_links_count++;
459 fs_total_count++;
460 next:
461 offset += dirent->rec_len;
462 } while (offset < fs->blocksize);
463#if 0
464 printf("\n");
465#endif
466 if (offset != fs->blocksize) {
467 printf("Final rec_len is %d, should be %d\n",
468 dirent->rec_len,
469 dirent->rec_len - fs->blocksize + offset);
470 }
471 if (dir_modified) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000472 retval = ext2fs_write_dir_block(fs, block_nr, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000473 if (retval) {
474 com_err(program_name, retval,
475 "while writing directory block %d", block_nr);
476 }
477 ext2fs_mark_changed(fs);
478 }
479 return 0;
480}
481
482/*
483 * This function is called to deallocate a block, and is an interator
484 * functioned called by deallocate inode via ext2fs_iterate_block().
485 */
486static int deallocate_inode_block(ext2_filsys fs,
487 blk_t *block_nr,
488 int blockcnt,
489 void *private)
490{
491 if (!*block_nr)
492 return 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000493 ext2fs_unmark_block_bitmap(block_found_map, *block_nr);
494 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000495 return 0;
496}
497
498/*
499 * This fuction deallocates an inode
500 */
501static void deallocate_inode(ext2_filsys fs, ino_t ino,
502 char* block_buf)
503{
504 errcode_t retval;
505 struct ext2_inode inode;
506
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000507 ext2fs_icount_store(inode_link_info, ino, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000508 e2fsck_read_inode(fs, ino, &inode, "deallocate_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000509 inode.i_links_count = 0;
510 inode.i_dtime = time(0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000511 e2fsck_write_inode(fs, ino, &inode, "deallocate_inode");
512
Theodore Ts'o3839e651997-04-26 13:21:57 +0000513 /*
514 * Fix up the bitmaps...
515 */
516 read_bitmaps(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000517 ext2fs_unmark_inode_bitmap(inode_used_map, ino);
518 ext2fs_unmark_inode_bitmap(inode_dir_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000519 if (inode_bad_map)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000520 ext2fs_unmark_inode_bitmap(inode_bad_map, ino);
521 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000522 ext2fs_mark_ib_dirty(fs);
523
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000524 if (!ext2fs_inode_has_valid_blocks(&inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000525 return;
526
527 ext2fs_mark_bb_dirty(fs);
528 retval = ext2fs_block_iterate(fs, ino, 0, block_buf,
529 deallocate_inode_block, 0);
530 if (retval)
531 com_err("deallocate_inode", retval,
532 "while calling ext2fs_block_iterate for inode %d",
533 ino);
534}
535
Theodore Ts'o3839e651997-04-26 13:21:57 +0000536static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino)
537{
538 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000539 int inode_modified = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000540 unsigned char *frag, *fsize;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000541 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000542
Theodore Ts'of3db3561997-04-26 13:34:30 +0000543 e2fsck_read_inode(fs, ino, &inode, "process_bad_inode");
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000544
545 clear_problem_context(&pctx);
546 pctx.ino = ino;
547 pctx.dir = dir;
548 pctx.inode = &inode;
549
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000550 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
551 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
552 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
553 !(LINUX_S_ISSOCK(inode.i_mode))) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000554 if (fix_problem(fs, PR_2_BAD_MODE, &pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000555 deallocate_inode(fs, ino, 0);
556 return 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000557 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000558 }
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000559
560 if (LINUX_S_ISCHR(inode.i_mode)
561 && !e2fsck_pass1_check_device_inode(&inode)) {
562 if (fix_problem(fs, PR_2_BAD_CHAR_DEV, &pctx)) {
563 deallocate_inode(fs, ino, 0);
564 return 1;
565 }
566 }
567
568 if (LINUX_S_ISBLK(inode.i_mode)
569 && !e2fsck_pass1_check_device_inode(&inode)) {
570 if (fix_problem(fs, PR_2_BAD_BLOCK_DEV, &pctx)) {
571 deallocate_inode(fs, ino, 0);
572 return 1;
573 }
574 }
575
576
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000577 if (inode.i_faddr &&
578 fix_problem(fs, PR_2_FADDR_ZERO, &pctx)) {
579 inode.i_faddr = 0;
580 inode_modified++;
581 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000582
583 switch (fs->super->s_creator_os) {
584 case EXT2_OS_LINUX:
585 frag = &inode.osd2.linux2.l_i_frag;
586 fsize = &inode.osd2.linux2.l_i_fsize;
587 break;
588 case EXT2_OS_HURD:
589 frag = &inode.osd2.hurd2.h_i_frag;
590 fsize = &inode.osd2.hurd2.h_i_fsize;
591 break;
592 case EXT2_OS_MASIX:
593 frag = &inode.osd2.masix2.m_i_frag;
594 fsize = &inode.osd2.masix2.m_i_fsize;
595 break;
596 default:
597 frag = fsize = 0;
598 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000599 if (frag && *frag) {
600 pctx.num = *frag;
601 if (fix_problem(fs, PR_2_FRAG_ZERO, &pctx)) {
602 *frag = 0;
603 inode_modified++;
604 }
605 pctx.num = 0;
606 }
607 if (fsize && *fsize) {
608 pctx.num = *fsize;
609 if (fix_problem(fs, PR_2_FSIZE_ZERO, &pctx)) {
610 *fsize = 0;
611 inode_modified++;
612 }
613 pctx.num = 0;
614 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000615
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000616 if (inode.i_file_acl &&
617 fix_problem(fs, PR_2_FILE_ACL_ZERO, &pctx)) {
618 inode.i_file_acl = 0;
619 inode_modified++;
620 }
621 if (inode.i_dir_acl &&
622 fix_problem(fs, PR_2_DIR_ACL_ZERO, &pctx)) {
623 inode.i_dir_acl = 0;
624 inode_modified++;
625 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000626 if (inode_modified)
627 e2fsck_write_inode(fs, ino, &inode, "process_bad_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628 return 0;
629}
630
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000631
632/*
633 * allocate_dir_block --- this function allocates a new directory
634 * block for a particular inode; this is done if a directory has
635 * a "hole" in it, or if a directory has a illegal block number
636 * that was zeroed out and now needs to be replaced.
637 */
638static int allocate_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000639 struct ext2_db_entry *db,
640 char *buf, struct problem_context *pctx)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000641{
642 blk_t blk;
643 char *block;
644 struct ext2_inode inode;
645 errcode_t retval;
646
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000647 if (fix_problem(fs, PR_2_DIRECTORY_HOLE, pctx) == 0)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000648 return 1;
649
650 /*
651 * Read the inode and block bitmaps in; we'll be messing with
652 * them.
653 */
654 read_bitmaps(fs);
655
656 /*
657 * First, find a free block
658 */
659 retval = ext2fs_new_block(fs, 0, block_found_map, &blk);
660 if (retval) {
661 com_err("allocate_dir_block", retval,
662 "while trying to fill a hole in a directory inode");
663 return 1;
664 }
665 ext2fs_mark_block_bitmap(block_found_map, blk);
666 ext2fs_mark_block_bitmap(fs->block_map, blk);
667 ext2fs_mark_bb_dirty(fs);
668
669 /*
670 * Now let's create the actual data block for the inode
671 */
672 if (db->blockcnt)
673 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
674 else
675 retval = ext2fs_new_dir_block(fs, db->ino, EXT2_ROOT_INO,
676 &block);
677
678 if (retval) {
679 com_err("allocate_dir_block", retval,
680 "while creating new directory block");
681 return 1;
682 }
683
684 retval = ext2fs_write_dir_block(fs, blk, block);
685 free(block);
686 if (retval) {
687 com_err("allocate_dir_block", retval,
688 "while writing an empty directory block");
689 return 1;
690 }
691
692 /*
693 * Update the inode block count
694 */
695 e2fsck_read_inode(fs, db->ino, &inode, "allocate_dir_block");
696 inode.i_blocks += fs->blocksize / 512;
697 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
698 inode.i_size = (db->blockcnt+1) * fs->blocksize;
699 e2fsck_write_inode(fs, db->ino, &inode, "allocate_dir_block");
700
701 /*
702 * Finally, update the block pointers for the inode
703 */
704 db->blk = blk;
705 retval = ext2fs_block_iterate(fs, db->ino, BLOCK_FLAG_HOLE,
706 0, update_dir_block, db);
707 if (retval) {
708 com_err("allocate_dir_block", retval,
709 "while calling ext2fs_block_iterate");
710 return 1;
711 }
712
713 return 0;
714}
715
716/*
717 * This is a helper function for allocate_dir_block().
718 */
719static int update_dir_block(ext2_filsys fs,
720 blk_t *block_nr,
721 int blockcnt,
722 void *private)
723{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000724 struct ext2_db_entry *db = private;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000725
726 if (db->blockcnt == blockcnt) {
727 *block_nr = db->blk;
728 return BLOCK_CHANGED;
729 }
730 return 0;
731}
732