Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * pass3.c -- pass #3 of e2fsck: Check for directory connectivity |
| 3 | * |
Theodore Ts'o | c1faf9c | 1999-09-14 20:00:54 +0000 | [diff] [blame] | 4 | * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999 Theodore Ts'o. |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 5 | * |
| 6 | * %Begin-Header% |
| 7 | * This file may be redistributed under the terms of the GNU Public |
| 8 | * License. |
| 9 | * %End-Header% |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 10 | * |
| 11 | * Pass #3 assures that all directories are connected to the |
| 12 | * filesystem tree, using the following algorithm: |
| 13 | * |
| 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 |
| 16 | * "done". |
| 17 | * |
| 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 |
| 26 | * filesystem loop. |
| 27 | * |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 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. |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 32 | * |
| 33 | * Pass 3 frees the following data structures: |
| 34 | * - The dirinfo directory information cache. |
| 35 | */ |
| 36 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 37 | #ifdef HAVE_ERRNO_H |
| 38 | #include <errno.h> |
| 39 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 40 | |
| 41 | #include "e2fsck.h" |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 42 | #include "problem.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 43 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 44 | static void check_root(e2fsck_t ctx); |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 45 | static int check_directory(e2fsck_t ctx, struct dir_info *dir, |
| 46 | struct problem_context *pctx); |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 47 | static ext2_ino_t get_lost_and_found(e2fsck_t ctx); |
| 48 | static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ext2_ino_t parent); |
| 49 | static errcode_t adjust_inode_count(e2fsck_t ctx, ext2_ino_t ino, int adj); |
| 50 | static errcode_t expand_directory(e2fsck_t ctx, ext2_ino_t dir); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 51 | |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 52 | static ext2_ino_t lost_and_found = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 53 | static int bad_lost_and_found = 0; |
| 54 | |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 55 | static ext2fs_inode_bitmap inode_loop_detect = 0; |
| 56 | static ext2fs_inode_bitmap inode_done_map = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 57 | |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 58 | void e2fsck_pass3(e2fsck_t ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 59 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 60 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 61 | int i; |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 62 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 63 | struct resource_track rtrack; |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 64 | #endif |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 65 | struct problem_context pctx; |
| 66 | struct dir_info *dir; |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 67 | unsigned long maxdirs, count; |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 68 | |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 69 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 70 | init_resource_track(&rtrack); |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 71 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 72 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 73 | clear_problem_context(&pctx); |
| 74 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 75 | #ifdef MTRACE |
| 76 | mtrace_print("Pass 3"); |
| 77 | #endif |
| 78 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 79 | if (!(ctx->options & E2F_OPT_PREEN)) |
| 80 | fix_problem(ctx, PR_3_PASS_HEADER, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * Allocate some bitmaps to do loop detection. |
| 84 | */ |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 85 | pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("inode done bitmap"), |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 86 | &inode_done_map); |
| 87 | if (pctx.errcode) { |
| 88 | pctx.num = 2; |
| 89 | fix_problem(ctx, PR_3_ALLOCATE_IBITMAP_ERROR, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 90 | ctx->flags |= E2F_FLAG_ABORT; |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 91 | goto abort_exit; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 92 | } |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 93 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 94 | if (ctx->options & E2F_OPT_TIME) { |
| 95 | e2fsck_clear_progbar(ctx); |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 96 | print_resource_track(_("Peak memory"), &ctx->global_rtrack); |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 97 | } |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 98 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 99 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 100 | check_root(ctx); |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 101 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
| 102 | goto abort_exit; |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 103 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 104 | ext2fs_mark_inode_bitmap(inode_done_map, EXT2_ROOT_INO); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 105 | |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 106 | maxdirs = e2fsck_get_num_dirinfo(ctx); |
Theodore Ts'o | f75c28d | 1998-08-01 04:18:06 +0000 | [diff] [blame] | 107 | count = 1; |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 108 | |
Theodore Ts'o | f75c28d | 1998-08-01 04:18:06 +0000 | [diff] [blame] | 109 | if (ctx->progress) |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 110 | if ((ctx->progress)(ctx, 3, 0, maxdirs)) |
Theodore Ts'o | f75c28d | 1998-08-01 04:18:06 +0000 | [diff] [blame] | 111 | goto abort_exit; |
| 112 | |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 113 | for (i=0; (dir = e2fsck_dir_info_iter(ctx, &i)) != 0;) { |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 114 | if (ctx->progress) |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 115 | if ((ctx->progress)(ctx, 3, count++, maxdirs)) |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 116 | goto abort_exit; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 117 | if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dir->ino)) |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 118 | if (check_directory(ctx, dir, &pctx)) |
| 119 | goto abort_exit; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 120 | } |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 121 | |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 122 | /* |
| 123 | * Force the creation of /lost+found if not present |
| 124 | */ |
| 125 | if ((ctx->flags & E2F_OPT_READONLY) == 0) |
| 126 | get_lost_and_found(ctx); |
| 127 | |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 128 | abort_exit: |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 129 | e2fsck_free_dir_info(ctx); |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 130 | if (inode_loop_detect) { |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 131 | ext2fs_free_inode_bitmap(inode_loop_detect); |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 132 | inode_loop_detect = 0; |
| 133 | } |
| 134 | if (inode_done_map) { |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 135 | ext2fs_free_inode_bitmap(inode_done_map); |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 136 | inode_done_map = 0; |
| 137 | } |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 138 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 139 | if (ctx->options & E2F_OPT_TIME2) { |
| 140 | e2fsck_clear_progbar(ctx); |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 141 | print_resource_track(_("Pass 3"), &rtrack); |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 142 | } |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 143 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* |
| 147 | * This makes sure the root inode is present; if not, we ask if the |
| 148 | * user wants us to create it. Not creating it is a fatal error. |
| 149 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 150 | static void check_root(e2fsck_t ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 151 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 152 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 153 | blk_t blk; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 154 | struct ext2_inode inode; |
| 155 | char * block; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 156 | struct problem_context pctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 157 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 158 | clear_problem_context(&pctx); |
| 159 | |
| 160 | if (ext2fs_test_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 161 | /* |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 162 | * If the root inode is not a directory, die here. The |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 163 | * user must have answered 'no' in pass1 when we |
| 164 | * offered to clear it. |
| 165 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 166 | if (!(ext2fs_test_inode_bitmap(ctx->inode_dir_map, |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 167 | EXT2_ROOT_INO))) { |
| 168 | fix_problem(ctx, PR_3_ROOT_NOT_DIR_ABORT, &pctx); |
| 169 | ctx->flags |= E2F_FLAG_ABORT; |
| 170 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 174 | if (!fix_problem(ctx, PR_3_NO_ROOT_INODE, &pctx)) { |
| 175 | fix_problem(ctx, PR_3_NO_ROOT_INODE_ABORT, &pctx); |
| 176 | ctx->flags |= E2F_FLAG_ABORT; |
| 177 | return; |
| 178 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 179 | |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 180 | e2fsck_read_bitmaps(ctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 181 | |
| 182 | /* |
| 183 | * First, find a free block |
| 184 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 185 | pctx.errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk); |
| 186 | if (pctx.errcode) { |
| 187 | pctx.str = "ext2fs_new_block"; |
| 188 | fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 189 | ctx->flags |= E2F_FLAG_ABORT; |
| 190 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 191 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 192 | ext2fs_mark_block_bitmap(ctx->block_found_map, blk); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 193 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 194 | ext2fs_mark_bb_dirty(fs); |
| 195 | |
| 196 | /* |
| 197 | * Now let's create the actual data block for the inode |
| 198 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 199 | pctx.errcode = ext2fs_new_dir_block(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, |
| 200 | &block); |
| 201 | if (pctx.errcode) { |
| 202 | pctx.str = "ext2fs_new_dir_block"; |
| 203 | fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 204 | ctx->flags |= E2F_FLAG_ABORT; |
| 205 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 208 | pctx.errcode = ext2fs_write_dir_block(fs, blk, block); |
| 209 | if (pctx.errcode) { |
| 210 | pctx.str = "ext2fs_write_dir_block"; |
| 211 | fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 212 | ctx->flags |= E2F_FLAG_ABORT; |
| 213 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 214 | } |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 215 | ext2fs_free_mem((void **) &block); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 216 | |
| 217 | /* |
| 218 | * Set up the inode structure |
| 219 | */ |
| 220 | memset(&inode, 0, sizeof(inode)); |
| 221 | inode.i_mode = 040755; |
| 222 | inode.i_size = fs->blocksize; |
| 223 | inode.i_atime = inode.i_ctime = inode.i_mtime = time(0); |
| 224 | inode.i_links_count = 2; |
| 225 | inode.i_blocks = fs->blocksize / 512; |
| 226 | inode.i_block[0] = blk; |
| 227 | |
| 228 | /* |
| 229 | * Write out the inode. |
| 230 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 231 | pctx.errcode = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode); |
| 232 | if (pctx.errcode) { |
| 233 | pctx.str = "ext2fs_write_inode"; |
| 234 | fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 235 | ctx->flags |= E2F_FLAG_ABORT; |
| 236 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Miscellaneous bookkeeping... |
| 241 | */ |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 242 | e2fsck_add_dir_info(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 243 | ext2fs_icount_store(ctx->inode_count, EXT2_ROOT_INO, 2); |
| 244 | ext2fs_icount_store(ctx->inode_link_info, EXT2_ROOT_INO, 2); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 245 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 246 | ext2fs_mark_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO); |
| 247 | ext2fs_mark_inode_bitmap(ctx->inode_dir_map, EXT2_ROOT_INO); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 248 | ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_ROOT_INO); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 249 | ext2fs_mark_ib_dirty(fs); |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | * This subroutine is responsible for making sure that a particular |
| 254 | * directory is connected to the root; if it isn't we trace it up as |
| 255 | * far as we can go, and then offer to connect the resulting parent to |
| 256 | * the lost+found. We have to do loop detection; if we ever discover |
| 257 | * a loop, we treat that as a disconnected directory and offer to |
| 258 | * reparent it to lost+found. |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 259 | * |
| 260 | * However, loop detection is expensive, because for very large |
| 261 | * filesystems, the inode_loop_detect bitmap is huge, and clearing it |
| 262 | * is non-trivial. Loops in filesystems are also a rare error case, |
| 263 | * and we shouldn't optimize for error cases. So we try two passes of |
| 264 | * the algorithm. The first time, we ignore loop detection and merely |
| 265 | * increment a counter; if the counter exceeds some extreme threshold, |
| 266 | * then we try again with the loop detection bitmap enabled. |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 267 | */ |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 268 | static int check_directory(e2fsck_t ctx, struct dir_info *dir, |
| 269 | struct problem_context *pctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 270 | { |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 271 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 272 | struct dir_info *p = dir; |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 273 | int loop_pass = 0, parent_count = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 274 | |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 275 | if (!p) |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 276 | return 0; |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 277 | |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 278 | while (1) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 279 | /* |
| 280 | * Mark this inode as being "done"; by the time we |
| 281 | * return from this function, the inode we either be |
| 282 | * verified as being connected to the directory tree, |
| 283 | * or we will have offered to reconnect this to |
| 284 | * lost+found. |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 285 | * |
| 286 | * If it was marked done already, then we've reached a |
| 287 | * parent we've already checked. |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 288 | */ |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 289 | if (ext2fs_mark_inode_bitmap(inode_done_map, p->ino)) |
| 290 | break; |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 291 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 292 | /* |
| 293 | * If this directory doesn't have a parent, or we've |
| 294 | * seen the parent once already, then offer to |
| 295 | * reparent it to lost+found |
| 296 | */ |
| 297 | if (!p->parent || |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 298 | (loop_pass && |
| 299 | (ext2fs_test_inode_bitmap(inode_loop_detect, |
| 300 | p->parent)))) { |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 301 | pctx->ino = p->ino; |
| 302 | if (fix_problem(ctx, PR_3_UNCONNECTED_DIR, pctx)) { |
| 303 | if (e2fsck_reconnect_file(ctx, p->ino)) |
| 304 | ext2fs_unmark_valid(fs); |
| 305 | else { |
| 306 | p->parent = lost_and_found; |
| 307 | fix_dotdot(ctx, p, lost_and_found); |
| 308 | } |
| 309 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 310 | break; |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 311 | } |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 312 | p = e2fsck_get_dir_info(ctx, p->parent); |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 313 | if (!p) { |
| 314 | fix_problem(ctx, PR_3_NO_DIRINFO, pctx); |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 315 | return 0; |
| 316 | } |
| 317 | if (loop_pass) { |
| 318 | ext2fs_mark_inode_bitmap(inode_loop_detect, |
| 319 | p->ino); |
| 320 | } else if (parent_count++ > 2048) { |
| 321 | /* |
| 322 | * If we've run into a path depth that's |
| 323 | * greater than 2048, try again with the inode |
| 324 | * loop bitmap turned on and start from the |
| 325 | * top. |
| 326 | */ |
| 327 | loop_pass = 1; |
| 328 | if (inode_loop_detect) |
| 329 | ext2fs_clear_inode_bitmap(inode_loop_detect); |
| 330 | else { |
| 331 | pctx->errcode = ext2fs_allocate_inode_bitmap(fs, _("inode loop detection bitmap"), &inode_loop_detect); |
| 332 | if (pctx->errcode) { |
| 333 | pctx->num = 1; |
| 334 | fix_problem(ctx, |
| 335 | PR_3_ALLOCATE_IBITMAP_ERROR, pctx); |
| 336 | ctx->flags |= E2F_FLAG_ABORT; |
| 337 | return -1; |
| 338 | } |
| 339 | } |
| 340 | p = dir; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 341 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 342 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 343 | |
| 344 | /* |
| 345 | * Make sure that .. and the parent directory are the same; |
| 346 | * offer to fix it if not. |
| 347 | */ |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 348 | if (dir->parent != dir->dotdot) { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 349 | pctx->ino = dir->ino; |
| 350 | pctx->ino2 = dir->dotdot; |
| 351 | pctx->dir = dir->parent; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 352 | if (fix_problem(ctx, PR_3_BAD_DOT_DOT, pctx)) |
| 353 | fix_dotdot(ctx, dir, dir->parent); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 354 | } |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 355 | return 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* |
| 359 | * This routine gets the lost_and_found inode, making it a directory |
| 360 | * if necessary |
| 361 | */ |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 362 | static ext2_ino_t get_lost_and_found(e2fsck_t ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 363 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 364 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 365 | ext2_ino_t ino; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 366 | blk_t blk; |
| 367 | errcode_t retval; |
| 368 | struct ext2_inode inode; |
| 369 | char * block; |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 370 | static const char name[] = "lost+found"; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 371 | struct problem_context pctx; |
Theodore Ts'o | 4a9f593 | 1999-03-16 19:32:52 +0000 | [diff] [blame] | 372 | struct dir_info *dirinfo; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 373 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 374 | clear_problem_context(&pctx); |
| 375 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 376 | retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, |
| 377 | sizeof(name)-1, 0, &ino); |
Theodore Ts'o | 4a9f593 | 1999-03-16 19:32:52 +0000 | [diff] [blame] | 378 | if (!retval) { |
| 379 | if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, ino)) |
| 380 | return ino; |
| 381 | /* Lost+found isn't a directory! */ |
| 382 | pctx.ino = ino; |
| 383 | if (!fix_problem(ctx, PR_3_LPF_NOTDIR, &pctx)) |
| 384 | return 0; |
| 385 | |
Theodore Ts'o | c54b3c3 | 1999-07-03 07:20:06 +0000 | [diff] [blame] | 386 | /* OK, unlink the old /lost+found file. */ |
Theodore Ts'o | 4a9f593 | 1999-03-16 19:32:52 +0000 | [diff] [blame] | 387 | pctx.errcode = ext2fs_unlink(fs, EXT2_ROOT_INO, name, ino, 0); |
| 388 | if (pctx.errcode) { |
| 389 | pctx.str = "ext2fs_unlink"; |
| 390 | fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx); |
| 391 | return 0; |
| 392 | } |
| 393 | dirinfo = e2fsck_get_dir_info(ctx, ino); |
| 394 | if (dirinfo) |
| 395 | dirinfo->parent = 0; |
| 396 | adjust_inode_count(ctx, ino, -1); |
| 397 | } else if (retval != EXT2_ET_FILE_NOT_FOUND) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 398 | pctx.errcode = retval; |
| 399 | fix_problem(ctx, PR_3_ERR_FIND_LPF, &pctx); |
| 400 | } |
| 401 | if (!fix_problem(ctx, PR_3_NO_LF_DIR, 0)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 402 | return 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 403 | |
| 404 | /* |
| 405 | * Read the inode and block bitmaps in; we'll be messing with |
| 406 | * them. |
| 407 | */ |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 408 | e2fsck_read_bitmaps(ctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 409 | |
| 410 | /* |
| 411 | * First, find a free block |
| 412 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 413 | retval = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 414 | if (retval) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 415 | pctx.errcode = retval; |
| 416 | fix_problem(ctx, PR_3_ERR_LPF_NEW_BLOCK, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 417 | return 0; |
| 418 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 419 | ext2fs_mark_block_bitmap(ctx->block_found_map, blk); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 420 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 421 | ext2fs_mark_bb_dirty(fs); |
| 422 | |
| 423 | /* |
| 424 | * Next find a free inode. |
| 425 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 426 | retval = ext2fs_new_inode(fs, EXT2_ROOT_INO, 040755, |
| 427 | ctx->inode_used_map, &ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 428 | if (retval) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 429 | pctx.errcode = retval; |
| 430 | fix_problem(ctx, PR_3_ERR_LPF_NEW_INODE, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 431 | return 0; |
| 432 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 433 | ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino); |
| 434 | ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 435 | ext2fs_mark_inode_bitmap(fs->inode_map, ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 436 | ext2fs_mark_ib_dirty(fs); |
| 437 | |
| 438 | /* |
| 439 | * Now let's create the actual data block for the inode |
| 440 | */ |
| 441 | retval = ext2fs_new_dir_block(fs, ino, EXT2_ROOT_INO, &block); |
| 442 | if (retval) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 443 | pctx.errcode = retval; |
| 444 | fix_problem(ctx, PR_3_ERR_LPF_NEW_DIR_BLOCK, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 445 | return 0; |
| 446 | } |
| 447 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 448 | retval = ext2fs_write_dir_block(fs, blk, block); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 449 | ext2fs_free_mem((void **) &block); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 450 | if (retval) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 451 | pctx.errcode = retval; |
| 452 | fix_problem(ctx, PR_3_ERR_LPF_WRITE_BLOCK, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 453 | return 0; |
| 454 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 455 | |
| 456 | /* |
| 457 | * Set up the inode structure |
| 458 | */ |
| 459 | memset(&inode, 0, sizeof(inode)); |
| 460 | inode.i_mode = 040755; |
| 461 | inode.i_size = fs->blocksize; |
| 462 | inode.i_atime = inode.i_ctime = inode.i_mtime = time(0); |
| 463 | inode.i_links_count = 2; |
| 464 | inode.i_blocks = fs->blocksize / 512; |
| 465 | inode.i_block[0] = blk; |
| 466 | |
| 467 | /* |
| 468 | * Next, write out the inode. |
| 469 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 470 | pctx.errcode = ext2fs_write_inode(fs, ino, &inode); |
| 471 | if (pctx.errcode) { |
| 472 | pctx.str = "ext2fs_write_inode"; |
| 473 | fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 474 | return 0; |
| 475 | } |
| 476 | /* |
| 477 | * Finally, create the directory link |
| 478 | */ |
Theodore Ts'o | 6fdc7a3 | 1999-11-10 13:34:40 +0000 | [diff] [blame] | 479 | pctx.errcode = ext2fs_link(fs, EXT2_ROOT_INO, name, ino, EXT2_FT_DIR); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 480 | if (pctx.errcode) { |
| 481 | pctx.str = "ext2fs_link"; |
| 482 | fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * Miscellaneous bookkeeping that needs to be kept straight. |
| 488 | */ |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 489 | e2fsck_add_dir_info(ctx, ino, EXT2_ROOT_INO); |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 490 | adjust_inode_count(ctx, EXT2_ROOT_INO, 1); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 491 | ext2fs_icount_store(ctx->inode_count, ino, 2); |
| 492 | ext2fs_icount_store(ctx->inode_link_info, ino, 2); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 493 | #if 0 |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 494 | printf("/lost+found created; inode #%lu\n", ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 495 | #endif |
| 496 | return ino; |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * This routine will connect a file to lost+found |
| 501 | */ |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 502 | int e2fsck_reconnect_file(e2fsck_t ctx, ext2_ino_t ino) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 503 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 504 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 505 | errcode_t retval; |
| 506 | char name[80]; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 507 | struct problem_context pctx; |
Theodore Ts'o | 6fdc7a3 | 1999-11-10 13:34:40 +0000 | [diff] [blame] | 508 | struct ext2_inode inode; |
| 509 | int file_type = 0; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 510 | |
| 511 | clear_problem_context(&pctx); |
Theodore Ts'o | 6fdc7a3 | 1999-11-10 13:34:40 +0000 | [diff] [blame] | 512 | pctx.ino = ino; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 513 | |
| 514 | if (!bad_lost_and_found && !lost_and_found) { |
| 515 | lost_and_found = get_lost_and_found(ctx); |
| 516 | if (!lost_and_found) |
| 517 | bad_lost_and_found++; |
| 518 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 519 | if (bad_lost_and_found) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 520 | fix_problem(ctx, PR_3_NO_LPF, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 521 | return 1; |
| 522 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 523 | |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 524 | sprintf(name, "#%u", ino); |
Theodore Ts'o | 6fdc7a3 | 1999-11-10 13:34:40 +0000 | [diff] [blame] | 525 | if (ext2fs_read_inode(fs, ino, &inode) == 0) |
| 526 | file_type = ext2_file_type(inode.i_mode); |
| 527 | retval = ext2fs_link(fs, lost_and_found, name, ino, file_type); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 528 | if (retval == EXT2_ET_DIR_NO_SPACE) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 529 | if (!fix_problem(ctx, PR_3_EXPAND_LF_DIR, &pctx)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 530 | return 1; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 531 | retval = expand_directory(ctx, lost_and_found); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 532 | if (retval) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 533 | pctx.errcode = retval; |
| 534 | fix_problem(ctx, PR_3_CANT_EXPAND_LPF, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 535 | return 1; |
| 536 | } |
Theodore Ts'o | 6fdc7a3 | 1999-11-10 13:34:40 +0000 | [diff] [blame] | 537 | retval = ext2fs_link(fs, lost_and_found, name, ino, file_type); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 538 | } |
| 539 | if (retval) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 540 | pctx.errcode = retval; |
| 541 | fix_problem(ctx, PR_3_CANT_RECONNECT, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 542 | return 1; |
| 543 | } |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 544 | adjust_inode_count(ctx, ino, 1); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 545 | |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | * Utility routine to adjust the inode counts on an inode. |
| 551 | */ |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 552 | static errcode_t adjust_inode_count(e2fsck_t ctx, ext2_ino_t ino, int adj) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 553 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 554 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 555 | errcode_t retval; |
| 556 | struct ext2_inode inode; |
| 557 | |
| 558 | if (!ino) |
| 559 | return 0; |
| 560 | |
| 561 | retval = ext2fs_read_inode(fs, ino, &inode); |
| 562 | if (retval) |
| 563 | return retval; |
| 564 | |
| 565 | #if 0 |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 566 | printf("Adjusting link count for inode %lu by %d (from %d)\n", ino, adj, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 567 | inode.i_links_count); |
| 568 | #endif |
| 569 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 570 | if (adj == 1) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 571 | ext2fs_icount_increment(ctx->inode_count, ino, 0); |
Theodore Ts'o | c1faf9c | 1999-09-14 20:00:54 +0000 | [diff] [blame] | 572 | if (inode.i_links_count == (__u16) ~0) |
| 573 | return 0; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 574 | ext2fs_icount_increment(ctx->inode_link_info, ino, 0); |
Theodore Ts'o | c1faf9c | 1999-09-14 20:00:54 +0000 | [diff] [blame] | 575 | inode.i_links_count++; |
| 576 | } else if (adj == -1) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 577 | ext2fs_icount_decrement(ctx->inode_count, ino, 0); |
Theodore Ts'o | c1faf9c | 1999-09-14 20:00:54 +0000 | [diff] [blame] | 578 | if (inode.i_links_count == 0) |
| 579 | return 0; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 580 | ext2fs_icount_decrement(ctx->inode_link_info, ino, 0); |
Theodore Ts'o | c1faf9c | 1999-09-14 20:00:54 +0000 | [diff] [blame] | 581 | inode.i_links_count--; |
| 582 | } else { |
| 583 | /* Should never happen */ |
Theodore Ts'o | 99a2cc9 | 2000-08-22 21:41:52 +0000 | [diff] [blame] | 584 | fatal_error(ctx, _("Debug error in e2fsck adjust_inode_count, " |
| 585 | "should never happen.\n")); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 588 | retval = ext2fs_write_inode(fs, ino, &inode); |
| 589 | if (retval) |
| 590 | return retval; |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | /* |
| 596 | * Fix parent --- this routine fixes up the parent of a directory. |
| 597 | */ |
| 598 | struct fix_dotdot_struct { |
| 599 | ext2_filsys fs; |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 600 | ext2_ino_t parent; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 601 | int done; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 602 | e2fsck_t ctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 603 | }; |
| 604 | |
| 605 | static int fix_dotdot_proc(struct ext2_dir_entry *dirent, |
| 606 | int offset, |
| 607 | int blocksize, |
| 608 | char *buf, |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 609 | void *priv_data) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 610 | { |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 611 | struct fix_dotdot_struct *fp = (struct fix_dotdot_struct *) priv_data; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 612 | errcode_t retval; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 613 | struct problem_context pctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 614 | |
Theodore Ts'o | b6f7983 | 1998-03-09 13:10:37 +0000 | [diff] [blame] | 615 | if ((dirent->name_len & 0xFF) != 2) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 616 | return 0; |
| 617 | if (strncmp(dirent->name, "..", 2)) |
| 618 | return 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 619 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 620 | clear_problem_context(&pctx); |
| 621 | |
| 622 | retval = adjust_inode_count(fp->ctx, dirent->inode, -1); |
| 623 | if (retval) { |
| 624 | pctx.errcode = retval; |
| 625 | fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx); |
| 626 | } |
| 627 | retval = adjust_inode_count(fp->ctx, fp->parent, 1); |
| 628 | if (retval) { |
| 629 | pctx.errcode = retval; |
| 630 | fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx); |
| 631 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 632 | dirent->inode = fp->parent; |
| 633 | |
| 634 | fp->done++; |
| 635 | return DIRENT_ABORT | DIRENT_CHANGED; |
| 636 | } |
| 637 | |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 638 | static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ext2_ino_t parent) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 639 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 640 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 641 | errcode_t retval; |
| 642 | struct fix_dotdot_struct fp; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 643 | struct problem_context pctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 644 | |
| 645 | fp.fs = fs; |
| 646 | fp.parent = parent; |
| 647 | fp.done = 0; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 648 | fp.ctx = ctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 649 | |
| 650 | #if 0 |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 651 | printf("Fixing '..' of inode %lu to be %lu...\n", dir->ino, parent); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 652 | #endif |
| 653 | |
| 654 | retval = ext2fs_dir_iterate(fs, dir->ino, DIRENT_FLAG_INCLUDE_EMPTY, |
| 655 | 0, fix_dotdot_proc, &fp); |
| 656 | if (retval || !fp.done) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 657 | clear_problem_context(&pctx); |
| 658 | pctx.ino = dir->ino; |
| 659 | pctx.errcode = retval; |
| 660 | fix_problem(ctx, retval ? PR_3_FIX_PARENT_ERR : |
| 661 | PR_3_FIX_PARENT_NOFIND, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 662 | ext2fs_unmark_valid(fs); |
| 663 | } |
| 664 | dir->dotdot = parent; |
| 665 | |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | /* |
| 670 | * These routines are responsible for expanding a /lost+found if it is |
| 671 | * too small. |
| 672 | */ |
| 673 | |
| 674 | struct expand_dir_struct { |
Theodore Ts'o | c1faf9c | 1999-09-14 20:00:54 +0000 | [diff] [blame] | 675 | int done; |
| 676 | int newblocks; |
| 677 | errcode_t err; |
| 678 | e2fsck_t ctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 679 | }; |
| 680 | |
| 681 | static int expand_dir_proc(ext2_filsys fs, |
| 682 | blk_t *blocknr, |
Theodore Ts'o | 133a56d | 2000-11-17 05:40:49 +0000 | [diff] [blame] | 683 | e2_blkcnt_t blockcnt, |
| 684 | blk_t ref_block, |
| 685 | int ref_offset, |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 686 | void *priv_data) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 687 | { |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 688 | struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 689 | blk_t new_blk; |
| 690 | static blk_t last_blk = 0; |
| 691 | char *block; |
| 692 | errcode_t retval; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 693 | e2fsck_t ctx; |
| 694 | |
| 695 | ctx = es->ctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 696 | |
| 697 | if (*blocknr) { |
| 698 | last_blk = *blocknr; |
| 699 | return 0; |
| 700 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 701 | retval = ext2fs_new_block(fs, last_blk, ctx->block_found_map, |
| 702 | &new_blk); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 703 | if (retval) { |
| 704 | es->err = retval; |
| 705 | return BLOCK_ABORT; |
| 706 | } |
| 707 | if (blockcnt > 0) { |
| 708 | retval = ext2fs_new_dir_block(fs, 0, 0, &block); |
| 709 | if (retval) { |
| 710 | es->err = retval; |
| 711 | return BLOCK_ABORT; |
| 712 | } |
| 713 | es->done = 1; |
Theodore Ts'o | b8647fa | 1997-11-20 21:52:43 +0000 | [diff] [blame] | 714 | retval = ext2fs_write_dir_block(fs, new_blk, block); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 715 | } else { |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 716 | retval = ext2fs_get_mem(fs->blocksize, (void **) &block); |
| 717 | if (retval) { |
| 718 | es->err = retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 719 | return BLOCK_ABORT; |
| 720 | } |
| 721 | memset(block, 0, fs->blocksize); |
Theodore Ts'o | b8647fa | 1997-11-20 21:52:43 +0000 | [diff] [blame] | 722 | retval = io_channel_write_blk(fs->io, new_blk, 1, block); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 723 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 724 | if (retval) { |
| 725 | es->err = retval; |
| 726 | return BLOCK_ABORT; |
| 727 | } |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 728 | ext2fs_free_mem((void **) &block); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 729 | *blocknr = new_blk; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 730 | ext2fs_mark_block_bitmap(ctx->block_found_map, new_blk); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 731 | ext2fs_mark_block_bitmap(fs->block_map, new_blk); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 732 | ext2fs_mark_bb_dirty(fs); |
Theodore Ts'o | c1faf9c | 1999-09-14 20:00:54 +0000 | [diff] [blame] | 733 | es->newblocks++; |
| 734 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 735 | if (es->done) |
| 736 | return (BLOCK_CHANGED | BLOCK_ABORT); |
| 737 | else |
| 738 | return BLOCK_CHANGED; |
| 739 | } |
| 740 | |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 741 | static errcode_t expand_directory(e2fsck_t ctx, ext2_ino_t dir) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 742 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 743 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 744 | errcode_t retval; |
| 745 | struct expand_dir_struct es; |
| 746 | struct ext2_inode inode; |
| 747 | |
| 748 | if (!(fs->flags & EXT2_FLAG_RW)) |
| 749 | return EXT2_ET_RO_FILSYS; |
| 750 | |
Theodore Ts'o | b8647fa | 1997-11-20 21:52:43 +0000 | [diff] [blame] | 751 | /* |
| 752 | * Read the inode and block bitmaps in; we'll be messing with |
| 753 | * them. |
| 754 | */ |
| 755 | e2fsck_read_bitmaps(ctx); |
Theodore Ts'o | c1faf9c | 1999-09-14 20:00:54 +0000 | [diff] [blame] | 756 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 757 | retval = ext2fs_check_directory(fs, dir); |
| 758 | if (retval) |
| 759 | return retval; |
| 760 | |
| 761 | es.done = 0; |
| 762 | es.err = 0; |
Theodore Ts'o | c1faf9c | 1999-09-14 20:00:54 +0000 | [diff] [blame] | 763 | es.newblocks = 0; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 764 | es.ctx = ctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 765 | |
Theodore Ts'o | 133a56d | 2000-11-17 05:40:49 +0000 | [diff] [blame] | 766 | retval = ext2fs_block_iterate2(fs, dir, BLOCK_FLAG_APPEND, |
| 767 | 0, expand_dir_proc, &es); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 768 | |
| 769 | if (es.err) |
| 770 | return es.err; |
| 771 | if (!es.done) |
| 772 | return EXT2_ET_EXPAND_DIR_ERR; |
| 773 | |
| 774 | /* |
| 775 | * Update the size and block count fields in the inode. |
| 776 | */ |
| 777 | retval = ext2fs_read_inode(fs, dir, &inode); |
| 778 | if (retval) |
| 779 | return retval; |
| 780 | |
| 781 | inode.i_size += fs->blocksize; |
Theodore Ts'o | c1faf9c | 1999-09-14 20:00:54 +0000 | [diff] [blame] | 782 | inode.i_blocks += (fs->blocksize / 512) * es.newblocks; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 783 | |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 784 | e2fsck_write_inode(ctx, dir, &inode, "expand_directory"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 785 | |
| 786 | return 0; |
| 787 | } |