]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - e2fsck/pass2.c
Many files:
[thirdparty/e2fsprogs.git] / e2fsck / pass2.c
1 /*
2 * pass2.c --- check directory structure
3 *
4 * 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%
10 *
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
38 *
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"
46 #include "problem.h"
47
48 /*
49 * Keeps track of how many times an inode is referenced.
50 */
51 ext2_icount_t inode_count = 0;
52
53 static void deallocate_inode(ext2_filsys fs, ino_t ino,
54 char* block_buf);
55 static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino);
56 static int check_dir_block(ext2_filsys fs,
57 struct ext2_db_entry *dir_blocks_info,
58 void *private);
59 static int allocate_dir_block(ext2_filsys fs,
60 struct ext2_db_entry *dir_blocks_info,
61 char *buf, struct problem_context *pctx);
62 static int update_dir_block(ext2_filsys fs,
63 blk_t *block_nr,
64 int blockcnt,
65 void *private);
66
67 struct check_dir_struct {
68 char *buf;
69 struct problem_context pctx;
70 };
71
72 void pass2(ext2_filsys fs)
73 {
74 char *buf;
75 struct resource_track rtrack;
76 struct dir_info *dir;
77 errcode_t retval;
78 ino_t size;
79 struct check_dir_struct cd;
80
81 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");
89 size = ext2fs_get_icount_size(inode_link_info) + 10;
90 retval = ext2fs_create_icount(fs, EXT2_ICOUNT_OPT_INCREMENT,
91 size, &inode_count);
92 if (retval) {
93 com_err("ext2fs_create_icount", retval,
94 "while creating inode_count");
95 fatal_error(0);
96 }
97 buf = allocate_memory(fs->blocksize, "directory scan buffer");
98
99 /*
100 * Set up the parent pointer for the root directory, if
101 * present. (If the root directory is not present, we will
102 * create it in pass 3.)
103 */
104 dir = get_dir_info(EXT2_ROOT_INO);
105 if (dir)
106 dir->parent = EXT2_ROOT_INO;
107
108 cd.buf = buf;
109 clear_problem_context(&cd.pctx);
110
111 retval = ext2fs_dblist_iterate(fs->dblist, check_dir_block, &cd);
112
113 free(buf);
114 ext2fs_free_dblist(fs->dblist);
115
116 if (inode_bad_map) {
117 ext2fs_free_inode_bitmap(inode_bad_map);
118 inode_bad_map = 0;
119 }
120 if (tflag > 1) {
121 printf("Pass 2: ");
122 print_resource_track(&rtrack);
123 }
124 }
125
126 /*
127 * Make sure the first entry in the directory is '.', and that the
128 * directory entry is sane.
129 */
130 static int check_dot(ext2_filsys fs,
131 struct ext2_dir_entry *dirent,
132 ino_t ino, struct problem_context *pctx)
133 {
134 struct ext2_dir_entry *nextdir;
135 int status = 0;
136 int created = 0;
137 int new_len;
138 int problem = 0;
139
140 if (!dirent->inode)
141 problem = PR_2_MISSING_DOT;
142 else if ((dirent->name_len != 1) ||
143 (dirent->name[0] != '.'))
144 problem = PR_2_1ST_NOT_DOT;
145 else if (dirent->name[1] != '\0')
146 problem = PR_2_DOT_NULL_TERM;
147
148 if (problem) {
149 if (fix_problem(fs, problem, pctx)) {
150 if (dirent->rec_len < 12)
151 dirent->rec_len = 12;
152 dirent->inode = ino;
153 dirent->name_len = 1;
154 dirent->name[0] = '.';
155 dirent->name[1] = '\0';
156 status = 1;
157 created = 1;
158 }
159 }
160 if (dirent->inode != ino) {
161 if (fix_problem(fs, PR_2_BAD_INODE_DOT, pctx)) {
162 dirent->inode = ino;
163 status = 1;
164 }
165 }
166 if (dirent->rec_len > 12) {
167 new_len = dirent->rec_len - 12;
168 if (new_len > 12) {
169 preenhalt(fs);
170 if (created ||
171 ask("Directory entry for '.' is big. Split", 1)) {
172 nextdir = (struct ext2_dir_entry *)
173 ((char *) dirent + 12);
174 dirent->rec_len = 12;
175 nextdir->rec_len = new_len;
176 nextdir->inode = 0;
177 nextdir->name_len = 0;
178 status = 1;
179 }
180 }
181 }
182 return status;
183 }
184
185 /*
186 * Make sure the second entry in the directory is '..', and that the
187 * directory entry is sane. We do not check the inode number of '..'
188 * here; this gets done in pass 3.
189 */
190 static int check_dotdot(ext2_filsys fs,
191 struct ext2_dir_entry *dirent,
192 struct dir_info *dir, struct problem_context *pctx)
193 {
194 int problem = 0;
195
196 if (!dirent->inode)
197 problem = PR_2_MISSING_DOT_DOT;
198 else if ((dirent->name_len != 2) ||
199 (dirent->name[0] != '.') ||
200 (dirent->name[1] != '.'))
201 problem = PR_2_2ND_NOT_DOT_DOT;
202 else if (dirent->name[2] != '\0')
203 problem = PR_2_DOT_DOT_NULL_TERM;
204
205 if (problem) {
206 if (fix_problem(fs, problem, pctx)) {
207 if (dirent->rec_len < 12)
208 dirent->rec_len = 12;
209 /*
210 * Note: we don't have the parent inode just
211 * yet, so we will fill it in with the root
212 * inode. This will get fixed in pass 3.
213 */
214 dirent->inode = EXT2_ROOT_INO;
215 dirent->name_len = 2;
216 dirent->name[0] = '.';
217 dirent->name[1] = '.';
218 dirent->name[2] = '\0';
219 return 1;
220 }
221 return 0;
222 }
223 dir->dotdot = dirent->inode;
224 return 0;
225 }
226
227 /*
228 * Check to make sure a directory entry doesn't contain any illegal
229 * characters.
230 */
231 static int check_name(ext2_filsys fs,
232 struct ext2_dir_entry *dirent,
233 ino_t dir_ino, struct problem_context *pctx)
234 {
235 int i;
236 int fixup = -1;
237 int ret = 0;
238
239 for ( i = 0; i < dirent->name_len; i++) {
240 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
241 if (fixup < 0) {
242 fixup = fix_problem(fs, PR_2_BAD_NAME, pctx);
243 }
244 if (fixup) {
245 dirent->name[i] = '.';
246 ret = 1;
247 }
248 }
249 }
250 return ret;
251 }
252
253 static int check_dir_block(ext2_filsys fs,
254 struct ext2_db_entry *db,
255 void *private)
256 {
257 struct dir_info *subdir, *dir;
258 struct ext2_dir_entry *dirent;
259 int offset = 0;
260 int dir_modified = 0;
261 errcode_t retval;
262 int dot_state;
263 blk_t block_nr = db->blk;
264 ino_t ino = db->ino;
265 __u16 links;
266 struct check_dir_struct *cd = private;
267 char *buf = cd->buf;
268
269 /*
270 * Make sure the inode is still in use (could have been
271 * deleted in the duplicate/bad blocks pass.
272 */
273 if (!(ext2fs_test_inode_bitmap(inode_used_map, ino)))
274 return 0;
275
276 cd->pctx.ino = ino;
277 cd->pctx.blk = block_nr;
278 cd->pctx.blkcount = db->blockcnt;
279 cd->pctx.ino2 = 0;
280 cd->pctx.dirent = 0;
281 cd->pctx.num = 0;
282
283 if (db->blk == 0) {
284 if (allocate_dir_block(fs, db, buf, &cd->pctx))
285 return 0;
286 block_nr = db->blk;
287 }
288
289 if (db->blockcnt)
290 dot_state = 2;
291 else
292 dot_state = 0;
293
294 #if 0
295 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
296 db->blockcnt, ino);
297 #endif
298
299 retval = ext2fs_read_dir_block(fs, block_nr, buf);
300 if (retval) {
301 com_err(program_name, retval,
302 "while reading directory block %d", block_nr);
303 }
304
305 do {
306 dot_state++;
307 dirent = (struct ext2_dir_entry *) (buf + offset);
308 cd->pctx.dirent = dirent;
309 cd->pctx.num = offset;
310 if (((offset + dirent->rec_len) > fs->blocksize) ||
311 (dirent->rec_len < 8) ||
312 ((dirent->rec_len % 4) != 0) ||
313 ((dirent->name_len+8) > dirent->rec_len)) {
314 if (fix_problem(fs, PR_2_DIR_CORRUPTED, &cd->pctx)) {
315 dirent->rec_len = fs->blocksize - offset;
316 dirent->name_len = 0;
317 dirent->inode = 0;
318 dir_modified++;
319 } else
320 return DIRENT_ABORT;
321 }
322
323 if (dirent->name_len > EXT2_NAME_LEN) {
324 if (fix_problem(fs, PR_2_FILENAME_LONG, &cd->pctx)) {
325 dirent->name_len = EXT2_NAME_LEN;
326 dir_modified++;
327 }
328 }
329
330 if (dot_state == 1) {
331 if (check_dot(fs, dirent, ino, &cd->pctx))
332 dir_modified++;
333 } else if (dot_state == 2) {
334 dir = get_dir_info(ino);
335 if (!dir) {
336 printf("Internal error: couldn't find dir_info for %lu\n",
337 ino);
338 fatal_error(0);
339 }
340 if (check_dotdot(fs, dirent, dir, &cd->pctx))
341 dir_modified++;
342 } else if (dirent->inode == ino) {
343 if (fix_problem(fs, PR_2_LINK_DOT, &cd->pctx)) {
344 dirent->inode = 0;
345 dir_modified++;
346 goto next;
347 }
348 }
349 if (!dirent->inode)
350 goto next;
351
352 if (check_name(fs, dirent, ino, &cd->pctx))
353 dir_modified++;
354
355 /*
356 * Make sure the inode listed is a legal one.
357 */
358 if (((dirent->inode != EXT2_ROOT_INO) &&
359 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
360 (dirent->inode > fs->super->s_inodes_count)) {
361 if (fix_problem(fs, PR_2_BAD_INO, &cd->pctx)) {
362 dirent->inode = 0;
363 dir_modified++;
364 goto next;
365 }
366 }
367
368 /*
369 * If the inode is unused, offer to clear it.
370 */
371 if (!(ext2fs_test_inode_bitmap(inode_used_map,
372 dirent->inode))) {
373 if (fix_problem(fs, PR_2_UNUSED_INODE, &cd->pctx)) {
374 dirent->inode = 0;
375 dir_modified++;
376 goto next;
377 }
378 }
379
380 /*
381 * If the inode is in a bad block, offer to clear it.
382 */
383 if (inode_bb_map &&
384 (ext2fs_test_inode_bitmap(inode_bb_map,
385 dirent->inode))) {
386 if (fix_problem(fs, PR_2_BB_INODE, &cd->pctx)) {
387 dirent->inode = 0;
388 dir_modified++;
389 goto next;
390 }
391 }
392
393 /*
394 * If the inode was marked as having bad fields in
395 * pass1, process it and offer to fix/clear it.
396 * (We wait until now so that we can display the
397 * pathname to the user.)
398 */
399 if (inode_bad_map &&
400 ext2fs_test_inode_bitmap(inode_bad_map,
401 dirent->inode)) {
402 if (process_bad_inode(fs, ino, dirent->inode)) {
403 dirent->inode = 0;
404 dir_modified++;
405 goto next;
406 }
407 }
408
409 /*
410 * Don't allow links to the root directory. We check
411 * this specially to make sure we catch this error
412 * case even if the root directory hasn't been created
413 * yet.
414 */
415 if ((dot_state > 2) && (dirent->inode == EXT2_ROOT_INO)) {
416 if (fix_problem(fs, PR_2_LINK_ROOT, &cd->pctx)) {
417 dirent->inode = 0;
418 dir_modified++;
419 goto next;
420 }
421 }
422
423 /*
424 * If this is a directory, then mark its parent in its
425 * dir_info structure. If the parent field is already
426 * filled in, then this directory has more than one
427 * hard link. We assume the first link is correct,
428 * and ask the user if he/she wants to clear this one.
429 */
430 if ((dot_state > 2) &&
431 (ext2fs_test_inode_bitmap(inode_dir_map,
432 dirent->inode))) {
433 subdir = get_dir_info(dirent->inode);
434 if (!subdir) {
435 printf("INTERNAL ERROR: missing dir %u\n",
436 dirent->inode);
437 fatal_error(0);
438 }
439 if (subdir->parent) {
440 cd->pctx.ino2 = subdir->parent;
441 if (fix_problem(fs, PR_2_LINK_DIR,
442 &cd->pctx)) {
443 dirent->inode = 0;
444 dir_modified++;
445 goto next;
446 }
447 cd->pctx.ino2 = 0;
448 } else
449 subdir->parent = ino;
450 }
451
452 ext2fs_icount_increment(inode_count, dirent->inode, &links);
453 if (links > 1)
454 fs_links_count++;
455 fs_total_count++;
456 next:
457 offset += dirent->rec_len;
458 } while (offset < fs->blocksize);
459 #if 0
460 printf("\n");
461 #endif
462 if (offset != fs->blocksize) {
463 printf("Final rec_len is %d, should be %d\n",
464 dirent->rec_len,
465 dirent->rec_len - fs->blocksize + offset);
466 }
467 if (dir_modified) {
468 retval = ext2fs_write_dir_block(fs, block_nr, buf);
469 if (retval) {
470 com_err(program_name, retval,
471 "while writing directory block %d", block_nr);
472 }
473 ext2fs_mark_changed(fs);
474 }
475 return 0;
476 }
477
478 /*
479 * This function is called to deallocate a block, and is an interator
480 * functioned called by deallocate inode via ext2fs_iterate_block().
481 */
482 static int deallocate_inode_block(ext2_filsys fs,
483 blk_t *block_nr,
484 int blockcnt,
485 void *private)
486 {
487 if (!*block_nr)
488 return 0;
489 ext2fs_unmark_block_bitmap(block_found_map, *block_nr);
490 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
491 return 0;
492 }
493
494 /*
495 * This fuction deallocates an inode
496 */
497 static void deallocate_inode(ext2_filsys fs, ino_t ino,
498 char* block_buf)
499 {
500 errcode_t retval;
501 struct ext2_inode inode;
502
503 ext2fs_icount_store(inode_link_info, ino, 0);
504 e2fsck_read_inode(fs, ino, &inode, "deallocate_inode");
505 inode.i_links_count = 0;
506 inode.i_dtime = time(0);
507 e2fsck_write_inode(fs, ino, &inode, "deallocate_inode");
508
509 /*
510 * Fix up the bitmaps...
511 */
512 read_bitmaps(fs);
513 ext2fs_unmark_inode_bitmap(inode_used_map, ino);
514 ext2fs_unmark_inode_bitmap(inode_dir_map, ino);
515 if (inode_bad_map)
516 ext2fs_unmark_inode_bitmap(inode_bad_map, ino);
517 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
518 ext2fs_mark_ib_dirty(fs);
519
520 if (!ext2fs_inode_has_valid_blocks(&inode))
521 return;
522
523 ext2fs_mark_bb_dirty(fs);
524 retval = ext2fs_block_iterate(fs, ino, 0, block_buf,
525 deallocate_inode_block, 0);
526 if (retval)
527 com_err("deallocate_inode", retval,
528 "while calling ext2fs_block_iterate for inode %d",
529 ino);
530 }
531
532 static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino)
533 {
534 struct ext2_inode inode;
535 int inode_modified = 0;
536 unsigned char *frag, *fsize;
537 struct problem_context pctx;
538
539 e2fsck_read_inode(fs, ino, &inode, "process_bad_inode");
540
541 clear_problem_context(&pctx);
542 pctx.ino = ino;
543 pctx.dir = dir;
544 pctx.inode = &inode;
545
546 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
547 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
548 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
549 !(LINUX_S_ISSOCK(inode.i_mode))) {
550 if (fix_problem(fs, PR_2_BAD_MODE, &pctx)) {
551 deallocate_inode(fs, ino, 0);
552 return 1;
553 }
554 }
555 if (inode.i_faddr &&
556 fix_problem(fs, PR_2_FADDR_ZERO, &pctx)) {
557 inode.i_faddr = 0;
558 inode_modified++;
559 }
560
561 switch (fs->super->s_creator_os) {
562 case EXT2_OS_LINUX:
563 frag = &inode.osd2.linux2.l_i_frag;
564 fsize = &inode.osd2.linux2.l_i_fsize;
565 break;
566 case EXT2_OS_HURD:
567 frag = &inode.osd2.hurd2.h_i_frag;
568 fsize = &inode.osd2.hurd2.h_i_fsize;
569 break;
570 case EXT2_OS_MASIX:
571 frag = &inode.osd2.masix2.m_i_frag;
572 fsize = &inode.osd2.masix2.m_i_fsize;
573 break;
574 default:
575 frag = fsize = 0;
576 }
577 if (frag && *frag) {
578 pctx.num = *frag;
579 if (fix_problem(fs, PR_2_FRAG_ZERO, &pctx)) {
580 *frag = 0;
581 inode_modified++;
582 }
583 pctx.num = 0;
584 }
585 if (fsize && *fsize) {
586 pctx.num = *fsize;
587 if (fix_problem(fs, PR_2_FSIZE_ZERO, &pctx)) {
588 *fsize = 0;
589 inode_modified++;
590 }
591 pctx.num = 0;
592 }
593
594 if (inode.i_file_acl &&
595 fix_problem(fs, PR_2_FILE_ACL_ZERO, &pctx)) {
596 inode.i_file_acl = 0;
597 inode_modified++;
598 }
599 if (inode.i_dir_acl &&
600 fix_problem(fs, PR_2_DIR_ACL_ZERO, &pctx)) {
601 inode.i_dir_acl = 0;
602 inode_modified++;
603 }
604 if (inode_modified)
605 e2fsck_write_inode(fs, ino, &inode, "process_bad_inode");
606 return 0;
607 }
608
609
610 /*
611 * allocate_dir_block --- this function allocates a new directory
612 * block for a particular inode; this is done if a directory has
613 * a "hole" in it, or if a directory has a illegal block number
614 * that was zeroed out and now needs to be replaced.
615 */
616 static int allocate_dir_block(ext2_filsys fs,
617 struct ext2_db_entry *db,
618 char *buf, struct problem_context *pctx)
619 {
620 blk_t blk;
621 char *block;
622 struct ext2_inode inode;
623 errcode_t retval;
624
625 if (fix_problem(fs, PR_2_DIRECTORY_HOLE, pctx) == 0)
626 return 1;
627
628 /*
629 * Read the inode and block bitmaps in; we'll be messing with
630 * them.
631 */
632 read_bitmaps(fs);
633
634 /*
635 * First, find a free block
636 */
637 retval = ext2fs_new_block(fs, 0, block_found_map, &blk);
638 if (retval) {
639 com_err("allocate_dir_block", retval,
640 "while trying to fill a hole in a directory inode");
641 return 1;
642 }
643 ext2fs_mark_block_bitmap(block_found_map, blk);
644 ext2fs_mark_block_bitmap(fs->block_map, blk);
645 ext2fs_mark_bb_dirty(fs);
646
647 /*
648 * Now let's create the actual data block for the inode
649 */
650 if (db->blockcnt)
651 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
652 else
653 retval = ext2fs_new_dir_block(fs, db->ino, EXT2_ROOT_INO,
654 &block);
655
656 if (retval) {
657 com_err("allocate_dir_block", retval,
658 "while creating new directory block");
659 return 1;
660 }
661
662 retval = ext2fs_write_dir_block(fs, blk, block);
663 free(block);
664 if (retval) {
665 com_err("allocate_dir_block", retval,
666 "while writing an empty directory block");
667 return 1;
668 }
669
670 /*
671 * Update the inode block count
672 */
673 e2fsck_read_inode(fs, db->ino, &inode, "allocate_dir_block");
674 inode.i_blocks += fs->blocksize / 512;
675 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
676 inode.i_size = (db->blockcnt+1) * fs->blocksize;
677 e2fsck_write_inode(fs, db->ino, &inode, "allocate_dir_block");
678
679 /*
680 * Finally, update the block pointers for the inode
681 */
682 db->blk = blk;
683 retval = ext2fs_block_iterate(fs, db->ino, BLOCK_FLAG_HOLE,
684 0, update_dir_block, db);
685 if (retval) {
686 com_err("allocate_dir_block", retval,
687 "while calling ext2fs_block_iterate");
688 return 1;
689 }
690
691 return 0;
692 }
693
694 /*
695 * This is a helper function for allocate_dir_block().
696 */
697 static int update_dir_block(ext2_filsys fs,
698 blk_t *block_nr,
699 int blockcnt,
700 void *private)
701 {
702 struct ext2_db_entry *db = private;
703
704 if (db->blockcnt == blockcnt) {
705 *block_nr = db->blk;
706 return BLOCK_CHANGED;
707 }
708 return 0;
709 }
710