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