blob: 817bf0873bb784dea507cd46b8152c3930f4d0f9 [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'of3db3561997-04-26 13:34:30 +000055static ext2fs_inode_bitmap inode_loop_detect;
56static ext2fs_inode_bitmap inode_done_map;
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'of8188ff1997-11-14 05:23:04 +000067 unsigned long max, count;
68
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;
91 return;
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;
99 return;
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'o08b21301997-11-03 19:42:40 +0000107 if (ctx->flags & E2F_FLAG_ABORT)
108 return;
109
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'of8188ff1997-11-14 05:23:04 +0000112 max = e2fsck_get_num_dirinfo(ctx);
113 count = 0;
114
115 if (ctx->progress)
116 (ctx->progress)(ctx, 3, 0, max);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000117 for (i=0; (dir = e2fsck_dir_info_iter(ctx, &i)) != 0;) {
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000118 if (ctx->progress)
119 (ctx->progress)(ctx, 3, count++, max);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000120 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dir->ino))
121 check_directory(ctx, dir, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000122 }
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000123 if (ctx->progress)
124 (ctx->progress)(ctx, 3, max, max);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000125
Theodore Ts'o08b21301997-11-03 19:42:40 +0000126 e2fsck_free_dir_info(ctx);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000127 ext2fs_free_inode_bitmap(inode_loop_detect);
128 ext2fs_free_inode_bitmap(inode_done_map);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000129#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000130 if (ctx->options & E2F_OPT_TIME2)
131 print_resource_track("Pass 3", &rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000132#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000133}
134
135/*
136 * This makes sure the root inode is present; if not, we ask if the
137 * user wants us to create it. Not creating it is a fatal error.
138 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000139static void check_root(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000140{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000141 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000142 blk_t blk;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000143 struct ext2_inode inode;
144 char * block;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000145 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000146
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000147 clear_problem_context(&pctx);
148
149 if (ext2fs_test_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000150 /*
Theodore Ts'o08b21301997-11-03 19:42:40 +0000151 * If the root inode is not a directory, die here. The
Theodore Ts'o3839e651997-04-26 13:21:57 +0000152 * user must have answered 'no' in pass1 when we
153 * offered to clear it.
154 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000155 if (!(ext2fs_test_inode_bitmap(ctx->inode_dir_map,
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000156 EXT2_ROOT_INO))) {
157 fix_problem(ctx, PR_3_ROOT_NOT_DIR_ABORT, &pctx);
158 ctx->flags |= E2F_FLAG_ABORT;
159 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000160 return;
161 }
162
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000163 if (!fix_problem(ctx, PR_3_NO_ROOT_INODE, &pctx)) {
164 fix_problem(ctx, PR_3_NO_ROOT_INODE_ABORT, &pctx);
165 ctx->flags |= E2F_FLAG_ABORT;
166 return;
167 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000168
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000169 e2fsck_read_bitmaps(ctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000170
171 /*
172 * First, find a free block
173 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000174 pctx.errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
175 if (pctx.errcode) {
176 pctx.str = "ext2fs_new_block";
177 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000178 ctx->flags |= E2F_FLAG_ABORT;
179 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000180 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000181 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000182 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000183 ext2fs_mark_bb_dirty(fs);
184
185 /*
186 * Now let's create the actual data block for the inode
187 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000188 pctx.errcode = ext2fs_new_dir_block(fs, EXT2_ROOT_INO, EXT2_ROOT_INO,
189 &block);
190 if (pctx.errcode) {
191 pctx.str = "ext2fs_new_dir_block";
192 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000193 ctx->flags |= E2F_FLAG_ABORT;
194 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000195 }
196
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000197 pctx.errcode = ext2fs_write_dir_block(fs, blk, block);
198 if (pctx.errcode) {
199 pctx.str = "ext2fs_write_dir_block";
200 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000201 ctx->flags |= E2F_FLAG_ABORT;
202 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000203 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000204 ext2fs_free_mem((void **) &block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000205
206 /*
207 * Set up the inode structure
208 */
209 memset(&inode, 0, sizeof(inode));
210 inode.i_mode = 040755;
211 inode.i_size = fs->blocksize;
212 inode.i_atime = inode.i_ctime = inode.i_mtime = time(0);
213 inode.i_links_count = 2;
214 inode.i_blocks = fs->blocksize / 512;
215 inode.i_block[0] = blk;
216
217 /*
218 * Write out the inode.
219 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000220 pctx.errcode = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
221 if (pctx.errcode) {
222 pctx.str = "ext2fs_write_inode";
223 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000224 ctx->flags |= E2F_FLAG_ABORT;
225 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000226 }
227
228 /*
229 * Miscellaneous bookkeeping...
230 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000231 e2fsck_add_dir_info(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000232 ext2fs_icount_store(ctx->inode_count, EXT2_ROOT_INO, 2);
233 ext2fs_icount_store(ctx->inode_link_info, EXT2_ROOT_INO, 2);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000234
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000235 ext2fs_mark_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO);
236 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, EXT2_ROOT_INO);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000237 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_ROOT_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000238 ext2fs_mark_ib_dirty(fs);
239}
240
241/*
242 * This subroutine is responsible for making sure that a particular
243 * directory is connected to the root; if it isn't we trace it up as
244 * far as we can go, and then offer to connect the resulting parent to
245 * the lost+found. We have to do loop detection; if we ever discover
246 * a loop, we treat that as a disconnected directory and offer to
247 * reparent it to lost+found.
248 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000249static void check_directory(e2fsck_t ctx, struct dir_info *dir,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000250 struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000251{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000252 ext2_filsys fs = ctx->fs;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000253 struct dir_info *p = dir;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000254
Theodore Ts'of3db3561997-04-26 13:34:30 +0000255 ext2fs_clear_inode_bitmap(inode_loop_detect);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000256 while (p) {
257 /*
258 * If we find a parent which we've already checked,
259 * then stop; we know it's either already connected to
260 * the directory tree, or it isn't but the user has
261 * already told us he doesn't want us to reconnect the
262 * disconnected subtree.
263 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000264 if (ext2fs_test_inode_bitmap(inode_done_map, p->ino))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000265 goto check_dot_dot;
266 /*
267 * Mark this inode as being "done"; by the time we
268 * return from this function, the inode we either be
269 * verified as being connected to the directory tree,
270 * or we will have offered to reconnect this to
271 * lost+found.
272 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000273 ext2fs_mark_inode_bitmap(inode_done_map, p->ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000274 /*
275 * If this directory doesn't have a parent, or we've
276 * seen the parent once already, then offer to
277 * reparent it to lost+found
278 */
279 if (!p->parent ||
Theodore Ts'of3db3561997-04-26 13:34:30 +0000280 (ext2fs_test_inode_bitmap(inode_loop_detect,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000281 p->parent)))
282 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000283 ext2fs_mark_inode_bitmap(inode_loop_detect,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000284 p->parent);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000285 p = e2fsck_get_dir_info(ctx, p->parent);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000286 }
287 /*
288 * If we've reached here, we've hit a detached directory
289 * inode; offer to reconnect it to lost+found.
290 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000291 pctx->ino = p->ino;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000292 if (fix_problem(ctx, PR_3_UNCONNECTED_DIR, pctx)) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000293 if (e2fsck_reconnect_file(ctx, p->ino))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000294 ext2fs_unmark_valid(fs);
295 else {
296 p->parent = lost_and_found;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000297 fix_dotdot(ctx, p, lost_and_found);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000298 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000299 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000300
301 /*
302 * Make sure that .. and the parent directory are the same;
303 * offer to fix it if not.
304 */
305check_dot_dot:
306 if (dir->parent != dir->dotdot) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000307 pctx->ino = dir->ino;
308 pctx->ino2 = dir->dotdot;
309 pctx->dir = dir->parent;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000310 if (fix_problem(ctx, PR_3_BAD_DOT_DOT, pctx))
311 fix_dotdot(ctx, dir, dir->parent);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000312 }
313}
314
315/*
316 * This routine gets the lost_and_found inode, making it a directory
317 * if necessary
318 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000319ino_t get_lost_and_found(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000320{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000321 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000322 ino_t ino;
323 blk_t blk;
324 errcode_t retval;
325 struct ext2_inode inode;
326 char * block;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000327 const char name[] = "lost+found";
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000328 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000329
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000330 clear_problem_context(&pctx);
331
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000332 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name,
333 sizeof(name)-1, 0, &ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000334 if (!retval)
335 return ino;
Theodore Ts'o291c9041997-10-31 06:17:08 +0000336 if (retval != EXT2_ET_FILE_NOT_FOUND) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000337 pctx.errcode = retval;
338 fix_problem(ctx, PR_3_ERR_FIND_LPF, &pctx);
339 }
340 if (!fix_problem(ctx, PR_3_NO_LF_DIR, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000341 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000342
343 /*
344 * Read the inode and block bitmaps in; we'll be messing with
345 * them.
346 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000347 e2fsck_read_bitmaps(ctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000348
349 /*
350 * First, find a free block
351 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000352 retval = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000353 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000354 pctx.errcode = retval;
355 fix_problem(ctx, PR_3_ERR_LPF_NEW_BLOCK, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000356 return 0;
357 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000358 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000359 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000360 ext2fs_mark_bb_dirty(fs);
361
362 /*
363 * Next find a free inode.
364 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000365 retval = ext2fs_new_inode(fs, EXT2_ROOT_INO, 040755,
366 ctx->inode_used_map, &ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000367 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000368 pctx.errcode = retval;
369 fix_problem(ctx, PR_3_ERR_LPF_NEW_INODE, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000370 return 0;
371 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000372 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
373 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000374 ext2fs_mark_inode_bitmap(fs->inode_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000375 ext2fs_mark_ib_dirty(fs);
376
377 /*
378 * Now let's create the actual data block for the inode
379 */
380 retval = ext2fs_new_dir_block(fs, ino, EXT2_ROOT_INO, &block);
381 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000382 pctx.errcode = retval;
383 fix_problem(ctx, PR_3_ERR_LPF_NEW_DIR_BLOCK, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000384 return 0;
385 }
386
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000387 retval = ext2fs_write_dir_block(fs, blk, block);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000388 ext2fs_free_mem((void **) &block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000389 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000390 pctx.errcode = retval;
391 fix_problem(ctx, PR_3_ERR_LPF_WRITE_BLOCK, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000392 return 0;
393 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000394
395 /*
396 * Set up the inode structure
397 */
398 memset(&inode, 0, sizeof(inode));
399 inode.i_mode = 040755;
400 inode.i_size = fs->blocksize;
401 inode.i_atime = inode.i_ctime = inode.i_mtime = time(0);
402 inode.i_links_count = 2;
403 inode.i_blocks = fs->blocksize / 512;
404 inode.i_block[0] = blk;
405
406 /*
407 * Next, write out the inode.
408 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000409 pctx.errcode = ext2fs_write_inode(fs, ino, &inode);
410 if (pctx.errcode) {
411 pctx.str = "ext2fs_write_inode";
412 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000413 return 0;
414 }
415 /*
416 * Finally, create the directory link
417 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000418 pctx.errcode = ext2fs_link(fs, EXT2_ROOT_INO, name, ino, 0);
419 if (pctx.errcode) {
420 pctx.str = "ext2fs_link";
421 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000422 return 0;
423 }
424
425 /*
426 * Miscellaneous bookkeeping that needs to be kept straight.
427 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000428 e2fsck_add_dir_info(ctx, ino, EXT2_ROOT_INO);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000429 adjust_inode_count(ctx, EXT2_ROOT_INO, +1);
430 ext2fs_icount_store(ctx->inode_count, ino, 2);
431 ext2fs_icount_store(ctx->inode_link_info, ino, 2);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000432#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000433 printf("/lost+found created; inode #%lu\n", ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000434#endif
435 return ino;
436}
437
438/*
439 * This routine will connect a file to lost+found
440 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000441int e2fsck_reconnect_file(e2fsck_t ctx, ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000442{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000443 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000444 errcode_t retval;
445 char name[80];
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000446 struct problem_context pctx;
447
448 clear_problem_context(&pctx);
449 pctx.ino = inode;
450
451 if (!bad_lost_and_found && !lost_and_found) {
452 lost_and_found = get_lost_and_found(ctx);
453 if (!lost_and_found)
454 bad_lost_and_found++;
455 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000456 if (bad_lost_and_found) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000457 fix_problem(ctx, PR_3_NO_LPF, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000458 return 1;
459 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000460
Theodore Ts'of3db3561997-04-26 13:34:30 +0000461 sprintf(name, "#%lu", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000462 retval = ext2fs_link(fs, lost_and_found, name, inode, 0);
463 if (retval == EXT2_ET_DIR_NO_SPACE) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000464 if (!fix_problem(ctx, PR_3_EXPAND_LF_DIR, &pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000465 return 1;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000466 retval = expand_directory(ctx, lost_and_found);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000467 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000468 pctx.errcode = retval;
469 fix_problem(ctx, PR_3_CANT_EXPAND_LPF, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000470 return 1;
471 }
472 retval = ext2fs_link(fs, lost_and_found, name, inode, 0);
473 }
474 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000475 pctx.errcode = retval;
476 fix_problem(ctx, PR_3_CANT_RECONNECT, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000477 return 1;
478 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000479 adjust_inode_count(ctx, inode, +1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000480
481 return 0;
482}
483
484/*
485 * Utility routine to adjust the inode counts on an inode.
486 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000487static errcode_t adjust_inode_count(e2fsck_t ctx, ino_t ino, int adj)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000488{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000489 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000490 errcode_t retval;
491 struct ext2_inode inode;
492
493 if (!ino)
494 return 0;
495
496 retval = ext2fs_read_inode(fs, ino, &inode);
497 if (retval)
498 return retval;
499
500#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000501 printf("Adjusting link count for inode %lu by %d (from %d)\n", ino, adj,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000502 inode.i_links_count);
503#endif
504
505 inode.i_links_count += adj;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000506 if (adj == 1) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000507 ext2fs_icount_increment(ctx->inode_count, ino, 0);
508 ext2fs_icount_increment(ctx->inode_link_info, ino, 0);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000509 } else {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000510 ext2fs_icount_decrement(ctx->inode_count, ino, 0);
511 ext2fs_icount_decrement(ctx->inode_link_info, ino, 0);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000512 }
513
Theodore Ts'o3839e651997-04-26 13:21:57 +0000514
515 retval = ext2fs_write_inode(fs, ino, &inode);
516 if (retval)
517 return retval;
518
519 return 0;
520}
521
522/*
523 * Fix parent --- this routine fixes up the parent of a directory.
524 */
525struct fix_dotdot_struct {
526 ext2_filsys fs;
527 ino_t parent;
528 int done;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000529 e2fsck_t ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000530};
531
532static int fix_dotdot_proc(struct ext2_dir_entry *dirent,
533 int offset,
534 int blocksize,
535 char *buf,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000536 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000537{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000538 struct fix_dotdot_struct *fp = (struct fix_dotdot_struct *) priv_data;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000539 errcode_t retval;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000540 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000541
542 if (dirent->name_len != 2)
543 return 0;
544 if (strncmp(dirent->name, "..", 2))
545 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000546
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000547 clear_problem_context(&pctx);
548
549 retval = adjust_inode_count(fp->ctx, dirent->inode, -1);
550 if (retval) {
551 pctx.errcode = retval;
552 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
553 }
554 retval = adjust_inode_count(fp->ctx, fp->parent, 1);
555 if (retval) {
556 pctx.errcode = retval;
557 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
558 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000559 dirent->inode = fp->parent;
560
561 fp->done++;
562 return DIRENT_ABORT | DIRENT_CHANGED;
563}
564
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000565static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ino_t parent)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000566{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000567 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000568 errcode_t retval;
569 struct fix_dotdot_struct fp;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000570 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000571
572 fp.fs = fs;
573 fp.parent = parent;
574 fp.done = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000575 fp.ctx = ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000576
577#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000578 printf("Fixing '..' of inode %lu to be %lu...\n", dir->ino, parent);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000579#endif
580
581 retval = ext2fs_dir_iterate(fs, dir->ino, DIRENT_FLAG_INCLUDE_EMPTY,
582 0, fix_dotdot_proc, &fp);
583 if (retval || !fp.done) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000584 clear_problem_context(&pctx);
585 pctx.ino = dir->ino;
586 pctx.errcode = retval;
587 fix_problem(ctx, retval ? PR_3_FIX_PARENT_ERR :
588 PR_3_FIX_PARENT_NOFIND, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000589 ext2fs_unmark_valid(fs);
590 }
591 dir->dotdot = parent;
592
593 return;
594}
595
596/*
597 * These routines are responsible for expanding a /lost+found if it is
598 * too small.
599 */
600
601struct expand_dir_struct {
602 int done;
603 errcode_t err;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000604 e2fsck_t ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000605};
606
607static int expand_dir_proc(ext2_filsys fs,
608 blk_t *blocknr,
609 int blockcnt,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000610 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000611{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000612 struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000613 blk_t new_blk;
614 static blk_t last_blk = 0;
615 char *block;
616 errcode_t retval;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000617 e2fsck_t ctx;
618
619 ctx = es->ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000620
621 if (*blocknr) {
622 last_blk = *blocknr;
623 return 0;
624 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000625 retval = ext2fs_new_block(fs, last_blk, ctx->block_found_map,
626 &new_blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000627 if (retval) {
628 es->err = retval;
629 return BLOCK_ABORT;
630 }
631 if (blockcnt > 0) {
632 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
633 if (retval) {
634 es->err = retval;
635 return BLOCK_ABORT;
636 }
637 es->done = 1;
Theodore Ts'ob8647fa1997-11-20 21:52:43 +0000638 retval = ext2fs_write_dir_block(fs, new_blk, block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000639 } else {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000640 retval = ext2fs_get_mem(fs->blocksize, (void **) &block);
641 if (retval) {
642 es->err = retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000643 return BLOCK_ABORT;
644 }
645 memset(block, 0, fs->blocksize);
Theodore Ts'ob8647fa1997-11-20 21:52:43 +0000646 retval = io_channel_write_blk(fs->io, new_blk, 1, block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000647 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000648 if (retval) {
649 es->err = retval;
650 return BLOCK_ABORT;
651 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000652 ext2fs_free_mem((void **) &block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000653 *blocknr = new_blk;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000654 ext2fs_mark_block_bitmap(ctx->block_found_map, new_blk);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000655 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000656 ext2fs_mark_bb_dirty(fs);
657 if (es->done)
658 return (BLOCK_CHANGED | BLOCK_ABORT);
659 else
660 return BLOCK_CHANGED;
661}
662
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000663static errcode_t expand_directory(e2fsck_t ctx, ino_t dir)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000664{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000665 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000666 errcode_t retval;
667 struct expand_dir_struct es;
668 struct ext2_inode inode;
669
670 if (!(fs->flags & EXT2_FLAG_RW))
671 return EXT2_ET_RO_FILSYS;
672
Theodore Ts'ob8647fa1997-11-20 21:52:43 +0000673 /*
674 * Read the inode and block bitmaps in; we'll be messing with
675 * them.
676 */
677 e2fsck_read_bitmaps(ctx);
678
Theodore Ts'o3839e651997-04-26 13:21:57 +0000679 retval = ext2fs_check_directory(fs, dir);
680 if (retval)
681 return retval;
682
683 es.done = 0;
684 es.err = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000685 es.ctx = ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000686
687 retval = ext2fs_block_iterate(fs, dir, BLOCK_FLAG_APPEND,
688 0, expand_dir_proc, &es);
689
690 if (es.err)
691 return es.err;
692 if (!es.done)
693 return EXT2_ET_EXPAND_DIR_ERR;
694
695 /*
696 * Update the size and block count fields in the inode.
697 */
698 retval = ext2fs_read_inode(fs, dir, &inode);
699 if (retval)
700 return retval;
701
702 inode.i_size += fs->blocksize;
703 inode.i_blocks += fs->blocksize / 512;
704
Theodore Ts'o08b21301997-11-03 19:42:40 +0000705 e2fsck_write_inode(ctx, dir, &inode, "expand_directory");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000706
707 return 0;
708}