2 * pass3.c -- pass #3 of e2fsck: Check for directory connectivity
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 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 void check_directory(e2fsck_t ctx, struct dir_info *dir,
46 struct problem_context *pctx);
47 static ino_t get_lost_and_found(e2fsck_t ctx);
48 static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ino_t parent);
49 static errcode_t adjust_inode_count(e2fsck_t ctx, ino_t ino, int adj);
50 static errcode_t expand_directory(e2fsck_t ctx, ino_t dir);
52 static ino_t lost_and_found = 0;
53 static int bad_lost_and_found = 0;
55 static ext2fs_inode_bitmap inode_loop_detect = 0;
56 static ext2fs_inode_bitmap inode_done_map = 0;
58 void e2fsck_pass3(e2fsck_t ctx)
60 ext2_filsys fs = ctx->fs;
63 struct resource_track rtrack;
65 struct problem_context pctx;
67 unsigned long maxdirs, count;
70 init_resource_track(&rtrack);
73 clear_problem_context(&pctx);
76 mtrace_print("Pass 3");
79 if (!(ctx->options & E2F_OPT_PREEN))
80 fix_problem(ctx, PR_3_PASS_HEADER, &pctx);
83 * Allocate some bitmaps to do loop detection.
85 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
86 "inode loop detection bitmap", &inode_loop_detect);
89 fix_problem(ctx, PR_3_ALLOCATE_IBITMAP_ERROR, &pctx);
90 ctx->flags |= E2F_FLAG_ABORT;
93 pctx.errcode = ext2fs_allocate_inode_bitmap(fs, "inode done bitmap",
97 fix_problem(ctx, PR_3_ALLOCATE_IBITMAP_ERROR, &pctx);
98 ctx->flags |= E2F_FLAG_ABORT;
101 #ifdef RESOURCE_TRACK
102 if (ctx->options & E2F_OPT_TIME)
103 print_resource_track("Peak memory", &ctx->global_rtrack);
107 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
110 ext2fs_mark_inode_bitmap(inode_done_map, EXT2_ROOT_INO);
112 maxdirs = e2fsck_get_num_dirinfo(ctx);
116 if ((ctx->progress)(ctx, 3, 0, maxdirs))
119 for (i=0; (dir = e2fsck_dir_info_iter(ctx, &i)) != 0;) {
121 if ((ctx->progress)(ctx, 3, count++, maxdirs))
123 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dir->ino))
124 check_directory(ctx, dir, &pctx);
128 e2fsck_free_dir_info(ctx);
129 if (inode_loop_detect)
130 ext2fs_free_inode_bitmap(inode_loop_detect);
132 ext2fs_free_inode_bitmap(inode_done_map);
133 #ifdef RESOURCE_TRACK
134 if (ctx->options & E2F_OPT_TIME2)
135 print_resource_track("Pass 3", &rtrack);
140 * This makes sure the root inode is present; if not, we ask if the
141 * user wants us to create it. Not creating it is a fatal error.
143 static void check_root(e2fsck_t ctx)
145 ext2_filsys fs = ctx->fs;
147 struct ext2_inode inode;
149 struct problem_context pctx;
151 clear_problem_context(&pctx);
153 if (ext2fs_test_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO)) {
155 * If the root inode is not a directory, die here. The
156 * user must have answered 'no' in pass1 when we
157 * offered to clear it.
159 if (!(ext2fs_test_inode_bitmap(ctx->inode_dir_map,
161 fix_problem(ctx, PR_3_ROOT_NOT_DIR_ABORT, &pctx);
162 ctx->flags |= E2F_FLAG_ABORT;
167 if (!fix_problem(ctx, PR_3_NO_ROOT_INODE, &pctx)) {
168 fix_problem(ctx, PR_3_NO_ROOT_INODE_ABORT, &pctx);
169 ctx->flags |= E2F_FLAG_ABORT;
173 e2fsck_read_bitmaps(ctx);
176 * First, find a free block
178 pctx.errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
180 pctx.str = "ext2fs_new_block";
181 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
182 ctx->flags |= E2F_FLAG_ABORT;
185 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
186 ext2fs_mark_block_bitmap(fs->block_map, blk);
187 ext2fs_mark_bb_dirty(fs);
190 * Now let's create the actual data block for the inode
192 pctx.errcode = ext2fs_new_dir_block(fs, EXT2_ROOT_INO, EXT2_ROOT_INO,
195 pctx.str = "ext2fs_new_dir_block";
196 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
197 ctx->flags |= E2F_FLAG_ABORT;
201 pctx.errcode = ext2fs_write_dir_block(fs, blk, block);
203 pctx.str = "ext2fs_write_dir_block";
204 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
205 ctx->flags |= E2F_FLAG_ABORT;
208 ext2fs_free_mem((void **) &block);
211 * Set up the inode structure
213 memset(&inode, 0, sizeof(inode));
214 inode.i_mode = 040755;
215 inode.i_size = fs->blocksize;
216 inode.i_atime = inode.i_ctime = inode.i_mtime = time(0);
217 inode.i_links_count = 2;
218 inode.i_blocks = fs->blocksize / 512;
219 inode.i_block[0] = blk;
222 * Write out the inode.
224 pctx.errcode = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
226 pctx.str = "ext2fs_write_inode";
227 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
228 ctx->flags |= E2F_FLAG_ABORT;
233 * Miscellaneous bookkeeping...
235 e2fsck_add_dir_info(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
236 ext2fs_icount_store(ctx->inode_count, EXT2_ROOT_INO, 2);
237 ext2fs_icount_store(ctx->inode_link_info, EXT2_ROOT_INO, 2);
239 ext2fs_mark_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO);
240 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, EXT2_ROOT_INO);
241 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_ROOT_INO);
242 ext2fs_mark_ib_dirty(fs);
246 * This subroutine is responsible for making sure that a particular
247 * directory is connected to the root; if it isn't we trace it up as
248 * far as we can go, and then offer to connect the resulting parent to
249 * the lost+found. We have to do loop detection; if we ever discover
250 * a loop, we treat that as a disconnected directory and offer to
251 * reparent it to lost+found.
253 static void check_directory(e2fsck_t ctx, struct dir_info *dir,
254 struct problem_context *pctx)
256 ext2_filsys fs = ctx->fs;
257 struct dir_info *p = dir;
262 ext2fs_clear_inode_bitmap(inode_loop_detect);
265 * Keep going until we find a parent which we've already
266 * checked. We know it's either already connected to the
267 * directory tree, or it isn't but the user has already told
268 * us he doesn't want us to reconnect the disconnected
271 while (!ext2fs_test_inode_bitmap(inode_done_map, p->ino)) {
273 * Mark this inode as being "done"; by the time we
274 * return from this function, the inode we either be
275 * verified as being connected to the directory tree,
276 * or we will have offered to reconnect this to
279 ext2fs_mark_inode_bitmap(inode_done_map, p->ino);
282 * If this directory doesn't have a parent, or we've
283 * seen the parent once already, then offer to
284 * reparent it to lost+found
287 (ext2fs_test_inode_bitmap(inode_loop_detect,
290 if (fix_problem(ctx, PR_3_UNCONNECTED_DIR, pctx)) {
291 if (e2fsck_reconnect_file(ctx, p->ino))
292 ext2fs_unmark_valid(fs);
294 p->parent = lost_and_found;
295 fix_dotdot(ctx, p, lost_and_found);
300 ext2fs_mark_inode_bitmap(inode_loop_detect,
302 pctx->ino = p->parent;
303 p = e2fsck_get_dir_info(ctx, p->parent);
305 fix_problem(ctx, PR_3_NO_DIRINFO, pctx);
311 * Make sure that .. and the parent directory are the same;
312 * offer to fix it if not.
314 if (dir->parent != dir->dotdot) {
315 pctx->ino = dir->ino;
316 pctx->ino2 = dir->dotdot;
317 pctx->dir = dir->parent;
318 if (fix_problem(ctx, PR_3_BAD_DOT_DOT, pctx))
319 fix_dotdot(ctx, dir, dir->parent);
324 * This routine gets the lost_and_found inode, making it a directory
327 ino_t get_lost_and_found(e2fsck_t ctx)
329 ext2_filsys fs = ctx->fs;
333 struct ext2_inode inode;
335 const char name[] = "lost+found";
336 struct problem_context pctx;
338 clear_problem_context(&pctx);
340 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name,
341 sizeof(name)-1, 0, &ino);
344 if (retval != EXT2_ET_FILE_NOT_FOUND) {
345 pctx.errcode = retval;
346 fix_problem(ctx, PR_3_ERR_FIND_LPF, &pctx);
348 if (!fix_problem(ctx, PR_3_NO_LF_DIR, 0))
352 * Read the inode and block bitmaps in; we'll be messing with
355 e2fsck_read_bitmaps(ctx);
358 * First, find a free block
360 retval = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
362 pctx.errcode = retval;
363 fix_problem(ctx, PR_3_ERR_LPF_NEW_BLOCK, &pctx);
366 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
367 ext2fs_mark_block_bitmap(fs->block_map, blk);
368 ext2fs_mark_bb_dirty(fs);
371 * Next find a free inode.
373 retval = ext2fs_new_inode(fs, EXT2_ROOT_INO, 040755,
374 ctx->inode_used_map, &ino);
376 pctx.errcode = retval;
377 fix_problem(ctx, PR_3_ERR_LPF_NEW_INODE, &pctx);
380 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
381 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
382 ext2fs_mark_inode_bitmap(fs->inode_map, ino);
383 ext2fs_mark_ib_dirty(fs);
386 * Now let's create the actual data block for the inode
388 retval = ext2fs_new_dir_block(fs, ino, EXT2_ROOT_INO, &block);
390 pctx.errcode = retval;
391 fix_problem(ctx, PR_3_ERR_LPF_NEW_DIR_BLOCK, &pctx);
395 retval = ext2fs_write_dir_block(fs, blk, block);
396 ext2fs_free_mem((void **) &block);
398 pctx.errcode = retval;
399 fix_problem(ctx, PR_3_ERR_LPF_WRITE_BLOCK, &pctx);
404 * Set up the inode structure
406 memset(&inode, 0, sizeof(inode));
407 inode.i_mode = 040755;
408 inode.i_size = fs->blocksize;
409 inode.i_atime = inode.i_ctime = inode.i_mtime = time(0);
410 inode.i_links_count = 2;
411 inode.i_blocks = fs->blocksize / 512;
412 inode.i_block[0] = blk;
415 * Next, write out the inode.
417 pctx.errcode = ext2fs_write_inode(fs, ino, &inode);
419 pctx.str = "ext2fs_write_inode";
420 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
424 * Finally, create the directory link
426 pctx.errcode = ext2fs_link(fs, EXT2_ROOT_INO, name, ino, 0);
428 pctx.str = "ext2fs_link";
429 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
434 * Miscellaneous bookkeeping that needs to be kept straight.
436 e2fsck_add_dir_info(ctx, ino, EXT2_ROOT_INO);
437 adjust_inode_count(ctx, EXT2_ROOT_INO, +1);
438 ext2fs_icount_store(ctx->inode_count, ino, 2);
439 ext2fs_icount_store(ctx->inode_link_info, ino, 2);
441 printf("/lost+found created; inode #%lu\n", ino);
447 * This routine will connect a file to lost+found
449 int e2fsck_reconnect_file(e2fsck_t ctx, ino_t inode)
451 ext2_filsys fs = ctx->fs;
454 struct problem_context pctx;
456 clear_problem_context(&pctx);
459 if (!bad_lost_and_found && !lost_and_found) {
460 lost_and_found = get_lost_and_found(ctx);
462 bad_lost_and_found++;
464 if (bad_lost_and_found) {
465 fix_problem(ctx, PR_3_NO_LPF, &pctx);
469 sprintf(name, "#%lu", inode);
470 retval = ext2fs_link(fs, lost_and_found, name, inode, 0);
471 if (retval == EXT2_ET_DIR_NO_SPACE) {
472 if (!fix_problem(ctx, PR_3_EXPAND_LF_DIR, &pctx))
474 retval = expand_directory(ctx, lost_and_found);
476 pctx.errcode = retval;
477 fix_problem(ctx, PR_3_CANT_EXPAND_LPF, &pctx);
480 retval = ext2fs_link(fs, lost_and_found, name, inode, 0);
483 pctx.errcode = retval;
484 fix_problem(ctx, PR_3_CANT_RECONNECT, &pctx);
487 adjust_inode_count(ctx, inode, +1);
493 * Utility routine to adjust the inode counts on an inode.
495 static errcode_t adjust_inode_count(e2fsck_t ctx, ino_t ino, int adj)
497 ext2_filsys fs = ctx->fs;
499 struct ext2_inode inode;
504 retval = ext2fs_read_inode(fs, ino, &inode);
509 printf("Adjusting link count for inode %lu by %d (from %d)\n", ino, adj,
510 inode.i_links_count);
513 inode.i_links_count += adj;
515 ext2fs_icount_increment(ctx->inode_count, ino, 0);
516 ext2fs_icount_increment(ctx->inode_link_info, ino, 0);
518 ext2fs_icount_decrement(ctx->inode_count, ino, 0);
519 ext2fs_icount_decrement(ctx->inode_link_info, ino, 0);
523 retval = ext2fs_write_inode(fs, ino, &inode);
531 * Fix parent --- this routine fixes up the parent of a directory.
533 struct fix_dotdot_struct {
540 static int fix_dotdot_proc(struct ext2_dir_entry *dirent,
546 struct fix_dotdot_struct *fp = (struct fix_dotdot_struct *) priv_data;
548 struct problem_context pctx;
550 if ((dirent->name_len & 0xFF) != 2)
552 if (strncmp(dirent->name, "..", 2))
555 clear_problem_context(&pctx);
557 retval = adjust_inode_count(fp->ctx, dirent->inode, -1);
559 pctx.errcode = retval;
560 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
562 retval = adjust_inode_count(fp->ctx, fp->parent, 1);
564 pctx.errcode = retval;
565 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
567 dirent->inode = fp->parent;
570 return DIRENT_ABORT | DIRENT_CHANGED;
573 static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ino_t parent)
575 ext2_filsys fs = ctx->fs;
577 struct fix_dotdot_struct fp;
578 struct problem_context pctx;
586 printf("Fixing '..' of inode %lu to be %lu...\n", dir->ino, parent);
589 retval = ext2fs_dir_iterate(fs, dir->ino, DIRENT_FLAG_INCLUDE_EMPTY,
590 0, fix_dotdot_proc, &fp);
591 if (retval || !fp.done) {
592 clear_problem_context(&pctx);
594 pctx.errcode = retval;
595 fix_problem(ctx, retval ? PR_3_FIX_PARENT_ERR :
596 PR_3_FIX_PARENT_NOFIND, &pctx);
597 ext2fs_unmark_valid(fs);
599 dir->dotdot = parent;
605 * These routines are responsible for expanding a /lost+found if it is
609 struct expand_dir_struct {
615 static int expand_dir_proc(ext2_filsys fs,
620 struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
622 static blk_t last_blk = 0;
633 retval = ext2fs_new_block(fs, last_blk, ctx->block_found_map,
640 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
646 retval = ext2fs_write_dir_block(fs, new_blk, block);
648 retval = ext2fs_get_mem(fs->blocksize, (void **) &block);
653 memset(block, 0, fs->blocksize);
654 retval = io_channel_write_blk(fs->io, new_blk, 1, block);
660 ext2fs_free_mem((void **) &block);
662 ext2fs_mark_block_bitmap(ctx->block_found_map, new_blk);
663 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
664 ext2fs_mark_bb_dirty(fs);
666 return (BLOCK_CHANGED | BLOCK_ABORT);
668 return BLOCK_CHANGED;
671 static errcode_t expand_directory(e2fsck_t ctx, ino_t dir)
673 ext2_filsys fs = ctx->fs;
675 struct expand_dir_struct es;
676 struct ext2_inode inode;
678 if (!(fs->flags & EXT2_FLAG_RW))
679 return EXT2_ET_RO_FILSYS;
682 * Read the inode and block bitmaps in; we'll be messing with
685 e2fsck_read_bitmaps(ctx);
687 retval = ext2fs_check_directory(fs, dir);
695 retval = ext2fs_block_iterate(fs, dir, BLOCK_FLAG_APPEND,
696 0, expand_dir_proc, &es);
701 return EXT2_ET_EXPAND_DIR_ERR;
704 * Update the size and block count fields in the inode.
706 retval = ext2fs_read_inode(fs, dir, &inode);
710 inode.i_size += fs->blocksize;
711 inode.i_blocks += fs->blocksize / 512;
713 e2fsck_write_inode(ctx, dir, &inode, "expand_directory");