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 | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 10 | * |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 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". |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 17 | * |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 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. |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 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 | 28db82a | 2007-04-04 22:33:31 -0400 | [diff] [blame] | 45 | static int check_directory(e2fsck_t ctx, ext2_ino_t ino, |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 46 | struct problem_context *pctx); |
Theodore Ts'o | 28db82a | 2007-04-04 22:33:31 -0400 | [diff] [blame] | 47 | static void fix_dotdot(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 48 | |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 49 | static ext2fs_inode_bitmap inode_loop_detect = 0; |
| 50 | static ext2fs_inode_bitmap inode_done_map = 0; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 51 | |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 52 | void e2fsck_pass3(e2fsck_t ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 53 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 54 | ext2_filsys fs = ctx->fs; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 55 | struct dir_info_iter *iter = NULL; |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 56 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 57 | struct resource_track rtrack; |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 58 | #endif |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 59 | struct problem_context pctx; |
| 60 | struct dir_info *dir; |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 61 | unsigned long maxdirs, count; |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 62 | |
Theodore Ts'o | 6d96b00 | 2007-08-03 20:07:09 -0400 | [diff] [blame] | 63 | init_resource_track(&rtrack, ctx->fs->io); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 64 | clear_problem_context(&pctx); |
| 65 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 66 | #ifdef MTRACE |
| 67 | mtrace_print("Pass 3"); |
| 68 | #endif |
| 69 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 70 | if (!(ctx->options & E2F_OPT_PREEN)) |
| 71 | fix_problem(ctx, PR_3_PASS_HEADER, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * Allocate some bitmaps to do loop detection. |
| 75 | */ |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 76 | pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("inode done bitmap"), |
| 77 | EXT2FS_BMAP64_AUTODIR, |
| 78 | "inode_done_map", &inode_done_map); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 79 | if (pctx.errcode) { |
| 80 | pctx.num = 2; |
| 81 | fix_problem(ctx, PR_3_ALLOCATE_IBITMAP_ERROR, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 82 | ctx->flags |= E2F_FLAG_ABORT; |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 83 | goto abort_exit; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 84 | } |
Ken Chen | 9facd07 | 2009-05-28 09:55:10 -0400 | [diff] [blame] | 85 | print_resource_track(ctx, _("Peak memory"), &ctx->global_rtrack, NULL); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 86 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 87 | check_root(ctx); |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 88 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
| 89 | goto abort_exit; |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 90 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 91 | ext2fs_mark_inode_bitmap2(inode_done_map, EXT2_ROOT_INO); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 92 | |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 93 | maxdirs = e2fsck_get_num_dirinfo(ctx); |
Theodore Ts'o | f75c28d | 1998-08-01 04:18:06 +0000 | [diff] [blame] | 94 | count = 1; |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 95 | |
Theodore Ts'o | f75c28d | 1998-08-01 04:18:06 +0000 | [diff] [blame] | 96 | if (ctx->progress) |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 97 | if ((ctx->progress)(ctx, 3, 0, maxdirs)) |
Theodore Ts'o | f75c28d | 1998-08-01 04:18:06 +0000 | [diff] [blame] | 98 | goto abort_exit; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 99 | |
Theodore Ts'o | 28db82a | 2007-04-04 22:33:31 -0400 | [diff] [blame] | 100 | iter = e2fsck_dir_info_iter_begin(ctx); |
| 101 | while ((dir = e2fsck_dir_info_iter(ctx, iter)) != 0) { |
Theodore Ts'o | 4cae045 | 2002-07-21 14:14:03 -0400 | [diff] [blame] | 102 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
| 103 | goto abort_exit; |
| 104 | if (ctx->progress && (ctx->progress)(ctx, 3, count++, maxdirs)) |
| 105 | goto abort_exit; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 106 | if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dir->ino)) |
Theodore Ts'o | 28db82a | 2007-04-04 22:33:31 -0400 | [diff] [blame] | 107 | if (check_directory(ctx, dir->ino, &pctx)) |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 108 | goto abort_exit; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 109 | } |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 110 | |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 111 | /* |
| 112 | * Force the creation of /lost+found if not present |
| 113 | */ |
Johan Erlandsson | e9a8c0c | 2013-12-19 13:55:51 -0500 | [diff] [blame] | 114 | if ((ctx->options & E2F_OPT_READONLY) == 0) |
Theodore Ts'o | 850d05e | 2002-07-25 00:00:08 -0400 | [diff] [blame] | 115 | e2fsck_get_lost_and_found(ctx, 1); |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 116 | |
Theodore Ts'o | 850d05e | 2002-07-25 00:00:08 -0400 | [diff] [blame] | 117 | /* |
| 118 | * If there are any directories that need to be indexed or |
| 119 | * optimized, do it here. |
| 120 | */ |
| 121 | e2fsck_rehash_directories(ctx); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 122 | |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 123 | abort_exit: |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 124 | if (iter) |
| 125 | e2fsck_dir_info_iter_end(ctx, iter); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 126 | e2fsck_free_dir_info(ctx); |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 127 | if (inode_loop_detect) { |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 128 | ext2fs_free_inode_bitmap(inode_loop_detect); |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 129 | inode_loop_detect = 0; |
| 130 | } |
| 131 | if (inode_done_map) { |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 132 | ext2fs_free_inode_bitmap(inode_done_map); |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 133 | inode_done_map = 0; |
| 134 | } |
Theodore Ts'o | b7a0056 | 2002-07-20 00:28:07 -0400 | [diff] [blame] | 135 | |
Ken Chen | 9facd07 | 2009-05-28 09:55:10 -0400 | [diff] [blame] | 136 | print_resource_track(ctx, _("Pass 3"), &rtrack, ctx->fs->io); |
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; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 146 | blk64_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 | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 150 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 151 | clear_problem_context(&pctx); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 152 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 153 | if (ext2fs_test_inode_bitmap2(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 | */ |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 159 | if (!(ext2fs_test_inode_bitmap2(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 | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 174 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 175 | /* |
| 176 | * First, find a free block |
| 177 | */ |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 178 | pctx.errcode = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 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 | } |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 185 | ext2fs_mark_block_bitmap2(ctx->block_found_map, blk); |
| 186 | ext2fs_mark_block_bitmap2(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 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 201 | pctx.errcode = ext2fs_write_dir_block3(fs, blk, block, 0); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 202 | if (pctx.errcode) { |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 203 | pctx.str = "ext2fs_write_dir_block3"; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 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 | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 208 | ext2fs_free_mem(&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; |
Theodore Ts'o | 1f3ad14 | 2005-04-14 14:07:53 -0400 | [diff] [blame] | 216 | inode.i_atime = inode.i_ctime = inode.i_mtime = ctx->now; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 217 | inode.i_links_count = 2; |
Theodore Ts'o | 1ca1059 | 2008-04-09 11:39:11 -0400 | [diff] [blame] | 218 | ext2fs_iblk_set(fs, &inode, 1); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 219 | inode.i_block[0] = blk; |
| 220 | |
| 221 | /* |
| 222 | * Write out the inode. |
| 223 | */ |
Theodore Ts'o | 030970e | 2005-03-20 20:05:22 -0500 | [diff] [blame] | 224 | pctx.errcode = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 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 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 231 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 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 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 239 | ext2fs_mark_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO); |
| 240 | ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, EXT2_ROOT_INO); |
| 241 | ext2fs_mark_inode_bitmap2(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. |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 252 | * |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 253 | * However, loop detection is expensive, because for very large |
| 254 | * filesystems, the inode_loop_detect bitmap is huge, and clearing it |
| 255 | * is non-trivial. Loops in filesystems are also a rare error case, |
| 256 | * and we shouldn't optimize for error cases. So we try two passes of |
| 257 | * the algorithm. The first time, we ignore loop detection and merely |
| 258 | * increment a counter; if the counter exceeds some extreme threshold, |
| 259 | * then we try again with the loop detection bitmap enabled. |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 260 | */ |
Theodore Ts'o | 28db82a | 2007-04-04 22:33:31 -0400 | [diff] [blame] | 261 | static int check_directory(e2fsck_t ctx, ext2_ino_t dir, |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 262 | struct problem_context *pctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 263 | { |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 264 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 28db82a | 2007-04-04 22:33:31 -0400 | [diff] [blame] | 265 | ext2_ino_t ino = dir, parent; |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 266 | int loop_pass = 0, parent_count = 0; |
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 | while (1) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 269 | /* |
| 270 | * Mark this inode as being "done"; by the time we |
| 271 | * return from this function, the inode we either be |
| 272 | * verified as being connected to the directory tree, |
| 273 | * or we will have offered to reconnect this to |
| 274 | * lost+found. |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 275 | * |
| 276 | * If it was marked done already, then we've reached a |
| 277 | * parent we've already checked. |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 278 | */ |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 279 | if (ext2fs_mark_inode_bitmap2(inode_done_map, ino)) |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 280 | break; |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 281 | |
Theodore Ts'o | 28db82a | 2007-04-04 22:33:31 -0400 | [diff] [blame] | 282 | if (e2fsck_dir_info_get_parent(ctx, ino, &parent)) { |
| 283 | fix_problem(ctx, PR_3_NO_DIRINFO, pctx); |
| 284 | return 0; |
| 285 | } |
| 286 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 287 | /* |
| 288 | * If this directory doesn't have a parent, or we've |
| 289 | * seen the parent once already, then offer to |
| 290 | * reparent it to lost+found |
| 291 | */ |
Theodore Ts'o | 28db82a | 2007-04-04 22:33:31 -0400 | [diff] [blame] | 292 | if (!parent || |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 293 | (loop_pass && |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 294 | (ext2fs_test_inode_bitmap2(inode_loop_detect, |
Theodore Ts'o | 28db82a | 2007-04-04 22:33:31 -0400 | [diff] [blame] | 295 | parent)))) { |
| 296 | pctx->ino = ino; |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 297 | if (fix_problem(ctx, PR_3_UNCONNECTED_DIR, pctx)) { |
Theodore Ts'o | 2e5fcce | 2003-12-12 03:00:56 -0500 | [diff] [blame] | 298 | if (e2fsck_reconnect_file(ctx, pctx->ino)) |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 299 | ext2fs_unmark_valid(fs); |
| 300 | else { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 301 | fix_dotdot(ctx, pctx->ino, |
Theodore Ts'o | 28db82a | 2007-04-04 22:33:31 -0400 | [diff] [blame] | 302 | ctx->lost_and_found); |
| 303 | parent = ctx->lost_and_found; |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 304 | } |
| 305 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 306 | break; |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 307 | } |
Theodore Ts'o | 28db82a | 2007-04-04 22:33:31 -0400 | [diff] [blame] | 308 | ino = parent; |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 309 | if (loop_pass) { |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 310 | ext2fs_mark_inode_bitmap2(inode_loop_detect, ino); |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 311 | } else if (parent_count++ > 2048) { |
| 312 | /* |
| 313 | * If we've run into a path depth that's |
| 314 | * greater than 2048, try again with the inode |
| 315 | * loop bitmap turned on and start from the |
| 316 | * top. |
| 317 | */ |
| 318 | loop_pass = 1; |
| 319 | if (inode_loop_detect) |
| 320 | ext2fs_clear_inode_bitmap(inode_loop_detect); |
| 321 | else { |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 322 | pctx->errcode = e2fsck_allocate_inode_bitmap(fs, _("inode loop detection bitmap"), EXT2FS_BMAP64_AUTODIR, "inode_loop_detect", &inode_loop_detect); |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 323 | if (pctx->errcode) { |
| 324 | pctx->num = 1; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 325 | fix_problem(ctx, |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 326 | PR_3_ALLOCATE_IBITMAP_ERROR, pctx); |
| 327 | ctx->flags |= E2F_FLAG_ABORT; |
| 328 | return -1; |
| 329 | } |
| 330 | } |
Theodore Ts'o | 28db82a | 2007-04-04 22:33:31 -0400 | [diff] [blame] | 331 | ino = dir; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 332 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 333 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 334 | |
| 335 | /* |
| 336 | * Make sure that .. and the parent directory are the same; |
| 337 | * offer to fix it if not. |
| 338 | */ |
Theodore Ts'o | 28db82a | 2007-04-04 22:33:31 -0400 | [diff] [blame] | 339 | pctx->ino = dir; |
| 340 | if (e2fsck_dir_info_get_dotdot(ctx, dir, &pctx->ino2) || |
| 341 | e2fsck_dir_info_get_parent(ctx, dir, &pctx->dir)) { |
| 342 | fix_problem(ctx, PR_3_NO_DIRINFO, pctx); |
| 343 | return 0; |
| 344 | } |
| 345 | if (pctx->ino2 != pctx->dir) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 346 | if (fix_problem(ctx, PR_3_BAD_DOT_DOT, pctx)) |
Theodore Ts'o | 28db82a | 2007-04-04 22:33:31 -0400 | [diff] [blame] | 347 | fix_dotdot(ctx, dir, pctx->dir); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 348 | } |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 349 | return 0; |
Theodore Ts'o | b7a0056 | 2002-07-20 00:28:07 -0400 | [diff] [blame] | 350 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 351 | |
| 352 | /* |
| 353 | * This routine gets the lost_and_found inode, making it a directory |
| 354 | * if necessary |
| 355 | */ |
Theodore Ts'o | 850d05e | 2002-07-25 00:00:08 -0400 | [diff] [blame] | 356 | ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 357 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 358 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 359 | ext2_ino_t ino; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 360 | blk64_t blk; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 361 | errcode_t retval; |
| 362 | struct ext2_inode inode; |
| 363 | char * block; |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 364 | static const char name[] = "lost+found"; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 365 | struct problem_context pctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 366 | |
Theodore Ts'o | 850d05e | 2002-07-25 00:00:08 -0400 | [diff] [blame] | 367 | if (ctx->lost_and_found) |
| 368 | return ctx->lost_and_found; |
| 369 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 370 | clear_problem_context(&pctx); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 371 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 372 | retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, |
| 373 | sizeof(name)-1, 0, &ino); |
Theodore Ts'o | 850d05e | 2002-07-25 00:00:08 -0400 | [diff] [blame] | 374 | if (retval && !fix) |
| 375 | return 0; |
Theodore Ts'o | 4a9f593 | 1999-03-16 19:32:52 +0000 | [diff] [blame] | 376 | if (!retval) { |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 377 | if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, ino)) { |
Theodore Ts'o | 850d05e | 2002-07-25 00:00:08 -0400 | [diff] [blame] | 378 | ctx->lost_and_found = ino; |
Theodore Ts'o | 4a9f593 | 1999-03-16 19:32:52 +0000 | [diff] [blame] | 379 | return ino; |
Theodore Ts'o | 850d05e | 2002-07-25 00:00:08 -0400 | [diff] [blame] | 380 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 381 | |
Theodore Ts'o | 4a9f593 | 1999-03-16 19:32:52 +0000 | [diff] [blame] | 382 | /* Lost+found isn't a directory! */ |
Theodore Ts'o | 850d05e | 2002-07-25 00:00:08 -0400 | [diff] [blame] | 383 | if (!fix) |
| 384 | return 0; |
Theodore Ts'o | 4a9f593 | 1999-03-16 19:32:52 +0000 | [diff] [blame] | 385 | pctx.ino = ino; |
| 386 | if (!fix_problem(ctx, PR_3_LPF_NOTDIR, &pctx)) |
| 387 | return 0; |
| 388 | |
Theodore Ts'o | c54b3c3 | 1999-07-03 07:20:06 +0000 | [diff] [blame] | 389 | /* OK, unlink the old /lost+found file. */ |
Theodore Ts'o | 4a9f593 | 1999-03-16 19:32:52 +0000 | [diff] [blame] | 390 | pctx.errcode = ext2fs_unlink(fs, EXT2_ROOT_INO, name, ino, 0); |
| 391 | if (pctx.errcode) { |
| 392 | pctx.str = "ext2fs_unlink"; |
| 393 | fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx); |
| 394 | return 0; |
| 395 | } |
Theodore Ts'o | 28db82a | 2007-04-04 22:33:31 -0400 | [diff] [blame] | 396 | (void) e2fsck_dir_info_set_parent(ctx, ino, 0); |
Theodore Ts'o | b0700a1 | 2003-03-14 01:43:56 -0500 | [diff] [blame] | 397 | e2fsck_adjust_inode_count(ctx, ino, -1); |
Theodore Ts'o | 4a9f593 | 1999-03-16 19:32:52 +0000 | [diff] [blame] | 398 | } else if (retval != EXT2_ET_FILE_NOT_FOUND) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 399 | pctx.errcode = retval; |
| 400 | fix_problem(ctx, PR_3_ERR_FIND_LPF, &pctx); |
| 401 | } |
| 402 | if (!fix_problem(ctx, PR_3_NO_LF_DIR, 0)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 403 | return 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 404 | |
| 405 | /* |
| 406 | * Read the inode and block bitmaps in; we'll be messing with |
| 407 | * them. |
| 408 | */ |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 409 | e2fsck_read_bitmaps(ctx); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 410 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 411 | /* |
| 412 | * First, find a free block |
| 413 | */ |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 414 | retval = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 415 | if (retval) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 416 | pctx.errcode = retval; |
| 417 | fix_problem(ctx, PR_3_ERR_LPF_NEW_BLOCK, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 418 | return 0; |
| 419 | } |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 420 | ext2fs_mark_block_bitmap2(ctx->block_found_map, blk); |
| 421 | ext2fs_block_alloc_stats2(fs, blk, +1); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 422 | |
| 423 | /* |
| 424 | * Next find a free inode. |
| 425 | */ |
Theodore Ts'o | b0700a1 | 2003-03-14 01:43:56 -0500 | [diff] [blame] | 426 | retval = ext2fs_new_inode(fs, EXT2_ROOT_INO, 040700, |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 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 | } |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 433 | ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino); |
| 434 | ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino); |
Theodore Ts'o | 0684a4f | 2002-08-17 10:19:44 -0400 | [diff] [blame] | 435 | ext2fs_inode_alloc_stats2(fs, ino, +1, 1); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 436 | |
| 437 | /* |
| 438 | * Now let's create the actual data block for the inode |
| 439 | */ |
| 440 | retval = ext2fs_new_dir_block(fs, ino, EXT2_ROOT_INO, &block); |
| 441 | if (retval) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 442 | pctx.errcode = retval; |
| 443 | fix_problem(ctx, PR_3_ERR_LPF_NEW_DIR_BLOCK, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 444 | return 0; |
| 445 | } |
| 446 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 447 | retval = ext2fs_write_dir_block3(fs, blk, block, 0); |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 448 | ext2fs_free_mem(&block); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 449 | if (retval) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 450 | pctx.errcode = retval; |
| 451 | fix_problem(ctx, PR_3_ERR_LPF_WRITE_BLOCK, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 452 | return 0; |
| 453 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 454 | |
| 455 | /* |
| 456 | * Set up the inode structure |
| 457 | */ |
| 458 | memset(&inode, 0, sizeof(inode)); |
Theodore Ts'o | 64aecc4 | 2002-10-11 17:44:12 -0400 | [diff] [blame] | 459 | inode.i_mode = 040700; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 460 | inode.i_size = fs->blocksize; |
Theodore Ts'o | 1f3ad14 | 2005-04-14 14:07:53 -0400 | [diff] [blame] | 461 | inode.i_atime = inode.i_ctime = inode.i_mtime = ctx->now; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 462 | inode.i_links_count = 2; |
Theodore Ts'o | 1ca1059 | 2008-04-09 11:39:11 -0400 | [diff] [blame] | 463 | ext2fs_iblk_set(fs, &inode, 1); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 464 | inode.i_block[0] = blk; |
| 465 | |
| 466 | /* |
| 467 | * Next, write out the inode. |
| 468 | */ |
Theodore Ts'o | 030970e | 2005-03-20 20:05:22 -0500 | [diff] [blame] | 469 | pctx.errcode = ext2fs_write_new_inode(fs, ino, &inode); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 470 | if (pctx.errcode) { |
| 471 | pctx.str = "ext2fs_write_inode"; |
| 472 | fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 473 | return 0; |
| 474 | } |
| 475 | /* |
| 476 | * Finally, create the directory link |
| 477 | */ |
Theodore Ts'o | 6fdc7a3 | 1999-11-10 13:34:40 +0000 | [diff] [blame] | 478 | 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] | 479 | if (pctx.errcode) { |
| 480 | pctx.str = "ext2fs_link"; |
| 481 | fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * Miscellaneous bookkeeping that needs to be kept straight. |
| 487 | */ |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 488 | e2fsck_add_dir_info(ctx, ino, EXT2_ROOT_INO); |
Theodore Ts'o | b0700a1 | 2003-03-14 01:43:56 -0500 | [diff] [blame] | 489 | e2fsck_adjust_inode_count(ctx, EXT2_ROOT_INO, 1); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 490 | ext2fs_icount_store(ctx->inode_count, ino, 2); |
| 491 | ext2fs_icount_store(ctx->inode_link_info, ino, 2); |
Theodore Ts'o | 850d05e | 2002-07-25 00:00:08 -0400 | [diff] [blame] | 492 | ctx->lost_and_found = ino; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 493 | quota_data_add(ctx->qctx, &inode, ino, fs->blocksize); |
| 494 | quota_data_inodes(ctx->qctx, &inode, ino, +1); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 495 | #if 0 |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 496 | printf("/lost+found created; inode #%lu\n", ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 497 | #endif |
| 498 | return ino; |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * This routine will connect a file to lost+found |
| 503 | */ |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 504 | int e2fsck_reconnect_file(e2fsck_t ctx, ext2_ino_t ino) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 505 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 506 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 507 | errcode_t retval; |
| 508 | char name[80]; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 509 | struct problem_context pctx; |
Theodore Ts'o | 6fdc7a3 | 1999-11-10 13:34:40 +0000 | [diff] [blame] | 510 | struct ext2_inode inode; |
| 511 | int file_type = 0; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 512 | |
| 513 | clear_problem_context(&pctx); |
Theodore Ts'o | 6fdc7a3 | 1999-11-10 13:34:40 +0000 | [diff] [blame] | 514 | pctx.ino = ino; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 515 | |
Theodore Ts'o | 850d05e | 2002-07-25 00:00:08 -0400 | [diff] [blame] | 516 | if (!ctx->bad_lost_and_found && !ctx->lost_and_found) { |
| 517 | if (e2fsck_get_lost_and_found(ctx, 1) == 0) |
| 518 | ctx->bad_lost_and_found++; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 519 | } |
Theodore Ts'o | 850d05e | 2002-07-25 00:00:08 -0400 | [diff] [blame] | 520 | if (ctx->bad_lost_and_found) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 521 | fix_problem(ctx, PR_3_NO_LPF, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 522 | return 1; |
| 523 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 524 | |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 525 | sprintf(name, "#%u", ino); |
Theodore Ts'o | 6fdc7a3 | 1999-11-10 13:34:40 +0000 | [diff] [blame] | 526 | if (ext2fs_read_inode(fs, ino, &inode) == 0) |
| 527 | file_type = ext2_file_type(inode.i_mode); |
Theodore Ts'o | 850d05e | 2002-07-25 00:00:08 -0400 | [diff] [blame] | 528 | retval = ext2fs_link(fs, ctx->lost_and_found, name, ino, file_type); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 529 | if (retval == EXT2_ET_DIR_NO_SPACE) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 530 | if (!fix_problem(ctx, PR_3_EXPAND_LF_DIR, &pctx)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 531 | return 1; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 532 | retval = e2fsck_expand_directory(ctx, ctx->lost_and_found, |
Theodore Ts'o | 850d05e | 2002-07-25 00:00:08 -0400 | [diff] [blame] | 533 | 1, 0); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 534 | if (retval) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 535 | pctx.errcode = retval; |
| 536 | fix_problem(ctx, PR_3_CANT_EXPAND_LPF, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 537 | return 1; |
| 538 | } |
Theodore Ts'o | 850d05e | 2002-07-25 00:00:08 -0400 | [diff] [blame] | 539 | retval = ext2fs_link(fs, ctx->lost_and_found, name, |
| 540 | ino, file_type); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 541 | } |
| 542 | if (retval) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 543 | pctx.errcode = retval; |
| 544 | fix_problem(ctx, PR_3_CANT_RECONNECT, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 545 | return 1; |
| 546 | } |
Theodore Ts'o | b0700a1 | 2003-03-14 01:43:56 -0500 | [diff] [blame] | 547 | e2fsck_adjust_inode_count(ctx, ino, 1); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * Utility routine to adjust the inode counts on an inode. |
| 554 | */ |
Theodore Ts'o | b0700a1 | 2003-03-14 01:43:56 -0500 | [diff] [blame] | 555 | errcode_t e2fsck_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] | 556 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 557 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 558 | errcode_t retval; |
| 559 | struct ext2_inode inode; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 560 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 561 | if (!ino) |
| 562 | return 0; |
| 563 | |
| 564 | retval = ext2fs_read_inode(fs, ino, &inode); |
| 565 | if (retval) |
| 566 | return retval; |
| 567 | |
| 568 | #if 0 |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 569 | 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] | 570 | inode.i_links_count); |
| 571 | #endif |
| 572 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 573 | if (adj == 1) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 574 | ext2fs_icount_increment(ctx->inode_count, ino, 0); |
Theodore Ts'o | c1faf9c | 1999-09-14 20:00:54 +0000 | [diff] [blame] | 575 | if (inode.i_links_count == (__u16) ~0) |
| 576 | return 0; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 577 | ext2fs_icount_increment(ctx->inode_link_info, ino, 0); |
Theodore Ts'o | c1faf9c | 1999-09-14 20:00:54 +0000 | [diff] [blame] | 578 | inode.i_links_count++; |
| 579 | } else if (adj == -1) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 580 | ext2fs_icount_decrement(ctx->inode_count, ino, 0); |
Theodore Ts'o | c1faf9c | 1999-09-14 20:00:54 +0000 | [diff] [blame] | 581 | if (inode.i_links_count == 0) |
| 582 | return 0; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 583 | ext2fs_icount_decrement(ctx->inode_link_info, ino, 0); |
Theodore Ts'o | c1faf9c | 1999-09-14 20:00:54 +0000 | [diff] [blame] | 584 | inode.i_links_count--; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 585 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 586 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 587 | retval = ext2fs_write_inode(fs, ino, &inode); |
| 588 | if (retval) |
| 589 | return retval; |
| 590 | |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | /* |
| 595 | * Fix parent --- this routine fixes up the parent of a directory. |
| 596 | */ |
| 597 | struct fix_dotdot_struct { |
| 598 | ext2_filsys fs; |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 599 | ext2_ino_t parent; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 600 | int done; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 601 | e2fsck_t ctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 602 | }; |
| 603 | |
| 604 | static int fix_dotdot_proc(struct ext2_dir_entry *dirent, |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 605 | int offset EXT2FS_ATTR((unused)), |
| 606 | int blocksize EXT2FS_ATTR((unused)), |
| 607 | char *buf EXT2FS_ATTR((unused)), |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 608 | void *priv_data) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 609 | { |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 610 | struct fix_dotdot_struct *fp = (struct fix_dotdot_struct *) priv_data; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 611 | errcode_t retval; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 612 | struct problem_context pctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 613 | |
Theodore Ts'o | b6f7983 | 1998-03-09 13:10:37 +0000 | [diff] [blame] | 614 | if ((dirent->name_len & 0xFF) != 2) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 615 | return 0; |
| 616 | if (strncmp(dirent->name, "..", 2)) |
| 617 | return 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 618 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 619 | clear_problem_context(&pctx); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 620 | |
Theodore Ts'o | b0700a1 | 2003-03-14 01:43:56 -0500 | [diff] [blame] | 621 | retval = e2fsck_adjust_inode_count(fp->ctx, dirent->inode, -1); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 622 | if (retval) { |
| 623 | pctx.errcode = retval; |
| 624 | fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx); |
| 625 | } |
Theodore Ts'o | b0700a1 | 2003-03-14 01:43:56 -0500 | [diff] [blame] | 626 | retval = e2fsck_adjust_inode_count(fp->ctx, fp->parent, 1); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 627 | if (retval) { |
| 628 | pctx.errcode = retval; |
| 629 | fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx); |
| 630 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 631 | dirent->inode = fp->parent; |
Theodore Ts'o | 56c8c59 | 2007-03-31 19:18:24 -0400 | [diff] [blame] | 632 | if (fp->ctx->fs->super->s_feature_incompat & |
| 633 | EXT2_FEATURE_INCOMPAT_FILETYPE) |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 634 | dirent->name_len = (dirent->name_len & 0xFF) | |
Theodore Ts'o | 56c8c59 | 2007-03-31 19:18:24 -0400 | [diff] [blame] | 635 | (EXT2_FT_DIR << 8); |
| 636 | else |
| 637 | dirent->name_len = dirent->name_len & 0xFF; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 638 | |
| 639 | fp->done++; |
| 640 | return DIRENT_ABORT | DIRENT_CHANGED; |
| 641 | } |
| 642 | |
Theodore Ts'o | 28db82a | 2007-04-04 22:33:31 -0400 | [diff] [blame] | 643 | static void fix_dotdot(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 644 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 645 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 646 | errcode_t retval; |
| 647 | struct fix_dotdot_struct fp; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 648 | struct problem_context pctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 649 | |
| 650 | fp.fs = fs; |
| 651 | fp.parent = parent; |
| 652 | fp.done = 0; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 653 | fp.ctx = ctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 654 | |
| 655 | #if 0 |
Theodore Ts'o | 28db82a | 2007-04-04 22:33:31 -0400 | [diff] [blame] | 656 | printf("Fixing '..' of inode %lu to be %lu...\n", ino, parent); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 657 | #endif |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 658 | |
Theodore Ts'o | 28db82a | 2007-04-04 22:33:31 -0400 | [diff] [blame] | 659 | clear_problem_context(&pctx); |
| 660 | pctx.ino = ino; |
| 661 | retval = ext2fs_dir_iterate(fs, ino, DIRENT_FLAG_INCLUDE_EMPTY, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 662 | 0, fix_dotdot_proc, &fp); |
| 663 | if (retval || !fp.done) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 664 | pctx.errcode = retval; |
| 665 | fix_problem(ctx, retval ? PR_3_FIX_PARENT_ERR : |
| 666 | PR_3_FIX_PARENT_NOFIND, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 667 | ext2fs_unmark_valid(fs); |
| 668 | } |
Theodore Ts'o | 28db82a | 2007-04-04 22:33:31 -0400 | [diff] [blame] | 669 | (void) e2fsck_dir_info_set_dotdot(ctx, ino, parent); |
| 670 | if (e2fsck_dir_info_set_parent(ctx, ino, ctx->lost_and_found)) |
| 671 | fix_problem(ctx, PR_3_NO_DIRINFO, &pctx); |
| 672 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 673 | return; |
| 674 | } |
| 675 | |
| 676 | /* |
| 677 | * These routines are responsible for expanding a /lost+found if it is |
| 678 | * too small. |
| 679 | */ |
| 680 | |
| 681 | struct expand_dir_struct { |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 682 | blk64_t num; |
| 683 | e2_blkcnt_t guaranteed_size; |
| 684 | blk64_t newblocks; |
| 685 | blk64_t last_block; |
Theodore Ts'o | c1faf9c | 1999-09-14 20:00:54 +0000 | [diff] [blame] | 686 | errcode_t err; |
| 687 | e2fsck_t ctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 688 | }; |
| 689 | |
| 690 | static int expand_dir_proc(ext2_filsys fs, |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 691 | blk64_t *blocknr, |
Theodore Ts'o | 133a56d | 2000-11-17 05:40:49 +0000 | [diff] [blame] | 692 | e2_blkcnt_t blockcnt, |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 693 | blk64_t ref_block EXT2FS_ATTR((unused)), |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 694 | int ref_offset EXT2FS_ATTR((unused)), |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 695 | void *priv_data) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 696 | { |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 697 | struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 698 | blk64_t new_blk; |
| 699 | static blk64_t last_blk = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 700 | char *block; |
| 701 | errcode_t retval; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 702 | e2fsck_t ctx; |
| 703 | |
| 704 | ctx = es->ctx; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 705 | |
Theodore Ts'o | b7a0056 | 2002-07-20 00:28:07 -0400 | [diff] [blame] | 706 | if (es->guaranteed_size && blockcnt >= es->guaranteed_size) |
| 707 | return BLOCK_ABORT; |
| 708 | |
| 709 | if (blockcnt > 0) |
| 710 | es->last_block = blockcnt; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 711 | if (*blocknr) { |
| 712 | last_blk = *blocknr; |
| 713 | return 0; |
| 714 | } |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 715 | |
| 716 | if (blockcnt && |
| 717 | (EXT2FS_B2C(fs, last_blk) == EXT2FS_B2C(fs, last_blk + 1))) |
| 718 | new_blk = last_blk + 1; |
| 719 | else { |
| 720 | last_blk &= ~EXT2FS_CLUSTER_MASK(fs); |
| 721 | retval = ext2fs_new_block2(fs, last_blk, ctx->block_found_map, |
| 722 | &new_blk); |
| 723 | if (retval) { |
| 724 | es->err = retval; |
| 725 | return BLOCK_ABORT; |
| 726 | } |
| 727 | es->newblocks++; |
| 728 | ext2fs_block_alloc_stats2(fs, new_blk, +1); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 729 | } |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 730 | last_blk = new_blk; |
| 731 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 732 | if (blockcnt > 0) { |
| 733 | retval = ext2fs_new_dir_block(fs, 0, 0, &block); |
| 734 | if (retval) { |
| 735 | es->err = retval; |
| 736 | return BLOCK_ABORT; |
| 737 | } |
Theodore Ts'o | b7a0056 | 2002-07-20 00:28:07 -0400 | [diff] [blame] | 738 | es->num--; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 739 | retval = ext2fs_write_dir_block3(fs, new_blk, block, 0); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 740 | } else { |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 741 | retval = ext2fs_get_mem(fs->blocksize, &block); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 742 | if (retval) { |
| 743 | es->err = retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 744 | return BLOCK_ABORT; |
| 745 | } |
| 746 | memset(block, 0, fs->blocksize); |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 747 | retval = io_channel_write_blk64(fs->io, new_blk, 1, block); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 748 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 749 | if (retval) { |
| 750 | es->err = retval; |
| 751 | return BLOCK_ABORT; |
| 752 | } |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 753 | ext2fs_free_mem(&block); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 754 | *blocknr = new_blk; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 755 | ext2fs_mark_block_bitmap2(ctx->block_found_map, new_blk); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 756 | |
Theodore Ts'o | b7a0056 | 2002-07-20 00:28:07 -0400 | [diff] [blame] | 757 | if (es->num == 0) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 758 | return (BLOCK_CHANGED | BLOCK_ABORT); |
| 759 | else |
| 760 | return BLOCK_CHANGED; |
| 761 | } |
| 762 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 763 | /* |
| 764 | * Ensure that all blocks are marked in the block_found_map, since it's |
| 765 | * possible that the library allocated an extent node block or a block map |
| 766 | * block during the directory rebuilding; these new allocations are not |
| 767 | * captured in block_found_map. This is bad since we could later use |
| 768 | * block_found_map to allocate more blocks. |
| 769 | */ |
| 770 | static int find_new_blocks_proc(ext2_filsys fs, |
| 771 | blk64_t *blocknr, |
| 772 | e2_blkcnt_t blockcnt, |
| 773 | blk64_t ref_block EXT2FS_ATTR((unused)), |
| 774 | int ref_offset EXT2FS_ATTR((unused)), |
| 775 | void *priv_data) |
| 776 | { |
| 777 | struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data; |
| 778 | e2fsck_t ctx = es->ctx; |
| 779 | |
| 780 | ext2fs_mark_block_bitmap2(ctx->block_found_map, *blocknr); |
| 781 | return 0; |
| 782 | } |
| 783 | |
Theodore Ts'o | b7a0056 | 2002-07-20 00:28:07 -0400 | [diff] [blame] | 784 | errcode_t e2fsck_expand_directory(e2fsck_t ctx, ext2_ino_t dir, |
| 785 | int num, int guaranteed_size) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 786 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 787 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 788 | errcode_t retval; |
| 789 | struct expand_dir_struct es; |
| 790 | struct ext2_inode inode; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 791 | blk64_t sz, before, after; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 792 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 793 | if (!(fs->flags & EXT2_FLAG_RW)) |
| 794 | return EXT2_ET_RO_FILSYS; |
| 795 | |
Theodore Ts'o | b8647fa | 1997-11-20 21:52:43 +0000 | [diff] [blame] | 796 | /* |
| 797 | * Read the inode and block bitmaps in; we'll be messing with |
| 798 | * them. |
| 799 | */ |
| 800 | e2fsck_read_bitmaps(ctx); |
Theodore Ts'o | c1faf9c | 1999-09-14 20:00:54 +0000 | [diff] [blame] | 801 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 802 | retval = ext2fs_check_directory(fs, dir); |
| 803 | if (retval) |
| 804 | return retval; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 805 | |
Theodore Ts'o | b7a0056 | 2002-07-20 00:28:07 -0400 | [diff] [blame] | 806 | es.num = num; |
| 807 | es.guaranteed_size = guaranteed_size; |
| 808 | es.last_block = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 809 | es.err = 0; |
Theodore Ts'o | c1faf9c | 1999-09-14 20:00:54 +0000 | [diff] [blame] | 810 | es.newblocks = 0; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 811 | es.ctx = ctx; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 812 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 813 | before = ext2fs_free_blocks_count(fs->super); |
| 814 | retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_APPEND, |
Theodore Ts'o | 133a56d | 2000-11-17 05:40:49 +0000 | [diff] [blame] | 815 | 0, expand_dir_proc, &es); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 816 | |
| 817 | if (es.err) |
| 818 | return es.err; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 819 | after = ext2fs_free_blocks_count(fs->super); |
| 820 | |
| 821 | /* |
| 822 | * If the free block count has dropped by more than the blocks we |
| 823 | * allocated ourselves, then we must've allocated some extent/map |
| 824 | * blocks. Therefore, we must iterate this dir's blocks again to |
| 825 | * ensure that all newly allocated blocks are captured in |
| 826 | * block_found_map. |
| 827 | */ |
| 828 | if ((before - after) > es.newblocks) { |
| 829 | retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_READ_ONLY, |
| 830 | 0, find_new_blocks_proc, &es); |
| 831 | if (es.err) |
| 832 | return es.err; |
| 833 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 834 | |
| 835 | /* |
| 836 | * Update the size and block count fields in the inode. |
| 837 | */ |
| 838 | retval = ext2fs_read_inode(fs, dir, &inode); |
| 839 | if (retval) |
| 840 | return retval; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 841 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 842 | sz = (es.last_block + 1) * fs->blocksize; |
| 843 | inode.i_size = sz; |
| 844 | inode.i_size_high = sz >> 32; |
Theodore Ts'o | 1ca1059 | 2008-04-09 11:39:11 -0400 | [diff] [blame] | 845 | ext2fs_iblk_add_blocks(fs, &inode, es.newblocks); |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 846 | quota_data_add(ctx->qctx, &inode, dir, es.newblocks * fs->blocksize); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 847 | |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 848 | e2fsck_write_inode(ctx, dir, &inode, "expand_directory"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 849 | |
| 850 | return 0; |
| 851 | } |
Theodore Ts'o | b7a0056 | 2002-07-20 00:28:07 -0400 | [diff] [blame] | 852 | |