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