2 * pass3.c -- pass #3 of e2fsck: Check for directory connectivity
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
11 * Pass #3 assures that all directories are connected to the
12 * filesystem tree, using the following algorithm:
14 * First, the root directory is checked to make sure it exists; if
15 * not, e2fsck will offer to create a new one. It is then marked as
18 * Then, pass3 interates over all directory inodes; for each directory
19 * it attempts to trace up the filesystem tree, using dirinfo.parent
20 * until it reaches a directory which has been marked "done". If it
21 * can not do so, then the directory must be disconnected, and e2fsck
22 * will offer to reconnect it to /lost+found. While it is chasing
23 * parent pointers up the filesystem tree, if pass3 sees a directory
24 * twice, then it has detected a filesystem loop, and it will again
25 * offer to reconnect the directory to /lost+found in to break the
28 * Pass 3 also contains the subroutine, e2fsck_reconnect_file() to
29 * reconnect inodes to /lost+found; this subroutine is also used by
30 * pass 4. e2fsck_reconnect_file() calls get_lost_and_found(), which
31 * is responsible for creating /lost+found if it does not exist.
33 * Pass 3 frees the following data structures:
34 * - The dirinfo directory information cache.
44 static void check_root(e2fsck_t ctx);
45 static int check_directory(e2fsck_t ctx, ext2_ino_t ino,
46 struct problem_context *pctx);
47 static void fix_dotdot(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent);
49 static ext2fs_inode_bitmap inode_loop_detect = 0;
50 static ext2fs_inode_bitmap inode_done_map = 0;
52 void e2fsck_pass3(e2fsck_t ctx)
54 ext2_filsys fs = ctx->fs;
55 struct dir_info_iter *iter;
57 struct resource_track rtrack;
59 struct problem_context pctx;
61 unsigned long maxdirs, count;
63 init_resource_track(&rtrack, ctx->fs->io);
64 clear_problem_context(&pctx);
67 mtrace_print("Pass 3");
70 if (!(ctx->options & E2F_OPT_PREEN))
71 fix_problem(ctx, PR_3_PASS_HEADER, &pctx);
74 * Allocate some bitmaps to do loop detection.
76 pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("inode done bitmap"),
80 fix_problem(ctx, PR_3_ALLOCATE_IBITMAP_ERROR, &pctx);
81 ctx->flags |= E2F_FLAG_ABORT;
84 print_resource_track(ctx, _("Peak memory"), &ctx->global_rtrack, NULL);
87 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
90 ext2fs_mark_inode_bitmap2(inode_done_map, EXT2_ROOT_INO);
92 maxdirs = e2fsck_get_num_dirinfo(ctx);
96 if ((ctx->progress)(ctx, 3, 0, maxdirs))
99 iter = e2fsck_dir_info_iter_begin(ctx);
100 while ((dir = e2fsck_dir_info_iter(ctx, iter)) != 0) {
101 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
103 if (ctx->progress && (ctx->progress)(ctx, 3, count++, maxdirs))
105 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dir->ino))
106 if (check_directory(ctx, dir->ino, &pctx))
109 e2fsck_dir_info_iter_end(ctx, iter);
112 * Force the creation of /lost+found if not present
114 if ((ctx->flags & E2F_OPT_READONLY) == 0)
115 e2fsck_get_lost_and_found(ctx, 1);
118 * If there are any directories that need to be indexed or
119 * optimized, do it here.
121 e2fsck_rehash_directories(ctx);
124 e2fsck_free_dir_info(ctx);
125 if (inode_loop_detect) {
126 ext2fs_free_inode_bitmap(inode_loop_detect);
127 inode_loop_detect = 0;
129 if (inode_done_map) {
130 ext2fs_free_inode_bitmap(inode_done_map);
134 print_resource_track(ctx, _("Pass 3"), &rtrack, ctx->fs->io);
138 * This makes sure the root inode is present; if not, we ask if the
139 * user wants us to create it. Not creating it is a fatal error.
141 static void check_root(e2fsck_t ctx)
143 ext2_filsys fs = ctx->fs;
145 struct ext2_inode inode;
147 struct problem_context pctx;
149 clear_problem_context(&pctx);
151 if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO)) {
153 * If the root inode is not a directory, die here. The
154 * user must have answered 'no' in pass1 when we
155 * offered to clear it.
157 if (!(ext2fs_test_inode_bitmap2(ctx->inode_dir_map,
159 fix_problem(ctx, PR_3_ROOT_NOT_DIR_ABORT, &pctx);
160 ctx->flags |= E2F_FLAG_ABORT;
165 if (!fix_problem(ctx, PR_3_NO_ROOT_INODE, &pctx)) {
166 fix_problem(ctx, PR_3_NO_ROOT_INODE_ABORT, &pctx);
167 ctx->flags |= E2F_FLAG_ABORT;
171 e2fsck_read_bitmaps(ctx);
174 * First, find a free block
176 pctx.errcode = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
178 pctx.str = "ext2fs_new_block";
179 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
180 ctx->flags |= E2F_FLAG_ABORT;
183 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
184 ext2fs_mark_block_bitmap2(fs->block_map, blk);
185 ext2fs_mark_bb_dirty(fs);
188 * Now let's create the actual data block for the inode
190 pctx.errcode = ext2fs_new_dir_block(fs, EXT2_ROOT_INO, EXT2_ROOT_INO,
193 pctx.str = "ext2fs_new_dir_block";
194 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
195 ctx->flags |= E2F_FLAG_ABORT;
199 pctx.errcode = ext2fs_write_dir_block(fs, blk, block);
201 pctx.str = "ext2fs_write_dir_block";
202 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
203 ctx->flags |= E2F_FLAG_ABORT;
206 ext2fs_free_mem(&block);
209 * Set up the inode structure
211 memset(&inode, 0, sizeof(inode));
212 inode.i_mode = 040755;
213 inode.i_size = fs->blocksize;
214 inode.i_atime = inode.i_ctime = inode.i_mtime = ctx->now;
215 inode.i_links_count = 2;
216 ext2fs_iblk_set(fs, &inode, 1);
217 inode.i_block[0] = blk;
220 * Write out the inode.
222 pctx.errcode = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
224 pctx.str = "ext2fs_write_inode";
225 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
226 ctx->flags |= E2F_FLAG_ABORT;
231 * Miscellaneous bookkeeping...
233 e2fsck_add_dir_info(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
234 ext2fs_icount_store(ctx->inode_count, EXT2_ROOT_INO, 2);
235 ext2fs_icount_store(ctx->inode_link_info, EXT2_ROOT_INO, 2);
237 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO);
238 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, EXT2_ROOT_INO);
239 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_ROOT_INO);
240 ext2fs_mark_ib_dirty(fs);
244 * This subroutine is responsible for making sure that a particular
245 * directory is connected to the root; if it isn't we trace it up as
246 * far as we can go, and then offer to connect the resulting parent to
247 * the lost+found. We have to do loop detection; if we ever discover
248 * a loop, we treat that as a disconnected directory and offer to
249 * reparent it to lost+found.
251 * However, loop detection is expensive, because for very large
252 * filesystems, the inode_loop_detect bitmap is huge, and clearing it
253 * is non-trivial. Loops in filesystems are also a rare error case,
254 * and we shouldn't optimize for error cases. So we try two passes of
255 * the algorithm. The first time, we ignore loop detection and merely
256 * increment a counter; if the counter exceeds some extreme threshold,
257 * then we try again with the loop detection bitmap enabled.
259 static int check_directory(e2fsck_t ctx, ext2_ino_t dir,
260 struct problem_context *pctx)
262 ext2_filsys fs = ctx->fs;
263 ext2_ino_t ino = dir, parent;
264 int loop_pass = 0, parent_count = 0;
268 * Mark this inode as being "done"; by the time we
269 * return from this function, the inode we either be
270 * verified as being connected to the directory tree,
271 * or we will have offered to reconnect this to
274 * If it was marked done already, then we've reached a
275 * parent we've already checked.
277 if (ext2fs_mark_inode_bitmap2(inode_done_map, ino))
280 if (e2fsck_dir_info_get_parent(ctx, ino, &parent)) {
281 fix_problem(ctx, PR_3_NO_DIRINFO, pctx);
286 * If this directory doesn't have a parent, or we've
287 * seen the parent once already, then offer to
288 * reparent it to lost+found
292 (ext2fs_test_inode_bitmap2(inode_loop_detect,
295 if (fix_problem(ctx, PR_3_UNCONNECTED_DIR, pctx)) {
296 if (e2fsck_reconnect_file(ctx, pctx->ino))
297 ext2fs_unmark_valid(fs);
299 fix_dotdot(ctx, pctx->ino,
300 ctx->lost_and_found);
301 parent = ctx->lost_and_found;
308 ext2fs_mark_inode_bitmap2(inode_loop_detect, ino);
309 } else if (parent_count++ > 2048) {
311 * If we've run into a path depth that's
312 * greater than 2048, try again with the inode
313 * loop bitmap turned on and start from the
317 if (inode_loop_detect)
318 ext2fs_clear_inode_bitmap(inode_loop_detect);
320 pctx->errcode = ext2fs_allocate_inode_bitmap(fs, _("inode loop detection bitmap"), &inode_loop_detect);
324 PR_3_ALLOCATE_IBITMAP_ERROR, pctx);
325 ctx->flags |= E2F_FLAG_ABORT;
334 * Make sure that .. and the parent directory are the same;
335 * offer to fix it if not.
338 if (e2fsck_dir_info_get_dotdot(ctx, dir, &pctx->ino2) ||
339 e2fsck_dir_info_get_parent(ctx, dir, &pctx->dir)) {
340 fix_problem(ctx, PR_3_NO_DIRINFO, pctx);
343 if (pctx->ino2 != pctx->dir) {
344 if (fix_problem(ctx, PR_3_BAD_DOT_DOT, pctx))
345 fix_dotdot(ctx, dir, pctx->dir);
351 * This routine gets the lost_and_found inode, making it a directory
354 ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
356 ext2_filsys fs = ctx->fs;
360 struct ext2_inode inode;
362 static const char name[] = "lost+found";
363 struct problem_context pctx;
365 if (ctx->lost_and_found)
366 return ctx->lost_and_found;
368 clear_problem_context(&pctx);
370 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name,
371 sizeof(name)-1, 0, &ino);
375 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, ino)) {
376 ctx->lost_and_found = ino;
380 /* Lost+found isn't a directory! */
384 if (!fix_problem(ctx, PR_3_LPF_NOTDIR, &pctx))
387 /* OK, unlink the old /lost+found file. */
388 pctx.errcode = ext2fs_unlink(fs, EXT2_ROOT_INO, name, ino, 0);
390 pctx.str = "ext2fs_unlink";
391 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
394 (void) e2fsck_dir_info_set_parent(ctx, ino, 0);
395 e2fsck_adjust_inode_count(ctx, ino, -1);
396 } else if (retval != EXT2_ET_FILE_NOT_FOUND) {
397 pctx.errcode = retval;
398 fix_problem(ctx, PR_3_ERR_FIND_LPF, &pctx);
400 if (!fix_problem(ctx, PR_3_NO_LF_DIR, 0))
404 * Read the inode and block bitmaps in; we'll be messing with
407 e2fsck_read_bitmaps(ctx);
410 * First, find a free block
412 retval = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
414 pctx.errcode = retval;
415 fix_problem(ctx, PR_3_ERR_LPF_NEW_BLOCK, &pctx);
418 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
419 ext2fs_block_alloc_stats2(fs, blk, +1);
422 * Next find a free inode.
424 retval = ext2fs_new_inode(fs, EXT2_ROOT_INO, 040700,
425 ctx->inode_used_map, &ino);
427 pctx.errcode = retval;
428 fix_problem(ctx, PR_3_ERR_LPF_NEW_INODE, &pctx);
431 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
432 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
433 ext2fs_inode_alloc_stats2(fs, ino, +1, 1);
436 * Now let's create the actual data block for the inode
438 retval = ext2fs_new_dir_block(fs, ino, EXT2_ROOT_INO, &block);
440 pctx.errcode = retval;
441 fix_problem(ctx, PR_3_ERR_LPF_NEW_DIR_BLOCK, &pctx);
445 retval = ext2fs_write_dir_block(fs, blk, block);
446 ext2fs_free_mem(&block);
448 pctx.errcode = retval;
449 fix_problem(ctx, PR_3_ERR_LPF_WRITE_BLOCK, &pctx);
454 * Set up the inode structure
456 memset(&inode, 0, sizeof(inode));
457 inode.i_mode = 040700;
458 inode.i_size = fs->blocksize;
459 inode.i_atime = inode.i_ctime = inode.i_mtime = ctx->now;
460 inode.i_links_count = 2;
461 ext2fs_iblk_set(fs, &inode, 1);
462 inode.i_block[0] = blk;
465 * Next, write out the inode.
467 pctx.errcode = ext2fs_write_new_inode(fs, ino, &inode);
469 pctx.str = "ext2fs_write_inode";
470 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
474 * Finally, create the directory link
476 pctx.errcode = ext2fs_link(fs, EXT2_ROOT_INO, name, ino, EXT2_FT_DIR);
478 pctx.str = "ext2fs_link";
479 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
484 * Miscellaneous bookkeeping that needs to be kept straight.
486 e2fsck_add_dir_info(ctx, ino, EXT2_ROOT_INO);
487 e2fsck_adjust_inode_count(ctx, EXT2_ROOT_INO, 1);
488 ext2fs_icount_store(ctx->inode_count, ino, 2);
489 ext2fs_icount_store(ctx->inode_link_info, ino, 2);
490 ctx->lost_and_found = ino;
491 quota_data_add(ctx->qctx, &inode, ino, fs->blocksize);
492 quota_data_inodes(ctx->qctx, &inode, ino, +1);
494 printf("/lost+found created; inode #%lu\n", ino);
500 * This routine will connect a file to lost+found
502 int e2fsck_reconnect_file(e2fsck_t ctx, ext2_ino_t ino)
504 ext2_filsys fs = ctx->fs;
507 struct problem_context pctx;
508 struct ext2_inode inode;
511 clear_problem_context(&pctx);
514 if (!ctx->bad_lost_and_found && !ctx->lost_and_found) {
515 if (e2fsck_get_lost_and_found(ctx, 1) == 0)
516 ctx->bad_lost_and_found++;
518 if (ctx->bad_lost_and_found) {
519 fix_problem(ctx, PR_3_NO_LPF, &pctx);
523 sprintf(name, "#%u", ino);
524 if (ext2fs_read_inode(fs, ino, &inode) == 0)
525 file_type = ext2_file_type(inode.i_mode);
526 retval = ext2fs_link(fs, ctx->lost_and_found, name, ino, file_type);
527 if (retval == EXT2_ET_DIR_NO_SPACE) {
528 if (!fix_problem(ctx, PR_3_EXPAND_LF_DIR, &pctx))
530 retval = e2fsck_expand_directory(ctx, ctx->lost_and_found,
533 pctx.errcode = retval;
534 fix_problem(ctx, PR_3_CANT_EXPAND_LPF, &pctx);
537 retval = ext2fs_link(fs, ctx->lost_and_found, name,
541 pctx.errcode = retval;
542 fix_problem(ctx, PR_3_CANT_RECONNECT, &pctx);
545 e2fsck_adjust_inode_count(ctx, ino, 1);
551 * Utility routine to adjust the inode counts on an inode.
553 errcode_t e2fsck_adjust_inode_count(e2fsck_t ctx, ext2_ino_t ino, int adj)
555 ext2_filsys fs = ctx->fs;
557 struct ext2_inode inode;
562 retval = ext2fs_read_inode(fs, ino, &inode);
567 printf("Adjusting link count for inode %lu by %d (from %d)\n", ino, adj,
568 inode.i_links_count);
572 ext2fs_icount_increment(ctx->inode_count, ino, 0);
573 if (inode.i_links_count == (__u16) ~0)
575 ext2fs_icount_increment(ctx->inode_link_info, ino, 0);
576 inode.i_links_count++;
577 } else if (adj == -1) {
578 ext2fs_icount_decrement(ctx->inode_count, ino, 0);
579 if (inode.i_links_count == 0)
581 ext2fs_icount_decrement(ctx->inode_link_info, ino, 0);
582 inode.i_links_count--;
585 retval = ext2fs_write_inode(fs, ino, &inode);
593 * Fix parent --- this routine fixes up the parent of a directory.
595 struct fix_dotdot_struct {
602 static int fix_dotdot_proc(struct ext2_dir_entry *dirent,
603 int offset EXT2FS_ATTR((unused)),
604 int blocksize EXT2FS_ATTR((unused)),
605 char *buf EXT2FS_ATTR((unused)),
608 struct fix_dotdot_struct *fp = (struct fix_dotdot_struct *) priv_data;
610 struct problem_context pctx;
612 if ((dirent->name_len & 0xFF) != 2)
614 if (strncmp(dirent->name, "..", 2))
617 clear_problem_context(&pctx);
619 retval = e2fsck_adjust_inode_count(fp->ctx, dirent->inode, -1);
621 pctx.errcode = retval;
622 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
624 retval = e2fsck_adjust_inode_count(fp->ctx, fp->parent, 1);
626 pctx.errcode = retval;
627 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
629 dirent->inode = fp->parent;
630 if (fp->ctx->fs->super->s_feature_incompat &
631 EXT2_FEATURE_INCOMPAT_FILETYPE)
632 dirent->name_len = (dirent->name_len & 0xFF) |
635 dirent->name_len = dirent->name_len & 0xFF;
638 return DIRENT_ABORT | DIRENT_CHANGED;
641 static void fix_dotdot(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent)
643 ext2_filsys fs = ctx->fs;
645 struct fix_dotdot_struct fp;
646 struct problem_context pctx;
654 printf("Fixing '..' of inode %lu to be %lu...\n", ino, parent);
657 clear_problem_context(&pctx);
659 retval = ext2fs_dir_iterate(fs, ino, DIRENT_FLAG_INCLUDE_EMPTY,
660 0, fix_dotdot_proc, &fp);
661 if (retval || !fp.done) {
662 pctx.errcode = retval;
663 fix_problem(ctx, retval ? PR_3_FIX_PARENT_ERR :
664 PR_3_FIX_PARENT_NOFIND, &pctx);
665 ext2fs_unmark_valid(fs);
667 (void) e2fsck_dir_info_set_dotdot(ctx, ino, parent);
668 if (e2fsck_dir_info_set_parent(ctx, ino, ctx->lost_and_found))
669 fix_problem(ctx, PR_3_NO_DIRINFO, &pctx);
675 * These routines are responsible for expanding a /lost+found if it is
679 struct expand_dir_struct {
681 e2_blkcnt_t guaranteed_size;
688 static int expand_dir_proc(ext2_filsys fs,
690 e2_blkcnt_t blockcnt,
691 blk64_t ref_block EXT2FS_ATTR((unused)),
692 int ref_offset EXT2FS_ATTR((unused)),
695 struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
697 static blk64_t last_blk = 0;
704 if (es->guaranteed_size && blockcnt >= es->guaranteed_size)
708 es->last_block = blockcnt;
713 retval = ext2fs_new_block2(fs, last_blk, ctx->block_found_map,
720 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
726 retval = ext2fs_write_dir_block(fs, new_blk, block);
728 retval = ext2fs_get_mem(fs->blocksize, &block);
733 memset(block, 0, fs->blocksize);
734 retval = io_channel_write_blk64(fs->io, new_blk, 1, block);
740 ext2fs_free_mem(&block);
742 ext2fs_mark_block_bitmap2(ctx->block_found_map, new_blk);
743 ext2fs_block_alloc_stats2(fs, new_blk, +1);
747 return (BLOCK_CHANGED | BLOCK_ABORT);
749 return BLOCK_CHANGED;
752 errcode_t e2fsck_expand_directory(e2fsck_t ctx, ext2_ino_t dir,
753 int num, int guaranteed_size)
755 ext2_filsys fs = ctx->fs;
757 struct expand_dir_struct es;
758 struct ext2_inode inode;
760 if (!(fs->flags & EXT2_FLAG_RW))
761 return EXT2_ET_RO_FILSYS;
764 * Read the inode and block bitmaps in; we'll be messing with
767 e2fsck_read_bitmaps(ctx);
769 retval = ext2fs_check_directory(fs, dir);
774 es.guaranteed_size = guaranteed_size;
780 retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_APPEND,
781 0, expand_dir_proc, &es);
787 * Update the size and block count fields in the inode.
789 retval = ext2fs_read_inode(fs, dir, &inode);
793 inode.i_size = (es.last_block + 1) * fs->blocksize;
794 ext2fs_iblk_add_blocks(fs, &inode, es.newblocks);
795 quota_data_add(ctx->qctx, &inode, dir, es.newblocks * fs->blocksize);
797 e2fsck_write_inode(ctx, dir, &inode, "expand_directory");