• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * pass2.c --- check directory structure
3  *
4  * 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%
10  *
11  * Pass 2 of e2fsck iterates through all active directory inodes, and
12  * applies to following tests to each directory entry in the directory
13  * blocks in the inodes:
14  *
15  *	- The length of the directory entry (rec_len) should be at
16  * 		least 8 bytes, and no more than the remaining space
17  * 		left in the directory block.
18  * 	- The length of the name in the directory entry (name_len)
19  * 		should be less than (rec_len - 8).
20  *	- The inode number in the directory entry should be within
21  * 		legal bounds.
22  * 	- The inode number should refer to a in-use inode.
23  *	- The first entry should be '.', and its inode should be
24  * 		the inode of the directory.
25  * 	- The second entry should be '..'.
26  *
27  * To minimize disk seek time, the directory blocks are processed in
28  * sorted order of block numbers.
29  *
30  * Pass 2 also collects the following information:
31  * 	- The inode numbers of the subdirectories for each directory.
32  *
33  * Pass 2 relies on the following information from previous passes:
34  * 	- The directory information collected in pass 1.
35  * 	- The inode_used_map bitmap
36  * 	- The inode_bad_map bitmap
37  * 	- The inode_dir_map bitmap
38  *
39  * Pass 2 frees the following data structures
40  * 	- The inode_bad_map bitmap
41  * 	- The inode_reg_map bitmap
42  */
43 
44 #define _GNU_SOURCE 1 /* get strnlen() */
45 #include <string.h>
46 
47 #include "e2fsck.h"
48 #include "problem.h"
49 #include "dict.h"
50 
51 #ifdef NO_INLINE_FUNCS
52 #define _INLINE_
53 #else
54 #define _INLINE_ inline
55 #endif
56 
57 /* #define DX_DEBUG */
58 
59 /*
60  * Keeps track of how many times an inode is referenced.
61  */
62 static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf);
63 static int check_dir_block(ext2_filsys fs,
64 			   struct ext2_db_entry2 *dir_blocks_info,
65 			   void *priv_data);
66 static int allocate_dir_block(e2fsck_t ctx,
67 			      struct ext2_db_entry2 *dir_blocks_info,
68 			      char *buf, struct problem_context *pctx);
69 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino);
70 static int htree_depth(struct dx_dir_info *dx_dir,
71 		       struct dx_dirblock_info *dx_db);
72 static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b);
73 
74 struct check_dir_struct {
75 	char *buf;
76 	struct problem_context	pctx;
77 	int	count, max;
78 	e2fsck_t ctx;
79 };
80 
e2fsck_pass2(e2fsck_t ctx)81 void e2fsck_pass2(e2fsck_t ctx)
82 {
83 	struct ext2_super_block *sb = ctx->fs->super;
84 	struct problem_context	pctx;
85 	ext2_filsys 		fs = ctx->fs;
86 	char			*buf;
87 #ifdef RESOURCE_TRACK
88 	struct resource_track	rtrack;
89 #endif
90 	struct check_dir_struct cd;
91 	struct dx_dir_info	*dx_dir;
92 	struct dx_dirblock_info	*dx_db, *dx_parent;
93 	unsigned int		save_type;
94 	int			b;
95 	int			i, depth;
96 	problem_t		code;
97 	int			bad_dir;
98 
99 	init_resource_track(&rtrack, ctx->fs->io);
100 	clear_problem_context(&cd.pctx);
101 
102 #ifdef MTRACE
103 	mtrace_print("Pass 2");
104 #endif
105 
106 	if (!(ctx->options & E2F_OPT_PREEN))
107 		fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
108 
109 	e2fsck_setup_tdb_icount(ctx, EXT2_ICOUNT_OPT_INCREMENT,
110 				&ctx->inode_count);
111 	if (ctx->inode_count)
112 		cd.pctx.errcode = 0;
113 	else {
114 		e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
115 				       "inode_count", &save_type);
116 		cd.pctx.errcode = ext2fs_create_icount2(fs,
117 						EXT2_ICOUNT_OPT_INCREMENT,
118 						0, ctx->inode_link_info,
119 						&ctx->inode_count);
120 		fs->default_bitmap_type = save_type;
121 	}
122 	if (cd.pctx.errcode) {
123 		fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
124 		ctx->flags |= E2F_FLAG_ABORT;
125 		return;
126 	}
127 	buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize,
128 					      "directory scan buffer");
129 
130 	/*
131 	 * Set up the parent pointer for the root directory, if
132 	 * present.  (If the root directory is not present, we will
133 	 * create it in pass 3.)
134 	 */
135 	(void) e2fsck_dir_info_set_parent(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
136 
137 	cd.buf = buf;
138 	cd.ctx = ctx;
139 	cd.count = 1;
140 	cd.max = ext2fs_dblist_count2(fs->dblist);
141 
142 	if (ctx->progress)
143 		(void) (ctx->progress)(ctx, 2, 0, cd.max);
144 
145 	if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX)
146 		ext2fs_dblist_sort2(fs->dblist, special_dir_block_cmp);
147 
148 	cd.pctx.errcode = ext2fs_dblist_iterate2(fs->dblist, check_dir_block,
149 						 &cd);
150 	if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
151 		return;
152 
153 	if (ctx->flags & E2F_FLAG_RESTART_LATER) {
154 		ctx->flags |= E2F_FLAG_RESTART;
155 		return;
156 	}
157 
158 	if (cd.pctx.errcode) {
159 		fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
160 		ctx->flags |= E2F_FLAG_ABORT;
161 		return;
162 	}
163 
164 #ifdef ENABLE_HTREE
165 	for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) {
166 		if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
167 			return;
168 		if (dx_dir->numblocks == 0)
169 			continue;
170 		clear_problem_context(&pctx);
171 		bad_dir = 0;
172 		pctx.dir = dx_dir->ino;
173 		dx_db = dx_dir->dx_block;
174 		if (dx_db->flags & DX_FLAG_REFERENCED)
175 			dx_db->flags |= DX_FLAG_DUP_REF;
176 		else
177 			dx_db->flags |= DX_FLAG_REFERENCED;
178 		/*
179 		 * Find all of the first and last leaf blocks, and
180 		 * update their parent's min and max hash values
181 		 */
182 		for (b=0, dx_db = dx_dir->dx_block;
183 		     b < dx_dir->numblocks;
184 		     b++, dx_db++) {
185 			if ((dx_db->type != DX_DIRBLOCK_LEAF) ||
186 			    !(dx_db->flags & (DX_FLAG_FIRST | DX_FLAG_LAST)))
187 				continue;
188 			dx_parent = &dx_dir->dx_block[dx_db->parent];
189 			/*
190 			 * XXX Make sure dx_parent->min_hash > dx_db->min_hash
191 			 */
192 			if (dx_db->flags & DX_FLAG_FIRST)
193 				dx_parent->min_hash = dx_db->min_hash;
194 			/*
195 			 * XXX Make sure dx_parent->max_hash < dx_db->max_hash
196 			 */
197 			if (dx_db->flags & DX_FLAG_LAST)
198 				dx_parent->max_hash = dx_db->max_hash;
199 		}
200 
201 		for (b=0, dx_db = dx_dir->dx_block;
202 		     b < dx_dir->numblocks;
203 		     b++, dx_db++) {
204 			pctx.blkcount = b;
205 			pctx.group = dx_db->parent;
206 			code = 0;
207 			if (!(dx_db->flags & DX_FLAG_FIRST) &&
208 			    (dx_db->min_hash < dx_db->node_min_hash)) {
209 				pctx.blk = dx_db->min_hash;
210 				pctx.blk2 = dx_db->node_min_hash;
211 				code = PR_2_HTREE_MIN_HASH;
212 				fix_problem(ctx, code, &pctx);
213 				bad_dir++;
214 			}
215 			if (dx_db->type == DX_DIRBLOCK_LEAF) {
216 				depth = htree_depth(dx_dir, dx_db);
217 				if (depth != dx_dir->depth) {
218 					pctx.num = dx_dir->depth;
219 					code = PR_2_HTREE_BAD_DEPTH;
220 					fix_problem(ctx, code, &pctx);
221 					bad_dir++;
222 				}
223 			}
224 			/*
225 			 * This test doesn't apply for the root block
226 			 * at block #0
227 			 */
228 			if (b &&
229 			    (dx_db->max_hash > dx_db->node_max_hash)) {
230 				pctx.blk = dx_db->max_hash;
231 				pctx.blk2 = dx_db->node_max_hash;
232 				code = PR_2_HTREE_MAX_HASH;
233 				fix_problem(ctx, code, &pctx);
234 				bad_dir++;
235 			}
236 			if (!(dx_db->flags & DX_FLAG_REFERENCED)) {
237 				code = PR_2_HTREE_NOTREF;
238 				fix_problem(ctx, code, &pctx);
239 				bad_dir++;
240 			} else if (dx_db->flags & DX_FLAG_DUP_REF) {
241 				code = PR_2_HTREE_DUPREF;
242 				fix_problem(ctx, code, &pctx);
243 				bad_dir++;
244 			}
245 		}
246 		if (bad_dir && fix_problem(ctx, PR_2_HTREE_CLEAR, &pctx)) {
247 			clear_htree(ctx, dx_dir->ino);
248 			dx_dir->numblocks = 0;
249 		}
250 	}
251 	e2fsck_free_dx_dir_info(ctx);
252 #endif
253 	ext2fs_free_mem(&buf);
254 	ext2fs_free_dblist(fs->dblist);
255 
256 	if (ctx->inode_bad_map) {
257 		ext2fs_free_inode_bitmap(ctx->inode_bad_map);
258 		ctx->inode_bad_map = 0;
259 	}
260 	if (ctx->inode_reg_map) {
261 		ext2fs_free_inode_bitmap(ctx->inode_reg_map);
262 		ctx->inode_reg_map = 0;
263 	}
264 	if (ctx->encrypted_dirs) {
265 		ext2fs_u32_list_free(ctx->encrypted_dirs);
266 		ctx->encrypted_dirs = 0;
267 	}
268 
269 	clear_problem_context(&pctx);
270 	if (ctx->large_files) {
271 		if (!(sb->s_feature_ro_compat &
272 		      EXT2_FEATURE_RO_COMPAT_LARGE_FILE) &&
273 		    fix_problem(ctx, PR_2_FEATURE_LARGE_FILES, &pctx)) {
274 			sb->s_feature_ro_compat |=
275 				EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
276 			fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
277 			ext2fs_mark_super_dirty(fs);
278 		}
279 		if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
280 		    fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
281 			ext2fs_update_dynamic_rev(fs);
282 			ext2fs_mark_super_dirty(fs);
283 		}
284 	}
285 
286 	print_resource_track(ctx, _("Pass 2"), &rtrack, fs->io);
287 }
288 
289 #define MAX_DEPTH 32000
htree_depth(struct dx_dir_info * dx_dir,struct dx_dirblock_info * dx_db)290 static int htree_depth(struct dx_dir_info *dx_dir,
291 		       struct dx_dirblock_info *dx_db)
292 {
293 	int	depth = 0;
294 
295 	while (dx_db->type != DX_DIRBLOCK_ROOT && depth < MAX_DEPTH) {
296 		dx_db = &dx_dir->dx_block[dx_db->parent];
297 		depth++;
298 	}
299 	return depth;
300 }
301 
dict_de_cmp(const void * a,const void * b)302 static int dict_de_cmp(const void *a, const void *b)
303 {
304 	const struct ext2_dir_entry *de_a, *de_b;
305 	int	a_len, b_len;
306 
307 	de_a = (const struct ext2_dir_entry *) a;
308 	a_len = de_a->name_len & 0xFF;
309 	de_b = (const struct ext2_dir_entry *) b;
310 	b_len = de_b->name_len & 0xFF;
311 
312 	if (a_len != b_len)
313 		return (a_len - b_len);
314 
315 	return memcmp(de_a->name, de_b->name, a_len);
316 }
317 
318 /*
319  * This is special sort function that makes sure that directory blocks
320  * with a dirblock of zero are sorted to the beginning of the list.
321  * This guarantees that the root node of the htree directories are
322  * processed first, so we know what hash version to use.
323  */
special_dir_block_cmp(const void * a,const void * b)324 static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b)
325 {
326 	const struct ext2_db_entry2 *db_a =
327 		(const struct ext2_db_entry2 *) a;
328 	const struct ext2_db_entry2 *db_b =
329 		(const struct ext2_db_entry2 *) b;
330 
331 	if (db_a->blockcnt && !db_b->blockcnt)
332 		return 1;
333 
334 	if (!db_a->blockcnt && db_b->blockcnt)
335 		return -1;
336 
337 	if (db_a->blk != db_b->blk)
338 		return (int) (db_a->blk - db_b->blk);
339 
340 	if (db_a->ino != db_b->ino)
341 		return (int) (db_a->ino - db_b->ino);
342 
343 	return (int) (db_a->blockcnt - db_b->blockcnt);
344 }
345 
346 
347 /*
348  * Make sure the first entry in the directory is '.', and that the
349  * directory entry is sane.
350  */
check_dot(e2fsck_t ctx,struct ext2_dir_entry * dirent,ext2_ino_t ino,struct problem_context * pctx)351 static int check_dot(e2fsck_t ctx,
352 		     struct ext2_dir_entry *dirent,
353 		     ext2_ino_t ino, struct problem_context *pctx)
354 {
355 	struct ext2_dir_entry *nextdir;
356 	unsigned int	rec_len, new_len;
357 	int		status = 0;
358 	int		created = 0;
359 	problem_t	problem = 0;
360 
361 	if (!dirent->inode)
362 		problem = PR_2_MISSING_DOT;
363 	else if (((dirent->name_len & 0xFF) != 1) ||
364 		 (dirent->name[0] != '.'))
365 		problem = PR_2_1ST_NOT_DOT;
366 	else if (dirent->name[1] != '\0')
367 		problem = PR_2_DOT_NULL_TERM;
368 
369 	(void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
370 	if (problem) {
371 		if (fix_problem(ctx, problem, pctx)) {
372 			if (rec_len < 12)
373 				rec_len = dirent->rec_len = 12;
374 			dirent->inode = ino;
375 			dirent->name_len = 1;
376 			dirent->name[0] = '.';
377 			dirent->name[1] = '\0';
378 			status = 1;
379 			created = 1;
380 		}
381 	}
382 	if (dirent->inode != ino) {
383 		if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
384 			dirent->inode = ino;
385 			status = 1;
386 		}
387 	}
388 	if (rec_len > 12) {
389 		new_len = rec_len - 12;
390 		if (new_len > 12) {
391 			if (created ||
392 			    fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
393 				nextdir = (struct ext2_dir_entry *)
394 					((char *) dirent + 12);
395 				dirent->rec_len = 12;
396 				(void) ext2fs_set_rec_len(ctx->fs, new_len,
397 							  nextdir);
398 				nextdir->inode = 0;
399 				nextdir->name_len = 0;
400 				status = 1;
401 			}
402 		}
403 	}
404 	return status;
405 }
406 
407 /*
408  * Make sure the second entry in the directory is '..', and that the
409  * directory entry is sane.  We do not check the inode number of '..'
410  * here; this gets done in pass 3.
411  */
check_dotdot(e2fsck_t ctx,struct ext2_dir_entry * dirent,ext2_ino_t ino,struct problem_context * pctx)412 static int check_dotdot(e2fsck_t ctx,
413 			struct ext2_dir_entry *dirent,
414 			ext2_ino_t ino, struct problem_context *pctx)
415 {
416 	problem_t	problem = 0;
417 	unsigned int	rec_len;
418 
419 	if (!dirent->inode)
420 		problem = PR_2_MISSING_DOT_DOT;
421 	else if (((dirent->name_len & 0xFF) != 2) ||
422 		 (dirent->name[0] != '.') ||
423 		 (dirent->name[1] != '.'))
424 		problem = PR_2_2ND_NOT_DOT_DOT;
425 	else if (dirent->name[2] != '\0')
426 		problem = PR_2_DOT_DOT_NULL_TERM;
427 
428 	(void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
429 	if (problem) {
430 		if (fix_problem(ctx, problem, pctx)) {
431 			if (rec_len < 12)
432 				dirent->rec_len = 12;
433 			/*
434 			 * Note: we don't have the parent inode just
435 			 * yet, so we will fill it in with the root
436 			 * inode.  This will get fixed in pass 3.
437 			 */
438 			dirent->inode = EXT2_ROOT_INO;
439 			dirent->name_len = 2;
440 			dirent->name[0] = '.';
441 			dirent->name[1] = '.';
442 			dirent->name[2] = '\0';
443 			return 1;
444 		}
445 		return 0;
446 	}
447 	if (e2fsck_dir_info_set_dotdot(ctx, ino, dirent->inode)) {
448 		fix_problem(ctx, PR_2_NO_DIRINFO, pctx);
449 		return -1;
450 	}
451 	return 0;
452 }
453 
454 /*
455  * Check to make sure a directory entry doesn't contain any illegal
456  * characters.
457  */
check_name(e2fsck_t ctx,struct ext2_dir_entry * dirent,struct problem_context * pctx)458 static int check_name(e2fsck_t ctx,
459 		      struct ext2_dir_entry *dirent,
460 		      struct problem_context *pctx)
461 {
462 	int	i;
463 	int	fixup = -1;
464 	int	ret = 0;
465 
466 	for ( i = 0; i < (dirent->name_len & 0xFF); i++) {
467 		if (dirent->name[i] != '/' && dirent->name[i] != '\0')
468 			continue;
469 		if (fixup < 0)
470 			fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
471 		if (fixup == 0)
472 			return 0;
473 		dirent->name[i] = '.';
474 		ret = 1;
475 	}
476 	return ret;
477 }
478 
encrypted_check_name(e2fsck_t ctx,struct ext2_dir_entry * dirent,struct problem_context * pctx)479 static int encrypted_check_name(e2fsck_t ctx,
480 				struct ext2_dir_entry *dirent,
481 				struct problem_context *pctx)
482 {
483 	if ((dirent->name_len & 0xff) < EXT4_CRYPTO_BLOCK_SIZE) {
484 		if (fix_problem(ctx, PR_2_BAD_ENCRYPTED_NAME, pctx)) {
485 			dirent->inode = 0;
486 			return 1;
487 		}
488 		ext2fs_unmark_valid(ctx->fs);
489 	}
490 	return 0;
491 }
492 
493 /*
494  * Check the directory filetype (if present)
495  */
check_filetype(e2fsck_t ctx,struct ext2_dir_entry * dirent,ext2_ino_t dir_ino EXT2FS_ATTR ((unused)),struct problem_context * pctx)496 static _INLINE_ int check_filetype(e2fsck_t ctx,
497 				   struct ext2_dir_entry *dirent,
498 				   ext2_ino_t dir_ino EXT2FS_ATTR((unused)),
499 				   struct problem_context *pctx)
500 {
501 	int	filetype = dirent->name_len >> 8;
502 	int	should_be = EXT2_FT_UNKNOWN;
503 	struct ext2_inode	inode;
504 
505 	if (!(ctx->fs->super->s_feature_incompat &
506 	      EXT2_FEATURE_INCOMPAT_FILETYPE)) {
507 		if (filetype == 0 ||
508 		    !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx))
509 			return 0;
510 		dirent->name_len = dirent->name_len & 0xFF;
511 		return 1;
512 	}
513 
514 	if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dirent->inode)) {
515 		should_be = EXT2_FT_DIR;
516 	} else if (ext2fs_test_inode_bitmap2(ctx->inode_reg_map,
517 					    dirent->inode)) {
518 		should_be = EXT2_FT_REG_FILE;
519 	} else if (ctx->inode_bad_map &&
520 		   ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
521 					    dirent->inode))
522 		should_be = 0;
523 	else {
524 		e2fsck_read_inode(ctx, dirent->inode, &inode,
525 				  "check_filetype");
526 		should_be = ext2_file_type(inode.i_mode);
527 	}
528 	if (filetype == should_be)
529 		return 0;
530 	pctx->num = should_be;
531 
532 	if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE,
533 			pctx) == 0)
534 		return 0;
535 
536 	dirent->name_len = (dirent->name_len & 0xFF) | should_be << 8;
537 	return 1;
538 }
539 
540 #ifdef ENABLE_HTREE
parse_int_node(ext2_filsys fs,struct ext2_db_entry2 * db,struct check_dir_struct * cd,struct dx_dir_info * dx_dir,char * block_buf)541 static void parse_int_node(ext2_filsys fs,
542 			   struct ext2_db_entry2 *db,
543 			   struct check_dir_struct *cd,
544 			   struct dx_dir_info	*dx_dir,
545 			   char *block_buf)
546 {
547 	struct 		ext2_dx_root_info  *root;
548 	struct 		ext2_dx_entry *ent;
549 	struct		ext2_dx_countlimit *limit;
550 	struct dx_dirblock_info	*dx_db;
551 	int		i, expect_limit, count;
552 	blk_t		blk;
553 	ext2_dirhash_t	min_hash = 0xffffffff;
554 	ext2_dirhash_t	max_hash = 0;
555 	ext2_dirhash_t	hash = 0, prev_hash;
556 
557 	if (db->blockcnt == 0) {
558 		root = (struct ext2_dx_root_info *) (block_buf + 24);
559 
560 #ifdef DX_DEBUG
561 		printf("Root node dump:\n");
562 		printf("\t Reserved zero: %u\n", root->reserved_zero);
563 		printf("\t Hash Version: %d\n", root->hash_version);
564 		printf("\t Info length: %d\n", root->info_length);
565 		printf("\t Indirect levels: %d\n", root->indirect_levels);
566 		printf("\t Flags: %d\n", root->unused_flags);
567 #endif
568 
569 		ent = (struct ext2_dx_entry *) (block_buf + 24 + root->info_length);
570 	} else {
571 		ent = (struct ext2_dx_entry *) (block_buf+8);
572 	}
573 	limit = (struct ext2_dx_countlimit *) ent;
574 
575 #ifdef DX_DEBUG
576 	printf("Number of entries (count): %d\n",
577 	       ext2fs_le16_to_cpu(limit->count));
578 	printf("Number of entries (limit): %d\n",
579 	       ext2fs_le16_to_cpu(limit->limit));
580 #endif
581 
582 	count = ext2fs_le16_to_cpu(limit->count);
583 	expect_limit = (fs->blocksize - ((char *) ent - block_buf)) /
584 		sizeof(struct ext2_dx_entry);
585 	if (ext2fs_le16_to_cpu(limit->limit) != expect_limit) {
586 		cd->pctx.num = ext2fs_le16_to_cpu(limit->limit);
587 		if (fix_problem(cd->ctx, PR_2_HTREE_BAD_LIMIT, &cd->pctx))
588 			goto clear_and_exit;
589 	}
590 	if (count > expect_limit) {
591 		cd->pctx.num = count;
592 		if (fix_problem(cd->ctx, PR_2_HTREE_BAD_COUNT, &cd->pctx))
593 			goto clear_and_exit;
594 		count = expect_limit;
595 	}
596 
597 	for (i=0; i < count; i++) {
598 		prev_hash = hash;
599 		hash = i ? (ext2fs_le32_to_cpu(ent[i].hash) & ~1) : 0;
600 #ifdef DX_DEBUG
601 		printf("Entry #%d: Hash 0x%08x, block %u\n", i,
602 		       hash, ext2fs_le32_to_cpu(ent[i].block));
603 #endif
604 		blk = ext2fs_le32_to_cpu(ent[i].block) & 0x0ffffff;
605 		/* Check to make sure the block is valid */
606 		if (blk >= (blk_t) dx_dir->numblocks) {
607 			cd->pctx.blk = blk;
608 			if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK,
609 					&cd->pctx))
610 				goto clear_and_exit;
611 			continue;
612 		}
613 		if (hash < prev_hash &&
614 		    fix_problem(cd->ctx, PR_2_HTREE_HASH_ORDER, &cd->pctx))
615 			goto clear_and_exit;
616 		dx_db = &dx_dir->dx_block[blk];
617 		if (dx_db->flags & DX_FLAG_REFERENCED) {
618 			dx_db->flags |= DX_FLAG_DUP_REF;
619 		} else {
620 			dx_db->flags |= DX_FLAG_REFERENCED;
621 			dx_db->parent = db->blockcnt;
622 		}
623 		if (hash < min_hash)
624 			min_hash = hash;
625 		if (hash > max_hash)
626 			max_hash = hash;
627 		dx_db->node_min_hash = hash;
628 		if ((i+1) < count)
629 			dx_db->node_max_hash =
630 			  ext2fs_le32_to_cpu(ent[i+1].hash) & ~1;
631 		else {
632 			dx_db->node_max_hash = 0xfffffffe;
633 			dx_db->flags |= DX_FLAG_LAST;
634 		}
635 		if (i == 0)
636 			dx_db->flags |= DX_FLAG_FIRST;
637 	}
638 #ifdef DX_DEBUG
639 	printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n",
640 	       db->blockcnt, min_hash, max_hash);
641 #endif
642 	dx_db = &dx_dir->dx_block[db->blockcnt];
643 	dx_db->min_hash = min_hash;
644 	dx_db->max_hash = max_hash;
645 	return;
646 
647 clear_and_exit:
648 	clear_htree(cd->ctx, cd->pctx.ino);
649 	dx_dir->numblocks = 0;
650 }
651 #endif /* ENABLE_HTREE */
652 
653 /*
654  * Given a busted directory, try to salvage it somehow.
655  *
656  */
salvage_directory(ext2_filsys fs,struct ext2_dir_entry * dirent,struct ext2_dir_entry * prev,unsigned int * offset)657 static void salvage_directory(ext2_filsys fs,
658 			      struct ext2_dir_entry *dirent,
659 			      struct ext2_dir_entry *prev,
660 			      unsigned int *offset)
661 {
662 	char	*cp = (char *) dirent;
663 	int left;
664 	unsigned int rec_len, prev_rec_len;
665 	unsigned int name_len = dirent->name_len & 0xFF;
666 
667 	(void) ext2fs_get_rec_len(fs, dirent, &rec_len);
668 	left = fs->blocksize - *offset - rec_len;
669 
670 	/*
671 	 * Special case of directory entry of size 8: copy what's left
672 	 * of the directory block up to cover up the invalid hole.
673 	 */
674 	if ((left >= 12) && (rec_len == 8)) {
675 		memmove(cp, cp+8, left);
676 		memset(cp + left, 0, 8);
677 		return;
678 	}
679 	/*
680 	 * If the directory entry overruns the end of the directory
681 	 * block, and the name is small enough to fit, then adjust the
682 	 * record length.
683 	 */
684 	if ((left < 0) &&
685 	    ((int) rec_len + left > 8) &&
686 	    ((int) name_len + 8 <= (int) rec_len + left) &&
687 	    dirent->inode <= fs->super->s_inodes_count &&
688 	    strnlen(dirent->name, name_len) == name_len) {
689 		(void) ext2fs_set_rec_len(fs, (int) rec_len + left, dirent);
690 		return;
691 	}
692 	/*
693 	 * If the record length of the directory entry is a multiple
694 	 * of four, and not too big, such that it is valid, let the
695 	 * previous directory entry absorb the invalid one.
696 	 */
697 	if (prev && rec_len && (rec_len % 4) == 0 &&
698 	    (*offset + rec_len <= fs->blocksize)) {
699 		(void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
700 		prev_rec_len += rec_len;
701 		(void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
702 		*offset += rec_len;
703 		return;
704 	}
705 	/*
706 	 * Default salvage method --- kill all of the directory
707 	 * entries for the rest of the block.  We will either try to
708 	 * absorb it into the previous directory entry, or create a
709 	 * new empty directory entry the rest of the directory block.
710 	 */
711 	if (prev) {
712 		(void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
713 		prev_rec_len += fs->blocksize - *offset;
714 		(void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
715 		*offset = fs->blocksize;
716 	} else {
717 		rec_len = fs->blocksize - *offset;
718 		(void) ext2fs_set_rec_len(fs, rec_len, dirent);
719 		dirent->name_len = 0;
720 		dirent->inode = 0;
721 	}
722 }
723 
check_dir_block(ext2_filsys fs,struct ext2_db_entry2 * db,void * priv_data)724 static int check_dir_block(ext2_filsys fs,
725 			   struct ext2_db_entry2 *db,
726 			   void *priv_data)
727 {
728  	struct dx_dir_info	*dx_dir;
729 #ifdef ENABLE_HTREE
730 	struct dx_dirblock_info	*dx_db = 0;
731 #endif /* ENABLE_HTREE */
732 	struct ext2_dir_entry 	*dirent, *prev;
733 	ext2_dirhash_t		hash;
734 	unsigned int		offset = 0;
735 	int			dir_modified = 0;
736 	int			dot_state;
737 	unsigned int		rec_len;
738 	blk64_t			block_nr = db->blk;
739 	ext2_ino_t 		ino = db->ino;
740 	ext2_ino_t 		subdir_parent;
741 	__u16			links;
742 	struct check_dir_struct	*cd;
743 	char 			*buf;
744 	e2fsck_t		ctx;
745 	problem_t		problem;
746 	struct ext2_dx_root_info *root;
747 	struct ext2_dx_countlimit *limit;
748 	static dict_t de_dict;
749 	struct problem_context	pctx;
750 	int	dups_found = 0;
751 	int	encrypted = 0;
752 	int	ret;
753 
754 	cd = (struct check_dir_struct *) priv_data;
755 	buf = cd->buf;
756 	ctx = cd->ctx;
757 
758 	if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
759 		return DIRENT_ABORT;
760 
761 	if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max))
762 		return DIRENT_ABORT;
763 
764 	/*
765 	 * Make sure the inode is still in use (could have been
766 	 * deleted in the duplicate/bad blocks pass.
767 	 */
768 	if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino)))
769 		return 0;
770 
771 	cd->pctx.ino = ino;
772 	cd->pctx.blk = block_nr;
773 	cd->pctx.blkcount = db->blockcnt;
774 	cd->pctx.ino2 = 0;
775 	cd->pctx.dirent = 0;
776 	cd->pctx.num = 0;
777 
778 	if (db->blk == 0) {
779 		if (allocate_dir_block(ctx, db, buf, &cd->pctx))
780 			return 0;
781 		block_nr = db->blk;
782 	}
783 
784 	if (db->blockcnt)
785 		dot_state = 2;
786 	else
787 		dot_state = 0;
788 
789 	if (ctx->dirs_to_hash &&
790 	    ext2fs_u32_list_test(ctx->dirs_to_hash, ino))
791 		dups_found++;
792 
793 #if 0
794 	printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
795 	       db->blockcnt, ino);
796 #endif
797 
798 	ehandler_operation(_("reading directory block"));
799 	cd->pctx.errcode = ext2fs_read_dir_block3(fs, block_nr, buf, 0);
800 	ehandler_operation(0);
801 	if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED)
802 		cd->pctx.errcode = 0; /* We'll handle this ourselves */
803 	if (cd->pctx.errcode) {
804 		if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
805 			ctx->flags |= E2F_FLAG_ABORT;
806 			return DIRENT_ABORT;
807 		}
808 		memset(buf, 0, fs->blocksize);
809 	}
810 #ifdef ENABLE_HTREE
811 	dx_dir = e2fsck_get_dx_dir_info(ctx, ino);
812 	if (dx_dir && dx_dir->numblocks) {
813 		if (db->blockcnt >= dx_dir->numblocks) {
814 			if (fix_problem(ctx, PR_2_UNEXPECTED_HTREE_BLOCK,
815 					&pctx)) {
816 				clear_htree(ctx, ino);
817 				dx_dir->numblocks = 0;
818 				dx_db = 0;
819 				goto out_htree;
820 			}
821 			fatal_error(ctx, _("Can not continue."));
822 		}
823 		dx_db = &dx_dir->dx_block[db->blockcnt];
824 		dx_db->type = DX_DIRBLOCK_LEAF;
825 		dx_db->phys = block_nr;
826 		dx_db->min_hash = ~0;
827 		dx_db->max_hash = 0;
828 
829 		dirent = (struct ext2_dir_entry *) buf;
830 		(void) ext2fs_get_rec_len(fs, dirent, &rec_len);
831 		limit = (struct ext2_dx_countlimit *) (buf+8);
832 		if (db->blockcnt == 0) {
833 			root = (struct ext2_dx_root_info *) (buf + 24);
834 			dx_db->type = DX_DIRBLOCK_ROOT;
835 			dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST;
836 			if ((root->reserved_zero ||
837 			     root->info_length < 8 ||
838 			     root->indirect_levels > 1) &&
839 			    fix_problem(ctx, PR_2_HTREE_BAD_ROOT, &cd->pctx)) {
840 				clear_htree(ctx, ino);
841 				dx_dir->numblocks = 0;
842 				dx_db = 0;
843 			}
844 			dx_dir->hashversion = root->hash_version;
845 			if ((dx_dir->hashversion <= EXT2_HASH_TEA) &&
846 			    (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
847 				dx_dir->hashversion += 3;
848 			dx_dir->depth = root->indirect_levels + 1;
849 		} else if ((dirent->inode == 0) &&
850 			   (rec_len == fs->blocksize) &&
851 			   (dirent->name_len == 0) &&
852 			   (ext2fs_le16_to_cpu(limit->limit) ==
853 			    ((fs->blocksize-8) /
854 			     sizeof(struct ext2_dx_entry))))
855 			dx_db->type = DX_DIRBLOCK_NODE;
856 	}
857 out_htree:
858 #endif /* ENABLE_HTREE */
859 
860 	if (ctx->encrypted_dirs)
861 		encrypted = ext2fs_u32_list_test(ctx->encrypted_dirs, ino);
862 
863 	dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cmp);
864 	prev = 0;
865 	do {
866 		dgrp_t group;
867 		ext2_ino_t first_unused_inode;
868 
869 		problem = 0;
870 		dirent = (struct ext2_dir_entry *) (buf + offset);
871 		(void) ext2fs_get_rec_len(fs, dirent, &rec_len);
872 		cd->pctx.dirent = dirent;
873 		cd->pctx.num = offset;
874 		if (((offset + rec_len) > fs->blocksize) ||
875 		    (rec_len < 12) ||
876 		    ((rec_len % 4) != 0) ||
877 		    (((dirent->name_len & (unsigned) 0xFF)+8) > rec_len)) {
878 			if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
879 				salvage_directory(fs, dirent, prev, &offset);
880 				dir_modified++;
881 				continue;
882 			} else
883 				goto abort_free_dict;
884 		}
885 
886 		if (dot_state == 0) {
887 			if (check_dot(ctx, dirent, ino, &cd->pctx))
888 				dir_modified++;
889 		} else if (dot_state == 1) {
890 			ret = check_dotdot(ctx, dirent, ino, &cd->pctx);
891 			if (ret < 0)
892 				goto abort_free_dict;
893 			if (ret)
894 				dir_modified++;
895 		} else if (dirent->inode == ino) {
896 			problem = PR_2_LINK_DOT;
897 			if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
898 				dirent->inode = 0;
899 				dir_modified++;
900 				goto next;
901 			}
902 		}
903 		if (!dirent->inode)
904 			goto next;
905 
906 		/*
907 		 * Make sure the inode listed is a legal one.
908 		 */
909 		if (((dirent->inode != EXT2_ROOT_INO) &&
910 		     (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
911 		    (dirent->inode > fs->super->s_inodes_count)) {
912 			problem = PR_2_BAD_INO;
913 		} else if (ctx->inode_bb_map &&
914 			   (ext2fs_test_inode_bitmap2(ctx->inode_bb_map,
915 						     dirent->inode))) {
916 			/*
917 			 * If the inode is in a bad block, offer to
918 			 * clear it.
919 			 */
920 			problem = PR_2_BB_INODE;
921 		} else if ((dot_state > 1) &&
922 			   ((dirent->name_len & 0xFF) == 1) &&
923 			   (dirent->name[0] == '.')) {
924 			/*
925 			 * If there's a '.' entry in anything other
926 			 * than the first directory entry, it's a
927 			 * duplicate entry that should be removed.
928 			 */
929 			problem = PR_2_DUP_DOT;
930 		} else if ((dot_state > 1) &&
931 			   ((dirent->name_len & 0xFF) == 2) &&
932 			   (dirent->name[0] == '.') &&
933 			   (dirent->name[1] == '.')) {
934 			/*
935 			 * If there's a '..' entry in anything other
936 			 * than the second directory entry, it's a
937 			 * duplicate entry that should be removed.
938 			 */
939 			problem = PR_2_DUP_DOT_DOT;
940 		} else if ((dot_state > 1) &&
941 			   (dirent->inode == EXT2_ROOT_INO)) {
942 			/*
943 			 * Don't allow links to the root directory.
944 			 * We check this specially to make sure we
945 			 * catch this error case even if the root
946 			 * directory hasn't been created yet.
947 			 */
948 			problem = PR_2_LINK_ROOT;
949 		} else if ((dot_state > 1) &&
950 			   (dirent->name_len & 0xFF) == 0) {
951 			/*
952 			 * Don't allow zero-length directory names.
953 			 */
954 			problem = PR_2_NULL_NAME;
955 		}
956 
957 		if (problem) {
958 			if (fix_problem(ctx, problem, &cd->pctx)) {
959 				dirent->inode = 0;
960 				dir_modified++;
961 				goto next;
962 			} else {
963 				ext2fs_unmark_valid(fs);
964 				if (problem == PR_2_BAD_INO)
965 					goto next;
966 			}
967 		}
968 
969 		/*
970 		 * If the inode was marked as having bad fields in
971 		 * pass1, process it and offer to fix/clear it.
972 		 * (We wait until now so that we can display the
973 		 * pathname to the user.)
974 		 */
975 		if (ctx->inode_bad_map &&
976 		    ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
977 					     dirent->inode)) {
978 			if (e2fsck_process_bad_inode(ctx, ino,
979 						     dirent->inode,
980 						     buf + fs->blocksize)) {
981 				dirent->inode = 0;
982 				dir_modified++;
983 				goto next;
984 			}
985 			if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
986 				return DIRENT_ABORT;
987 		}
988 
989 		group = ext2fs_group_of_ino(fs, dirent->inode);
990 		first_unused_inode = group * fs->super->s_inodes_per_group +
991 					1 + fs->super->s_inodes_per_group -
992 					ext2fs_bg_itable_unused(fs, group);
993 		cd->pctx.group = group;
994 
995 		/*
996 		 * Check if the inode was missed out because
997 		 * _INODE_UNINIT flag was set or bg_itable_unused was
998 		 * incorrect.  If so, clear the _INODE_UNINIT flag and
999 		 * restart e2fsck.  In the future it would be nice if
1000 		 * we could call a function in pass1.c that checks the
1001 		 * newly visible inodes.
1002 		 */
1003 		if (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)) {
1004 			pctx.num = dirent->inode;
1005 			if (fix_problem(ctx, PR_2_INOREF_BG_INO_UNINIT,
1006 					&cd->pctx)){
1007 				ext2fs_bg_flags_clear(fs, group,
1008 						      EXT2_BG_INODE_UNINIT);
1009 				ext2fs_mark_super_dirty(fs);
1010 				ctx->flags |= E2F_FLAG_RESTART_LATER;
1011 			} else {
1012 				ext2fs_unmark_valid(fs);
1013 				if (problem == PR_2_BAD_INO)
1014 					goto next;
1015 			}
1016 		} else if (dirent->inode >= first_unused_inode) {
1017 			pctx.num = dirent->inode;
1018 			if (fix_problem(ctx, PR_2_INOREF_IN_UNUSED, &cd->pctx)){
1019 				ext2fs_bg_itable_unused_set(fs, group, 0);
1020 				ext2fs_mark_super_dirty(fs);
1021 				ctx->flags |= E2F_FLAG_RESTART_LATER;
1022 			} else {
1023 				ext2fs_unmark_valid(fs);
1024 				if (problem == PR_2_BAD_INO)
1025 					goto next;
1026 			}
1027 		}
1028 
1029 		/*
1030 		 * Offer to clear unused inodes; if we are going to be
1031 		 * restarting the scan due to bg_itable_unused being
1032 		 * wrong, then don't clear any inodes to avoid zapping
1033 		 * inodes that were skipped during pass1 due to an
1034 		 * incorrect bg_itable_unused; we'll get any real
1035 		 * problems after we restart.
1036 		 */
1037 		if (!(ctx->flags & E2F_FLAG_RESTART_LATER) &&
1038 		    !(ext2fs_test_inode_bitmap2(ctx->inode_used_map,
1039 						dirent->inode)))
1040 			problem = PR_2_UNUSED_INODE;
1041 
1042 		if (problem) {
1043 			if (fix_problem(ctx, problem, &cd->pctx)) {
1044 				dirent->inode = 0;
1045 				dir_modified++;
1046 				goto next;
1047 			} else {
1048 				ext2fs_unmark_valid(fs);
1049 				if (problem == PR_2_BAD_INO)
1050 					goto next;
1051 			}
1052 		}
1053 
1054 		if (!encrypted && check_name(ctx, dirent, &cd->pctx))
1055 			dir_modified++;
1056 
1057 		if (encrypted && (dot_state) > 1 &&
1058 		    encrypted_check_name(ctx, dirent, &cd->pctx)) {
1059 			dir_modified++;
1060 			goto next;
1061 		}
1062 
1063 		if (check_filetype(ctx, dirent, ino, &cd->pctx))
1064 			dir_modified++;
1065 
1066 #ifdef ENABLE_HTREE
1067 		if (dx_db) {
1068 			ext2fs_dirhash(dx_dir->hashversion, dirent->name,
1069 				       (dirent->name_len & 0xFF),
1070 				       fs->super->s_hash_seed, &hash, 0);
1071 			if (hash < dx_db->min_hash)
1072 				dx_db->min_hash = hash;
1073 			if (hash > dx_db->max_hash)
1074 				dx_db->max_hash = hash;
1075 		}
1076 #endif
1077 
1078 		/*
1079 		 * If this is a directory, then mark its parent in its
1080 		 * dir_info structure.  If the parent field is already
1081 		 * filled in, then this directory has more than one
1082 		 * hard link.  We assume the first link is correct,
1083 		 * and ask the user if he/she wants to clear this one.
1084 		 */
1085 		if ((dot_state > 1) &&
1086 		    (ext2fs_test_inode_bitmap2(ctx->inode_dir_map,
1087 					      dirent->inode))) {
1088 			if (e2fsck_dir_info_get_parent(ctx, dirent->inode,
1089 						       &subdir_parent)) {
1090 				cd->pctx.ino = dirent->inode;
1091 				fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
1092 				goto abort_free_dict;
1093 			}
1094 			if (subdir_parent) {
1095 				cd->pctx.ino2 = subdir_parent;
1096 				if (fix_problem(ctx, PR_2_LINK_DIR,
1097 						&cd->pctx)) {
1098 					dirent->inode = 0;
1099 					dir_modified++;
1100 					goto next;
1101 				}
1102 				cd->pctx.ino2 = 0;
1103 			} else {
1104 				(void) e2fsck_dir_info_set_parent(ctx,
1105 						  dirent->inode, ino);
1106 			}
1107 		}
1108 
1109 		if (dups_found) {
1110 			;
1111 		} else if (dict_lookup(&de_dict, dirent)) {
1112 			clear_problem_context(&pctx);
1113 			pctx.ino = ino;
1114 			pctx.dirent = dirent;
1115 			fix_problem(ctx, PR_2_REPORT_DUP_DIRENT, &pctx);
1116 			if (!ctx->dirs_to_hash)
1117 				ext2fs_u32_list_create(&ctx->dirs_to_hash, 50);
1118 			if (ctx->dirs_to_hash)
1119 				ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1120 			dups_found++;
1121 		} else
1122 			dict_alloc_insert(&de_dict, dirent, dirent);
1123 
1124 		ext2fs_icount_increment(ctx->inode_count, dirent->inode,
1125 					&links);
1126 		if (links > 1)
1127 			ctx->fs_links_count++;
1128 		ctx->fs_total_count++;
1129 	next:
1130 		prev = dirent;
1131 		if (dir_modified)
1132 			(void) ext2fs_get_rec_len(fs, dirent, &rec_len);
1133 		offset += rec_len;
1134 		dot_state++;
1135 	} while (offset < fs->blocksize);
1136 #if 0
1137 	printf("\n");
1138 #endif
1139 #ifdef ENABLE_HTREE
1140 	if (dx_db) {
1141 #ifdef DX_DEBUG
1142 		printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n",
1143 		       db->blockcnt, dx_db->type,
1144 		       dx_db->min_hash, dx_db->max_hash);
1145 #endif
1146 		cd->pctx.dir = cd->pctx.ino;
1147 		if ((dx_db->type == DX_DIRBLOCK_ROOT) ||
1148 		    (dx_db->type == DX_DIRBLOCK_NODE))
1149 			parse_int_node(fs, db, cd, dx_dir, buf);
1150 	}
1151 #endif /* ENABLE_HTREE */
1152 	if (offset != fs->blocksize) {
1153 		cd->pctx.num = rec_len - fs->blocksize + offset;
1154 		if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
1155 			dirent->rec_len = cd->pctx.num;
1156 			dir_modified++;
1157 		}
1158 	}
1159 	if (dir_modified) {
1160 		cd->pctx.errcode = ext2fs_write_dir_block3(fs, block_nr, buf, 0);
1161 		if (cd->pctx.errcode) {
1162 			if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
1163 					 &cd->pctx))
1164 				goto abort_free_dict;
1165 		}
1166 		ext2fs_mark_changed(fs);
1167 	}
1168 	dict_free_nodes(&de_dict);
1169 	return 0;
1170 abort_free_dict:
1171 	ctx->flags |= E2F_FLAG_ABORT;
1172 	dict_free_nodes(&de_dict);
1173 	return DIRENT_ABORT;
1174 }
1175 
1176 struct del_block {
1177 	e2fsck_t	ctx;
1178 	e2_blkcnt_t	num;
1179 };
1180 
1181 /*
1182  * This function is called to deallocate a block, and is an interator
1183  * functioned called by deallocate inode via ext2fs_iterate_block().
1184  */
deallocate_inode_block(ext2_filsys fs,blk64_t * block_nr,e2_blkcnt_t blockcnt EXT2FS_ATTR ((unused)),blk64_t ref_block EXT2FS_ATTR ((unused)),int ref_offset EXT2FS_ATTR ((unused)),void * priv_data)1185 static int deallocate_inode_block(ext2_filsys fs,
1186 				  blk64_t	*block_nr,
1187 				  e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1188 				  blk64_t ref_block EXT2FS_ATTR((unused)),
1189 				  int ref_offset EXT2FS_ATTR((unused)),
1190 				  void *priv_data)
1191 {
1192 	struct del_block *p = priv_data;
1193 
1194 	if (HOLE_BLKADDR(*block_nr))
1195 		return 0;
1196 	if ((*block_nr < fs->super->s_first_data_block) ||
1197 	    (*block_nr >= ext2fs_blocks_count(fs->super)))
1198 		return 0;
1199 	ext2fs_unmark_block_bitmap2(p->ctx->block_found_map, *block_nr);
1200 	ext2fs_block_alloc_stats2(fs, *block_nr, -1);
1201 	p->num++;
1202 	return 0;
1203 }
1204 
1205 /*
1206  * This fuction deallocates an inode
1207  */
deallocate_inode(e2fsck_t ctx,ext2_ino_t ino,char * block_buf)1208 static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
1209 {
1210 	ext2_filsys fs = ctx->fs;
1211 	struct ext2_inode	inode;
1212 	struct problem_context	pctx;
1213 	__u32			count;
1214 	struct del_block	del_block;
1215 
1216 	e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1217 	clear_problem_context(&pctx);
1218 	pctx.ino = ino;
1219 
1220 	/*
1221 	 * Fix up the bitmaps...
1222 	 */
1223 	e2fsck_read_bitmaps(ctx);
1224 	ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
1225 
1226 	if (ext2fs_file_acl_block(fs, &inode) &&
1227 	    (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
1228 		pctx.errcode = ext2fs_adjust_ea_refcount2(fs,
1229 					ext2fs_file_acl_block(fs, &inode),
1230 					block_buf, -1, &count);
1231 		if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
1232 			pctx.errcode = 0;
1233 			count = 1;
1234 		}
1235 		if (pctx.errcode) {
1236 			pctx.blk = ext2fs_file_acl_block(fs, &inode);
1237 			fix_problem(ctx, PR_2_ADJ_EA_REFCOUNT, &pctx);
1238 			ctx->flags |= E2F_FLAG_ABORT;
1239 			return;
1240 		}
1241 		if (count == 0) {
1242 			ext2fs_unmark_block_bitmap2(ctx->block_found_map,
1243 					ext2fs_file_acl_block(fs, &inode));
1244 			ext2fs_block_alloc_stats2(fs,
1245 				  ext2fs_file_acl_block(fs, &inode), -1);
1246 		}
1247 		ext2fs_file_acl_block_set(fs, &inode, 0);
1248 	}
1249 
1250 	if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
1251 		goto clear_inode;
1252 
1253 	if (LINUX_S_ISREG(inode.i_mode) &&
1254 	    ext2fs_needs_large_file_feature(EXT2_I_SIZE(&inode)))
1255 		ctx->large_files--;
1256 
1257 	del_block.ctx = ctx;
1258 	del_block.num = 0;
1259 	pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf,
1260 					     deallocate_inode_block,
1261 					     &del_block);
1262 	if (pctx.errcode) {
1263 		fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
1264 		ctx->flags |= E2F_FLAG_ABORT;
1265 		return;
1266 	}
1267 clear_inode:
1268 	/* Inode may have changed by block_iterate, so reread it */
1269 	e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1270 	e2fsck_clear_inode(ctx, ino, &inode, 0, "deallocate_inode");
1271 }
1272 
1273 /*
1274  * This fuction clears the htree flag on an inode
1275  */
clear_htree(e2fsck_t ctx,ext2_ino_t ino)1276 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino)
1277 {
1278 	struct ext2_inode	inode;
1279 
1280 	e2fsck_read_inode(ctx, ino, &inode, "clear_htree");
1281 	inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL;
1282 	e2fsck_write_inode(ctx, ino, &inode, "clear_htree");
1283 	if (ctx->dirs_to_hash)
1284 		ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1285 }
1286 
1287 
e2fsck_process_bad_inode(e2fsck_t ctx,ext2_ino_t dir,ext2_ino_t ino,char * buf)1288 int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
1289 			     ext2_ino_t ino, char *buf)
1290 {
1291 	ext2_filsys fs = ctx->fs;
1292 	struct ext2_inode	inode;
1293 	int			inode_modified = 0;
1294 	int			not_fixed = 0;
1295 	unsigned char		*frag, *fsize;
1296 	struct problem_context	pctx;
1297 	problem_t		problem = 0;
1298 
1299 	e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
1300 
1301 	clear_problem_context(&pctx);
1302 	pctx.ino = ino;
1303 	pctx.dir = dir;
1304 	pctx.inode = &inode;
1305 
1306 	if (ext2fs_file_acl_block(fs, &inode) &&
1307 	    !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
1308 		if (fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
1309 			ext2fs_file_acl_block_set(fs, &inode, 0);
1310 			inode_modified++;
1311 		} else
1312 			not_fixed++;
1313 	}
1314 
1315 	if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
1316 	    !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
1317 	    !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
1318 	    !(LINUX_S_ISSOCK(inode.i_mode)))
1319 		problem = PR_2_BAD_MODE;
1320 	else if (LINUX_S_ISCHR(inode.i_mode)
1321 		 && !e2fsck_pass1_check_device_inode(fs, &inode))
1322 		problem = PR_2_BAD_CHAR_DEV;
1323 	else if (LINUX_S_ISBLK(inode.i_mode)
1324 		 && !e2fsck_pass1_check_device_inode(fs, &inode))
1325 		problem = PR_2_BAD_BLOCK_DEV;
1326 	else if (LINUX_S_ISFIFO(inode.i_mode)
1327 		 && !e2fsck_pass1_check_device_inode(fs, &inode))
1328 		problem = PR_2_BAD_FIFO;
1329 	else if (LINUX_S_ISSOCK(inode.i_mode)
1330 		 && !e2fsck_pass1_check_device_inode(fs, &inode))
1331 		problem = PR_2_BAD_SOCKET;
1332 	else if (LINUX_S_ISLNK(inode.i_mode)
1333 		 && !e2fsck_pass1_check_symlink(fs, ino, &inode, buf)) {
1334 		problem = PR_2_INVALID_SYMLINK;
1335 	}
1336 
1337 	if (problem) {
1338 		if (fix_problem(ctx, problem, &pctx)) {
1339 			deallocate_inode(ctx, ino, 0);
1340 			if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1341 				return 0;
1342 			return 1;
1343 		} else
1344 			not_fixed++;
1345 		problem = 0;
1346 	}
1347 
1348 	if (inode.i_faddr) {
1349 		if (fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
1350 			inode.i_faddr = 0;
1351 			inode_modified++;
1352 		} else
1353 			not_fixed++;
1354 	}
1355 
1356 	switch (fs->super->s_creator_os) {
1357 	    case EXT2_OS_HURD:
1358 		frag = &inode.osd2.hurd2.h_i_frag;
1359 		fsize = &inode.osd2.hurd2.h_i_fsize;
1360 		break;
1361 	    default:
1362 		frag = fsize = 0;
1363 	}
1364 	if (frag && *frag) {
1365 		pctx.num = *frag;
1366 		if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
1367 			*frag = 0;
1368 			inode_modified++;
1369 		} else
1370 			not_fixed++;
1371 		pctx.num = 0;
1372 	}
1373 	if (fsize && *fsize) {
1374 		pctx.num = *fsize;
1375 		if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
1376 			*fsize = 0;
1377 			inode_modified++;
1378 		} else
1379 			not_fixed++;
1380 		pctx.num = 0;
1381 	}
1382 
1383 	if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1384 	    !(fs->super->s_feature_ro_compat &
1385 	      EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1386 	    (inode.osd2.linux2.l_i_blocks_hi != 0)) {
1387 		pctx.num = inode.osd2.linux2.l_i_blocks_hi;
1388 		if (fix_problem(ctx, PR_2_BLOCKS_HI_ZERO, &pctx)) {
1389 			inode.osd2.linux2.l_i_blocks_hi = 0;
1390 			inode_modified++;
1391 		}
1392 	}
1393 
1394 	if (!(fs->super->s_feature_incompat &
1395 	     EXT4_FEATURE_INCOMPAT_64BIT) &&
1396 	    inode.osd2.linux2.l_i_file_acl_high != 0) {
1397 		pctx.num = inode.osd2.linux2.l_i_file_acl_high;
1398 		if (fix_problem(ctx, PR_2_I_FILE_ACL_HI_ZERO, &pctx)) {
1399 			inode.osd2.linux2.l_i_file_acl_high = 0;
1400 			inode_modified++;
1401 		} else
1402 			not_fixed++;
1403 	}
1404 
1405 	if (ext2fs_file_acl_block(fs, &inode) &&
1406 	    ((ext2fs_file_acl_block(fs, &inode) < fs->super->s_first_data_block) ||
1407 	     (ext2fs_file_acl_block(fs, &inode) >= ext2fs_blocks_count(fs->super)))) {
1408 		if (fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) {
1409 			ext2fs_file_acl_block_set(fs, &inode, 0);
1410 			inode_modified++;
1411 		} else
1412 			not_fixed++;
1413 	}
1414 	if (inode.i_dir_acl &&
1415 	    LINUX_S_ISDIR(inode.i_mode)) {
1416 		if (fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
1417 			inode.i_dir_acl = 0;
1418 			inode_modified++;
1419 		} else
1420 			not_fixed++;
1421 	}
1422 
1423 	if (inode_modified)
1424 		e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
1425 	if (!not_fixed && ctx->inode_bad_map)
1426 		ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
1427 	return 0;
1428 }
1429 
1430 
1431 /*
1432  * allocate_dir_block --- this function allocates a new directory
1433  * 	block for a particular inode; this is done if a directory has
1434  * 	a "hole" in it, or if a directory has a illegal block number
1435  * 	that was zeroed out and now needs to be replaced.
1436  */
allocate_dir_block(e2fsck_t ctx,struct ext2_db_entry2 * db,char * buf EXT2FS_ATTR ((unused)),struct problem_context * pctx)1437 static int allocate_dir_block(e2fsck_t ctx,
1438 			      struct ext2_db_entry2 *db,
1439 			      char *buf EXT2FS_ATTR((unused)),
1440 			      struct problem_context *pctx)
1441 {
1442 	ext2_filsys fs = ctx->fs;
1443 	blk64_t			blk;
1444 	char			*block;
1445 	struct ext2_inode	inode;
1446 
1447 	if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
1448 		return 1;
1449 
1450 	/*
1451 	 * Read the inode and block bitmaps in; we'll be messing with
1452 	 * them.
1453 	 */
1454 	e2fsck_read_bitmaps(ctx);
1455 
1456 	/*
1457 	 * First, find a free block
1458 	 */
1459 	pctx->errcode = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
1460 	if (pctx->errcode) {
1461 		pctx->str = "ext2fs_new_block";
1462 		fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1463 		return 1;
1464 	}
1465 	ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1466 	ext2fs_mark_block_bitmap2(fs->block_map, blk);
1467 	ext2fs_mark_bb_dirty(fs);
1468 
1469 	/*
1470 	 * Now let's create the actual data block for the inode
1471 	 */
1472 	if (db->blockcnt)
1473 		pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
1474 	else
1475 		pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
1476 						     EXT2_ROOT_INO, &block);
1477 
1478 	if (pctx->errcode) {
1479 		pctx->str = "ext2fs_new_dir_block";
1480 		fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1481 		return 1;
1482 	}
1483 
1484 	pctx->errcode = ext2fs_write_dir_block3(fs, blk, block, 0);
1485 	ext2fs_free_mem(&block);
1486 	if (pctx->errcode) {
1487 		pctx->str = "ext2fs_write_dir_block";
1488 		fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1489 		return 1;
1490 	}
1491 
1492 	/*
1493 	 * Update the inode block count
1494 	 */
1495 	e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
1496 	ext2fs_iblk_add_blocks(fs, &inode, 1);
1497 	if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
1498 		inode.i_size = (db->blockcnt+1) * fs->blocksize;
1499 	e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
1500 
1501 	/*
1502 	 * Finally, update the block pointers for the inode
1503 	 */
1504 	db->blk = blk;
1505 	pctx->errcode = ext2fs_bmap2(fs, db->ino, &inode, 0, BMAP_SET,
1506 				     db->blockcnt, 0, &blk);
1507 	if (pctx->errcode) {
1508 		pctx->str = "ext2fs_block_iterate";
1509 		fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1510 		return 1;
1511 	}
1512 
1513 	return 0;
1514 }
1515