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 | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 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% |
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); |
| 45 | static void check_directory(e2fsck_t ctx, struct dir_info *dir, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 46 | struct problem_context *pctx); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 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); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 51 | |
| 52 | static ino_t lost_and_found = 0; |
| 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 | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 67 | unsigned long max, count; |
| 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 | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 85 | pctx.errcode = ext2fs_allocate_inode_bitmap(fs, |
| 86 | "inode loop detection bitmap", &inode_loop_detect); |
| 87 | if (pctx.errcode) { |
| 88 | pctx.num = 1; |
| 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 | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 93 | pctx.errcode = ext2fs_allocate_inode_bitmap(fs, "inode done bitmap", |
| 94 | &inode_done_map); |
| 95 | if (pctx.errcode) { |
| 96 | pctx.num = 2; |
| 97 | fix_problem(ctx, PR_3_ALLOCATE_IBITMAP_ERROR, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 98 | ctx->flags |= E2F_FLAG_ABORT; |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 99 | goto abort_exit; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 100 | } |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 101 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 102 | if (ctx->options & E2F_OPT_TIME) |
| 103 | print_resource_track("Peak memory", &ctx->global_rtrack); |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 104 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 105 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 106 | check_root(ctx); |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 107 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
| 108 | goto abort_exit; |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 109 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 110 | ext2fs_mark_inode_bitmap(inode_done_map, EXT2_ROOT_INO); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 111 | |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 112 | max = e2fsck_get_num_dirinfo(ctx); |
Theodore Ts'o | f75c28d | 1998-08-01 04:18:06 +0000 | [diff] [blame] | 113 | count = 1; |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 114 | |
Theodore Ts'o | f75c28d | 1998-08-01 04:18:06 +0000 | [diff] [blame] | 115 | if (ctx->progress) |
| 116 | if ((ctx->progress)(ctx, 3, 0, max)) |
| 117 | goto abort_exit; |
| 118 | |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 119 | for (i=0; (dir = e2fsck_dir_info_iter(ctx, &i)) != 0;) { |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 120 | if (ctx->progress) |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 121 | if ((ctx->progress)(ctx, 3, count++, max)) |
| 122 | goto abort_exit; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 123 | if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dir->ino)) |
| 124 | check_directory(ctx, dir, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 125 | } |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 126 | |
| 127 | abort_exit: |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 128 | e2fsck_free_dir_info(ctx); |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 129 | if (inode_loop_detect) |
| 130 | ext2fs_free_inode_bitmap(inode_loop_detect); |
| 131 | if (inode_done_map) |
| 132 | ext2fs_free_inode_bitmap(inode_done_map); |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 133 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 134 | if (ctx->options & E2F_OPT_TIME2) |
| 135 | print_resource_track("Pass 3", &rtrack); |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 136 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /* |
| 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. |
| 142 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 143 | static void check_root(e2fsck_t ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 144 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 145 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 146 | blk_t blk; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 147 | struct ext2_inode inode; |
| 148 | char * block; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 149 | struct problem_context pctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 150 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 151 | clear_problem_context(&pctx); |
| 152 | |
| 153 | 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] | 154 | /* |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 155 | * If the root inode is not a directory, die here. The |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 156 | * user must have answered 'no' in pass1 when we |
| 157 | * offered to clear it. |
| 158 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 159 | if (!(ext2fs_test_inode_bitmap(ctx->inode_dir_map, |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 160 | EXT2_ROOT_INO))) { |
| 161 | fix_problem(ctx, PR_3_ROOT_NOT_DIR_ABORT, &pctx); |
| 162 | ctx->flags |= E2F_FLAG_ABORT; |
| 163 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 164 | return; |
| 165 | } |
| 166 | |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 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; |
| 170 | return; |
| 171 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 172 | |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 173 | e2fsck_read_bitmaps(ctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 174 | |
| 175 | /* |
| 176 | * First, find a free block |
| 177 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 178 | pctx.errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk); |
| 179 | if (pctx.errcode) { |
| 180 | pctx.str = "ext2fs_new_block"; |
| 181 | fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 182 | ctx->flags |= E2F_FLAG_ABORT; |
| 183 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 184 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 185 | ext2fs_mark_block_bitmap(ctx->block_found_map, blk); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 186 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 187 | ext2fs_mark_bb_dirty(fs); |
| 188 | |
| 189 | /* |
| 190 | * Now let's create the actual data block for the inode |
| 191 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 192 | pctx.errcode = ext2fs_new_dir_block(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, |
| 193 | &block); |
| 194 | if (pctx.errcode) { |
| 195 | pctx.str = "ext2fs_new_dir_block"; |
| 196 | fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 197 | ctx->flags |= E2F_FLAG_ABORT; |
| 198 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 201 | pctx.errcode = ext2fs_write_dir_block(fs, blk, block); |
| 202 | if (pctx.errcode) { |
| 203 | pctx.str = "ext2fs_write_dir_block"; |
| 204 | fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 205 | ctx->flags |= E2F_FLAG_ABORT; |
| 206 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 207 | } |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 208 | ext2fs_free_mem((void **) &block); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | * Set up the inode structure |
| 212 | */ |
| 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; |
| 220 | |
| 221 | /* |
| 222 | * Write out the inode. |
| 223 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 224 | pctx.errcode = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode); |
| 225 | if (pctx.errcode) { |
| 226 | pctx.str = "ext2fs_write_inode"; |
| 227 | fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 228 | ctx->flags |= E2F_FLAG_ABORT; |
| 229 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Miscellaneous bookkeeping... |
| 234 | */ |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 235 | e2fsck_add_dir_info(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 236 | ext2fs_icount_store(ctx->inode_count, EXT2_ROOT_INO, 2); |
| 237 | ext2fs_icount_store(ctx->inode_link_info, EXT2_ROOT_INO, 2); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 238 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 239 | ext2fs_mark_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO); |
| 240 | ext2fs_mark_inode_bitmap(ctx->inode_dir_map, EXT2_ROOT_INO); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 241 | ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_ROOT_INO); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 242 | ext2fs_mark_ib_dirty(fs); |
| 243 | } |
| 244 | |
| 245 | /* |
| 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. |
| 252 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 253 | static void check_directory(e2fsck_t ctx, struct dir_info *dir, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 254 | struct problem_context *pctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 255 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 256 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 257 | struct dir_info *p = dir; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 258 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 259 | ext2fs_clear_inode_bitmap(inode_loop_detect); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 260 | while (p) { |
| 261 | /* |
| 262 | * If we find a parent which we've already checked, |
| 263 | * then stop; we know it's either already connected to |
| 264 | * the directory tree, or it isn't but the user has |
| 265 | * already told us he doesn't want us to reconnect the |
| 266 | * disconnected subtree. |
| 267 | */ |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 268 | if (ext2fs_test_inode_bitmap(inode_done_map, p->ino)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 269 | goto check_dot_dot; |
| 270 | /* |
| 271 | * Mark this inode as being "done"; by the time we |
| 272 | * return from this function, the inode we either be |
| 273 | * verified as being connected to the directory tree, |
| 274 | * or we will have offered to reconnect this to |
| 275 | * lost+found. |
| 276 | */ |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 277 | ext2fs_mark_inode_bitmap(inode_done_map, p->ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 278 | /* |
| 279 | * If this directory doesn't have a parent, or we've |
| 280 | * seen the parent once already, then offer to |
| 281 | * reparent it to lost+found |
| 282 | */ |
| 283 | if (!p->parent || |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 284 | (ext2fs_test_inode_bitmap(inode_loop_detect, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 285 | p->parent))) |
| 286 | break; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 287 | ext2fs_mark_inode_bitmap(inode_loop_detect, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 288 | p->parent); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 289 | p = e2fsck_get_dir_info(ctx, p->parent); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 290 | } |
| 291 | /* |
| 292 | * If we've reached here, we've hit a detached directory |
| 293 | * inode; offer to reconnect it to lost+found. |
| 294 | */ |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 295 | pctx->ino = p->ino; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 296 | if (fix_problem(ctx, PR_3_UNCONNECTED_DIR, pctx)) { |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 297 | if (e2fsck_reconnect_file(ctx, p->ino)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 298 | ext2fs_unmark_valid(fs); |
| 299 | else { |
| 300 | p->parent = lost_and_found; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 301 | fix_dotdot(ctx, p, lost_and_found); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 302 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 303 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 304 | |
| 305 | /* |
| 306 | * Make sure that .. and the parent directory are the same; |
| 307 | * offer to fix it if not. |
| 308 | */ |
| 309 | check_dot_dot: |
| 310 | if (dir->parent != dir->dotdot) { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 311 | pctx->ino = dir->ino; |
| 312 | pctx->ino2 = dir->dotdot; |
| 313 | pctx->dir = dir->parent; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 314 | if (fix_problem(ctx, PR_3_BAD_DOT_DOT, pctx)) |
| 315 | fix_dotdot(ctx, dir, dir->parent); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * This routine gets the lost_and_found inode, making it a directory |
| 321 | * if necessary |
| 322 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 323 | ino_t get_lost_and_found(e2fsck_t ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 324 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 325 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 326 | ino_t ino; |
| 327 | blk_t blk; |
| 328 | errcode_t retval; |
| 329 | struct ext2_inode inode; |
| 330 | char * block; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 331 | const char name[] = "lost+found"; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 332 | struct problem_context pctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 333 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 334 | clear_problem_context(&pctx); |
| 335 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 336 | retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, |
| 337 | sizeof(name)-1, 0, &ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 338 | if (!retval) |
| 339 | return ino; |
Theodore Ts'o | 291c904 | 1997-10-31 06:17:08 +0000 | [diff] [blame] | 340 | if (retval != EXT2_ET_FILE_NOT_FOUND) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 341 | pctx.errcode = retval; |
| 342 | fix_problem(ctx, PR_3_ERR_FIND_LPF, &pctx); |
| 343 | } |
| 344 | if (!fix_problem(ctx, PR_3_NO_LF_DIR, 0)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 345 | return 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 346 | |
| 347 | /* |
| 348 | * Read the inode and block bitmaps in; we'll be messing with |
| 349 | * them. |
| 350 | */ |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 351 | e2fsck_read_bitmaps(ctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 352 | |
| 353 | /* |
| 354 | * First, find a free block |
| 355 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 356 | retval = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 357 | if (retval) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 358 | pctx.errcode = retval; |
| 359 | fix_problem(ctx, PR_3_ERR_LPF_NEW_BLOCK, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 360 | return 0; |
| 361 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 362 | ext2fs_mark_block_bitmap(ctx->block_found_map, blk); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 363 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 364 | ext2fs_mark_bb_dirty(fs); |
| 365 | |
| 366 | /* |
| 367 | * Next find a free inode. |
| 368 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 369 | retval = ext2fs_new_inode(fs, EXT2_ROOT_INO, 040755, |
| 370 | ctx->inode_used_map, &ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 371 | if (retval) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 372 | pctx.errcode = retval; |
| 373 | fix_problem(ctx, PR_3_ERR_LPF_NEW_INODE, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 374 | return 0; |
| 375 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 376 | ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino); |
| 377 | ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 378 | ext2fs_mark_inode_bitmap(fs->inode_map, ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 379 | ext2fs_mark_ib_dirty(fs); |
| 380 | |
| 381 | /* |
| 382 | * Now let's create the actual data block for the inode |
| 383 | */ |
| 384 | retval = ext2fs_new_dir_block(fs, ino, EXT2_ROOT_INO, &block); |
| 385 | if (retval) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 386 | pctx.errcode = retval; |
| 387 | fix_problem(ctx, PR_3_ERR_LPF_NEW_DIR_BLOCK, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 391 | retval = ext2fs_write_dir_block(fs, blk, block); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 392 | ext2fs_free_mem((void **) &block); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 393 | if (retval) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 394 | pctx.errcode = retval; |
| 395 | fix_problem(ctx, PR_3_ERR_LPF_WRITE_BLOCK, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 396 | return 0; |
| 397 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 398 | |
| 399 | /* |
| 400 | * Set up the inode structure |
| 401 | */ |
| 402 | memset(&inode, 0, sizeof(inode)); |
| 403 | inode.i_mode = 040755; |
| 404 | inode.i_size = fs->blocksize; |
| 405 | inode.i_atime = inode.i_ctime = inode.i_mtime = time(0); |
| 406 | inode.i_links_count = 2; |
| 407 | inode.i_blocks = fs->blocksize / 512; |
| 408 | inode.i_block[0] = blk; |
| 409 | |
| 410 | /* |
| 411 | * Next, write out the inode. |
| 412 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 413 | pctx.errcode = ext2fs_write_inode(fs, ino, &inode); |
| 414 | if (pctx.errcode) { |
| 415 | pctx.str = "ext2fs_write_inode"; |
| 416 | fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 417 | return 0; |
| 418 | } |
| 419 | /* |
| 420 | * Finally, create the directory link |
| 421 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 422 | pctx.errcode = ext2fs_link(fs, EXT2_ROOT_INO, name, ino, 0); |
| 423 | if (pctx.errcode) { |
| 424 | pctx.str = "ext2fs_link"; |
| 425 | fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | * Miscellaneous bookkeeping that needs to be kept straight. |
| 431 | */ |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 432 | e2fsck_add_dir_info(ctx, ino, EXT2_ROOT_INO); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 433 | adjust_inode_count(ctx, EXT2_ROOT_INO, +1); |
| 434 | ext2fs_icount_store(ctx->inode_count, ino, 2); |
| 435 | ext2fs_icount_store(ctx->inode_link_info, ino, 2); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 436 | #if 0 |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 437 | printf("/lost+found created; inode #%lu\n", ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 438 | #endif |
| 439 | return ino; |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * This routine will connect a file to lost+found |
| 444 | */ |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 445 | int e2fsck_reconnect_file(e2fsck_t ctx, ino_t inode) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 446 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 447 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 448 | errcode_t retval; |
| 449 | char name[80]; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 450 | struct problem_context pctx; |
| 451 | |
| 452 | clear_problem_context(&pctx); |
| 453 | pctx.ino = inode; |
| 454 | |
| 455 | if (!bad_lost_and_found && !lost_and_found) { |
| 456 | lost_and_found = get_lost_and_found(ctx); |
| 457 | if (!lost_and_found) |
| 458 | bad_lost_and_found++; |
| 459 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 460 | if (bad_lost_and_found) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 461 | fix_problem(ctx, PR_3_NO_LPF, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 462 | return 1; |
| 463 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 464 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 465 | sprintf(name, "#%lu", inode); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 466 | retval = ext2fs_link(fs, lost_and_found, name, inode, 0); |
| 467 | if (retval == EXT2_ET_DIR_NO_SPACE) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 468 | if (!fix_problem(ctx, PR_3_EXPAND_LF_DIR, &pctx)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 469 | return 1; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 470 | retval = expand_directory(ctx, lost_and_found); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 471 | if (retval) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 472 | pctx.errcode = retval; |
| 473 | fix_problem(ctx, PR_3_CANT_EXPAND_LPF, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 474 | return 1; |
| 475 | } |
| 476 | retval = ext2fs_link(fs, lost_and_found, name, inode, 0); |
| 477 | } |
| 478 | if (retval) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 479 | pctx.errcode = retval; |
| 480 | fix_problem(ctx, PR_3_CANT_RECONNECT, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 481 | return 1; |
| 482 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 483 | adjust_inode_count(ctx, inode, +1); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * Utility routine to adjust the inode counts on an inode. |
| 490 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 491 | static errcode_t adjust_inode_count(e2fsck_t ctx, ino_t ino, int adj) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 492 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 493 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 494 | errcode_t retval; |
| 495 | struct ext2_inode inode; |
| 496 | |
| 497 | if (!ino) |
| 498 | return 0; |
| 499 | |
| 500 | retval = ext2fs_read_inode(fs, ino, &inode); |
| 501 | if (retval) |
| 502 | return retval; |
| 503 | |
| 504 | #if 0 |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 505 | 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] | 506 | inode.i_links_count); |
| 507 | #endif |
| 508 | |
| 509 | inode.i_links_count += adj; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 510 | if (adj == 1) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 511 | ext2fs_icount_increment(ctx->inode_count, ino, 0); |
| 512 | ext2fs_icount_increment(ctx->inode_link_info, ino, 0); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 513 | } else { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 514 | ext2fs_icount_decrement(ctx->inode_count, ino, 0); |
| 515 | ext2fs_icount_decrement(ctx->inode_link_info, ino, 0); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 518 | |
| 519 | retval = ext2fs_write_inode(fs, ino, &inode); |
| 520 | if (retval) |
| 521 | return retval; |
| 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * Fix parent --- this routine fixes up the parent of a directory. |
| 528 | */ |
| 529 | struct fix_dotdot_struct { |
| 530 | ext2_filsys fs; |
| 531 | ino_t parent; |
| 532 | int done; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 533 | e2fsck_t ctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 534 | }; |
| 535 | |
| 536 | static int fix_dotdot_proc(struct ext2_dir_entry *dirent, |
| 537 | int offset, |
| 538 | int blocksize, |
| 539 | char *buf, |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 540 | void *priv_data) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 541 | { |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 542 | struct fix_dotdot_struct *fp = (struct fix_dotdot_struct *) priv_data; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 543 | errcode_t retval; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 544 | struct problem_context pctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 545 | |
Theodore Ts'o | b6f7983 | 1998-03-09 13:10:37 +0000 | [diff] [blame] | 546 | if ((dirent->name_len & 0xFF) != 2) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 547 | return 0; |
| 548 | if (strncmp(dirent->name, "..", 2)) |
| 549 | return 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 550 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 551 | clear_problem_context(&pctx); |
| 552 | |
| 553 | retval = adjust_inode_count(fp->ctx, dirent->inode, -1); |
| 554 | if (retval) { |
| 555 | pctx.errcode = retval; |
| 556 | fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx); |
| 557 | } |
| 558 | retval = adjust_inode_count(fp->ctx, fp->parent, 1); |
| 559 | if (retval) { |
| 560 | pctx.errcode = retval; |
| 561 | fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx); |
| 562 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 563 | dirent->inode = fp->parent; |
| 564 | |
| 565 | fp->done++; |
| 566 | return DIRENT_ABORT | DIRENT_CHANGED; |
| 567 | } |
| 568 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 569 | static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ino_t parent) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 570 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 571 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 572 | errcode_t retval; |
| 573 | struct fix_dotdot_struct fp; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 574 | struct problem_context pctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 575 | |
| 576 | fp.fs = fs; |
| 577 | fp.parent = parent; |
| 578 | fp.done = 0; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 579 | fp.ctx = ctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 580 | |
| 581 | #if 0 |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 582 | 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] | 583 | #endif |
| 584 | |
| 585 | retval = ext2fs_dir_iterate(fs, dir->ino, DIRENT_FLAG_INCLUDE_EMPTY, |
| 586 | 0, fix_dotdot_proc, &fp); |
| 587 | if (retval || !fp.done) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 588 | clear_problem_context(&pctx); |
| 589 | pctx.ino = dir->ino; |
| 590 | pctx.errcode = retval; |
| 591 | fix_problem(ctx, retval ? PR_3_FIX_PARENT_ERR : |
| 592 | PR_3_FIX_PARENT_NOFIND, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 593 | ext2fs_unmark_valid(fs); |
| 594 | } |
| 595 | dir->dotdot = parent; |
| 596 | |
| 597 | return; |
| 598 | } |
| 599 | |
| 600 | /* |
| 601 | * These routines are responsible for expanding a /lost+found if it is |
| 602 | * too small. |
| 603 | */ |
| 604 | |
| 605 | struct expand_dir_struct { |
| 606 | int done; |
| 607 | errcode_t err; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 608 | e2fsck_t ctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 609 | }; |
| 610 | |
| 611 | static int expand_dir_proc(ext2_filsys fs, |
| 612 | blk_t *blocknr, |
| 613 | int blockcnt, |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 614 | void *priv_data) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 615 | { |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 616 | struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 617 | blk_t new_blk; |
| 618 | static blk_t last_blk = 0; |
| 619 | char *block; |
| 620 | errcode_t retval; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 621 | e2fsck_t ctx; |
| 622 | |
| 623 | ctx = es->ctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 624 | |
| 625 | if (*blocknr) { |
| 626 | last_blk = *blocknr; |
| 627 | return 0; |
| 628 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 629 | retval = ext2fs_new_block(fs, last_blk, ctx->block_found_map, |
| 630 | &new_blk); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 631 | if (retval) { |
| 632 | es->err = retval; |
| 633 | return BLOCK_ABORT; |
| 634 | } |
| 635 | if (blockcnt > 0) { |
| 636 | retval = ext2fs_new_dir_block(fs, 0, 0, &block); |
| 637 | if (retval) { |
| 638 | es->err = retval; |
| 639 | return BLOCK_ABORT; |
| 640 | } |
| 641 | es->done = 1; |
Theodore Ts'o | b8647fa | 1997-11-20 21:52:43 +0000 | [diff] [blame] | 642 | retval = ext2fs_write_dir_block(fs, new_blk, block); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 643 | } else { |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 644 | retval = ext2fs_get_mem(fs->blocksize, (void **) &block); |
| 645 | if (retval) { |
| 646 | es->err = retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 647 | return BLOCK_ABORT; |
| 648 | } |
| 649 | memset(block, 0, fs->blocksize); |
Theodore Ts'o | b8647fa | 1997-11-20 21:52:43 +0000 | [diff] [blame] | 650 | retval = io_channel_write_blk(fs->io, new_blk, 1, block); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 651 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 652 | if (retval) { |
| 653 | es->err = retval; |
| 654 | return BLOCK_ABORT; |
| 655 | } |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 656 | ext2fs_free_mem((void **) &block); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 657 | *blocknr = new_blk; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 658 | ext2fs_mark_block_bitmap(ctx->block_found_map, new_blk); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 659 | ext2fs_mark_block_bitmap(fs->block_map, new_blk); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 660 | ext2fs_mark_bb_dirty(fs); |
| 661 | if (es->done) |
| 662 | return (BLOCK_CHANGED | BLOCK_ABORT); |
| 663 | else |
| 664 | return BLOCK_CHANGED; |
| 665 | } |
| 666 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 667 | static errcode_t expand_directory(e2fsck_t ctx, ino_t dir) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 668 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 669 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 670 | errcode_t retval; |
| 671 | struct expand_dir_struct es; |
| 672 | struct ext2_inode inode; |
| 673 | |
| 674 | if (!(fs->flags & EXT2_FLAG_RW)) |
| 675 | return EXT2_ET_RO_FILSYS; |
| 676 | |
Theodore Ts'o | b8647fa | 1997-11-20 21:52:43 +0000 | [diff] [blame] | 677 | /* |
| 678 | * Read the inode and block bitmaps in; we'll be messing with |
| 679 | * them. |
| 680 | */ |
| 681 | e2fsck_read_bitmaps(ctx); |
| 682 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 683 | retval = ext2fs_check_directory(fs, dir); |
| 684 | if (retval) |
| 685 | return retval; |
| 686 | |
| 687 | es.done = 0; |
| 688 | es.err = 0; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 689 | es.ctx = ctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 690 | |
| 691 | retval = ext2fs_block_iterate(fs, dir, BLOCK_FLAG_APPEND, |
| 692 | 0, expand_dir_proc, &es); |
| 693 | |
| 694 | if (es.err) |
| 695 | return es.err; |
| 696 | if (!es.done) |
| 697 | return EXT2_ET_EXPAND_DIR_ERR; |
| 698 | |
| 699 | /* |
| 700 | * Update the size and block count fields in the inode. |
| 701 | */ |
| 702 | retval = ext2fs_read_inode(fs, dir, &inode); |
| 703 | if (retval) |
| 704 | return retval; |
| 705 | |
| 706 | inode.i_size += fs->blocksize; |
| 707 | inode.i_blocks += fs->blocksize / 512; |
| 708 | |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 709 | e2fsck_write_inode(ctx, dir, &inode, "expand_directory"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 710 | |
| 711 | return 0; |
| 712 | } |