blob: 07006982f2cda96f54bbf3b2bd280fd86bad0a79 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * pass3.c -- pass #3 of e2fsck: Check for directory connectivity
3 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00004 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 *
11 * Pass #3 assures that all directories are connected to the
12 * filesystem tree, using the following algorithm:
13 *
14 * First, the root directory is checked to make sure it exists; if
15 * not, e2fsck will offer to create a new one. It is then marked as
16 * "done".
17 *
18 * Then, pass3 interates over all directory inodes; for each directory
19 * it attempts to trace up the filesystem tree, using dirinfo.parent
20 * until it reaches a directory which has been marked "done". If it
21 * can not do so, then the directory must be disconnected, and e2fsck
22 * will offer to reconnect it to /lost+found. While it is chasing
23 * parent pointers up the filesystem tree, if pass3 sees a directory
24 * twice, then it has detected a filesystem loop, and it will again
25 * offer to reconnect the directory to /lost+found in to break the
26 * filesystem loop.
27 *
Theodore Ts'o08b21301997-11-03 19:42:40 +000028 * 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'o3839e651997-04-26 13:21:57 +000032 *
33 * Pass 3 frees the following data structures:
34 * - The dirinfo directory information cache.
35 */
36
Theodore Ts'o50e1e101997-04-26 13:58:21 +000037#ifdef HAVE_ERRNO_H
38#include <errno.h>
39#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000040
41#include "e2fsck.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000042#include "problem.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000043
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000044static void check_root(e2fsck_t ctx);
45static void check_directory(e2fsck_t ctx, struct dir_info *dir,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000046 struct problem_context *pctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000047static ino_t get_lost_and_found(e2fsck_t ctx);
48static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ino_t parent);
49static errcode_t adjust_inode_count(e2fsck_t ctx, ino_t ino, int adj);
50static errcode_t expand_directory(e2fsck_t ctx, ino_t dir);
Theodore Ts'o3839e651997-04-26 13:21:57 +000051
52static ino_t lost_and_found = 0;
53static int bad_lost_and_found = 0;
54
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +000055static ext2fs_inode_bitmap inode_loop_detect = 0;
56static ext2fs_inode_bitmap inode_done_map = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000057
Theodore Ts'o08b21301997-11-03 19:42:40 +000058void e2fsck_pass3(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +000059{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000060 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +000061 int i;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000062#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000063 struct resource_track rtrack;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000064#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +000065 struct problem_context pctx;
66 struct dir_info *dir;
Theodore Ts'o7f813ba1998-09-03 01:26:03 +000067 unsigned long maxdirs, count;
Theodore Ts'of8188ff1997-11-14 05:23:04 +000068
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000069#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000070 init_resource_track(&rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000071#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000072
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000073 clear_problem_context(&pctx);
74
Theodore Ts'o3839e651997-04-26 13:21:57 +000075#ifdef MTRACE
76 mtrace_print("Pass 3");
77#endif
78
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000079 if (!(ctx->options & E2F_OPT_PREEN))
80 fix_problem(ctx, PR_3_PASS_HEADER, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +000081
82 /*
83 * Allocate some bitmaps to do loop detection.
84 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000085 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
86 "inode loop detection bitmap", &inode_loop_detect);
87 if (pctx.errcode) {
88 pctx.num = 1;
89 fix_problem(ctx, PR_3_ALLOCATE_IBITMAP_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +000090 ctx->flags |= E2F_FLAG_ABORT;
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +000091 goto abort_exit;
Theodore Ts'o3839e651997-04-26 13:21:57 +000092 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000093 pctx.errcode = ext2fs_allocate_inode_bitmap(fs, "inode done bitmap",
94 &inode_done_map);
95 if (pctx.errcode) {
96 pctx.num = 2;
97 fix_problem(ctx, PR_3_ALLOCATE_IBITMAP_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +000098 ctx->flags |= E2F_FLAG_ABORT;
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +000099 goto abort_exit;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000100 }
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000101#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000102 if (ctx->options & E2F_OPT_TIME)
103 print_resource_track("Peak memory", &ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000104#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000105
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000106 check_root(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000107 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
108 goto abort_exit;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000109
Theodore Ts'of3db3561997-04-26 13:34:30 +0000110 ext2fs_mark_inode_bitmap(inode_done_map, EXT2_ROOT_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000111
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000112 maxdirs = e2fsck_get_num_dirinfo(ctx);
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000113 count = 1;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000114
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000115 if (ctx->progress)
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000116 if ((ctx->progress)(ctx, 3, 0, maxdirs))
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000117 goto abort_exit;
118
Theodore Ts'o08b21301997-11-03 19:42:40 +0000119 for (i=0; (dir = e2fsck_dir_info_iter(ctx, &i)) != 0;) {
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000120 if (ctx->progress)
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000121 if ((ctx->progress)(ctx, 3, count++, maxdirs))
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000122 goto abort_exit;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000123 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dir->ino))
124 check_directory(ctx, dir, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000125 }
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000126
127abort_exit:
Theodore Ts'o08b21301997-11-03 19:42:40 +0000128 e2fsck_free_dir_info(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000129 if (inode_loop_detect)
130 ext2fs_free_inode_bitmap(inode_loop_detect);
131 if (inode_done_map)
132 ext2fs_free_inode_bitmap(inode_done_map);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000133#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000134 if (ctx->options & E2F_OPT_TIME2)
135 print_resource_track("Pass 3", &rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000136#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000137}
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'o1b6bf171997-10-03 17:48:10 +0000143static void check_root(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000144{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000145 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000146 blk_t blk;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000147 struct ext2_inode inode;
148 char * block;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000149 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000150
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000151 clear_problem_context(&pctx);
152
153 if (ext2fs_test_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000154 /*
Theodore Ts'o08b21301997-11-03 19:42:40 +0000155 * If the root inode is not a directory, die here. The
Theodore Ts'o3839e651997-04-26 13:21:57 +0000156 * user must have answered 'no' in pass1 when we
157 * offered to clear it.
158 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000159 if (!(ext2fs_test_inode_bitmap(ctx->inode_dir_map,
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000160 EXT2_ROOT_INO))) {
161 fix_problem(ctx, PR_3_ROOT_NOT_DIR_ABORT, &pctx);
162 ctx->flags |= E2F_FLAG_ABORT;
163 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000164 return;
165 }
166
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000167 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'o3839e651997-04-26 13:21:57 +0000172
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000173 e2fsck_read_bitmaps(ctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000174
175 /*
176 * First, find a free block
177 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000178 pctx.errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
179 if (pctx.errcode) {
180 pctx.str = "ext2fs_new_block";
181 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000182 ctx->flags |= E2F_FLAG_ABORT;
183 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000184 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000185 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000186 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000187 ext2fs_mark_bb_dirty(fs);
188
189 /*
190 * Now let's create the actual data block for the inode
191 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000192 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'o08b21301997-11-03 19:42:40 +0000197 ctx->flags |= E2F_FLAG_ABORT;
198 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000199 }
200
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000201 pctx.errcode = ext2fs_write_dir_block(fs, blk, block);
202 if (pctx.errcode) {
203 pctx.str = "ext2fs_write_dir_block";
204 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000205 ctx->flags |= E2F_FLAG_ABORT;
206 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000207 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000208 ext2fs_free_mem((void **) &block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000209
210 /*
211 * Set up the inode structure
212 */
213 memset(&inode, 0, sizeof(inode));
214 inode.i_mode = 040755;
215 inode.i_size = fs->blocksize;
216 inode.i_atime = inode.i_ctime = inode.i_mtime = time(0);
217 inode.i_links_count = 2;
218 inode.i_blocks = fs->blocksize / 512;
219 inode.i_block[0] = blk;
220
221 /*
222 * Write out the inode.
223 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000224 pctx.errcode = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
225 if (pctx.errcode) {
226 pctx.str = "ext2fs_write_inode";
227 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000228 ctx->flags |= E2F_FLAG_ABORT;
229 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000230 }
231
232 /*
233 * Miscellaneous bookkeeping...
234 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000235 e2fsck_add_dir_info(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000236 ext2fs_icount_store(ctx->inode_count, EXT2_ROOT_INO, 2);
237 ext2fs_icount_store(ctx->inode_link_info, EXT2_ROOT_INO, 2);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000238
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000239 ext2fs_mark_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO);
240 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, EXT2_ROOT_INO);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000241 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_ROOT_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000242 ext2fs_mark_ib_dirty(fs);
243}
244
245/*
246 * This subroutine is responsible for making sure that a particular
247 * directory is connected to the root; if it isn't we trace it up as
248 * far as we can go, and then offer to connect the resulting parent to
249 * the lost+found. We have to do loop detection; if we ever discover
250 * a loop, we treat that as a disconnected directory and offer to
251 * reparent it to lost+found.
252 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000253static void check_directory(e2fsck_t ctx, struct dir_info *dir,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000254 struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000255{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000256 ext2_filsys fs = ctx->fs;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000257 struct dir_info *p = dir;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000258
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000259 if (!p)
260 return;
261
Theodore Ts'of3db3561997-04-26 13:34:30 +0000262 ext2fs_clear_inode_bitmap(inode_loop_detect);
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000263
264 /*
265 * Keep going until we find a parent which we've already
266 * checked. We know it's either already connected to the
267 * directory tree, or it isn't but the user has already told
268 * us he doesn't want us to reconnect the disconnected
269 * subtree.
270 */
271 while (!ext2fs_test_inode_bitmap(inode_done_map, p->ino)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000272 /*
273 * Mark this inode as being "done"; by the time we
274 * return from this function, the inode we either be
275 * verified as being connected to the directory tree,
276 * or we will have offered to reconnect this to
277 * lost+found.
278 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000279 ext2fs_mark_inode_bitmap(inode_done_map, p->ino);
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000280
Theodore Ts'o3839e651997-04-26 13:21:57 +0000281 /*
282 * If this directory doesn't have a parent, or we've
283 * seen the parent once already, then offer to
284 * reparent it to lost+found
285 */
286 if (!p->parent ||
Theodore Ts'of3db3561997-04-26 13:34:30 +0000287 (ext2fs_test_inode_bitmap(inode_loop_detect,
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000288 p->parent))) {
289 pctx->ino = p->ino;
290 if (fix_problem(ctx, PR_3_UNCONNECTED_DIR, pctx)) {
291 if (e2fsck_reconnect_file(ctx, p->ino))
292 ext2fs_unmark_valid(fs);
293 else {
294 p->parent = lost_and_found;
295 fix_dotdot(ctx, p, lost_and_found);
296 }
297 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000298 break;
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000299 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000300 ext2fs_mark_inode_bitmap(inode_loop_detect,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000301 p->parent);
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000302 pctx->ino = p->parent;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000303 p = e2fsck_get_dir_info(ctx, p->parent);
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000304 if (!p) {
305 fix_problem(ctx, PR_3_NO_DIRINFO, pctx);
306 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000307 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000308 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000309
310 /*
311 * Make sure that .. and the parent directory are the same;
312 * offer to fix it if not.
313 */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000314 if (dir->parent != dir->dotdot) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000315 pctx->ino = dir->ino;
316 pctx->ino2 = dir->dotdot;
317 pctx->dir = dir->parent;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000318 if (fix_problem(ctx, PR_3_BAD_DOT_DOT, pctx))
319 fix_dotdot(ctx, dir, dir->parent);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000320 }
321}
322
323/*
324 * This routine gets the lost_and_found inode, making it a directory
325 * if necessary
326 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000327ino_t get_lost_and_found(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000328{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000329 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000330 ino_t ino;
331 blk_t blk;
332 errcode_t retval;
333 struct ext2_inode inode;
334 char * block;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000335 const char name[] = "lost+found";
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000336 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000337
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000338 clear_problem_context(&pctx);
339
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000340 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name,
341 sizeof(name)-1, 0, &ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000342 if (!retval)
343 return ino;
Theodore Ts'o291c9041997-10-31 06:17:08 +0000344 if (retval != EXT2_ET_FILE_NOT_FOUND) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000345 pctx.errcode = retval;
346 fix_problem(ctx, PR_3_ERR_FIND_LPF, &pctx);
347 }
348 if (!fix_problem(ctx, PR_3_NO_LF_DIR, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000349 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000350
351 /*
352 * Read the inode and block bitmaps in; we'll be messing with
353 * them.
354 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000355 e2fsck_read_bitmaps(ctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000356
357 /*
358 * First, find a free block
359 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000360 retval = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000361 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000362 pctx.errcode = retval;
363 fix_problem(ctx, PR_3_ERR_LPF_NEW_BLOCK, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000364 return 0;
365 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000366 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000367 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000368 ext2fs_mark_bb_dirty(fs);
369
370 /*
371 * Next find a free inode.
372 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000373 retval = ext2fs_new_inode(fs, EXT2_ROOT_INO, 040755,
374 ctx->inode_used_map, &ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000375 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000376 pctx.errcode = retval;
377 fix_problem(ctx, PR_3_ERR_LPF_NEW_INODE, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000378 return 0;
379 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000380 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
381 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000382 ext2fs_mark_inode_bitmap(fs->inode_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000383 ext2fs_mark_ib_dirty(fs);
384
385 /*
386 * Now let's create the actual data block for the inode
387 */
388 retval = ext2fs_new_dir_block(fs, ino, EXT2_ROOT_INO, &block);
389 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000390 pctx.errcode = retval;
391 fix_problem(ctx, PR_3_ERR_LPF_NEW_DIR_BLOCK, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000392 return 0;
393 }
394
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000395 retval = ext2fs_write_dir_block(fs, blk, block);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000396 ext2fs_free_mem((void **) &block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000397 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000398 pctx.errcode = retval;
399 fix_problem(ctx, PR_3_ERR_LPF_WRITE_BLOCK, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000400 return 0;
401 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000402
403 /*
404 * Set up the inode structure
405 */
406 memset(&inode, 0, sizeof(inode));
407 inode.i_mode = 040755;
408 inode.i_size = fs->blocksize;
409 inode.i_atime = inode.i_ctime = inode.i_mtime = time(0);
410 inode.i_links_count = 2;
411 inode.i_blocks = fs->blocksize / 512;
412 inode.i_block[0] = blk;
413
414 /*
415 * Next, write out the inode.
416 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000417 pctx.errcode = ext2fs_write_inode(fs, ino, &inode);
418 if (pctx.errcode) {
419 pctx.str = "ext2fs_write_inode";
420 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000421 return 0;
422 }
423 /*
424 * Finally, create the directory link
425 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000426 pctx.errcode = ext2fs_link(fs, EXT2_ROOT_INO, name, ino, 0);
427 if (pctx.errcode) {
428 pctx.str = "ext2fs_link";
429 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000430 return 0;
431 }
432
433 /*
434 * Miscellaneous bookkeeping that needs to be kept straight.
435 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000436 e2fsck_add_dir_info(ctx, ino, EXT2_ROOT_INO);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000437 adjust_inode_count(ctx, EXT2_ROOT_INO, +1);
438 ext2fs_icount_store(ctx->inode_count, ino, 2);
439 ext2fs_icount_store(ctx->inode_link_info, ino, 2);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000440#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000441 printf("/lost+found created; inode #%lu\n", ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000442#endif
443 return ino;
444}
445
446/*
447 * This routine will connect a file to lost+found
448 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000449int e2fsck_reconnect_file(e2fsck_t ctx, ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000450{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000451 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000452 errcode_t retval;
453 char name[80];
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000454 struct problem_context pctx;
455
456 clear_problem_context(&pctx);
457 pctx.ino = inode;
458
459 if (!bad_lost_and_found && !lost_and_found) {
460 lost_and_found = get_lost_and_found(ctx);
461 if (!lost_and_found)
462 bad_lost_and_found++;
463 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000464 if (bad_lost_and_found) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000465 fix_problem(ctx, PR_3_NO_LPF, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000466 return 1;
467 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000468
Theodore Ts'of3db3561997-04-26 13:34:30 +0000469 sprintf(name, "#%lu", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000470 retval = ext2fs_link(fs, lost_and_found, name, inode, 0);
471 if (retval == EXT2_ET_DIR_NO_SPACE) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000472 if (!fix_problem(ctx, PR_3_EXPAND_LF_DIR, &pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000473 return 1;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000474 retval = expand_directory(ctx, lost_and_found);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000475 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000476 pctx.errcode = retval;
477 fix_problem(ctx, PR_3_CANT_EXPAND_LPF, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000478 return 1;
479 }
480 retval = ext2fs_link(fs, lost_and_found, name, inode, 0);
481 }
482 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000483 pctx.errcode = retval;
484 fix_problem(ctx, PR_3_CANT_RECONNECT, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000485 return 1;
486 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000487 adjust_inode_count(ctx, inode, +1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000488
489 return 0;
490}
491
492/*
493 * Utility routine to adjust the inode counts on an inode.
494 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000495static errcode_t adjust_inode_count(e2fsck_t ctx, ino_t ino, int adj)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000496{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000497 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000498 errcode_t retval;
499 struct ext2_inode inode;
500
501 if (!ino)
502 return 0;
503
504 retval = ext2fs_read_inode(fs, ino, &inode);
505 if (retval)
506 return retval;
507
508#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000509 printf("Adjusting link count for inode %lu by %d (from %d)\n", ino, adj,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000510 inode.i_links_count);
511#endif
512
513 inode.i_links_count += adj;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000514 if (adj == 1) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000515 ext2fs_icount_increment(ctx->inode_count, ino, 0);
516 ext2fs_icount_increment(ctx->inode_link_info, ino, 0);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000517 } else {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000518 ext2fs_icount_decrement(ctx->inode_count, ino, 0);
519 ext2fs_icount_decrement(ctx->inode_link_info, ino, 0);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000520 }
521
Theodore Ts'o3839e651997-04-26 13:21:57 +0000522
523 retval = ext2fs_write_inode(fs, ino, &inode);
524 if (retval)
525 return retval;
526
527 return 0;
528}
529
530/*
531 * Fix parent --- this routine fixes up the parent of a directory.
532 */
533struct fix_dotdot_struct {
534 ext2_filsys fs;
535 ino_t parent;
536 int done;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000537 e2fsck_t ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000538};
539
540static int fix_dotdot_proc(struct ext2_dir_entry *dirent,
541 int offset,
542 int blocksize,
543 char *buf,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000544 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000545{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000546 struct fix_dotdot_struct *fp = (struct fix_dotdot_struct *) priv_data;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000547 errcode_t retval;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000548 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000549
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000550 if ((dirent->name_len & 0xFF) != 2)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000551 return 0;
552 if (strncmp(dirent->name, "..", 2))
553 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000554
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000555 clear_problem_context(&pctx);
556
557 retval = adjust_inode_count(fp->ctx, dirent->inode, -1);
558 if (retval) {
559 pctx.errcode = retval;
560 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
561 }
562 retval = adjust_inode_count(fp->ctx, fp->parent, 1);
563 if (retval) {
564 pctx.errcode = retval;
565 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
566 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000567 dirent->inode = fp->parent;
568
569 fp->done++;
570 return DIRENT_ABORT | DIRENT_CHANGED;
571}
572
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000573static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ino_t parent)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000574{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000575 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000576 errcode_t retval;
577 struct fix_dotdot_struct fp;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000578 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000579
580 fp.fs = fs;
581 fp.parent = parent;
582 fp.done = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000583 fp.ctx = ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000584
585#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000586 printf("Fixing '..' of inode %lu to be %lu...\n", dir->ino, parent);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000587#endif
588
589 retval = ext2fs_dir_iterate(fs, dir->ino, DIRENT_FLAG_INCLUDE_EMPTY,
590 0, fix_dotdot_proc, &fp);
591 if (retval || !fp.done) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000592 clear_problem_context(&pctx);
593 pctx.ino = dir->ino;
594 pctx.errcode = retval;
595 fix_problem(ctx, retval ? PR_3_FIX_PARENT_ERR :
596 PR_3_FIX_PARENT_NOFIND, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000597 ext2fs_unmark_valid(fs);
598 }
599 dir->dotdot = parent;
600
601 return;
602}
603
604/*
605 * These routines are responsible for expanding a /lost+found if it is
606 * too small.
607 */
608
609struct expand_dir_struct {
610 int done;
611 errcode_t err;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000612 e2fsck_t ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000613};
614
615static int expand_dir_proc(ext2_filsys fs,
616 blk_t *blocknr,
617 int blockcnt,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000618 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000619{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000620 struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000621 blk_t new_blk;
622 static blk_t last_blk = 0;
623 char *block;
624 errcode_t retval;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000625 e2fsck_t ctx;
626
627 ctx = es->ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628
629 if (*blocknr) {
630 last_blk = *blocknr;
631 return 0;
632 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000633 retval = ext2fs_new_block(fs, last_blk, ctx->block_found_map,
634 &new_blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000635 if (retval) {
636 es->err = retval;
637 return BLOCK_ABORT;
638 }
639 if (blockcnt > 0) {
640 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
641 if (retval) {
642 es->err = retval;
643 return BLOCK_ABORT;
644 }
645 es->done = 1;
Theodore Ts'ob8647fa1997-11-20 21:52:43 +0000646 retval = ext2fs_write_dir_block(fs, new_blk, block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000647 } else {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000648 retval = ext2fs_get_mem(fs->blocksize, (void **) &block);
649 if (retval) {
650 es->err = retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000651 return BLOCK_ABORT;
652 }
653 memset(block, 0, fs->blocksize);
Theodore Ts'ob8647fa1997-11-20 21:52:43 +0000654 retval = io_channel_write_blk(fs->io, new_blk, 1, block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000655 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000656 if (retval) {
657 es->err = retval;
658 return BLOCK_ABORT;
659 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000660 ext2fs_free_mem((void **) &block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000661 *blocknr = new_blk;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000662 ext2fs_mark_block_bitmap(ctx->block_found_map, new_blk);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000663 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000664 ext2fs_mark_bb_dirty(fs);
665 if (es->done)
666 return (BLOCK_CHANGED | BLOCK_ABORT);
667 else
668 return BLOCK_CHANGED;
669}
670
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000671static errcode_t expand_directory(e2fsck_t ctx, ino_t dir)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000672{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000673 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000674 errcode_t retval;
675 struct expand_dir_struct es;
676 struct ext2_inode inode;
677
678 if (!(fs->flags & EXT2_FLAG_RW))
679 return EXT2_ET_RO_FILSYS;
680
Theodore Ts'ob8647fa1997-11-20 21:52:43 +0000681 /*
682 * Read the inode and block bitmaps in; we'll be messing with
683 * them.
684 */
685 e2fsck_read_bitmaps(ctx);
686
Theodore Ts'o3839e651997-04-26 13:21:57 +0000687 retval = ext2fs_check_directory(fs, dir);
688 if (retval)
689 return retval;
690
691 es.done = 0;
692 es.err = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000693 es.ctx = ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000694
695 retval = ext2fs_block_iterate(fs, dir, BLOCK_FLAG_APPEND,
696 0, expand_dir_proc, &es);
697
698 if (es.err)
699 return es.err;
700 if (!es.done)
701 return EXT2_ET_EXPAND_DIR_ERR;
702
703 /*
704 * Update the size and block count fields in the inode.
705 */
706 retval = ext2fs_read_inode(fs, dir, &inode);
707 if (retval)
708 return retval;
709
710 inode.i_size += fs->blocksize;
711 inode.i_blocks += fs->blocksize / 512;
712
Theodore Ts'o08b21301997-11-03 19:42:40 +0000713 e2fsck_write_inode(ctx, dir, &inode, "expand_directory");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000714
715 return 0;
716}